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