blob: 0f2c323a4c05f09b899b7fb7f8b41e0e8bef6c70 [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_AUDIOTRACK_H
18#define ANDROID_AUDIOTRACK_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <media/IAudioFlinger.h>
24#include <media/IAudioTrack.h>
25#include <media/AudioSystem.h>
26
27#include <utils/RefBase.h>
28#include <utils/Errors.h>
Mathias Agopian75624082009-05-19 19:08:10 -070029#include <binder/IInterface.h>
30#include <binder/IMemory.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080031#include <utils/threads.h>
32
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080033namespace android {
34
35// ----------------------------------------------------------------------------
36
37class audio_track_cblk_t;
38
39// ----------------------------------------------------------------------------
40
Glenn Kastenb68a91a2011-11-15 13:55:13 -080041class AudioTrack : virtual public RefBase
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080042{
43public:
44 enum channel_index {
45 MONO = 0,
46 LEFT = 0,
47 RIGHT = 1
48 };
49
50 /* Events used by AudioTrack callback function (audio_track_cblk_t).
51 */
52 enum event_type {
53 EVENT_MORE_DATA = 0, // Request to write more data to PCM buffer.
54 EVENT_UNDERRUN = 1, // PCM buffer underrun occured.
55 EVENT_LOOP_END = 2, // Sample loop end was reached; playback restarted from loop start if loop count was not 0.
56 EVENT_MARKER = 3, // Playback head is at the specified marker position (See setMarkerPosition()).
57 EVENT_NEW_POS = 4, // Playback head is at a new position (See setPositionUpdatePeriod()).
58 EVENT_BUFFER_END = 5 // Playback head is at the end of the buffer.
59 };
60
Glenn Kasten99e53b82012-01-19 08:59:58 -080061 /* Client should declare Buffer on the stack and pass address to obtainBuffer()
62 * and releaseBuffer(). See also callback_t for EVENT_MORE_DATA.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080063 */
64
65 class Buffer
66 {
67 public:
68 enum {
69 MUTE = 0x00000001
70 };
Glenn Kasten99e53b82012-01-19 08:59:58 -080071 uint32_t flags; // 0 or MUTE
Glenn Kastene1c39622012-01-04 09:36:37 -080072 audio_format_t format; // but AUDIO_FORMAT_PCM_8_BIT -> AUDIO_FORMAT_PCM_16_BIT
73 // accessed directly by WebKit ANP callback
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070074 int channelCount; // will be removed in the future, do not use
Glenn Kasten99e53b82012-01-19 08:59:58 -080075
76 size_t frameCount; // number of sample frames corresponding to size;
77 // on input it is the number of frames desired,
78 // on output is the number of frames actually filled
79
80 size_t size; // input/output in byte units
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080081 union {
82 void* raw;
Glenn Kasten362c4e62011-12-14 10:28:06 -080083 short* i16; // signed 16-bit
84 int8_t* i8; // unsigned 8-bit, offset by 0x80
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080085 };
86 };
87
88
89 /* As a convenience, if a callback is supplied, a handler thread
90 * is automatically created with the appropriate priority. This thread
Glenn Kasten99e53b82012-01-19 08:59:58 -080091 * invokes the callback when a new buffer becomes available or various conditions occur.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080092 * Parameters:
93 *
94 * event: type of event notified (see enum AudioTrack::event_type).
95 * user: Pointer to context for use by the callback receiver.
96 * info: Pointer to optional parameter according to event type:
97 * - EVENT_MORE_DATA: pointer to AudioTrack::Buffer struct. The callback must not write
Glenn Kasten99e53b82012-01-19 08:59:58 -080098 * more bytes than indicated by 'size' field and update 'size' if fewer bytes are
99 * written.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800100 * - EVENT_UNDERRUN: unused.
101 * - EVENT_LOOP_END: pointer to an int indicating the number of loops remaining.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800102 * - EVENT_MARKER: pointer to an uint32_t containing the marker position in frames.
103 * - EVENT_NEW_POS: pointer to an uint32_t containing the new position in frames.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800104 * - EVENT_BUFFER_END: unused.
105 */
106
Glenn Kastend217a8c2011-06-01 15:20:35 -0700107 typedef void (*callback_t)(int event, void* user, void *info);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800108
Chia-chi Yeh33005a92010-06-16 06:33:13 +0800109 /* Returns the minimum frame count required for the successful creation of
110 * an AudioTrack object.
111 * Returned status (from utils/Errors.h) can be:
112 * - NO_ERROR: successful operation
113 * - NO_INIT: audio server or audio hardware not initialized
114 */
115
116 static status_t getMinFrameCount(int* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -0800117 audio_stream_type_t streamType = AUDIO_STREAM_DEFAULT,
Chia-chi Yeh33005a92010-06-16 06:33:13 +0800118 uint32_t sampleRate = 0);
119
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800120 /* Constructs an uninitialized AudioTrack. No connection with
121 * AudioFlinger takes place.
122 */
123 AudioTrack();
124
125 /* Creates an audio track and registers it with AudioFlinger.
126 * Once created, the track needs to be started before it can be used.
127 * Unspecified values are set to the audio hardware's current
128 * values.
129 *
130 * Parameters:
131 *
132 * streamType: Select the type of audio stream this track is attached to
Dima Zavinfce7a472011-04-19 22:30:36 -0700133 * (e.g. AUDIO_STREAM_MUSIC).
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800134 * sampleRate: Track sampling rate in Hz.
Dima Zavinfce7a472011-04-19 22:30:36 -0700135 * format: Audio format (e.g AUDIO_FORMAT_PCM_16_BIT for signed
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800136 * 16 bits per sample).
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700137 * channelMask: Channel mask: see audio_channels_t.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800138 * frameCount: Total size of track PCM buffer in frames. This defines the
139 * latency of the track.
140 * flags: Reserved for future use.
141 * cbf: Callback function. If not null, this function is called periodically
142 * to request new PCM data.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800143 * user: Context for use by the callback receiver.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800144 * notificationFrames: The callback function is called each time notificationFrames PCM
Glenn Kasten362c4e62011-12-14 10:28:06 -0800145 * frames have been consumed from track input buffer.
146 * sessionId: Specific session ID, or zero to use default.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800147 */
148
Glenn Kastenfff6d712012-01-12 16:38:12 -0800149 AudioTrack( audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800150 uint32_t sampleRate = 0,
Glenn Kastene1c39622012-01-04 09:36:37 -0800151 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700152 int channelMask = 0,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800153 int frameCount = 0,
154 uint32_t flags = 0,
Glenn Kastena0d68332012-01-27 16:47:15 -0800155 callback_t cbf = NULL,
156 void* user = NULL,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700157 int notificationFrames = 0,
158 int sessionId = 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800159
Andreas Huberc8139852012-01-18 10:51:55 -0800160 // DEPRECATED
161 explicit AudioTrack( int streamType,
162 uint32_t sampleRate = 0,
163 int format = AUDIO_FORMAT_DEFAULT,
164 int channelMask = 0,
165 int frameCount = 0,
166 uint32_t flags = 0,
167 callback_t cbf = 0,
168 void* user = 0,
169 int notificationFrames = 0,
170 int sessionId = 0);
171
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800172 /* Creates an audio track and registers it with AudioFlinger. With this constructor,
Glenn Kasten362c4e62011-12-14 10:28:06 -0800173 * the PCM data to be rendered by AudioTrack is passed in a shared memory buffer
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800174 * identified by the argument sharedBuffer. This prototype is for static buffer playback.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800175 * PCM data must be present in memory before the AudioTrack is started.
176 * The write() and flush() methods are not supported in this case.
177 * It is recommended to pass a callback function to be notified of playback end by an
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800178 * EVENT_UNDERRUN event.
179 */
180
Glenn Kastenfff6d712012-01-12 16:38:12 -0800181 AudioTrack( audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800182 uint32_t sampleRate = 0,
Glenn Kastene1c39622012-01-04 09:36:37 -0800183 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700184 int channelMask = 0,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800185 const sp<IMemory>& sharedBuffer = 0,
186 uint32_t flags = 0,
Glenn Kastena0d68332012-01-27 16:47:15 -0800187 callback_t cbf = NULL,
188 void* user = NULL,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700189 int notificationFrames = 0,
190 int sessionId = 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800191
192 /* Terminates the AudioTrack and unregisters it from AudioFlinger.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800193 * Also destroys all resources associated with the AudioTrack.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800194 */
195 ~AudioTrack();
196
197
198 /* Initialize an uninitialized AudioTrack.
199 * Returned status (from utils/Errors.h) can be:
Glenn Kasten362c4e62011-12-14 10:28:06 -0800200 * - NO_ERROR: successful initialization
201 * - INVALID_OPERATION: AudioTrack is already initialized
Eric Laurentc2f1f072009-07-17 12:17:14 -0700202 * - BAD_VALUE: invalid parameter (channels, format, sampleRate...)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800203 * - NO_INIT: audio server or audio hardware not initialized
204 * */
Glenn Kastenfff6d712012-01-12 16:38:12 -0800205 status_t set(audio_stream_type_t streamType = AUDIO_STREAM_DEFAULT,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800206 uint32_t sampleRate = 0,
Glenn Kastene1c39622012-01-04 09:36:37 -0800207 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700208 int channelMask = 0,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800209 int frameCount = 0,
210 uint32_t flags = 0,
Glenn Kastena0d68332012-01-27 16:47:15 -0800211 callback_t cbf = NULL,
212 void* user = NULL,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800213 int notificationFrames = 0,
214 const sp<IMemory>& sharedBuffer = 0,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700215 bool threadCanCallJava = false,
216 int sessionId = 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800217
218
219 /* Result of constructing the AudioTrack. This must be checked
Glenn Kasten362c4e62011-12-14 10:28:06 -0800220 * before using any AudioTrack API (except for set()), because using
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800221 * an uninitialized AudioTrack produces undefined results.
222 * See set() method above for possible return codes.
223 */
224 status_t initCheck() const;
225
Glenn Kasten362c4e62011-12-14 10:28:06 -0800226 /* Returns this track's estimated latency in milliseconds.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800227 * This includes the latency due to AudioTrack buffer size, AudioMixer (if any)
228 * and audio hardware driver.
229 */
230 uint32_t latency() const;
231
Glenn Kasten99e53b82012-01-19 08:59:58 -0800232 /* getters, see constructors and set() */
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800233
Glenn Kastenfff6d712012-01-12 16:38:12 -0800234 audio_stream_type_t streamType() const;
Glenn Kastene1c39622012-01-04 09:36:37 -0800235 audio_format_t format() const;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800236 int channelCount() const;
237 uint32_t frameCount() const;
Glenn Kastenb9980652012-01-11 09:48:27 -0800238
239 /* Return channelCount * (bit depth per channel / 8).
240 * channelCount is determined from channelMask, and bit depth comes from format.
241 */
242 size_t frameSize() const;
243
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800244 sp<IMemory>& sharedBuffer();
245
246
247 /* After it's created the track is not active. Call start() to
248 * make it active. If set, the callback will start being called.
249 */
250 void start();
251
252 /* Stop a track. If set, the callback will cease being called and
253 * obtainBuffer returns STOPPED. Note that obtainBuffer() still works
254 * and will fill up buffers until the pool is exhausted.
255 */
256 void stop();
257 bool stopped() const;
258
Glenn Kasten362c4e62011-12-14 10:28:06 -0800259 /* Flush a stopped track. All pending buffers are discarded.
260 * This function has no effect if the track is not stopped.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800261 */
262 void flush();
263
264 /* Pause a track. If set, the callback will cease being called and
265 * obtainBuffer returns STOPPED. Note that obtainBuffer() still works
266 * and will fill up buffers until the pool is exhausted.
267 */
268 void pause();
269
Glenn Kasten362c4e62011-12-14 10:28:06 -0800270 /* Mute or unmute this track.
271 * While muted, the callback, if set, is still called.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800272 */
273 void mute(bool);
274 bool muted() const;
275
Glenn Kasten362c4e62011-12-14 10:28:06 -0800276 /* Set volume for this track, mostly used for games' sound effects
277 * left and right volumes. Levels must be >= 0.0 and <= 1.0.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800278 */
Eric Laurentbe916aa2010-06-01 23:49:17 -0700279 status_t setVolume(float left, float right);
Glenn Kastena5224f32012-01-04 12:41:44 -0800280 void getVolume(float* left, float* right) const;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800281
Glenn Kasten362c4e62011-12-14 10:28:06 -0800282 /* Set the send level for this track. An auxiliary effect should be attached
283 * to the track with attachEffect(). Level must be >= 0.0 and <= 1.0.
Eric Laurentbe916aa2010-06-01 23:49:17 -0700284 */
Eric Laurent2beeb502010-07-16 07:43:46 -0700285 status_t setAuxEffectSendLevel(float level);
Glenn Kastena5224f32012-01-04 12:41:44 -0800286 void getAuxEffectSendLevel(float* level) const;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700287
Glenn Kasten362c4e62011-12-14 10:28:06 -0800288 /* Set sample rate for this track, mostly used for games' sound effects
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800289 */
Eric Laurent57326622009-07-07 07:10:45 -0700290 status_t setSampleRate(int sampleRate);
Glenn Kastena5224f32012-01-04 12:41:44 -0800291 uint32_t getSampleRate() const;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800292
293 /* Enables looping and sets the start and end points of looping.
294 *
295 * Parameters:
296 *
297 * loopStart: loop start expressed as the number of PCM frames played since AudioTrack start.
298 * loopEnd: loop end expressed as the number of PCM frames played since AudioTrack start.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800299 * loopCount: number of loops to execute. Calling setLoop() with loopCount == 0 cancels any
300 * pending or active loop. loopCount = -1 means infinite looping.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800301 *
302 * For proper operation the following condition must be respected:
303 * (loopEnd-loopStart) <= framecount()
304 */
305 status_t setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount);
Glenn Kastena5224f32012-01-04 12:41:44 -0800306 status_t getLoop(uint32_t *loopStart, uint32_t *loopEnd, int *loopCount) const;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800307
Glenn Kasten362c4e62011-12-14 10:28:06 -0800308 /* Sets marker position. When playback reaches the number of frames specified, a callback with
309 * event type EVENT_MARKER is called. Calling setMarkerPosition with marker == 0 cancels marker
310 * notification callback.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800311 * If the AudioTrack has been opened with no callback function associated, the operation will fail.
312 *
313 * Parameters:
314 *
315 * marker: marker position expressed in frames.
316 *
317 * Returned status (from utils/Errors.h) can be:
318 * - NO_ERROR: successful operation
319 * - INVALID_OPERATION: the AudioTrack has no callback installed.
320 */
321 status_t setMarkerPosition(uint32_t marker);
Glenn Kastena5224f32012-01-04 12:41:44 -0800322 status_t getMarkerPosition(uint32_t *marker) const;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800323
324
Glenn Kasten362c4e62011-12-14 10:28:06 -0800325 /* Sets position update period. Every time the number of frames specified has been played,
326 * a callback with event type EVENT_NEW_POS is called.
327 * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification
328 * callback.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800329 * If the AudioTrack has been opened with no callback function associated, the operation will fail.
330 *
331 * Parameters:
332 *
333 * updatePeriod: position update notification period expressed in frames.
334 *
335 * Returned status (from utils/Errors.h) can be:
336 * - NO_ERROR: successful operation
337 * - INVALID_OPERATION: the AudioTrack has no callback installed.
338 */
339 status_t setPositionUpdatePeriod(uint32_t updatePeriod);
Glenn Kastena5224f32012-01-04 12:41:44 -0800340 status_t getPositionUpdatePeriod(uint32_t *updatePeriod) const;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800341
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800342 /* Sets playback head position within AudioTrack buffer. The new position is specified
Glenn Kasten362c4e62011-12-14 10:28:06 -0800343 * in number of frames.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800344 * This method must be called with the AudioTrack in paused or stopped state.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800345 * Note that the actual position set is <position> modulo the AudioTrack buffer size in frames.
346 * Therefore using this method makes sense only when playing a "static" audio buffer
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800347 * as opposed to streaming.
348 * The getPosition() method on the other hand returns the total number of frames played since
349 * playback start.
350 *
351 * Parameters:
352 *
353 * position: New playback head position within AudioTrack buffer.
354 *
355 * Returned status (from utils/Errors.h) can be:
356 * - NO_ERROR: successful operation
357 * - INVALID_OPERATION: the AudioTrack is not stopped.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800358 * - BAD_VALUE: The specified position is beyond the number of frames present in AudioTrack buffer
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800359 */
360 status_t setPosition(uint32_t position);
361 status_t getPosition(uint32_t *position);
362
Glenn Kasten362c4e62011-12-14 10:28:06 -0800363 /* Forces AudioTrack buffer full condition. When playing a static buffer, this method avoids
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800364 * rewriting the buffer before restarting playback after a stop.
365 * This method must be called with the AudioTrack in paused or stopped state.
366 *
367 * Returned status (from utils/Errors.h) can be:
368 * - NO_ERROR: successful operation
369 * - INVALID_OPERATION: the AudioTrack is not stopped.
370 */
371 status_t reload();
372
Glenn Kasten362c4e62011-12-14 10:28:06 -0800373 /* Returns a handle on the audio output used by this AudioTrack.
Eric Laurentc2f1f072009-07-17 12:17:14 -0700374 *
375 * Parameters:
376 * none.
377 *
378 * Returned value:
379 * handle on audio hardware output
380 */
381 audio_io_handle_t getOutput();
382
Glenn Kasten362c4e62011-12-14 10:28:06 -0800383 /* Returns the unique session ID associated with this track.
Eric Laurentbe916aa2010-06-01 23:49:17 -0700384 *
385 * Parameters:
386 * none.
387 *
388 * Returned value:
Glenn Kasten362c4e62011-12-14 10:28:06 -0800389 * AudioTrack session ID.
Eric Laurentbe916aa2010-06-01 23:49:17 -0700390 */
Glenn Kastena5224f32012-01-04 12:41:44 -0800391 int getSessionId() const;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700392
Glenn Kasten362c4e62011-12-14 10:28:06 -0800393 /* Attach track auxiliary output to specified effect. Use effectId = 0
Eric Laurentbe916aa2010-06-01 23:49:17 -0700394 * to detach track from effect.
395 *
396 * Parameters:
397 *
398 * effectId: effectId obtained from AudioEffect::id().
399 *
400 * Returned status (from utils/Errors.h) can be:
401 * - NO_ERROR: successful operation
402 * - INVALID_OPERATION: the effect is not an auxiliary effect.
403 * - BAD_VALUE: The specified effect ID is invalid
404 */
405 status_t attachAuxEffect(int effectId);
406
Glenn Kasten362c4e62011-12-14 10:28:06 -0800407 /* Obtains a buffer of "frameCount" frames. The buffer must be
Glenn Kasten99e53b82012-01-19 08:59:58 -0800408 * filled entirely, and then released with releaseBuffer().
409 * If the track is stopped, obtainBuffer() returns
Glenn Kasten362c4e62011-12-14 10:28:06 -0800410 * STOPPED instead of NO_ERROR as long as there are buffers available,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800411 * at which point NO_MORE_BUFFERS is returned.
412 * Buffers will be returned until the pool (buffercount())
413 * is exhausted, at which point obtainBuffer() will either block
414 * or return WOULD_BLOCK depending on the value of the "blocking"
415 * parameter.
Glenn Kasten99e53b82012-01-19 08:59:58 -0800416 *
417 * Interpretation of waitCount:
418 * +n limits wait time to n * WAIT_PERIOD_MS,
419 * -1 causes an (almost) infinite wait time,
420 * 0 non-blocking.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800421 */
422
423 enum {
Glenn Kasten335787f2012-01-20 17:00:00 -0800424 NO_MORE_BUFFERS = 0x80000001, // same name in AudioFlinger.h, ok to be different value
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800425 STOPPED = 1
426 };
427
428 status_t obtainBuffer(Buffer* audioBuffer, int32_t waitCount);
Glenn Kasten99e53b82012-01-19 08:59:58 -0800429
430 /* Release a filled buffer of "frameCount" frames for AudioFlinger to process. */
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800431 void releaseBuffer(Buffer* audioBuffer);
432
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800433 /* As a convenience we provide a write() interface to the audio buffer.
Glenn Kasten99e53b82012-01-19 08:59:58 -0800434 * This is implemented on top of obtainBuffer/releaseBuffer. For best
435 * performance use callbacks. Returns actual number of bytes written >= 0,
436 * or one of the following negative status codes:
437 * INVALID_OPERATION AudioTrack is configured for shared buffer mode
438 * BAD_VALUE size is invalid
439 * STOPPED AudioTrack was stopped during the write
440 * NO_MORE_BUFFERS when obtainBuffer() returns same
441 * or any other error code returned by IAudioTrack::start() or restoreTrack_l().
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800442 */
443 ssize_t write(const void* buffer, size_t size);
444
445 /*
446 * Dumps the state of an audio track.
447 */
448 status_t dump(int fd, const Vector<String16>& args) const;
449
450private:
451 /* copying audio tracks is not allowed */
452 AudioTrack(const AudioTrack& other);
453 AudioTrack& operator = (const AudioTrack& other);
454
455 /* a small internal class to handle the callback */
456 class AudioTrackThread : public Thread
457 {
458 public:
459 AudioTrackThread(AudioTrack& receiver, bool bCanCallJava = false);
460 private:
461 friend class AudioTrack;
462 virtual bool threadLoop();
463 virtual status_t readyToRun();
464 virtual void onFirstRef();
465 AudioTrack& mReceiver;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800466 };
467
Glenn Kasten99e53b82012-01-19 08:59:58 -0800468 // body of AudioTrackThread::threadLoop()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800469 bool processAudioBuffer(const sp<AudioTrackThread>& thread);
Glenn Kastenfff6d712012-01-12 16:38:12 -0800470 status_t createTrack_l(audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800471 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800472 audio_format_t format,
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700473 uint32_t channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800474 int frameCount,
475 uint32_t flags,
476 const sp<IMemory>& sharedBuffer,
Eric Laurentd1b449a2010-05-14 03:26:45 -0700477 audio_io_handle_t output,
478 bool enforceFrameCount);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800479 void flush_l();
480 status_t setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount);
481 audio_io_handle_t getOutput_l();
482 status_t restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800483 bool stopped_l() const { return !mActive; }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800484
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485 sp<IAudioTrack> mAudioTrack;
486 sp<IMemory> mCblkMemory;
487 sp<AudioTrackThread> mAudioTrackThread;
488
489 float mVolume[2];
Eric Laurentbe916aa2010-06-01 23:49:17 -0700490 float mSendLevel;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800491 uint32_t mFrameCount;
492
493 audio_track_cblk_t* mCblk;
Glenn Kastene1c39622012-01-04 09:36:37 -0800494 audio_format_t mFormat;
Glenn Kastenfff6d712012-01-12 16:38:12 -0800495 audio_stream_type_t mStreamType;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800496 uint8_t mChannelCount;
497 uint8_t mMuted;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700498 uint8_t mReserved;
499 uint32_t mChannelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800500 status_t mStatus;
501 uint32_t mLatency;
502
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800503 bool mActive; // protected by mLock
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800504
Glenn Kasten99e53b82012-01-19 08:59:58 -0800505 callback_t mCbf; // callback handler for events, or NULL
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800506 void* mUserData;
Eric Laurentd1b449a2010-05-14 03:26:45 -0700507 uint32_t mNotificationFramesReq; // requested number of frames between each notification callback
508 uint32_t mNotificationFramesAct; // actual number of frames between each notification callback
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800509 sp<IMemory> mSharedBuffer;
510 int mLoopCount;
511 uint32_t mRemainingFrames;
512 uint32_t mMarkerPosition;
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700513 bool mMarkerReached;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800514 uint32_t mNewPosition;
515 uint32_t mUpdatePeriod;
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700516 bool mFlushed; // FIXME will be made obsolete by making flush() synchronous
Eric Laurentc2f1f072009-07-17 12:17:14 -0700517 uint32_t mFlags;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700518 int mSessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700519 int mAuxEffectId;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800520 mutable Mutex mLock;
Eric Laurentcfe2ba62011-09-13 15:04:17 -0700521 status_t mRestoreStatus;
Glenn Kasten87913512011-06-22 16:15:25 -0700522 int mPreviousPriority; // before start()
523 int mPreviousSchedulingGroup;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800524};
525
526
527}; // namespace android
528
529#endif // ANDROID_AUDIOTRACK_H