| 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 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 return audio_device_id; | 229 return audio_device_id; |
| 230 } | 230 } |
| 231 | 231 |
| 232 AudioManagerMac::AudioManagerMac() | 232 AudioManagerMac::AudioManagerMac() |
| 233 : current_sample_rate_(0) { | 233 : current_sample_rate_(0) { |
| 234 current_output_device_ = kAudioDeviceUnknown; | 234 current_output_device_ = kAudioDeviceUnknown; |
| 235 | 235 |
| 236 SetMaxOutputStreamsAllowed(kMaxOutputStreams); | 236 SetMaxOutputStreamsAllowed(kMaxOutputStreams); |
| 237 | 237 |
| 238 // Task must be posted last to avoid races from handing out "this" to the | 238 // Task must be posted last to avoid races from handing out "this" to the |
| 239 // audio thread. | 239 // audio thread. Always PostTask even if we're on the right thread since |
| 240 // AudioManager creation is on the startup path and this may be slow. |
| 240 GetMessageLoop()->PostTask(FROM_HERE, base::Bind( | 241 GetMessageLoop()->PostTask(FROM_HERE, base::Bind( |
| 241 &AudioManagerMac::CreateDeviceListener, base::Unretained(this))); | 242 &AudioManagerMac::CreateDeviceListener, base::Unretained(this))); |
| 242 } | 243 } |
| 243 | 244 |
| 244 AudioManagerMac::~AudioManagerMac() { | 245 AudioManagerMac::~AudioManagerMac() { |
| 245 // It's safe to post a task here since Shutdown() will wait for all tasks to | 246 if (GetMessageLoop()->BelongsToCurrentThread()) { |
| 246 // complete before returning. | 247 DestroyDeviceListener(); |
| 247 GetMessageLoop()->PostTask(FROM_HERE, base::Bind( | 248 } else { |
| 248 &AudioManagerMac::DestroyDeviceListener, base::Unretained(this))); | 249 // It's safe to post a task here since Shutdown() will wait for all tasks to |
| 250 // complete before returning. |
| 251 GetMessageLoop()->PostTask(FROM_HERE, base::Bind( |
| 252 &AudioManagerMac::DestroyDeviceListener, base::Unretained(this))); |
| 253 } |
| 249 | 254 |
| 250 Shutdown(); | 255 Shutdown(); |
| 251 } | 256 } |
| 252 | 257 |
| 253 bool AudioManagerMac::HasAudioOutputDevices() { | 258 bool AudioManagerMac::HasAudioOutputDevices() { |
| 254 return HasAudioHardware(kAudioHardwarePropertyDefaultOutputDevice); | 259 return HasAudioHardware(kAudioHardwarePropertyDefaultOutputDevice); |
| 255 } | 260 } |
| 256 | 261 |
| 257 bool AudioManagerMac::HasAudioInputDevices() { | 262 bool AudioManagerMac::HasAudioInputDevices() { |
| 258 return HasAudioHardware(kAudioHardwarePropertyDefaultInputDevice); | 263 return HasAudioHardware(kAudioHardwarePropertyDefaultInputDevice); |
| (...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 543 | 548 |
| 544 void AudioManagerMac::CreateDeviceListener() { | 549 void AudioManagerMac::CreateDeviceListener() { |
| 545 DCHECK(GetMessageLoop()->BelongsToCurrentThread()); | 550 DCHECK(GetMessageLoop()->BelongsToCurrentThread()); |
| 546 | 551 |
| 547 // Get a baseline for the sample-rate and current device, | 552 // Get a baseline for the sample-rate and current device, |
| 548 // so we can intelligently handle device notifications only when necessary. | 553 // so we can intelligently handle device notifications only when necessary. |
| 549 current_sample_rate_ = HardwareSampleRate(); | 554 current_sample_rate_ = HardwareSampleRate(); |
| 550 if (!GetDefaultOutputDevice(¤t_output_device_)) | 555 if (!GetDefaultOutputDevice(¤t_output_device_)) |
| 551 current_output_device_ = kAudioDeviceUnknown; | 556 current_output_device_ = kAudioDeviceUnknown; |
| 552 | 557 |
| 553 output_device_listener_.reset(new AudioDeviceListenerMac(BindToLoop( | 558 output_device_listener_.reset(new AudioDeviceListenerMac(base::Bind( |
| 554 GetMessageLoop(), base::Bind( | 559 &AudioManagerMac::HandleDeviceChanges, base::Unretained(this)))); |
| 555 &AudioManagerMac::HandleDeviceChanges, | |
| 556 base::Unretained(this))))); | |
| 557 } | 560 } |
| 558 | 561 |
| 559 void AudioManagerMac::DestroyDeviceListener() { | 562 void AudioManagerMac::DestroyDeviceListener() { |
| 560 DCHECK(GetMessageLoop()->BelongsToCurrentThread()); | 563 DCHECK(GetMessageLoop()->BelongsToCurrentThread()); |
| 561 output_device_listener_.reset(); | 564 output_device_listener_.reset(); |
| 562 } | 565 } |
| 563 | 566 |
| 564 void AudioManagerMac::HandleDeviceChanges() { | 567 void AudioManagerMac::HandleDeviceChanges() { |
| 565 int new_sample_rate = HardwareSampleRate(); | 568 int new_sample_rate = HardwareSampleRate(); |
| 566 AudioDeviceID new_output_device; | 569 AudioDeviceID new_output_device; |
| 567 GetDefaultOutputDevice(&new_output_device); | 570 GetDefaultOutputDevice(&new_output_device); |
| 568 | 571 |
| 569 if (current_sample_rate_ == new_sample_rate && | 572 if (current_sample_rate_ == new_sample_rate && |
| 570 current_output_device_ == new_output_device) | 573 current_output_device_ == new_output_device) |
| 571 return; | 574 return; |
| 572 | 575 |
| 573 current_sample_rate_ = new_sample_rate; | 576 current_sample_rate_ = new_sample_rate; |
| 574 current_output_device_ = new_output_device; | 577 current_output_device_ = new_output_device; |
| 575 NotifyAllOutputDeviceChangeListeners(); | 578 NotifyAllOutputDeviceChangeListeners(); |
| 576 } | 579 } |
| 577 | 580 |
| 578 AudioManager* CreateAudioManager() { | 581 AudioManager* CreateAudioManager() { |
| 579 return new AudioManagerMac(); | 582 return new AudioManagerMac(); |
| 580 } | 583 } |
| 581 | 584 |
| 582 } // namespace media | 585 } // namespace media |
| OLD | NEW |