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_NDEBUG 0 18 #define LOG_TAG "SoundPool" 19 #include <utils/Log.h> 20 21 //#define USE_SHARED_MEM_BUFFER 22 23 // XXX needed for timing latency 24 #include <utils/Timers.h> 25 26 #include <media/AudioTrack.h> 27 #include <media/mediaplayer.h> 28 29 #include <system/audio.h> 30 31 #include "SoundPool.h" 32 #include "SoundPoolThread.h" 33 34 namespace android 35 { 36 37 int kDefaultBufferCount = 4; 38 uint32_t kMaxSampleRate = 48000; 39 uint32_t kDefaultSampleRate = 44100; 40 uint32_t kDefaultFrameCount = 1200; 41 42 SoundPool::SoundPool(int maxChannels, int streamType, int srcQuality) 43 { 44 LOGV("SoundPool constructor: maxChannels=%d, streamType=%d, srcQuality=%d", 45 maxChannels, streamType, srcQuality); 46 47 // check limits 48 mMaxChannels = maxChannels; 49 if (mMaxChannels < 1) { 50 mMaxChannels = 1; 51 } 52 else if (mMaxChannels > 32) { 53 mMaxChannels = 32; 54 } 55 LOGW_IF(maxChannels != mMaxChannels, "App requested %d channels", maxChannels); 56 57 mQuit = false; 58 mDecodeThread = 0; 59 mStreamType = streamType; 60 mSrcQuality = srcQuality; 61 mAllocated = 0; 62 mNextSampleID = 0; 63 mNextChannelID = 0; 64 65 mCallback = 0; 66 mUserData = 0; 67 68 mChannelPool = new SoundChannel[mMaxChannels]; 69 for (int i = 0; i < mMaxChannels; ++i) { 70 mChannelPool[i].init(this); 71 mChannels.push_back(&mChannelPool[i]); 72 } 73 74 // start decode thread 75 startThreads(); 76 } 77 78 SoundPool::~SoundPool() 79 { 80 LOGV("SoundPool destructor"); 81 mDecodeThread->quit(); 82 quit(); 83 84 Mutex::Autolock lock(&mLock); 85 86 mChannels.clear(); 87 if (mChannelPool) 88 delete [] mChannelPool; 89 // clean up samples 90 LOGV("clear samples"); 91 mSamples.clear(); 92 93 if (mDecodeThread) 94 delete mDecodeThread; 95 } 96 97 void SoundPool::addToRestartList(SoundChannel* channel) 98 { 99 Mutex::Autolock lock(&mRestartLock); 100 if (!mQuit) { 101 mRestart.push_back(channel); 102 mCondition.signal(); 103 } 104 } 105 106 void SoundPool::addToStopList(SoundChannel* channel) 107 { 108 Mutex::Autolock lock(&mRestartLock); 109 if (!mQuit) { 110 mStop.push_back(channel); 111 mCondition.signal(); 112 } 113 } 114 115 int SoundPool::beginThread(void* arg) 116 { 117 SoundPool* p = (SoundPool*)arg; 118 return p->run(); 119 } 120 121 int SoundPool::run() 122 { 123 mRestartLock.lock(); 124 while (!mQuit) { 125 mCondition.wait(mRestartLock); 126 LOGV("awake"); 127 if (mQuit) break; 128 129 while (!mStop.empty()) { 130 SoundChannel* channel; 131 LOGV("Getting channel from stop list"); 132 List<SoundChannel* >::iterator iter = mStop.begin(); 133 channel = *iter; 134 mStop.erase(iter); 135 mRestartLock.unlock(); 136 if (channel != 0) { 137 Mutex::Autolock lock(&mLock); 138 channel->stop(); 139 } 140 mRestartLock.lock(); 141 if (mQuit) break; 142 } 143 144 while (!mRestart.empty()) { 145 SoundChannel* channel; 146 LOGV("Getting channel from list"); 147 List<SoundChannel*>::iterator iter = mRestart.begin(); 148 channel = *iter; 149 mRestart.erase(iter); 150 mRestartLock.unlock(); 151 if (channel != 0) { 152 Mutex::Autolock lock(&mLock); 153 channel->nextEvent(); 154 } 155 mRestartLock.lock(); 156 if (mQuit) break; 157 } 158 } 159 160 mStop.clear(); 161 mRestart.clear(); 162 mCondition.signal(); 163 mRestartLock.unlock(); 164 LOGV("goodbye"); 165 return 0; 166 } 167 168 void SoundPool::quit() 169 { 170 mRestartLock.lock(); 171 mQuit = true; 172 mCondition.signal(); 173 mCondition.wait(mRestartLock); 174 LOGV("return from quit"); 175 mRestartLock.unlock(); 176 } 177 178 bool SoundPool::startThreads() 179 { 180 createThreadEtc(beginThread, this, "SoundPool"); 181 if (mDecodeThread == NULL) 182 mDecodeThread = new SoundPoolThread(this); 183 return mDecodeThread != NULL; 184 } 185 186 SoundChannel* SoundPool::findChannel(int channelID) 187 { 188 for (int i = 0; i < mMaxChannels; ++i) { 189 if (mChannelPool[i].channelID() == channelID) { 190 return &mChannelPool[i]; 191 } 192 } 193 return NULL; 194 } 195 196 SoundChannel* SoundPool::findNextChannel(int channelID) 197 { 198 for (int i = 0; i < mMaxChannels; ++i) { 199 if (mChannelPool[i].nextChannelID() == channelID) { 200 return &mChannelPool[i]; 201 } 202 } 203 return NULL; 204 } 205 206 int SoundPool::load(const char* path, int priority) 207 { 208 LOGV("load: path=%s, priority=%d", path, priority); 209 Mutex::Autolock lock(&mLock); 210 sp<Sample> sample = new Sample(++mNextSampleID, path); 211 mSamples.add(sample->sampleID(), sample); 212 doLoad(sample); 213 return sample->sampleID(); 214 } 215 216 int SoundPool::load(int fd, int64_t offset, int64_t length, int priority) 217 { 218 LOGV("load: fd=%d, offset=%lld, length=%lld, priority=%d", 219 fd, offset, length, priority); 220 Mutex::Autolock lock(&mLock); 221 sp<Sample> sample = new Sample(++mNextSampleID, fd, offset, length); 222 mSamples.add(sample->sampleID(), sample); 223 doLoad(sample); 224 return sample->sampleID(); 225 } 226 227 void SoundPool::doLoad(sp<Sample>& sample) 228 { 229 LOGV("doLoad: loading sample sampleID=%d", sample->sampleID()); 230 sample->startLoad(); 231 mDecodeThread->loadSample(sample->sampleID()); 232 } 233 234 bool SoundPool::unload(int sampleID) 235 { 236 LOGV("unload: sampleID=%d", sampleID); 237 Mutex::Autolock lock(&mLock); 238 return mSamples.removeItem(sampleID); 239 } 240 241 int SoundPool::play(int sampleID, float leftVolume, float rightVolume, 242 int priority, int loop, float rate) 243 { 244 LOGV("play sampleID=%d, leftVolume=%f, rightVolume=%f, priority=%d, loop=%d, rate=%f", 245 sampleID, leftVolume, rightVolume, priority, loop, rate); 246 sp<Sample> sample; 247 SoundChannel* channel; 248 int channelID; 249 250 Mutex::Autolock lock(&mLock); 251 252 if (mQuit) { 253 return 0; 254 } 255 // is sample ready? 256 sample = findSample(sampleID); 257 if ((sample == 0) || (sample->state() != Sample::READY)) { 258 LOGW(" sample %d not READY", sampleID); 259 return 0; 260 } 261 262 dump(); 263 264 // allocate a channel 265 channel = allocateChannel_l(priority); 266 267 // no channel allocated - return 0 268 if (!channel) { 269 LOGV("No channel allocated"); 270 return 0; 271 } 272 273 channelID = ++mNextChannelID; 274 275 LOGV("play channel %p state = %d", channel, channel->state()); 276 channel->play(sample, channelID, leftVolume, rightVolume, priority, loop, rate); 277 return channelID; 278 } 279 280 SoundChannel* SoundPool::allocateChannel_l(int priority) 281 { 282 List<SoundChannel*>::iterator iter; 283 SoundChannel* channel = NULL; 284 285 // allocate a channel 286 if (!mChannels.empty()) { 287 iter = mChannels.begin(); 288 if (priority >= (*iter)->priority()) { 289 channel = *iter; 290 mChannels.erase(iter); 291 LOGV("Allocated active channel"); 292 } 293 } 294 295 // update priority and put it back in the list 296 if (channel) { 297 channel->setPriority(priority); 298 for (iter = mChannels.begin(); iter != mChannels.end(); ++iter) { 299 if (priority < (*iter)->priority()) { 300 break; 301 } 302 } 303 mChannels.insert(iter, channel); 304 } 305 return channel; 306 } 307 308 // move a channel from its current position to the front of the list 309 void SoundPool::moveToFront_l(SoundChannel* channel) 310 { 311 for (List<SoundChannel*>::iterator iter = mChannels.begin(); iter != mChannels.end(); ++iter) { 312 if (*iter == channel) { 313 mChannels.erase(iter); 314 mChannels.push_front(channel); 315 break; 316 } 317 } 318 } 319 320 void SoundPool::pause(int channelID) 321 { 322 LOGV("pause(%d)", channelID); 323 Mutex::Autolock lock(&mLock); 324 SoundChannel* channel = findChannel(channelID); 325 if (channel) { 326 channel->pause(); 327 } 328 } 329 330 void SoundPool::autoPause() 331 { 332 LOGV("autoPause()"); 333 Mutex::Autolock lock(&mLock); 334 for (int i = 0; i < mMaxChannels; ++i) { 335 SoundChannel* channel = &mChannelPool[i]; 336 channel->autoPause(); 337 } 338 } 339 340 void SoundPool::resume(int channelID) 341 { 342 LOGV("resume(%d)", channelID); 343 Mutex::Autolock lock(&mLock); 344 SoundChannel* channel = findChannel(channelID); 345 if (channel) { 346 channel->resume(); 347 } 348 } 349 350 void SoundPool::autoResume() 351 { 352 LOGV("autoResume()"); 353 Mutex::Autolock lock(&mLock); 354 for (int i = 0; i < mMaxChannels; ++i) { 355 SoundChannel* channel = &mChannelPool[i]; 356 channel->autoResume(); 357 } 358 } 359 360 void SoundPool::stop(int channelID) 361 { 362 LOGV("stop(%d)", channelID); 363 Mutex::Autolock lock(&mLock); 364 SoundChannel* channel = findChannel(channelID); 365 if (channel) { 366 channel->stop(); 367 } else { 368 channel = findNextChannel(channelID); 369 if (channel) 370 channel->clearNextEvent(); 371 } 372 } 373 374 void SoundPool::setVolume(int channelID, float leftVolume, float rightVolume) 375 { 376 Mutex::Autolock lock(&mLock); 377 SoundChannel* channel = findChannel(channelID); 378 if (channel) { 379 channel->setVolume(leftVolume, rightVolume); 380 } 381 } 382 383 void SoundPool::setPriority(int channelID, int priority) 384 { 385 LOGV("setPriority(%d, %d)", channelID, priority); 386 Mutex::Autolock lock(&mLock); 387 SoundChannel* channel = findChannel(channelID); 388 if (channel) { 389 channel->setPriority(priority); 390 } 391 } 392 393 void SoundPool::setLoop(int channelID, int loop) 394 { 395 LOGV("setLoop(%d, %d)", channelID, loop); 396 Mutex::Autolock lock(&mLock); 397 SoundChannel* channel = findChannel(channelID); 398 if (channel) { 399 channel->setLoop(loop); 400 } 401 } 402 403 void SoundPool::setRate(int channelID, float rate) 404 { 405 LOGV("setRate(%d, %f)", channelID, rate); 406 Mutex::Autolock lock(&mLock); 407 SoundChannel* channel = findChannel(channelID); 408 if (channel) { 409 channel->setRate(rate); 410 } 411 } 412 413 // call with lock held 414 void SoundPool::done_l(SoundChannel* channel) 415 { 416 LOGV("done_l(%d)", channel->channelID()); 417 // if "stolen", play next event 418 if (channel->nextChannelID() != 0) { 419 LOGV("add to restart list"); 420 addToRestartList(channel); 421 } 422 423 // return to idle state 424 else { 425 LOGV("move to front"); 426 moveToFront_l(channel); 427 } 428 } 429 430 void SoundPool::setCallback(SoundPoolCallback* callback, void* user) 431 { 432 Mutex::Autolock lock(&mCallbackLock); 433 mCallback = callback; 434 mUserData = user; 435 } 436 437 void SoundPool::notify(SoundPoolEvent event) 438 { 439 Mutex::Autolock lock(&mCallbackLock); 440 if (mCallback != NULL) { 441 mCallback(event, this, mUserData); 442 } 443 } 444 445 void SoundPool::dump() 446 { 447 for (int i = 0; i < mMaxChannels; ++i) { 448 mChannelPool[i].dump(); 449 } 450 } 451 452 453 Sample::Sample(int sampleID, const char* url) 454 { 455 init(); 456 mSampleID = sampleID; 457 mUrl = strdup(url); 458 LOGV("create sampleID=%d, url=%s", mSampleID, mUrl); 459 } 460 461 Sample::Sample(int sampleID, int fd, int64_t offset, int64_t length) 462 { 463 init(); 464 mSampleID = sampleID; 465 mFd = dup(fd); 466 mOffset = offset; 467 mLength = length; 468 LOGV("create sampleID=%d, fd=%d, offset=%lld, length=%lld", mSampleID, mFd, mLength, mOffset); 469 } 470 471 void Sample::init() 472 { 473 mData = 0; 474 mSize = 0; 475 mRefCount = 0; 476 mSampleID = 0; 477 mState = UNLOADED; 478 mFd = -1; 479 mOffset = 0; 480 mLength = 0; 481 mUrl = 0; 482 } 483 484 Sample::~Sample() 485 { 486 LOGV("Sample::destructor sampleID=%d, fd=%d", mSampleID, mFd); 487 if (mFd > 0) { 488 LOGV("close(%d)", mFd); 489 ::close(mFd); 490 } 491 mData.clear(); 492 delete mUrl; 493 } 494 495 status_t Sample::doLoad() 496 { 497 uint32_t sampleRate; 498 int numChannels; 499 int format; 500 sp<IMemory> p; 501 LOGV("Start decode"); 502 if (mUrl) { 503 p = MediaPlayer::decode(mUrl, &sampleRate, &numChannels, &format); 504 } else { 505 p = MediaPlayer::decode(mFd, mOffset, mLength, &sampleRate, &numChannels, &format); 506 LOGV("close(%d)", mFd); 507 ::close(mFd); 508 mFd = -1; 509 } 510 if (p == 0) { 511 LOGE("Unable to load sample: %s", mUrl); 512 return -1; 513 } 514 LOGV("pointer = %p, size = %u, sampleRate = %u, numChannels = %d", 515 p->pointer(), p->size(), sampleRate, numChannels); 516 517 if (sampleRate > kMaxSampleRate) { 518 LOGE("Sample rate (%u) out of range", sampleRate); 519 return - 1; 520 } 521 522 if ((numChannels < 1) || (numChannels > 2)) { 523 LOGE("Sample channel count (%d) out of range", numChannels); 524 return - 1; 525 } 526 527 //_dumpBuffer(p->pointer(), p->size()); 528 uint8_t* q = static_cast<uint8_t*>(p->pointer()) + p->size() - 10; 529 //_dumpBuffer(q, 10, 10, false); 530 531 mData = p; 532 mSize = p->size(); 533 mSampleRate = sampleRate; 534 mNumChannels = numChannels; 535 mFormat = format; 536 mState = READY; 537 return 0; 538 } 539 540 541 void SoundChannel::init(SoundPool* soundPool) 542 { 543 mSoundPool = soundPool; 544 } 545 546 // call with sound pool lock held 547 void SoundChannel::play(const sp<Sample>& sample, int nextChannelID, float leftVolume, 548 float rightVolume, int priority, int loop, float rate) 549 { 550 AudioTrack* oldTrack; 551 AudioTrack* newTrack; 552 status_t status; 553 554 { // scope for the lock 555 Mutex::Autolock lock(&mLock); 556 557 LOGV("SoundChannel::play %p: sampleID=%d, channelID=%d, leftVolume=%f, rightVolume=%f," 558 " priority=%d, loop=%d, rate=%f", 559 this, sample->sampleID(), nextChannelID, leftVolume, rightVolume, 560 priority, loop, rate); 561 562 // if not idle, this voice is being stolen 563 if (mState != IDLE) { 564 LOGV("channel %d stolen - event queued for channel %d", channelID(), nextChannelID); 565 mNextEvent.set(sample, nextChannelID, leftVolume, rightVolume, priority, loop, rate); 566 stop_l(); 567 return; 568 } 569 570 // initialize track 571 int afFrameCount; 572 int afSampleRate; 573 int streamType = mSoundPool->streamType(); 574 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) { 575 afFrameCount = kDefaultFrameCount; 576 } 577 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) { 578 afSampleRate = kDefaultSampleRate; 579 } 580 int numChannels = sample->numChannels(); 581 uint32_t sampleRate = uint32_t(float(sample->sampleRate()) * rate + 0.5); 582 uint32_t totalFrames = (kDefaultBufferCount * afFrameCount * sampleRate) / afSampleRate; 583 uint32_t bufferFrames = (totalFrames + (kDefaultBufferCount - 1)) / kDefaultBufferCount; 584 uint32_t frameCount = 0; 585 586 if (loop) { 587 frameCount = sample->size()/numChannels/ 588 ((sample->format() == AUDIO_FORMAT_PCM_16_BIT) ? sizeof(int16_t) : sizeof(uint8_t)); 589 } 590 591 #ifndef USE_SHARED_MEM_BUFFER 592 // Ensure minimum audio buffer size in case of short looped sample 593 if(frameCount < totalFrames) { 594 frameCount = totalFrames; 595 } 596 #endif 597 598 // mToggle toggles each time a track is started on a given channel. 599 // The toggle is concatenated with the SoundChannel address and passed to AudioTrack 600 // as callback user data. This enables the detection of callbacks received from the old 601 // audio track while the new one is being started and avoids processing them with 602 // wrong audio audio buffer size (mAudioBufferSize) 603 unsigned long toggle = mToggle ^ 1; 604 void *userData = (void *)((unsigned long)this | toggle); 605 uint32_t channels = (numChannels == 2) ? 606 AUDIO_CHANNEL_OUT_STEREO : AUDIO_CHANNEL_OUT_MONO; 607 608 // do not create a new audio track if current track is compatible with sample parameters 609 #ifdef USE_SHARED_MEM_BUFFER 610 newTrack = new AudioTrack(streamType, sampleRate, sample->format(), 611 channels, sample->getIMemory(), 0, callback, userData); 612 #else 613 newTrack = new AudioTrack(streamType, sampleRate, sample->format(), 614 channels, frameCount, 0, callback, userData, bufferFrames); 615 #endif 616 oldTrack = mAudioTrack; 617 status = newTrack->initCheck(); 618 if (status != NO_ERROR) { 619 LOGE("Error creating AudioTrack"); 620 goto exit; 621 } 622 LOGV("setVolume %p", newTrack); 623 newTrack->setVolume(leftVolume, rightVolume); 624 newTrack->setLoop(0, frameCount, loop); 625 626 // From now on, AudioTrack callbacks recevieved with previous toggle value will be ignored. 627 mToggle = toggle; 628 mAudioTrack = newTrack; 629 mPos = 0; 630 mSample = sample; 631 mChannelID = nextChannelID; 632 mPriority = priority; 633 mLoop = loop; 634 mLeftVolume = leftVolume; 635 mRightVolume = rightVolume; 636 mNumChannels = numChannels; 637 mRate = rate; 638 clearNextEvent(); 639 mState = PLAYING; 640 mAudioTrack->start(); 641 mAudioBufferSize = newTrack->frameCount()*newTrack->frameSize(); 642 } 643 644 exit: 645 LOGV("delete oldTrack %p", oldTrack); 646 delete oldTrack; 647 if (status != NO_ERROR) { 648 delete newTrack; 649 mAudioTrack = NULL; 650 } 651 } 652 653 void SoundChannel::nextEvent() 654 { 655 sp<Sample> sample; 656 int nextChannelID; 657 float leftVolume; 658 float rightVolume; 659 int priority; 660 int loop; 661 float rate; 662 663 // check for valid event 664 { 665 Mutex::Autolock lock(&mLock); 666 nextChannelID = mNextEvent.channelID(); 667 if (nextChannelID == 0) { 668 LOGV("stolen channel has no event"); 669 return; 670 } 671 672 sample = mNextEvent.sample(); 673 leftVolume = mNextEvent.leftVolume(); 674 rightVolume = mNextEvent.rightVolume(); 675 priority = mNextEvent.priority(); 676 loop = mNextEvent.loop(); 677 rate = mNextEvent.rate(); 678 } 679 680 LOGV("Starting stolen channel %d -> %d", channelID(), nextChannelID); 681 play(sample, nextChannelID, leftVolume, rightVolume, priority, loop, rate); 682 } 683 684 void SoundChannel::callback(int event, void* user, void *info) 685 { 686 SoundChannel* channel = static_cast<SoundChannel*>((void *)((unsigned long)user & ~1)); 687 688 channel->process(event, info, (unsigned long)user & 1); 689 } 690 691 void SoundChannel::process(int event, void *info, unsigned long toggle) 692 { 693 //LOGV("process(%d)", mChannelID); 694 695 Mutex::Autolock lock(&mLock); 696 697 AudioTrack::Buffer* b = NULL; 698 if (event == AudioTrack::EVENT_MORE_DATA) { 699 b = static_cast<AudioTrack::Buffer *>(info); 700 } 701 702 if (mToggle != toggle) { 703 LOGV("process wrong toggle %p channel %d", this, mChannelID); 704 if (b != NULL) { 705 b->size = 0; 706 } 707 return; 708 } 709 710 sp<Sample> sample = mSample; 711 712 // LOGV("SoundChannel::process event %d", event); 713 714 if (event == AudioTrack::EVENT_MORE_DATA) { 715 716 // check for stop state 717 if (b->size == 0) return; 718 719 if (mState == IDLE) { 720 b->size = 0; 721 return; 722 } 723 724 if (sample != 0) { 725 // fill buffer 726 uint8_t* q = (uint8_t*) b->i8; 727 size_t count = 0; 728 729 if (mPos < (int)sample->size()) { 730 uint8_t* p = sample->data() + mPos; 731 count = sample->size() - mPos; 732 if (count > b->size) { 733 count = b->size; 734 } 735 memcpy(q, p, count); 736 // LOGV("fill: q=%p, p=%p, mPos=%u, b->size=%u, count=%d", q, p, mPos, b->size, count); 737 } else if (mPos < mAudioBufferSize) { 738 count = mAudioBufferSize - mPos; 739 if (count > b->size) { 740 count = b->size; 741 } 742 memset(q, 0, count); 743 // LOGV("fill extra: q=%p, mPos=%u, b->size=%u, count=%d", q, mPos, b->size, count); 744 } 745 746 mPos += count; 747 b->size = count; 748 //LOGV("buffer=%p, [0]=%d", b->i16, b->i16[0]); 749 } 750 } else if (event == AudioTrack::EVENT_UNDERRUN) { 751 LOGV("process %p channel %d EVENT_UNDERRUN", this, mChannelID); 752 mSoundPool->addToStopList(this); 753 } else if (event == AudioTrack::EVENT_LOOP_END) { 754 LOGV("End loop %p channel %d count %d", this, mChannelID, *(int *)info); 755 } 756 } 757 758 759 // call with lock held 760 bool SoundChannel::doStop_l() 761 { 762 if (mState != IDLE) { 763 setVolume_l(0, 0); 764 LOGV("stop"); 765 mAudioTrack->stop(); 766 mSample.clear(); 767 mState = IDLE; 768 mPriority = IDLE_PRIORITY; 769 return true; 770 } 771 return false; 772 } 773 774 // call with lock held and sound pool lock held 775 void SoundChannel::stop_l() 776 { 777 if (doStop_l()) { 778 mSoundPool->done_l(this); 779 } 780 } 781 782 // call with sound pool lock held 783 void SoundChannel::stop() 784 { 785 bool stopped; 786 { 787 Mutex::Autolock lock(&mLock); 788 stopped = doStop_l(); 789 } 790 791 if (stopped) { 792 mSoundPool->done_l(this); 793 } 794 } 795 796 //FIXME: Pause is a little broken right now 797 void SoundChannel::pause() 798 { 799 Mutex::Autolock lock(&mLock); 800 if (mState == PLAYING) { 801 LOGV("pause track"); 802 mState = PAUSED; 803 mAudioTrack->pause(); 804 } 805 } 806 807 void SoundChannel::autoPause() 808 { 809 Mutex::Autolock lock(&mLock); 810 if (mState == PLAYING) { 811 LOGV("pause track"); 812 mState = PAUSED; 813 mAutoPaused = true; 814 mAudioTrack->pause(); 815 } 816 } 817 818 void SoundChannel::resume() 819 { 820 Mutex::Autolock lock(&mLock); 821 if (mState == PAUSED) { 822 LOGV("resume track"); 823 mState = PLAYING; 824 mAutoPaused = false; 825 mAudioTrack->start(); 826 } 827 } 828 829 void SoundChannel::autoResume() 830 { 831 Mutex::Autolock lock(&mLock); 832 if (mAutoPaused && (mState == PAUSED)) { 833 LOGV("resume track"); 834 mState = PLAYING; 835 mAutoPaused = false; 836 mAudioTrack->start(); 837 } 838 } 839 840 void SoundChannel::setRate(float rate) 841 { 842 Mutex::Autolock lock(&mLock); 843 if (mAudioTrack != 0 && mSample.get() != 0) { 844 uint32_t sampleRate = uint32_t(float(mSample->sampleRate()) * rate + 0.5); 845 mAudioTrack->setSampleRate(sampleRate); 846 mRate = rate; 847 } 848 } 849 850 // call with lock held 851 void SoundChannel::setVolume_l(float leftVolume, float rightVolume) 852 { 853 mLeftVolume = leftVolume; 854 mRightVolume = rightVolume; 855 if (mAudioTrack != 0) mAudioTrack->setVolume(leftVolume, rightVolume); 856 } 857 858 void SoundChannel::setVolume(float leftVolume, float rightVolume) 859 { 860 Mutex::Autolock lock(&mLock); 861 setVolume_l(leftVolume, rightVolume); 862 } 863 864 void SoundChannel::setLoop(int loop) 865 { 866 Mutex::Autolock lock(&mLock); 867 if (mAudioTrack != 0 && mSample.get() != 0) { 868 uint32_t loopEnd = mSample->size()/mNumChannels/ 869 ((mSample->format() == AUDIO_FORMAT_PCM_16_BIT) ? sizeof(int16_t) : sizeof(uint8_t)); 870 mAudioTrack->setLoop(0, loopEnd, loop); 871 mLoop = loop; 872 } 873 } 874 875 SoundChannel::~SoundChannel() 876 { 877 LOGV("SoundChannel destructor %p", this); 878 { 879 Mutex::Autolock lock(&mLock); 880 clearNextEvent(); 881 doStop_l(); 882 } 883 // do not call AudioTrack destructor with mLock held as it will wait for the AudioTrack 884 // callback thread to exit which may need to execute process() and acquire the mLock. 885 delete mAudioTrack; 886 } 887 888 void SoundChannel::dump() 889 { 890 LOGV("mState = %d mChannelID=%d, mNumChannels=%d, mPos = %d, mPriority=%d, mLoop=%d", 891 mState, mChannelID, mNumChannels, mPos, mPriority, mLoop); 892 } 893 894 void SoundEvent::set(const sp<Sample>& sample, int channelID, float leftVolume, 895 float rightVolume, int priority, int loop, float rate) 896 { 897 mSample = sample; 898 mChannelID = channelID; 899 mLeftVolume = leftVolume; 900 mRightVolume = rightVolume; 901 mPriority = priority; 902 mLoop = loop; 903 mRate =rate; 904 } 905 906 } // end namespace android 907