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