AudioTrackShared.cpp revision 7db7df0e8d9d7cee8ba374468cdbfa0108e3337c
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#define LOG_TAG "AudioTrackShared" 18//#define LOG_NDEBUG 0 19 20#include <private/media/AudioTrackShared.h> 21#include <utils/Log.h> 22extern "C" { 23#include "../private/bionic_futex.h" 24} 25 26namespace android { 27 28audio_track_cblk_t::audio_track_cblk_t() 29 : server(0), frameCount_(0), mFutex(0), mMinimum(0), 30 mVolumeLR(0x10001000), mSampleRate(0), mSendLevel(0), mName(0), flags(0) 31{ 32 memset(&u, 0, sizeof(u)); 33} 34 35// --------------------------------------------------------------------------- 36 37Proxy::Proxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, size_t frameSize, 38 bool isOut, bool clientInServer) 39 : mCblk(cblk), mBuffers(buffers), mFrameCount(frameCount), mFrameSize(frameSize), 40 mFrameCountP2(roundup(frameCount)), mIsOut(isOut), mClientInServer(clientInServer), 41 mIsShutdown(false), mUnreleased(0) 42{ 43} 44 45// --------------------------------------------------------------------------- 46 47ClientProxy::ClientProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, 48 size_t frameSize, bool isOut, bool clientInServer) 49 : Proxy(cblk, buffers, frameCount, frameSize, isOut, clientInServer), mEpoch(0) 50{ 51} 52 53const struct timespec ClientProxy::kForever = {INT_MAX /*tv_sec*/, 0 /*tv_nsec*/}; 54const struct timespec ClientProxy::kNonBlocking = {0 /*tv_sec*/, 0 /*tv_nsec*/}; 55 56#define MEASURE_NS 10000000 // attempt to provide accurate timeouts if requested >= MEASURE_NS 57 58// To facilitate quicker recovery from server failure, this value limits the timeout per each futex 59// wait. However it does not protect infinite timeouts. If defined to be zero, there is no limit. 60// FIXME May not be compatible with audio tunneling requirements where timeout should be in the 61// order of minutes. 62#define MAX_SEC 5 63 64status_t ClientProxy::obtainBuffer(Buffer* buffer, const struct timespec *requested, 65 struct timespec *elapsed) 66{ 67 LOG_ALWAYS_FATAL_IF(buffer == NULL || buffer->mFrameCount == 0); 68 struct timespec total; // total elapsed time spent waiting 69 total.tv_sec = 0; 70 total.tv_nsec = 0; 71 bool measure = elapsed != NULL; // whether to measure total elapsed time spent waiting 72 73 status_t status; 74 enum { 75 TIMEOUT_ZERO, // requested == NULL || *requested == 0 76 TIMEOUT_INFINITE, // *requested == infinity 77 TIMEOUT_FINITE, // 0 < *requested < infinity 78 TIMEOUT_CONTINUE, // additional chances after TIMEOUT_FINITE 79 } timeout; 80 if (requested == NULL) { 81 timeout = TIMEOUT_ZERO; 82 } else if (requested->tv_sec == 0 && requested->tv_nsec == 0) { 83 timeout = TIMEOUT_ZERO; 84 } else if (requested->tv_sec == INT_MAX) { 85 timeout = TIMEOUT_INFINITE; 86 } else { 87 timeout = TIMEOUT_FINITE; 88 if (requested->tv_sec > 0 || requested->tv_nsec >= MEASURE_NS) { 89 measure = true; 90 } 91 } 92 struct timespec before; 93 bool beforeIsValid = false; 94 audio_track_cblk_t* cblk = mCblk; 95 bool ignoreInitialPendingInterrupt = true; 96 // check for shared memory corruption 97 if (mIsShutdown) { 98 status = NO_INIT; 99 goto end; 100 } 101 for (;;) { 102 int32_t flags = android_atomic_and(~CBLK_INTERRUPT, &cblk->flags); 103 // check for track invalidation by server, or server death detection 104 if (flags & CBLK_INVALID) { 105 ALOGV("Track invalidated"); 106 status = DEAD_OBJECT; 107 goto end; 108 } 109 // check for obtainBuffer interrupted by client 110 if (!ignoreInitialPendingInterrupt && (flags & CBLK_INTERRUPT)) { 111 ALOGV("obtainBuffer() interrupted by client"); 112 status = -EINTR; 113 goto end; 114 } 115 ignoreInitialPendingInterrupt = false; 116 // compute number of frames available to write (AudioTrack) or read (AudioRecord) 117 int32_t front; 118 int32_t rear; 119 if (mIsOut) { 120 // The barrier following the read of mFront is probably redundant. 121 // We're about to perform a conditional branch based on 'filled', 122 // which will force the processor to observe the read of mFront 123 // prior to allowing data writes starting at mRaw. 124 // However, the processor may support speculative execution, 125 // and be unable to undo speculative writes into shared memory. 126 // The barrier will prevent such speculative execution. 127 front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront); 128 rear = cblk->u.mStreaming.mRear; 129 } else { 130 // On the other hand, this barrier is required. 131 rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear); 132 front = cblk->u.mStreaming.mFront; 133 } 134 ssize_t filled = rear - front; 135 // pipe should not be overfull 136 if (!(0 <= filled && (size_t) filled <= mFrameCount)) { 137 ALOGE("Shared memory control block is corrupt (filled=%d); shutting down", filled); 138 mIsShutdown = true; 139 status = NO_INIT; 140 goto end; 141 } 142 // don't allow filling pipe beyond the nominal size 143 size_t avail = mIsOut ? mFrameCount - filled : filled; 144 if (avail > 0) { 145 // 'avail' may be non-contiguous, so return only the first contiguous chunk 146 size_t part1; 147 if (mIsOut) { 148 rear &= mFrameCountP2 - 1; 149 part1 = mFrameCountP2 - rear; 150 } else { 151 front &= mFrameCountP2 - 1; 152 part1 = mFrameCountP2 - front; 153 } 154 if (part1 > avail) { 155 part1 = avail; 156 } 157 if (part1 > buffer->mFrameCount) { 158 part1 = buffer->mFrameCount; 159 } 160 buffer->mFrameCount = part1; 161 buffer->mRaw = part1 > 0 ? 162 &((char *) mBuffers)[(mIsOut ? rear : front) * mFrameSize] : NULL; 163 buffer->mNonContig = avail - part1; 164 mUnreleased = part1; 165 status = NO_ERROR; 166 break; 167 } 168 struct timespec remaining; 169 const struct timespec *ts; 170 switch (timeout) { 171 case TIMEOUT_ZERO: 172 status = WOULD_BLOCK; 173 goto end; 174 case TIMEOUT_INFINITE: 175 ts = NULL; 176 break; 177 case TIMEOUT_FINITE: 178 timeout = TIMEOUT_CONTINUE; 179 if (MAX_SEC == 0) { 180 ts = requested; 181 break; 182 } 183 // fall through 184 case TIMEOUT_CONTINUE: 185 // FIXME we do not retry if requested < 10ms? needs documentation on this state machine 186 if (!measure || requested->tv_sec < total.tv_sec || 187 (requested->tv_sec == total.tv_sec && requested->tv_nsec <= total.tv_nsec)) { 188 status = TIMED_OUT; 189 goto end; 190 } 191 remaining.tv_sec = requested->tv_sec - total.tv_sec; 192 if ((remaining.tv_nsec = requested->tv_nsec - total.tv_nsec) < 0) { 193 remaining.tv_nsec += 1000000000; 194 remaining.tv_sec++; 195 } 196 if (0 < MAX_SEC && MAX_SEC < remaining.tv_sec) { 197 remaining.tv_sec = MAX_SEC; 198 remaining.tv_nsec = 0; 199 } 200 ts = &remaining; 201 break; 202 default: 203 LOG_FATAL("%s timeout=%d", timeout); 204 ts = NULL; 205 break; 206 } 207 int32_t old = android_atomic_and(~CBLK_FUTEX_WAKE, &cblk->mFutex); 208 if (!(old & CBLK_FUTEX_WAKE)) { 209 int rc; 210 if (measure && !beforeIsValid) { 211 clock_gettime(CLOCK_MONOTONIC, &before); 212 beforeIsValid = true; 213 } 214 int ret = __futex_syscall4(&cblk->mFutex, 215 mClientInServer ? FUTEX_WAIT_PRIVATE : FUTEX_WAIT, old & ~CBLK_FUTEX_WAKE, ts); 216 // update total elapsed time spent waiting 217 if (measure) { 218 struct timespec after; 219 clock_gettime(CLOCK_MONOTONIC, &after); 220 total.tv_sec += after.tv_sec - before.tv_sec; 221 long deltaNs = after.tv_nsec - before.tv_nsec; 222 if (deltaNs < 0) { 223 deltaNs += 1000000000; 224 total.tv_sec--; 225 } 226 if ((total.tv_nsec += deltaNs) >= 1000000000) { 227 total.tv_nsec -= 1000000000; 228 total.tv_sec++; 229 } 230 before = after; 231 beforeIsValid = true; 232 } 233 switch (ret) { 234 case 0: // normal wakeup by server, or by binderDied() 235 case -EWOULDBLOCK: // benign race condition with server 236 case -EINTR: // wait was interrupted by signal or other spurious wakeup 237 case -ETIMEDOUT: // time-out expired 238 // FIXME these error/non-0 status are being dropped 239 break; 240 default: 241 ALOGE("%s unexpected error %d", __func__, ret); 242 status = -ret; 243 goto end; 244 } 245 } 246 } 247 248end: 249 if (status != NO_ERROR) { 250 buffer->mFrameCount = 0; 251 buffer->mRaw = NULL; 252 buffer->mNonContig = 0; 253 mUnreleased = 0; 254 } 255 if (elapsed != NULL) { 256 *elapsed = total; 257 } 258 if (requested == NULL) { 259 requested = &kNonBlocking; 260 } 261 if (measure) { 262 ALOGV("requested %d.%03d elapsed %d.%03d", requested->tv_sec, requested->tv_nsec / 1000000, 263 total.tv_sec, total.tv_nsec / 1000000); 264 } 265 return status; 266} 267 268void ClientProxy::releaseBuffer(Buffer* buffer) 269{ 270 LOG_ALWAYS_FATAL_IF(buffer == NULL); 271 size_t stepCount = buffer->mFrameCount; 272 if (stepCount == 0 || mIsShutdown) { 273 // prevent accidental re-use of buffer 274 buffer->mFrameCount = 0; 275 buffer->mRaw = NULL; 276 buffer->mNonContig = 0; 277 return; 278 } 279 LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased && mUnreleased <= mFrameCount)); 280 mUnreleased -= stepCount; 281 audio_track_cblk_t* cblk = mCblk; 282 // Both of these barriers are required 283 if (mIsOut) { 284 int32_t rear = cblk->u.mStreaming.mRear; 285 android_atomic_release_store(stepCount + rear, &cblk->u.mStreaming.mRear); 286 } else { 287 int32_t front = cblk->u.mStreaming.mFront; 288 android_atomic_release_store(stepCount + front, &cblk->u.mStreaming.mFront); 289 } 290} 291 292void ClientProxy::binderDied() 293{ 294 audio_track_cblk_t* cblk = mCblk; 295 if (!(android_atomic_or(CBLK_INVALID, &cblk->flags) & CBLK_INVALID)) { 296 // it seems that a FUTEX_WAKE_PRIVATE will not wake a FUTEX_WAIT, even within same process 297 (void) __futex_syscall3(&cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 298 1); 299 } 300} 301 302void ClientProxy::interrupt() 303{ 304 audio_track_cblk_t* cblk = mCblk; 305 if (!(android_atomic_or(CBLK_INTERRUPT, &cblk->flags) & CBLK_INTERRUPT)) { 306 (void) __futex_syscall3(&cblk->mFutex, mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 307 1); 308 } 309} 310 311size_t ClientProxy::getMisalignment() 312{ 313 audio_track_cblk_t* cblk = mCblk; 314 return (mFrameCountP2 - (mIsOut ? cblk->u.mStreaming.mRear : cblk->u.mStreaming.mFront)) & 315 (mFrameCountP2 - 1); 316} 317 318// --------------------------------------------------------------------------- 319 320void AudioTrackClientProxy::flush() 321{ 322 mCblk->u.mStreaming.mFlush++; 323} 324 325// --------------------------------------------------------------------------- 326 327StaticAudioTrackClientProxy::StaticAudioTrackClientProxy(audio_track_cblk_t* cblk, void *buffers, 328 size_t frameCount, size_t frameSize) 329 : AudioTrackClientProxy(cblk, buffers, frameCount, frameSize), 330 mMutator(&cblk->u.mStatic.mSingleStateQueue), mBufferPosition(0) 331{ 332} 333 334void StaticAudioTrackClientProxy::flush() 335{ 336 LOG_FATAL("static flush"); 337} 338 339void StaticAudioTrackClientProxy::setLoop(size_t loopStart, size_t loopEnd, int loopCount) 340{ 341 StaticAudioTrackState newState; 342 newState.mLoopStart = loopStart; 343 newState.mLoopEnd = loopEnd; 344 newState.mLoopCount = loopCount; 345 mBufferPosition = loopStart; 346 (void) mMutator.push(newState); 347} 348 349size_t StaticAudioTrackClientProxy::getBufferPosition() 350{ 351 size_t bufferPosition; 352 if (mMutator.ack()) { 353 bufferPosition = mCblk->u.mStatic.mBufferPosition; 354 if (bufferPosition > mFrameCount) { 355 bufferPosition = mFrameCount; 356 } 357 } else { 358 bufferPosition = mBufferPosition; 359 } 360 return bufferPosition; 361} 362 363// --------------------------------------------------------------------------- 364 365ServerProxy::ServerProxy(audio_track_cblk_t* cblk, void *buffers, size_t frameCount, 366 size_t frameSize, bool isOut, bool clientInServer) 367 : Proxy(cblk, buffers, frameCount, frameSize, isOut, clientInServer), 368 mAvailToClient(0), mFlush(0), mDeferWake(false) 369{ 370} 371 372status_t ServerProxy::obtainBuffer(Buffer* buffer) 373{ 374 LOG_ALWAYS_FATAL_IF(buffer == NULL || buffer->mFrameCount == 0); 375 if (mIsShutdown) { 376 goto no_init; 377 } 378 { 379 audio_track_cblk_t* cblk = mCblk; 380 // compute number of frames available to write (AudioTrack) or read (AudioRecord), 381 // or use previous cached value from framesReady(), with added barrier if it omits. 382 int32_t front; 383 int32_t rear; 384 // See notes on barriers at ClientProxy::obtainBuffer() 385 if (mIsOut) { 386 int32_t flush = cblk->u.mStreaming.mFlush; 387 rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear); 388 if (flush != mFlush) { 389 front = rear; 390 mFlush = flush; 391 } else { 392 front = cblk->u.mStreaming.mFront; 393 } 394 } else { 395 front = android_atomic_acquire_load(&cblk->u.mStreaming.mFront); 396 rear = cblk->u.mStreaming.mRear; 397 } 398 ssize_t filled = rear - front; 399 // pipe should not already be overfull 400 if (!(0 <= filled && (size_t) filled <= mFrameCount)) { 401 ALOGE("Shared memory control block is corrupt (filled=%d); shutting down", filled); 402 mIsShutdown = true; 403 } 404 if (mIsShutdown) { 405 goto no_init; 406 } 407 // don't allow filling pipe beyond the nominal size 408 size_t availToServer; 409 if (mIsOut) { 410 availToServer = filled; 411 mAvailToClient = mFrameCount - filled; 412 } else { 413 availToServer = mFrameCount - filled; 414 mAvailToClient = filled; 415 } 416 // 'availToServer' may be non-contiguous, so return only the first contiguous chunk 417 size_t part1; 418 if (mIsOut) { 419 front &= mFrameCountP2 - 1; 420 part1 = mFrameCountP2 - front; 421 } else { 422 rear &= mFrameCountP2 - 1; 423 part1 = mFrameCountP2 - rear; 424 } 425 if (part1 > availToServer) { 426 part1 = availToServer; 427 } 428 size_t ask = buffer->mFrameCount; 429 if (part1 > ask) { 430 part1 = ask; 431 } 432 // is assignment redundant in some cases? 433 buffer->mFrameCount = part1; 434 buffer->mRaw = part1 > 0 ? 435 &((char *) mBuffers)[(mIsOut ? front : rear) * mFrameSize] : NULL; 436 buffer->mNonContig = availToServer - part1; 437 mUnreleased = part1; 438 // optimization to avoid waking up the client too early 439 // FIXME need to test for recording 440 mDeferWake = part1 < ask && availToServer >= ask; 441 return part1 > 0 ? NO_ERROR : WOULD_BLOCK; 442 } 443no_init: 444 buffer->mFrameCount = 0; 445 buffer->mRaw = NULL; 446 buffer->mNonContig = 0; 447 mUnreleased = 0; 448 return NO_INIT; 449} 450 451void ServerProxy::releaseBuffer(Buffer* buffer) 452{ 453 LOG_ALWAYS_FATAL_IF(buffer == NULL); 454 size_t stepCount = buffer->mFrameCount; 455 if (stepCount == 0 || mIsShutdown) { 456 // prevent accidental re-use of buffer 457 buffer->mFrameCount = 0; 458 buffer->mRaw = NULL; 459 buffer->mNonContig = 0; 460 return; 461 } 462 LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased && mUnreleased <= mFrameCount)); 463 mUnreleased -= stepCount; 464 audio_track_cblk_t* cblk = mCblk; 465 if (mIsOut) { 466 int32_t front = cblk->u.mStreaming.mFront; 467 android_atomic_release_store(stepCount + front, &cblk->u.mStreaming.mFront); 468 } else { 469 int32_t rear = cblk->u.mStreaming.mRear; 470 android_atomic_release_store(stepCount + rear, &cblk->u.mStreaming.mRear); 471 } 472 473 mCblk->server += stepCount; 474 475 size_t half = mFrameCount / 2; 476 if (half == 0) { 477 half = 1; 478 } 479 size_t minimum = cblk->mMinimum; 480 if (minimum == 0) { 481 minimum = mIsOut ? half : 1; 482 } else if (minimum > half) { 483 minimum = half; 484 } 485 // FIXME AudioRecord wakeup needs to be optimized; it currently wakes up client every time 486 if (!mIsOut || (!mDeferWake && mAvailToClient + stepCount >= minimum)) { 487 ALOGV("mAvailToClient=%u stepCount=%u minimum=%u", mAvailToClient, stepCount, minimum); 488 int32_t old = android_atomic_or(CBLK_FUTEX_WAKE, &cblk->mFutex); 489 if (!(old & CBLK_FUTEX_WAKE)) { 490 (void) __futex_syscall3(&cblk->mFutex, 491 mClientInServer ? FUTEX_WAKE_PRIVATE : FUTEX_WAKE, 1); 492 } 493 } 494 495 buffer->mFrameCount = 0; 496 buffer->mRaw = NULL; 497 buffer->mNonContig = 0; 498} 499 500// --------------------------------------------------------------------------- 501 502size_t AudioTrackServerProxy::framesReady() 503{ 504 LOG_ALWAYS_FATAL_IF(!mIsOut); 505 506 if (mIsShutdown) { 507 return 0; 508 } 509 audio_track_cblk_t* cblk = mCblk; 510 // the acquire might not be necessary since not doing a subsequent read 511 int32_t rear = android_atomic_acquire_load(&cblk->u.mStreaming.mRear); 512 ssize_t filled = rear - cblk->u.mStreaming.mFront; 513 // pipe should not already be overfull 514 if (!(0 <= filled && (size_t) filled <= mFrameCount)) { 515 ALOGE("Shared memory control block is corrupt (filled=%d); shutting down", filled); 516 mIsShutdown = true; 517 return 0; 518 } 519 // cache this value for later use by obtainBuffer(), with added barrier 520 // and racy if called by normal mixer thread 521 // ignores flush(), so framesReady() may report a larger mFrameCount than obtainBuffer() 522 return filled; 523} 524 525// --------------------------------------------------------------------------- 526 527StaticAudioTrackServerProxy::StaticAudioTrackServerProxy(audio_track_cblk_t* cblk, void *buffers, 528 size_t frameCount, size_t frameSize) 529 : AudioTrackServerProxy(cblk, buffers, frameCount, frameSize), 530 mObserver(&cblk->u.mStatic.mSingleStateQueue), mPosition(0), 531 mEnd(frameCount), mFramesReadyIsCalledByMultipleThreads(false) 532{ 533 mState.mLoopStart = 0; 534 mState.mLoopEnd = 0; 535 mState.mLoopCount = 0; 536} 537 538void StaticAudioTrackServerProxy::framesReadyIsCalledByMultipleThreads() 539{ 540 mFramesReadyIsCalledByMultipleThreads = true; 541} 542 543size_t StaticAudioTrackServerProxy::framesReady() 544{ 545 // FIXME 546 // This is racy if called by normal mixer thread, 547 // as we're reading 2 independent variables without a lock. 548 // Can't call mObserver.poll(), as we might be called from wrong thread. 549 // If looping is enabled, should return a higher number (since includes non-contiguous). 550 size_t position = mPosition; 551 if (!mFramesReadyIsCalledByMultipleThreads) { 552 ssize_t positionOrStatus = pollPosition(); 553 if (positionOrStatus >= 0) { 554 position = (size_t) positionOrStatus; 555 } 556 } 557 size_t end = mEnd; 558 return position < end ? end - position : 0; 559} 560 561ssize_t StaticAudioTrackServerProxy::pollPosition() 562{ 563 size_t position = mPosition; 564 StaticAudioTrackState state; 565 if (mObserver.poll(state)) { 566 bool valid = false; 567 size_t loopStart = state.mLoopStart; 568 size_t loopEnd = state.mLoopEnd; 569 if (state.mLoopCount == 0) { 570 if (loopStart > mFrameCount) { 571 loopStart = mFrameCount; 572 } 573 // ignore loopEnd 574 mPosition = position = loopStart; 575 mEnd = mFrameCount; 576 mState.mLoopCount = 0; 577 valid = true; 578 } else { 579 if (loopStart < loopEnd && loopEnd <= mFrameCount && 580 loopEnd - loopStart >= MIN_LOOP) { 581 if (!(loopStart <= position && position < loopEnd)) { 582 mPosition = position = loopStart; 583 } 584 mEnd = loopEnd; 585 mState = state; 586 valid = true; 587 } 588 } 589 if (!valid) { 590 ALOGE("%s client pushed an invalid state, shutting down", __func__); 591 mIsShutdown = true; 592 return (ssize_t) NO_INIT; 593 } 594 mCblk->u.mStatic.mBufferPosition = position; 595 } 596 return (ssize_t) position; 597} 598 599status_t StaticAudioTrackServerProxy::obtainBuffer(Buffer* buffer) 600{ 601 if (mIsShutdown) { 602 buffer->mFrameCount = 0; 603 buffer->mRaw = NULL; 604 buffer->mNonContig = 0; 605 mUnreleased = 0; 606 return NO_INIT; 607 } 608 ssize_t positionOrStatus = pollPosition(); 609 if (positionOrStatus < 0) { 610 buffer->mFrameCount = 0; 611 buffer->mRaw = NULL; 612 buffer->mNonContig = 0; 613 mUnreleased = 0; 614 return (status_t) positionOrStatus; 615 } 616 size_t position = (size_t) positionOrStatus; 617 size_t avail; 618 if (position < mEnd) { 619 avail = mEnd - position; 620 size_t wanted = buffer->mFrameCount; 621 if (avail < wanted) { 622 buffer->mFrameCount = avail; 623 } else { 624 avail = wanted; 625 } 626 buffer->mRaw = &((char *) mBuffers)[position * mFrameSize]; 627 } else { 628 avail = 0; 629 buffer->mFrameCount = 0; 630 buffer->mRaw = NULL; 631 } 632 buffer->mNonContig = 0; // FIXME should be > 0 for looping 633 mUnreleased = avail; 634 return NO_ERROR; 635} 636 637void StaticAudioTrackServerProxy::releaseBuffer(Buffer* buffer) 638{ 639 size_t stepCount = buffer->mFrameCount; 640 LOG_ALWAYS_FATAL_IF(!(stepCount <= mUnreleased)); 641 if (stepCount == 0) { 642 // prevent accidental re-use of buffer 643 buffer->mRaw = NULL; 644 buffer->mNonContig = 0; 645 return; 646 } 647 mUnreleased -= stepCount; 648 audio_track_cblk_t* cblk = mCblk; 649 size_t position = mPosition; 650 size_t newPosition = position + stepCount; 651 int32_t setFlags = 0; 652 if (!(position <= newPosition && newPosition <= mFrameCount)) { 653 ALOGW("%s newPosition %u outside [%u, %u]", __func__, newPosition, position, mFrameCount); 654 newPosition = mFrameCount; 655 } else if (mState.mLoopCount != 0 && newPosition == mState.mLoopEnd) { 656 if (mState.mLoopCount == -1 || --mState.mLoopCount != 0) { 657 newPosition = mState.mLoopStart; 658 setFlags = CBLK_LOOP_CYCLE; 659 } else { 660 mEnd = mFrameCount; // this is what allows playback to continue after the loop 661 setFlags = CBLK_LOOP_FINAL; 662 } 663 } 664 if (newPosition == mFrameCount) { 665 setFlags |= CBLK_BUFFER_END; 666 } 667 mPosition = newPosition; 668 669 cblk->server += stepCount; 670 cblk->u.mStatic.mBufferPosition = newPosition; 671 if (setFlags != 0) { 672 (void) android_atomic_or(setFlags, &cblk->flags); 673 // this would be a good place to wake a futex 674 } 675 676 buffer->mFrameCount = 0; 677 buffer->mRaw = NULL; 678 buffer->mNonContig = 0; 679} 680 681// --------------------------------------------------------------------------- 682 683} // namespace android 684