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 AudioOutputStream
;
26 // Manages all audio resources. Provides some convenience functions that avoid
27 // the need to provide iterators over the existing streams.
28 class MEDIA_EXPORT AudioManager
{
30 virtual ~AudioManager();
32 // Construct the audio manager; only one instance is allowed. The manager
33 // will forward CreateAudioLog() calls to the provided AudioLogFactory; as
34 // such |audio_log_factory| must outlive the AudioManager.
35 static AudioManager
* Create(AudioLogFactory
* audio_log_factory
);
37 // Similar to Create() except also schedules a monitor on the given task
38 // runner to ensure the audio thread is not stuck for more than 60 seconds; if
39 // a hang is detected, the process will be crashed.
40 static AudioManager
* CreateWithHangTimer(
41 AudioLogFactory
* audio_log_factory
,
42 const scoped_refptr
<base::SingleThreadTaskRunner
>& monitor_task_runner
);
44 // Similar to Create() except uses a FakeAudioLogFactory for testing.
45 static AudioManager
* CreateForTesting();
47 // Returns the pointer to the last created instance, or NULL if not yet
48 // created. This is a utility method for the code outside of media directory,
50 static AudioManager
* Get();
52 // Returns true if the OS reports existence of audio devices. This does not
53 // guarantee that the existing devices support all formats and sample rates.
54 virtual bool HasAudioOutputDevices() = 0;
56 // Returns true if the OS reports existence of audio recording devices. This
57 // does not guarantee that the existing devices support all formats and
59 virtual bool HasAudioInputDevices() = 0;
61 // Returns a human readable string for the model/make of the active audio
62 // input device for this computer.
63 virtual base::string16
GetAudioInputDeviceModel() = 0;
65 // Opens the platform default audio input settings UI.
66 // Note: This could invoke an external application/preferences pane, so
67 // ideally must not be called from the UI thread or other time sensitive
68 // threads to avoid blocking the rest of the application.
69 virtual void ShowAudioInputSettings() = 0;
71 // Appends a list of available input devices to |device_names|,
72 // which must initially be empty. It is not guaranteed that all the
73 // devices in the list support all formats and sample rates for
76 // Not threadsafe; in production this should only be called from the
77 // Audio worker thread (see GetWorkerTaskRunner()).
78 virtual void GetAudioInputDeviceNames(AudioDeviceNames
* device_names
) = 0;
80 // Appends a list of available output devices to |device_names|,
81 // which must initially be empty.
83 // Not threadsafe; in production this should only be called from the
84 // Audio worker thread (see GetWorkerTaskRunner()).
85 virtual void GetAudioOutputDeviceNames(AudioDeviceNames
* device_names
) = 0;
87 // Factory for all the supported stream formats. |params| defines parameters
88 // of the audio stream to be created.
90 // |params.sample_per_packet| is the requested buffer allocation which the
91 // audio source thinks it can usually fill without blocking. Internally two
92 // or three buffers are created, one will be locked for playback and one will
93 // be ready to be filled in the call to AudioSourceCallback::OnMoreData().
95 // To create a stream for the default output device, pass an empty string
96 // for |device_id|, otherwise the specified audio device will be opened.
98 // Returns NULL if the combination of the parameters is not supported, or if
99 // we have reached some other platform specific limit.
101 // |params.format| can be set to AUDIO_PCM_LOW_LATENCY and that has two
103 // 1- Instead of triple buffered the audio will be double buffered.
104 // 2- A low latency driver or alternative audio subsystem will be used when
107 // Do not free the returned AudioOutputStream. It is owned by AudioManager.
108 virtual AudioOutputStream
* MakeAudioOutputStream(
109 const AudioParameters
& params
,
110 const std::string
& device_id
) = 0;
112 // Creates new audio output proxy. A proxy implements
113 // AudioOutputStream interface, but unlike regular output stream
114 // created with MakeAudioOutputStream() it opens device only when a
115 // sound is actually playing.
116 virtual AudioOutputStream
* MakeAudioOutputStreamProxy(
117 const AudioParameters
& params
,
118 const std::string
& device_id
) = 0;
120 // Factory to create audio recording streams.
121 // |channels| can be 1 or 2.
122 // |sample_rate| is in hertz and can be any value supported by the platform.
123 // |bits_per_sample| can be any value supported by the platform.
124 // |samples_per_packet| is in hertz as well and can be 0 to |sample_rate|,
125 // with 0 suggesting that the implementation use a default value for that
127 // Returns NULL if the combination of the parameters is not supported, or if
128 // we have reached some other platform specific limit.
130 // Do not free the returned AudioInputStream. It is owned by AudioManager.
131 // When you are done with it, call |Stop()| and |Close()| to release it.
132 virtual AudioInputStream
* MakeAudioInputStream(
133 const AudioParameters
& params
, const std::string
& device_id
) = 0;
135 // Returns the task runner used for audio IO.
136 virtual scoped_refptr
<base::SingleThreadTaskRunner
> GetTaskRunner() = 0;
138 // Heavyweight tasks should use GetWorkerTaskRunner() instead of
139 // GetTaskRunner(). On most platforms they are the same, but some share the
140 // UI loop with the audio IO loop.
141 virtual scoped_refptr
<base::SingleThreadTaskRunner
> GetWorkerTaskRunner() = 0;
143 // Allows clients to listen for device state changes; e.g. preferred sample
144 // rate or channel layout changes. The typical response to receiving this
145 // callback is to recreate the stream.
146 class AudioDeviceListener
{
148 virtual void OnDeviceChange() = 0;
151 virtual void AddOutputDeviceChangeListener(AudioDeviceListener
* listener
) = 0;
152 virtual void RemoveOutputDeviceChangeListener(
153 AudioDeviceListener
* listener
) = 0;
155 // Returns the default output hardware audio parameters for opening output
156 // streams. It is a convenience interface to
157 // AudioManagerBase::GetPreferredOutputStreamParameters and each AudioManager
158 // does not need their own implementation to this interface.
159 // TODO(tommi): Remove this method and use GetOutputStreamParameteres instead.
160 virtual AudioParameters
GetDefaultOutputStreamParameters() = 0;
162 // Returns the output hardware audio parameters for a specific output device.
163 virtual AudioParameters
GetOutputStreamParameters(
164 const std::string
& device_id
) = 0;
166 // Returns the input hardware audio parameters of the specific device
167 // for opening input streams. Each AudioManager needs to implement their own
168 // version of this interface.
169 virtual AudioParameters
GetInputStreamParameters(
170 const std::string
& device_id
) = 0;
172 // Returns the device id of an output device that belongs to the same hardware
173 // as the specified input device.
174 // If the hardware has only an input device (e.g. a webcam), the return value
175 // will be empty (which the caller can then interpret to be the default output
176 // device). Implementations that don't yet support this feature, must return
177 // an empty string. Must be called on the audio worker thread (see
178 // GetWorkerTaskRunner()).
179 virtual std::string
GetAssociatedOutputDeviceID(
180 const std::string
& input_device_id
) = 0;
182 // Create a new AudioLog object for tracking the behavior for one or more
183 // instances of the given component. See AudioLogFactory for more details.
184 virtual scoped_ptr
<AudioLog
> CreateAudioLog(
185 AudioLogFactory::AudioComponent component
) = 0;
187 // Informs the audio manager that the system has support for a keyboard mic.
188 // This information will be passed on in the return value of
189 // GetInputStreamParameters as an effect. Only supported on ChromeOS.
190 virtual void SetHasKeyboardMic() = 0;
196 DISALLOW_COPY_AND_ASSIGN(AudioManager
);
201 #endif // MEDIA_AUDIO_AUDIO_MANAGER_H_