Home | History | Annotate | Download | only in libopensles
      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 "utils/RefBase.h"
     19 #include "android_prompts.h"
     20 
     21 template class android::KeyedVector<SLuint32, android::AudioEffect* > ;
     22 
     23 #define KEY_STREAM_TYPE_PARAMSIZE  sizeof(SLint32)
     24 
     25 //-----------------------------------------------------------------------------
     26 int android_getMinFrameCount(uint32_t sampleRate) {
     27     int afSampleRate;
     28     if (android::AudioSystem::getOutputSamplingRate(&afSampleRate,
     29             ANDROID_DEFAULT_OUTPUT_STREAM_TYPE) != android::NO_ERROR) {
     30         return ANDROID_DEFAULT_AUDIOTRACK_BUFFER_SIZE;
     31     }
     32     int afFrameCount;
     33     if (android::AudioSystem::getOutputFrameCount(&afFrameCount,
     34             ANDROID_DEFAULT_OUTPUT_STREAM_TYPE) != android::NO_ERROR) {
     35         return ANDROID_DEFAULT_AUDIOTRACK_BUFFER_SIZE;
     36     }
     37     uint32_t afLatency;
     38     if (android::AudioSystem::getOutputLatency(&afLatency,
     39             ANDROID_DEFAULT_OUTPUT_STREAM_TYPE) != android::NO_ERROR) {
     40         return ANDROID_DEFAULT_AUDIOTRACK_BUFFER_SIZE;
     41     }
     42     // minimum nb of buffers to cover output latency, given the size of each hardware audio buffer
     43     uint32_t minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
     44     if (minBufCount < 2) minBufCount = 2;
     45     // minimum number of frames to cover output latency at the sample rate of the content
     46     return (afFrameCount*sampleRate*minBufCount)/afSampleRate;
     47 }
     48 
     49 
     50 //-----------------------------------------------------------------------------
     51 #define LEFT_CHANNEL_MASK  0x1 << 0
     52 #define RIGHT_CHANNEL_MASK 0x1 << 1
     53 
     54 static void android_audioPlayer_updateStereoVolume(CAudioPlayer* ap) {
     55     float leftVol = 1.0f, rightVol = 1.0f;
     56 
     57     if (NULL == ap->mAudioTrack) {
     58         return;
     59     }
     60     // should not be used when muted
     61     if (SL_BOOLEAN_TRUE == ap->mMute) {
     62         return;
     63     }
     64 
     65     int channelCount = ap->mNumChannels;
     66 
     67     // mute has priority over solo
     68     int leftAudibilityFactor = 1, rightAudibilityFactor = 1;
     69 
     70     if (channelCount >= STEREO_CHANNELS) {
     71         if (ap->mMuteMask & LEFT_CHANNEL_MASK) {
     72             // left muted
     73             leftAudibilityFactor = 0;
     74         } else {
     75             // left not muted
     76             if (ap->mSoloMask & LEFT_CHANNEL_MASK) {
     77                 // left soloed
     78                 leftAudibilityFactor = 1;
     79             } else {
     80                 // left not soloed
     81                 if (ap->mSoloMask & RIGHT_CHANNEL_MASK) {
     82                     // right solo silences left
     83                     leftAudibilityFactor = 0;
     84                 } else {
     85                     // left and right are not soloed, and left is not muted
     86                     leftAudibilityFactor = 1;
     87                 }
     88             }
     89         }
     90 
     91         if (ap->mMuteMask & RIGHT_CHANNEL_MASK) {
     92             // right muted
     93             rightAudibilityFactor = 0;
     94         } else {
     95             // right not muted
     96             if (ap->mSoloMask & RIGHT_CHANNEL_MASK) {
     97                 // right soloed
     98                 rightAudibilityFactor = 1;
     99             } else {
    100                 // right not soloed
    101                 if (ap->mSoloMask & LEFT_CHANNEL_MASK) {
    102                     // left solo silences right
    103                     rightAudibilityFactor = 0;
    104                 } else {
    105                     // left and right are not soloed, and right is not muted
    106                     rightAudibilityFactor = 1;
    107                 }
    108             }
    109         }
    110     }
    111 
    112     // compute amplification as the combination of volume level and stereo position
    113     //   amplification from volume level
    114     ap->mAmplFromVolLevel = sles_to_android_amplification(ap->mVolume.mLevel);
    115     //   amplification from direct level (changed in SLEffectSendtItf and SLAndroidEffectSendItf)
    116     leftVol  *= ap->mAmplFromVolLevel * ap->mAmplFromDirectLevel;
    117     rightVol *= ap->mAmplFromVolLevel * ap->mAmplFromDirectLevel;
    118 
    119     // amplification from stereo position
    120     if (ap->mVolume.mEnableStereoPosition) {
    121         // panning law depends on number of channels of content: stereo panning vs 2ch. balance
    122         if(1 == channelCount) {
    123             // stereo panning
    124             double theta = (1000+ap->mVolume.mStereoPosition)*M_PI_4/1000.0f; // 0 <= theta <= Pi/2
    125             ap->mAmplFromStereoPos[0] = cos(theta);
    126             ap->mAmplFromStereoPos[1] = sin(theta);
    127         } else {
    128             // stereo balance
    129             if (ap->mVolume.mStereoPosition > 0) {
    130                 ap->mAmplFromStereoPos[0] = (1000-ap->mVolume.mStereoPosition)/1000.0f;
    131                 ap->mAmplFromStereoPos[1] = 1.0f;
    132             } else {
    133                 ap->mAmplFromStereoPos[0] = 1.0f;
    134                 ap->mAmplFromStereoPos[1] = (1000+ap->mVolume.mStereoPosition)/1000.0f;
    135             }
    136         }
    137         leftVol  *= ap->mAmplFromStereoPos[0];
    138         rightVol *= ap->mAmplFromStereoPos[1];
    139     }
    140 
    141     ap->mAudioTrack->setVolume(leftVol * leftAudibilityFactor, rightVol * rightAudibilityFactor);
    142 
    143     // changes in the AudioPlayer volume must be reflected in the send level:
    144     //  in SLEffectSendItf or in SLAndroidEffectSendItf?
    145     // FIXME replace interface test by an internal API once we have one.
    146     if (NULL != ap->mEffectSend.mItf) {
    147         for (unsigned int i=0 ; i<AUX_MAX ; i++) {
    148             if (ap->mEffectSend.mEnableLevels[i].mEnable) {
    149                 android_fxSend_setSendLevel(ap,
    150                         ap->mEffectSend.mEnableLevels[i].mSendLevel + ap->mVolume.mLevel);
    151                 // there's a single aux bus on Android, so we can stop looking once the first
    152                 // aux effect is found.
    153                 break;
    154             }
    155         }
    156     } else if (NULL != ap->mAndroidEffectSend.mItf) {
    157         android_fxSend_setSendLevel(ap, ap->mAndroidEffectSend.mSendLevel + ap->mVolume.mLevel);
    158     }
    159 }
    160 
    161 //-----------------------------------------------------------------------------
    162 void audioTrack_handleMarker_lockPlay(CAudioPlayer* ap) {
    163     //SL_LOGV("received event EVENT_MARKER from AudioTrack");
    164     slPlayCallback callback = NULL;
    165     void* callbackPContext = NULL;
    166 
    167     interface_lock_shared(&ap->mPlay);
    168     callback = ap->mPlay.mCallback;
    169     callbackPContext = ap->mPlay.mContext;
    170     interface_unlock_shared(&ap->mPlay);
    171 
    172     if (NULL != callback) {
    173         // getting this event implies SL_PLAYEVENT_HEADATMARKER was set in the event mask
    174         (*callback)(&ap->mPlay.mItf, callbackPContext, SL_PLAYEVENT_HEADATMARKER);
    175     }
    176 }
    177 
    178 //-----------------------------------------------------------------------------
    179 void audioTrack_handleNewPos_lockPlay(CAudioPlayer* ap) {
    180     //SL_LOGV("received event EVENT_NEW_POS from AudioTrack");
    181     slPlayCallback callback = NULL;
    182     void* callbackPContext = NULL;
    183 
    184     interface_lock_shared(&ap->mPlay);
    185     callback = ap->mPlay.mCallback;
    186     callbackPContext = ap->mPlay.mContext;
    187     interface_unlock_shared(&ap->mPlay);
    188 
    189     if (NULL != callback) {
    190         // getting this event implies SL_PLAYEVENT_HEADATNEWPOS was set in the event mask
    191         (*callback)(&ap->mPlay.mItf, callbackPContext, SL_PLAYEVENT_HEADATNEWPOS);
    192     }
    193 }
    194 
    195 
    196 //-----------------------------------------------------------------------------
    197 void audioTrack_handleUnderrun_lockPlay(CAudioPlayer* ap) {
    198     slPlayCallback callback = NULL;
    199     void* callbackPContext = NULL;
    200 
    201     interface_lock_shared(&ap->mPlay);
    202     callback = ap->mPlay.mCallback;
    203     callbackPContext = ap->mPlay.mContext;
    204     bool headStalled = (ap->mPlay.mEventFlags & SL_PLAYEVENT_HEADSTALLED) != 0;
    205     interface_unlock_shared(&ap->mPlay);
    206 
    207     if ((NULL != callback) && headStalled) {
    208         (*callback)(&ap->mPlay.mItf, callbackPContext, SL_PLAYEVENT_HEADSTALLED);
    209     }
    210 }
    211 
    212 
    213 //-----------------------------------------------------------------------------
    214 /**
    215  * post-condition: play state of AudioPlayer is SL_PLAYSTATE_PAUSED if setPlayStateToPaused is true
    216  *
    217  * note: a conditional flag, setPlayStateToPaused, is used here to specify whether the play state
    218  *       needs to be changed when the player reaches the end of the content to play. This is
    219  *       relative to what the specification describes for buffer queues vs the
    220  *       SL_PLAYEVENT_HEADATEND event. In the OpenSL ES specification 1.0.1:
    221  *        - section 8.12 SLBufferQueueItf states "In the case of starvation due to insufficient
    222  *          buffers in the queue, the playing of audio data stops. The player remains in the
    223  *          SL_PLAYSTATE_PLAYING state."
    224  *        - section 9.2.31 SL_PLAYEVENT states "SL_PLAYEVENT_HEADATEND Playback head is at the end
    225  *          of the current content and the player has paused."
    226  */
    227 void audioPlayer_dispatch_headAtEnd_lockPlay(CAudioPlayer *ap, bool setPlayStateToPaused,
    228         bool needToLock) {
    229     //SL_LOGV("ap=%p, setPlayStateToPaused=%d, needToLock=%d", ap, setPlayStateToPaused,
    230     //        needToLock);
    231     slPlayCallback playCallback = NULL;
    232     void * playContext = NULL;
    233     // SLPlayItf callback or no callback?
    234     if (needToLock) {
    235         interface_lock_exclusive(&ap->mPlay);
    236     }
    237     if (ap->mPlay.mEventFlags & SL_PLAYEVENT_HEADATEND) {
    238         playCallback = ap->mPlay.mCallback;
    239         playContext = ap->mPlay.mContext;
    240     }
    241     if (setPlayStateToPaused) {
    242         ap->mPlay.mState = SL_PLAYSTATE_PAUSED;
    243     }
    244     if (needToLock) {
    245         interface_unlock_exclusive(&ap->mPlay);
    246     }
    247     // callback with no lock held
    248     if (NULL != playCallback) {
    249         (*playCallback)(&ap->mPlay.mItf, playContext, SL_PLAYEVENT_HEADATEND);
    250     }
    251 
    252 }
    253 
    254 
    255 //-----------------------------------------------------------------------------
    256 /**
    257  * pre-condition: AudioPlayer has SLPrefetchStatusItf initialized
    258  * post-condition:
    259  *  - ap->mPrefetchStatus.mStatus == status
    260  *  - the prefetch status callback, if any, has been notified if a change occurred
    261  *
    262  */
    263 void audioPlayer_dispatch_prefetchStatus_lockPrefetch(CAudioPlayer *ap, SLuint32 status,
    264         bool needToLock) {
    265     slPrefetchCallback prefetchCallback = NULL;
    266     void * prefetchContext = NULL;
    267 
    268     if (needToLock) {
    269         interface_lock_exclusive(&ap->mPrefetchStatus);
    270     }
    271     // status change?
    272     if (ap->mPrefetchStatus.mStatus != status) {
    273         ap->mPrefetchStatus.mStatus = status;
    274         // callback or no callback?
    275         if (ap->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_STATUSCHANGE) {
    276             prefetchCallback = ap->mPrefetchStatus.mCallback;
    277             prefetchContext  = ap->mPrefetchStatus.mContext;
    278         }
    279     }
    280     if (needToLock) {
    281         interface_unlock_exclusive(&ap->mPrefetchStatus);
    282     }
    283 
    284     // callback with no lock held
    285     if (NULL != prefetchCallback) {
    286         (*prefetchCallback)(&ap->mPrefetchStatus.mItf, prefetchContext, status);
    287     }
    288 }
    289 
    290 
    291 //-----------------------------------------------------------------------------
    292 SLresult audioPlayer_setStreamType(CAudioPlayer* ap, SLint32 type) {
    293     SLresult result = SL_RESULT_SUCCESS;
    294     SL_LOGV("type %ld", type);
    295 
    296     int newStreamType = ANDROID_DEFAULT_OUTPUT_STREAM_TYPE;
    297     switch(type) {
    298     case SL_ANDROID_STREAM_VOICE:
    299         newStreamType = android::AudioSystem::VOICE_CALL;
    300         break;
    301     case SL_ANDROID_STREAM_SYSTEM:
    302         newStreamType = android::AudioSystem::SYSTEM;
    303         break;
    304     case SL_ANDROID_STREAM_RING:
    305         newStreamType = android::AudioSystem::RING;
    306         break;
    307     case SL_ANDROID_STREAM_MEDIA:
    308         newStreamType = android::AudioSystem::MUSIC;
    309         break;
    310     case SL_ANDROID_STREAM_ALARM:
    311         newStreamType = android::AudioSystem::ALARM;
    312         break;
    313     case SL_ANDROID_STREAM_NOTIFICATION:
    314         newStreamType = android::AudioSystem::NOTIFICATION;
    315         break;
    316     default:
    317         SL_LOGE(ERROR_PLAYERSTREAMTYPE_SET_UNKNOWN_TYPE);
    318         result = SL_RESULT_PARAMETER_INVALID;
    319         break;
    320     }
    321 
    322     // stream type needs to be set before the object is realized
    323     // (ap->mAudioTrack is supposed to be NULL until then)
    324     if (SL_OBJECT_STATE_UNREALIZED != ap->mObject.mState) {
    325         SL_LOGE(ERROR_PLAYERSTREAMTYPE_REALIZED);
    326         result = SL_RESULT_PRECONDITIONS_VIOLATED;
    327     } else {
    328         ap->mStreamType = newStreamType;
    329     }
    330 
    331     return result;
    332 }
    333 
    334 
    335 //-----------------------------------------------------------------------------
    336 SLresult audioPlayer_getStreamType(CAudioPlayer* ap, SLint32 *pType) {
    337     SLresult result = SL_RESULT_SUCCESS;
    338 
    339     switch(ap->mStreamType) {
    340     case android::AudioSystem::VOICE_CALL:
    341         *pType = SL_ANDROID_STREAM_VOICE;
    342         break;
    343     case android::AudioSystem::SYSTEM:
    344         *pType = SL_ANDROID_STREAM_SYSTEM;
    345         break;
    346     case android::AudioSystem::RING:
    347         *pType = SL_ANDROID_STREAM_RING;
    348         break;
    349     case android::AudioSystem::DEFAULT:
    350     case android::AudioSystem::MUSIC:
    351         *pType = SL_ANDROID_STREAM_MEDIA;
    352         break;
    353     case android::AudioSystem::ALARM:
    354         *pType = SL_ANDROID_STREAM_ALARM;
    355         break;
    356     case android::AudioSystem::NOTIFICATION:
    357         *pType = SL_ANDROID_STREAM_NOTIFICATION;
    358         break;
    359     default:
    360         result = SL_RESULT_INTERNAL_ERROR;
    361         *pType = SL_ANDROID_STREAM_MEDIA;
    362         break;
    363     }
    364 
    365     return result;
    366 }
    367 
    368 
    369 //-----------------------------------------------------------------------------
    370 void audioPlayer_auxEffectUpdate(CAudioPlayer* ap) {
    371     if ((NULL != ap->mAudioTrack) && (ap->mAuxEffect != 0)) {
    372         android_fxSend_attach(ap, true, ap->mAuxEffect, ap->mVolume.mLevel + ap->mAuxSendLevel);
    373     }
    374 }
    375 
    376 
    377 //-----------------------------------------------------------------------------
    378 #ifndef USE_BACKPORT
    379 static void sfplayer_prepare(CAudioPlayer *ap, bool lockAP) {
    380 
    381     if (lockAP) { object_lock_exclusive(&ap->mObject); }
    382     ap->mAndroidObjState = ANDROID_PREPARING;
    383     if (lockAP) { object_unlock_exclusive(&ap->mObject); }
    384 
    385     if (ap->mSfPlayer != 0) {
    386         ap->mSfPlayer->prepare();
    387     }
    388 }
    389 #endif
    390 
    391 //-----------------------------------------------------------------------------
    392 #ifndef USE_BACKPORT
    393 // Callback associated with an SfPlayer of an SL ES AudioPlayer that gets its data
    394 // from a URI or FD, for prepare and prefetch events
    395 static void sfplayer_handlePrefetchEvent(const int event, const int data1, void* user) {
    396     if (NULL == user) {
    397         return;
    398     }
    399 
    400     CAudioPlayer *ap = (CAudioPlayer *)user;
    401     //SL_LOGV("received event %d, data %d from SfAudioPlayer", event, data1);
    402     switch(event) {
    403 
    404     case(android::SfPlayer::kEventPrepared): {
    405 
    406         if (SFPLAYER_SUCCESS != data1) {
    407             object_lock_exclusive(&ap->mObject);
    408 
    409             ap->mAudioTrack = NULL;
    410             ap->mNumChannels = 0;
    411             ap->mSampleRateMilliHz = 0;
    412             ap->mAndroidObjState = ANDROID_UNINITIALIZED;
    413 
    414             object_unlock_exclusive(&ap->mObject);
    415 
    416             // SfPlayer prepare() failed prefetching, there is no event in SLPrefetchStatus to
    417             //  indicate a prefetch error, so we signal it by sending simulataneously two events:
    418             //  - SL_PREFETCHEVENT_FILLLEVELCHANGE with a level of 0
    419             //  - SL_PREFETCHEVENT_STATUSCHANGE with a status of SL_PREFETCHSTATUS_UNDERFLOW
    420             SL_LOGE(ERROR_PLAYER_PREFETCH_d, data1);
    421             if (!IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) {
    422                 break;
    423             }
    424 
    425             slPrefetchCallback callback = NULL;
    426             void* callbackPContext = NULL;
    427 
    428             interface_lock_exclusive(&ap->mPrefetchStatus);
    429             ap->mPrefetchStatus.mLevel = 0;
    430             ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW;
    431             if ((ap->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_FILLLEVELCHANGE)
    432                     && (ap->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_STATUSCHANGE)) {
    433                 callback = ap->mPrefetchStatus.mCallback;
    434                 callbackPContext = ap->mPrefetchStatus.mContext;
    435             }
    436             interface_unlock_exclusive(&ap->mPrefetchStatus);
    437 
    438             // callback with no lock held
    439             if (NULL != callback) {
    440                 (*callback)(&ap->mPrefetchStatus.mItf, callbackPContext,
    441                         SL_PREFETCHEVENT_FILLLEVELCHANGE | SL_PREFETCHEVENT_STATUSCHANGE);
    442             }
    443 
    444 
    445         } else {
    446             object_lock_exclusive(&ap->mObject);
    447 
    448             ap->mAudioTrack = ap->mSfPlayer->getAudioTrack();
    449             ap->mNumChannels = ap->mSfPlayer->getNumChannels();
    450             ap->mSampleRateMilliHz = android_to_sles_sampleRate(ap->mSfPlayer->getSampleRateHz());
    451             ap->mSfPlayer->startPrefetch_async();
    452 
    453             // update the new track with the current settings
    454             audioPlayer_auxEffectUpdate(ap);
    455             android_audioPlayer_useEventMask(ap);
    456             android_audioPlayer_volumeUpdate(ap);
    457             android_audioPlayer_setPlayRate(ap, ap->mPlaybackRate.mRate, false /*lockAP*/);
    458 
    459             ap->mAndroidObjState = ANDROID_READY;
    460 
    461             object_unlock_exclusive(&ap->mObject);
    462         }
    463 
    464     } break;
    465 
    466     case(android::SfPlayer::kEventNewAudioTrack): {
    467         object_lock_exclusive(&ap->mObject);
    468 #if 1
    469         // SfPlayer has a new AudioTrack, update our pointer copy and configure the new one before
    470         // starting to use it
    471 #else
    472         // SfPlayer has a new AudioTrack, delete the old one and configure the new one before
    473         // starting to use it
    474 
    475         if (NULL != ap->mAudioTrack) {
    476             delete ap->mAudioTrack;
    477             ap->mAudioTrack = NULL;
    478         }
    479 #endif
    480         ap->mAudioTrack = ap->mSfPlayer->getAudioTrack();
    481         ap->mNumChannels = ap->mSfPlayer->getNumChannels();
    482         ap->mSampleRateMilliHz = android_to_sles_sampleRate(ap->mSfPlayer->getSampleRateHz());
    483 
    484         // update the new track with the current settings
    485         audioPlayer_auxEffectUpdate(ap);
    486         android_audioPlayer_useEventMask(ap);
    487         android_audioPlayer_volumeUpdate(ap);
    488         android_audioPlayer_setPlayRate(ap, ap->mPlaybackRate.mRate, false /*lockAP*/);
    489 
    490         object_unlock_exclusive(&ap->mObject);
    491     } break;
    492 
    493     case(android::SfPlayer::kEventPrefetchFillLevelUpdate): {
    494         if (!IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) {
    495             break;
    496         }
    497         slPrefetchCallback callback = NULL;
    498         void* callbackPContext = NULL;
    499 
    500         // SLPrefetchStatusItf callback or no callback?
    501         interface_lock_exclusive(&ap->mPrefetchStatus);
    502         if (ap->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_FILLLEVELCHANGE) {
    503             callback = ap->mPrefetchStatus.mCallback;
    504             callbackPContext = ap->mPrefetchStatus.mContext;
    505         }
    506         ap->mPrefetchStatus.mLevel = (SLpermille)data1;
    507         interface_unlock_exclusive(&ap->mPrefetchStatus);
    508 
    509         // callback with no lock held
    510         if (NULL != callback) {
    511             (*callback)(&ap->mPrefetchStatus.mItf, callbackPContext,
    512                     SL_PREFETCHEVENT_FILLLEVELCHANGE);
    513         }
    514     } break;
    515 
    516     case(android::SfPlayer::kEventPrefetchStatusChange): {
    517         if (!IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) {
    518             break;
    519         }
    520         slPrefetchCallback callback = NULL;
    521         void* callbackPContext = NULL;
    522 
    523         // SLPrefetchStatusItf callback or no callback?
    524         object_lock_exclusive(&ap->mObject);
    525         if (ap->mPrefetchStatus.mCallbackEventsMask & SL_PREFETCHEVENT_STATUSCHANGE) {
    526             callback = ap->mPrefetchStatus.mCallback;
    527             callbackPContext = ap->mPrefetchStatus.mContext;
    528         }
    529         if (data1 >= android::SfPlayer::kStatusIntermediate) {
    530             ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_SUFFICIENTDATA;
    531             // FIXME estimate fill level better?
    532             ap->mPrefetchStatus.mLevel = 1000;
    533             ap->mAndroidObjState = ANDROID_READY;
    534         } else if (data1 < android::SfPlayer::kStatusIntermediate) {
    535             ap->mPrefetchStatus.mStatus = SL_PREFETCHSTATUS_UNDERFLOW;
    536             // FIXME estimate fill level better?
    537             ap->mPrefetchStatus.mLevel = 0;
    538         }
    539         object_unlock_exclusive(&ap->mObject);
    540 
    541         // callback with no lock held
    542         if (NULL != callback) {
    543             (*callback)(&ap->mPrefetchStatus.mItf, callbackPContext, SL_PREFETCHEVENT_STATUSCHANGE);
    544         }
    545         } break;
    546 
    547     case(android::SfPlayer::kEventEndOfStream): {
    548         audioPlayer_dispatch_headAtEnd_lockPlay(ap, true /*set state to paused?*/, true);
    549         if ((NULL != ap->mAudioTrack) && (!ap->mSeek.mLoopEnabled)) {
    550             ap->mAudioTrack->stop();
    551         }
    552         } break;
    553 
    554     default:
    555         break;
    556     }
    557 }
    558 #endif
    559 
    560 
    561 //-----------------------------------------------------------------------------
    562 SLresult android_audioPlayer_checkSourceSink(CAudioPlayer *pAudioPlayer)
    563 {
    564     const SLDataSource *pAudioSrc = &pAudioPlayer->mDataSource.u.mSource;
    565     const SLDataSink *pAudioSnk = &pAudioPlayer->mDataSink.u.mSink;
    566     //--------------------------------------
    567     // Sink check:
    568     //     currently only OutputMix sinks are supported, regardless of the data source
    569     if (*(SLuint32 *)pAudioSnk->pLocator != SL_DATALOCATOR_OUTPUTMIX) {
    570         SL_LOGE("Cannot create audio player: data sink is not SL_DATALOCATOR_OUTPUTMIX");
    571         return SL_RESULT_PARAMETER_INVALID;
    572     }
    573 
    574     //--------------------------------------
    575     // Source check:
    576     SLuint32 locatorType = *(SLuint32 *)pAudioSrc->pLocator;
    577     SLuint32 formatType = *(SLuint32 *)pAudioSrc->pFormat;
    578 
    579     switch (locatorType) {
    580     //------------------
    581     //   Buffer Queues
    582     case SL_DATALOCATOR_BUFFERQUEUE:
    583     case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE:
    584         {
    585         SLDataLocator_BufferQueue *dl_bq =  (SLDataLocator_BufferQueue *) pAudioSrc->pLocator;
    586 
    587         // Buffer format
    588         switch (formatType) {
    589         //     currently only PCM buffer queues are supported,
    590         case SL_DATAFORMAT_PCM: {
    591             SLDataFormat_PCM *df_pcm = (SLDataFormat_PCM *) pAudioSrc->pFormat;
    592             switch (df_pcm->numChannels) {
    593             case 1:
    594             case 2:
    595                 break;
    596             default:
    597                 // this should have already been rejected by checkDataFormat
    598                 SL_LOGE("Cannot create audio player: unsupported " \
    599                     "PCM data source with %u channels", (unsigned) df_pcm->numChannels);
    600                 return SL_RESULT_CONTENT_UNSUPPORTED;
    601             }
    602             switch (df_pcm->samplesPerSec) {
    603             case SL_SAMPLINGRATE_8:
    604             case SL_SAMPLINGRATE_11_025:
    605             case SL_SAMPLINGRATE_12:
    606             case SL_SAMPLINGRATE_16:
    607             case SL_SAMPLINGRATE_22_05:
    608             case SL_SAMPLINGRATE_24:
    609             case SL_SAMPLINGRATE_32:
    610             case SL_SAMPLINGRATE_44_1:
    611             case SL_SAMPLINGRATE_48:
    612                 break;
    613             case SL_SAMPLINGRATE_64:
    614             case SL_SAMPLINGRATE_88_2:
    615             case SL_SAMPLINGRATE_96:
    616             case SL_SAMPLINGRATE_192:
    617             default:
    618                 SL_LOGE("Cannot create audio player: unsupported sample rate %u milliHz",
    619                     (unsigned) df_pcm->samplesPerSec);
    620                 return SL_RESULT_CONTENT_UNSUPPORTED;
    621             }
    622             switch (df_pcm->bitsPerSample) {
    623             case SL_PCMSAMPLEFORMAT_FIXED_8:
    624                 // FIXME We should support this
    625                 //SL_LOGE("Cannot create audio player: unsupported 8-bit data");
    626                 //return SL_RESULT_CONTENT_UNSUPPORTED;
    627             case SL_PCMSAMPLEFORMAT_FIXED_16:
    628                 break;
    629                 // others
    630             default:
    631                 // this should have already been rejected by checkDataFormat
    632                 SL_LOGE("Cannot create audio player: unsupported sample bit depth %lu",
    633                         (SLuint32)df_pcm->bitsPerSample);
    634                 return SL_RESULT_CONTENT_UNSUPPORTED;
    635             }
    636             switch (df_pcm->containerSize) {
    637             case 8:
    638             case 16:
    639                 break;
    640                 // others
    641             default:
    642                 SL_LOGE("Cannot create audio player: unsupported container size %u",
    643                     (unsigned) df_pcm->containerSize);
    644                 return SL_RESULT_CONTENT_UNSUPPORTED;
    645             }
    646             switch (df_pcm->channelMask) {
    647                 // FIXME needs work
    648             default:
    649                 break;
    650             }
    651             switch (df_pcm->endianness) {
    652             case SL_BYTEORDER_LITTLEENDIAN:
    653                 break;
    654             case SL_BYTEORDER_BIGENDIAN:
    655                 SL_LOGE("Cannot create audio player: unsupported big-endian byte order");
    656                 return SL_RESULT_CONTENT_UNSUPPORTED;
    657                 // native is proposed but not yet in spec
    658             default:
    659                 SL_LOGE("Cannot create audio player: unsupported byte order %u",
    660                     (unsigned) df_pcm->endianness);
    661                 return SL_RESULT_CONTENT_UNSUPPORTED;
    662             }
    663             } //case SL_DATAFORMAT_PCM
    664             break;
    665         case SL_DATAFORMAT_MIME:
    666         case SL_DATAFORMAT_RESERVED3:
    667             SL_LOGE("Cannot create audio player with buffer queue data source "
    668                 "without SL_DATAFORMAT_PCM format");
    669             return SL_RESULT_CONTENT_UNSUPPORTED;
    670         default:
    671             SL_LOGE("Cannot create audio player with buffer queue data source "
    672                 "without SL_DATAFORMAT_PCM format");
    673             return SL_RESULT_PARAMETER_INVALID;
    674         } // switch (formatType)
    675         } // case SL_DATALOCATOR_BUFFERQUEUE or SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE
    676         break;
    677     //------------------
    678     //   URI
    679     case SL_DATALOCATOR_URI:
    680         {
    681         SLDataLocator_URI *dl_uri =  (SLDataLocator_URI *) pAudioSrc->pLocator;
    682         if (NULL == dl_uri->URI) {
    683             return SL_RESULT_PARAMETER_INVALID;
    684         }
    685         // URI format
    686         switch (formatType) {
    687         case SL_DATAFORMAT_MIME:
    688             break;
    689         case SL_DATAFORMAT_PCM:
    690         case SL_DATAFORMAT_RESERVED3:
    691             SL_LOGE("Cannot create audio player with SL_DATALOCATOR_URI data source without "
    692                 "SL_DATAFORMAT_MIME format");
    693             return SL_RESULT_CONTENT_UNSUPPORTED;
    694         } // switch (formatType)
    695         } // case SL_DATALOCATOR_URI
    696         break;
    697     //------------------
    698     //   File Descriptor
    699     case SL_DATALOCATOR_ANDROIDFD:
    700         {
    701         // fd is already non null
    702         switch (formatType) {
    703         case SL_DATAFORMAT_MIME:
    704             break;
    705         case SL_DATAFORMAT_PCM:
    706             // FIXME implement
    707             SL_LOGD("[ FIXME implement PCM FD data sources ]");
    708             break;
    709         case SL_DATAFORMAT_RESERVED3:
    710             SL_LOGE("Cannot create audio player with SL_DATALOCATOR_ANDROIDFD data source "
    711                 "without SL_DATAFORMAT_MIME or SL_DATAFORMAT_PCM format");
    712             return SL_RESULT_CONTENT_UNSUPPORTED;
    713         } // switch (formatType)
    714         } // case SL_DATALOCATOR_ANDROIDFD
    715         break;
    716     //------------------
    717     //   Address
    718     case SL_DATALOCATOR_ADDRESS:
    719     case SL_DATALOCATOR_IODEVICE:
    720     case SL_DATALOCATOR_OUTPUTMIX:
    721     case SL_DATALOCATOR_RESERVED5:
    722     case SL_DATALOCATOR_MIDIBUFFERQUEUE:
    723     case SL_DATALOCATOR_RESERVED8:
    724         SL_LOGE("Cannot create audio player with data locator type 0x%x", (unsigned) locatorType);
    725         return SL_RESULT_CONTENT_UNSUPPORTED;
    726     default:
    727         return SL_RESULT_PARAMETER_INVALID;
    728     }// switch (locatorType)
    729 
    730     return SL_RESULT_SUCCESS;
    731 }
    732 
    733 
    734 
    735 //-----------------------------------------------------------------------------
    736 static void audioTrack_callBack_uri(int event, void* user, void *info) {
    737     // EVENT_MORE_DATA needs to be handled with priority over the other events
    738     // because it will be called the most often during playback
    739     if (event == android::AudioTrack::EVENT_MORE_DATA) {
    740         //SL_LOGV("received event EVENT_MORE_DATA from AudioTrack");
    741         // set size to 0 to signal we're not using the callback to write more data
    742         android::AudioTrack::Buffer* pBuff = (android::AudioTrack::Buffer*)info;
    743         pBuff->size = 0;
    744     } else if (NULL != user) {
    745         switch (event) {
    746             case (android::AudioTrack::EVENT_MARKER) :
    747                 audioTrack_handleMarker_lockPlay((CAudioPlayer *)user);
    748                 break;
    749             case (android::AudioTrack::EVENT_NEW_POS) :
    750                 audioTrack_handleNewPos_lockPlay((CAudioPlayer *)user);
    751                 break;
    752             case (android::AudioTrack::EVENT_UNDERRUN) :
    753                 audioTrack_handleUnderrun_lockPlay((CAudioPlayer *)user);
    754                 break;
    755             case (android::AudioTrack::EVENT_BUFFER_END) :
    756             case (android::AudioTrack::EVENT_LOOP_END) :
    757                 break;
    758             default:
    759                 SL_LOGE("Encountered unknown AudioTrack event %d for CAudioPlayer %p", event,
    760                         (CAudioPlayer *)user);
    761                 break;
    762         }
    763     }
    764 }
    765 
    766 //-----------------------------------------------------------------------------
    767 // Callback associated with an AudioTrack of an SL ES AudioPlayer that gets its data
    768 // from a buffer queue.
    769 static void audioTrack_callBack_pullFromBuffQueue(int event, void* user, void *info) {
    770     CAudioPlayer *ap = (CAudioPlayer *)user;
    771     void * callbackPContext = NULL;
    772     switch(event) {
    773 
    774     case (android::AudioTrack::EVENT_MORE_DATA) : {
    775         //SL_LOGV("received event EVENT_MORE_DATA from AudioTrack");
    776         slBufferQueueCallback callback = NULL;
    777         android::AudioTrack::Buffer* pBuff = (android::AudioTrack::Buffer*)info;
    778         // retrieve data from the buffer queue
    779         interface_lock_exclusive(&ap->mBufferQueue);
    780         if (ap->mBufferQueue.mState.count != 0) {
    781             //SL_LOGV("nbBuffers in queue = %lu",ap->mBufferQueue.mState.count);
    782             assert(ap->mBufferQueue.mFront != ap->mBufferQueue.mRear);
    783 
    784             BufferHeader *oldFront = ap->mBufferQueue.mFront;
    785             BufferHeader *newFront = &oldFront[1];
    786 
    787             // FIXME handle 8bit based on buffer format
    788             short *pSrc = (short*)((char *)oldFront->mBuffer
    789                     + ap->mBufferQueue.mSizeConsumed);
    790             if (ap->mBufferQueue.mSizeConsumed + pBuff->size < oldFront->mSize) {
    791                 // can't consume the whole or rest of the buffer in one shot
    792                 ap->mBufferQueue.mSizeConsumed += pBuff->size;
    793                 // leave pBuff->size untouched
    794                 // consume data
    795                 // FIXME can we avoid holding the lock during the copy?
    796                 memcpy (pBuff->i16, pSrc, pBuff->size);
    797             } else {
    798                 // finish consuming the buffer or consume the buffer in one shot
    799                 pBuff->size = oldFront->mSize - ap->mBufferQueue.mSizeConsumed;
    800                 ap->mBufferQueue.mSizeConsumed = 0;
    801 
    802                 if (newFront ==
    803                         &ap->mBufferQueue.mArray
    804                             [ap->mBufferQueue.mNumBuffers + 1])
    805                 {
    806                     newFront = ap->mBufferQueue.mArray;
    807                 }
    808                 ap->mBufferQueue.mFront = newFront;
    809 
    810                 ap->mBufferQueue.mState.count--;
    811                 ap->mBufferQueue.mState.playIndex++;
    812 
    813                 // consume data
    814                 // FIXME can we avoid holding the lock during the copy?
    815                 memcpy (pBuff->i16, pSrc, pBuff->size);
    816 
    817                 // data has been consumed, and the buffer queue state has been updated
    818                 // we will notify the client if applicable
    819                 callback = ap->mBufferQueue.mCallback;
    820                 // save callback data
    821                 callbackPContext = ap->mBufferQueue.mContext;
    822             }
    823         } else { // empty queue
    824             // signal no data available
    825             pBuff->size = 0;
    826 
    827             // signal we're at the end of the content, but don't pause (see note in function)
    828             audioPlayer_dispatch_headAtEnd_lockPlay(ap, false /*set state to paused?*/, false);
    829 
    830             // signal underflow to prefetch status itf
    831             if (IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) {
    832                 audioPlayer_dispatch_prefetchStatus_lockPrefetch(ap, SL_PREFETCHSTATUS_UNDERFLOW,
    833                     false);
    834             }
    835 
    836             // stop the track so it restarts playing faster when new data is enqueued
    837             ap->mAudioTrack->stop();
    838         }
    839         interface_unlock_exclusive(&ap->mBufferQueue);
    840         // notify client
    841         if (NULL != callback) {
    842             (*callback)(&ap->mBufferQueue.mItf, callbackPContext);
    843         }
    844     }
    845     break;
    846 
    847     case (android::AudioTrack::EVENT_MARKER) :
    848         audioTrack_handleMarker_lockPlay(ap);
    849         break;
    850 
    851     case (android::AudioTrack::EVENT_NEW_POS) :
    852         audioTrack_handleNewPos_lockPlay(ap);
    853         break;
    854 
    855     case (android::AudioTrack::EVENT_UNDERRUN) :
    856         audioTrack_handleUnderrun_lockPlay(ap);
    857         break;
    858 
    859     default:
    860         // FIXME where does the notification of SL_PLAYEVENT_HEADMOVING fit?
    861         SL_LOGE("Encountered unknown AudioTrack event %d for CAudioPlayer %p", event,
    862                 (CAudioPlayer *)user);
    863         break;
    864     }
    865 }
    866 
    867 
    868 //-----------------------------------------------------------------------------
    869 SLresult android_audioPlayer_create(
    870         CAudioPlayer *pAudioPlayer) {
    871 
    872     const SLDataSource *pAudioSrc = &pAudioPlayer->mDataSource.u.mSource;
    873     const SLDataSink *pAudioSnk = &pAudioPlayer->mDataSink.u.mSink;
    874     SLresult result = SL_RESULT_SUCCESS;
    875 
    876     //--------------------------------------
    877     // Sink check:
    878     // currently only OutputMix sinks are supported
    879     // this has already been verified in sles_to_android_CheckAudioPlayerSourceSink
    880     // SLuint32 locatorType = *(SLuint32 *)pAudioSnk->pLocator;
    881     // if (SL_DATALOCATOR_OUTPUTMIX == locatorType) {
    882     // }
    883 
    884     //--------------------------------------
    885     // Source check:
    886     SLuint32 locatorType = *(SLuint32 *)pAudioSrc->pLocator;
    887     switch (locatorType) {
    888     //   -----------------------------------
    889     //   Buffer Queue to AudioTrack
    890     case SL_DATALOCATOR_BUFFERQUEUE:
    891     case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE:
    892         pAudioPlayer->mAndroidObjType = AUDIOTRACK_PULL;
    893         pAudioPlayer->mpLock = new android::Mutex();
    894         pAudioPlayer->mPlaybackRate.mCapabilities = SL_RATEPROP_NOPITCHCORAUDIO;
    895         break;
    896     //   -----------------------------------
    897     //   URI or FD to MediaPlayer
    898     case SL_DATALOCATOR_URI:
    899     case SL_DATALOCATOR_ANDROIDFD:
    900         pAudioPlayer->mAndroidObjType = MEDIAPLAYER;
    901         pAudioPlayer->mpLock = new android::Mutex();
    902         pAudioPlayer->mPlaybackRate.mCapabilities = SL_RATEPROP_NOPITCHCORAUDIO;
    903         break;
    904     default:
    905         pAudioPlayer->mAndroidObjType = INVALID_TYPE;
    906         pAudioPlayer->mpLock = NULL;
    907         pAudioPlayer->mPlaybackRate.mCapabilities = 0;
    908         result = SL_RESULT_PARAMETER_INVALID;
    909         break;
    910     }
    911 
    912     pAudioPlayer->mAndroidObjState = ANDROID_UNINITIALIZED;
    913     pAudioPlayer->mStreamType = ANDROID_DEFAULT_OUTPUT_STREAM_TYPE;
    914     pAudioPlayer->mAudioTrack = NULL;
    915 #ifndef USE_BACKPORT
    916     // no longer needed, as placement new (explicit constructor) already does this
    917     // pAudioPlayer->mSfPlayer.clear();
    918 #endif
    919 
    920 #ifndef USE_BACKPORT
    921     pAudioPlayer->mSessionId = android::AudioSystem::newAudioSessionId();
    922 #endif
    923 
    924     pAudioPlayer->mAmplFromVolLevel = 1.0f;
    925     pAudioPlayer->mAmplFromStereoPos[0] = 1.0f;
    926     pAudioPlayer->mAmplFromStereoPos[1] = 1.0f;
    927     pAudioPlayer->mDirectLevel = 0; // no attenuation
    928     pAudioPlayer->mAmplFromDirectLevel = 1.0f; // matches initial mDirectLevel value
    929     pAudioPlayer->mAuxSendLevel = 0;
    930 
    931     // initialize interface-specific fields that can be used regardless of whether the interface
    932     // is exposed on the AudioPlayer or not
    933     // (section no longer applicable, as all previous initializations were the same as the defaults)
    934 
    935     return result;
    936 
    937 }
    938 
    939 
    940 //-----------------------------------------------------------------------------
    941 SLresult android_audioPlayer_setConfig(CAudioPlayer *ap, const SLchar *configKey,
    942         const void *pConfigValue, SLuint32 valueSize) {
    943 
    944     SLresult result = SL_RESULT_SUCCESS;
    945 
    946     if (NULL == ap) {
    947         result = SL_RESULT_INTERNAL_ERROR;
    948     } else if (NULL == pConfigValue) {
    949         SL_LOGE(ERROR_CONFIG_NULL_PARAM);
    950         result = SL_RESULT_PARAMETER_INVALID;
    951 
    952     } else if(strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_STREAM_TYPE) == 0) {
    953 
    954         // stream type
    955         if (KEY_STREAM_TYPE_PARAMSIZE > valueSize) {
    956             SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW);
    957             result = SL_RESULT_PARAMETER_INVALID;
    958         } else {
    959             result = audioPlayer_setStreamType(ap, *(SLuint32*)pConfigValue);
    960         }
    961 
    962     } else {
    963         SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY);
    964         result = SL_RESULT_PARAMETER_INVALID;
    965     }
    966 
    967     return result;
    968 }
    969 
    970 
    971 //-----------------------------------------------------------------------------
    972 SLresult android_audioPlayer_getConfig(CAudioPlayer* ap, const SLchar *configKey,
    973         SLuint32* pValueSize, void *pConfigValue) {
    974 
    975     SLresult result = SL_RESULT_SUCCESS;
    976 
    977     if (NULL == ap) {
    978         return SL_RESULT_INTERNAL_ERROR;
    979     } else if (NULL == pValueSize) {
    980         SL_LOGE(ERROR_CONFIG_NULL_PARAM);
    981         result = SL_RESULT_PARAMETER_INVALID;
    982 
    983     } else if(strcmp((const char*)configKey, (const char*)SL_ANDROID_KEY_STREAM_TYPE) == 0) {
    984 
    985         // stream type
    986         if (KEY_STREAM_TYPE_PARAMSIZE > *pValueSize) {
    987             SL_LOGE(ERROR_CONFIG_VALUESIZE_TOO_LOW);
    988             result = SL_RESULT_PARAMETER_INVALID;
    989         } else {
    990             *pValueSize = KEY_STREAM_TYPE_PARAMSIZE;
    991             if (NULL != pConfigValue) {
    992                 result = audioPlayer_getStreamType(ap, (SLint32*)pConfigValue);
    993             }
    994         }
    995 
    996     } else {
    997         SL_LOGE(ERROR_CONFIG_UNKNOWN_KEY);
    998         result = SL_RESULT_PARAMETER_INVALID;
    999     }
   1000 
   1001     return result;
   1002 }
   1003 
   1004 
   1005 //-----------------------------------------------------------------------------
   1006 SLresult android_audioPlayer_realize(CAudioPlayer *pAudioPlayer, SLboolean async) {
   1007 
   1008     SLresult result = SL_RESULT_SUCCESS;
   1009     SL_LOGV("Realize pAudioPlayer=%p", pAudioPlayer);
   1010 
   1011     switch (pAudioPlayer->mAndroidObjType) {
   1012     //-----------------------------------
   1013     // AudioTrack
   1014     case AUDIOTRACK_PULL:
   1015         {
   1016         // initialize platform-specific CAudioPlayer fields
   1017 
   1018         SLDataLocator_BufferQueue *dl_bq =  (SLDataLocator_BufferQueue *)
   1019                 pAudioPlayer->mDynamicSource.mDataSource;
   1020         SLDataFormat_PCM *df_pcm = (SLDataFormat_PCM *)
   1021                 pAudioPlayer->mDynamicSource.mDataSource->pFormat;
   1022 
   1023         uint32_t sampleRate = sles_to_android_sampleRate(df_pcm->samplesPerSec);
   1024 
   1025         pAudioPlayer->mAudioTrack = new android::AudioTrack(
   1026                 pAudioPlayer->mStreamType,                           // streamType
   1027                 sampleRate,                                          // sampleRate
   1028                 sles_to_android_sampleFormat(df_pcm->bitsPerSample), // format
   1029                 sles_to_android_channelMaskOut(df_pcm->numChannels, df_pcm->channelMask),
   1030                                                                      //channel mask
   1031                 0,                                                   // frameCount (here min)
   1032                 0,                                                   // flags
   1033                 audioTrack_callBack_pullFromBuffQueue,               // callback
   1034                 (void *) pAudioPlayer,                               // user
   1035                 0      // FIXME find appropriate frame count         // notificationFrame
   1036 #ifndef USE_BACKPORT
   1037                 , pAudioPlayer->mSessionId
   1038 #endif
   1039                 );
   1040         android::status_t status = pAudioPlayer->mAudioTrack->initCheck();
   1041         if (status != android::NO_ERROR) {
   1042             SL_LOGE("AudioTrack::initCheck status %u", status);
   1043             result = SL_RESULT_CONTENT_UNSUPPORTED;
   1044         }
   1045 
   1046         // initialize platform-independent CAudioPlayer fields
   1047 
   1048         pAudioPlayer->mNumChannels = df_pcm->numChannels;
   1049         pAudioPlayer->mSampleRateMilliHz = df_pcm->samplesPerSec; // Note: bad field name in SL ES
   1050 
   1051         pAudioPlayer->mAndroidObjState = ANDROID_READY;
   1052         } break;
   1053 #ifndef USE_BACKPORT
   1054     //-----------------------------------
   1055     // MediaPlayer
   1056     case MEDIAPLAYER: {
   1057         object_lock_exclusive(&pAudioPlayer->mObject);
   1058 
   1059         pAudioPlayer->mAndroidObjState = ANDROID_UNINITIALIZED;
   1060         pAudioPlayer->mNumChannels = 0;
   1061         pAudioPlayer->mSampleRateMilliHz = 0;
   1062         pAudioPlayer->mAudioTrack = NULL;
   1063 
   1064         AudioPlayback_Parameters app;
   1065         app.sessionId = pAudioPlayer->mSessionId;
   1066         app.streamType = pAudioPlayer->mStreamType;
   1067         app.trackcb = audioTrack_callBack_uri;
   1068         app.trackcbUser = (void *) pAudioPlayer;
   1069 
   1070         pAudioPlayer->mSfPlayer = new android::SfPlayer(&app);
   1071         pAudioPlayer->mSfPlayer->setNotifListener(sfplayer_handlePrefetchEvent,
   1072                         (void*)pAudioPlayer /*notifUSer*/);
   1073         pAudioPlayer->mSfPlayer->armLooper();
   1074 
   1075         object_unlock_exclusive(&pAudioPlayer->mObject);
   1076 
   1077         switch (pAudioPlayer->mDataSource.mLocator.mLocatorType) {
   1078             case SL_DATALOCATOR_URI:
   1079                 pAudioPlayer->mSfPlayer->setDataSource(
   1080                         (const char*)pAudioPlayer->mDataSource.mLocator.mURI.URI);
   1081                 break;
   1082             case SL_DATALOCATOR_ANDROIDFD: {
   1083                 int64_t offset = (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.offset;
   1084                 pAudioPlayer->mSfPlayer->setDataSource(
   1085                         (int)pAudioPlayer->mDataSource.mLocator.mFD.fd,
   1086                         offset == SL_DATALOCATOR_ANDROIDFD_USE_FILE_SIZE ?
   1087                                 (int64_t)SFPLAYER_FD_FIND_FILE_SIZE : offset,
   1088                         (int64_t)pAudioPlayer->mDataSource.mLocator.mFD.length);
   1089                 } break;
   1090             default:
   1091                 SL_LOGE(ERROR_PLAYERREALIZE_UNKNOWN_DATASOURCE_LOCATOR);
   1092                 break;
   1093         }
   1094 
   1095         } break;
   1096 #endif
   1097     default:
   1098         SL_LOGE("Unexpected object type %d", pAudioPlayer->mAndroidObjType);
   1099         result = SL_RESULT_INTERNAL_ERROR;
   1100         break;
   1101     }
   1102 
   1103 #ifndef USE_BACKPORT
   1104 
   1105     // proceed with effect initialization
   1106     // initialize EQ
   1107     // FIXME use a table of effect descriptors when adding support for more effects
   1108     if (memcmp(SL_IID_EQUALIZER, &pAudioPlayer->mEqualizer.mEqDescriptor.type,
   1109             sizeof(effect_uuid_t)) == 0) {
   1110         SL_LOGV("Need to initialize EQ for AudioPlayer=%p", pAudioPlayer);
   1111         android_eq_init(pAudioPlayer->mSessionId, &pAudioPlayer->mEqualizer);
   1112     }
   1113     // initialize BassBoost
   1114     if (memcmp(SL_IID_BASSBOOST, &pAudioPlayer->mBassBoost.mBassBoostDescriptor.type,
   1115             sizeof(effect_uuid_t)) == 0) {
   1116         SL_LOGV("Need to initialize BassBoost for AudioPlayer=%p", pAudioPlayer);
   1117         android_bb_init(pAudioPlayer->mSessionId, &pAudioPlayer->mBassBoost);
   1118     }
   1119     // initialize Virtualizer
   1120     if (memcmp(SL_IID_VIRTUALIZER, &pAudioPlayer->mVirtualizer.mVirtualizerDescriptor.type,
   1121                sizeof(effect_uuid_t)) == 0) {
   1122         SL_LOGV("Need to initialize Virtualizer for AudioPlayer=%p", pAudioPlayer);
   1123         android_virt_init(pAudioPlayer->mSessionId, &pAudioPlayer->mVirtualizer);
   1124     }
   1125 
   1126     // initialize EffectSend
   1127     // FIXME initialize EffectSend
   1128 #endif
   1129 
   1130     return result;
   1131 }
   1132 
   1133 
   1134 //-----------------------------------------------------------------------------
   1135 SLresult android_audioPlayer_destroy(CAudioPlayer *pAudioPlayer) {
   1136     SLresult result = SL_RESULT_SUCCESS;
   1137     SL_LOGV("android_audioPlayer_destroy(%p)", pAudioPlayer);
   1138     switch (pAudioPlayer->mAndroidObjType) {
   1139     //-----------------------------------
   1140     // AudioTrack
   1141     case AUDIOTRACK_PULL:
   1142         // We own the audio track for PCM buffer queue players
   1143         if (pAudioPlayer->mAudioTrack != NULL) {
   1144             pAudioPlayer->mAudioTrack->stop();
   1145             delete pAudioPlayer->mAudioTrack;
   1146             pAudioPlayer->mAudioTrack = NULL;
   1147         }
   1148         break;
   1149 #ifndef USE_BACKPORT
   1150     //-----------------------------------
   1151     // MediaPlayer
   1152     case MEDIAPLAYER:
   1153         // We don't own this audio track, SfPlayer does
   1154         pAudioPlayer->mAudioTrack = NULL;
   1155         // FIXME might no longer be needed since we call explicit destructor
   1156         if (pAudioPlayer->mSfPlayer != 0) {
   1157             pAudioPlayer->mSfPlayer.clear();
   1158         }
   1159         break;
   1160 #endif
   1161     default:
   1162         SL_LOGE("Unexpected object type %d", pAudioPlayer->mAndroidObjType);
   1163         result = SL_RESULT_INTERNAL_ERROR;
   1164         break;
   1165     }
   1166 
   1167     // FIXME might not be needed
   1168     pAudioPlayer->mAndroidObjType = INVALID_TYPE;
   1169 
   1170     // explicit destructor
   1171     pAudioPlayer->mSfPlayer.~sp();
   1172     pAudioPlayer->mAuxEffect.~sp();
   1173 
   1174     if (pAudioPlayer->mpLock != NULL) {
   1175         delete pAudioPlayer->mpLock;
   1176         pAudioPlayer->mpLock = NULL;
   1177     }
   1178 
   1179     return result;
   1180 }
   1181 
   1182 
   1183 //-----------------------------------------------------------------------------
   1184 SLresult android_audioPlayer_setPlayRate(CAudioPlayer *ap, SLpermille rate, bool lockAP) {
   1185     SLresult result = SL_RESULT_SUCCESS;
   1186     uint32_t contentRate = 0;
   1187     switch(ap->mAndroidObjType) {
   1188     case AUDIOTRACK_PULL:
   1189     case MEDIAPLAYER: {
   1190         // get the content sample rate
   1191         if (lockAP) { object_lock_shared(&ap->mObject); }
   1192         uint32_t contentRate = sles_to_android_sampleRate(ap->mSampleRateMilliHz);
   1193         if (lockAP) { object_unlock_shared(&ap->mObject); }
   1194         // apply the SL ES playback rate on the AudioTrack as a factor of its content sample rate
   1195         if (ap->mAudioTrack != NULL) {
   1196             ap->mAudioTrack->setSampleRate(contentRate * (rate/1000.0f));
   1197         }
   1198         }
   1199         break;
   1200 
   1201     default:
   1202         SL_LOGE("Unexpected object type %d", ap->mAndroidObjType);
   1203         result = SL_RESULT_INTERNAL_ERROR;
   1204         break;
   1205     }
   1206     return result;
   1207 }
   1208 
   1209 
   1210 //-----------------------------------------------------------------------------
   1211 // called with no lock held
   1212 SLresult android_audioPlayer_setPlaybackRateBehavior(CAudioPlayer *ap,
   1213         SLuint32 constraints) {
   1214     SLresult result = SL_RESULT_SUCCESS;
   1215     switch(ap->mAndroidObjType) {
   1216     case AUDIOTRACK_PULL:
   1217     case MEDIAPLAYER:
   1218         if (constraints != (constraints & SL_RATEPROP_NOPITCHCORAUDIO)) {
   1219             result = SL_RESULT_FEATURE_UNSUPPORTED;
   1220         }
   1221         break;
   1222     default:
   1223         SL_LOGE("Unexpected object type %d", ap->mAndroidObjType);
   1224         result = SL_RESULT_INTERNAL_ERROR;
   1225         break;
   1226     }
   1227     return result;
   1228 }
   1229 
   1230 
   1231 //-----------------------------------------------------------------------------
   1232 // called with no lock held
   1233 SLresult android_audioPlayer_getCapabilitiesOfRate(CAudioPlayer *ap,
   1234         SLuint32 *pCapabilities) {
   1235     switch(ap->mAndroidObjType) {
   1236     case AUDIOTRACK_PULL:
   1237     case MEDIAPLAYER:
   1238         *pCapabilities = SL_RATEPROP_NOPITCHCORAUDIO;
   1239         break;
   1240     default:
   1241         *pCapabilities = 0;
   1242         break;
   1243     }
   1244     return SL_RESULT_SUCCESS;
   1245 }
   1246 
   1247 
   1248 //-----------------------------------------------------------------------------
   1249 void android_audioPlayer_setPlayState(CAudioPlayer *ap, bool lockAP) {
   1250 
   1251     if (lockAP) { object_lock_shared(&ap->mObject); }
   1252     SLuint32 playState = ap->mPlay.mState;
   1253     AndroidObject_state objState = ap->mAndroidObjState;
   1254     if (lockAP) { object_unlock_shared(&ap->mObject); }
   1255 
   1256     switch(ap->mAndroidObjType) {
   1257     case AUDIOTRACK_PULL:
   1258         switch (playState) {
   1259         case SL_PLAYSTATE_STOPPED:
   1260             SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_STOPPED");
   1261             if (NULL != ap->mAudioTrack) {
   1262                 ap->mAudioTrack->stop();
   1263             }
   1264             break;
   1265         case SL_PLAYSTATE_PAUSED:
   1266             SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PAUSED");
   1267             if (NULL != ap->mAudioTrack) {
   1268                 ap->mAudioTrack->pause();
   1269             }
   1270             break;
   1271         case SL_PLAYSTATE_PLAYING:
   1272             SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PLAYING");
   1273             if (NULL != ap->mAudioTrack) {
   1274                 ap->mAudioTrack->start();
   1275             }
   1276             break;
   1277         default:
   1278             // checked by caller, should not happen
   1279             break;
   1280         }
   1281         break;
   1282 #ifndef USE_BACKPORT
   1283     case MEDIAPLAYER:
   1284         switch (playState) {
   1285         case SL_PLAYSTATE_STOPPED: {
   1286             SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_STOPPED");
   1287             if (ap->mSfPlayer != 0) {
   1288                 ap->mSfPlayer->stop();
   1289             }
   1290             } break;
   1291         case SL_PLAYSTATE_PAUSED: {
   1292             SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PAUSED");
   1293             switch(objState) {
   1294                 case(ANDROID_UNINITIALIZED):
   1295                     sfplayer_prepare(ap, lockAP);
   1296                     break;
   1297                 case(ANDROID_PREPARING):
   1298                     break;
   1299                 case(ANDROID_READY):
   1300                     if (ap->mSfPlayer != 0) {
   1301                         ap->mSfPlayer->pause();
   1302                     }
   1303                     break;
   1304                 default:
   1305                     break;
   1306             }
   1307             } break;
   1308         case SL_PLAYSTATE_PLAYING: {
   1309             SL_LOGV("setting AudioPlayer to SL_PLAYSTATE_PLAYING");
   1310             switch(objState) {
   1311                 case(ANDROID_UNINITIALIZED):
   1312                     sfplayer_prepare(ap, lockAP);
   1313                     // fall through
   1314                 case(ANDROID_PREPARING):
   1315                 case(ANDROID_READY):
   1316                     if (ap->mSfPlayer != 0) {
   1317                         ap->mSfPlayer->play();
   1318                     }
   1319                     break;
   1320                 default:
   1321                     break;
   1322             }
   1323             } break;
   1324 
   1325         default:
   1326             // checked by caller, should not happen
   1327             break;
   1328         }
   1329         break;
   1330 #endif
   1331     default:
   1332         break;
   1333     }
   1334 }
   1335 
   1336 
   1337 //-----------------------------------------------------------------------------
   1338 void android_audioPlayer_useEventMask(CAudioPlayer *ap) {
   1339     IPlay *pPlayItf = &ap->mPlay;
   1340     SLuint32 eventFlags = pPlayItf->mEventFlags;
   1341     /*switch(ap->mAndroidObjType) {
   1342     case AUDIOTRACK_PULL:*/
   1343 
   1344     if (NULL == ap->mAudioTrack) {
   1345         return;
   1346     }
   1347 
   1348     if (eventFlags & SL_PLAYEVENT_HEADATMARKER) {
   1349         ap->mAudioTrack->setMarkerPosition((uint32_t)((((int64_t)pPlayItf->mMarkerPosition
   1350                 * sles_to_android_sampleRate(ap->mSampleRateMilliHz)))/1000));
   1351     } else {
   1352         // clear marker
   1353         ap->mAudioTrack->setMarkerPosition(0);
   1354     }
   1355 
   1356     if (eventFlags & SL_PLAYEVENT_HEADATNEWPOS) {
   1357          ap->mAudioTrack->setPositionUpdatePeriod(
   1358                 (uint32_t)((((int64_t)pPlayItf->mPositionUpdatePeriod
   1359                 * sles_to_android_sampleRate(ap->mSampleRateMilliHz)))/1000));
   1360     } else {
   1361         // clear periodic update
   1362         ap->mAudioTrack->setPositionUpdatePeriod(0);
   1363     }
   1364 
   1365     if (eventFlags & SL_PLAYEVENT_HEADATEND) {
   1366         // nothing to do for SL_PLAYEVENT_HEADATEND, callback event will be checked against mask
   1367     }
   1368 
   1369     if (eventFlags & SL_PLAYEVENT_HEADMOVING) {
   1370         // FIXME support SL_PLAYEVENT_HEADMOVING
   1371         SL_LOGD("[ FIXME: IPlay_SetCallbackEventsMask(SL_PLAYEVENT_HEADMOVING) on an "
   1372             "SL_OBJECTID_AUDIOPLAYER to be implemented ]");
   1373     }
   1374     if (eventFlags & SL_PLAYEVENT_HEADSTALLED) {
   1375         // nothing to do for SL_PLAYEVENT_HEADSTALLED, callback event will be checked against mask
   1376     }
   1377 
   1378 }
   1379 
   1380 
   1381 //-----------------------------------------------------------------------------
   1382 SLresult android_audioPlayer_getDuration(IPlay *pPlayItf, SLmillisecond *pDurMsec) {
   1383     CAudioPlayer *ap = (CAudioPlayer *)pPlayItf->mThis;
   1384     switch(ap->mAndroidObjType) {
   1385     case AUDIOTRACK_PULL:
   1386         *pDurMsec = SL_TIME_UNKNOWN;
   1387         // FIXME if the data source is not a buffer queue, and the audio data is saved in
   1388         //       shared memory with the mixer process, the duration is the size of the buffer
   1389         SL_LOGD("FIXME: android_audioPlayer_getDuration() verify if duration can be retrieved");
   1390         break;
   1391 #ifndef USE_BACKPORT
   1392     case MEDIAPLAYER: {
   1393         int64_t durationUsec = SL_TIME_UNKNOWN;
   1394         if (ap->mSfPlayer != 0) {
   1395             durationUsec = ap->mSfPlayer->getDurationUsec();
   1396             *pDurMsec = durationUsec == -1 ? SL_TIME_UNKNOWN : durationUsec / 1000;
   1397         }
   1398         } break;
   1399 #endif
   1400     default:
   1401         break;
   1402     }
   1403     return SL_RESULT_SUCCESS;
   1404 }
   1405 
   1406 
   1407 //-----------------------------------------------------------------------------
   1408 void android_audioPlayer_getPosition(IPlay *pPlayItf, SLmillisecond *pPosMsec) {
   1409     CAudioPlayer *ap = (CAudioPlayer *)pPlayItf->mThis;
   1410     switch(ap->mAndroidObjType) {
   1411     case AUDIOTRACK_PULL:
   1412         if ((ap->mSampleRateMilliHz == 0) || (NULL == ap->mAudioTrack)) {
   1413             *pPosMsec = 0;
   1414         } else {
   1415             uint32_t positionInFrames;
   1416             ap->mAudioTrack->getPosition(&positionInFrames);
   1417             *pPosMsec = ((int64_t)positionInFrames * 1000) /
   1418                     sles_to_android_sampleRate(ap->mSampleRateMilliHz);
   1419         }
   1420         break;
   1421     case MEDIAPLAYER:
   1422         if (ap->mSfPlayer != 0) {
   1423             *pPosMsec = ap->mSfPlayer->getPositionMsec();
   1424         } else {
   1425             *pPosMsec = 0;
   1426         }
   1427         break;
   1428     default:
   1429         break;
   1430     }
   1431 }
   1432 
   1433 
   1434 //-----------------------------------------------------------------------------
   1435 void android_audioPlayer_seek(CAudioPlayer *ap, SLmillisecond posMsec) {
   1436 
   1437     switch(ap->mAndroidObjType) {
   1438     case AUDIOTRACK_PULL:
   1439         break;
   1440 #ifndef USE_BACKPORT
   1441     case MEDIAPLAYER:
   1442         if (ap->mSfPlayer != 0) {
   1443             ap->mSfPlayer->seek(posMsec);
   1444         }
   1445         break;
   1446 #endif
   1447     default:
   1448         break;
   1449     }
   1450 }
   1451 
   1452 
   1453 //-----------------------------------------------------------------------------
   1454 void android_audioPlayer_loop(CAudioPlayer *ap, SLboolean loopEnable) {
   1455 
   1456     if ((MEDIAPLAYER == ap->mAndroidObjType) && (ap->mSfPlayer != 0)) {
   1457         ap->mSfPlayer->loop((bool)loopEnable);
   1458     }
   1459 }
   1460 
   1461 
   1462 //-----------------------------------------------------------------------------
   1463 /*
   1464  * Mutes or unmutes the Android media framework object associated with the CAudioPlayer that carries
   1465  * the IVolume interface.
   1466  * Pre-condition:
   1467  *   if ap->mMute is SL_BOOLEAN_FALSE, a call to this function was preceded by a call
   1468  *   to android_audioPlayer_volumeUpdate()
   1469  */
   1470 static void android_audioPlayer_setMute(CAudioPlayer* ap) {
   1471     android::AudioTrack *t = NULL;
   1472     switch(ap->mAndroidObjType) {
   1473     case AUDIOTRACK_PULL:
   1474     case MEDIAPLAYER:
   1475         t = ap->mAudioTrack;
   1476         break;
   1477     default:
   1478         break;
   1479     }
   1480     // when unmuting: volume levels have already been updated in IVolume_SetMute
   1481     if (NULL != t) {
   1482         t->mute(ap->mMute);
   1483     }
   1484 }
   1485 
   1486 
   1487 //-----------------------------------------------------------------------------
   1488 SLresult android_audioPlayer_volumeUpdate(CAudioPlayer* ap) {
   1489     android_audioPlayer_updateStereoVolume(ap);
   1490     android_audioPlayer_setMute(ap);
   1491     return SL_RESULT_SUCCESS;
   1492 }
   1493 
   1494 
   1495 //-----------------------------------------------------------------------------
   1496 void android_audioPlayer_bufferQueue_onRefilled(CAudioPlayer *ap) {
   1497     // the AudioTrack associated with the AudioPlayer receiving audio from a PCM buffer
   1498     // queue was stopped when the queue become empty, we restart as soon as a new buffer
   1499     // has been enqueued since we're in playing state
   1500     if (NULL != ap->mAudioTrack) {
   1501         ap->mAudioTrack->start();
   1502     }
   1503 
   1504     // when the queue became empty, an underflow on the prefetch status itf was sent. Now the queue
   1505     // has received new data, signal it has sufficient data
   1506     if (IsInterfaceInitialized(&(ap->mObject), MPH_PREFETCHSTATUS)) {
   1507         audioPlayer_dispatch_prefetchStatus_lockPrefetch(ap, SL_PREFETCHSTATUS_SUFFICIENTDATA,
   1508             true);
   1509     }
   1510 }
   1511 
   1512 
   1513 //-----------------------------------------------------------------------------
   1514 /*
   1515  * BufferQueue::Clear
   1516  */
   1517 SLresult android_audioPlayer_bufferQueue_onClear(CAudioPlayer *ap) {
   1518     SLresult result = SL_RESULT_SUCCESS;
   1519 
   1520     switch (ap->mAndroidObjType) {
   1521     //-----------------------------------
   1522     // AudioTrack
   1523     case AUDIOTRACK_PULL:
   1524         if (NULL != ap->mAudioTrack) {
   1525             ap->mAudioTrack->flush();
   1526         }
   1527         break;
   1528     default:
   1529         result = SL_RESULT_INTERNAL_ERROR;
   1530         break;
   1531     }
   1532 
   1533     return result;
   1534 }
   1535 
   1536