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