From 91a87b3fa0c314ec0e9929abf7673ab891c70bd4 Mon Sep 17 00:00:00 2001 From: tonihei Date: Fri, 28 Feb 2020 12:03:25 +0000 Subject: [PATCH] Prevent deletion of unprepared periods. When a new Timeline arrives in the Player, we check whether we can keep existing MediaPeriods. This check currently involves a condition that checks if the MediaPeriod is already prepared. The only reason we do that is to avoid calling MediaPeriod.seekToUs, which is not allowed on an unprepared MediaPeriod. It's better to keep the MediaPeriod to prevent restarting the preparation process. The prepration check can move further down to the place right before we would call seekToUs. PiperOrigin-RevId: 297812584 --- .../android/exoplayer2/ExoPlayerImplInternal.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImplInternal.java b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImplInternal.java index 196dd35e96..aa6df39f3d 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImplInternal.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImplInternal.java @@ -971,11 +971,11 @@ import java.util.concurrent.atomic.AtomicBoolean; setState(Player.STATE_BUFFERING); } - // Find the requested period if it's already prepared. + // Find the requested period if it already exists. @Nullable MediaPeriodHolder oldPlayingPeriodHolder = queue.getPlayingPeriod(); @Nullable MediaPeriodHolder newPlayingPeriodHolder = oldPlayingPeriodHolder; while (newPlayingPeriodHolder != null) { - if (periodId.equals(newPlayingPeriodHolder.info.id) && newPlayingPeriodHolder.prepared) { + if (periodId.equals(newPlayingPeriodHolder.info.id)) { break; } newPlayingPeriodHolder = newPlayingPeriodHolder.getNext(); @@ -1004,7 +1004,10 @@ import java.util.concurrent.atomic.AtomicBoolean; // Do the actual seeking. if (newPlayingPeriodHolder != null) { queue.removeAfter(newPlayingPeriodHolder); - if (newPlayingPeriodHolder.hasEnabledTracks) { + if (!newPlayingPeriodHolder.prepared) { + newPlayingPeriodHolder.info = + newPlayingPeriodHolder.info.copyWithStartPositionUs(periodPositionUs); + } else if (newPlayingPeriodHolder.hasEnabledTracks) { periodPositionUs = newPlayingPeriodHolder.mediaPeriod.seekToUs(periodPositionUs); newPlayingPeriodHolder.mediaPeriod.discardBuffer( periodPositionUs - backBufferDurationUs, retainBackBufferFromKeyframe); @@ -1896,7 +1899,9 @@ import java.util.concurrent.atomic.AtomicBoolean; private PlaybackInfo copyWithNewPosition( MediaPeriodId mediaPeriodId, long positionUs, long contentPositionUs) { deliverPendingMessageAtStartPositionRequired = - positionUs != playbackInfo.positionUs || !mediaPeriodId.equals(playbackInfo.periodId); + deliverPendingMessageAtStartPositionRequired + || positionUs != playbackInfo.positionUs + || !mediaPeriodId.equals(playbackInfo.periodId); TrackGroupArray trackGroupArray = playbackInfo.trackGroups; TrackSelectorResult trackSelectorResult = playbackInfo.trackSelectorResult; if (playlist.isPrepared()) {