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