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 3 // found in the LICENSE file. 4 5 #ifndef MEDIA_AUDIO_MAC_AUDIO_MANAGER_MAC_H_ 6 #define MEDIA_AUDIO_MAC_AUDIO_MANAGER_MAC_H_ 7 8 #include <CoreAudio/AudioHardware.h> 9 #include <list> 10 #include <string> 11 12 #include "base/basictypes.h" 13 #include "base/compiler_specific.h" 14 #include "media/audio/audio_manager_base.h" 15 #include "media/audio/mac/audio_device_listener_mac.h" 16 17 namespace media { 18 19 // Mac OS X implementation of the AudioManager singleton. This class is internal 20 // to the audio output and only internal users can call methods not exposed by 21 // the AudioManager class. 22 class MEDIA_EXPORT AudioManagerMac : public AudioManagerBase { 23 public: 24 AudioManagerMac(AudioLogFactory* audio_log_factory); 25 26 // Implementation of AudioManager. 27 virtual bool HasAudioOutputDevices() OVERRIDE; 28 virtual bool HasAudioInputDevices() OVERRIDE; 29 virtual void GetAudioInputDeviceNames( 30 AudioDeviceNames* device_names) OVERRIDE; 31 virtual void GetAudioOutputDeviceNames( 32 AudioDeviceNames* device_names) OVERRIDE; 33 virtual AudioParameters GetInputStreamParameters( 34 const std::string& device_id) OVERRIDE; 35 virtual std::string GetAssociatedOutputDeviceID( 36 const std::string& input_device_id) OVERRIDE; 37 38 // Implementation of AudioManagerBase. 39 virtual AudioOutputStream* MakeLinearOutputStream( 40 const AudioParameters& params) OVERRIDE; 41 virtual AudioOutputStream* MakeLowLatencyOutputStream( 42 const AudioParameters& params, 43 const std::string& device_id) OVERRIDE; 44 virtual AudioInputStream* MakeLinearInputStream( 45 const AudioParameters& params, const std::string& device_id) OVERRIDE; 46 virtual AudioInputStream* MakeLowLatencyInputStream( 47 const AudioParameters& params, const std::string& device_id) OVERRIDE; 48 virtual std::string GetDefaultOutputDeviceID() OVERRIDE; 49 50 // Used to track destruction of input and output streams. 51 virtual void ReleaseOutputStream(AudioOutputStream* stream) OVERRIDE; 52 virtual void ReleaseInputStream(AudioInputStream* stream) OVERRIDE; 53 54 static bool GetDefaultInputDevice(AudioDeviceID* device); 55 static bool GetDefaultOutputDevice(AudioDeviceID* device); 56 static bool GetDefaultDevice(AudioDeviceID* device, bool input); 57 58 static bool GetDefaultOutputChannels(int* channels); 59 60 static bool GetDeviceChannels(AudioDeviceID device, 61 AudioObjectPropertyScope scope, 62 int* channels); 63 64 static int HardwareSampleRateForDevice(AudioDeviceID device_id); 65 static int HardwareSampleRate(); 66 67 // OSX has issues with starting streams as the sytem goes into suspend and 68 // immediately after it wakes up from resume. See http://crbug.com/160920. 69 // As a workaround we delay Start() when it occurs after suspend and for a 70 // small amount of time after resume. 71 // 72 // Streams should consult ShouldDeferStreamStart() and if true check the value 73 // again after |kStartDelayInSecsForPowerEvents| has elapsed. If false, the 74 // stream may be started immediately. 75 enum { kStartDelayInSecsForPowerEvents = 1 }; 76 bool ShouldDeferStreamStart(); 77 78 protected: 79 virtual ~AudioManagerMac(); 80 81 virtual AudioParameters GetPreferredOutputStreamParameters( 82 const std::string& output_device_id, 83 const AudioParameters& input_params) OVERRIDE; 84 85 private: 86 void InitializeOnAudioThread(); 87 void ShutdownOnAudioThread(); 88 89 int ChooseBufferSize(bool is_input, int sample_rate); 90 91 // Notify streams of a device change if the default output device or its 92 // sample rate has changed, otherwise does nothing. 93 void HandleDeviceChanges(); 94 95 scoped_ptr<AudioDeviceListenerMac> output_device_listener_; 96 97 // Track the output sample-rate and the default output device 98 // so we can intelligently handle device notifications only when necessary. 99 int current_sample_rate_; 100 AudioDeviceID current_output_device_; 101 102 // Helper class which monitors power events to determine if output streams 103 // should defer Start() calls. Required to workaround an OSX bug. See 104 // http://crbug.com/160920 for more details. 105 class AudioPowerObserver; 106 scoped_ptr<AudioPowerObserver> power_observer_; 107 108 // Tracks all constructed input and output streams so they can be stopped at 109 // shutdown. See ShutdownOnAudioThread() for more details. 110 std::list<AudioInputStream*> input_streams_; 111 std::list<AudioOutputStream*> output_streams_; 112 113 DISALLOW_COPY_AND_ASSIGN(AudioManagerMac); 114 }; 115 116 } // namespace media 117 118 #endif // MEDIA_AUDIO_MAC_AUDIO_MANAGER_MAC_H_ 119