PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/frameworks/av/media/libmedia/SoundPool.cpp

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