1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef MEDIA_AUDIO_AUDIO_IO_H_
#define MEDIA_AUDIO_AUDIO_IO_H_
#include "base/basictypes.h"
// Low-level audio output support. To make sound there are 3 objects involved:
// - AudioSource : produces audio samples on a pull model. Implements
// the AudioSourceCallback interface.
// - AudioOutputStream : uses the AudioSource to render audio on a given
// channel, format and sample frequency configuration. Data from the
// AudioSource is delivered in a 'pull' model.
// - AudioManager : factory for the AudioOutputStream objects, manager
// of the hardware resources and mixer control.
//
// The number and configuration of AudioOutputStream does not need to match the
// physically available hardware resources. For example you can have:
//
// MonoPCMSource1 --> MonoPCMStream1 --> | | --> audio left channel
// StereoPCMSource -> StereoPCMStream -> | mixer |
// MonoPCMSource2 --> MonoPCMStream2 --> | | --> audio right channel
//
// This facility's objective is mix and render audio with low overhead using
// the OS basic audio support, abstracting as much as possible the
// idiosyncrasies of each platform. Non-goals:
// - Positional, 3d audio
// - Dependence on non-default libraries such as DirectX 9, 10, XAudio
// - Digital signal processing or effects
// - Extra features if a specific hardware is installed (EAX, X-fi)
//
// The primary client of this facility is audio coming from several tabs.
// Specifically for this case we avoid supporting complex formats such as MP3
// or WMA. Complex format decoding should be done by the renderers.
// Models an audio stream that gets rendered to the audio hardware output.
// Because we support more audio streams than physically available channels
// a given AudioOutputStream might or might not talk directly to hardware.
class AudioOutputStream {
public:
// Audio sources must implement AudioSourceCallback. This interface will be
// called in a random thread which very likely is a high priority thread. Do
// not rely on using this thread TLS or make calls that alter the thread
// itself such as creating Windows or initializing COM.
class AudioSourceCallback {
public:
virtual ~AudioSourceCallback() {}
// Provide more data by filling |dest| up to |max_size| bytes. The provided
// buffer size is usually what is specified in Open(). The source
// will return the number of bytes it filled. The expected structure of
// |dest| is platform and format specific.
// |pending_bytes| is the number of bytes will be played before the
// requested data is played.
virtual uint32 OnMoreData(AudioOutputStream* stream, void* dest,
uint32 max_size, uint32 pending_bytes) = 0;
// The stream is done with this callback. After this call the audio source
// can go away or be destroyed.
virtual void OnClose(AudioOutputStream* stream) = 0;
// There was an error while playing a buffer. Audio source cannot be
// destroyed yet. No direct action needed by the AudioStream, but it is
// a good place to stop accumulating sound data since is is likely that
// playback will not continue. |code| is an error code that is platform
// specific.
virtual void OnError(AudioOutputStream* stream, int code) = 0;
};
// Open the stream. |packet_size| is the requested buffer allocation which
// the audio source thinks it can usually fill without blocking. Internally
// two or three buffers of |packet_size| size are created, one will be
// locked for playback and one will be ready to be filled in the call to
// AudioSourceCallback::OnMoreData().
// The number of buffers is controlled by AUDIO_PCM_LOW_LATENCY. See more
// information below.
//
// TODO(ajwong): Streams are not reusable, so try to move packet_size into the
// constructor.
virtual bool Open(uint32 packet_size) = 0;
// Starts playing audio and generating AudioSourceCallback::OnMoreData().
// Since implementor of AudioOutputStream may have internal buffers, right
// after calling this method initial buffers are fetched.
//
// The output stream does not take ownership of this callback.
virtual void Start(AudioSourceCallback* callback) = 0;
// Stops playing audio. Effect might not be instantaneous as the hardware
// might have locked audio data that is processing.
virtual void Stop() = 0;
// Sets the relative volume, with range [0.0, 1.0] inclusive.
virtual void SetVolume(double volume) = 0;
// Gets the relative volume, with range [0.0, 1.0] inclusive.
virtual void GetVolume(double* volume) = 0;
// Close the stream. This also generates AudioSourceCallback::OnClose().
// After calling this method, the object should not be used anymore.
virtual void Close() = 0;
protected:
virtual ~AudioOutputStream() {}
};
// Models an audio sink receiving recorded audio from the audio driver.
class AudioInputStream {
public:
class AudioInputCallback {
public:
virtual ~AudioInputCallback() {}
// Called by the audio recorder when a full packet of audio data is
// available. This is called from a special audio thread and the
// implementation should return as soon as possible.
virtual void OnData(AudioInputStream* stream, const uint8* src,
uint32 size) = 0;
// The stream is done with this callback, the last call received by this
// audio sink.
virtual void OnClose(AudioInputStream* stream) = 0;
// There was an error while recording audio. The audio sink cannot be
// destroyed yet. No direct action needed by the AudioInputStream, but it
// is a good place to stop accumulating sound data since is is likely that
// recording will not continue. |code| is an error code that is platform
// specific.
virtual void OnError(AudioInputStream* stream, int code) = 0;
};
// Open the stream and prepares it for recording. Call Start() to actually
// begin recording.
virtual bool Open() = 0;
// Starts recording audio and generating AudioInputCallback::OnData().
// The input stream does not take ownership of this callback.
virtual void Start(AudioInputCallback* callback) = 0;
// Stops recording audio. Effect might not be instantaneous as there could be
// pending audio callbacks in the queue which will be issued first before
// recording stops.
virtual void Stop() = 0;
// Close the stream. This also generates AudioInputCallback::OnClose(). This
// should be the last call made on this object.
virtual void Close() = 0;
protected:
virtual ~AudioInputStream() {}
};
// Manages all audio resources. In particular it owns the AudioOutputStream
// objects. Provides some convenience functions that avoid the need to provide
// iterators over the existing streams.
class AudioManager {
public:
enum Format {
AUDIO_PCM_LINEAR = 0, // PCM is 'raw' amplitude samples.
AUDIO_PCM_LOW_LATENCY, // Linear PCM, low latency requested.
AUDIO_MOCK, // Creates a dummy AudioOutputStream object.
AUDIO_LAST_FORMAT // Only used for validation of format.
};
// Telephone quality sample rate, mostly for speech-only audio.
static const uint32 kTelephoneSampleRate = 8000;
// CD sampling rate is 44.1 KHz or conveniently 2x2x3x3x5x5x7x7.
static const uint32 kAudioCDSampleRate = 44100;
// Digital Audio Tape sample rate.
static const uint32 kAudioDATSampleRate = 48000;
// Returns true if the OS reports existence of audio devices. This does not
// guarantee that the existing devices support all formats and sample rates.
virtual bool HasAudioOutputDevices() = 0;
// Returns true if the OS reports existence of audio recording devices. This
// does not guarantee that the existing devices support all formats and
// sample rates.
virtual bool HasAudioInputDevices() = 0;
// Factory for all the supported stream formats. The |channels| can be 1 to 5.
// The |sample_rate| is in hertz and can be any value supported by the
// platform. For some future formats the |sample_rate| and |bits_per_sample|
// can take special values.
// Returns NULL if the combination of the parameters is not supported, or if
// we have reached some other platform specific limit.
//
// AUDIO_PCM_LOW_LATENCY can be passed to this method and it has two effects:
// 1- Instead of triple buffered the audio will be double buffered.
// 2- A low latency driver or alternative audio subsystem will be used when
// available.
//
// Do not free the returned AudioOutputStream. It is owned by AudioManager.
virtual AudioOutputStream* MakeAudioOutputStream(Format format, int channels,
int sample_rate,
char bits_per_sample) = 0;
// Factory to create audio recording streams.
// |channels| can be 1 or 2.
// |sample_rate| is in hertz and can be any value supported by the platform.
// |bits_per_sample| can be any value supported by the platform.
// |samples_per_packet| is in hertz as well and can be 0 to |sample_rate|,
// with 0 suggesting that the implementation use a default value for that
// platform.
// Returns NULL if the combination of the parameters is not supported, or if
// we have reached some other platform specific limit.
//
// Do not free the returned AudioInputStream. It is owned by AudioManager.
// When you are done with it, call |Stop()| and |Close()| to release it.
virtual AudioInputStream* MakeAudioInputStream(Format format, int channels,
int sample_rate,
char bits_per_sample,
uint32 samples_per_packet) = 0;
// Muting continues playback but effectively the volume is set to zero.
// Un-muting returns the volume to the previous level.
virtual void MuteAll() = 0;
virtual void UnMuteAll() = 0;
// Get AudioManager singleton.
// TODO(cpu): Define threading requirements for interacting with AudioManager.
static AudioManager* GetAudioManager();
protected:
virtual ~AudioManager() {}
};
#endif // MEDIA_AUDIO_AUDIO_IO_H_
|