Update SingleSampleMediaSource with factory/listener changes

- Convert the Builder into a Factory
- Have it use MediaSourceEventListener
- Also made some misc related fixes to other sources

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=178906521
This commit is contained in:
olly 2017-12-13 08:32:28 -08:00 committed by Oliver Woodman
parent 8a6c375c53
commit 67b94a72a5
5 changed files with 280 additions and 126 deletions

View File

@ -431,9 +431,6 @@ import java.util.Arrays;
@Override @Override
public void onLoadCanceled(ExtractingLoadable loadable, long elapsedRealtimeMs, public void onLoadCanceled(ExtractingLoadable loadable, long elapsedRealtimeMs,
long loadDurationMs, boolean released) { long loadDurationMs, boolean released) {
if (released) {
return;
}
eventDispatcher.loadCanceled( eventDispatcher.loadCanceled(
loadable.dataSpec, loadable.dataSpec,
C.DATA_TYPE_MEDIA, C.DATA_TYPE_MEDIA,
@ -446,12 +443,14 @@ import java.util.Arrays;
elapsedRealtimeMs, elapsedRealtimeMs,
loadDurationMs, loadDurationMs,
loadable.bytesLoaded); loadable.bytesLoaded);
copyLengthFromLoader(loadable); if (!released) {
for (SampleQueue sampleQueue : sampleQueues) { copyLengthFromLoader(loadable);
sampleQueue.reset(); for (SampleQueue sampleQueue : sampleQueues) {
} sampleQueue.reset();
if (enabledTrackCount > 0) { }
callback.onContinueLoadingRequested(this); if (enabledTrackCount > 0) {
callback.onContinueLoadingRequested(this);
}
} }
} }

View File

