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_IO_H_
6 #define MEDIA_AUDIO_AUDIO_IO_H_
8 #include "base/basictypes.h"
9 #include "media/base/audio_bus.h"
11 // Low-level audio output support. To make sound there are 3 objects involved:
12 // - AudioSource : produces audio samples on a pull model. Implements
13 // the AudioSourceCallback interface.
14 // - AudioOutputStream : uses the AudioSource to render audio on a given
15 // channel, format and sample frequency configuration. Data from the
16 // AudioSource is delivered in a 'pull' model.
17 // - AudioManager : factory for the AudioOutputStream objects, manager
18 // of the hardware resources and mixer control.
20 // The number and configuration of AudioOutputStream does not need to match the
21 // physically available hardware resources. For example you can have:
23 // MonoPCMSource1 --> MonoPCMStream1 --> | | --> audio left channel
24 // StereoPCMSource -> StereoPCMStream -> | mixer |
25 // MonoPCMSource2 --> MonoPCMStream2 --> | | --> audio right channel
27 // This facility's objective is mix and render audio with low overhead using
28 // the OS basic audio support, abstracting as much as possible the
29 // idiosyncrasies of each platform. Non-goals:
30 // - Positional, 3d audio
31 // - Dependence on non-default libraries such as DirectX 9, 10, XAudio
32 // - Digital signal processing or effects
33 // - Extra features if a specific hardware is installed (EAX, X-fi)
35 // The primary client of this facility is audio coming from several tabs.
36 // Specifically for this case we avoid supporting complex formats such as MP3
37 // or WMA. Complex format decoding should be done by the renderers.
40 // Models an audio stream that gets rendered to the audio hardware output.
41 // Because we support more audio streams than physically available channels
42 // a given AudioOutputStream might or might not talk directly to hardware.
43 // An audio stream allocates several buffers for audio data and calls
44 // AudioSourceCallback::OnMoreData() periodically to fill these buffers,
45 // as the data is written to the audio device. Size of each packet is determined
46 // by |samples_per_packet| specified in AudioParameters when the stream is
51 class MEDIA_EXPORT AudioOutputStream
{
53 // Audio sources must implement AudioSourceCallback. This interface will be
54 // called in a random thread which very likely is a high priority thread. Do
55 // not rely on using this thread TLS or make calls that alter the thread
56 // itself such as creating Windows or initializing COM.
57 class MEDIA_EXPORT AudioSourceCallback
{
59 // Provide more data by fully filling |dest|. The source will return
60 // the number of frames it filled. |total_bytes_delay| contains current
61 // number of bytes of delay buffered by the AudioOutputStream.
62 virtual int OnMoreData(AudioBus
* dest
, uint32 total_bytes_delay
) = 0;
64 // There was an error while playing a buffer. Audio source cannot be
65 // destroyed yet. No direct action needed by the AudioStream, but it is
66 // a good place to stop accumulating sound data since is is likely that
67 // playback will not continue.
68 virtual void OnError(AudioOutputStream
* stream
) = 0;
71 virtual ~AudioSourceCallback() {}
74 virtual ~AudioOutputStream() {}
76 // Open the stream. false is returned if the stream cannot be opened. Open()
77 // must always be followed by a call to Close() even if Open() fails.
78 virtual bool Open() = 0;
80 // Starts playing audio and generating AudioSourceCallback::OnMoreData().
81 // Since implementor of AudioOutputStream may have internal buffers, right
82 // after calling this method initial buffers are fetched.
84 // The output stream does not take ownership of this callback.
85 virtual void Start(AudioSourceCallback
* callback
) = 0;
87 // Stops playing audio. Effect might not be instantaneous as the hardware
88 // might have locked audio data that is processing.
89 virtual void Stop() = 0;
91 // Sets the relative volume, with range [0.0, 1.0] inclusive.
92 virtual void SetVolume(double volume
) = 0;
94 // Gets the relative volume, with range [0.0, 1.0] inclusive.
95 virtual void GetVolume(double* volume
) = 0;
97 // Close the stream. This also generates AudioSourceCallback::OnClose().
98 // After calling this method, the object should not be used anymore.
99 virtual void Close() = 0;
102 // Models an audio sink receiving recorded audio from the audio driver.
103 class MEDIA_EXPORT AudioInputStream
{
105 class MEDIA_EXPORT AudioInputCallback
{
107 // Called by the audio recorder when a full packet of audio data is
108 // available. This is called from a special audio thread and the
109 // implementation should return as soon as possible.
110 // TODO(henrika): should be pure virtual when old OnData() is phased out.
111 virtual void OnData(AudioInputStream
* stream
,
112 const AudioBus
* source
,
113 uint32 hardware_delay_bytes
,
116 // TODO(henrika): don't use; to be removed.
117 virtual void OnData(AudioInputStream
* stream
,
120 uint32 hardware_delay_bytes
,
123 // There was an error while recording audio. The audio sink cannot be
124 // destroyed yet. No direct action needed by the AudioInputStream, but it
125 // is a good place to stop accumulating sound data since is is likely that
126 // recording will not continue.
127 virtual void OnError(AudioInputStream
* stream
) = 0;
130 virtual ~AudioInputCallback() {}
133 virtual ~AudioInputStream() {}
135 // Open the stream and prepares it for recording. Call Start() to actually
137 virtual bool Open() = 0;
139 // Starts recording audio and generating AudioInputCallback::OnData().
140 // The input stream does not take ownership of this callback.
141 virtual void Start(AudioInputCallback
* callback
) = 0;
143 // Stops recording audio. Effect might not be instantaneous as there could be
144 // pending audio callbacks in the queue which will be issued first before
146 virtual void Stop() = 0;
148 // Close the stream. This also generates AudioInputCallback::OnClose(). This
149 // should be the last call made on this object.
150 virtual void Close() = 0;
152 // Returns the maximum microphone analog volume or 0.0 if device does not
153 // have volume control.
154 virtual double GetMaxVolume() = 0;
156 // Sets the microphone analog volume, with range [0, max_volume] inclusive.
157 virtual void SetVolume(double volume
) = 0;
159 // Returns the microphone analog volume, with range [0, max_volume] inclusive.
160 virtual double GetVolume() = 0;
162 // Sets the Automatic Gain Control (AGC) state.
163 virtual void SetAutomaticGainControl(bool enabled
) = 0;
165 // Returns the Automatic Gain Control (AGC) state.
166 virtual bool GetAutomaticGainControl() = 0;
168 // Returns the current muting state for the microphone.
169 virtual bool IsMuted() = 0;
174 #endif // MEDIA_AUDIO_AUDIO_IO_H_