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