package com.mbridge.msdk.playercommon.exoplayer2.source; import com.mbridge.msdk.playercommon.exoplayer2.C; import com.mbridge.msdk.playercommon.exoplayer2.Format; import com.mbridge.msdk.playercommon.exoplayer2.FormatHolder; import com.mbridge.msdk.playercommon.exoplayer2.SeekParameters; import com.mbridge.msdk.playercommon.exoplayer2.decoder.DecoderInputBuffer; import com.mbridge.msdk.playercommon.exoplayer2.source.MediaPeriod; import com.mbridge.msdk.playercommon.exoplayer2.source.MediaSourceEventListener; import com.mbridge.msdk.playercommon.exoplayer2.trackselection.TrackSelection; import com.mbridge.msdk.playercommon.exoplayer2.upstream.DataSource; import com.mbridge.msdk.playercommon.exoplayer2.upstream.DataSpec; import com.mbridge.msdk.playercommon.exoplayer2.upstream.Loader; import com.mbridge.msdk.playercommon.exoplayer2.util.MimeTypes; import com.mbridge.msdk.playercommon.exoplayer2.util.Util; import java.io.IOException; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; /* loaded from: classes4.dex */ final class SingleSampleMediaPeriod implements MediaPeriod, Loader.Callback { private static final int INITIAL_SAMPLE_SIZE = 1024; private final DataSource.Factory dataSourceFactory; private final DataSpec dataSpec; private final long durationUs; private int errorCount; private final MediaSourceEventListener.EventDispatcher eventDispatcher; final Format format; boolean loadingFinished; boolean loadingSucceeded; private final int minLoadableRetryCount; boolean notifiedReadingStarted; byte[] sampleData; int sampleSize; private final TrackGroupArray tracks; final boolean treatLoadErrorsAsEndOfStream; private final ArrayList sampleStreams = new ArrayList<>(); final Loader loader = new Loader("Loader:SingleSampleMediaPeriod"); @Override // com.mbridge.msdk.playercommon.exoplayer2.source.MediaPeriod public final void discardBuffer(long j, boolean z) { } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.MediaPeriod public final long getAdjustedSeekPositionUs(long j, SeekParameters seekParameters) { return j; } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.MediaPeriod, com.mbridge.msdk.playercommon.exoplayer2.source.SequenceableLoader public final long getBufferedPositionUs() { return this.loadingFinished ? Long.MIN_VALUE : 0L; } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.MediaPeriod public final TrackGroupArray getTrackGroups() { return this.tracks; } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.MediaPeriod public final void maybeThrowPrepareError() throws IOException { } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.MediaPeriod, com.mbridge.msdk.playercommon.exoplayer2.source.SequenceableLoader public final void reevaluateBuffer(long j) { } public SingleSampleMediaPeriod(DataSpec dataSpec, DataSource.Factory factory, Format format, long j, int i, MediaSourceEventListener.EventDispatcher eventDispatcher, boolean z) { this.dataSpec = dataSpec; this.dataSourceFactory = factory; this.format = format; this.durationUs = j; this.minLoadableRetryCount = i; this.eventDispatcher = eventDispatcher; this.treatLoadErrorsAsEndOfStream = z; this.tracks = new TrackGroupArray(new TrackGroup(format)); eventDispatcher.mediaPeriodCreated(); } public final void release() { this.loader.release(); this.eventDispatcher.mediaPeriodReleased(); } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.MediaPeriod public final void prepare(MediaPeriod.Callback callback, long j) { callback.onPrepared(this); } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.MediaPeriod public final long selectTracks(TrackSelection[] trackSelectionArr, boolean[] zArr, SampleStream[] sampleStreamArr, boolean[] zArr2, long j) { for (int i = 0; i < trackSelectionArr.length; i++) { SampleStream sampleStream = sampleStreamArr[i]; if (sampleStream != null && (trackSelectionArr[i] == null || !zArr[i])) { this.sampleStreams.remove(sampleStream); sampleStreamArr[i] = null; } if (sampleStreamArr[i] == null && trackSelectionArr[i] != null) { SampleStreamImpl sampleStreamImpl = new SampleStreamImpl(); this.sampleStreams.add(sampleStreamImpl); sampleStreamArr[i] = sampleStreamImpl; zArr2[i] = true; } } return j; } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.MediaPeriod, com.mbridge.msdk.playercommon.exoplayer2.source.SequenceableLoader public final boolean continueLoading(long j) { if (this.loadingFinished || this.loader.isLoading()) { return false; } this.eventDispatcher.loadStarted(this.dataSpec, 1, -1, this.format, 0, null, 0L, this.durationUs, this.loader.startLoading(new SourceLoadable(this.dataSpec, this.dataSourceFactory.createDataSource()), this, this.minLoadableRetryCount)); return true; } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.MediaPeriod public final long readDiscontinuity() { if (this.notifiedReadingStarted) { return C.TIME_UNSET; } this.eventDispatcher.readingStarted(); this.notifiedReadingStarted = true; return C.TIME_UNSET; } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.MediaPeriod, com.mbridge.msdk.playercommon.exoplayer2.source.SequenceableLoader public final long getNextLoadPositionUs() { return (this.loadingFinished || this.loader.isLoading()) ? Long.MIN_VALUE : 0L; } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.MediaPeriod public final long seekToUs(long j) { for (int i = 0; i < this.sampleStreams.size(); i++) { this.sampleStreams.get(i).reset(); } return j; } @Override // com.mbridge.msdk.playercommon.exoplayer2.upstream.Loader.Callback public final void onLoadCompleted(SourceLoadable sourceLoadable, long j, long j2) { this.eventDispatcher.loadCompleted(sourceLoadable.dataSpec, 1, -1, this.format, 0, null, 0L, this.durationUs, j, j2, sourceLoadable.sampleSize); this.sampleSize = sourceLoadable.sampleSize; this.sampleData = sourceLoadable.sampleData; this.loadingFinished = true; this.loadingSucceeded = true; } @Override // com.mbridge.msdk.playercommon.exoplayer2.upstream.Loader.Callback public final void onLoadCanceled(SourceLoadable sourceLoadable, long j, long j2, boolean z) { this.eventDispatcher.loadCanceled(sourceLoadable.dataSpec, 1, -1, null, 0, null, 0L, this.durationUs, j, j2, sourceLoadable.sampleSize); } @Override // com.mbridge.msdk.playercommon.exoplayer2.upstream.Loader.Callback public final int onLoadError(SourceLoadable sourceLoadable, long j, long j2, IOException iOException) { int i = this.errorCount + 1; this.errorCount = i; boolean z = this.treatLoadErrorsAsEndOfStream && i >= this.minLoadableRetryCount; this.eventDispatcher.loadError(sourceLoadable.dataSpec, 1, -1, this.format, 0, null, 0L, this.durationUs, j, j2, sourceLoadable.sampleSize, iOException, z); if (!z) { return 0; } this.loadingFinished = true; return 2; } public final class SampleStreamImpl implements SampleStream { private static final int STREAM_STATE_END_OF_STREAM = 2; private static final int STREAM_STATE_SEND_FORMAT = 0; private static final int STREAM_STATE_SEND_SAMPLE = 1; private boolean formatSent; private int streamState; public final void reset() { if (this.streamState == 2) { this.streamState = 1; } } private SampleStreamImpl() { } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.SampleStream public final boolean isReady() { return SingleSampleMediaPeriod.this.loadingFinished; } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.SampleStream public final void maybeThrowError() throws IOException { SingleSampleMediaPeriod singleSampleMediaPeriod = SingleSampleMediaPeriod.this; if (singleSampleMediaPeriod.treatLoadErrorsAsEndOfStream) { return; } singleSampleMediaPeriod.loader.maybeThrowError(); } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.SampleStream public final int readData(FormatHolder formatHolder, DecoderInputBuffer decoderInputBuffer, boolean z) { int i = this.streamState; if (i == 2) { decoderInputBuffer.addFlag(4); return -4; } if (z || i == 0) { formatHolder.format = SingleSampleMediaPeriod.this.format; this.streamState = 1; return -5; } SingleSampleMediaPeriod singleSampleMediaPeriod = SingleSampleMediaPeriod.this; if (!singleSampleMediaPeriod.loadingFinished) { return -3; } if (singleSampleMediaPeriod.loadingSucceeded) { decoderInputBuffer.timeUs = 0L; decoderInputBuffer.addFlag(1); decoderInputBuffer.ensureSpaceForWrite(SingleSampleMediaPeriod.this.sampleSize); ByteBuffer byteBuffer = decoderInputBuffer.data; SingleSampleMediaPeriod singleSampleMediaPeriod2 = SingleSampleMediaPeriod.this; byteBuffer.put(singleSampleMediaPeriod2.sampleData, 0, singleSampleMediaPeriod2.sampleSize); sendFormat(); } else { decoderInputBuffer.addFlag(4); } this.streamState = 2; return -4; } @Override // com.mbridge.msdk.playercommon.exoplayer2.source.SampleStream public final int skipData(long j) { if (j <= 0 || this.streamState == 2) { return 0; } this.streamState = 2; sendFormat(); return 1; } private void sendFormat() { if (this.formatSent) { return; } SingleSampleMediaPeriod.this.eventDispatcher.downstreamFormatChanged(MimeTypes.getTrackType(SingleSampleMediaPeriod.this.format.sampleMimeType), SingleSampleMediaPeriod.this.format, 0, null, 0L); this.formatSent = true; } } public static final class SourceLoadable implements Loader.Loadable { private final DataSource dataSource; public final DataSpec dataSpec; private byte[] sampleData; private int sampleSize; @Override // com.mbridge.msdk.playercommon.exoplayer2.upstream.Loader.Loadable public final void cancelLoad() { } public SourceLoadable(DataSpec dataSpec, DataSource dataSource) { this.dataSpec = dataSpec; this.dataSource = dataSource; } @Override // com.mbridge.msdk.playercommon.exoplayer2.upstream.Loader.Loadable public final void load() throws IOException, InterruptedException { int i = 0; this.sampleSize = 0; try { this.dataSource.open(this.dataSpec); while (i != -1) { int i2 = this.sampleSize + i; this.sampleSize = i2; byte[] bArr = this.sampleData; if (bArr == null) { this.sampleData = new byte[1024]; } else if (i2 == bArr.length) { this.sampleData = Arrays.copyOf(bArr, bArr.length * 2); } DataSource dataSource = this.dataSource; byte[] bArr2 = this.sampleData; int i3 = this.sampleSize; i = dataSource.read(bArr2, i3, bArr2.length - i3); } Util.closeQuietly(this.dataSource); } catch (Throwable th) { Util.closeQuietly(this.dataSource); throw th; } } } }