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_parameters.h"
18 class MessageLoopProxy
;
23 class AudioInputStream
;
24 class AudioOutputStream
;
26 // Manages all audio resources. In particular it owns the AudioOutputStream
27 // objects. Provides some convenience functions that avoid the need to provide
28 // iterators over the existing streams.
29 class MEDIA_EXPORT AudioManager
{
31 virtual ~AudioManager();
33 // Use to construct the audio manager.
34 // NOTE: There should only be one instance.
35 static AudioManager
* Create();
37 // Returns the pointer to the last created instance, or NULL if not yet
38 // created. This is a utility method for the code outside of media directory,
40 static AudioManager
* Get();
42 // Returns true if the OS reports existence of audio devices. This does not
43 // guarantee that the existing devices support all formats and sample rates.
44 virtual bool HasAudioOutputDevices() = 0;
46 // Returns true if the OS reports existence of audio recording devices. This
47 // does not guarantee that the existing devices support all formats and
49 virtual bool HasAudioInputDevices() = 0;
51 // Returns a human readable string for the model/make of the active audio
52 // input device for this computer.
53 virtual string16
GetAudioInputDeviceModel() = 0;
55 // Opens the platform default audio input settings UI.
56 // Note: This could invoke an external application/preferences pane, so
57 // ideally must not be called from the UI thread or other time sensitive
58 // threads to avoid blocking the rest of the application.
59 virtual void ShowAudioInputSettings() = 0;
61 // Appends a list of available input devices to |device_names|,
62 // which must initially be empty. It is not guaranteed that all the
63 // devices in the list support all formats and sample rates for
65 virtual void GetAudioInputDeviceNames(AudioDeviceNames
* device_names
) = 0;
67 // Appends a list of available output devices to |device_names|,
68 // which must initially be empty.
69 virtual void GetAudioOutputDeviceNames(AudioDeviceNames
* device_names
) = 0;
71 // Factory for all the supported stream formats. |params| defines parameters
72 // of the audio stream to be created.
74 // |params.sample_per_packet| is the requested buffer allocation which the
75 // audio source thinks it can usually fill without blocking. Internally two
76 // or three buffers are created, one will be locked for playback and one will
77 // be ready to be filled in the call to AudioSourceCallback::OnMoreData().
79 // To create a stream for the default output device, pass an empty string
80 // for |device_id|, otherwise the specified audio device will be opened.
82 // The |input_device_id| is used for low-latency unified streams
83 // (input+output) only and then only if the audio parameters specify a >0
84 // input channel count. In other cases this id is ignored and should be
87 // Returns NULL if the combination of the parameters is not supported, or if
88 // we have reached some other platform specific limit.
90 // |params.format| can be set to AUDIO_PCM_LOW_LATENCY and that has two
92 // 1- Instead of triple buffered the audio will be double buffered.
93 // 2- A low latency driver or alternative audio subsystem will be used when
96 // Do not free the returned AudioOutputStream. It is owned by AudioManager.
97 virtual AudioOutputStream
* MakeAudioOutputStream(
98 const AudioParameters
& params
,
99 const std::string
& device_id
,
100 const std::string
& input_device_id
) = 0;
102 // Creates new audio output proxy. A proxy implements
103 // AudioOutputStream interface, but unlike regular output stream
104 // created with MakeAudioOutputStream() it opens device only when a
105 // sound is actually playing.
106 virtual AudioOutputStream
* MakeAudioOutputStreamProxy(
107 const AudioParameters
& params
,
108 const std::string
& device_id
,
109 const std::string
& input_device_id
) = 0;
111 // Factory to create audio recording streams.
112 // |channels| can be 1 or 2.
113 // |sample_rate| is in hertz and can be any value supported by the platform.
114 // |bits_per_sample| can be any value supported by the platform.
115 // |samples_per_packet| is in hertz as well and can be 0 to |sample_rate|,
116 // with 0 suggesting that the implementation use a default value for that
118 // Returns NULL if the combination of the parameters is not supported, or if
119 // we have reached some other platform specific limit.
121 // Do not free the returned AudioInputStream. It is owned by AudioManager.
122 // When you are done with it, call |Stop()| and |Close()| to release it.
123 virtual AudioInputStream
* MakeAudioInputStream(
124 const AudioParameters
& params
, const std::string
& device_id
) = 0;
126 // Returns message loop used for audio IO.
127 virtual scoped_refptr
<base::MessageLoopProxy
> GetMessageLoop() = 0;
129 // Heavyweight tasks should use GetWorkerLoop() instead of GetMessageLoop().
130 // On most platforms they are the same, but some share the UI loop with the
132 virtual scoped_refptr
<base::MessageLoopProxy
> GetWorkerLoop() = 0;
134 // Allows clients to listen for device state changes; e.g. preferred sample
135 // rate or channel layout changes. The typical response to receiving this
136 // callback is to recreate the stream.
137 class AudioDeviceListener
{
139 virtual void OnDeviceChange() = 0;
142 virtual void AddOutputDeviceChangeListener(AudioDeviceListener
* listener
) = 0;
143 virtual void RemoveOutputDeviceChangeListener(
144 AudioDeviceListener
* listener
) = 0;
146 // Returns the default output hardware audio parameters for opening output
147 // streams. It is a convenience interface to
148 // AudioManagerBase::GetPreferredOutputStreamParameters and each AudioManager
149 // does not need their own implementation to this interface.
150 // TODO(tommi): Remove this method and use GetOutputStreamParameteres instead.
151 virtual AudioParameters
GetDefaultOutputStreamParameters() = 0;
153 // Returns the output hardware audio parameters for a specific output device.
154 virtual AudioParameters
GetOutputStreamParameters(
155 const std::string
& device_id
) = 0;
157 // Returns the input hardware audio parameters of the specific device
158 // for opening input streams. Each AudioManager needs to implement their own
159 // version of this interface.
160 virtual AudioParameters
GetInputStreamParameters(
161 const std::string
& device_id
) = 0;
163 // Returns the device id of an output device that belongs to the same hardware
164 // as the specified input device.
165 // If the hardware has only an input device (e.g. a webcam), the return value
166 // will be empty (which the caller can then interpret to be the default output
167 // device). Implementations that don't yet support this feature, must return
169 virtual std::string
GetAssociatedOutputDeviceID(
170 const std::string
& input_device_id
) = 0;
176 DISALLOW_COPY_AND_ASSIGN(AudioManager
);
181 #endif // MEDIA_AUDIO_AUDIO_MANAGER_H_