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.
5 #ifndef MEDIA_AUDIO_AUDIO_MANAGER_H_
6 #define MEDIA_AUDIO_AUDIO_MANAGER_H_
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/strings/string16.h"
13 #include "media/audio/audio_device_name.h"
14 #include "media/audio/audio_logging.h"
15 #include "media/audio/audio_parameters.h"
18 class SingleThreadTaskRunner
;
23 class AudioInputStream
;
24 class AudioManagerFactory
;
25 class AudioOutputStream
;
27 // Manages all audio resources. Provides some convenience functions that avoid
28 // the need to provide iterators over the existing streams.
29 class MEDIA_EXPORT AudioManager
{
31 virtual ~AudioManager();
33 // This provides an alternative to the statically linked factory method used
34 // to create AudioManager. This is useful for dynamically-linked third
35 // party clients seeking to provide a platform-specific implementation of
36 // AudioManager. After this is called, all static AudioManager::Create*
37 // methods will return an instance of AudioManager provided by |factory|. This
38 // call may be made at most once per process, and before any calls to static
39 // AudioManager::Create* methods. This method takes ownership of |factory|,
40 // which must not be NULL.
41 static void SetFactory(AudioManagerFactory
* factory
);
43 // Construct the audio manager; only one instance is allowed. The manager
44 // will forward CreateAudioLog() calls to the provided AudioLogFactory; as
45 // such |audio_log_factory| must outlive the AudioManager.
46 static AudioManager
* Create(AudioLogFactory
* audio_log_factory
);
48 // Similar to Create() except also schedules a monitor on the given task
49 // runner to ensure the audio thread is not stuck for more than 60 seconds; if
50 // a hang is detected, the process will be crashed. See EnableHangMonitor().
51 static AudioManager
* CreateWithHangTimer(
52 AudioLogFactory
* audio_log_factory
,
53 const scoped_refptr
<base::SingleThreadTaskRunner
>& monitor_task_runner
);
55 // Similar to Create() except uses a FakeAudioLogFactory for testing.
56 static AudioManager
* CreateForTesting();
58 // Enables the hang monitor for the AudioManager once it's created. Must be
59 // called before the AudioManager is created. CreateWithHangTimer() requires
60 // either switches::kEnableAudioHangMonitor to be present or this to have been
61 // called previously to start the hang monitor. Does nothing on OSX.
62 static void EnableHangMonitor();
65 // Sets the name of the audio source as seen by external apps. Only actually
66 // used with PulseAudio as of this writing.
67 static void SetGlobalAppName(const std::string
& app_name
);
69 // Returns the app name or an empty string if it is not set.
70 static const std::string
& GetGlobalAppName();
73 // Should only be used for testing. Resets a previously-set
74 // AudioManagerFactory. The instance of AudioManager is not affected.
75 static void ResetFactoryForTesting();
77 // Returns the pointer to the last created instance, or NULL if not yet
78 // created. This is a utility method for the code outside of media directory,
80 static AudioManager
* Get();
82 // Returns true if the OS reports existence of audio devices. This does not
83 // guarantee that the existing devices support all formats and sample rates.
84 virtual bool HasAudioOutputDevices() = 0;
86 // Returns true if the OS reports existence of audio recording devices. This
87 // does not guarantee that the existing devices support all formats and
89 virtual bool HasAudioInputDevices() = 0;
91 // Returns a human readable string for the model/make of the active audio
92 // input device for this computer.
93 virtual base::string16
GetAudioInputDeviceModel() = 0;
95 // Opens the platform default audio input settings UI.
96 // Note: This could invoke an external application/preferences pane, so
97 // ideally must not be called from the UI thread or other time sensitive
98 // threads to avoid blocking the rest of the application.
99 virtual void ShowAudioInputSettings() = 0;
101 // Appends a list of available input devices to |device_names|,
102 // which must initially be empty. It is not guaranteed that all the
103 // devices in the list support all formats and sample rates for
106 // Not threadsafe; in production this should only be called from the
107 // Audio worker thread (see GetWorkerTaskRunner()).
108 virtual void GetAudioInputDeviceNames(AudioDeviceNames
* device_names
) = 0;
110 // Appends a list of available output devices to |device_names|,
111 // which must initially be empty.
113 // Not threadsafe; in production this should only be called from the
114 // Audio worker thread (see GetWorkerTaskRunner()).
115 virtual void GetAudioOutputDeviceNames(AudioDeviceNames
* device_names
) = 0;
117 // Factory for all the supported stream formats. |params| defines parameters
118 // of the audio stream to be created.
120 // |params.sample_per_packet| is the requested buffer allocation which the
121 // audio source thinks it can usually fill without blocking. Internally two
122 // or three buffers are created, one will be locked for playback and one will
123 // be ready to be filled in the call to AudioSourceCallback::OnMoreData().
125 // To create a stream for the default output device, pass an empty string
126 // for |device_id|, otherwise the specified audio device will be opened.
128 // Returns NULL if the combination of the parameters is not supported, or if
129 // we have reached some other platform specific limit.
131 // |params.format| can be set to AUDIO_PCM_LOW_LATENCY and that has two
133 // 1- Instead of triple buffered the audio will be double buffered.
134 // 2- A low latency driver or alternative audio subsystem will be used when
137 // Do not free the returned AudioOutputStream. It is owned by AudioManager.
138 virtual AudioOutputStream
* MakeAudioOutputStream(
139 const AudioParameters
& params
,
140 const std::string
& device_id
) = 0;
142 // Creates new audio output proxy. A proxy implements
143 // AudioOutputStream interface, but unlike regular output stream
144 // created with MakeAudioOutputStream() it opens device only when a
145 // sound is actually playing.
146 virtual AudioOutputStream
* MakeAudioOutputStreamProxy(
147 const AudioParameters
& params
,
148 const std::string
& device_id
) = 0;
150 // Factory to create audio recording streams.
151 // |channels| can be 1 or 2.
152 // |sample_rate| is in hertz and can be any value supported by the platform.
153 // |bits_per_sample| can be any value supported by the platform.
154 // |samples_per_packet| is in hertz as well and can be 0 to |sample_rate|,
155 // with 0 suggesting that the implementation use a default value for that
157 // Returns NULL if the combination of the parameters is not supported, or if
158 // we have reached some other platform specific limit.
160 // Do not free the returned AudioInputStream. It is owned by AudioManager.
161 // When you are done with it, call |Stop()| and |Close()| to release it.
162 virtual AudioInputStream
* MakeAudioInputStream(
163 const AudioParameters
& params
,
164 const std::string
& device_id
) = 0;
166 // Returns the task runner used for audio IO.
167 virtual scoped_refptr
<base::SingleThreadTaskRunner
> GetTaskRunner() = 0;
169 // Heavyweight tasks should use GetWorkerTaskRunner() instead of
170 // GetTaskRunner(). On most platforms they are the same, but some share the
171 // UI loop with the audio IO loop.
172 virtual scoped_refptr
<base::SingleThreadTaskRunner
> GetWorkerTaskRunner() = 0;
174 // Allows clients to listen for device state changes; e.g. preferred sample
175 // rate or channel layout changes. The typical response to receiving this
176 // callback is to recreate the stream.
177 class AudioDeviceListener
{
179 virtual void OnDeviceChange() = 0;
182 virtual void AddOutputDeviceChangeListener(AudioDeviceListener
* listener
) = 0;
183 virtual void RemoveOutputDeviceChangeListener(
184 AudioDeviceListener
* listener
) = 0;
186 // Returns the default output hardware audio parameters for opening output
187 // streams. It is a convenience interface to
188 // AudioManagerBase::GetPreferredOutputStreamParameters and each AudioManager
189 // does not need their own implementation to this interface.
190 // TODO(tommi): Remove this method and use GetOutputStreamParameteres instead.
191 virtual AudioParameters
GetDefaultOutputStreamParameters() = 0;
193 // Returns the output hardware audio parameters for a specific output device.
194 virtual AudioParameters
GetOutputStreamParameters(
195 const std::string
& device_id
) = 0;
197 // Returns the input hardware audio parameters of the specific device
198 // for opening input streams. Each AudioManager needs to implement their own
199 // version of this interface.
200 virtual AudioParameters
GetInputStreamParameters(
201 const std::string
& device_id
) = 0;
203 // Returns the device id of an output device that belongs to the same hardware
204 // as the specified input device.
205 // If the hardware has only an input device (e.g. a webcam), the return value
206 // will be empty (which the caller can then interpret to be the default output
207 // device). Implementations that don't yet support this feature, must return
208 // an empty string. Must be called on the audio worker thread (see
209 // GetWorkerTaskRunner()).
210 virtual std::string
GetAssociatedOutputDeviceID(
211 const std::string
& input_device_id
) = 0;
213 // Create a new AudioLog object for tracking the behavior for one or more
214 // instances of the given component. See AudioLogFactory for more details.
215 virtual scoped_ptr
<AudioLog
> CreateAudioLog(
216 AudioLogFactory::AudioComponent component
) = 0;
218 // Informs the audio manager that the system has support for a keyboard mic.
219 // This information will be passed on in the return value of
220 // GetInputStreamParameters as an effect. Only supported on ChromeOS.
221 virtual void SetHasKeyboardMic() = 0;
227 DISALLOW_COPY_AND_ASSIGN(AudioManager
);
232 #endif // MEDIA_AUDIO_AUDIO_MANAGER_H_