AudioPlayer_to_android.cpp revision f930011635e970346ca31d0509a173a695023f27
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        // negative notificationFrames is the number of notifications (sub-buffers) per track buffer
1499        // for details see the explanation at frameworks/av/include/media/AudioTrack.h
1500        const int32_t notificationFrames = -pAudioPlayer->mBufferQueue.mNumBuffers;
1501
1502        pAudioPlayer->mAudioTrack = new android::AudioTrack(
1503                pAudioPlayer->mStreamType,                           // streamType
1504                sampleRate,                                          // sampleRate
1505                sles_to_android_sampleFormat(df_pcm),                // format
1506                channelMask,                                         // channel mask
1507                0,                                                   // frameCount
1508                policy,                                              // flags
1509                audioTrack_callBack_pullFromBuffQueue,               // callback
1510                (void *) pAudioPlayer,                               // user
1511                notificationFrames,                                  // see comment above
1512                pAudioPlayer->mSessionId);
1513        android::status_t status = pAudioPlayer->mAudioTrack->initCheck();
1514        if (status != android::NO_ERROR) {
1515            SL_LOGE("AudioTrack::initCheck status %u", status);
1516            // FIXME should return a more specific result depending on status
1517            result = SL_RESULT_CONTENT_UNSUPPORTED;
1518            pAudioPlayer->mAudioTrack.clear();
1519            return result;
1520        }
1521
1522        // initialize platform-independent CAudioPlayer fields
1523
1524        pAudioPlayer->mNumChannels = df_pcm->numChannels;
1525        pAudioPlayer->mSampleRateMilliHz = df_pcm->samplesPerSec; // Note: bad field name in SL ES
1526
1527        // This use case does not have a separate "prepare" step
1528        pAudioPlayer->mAndroidObjState = ANDROID_READY;
1529
1530        // If there is a JavaAudioRoutingProxy associated with this player, hook it up...
1531        JNIEnv* j_env = NULL;
1532        jclass clsAudioTrack = NULL;
1533        jmethodID midRoutingProxy_connect = NULL;
1534        if (pAudioPlayer->mAndroidConfiguration.mRoutingProxy != NULL &&
1535                (j_env = android::AndroidRuntime::getJNIEnv()) != NULL &&
1536                (clsAudioTrack = j_env->FindClass("android/media/AudioTrack")) != NULL &&
1537                (midRoutingProxy_connect =
1538                    j_env->GetMethodID(clsAudioTrack, "deferred_connect", "(J)V")) != NULL) {
1539            j_env->ExceptionClear();
1540            j_env->CallVoidMethod(pAudioPlayer->mAndroidConfiguration.mRoutingProxy,
1541                                  midRoutingProxy_connect,
1542                                  pAudioPlayer->mAudioTrack.get());
1543            if (j_env->ExceptionCheck()) {
1544                SL_LOGE("Java exception releasing recorder routing object.");
1545                result = SL_RESULT_INTERNAL_ERROR;
1546            }
1547        }
1548    }
1549        break;
1550
1551    //-----------------------------------
1552    // MediaPlayer
1553    case AUDIOPLAYER_FROM_URIFD: {
1554        pAudioPlayer->mAPlayer = new android::LocAVPlayer(&app, false /*hasVideo*/);
1555        pAudioPlayer->mAPlayer->init(sfplayer_handlePrefetchEvent,
1556                        (void*)pAudioPlayer /*notifUSer*/);
1557
1558        switch (pAudioPlayer->mDataSource.mLocator.mLocatorType) {
1559            case SL_DATALOCATOR_URI: {
1560                // The legacy implementation ran Stagefright within the application process, and
1561                // so allowed local pathnames specified by URI that were openable by
1562                // the application but were not openable by mediaserver.
1563                // The current implementation runs Stagefright (mostly) within mediaserver,
1564                // which runs as a different UID and likely a different current working directory.
1565                // For backwards compatibility with any applications which may have relied on the
1566                // previous behavior, we convert an openable file URI into an FD.
1567                // Note that unlike SL_DATALOCATOR_ANDROIDFD, this FD is owned by us
1568                // and so we close it as soon as we've passed it (via Binder dup) to mediaserver.
1569                const char *uri = (const char *)pAudioPlayer->mDataSource.mLocator.mURI.URI;
1570                if (!isDistantProtocol(uri)) {
1571                    // don't touch the original uri, we may need it later
1572                    const char *pathname = uri;
1573                    // skip over an optional leading file:// prefix
1574                    if (!strncasecmp(pathname, "file://", 7)) {
1575                        pathname += 7;
1576                    }
1577                    // attempt to open it as a file using the application's credentials
1578                    int fd = ::open(pathname, O_RDONLY);
1579                    if (fd >= 0) {
1580                        // if open is successful, then check to see if it's a regular file
1581                        struct stat statbuf;
1582                        if (!::fstat(fd, &statbuf) && S_ISREG(statbuf.st_mode)) {
1583                            // treat similarly to an FD data locator, but
1584                            // let setDataSource take responsibility for closing fd
1585                            pAudioPlayer->mAPlayer->setDataSource(fd, 0, statbuf.st_size, true);
1586                            break;
1587                        }
1588                        // we were able to open it, but it's not a file, so let mediaserver try
1589                        (void) ::close(fd);
1590                    }
1591                }
1592                // if either the URI didn't look like a file, or open failed, or not a file
1593                pAudioPlayer->mAPlayer->setDataSource(uri);
1594                } break;
1595            case SL_DATALOCATOR_ANDROIDFD: {
1596                int64_t offset = (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.offset;
1597                pAudioPlayer->mAPlayer->setDataSource(
1598                        (int)pAudioPlayer->mDataSource.mLocator.mFD.fd,
1599                        offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ?
1600                                (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset,
1601                        (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.length);
1602                }
1603                break;
1604            default:
1605                SL_LOGE(ERROR_PLAYERREALIZE_UNKNOWN_DATASOURCE_LOCATOR);
1606                break;
1607        }
1608
1609        }
1610        break;
1611
1612    //-----------------------------------
1613    // StreamPlayer
1614    case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: {
1615        android::StreamPlayer* splr = new android::StreamPlayer(&app, false /*hasVideo*/,
1616                &pAudioPlayer->mAndroidBufferQueue, pAudioPlayer->mCallbackProtector);
1617        pAudioPlayer->mAPlayer = splr;
1618        splr->init(sfplayer_handlePrefetchEvent, (void*)pAudioPlayer);
1619        }
1620        break;
1621
1622    //-----------------------------------
1623    // AudioToCbRenderer
1624    case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: {
1625        android::AudioToCbRenderer* decoder = new android::AudioToCbRenderer(&app);
1626        pAudioPlayer->mAPlayer = decoder;
1627        // configures the callback for the sink buffer queue
1628        decoder->setDataPushListener(adecoder_writeToBufferQueue, pAudioPlayer);
1629        // configures the callback for the notifications coming from the SF code
1630        decoder->init(sfplayer_handlePrefetchEvent, (void*)pAudioPlayer);
1631
1632        switch (pAudioPlayer->mDataSource.mLocator.mLocatorType) {
1633        case SL_DATALOCATOR_URI:
1634            decoder->setDataSource(
1635                    (const char*)pAudioPlayer->mDataSource.mLocator.mURI.URI);
1636            break;
1637        case SL_DATALOCATOR_ANDROIDFD: {
1638            int64_t offset = (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.offset;
1639            decoder->setDataSource(
1640                    (int)pAudioPlayer->mDataSource.mLocator.mFD.fd,
1641                    offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ?
1642                            (int64_t)PLAYER_FD_FIND_FILE_SIZE : offset,
1643                            (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.length);
1644            }
1645            break;
1646        default:
1647            SL_LOGE(ERROR_PLAYERREALIZE_UNKNOWN_DATASOURCE_LOCATOR);
1648            break;
1649        }
1650
1651        }
1652        break;
1653
1654    //-----------------------------------
1655    // AacBqToPcmCbRenderer
1656    case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: {
1657        android::AacBqToPcmCbRenderer* bqtobq = new android::AacBqToPcmCbRenderer(&app,
1658                &pAudioPlayer->mAndroidBufferQueue);
1659        // configures the callback for the sink buffer queue
1660        bqtobq->setDataPushListener(adecoder_writeToBufferQueue, pAudioPlayer);
1661        pAudioPlayer->mAPlayer = bqtobq;
1662        // configures the callback for the notifications coming from the SF code,
1663        // but also implicitly configures the AndroidBufferQueue from which ADTS data is read
1664        pAudioPlayer->mAPlayer->init(sfplayer_handlePrefetchEvent, (void*)pAudioPlayer);
1665        }
1666        break;
1667
1668    //-----------------------------------
1669    default:
1670        SL_LOGE(ERROR_PLAYERREALIZE_UNEXPECTED_OBJECT_TYPE_D, pAudioPlayer->mAndroidObjType);
1671        result = SL_RESULT_INTERNAL_ERROR;
1672        break;
1673    }
1674
1675    // proceed with effect initialization
1676    // initialize EQ
1677    // FIXME use a table of effect descriptors when adding support for more effects
1678    if (memcmp(SL_IID_EQUALIZER, &pAudioPlayer->mEqualizer.mEqDescriptor.type,
1679            sizeof(effect_uuid_t)) == 0) {
1680        SL_LOGV("Need to initialize EQ for AudioPlayer=%p", pAudioPlayer);
1681        android_eq_init(pAudioPlayer->mSessionId, &pAudioPlayer->mEqualizer);
1682    }
1683    // initialize BassBoost
1684    if (memcmp(SL_IID_BASSBOOST, &pAudioPlayer->mBassBoost.mBassBoostDescriptor.type,
1685            sizeof(effect_uuid_t)) == 0) {
1686        SL_LOGV("Need to initialize BassBoost for AudioPlayer=%p", pAudioPlayer);
1687        android_bb_init(pAudioPlayer->mSessionId, &pAudioPlayer->mBassBoost);
1688    }
1689    // initialize Virtualizer
1690    if (memcmp(SL_IID_VIRTUALIZER, &pAudioPlayer->mVirtualizer.mVirtualizerDescriptor.type,
1691               sizeof(effect_uuid_t)) == 0) {
1692        SL_LOGV("Need to initialize Virtualizer for AudioPlayer=%p", pAudioPlayer);
1693        android_virt_init(pAudioPlayer->mSessionId, &pAudioPlayer->mVirtualizer);
1694    }
1695
1696    // initialize EffectSend
1697    // FIXME initialize EffectSend
1698
1699    return result;
1700}
1701
1702
1703//-----------------------------------------------------------------------------
1704/**
1705 * Called with a lock on AudioPlayer, and blocks until safe to destroy
1706 */
1707SLresult android_audioPlayer_preDestroy(CAudioPlayer *pAudioPlayer) {
1708    SL_LOGD("android_audioPlayer_preDestroy(%p)", pAudioPlayer);
1709    SLresult result = SL_RESULT_SUCCESS;
1710
1711    bool disableCallbacksBeforePreDestroy;
1712    switch (pAudioPlayer->mAndroidObjType) {
1713    // Not yet clear why this order is important, but it reduces detected deadlocks
1714    case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1715        disableCallbacksBeforePreDestroy = true;
1716        break;
1717    // Use the old behavior for all other use cases until proven
1718    // case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1719    default:
1720        disableCallbacksBeforePreDestroy = false;
1721        break;
1722    }
1723
1724    if (disableCallbacksBeforePreDestroy) {
1725        object_unlock_exclusive(&pAudioPlayer->mObject);
1726        if (pAudioPlayer->mCallbackProtector != 0) {
1727            pAudioPlayer->mCallbackProtector->requestCbExitAndWait();
1728        }
1729        object_lock_exclusive(&pAudioPlayer->mObject);
1730    }
1731
1732    if (pAudioPlayer->mAPlayer != 0) {
1733        pAudioPlayer->mAPlayer->preDestroy();
1734    }
1735    SL_LOGD("android_audioPlayer_preDestroy(%p) after mAPlayer->preDestroy()", pAudioPlayer);
1736
1737    if (!disableCallbacksBeforePreDestroy) {
1738        object_unlock_exclusive(&pAudioPlayer->mObject);
1739        if (pAudioPlayer->mCallbackProtector != 0) {
1740            pAudioPlayer->mCallbackProtector->requestCbExitAndWait();
1741        }
1742        object_lock_exclusive(&pAudioPlayer->mObject);
1743    }
1744
1745    return result;
1746}
1747
1748
1749//-----------------------------------------------------------------------------
1750SLresult android_audioPlayer_destroy(CAudioPlayer *pAudioPlayer) {
1751    SLresult result = SL_RESULT_SUCCESS;
1752    SL_LOGV("android_audioPlayer_destroy(%p)", pAudioPlayer);
1753    switch (pAudioPlayer->mAndroidObjType) {
1754
1755    case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
1756        // We own the audio track for PCM buffer queue players
1757        if (pAudioPlayer->mAudioTrack != 0) {
1758            pAudioPlayer->mAudioTrack->stop();
1759            // Note that there may still be another reference in post-unlock phase of SetPlayState
1760            pAudioPlayer->mAudioTrack.clear();
1761        }
1762        break;
1763
1764    case AUDIOPLAYER_FROM_URIFD:     // intended fall-through
1765    case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:    // intended fall-through
1766    case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: // intended fall-through
1767    case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1768        pAudioPlayer->mAPlayer.clear();
1769        break;
1770    //-----------------------------------
1771    default:
1772        SL_LOGE(ERROR_PLAYERDESTROY_UNEXPECTED_OBJECT_TYPE_D, pAudioPlayer->mAndroidObjType);
1773        result = SL_RESULT_INTERNAL_ERROR;
1774        break;
1775    }
1776
1777    // placeholder: not necessary yet as session ID lifetime doesn't extend beyond player
1778    // android::AudioSystem::releaseAudioSessionId(pAudioPlayer->mSessionId);
1779
1780    pAudioPlayer->mCallbackProtector.clear();
1781
1782    // explicit destructor
1783    pAudioPlayer->mAudioTrack.~sp();
1784    // note that SetPlayState(PLAYING) may still hold a reference
1785    pAudioPlayer->mCallbackProtector.~sp();
1786    pAudioPlayer->mAuxEffect.~sp();
1787    pAudioPlayer->mAPlayer.~sp();
1788
1789    return result;
1790}
1791
1792
1793//-----------------------------------------------------------------------------
1794SLresult android_audioPlayer_setPlaybackRateAndConstraints(CAudioPlayer *ap, SLpermille rate,
1795        SLuint32 constraints) {
1796    SLresult result = SL_RESULT_SUCCESS;
1797    switch (ap->mAndroidObjType) {
1798    case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE: {
1799        // these asserts were already checked by the platform-independent layer
1800        assert((AUDIOTRACK_MIN_PLAYBACKRATE_PERMILLE <= rate) &&
1801                (rate <= AUDIOTRACK_MAX_PLAYBACKRATE_PERMILLE));
1802        assert(constraints & SL_RATEPROP_NOPITCHCORAUDIO);
1803        // get the content sample rate
1804        uint32_t contentRate = sles_to_android_sampleRate(ap->mSampleRateMilliHz);
1805        // apply the SL ES playback rate on the AudioTrack as a factor of its content sample rate
1806        if (ap->mAudioTrack != 0) {
1807            ap->mAudioTrack->setSampleRate(contentRate * (rate/1000.0f));
1808        }
1809        }
1810        break;
1811    case AUDIOPLAYER_FROM_URIFD: {
1812        assert((MEDIAPLAYER_MIN_PLAYBACKRATE_PERMILLE <= rate) &&
1813                        (rate <= MEDIAPLAYER_MAX_PLAYBACKRATE_PERMILLE));
1814        assert(constraints & SL_RATEPROP_NOPITCHCORAUDIO);
1815        // apply the SL ES playback rate on the GenericPlayer
1816        if (ap->mAPlayer != 0) {
1817            ap->mAPlayer->setPlaybackRate((int16_t)rate);
1818        }
1819        }
1820        break;
1821
1822    default:
1823        SL_LOGE("Unexpected object type %d", ap->mAndroidObjType);
1824        result = SL_RESULT_FEATURE_UNSUPPORTED;
1825        break;
1826    }
1827    return result;
1828}
1829
1830
1831//-----------------------------------------------------------------------------
1832// precondition
1833//  called with no lock held
1834//  ap != NULL
1835//  pItemCount != NULL
1836SLresult android_audioPlayer_metadata_getItemCount(CAudioPlayer *ap, SLuint32 *pItemCount) {
1837    if (ap->mAPlayer == 0) {
1838        return SL_RESULT_PARAMETER_INVALID;
1839    }
1840    switch (ap->mAndroidObjType) {
1841      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1842      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1843        {
1844            android::AudioSfDecoder* decoder =
1845                    static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get());
1846            *pItemCount = decoder->getPcmFormatKeyCount();
1847        }
1848        break;
1849      default:
1850        *pItemCount = 0;
1851        break;
1852    }
1853    return SL_RESULT_SUCCESS;
1854}
1855
1856
1857//-----------------------------------------------------------------------------
1858// precondition
1859//  called with no lock held
1860//  ap != NULL
1861//  pKeySize != NULL
1862SLresult android_audioPlayer_metadata_getKeySize(CAudioPlayer *ap,
1863        SLuint32 index, SLuint32 *pKeySize) {
1864    if (ap->mAPlayer == 0) {
1865        return SL_RESULT_PARAMETER_INVALID;
1866    }
1867    SLresult res = SL_RESULT_SUCCESS;
1868    switch (ap->mAndroidObjType) {
1869      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1870      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1871        {
1872            android::AudioSfDecoder* decoder =
1873                    static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get());
1874            SLuint32 keyNameSize = 0;
1875            if (!decoder->getPcmFormatKeySize(index, &keyNameSize)) {
1876                res = SL_RESULT_PARAMETER_INVALID;
1877            } else {
1878                // *pKeySize is the size of the region used to store the key name AND
1879                //   the information about the key (size, lang, encoding)
1880                *pKeySize = keyNameSize + sizeof(SLMetadataInfo);
1881            }
1882        }
1883        break;
1884      default:
1885        *pKeySize = 0;
1886        res = SL_RESULT_PARAMETER_INVALID;
1887        break;
1888    }
1889    return res;
1890}
1891
1892
1893//-----------------------------------------------------------------------------
1894// precondition
1895//  called with no lock held
1896//  ap != NULL
1897//  pKey != NULL
1898SLresult android_audioPlayer_metadata_getKey(CAudioPlayer *ap,
1899        SLuint32 index, SLuint32 size, SLMetadataInfo *pKey) {
1900    if (ap->mAPlayer == 0) {
1901        return SL_RESULT_PARAMETER_INVALID;
1902    }
1903    SLresult res = SL_RESULT_SUCCESS;
1904    switch (ap->mAndroidObjType) {
1905      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1906      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1907        {
1908            android::AudioSfDecoder* decoder =
1909                    static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get());
1910            if ((size < sizeof(SLMetadataInfo) ||
1911                    (!decoder->getPcmFormatKeyName(index, size - sizeof(SLMetadataInfo),
1912                            (char*)pKey->data)))) {
1913                res = SL_RESULT_PARAMETER_INVALID;
1914            } else {
1915                // successfully retrieved the key value, update the other fields
1916                pKey->encoding = SL_CHARACTERENCODING_UTF8;
1917                memcpy((char *) pKey->langCountry, "en", 3);
1918                pKey->size = strlen((char*)pKey->data) + 1;
1919            }
1920        }
1921        break;
1922      default:
1923        res = SL_RESULT_PARAMETER_INVALID;
1924        break;
1925    }
1926    return res;
1927}
1928
1929
1930//-----------------------------------------------------------------------------
1931// precondition
1932//  called with no lock held
1933//  ap != NULL
1934//  pValueSize != NULL
1935SLresult android_audioPlayer_metadata_getValueSize(CAudioPlayer *ap,
1936        SLuint32 index, SLuint32 *pValueSize) {
1937    if (ap->mAPlayer == 0) {
1938        return SL_RESULT_PARAMETER_INVALID;
1939    }
1940    SLresult res = SL_RESULT_SUCCESS;
1941    switch (ap->mAndroidObjType) {
1942      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1943      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1944        {
1945            android::AudioSfDecoder* decoder =
1946                    static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get());
1947            SLuint32 valueSize = 0;
1948            if (!decoder->getPcmFormatValueSize(index, &valueSize)) {
1949                res = SL_RESULT_PARAMETER_INVALID;
1950            } else {
1951                // *pValueSize is the size of the region used to store the key value AND
1952                //   the information about the value (size, lang, encoding)
1953                *pValueSize = valueSize + sizeof(SLMetadataInfo);
1954            }
1955        }
1956        break;
1957      default:
1958          *pValueSize = 0;
1959          res = SL_RESULT_PARAMETER_INVALID;
1960          break;
1961    }
1962    return res;
1963}
1964
1965
1966//-----------------------------------------------------------------------------
1967// precondition
1968//  called with no lock held
1969//  ap != NULL
1970//  pValue != NULL
1971SLresult android_audioPlayer_metadata_getValue(CAudioPlayer *ap,
1972        SLuint32 index, SLuint32 size, SLMetadataInfo *pValue) {
1973    if (ap->mAPlayer == 0) {
1974        return SL_RESULT_PARAMETER_INVALID;
1975    }
1976    SLresult res = SL_RESULT_SUCCESS;
1977    switch (ap->mAndroidObjType) {
1978      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
1979      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
1980        {
1981            android::AudioSfDecoder* decoder =
1982                    static_cast<android::AudioSfDecoder*>(ap->mAPlayer.get());
1983            pValue->encoding = SL_CHARACTERENCODING_BINARY;
1984            memcpy((char *) pValue->langCountry, "en", 3); // applicable here?
1985            SLuint32 valueSize = 0;
1986            if ((size < sizeof(SLMetadataInfo)
1987                    || (!decoder->getPcmFormatValueSize(index, &valueSize))
1988                    || (!decoder->getPcmFormatKeyValue(index, size - sizeof(SLMetadataInfo),
1989                            (SLuint32*)pValue->data)))) {
1990                res = SL_RESULT_PARAMETER_INVALID;
1991            } else {
1992                pValue->size = valueSize;
1993            }
1994        }
1995        break;
1996      default:
1997        res = SL_RESULT_PARAMETER_INVALID;
1998        break;
1999    }
2000    return res;
2001}
2002
2003//-----------------------------------------------------------------------------
2004// preconditions
2005//  ap != NULL
2006//  mutex is locked
2007//  play state has changed
2008void android_audioPlayer_setPlayState(CAudioPlayer *ap) {
2009
2010    SLuint32 playState = ap->mPlay.mState;
2011
2012    switch (ap->mAndroidObjType) {
2013    case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
2014        switch (playState) {
2015        case SL_PLAYSTATE_STOPPED:
2016            SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_STOPPED");
2017            if (ap->mAudioTrack != 0) {
2018                ap->mAudioTrack->stop();
2019            }
2020            break;
2021        case SL_PLAYSTATE_PAUSED:
2022            SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PAUSED");
2023            if (ap->mAudioTrack != 0) {
2024                ap->mAudioTrack->pause();
2025            }
2026            break;
2027        case SL_PLAYSTATE_PLAYING:
2028            SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PLAYING");
2029            if (ap->mAudioTrack != 0) {
2030                // instead of ap->mAudioTrack->start();
2031                ap->mDeferredStart = true;
2032            }
2033            break;
2034        default:
2035            // checked by caller, should not happen
2036            break;
2037        }
2038        break;
2039
2040    case AUDIOPLAYER_FROM_URIFD:      // intended fall-through
2041    case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:     // intended fall-through
2042    case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:  // intended fall-through
2043    case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
2044        // FIXME report and use the return code to the lock mechanism, which is where play state
2045        //   changes are updated (see object_unlock_exclusive_attributes())
2046        aplayer_setPlayState(ap->mAPlayer, playState, &ap->mAndroidObjState);
2047        break;
2048    default:
2049        SL_LOGE(ERROR_PLAYERSETPLAYSTATE_UNEXPECTED_OBJECT_TYPE_D, ap->mAndroidObjType);
2050        break;
2051    }
2052}
2053
2054
2055//-----------------------------------------------------------------------------
2056// call when either player event flags, marker position, or position update period changes
2057void android_audioPlayer_usePlayEventMask(CAudioPlayer *ap) {
2058    IPlay *pPlayItf = &ap->mPlay;
2059    SLuint32 eventFlags = pPlayItf->mEventFlags;
2060    /*switch (ap->mAndroidObjType) {
2061    case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:*/
2062
2063    if (ap->mAPlayer != 0) {
2064        assert(ap->mAudioTrack == 0);
2065        ap->mAPlayer->setPlayEvents((int32_t) eventFlags, (int32_t) pPlayItf->mMarkerPosition,
2066                (int32_t) pPlayItf->mPositionUpdatePeriod);
2067        return;
2068    }
2069
2070    if (ap->mAudioTrack == 0) {
2071        return;
2072    }
2073
2074    if (eventFlags & SL_PLAYEVENT_HEADATMARKER) {
2075        ap->mAudioTrack->setMarkerPosition((uint32_t)((((int64_t)pPlayItf->mMarkerPosition
2076                * sles_to_android_sampleRate(ap->mSampleRateMilliHz)))/1000));
2077    } else {
2078        // clear marker
2079        ap->mAudioTrack->setMarkerPosition(0);
2080    }
2081
2082    if (eventFlags & SL_PLAYEVENT_HEADATNEWPOS) {
2083         ap->mAudioTrack->setPositionUpdatePeriod(
2084                (uint32_t)((((int64_t)pPlayItf->mPositionUpdatePeriod
2085                * sles_to_android_sampleRate(ap->mSampleRateMilliHz)))/1000));
2086    } else {
2087        // clear periodic update
2088        ap->mAudioTrack->setPositionUpdatePeriod(0);
2089    }
2090
2091    if (eventFlags & SL_PLAYEVENT_HEADATEND) {
2092        // nothing to do for SL_PLAYEVENT_HEADATEND, callback event will be checked against mask
2093    }
2094
2095    if (eventFlags & SL_PLAYEVENT_HEADMOVING) {
2096        // FIXME support SL_PLAYEVENT_HEADMOVING
2097        SL_LOGD("[ FIXME: IPlay_SetCallbackEventsMask(SL_PLAYEVENT_HEADMOVING) on an "
2098            "SL_OBJECTID_AUDIOPLAYER to be implemented ]");
2099    }
2100    if (eventFlags & SL_PLAYEVENT_HEADSTALLED) {
2101        // nothing to do for SL_PLAYEVENT_HEADSTALLED, callback event will be checked against mask
2102    }
2103
2104}
2105
2106
2107//-----------------------------------------------------------------------------
2108SLresult android_audioPlayer_getDuration(IPlay *pPlayItf, SLmillisecond *pDurMsec) {
2109    CAudioPlayer *ap = (CAudioPlayer *)pPlayItf->mThis;
2110    switch (ap->mAndroidObjType) {
2111
2112      case AUDIOPLAYER_FROM_URIFD:  // intended fall-through
2113      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE: {
2114        int32_t durationMsec = ANDROID_UNKNOWN_TIME;
2115        if (ap->mAPlayer != 0) {
2116            ap->mAPlayer->getDurationMsec(&durationMsec);
2117        }
2118        *pDurMsec = durationMsec == ANDROID_UNKNOWN_TIME ? SL_TIME_UNKNOWN : durationMsec;
2119        break;
2120      }
2121
2122      case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE: // intended fall-through
2123      case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
2124      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
2125      default: {
2126        *pDurMsec = SL_TIME_UNKNOWN;
2127      }
2128    }
2129    return SL_RESULT_SUCCESS;
2130}
2131
2132
2133//-----------------------------------------------------------------------------
2134void android_audioPlayer_getPosition(IPlay *pPlayItf, SLmillisecond *pPosMsec) {
2135    CAudioPlayer *ap = (CAudioPlayer *)pPlayItf->mThis;
2136    switch (ap->mAndroidObjType) {
2137
2138      case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
2139        if ((ap->mSampleRateMilliHz == UNKNOWN_SAMPLERATE) || (ap->mAudioTrack == 0)) {
2140            *pPosMsec = 0;
2141        } else {
2142            uint32_t positionInFrames;
2143            ap->mAudioTrack->getPosition(&positionInFrames);
2144            *pPosMsec = ((int64_t)positionInFrames * 1000) /
2145                    sles_to_android_sampleRate(ap->mSampleRateMilliHz);
2146        }
2147        break;
2148
2149      case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:    // intended fall-through
2150      case AUDIOPLAYER_FROM_URIFD:
2151      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
2152      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE: {
2153        int32_t posMsec = ANDROID_UNKNOWN_TIME;
2154        if (ap->mAPlayer != 0) {
2155            ap->mAPlayer->getPositionMsec(&posMsec);
2156        }
2157        *pPosMsec = posMsec == ANDROID_UNKNOWN_TIME ? 0 : posMsec;
2158        break;
2159      }
2160
2161      default:
2162        *pPosMsec = 0;
2163    }
2164}
2165
2166
2167//-----------------------------------------------------------------------------
2168SLresult android_audioPlayer_seek(CAudioPlayer *ap, SLmillisecond posMsec) {
2169    SLresult result = SL_RESULT_SUCCESS;
2170
2171    switch (ap->mAndroidObjType) {
2172
2173      case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:      // intended fall-through
2174      case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:
2175      case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
2176        result = SL_RESULT_FEATURE_UNSUPPORTED;
2177        break;
2178
2179      case AUDIOPLAYER_FROM_URIFD:                   // intended fall-through
2180      case AUDIOPLAYER_FROM_URIFD_TO_PCM_BUFFERQUEUE:
2181        if (ap->mAPlayer != 0) {
2182            ap->mAPlayer->seek(posMsec);
2183        }
2184        break;
2185
2186      default:
2187        break;
2188    }
2189    return result;
2190}
2191
2192
2193//-----------------------------------------------------------------------------
2194SLresult android_audioPlayer_loop(CAudioPlayer *ap, SLboolean loopEnable) {
2195    SLresult result = SL_RESULT_SUCCESS;
2196
2197    switch (ap->mAndroidObjType) {
2198    case AUDIOPLAYER_FROM_URIFD:
2199    // case AUDIOPLAY_FROM_URIFD_TO_PCM_BUFFERQUEUE:
2200    //      would actually work, but what's the point?
2201      if (ap->mAPlayer != 0) {
2202        ap->mAPlayer->loop((bool)loopEnable);
2203      }
2204      break;
2205    default:
2206      result = SL_RESULT_FEATURE_UNSUPPORTED;
2207      break;
2208    }
2209    return result;
2210}
2211
2212
2213//-----------------------------------------------------------------------------
2214SLresult android_audioPlayer_setBufferingUpdateThresholdPerMille(CAudioPlayer *ap,
2215        SLpermille threshold) {
2216    SLresult result = SL_RESULT_SUCCESS;
2217
2218    switch (ap->mAndroidObjType) {
2219      case AUDIOPLAYER_FROM_URIFD:
2220        if (ap->mAPlayer != 0) {
2221            ap->mAPlayer->setBufferingUpdateThreshold(threshold / 10);
2222        }
2223        break;
2224
2225      default: {}
2226    }
2227
2228    return result;
2229}
2230
2231
2232//-----------------------------------------------------------------------------
2233void android_audioPlayer_bufferQueue_onRefilled_l(CAudioPlayer *ap) {
2234    // the AudioTrack associated with the AudioPlayer receiving audio from a PCM buffer
2235    // queue was stopped when the queue become empty, we restart as soon as a new buffer
2236    // has been enqueued since we're in playing state
2237    if (ap->mAudioTrack != 0) {
2238        // instead of ap->mAudioTrack->start();
2239        ap->mDeferredStart = true;
2240    }
2241
2242    // when the queue became empty, an underflow on the prefetch status itf was sent. Now the queue
2243    // has received new data, signal it has sufficient data
2244    if (IsInterfaceInitialized(&ap->mObject, MPH_PREFETCHSTATUS)) {
2245        // we wouldn't have been called unless we were previously in the underflow state
2246        assert(SL_PREFETCHSTATUS_UNDERFLOW == ap->mPrefetchStatus.mStatus);
2247        assert(0 == ap->mPrefetchStatus.mLevel);
2248        ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_SUFFICIENTDATA;
2249        ap->mPrefetchStatus.mLevel = 1000;
2250        // callback or no callback?
2251        SLuint32 prefetchEvents = ap->mPrefetchStatus.mCallbackEventsMask &
2252                (SL_PREFETCHEVENT_STATUSCHANGE | SL_PREFETCHEVENT_FILLLEVELCHANGE);
2253        if (SL_PREFETCHEVENT_NONE != prefetchEvents) {
2254            ap->mPrefetchStatus.mDeferredPrefetchCallback = ap->mPrefetchStatus.mCallback;
2255            ap->mPrefetchStatus.mDeferredPrefetchContext  = ap->mPrefetchStatus.mContext;
2256            ap->mPrefetchStatus.mDeferredPrefetchEvents   = prefetchEvents;
2257        }
2258    }
2259}
2260
2261
2262//-----------------------------------------------------------------------------
2263/*
2264 * BufferQueue::Clear
2265 */
2266SLresult android_audioPlayer_bufferQueue_onClear(CAudioPlayer *ap) {
2267    SLresult result = SL_RESULT_SUCCESS;
2268
2269    switch (ap->mAndroidObjType) {
2270    //-----------------------------------
2271    // AudioTrack
2272    case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
2273        if (ap->mAudioTrack != 0) {
2274            ap->mAudioTrack->flush();
2275        }
2276        break;
2277    default:
2278        result = SL_RESULT_INTERNAL_ERROR;
2279        break;
2280    }
2281
2282    return result;
2283}
2284
2285
2286//-----------------------------------------------------------------------------
2287void android_audioPlayer_androidBufferQueue_clear_l(CAudioPlayer *ap) {
2288    switch (ap->mAndroidObjType) {
2289    case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:
2290      if (ap->mAPlayer != 0) {
2291        android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(ap->mAPlayer.get());
2292        splr->appClear_l();
2293      } break;
2294    case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
2295      // nothing to do here, fall through
2296    default:
2297      break;
2298    }
2299}
2300
2301void android_audioPlayer_androidBufferQueue_onRefilled_l(CAudioPlayer *ap) {
2302    switch (ap->mAndroidObjType) {
2303    case AUDIOPLAYER_FROM_TS_ANDROIDBUFFERQUEUE:
2304      if (ap->mAPlayer != 0) {
2305        android::StreamPlayer* splr = static_cast<android::StreamPlayer*>(ap->mAPlayer.get());
2306        splr->queueRefilled();
2307      } break;
2308    case AUDIOPLAYER_FROM_ADTS_ABQ_TO_PCM_BUFFERQUEUE:
2309      // FIXME this may require waking up the decoder if it is currently starved and isn't polling
2310    default:
2311      break;
2312    }
2313}
2314