blob: 6d190924db6a560cab7c1a92c1274b6069368493 [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>
Glenn Kastena6364332012-04-19 09:35:04 -070031#include <cutils/sched_policy.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080032#include <utils/threads.h>
33
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080034namespace android {
35
36// ----------------------------------------------------------------------------
37
38class audio_track_cblk_t;
39
40// ----------------------------------------------------------------------------
41
Glenn Kastenb68a91a2011-11-15 13:55:13 -080042class AudioTrack : virtual public RefBase
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080043{
44public:
45 enum channel_index {
46 MONO = 0,
47 LEFT = 0,
48 RIGHT = 1
49 };
50
51 /* Events used by AudioTrack callback function (audio_track_cblk_t).
Glenn Kastenad2f6db2012-11-01 15:45:06 -070052 * Keep in sync with frameworks/base/media/java/android/media/AudioTrack.java NATIVE_EVENT_*.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080053 */
54 enum event_type {
55 EVENT_MORE_DATA = 0, // Request to write more data to PCM buffer.
Glenn Kastenad2f6db2012-11-01 15:45:06 -070056 EVENT_UNDERRUN = 1, // PCM buffer underrun occurred.
Glenn Kasten85ab62c2012-11-01 11:11:38 -070057 EVENT_LOOP_END = 2, // Sample loop end was reached; playback restarted from
58 // loop start if loop count was not 0.
59 EVENT_MARKER = 3, // Playback head is at the specified marker position
60 // (See setMarkerPosition()).
61 EVENT_NEW_POS = 4, // Playback head is at a new position
62 // (See setPositionUpdatePeriod()).
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080063 EVENT_BUFFER_END = 5 // Playback head is at the end of the buffer.
64 };
65
Glenn Kasten99e53b82012-01-19 08:59:58 -080066 /* Client should declare Buffer on the stack and pass address to obtainBuffer()
67 * and releaseBuffer(). See also callback_t for EVENT_MORE_DATA.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080068 */
69
70 class Buffer
71 {
72 public:
73 enum {
74 MUTE = 0x00000001
75 };
Glenn Kasten99e53b82012-01-19 08:59:58 -080076 uint32_t flags; // 0 or MUTE
Glenn Kastenad2f6db2012-11-01 15:45:06 -070077 audio_format_t format; // but AUDIO_FORMAT_PCM_8_BIT -> AUDIO_FORMAT_PCM_16_BIT
Glenn Kastene1c39622012-01-04 09:36:37 -080078 // accessed directly by WebKit ANP callback
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070079 int channelCount; // will be removed in the future, do not use
Glenn Kasten99e53b82012-01-19 08:59:58 -080080
81 size_t frameCount; // number of sample frames corresponding to size;
82 // on input it is the number of frames desired,
83 // on output is the number of frames actually filled
84
85 size_t size; // input/output in byte units
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080086 union {
87 void* raw;
Glenn Kasten362c4e62011-12-14 10:28:06 -080088 short* i16; // signed 16-bit
89 int8_t* i8; // unsigned 8-bit, offset by 0x80
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080090 };
91 };
92
93
94 /* As a convenience, if a callback is supplied, a handler thread
95 * is automatically created with the appropriate priority. This thread
Glenn Kasten99e53b82012-01-19 08:59:58 -080096 * invokes the callback when a new buffer becomes available or various conditions occur.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080097 * Parameters:
98 *
99 * event: type of event notified (see enum AudioTrack::event_type).
100 * user: Pointer to context for use by the callback receiver.
101 * info: Pointer to optional parameter according to event type:
102 * - EVENT_MORE_DATA: pointer to AudioTrack::Buffer struct. The callback must not write
Glenn Kasten99e53b82012-01-19 08:59:58 -0800103 * more bytes than indicated by 'size' field and update 'size' if fewer bytes are
104 * written.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800105 * - EVENT_UNDERRUN: unused.
106 * - EVENT_LOOP_END: pointer to an int indicating the number of loops remaining.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800107 * - EVENT_MARKER: pointer to an uint32_t containing the marker position in frames.
108 * - EVENT_NEW_POS: pointer to an uint32_t containing the new position in frames.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800109 * - EVENT_BUFFER_END: unused.
110 */
111
Glenn Kastend217a8c2011-06-01 15:20:35 -0700112 typedef void (*callback_t)(int event, void* user, void *info);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800113
Chia-chi Yeh33005a92010-06-16 06:33:13 +0800114 /* Returns the minimum frame count required for the successful creation of
115 * an AudioTrack object.
116 * Returned status (from utils/Errors.h) can be:
117 * - NO_ERROR: successful operation
118 * - NO_INIT: audio server or audio hardware not initialized
119 */
120
121 static status_t getMinFrameCount(int* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -0800122 audio_stream_type_t streamType = AUDIO_STREAM_DEFAULT,
Chia-chi Yeh33005a92010-06-16 06:33:13 +0800123 uint32_t sampleRate = 0);
124
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800125 /* Constructs an uninitialized AudioTrack. No connection with
126 * AudioFlinger takes place.
127 */
128 AudioTrack();
129
Glenn Kastenad2f6db2012-11-01 15:45:06 -0700130 /* Creates an AudioTrack object and registers it with AudioFlinger.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800131 * Once created, the track needs to be started before it can be used.
132 * Unspecified values are set to the audio hardware's current
133 * values.
134 *
135 * Parameters:
136 *
137 * streamType: Select the type of audio stream this track is attached to
Dima Zavinfce7a472011-04-19 22:30:36 -0700138 * (e.g. AUDIO_STREAM_MUSIC).
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800139 * sampleRate: Track sampling rate in Hz.
Dima Zavinfce7a472011-04-19 22:30:36 -0700140 * format: Audio format (e.g AUDIO_FORMAT_PCM_16_BIT for signed
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800141 * 16 bits per sample).
Glenn Kasten28b76b32012-07-03 17:24:41 -0700142 * channelMask: Channel mask.
Eric Laurentd8d61852012-03-05 17:06:40 -0800143 * frameCount: Minimum size of track PCM buffer in frames. This defines the
Glenn Kastenad2f6db2012-11-01 15:45:06 -0700144 * application's contribution to the
Eric Laurentd8d61852012-03-05 17:06:40 -0800145 * latency of the track. The actual size selected by the AudioTrack could be
146 * larger if the requested size is not compatible with current audio HAL
Glenn Kasten3acbd052012-02-28 10:39:56 -0800147 * latency. Zero means to use a default value.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700148 * flags: See comments on audio_output_flags_t in <system/audio.h>.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800149 * cbf: Callback function. If not null, this function is called periodically
Glenn Kastenad2f6db2012-11-01 15:45:06 -0700150 * to provide new PCM data.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800151 * user: Context for use by the callback receiver.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800152 * notificationFrames: The callback function is called each time notificationFrames PCM
Glenn Kasten362c4e62011-12-14 10:28:06 -0800153 * frames have been consumed from track input buffer.
154 * sessionId: Specific session ID, or zero to use default.
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700155 * threadCanCallJava: Whether callbacks are made from an attached thread and thus can call JNI.
156 * If not present in parameter list, then fixed at false.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800157 */
158
Glenn Kastenfff6d712012-01-12 16:38:12 -0800159 AudioTrack( audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800160 uint32_t sampleRate = 0,
Glenn Kastene1c39622012-01-04 09:36:37 -0800161 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700162 audio_channel_mask_t channelMask = 0,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800163 int frameCount = 0,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700164 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
Glenn Kastena0d68332012-01-27 16:47:15 -0800165 callback_t cbf = NULL,
166 void* user = NULL,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700167 int notificationFrames = 0,
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700168 int sessionId = 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800169
Andreas Huberc8139852012-01-18 10:51:55 -0800170 // DEPRECATED
171 explicit AudioTrack( int streamType,
172 uint32_t sampleRate = 0,
173 int format = AUDIO_FORMAT_DEFAULT,
174 int channelMask = 0,
175 int frameCount = 0,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700176 uint32_t flags = (uint32_t) AUDIO_OUTPUT_FLAG_NONE,
Andreas Huberc8139852012-01-18 10:51:55 -0800177 callback_t cbf = 0,
178 void* user = 0,
179 int notificationFrames = 0,
Glenn Kastenea7939a2012-03-14 12:56:26 -0700180 int sessionId = 0);
Andreas Huberc8139852012-01-18 10:51:55 -0800181
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800182 /* Creates an audio track and registers it with AudioFlinger. With this constructor,
Glenn Kasten362c4e62011-12-14 10:28:06 -0800183 * 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 -0800184 * identified by the argument sharedBuffer. This prototype is for static buffer playback.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800185 * PCM data must be present in memory before the AudioTrack is started.
186 * The write() and flush() methods are not supported in this case.
187 * 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 -0800188 * EVENT_UNDERRUN event.
189 */
190
Glenn Kastenfff6d712012-01-12 16:38:12 -0800191 AudioTrack( audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800192 uint32_t sampleRate = 0,
Glenn Kastene1c39622012-01-04 09:36:37 -0800193 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700194 audio_channel_mask_t channelMask = 0,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800195 const sp<IMemory>& sharedBuffer = 0,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700196 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
Glenn Kastena0d68332012-01-27 16:47:15 -0800197 callback_t cbf = NULL,
198 void* user = NULL,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700199 int notificationFrames = 0,
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700200 int sessionId = 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800201
202 /* Terminates the AudioTrack and unregisters it from AudioFlinger.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800203 * Also destroys all resources associated with the AudioTrack.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800204 */
205 ~AudioTrack();
206
207
208 /* Initialize an uninitialized AudioTrack.
209 * Returned status (from utils/Errors.h) can be:
Glenn Kasten362c4e62011-12-14 10:28:06 -0800210 * - NO_ERROR: successful initialization
211 * - INVALID_OPERATION: AudioTrack is already initialized
Glenn Kasten28b76b32012-07-03 17:24:41 -0700212 * - BAD_VALUE: invalid parameter (channelMask, format, sampleRate...)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800213 * - NO_INIT: audio server or audio hardware not initialized
Glenn Kastenad2f6db2012-11-01 15:45:06 -0700214 */
Glenn Kastenfff6d712012-01-12 16:38:12 -0800215 status_t set(audio_stream_type_t streamType = AUDIO_STREAM_DEFAULT,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800216 uint32_t sampleRate = 0,
Glenn Kastene1c39622012-01-04 09:36:37 -0800217 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700218 audio_channel_mask_t channelMask = 0,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800219 int frameCount = 0,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700220 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
Glenn Kastena0d68332012-01-27 16:47:15 -0800221 callback_t cbf = NULL,
222 void* user = NULL,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800223 int notificationFrames = 0,
224 const sp<IMemory>& sharedBuffer = 0,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700225 bool threadCanCallJava = false,
Glenn Kastenea7939a2012-03-14 12:56:26 -0700226 int sessionId = 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800227
228
229 /* Result of constructing the AudioTrack. This must be checked
Glenn Kasten362c4e62011-12-14 10:28:06 -0800230 * before using any AudioTrack API (except for set()), because using
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800231 * an uninitialized AudioTrack produces undefined results.
232 * See set() method above for possible return codes.
233 */
234 status_t initCheck() const;
235
Glenn Kasten362c4e62011-12-14 10:28:06 -0800236 /* Returns this track's estimated latency in milliseconds.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800237 * This includes the latency due to AudioTrack buffer size, AudioMixer (if any)
238 * and audio hardware driver.
239 */
240 uint32_t latency() const;
241
Glenn Kasten99e53b82012-01-19 08:59:58 -0800242 /* getters, see constructors and set() */
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800243
Glenn Kastenfff6d712012-01-12 16:38:12 -0800244 audio_stream_type_t streamType() const;
Glenn Kastene1c39622012-01-04 09:36:37 -0800245 audio_format_t format() const;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800246 int channelCount() const;
247 uint32_t frameCount() const;
Glenn Kastenb9980652012-01-11 09:48:27 -0800248
249 /* Return channelCount * (bit depth per channel / 8).
250 * channelCount is determined from channelMask, and bit depth comes from format.
251 */
252 size_t frameSize() const;
253
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800254 sp<IMemory>& sharedBuffer();
255
256
257 /* After it's created the track is not active. Call start() to
258 * make it active. If set, the callback will start being called.
259 */
260 void start();
261
262 /* Stop a track. If set, the callback will cease being called and
263 * obtainBuffer returns STOPPED. Note that obtainBuffer() still works
264 * and will fill up buffers until the pool is exhausted.
265 */
266 void stop();
267 bool stopped() const;
268
Glenn Kasten362c4e62011-12-14 10:28:06 -0800269 /* Flush a stopped track. All pending buffers are discarded.
270 * This function has no effect if the track is not stopped.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800271 */
272 void flush();
273
274 /* Pause a track. If set, the callback will cease being called and
275 * obtainBuffer returns STOPPED. Note that obtainBuffer() still works
276 * and will fill up buffers until the pool is exhausted.
277 */
278 void pause();
279
Glenn Kasten362c4e62011-12-14 10:28:06 -0800280 /* Mute or unmute this track.
281 * While muted, the callback, if set, is still called.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800282 */
283 void mute(bool);
284 bool muted() const;
285
Glenn Kasten362c4e62011-12-14 10:28:06 -0800286 /* Set volume for this track, mostly used for games' sound effects
287 * left and right volumes. Levels must be >= 0.0 and <= 1.0.
Glenn Kastenb1c09932012-02-27 16:21:04 -0800288 * This is the older API. New applications should use setVolume(float) when possible.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800289 */
Eric Laurentbe916aa2010-06-01 23:49:17 -0700290 status_t setVolume(float left, float right);
Glenn Kastenb1c09932012-02-27 16:21:04 -0800291
292 /* Set volume for all channels. This is the preferred API for new applications,
293 * especially for multi-channel content.
294 */
295 status_t setVolume(float volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800296
Glenn Kasten362c4e62011-12-14 10:28:06 -0800297 /* Set the send level for this track. An auxiliary effect should be attached
298 * to the track with attachEffect(). Level must be >= 0.0 and <= 1.0.
Eric Laurentbe916aa2010-06-01 23:49:17 -0700299 */
Eric Laurent2beeb502010-07-16 07:43:46 -0700300 status_t setAuxEffectSendLevel(float level);
Glenn Kastena5224f32012-01-04 12:41:44 -0800301 void getAuxEffectSendLevel(float* level) const;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700302
Glenn Kastenad2f6db2012-11-01 15:45:06 -0700303 /* Set sample rate for this track in Hz, mostly used for games' sound effects
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800304 */
Eric Laurent57326622009-07-07 07:10:45 -0700305 status_t setSampleRate(int sampleRate);
Glenn Kastena5224f32012-01-04 12:41:44 -0800306 uint32_t getSampleRate() const;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800307
308 /* Enables looping and sets the start and end points of looping.
309 *
310 * Parameters:
311 *
312 * loopStart: loop start expressed as the number of PCM frames played since AudioTrack start.
313 * loopEnd: loop end expressed as the number of PCM frames played since AudioTrack start.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800314 * loopCount: number of loops to execute. Calling setLoop() with loopCount == 0 cancels any
315 * pending or active loop. loopCount = -1 means infinite looping.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800316 *
317 * For proper operation the following condition must be respected:
318 * (loopEnd-loopStart) <= framecount()
319 */
320 status_t setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800321
Glenn Kasten362c4e62011-12-14 10:28:06 -0800322 /* Sets marker position. When playback reaches the number of frames specified, a callback with
323 * event type EVENT_MARKER is called. Calling setMarkerPosition with marker == 0 cancels marker
324 * notification callback.
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700325 * If the AudioTrack has been opened with no callback function associated, the operation will
326 * fail.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800327 *
328 * Parameters:
329 *
330 * marker: marker position expressed in frames.
331 *
332 * Returned status (from utils/Errors.h) can be:
333 * - NO_ERROR: successful operation
334 * - INVALID_OPERATION: the AudioTrack has no callback installed.
335 */
336 status_t setMarkerPosition(uint32_t marker);
Glenn Kastena5224f32012-01-04 12:41:44 -0800337 status_t getMarkerPosition(uint32_t *marker) const;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800338
339
Glenn Kasten362c4e62011-12-14 10:28:06 -0800340 /* Sets position update period. Every time the number of frames specified has been played,
341 * a callback with event type EVENT_NEW_POS is called.
342 * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification
343 * callback.
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700344 * If the AudioTrack has been opened with no callback function associated, the operation will
345 * fail.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800346 *
347 * Parameters:
348 *
349 * updatePeriod: position update notification period expressed in frames.
350 *
351 * Returned status (from utils/Errors.h) can be:
352 * - NO_ERROR: successful operation
353 * - INVALID_OPERATION: the AudioTrack has no callback installed.
354 */
355 status_t setPositionUpdatePeriod(uint32_t updatePeriod);
Glenn Kastena5224f32012-01-04 12:41:44 -0800356 status_t getPositionUpdatePeriod(uint32_t *updatePeriod) const;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800357
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800358 /* Sets playback head position within AudioTrack buffer. The new position is specified
Glenn Kasten362c4e62011-12-14 10:28:06 -0800359 * in number of frames.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800360 * This method must be called with the AudioTrack in paused or stopped state.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800361 * Note that the actual position set is <position> modulo the AudioTrack buffer size in frames.
362 * Therefore using this method makes sense only when playing a "static" audio buffer
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800363 * as opposed to streaming.
364 * The getPosition() method on the other hand returns the total number of frames played since
365 * playback start.
366 *
367 * Parameters:
368 *
369 * position: New playback head position within AudioTrack buffer.
370 *
371 * Returned status (from utils/Errors.h) can be:
372 * - NO_ERROR: successful operation
373 * - INVALID_OPERATION: the AudioTrack is not stopped.
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700374 * - BAD_VALUE: The specified position is beyond the number of frames present in AudioTrack
375 * buffer
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800376 */
377 status_t setPosition(uint32_t position);
378 status_t getPosition(uint32_t *position);
379
Glenn Kasten362c4e62011-12-14 10:28:06 -0800380 /* Forces AudioTrack buffer full condition. When playing a static buffer, this method avoids
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800381 * rewriting the buffer before restarting playback after a stop.
382 * This method must be called with the AudioTrack in paused or stopped state.
383 *
384 * Returned status (from utils/Errors.h) can be:
385 * - NO_ERROR: successful operation
386 * - INVALID_OPERATION: the AudioTrack is not stopped.
387 */
388 status_t reload();
389
Glenn Kasten362c4e62011-12-14 10:28:06 -0800390 /* Returns a handle on the audio output used by this AudioTrack.
Eric Laurentc2f1f072009-07-17 12:17:14 -0700391 *
392 * Parameters:
393 * none.
394 *
395 * Returned value:
396 * handle on audio hardware output
397 */
398 audio_io_handle_t getOutput();
399
Glenn Kasten362c4e62011-12-14 10:28:06 -0800400 /* Returns the unique session ID associated with this track.
Eric Laurentbe916aa2010-06-01 23:49:17 -0700401 *
402 * Parameters:
403 * none.
404 *
405 * Returned value:
Glenn Kasten362c4e62011-12-14 10:28:06 -0800406 * AudioTrack session ID.
Eric Laurentbe916aa2010-06-01 23:49:17 -0700407 */
Glenn Kastena5224f32012-01-04 12:41:44 -0800408 int getSessionId() const;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700409
Glenn Kasten362c4e62011-12-14 10:28:06 -0800410 /* Attach track auxiliary output to specified effect. Use effectId = 0
Eric Laurentbe916aa2010-06-01 23:49:17 -0700411 * to detach track from effect.
412 *
413 * Parameters:
414 *
415 * effectId: effectId obtained from AudioEffect::id().
416 *
417 * Returned status (from utils/Errors.h) can be:
418 * - NO_ERROR: successful operation
419 * - INVALID_OPERATION: the effect is not an auxiliary effect.
420 * - BAD_VALUE: The specified effect ID is invalid
421 */
422 status_t attachAuxEffect(int effectId);
423
Glenn Kasten362c4e62011-12-14 10:28:06 -0800424 /* Obtains a buffer of "frameCount" frames. The buffer must be
Glenn Kasten99e53b82012-01-19 08:59:58 -0800425 * filled entirely, and then released with releaseBuffer().
426 * If the track is stopped, obtainBuffer() returns
Glenn Kasten362c4e62011-12-14 10:28:06 -0800427 * STOPPED instead of NO_ERROR as long as there are buffers available,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800428 * at which point NO_MORE_BUFFERS is returned.
Glenn Kastenad2f6db2012-11-01 15:45:06 -0700429 * Buffers will be returned until the pool
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800430 * is exhausted, at which point obtainBuffer() will either block
431 * or return WOULD_BLOCK depending on the value of the "blocking"
432 * parameter.
Glenn Kasten99e53b82012-01-19 08:59:58 -0800433 *
434 * Interpretation of waitCount:
435 * +n limits wait time to n * WAIT_PERIOD_MS,
436 * -1 causes an (almost) infinite wait time,
437 * 0 non-blocking.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800438 */
439
440 enum {
Glenn Kasten335787f2012-01-20 17:00:00 -0800441 NO_MORE_BUFFERS = 0x80000001, // same name in AudioFlinger.h, ok to be different value
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800442 STOPPED = 1
443 };
444
445 status_t obtainBuffer(Buffer* audioBuffer, int32_t waitCount);
Glenn Kasten99e53b82012-01-19 08:59:58 -0800446
447 /* Release a filled buffer of "frameCount" frames for AudioFlinger to process. */
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800448 void releaseBuffer(Buffer* audioBuffer);
449
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800450 /* As a convenience we provide a write() interface to the audio buffer.
Glenn Kasten99e53b82012-01-19 08:59:58 -0800451 * This is implemented on top of obtainBuffer/releaseBuffer. For best
452 * performance use callbacks. Returns actual number of bytes written >= 0,
453 * or one of the following negative status codes:
454 * INVALID_OPERATION AudioTrack is configured for shared buffer mode
455 * BAD_VALUE size is invalid
456 * STOPPED AudioTrack was stopped during the write
457 * NO_MORE_BUFFERS when obtainBuffer() returns same
458 * or any other error code returned by IAudioTrack::start() or restoreTrack_l().
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800459 */
460 ssize_t write(const void* buffer, size_t size);
461
462 /*
463 * Dumps the state of an audio track.
464 */
465 status_t dump(int fd, const Vector<String16>& args) const;
466
John Grossman4ff14ba2012-02-08 16:37:41 -0800467protected:
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800468 /* copying audio tracks is not allowed */
469 AudioTrack(const AudioTrack& other);
470 AudioTrack& operator = (const AudioTrack& other);
471
472 /* a small internal class to handle the callback */
473 class AudioTrackThread : public Thread
474 {
475 public:
476 AudioTrackThread(AudioTrack& receiver, bool bCanCallJava = false);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800477
478 // Do not call Thread::requestExitAndWait() without first calling requestExit().
479 // Thread::requestExitAndWait() is not virtual, and the implementation doesn't do enough.
480 virtual void requestExit();
481
482 void pause(); // suspend thread from execution at next loop boundary
483 void resume(); // allow thread to execute, if not requested to exit
484
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485 private:
486 friend class AudioTrack;
487 virtual bool threadLoop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800488 AudioTrack& mReceiver;
Glenn Kastena997e7a2012-08-07 09:44:19 -0700489 ~AudioTrackThread();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800490 Mutex mMyLock; // Thread::mLock is private
491 Condition mMyCond; // Thread::mThreadExitedCondition is private
492 bool mPaused; // whether thread is currently paused
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800493 };
494
Glenn Kasten99e53b82012-01-19 08:59:58 -0800495 // body of AudioTrackThread::threadLoop()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800496 bool processAudioBuffer(const sp<AudioTrackThread>& thread);
Glenn Kastenea7939a2012-03-14 12:56:26 -0700497
Glenn Kastenfff6d712012-01-12 16:38:12 -0800498 status_t createTrack_l(audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800499 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800500 audio_format_t format,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700501 audio_channel_mask_t channelMask,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800502 int frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700503 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800504 const sp<IMemory>& sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700505 audio_io_handle_t output);
Eric Laurent1703cdf2011-03-07 14:52:59 -0800506 void flush_l();
507 status_t setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount);
508 audio_io_handle_t getOutput_l();
509 status_t restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800510 bool stopped_l() const { return !mActive; }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800511
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800512 sp<IAudioTrack> mAudioTrack;
513 sp<IMemory> mCblkMemory;
514 sp<AudioTrackThread> mAudioTrackThread;
515
516 float mVolume[2];
Eric Laurentbe916aa2010-06-01 23:49:17 -0700517 float mSendLevel;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800518 uint32_t mFrameCount;
519
520 audio_track_cblk_t* mCblk;
Glenn Kastene1c39622012-01-04 09:36:37 -0800521 audio_format_t mFormat;
Glenn Kastenfff6d712012-01-12 16:38:12 -0800522 audio_stream_type_t mStreamType;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800523 uint8_t mChannelCount;
524 uint8_t mMuted;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700525 uint8_t mReserved;
Glenn Kasten28b76b32012-07-03 17:24:41 -0700526 audio_channel_mask_t mChannelMask;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800527 status_t mStatus;
528 uint32_t mLatency;
529
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800530 bool mActive; // protected by mLock
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800531
Glenn Kasten99e53b82012-01-19 08:59:58 -0800532 callback_t mCbf; // callback handler for events, or NULL
Glenn Kastenad2f6db2012-11-01 15:45:06 -0700533 void* mUserData; // for client callback handler
534
535 // for notification APIs
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700536 uint32_t mNotificationFramesReq; // requested number of frames between each
537 // notification callback
538 uint32_t mNotificationFramesAct; // actual number of frames between each
539 // notification callback
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800540 sp<IMemory> mSharedBuffer;
541 int mLoopCount;
542 uint32_t mRemainingFrames;
Glenn Kastenad2f6db2012-11-01 15:45:06 -0700543 uint32_t mMarkerPosition; // in frames
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700544 bool mMarkerReached;
Glenn Kastenad2f6db2012-11-01 15:45:06 -0700545 uint32_t mNewPosition; // in frames
546 uint32_t mUpdatePeriod; // in frames
547
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700548 bool mFlushed; // FIXME will be made obsolete by making flush() synchronous
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700549 audio_output_flags_t mFlags;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700550 int mSessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700551 int mAuxEffectId;
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800552 mutable Mutex mLock;
Eric Laurentcfe2ba62011-09-13 15:04:17 -0700553 status_t mRestoreStatus;
John Grossman4ff14ba2012-02-08 16:37:41 -0800554 bool mIsTimed;
Glenn Kasten87913512011-06-22 16:15:25 -0700555 int mPreviousPriority; // before start()
Glenn Kastena6364332012-04-19 09:35:04 -0700556 SchedPolicy mPreviousSchedulingGroup;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800557};
558
John Grossman4ff14ba2012-02-08 16:37:41 -0800559class TimedAudioTrack : public AudioTrack
560{
561public:
562 TimedAudioTrack();
563
564 /* allocate a shared memory buffer that can be passed to queueTimedBuffer */
565 status_t allocateTimedBuffer(size_t size, sp<IMemory>* buffer);
566
567 /* queue a buffer obtained via allocateTimedBuffer for playback at the
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700568 given timestamp. PTS units are microseconds on the media time timeline.
John Grossman4ff14ba2012-02-08 16:37:41 -0800569 The media time transform (set with setMediaTimeTransform) set by the
570 audio producer will handle converting from media time to local time
571 (perhaps going through the common time timeline in the case of
572 synchronized multiroom audio case) */
573 status_t queueTimedBuffer(const sp<IMemory>& buffer, int64_t pts);
574
575 /* define a transform between media time and either common time or
576 local time */
577 enum TargetTimeline {LOCAL_TIME, COMMON_TIME};
578 status_t setMediaTimeTransform(const LinearTransform& xform,
579 TargetTimeline target);
580};
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800581
582}; // namespace android
583
584#endif // ANDROID_AUDIOTRACK_H