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 | |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 61 | /* Client should declare Buffer on the stack and pass address to obtainBuffer() |
| 62 | * and releaseBuffer(). See also callback_t for EVENT_MORE_DATA. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | */ |
| 64 | |
| 65 | class Buffer |
| 66 | { |
| 67 | public: |
| 68 | enum { |
| 69 | MUTE = 0x00000001 |
| 70 | }; |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 71 | uint32_t flags; // 0 or MUTE |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 72 | audio_format_t format; // but AUDIO_FORMAT_PCM_8_BIT -> AUDIO_FORMAT_PCM_16_BIT |
| 73 | // accessed directly by WebKit ANP callback |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 74 | int channelCount; // will be removed in the future, do not use |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 75 | |
| 76 | size_t frameCount; // number of sample frames corresponding to size; |
| 77 | // on input it is the number of frames desired, |
| 78 | // on output is the number of frames actually filled |
| 79 | |
| 80 | size_t size; // input/output in byte units |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | union { |
| 82 | void* raw; |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 83 | short* i16; // signed 16-bit |
| 84 | int8_t* i8; // unsigned 8-bit, offset by 0x80 |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | }; |
| 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 Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 91 | * invokes the callback when a new buffer becomes available or various conditions occur. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | * 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 Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 98 | * more bytes than indicated by 'size' field and update 'size' if fewer bytes are |
| 99 | * written. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | * - EVENT_UNDERRUN: unused. |
| 101 | * - 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] | 102 | * - 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 Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | * - EVENT_BUFFER_END: unused. |
| 105 | */ |
| 106 | |
Glenn Kasten | d217a8c | 2011-06-01 15:20:35 -0700 | [diff] [blame] | 107 | 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] | 108 | |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 109 | /* Returns the minimum frame count required for the successful creation of |
| 110 | * an AudioTrack object. |
| 111 | * Returned status (from utils/Errors.h) can be: |
| 112 | * - NO_ERROR: successful operation |
| 113 | * - NO_INIT: audio server or audio hardware not initialized |
| 114 | */ |
| 115 | |
| 116 | static status_t getMinFrameCount(int* frameCount, |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 117 | audio_stream_type_t streamType = AUDIO_STREAM_DEFAULT, |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 118 | uint32_t sampleRate = 0); |
| 119 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | /* Constructs an uninitialized AudioTrack. No connection with |
| 121 | * AudioFlinger takes place. |
| 122 | */ |
| 123 | AudioTrack(); |
| 124 | |
| 125 | /* Creates an audio track and registers it with AudioFlinger. |
| 126 | * Once created, the track needs to be started before it can be used. |
| 127 | * Unspecified values are set to the audio hardware's current |
| 128 | * values. |
| 129 | * |
| 130 | * Parameters: |
| 131 | * |
| 132 | * streamType: Select the type of audio stream this track is attached to |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 133 | * (e.g. AUDIO_STREAM_MUSIC). |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | * sampleRate: Track sampling rate in Hz. |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 135 | * 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] | 136 | * 16 bits per sample). |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 137 | * channelMask: Channel mask: see audio_channels_t. |
Eric Laurent | d8d6185 | 2012-03-05 17:06:40 -0800 | [diff] [blame] | 138 | * frameCount: Minimum size of track PCM buffer in frames. This defines the |
| 139 | * latency of the track. The actual size selected by the AudioTrack could be |
| 140 | * larger if the requested size is not compatible with current audio HAL |
| 141 | * latency. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | * flags: Reserved for future use. |
| 143 | * cbf: Callback function. If not null, this function is called periodically |
| 144 | * to request new PCM data. |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 145 | * user: Context for use by the callback receiver. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | * notificationFrames: The callback function is called each time notificationFrames PCM |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 147 | * frames have been consumed from track input buffer. |
| 148 | * sessionId: Specific session ID, or zero to use default. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | */ |
| 150 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 151 | AudioTrack( audio_stream_type_t streamType, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | uint32_t sampleRate = 0, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 153 | audio_format_t format = AUDIO_FORMAT_DEFAULT, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 154 | int channelMask = 0, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | int frameCount = 0, |
Glenn Kasten | 18868c5 | 2012-03-07 09:15:37 -0800 | [diff] [blame] | 156 | audio_policy_output_flags_t flags = AUDIO_POLICY_OUTPUT_FLAG_NONE, |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 157 | callback_t cbf = NULL, |
| 158 | void* user = NULL, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 159 | int notificationFrames = 0, |
| 160 | int sessionId = 0); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | |
Andreas Huber | c813985 | 2012-01-18 10:51:55 -0800 | [diff] [blame] | 162 | // DEPRECATED |
| 163 | explicit AudioTrack( int streamType, |
| 164 | uint32_t sampleRate = 0, |
| 165 | int format = AUDIO_FORMAT_DEFAULT, |
| 166 | int channelMask = 0, |
| 167 | int frameCount = 0, |
Glenn Kasten | 18868c5 | 2012-03-07 09:15:37 -0800 | [diff] [blame] | 168 | uint32_t flags = (uint32_t) AUDIO_POLICY_OUTPUT_FLAG_NONE, |
Andreas Huber | c813985 | 2012-01-18 10:51:55 -0800 | [diff] [blame] | 169 | callback_t cbf = 0, |
| 170 | void* user = 0, |
| 171 | int notificationFrames = 0, |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame^] | 172 | int sessionId = 0); |
Andreas Huber | c813985 | 2012-01-18 10:51:55 -0800 | [diff] [blame] | 173 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | /* Creates an audio track and registers it with AudioFlinger. With this constructor, |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 175 | * 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] | 176 | * identified by the argument sharedBuffer. This prototype is for static buffer playback. |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 177 | * PCM data must be present in memory before the AudioTrack is started. |
| 178 | * The write() and flush() methods are not supported in this case. |
| 179 | * 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] | 180 | * EVENT_UNDERRUN event. |
| 181 | */ |
| 182 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 183 | AudioTrack( audio_stream_type_t streamType, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | uint32_t sampleRate = 0, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 185 | audio_format_t format = AUDIO_FORMAT_DEFAULT, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 186 | int channelMask = 0, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | const sp<IMemory>& sharedBuffer = 0, |
Glenn Kasten | 18868c5 | 2012-03-07 09:15:37 -0800 | [diff] [blame] | 188 | audio_policy_output_flags_t flags = AUDIO_POLICY_OUTPUT_FLAG_NONE, |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 189 | callback_t cbf = NULL, |
| 190 | void* user = NULL, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 191 | int notificationFrames = 0, |
| 192 | int sessionId = 0); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | |
| 194 | /* Terminates the AudioTrack and unregisters it from AudioFlinger. |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 195 | * Also destroys all resources associated with the AudioTrack. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | */ |
| 197 | ~AudioTrack(); |
| 198 | |
| 199 | |
| 200 | /* Initialize an uninitialized AudioTrack. |
| 201 | * Returned status (from utils/Errors.h) can be: |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 202 | * - NO_ERROR: successful initialization |
| 203 | * - INVALID_OPERATION: AudioTrack is already initialized |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 204 | * - BAD_VALUE: invalid parameter (channels, format, sampleRate...) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | * - NO_INIT: audio server or audio hardware not initialized |
| 206 | * */ |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 207 | status_t set(audio_stream_type_t streamType = AUDIO_STREAM_DEFAULT, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | uint32_t sampleRate = 0, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 209 | audio_format_t format = AUDIO_FORMAT_DEFAULT, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 210 | int channelMask = 0, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | int frameCount = 0, |
Glenn Kasten | 18868c5 | 2012-03-07 09:15:37 -0800 | [diff] [blame] | 212 | audio_policy_output_flags_t flags = AUDIO_POLICY_OUTPUT_FLAG_NONE, |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 213 | callback_t cbf = NULL, |
| 214 | void* user = NULL, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | int notificationFrames = 0, |
| 216 | const sp<IMemory>& sharedBuffer = 0, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 217 | bool threadCanCallJava = false, |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame^] | 218 | int sessionId = 0); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | |
| 220 | |
| 221 | /* Result of constructing the AudioTrack. This must be checked |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 222 | * 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] | 223 | * an uninitialized AudioTrack produces undefined results. |
| 224 | * See set() method above for possible return codes. |
| 225 | */ |
| 226 | status_t initCheck() const; |
| 227 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 228 | /* Returns this track's estimated latency in milliseconds. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | * This includes the latency due to AudioTrack buffer size, AudioMixer (if any) |
| 230 | * and audio hardware driver. |
| 231 | */ |
| 232 | uint32_t latency() const; |
| 233 | |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 234 | /* getters, see constructors and set() */ |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 236 | audio_stream_type_t streamType() const; |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 237 | audio_format_t format() const; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | int channelCount() const; |
| 239 | uint32_t frameCount() const; |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 240 | |
| 241 | /* Return channelCount * (bit depth per channel / 8). |
| 242 | * channelCount is determined from channelMask, and bit depth comes from format. |
| 243 | */ |
| 244 | size_t frameSize() const; |
| 245 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | sp<IMemory>& sharedBuffer(); |
| 247 | |
| 248 | |
| 249 | /* After it's created the track is not active. Call start() to |
| 250 | * make it active. If set, the callback will start being called. |
| 251 | */ |
| 252 | void start(); |
| 253 | |
| 254 | /* Stop a track. If set, the callback will cease being called and |
| 255 | * obtainBuffer returns STOPPED. Note that obtainBuffer() still works |
| 256 | * and will fill up buffers until the pool is exhausted. |
| 257 | */ |
| 258 | void stop(); |
| 259 | bool stopped() const; |
| 260 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 261 | /* Flush a stopped track. All pending buffers are discarded. |
| 262 | * 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] | 263 | */ |
| 264 | void flush(); |
| 265 | |
| 266 | /* Pause a track. If set, the callback will cease being called and |
| 267 | * obtainBuffer returns STOPPED. Note that obtainBuffer() still works |
| 268 | * and will fill up buffers until the pool is exhausted. |
| 269 | */ |
| 270 | void pause(); |
| 271 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 272 | /* Mute or unmute this track. |
| 273 | * While muted, the callback, if set, is still called. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | */ |
| 275 | void mute(bool); |
| 276 | bool muted() const; |
| 277 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 278 | /* Set volume for this track, mostly used for games' sound effects |
| 279 | * 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] | 280 | */ |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 281 | status_t setVolume(float left, float right); |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 282 | void getVolume(float* left, float* right) const; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 284 | /* Set the send level for this track. An auxiliary effect should be attached |
| 285 | * 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] | 286 | */ |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 287 | status_t setAuxEffectSendLevel(float level); |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 288 | void getAuxEffectSendLevel(float* level) const; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 289 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 290 | /* 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] | 291 | */ |
Eric Laurent | 5732662 | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 292 | status_t setSampleRate(int sampleRate); |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 293 | uint32_t getSampleRate() const; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | |
| 295 | /* Enables looping and sets the start and end points of looping. |
| 296 | * |
| 297 | * Parameters: |
| 298 | * |
| 299 | * loopStart: loop start expressed as the number of PCM frames played since AudioTrack start. |
| 300 | * 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] | 301 | * loopCount: number of loops to execute. Calling setLoop() with loopCount == 0 cancels any |
| 302 | * pending or active loop. loopCount = -1 means infinite looping. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | * |
| 304 | * For proper operation the following condition must be respected: |
| 305 | * (loopEnd-loopStart) <= framecount() |
| 306 | */ |
| 307 | status_t setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 309 | /* Sets marker position. When playback reaches the number of frames specified, a callback with |
| 310 | * event type EVENT_MARKER is called. Calling setMarkerPosition with marker == 0 cancels marker |
| 311 | * notification callback. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | * If the AudioTrack has been opened with no callback function associated, the operation will fail. |
| 313 | * |
| 314 | * Parameters: |
| 315 | * |
| 316 | * marker: marker position expressed in frames. |
| 317 | * |
| 318 | * Returned status (from utils/Errors.h) can be: |
| 319 | * - NO_ERROR: successful operation |
| 320 | * - INVALID_OPERATION: the AudioTrack has no callback installed. |
| 321 | */ |
| 322 | status_t setMarkerPosition(uint32_t marker); |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 323 | status_t getMarkerPosition(uint32_t *marker) const; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | |
| 325 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 326 | /* Sets position update period. Every time the number of frames specified has been played, |
| 327 | * a callback with event type EVENT_NEW_POS is called. |
| 328 | * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification |
| 329 | * callback. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | * If the AudioTrack has been opened with no callback function associated, the operation will fail. |
| 331 | * |
| 332 | * Parameters: |
| 333 | * |
| 334 | * updatePeriod: position update notification period expressed in frames. |
| 335 | * |
| 336 | * Returned status (from utils/Errors.h) can be: |
| 337 | * - NO_ERROR: successful operation |
| 338 | * - INVALID_OPERATION: the AudioTrack has no callback installed. |
| 339 | */ |
| 340 | status_t setPositionUpdatePeriod(uint32_t updatePeriod); |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 341 | status_t getPositionUpdatePeriod(uint32_t *updatePeriod) const; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | /* Sets playback head position within AudioTrack buffer. The new position is specified |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 344 | * in number of frames. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | * 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] | 346 | * Note that the actual position set is <position> modulo the AudioTrack buffer size in frames. |
| 347 | * 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] | 348 | * as opposed to streaming. |
| 349 | * The getPosition() method on the other hand returns the total number of frames played since |
| 350 | * playback start. |
| 351 | * |
| 352 | * Parameters: |
| 353 | * |
| 354 | * position: New playback head position within AudioTrack buffer. |
| 355 | * |
| 356 | * Returned status (from utils/Errors.h) can be: |
| 357 | * - NO_ERROR: successful operation |
| 358 | * - INVALID_OPERATION: the AudioTrack is not stopped. |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 359 | * - 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] | 360 | */ |
| 361 | status_t setPosition(uint32_t position); |
| 362 | status_t getPosition(uint32_t *position); |
| 363 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 364 | /* 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] | 365 | * rewriting the buffer before restarting playback after a stop. |
| 366 | * This method must be called with the AudioTrack in paused or stopped state. |
| 367 | * |
| 368 | * Returned status (from utils/Errors.h) can be: |
| 369 | * - NO_ERROR: successful operation |
| 370 | * - INVALID_OPERATION: the AudioTrack is not stopped. |
| 371 | */ |
| 372 | status_t reload(); |
| 373 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 374 | /* Returns a handle on the audio output used by this AudioTrack. |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 375 | * |
| 376 | * Parameters: |
| 377 | * none. |
| 378 | * |
| 379 | * Returned value: |
| 380 | * handle on audio hardware output |
| 381 | */ |
| 382 | audio_io_handle_t getOutput(); |
| 383 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 384 | /* Returns the unique session ID associated with this track. |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 385 | * |
| 386 | * Parameters: |
| 387 | * none. |
| 388 | * |
| 389 | * Returned value: |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 390 | * AudioTrack session ID. |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 391 | */ |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 392 | int getSessionId() const; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 393 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 394 | /* Attach track auxiliary output to specified effect. Use effectId = 0 |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 395 | * to detach track from effect. |
| 396 | * |
| 397 | * Parameters: |
| 398 | * |
| 399 | * effectId: effectId obtained from AudioEffect::id(). |
| 400 | * |
| 401 | * Returned status (from utils/Errors.h) can be: |
| 402 | * - NO_ERROR: successful operation |
| 403 | * - INVALID_OPERATION: the effect is not an auxiliary effect. |
| 404 | * - BAD_VALUE: The specified effect ID is invalid |
| 405 | */ |
| 406 | status_t attachAuxEffect(int effectId); |
| 407 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 408 | /* Obtains a buffer of "frameCount" frames. The buffer must be |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 409 | * filled entirely, and then released with releaseBuffer(). |
| 410 | * If the track is stopped, obtainBuffer() returns |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 411 | * 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] | 412 | * at which point NO_MORE_BUFFERS is returned. |
| 413 | * Buffers will be returned until the pool (buffercount()) |
| 414 | * is exhausted, at which point obtainBuffer() will either block |
| 415 | * or return WOULD_BLOCK depending on the value of the "blocking" |
| 416 | * parameter. |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 417 | * |
| 418 | * Interpretation of waitCount: |
| 419 | * +n limits wait time to n * WAIT_PERIOD_MS, |
| 420 | * -1 causes an (almost) infinite wait time, |
| 421 | * 0 non-blocking. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | */ |
| 423 | |
| 424 | enum { |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 425 | NO_MORE_BUFFERS = 0x80000001, // same name in AudioFlinger.h, ok to be different value |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | STOPPED = 1 |
| 427 | }; |
| 428 | |
| 429 | status_t obtainBuffer(Buffer* audioBuffer, int32_t waitCount); |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 430 | |
| 431 | /* Release a filled buffer of "frameCount" frames for AudioFlinger to process. */ |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | void releaseBuffer(Buffer* audioBuffer); |
| 433 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | /* As a convenience we provide a write() interface to the audio buffer. |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 435 | * This is implemented on top of obtainBuffer/releaseBuffer. For best |
| 436 | * performance use callbacks. Returns actual number of bytes written >= 0, |
| 437 | * or one of the following negative status codes: |
| 438 | * INVALID_OPERATION AudioTrack is configured for shared buffer mode |
| 439 | * BAD_VALUE size is invalid |
| 440 | * STOPPED AudioTrack was stopped during the write |
| 441 | * NO_MORE_BUFFERS when obtainBuffer() returns same |
| 442 | * or any other error code returned by IAudioTrack::start() or restoreTrack_l(). |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | */ |
| 444 | ssize_t write(const void* buffer, size_t size); |
| 445 | |
| 446 | /* |
| 447 | * Dumps the state of an audio track. |
| 448 | */ |
| 449 | status_t dump(int fd, const Vector<String16>& args) const; |
| 450 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 451 | protected: |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | /* copying audio tracks is not allowed */ |
| 453 | AudioTrack(const AudioTrack& other); |
| 454 | AudioTrack& operator = (const AudioTrack& other); |
| 455 | |
| 456 | /* a small internal class to handle the callback */ |
| 457 | class AudioTrackThread : public Thread |
| 458 | { |
| 459 | public: |
| 460 | AudioTrackThread(AudioTrack& receiver, bool bCanCallJava = false); |
| 461 | private: |
| 462 | friend class AudioTrack; |
| 463 | virtual bool threadLoop(); |
| 464 | virtual status_t readyToRun(); |
| 465 | virtual void onFirstRef(); |
| 466 | AudioTrack& mReceiver; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | }; |
| 468 | |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 469 | // body of AudioTrackThread::threadLoop() |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | bool processAudioBuffer(const sp<AudioTrackThread>& thread); |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame^] | 471 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 472 | status_t createTrack_l(audio_stream_type_t streamType, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 473 | uint32_t sampleRate, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 474 | audio_format_t format, |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 475 | uint32_t channelMask, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 476 | int frameCount, |
Glenn Kasten | 18868c5 | 2012-03-07 09:15:37 -0800 | [diff] [blame] | 477 | audio_policy_output_flags_t flags, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 478 | const sp<IMemory>& sharedBuffer, |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 479 | audio_io_handle_t output, |
| 480 | bool enforceFrameCount); |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 481 | void flush_l(); |
| 482 | status_t setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount); |
| 483 | audio_io_handle_t getOutput_l(); |
| 484 | status_t restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart); |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 485 | bool stopped_l() const { return !mActive; } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | sp<IAudioTrack> mAudioTrack; |
| 488 | sp<IMemory> mCblkMemory; |
| 489 | sp<AudioTrackThread> mAudioTrackThread; |
| 490 | |
| 491 | float mVolume[2]; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 492 | float mSendLevel; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | uint32_t mFrameCount; |
| 494 | |
| 495 | audio_track_cblk_t* mCblk; |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 496 | audio_format_t mFormat; |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 497 | audio_stream_type_t mStreamType; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | uint8_t mChannelCount; |
| 499 | uint8_t mMuted; |
Jean-Michel Trivi | 0d255b2 | 2011-05-24 15:53:33 -0700 | [diff] [blame] | 500 | uint8_t mReserved; |
| 501 | uint32_t mChannelMask; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | status_t mStatus; |
| 503 | uint32_t mLatency; |
| 504 | |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 505 | bool mActive; // protected by mLock |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 507 | callback_t mCbf; // callback handler for events, or NULL |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | void* mUserData; |
Eric Laurent | d1b449a | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 509 | uint32_t mNotificationFramesReq; // requested number of frames between each notification callback |
| 510 | 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] | 511 | sp<IMemory> mSharedBuffer; |
| 512 | int mLoopCount; |
| 513 | uint32_t mRemainingFrames; |
| 514 | uint32_t mMarkerPosition; |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 515 | bool mMarkerReached; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | uint32_t mNewPosition; |
| 517 | uint32_t mUpdatePeriod; |
Jean-Michel Trivi | cd07594 | 2011-08-25 16:47:23 -0700 | [diff] [blame] | 518 | bool mFlushed; // FIXME will be made obsolete by making flush() synchronous |
Glenn Kasten | 18868c5 | 2012-03-07 09:15:37 -0800 | [diff] [blame] | 519 | audio_policy_output_flags_t mFlags; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 520 | int mSessionId; |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 521 | int mAuxEffectId; |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 522 | mutable Mutex mLock; |
Eric Laurent | cfe2ba6 | 2011-09-13 15:04:17 -0700 | [diff] [blame] | 523 | status_t mRestoreStatus; |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 524 | bool mIsTimed; |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 525 | int mPreviousPriority; // before start() |
| 526 | int mPreviousSchedulingGroup; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 527 | }; |
| 528 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 529 | class TimedAudioTrack : public AudioTrack |
| 530 | { |
| 531 | public: |
| 532 | TimedAudioTrack(); |
| 533 | |
| 534 | /* allocate a shared memory buffer that can be passed to queueTimedBuffer */ |
| 535 | status_t allocateTimedBuffer(size_t size, sp<IMemory>* buffer); |
| 536 | |
| 537 | /* queue a buffer obtained via allocateTimedBuffer for playback at the |
| 538 | given timestamp. PTS units a microseconds on the media time timeline. |
| 539 | The media time transform (set with setMediaTimeTransform) set by the |
| 540 | audio producer will handle converting from media time to local time |
| 541 | (perhaps going through the common time timeline in the case of |
| 542 | synchronized multiroom audio case) */ |
| 543 | status_t queueTimedBuffer(const sp<IMemory>& buffer, int64_t pts); |
| 544 | |
| 545 | /* define a transform between media time and either common time or |
| 546 | local time */ |
| 547 | enum TargetTimeline {LOCAL_TIME, COMMON_TIME}; |
| 548 | status_t setMediaTimeTransform(const LinearTransform& xform, |
| 549 | TargetTimeline target); |
| 550 | }; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 | |
| 552 | }; // namespace android |
| 553 | |
| 554 | #endif // ANDROID_AUDIOTRACK_H |