Commit 11e2e0e6 by andrewlewis Committed by Oliver Woodman

Release Extractors on the loader's thread.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=127294338
parent 62ebcaf8
...@@ -120,8 +120,8 @@ public final class ExtractorMediaSource implements MediaPeriod, MediaSource, ...@@ -120,8 +120,8 @@ public final class ExtractorMediaSource implements MediaPeriod, MediaSource,
private final EventListener eventListener; private final EventListener eventListener;
private DataSource dataSource; private DataSource dataSource;
private ExtractorHolder extractorHolder;
private Loader loader; private Loader loader;
private ExtractorHolder extractorHolder;
private ConditionVariable loadCondition; private ConditionVariable loadCondition;
private Callback callback; private Callback callback;
...@@ -205,8 +205,8 @@ public final class ExtractorMediaSource implements MediaPeriod, MediaSource, ...@@ -205,8 +205,8 @@ public final class ExtractorMediaSource implements MediaPeriod, MediaSource,
this.allocator = allocator; this.allocator = allocator;
dataSource = dataSourceFactory.createDataSource(bandwidthMeter); dataSource = dataSourceFactory.createDataSource(bandwidthMeter);
loader = new Loader("Loader:ExtractorMediaSource");
extractorHolder = new ExtractorHolder(extractorsFactory.createExtractors(), this); extractorHolder = new ExtractorHolder(extractorsFactory.createExtractors(), this);
loader = new Loader("Loader:ExtractorMediaSource", extractorHolder);
loadCondition = new ConditionVariable(); loadCondition = new ConditionVariable();
pendingResetPositionUs = C.UNSET_TIME_US; pendingResetPositionUs = C.UNSET_TIME_US;
sampleQueues = new DefaultTrackOutput[0]; sampleQueues = new DefaultTrackOutput[0];
...@@ -347,11 +347,17 @@ public final class ExtractorMediaSource implements MediaPeriod, MediaSource, ...@@ -347,11 +347,17 @@ public final class ExtractorMediaSource implements MediaPeriod, MediaSource,
@Override @Override
public void release() { public void release() {
dataSource = null; dataSource = null;
extractorHolder = null;
if (loader != null) { if (loader != null) {
loader.release(); // Releases extractorHolder via its own reference on the loader's thread. final ExtractorHolder extractorHolder = this.extractorHolder;
loader.release(new Runnable() {
@Override
public void run() {
extractorHolder.release();
}
});
loader = null; loader = null;
} }
extractorHolder = null;
loadCondition = null; loadCondition = null;
callback = null; callback = null;
allocator = null; allocator = null;
...@@ -693,7 +699,7 @@ public final class ExtractorMediaSource implements MediaPeriod, MediaSource, ...@@ -693,7 +699,7 @@ public final class ExtractorMediaSource implements MediaPeriod, MediaSource,
/** /**
* Stores a list of extractors and a selected extractor when the format has been detected. * Stores a list of extractors and a selected extractor when the format has been detected.
*/ */
private static final class ExtractorHolder implements Loader.Releasable { private static final class ExtractorHolder {
private final Extractor[] extractors; private final Extractor[] extractors;
private final ExtractorOutput extractorOutput; private final ExtractorOutput extractorOutput;
...@@ -744,7 +750,6 @@ public final class ExtractorMediaSource implements MediaPeriod, MediaSource, ...@@ -744,7 +750,6 @@ public final class ExtractorMediaSource implements MediaPeriod, MediaSource,
return extractor; return extractor;
} }
@Override
public void release() { public void release() {
if (extractor != null) { if (extractor != null) {
extractor.release(); extractor.release();
......
...@@ -73,18 +73,6 @@ public final class Loader { ...@@ -73,18 +73,6 @@ public final class Loader {
} }
/** /**
* An object that can be released on the loading thread when {@link Loader#release()} is called.
*/
public interface Releasable {
/**
* Releases any resources associated with the instance.
*/
void release();
}
/**
* A callback to be notified of {@link Loader} events. * A callback to be notified of {@link Loader} events.
*/ */
public interface Callback<T extends Loadable> { public interface Callback<T extends Loadable> {
...@@ -147,7 +135,6 @@ public final class Loader { ...@@ -147,7 +135,6 @@ public final class Loader {
private static final int MSG_FATAL_ERROR = 4; private static final int MSG_FATAL_ERROR = 4;
private final ExecutorService downloadExecutorService; private final ExecutorService downloadExecutorService;
private final Releasable releasable;
private LoadTask<? extends Loadable> currentTask; private LoadTask<? extends Loadable> currentTask;
private IOException fatalError; private IOException fatalError;
...@@ -156,17 +143,7 @@ public final class Loader { ...@@ -156,17 +143,7 @@ public final class Loader {
* @param threadName A name for the loader's thread. * @param threadName A name for the loader's thread.
*/ */
public Loader(String threadName) { public Loader(String threadName) {
this(threadName, null);
}
/**
* @param threadName A name for the loader's thread.
* @param releasable An object to release on the loader's thread when {@link Loader#release()} is
* called.
*/
public Loader(String threadName, Releasable releasable) {
this.downloadExecutorService = Util.newSingleThreadExecutor(threadName); this.downloadExecutorService = Util.newSingleThreadExecutor(threadName);
this.releasable = releasable;
} }
/** /**
...@@ -243,16 +220,23 @@ public final class Loader { ...@@ -243,16 +220,23 @@ public final class Loader {
* This method should be called when the {@link Loader} is no longer required. * This method should be called when the {@link Loader} is no longer required.
*/ */
public void release() { public void release() {
release(null);
}
/**
* Releases the {@link Loader}, running {@code postLoadAction} on its thread.
* <p>
* This method should be called when the {@link Loader} is no longer required.
*
* @param postLoadAction A {@link Runnable} to run on the loader's thread when
* {@link Loadable#load()} is no longer running.
*/
public void release(Runnable postLoadAction) {
if (currentTask != null) { if (currentTask != null) {
currentTask.cancel(true); currentTask.cancel(true);
} }
if (releasable != null) { if (postLoadAction != null) {
downloadExecutorService.submit(new Runnable() { downloadExecutorService.submit(postLoadAction);
@Override
public void run() {
releasable.release();
}
});
} }
downloadExecutorService.shutdown(); downloadExecutorService.shutdown();
} }
......
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