Delete unused downloads page asset.
[chromium-blink-merge.git] / media / audio / audio_manager.h
blob1391742fe5f68a83e08da7e0aa3f052c22e5f4a9
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_
8 #include <string>
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"
17 namespace base {
18 class SingleThreadTaskRunner;
21 namespace media {
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 {
30 public:
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();
64 // Should only be used for testing. Resets a previously-set
65 // AudioManagerFactory. The instance of AudioManager is not affected.
66 static void ResetFactoryForTesting();
68 // Returns the pointer to the last created instance, or NULL if not yet
69 // created. This is a utility method for the code outside of media directory,
70 // like src/chrome.
71 static AudioManager* Get();
73 // Returns true if the OS reports existence of audio devices. This does not
74 // guarantee that the existing devices support all formats and sample rates.
75 virtual bool HasAudioOutputDevices() = 0;
77 // Returns true if the OS reports existence of audio recording devices. This
78 // does not guarantee that the existing devices support all formats and
79 // sample rates.
80 virtual bool HasAudioInputDevices() = 0;
82 // Returns a human readable string for the model/make of the active audio
83 // input device for this computer.
84 virtual base::string16 GetAudioInputDeviceModel() = 0;
86 // Opens the platform default audio input settings UI.
87 // Note: This could invoke an external application/preferences pane, so
88 // ideally must not be called from the UI thread or other time sensitive
89 // threads to avoid blocking the rest of the application.
90 virtual void ShowAudioInputSettings() = 0;
92 // Appends a list of available input devices to |device_names|,
93 // which must initially be empty. It is not guaranteed that all the
94 // devices in the list support all formats and sample rates for
95 // recording.
97 // Not threadsafe; in production this should only be called from the
98 // Audio worker thread (see GetWorkerTaskRunner()).
99 virtual void GetAudioInputDeviceNames(AudioDeviceNames* device_names) = 0;
101 // Appends a list of available output devices to |device_names|,
102 // which must initially be empty.
104 // Not threadsafe; in production this should only be called from the
105 // Audio worker thread (see GetWorkerTaskRunner()).
106 virtual void GetAudioOutputDeviceNames(AudioDeviceNames* device_names) = 0;
108 // Factory for all the supported stream formats. |params| defines parameters
109 // of the audio stream to be created.
111 // |params.sample_per_packet| is the requested buffer allocation which the
112 // audio source thinks it can usually fill without blocking. Internally two
113 // or three buffers are created, one will be locked for playback and one will
114 // be ready to be filled in the call to AudioSourceCallback::OnMoreData().
116 // To create a stream for the default output device, pass an empty string
117 // for |device_id|, otherwise the specified audio device will be opened.
119 // Returns NULL if the combination of the parameters is not supported, or if
120 // we have reached some other platform specific limit.
122 // |params.format| can be set to AUDIO_PCM_LOW_LATENCY and that has two
123 // effects:
124 // 1- Instead of triple buffered the audio will be double buffered.
125 // 2- A low latency driver or alternative audio subsystem will be used when
126 // available.
128 // Do not free the returned AudioOutputStream. It is owned by AudioManager.
129 virtual AudioOutputStream* MakeAudioOutputStream(
130 const AudioParameters& params,
131 const std::string& device_id) = 0;
133 // Creates new audio output proxy. A proxy implements
134 // AudioOutputStream interface, but unlike regular output stream
135 // created with MakeAudioOutputStream() it opens device only when a
136 // sound is actually playing.
137 virtual AudioOutputStream* MakeAudioOutputStreamProxy(
138 const AudioParameters& params,
139 const std::string& device_id) = 0;
141 // Factory to create audio recording streams.
142 // |channels| can be 1 or 2.
143 // |sample_rate| is in hertz and can be any value supported by the platform.
144 // |bits_per_sample| can be any value supported by the platform.
145 // |samples_per_packet| is in hertz as well and can be 0 to |sample_rate|,
146 // with 0 suggesting that the implementation use a default value for that
147 // platform.
148 // Returns NULL if the combination of the parameters is not supported, or if
149 // we have reached some other platform specific limit.
151 // Do not free the returned AudioInputStream. It is owned by AudioManager.
152 // When you are done with it, call |Stop()| and |Close()| to release it.
153 virtual AudioInputStream* MakeAudioInputStream(
154 const AudioParameters& params,
155 const std::string& device_id) = 0;
157 // Returns the task runner used for audio IO.
158 virtual scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() = 0;
160 // Heavyweight tasks should use GetWorkerTaskRunner() instead of
161 // GetTaskRunner(). On most platforms they are the same, but some share the
162 // UI loop with the audio IO loop.
163 virtual scoped_refptr<base::SingleThreadTaskRunner> GetWorkerTaskRunner() = 0;
165 // Allows clients to listen for device state changes; e.g. preferred sample
166 // rate or channel layout changes. The typical response to receiving this
167 // callback is to recreate the stream.
168 class AudioDeviceListener {
169 public:
170 virtual void OnDeviceChange() = 0;
173 virtual void AddOutputDeviceChangeListener(AudioDeviceListener* listener) = 0;
174 virtual void RemoveOutputDeviceChangeListener(
175 AudioDeviceListener* listener) = 0;
177 // Returns the default output hardware audio parameters for opening output
178 // streams. It is a convenience interface to
179 // AudioManagerBase::GetPreferredOutputStreamParameters and each AudioManager
180 // does not need their own implementation to this interface.
181 // TODO(tommi): Remove this method and use GetOutputStreamParameteres instead.
182 virtual AudioParameters GetDefaultOutputStreamParameters() = 0;
184 // Returns the output hardware audio parameters for a specific output device.
185 virtual AudioParameters GetOutputStreamParameters(
186 const std::string& device_id) = 0;
188 // Returns the input hardware audio parameters of the specific device
189 // for opening input streams. Each AudioManager needs to implement their own
190 // version of this interface.
191 virtual AudioParameters GetInputStreamParameters(
192 const std::string& device_id) = 0;
194 // Returns the device id of an output device that belongs to the same hardware
195 // as the specified input device.
196 // If the hardware has only an input device (e.g. a webcam), the return value
197 // will be empty (which the caller can then interpret to be the default output
198 // device). Implementations that don't yet support this feature, must return
199 // an empty string. Must be called on the audio worker thread (see
200 // GetWorkerTaskRunner()).
201 virtual std::string GetAssociatedOutputDeviceID(
202 const std::string& input_device_id) = 0;
204 // Create a new AudioLog object for tracking the behavior for one or more
205 // instances of the given component. See AudioLogFactory for more details.
206 virtual scoped_ptr<AudioLog> CreateAudioLog(
207 AudioLogFactory::AudioComponent component) = 0;
209 // Informs the audio manager that the system has support for a keyboard mic.
210 // This information will be passed on in the return value of
211 // GetInputStreamParameters as an effect. Only supported on ChromeOS.
212 virtual void SetHasKeyboardMic() = 0;
214 protected:
215 AudioManager();
217 private:
218 DISALLOW_COPY_AND_ASSIGN(AudioManager);
221 } // namespace media
223 #endif // MEDIA_AUDIO_AUDIO_MANAGER_H_