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