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