Skip to content

Commit

Permalink
Update SingleSampleMediaSource with factory/listener changes
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
ojw28 committed Dec 15, 2017
1 parent 8a6c375 commit 67b94a7
Show file tree
Hide file tree
Showing 5 changed files with 280 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,6 @@ public void onLoadCompleted(ExtractingLoadable loadable, long elapsedRealtimeMs,
@Override
public void onLoadCanceled(ExtractingLoadable loadable, long elapsedRealtimeMs,
long loadDurationMs, boolean released) {
if (released) {
return;
}
eventDispatcher.loadCanceled(
loadable.dataSpec,
C.DATA_TYPE_MEDIA,
Expand All @@ -446,12 +443,14 @@ public void onLoadCanceled(ExtractingLoadable loadable, long elapsedRealtimeMs,
elapsedRealtimeMs,
loadDurationMs,
loadable.bytesLoaded);
copyLengthFromLoader(loadable);
for (SampleQueue sampleQueue : sampleQueues) {
sampleQueue.reset();
}
if (enabledTrackCount > 0) {
callback.onContinueLoadingRequested(this);
if (!released) {
copyLengthFromLoader(loadable);
for (SampleQueue sampleQueue : sampleQueues) {
sampleQueue.reset();
}
if (enabledTrackCount > 0) {
callback.onContinueLoadingRequested(this);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public Factory setContinueLoadingCheckIntervalBytes(int continueLoadingCheckInte
* @param uri The {@link Uri}.
* @return The new {@link ExtractorMediaSource}.
*/
public MediaSource createMediaSource(Uri uri) {
public ExtractorMediaSource createMediaSource(Uri uri) {
return createMediaSource(uri, null, null);
}

Expand All @@ -208,7 +208,7 @@ public MediaSource createMediaSource(Uri uri) {
* @return The new {@link ExtractorMediaSource}.
*/
@Override
public MediaSource createMediaSource(
public ExtractorMediaSource createMediaSource(
Uri uri, @Nullable Handler eventHandler, @Nullable MediaSourceEventListener eventListener) {
isCreateCalled = true;
if (extractorsFactory == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
*/
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.Format;
import com.google.android.exoplayer2.FormatHolder;
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.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSpec;
Expand All @@ -43,14 +41,14 @@
*/
private static final int INITIAL_SAMPLE_SIZE = 1024;

private final Uri uri;
private final DataSpec dataSpec;
private final DataSource.Factory dataSourceFactory;
private final int minLoadableRetryCount;
private final Handler eventHandler;
private final EventListener eventListener;
private final int eventSourceId;
private final EventDispatcher eventDispatcher;
private final TrackGroupArray tracks;
private final ArrayList<SampleStreamImpl> sampleStreams;
private final long durationUs;

// Package private to avoid thunk methods.
/* package */ final Loader loader;
/* package */ final Format format;
Expand All @@ -62,16 +60,20 @@
/* package */ int sampleSize;
private int errorCount;

public SingleSampleMediaPeriod(Uri uri, DataSource.Factory dataSourceFactory, Format format,
int minLoadableRetryCount, Handler eventHandler, EventListener eventListener,
int eventSourceId, boolean treatLoadErrorsAsEndOfStream) {
this.uri = uri;
public SingleSampleMediaPeriod(
DataSpec dataSpec,
DataSource.Factory dataSourceFactory,
Format format,
long durationUs,
int minLoadableRetryCount,
EventDispatcher eventDispatcher,
boolean treatLoadErrorsAsEndOfStream) {
this.dataSpec = dataSpec;
this.dataSourceFactory = dataSourceFactory;
this.format = format;
this.durationUs = durationUs;
this.minLoadableRetryCount = minLoadableRetryCount;
this.eventHandler = eventHandler;
this.eventListener = eventListener;
this.eventSourceId = eventSourceId;
this.eventDispatcher = eventDispatcher;
this.treatLoadErrorsAsEndOfStream = treatLoadErrorsAsEndOfStream;
tracks = new TrackGroupArray(new TrackGroup(format));
sampleStreams = new ArrayList<>();
Expand Down Expand Up @@ -125,7 +127,9 @@ public boolean continueLoading(long positionUs) {
if (loadingFinished || loader.isLoading()) {
return false;
}
loader.startLoading(new SourceLoadable(uri, dataSourceFactory.createDataSource()), this,
loader.startLoading(
new SourceLoadable(dataSpec, dataSourceFactory.createDataSource()),
this,
minLoadableRetryCount);
return true;
}
Expand Down Expand Up @@ -158,6 +162,18 @@ public long seekToUs(long positionUs) {
@Override
public void onLoadCompleted(SourceLoadable loadable, long elapsedRealtimeMs,
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;
sampleData = loadable.sampleData;
loadingFinished = true;
Expand All @@ -167,34 +183,46 @@ public void onLoadCompleted(SourceLoadable loadable, long elapsedRealtimeMs,
@Override
public void onLoadCanceled(SourceLoadable loadable, long elapsedRealtimeMs, long loadDurationMs,
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
public int onLoadError(SourceLoadable loadable, long elapsedRealtimeMs, long loadDurationMs,
IOException error) {
notifyLoadError(error);
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;
return Loader.DONT_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 static final int STREAM_STATE_SEND_FORMAT = 0;
Expand Down Expand Up @@ -259,14 +287,15 @@ public int skipData(long positionUs) {

/* package */ static final class SourceLoadable implements Loadable {

private final Uri uri;
public final DataSpec dataSpec;

private final DataSource dataSource;

private int sampleSize;
private byte[] sampleData;

public SourceLoadable(Uri uri, DataSource dataSource) {
this.uri = uri;
public SourceLoadable(DataSpec dataSpec, DataSource dataSource) {
this.dataSpec = dataSpec;
this.dataSource = dataSource;
}

Expand All @@ -286,7 +315,7 @@ public void load() throws IOException, InterruptedException {
sampleSize = 0;
try {
// Create and open the input.
dataSource.open(new DataSpec(uri));
dataSource.open(dataSpec);
// Load the sample data.
int result = 0;
while (result != C.RESULT_END_OF_INPUT) {
Expand Down
Loading

0 comments on commit 67b94a7

Please sign in to comment.