From 3bd59f8cf05152edd404e60a2ce26cc0eabb8f3a Mon Sep 17 00:00:00 2001 From: tonihei Date: Tue, 13 Apr 2021 15:06:07 +0100 Subject: [PATCH] Ensure minDurationToRetainAfterDiscard >= minDurationForQualityIncrease If this condition isn't true, the player may enter a cycle of discarding and reloading the same format. As minDurationToRetainAfterDiscard is a parameter likely left at its default, and minDurationForQualityIncrease is likely adjusted more often, we correct the value in the problematic case and log a warning instead of asserting it outright to prevent unnecessary app breakages. Issue: #8807 PiperOrigin-RevId: 368207417 --- .../trackselection/AdaptiveTrackSelection.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/trackselection/AdaptiveTrackSelection.java b/library/core/src/main/java/com/google/android/exoplayer2/trackselection/AdaptiveTrackSelection.java index e02f8198d5..18a71cbf2f 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/trackselection/AdaptiveTrackSelection.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/trackselection/AdaptiveTrackSelection.java @@ -26,6 +26,7 @@ import com.google.android.exoplayer2.source.chunk.MediaChunk; import com.google.android.exoplayer2.source.chunk.MediaChunkIterator; import com.google.android.exoplayer2.upstream.BandwidthMeter; import com.google.android.exoplayer2.util.Clock; +import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.Util; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; @@ -42,6 +43,8 @@ import org.checkerframework.checker.nullness.compatqual.NullableType; */ public class AdaptiveTrackSelection extends BaseTrackSelection { + private static final String TAG = "AdaptiveTrackSelection"; + /** Factory for {@link AdaptiveTrackSelection} instances. */ public static class Factory implements ExoTrackSelection.Factory { @@ -73,7 +76,8 @@ public class AdaptiveTrackSelection extends BaseTrackSelection { * @param minDurationToRetainAfterDiscardMs When switching to a track of significantly higher * quality, the selection may indicate that media already buffered at the lower quality can * be discarded to speed up the switch. This is the minimum duration of media that must be - * retained at the lower quality. + * retained at the lower quality. It must be at least {@code + * minDurationForQualityIncreaseMs}. * @param bandwidthFraction The fraction of the available bandwidth that the selection should * consider available for use. Setting to a value less than 1 is recommended to account for * inaccuracies in the bandwidth estimator. @@ -102,7 +106,8 @@ public class AdaptiveTrackSelection extends BaseTrackSelection { * @param minDurationToRetainAfterDiscardMs When switching to a track of significantly higher * quality, the selection may indicate that media already buffered at the lower quality can * be discarded to speed up the switch. This is the minimum duration of media that must be - * retained at the lower quality. + * retained at the lower quality. It must be at least {@code + * minDurationForQualityIncreaseMs}. * @param bandwidthFraction The fraction of the available bandwidth that the selection should * consider available for use. Setting to a value less than 1 is recommended to account for * inaccuracies in the bandwidth estimator. @@ -249,7 +254,7 @@ public class AdaptiveTrackSelection extends BaseTrackSelection { * @param minDurationToRetainAfterDiscardMs When switching to a track of significantly higher * quality, the selection may indicate that media already buffered at the lower quality can be * discarded to speed up the switch. This is the minimum duration of media that must be - * retained at the lower quality. + * retained at the lower quality. It must be at least {@code minDurationForQualityIncreaseMs}. * @param bandwidthFraction The fraction of the available bandwidth that the selection should * consider available for use. Setting to a value less than 1 is recommended to account for * inaccuracies in the bandwidth estimator. @@ -276,6 +281,13 @@ public class AdaptiveTrackSelection extends BaseTrackSelection { List adaptationCheckpoints, Clock clock) { super(group, tracks, type); + if (minDurationToRetainAfterDiscardMs < minDurationForQualityIncreaseMs) { + Log.w( + TAG, + "Adjusting minDurationToRetainAfterDiscardMs to be at least" + + " minDurationForQualityIncreaseMs"); + minDurationToRetainAfterDiscardMs = minDurationForQualityIncreaseMs; + } this.bandwidthMeter = bandwidthMeter; this.minDurationForQualityIncreaseUs = minDurationForQualityIncreaseMs * 1000L; this.maxDurationForQualityDecreaseUs = maxDurationForQualityDecreaseMs * 1000L;