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
This commit is contained in:
tonihei 2022-01-27 08:46:22 +00:00 committed by Andrew Lewis
parent 0b6837208b
commit 939643af13

View File

@ -1234,7 +1234,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
/* forceBufferingState= */ playbackInfo.playbackState == Player.STATE_ENDED); /* forceBufferingState= */ playbackInfo.playbackState == Player.STATE_ENDED);
seekPositionAdjusted |= periodPositionUs != newPeriodPositionUs; seekPositionAdjusted |= periodPositionUs != newPeriodPositionUs;
periodPositionUs = newPeriodPositionUs; periodPositionUs = newPeriodPositionUs;
updateLivePlaybackSpeedControl( updatePlaybackSpeedSettingsForNewPeriod(
/* newTimeline= */ playbackInfo.timeline, /* newTimeline= */ playbackInfo.timeline,
/* newPeriodId= */ periodId, /* newPeriodId= */ periodId,
/* oldTimeline= */ playbackInfo.timeline, /* oldTimeline= */ playbackInfo.timeline,
@ -1874,7 +1874,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
newPositionUs = seekToPeriodPosition(newPeriodId, newPositionUs, forceBufferingState); newPositionUs = seekToPeriodPosition(newPeriodId, newPositionUs, forceBufferingState);
} }
} finally { } finally {
updateLivePlaybackSpeedControl( updatePlaybackSpeedSettingsForNewPeriod(
/* newTimeline= */ timeline, /* newTimeline= */ timeline,
newPeriodId, newPeriodId,
/* oldTimeline= */ playbackInfo.timeline, /* oldTimeline= */ playbackInfo.timeline,
@ -1914,15 +1914,15 @@ import java.util.concurrent.atomic.AtomicBoolean;
} }
} }
private void updateLivePlaybackSpeedControl( private void updatePlaybackSpeedSettingsForNewPeriod(
Timeline newTimeline, Timeline newTimeline,
MediaPeriodId newPeriodId, MediaPeriodId newPeriodId,
Timeline oldTimeline, Timeline oldTimeline,
MediaPeriodId oldPeriodId, MediaPeriodId oldPeriodId,
long positionForTargetOffsetOverrideUs) { 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. // 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); mediaClock.setPlaybackParameters(playbackInfo.playbackParameters);
} }
return; return;
@ -2054,10 +2054,18 @@ import java.util.concurrent.atomic.AtomicBoolean;
return; return;
} }
MediaPeriodHolder oldReadingPeriodHolder = readingPeriodHolder;
TrackSelectorResult oldTrackSelectorResult = readingPeriodHolder.getTrackSelectorResult(); TrackSelectorResult oldTrackSelectorResult = readingPeriodHolder.getTrackSelectorResult();
readingPeriodHolder = queue.advanceReadingPeriod(); readingPeriodHolder = queue.advanceReadingPeriod();
TrackSelectorResult newTrackSelectorResult = readingPeriodHolder.getTrackSelectorResult(); 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 if (readingPeriodHolder.prepared
&& readingPeriodHolder.mediaPeriod.readDiscontinuity() != C.TIME_UNSET) { && readingPeriodHolder.mediaPeriod.readDiscontinuity() != C.TIME_UNSET) {
// The new period starts with a discontinuity, so the renderers will play out all data, then // 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. // If we advance more than one period at a time, notify listeners after each update.
maybeNotifyPlaybackInfoChanged(); maybeNotifyPlaybackInfoChanged();
} }
MediaPeriodHolder oldPlayingPeriodHolder = queue.getPlayingPeriod();
MediaPeriodHolder newPlayingPeriodHolder = queue.advancePlayingPeriod(); MediaPeriodHolder newPlayingPeriodHolder = queue.advancePlayingPeriod();
playbackInfo = playbackInfo =
handlePositionDiscontinuity( handlePositionDiscontinuity(
@ -2152,12 +2159,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
/* discontinuityStartPositionUs= */ newPlayingPeriodHolder.info.startPositionUs, /* discontinuityStartPositionUs= */ newPlayingPeriodHolder.info.startPositionUs,
/* reportDiscontinuity= */ true, /* reportDiscontinuity= */ true,
Player.DISCONTINUITY_REASON_AUTO_TRANSITION); 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(); resetPendingPauseAtEndOfPeriod();
updatePlaybackPositions(); updatePlaybackPositions();
advancedPlayingPeriod = true; advancedPlayingPeriod = true;