| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/audio/mac/audio_manager_mac.h" | 5 #include "media/audio/mac/audio_manager_mac.h" |
| 6 | 6 |
| 7 #include <CoreAudio/AudioHardware.h> | 7 #include <CoreAudio/AudioHardware.h> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 bool AudioManagerMac::HasAudioOutputDevices() { | 300 bool AudioManagerMac::HasAudioOutputDevices() { |
| 301 return HasAudioHardware(kAudioHardwarePropertyDefaultOutputDevice); | 301 return HasAudioHardware(kAudioHardwarePropertyDefaultOutputDevice); |
| 302 } | 302 } |
| 303 | 303 |
| 304 bool AudioManagerMac::HasAudioInputDevices() { | 304 bool AudioManagerMac::HasAudioInputDevices() { |
| 305 return HasAudioHardware(kAudioHardwarePropertyDefaultInputDevice); | 305 return HasAudioHardware(kAudioHardwarePropertyDefaultInputDevice); |
| 306 } | 306 } |
| 307 | 307 |
| 308 // TODO(xians): There are several places on the OSX specific code which | 308 // TODO(xians): There are several places on the OSX specific code which |
| 309 // could benefit from these helper functions. | 309 // could benefit from these helper functions. |
| 310 bool AudioManagerMac::GetDefaultInputDevice( | 310 bool AudioManagerMac::GetDefaultInputDevice(AudioDeviceID* device) { |
| 311 AudioDeviceID* device) { | |
| 312 return GetDefaultDevice(device, true); | 311 return GetDefaultDevice(device, true); |
| 313 } | 312 } |
| 314 | 313 |
| 315 bool AudioManagerMac::GetDefaultOutputDevice( | 314 bool AudioManagerMac::GetDefaultOutputDevice(AudioDeviceID* device) { |
| 316 AudioDeviceID* device) { | |
| 317 return GetDefaultDevice(device, false); | 315 return GetDefaultDevice(device, false); |
| 318 } | 316 } |
| 319 | 317 |
| 320 bool AudioManagerMac::GetDefaultDevice( | 318 bool AudioManagerMac::GetDefaultDevice(AudioDeviceID* device, bool input) { |
| 321 AudioDeviceID* device, bool input) { | |
| 322 CHECK(device); | 319 CHECK(device); |
| 323 | 320 |
| 324 // Obtain the current output device selected by the user. | 321 // Obtain the current output device selected by the user. |
| 325 AudioObjectPropertyAddress pa; | 322 AudioObjectPropertyAddress pa; |
| 326 pa.mSelector = input ? kAudioHardwarePropertyDefaultInputDevice : | 323 pa.mSelector = input ? kAudioHardwarePropertyDefaultInputDevice : |
| 327 kAudioHardwarePropertyDefaultOutputDevice; | 324 kAudioHardwarePropertyDefaultOutputDevice; |
| 328 pa.mScope = kAudioObjectPropertyScopeGlobal; | 325 pa.mScope = kAudioObjectPropertyScopeGlobal; |
| 329 pa.mElement = kAudioObjectPropertyElementMaster; | 326 pa.mElement = kAudioObjectPropertyElementMaster; |
| 330 | 327 |
| 331 UInt32 size = sizeof(*device); | 328 UInt32 size = sizeof(*device); |
| 332 | 329 OSStatus result = AudioObjectGetPropertyData(kAudioObjectSystemObject, |
| 333 OSStatus result = AudioObjectGetPropertyData( | 330 &pa, |
| 334 kAudioObjectSystemObject, | 331 0, |
| 335 &pa, | 332 0, |
| 336 0, | 333 &size, |
| 337 0, | 334 device); |
| 338 &size, | |
| 339 device); | |
| 340 | 335 |
| 341 if ((result != kAudioHardwareNoError) || (*device == kAudioDeviceUnknown)) { | 336 if ((result != kAudioHardwareNoError) || (*device == kAudioDeviceUnknown)) { |
| 342 DLOG(ERROR) << "Error getting default AudioDevice."; | 337 DLOG(ERROR) << "Error getting default AudioDevice."; |
| 343 return false; | 338 return false; |
| 344 } | 339 } |
| 345 | 340 |
| 346 return true; | 341 return true; |
| 347 } | 342 } |
| 348 | 343 |
| 349 bool AudioManagerMac::GetDefaultOutputChannels( | 344 bool AudioManagerMac::GetDefaultOutputChannels(int* channels) { |
| 350 int* channels) { | |
| 351 AudioDeviceID device; | 345 AudioDeviceID device; |
| 352 if (!GetDefaultOutputDevice(&device)) | 346 if (!GetDefaultOutputDevice(&device)) |
| 353 return false; | 347 return false; |
| 354 | 348 return GetDeviceChannels(device, kAudioDevicePropertyScopeOutput, channels); |
| 355 return GetDeviceChannels(device, | |
| 356 kAudioDevicePropertyScopeOutput, | |
| 357 channels); | |
| 358 } | 349 } |
| 359 | 350 |
| 360 bool AudioManagerMac::GetDeviceChannels( | 351 bool AudioManagerMac::GetDeviceChannels(AudioDeviceID device, |
| 361 AudioDeviceID device, | 352 AudioObjectPropertyScope scope, |
| 362 AudioObjectPropertyScope scope, | 353 int* channels) { |
| 363 int* channels) { | |
| 364 CHECK(channels); | 354 CHECK(channels); |
| 365 | 355 |
| 366 // Get stream configuration. | 356 // Get stream configuration. |
| 367 AudioObjectPropertyAddress pa; | 357 AudioObjectPropertyAddress pa; |
| 368 pa.mSelector = kAudioDevicePropertyStreamConfiguration; | 358 pa.mSelector = kAudioDevicePropertyStreamConfiguration; |
| 369 pa.mScope = scope; | 359 pa.mScope = scope; |
| 370 pa.mElement = kAudioObjectPropertyElementMaster; | 360 pa.mElement = kAudioObjectPropertyElementMaster; |
| 371 | 361 |
| 372 UInt32 size; | 362 UInt32 size; |
| 373 OSStatus result = AudioObjectGetPropertyDataSize(device, &pa, 0, 0, &size); | 363 OSStatus result = AudioObjectGetPropertyDataSize(device, &pa, 0, 0, &size); |
| 374 if (result != noErr || !size) | 364 if (result != noErr || !size) |
| 375 return false; | 365 return false; |
| 376 | 366 |
| 377 // Allocate storage. | 367 // Allocate storage. |
| 378 scoped_ptr<uint8[]> list_storage(new uint8[size]); | 368 scoped_ptr<uint8[]> list_storage(new uint8[size]); |
| 379 AudioBufferList& buffer_list = | 369 AudioBufferList& buffer_list = |
| 380 *reinterpret_cast<AudioBufferList*>(list_storage.get()); | 370 *reinterpret_cast<AudioBufferList*>(list_storage.get()); |
| 381 | 371 |
| 382 result = AudioObjectGetPropertyData( | 372 result = AudioObjectGetPropertyData(device, &pa, 0, 0, &size, &buffer_list); |
| 383 device, | |
| 384 &pa, | |
| 385 0, | |
| 386 0, | |
| 387 &size, | |
| 388 &buffer_list); | |
| 389 if (result != noErr) | 373 if (result != noErr) |
| 390 return false; | 374 return false; |
| 391 | 375 |
| 392 // Determine number of input channels. | 376 // Determine number of input channels. |
| 393 int channels_per_frame = buffer_list.mNumberBuffers > 0 ? | 377 int channels_per_frame = buffer_list.mNumberBuffers > 0 ? |
| 394 buffer_list.mBuffers[0].mNumberChannels : 0; | 378 buffer_list.mBuffers[0].mNumberChannels : 0; |
| 395 if (channels_per_frame == 1 && buffer_list.mNumberBuffers > 1) { | 379 if (channels_per_frame == 1 && buffer_list.mNumberBuffers > 1) { |
| 396 // Non-interleaved. | 380 // Non-interleaved. |
| 397 *channels = buffer_list.mNumberBuffers; | 381 *channels = buffer_list.mNumberBuffers; |
| 398 } else { | 382 } else { |
| 399 // Interleaved. | 383 // Interleaved. |
| 400 *channels = channels_per_frame; | 384 *channels = channels_per_frame; |
| 401 } | 385 } |
| 402 | 386 |
| 403 return true; | 387 return true; |
| 404 } | 388 } |
| 405 | 389 |
| 406 int AudioManagerMac::HardwareSampleRateForDevice(AudioDeviceID device_id) { | 390 int AudioManagerMac::HardwareSampleRateForDevice(AudioDeviceID device_id) { |
| 407 Float64 nominal_sample_rate; | 391 Float64 nominal_sample_rate; |
| 408 UInt32 info_size = sizeof(nominal_sample_rate); | 392 UInt32 info_size = sizeof(nominal_sample_rate); |
| 409 | 393 |
| 410 static const AudioObjectPropertyAddress kNominalSampleRateAddress = { | 394 static const AudioObjectPropertyAddress kNominalSampleRateAddress = { |
| 411 kAudioDevicePropertyNominalSampleRate, | 395 kAudioDevicePropertyNominalSampleRate, |
| 412 kAudioObjectPropertyScopeGlobal, | 396 kAudioObjectPropertyScopeGlobal, |
| 413 kAudioObjectPropertyElementMaster | 397 kAudioObjectPropertyElementMaster |
| 414 }; | 398 }; |
| 415 OSStatus result = AudioObjectGetPropertyData( | 399 OSStatus result = AudioObjectGetPropertyData(device_id, |
| 416 device_id, | 400 &kNominalSampleRateAddress, |
| 417 &kNominalSampleRateAddress, | 401 0, |
| 418 0, | 402 0, |
| 419 0, | 403 &info_size, |
| 420 &info_size, | 404 &nominal_sample_rate); |
| 421 &nominal_sample_rate); | |
| 422 if (result != noErr) { | 405 if (result != noErr) { |
| 423 OSSTATUS_DLOG(WARNING, result) | 406 OSSTATUS_DLOG(WARNING, result) |
| 424 << "Could not get default sample rate for device: " << device_id; | 407 << "Could not get default sample rate for device: " << device_id; |
| 425 return 0; | 408 return 0; |
| 426 } | 409 } |
| 427 | 410 |
| 428 return static_cast<int>(nominal_sample_rate); | 411 return static_cast<int>(nominal_sample_rate); |
| 429 } | 412 } |
| 430 | 413 |
| 431 int AudioManagerMac::HardwareSampleRate() { | 414 int AudioManagerMac::HardwareSampleRate() { |
| (...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 786 void AudioManagerMac::ReleaseInputStream(AudioInputStream* stream) { | 769 void AudioManagerMac::ReleaseInputStream(AudioInputStream* stream) { |
| 787 input_streams_.remove(stream); | 770 input_streams_.remove(stream); |
| 788 AudioManagerBase::ReleaseInputStream(stream); | 771 AudioManagerBase::ReleaseInputStream(stream); |
| 789 } | 772 } |
| 790 | 773 |
| 791 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory) { | 774 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory) { |
| 792 return new AudioManagerMac(audio_log_factory); | 775 return new AudioManagerMac(audio_log_factory); |
| 793 } | 776 } |
| 794 | 777 |
| 795 } // namespace media | 778 } // namespace media |
| OLD | NEW |