LoadControl - Start cleaning up usage somewhat.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=119758106
This commit is contained in:
olly 2016-04-13 10:12:02 -07:00 committed by Oliver Woodman
parent fec7798005
commit 3ae1c615d9
3 changed files with 69 additions and 82 deletions

View File

@ -68,7 +68,6 @@ public class ChunkSampleSource implements SampleSource, TrackStream, Loader.Call
private long pendingResetPositionUs;
private long lastPreferredQueueSizeEvaluationTimeMs;
private boolean pendingReset;
private boolean loadControlRegistered;
private TrackGroupArray trackGroups;
private long durationUs;
@ -169,6 +168,7 @@ public class ChunkSampleSource implements SampleSource, TrackStream, Loader.Call
Assertions.checkState(prepared);
Assertions.checkState(oldStreams.size() <= 1);
Assertions.checkState(newSelections.size() <= 1);
boolean trackWasEnabled = trackEnabled;
// Unselect old tracks.
if (!oldStreams.isEmpty()) {
Assertions.checkState(trackEnabled);
@ -185,9 +185,8 @@ public class ChunkSampleSource implements SampleSource, TrackStream, Loader.Call
}
// Cancel or start requests as necessary.
if (!trackEnabled) {
if (loadControlRegistered) {
if (trackWasEnabled) {
loadControl.unregister(this);
loadControlRegistered = false;
}
if (loader.isLoading()) {
loader.cancelLoading();
@ -195,10 +194,9 @@ public class ChunkSampleSource implements SampleSource, TrackStream, Loader.Call
clearState();
loadControl.trimAllocator();
}
} else if (trackEnabled) {
if (!loadControlRegistered) {
} else {
if (!trackWasEnabled) {
loadControl.register(this, bufferSizeContribution);
loadControlRegistered = true;
}
downstreamFormat = null;
downstreamSampleFormat = null;
@ -214,8 +212,10 @@ public class ChunkSampleSource implements SampleSource, TrackStream, Loader.Call
public void continueBuffering(long positionUs) {
downstreamPositionUs = positionUs;
chunkSource.continueBuffering(positionUs);
if (!loader.isLoading()) {
maybeStartLoading();
}
}
@Override
public long getBufferedPositionUs() {
@ -253,8 +253,10 @@ public class ChunkSampleSource implements SampleSource, TrackStream, Loader.Call
@Override
public void release() {
prepared = false;
if (trackEnabled) {
loadControl.unregister(this);
trackEnabled = false;
}
loader.release();
}
@ -420,10 +422,6 @@ public class ChunkSampleSource implements SampleSource, TrackStream, Loader.Call
}
private void maybeStartLoading() {
if (loader.isLoading()) {
return;
}
long now = SystemClock.elapsedRealtime();
if (now - lastPreferredQueueSizeEvaluationTimeMs > 5000) {
int queueSize = chunkSource.getPreferredQueueSize(downstreamPositionUs, readOnlyMediaChunks);

View File

@ -336,11 +336,7 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu
if (prepared) {
return true;
}
maybeStartLoading();
if (seekMap == null || !tracksBuilt || !haveFormatsForAllTracks()) {
maybeThrowError();
return false;
}
if (seekMap != null && tracksBuilt && haveFormatsForAllTracks()) {
int trackCount = sampleQueues.size();
TrackGroup[] trackArray = new TrackGroup[trackCount];
trackEnabledStates = new boolean[trackCount];
@ -358,6 +354,13 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu
prepared = true;
return true;
}
// We're not prepared.
maybeThrowError();
if (!loader.isLoading()) {
startLoading();
}
return false;
}
@Override
public long getDurationUs() {
@ -414,10 +417,6 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu
public void continueBuffering(long playbackPositionUs) {
downstreamPositionUs = playbackPositionUs;
discardSamplesForDisabledTracks(downstreamPositionUs);
if (loadingFinished) {
return;
}
maybeStartLoading();
}
@Override
@ -588,15 +587,11 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu
loader.cancelLoading();
} else {
clearState();
maybeStartLoading();
startLoading();
}
}
private void maybeStartLoading() {
if (loadingFinished || loader.isLoading() || fatalException != null) {
return;
}
private void startLoading() {
sampleTimeOffsetUs = 0;
havePendingNextSampleUs = false;
loadable = new ExtractingLoadable(uri, dataSource, extractorHolder, allocator,

View File

@ -69,7 +69,6 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
private boolean prepared;
private boolean seenFirstTrackSelection;
private boolean loadControlRegistered;
private int enabledTrackCount;
private Format downstreamFormat;
@ -140,7 +139,6 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
HlsExtractorWrapper extractor = extractors.getFirst();
if (extractor.isPrepared()) {
buildTracks(extractor);
maybeStartLoading(); // Update the load control.
prepared = true;
return true;
} else if (extractors.size() > 1) {
@ -150,20 +148,16 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
}
}
}
// We're not prepared and we haven't loaded what we need.
if (!loadControlRegistered) {
loadControl.register(this, bufferSizeContribution);
loadControlRegistered = true;
}
// We're not prepared.
maybeThrowError();
if (!loader.isLoading()) {
// We're going to have to start loading a chunk to get what we need for preparation. We should
// attempt to load the chunk at positionUs, so that we'll already be loading the correct chunk
// in the common case where the renderer is subsequently enabled at this position.
pendingResetPositionUs = positionUs;
downstreamPositionUs = positionUs;
}
maybeStartLoading();
maybeThrowError();
}
return false;
}
@ -181,6 +175,7 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
public TrackStream[] selectTracks(List<TrackStream> oldStreams,
List<TrackSelection> newSelections, long positionUs) {
Assertions.checkState(prepared);
boolean tracksWereEnabled = enabledTrackCount > 0;
// Unselect old tracks.
for (int i = 0; i < oldStreams.size(); i++) {
int group = ((TrackStreamImpl) oldStreams.get(i)).group;
@ -206,10 +201,8 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
chunkSource.reset();
downstreamPositionUs = Long.MIN_VALUE;
downstreamFormat = null;
if (loader != null) {
if (loadControlRegistered) {
if (tracksWereEnabled) {
loadControl.unregister(this);
loadControlRegistered = false;
}
if (loader.isLoading()) {
loader.cancelLoading();
@ -217,14 +210,14 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
clearState();
loadControl.trimAllocator();
}
}
} else if (primaryTracksDeselected || (seenFirstTrackSelection && newStreams.length > 0)) {
if (!loadControlRegistered) {
} else {
if (!tracksWereEnabled) {
loadControl.register(this, bufferSizeContribution);
loadControlRegistered = true;
}
if (primaryTracksDeselected || (seenFirstTrackSelection && newStreams.length > 0)) {
seekToInternal(positionUs);
}
}
seenFirstTrackSelection = true;
return newStreams;
}
@ -235,8 +228,10 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
if (!extractors.isEmpty()) {
discardSamplesForDisabledTracks(getCurrentExtractor(), downstreamPositionUs);
}
if (!loader.isLoading()) {
maybeStartLoading();
}
}
@Override
public long getBufferedPositionUs() {
@ -269,10 +264,9 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
@Override
public void release() {
enabledTrackCount = 0;
if (loadControlRegistered) {
if (enabledTrackCount > 0) {
loadControl.unregister(this);
loadControlRegistered = false;
enabledTrackCount = 0;
}
loader.release();
}
@ -629,13 +623,9 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
}
private void maybeStartLoading() {
if (loader.isLoading()) {
return;
}
long nextLoadPositionUs = getNextLoadPositionUs();
boolean isNext = loadControl.update(this, downstreamPositionUs, nextLoadPositionUs, false);
if (!isNext || (prepared && enabledTrackCount == 0)) {
boolean shouldStartLoading = !prepared || (enabledTrackCount > 0
&& loadControl.update(this, downstreamPositionUs, getNextLoadPositionUs(), false));
if (!shouldStartLoading) {
return;
}
@ -648,7 +638,9 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
if (endOfStream) {
loadingFinished = true;
if (prepared) {
loadControl.update(this, downstreamPositionUs, -1, false);
}
return;
}
@ -676,9 +668,11 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
currentLoadable.trigger, currentLoadable.format, -1, -1);
}
loader.startLoading(currentLoadable, this);
if (prepared) {
// Update the load control again to indicate that we're now loading.
loadControl.update(this, downstreamPositionUs, getNextLoadPositionUs(), true);
}
}
/**
* Gets the next load time, assuming that the next load starts where the previous chunk ended (or
@ -688,8 +682,8 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
if (isPendingReset()) {
return pendingResetPositionUs;
} else {
return loadingFinished || (prepared && enabledTrackCount == 0) ? -1
: currentTsLoadable != null ? currentTsLoadable.endTimeUs : previousTsLoadable.endTimeUs;
return loadingFinished ? -1 : (currentTsLoadable != null ? currentTsLoadable.endTimeUs
: previousTsLoadable.endTimeUs);
}
}