mirror of
https://github.com/samsonjs/media.git
synced 2026-04-01 10:35:48 +00:00
Use LoadErrorHandlingPolicy in SingleSampleMediaSource
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=207525746
This commit is contained in:
parent
bf6b647088
commit
18d2a2ea2a
2 changed files with 61 additions and 23 deletions
|
|
@ -25,6 +25,7 @@ import com.google.android.exoplayer2.source.MediaSourceEventListener.EventDispat
|
|||
import com.google.android.exoplayer2.trackselection.TrackSelection;
|
||||
import com.google.android.exoplayer2.upstream.DataSource;
|
||||
import com.google.android.exoplayer2.upstream.DataSpec;
|
||||
import com.google.android.exoplayer2.upstream.LoadErrorHandlingPolicy;
|
||||
import com.google.android.exoplayer2.upstream.Loader;
|
||||
import com.google.android.exoplayer2.upstream.Loader.LoadErrorAction;
|
||||
import com.google.android.exoplayer2.upstream.Loader.Loadable;
|
||||
|
|
@ -50,7 +51,7 @@ import java.util.Arrays;
|
|||
private final DataSpec dataSpec;
|
||||
private final DataSource.Factory dataSourceFactory;
|
||||
private final @Nullable TransferListener transferListener;
|
||||
private final int minLoadableRetryCount;
|
||||
private final LoadErrorHandlingPolicy loadErrorHandlingPolicy;
|
||||
private final EventDispatcher eventDispatcher;
|
||||
private final TrackGroupArray tracks;
|
||||
private final ArrayList<SampleStreamImpl> sampleStreams;
|
||||
|
|
@ -73,7 +74,7 @@ import java.util.Arrays;
|
|||
@Nullable TransferListener transferListener,
|
||||
Format format,
|
||||
long durationUs,
|
||||
int minLoadableRetryCount,
|
||||
LoadErrorHandlingPolicy loadErrorHandlingPolicy,
|
||||
EventDispatcher eventDispatcher,
|
||||
boolean treatLoadErrorsAsEndOfStream) {
|
||||
this.dataSpec = dataSpec;
|
||||
|
|
@ -81,7 +82,7 @@ import java.util.Arrays;
|
|||
this.transferListener = transferListener;
|
||||
this.format = format;
|
||||
this.durationUs = durationUs;
|
||||
this.minLoadableRetryCount = minLoadableRetryCount;
|
||||
this.loadErrorHandlingPolicy = loadErrorHandlingPolicy;
|
||||
this.eventDispatcher = eventDispatcher;
|
||||
this.treatLoadErrorsAsEndOfStream = treatLoadErrorsAsEndOfStream;
|
||||
tracks = new TrackGroupArray(new TrackGroup(format));
|
||||
|
|
@ -149,7 +150,9 @@ import java.util.Arrays;
|
|||
}
|
||||
long elapsedRealtimeMs =
|
||||
loader.startLoading(
|
||||
new SourceLoadable(dataSpec, dataSource), /* callback= */ this, minLoadableRetryCount);
|
||||
new SourceLoadable(dataSpec, dataSource),
|
||||
/* callback= */ this,
|
||||
loadErrorHandlingPolicy.getMinimumLoadableRetryCount(C.DATA_TYPE_MEDIA));
|
||||
eventDispatcher.loadStarted(
|
||||
dataSpec,
|
||||
dataSpec.uri,
|
||||
|
|
@ -245,7 +248,24 @@ import java.util.Arrays;
|
|||
long loadDurationMs,
|
||||
IOException error,
|
||||
int errorCount) {
|
||||
boolean cancel = treatLoadErrorsAsEndOfStream && errorCount >= minLoadableRetryCount;
|
||||
long retryDelay =
|
||||
loadErrorHandlingPolicy.getRetryDelayMsFor(
|
||||
C.DATA_TYPE_MEDIA, durationUs, error, errorCount);
|
||||
boolean errorCanBePropagated =
|
||||
retryDelay == C.TIME_UNSET
|
||||
|| errorCount
|
||||
>= loadErrorHandlingPolicy.getMinimumLoadableRetryCount(C.DATA_TYPE_MEDIA);
|
||||
|
||||
LoadErrorAction action;
|
||||
if (treatLoadErrorsAsEndOfStream && errorCanBePropagated) {
|
||||
loadingFinished = true;
|
||||
action = Loader.DONT_RETRY;
|
||||
} else {
|
||||
action =
|
||||
retryDelay != C.TIME_UNSET
|
||||
? Loader.createRetryAction(/* resetErrorCount= */ false, retryDelay)
|
||||
: Loader.DONT_RETRY_FATAL;
|
||||
}
|
||||
eventDispatcher.loadError(
|
||||
loadable.dataSpec,
|
||||
loadable.dataSource.getLastOpenedUri(),
|
||||
|
|
@ -260,12 +280,8 @@ import java.util.Arrays;
|
|||
loadDurationMs,
|
||||
loadable.dataSource.getBytesRead(),
|
||||
error,
|
||||
/* wasCanceled= */ cancel);
|
||||
if (cancel) {
|
||||
loadingFinished = true;
|
||||
return Loader.DONT_RETRY;
|
||||
}
|
||||
return Loader.RETRY;
|
||||
/* wasCanceled= */ !action.isRetry());
|
||||
return action;
|
||||
}
|
||||
|
||||
private final class SampleStreamImpl implements SampleStream {
|
||||
|
|
|
|||
|
|
@ -24,6 +24,8 @@ import com.google.android.exoplayer2.Timeline;
|
|||
import com.google.android.exoplayer2.upstream.Allocator;
|
||||
import com.google.android.exoplayer2.upstream.DataSource;
|
||||
import com.google.android.exoplayer2.upstream.DataSpec;
|
||||
import com.google.android.exoplayer2.upstream.DefaultLoadErrorHandlingPolicy;
|
||||
import com.google.android.exoplayer2.upstream.LoadErrorHandlingPolicy;
|
||||
import com.google.android.exoplayer2.upstream.TransferListener;
|
||||
import com.google.android.exoplayer2.util.Assertions;
|
||||
import java.io.IOException;
|
||||
|
|
@ -56,7 +58,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
|
|||
|
||||
private final DataSource.Factory dataSourceFactory;
|
||||
|
||||
private int minLoadableRetryCount;
|
||||
private LoadErrorHandlingPolicy loadErrorHandlingPolicy;
|
||||
private boolean treatLoadErrorsAsEndOfStream;
|
||||
private boolean isCreateCalled;
|
||||
private @Nullable Object tag;
|
||||
|
|
@ -69,7 +71,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
|
|||
*/
|
||||
public Factory(DataSource.Factory dataSourceFactory) {
|
||||
this.dataSourceFactory = Assertions.checkNotNull(dataSourceFactory);
|
||||
this.minLoadableRetryCount = DEFAULT_MIN_LOADABLE_RETRY_COUNT;
|
||||
loadErrorHandlingPolicy = new DefaultLoadErrorHandlingPolicy();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -87,16 +89,36 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the minimum number of times to retry if a loading error occurs. The default value is
|
||||
* {@link #DEFAULT_MIN_LOADABLE_RETRY_COUNT}.
|
||||
* Sets the minimum number of times to retry if a loading error occurs. See {@link
|
||||
* #setLoadErrorHandlingPolicy} for the default value.
|
||||
*
|
||||
* <p>Calling this method is equivalent to calling {@link #setLoadErrorHandlingPolicy} with
|
||||
* {@link DefaultLoadErrorHandlingPolicy (int)
|
||||
* DefaultLoadErrorHandlingPolicy(minLoadableRetryCount)}
|
||||
*
|
||||
* @param minLoadableRetryCount The minimum number of times to retry if a loading error occurs.
|
||||
* @return This factory, for convenience.
|
||||
* @throws IllegalStateException If one of the {@code create} methods has already been called.
|
||||
* @deprecated Use {@link #setLoadErrorHandlingPolicy(LoadErrorHandlingPolicy)} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public Factory setMinLoadableRetryCount(int minLoadableRetryCount) {
|
||||
return setLoadErrorHandlingPolicy(new DefaultLoadErrorHandlingPolicy(minLoadableRetryCount));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link LoadErrorHandlingPolicy}. The default value is created by calling {@link
|
||||
* DefaultLoadErrorHandlingPolicy()}.
|
||||
*
|
||||
* <p>Calling this method overrides any calls to {@link #setMinLoadableRetryCount(int)}.
|
||||
*
|
||||
* @param loadErrorHandlingPolicy A {@link LoadErrorHandlingPolicy}.
|
||||
* @return This factory, for convenience.
|
||||
* @throws IllegalStateException If one of the {@code create} methods has already been called.
|
||||
*/
|
||||
public Factory setLoadErrorHandlingPolicy(LoadErrorHandlingPolicy loadErrorHandlingPolicy) {
|
||||
Assertions.checkState(!isCreateCalled);
|
||||
this.minLoadableRetryCount = minLoadableRetryCount;
|
||||
this.loadErrorHandlingPolicy = loadErrorHandlingPolicy;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
@ -131,7 +153,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
|
|||
dataSourceFactory,
|
||||
format,
|
||||
durationUs,
|
||||
minLoadableRetryCount,
|
||||
loadErrorHandlingPolicy,
|
||||
treatLoadErrorsAsEndOfStream,
|
||||
tag);
|
||||
}
|
||||
|
|
@ -165,7 +187,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
|
|||
private final DataSource.Factory dataSourceFactory;
|
||||
private final Format format;
|
||||
private final long durationUs;
|
||||
private final int minLoadableRetryCount;
|
||||
private final LoadErrorHandlingPolicy loadErrorHandlingPolicy;
|
||||
private final boolean treatLoadErrorsAsEndOfStream;
|
||||
private final Timeline timeline;
|
||||
|
||||
|
|
@ -206,7 +228,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
|
|||
dataSourceFactory,
|
||||
format,
|
||||
durationUs,
|
||||
minLoadableRetryCount,
|
||||
new DefaultLoadErrorHandlingPolicy(minLoadableRetryCount),
|
||||
/* treatLoadErrorsAsEndOfStream= */ false,
|
||||
/* tag= */ null);
|
||||
}
|
||||
|
|
@ -242,7 +264,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
|
|||
dataSourceFactory,
|
||||
format,
|
||||
durationUs,
|
||||
minLoadableRetryCount,
|
||||
new DefaultLoadErrorHandlingPolicy(minLoadableRetryCount),
|
||||
treatLoadErrorsAsEndOfStream,
|
||||
/* tag= */ null);
|
||||
if (eventHandler != null && eventListener != null) {
|
||||
|
|
@ -255,13 +277,13 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
|
|||
DataSource.Factory dataSourceFactory,
|
||||
Format format,
|
||||
long durationUs,
|
||||
int minLoadableRetryCount,
|
||||
LoadErrorHandlingPolicy loadErrorHandlingPolicy,
|
||||
boolean treatLoadErrorsAsEndOfStream,
|
||||
@Nullable Object tag) {
|
||||
this.dataSourceFactory = dataSourceFactory;
|
||||
this.format = format;
|
||||
this.durationUs = durationUs;
|
||||
this.minLoadableRetryCount = minLoadableRetryCount;
|
||||
this.loadErrorHandlingPolicy = loadErrorHandlingPolicy;
|
||||
this.treatLoadErrorsAsEndOfStream = treatLoadErrorsAsEndOfStream;
|
||||
dataSpec = new DataSpec(uri);
|
||||
timeline =
|
||||
|
|
@ -293,7 +315,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
|
|||
transferListener,
|
||||
format,
|
||||
durationUs,
|
||||
minLoadableRetryCount,
|
||||
loadErrorHandlingPolicy,
|
||||
createEventDispatcher(id),
|
||||
treatLoadErrorsAsEndOfStream);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue