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