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 548 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
559 const AudioParameters& params, | 559 const AudioParameters& params, |
560 const std::string& device_id) { | 560 const std::string& device_id) { |
561 AudioDeviceID device = GetAudioDeviceIdByUId(false, device_id); | 561 AudioDeviceID device = GetAudioDeviceIdByUId(false, device_id); |
562 if (device == kAudioObjectUnknown) { | 562 if (device == kAudioObjectUnknown) { |
563 DLOG(ERROR) << "Failed to open output device: " << device_id; | 563 DLOG(ERROR) << "Failed to open output device: " << device_id; |
564 return NULL; | 564 return NULL; |
565 } | 565 } |
566 | 566 |
567 // Lazily create the audio device listener on the first stream creation. | 567 // Lazily create the audio device listener on the first stream creation. |
568 if (!output_device_listener_) { | 568 if (!output_device_listener_) { |
569 output_device_listener_.reset(new AudioDeviceListenerMac(base::Bind( | 569 // NOTE: Use BindToCurrentLoop() to ensure the callback is always PostTask'd |
570 &AudioManagerMac::HandleDeviceChanges, base::Unretained(this)))); | 570 // even if OSX calls us on the right thread. Some CoreAudio drivers will |
| 571 // fire the callbacks during stream creation, leading to re-entrancy issues |
| 572 // otherwise. See http://crbug.com/349604 |
| 573 output_device_listener_.reset( |
| 574 new AudioDeviceListenerMac(BindToCurrentLoop(base::Bind( |
| 575 &AudioManagerMac::HandleDeviceChanges, base::Unretained(this))))); |
571 // Only set the current output device for the default device. | 576 // Only set the current output device for the default device. |
572 if (device_id == AudioManagerBase::kDefaultDeviceId || device_id.empty()) | 577 if (device_id == AudioManagerBase::kDefaultDeviceId || device_id.empty()) |
573 current_output_device_ = device; | 578 current_output_device_ = device; |
574 // Just use the current sample rate since we don't allow non-native sample | 579 // Just use the current sample rate since we don't allow non-native sample |
575 // rates on OSX. | 580 // rates on OSX. |
576 current_sample_rate_ = params.sample_rate(); | 581 current_sample_rate_ = params.sample_rate(); |
577 } | 582 } |
578 | 583 |
579 return new AUHALStream(this, params, device); | 584 return new AUHALStream(this, params, device); |
580 } | 585 } |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
691 power_observer_.reset(new AudioPowerObserver()); | 696 power_observer_.reset(new AudioPowerObserver()); |
692 } | 697 } |
693 | 698 |
694 void AudioManagerMac::ShutdownOnAudioThread() { | 699 void AudioManagerMac::ShutdownOnAudioThread() { |
695 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 700 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
696 output_device_listener_.reset(); | 701 output_device_listener_.reset(); |
697 power_observer_.reset(); | 702 power_observer_.reset(); |
698 } | 703 } |
699 | 704 |
700 void AudioManagerMac::HandleDeviceChanges() { | 705 void AudioManagerMac::HandleDeviceChanges() { |
701 if (!GetTaskRunner()->BelongsToCurrentThread()) { | 706 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
702 GetTaskRunner()->PostTask(FROM_HERE, base::Bind( | 707 const int new_sample_rate = HardwareSampleRate(); |
703 &AudioManagerMac::HandleDeviceChanges, base::Unretained(this))); | |
704 return; | |
705 } | |
706 | |
707 int new_sample_rate = HardwareSampleRate(); | |
708 AudioDeviceID new_output_device; | 708 AudioDeviceID new_output_device; |
709 GetDefaultOutputDevice(&new_output_device); | 709 GetDefaultOutputDevice(&new_output_device); |
710 | 710 |
711 if (current_sample_rate_ == new_sample_rate && | 711 if (current_sample_rate_ == new_sample_rate && |
712 current_output_device_ == new_output_device) | 712 current_output_device_ == new_output_device) |
713 return; | 713 return; |
714 | 714 |
715 current_sample_rate_ = new_sample_rate; | 715 current_sample_rate_ = new_sample_rate; |
716 current_output_device_ = new_output_device; | 716 current_output_device_ = new_output_device; |
717 NotifyAllOutputDeviceChangeListeners(); | 717 NotifyAllOutputDeviceChangeListeners(); |
(...skipping 19 matching lines...) Expand all Loading... |
737 bool AudioManagerMac::ShouldDeferOutputStreamStart() { | 737 bool AudioManagerMac::ShouldDeferOutputStreamStart() { |
738 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 738 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
739 return power_observer_->ShouldDeferOutputStreamStart(); | 739 return power_observer_->ShouldDeferOutputStreamStart(); |
740 } | 740 } |
741 | 741 |
742 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory) { | 742 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory) { |
743 return new AudioManagerMac(audio_log_factory); | 743 return new AudioManagerMac(audio_log_factory); |
744 } | 744 } |
745 | 745 |
746 } // namespace media | 746 } // namespace media |
OLD | NEW |