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