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