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