@ -195,7 +195,7 @@ public final class ExtractorMediaSource implements MediaSource, ExtractorMediaPe
* @param uri The {@link Uri}. * @param uri The {@link Uri}.
* @return The new {@link ExtractorMediaSource}. * @return The new {@link ExtractorMediaSource}.
*/ */
public MediaSource createMediaSource(Uri uri) { public ExtractorMediaSource createMediaSource(Uri uri) {
return createMediaSource(uri, null, null); return createMediaSource(uri, null, null);
} }
@ -208,7 +208,7 @@ public final class ExtractorMediaSource implements MediaSource, ExtractorMediaPe
* @return The new {@link ExtractorMediaSource}. * @return The new {@link ExtractorMediaSource}.
*/ */
@Override @Override
public MediaSource createMediaSource( public ExtractorMediaSource createMediaSource(
Uri uri, @Nullable Handler eventHandler, @Nullable MediaSourceEventListener eventListener) { Uri uri, @Nullable Handler eventHandler, @Nullable MediaSourceEventListener eventListener) {
isCreateCalled = true; isCreateCalled = true;
if (extractorsFactory == null) { if (extractorsFactory == null) {

View File

@ -15,13 +15,11 @@
*/ */
package com.google.android.exoplayer2.source; package com.google.android.exoplayer2.source;
import android.net.Uri;
import android.os.Handler;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.FormatHolder; import com.google.android.exoplayer2.FormatHolder;
import com.google.android.exoplayer2.decoder.DecoderInputBuffer; import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
import com.google.android.exoplayer2.source.SingleSampleMediaSource.EventListener; import com.google.android.exoplayer2.source.MediaSourceEventListener.EventDispatcher;
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;
@ -43,14 +41,14 @@ import java.util.Arrays;
*/ */
private static final int INITIAL_SAMPLE_SIZE = 1024; private static final int INITIAL_SAMPLE_SIZE = 1024;
private final Uri uri; private final DataSpec dataSpec;
private final DataSource.Factory dataSourceFactory; private final DataSource.Factory dataSourceFactory;
private final int minLoadableRetryCount; private final int minLoadableRetryCount;
private final Handler eventHandler; private final EventDispatcher eventDispatcher;
private final EventListener eventListener;
private final int eventSourceId;
private final TrackGroupArray tracks; private final TrackGroupArray tracks;
private final ArrayList<SampleStreamImpl> sampleStreams; private final ArrayList<SampleStreamImpl> sampleStreams;
private final long durationUs;
// Package private to avoid thunk methods. // Package private to avoid thunk methods.
/* package */ final Loader loader; /* package */ final Loader loader;
/* package */ final Format format; /* package */ final Format format;
@ -62,16 +60,20 @@ import java.util.Arrays;
/* package */ int sampleSize; /* package */ int sampleSize;
private int errorCount; private int errorCount;
public SingleSampleMediaPeriod(Uri uri, DataSource.Factory dataSourceFactory, Format format, public SingleSampleMediaPeriod(
int minLoadableRetryCount, Handler eventHandler, EventListener eventListener, DataSpec dataSpec,
int eventSourceId, boolean treatLoadErrorsAsEndOfStream) { DataSource.Factory dataSourceFactory,
this.uri = uri; Format format,
long durationUs,
int minLoadableRetryCount,
EventDispatcher eventDispatcher,
boolean treatLoadErrorsAsEndOfStream) {
this.dataSpec = dataSpec;
this.dataSourceFactory = dataSourceFactory; this.dataSourceFactory = dataSourceFactory;
this.format = format; this.format = format;
this.durationUs = durationUs;
this.minLoadableRetryCount = minLoadableRetryCount; this.minLoadableRetryCount = minLoadableRetryCount;
this.eventHandler = eventHandler; this.eventDispatcher = eventDispatcher;
this.eventListener = eventListener;
this.eventSourceId = eventSourceId;
this.treatLoadErrorsAsEndOfStream = treatLoadErrorsAsEndOfStream; this.treatLoadErrorsAsEndOfStream = treatLoadErrorsAsEndOfStream;
tracks = new TrackGroupArray(new TrackGroup(format)); tracks = new TrackGroupArray(new TrackGroup(format));
sampleStreams = new ArrayList<>(); sampleStreams = new ArrayList<>();
@ -125,7 +127,9 @@ import java.util.Arrays;
if (loadingFinished || loader.isLoading()) { if (loadingFinished || loader.isLoading()) {
return false; return false;
} }
loader.startLoading(new SourceLoadable(uri, dataSourceFactory.createDataSource()), this, loader.startLoading(
new SourceLoadable(dataSpec, dataSourceFactory.createDataSource()),
this,
minLoadableRetryCount); minLoadableRetryCount);
return true; return true;
} }
@ -158,6 +162,18 @@ import java.util.Arrays;
@Override @Override
public void onLoadCompleted(SourceLoadable loadable, long elapsedRealtimeMs, public void onLoadCompleted(SourceLoadable loadable, long elapsedRealtimeMs,
long loadDurationMs) { long loadDurationMs) {
eventDispatcher.loadCompleted(
loadable.dataSpec,
C.DATA_TYPE_MEDIA,
C.TRACK_TYPE_UNKNOWN,
format,
C.SELECTION_REASON_UNKNOWN,
/* trackSelectionData= */ null,
/* mediaStartTimeUs= */ 0,
durationUs,
elapsedRealtimeMs,
loadDurationMs,
loadable.sampleSize);
sampleSize = loadable.sampleSize; sampleSize = loadable.sampleSize;
sampleData = loadable.sampleData; sampleData = loadable.sampleData;
loadingFinished = true; loadingFinished = true;
@ -167,34 +183,46 @@ import java.util.Arrays;
@Override @Override
public void onLoadCanceled(SourceLoadable loadable, long elapsedRealtimeMs, long loadDurationMs, public void onLoadCanceled(SourceLoadable loadable, long elapsedRealtimeMs, long loadDurationMs,
boolean released) { boolean released) {
// Do nothing. eventDispatcher.loadCanceled(
loadable.dataSpec,
C.DATA_TYPE_MEDIA,
C.TRACK_TYPE_UNKNOWN,
/* trackFormat= */ null,
C.SELECTION_REASON_UNKNOWN,
/* trackSelectionData= */ null,
/* mediaStartTimeUs= */ 0,
durationUs,
elapsedRealtimeMs,
loadDurationMs,
loadable.sampleSize);
} }
@Override @Override
public int onLoadError(SourceLoadable loadable, long elapsedRealtimeMs, long loadDurationMs, public int onLoadError(SourceLoadable loadable, long elapsedRealtimeMs, long loadDurationMs,
IOException error) { IOException error) {
notifyLoadError(error);
errorCount++; errorCount++;
if (treatLoadErrorsAsEndOfStream && errorCount >= minLoadableRetryCount) { boolean cancel = treatLoadErrorsAsEndOfStream && errorCount >= minLoadableRetryCount;
eventDispatcher.loadError(
loadable.dataSpec,
C.DATA_TYPE_MEDIA,
C.TRACK_TYPE_UNKNOWN,
format,
C.SELECTION_REASON_UNKNOWN,
/* trackSelectionData= */ null,
/* mediaStartTimeUs= */ 0,
durationUs,
elapsedRealtimeMs,
loadDurationMs,
loadable.sampleSize,
error,
/* wasCanceled= */ cancel);
if (cancel) {
loadingFinished = true; loadingFinished = true;
return Loader.DONT_RETRY; return Loader.DONT_RETRY;
} }
return Loader.RETRY; return Loader.RETRY;
} }
// Internal methods.
private void notifyLoadError(final IOException e) {
if (eventHandler != null && eventListener != null) {
eventHandler.post(new Runnable() {
@Override
public void run() {
eventListener.onLoadError(eventSourceId, e);
}
});
}
}
private final class SampleStreamImpl implements SampleStream { private final class SampleStreamImpl implements SampleStream {
private static final int STREAM_STATE_SEND_FORMAT = 0; private static final int STREAM_STATE_SEND_FORMAT = 0;
@ -259,14 +287,15 @@ import java.util.Arrays;
/* package */ static final class SourceLoadable implements Loadable { /* package */ static final class SourceLoadable implements Loadable {
private final Uri uri; public final DataSpec dataSpec;
private final DataSource dataSource; private final DataSource dataSource;
private int sampleSize; private int sampleSize;
private byte[] sampleData; private byte[] sampleData;
public SourceLoadable(Uri uri, DataSource dataSource) { public SourceLoadable(DataSpec dataSpec, DataSource dataSource) {
this.uri = uri; this.dataSpec = dataSpec;
this.dataSource = dataSource; this.dataSource = dataSource;
} }
@ -286,7 +315,7 @@ import java.util.Arrays;
sampleSize = 0; sampleSize = 0;
try { try {
// Create and open the input. // Create and open the input.
dataSource.open(new DataSpec(uri)); dataSource.open(dataSpec);
// Load the sample data. // Load the sample data.
int result = 0; int result = 0;
while (result != C.RESULT_END_OF_INPUT) { while (result != C.RESULT_END_OF_INPUT) {

View File

@ -17,11 +17,14 @@ package com.google.android.exoplayer2.source;
import android.net.Uri; import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import android.support.annotation.Nullable;
import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.source.MediaSourceEventListener.EventDispatcher;
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.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
import java.io.IOException; import java.io.IOException;
@ -32,7 +35,10 @@ public final class SingleSampleMediaSource implements MediaSource {
/** /**
* Listener of {@link SingleSampleMediaSource} events. * Listener of {@link SingleSampleMediaSource} events.
*
* @deprecated Use {@link MediaSourceEventListener}.
*/ */
@Deprecated
public interface EventListener { public interface EventListener {
/** /**
@ -45,35 +51,23 @@ public final class SingleSampleMediaSource implements MediaSource {
} }
/** /** Factory for {@link SingleSampleMediaSource}. */
* Builder for {@link SingleSampleMediaSource}. Each builder instance can only be used once. public static final class Factory {
*/
public static final class Builder {
private final Uri uri;
private final DataSource.Factory dataSourceFactory; private final DataSource.Factory dataSourceFactory;
private final Format format;
private final long durationUs;
private int minLoadableRetryCount; private int minLoadableRetryCount;
private Handler eventHandler;
private EventListener eventListener;
private int eventSourceId;
private boolean treatLoadErrorsAsEndOfStream; private boolean treatLoadErrorsAsEndOfStream;
private boolean isBuildCalled; private boolean isCreateCalled;
/** /**
* @param uri The {@link Uri} of the media stream. * Creates a factory for {@link SingleSampleMediaSource}s.
*
* @param dataSourceFactory The factory from which the {@link DataSource} to read the media will * @param dataSourceFactory The factory from which the {@link DataSource} to read the media will
* be obtained. * be obtained.
* @param format The {@link Format} associated with the output track.
* @param durationUs The duration of the media stream in microseconds.
*/ */
public Builder(Uri uri, DataSource.Factory dataSourceFactory, Format format, long durationUs) { public Factory(DataSource.Factory dataSourceFactory) {
this.uri = uri; this.dataSourceFactory = Assertions.checkNotNull(dataSourceFactory);
this.dataSourceFactory = dataSourceFactory;
this.format = format;
this.durationUs = durationUs;
this.minLoadableRetryCount = DEFAULT_MIN_LOADABLE_RETRY_COUNT; this.minLoadableRetryCount = DEFAULT_MIN_LOADABLE_RETRY_COUNT;
} }
@ -82,37 +76,15 @@ public final class SingleSampleMediaSource implements MediaSource {
* {@link #DEFAULT_MIN_LOADABLE_RETRY_COUNT}. * {@link #DEFAULT_MIN_LOADABLE_RETRY_COUNT}.
* *
* @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 builder. * @return This factory, for convenience.
* @throws IllegalStateException If one of the {@code create} methods has already been called.
*/ */
public Builder setMinLoadableRetryCount(int minLoadableRetryCount) { public Factory setMinLoadableRetryCount(int minLoadableRetryCount) {
Assertions.checkState(!isCreateCalled);
this.minLoadableRetryCount = minLoadableRetryCount; this.minLoadableRetryCount = minLoadableRetryCount;
return this; return this;
} }
/**
* Sets the listener to respond to events and the handler to deliver these events.
*
* @param eventHandler A handler for events.
* @param eventListener A listener of events.
* @return This builder.
*/
public Builder setEventListener(Handler eventHandler, EventListener eventListener) {
this.eventHandler = eventHandler;
this.eventListener = eventListener;
return this;
}
/**
* Sets an identifier that gets passed to {@code eventListener} methods. The default value is 0.
*
* @param eventSourceId An identifier that gets passed to {@code eventListener} methods.
* @return This builder.
*/
public Builder setEventSourceId(int eventSourceId) {
this.eventSourceId = eventSourceId;
return this;
}
/** /**
* Sets whether load errors will be treated as end-of-stream signal (load errors will not be * Sets whether load errors will be treated as end-of-stream signal (load errors will not be
* propagated). The default value is false. * propagated). The default value is false.
@ -120,27 +92,53 @@ public final class SingleSampleMediaSource implements MediaSource {
* @param treatLoadErrorsAsEndOfStream If true, load errors will not be propagated by sample * @param treatLoadErrorsAsEndOfStream If true, load errors will not be propagated by sample
* streams, treating them as ended instead. If false, load errors will be propagated * streams, treating them as ended instead. If false, load errors will be propagated
* normally by {@link SampleStream#maybeThrowError()}. * normally by {@link SampleStream#maybeThrowError()}.
* @return This builder. * @return This factory, for convenience.
* @throws IllegalStateException If one of the {@code create} methods has already been called.
*/ */
public Builder setTreatLoadErrorsAsEndOfStream(boolean treatLoadErrorsAsEndOfStream) { public Factory setTreatLoadErrorsAsEndOfStream(boolean treatLoadErrorsAsEndOfStream) {
Assertions.checkState(!isCreateCalled);
this.treatLoadErrorsAsEndOfStream = treatLoadErrorsAsEndOfStream; this.treatLoadErrorsAsEndOfStream = treatLoadErrorsAsEndOfStream;
return this; return this;
} }
/** /**
* Builds a new {@link SingleSampleMediaSource} using the current parameters. * Returns a new {@link ExtractorMediaSource} using the current parameters. Media source events
* <p> * will not be delivered.
* After this call, the builder should not be re-used.
* *
* @param uri The {@link Uri}.
* @param format The {@link Format} of the media stream.
* @param durationUs The duration of the media stream in microseconds.
* @return The new {@link ExtractorMediaSource}.
*/
public SingleSampleMediaSource createMediaSource(Uri uri, Format format, long durationUs) {
return createMediaSource(uri, format, durationUs, null, null);
}
/**
* Returns a new {@link SingleSampleMediaSource} using the current parameters.
*
* @param uri The {@link Uri}.
* @param format The {@link Format} of the media stream.
* @param durationUs The duration of the media stream in microseconds.
* @param eventHandler A handler for events.
* @param eventListener A listener of events., Format format, long durationUs
* @return The newly built {@link SingleSampleMediaSource}. * @return The newly built {@link SingleSampleMediaSource}.
*/ */
public SingleSampleMediaSource build() { public SingleSampleMediaSource createMediaSource(
Assertions.checkArgument((eventListener == null) == (eventHandler == null)); Uri uri,
Assertions.checkState(!isBuildCalled); Format format,
isBuildCalled = true; long durationUs,
@Nullable Handler eventHandler,
return new SingleSampleMediaSource(uri, dataSourceFactory, format, durationUs, @Nullable MediaSourceEventListener eventListener) {
minLoadableRetryCount, eventHandler, eventListener, eventSourceId, isCreateCalled = true;
return new SingleSampleMediaSource(
uri,
dataSourceFactory,
format,
durationUs,
minLoadableRetryCount,
eventHandler,
eventListener,
treatLoadErrorsAsEndOfStream); treatLoadErrorsAsEndOfStream);
} }
@ -151,13 +149,12 @@ public final class SingleSampleMediaSource implements MediaSource {
*/ */
public static final int DEFAULT_MIN_LOADABLE_RETRY_COUNT = 3; public static final int DEFAULT_MIN_LOADABLE_RETRY_COUNT = 3;
private final Uri uri; private final DataSpec dataSpec;
private final DataSource.Factory dataSourceFactory; private final DataSource.Factory dataSourceFactory;
private final Format format; private final Format format;
private final long durationUs;
private final MediaSourceEventListener.EventDispatcher eventDispatcher;
private final int minLoadableRetryCount; private final int minLoadableRetryCount;
private final Handler eventHandler;
private final EventListener eventListener;
private final int eventSourceId;
private final boolean treatLoadErrorsAsEndOfStream; private final boolean treatLoadErrorsAsEndOfStream;
private final Timeline timeline; private final Timeline timeline;
@ -167,11 +164,11 @@ public final class SingleSampleMediaSource implements MediaSource {
* be obtained. * be obtained.
* @param format The {@link Format} associated with the output track. * @param format The {@link Format} associated with the output track.
* @param durationUs The duration of the media stream in microseconds. * @param durationUs The duration of the media stream in microseconds.
* @deprecated Use {@link Builder} instead. * @deprecated Use {@link Factory} instead.
*/ */
@Deprecated @Deprecated
public SingleSampleMediaSource(Uri uri, DataSource.Factory dataSourceFactory, Format format, public SingleSampleMediaSource(
long durationUs) { Uri uri, DataSource.Factory dataSourceFactory, Format format, long durationUs) {
this(uri, dataSourceFactory, format, durationUs, DEFAULT_MIN_LOADABLE_RETRY_COUNT); this(uri, dataSourceFactory, format, durationUs, DEFAULT_MIN_LOADABLE_RETRY_COUNT);
} }
@ -182,12 +179,16 @@ public final class SingleSampleMediaSource implements MediaSource {
* @param format The {@link Format} associated with the output track. * @param format The {@link Format} associated with the output track.
* @param durationUs The duration of the media stream in microseconds. * @param durationUs The duration of the media stream in microseconds.
* @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.
* @deprecated Use {@link Builder} instead. * @deprecated Use {@link Factory} instead.
*/ */
@Deprecated @Deprecated
public SingleSampleMediaSource(Uri uri, DataSource.Factory dataSourceFactory, Format format, public SingleSampleMediaSource(
long durationUs, int minLoadableRetryCount) { Uri uri,
this(uri, dataSourceFactory, format, durationUs, minLoadableRetryCount, null, null, 0, false); DataSource.Factory dataSourceFactory,
Format format,
long durationUs,
int minLoadableRetryCount) {
this(uri, dataSourceFactory, format, durationUs, minLoadableRetryCount, null, null, false);
} }
/** /**
@ -203,20 +204,46 @@ public final class SingleSampleMediaSource implements MediaSource {
* @param treatLoadErrorsAsEndOfStream If true, load errors will not be propagated by sample * @param treatLoadErrorsAsEndOfStream If true, load errors will not be propagated by sample
* streams, treating them as ended instead. If false, load errors will be propagated normally * streams, treating them as ended instead. If false, load errors will be propagated normally
* by {@link SampleStream#maybeThrowError()}. * by {@link SampleStream#maybeThrowError()}.
* @deprecated Use {@link Builder} instead. * @deprecated Use {@link Factory} instead.
*/ */
@Deprecated @Deprecated
public SingleSampleMediaSource(Uri uri, DataSource.Factory dataSourceFactory, Format format, public SingleSampleMediaSource(
long durationUs, int minLoadableRetryCount, Handler eventHandler, EventListener eventListener, Uri uri,
int eventSourceId, boolean treatLoadErrorsAsEndOfStream) { DataSource.Factory dataSourceFactory,
this.uri = uri; Format format,
long durationUs,
int minLoadableRetryCount,
Handler eventHandler,
EventListener eventListener,
int eventSourceId,
boolean treatLoadErrorsAsEndOfStream) {
this(
uri,
dataSourceFactory,
format,
durationUs,
minLoadableRetryCount,
eventHandler,
eventListener == null ? null : new EventListenerWrapper(eventListener, eventSourceId),
treatLoadErrorsAsEndOfStream);
}
private SingleSampleMediaSource(
Uri uri,
DataSource.Factory dataSourceFactory,
Format format,
long durationUs,
int minLoadableRetryCount,
Handler eventHandler,
MediaSourceEventListener eventListener,
boolean treatLoadErrorsAsEndOfStream) {
this.dataSourceFactory = dataSourceFactory; this.dataSourceFactory = dataSourceFactory;
this.format = format; this.format = format;
this.durationUs = durationUs;
this.minLoadableRetryCount = minLoadableRetryCount; this.minLoadableRetryCount = minLoadableRetryCount;
this.eventHandler = eventHandler;
this.eventListener = eventListener;
this.eventSourceId = eventSourceId;
this.treatLoadErrorsAsEndOfStream = treatLoadErrorsAsEndOfStream; this.treatLoadErrorsAsEndOfStream = treatLoadErrorsAsEndOfStream;
this.eventDispatcher = new EventDispatcher(eventHandler, eventListener);
dataSpec = new DataSpec(uri);
timeline = new SinglePeriodTimeline(durationUs, true); timeline = new SinglePeriodTimeline(durationUs, true);
} }
@ -235,8 +262,14 @@ public final class SingleSampleMediaSource implements MediaSource {
@Override @Override
public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator) { public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator) {
Assertions.checkArgument(id.periodIndex == 0); Assertions.checkArgument(id.periodIndex == 0);
return new SingleSampleMediaPeriod(uri, dataSourceFactory, format, minLoadableRetryCount, return new SingleSampleMediaPeriod(
eventHandler, eventListener, eventSourceId, treatLoadErrorsAsEndOfStream); dataSpec,
dataSourceFactory,
format,
durationUs,
minLoadableRetryCount,
eventDispatcher,
treatLoadErrorsAsEndOfStream);
} }
@Override @Override
@ -249,4 +282,97 @@ public final class SingleSampleMediaSource implements MediaSource {
// Do nothing. // Do nothing.
} }
/**
* Wraps a deprecated {@link EventListener}, invoking its callback from the equivalent callback in
* {@link MediaSourceEventListener}.
*/
private static final class EventListenerWrapper implements MediaSourceEventListener {
private final EventListener eventListener;
private final int eventSourceId;
public EventListenerWrapper(EventListener eventListener, int eventSourceId) {
this.eventListener = Assertions.checkNotNull(eventListener);
this.eventSourceId = eventSourceId;
}
@Override
public void onLoadStarted(
DataSpec dataSpec,
int dataType,
int trackType,
Format trackFormat,
int trackSelectionReason,
Object trackSelectionData,
long mediaStartTimeMs,
long mediaEndTimeMs,
long elapsedRealtimeMs) {
// Do nothing.
}
@Override
public void onLoadCompleted(
DataSpec dataSpec,
int dataType,
int trackType,
Format trackFormat,
int trackSelectionReason,
Object trackSelectionData,
long mediaStartTimeMs,
long mediaEndTimeMs,
long elapsedRealtimeMs,
long loadDurationMs,
long bytesLoaded) {
// Do nothing.
}
@Override
public void onLoadCanceled(
DataSpec dataSpec,
int dataType,
int trackType,
Format trackFormat,
int trackSelectionReason,
Object trackSelectionData,
long mediaStartTimeMs,
long mediaEndTimeMs,
long elapsedRealtimeMs,
long loadDurationMs,
long bytesLoaded) {
// Do nothing.
}
@Override
public void onLoadError(
DataSpec dataSpec,
int dataType,
int trackType,
Format trackFormat,
int trackSelectionReason,
Object trackSelectionData,
long mediaStartTimeMs,
long mediaEndTimeMs,
long elapsedRealtimeMs,
long loadDurationMs,
long bytesLoaded,
IOException error,
boolean wasCanceled) {
eventListener.onLoadError(eventSourceId, error);
}
@Override
public void onUpstreamDiscarded(int trackType, long mediaStartTimeMs, long mediaEndTimeMs) {
// Do nothing.
}
@Override
public void onDownstreamFormatChanged(
int trackType,
Format trackFormat,
int trackSelectionReason,
Object trackSelectionData,
long mediaTimeMs) {
// Do nothing.
}
}
} }

View File

@ -131,7 +131,7 @@ public final class HlsMediaSource implements MediaSource,
* *
* @return The new {@link HlsMediaSource}. * @return The new {@link HlsMediaSource}.
*/ */
public MediaSource createMediaSource(Uri playlistUri) { public HlsMediaSource createMediaSource(Uri playlistUri) {
return createMediaSource(playlistUri, null, null); return createMediaSource(playlistUri, null, null);
} }
@ -144,7 +144,7 @@ public final class HlsMediaSource implements MediaSource,
* @return The new {@link HlsMediaSource}. * @return The new {@link HlsMediaSource}.
*/ */
@Override @Override
public MediaSource createMediaSource( public HlsMediaSource createMediaSource(
Uri playlistUri, Uri playlistUri,
@Nullable Handler eventHandler, @Nullable Handler eventHandler,
@Nullable MediaSourceEventListener eventListener) { @Nullable MediaSourceEventListener eventListener) {