Home | History | Annotate | Download | only in android
      1 /*
      2  * Copyright (C) 2011 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 //#define USE_LOG SLAndroidLogLevel_Verbose
     18 
     19 #include "sles_allinclusive.h"
     20 
     21 #include <media/stagefright/foundation/ADebug.h>
     22 #include <sys/stat.h>
     23 
     24 namespace android {
     25 
     26 //--------------------------------------------------------------------------------------------------
     27 GenericPlayer::GenericPlayer(const AudioPlayback_Parameters* params) :
     28         mDataLocatorType(kDataLocatorNone),
     29         mNotifyClient(NULL),
     30         mNotifyUser(NULL),
     31         mStateFlags(0),
     32         mPlaybackParams(*params),
     33         mDurationMsec(ANDROID_UNKNOWN_TIME),
     34         mCacheStatus(kStatusEmpty),
     35         mCacheFill(0),
     36         mLastNotifiedCacheFill(0),
     37         mCacheFillNotifThreshold(100),
     38         mEventFlags(0),
     39         mMarkerPositionMs(ANDROID_UNKNOWN_TIME),
     40         mPositionUpdatePeriodMs(1000), // per spec
     41         mOneShotGeneration(0),
     42         mDeliveredNewPosMs(ANDROID_UNKNOWN_TIME),
     43         mObservedPositionMs(ANDROID_UNKNOWN_TIME)
     44 {
     45     SL_LOGD("GenericPlayer::GenericPlayer()");
     46 
     47     mLooper = new android::ALooper();
     48 
     49     mAndroidAudioLevels.mFinalVolume[0] = 1.0f;
     50     mAndroidAudioLevels.mFinalVolume[1] = 1.0f;
     51 }
     52 
     53 
     54 GenericPlayer::~GenericPlayer() {
     55     SL_LOGV("GenericPlayer::~GenericPlayer()");
     56 
     57     resetDataLocator();
     58 }
     59 
     60 
     61 void GenericPlayer::init(const notif_cbf_t cbf, void* notifUser) {
     62     SL_LOGD("GenericPlayer::init()");
     63 
     64     {
     65         android::Mutex::Autolock autoLock(mNotifyClientLock);
     66         mNotifyClient = cbf;
     67         mNotifyUser = notifUser;
     68     }
     69 
     70     mLooper->registerHandler(this);
     71     mLooper->start(false /*runOnCallingThread*/, false /*canCallJava*/, PRIORITY_DEFAULT);
     72 }
     73 
     74 
     75 void GenericPlayer::preDestroy() {
     76     SL_LOGD("GenericPlayer::preDestroy()");
     77     {
     78         android::Mutex::Autolock autoLock(mNotifyClientLock);
     79         mNotifyClient = NULL;
     80         mNotifyUser = NULL;
     81     }
     82 
     83     mLooper->stop();
     84     mLooper->unregisterHandler(id());
     85 }
     86 
     87 
     88 void GenericPlayer::setDataSource(const char *uri) {
     89     SL_LOGV("GenericPlayer::setDataSource(uri=%s)", uri);
     90     resetDataLocator();
     91 
     92     mDataLocator.uriRef = uri;
     93 
     94     mDataLocatorType = kDataLocatorUri;
     95 }
     96 
     97 
     98 void GenericPlayer::setDataSource(int fd, int64_t offset, int64_t length, bool closeAfterUse) {
     99     SL_LOGV("GenericPlayer::setDataSource(fd=%d, offset=%lld, length=%lld, closeAfterUse=%s)", fd,
    100             offset, length, closeAfterUse ? "true" : "false");
    101     resetDataLocator();
    102 
    103     mDataLocator.fdi.fd = fd;
    104 
    105     struct stat sb;
    106     int ret = fstat(fd, &sb);
    107     if (ret != 0) {
    108         SL_LOGE("GenericPlayer::setDataSource: fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
    109         return;
    110     }
    111 
    112     if (offset >= sb.st_size) {
    113         SL_LOGE("SfPlayer::setDataSource: invalid offset");
    114         return;
    115     }
    116     mDataLocator.fdi.offset = offset;
    117 
    118     if (PLAYER_FD_FIND_FILE_SIZE == length) {
    119         mDataLocator.fdi.length = sb.st_size;
    120     } else if (offset + length > sb.st_size) {
    121         mDataLocator.fdi.length = sb.st_size - offset;
    122     } else {
    123         mDataLocator.fdi.length = length;
    124     }
    125 
    126     mDataLocator.fdi.mCloseAfterUse = closeAfterUse;
    127 
    128     mDataLocatorType = kDataLocatorFd;
    129 }
    130 
    131 
    132 void GenericPlayer::prepare() {
    133     SL_LOGD("GenericPlayer::prepare()");
    134     // do not attempt prepare more than once
    135     if (!(mStateFlags & (kFlagPrepared | kFlagPreparedUnsuccessfully))) {
    136         sp<AMessage> msg = new AMessage(kWhatPrepare, id());
    137         msg->post();
    138     }
    139 }
    140 
    141 
    142 void GenericPlayer::play() {
    143     SL_LOGD("GenericPlayer::play()");
    144     sp<AMessage> msg = new AMessage(kWhatPlay, id());
    145     msg->post();
    146 }
    147 
    148 
    149 void GenericPlayer::pause() {
    150     SL_LOGD("GenericPlayer::pause()");
    151     sp<AMessage> msg = new AMessage(kWhatPause, id());
    152     msg->post();
    153 }
    154 
    155 
    156 void GenericPlayer::stop() {
    157     SL_LOGD("GenericPlayer::stop()");
    158     (new AMessage(kWhatPause, id()))->post();
    159 
    160     // after a stop, playback should resume from the start.
    161     seek(0);
    162 }
    163 
    164 
    165 void GenericPlayer::seek(int64_t timeMsec) {
    166     SL_LOGV("GenericPlayer::seek %lld", timeMsec);
    167     if (timeMsec < 0 && timeMsec != ANDROID_UNKNOWN_TIME) {
    168         SL_LOGE("GenericPlayer::seek error, can't seek to negative time %lldms", timeMsec);
    169         return;
    170     }
    171     sp<AMessage> msg = new AMessage(kWhatSeek, id());
    172     msg->setInt64(WHATPARAM_SEEK_SEEKTIME_MS, timeMsec);
    173     msg->post();
    174 }
    175 
    176 
    177 void GenericPlayer::loop(bool loop) {
    178     SL_LOGV("GenericPlayer::loop %s", loop ? "true" : "false");
    179     sp<AMessage> msg = new AMessage(kWhatLoop, id());
    180     msg->setInt32(WHATPARAM_LOOP_LOOPING, (int32_t)loop);
    181     msg->post();
    182 }
    183 
    184 
    185 void GenericPlayer::setBufferingUpdateThreshold(int16_t thresholdPercent) {
    186     SL_LOGV("GenericPlayer::setBufferingUpdateThreshold %d", thresholdPercent);
    187     sp<AMessage> msg = new AMessage(kWhatBuffUpdateThres, id());
    188     msg->setInt32(WHATPARAM_BUFFERING_UPDATETHRESHOLD_PERCENT, (int32_t)thresholdPercent);
    189     msg->post();
    190 }
    191 
    192 
    193 //--------------------------------------------------
    194 void GenericPlayer::getDurationMsec(int* msec) {
    195     Mutex::Autolock _l(mSettingsLock);
    196     *msec = mDurationMsec;
    197 }
    198 
    199 //--------------------------------------------------
    200 void GenericPlayer::setVolume(float leftVol, float rightVol)
    201 {
    202     {
    203         Mutex::Autolock _l(mSettingsLock);
    204         mAndroidAudioLevels.mFinalVolume[0] = leftVol;
    205         mAndroidAudioLevels.mFinalVolume[1] = rightVol;
    206     }
    207     // send a message for the volume to be updated by the object which implements the volume
    208     (new AMessage(kWhatVolumeUpdate, id()))->post();
    209 }
    210 
    211 
    212 //--------------------------------------------------
    213 void GenericPlayer::attachAuxEffect(int32_t effectId)
    214 {
    215     SL_LOGV("GenericPlayer::attachAuxEffect(id=%d)", effectId);
    216     sp<AMessage> msg = new AMessage(kWhatAttachAuxEffect, id());
    217     msg->setInt32(WHATPARAM_ATTACHAUXEFFECT, effectId);
    218     msg->post();
    219 }
    220 
    221 
    222 //--------------------------------------------------
    223 void GenericPlayer::setAuxEffectSendLevel(float level)
    224 {
    225     SL_LOGV("GenericPlayer::setAuxEffectSendLevel(level=%g)", level);
    226     sp<AMessage> msg = new AMessage(kWhatSetAuxEffectSendLevel, id());
    227     msg->setFloat(WHATPARAM_SETAUXEFFECTSENDLEVEL, level);
    228     msg->post();
    229 }
    230 
    231 
    232 //--------------------------------------------------
    233 // Call after changing any of the IPlay settings related to SL_PLAYEVENT_*
    234 void GenericPlayer::setPlayEvents(int32_t eventFlags, int32_t markerPositionMs,
    235         int32_t positionUpdatePeriodMs)
    236 {
    237     // Normalize ms that are within the valid unsigned range, but not in the int32_t range
    238     if (markerPositionMs < 0) {
    239         markerPositionMs = ANDROID_UNKNOWN_TIME;
    240     }
    241     if (positionUpdatePeriodMs < 0) {
    242         positionUpdatePeriodMs = ANDROID_UNKNOWN_TIME;
    243     }
    244     // markers are delivered accurately, but new position updates are limited to every 100 ms
    245     if (positionUpdatePeriodMs < 100) {
    246         positionUpdatePeriodMs = 100;
    247     }
    248     sp<AMessage> msg = new AMessage(kWhatSetPlayEvents, id());
    249     msg->setInt32(WHATPARAM_SETPLAYEVENTS_FLAGS, eventFlags);
    250     msg->setInt32(WHATPARAM_SETPLAYEVENTS_MARKER, markerPositionMs);
    251     msg->setInt32(WHATPARAM_SETPLAYEVENTS_UPDATE, positionUpdatePeriodMs);
    252     msg->post();
    253 }
    254 
    255 
    256 //--------------------------------------------------
    257 /*
    258  * post-condition: mDataLocatorType == kDataLocatorNone
    259  *
    260  */
    261 void GenericPlayer::resetDataLocator() {
    262     SL_LOGV("GenericPlayer::resetDataLocator()");
    263     if (mDataLocatorType == kDataLocatorFd && mDataLocator.fdi.mCloseAfterUse) {
    264         (void) ::close(mDataLocator.fdi.fd);
    265         // would be redundant, as we're about to invalidate the union mDataLocator
    266         //mDataLocator.fdi.fd = -1;
    267         //mDataLocator.fdi.mCloseAfterUse = false;
    268     }
    269     mDataLocatorType = kDataLocatorNone;
    270 }
    271 
    272 
    273 void GenericPlayer::notify(const char* event, int data, bool async) {
    274     SL_LOGV("GenericPlayer::notify(event=%s, data=%d, async=%s)", event, data,
    275             async ? "true" : "false");
    276     sp<AMessage> msg = new AMessage(kWhatNotif, id());
    277     msg->setInt32(event, (int32_t)data);
    278     if (async) {
    279         msg->post();
    280     } else {
    281         this->onNotify(msg);
    282     }
    283 }
    284 
    285 
    286 void GenericPlayer::notify(const char* event, int data1, int data2, bool async) {
    287     SL_LOGV("GenericPlayer::notify(event=%s, data1=%d, data2=%d, async=%s)", event, data1, data2,
    288             async ? "true" : "false");
    289     sp<AMessage> msg = new AMessage(kWhatNotif, id());
    290     msg->setRect(event, 0, 0, (int32_t)data1, (int32_t)data2);
    291     if (async) {
    292         msg->post();
    293     } else {
    294         this->onNotify(msg);
    295     }
    296 }
    297 
    298 
    299 //--------------------------------------------------
    300 // AHandler implementation
    301 void GenericPlayer::onMessageReceived(const sp<AMessage> &msg) {
    302     SL_LOGV("GenericPlayer::onMessageReceived()");
    303     switch (msg->what()) {
    304         case kWhatPrepare:
    305             SL_LOGV("kWhatPrepare");
    306             onPrepare();
    307             break;
    308 
    309         case kWhatNotif:
    310             SL_LOGV("kWhatNotif");
    311             onNotify(msg);
    312             break;
    313 
    314         case kWhatPlay:
    315             SL_LOGV("kWhatPlay");
    316             onPlay();
    317             break;
    318 
    319         case kWhatPause:
    320             SL_LOGV("kWhatPause");
    321             onPause();
    322             break;
    323 
    324         case kWhatSeek:
    325             SL_LOGV("kWhatSeek");
    326             onSeek(msg);
    327             break;
    328 
    329         case kWhatLoop:
    330             SL_LOGV("kWhatLoop");
    331             onLoop(msg);
    332             break;
    333 
    334         case kWhatVolumeUpdate:
    335             SL_LOGV("kWhatVolumeUpdate");
    336             onVolumeUpdate();
    337             break;
    338 
    339         case kWhatSeekComplete:
    340             SL_LOGV("kWhatSeekComplete");
    341             onSeekComplete();
    342             break;
    343 
    344         case kWhatBufferingUpdate:
    345             SL_LOGV("kWhatBufferingUpdate");
    346             onBufferingUpdate(msg);
    347             break;
    348 
    349         case kWhatBuffUpdateThres:
    350             SL_LOGV("kWhatBuffUpdateThres");
    351             onSetBufferingUpdateThreshold(msg);
    352             break;
    353 
    354         case kWhatAttachAuxEffect:
    355             SL_LOGV("kWhatAttachAuxEffect");
    356             onAttachAuxEffect(msg);
    357             break;
    358 
    359         case kWhatSetAuxEffectSendLevel:
    360             SL_LOGV("kWhatSetAuxEffectSendLevel");
    361             onSetAuxEffectSendLevel(msg);
    362             break;
    363 
    364         case kWhatSetPlayEvents:
    365             SL_LOGV("kWhatSetPlayEvents");
    366             onSetPlayEvents(msg);
    367             break;
    368 
    369         case kWhatOneShot:
    370             SL_LOGV("kWhatOneShot");
    371             onOneShot(msg);
    372             break;
    373 
    374         default:
    375             SL_LOGE("GenericPlayer::onMessageReceived unknown message %d", msg->what());
    376             TRESPASS();
    377     }
    378 }
    379 
    380 
    381 //--------------------------------------------------
    382 // Event handlers
    383 //  it is strictly verboten to call those methods outside of the event loop
    384 
    385 void GenericPlayer::onPrepare() {
    386     SL_LOGV("GenericPlayer::onPrepare()");
    387     // Subclass is responsible for indicating whether prepare was successful or unsuccessful
    388     // by updating mStateFlags accordingly.  It must set exactly one of these two flags.
    389     assert(!(mStateFlags & kFlagPrepared) != !(mStateFlags & kFlagPreparedUnsuccessfully));
    390     notify(PLAYEREVENT_PREPARED, mStateFlags & kFlagPrepared ? PLAYER_SUCCESS : PLAYER_FAILURE,
    391             true /*async*/);
    392     SL_LOGD("GenericPlayer::onPrepare() done, mStateFlags=0x%x", mStateFlags);
    393 }
    394 
    395 
    396 void GenericPlayer::onNotify(const sp<AMessage> &msg) {
    397     SL_LOGV("GenericPlayer::onNotify()");
    398     notif_cbf_t notifClient;
    399     void*       notifUser;
    400     {
    401         android::Mutex::Autolock autoLock(mNotifyClientLock);
    402         if (NULL == mNotifyClient) {
    403             return;
    404         } else {
    405             notifClient = mNotifyClient;
    406             notifUser   = mNotifyUser;
    407         }
    408     }
    409 
    410     int32_t val1, val2;
    411     if (msg->findInt32(PLAYEREVENT_PREFETCHSTATUSCHANGE, &val1)) {
    412         SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_PREFETCHSTATUSCHANGE, val1);
    413         notifClient(kEventPrefetchStatusChange, val1, 0, notifUser);
    414     } else if (msg->findInt32(PLAYEREVENT_PREFETCHFILLLEVELUPDATE, &val1)) {
    415         SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_PREFETCHFILLLEVELUPDATE, val1);
    416         notifClient(kEventPrefetchFillLevelUpdate, val1, 0, notifUser);
    417     } else if (msg->findInt32(PLAYEREVENT_ENDOFSTREAM, &val1)) {
    418         SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_ENDOFSTREAM, val1);
    419         notifClient(kEventEndOfStream, val1, 0, notifUser);
    420     } else if (msg->findInt32(PLAYEREVENT_PREPARED, &val1)) {
    421         SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_PREPARED, val1);
    422         notifClient(kEventPrepared, val1, 0, notifUser);
    423     } else if (msg->findInt32(PLAYEREVENT_CHANNEL_COUNT, &val1)) {
    424         SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_CHANNEL_COUNT, val1);
    425         notifClient(kEventChannelCount, val1, 0, notifUser);
    426     } else if (msg->findRect(PLAYEREVENT_VIDEO_SIZE_UPDATE, &val1, &val2, &val1, &val2)) {
    427         SL_LOGV("GenericPlayer notifying %s = %d, %d", PLAYEREVENT_VIDEO_SIZE_UPDATE, val1, val2);
    428         notifClient(kEventHasVideoSize, val1, val2, notifUser);
    429     } else if (msg->findInt32(PLAYEREVENT_PLAY, &val1)) {
    430         SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_PLAY, val1);
    431         notifClient(kEventPlay, val1, 0, notifUser);
    432     } else if (msg->findInt32(PLAYEREVENT_ERRORAFTERPREPARE, &val1)) {
    433         SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_ERRORAFTERPREPARE, val1);
    434         notifClient(kEventErrorAfterPrepare, val1, 0, notifUser);
    435     } else {
    436         SL_LOGV("GenericPlayer notifying unknown");
    437     }
    438 }
    439 
    440 
    441 void GenericPlayer::onPlay() {
    442     SL_LOGD("GenericPlayer::onPlay()");
    443     if ((mStateFlags & (kFlagPrepared | kFlagPlaying)) == kFlagPrepared) {
    444         SL_LOGD("starting player");
    445         mStateFlags |= kFlagPlaying;
    446         updateOneShot();
    447     }
    448 }
    449 
    450 
    451 void GenericPlayer::onPause() {
    452     SL_LOGD("GenericPlayer::onPause()");
    453     if (!(~mStateFlags & (kFlagPrepared | kFlagPlaying))) {
    454         SL_LOGV("pausing player");
    455         mStateFlags &= ~kFlagPlaying;
    456         updateOneShot();
    457     }
    458 }
    459 
    460 
    461 void GenericPlayer::onSeek(const sp<AMessage> &msg) {
    462     SL_LOGV("GenericPlayer::onSeek");
    463 }
    464 
    465 
    466 void GenericPlayer::onLoop(const sp<AMessage> &msg) {
    467     SL_LOGV("GenericPlayer::onLoop");
    468 }
    469 
    470 
    471 void GenericPlayer::onVolumeUpdate() {
    472     SL_LOGV("GenericPlayer::onVolumeUpdate");
    473 }
    474 
    475 
    476 void GenericPlayer::onSeekComplete() {
    477     SL_LOGD("GenericPlayer::onSeekComplete()");
    478     mStateFlags &= ~kFlagSeeking;
    479     // avoid spurious or lost events caused by seeking past a marker
    480     mDeliveredNewPosMs = ANDROID_UNKNOWN_TIME;
    481     mObservedPositionMs = ANDROID_UNKNOWN_TIME;
    482     updateOneShot();
    483 }
    484 
    485 
    486 void GenericPlayer::onBufferingUpdate(const sp<AMessage> &msg) {
    487     SL_LOGV("GenericPlayer::onBufferingUpdate");
    488 }
    489 
    490 
    491 void GenericPlayer::onSetBufferingUpdateThreshold(const sp<AMessage> &msg) {
    492     SL_LOGV("GenericPlayer::onSetBufferingUpdateThreshold");
    493     int32_t thresholdPercent = 0;
    494     if (msg->findInt32(WHATPARAM_BUFFERING_UPDATETHRESHOLD_PERCENT, &thresholdPercent)) {
    495         Mutex::Autolock _l(mSettingsLock);
    496         mCacheFillNotifThreshold = (int16_t)thresholdPercent;
    497     }
    498 }
    499 
    500 
    501 void GenericPlayer::onAttachAuxEffect(const sp<AMessage> &msg) {
    502     SL_LOGV("GenericPlayer::onAttachAuxEffect()");
    503 }
    504 
    505 
    506 void GenericPlayer::onSetAuxEffectSendLevel(const sp<AMessage> &msg) {
    507     SL_LOGV("GenericPlayer::onSetAuxEffectSendLevel()");
    508 }
    509 
    510 
    511 void GenericPlayer::onSetPlayEvents(const sp<AMessage> &msg) {
    512     SL_LOGV("GenericPlayer::onSetPlayEvents()");
    513     int32_t eventFlags, markerPositionMs, positionUpdatePeriodMs;
    514     if (msg->findInt32(WHATPARAM_SETPLAYEVENTS_FLAGS, &eventFlags) &&
    515             msg->findInt32(WHATPARAM_SETPLAYEVENTS_MARKER, &markerPositionMs) &&
    516             msg->findInt32(WHATPARAM_SETPLAYEVENTS_UPDATE, &positionUpdatePeriodMs)) {
    517         mEventFlags = eventFlags;
    518         mMarkerPositionMs = markerPositionMs;
    519         mPositionUpdatePeriodMs = positionUpdatePeriodMs;
    520         updateOneShot();
    521     }
    522 }
    523 
    524 
    525 void GenericPlayer::onOneShot(const sp<AMessage> &msg) {
    526     SL_LOGV("GenericPlayer::onOneShot()");
    527     int32_t generation;
    528     if (msg->findInt32(WHATPARAM_ONESHOT_GENERATION, &generation)) {
    529         if (generation != mOneShotGeneration) {
    530             SL_LOGV("GenericPlayer::onOneShot() generation %d cancelled; latest is %d",
    531                     generation, mOneShotGeneration);
    532             return;
    533         }
    534         updateOneShot();
    535     }
    536 }
    537 
    538 
    539 //-------------------------------------------------
    540 void GenericPlayer::notifyStatus() {
    541     SL_LOGV("GenericPlayer::notifyStatus");
    542     notify(PLAYEREVENT_PREFETCHSTATUSCHANGE, (int32_t)mCacheStatus, true /*async*/);
    543 }
    544 
    545 
    546 void GenericPlayer::notifyCacheFill() {
    547     SL_LOGV("GenericPlayer::notifyCacheFill");
    548     mLastNotifiedCacheFill = mCacheFill;
    549     notify(PLAYEREVENT_PREFETCHFILLLEVELUPDATE, (int32_t)mLastNotifiedCacheFill, true/*async*/);
    550 }
    551 
    552 
    553 void GenericPlayer::seekComplete() {
    554     SL_LOGV("GenericPlayer::seekComplete");
    555     sp<AMessage> msg = new AMessage(kWhatSeekComplete, id());
    556     msg->post();
    557 }
    558 
    559 
    560 void GenericPlayer::bufferingUpdate(int16_t fillLevelPerMille) {
    561     SL_LOGV("GenericPlayer::bufferingUpdate");
    562     sp<AMessage> msg = new AMessage(kWhatBufferingUpdate, id());
    563     msg->setInt32(WHATPARAM_BUFFERING_UPDATE, fillLevelPerMille);
    564     msg->post();
    565 }
    566 
    567 
    568 // For the meaning of positionMs, see comment in declaration at android_GenericPlayer.h
    569 void GenericPlayer::updateOneShot(int positionMs)
    570 {
    571     SL_LOGV("GenericPlayer::updateOneShot");
    572 
    573     // nop until prepared
    574     if (!(mStateFlags & kFlagPrepared)) {
    575         return;
    576     }
    577 
    578     // cancel any pending one-shot(s)
    579     ++mOneShotGeneration;
    580 
    581     // don't restart one-shot if player is paused or stopped
    582     if (!(mStateFlags & kFlagPlaying)) {
    583         return;
    584     }
    585 
    586     // get current player position in milliseconds
    587     if (positionMs < 0) {
    588         positionMs = ANDROID_UNKNOWN_TIME;
    589     }
    590     if (positionMs == ANDROID_UNKNOWN_TIME) {
    591         getPositionMsec(&positionMs);
    592         // normalize it
    593         if (positionMs < 0) {
    594             positionMs = ANDROID_UNKNOWN_TIME;
    595         }
    596         if (ANDROID_UNKNOWN_TIME == positionMs) {
    597             // getPositionMsec is not working for some reason, give up
    598             //LOGV("Does anyone really know what time it is?");
    599             return;
    600         }
    601     }
    602 
    603     // if we observe the player position going backwards, even without without a seek, then recover
    604     if (mObservedPositionMs != ANDROID_UNKNOWN_TIME && positionMs < mObservedPositionMs) {
    605         mDeliveredNewPosMs = ANDROID_UNKNOWN_TIME;
    606         mObservedPositionMs = positionMs;
    607     }
    608 
    609     // delayUs is the expected delay between current position and marker;
    610     // the default is infinity in case there are no upcoming marker(s)
    611     int64_t delayUs = -1;
    612 
    613     // is there a marker?
    614     if ((mEventFlags & SL_PLAYEVENT_HEADATMARKER) && (mMarkerPositionMs != ANDROID_UNKNOWN_TIME)) {
    615         // check to see if we have observed the position passing through the marker
    616         if (mObservedPositionMs <= mMarkerPositionMs && mMarkerPositionMs <= positionMs) {
    617             notify(PLAYEREVENT_PLAY, (int32_t) SL_PLAYEVENT_HEADATMARKER, true /*async*/);
    618         } else if (positionMs < mMarkerPositionMs) {
    619             delayUs = (mMarkerPositionMs - positionMs) * 1000LL;
    620         }
    621     }
    622 
    623     // are periodic position updates needed?
    624     if ((mEventFlags & SL_PLAYEVENT_HEADATNEWPOS) &&
    625             (mPositionUpdatePeriodMs != ANDROID_UNKNOWN_TIME)) {
    626         // check to see if we have observed the position passing through a virtual marker, where the
    627         // virtual marker is at the previously delivered new position plus position update period
    628         int32_t virtualMarkerMs;
    629         if (mDeliveredNewPosMs != ANDROID_UNKNOWN_TIME) {
    630             virtualMarkerMs = mDeliveredNewPosMs + mPositionUpdatePeriodMs;
    631         } else if (mObservedPositionMs != ANDROID_UNKNOWN_TIME) {
    632             virtualMarkerMs = mObservedPositionMs + mPositionUpdatePeriodMs;
    633             // pretend there has been an update in the past
    634             mDeliveredNewPosMs = mObservedPositionMs;
    635         } else {
    636             virtualMarkerMs = positionMs + mPositionUpdatePeriodMs;
    637             // pretend there has been an update in the past
    638             mDeliveredNewPosMs = positionMs;
    639         }
    640         // nextVirtualMarkerMs will be set to the position of the next upcoming virtual marker
    641         int32_t nextVirtualMarkerMs;
    642         if (mObservedPositionMs <= virtualMarkerMs && virtualMarkerMs <= positionMs) {
    643             // we did pass through the virtual marker, now compute the next virtual marker
    644             mDeliveredNewPosMs = virtualMarkerMs;
    645             nextVirtualMarkerMs = virtualMarkerMs + mPositionUpdatePeriodMs;
    646             // re-synchronize if we missed an update
    647             if (nextVirtualMarkerMs <= positionMs) {
    648                 SL_LOGW("Missed SL_PLAYEVENT_HEADATNEWPOS for position %d; current position %d",
    649                         nextVirtualMarkerMs, positionMs);
    650                 // try to catch up by setting next goal to current position plus update period
    651                 mDeliveredNewPosMs = positionMs;
    652                 nextVirtualMarkerMs = positionMs + mPositionUpdatePeriodMs;
    653             }
    654             notify(PLAYEREVENT_PLAY, (int32_t) SL_PLAYEVENT_HEADATNEWPOS, true /*async*/);
    655         } else {
    656             // we did not pass through the virtual marker yet, so use same marker again
    657             nextVirtualMarkerMs = virtualMarkerMs;
    658         }
    659         // note that if arithmetic overflow occurred, nextVirtualMarkerMs will be negative
    660         if (positionMs < nextVirtualMarkerMs) {
    661             int64_t trialDelayUs;
    662             trialDelayUs = (nextVirtualMarkerMs - positionMs) * 1000LL;
    663             if (trialDelayUs > 0 && (delayUs == -1 || trialDelayUs < delayUs)) {
    664                 delayUs = trialDelayUs;
    665             }
    666         }
    667     }
    668 
    669     // we have a new observed position
    670     mObservedPositionMs = positionMs;
    671 
    672     // post the new one-shot message if needed
    673     if (advancesPositionInRealTime() && delayUs >= 0) {
    674         // 20 ms min delay to avoid near busy waiting
    675         if (delayUs < 20000LL) {
    676             delayUs = 20000LL;
    677         }
    678         // 1 minute max delay avoids indefinite memory leaks caused by cancelled one-shots
    679         if (delayUs > 60000000LL) {
    680             delayUs = 60000000LL;
    681         }
    682         //SL_LOGI("delayUs = %lld", delayUs);
    683         sp<AMessage> msg = new AMessage(kWhatOneShot, id());
    684         msg->setInt32(WHATPARAM_ONESHOT_GENERATION, mOneShotGeneration);
    685         msg->post(delayUs);
    686     }
    687 
    688 }
    689 
    690 } // namespace android
    691