AudioPlayer_to_android.cpp revision 213c31153b9bda7b5a091f2996da7b655c8ed451
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
947    const SLuint32 *df_representation = NULL; // pointer to representation field, if it exists
948
949    switch (sourceLocatorType) {
950    //------------------
951    //   Buffer Queues
952    case SL_DATALOCATOR_BUFFERQUEUE:
953    case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE:
954        {
955        // Buffer format
956        switch (sourceFormatType) {
957        //     currently only PCM buffer queues are supported,
958        case SL_ANDROID_DATAFORMAT_PCM_EX: {
959            const SLAndroidDataFormat_PCM_EX *df_pcm =
960                    (const SLAndroidDataFormat_PCM_EX *) pAudioSrc->pFormat;
961            // checkDataFormat() already checked representation
962            df_representation = &df_pcm->representation;
963            } // SL_ANDROID_DATAFORMAT_PCM_EX - fall through to next test.
964        case SL_DATAFORMAT_PCM: {
965            // checkDataFormat() already did generic checks, now do the Android-specific checks
966            const SLDataFormat_PCM *df_pcm = (const SLDataFormat_PCM *) pAudioSrc->pFormat;
967            SLresult result = android_audioPlayer_validateChannelMask(df_pcm->channelMask,
968                                                                      df_pcm->numChannels);
969            if (result != SL_RESULT_SUCCESS) {
970                SL_LOGE("Cannot create audio player: unsupported PCM data source with %u channels",
971                        (unsigned) df_pcm->numChannels);
972                return result;
973            }
974
975            // checkDataFormat() already checked sample rate
976
977            // checkDataFormat() already checked bits per sample, container size, and representation
978
979            // FIXME confirm the following
980            // df_pcm->channelMask: the earlier platform-independent check and the
981            //     upcoming check by sles_to_android_channelMaskOut are sufficient
982
983            if (df_pcm->endianness != pAudioPlayer->mObject.mEngine->mEngine.mNativeEndianness) {
984                SL_LOGE("Cannot create audio player: unsupported byte order %u",
985                        df_pcm->endianness);
986                return SL_RESULT_CONTENT_UNSUPPORTED;
987            }
988
989            // we don't support container size != sample depth
990            if (df_pcm->containerSize != df_pcm->bitsPerSample) {
991                SL_LOGE("Cannot create audio player: unsupported container size %u bits for "
992                        "sample depth %u bits",
993                        df_pcm->containerSize, (SLuint32)df_pcm->bitsPerSample);
994                return SL_RESULT_CONTENT_UNSUPPORTED;
995            }
996
997            } //case SL_DATAFORMAT_PCM
998            break;
999        case SL_DATAFORMAT_MIME:
1000        case XA_DATAFORMAT_RAWIMAGE:
1001            SL_LOGE("Cannot create audio player with buffer queue data source "
1002                "without SL_DATAFORMAT_PCM format");
1003            return SL_RESULT_CONTENT_UNSUPPORTED;
1004        default:
1005            // invalid data format is detected earlier
1006            assert(false);
1007            return SL_RESULT_INTERNAL_ERROR;
1008        } // switch (sourceFormatType)
1009        } // case SL_DATALOCATOR_BUFFERQUEUE or SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE
1010        break;
1011    //------------------
1012    //   URI
1013    case SL_DATALOCATOR_URI:
1014        {
1015        SLDataLocator_URI *dl_uri = (SLDataLocator_URI *) pAudioSrc->pLocator;
1016        if (NULL == dl_uri->URI) {
1017            return SL_RESULT_PARAMETER_INVALID;
1018        }
1019        // URI format
1020        switch (sourceFormatType) {
1021        case SL_DATAFORMAT_MIME:
1022            break;
1023        default:
1024            SL_LOGE("Cannot create audio player with SL_DATALOCATOR_URI data source without "
1025                "SL_DATAFORMAT_MIME format");
1026            return SL_RESULT_CONTENT_UNSUPPORTED;
1027        } // switch (sourceFormatType)
1028        // decoding format check
1029        if ((sinkLocatorType != SL_DATALOCATOR_OUTPUTMIX) &&
1030                !audioPlayer_isSupportedNonOutputMixSink(pAudioSnk)) {
1031            return SL_RESULT_CONTENT_UNSUPPORTED;
1032        }
1033        } // case SL_DATALOCATOR_URI
1034        break;
1035    //------------------
1036    //   File Descriptor
1037    case SL_DATALOCATOR_ANDROIDFD:
1038        {
1039        // fd is already non null
1040        switch (sourceFormatType) {
1041        case SL_DATAFORMAT_MIME:
1042            break;
1043        default:
1044            SL_LOGE("Cannot create audio player with SL_DATALOCATOR_ANDROIDFD data source "
1045                "without SL_DATAFORMAT_MIME format");
1046            return SL_RESULT_CONTENT_UNSUPPORTED;
1047        } // switch (sourceFormatType)
1048        if ((sinkLocatorType != SL_DATALOCATOR_OUTPUTMIX) &&
1049                !audioPlayer_isSupportedNonOutputMixSink(pAudioSnk)) {
1050            return SL_RESULT_CONTENT_UNSUPPORTED;
1051        }
1052        } // case SL_DATALOCATOR_ANDROIDFD
1053        break;
1054    //------------------
1055    //   Stream
1056    case SL_DATALOCATOR_ANDROIDBUFFERQUEUE:
1057    {
1058        switch (sourceFormatType) {
1059        case SL_DATAFORMAT_MIME:
1060        {
1061            SLDataFormat_MIME *df_mime = (SLDataFormat_MIME *) pAudioSrc->pFormat;
1062            if (NULL == df_mime) {
1063                SL_LOGE("MIME type null invalid");
1064                return SL_RESULT_CONTENT_UNSUPPORTED;
1065            }
1066            SL_LOGD("source MIME is %s", (char*)df_mime->mimeType);
1067            switch (df_mime->containerType) {
1068            case SL_CONTAINERTYPE_MPEG_TS:
1069                if (strcasecmp((char*)df_mime->mimeType, (const char *)XA_ANDROID_MIME_MP2TS)) {
1070                    SL_LOGE("Invalid MIME (%s) for container SL_CONTAINERTYPE_MPEG_TS, expects %s",
1071                            (char*)df_mime->mimeType, XA_ANDROID_MIME_MP2TS);
1072                    return SL_RESULT_CONTENT_UNSUPPORTED;
1073                }
1074                if (pAudioPlayer->mAndroidObjType != AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE) {
1075                    SL_LOGE("Invalid sink for container SL_CONTAINERTYPE_MPEG_TS");
1076                    return SL_RESULT_PARAMETER_INVALID;
1077                }
1078                break;
1079            case SL_CONTAINERTYPE_RAW:
1080            case SL_CONTAINERTYPE_AAC:
1081                if (strcasecmp((char*)df_mime->mimeType, (const char *)SL_ANDROID_MIME_AACADTS) &&
1082                        strcasecmp((char*)df_mime->mimeType,
1083                                ANDROID_MIME_AACADTS_ANDROID_FRAMEWORK)) {
1084                    SL_LOGE("Invalid MIME (%s) for container type %d, expects %s",
1085                            (char*)df_mime->mimeType, df_mime->containerType,
1086                            SL_ANDROID_MIME_AACADTS);
1087                    return SL_RESULT_CONTENT_UNSUPPORTED;
1088                }
1089                if (pAudioPlayer->mAndroidObjType != AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE) {
1090                    SL_LOGE("Invalid sink for container SL_CONTAINERTYPE_AAC");
1091                    return SL_RESULT_PARAMETER_INVALID;
1092                }
1093                break;
1094            default:
1095                SL_LOGE("Cannot create player with SL_DATALOCATOR_ANDROIDBUFFERQUEUE data source "
1096                                        "that is not fed MPEG-2 TS data or AAC ADTS data");
1097                return SL_RESULT_CONTENT_UNSUPPORTED;
1098            }
1099        }
1100        break;
1101        default:
1102            SL_LOGE("Cannot create player with SL_DATALOCATOR_ANDROIDBUFFERQUEUE data source "
1103                    "without SL_DATAFORMAT_MIME format");
1104            return SL_RESULT_CONTENT_UNSUPPORTED;
1105        }
1106    }
1107    break; // case SL_DATALOCATOR_ANDROIDBUFFERQUEUE
1108    //------------------
1109    //   Address
1110    case SL_DATALOCATOR_ADDRESS:
1111    case SL_DATALOCATOR_IODEVICE:
1112    case SL_DATALOCATOR_OUTPUTMIX:
1113    case XA_DATALOCATOR_NATIVEDISPLAY:
1114    case SL_DATALOCATOR_MIDIBUFFERQUEUE:
1115        SL_LOGE("Cannot create audio player with data locator type 0x%x",
1116                (unsigned) sourceLocatorType);
1117        return SL_RESULT_CONTENT_UNSUPPORTED;
1118    default:
1119        SL_LOGE("Cannot create audio player with invalid data locator type 0x%x",
1120                (unsigned) sourceLocatorType);
1121        return SL_RESULT_PARAMETER_INVALID;
1122    }// switch (locatorType)
1123
1124    return SL_RESULT_SUCCESS;
1125}
1126
1127
1128//-----------------------------------------------------------------------------
1129// Callback associated with an AudioTrack of an SL ES AudioPlayer that gets its data
1130// from a buffer queue. This will not be called once the AudioTrack has been destroyed.
1131static void audioTrack_callBack_pullFromBuffQueue(int event, void* user, void *info) {
1132    CAudioPlayer *ap = (CAudioPlayer *)user;
1133
1134    if (!android::CallbackProtector::enterCbIfOk(ap->mCallbackProtector)) {
1135        // it is not safe to enter the callback (the track is about to go away)
1136        return;
1137    }
1138
1139    void * callbackPContext = NULL;
1140    switch (event) {
1141
1142    case android::AudioTrack::EVENT_MORE_DATA: {
1143        //SL_LOGV("received event EVENT_MORE_DATA from AudioTrack TID=%d", gettid());
1144        slPrefetchCallback prefetchCallback = NULL;
1145        void *prefetchContext = NULL;
1146        SLuint32 prefetchEvents = SL_PREFETCHEVENT_NONE;
1147        android::AudioTrack::Buffer* pBuff = (android::AudioTrack::Buffer*)info;
1148
1149        // retrieve data from the buffer queue
1150        interface_lock_exclusive(&ap->mBufferQueue);
1151
1152        if (ap->mBufferQueue.mCallbackPending) {
1153            // call callback with lock not held
1154            slBufferQueueCallback callback = ap->mBufferQueue.mCallback;
1155            if (NULL != callback) {
1156                callbackPContext = ap->mBufferQueue.mContext;
1157                interface_unlock_exclusive(&ap->mBufferQueue);
1158                (*callback)(&ap->mBufferQueue.mItf, callbackPContext);
1159                interface_lock_exclusive(&ap->mBufferQueue);
1160                ap->mBufferQueue.mCallbackPending = false;
1161            }
1162        }
1163
1164        if (ap->mBufferQueue.mState.count != 0) {
1165            //SL_LOGV("nbBuffers in queue = %u",ap->mBufferQueue.mState.count);
1166            assert(ap->mBufferQueue.mFront != ap->mBufferQueue.mRear);
1167
1168            BufferHeader *oldFront = ap->mBufferQueue.mFront;
1169            BufferHeader *newFront = &oldFront[1];
1170
1171            size_t availSource = oldFront->mSize - ap->mBufferQueue.mSizeConsumed;
1172            size_t availSink = pBuff->size;
1173            size_t bytesToCopy = availSource < availSink ? availSource : availSink;
1174            void *pSrc = (char *)oldFront->mBuffer + ap->mBufferQueue.mSizeConsumed;
1175            memcpy(pBuff->raw, pSrc, bytesToCopy);
1176
1177            if (bytesToCopy < availSource) {
1178                ap->mBufferQueue.mSizeConsumed += bytesToCopy;
1179                // pBuff->size is already equal to bytesToCopy in this case
1180            } else {
1181                // consumed an entire buffer, dequeue
1182                pBuff->size = bytesToCopy;
1183                ap->mBufferQueue.mSizeConsumed = 0;
1184                if (newFront ==
1185                        &ap->mBufferQueue.mArray
1186                            [ap->mBufferQueue.mNumBuffers + 1])
1187                {
1188                    newFront = ap->mBufferQueue.mArray;
1189                }
1190                ap->mBufferQueue.mFront = newFront;
1191
1192                ap->mBufferQueue.mState.count--;
1193                ap->mBufferQueue.mState.playIndex++;
1194                ap->mBufferQueue.mCallbackPending = true;
1195            }
1196        } else { // empty queue
1197            // signal no data available
1198            pBuff->size = 0;
1199
1200            // signal we're at the end of the content, but don't pause (see note in function)
1201            audioPlayer_dispatch_headAtEnd_lockPlay(ap, false /*set state to paused?*/, false);
1202
1203            // signal underflow to prefetch status itf
1204            if (IsInterfaceInitialized(&ap->mObject, MPH_PREFETCHSTATUS)) {
1205                ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW;
1206                ap->mPrefetchStatus.mLevel = 0;
1207                // callback or no callback?
1208                prefetchEvents = ap->mPrefetchStatus.mCallbackEventsMask &
1209                        (SL_PREFETCHEVENT_STATUSCHANGE | SL_PREFETCHEVENT_FILLLEVELCHANGE);
1210                if (SL_PREFETCHEVENT_NONE != prefetchEvents) {
1211                    prefetchCallback = ap->mPrefetchStatus.mCallback;
1212                    prefetchContext  = ap->mPrefetchStatus.mContext;
1213                }
1214            }
1215
1216            // stop the track so it restarts playing faster when new data is enqueued
1217            ap->mAudioTrack->stop();
1218        }
1219        interface_unlock_exclusive(&ap->mBufferQueue);
1220
1221        // notify client
1222        if (NULL != prefetchCallback) {
1223            assert(SL_PREFETCHEVENT_NONE != prefetchEvents);
1224            // spec requires separate callbacks for each event
1225            if (prefetchEvents & SL_PREFETCHEVENT_STATUSCHANGE) {
1226                (*prefetchCallback)(&ap->mPrefetchStatus.mItf, prefetchContext,
1227                        SL_PREFETCHEVENT_STATUSCHANGE);
1228            }
1229            if (prefetchEvents & SL_PREFETCHEVENT_FILLLEVELCHANGE) {
1230                (*prefetchCallback)(&ap->mPrefetchStatus.mItf, prefetchContext,
1231                        SL_PREFETCHEVENT_FILLLEVELCHANGE);
1232            }
1233        }
1234    }
1235    break;
1236
1237    case android::AudioTrack::EVENT_MARKER:
1238        //SL_LOGI("received event EVENT_MARKER from AudioTrack");
1239        audioTrack_handleMarker_lockPlay(ap);
1240        break;
1241
1242    case android::AudioTrack::EVENT_NEW_POS:
1243        //SL_LOGI("received event EVENT_NEW_POS from AudioTrack");
1244        audioTrack_handleNewPos_lockPlay(ap);
1245        break;
1246
1247    case android::AudioTrack::EVENT_UNDERRUN:
1248        //SL_LOGI("received event EVENT_UNDERRUN from AudioTrack");
1249        audioTrack_handleUnderrun_lockPlay(ap);
1250        break;
1251
1252    case android::AudioTrack::EVENT_NEW_IAUDIOTRACK:
1253        // ignore for now
1254        break;
1255
1256    case android::AudioTrack::EVENT_BUFFER_END:
1257    case android::AudioTrack::EVENT_LOOP_END:
1258    case android::AudioTrack::EVENT_STREAM_END:
1259        // These are unexpected so fall through
1260    default:
1261        // FIXME where does the notification of SL_PLAYEVENT_HEADMOVING fit?
1262        SL_LOGE("Encountered unknown AudioTrack event %d for CAudioPlayer %p", event,
1263                (CAudioPlayer *)user);
1264        break;
1265    }
1266
1267    ap->mCallbackProtector->exitCb();
1268}
1269
1270
1271//-----------------------------------------------------------------------------
1272void android_audioPlayer_create(CAudioPlayer *pAudioPlayer) {
1273
1274    // pAudioPlayer->mAndroidObjType has been set in android_audioPlayer_checkSourceSink()
1275    // and if it was == INVALID_TYPE, then IEngine_CreateAudioPlayer would never call us
1276    assert(INVALID_TYPE != pAudioPlayer->mAndroidObjType);
1277
1278    // These initializations are in the same order as the field declarations in classes.h
1279
1280    // FIXME Consolidate initializations (many of these already in IEngine_CreateAudioPlayer)
1281    // mAndroidObjType: see above comment
1282    pAudioPlayer->mAndroidObjState = ANDROID_UNINITIALIZED;
1283    pAudioPlayer->mSessionId = (audio_session_t) android::AudioSystem::newAudioUniqueId(
1284            AUDIO_UNIQUE_ID_USE_SESSION);
1285
1286    // placeholder: not necessary yet as session ID lifetime doesn't extend beyond player
1287    // android::AudioSystem::acquireAudioSessionId(pAudioPlayer->mSessionId);
1288
1289    pAudioPlayer->mStreamType = ANDROID_DEFAULT_OUTPUT_STREAM_TYPE;
1290
1291    // mAudioTrack
1292    pAudioPlayer->mCallbackProtector = new android::CallbackProtector();
1293    // mAPLayer
1294    // mAuxEffect
1295
1296    pAudioPlayer->mAuxSendLevel = 0;
1297    pAudioPlayer->mAmplFromDirectLevel = 1.0f; // matches initial mDirectLevel value
1298    pAudioPlayer->mDeferredStart = false;
1299
1300    // This section re-initializes interface-specific fields that
1301    // can be set or used regardless of whether the interface is
1302    // exposed on the AudioPlayer or not
1303
1304    switch (pAudioPlayer->mAndroidObjType) {
1305    case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
1306        pAudioPlayer->mPlaybackRate.mMinRate = AUDIOTRACK_MIN_PLAYBACKRATE_PERMILLE;
1307        pAudioPlayer->mPlaybackRate.mMaxRate = AUDIOTRACK_MAX_PLAYBACKRATE_PERMILLE;
1308        break;
1309    case AUDIOPLAYER_FROM_URIFD:
1310        pAudioPlayer->mPlaybackRate.mMinRate = MEDIAPLAYER_MIN_PLAYBACKRATE_PERMILLE;
1311        pAudioPlayer->mPlaybackRate.mMaxRate = MEDIAPLAYER_MAX_PLAYBACKRATE_PERMILLE;
1312        break;
1313    default:
1314        // use the default range
1315        break;
1316    }
1317
1318}
1319
1320
1321//-----------------------------------------------------------------------------
1322SLresult android_audioPlayer_setConfig(CAudioPlayer *ap, const SLchar *configKey,
1323        const void *pConfigValue, SLuint32 valueSize) {
1324
1325    SLresult result;
1326
1327    assert(NULL != ap && NULL != configKey && NULL != pConfigValue);
1328    if (strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_STREAM_TYPE) == 0) {
1329
1330        // stream type
1331        if (KEY_STREAM_TYPE_PARAMSIZE > valueSize) {
1332            SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW);
1333            result = SL_RESULT_BUFFER_INSUFFICIENT;
1334        } else {
1335            result = audioPlayer_setStreamType(ap, *(SLuint32*)pConfigValue);
1336        }
1337
1338    } else {
1339        SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY);
1340        result = SL_RESULT_PARAMETER_INVALID;
1341    }
1342
1343    return result;
1344}
1345
1346
1347//-----------------------------------------------------------------------------
1348SLresult android_audioPlayer_getConfig(CAudioPlayer* ap, const SLchar *configKey,
1349        SLuint32* pValueSize, void *pConfigValue) {
1350
1351    SLresult result;
1352
1353    assert(NULL != ap && NULL != configKey && NULL != pValueSize);
1354    if (strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_STREAM_TYPE) == 0) {
1355
1356        // stream type
1357        if (NULL == pConfigValue) {
1358            result = SL_RESULT_SUCCESS;
1359        } else if (KEY_STREAM_TYPE_PARAMSIZE > *pValueSize) {
1360            SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW);
1361            result = SL_RESULT_BUFFER_INSUFFICIENT;
1362        } else {
1363            result = audioPlayer_getStreamType(ap, (SLint32*)pConfigValue);
1364        }
1365        *pValueSize = KEY_STREAM_TYPE_PARAMSIZE;
1366
1367    } else {
1368        SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY);
1369        result = SL_RESULT_PARAMETER_INVALID;
1370    }
1371
1372    return result;
1373}
1374
1375
1376// Called from android_audioPlayer_realize for a PCM buffer queue player
1377// to determine if it can use a fast track.
1378static bool canUseFastTrack(CAudioPlayer *pAudioPlayer)
1379{
1380    assert(pAudioPlayer->mAndroidObjType == AUDIOPLAYER_FROM_PCM_BUFFERQUEUE);
1381
1382    // no need to check the buffer queue size, application side
1383    // double-buffering (and more) is not a requirement for using fast tracks
1384
1385    // Check a blacklist of interfaces that are incompatible with fast tracks.
1386    // The alternative, to check a whitelist of compatible interfaces, is
1387    // more maintainable but is too slow.  As a compromise, in a debug build
1388    // we use both methods and warn if they produce different results.
1389    // In release builds, we only use the blacklist method.
1390    // If a blacklisted interface is added after realization using
1391    // DynamicInterfaceManagement::AddInterface,
1392    // then this won't be detected but the interface will be ineffective.
1393    bool blacklistResult = true;
1394    static const unsigned blacklist[] = {
1395        MPH_BASSBOOST,
1396        MPH_EFFECTSEND,
1397        MPH_ENVIRONMENTALREVERB,
1398        MPH_EQUALIZER,
1399        MPH_PLAYBACKRATE,
1400        MPH_PRESETREVERB,
1401        MPH_VIRTUALIZER,
1402        MPH_ANDROIDEFFECT,
1403        MPH_ANDROIDEFFECTSEND,
1404        // FIXME The problem with a blacklist is remembering to add new interfaces here
1405    };
1406    for (unsigned i = 0; i < sizeof(blacklist)/sizeof(blacklist[0]); ++i) {
1407        if (IsInterfaceInitialized(&pAudioPlayer->mObject, blacklist[i])) {
1408            blacklistResult = false;
1409            break;
1410        }
1411    }
1412#if LOG_NDEBUG == 0
1413    bool whitelistResult = true;
1414    static const unsigned whitelist[] = {
1415        MPH_BUFFERQUEUE,
1416        MPH_DYNAMICINTERFACEMANAGEMENT,
1417        MPH_METADATAEXTRACTION,
1418        MPH_MUTESOLO,
1419        MPH_OBJECT,
1420        MPH_PLAY,
1421        MPH_PREFETCHSTATUS,
1422        MPH_VOLUME,
1423        MPH_ANDROIDCONFIGURATION,
1424        MPH_ANDROIDSIMPLEBUFFERQUEUE,
1425        MPH_ANDROIDBUFFERQUEUESOURCE,
1426    };
1427    for (unsigned mph = MPH_MIN; mph < MPH_MAX; ++mph) {
1428        for (unsigned i = 0; i < sizeof(whitelist)/sizeof(whitelist[0]); ++i) {
1429            if (mph == whitelist[i]) {
1430                goto compatible;
1431            }
1432        }
1433        if (IsInterfaceInitialized(&pAudioPlayer->mObject, mph)) {
1434            whitelistResult = false;
1435            break;
1436        }
1437compatible: ;
1438    }
1439    if (whitelistResult != blacklistResult) {
1440        SL_LOGW("whitelistResult != blacklistResult");
1441        // and use blacklistResult below
1442    }
1443#endif
1444    return blacklistResult;
1445}
1446
1447
1448//-----------------------------------------------------------------------------
1449// FIXME abstract out the diff between CMediaPlayer and CAudioPlayer
1450SLresult android_audioPlayer_realize(CAudioPlayer *pAudioPlayer, SLboolean async) {
1451
1452    SLresult result = SL_RESULT_SUCCESS;
1453    SL_LOGV("Realize pAudioPlayer=%p", pAudioPlayer);
1454
1455    AudioPlayback_Parameters app;
1456    app.sessionId = pAudioPlayer->mSessionId;
1457    app.streamType = pAudioPlayer->mStreamType;
1458
1459    switch (pAudioPlayer->mAndroidObjType) {
1460
1461    //-----------------------------------
1462    // AudioTrack
1463    case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: {
1464        // initialize platform-specific CAudioPlayer fields
1465
1466        SLDataFormat_PCM *df_pcm = (SLDataFormat_PCM *)
1467                pAudioPlayer->mDynamicSource.mDataSource->pFormat;
1468
1469        uint32_t sampleRate = sles_to_android_sampleRate(df_pcm->samplesPerSec);
1470
1471        audio_channel_mask_t channelMask;
1472        channelMask = sles_to_audio_output_channel_mask(df_pcm->channelMask);
1473
1474        // To maintain backward compatibility with previous releases, ignore
1475        // channel masks that are not indexed.
1476        if (channelMask == AUDIO_CHANNEL_INVALID
1477                || audio_channel_mask_get_representation(channelMask)
1478                        == AUDIO_CHANNEL_REPRESENTATION_POSITION) {
1479            channelMask = audio_channel_out_mask_from_count(df_pcm->numChannels);
1480            SL_LOGI("Emulating old channel mask behavior "
1481                    "(ignoring positional mask %#x, using default mask %#x based on "
1482                    "channel count of %d)", df_pcm->channelMask, channelMask,
1483                    df_pcm->numChannels);
1484        }
1485        SL_LOGV("AudioPlayer: mapped SLES channel mask %#x to android channel mask %#x",
1486            df_pcm->channelMask,
1487            channelMask);
1488
1489        audio_output_flags_t policy;
1490        if (canUseFastTrack(pAudioPlayer)) {
1491            policy = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_RAW);
1492        } else {
1493            policy = AUDIO_OUTPUT_FLAG_NONE;
1494        }
1495
1496        pAudioPlayer->mAudioTrack = new android::AudioTrack(
1497                pAudioPlayer->mStreamType,                           // streamType
1498                sampleRate,                                          // sampleRate
1499                sles_to_android_sampleFormat(df_pcm),                // format
1500                channelMask,                                         // channel mask
1501                0,                                                   // frameCount
1502                policy,                                              // flags
1503                audioTrack_callBack_pullFromBuffQueue,               // callback
1504                (void *) pAudioPlayer,                               // user
1505                0,     // FIXME find appropriate frame count         // notificationFrame
1506                pAudioPlayer->mSessionId);
1507        android::status_t status = pAudioPlayer->mAudioTrack->initCheck();
1508        if (status != android::NO_ERROR) {
1509            SL_LOGE("AudioTrack::initCheck status %u", status);
1510            // FIXME should return a more specific result depending on status
1511            result = SL_RESULT_CONTENT_UNSUPPORTED;
1512            pAudioPlayer->mAudioTrack.clear();
1513            return result;
1514        }
1515
1516        // initialize platform-independent CAudioPlayer fields
1517
1518        pAudioPlayer->mNumChannels = df_pcm->numChannels;
1519        pAudioPlayer->mSampleRateMilliHz = df_pcm->samplesPerSec; // Note: bad field name in SL ES
1520
1521        // This use case does not have a separate "prepare" step
1522        pAudioPlayer->mAndroidObjState = ANDROID_READY;
1523        }
1524        break;
1525
1526    //-----------------------------------
1527    // MediaPlayer
1528    case AUDIOPLAYER_FROM_URIFD: {
1529        pAudioPlayer->mAPlayer = new android::LocAVPlayer(&app, false /*hasVideo*/);
1530        pAudioPlayer->mAPlayer->init(sfplayer_handlePrefetchEvent,
1531                        (void*)pAudioPlayer /*notifUSer*/);
1532
1533        switch (pAudioPlayer->mDataSource.mLocator.mLocatorType) {
1534            case SL_DATALOCATOR_URI: {
1535                // The legacy implementation ran Stagefright within the application process, and
1536                // so allowed local pathnames specified by URI that were openable by
1537                // the application but were not openable by mediaserver.
1538                // The current implementation runs Stagefright (mostly) within mediaserver,
1539                // which runs as a different UID and likely a different current working directory.
1540                // For backwards compatibility with any applications which may have relied on the
1541                // previous behavior, we convert an openable file URI into an FD.
1542                // Note that unlike SL_DATALOCATOR_ANDROIDFD, this FD is owned by us
1543                // and so we close it as soon as we've passed it (via Binder dup) to mediaserver.
1544                const char *uri = (const char *)pAudioPlayer->mDataSource.mLocator.mURI.URI;
1545                if (!isDistantProtocol(uri)) {
1546                    // don't touch the original uri, we may need it later
1547                    const char *pathname = uri;
1548                    // skip over an optional leading file:// prefix
1549                    if (!strncasecmp(pathname, "file://", 7)) {
1550                        pathname += 7;
1551                    }
1552                    // attempt to open it as a file using the application's credentials
1553                    int fd = ::open(pathname, O_RDONLY);
1554                    if (fd >= 0) {
1555                        // if open is successful, then check to see if it's a regular file
1556                        struct stat statbuf;
1557                        if (!::fstat(fd, &statbuf) && S_ISREG(statbuf.st_mode)) {
1558                            // treat similarly to an FD data locator, but
1559                            // let setDataSource take responsibility for closing fd
1560                            pAudioPlayer->mAPlayer->setDataSource(fd, 0, statbuf.st_size, true);
1561                            break;
1562                        }
1563                        // we were able to open it, but it's not a file, so let mediaserver try
1564                        (void) ::close(fd);
1565                    }
1566                }
1567                // if either the URI didn't look like a file, or open failed, or not a file
1568                pAudioPlayer->mAPlayer->setDataSource(uri);
1569                } break;
1570            case SL_DATALOCATOR_ANDROIDFD: {
1571                int64_t offset = (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.offset;
1572                pAudioPlayer->mAPlayer->setDataSource(
1573                        (int)pAudioPlayer->mDataSource.mLocator.mFD.fd,
1574                        offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ?
1575                                (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset,
1576                        (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.length);
1577                }
1578                break;
1579            default:
1580                SL_LOGE(ERROR_PLAYERREALIZE_UNKNOWN_DATASOURCE_LOCATOR);
1581                break;
1582        }
1583
1584        }
1585        break;
1586
1587    //-----------------------------------
1588    // StreamPlayer
1589    case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: {
1590        android::StreamPlayer* splr = new android::StreamPlayer(&app, false /*hasVideo*/,
1591                &pAudioPlayer->mAndroidBufferQueue, pAudioPlayer->mCallbackProtector);
1592        pAudioPlayer->mAPlayer = splr;
1593        splr->init(sfplayer_handlePrefetchEvent, (void*)pAudioPlayer);
1594        }
1595        break;
1596
1597    //-----------------------------------
1598    // AudioToCbRenderer
1599    case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: {
1600        android::AudioToCbRenderer* decoder = new android::AudioToCbRenderer(&app);
1601        pAudioPlayer->mAPlayer = decoder;
1602        // configures the callback for the sink buffer queue
1603        decoder->setDataPushListener(adecoder_writeToBufferQueue, pAudioPlayer);
1604        // configures the callback for the notifications coming from the SF code
1605        decoder->init(sfplayer_handlePrefetchEvent, (void*)pAudioPlayer);
1606
1607        switch (pAudioPlayer->mDataSource.mLocator.mLocatorType) {
1608        case SL_DATALOCATOR_URI:
1609            decoder->setDataSource(
1610                    (const char*)pAudioPlayer->mDataSource.mLocator.mURI.URI);
1611            break;
1612        case SL_DATALOCATOR_ANDROIDFD: {
1613            int64_t offset = (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.offset;
1614            decoder->setDataSource(
1615                    (int)pAudioPlayer->mDataSource.mLocator.mFD.fd,
1616                    offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ?
1617                            (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset,
1618                            (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.length);
1619            }
1620            break;
1621        default:
1622            SL_LOGE(ERROR_PLAYERREALIZE_UNKNOWN_DATASOURCE_LOCATOR);
1623            break;
1624        }
1625
1626        }
1627        break;
1628
1629    //-----------------------------------
1630    // AacBqToPcmCbRenderer
1631    case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: {
1632        android::AacBqToPcmCbRenderer* bqtobq = new android::AacBqToPcmCbRenderer(&app,
1633                &pAudioPlayer->mAndroidBufferQueue);
1634        // configures the callback for the sink buffer queue
1635        bqtobq->setDataPushListener(adecoder_writeToBufferQueue, pAudioPlayer);
1636        pAudioPlayer->mAPlayer = bqtobq;
1637        // configures the callback for the notifications coming from the SF code,
1638        // but also implicitly configures the AndroidBufferQueue from which ADTS data is read
1639        pAudioPlayer->mAPlayer->init(sfplayer_handlePrefetchEvent, (void*)pAudioPlayer);
1640        }
1641        break;
1642
1643    //-----------------------------------
1644    default:
1645        SL_LOGE(ERROR_PLAYERREALIZE_UNEXPECTED_OBJECT_TYPE_D, pAudioPlayer->mAndroidObjType);
1646        result = SL_RESULT_INTERNAL_ERROR;
1647        break;
1648    }
1649
1650    // proceed with effect initialization
1651    // initialize EQ
1652    // FIXME use a table of effect descriptors when adding support for more effects
1653    if (memcmp(SL_IID_EQUALIZER, &pAudioPlayer->mEqualizer.mEqDescriptor.type,
1654            sizeof(effect_uuid_t)) == 0) {
1655        SL_LOGV("Need to initialize EQ for AudioPlayer=%p", pAudioPlayer);
1656        android_eq_init(pAudioPlayer->mSessionId, &pAudioPlayer->mEqualizer);
1657    }
1658    // initialize BassBoost
1659    if (memcmp(SL_IID_BASSBOOST, &pAudioPlayer->mBassBoost.mBassBoostDescriptor.type,
1660            sizeof(effect_uuid_t)) == 0) {
1661        SL_LOGV("Need to initialize BassBoost for AudioPlayer=%p", pAudioPlayer);
1662        android_bb_init(pAudioPlayer->mSessionId, &pAudioPlayer->mBassBoost);
1663    }
1664    // initialize Virtualizer
1665    if (memcmp(SL_IID_VIRTUALIZER, &pAudioPlayer->mVirtualizer.mVirtualizerDescriptor.type,
1666               sizeof(effect_uuid_t)) == 0) {
1667        SL_LOGV("Need to initialize Virtualizer for AudioPlayer=%p", pAudioPlayer);
1668        android_virt_init(pAudioPlayer->mSessionId, &pAudioPlayer->mVirtualizer);
1669    }
1670
1671    // initialize EffectSend
1672    // FIXME initialize EffectSend
1673
1674    return result;
1675}
1676
1677
1678//-----------------------------------------------------------------------------
1679/**
1680 * Called with a lock on AudioPlayer, and blocks until safe to destroy
1681 */
1682SLresult android_audioPlayer_preDestroy(CAudioPlayer *pAudioPlayer) {
1683    SL_LOGD("android_audioPlayer_preDestroy(%p)", pAudioPlayer);
1684    SLresult result = SL_RESULT_SUCCESS;
1685
1686    bool disableCallbacksBeforePreDestroy;
1687    switch (pAudioPlayer->mAndroidObjType) {
1688    // Not yet clear why this order is important, but it reduces detected deadlocks
1689    case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1690        disableCallbacksBeforePreDestroy = true;
1691        break;
1692    // Use the old behavior for all other use cases until proven
1693    // case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1694    default:
1695        disableCallbacksBeforePreDestroy = false;
1696        break;
1697    }
1698
1699    if (disableCallbacksBeforePreDestroy) {
1700        object_unlock_exclusive(&pAudioPlayer->mObject);
1701        if (pAudioPlayer->mCallbackProtector != 0) {
1702            pAudioPlayer->mCallbackProtector->requestCbExitAndWait();
1703        }
1704        object_lock_exclusive(&pAudioPlayer->mObject);
1705    }
1706
1707    if (pAudioPlayer->mAPlayer != 0) {
1708        pAudioPlayer->mAPlayer->preDestroy();
1709    }
1710    SL_LOGD("android_audioPlayer_preDestroy(%p) after mAPlayer->preDestroy()", pAudioPlayer);
1711
1712    if (!disableCallbacksBeforePreDestroy) {
1713        object_unlock_exclusive(&pAudioPlayer->mObject);
1714        if (pAudioPlayer->mCallbackProtector != 0) {
1715            pAudioPlayer->mCallbackProtector->requestCbExitAndWait();
1716        }
1717        object_lock_exclusive(&pAudioPlayer->mObject);
1718    }
1719
1720    return result;
1721}
1722
1723
1724//-----------------------------------------------------------------------------
1725SLresult android_audioPlayer_destroy(CAudioPlayer *pAudioPlayer) {
1726    SLresult result = SL_RESULT_SUCCESS;
1727    SL_LOGV("android_audioPlayer_destroy(%p)", pAudioPlayer);
1728    switch (pAudioPlayer->mAndroidObjType) {
1729
1730    case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
1731        // We own the audio track for PCM buffer queue players
1732        if (pAudioPlayer->mAudioTrack != 0) {
1733            pAudioPlayer->mAudioTrack->stop();
1734            // Note that there may still be another reference in post-unlock phase of SetPlayState
1735            pAudioPlayer->mAudioTrack.clear();
1736        }
1737        break;
1738
1739    case AUDIOPLAYER_FROM_URIFD:     // intended fall-through
1740    case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:    // intended fall-through
1741    case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: // intended fall-through
1742    case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1743        pAudioPlayer->mAPlayer.clear();
1744        break;
1745    //-----------------------------------
1746    default:
1747        SL_LOGE(ERROR_PLAYERDESTROY_UNEXPECTED_OBJECT_TYPE_D, pAudioPlayer->mAndroidObjType);
1748        result = SL_RESULT_INTERNAL_ERROR;
1749        break;
1750    }
1751
1752    // placeholder: not necessary yet as session ID lifetime doesn't extend beyond player
1753    // android::AudioSystem::releaseAudioSessionId(pAudioPlayer->mSessionId);
1754
1755    pAudioPlayer->mCallbackProtector.clear();
1756
1757    // explicit destructor
1758    pAudioPlayer->mAudioTrack.~sp();
1759    // note that SetPlayState(PLAYING) may still hold a reference
1760    pAudioPlayer->mCallbackProtector.~sp();
1761    pAudioPlayer->mAuxEffect.~sp();
1762    pAudioPlayer->mAPlayer.~sp();
1763
1764    return result;
1765}
1766
1767
1768//-----------------------------------------------------------------------------
1769SLresult android_audioPlayer_setPlaybackRateAndConstraints(CAudioPlayer *ap, SLpermille rate,
1770        SLuint32 constraints) {
1771    SLresult result = SL_RESULT_SUCCESS;
1772    switch (ap->mAndroidObjType) {
1773    case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: {
1774        // these asserts were already checked by the platform-independent layer
1775        assert((AUDIOTRACK_MIN_PLAYBACKRATE_PERMILLE <= rate) &&
1776                (rate <= AUDIOTRACK_MAX_PLAYBACKRATE_PERMILLE));
1777        assert(constraints & SL_RATEPROP_NOPITCHCORAUDIO);
1778        // get the content sample rate
1779        uint32_t contentRate = sles_to_android_sampleRate(ap->mSampleRateMilliHz);
1780        // apply the SL ES playback rate on the AudioTrack as a factor of its content sample rate
1781        if (ap->mAudioTrack != 0) {
1782            ap->mAudioTrack->setSampleRate(contentRate * (rate/1000.0f));
1783        }
1784        }
1785        break;
1786    case AUDIOPLAYER_FROM_URIFD: {
1787        assert((MEDIAPLAYER_MIN_PLAYBACKRATE_PERMILLE <= rate) &&
1788                        (rate <= MEDIAPLAYER_MAX_PLAYBACKRATE_PERMILLE));
1789        assert(constraints & SL_RATEPROP_NOPITCHCORAUDIO);
1790        // apply the SL ES playback rate on the GenericPlayer
1791        if (ap->mAPlayer != 0) {
1792            ap->mAPlayer->setPlaybackRate((int16_t)rate);
1793        }
1794        }
1795        break;
1796
1797    default:
1798        SL_LOGE("Unexpected object type %d", ap->mAndroidObjType);
1799        result = SL_RESULT_FEATURE_UNSUPPORTED;
1800        break;
1801    }
1802    return result;
1803}
1804
1805
1806//-----------------------------------------------------------------------------
1807// precondition
1808//  called with no lock held
1809//  ap != NULL
1810//  pItemCount != NULL
1811SLresult android_audioPlayer_metadata_getItemCount(CAudioPlayer *ap, SLuint32 *pItemCount) {
1812    if (ap->mAPlayer == 0) {
1813        return SL_RESULT_PARAMETER_INVALID;
1814    }
1815    switch (ap->mAndroidObjType) {
1816      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1817      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1818        {
1819            android::AudioSfDecoder* decoder =
1820                    static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get());
1821            *pItemCount = decoder->getPcmFormatKeyCount();
1822        }
1823        break;
1824      default:
1825        *pItemCount = 0;
1826        break;
1827    }
1828    return SL_RESULT_SUCCESS;
1829}
1830
1831
1832//-----------------------------------------------------------------------------
1833// precondition
1834//  called with no lock held
1835//  ap != NULL
1836//  pKeySize != NULL
1837SLresult android_audioPlayer_metadata_getKeySize(CAudioPlayer *ap,
1838        SLuint32 index, SLuint32 *pKeySize) {
1839    if (ap->mAPlayer == 0) {
1840        return SL_RESULT_PARAMETER_INVALID;
1841    }
1842    SLresult res = SL_RESULT_SUCCESS;
1843    switch (ap->mAndroidObjType) {
1844      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1845      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1846        {
1847            android::AudioSfDecoder* decoder =
1848                    static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get());
1849            SLuint32 keyNameSize = 0;
1850            if (!decoder->getPcmFormatKeySize(index, &keyNameSize)) {
1851                res = SL_RESULT_PARAMETER_INVALID;
1852            } else {
1853                // *pKeySize is the size of the region used to store the key name AND
1854                //   the information about the key (size, lang, encoding)
1855                *pKeySize = keyNameSize + sizeof(SLMetadataInfo);
1856            }
1857        }
1858        break;
1859      default:
1860        *pKeySize = 0;
1861        res = SL_RESULT_PARAMETER_INVALID;
1862        break;
1863    }
1864    return res;
1865}
1866
1867
1868//-----------------------------------------------------------------------------
1869// precondition
1870//  called with no lock held
1871//  ap != NULL
1872//  pKey != NULL
1873SLresult android_audioPlayer_metadata_getKey(CAudioPlayer *ap,
1874        SLuint32 index, SLuint32 size, SLMetadataInfo *pKey) {
1875    if (ap->mAPlayer == 0) {
1876        return SL_RESULT_PARAMETER_INVALID;
1877    }
1878    SLresult res = SL_RESULT_SUCCESS;
1879    switch (ap->mAndroidObjType) {
1880      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1881      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1882        {
1883            android::AudioSfDecoder* decoder =
1884                    static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get());
1885            if ((size < sizeof(SLMetadataInfo) ||
1886                    (!decoder->getPcmFormatKeyName(index, size - sizeof(SLMetadataInfo),
1887                            (char*)pKey->data)))) {
1888                res = SL_RESULT_PARAMETER_INVALID;
1889            } else {
1890                // successfully retrieved the key value, update the other fields
1891                pKey->encoding = SL_CHARACTERENCODING_UTF8;
1892                memcpy((char *) pKey->langCountry, "en", 3);
1893                pKey->size = strlen((char*)pKey->data) + 1;
1894            }
1895        }
1896        break;
1897      default:
1898        res = SL_RESULT_PARAMETER_INVALID;
1899        break;
1900    }
1901    return res;
1902}
1903
1904
1905//-----------------------------------------------------------------------------
1906// precondition
1907//  called with no lock held
1908//  ap != NULL
1909//  pValueSize != NULL
1910SLresult android_audioPlayer_metadata_getValueSize(CAudioPlayer *ap,
1911        SLuint32 index, SLuint32 *pValueSize) {
1912    if (ap->mAPlayer == 0) {
1913        return SL_RESULT_PARAMETER_INVALID;
1914    }
1915    SLresult res = SL_RESULT_SUCCESS;
1916    switch (ap->mAndroidObjType) {
1917      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1918      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1919        {
1920            android::AudioSfDecoder* decoder =
1921                    static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get());
1922            SLuint32 valueSize = 0;
1923            if (!decoder->getPcmFormatValueSize(index, &valueSize)) {
1924                res = SL_RESULT_PARAMETER_INVALID;
1925            } else {
1926                // *pValueSize is the size of the region used to store the key value AND
1927                //   the information about the value (size, lang, encoding)
1928                *pValueSize = valueSize + sizeof(SLMetadataInfo);
1929            }
1930        }
1931        break;
1932      default:
1933          *pValueSize = 0;
1934          res = SL_RESULT_PARAMETER_INVALID;
1935          break;
1936    }
1937    return res;
1938}
1939
1940
1941//-----------------------------------------------------------------------------
1942// precondition
1943//  called with no lock held
1944//  ap != NULL
1945//  pValue != NULL
1946SLresult android_audioPlayer_metadata_getValue(CAudioPlayer *ap,
1947        SLuint32 index, SLuint32 size, SLMetadataInfo *pValue) {
1948    if (ap->mAPlayer == 0) {
1949        return SL_RESULT_PARAMETER_INVALID;
1950    }
1951    SLresult res = SL_RESULT_SUCCESS;
1952    switch (ap->mAndroidObjType) {
1953      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1954      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1955        {
1956            android::AudioSfDecoder* decoder =
1957                    static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get());
1958            pValue->encoding = SL_CHARACTERENCODING_BINARY;
1959            memcpy((char *) pValue->langCountry, "en", 3); // applicable here?
1960            SLuint32 valueSize = 0;
1961            if ((size < sizeof(SLMetadataInfo)
1962                    || (!decoder->getPcmFormatValueSize(index, &valueSize))
1963                    || (!decoder->getPcmFormatKeyValue(index, size - sizeof(SLMetadataInfo),
1964                            (SLuint32*)pValue->data)))) {
1965                res = SL_RESULT_PARAMETER_INVALID;
1966            } else {
1967                pValue->size = valueSize;
1968            }
1969        }
1970        break;
1971      default:
1972        res = SL_RESULT_PARAMETER_INVALID;
1973        break;
1974    }
1975    return res;
1976}
1977
1978//-----------------------------------------------------------------------------
1979// preconditions
1980//  ap != NULL
1981//  mutex is locked
1982//  play state has changed
1983void android_audioPlayer_setPlayState(CAudioPlayer *ap) {
1984
1985    SLuint32 playState = ap->mPlay.mState;
1986
1987    switch (ap->mAndroidObjType) {
1988    case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
1989        switch (playState) {
1990        case SL_PLAYSTATE_STOPPED:
1991            SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_STOPPED");
1992            if (ap->mAudioTrack != 0) {
1993                ap->mAudioTrack->stop();
1994            }
1995            break;
1996        case SL_PLAYSTATE_PAUSED:
1997            SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PAUSED");
1998            if (ap->mAudioTrack != 0) {
1999                ap->mAudioTrack->pause();
2000            }
2001            break;
2002        case SL_PLAYSTATE_PLAYING:
2003            SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PLAYING");
2004            if (ap->mAudioTrack != 0) {
2005                // instead of ap->mAudioTrack->start();
2006                ap->mDeferredStart = true;
2007            }
2008            break;
2009        default:
2010            // checked by caller, should not happen
2011            break;
2012        }
2013        break;
2014
2015    case AUDIOPLAYER_FROM_URIFD:      // intended fall-through
2016    case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:     // intended fall-through
2017    case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:  // intended fall-through
2018    case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
2019        // FIXME report and use the return code to the lock mechanism, which is where play state
2020        //   changes are updated (see object_unlock_exclusive_attributes())
2021        aplayer_setPlayState(ap->mAPlayer, playState, &ap->mAndroidObjState);
2022        break;
2023    default:
2024        SL_LOGE(ERROR_PLAYERSETPLAYSTATE_UNEXPECTED_OBJECT_TYPE_D, ap->mAndroidObjType);
2025        break;
2026    }
2027}
2028
2029
2030//-----------------------------------------------------------------------------
2031// call when either player event flags, marker position, or position update period changes
2032void android_audioPlayer_usePlayEventMask(CAudioPlayer *ap) {
2033    IPlay *pPlayItf = &ap->mPlay;
2034    SLuint32 eventFlags = pPlayItf->mEventFlags;
2035    /*switch (ap->mAndroidObjType) {
2036    case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:*/
2037
2038    if (ap->mAPlayer != 0) {
2039        assert(ap->mAudioTrack == 0);
2040        ap->mAPlayer->setPlayEvents((int32_t) eventFlags, (int32_t) pPlayItf->mMarkerPosition,
2041                (int32_t) pPlayItf->mPositionUpdatePeriod);
2042        return;
2043    }
2044
2045    if (ap->mAudioTrack == 0) {
2046        return;
2047    }
2048
2049    if (eventFlags & SL_PLAYEVENT_HEADATMARKER) {
2050        ap->mAudioTrack->setMarkerPosition((uint32_t)((((int64_t)pPlayItf->mMarkerPosition
2051                * sles_to_android_sampleRate(ap->mSampleRateMilliHz)))/1000));
2052    } else {
2053        // clear marker
2054        ap->mAudioTrack->setMarkerPosition(0);
2055    }
2056
2057    if (eventFlags & SL_PLAYEVENT_HEADATNEWPOS) {
2058         ap->mAudioTrack->setPositionUpdatePeriod(
2059                (uint32_t)((((int64_t)pPlayItf->mPositionUpdatePeriod
2060                * sles_to_android_sampleRate(ap->mSampleRateMilliHz)))/1000));
2061    } else {
2062        // clear periodic update
2063        ap->mAudioTrack->setPositionUpdatePeriod(0);
2064    }
2065
2066    if (eventFlags & SL_PLAYEVENT_HEADATEND) {
2067        // nothing to do for SL_PLAYEVENT_HEADATEND, callback event will be checked against mask
2068    }
2069
2070    if (eventFlags & SL_PLAYEVENT_HEADMOVING) {
2071        // FIXME support SL_PLAYEVENT_HEADMOVING
2072        SL_LOGD("[ FIXME: IPlay_SetCallbackEventsMask(SL_PLAYEVENT_HEADMOVING) on an "
2073            "SL_OBJECTID_AUDIOPLAYER to be implemented ]");
2074    }
2075    if (eventFlags & SL_PLAYEVENT_HEADSTALLED) {
2076        // nothing to do for SL_PLAYEVENT_HEADSTALLED, callback event will be checked against mask
2077    }
2078
2079}
2080
2081
2082//-----------------------------------------------------------------------------
2083SLresult android_audioPlayer_getDuration(IPlay *pPlayItf, SLmillisecond *pDurMsec) {
2084    CAudioPlayer *ap = (CAudioPlayer *)pPlayItf->mThis;
2085    switch (ap->mAndroidObjType) {
2086
2087      case AUDIOPLAYER_FROM_URIFD:  // intended fall-through
2088      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: {
2089        int32_t durationMsec = ANDROID_UNKNOWN_TIME;
2090        if (ap->mAPlayer != 0) {
2091            ap->mAPlayer->getDurationMsec(&durationMsec);
2092        }
2093        *pDurMsec = durationMsec == ANDROID_UNKNOWN_TIME ? SL_TIME_UNKNOWN : durationMsec;
2094        break;
2095      }
2096
2097      case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
2098      case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
2099      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
2100      default: {
2101        *pDurMsec = SL_TIME_UNKNOWN;
2102      }
2103    }
2104    return SL_RESULT_SUCCESS;
2105}
2106
2107
2108//-----------------------------------------------------------------------------
2109void android_audioPlayer_getPosition(IPlay *pPlayItf, SLmillisecond *pPosMsec) {
2110    CAudioPlayer *ap = (CAudioPlayer *)pPlayItf->mThis;
2111    switch (ap->mAndroidObjType) {
2112
2113      case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
2114        if ((ap->mSampleRateMilliHz == UNKNOWN_SAMPLERATE) || (ap->mAudioTrack == 0)) {
2115            *pPosMsec = 0;
2116        } else {
2117            uint32_t positionInFrames;
2118            ap->mAudioTrack->getPosition(&positionInFrames);
2119            *pPosMsec = ((int64_t)positionInFrames * 1000) /
2120                    sles_to_android_sampleRate(ap->mSampleRateMilliHz);
2121        }
2122        break;
2123
2124      case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:    // intended fall-through
2125      case AUDIOPLAYER_FROM_URIFD:
2126      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
2127      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: {
2128        int32_t posMsec = ANDROID_UNKNOWN_TIME;
2129        if (ap->mAPlayer != 0) {
2130            ap->mAPlayer->getPositionMsec(&posMsec);
2131        }
2132        *pPosMsec = posMsec == ANDROID_UNKNOWN_TIME ? 0 : posMsec;
2133        break;
2134      }
2135
2136      default:
2137        *pPosMsec = 0;
2138    }
2139}
2140
2141
2142//-----------------------------------------------------------------------------
2143SLresult android_audioPlayer_seek(CAudioPlayer *ap, SLmillisecond posMsec) {
2144    SLresult result = SL_RESULT_SUCCESS;
2145
2146    switch (ap->mAndroidObjType) {
2147
2148      case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:      // intended fall-through
2149      case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:
2150      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
2151        result = SL_RESULT_FEATURE_UNSUPPORTED;
2152        break;
2153
2154      case AUDIOPLAYER_FROM_URIFD:                   // intended fall-through
2155      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
2156        if (ap->mAPlayer != 0) {
2157            ap->mAPlayer->seek(posMsec);
2158        }
2159        break;
2160
2161      default:
2162        break;
2163    }
2164    return result;
2165}
2166
2167
2168//-----------------------------------------------------------------------------
2169SLresult android_audioPlayer_loop(CAudioPlayer *ap, SLboolean loopEnable) {
2170    SLresult result = SL_RESULT_SUCCESS;
2171
2172    switch (ap->mAndroidObjType) {
2173    case AUDIOPLAYER_FROM_URIFD:
2174    // case AUDIOPLAY_FROM_URIFD_TO_PCM_BUFFERQUEUE:
2175    //      would actually work, but what's the point?
2176      if (ap->mAPlayer != 0) {
2177        ap->mAPlayer->loop((bool)loopEnable);
2178      }
2179      break;
2180    default:
2181      result = SL_RESULT_FEATURE_UNSUPPORTED;
2182      break;
2183    }
2184    return result;
2185}
2186
2187
2188//-----------------------------------------------------------------------------
2189SLresult android_audioPlayer_setBufferingUpdateThresholdPerMille(CAudioPlayer *ap,
2190        SLpermille threshold) {
2191    SLresult result = SL_RESULT_SUCCESS;
2192
2193    switch (ap->mAndroidObjType) {
2194      case AUDIOPLAYER_FROM_URIFD:
2195        if (ap->mAPlayer != 0) {
2196            ap->mAPlayer->setBufferingUpdateThreshold(threshold / 10);
2197        }
2198        break;
2199
2200      default: {}
2201    }
2202
2203    return result;
2204}
2205
2206
2207//-----------------------------------------------------------------------------
2208void android_audioPlayer_bufferQueue_onRefilled_l(CAudioPlayer *ap) {
2209    // the AudioTrack associated with the AudioPlayer receiving audio from a PCM buffer
2210    // queue was stopped when the queue become empty, we restart as soon as a new buffer
2211    // has been enqueued since we're in playing state
2212    if (ap->mAudioTrack != 0) {
2213        // instead of ap->mAudioTrack->start();
2214        ap->mDeferredStart = true;
2215    }
2216
2217    // when the queue became empty, an underflow on the prefetch status itf was sent. Now the queue
2218    // has received new data, signal it has sufficient data
2219    if (IsInterfaceInitialized(&ap->mObject, MPH_PREFETCHSTATUS)) {
2220        // we wouldn't have been called unless we were previously in the underflow state
2221        assert(SL_PREFETCHSTATUS_UNDERFLOW == ap->mPrefetchStatus.mStatus);
2222        assert(0 == ap->mPrefetchStatus.mLevel);
2223        ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_SUFFICIENTDATA;
2224        ap->mPrefetchStatus.mLevel = 1000;
2225        // callback or no callback?
2226        SLuint32 prefetchEvents = ap->mPrefetchStatus.mCallbackEventsMask &
2227                (SL_PREFETCHEVENT_STATUSCHANGE | SL_PREFETCHEVENT_FILLLEVELCHANGE);
2228        if (SL_PREFETCHEVENT_NONE != prefetchEvents) {
2229            ap->mPrefetchStatus.mDeferredPrefetchCallback = ap->mPrefetchStatus.mCallback;
2230            ap->mPrefetchStatus.mDeferredPrefetchContext  = ap->mPrefetchStatus.mContext;
2231            ap->mPrefetchStatus.mDeferredPrefetchEvents   = prefetchEvents;
2232        }
2233    }
2234}
2235
2236
2237//-----------------------------------------------------------------------------
2238/*
2239 * BufferQueue::Clear
2240 */
2241SLresult android_audioPlayer_bufferQueue_onClear(CAudioPlayer *ap) {
2242    SLresult result = SL_RESULT_SUCCESS;
2243
2244    switch (ap->mAndroidObjType) {
2245    //-----------------------------------
2246    // AudioTrack
2247    case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
2248        if (ap->mAudioTrack != 0) {
2249            ap->mAudioTrack->flush();
2250        }
2251        break;
2252    default:
2253        result = SL_RESULT_INTERNAL_ERROR;
2254        break;
2255    }
2256
2257    return result;
2258}
2259
2260
2261//-----------------------------------------------------------------------------
2262void android_audioPlayer_androidBufferQueue_clear_l(CAudioPlayer *ap) {
2263    switch (ap->mAndroidObjType) {
2264    case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:
2265      if (ap->mAPlayer != 0) {
2266        android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(ap->mAPlayer.get());
2267        splr->appClear_l();
2268      } break;
2269    case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
2270      // nothing to do here, fall through
2271    default:
2272      break;
2273    }
2274}
2275
2276void android_audioPlayer_androidBufferQueue_onRefilled_l(CAudioPlayer *ap) {
2277    switch (ap->mAndroidObjType) {
2278    case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:
2279      if (ap->mAPlayer != 0) {
2280        android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(ap->mAPlayer.get());
2281        splr->queueRefilled();
2282      } break;
2283    case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
2284      // FIXME this may require waking up the decoder if it is currently starved and isn't polling
2285    default:
2286      break;
2287    }
2288}
2289