From 939643af13fd18806eca350638d29246ed1ffece Mon Sep 17 00:00:00 2001 From: tonihei Date: Thu, 27 Jan 2022 08:46:22 +0000 Subject: [PATCH] Move playback speed update at period transitions to reading period. In some cases we need to update the PlaybackParameters at period boundaries, for example when switching from live to VOD and live playback speed adjustment was still active at the point of switching. Currently, we send the update when the playing MediaPeriod changes in EPII, which is slightly too late because the new speed gets only applied after the entire existing AudioTrack buffer has been played out. We can time the update slightly better by updating the values at the point where we change the reading period. This makes the update slightly too early because it also applies to all samples currently in the decoder. But generally, this is a lot better because the time spent in the decoder is likely to be considerably lower than the duration of the AudioTrack buffer. Note that this change isn't perfectly aligning to the period boundary, but reduces the number of samples with the wrong speed to a minimum. If we are getting around to add additional code that allows updating the speed at exactly the boundary, it also needs to be triggered from the reading period update, so the new code location is also helpful in the future. Issue: google/ExoPlayer#9018 PiperOrigin-RevId: 424540551 --- .../exoplayer/ExoPlayerImplInternal.java | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/ExoPlayerImplInternal.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/ExoPlayerImplInternal.java index baec1a3f7b..44ab5e408e 100644 --- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/ExoPlayerImplInternal.java +++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/ExoPlayerImplInternal.java @@ -1234,7 +1234,7 @@ import java.util.concurrent.atomic.AtomicBoolean; /* forceBufferingState= */ playbackInfo.playbackState == Player.STATE_ENDED); seekPositionAdjusted |= periodPositionUs != newPeriodPositionUs; periodPositionUs = newPeriodPositionUs; - updateLivePlaybackSpeedControl( + updatePlaybackSpeedSettingsForNewPeriod( /* newTimeline= */ playbackInfo.timeline, /* newPeriodId= */ periodId, /* oldTimeline= */ playbackInfo.timeline, @@ -1874,7 +1874,7 @@ import java.util.concurrent.atomic.AtomicBoolean; newPositionUs = seekToPeriodPosition(newPeriodId, newPositionUs, forceBufferingState); } } finally { - updateLivePlaybackSpeedControl( + updatePlaybackSpeedSettingsForNewPeriod( /* newTimeline= */ timeline, newPeriodId, /* oldTimeline= */ playbackInfo.timeline, @@ -1914,15 +1914,15 @@ import java.util.concurrent.atomic.AtomicBoolean; } } - private void updateLivePlaybackSpeedControl( + private void updatePlaybackSpeedSettingsForNewPeriod( Timeline newTimeline, MediaPeriodId newPeriodId, Timeline oldTimeline, MediaPeriodId oldPeriodId, long positionForTargetOffsetOverrideUs) { - if (newTimeline.isEmpty() || !shouldUseLivePlaybackSpeedControl(newTimeline, newPeriodId)) { + if (!shouldUseLivePlaybackSpeedControl(newTimeline, newPeriodId)) { // Live playback speed control is unused for the current period, reset speed if adjusted. - if (mediaClock.getPlaybackParameters().speed != playbackInfo.playbackParameters.speed) { + if (!mediaClock.getPlaybackParameters().equals(playbackInfo.playbackParameters)) { mediaClock.setPlaybackParameters(playbackInfo.playbackParameters); } return; @@ -2054,10 +2054,18 @@ import java.util.concurrent.atomic.AtomicBoolean; return; } + MediaPeriodHolder oldReadingPeriodHolder = readingPeriodHolder; TrackSelectorResult oldTrackSelectorResult = readingPeriodHolder.getTrackSelectorResult(); readingPeriodHolder = queue.advanceReadingPeriod(); TrackSelectorResult newTrackSelectorResult = readingPeriodHolder.getTrackSelectorResult(); + updatePlaybackSpeedSettingsForNewPeriod( + /* newTimeline= */ playbackInfo.timeline, + /* newPeriodId= */ readingPeriodHolder.info.id, + /* oldTimeline= */ playbackInfo.timeline, + /* oldPeriodId= */ oldReadingPeriodHolder.info.id, + /* positionForTargetOffsetOverrideUs= */ C.TIME_UNSET); + if (readingPeriodHolder.prepared && readingPeriodHolder.mediaPeriod.readDiscontinuity() != C.TIME_UNSET) { // The new period starts with a discontinuity, so the renderers will play out all data, then @@ -2142,7 +2150,6 @@ import java.util.concurrent.atomic.AtomicBoolean; // If we advance more than one period at a time, notify listeners after each update. maybeNotifyPlaybackInfoChanged(); } - MediaPeriodHolder oldPlayingPeriodHolder = queue.getPlayingPeriod(); MediaPeriodHolder newPlayingPeriodHolder = queue.advancePlayingPeriod(); playbackInfo = handlePositionDiscontinuity( @@ -2152,12 +2159,6 @@ import java.util.concurrent.atomic.AtomicBoolean; /* discontinuityStartPositionUs= */ newPlayingPeriodHolder.info.startPositionUs, /* reportDiscontinuity= */ true, Player.DISCONTINUITY_REASON_AUTO_TRANSITION); - updateLivePlaybackSpeedControl( - /* newTimeline= */ playbackInfo.timeline, - /* newPeriodId= */ newPlayingPeriodHolder.info.id, - /* oldTimeline= */ playbackInfo.timeline, - /* oldPeriodId= */ oldPlayingPeriodHolder.info.id, - /* positionForTargetOffsetOverrideUs= */ C.TIME_UNSET); resetPendingPauseAtEndOfPeriod(); updatePlaybackPositions(); advancedPlayingPeriod = true;