Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 91b0ca1a authored by Eric Laurent's avatar Eric Laurent
Browse files

fix playback position after switching to offload

After switching from offloaded track to PCM track
while paused (e.g. when connecting A2DP), playback
restarts from the beginning of the song when resuming.

Save current position before recreating an AudioPlayer
in AwesomePlayer::play_l() and seek to the saved position before
starting playback.
Also fix a problem where the position is not reported properly
by AudioPlayer if a seek is pending and queried just after start
and before the first buffer is read from the MediaSource.

Bug: 8174034.
Change-Id: I254e65418ff903a9bf2e2111b89a00e2e54876c5
parent 0adc67df
Loading
Loading
Loading
Loading
+21 −13
Original line number Diff line number Diff line
@@ -363,6 +363,7 @@ void AudioPlayer::reset() {
    mPositionTimeMediaUs = -1;
    mPositionTimeRealUs = -1;
    mSeeking = false;
    mSeekTimeUs = 0;
    mReachedEOS = false;
    mFinalStatus = OK;
    mStarted = false;
@@ -602,7 +603,8 @@ size_t AudioPlayer::fillBuffer(void *data, size_t size) {

            // need to adjust the mStartPosUs for offload decoding since parser
            // might not be able to get the exact seek time requested.
            if (refreshSeekTime && useOffload()) {
            if (refreshSeekTime) {
                if (useOffload()) {
                    if (postSeekComplete) {
                        ALOGV("fillBuffer is going to post SEEK_COMPLETE");
                        mObserver->postAudioSeekComplete();
@@ -612,6 +614,14 @@ size_t AudioPlayer::fillBuffer(void *data, size_t size) {
                    mStartPosUs = mPositionTimeMediaUs;
                    ALOGV("adjust seek time to: %.2f", mStartPosUs/ 1E6);
                }
                // clear seek time with mLock locked and once we have valid mPositionTimeMediaUs
                // and mPositionTimeRealUs
                // before clearing mSeekTimeUs check if a new seek request has been received while
                // we were reading from the source with mLock released.
                if (!mSeeking) {
                    mSeekTimeUs = 0;
                }
            }

            if (!useOffload()) {
                mPositionTimeRealUs =
@@ -741,14 +751,12 @@ int64_t AudioPlayer::getMediaTimeUs() {
        return mPositionTimeRealUs;
    }


    if (mPositionTimeMediaUs < 0 || mPositionTimeRealUs < 0) {
        if (mSeeking) {
        // mSeekTimeUs is either seek time while seeking or 0 if playback did not start.
        return mSeekTimeUs;
    }

        return 0;
    }

    int64_t realTimeOffset = getRealTimeUsLocked() - mPositionTimeRealUs;
    if (realTimeOffset < 0) {
        realTimeOffset = 0;
+7 −0
Original line number Diff line number Diff line
@@ -927,6 +927,9 @@ status_t AwesomePlayer::play_l() {

            if ((err != OK) && mOffloadAudio) {
                ALOGI("play_l() cannot create offload output, fallback to sw decode");
                int64_t curTimeUs;
                getPosition(&curTimeUs);

                delete mAudioPlayer;
                mAudioPlayer = NULL;
                // if the player was started it will take care of stopping the source when destroyed
@@ -942,6 +945,10 @@ status_t AwesomePlayer::play_l() {
                    if (err != OK) {
                        mAudioSource.clear();
                    } else {
                        mSeekNotificationSent = true;
                        if (mExtractorFlags & MediaExtractor::CAN_SEEK) {
                            seekTo_l(curTimeUs);
                        }
                        createAudioPlayer_l();
                        err = startAudioPlayer_l(false);
                    }