blob: 6f85527369d65eeef4756762d5f97142739b8313 [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 Kastenc5b0fa32012-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 {
Glenn Kasten8f7453f2012-11-30 15:00:36 -080055 EVENT_MORE_DATA = 0, // Request to write more data to buffer.
56 // If this event is delivered but the callback handler
57 // does not want to write more data, the handler must explicitly
58 // ignore the event by setting frameCount to zero.
59 EVENT_UNDERRUN = 1, // Buffer underrun occurred.
Glenn Kasten8af901c2012-11-01 11:11:38 -070060 EVENT_LOOP_END = 2, // Sample loop end was reached; playback restarted from
61 // loop start if loop count was not 0.
62 EVENT_MARKER = 3, // Playback head is at the specified marker position
63 // (See setMarkerPosition()).
64 EVENT_NEW_POS = 4, // Playback head is at a new position
65 // (See setPositionUpdatePeriod()).
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080066 EVENT_BUFFER_END = 5 // Playback head is at the end of the buffer.
67 };
68
Glenn Kasten99e53b82012-01-19 08:59:58 -080069 /* Client should declare Buffer on the stack and pass address to obtainBuffer()
70 * and releaseBuffer(). See also callback_t for EVENT_MORE_DATA.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080071 */
72
73 class Buffer
74 {
75 public:
Glenn Kasten99e53b82012-01-19 08:59:58 -080076 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
Glenn Kasten7da35f22012-11-14 12:54:39 -0800116 static status_t getMinFrameCount(size_t* 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
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800121 * AudioFlinger takes place. Use set() after this.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800122 */
123 AudioTrack();
124
Glenn Kastenc5b0fa32012-11-01 15:45:06 -0700125 /* Creates an AudioTrack object and registers it with AudioFlinger.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800126 * Once created, the track needs to be started before it can be used.
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800127 * Unspecified values are set to appropriate default values.
128 * With this constructor, the track is configured for streaming mode.
129 * Data to be rendered is supplied by write() or by the callback EVENT_MORE_DATA.
130 * Intermixing a combination of write() and non-ignored EVENT_MORE_DATA is deprecated.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800131 *
132 * Parameters:
133 *
134 * streamType: Select the type of audio stream this track is attached to
Dima Zavinfce7a472011-04-19 22:30:36 -0700135 * (e.g. AUDIO_STREAM_MUSIC).
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800136 * sampleRate: Track sampling rate in Hz.
Dima Zavinfce7a472011-04-19 22:30:36 -0700137 * format: Audio format (e.g AUDIO_FORMAT_PCM_16_BIT for signed
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800138 * 16 bits per sample).
Glenn Kasten28b76b32012-07-03 17:24:41 -0700139 * channelMask: Channel mask.
Eric Laurentd8d61852012-03-05 17:06:40 -0800140 * frameCount: Minimum size of track PCM buffer in frames. This defines the
Glenn Kastenc5b0fa32012-11-01 15:45:06 -0700141 * application's contribution to the
Eric Laurentd8d61852012-03-05 17:06:40 -0800142 * latency of the track. The actual size selected by the AudioTrack could be
143 * larger if the requested size is not compatible with current audio HAL
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800144 * configuration. Zero means to use a default value.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700145 * flags: See comments on audio_output_flags_t in <system/audio.h>.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800146 * cbf: Callback function. If not null, this function is called periodically
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800147 * to provide new data and inform of marker, position updates, etc.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800148 * user: Context for use by the callback receiver.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800149 * notificationFrames: The callback function is called each time notificationFrames PCM
Glenn Kasten362c4e62011-12-14 10:28:06 -0800150 * frames have been consumed from track input buffer.
151 * sessionId: Specific session ID, or zero to use default.
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700152 * threadCanCallJava: Whether callbacks are made from an attached thread and thus can call JNI.
153 * If not present in parameter list, then fixed at false.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800154 */
155
Glenn Kastenfff6d712012-01-12 16:38:12 -0800156 AudioTrack( audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800157 uint32_t sampleRate = 0,
Glenn Kastene1c39622012-01-04 09:36:37 -0800158 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700159 audio_channel_mask_t channelMask = 0,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800160 int frameCount = 0,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700161 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
Glenn Kastena0d68332012-01-27 16:47:15 -0800162 callback_t cbf = NULL,
163 void* user = NULL,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700164 int notificationFrames = 0,
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700165 int sessionId = 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800166
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800167 /* Creates an audio track and registers it with AudioFlinger.
168 * With this constructor, the track is configured for static buffer mode.
169 * The format must not be 8-bit linear PCM.
170 * Data to be rendered is passed in a shared memory buffer
171 * identified by the argument sharedBuffer, which must be non-0.
172 * The memory should be initialized to the desired data before calling start().
Glenn Kastenec7dcac2012-11-30 13:41:12 -0800173 * The write() method is not supported in this case.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800174 * 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 -0800175 * EVENT_UNDERRUN event.
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800176 * FIXME EVENT_MORE_DATA still occurs; it must be ignored.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800177 */
178
Glenn Kastenfff6d712012-01-12 16:38:12 -0800179 AudioTrack( audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800180 uint32_t sampleRate = 0,
Glenn Kastene1c39622012-01-04 09:36:37 -0800181 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700182 audio_channel_mask_t channelMask = 0,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800183 const sp<IMemory>& sharedBuffer = 0,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700184 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
Glenn Kastena0d68332012-01-27 16:47:15 -0800185 callback_t cbf = NULL,
186 void* user = NULL,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700187 int notificationFrames = 0,
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700188 int sessionId = 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800189
190 /* Terminates the AudioTrack and unregisters it from AudioFlinger.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800191 * Also destroys all resources associated with the AudioTrack.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800192 */
193 ~AudioTrack();
194
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800195 /* Initialize an uninitialized AudioTrack.
196 * Returned status (from utils/Errors.h) can be:
Glenn Kasten362c4e62011-12-14 10:28:06 -0800197 * - NO_ERROR: successful initialization
198 * - INVALID_OPERATION: AudioTrack is already initialized
Glenn Kasten28b76b32012-07-03 17:24:41 -0700199 * - BAD_VALUE: invalid parameter (channelMask, format, sampleRate...)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800200 * - NO_INIT: audio server or audio hardware not initialized
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800201 * If sharedBuffer is non-0, the frameCount parameter is ignored and
202 * replaced by the shared buffer's total allocated size in frame units.
Glenn Kastenc5b0fa32012-11-01 15:45:06 -0700203 */
Glenn Kastenfff6d712012-01-12 16:38:12 -0800204 status_t set(audio_stream_type_t streamType = AUDIO_STREAM_DEFAULT,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800205 uint32_t sampleRate = 0,
Glenn Kastene1c39622012-01-04 09:36:37 -0800206 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700207 audio_channel_mask_t channelMask = 0,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800208 int frameCount = 0,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700209 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
Glenn Kastena0d68332012-01-27 16:47:15 -0800210 callback_t cbf = NULL,
211 void* user = NULL,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800212 int notificationFrames = 0,
213 const sp<IMemory>& sharedBuffer = 0,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700214 bool threadCanCallJava = false,
Glenn Kastenea7939a2012-03-14 12:56:26 -0700215 int sessionId = 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800216
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800217 /* Result of constructing the AudioTrack. This must be checked
Glenn Kasten362c4e62011-12-14 10:28:06 -0800218 * before using any AudioTrack API (except for set()), because using
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800219 * an uninitialized AudioTrack produces undefined results.
220 * See set() method above for possible return codes.
221 */
Glenn Kastenab5bfb12012-11-29 07:32:49 -0800222 status_t initCheck() const { return mStatus; }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800223
Glenn Kasten362c4e62011-12-14 10:28:06 -0800224 /* Returns this track's estimated latency in milliseconds.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800225 * This includes the latency due to AudioTrack buffer size, AudioMixer (if any)
226 * and audio hardware driver.
227 */
Glenn Kastenab5bfb12012-11-29 07:32:49 -0800228 uint32_t latency() const { return mLatency; }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800229
Glenn Kasten99e53b82012-01-19 08:59:58 -0800230 /* getters, see constructors and set() */
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800231
Glenn Kastenab5bfb12012-11-29 07:32:49 -0800232 audio_stream_type_t streamType() const { return mStreamType; }
233 audio_format_t format() const { return mFormat; }
Glenn Kastenb9980652012-01-11 09:48:27 -0800234
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800235 /* Return frame size in bytes, which for linear PCM is channelCount * (bit depth per channel / 8).
Glenn Kastenb9980652012-01-11 09:48:27 -0800236 * channelCount is determined from channelMask, and bit depth comes from format.
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800237 * For non-linear formats, the frame size is typically 1 byte.
Glenn Kastenb9980652012-01-11 09:48:27 -0800238 */
Glenn Kastenab5bfb12012-11-29 07:32:49 -0800239 uint32_t channelCount() const { return mChannelCount; }
Glenn Kastenb9980652012-01-11 09:48:27 -0800240
Glenn Kastenab5bfb12012-11-29 07:32:49 -0800241 uint32_t frameCount() const { return mFrameCount; }
242 size_t frameSize() const { return mFrameSize; }
243
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800244 /* Return the static buffer specified in constructor or set(), or 0 for streaming mode */
Glenn Kastenab5bfb12012-11-29 07:32:49 -0800245 sp<IMemory> sharedBuffer() const { return mSharedBuffer; }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800246
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800247 /* After it's created the track is not active. Call start() to
248 * make it active. If set, the callback will start being called.
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800249 * If the track was previously paused, volume is ramped up over the first mix buffer.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800250 */
251 void start();
252
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800253 /* Stop a track.
254 * In static buffer mode, the track is stopped immediately.
255 * In streaming mode, the callback will cease being called and
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800256 * obtainBuffer returns STOPPED. Note that obtainBuffer() still works
257 * and will fill up buffers until the pool is exhausted.
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800258 * The stop does not occur immediately: any data remaining in the buffer
259 * is first drained, mixed, and output, and only then is the track marked as stopped.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800260 */
261 void stop();
262 bool stopped() const;
263
Glenn Kastenec7dcac2012-11-30 13:41:12 -0800264 /* Flush a stopped or paused track. All previously buffered data is discarded immediately.
265 * This has the effect of draining the buffers without mixing or output.
266 * Flush is intended for streaming mode, for example before switching to non-contiguous content.
267 * This function is a no-op if the track is not stopped or paused, or uses a static buffer.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800268 */
269 void flush();
270
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800271 /* Pause a track. After pause, the callback will cease being called and
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800272 * obtainBuffer returns STOPPED. Note that obtainBuffer() still works
273 * and will fill up buffers until the pool is exhausted.
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800274 * Volume is ramped down over the next mix buffer following the pause request,
275 * and then the track is marked as paused. It can be resumed with ramp up by start().
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800276 */
277 void pause();
278
Glenn Kasten362c4e62011-12-14 10:28:06 -0800279 /* Set volume for this track, mostly used for games' sound effects
280 * left and right volumes. Levels must be >= 0.0 and <= 1.0.
Glenn Kasten164d6532012-02-27 16:21:04 -0800281 * This is the older API. New applications should use setVolume(float) when possible.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800282 */
Eric Laurentbe916aa2010-06-01 23:49:17 -0700283 status_t setVolume(float left, float right);
Glenn Kasten164d6532012-02-27 16:21:04 -0800284
285 /* Set volume for all channels. This is the preferred API for new applications,
286 * especially for multi-channel content.
287 */
288 status_t setVolume(float volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800289
Glenn Kasten362c4e62011-12-14 10:28:06 -0800290 /* Set the send level for this track. An auxiliary effect should be attached
291 * to the track with attachEffect(). Level must be >= 0.0 and <= 1.0.
Eric Laurentbe916aa2010-06-01 23:49:17 -0700292 */
Eric Laurent2beeb502010-07-16 07:43:46 -0700293 status_t setAuxEffectSendLevel(float level);
Glenn Kastena5224f32012-01-04 12:41:44 -0800294 void getAuxEffectSendLevel(float* level) const;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700295
Glenn Kastenc5b0fa32012-11-01 15:45:06 -0700296 /* Set sample rate for this track in Hz, mostly used for games' sound effects
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800297 */
Glenn Kasten1127d652012-11-14 08:44:39 -0800298 status_t setSampleRate(uint32_t sampleRate);
299
300 /* Return current sample rate in Hz, or 0 if unknown */
Glenn Kastena5224f32012-01-04 12:41:44 -0800301 uint32_t getSampleRate() const;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800302
303 /* Enables looping and sets the start and end points of looping.
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800304 * Only supported for static buffer mode.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800305 *
306 * Parameters:
307 *
308 * loopStart: loop start expressed as the number of PCM frames played since AudioTrack start.
309 * loopEnd: loop end expressed as the number of PCM frames played since AudioTrack start.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800310 * loopCount: number of loops to execute. Calling setLoop() with loopCount == 0 cancels any
311 * pending or active loop. loopCount = -1 means infinite looping.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800312 *
313 * For proper operation the following condition must be respected:
314 * (loopEnd-loopStart) <= framecount()
315 */
316 status_t setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800317
Glenn Kasten362c4e62011-12-14 10:28:06 -0800318 /* Sets marker position. When playback reaches the number of frames specified, a callback with
319 * event type EVENT_MARKER is called. Calling setMarkerPosition with marker == 0 cancels marker
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800320 * notification callback. To set a marker at a position which would compute as 0,
321 * a workaround is to the set the marker at a nearby position such as -1 or 1.
Glenn Kasten8af901c2012-11-01 11:11:38 -0700322 * If the AudioTrack has been opened with no callback function associated, the operation will
323 * fail.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800324 *
325 * Parameters:
326 *
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800327 * marker: marker position expressed in wrapping (overflow) frame units,
328 * like the return value of getPosition().
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800329 *
330 * Returned status (from utils/Errors.h) can be:
331 * - NO_ERROR: successful operation
332 * - INVALID_OPERATION: the AudioTrack has no callback installed.
333 */
334 status_t setMarkerPosition(uint32_t marker);
Glenn Kastena5224f32012-01-04 12:41:44 -0800335 status_t getMarkerPosition(uint32_t *marker) const;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800336
Glenn Kasten362c4e62011-12-14 10:28:06 -0800337 /* Sets position update period. Every time the number of frames specified has been played,
338 * a callback with event type EVENT_NEW_POS is called.
339 * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification
340 * callback.
Glenn Kasten8af901c2012-11-01 11:11:38 -0700341 * If the AudioTrack has been opened with no callback function associated, the operation will
342 * fail.
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800343 * Extremely small values may be rounded up to a value the implementation can support.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800344 *
345 * Parameters:
346 *
347 * updatePeriod: position update notification period expressed in frames.
348 *
349 * Returned status (from utils/Errors.h) can be:
350 * - NO_ERROR: successful operation
351 * - INVALID_OPERATION: the AudioTrack has no callback installed.
352 */
353 status_t setPositionUpdatePeriod(uint32_t updatePeriod);
Glenn Kastena5224f32012-01-04 12:41:44 -0800354 status_t getPositionUpdatePeriod(uint32_t *updatePeriod) const;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800355
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800356 /* Sets playback head position within AudioTrack buffer. The new position is specified
Glenn Kasten362c4e62011-12-14 10:28:06 -0800357 * in number of frames.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800358 * This method must be called with the AudioTrack in paused or stopped state.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800359 * Note that the actual position set is <position> modulo the AudioTrack buffer size in frames.
360 * Therefore using this method makes sense only when playing a "static" audio buffer
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800361 * as opposed to streaming.
362 * The getPosition() method on the other hand returns the total number of frames played since
363 * playback start.
364 *
365 * Parameters:
366 *
367 * position: New playback head position within AudioTrack buffer.
368 *
369 * Returned status (from utils/Errors.h) can be:
370 * - NO_ERROR: successful operation
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800371 * - INVALID_OPERATION: the AudioTrack is not stopped or paused, or is streaming mode.
Glenn Kasten8af901c2012-11-01 11:11:38 -0700372 * - BAD_VALUE: The specified position is beyond the number of frames present in AudioTrack
373 * buffer
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800374 */
375 status_t setPosition(uint32_t position);
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800376
377 /* Return the total number of frames played since playback start.
378 * The counter will wrap (overflow) periodically, e.g. every ~27 hours at 44.1 kHz.
379 * It is reset to zero by flush(), reload(), and stop().
380 */
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800381 status_t getPosition(uint32_t *position);
382
Glenn Kasten362c4e62011-12-14 10:28:06 -0800383 /* Forces AudioTrack buffer full condition. When playing a static buffer, this method avoids
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800384 * rewriting the buffer before restarting playback after a stop.
385 * This method must be called with the AudioTrack in paused or stopped state.
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800386 * Not allowed in streaming mode.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800387 *
388 * Returned status (from utils/Errors.h) can be:
389 * - NO_ERROR: successful operation
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800390 * - INVALID_OPERATION: the AudioTrack is not stopped or paused, or is streaming mode.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800391 */
392 status_t reload();
393
Glenn Kasten362c4e62011-12-14 10:28:06 -0800394 /* Returns a handle on the audio output used by this AudioTrack.
Eric Laurentc2f1f072009-07-17 12:17:14 -0700395 *
396 * Parameters:
397 * none.
398 *
399 * Returned value:
400 * handle on audio hardware output
401 */
402 audio_io_handle_t getOutput();
403
Glenn Kasten362c4e62011-12-14 10:28:06 -0800404 /* Returns the unique session ID associated with this track.
Eric Laurentbe916aa2010-06-01 23:49:17 -0700405 *
406 * Parameters:
407 * none.
408 *
409 * Returned value:
Glenn Kasten362c4e62011-12-14 10:28:06 -0800410 * AudioTrack session ID.
Eric Laurentbe916aa2010-06-01 23:49:17 -0700411 */
Glenn Kastenab5bfb12012-11-29 07:32:49 -0800412 int getSessionId() const { return mSessionId; }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700413
Glenn Kasten362c4e62011-12-14 10:28:06 -0800414 /* Attach track auxiliary output to specified effect. Use effectId = 0
Eric Laurentbe916aa2010-06-01 23:49:17 -0700415 * to detach track from effect.
416 *
417 * Parameters:
418 *
419 * effectId: effectId obtained from AudioEffect::id().
420 *
421 * Returned status (from utils/Errors.h) can be:
422 * - NO_ERROR: successful operation
423 * - INVALID_OPERATION: the effect is not an auxiliary effect.
424 * - BAD_VALUE: The specified effect ID is invalid
425 */
426 status_t attachAuxEffect(int effectId);
427
Glenn Kasten362c4e62011-12-14 10:28:06 -0800428 /* Obtains a buffer of "frameCount" frames. The buffer must be
Glenn Kasten99e53b82012-01-19 08:59:58 -0800429 * filled entirely, and then released with releaseBuffer().
430 * If the track is stopped, obtainBuffer() returns
Glenn Kasten362c4e62011-12-14 10:28:06 -0800431 * STOPPED instead of NO_ERROR as long as there are buffers available,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800432 * at which point NO_MORE_BUFFERS is returned.
Glenn Kastenc5b0fa32012-11-01 15:45:06 -0700433 * Buffers will be returned until the pool
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800434 * is exhausted, at which point obtainBuffer() will either block
435 * or return WOULD_BLOCK depending on the value of the "blocking"
436 * parameter.
Glenn Kasten99e53b82012-01-19 08:59:58 -0800437 *
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800438 * obtainBuffer() and releaseBuffer() are deprecated for direct use by applications,
439 * which should use write() or callback EVENT_MORE_DATA instead.
440 *
Glenn Kasten99e53b82012-01-19 08:59:58 -0800441 * Interpretation of waitCount:
442 * +n limits wait time to n * WAIT_PERIOD_MS,
443 * -1 causes an (almost) infinite wait time,
444 * 0 non-blocking.
Glenn Kasten84243612012-11-06 14:25:20 -0800445 *
446 * Buffer fields
447 * On entry:
448 * frameCount number of frames requested
449 * After error return:
450 * frameCount 0
451 * size 0
Glenn Kasten020f79f2012-11-07 14:03:00 -0800452 * raw undefined
Glenn Kasten84243612012-11-06 14:25:20 -0800453 * After successful return:
454 * frameCount actual number of frames available, <= number requested
455 * size actual number of bytes available
456 * raw pointer to the buffer
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800457 */
458
459 enum {
Glenn Kasten335787f2012-01-20 17:00:00 -0800460 NO_MORE_BUFFERS = 0x80000001, // same name in AudioFlinger.h, ok to be different value
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800461 STOPPED = 1
462 };
463
464 status_t obtainBuffer(Buffer* audioBuffer, int32_t waitCount);
Glenn Kasten99e53b82012-01-19 08:59:58 -0800465
466 /* Release a filled buffer of "frameCount" frames for AudioFlinger to process. */
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800467 void releaseBuffer(Buffer* audioBuffer);
468
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800469 /* As a convenience we provide a write() interface to the audio buffer.
Glenn Kasten99e53b82012-01-19 08:59:58 -0800470 * This is implemented on top of obtainBuffer/releaseBuffer. For best
471 * performance use callbacks. Returns actual number of bytes written >= 0,
472 * or one of the following negative status codes:
473 * INVALID_OPERATION AudioTrack is configured for shared buffer mode
474 * BAD_VALUE size is invalid
475 * STOPPED AudioTrack was stopped during the write
476 * NO_MORE_BUFFERS when obtainBuffer() returns same
477 * or any other error code returned by IAudioTrack::start() or restoreTrack_l().
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800478 * Not supported for static buffer mode.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800479 */
480 ssize_t write(const void* buffer, size_t size);
481
482 /*
483 * Dumps the state of an audio track.
484 */
485 status_t dump(int fd, const Vector<String16>& args) const;
486
John Grossman4ff14ba2012-02-08 16:37:41 -0800487protected:
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800488 /* copying audio tracks is not allowed */
489 AudioTrack(const AudioTrack& other);
490 AudioTrack& operator = (const AudioTrack& other);
491
492 /* a small internal class to handle the callback */
493 class AudioTrackThread : public Thread
494 {
495 public:
496 AudioTrackThread(AudioTrack& receiver, bool bCanCallJava = false);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800497
498 // Do not call Thread::requestExitAndWait() without first calling requestExit().
499 // Thread::requestExitAndWait() is not virtual, and the implementation doesn't do enough.
500 virtual void requestExit();
501
502 void pause(); // suspend thread from execution at next loop boundary
503 void resume(); // allow thread to execute, if not requested to exit
504
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800505 private:
506 friend class AudioTrack;
507 virtual bool threadLoop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800508 AudioTrack& mReceiver;
Glenn Kastena997e7a2012-08-07 09:44:19 -0700509 ~AudioTrackThread();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800510 Mutex mMyLock; // Thread::mLock is private
511 Condition mMyCond; // Thread::mThreadExitedCondition is private
512 bool mPaused; // whether thread is currently paused
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800513 };
514
Glenn Kasten99e53b82012-01-19 08:59:58 -0800515 // body of AudioTrackThread::threadLoop()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800516 bool processAudioBuffer(const sp<AudioTrackThread>& thread);
Glenn Kastenea7939a2012-03-14 12:56:26 -0700517
Glenn Kastena96bd952012-11-02 13:05:14 -0700518 // caller must hold lock on mLock for all _l methods
Glenn Kastenfff6d712012-01-12 16:38:12 -0800519 status_t createTrack_l(audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800520 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800521 audio_format_t format,
Glenn Kasten7da35f22012-11-14 12:54:39 -0800522 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700523 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800524 const sp<IMemory>& sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700525 audio_io_handle_t output);
Glenn Kastenec7dcac2012-11-30 13:41:12 -0800526
527 // can only be called when !mActive
Eric Laurent1703cdf2011-03-07 14:52:59 -0800528 void flush_l();
Glenn Kastenec7dcac2012-11-30 13:41:12 -0800529
Eric Laurent1703cdf2011-03-07 14:52:59 -0800530 status_t setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount);
531 audio_io_handle_t getOutput_l();
532 status_t restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800533 bool stopped_l() const { return !mActive; }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800534
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800535 sp<IAudioTrack> mAudioTrack;
536 sp<IMemory> mCblkMemory;
537 sp<AudioTrackThread> mAudioTrackThread;
538
539 float mVolume[2];
Eric Laurentbe916aa2010-06-01 23:49:17 -0700540 float mSendLevel;
Glenn Kastend7101432012-11-14 13:42:25 -0800541 size_t mFrameCount; // corresponds to current IAudioTrack
542 size_t mReqFrameCount; // frame count to request the next time a new
543 // IAudioTrack is needed
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800544
Glenn Kastene0461d12012-11-07 10:13:08 -0800545 audio_track_cblk_t* mCblk; // re-load after mLock.unlock()
Glenn Kasten020f79f2012-11-07 14:03:00 -0800546
547 // Starting address of buffers in shared memory. If there is a shared buffer, mBuffers
548 // is the value of pointer() for the shared buffer, otherwise mBuffers points
549 // immediately after the control block. This address is for the mapping within client
550 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
551 void* mBuffers;
552
Glenn Kasten520a9af2012-06-21 12:56:37 -0700553 audio_format_t mFormat; // as requested by client, not forced to 16-bit
Glenn Kastenfff6d712012-01-12 16:38:12 -0800554 audio_stream_type_t mStreamType;
Glenn Kasten4b3a49e2012-11-29 13:38:14 -0800555 uint32_t mChannelCount;
Glenn Kasten28b76b32012-07-03 17:24:41 -0700556 audio_channel_mask_t mChannelMask;
Glenn Kasten5aab59a2012-11-12 07:58:20 -0800557
558 // mFrameSize is equal to mFrameSizeAF for non-PCM or 16-bit PCM data.
559 // For 8-bit PCM data, mFrameSizeAF is
560 // twice as large because data is expanded to 16-bit before being stored in buffer.
561 size_t mFrameSize; // app-level frame size
562 size_t mFrameSizeAF; // AudioFlinger frame size
563
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800564 status_t mStatus;
565 uint32_t mLatency;
566
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800567 bool mActive; // protected by mLock
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800568
Glenn Kasten99e53b82012-01-19 08:59:58 -0800569 callback_t mCbf; // callback handler for events, or NULL
Glenn Kastenc5b0fa32012-11-01 15:45:06 -0700570 void* mUserData; // for client callback handler
571
572 // for notification APIs
Glenn Kasten8af901c2012-11-01 11:11:38 -0700573 uint32_t mNotificationFramesReq; // requested number of frames between each
574 // notification callback
575 uint32_t mNotificationFramesAct; // actual number of frames between each
576 // notification callback
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800577 sp<IMemory> mSharedBuffer;
578 int mLoopCount;
579 uint32_t mRemainingFrames;
Glenn Kasten8f7453f2012-11-30 15:00:36 -0800580 uint32_t mMarkerPosition; // in wrapping (overflow) frame units
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700581 bool mMarkerReached;
Glenn Kastenc5b0fa32012-11-01 15:45:06 -0700582 uint32_t mNewPosition; // in frames
583 uint32_t mUpdatePeriod; // in frames
584
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700585 bool mFlushed; // FIXME will be made obsolete by making flush() synchronous
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700586 audio_output_flags_t mFlags;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700587 int mSessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700588 int mAuxEffectId;
Glenn Kastena96bd952012-11-02 13:05:14 -0700589
590 // When locking both mLock and mCblk->lock, must lock in this order to avoid deadlock:
591 // 1. mLock
592 // 2. mCblk->lock
593 // It is OK to lock only mCblk->lock.
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800594 mutable Mutex mLock;
Glenn Kastena96bd952012-11-02 13:05:14 -0700595
John Grossman4ff14ba2012-02-08 16:37:41 -0800596 bool mIsTimed;
Glenn Kasten87913512011-06-22 16:15:25 -0700597 int mPreviousPriority; // before start()
Glenn Kastena6364332012-04-19 09:35:04 -0700598 SchedPolicy mPreviousSchedulingGroup;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800599};
600
John Grossman4ff14ba2012-02-08 16:37:41 -0800601class TimedAudioTrack : public AudioTrack
602{
603public:
604 TimedAudioTrack();
605
606 /* allocate a shared memory buffer that can be passed to queueTimedBuffer */
607 status_t allocateTimedBuffer(size_t size, sp<IMemory>* buffer);
608
609 /* queue a buffer obtained via allocateTimedBuffer for playback at the
Glenn Kasten2662ac92012-07-30 10:59:30 -0700610 given timestamp. PTS units are microseconds on the media time timeline.
John Grossman4ff14ba2012-02-08 16:37:41 -0800611 The media time transform (set with setMediaTimeTransform) set by the
612 audio producer will handle converting from media time to local time
613 (perhaps going through the common time timeline in the case of
614 synchronized multiroom audio case) */
615 status_t queueTimedBuffer(const sp<IMemory>& buffer, int64_t pts);
616
617 /* define a transform between media time and either common time or
618 local time */
619 enum TargetTimeline {LOCAL_TIME, COMMON_TIME};
620 status_t setMediaTimeTransform(const LinearTransform& xform,
621 TargetTimeline target);
622};
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800623
624}; // namespace android
625
626#endif // ANDROID_AUDIOTRACK_H