android_GenericPlayer.cpp revision b4393ef4ef3edb785746c37fd7b68950e85283ae
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
24namespace android {
25
26//--------------------------------------------------------------------------------------------------
27GenericPlayer::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
54GenericPlayer::~GenericPlayer() {
55    SL_LOGV("GenericPlayer::~GenericPlayer()");
56
57    resetDataLocator();
58}
59
60
61void 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
75void 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
88void 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
98void 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
132void 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
142void GenericPlayer::play() {
143    SL_LOGD("GenericPlayer::play()");
144    sp<AMessage> msg = new AMessage(kWhatPlay, id());
145    msg->post();
146}
147
148
149void GenericPlayer::pause() {
150    SL_LOGD("GenericPlayer::pause()");
151    sp<AMessage> msg = new AMessage(kWhatPause, id());
152    msg->post();
153}
154
155
156void 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
165void 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
177void 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
185void 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//--------------------------------------------------
194void GenericPlayer::getDurationMsec(int* msec) {
195    *msec = mDurationMsec;
196}
197
198//--------------------------------------------------
199void GenericPlayer::setVolume(float leftVol, float rightVol)
200{
201    {
202        Mutex::Autolock _l(mSettingsLock);
203        mAndroidAudioLevels.mFinalVolume[0] = leftVol;
204        mAndroidAudioLevels.mFinalVolume[1] = rightVol;
205    }
206    // send a message for the volume to be updated by the object which implements the volume
207    (new AMessage(kWhatVolumeUpdate, id()))->post();
208}
209
210
211//--------------------------------------------------
212void GenericPlayer::attachAuxEffect(int32_t effectId)
213{
214    SL_LOGV("GenericPlayer::attachAuxEffect(id=%d)", effectId);
215    sp<AMessage> msg = new AMessage(kWhatAttachAuxEffect, id());
216    msg->setInt32(WHATPARAM_ATTACHAUXEFFECT, effectId);
217    msg->post();
218}
219
220
221//--------------------------------------------------
222void GenericPlayer::setAuxEffectSendLevel(float level)
223{
224    SL_LOGV("GenericPlayer::setAuxEffectSendLevel(level=%g)", level);
225    sp<AMessage> msg = new AMessage(kWhatSetAuxEffectSendLevel, id());
226    msg->setFloat(WHATPARAM_SETAUXEFFECTSENDLEVEL, level);
227    msg->post();
228}
229
230
231//--------------------------------------------------
232// Call after changing any of the IPlay settings related to SL_PLAYEVENT_*
233void GenericPlayer::setPlayEvents(int32_t eventFlags, int32_t markerPositionMs,
234        int32_t positionUpdatePeriodMs)
235{
236    // Normalize ms that are within the valid unsigned range, but not in the int32_t range
237    if (markerPositionMs < 0) {
238        markerPositionMs = ANDROID_UNKNOWN_TIME;
239    }
240    if (positionUpdatePeriodMs < 0) {
241        positionUpdatePeriodMs = ANDROID_UNKNOWN_TIME;
242    }
243    // markers are delivered accurately, but new position updates are limited to every 100 ms
244    if (positionUpdatePeriodMs < 100) {
245        positionUpdatePeriodMs = 100;
246    }
247    sp<AMessage> msg = new AMessage(kWhatSetPlayEvents, id());
248    msg->setInt32(WHATPARAM_SETPLAYEVENTS_FLAGS, eventFlags);
249    msg->setInt32(WHATPARAM_SETPLAYEVENTS_MARKER, markerPositionMs);
250    msg->setInt32(WHATPARAM_SETPLAYEVENTS_UPDATE, positionUpdatePeriodMs);
251    msg->post();
252}
253
254
255//--------------------------------------------------
256/*
257 * post-condition: mDataLocatorType == kDataLocatorNone
258 *
259 */
260void GenericPlayer::resetDataLocator() {
261    SL_LOGV("GenericPlayer::resetDataLocator()");
262    if (mDataLocatorType == kDataLocatorFd && mDataLocator.fdi.mCloseAfterUse) {
263        (void) ::close(mDataLocator.fdi.fd);
264        // would be redundant, as we're about to invalidate the union mDataLocator
265        //mDataLocator.fdi.fd = -1;
266        //mDataLocator.fdi.mCloseAfterUse = false;
267    }
268    mDataLocatorType = kDataLocatorNone;
269}
270
271
272void GenericPlayer::notify(const char* event, int data, bool async) {
273    SL_LOGV("GenericPlayer::notify(event=%s, data=%d, async=%s)", event, data,
274            async ? "true" : "false");
275    sp<AMessage> msg = new AMessage(kWhatNotif, id());
276    msg->setInt32(event, (int32_t)data);
277    if (async) {
278        msg->post();
279    } else {
280        this->onNotify(msg);
281    }
282}
283
284
285void GenericPlayer::notify(const char* event, int data1, int data2, bool async) {
286    SL_LOGV("GenericPlayer::notify(event=%s, data1=%d, data2=%d, async=%s)", event, data1, data2,
287            async ? "true" : "false");
288    sp<AMessage> msg = new AMessage(kWhatNotif, id());
289    msg->setRect(event, 0, 0, (int32_t)data1, (int32_t)data2);
290    if (async) {
291        msg->post();
292    } else {
293        this->onNotify(msg);
294    }
295}
296
297
298//--------------------------------------------------
299// AHandler implementation
300void GenericPlayer::onMessageReceived(const sp<AMessage> &msg) {
301    SL_LOGV("GenericPlayer::onMessageReceived()");
302    switch (msg->what()) {
303        case kWhatPrepare:
304            SL_LOGV("kWhatPrepare");
305            onPrepare();
306            break;
307
308        case kWhatNotif:
309            SL_LOGV("kWhatNotif");
310            onNotify(msg);
311            break;
312
313        case kWhatPlay:
314            SL_LOGV("kWhatPlay");
315            onPlay();
316            break;
317
318        case kWhatPause:
319            SL_LOGV("kWhatPause");
320            onPause();
321            break;
322
323        case kWhatSeek:
324            SL_LOGV("kWhatSeek");
325            onSeek(msg);
326            break;
327
328        case kWhatLoop:
329            SL_LOGV("kWhatLoop");
330            onLoop(msg);
331            break;
332
333        case kWhatVolumeUpdate:
334            SL_LOGV("kWhatVolumeUpdate");
335            onVolumeUpdate();
336            break;
337
338        case kWhatSeekComplete:
339            SL_LOGV("kWhatSeekComplete");
340            onSeekComplete();
341            break;
342
343        case kWhatBufferingUpdate:
344            SL_LOGV("kWhatBufferingUpdate");
345            onBufferingUpdate(msg);
346            break;
347
348        case kWhatBuffUpdateThres:
349            SL_LOGV("kWhatBuffUpdateThres");
350            onSetBufferingUpdateThreshold(msg);
351            break;
352
353        case kWhatAttachAuxEffect:
354            SL_LOGV("kWhatAttachAuxEffect");
355            onAttachAuxEffect(msg);
356            break;
357
358        case kWhatSetAuxEffectSendLevel:
359            SL_LOGV("kWhatSetAuxEffectSendLevel");
360            onSetAuxEffectSendLevel(msg);
361            break;
362
363        case kWhatSetPlayEvents:
364            SL_LOGV("kWhatSetPlayEvents");
365            onSetPlayEvents(msg);
366            break;
367
368        case kWhatOneShot:
369            SL_LOGV("kWhatOneShot");
370            onOneShot(msg);
371            break;
372
373        default:
374            SL_LOGE("GenericPlayer::onMessageReceived unknown message %d", msg->what());
375            TRESPASS();
376    }
377}
378
379
380//--------------------------------------------------
381// Event handlers
382//  it is strictly verboten to call those methods outside of the event loop
383
384void GenericPlayer::onPrepare() {
385    SL_LOGV("GenericPlayer::onPrepare()");
386    // Subclass is responsible for indicating whether prepare was successful or unsuccessful
387    // by updating mStateFlags accordingly.  It must set exactly one of these two flags.
388    assert(!(mStateFlags & kFlagPrepared) != !(mStateFlags & kFlagPreparedUnsuccessfully));
389    notify(PLAYEREVENT_PREPARED, mStateFlags & kFlagPrepared ? PLAYER_SUCCESS : PLAYER_FAILURE,
390            true /*async*/);
391    SL_LOGD("GenericPlayer::onPrepare() done, mStateFlags=0x%x", mStateFlags);
392}
393
394
395void GenericPlayer::onNotify(const sp<AMessage> &msg) {
396    SL_LOGV("GenericPlayer::onNotify()");
397    notif_cbf_t notifClient;
398    void*       notifUser;
399    {
400        android::Mutex::Autolock autoLock(mNotifyClientLock);
401        if (NULL == mNotifyClient) {
402            return;
403        } else {
404            notifClient = mNotifyClient;
405            notifUser   = mNotifyUser;
406        }
407    }
408
409    int32_t val1, val2;
410    if (msg->findInt32(PLAYEREVENT_PREFETCHSTATUSCHANGE, &val1)) {
411        SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_PREFETCHSTATUSCHANGE, val1);
412        notifClient(kEventPrefetchStatusChange, val1, 0, notifUser);
413    } else if (msg->findInt32(PLAYEREVENT_PREFETCHFILLLEVELUPDATE, &val1)) {
414        SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_PREFETCHFILLLEVELUPDATE, val1);
415        notifClient(kEventPrefetchFillLevelUpdate, val1, 0, notifUser);
416    } else if (msg->findInt32(PLAYEREVENT_ENDOFSTREAM, &val1)) {
417        SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_ENDOFSTREAM, val1);
418        notifClient(kEventEndOfStream, val1, 0, notifUser);
419    } else if (msg->findInt32(PLAYEREVENT_PREPARED, &val1)) {
420        SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_PREPARED, val1);
421        notifClient(kEventPrepared, val1, 0, notifUser);
422    } else if (msg->findInt32(PLAYEREVENT_CHANNEL_COUNT, &val1)) {
423        SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_CHANNEL_COUNT, val1);
424        notifClient(kEventChannelCount, val1, 0, notifUser);
425    } else if (msg->findRect(PLAYEREVENT_VIDEO_SIZE_UPDATE, &val1, &val2, &val1, &val2)) {
426        SL_LOGV("GenericPlayer notifying %s = %d, %d", PLAYEREVENT_VIDEO_SIZE_UPDATE, val1, val2);
427        notifClient(kEventHasVideoSize, val1, val2, notifUser);
428    } else if (msg->findInt32(PLAYEREVENT_PLAY, &val1)) {
429        SL_LOGV("GenericPlayer notifying %s = %d", PLAYEREVENT_PLAY, val1);
430        notifClient(kEventPlay, val1, 0, notifUser);
431    } else {
432        SL_LOGV("GenericPlayer notifying unknown");
433    }
434}
435
436
437void GenericPlayer::onPlay() {
438    SL_LOGD("GenericPlayer::onPlay()");
439    if ((mStateFlags & (kFlagPrepared | kFlagPlaying)) == kFlagPrepared) {
440        SL_LOGD("starting player");
441        mStateFlags |= kFlagPlaying;
442        updateOneShot();
443    }
444}
445
446
447void GenericPlayer::onPause() {
448    SL_LOGD("GenericPlayer::onPause()");
449    if (!(~mStateFlags & (kFlagPrepared | kFlagPlaying))) {
450        SL_LOGV("pausing player");
451        mStateFlags &= ~kFlagPlaying;
452        updateOneShot();
453    }
454}
455
456
457void GenericPlayer::onSeek(const sp<AMessage> &msg) {
458    SL_LOGV("GenericPlayer::onSeek");
459}
460
461
462void GenericPlayer::onLoop(const sp<AMessage> &msg) {
463    SL_LOGV("GenericPlayer::onLoop");
464}
465
466
467void GenericPlayer::onVolumeUpdate() {
468    SL_LOGV("GenericPlayer::onVolumeUpdate");
469}
470
471
472void GenericPlayer::onSeekComplete() {
473    SL_LOGD("GenericPlayer::onSeekComplete()");
474    mStateFlags &= ~kFlagSeeking;
475    // avoid spurious or lost events caused by seeking past a marker
476    mDeliveredNewPosMs = ANDROID_UNKNOWN_TIME;
477    mObservedPositionMs = ANDROID_UNKNOWN_TIME;
478    updateOneShot();
479}
480
481
482void GenericPlayer::onBufferingUpdate(const sp<AMessage> &msg) {
483    SL_LOGV("GenericPlayer::onBufferingUpdate");
484}
485
486
487void GenericPlayer::onSetBufferingUpdateThreshold(const sp<AMessage> &msg) {
488    SL_LOGV("GenericPlayer::onSetBufferingUpdateThreshold");
489    int32_t thresholdPercent = 0;
490    if (msg->findInt32(WHATPARAM_BUFFERING_UPDATETHRESHOLD_PERCENT, &thresholdPercent)) {
491        Mutex::Autolock _l(mSettingsLock);
492        mCacheFillNotifThreshold = (int16_t)thresholdPercent;
493    }
494}
495
496
497void GenericPlayer::onAttachAuxEffect(const sp<AMessage> &msg) {
498    SL_LOGV("GenericPlayer::onAttachAuxEffect()");
499}
500
501
502void GenericPlayer::onSetAuxEffectSendLevel(const sp<AMessage> &msg) {
503    SL_LOGV("GenericPlayer::onSetAuxEffectSendLevel()");
504}
505
506
507void GenericPlayer::onSetPlayEvents(const sp<AMessage> &msg) {
508    SL_LOGV("GenericPlayer::onSetPlayEvents()");
509    int32_t eventFlags, markerPositionMs, positionUpdatePeriodMs;
510    if (msg->findInt32(WHATPARAM_SETPLAYEVENTS_FLAGS, &eventFlags) &&
511            msg->findInt32(WHATPARAM_SETPLAYEVENTS_MARKER, &markerPositionMs) &&
512            msg->findInt32(WHATPARAM_SETPLAYEVENTS_UPDATE, &positionUpdatePeriodMs)) {
513        mEventFlags = eventFlags;
514        mMarkerPositionMs = markerPositionMs;
515        mPositionUpdatePeriodMs = positionUpdatePeriodMs;
516        updateOneShot();
517    }
518}
519
520
521void GenericPlayer::onOneShot(const sp<AMessage> &msg) {
522    SL_LOGV("GenericPlayer::onOneShot()");
523    int32_t generation;
524    if (msg->findInt32(WHATPARAM_ONESHOT_GENERATION, &generation)) {
525        if (generation != mOneShotGeneration) {
526            SL_LOGV("GenericPlayer::onOneShot() generation %d cancelled; latest is %d",
527                    generation, mOneShotGeneration);
528            return;
529        }
530        updateOneShot();
531    }
532}
533
534
535//-------------------------------------------------
536void GenericPlayer::notifyStatus() {
537    SL_LOGV("GenericPlayer::notifyStatus");
538    notify(PLAYEREVENT_PREFETCHSTATUSCHANGE, (int32_t)mCacheStatus, true /*async*/);
539}
540
541
542void GenericPlayer::notifyCacheFill() {
543    SL_LOGV("GenericPlayer::notifyCacheFill");
544    mLastNotifiedCacheFill = mCacheFill;
545    notify(PLAYEREVENT_PREFETCHFILLLEVELUPDATE, (int32_t)mLastNotifiedCacheFill, true/*async*/);
546}
547
548
549void GenericPlayer::seekComplete() {
550    SL_LOGV("GenericPlayer::seekComplete");
551    sp<AMessage> msg = new AMessage(kWhatSeekComplete, id());
552    msg->post();
553}
554
555
556void GenericPlayer::bufferingUpdate(int16_t fillLevelPerMille) {
557    SL_LOGV("GenericPlayer::bufferingUpdate");
558    sp<AMessage> msg = new AMessage(kWhatBufferingUpdate, id());
559    msg->setInt32(WHATPARAM_BUFFERING_UPDATE, fillLevelPerMille);
560    msg->post();
561}
562
563
564// For the meaning of positionMs, see comment in declaration at android_GenericPlayer.h
565void GenericPlayer::updateOneShot(int positionMs)
566{
567    SL_LOGV("GenericPlayer::updateOneShot");
568
569    // nop until prepared
570    if (!(mStateFlags & kFlagPrepared)) {
571        return;
572    }
573
574    // cancel any pending one-shot(s)
575    ++mOneShotGeneration;
576
577    // don't restart one-shot if player is paused or stopped
578    if (!(mStateFlags & kFlagPlaying)) {
579        return;
580    }
581
582    // get current player position in milliseconds
583    if (positionMs < 0) {
584        positionMs = ANDROID_UNKNOWN_TIME;
585    }
586    if (positionMs == ANDROID_UNKNOWN_TIME) {
587        getPositionMsec(&positionMs);
588        // normalize it
589        if (positionMs < 0) {
590            positionMs = ANDROID_UNKNOWN_TIME;
591        }
592        if (ANDROID_UNKNOWN_TIME == positionMs) {
593            // getPositionMsec is not working for some reason, give up
594            //LOGV("Does anyone really know what time it is?");
595            return;
596        }
597    }
598
599    // delayUs is the expected delay between current position and marker;
600    // the default is infinity in case there are no upcoming marker(s)
601    int64_t delayUs = -1;
602
603    // is there a marker?
604    if ((mEventFlags & SL_PLAYEVENT_HEADATMARKER) && (mMarkerPositionMs != ANDROID_UNKNOWN_TIME)) {
605        // check to see if we have observed the position passing through the marker
606        if (mObservedPositionMs <= mMarkerPositionMs && mMarkerPositionMs <= positionMs) {
607            notify(PLAYEREVENT_PLAY, (int32_t) SL_PLAYEVENT_HEADATMARKER, true /*async*/);
608        } else if (positionMs < mMarkerPositionMs) {
609            delayUs = (mMarkerPositionMs - positionMs) * 1000LL;
610        }
611    }
612
613    // are periodic position updates needed?
614    if ((mEventFlags & SL_PLAYEVENT_HEADATNEWPOS) &&
615            (mPositionUpdatePeriodMs != ANDROID_UNKNOWN_TIME)) {
616        // check to see if we have observed the position passing through a virtual marker, where the
617        // virtual marker is at the previously delivered new position plus position update period
618        int32_t virtualMarkerMs;
619        if (mDeliveredNewPosMs != ANDROID_UNKNOWN_TIME) {
620            virtualMarkerMs = mDeliveredNewPosMs + mPositionUpdatePeriodMs;
621        } else if (mObservedPositionMs != ANDROID_UNKNOWN_TIME) {
622            virtualMarkerMs = mObservedPositionMs + mPositionUpdatePeriodMs;
623            // pretend there has been an update in the past
624            mDeliveredNewPosMs = mObservedPositionMs;
625        } else {
626            virtualMarkerMs = positionMs + mPositionUpdatePeriodMs;
627            // pretend there has been an update in the past
628            mDeliveredNewPosMs = positionMs;
629        }
630        // nextVirtualMarkerMs will be set to the position of the next upcoming virtual marker
631        int32_t nextVirtualMarkerMs;
632        if (mObservedPositionMs <= virtualMarkerMs && virtualMarkerMs <= positionMs) {
633            // we did pass through the virtual marker, now compute the next virtual marker
634            mDeliveredNewPosMs = virtualMarkerMs;
635            nextVirtualMarkerMs = virtualMarkerMs + mPositionUpdatePeriodMs;
636            // re-synchronize if we missed an update
637            if (nextVirtualMarkerMs <= positionMs) {
638                SL_LOGW("Missed SL_PLAYEVENT_HEADATNEWPOS for position %d; current position %d",
639                        nextVirtualMarkerMs, positionMs);
640                // try to catch up by setting next goal to current position plus update period
641                mDeliveredNewPosMs = positionMs;
642                nextVirtualMarkerMs = positionMs + mPositionUpdatePeriodMs;
643            }
644            notify(PLAYEREVENT_PLAY, (int32_t) SL_PLAYEVENT_HEADATNEWPOS, true /*async*/);
645        } else {
646            // we did not pass through the virtual marker yet, so use same marker again
647            nextVirtualMarkerMs = virtualMarkerMs;
648        }
649        // note that if arithmetic overflow occurred, nextVirtualMarkerMs will be negative
650        if (positionMs < nextVirtualMarkerMs) {
651            int64_t trialDelayUs;
652            trialDelayUs = (nextVirtualMarkerMs - positionMs) * 1000LL;
653            if (trialDelayUs > 0 && (delayUs == -1 || trialDelayUs < delayUs)) {
654                delayUs = trialDelayUs;
655            }
656        }
657    }
658
659    // we have a new observed position
660    mObservedPositionMs = positionMs;
661
662    // post the new one-shot message if needed
663    if (advancesPositionInRealTime() && delayUs >= 0) {
664        // 20 ms min delay to avoid near busy waiting
665        if (delayUs < 20000LL) {
666            delayUs = 20000LL;
667        }
668        // 1 minute max delay avoids indefinite memory leaks caused by cancelled one-shots
669        if (delayUs > 60000000LL) {
670            delayUs = 60000000LL;
671        }
672        //SL_LOGI("delayUs = %lld", delayUs);
673        sp<AMessage> msg = new AMessage(kWhatOneShot, id());
674        msg->setInt32(WHATPARAM_ONESHOT_GENERATION, mOneShotGeneration);
675        msg->post(delayUs);
676    }
677
678}
679
680} // namespace android
681