package com.mbridge.msdk.playercommon.exoplayer2.source; import androidx.annotation.Nullable; import com.mbridge.msdk.playercommon.exoplayer2.ExoPlayer; import com.mbridge.msdk.playercommon.exoplayer2.Timeline; import com.mbridge.msdk.playercommon.exoplayer2.source.MediaSource; import com.mbridge.msdk.playercommon.exoplayer2.source.ShuffleOrder; import com.mbridge.msdk.playercommon.exoplayer2.upstream.Allocator; import com.mbridge.msdk.playercommon.exoplayer2.util.Assertions; /* loaded from: classes4.dex */ public final class LoopingMediaSource extends CompositeMediaSource { private int childPeriodCount; private final MediaSource childSource; private final int loopCount; public LoopingMediaSource(MediaSource mediaSource) { this(mediaSource, Integer.MAX_VALUE); } public LoopingMediaSource(MediaSource mediaSource, int i) { Assertions.checkArgument(i > 0); this.childSource = mediaSource; this.loopCount = i; } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.CompositeMediaSource, com.mbridge.msdk.playercommon.exoplayer2.source.BaseMediaSource public final void prepareSourceInternal(ExoPlayer exoPlayer, boolean z) { super.prepareSourceInternal(exoPlayer, z); prepareChildSource(null, this.childSource); } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.MediaSource public final MediaPeriod createPeriod(MediaSource.MediaPeriodId mediaPeriodId, Allocator allocator) { if (this.loopCount != Integer.MAX_VALUE) { return this.childSource.createPeriod(mediaPeriodId.copyWithPeriodIndex(mediaPeriodId.periodIndex % this.childPeriodCount), allocator); } return this.childSource.createPeriod(mediaPeriodId, allocator); } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.MediaSource public final void releasePeriod(MediaPeriod mediaPeriod) { this.childSource.releasePeriod(mediaPeriod); } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.CompositeMediaSource, com.mbridge.msdk.playercommon.exoplayer2.source.BaseMediaSource public final void releaseSourceInternal() { super.releaseSourceInternal(); this.childPeriodCount = 0; } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.CompositeMediaSource public final void onChildSourceInfoRefreshed(Void r1, MediaSource mediaSource, Timeline timeline, @Nullable Object obj) { Timeline infinitelyLoopingTimeline; this.childPeriodCount = timeline.getPeriodCount(); if (this.loopCount != Integer.MAX_VALUE) { infinitelyLoopingTimeline = new LoopingTimeline(timeline, this.loopCount); } else { infinitelyLoopingTimeline = new InfinitelyLoopingTimeline(timeline); } refreshSourceInfo(infinitelyLoopingTimeline, obj); } public static final class LoopingTimeline extends AbstractConcatenatedTimeline { private final int childPeriodCount; private final Timeline childTimeline; private final int childWindowCount; private final int loopCount; @Override // com.mbridge.msdk.playercommon.exoplayer2.source.AbstractConcatenatedTimeline public final int getFirstPeriodIndexByChildIndex(int i) { return i * this.childPeriodCount; } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.AbstractConcatenatedTimeline public final int getFirstWindowIndexByChildIndex(int i) { return i * this.childWindowCount; } @Override // com.mbridge.msdk.playercommon.exoplayer2.Timeline public final int getPeriodCount() { return this.childPeriodCount * this.loopCount; } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.AbstractConcatenatedTimeline public final Timeline getTimelineByChildIndex(int i) { return this.childTimeline; } @Override // com.mbridge.msdk.playercommon.exoplayer2.Timeline public final int getWindowCount() { return this.childWindowCount * this.loopCount; } public LoopingTimeline(Timeline timeline, int i) { super(false, new ShuffleOrder.UnshuffledShuffleOrder(i)); this.childTimeline = timeline; int periodCount = timeline.getPeriodCount(); this.childPeriodCount = periodCount; this.childWindowCount = timeline.getWindowCount(); this.loopCount = i; if (periodCount > 0) { Assertions.checkState(i <= Integer.MAX_VALUE / periodCount, "LoopingMediaSource contains too many periods"); } } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.AbstractConcatenatedTimeline public final int getChildIndexByPeriodIndex(int i) { return i / this.childPeriodCount; } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.AbstractConcatenatedTimeline public final int getChildIndexByWindowIndex(int i) { return i / this.childWindowCount; } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.AbstractConcatenatedTimeline public final int getChildIndexByChildUid(Object obj) { if (obj instanceof Integer) { return ((Integer) obj).intValue(); } return -1; } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.AbstractConcatenatedTimeline public final Object getChildUidByChildIndex(int i) { return Integer.valueOf(i); } } public static final class InfinitelyLoopingTimeline extends ForwardingTimeline { public InfinitelyLoopingTimeline(Timeline timeline) { super(timeline); } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.ForwardingTimeline, com.mbridge.msdk.playercommon.exoplayer2.Timeline public final int getNextWindowIndex(int i, int i2, boolean z) { int nextWindowIndex = this.timeline.getNextWindowIndex(i, i2, z); return nextWindowIndex == -1 ? getFirstWindowIndex(z) : nextWindowIndex; } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.ForwardingTimeline, com.mbridge.msdk.playercommon.exoplayer2.Timeline public final int getPreviousWindowIndex(int i, int i2, boolean z) { int previousWindowIndex = this.timeline.getPreviousWindowIndex(i, i2, z); return previousWindowIndex == -1 ? getLastWindowIndex(z) : previousWindowIndex; } } }