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