audio_manager_base.h revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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_AUDIO_MANAGER_BASE_H_ 6#define MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_ 7 8#include <string> 9#include <utility> 10 11#include "base/compiler_specific.h" 12#include "base/memory/scoped_ptr.h" 13#include "base/memory/scoped_vector.h" 14#include "base/observer_list.h" 15#include "base/synchronization/lock.h" 16#include "media/audio/audio_manager.h" 17 18#include "media/audio/audio_output_dispatcher.h" 19 20#if defined(OS_WIN) 21#include "base/win/scoped_com_initializer.h" 22#endif 23 24namespace base { 25class Thread; 26} 27 28namespace media { 29 30class AudioOutputDispatcher; 31 32// AudioManagerBase provides AudioManager functions common for all platforms. 33class MEDIA_EXPORT AudioManagerBase : public AudioManager { 34 public: 35 // TODO(sergeyu): The constants below belong to AudioManager interface, not 36 // to the base implementation. 37 38 // Name of the generic "default" device. 39 static const char kDefaultDeviceName[]; 40 // Unique Id of the generic "default" device. 41 static const char kDefaultDeviceId[]; 42 43 // Input device ID used to capture the default system playback stream. When 44 // this device ID is passed to MakeAudioInputStream() the returned 45 // AudioInputStream will be capturing audio currently being played on the 46 // default playback device. At the moment this feature is supported only on 47 // some platforms. AudioInputStream::Intialize() will return an error on 48 // platforms that don't support it. GetInputStreamParameters() must be used 49 // to get the parameters of the loopback device before creating a loopback 50 // stream, otherwise stream initialization may fail. 51 static const char kLoopbackInputDeviceId[]; 52 53 virtual ~AudioManagerBase(); 54 55 virtual scoped_refptr<base::MessageLoopProxy> GetMessageLoop() OVERRIDE; 56 virtual scoped_refptr<base::MessageLoopProxy> GetWorkerLoop() OVERRIDE; 57 58 virtual string16 GetAudioInputDeviceModel() OVERRIDE; 59 60 virtual void ShowAudioInputSettings() OVERRIDE; 61 62 virtual void GetAudioInputDeviceNames( 63 AudioDeviceNames* device_names) OVERRIDE; 64 65 virtual void GetAudioOutputDeviceNames( 66 AudioDeviceNames* device_names) OVERRIDE; 67 68 virtual AudioOutputStream* MakeAudioOutputStream( 69 const AudioParameters& params, 70 const std::string& device_id, 71 const std::string& input_device_id) OVERRIDE; 72 73 virtual AudioInputStream* MakeAudioInputStream( 74 const AudioParameters& params, const std::string& device_id) OVERRIDE; 75 76 virtual AudioOutputStream* MakeAudioOutputStreamProxy( 77 const AudioParameters& params, 78 const std::string& device_id, 79 const std::string& input_device_id) OVERRIDE; 80 81 // Called internally by the audio stream when it has been closed. 82 virtual void ReleaseOutputStream(AudioOutputStream* stream); 83 virtual void ReleaseInputStream(AudioInputStream* stream); 84 85 // Creates the output stream for the |AUDIO_PCM_LINEAR| format. The legacy 86 // name is also from |AUDIO_PCM_LINEAR|. 87 virtual AudioOutputStream* MakeLinearOutputStream( 88 const AudioParameters& params) = 0; 89 90 // Creates the output stream for the |AUDIO_PCM_LOW_LATENCY| format. 91 // |input_device_id| is used by unified IO to open the correct input device. 92 virtual AudioOutputStream* MakeLowLatencyOutputStream( 93 const AudioParameters& params, 94 const std::string& device_id, 95 const std::string& input_device_id) = 0; 96 97 // Creates the input stream for the |AUDIO_PCM_LINEAR| format. The legacy 98 // name is also from |AUDIO_PCM_LINEAR|. 99 virtual AudioInputStream* MakeLinearInputStream( 100 const AudioParameters& params, const std::string& device_id) = 0; 101 102 // Creates the input stream for the |AUDIO_PCM_LOW_LATENCY| format. 103 virtual AudioInputStream* MakeLowLatencyInputStream( 104 const AudioParameters& params, const std::string& device_id) = 0; 105 106 // Listeners will be notified on the AudioManager::GetMessageLoop() loop. 107 virtual void AddOutputDeviceChangeListener( 108 AudioDeviceListener* listener) OVERRIDE; 109 virtual void RemoveOutputDeviceChangeListener( 110 AudioDeviceListener* listener) OVERRIDE; 111 112 virtual AudioParameters GetDefaultOutputStreamParameters() OVERRIDE; 113 virtual AudioParameters GetOutputStreamParameters( 114 const std::string& device_id) OVERRIDE; 115 116 virtual AudioParameters GetInputStreamParameters( 117 const std::string& device_id) OVERRIDE; 118 119 virtual std::string GetAssociatedOutputDeviceID( 120 const std::string& input_device_id) OVERRIDE; 121 122 protected: 123 AudioManagerBase(); 124 125 126 // Shuts down the audio thread and releases all the audio output dispatchers 127 // on the audio thread. All audio streams should be freed before Shutdown() 128 // is called. This must be called in the destructor of every AudioManagerBase 129 // implementation. 130 void Shutdown(); 131 132 void SetMaxOutputStreamsAllowed(int max) { max_num_output_streams_ = max; } 133 134 // Called by each platform specific AudioManager to notify output state change 135 // listeners that a state change has occurred. Must be called from the audio 136 // thread. 137 void NotifyAllOutputDeviceChangeListeners(); 138 139 // Returns the preferred hardware audio output parameters for opening output 140 // streams. If the users inject a valid |input_params|, each AudioManager 141 // will decide if they should return the values from |input_params| or the 142 // default hardware values. If the |input_params| is invalid, it will return 143 // the default hardware audio parameters. 144 // If |output_device_id| is empty, the implementation must treat that as 145 // a request for the default output device. 146 virtual AudioParameters GetPreferredOutputStreamParameters( 147 const std::string& output_device_id, 148 const AudioParameters& input_params) = 0; 149 150 // Returns the ID of the default audio output device. 151 // Implementations that don't yet support this should return an empty string. 152 virtual std::string GetDefaultOutputDeviceID(); 153 154 // Get number of input or output streams. 155 int input_stream_count() { return num_input_streams_; } 156 int output_stream_count() { return num_output_streams_; } 157 158 private: 159 struct DispatcherParams; 160 typedef ScopedVector<DispatcherParams> AudioOutputDispatchers; 161 162 class CompareByParams; 163 164 // Called by Shutdown(). 165 void ShutdownOnAudioThread(); 166 167 // Max number of open output streams, modified by 168 // SetMaxOutputStreamsAllowed(). 169 int max_num_output_streams_; 170 171 // Max number of open input streams. 172 int max_num_input_streams_; 173 174 // Number of currently open output streams. 175 int num_output_streams_; 176 177 // Number of currently open input streams. 178 int num_input_streams_; 179 180 // Track output state change listeners. 181 ObserverList<AudioDeviceListener> output_listeners_; 182 183 // Thread used to interact with audio streams created by this audio manager. 184 scoped_ptr<base::Thread> audio_thread_; 185 mutable base::Lock audio_thread_lock_; 186 187 // The message loop of the audio thread this object runs on. Used for internal 188 // tasks which run on the audio thread even after Shutdown() has been started 189 // and GetMessageLoop() starts returning NULL. 190 scoped_refptr<base::MessageLoopProxy> message_loop_; 191 192 // Map of cached AudioOutputDispatcher instances. Must only be touched 193 // from the audio thread (no locking). 194 AudioOutputDispatchers output_dispatchers_; 195 196 DISALLOW_COPY_AND_ASSIGN(AudioManagerBase); 197}; 198 199} // namespace media 200 201#endif // MEDIA_AUDIO_AUDIO_MANAGER_BASE_H_ 202