The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* //device/extlibs/pv/android/AudioTrack.cpp |
| 2 | ** |
| 3 | ** Copyright 2007, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | #define LOG_TAG "AudioTrack" |
| 21 | |
| 22 | #include <stdint.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <limits.h> |
| 25 | |
| 26 | #include <sched.h> |
| 27 | #include <sys/resource.h> |
| 28 | |
| 29 | #include <private/media/AudioTrackShared.h> |
| 30 | |
| 31 | #include <media/AudioSystem.h> |
| 32 | #include <media/AudioTrack.h> |
| 33 | |
| 34 | #include <utils/Log.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 35 | #include <binder/Parcel.h> |
| 36 | #include <binder/IPCThreadState.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | #include <utils/Timers.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
| 39 | #define LIKELY( exp ) (__builtin_expect( (exp) != 0, true )) |
| 40 | #define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false )) |
| 41 | |
| 42 | namespace android { |
Chia-chi Yeh | bd240c2 | 2010-06-16 06:33:13 +0800 | [diff] [blame] | 43 | // --------------------------------------------------------------------------- |
| 44 | |
| 45 | // static |
| 46 | status_t AudioTrack::getMinFrameCount( |
| 47 | int* frameCount, |
| 48 | int streamType, |
| 49 | uint32_t sampleRate) |
| 50 | { |
| 51 | int afSampleRate; |
| 52 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) { |
| 53 | return NO_INIT; |
| 54 | } |
| 55 | int afFrameCount; |
| 56 | if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) { |
| 57 | return NO_INIT; |
| 58 | } |
| 59 | uint32_t afLatency; |
| 60 | if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) { |
| 61 | return NO_INIT; |
| 62 | } |
| 63 | |
| 64 | // Ensure that buffer depth covers at least audio hardware latency |
| 65 | uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate); |
| 66 | if (minBufCount < 2) minBufCount = 2; |
| 67 | |
| 68 | *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount : |
| 69 | afFrameCount * minBufCount * sampleRate / afSampleRate; |
| 70 | return NO_ERROR; |
| 71 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | |
| 73 | // --------------------------------------------------------------------------- |
| 74 | |
| 75 | AudioTrack::AudioTrack() |
| 76 | : mStatus(NO_INIT) |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | AudioTrack::AudioTrack( |
| 81 | int streamType, |
| 82 | uint32_t sampleRate, |
| 83 | int format, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 84 | int channels, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | int frameCount, |
| 86 | uint32_t flags, |
| 87 | callback_t cbf, |
| 88 | void* user, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 89 | int notificationFrames, |
| 90 | int sessionId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | : mStatus(NO_INIT) |
| 92 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 93 | mStatus = set(streamType, sampleRate, format, channels, |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 94 | frameCount, flags, cbf, user, notificationFrames, |
| 95 | 0, false, sessionId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | AudioTrack::AudioTrack( |
| 99 | int streamType, |
| 100 | uint32_t sampleRate, |
| 101 | int format, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 102 | int channels, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | const sp<IMemory>& sharedBuffer, |
| 104 | uint32_t flags, |
| 105 | callback_t cbf, |
| 106 | void* user, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 107 | int notificationFrames, |
| 108 | int sessionId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | : mStatus(NO_INIT) |
| 110 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 111 | mStatus = set(streamType, sampleRate, format, channels, |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 112 | 0, flags, cbf, user, notificationFrames, |
| 113 | sharedBuffer, false, sessionId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | AudioTrack::~AudioTrack() |
| 117 | { |
| 118 | LOGV_IF(mSharedBuffer != 0, "Destructor sharedBuffer: %p", mSharedBuffer->pointer()); |
| 119 | |
| 120 | if (mStatus == NO_ERROR) { |
| 121 | // Make sure that callback function exits in the case where |
| 122 | // it is looping on buffer full condition in obtainBuffer(). |
| 123 | // Otherwise the callback thread will never exit. |
| 124 | stop(); |
| 125 | if (mAudioTrackThread != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | mAudioTrackThread->requestExitAndWait(); |
| 127 | mAudioTrackThread.clear(); |
| 128 | } |
| 129 | mAudioTrack.clear(); |
| 130 | IPCThreadState::self()->flushCommands(); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | status_t AudioTrack::set( |
| 135 | int streamType, |
| 136 | uint32_t sampleRate, |
| 137 | int format, |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 138 | int channels, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | int frameCount, |
| 140 | uint32_t flags, |
| 141 | callback_t cbf, |
| 142 | void* user, |
| 143 | int notificationFrames, |
| 144 | const sp<IMemory>& sharedBuffer, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 145 | bool threadCanCallJava, |
| 146 | int sessionId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | { |
| 148 | |
| 149 | LOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size()); |
| 150 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 151 | AutoMutex lock(mLock); |
Eric Laurent | ef02827 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 152 | if (mAudioTrack != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | LOGE("Track already in use"); |
| 154 | return INVALID_OPERATION; |
| 155 | } |
| 156 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | int afSampleRate; |
| 158 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) { |
| 159 | return NO_INIT; |
| 160 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | uint32_t afLatency; |
| 162 | if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) { |
| 163 | return NO_INIT; |
| 164 | } |
| 165 | |
| 166 | // handle default values first. |
| 167 | if (streamType == AudioSystem::DEFAULT) { |
| 168 | streamType = AudioSystem::MUSIC; |
| 169 | } |
| 170 | if (sampleRate == 0) { |
| 171 | sampleRate = afSampleRate; |
| 172 | } |
| 173 | // these below should probably come from the audioFlinger too... |
| 174 | if (format == 0) { |
| 175 | format = AudioSystem::PCM_16_BIT; |
| 176 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 177 | if (channels == 0) { |
| 178 | channels = AudioSystem::CHANNEL_OUT_STEREO; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // validate parameters |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 182 | if (!AudioSystem::isValidFormat(format)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | LOGE("Invalid format"); |
| 184 | return BAD_VALUE; |
| 185 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 186 | |
| 187 | // force direct flag if format is not linear PCM |
| 188 | if (!AudioSystem::isLinearPCM(format)) { |
| 189 | flags |= AudioSystem::OUTPUT_FLAG_DIRECT; |
| 190 | } |
| 191 | |
| 192 | if (!AudioSystem::isOutputChannel(channels)) { |
| 193 | LOGE("Invalid channel mask"); |
| 194 | return BAD_VALUE; |
| 195 | } |
| 196 | uint32_t channelCount = AudioSystem::popCount(channels); |
| 197 | |
| 198 | audio_io_handle_t output = AudioSystem::getOutput((AudioSystem::stream_type)streamType, |
| 199 | sampleRate, format, channels, (AudioSystem::output_flags)flags); |
| 200 | |
| 201 | if (output == 0) { |
| 202 | LOGE("Could not get audio output for stream type %d", streamType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | return BAD_VALUE; |
| 204 | } |
| 205 | |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 206 | mVolume[LEFT] = 1.0f; |
| 207 | mVolume[RIGHT] = 1.0f; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 208 | mSendLevel = 0; |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 209 | mFrameCount = frameCount; |
| 210 | mNotificationFramesReq = notificationFrames; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 211 | mSessionId = sessionId; |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 212 | mAuxEffectId = 0; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 213 | |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 214 | // create the IAudioTrack |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 215 | status_t status = createTrack_l(streamType, |
| 216 | sampleRate, |
| 217 | format, |
| 218 | channelCount, |
| 219 | frameCount, |
| 220 | flags, |
| 221 | sharedBuffer, |
| 222 | output, |
| 223 | true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 225 | if (status != NO_ERROR) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | return status; |
| 227 | } |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 228 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | if (cbf != 0) { |
| 230 | mAudioTrackThread = new AudioTrackThread(*this, threadCanCallJava); |
| 231 | if (mAudioTrackThread == 0) { |
| 232 | LOGE("Could not create callback thread"); |
| 233 | return NO_INIT; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | mStatus = NO_ERROR; |
| 238 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | mStreamType = streamType; |
| 240 | mFormat = format; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 241 | mChannels = channels; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | mChannelCount = channelCount; |
| 243 | mSharedBuffer = sharedBuffer; |
| 244 | mMuted = false; |
| 245 | mActive = 0; |
| 246 | mCbf = cbf; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | mUserData = user; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | mLoopCount = 0; |
| 249 | mMarkerPosition = 0; |
Jean-Michel Trivi | 4a5c1a7 | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 250 | mMarkerReached = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | mNewPosition = 0; |
| 252 | mUpdatePeriod = 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 253 | mFlags = flags; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | |
| 255 | return NO_ERROR; |
| 256 | } |
| 257 | |
| 258 | status_t AudioTrack::initCheck() const |
| 259 | { |
| 260 | return mStatus; |
| 261 | } |
| 262 | |
| 263 | // ------------------------------------------------------------------------- |
| 264 | |
| 265 | uint32_t AudioTrack::latency() const |
| 266 | { |
| 267 | return mLatency; |
| 268 | } |
| 269 | |
| 270 | int AudioTrack::streamType() const |
| 271 | { |
| 272 | return mStreamType; |
| 273 | } |
| 274 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | int AudioTrack::format() const |
| 276 | { |
| 277 | return mFormat; |
| 278 | } |
| 279 | |
| 280 | int AudioTrack::channelCount() const |
| 281 | { |
| 282 | return mChannelCount; |
| 283 | } |
| 284 | |
| 285 | uint32_t AudioTrack::frameCount() const |
| 286 | { |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 287 | return mCblk->frameCount; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | int AudioTrack::frameSize() const |
| 291 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 292 | if (AudioSystem::isLinearPCM(mFormat)) { |
| 293 | return channelCount()*((format() == AudioSystem::PCM_8_BIT) ? sizeof(uint8_t) : sizeof(int16_t)); |
| 294 | } else { |
| 295 | return sizeof(uint8_t); |
| 296 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | sp<IMemory>& AudioTrack::sharedBuffer() |
| 300 | { |
| 301 | return mSharedBuffer; |
| 302 | } |
| 303 | |
| 304 | // ------------------------------------------------------------------------- |
| 305 | |
| 306 | void AudioTrack::start() |
| 307 | { |
| 308 | sp<AudioTrackThread> t = mAudioTrackThread; |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 309 | status_t status; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | |
| 311 | LOGV("start %p", this); |
| 312 | if (t != 0) { |
| 313 | if (t->exitPending()) { |
| 314 | if (t->requestExitAndWait() == WOULD_BLOCK) { |
| 315 | LOGE("AudioTrack::start called from thread"); |
| 316 | return; |
| 317 | } |
| 318 | } |
| 319 | t->mLock.lock(); |
| 320 | } |
| 321 | |
Eric Laurent | f3d6dd0 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 322 | AutoMutex lock(mLock); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 323 | // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed |
| 324 | // while we are accessing the cblk |
| 325 | sp <IAudioTrack> audioTrack = mAudioTrack; |
| 326 | sp <IMemory> iMem = mCblkMemory; |
| 327 | audio_track_cblk_t* cblk = mCblk; |
| 328 | |
Eric Laurent | f3d6dd0 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 329 | if (mActive == 0) { |
| 330 | mActive = 1; |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 331 | mNewPosition = cblk->server + mUpdatePeriod; |
Eric Laurent | 913af0b | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 332 | cblk->lock.lock(); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 333 | cblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS; |
| 334 | cblk->waitTimeMs = 0; |
| 335 | cblk->flags &= ~CBLK_DISABLED_ON; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 336 | if (t != 0) { |
| 337 | t->run("AudioTrackThread", THREAD_PRIORITY_AUDIO_CLIENT); |
| 338 | } else { |
| 339 | setpriority(PRIO_PROCESS, 0, THREAD_PRIORITY_AUDIO_CLIENT); |
| 340 | } |
| 341 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 342 | LOGV("start %p before lock cblk %p", this, mCblk); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 343 | if (!(cblk->flags & CBLK_INVALID_MSK)) { |
| 344 | cblk->lock.unlock(); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 345 | status = mAudioTrack->start(); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 346 | cblk->lock.lock(); |
| 347 | if (status == DEAD_OBJECT) { |
| 348 | cblk->flags |= CBLK_INVALID_MSK; |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 349 | } |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 350 | } |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 351 | if (cblk->flags & CBLK_INVALID_MSK) { |
| 352 | status = restoreTrack_l(cblk, true); |
| 353 | } |
| 354 | cblk->lock.unlock(); |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 355 | if (status != NO_ERROR) { |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 356 | LOGV("start() failed"); |
Eric Laurent | f3d6dd0 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 357 | mActive = 0; |
Eric Laurent | 059b4be | 2009-11-09 23:32:22 -0800 | [diff] [blame] | 358 | if (t != 0) { |
| 359 | t->requestExit(); |
| 360 | } else { |
| 361 | setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_NORMAL); |
| 362 | } |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 363 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | if (t != 0) { |
| 367 | t->mLock.unlock(); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | void AudioTrack::stop() |
| 372 | { |
| 373 | sp<AudioTrackThread> t = mAudioTrackThread; |
| 374 | |
| 375 | LOGV("stop %p", this); |
| 376 | if (t != 0) { |
| 377 | t->mLock.lock(); |
| 378 | } |
| 379 | |
Eric Laurent | f3d6dd0 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 380 | AutoMutex lock(mLock); |
| 381 | if (mActive == 1) { |
| 382 | mActive = 0; |
Eric Laurent | ef02827 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 383 | mCblk->cv.signal(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | mAudioTrack->stop(); |
| 385 | // Cancel loops (If we are in the middle of a loop, playback |
| 386 | // would not stop until loopCount reaches 0). |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 387 | setLoop_l(0, 0, 0); |
Jean-Michel Trivi | 4a5c1a7 | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 388 | // the playback head position will reset to 0, so if a marker is set, we need |
| 389 | // to activate it again |
| 390 | mMarkerReached = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | // Force flush if a shared buffer is used otherwise audioflinger |
| 392 | // will not stop before end of buffer is reached. |
| 393 | if (mSharedBuffer != 0) { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 394 | flush_l(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | } |
| 396 | if (t != 0) { |
| 397 | t->requestExit(); |
| 398 | } else { |
| 399 | setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_NORMAL); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | if (t != 0) { |
| 404 | t->mLock.unlock(); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | bool AudioTrack::stopped() const |
| 409 | { |
| 410 | return !mActive; |
| 411 | } |
| 412 | |
| 413 | void AudioTrack::flush() |
| 414 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 415 | AutoMutex lock(mLock); |
| 416 | flush_l(); |
| 417 | } |
| 418 | |
| 419 | // must be called with mLock held |
| 420 | void AudioTrack::flush_l() |
| 421 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | LOGV("flush"); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 423 | |
Jean-Michel Trivi | 4a5c1a7 | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 424 | // clear playback marker and periodic update counter |
| 425 | mMarkerPosition = 0; |
| 426 | mMarkerReached = false; |
| 427 | mUpdatePeriod = 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 428 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | if (!mActive) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | mAudioTrack->flush(); |
| 431 | // Release AudioTrack callback thread in case it was waiting for new buffers |
| 432 | // in AudioTrack::obtainBuffer() |
| 433 | mCblk->cv.signal(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
| 437 | void AudioTrack::pause() |
| 438 | { |
| 439 | LOGV("pause"); |
Eric Laurent | f3d6dd0 | 2010-11-18 08:40:16 -0800 | [diff] [blame] | 440 | AutoMutex lock(mLock); |
| 441 | if (mActive == 1) { |
| 442 | mActive = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | mAudioTrack->pause(); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | void AudioTrack::mute(bool e) |
| 448 | { |
| 449 | mAudioTrack->mute(e); |
| 450 | mMuted = e; |
| 451 | } |
| 452 | |
| 453 | bool AudioTrack::muted() const |
| 454 | { |
| 455 | return mMuted; |
| 456 | } |
| 457 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 458 | status_t AudioTrack::setVolume(float left, float right) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 460 | if (left > 1.0f || right > 1.0f) { |
| 461 | return BAD_VALUE; |
| 462 | } |
| 463 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 464 | AutoMutex lock(mLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | mVolume[LEFT] = left; |
| 466 | mVolume[RIGHT] = right; |
| 467 | |
| 468 | // write must be atomic |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 469 | mCblk->volumeLR = (uint32_t(uint16_t(right * 0x1000)) << 16) | uint16_t(left * 0x1000); |
| 470 | |
| 471 | return NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | void AudioTrack::getVolume(float* left, float* right) |
| 475 | { |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 476 | if (left != NULL) { |
| 477 | *left = mVolume[LEFT]; |
| 478 | } |
| 479 | if (right != NULL) { |
| 480 | *right = mVolume[RIGHT]; |
| 481 | } |
| 482 | } |
| 483 | |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 484 | status_t AudioTrack::setAuxEffectSendLevel(float level) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 485 | { |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 486 | LOGV("setAuxEffectSendLevel(%f)", level); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 487 | if (level > 1.0f) { |
| 488 | return BAD_VALUE; |
| 489 | } |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 490 | AutoMutex lock(mLock); |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 491 | |
| 492 | mSendLevel = level; |
| 493 | |
| 494 | mCblk->sendLevel = uint16_t(level * 0x1000); |
| 495 | |
| 496 | return NO_ERROR; |
| 497 | } |
| 498 | |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 499 | void AudioTrack::getAuxEffectSendLevel(float* level) |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 500 | { |
| 501 | if (level != NULL) { |
| 502 | *level = mSendLevel; |
| 503 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | } |
| 505 | |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 506 | status_t AudioTrack::setSampleRate(int rate) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | { |
| 508 | int afSamplingRate; |
| 509 | |
| 510 | if (AudioSystem::getOutputSamplingRate(&afSamplingRate, mStreamType) != NO_ERROR) { |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 511 | return NO_INIT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 512 | } |
| 513 | // Resampler implementation limits input sampling rate to 2 x output sampling rate. |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 514 | if (rate <= 0 || rate > afSamplingRate*2 ) return BAD_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 515 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 516 | AutoMutex lock(mLock); |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 517 | mCblk->sampleRate = rate; |
| 518 | return NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | uint32_t AudioTrack::getSampleRate() |
| 522 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 523 | AutoMutex lock(mLock); |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 524 | return mCblk->sampleRate; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | status_t AudioTrack::setLoop(uint32_t loopStart, uint32_t loopEnd, int loopCount) |
| 528 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 529 | AutoMutex lock(mLock); |
| 530 | return setLoop_l(loopStart, loopEnd, loopCount); |
| 531 | } |
| 532 | |
| 533 | // must be called with mLock held |
| 534 | status_t AudioTrack::setLoop_l(uint32_t loopStart, uint32_t loopEnd, int loopCount) |
| 535 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | audio_track_cblk_t* cblk = mCblk; |
| 537 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | Mutex::Autolock _l(cblk->lock); |
| 539 | |
| 540 | if (loopCount == 0) { |
| 541 | cblk->loopStart = UINT_MAX; |
| 542 | cblk->loopEnd = UINT_MAX; |
| 543 | cblk->loopCount = 0; |
| 544 | mLoopCount = 0; |
| 545 | return NO_ERROR; |
| 546 | } |
| 547 | |
| 548 | if (loopStart >= loopEnd || |
Eric Laurent | 6667ac3 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 549 | loopEnd - loopStart > cblk->frameCount || |
| 550 | cblk->server > loopStart) { |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 551 | LOGE("setLoop invalid value: loopStart %d, loopEnd %d, loopCount %d, framecount %d, user %d", loopStart, loopEnd, loopCount, cblk->frameCount, cblk->user); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | return BAD_VALUE; |
| 553 | } |
| 554 | |
Eric Laurent | 6667ac3 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 555 | if ((mSharedBuffer != 0) && (loopEnd > cblk->frameCount)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 556 | LOGE("setLoop invalid value: loop markers beyond data: loopStart %d, loopEnd %d, framecount %d", |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 557 | loopStart, loopEnd, cblk->frameCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | return BAD_VALUE; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 559 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | |
| 561 | cblk->loopStart = loopStart; |
| 562 | cblk->loopEnd = loopEnd; |
| 563 | cblk->loopCount = loopCount; |
| 564 | mLoopCount = loopCount; |
| 565 | |
| 566 | return NO_ERROR; |
| 567 | } |
| 568 | |
| 569 | status_t AudioTrack::getLoop(uint32_t *loopStart, uint32_t *loopEnd, int *loopCount) |
| 570 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 571 | AutoMutex lock(mLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | if (loopStart != 0) { |
| 573 | *loopStart = mCblk->loopStart; |
| 574 | } |
| 575 | if (loopEnd != 0) { |
| 576 | *loopEnd = mCblk->loopEnd; |
| 577 | } |
| 578 | if (loopCount != 0) { |
| 579 | if (mCblk->loopCount < 0) { |
| 580 | *loopCount = -1; |
| 581 | } else { |
| 582 | *loopCount = mCblk->loopCount; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | return NO_ERROR; |
| 587 | } |
| 588 | |
| 589 | status_t AudioTrack::setMarkerPosition(uint32_t marker) |
| 590 | { |
| 591 | if (mCbf == 0) return INVALID_OPERATION; |
| 592 | |
| 593 | mMarkerPosition = marker; |
Jean-Michel Trivi | 4a5c1a7 | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 594 | mMarkerReached = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | |
| 596 | return NO_ERROR; |
| 597 | } |
| 598 | |
| 599 | status_t AudioTrack::getMarkerPosition(uint32_t *marker) |
| 600 | { |
| 601 | if (marker == 0) return BAD_VALUE; |
| 602 | |
| 603 | *marker = mMarkerPosition; |
| 604 | |
| 605 | return NO_ERROR; |
| 606 | } |
| 607 | |
| 608 | status_t AudioTrack::setPositionUpdatePeriod(uint32_t updatePeriod) |
| 609 | { |
| 610 | if (mCbf == 0) return INVALID_OPERATION; |
| 611 | |
| 612 | uint32_t curPosition; |
| 613 | getPosition(&curPosition); |
| 614 | mNewPosition = curPosition + updatePeriod; |
| 615 | mUpdatePeriod = updatePeriod; |
| 616 | |
| 617 | return NO_ERROR; |
| 618 | } |
| 619 | |
| 620 | status_t AudioTrack::getPositionUpdatePeriod(uint32_t *updatePeriod) |
| 621 | { |
| 622 | if (updatePeriod == 0) return BAD_VALUE; |
| 623 | |
| 624 | *updatePeriod = mUpdatePeriod; |
| 625 | |
| 626 | return NO_ERROR; |
| 627 | } |
| 628 | |
| 629 | status_t AudioTrack::setPosition(uint32_t position) |
| 630 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 631 | AutoMutex lock(mLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | Mutex::Autolock _l(mCblk->lock); |
| 633 | |
| 634 | if (!stopped()) return INVALID_OPERATION; |
| 635 | |
| 636 | if (position > mCblk->user) return BAD_VALUE; |
| 637 | |
| 638 | mCblk->server = position; |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 639 | mCblk->flags |= CBLK_FORCEREADY_ON; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 640 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | return NO_ERROR; |
| 642 | } |
| 643 | |
| 644 | status_t AudioTrack::getPosition(uint32_t *position) |
| 645 | { |
| 646 | if (position == 0) return BAD_VALUE; |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 647 | AutoMutex lock(mLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | *position = mCblk->server; |
| 649 | |
| 650 | return NO_ERROR; |
| 651 | } |
| 652 | |
| 653 | status_t AudioTrack::reload() |
| 654 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 655 | AutoMutex lock(mLock); |
| 656 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | if (!stopped()) return INVALID_OPERATION; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 658 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 659 | flush_l(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 660 | |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 661 | mCblk->stepUser(mCblk->frameCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 662 | |
| 663 | return NO_ERROR; |
| 664 | } |
| 665 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 666 | audio_io_handle_t AudioTrack::getOutput() |
| 667 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 668 | AutoMutex lock(mLock); |
| 669 | return getOutput_l(); |
| 670 | } |
| 671 | |
| 672 | // must be called with mLock held |
| 673 | audio_io_handle_t AudioTrack::getOutput_l() |
| 674 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 675 | return AudioSystem::getOutput((AudioSystem::stream_type)mStreamType, |
| 676 | mCblk->sampleRate, mFormat, mChannels, (AudioSystem::output_flags)mFlags); |
| 677 | } |
| 678 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 679 | int AudioTrack::getSessionId() |
| 680 | { |
| 681 | return mSessionId; |
| 682 | } |
| 683 | |
| 684 | status_t AudioTrack::attachAuxEffect(int effectId) |
| 685 | { |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 686 | LOGV("attachAuxEffect(%d)", effectId); |
| 687 | status_t status = mAudioTrack->attachAuxEffect(effectId); |
| 688 | if (status == NO_ERROR) { |
| 689 | mAuxEffectId = effectId; |
| 690 | } |
| 691 | return status; |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 692 | } |
| 693 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 694 | // ------------------------------------------------------------------------- |
| 695 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 696 | // must be called with mLock held |
| 697 | status_t AudioTrack::createTrack_l( |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 698 | int streamType, |
| 699 | uint32_t sampleRate, |
| 700 | int format, |
| 701 | int channelCount, |
| 702 | int frameCount, |
| 703 | uint32_t flags, |
| 704 | const sp<IMemory>& sharedBuffer, |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 705 | audio_io_handle_t output, |
| 706 | bool enforceFrameCount) |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 707 | { |
| 708 | status_t status; |
| 709 | const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger(); |
| 710 | if (audioFlinger == 0) { |
| 711 | LOGE("Could not get audioflinger"); |
| 712 | return NO_INIT; |
| 713 | } |
| 714 | |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 715 | int afSampleRate; |
| 716 | if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) { |
| 717 | return NO_INIT; |
| 718 | } |
| 719 | int afFrameCount; |
| 720 | if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) { |
| 721 | return NO_INIT; |
| 722 | } |
| 723 | uint32_t afLatency; |
| 724 | if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) { |
| 725 | return NO_INIT; |
| 726 | } |
| 727 | |
| 728 | mNotificationFramesAct = mNotificationFramesReq; |
| 729 | if (!AudioSystem::isLinearPCM(format)) { |
| 730 | if (sharedBuffer != 0) { |
| 731 | frameCount = sharedBuffer->size(); |
| 732 | } |
| 733 | } else { |
| 734 | // Ensure that buffer depth covers at least audio hardware latency |
| 735 | uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate); |
| 736 | if (minBufCount < 2) minBufCount = 2; |
| 737 | |
| 738 | int minFrameCount = (afFrameCount*sampleRate*minBufCount)/afSampleRate; |
| 739 | |
| 740 | if (sharedBuffer == 0) { |
| 741 | if (frameCount == 0) { |
| 742 | frameCount = minFrameCount; |
| 743 | } |
| 744 | if (mNotificationFramesAct == 0) { |
| 745 | mNotificationFramesAct = frameCount/2; |
| 746 | } |
| 747 | // Make sure that application is notified with sufficient margin |
| 748 | // before underrun |
| 749 | if (mNotificationFramesAct > (uint32_t)frameCount/2) { |
| 750 | mNotificationFramesAct = frameCount/2; |
| 751 | } |
| 752 | if (frameCount < minFrameCount) { |
| 753 | if (enforceFrameCount) { |
| 754 | LOGE("Invalid buffer size: minFrameCount %d, frameCount %d", minFrameCount, frameCount); |
| 755 | return BAD_VALUE; |
| 756 | } else { |
| 757 | frameCount = minFrameCount; |
| 758 | } |
| 759 | } |
| 760 | } else { |
| 761 | // Ensure that buffer alignment matches channelcount |
| 762 | if (((uint32_t)sharedBuffer->pointer() & (channelCount | 1)) != 0) { |
| 763 | LOGE("Invalid buffer alignement: address %p, channelCount %d", sharedBuffer->pointer(), channelCount); |
| 764 | return BAD_VALUE; |
| 765 | } |
| 766 | frameCount = sharedBuffer->size()/channelCount/sizeof(int16_t); |
| 767 | } |
| 768 | } |
| 769 | |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 770 | sp<IAudioTrack> track = audioFlinger->createTrack(getpid(), |
| 771 | streamType, |
| 772 | sampleRate, |
| 773 | format, |
| 774 | channelCount, |
| 775 | frameCount, |
| 776 | ((uint16_t)flags) << 16, |
| 777 | sharedBuffer, |
| 778 | output, |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 779 | &mSessionId, |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 780 | &status); |
| 781 | |
| 782 | if (track == 0) { |
| 783 | LOGE("AudioFlinger could not create track, status: %d", status); |
| 784 | return status; |
| 785 | } |
| 786 | sp<IMemory> cblk = track->getCblk(); |
| 787 | if (cblk == 0) { |
| 788 | LOGE("Could not get control block"); |
| 789 | return NO_INIT; |
| 790 | } |
| 791 | mAudioTrack.clear(); |
| 792 | mAudioTrack = track; |
| 793 | mCblkMemory.clear(); |
| 794 | mCblkMemory = cblk; |
| 795 | mCblk = static_cast<audio_track_cblk_t*>(cblk->pointer()); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 796 | mCblk->flags |= CBLK_DIRECTION_OUT; |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 797 | if (sharedBuffer == 0) { |
| 798 | mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 799 | } else { |
| 800 | mCblk->buffers = sharedBuffer->pointer(); |
| 801 | // Force buffer full condition as data is already present in shared memory |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 802 | mCblk->stepUser(mCblk->frameCount); |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 803 | } |
| 804 | |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 805 | mCblk->volumeLR = (uint32_t(uint16_t(mVolume[RIGHT] * 0x1000)) << 16) | uint16_t(mVolume[LEFT] * 0x1000); |
| 806 | mCblk->sendLevel = uint16_t(mSendLevel * 0x1000); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 807 | mAudioTrack->attachAuxEffect(mAuxEffectId); |
Eric Laurent | 49f02be | 2009-11-19 09:00:56 -0800 | [diff] [blame] | 808 | mCblk->bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS; |
| 809 | mCblk->waitTimeMs = 0; |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 810 | mRemainingFrames = mNotificationFramesAct; |
| 811 | mLatency = afLatency + (1000*mCblk->frameCount) / sampleRate; |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 812 | return NO_ERROR; |
| 813 | } |
| 814 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 815 | status_t AudioTrack::obtainBuffer(Buffer* audioBuffer, int32_t waitCount) |
| 816 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 817 | AutoMutex lock(mLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 818 | int active; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 819 | status_t result; |
| 820 | audio_track_cblk_t* cblk = mCblk; |
| 821 | uint32_t framesReq = audioBuffer->frameCount; |
Eric Laurent | ef02827 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 822 | uint32_t waitTimeMs = (waitCount < 0) ? cblk->bufferTimeoutMs : WAIT_PERIOD_MS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 823 | |
| 824 | audioBuffer->frameCount = 0; |
| 825 | audioBuffer->size = 0; |
| 826 | |
| 827 | uint32_t framesAvail = cblk->framesAvailable(); |
| 828 | |
Eric Laurent | 6667ac3 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 829 | cblk->lock.lock(); |
| 830 | if (cblk->flags & CBLK_INVALID_MSK) { |
| 831 | goto create_new_track; |
| 832 | } |
| 833 | cblk->lock.unlock(); |
| 834 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 835 | if (framesAvail == 0) { |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 836 | cblk->lock.lock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 837 | goto start_loop_here; |
| 838 | while (framesAvail == 0) { |
| 839 | active = mActive; |
| 840 | if (UNLIKELY(!active)) { |
| 841 | LOGV("Not active and NO_MORE_BUFFERS"); |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 842 | cblk->lock.unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 843 | return NO_MORE_BUFFERS; |
| 844 | } |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 845 | if (UNLIKELY(!waitCount)) { |
| 846 | cblk->lock.unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 847 | return WOULD_BLOCK; |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 848 | } |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 849 | if (!(cblk->flags & CBLK_INVALID_MSK)) { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 850 | mLock.unlock(); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 851 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs)); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 852 | cblk->lock.unlock(); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 853 | mLock.lock(); |
| 854 | if (mActive == 0) { |
| 855 | return status_t(STOPPED); |
| 856 | } |
| 857 | cblk->lock.lock(); |
| 858 | } |
| 859 | |
| 860 | if (cblk->flags & CBLK_INVALID_MSK) { |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 861 | goto create_new_track; |
| 862 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 863 | if (__builtin_expect(result!=NO_ERROR, false)) { |
Eric Laurent | ef02827 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 864 | cblk->waitTimeMs += waitTimeMs; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 865 | if (cblk->waitTimeMs >= cblk->bufferTimeoutMs) { |
| 866 | // timing out when a loop has been set and we have already written upto loop end |
| 867 | // is a normal condition: no need to wake AudioFlinger up. |
| 868 | if (cblk->user < cblk->loopEnd) { |
| 869 | LOGW( "obtainBuffer timed out (is the CPU pegged?) %p " |
| 870 | "user=%08x, server=%08x", this, cblk->user, cblk->server); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 871 | //unlock cblk mutex before calling mAudioTrack->start() (see issue #1617140) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 872 | cblk->lock.unlock(); |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 873 | result = mAudioTrack->start(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | cblk->lock.lock(); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 875 | if (result == DEAD_OBJECT) { |
| 876 | cblk->flags |= CBLK_INVALID_MSK; |
| 877 | create_new_track: |
| 878 | result = restoreTrack_l(cblk, false); |
| 879 | } |
| 880 | if (result != NO_ERROR) { |
| 881 | LOGW("obtainBuffer create Track error %d", result); |
| 882 | cblk->lock.unlock(); |
| 883 | return result; |
| 884 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 885 | } |
| 886 | cblk->waitTimeMs = 0; |
| 887 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 888 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 889 | if (--waitCount == 0) { |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 890 | cblk->lock.unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 891 | return TIMED_OUT; |
| 892 | } |
| 893 | } |
| 894 | // read the server count again |
| 895 | start_loop_here: |
| 896 | framesAvail = cblk->framesAvailable_l(); |
| 897 | } |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 898 | cblk->lock.unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 899 | } |
| 900 | |
Eric Laurent | 4712baa | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 901 | // restart track if it was disabled by audioflinger due to previous underrun |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 902 | if (mActive && (cblk->flags & CBLK_DISABLED_MSK)) { |
Eric Laurent | 913af0b | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 903 | AutoMutex _l(cblk->lock); |
| 904 | if (mActive && (cblk->flags & CBLK_DISABLED_MSK)) { |
| 905 | cblk->flags &= ~CBLK_DISABLED_ON; |
| 906 | cblk->lock.unlock(); |
| 907 | LOGW("obtainBuffer() track %p disabled, restarting", this); |
| 908 | mAudioTrack->start(); |
| 909 | cblk->lock.lock(); |
| 910 | } |
Eric Laurent | 4712baa | 2010-09-30 16:12:31 -0700 | [diff] [blame] | 911 | } |
| 912 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 913 | cblk->waitTimeMs = 0; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 914 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 915 | if (framesReq > framesAvail) { |
| 916 | framesReq = framesAvail; |
| 917 | } |
| 918 | |
| 919 | uint32_t u = cblk->user; |
| 920 | uint32_t bufferEnd = cblk->userBase + cblk->frameCount; |
| 921 | |
| 922 | if (u + framesReq > bufferEnd) { |
| 923 | framesReq = bufferEnd - u; |
| 924 | } |
| 925 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 926 | audioBuffer->flags = mMuted ? Buffer::MUTE : 0; |
| 927 | audioBuffer->channelCount = mChannelCount; |
| 928 | audioBuffer->frameCount = framesReq; |
| 929 | audioBuffer->size = framesReq * cblk->frameSize; |
| 930 | if (AudioSystem::isLinearPCM(mFormat)) { |
| 931 | audioBuffer->format = AudioSystem::PCM_16_BIT; |
| 932 | } else { |
| 933 | audioBuffer->format = mFormat; |
| 934 | } |
| 935 | audioBuffer->raw = (int8_t *)cblk->buffer(u); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 936 | active = mActive; |
| 937 | return active ? status_t(NO_ERROR) : status_t(STOPPED); |
| 938 | } |
| 939 | |
| 940 | void AudioTrack::releaseBuffer(Buffer* audioBuffer) |
| 941 | { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 942 | AutoMutex lock(mLock); |
| 943 | mCblk->stepUser(audioBuffer->frameCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | // ------------------------------------------------------------------------- |
| 947 | |
| 948 | ssize_t AudioTrack::write(const void* buffer, size_t userSize) |
| 949 | { |
| 950 | |
| 951 | if (mSharedBuffer != 0) return INVALID_OPERATION; |
| 952 | |
| 953 | if (ssize_t(userSize) < 0) { |
| 954 | // sanity-check. user is most-likely passing an error code. |
| 955 | LOGE("AudioTrack::write(buffer=%p, size=%u (%d)", |
| 956 | buffer, userSize, userSize); |
| 957 | return BAD_VALUE; |
| 958 | } |
| 959 | |
| 960 | LOGV("write %p: %d bytes, mActive=%d", this, userSize, mActive); |
| 961 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 962 | // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed |
| 963 | // while we are accessing the cblk |
| 964 | mLock.lock(); |
| 965 | sp <IAudioTrack> audioTrack = mAudioTrack; |
| 966 | sp <IMemory> iMem = mCblkMemory; |
| 967 | mLock.unlock(); |
| 968 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 969 | ssize_t written = 0; |
| 970 | const int8_t *src = (const int8_t *)buffer; |
| 971 | Buffer audioBuffer; |
Eric Laurent | 913af0b | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 972 | size_t frameSz = (size_t)frameSize(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 973 | |
| 974 | do { |
Eric Laurent | 913af0b | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 975 | audioBuffer.frameCount = userSize/frameSz; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 976 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 977 | // Calling obtainBuffer() with a negative wait count causes |
| 978 | // an (almost) infinite wait time. |
| 979 | status_t err = obtainBuffer(&audioBuffer, -1); |
| 980 | if (err < 0) { |
| 981 | // out of buffers, return #bytes written |
| 982 | if (err == status_t(NO_MORE_BUFFERS)) |
| 983 | break; |
| 984 | return ssize_t(err); |
| 985 | } |
| 986 | |
| 987 | size_t toWrite; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 988 | |
Eric Laurent | 28ad42b | 2009-08-04 10:42:26 -0700 | [diff] [blame] | 989 | if (mFormat == AudioSystem::PCM_8_BIT && !(mFlags & AudioSystem::OUTPUT_FLAG_DIRECT)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 990 | // Divide capacity by 2 to take expansion into account |
| 991 | toWrite = audioBuffer.size>>1; |
| 992 | // 8 to 16 bit conversion |
| 993 | int count = toWrite; |
| 994 | int16_t *dst = (int16_t *)(audioBuffer.i8); |
| 995 | while(count--) { |
| 996 | *dst++ = (int16_t)(*src++^0x80) << 8; |
| 997 | } |
Eric Laurent | 28ad42b | 2009-08-04 10:42:26 -0700 | [diff] [blame] | 998 | } else { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 999 | toWrite = audioBuffer.size; |
| 1000 | memcpy(audioBuffer.i8, src, toWrite); |
| 1001 | src += toWrite; |
| 1002 | } |
| 1003 | userSize -= toWrite; |
| 1004 | written += toWrite; |
| 1005 | |
| 1006 | releaseBuffer(&audioBuffer); |
Eric Laurent | 913af0b | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 1007 | } while (userSize >= frameSz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1008 | |
| 1009 | return written; |
| 1010 | } |
| 1011 | |
| 1012 | // ------------------------------------------------------------------------- |
| 1013 | |
| 1014 | bool AudioTrack::processAudioBuffer(const sp<AudioTrackThread>& thread) |
| 1015 | { |
| 1016 | Buffer audioBuffer; |
| 1017 | uint32_t frames; |
| 1018 | size_t writtenSize; |
| 1019 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1020 | mLock.lock(); |
| 1021 | // acquire a strong reference on the IMemory and IAudioTrack so that they cannot be destroyed |
| 1022 | // while we are accessing the cblk |
| 1023 | sp <IAudioTrack> audioTrack = mAudioTrack; |
| 1024 | sp <IMemory> iMem = mCblkMemory; |
| 1025 | audio_track_cblk_t* cblk = mCblk; |
| 1026 | mLock.unlock(); |
| 1027 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1028 | // Manage underrun callback |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1029 | if (mActive && (cblk->framesReady() == 0)) { |
| 1030 | LOGV("Underrun user: %x, server: %x, flags %04x", cblk->user, cblk->server, cblk->flags); |
Eric Laurent | 913af0b | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 1031 | AutoMutex _l(cblk->lock); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1032 | if ((cblk->flags & CBLK_UNDERRUN_MSK) == CBLK_UNDERRUN_OFF) { |
Eric Laurent | 913af0b | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 1033 | cblk->flags |= CBLK_UNDERRUN_ON; |
| 1034 | cblk->lock.unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1035 | mCbf(EVENT_UNDERRUN, mUserData, 0); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1036 | if (cblk->server == cblk->frameCount) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1037 | mCbf(EVENT_BUFFER_END, mUserData, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1038 | } |
Eric Laurent | 913af0b | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 1039 | cblk->lock.lock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1040 | if (mSharedBuffer != 0) return false; |
| 1041 | } |
| 1042 | } |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1043 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1044 | // Manage loop end callback |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1045 | while (mLoopCount > cblk->loopCount) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1046 | int loopCount = -1; |
| 1047 | mLoopCount--; |
| 1048 | if (mLoopCount >= 0) loopCount = mLoopCount; |
| 1049 | |
| 1050 | mCbf(EVENT_LOOP_END, mUserData, (void *)&loopCount); |
| 1051 | } |
| 1052 | |
| 1053 | // Manage marker callback |
Jean-Michel Trivi | 4a5c1a7 | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 1054 | if (!mMarkerReached && (mMarkerPosition > 0)) { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1055 | if (cblk->server >= mMarkerPosition) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1056 | mCbf(EVENT_MARKER, mUserData, (void *)&mMarkerPosition); |
Jean-Michel Trivi | 4a5c1a7 | 2009-03-24 18:11:07 -0700 | [diff] [blame] | 1057 | mMarkerReached = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | // Manage new position callback |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1062 | if (mUpdatePeriod > 0) { |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1063 | while (cblk->server >= mNewPosition) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1064 | mCbf(EVENT_NEW_POS, mUserData, (void *)&mNewPosition); |
| 1065 | mNewPosition += mUpdatePeriod; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | // If Shared buffer is used, no data is requested from client. |
| 1070 | if (mSharedBuffer != 0) { |
| 1071 | frames = 0; |
| 1072 | } else { |
| 1073 | frames = mRemainingFrames; |
| 1074 | } |
| 1075 | |
| 1076 | do { |
| 1077 | |
| 1078 | audioBuffer.frameCount = frames; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1079 | |
| 1080 | // Calling obtainBuffer() with a wait count of 1 |
| 1081 | // limits wait time to WAIT_PERIOD_MS. This prevents from being |
| 1082 | // stuck here not being able to handle timed events (position, markers, loops). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1083 | status_t err = obtainBuffer(&audioBuffer, 1); |
| 1084 | if (err < NO_ERROR) { |
| 1085 | if (err != TIMED_OUT) { |
Eric Laurent | ef02827 | 2009-04-21 07:56:33 -0700 | [diff] [blame] | 1086 | LOGE_IF(err != status_t(NO_MORE_BUFFERS), "Error obtaining an audio buffer, giving up."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1087 | return false; |
| 1088 | } |
| 1089 | break; |
| 1090 | } |
| 1091 | if (err == status_t(STOPPED)) return false; |
| 1092 | |
| 1093 | // Divide buffer size by 2 to take into account the expansion |
| 1094 | // due to 8 to 16 bit conversion: the callback must fill only half |
| 1095 | // of the destination buffer |
Eric Laurent | 28ad42b | 2009-08-04 10:42:26 -0700 | [diff] [blame] | 1096 | if (mFormat == AudioSystem::PCM_8_BIT && !(mFlags & AudioSystem::OUTPUT_FLAG_DIRECT)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1097 | audioBuffer.size >>= 1; |
| 1098 | } |
| 1099 | |
| 1100 | size_t reqSize = audioBuffer.size; |
| 1101 | mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer); |
| 1102 | writtenSize = audioBuffer.size; |
| 1103 | |
| 1104 | // Sanity check on returned size |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1105 | if (ssize_t(writtenSize) <= 0) { |
| 1106 | // The callback is done filling buffers |
| 1107 | // Keep this thread going to handle timed events and |
| 1108 | // still try to get more data in intervals of WAIT_PERIOD_MS |
| 1109 | // but don't just loop and block the CPU, so wait |
| 1110 | usleep(WAIT_PERIOD_MS*1000); |
| 1111 | break; |
| 1112 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1113 | if (writtenSize > reqSize) writtenSize = reqSize; |
| 1114 | |
Eric Laurent | 28ad42b | 2009-08-04 10:42:26 -0700 | [diff] [blame] | 1115 | if (mFormat == AudioSystem::PCM_8_BIT && !(mFlags & AudioSystem::OUTPUT_FLAG_DIRECT)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1116 | // 8 to 16 bit conversion |
| 1117 | const int8_t *src = audioBuffer.i8 + writtenSize-1; |
| 1118 | int count = writtenSize; |
| 1119 | int16_t *dst = audioBuffer.i16 + writtenSize-1; |
| 1120 | while(count--) { |
| 1121 | *dst-- = (int16_t)(*src--^0x80) << 8; |
| 1122 | } |
| 1123 | writtenSize <<= 1; |
| 1124 | } |
| 1125 | |
| 1126 | audioBuffer.size = writtenSize; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1127 | // NOTE: mCblk->frameSize is not equal to AudioTrack::frameSize() for |
| 1128 | // 8 bit PCM data: in this case, mCblk->frameSize is based on a sampel size of |
| 1129 | // 16 bit. |
| 1130 | audioBuffer.frameCount = writtenSize/mCblk->frameSize; |
| 1131 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1132 | frames -= audioBuffer.frameCount; |
| 1133 | |
| 1134 | releaseBuffer(&audioBuffer); |
| 1135 | } |
| 1136 | while (frames); |
| 1137 | |
| 1138 | if (frames == 0) { |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1139 | mRemainingFrames = mNotificationFramesAct; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1140 | } else { |
| 1141 | mRemainingFrames = frames; |
| 1142 | } |
| 1143 | return true; |
| 1144 | } |
| 1145 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1146 | // must be called with mLock and cblk.lock held. Callers must also hold strong references on |
| 1147 | // the IAudioTrack and IMemory in case they are recreated here. |
| 1148 | // If the IAudioTrack is successfully restored, the cblk pointer is updated |
| 1149 | status_t AudioTrack::restoreTrack_l(audio_track_cblk_t*& cblk, bool fromStart) |
| 1150 | { |
| 1151 | status_t result; |
| 1152 | |
| 1153 | if (!(cblk->flags & CBLK_RESTORING_MSK)) { |
| 1154 | LOGW("dead IAudioTrack, creating a new one from %s", |
| 1155 | fromStart ? "start()" : "obtainBuffer()"); |
| 1156 | |
| 1157 | cblk->flags |= CBLK_RESTORING_ON; |
Eric Laurent | 6667ac3 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 1158 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1159 | // signal old cblk condition so that other threads waiting for available buffers stop |
| 1160 | // waiting now |
| 1161 | cblk->cv.broadcast(); |
| 1162 | cblk->lock.unlock(); |
| 1163 | |
| 1164 | // if the new IAudioTrack is created, createTrack_l() will modify the |
| 1165 | // following member variables: mAudioTrack, mCblkMemory and mCblk. |
| 1166 | // It will also delete the strong references on previous IAudioTrack and IMemory |
| 1167 | result = createTrack_l(mStreamType, |
| 1168 | cblk->sampleRate, |
| 1169 | mFormat, |
| 1170 | mChannelCount, |
| 1171 | mFrameCount, |
| 1172 | mFlags, |
| 1173 | mSharedBuffer, |
| 1174 | getOutput_l(), |
| 1175 | false); |
| 1176 | |
| 1177 | if (result == NO_ERROR) { |
Eric Laurent | 6667ac3 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 1178 | // restore write index and set other indexes to reflect empty buffer status |
| 1179 | mCblk->user = cblk->user; |
| 1180 | mCblk->server = cblk->user; |
| 1181 | mCblk->userBase = cblk->user; |
| 1182 | mCblk->serverBase = cblk->user; |
| 1183 | // restore loop: this is not guaranteed to succeed if new frame count is not |
| 1184 | // compatible with loop length |
| 1185 | setLoop_l(cblk->loopStart, cblk->loopEnd, cblk->loopCount); |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1186 | if (!fromStart) { |
| 1187 | mCblk->bufferTimeoutMs = MAX_RUN_TIMEOUT_MS; |
| 1188 | } |
Eric Laurent | 6667ac3 | 2011-03-21 11:49:00 -0700 | [diff] [blame] | 1189 | if (mActive) { |
| 1190 | result = mAudioTrack->start(); |
| 1191 | } |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1192 | if (fromStart && result == NO_ERROR) { |
| 1193 | mNewPosition = mCblk->server + mUpdatePeriod; |
| 1194 | } |
| 1195 | } |
| 1196 | if (result != NO_ERROR) { |
| 1197 | mActive = false; |
| 1198 | } |
| 1199 | |
| 1200 | // signal old cblk condition for other threads waiting for restore completion |
| 1201 | cblk->lock.lock(); |
| 1202 | cblk->flags |= CBLK_RESTORED_MSK; |
| 1203 | cblk->cv.broadcast(); |
| 1204 | cblk->lock.unlock(); |
| 1205 | } else { |
| 1206 | if (!(cblk->flags & CBLK_RESTORED_MSK)) { |
| 1207 | LOGW("dead IAudioTrack, waiting for a new one"); |
| 1208 | mLock.unlock(); |
| 1209 | result = cblk->cv.waitRelative(cblk->lock, milliseconds(RESTORE_TIMEOUT_MS)); |
| 1210 | cblk->lock.unlock(); |
| 1211 | mLock.lock(); |
| 1212 | } else { |
| 1213 | LOGW("dead IAudioTrack, already restored"); |
| 1214 | result = NO_ERROR; |
| 1215 | cblk->lock.unlock(); |
| 1216 | } |
| 1217 | if (result != NO_ERROR || mActive == 0) { |
| 1218 | result = status_t(STOPPED); |
| 1219 | } |
| 1220 | } |
| 1221 | LOGV("restoreTrack_l() status %d mActive %d cblk %p, old cblk %p flags %08x old flags %08x", |
| 1222 | result, mActive, mCblk, cblk, mCblk->flags, cblk->flags); |
| 1223 | |
| 1224 | if (result == NO_ERROR) { |
| 1225 | // from now on we switch to the newly created cblk |
| 1226 | cblk = mCblk; |
| 1227 | } |
| 1228 | cblk->lock.lock(); |
| 1229 | |
| 1230 | LOGW_IF(result != NO_ERROR, "restoreTrack_l() error %d", result); |
| 1231 | |
| 1232 | return result; |
| 1233 | } |
| 1234 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1235 | status_t AudioTrack::dump(int fd, const Vector<String16>& args) const |
| 1236 | { |
| 1237 | |
| 1238 | const size_t SIZE = 256; |
| 1239 | char buffer[SIZE]; |
| 1240 | String8 result; |
| 1241 | |
| 1242 | result.append(" AudioTrack::dump\n"); |
| 1243 | snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n", mStreamType, mVolume[0], mVolume[1]); |
| 1244 | result.append(buffer); |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1245 | snprintf(buffer, 255, " format(%d), channel count(%d), frame count(%d)\n", mFormat, mChannelCount, mCblk->frameCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1246 | result.append(buffer); |
Eric Laurent | 88e209d | 2009-07-07 07:10:45 -0700 | [diff] [blame] | 1247 | snprintf(buffer, 255, " sample rate(%d), status(%d), muted(%d)\n", (mCblk == 0) ? 0 : mCblk->sampleRate, mStatus, mMuted); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1248 | result.append(buffer); |
| 1249 | snprintf(buffer, 255, " active(%d), latency (%d)\n", mActive, mLatency); |
| 1250 | result.append(buffer); |
| 1251 | ::write(fd, result.string(), result.size()); |
| 1252 | return NO_ERROR; |
| 1253 | } |
| 1254 | |
| 1255 | // ========================================================================= |
| 1256 | |
| 1257 | AudioTrack::AudioTrackThread::AudioTrackThread(AudioTrack& receiver, bool bCanCallJava) |
| 1258 | : Thread(bCanCallJava), mReceiver(receiver) |
| 1259 | { |
| 1260 | } |
| 1261 | |
| 1262 | bool AudioTrack::AudioTrackThread::threadLoop() |
| 1263 | { |
| 1264 | return mReceiver.processAudioBuffer(this); |
| 1265 | } |
| 1266 | |
| 1267 | status_t AudioTrack::AudioTrackThread::readyToRun() |
| 1268 | { |
| 1269 | return NO_ERROR; |
| 1270 | } |
| 1271 | |
| 1272 | void AudioTrack::AudioTrackThread::onFirstRef() |
| 1273 | { |
| 1274 | } |
| 1275 | |
| 1276 | // ========================================================================= |
| 1277 | |
| 1278 | audio_track_cblk_t::audio_track_cblk_t() |
Mathias Agopian | a729f97 | 2010-03-19 16:14:13 -0700 | [diff] [blame] | 1279 | : lock(Mutex::SHARED), cv(Condition::SHARED), user(0), server(0), |
| 1280 | userBase(0), serverBase(0), buffers(0), frameCount(0), |
| 1281 | loopStart(UINT_MAX), loopEnd(UINT_MAX), loopCount(0), volumeLR(0), |
Eric Laurent | 65b6545 | 2010-06-01 23:49:17 -0700 | [diff] [blame] | 1282 | flags(0), sendLevel(0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1283 | { |
| 1284 | } |
| 1285 | |
| 1286 | uint32_t audio_track_cblk_t::stepUser(uint32_t frameCount) |
| 1287 | { |
| 1288 | uint32_t u = this->user; |
| 1289 | |
| 1290 | u += frameCount; |
| 1291 | // Ensure that user is never ahead of server for AudioRecord |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1292 | if (flags & CBLK_DIRECTION_MSK) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1293 | // If stepServer() has been called once, switch to normal obtainBuffer() timeout period |
| 1294 | if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS-1) { |
| 1295 | bufferTimeoutMs = MAX_RUN_TIMEOUT_MS; |
| 1296 | } |
| 1297 | } else if (u > this->server) { |
| 1298 | LOGW("stepServer occured after track reset"); |
| 1299 | u = this->server; |
| 1300 | } |
| 1301 | |
| 1302 | if (u >= userBase + this->frameCount) { |
| 1303 | userBase += this->frameCount; |
| 1304 | } |
| 1305 | |
| 1306 | this->user = u; |
| 1307 | |
| 1308 | // Clear flow control error condition as new data has been written/read to/from buffer. |
Eric Laurent | 913af0b | 2011-03-17 09:36:51 -0700 | [diff] [blame] | 1309 | if (flags & CBLK_UNDERRUN_MSK) { |
| 1310 | AutoMutex _l(lock); |
| 1311 | if (flags & CBLK_UNDERRUN_MSK) { |
| 1312 | flags &= ~CBLK_UNDERRUN_MSK; |
| 1313 | } |
| 1314 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1315 | |
| 1316 | return u; |
| 1317 | } |
| 1318 | |
| 1319 | bool audio_track_cblk_t::stepServer(uint32_t frameCount) |
| 1320 | { |
| 1321 | // the code below simulates lock-with-timeout |
| 1322 | // we MUST do this to protect the AudioFlinger server |
| 1323 | // as this lock is shared with the client. |
| 1324 | status_t err; |
| 1325 | |
| 1326 | err = lock.tryLock(); |
| 1327 | if (err == -EBUSY) { // just wait a bit |
| 1328 | usleep(1000); |
| 1329 | err = lock.tryLock(); |
| 1330 | } |
| 1331 | if (err != NO_ERROR) { |
| 1332 | // probably, the client just died. |
| 1333 | return false; |
| 1334 | } |
| 1335 | |
| 1336 | uint32_t s = this->server; |
| 1337 | |
| 1338 | s += frameCount; |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1339 | if (flags & CBLK_DIRECTION_MSK) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1340 | // Mark that we have read the first buffer so that next time stepUser() is called |
| 1341 | // we switch to normal obtainBuffer() timeout period |
| 1342 | if (bufferTimeoutMs == MAX_STARTUP_TIMEOUT_MS) { |
Eric Laurent | bda7469 | 2009-11-04 08:27:26 -0800 | [diff] [blame] | 1343 | bufferTimeoutMs = MAX_STARTUP_TIMEOUT_MS - 1; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1344 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1345 | // It is possible that we receive a flush() |
| 1346 | // while the mixer is processing a block: in this case, |
| 1347 | // stepServer() is called After the flush() has reset u & s and |
| 1348 | // we have s > u |
| 1349 | if (s > this->user) { |
| 1350 | LOGW("stepServer occured after track reset"); |
| 1351 | s = this->user; |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | if (s >= loopEnd) { |
| 1356 | LOGW_IF(s > loopEnd, "stepServer: s %u > loopEnd %u", s, loopEnd); |
| 1357 | s = loopStart; |
| 1358 | if (--loopCount == 0) { |
| 1359 | loopEnd = UINT_MAX; |
| 1360 | loopStart = UINT_MAX; |
| 1361 | } |
| 1362 | } |
| 1363 | if (s >= serverBase + this->frameCount) { |
| 1364 | serverBase += this->frameCount; |
| 1365 | } |
| 1366 | |
| 1367 | this->server = s; |
| 1368 | |
Eric Laurent | 421ddc0 | 2011-03-07 14:52:59 -0800 | [diff] [blame] | 1369 | if (!(flags & CBLK_INVALID_MSK)) { |
| 1370 | cv.signal(); |
| 1371 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1372 | lock.unlock(); |
| 1373 | return true; |
| 1374 | } |
| 1375 | |
| 1376 | void* audio_track_cblk_t::buffer(uint32_t offset) const |
| 1377 | { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1378 | return (int8_t *)this->buffers + (offset - userBase) * this->frameSize; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | uint32_t audio_track_cblk_t::framesAvailable() |
| 1382 | { |
| 1383 | Mutex::Autolock _l(lock); |
| 1384 | return framesAvailable_l(); |
| 1385 | } |
| 1386 | |
| 1387 | uint32_t audio_track_cblk_t::framesAvailable_l() |
| 1388 | { |
| 1389 | uint32_t u = this->user; |
| 1390 | uint32_t s = this->server; |
| 1391 | |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1392 | if (flags & CBLK_DIRECTION_MSK) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1393 | uint32_t limit = (s < loopStart) ? s : loopStart; |
| 1394 | return limit + frameCount - u; |
| 1395 | } else { |
| 1396 | return frameCount + u - s; |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | uint32_t audio_track_cblk_t::framesReady() |
| 1401 | { |
| 1402 | uint32_t u = this->user; |
| 1403 | uint32_t s = this->server; |
| 1404 | |
Eric Laurent | eb8f850d | 2010-05-14 03:26:45 -0700 | [diff] [blame] | 1405 | if (flags & CBLK_DIRECTION_MSK) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1406 | if (u < loopEnd) { |
| 1407 | return u - s; |
| 1408 | } else { |
| 1409 | Mutex::Autolock _l(lock); |
| 1410 | if (loopCount >= 0) { |
| 1411 | return (loopEnd - loopStart)*loopCount + u - s; |
| 1412 | } else { |
| 1413 | return UINT_MAX; |
| 1414 | } |
| 1415 | } |
| 1416 | } else { |
| 1417 | return s - u; |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | // ------------------------------------------------------------------------- |
| 1422 | |
| 1423 | }; // namespace android |
| 1424 | |