Commit a5cd0b87 by olly Committed by Oliver Woodman

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
parent 106e69b3
...@@ -443,9 +443,6 @@ import java.util.Arrays; ...@@ -443,9 +443,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,
...@@ -458,12 +455,14 @@ import java.util.Arrays; ...@@ -458,12 +455,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);
}
} }
} }
......
...@@ -195,7 +195,7 @@ public final class ExtractorMediaSource implements MediaSource, ExtractorMediaPe ...@@ -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 ...@@ -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) {
......
...@@ -15,14 +15,12 @@ ...@@ -15,14 +15,12 @@
*/ */
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.SeekParameters; import com.google.android.exoplayer2.SeekParameters;
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;
...@@ -44,14 +42,14 @@ import java.util.Arrays; ...@@ -44,14 +42,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;
...@@ -63,16 +61,20 @@ import java.util.Arrays; ...@@ -63,16 +61,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<>();
...@@ -131,7 +133,9 @@ import java.util.Arrays; ...@@ -131,7 +133,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;
} }
...@@ -169,6 +173,18 @@ import java.util.Arrays; ...@@ -169,6 +173,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;
...@@ -178,34 +194,46 @@ import java.util.Arrays; ...@@ -178,34 +194,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;
...@@ -270,14 +298,15 @@ import java.util.Arrays; ...@@ -270,14 +298,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;
} }
...@@ -297,7 +326,7 @@ import java.util.Arrays; ...@@ -297,7 +326,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) {
......
...@@ -170,7 +170,7 @@ public final class HlsMediaSource implements MediaSource, ...@@ -170,7 +170,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);
} }
...@@ -183,7 +183,7 @@ public final class HlsMediaSource implements MediaSource, ...@@ -183,7 +183,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) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment