1 /*
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 #include <sys/system_properties.h>
30 
31 // ****************************************************************************
32 // Constants, utility methods, structures and other miscellany used throughout
33 // this file.
34 
35 namespace {
36 
37 // These variables are used to determine the size of the buffer queue used by
38 // the decoder.
39 // This is not the same as the large buffer used to hold the uncompressed data
40 // - for that see the member variable decodeBuffer_.
41 // The choice of 1152 corresponds to the number of samples per mp3 frame, so is
42 // a good choice of size for a decoding buffer in the absence of other
43 // information (we don't know exactly what formats we will be working with).
44 const size_t kNumberOfBuffersInQueue = 4;
45 const size_t kNumberOfSamplesPerBuffer = 1152;
46 const size_t kBufferSizeInBytes = 2 * kNumberOfSamplesPerBuffer;
47 const size_t kSampleSizeInBytes = 4;
48 
49 // When calculating play buffer size before pushing to audio player.
50 const size_t kNumberOfBytesPerInt16 = 2;
51 
52 // How long to sleep during the main play loop and the decoding callback loop.
53 // In due course this should be replaced with the better signal and wait on
54 // condition rather than busy-looping.
55 const int kSleepTimeMicros = 1000;
56 
57 // Used in detecting errors with the OpenSL ES framework.
58 const SLuint32 kPrefetchErrorCandidate =
59     SL_PREFETCHEVENT_STATUSCHANGE | SL_PREFETCHEVENT_FILLLEVELCHANGE;
60 
61 // Structure used when we perform a decoding callback.
62 typedef struct CallbackContext_ {
63   // Pointer to local storage buffers for decoded audio data.
64   int8_t* pDataBase;
65   // Pointer to the current buffer within local storage.
66   int8_t* pData;
67   // Used to read the sample rate and channels from the decoding stream during
68   // the first decoding callback.
69   SLMetadataExtractionItf decoderMetadata;
70   // The play interface used for reading duration.
71   SLPlayItf playItf;
72 } CallbackContext;
73 
74 // Local storage for decoded audio data.
75 int8_t pcmData[kNumberOfBuffersInQueue * kBufferSizeInBytes];
76 
77 #define CheckSLResult(message, result) \
78     CheckSLResult_Real(message, result, __LINE__)
79 
80 // Helper function for debugging - checks the OpenSL result for success.
CheckSLResult_Real(const char * message,SLresult result,int line)81 void CheckSLResult_Real(const char* message, SLresult result, int line) {
82   // This can be helpful when debugging.
83   // LOGD("sl result %d for %s", result, message);
84   if (SL_RESULT_SUCCESS != result) {
85     LOGE("slresult was %d at %s file variablespeed line %d",
86         static_cast<int>(result), message, line);
87   }
88   CHECK(SL_RESULT_SUCCESS == result);
89 }
90 
91 // Whether logging should be enabled. Only used if LOG_OPENSL_API_CALL is
92 // defined to use it.
93 bool gLogEnabled = false;
94 // The property to set in order to enable logging.
95 const char *const kLogTagVariableSpeed = "log.tag.VariableSpeed";
96 
ShouldLog()97 bool ShouldLog() {
98   char buffer[PROP_VALUE_MAX];
99   __system_property_get(kLogTagVariableSpeed, buffer);
100   return strlen(buffer) > 0;
101 }
102 
103 }  // namespace
104 
105 // ****************************************************************************
106 // Static instance of audio engine, and methods for getting, setting and
107 // deleting it.
108 
109 // The single global audio engine instance.
110 AudioEngine* AudioEngine::audioEngine_ = NULL;
111 android::Mutex publishEngineLock_;
112 
GetEngine()113 AudioEngine* AudioEngine::GetEngine() {
114   android::Mutex::Autolock autoLock(publishEngineLock_);
115   if (audioEngine_ == NULL) {
116     LOGE("you haven't initialized the audio engine");
117     CHECK(false);
118     return NULL;
119   }
120   return audioEngine_;
121 }
122 
SetEngine(AudioEngine * engine)123 void AudioEngine::SetEngine(AudioEngine* engine) {
124   if (audioEngine_ != NULL) {
125     LOGE("you have already set the audio engine");
126     CHECK(false);
127     return;
128   }
129   audioEngine_ = engine;
130 }
131 
CompareAndSetEngine(AudioEngine * expect,AudioEngine * update)132 bool AudioEngine::CompareAndSetEngine(AudioEngine* expect, AudioEngine* update) {
133   android::Mutex::Autolock autoLock(publishEngineLock_);
134   if (audioEngine_ == expect) {
135     DeleteEngine();
136     audioEngine_ = update;
137     return true;
138   }
139   return false;
140 }
141 
DeleteEngine()142 void AudioEngine::DeleteEngine() {
143   if (audioEngine_ != NULL) {
144     delete audioEngine_;
145     audioEngine_ = NULL;
146   }
147 }
148 
149 // ****************************************************************************
150 // The callbacks from the engine require static callback functions.
151 // Here are the static functions - they just delegate to instance methods on
152 // the engine.
153 
PlayingBufferQueueCb(SLAndroidSimpleBufferQueueItf,void *)154 static void PlayingBufferQueueCb(SLAndroidSimpleBufferQueueItf, void*) {
155   AudioEngine::GetEngine()->PlayingBufferQueueCallback();
156 }
157 
PrefetchEventCb(SLPrefetchStatusItf caller,void *,SLuint32 event)158 static void PrefetchEventCb(SLPrefetchStatusItf caller, void*, SLuint32 event) {
159   AudioEngine::GetEngine()->PrefetchEventCallback(caller, event);
160 }
161 
DecodingBufferQueueCb(SLAndroidSimpleBufferQueueItf queueItf,void * context)162 static void DecodingBufferQueueCb(SLAndroidSimpleBufferQueueItf queueItf,
163     void *context) {
164   AudioEngine::GetEngine()->DecodingBufferQueueCallback(queueItf, context);
165 }
166 
DecodingEventCb(SLPlayItf caller,void *,SLuint32 event)167 static void DecodingEventCb(SLPlayItf caller, void*, SLuint32 event) {
168   AudioEngine::GetEngine()->DecodingEventCallback(caller, event);
169 }
170 
171 // ****************************************************************************
172 // Macros for making working with OpenSL easier.
173 
174 // Log based on the value of a property.
175 #define LOG_OPENSL_API_CALL(string) (gLogEnabled && LOGV(string))
176 
177 // The regular macro: log an api call, make the api call, check the result.
178 #define OpenSL(obj, method, ...) \
179 { \
180   LOG_OPENSL_API_CALL("OpenSL " #method "(" #obj ", " #__VA_ARGS__ ")"); \
181   SLresult result = (*obj)->method(obj, __VA_ARGS__); \
182   CheckSLResult("OpenSL " #method "(" #obj ", " #__VA_ARGS__ ")", result); \
183 }
184 
185 // Special case call for api call that has void return value, can't be checked.
186 #define VoidOpenSL(obj, method) \
187 { \
188   LOG_OPENSL_API_CALL("OpenSL (void) " #method "(" #obj ")"); \
189   (*obj)->method(obj); \
190 }
191 
192 // Special case for api call with checked result but takes no arguments.
193 #define OpenSL0(obj, method) \
194 { \
195   LOG_OPENSL_API_CALL("OpenSL " #method "(" #obj ")"); \
196   SLresult result = (*obj)->method(obj); \
197   CheckSLResult("OpenSL " #method "(" #obj ")", result); \
198 }
199 
200 // Special case for api call whose result we want to store, not check.
201 // We have to encapsulate the two calls in braces, so that this expression
202 // evaluates to the last expression not the first.
203 #define ReturnOpenSL(obj, method, ...) \
204 ( \
205     LOG_OPENSL_API_CALL("OpenSL (int) " \
206         #method "(" #obj ", " #__VA_ARGS__ ")"), \
207     (*obj)->method(obj, __VA_ARGS__) \
208 ) \
209 
210 // ****************************************************************************
211 // Static utility methods.
212 
213 // Set the audio stream type for the player.
214 //
215 // Must be called before it is realized.
216 //
217 // The caller must have requested the SL_IID_ANDROIDCONFIGURATION interface when
218 // creating the player.
setAudioStreamType(SLObjectItf audioPlayer,SLint32 audioStreamType)219 static void setAudioStreamType(SLObjectItf audioPlayer, SLint32 audioStreamType) {
220   SLAndroidConfigurationItf playerConfig;
221   OpenSL(audioPlayer, GetInterface, SL_IID_ANDROIDCONFIGURATION, &playerConfig);
222   // The STREAM_XXX constants defined by android.media.AudioManager match the
223   // corresponding SL_ANDROID_STREAM_XXX constants defined by
224   // include/SLES/OpenSLES_AndroidConfiguration.h, so we can just pass the
225   // value across.
226   OpenSL(playerConfig, SetConfiguration, SL_ANDROID_KEY_STREAM_TYPE,
227          &audioStreamType, sizeof(audioStreamType));
228 }
229 
230 // Must be called with callbackLock_ held.
ReadSampleRateAndChannelCount(CallbackContext * pContext,SLuint32 * sampleRateOut,SLuint32 * channelsOut)231 static void ReadSampleRateAndChannelCount(CallbackContext *pContext,
232     SLuint32 *sampleRateOut, SLuint32 *channelsOut) {
233   SLMetadataExtractionItf decoderMetadata = pContext->decoderMetadata;
234   SLuint32 itemCount;
235   OpenSL(decoderMetadata, GetItemCount, &itemCount);
236   SLuint32 i, keySize, valueSize;
237   SLMetadataInfo *keyInfo, *value;
238   for (i = 0; i < itemCount; ++i) {
239     keyInfo = value = NULL;
240     keySize = valueSize = 0;
241     OpenSL(decoderMetadata, GetKeySize, i, &keySize);
242     keyInfo = static_cast<SLMetadataInfo*>(malloc(keySize));
243     if (keyInfo) {
244       OpenSL(decoderMetadata, GetKey, i, keySize, keyInfo);
245       if (keyInfo->encoding == SL_CHARACTERENCODING_ASCII
246           || keyInfo->encoding == SL_CHARACTERENCODING_UTF8) {
247         OpenSL(decoderMetadata, GetValueSize, i, &valueSize);
248         value = static_cast<SLMetadataInfo*>(malloc(valueSize));
249         if (value) {
250           OpenSL(decoderMetadata, GetValue, i, valueSize, value);
251           if (strcmp((char*) keyInfo->data, ANDROID_KEY_PCMFORMAT_SAMPLERATE) == 0) {
252             SLuint32 sampleRate = *(reinterpret_cast<SLuint32*>(value->data));
253             LOGD("sample Rate: %d", sampleRate);
254             *sampleRateOut = sampleRate;
255           } else if (strcmp((char*) keyInfo->data, ANDROID_KEY_PCMFORMAT_NUMCHANNELS) == 0) {
256             SLuint32 channels = *(reinterpret_cast<SLuint32*>(value->data));
257             LOGD("channels: %d", channels);
258             *channelsOut = channels;
259           }
260           free(value);
261         }
262       }
263       free(keyInfo);
264     }
265   }
266 }
267 
268 // Must be called with callbackLock_ held.
RegisterCallbackContextAndAddEnqueueBuffersToDecoder(SLAndroidSimpleBufferQueueItf decoderQueue,CallbackContext * context)269 static void RegisterCallbackContextAndAddEnqueueBuffersToDecoder(
270     SLAndroidSimpleBufferQueueItf decoderQueue, CallbackContext* context) {
271   // Register a callback on the decoder queue, so that we will be called
272   // throughout the decoding process (and can then extract the decoded audio
273   // for the next bit of the pipeline).
274   OpenSL(decoderQueue, RegisterCallback, DecodingBufferQueueCb, context);
275 
276   // Enqueue buffers to map the region of memory allocated to store the
277   // decoded data.
278   for (size_t i = 0; i < kNumberOfBuffersInQueue; i++) {
279     OpenSL(decoderQueue, Enqueue, context->pData, kBufferSizeInBytes);
280     context->pData += kBufferSizeInBytes;
281   }
282   context->pData = context->pDataBase;
283 }
284 
285 // ****************************************************************************
286 // Constructor and Destructor.
287 
AudioEngine(size_t targetFrames,float windowDuration,float windowOverlapDuration,size_t maxPlayBufferCount,float initialRate,size_t decodeInitialSize,size_t decodeMaxSize,size_t startPositionMillis,int audioStreamType)288 AudioEngine::AudioEngine(size_t targetFrames, float windowDuration,
289     float windowOverlapDuration, size_t maxPlayBufferCount, float initialRate,
290     size_t decodeInitialSize, size_t decodeMaxSize, size_t startPositionMillis,
291     int audioStreamType)
292     : decodeBuffer_(decodeInitialSize, decodeMaxSize),
293       playingBuffers_(), freeBuffers_(), timeScaler_(NULL),
294       floatBuffer_(NULL), injectBuffer_(NULL),
295       mSampleRate(0), mChannels(0),
296       targetFrames_(targetFrames),
297       windowDuration_(windowDuration),
298       windowOverlapDuration_(windowOverlapDuration),
299       maxPlayBufferCount_(maxPlayBufferCount), initialRate_(initialRate),
300       startPositionMillis_(startPositionMillis),
301       audioStreamType_(audioStreamType),
302       totalDurationMs_(0), decoderCurrentPosition_(0), startRequested_(false),
303       stopRequested_(false), finishedDecoding_(false) {
304   // Determine whether we should log calls.
305   gLogEnabled = ShouldLog();
306 }
307 
~AudioEngine()308 AudioEngine::~AudioEngine() {
309   // destroy the time scaler
310   if (timeScaler_ != NULL) {
311     delete timeScaler_;
312     timeScaler_ = NULL;
313   }
314 
315   // delete all outstanding playing and free buffers
316   android::Mutex::Autolock autoLock(playBufferLock_);
317   while (playingBuffers_.size() > 0) {
318     delete[] playingBuffers_.front();
319     playingBuffers_.pop();
320   }
321   while (freeBuffers_.size() > 0) {
322     delete[] freeBuffers_.top();
323     freeBuffers_.pop();
324   }
325 
326   delete[] floatBuffer_;
327   floatBuffer_ = NULL;
328   delete[] injectBuffer_;
329   injectBuffer_ = NULL;
330 }
331 
332 // ****************************************************************************
333 // Regular AudioEngine class methods.
334 
SetVariableSpeed(float speed)335 void AudioEngine::SetVariableSpeed(float speed) {
336   // TODO: Mutex for shared time scaler accesses.
337   if (HasSampleRateAndChannels()) {
338     GetTimeScaler()->set_speed(speed);
339   } else {
340     // This is being called at a point where we have not yet processed enough
341     // data to determine the sample rate and number of channels.
342     // Ignore the call.  See http://b/5140693.
343     LOGD("set varaible speed called, sample rate and channels not ready yet");
344   }
345 }
346 
RequestStart()347 void AudioEngine::RequestStart() {
348   android::Mutex::Autolock autoLock(lock_);
349   startRequested_ = true;
350 }
351 
ClearRequestStart()352 void AudioEngine::ClearRequestStart() {
353   android::Mutex::Autolock autoLock(lock_);
354   startRequested_ = false;
355 }
356 
GetWasStartRequested()357 bool AudioEngine::GetWasStartRequested() {
358   android::Mutex::Autolock autoLock(lock_);
359   return startRequested_;
360 }
361 
RequestStop()362 void AudioEngine::RequestStop() {
363   android::Mutex::Autolock autoLock(lock_);
364   stopRequested_ = true;
365 }
366 
GetCurrentPosition()367 int AudioEngine::GetCurrentPosition() {
368   android::Mutex::Autolock autoLock(decodeBufferLock_);
369   double result = decodeBuffer_.GetTotalAdvancedCount();
370   // TODO: This is horrible, but should be removed soon once the outstanding
371   // issue with get current position on decoder is fixed.
372   android::Mutex::Autolock autoLock2(callbackLock_);
373   return static_cast<int>(
374       (result * 1000) / mSampleRate / mChannels + startPositionMillis_);
375 }
376 
GetTotalDuration()377 int AudioEngine::GetTotalDuration() {
378   android::Mutex::Autolock autoLock(lock_);
379   return static_cast<int>(totalDurationMs_);
380 }
381 
GetTimeScaler()382 video_editing::SolaTimeScaler* AudioEngine::GetTimeScaler() {
383   if (timeScaler_ == NULL) {
384     CHECK(HasSampleRateAndChannels());
385     android::Mutex::Autolock autoLock(callbackLock_);
386     timeScaler_ = new video_editing::SolaTimeScaler();
387     timeScaler_->Init(mSampleRate, mChannels, initialRate_, windowDuration_,
388         windowOverlapDuration_);
389   }
390   return timeScaler_;
391 }
392 
EnqueueNextBufferOfAudio(SLAndroidSimpleBufferQueueItf audioPlayerQueue)393 bool AudioEngine::EnqueueNextBufferOfAudio(
394     SLAndroidSimpleBufferQueueItf audioPlayerQueue) {
395   size_t channels;
396   {
397     android::Mutex::Autolock autoLock(callbackLock_);
398     channels = mChannels;
399   }
400   size_t frameSizeInBytes = kSampleSizeInBytes * channels;
401   size_t frameCount = 0;
402   while (frameCount < targetFrames_) {
403     size_t framesLeft = targetFrames_ - frameCount;
404     // If there is data already in the time scaler, retrieve it.
405     if (GetTimeScaler()->available() > 0) {
406       size_t retrieveCount = min(GetTimeScaler()->available(), framesLeft);
407       int count = GetTimeScaler()->RetrieveSamples(
408           floatBuffer_ + frameCount * channels, retrieveCount);
409       if (count <= 0) {
410         LOGD("error: count was %d", count);
411         break;
412       }
413       frameCount += count;
414       continue;
415     }
416     // If there is no data in the time scaler, then feed some into it.
417     android::Mutex::Autolock autoLock(decodeBufferLock_);
418     size_t framesInDecodeBuffer =
419         decodeBuffer_.GetSizeInBytes() / frameSizeInBytes;
420     size_t framesScalerCanHandle = GetTimeScaler()->input_limit();
421     size_t framesToInject = min(framesInDecodeBuffer,
422         min(targetFrames_, framesScalerCanHandle));
423     if (framesToInject <= 0) {
424       // No more frames left to inject.
425       break;
426     }
427     for (size_t i = 0; i < framesToInject * channels; ++i) {
428       injectBuffer_[i] = decodeBuffer_.GetAtIndex(i);
429     }
430     int count = GetTimeScaler()->InjectSamples(injectBuffer_, framesToInject);
431     if (count <= 0) {
432       LOGD("error: count was %d", count);
433       break;
434     }
435     decodeBuffer_.AdvanceHeadPointerShorts(count * channels);
436   }
437   if (frameCount <= 0) {
438     // We must have finished playback.
439     if (GetEndOfDecoderReached()) {
440       // If we've finished decoding, clear the buffer - so we will terminate.
441       ClearDecodeBuffer();
442     }
443     return false;
444   }
445 
446   // Get a free playing buffer.
447   int16* playBuffer;
448   {
449     android::Mutex::Autolock autoLock(playBufferLock_);
450     if (freeBuffers_.size() > 0) {
451       // If we have a free buffer, recycle it.
452       playBuffer = freeBuffers_.top();
453       freeBuffers_.pop();
454     } else {
455       // Otherwise allocate a new one.
456       playBuffer = new int16[targetFrames_ * channels];
457     }
458   }
459 
460   // Try to play the buffer.
461   for (size_t i = 0; i < frameCount * channels; ++i) {
462     playBuffer[i] = floatBuffer_[i];
463   }
464   size_t sizeOfPlayBufferInBytes =
465       frameCount * channels * kNumberOfBytesPerInt16;
466   SLresult result = ReturnOpenSL(audioPlayerQueue, Enqueue, playBuffer,
467       sizeOfPlayBufferInBytes);
468   if (result == SL_RESULT_SUCCESS) {
469     android::Mutex::Autolock autoLock(playBufferLock_);
470     playingBuffers_.push(playBuffer);
471   } else {
472     LOGE("could not enqueue audio buffer");
473     delete[] playBuffer;
474   }
475 
476   return (result == SL_RESULT_SUCCESS);
477 }
478 
GetEndOfDecoderReached()479 bool AudioEngine::GetEndOfDecoderReached() {
480   android::Mutex::Autolock autoLock(lock_);
481   return finishedDecoding_;
482 }
483 
SetEndOfDecoderReached()484 void AudioEngine::SetEndOfDecoderReached() {
485   android::Mutex::Autolock autoLock(lock_);
486   finishedDecoding_ = true;
487 }
488 
PlayFileDescriptor(int fd,int64 offset,int64 length)489 bool AudioEngine::PlayFileDescriptor(int fd, int64 offset, int64 length) {
490   SLDataLocator_AndroidFD loc_fd = {
491       SL_DATALOCATOR_ANDROIDFD, fd, offset, length };
492   SLDataFormat_MIME format_mime = {
493       SL_DATAFORMAT_MIME, NULL, SL_CONTAINERTYPE_UNSPECIFIED };
494   SLDataSource audioSrc = { &loc_fd, &format_mime };
495   return PlayFromThisSource(audioSrc);
496 }
497 
PlayUri(const char * uri)498 bool AudioEngine::PlayUri(const char* uri) {
499   // Source of audio data for the decoding
500   SLDataLocator_URI decUri = { SL_DATALOCATOR_URI,
501       const_cast<SLchar*>(reinterpret_cast<const SLchar*>(uri)) };
502   SLDataFormat_MIME decMime = {
503       SL_DATAFORMAT_MIME, NULL, SL_CONTAINERTYPE_UNSPECIFIED };
504   SLDataSource decSource = { &decUri, &decMime };
505   return PlayFromThisSource(decSource);
506 }
507 
IsDecodeBufferEmpty()508 bool AudioEngine::IsDecodeBufferEmpty() {
509   android::Mutex::Autolock autoLock(decodeBufferLock_);
510   return decodeBuffer_.GetSizeInBytes() <= 0;
511 }
512 
ClearDecodeBuffer()513 void AudioEngine::ClearDecodeBuffer() {
514   android::Mutex::Autolock autoLock(decodeBufferLock_);
515   decodeBuffer_.Clear();
516 }
517 
ReadDuration(SLPlayItf playItf)518 static size_t ReadDuration(SLPlayItf playItf) {
519   SLmillisecond durationInMsec = SL_TIME_UNKNOWN;
520   OpenSL(playItf, GetDuration, &durationInMsec);
521   if (durationInMsec == SL_TIME_UNKNOWN) {
522     LOGE("can't get duration");
523     return 0;
524   }
525   LOGD("duration: %d", static_cast<int>(durationInMsec));
526   return durationInMsec;
527 }
528 
ReadPosition(SLPlayItf playItf)529 static size_t ReadPosition(SLPlayItf playItf) {
530   SLmillisecond positionInMsec = SL_TIME_UNKNOWN;
531   OpenSL(playItf, GetPosition, &positionInMsec);
532   if (positionInMsec == SL_TIME_UNKNOWN) {
533     LOGE("can't get position");
534     return 0;
535   }
536   LOGW("decoder position: %d", static_cast<int>(positionInMsec));
537   return positionInMsec;
538 }
539 
CreateAndRealizeEngine(SLObjectItf & engine,SLEngineItf & engineInterface)540 static void CreateAndRealizeEngine(SLObjectItf &engine,
541     SLEngineItf &engineInterface) {
542   SLEngineOption EngineOption[] = { {
543       SL_ENGINEOPTION_THREADSAFE, SL_BOOLEAN_TRUE } };
544   SLresult result = slCreateEngine(&engine, 1, EngineOption, 0, NULL, NULL);
545   CheckSLResult("create engine", result);
546   OpenSL(engine, Realize, SL_BOOLEAN_FALSE);
547   OpenSL(engine, GetInterface, SL_IID_ENGINE, &engineInterface);
548 }
549 
GetSLSampleRate()550 SLuint32 AudioEngine::GetSLSampleRate() {
551   android::Mutex::Autolock autoLock(callbackLock_);
552   return mSampleRate * 1000;
553 }
554 
GetSLChannels()555 SLuint32 AudioEngine::GetSLChannels() {
556   android::Mutex::Autolock autoLock(callbackLock_);
557   switch (mChannels) {
558     case 2:
559       return SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
560     case 1:
561       return SL_SPEAKER_FRONT_CENTER;
562     default:
563       LOGE("unknown channels %d, using 2", mChannels);
564       return SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
565   }
566 }
567 
GetChannelCount()568 SLuint32 AudioEngine::GetChannelCount() {
569   android::Mutex::Autolock autoLock(callbackLock_);
570   return mChannels;
571 }
572 
CreateAndRealizeAudioPlayer(SLuint32 slSampleRate,SLuint32 channelCount,SLuint32 slChannels,SLint32 audioStreamType,SLObjectItf & outputMix,SLObjectItf & audioPlayer,SLEngineItf & engineInterface)573 static void CreateAndRealizeAudioPlayer(SLuint32 slSampleRate,
574     SLuint32 channelCount, SLuint32 slChannels, SLint32 audioStreamType, SLObjectItf &outputMix,
575     SLObjectItf &audioPlayer, SLEngineItf &engineInterface) {
576   // Define the source and sink for the audio player: comes from a buffer queue
577   // and goes to the output mix.
578   SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {
579       SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2 };
580   SLDataFormat_PCM format_pcm = {SL_DATAFORMAT_PCM, channelCount, slSampleRate,
581       SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16,
582       slChannels, SL_BYTEORDER_LITTLEENDIAN};
583   SLDataSource playingSrc = {&loc_bufq, &format_pcm};
584   SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, outputMix};
585   SLDataSink audioSnk = {&loc_outmix, NULL};
586 
587   // Create the audio player, which will play from the buffer queue and send to
588   // the output mix.
589   const size_t playerInterfaceCount = 2;
590   const SLInterfaceID iids[playerInterfaceCount] = {
591       SL_IID_ANDROIDSIMPLEBUFFERQUEUE, SL_IID_ANDROIDCONFIGURATION };
592   const SLboolean reqs[playerInterfaceCount] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
593   OpenSL(engineInterface, CreateAudioPlayer, &audioPlayer, &playingSrc,
594       &audioSnk, playerInterfaceCount, iids, reqs);
595   setAudioStreamType(audioPlayer, audioStreamType);
596   OpenSL(audioPlayer, Realize, SL_BOOLEAN_FALSE);
597 }
598 
HasSampleRateAndChannels()599 bool AudioEngine::HasSampleRateAndChannels() {
600   android::Mutex::Autolock autoLock(callbackLock_);
601   return mChannels != 0 && mSampleRate != 0;
602 }
603 
PlayFromThisSource(const SLDataSource & audioSrc)604 bool AudioEngine::PlayFromThisSource(const SLDataSource& audioSrc) {
605   ClearDecodeBuffer();
606 
607   SLObjectItf engine;
608   SLEngineItf engineInterface;
609   CreateAndRealizeEngine(engine, engineInterface);
610 
611   // Define the source and sink for the decoding player: comes from the source
612   // this method was called with, is sent to another buffer queue.
613   SLDataLocator_AndroidSimpleBufferQueue decBuffQueue;
614   decBuffQueue.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE;
615   decBuffQueue.numBuffers = kNumberOfBuffersInQueue;
616   // A valid value seems required here but is currently ignored.
617   SLDataFormat_PCM pcm = {SL_DATAFORMAT_PCM, 1, SL_SAMPLINGRATE_44_1,
618       SL_PCMSAMPLEFORMAT_FIXED_16, 16,
619       SL_SPEAKER_FRONT_LEFT, SL_BYTEORDER_LITTLEENDIAN};
620   SLDataSink decDest = { &decBuffQueue, &pcm };
621 
622   // Create the decoder with the given source and sink.
623   const size_t decoderInterfaceCount = 5;
624   SLObjectItf decoder;
625   const SLInterfaceID decodePlayerInterfaces[decoderInterfaceCount] = {
626       SL_IID_ANDROIDSIMPLEBUFFERQUEUE, SL_IID_PREFETCHSTATUS, SL_IID_SEEK,
627       SL_IID_METADATAEXTRACTION, SL_IID_ANDROIDCONFIGURATION };
628   const SLboolean decodePlayerRequired[decoderInterfaceCount] = {
629       SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
630   SLDataSource sourceCopy(audioSrc);
631   OpenSL(engineInterface, CreateAudioPlayer, &decoder, &sourceCopy, &decDest,
632       decoderInterfaceCount, decodePlayerInterfaces, decodePlayerRequired);
633   // Not sure if this is necessary, but just in case.
634   setAudioStreamType(decoder, audioStreamType_);
635   OpenSL(decoder, Realize, SL_BOOLEAN_FALSE);
636 
637   // Get the play interface from the decoder, and register event callbacks.
638   // Get the buffer queue, prefetch and seek interfaces.
639   SLPlayItf decoderPlay = NULL;
640   SLAndroidSimpleBufferQueueItf decoderQueue = NULL;
641   SLPrefetchStatusItf decoderPrefetch = NULL;
642   SLSeekItf decoderSeek = NULL;
643   SLMetadataExtractionItf decoderMetadata = NULL;
644   OpenSL(decoder, GetInterface, SL_IID_PLAY, &decoderPlay);
645   OpenSL(decoderPlay, SetCallbackEventsMask, SL_PLAYEVENT_HEADATEND);
646   OpenSL(decoderPlay, RegisterCallback, DecodingEventCb, NULL);
647   OpenSL(decoder, GetInterface, SL_IID_PREFETCHSTATUS, &decoderPrefetch);
648   OpenSL(decoder, GetInterface, SL_IID_SEEK, &decoderSeek);
649   OpenSL(decoder, GetInterface, SL_IID_METADATAEXTRACTION, &decoderMetadata);
650   OpenSL(decoder, GetInterface, SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
651       &decoderQueue);
652 
653   // Initialize the callback structure, used during the decoding.
654   CallbackContext callbackContext;
655   {
656     android::Mutex::Autolock autoLock(callbackLock_);
657     callbackContext.pDataBase = pcmData;
658     callbackContext.pData = pcmData;
659     callbackContext.decoderMetadata = decoderMetadata;
660     callbackContext.playItf = decoderPlay;
661     RegisterCallbackContextAndAddEnqueueBuffersToDecoder(
662         decoderQueue, &callbackContext);
663   }
664 
665   // Initialize the callback for prefetch errors, if we can't open the
666   // resource to decode.
667   OpenSL(decoderPrefetch, SetCallbackEventsMask, kPrefetchErrorCandidate);
668   OpenSL(decoderPrefetch, RegisterCallback, PrefetchEventCb, &decoderPrefetch);
669 
670   // Seek to the start position.
671   OpenSL(decoderSeek, SetPosition, startPositionMillis_, SL_SEEKMODE_ACCURATE);
672 
673   // Start decoding immediately.
674   OpenSL(decoderPlay, SetPlayState, SL_PLAYSTATE_PLAYING);
675 
676   // These variables hold the audio player and its output.
677   // They will only be constructed once the decoder has invoked the callback,
678   // and given us the correct sample rate, number of channels and duration.
679   SLObjectItf outputMix = NULL;
680   SLObjectItf audioPlayer = NULL;
681   SLPlayItf audioPlayerPlay = NULL;
682   SLAndroidSimpleBufferQueueItf audioPlayerQueue = NULL;
683 
684   // The main loop - until we're told to stop: if there is audio data coming
685   // out of the decoder, feed it through the time scaler.
686   // As it comes out of the time scaler, feed it into the audio player.
687   while (!Finished()) {
688     if (GetWasStartRequested() && HasSampleRateAndChannels()) {
689       // Build the audio player.
690       // TODO: What happens if I maliciously call start lots of times?
691       floatBuffer_ = new float[targetFrames_ * mChannels];
692       injectBuffer_ = new float[targetFrames_ * mChannels];
693       OpenSL(engineInterface, CreateOutputMix, &outputMix, 0, NULL, NULL);
694       OpenSL(outputMix, Realize, SL_BOOLEAN_FALSE);
695       CreateAndRealizeAudioPlayer(GetSLSampleRate(), GetChannelCount(),
696           GetSLChannels(), audioStreamType_, outputMix, audioPlayer,
697           engineInterface);
698       OpenSL(audioPlayer, GetInterface, SL_IID_PLAY, &audioPlayerPlay);
699       OpenSL(audioPlayer, GetInterface, SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
700           &audioPlayerQueue);
701       OpenSL(audioPlayerQueue, RegisterCallback, PlayingBufferQueueCb, NULL);
702       ClearRequestStart();
703       OpenSL(audioPlayerPlay, SetPlayState, SL_PLAYSTATE_PLAYING);
704     }
705     EnqueueMoreAudioIfNecessary(audioPlayerQueue);
706     usleep(kSleepTimeMicros);
707   }
708 
709   // Delete the audio player and output mix, iff they have been created.
710   if (audioPlayer != NULL) {
711     OpenSL(audioPlayerPlay, SetPlayState, SL_PLAYSTATE_STOPPED);
712     OpenSL0(audioPlayerQueue, Clear);
713     OpenSL(audioPlayerQueue, RegisterCallback, NULL, NULL);
714     VoidOpenSL(audioPlayer, AbortAsyncOperation);
715     VoidOpenSL(audioPlayer, Destroy);
716     VoidOpenSL(outputMix, Destroy);
717     audioPlayer = NULL;
718     audioPlayerPlay = NULL;
719     audioPlayerQueue = NULL;
720     outputMix = NULL;
721   }
722 
723   // Delete the decoder.
724   OpenSL(decoderPlay, SetPlayState, SL_PLAYSTATE_STOPPED);
725   OpenSL(decoderPrefetch, RegisterCallback, NULL, NULL);
726   // This is returning slresult 13 if I do no playback.
727   // Repro is to comment out all before this line, and all after enqueueing
728   // my buffers.
729   // OpenSL0(decoderQueue, Clear);
730   OpenSL(decoderQueue, RegisterCallback, NULL, NULL);
731   decoderSeek = NULL;
732   decoderPrefetch = NULL;
733   decoderQueue = NULL;
734   OpenSL(decoderPlay, RegisterCallback, NULL, NULL);
735   VoidOpenSL(decoder, AbortAsyncOperation);
736   VoidOpenSL(decoder, Destroy);
737   decoderPlay = NULL;
738 
739   // Delete the engine.
740   VoidOpenSL(engine, Destroy);
741   engineInterface = NULL;
742 
743   return true;
744 }
745 
Finished()746 bool AudioEngine::Finished() {
747   if (GetWasStopRequested()) {
748     return true;
749   }
750   android::Mutex::Autolock autoLock(playBufferLock_);
751   return playingBuffers_.size() <= 0 &&
752       IsDecodeBufferEmpty() &&
753       GetEndOfDecoderReached();
754 }
755 
GetWasStopRequested()756 bool AudioEngine::GetWasStopRequested() {
757   android::Mutex::Autolock autoLock(lock_);
758   return stopRequested_;
759 }
760 
GetHasReachedPlayingBuffersLimit()761 bool AudioEngine::GetHasReachedPlayingBuffersLimit() {
762   android::Mutex::Autolock autoLock(playBufferLock_);
763   return playingBuffers_.size() >= maxPlayBufferCount_;
764 }
765 
EnqueueMoreAudioIfNecessary(SLAndroidSimpleBufferQueueItf audioPlayerQueue)766 void AudioEngine::EnqueueMoreAudioIfNecessary(
767     SLAndroidSimpleBufferQueueItf audioPlayerQueue) {
768   bool keepEnqueueing = true;
769   while (audioPlayerQueue != NULL &&
770          !GetWasStopRequested() &&
771          !IsDecodeBufferEmpty() &&
772          !GetHasReachedPlayingBuffersLimit() &&
773          keepEnqueueing) {
774     keepEnqueueing = EnqueueNextBufferOfAudio(audioPlayerQueue);
775   }
776 }
777 
DecodeBufferTooFull()778 bool AudioEngine::DecodeBufferTooFull() {
779   android::Mutex::Autolock autoLock(decodeBufferLock_);
780   return decodeBuffer_.IsTooLarge();
781 }
782 
783 // ****************************************************************************
784 // Code for handling the static callbacks.
785 
PlayingBufferQueueCallback()786 void AudioEngine::PlayingBufferQueueCallback() {
787   // The head playing buffer is done, move it to the free list.
788   android::Mutex::Autolock autoLock(playBufferLock_);
789   if (playingBuffers_.size() > 0) {
790     freeBuffers_.push(playingBuffers_.front());
791     playingBuffers_.pop();
792   }
793 }
794 
PrefetchEventCallback(SLPrefetchStatusItf caller,SLuint32 event)795 void AudioEngine::PrefetchEventCallback(
796     SLPrefetchStatusItf caller, SLuint32 event) {
797   // If there was a problem during decoding, then signal the end.
798   SLpermille level = 0;
799   SLuint32 status;
800   OpenSL(caller, GetFillLevel, &level);
801   OpenSL(caller, GetPrefetchStatus, &status);
802   if ((kPrefetchErrorCandidate == (event & kPrefetchErrorCandidate)) &&
803       (level == 0) &&
804       (status == SL_PREFETCHSTATUS_UNDERFLOW)) {
805     LOGI("prefetcheventcallback error while prefetching data");
806     SetEndOfDecoderReached();
807   }
808   if (SL_PREFETCHSTATUS_SUFFICIENTDATA == event) {
809     // android::Mutex::Autolock autoLock(prefetchLock_);
810     // prefetchCondition_.broadcast();
811   }
812 }
813 
DecodingBufferQueueCallback(SLAndroidSimpleBufferQueueItf queueItf,void * context)814 void AudioEngine::DecodingBufferQueueCallback(
815     SLAndroidSimpleBufferQueueItf queueItf, void *context) {
816   if (GetWasStopRequested()) {
817     return;
818   }
819 
820   CallbackContext *pCntxt;
821   {
822     android::Mutex::Autolock autoLock(callbackLock_);
823     pCntxt = reinterpret_cast<CallbackContext*>(context);
824   }
825   {
826     android::Mutex::Autolock autoLock(decodeBufferLock_);
827     decodeBuffer_.AddData(pCntxt->pData, kBufferSizeInBytes);
828   }
829 
830   if (!HasSampleRateAndChannels()) {
831     android::Mutex::Autolock autoLock(callbackLock_);
832     ReadSampleRateAndChannelCount(pCntxt, &mSampleRate, &mChannels);
833   }
834 
835   {
836     android::Mutex::Autolock autoLock(lock_);
837     if (totalDurationMs_ == 0) {
838       totalDurationMs_ = ReadDuration(pCntxt->playItf);
839     }
840     // TODO: This isn't working, it always reports zero.
841     // ReadPosition(pCntxt->playItf);
842   }
843 
844   OpenSL(queueItf, Enqueue, pCntxt->pData, kBufferSizeInBytes);
845 
846   // Increase data pointer by buffer size
847   pCntxt->pData += kBufferSizeInBytes;
848   if (pCntxt->pData >= pCntxt->pDataBase +
849       (kNumberOfBuffersInQueue * kBufferSizeInBytes)) {
850     pCntxt->pData = pCntxt->pDataBase;
851   }
852 
853   // If we get too much data into the decoder,
854   // sleep until the playback catches up.
855   while (!GetWasStopRequested() && DecodeBufferTooFull()) {
856     usleep(kSleepTimeMicros);
857   }
858 }
859 
DecodingEventCallback(SLPlayItf,SLuint32 event)860 void AudioEngine::DecodingEventCallback(SLPlayItf, SLuint32 event) {
861   if (SL_PLAYEVENT_HEADATEND & event) {
862     SetEndOfDecoderReached();
863   }
864 }
865