blob: 1205f9d3a2f249bc9f443431865fc76274fd0c93 [file] [log] [blame]
Marco Nelissen372be892014-12-04 08:59:22 -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
20#include <inttypes.h>
21
22#include <utils/Log.h>
23
24#define USE_SHARED_MEM_BUFFER
25
26#include <media/AudioTrack.h>
27#include <media/IMediaHTTPService.h>
28#include <media/mediaplayer.h>
29#include <media/stagefright/MediaExtractor.h>
30#include "SoundPool.h"
31#include "SoundPoolThread.h"
32#include <media/AudioPolicyHelper.h>
33#include <ndk/NdkMediaCodec.h>
34#include <ndk/NdkMediaExtractor.h>
35#include <ndk/NdkMediaFormat.h>
36
37namespace android
38{
39
40int kDefaultBufferCount = 4;
41uint32_t kMaxSampleRate = 48000;
42uint32_t kDefaultSampleRate = 44100;
43uint32_t kDefaultFrameCount = 1200;
44size_t kDefaultHeapSize = 1024 * 1024; // 1MB
45
46
47SoundPool::SoundPool(int maxChannels, const audio_attributes_t* pAttributes)
48{
49 ALOGV("SoundPool constructor: maxChannels=%d, attr.usage=%d, attr.flags=0x%x, attr.tags=%s",
50 maxChannels, pAttributes->usage, pAttributes->flags, pAttributes->tags);
51
52 // check limits
53 mMaxChannels = maxChannels;
54 if (mMaxChannels < 1) {
55 mMaxChannels = 1;
56 }
57 else if (mMaxChannels > 32) {
58 mMaxChannels = 32;
59 }
60 ALOGW_IF(maxChannels != mMaxChannels, "App requested %d channels", maxChannels);
61
62 mQuit = false;
63 mDecodeThread = 0;
64 memcpy(&mAttributes, pAttributes, sizeof(audio_attributes_t));
65 mAllocated = 0;
66 mNextSampleID = 0;
67 mNextChannelID = 0;
68
69 mCallback = 0;
70 mUserData = 0;
71
72 mChannelPool = new SoundChannel[mMaxChannels];
73 for (int i = 0; i < mMaxChannels; ++i) {
74 mChannelPool[i].init(this);
75 mChannels.push_back(&mChannelPool[i]);
76 }
77
78 // start decode thread
79 startThreads();
80}
81
82SoundPool::~SoundPool()
83{
84 ALOGV("SoundPool destructor");
85 mDecodeThread->quit();
86 quit();
87
88 Mutex::Autolock lock(&mLock);
89
90 mChannels.clear();
91 if (mChannelPool)
92 delete [] mChannelPool;
93 // clean up samples
94 ALOGV("clear samples");
95 mSamples.clear();
96
97 if (mDecodeThread)
98 delete mDecodeThread;
99}
100
101void SoundPool::addToRestartList(SoundChannel* channel)
102{
103 Mutex::Autolock lock(&mRestartLock);
104 if (!mQuit) {
105 mRestart.push_back(channel);
106 mCondition.signal();
107 }
108}
109
110void SoundPool::addToStopList(SoundChannel* channel)
111{
112 Mutex::Autolock lock(&mRestartLock);
113 if (!mQuit) {
114 mStop.push_back(channel);
115 mCondition.signal();
116 }
117}
118
119int SoundPool::beginThread(void* arg)
120{
121 SoundPool* p = (SoundPool*)arg;
122 return p->run();
123}
124
125int SoundPool::run()
126{
127 mRestartLock.lock();
128 while (!mQuit) {
129 mCondition.wait(mRestartLock);
130 ALOGV("awake");
131 if (mQuit) break;
132
133 while (!mStop.empty()) {
134 SoundChannel* channel;
135 ALOGV("Getting channel from stop list");
136 List<SoundChannel* >::iterator iter = mStop.begin();
137 channel = *iter;
138 mStop.erase(iter);
139 mRestartLock.unlock();
140 if (channel != 0) {
141 Mutex::Autolock lock(&mLock);
142 channel->stop();
143 }
144 mRestartLock.lock();
145 if (mQuit) break;
146 }
147
148 while (!mRestart.empty()) {
149 SoundChannel* channel;
150 ALOGV("Getting channel from list");
151 List<SoundChannel*>::iterator iter = mRestart.begin();
152 channel = *iter;
153 mRestart.erase(iter);
154 mRestartLock.unlock();
155 if (channel != 0) {
156 Mutex::Autolock lock(&mLock);
157 channel->nextEvent();
158 }
159 mRestartLock.lock();
160 if (mQuit) break;
161 }
162 }
163
164 mStop.clear();
165 mRestart.clear();
166 mCondition.signal();
167 mRestartLock.unlock();
168 ALOGV("goodbye");
169 return 0;
170}
171
172void SoundPool::quit()
173{
174 mRestartLock.lock();
175 mQuit = true;
176 mCondition.signal();
177 mCondition.wait(mRestartLock);
178 ALOGV("return from quit");
179 mRestartLock.unlock();
180}
181
182bool SoundPool::startThreads()
183{
184 createThreadEtc(beginThread, this, "SoundPool");
185 if (mDecodeThread == NULL)
186 mDecodeThread = new SoundPoolThread(this);
187 return mDecodeThread != NULL;
188}
189
190SoundChannel* SoundPool::findChannel(int channelID)
191{
192 for (int i = 0; i < mMaxChannels; ++i) {
193 if (mChannelPool[i].channelID() == channelID) {
194 return &mChannelPool[i];
195 }
196 }
197 return NULL;
198}
199
200SoundChannel* SoundPool::findNextChannel(int channelID)
201{
202 for (int i = 0; i < mMaxChannels; ++i) {
203 if (mChannelPool[i].nextChannelID() == channelID) {
204 return &mChannelPool[i];
205 }
206 }
207 return NULL;
208}
209
210int SoundPool::load(int fd, int64_t offset, int64_t length, int priority __unused)
211{
212 ALOGV("load: fd=%d, offset=%" PRId64 ", length=%" PRId64 ", priority=%d",
213 fd, offset, length, priority);
214 Mutex::Autolock lock(&mLock);
215 sp<Sample> sample = new Sample(++mNextSampleID, fd, offset, length);
216 mSamples.add(sample->sampleID(), sample);
217 doLoad(sample);
218 return sample->sampleID();
219}
220
221void SoundPool::doLoad(sp<Sample>& sample)
222{
223 ALOGV("doLoad: loading sample sampleID=%d", sample->sampleID());
224 sample->startLoad();
225 mDecodeThread->loadSample(sample->sampleID());
226}
227
228bool SoundPool::unload(int sampleID)
229{
230 ALOGV("unload: sampleID=%d", sampleID);
231 Mutex::Autolock lock(&mLock);
232 return mSamples.removeItem(sampleID);
233}
234
235int SoundPool::play(int sampleID, float leftVolume, float rightVolume,
236 int priority, int loop, float rate)
237{
238 ALOGV("play sampleID=%d, leftVolume=%f, rightVolume=%f, priority=%d, loop=%d, rate=%f",
239 sampleID, leftVolume, rightVolume, priority, loop, rate);
240 sp<Sample> sample;
241 SoundChannel* channel;
242 int channelID;
243
244 Mutex::Autolock lock(&mLock);
245
246 if (mQuit) {
247 return 0;
248 }
249 // is sample ready?
250 sample = findSample(sampleID);
251 if ((sample == 0) || (sample->state() != Sample::READY)) {
252 ALOGW(" sample %d not READY", sampleID);
253 return 0;
254 }
255
256 dump();
257
258 // allocate a channel
Glenn Kasten18c225d2015-03-09 08:36:35 -0700259 channel = allocateChannel_l(priority);
Marco Nelissen372be892014-12-04 08:59:22 -0800260
261 // no channel allocated - return 0
262 if (!channel) {
263 ALOGV("No channel allocated");
264 return 0;
265 }
266
267 channelID = ++mNextChannelID;
268
269 ALOGV("play channel %p state = %d", channel, channel->state());
270 channel->play(sample, channelID, leftVolume, rightVolume, priority, loop, rate);
271 return channelID;
272}
273
Glenn Kasten18c225d2015-03-09 08:36:35 -0700274SoundChannel* SoundPool::allocateChannel_l(int priority)
Marco Nelissen372be892014-12-04 08:59:22 -0800275{
276 List<SoundChannel*>::iterator iter;
277 SoundChannel* channel = NULL;
278
Glenn Kasten18c225d2015-03-09 08:36:35 -0700279 // allocate a channel
Marco Nelissen372be892014-12-04 08:59:22 -0800280 if (!mChannels.empty()) {
281 iter = mChannels.begin();
282 if (priority >= (*iter)->priority()) {
283 channel = *iter;
284 mChannels.erase(iter);
285 ALOGV("Allocated active channel");
286 }
287 }
288
289 // update priority and put it back in the list
290 if (channel) {
291 channel->setPriority(priority);
292 for (iter = mChannels.begin(); iter != mChannels.end(); ++iter) {
293 if (priority < (*iter)->priority()) {
294 break;
295 }
296 }
297 mChannels.insert(iter, channel);
298 }
299 return channel;
300}
301
302// move a channel from its current position to the front of the list
303void SoundPool::moveToFront_l(SoundChannel* channel)
304{
305 for (List<SoundChannel*>::iterator iter = mChannels.begin(); iter != mChannels.end(); ++iter) {
306 if (*iter == channel) {
307 mChannels.erase(iter);
308 mChannels.push_front(channel);
309 break;
310 }
311 }
312}
313
314void SoundPool::pause(int channelID)
315{
316 ALOGV("pause(%d)", channelID);
317 Mutex::Autolock lock(&mLock);
318 SoundChannel* channel = findChannel(channelID);
319 if (channel) {
320 channel->pause();
321 }
322}
323
324void SoundPool::autoPause()
325{
326 ALOGV("autoPause()");
327 Mutex::Autolock lock(&mLock);
328 for (int i = 0; i < mMaxChannels; ++i) {
329 SoundChannel* channel = &mChannelPool[i];
330 channel->autoPause();
331 }
332}
333
334void SoundPool::resume(int channelID)
335{
336 ALOGV("resume(%d)", channelID);
337 Mutex::Autolock lock(&mLock);
338 SoundChannel* channel = findChannel(channelID);
339 if (channel) {
340 channel->resume();
341 }
342}
343
344void SoundPool::autoResume()
345{
346 ALOGV("autoResume()");
347 Mutex::Autolock lock(&mLock);
348 for (int i = 0; i < mMaxChannels; ++i) {
349 SoundChannel* channel = &mChannelPool[i];
350 channel->autoResume();
351 }
352}
353
354void SoundPool::stop(int channelID)
355{
356 ALOGV("stop(%d)", channelID);
357 Mutex::Autolock lock(&mLock);
358 SoundChannel* channel = findChannel(channelID);
359 if (channel) {
360 channel->stop();
361 } else {
362 channel = findNextChannel(channelID);
363 if (channel)
364 channel->clearNextEvent();
365 }
366}
367
368void SoundPool::setVolume(int channelID, float leftVolume, float rightVolume)
369{
370 Mutex::Autolock lock(&mLock);
371 SoundChannel* channel = findChannel(channelID);
372 if (channel) {
373 channel->setVolume(leftVolume, rightVolume);
374 }
375}
376
377void SoundPool::setPriority(int channelID, int priority)
378{
379 ALOGV("setPriority(%d, %d)", channelID, priority);
380 Mutex::Autolock lock(&mLock);
381 SoundChannel* channel = findChannel(channelID);
382 if (channel) {
383 channel->setPriority(priority);
384 }
385}
386
387void SoundPool::setLoop(int channelID, int loop)
388{
389 ALOGV("setLoop(%d, %d)", channelID, loop);
390 Mutex::Autolock lock(&mLock);
391 SoundChannel* channel = findChannel(channelID);
392 if (channel) {
393 channel->setLoop(loop);
394 }
395}
396
397void SoundPool::setRate(int channelID, float rate)
398{
399 ALOGV("setRate(%d, %f)", channelID, rate);
400 Mutex::Autolock lock(&mLock);
401 SoundChannel* channel = findChannel(channelID);
402 if (channel) {
403 channel->setRate(rate);
404 }
405}
406
407// call with lock held
408void SoundPool::done_l(SoundChannel* channel)
409{
410 ALOGV("done_l(%d)", channel->channelID());
411 // if "stolen", play next event
412 if (channel->nextChannelID() != 0) {
413 ALOGV("add to restart list");
414 addToRestartList(channel);
415 }
416
417 // return to idle state
418 else {
419 ALOGV("move to front");
420 moveToFront_l(channel);
421 }
422}
423
424void SoundPool::setCallback(SoundPoolCallback* callback, void* user)
425{
426 Mutex::Autolock lock(&mCallbackLock);
427 mCallback = callback;
428 mUserData = user;
429}
430
431void SoundPool::notify(SoundPoolEvent event)
432{
433 Mutex::Autolock lock(&mCallbackLock);
434 if (mCallback != NULL) {
435 mCallback(event, this, mUserData);
436 }
437}
438
439void SoundPool::dump()
440{
441 for (int i = 0; i < mMaxChannels; ++i) {
442 mChannelPool[i].dump();
443 }
444}
445
446
447Sample::Sample(int sampleID, int fd, int64_t offset, int64_t length)
448{
449 init();
450 mSampleID = sampleID;
451 mFd = dup(fd);
452 mOffset = offset;
453 mLength = length;
454 ALOGV("create sampleID=%d, fd=%d, offset=%" PRId64 " length=%" PRId64,
455 mSampleID, mFd, mLength, mOffset);
456}
457
458void Sample::init()
459{
460 mSize = 0;
461 mRefCount = 0;
462 mSampleID = 0;
463 mState = UNLOADED;
464 mFd = -1;
465 mOffset = 0;
466 mLength = 0;
467}
468
469Sample::~Sample()
470{
471 ALOGV("Sample::destructor sampleID=%d, fd=%d", mSampleID, mFd);
472 if (mFd > 0) {
473 ALOGV("close(%d)", mFd);
474 ::close(mFd);
475 }
476}
477
478static status_t decode(int fd, int64_t offset, int64_t length,
479 uint32_t *rate, int *numChannels, audio_format_t *audioFormat,
480 sp<MemoryHeapBase> heap, size_t *memsize) {
481
Marco Nelissen6cd61102015-01-27 12:17:48 -0800482 ALOGV("fd %d, offset %" PRId64 ", size %" PRId64, fd, offset, length);
Marco Nelissen372be892014-12-04 08:59:22 -0800483 AMediaExtractor *ex = AMediaExtractor_new();
484 status_t err = AMediaExtractor_setDataSourceFd(ex, fd, offset, length);
485
486 if (err != AMEDIA_OK) {
Marco Nelissen06524dc2015-02-10 15:45:23 -0800487 AMediaExtractor_delete(ex);
Marco Nelissen372be892014-12-04 08:59:22 -0800488 return err;
489 }
490
491 *audioFormat = AUDIO_FORMAT_PCM_16_BIT;
492
493 size_t numTracks = AMediaExtractor_getTrackCount(ex);
494 for (size_t i = 0; i < numTracks; i++) {
495 AMediaFormat *format = AMediaExtractor_getTrackFormat(ex, i);
496 const char *mime;
497 if (!AMediaFormat_getString(format, AMEDIAFORMAT_KEY_MIME, &mime)) {
498 AMediaExtractor_delete(ex);
499 AMediaFormat_delete(format);
500 return UNKNOWN_ERROR;
501 }
502 if (strncmp(mime, "audio/", 6) == 0) {
503
504 AMediaCodec *codec = AMediaCodec_createDecoderByType(mime);
505 if (AMediaCodec_configure(codec, format,
506 NULL /* window */, NULL /* drm */, 0 /* flags */) != AMEDIA_OK
507 || AMediaCodec_start(codec) != AMEDIA_OK
508 || AMediaExtractor_selectTrack(ex, i) != AMEDIA_OK) {
509 AMediaExtractor_delete(ex);
510 AMediaCodec_delete(codec);
511 AMediaFormat_delete(format);
512 return UNKNOWN_ERROR;
513 }
514
515 bool sawInputEOS = false;
516 bool sawOutputEOS = false;
517 uint8_t* writePos = static_cast<uint8_t*>(heap->getBase());
518 size_t available = heap->getSize();
519 size_t written = 0;
520
521 AMediaFormat_delete(format);
522 format = AMediaCodec_getOutputFormat(codec);
523
524 while (!sawOutputEOS) {
525 if (!sawInputEOS) {
526 ssize_t bufidx = AMediaCodec_dequeueInputBuffer(codec, 5000);
Marco Nelissen6cd61102015-01-27 12:17:48 -0800527 ALOGV("input buffer %zd", bufidx);
Marco Nelissen372be892014-12-04 08:59:22 -0800528 if (bufidx >= 0) {
529 size_t bufsize;
530 uint8_t *buf = AMediaCodec_getInputBuffer(codec, bufidx, &bufsize);
531 int sampleSize = AMediaExtractor_readSampleData(ex, buf, bufsize);
532 ALOGV("read %d", sampleSize);
533 if (sampleSize < 0) {
534 sampleSize = 0;
535 sawInputEOS = true;
536 ALOGV("EOS");
537 }
538 int64_t presentationTimeUs = AMediaExtractor_getSampleTime(ex);
539
540 AMediaCodec_queueInputBuffer(codec, bufidx,
541 0 /* offset */, sampleSize, presentationTimeUs,
542 sawInputEOS ? AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM : 0);
543 AMediaExtractor_advance(ex);
544 }
545 }
546
547 AMediaCodecBufferInfo info;
548 int status = AMediaCodec_dequeueOutputBuffer(codec, &info, 1);
549 ALOGV("dequeueoutput returned: %d", status);
550 if (status >= 0) {
551 if (info.flags & AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM) {
552 ALOGV("output EOS");
553 sawOutputEOS = true;
554 }
555 ALOGV("got decoded buffer size %d", info.size);
556
557 uint8_t *buf = AMediaCodec_getOutputBuffer(codec, status, NULL /* out_size */);
558 size_t dataSize = info.size;
559 if (dataSize > available) {
560 dataSize = available;
561 }
562 memcpy(writePos, buf + info.offset, dataSize);
563 writePos += dataSize;
564 written += dataSize;
565 available -= dataSize;
566 AMediaCodec_releaseOutputBuffer(codec, status, false /* render */);
567 if (available == 0) {
568 // there might be more data, but there's no space for it
569 sawOutputEOS = true;
570 }
571 } else if (status == AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED) {
572 ALOGV("output buffers changed");
573 } else if (status == AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED) {
574 AMediaFormat_delete(format);
575 format = AMediaCodec_getOutputFormat(codec);
576 ALOGV("format changed to: %s", AMediaFormat_toString(format));
577 } else if (status == AMEDIACODEC_INFO_TRY_AGAIN_LATER) {
578 ALOGV("no output buffer right now");
579 } else {
580 ALOGV("unexpected info code: %d", status);
581 }
582 }
583
584 AMediaCodec_stop(codec);
585 AMediaCodec_delete(codec);
586 AMediaExtractor_delete(ex);
587 if (!AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_SAMPLE_RATE, (int32_t*) rate) ||
588 !AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_CHANNEL_COUNT, numChannels)) {
589 AMediaFormat_delete(format);
590 return UNKNOWN_ERROR;
591 }
592 AMediaFormat_delete(format);
593 *memsize = written;
594 return OK;
595 }
596 AMediaFormat_delete(format);
597 }
598 AMediaExtractor_delete(ex);
599 return UNKNOWN_ERROR;
600}
601
602status_t Sample::doLoad()
603{
604 uint32_t sampleRate;
605 int numChannels;
606 audio_format_t format;
607 status_t status;
608 mHeap = new MemoryHeapBase(kDefaultHeapSize);
609
610 ALOGV("Start decode");
611 status = decode(mFd, mOffset, mLength, &sampleRate, &numChannels, &format,
612 mHeap, &mSize);
613 ALOGV("close(%d)", mFd);
614 ::close(mFd);
615 mFd = -1;
616 if (status != NO_ERROR) {
617 ALOGE("Unable to load sample");
618 goto error;
619 }
620 ALOGV("pointer = %p, size = %zu, sampleRate = %u, numChannels = %d",
621 mHeap->getBase(), mSize, sampleRate, numChannels);
622
623 if (sampleRate > kMaxSampleRate) {
624 ALOGE("Sample rate (%u) out of range", sampleRate);
625 status = BAD_VALUE;
626 goto error;
627 }
628
Andy Hunga1c35162015-03-06 15:00:42 -0800629 if ((numChannels < 1) || (numChannels > 8)) {
Marco Nelissen372be892014-12-04 08:59:22 -0800630 ALOGE("Sample channel count (%d) out of range", numChannels);
631 status = BAD_VALUE;
632 goto error;
633 }
634
635 mData = new MemoryBase(mHeap, 0, mSize);
636 mSampleRate = sampleRate;
637 mNumChannels = numChannels;
638 mFormat = format;
639 mState = READY;
640 return NO_ERROR;
641
642error:
643 mHeap.clear();
644 return status;
645}
646
647
648void SoundChannel::init(SoundPool* soundPool)
649{
650 mSoundPool = soundPool;
651}
652
653// call with sound pool lock held
654void SoundChannel::play(const sp<Sample>& sample, int nextChannelID, float leftVolume,
655 float rightVolume, int priority, int loop, float rate)
656{
657 sp<AudioTrack> oldTrack;
658 sp<AudioTrack> newTrack;
Glenn Kasten18c225d2015-03-09 08:36:35 -0700659 status_t status;
Marco Nelissen372be892014-12-04 08:59:22 -0800660
661 { // scope for the lock
662 Mutex::Autolock lock(&mLock);
663
664 ALOGV("SoundChannel::play %p: sampleID=%d, channelID=%d, leftVolume=%f, rightVolume=%f,"
665 " priority=%d, loop=%d, rate=%f",
666 this, sample->sampleID(), nextChannelID, leftVolume, rightVolume,
667 priority, loop, rate);
668
669 // if not idle, this voice is being stolen
670 if (mState != IDLE) {
671 ALOGV("channel %d stolen - event queued for channel %d", channelID(), nextChannelID);
672 mNextEvent.set(sample, nextChannelID, leftVolume, rightVolume, priority, loop, rate);
673 stop_l();
674 return;
675 }
676
677 // initialize track
678 size_t afFrameCount;
679 uint32_t afSampleRate;
680 audio_stream_type_t streamType = audio_attributes_to_stream_type(mSoundPool->attributes());
681 if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) {
682 afFrameCount = kDefaultFrameCount;
683 }
684 if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) {
685 afSampleRate = kDefaultSampleRate;
686 }
687 int numChannels = sample->numChannels();
688 uint32_t sampleRate = uint32_t(float(sample->sampleRate()) * rate + 0.5);
689 size_t frameCount = 0;
690
691 if (loop) {
Andy Hunga1c35162015-03-06 15:00:42 -0800692 const audio_format_t format = sample->format();
693 const size_t frameSize = audio_is_linear_pcm(format)
694 ? numChannels * audio_bytes_per_sample(format) : 1;
695 frameCount = sample->size() / frameSize;
Marco Nelissen372be892014-12-04 08:59:22 -0800696 }
697
698#ifndef USE_SHARED_MEM_BUFFER
699 uint32_t totalFrames = (kDefaultBufferCount * afFrameCount * sampleRate) / afSampleRate;
700 // Ensure minimum audio buffer size in case of short looped sample
701 if(frameCount < totalFrames) {
702 frameCount = totalFrames;
703 }
704#endif
705
Glenn Kasten18c225d2015-03-09 08:36:35 -0700706 // mToggle toggles each time a track is started on a given channel.
707 // The toggle is concatenated with the SoundChannel address and passed to AudioTrack
708 // as callback user data. This enables the detection of callbacks received from the old
709 // audio track while the new one is being started and avoids processing them with
710 // wrong audio audio buffer size (mAudioBufferSize)
711 unsigned long toggle = mToggle ^ 1;
712 void *userData = (void *)((unsigned long)this | toggle);
713 audio_channel_mask_t channelMask = audio_channel_out_mask_from_count(numChannels);
Marco Nelissen372be892014-12-04 08:59:22 -0800714
Glenn Kasten18c225d2015-03-09 08:36:35 -0700715 // do not create a new audio track if current track is compatible with sample parameters
716#ifdef USE_SHARED_MEM_BUFFER
717 newTrack = new AudioTrack(streamType, sampleRate, sample->format(),
718 channelMask, sample->getIMemory(), AUDIO_OUTPUT_FLAG_FAST, callback, userData);
719#else
720 uint32_t bufferFrames = (totalFrames + (kDefaultBufferCount - 1)) / kDefaultBufferCount;
721 newTrack = new AudioTrack(streamType, sampleRate, sample->format(),
722 channelMask, frameCount, AUDIO_OUTPUT_FLAG_FAST, callback, userData,
723 bufferFrames);
724#endif
725 oldTrack = mAudioTrack;
726 status = newTrack->initCheck();
727 if (status != NO_ERROR) {
728 ALOGE("Error creating AudioTrack");
729 goto exit;
Marco Nelissen372be892014-12-04 08:59:22 -0800730 }
Glenn Kasten18c225d2015-03-09 08:36:35 -0700731 ALOGV("setVolume %p", newTrack.get());
Marco Nelissen372be892014-12-04 08:59:22 -0800732 newTrack->setVolume(leftVolume, rightVolume);
733 newTrack->setLoop(0, frameCount, loop);
Glenn Kasten18c225d2015-03-09 08:36:35 -0700734
735 // From now on, AudioTrack callbacks received with previous toggle value will be ignored.
736 mToggle = toggle;
737 mAudioTrack = newTrack;
Marco Nelissen372be892014-12-04 08:59:22 -0800738 mPos = 0;
739 mSample = sample;
740 mChannelID = nextChannelID;
741 mPriority = priority;
742 mLoop = loop;
743 mLeftVolume = leftVolume;
744 mRightVolume = rightVolume;
745 mNumChannels = numChannels;
746 mRate = rate;
747 clearNextEvent();
748 mState = PLAYING;
749 mAudioTrack->start();
750 mAudioBufferSize = newTrack->frameCount()*newTrack->frameSize();
751 }
752
753exit:
754 ALOGV("delete oldTrack %p", oldTrack.get());
755 if (status != NO_ERROR) {
756 mAudioTrack.clear();
757 }
758}
759
760void SoundChannel::nextEvent()
761{
762 sp<Sample> sample;
763 int nextChannelID;
764 float leftVolume;
765 float rightVolume;
766 int priority;
767 int loop;
768 float rate;
769
770 // check for valid event
771 {
772 Mutex::Autolock lock(&mLock);
773 nextChannelID = mNextEvent.channelID();
774 if (nextChannelID == 0) {
775 ALOGV("stolen channel has no event");
776 return;
777 }
778
779 sample = mNextEvent.sample();
780 leftVolume = mNextEvent.leftVolume();
781 rightVolume = mNextEvent.rightVolume();
782 priority = mNextEvent.priority();
783 loop = mNextEvent.loop();
784 rate = mNextEvent.rate();
785 }
786
787 ALOGV("Starting stolen channel %d -> %d", channelID(), nextChannelID);
788 play(sample, nextChannelID, leftVolume, rightVolume, priority, loop, rate);
789}
790
791void SoundChannel::callback(int event, void* user, void *info)
792{
793 SoundChannel* channel = static_cast<SoundChannel*>((void *)((unsigned long)user & ~1));
794
795 channel->process(event, info, (unsigned long)user & 1);
796}
797
798void SoundChannel::process(int event, void *info, unsigned long toggle)
799{
800 //ALOGV("process(%d)", mChannelID);
801
802 Mutex::Autolock lock(&mLock);
803
804 AudioTrack::Buffer* b = NULL;
805 if (event == AudioTrack::EVENT_MORE_DATA) {
806 b = static_cast<AudioTrack::Buffer *>(info);
807 }
808
809 if (mToggle != toggle) {
810 ALOGV("process wrong toggle %p channel %d", this, mChannelID);
811 if (b != NULL) {
812 b->size = 0;
813 }
814 return;
815 }
816
817 sp<Sample> sample = mSample;
818
819// ALOGV("SoundChannel::process event %d", event);
820
821 if (event == AudioTrack::EVENT_MORE_DATA) {
822
823 // check for stop state
824 if (b->size == 0) return;
825
826 if (mState == IDLE) {
827 b->size = 0;
828 return;
829 }
830
831 if (sample != 0) {
832 // fill buffer
833 uint8_t* q = (uint8_t*) b->i8;
834 size_t count = 0;
835
836 if (mPos < (int)sample->size()) {
837 uint8_t* p = sample->data() + mPos;
838 count = sample->size() - mPos;
839 if (count > b->size) {
840 count = b->size;
841 }
842 memcpy(q, p, count);
843// ALOGV("fill: q=%p, p=%p, mPos=%u, b->size=%u, count=%d", q, p, mPos, b->size,
844// count);
845 } else if (mPos < mAudioBufferSize) {
846 count = mAudioBufferSize - mPos;
847 if (count > b->size) {
848 count = b->size;
849 }
850 memset(q, 0, count);
851// ALOGV("fill extra: q=%p, mPos=%u, b->size=%u, count=%d", q, mPos, b->size, count);
852 }
853
854 mPos += count;
855 b->size = count;
856 //ALOGV("buffer=%p, [0]=%d", b->i16, b->i16[0]);
857 }
858 } else if (event == AudioTrack::EVENT_UNDERRUN || event == AudioTrack::EVENT_BUFFER_END) {
859 ALOGV("process %p channel %d event %s",
860 this, mChannelID, (event == AudioTrack::EVENT_UNDERRUN) ? "UNDERRUN" :
861 "BUFFER_END");
862 mSoundPool->addToStopList(this);
863 } else if (event == AudioTrack::EVENT_LOOP_END) {
864 ALOGV("End loop %p channel %d", this, mChannelID);
865 } else if (event == AudioTrack::EVENT_NEW_IAUDIOTRACK) {
866 ALOGV("process %p channel %d NEW_IAUDIOTRACK", this, mChannelID);
867 } else {
868 ALOGW("SoundChannel::process unexpected event %d", event);
869 }
870}
871
872
873// call with lock held
874bool SoundChannel::doStop_l()
875{
876 if (mState != IDLE) {
877 setVolume_l(0, 0);
878 ALOGV("stop");
879 mAudioTrack->stop();
Marco Nelissen372be892014-12-04 08:59:22 -0800880 mSample.clear();
881 mState = IDLE;
882 mPriority = IDLE_PRIORITY;
883 return true;
884 }
885 return false;
886}
887
888// call with lock held and sound pool lock held
889void SoundChannel::stop_l()
890{
891 if (doStop_l()) {
892 mSoundPool->done_l(this);
893 }
894}
895
896// call with sound pool lock held
897void SoundChannel::stop()
898{
899 bool stopped;
900 {
901 Mutex::Autolock lock(&mLock);
902 stopped = doStop_l();
903 }
904
905 if (stopped) {
906 mSoundPool->done_l(this);
907 }
908}
909
910//FIXME: Pause is a little broken right now
911void SoundChannel::pause()
912{
913 Mutex::Autolock lock(&mLock);
914 if (mState == PLAYING) {
915 ALOGV("pause track");
916 mState = PAUSED;
917 mAudioTrack->pause();
918 }
919}
920
921void SoundChannel::autoPause()
922{
923 Mutex::Autolock lock(&mLock);
924 if (mState == PLAYING) {
925 ALOGV("pause track");
926 mState = PAUSED;
927 mAutoPaused = true;
928 mAudioTrack->pause();
929 }
930}
931
932void SoundChannel::resume()
933{
934 Mutex::Autolock lock(&mLock);
935 if (mState == PAUSED) {
936 ALOGV("resume track");
937 mState = PLAYING;
938 mAutoPaused = false;
939 mAudioTrack->start();
940 }
941}
942
943void SoundChannel::autoResume()
944{
945 Mutex::Autolock lock(&mLock);
946 if (mAutoPaused && (mState == PAUSED)) {
947 ALOGV("resume track");
948 mState = PLAYING;
949 mAutoPaused = false;
950 mAudioTrack->start();
951 }
952}
953
954void SoundChannel::setRate(float rate)
955{
956 Mutex::Autolock lock(&mLock);
957 if (mAudioTrack != NULL && mSample != 0) {
958 uint32_t sampleRate = uint32_t(float(mSample->sampleRate()) * rate + 0.5);
959 mAudioTrack->setSampleRate(sampleRate);
960 mRate = rate;
961 }
962}
963
964// call with lock held
965void SoundChannel::setVolume_l(float leftVolume, float rightVolume)
966{
967 mLeftVolume = leftVolume;
968 mRightVolume = rightVolume;
969 if (mAudioTrack != NULL)
970 mAudioTrack->setVolume(leftVolume, rightVolume);
971}
972
973void SoundChannel::setVolume(float leftVolume, float rightVolume)
974{
975 Mutex::Autolock lock(&mLock);
976 setVolume_l(leftVolume, rightVolume);
977}
978
979void SoundChannel::setLoop(int loop)
980{
981 Mutex::Autolock lock(&mLock);
982 if (mAudioTrack != NULL && mSample != 0) {
983 uint32_t loopEnd = mSample->size()/mNumChannels/
984 ((mSample->format() == AUDIO_FORMAT_PCM_16_BIT) ? sizeof(int16_t) : sizeof(uint8_t));
985 mAudioTrack->setLoop(0, loopEnd, loop);
986 mLoop = loop;
987 }
988}
989
990SoundChannel::~SoundChannel()
991{
992 ALOGV("SoundChannel destructor %p", this);
993 {
994 Mutex::Autolock lock(&mLock);
995 clearNextEvent();
996 doStop_l();
997 }
998 // do not call AudioTrack destructor with mLock held as it will wait for the AudioTrack
999 // callback thread to exit which may need to execute process() and acquire the mLock.
1000 mAudioTrack.clear();
1001}
1002
1003void SoundChannel::dump()
1004{
1005 ALOGV("mState = %d mChannelID=%d, mNumChannels=%d, mPos = %d, mPriority=%d, mLoop=%d",
1006 mState, mChannelID, mNumChannels, mPos, mPriority, mLoop);
1007}
1008
1009void SoundEvent::set(const sp<Sample>& sample, int channelID, float leftVolume,
1010 float rightVolume, int priority, int loop, float rate)
1011{
1012 mSample = sample;
1013 mChannelID = channelID;
1014 mLeftVolume = leftVolume;
1015 mRightVolume = rightVolume;
1016 mPriority = priority;
1017 mLoop = loop;
1018 mRate =rate;
1019}
1020
1021} // end namespace android