package androidx.media3.exoplayer.source; import androidx.media3.common.Timeline; import androidx.media3.common.util.Assertions; import androidx.media3.common.util.Util; import androidx.media3.exoplayer.source.MediaSource; import androidx.media3.exoplayer.upstream.Allocator; import java.io.IOException; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.util.ArrayList; /* loaded from: classes2.dex */ public final class ClippingMediaSource extends WrappingMediaSource { private final boolean allowDynamicClippingUpdates; private IllegalClippingException clippingError; private ClippingTimeline clippingTimeline; private final boolean enableInitialDiscontinuity; private final long endUs; private final ArrayList mediaPeriods; private long periodEndUs; private long periodStartUs; private final boolean relativeToDefaultPosition; private final long startUs; private final Timeline.Window window; /* loaded from: classes2.dex */ public static final class IllegalClippingException extends IOException { public static final int REASON_INVALID_PERIOD_COUNT = 0; public static final int REASON_NOT_SEEKABLE_TO_START = 1; public static final int REASON_START_EXCEEDS_END = 2; public final int reason; @Target({ElementType.TYPE_USE}) @Documented @Retention(RetentionPolicy.SOURCE) /* loaded from: classes2.dex */ public @interface Reason { } private static String getReasonDescription(int i) { return i != 0 ? i != 1 ? i != 2 ? "unknown" : "start exceeds end" : "not seekable to start" : "invalid period count"; } public IllegalClippingException(int i) { super("Illegal clipping: " + getReasonDescription(i)); this.reason = i; } } public ClippingMediaSource(MediaSource mediaSource, long j, long j2) { this(mediaSource, j, j2, true, false, false); } public ClippingMediaSource(MediaSource mediaSource, long j) { this(mediaSource, 0L, j, true, false, true); } public ClippingMediaSource(MediaSource mediaSource, long j, long j2, boolean z, boolean z2, boolean z3) { super((MediaSource) Assertions.checkNotNull(mediaSource)); Assertions.checkArgument(j >= 0); this.startUs = j; this.endUs = j2; this.enableInitialDiscontinuity = z; this.allowDynamicClippingUpdates = z2; this.relativeToDefaultPosition = z3; this.mediaPeriods = new ArrayList<>(); this.window = new Timeline.Window(); } @Override // androidx.media3.exoplayer.source.CompositeMediaSource, androidx.media3.exoplayer.source.MediaSource public void maybeThrowSourceInfoRefreshError() throws IOException { IllegalClippingException illegalClippingException = this.clippingError; if (illegalClippingException != null) { throw illegalClippingException; } super.maybeThrowSourceInfoRefreshError(); } @Override // androidx.media3.exoplayer.source.WrappingMediaSource, androidx.media3.exoplayer.source.MediaSource public MediaPeriod createPeriod(MediaSource.MediaPeriodId mediaPeriodId, Allocator allocator, long j) { ClippingMediaPeriod clippingMediaPeriod = new ClippingMediaPeriod(this.mediaSource.createPeriod(mediaPeriodId, allocator, j), this.enableInitialDiscontinuity, this.periodStartUs, this.periodEndUs); this.mediaPeriods.add(clippingMediaPeriod); return clippingMediaPeriod; } @Override // androidx.media3.exoplayer.source.WrappingMediaSource, androidx.media3.exoplayer.source.MediaSource public void releasePeriod(MediaPeriod mediaPeriod) { Assertions.checkState(this.mediaPeriods.remove(mediaPeriod)); this.mediaSource.releasePeriod(((ClippingMediaPeriod) mediaPeriod).mediaPeriod); if (!this.mediaPeriods.isEmpty() || this.allowDynamicClippingUpdates) { return; } refreshClippedTimeline(((ClippingTimeline) Assertions.checkNotNull(this.clippingTimeline)).timeline); } /* JADX INFO: Access modifiers changed from: protected */ @Override // androidx.media3.exoplayer.source.CompositeMediaSource, androidx.media3.exoplayer.source.BaseMediaSource public void releaseSourceInternal() { super.releaseSourceInternal(); this.clippingError = null; this.clippingTimeline = null; } @Override // androidx.media3.exoplayer.source.WrappingMediaSource protected void onChildSourceInfoRefreshed(Timeline timeline) { if (this.clippingError != null) { return; } refreshClippedTimeline(timeline); } private void refreshClippedTimeline(Timeline timeline) { long j; long j2; timeline.getWindow(0, this.window); long positionInFirstPeriodUs = this.window.getPositionInFirstPeriodUs(); if (this.clippingTimeline == null || this.mediaPeriods.isEmpty() || this.allowDynamicClippingUpdates) { long j3 = this.startUs; long j4 = this.endUs; if (this.relativeToDefaultPosition) { long defaultPositionUs = this.window.getDefaultPositionUs(); j3 += defaultPositionUs; j4 += defaultPositionUs; } this.periodStartUs = positionInFirstPeriodUs + j3; this.periodEndUs = this.endUs != Long.MIN_VALUE ? positionInFirstPeriodUs + j4 : Long.MIN_VALUE; int size = this.mediaPeriods.size(); for (int i = 0; i < size; i++) { this.mediaPeriods.get(i).updateClipping(this.periodStartUs, this.periodEndUs); } j = j3; j2 = j4; } else { long j5 = this.periodStartUs - positionInFirstPeriodUs; j2 = this.endUs != Long.MIN_VALUE ? this.periodEndUs - positionInFirstPeriodUs : Long.MIN_VALUE; j = j5; } try { ClippingTimeline clippingTimeline = new ClippingTimeline(timeline, j, j2); this.clippingTimeline = clippingTimeline; refreshSourceInfo(clippingTimeline); } catch (IllegalClippingException e) { this.clippingError = e; for (int i2 = 0; i2 < this.mediaPeriods.size(); i2++) { this.mediaPeriods.get(i2).setClippingError(this.clippingError); } } } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes2.dex */ public static final class ClippingTimeline extends ForwardingTimeline { private final long durationUs; private final long endUs; private final boolean isDynamic; private final long startUs; public ClippingTimeline(Timeline timeline, long j, long j2) throws IllegalClippingException { super(timeline); boolean z = false; if (timeline.getPeriodCount() != 1) { throw new IllegalClippingException(0); } Timeline.Window window = timeline.getWindow(0, new Timeline.Window()); long max = Math.max(0L, j); if (!window.isPlaceholder && max != 0 && !window.isSeekable) { throw new IllegalClippingException(1); } long max2 = j2 == Long.MIN_VALUE ? window.durationUs : Math.max(0L, j2); if (window.durationUs != -9223372036854775807L) { max2 = max2 > window.durationUs ? window.durationUs : max2; if (max > max2) { throw new IllegalClippingException(2); } } this.startUs = max; this.endUs = max2; this.durationUs = max2 == -9223372036854775807L ? -9223372036854775807L : max2 - max; if (window.isDynamic && (max2 == -9223372036854775807L || (window.durationUs != -9223372036854775807L && max2 == window.durationUs))) { z = true; } this.isDynamic = z; } @Override // androidx.media3.exoplayer.source.ForwardingTimeline, androidx.media3.common.Timeline public Timeline.Window getWindow(int i, Timeline.Window window, long j) { this.timeline.getWindow(0, window, 0L); window.positionInFirstPeriodUs += this.startUs; window.durationUs = this.durationUs; window.isDynamic = this.isDynamic; if (window.defaultPositionUs != -9223372036854775807L) { window.defaultPositionUs = Math.max(window.defaultPositionUs, this.startUs); long j2 = this.endUs; long j3 = window.defaultPositionUs; if (j2 != -9223372036854775807L) { j3 = Math.min(j3, this.endUs); } window.defaultPositionUs = j3; window.defaultPositionUs -= this.startUs; } long usToMs = Util.usToMs(this.startUs); if (window.presentationStartTimeMs != -9223372036854775807L) { window.presentationStartTimeMs += usToMs; } if (window.windowStartTimeMs != -9223372036854775807L) { window.windowStartTimeMs += usToMs; } return window; } @Override // androidx.media3.exoplayer.source.ForwardingTimeline, androidx.media3.common.Timeline public Timeline.Period getPeriod(int i, Timeline.Period period, boolean z) { this.timeline.getPeriod(0, period, z); long positionInWindowUs = period.getPositionInWindowUs() - this.startUs; long j = this.durationUs; return period.set(period.id, period.uid, 0, j == -9223372036854775807L ? -9223372036854775807L : j - positionInWindowUs, positionInWindowUs); } } }