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