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