blob: 4ffb2c05c4bafb0dace5251942c5a6542165ed02 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026#include <media/AudioTrack.h>
27#include <media/mediaplayer.h>
28
Dima Zavin34bb4192011-05-11 14:15:23 -070029#include <system/audio.h>
Dima Zavin24fc2fb2011-04-19 22:30:36 -070030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031#include "SoundPool.h"
32#include "SoundPoolThread.h"
33
34namespace android
35{
36
37int kDefaultBufferCount = 4;
38uint32_t kMaxSampleRate = 48000;
39uint32_t kDefaultSampleRate = 44100;
40uint32_t kDefaultFrameCount = 1200;
41
Dave Sparksc0e3ddf2009-12-07 12:36:20 -080042SoundPool::SoundPool(int maxChannels, int streamType, int srcQuality)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043{
44 LOGV("SoundPool constructor: maxChannels=%d, streamType=%d, srcQuality=%d",
45 maxChannels, streamType, srcQuality);
46
Dave Sparks3c8704b2009-05-29 19:12:11 -070047 // check limits
48 mMaxChannels = maxChannels;
49 if (mMaxChannels < 1) {
50 mMaxChannels = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 }
Dave Sparks3c8704b2009-05-29 19:12:11 -070052 else if (mMaxChannels > 32) {
53 mMaxChannels = 32;
54 }
55 LOGW_IF(maxChannels != mMaxChannels, "App requested %d channels", maxChannels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
57 mQuit = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 mDecodeThread = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 mStreamType = streamType;
60 mSrcQuality = srcQuality;
61 mAllocated = 0;
62 mNextSampleID = 0;
63 mNextChannelID = 0;
64
Dave Sparksf6e43bf2009-12-08 08:10:42 -080065 mCallback = 0;
66 mUserData = 0;
67
Dave Sparks3c8704b2009-05-29 19:12:11 -070068 mChannelPool = new SoundChannel[mMaxChannels];
69 for (int i = 0; i < mMaxChannels; ++i) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 mChannelPool[i].init(this);
71 mChannels.push_back(&mChannelPool[i]);
72 }
73
74 // start decode thread
75 startThreads();
76}
77
78SoundPool::~SoundPool()
79{
80 LOGV("SoundPool destructor");
81 mDecodeThread->quit();
82 quit();
83
84 Mutex::Autolock lock(&mLock);
Eric Laurenta60e2122010-12-28 16:49:07 -080085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 mChannels.clear();
87 if (mChannelPool)
88 delete [] mChannelPool;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 // clean up samples
90 LOGV("clear samples");
91 mSamples.clear();
92
93 if (mDecodeThread)
94 delete mDecodeThread;
95}
96
97void SoundPool::addToRestartList(SoundChannel* channel)
98{
Eric Laurentfd8c0e12009-07-31 06:29:13 -070099 Mutex::Autolock lock(&mRestartLock);
Eric Laurenta60e2122010-12-28 16:49:07 -0800100 if (!mQuit) {
101 mRestart.push_back(channel);
102 mCondition.signal();
103 }
104}
105
106void SoundPool::addToStopList(SoundChannel* channel)
107{
108 Mutex::Autolock lock(&mRestartLock);
109 if (!mQuit) {
110 mStop.push_back(channel);
111 mCondition.signal();
112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113}
114
115int SoundPool::beginThread(void* arg)
116{
117 SoundPool* p = (SoundPool*)arg;
118 return p->run();
119}
120
121int SoundPool::run()
122{
Eric Laurentfd8c0e12009-07-31 06:29:13 -0700123 mRestartLock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 while (!mQuit) {
Eric Laurentfd8c0e12009-07-31 06:29:13 -0700125 mCondition.wait(mRestartLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 LOGV("awake");
127 if (mQuit) break;
128
Eric Laurenta60e2122010-12-28 16:49:07 -0800129 while (!mStop.empty()) {
130 SoundChannel* channel;
131 LOGV("Getting channel from stop list");
132 List<SoundChannel* >::iterator iter = mStop.begin();
133 channel = *iter;
134 mStop.erase(iter);
135 mRestartLock.unlock();
136 if (channel != 0) {
137 Mutex::Autolock lock(&mLock);
138 channel->stop();
139 }
140 mRestartLock.lock();
141 if (mQuit) break;
142 }
143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 while (!mRestart.empty()) {
145 SoundChannel* channel;
146 LOGV("Getting channel from list");
147 List<SoundChannel*>::iterator iter = mRestart.begin();
148 channel = *iter;
149 mRestart.erase(iter);
Eric Laurenta60e2122010-12-28 16:49:07 -0800150 mRestartLock.unlock();
151 if (channel != 0) {
152 Mutex::Autolock lock(&mLock);
153 channel->nextEvent();
154 }
155 mRestartLock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 if (mQuit) break;
157 }
158 }
159
Eric Laurenta60e2122010-12-28 16:49:07 -0800160 mStop.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 mRestart.clear();
162 mCondition.signal();
Eric Laurentfd8c0e12009-07-31 06:29:13 -0700163 mRestartLock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 LOGV("goodbye");
165 return 0;
166}
167
168void SoundPool::quit()
169{
Eric Laurentfd8c0e12009-07-31 06:29:13 -0700170 mRestartLock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 mQuit = true;
172 mCondition.signal();
Eric Laurentfd8c0e12009-07-31 06:29:13 -0700173 mCondition.wait(mRestartLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 LOGV("return from quit");
Eric Laurentfd8c0e12009-07-31 06:29:13 -0700175 mRestartLock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176}
177
178bool SoundPool::startThreads()
179{
Steve Howard09468db2010-03-11 13:51:52 -0800180 createThreadEtc(beginThread, this, "SoundPool");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 if (mDecodeThread == NULL)
182 mDecodeThread = new SoundPoolThread(this);
183 return mDecodeThread != NULL;
184}
185
186SoundChannel* SoundPool::findChannel(int channelID)
187{
188 for (int i = 0; i < mMaxChannels; ++i) {
189 if (mChannelPool[i].channelID() == channelID) {
190 return &mChannelPool[i];
191 }
192 }
193 return NULL;
194}
195
196SoundChannel* SoundPool::findNextChannel(int channelID)
197{
198 for (int i = 0; i < mMaxChannels; ++i) {
199 if (mChannelPool[i].nextChannelID() == channelID) {
200 return &mChannelPool[i];
201 }
202 }
203 return NULL;
204}
205
206int SoundPool::load(const char* path, int priority)
207{
208 LOGV("load: path=%s, priority=%d", path, priority);
209 Mutex::Autolock lock(&mLock);
210 sp<Sample> sample = new Sample(++mNextSampleID, path);
211 mSamples.add(sample->sampleID(), sample);
212 doLoad(sample);
213 return sample->sampleID();
214}
215
216int SoundPool::load(int fd, int64_t offset, int64_t length, int priority)
217{
218 LOGV("load: fd=%d, offset=%lld, length=%lld, priority=%d",
219 fd, offset, length, priority);
220 Mutex::Autolock lock(&mLock);
221 sp<Sample> sample = new Sample(++mNextSampleID, fd, offset, length);
222 mSamples.add(sample->sampleID(), sample);
223 doLoad(sample);
224 return sample->sampleID();
225}
226
227void SoundPool::doLoad(sp<Sample>& sample)
228{
229 LOGV("doLoad: loading sample sampleID=%d", sample->sampleID());
230 sample->startLoad();
231 mDecodeThread->loadSample(sample->sampleID());
232}
233
234bool SoundPool::unload(int sampleID)
235{
236 LOGV("unload: sampleID=%d", sampleID);
237 Mutex::Autolock lock(&mLock);
238 return mSamples.removeItem(sampleID);
239}
240
241int SoundPool::play(int sampleID, float leftVolume, float rightVolume,
242 int priority, int loop, float rate)
243{
Eric Laurenta60e2122010-12-28 16:49:07 -0800244 LOGV("play sampleID=%d, leftVolume=%f, rightVolume=%f, priority=%d, loop=%d, rate=%f",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 sampleID, leftVolume, rightVolume, priority, loop, rate);
246 sp<Sample> sample;
247 SoundChannel* channel;
248 int channelID;
249
Eric Laurenta60e2122010-12-28 16:49:07 -0800250 Mutex::Autolock lock(&mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251
Eric Laurenta60e2122010-12-28 16:49:07 -0800252 if (mQuit) {
253 return 0;
254 }
255 // is sample ready?
256 sample = findSample(sampleID);
257 if ((sample == 0) || (sample->state() != Sample::READY)) {
258 LOGW(" sample %d not READY", sampleID);
259 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 }
261
Eric Laurenta60e2122010-12-28 16:49:07 -0800262 dump();
263
264 // allocate a channel
265 channel = allocateChannel_l(priority);
266
267 // no channel allocated - return 0
268 if (!channel) {
269 LOGV("No channel allocated");
270 return 0;
271 }
272
273 channelID = ++mNextChannelID;
274
275 LOGV("play channel %p state = %d", channel, channel->state());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 channel->play(sample, channelID, leftVolume, rightVolume, priority, loop, rate);
277 return channelID;
278}
279
Eric Laurenta60e2122010-12-28 16:49:07 -0800280SoundChannel* SoundPool::allocateChannel_l(int priority)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281{
282 List<SoundChannel*>::iterator iter;
283 SoundChannel* channel = NULL;
284
285 // allocate a channel
286 if (!mChannels.empty()) {
287 iter = mChannels.begin();
288 if (priority >= (*iter)->priority()) {
289 channel = *iter;
290 mChannels.erase(iter);
291 LOGV("Allocated active channel");
292 }
293 }
294
295 // update priority and put it back in the list
296 if (channel) {
297 channel->setPriority(priority);
298 for (iter = mChannels.begin(); iter != mChannels.end(); ++iter) {
299 if (priority < (*iter)->priority()) {
300 break;
301 }
302 }
303 mChannels.insert(iter, channel);
304 }
305 return channel;
306}
307
308// move a channel from its current position to the front of the list
Eric Laurenta60e2122010-12-28 16:49:07 -0800309void SoundPool::moveToFront_l(SoundChannel* channel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310{
311 for (List<SoundChannel*>::iterator iter = mChannels.begin(); iter != mChannels.end(); ++iter) {
312 if (*iter == channel) {
313 mChannels.erase(iter);
314 mChannels.push_front(channel);
315 break;
316 }
317 }
318}
319
320void SoundPool::pause(int channelID)
321{
322 LOGV("pause(%d)", channelID);
323 Mutex::Autolock lock(&mLock);
324 SoundChannel* channel = findChannel(channelID);
325 if (channel) {
326 channel->pause();
327 }
328}
329
Dave Sparksf992cbb2010-02-09 13:00:09 -0800330void SoundPool::autoPause()
331{
Dave Sparks7bef5a52010-02-17 09:09:24 -0800332 LOGV("autoPause()");
Dave Sparksf992cbb2010-02-09 13:00:09 -0800333 Mutex::Autolock lock(&mLock);
334 for (int i = 0; i < mMaxChannels; ++i) {
335 SoundChannel* channel = &mChannelPool[i];
336 channel->autoPause();
337 }
338}
339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340void SoundPool::resume(int channelID)
341{
342 LOGV("resume(%d)", channelID);
343 Mutex::Autolock lock(&mLock);
344 SoundChannel* channel = findChannel(channelID);
345 if (channel) {
346 channel->resume();
347 }
348}
349
Dave Sparksf992cbb2010-02-09 13:00:09 -0800350void SoundPool::autoResume()
351{
Dave Sparks7bef5a52010-02-17 09:09:24 -0800352 LOGV("autoResume()");
Dave Sparksf992cbb2010-02-09 13:00:09 -0800353 Mutex::Autolock lock(&mLock);
354 for (int i = 0; i < mMaxChannels; ++i) {
355 SoundChannel* channel = &mChannelPool[i];
356 channel->autoResume();
357 }
358}
359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360void SoundPool::stop(int channelID)
361{
362 LOGV("stop(%d)", channelID);
363 Mutex::Autolock lock(&mLock);
364 SoundChannel* channel = findChannel(channelID);
365 if (channel) {
366 channel->stop();
367 } else {
368 channel = findNextChannel(channelID);
369 if (channel)
370 channel->clearNextEvent();
371 }
372}
373
374void SoundPool::setVolume(int channelID, float leftVolume, float rightVolume)
375{
376 Mutex::Autolock lock(&mLock);
377 SoundChannel* channel = findChannel(channelID);
378 if (channel) {
379 channel->setVolume(leftVolume, rightVolume);
380 }
381}
382
383void SoundPool::setPriority(int channelID, int priority)
384{
385 LOGV("setPriority(%d, %d)", channelID, priority);
386 Mutex::Autolock lock(&mLock);
387 SoundChannel* channel = findChannel(channelID);
388 if (channel) {
389 channel->setPriority(priority);
390 }
391}
392
393void SoundPool::setLoop(int channelID, int loop)
394{
395 LOGV("setLoop(%d, %d)", channelID, loop);
396 Mutex::Autolock lock(&mLock);
397 SoundChannel* channel = findChannel(channelID);
398 if (channel) {
399 channel->setLoop(loop);
400 }
401}
402
403void SoundPool::setRate(int channelID, float rate)
404{
405 LOGV("setRate(%d, %f)", channelID, rate);
406 Mutex::Autolock lock(&mLock);
407 SoundChannel* channel = findChannel(channelID);
408 if (channel) {
409 channel->setRate(rate);
410 }
411}
412
413// call with lock held
Eric Laurenta60e2122010-12-28 16:49:07 -0800414void SoundPool::done_l(SoundChannel* channel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415{
Eric Laurenta60e2122010-12-28 16:49:07 -0800416 LOGV("done_l(%d)", channel->channelID());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 // if "stolen", play next event
418 if (channel->nextChannelID() != 0) {
419 LOGV("add to restart list");
420 addToRestartList(channel);
421 }
422
423 // return to idle state
424 else {
425 LOGV("move to front");
Eric Laurenta60e2122010-12-28 16:49:07 -0800426 moveToFront_l(channel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 }
428}
429
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800430void SoundPool::setCallback(SoundPoolCallback* callback, void* user)
431{
432 Mutex::Autolock lock(&mCallbackLock);
433 mCallback = callback;
434 mUserData = user;
435}
436
437void SoundPool::notify(SoundPoolEvent event)
438{
439 Mutex::Autolock lock(&mCallbackLock);
440 if (mCallback != NULL) {
441 mCallback(event, this, mUserData);
442 }
443}
444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445void SoundPool::dump()
446{
447 for (int i = 0; i < mMaxChannels; ++i) {
448 mChannelPool[i].dump();
449 }
450}
451
452
453Sample::Sample(int sampleID, const char* url)
454{
455 init();
456 mSampleID = sampleID;
457 mUrl = strdup(url);
458 LOGV("create sampleID=%d, url=%s", mSampleID, mUrl);
459}
460
461Sample::Sample(int sampleID, int fd, int64_t offset, int64_t length)
462{
463 init();
464 mSampleID = sampleID;
465 mFd = dup(fd);
466 mOffset = offset;
467 mLength = length;
468 LOGV("create sampleID=%d, fd=%d, offset=%lld, length=%lld", mSampleID, mFd, mLength, mOffset);
469}
470
471void Sample::init()
472{
473 mData = 0;
474 mSize = 0;
475 mRefCount = 0;
476 mSampleID = 0;
477 mState = UNLOADED;
478 mFd = -1;
479 mOffset = 0;
480 mLength = 0;
481 mUrl = 0;
482}
483
484Sample::~Sample()
485{
486 LOGV("Sample::destructor sampleID=%d, fd=%d", mSampleID, mFd);
487 if (mFd > 0) {
488 LOGV("close(%d)", mFd);
489 ::close(mFd);
490 }
491 mData.clear();
492 delete mUrl;
493}
494
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800495status_t Sample::doLoad()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496{
497 uint32_t sampleRate;
498 int numChannels;
499 int format;
500 sp<IMemory> p;
501 LOGV("Start decode");
502 if (mUrl) {
503 p = MediaPlayer::decode(mUrl, &sampleRate, &numChannels, &format);
504 } else {
505 p = MediaPlayer::decode(mFd, mOffset, mLength, &sampleRate, &numChannels, &format);
506 LOGV("close(%d)", mFd);
507 ::close(mFd);
508 mFd = -1;
509 }
510 if (p == 0) {
511 LOGE("Unable to load sample: %s", mUrl);
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800512 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 }
514 LOGV("pointer = %p, size = %u, sampleRate = %u, numChannels = %d",
515 p->pointer(), p->size(), sampleRate, numChannels);
516
517 if (sampleRate > kMaxSampleRate) {
518 LOGE("Sample rate (%u) out of range", sampleRate);
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800519 return - 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 }
521
522 if ((numChannels < 1) || (numChannels > 2)) {
523 LOGE("Sample channel count (%d) out of range", numChannels);
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800524 return - 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 }
526
527 //_dumpBuffer(p->pointer(), p->size());
528 uint8_t* q = static_cast<uint8_t*>(p->pointer()) + p->size() - 10;
529 //_dumpBuffer(q, 10, 10, false);
530
531 mData = p;
532 mSize = p->size();
533 mSampleRate = sampleRate;
534 mNumChannels = numChannels;
535 mFormat = format;
536 mState = READY;
Dave Sparksf6e43bf2009-12-08 08:10:42 -0800537 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538}
539
540
541void SoundChannel::init(SoundPool* soundPool)
542{
543 mSoundPool = soundPool;
544}
545
Eric Laurenta60e2122010-12-28 16:49:07 -0800546// call with sound pool lock held
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547void SoundChannel::play(const sp<Sample>& sample, int nextChannelID, float leftVolume,
548 float rightVolume, int priority, int loop, float rate)
549{
550 AudioTrack* oldTrack;
Eric Laurenta60e2122010-12-28 16:49:07 -0800551 AudioTrack* newTrack;
552 status_t status;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553
Eric Laurenta60e2122010-12-28 16:49:07 -0800554 { // scope for the lock
555 Mutex::Autolock lock(&mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556
Eric Laurenta60e2122010-12-28 16:49:07 -0800557 LOGV("SoundChannel::play %p: sampleID=%d, channelID=%d, leftVolume=%f, rightVolume=%f,"
558 " priority=%d, loop=%d, rate=%f",
559 this, sample->sampleID(), nextChannelID, leftVolume, rightVolume,
560 priority, loop, rate);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561
Eric Laurenta60e2122010-12-28 16:49:07 -0800562 // if not idle, this voice is being stolen
563 if (mState != IDLE) {
564 LOGV("channel %d stolen - event queued for channel %d", channelID(), nextChannelID);
565 mNextEvent.set(sample, nextChannelID, leftVolume, rightVolume, priority, loop, rate);
566 stop_l();
567 return;
568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569
Eric Laurenta60e2122010-12-28 16:49:07 -0800570 // initialize track
571 int afFrameCount;
572 int afSampleRate;
573 int streamType = mSoundPool->streamType();
574 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
575 afFrameCount = kDefaultFrameCount;
576 }
577 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
578 afSampleRate = kDefaultSampleRate;
579 }
580 int numChannels = sample->numChannels();
581 uint32_t sampleRate = uint32_t(float(sample->sampleRate()) * rate + 0.5);
582 uint32_t totalFrames = (kDefaultBufferCount * afFrameCount * sampleRate) / afSampleRate;
583 uint32_t bufferFrames = (totalFrames + (kDefaultBufferCount - 1)) / kDefaultBufferCount;
584 uint32_t frameCount = 0;
585
586 if (loop) {
587 frameCount = sample->size()/numChannels/
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700588 ((sample->format() == AUDIO_FORMAT_PCM_16_BIT) ? sizeof(int16_t) : sizeof(uint8_t));
Eric Laurenta60e2122010-12-28 16:49:07 -0800589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590
Eric Laurent9648e4b2009-05-07 03:14:31 -0700591#ifndef USE_SHARED_MEM_BUFFER
Eric Laurenta60e2122010-12-28 16:49:07 -0800592 // Ensure minimum audio buffer size in case of short looped sample
593 if(frameCount < totalFrames) {
594 frameCount = totalFrames;
595 }
Eric Laurent9648e4b2009-05-07 03:14:31 -0700596#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597
Eric Laurenta60e2122010-12-28 16:49:07 -0800598 // mToggle toggles each time a track is started on a given channel.
599 // The toggle is concatenated with the SoundChannel address and passed to AudioTrack
600 // as callback user data. This enables the detection of callbacks received from the old
601 // audio track while the new one is being started and avoids processing them with
602 // wrong audio audio buffer size (mAudioBufferSize)
603 unsigned long toggle = mToggle ^ 1;
604 void *userData = (void *)((unsigned long)this | toggle);
605 uint32_t channels = (numChannels == 2) ?
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700606 AUDIO_CHANNEL_OUT_STEREO : AUDIO_CHANNEL_OUT_MONO;
Dave Sparks66d28ce2009-12-14 21:12:05 -0800607
Eric Laurenta60e2122010-12-28 16:49:07 -0800608 // 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 -0800609#ifdef USE_SHARED_MEM_BUFFER
Eric Laurenta60e2122010-12-28 16:49:07 -0800610 newTrack = new AudioTrack(streamType, sampleRate, sample->format(),
611 channels, sample->getIMemory(), 0, callback, userData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612#else
Eric Laurenta60e2122010-12-28 16:49:07 -0800613 newTrack = new AudioTrack(streamType, sampleRate, sample->format(),
614 channels, frameCount, 0, callback, userData, bufferFrames);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615#endif
Eric Laurenta60e2122010-12-28 16:49:07 -0800616 oldTrack = mAudioTrack;
617 status = newTrack->initCheck();
618 if (status != NO_ERROR) {
619 LOGE("Error creating AudioTrack");
620 goto exit;
621 }
622 LOGV("setVolume %p", newTrack);
623 newTrack->setVolume(leftVolume, rightVolume);
624 newTrack->setLoop(0, frameCount, loop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 // From now on, AudioTrack callbacks recevieved with previous toggle value will be ignored.
627 mToggle = toggle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 mAudioTrack = newTrack;
629 mPos = 0;
630 mSample = sample;
631 mChannelID = nextChannelID;
632 mPriority = priority;
633 mLoop = loop;
634 mLeftVolume = leftVolume;
635 mRightVolume = rightVolume;
636 mNumChannels = numChannels;
637 mRate = rate;
638 clearNextEvent();
639 mState = PLAYING;
640 mAudioTrack->start();
641 mAudioBufferSize = newTrack->frameCount()*newTrack->frameSize();
642 }
643
Eric Laurenta60e2122010-12-28 16:49:07 -0800644exit:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 LOGV("delete oldTrack %p", oldTrack);
646 delete oldTrack;
Eric Laurenta60e2122010-12-28 16:49:07 -0800647 if (status != NO_ERROR) {
648 delete newTrack;
649 mAudioTrack = NULL;
650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651}
652
653void SoundChannel::nextEvent()
654{
655 sp<Sample> sample;
656 int nextChannelID;
657 float leftVolume;
658 float rightVolume;
659 int priority;
660 int loop;
661 float rate;
662
663 // check for valid event
664 {
665 Mutex::Autolock lock(&mLock);
666 nextChannelID = mNextEvent.channelID();
667 if (nextChannelID == 0) {
668 LOGV("stolen channel has no event");
669 return;
670 }
671
672 sample = mNextEvent.sample();
673 leftVolume = mNextEvent.leftVolume();
674 rightVolume = mNextEvent.rightVolume();
675 priority = mNextEvent.priority();
676 loop = mNextEvent.loop();
677 rate = mNextEvent.rate();
678 }
679
680 LOGV("Starting stolen channel %d -> %d", channelID(), nextChannelID);
681 play(sample, nextChannelID, leftVolume, rightVolume, priority, loop, rate);
682}
683
Glenn Kastene46a86f2011-06-01 15:20:35 -0700684void SoundChannel::callback(int event, void* user, void *info)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 SoundChannel* channel = static_cast<SoundChannel*>((void *)((unsigned long)user & ~1));
687
Eric Laurenta60e2122010-12-28 16:49:07 -0800688 channel->process(event, info, (unsigned long)user & 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689}
690
Glenn Kastene46a86f2011-06-01 15:20:35 -0700691void SoundChannel::process(int event, void *info, unsigned long toggle)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692{
693 //LOGV("process(%d)", mChannelID);
Eric Laurenta60e2122010-12-28 16:49:07 -0800694
695 Mutex::Autolock lock(&mLock);
696
697 AudioTrack::Buffer* b = NULL;
698 if (event == AudioTrack::EVENT_MORE_DATA) {
699 b = static_cast<AudioTrack::Buffer *>(info);
700 }
701
702 if (mToggle != toggle) {
703 LOGV("process wrong toggle %p channel %d", this, mChannelID);
704 if (b != NULL) {
705 b->size = 0;
706 }
707 return;
708 }
709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 sp<Sample> sample = mSample;
711
712// LOGV("SoundChannel::process event %d", event);
713
714 if (event == AudioTrack::EVENT_MORE_DATA) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715
716 // check for stop state
717 if (b->size == 0) return;
718
Eric Laurenta60e2122010-12-28 16:49:07 -0800719 if (mState == IDLE) {
720 b->size = 0;
721 return;
722 }
723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 if (sample != 0) {
725 // fill buffer
726 uint8_t* q = (uint8_t*) b->i8;
727 size_t count = 0;
728
729 if (mPos < (int)sample->size()) {
730 uint8_t* p = sample->data() + mPos;
731 count = sample->size() - mPos;
732 if (count > b->size) {
733 count = b->size;
734 }
735 memcpy(q, p, count);
Eric Laurenta60e2122010-12-28 16:49:07 -0800736// 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 -0800737 } else if (mPos < mAudioBufferSize) {
738 count = mAudioBufferSize - mPos;
739 if (count > b->size) {
740 count = b->size;
741 }
742 memset(q, 0, count);
Eric Laurenta60e2122010-12-28 16:49:07 -0800743// 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 -0800744 }
745
746 mPos += count;
747 b->size = count;
748 //LOGV("buffer=%p, [0]=%d", b->i16, b->i16[0]);
749 }
750 } else if (event == AudioTrack::EVENT_UNDERRUN) {
Eric Laurenta60e2122010-12-28 16:49:07 -0800751 LOGV("process %p channel %d EVENT_UNDERRUN", this, mChannelID);
752 mSoundPool->addToStopList(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 } else if (event == AudioTrack::EVENT_LOOP_END) {
Eric Laurenta60e2122010-12-28 16:49:07 -0800754 LOGV("End loop %p channel %d count %d", this, mChannelID, *(int *)info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 }
756}
757
758
759// call with lock held
Eric Laurenta60e2122010-12-28 16:49:07 -0800760bool SoundChannel::doStop_l()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761{
762 if (mState != IDLE) {
763 setVolume_l(0, 0);
764 LOGV("stop");
765 mAudioTrack->stop();
766 mSample.clear();
767 mState = IDLE;
768 mPriority = IDLE_PRIORITY;
Eric Laurenta60e2122010-12-28 16:49:07 -0800769 return true;
770 }
771 return false;
772}
773
774// call with lock held and sound pool lock held
775void SoundChannel::stop_l()
776{
777 if (doStop_l()) {
778 mSoundPool->done_l(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 }
780}
781
Eric Laurenta60e2122010-12-28 16:49:07 -0800782// call with sound pool lock held
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783void SoundChannel::stop()
784{
Eric Laurenta60e2122010-12-28 16:49:07 -0800785 bool stopped;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 {
787 Mutex::Autolock lock(&mLock);
Eric Laurenta60e2122010-12-28 16:49:07 -0800788 stopped = doStop_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 }
Eric Laurenta60e2122010-12-28 16:49:07 -0800790
791 if (stopped) {
792 mSoundPool->done_l(this);
793 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794}
795
796//FIXME: Pause is a little broken right now
797void SoundChannel::pause()
798{
799 Mutex::Autolock lock(&mLock);
800 if (mState == PLAYING) {
801 LOGV("pause track");
802 mState = PAUSED;
803 mAudioTrack->pause();
804 }
805}
806
Dave Sparksf992cbb2010-02-09 13:00:09 -0800807void SoundChannel::autoPause()
808{
809 Mutex::Autolock lock(&mLock);
810 if (mState == PLAYING) {
811 LOGV("pause track");
812 mState = PAUSED;
813 mAutoPaused = true;
814 mAudioTrack->pause();
815 }
816}
817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818void SoundChannel::resume()
819{
820 Mutex::Autolock lock(&mLock);
821 if (mState == PAUSED) {
822 LOGV("resume track");
823 mState = PLAYING;
Dave Sparksf992cbb2010-02-09 13:00:09 -0800824 mAutoPaused = false;
825 mAudioTrack->start();
826 }
827}
828
829void SoundChannel::autoResume()
830{
831 Mutex::Autolock lock(&mLock);
832 if (mAutoPaused && (mState == PAUSED)) {
833 LOGV("resume track");
834 mState = PLAYING;
835 mAutoPaused = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 mAudioTrack->start();
837 }
838}
839
840void SoundChannel::setRate(float rate)
841{
842 Mutex::Autolock lock(&mLock);
843 if (mAudioTrack != 0 && mSample.get() != 0) {
844 uint32_t sampleRate = uint32_t(float(mSample->sampleRate()) * rate + 0.5);
845 mAudioTrack->setSampleRate(sampleRate);
846 mRate = rate;
847 }
848}
849
850// call with lock held
851void SoundChannel::setVolume_l(float leftVolume, float rightVolume)
852{
853 mLeftVolume = leftVolume;
854 mRightVolume = rightVolume;
855 if (mAudioTrack != 0) mAudioTrack->setVolume(leftVolume, rightVolume);
856}
857
858void SoundChannel::setVolume(float leftVolume, float rightVolume)
859{
860 Mutex::Autolock lock(&mLock);
861 setVolume_l(leftVolume, rightVolume);
862}
863
864void SoundChannel::setLoop(int loop)
865{
866 Mutex::Autolock lock(&mLock);
867 if (mAudioTrack != 0 && mSample.get() != 0) {
Eric Laurenta60e2122010-12-28 16:49:07 -0800868 uint32_t loopEnd = mSample->size()/mNumChannels/
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700869 ((mSample->format() == AUDIO_FORMAT_PCM_16_BIT) ? sizeof(int16_t) : sizeof(uint8_t));
Eric Laurenta60e2122010-12-28 16:49:07 -0800870 mAudioTrack->setLoop(0, loopEnd, loop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 mLoop = loop;
872 }
873}
874
875SoundChannel::~SoundChannel()
876{
Eric Laurenta60e2122010-12-28 16:49:07 -0800877 LOGV("SoundChannel destructor %p", this);
878 {
879 Mutex::Autolock lock(&mLock);
880 clearNextEvent();
881 doStop_l();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 }
Eric Laurenta60e2122010-12-28 16:49:07 -0800883 // do not call AudioTrack destructor with mLock held as it will wait for the AudioTrack
884 // callback thread to exit which may need to execute process() and acquire the mLock.
885 delete mAudioTrack;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886}
887
888void SoundChannel::dump()
889{
890 LOGV("mState = %d mChannelID=%d, mNumChannels=%d, mPos = %d, mPriority=%d, mLoop=%d",
891 mState, mChannelID, mNumChannels, mPos, mPriority, mLoop);
892}
893
894void SoundEvent::set(const sp<Sample>& sample, int channelID, float leftVolume,
895 float rightVolume, int priority, int loop, float rate)
896{
Eric Laurenta60e2122010-12-28 16:49:07 -0800897 mSample = sample;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 mChannelID = channelID;
899 mLeftVolume = leftVolume;
900 mRightVolume = rightVolume;
901 mPriority = priority;
902 mLoop = loop;
903 mRate =rate;
904}
905
906} // end namespace android