| 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 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 } | 257 } |
| 258 | 258 |
| 259 bool AudioManagerMac::HasAudioOutputDevices() { | 259 bool AudioManagerMac::HasAudioOutputDevices() { |
| 260 return HasAudioHardware(kAudioHardwarePropertyDefaultOutputDevice); | 260 return HasAudioHardware(kAudioHardwarePropertyDefaultOutputDevice); |
| 261 } | 261 } |
| 262 | 262 |
| 263 bool AudioManagerMac::HasAudioInputDevices() { | 263 bool AudioManagerMac::HasAudioInputDevices() { |
| 264 return HasAudioHardware(kAudioHardwarePropertyDefaultInputDevice); | 264 return HasAudioHardware(kAudioHardwarePropertyDefaultInputDevice); |
| 265 } | 265 } |
| 266 | 266 |
| 267 // TODO(crogers): There are several places on the OSX specific code which | |
| 268 // could benefit from this helper function. | |
| 269 bool AudioManagerMac::GetDefaultOutputDevice( | |
| 270 AudioDeviceID* device) { | |
| 271 CHECK(device); | |
| 272 | |
| 273 // Obtain the current output device selected by the user. | |
| 274 static const AudioObjectPropertyAddress kAddress = { | |
| 275 kAudioHardwarePropertyDefaultOutputDevice, | |
| 276 kAudioObjectPropertyScopeGlobal, | |
| 277 kAudioObjectPropertyElementMaster | |
| 278 }; | |
| 279 | |
| 280 UInt32 size = sizeof(*device); | |
| 281 | |
| 282 OSStatus result = AudioObjectGetPropertyData( | |
| 283 kAudioObjectSystemObject, | |
| 284 &kAddress, | |
| 285 0, | |
| 286 0, | |
| 287 &size, | |
| 288 device); | |
| 289 | |
| 290 if ((result != kAudioHardwareNoError) || (*device == kAudioDeviceUnknown)) { | |
| 291 DLOG(ERROR) << "Error getting default output AudioDevice."; | |
| 292 return false; | |
| 293 } | |
| 294 | |
| 295 return true; | |
| 296 } | |
| 297 | |
| 298 bool AudioManagerMac::GetDefaultOutputChannels( | |
| 299 int* channels, int* channels_per_frame) { | |
| 300 AudioDeviceID device; | |
| 301 if (!GetDefaultOutputDevice(&device)) | |
| 302 return false; | |
| 303 | |
| 304 return GetDeviceChannels(device, | |
| 305 kAudioDevicePropertyScopeOutput, | |
| 306 channels, | |
| 307 channels_per_frame); | |
| 308 } | |
| 309 | |
| 310 bool AudioManagerMac::GetDeviceChannels( | |
| 311 AudioDeviceID device, | |
| 312 AudioObjectPropertyScope scope, | |
| 313 int* channels, | |
| 314 int* channels_per_frame) { | |
| 315 CHECK(channels); | |
| 316 CHECK(channels_per_frame); | |
| 317 | |
| 318 // Get stream configuration. | |
| 319 AudioObjectPropertyAddress pa; | |
| 320 pa.mSelector = kAudioDevicePropertyStreamConfiguration; | |
| 321 pa.mScope = scope; | |
| 322 pa.mElement = kAudioObjectPropertyElementMaster; | |
| 323 | |
| 324 UInt32 size; | |
| 325 OSStatus result = AudioObjectGetPropertyDataSize(device, &pa, 0, 0, &size); | |
| 326 if (result != noErr || !size) | |
| 327 return false; | |
| 328 | |
| 329 // Allocate storage. | |
| 330 scoped_array<uint8> list_storage(new uint8[size]); | |
| 331 AudioBufferList& buffer_list = | |
| 332 *reinterpret_cast<AudioBufferList*>(list_storage.get()); | |
| 333 | |
| 334 result = AudioObjectGetPropertyData( | |
| 335 device, | |
| 336 &pa, | |
| 337 0, | |
| 338 0, | |
| 339 &size, | |
| 340 &buffer_list); | |
| 341 if (result != noErr) | |
| 342 return false; | |
| 343 | |
| 344 // Determine number of input channels. | |
| 345 *channels_per_frame = buffer_list.mNumberBuffers > 0 ? | |
| 346 buffer_list.mBuffers[0].mNumberChannels : 0; | |
| 347 if (*channels_per_frame == 1 && buffer_list.mNumberBuffers > 1) { | |
| 348 // Non-interleaved. | |
| 349 *channels = buffer_list.mNumberBuffers; | |
| 350 } else { | |
| 351 // Interleaved. | |
| 352 *channels = *channels_per_frame; | |
| 353 } | |
| 354 | |
| 355 return true; | |
| 356 } | |
| 357 | |
| 358 void AudioManagerMac::GetAudioInputDeviceNames( | 267 void AudioManagerMac::GetAudioInputDeviceNames( |
| 359 media::AudioDeviceNames* device_names) { | 268 media::AudioDeviceNames* device_names) { |
| 360 GetAudioDeviceInfo(true, device_names); | 269 GetAudioDeviceInfo(true, device_names); |
| 361 if (!device_names->empty()) { | 270 if (!device_names->empty()) { |
| 362 // Prepend the default device to the list since we always want it to be | 271 // Prepend the default device to the list since we always want it to be |
| 363 // on the top of the list for all platforms. There is no duplicate | 272 // on the top of the list for all platforms. There is no duplicate |
| 364 // counting here since the default device has been abstracted out before. | 273 // counting here since the default device has been abstracted out before. |
| 365 media::AudioDeviceName name; | 274 media::AudioDeviceName name; |
| 366 name.device_name = AudioManagerBase::kDefaultDeviceName; | 275 name.device_name = AudioManagerBase::kDefaultDeviceName; |
| 367 name.unique_id = AudioManagerBase::kDefaultDeviceId; | 276 name.unique_id = AudioManagerBase::kDefaultDeviceId; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 AudioDeviceID audio_device_id = GetAudioDeviceIdByUId(true, device_id); | 326 AudioDeviceID audio_device_id = GetAudioDeviceIdByUId(true, device_id); |
| 418 AudioInputStream* stream = NULL; | 327 AudioInputStream* stream = NULL; |
| 419 if (audio_device_id != kAudioObjectUnknown) | 328 if (audio_device_id != kAudioObjectUnknown) |
| 420 stream = new AUAudioInputStream(this, params, audio_device_id); | 329 stream = new AUAudioInputStream(this, params, audio_device_id); |
| 421 | 330 |
| 422 return stream; | 331 return stream; |
| 423 } | 332 } |
| 424 | 333 |
| 425 AudioParameters AudioManagerMac::GetPreferredOutputStreamParameters( | 334 AudioParameters AudioManagerMac::GetPreferredOutputStreamParameters( |
| 426 const AudioParameters& input_params) { | 335 const AudioParameters& input_params) { |
| 427 int hardware_channels = 2; | 336 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; |
| 428 int hardware_channels_per_frame = 1; | |
| 429 if (!GetDefaultOutputChannels(&hardware_channels, | |
| 430 &hardware_channels_per_frame)) { | |
| 431 // Fallback to stereo. | |
| 432 hardware_channels = 2; | |
| 433 } | |
| 434 | |
| 435 ChannelLayout channel_layout = GuessChannelLayout(hardware_channels); | |
| 436 | |
| 437 int buffer_size = kDefaultLowLatencyBufferSize; | 337 int buffer_size = kDefaultLowLatencyBufferSize; |
| 438 int user_buffer_size = GetUserBufferSize(); | |
| 439 if (user_buffer_size) | |
| 440 buffer_size = user_buffer_size; | |
| 441 | |
| 442 int input_channels = 0; | 338 int input_channels = 0; |
| 443 if (input_params.IsValid()) { | 339 if (input_params.IsValid()) { |
| 340 channel_layout = input_params.channel_layout(); |
| 444 input_channels = input_params.input_channels(); | 341 input_channels = input_params.input_channels(); |
| 445 | 342 |
| 446 if (input_channels > 0) { | 343 if (input_channels > 0) { |
| 447 // TODO(crogers): given the limitations of the AudioOutputStream | 344 // TODO(crogers): given the limitations of the AudioOutputStream |
| 448 // back-ends used with synchronized I/O, we hard-code to stereo. | 345 // back-ends used with synchronized I/O, we hard-code to stereo. |
| 449 // Specifically, this is a limitation of AudioSynchronizedStream which | 346 // Specifically, this is a limitation of AudioSynchronizedStream which |
| 450 // can be removed as part of the work to consolidate these back-ends. | 347 // can be removed as part of the work to consolidate these back-ends. |
| 451 channel_layout = CHANNEL_LAYOUT_STEREO; | 348 channel_layout = CHANNEL_LAYOUT_STEREO; |
| 452 } | 349 } |
| 453 } | 350 } |
| 454 | 351 |
| 455 AudioParameters params( | 352 int user_buffer_size = GetUserBufferSize(); |
| 456 AudioParameters::AUDIO_PCM_LOW_LATENCY, | 353 if (user_buffer_size) |
| 457 channel_layout, | 354 buffer_size = user_buffer_size; |
| 458 input_channels, | |
| 459 AUAudioOutputStream::HardwareSampleRate(), | |
| 460 16, | |
| 461 buffer_size); | |
| 462 | 355 |
| 463 if (channel_layout == CHANNEL_LAYOUT_UNSUPPORTED) | 356 return AudioParameters( |
| 464 params.SetDiscreteChannels(hardware_channels); | 357 AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, input_channels, |
| 465 | 358 AUAudioOutputStream::HardwareSampleRate(), 16, buffer_size); |
| 466 return params; | |
| 467 } | 359 } |
| 468 | 360 |
| 469 void AudioManagerMac::CreateDeviceListener() { | 361 void AudioManagerMac::CreateDeviceListener() { |
| 470 DCHECK(GetMessageLoop()->BelongsToCurrentThread()); | 362 DCHECK(GetMessageLoop()->BelongsToCurrentThread()); |
| 471 output_device_listener_.reset(new AudioDeviceListenerMac(base::Bind( | 363 output_device_listener_.reset(new AudioDeviceListenerMac(base::Bind( |
| 472 &AudioManagerMac::DelayedDeviceChange, base::Unretained(this)))); | 364 &AudioManagerMac::DelayedDeviceChange, base::Unretained(this)))); |
| 473 } | 365 } |
| 474 | 366 |
| 475 void AudioManagerMac::DestroyDeviceListener() { | 367 void AudioManagerMac::DestroyDeviceListener() { |
| 476 DCHECK(GetMessageLoop()->BelongsToCurrentThread()); | 368 DCHECK(GetMessageLoop()->BelongsToCurrentThread()); |
| 477 output_device_listener_.reset(); | 369 output_device_listener_.reset(); |
| 478 } | 370 } |
| 479 | 371 |
| 480 void AudioManagerMac::DelayedDeviceChange() { | 372 void AudioManagerMac::DelayedDeviceChange() { |
| 481 // TODO(dalecurtis): This is ridiculous, but we need to delay device changes | 373 // TODO(dalecurtis): This is ridiculous, but we need to delay device changes |
| 482 // to workaround threading issues with OSX property listener callbacks. See | 374 // to workaround threading issues with OSX property listener callbacks. See |
| 483 // http://crbug.com/158170 | 375 // http://crbug.com/158170 |
| 484 GetMessageLoop()->PostDelayedTask(FROM_HERE, base::Bind( | 376 GetMessageLoop()->PostDelayedTask(FROM_HERE, base::Bind( |
| 485 &AudioManagerMac::NotifyAllOutputDeviceChangeListeners, | 377 &AudioManagerMac::NotifyAllOutputDeviceChangeListeners, |
| 486 base::Unretained(this)), base::TimeDelta::FromSeconds(2)); | 378 base::Unretained(this)), base::TimeDelta::FromSeconds(2)); |
| 487 } | 379 } |
| 488 | 380 |
| 489 AudioManager* CreateAudioManager() { | 381 AudioManager* CreateAudioManager() { |
| 490 return new AudioManagerMac(); | 382 return new AudioManagerMac(); |
| 491 } | 383 } |
| 492 | 384 |
| 493 } // namespace media | 385 } // namespace media |
| OLD | NEW |