| 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" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/mac/mac_logging.h" | 12 #include "base/mac/mac_logging.h" |
| 13 #include "base/mac/scoped_cftyperef.h" | 13 #include "base/mac/scoped_cftyperef.h" |
| 14 #include "base/sys_string_conversions.h" | 14 #include "base/sys_string_conversions.h" |
| 15 #include "media/audio/audio_util.h" | 15 #include "media/audio/audio_parameters.h" |
| 16 #include "media/audio/mac/audio_input_mac.h" | 16 #include "media/audio/mac/audio_input_mac.h" |
| 17 #include "media/audio/mac/audio_low_latency_input_mac.h" | 17 #include "media/audio/mac/audio_low_latency_input_mac.h" |
| 18 #include "media/audio/mac/audio_low_latency_output_mac.h" | 18 #include "media/audio/mac/audio_low_latency_output_mac.h" |
| 19 #include "media/audio/mac/audio_output_mac.h" | 19 #include "media/audio/mac/audio_output_mac.h" |
| 20 #include "media/audio/mac/audio_synchronized_mac.h" | 20 #include "media/audio/mac/audio_synchronized_mac.h" |
| 21 #include "media/audio/mac/audio_unified_mac.h" | 21 #include "media/audio/mac/audio_unified_mac.h" |
| 22 #include "media/base/bind_to_loop.h" | 22 #include "media/base/bind_to_loop.h" |
| 23 #include "media/base/channel_layout.h" |
| 23 #include "media/base/limits.h" | 24 #include "media/base/limits.h" |
| 24 #include "media/base/media_switches.h" | 25 #include "media/base/media_switches.h" |
| 25 | 26 |
| 26 namespace media { | 27 namespace media { |
| 27 | 28 |
| 28 // Maximum number of output streams that can be open simultaneously. | 29 // Maximum number of output streams that can be open simultaneously. |
| 29 static const int kMaxOutputStreams = 50; | 30 static const int kMaxOutputStreams = 50; |
| 30 | 31 |
| 32 // Default buffer size in samples for low-latency input and output streams. |
| 33 static const int kDefaultLowLatencyBufferSize = 128; |
| 34 |
| 31 static bool HasAudioHardware(AudioObjectPropertySelector selector) { | 35 static bool HasAudioHardware(AudioObjectPropertySelector selector) { |
| 32 AudioDeviceID output_device_id = kAudioObjectUnknown; | 36 AudioDeviceID output_device_id = kAudioObjectUnknown; |
| 33 const AudioObjectPropertyAddress property_address = { | 37 const AudioObjectPropertyAddress property_address = { |
| 34 selector, | 38 selector, |
| 35 kAudioObjectPropertyScopeGlobal, // mScope | 39 kAudioObjectPropertyScopeGlobal, // mScope |
| 36 kAudioObjectPropertyElementMaster // mElement | 40 kAudioObjectPropertyElementMaster // mElement |
| 37 }; | 41 }; |
| 38 UInt32 output_device_id_size = static_cast<UInt32>(sizeof(output_device_id)); | 42 UInt32 output_device_id_size = static_cast<UInt32>(sizeof(output_device_id)); |
| 39 OSStatus err = AudioObjectGetPropertyData(kAudioObjectSystemObject, | 43 OSStatus err = AudioObjectGetPropertyData(kAudioObjectSystemObject, |
| 40 &property_address, | 44 &property_address, |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 // Prepend the default device to the list since we always want it to be | 270 // Prepend the default device to the list since we always want it to be |
| 267 // on the top of the list for all platforms. There is no duplicate | 271 // on the top of the list for all platforms. There is no duplicate |
| 268 // counting here since the default device has been abstracted out before. | 272 // counting here since the default device has been abstracted out before. |
| 269 media::AudioDeviceName name; | 273 media::AudioDeviceName name; |
| 270 name.device_name = AudioManagerBase::kDefaultDeviceName; | 274 name.device_name = AudioManagerBase::kDefaultDeviceName; |
| 271 name.unique_id = AudioManagerBase::kDefaultDeviceId; | 275 name.unique_id = AudioManagerBase::kDefaultDeviceId; |
| 272 device_names->push_front(name); | 276 device_names->push_front(name); |
| 273 } | 277 } |
| 274 } | 278 } |
| 275 | 279 |
| 280 AudioParameters AudioManagerMac::GetInputStreamParameters( |
| 281 const std::string& device_id) { |
| 282 // TODO(xians): query the native channel layout for the specific device. |
| 283 return AudioParameters( |
| 284 AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO, |
| 285 AUAudioInputStream::HardwareSampleRate(), 16, |
| 286 kDefaultLowLatencyBufferSize); |
| 287 } |
| 288 |
| 276 AudioOutputStream* AudioManagerMac::MakeLinearOutputStream( | 289 AudioOutputStream* AudioManagerMac::MakeLinearOutputStream( |
| 277 const AudioParameters& params) { | 290 const AudioParameters& params) { |
| 278 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); | 291 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); |
| 279 return new PCMQueueOutAudioOutputStream(this, params); | 292 return new PCMQueueOutAudioOutputStream(this, params); |
| 280 } | 293 } |
| 281 | 294 |
| 282 AudioOutputStream* AudioManagerMac::MakeLowLatencyOutputStream( | 295 AudioOutputStream* AudioManagerMac::MakeLowLatencyOutputStream( |
| 283 const AudioParameters& params) { | 296 const AudioParameters& params) { |
| 284 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); | 297 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); |
| 285 | 298 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 314 // Gets the AudioDeviceID that refers to the AudioOutputDevice with the device | 327 // Gets the AudioDeviceID that refers to the AudioOutputDevice with the device |
| 315 // unique id. This AudioDeviceID is used to set the device for Audio Unit. | 328 // unique id. This AudioDeviceID is used to set the device for Audio Unit. |
| 316 AudioDeviceID audio_device_id = GetAudioDeviceIdByUId(true, device_id); | 329 AudioDeviceID audio_device_id = GetAudioDeviceIdByUId(true, device_id); |
| 317 AudioInputStream* stream = NULL; | 330 AudioInputStream* stream = NULL; |
| 318 if (audio_device_id != kAudioObjectUnknown) | 331 if (audio_device_id != kAudioObjectUnknown) |
| 319 stream = new AUAudioInputStream(this, params, audio_device_id); | 332 stream = new AUAudioInputStream(this, params, audio_device_id); |
| 320 | 333 |
| 321 return stream; | 334 return stream; |
| 322 } | 335 } |
| 323 | 336 |
| 324 AudioParameters AudioManagerMac::GetPreferredLowLatencyOutputStreamParameters( | 337 AudioParameters AudioManagerMac::GetPreferredOutputStreamParameters( |
| 325 const AudioParameters& input_params) { | 338 const AudioParameters& input_params) { |
| 326 if (CommandLine::ForCurrentProcess()->HasSwitch( | 339 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; |
| 327 switches::kEnableWebAudioInput)) { | 340 int input_channels = 0; |
| 328 // TODO(crogers): given the limitations of the AudioOutputStream | 341 if (input_params.IsValid()) { |
| 329 // back-ends used with kEnableWebAudioInput, we hard-code to stereo. | 342 channel_layout = input_params.channel_layout(); |
| 330 // Specifically, this is a limitation of AudioSynchronizedStream which | 343 input_channels = input_params.input_channels(); |
| 331 // can be removed as part of the work to consolidate these back-ends. | 344 |
| 332 return AudioParameters( | 345 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 333 AudioParameters::AUDIO_PCM_LOW_LATENCY, | 346 switches::kEnableWebAudioInput)) { |
| 334 CHANNEL_LAYOUT_STEREO, input_params.input_channels(), | 347 // TODO(crogers): given the limitations of the AudioOutputStream |
| 335 GetAudioHardwareSampleRate(), 16, GetAudioHardwareBufferSize()); | 348 // back-ends used with kEnableWebAudioInput, we hard-code to stereo. |
| 349 // Specifically, this is a limitation of AudioSynchronizedStream which |
| 350 // can be removed as part of the work to consolidate these back-ends. |
| 351 channel_layout = CHANNEL_LAYOUT_STEREO; |
| 352 } |
| 336 } | 353 } |
| 337 | 354 |
| 338 return AudioManagerBase::GetPreferredLowLatencyOutputStreamParameters( | 355 return AudioParameters( |
| 339 input_params); | 356 AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, input_channels, |
| 357 AUAudioOutputStream::HardwareSampleRate(), 16, |
| 358 kDefaultLowLatencyBufferSize); |
| 340 } | 359 } |
| 341 | 360 |
| 342 void AudioManagerMac::CreateDeviceListener() { | 361 void AudioManagerMac::CreateDeviceListener() { |
| 343 DCHECK(GetMessageLoop()->BelongsToCurrentThread()); | 362 DCHECK(GetMessageLoop()->BelongsToCurrentThread()); |
| 344 output_device_listener_.reset(new AudioDeviceListenerMac(base::Bind( | 363 output_device_listener_.reset(new AudioDeviceListenerMac(base::Bind( |
| 345 &AudioManagerMac::DelayedDeviceChange, base::Unretained(this)))); | 364 &AudioManagerMac::DelayedDeviceChange, base::Unretained(this)))); |
| 346 } | 365 } |
| 347 | 366 |
| 348 void AudioManagerMac::DestroyDeviceListener() { | 367 void AudioManagerMac::DestroyDeviceListener() { |
| 349 DCHECK(GetMessageLoop()->BelongsToCurrentThread()); | 368 DCHECK(GetMessageLoop()->BelongsToCurrentThread()); |
| 350 output_device_listener_.reset(); | 369 output_device_listener_.reset(); |
| 351 } | 370 } |
| 352 | 371 |
| 353 void AudioManagerMac::DelayedDeviceChange() { | 372 void AudioManagerMac::DelayedDeviceChange() { |
| 354 // 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 |
| 355 // to workaround threading issues with OSX property listener callbacks. See | 374 // to workaround threading issues with OSX property listener callbacks. See |
| 356 // http://crbug.com/158170 | 375 // http://crbug.com/158170 |
| 357 GetMessageLoop()->PostDelayedTask(FROM_HERE, base::Bind( | 376 GetMessageLoop()->PostDelayedTask(FROM_HERE, base::Bind( |
| 358 &AudioManagerMac::NotifyAllOutputDeviceChangeListeners, | 377 &AudioManagerMac::NotifyAllOutputDeviceChangeListeners, |
| 359 base::Unretained(this)), base::TimeDelta::FromSeconds(2)); | 378 base::Unretained(this)), base::TimeDelta::FromSeconds(2)); |
| 360 } | 379 } |
| 361 | 380 |
| 362 AudioManager* CreateAudioManager() { | 381 AudioManager* CreateAudioManager() { |
| 363 return new AudioManagerMac(); | 382 return new AudioManagerMac(); |
| 364 } | 383 } |
| 365 | 384 |
| 366 } // namespace media | 385 } // namespace media |
| OLD | NEW |