blob: bf99c4d6aaa7376dfd1a81e5f47a834182b397dc [file] [log] [blame]
Hugo Hudsonb83ad732011-07-14 23:31:17 +01001/*
2 * Copyright (C) 2011 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#include <variablespeed.h>
18
19#include <unistd.h>
20#include <stdlib.h>
21
22#include <sola_time_scaler.h>
23#include <ring_buffer.h>
24
25#include <hlogging.h>
26
27#include <vector>
28
29// ****************************************************************************
30// Constants, utility methods, structures and other miscellany used throughout
31// this file.
32
33namespace {
34
35// These variables are used to determine the size of the buffer queue used by
36// the decoder.
37// This is not the same as the large buffer used to hold the uncompressed data
38// - for that see the member variable decodeBuffer_.
39// The choice of 1152 corresponds to the number of samples per mp3 frame, so is
40// a good choice of size for a decoding buffer in the absence of other
41// information (we don't know exactly what formats we will be working with).
42const size_t kNumberOfBuffersInQueue = 4;
43const size_t kNumberOfSamplesPerBuffer = 1152;
44const size_t kBufferSizeInBytes = 2 * kNumberOfSamplesPerBuffer;
45const size_t kSampleSizeInBytes = 4;
46
47// When calculating play buffer size before pushing to audio player.
48const size_t kNumberOfBytesPerInt16 = 2;
49
50// How long to sleep during the main play loop and the decoding callback loop.
51// In due course this should be replaced with the better signal and wait on
52// condition rather than busy-looping.
53const int kSleepTimeMicros = 1000;
54
55// Used in detecting errors with the OpenSL ES framework.
56const SLuint32 kPrefetchErrorCandidate =
57 SL_PREFETCHEVENT_STATUSCHANGE | SL_PREFETCHEVENT_FILLLEVELCHANGE;
58
59// Structure used when we perform a decoding callback.
60typedef struct CallbackContext_ {
Hugo Hudson9730f152011-07-25 17:04:42 +010061 SLMetadataExtractionItf decoderMetadata;
Hugo Hudsonb83ad732011-07-14 23:31:17 +010062 // Pointer to local storage buffers for decoded audio data.
63 int8_t* pDataBase;
64 // Pointer to the current buffer within local storage.
65 int8_t* pData;
66} CallbackContext;
67
68// Local storage for decoded audio data.
69int8_t pcmData[kNumberOfBuffersInQueue * kBufferSizeInBytes];
70
71#define CheckSLResult(message, result) \
72 CheckSLResult_Real(message, result, __LINE__)
73
74// Helper function for debugging - checks the OpenSL result for success.
75void CheckSLResult_Real(const char* message, SLresult result, int line) {
76 // This can be helpful when debugging.
77 // LOGD("sl result %d for %s", result, message);
78 if (SL_RESULT_SUCCESS != result) {
79 LOGE("slresult was %d at %s file variablespeed line %d",
80 static_cast<int>(result), message, line);
81 }
82 CHECK(SL_RESULT_SUCCESS == result);
83}
84
85} // namespace
86
87// ****************************************************************************
88// Static instance of audio engine, and methods for getting, setting and
89// deleting it.
90
91// The single global audio engine instance.
92AudioEngine* AudioEngine::audioEngine_ = NULL;
93android::Mutex publishEngineLock_;
94
95AudioEngine* AudioEngine::GetEngine() {
96 android::Mutex::Autolock autoLock(publishEngineLock_);
97 if (audioEngine_ == NULL) {
98 LOGE("you haven't initialized the audio engine");
99 CHECK(false);
100 return NULL;
101 }
102 return audioEngine_;
103}
104
105void AudioEngine::SetEngine(AudioEngine* engine) {
106 if (audioEngine_ != NULL) {
107 LOGE("you have already set the audio engine");
108 CHECK(false);
109 return;
110 }
111 audioEngine_ = engine;
112}
113
114void AudioEngine::DeleteEngine() {
115 if (audioEngine_ == NULL) {
116 LOGE("you haven't initialized the audio engine");
117 CHECK(false);
118 return;
119 }
120 delete audioEngine_;
121 audioEngine_ = NULL;
122}
123
124// ****************************************************************************
125// The callbacks from the engine require static callback functions.
126// Here are the static functions - they just delegate to instance methods on
127// the engine.
128
129static void PlayingBufferQueueCb(SLAndroidSimpleBufferQueueItf, void*) {
130 AudioEngine::GetEngine()->PlayingBufferQueueCallback();
131}
132
133static void PrefetchEventCb(SLPrefetchStatusItf caller, void*, SLuint32 event) {
134 AudioEngine::GetEngine()->PrefetchEventCallback(caller, event);
135}
136
137static void DecodingBufferQueueCb(SLAndroidSimpleBufferQueueItf queueItf,
138 void *context) {
139 AudioEngine::GetEngine()->DecodingBufferQueueCallback(queueItf, context);
140}
141
142static void DecodingEventCb(SLPlayItf caller, void*, SLuint32 event) {
143 AudioEngine::GetEngine()->DecodingEventCallback(caller, event);
144}
145
146// ****************************************************************************
147// Static utility methods.
148
149static void PausePlaying(SLPlayItf playItf) {
150 SLresult result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PAUSED);
151 CheckSLResult("pause playing", result);
152}
153
154static void StartPlaying(SLPlayItf playItf) {
155 SLresult result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
156 CheckSLResult("start playing", result);
157}
158
159static void StopPlaying(SLPlayItf playItf) {
160 SLresult result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
161 CheckSLResult("stop playing", result);
162}
163
Hugo Hudson9730f152011-07-25 17:04:42 +0100164static void ExtractMetadataFromDecoder(
165 SLMetadataExtractionItf decoderMetadata) {
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100166 SLuint32 itemCount;
Hugo Hudson9730f152011-07-25 17:04:42 +0100167 SLresult result = (*decoderMetadata)->GetItemCount(
168 decoderMetadata, &itemCount);
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100169 CheckSLResult("getting item count", result);
170 SLuint32 i, keySize, valueSize;
171 SLMetadataInfo *keyInfo, *value;
172 for (i = 0; i < itemCount ; ++i) {
173 keyInfo = NULL;
174 keySize = 0;
175 value = NULL;
176 valueSize = 0;
177 result = (*decoderMetadata)->GetKeySize(decoderMetadata, i, &keySize);
178 CheckSLResult("get key size", result);
179 keyInfo = static_cast<SLMetadataInfo*>(malloc(keySize));
180 if (keyInfo) {
181 result = (*decoderMetadata)->GetKey(
182 decoderMetadata, i, keySize, keyInfo);
183 CheckSLResult("get key", result);
184 if (keyInfo->encoding == SL_CHARACTERENCODING_ASCII
185 || keyInfo->encoding == SL_CHARACTERENCODING_UTF8) {
186 result = (*decoderMetadata)->GetValueSize(
187 decoderMetadata, i, &valueSize);
188 CheckSLResult("get value size", result);
189 value = static_cast<SLMetadataInfo*>(malloc(valueSize));
190 if (value) {
191 result = (*decoderMetadata)->GetValue(
192 decoderMetadata, i, valueSize, value);
193 CheckSLResult("get value", result);
194 if (value->encoding == SL_CHARACTERENCODING_BINARY) {
195 LOGD("key[%d] size=%d, name=%s value size=%d value=%d",
196 i, keyInfo->size, keyInfo->data, value->size,
197 *(reinterpret_cast<SLuint32*>(value->data)));
198 }
199 free(value);
200 }
201 }
202 free(keyInfo);
203 }
204 }
205}
206
207static void SeekToPosition(SLSeekItf seekItf, size_t startPositionMillis) {
208 SLresult result = (*seekItf)->SetPosition(
209 seekItf, startPositionMillis, SL_SEEKMODE_ACCURATE);
210 CheckSLResult("seek to position", result);
211}
212
213static void RegisterCallbackContextAndAddEnqueueBuffersToDecoder(
Hugo Hudson9730f152011-07-25 17:04:42 +0100214 SLAndroidSimpleBufferQueueItf decoderQueue,
215 SLMetadataExtractionItf decoderMetadata, android::Mutex &callbackLock,
216 CallbackContext* context) {
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100217 android::Mutex::Autolock autoLock(callbackLock);
218 // Initialize the callback structure, used during the decoding.
219 // Then register a callback on the decoder queue, so that we will be called
220 // throughout the decoding process (and can then extract the decoded audio
221 // for the next bit of the pipeline).
Hugo Hudson9730f152011-07-25 17:04:42 +0100222 context->decoderMetadata = decoderMetadata;
223 context->pDataBase = pcmData;
224 context->pData = pcmData;
225
226 SLresult result = (*decoderQueue)->RegisterCallback(
227 decoderQueue, DecodingBufferQueueCb, context);
228 CheckSLResult("decode callback", result);
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100229
230 // Enqueue buffers to map the region of memory allocated to store the
231 // decoded data.
232 for (size_t i = 0; i < kNumberOfBuffersInQueue; i++) {
233 SLresult result = (*decoderQueue)->Enqueue(
Hugo Hudson9730f152011-07-25 17:04:42 +0100234 decoderQueue, context->pData, kBufferSizeInBytes);
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100235 CheckSLResult("enqueue something", result);
Hugo Hudson9730f152011-07-25 17:04:42 +0100236 context->pData += kBufferSizeInBytes;
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100237 }
Hugo Hudson9730f152011-07-25 17:04:42 +0100238 context->pData = context->pDataBase;
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100239}
240
241// ****************************************************************************
242// Constructor and Destructor.
243
244AudioEngine::AudioEngine(size_t channels, size_t sampleRate,
245 size_t targetFrames, float windowDuration, float windowOverlapDuration,
246 size_t maxPlayBufferCount, float initialRate, size_t decodeInitialSize,
247 size_t decodeMaxSize, size_t startPositionMillis)
248 : decodeBuffer_(decodeInitialSize, decodeMaxSize),
249 playingBuffers_(), freeBuffers_(), timeScaler_(NULL),
250 floatBuffer_(NULL), injectBuffer_(NULL),
Hugo Hudson9730f152011-07-25 17:04:42 +0100251 channels_(channels), sampleRate_(sampleRate),
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100252 targetFrames_(targetFrames),
253 windowDuration_(windowDuration),
254 windowOverlapDuration_(windowOverlapDuration),
255 maxPlayBufferCount_(maxPlayBufferCount), initialRate_(initialRate),
256 startPositionMillis_(startPositionMillis),
257 totalDurationMs_(0), startRequested_(false),
258 stopRequested_(false), finishedDecoding_(false) {
Hugo Hudson9730f152011-07-25 17:04:42 +0100259 floatBuffer_ = new float[targetFrames_ * channels_];
260 injectBuffer_ = new float[targetFrames_ * channels_];
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100261}
262
263AudioEngine::~AudioEngine() {
264 // destroy the time scaler
265 if (timeScaler_ != NULL) {
266 delete timeScaler_;
267 timeScaler_ = NULL;
268 }
269
270 // delete all outstanding playing and free buffers
271 android::Mutex::Autolock autoLock(playBufferLock_);
272 while (playingBuffers_.size() > 0) {
273 delete[] playingBuffers_.front();
274 playingBuffers_.pop();
275 }
276 while (freeBuffers_.size() > 0) {
277 delete[] freeBuffers_.top();
278 freeBuffers_.pop();
279 }
280
281 delete[] floatBuffer_;
282 floatBuffer_ = NULL;
283 delete[] injectBuffer_;
284 injectBuffer_ = NULL;
285}
286
287// ****************************************************************************
288// Regular AudioEngine class methods.
289
290void AudioEngine::SetVariableSpeed(float speed) {
291 GetTimeScaler()->set_speed(speed);
292}
293
294void AudioEngine::RequestStart() {
295 android::Mutex::Autolock autoLock(lock_);
296 startRequested_ = true;
297}
298
299void AudioEngine::ClearRequestStart() {
300 android::Mutex::Autolock autoLock(lock_);
301 startRequested_ = false;
302}
303
304bool AudioEngine::GetWasStartRequested() {
305 android::Mutex::Autolock autoLock(lock_);
306 return startRequested_;
307}
308
309void AudioEngine::RequestStop() {
310 android::Mutex::Autolock autoLock(lock_);
311 stopRequested_ = true;
312}
313
314int AudioEngine::GetCurrentPosition() {
315 android::Mutex::Autolock autoLock(decodeBufferLock_);
316 double result = decodeBuffer_.GetTotalAdvancedCount();
317 return static_cast<int>(
318 (result * 1000) / sampleRate_ / channels_ + startPositionMillis_);
319}
320
321int AudioEngine::GetTotalDuration() {
322 android::Mutex::Autolock autoLock(lock_);
323 return static_cast<int>(totalDurationMs_);
324}
325
326video_editing::SolaTimeScaler* AudioEngine::GetTimeScaler() {
327 if (timeScaler_ == NULL) {
328 timeScaler_ = new video_editing::SolaTimeScaler();
329 timeScaler_->Init(sampleRate_, channels_, initialRate_, windowDuration_,
330 windowOverlapDuration_);
331 }
332 return timeScaler_;
333}
334
335void AudioEngine::PrefetchDurationSampleRateAndChannels(
336 SLPlayItf playItf, SLPrefetchStatusItf prefetchItf) {
337 // Set play state to pause, to begin the prefetching.
338 PausePlaying(playItf);
339
340 // Wait until the data has been prefetched.
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100341 {
342 SLuint32 prefetchStatus = SL_PREFETCHSTATUS_UNDERFLOW;
343 android::Mutex::Autolock autoLock(prefetchLock_);
344 while (prefetchStatus != SL_PREFETCHSTATUS_SUFFICIENTDATA) {
345 LOGI("waiting for condition");
346 // prefetchCondition_.waitRelative(prefetchLock, 1000 * 1000 * 10);
347 usleep(10 * 1000);
348 LOGI("getting the value");
349 (*prefetchItf)->GetPrefetchStatus(prefetchItf, &prefetchStatus);
350 }
351 LOGI("done with wait");
352 }
353
354 SLmillisecond durationInMsec = SL_TIME_UNKNOWN;
355 SLresult result = (*playItf)->GetDuration(playItf, &durationInMsec);
356 CheckSLResult("getting duration", result);
357 CHECK(durationInMsec != SL_TIME_UNKNOWN);
358 LOGD("duration: %d", static_cast<int>(durationInMsec));
359 android::Mutex::Autolock autoLock(lock_);
360 totalDurationMs_ = durationInMsec;
361}
362
363bool AudioEngine::EnqueueNextBufferOfAudio(
364 SLAndroidSimpleBufferQueueItf audioPlayerQueue) {
365 size_t frameSizeInBytes = kSampleSizeInBytes * channels_;
366 size_t frameCount = 0;
367 while (frameCount < targetFrames_) {
368 size_t framesLeft = targetFrames_ - frameCount;
369 // If there is data already in the time scaler, retrieve it.
370 if (GetTimeScaler()->available() > 0) {
371 size_t retrieveCount = min(GetTimeScaler()->available(), framesLeft);
372 int count = GetTimeScaler()->RetrieveSamples(
373 floatBuffer_ + frameCount * channels_, retrieveCount);
374 if (count <= 0) {
375 LOGD("ERROR: Count was %d", count);
376 break;
377 }
378 frameCount += count;
379 continue;
380 }
381 // If there is no data in the time scaler, then feed some into it.
382 android::Mutex::Autolock autoLock(decodeBufferLock_);
383 size_t framesInDecodeBuffer =
384 decodeBuffer_.GetSizeInBytes() / frameSizeInBytes;
385 size_t framesScalerCanHandle = GetTimeScaler()->input_limit();
386 size_t framesToInject = min(framesInDecodeBuffer,
387 min(targetFrames_, framesScalerCanHandle));
388 if (framesToInject <= 0) {
389 // No more frames left to inject.
390 break;
391 }
392 for (size_t i = 0; i < framesToInject * channels_ ; ++i) {
393 injectBuffer_[i] = decodeBuffer_.GetAtIndex(i);
394 }
395 int count = GetTimeScaler()->InjectSamples(injectBuffer_, framesToInject);
396 if (count <= 0) {
397 LOGD("ERROR: Count was %d", count);
398 break;
399 }
400 decodeBuffer_.AdvanceHeadPointerShorts(count * channels_);
401 }
402 if (frameCount <= 0) {
403 // We must have finished playback.
404 if (GetEndOfDecoderReached()) {
405 // If we've finished decoding, clear the buffer - so we will terminate.
406 ClearDecodeBuffer();
407 }
408 return false;
409 }
410
411 // Get a free playing buffer.
412 int16* playBuffer;
413 {
414 android::Mutex::Autolock autoLock(playBufferLock_);
415 if (freeBuffers_.size() > 0) {
416 // If we have a free buffer, recycle it.
417 playBuffer = freeBuffers_.top();
418 freeBuffers_.pop();
419 } else {
420 // Otherwise allocate a new one.
421 playBuffer = new int16[targetFrames_ * channels_];
422 }
423 }
424
425 // Try to play the buffer.
426 for (size_t i = 0; i < frameCount * channels_ ; ++i) {
427 playBuffer[i] = floatBuffer_[i];
428 }
429 size_t sizeOfPlayBufferInBytes =
430 frameCount * channels_ * kNumberOfBytesPerInt16;
431 SLresult result = (*audioPlayerQueue)->Enqueue(audioPlayerQueue, playBuffer,
432 sizeOfPlayBufferInBytes);
433 CheckSLResult("enqueue prebuilt audio", result);
434 if (result == SL_RESULT_SUCCESS) {
435 android::Mutex::Autolock autoLock(playBufferLock_);
436 playingBuffers_.push(playBuffer);
437 } else {
438 LOGE("could not enqueue audio buffer");
439 delete[] playBuffer;
440 }
441
442 return (result == SL_RESULT_SUCCESS);
443}
444
445bool AudioEngine::GetEndOfDecoderReached() {
446 android::Mutex::Autolock autoLock(lock_);
447 return finishedDecoding_;
448}
449
450void AudioEngine::SetEndOfDecoderReached() {
451 android::Mutex::Autolock autoLock(lock_);
452 finishedDecoding_ = true;
453}
454
455bool AudioEngine::PlayFileDescriptor(int fd, int64 offset, int64 length) {
456 SLDataLocator_AndroidFD loc_fd = {
457 SL_DATALOCATOR_ANDROIDFD, fd, offset, length };
458 SLDataFormat_MIME format_mime = {
459 SL_DATAFORMAT_MIME, NULL, SL_CONTAINERTYPE_UNSPECIFIED };
460 SLDataSource audioSrc = { &loc_fd, &format_mime };
461 return PlayFromThisSource(audioSrc);
462}
463
464bool AudioEngine::PlayUri(const char* uri) {
465 // Source of audio data for the decoding
466 SLDataLocator_URI decUri = { SL_DATALOCATOR_URI,
467 const_cast<SLchar*>(reinterpret_cast<const SLchar*>(uri)) };
468 SLDataFormat_MIME decMime = {
469 SL_DATAFORMAT_MIME, NULL, SL_CONTAINERTYPE_UNSPECIFIED };
470 SLDataSource decSource = { &decUri, &decMime };
471 return PlayFromThisSource(decSource);
472}
473
474bool AudioEngine::IsDecodeBufferEmpty() {
475 android::Mutex::Autolock autoLock(decodeBufferLock_);
476 return decodeBuffer_.GetSizeInBytes() <= 0;
477}
478
479void AudioEngine::ClearDecodeBuffer() {
480 android::Mutex::Autolock autoLock(decodeBufferLock_);
481 decodeBuffer_.Clear();
482}
483
Hugo Hudson9730f152011-07-25 17:04:42 +0100484static void CreateAndRealizeEngine(SLObjectItf &engine,
485 SLEngineItf &engineInterface) {
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100486 SLEngineOption EngineOption[] = { {
487 SL_ENGINEOPTION_THREADSAFE, SL_BOOLEAN_TRUE } };
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100488 SLresult result = slCreateEngine(&engine, 1, EngineOption, 0, NULL, NULL);
489 CheckSLResult("create engine", result);
490 result = (*engine)->Realize(engine, SL_BOOLEAN_FALSE);
491 CheckSLResult("realise engine", result);
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100492 result = (*engine)->GetInterface(engine, SL_IID_ENGINE, &engineInterface);
493 CheckSLResult("get interface", result);
Hugo Hudson9730f152011-07-25 17:04:42 +0100494}
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100495
Hugo Hudson9730f152011-07-25 17:04:42 +0100496static void CreateAndRealizeOutputMix(SLEngineItf &engineInterface,
497 SLObjectItf &outputMix) {
498 SLresult result;
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100499 // Create the output mix for playing.
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100500 result = (*engineInterface)->CreateOutputMix(
501 engineInterface, &outputMix, 0, NULL, NULL);
502 CheckSLResult("create output mix", result);
503 result = (*outputMix)->Realize(outputMix, SL_BOOLEAN_FALSE);
504 CheckSLResult("realize", result);
Hugo Hudson9730f152011-07-25 17:04:42 +0100505}
506
507static void CreateAndRealizeAudioPlayer(size_t sampleRate, size_t channels,
508 SLObjectItf &outputMix, SLObjectItf &audioPlayer,
509 SLEngineItf &engineInterface) {
510 SLresult result;
511 SLuint32 slSampleRate;
512 SLuint32 slOutputChannels;
513 switch (sampleRate) {
514 case 44100:
515 slSampleRate = SL_SAMPLINGRATE_44_1;
516 break;
517 case 8000:
518 slSampleRate = SL_SAMPLINGRATE_8;
519 break;
520 case 11025:
521 slSampleRate = SL_SAMPLINGRATE_11_025;
522 break;
523 default:
524 LOGE("unknown sample rate, using SL_SAMPLINGRATE_44_1");
525 slSampleRate = SL_SAMPLINGRATE_44_1;
526 break;
527 }
528 switch (channels) {
529 case 2:
530 slOutputChannels = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
531 break;
532 case 1:
533 slOutputChannels = SL_SPEAKER_FRONT_LEFT;
534 break;
535 default:
536 LOGE("unknown channels, using 2");
537 slOutputChannels = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
538 break;
539 }
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100540
541 // Define the source and sink for the audio player: comes from a buffer queue
542 // and goes to the output mix.
543 SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {
544 SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2 };
Hugo Hudson9730f152011-07-25 17:04:42 +0100545 SLDataFormat_PCM format_pcm = {SL_DATAFORMAT_PCM, channels, slSampleRate,
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100546 SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16,
Hugo Hudson9730f152011-07-25 17:04:42 +0100547 slOutputChannels, SL_BYTEORDER_LITTLEENDIAN};
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100548 SLDataSource playingSrc = {&loc_bufq, &format_pcm};
549 SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, outputMix};
550 SLDataSink audioSnk = {&loc_outmix, NULL};
551
552 // Create the audio player, which will play from the buffer queue and send to
553 // the output mix.
554 const size_t playerInterfaceCount = 1;
555 const SLInterfaceID iids[playerInterfaceCount] = {
556 SL_IID_ANDROIDSIMPLEBUFFERQUEUE };
557 const SLboolean reqs[playerInterfaceCount] = { SL_BOOLEAN_TRUE };
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100558 result = (*engineInterface)->CreateAudioPlayer(engineInterface, &audioPlayer,
559 &playingSrc, &audioSnk, playerInterfaceCount, iids, reqs);
560 CheckSLResult("create audio player", result);
561 result = (*audioPlayer)->Realize(audioPlayer, SL_BOOLEAN_FALSE);
562 CheckSLResult("realize buffer queue", result);
Hugo Hudson9730f152011-07-25 17:04:42 +0100563}
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100564
Hugo Hudson9730f152011-07-25 17:04:42 +0100565static void GetAudioPlayInterfacesAndRegisterCallback(SLObjectItf &audioPlayer,
566 SLPlayItf &audioPlayerPlay,
567 SLAndroidSimpleBufferQueueItf &audioPlayerQueue) {
568 SLresult result;
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100569 // Get the play interface from the player, as well as the buffer queue
570 // interface from its source.
571 // Register for callbacks during play.
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100572 result = (*audioPlayer)->GetInterface(
573 audioPlayer, SL_IID_PLAY, &audioPlayerPlay);
574 CheckSLResult("get interface", result);
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100575 result = (*audioPlayer)->GetInterface(audioPlayer,
576 SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &audioPlayerQueue);
577 CheckSLResult("get interface again", result);
578 result = (*audioPlayerQueue)->RegisterCallback(
579 audioPlayerQueue, PlayingBufferQueueCb, NULL);
580 CheckSLResult("register callback", result);
Hugo Hudson9730f152011-07-25 17:04:42 +0100581}
582
583bool AudioEngine::PlayFromThisSource(const SLDataSource& audioSrc) {
584 ClearDecodeBuffer();
585
586 SLresult result;
587
588 SLObjectItf engine;
589 SLEngineItf engineInterface;
590 CreateAndRealizeEngine(engine, engineInterface);
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100591
592 // Define the source and sink for the decoding player: comes from the source
593 // this method was called with, is sent to another buffer queue.
594 SLDataLocator_AndroidSimpleBufferQueue decBuffQueue;
595 decBuffQueue.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE;
596 decBuffQueue.numBuffers = kNumberOfBuffersInQueue;
597 // A valid value seems required here but is currently ignored.
Hugo Hudson9730f152011-07-25 17:04:42 +0100598 SLDataFormat_PCM pcm = {SL_DATAFORMAT_PCM, 1, SL_SAMPLINGRATE_44_1,
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100599 SL_PCMSAMPLEFORMAT_FIXED_16, 16,
600 SL_SPEAKER_FRONT_LEFT, SL_BYTEORDER_LITTLEENDIAN};
601 SLDataSink decDest = { &decBuffQueue, &pcm };
602
603 // Create the decoder with the given source and sink.
604 const size_t decoderInterfaceCount = 4;
605 SLObjectItf decoder;
606 const SLInterfaceID decodePlayerInterfaces[decoderInterfaceCount] = {
607 SL_IID_ANDROIDSIMPLEBUFFERQUEUE, SL_IID_PREFETCHSTATUS, SL_IID_SEEK,
608 SL_IID_METADATAEXTRACTION };
609 const SLboolean decodePlayerRequired[decoderInterfaceCount] = {
610 SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
611 SLDataSource sourceCopy(audioSrc);
612 result = (*engineInterface)->CreateAudioPlayer(engineInterface, &decoder,
613 &sourceCopy, &decDest, decoderInterfaceCount, decodePlayerInterfaces,
614 decodePlayerRequired);
615 CheckSLResult("create audio player", result);
616 result = (*decoder)->Realize(decoder, SL_BOOLEAN_FALSE);
617 CheckSLResult("realize in sync mode", result);
618
619 // Get the play interface from the decoder, and register event callbacks.
620 // Get the buffer queue, prefetch and seek interfaces.
Hugo Hudson9730f152011-07-25 17:04:42 +0100621 SLPlayItf decoderPlay = NULL;
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100622 result = (*decoder)->GetInterface(decoder, SL_IID_PLAY, &decoderPlay);
623 CheckSLResult("get play interface, implicit", result);
624 result = (*decoderPlay)->SetCallbackEventsMask(
625 decoderPlay, SL_PLAYEVENT_HEADATEND);
626 CheckSLResult("set the event mask", result);
627 result = (*decoderPlay)->RegisterCallback(
628 decoderPlay, DecodingEventCb, NULL);
629 CheckSLResult("register decoding event callback", result);
630 SLAndroidSimpleBufferQueueItf decoderQueue;
631 result = (*decoder)->GetInterface(
632 decoder, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &decoderQueue);
633 CheckSLResult("get decoder buffer queue", result);
634 SLPrefetchStatusItf decoderPrefetch;
635 result = (*decoder)->GetInterface(
636 decoder, SL_IID_PREFETCHSTATUS, &decoderPrefetch);
637 CheckSLResult("get prefetch status interface", result);
638 SLSeekItf decoderSeek;
639 result = (*decoder)->GetInterface(decoder, SL_IID_SEEK, &decoderSeek);
640 CheckSLResult("get seek interface", result);
641
Hugo Hudson9730f152011-07-25 17:04:42 +0100642 // Get the metadata interface from the decoder.
643 SLMetadataExtractionItf decoderMetadata;
644 result = (*decoder)->GetInterface(decoder,
645 SL_IID_METADATAEXTRACTION, &decoderMetadata);
646 CheckSLResult("getting metadata interface", result);
647
648 CallbackContext callbackContext;
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100649 RegisterCallbackContextAndAddEnqueueBuffersToDecoder(
Hugo Hudson9730f152011-07-25 17:04:42 +0100650 decoderQueue, decoderMetadata, callbackLock_, &callbackContext);
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100651
652 // Initialize the callback for prefetch errors, if we can't open the
653 // resource to decode.
654 result = (*decoderPrefetch)->SetCallbackEventsMask(
655 decoderPrefetch, kPrefetchErrorCandidate);
656 CheckSLResult("set prefetch callback mask", result);
657 result = (*decoderPrefetch)->RegisterCallback(
658 decoderPrefetch, PrefetchEventCb, &decoderPrefetch);
659 CheckSLResult("set prefetch callback", result);
660
661 SeekToPosition(decoderSeek, startPositionMillis_);
662
663 PrefetchDurationSampleRateAndChannels(decoderPlay, decoderPrefetch);
664
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100665 StartPlaying(decoderPlay);
666
Hugo Hudson9730f152011-07-25 17:04:42 +0100667 SLObjectItf outputMix = NULL;
668 SLObjectItf audioPlayer = NULL;
669 SLPlayItf audioPlayerPlay = NULL;
670 SLAndroidSimpleBufferQueueItf audioPlayerQueue = NULL;
671
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100672 // The main loop - until we're told to stop: if there is audio data coming
673 // out of the decoder, feed it through the time scaler.
674 // As it comes out of the time scaler, feed it into the audio player.
675 while (!Finished()) {
676 if (GetWasStartRequested()) {
Hugo Hudson9730f152011-07-25 17:04:42 +0100677 CreateAndRealizeOutputMix(engineInterface, outputMix);
678 CreateAndRealizeAudioPlayer(sampleRate_, channels_, outputMix,
679 audioPlayer, engineInterface);
680 GetAudioPlayInterfacesAndRegisterCallback(audioPlayer, audioPlayerPlay,
681 audioPlayerQueue);
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100682 ClearRequestStart();
683 StartPlaying(audioPlayerPlay);
684 }
685 EnqueueMoreAudioIfNecessary(audioPlayerQueue);
686 usleep(kSleepTimeMicros);
687 }
688
Hugo Hudson9730f152011-07-25 17:04:42 +0100689 // Delete the audio player and output mix, iff they have been created.
690 if (audioPlayer != NULL) {
691 StopPlaying(audioPlayerPlay);
692 result = (*audioPlayerQueue)->Clear(audioPlayerQueue);
693 CheckSLResult("clear audio player queue", result);
694 result = (*audioPlayerQueue)->RegisterCallback(audioPlayerQueue, NULL, NULL);
695 CheckSLResult("clear callback", result);
696 (*audioPlayer)->AbortAsyncOperation(audioPlayer);
697 (*audioPlayer)->Destroy(audioPlayer);
698 (*outputMix)->Destroy(outputMix);
699 audioPlayer = NULL;
700 audioPlayerPlay = NULL;
701 audioPlayerQueue = NULL;
702 outputMix = NULL;
703 }
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100704
705 // Delete the decoder.
Hugo Hudson9730f152011-07-25 17:04:42 +0100706 StopPlaying(decoderPlay);
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100707 result = (*decoderPrefetch)->RegisterCallback(decoderPrefetch, NULL, NULL);
708 CheckSLResult("clearing prefetch error callback", result);
Hugo Hudson9730f152011-07-25 17:04:42 +0100709 // This is returning slresult 13 if I do no playback.
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100710 // Repro is to comment out all before this line, and all after enqueueing
711 // my buffers.
712 // result = (*decoderQueue)->Clear(decoderQueue);
713 // CheckSLResult("clearing decode buffer queue", result);
714 result = (*decoderQueue)->RegisterCallback(decoderQueue, NULL, NULL);
715 CheckSLResult("clearing decode callback", result);
716 decoderSeek = NULL;
717 decoderPrefetch = NULL;
718 decoderQueue = NULL;
719 result = (*decoderPlay)->RegisterCallback(decoderPlay, NULL, NULL);
720 CheckSLResult("clear decoding event callback", result);
721 (*decoder)->AbortAsyncOperation(decoder);
722 decoderPlay = NULL;
723 (*decoder)->Destroy(decoder);
724
Hugo Hudson9730f152011-07-25 17:04:42 +0100725 // Delete the engine.
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100726 (*engine)->Destroy(engine);
Hugo Hudson9730f152011-07-25 17:04:42 +0100727 engineInterface = NULL;
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100728
729 return true;
730}
731
732bool AudioEngine::Finished() {
733 if (GetWasStopRequested()) {
734 return true;
735 }
736 android::Mutex::Autolock autoLock(playBufferLock_);
737 return playingBuffers_.size() <= 0 &&
738 IsDecodeBufferEmpty() &&
739 GetEndOfDecoderReached();
740}
741
742bool AudioEngine::GetWasStopRequested() {
743 android::Mutex::Autolock autoLock(lock_);
744 return stopRequested_;
745}
746
747bool AudioEngine::GetHasReachedPlayingBuffersLimit() {
748 android::Mutex::Autolock autoLock(playBufferLock_);
749 return playingBuffers_.size() >= maxPlayBufferCount_;
750}
751
752void AudioEngine::EnqueueMoreAudioIfNecessary(
753 SLAndroidSimpleBufferQueueItf audioPlayerQueue) {
754 bool keepEnqueueing = true;
755 while (!GetWasStopRequested() &&
756 !IsDecodeBufferEmpty() &&
757 !GetHasReachedPlayingBuffersLimit() &&
758 keepEnqueueing) {
759 keepEnqueueing = EnqueueNextBufferOfAudio(audioPlayerQueue);
760 }
761}
762
763bool AudioEngine::DecodeBufferTooFull() {
764 android::Mutex::Autolock autoLock(decodeBufferLock_);
765 return decodeBuffer_.IsTooLarge();
766}
767
768// ****************************************************************************
769// Code for handling the static callbacks.
770
771void AudioEngine::PlayingBufferQueueCallback() {
772 // The head playing buffer is done, move it to the free list.
773 android::Mutex::Autolock autoLock(playBufferLock_);
774 if (playingBuffers_.size() > 0) {
775 freeBuffers_.push(playingBuffers_.front());
776 playingBuffers_.pop();
777 }
778}
779
780void AudioEngine::PrefetchEventCallback(
781 SLPrefetchStatusItf caller, SLuint32 event) {
782 // If there was a problem during decoding, then signal the end.
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100783 SLpermille level = 0;
784 SLresult result = (*caller)->GetFillLevel(caller, &level);
785 CheckSLResult("get fill level", result);
786 SLuint32 status;
787 result = (*caller)->GetPrefetchStatus(caller, &status);
788 CheckSLResult("get prefetch status", result);
789 if ((kPrefetchErrorCandidate == (event & kPrefetchErrorCandidate)) &&
790 (level == 0) &&
791 (status == SL_PREFETCHSTATUS_UNDERFLOW)) {
792 LOGI("PrefetchEventCallback error while prefetching data");
793 SetEndOfDecoderReached();
794 }
795 if (SL_PREFETCHSTATUS_SUFFICIENTDATA == event) {
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100796 // android::Mutex::Autolock autoLock(prefetchLock_);
797 // prefetchCondition_.broadcast();
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100798 }
799}
800
801void AudioEngine::DecodingBufferQueueCallback(
802 SLAndroidSimpleBufferQueueItf queueItf, void *context) {
803 if (GetWasStopRequested()) {
804 return;
805 }
806
807 CallbackContext *pCntxt;
808 {
809 android::Mutex::Autolock autoLock(callbackLock_);
810 pCntxt = reinterpret_cast<CallbackContext*>(context);
811 }
812 {
813 android::Mutex::Autolock autoLock(decodeBufferLock_);
814 decodeBuffer_.AddData(pCntxt->pDataBase, kBufferSizeInBytes);
815 }
816
Hugo Hudson9730f152011-07-25 17:04:42 +0100817 // TODO: This call must be added back in to fix the bug relating to using
818 // the correct sample rate and channels. I will do this in the follow-up.
819 // ExtractMetadataFromDecoder(pCntxt->decoderMetadata);
820
Hugo Hudsonb83ad732011-07-14 23:31:17 +0100821 // Increase data pointer by buffer size
822 pCntxt->pData += kBufferSizeInBytes;
823 if (pCntxt->pData >= pCntxt->pDataBase +
824 (kNumberOfBuffersInQueue * kBufferSizeInBytes)) {
825 pCntxt->pData = pCntxt->pDataBase;
826 }
827
828 SLresult result = (*queueItf)->Enqueue(
829 queueItf, pCntxt->pDataBase, kBufferSizeInBytes);
830 CheckSLResult("enqueue something else", result);
831
832 // If we get too much data into the decoder,
833 // sleep until the playback catches up.
834 while (!GetWasStopRequested() && DecodeBufferTooFull()) {
835 usleep(kSleepTimeMicros);
836 }
837}
838
839void AudioEngine::DecodingEventCallback(SLPlayItf, SLuint32 event) {
840 if (SL_PLAYEVENT_HEADATEND & event) {
841 SetEndOfDecoderReached();
842 }
843}