The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Agopian | 7562408 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 29 | #include <binder/IInterface.h> |
| 30 | #include <binder/IMemory.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | #include <utils/threads.h> |
| 32 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | namespace android { |
| 34 | |
| 35 | // ---------------------------------------------------------------------------- |
| 36 | |
| 37 | class audio_track_cblk_t; |
| 38 | |
| 39 | // ---------------------------------------------------------------------------- |
| 40 | |
Glenn Kasten | b68a91a | 2011-11-15 13:55:13 -0800 | [diff] [blame] | 41 | class AudioTrack : virtual public RefBase |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | { |
| 43 | public: |
| 44 | enum channel_index { |
| 45 | MONO = 0, |
| 46 | LEFT = 0, |
| 47 | RIGHT = 1 |
| 48 | }; |
| 49 | |
| 50 | /* Events used by AudioTrack callback function (audio_track_cblk_t). |
| 51 | */ |
| 52 | enum event_type { |
| 53 | EVENT_MORE_DATA = 0, // Request to write more data to PCM buffer. |
| 54 | EVENT_UNDERRUN = 1, // PCM buffer underrun occured. |
| 55 | EVENT_LOOP_END = 2, // Sample loop end was reached; playback restarted from loop start if loop count was not 0. |
| 56 | EVENT_MARKER = 3, // Playback head is at the specified marker position (See setMarkerPosition()). |
| 57 | EVENT_NEW_POS = 4, // Playback head is at a new position (See setPositionUpdatePeriod()). |
| 58 | EVENT_BUFFER_END = 5 // Playback head is at the end of the buffer. |
| 59 | }; |
| 60 | |
| 61 | /* Create Buffer on the stack and pass it to obtainBuffer() |
| 62 | * and releaseBuffer(). |
| 63 | */ |
| 64 | |
| 65 | class Buffer |
| 66 | { |
| 67 | public: |
| 68 | enum { |
| 69 | MUTE = 0x00000001 |
| 70 | }; |
| 71 | uint32_t flags; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | int format; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 73 | int channelCount; // will be removed in the future, do not use |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | size_t frameCount; |
| 75 | size_t size; |
| 76 | union { |
| 77 | void* raw; |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 78 | short* i16; // signed 16-bit |
| 79 | int8_t* i8; // unsigned 8-bit, offset by 0x80 |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | }; |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | /* As a convenience, if a callback is supplied, a handler thread |
| 85 | * is automatically created with the appropriate priority. This thread |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 86 | * invokes the callback when a new buffer becomes available or an underrun condition occurs. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | * Parameters: |
| 88 | * |
| 89 | * event: type of event notified (see enum AudioTrack::event_type). |
| 90 | * user: Pointer to context for use by the callback receiver. |
| 91 | * info: Pointer to optional parameter according to event type: |
| 92 | * - EVENT_MORE_DATA: pointer to AudioTrack::Buffer struct. The callback must not write |
| 93 | * more bytes than indicated by 'size' field and update 'size' if less bytes are |
| 94 | * written. |
| 95 | * - EVENT_UNDERRUN: unused. |
| 96 | * - EVENT_LOOP_END: pointer to an int indicating the number of loops remaining. |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 97 | * - EVENT_MARKER: pointer to an uint32_t containing the marker position in frames. |
| 98 | * - EVENT_NEW_POS: pointer to an uint32_t containing the new position in frames. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | * - EVENT_BUFFER_END: unused. |
| 100 | */ |
| 101 | |
Glenn Kasten | d217a8c | 2011-06-01 15:20:35 -0700 | [diff] [blame] | 102 | typedef void (*callback_t)(int event, void* user, void *info); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 104 | /* Returns the minimum frame count required for the successful creation of |
| 105 | * an AudioTrack object. |
| 106 | * Returned status (from utils/Errors.h) can be: |
| 107 | * - NO_ERROR: successful operation |
| 108 | * - NO_INIT: audio server or audio hardware not initialized |
| 109 | */ |
| 110 | |
| 111 | static status_t getMinFrameCount(int* frameCount, |
| 112 | int streamType =-1, |
| 113 | uint32_t sampleRate = 0); |
| 114 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | /* Constructs an uninitialized AudioTrack. No connection with |
| 116 | * AudioFlinger takes place. |
| 117 | */ |
| 118 | AudioTrack(); |
| 119 | |
| 120 | /* Creates an audio track and registers it with AudioFlinger. |
| 121 | * Once created, the track needs to be started before it can be used. |
| 122 | * Unspecified values are set to the audio hardware's current |
| 123 | * values. |
| 124 | * |
| 125 | * Parameters: |
| 126 | * |
| 127 | * streamType: Select the type of audio stream this track is attached to |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 128 | * (e.g. AUDIO_STREAM_MUSIC). |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | * sampleRate: Track sampling rate in Hz. |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 130 | * format: Audio format (e.g AUDIO_FORMAT_PCM_16_BIT for signed |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | * 16 bits per sample). |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 132 | * channelMask: Channel mask: see audio_channels_t. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | * frameCount: Total size of track PCM buffer in frames. This defines the |
| 134 | * latency of the track. |
| 135 | * flags: Reserved for future use. |
| 136 | * cbf: Callback function. If not null, this function is called periodically |
| 137 | * to request new PCM data. |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 138 | * user: Context for use by the callback receiver. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | * notificationFrames: The callback function is called each time notificationFrames PCM |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 140 | * frames have been consumed from track input buffer. |
| 141 | * sessionId: Specific session ID, or zero to use default. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | */ |
| 143 | |
| 144 | AudioTrack( int streamType, |
| 145 | uint32_t sampleRate = 0, |
| 146 | int format = 0, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 147 | int channelMask = 0, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | int frameCount = 0, |
| 149 | uint32_t flags = 0, |
| 150 | callback_t cbf = 0, |
| 151 | void* user = 0, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 152 | int notificationFrames = 0, |
| 153 | int sessionId = 0); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | |
| 155 | /* Creates an audio track and registers it with AudioFlinger. With this constructor, |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 156 | * the PCM data to be rendered by AudioTrack is passed in a shared memory buffer |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | * identified by the argument sharedBuffer. This prototype is for static buffer playback. |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 158 | * PCM data must be present in memory before the AudioTrack is started. |
| 159 | * The write() and flush() methods are not supported in this case. |
| 160 | * It is recommended to pass a callback function to be notified of playback end by an |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | * EVENT_UNDERRUN event. |
| 162 | */ |
| 163 | |
| 164 | AudioTrack( int streamType, |
| 165 | uint32_t sampleRate = 0, |
| 166 | int format = 0, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 167 | int channelMask = 0, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | const sp<IMemory>& sharedBuffer = 0, |
| 169 | uint32_t flags = 0, |
| 170 | callback_t cbf = 0, |
| 171 | void* user = 0, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 172 | int notificationFrames = 0, |
| 173 | int sessionId = 0); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | |
| 175 | /* Terminates the AudioTrack and unregisters it from AudioFlinger. |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 176 | * Also destroys all resources associated with the AudioTrack. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | */ |
| 178 | ~AudioTrack(); |
| 179 | |
| 180 | |
| 181 | /* Initialize an uninitialized AudioTrack. |
| 182 | * Returned status (from utils/Errors.h) can be: |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 183 | * - NO_ERROR: successful initialization |
| 184 | * - INVALID_OPERATION: AudioTrack is already initialized |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 185 | * - BAD_VALUE: invalid parameter (channels, format, sampleRate...) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | * - NO_INIT: audio server or audio hardware not initialized |
| 187 | * */ |
| 188 | status_t set(int streamType =-1, |
| 189 | uint32_t sampleRate = 0, |
| 190 | int format = 0, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 191 | int channelMask = 0, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | int frameCount = 0, |
| 193 | uint32_t flags = 0, |
| 194 | callback_t cbf = 0, |
| 195 | void* user = 0, |
| 196 | int notificationFrames = 0, |
| 197 | const sp<IMemory>& sharedBuffer = 0, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 198 | bool threadCanCallJava = false, |
| 199 | int sessionId = 0); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | |
| 201 | |
| 202 | /* Result of constructing the AudioTrack. This must be checked |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 203 | * before using any AudioTrack API (except for set()), because using |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | * an uninitialized AudioTrack produces undefined results. |
| 205 | * See set() method above for possible return codes. |
| 206 | */ |
| 207 | status_t initCheck() const; |
| 208 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 209 | /* Returns this track's estimated latency in milliseconds. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | * This includes the latency due to AudioTrack buffer size, AudioMixer (if any) |
| 211 | * and audio hardware driver. |
| 212 | */ |
| 213 | uint32_t latency() const; |
| 214 | |
| 215 | /* getters, see constructor */ |
| 216 | |
| 217 | int streamType() const; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | int format() const; |
| 219 | int channelCount() const; |
| 220 | uint32_t frameCount() const; |
| 221 | int frameSize() const; |
| 222 | sp<IMemory>& sharedBuffer(); |
| 223 | |
| 224 | |
| 225 | /* After it's created the track is not active. Call start() to |
| 226 | * make it active. If set, the callback will start being called. |
| 227 | */ |
| 228 | void start(); |
| 229 | |
| 230 | /* Stop a track. If set, the callback will cease being called and |
| 231 | * obtainBuffer returns STOPPED. Note that obtainBuffer() still works |
| 232 | * and will fill up buffers until the pool is exhausted. |
| 233 | */ |
| 234 | void stop(); |
| 235 | bool stopped() const; |
| 236 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 237 | /* Flush a stopped track. All pending buffers are discarded. |
| 238 | * This function has no effect if the track is not stopped. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | */ |
| 240 | void flush(); |
| 241 | |
| 242 | /* Pause a track. If set, the callback will cease being called and |
| 243 | * obtainBuffer returns STOPPED. Note that obtainBuffer() still works |
| 244 | * and will fill up buffers until the pool is exhausted. |
| 245 | */ |
| 246 | void pause(); |
| 247 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 248 | /* Mute or unmute this track. |
| 249 | * While muted, the callback, if set, is still called. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | */ |
| 251 | void mute(bool); |
| 252 | bool muted() const; |
| 253 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 254 | /* Set volume for this track, mostly used for games' sound effects |
| 255 | * left and right volumes. Levels must be >= 0.0 and <= 1.0. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | */ |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 257 | status_t setVolume(float left, float right); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | void getVolume(float* left, float* right); |
| 259 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 260 | /* Set the send level for this track. An auxiliary effect should be attached |
| 261 | * to the track with attachEffect(). Level must be >= 0.0 and <= 1.0. |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 262 | */ |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 263 | status_t setAuxEffectSendLevel(float level); |
| 264 | void getAuxEffectSendLevel(float* level); |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 265 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 266 | /* Set sample rate for this track, mostly used for games' sound effects |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | */ |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 268 | status_t setSampleRate(int sampleRate); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | uint32_t getSampleRate(); |
| 270 | |
| 271 | /* Enables looping and sets the start and end points of looping. |
| 272 | * |
| 273 | * Parameters: |
| 274 | * |
| 275 | * loopStart: loop start expressed as the number of PCM frames played since AudioTrack start. |
| 276 | * loopEnd: loop end expressed as the number of PCM frames played since AudioTrack start. |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 277 | * loopCount: number of loops to execute. Calling setLoop() with loopCount == 0 cancels any |
| 278 | * pending or active loop. loopCount = -1 means infinite looping. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | * |
| 280 | * For proper operation the following condition must be respected: |
| 281 | * (loopEnd-loopStart) <= framecount() |
| 282 | */ |
| 283 | status_t setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount); |
| 284 | status_t getLoop(uint32_t *loopStart, uint32_t *loopEnd, int *loopCount); |
| 285 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 286 | /* Sets marker position. When playback reaches the number of frames specified, a callback with |
| 287 | * event type EVENT_MARKER is called. Calling setMarkerPosition with marker == 0 cancels marker |
| 288 | * notification callback. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | * If the AudioTrack has been opened with no callback function associated, the operation will fail. |
| 290 | * |
| 291 | * Parameters: |
| 292 | * |
| 293 | * marker: marker position expressed in frames. |
| 294 | * |
| 295 | * Returned status (from utils/Errors.h) can be: |
| 296 | * - NO_ERROR: successful operation |
| 297 | * - INVALID_OPERATION: the AudioTrack has no callback installed. |
| 298 | */ |
| 299 | status_t setMarkerPosition(uint32_t marker); |
| 300 | status_t getMarkerPosition(uint32_t *marker); |
| 301 | |
| 302 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 303 | /* Sets position update period. Every time the number of frames specified has been played, |
| 304 | * a callback with event type EVENT_NEW_POS is called. |
| 305 | * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification |
| 306 | * callback. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | * If the AudioTrack has been opened with no callback function associated, the operation will fail. |
| 308 | * |
| 309 | * Parameters: |
| 310 | * |
| 311 | * updatePeriod: position update notification period expressed in frames. |
| 312 | * |
| 313 | * Returned status (from utils/Errors.h) can be: |
| 314 | * - NO_ERROR: successful operation |
| 315 | * - INVALID_OPERATION: the AudioTrack has no callback installed. |
| 316 | */ |
| 317 | status_t setPositionUpdatePeriod(uint32_t updatePeriod); |
| 318 | status_t getPositionUpdatePeriod(uint32_t *updatePeriod); |
| 319 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | /* Sets playback head position within AudioTrack buffer. The new position is specified |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 321 | * in number of frames. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | * This method must be called with the AudioTrack in paused or stopped state. |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 323 | * Note that the actual position set is <position> modulo the AudioTrack buffer size in frames. |
| 324 | * Therefore using this method makes sense only when playing a "static" audio buffer |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | * as opposed to streaming. |
| 326 | * The getPosition() method on the other hand returns the total number of frames played since |
| 327 | * playback start. |
| 328 | * |
| 329 | * Parameters: |
| 330 | * |
| 331 | * position: New playback head position within AudioTrack buffer. |
| 332 | * |
| 333 | * Returned status (from utils/Errors.h) can be: |
| 334 | * - NO_ERROR: successful operation |
| 335 | * - INVALID_OPERATION: the AudioTrack is not stopped. |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 336 | * - BAD_VALUE: The specified position is beyond the number of frames present in AudioTrack buffer |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | */ |
| 338 | status_t setPosition(uint32_t position); |
| 339 | status_t getPosition(uint32_t *position); |
| 340 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 341 | /* Forces AudioTrack buffer full condition. When playing a static buffer, this method avoids |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | * rewriting the buffer before restarting playback after a stop. |
| 343 | * This method must be called with the AudioTrack in paused or stopped state. |
| 344 | * |
| 345 | * Returned status (from utils/Errors.h) can be: |
| 346 | * - NO_ERROR: successful operation |
| 347 | * - INVALID_OPERATION: the AudioTrack is not stopped. |
| 348 | */ |
| 349 | status_t reload(); |
| 350 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 351 | /* Returns a handle on the audio output used by this AudioTrack. |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 352 | * |
| 353 | * Parameters: |
| 354 | * none. |
| 355 | * |
| 356 | * Returned value: |
| 357 | * handle on audio hardware output |
| 358 | */ |
| 359 | audio_io_handle_t getOutput(); |
| 360 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 361 | /* Returns the unique session ID associated with this track. |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 362 | * |
| 363 | * Parameters: |
| 364 | * none. |
| 365 | * |
| 366 | * Returned value: |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 367 | * AudioTrack session ID. |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 368 | */ |
| 369 | int getSessionId(); |
| 370 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 371 | /* Attach track auxiliary output to specified effect. Use effectId = 0 |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 372 | * to detach track from effect. |
| 373 | * |
| 374 | * Parameters: |
| 375 | * |
| 376 | * effectId: effectId obtained from AudioEffect::id(). |
| 377 | * |
| 378 | * Returned status (from utils/Errors.h) can be: |
| 379 | * - NO_ERROR: successful operation |
| 380 | * - INVALID_OPERATION: the effect is not an auxiliary effect. |
| 381 | * - BAD_VALUE: The specified effect ID is invalid |
| 382 | */ |
| 383 | status_t attachAuxEffect(int effectId); |
| 384 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 385 | /* Obtains a buffer of "frameCount" frames. The buffer must be |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | * filled entirely. If the track is stopped, obtainBuffer() returns |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 387 | * STOPPED instead of NO_ERROR as long as there are buffers available, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | * at which point NO_MORE_BUFFERS is returned. |
| 389 | * Buffers will be returned until the pool (buffercount()) |
| 390 | * is exhausted, at which point obtainBuffer() will either block |
| 391 | * or return WOULD_BLOCK depending on the value of the "blocking" |
| 392 | * parameter. |
| 393 | */ |
| 394 | |
| 395 | enum { |
| 396 | NO_MORE_BUFFERS = 0x80000001, |
| 397 | STOPPED = 1 |
| 398 | }; |
| 399 | |
| 400 | status_t obtainBuffer(Buffer* audioBuffer, int32_t waitCount); |
| 401 | void releaseBuffer(Buffer* audioBuffer); |
| 402 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | /* As a convenience we provide a write() interface to the audio buffer. |
| 404 | * This is implemented on top of lockBuffer/unlockBuffer. For best |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 405 | * performance use callbacks. Return actual number of bytes written. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | * |
| 407 | */ |
| 408 | ssize_t write(const void* buffer, size_t size); |
| 409 | |
| 410 | /* |
| 411 | * Dumps the state of an audio track. |
| 412 | */ |
| 413 | status_t dump(int fd, const Vector<String16>& args) const; |
| 414 | |
| 415 | private: |
| 416 | /* copying audio tracks is not allowed */ |
| 417 | AudioTrack(const AudioTrack& other); |
| 418 | AudioTrack& operator = (const AudioTrack& other); |
| 419 | |
| 420 | /* a small internal class to handle the callback */ |
| 421 | class AudioTrackThread : public Thread |
| 422 | { |
| 423 | public: |
| 424 | AudioTrackThread(AudioTrack& receiver, bool bCanCallJava = false); |
| 425 | private: |
| 426 | friend class AudioTrack; |
| 427 | virtual bool threadLoop(); |
| 428 | virtual status_t readyToRun(); |
| 429 | virtual void onFirstRef(); |
| 430 | AudioTrack& mReceiver; |
| 431 | Mutex mLock; |
| 432 | }; |
| 433 | |
| 434 | bool processAudioBuffer(const sp<AudioTrackThread>& thread); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 435 | status_t createTrack_l(int streamType, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 436 | uint32_t sampleRate, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 437 | uint32_t format, |
| 438 | uint32_t channelMask, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 439 | int frameCount, |
| 440 | uint32_t flags, |
| 441 | const sp<IMemory>& sharedBuffer, |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 442 | audio_io_handle_t output, |
| 443 | bool enforceFrameCount); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 444 | void flush_l(); |
| 445 | status_t setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount); |
| 446 | audio_io_handle_t getOutput_l(); |
| 447 | status_t restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart); |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame^] | 448 | bool stopped_l() const { return !mActive; } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | sp<IAudioTrack> mAudioTrack; |
| 451 | sp<IMemory> mCblkMemory; |
| 452 | sp<AudioTrackThread> mAudioTrackThread; |
| 453 | |
| 454 | float mVolume[2]; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 455 | float mSendLevel; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | uint32_t mFrameCount; |
| 457 | |
| 458 | audio_track_cblk_t* mCblk; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 459 | uint32_t mFormat; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | uint8_t mStreamType; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | uint8_t mChannelCount; |
| 462 | uint8_t mMuted; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 463 | uint8_t mReserved; |
| 464 | uint32_t mChannelMask; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | status_t mStatus; |
| 466 | uint32_t mLatency; |
| 467 | |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame^] | 468 | bool mActive; // protected by mLock |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | |
| 470 | callback_t mCbf; |
| 471 | void* mUserData; |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 472 | uint32_t mNotificationFramesReq; // requested number of frames between each notification callback |
| 473 | uint32_t mNotificationFramesAct; // actual number of frames between each notification callback |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | sp<IMemory> mSharedBuffer; |
| 475 | int mLoopCount; |
| 476 | uint32_t mRemainingFrames; |
| 477 | uint32_t mMarkerPosition; |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 478 | bool mMarkerReached; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | uint32_t mNewPosition; |
| 480 | uint32_t mUpdatePeriod; |
Jean-Michel Trivi | cd07594 | 2011-08-25 16:47:23 -0700 | [diff] [blame] | 481 | bool mFlushed; // FIXME will be made obsolete by making flush() synchronous |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 482 | uint32_t mFlags; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 483 | int mSessionId; |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 484 | int mAuxEffectId; |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame^] | 485 | mutable Mutex mLock; |
Eric Laurent | cfe2ba6 | 2011-09-13 15:04:17 -0700 | [diff] [blame] | 486 | status_t mRestoreStatus; |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 487 | int mPreviousPriority; // before start() |
| 488 | int mPreviousSchedulingGroup; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | }; |
| 490 | |
| 491 | |
| 492 | }; // namespace android |
| 493 | |
| 494 | #endif // ANDROID_AUDIOTRACK_H |