package androidx.media3.exoplayer.trackselection; import androidx.media3.common.Format; import androidx.media3.common.Timeline; import androidx.media3.common.TrackGroup; import androidx.media3.common.util.Clock; import androidx.media3.common.util.Log; import androidx.media3.common.util.Util; import androidx.media3.exoplayer.source.MediaSource; import androidx.media3.exoplayer.source.chunk.MediaChunk; import androidx.media3.exoplayer.source.chunk.MediaChunkIterator; import androidx.media3.exoplayer.trackselection.ExoTrackSelection; import androidx.media3.exoplayer.upstream.BandwidthMeter; import androidx.work.WorkRequest; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.common.collect.Multimap; import com.google.common.collect.MultimapBuilder; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; import tech.rabbit.r1launcher.BuildConfig; /* loaded from: classes2.dex */ public class AdaptiveTrackSelection extends BaseTrackSelection { public static final float DEFAULT_BANDWIDTH_FRACTION = 0.7f; public static final float DEFAULT_BUFFERED_FRACTION_TO_LIVE_EDGE_FOR_QUALITY_INCREASE = 0.75f; public static final int DEFAULT_MAX_DURATION_FOR_QUALITY_DECREASE_MS = 25000; public static final int DEFAULT_MAX_HEIGHT_TO_DISCARD = 719; public static final int DEFAULT_MAX_WIDTH_TO_DISCARD = 1279; public static final int DEFAULT_MIN_DURATION_FOR_QUALITY_INCREASE_MS = 10000; public static final int DEFAULT_MIN_DURATION_TO_RETAIN_AFTER_DISCARD_MS = 25000; private static final long MIN_TIME_BETWEEN_BUFFER_REEVALUTATION_MS = 1000; private static final String TAG = "AdaptiveTrackSelection"; private final ImmutableList adaptationCheckpoints; private final float bandwidthFraction; private final BandwidthMeter bandwidthMeter; private final float bufferedFractionToLiveEdgeForQualityIncrease; private final Clock clock; private MediaChunk lastBufferEvaluationMediaChunk; private long lastBufferEvaluationMs; private long latestBitrateEstimate; private final long maxDurationForQualityDecreaseUs; private final int maxHeightToDiscard; private final int maxWidthToDiscard; private final long minDurationForQualityIncreaseUs; private final long minDurationToRetainAfterDiscardUs; private float playbackSpeed; private int reason; private int selectedIndex; protected boolean canSelectFormat(Format format, int i, long j) { return ((long) i) <= j; } @Override // androidx.media3.exoplayer.trackselection.BaseTrackSelection, androidx.media3.exoplayer.trackselection.ExoTrackSelection public void disable() { this.lastBufferEvaluationMediaChunk = null; } @Override // androidx.media3.exoplayer.trackselection.BaseTrackSelection, androidx.media3.exoplayer.trackselection.ExoTrackSelection public void enable() { this.lastBufferEvaluationMs = -9223372036854775807L; this.lastBufferEvaluationMediaChunk = null; } @Override // androidx.media3.exoplayer.trackselection.ExoTrackSelection public long getLatestBitrateEstimate() { return this.latestBitrateEstimate; } protected long getMinDurationToRetainAfterDiscardUs() { return this.minDurationToRetainAfterDiscardUs; } @Override // androidx.media3.exoplayer.trackselection.ExoTrackSelection public int getSelectedIndex() { return this.selectedIndex; } @Override // androidx.media3.exoplayer.trackselection.ExoTrackSelection public Object getSelectionData() { return null; } @Override // androidx.media3.exoplayer.trackselection.ExoTrackSelection public int getSelectionReason() { return this.reason; } @Override // androidx.media3.exoplayer.trackselection.BaseTrackSelection, androidx.media3.exoplayer.trackselection.ExoTrackSelection public void onPlaybackSpeed(float f) { this.playbackSpeed = f; } /* loaded from: classes2.dex */ public static class Factory implements ExoTrackSelection.Factory { private final float bandwidthFraction; private final float bufferedFractionToLiveEdgeForQualityIncrease; private final Clock clock; private final int maxDurationForQualityDecreaseMs; private final int maxHeightToDiscard; private final int maxWidthToDiscard; private final int minDurationForQualityIncreaseMs; private final int minDurationToRetainAfterDiscardMs; public Factory() { this(10000, 25000, 25000, 0.7f); } public Factory(int i, int i2, int i3, float f) { this(i, i2, i3, 1279, 719, f, 0.75f, Clock.DEFAULT); } public Factory(int i, int i2, int i3, int i4, int i5, float f) { this(i, i2, i3, i4, i5, f, 0.75f, Clock.DEFAULT); } public Factory(int i, int i2, int i3, float f, float f2, Clock clock) { this(i, i2, i3, 1279, 719, f, f2, clock); } public Factory(int i, int i2, int i3, int i4, int i5, float f, float f2, Clock clock) { this.minDurationForQualityIncreaseMs = i; this.maxDurationForQualityDecreaseMs = i2; this.minDurationToRetainAfterDiscardMs = i3; this.maxWidthToDiscard = i4; this.maxHeightToDiscard = i5; this.bandwidthFraction = f; this.bufferedFractionToLiveEdgeForQualityIncrease = f2; this.clock = clock; } /* JADX WARN: Multi-variable type inference failed */ @Override // androidx.media3.exoplayer.trackselection.ExoTrackSelection.Factory public final ExoTrackSelection[] createTrackSelections(ExoTrackSelection.Definition[] definitionArr, BandwidthMeter bandwidthMeter, MediaSource.MediaPeriodId mediaPeriodId, Timeline timeline) { ExoTrackSelection createAdaptiveTrackSelection; ImmutableList adaptationCheckpoints = AdaptiveTrackSelection.getAdaptationCheckpoints(definitionArr); ExoTrackSelection[] exoTrackSelectionArr = new ExoTrackSelection[definitionArr.length]; for (int i = 0; i < definitionArr.length; i++) { ExoTrackSelection.Definition definition = definitionArr[i]; if (definition != null && definition.tracks.length != 0) { if (definition.tracks.length == 1) { createAdaptiveTrackSelection = new FixedTrackSelection(definition.group, definition.tracks[0], definition.type); } else { createAdaptiveTrackSelection = createAdaptiveTrackSelection(definition.group, definition.tracks, definition.type, bandwidthMeter, (ImmutableList) adaptationCheckpoints.get(i)); } exoTrackSelectionArr[i] = createAdaptiveTrackSelection; } } return exoTrackSelectionArr; } protected AdaptiveTrackSelection createAdaptiveTrackSelection(TrackGroup trackGroup, int[] iArr, int i, BandwidthMeter bandwidthMeter, ImmutableList immutableList) { return new AdaptiveTrackSelection(trackGroup, iArr, i, bandwidthMeter, this.minDurationForQualityIncreaseMs, this.maxDurationForQualityDecreaseMs, this.minDurationToRetainAfterDiscardMs, this.maxWidthToDiscard, this.maxHeightToDiscard, this.bandwidthFraction, this.bufferedFractionToLiveEdgeForQualityIncrease, immutableList, this.clock); } } public AdaptiveTrackSelection(TrackGroup trackGroup, int[] iArr, BandwidthMeter bandwidthMeter) { this(trackGroup, iArr, 0, bandwidthMeter, WorkRequest.MIN_BACKOFF_MILLIS, 25000L, 25000L, 1279, 719, 0.7f, 0.75f, ImmutableList.of(), Clock.DEFAULT); } protected AdaptiveTrackSelection(TrackGroup trackGroup, int[] iArr, int i, BandwidthMeter bandwidthMeter, long j, long j2, long j3, int i2, int i3, float f, float f2, List list, Clock clock) { super(trackGroup, iArr, i); BandwidthMeter bandwidthMeter2; long j4; if (j3 < j) { Log.w(TAG, "Adjusting minDurationToRetainAfterDiscardMs to be at least minDurationForQualityIncreaseMs"); bandwidthMeter2 = bandwidthMeter; j4 = j; } else { bandwidthMeter2 = bandwidthMeter; j4 = j3; } this.bandwidthMeter = bandwidthMeter2; this.minDurationForQualityIncreaseUs = j * 1000; this.maxDurationForQualityDecreaseUs = j2 * 1000; this.minDurationToRetainAfterDiscardUs = j4 * 1000; this.maxWidthToDiscard = i2; this.maxHeightToDiscard = i3; this.bandwidthFraction = f; this.bufferedFractionToLiveEdgeForQualityIncrease = f2; this.adaptationCheckpoints = ImmutableList.copyOf((Collection) list); this.clock = clock; this.playbackSpeed = 1.0f; this.reason = 0; this.lastBufferEvaluationMs = -9223372036854775807L; this.latestBitrateEstimate = Long.MIN_VALUE; } @Override // androidx.media3.exoplayer.trackselection.ExoTrackSelection public void updateSelectedTrack(long j, long j2, long j3, List list, MediaChunkIterator[] mediaChunkIteratorArr) { long elapsedRealtime = this.clock.elapsedRealtime(); long nextChunkDurationUs = getNextChunkDurationUs(mediaChunkIteratorArr, list); int i = this.reason; if (i == 0) { this.reason = 1; this.selectedIndex = determineIdealSelectedIndex(elapsedRealtime, nextChunkDurationUs); return; } int i2 = this.selectedIndex; int indexOf = list.isEmpty() ? -1 : indexOf(((MediaChunk) Iterables.getLast(list)).trackFormat); if (indexOf != -1) { i = ((MediaChunk) Iterables.getLast(list)).trackSelectionReason; i2 = indexOf; } int determineIdealSelectedIndex = determineIdealSelectedIndex(elapsedRealtime, nextChunkDurationUs); if (determineIdealSelectedIndex != i2 && !isTrackExcluded(i2, elapsedRealtime)) { Format format = getFormat(i2); Format format2 = getFormat(determineIdealSelectedIndex); long minDurationForQualityIncreaseUs = minDurationForQualityIncreaseUs(j3, nextChunkDurationUs); if ((format2.bitrate > format.bitrate && j2 < minDurationForQualityIncreaseUs) || (format2.bitrate < format.bitrate && j2 >= this.maxDurationForQualityDecreaseUs)) { determineIdealSelectedIndex = i2; } } if (determineIdealSelectedIndex != i2) { i = 3; } this.reason = i; this.selectedIndex = determineIdealSelectedIndex; } @Override // androidx.media3.exoplayer.trackselection.BaseTrackSelection, androidx.media3.exoplayer.trackselection.ExoTrackSelection public int evaluateQueueSize(long j, List list) { long elapsedRealtime = this.clock.elapsedRealtime(); if (!shouldEvaluateQueueSize(elapsedRealtime, list)) { return list.size(); } this.lastBufferEvaluationMs = elapsedRealtime; this.lastBufferEvaluationMediaChunk = list.isEmpty() ? null : (MediaChunk) Iterables.getLast(list); if (list.isEmpty()) { return 0; } int size = list.size(); long playoutDurationForMediaDuration = Util.getPlayoutDurationForMediaDuration(list.get(size - 1).startTimeUs - j, this.playbackSpeed); long minDurationToRetainAfterDiscardUs = getMinDurationToRetainAfterDiscardUs(); if (playoutDurationForMediaDuration < minDurationToRetainAfterDiscardUs) { return size; } Format format = getFormat(determineIdealSelectedIndex(elapsedRealtime, getLastChunkDurationUs(list))); for (int i = 0; i < size; i++) { MediaChunk mediaChunk = list.get(i); Format format2 = mediaChunk.trackFormat; if (Util.getPlayoutDurationForMediaDuration(mediaChunk.startTimeUs - j, this.playbackSpeed) >= minDurationToRetainAfterDiscardUs && format2.bitrate < format.bitrate && format2.height != -1 && format2.height <= this.maxHeightToDiscard && format2.width != -1 && format2.width <= this.maxWidthToDiscard && format2.height < format.height) { return i; } } return size; } protected boolean shouldEvaluateQueueSize(long j, List list) { long j2 = this.lastBufferEvaluationMs; return j2 == -9223372036854775807L || j - j2 >= 1000 || !(list.isEmpty() || ((MediaChunk) Iterables.getLast(list)).equals(this.lastBufferEvaluationMediaChunk)); } private int determineIdealSelectedIndex(long j, long j2) { long allocatedBandwidth = getAllocatedBandwidth(j2); int i = 0; for (int i2 = 0; i2 < this.length; i2++) { if (j == Long.MIN_VALUE || !isTrackExcluded(i2, j)) { Format format = getFormat(i2); if (canSelectFormat(format, format.bitrate, allocatedBandwidth)) { return i2; } i = i2; } } return i; } private long minDurationForQualityIncreaseUs(long j, long j2) { if (j == -9223372036854775807L) { return this.minDurationForQualityIncreaseUs; } if (j2 != -9223372036854775807L) { j -= j2; } return Math.min(((float) j) * this.bufferedFractionToLiveEdgeForQualityIncrease, this.minDurationForQualityIncreaseUs); } private long getNextChunkDurationUs(MediaChunkIterator[] mediaChunkIteratorArr, List list) { int i = this.selectedIndex; if (i < mediaChunkIteratorArr.length && mediaChunkIteratorArr[i].next()) { MediaChunkIterator mediaChunkIterator = mediaChunkIteratorArr[this.selectedIndex]; return mediaChunkIterator.getChunkEndTimeUs() - mediaChunkIterator.getChunkStartTimeUs(); } for (MediaChunkIterator mediaChunkIterator2 : mediaChunkIteratorArr) { if (mediaChunkIterator2.next()) { return mediaChunkIterator2.getChunkEndTimeUs() - mediaChunkIterator2.getChunkStartTimeUs(); } } return getLastChunkDurationUs(list); } private long getLastChunkDurationUs(List list) { if (list.isEmpty()) { return -9223372036854775807L; } MediaChunk mediaChunk = (MediaChunk) Iterables.getLast(list); if (mediaChunk.startTimeUs == -9223372036854775807L || mediaChunk.endTimeUs == -9223372036854775807L) { return -9223372036854775807L; } return mediaChunk.endTimeUs - mediaChunk.startTimeUs; } private long getAllocatedBandwidth(long j) { long totalAllocatableBandwidth = getTotalAllocatableBandwidth(j); if (this.adaptationCheckpoints.isEmpty()) { return totalAllocatableBandwidth; } int i = 1; while (i < this.adaptationCheckpoints.size() - 1 && this.adaptationCheckpoints.get(i).totalBandwidth < totalAllocatableBandwidth) { i++; } AdaptationCheckpoint adaptationCheckpoint = this.adaptationCheckpoints.get(i - 1); AdaptationCheckpoint adaptationCheckpoint2 = this.adaptationCheckpoints.get(i); return adaptationCheckpoint.allocatedBandwidth + ((((float) (totalAllocatableBandwidth - adaptationCheckpoint.totalBandwidth)) / ((float) (adaptationCheckpoint2.totalBandwidth - adaptationCheckpoint.totalBandwidth))) * ((float) (adaptationCheckpoint2.allocatedBandwidth - adaptationCheckpoint.allocatedBandwidth))); } private long getTotalAllocatableBandwidth(long j) { long bitrateEstimate = this.bandwidthMeter.getBitrateEstimate(); this.latestBitrateEstimate = bitrateEstimate; long j2 = ((float) bitrateEstimate) * this.bandwidthFraction; if (this.bandwidthMeter.getTimeToFirstByteEstimateUs() == -9223372036854775807L || j == -9223372036854775807L) { return ((float) j2) / this.playbackSpeed; } float f = (float) j; return (((float) j2) * Math.max((f / this.playbackSpeed) - ((float) r2), 0.0f)) / f; } /* JADX INFO: Access modifiers changed from: private */ public static ImmutableList> getAdaptationCheckpoints(ExoTrackSelection.Definition[] definitionArr) { ArrayList arrayList = new ArrayList(); for (ExoTrackSelection.Definition definition : definitionArr) { if (definition != null && definition.tracks.length > 1) { ImmutableList.Builder builder = ImmutableList.builder(); builder.add((ImmutableList.Builder) new AdaptationCheckpoint(0L, 0L)); arrayList.add(builder); } else { arrayList.add(null); } } long[][] sortedTrackBitrates = getSortedTrackBitrates(definitionArr); int[] iArr = new int[sortedTrackBitrates.length]; long[] jArr = new long[sortedTrackBitrates.length]; for (int i = 0; i < sortedTrackBitrates.length; i++) { long[] jArr2 = sortedTrackBitrates[i]; jArr[i] = jArr2.length == 0 ? 0L : jArr2[0]; } addCheckpoint(arrayList, jArr); ImmutableList switchOrder = getSwitchOrder(sortedTrackBitrates); for (int i2 = 0; i2 < switchOrder.size(); i2++) { int intValue = switchOrder.get(i2).intValue(); int i3 = iArr[intValue] + 1; iArr[intValue] = i3; jArr[intValue] = sortedTrackBitrates[intValue][i3]; addCheckpoint(arrayList, jArr); } for (int i4 = 0; i4 < definitionArr.length; i4++) { if (arrayList.get(i4) != null) { jArr[i4] = jArr[i4] * 2; } } addCheckpoint(arrayList, jArr); ImmutableList.Builder builder2 = ImmutableList.builder(); for (int i5 = 0; i5 < arrayList.size(); i5++) { ImmutableList.Builder builder3 = (ImmutableList.Builder) arrayList.get(i5); builder2.add((ImmutableList.Builder) (builder3 == null ? ImmutableList.of() : builder3.build())); } return builder2.build(); } private static long[][] getSortedTrackBitrates(ExoTrackSelection.Definition[] definitionArr) { long[][] jArr = new long[definitionArr.length]; for (int i = 0; i < definitionArr.length; i++) { ExoTrackSelection.Definition definition = definitionArr[i]; if (definition == null) { jArr[i] = new long[0]; } else { jArr[i] = new long[definition.tracks.length]; for (int i2 = 0; i2 < definition.tracks.length; i2++) { long j = definition.group.getFormat(definition.tracks[i2]).bitrate; long[] jArr2 = jArr[i]; if (j == -1) { j = 0; } jArr2[i2] = j; } Arrays.sort(jArr[i]); } } return jArr; } private static ImmutableList getSwitchOrder(long[][] jArr) { Multimap build = MultimapBuilder.treeKeys().arrayListValues().build(); for (int i = 0; i < jArr.length; i++) { long[] jArr2 = jArr[i]; if (jArr2.length > 1) { int length = jArr2.length; double[] dArr = new double[length]; int i2 = 0; while (true) { long[] jArr3 = jArr[i]; int length2 = jArr3.length; double d = BuildConfig.SENTRY_SAMPLE_RATE; if (i2 >= length2) { break; } long j = jArr3[i2]; if (j != -1) { d = Math.log(j); } dArr[i2] = d; i2++; } int i3 = length - 1; double d2 = dArr[i3] - dArr[0]; int i4 = 0; while (i4 < i3) { double d3 = dArr[i4]; i4++; build.put(Double.valueOf(d2 == BuildConfig.SENTRY_SAMPLE_RATE ? 1.0d : (((d3 + dArr[i4]) * 0.5d) - dArr[0]) / d2), Integer.valueOf(i)); } } } return ImmutableList.copyOf(build.values()); } private static void addCheckpoint(List> list, long[] jArr) { long j = 0; for (long j2 : jArr) { j += j2; } for (int i = 0; i < list.size(); i++) { ImmutableList.Builder builder = list.get(i); if (builder != null) { builder.add((ImmutableList.Builder) new AdaptationCheckpoint(j, jArr[i])); } } } /* loaded from: classes2.dex */ public static final class AdaptationCheckpoint { public final long allocatedBandwidth; public final long totalBandwidth; public int hashCode() { return (((int) this.totalBandwidth) * 31) + ((int) this.allocatedBandwidth); } public AdaptationCheckpoint(long j, long j2) { this.totalBandwidth = j; this.allocatedBandwidth = j2; } public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof AdaptationCheckpoint)) { return false; } AdaptationCheckpoint adaptationCheckpoint = (AdaptationCheckpoint) obj; return this.totalBandwidth == adaptationCheckpoint.totalBandwidth && this.allocatedBandwidth == adaptationCheckpoint.allocatedBandwidth; } } }