Rabbit-R1/switch port/java/sources/androidx/media3/exoplayer/source/SingleSampleMediaPeriod.java

317 lines
14 KiB
Java
Raw Normal View History

2024-05-21 16:08:36 -05:00
package androidx.media3.exoplayer.source;
import androidx.media3.common.Format;
import androidx.media3.common.MimeTypes;
import androidx.media3.common.TrackGroup;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.Util;
import androidx.media3.datasource.DataSource;
import androidx.media3.datasource.DataSourceUtil;
import androidx.media3.datasource.DataSpec;
import androidx.media3.datasource.StatsDataSource;
import androidx.media3.datasource.TransferListener;
import androidx.media3.decoder.DecoderInputBuffer;
import androidx.media3.exoplayer.FormatHolder;
import androidx.media3.exoplayer.SeekParameters;
import androidx.media3.exoplayer.source.MediaPeriod;
import androidx.media3.exoplayer.source.MediaSourceEventListener;
import androidx.media3.exoplayer.trackselection.ExoTrackSelection;
import androidx.media3.exoplayer.upstream.LoadErrorHandlingPolicy;
import androidx.media3.exoplayer.upstream.Loader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public final class SingleSampleMediaPeriod implements MediaPeriod, Loader.Callback<SourceLoadable> {
private static final int INITIAL_SAMPLE_SIZE = 1024;
private static final String TAG = "SingleSampleMediaPeriod";
private final DataSource.Factory dataSourceFactory;
private final DataSpec dataSpec;
private final long durationUs;
private final MediaSourceEventListener.EventDispatcher eventDispatcher;
final Format format;
private final LoadErrorHandlingPolicy loadErrorHandlingPolicy;
boolean loadingFinished;
byte[] sampleData;
int sampleSize;
private final TrackGroupArray tracks;
private final TransferListener transferListener;
final boolean treatLoadErrorsAsEndOfStream;
private final ArrayList<SampleStreamImpl> sampleStreams = new ArrayList<>();
final Loader loader = new Loader(TAG);
@Override // androidx.media3.exoplayer.source.MediaPeriod
public void discardBuffer(long j, boolean z) {
}
@Override // androidx.media3.exoplayer.source.MediaPeriod
public long getAdjustedSeekPositionUs(long j, SeekParameters seekParameters) {
return j;
}
@Override // androidx.media3.exoplayer.source.MediaPeriod, androidx.media3.exoplayer.source.SequenceableLoader
public long getBufferedPositionUs() {
return this.loadingFinished ? Long.MIN_VALUE : 0L;
}
@Override // androidx.media3.exoplayer.source.MediaPeriod
public TrackGroupArray getTrackGroups() {
return this.tracks;
}
@Override // androidx.media3.exoplayer.source.MediaPeriod
public void maybeThrowPrepareError() {
}
@Override // androidx.media3.exoplayer.source.MediaPeriod
public long readDiscontinuity() {
return -9223372036854775807L;
}
@Override // androidx.media3.exoplayer.source.MediaPeriod, androidx.media3.exoplayer.source.SequenceableLoader
public void reevaluateBuffer(long j) {
}
public SingleSampleMediaPeriod(DataSpec dataSpec, DataSource.Factory factory, TransferListener transferListener, Format format, long j, LoadErrorHandlingPolicy loadErrorHandlingPolicy, MediaSourceEventListener.EventDispatcher eventDispatcher, boolean z) {
this.dataSpec = dataSpec;
this.dataSourceFactory = factory;
this.transferListener = transferListener;
this.format = format;
this.durationUs = j;
this.loadErrorHandlingPolicy = loadErrorHandlingPolicy;
this.eventDispatcher = eventDispatcher;
this.treatLoadErrorsAsEndOfStream = z;
this.tracks = new TrackGroupArray(new TrackGroup(format));
}
public void release() {
this.loader.release();
}
@Override // androidx.media3.exoplayer.source.MediaPeriod
public void prepare(MediaPeriod.Callback callback, long j) {
callback.onPrepared(this);
}
@Override // androidx.media3.exoplayer.source.MediaPeriod
public long selectTracks(ExoTrackSelection[] exoTrackSelectionArr, boolean[] zArr, SampleStream[] sampleStreamArr, boolean[] zArr2, long j) {
for (int i = 0; i < exoTrackSelectionArr.length; i++) {
SampleStream sampleStream = sampleStreamArr[i];
if (sampleStream != null && (exoTrackSelectionArr[i] == null || !zArr[i])) {
this.sampleStreams.remove(sampleStream);
sampleStreamArr[i] = null;
}
if (sampleStreamArr[i] == null && exoTrackSelectionArr[i] != null) {
SampleStreamImpl sampleStreamImpl = new SampleStreamImpl();
this.sampleStreams.add(sampleStreamImpl);
sampleStreamArr[i] = sampleStreamImpl;
zArr2[i] = true;
}
}
return j;
}
@Override // androidx.media3.exoplayer.source.MediaPeriod, androidx.media3.exoplayer.source.SequenceableLoader
public boolean continueLoading(long j) {
if (this.loadingFinished || this.loader.isLoading() || this.loader.hasFatalError()) {
return false;
}
DataSource createDataSource = this.dataSourceFactory.createDataSource();
TransferListener transferListener = this.transferListener;
if (transferListener != null) {
createDataSource.addTransferListener(transferListener);
}
SourceLoadable sourceLoadable = new SourceLoadable(this.dataSpec, createDataSource);
this.eventDispatcher.loadStarted(new LoadEventInfo(sourceLoadable.loadTaskId, this.dataSpec, this.loader.startLoading(sourceLoadable, this, this.loadErrorHandlingPolicy.getMinimumLoadableRetryCount(1))), 1, -1, this.format, 0, null, 0L, this.durationUs);
return true;
}
@Override // androidx.media3.exoplayer.source.MediaPeriod, androidx.media3.exoplayer.source.SequenceableLoader
public boolean isLoading() {
return this.loader.isLoading();
}
@Override // androidx.media3.exoplayer.source.MediaPeriod, androidx.media3.exoplayer.source.SequenceableLoader
public long getNextLoadPositionUs() {
return (this.loadingFinished || this.loader.isLoading()) ? Long.MIN_VALUE : 0L;
}
@Override // androidx.media3.exoplayer.source.MediaPeriod
public long seekToUs(long j) {
for (int i = 0; i < this.sampleStreams.size(); i++) {
this.sampleStreams.get(i).reset();
}
return j;
}
@Override // androidx.media3.exoplayer.upstream.Loader.Callback
public void onLoadCompleted(SourceLoadable sourceLoadable, long j, long j2) {
this.sampleSize = (int) sourceLoadable.dataSource.getBytesRead();
this.sampleData = (byte[]) Assertions.checkNotNull(sourceLoadable.sampleData);
this.loadingFinished = true;
StatsDataSource statsDataSource = sourceLoadable.dataSource;
LoadEventInfo loadEventInfo = new LoadEventInfo(sourceLoadable.loadTaskId, sourceLoadable.dataSpec, statsDataSource.getLastOpenedUri(), statsDataSource.getLastResponseHeaders(), j, j2, this.sampleSize);
this.loadErrorHandlingPolicy.onLoadTaskConcluded(sourceLoadable.loadTaskId);
this.eventDispatcher.loadCompleted(loadEventInfo, 1, -1, this.format, 0, null, 0L, this.durationUs);
}
@Override // androidx.media3.exoplayer.upstream.Loader.Callback
public void onLoadCanceled(SourceLoadable sourceLoadable, long j, long j2, boolean z) {
StatsDataSource statsDataSource = sourceLoadable.dataSource;
LoadEventInfo loadEventInfo = new LoadEventInfo(sourceLoadable.loadTaskId, sourceLoadable.dataSpec, statsDataSource.getLastOpenedUri(), statsDataSource.getLastResponseHeaders(), j, j2, statsDataSource.getBytesRead());
this.loadErrorHandlingPolicy.onLoadTaskConcluded(sourceLoadable.loadTaskId);
this.eventDispatcher.loadCanceled(loadEventInfo, 1, -1, null, 0, null, 0L, this.durationUs);
}
@Override // androidx.media3.exoplayer.upstream.Loader.Callback
public Loader.LoadErrorAction onLoadError(SourceLoadable sourceLoadable, long j, long j2, IOException iOException, int i) {
Loader.LoadErrorAction loadErrorAction;
StatsDataSource statsDataSource = sourceLoadable.dataSource;
LoadEventInfo loadEventInfo = new LoadEventInfo(sourceLoadable.loadTaskId, sourceLoadable.dataSpec, statsDataSource.getLastOpenedUri(), statsDataSource.getLastResponseHeaders(), j, j2, statsDataSource.getBytesRead());
long retryDelayMsFor = this.loadErrorHandlingPolicy.getRetryDelayMsFor(new LoadErrorHandlingPolicy.LoadErrorInfo(loadEventInfo, new MediaLoadData(1, -1, this.format, 0, null, 0L, Util.usToMs(this.durationUs)), iOException, i));
boolean z = retryDelayMsFor == -9223372036854775807L || i >= this.loadErrorHandlingPolicy.getMinimumLoadableRetryCount(1);
if (this.treatLoadErrorsAsEndOfStream && z) {
Log.w(TAG, "Loading failed, treating as end-of-stream.", iOException);
this.loadingFinished = true;
loadErrorAction = Loader.DONT_RETRY;
} else if (retryDelayMsFor != -9223372036854775807L) {
loadErrorAction = Loader.createRetryAction(false, retryDelayMsFor);
} else {
loadErrorAction = Loader.DONT_RETRY_FATAL;
}
Loader.LoadErrorAction loadErrorAction2 = loadErrorAction;
boolean z2 = !loadErrorAction2.isRetry();
this.eventDispatcher.loadError(loadEventInfo, 1, -1, this.format, 0, null, 0L, this.durationUs, iOException, z2);
if (z2) {
this.loadErrorHandlingPolicy.onLoadTaskConcluded(sourceLoadable.loadTaskId);
}
return loadErrorAction2;
}
/* loaded from: classes2.dex */
private 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 notifiedDownstreamFormat;
private int streamState;
public void reset() {
if (this.streamState == 2) {
this.streamState = 1;
}
}
private SampleStreamImpl() {
}
@Override // androidx.media3.exoplayer.source.SampleStream
public boolean isReady() {
return SingleSampleMediaPeriod.this.loadingFinished;
}
@Override // androidx.media3.exoplayer.source.SampleStream
public void maybeThrowError() throws IOException {
if (SingleSampleMediaPeriod.this.treatLoadErrorsAsEndOfStream) {
return;
}
SingleSampleMediaPeriod.this.loader.maybeThrowError();
}
@Override // androidx.media3.exoplayer.source.SampleStream
public int readData(FormatHolder formatHolder, DecoderInputBuffer decoderInputBuffer, int i) {
maybeNotifyDownstreamFormat();
if (SingleSampleMediaPeriod.this.loadingFinished && SingleSampleMediaPeriod.this.sampleData == null) {
this.streamState = 2;
}
int i2 = this.streamState;
if (i2 == 2) {
decoderInputBuffer.addFlag(4);
return -4;
}
if ((i & 2) != 0 || i2 == 0) {
formatHolder.format = SingleSampleMediaPeriod.this.format;
this.streamState = 1;
return -5;
}
if (!SingleSampleMediaPeriod.this.loadingFinished) {
return -3;
}
Assertions.checkNotNull(SingleSampleMediaPeriod.this.sampleData);
decoderInputBuffer.addFlag(1);
decoderInputBuffer.timeUs = 0L;
if ((i & 4) == 0) {
decoderInputBuffer.ensureSpaceForWrite(SingleSampleMediaPeriod.this.sampleSize);
decoderInputBuffer.data.put(SingleSampleMediaPeriod.this.sampleData, 0, SingleSampleMediaPeriod.this.sampleSize);
}
if ((i & 1) == 0) {
this.streamState = 2;
}
return -4;
}
@Override // androidx.media3.exoplayer.source.SampleStream
public int skipData(long j) {
maybeNotifyDownstreamFormat();
if (j <= 0 || this.streamState == 2) {
return 0;
}
this.streamState = 2;
return 1;
}
private void maybeNotifyDownstreamFormat() {
if (this.notifiedDownstreamFormat) {
return;
}
SingleSampleMediaPeriod.this.eventDispatcher.downstreamFormatChanged(MimeTypes.getTrackType(SingleSampleMediaPeriod.this.format.sampleMimeType), SingleSampleMediaPeriod.this.format, 0, null, 0L);
this.notifiedDownstreamFormat = true;
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static final class SourceLoadable implements Loader.Loadable {
private final StatsDataSource dataSource;
public final DataSpec dataSpec;
public final long loadTaskId = LoadEventInfo.getNewId();
private byte[] sampleData;
@Override // androidx.media3.exoplayer.upstream.Loader.Loadable
public void cancelLoad() {
}
public SourceLoadable(DataSpec dataSpec, DataSource dataSource) {
this.dataSpec = dataSpec;
this.dataSource = new StatsDataSource(dataSource);
}
@Override // androidx.media3.exoplayer.upstream.Loader.Loadable
public void load() throws IOException {
int bytesRead;
StatsDataSource statsDataSource;
byte[] bArr;
this.dataSource.resetBytesRead();
try {
this.dataSource.open(this.dataSpec);
do {
bytesRead = (int) this.dataSource.getBytesRead();
byte[] bArr2 = this.sampleData;
if (bArr2 == null) {
this.sampleData = new byte[1024];
} else if (bytesRead == bArr2.length) {
this.sampleData = Arrays.copyOf(bArr2, bArr2.length * 2);
}
statsDataSource = this.dataSource;
bArr = this.sampleData;
} while (statsDataSource.read(bArr, bytesRead, bArr.length - bytesRead) != -1);
} finally {
DataSourceUtil.closeQuietly(this.dataSource);
}
}
}
}