blob: 8dbc9eeaea6e34cedcc926577b52ca0294ae8aec [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;
Glenn Kastene3aa6592012-12-04 12:22:46 -080039class AudioTrackClientProxy;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080040
41// ----------------------------------------------------------------------------
42
Glenn Kastenb68a91a2011-11-15 13:55:13 -080043class AudioTrack : virtual public RefBase
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080044{
45public:
46 enum channel_index {
47 MONO = 0,
48 LEFT = 0,
49 RIGHT = 1
50 };
51
52 /* Events used by AudioTrack callback function (audio_track_cblk_t).
Glenn Kastenad2f6db2012-11-01 15:45:06 -070053 * Keep in sync with frameworks/base/media/java/android/media/AudioTrack.java NATIVE_EVENT_*.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080054 */
55 enum event_type {
Glenn Kasten083d1c12012-11-30 15:00:36 -080056 EVENT_MORE_DATA = 0, // Request to write more data to buffer.
57 // If this event is delivered but the callback handler
58 // does not want to write more data, the handler must explicitly
59 // ignore the event by setting frameCount to zero.
60 EVENT_UNDERRUN = 1, // Buffer underrun occurred.
Glenn Kasten85ab62c2012-11-01 11:11:38 -070061 EVENT_LOOP_END = 2, // Sample loop end was reached; playback restarted from
62 // loop start if loop count was not 0.
63 EVENT_MARKER = 3, // Playback head is at the specified marker position
64 // (See setMarkerPosition()).
65 EVENT_NEW_POS = 4, // Playback head is at a new position
66 // (See setPositionUpdatePeriod()).
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080067 EVENT_BUFFER_END = 5 // Playback head is at the end of the buffer.
68 };
69
Glenn Kasten99e53b82012-01-19 08:59:58 -080070 /* Client should declare Buffer on the stack and pass address to obtainBuffer()
71 * and releaseBuffer(). See also callback_t for EVENT_MORE_DATA.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080072 */
73
74 class Buffer
75 {
76 public:
Glenn Kasten99e53b82012-01-19 08:59:58 -080077 size_t frameCount; // number of sample frames corresponding to size;
78 // on input it is the number of frames desired,
79 // on output is the number of frames actually filled
80
81 size_t size; // input/output in byte units
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080082 union {
83 void* raw;
Glenn Kasten362c4e62011-12-14 10:28:06 -080084 short* i16; // signed 16-bit
85 int8_t* i8; // unsigned 8-bit, offset by 0x80
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080086 };
87 };
88
89
90 /* As a convenience, if a callback is supplied, a handler thread
91 * is automatically created with the appropriate priority. This thread
Glenn Kasten99e53b82012-01-19 08:59:58 -080092 * invokes the callback when a new buffer becomes available or various conditions occur.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080093 * Parameters:
94 *
95 * event: type of event notified (see enum AudioTrack::event_type).
96 * user: Pointer to context for use by the callback receiver.
97 * info: Pointer to optional parameter according to event type:
98 * - EVENT_MORE_DATA: pointer to AudioTrack::Buffer struct. The callback must not write
Glenn Kasten99e53b82012-01-19 08:59:58 -080099 * more bytes than indicated by 'size' field and update 'size' if fewer bytes are
100 * written.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800101 * - EVENT_UNDERRUN: unused.
102 * - EVENT_LOOP_END: pointer to an int indicating the number of loops remaining.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800103 * - EVENT_MARKER: pointer to an uint32_t containing the marker position in frames.
104 * - EVENT_NEW_POS: pointer to an uint32_t containing the new position in frames.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800105 * - EVENT_BUFFER_END: unused.
106 */
107
Glenn Kastend217a8c2011-06-01 15:20:35 -0700108 typedef void (*callback_t)(int event, void* user, void *info);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800109
Chia-chi Yeh33005a92010-06-16 06:33:13 +0800110 /* Returns the minimum frame count required for the successful creation of
111 * an AudioTrack object.
112 * Returned status (from utils/Errors.h) can be:
113 * - NO_ERROR: successful operation
114 * - NO_INIT: audio server or audio hardware not initialized
115 */
116
Glenn Kastene33054e2012-11-14 12:54:39 -0800117 static status_t getMinFrameCount(size_t* frameCount,
Glenn Kastenfff6d712012-01-12 16:38:12 -0800118 audio_stream_type_t streamType = AUDIO_STREAM_DEFAULT,
Chia-chi Yeh33005a92010-06-16 06:33:13 +0800119 uint32_t sampleRate = 0);
120
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800121 /* Constructs an uninitialized AudioTrack. No connection with
Glenn Kasten083d1c12012-11-30 15:00:36 -0800122 * AudioFlinger takes place. Use set() after this.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800123 */
124 AudioTrack();
125
Glenn Kastenad2f6db2012-11-01 15:45:06 -0700126 /* Creates an AudioTrack object and registers it with AudioFlinger.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800127 * Once created, the track needs to be started before it can be used.
Glenn Kasten083d1c12012-11-30 15:00:36 -0800128 * Unspecified values are set to appropriate default values.
129 * With this constructor, the track is configured for streaming mode.
130 * Data to be rendered is supplied by write() or by the callback EVENT_MORE_DATA.
131 * Intermixing a combination of write() and non-ignored EVENT_MORE_DATA is deprecated.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800132 *
133 * Parameters:
134 *
135 * streamType: Select the type of audio stream this track is attached to
Dima Zavinfce7a472011-04-19 22:30:36 -0700136 * (e.g. AUDIO_STREAM_MUSIC).
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800137 * sampleRate: Track sampling rate in Hz.
Dima Zavinfce7a472011-04-19 22:30:36 -0700138 * format: Audio format (e.g AUDIO_FORMAT_PCM_16_BIT for signed
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800139 * 16 bits per sample).
Glenn Kasten28b76b32012-07-03 17:24:41 -0700140 * channelMask: Channel mask.
Eric Laurentd8d61852012-03-05 17:06:40 -0800141 * frameCount: Minimum size of track PCM buffer in frames. This defines the
Glenn Kastenad2f6db2012-11-01 15:45:06 -0700142 * application's contribution to the
Eric Laurentd8d61852012-03-05 17:06:40 -0800143 * latency of the track. The actual size selected by the AudioTrack could be
144 * larger if the requested size is not compatible with current audio HAL
Glenn Kasten083d1c12012-11-30 15:00:36 -0800145 * configuration. Zero means to use a default value.
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700146 * flags: See comments on audio_output_flags_t in <system/audio.h>.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800147 * cbf: Callback function. If not null, this function is called periodically
Glenn Kasten083d1c12012-11-30 15:00:36 -0800148 * to provide new data and inform of marker, position updates, etc.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800149 * user: Context for use by the callback receiver.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800150 * notificationFrames: The callback function is called each time notificationFrames PCM
Glenn Kasten362c4e62011-12-14 10:28:06 -0800151 * frames have been consumed from track input buffer.
152 * sessionId: Specific session ID, or zero to use default.
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700153 * threadCanCallJava: Whether callbacks are made from an attached thread and thus can call JNI.
154 * If not present in parameter list, then fixed at false.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800155 */
156
Glenn Kastenfff6d712012-01-12 16:38:12 -0800157 AudioTrack( audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800158 uint32_t sampleRate = 0,
Glenn Kastene1c39622012-01-04 09:36:37 -0800159 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700160 audio_channel_mask_t channelMask = 0,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800161 int frameCount = 0,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700162 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
Glenn Kastena0d68332012-01-27 16:47:15 -0800163 callback_t cbf = NULL,
164 void* user = NULL,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700165 int notificationFrames = 0,
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700166 int sessionId = 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800167
Glenn Kasten083d1c12012-11-30 15:00:36 -0800168 /* Creates an audio track and registers it with AudioFlinger.
169 * With this constructor, the track is configured for static buffer mode.
170 * The format must not be 8-bit linear PCM.
171 * Data to be rendered is passed in a shared memory buffer
172 * identified by the argument sharedBuffer, which must be non-0.
173 * The memory should be initialized to the desired data before calling start().
Glenn Kasten4bae3642012-11-30 13:41:12 -0800174 * The write() method is not supported in this case.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800175 * 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 -0800176 * EVENT_UNDERRUN event.
Glenn Kasten083d1c12012-11-30 15:00:36 -0800177 * FIXME EVENT_MORE_DATA still occurs; it must be ignored.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800178 */
179
Glenn Kastenfff6d712012-01-12 16:38:12 -0800180 AudioTrack( audio_stream_type_t streamType,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800181 uint32_t sampleRate = 0,
Glenn Kastene1c39622012-01-04 09:36:37 -0800182 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700183 audio_channel_mask_t channelMask = 0,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800184 const sp<IMemory>& sharedBuffer = 0,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700185 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
Glenn Kastena0d68332012-01-27 16:47:15 -0800186 callback_t cbf = NULL,
187 void* user = NULL,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700188 int notificationFrames = 0,
Glenn Kasten4a4a0952012-03-19 11:38:14 -0700189 int sessionId = 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800190
191 /* Terminates the AudioTrack and unregisters it from AudioFlinger.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800192 * Also destroys all resources associated with the AudioTrack.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193 */
Glenn Kasten2799d742013-05-30 14:33:29 -0700194protected:
195 virtual ~AudioTrack();
196public:
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800197
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800198 /* 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
Glenn Kasten28b76b32012-07-03 17:24:41 -0700202 * - BAD_VALUE: invalid parameter (channelMask, format, sampleRate...)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800203 * - NO_INIT: audio server or audio hardware not initialized
Glenn Kasten083d1c12012-11-30 15:00:36 -0800204 * If sharedBuffer is non-0, the frameCount parameter is ignored and
205 * replaced by the shared buffer's total allocated size in frame units.
Glenn Kastenad2f6db2012-11-01 15:45:06 -0700206 */
Glenn Kastenfff6d712012-01-12 16:38:12 -0800207 status_t set(audio_stream_type_t streamType = AUDIO_STREAM_DEFAULT,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800208 uint32_t sampleRate = 0,
Glenn Kastene1c39622012-01-04 09:36:37 -0800209 audio_format_t format = AUDIO_FORMAT_DEFAULT,
Glenn Kasten28b76b32012-07-03 17:24:41 -0700210 audio_channel_mask_t channelMask = 0,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800211 int frameCount = 0,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700212 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
Glenn Kastena0d68332012-01-27 16:47:15 -0800213 callback_t cbf = NULL,
214 void* user = NULL,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800215 int notificationFrames = 0,
216 const sp<IMemory>& sharedBuffer = 0,
Eric Laurentbe916aa2010-06-01 23:49:17 -0700217 bool threadCanCallJava = false,
Glenn Kastenea7939a2012-03-14 12:56:26 -0700218 int sessionId = 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800219
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800220 /* Result of constructing the AudioTrack. This must be checked
Glenn Kasten362c4e62011-12-14 10:28:06 -0800221 * before using any AudioTrack API (except for set()), because using
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800222 * an uninitialized AudioTrack produces undefined results.
223 * See set() method above for possible return codes.
224 */
Glenn Kasten01437b72012-11-29 07:32:49 -0800225 status_t initCheck() const { return mStatus; }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800226
Glenn Kasten362c4e62011-12-14 10:28:06 -0800227 /* Returns this track's estimated latency in milliseconds.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800228 * This includes the latency due to AudioTrack buffer size, AudioMixer (if any)
229 * and audio hardware driver.
230 */
Glenn Kastenc9b2e202013-02-26 11:32:32 -0800231 uint32_t latency() const { return mLatency; }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800232
Glenn Kasten99e53b82012-01-19 08:59:58 -0800233 /* getters, see constructors and set() */
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800234
Glenn Kasten01437b72012-11-29 07:32:49 -0800235 audio_stream_type_t streamType() const { return mStreamType; }
236 audio_format_t format() const { return mFormat; }
Glenn Kastenb9980652012-01-11 09:48:27 -0800237
Glenn Kasten083d1c12012-11-30 15:00:36 -0800238 /* Return frame size in bytes, which for linear PCM is channelCount * (bit depth per channel / 8).
Glenn Kastenb9980652012-01-11 09:48:27 -0800239 * channelCount is determined from channelMask, and bit depth comes from format.
Glenn Kasten083d1c12012-11-30 15:00:36 -0800240 * For non-linear formats, the frame size is typically 1 byte.
Glenn Kastenb9980652012-01-11 09:48:27 -0800241 */
Glenn Kasten01437b72012-11-29 07:32:49 -0800242 uint32_t channelCount() const { return mChannelCount; }
Glenn Kastenb9980652012-01-11 09:48:27 -0800243
Glenn Kasten01437b72012-11-29 07:32:49 -0800244 uint32_t frameCount() const { return mFrameCount; }
245 size_t frameSize() const { return mFrameSize; }
246
Glenn Kasten083d1c12012-11-30 15:00:36 -0800247 /* Return the static buffer specified in constructor or set(), or 0 for streaming mode */
Glenn Kasten01437b72012-11-29 07:32:49 -0800248 sp<IMemory> sharedBuffer() const { return mSharedBuffer; }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800249
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800250 /* After it's created the track is not active. Call start() to
251 * make it active. If set, the callback will start being called.
Glenn Kasten083d1c12012-11-30 15:00:36 -0800252 * 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 -0800253 */
254 void start();
255
Glenn Kasten083d1c12012-11-30 15:00:36 -0800256 /* Stop a track.
257 * In static buffer mode, the track is stopped immediately.
258 * In streaming mode, the callback will cease being called and
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800259 * obtainBuffer returns STOPPED. Note that obtainBuffer() still works
260 * and will fill up buffers until the pool is exhausted.
Glenn Kasten083d1c12012-11-30 15:00:36 -0800261 * The stop does not occur immediately: any data remaining in the buffer
262 * 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 -0800263 */
264 void stop();
265 bool stopped() const;
266
Glenn Kasten4bae3642012-11-30 13:41:12 -0800267 /* Flush a stopped or paused track. All previously buffered data is discarded immediately.
268 * This has the effect of draining the buffers without mixing or output.
269 * Flush is intended for streaming mode, for example before switching to non-contiguous content.
270 * 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 -0800271 */
272 void flush();
273
Glenn Kasten083d1c12012-11-30 15:00:36 -0800274 /* Pause a track. After pause, the callback will cease being called and
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800275 * obtainBuffer returns STOPPED. Note that obtainBuffer() still works
276 * and will fill up buffers until the pool is exhausted.
Glenn Kasten083d1c12012-11-30 15:00:36 -0800277 * Volume is ramped down over the next mix buffer following the pause request,
278 * 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 -0800279 */
280 void pause();
281
Glenn Kasten362c4e62011-12-14 10:28:06 -0800282 /* Set volume for this track, mostly used for games' sound effects
283 * left and right volumes. Levels must be >= 0.0 and <= 1.0.
Glenn Kastenb1c09932012-02-27 16:21:04 -0800284 * This is the older API. New applications should use setVolume(float) when possible.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800285 */
Eric Laurentbe916aa2010-06-01 23:49:17 -0700286 status_t setVolume(float left, float right);
Glenn Kastenb1c09932012-02-27 16:21:04 -0800287
288 /* Set volume for all channels. This is the preferred API for new applications,
289 * especially for multi-channel content.
290 */
291 status_t setVolume(float volume);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800292
Glenn Kasten362c4e62011-12-14 10:28:06 -0800293 /* Set the send level for this track. An auxiliary effect should be attached
294 * to the track with attachEffect(). Level must be >= 0.0 and <= 1.0.
Eric Laurentbe916aa2010-06-01 23:49:17 -0700295 */
Eric Laurent2beeb502010-07-16 07:43:46 -0700296 status_t setAuxEffectSendLevel(float level);
Glenn Kastena5224f32012-01-04 12:41:44 -0800297 void getAuxEffectSendLevel(float* level) const;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700298
Glenn Kastenad2f6db2012-11-01 15:45:06 -0700299 /* Set sample rate for this track in Hz, mostly used for games' sound effects
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800300 */
Glenn Kasten3b16c762012-11-14 08:44:39 -0800301 status_t setSampleRate(uint32_t sampleRate);
302
303 /* Return current sample rate in Hz, or 0 if unknown */
Glenn Kastena5224f32012-01-04 12:41:44 -0800304 uint32_t getSampleRate() const;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800305
306 /* Enables looping and sets the start and end points of looping.
Glenn Kasten083d1c12012-11-30 15:00:36 -0800307 * Only supported for static buffer mode.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800308 *
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800309 * FIXME The comments below are for the new planned interpretation which is not yet implemented.
310 * Currently the legacy behavior is still implemented, where loopStart and loopEnd
311 * are in wrapping (overflow) frame units like the return value of getPosition().
312 * The plan is to fix all callers to use the new version at same time implementation changes.
313 *
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800314 * Parameters:
315 *
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800316 * loopStart: loop start in frames relative to start of buffer.
317 * loopEnd: loop end in frames relative to start of buffer.
Glenn Kasten362c4e62011-12-14 10:28:06 -0800318 * loopCount: number of loops to execute. Calling setLoop() with loopCount == 0 cancels any
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800319 * pending or active loop. loopCount == -1 means infinite looping.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800320 *
321 * For proper operation the following condition must be respected:
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800322 * loopCount != 0 implies 0 <= loopStart < loopEnd <= frameCount().
323 *
324 * If the loop period (loopEnd - loopStart) is too small for the implementation to support,
325 * setLoop() will return BAD_VALUE.
326 *
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800327 */
328 status_t setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800329
Glenn Kasten362c4e62011-12-14 10:28:06 -0800330 /* Sets marker position. When playback reaches the number of frames specified, a callback with
331 * event type EVENT_MARKER is called. Calling setMarkerPosition with marker == 0 cancels marker
Glenn Kasten083d1c12012-11-30 15:00:36 -0800332 * notification callback. To set a marker at a position which would compute as 0,
333 * a workaround is to the set the marker at a nearby position such as -1 or 1.
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700334 * If the AudioTrack has been opened with no callback function associated, the operation will
335 * fail.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800336 *
337 * Parameters:
338 *
Glenn Kasten083d1c12012-11-30 15:00:36 -0800339 * marker: marker position expressed in wrapping (overflow) frame units,
340 * like the return value of getPosition().
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800341 *
342 * Returned status (from utils/Errors.h) can be:
343 * - NO_ERROR: successful operation
344 * - INVALID_OPERATION: the AudioTrack has no callback installed.
345 */
346 status_t setMarkerPosition(uint32_t marker);
Glenn Kastena5224f32012-01-04 12:41:44 -0800347 status_t getMarkerPosition(uint32_t *marker) const;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800348
Glenn Kasten362c4e62011-12-14 10:28:06 -0800349 /* Sets position update period. Every time the number of frames specified has been played,
350 * a callback with event type EVENT_NEW_POS is called.
351 * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification
352 * callback.
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700353 * If the AudioTrack has been opened with no callback function associated, the operation will
354 * fail.
Glenn Kasten083d1c12012-11-30 15:00:36 -0800355 * Extremely small values may be rounded up to a value the implementation can support.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800356 *
357 * Parameters:
358 *
359 * updatePeriod: position update notification period expressed in frames.
360 *
361 * Returned status (from utils/Errors.h) can be:
362 * - NO_ERROR: successful operation
363 * - INVALID_OPERATION: the AudioTrack has no callback installed.
364 */
365 status_t setPositionUpdatePeriod(uint32_t updatePeriod);
Glenn Kastena5224f32012-01-04 12:41:44 -0800366 status_t getPositionUpdatePeriod(uint32_t *updatePeriod) const;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800367
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800368 /* Sets playback head position.
369 * Only supported for static buffer mode.
370 *
371 * FIXME The comments below are for the new planned interpretation which is not yet implemented.
372 * Currently the legacy behavior is still implemented, where the new position
373 * is in wrapping (overflow) frame units like the return value of getPosition().
374 * The plan is to fix all callers to use the new version at same time implementation changes.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800375 *
376 * Parameters:
377 *
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800378 * position: New playback head position in frames relative to start of buffer.
379 * 0 <= position <= frameCount(). Note that end of buffer is permitted,
380 * but will result in an immediate underrun if started.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800381 *
382 * Returned status (from utils/Errors.h) can be:
383 * - NO_ERROR: successful operation
Glenn Kasten083d1c12012-11-30 15:00:36 -0800384 * - INVALID_OPERATION: the AudioTrack is not stopped or paused, or is streaming mode.
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700385 * - BAD_VALUE: The specified position is beyond the number of frames present in AudioTrack
386 * buffer
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800387 */
388 status_t setPosition(uint32_t position);
Glenn Kasten083d1c12012-11-30 15:00:36 -0800389
390 /* Return the total number of frames played since playback start.
391 * The counter will wrap (overflow) periodically, e.g. every ~27 hours at 44.1 kHz.
392 * It is reset to zero by flush(), reload(), and stop().
393 */
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800394 status_t getPosition(uint32_t *position);
395
Glenn Kasten9c6745f2012-11-30 13:35:29 -0800396#if 0
397 /* For static buffer mode only, this returns the current playback position in frames
398 * relative to start of buffer. It is analogous to the new API for
399 * setLoop() and setPosition(). After underrun, the position will be at end of buffer.
400 */
401 status_t getBufferPosition(uint32_t *position);
402#endif
403
Glenn Kasten362c4e62011-12-14 10:28:06 -0800404 /* Forces AudioTrack buffer full condition. When playing a static buffer, this method avoids
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800405 * rewriting the buffer before restarting playback after a stop.
406 * This method must be called with the AudioTrack in paused or stopped state.
Glenn Kasten083d1c12012-11-30 15:00:36 -0800407 * Not allowed in streaming mode.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800408 *
409 * Returned status (from utils/Errors.h) can be:
410 * - NO_ERROR: successful operation
Glenn Kasten083d1c12012-11-30 15:00:36 -0800411 * - INVALID_OPERATION: the AudioTrack is not stopped or paused, or is streaming mode.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800412 */
413 status_t reload();
414
Glenn Kasten362c4e62011-12-14 10:28:06 -0800415 /* Returns a handle on the audio output used by this AudioTrack.
Eric Laurentc2f1f072009-07-17 12:17:14 -0700416 *
417 * Parameters:
418 * none.
419 *
420 * Returned value:
421 * handle on audio hardware output
422 */
423 audio_io_handle_t getOutput();
424
Glenn Kasten362c4e62011-12-14 10:28:06 -0800425 /* Returns the unique session ID associated with this track.
Eric Laurentbe916aa2010-06-01 23:49:17 -0700426 *
427 * Parameters:
428 * none.
429 *
430 * Returned value:
Glenn Kasten362c4e62011-12-14 10:28:06 -0800431 * AudioTrack session ID.
Eric Laurentbe916aa2010-06-01 23:49:17 -0700432 */
Glenn Kasten01437b72012-11-29 07:32:49 -0800433 int getSessionId() const { return mSessionId; }
Eric Laurentbe916aa2010-06-01 23:49:17 -0700434
Glenn Kasten362c4e62011-12-14 10:28:06 -0800435 /* Attach track auxiliary output to specified effect. Use effectId = 0
Eric Laurentbe916aa2010-06-01 23:49:17 -0700436 * to detach track from effect.
437 *
438 * Parameters:
439 *
440 * effectId: effectId obtained from AudioEffect::id().
441 *
442 * Returned status (from utils/Errors.h) can be:
443 * - NO_ERROR: successful operation
444 * - INVALID_OPERATION: the effect is not an auxiliary effect.
445 * - BAD_VALUE: The specified effect ID is invalid
446 */
447 status_t attachAuxEffect(int effectId);
448
Glenn Kasten362c4e62011-12-14 10:28:06 -0800449 /* Obtains a buffer of "frameCount" frames. The buffer must be
Glenn Kasten99e53b82012-01-19 08:59:58 -0800450 * filled entirely, and then released with releaseBuffer().
451 * If the track is stopped, obtainBuffer() returns
Glenn Kasten362c4e62011-12-14 10:28:06 -0800452 * STOPPED instead of NO_ERROR as long as there are buffers available,
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800453 * at which point NO_MORE_BUFFERS is returned.
Glenn Kastenad2f6db2012-11-01 15:45:06 -0700454 * Buffers will be returned until the pool
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800455 * is exhausted, at which point obtainBuffer() will either block
456 * or return WOULD_BLOCK depending on the value of the "blocking"
457 * parameter.
Glenn Kasten99e53b82012-01-19 08:59:58 -0800458 *
Glenn Kasten083d1c12012-11-30 15:00:36 -0800459 * obtainBuffer() and releaseBuffer() are deprecated for direct use by applications,
460 * which should use write() or callback EVENT_MORE_DATA instead.
461 *
Glenn Kasten99e53b82012-01-19 08:59:58 -0800462 * Interpretation of waitCount:
463 * +n limits wait time to n * WAIT_PERIOD_MS,
464 * -1 causes an (almost) infinite wait time,
465 * 0 non-blocking.
Glenn Kasten05d49992012-11-06 14:25:20 -0800466 *
467 * Buffer fields
468 * On entry:
469 * frameCount number of frames requested
470 * After error return:
471 * frameCount 0
472 * size 0
Glenn Kasten22eb4e22012-11-07 14:03:00 -0800473 * raw undefined
Glenn Kasten05d49992012-11-06 14:25:20 -0800474 * After successful return:
475 * frameCount actual number of frames available, <= number requested
476 * size actual number of bytes available
477 * raw pointer to the buffer
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800478 */
479
480 enum {
Glenn Kasten335787f2012-01-20 17:00:00 -0800481 NO_MORE_BUFFERS = 0x80000001, // same name in AudioFlinger.h, ok to be different value
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800482 STOPPED = 1
483 };
484
485 status_t obtainBuffer(Buffer* audioBuffer, int32_t waitCount);
Glenn Kasten99e53b82012-01-19 08:59:58 -0800486
487 /* Release a filled buffer of "frameCount" frames for AudioFlinger to process. */
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800488 void releaseBuffer(Buffer* audioBuffer);
489
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800490 /* As a convenience we provide a write() interface to the audio buffer.
Glenn Kasten99e53b82012-01-19 08:59:58 -0800491 * This is implemented on top of obtainBuffer/releaseBuffer. For best
492 * performance use callbacks. Returns actual number of bytes written >= 0,
493 * or one of the following negative status codes:
494 * INVALID_OPERATION AudioTrack is configured for shared buffer mode
495 * BAD_VALUE size is invalid
496 * STOPPED AudioTrack was stopped during the write
497 * NO_MORE_BUFFERS when obtainBuffer() returns same
498 * or any other error code returned by IAudioTrack::start() or restoreTrack_l().
Glenn Kasten083d1c12012-11-30 15:00:36 -0800499 * Not supported for static buffer mode.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800500 */
501 ssize_t write(const void* buffer, size_t size);
502
503 /*
504 * Dumps the state of an audio track.
505 */
506 status_t dump(int fd, const Vector<String16>& args) const;
507
John Grossman4ff14ba2012-02-08 16:37:41 -0800508protected:
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800509 /* copying audio tracks is not allowed */
510 AudioTrack(const AudioTrack& other);
511 AudioTrack& operator = (const AudioTrack& other);
512
513 /* a small internal class to handle the callback */
514 class AudioTrackThread : public Thread
515 {
516 public:
517 AudioTrackThread(AudioTrack& receiver, bool bCanCallJava = false);
Glenn Kasten3acbd052012-02-28 10:39:56 -0800518
519 // Do not call Thread::requestExitAndWait() without first calling requestExit().
520 // Thread::requestExitAndWait() is not virtual, and the implementation doesn't do enough.
521 virtual void requestExit();
522
523 void pause(); // suspend thread from execution at next loop boundary
524 void resume(); // allow thread to execute, if not requested to exit
525
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800526 private:
527 friend class AudioTrack;
528 virtual bool threadLoop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800529 AudioTrack& mReceiver;
Glenn Kastena997e7a2012-08-07 09:44:19 -0700530 ~AudioTrackThread();
Glenn Kasten3acbd052012-02-28 10:39:56 -0800531 Mutex mMyLock; // Thread::mLock is private
532 Condition mMyCond; // Thread::mThreadExitedCondition is private
533 bool mPaused; // whether thread is currently paused
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800534 };
535
Glenn Kasten99e53b82012-01-19 08:59:58 -0800536 // body of AudioTrackThread::threadLoop()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800537 bool processAudioBuffer(const sp<AudioTrackThread>& thread);
Glenn Kastenea7939a2012-03-14 12:56:26 -0700538
Glenn Kastend5ed6e82012-11-02 13:05:14 -0700539 // caller must hold lock on mLock for all _l methods
Glenn Kastenfff6d712012-01-12 16:38:12 -0800540 status_t createTrack_l(audio_stream_type_t streamType,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800541 uint32_t sampleRate,
Glenn Kastene1c39622012-01-04 09:36:37 -0800542 audio_format_t format,
Glenn Kastene33054e2012-11-14 12:54:39 -0800543 size_t frameCount,
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700544 audio_output_flags_t flags,
Eric Laurent34f1d8e2009-11-04 08:27:26 -0800545 const sp<IMemory>& sharedBuffer,
Glenn Kasten291f4d52012-03-19 12:16:56 -0700546 audio_io_handle_t output);
Glenn Kasten4bae3642012-11-30 13:41:12 -0800547
548 // can only be called when !mActive
Eric Laurent1703cdf2011-03-07 14:52:59 -0800549 void flush_l();
Glenn Kasten4bae3642012-11-30 13:41:12 -0800550
Eric Laurent1703cdf2011-03-07 14:52:59 -0800551 status_t setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount);
552 audio_io_handle_t getOutput_l();
553 status_t restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart);
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800554 bool stopped_l() const { return !mActive; }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800556 sp<IAudioTrack> mAudioTrack;
557 sp<IMemory> mCblkMemory;
558 sp<AudioTrackThread> mAudioTrackThread;
559
560 float mVolume[2];
Eric Laurentbe916aa2010-06-01 23:49:17 -0700561 float mSendLevel;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800562 uint32_t mSampleRate;
Glenn Kastenb6037442012-11-14 13:42:25 -0800563 size_t mFrameCount; // corresponds to current IAudioTrack
564 size_t mReqFrameCount; // frame count to request the next time a new
565 // IAudioTrack is needed
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566
Glenn Kastena47f3162012-11-07 10:13:08 -0800567 audio_track_cblk_t* mCblk; // re-load after mLock.unlock()
Glenn Kasten22eb4e22012-11-07 14:03:00 -0800568
569 // Starting address of buffers in shared memory. If there is a shared buffer, mBuffers
570 // is the value of pointer() for the shared buffer, otherwise mBuffers points
571 // immediately after the control block. This address is for the mapping within client
572 // address space. AudioFlinger::TrackBase::mBuffer is for the server address space.
573 void* mBuffers;
574
Glenn Kasten60a83922012-06-21 12:56:37 -0700575 audio_format_t mFormat; // as requested by client, not forced to 16-bit
Glenn Kastenfff6d712012-01-12 16:38:12 -0800576 audio_stream_type_t mStreamType;
Glenn Kastene4756fe2012-11-29 13:38:14 -0800577 uint32_t mChannelCount;
Glenn Kasten28b76b32012-07-03 17:24:41 -0700578 audio_channel_mask_t mChannelMask;
Glenn Kasten83a03822012-11-12 07:58:20 -0800579
580 // mFrameSize is equal to mFrameSizeAF for non-PCM or 16-bit PCM data.
581 // For 8-bit PCM data, mFrameSizeAF is
582 // twice as large because data is expanded to 16-bit before being stored in buffer.
583 size_t mFrameSize; // app-level frame size
584 size_t mFrameSizeAF; // AudioFlinger frame size
585
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800586 status_t mStatus;
587 uint32_t mLatency;
588
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800589 bool mActive; // protected by mLock
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800590
Glenn Kasten99e53b82012-01-19 08:59:58 -0800591 callback_t mCbf; // callback handler for events, or NULL
Glenn Kastenad2f6db2012-11-01 15:45:06 -0700592 void* mUserData; // for client callback handler
593
594 // for notification APIs
Glenn Kasten85ab62c2012-11-01 11:11:38 -0700595 uint32_t mNotificationFramesReq; // requested number of frames between each
596 // notification callback
597 uint32_t mNotificationFramesAct; // actual number of frames between each
598 // notification callback
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800599 sp<IMemory> mSharedBuffer;
600 int mLoopCount;
601 uint32_t mRemainingFrames;
Glenn Kasten083d1c12012-11-30 15:00:36 -0800602 uint32_t mMarkerPosition; // in wrapping (overflow) frame units
Jean-Michel Trivi2c22aeb2009-03-24 18:11:07 -0700603 bool mMarkerReached;
Glenn Kastenad2f6db2012-11-01 15:45:06 -0700604 uint32_t mNewPosition; // in frames
605 uint32_t mUpdatePeriod; // in frames
606
Jean-Michel Trivicd075942011-08-25 16:47:23 -0700607 bool mFlushed; // FIXME will be made obsolete by making flush() synchronous
Eric Laurent0ca3cf92012-04-18 09:24:29 -0700608 audio_output_flags_t mFlags;
Eric Laurentbe916aa2010-06-01 23:49:17 -0700609 int mSessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700610 int mAuxEffectId;
Glenn Kastend5ed6e82012-11-02 13:05:14 -0700611
612 // When locking both mLock and mCblk->lock, must lock in this order to avoid deadlock:
613 // 1. mLock
614 // 2. mCblk->lock
615 // It is OK to lock only mCblk->lock.
Glenn Kasten9a2aaf92012-01-03 09:42:47 -0800616 mutable Mutex mLock;
Glenn Kastend5ed6e82012-11-02 13:05:14 -0700617
John Grossman4ff14ba2012-02-08 16:37:41 -0800618 bool mIsTimed;
Glenn Kasten87913512011-06-22 16:15:25 -0700619 int mPreviousPriority; // before start()
Glenn Kastena6364332012-04-19 09:35:04 -0700620 SchedPolicy mPreviousSchedulingGroup;
Glenn Kastene3aa6592012-12-04 12:22:46 -0800621 AudioTrackClientProxy* mProxy;
Glenn Kastena07f17c2013-04-23 12:39:37 -0700622 bool mAwaitBoost; // thread should wait for priority boost before running
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800623};
624
John Grossman4ff14ba2012-02-08 16:37:41 -0800625class TimedAudioTrack : public AudioTrack
626{
627public:
628 TimedAudioTrack();
629
630 /* allocate a shared memory buffer that can be passed to queueTimedBuffer */
631 status_t allocateTimedBuffer(size_t size, sp<IMemory>* buffer);
632
633 /* queue a buffer obtained via allocateTimedBuffer for playback at the
Glenn Kastenc3ae93f2012-07-30 10:59:30 -0700634 given timestamp. PTS units are microseconds on the media time timeline.
John Grossman4ff14ba2012-02-08 16:37:41 -0800635 The media time transform (set with setMediaTimeTransform) set by the
636 audio producer will handle converting from media time to local time
637 (perhaps going through the common time timeline in the case of
638 synchronized multiroom audio case) */
639 status_t queueTimedBuffer(const sp<IMemory>& buffer, int64_t pts);
640
641 /* define a transform between media time and either common time or
642 local time */
643 enum TargetTimeline {LOCAL_TIME, COMMON_TIME};
644 status_t setMediaTimeTransform(const LinearTransform& xform,
645 TargetTimeline target);
646};
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800647
648}; // namespace android
649
650#endif // ANDROID_AUDIOTRACK_H