Rabbit-R1/switch port/java/sources/androidx/media3/exoplayer/source/ProgressiveMediaPeriod.java
2024-05-21 17:08:36 -04:00

902 lines
40 KiB
Java

package androidx.media3.exoplayer.source;
import android.net.Uri;
import android.os.Handler;
import androidx.media3.common.DataReader;
import androidx.media3.common.Format;
import androidx.media3.common.Metadata;
import androidx.media3.common.MimeTypes;
import androidx.media3.common.ParserException;
import androidx.media3.common.TrackGroup;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.ConditionVariable;
import androidx.media3.common.util.ParsableByteArray;
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.decoder.DecoderInputBuffer;
import androidx.media3.exoplayer.FormatHolder;
import androidx.media3.exoplayer.SeekParameters;
import androidx.media3.exoplayer.drm.DrmSessionEventListener;
import androidx.media3.exoplayer.drm.DrmSessionManager;
import androidx.media3.exoplayer.source.IcyDataSource;
import androidx.media3.exoplayer.source.MediaPeriod;
import androidx.media3.exoplayer.source.MediaSourceEventListener;
import androidx.media3.exoplayer.source.SampleQueue;
import androidx.media3.exoplayer.trackselection.ExoTrackSelection;
import androidx.media3.exoplayer.upstream.Allocator;
import androidx.media3.exoplayer.upstream.LoadErrorHandlingPolicy;
import androidx.media3.exoplayer.upstream.Loader;
import androidx.media3.extractor.ExtractorOutput;
import androidx.media3.extractor.PositionHolder;
import androidx.media3.extractor.SeekMap;
import androidx.media3.extractor.TrackOutput;
import androidx.media3.extractor.metadata.icy.IcyHeaders;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.checkerframework.checker.nullness.qual.EnsuresNonNull;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public final class ProgressiveMediaPeriod implements MediaPeriod, ExtractorOutput, Loader.Callback<ExtractingLoadable>, Loader.ReleaseCallback, SampleQueue.UpstreamFormatChangedListener {
private static final long DEFAULT_LAST_SAMPLE_DURATION_US = 10000;
private final Allocator allocator;
private MediaPeriod.Callback callback;
private final long continueLoadingCheckIntervalBytes;
private final String customCacheKey;
private final DataSource dataSource;
private final DrmSessionEventListener.EventDispatcher drmEventDispatcher;
private final DrmSessionManager drmSessionManager;
private int enabledTrackCount;
private int extractedSamplesCountAtStartOfLoad;
private boolean haveAudioVideoTracks;
private IcyHeaders icyHeaders;
private boolean isLengthKnown;
private boolean isLive;
private long lastSeekPositionUs;
private final Listener listener;
private final LoadErrorHandlingPolicy loadErrorHandlingPolicy;
private boolean loadingFinished;
private final MediaSourceEventListener.EventDispatcher mediaSourceEventDispatcher;
private boolean notifyDiscontinuity;
private boolean pendingDeferredRetry;
private boolean prepared;
private final ProgressiveMediaExtractor progressiveMediaExtractor;
private boolean released;
private boolean sampleQueuesBuilt;
private SeekMap seekMap;
private boolean seenFirstTrackSelection;
private TrackState trackState;
private final Uri uri;
private static final Map<String, String> ICY_METADATA_HEADERS = createIcyMetadataHeaders();
private static final Format ICY_FORMAT = new Format.Builder().setId("icy").setSampleMimeType("application/x-icy").build();
private final Loader loader = new Loader("ProgressiveMediaPeriod");
private final ConditionVariable loadCondition = new ConditionVariable();
private final Runnable maybeFinishPrepareRunnable = new Runnable() { // from class: androidx.media3.exoplayer.source.ProgressiveMediaPeriod$$ExternalSyntheticLambda0
@Override // java.lang.Runnable
public final void run() {
ProgressiveMediaPeriod.this.maybeFinishPrepare();
}
};
private final Runnable onContinueLoadingRequestedRunnable = new Runnable() { // from class: androidx.media3.exoplayer.source.ProgressiveMediaPeriod$$ExternalSyntheticLambda1
@Override // java.lang.Runnable
public final void run() {
ProgressiveMediaPeriod.this.m5265x97cae34d();
}
};
private final Handler handler = Util.createHandlerForCurrentLooper();
private TrackId[] sampleQueueTrackIds = new TrackId[0];
private SampleQueue[] sampleQueues = new SampleQueue[0];
private long pendingResetPositionUs = -9223372036854775807L;
private long durationUs = -9223372036854775807L;
private int dataType = 1;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public interface Listener {
void onSourceInfoRefreshed(long j, boolean z, boolean z2);
}
private boolean isPendingReset() {
return this.pendingResetPositionUs != -9223372036854775807L;
}
/* JADX INFO: Access modifiers changed from: package-private */
/* renamed from: lambda$onLengthKnown$2$androidx-media3-exoplayer-source-ProgressiveMediaPeriod, reason: not valid java name */
public /* synthetic */ void m5266xa2bd730d() {
this.isLengthKnown = true;
}
@Override // androidx.media3.exoplayer.source.MediaPeriod, androidx.media3.exoplayer.source.SequenceableLoader
public void reevaluateBuffer(long j) {
}
public ProgressiveMediaPeriod(Uri uri, DataSource dataSource, ProgressiveMediaExtractor progressiveMediaExtractor, DrmSessionManager drmSessionManager, DrmSessionEventListener.EventDispatcher eventDispatcher, LoadErrorHandlingPolicy loadErrorHandlingPolicy, MediaSourceEventListener.EventDispatcher eventDispatcher2, Listener listener, Allocator allocator, String str, int i) {
this.uri = uri;
this.dataSource = dataSource;
this.drmSessionManager = drmSessionManager;
this.drmEventDispatcher = eventDispatcher;
this.loadErrorHandlingPolicy = loadErrorHandlingPolicy;
this.mediaSourceEventDispatcher = eventDispatcher2;
this.listener = listener;
this.allocator = allocator;
this.customCacheKey = str;
this.continueLoadingCheckIntervalBytes = i;
this.progressiveMediaExtractor = progressiveMediaExtractor;
}
/* JADX INFO: Access modifiers changed from: package-private */
/* renamed from: lambda$new$0$androidx-media3-exoplayer-source-ProgressiveMediaPeriod, reason: not valid java name */
public /* synthetic */ void m5265x97cae34d() {
if (this.released) {
return;
}
((MediaPeriod.Callback) Assertions.checkNotNull(this.callback)).onContinueLoadingRequested(this);
}
public void release() {
if (this.prepared) {
for (SampleQueue sampleQueue : this.sampleQueues) {
sampleQueue.preRelease();
}
}
this.loader.release(this);
this.handler.removeCallbacksAndMessages(null);
this.callback = null;
this.released = true;
}
@Override // androidx.media3.exoplayer.upstream.Loader.ReleaseCallback
public void onLoaderReleased() {
for (SampleQueue sampleQueue : this.sampleQueues) {
sampleQueue.release();
}
this.progressiveMediaExtractor.release();
}
@Override // androidx.media3.exoplayer.source.MediaPeriod
public void prepare(MediaPeriod.Callback callback, long j) {
this.callback = callback;
this.loadCondition.open();
startLoading();
}
@Override // androidx.media3.exoplayer.source.MediaPeriod
public void maybeThrowPrepareError() throws IOException {
maybeThrowError();
if (this.loadingFinished && !this.prepared) {
throw ParserException.createForMalformedContainer("Loading finished before preparation is complete.", null);
}
}
@Override // androidx.media3.exoplayer.source.MediaPeriod
public TrackGroupArray getTrackGroups() {
assertPrepared();
return this.trackState.tracks;
}
@Override // androidx.media3.exoplayer.source.MediaPeriod
public long selectTracks(ExoTrackSelection[] exoTrackSelectionArr, boolean[] zArr, SampleStream[] sampleStreamArr, boolean[] zArr2, long j) {
ExoTrackSelection exoTrackSelection;
assertPrepared();
TrackGroupArray trackGroupArray = this.trackState.tracks;
boolean[] zArr3 = this.trackState.trackEnabledStates;
int i = this.enabledTrackCount;
int i2 = 0;
for (int i3 = 0; i3 < exoTrackSelectionArr.length; i3++) {
SampleStream sampleStream = sampleStreamArr[i3];
if (sampleStream != null && (exoTrackSelectionArr[i3] == null || !zArr[i3])) {
int i4 = ((SampleStreamImpl) sampleStream).track;
Assertions.checkState(zArr3[i4]);
this.enabledTrackCount--;
zArr3[i4] = false;
sampleStreamArr[i3] = null;
}
}
boolean z = !this.seenFirstTrackSelection ? j == 0 : i != 0;
for (int i5 = 0; i5 < exoTrackSelectionArr.length; i5++) {
if (sampleStreamArr[i5] == null && (exoTrackSelection = exoTrackSelectionArr[i5]) != null) {
Assertions.checkState(exoTrackSelection.length() == 1);
Assertions.checkState(exoTrackSelection.getIndexInTrackGroup(0) == 0);
int indexOf = trackGroupArray.indexOf(exoTrackSelection.getTrackGroup());
Assertions.checkState(!zArr3[indexOf]);
this.enabledTrackCount++;
zArr3[indexOf] = true;
sampleStreamArr[i5] = new SampleStreamImpl(indexOf);
zArr2[i5] = true;
if (!z) {
SampleQueue sampleQueue = this.sampleQueues[indexOf];
z = (sampleQueue.seekTo(j, true) || sampleQueue.getReadIndex() == 0) ? false : true;
}
}
}
if (this.enabledTrackCount == 0) {
this.pendingDeferredRetry = false;
this.notifyDiscontinuity = false;
if (this.loader.isLoading()) {
SampleQueue[] sampleQueueArr = this.sampleQueues;
int length = sampleQueueArr.length;
while (i2 < length) {
sampleQueueArr[i2].discardToEnd();
i2++;
}
this.loader.cancelLoading();
} else {
SampleQueue[] sampleQueueArr2 = this.sampleQueues;
int length2 = sampleQueueArr2.length;
while (i2 < length2) {
sampleQueueArr2[i2].reset();
i2++;
}
}
} else if (z) {
j = seekToUs(j);
while (i2 < sampleStreamArr.length) {
if (sampleStreamArr[i2] != null) {
zArr2[i2] = true;
}
i2++;
}
}
this.seenFirstTrackSelection = true;
return j;
}
@Override // androidx.media3.exoplayer.source.MediaPeriod
public void discardBuffer(long j, boolean z) {
assertPrepared();
if (isPendingReset()) {
return;
}
boolean[] zArr = this.trackState.trackEnabledStates;
int length = this.sampleQueues.length;
for (int i = 0; i < length; i++) {
this.sampleQueues[i].discardTo(j, z, zArr[i]);
}
}
@Override // androidx.media3.exoplayer.source.MediaPeriod, androidx.media3.exoplayer.source.SequenceableLoader
public boolean continueLoading(long j) {
if (this.loadingFinished || this.loader.hasFatalError() || this.pendingDeferredRetry) {
return false;
}
if (this.prepared && this.enabledTrackCount == 0) {
return false;
}
boolean open = this.loadCondition.open();
if (this.loader.isLoading()) {
return open;
}
startLoading();
return true;
}
@Override // androidx.media3.exoplayer.source.MediaPeriod, androidx.media3.exoplayer.source.SequenceableLoader
public boolean isLoading() {
return this.loader.isLoading() && this.loadCondition.isOpen();
}
@Override // androidx.media3.exoplayer.source.MediaPeriod, androidx.media3.exoplayer.source.SequenceableLoader
public long getNextLoadPositionUs() {
return getBufferedPositionUs();
}
@Override // androidx.media3.exoplayer.source.MediaPeriod
public long readDiscontinuity() {
if (!this.notifyDiscontinuity) {
return -9223372036854775807L;
}
if (!this.loadingFinished && getExtractedSamplesCount() <= this.extractedSamplesCountAtStartOfLoad) {
return -9223372036854775807L;
}
this.notifyDiscontinuity = false;
return this.lastSeekPositionUs;
}
@Override // androidx.media3.exoplayer.source.MediaPeriod, androidx.media3.exoplayer.source.SequenceableLoader
public long getBufferedPositionUs() {
long j;
assertPrepared();
if (this.loadingFinished || this.enabledTrackCount == 0) {
return Long.MIN_VALUE;
}
if (isPendingReset()) {
return this.pendingResetPositionUs;
}
if (this.haveAudioVideoTracks) {
int length = this.sampleQueues.length;
j = Long.MAX_VALUE;
for (int i = 0; i < length; i++) {
if (this.trackState.trackIsAudioVideoFlags[i] && this.trackState.trackEnabledStates[i] && !this.sampleQueues[i].isLastSampleQueued()) {
j = Math.min(j, this.sampleQueues[i].getLargestQueuedTimestampUs());
}
}
} else {
j = Long.MAX_VALUE;
}
if (j == Long.MAX_VALUE) {
j = getLargestQueuedTimestampUs(false);
}
return j == Long.MIN_VALUE ? this.lastSeekPositionUs : j;
}
@Override // androidx.media3.exoplayer.source.MediaPeriod
public long seekToUs(long j) {
assertPrepared();
boolean[] zArr = this.trackState.trackIsAudioVideoFlags;
if (!this.seekMap.isSeekable()) {
j = 0;
}
int i = 0;
this.notifyDiscontinuity = false;
this.lastSeekPositionUs = j;
if (isPendingReset()) {
this.pendingResetPositionUs = j;
return j;
}
if (this.dataType != 7 && seekInsideBufferUs(zArr, j)) {
return j;
}
this.pendingDeferredRetry = false;
this.pendingResetPositionUs = j;
this.loadingFinished = false;
if (this.loader.isLoading()) {
SampleQueue[] sampleQueueArr = this.sampleQueues;
int length = sampleQueueArr.length;
while (i < length) {
sampleQueueArr[i].discardToEnd();
i++;
}
this.loader.cancelLoading();
} else {
this.loader.clearFatalError();
SampleQueue[] sampleQueueArr2 = this.sampleQueues;
int length2 = sampleQueueArr2.length;
while (i < length2) {
sampleQueueArr2[i].reset();
i++;
}
}
return j;
}
@Override // androidx.media3.exoplayer.source.MediaPeriod
public long getAdjustedSeekPositionUs(long j, SeekParameters seekParameters) {
assertPrepared();
if (!this.seekMap.isSeekable()) {
return 0L;
}
SeekMap.SeekPoints seekPoints = this.seekMap.getSeekPoints(j);
return seekParameters.resolveSeekPositionUs(j, seekPoints.first.timeUs, seekPoints.second.timeUs);
}
boolean isReady(int i) {
return !suppressRead() && this.sampleQueues[i].isReady(this.loadingFinished);
}
void maybeThrowError(int i) throws IOException {
this.sampleQueues[i].maybeThrowError();
maybeThrowError();
}
void maybeThrowError() throws IOException {
this.loader.maybeThrowError(this.loadErrorHandlingPolicy.getMinimumLoadableRetryCount(this.dataType));
}
int readData(int i, FormatHolder formatHolder, DecoderInputBuffer decoderInputBuffer, int i2) {
if (suppressRead()) {
return -3;
}
maybeNotifyDownstreamFormat(i);
int read = this.sampleQueues[i].read(formatHolder, decoderInputBuffer, i2, this.loadingFinished);
if (read == -3) {
maybeStartDeferredRetry(i);
}
return read;
}
int skipData(int i, long j) {
if (suppressRead()) {
return 0;
}
maybeNotifyDownstreamFormat(i);
SampleQueue sampleQueue = this.sampleQueues[i];
int skipCount = sampleQueue.getSkipCount(j, this.loadingFinished);
sampleQueue.skip(skipCount);
if (skipCount == 0) {
maybeStartDeferredRetry(i);
}
return skipCount;
}
private void maybeNotifyDownstreamFormat(int i) {
assertPrepared();
boolean[] zArr = this.trackState.trackNotifiedDownstreamFormats;
if (zArr[i]) {
return;
}
Format format = this.trackState.tracks.get(i).getFormat(0);
this.mediaSourceEventDispatcher.downstreamFormatChanged(MimeTypes.getTrackType(format.sampleMimeType), format, 0, null, this.lastSeekPositionUs);
zArr[i] = true;
}
private void maybeStartDeferredRetry(int i) {
assertPrepared();
boolean[] zArr = this.trackState.trackIsAudioVideoFlags;
if (this.pendingDeferredRetry && zArr[i]) {
if (this.sampleQueues[i].isReady(false)) {
return;
}
this.pendingResetPositionUs = 0L;
this.pendingDeferredRetry = false;
this.notifyDiscontinuity = true;
this.lastSeekPositionUs = 0L;
this.extractedSamplesCountAtStartOfLoad = 0;
for (SampleQueue sampleQueue : this.sampleQueues) {
sampleQueue.reset();
}
((MediaPeriod.Callback) Assertions.checkNotNull(this.callback)).onContinueLoadingRequested(this);
}
}
private boolean suppressRead() {
return this.notifyDiscontinuity || isPendingReset();
}
@Override // androidx.media3.exoplayer.upstream.Loader.Callback
public void onLoadCompleted(ExtractingLoadable extractingLoadable, long j, long j2) {
SeekMap seekMap;
if (this.durationUs == -9223372036854775807L && (seekMap = this.seekMap) != null) {
boolean isSeekable = seekMap.isSeekable();
long largestQueuedTimestampUs = getLargestQueuedTimestampUs(true);
long j3 = largestQueuedTimestampUs == Long.MIN_VALUE ? 0L : largestQueuedTimestampUs + 10000;
this.durationUs = j3;
this.listener.onSourceInfoRefreshed(j3, isSeekable, this.isLive);
}
StatsDataSource statsDataSource = extractingLoadable.dataSource;
LoadEventInfo loadEventInfo = new LoadEventInfo(extractingLoadable.loadTaskId, extractingLoadable.dataSpec, statsDataSource.getLastOpenedUri(), statsDataSource.getLastResponseHeaders(), j, j2, statsDataSource.getBytesRead());
this.loadErrorHandlingPolicy.onLoadTaskConcluded(extractingLoadable.loadTaskId);
this.mediaSourceEventDispatcher.loadCompleted(loadEventInfo, 1, -1, null, 0, null, extractingLoadable.seekTimeUs, this.durationUs);
this.loadingFinished = true;
((MediaPeriod.Callback) Assertions.checkNotNull(this.callback)).onContinueLoadingRequested(this);
}
@Override // androidx.media3.exoplayer.upstream.Loader.Callback
public void onLoadCanceled(ExtractingLoadable extractingLoadable, long j, long j2, boolean z) {
StatsDataSource statsDataSource = extractingLoadable.dataSource;
LoadEventInfo loadEventInfo = new LoadEventInfo(extractingLoadable.loadTaskId, extractingLoadable.dataSpec, statsDataSource.getLastOpenedUri(), statsDataSource.getLastResponseHeaders(), j, j2, statsDataSource.getBytesRead());
this.loadErrorHandlingPolicy.onLoadTaskConcluded(extractingLoadable.loadTaskId);
this.mediaSourceEventDispatcher.loadCanceled(loadEventInfo, 1, -1, null, 0, null, extractingLoadable.seekTimeUs, this.durationUs);
if (z) {
return;
}
for (SampleQueue sampleQueue : this.sampleQueues) {
sampleQueue.reset();
}
if (this.enabledTrackCount > 0) {
((MediaPeriod.Callback) Assertions.checkNotNull(this.callback)).onContinueLoadingRequested(this);
}
}
@Override // androidx.media3.exoplayer.upstream.Loader.Callback
public Loader.LoadErrorAction onLoadError(ExtractingLoadable extractingLoadable, long j, long j2, IOException iOException, int i) {
boolean z;
ExtractingLoadable extractingLoadable2;
Loader.LoadErrorAction loadErrorAction;
StatsDataSource statsDataSource = extractingLoadable.dataSource;
LoadEventInfo loadEventInfo = new LoadEventInfo(extractingLoadable.loadTaskId, extractingLoadable.dataSpec, statsDataSource.getLastOpenedUri(), statsDataSource.getLastResponseHeaders(), j, j2, statsDataSource.getBytesRead());
long retryDelayMsFor = this.loadErrorHandlingPolicy.getRetryDelayMsFor(new LoadErrorHandlingPolicy.LoadErrorInfo(loadEventInfo, new MediaLoadData(1, -1, null, 0, null, Util.usToMs(extractingLoadable.seekTimeUs), Util.usToMs(this.durationUs)), iOException, i));
if (retryDelayMsFor == -9223372036854775807L) {
loadErrorAction = Loader.DONT_RETRY_FATAL;
} else {
int extractedSamplesCount = getExtractedSamplesCount();
if (extractedSamplesCount > this.extractedSamplesCountAtStartOfLoad) {
extractingLoadable2 = extractingLoadable;
z = true;
} else {
z = false;
extractingLoadable2 = extractingLoadable;
}
if (configureRetry(extractingLoadable2, extractedSamplesCount)) {
loadErrorAction = Loader.createRetryAction(z, retryDelayMsFor);
} else {
loadErrorAction = Loader.DONT_RETRY;
}
}
boolean z2 = !loadErrorAction.isRetry();
this.mediaSourceEventDispatcher.loadError(loadEventInfo, 1, -1, null, 0, null, extractingLoadable.seekTimeUs, this.durationUs, iOException, z2);
if (z2) {
this.loadErrorHandlingPolicy.onLoadTaskConcluded(extractingLoadable.loadTaskId);
}
return loadErrorAction;
}
@Override // androidx.media3.extractor.ExtractorOutput
public TrackOutput track(int i, int i2) {
return prepareTrackOutput(new TrackId(i, false));
}
@Override // androidx.media3.extractor.ExtractorOutput
public void endTracks() {
this.sampleQueuesBuilt = true;
this.handler.post(this.maybeFinishPrepareRunnable);
}
@Override // androidx.media3.extractor.ExtractorOutput
public void seekMap(final SeekMap seekMap) {
this.handler.post(new Runnable() { // from class: androidx.media3.exoplayer.source.ProgressiveMediaPeriod$$ExternalSyntheticLambda3
@Override // java.lang.Runnable
public final void run() {
ProgressiveMediaPeriod.this.m5267x33ac0ff2(seekMap);
}
});
}
TrackOutput icyTrack() {
return prepareTrackOutput(new TrackId(0, true));
}
@Override // androidx.media3.exoplayer.source.SampleQueue.UpstreamFormatChangedListener
public void onUpstreamFormatChanged(Format format) {
this.handler.post(this.maybeFinishPrepareRunnable);
}
/* JADX INFO: Access modifiers changed from: private */
public void onLengthKnown() {
this.handler.post(new Runnable() { // from class: androidx.media3.exoplayer.source.ProgressiveMediaPeriod$$ExternalSyntheticLambda2
@Override // java.lang.Runnable
public final void run() {
ProgressiveMediaPeriod.this.m5266xa2bd730d();
}
});
}
private TrackOutput prepareTrackOutput(TrackId trackId) {
int length = this.sampleQueues.length;
for (int i = 0; i < length; i++) {
if (trackId.equals(this.sampleQueueTrackIds[i])) {
return this.sampleQueues[i];
}
}
SampleQueue createWithDrm = SampleQueue.createWithDrm(this.allocator, this.drmSessionManager, this.drmEventDispatcher);
createWithDrm.setUpstreamFormatChangeListener(this);
int i2 = length + 1;
TrackId[] trackIdArr = (TrackId[]) Arrays.copyOf(this.sampleQueueTrackIds, i2);
trackIdArr[length] = trackId;
this.sampleQueueTrackIds = (TrackId[]) Util.castNonNullTypeArray(trackIdArr);
SampleQueue[] sampleQueueArr = (SampleQueue[]) Arrays.copyOf(this.sampleQueues, i2);
sampleQueueArr[length] = createWithDrm;
this.sampleQueues = (SampleQueue[]) Util.castNonNullTypeArray(sampleQueueArr);
return createWithDrm;
}
/* JADX INFO: Access modifiers changed from: private */
/* renamed from: setSeekMap, reason: merged with bridge method [inline-methods] */
public void m5267x33ac0ff2(SeekMap seekMap) {
this.seekMap = this.icyHeaders == null ? seekMap : new SeekMap.Unseekable(-9223372036854775807L);
this.durationUs = seekMap.getDurationUs();
boolean z = !this.isLengthKnown && seekMap.getDurationUs() == -9223372036854775807L;
this.isLive = z;
this.dataType = z ? 7 : 1;
this.listener.onSourceInfoRefreshed(this.durationUs, seekMap.isSeekable(), this.isLive);
if (this.prepared) {
return;
}
maybeFinishPrepare();
}
/* JADX INFO: Access modifiers changed from: private */
public void maybeFinishPrepare() {
if (this.released || this.prepared || !this.sampleQueuesBuilt || this.seekMap == null) {
return;
}
for (SampleQueue sampleQueue : this.sampleQueues) {
if (sampleQueue.getUpstreamFormat() == null) {
return;
}
}
this.loadCondition.close();
int length = this.sampleQueues.length;
TrackGroup[] trackGroupArr = new TrackGroup[length];
boolean[] zArr = new boolean[length];
for (int i = 0; i < length; i++) {
Format format = (Format) Assertions.checkNotNull(this.sampleQueues[i].getUpstreamFormat());
String str = format.sampleMimeType;
boolean isAudio = MimeTypes.isAudio(str);
boolean z = isAudio || MimeTypes.isVideo(str);
zArr[i] = z;
this.haveAudioVideoTracks = z | this.haveAudioVideoTracks;
IcyHeaders icyHeaders = this.icyHeaders;
if (icyHeaders != null) {
if (isAudio || this.sampleQueueTrackIds[i].isIcyTrack) {
Metadata metadata = format.metadata;
format = format.buildUpon().setMetadata(metadata == null ? new Metadata(icyHeaders) : metadata.copyWithAppendedEntries(icyHeaders)).build();
}
if (isAudio && format.averageBitrate == -1 && format.peakBitrate == -1 && icyHeaders.bitrate != -1) {
format = format.buildUpon().setAverageBitrate(icyHeaders.bitrate).build();
}
}
trackGroupArr[i] = new TrackGroup(Integer.toString(i), format.copyWithCryptoType(this.drmSessionManager.getCryptoType(format)));
}
this.trackState = new TrackState(new TrackGroupArray(trackGroupArr), zArr);
this.prepared = true;
((MediaPeriod.Callback) Assertions.checkNotNull(this.callback)).onPrepared(this);
}
private void startLoading() {
ExtractingLoadable extractingLoadable = new ExtractingLoadable(this.uri, this.dataSource, this.progressiveMediaExtractor, this, this.loadCondition);
if (this.prepared) {
Assertions.checkState(isPendingReset());
long j = this.durationUs;
if (j != -9223372036854775807L && this.pendingResetPositionUs > j) {
this.loadingFinished = true;
this.pendingResetPositionUs = -9223372036854775807L;
return;
}
extractingLoadable.setLoadPosition(((SeekMap) Assertions.checkNotNull(this.seekMap)).getSeekPoints(this.pendingResetPositionUs).first.position, this.pendingResetPositionUs);
for (SampleQueue sampleQueue : this.sampleQueues) {
sampleQueue.setStartTimeUs(this.pendingResetPositionUs);
}
this.pendingResetPositionUs = -9223372036854775807L;
}
this.extractedSamplesCountAtStartOfLoad = getExtractedSamplesCount();
this.mediaSourceEventDispatcher.loadStarted(new LoadEventInfo(extractingLoadable.loadTaskId, extractingLoadable.dataSpec, this.loader.startLoading(extractingLoadable, this, this.loadErrorHandlingPolicy.getMinimumLoadableRetryCount(this.dataType))), 1, -1, null, 0, null, extractingLoadable.seekTimeUs, this.durationUs);
}
private boolean configureRetry(ExtractingLoadable extractingLoadable, int i) {
SeekMap seekMap;
if (this.isLengthKnown || !((seekMap = this.seekMap) == null || seekMap.getDurationUs() == -9223372036854775807L)) {
this.extractedSamplesCountAtStartOfLoad = i;
return true;
}
if (this.prepared && !suppressRead()) {
this.pendingDeferredRetry = true;
return false;
}
this.notifyDiscontinuity = this.prepared;
this.lastSeekPositionUs = 0L;
this.extractedSamplesCountAtStartOfLoad = 0;
for (SampleQueue sampleQueue : this.sampleQueues) {
sampleQueue.reset();
}
extractingLoadable.setLoadPosition(0L, 0L);
return true;
}
private boolean seekInsideBufferUs(boolean[] zArr, long j) {
int length = this.sampleQueues.length;
for (int i = 0; i < length; i++) {
if (!this.sampleQueues[i].seekTo(j, false) && (zArr[i] || !this.haveAudioVideoTracks)) {
return false;
}
}
return true;
}
private int getExtractedSamplesCount() {
int i = 0;
for (SampleQueue sampleQueue : this.sampleQueues) {
i += sampleQueue.getWriteIndex();
}
return i;
}
/* JADX INFO: Access modifiers changed from: private */
public long getLargestQueuedTimestampUs(boolean z) {
long j = Long.MIN_VALUE;
for (int i = 0; i < this.sampleQueues.length; i++) {
if (z || ((TrackState) Assertions.checkNotNull(this.trackState)).trackEnabledStates[i]) {
j = Math.max(j, this.sampleQueues[i].getLargestQueuedTimestampUs());
}
}
return j;
}
@EnsuresNonNull({"trackState", "seekMap"})
private void assertPrepared() {
Assertions.checkState(this.prepared);
Assertions.checkNotNull(this.trackState);
Assertions.checkNotNull(this.seekMap);
}
/* loaded from: classes2.dex */
private final class SampleStreamImpl implements SampleStream {
private final int track;
public SampleStreamImpl(int i) {
this.track = i;
}
@Override // androidx.media3.exoplayer.source.SampleStream
public boolean isReady() {
return ProgressiveMediaPeriod.this.isReady(this.track);
}
@Override // androidx.media3.exoplayer.source.SampleStream
public void maybeThrowError() throws IOException {
ProgressiveMediaPeriod.this.maybeThrowError(this.track);
}
@Override // androidx.media3.exoplayer.source.SampleStream
public int readData(FormatHolder formatHolder, DecoderInputBuffer decoderInputBuffer, int i) {
return ProgressiveMediaPeriod.this.readData(this.track, formatHolder, decoderInputBuffer, i);
}
@Override // androidx.media3.exoplayer.source.SampleStream
public int skipData(long j) {
return ProgressiveMediaPeriod.this.skipData(this.track, j);
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public final class ExtractingLoadable implements Loader.Loadable, IcyDataSource.Listener {
private final StatsDataSource dataSource;
private final ExtractorOutput extractorOutput;
private TrackOutput icyTrackOutput;
private volatile boolean loadCanceled;
private final ConditionVariable loadCondition;
private final ProgressiveMediaExtractor progressiveMediaExtractor;
private long seekTimeUs;
private boolean seenIcyMetadata;
private final Uri uri;
private final PositionHolder positionHolder = new PositionHolder();
private boolean pendingExtractorSeek = true;
private final long loadTaskId = LoadEventInfo.getNewId();
private DataSpec dataSpec = buildDataSpec(0);
@Override // androidx.media3.exoplayer.upstream.Loader.Loadable
public void cancelLoad() {
this.loadCanceled = true;
}
public ExtractingLoadable(Uri uri, DataSource dataSource, ProgressiveMediaExtractor progressiveMediaExtractor, ExtractorOutput extractorOutput, ConditionVariable conditionVariable) {
this.uri = uri;
this.dataSource = new StatsDataSource(dataSource);
this.progressiveMediaExtractor = progressiveMediaExtractor;
this.extractorOutput = extractorOutput;
this.loadCondition = conditionVariable;
}
@Override // androidx.media3.exoplayer.upstream.Loader.Loadable
public void load() throws IOException {
int i = 0;
while (i == 0 && !this.loadCanceled) {
try {
long j = this.positionHolder.position;
DataSpec buildDataSpec = buildDataSpec(j);
this.dataSpec = buildDataSpec;
long open = this.dataSource.open(buildDataSpec);
if (open != -1) {
open += j;
ProgressiveMediaPeriod.this.onLengthKnown();
}
long j2 = open;
ProgressiveMediaPeriod.this.icyHeaders = IcyHeaders.parse(this.dataSource.getResponseHeaders());
DataReader dataReader = this.dataSource;
if (ProgressiveMediaPeriod.this.icyHeaders != null && ProgressiveMediaPeriod.this.icyHeaders.metadataInterval != -1) {
dataReader = new IcyDataSource(this.dataSource, ProgressiveMediaPeriod.this.icyHeaders.metadataInterval, this);
TrackOutput icyTrack = ProgressiveMediaPeriod.this.icyTrack();
this.icyTrackOutput = icyTrack;
icyTrack.format(ProgressiveMediaPeriod.ICY_FORMAT);
}
long j3 = j;
this.progressiveMediaExtractor.init(dataReader, this.uri, this.dataSource.getResponseHeaders(), j, j2, this.extractorOutput);
if (ProgressiveMediaPeriod.this.icyHeaders != null) {
this.progressiveMediaExtractor.disableSeekingOnMp3Streams();
}
if (this.pendingExtractorSeek) {
this.progressiveMediaExtractor.seek(j3, this.seekTimeUs);
this.pendingExtractorSeek = false;
}
while (true) {
long j4 = j3;
while (i == 0 && !this.loadCanceled) {
try {
this.loadCondition.block();
i = this.progressiveMediaExtractor.read(this.positionHolder);
j3 = this.progressiveMediaExtractor.getCurrentInputPosition();
if (j3 > ProgressiveMediaPeriod.this.continueLoadingCheckIntervalBytes + j4) {
break;
}
} catch (InterruptedException unused) {
throw new InterruptedIOException();
}
}
this.loadCondition.close();
ProgressiveMediaPeriod.this.handler.post(ProgressiveMediaPeriod.this.onContinueLoadingRequestedRunnable);
}
if (i == 1) {
i = 0;
} else if (this.progressiveMediaExtractor.getCurrentInputPosition() != -1) {
this.positionHolder.position = this.progressiveMediaExtractor.getCurrentInputPosition();
}
DataSourceUtil.closeQuietly(this.dataSource);
} catch (Throwable th) {
if (i != 1 && this.progressiveMediaExtractor.getCurrentInputPosition() != -1) {
this.positionHolder.position = this.progressiveMediaExtractor.getCurrentInputPosition();
}
DataSourceUtil.closeQuietly(this.dataSource);
throw th;
}
}
}
@Override // androidx.media3.exoplayer.source.IcyDataSource.Listener
public void onIcyMetadata(ParsableByteArray parsableByteArray) {
long max = !this.seenIcyMetadata ? this.seekTimeUs : Math.max(ProgressiveMediaPeriod.this.getLargestQueuedTimestampUs(true), this.seekTimeUs);
int bytesLeft = parsableByteArray.bytesLeft();
TrackOutput trackOutput = (TrackOutput) Assertions.checkNotNull(this.icyTrackOutput);
trackOutput.sampleData(parsableByteArray, bytesLeft);
trackOutput.sampleMetadata(max, 1, bytesLeft, 0, null);
this.seenIcyMetadata = true;
}
private DataSpec buildDataSpec(long j) {
return new DataSpec.Builder().setUri(this.uri).setPosition(j).setKey(ProgressiveMediaPeriod.this.customCacheKey).setFlags(6).setHttpRequestHeaders(ProgressiveMediaPeriod.ICY_METADATA_HEADERS).build();
}
/* JADX INFO: Access modifiers changed from: private */
public void setLoadPosition(long j, long j2) {
this.positionHolder.position = j;
this.seekTimeUs = j2;
this.pendingExtractorSeek = true;
this.seenIcyMetadata = false;
}
}
/* JADX INFO: Access modifiers changed from: private */
/* loaded from: classes2.dex */
public static final class TrackState {
public final boolean[] trackEnabledStates;
public final boolean[] trackIsAudioVideoFlags;
public final boolean[] trackNotifiedDownstreamFormats;
public final TrackGroupArray tracks;
public TrackState(TrackGroupArray trackGroupArray, boolean[] zArr) {
this.tracks = trackGroupArray;
this.trackIsAudioVideoFlags = zArr;
this.trackEnabledStates = new boolean[trackGroupArray.length];
this.trackNotifiedDownstreamFormats = new boolean[trackGroupArray.length];
}
}
/* JADX INFO: Access modifiers changed from: private */
/* loaded from: classes2.dex */
public static final class TrackId {
public final int id;
public final boolean isIcyTrack;
public int hashCode() {
return (this.id * 31) + (this.isIcyTrack ? 1 : 0);
}
public TrackId(int i, boolean z) {
this.id = i;
this.isIcyTrack = z;
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
TrackId trackId = (TrackId) obj;
return this.id == trackId.id && this.isIcyTrack == trackId.isIcyTrack;
}
}
private static Map<String, String> createIcyMetadataHeaders() {
HashMap hashMap = new HashMap();
hashMap.put("Icy-MetaData", "1");
return Collections.unmodifiableMap(hashMap);
}
}