Allow customization of ExtractorMediaSource's check interval

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=158683582
This commit is contained in:
andrewlewis 2017-06-12 00:51:19 -07:00 committed by Oliver Woodman
parent 646047f088
commit cb5b6fba01
2 changed files with 20 additions and 13 deletions

View File

@ -64,6 +64,7 @@ import java.io.IOException;
private final MediaSource.Listener sourceListener; private final MediaSource.Listener sourceListener;
private final Allocator allocator; private final Allocator allocator;
private final String customCacheKey; private final String customCacheKey;
private final long continueLoadingCheckIntervalBytes;
private final Loader loader; private final Loader loader;
private final ExtractorHolder extractorHolder; private final ExtractorHolder extractorHolder;
private final ConditionVariable loadCondition; private final ConditionVariable loadCondition;
@ -105,11 +106,13 @@ import java.io.IOException;
* @param allocator An {@link Allocator} from which to obtain media buffer allocations. * @param allocator An {@link Allocator} from which to obtain media buffer allocations.
* @param customCacheKey A custom key that uniquely identifies the original stream. Used for cache * @param customCacheKey A custom key that uniquely identifies the original stream. Used for cache
* indexing. May be null. * indexing. May be null.
* @param continueLoadingCheckIntervalBytes The number of bytes that should be loaded between each
* invocation of {@link Callback#onContinueLoadingRequested(SequenceableLoader)}.
*/ */
public ExtractorMediaPeriod(Uri uri, DataSource dataSource, Extractor[] extractors, public ExtractorMediaPeriod(Uri uri, DataSource dataSource, Extractor[] extractors,
int minLoadableRetryCount, Handler eventHandler, int minLoadableRetryCount, Handler eventHandler,
ExtractorMediaSource.EventListener eventListener, MediaSource.Listener sourceListener, ExtractorMediaSource.EventListener eventListener, MediaSource.Listener sourceListener,
Allocator allocator, String customCacheKey) { Allocator allocator, String customCacheKey, int continueLoadingCheckIntervalBytes) {
this.uri = uri; this.uri = uri;
this.dataSource = dataSource; this.dataSource = dataSource;
this.minLoadableRetryCount = minLoadableRetryCount; this.minLoadableRetryCount = minLoadableRetryCount;
@ -118,6 +121,7 @@ import java.io.IOException;
this.sourceListener = sourceListener; this.sourceListener = sourceListener;
this.allocator = allocator; this.allocator = allocator;
this.customCacheKey = customCacheKey; this.customCacheKey = customCacheKey;
this.continueLoadingCheckIntervalBytes = continueLoadingCheckIntervalBytes;
loader = new Loader("Loader:ExtractorMediaPeriod"); loader = new Loader("Loader:ExtractorMediaPeriod");
extractorHolder = new ExtractorHolder(extractors, this); extractorHolder = new ExtractorHolder(extractors, this);
loadCondition = new ConditionVariable(); loadCondition = new ConditionVariable();
@ -585,12 +589,6 @@ import java.io.IOException;
*/ */
/* package */ final class ExtractingLoadable implements Loadable { /* package */ final class ExtractingLoadable implements Loadable {
/**
* The number of bytes that should be loaded between each each invocation of
* {@link Callback#onContinueLoadingRequested(SequenceableLoader)}.
*/
private static final int CONTINUE_LOADING_CHECK_INTERVAL_BYTES = 1024 * 1024;
private final Uri uri; private final Uri uri;
private final DataSource dataSource; private final DataSource dataSource;
private final ExtractorHolder extractorHolder; private final ExtractorHolder extractorHolder;
@ -650,7 +648,7 @@ import java.io.IOException;
while (result == Extractor.RESULT_CONTINUE && !loadCanceled) { while (result == Extractor.RESULT_CONTINUE && !loadCanceled) {
loadCondition.block(); loadCondition.block();
result = extractor.read(input, positionHolder); result = extractor.read(input, positionHolder);
if (input.getPosition() > position + CONTINUE_LOADING_CHECK_INTERVAL_BYTES) { if (input.getPosition() > position + continueLoadingCheckIntervalBytes) {
position = input.getPosition(); position = input.getPosition();
loadCondition.close(); loadCondition.close();
handler.post(onContinueLoadingRequestedRunnable); handler.post(onContinueLoadingRequestedRunnable);

View File

@ -72,6 +72,12 @@ public final class ExtractorMediaSource implements MediaSource, MediaSource.List
*/ */
public static final int MIN_RETRY_COUNT_DEFAULT_FOR_MEDIA = -1; public static final int MIN_RETRY_COUNT_DEFAULT_FOR_MEDIA = -1;
/**
* The default number of bytes that should be loaded between each each invocation of
* {@link MediaPeriod.Callback#onContinueLoadingRequested(SequenceableLoader)}.
*/
public static final int DEFAULT_LOADING_CHECK_INTERVAL_BYTES = 1024 * 1024;
private final Uri uri; private final Uri uri;
private final DataSource.Factory dataSourceFactory; private final DataSource.Factory dataSourceFactory;
private final ExtractorsFactory extractorsFactory; private final ExtractorsFactory extractorsFactory;
@ -80,6 +86,7 @@ public final class ExtractorMediaSource implements MediaSource, MediaSource.List
private final EventListener eventListener; private final EventListener eventListener;
private final Timeline.Period period; private final Timeline.Period period;
private final String customCacheKey; private final String customCacheKey;
private final int continueLoadingCheckIntervalBytes;
private MediaSource.Listener sourceListener; private MediaSource.Listener sourceListener;
private Timeline timeline; private Timeline timeline;
@ -96,8 +103,7 @@ public final class ExtractorMediaSource implements MediaSource, MediaSource.List
*/ */
public ExtractorMediaSource(Uri uri, DataSource.Factory dataSourceFactory, public ExtractorMediaSource(Uri uri, DataSource.Factory dataSourceFactory,
ExtractorsFactory extractorsFactory, Handler eventHandler, EventListener eventListener) { ExtractorsFactory extractorsFactory, Handler eventHandler, EventListener eventListener) {
this(uri, dataSourceFactory, extractorsFactory, MIN_RETRY_COUNT_DEFAULT_FOR_MEDIA, eventHandler, this(uri, dataSourceFactory, extractorsFactory, eventHandler, eventListener, null);
eventListener, null);
} }
/** /**
@ -115,7 +121,7 @@ public final class ExtractorMediaSource implements MediaSource, MediaSource.List
ExtractorsFactory extractorsFactory, Handler eventHandler, EventListener eventListener, ExtractorsFactory extractorsFactory, Handler eventHandler, EventListener eventListener,
String customCacheKey) { String customCacheKey) {
this(uri, dataSourceFactory, extractorsFactory, MIN_RETRY_COUNT_DEFAULT_FOR_MEDIA, eventHandler, this(uri, dataSourceFactory, extractorsFactory, MIN_RETRY_COUNT_DEFAULT_FOR_MEDIA, eventHandler,
eventListener, customCacheKey); eventListener, customCacheKey, DEFAULT_LOADING_CHECK_INTERVAL_BYTES);
} }
/** /**
@ -129,10 +135,12 @@ public final class ExtractorMediaSource implements MediaSource, MediaSource.List
* @param eventListener A listener of events. 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 customCacheKey A custom key that uniquely identifies the original stream. Used for cache * @param customCacheKey A custom key that uniquely identifies the original stream. Used for cache
* indexing. May be null. * indexing. May be null.
* @param continueLoadingCheckIntervalBytes The number of bytes that should be loaded between each
* invocation of {@link MediaPeriod.Callback#onContinueLoadingRequested(SequenceableLoader)}.
*/ */
public ExtractorMediaSource(Uri uri, DataSource.Factory dataSourceFactory, public ExtractorMediaSource(Uri uri, DataSource.Factory dataSourceFactory,
ExtractorsFactory extractorsFactory, int minLoadableRetryCount, Handler eventHandler, ExtractorsFactory extractorsFactory, int minLoadableRetryCount, Handler eventHandler,
EventListener eventListener, String customCacheKey) { EventListener eventListener, String customCacheKey, int continueLoadingCheckIntervalBytes) {
this.uri = uri; this.uri = uri;
this.dataSourceFactory = dataSourceFactory; this.dataSourceFactory = dataSourceFactory;
this.extractorsFactory = extractorsFactory; this.extractorsFactory = extractorsFactory;
@ -140,6 +148,7 @@ public final class ExtractorMediaSource implements MediaSource, MediaSource.List
this.eventHandler = eventHandler; this.eventHandler = eventHandler;
this.eventListener = eventListener; this.eventListener = eventListener;
this.customCacheKey = customCacheKey; this.customCacheKey = customCacheKey;
this.continueLoadingCheckIntervalBytes = continueLoadingCheckIntervalBytes;
period = new Timeline.Period(); period = new Timeline.Period();
} }
@ -160,7 +169,7 @@ public final class ExtractorMediaSource implements MediaSource, MediaSource.List
Assertions.checkArgument(index == 0); Assertions.checkArgument(index == 0);
return new ExtractorMediaPeriod(uri, dataSourceFactory.createDataSource(), return new ExtractorMediaPeriod(uri, dataSourceFactory.createDataSource(),
extractorsFactory.createExtractors(), minLoadableRetryCount, eventHandler, eventListener, extractorsFactory.createExtractors(), minLoadableRetryCount, eventHandler, eventListener,
this, allocator, customCacheKey); this, allocator, customCacheKey, continueLoadingCheckIntervalBytes);
} }
@Override @Override