blob: bfbf04a926c0ecb7eebe527bd1e0cb2f27fd0131 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Eric Laurent45fce582009-07-22 11:12:31 -070021//#define USE_SHARED_MEM_BUFFER
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022
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
Dima Zavin24fc2fb2011-04-19 22:30:36 -070030#include <hardware/audio.h>
31
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032#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
Dave Sparksc0e3ddf2009-12-07 12:36:20 -080043SoundPool::SoundPool(int maxChannels, int streamType, int srcQuality)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044{
45 LOGV("SoundPool constructor: maxChannels=%d, streamType=%d, srcQuality=%d",
46 maxChannels, streamType, srcQuality);
47
Dave Sparks3c8704b2009-05-29 19:12:11 -070048 // check limits
49 mMaxChannels = maxChannels;
50 if (mMaxChannels < 1) {
51 mMaxChannels = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 }
Dave Sparks3c8704b2009-05-29 19:12:11 -070053 else if (mMaxChannels > 32) {
54 mMaxChannels = 32;
55 }
56 LOGW_IF(maxChannels != mMaxChannels, "App requested %d channels", maxChannels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
58 mQuit = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 mDecodeThread = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 mStreamType = streamType;
61 mSrcQuality = srcQuality;
62 mAllocated = 0;
63 mNextSampleID = 0;
64 mNextChannelID = 0;
65
Dave Sparksf6e43bf2009-12-08 08:10:42 -080066 mCallback = 0;
67 mUserData = 0;
68
Dave Sparks3c8704b2009-05-29 19:12:11 -070069 mChannelPool = new SoundChannel[mMaxChannels];
70 for (int i = 0; i < mMaxChannels; ++i) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 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);
Eric Laurenta60e2122010-12-28 16:49:07 -080086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 mChannels.clear();
88 if (mChannelPool)
89 delete [] mChannelPool;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 // 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{
Eric Laurentfd8c0e12009-07-31 06:29:13 -0700100 Mutex::Autolock lock(&mRestartLock);
Eric Laurenta60e2122010-12-28 16:49:07 -0800101 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 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114}
115
116int SoundPool::beginThread(void* arg)
117{
118 SoundPool* p = (SoundPool*)arg;
119 return p->run();
120}
121
122int SoundPool::run()
123{
Eric Laurentfd8c0e12009-07-31 06:29:13 -0700124 mRestartLock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 while (!mQuit) {
Eric Laurentfd8c0e12009-07-31 06:29:13 -0700126 mCondition.wait(mRestartLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 LOGV("awake");
128 if (mQuit) break;
129
Eric Laurenta60e2122010-12-28 16:49:07 -0800130 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 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);
Eric Laurenta60e2122010-12-28 16:49:07 -0800151 mRestartLock.unlock();
152 if (channel != 0) {
153 Mutex::Autolock lock(&mLock);
154 channel->nextEvent();
155 }
156 mRestartLock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 if (mQuit) break;
158 }
159 }
160
Eric Laurenta60e2122010-12-28 16:49:07 -0800161 mStop.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 mRestart.clear();
163 mCondition.signal();
Eric Laurentfd8c0e12009-07-31 06:29:13 -0700164 mRestartLock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 LOGV("goodbye");
166 return 0;
167}
168
169void SoundPool::quit()
170{
Eric Laurentfd8c0e12009-07-31 06:29:13 -0700171 mRestartLock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 mQuit = true;
173 mCondition.signal();
Eric Laurentfd8c0e12009-07-31 06:29:13 -0700174 mCondition.wait(mRestartLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 LOGV("return from quit");
Eric Laurentfd8c0e12009-07-31 06:29:13 -0700176 mRestartLock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177}
178
179bool SoundPool::startThreads()
180{
Steve Howard09468db2010-03-11 13:51:52 -0800181 createThreadEtc(beginThread, this, "SoundPool");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 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{
Eric Laurenta60e2122010-12-28 16:49:07 -0800245 LOGV("play sampleID=%d, leftVolume=%f, rightVolume=%f, priority=%d, loop=%d, rate=%f",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 sampleID, leftVolume, rightVolume, priority, loop, rate);
247 sp<Sample> sample;
248 SoundChannel* channel;
249 int channelID;
250
Eric Laurenta60e2122010-12-28 16:49:07 -0800251 Mutex::Autolock lock(&mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
Eric Laurenta60e2122010-12-28 16:49:07 -0800253 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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 }
262
Eric Laurenta60e2122010-12-28 16:49:07 -0800263 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());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 channel->play(sample, channelID, leftVolume, rightVolume, priority, loop, rate);
278 return channelID;
279}
280
Eric Laurenta60e2122010-12-28 16:49:07 -0800281SoundChannel* SoundPool::allocateChannel_l(int priority)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282{
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
Eric Laurenta60e2122010-12-28 16:49:07 -0800310void SoundPool::moveToFront_l(SoundChannel* channel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311{
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
Dave Sparksf992cbb2010-02-09 13:00:09 -0800331void SoundPool::autoPause()
332{
Dave Sparks7bef5a52010-02-17 09:09:24 -0800333 LOGV("autoPause()");
Dave Sparksf992cbb2010-02-09 13:00:09 -0800334 Mutex::Autolock lock(&mLock);
335 for (int i = 0; i < mMaxChannels; ++i) {
336 SoundChannel* channel = &mChannelPool[i];
337 channel->autoPause();
338 }
339}
340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341void 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
Dave Sparksf992cbb2010-02-09 13:00:09 -0800351void SoundPool::autoResume()
352{
Dave Sparks7bef5a52010-02-17 09:09:24 -0800353 LOGV("autoResume()");
Dave Sparksf992cbb2010-02-09 13:00:09 -0800354 Mutex::Autolock lock(&mLock);
355 for (int i = 0; i < mMaxChannels; ++i) {
356 SoundChannel* channel = &mChannelPool[i];
357 channel->autoResume();
358 }
359}
360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361void 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
Eric Laurenta60e2122010-12-28 16:49:07 -0800415void SoundPool::done_l(SoundChannel* channel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416{
Eric Laurenta60e2122010-12-28 16:49:07 -0800417 LOGV("done_l(%d)", channel->channelID());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 // 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");
Eric Laurenta60e2122010-12-28 16:49:07 -0800427 moveToFront_l(channel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 }
429}
430
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800431void 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446void 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
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800496status_t Sample::doLoad()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497{
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);
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800513 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 }
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);
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800520 return - 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 }
522
523 if ((numChannels < 1) || (numChannels > 2)) {
524 LOGE("Sample channel count (%d) out of range", numChannels);
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800525 return - 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 }
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;
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800538 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539}
540
541
542void SoundChannel::init(SoundPool* soundPool)
543{
544 mSoundPool = soundPool;
545}
546
Eric Laurenta60e2122010-12-28 16:49:07 -0800547// call with sound pool lock held
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548void SoundChannel::play(const sp<Sample>& sample, int nextChannelID, float leftVolume,
549 float rightVolume, int priority, int loop, float rate)
550{
551 AudioTrack* oldTrack;
Eric Laurenta60e2122010-12-28 16:49:07 -0800552 AudioTrack* newTrack;
553 status_t status;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554
Eric Laurenta60e2122010-12-28 16:49:07 -0800555 { // scope for the lock
556 Mutex::Autolock lock(&mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557
Eric Laurenta60e2122010-12-28 16:49:07 -0800558 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562
Eric Laurenta60e2122010-12-28 16:49:07 -0800563 // 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 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570
Eric Laurenta60e2122010-12-28 16:49:07 -0800571 // 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/
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700589 ((sample->format() == AUDIO_FORMAT_PCM_16_BIT) ? sizeof(int16_t) : sizeof(uint8_t));
Eric Laurenta60e2122010-12-28 16:49:07 -0800590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591
Eric Laurent9648e4b2009-05-07 03:14:31 -0700592#ifndef USE_SHARED_MEM_BUFFER
Eric Laurenta60e2122010-12-28 16:49:07 -0800593 // Ensure minimum audio buffer size in case of short looped sample
594 if(frameCount < totalFrames) {
595 frameCount = totalFrames;
596 }
Eric Laurent9648e4b2009-05-07 03:14:31 -0700597#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598
Eric Laurenta60e2122010-12-28 16:49:07 -0800599 // 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) ?
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700607 AUDIO_CHANNEL_OUT_STEREO : AUDIO_CHANNEL_OUT_MONO;
Dave Sparks66d28ce2009-12-14 21:12:05 -0800608
Eric Laurenta60e2122010-12-28 16:49:07 -0800609 // do not create a new audio track if current track is compatible with sample parameters
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610#ifdef USE_SHARED_MEM_BUFFER
Eric Laurenta60e2122010-12-28 16:49:07 -0800611 newTrack = new AudioTrack(streamType, sampleRate, sample->format(),
612 channels, sample->getIMemory(), 0, callback, userData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613#else
Eric Laurenta60e2122010-12-28 16:49:07 -0800614 newTrack = new AudioTrack(streamType, sampleRate, sample->format(),
615 channels, frameCount, 0, callback, userData, bufferFrames);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616#endif
Eric Laurenta60e2122010-12-28 16:49:07 -0800617 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 // From now on, AudioTrack callbacks recevieved with previous toggle value will be ignored.
628 mToggle = toggle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 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
Eric Laurenta60e2122010-12-28 16:49:07 -0800645exit:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 LOGV("delete oldTrack %p", oldTrack);
647 delete oldTrack;
Eric Laurenta60e2122010-12-28 16:49:07 -0800648 if (status != NO_ERROR) {
649 delete newTrack;
650 mAudioTrack = NULL;
651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652}
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(int event, void* user, void *info)
686{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 SoundChannel* channel = static_cast<SoundChannel*>((void *)((unsigned long)user & ~1));
688
Eric Laurenta60e2122010-12-28 16:49:07 -0800689 channel->process(event, info, (unsigned long)user & 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690}
691
Eric Laurenta60e2122010-12-28 16:49:07 -0800692void SoundChannel::process(int event, void *info, unsigned long toggle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693{
694 //LOGV("process(%d)", mChannelID);
Eric Laurenta60e2122010-12-28 16:49:07 -0800695
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 sp<Sample> sample = mSample;
712
713// LOGV("SoundChannel::process event %d", event);
714
715 if (event == AudioTrack::EVENT_MORE_DATA) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716
717 // check for stop state
718 if (b->size == 0) return;
719
Eric Laurenta60e2122010-12-28 16:49:07 -0800720 if (mState == IDLE) {
721 b->size = 0;
722 return;
723 }
724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 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);
Eric Laurenta60e2122010-12-28 16:49:07 -0800737// LOGV("fill: q=%p, p=%p, mPos=%u, b->size=%u, count=%d", q, p, mPos, b->size, count);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 } else if (mPos < mAudioBufferSize) {
739 count = mAudioBufferSize - mPos;
740 if (count > b->size) {
741 count = b->size;
742 }
743 memset(q, 0, count);
Eric Laurenta60e2122010-12-28 16:49:07 -0800744// LOGV("fill extra: q=%p, mPos=%u, b->size=%u, count=%d", q, mPos, b->size, count);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 }
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) {
Eric Laurenta60e2122010-12-28 16:49:07 -0800752 LOGV("process %p channel %d EVENT_UNDERRUN", this, mChannelID);
753 mSoundPool->addToStopList(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 } else if (event == AudioTrack::EVENT_LOOP_END) {
Eric Laurenta60e2122010-12-28 16:49:07 -0800755 LOGV("End loop %p channel %d count %d", this, mChannelID, *(int *)info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 }
757}
758
759
760// call with lock held
Eric Laurenta60e2122010-12-28 16:49:07 -0800761bool SoundChannel::doStop_l()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762{
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;
Eric Laurenta60e2122010-12-28 16:49:07 -0800770 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 }
781}
782
Eric Laurenta60e2122010-12-28 16:49:07 -0800783// call with sound pool lock held
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784void SoundChannel::stop()
785{
Eric Laurenta60e2122010-12-28 16:49:07 -0800786 bool stopped;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 {
788 Mutex::Autolock lock(&mLock);
Eric Laurenta60e2122010-12-28 16:49:07 -0800789 stopped = doStop_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 }
Eric Laurenta60e2122010-12-28 16:49:07 -0800791
792 if (stopped) {
793 mSoundPool->done_l(this);
794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795}
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
Dave Sparksf992cbb2010-02-09 13:00:09 -0800808void 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819void SoundChannel::resume()
820{
821 Mutex::Autolock lock(&mLock);
822 if (mState == PAUSED) {
823 LOGV("resume track");
824 mState = PLAYING;
Dave Sparksf992cbb2010-02-09 13:00:09 -0800825 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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 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) {
Eric Laurenta60e2122010-12-28 16:49:07 -0800869 uint32_t loopEnd = mSample->size()/mNumChannels/
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700870 ((mSample->format() == AUDIO_FORMAT_PCM_16_BIT) ? sizeof(int16_t) : sizeof(uint8_t));
Eric Laurenta60e2122010-12-28 16:49:07 -0800871 mAudioTrack->setLoop(0, loopEnd, loop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 mLoop = loop;
873 }
874}
875
876SoundChannel::~SoundChannel()
877{
Eric Laurenta60e2122010-12-28 16:49:07 -0800878 LOGV("SoundChannel destructor %p", this);
879 {
880 Mutex::Autolock lock(&mLock);
881 clearNextEvent();
882 doStop_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 }
Eric Laurenta60e2122010-12-28 16:49:07 -0800884 // 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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887}
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{
Eric Laurenta60e2122010-12-28 16:49:07 -0800898 mSample = sample;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 mChannelID = channelID;
900 mLeftVolume = leftVolume;
901 mRightVolume = rightVolume;
902 mPriority = priority;
903 mLoop = loop;
904 mRate =rate;
905}
906
907} // end namespace android