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