From bbceb459fdb79ebc2b736834f222b4891cd73439 Mon Sep 17 00:00:00 2001 From: olly Date: Tue, 15 Mar 2016 07:25:20 -0700 Subject: [PATCH] Add loading error notification to ExtractorSampleSource ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=117237109 --- .../extractor/ExtractorSampleSource.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/library/src/main/java/com/google/android/exoplayer/extractor/ExtractorSampleSource.java b/library/src/main/java/com/google/android/exoplayer/extractor/ExtractorSampleSource.java index a0dab7d60d..db13fbacb4 100644 --- a/library/src/main/java/com/google/android/exoplayer/extractor/ExtractorSampleSource.java +++ b/library/src/main/java/com/google/android/exoplayer/extractor/ExtractorSampleSource.java @@ -34,6 +34,7 @@ import com.google.android.exoplayer.util.Assertions; import com.google.android.exoplayer.util.Util; import android.net.Uri; +import android.os.Handler; import android.os.SystemClock; import android.util.SparseArray; @@ -70,6 +71,21 @@ import java.util.List; */ public final class ExtractorSampleSource implements SampleSource, ExtractorOutput, Loader.Callback { + /** + * Interface definition for a callback to be notified of {@link ExtractorSampleSource} events. + */ + public interface EventListener { + + /** + * Invoked when an error occurs loading media data. + * + * @param sourceId The id of the reporting {@link SampleSource}. + * @param e The cause of the failure. + */ + void onLoadError(int sourceId, IOException e); + + } + /** * Thrown if the input format could not recognized. */ @@ -177,6 +193,9 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu private final int minLoadableRetryCount; private final Uri uri; private final DataSource dataSource; + private final Handler eventHandler; + private final EventListener eventListener; + private final int eventSourceId; private volatile boolean tracksBuilt; private volatile SeekMap seekMap; @@ -224,6 +243,26 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu extractors); } + /** + * @param uri The {@link Uri} of the media stream. + * @param dataSource A data source to read the media stream. + * @param allocator An {@link Allocator} from which to obtain memory allocations. + * @param requestedBufferSize The requested total buffer size for storing sample data, in bytes. + * The actual allocated size may exceed the value passed in if the implementation requires it. + * @param eventHandler A handler to use when delivering events to {@code eventListener}. May be + * null if delivery of events is not required. + * @param eventListener A listener of events. May be null if delivery of events is not required. + * @param eventSourceId An identifier that gets passed to {@code eventListener} methods. + * @param extractors {@link Extractor}s to extract the media stream, in order of decreasing + * priority. If omitted, the default extractors will be used. + */ + public ExtractorSampleSource(Uri uri, DataSource dataSource, Allocator allocator, + int requestedBufferSize, Handler eventHandler, EventListener eventListener, + int eventSourceId, Extractor... extractors) { + this(uri, dataSource, allocator, requestedBufferSize, MIN_RETRY_COUNT_DEFAULT_FOR_MEDIA, + eventHandler, eventListener, eventSourceId, extractors); + } + /** * @param uri The {@link Uri} of the media stream. * @param dataSource A data source to read the media stream. @@ -237,8 +276,33 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu */ public ExtractorSampleSource(Uri uri, DataSource dataSource, Allocator allocator, int requestedBufferSize, int minLoadableRetryCount, Extractor... extractors) { + this(uri, dataSource, allocator, requestedBufferSize, minLoadableRetryCount, null, null, 0, + extractors); + } + + /** + * @param uri The {@link Uri} of the media stream. + * @param dataSource A data source to read the media stream. + * @param allocator An {@link Allocator} from which to obtain memory allocations. + * @param requestedBufferSize The requested total buffer size for storing sample data, in bytes. + * The actual allocated size may exceed the value passed in if the implementation requires it. + * @param minLoadableRetryCount The minimum number of times that the sample source will retry + * if a loading error occurs. + * @param eventHandler A handler to use when delivering events to {@code eventListener}. May be + * null if delivery of events is not required. + * @param eventListener A listener of events. May be null if delivery of events is not required. + * @param eventSourceId An identifier that gets passed to {@code eventListener} methods. + * @param extractors {@link Extractor}s to extract the media stream, in order of decreasing + * priority. If omitted, the default extractors will be used. + */ + public ExtractorSampleSource(Uri uri, DataSource dataSource, Allocator allocator, + int requestedBufferSize, int minLoadableRetryCount, Handler eventHandler, + EventListener eventListener, int eventSourceId, Extractor... extractors) { this.uri = uri; this.dataSource = dataSource; + this.eventListener = eventListener; + this.eventHandler = eventHandler; + this.eventSourceId = eventSourceId; this.allocator = allocator; this.requestedBufferSize = requestedBufferSize; this.minLoadableRetryCount = minLoadableRetryCount; @@ -493,6 +557,7 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu currentLoadableExceptionCount = extractedSampleCount > extractedSampleCountAtStartOfLoad ? 1 : currentLoadableExceptionCount + 1; currentLoadableExceptionTimestamp = SystemClock.elapsedRealtime(); + notifyLoadError(e); maybeStartLoading(); } @@ -650,6 +715,17 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu return Math.min((errorCount - 1) * 1000, 5000); } + 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 TrackStreamImpl implements TrackStream { private final int track;