Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / audio / audio_io.h
blob56392154e3229a062a77fd8b1d9ba12090558bca
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
47 // created.
49 namespace media {
51 class MEDIA_EXPORT AudioOutputStream {
52 public:
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 {
58 public:
59 virtual ~AudioSourceCallback() {}
61 // Provide more data by fully filling |dest|. The source will return
62 // the number of frames it filled. |total_bytes_delay| contains current
63 // number of bytes of delay buffered by the AudioOutputStream.
64 virtual int OnMoreData(AudioBus* dest, uint32 total_bytes_delay) = 0;
66 // There was an error while playing a buffer. Audio source cannot be
67 // destroyed yet. No direct action needed by the AudioStream, but it is
68 // a good place to stop accumulating sound data since is is likely that
69 // playback will not continue.
70 virtual void OnError(AudioOutputStream* stream) = 0;
73 virtual ~AudioOutputStream() {}
75 // Open the stream. false is returned if the stream cannot be opened. Open()
76 // must always be followed by a call to Close() even if Open() fails.
77 virtual bool Open() = 0;
79 // Starts playing audio and generating AudioSourceCallback::OnMoreData().
80 // Since implementor of AudioOutputStream may have internal buffers, right
81 // after calling this method initial buffers are fetched.
83 // The output stream does not take ownership of this callback.
84 virtual void Start(AudioSourceCallback* callback) = 0;
86 // Stops playing audio. Effect might not be instantaneous as the hardware
87 // might have locked audio data that is processing.
88 virtual void Stop() = 0;
90 // Sets the relative volume, with range [0.0, 1.0] inclusive.
91 virtual void SetVolume(double volume) = 0;
93 // Gets the relative volume, with range [0.0, 1.0] inclusive.
94 virtual void GetVolume(double* volume) = 0;
96 // Close the stream. This also generates AudioSourceCallback::OnClose().
97 // After calling this method, the object should not be used anymore.
98 virtual void Close() = 0;
101 // Models an audio sink receiving recorded audio from the audio driver.
102 class MEDIA_EXPORT AudioInputStream {
103 public:
104 class MEDIA_EXPORT AudioInputCallback {
105 public:
106 // Called by the audio recorder when a full packet of audio data is
107 // available. This is called from a special audio thread and the
108 // implementation should return as soon as possible.
109 // TODO(henrika): should be pure virtual when old OnData() is phased out.
110 virtual void OnData(AudioInputStream* stream,
111 const AudioBus* source,
112 uint32 hardware_delay_bytes,
113 double volume) {};
115 // TODO(henrika): don't use; to be removed.
116 virtual void OnData(AudioInputStream* stream,
117 const uint8* src,
118 uint32 size,
119 uint32 hardware_delay_bytes,
120 double volume) {};
122 // There was an error while recording audio. The audio sink cannot be
123 // destroyed yet. No direct action needed by the AudioInputStream, but it
124 // is a good place to stop accumulating sound data since is is likely that
125 // recording will not continue.
126 virtual void OnError(AudioInputStream* stream) = 0;
128 protected:
129 virtual ~AudioInputCallback() {}
132 virtual ~AudioInputStream() {}
134 // Open the stream and prepares it for recording. Call Start() to actually
135 // begin recording.
136 virtual bool Open() = 0;
138 // Starts recording audio and generating AudioInputCallback::OnData().
139 // The input stream does not take ownership of this callback.
140 virtual void Start(AudioInputCallback* callback) = 0;
142 // Stops recording audio. Effect might not be instantaneous as there could be
143 // pending audio callbacks in the queue which will be issued first before
144 // recording stops.
145 virtual void Stop() = 0;
147 // Close the stream. This also generates AudioInputCallback::OnClose(). This
148 // should be the last call made on this object.
149 virtual void Close() = 0;
151 // Returns the maximum microphone analog volume or 0.0 if device does not
152 // have volume control.
153 virtual double GetMaxVolume() = 0;
155 // Sets the microphone analog volume, with range [0, max_volume] inclusive.
156 virtual void SetVolume(double volume) = 0;
158 // Returns the microphone analog volume, with range [0, max_volume] inclusive.
159 virtual double GetVolume() = 0;
161 // Sets the Automatic Gain Control (AGC) state.
162 virtual bool SetAutomaticGainControl(bool enabled) = 0;
164 // Returns the Automatic Gain Control (AGC) state.
165 virtual bool GetAutomaticGainControl() = 0;
167 // Returns the current muting state for the microphone.
168 virtual bool IsMuted() = 0;
171 } // namespace media
173 #endif // MEDIA_AUDIO_AUDIO_IO_H_