1 /*
2 * Copyright (C) 2010 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 "sles_allinclusive.h"
18 #include "android_prompts.h"
19 #include "android/android_AudioToCbRenderer.h"
20 #include "android/android_StreamPlayer.h"
21 #include "android/android_LocAVPlayer.h"
22 #include "android/include/AacBqToPcmCbRenderer.h"
23
24 #include <fcntl.h>
25 #include <sys/stat.h>
26
27 #include <system/audio.h>
28
29 template class android::KeyedVector<SLuint32, android::AudioEffect* > ;
30
31 #define KEY_STREAM_TYPE_PARAMSIZE sizeof(SLint32)
32
33 #define AUDIOTRACK_MIN_PLAYBACKRATE_PERMILLE 500
34 #define AUDIOTRACK_MAX_PLAYBACKRATE_PERMILLE 2000
35
36 #define MEDIAPLAYER_MIN_PLAYBACKRATE_PERMILLE AUDIOTRACK_MIN_PLAYBACKRATE_PERMILLE
37 #define MEDIAPLAYER_MAX_PLAYBACKRATE_PERMILLE AUDIOTRACK_MAX_PLAYBACKRATE_PERMILLE
38
39 //-----------------------------------------------------------------------------
40 // FIXME this method will be absorbed into android_audioPlayer_setPlayState() once
41 // bufferqueue and uri/fd playback are moved under the GenericPlayer C++ object
aplayer_setPlayState(const android::sp<android::GenericPlayer> & ap,SLuint32 playState,AndroidObjectState * pObjState)42 SLresult aplayer_setPlayState(const android::sp<android::GenericPlayer> &ap, SLuint32 playState,
43 AndroidObjectState* pObjState) {
44 SLresult result = SL_RESULT_SUCCESS;
45 AndroidObjectState objState = *pObjState;
46
47 switch (playState) {
48 case SL_PLAYSTATE_STOPPED:
49 SL_LOGV("setting GenericPlayer to SL_PLAYSTATE_STOPPED");
50 ap->stop();
51 break;
52 case SL_PLAYSTATE_PAUSED:
53 SL_LOGV("setting GenericPlayer to SL_PLAYSTATE_PAUSED");
54 switch(objState) {
55 case ANDROID_UNINITIALIZED:
56 *pObjState = ANDROID_PREPARING;
57 ap->prepare();
58 break;
59 case ANDROID_PREPARING:
60 break;
61 case ANDROID_READY:
62 ap->pause();
63 break;
64 default:
65 SL_LOGE(ERROR_PLAYERSETPLAYSTATE_INVALID_OBJECT_STATE_D, playState);
66 result = SL_RESULT_INTERNAL_ERROR;
67 break;
68 }
69 break;
70 case SL_PLAYSTATE_PLAYING: {
71 SL_LOGV("setting GenericPlayer to SL_PLAYSTATE_PLAYING");
72 switch(objState) {
73 case ANDROID_UNINITIALIZED:
74 *pObjState = ANDROID_PREPARING;
75 ap->prepare();
76 // intended fall through
77 case ANDROID_PREPARING:
78 // intended fall through
79 case ANDROID_READY:
80 ap->play();
81 break;
82 default:
83 SL_LOGE(ERROR_PLAYERSETPLAYSTATE_INVALID_OBJECT_STATE_D, playState);
84 result = SL_RESULT_INTERNAL_ERROR;
85 break;
86 }
87 }
88 break;
89 default:
90 // checked by caller, should not happen
91 SL_LOGE(ERROR_SHOULDNT_BE_HERE_S, "aplayer_setPlayState");
92 result = SL_RESULT_INTERNAL_ERROR;
93 break;
94 }
95
96 return result;
97 }
98
99
100 //-----------------------------------------------------------------------------
101 // Callback associated with a AudioToCbRenderer of an SL ES AudioPlayer that gets its data
102 // from a URI or FD, to write the decoded audio data to a buffer queue
adecoder_writeToBufferQueue(const uint8_t * data,size_t size,CAudioPlayer * ap)103 static size_t adecoder_writeToBufferQueue(const uint8_t *data, size_t size, CAudioPlayer* ap) {
104 if (!android::CallbackProtector::enterCbIfOk(ap->mCallbackProtector)) {
105 // it is not safe to enter the callback (the player is about to go away)
106 return 0;
107 }
108 size_t sizeConsumed = 0;
109 SL_LOGD("received %d bytes from decoder", size);
110 slBufferQueueCallback callback = NULL;
111 void * callbackPContext = NULL;
112
113 // push decoded data to the buffer queue
114 object_lock_exclusive(&ap->mObject);
115
116 if (ap->mBufferQueue.mState.count != 0) {
117 assert(ap->mBufferQueue.mFront != ap->mBufferQueue.mRear);
118
119 BufferHeader *oldFront = ap->mBufferQueue.mFront;
120 BufferHeader *newFront = &oldFront[1];
121
122 uint8_t *pDest = (uint8_t *)oldFront->mBuffer + ap->mBufferQueue.mSizeConsumed;
123 if (ap->mBufferQueue.mSizeConsumed + size < oldFront->mSize) {
124 // room to consume the whole or rest of the decoded data in one shot
125 ap->mBufferQueue.mSizeConsumed += size;
126 // consume data but no callback to the BufferQueue interface here
127 memcpy (pDest, data, size);
128 sizeConsumed = size;
129 } else {
130 // push as much as possible of the decoded data into the buffer queue
131 sizeConsumed = oldFront->mSize - ap->mBufferQueue.mSizeConsumed;
132
133 // the buffer at the head of the buffer queue is full, update the state
134 ap->mBufferQueue.mSizeConsumed = 0;
135 if (newFront == &ap->mBufferQueue.mArray[ap->mBufferQueue.mNumBuffers + 1]) {
136 newFront = ap->mBufferQueue.mArray;
137 }
138 ap->mBufferQueue.mFront = newFront;
139
140 ap->mBufferQueue.mState.count--;
141 ap->mBufferQueue.mState.playIndex++;
142 // consume data
143 memcpy (pDest, data, sizeConsumed);
144 // data has been copied to the buffer, and the buffer queue state has been updated
145 // we will notify the client if applicable
146 callback = ap->mBufferQueue.mCallback;
147 // save callback data
148 callbackPContext = ap->mBufferQueue.mContext;
149 }
150
151 } else {
152 // no available buffers in the queue to write the decoded data
153 sizeConsumed = 0;
154 }
155
156 object_unlock_exclusive(&ap->mObject);
157 // notify client
158 if (NULL != callback) {
159 (*callback)(&ap->mBufferQueue.mItf, callbackPContext);
160 }
161
162 ap->mCallbackProtector->exitCb();
163 return sizeConsumed;
164 }
165
166 //-----------------------------------------------------------------------------
android_getMinFrameCount(uint32_t sampleRate)167 int android_getMinFrameCount(uint32_t sampleRate) {
168 int afSampleRate;
169 if (android::AudioSystem::getOutputSamplingRate(&afSampleRate,
170 ANDROID_DEFAULT_OUTPUT_STREAM_TYPE) != android::NO_ERROR) {
171 return ANDROID_DEFAULT_AUDIOTRACK_BUFFER_SIZE;
172 }
173 int afFrameCount;
174 if (android::AudioSystem::getOutputFrameCount(&afFrameCount,
175 ANDROID_DEFAULT_OUTPUT_STREAM_TYPE) != android::NO_ERROR) {
176 return ANDROID_DEFAULT_AUDIOTRACK_BUFFER_SIZE;
177 }
178 uint32_t afLatency;
179 if (android::AudioSystem::getOutputLatency(&afLatency,
180 ANDROID_DEFAULT_OUTPUT_STREAM_TYPE) != android::NO_ERROR) {
181 return ANDROID_DEFAULT_AUDIOTRACK_BUFFER_SIZE;
182 }
183 // minimum nb of buffers to cover output latency, given the size of each hardware audio buffer
184 uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
185 if (minBufCount < 2) minBufCount = 2;
186 // minimum number of frames to cover output latency at the sample rate of the content
187 return (afFrameCount*sampleRate*minBufCount)/afSampleRate;
188 }
189
190
191 //-----------------------------------------------------------------------------
192 #define LEFT_CHANNEL_MASK 0x1 << 0
193 #define RIGHT_CHANNEL_MASK 0x1 << 1
194
android_audioPlayer_volumeUpdate(CAudioPlayer * ap)195 void android_audioPlayer_volumeUpdate(CAudioPlayer* ap)
196 {
197 assert(ap != NULL);
198
199 // the source's channel count, where zero means unknown
200 SLuint8 channelCount = ap->mNumChannels;
201
202 // whether each channel is audible
203 bool leftAudibilityFactor, rightAudibilityFactor;
204
205 // mute has priority over solo
206 if (channelCount >= STEREO_CHANNELS) {
207 if (ap->mMuteMask & LEFT_CHANNEL_MASK) {
208 // left muted
209 leftAudibilityFactor = false;
210 } else {
211 // left not muted
212 if (ap->mSoloMask & LEFT_CHANNEL_MASK) {
213 // left soloed
214 leftAudibilityFactor = true;
215 } else {
216 // left not soloed
217 if (ap->mSoloMask & RIGHT_CHANNEL_MASK) {
218 // right solo silences left
219 leftAudibilityFactor = false;
220 } else {
221 // left and right are not soloed, and left is not muted
222 leftAudibilityFactor = true;
223 }
224 }
225 }
226
227 if (ap->mMuteMask & RIGHT_CHANNEL_MASK) {
228 // right muted
229 rightAudibilityFactor = false;
230 } else {
231 // right not muted
232 if (ap->mSoloMask & RIGHT_CHANNEL_MASK) {
233 // right soloed
234 rightAudibilityFactor = true;
235 } else {
236 // right not soloed
237 if (ap->mSoloMask & LEFT_CHANNEL_MASK) {
238 // left solo silences right
239 rightAudibilityFactor = false;
240 } else {
241 // left and right are not soloed, and right is not muted
242 rightAudibilityFactor = true;
243 }
244 }
245 }
246
247 // channel mute and solo are ignored for mono and unknown channel count sources
248 } else {
249 leftAudibilityFactor = true;
250 rightAudibilityFactor = true;
251 }
252
253 // compute volumes without setting
254 const bool audibilityFactors[2] = {leftAudibilityFactor, rightAudibilityFactor};
255 float volumes[2];
256 android_player_volumeUpdate(volumes, &ap->mVolume, channelCount, ap->mAmplFromDirectLevel,
257 audibilityFactors);
258 float leftVol = volumes[0], rightVol = volumes[1];
259
260 // set volume on the underlying media player or audio track
261 if (ap->mAPlayer != 0) {
262 ap->mAPlayer->setVolume(leftVol, rightVol);
263 } else if (ap->mAudioTrack != 0) {
264 ap->mAudioTrack->setVolume(leftVol, rightVol);
265 }
266
267 // changes in the AudioPlayer volume must be reflected in the send level:
268 // in SLEffectSendItf or in SLAndroidEffectSendItf?
269 // FIXME replace interface test by an internal API once we have one.
270 if (NULL != ap->mEffectSend.mItf) {
271 for (unsigned int i=0 ; i<AUX_MAX ; i++) {
272 if (ap->mEffectSend.mEnableLevels[i].mEnable) {
273 android_fxSend_setSendLevel(ap,
274 ap->mEffectSend.mEnableLevels[i].mSendLevel + ap->mVolume.mLevel);
275 // there's a single aux bus on Android, so we can stop looking once the first
276 // aux effect is found.
277 break;
278 }
279 }
280 } else if (NULL != ap->mAndroidEffectSend.mItf) {
281 android_fxSend_setSendLevel(ap, ap->mAndroidEffectSend.mSendLevel + ap->mVolume.mLevel);
282 }
283 }
284
285 // Called by android_audioPlayer_volumeUpdate and android_mediaPlayer_volumeUpdate to compute
286 // volumes, but setting volumes is handled by the caller.
287
android_player_volumeUpdate(float * pVolumes,const IVolume * volumeItf,unsigned channelCount,float amplFromDirectLevel,const bool * audibilityFactors)288 void android_player_volumeUpdate(float *pVolumes /*[2]*/, const IVolume *volumeItf, unsigned
289 channelCount, float amplFromDirectLevel, const bool *audibilityFactors /*[2]*/)
290 {
291 assert(pVolumes != NULL);
292 assert(volumeItf != NULL);
293 // OK for audibilityFactors to be NULL
294
295 bool leftAudibilityFactor, rightAudibilityFactor;
296
297 // apply player mute factor
298 // note that AudioTrack has mute() but not MediaPlayer, so it's easier to use volume
299 // to mute for both rather than calling mute() for AudioTrack
300
301 // player is muted
302 if (volumeItf->mMute) {
303 leftAudibilityFactor = false;
304 rightAudibilityFactor = false;
305 // player isn't muted, and channel mute/solo audibility factors are available (AudioPlayer)
306 } else if (audibilityFactors != NULL) {
307 leftAudibilityFactor = audibilityFactors[0];
308 rightAudibilityFactor = audibilityFactors[1];
309 // player isn't muted, and channel mute/solo audibility factors aren't available (MediaPlayer)
310 } else {
311 leftAudibilityFactor = true;
312 rightAudibilityFactor = true;
313 }
314
315 // compute amplification as the combination of volume level and stereo position
316 // amplification (or attenuation) from volume level
317 float amplFromVolLevel = sles_to_android_amplification(volumeItf->mLevel);
318 // amplification from direct level (changed in SLEffectSendtItf and SLAndroidEffectSendItf)
319 float leftVol = amplFromVolLevel * amplFromDirectLevel;
320 float rightVol = leftVol;
321
322 // amplification from stereo position
323 if (volumeItf->mEnableStereoPosition) {
324 // Left/right amplification (can be attenuations) factors derived for the StereoPosition
325 float amplFromStereoPos[STEREO_CHANNELS];
326 // panning law depends on content channel count: mono to stereo panning vs stereo balance
327 if (1 == channelCount) {
328 // mono to stereo panning
329 double theta = (1000+volumeItf->mStereoPosition)*M_PI_4/1000.0f; // 0 <= theta <= Pi/2
330 amplFromStereoPos[0] = cos(theta);
331 amplFromStereoPos[1] = sin(theta);
332 // channel count is 0 (unknown), 2 (stereo), or > 2 (multi-channel)
333 } else {
334 // stereo balance
335 if (volumeItf->mStereoPosition > 0) {
336 amplFromStereoPos[0] = (1000-volumeItf->mStereoPosition)/1000.0f;
337 amplFromStereoPos[1] = 1.0f;
338 } else {
339 amplFromStereoPos[0] = 1.0f;
340 amplFromStereoPos[1] = (1000+volumeItf->mStereoPosition)/1000.0f;
341 }
342 }
343 leftVol *= amplFromStereoPos[0];
344 rightVol *= amplFromStereoPos[1];
345 }
346
347 // apply audibility factors
348 if (!leftAudibilityFactor) {
349 leftVol = 0.0;
350 }
351 if (!rightAudibilityFactor) {
352 rightVol = 0.0;
353 }
354
355 // return the computed volumes
356 pVolumes[0] = leftVol;
357 pVolumes[1] = rightVol;
358 }
359
360 //-----------------------------------------------------------------------------
audioTrack_handleMarker_lockPlay(CAudioPlayer * ap)361 void audioTrack_handleMarker_lockPlay(CAudioPlayer* ap) {
362 //SL_LOGV("received event EVENT_MARKER from AudioTrack");
363 slPlayCallback callback = NULL;
364 void* callbackPContext = NULL;
365
366 interface_lock_shared(&ap->mPlay);
367 callback = ap->mPlay.mCallback;
368 callbackPContext = ap->mPlay.mContext;
369 interface_unlock_shared(&ap->mPlay);
370
371 if (NULL != callback) {
372 // getting this event implies SL_PLAYEVENT_HEADATMARKER was set in the event mask
373 (*callback)(&ap->mPlay.mItf, callbackPContext, SL_PLAYEVENT_HEADATMARKER);
374 }
375 }
376
377 //-----------------------------------------------------------------------------
audioTrack_handleNewPos_lockPlay(CAudioPlayer * ap)378 void audioTrack_handleNewPos_lockPlay(CAudioPlayer* ap) {
379 //SL_LOGV("received event EVENT_NEW_POS from AudioTrack");
380 slPlayCallback callback = NULL;
381 void* callbackPContext = NULL;
382
383 interface_lock_shared(&ap->mPlay);
384 callback = ap->mPlay.mCallback;
385 callbackPContext = ap->mPlay.mContext;
386 interface_unlock_shared(&ap->mPlay);
387
388 if (NULL != callback) {
389 // getting this event implies SL_PLAYEVENT_HEADATNEWPOS was set in the event mask
390 (*callback)(&ap->mPlay.mItf, callbackPContext, SL_PLAYEVENT_HEADATNEWPOS);
391 }
392 }
393
394
395 //-----------------------------------------------------------------------------
audioTrack_handleUnderrun_lockPlay(CAudioPlayer * ap)396 void audioTrack_handleUnderrun_lockPlay(CAudioPlayer* ap) {
397 slPlayCallback callback = NULL;
398 void* callbackPContext = NULL;
399
400 interface_lock_shared(&ap->mPlay);
401 callback = ap->mPlay.mCallback;
402 callbackPContext = ap->mPlay.mContext;
403 bool headStalled = (ap->mPlay.mEventFlags & SL_PLAYEVENT_HEADSTALLED) != 0;
404 interface_unlock_shared(&ap->mPlay);
405
406 if ((NULL != callback) && headStalled) {
407 (*callback)(&ap->mPlay.mItf, callbackPContext, SL_PLAYEVENT_HEADSTALLED);
408 }
409 }
410
411
412 //-----------------------------------------------------------------------------
413 /**
414 * post-condition: play state of AudioPlayer is SL_PLAYSTATE_PAUSED if setPlayStateToPaused is true
415 *
416 * note: a conditional flag, setPlayStateToPaused, is used here to specify whether the play state
417 * needs to be changed when the player reaches the end of the content to play. This is
418 * relative to what the specification describes for buffer queues vs the
419 * SL_PLAYEVENT_HEADATEND event. In the OpenSL ES specification 1.0.1:
420 * - section 8.12 SLBufferQueueItf states "In the case of starvation due to insufficient
421 * buffers in the queue, the playing of audio data stops. The player remains in the
422 * SL_PLAYSTATE_PLAYING state."
423 * - section 9.2.31 SL_PLAYEVENT states "SL_PLAYEVENT_HEADATEND Playback head is at the end
424 * of the current content and the player has paused."
425 */
audioPlayer_dispatch_headAtEnd_lockPlay(CAudioPlayer * ap,bool setPlayStateToPaused,bool needToLock)426 void audioPlayer_dispatch_headAtEnd_lockPlay(CAudioPlayer *ap, bool setPlayStateToPaused,
427 bool needToLock) {
428 //SL_LOGV("ap=%p, setPlayStateToPaused=%d, needToLock=%d", ap, setPlayStateToPaused,
429 // needToLock);
430 slPlayCallback playCallback = NULL;
431 void * playContext = NULL;
432 // SLPlayItf callback or no callback?
433 if (needToLock) {
434 interface_lock_exclusive(&ap->mPlay);
435 }
436 if (ap->mPlay.mEventFlags & SL_PLAYEVENT_HEADATEND) {
437 playCallback = ap->mPlay.mCallback;
438 playContext = ap->mPlay.mContext;
439 }
440 if (setPlayStateToPaused) {
441 ap->mPlay.mState = SL_PLAYSTATE_PAUSED;
442 }
443 if (needToLock) {
444 interface_unlock_exclusive(&ap->mPlay);
445 }
446 // enqueue callback with no lock held
447 if (NULL != playCallback) {
448 #ifndef USE_ASYNCHRONOUS_PLAY_CALLBACK
449 (*playCallback)(&ap->mPlay.mItf, playContext, SL_PLAYEVENT_HEADATEND);
450 #else
451 SLresult result = EnqueueAsyncCallback_ppi(ap, playCallback, &ap->mPlay.mItf, playContext,
452 SL_PLAYEVENT_HEADATEND);
453 if (SL_RESULT_SUCCESS != result) {
454 ALOGW("Callback %p(%p, %p, SL_PLAYEVENT_HEADATEND) dropped", playCallback,
455 &ap->mPlay.mItf, playContext);
456 }
457 #endif
458 }
459
460 }
461
462
463 //-----------------------------------------------------------------------------
audioPlayer_setStreamType(CAudioPlayer * ap,SLint32 type)464 SLresult audioPlayer_setStreamType(CAudioPlayer* ap, SLint32 type) {
465 SLresult result = SL_RESULT_SUCCESS;
466 SL_LOGV("type %d", type);
467
468 audio_stream_type_t newStreamType = ANDROID_DEFAULT_OUTPUT_STREAM_TYPE;
469 switch(type) {
470 case SL_ANDROID_STREAM_VOICE:
471 newStreamType = AUDIO_STREAM_VOICE_CALL;
472 break;
473 case SL_ANDROID_STREAM_SYSTEM:
474 newStreamType = AUDIO_STREAM_SYSTEM;
475 break;
476 case SL_ANDROID_STREAM_RING:
477 newStreamType = AUDIO_STREAM_RING;
478 break;
479 case SL_ANDROID_STREAM_MEDIA:
480 newStreamType = AUDIO_STREAM_MUSIC;
481 break;
482 case SL_ANDROID_STREAM_ALARM:
483 newStreamType = AUDIO_STREAM_ALARM;
484 break;
485 case SL_ANDROID_STREAM_NOTIFICATION:
486 newStreamType = AUDIO_STREAM_NOTIFICATION;
487 break;
488 default:
489 SL_LOGE(ERROR_PLAYERSTREAMTYPE_SET_UNKNOWN_TYPE);
490 result = SL_RESULT_PARAMETER_INVALID;
491 break;
492 }
493
494 // stream type needs to be set before the object is realized
495 // (ap->mAudioTrack is supposed to be NULL until then)
496 if (SL_OBJECT_STATE_UNREALIZED != ap->mObject.mState) {
497 SL_LOGE(ERROR_PLAYERSTREAMTYPE_REALIZED);
498 result = SL_RESULT_PRECONDITIONS_VIOLATED;
499 } else {
500 ap->mStreamType = newStreamType;
501 }
502
503 return result;
504 }
505
506
507 //-----------------------------------------------------------------------------
audioPlayer_getStreamType(CAudioPlayer * ap,SLint32 * pType)508 SLresult audioPlayer_getStreamType(CAudioPlayer* ap, SLint32 *pType) {
509 SLresult result = SL_RESULT_SUCCESS;
510
511 switch(ap->mStreamType) {
512 case AUDIO_STREAM_VOICE_CALL:
513 *pType = SL_ANDROID_STREAM_VOICE;
514 break;
515 case AUDIO_STREAM_SYSTEM:
516 *pType = SL_ANDROID_STREAM_SYSTEM;
517 break;
518 case AUDIO_STREAM_RING:
519 *pType = SL_ANDROID_STREAM_RING;
520 break;
521 case AUDIO_STREAM_DEFAULT:
522 case AUDIO_STREAM_MUSIC:
523 *pType = SL_ANDROID_STREAM_MEDIA;
524 break;
525 case AUDIO_STREAM_ALARM:
526 *pType = SL_ANDROID_STREAM_ALARM;
527 break;
528 case AUDIO_STREAM_NOTIFICATION:
529 *pType = SL_ANDROID_STREAM_NOTIFICATION;
530 break;
531 default:
532 result = SL_RESULT_INTERNAL_ERROR;
533 *pType = SL_ANDROID_STREAM_MEDIA;
534 break;
535 }
536
537 return result;
538 }
539
540
541 //-----------------------------------------------------------------------------
audioPlayer_auxEffectUpdate(CAudioPlayer * ap)542 void audioPlayer_auxEffectUpdate(CAudioPlayer* ap) {
543 if ((ap->mAudioTrack != 0) && (ap->mAuxEffect != 0)) {
544 android_fxSend_attach(ap, true, ap->mAuxEffect, ap->mVolume.mLevel + ap->mAuxSendLevel);
545 }
546 }
547
548
549 //-----------------------------------------------------------------------------
550 /*
551 * returns true if the given data sink is supported by AudioPlayer that doesn't
552 * play to an OutputMix object, false otherwise
553 *
554 * pre-condition: the locator of the audio sink is not SL_DATALOCATOR_OUTPUTMIX
555 */
audioPlayer_isSupportedNonOutputMixSink(const SLDataSink * pAudioSink)556 bool audioPlayer_isSupportedNonOutputMixSink(const SLDataSink* pAudioSink) {
557 bool result = true;
558 const SLuint32 sinkLocatorType = *(SLuint32 *)pAudioSink->pLocator;
559 const SLuint32 sinkFormatType = *(SLuint32 *)pAudioSink->pFormat;
560
561 switch (sinkLocatorType) {
562
563 case SL_DATALOCATOR_BUFFERQUEUE:
564 case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE:
565 if (SL_DATAFORMAT_PCM != sinkFormatType) {
566 SL_LOGE("Unsupported sink format 0x%x, expected SL_DATAFORMAT_PCM",
567 (unsigned)sinkFormatType);
568 result = false;
569 }
570 // it's no use checking the PCM format fields because additional characteristics
571 // such as the number of channels, or sample size are unknown to the player at this stage
572 break;
573
574 default:
575 SL_LOGE("Unsupported sink locator type 0x%x", (unsigned)sinkLocatorType);
576 result = false;
577 break;
578 }
579
580 return result;
581 }
582
583
584 //-----------------------------------------------------------------------------
585 /*
586 * returns the Android object type if the locator type combinations for the source and sinks
587 * are supported by this implementation, INVALID_TYPE otherwise
588 */
audioPlayer_getAndroidObjectTypeForSourceSink(CAudioPlayer * ap)589 AndroidObjectType audioPlayer_getAndroidObjectTypeForSourceSink(CAudioPlayer *ap) {
590
591 const SLDataSource *pAudioSrc = &ap->mDataSource.u.mSource;
592 const SLDataSink *pAudioSnk = &ap->mDataSink.u.mSink;
593 const SLuint32 sourceLocatorType = *(SLuint32 *)pAudioSrc->pLocator;
594 const SLuint32 sinkLocatorType = *(SLuint32 *)pAudioSnk->pLocator;
595 AndroidObjectType type = INVALID_TYPE;
596
597 //--------------------------------------
598 // Sink / source matching check:
599 // the following source / sink combinations are supported
600 // SL_DATALOCATOR_BUFFERQUEUE / SL_DATALOCATOR_OUTPUTMIX
601 // SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE / SL_DATALOCATOR_OUTPUTMIX
602 // SL_DATALOCATOR_URI / SL_DATALOCATOR_OUTPUTMIX
603 // SL_DATALOCATOR_ANDROIDFD / SL_DATALOCATOR_OUTPUTMIX
604 // SL_DATALOCATOR_ANDROIDBUFFERQUEUE / SL_DATALOCATOR_OUTPUTMIX
605 // SL_DATALOCATOR_ANDROIDBUFFERQUEUE / SL_DATALOCATOR_BUFFERQUEUE
606 // SL_DATALOCATOR_URI / SL_DATALOCATOR_BUFFERQUEUE
607 // SL_DATALOCATOR_ANDROIDFD / SL_DATALOCATOR_BUFFERQUEUE
608 // SL_DATALOCATOR_URI / SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE
609 // SL_DATALOCATOR_ANDROIDFD / SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE
610 switch (sinkLocatorType) {
611
612 case SL_DATALOCATOR_OUTPUTMIX: {
613 switch (sourceLocatorType) {
614
615 // Buffer Queue to AudioTrack
616 case SL_DATALOCATOR_BUFFERQUEUE:
617 case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE:
618 type = AUDIOPLAYER_FROM_PCM_BUFFERQUEUE;
619 break;
620
621 // URI or FD to MediaPlayer
622 case SL_DATALOCATOR_URI:
623 case SL_DATALOCATOR_ANDROIDFD:
624 type = AUDIOPLAYER_FROM_URIFD;
625 break;
626
627 // Android BufferQueue to MediaPlayer (shared memory streaming)
628 case SL_DATALOCATOR_ANDROIDBUFFERQUEUE:
629 type = AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE;
630 break;
631
632 default:
633 SL_LOGE("Source data locator 0x%x not supported with SL_DATALOCATOR_OUTPUTMIX sink",
634 (unsigned)sourceLocatorType);
635 break;
636 }
637 }
638 break;
639
640 case SL_DATALOCATOR_BUFFERQUEUE:
641 case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE:
642 switch (sourceLocatorType) {
643
644 // URI or FD decoded to PCM in a buffer queue
645 case SL_DATALOCATOR_URI:
646 case SL_DATALOCATOR_ANDROIDFD:
647 type = AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE;
648 break;
649
650 // AAC ADTS Android buffer queue decoded to PCM in a buffer queue
651 case SL_DATALOCATOR_ANDROIDBUFFERQUEUE:
652 type = AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE;
653 break;
654
655 default:
656 SL_LOGE("Source data locator 0x%x not supported with SL_DATALOCATOR_BUFFERQUEUE sink",
657 (unsigned)sourceLocatorType);
658 break;
659 }
660 break;
661
662 default:
663 SL_LOGE("Sink data locator 0x%x not supported", (unsigned)sinkLocatorType);
664 break;
665 }
666
667 return type;
668 }
669
670
671 //-----------------------------------------------------------------------------
672 /*
673 * Callback associated with an SfPlayer of an SL ES AudioPlayer that gets its data
674 * from a URI or FD, for prepare, prefetch, and play events
675 */
sfplayer_handlePrefetchEvent(int event,int data1,int data2,void * user)676 static void sfplayer_handlePrefetchEvent(int event, int data1, int data2, void* user) {
677
678 // FIXME see similar code and comment in player_handleMediaPlayerEventNotifications
679
680 if (NULL == user) {
681 return;
682 }
683
684 CAudioPlayer *ap = (CAudioPlayer *)user;
685 if (!android::CallbackProtector::enterCbIfOk(ap->mCallbackProtector)) {
686 // it is not safe to enter the callback (the track is about to go away)
687 return;
688 }
689 union {
690 char c[sizeof(int)];
691 int i;
692 } u;
693 u.i = event;
694 SL_LOGV("sfplayer_handlePrefetchEvent(event='%c%c%c%c' (%d), data1=%d, data2=%d, user=%p) from "
695 "SfAudioPlayer", u.c[3], u.c[2], u.c[1], u.c[0], event, data1, data2, user);
696 switch(event) {
697
698 case android::GenericPlayer::kEventPrepared: {
699 SL_LOGV("Received GenericPlayer::kEventPrepared for CAudioPlayer %p", ap);
700
701 // assume no callback
702 slPrefetchCallback callback = NULL;
703 void* callbackPContext;
704 SLuint32 events;
705
706 object_lock_exclusive(&ap->mObject);
707
708 // mark object as prepared; same state is used for successful or unsuccessful prepare
709 assert(ap->mAndroidObjState == ANDROID_PREPARING);
710 ap->mAndroidObjState = ANDROID_READY;
711
712 if (PLAYER_SUCCESS == data1) {
713 // Most of successful prepare completion for ap->mAPlayer
714 // is handled by GenericPlayer and its subclasses.
715 } else {
716 // SfPlayer prepare() failed prefetching, there is no event in SLPrefetchStatus to
717 // indicate a prefetch error, so we signal it by sending simultaneously two events:
718 // - SL_PREFETCHEVENT_FILLLEVELCHANGE with a level of 0
719 // - SL_PREFETCHEVENT_STATUSCHANGE with a status of SL_PREFETCHSTATUS_UNDERFLOW
720 SL_LOGE(ERROR_PLAYER_PREFETCH_d, data1);
721 if (IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) {
722 ap->mPrefetchStatus.mLevel = 0;
723 ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW;
724 if (!(~ap->mPrefetchStatus.mCallbackEventsMask &
725 (SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE))) {
726 callback = ap->mPrefetchStatus.mCallback;
727 callbackPContext = ap->mPrefetchStatus.mContext;
728 events = SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE;
729 }
730 }
731 }
732
733 object_unlock_exclusive(&ap->mObject);
734
735 // callback with no lock held
736 if (NULL != callback) {
737 (*callback)(&ap->mPrefetchStatus.mItf, callbackPContext, events);
738 }
739
740 }
741 break;
742
743 case android::GenericPlayer::kEventPrefetchFillLevelUpdate : {
744 if (!IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) {
745 break;
746 }
747 slPrefetchCallback callback = NULL;
748 void* callbackPContext = NULL;
749
750 // SLPrefetchStatusItf callback or no callback?
751 interface_lock_exclusive(&ap->mPrefetchStatus);
752 if (ap->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_FILLLEVELCHANGE) {
753 callback = ap->mPrefetchStatus.mCallback;
754 callbackPContext = ap->mPrefetchStatus.mContext;
755 }
756 ap->mPrefetchStatus.mLevel = (SLpermille)data1;
757 interface_unlock_exclusive(&ap->mPrefetchStatus);
758
759 // callback with no lock held
760 if (NULL != callback) {
761 (*callback)(&ap->mPrefetchStatus.mItf, callbackPContext,
762 SL_PREFETCHEVENT_FILLLEVELCHANGE);
763 }
764 }
765 break;
766
767 case android::GenericPlayer::kEventPrefetchStatusChange: {
768 if (!IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) {
769 break;
770 }
771 slPrefetchCallback callback = NULL;
772 void* callbackPContext = NULL;
773
774 // SLPrefetchStatusItf callback or no callback?
775 object_lock_exclusive(&ap->mObject);
776 if (ap->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_STATUSCHANGE) {
777 callback = ap->mPrefetchStatus.mCallback;
778 callbackPContext = ap->mPrefetchStatus.mContext;
779 }
780 if (data1 >= android::kStatusIntermediate) {
781 ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_SUFFICIENTDATA;
782 } else if (data1 < android::kStatusIntermediate) {
783 ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW;
784 }
785 object_unlock_exclusive(&ap->mObject);
786
787 // callback with no lock held
788 if (NULL != callback) {
789 (*callback)(&ap->mPrefetchStatus.mItf, callbackPContext, SL_PREFETCHEVENT_STATUSCHANGE);
790 }
791 }
792 break;
793
794 case android::GenericPlayer::kEventEndOfStream: {
795 audioPlayer_dispatch_headAtEnd_lockPlay(ap, true /*set state to paused?*/, true);
796 if ((ap->mAudioTrack != 0) && (!ap->mSeek.mLoopEnabled)) {
797 ap->mAudioTrack->stop();
798 }
799 }
800 break;
801
802 case android::GenericPlayer::kEventChannelCount: {
803 object_lock_exclusive(&ap->mObject);
804 if (UNKNOWN_NUMCHANNELS == ap->mNumChannels && UNKNOWN_NUMCHANNELS != data1) {
805 ap->mNumChannels = data1;
806 android_audioPlayer_volumeUpdate(ap);
807 }
808 object_unlock_exclusive(&ap->mObject);
809 }
810 break;
811
812 case android::GenericPlayer::kEventPlay: {
813 slPlayCallback callback = NULL;
814 void* callbackPContext = NULL;
815
816 interface_lock_shared(&ap->mPlay);
817 callback = ap->mPlay.mCallback;
818 callbackPContext = ap->mPlay.mContext;
819 interface_unlock_shared(&ap->mPlay);
820
821 if (NULL != callback) {
822 SLuint32 event = (SLuint32) data1; // SL_PLAYEVENT_HEAD*
823 #ifndef USE_ASYNCHRONOUS_PLAY_CALLBACK
824 // synchronous callback requires a synchronous GetPosition implementation
825 (*callback)(&ap->mPlay.mItf, callbackPContext, event);
826 #else
827 // asynchronous callback works with any GetPosition implementation
828 SLresult result = EnqueueAsyncCallback_ppi(ap, callback, &ap->mPlay.mItf,
829 callbackPContext, event);
830 if (SL_RESULT_SUCCESS != result) {
831 ALOGW("Callback %p(%p, %p, 0x%x) dropped", callback,
832 &ap->mPlay.mItf, callbackPContext, event);
833 }
834 #endif
835 }
836 }
837 break;
838
839 case android::GenericPlayer::kEventErrorAfterPrepare: {
840 SL_LOGV("kEventErrorAfterPrepare");
841
842 // assume no callback
843 slPrefetchCallback callback = NULL;
844 void* callbackPContext = NULL;
845
846 object_lock_exclusive(&ap->mObject);
847 if (IsInterfaceInitialized(&ap->mObject, MPH_PREFETCHSTATUS)) {
848 ap->mPrefetchStatus.mLevel = 0;
849 ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW;
850 if (!(~ap->mPrefetchStatus.mCallbackEventsMask &
851 (SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE))) {
852 callback = ap->mPrefetchStatus.mCallback;
853 callbackPContext = ap->mPrefetchStatus.mContext;
854 }
855 }
856 object_unlock_exclusive(&ap->mObject);
857
858 // FIXME there's interesting information in data1, but no API to convey it to client
859 SL_LOGE("Error after prepare: %d", data1);
860
861 // callback with no lock held
862 if (NULL != callback) {
863 (*callback)(&ap->mPrefetchStatus.mItf, callbackPContext,
864 SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE);
865 }
866
867 }
868 break;
869
870 case android::GenericPlayer::kEventHasVideoSize:
871 //SL_LOGW("Unexpected kEventHasVideoSize");
872 break;
873
874 default:
875 break;
876 }
877
878 ap->mCallbackProtector->exitCb();
879 }
880
881
882 //-----------------------------------------------------------------------------
android_audioPlayer_checkSourceSink(CAudioPlayer * pAudioPlayer)883 SLresult android_audioPlayer_checkSourceSink(CAudioPlayer *pAudioPlayer)
884 {
885 // verify that the locator types for the source / sink combination is supported
886 pAudioPlayer->mAndroidObjType = audioPlayer_getAndroidObjectTypeForSourceSink(pAudioPlayer);
887 if (INVALID_TYPE == pAudioPlayer->mAndroidObjType) {
888 return SL_RESULT_PARAMETER_INVALID;
889 }
890
891 const SLDataSource *pAudioSrc = &pAudioPlayer->mDataSource.u.mSource;
892 const SLDataSink *pAudioSnk = &pAudioPlayer->mDataSink.u.mSink;
893
894 // format check:
895 const SLuint32 sourceLocatorType = *(SLuint32 *)pAudioSrc->pLocator;
896 const SLuint32 sinkLocatorType = *(SLuint32 *)pAudioSnk->pLocator;
897 const SLuint32 sourceFormatType = *(SLuint32 *)pAudioSrc->pFormat;
898 const SLuint32 sinkFormatType = *(SLuint32 *)pAudioSnk->pFormat;
899
900 switch (sourceLocatorType) {
901 //------------------
902 // Buffer Queues
903 case SL_DATALOCATOR_BUFFERQUEUE:
904 case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE:
905 {
906 SLDataLocator_BufferQueue *dl_bq = (SLDataLocator_BufferQueue *) pAudioSrc->pLocator;
907
908 // Buffer format
909 switch (sourceFormatType) {
910 // currently only PCM buffer queues are supported,
911 case SL_DATAFORMAT_PCM: {
912 SLDataFormat_PCM *df_pcm = (SLDataFormat_PCM *) pAudioSrc->pFormat;
913 switch (df_pcm->numChannels) {
914 case 1:
915 case 2:
916 break;
917 default:
918 // this should have already been rejected by checkDataFormat
919 SL_LOGE("Cannot create audio player: unsupported " \
920 "PCM data source with %u channels", (unsigned) df_pcm->numChannels);
921 return SL_RESULT_CONTENT_UNSUPPORTED;
922 }
923 switch (df_pcm->samplesPerSec) {
924 case SL_SAMPLINGRATE_8:
925 case SL_SAMPLINGRATE_11_025:
926 case SL_SAMPLINGRATE_12:
927 case SL_SAMPLINGRATE_16:
928 case SL_SAMPLINGRATE_22_05:
929 case SL_SAMPLINGRATE_24:
930 case SL_SAMPLINGRATE_32:
931 case SL_SAMPLINGRATE_44_1:
932 case SL_SAMPLINGRATE_48:
933 break;
934 case SL_SAMPLINGRATE_64:
935 case SL_SAMPLINGRATE_88_2:
936 case SL_SAMPLINGRATE_96:
937 case SL_SAMPLINGRATE_192:
938 default:
939 SL_LOGE("Cannot create audio player: unsupported sample rate %u milliHz",
940 (unsigned) df_pcm->samplesPerSec);
941 return SL_RESULT_CONTENT_UNSUPPORTED;
942 }
943 switch (df_pcm->bitsPerSample) {
944 case SL_PCMSAMPLEFORMAT_FIXED_8:
945 case SL_PCMSAMPLEFORMAT_FIXED_16:
946 break;
947 // others
948 default:
949 // this should have already been rejected by checkDataFormat
950 SL_LOGE("Cannot create audio player: unsupported sample bit depth %u",
951 (SLuint32)df_pcm->bitsPerSample);
952 return SL_RESULT_CONTENT_UNSUPPORTED;
953 }
954 switch (df_pcm->containerSize) {
955 case 8:
956 case 16:
957 break;
958 // others
959 default:
960 SL_LOGE("Cannot create audio player: unsupported container size %u",
961 (unsigned) df_pcm->containerSize);
962 return SL_RESULT_CONTENT_UNSUPPORTED;
963 }
964 // df_pcm->channelMask: the earlier platform-independent check and the
965 // upcoming check by sles_to_android_channelMaskOut are sufficient
966 switch (df_pcm->endianness) {
967 case SL_BYTEORDER_LITTLEENDIAN:
968 break;
969 case SL_BYTEORDER_BIGENDIAN:
970 SL_LOGE("Cannot create audio player: unsupported big-endian byte order");
971 return SL_RESULT_CONTENT_UNSUPPORTED;
972 // native is proposed but not yet in spec
973 default:
974 SL_LOGE("Cannot create audio player: unsupported byte order %u",
975 (unsigned) df_pcm->endianness);
976 return SL_RESULT_CONTENT_UNSUPPORTED;
977 }
978 } //case SL_DATAFORMAT_PCM
979 break;
980 case SL_DATAFORMAT_MIME:
981 case XA_DATAFORMAT_RAWIMAGE:
982 SL_LOGE("Cannot create audio player with buffer queue data source "
983 "without SL_DATAFORMAT_PCM format");
984 return SL_RESULT_CONTENT_UNSUPPORTED;
985 default:
986 // invalid data format is detected earlier
987 assert(false);
988 return SL_RESULT_INTERNAL_ERROR;
989 } // switch (sourceFormatType)
990 } // case SL_DATALOCATOR_BUFFERQUEUE or SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE
991 break;
992 //------------------
993 // URI
994 case SL_DATALOCATOR_URI:
995 {
996 SLDataLocator_URI *dl_uri = (SLDataLocator_URI *) pAudioSrc->pLocator;
997 if (NULL == dl_uri->URI) {
998 return SL_RESULT_PARAMETER_INVALID;
999 }
1000 // URI format
1001 switch (sourceFormatType) {
1002 case SL_DATAFORMAT_MIME:
1003 break;
1004 case SL_DATAFORMAT_PCM:
1005 case XA_DATAFORMAT_RAWIMAGE:
1006 SL_LOGE("Cannot create audio player with SL_DATALOCATOR_URI data source without "
1007 "SL_DATAFORMAT_MIME format");
1008 return SL_RESULT_CONTENT_UNSUPPORTED;
1009 } // switch (sourceFormatType)
1010 // decoding format check
1011 if ((sinkLocatorType != SL_DATALOCATOR_OUTPUTMIX) &&
1012 !audioPlayer_isSupportedNonOutputMixSink(pAudioSnk)) {
1013 return SL_RESULT_CONTENT_UNSUPPORTED;
1014 }
1015 } // case SL_DATALOCATOR_URI
1016 break;
1017 //------------------
1018 // File Descriptor
1019 case SL_DATALOCATOR_ANDROIDFD:
1020 {
1021 // fd is already non null
1022 switch (sourceFormatType) {
1023 case SL_DATAFORMAT_MIME:
1024 break;
1025 case SL_DATAFORMAT_PCM:
1026 // FIXME implement
1027 SL_LOGD("[ FIXME implement PCM FD data sources ]");
1028 break;
1029 case XA_DATAFORMAT_RAWIMAGE:
1030 SL_LOGE("Cannot create audio player with SL_DATALOCATOR_ANDROIDFD data source "
1031 "without SL_DATAFORMAT_MIME or SL_DATAFORMAT_PCM format");
1032 return SL_RESULT_CONTENT_UNSUPPORTED;
1033 default:
1034 // invalid data format is detected earlier
1035 assert(false);
1036 return SL_RESULT_INTERNAL_ERROR;
1037 } // switch (sourceFormatType)
1038 if ((sinkLocatorType != SL_DATALOCATOR_OUTPUTMIX) &&
1039 !audioPlayer_isSupportedNonOutputMixSink(pAudioSnk)) {
1040 return SL_RESULT_CONTENT_UNSUPPORTED;
1041 }
1042 } // case SL_DATALOCATOR_ANDROIDFD
1043 break;
1044 //------------------
1045 // Stream
1046 case SL_DATALOCATOR_ANDROIDBUFFERQUEUE:
1047 {
1048 switch (sourceFormatType) {
1049 case SL_DATAFORMAT_MIME:
1050 {
1051 SLDataFormat_MIME *df_mime = (SLDataFormat_MIME *) pAudioSrc->pFormat;
1052 if (NULL == df_mime) {
1053 SL_LOGE("MIME type null invalid");
1054 return SL_RESULT_CONTENT_UNSUPPORTED;
1055 }
1056 SL_LOGD("source MIME is %s", (char*)df_mime->mimeType);
1057 switch(df_mime->containerType) {
1058 case SL_CONTAINERTYPE_MPEG_TS:
1059 if (strcasecmp((char*)df_mime->mimeType, (const char *)XA_ANDROID_MIME_MP2TS)) {
1060 SL_LOGE("Invalid MIME (%s) for container SL_CONTAINERTYPE_MPEG_TS, expects %s",
1061 (char*)df_mime->mimeType, XA_ANDROID_MIME_MP2TS);
1062 return SL_RESULT_CONTENT_UNSUPPORTED;
1063 }
1064 break;
1065 case SL_CONTAINERTYPE_RAW:
1066 case SL_CONTAINERTYPE_AAC:
1067 if (strcasecmp((char*)df_mime->mimeType, (const char *)SL_ANDROID_MIME_AACADTS) &&
1068 strcasecmp((char*)df_mime->mimeType,
1069 ANDROID_MIME_AACADTS_ANDROID_FRAMEWORK)) {
1070 SL_LOGE("Invalid MIME (%s) for container type %d, expects %s",
1071 (char*)df_mime->mimeType, df_mime->containerType,
1072 SL_ANDROID_MIME_AACADTS);
1073 return SL_RESULT_CONTENT_UNSUPPORTED;
1074 }
1075 break;
1076 default:
1077 SL_LOGE("Cannot create player with SL_DATALOCATOR_ANDROIDBUFFERQUEUE data source "
1078 "that is not fed MPEG-2 TS data or AAC ADTS data");
1079 return SL_RESULT_CONTENT_UNSUPPORTED;
1080 }
1081 }
1082 break;
1083 default:
1084 SL_LOGE("Cannot create player with SL_DATALOCATOR_ANDROIDBUFFERQUEUE data source "
1085 "without SL_DATAFORMAT_MIME format");
1086 return SL_RESULT_CONTENT_UNSUPPORTED;
1087 }
1088 }
1089 break; // case SL_DATALOCATOR_ANDROIDBUFFERQUEUE
1090 //------------------
1091 // Address
1092 case SL_DATALOCATOR_ADDRESS:
1093 case SL_DATALOCATOR_IODEVICE:
1094 case SL_DATALOCATOR_OUTPUTMIX:
1095 case XA_DATALOCATOR_NATIVEDISPLAY:
1096 case SL_DATALOCATOR_MIDIBUFFERQUEUE:
1097 SL_LOGE("Cannot create audio player with data locator type 0x%x",
1098 (unsigned) sourceLocatorType);
1099 return SL_RESULT_CONTENT_UNSUPPORTED;
1100 default:
1101 SL_LOGE("Cannot create audio player with invalid data locator type 0x%x",
1102 (unsigned) sourceLocatorType);
1103 return SL_RESULT_PARAMETER_INVALID;
1104 }// switch (locatorType)
1105
1106 return SL_RESULT_SUCCESS;
1107 }
1108
1109
1110 //-----------------------------------------------------------------------------
1111 // Callback associated with an AudioTrack of an SL ES AudioPlayer that gets its data
1112 // from a buffer queue. This will not be called once the AudioTrack has been destroyed.
audioTrack_callBack_pullFromBuffQueue(int event,void * user,void * info)1113 static void audioTrack_callBack_pullFromBuffQueue(int event, void* user, void *info) {
1114 CAudioPlayer *ap = (CAudioPlayer *)user;
1115
1116 if (!android::CallbackProtector::enterCbIfOk(ap->mCallbackProtector)) {
1117 // it is not safe to enter the callback (the track is about to go away)
1118 return;
1119 }
1120
1121 void * callbackPContext = NULL;
1122 switch(event) {
1123
1124 case android::AudioTrack::EVENT_MORE_DATA: {
1125 //SL_LOGV("received event EVENT_MORE_DATA from AudioTrack TID=%d", gettid());
1126 slBufferQueueCallback callback = NULL;
1127 slPrefetchCallback prefetchCallback = NULL;
1128 void *prefetchContext = NULL;
1129 SLuint32 prefetchEvents = SL_PREFETCHEVENT_NONE;
1130 android::AudioTrack::Buffer* pBuff = (android::AudioTrack::Buffer*)info;
1131
1132 // retrieve data from the buffer queue
1133 interface_lock_exclusive(&ap->mBufferQueue);
1134
1135 if (ap->mBufferQueue.mState.count != 0) {
1136 //SL_LOGV("nbBuffers in queue = %u",ap->mBufferQueue.mState.count);
1137 assert(ap->mBufferQueue.mFront != ap->mBufferQueue.mRear);
1138
1139 BufferHeader *oldFront = ap->mBufferQueue.mFront;
1140 BufferHeader *newFront = &oldFront[1];
1141
1142 // declared as void * because this code supports both 8-bit and 16-bit PCM data
1143 void *pSrc = (char *)oldFront->mBuffer + ap->mBufferQueue.mSizeConsumed;
1144 if (ap->mBufferQueue.mSizeConsumed + pBuff->size < oldFront->mSize) {
1145 // can't consume the whole or rest of the buffer in one shot
1146 ap->mBufferQueue.mSizeConsumed += pBuff->size;
1147 // leave pBuff->size untouched
1148 // consume data
1149 // FIXME can we avoid holding the lock during the copy?
1150 memcpy (pBuff->raw, pSrc, pBuff->size);
1151 } else {
1152 // finish consuming the buffer or consume the buffer in one shot
1153 pBuff->size = oldFront->mSize - ap->mBufferQueue.mSizeConsumed;
1154 ap->mBufferQueue.mSizeConsumed = 0;
1155
1156 if (newFront ==
1157 &ap->mBufferQueue.mArray
1158 [ap->mBufferQueue.mNumBuffers + 1])
1159 {
1160 newFront = ap->mBufferQueue.mArray;
1161 }
1162 ap->mBufferQueue.mFront = newFront;
1163
1164 ap->mBufferQueue.mState.count--;
1165 ap->mBufferQueue.mState.playIndex++;
1166
1167 // consume data
1168 // FIXME can we avoid holding the lock during the copy?
1169 memcpy (pBuff->raw, pSrc, pBuff->size);
1170
1171 // data has been consumed, and the buffer queue state has been updated
1172 // we will notify the client if applicable
1173 callback = ap->mBufferQueue.mCallback;
1174 // save callback data
1175 callbackPContext = ap->mBufferQueue.mContext;
1176 }
1177 } else { // empty queue
1178 // signal no data available
1179 pBuff->size = 0;
1180
1181 // signal we're at the end of the content, but don't pause (see note in function)
1182 audioPlayer_dispatch_headAtEnd_lockPlay(ap, false /*set state to paused?*/, false);
1183
1184 // signal underflow to prefetch status itf
1185 if (IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) {
1186 ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW;
1187 ap->mPrefetchStatus.mLevel = 0;
1188 // callback or no callback?
1189 prefetchEvents = ap->mPrefetchStatus.mCallbackEventsMask &
1190 (SL_PREFETCHEVENT_STATUSCHANGE | SL_PREFETCHEVENT_FILLLEVELCHANGE);
1191 if (SL_PREFETCHEVENT_NONE != prefetchEvents) {
1192 prefetchCallback = ap->mPrefetchStatus.mCallback;
1193 prefetchContext = ap->mPrefetchStatus.mContext;
1194 }
1195 }
1196
1197 // stop the track so it restarts playing faster when new data is enqueued
1198 ap->mAudioTrack->stop();
1199 }
1200 interface_unlock_exclusive(&ap->mBufferQueue);
1201
1202 // notify client
1203 if (NULL != prefetchCallback) {
1204 assert(SL_PREFETCHEVENT_NONE != prefetchEvents);
1205 // spec requires separate callbacks for each event
1206 if (prefetchEvents & SL_PREFETCHEVENT_STATUSCHANGE) {
1207 (*prefetchCallback)(&ap->mPrefetchStatus.mItf, prefetchContext,
1208 SL_PREFETCHEVENT_STATUSCHANGE);
1209 }
1210 if (prefetchEvents & SL_PREFETCHEVENT_FILLLEVELCHANGE) {
1211 (*prefetchCallback)(&ap->mPrefetchStatus.mItf, prefetchContext,
1212 SL_PREFETCHEVENT_FILLLEVELCHANGE);
1213 }
1214 }
1215 if (NULL != callback) {
1216 (*callback)(&ap->mBufferQueue.mItf, callbackPContext);
1217 }
1218 }
1219 break;
1220
1221 case android::AudioTrack::EVENT_MARKER:
1222 //SL_LOGI("received event EVENT_MARKER from AudioTrack");
1223 audioTrack_handleMarker_lockPlay(ap);
1224 break;
1225
1226 case android::AudioTrack::EVENT_NEW_POS:
1227 //SL_LOGI("received event EVENT_NEW_POS from AudioTrack");
1228 audioTrack_handleNewPos_lockPlay(ap);
1229 break;
1230
1231 case android::AudioTrack::EVENT_UNDERRUN:
1232 //SL_LOGI("received event EVENT_UNDERRUN from AudioTrack");
1233 audioTrack_handleUnderrun_lockPlay(ap);
1234 break;
1235
1236 case android::AudioTrack::EVENT_BUFFER_END:
1237 case android::AudioTrack::EVENT_LOOP_END:
1238 // These are unexpected so fall through
1239 default:
1240 // FIXME where does the notification of SL_PLAYEVENT_HEADMOVING fit?
1241 SL_LOGE("Encountered unknown AudioTrack event %d for CAudioPlayer %p", event,
1242 (CAudioPlayer *)user);
1243 break;
1244 }
1245
1246 ap->mCallbackProtector->exitCb();
1247 }
1248
1249
1250 //-----------------------------------------------------------------------------
android_audioPlayer_create(CAudioPlayer * pAudioPlayer)1251 void android_audioPlayer_create(CAudioPlayer *pAudioPlayer) {
1252
1253 // pAudioPlayer->mAndroidObjType has been set in android_audioPlayer_checkSourceSink()
1254 // and if it was == INVALID_TYPE, then IEngine_CreateAudioPlayer would never call us
1255 assert(INVALID_TYPE != pAudioPlayer->mAndroidObjType);
1256
1257 // These initializations are in the same order as the field declarations in classes.h
1258
1259 // FIXME Consolidate initializations (many of these already in IEngine_CreateAudioPlayer)
1260 // mAndroidObjType: see above comment
1261 pAudioPlayer->mAndroidObjState = ANDROID_UNINITIALIZED;
1262 pAudioPlayer->mSessionId = android::AudioSystem::newAudioSessionId();
1263
1264 // placeholder: not necessary yet as session ID lifetime doesn't extend beyond player
1265 // android::AudioSystem::acquireAudioSessionId(pAudioPlayer->mSessionId);
1266
1267 pAudioPlayer->mStreamType = ANDROID_DEFAULT_OUTPUT_STREAM_TYPE;
1268
1269 // mAudioTrack
1270 pAudioPlayer->mCallbackProtector = new android::CallbackProtector();
1271 // mAPLayer
1272 // mAuxEffect
1273
1274 pAudioPlayer->mAuxSendLevel = 0;
1275 pAudioPlayer->mAmplFromDirectLevel = 1.0f; // matches initial mDirectLevel value
1276 pAudioPlayer->mDeferredStart = false;
1277
1278 // This section re-initializes interface-specific fields that
1279 // can be set or used regardless of whether the interface is
1280 // exposed on the AudioPlayer or not
1281
1282 switch (pAudioPlayer->mAndroidObjType) {
1283 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
1284 pAudioPlayer->mPlaybackRate.mMinRate = AUDIOTRACK_MIN_PLAYBACKRATE_PERMILLE;
1285 pAudioPlayer->mPlaybackRate.mMaxRate = AUDIOTRACK_MAX_PLAYBACKRATE_PERMILLE;
1286 break;
1287 case AUDIOPLAYER_FROM_URIFD:
1288 pAudioPlayer->mPlaybackRate.mMinRate = MEDIAPLAYER_MIN_PLAYBACKRATE_PERMILLE;
1289 pAudioPlayer->mPlaybackRate.mMaxRate = MEDIAPLAYER_MAX_PLAYBACKRATE_PERMILLE;
1290 break;
1291 default:
1292 // use the default range
1293 break;
1294 }
1295
1296 }
1297
1298
1299 //-----------------------------------------------------------------------------
android_audioPlayer_setConfig(CAudioPlayer * ap,const SLchar * configKey,const void * pConfigValue,SLuint32 valueSize)1300 SLresult android_audioPlayer_setConfig(CAudioPlayer *ap, const SLchar *configKey,
1301 const void *pConfigValue, SLuint32 valueSize) {
1302
1303 SLresult result;
1304
1305 assert(NULL != ap && NULL != configKey && NULL != pConfigValue);
1306 if (strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_STREAM_TYPE) == 0) {
1307
1308 // stream type
1309 if (KEY_STREAM_TYPE_PARAMSIZE > valueSize) {
1310 SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW);
1311 result = SL_RESULT_BUFFER_INSUFFICIENT;
1312 } else {
1313 result = audioPlayer_setStreamType(ap, *(SLuint32*)pConfigValue);
1314 }
1315
1316 } else {
1317 SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY);
1318 result = SL_RESULT_PARAMETER_INVALID;
1319 }
1320
1321 return result;
1322 }
1323
1324
1325 //-----------------------------------------------------------------------------
android_audioPlayer_getConfig(CAudioPlayer * ap,const SLchar * configKey,SLuint32 * pValueSize,void * pConfigValue)1326 SLresult android_audioPlayer_getConfig(CAudioPlayer* ap, const SLchar *configKey,
1327 SLuint32* pValueSize, void *pConfigValue) {
1328
1329 SLresult result;
1330
1331 assert(NULL != ap && NULL != configKey && NULL != pValueSize);
1332 if (strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_STREAM_TYPE) == 0) {
1333
1334 // stream type
1335 if (NULL == pConfigValue) {
1336 result = SL_RESULT_SUCCESS;
1337 } else if (KEY_STREAM_TYPE_PARAMSIZE > *pValueSize) {
1338 SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW);
1339 result = SL_RESULT_BUFFER_INSUFFICIENT;
1340 } else {
1341 result = audioPlayer_getStreamType(ap, (SLint32*)pConfigValue);
1342 }
1343 *pValueSize = KEY_STREAM_TYPE_PARAMSIZE;
1344
1345 } else {
1346 SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY);
1347 result = SL_RESULT_PARAMETER_INVALID;
1348 }
1349
1350 return result;
1351 }
1352
1353
1354 // Called from android_audioPlayer_realize for a PCM buffer queue player
1355 // to determine if it can use a fast track.
canUseFastTrack(CAudioPlayer * pAudioPlayer)1356 static bool canUseFastTrack(CAudioPlayer *pAudioPlayer)
1357 {
1358 assert(pAudioPlayer->mAndroidObjType == AUDIOPLAYER_FROM_PCM_BUFFERQUEUE);
1359 if (pAudioPlayer->mBufferQueue.mNumBuffers < 2) {
1360 return false;
1361 }
1362
1363 // Check a blacklist of interfaces that are incompatible with fast tracks.
1364 // The alternative, to check a whitelist of compatible interfaces, is
1365 // more maintainable but is too slow. As a compromise, in a debug build
1366 // we use both methods and warn if they produce different results.
1367 // In release builds, we only use the blacklist method.
1368 // If a blacklisted interface is added after realization using
1369 // DynamicInterfaceManagement::AddInterface,
1370 // then this won't be detected but the interface will be ineffective.
1371 bool blacklistResult = true;
1372 static const unsigned blacklist[] = {
1373 MPH_BASSBOOST,
1374 MPH_EFFECTSEND,
1375 MPH_ENVIRONMENTALREVERB,
1376 MPH_EQUALIZER,
1377 MPH_PLAYBACKRATE,
1378 MPH_PRESETREVERB,
1379 MPH_VIRTUALIZER,
1380 MPH_ANDROIDEFFECT,
1381 MPH_ANDROIDEFFECTSEND,
1382 // FIXME The problem with a blacklist is remembering to add new interfaces here
1383 };
1384 for (unsigned i = 0; i < sizeof(blacklist)/sizeof(blacklist[0]); ++i) {
1385 if (IsInterfaceInitialized(&pAudioPlayer->mObject, blacklist[i])) {
1386 blacklistResult = false;
1387 break;
1388 }
1389 }
1390 #if LOG_NDEBUG == 0
1391 bool whitelistResult = true;
1392 static const unsigned whitelist[] = {
1393 MPH_BUFFERQUEUE,
1394 MPH_DYNAMICINTERFACEMANAGEMENT,
1395 MPH_METADATAEXTRACTION,
1396 MPH_MUTESOLO,
1397 MPH_OBJECT,
1398 MPH_PLAY,
1399 MPH_PREFETCHSTATUS,
1400 MPH_VOLUME,
1401 MPH_ANDROIDCONFIGURATION,
1402 MPH_ANDROIDSIMPLEBUFFERQUEUE,
1403 MPH_ANDROIDBUFFERQUEUESOURCE,
1404 };
1405 for (unsigned mph = MPH_MIN; mph < MPH_MAX; ++mph) {
1406 for (unsigned i = 0; i < sizeof(whitelist)/sizeof(whitelist[0]); ++i) {
1407 if (mph == whitelist[i]) {
1408 goto compatible;
1409 }
1410 }
1411 if (IsInterfaceInitialized(&pAudioPlayer->mObject, mph)) {
1412 whitelistResult = false;
1413 break;
1414 }
1415 compatible: ;
1416 }
1417 if (whitelistResult != blacklistResult) {
1418 ALOGW("whitelistResult != blacklistResult");
1419 // and use blacklistResult below
1420 }
1421 #endif
1422 return blacklistResult;
1423 }
1424
1425
1426 //-----------------------------------------------------------------------------
1427 // FIXME abstract out the diff between CMediaPlayer and CAudioPlayer
android_audioPlayer_realize(CAudioPlayer * pAudioPlayer,SLboolean async)1428 SLresult android_audioPlayer_realize(CAudioPlayer *pAudioPlayer, SLboolean async) {
1429
1430 SLresult result = SL_RESULT_SUCCESS;
1431 SL_LOGV("Realize pAudioPlayer=%p", pAudioPlayer);
1432
1433 AudioPlayback_Parameters app;
1434 app.sessionId = pAudioPlayer->mSessionId;
1435 app.streamType = pAudioPlayer->mStreamType;
1436
1437 switch (pAudioPlayer->mAndroidObjType) {
1438
1439 //-----------------------------------
1440 // AudioTrack
1441 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
1442 {
1443 // initialize platform-specific CAudioPlayer fields
1444
1445 SLDataLocator_BufferQueue *dl_bq = (SLDataLocator_BufferQueue *)
1446 pAudioPlayer->mDynamicSource.mDataSource;
1447 SLDataFormat_PCM *df_pcm = (SLDataFormat_PCM *)
1448 pAudioPlayer->mDynamicSource.mDataSource->pFormat;
1449
1450 uint32_t sampleRate = sles_to_android_sampleRate(df_pcm->samplesPerSec);
1451
1452 audio_output_flags_t policy;
1453 if (canUseFastTrack(pAudioPlayer)) {
1454 policy = AUDIO_OUTPUT_FLAG_FAST;
1455 } else {
1456 policy = AUDIO_OUTPUT_FLAG_NONE;
1457 }
1458
1459 pAudioPlayer->mAudioTrack = new android::AudioTrack(
1460 pAudioPlayer->mStreamType, // streamType
1461 sampleRate, // sampleRate
1462 sles_to_android_sampleFormat(df_pcm->bitsPerSample), // format
1463 sles_to_android_channelMaskOut(df_pcm->numChannels, df_pcm->channelMask),
1464 // channel mask
1465 0, // frameCount
1466 policy, // flags
1467 audioTrack_callBack_pullFromBuffQueue, // callback
1468 (void *) pAudioPlayer, // user
1469 0, // FIXME find appropriate frame count // notificationFrame
1470 pAudioPlayer->mSessionId);
1471 android::status_t status = pAudioPlayer->mAudioTrack->initCheck();
1472 if (status != android::NO_ERROR) {
1473 SL_LOGE("AudioTrack::initCheck status %u", status);
1474 result = SL_RESULT_CONTENT_UNSUPPORTED;
1475 pAudioPlayer->mAudioTrack.clear();
1476 return result;
1477 }
1478
1479 // initialize platform-independent CAudioPlayer fields
1480
1481 pAudioPlayer->mNumChannels = df_pcm->numChannels;
1482 pAudioPlayer->mSampleRateMilliHz = df_pcm->samplesPerSec; // Note: bad field name in SL ES
1483
1484 // This use case does not have a separate "prepare" step
1485 pAudioPlayer->mAndroidObjState = ANDROID_READY;
1486 }
1487 break;
1488
1489 //-----------------------------------
1490 // MediaPlayer
1491 case AUDIOPLAYER_FROM_URIFD: {
1492 pAudioPlayer->mAPlayer = new android::LocAVPlayer(&app, false /*hasVideo*/);
1493 pAudioPlayer->mAPlayer->init(sfplayer_handlePrefetchEvent,
1494 (void*)pAudioPlayer /*notifUSer*/);
1495
1496 switch (pAudioPlayer->mDataSource.mLocator.mLocatorType) {
1497 case SL_DATALOCATOR_URI: {
1498 // The legacy implementation ran Stagefright within the application process, and
1499 // so allowed local pathnames specified by URI that were openable by
1500 // the application but were not openable by mediaserver.
1501 // The current implementation runs Stagefright (mostly) within mediaserver,
1502 // which runs as a different UID and likely a different current working directory.
1503 // For backwards compatibility with any applications which may have relied on the
1504 // previous behavior, we convert an openable file URI into an FD.
1505 // Note that unlike SL_DATALOCATOR_ANDROIDFD, this FD is owned by us
1506 // and so we close it as soon as we've passed it (via Binder dup) to mediaserver.
1507 const char *uri = (const char *)pAudioPlayer->mDataSource.mLocator.mURI.URI;
1508 if (!isDistantProtocol(uri)) {
1509 // don't touch the original uri, we may need it later
1510 const char *pathname = uri;
1511 // skip over an optional leading file:// prefix
1512 if (!strncasecmp(pathname, "file://", 7)) {
1513 pathname += 7;
1514 }
1515 // attempt to open it as a file using the application's credentials
1516 int fd = ::open(pathname, O_RDONLY);
1517 if (fd >= 0) {
1518 // if open is successful, then check to see if it's a regular file
1519 struct stat statbuf;
1520 if (!::fstat(fd, &statbuf) && S_ISREG(statbuf.st_mode)) {
1521 // treat similarly to an FD data locator, but
1522 // let setDataSource take responsibility for closing fd
1523 pAudioPlayer->mAPlayer->setDataSource(fd, 0, statbuf.st_size, true);
1524 break;
1525 }
1526 // we were able to open it, but it's not a file, so let mediaserver try
1527 (void) ::close(fd);
1528 }
1529 }
1530 // if either the URI didn't look like a file, or open failed, or not a file
1531 pAudioPlayer->mAPlayer->setDataSource(uri);
1532 } break;
1533 case SL_DATALOCATOR_ANDROIDFD: {
1534 int64_t offset = (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.offset;
1535 pAudioPlayer->mAPlayer->setDataSource(
1536 (int)pAudioPlayer->mDataSource.mLocator.mFD.fd,
1537 offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ?
1538 (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset,
1539 (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.length);
1540 }
1541 break;
1542 default:
1543 SL_LOGE(ERROR_PLAYERREALIZE_UNKNOWN_DATASOURCE_LOCATOR);
1544 break;
1545 }
1546
1547 }
1548 break;
1549
1550 //-----------------------------------
1551 // StreamPlayer
1552 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: {
1553 android::StreamPlayer* splr = new android::StreamPlayer(&app, false /*hasVideo*/,
1554 &pAudioPlayer->mAndroidBufferQueue, pAudioPlayer->mCallbackProtector);
1555 pAudioPlayer->mAPlayer = splr;
1556 splr->init(sfplayer_handlePrefetchEvent, (void*)pAudioPlayer);
1557 }
1558 break;
1559
1560 //-----------------------------------
1561 // AudioToCbRenderer
1562 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: {
1563 android::AudioToCbRenderer* decoder = new android::AudioToCbRenderer(&app);
1564 pAudioPlayer->mAPlayer = decoder;
1565 // configures the callback for the sink buffer queue
1566 decoder->setDataPushListener(adecoder_writeToBufferQueue, pAudioPlayer);
1567 // configures the callback for the notifications coming from the SF code
1568 decoder->init(sfplayer_handlePrefetchEvent, (void*)pAudioPlayer);
1569
1570 switch (pAudioPlayer->mDataSource.mLocator.mLocatorType) {
1571 case SL_DATALOCATOR_URI:
1572 decoder->setDataSource(
1573 (const char*)pAudioPlayer->mDataSource.mLocator.mURI.URI);
1574 break;
1575 case SL_DATALOCATOR_ANDROIDFD: {
1576 int64_t offset = (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.offset;
1577 decoder->setDataSource(
1578 (int)pAudioPlayer->mDataSource.mLocator.mFD.fd,
1579 offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ?
1580 (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset,
1581 (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.length);
1582 }
1583 break;
1584 default:
1585 SL_LOGE(ERROR_PLAYERREALIZE_UNKNOWN_DATASOURCE_LOCATOR);
1586 break;
1587 }
1588
1589 }
1590 break;
1591
1592 //-----------------------------------
1593 // AacBqToPcmCbRenderer
1594 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: {
1595 android::AacBqToPcmCbRenderer* bqtobq = new android::AacBqToPcmCbRenderer(&app,
1596 &pAudioPlayer->mAndroidBufferQueue);
1597 // configures the callback for the sink buffer queue
1598 bqtobq->setDataPushListener(adecoder_writeToBufferQueue, pAudioPlayer);
1599 pAudioPlayer->mAPlayer = bqtobq;
1600 // configures the callback for the notifications coming from the SF code,
1601 // but also implicitly configures the AndroidBufferQueue from which ADTS data is read
1602 pAudioPlayer->mAPlayer->init(sfplayer_handlePrefetchEvent, (void*)pAudioPlayer);
1603 }
1604 break;
1605
1606 //-----------------------------------
1607 default:
1608 SL_LOGE(ERROR_PLAYERREALIZE_UNEXPECTED_OBJECT_TYPE_D, pAudioPlayer->mAndroidObjType);
1609 result = SL_RESULT_INTERNAL_ERROR;
1610 break;
1611 }
1612
1613 // proceed with effect initialization
1614 // initialize EQ
1615 // FIXME use a table of effect descriptors when adding support for more effects
1616 if (memcmp(SL_IID_EQUALIZER, &pAudioPlayer->mEqualizer.mEqDescriptor.type,
1617 sizeof(effect_uuid_t)) == 0) {
1618 SL_LOGV("Need to initialize EQ for AudioPlayer=%p", pAudioPlayer);
1619 android_eq_init(pAudioPlayer->mSessionId, &pAudioPlayer->mEqualizer);
1620 }
1621 // initialize BassBoost
1622 if (memcmp(SL_IID_BASSBOOST, &pAudioPlayer->mBassBoost.mBassBoostDescriptor.type,
1623 sizeof(effect_uuid_t)) == 0) {
1624 SL_LOGV("Need to initialize BassBoost for AudioPlayer=%p", pAudioPlayer);
1625 android_bb_init(pAudioPlayer->mSessionId, &pAudioPlayer->mBassBoost);
1626 }
1627 // initialize Virtualizer
1628 if (memcmp(SL_IID_VIRTUALIZER, &pAudioPlayer->mVirtualizer.mVirtualizerDescriptor.type,
1629 sizeof(effect_uuid_t)) == 0) {
1630 SL_LOGV("Need to initialize Virtualizer for AudioPlayer=%p", pAudioPlayer);
1631 android_virt_init(pAudioPlayer->mSessionId, &pAudioPlayer->mVirtualizer);
1632 }
1633
1634 // initialize EffectSend
1635 // FIXME initialize EffectSend
1636
1637 return result;
1638 }
1639
1640
1641 //-----------------------------------------------------------------------------
1642 /**
1643 * Called with a lock on AudioPlayer, and blocks until safe to destroy
1644 */
android_audioPlayer_preDestroy(CAudioPlayer * pAudioPlayer)1645 SLresult android_audioPlayer_preDestroy(CAudioPlayer *pAudioPlayer) {
1646 SL_LOGD("android_audioPlayer_preDestroy(%p)", pAudioPlayer);
1647 SLresult result = SL_RESULT_SUCCESS;
1648
1649 bool disableCallbacksBeforePreDestroy;
1650 switch (pAudioPlayer->mAndroidObjType) {
1651 // Not yet clear why this order is important, but it reduces detected deadlocks
1652 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1653 disableCallbacksBeforePreDestroy = true;
1654 break;
1655 // Use the old behavior for all other use cases until proven
1656 // case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1657 default:
1658 disableCallbacksBeforePreDestroy = false;
1659 break;
1660 }
1661
1662 if (disableCallbacksBeforePreDestroy) {
1663 object_unlock_exclusive(&pAudioPlayer->mObject);
1664 if (pAudioPlayer->mCallbackProtector != 0) {
1665 pAudioPlayer->mCallbackProtector->requestCbExitAndWait();
1666 }
1667 object_lock_exclusive(&pAudioPlayer->mObject);
1668 }
1669
1670 if (pAudioPlayer->mAPlayer != 0) {
1671 pAudioPlayer->mAPlayer->preDestroy();
1672 }
1673 SL_LOGD("android_audioPlayer_preDestroy(%p) after mAPlayer->preDestroy()", pAudioPlayer);
1674
1675 if (!disableCallbacksBeforePreDestroy) {
1676 object_unlock_exclusive(&pAudioPlayer->mObject);
1677 if (pAudioPlayer->mCallbackProtector != 0) {
1678 pAudioPlayer->mCallbackProtector->requestCbExitAndWait();
1679 }
1680 object_lock_exclusive(&pAudioPlayer->mObject);
1681 }
1682
1683 return result;
1684 }
1685
1686
1687 //-----------------------------------------------------------------------------
android_audioPlayer_destroy(CAudioPlayer * pAudioPlayer)1688 SLresult android_audioPlayer_destroy(CAudioPlayer *pAudioPlayer) {
1689 SLresult result = SL_RESULT_SUCCESS;
1690 SL_LOGV("android_audioPlayer_destroy(%p)", pAudioPlayer);
1691 switch (pAudioPlayer->mAndroidObjType) {
1692
1693 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
1694 // We own the audio track for PCM buffer queue players
1695 if (pAudioPlayer->mAudioTrack != 0) {
1696 pAudioPlayer->mAudioTrack->stop();
1697 // Note that there may still be another reference in post-unlock phase of SetPlayState
1698 pAudioPlayer->mAudioTrack.clear();
1699 }
1700 break;
1701
1702 case AUDIOPLAYER_FROM_URIFD: // intended fall-through
1703 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
1704 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: // intended fall-through
1705 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1706 pAudioPlayer->mAPlayer.clear();
1707 break;
1708 //-----------------------------------
1709 default:
1710 SL_LOGE(ERROR_PLAYERDESTROY_UNEXPECTED_OBJECT_TYPE_D, pAudioPlayer->mAndroidObjType);
1711 result = SL_RESULT_INTERNAL_ERROR;
1712 break;
1713 }
1714
1715 // placeholder: not necessary yet as session ID lifetime doesn't extend beyond player
1716 // android::AudioSystem::releaseAudioSessionId(pAudioPlayer->mSessionId);
1717
1718 pAudioPlayer->mCallbackProtector.clear();
1719
1720 // explicit destructor
1721 pAudioPlayer->mAudioTrack.~sp();
1722 // note that SetPlayState(PLAYING) may still hold a reference
1723 pAudioPlayer->mCallbackProtector.~sp();
1724 pAudioPlayer->mAuxEffect.~sp();
1725 pAudioPlayer->mAPlayer.~sp();
1726
1727 return result;
1728 }
1729
1730
1731 //-----------------------------------------------------------------------------
android_audioPlayer_setPlaybackRateAndConstraints(CAudioPlayer * ap,SLpermille rate,SLuint32 constraints)1732 SLresult android_audioPlayer_setPlaybackRateAndConstraints(CAudioPlayer *ap, SLpermille rate,
1733 SLuint32 constraints) {
1734 SLresult result = SL_RESULT_SUCCESS;
1735 switch(ap->mAndroidObjType) {
1736 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: {
1737 // these asserts were already checked by the platform-independent layer
1738 assert((AUDIOTRACK_MIN_PLAYBACKRATE_PERMILLE <= rate) &&
1739 (rate <= AUDIOTRACK_MAX_PLAYBACKRATE_PERMILLE));
1740 assert(constraints & SL_RATEPROP_NOPITCHCORAUDIO);
1741 // get the content sample rate
1742 uint32_t contentRate = sles_to_android_sampleRate(ap->mSampleRateMilliHz);
1743 // apply the SL ES playback rate on the AudioTrack as a factor of its content sample rate
1744 if (ap->mAudioTrack != 0) {
1745 ap->mAudioTrack->setSampleRate(contentRate * (rate/1000.0f));
1746 }
1747 }
1748 break;
1749 case AUDIOPLAYER_FROM_URIFD: {
1750 assert((MEDIAPLAYER_MIN_PLAYBACKRATE_PERMILLE <= rate) &&
1751 (rate <= MEDIAPLAYER_MAX_PLAYBACKRATE_PERMILLE));
1752 assert(constraints & SL_RATEPROP_NOPITCHCORAUDIO);
1753 // apply the SL ES playback rate on the GenericPlayer
1754 if (ap->mAPlayer != 0) {
1755 ap->mAPlayer->setPlaybackRate((int16_t)rate);
1756 }
1757 }
1758 break;
1759
1760 default:
1761 SL_LOGE("Unexpected object type %d", ap->mAndroidObjType);
1762 result = SL_RESULT_FEATURE_UNSUPPORTED;
1763 break;
1764 }
1765 return result;
1766 }
1767
1768
1769 //-----------------------------------------------------------------------------
1770 // precondition
1771 // called with no lock held
1772 // ap != NULL
1773 // pItemCount != NULL
android_audioPlayer_metadata_getItemCount(CAudioPlayer * ap,SLuint32 * pItemCount)1774 SLresult android_audioPlayer_metadata_getItemCount(CAudioPlayer *ap, SLuint32 *pItemCount) {
1775 if (ap->mAPlayer == 0) {
1776 return SL_RESULT_PARAMETER_INVALID;
1777 }
1778 switch(ap->mAndroidObjType) {
1779 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1780 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1781 {
1782 android::AudioSfDecoder* decoder =
1783 static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get());
1784 *pItemCount = decoder->getPcmFormatKeyCount();
1785 }
1786 break;
1787 default:
1788 *pItemCount = 0;
1789 break;
1790 }
1791 return SL_RESULT_SUCCESS;
1792 }
1793
1794
1795 //-----------------------------------------------------------------------------
1796 // precondition
1797 // called with no lock held
1798 // ap != NULL
1799 // pKeySize != NULL
android_audioPlayer_metadata_getKeySize(CAudioPlayer * ap,SLuint32 index,SLuint32 * pKeySize)1800 SLresult android_audioPlayer_metadata_getKeySize(CAudioPlayer *ap,
1801 SLuint32 index, SLuint32 *pKeySize) {
1802 if (ap->mAPlayer == 0) {
1803 return SL_RESULT_PARAMETER_INVALID;
1804 }
1805 SLresult res = SL_RESULT_SUCCESS;
1806 switch(ap->mAndroidObjType) {
1807 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1808 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1809 {
1810 android::AudioSfDecoder* decoder =
1811 static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get());
1812 SLuint32 keyNameSize = 0;
1813 if (!decoder->getPcmFormatKeySize(index, &keyNameSize)) {
1814 res = SL_RESULT_PARAMETER_INVALID;
1815 } else {
1816 // *pKeySize is the size of the region used to store the key name AND
1817 // the information about the key (size, lang, encoding)
1818 *pKeySize = keyNameSize + sizeof(SLMetadataInfo);
1819 }
1820 }
1821 break;
1822 default:
1823 *pKeySize = 0;
1824 res = SL_RESULT_PARAMETER_INVALID;
1825 break;
1826 }
1827 return res;
1828 }
1829
1830
1831 //-----------------------------------------------------------------------------
1832 // precondition
1833 // called with no lock held
1834 // ap != NULL
1835 // pKey != NULL
android_audioPlayer_metadata_getKey(CAudioPlayer * ap,SLuint32 index,SLuint32 size,SLMetadataInfo * pKey)1836 SLresult android_audioPlayer_metadata_getKey(CAudioPlayer *ap,
1837 SLuint32 index, SLuint32 size, SLMetadataInfo *pKey) {
1838 if (ap->mAPlayer == 0) {
1839 return SL_RESULT_PARAMETER_INVALID;
1840 }
1841 SLresult res = SL_RESULT_SUCCESS;
1842 switch(ap->mAndroidObjType) {
1843 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1844 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1845 {
1846 android::AudioSfDecoder* decoder =
1847 static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get());
1848 if ((size < sizeof(SLMetadataInfo) ||
1849 (!decoder->getPcmFormatKeyName(index, size - sizeof(SLMetadataInfo),
1850 (char*)pKey->data)))) {
1851 res = SL_RESULT_PARAMETER_INVALID;
1852 } else {
1853 // successfully retrieved the key value, update the other fields
1854 pKey->encoding = SL_CHARACTERENCODING_UTF8;
1855 memcpy((char *) pKey->langCountry, "en", 3);
1856 pKey->size = strlen((char*)pKey->data) + 1;
1857 }
1858 }
1859 break;
1860 default:
1861 res = SL_RESULT_PARAMETER_INVALID;
1862 break;
1863 }
1864 return res;
1865 }
1866
1867
1868 //-----------------------------------------------------------------------------
1869 // precondition
1870 // called with no lock held
1871 // ap != NULL
1872 // pValueSize != NULL
android_audioPlayer_metadata_getValueSize(CAudioPlayer * ap,SLuint32 index,SLuint32 * pValueSize)1873 SLresult android_audioPlayer_metadata_getValueSize(CAudioPlayer *ap,
1874 SLuint32 index, SLuint32 *pValueSize) {
1875 if (ap->mAPlayer == 0) {
1876 return SL_RESULT_PARAMETER_INVALID;
1877 }
1878 SLresult res = SL_RESULT_SUCCESS;
1879 switch(ap->mAndroidObjType) {
1880 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1881 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1882 {
1883 android::AudioSfDecoder* decoder =
1884 static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get());
1885 SLuint32 valueSize = 0;
1886 if (!decoder->getPcmFormatValueSize(index, &valueSize)) {
1887 res = SL_RESULT_PARAMETER_INVALID;
1888 } else {
1889 // *pValueSize is the size of the region used to store the key value AND
1890 // the information about the value (size, lang, encoding)
1891 *pValueSize = valueSize + sizeof(SLMetadataInfo);
1892 }
1893 }
1894 break;
1895 default:
1896 *pValueSize = 0;
1897 res = SL_RESULT_PARAMETER_INVALID;
1898 break;
1899 }
1900 return res;
1901 }
1902
1903
1904 //-----------------------------------------------------------------------------
1905 // precondition
1906 // called with no lock held
1907 // ap != NULL
1908 // pValue != NULL
android_audioPlayer_metadata_getValue(CAudioPlayer * ap,SLuint32 index,SLuint32 size,SLMetadataInfo * pValue)1909 SLresult android_audioPlayer_metadata_getValue(CAudioPlayer *ap,
1910 SLuint32 index, SLuint32 size, SLMetadataInfo *pValue) {
1911 if (ap->mAPlayer == 0) {
1912 return SL_RESULT_PARAMETER_INVALID;
1913 }
1914 SLresult res = SL_RESULT_SUCCESS;
1915 switch(ap->mAndroidObjType) {
1916 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1917 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1918 {
1919 android::AudioSfDecoder* decoder =
1920 static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get());
1921 pValue->encoding = SL_CHARACTERENCODING_BINARY;
1922 memcpy((char *) pValue->langCountry, "en", 3); // applicable here?
1923 SLuint32 valueSize = 0;
1924 if ((size < sizeof(SLMetadataInfo)
1925 || (!decoder->getPcmFormatValueSize(index, &valueSize))
1926 || (!decoder->getPcmFormatKeyValue(index, size - sizeof(SLMetadataInfo),
1927 (SLuint32*)pValue->data)))) {
1928 res = SL_RESULT_PARAMETER_INVALID;
1929 } else {
1930 pValue->size = valueSize;
1931 }
1932 }
1933 break;
1934 default:
1935 res = SL_RESULT_PARAMETER_INVALID;
1936 break;
1937 }
1938 return res;
1939 }
1940
1941 //-----------------------------------------------------------------------------
1942 // preconditions
1943 // ap != NULL
1944 // mutex is locked
1945 // play state has changed
android_audioPlayer_setPlayState(CAudioPlayer * ap)1946 void android_audioPlayer_setPlayState(CAudioPlayer *ap) {
1947
1948 SLuint32 playState = ap->mPlay.mState;
1949 AndroidObjectState objState = ap->mAndroidObjState;
1950
1951 switch(ap->mAndroidObjType) {
1952 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
1953 switch (playState) {
1954 case SL_PLAYSTATE_STOPPED:
1955 SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_STOPPED");
1956 if (ap->mAudioTrack != 0) {
1957 ap->mAudioTrack->stop();
1958 }
1959 break;
1960 case SL_PLAYSTATE_PAUSED:
1961 SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PAUSED");
1962 if (ap->mAudioTrack != 0) {
1963 ap->mAudioTrack->pause();
1964 }
1965 break;
1966 case SL_PLAYSTATE_PLAYING:
1967 SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PLAYING");
1968 if (ap->mAudioTrack != 0) {
1969 // instead of ap->mAudioTrack->start();
1970 ap->mDeferredStart = true;
1971 }
1972 break;
1973 default:
1974 // checked by caller, should not happen
1975 break;
1976 }
1977 break;
1978
1979 case AUDIOPLAYER_FROM_URIFD: // intended fall-through
1980 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
1981 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: // intended fall-through
1982 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1983 // FIXME report and use the return code to the lock mechanism, which is where play state
1984 // changes are updated (see object_unlock_exclusive_attributes())
1985 aplayer_setPlayState(ap->mAPlayer, playState, &(ap->mAndroidObjState));
1986 break;
1987 default:
1988 SL_LOGE(ERROR_PLAYERSETPLAYSTATE_UNEXPECTED_OBJECT_TYPE_D, ap->mAndroidObjType);
1989 break;
1990 }
1991 }
1992
1993
1994 //-----------------------------------------------------------------------------
1995 // call when either player event flags, marker position, or position update period changes
android_audioPlayer_usePlayEventMask(CAudioPlayer * ap)1996 void android_audioPlayer_usePlayEventMask(CAudioPlayer *ap) {
1997 IPlay *pPlayItf = &ap->mPlay;
1998 SLuint32 eventFlags = pPlayItf->mEventFlags;
1999 /*switch(ap->mAndroidObjType) {
2000 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:*/
2001
2002 if (ap->mAPlayer != 0) {
2003 assert(ap->mAudioTrack == 0);
2004 ap->mAPlayer->setPlayEvents((int32_t) eventFlags, (int32_t) pPlayItf->mMarkerPosition,
2005 (int32_t) pPlayItf->mPositionUpdatePeriod);
2006 return;
2007 }
2008
2009 if (ap->mAudioTrack == 0) {
2010 return;
2011 }
2012
2013 if (eventFlags & SL_PLAYEVENT_HEADATMARKER) {
2014 ap->mAudioTrack->setMarkerPosition((uint32_t)((((int64_t)pPlayItf->mMarkerPosition
2015 * sles_to_android_sampleRate(ap->mSampleRateMilliHz)))/1000));
2016 } else {
2017 // clear marker
2018 ap->mAudioTrack->setMarkerPosition(0);
2019 }
2020
2021 if (eventFlags & SL_PLAYEVENT_HEADATNEWPOS) {
2022 ap->mAudioTrack->setPositionUpdatePeriod(
2023 (uint32_t)((((int64_t)pPlayItf->mPositionUpdatePeriod
2024 * sles_to_android_sampleRate(ap->mSampleRateMilliHz)))/1000));
2025 } else {
2026 // clear periodic update
2027 ap->mAudioTrack->setPositionUpdatePeriod(0);
2028 }
2029
2030 if (eventFlags & SL_PLAYEVENT_HEADATEND) {
2031 // nothing to do for SL_PLAYEVENT_HEADATEND, callback event will be checked against mask
2032 }
2033
2034 if (eventFlags & SL_PLAYEVENT_HEADMOVING) {
2035 // FIXME support SL_PLAYEVENT_HEADMOVING
2036 SL_LOGD("[ FIXME: IPlay_SetCallbackEventsMask(SL_PLAYEVENT_HEADMOVING) on an "
2037 "SL_OBJECTID_AUDIOPLAYER to be implemented ]");
2038 }
2039 if (eventFlags & SL_PLAYEVENT_HEADSTALLED) {
2040 // nothing to do for SL_PLAYEVENT_HEADSTALLED, callback event will be checked against mask
2041 }
2042
2043 }
2044
2045
2046 //-----------------------------------------------------------------------------
android_audioPlayer_getDuration(IPlay * pPlayItf,SLmillisecond * pDurMsec)2047 SLresult android_audioPlayer_getDuration(IPlay *pPlayItf, SLmillisecond *pDurMsec) {
2048 CAudioPlayer *ap = (CAudioPlayer *)pPlayItf->mThis;
2049 switch(ap->mAndroidObjType) {
2050
2051 case AUDIOPLAYER_FROM_URIFD: // intended fall-through
2052 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: {
2053 int32_t durationMsec = ANDROID_UNKNOWN_TIME;
2054 if (ap->mAPlayer != 0) {
2055 ap->mAPlayer->getDurationMsec(&durationMsec);
2056 }
2057 *pDurMsec = durationMsec == ANDROID_UNKNOWN_TIME ? SL_TIME_UNKNOWN : durationMsec;
2058 break;
2059 }
2060
2061 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
2062 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
2063 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
2064 default: {
2065 *pDurMsec = SL_TIME_UNKNOWN;
2066 }
2067 }
2068 return SL_RESULT_SUCCESS;
2069 }
2070
2071
2072 //-----------------------------------------------------------------------------
android_audioPlayer_getPosition(IPlay * pPlayItf,SLmillisecond * pPosMsec)2073 void android_audioPlayer_getPosition(IPlay *pPlayItf, SLmillisecond *pPosMsec) {
2074 CAudioPlayer *ap = (CAudioPlayer *)pPlayItf->mThis;
2075 switch(ap->mAndroidObjType) {
2076
2077 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
2078 if ((ap->mSampleRateMilliHz == UNKNOWN_SAMPLERATE) || (ap->mAudioTrack == 0)) {
2079 *pPosMsec = 0;
2080 } else {
2081 uint32_t positionInFrames;
2082 ap->mAudioTrack->getPosition(&positionInFrames);
2083 *pPosMsec = ((int64_t)positionInFrames * 1000) /
2084 sles_to_android_sampleRate(ap->mSampleRateMilliHz);
2085 }
2086 break;
2087
2088 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
2089 case AUDIOPLAYER_FROM_URIFD:
2090 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
2091 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: {
2092 int32_t posMsec = ANDROID_UNKNOWN_TIME;
2093 if (ap->mAPlayer != 0) {
2094 ap->mAPlayer->getPositionMsec(&posMsec);
2095 }
2096 *pPosMsec = posMsec == ANDROID_UNKNOWN_TIME ? 0 : posMsec;
2097 break;
2098 }
2099
2100 default:
2101 *pPosMsec = 0;
2102 }
2103 }
2104
2105
2106 //-----------------------------------------------------------------------------
android_audioPlayer_seek(CAudioPlayer * ap,SLmillisecond posMsec)2107 SLresult android_audioPlayer_seek(CAudioPlayer *ap, SLmillisecond posMsec) {
2108 SLresult result = SL_RESULT_SUCCESS;
2109
2110 switch(ap->mAndroidObjType) {
2111
2112 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: // intended fall-through
2113 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:
2114 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
2115 result = SL_RESULT_FEATURE_UNSUPPORTED;
2116 break;
2117
2118 case AUDIOPLAYER_FROM_URIFD: // intended fall-through
2119 case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
2120 if (ap->mAPlayer != 0) {
2121 ap->mAPlayer->seek(posMsec);
2122 }
2123 break;
2124
2125 default:
2126 break;
2127 }
2128 return result;
2129 }
2130
2131
2132 //-----------------------------------------------------------------------------
android_audioPlayer_loop(CAudioPlayer * ap,SLboolean loopEnable)2133 SLresult android_audioPlayer_loop(CAudioPlayer *ap, SLboolean loopEnable) {
2134 SLresult result = SL_RESULT_SUCCESS;
2135
2136 switch (ap->mAndroidObjType) {
2137 case AUDIOPLAYER_FROM_URIFD:
2138 // case AUDIOPLAY_FROM_URIFD_TO_PCM_BUFFERQUEUE:
2139 // would actually work, but what's the point?
2140 if (ap->mAPlayer != 0) {
2141 ap->mAPlayer->loop((bool)loopEnable);
2142 }
2143 break;
2144 default:
2145 result = SL_RESULT_FEATURE_UNSUPPORTED;
2146 break;
2147 }
2148 return result;
2149 }
2150
2151
2152 //-----------------------------------------------------------------------------
android_audioPlayer_setBufferingUpdateThresholdPerMille(CAudioPlayer * ap,SLpermille threshold)2153 SLresult android_audioPlayer_setBufferingUpdateThresholdPerMille(CAudioPlayer *ap,
2154 SLpermille threshold) {
2155 SLresult result = SL_RESULT_SUCCESS;
2156
2157 switch (ap->mAndroidObjType) {
2158 case AUDIOPLAYER_FROM_URIFD:
2159 if (ap->mAPlayer != 0) {
2160 ap->mAPlayer->setBufferingUpdateThreshold(threshold / 10);
2161 }
2162 break;
2163
2164 default: {}
2165 }
2166
2167 return result;
2168 }
2169
2170
2171 //-----------------------------------------------------------------------------
android_audioPlayer_bufferQueue_onRefilled_l(CAudioPlayer * ap)2172 void android_audioPlayer_bufferQueue_onRefilled_l(CAudioPlayer *ap) {
2173 // the AudioTrack associated with the AudioPlayer receiving audio from a PCM buffer
2174 // queue was stopped when the queue become empty, we restart as soon as a new buffer
2175 // has been enqueued since we're in playing state
2176 if (ap->mAudioTrack != 0) {
2177 // instead of ap->mAudioTrack->start();
2178 ap->mDeferredStart = true;
2179 }
2180
2181 // when the queue became empty, an underflow on the prefetch status itf was sent. Now the queue
2182 // has received new data, signal it has sufficient data
2183 if (IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) {
2184 // we wouldn't have been called unless we were previously in the underflow state
2185 assert(SL_PREFETCHSTATUS_UNDERFLOW == ap->mPrefetchStatus.mStatus);
2186 assert(0 == ap->mPrefetchStatus.mLevel);
2187 ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_SUFFICIENTDATA;
2188 ap->mPrefetchStatus.mLevel = 1000;
2189 // callback or no callback?
2190 SLuint32 prefetchEvents = ap->mPrefetchStatus.mCallbackEventsMask &
2191 (SL_PREFETCHEVENT_STATUSCHANGE | SL_PREFETCHEVENT_FILLLEVELCHANGE);
2192 if (SL_PREFETCHEVENT_NONE != prefetchEvents) {
2193 ap->mPrefetchStatus.mDeferredPrefetchCallback = ap->mPrefetchStatus.mCallback;
2194 ap->mPrefetchStatus.mDeferredPrefetchContext = ap->mPrefetchStatus.mContext;
2195 ap->mPrefetchStatus.mDeferredPrefetchEvents = prefetchEvents;
2196 }
2197 }
2198 }
2199
2200
2201 //-----------------------------------------------------------------------------
2202 /*
2203 * BufferQueue::Clear
2204 */
android_audioPlayer_bufferQueue_onClear(CAudioPlayer * ap)2205 SLresult android_audioPlayer_bufferQueue_onClear(CAudioPlayer *ap) {
2206 SLresult result = SL_RESULT_SUCCESS;
2207
2208 switch (ap->mAndroidObjType) {
2209 //-----------------------------------
2210 // AudioTrack
2211 case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
2212 if (ap->mAudioTrack != 0) {
2213 ap->mAudioTrack->flush();
2214 }
2215 break;
2216 default:
2217 result = SL_RESULT_INTERNAL_ERROR;
2218 break;
2219 }
2220
2221 return result;
2222 }
2223
2224
2225 //-----------------------------------------------------------------------------
android_audioPlayer_androidBufferQueue_clear_l(CAudioPlayer * ap)2226 void android_audioPlayer_androidBufferQueue_clear_l(CAudioPlayer *ap) {
2227 switch (ap->mAndroidObjType) {
2228 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:
2229 if (ap->mAPlayer != 0) {
2230 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(ap->mAPlayer.get());
2231 splr->appClear_l();
2232 } break;
2233 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
2234 // nothing to do here, fall through
2235 default:
2236 break;
2237 }
2238 }
2239
android_audioPlayer_androidBufferQueue_onRefilled_l(CAudioPlayer * ap)2240 void android_audioPlayer_androidBufferQueue_onRefilled_l(CAudioPlayer *ap) {
2241 switch (ap->mAndroidObjType) {
2242 case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:
2243 if (ap->mAPlayer != 0) {
2244 android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(ap->mAPlayer.get());
2245 splr->queueRefilled();
2246 } break;
2247 case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
2248 // FIXME this may require waking up the decoder if it is currently starved and isn't polling
2249 default:
2250 break;
2251 }
2252 }
2253