mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +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.trackselection.TrackSelection;
|
||||||
import com.google.android.exoplayer2.upstream.DataSource;
|
import com.google.android.exoplayer2.upstream.DataSource;
|
||||||
import com.google.android.exoplayer2.upstream.DataSpec;
|
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;
|
||||||
import com.google.android.exoplayer2.upstream.Loader.LoadErrorAction;
|
import com.google.android.exoplayer2.upstream.Loader.LoadErrorAction;
|
||||||
import com.google.android.exoplayer2.upstream.Loader.Loadable;
|
import com.google.android.exoplayer2.upstream.Loader.Loadable;
|
||||||
|
|
@ -50,7 +51,7 @@ import java.util.Arrays;
|
||||||
private final DataSpec dataSpec;
|
private final DataSpec dataSpec;
|
||||||
private final DataSource.Factory dataSourceFactory;
|
private final DataSource.Factory dataSourceFactory;
|
||||||
private final @Nullable TransferListener transferListener;
|
private final @Nullable TransferListener transferListener;
|
||||||
private final int minLoadableRetryCount;
|
private final LoadErrorHandlingPolicy loadErrorHandlingPolicy;
|
||||||
private final EventDispatcher eventDispatcher;
|
private final EventDispatcher eventDispatcher;
|
||||||
private final TrackGroupArray tracks;
|
private final TrackGroupArray tracks;
|
||||||
private final ArrayList<SampleStreamImpl> sampleStreams;
|
private final ArrayList<SampleStreamImpl> sampleStreams;
|
||||||
|
|
@ -73,7 +74,7 @@ import java.util.Arrays;
|
||||||
@Nullable TransferListener transferListener,
|
@Nullable TransferListener transferListener,
|
||||||
Format format,
|
Format format,
|
||||||
long durationUs,
|
long durationUs,
|
||||||
int minLoadableRetryCount,
|
LoadErrorHandlingPolicy loadErrorHandlingPolicy,
|
||||||
EventDispatcher eventDispatcher,
|
EventDispatcher eventDispatcher,
|
||||||
boolean treatLoadErrorsAsEndOfStream) {
|
boolean treatLoadErrorsAsEndOfStream) {
|
||||||
this.dataSpec = dataSpec;
|
this.dataSpec = dataSpec;
|
||||||
|
|
@ -81,7 +82,7 @@ import java.util.Arrays;
|
||||||
this.transferListener = transferListener;
|
this.transferListener = transferListener;
|
||||||
this.format = format;
|
this.format = format;
|
||||||
this.durationUs = durationUs;
|
this.durationUs = durationUs;
|
||||||
this.minLoadableRetryCount = minLoadableRetryCount;
|
this.loadErrorHandlingPolicy = loadErrorHandlingPolicy;
|
||||||
this.eventDispatcher = eventDispatcher;
|
this.eventDispatcher = eventDispatcher;
|
||||||
this.treatLoadErrorsAsEndOfStream = treatLoadErrorsAsEndOfStream;
|
this.treatLoadErrorsAsEndOfStream = treatLoadErrorsAsEndOfStream;
|
||||||
tracks = new TrackGroupArray(new TrackGroup(format));
|
tracks = new TrackGroupArray(new TrackGroup(format));
|
||||||
|
|
@ -149,7 +150,9 @@ import java.util.Arrays;
|
||||||
}
|
}
|
||||||
long elapsedRealtimeMs =
|
long elapsedRealtimeMs =
|
||||||
loader.startLoading(
|
loader.startLoading(
|
||||||
new SourceLoadable(dataSpec, dataSource), /* callback= */ this, minLoadableRetryCount);
|
new SourceLoadable(dataSpec, dataSource),
|
||||||
|
/* callback= */ this,
|
||||||
|
loadErrorHandlingPolicy.getMinimumLoadableRetryCount(C.DATA_TYPE_MEDIA));
|
||||||
eventDispatcher.loadStarted(
|
eventDispatcher.loadStarted(
|
||||||
dataSpec,
|
dataSpec,
|
||||||
dataSpec.uri,
|
dataSpec.uri,
|
||||||
|
|
@ -245,7 +248,24 @@ import java.util.Arrays;
|
||||||
long loadDurationMs,
|
long loadDurationMs,
|
||||||
IOException error,
|
IOException error,
|
||||||
int errorCount) {
|
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(
|
eventDispatcher.loadError(
|
||||||
loadable.dataSpec,
|
loadable.dataSpec,
|
||||||
loadable.dataSource.getLastOpenedUri(),
|
loadable.dataSource.getLastOpenedUri(),
|
||||||
|
|
@ -260,12 +280,8 @@ import java.util.Arrays;
|
||||||
loadDurationMs,
|
loadDurationMs,
|
||||||
loadable.dataSource.getBytesRead(),
|
loadable.dataSource.getBytesRead(),
|
||||||
error,
|
error,
|
||||||
/* wasCanceled= */ cancel);
|
/* wasCanceled= */ !action.isRetry());
|
||||||
if (cancel) {
|
return action;
|
||||||
loadingFinished = true;
|
|
||||||
return Loader.DONT_RETRY;
|
|
||||||
}
|
|
||||||
return Loader.RETRY;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private final class SampleStreamImpl implements SampleStream {
|
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.Allocator;
|
||||||
import com.google.android.exoplayer2.upstream.DataSource;
|
import com.google.android.exoplayer2.upstream.DataSource;
|
||||||
import com.google.android.exoplayer2.upstream.DataSpec;
|
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.upstream.TransferListener;
|
||||||
import com.google.android.exoplayer2.util.Assertions;
|
import com.google.android.exoplayer2.util.Assertions;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
@ -56,7 +58,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
|
||||||
|
|
||||||
private final DataSource.Factory dataSourceFactory;
|
private final DataSource.Factory dataSourceFactory;
|
||||||
|
|
||||||
private int minLoadableRetryCount;
|
private LoadErrorHandlingPolicy loadErrorHandlingPolicy;
|
||||||
private boolean treatLoadErrorsAsEndOfStream;
|
private boolean treatLoadErrorsAsEndOfStream;
|
||||||
private boolean isCreateCalled;
|
private boolean isCreateCalled;
|
||||||
private @Nullable Object tag;
|
private @Nullable Object tag;
|
||||||
|
|
@ -69,7 +71,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
|
||||||
*/
|
*/
|
||||||
public Factory(DataSource.Factory dataSourceFactory) {
|
public Factory(DataSource.Factory dataSourceFactory) {
|
||||||
this.dataSourceFactory = Assertions.checkNotNull(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
|
* Sets the minimum number of times to retry if a loading error occurs. See {@link
|
||||||
* {@link #DEFAULT_MIN_LOADABLE_RETRY_COUNT}.
|
* #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.
|
* @param minLoadableRetryCount The minimum number of times to retry if a loading error occurs.
|
||||||
* @return This factory, for convenience.
|
* @return This factory, for convenience.
|
||||||
* @throws IllegalStateException If one of the {@code create} methods has already been called.
|
* @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) {
|
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);
|
Assertions.checkState(!isCreateCalled);
|
||||||
this.minLoadableRetryCount = minLoadableRetryCount;
|
this.loadErrorHandlingPolicy = loadErrorHandlingPolicy;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -131,7 +153,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
|
||||||
dataSourceFactory,
|
dataSourceFactory,
|
||||||
format,
|
format,
|
||||||
durationUs,
|
durationUs,
|
||||||
minLoadableRetryCount,
|
loadErrorHandlingPolicy,
|
||||||
treatLoadErrorsAsEndOfStream,
|
treatLoadErrorsAsEndOfStream,
|
||||||
tag);
|
tag);
|
||||||
}
|
}
|
||||||
|
|
@ -165,7 +187,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
|
||||||
private final DataSource.Factory dataSourceFactory;
|
private final DataSource.Factory dataSourceFactory;
|
||||||
private final Format format;
|
private final Format format;
|
||||||
private final long durationUs;
|
private final long durationUs;
|
||||||
private final int minLoadableRetryCount;
|
private final LoadErrorHandlingPolicy loadErrorHandlingPolicy;
|
||||||
private final boolean treatLoadErrorsAsEndOfStream;
|
private final boolean treatLoadErrorsAsEndOfStream;
|
||||||
private final Timeline timeline;
|
private final Timeline timeline;
|
||||||
|
|
||||||
|
|
@ -206,7 +228,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
|
||||||
dataSourceFactory,
|
dataSourceFactory,
|
||||||
format,
|
format,
|
||||||
durationUs,
|
durationUs,
|
||||||
minLoadableRetryCount,
|
new DefaultLoadErrorHandlingPolicy(minLoadableRetryCount),
|
||||||
/* treatLoadErrorsAsEndOfStream= */ false,
|
/* treatLoadErrorsAsEndOfStream= */ false,
|
||||||
/* tag= */ null);
|
/* tag= */ null);
|
||||||
}
|
}
|
||||||
|
|
@ -242,7 +264,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
|
||||||
dataSourceFactory,
|
dataSourceFactory,
|
||||||
format,
|
format,
|
||||||
durationUs,
|
durationUs,
|
||||||
minLoadableRetryCount,
|
new DefaultLoadErrorHandlingPolicy(minLoadableRetryCount),
|
||||||
treatLoadErrorsAsEndOfStream,
|
treatLoadErrorsAsEndOfStream,
|
||||||
/* tag= */ null);
|
/* tag= */ null);
|
||||||
if (eventHandler != null && eventListener != null) {
|
if (eventHandler != null && eventListener != null) {
|
||||||
|
|
@ -255,13 +277,13 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
|
||||||
DataSource.Factory dataSourceFactory,
|
DataSource.Factory dataSourceFactory,
|
||||||
Format format,
|
Format format,
|
||||||
long durationUs,
|
long durationUs,
|
||||||
int minLoadableRetryCount,
|
LoadErrorHandlingPolicy loadErrorHandlingPolicy,
|
||||||
boolean treatLoadErrorsAsEndOfStream,
|
boolean treatLoadErrorsAsEndOfStream,
|
||||||
@Nullable Object tag) {
|
@Nullable Object tag) {
|
||||||
this.dataSourceFactory = dataSourceFactory;
|
this.dataSourceFactory = dataSourceFactory;
|
||||||
this.format = format;
|
this.format = format;
|
||||||
this.durationUs = durationUs;
|
this.durationUs = durationUs;
|
||||||
this.minLoadableRetryCount = minLoadableRetryCount;
|
this.loadErrorHandlingPolicy = loadErrorHandlingPolicy;
|
||||||
this.treatLoadErrorsAsEndOfStream = treatLoadErrorsAsEndOfStream;
|
this.treatLoadErrorsAsEndOfStream = treatLoadErrorsAsEndOfStream;
|
||||||
dataSpec = new DataSpec(uri);
|
dataSpec = new DataSpec(uri);
|
||||||
timeline =
|
timeline =
|
||||||
|
|
@ -293,7 +315,7 @@ public final class SingleSampleMediaSource extends BaseMediaSource {
|
||||||
transferListener,
|
transferListener,
|
||||||
format,
|
format,
|
||||||
durationUs,
|
durationUs,
|
||||||
minLoadableRetryCount,
|
loadErrorHandlingPolicy,
|
||||||
createEventDispatcher(id),
|
createEventDispatcher(id),
|
||||||
treatLoadErrorsAsEndOfStream);
|
treatLoadErrorsAsEndOfStream);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue