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> |
Glenn Kasten | a636433 | 2012-04-19 09:35:04 -0700 | [diff] [blame] | 31 | #include <cutils/sched_policy.h> |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | #include <utils/threads.h> |
| 33 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | namespace android { |
| 35 | |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | |
| 38 | class audio_track_cblk_t; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame^] | 39 | class AudioTrackClientProxy; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
| 41 | // ---------------------------------------------------------------------------- |
| 42 | |
Glenn Kasten | b68a91a | 2011-11-15 13:55:13 -0800 | [diff] [blame] | 43 | class AudioTrack : virtual public RefBase |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | { |
| 45 | public: |
| 46 | enum channel_index { |
| 47 | MONO = 0, |
| 48 | LEFT = 0, |
| 49 | RIGHT = 1 |
| 50 | }; |
| 51 | |
| 52 | /* Events used by AudioTrack callback function (audio_track_cblk_t). |
Glenn Kasten | ad2f6db | 2012-11-01 15:45:06 -0700 | [diff] [blame] | 53 | * Keep in sync with frameworks/base/media/java/android/media/AudioTrack.java NATIVE_EVENT_*. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | */ |
| 55 | enum event_type { |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 56 | EVENT_MORE_DATA = 0, // Request to write more data to buffer. |
| 57 | // If this event is delivered but the callback handler |
| 58 | // does not want to write more data, the handler must explicitly |
| 59 | // ignore the event by setting frameCount to zero. |
| 60 | EVENT_UNDERRUN = 1, // Buffer underrun occurred. |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 61 | EVENT_LOOP_END = 2, // Sample loop end was reached; playback restarted from |
| 62 | // loop start if loop count was not 0. |
| 63 | EVENT_MARKER = 3, // Playback head is at the specified marker position |
| 64 | // (See setMarkerPosition()). |
| 65 | EVENT_NEW_POS = 4, // Playback head is at a new position |
| 66 | // (See setPositionUpdatePeriod()). |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | EVENT_BUFFER_END = 5 // Playback head is at the end of the buffer. |
| 68 | }; |
| 69 | |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 70 | /* Client should declare Buffer on the stack and pass address to obtainBuffer() |
| 71 | * and releaseBuffer(). See also callback_t for EVENT_MORE_DATA. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | */ |
| 73 | |
| 74 | class Buffer |
| 75 | { |
| 76 | public: |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 77 | size_t frameCount; // number of sample frames corresponding to size; |
| 78 | // on input it is the number of frames desired, |
| 79 | // on output is the number of frames actually filled |
| 80 | |
| 81 | size_t size; // input/output in byte units |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | union { |
| 83 | void* raw; |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 84 | short* i16; // signed 16-bit |
| 85 | int8_t* i8; // unsigned 8-bit, offset by 0x80 |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | }; |
| 87 | }; |
| 88 | |
| 89 | |
| 90 | /* As a convenience, if a callback is supplied, a handler thread |
| 91 | * is automatically created with the appropriate priority. This thread |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 92 | * 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] | 93 | * Parameters: |
| 94 | * |
| 95 | * event: type of event notified (see enum AudioTrack::event_type). |
| 96 | * user: Pointer to context for use by the callback receiver. |
| 97 | * info: Pointer to optional parameter according to event type: |
| 98 | * - EVENT_MORE_DATA: pointer to AudioTrack::Buffer struct. The callback must not write |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 99 | * more bytes than indicated by 'size' field and update 'size' if fewer bytes are |
| 100 | * written. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | * - EVENT_UNDERRUN: unused. |
| 102 | * - 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] | 103 | * - EVENT_MARKER: pointer to an uint32_t containing the marker position in frames. |
| 104 | * - EVENT_NEW_POS: pointer to an uint32_t containing the new position in frames. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | * - EVENT_BUFFER_END: unused. |
| 106 | */ |
| 107 | |
Glenn Kasten | d217a8c | 2011-06-01 15:20:35 -0700 | [diff] [blame] | 108 | 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] | 109 | |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 110 | /* Returns the minimum frame count required for the successful creation of |
| 111 | * an AudioTrack object. |
| 112 | * Returned status (from utils/Errors.h) can be: |
| 113 | * - NO_ERROR: successful operation |
| 114 | * - NO_INIT: audio server or audio hardware not initialized |
| 115 | */ |
| 116 | |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 117 | static status_t getMinFrameCount(size_t* frameCount, |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 118 | audio_stream_type_t streamType = AUDIO_STREAM_DEFAULT, |
Chia-chi Yeh | 33005a9 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 119 | uint32_t sampleRate = 0); |
| 120 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | /* Constructs an uninitialized AudioTrack. No connection with |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 122 | * AudioFlinger takes place. Use set() after this. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | */ |
| 124 | AudioTrack(); |
| 125 | |
Glenn Kasten | ad2f6db | 2012-11-01 15:45:06 -0700 | [diff] [blame] | 126 | /* Creates an AudioTrack object and registers it with AudioFlinger. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | * Once created, the track needs to be started before it can be used. |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 128 | * Unspecified values are set to appropriate default values. |
| 129 | * With this constructor, the track is configured for streaming mode. |
| 130 | * Data to be rendered is supplied by write() or by the callback EVENT_MORE_DATA. |
| 131 | * Intermixing a combination of write() and non-ignored EVENT_MORE_DATA is deprecated. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | * |
| 133 | * Parameters: |
| 134 | * |
| 135 | * streamType: Select the type of audio stream this track is attached to |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 136 | * (e.g. AUDIO_STREAM_MUSIC). |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | * sampleRate: Track sampling rate in Hz. |
Dima Zavin | fce7a47 | 2011-04-19 22:30:36 -0700 | [diff] [blame] | 138 | * 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] | 139 | * 16 bits per sample). |
Glenn Kasten | 28b76b3 | 2012-07-03 17:24:41 -0700 | [diff] [blame] | 140 | * channelMask: Channel mask. |
Eric Laurent | d8d6185 | 2012-03-05 17:06:40 -0800 | [diff] [blame] | 141 | * frameCount: Minimum size of track PCM buffer in frames. This defines the |
Glenn Kasten | ad2f6db | 2012-11-01 15:45:06 -0700 | [diff] [blame] | 142 | * application's contribution to the |
Eric Laurent | d8d6185 | 2012-03-05 17:06:40 -0800 | [diff] [blame] | 143 | * latency of the track. The actual size selected by the AudioTrack could be |
| 144 | * larger if the requested size is not compatible with current audio HAL |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 145 | * configuration. Zero means to use a default value. |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 146 | * flags: See comments on audio_output_flags_t in <system/audio.h>. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | * cbf: Callback function. If not null, this function is called periodically |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 148 | * to provide new data and inform of marker, position updates, etc. |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 149 | * user: Context for use by the callback receiver. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | * notificationFrames: The callback function is called each time notificationFrames PCM |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 151 | * frames have been consumed from track input buffer. |
| 152 | * sessionId: Specific session ID, or zero to use default. |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 153 | * threadCanCallJava: Whether callbacks are made from an attached thread and thus can call JNI. |
| 154 | * If not present in parameter list, then fixed at false. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | */ |
| 156 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 157 | AudioTrack( audio_stream_type_t streamType, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | uint32_t sampleRate = 0, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 159 | audio_format_t format = AUDIO_FORMAT_DEFAULT, |
Glenn Kasten | 28b76b3 | 2012-07-03 17:24:41 -0700 | [diff] [blame] | 160 | audio_channel_mask_t channelMask = 0, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | int frameCount = 0, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 162 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE, |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 163 | callback_t cbf = NULL, |
| 164 | void* user = NULL, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 165 | int notificationFrames = 0, |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 166 | int sessionId = 0); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 168 | /* Creates an audio track and registers it with AudioFlinger. |
| 169 | * With this constructor, the track is configured for static buffer mode. |
| 170 | * The format must not be 8-bit linear PCM. |
| 171 | * Data to be rendered is passed in a shared memory buffer |
| 172 | * identified by the argument sharedBuffer, which must be non-0. |
| 173 | * The memory should be initialized to the desired data before calling start(). |
Glenn Kasten | 4bae364 | 2012-11-30 13:41:12 -0800 | [diff] [blame] | 174 | * The write() method is not supported in this case. |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 175 | * 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] | 176 | * EVENT_UNDERRUN event. |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 177 | * FIXME EVENT_MORE_DATA still occurs; it must be ignored. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | */ |
| 179 | |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 180 | AudioTrack( audio_stream_type_t streamType, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | uint32_t sampleRate = 0, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 182 | audio_format_t format = AUDIO_FORMAT_DEFAULT, |
Glenn Kasten | 28b76b3 | 2012-07-03 17:24:41 -0700 | [diff] [blame] | 183 | audio_channel_mask_t channelMask = 0, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | const sp<IMemory>& sharedBuffer = 0, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 185 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE, |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 186 | callback_t cbf = NULL, |
| 187 | void* user = NULL, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 188 | int notificationFrames = 0, |
Glenn Kasten | 4a4a095 | 2012-03-19 11:38:14 -0700 | [diff] [blame] | 189 | int sessionId = 0); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | |
| 191 | /* Terminates the AudioTrack and unregisters it from AudioFlinger. |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 192 | * Also destroys all resources associated with the AudioTrack. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | */ |
| 194 | ~AudioTrack(); |
| 195 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | /* Initialize an uninitialized AudioTrack. |
| 197 | * Returned status (from utils/Errors.h) can be: |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 198 | * - NO_ERROR: successful initialization |
| 199 | * - INVALID_OPERATION: AudioTrack is already initialized |
Glenn Kasten | 28b76b3 | 2012-07-03 17:24:41 -0700 | [diff] [blame] | 200 | * - BAD_VALUE: invalid parameter (channelMask, format, sampleRate...) |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | * - NO_INIT: audio server or audio hardware not initialized |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 202 | * If sharedBuffer is non-0, the frameCount parameter is ignored and |
| 203 | * replaced by the shared buffer's total allocated size in frame units. |
Glenn Kasten | ad2f6db | 2012-11-01 15:45:06 -0700 | [diff] [blame] | 204 | */ |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 205 | 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] | 206 | uint32_t sampleRate = 0, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 207 | audio_format_t format = AUDIO_FORMAT_DEFAULT, |
Glenn Kasten | 28b76b3 | 2012-07-03 17:24:41 -0700 | [diff] [blame] | 208 | audio_channel_mask_t channelMask = 0, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | int frameCount = 0, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 210 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE, |
Glenn Kasten | a0d6833 | 2012-01-27 16:47:15 -0800 | [diff] [blame] | 211 | callback_t cbf = NULL, |
| 212 | void* user = NULL, |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | int notificationFrames = 0, |
| 214 | const sp<IMemory>& sharedBuffer = 0, |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 215 | bool threadCanCallJava = false, |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 216 | int sessionId = 0); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | /* Result of constructing the AudioTrack. This must be checked |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 219 | * 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] | 220 | * an uninitialized AudioTrack produces undefined results. |
| 221 | * See set() method above for possible return codes. |
| 222 | */ |
Glenn Kasten | 01437b7 | 2012-11-29 07:32:49 -0800 | [diff] [blame] | 223 | status_t initCheck() const { return mStatus; } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 225 | /* Returns this track's estimated latency in milliseconds. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | * This includes the latency due to AudioTrack buffer size, AudioMixer (if any) |
| 227 | * and audio hardware driver. |
| 228 | */ |
Glenn Kasten | 01437b7 | 2012-11-29 07:32:49 -0800 | [diff] [blame] | 229 | uint32_t latency() const { return mLatency; } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 231 | /* getters, see constructors and set() */ |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | |
Glenn Kasten | 01437b7 | 2012-11-29 07:32:49 -0800 | [diff] [blame] | 233 | audio_stream_type_t streamType() const { return mStreamType; } |
| 234 | audio_format_t format() const { return mFormat; } |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 235 | |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 236 | /* Return frame size in bytes, which for linear PCM is channelCount * (bit depth per channel / 8). |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 237 | * channelCount is determined from channelMask, and bit depth comes from format. |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 238 | * For non-linear formats, the frame size is typically 1 byte. |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 239 | */ |
Glenn Kasten | 01437b7 | 2012-11-29 07:32:49 -0800 | [diff] [blame] | 240 | uint32_t channelCount() const { return mChannelCount; } |
Glenn Kasten | b998065 | 2012-01-11 09:48:27 -0800 | [diff] [blame] | 241 | |
Glenn Kasten | 01437b7 | 2012-11-29 07:32:49 -0800 | [diff] [blame] | 242 | uint32_t frameCount() const { return mFrameCount; } |
| 243 | size_t frameSize() const { return mFrameSize; } |
| 244 | |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 245 | /* Return the static buffer specified in constructor or set(), or 0 for streaming mode */ |
Glenn Kasten | 01437b7 | 2012-11-29 07:32:49 -0800 | [diff] [blame] | 246 | sp<IMemory> sharedBuffer() const { return mSharedBuffer; } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | /* After it's created the track is not active. Call start() to |
| 249 | * make it active. If set, the callback will start being called. |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 250 | * If the track was previously paused, volume is ramped up over the first mix buffer. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | */ |
| 252 | void start(); |
| 253 | |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 254 | /* Stop a track. |
| 255 | * In static buffer mode, the track is stopped immediately. |
| 256 | * In streaming mode, the callback will cease being called and |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | * obtainBuffer returns STOPPED. Note that obtainBuffer() still works |
| 258 | * and will fill up buffers until the pool is exhausted. |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 259 | * The stop does not occur immediately: any data remaining in the buffer |
| 260 | * is first drained, mixed, and output, and only then is the track marked as stopped. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | */ |
| 262 | void stop(); |
| 263 | bool stopped() const; |
| 264 | |
Glenn Kasten | 4bae364 | 2012-11-30 13:41:12 -0800 | [diff] [blame] | 265 | /* Flush a stopped or paused track. All previously buffered data is discarded immediately. |
| 266 | * This has the effect of draining the buffers without mixing or output. |
| 267 | * Flush is intended for streaming mode, for example before switching to non-contiguous content. |
| 268 | * This function is a no-op if the track is not stopped or paused, or uses a static buffer. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | */ |
| 270 | void flush(); |
| 271 | |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 272 | /* Pause a track. After pause, the callback will cease being called and |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | * obtainBuffer returns STOPPED. Note that obtainBuffer() still works |
| 274 | * and will fill up buffers until the pool is exhausted. |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 275 | * Volume is ramped down over the next mix buffer following the pause request, |
| 276 | * and then the track is marked as paused. It can be resumed with ramp up by start(). |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | */ |
| 278 | void pause(); |
| 279 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 280 | /* Set volume for this track, mostly used for games' sound effects |
| 281 | * left and right volumes. Levels must be >= 0.0 and <= 1.0. |
Glenn Kasten | b1c0993 | 2012-02-27 16:21:04 -0800 | [diff] [blame] | 282 | * This is the older API. New applications should use setVolume(float) when possible. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | */ |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 284 | status_t setVolume(float left, float right); |
Glenn Kasten | b1c0993 | 2012-02-27 16:21:04 -0800 | [diff] [blame] | 285 | |
| 286 | /* Set volume for all channels. This is the preferred API for new applications, |
| 287 | * especially for multi-channel content. |
| 288 | */ |
| 289 | status_t setVolume(float volume); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 291 | /* Set the send level for this track. An auxiliary effect should be attached |
| 292 | * 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] | 293 | */ |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 294 | status_t setAuxEffectSendLevel(float level); |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 295 | void getAuxEffectSendLevel(float* level) const; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 296 | |
Glenn Kasten | ad2f6db | 2012-11-01 15:45:06 -0700 | [diff] [blame] | 297 | /* Set sample rate for this track in Hz, mostly used for games' sound effects |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | */ |
Glenn Kasten | 3b16c76 | 2012-11-14 08:44:39 -0800 | [diff] [blame] | 299 | status_t setSampleRate(uint32_t sampleRate); |
| 300 | |
| 301 | /* Return current sample rate in Hz, or 0 if unknown */ |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 302 | uint32_t getSampleRate() const; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | |
| 304 | /* Enables looping and sets the start and end points of looping. |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 305 | * Only supported for static buffer mode. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | * |
| 307 | * Parameters: |
| 308 | * |
| 309 | * loopStart: loop start expressed as the number of PCM frames played since AudioTrack start. |
| 310 | * 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] | 311 | * loopCount: number of loops to execute. Calling setLoop() with loopCount == 0 cancels any |
| 312 | * pending or active loop. loopCount = -1 means infinite looping. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | * |
| 314 | * For proper operation the following condition must be respected: |
| 315 | * (loopEnd-loopStart) <= framecount() |
| 316 | */ |
| 317 | 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] | 318 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 319 | /* Sets marker position. When playback reaches the number of frames specified, a callback with |
| 320 | * event type EVENT_MARKER is called. Calling setMarkerPosition with marker == 0 cancels marker |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 321 | * notification callback. To set a marker at a position which would compute as 0, |
| 322 | * a workaround is to the set the marker at a nearby position such as -1 or 1. |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 323 | * If the AudioTrack has been opened with no callback function associated, the operation will |
| 324 | * fail. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | * |
| 326 | * Parameters: |
| 327 | * |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 328 | * marker: marker position expressed in wrapping (overflow) frame units, |
| 329 | * like the return value of getPosition(). |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 setMarkerPosition(uint32_t marker); |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 336 | status_t getMarkerPosition(uint32_t *marker) const; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 338 | /* Sets position update period. Every time the number of frames specified has been played, |
| 339 | * a callback with event type EVENT_NEW_POS is called. |
| 340 | * Calling setPositionUpdatePeriod with updatePeriod == 0 cancels new position notification |
| 341 | * callback. |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 342 | * If the AudioTrack has been opened with no callback function associated, the operation will |
| 343 | * fail. |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 344 | * Extremely small values may be rounded up to a value the implementation can support. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | * |
| 346 | * Parameters: |
| 347 | * |
| 348 | * updatePeriod: position update notification period expressed in frames. |
| 349 | * |
| 350 | * Returned status (from utils/Errors.h) can be: |
| 351 | * - NO_ERROR: successful operation |
| 352 | * - INVALID_OPERATION: the AudioTrack has no callback installed. |
| 353 | */ |
| 354 | status_t setPositionUpdatePeriod(uint32_t updatePeriod); |
Glenn Kasten | a5224f3 | 2012-01-04 12:41:44 -0800 | [diff] [blame] | 355 | status_t getPositionUpdatePeriod(uint32_t *updatePeriod) const; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | /* Sets playback head position within AudioTrack buffer. The new position is specified |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 358 | * in number of frames. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | * 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] | 360 | * Note that the actual position set is <position> modulo the AudioTrack buffer size in frames. |
| 361 | * 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] | 362 | * as opposed to streaming. |
| 363 | * The getPosition() method on the other hand returns the total number of frames played since |
| 364 | * playback start. |
| 365 | * |
| 366 | * Parameters: |
| 367 | * |
| 368 | * position: New playback head position within AudioTrack buffer. |
| 369 | * |
| 370 | * Returned status (from utils/Errors.h) can be: |
| 371 | * - NO_ERROR: successful operation |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 372 | * - INVALID_OPERATION: the AudioTrack is not stopped or paused, or is streaming mode. |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 373 | * - BAD_VALUE: The specified position is beyond the number of frames present in AudioTrack |
| 374 | * buffer |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | */ |
| 376 | status_t setPosition(uint32_t position); |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 377 | |
| 378 | /* Return the total number of frames played since playback start. |
| 379 | * The counter will wrap (overflow) periodically, e.g. every ~27 hours at 44.1 kHz. |
| 380 | * It is reset to zero by flush(), reload(), and stop(). |
| 381 | */ |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | status_t getPosition(uint32_t *position); |
| 383 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 384 | /* 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] | 385 | * rewriting the buffer before restarting playback after a stop. |
| 386 | * This method must be called with the AudioTrack in paused or stopped state. |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 387 | * Not allowed in streaming mode. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | * |
| 389 | * Returned status (from utils/Errors.h) can be: |
| 390 | * - NO_ERROR: successful operation |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 391 | * - INVALID_OPERATION: the AudioTrack is not stopped or paused, or is streaming mode. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | */ |
| 393 | status_t reload(); |
| 394 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 395 | /* Returns a handle on the audio output used by this AudioTrack. |
Eric Laurent | c2f1f07 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 396 | * |
| 397 | * Parameters: |
| 398 | * none. |
| 399 | * |
| 400 | * Returned value: |
| 401 | * handle on audio hardware output |
| 402 | */ |
| 403 | audio_io_handle_t getOutput(); |
| 404 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 405 | /* Returns the unique session ID associated with this track. |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 406 | * |
| 407 | * Parameters: |
| 408 | * none. |
| 409 | * |
| 410 | * Returned value: |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 411 | * AudioTrack session ID. |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 412 | */ |
Glenn Kasten | 01437b7 | 2012-11-29 07:32:49 -0800 | [diff] [blame] | 413 | int getSessionId() const { return mSessionId; } |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 414 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 415 | /* Attach track auxiliary output to specified effect. Use effectId = 0 |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 416 | * to detach track from effect. |
| 417 | * |
| 418 | * Parameters: |
| 419 | * |
| 420 | * effectId: effectId obtained from AudioEffect::id(). |
| 421 | * |
| 422 | * Returned status (from utils/Errors.h) can be: |
| 423 | * - NO_ERROR: successful operation |
| 424 | * - INVALID_OPERATION: the effect is not an auxiliary effect. |
| 425 | * - BAD_VALUE: The specified effect ID is invalid |
| 426 | */ |
| 427 | status_t attachAuxEffect(int effectId); |
| 428 | |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 429 | /* Obtains a buffer of "frameCount" frames. The buffer must be |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 430 | * filled entirely, and then released with releaseBuffer(). |
| 431 | * If the track is stopped, obtainBuffer() returns |
Glenn Kasten | 362c4e6 | 2011-12-14 10:28:06 -0800 | [diff] [blame] | 432 | * 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] | 433 | * at which point NO_MORE_BUFFERS is returned. |
Glenn Kasten | ad2f6db | 2012-11-01 15:45:06 -0700 | [diff] [blame] | 434 | * Buffers will be returned until the pool |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | * is exhausted, at which point obtainBuffer() will either block |
| 436 | * or return WOULD_BLOCK depending on the value of the "blocking" |
| 437 | * parameter. |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 438 | * |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 439 | * obtainBuffer() and releaseBuffer() are deprecated for direct use by applications, |
| 440 | * which should use write() or callback EVENT_MORE_DATA instead. |
| 441 | * |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 442 | * Interpretation of waitCount: |
| 443 | * +n limits wait time to n * WAIT_PERIOD_MS, |
| 444 | * -1 causes an (almost) infinite wait time, |
| 445 | * 0 non-blocking. |
Glenn Kasten | 05d4999 | 2012-11-06 14:25:20 -0800 | [diff] [blame] | 446 | * |
| 447 | * Buffer fields |
| 448 | * On entry: |
| 449 | * frameCount number of frames requested |
| 450 | * After error return: |
| 451 | * frameCount 0 |
| 452 | * size 0 |
Glenn Kasten | 22eb4e2 | 2012-11-07 14:03:00 -0800 | [diff] [blame] | 453 | * raw undefined |
Glenn Kasten | 05d4999 | 2012-11-06 14:25:20 -0800 | [diff] [blame] | 454 | * After successful return: |
| 455 | * frameCount actual number of frames available, <= number requested |
| 456 | * size actual number of bytes available |
| 457 | * raw pointer to the buffer |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | */ |
| 459 | |
| 460 | enum { |
Glenn Kasten | 335787f | 2012-01-20 17:00:00 -0800 | [diff] [blame] | 461 | 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] | 462 | STOPPED = 1 |
| 463 | }; |
| 464 | |
| 465 | status_t obtainBuffer(Buffer* audioBuffer, int32_t waitCount); |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 466 | |
| 467 | /* 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] | 468 | void releaseBuffer(Buffer* audioBuffer); |
| 469 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | /* As a convenience we provide a write() interface to the audio buffer. |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 471 | * This is implemented on top of obtainBuffer/releaseBuffer. For best |
| 472 | * performance use callbacks. Returns actual number of bytes written >= 0, |
| 473 | * or one of the following negative status codes: |
| 474 | * INVALID_OPERATION AudioTrack is configured for shared buffer mode |
| 475 | * BAD_VALUE size is invalid |
| 476 | * STOPPED AudioTrack was stopped during the write |
| 477 | * NO_MORE_BUFFERS when obtainBuffer() returns same |
| 478 | * or any other error code returned by IAudioTrack::start() or restoreTrack_l(). |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 479 | * Not supported for static buffer mode. |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | */ |
| 481 | ssize_t write(const void* buffer, size_t size); |
| 482 | |
| 483 | /* |
| 484 | * Dumps the state of an audio track. |
| 485 | */ |
| 486 | status_t dump(int fd, const Vector<String16>& args) const; |
| 487 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 488 | protected: |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | /* copying audio tracks is not allowed */ |
| 490 | AudioTrack(const AudioTrack& other); |
| 491 | AudioTrack& operator = (const AudioTrack& other); |
| 492 | |
| 493 | /* a small internal class to handle the callback */ |
| 494 | class AudioTrackThread : public Thread |
| 495 | { |
| 496 | public: |
| 497 | AudioTrackThread(AudioTrack& receiver, bool bCanCallJava = false); |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 498 | |
| 499 | // Do not call Thread::requestExitAndWait() without first calling requestExit(). |
| 500 | // Thread::requestExitAndWait() is not virtual, and the implementation doesn't do enough. |
| 501 | virtual void requestExit(); |
| 502 | |
| 503 | void pause(); // suspend thread from execution at next loop boundary |
| 504 | void resume(); // allow thread to execute, if not requested to exit |
| 505 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | private: |
| 507 | friend class AudioTrack; |
| 508 | virtual bool threadLoop(); |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | AudioTrack& mReceiver; |
Glenn Kasten | a997e7a | 2012-08-07 09:44:19 -0700 | [diff] [blame] | 510 | ~AudioTrackThread(); |
Glenn Kasten | 3acbd05 | 2012-02-28 10:39:56 -0800 | [diff] [blame] | 511 | Mutex mMyLock; // Thread::mLock is private |
| 512 | Condition mMyCond; // Thread::mThreadExitedCondition is private |
| 513 | bool mPaused; // whether thread is currently paused |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | }; |
| 515 | |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 516 | // body of AudioTrackThread::threadLoop() |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | bool processAudioBuffer(const sp<AudioTrackThread>& thread); |
Glenn Kasten | ea7939a | 2012-03-14 12:56:26 -0700 | [diff] [blame] | 518 | |
Glenn Kasten | d5ed6e8 | 2012-11-02 13:05:14 -0700 | [diff] [blame] | 519 | // caller must hold lock on mLock for all _l methods |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 520 | status_t createTrack_l(audio_stream_type_t streamType, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 521 | uint32_t sampleRate, |
Glenn Kasten | e1c3962 | 2012-01-04 09:36:37 -0800 | [diff] [blame] | 522 | audio_format_t format, |
Glenn Kasten | e33054e | 2012-11-14 12:54:39 -0800 | [diff] [blame] | 523 | size_t frameCount, |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 524 | audio_output_flags_t flags, |
Eric Laurent | 34f1d8e | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 525 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | 291f4d5 | 2012-03-19 12:16:56 -0700 | [diff] [blame] | 526 | audio_io_handle_t output); |
Glenn Kasten | 4bae364 | 2012-11-30 13:41:12 -0800 | [diff] [blame] | 527 | |
| 528 | // can only be called when !mActive |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 529 | void flush_l(); |
Glenn Kasten | 4bae364 | 2012-11-30 13:41:12 -0800 | [diff] [blame] | 530 | |
Eric Laurent | 1703cdf | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 531 | status_t setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount); |
| 532 | audio_io_handle_t getOutput_l(); |
| 533 | status_t restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart); |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 534 | bool stopped_l() const { return !mActive; } |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 535 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | sp<IAudioTrack> mAudioTrack; |
| 537 | sp<IMemory> mCblkMemory; |
| 538 | sp<AudioTrackThread> mAudioTrackThread; |
| 539 | |
| 540 | float mVolume[2]; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 541 | float mSendLevel; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame^] | 542 | uint32_t mSampleRate; |
Glenn Kasten | b603744 | 2012-11-14 13:42:25 -0800 | [diff] [blame] | 543 | size_t mFrameCount; // corresponds to current IAudioTrack |
| 544 | size_t mReqFrameCount; // frame count to request the next time a new |
| 545 | // IAudioTrack is needed |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | |
Glenn Kasten | a47f316 | 2012-11-07 10:13:08 -0800 | [diff] [blame] | 547 | audio_track_cblk_t* mCblk; // re-load after mLock.unlock() |
Glenn Kasten | 22eb4e2 | 2012-11-07 14:03:00 -0800 | [diff] [blame] | 548 | |
| 549 | // Starting address of buffers in shared memory. If there is a shared buffer, mBuffers |
| 550 | // is the value of pointer() for the shared buffer, otherwise mBuffers points |
| 551 | // immediately after the control block. This address is for the mapping within client |
| 552 | // address space. AudioFlinger::TrackBase::mBuffer is for the server address space. |
| 553 | void* mBuffers; |
| 554 | |
Glenn Kasten | 60a8392 | 2012-06-21 12:56:37 -0700 | [diff] [blame] | 555 | audio_format_t mFormat; // as requested by client, not forced to 16-bit |
Glenn Kasten | fff6d71 | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 556 | audio_stream_type_t mStreamType; |
Glenn Kasten | e4756fe | 2012-11-29 13:38:14 -0800 | [diff] [blame] | 557 | uint32_t mChannelCount; |
Glenn Kasten | 28b76b3 | 2012-07-03 17:24:41 -0700 | [diff] [blame] | 558 | audio_channel_mask_t mChannelMask; |
Glenn Kasten | 83a0382 | 2012-11-12 07:58:20 -0800 | [diff] [blame] | 559 | |
| 560 | // mFrameSize is equal to mFrameSizeAF for non-PCM or 16-bit PCM data. |
| 561 | // For 8-bit PCM data, mFrameSizeAF is |
| 562 | // twice as large because data is expanded to 16-bit before being stored in buffer. |
| 563 | size_t mFrameSize; // app-level frame size |
| 564 | size_t mFrameSizeAF; // AudioFlinger frame size |
| 565 | |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | status_t mStatus; |
| 567 | uint32_t mLatency; |
| 568 | |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 569 | bool mActive; // protected by mLock |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | |
Glenn Kasten | 99e53b8 | 2012-01-19 08:59:58 -0800 | [diff] [blame] | 571 | callback_t mCbf; // callback handler for events, or NULL |
Glenn Kasten | ad2f6db | 2012-11-01 15:45:06 -0700 | [diff] [blame] | 572 | void* mUserData; // for client callback handler |
| 573 | |
| 574 | // for notification APIs |
Glenn Kasten | 85ab62c | 2012-11-01 11:11:38 -0700 | [diff] [blame] | 575 | uint32_t mNotificationFramesReq; // requested number of frames between each |
| 576 | // notification callback |
| 577 | uint32_t mNotificationFramesAct; // actual number of frames between each |
| 578 | // notification callback |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | sp<IMemory> mSharedBuffer; |
| 580 | int mLoopCount; |
| 581 | uint32_t mRemainingFrames; |
Glenn Kasten | 083d1c1 | 2012-11-30 15:00:36 -0800 | [diff] [blame] | 582 | uint32_t mMarkerPosition; // in wrapping (overflow) frame units |
Jean-Michel Trivi | 2c22aeb | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 583 | bool mMarkerReached; |
Glenn Kasten | ad2f6db | 2012-11-01 15:45:06 -0700 | [diff] [blame] | 584 | uint32_t mNewPosition; // in frames |
| 585 | uint32_t mUpdatePeriod; // in frames |
| 586 | |
Jean-Michel Trivi | cd07594 | 2011-08-25 16:47:23 -0700 | [diff] [blame] | 587 | bool mFlushed; // FIXME will be made obsolete by making flush() synchronous |
Eric Laurent | 0ca3cf9 | 2012-04-18 09:24:29 -0700 | [diff] [blame] | 588 | audio_output_flags_t mFlags; |
Eric Laurent | be916aa | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 589 | int mSessionId; |
Eric Laurent | 2beeb50 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 590 | int mAuxEffectId; |
Glenn Kasten | d5ed6e8 | 2012-11-02 13:05:14 -0700 | [diff] [blame] | 591 | |
| 592 | // When locking both mLock and mCblk->lock, must lock in this order to avoid deadlock: |
| 593 | // 1. mLock |
| 594 | // 2. mCblk->lock |
| 595 | // It is OK to lock only mCblk->lock. |
Glenn Kasten | 9a2aaf9 | 2012-01-03 09:42:47 -0800 | [diff] [blame] | 596 | mutable Mutex mLock; |
Glenn Kasten | d5ed6e8 | 2012-11-02 13:05:14 -0700 | [diff] [blame] | 597 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 598 | bool mIsTimed; |
Glenn Kasten | 8791351 | 2011-06-22 16:15:25 -0700 | [diff] [blame] | 599 | int mPreviousPriority; // before start() |
Glenn Kasten | a636433 | 2012-04-19 09:35:04 -0700 | [diff] [blame] | 600 | SchedPolicy mPreviousSchedulingGroup; |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame^] | 601 | AudioTrackClientProxy* mProxy; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 602 | }; |
| 603 | |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 604 | class TimedAudioTrack : public AudioTrack |
| 605 | { |
| 606 | public: |
| 607 | TimedAudioTrack(); |
| 608 | |
| 609 | /* allocate a shared memory buffer that can be passed to queueTimedBuffer */ |
| 610 | status_t allocateTimedBuffer(size_t size, sp<IMemory>* buffer); |
| 611 | |
| 612 | /* queue a buffer obtained via allocateTimedBuffer for playback at the |
Glenn Kasten | c3ae93f | 2012-07-30 10:59:30 -0700 | [diff] [blame] | 613 | given timestamp. PTS units are microseconds on the media time timeline. |
John Grossman | 4ff14ba | 2012-02-08 16:37:41 -0800 | [diff] [blame] | 614 | The media time transform (set with setMediaTimeTransform) set by the |
| 615 | audio producer will handle converting from media time to local time |
| 616 | (perhaps going through the common time timeline in the case of |
| 617 | synchronized multiroom audio case) */ |
| 618 | status_t queueTimedBuffer(const sp<IMemory>& buffer, int64_t pts); |
| 619 | |
| 620 | /* define a transform between media time and either common time or |
| 621 | local time */ |
| 622 | enum TargetTimeline {LOCAL_TIME, COMMON_TIME}; |
| 623 | status_t setMediaTimeTransform(const LinearTransform& xform, |
| 624 | TargetTimeline target); |
| 625 | }; |
The Android Open Source Project | 89fa4ad | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | |
| 627 | }; // namespace android |
| 628 | |
| 629 | #endif // ANDROID_AUDIOTRACK_H |