Release Extractors on the loader's thread.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=127294338
This commit is contained in:
andrewlewis 2016-07-13 01:45:34 -07:00 committed by Oliver Woodman
parent 62ebcaf853
commit 11e2e0e640
2 changed files with 25 additions and 36 deletions

View file

@ -120,8 +120,8 @@ public final class ExtractorMediaSource implements MediaPeriod, MediaSource,
private final EventListener eventListener;
private DataSource dataSource;
private ExtractorHolder extractorHolder;
private Loader loader;
private ExtractorHolder extractorHolder;
private ConditionVariable loadCondition;
private Callback callback;
@ -205,8 +205,8 @@ public final class ExtractorMediaSource implements MediaPeriod, MediaSource,
this.allocator = allocator;
dataSource = dataSourceFactory.createDataSource(bandwidthMeter);
loader = new Loader("Loader:ExtractorMediaSource");
extractorHolder = new ExtractorHolder(extractorsFactory.createExtractors(), this);
loader = new Loader("Loader:ExtractorMediaSource", extractorHolder);
loadCondition = new ConditionVariable();
pendingResetPositionUs = C.UNSET_TIME_US;
sampleQueues = new DefaultTrackOutput[0];
@ -347,11 +347,17 @@ public final class ExtractorMediaSource implements MediaPeriod, MediaSource,
@Override
public void release() {
dataSource = null;
extractorHolder = 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;
}
extractorHolder = null;
loadCondition = null;
callback = null;
allocator = null;
@ -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.
*/
private static final class ExtractorHolder implements Loader.Releasable {
private static final class ExtractorHolder {
private final Extractor[] extractors;
private final ExtractorOutput extractorOutput;
@ -744,7 +750,6 @@ public final class ExtractorMediaSource implements MediaPeriod, MediaSource,
return extractor;
}
@Override
public void release() {
if (extractor != null) {
extractor.release();

View file

@ -72,18 +72,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.
*/
@ -147,7 +135,6 @@ public final class Loader {
private static final int MSG_FATAL_ERROR = 4;
private final ExecutorService downloadExecutorService;
private final Releasable releasable;
private LoadTask<? extends Loadable> currentTask;
private IOException fatalError;
@ -156,17 +143,7 @@ public final class Loader {
* @param threadName A name for the loader's thread.
*/
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.releasable = releasable;
}
/**
@ -243,16 +220,23 @@ public final class Loader {
* This method should be called when the {@link Loader} is no longer required.
*/
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) {
currentTask.cancel(true);
}
if (releasable != null) {
downloadExecutorService.submit(new Runnable() {
@Override
public void run() {
releasable.release();
}
});
if (postLoadAction != null) {
downloadExecutorService.submit(postLoadAction);
}
downloadExecutorService.shutdown();
}