AudioPlayer_to_android.cpp revision b30330217356d0e10abe5220662424231dc57880
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 = (audio_session_t) android::AudioSystem::newAudioUniqueId(
1286            AUDIO_UNIQUE_ID_USE_SESSION);
1287
1288    // placeholder: not necessary yet as session ID lifetime doesn't extend beyond player
1289    // android::AudioSystem::acquireAudioSessionId(pAudioPlayer->mSessionId);
1290
1291    pAudioPlayer->mStreamType = ANDROID_DEFAULT_OUTPUT_STREAM_TYPE;
1292
1293    // mAudioTrack
1294    pAudioPlayer->mCallbackProtector = new android::CallbackProtector();
1295    // mAPLayer
1296    // mAuxEffect
1297
1298    pAudioPlayer->mAuxSendLevel = 0;
1299    pAudioPlayer->mAmplFromDirectLevel = 1.0f; // matches initial mDirectLevel value
1300    pAudioPlayer->mDeferredStart = false;
1301
1302    // This section re-initializes interface-specific fields that
1303    // can be set or used regardless of whether the interface is
1304    // exposed on the AudioPlayer or not
1305
1306    switch (pAudioPlayer->mAndroidObjType) {
1307    case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
1308        pAudioPlayer->mPlaybackRate.mMinRate = AUDIOTRACK_MIN_PLAYBACKRATE_PERMILLE;
1309        pAudioPlayer->mPlaybackRate.mMaxRate = AUDIOTRACK_MAX_PLAYBACKRATE_PERMILLE;
1310        break;
1311    case AUDIOPLAYER_FROM_URIFD:
1312        pAudioPlayer->mPlaybackRate.mMinRate = MEDIAPLAYER_MIN_PLAYBACKRATE_PERMILLE;
1313        pAudioPlayer->mPlaybackRate.mMaxRate = MEDIAPLAYER_MAX_PLAYBACKRATE_PERMILLE;
1314        break;
1315    default:
1316        // use the default range
1317        break;
1318    }
1319
1320}
1321
1322
1323//-----------------------------------------------------------------------------
1324SLresult android_audioPlayer_setConfig(CAudioPlayer *ap, const SLchar *configKey,
1325        const void *pConfigValue, SLuint32 valueSize) {
1326
1327    SLresult result;
1328
1329    assert(NULL != ap && NULL != configKey && NULL != pConfigValue);
1330    if (strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_STREAM_TYPE) == 0) {
1331
1332        // stream type
1333        if (KEY_STREAM_TYPE_PARAMSIZE > valueSize) {
1334            SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW);
1335            result = SL_RESULT_BUFFER_INSUFFICIENT;
1336        } else {
1337            result = audioPlayer_setStreamType(ap, *(SLuint32*)pConfigValue);
1338        }
1339
1340    } else {
1341        SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY);
1342        result = SL_RESULT_PARAMETER_INVALID;
1343    }
1344
1345    return result;
1346}
1347
1348
1349//-----------------------------------------------------------------------------
1350SLresult android_audioPlayer_getConfig(CAudioPlayer* ap, const SLchar *configKey,
1351        SLuint32* pValueSize, void *pConfigValue) {
1352
1353    SLresult result;
1354
1355    assert(NULL != ap && NULL != configKey && NULL != pValueSize);
1356    if (strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_STREAM_TYPE) == 0) {
1357
1358        // stream type
1359        if (NULL == pConfigValue) {
1360            result = SL_RESULT_SUCCESS;
1361        } else if (KEY_STREAM_TYPE_PARAMSIZE > *pValueSize) {
1362            SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW);
1363            result = SL_RESULT_BUFFER_INSUFFICIENT;
1364        } else {
1365            result = audioPlayer_getStreamType(ap, (SLint32*)pConfigValue);
1366        }
1367        *pValueSize = KEY_STREAM_TYPE_PARAMSIZE;
1368
1369    } else {
1370        SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY);
1371        result = SL_RESULT_PARAMETER_INVALID;
1372    }
1373
1374    return result;
1375}
1376
1377
1378// Called from android_audioPlayer_realize for a PCM buffer queue player
1379// to determine if it can use a fast track.
1380static bool canUseFastTrack(CAudioPlayer *pAudioPlayer)
1381{
1382    assert(pAudioPlayer->mAndroidObjType == AUDIOPLAYER_FROM_PCM_BUFFERQUEUE);
1383
1384    // no need to check the buffer queue size, application side
1385    // double-buffering (and more) is not a requirement for using fast tracks
1386
1387    // Check a blacklist of interfaces that are incompatible with fast tracks.
1388    // The alternative, to check a whitelist of compatible interfaces, is
1389    // more maintainable but is too slow.  As a compromise, in a debug build
1390    // we use both methods and warn if they produce different results.
1391    // In release builds, we only use the blacklist method.
1392    // If a blacklisted interface is added after realization using
1393    // DynamicInterfaceManagement::AddInterface,
1394    // then this won't be detected but the interface will be ineffective.
1395    bool blacklistResult = true;
1396    static const unsigned blacklist[] = {
1397        MPH_BASSBOOST,
1398        MPH_EFFECTSEND,
1399        MPH_ENVIRONMENTALREVERB,
1400        MPH_EQUALIZER,
1401        MPH_PLAYBACKRATE,
1402        MPH_PRESETREVERB,
1403        MPH_VIRTUALIZER,
1404        MPH_ANDROIDEFFECT,
1405        MPH_ANDROIDEFFECTSEND,
1406        // FIXME The problem with a blacklist is remembering to add new interfaces here
1407    };
1408    for (unsigned i = 0; i < sizeof(blacklist)/sizeof(blacklist[0]); ++i) {
1409        if (IsInterfaceInitialized(&pAudioPlayer->mObject, blacklist[i])) {
1410            blacklistResult = false;
1411            break;
1412        }
1413    }
1414#if LOG_NDEBUG == 0
1415    bool whitelistResult = true;
1416    static const unsigned whitelist[] = {
1417        MPH_BUFFERQUEUE,
1418        MPH_DYNAMICINTERFACEMANAGEMENT,
1419        MPH_METADATAEXTRACTION,
1420        MPH_MUTESOLO,
1421        MPH_OBJECT,
1422        MPH_PLAY,
1423        MPH_PREFETCHSTATUS,
1424        MPH_VOLUME,
1425        MPH_ANDROIDCONFIGURATION,
1426        MPH_ANDROIDSIMPLEBUFFERQUEUE,
1427        MPH_ANDROIDBUFFERQUEUESOURCE,
1428    };
1429    for (unsigned mph = MPH_MIN; mph < MPH_MAX; ++mph) {
1430        for (unsigned i = 0; i < sizeof(whitelist)/sizeof(whitelist[0]); ++i) {
1431            if (mph == whitelist[i]) {
1432                goto compatible;
1433            }
1434        }
1435        if (IsInterfaceInitialized(&pAudioPlayer->mObject, mph)) {
1436            whitelistResult = false;
1437            break;
1438        }
1439compatible: ;
1440    }
1441    if (whitelistResult != blacklistResult) {
1442        SL_LOGW("whitelistResult != blacklistResult");
1443        // and use blacklistResult below
1444    }
1445#endif
1446    return blacklistResult;
1447}
1448
1449
1450//-----------------------------------------------------------------------------
1451// FIXME abstract out the diff between CMediaPlayer and CAudioPlayer
1452SLresult android_audioPlayer_realize(CAudioPlayer *pAudioPlayer, SLboolean async) {
1453
1454    SLresult result = SL_RESULT_SUCCESS;
1455    SL_LOGV("Realize pAudioPlayer=%p", pAudioPlayer);
1456
1457    AudioPlayback_Parameters app;
1458    app.sessionId = pAudioPlayer->mSessionId;
1459    app.streamType = pAudioPlayer->mStreamType;
1460
1461    switch (pAudioPlayer->mAndroidObjType) {
1462
1463    //-----------------------------------
1464    // AudioTrack
1465    case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: {
1466        // initialize platform-specific CAudioPlayer fields
1467
1468        SLDataFormat_PCM *df_pcm = (SLDataFormat_PCM *)
1469                pAudioPlayer->mDynamicSource.mDataSource->pFormat;
1470
1471        uint32_t sampleRate = sles_to_android_sampleRate(df_pcm->samplesPerSec);
1472
1473        audio_channel_mask_t channelMask;
1474        channelMask = sles_to_audio_output_channel_mask(df_pcm->channelMask);
1475
1476        // To maintain backward compatibility with previous releases, ignore
1477        // channel masks that are not indexed.
1478        if (channelMask == AUDIO_CHANNEL_INVALID
1479                || audio_channel_mask_get_representation(channelMask)
1480                        == AUDIO_CHANNEL_REPRESENTATION_POSITION) {
1481            channelMask = audio_channel_out_mask_from_count(df_pcm->numChannels);
1482            SL_LOGI("Emulating old channel mask behavior "
1483                    "(ignoring positional mask %#x, using default mask %#x based on "
1484                    "channel count of %d)", df_pcm->channelMask, channelMask,
1485                    df_pcm->numChannels);
1486        }
1487        SL_LOGV("AudioPlayer: mapped SLES channel mask %#x to android channel mask %#x",
1488            df_pcm->channelMask,
1489            channelMask);
1490
1491        audio_output_flags_t policy;
1492        if (canUseFastTrack(pAudioPlayer)) {
1493            policy = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_RAW);
1494        } else {
1495            policy = AUDIO_OUTPUT_FLAG_NONE;
1496        }
1497
1498        pAudioPlayer->mAudioTrack = new android::AudioTrack(
1499                pAudioPlayer->mStreamType,                           // streamType
1500                sampleRate,                                          // sampleRate
1501                sles_to_android_sampleFormat(df_pcm),                // format
1502                channelMask,                                         // channel mask
1503                0,                                                   // frameCount
1504                policy,                                              // flags
1505                audioTrack_callBack_pullFromBuffQueue,               // callback
1506                (void *) pAudioPlayer,                               // user
1507                0,     // FIXME find appropriate frame count         // notificationFrame
1508                pAudioPlayer->mSessionId);
1509        android::status_t status = pAudioPlayer->mAudioTrack->initCheck();
1510        if (status != android::NO_ERROR) {
1511            SL_LOGE("AudioTrack::initCheck status %u", status);
1512            // FIXME should return a more specific result depending on status
1513            result = SL_RESULT_CONTENT_UNSUPPORTED;
1514            pAudioPlayer->mAudioTrack.clear();
1515            return result;
1516        }
1517
1518        // initialize platform-independent CAudioPlayer fields
1519
1520        pAudioPlayer->mNumChannels = df_pcm->numChannels;
1521        pAudioPlayer->mSampleRateMilliHz = df_pcm->samplesPerSec; // Note: bad field name in SL ES
1522
1523        // This use case does not have a separate "prepare" step
1524        pAudioPlayer->mAndroidObjState = ANDROID_READY;
1525
1526        // If there is a JavaAudioRoutingProxy associated with this player, hook it up...
1527        JNIEnv* j_env = NULL;
1528        jclass clsAudioTrack = NULL;
1529        jmethodID midRoutingProxy_connect = NULL;
1530        if (pAudioPlayer->mAndroidConfiguration.mRoutingProxy != NULL &&
1531                (j_env = android::AndroidRuntime::getJNIEnv()) != NULL &&
1532                (clsAudioTrack = j_env->FindClass("android/media/AudioTrack")) != NULL &&
1533                (midRoutingProxy_connect =
1534                    j_env->GetMethodID(clsAudioTrack, "deferred_connect", "(J)V")) != NULL) {
1535            j_env->ExceptionClear();
1536            j_env->CallVoidMethod(pAudioPlayer->mAndroidConfiguration.mRoutingProxy,
1537                                  midRoutingProxy_connect,
1538                                  pAudioPlayer->mAudioTrack.get());
1539            if (j_env->ExceptionCheck()) {
1540                SL_LOGE("Java exception releasing recorder routing object.");
1541                result = SL_RESULT_INTERNAL_ERROR;
1542            }
1543        }
1544    }
1545        break;
1546
1547    //-----------------------------------
1548    // MediaPlayer
1549    case AUDIOPLAYER_FROM_URIFD: {
1550        pAudioPlayer->mAPlayer = new android::LocAVPlayer(&app, false /*hasVideo*/);
1551        pAudioPlayer->mAPlayer->init(sfplayer_handlePrefetchEvent,
1552                        (void*)pAudioPlayer /*notifUSer*/);
1553
1554        switch (pAudioPlayer->mDataSource.mLocator.mLocatorType) {
1555            case SL_DATALOCATOR_URI: {
1556                // The legacy implementation ran Stagefright within the application process, and
1557                // so allowed local pathnames specified by URI that were openable by
1558                // the application but were not openable by mediaserver.
1559                // The current implementation runs Stagefright (mostly) within mediaserver,
1560                // which runs as a different UID and likely a different current working directory.
1561                // For backwards compatibility with any applications which may have relied on the
1562                // previous behavior, we convert an openable file URI into an FD.
1563                // Note that unlike SL_DATALOCATOR_ANDROIDFD, this FD is owned by us
1564                // and so we close it as soon as we've passed it (via Binder dup) to mediaserver.
1565                const char *uri = (const char *)pAudioPlayer->mDataSource.mLocator.mURI.URI;
1566                if (!isDistantProtocol(uri)) {
1567                    // don't touch the original uri, we may need it later
1568                    const char *pathname = uri;
1569                    // skip over an optional leading file:// prefix
1570                    if (!strncasecmp(pathname, "file://", 7)) {
1571                        pathname += 7;
1572                    }
1573                    // attempt to open it as a file using the application's credentials
1574                    int fd = ::open(pathname, O_RDONLY);
1575                    if (fd >= 0) {
1576                        // if open is successful, then check to see if it's a regular file
1577                        struct stat statbuf;
1578                        if (!::fstat(fd, &statbuf) && S_ISREG(statbuf.st_mode)) {
1579                            // treat similarly to an FD data locator, but
1580                            // let setDataSource take responsibility for closing fd
1581                            pAudioPlayer->mAPlayer->setDataSource(fd, 0, statbuf.st_size, true);
1582                            break;
1583                        }
1584                        // we were able to open it, but it's not a file, so let mediaserver try
1585                        (void) ::close(fd);
1586                    }
1587                }
1588                // if either the URI didn't look like a file, or open failed, or not a file
1589                pAudioPlayer->mAPlayer->setDataSource(uri);
1590                } break;
1591            case SL_DATALOCATOR_ANDROIDFD: {
1592                int64_t offset = (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.offset;
1593                pAudioPlayer->mAPlayer->setDataSource(
1594                        (int)pAudioPlayer->mDataSource.mLocator.mFD.fd,
1595                        offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ?
1596                                (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset,
1597                        (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.length);
1598                }
1599                break;
1600            default:
1601                SL_LOGE(ERROR_PLAYERREALIZE_UNKNOWN_DATASOURCE_LOCATOR);
1602                break;
1603        }
1604
1605        }
1606        break;
1607
1608    //-----------------------------------
1609    // StreamPlayer
1610    case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: {
1611        android::StreamPlayer* splr = new android::StreamPlayer(&app, false /*hasVideo*/,
1612                &pAudioPlayer->mAndroidBufferQueue, pAudioPlayer->mCallbackProtector);
1613        pAudioPlayer->mAPlayer = splr;
1614        splr->init(sfplayer_handlePrefetchEvent, (void*)pAudioPlayer);
1615        }
1616        break;
1617
1618    //-----------------------------------
1619    // AudioToCbRenderer
1620    case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: {
1621        android::AudioToCbRenderer* decoder = new android::AudioToCbRenderer(&app);
1622        pAudioPlayer->mAPlayer = decoder;
1623        // configures the callback for the sink buffer queue
1624        decoder->setDataPushListener(adecoder_writeToBufferQueue, pAudioPlayer);
1625        // configures the callback for the notifications coming from the SF code
1626        decoder->init(sfplayer_handlePrefetchEvent, (void*)pAudioPlayer);
1627
1628        switch (pAudioPlayer->mDataSource.mLocator.mLocatorType) {
1629        case SL_DATALOCATOR_URI:
1630            decoder->setDataSource(
1631                    (const char*)pAudioPlayer->mDataSource.mLocator.mURI.URI);
1632            break;
1633        case SL_DATALOCATOR_ANDROIDFD: {
1634            int64_t offset = (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.offset;
1635            decoder->setDataSource(
1636                    (int)pAudioPlayer->mDataSource.mLocator.mFD.fd,
1637                    offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ?
1638                            (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset,
1639                            (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.length);
1640            }
1641            break;
1642        default:
1643            SL_LOGE(ERROR_PLAYERREALIZE_UNKNOWN_DATASOURCE_LOCATOR);
1644            break;
1645        }
1646
1647        }
1648        break;
1649
1650    //-----------------------------------
1651    // AacBqToPcmCbRenderer
1652    case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: {
1653        android::AacBqToPcmCbRenderer* bqtobq = new android::AacBqToPcmCbRenderer(&app,
1654                &pAudioPlayer->mAndroidBufferQueue);
1655        // configures the callback for the sink buffer queue
1656        bqtobq->setDataPushListener(adecoder_writeToBufferQueue, pAudioPlayer);
1657        pAudioPlayer->mAPlayer = bqtobq;
1658        // configures the callback for the notifications coming from the SF code,
1659        // but also implicitly configures the AndroidBufferQueue from which ADTS data is read
1660        pAudioPlayer->mAPlayer->init(sfplayer_handlePrefetchEvent, (void*)pAudioPlayer);
1661        }
1662        break;
1663
1664    //-----------------------------------
1665    default:
1666        SL_LOGE(ERROR_PLAYERREALIZE_UNEXPECTED_OBJECT_TYPE_D, pAudioPlayer->mAndroidObjType);
1667        result = SL_RESULT_INTERNAL_ERROR;
1668        break;
1669    }
1670
1671    // proceed with effect initialization
1672    // initialize EQ
1673    // FIXME use a table of effect descriptors when adding support for more effects
1674    if (memcmp(SL_IID_EQUALIZER, &pAudioPlayer->mEqualizer.mEqDescriptor.type,
1675            sizeof(effect_uuid_t)) == 0) {
1676        SL_LOGV("Need to initialize EQ for AudioPlayer=%p", pAudioPlayer);
1677        android_eq_init(pAudioPlayer->mSessionId, &pAudioPlayer->mEqualizer);
1678    }
1679    // initialize BassBoost
1680    if (memcmp(SL_IID_BASSBOOST, &pAudioPlayer->mBassBoost.mBassBoostDescriptor.type,
1681            sizeof(effect_uuid_t)) == 0) {
1682        SL_LOGV("Need to initialize BassBoost for AudioPlayer=%p", pAudioPlayer);
1683        android_bb_init(pAudioPlayer->mSessionId, &pAudioPlayer->mBassBoost);
1684    }
1685    // initialize Virtualizer
1686    if (memcmp(SL_IID_VIRTUALIZER, &pAudioPlayer->mVirtualizer.mVirtualizerDescriptor.type,
1687               sizeof(effect_uuid_t)) == 0) {
1688        SL_LOGV("Need to initialize Virtualizer for AudioPlayer=%p", pAudioPlayer);
1689        android_virt_init(pAudioPlayer->mSessionId, &pAudioPlayer->mVirtualizer);
1690    }
1691
1692    // initialize EffectSend
1693    // FIXME initialize EffectSend
1694
1695    return result;
1696}
1697
1698
1699//-----------------------------------------------------------------------------
1700/**
1701 * Called with a lock on AudioPlayer, and blocks until safe to destroy
1702 */
1703SLresult android_audioPlayer_preDestroy(CAudioPlayer *pAudioPlayer) {
1704    SL_LOGD("android_audioPlayer_preDestroy(%p)", pAudioPlayer);
1705    SLresult result = SL_RESULT_SUCCESS;
1706
1707    bool disableCallbacksBeforePreDestroy;
1708    switch (pAudioPlayer->mAndroidObjType) {
1709    // Not yet clear why this order is important, but it reduces detected deadlocks
1710    case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1711        disableCallbacksBeforePreDestroy = true;
1712        break;
1713    // Use the old behavior for all other use cases until proven
1714    // case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1715    default:
1716        disableCallbacksBeforePreDestroy = false;
1717        break;
1718    }
1719
1720    if (disableCallbacksBeforePreDestroy) {
1721        object_unlock_exclusive(&pAudioPlayer->mObject);
1722        if (pAudioPlayer->mCallbackProtector != 0) {
1723            pAudioPlayer->mCallbackProtector->requestCbExitAndWait();
1724        }
1725        object_lock_exclusive(&pAudioPlayer->mObject);
1726    }
1727
1728    if (pAudioPlayer->mAPlayer != 0) {
1729        pAudioPlayer->mAPlayer->preDestroy();
1730    }
1731    SL_LOGD("android_audioPlayer_preDestroy(%p) after mAPlayer->preDestroy()", pAudioPlayer);
1732
1733    if (!disableCallbacksBeforePreDestroy) {
1734        object_unlock_exclusive(&pAudioPlayer->mObject);
1735        if (pAudioPlayer->mCallbackProtector != 0) {
1736            pAudioPlayer->mCallbackProtector->requestCbExitAndWait();
1737        }
1738        object_lock_exclusive(&pAudioPlayer->mObject);
1739    }
1740
1741    return result;
1742}
1743
1744
1745//-----------------------------------------------------------------------------
1746SLresult android_audioPlayer_destroy(CAudioPlayer *pAudioPlayer) {
1747    SLresult result = SL_RESULT_SUCCESS;
1748    SL_LOGV("android_audioPlayer_destroy(%p)", pAudioPlayer);
1749    switch (pAudioPlayer->mAndroidObjType) {
1750
1751    case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
1752        // We own the audio track for PCM buffer queue players
1753        if (pAudioPlayer->mAudioTrack != 0) {
1754            pAudioPlayer->mAudioTrack->stop();
1755            // Note that there may still be another reference in post-unlock phase of SetPlayState
1756            pAudioPlayer->mAudioTrack.clear();
1757        }
1758        break;
1759
1760    case AUDIOPLAYER_FROM_URIFD:     // intended fall-through
1761    case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:    // intended fall-through
1762    case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: // intended fall-through
1763    case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1764        pAudioPlayer->mAPlayer.clear();
1765        break;
1766    //-----------------------------------
1767    default:
1768        SL_LOGE(ERROR_PLAYERDESTROY_UNEXPECTED_OBJECT_TYPE_D, pAudioPlayer->mAndroidObjType);
1769        result = SL_RESULT_INTERNAL_ERROR;
1770        break;
1771    }
1772
1773    // placeholder: not necessary yet as session ID lifetime doesn't extend beyond player
1774    // android::AudioSystem::releaseAudioSessionId(pAudioPlayer->mSessionId);
1775
1776    pAudioPlayer->mCallbackProtector.clear();
1777
1778    // explicit destructor
1779    pAudioPlayer->mAudioTrack.~sp();
1780    // note that SetPlayState(PLAYING) may still hold a reference
1781    pAudioPlayer->mCallbackProtector.~sp();
1782    pAudioPlayer->mAuxEffect.~sp();
1783    pAudioPlayer->mAPlayer.~sp();
1784
1785    return result;
1786}
1787
1788
1789//-----------------------------------------------------------------------------
1790SLresult android_audioPlayer_setPlaybackRateAndConstraints(CAudioPlayer *ap, SLpermille rate,
1791        SLuint32 constraints) {
1792    SLresult result = SL_RESULT_SUCCESS;
1793    switch (ap->mAndroidObjType) {
1794    case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: {
1795        // these asserts were already checked by the platform-independent layer
1796        assert((AUDIOTRACK_MIN_PLAYBACKRATE_PERMILLE <= rate) &&
1797                (rate <= AUDIOTRACK_MAX_PLAYBACKRATE_PERMILLE));
1798        assert(constraints & SL_RATEPROP_NOPITCHCORAUDIO);
1799        // get the content sample rate
1800        uint32_t contentRate = sles_to_android_sampleRate(ap->mSampleRateMilliHz);
1801        // apply the SL ES playback rate on the AudioTrack as a factor of its content sample rate
1802        if (ap->mAudioTrack != 0) {
1803            ap->mAudioTrack->setSampleRate(contentRate * (rate/1000.0f));
1804        }
1805        }
1806        break;
1807    case AUDIOPLAYER_FROM_URIFD: {
1808        assert((MEDIAPLAYER_MIN_PLAYBACKRATE_PERMILLE <= rate) &&
1809                        (rate <= MEDIAPLAYER_MAX_PLAYBACKRATE_PERMILLE));
1810        assert(constraints & SL_RATEPROP_NOPITCHCORAUDIO);
1811        // apply the SL ES playback rate on the GenericPlayer
1812        if (ap->mAPlayer != 0) {
1813            ap->mAPlayer->setPlaybackRate((int16_t)rate);
1814        }
1815        }
1816        break;
1817
1818    default:
1819        SL_LOGE("Unexpected object type %d", ap->mAndroidObjType);
1820        result = SL_RESULT_FEATURE_UNSUPPORTED;
1821        break;
1822    }
1823    return result;
1824}
1825
1826
1827//-----------------------------------------------------------------------------
1828// precondition
1829//  called with no lock held
1830//  ap != NULL
1831//  pItemCount != NULL
1832SLresult android_audioPlayer_metadata_getItemCount(CAudioPlayer *ap, SLuint32 *pItemCount) {
1833    if (ap->mAPlayer == 0) {
1834        return SL_RESULT_PARAMETER_INVALID;
1835    }
1836    switch (ap->mAndroidObjType) {
1837      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1838      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1839        {
1840            android::AudioSfDecoder* decoder =
1841                    static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get());
1842            *pItemCount = decoder->getPcmFormatKeyCount();
1843        }
1844        break;
1845      default:
1846        *pItemCount = 0;
1847        break;
1848    }
1849    return SL_RESULT_SUCCESS;
1850}
1851
1852
1853//-----------------------------------------------------------------------------
1854// precondition
1855//  called with no lock held
1856//  ap != NULL
1857//  pKeySize != NULL
1858SLresult android_audioPlayer_metadata_getKeySize(CAudioPlayer *ap,
1859        SLuint32 index, SLuint32 *pKeySize) {
1860    if (ap->mAPlayer == 0) {
1861        return SL_RESULT_PARAMETER_INVALID;
1862    }
1863    SLresult res = SL_RESULT_SUCCESS;
1864    switch (ap->mAndroidObjType) {
1865      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1866      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1867        {
1868            android::AudioSfDecoder* decoder =
1869                    static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get());
1870            SLuint32 keyNameSize = 0;
1871            if (!decoder->getPcmFormatKeySize(index, &keyNameSize)) {
1872                res = SL_RESULT_PARAMETER_INVALID;
1873            } else {
1874                // *pKeySize is the size of the region used to store the key name AND
1875                //   the information about the key (size, lang, encoding)
1876                *pKeySize = keyNameSize + sizeof(SLMetadataInfo);
1877            }
1878        }
1879        break;
1880      default:
1881        *pKeySize = 0;
1882        res = SL_RESULT_PARAMETER_INVALID;
1883        break;
1884    }
1885    return res;
1886}
1887
1888
1889//-----------------------------------------------------------------------------
1890// precondition
1891//  called with no lock held
1892//  ap != NULL
1893//  pKey != NULL
1894SLresult android_audioPlayer_metadata_getKey(CAudioPlayer *ap,
1895        SLuint32 index, SLuint32 size, SLMetadataInfo *pKey) {
1896    if (ap->mAPlayer == 0) {
1897        return SL_RESULT_PARAMETER_INVALID;
1898    }
1899    SLresult res = SL_RESULT_SUCCESS;
1900    switch (ap->mAndroidObjType) {
1901      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1902      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1903        {
1904            android::AudioSfDecoder* decoder =
1905                    static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get());
1906            if ((size < sizeof(SLMetadataInfo) ||
1907                    (!decoder->getPcmFormatKeyName(index, size - sizeof(SLMetadataInfo),
1908                            (char*)pKey->data)))) {
1909                res = SL_RESULT_PARAMETER_INVALID;
1910            } else {
1911                // successfully retrieved the key value, update the other fields
1912                pKey->encoding = SL_CHARACTERENCODING_UTF8;
1913                memcpy((char *) pKey->langCountry, "en", 3);
1914                pKey->size = strlen((char*)pKey->data) + 1;
1915            }
1916        }
1917        break;
1918      default:
1919        res = SL_RESULT_PARAMETER_INVALID;
1920        break;
1921    }
1922    return res;
1923}
1924
1925
1926//-----------------------------------------------------------------------------
1927// precondition
1928//  called with no lock held
1929//  ap != NULL
1930//  pValueSize != NULL
1931SLresult android_audioPlayer_metadata_getValueSize(CAudioPlayer *ap,
1932        SLuint32 index, SLuint32 *pValueSize) {
1933    if (ap->mAPlayer == 0) {
1934        return SL_RESULT_PARAMETER_INVALID;
1935    }
1936    SLresult res = SL_RESULT_SUCCESS;
1937    switch (ap->mAndroidObjType) {
1938      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1939      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1940        {
1941            android::AudioSfDecoder* decoder =
1942                    static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get());
1943            SLuint32 valueSize = 0;
1944            if (!decoder->getPcmFormatValueSize(index, &valueSize)) {
1945                res = SL_RESULT_PARAMETER_INVALID;
1946            } else {
1947                // *pValueSize is the size of the region used to store the key value AND
1948                //   the information about the value (size, lang, encoding)
1949                *pValueSize = valueSize + sizeof(SLMetadataInfo);
1950            }
1951        }
1952        break;
1953      default:
1954          *pValueSize = 0;
1955          res = SL_RESULT_PARAMETER_INVALID;
1956          break;
1957    }
1958    return res;
1959}
1960
1961
1962//-----------------------------------------------------------------------------
1963// precondition
1964//  called with no lock held
1965//  ap != NULL
1966//  pValue != NULL
1967SLresult android_audioPlayer_metadata_getValue(CAudioPlayer *ap,
1968        SLuint32 index, SLuint32 size, SLMetadataInfo *pValue) {
1969    if (ap->mAPlayer == 0) {
1970        return SL_RESULT_PARAMETER_INVALID;
1971    }
1972    SLresult res = SL_RESULT_SUCCESS;
1973    switch (ap->mAndroidObjType) {
1974      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1975      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1976        {
1977            android::AudioSfDecoder* decoder =
1978                    static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get());
1979            pValue->encoding = SL_CHARACTERENCODING_BINARY;
1980            memcpy((char *) pValue->langCountry, "en", 3); // applicable here?
1981            SLuint32 valueSize = 0;
1982            if ((size < sizeof(SLMetadataInfo)
1983                    || (!decoder->getPcmFormatValueSize(index, &valueSize))
1984                    || (!decoder->getPcmFormatKeyValue(index, size - sizeof(SLMetadataInfo),
1985                            (SLuint32*)pValue->data)))) {
1986                res = SL_RESULT_PARAMETER_INVALID;
1987            } else {
1988                pValue->size = valueSize;
1989            }
1990        }
1991        break;
1992      default:
1993        res = SL_RESULT_PARAMETER_INVALID;
1994        break;
1995    }
1996    return res;
1997}
1998
1999//-----------------------------------------------------------------------------
2000// preconditions
2001//  ap != NULL
2002//  mutex is locked
2003//  play state has changed
2004void android_audioPlayer_setPlayState(CAudioPlayer *ap) {
2005
2006    SLuint32 playState = ap->mPlay.mState;
2007
2008    switch (ap->mAndroidObjType) {
2009    case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
2010        switch (playState) {
2011        case SL_PLAYSTATE_STOPPED:
2012            SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_STOPPED");
2013            if (ap->mAudioTrack != 0) {
2014                ap->mAudioTrack->stop();
2015            }
2016            break;
2017        case SL_PLAYSTATE_PAUSED:
2018            SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PAUSED");
2019            if (ap->mAudioTrack != 0) {
2020                ap->mAudioTrack->pause();
2021            }
2022            break;
2023        case SL_PLAYSTATE_PLAYING:
2024            SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PLAYING");
2025            if (ap->mAudioTrack != 0) {
2026                // instead of ap->mAudioTrack->start();
2027                ap->mDeferredStart = true;
2028            }
2029            break;
2030        default:
2031            // checked by caller, should not happen
2032            break;
2033        }
2034        break;
2035
2036    case AUDIOPLAYER_FROM_URIFD:      // intended fall-through
2037    case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:     // intended fall-through
2038    case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:  // intended fall-through
2039    case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
2040        // FIXME report and use the return code to the lock mechanism, which is where play state
2041        //   changes are updated (see object_unlock_exclusive_attributes())
2042        aplayer_setPlayState(ap->mAPlayer, playState, &ap->mAndroidObjState);
2043        break;
2044    default:
2045        SL_LOGE(ERROR_PLAYERSETPLAYSTATE_UNEXPECTED_OBJECT_TYPE_D, ap->mAndroidObjType);
2046        break;
2047    }
2048}
2049
2050
2051//-----------------------------------------------------------------------------
2052// call when either player event flags, marker position, or position update period changes
2053void android_audioPlayer_usePlayEventMask(CAudioPlayer *ap) {
2054    IPlay *pPlayItf = &ap->mPlay;
2055    SLuint32 eventFlags = pPlayItf->mEventFlags;
2056    /*switch (ap->mAndroidObjType) {
2057    case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:*/
2058
2059    if (ap->mAPlayer != 0) {
2060        assert(ap->mAudioTrack == 0);
2061        ap->mAPlayer->setPlayEvents((int32_t) eventFlags, (int32_t) pPlayItf->mMarkerPosition,
2062                (int32_t) pPlayItf->mPositionUpdatePeriod);
2063        return;
2064    }
2065
2066    if (ap->mAudioTrack == 0) {
2067        return;
2068    }
2069
2070    if (eventFlags & SL_PLAYEVENT_HEADATMARKER) {
2071        ap->mAudioTrack->setMarkerPosition((uint32_t)((((int64_t)pPlayItf->mMarkerPosition
2072                * sles_to_android_sampleRate(ap->mSampleRateMilliHz)))/1000));
2073    } else {
2074        // clear marker
2075        ap->mAudioTrack->setMarkerPosition(0);
2076    }
2077
2078    if (eventFlags & SL_PLAYEVENT_HEADATNEWPOS) {
2079         ap->mAudioTrack->setPositionUpdatePeriod(
2080                (uint32_t)((((int64_t)pPlayItf->mPositionUpdatePeriod
2081                * sles_to_android_sampleRate(ap->mSampleRateMilliHz)))/1000));
2082    } else {
2083        // clear periodic update
2084        ap->mAudioTrack->setPositionUpdatePeriod(0);
2085    }
2086
2087    if (eventFlags & SL_PLAYEVENT_HEADATEND) {
2088        // nothing to do for SL_PLAYEVENT_HEADATEND, callback event will be checked against mask
2089    }
2090
2091    if (eventFlags & SL_PLAYEVENT_HEADMOVING) {
2092        // FIXME support SL_PLAYEVENT_HEADMOVING
2093        SL_LOGD("[ FIXME: IPlay_SetCallbackEventsMask(SL_PLAYEVENT_HEADMOVING) on an "
2094            "SL_OBJECTID_AUDIOPLAYER to be implemented ]");
2095    }
2096    if (eventFlags & SL_PLAYEVENT_HEADSTALLED) {
2097        // nothing to do for SL_PLAYEVENT_HEADSTALLED, callback event will be checked against mask
2098    }
2099
2100}
2101
2102
2103//-----------------------------------------------------------------------------
2104SLresult android_audioPlayer_getDuration(IPlay *pPlayItf, SLmillisecond *pDurMsec) {
2105    CAudioPlayer *ap = (CAudioPlayer *)pPlayItf->mThis;
2106    switch (ap->mAndroidObjType) {
2107
2108      case AUDIOPLAYER_FROM_URIFD:  // intended fall-through
2109      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: {
2110        int32_t durationMsec = ANDROID_UNKNOWN_TIME;
2111        if (ap->mAPlayer != 0) {
2112            ap->mAPlayer->getDurationMsec(&durationMsec);
2113        }
2114        *pDurMsec = durationMsec == ANDROID_UNKNOWN_TIME ? SL_TIME_UNKNOWN : durationMsec;
2115        break;
2116      }
2117
2118      case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
2119      case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
2120      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
2121      default: {
2122        *pDurMsec = SL_TIME_UNKNOWN;
2123      }
2124    }
2125    return SL_RESULT_SUCCESS;
2126}
2127
2128
2129//-----------------------------------------------------------------------------
2130void android_audioPlayer_getPosition(IPlay *pPlayItf, SLmillisecond *pPosMsec) {
2131    CAudioPlayer *ap = (CAudioPlayer *)pPlayItf->mThis;
2132    switch (ap->mAndroidObjType) {
2133
2134      case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
2135        if ((ap->mSampleRateMilliHz == UNKNOWN_SAMPLERATE) || (ap->mAudioTrack == 0)) {
2136            *pPosMsec = 0;
2137        } else {
2138            uint32_t positionInFrames;
2139            ap->mAudioTrack->getPosition(&positionInFrames);
2140            *pPosMsec = ((int64_t)positionInFrames * 1000) /
2141                    sles_to_android_sampleRate(ap->mSampleRateMilliHz);
2142        }
2143        break;
2144
2145      case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:    // intended fall-through
2146      case AUDIOPLAYER_FROM_URIFD:
2147      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
2148      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: {
2149        int32_t posMsec = ANDROID_UNKNOWN_TIME;
2150        if (ap->mAPlayer != 0) {
2151            ap->mAPlayer->getPositionMsec(&posMsec);
2152        }
2153        *pPosMsec = posMsec == ANDROID_UNKNOWN_TIME ? 0 : posMsec;
2154        break;
2155      }
2156
2157      default:
2158        *pPosMsec = 0;
2159    }
2160}
2161
2162
2163//-----------------------------------------------------------------------------
2164SLresult android_audioPlayer_seek(CAudioPlayer *ap, SLmillisecond posMsec) {
2165    SLresult result = SL_RESULT_SUCCESS;
2166
2167    switch (ap->mAndroidObjType) {
2168
2169      case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:      // intended fall-through
2170      case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:
2171      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
2172        result = SL_RESULT_FEATURE_UNSUPPORTED;
2173        break;
2174
2175      case AUDIOPLAYER_FROM_URIFD:                   // intended fall-through
2176      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
2177        if (ap->mAPlayer != 0) {
2178            ap->mAPlayer->seek(posMsec);
2179        }
2180        break;
2181
2182      default:
2183        break;
2184    }
2185    return result;
2186}
2187
2188
2189//-----------------------------------------------------------------------------
2190SLresult android_audioPlayer_loop(CAudioPlayer *ap, SLboolean loopEnable) {
2191    SLresult result = SL_RESULT_SUCCESS;
2192
2193    switch (ap->mAndroidObjType) {
2194    case AUDIOPLAYER_FROM_URIFD:
2195    // case AUDIOPLAY_FROM_URIFD_TO_PCM_BUFFERQUEUE:
2196    //      would actually work, but what's the point?
2197      if (ap->mAPlayer != 0) {
2198        ap->mAPlayer->loop((bool)loopEnable);
2199      }
2200      break;
2201    default:
2202      result = SL_RESULT_FEATURE_UNSUPPORTED;
2203      break;
2204    }
2205    return result;
2206}
2207
2208
2209//-----------------------------------------------------------------------------
2210SLresult android_audioPlayer_setBufferingUpdateThresholdPerMille(CAudioPlayer *ap,
2211        SLpermille threshold) {
2212    SLresult result = SL_RESULT_SUCCESS;
2213
2214    switch (ap->mAndroidObjType) {
2215      case AUDIOPLAYER_FROM_URIFD:
2216        if (ap->mAPlayer != 0) {
2217            ap->mAPlayer->setBufferingUpdateThreshold(threshold / 10);
2218        }
2219        break;
2220
2221      default: {}
2222    }
2223
2224    return result;
2225}
2226
2227
2228//-----------------------------------------------------------------------------
2229void android_audioPlayer_bufferQueue_onRefilled_l(CAudioPlayer *ap) {
2230    // the AudioTrack associated with the AudioPlayer receiving audio from a PCM buffer
2231    // queue was stopped when the queue become empty, we restart as soon as a new buffer
2232    // has been enqueued since we're in playing state
2233    if (ap->mAudioTrack != 0) {
2234        // instead of ap->mAudioTrack->start();
2235        ap->mDeferredStart = true;
2236    }
2237
2238    // when the queue became empty, an underflow on the prefetch status itf was sent. Now the queue
2239    // has received new data, signal it has sufficient data
2240    if (IsInterfaceInitialized(&ap->mObject, MPH_PREFETCHSTATUS)) {
2241        // we wouldn't have been called unless we were previously in the underflow state
2242        assert(SL_PREFETCHSTATUS_UNDERFLOW == ap->mPrefetchStatus.mStatus);
2243        assert(0 == ap->mPrefetchStatus.mLevel);
2244        ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_SUFFICIENTDATA;
2245        ap->mPrefetchStatus.mLevel = 1000;
2246        // callback or no callback?
2247        SLuint32 prefetchEvents = ap->mPrefetchStatus.mCallbackEventsMask &
2248                (SL_PREFETCHEVENT_STATUSCHANGE | SL_PREFETCHEVENT_FILLLEVELCHANGE);
2249        if (SL_PREFETCHEVENT_NONE != prefetchEvents) {
2250            ap->mPrefetchStatus.mDeferredPrefetchCallback = ap->mPrefetchStatus.mCallback;
2251            ap->mPrefetchStatus.mDeferredPrefetchContext  = ap->mPrefetchStatus.mContext;
2252            ap->mPrefetchStatus.mDeferredPrefetchEvents   = prefetchEvents;
2253        }
2254    }
2255}
2256
2257
2258//-----------------------------------------------------------------------------
2259/*
2260 * BufferQueue::Clear
2261 */
2262SLresult android_audioPlayer_bufferQueue_onClear(CAudioPlayer *ap) {
2263    SLresult result = SL_RESULT_SUCCESS;
2264
2265    switch (ap->mAndroidObjType) {
2266    //-----------------------------------
2267    // AudioTrack
2268    case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
2269        if (ap->mAudioTrack != 0) {
2270            ap->mAudioTrack->flush();
2271        }
2272        break;
2273    default:
2274        result = SL_RESULT_INTERNAL_ERROR;
2275        break;
2276    }
2277
2278    return result;
2279}
2280
2281
2282//-----------------------------------------------------------------------------
2283void android_audioPlayer_androidBufferQueue_clear_l(CAudioPlayer *ap) {
2284    switch (ap->mAndroidObjType) {
2285    case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:
2286      if (ap->mAPlayer != 0) {
2287        android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(ap->mAPlayer.get());
2288        splr->appClear_l();
2289      } break;
2290    case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
2291      // nothing to do here, fall through
2292    default:
2293      break;
2294    }
2295}
2296
2297void android_audioPlayer_androidBufferQueue_onRefilled_l(CAudioPlayer *ap) {
2298    switch (ap->mAndroidObjType) {
2299    case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:
2300      if (ap->mAPlayer != 0) {
2301        android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(ap->mAPlayer.get());
2302        splr->queueRefilled();
2303      } break;
2304    case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
2305      // FIXME this may require waking up the decoder if it is currently starved and isn't polling
2306    default:
2307      break;
2308    }
2309}
2310