From 9f1e32f11292bdab5414c0874917a7466e2d2fdf Mon Sep 17 00:00:00 2001 From: tonihei Date: Mon, 3 Dec 2018 14:21:09 +0000 Subject: [PATCH] Add experimental flag to AdaptiveTrackSelection.Factory to block fixed track bandwidth This option to block bandwidth already exists on the AdaptiveTrackSelection itself but it's not currently possible to forward the total fixed track bandwidth automatically. PiperOrigin-RevId: 223785139 --- .../AdaptiveTrackSelection.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) 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 f7188fbf72..66b49555ef 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.upstream.BandwidthMeter; import com.google.android.exoplayer2.util.Clock; import com.google.android.exoplayer2.util.Util; import java.util.List; +import org.checkerframework.checker.nullness.compatqual.NullableType; /** * A bandwidth based adaptive {@link TrackSelection}, whose selected track is updated to be the one @@ -48,6 +49,7 @@ public class AdaptiveTrackSelection extends BaseTrackSelection { private final Clock clock; private TrackBitrateEstimator trackBitrateEstimator; + private boolean blockFixedTrackSelectionBandwidth; /** Creates an adaptive track selection factory with default parameters. */ public Factory() { @@ -215,6 +217,15 @@ public class AdaptiveTrackSelection extends BaseTrackSelection { this.trackBitrateEstimator = trackBitrateEstimator; } + /** + * Enables blocking of the total fixed track selection bandwidth. + * + *

This method is experimental, and will be renamed or removed in a future release. + */ + public void experimental_enableBlockFixedTrackSelectionBandwidth() { + this.blockFixedTrackSelectionBandwidth = true; + } + @Override public AdaptiveTrackSelection createTrackSelection( TrackGroup group, BandwidthMeter bandwidthMeter, int... tracks) { @@ -235,6 +246,34 @@ public class AdaptiveTrackSelection extends BaseTrackSelection { adaptiveTrackSelection.experimental_setTrackBitrateEstimator(trackBitrateEstimator); return adaptiveTrackSelection; } + + @Override + public @NullableType TrackSelection[] createTrackSelections( + @NullableType Definition[] definitions, BandwidthMeter bandwidthMeter) { + TrackSelection[] selections = new TrackSelection[definitions.length]; + AdaptiveTrackSelection adaptiveSelection = null; + int totalFixedBandwidth = 0; + for (int i = 0; i < definitions.length; i++) { + Definition definition = definitions[i]; + if (definition == null) { + continue; + } + if (definition.tracks.length > 1) { + selections[i] = createTrackSelection(definition.group, bandwidthMeter, definition.tracks); + adaptiveSelection = (AdaptiveTrackSelection) selections[i]; + } else { + selections[i] = new FixedTrackSelection(definition.group, definition.tracks[0]); + int trackBitrate = definition.group.getFormat(definition.tracks[0]).bitrate; + if (trackBitrate != Format.NO_VALUE) { + totalFixedBandwidth += trackBitrate; + } + } + } + if (blockFixedTrackSelectionBandwidth && adaptiveSelection != null) { + adaptiveSelection.experimental_setNonAllocatableBandwidth(totalFixedBandwidth); + } + return selections; + } } public static final int DEFAULT_MIN_DURATION_FOR_QUALITY_INCREASE_MS = 10000;