mirror of
https://github.com/androidx/media.git
synced 2025-05-10 00:59:51 +08:00
LoadControl - Start cleaning up usage somewhat.
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=119758106
This commit is contained in:
parent
fec7798005
commit
3ae1c615d9
@ -68,7 +68,6 @@ public class ChunkSampleSource implements SampleSource, TrackStream, Loader.Call
|
|||||||
private long pendingResetPositionUs;
|
private long pendingResetPositionUs;
|
||||||
private long lastPreferredQueueSizeEvaluationTimeMs;
|
private long lastPreferredQueueSizeEvaluationTimeMs;
|
||||||
private boolean pendingReset;
|
private boolean pendingReset;
|
||||||
private boolean loadControlRegistered;
|
|
||||||
|
|
||||||
private TrackGroupArray trackGroups;
|
private TrackGroupArray trackGroups;
|
||||||
private long durationUs;
|
private long durationUs;
|
||||||
@ -169,6 +168,7 @@ public class ChunkSampleSource implements SampleSource, TrackStream, Loader.Call
|
|||||||
Assertions.checkState(prepared);
|
Assertions.checkState(prepared);
|
||||||
Assertions.checkState(oldStreams.size() <= 1);
|
Assertions.checkState(oldStreams.size() <= 1);
|
||||||
Assertions.checkState(newSelections.size() <= 1);
|
Assertions.checkState(newSelections.size() <= 1);
|
||||||
|
boolean trackWasEnabled = trackEnabled;
|
||||||
// Unselect old tracks.
|
// Unselect old tracks.
|
||||||
if (!oldStreams.isEmpty()) {
|
if (!oldStreams.isEmpty()) {
|
||||||
Assertions.checkState(trackEnabled);
|
Assertions.checkState(trackEnabled);
|
||||||
@ -185,9 +185,8 @@ public class ChunkSampleSource implements SampleSource, TrackStream, Loader.Call
|
|||||||
}
|
}
|
||||||
// Cancel or start requests as necessary.
|
// Cancel or start requests as necessary.
|
||||||
if (!trackEnabled) {
|
if (!trackEnabled) {
|
||||||
if (loadControlRegistered) {
|
if (trackWasEnabled) {
|
||||||
loadControl.unregister(this);
|
loadControl.unregister(this);
|
||||||
loadControlRegistered = false;
|
|
||||||
}
|
}
|
||||||
if (loader.isLoading()) {
|
if (loader.isLoading()) {
|
||||||
loader.cancelLoading();
|
loader.cancelLoading();
|
||||||
@ -195,10 +194,9 @@ public class ChunkSampleSource implements SampleSource, TrackStream, Loader.Call
|
|||||||
clearState();
|
clearState();
|
||||||
loadControl.trimAllocator();
|
loadControl.trimAllocator();
|
||||||
}
|
}
|
||||||
} else if (trackEnabled) {
|
} else {
|
||||||
if (!loadControlRegistered) {
|
if (!trackWasEnabled) {
|
||||||
loadControl.register(this, bufferSizeContribution);
|
loadControl.register(this, bufferSizeContribution);
|
||||||
loadControlRegistered = true;
|
|
||||||
}
|
}
|
||||||
downstreamFormat = null;
|
downstreamFormat = null;
|
||||||
downstreamSampleFormat = null;
|
downstreamSampleFormat = null;
|
||||||
@ -214,7 +212,9 @@ public class ChunkSampleSource implements SampleSource, TrackStream, Loader.Call
|
|||||||
public void continueBuffering(long positionUs) {
|
public void continueBuffering(long positionUs) {
|
||||||
downstreamPositionUs = positionUs;
|
downstreamPositionUs = positionUs;
|
||||||
chunkSource.continueBuffering(positionUs);
|
chunkSource.continueBuffering(positionUs);
|
||||||
maybeStartLoading();
|
if (!loader.isLoading()) {
|
||||||
|
maybeStartLoading();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -253,8 +253,10 @@ public class ChunkSampleSource implements SampleSource, TrackStream, Loader.Call
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void release() {
|
public void release() {
|
||||||
prepared = false;
|
if (trackEnabled) {
|
||||||
trackEnabled = false;
|
loadControl.unregister(this);
|
||||||
|
trackEnabled = false;
|
||||||
|
}
|
||||||
loader.release();
|
loader.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -420,10 +422,6 @@ public class ChunkSampleSource implements SampleSource, TrackStream, Loader.Call
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void maybeStartLoading() {
|
private void maybeStartLoading() {
|
||||||
if (loader.isLoading()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
long now = SystemClock.elapsedRealtime();
|
long now = SystemClock.elapsedRealtime();
|
||||||
if (now - lastPreferredQueueSizeEvaluationTimeMs > 5000) {
|
if (now - lastPreferredQueueSizeEvaluationTimeMs > 5000) {
|
||||||
int queueSize = chunkSource.getPreferredQueueSize(downstreamPositionUs, readOnlyMediaChunks);
|
int queueSize = chunkSource.getPreferredQueueSize(downstreamPositionUs, readOnlyMediaChunks);
|
||||||
|
@ -336,27 +336,30 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu
|
|||||||
if (prepared) {
|
if (prepared) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
maybeStartLoading();
|
if (seekMap != null && tracksBuilt && haveFormatsForAllTracks()) {
|
||||||
if (seekMap == null || !tracksBuilt || !haveFormatsForAllTracks()) {
|
int trackCount = sampleQueues.size();
|
||||||
maybeThrowError();
|
TrackGroup[] trackArray = new TrackGroup[trackCount];
|
||||||
return false;
|
trackEnabledStates = new boolean[trackCount];
|
||||||
|
pendingResets = new boolean[trackCount];
|
||||||
|
pendingMediaFormat = new boolean[trackCount];
|
||||||
|
durationUs = seekMap.getDurationUs();
|
||||||
|
for (int i = 0; i < trackCount; i++) {
|
||||||
|
trackArray[i] = new TrackGroup(sampleQueues.valueAt(i).getFormat());
|
||||||
|
}
|
||||||
|
tracks = new TrackGroupArray(trackArray);
|
||||||
|
if (minLoadableRetryCount == MIN_RETRY_COUNT_DEFAULT_FOR_MEDIA && !seekMap.isSeekable()
|
||||||
|
&& durationUs == C.UNKNOWN_TIME_US) {
|
||||||
|
loader.setMinRetryCount(DEFAULT_MIN_LOADABLE_RETRY_COUNT_LIVE);
|
||||||
|
}
|
||||||
|
prepared = true;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
int trackCount = sampleQueues.size();
|
// We're not prepared.
|
||||||
TrackGroup[] trackArray = new TrackGroup[trackCount];
|
maybeThrowError();
|
||||||
trackEnabledStates = new boolean[trackCount];
|
if (!loader.isLoading()) {
|
||||||
pendingResets = new boolean[trackCount];
|
startLoading();
|
||||||
pendingMediaFormat = new boolean[trackCount];
|
|
||||||
durationUs = seekMap.getDurationUs();
|
|
||||||
for (int i = 0; i < trackCount; i++) {
|
|
||||||
trackArray[i] = new TrackGroup(sampleQueues.valueAt(i).getFormat());
|
|
||||||
}
|
}
|
||||||
tracks = new TrackGroupArray(trackArray);
|
return false;
|
||||||
if (minLoadableRetryCount == MIN_RETRY_COUNT_DEFAULT_FOR_MEDIA && !seekMap.isSeekable()
|
|
||||||
&& durationUs == C.UNKNOWN_TIME_US) {
|
|
||||||
loader.setMinRetryCount(DEFAULT_MIN_LOADABLE_RETRY_COUNT_LIVE);
|
|
||||||
}
|
|
||||||
prepared = true;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -414,10 +417,6 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu
|
|||||||
public void continueBuffering(long playbackPositionUs) {
|
public void continueBuffering(long playbackPositionUs) {
|
||||||
downstreamPositionUs = playbackPositionUs;
|
downstreamPositionUs = playbackPositionUs;
|
||||||
discardSamplesForDisabledTracks(downstreamPositionUs);
|
discardSamplesForDisabledTracks(downstreamPositionUs);
|
||||||
if (loadingFinished) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
maybeStartLoading();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -588,15 +587,11 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu
|
|||||||
loader.cancelLoading();
|
loader.cancelLoading();
|
||||||
} else {
|
} else {
|
||||||
clearState();
|
clearState();
|
||||||
maybeStartLoading();
|
startLoading();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void maybeStartLoading() {
|
private void startLoading() {
|
||||||
if (loadingFinished || loader.isLoading() || fatalException != null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
sampleTimeOffsetUs = 0;
|
sampleTimeOffsetUs = 0;
|
||||||
havePendingNextSampleUs = false;
|
havePendingNextSampleUs = false;
|
||||||
loadable = new ExtractingLoadable(uri, dataSource, extractorHolder, allocator,
|
loadable = new ExtractingLoadable(uri, dataSource, extractorHolder, allocator,
|
||||||
|
@ -69,7 +69,6 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
|
|||||||
|
|
||||||
private boolean prepared;
|
private boolean prepared;
|
||||||
private boolean seenFirstTrackSelection;
|
private boolean seenFirstTrackSelection;
|
||||||
private boolean loadControlRegistered;
|
|
||||||
private int enabledTrackCount;
|
private int enabledTrackCount;
|
||||||
|
|
||||||
private Format downstreamFormat;
|
private Format downstreamFormat;
|
||||||
@ -140,7 +139,6 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
|
|||||||
HlsExtractorWrapper extractor = extractors.getFirst();
|
HlsExtractorWrapper extractor = extractors.getFirst();
|
||||||
if (extractor.isPrepared()) {
|
if (extractor.isPrepared()) {
|
||||||
buildTracks(extractor);
|
buildTracks(extractor);
|
||||||
maybeStartLoading(); // Update the load control.
|
|
||||||
prepared = true;
|
prepared = true;
|
||||||
return true;
|
return true;
|
||||||
} else if (extractors.size() > 1) {
|
} 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.
|
// We're not prepared.
|
||||||
if (!loadControlRegistered) {
|
maybeThrowError();
|
||||||
loadControl.register(this, bufferSizeContribution);
|
|
||||||
loadControlRegistered = true;
|
|
||||||
}
|
|
||||||
if (!loader.isLoading()) {
|
if (!loader.isLoading()) {
|
||||||
// We're going to have to start loading a chunk to get what we need for preparation. We should
|
// 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
|
// 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.
|
// in the common case where the renderer is subsequently enabled at this position.
|
||||||
pendingResetPositionUs = positionUs;
|
pendingResetPositionUs = positionUs;
|
||||||
downstreamPositionUs = positionUs;
|
downstreamPositionUs = positionUs;
|
||||||
|
maybeStartLoading();
|
||||||
}
|
}
|
||||||
maybeStartLoading();
|
|
||||||
maybeThrowError();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,6 +175,7 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
|
|||||||
public TrackStream[] selectTracks(List<TrackStream> oldStreams,
|
public TrackStream[] selectTracks(List<TrackStream> oldStreams,
|
||||||
List<TrackSelection> newSelections, long positionUs) {
|
List<TrackSelection> newSelections, long positionUs) {
|
||||||
Assertions.checkState(prepared);
|
Assertions.checkState(prepared);
|
||||||
|
boolean tracksWereEnabled = enabledTrackCount > 0;
|
||||||
// Unselect old tracks.
|
// Unselect old tracks.
|
||||||
for (int i = 0; i < oldStreams.size(); i++) {
|
for (int i = 0; i < oldStreams.size(); i++) {
|
||||||
int group = ((TrackStreamImpl) oldStreams.get(i)).group;
|
int group = ((TrackStreamImpl) oldStreams.get(i)).group;
|
||||||
@ -206,24 +201,22 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
|
|||||||
chunkSource.reset();
|
chunkSource.reset();
|
||||||
downstreamPositionUs = Long.MIN_VALUE;
|
downstreamPositionUs = Long.MIN_VALUE;
|
||||||
downstreamFormat = null;
|
downstreamFormat = null;
|
||||||
if (loader != null) {
|
if (tracksWereEnabled) {
|
||||||
if (loadControlRegistered) {
|
loadControl.unregister(this);
|
||||||
loadControl.unregister(this);
|
|
||||||
loadControlRegistered = false;
|
|
||||||
}
|
|
||||||
if (loader.isLoading()) {
|
|
||||||
loader.cancelLoading();
|
|
||||||
} else {
|
|
||||||
clearState();
|
|
||||||
loadControl.trimAllocator();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (primaryTracksDeselected || (seenFirstTrackSelection && newStreams.length > 0)) {
|
if (loader.isLoading()) {
|
||||||
if (!loadControlRegistered) {
|
loader.cancelLoading();
|
||||||
|
} else {
|
||||||
|
clearState();
|
||||||
|
loadControl.trimAllocator();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!tracksWereEnabled) {
|
||||||
loadControl.register(this, bufferSizeContribution);
|
loadControl.register(this, bufferSizeContribution);
|
||||||
loadControlRegistered = true;
|
|
||||||
}
|
}
|
||||||
seekToInternal(positionUs);
|
if (primaryTracksDeselected || (seenFirstTrackSelection && newStreams.length > 0)) {
|
||||||
|
seekToInternal(positionUs);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
seenFirstTrackSelection = true;
|
seenFirstTrackSelection = true;
|
||||||
return newStreams;
|
return newStreams;
|
||||||
@ -235,7 +228,9 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
|
|||||||
if (!extractors.isEmpty()) {
|
if (!extractors.isEmpty()) {
|
||||||
discardSamplesForDisabledTracks(getCurrentExtractor(), downstreamPositionUs);
|
discardSamplesForDisabledTracks(getCurrentExtractor(), downstreamPositionUs);
|
||||||
}
|
}
|
||||||
maybeStartLoading();
|
if (!loader.isLoading()) {
|
||||||
|
maybeStartLoading();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -269,10 +264,9 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void release() {
|
public void release() {
|
||||||
enabledTrackCount = 0;
|
if (enabledTrackCount > 0) {
|
||||||
if (loadControlRegistered) {
|
|
||||||
loadControl.unregister(this);
|
loadControl.unregister(this);
|
||||||
loadControlRegistered = false;
|
enabledTrackCount = 0;
|
||||||
}
|
}
|
||||||
loader.release();
|
loader.release();
|
||||||
}
|
}
|
||||||
@ -629,13 +623,9 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void maybeStartLoading() {
|
private void maybeStartLoading() {
|
||||||
if (loader.isLoading()) {
|
boolean shouldStartLoading = !prepared || (enabledTrackCount > 0
|
||||||
return;
|
&& loadControl.update(this, downstreamPositionUs, getNextLoadPositionUs(), false));
|
||||||
}
|
if (!shouldStartLoading) {
|
||||||
|
|
||||||
long nextLoadPositionUs = getNextLoadPositionUs();
|
|
||||||
boolean isNext = loadControl.update(this, downstreamPositionUs, nextLoadPositionUs, false);
|
|
||||||
if (!isNext || (prepared && enabledTrackCount == 0)) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -648,7 +638,9 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
|
|||||||
|
|
||||||
if (endOfStream) {
|
if (endOfStream) {
|
||||||
loadingFinished = true;
|
loadingFinished = true;
|
||||||
loadControl.update(this, downstreamPositionUs, -1, false);
|
if (prepared) {
|
||||||
|
loadControl.update(this, downstreamPositionUs, -1, false);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -676,8 +668,10 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
|
|||||||
currentLoadable.trigger, currentLoadable.format, -1, -1);
|
currentLoadable.trigger, currentLoadable.format, -1, -1);
|
||||||
}
|
}
|
||||||
loader.startLoading(currentLoadable, this);
|
loader.startLoading(currentLoadable, this);
|
||||||
// Update the load control again to indicate that we're now loading.
|
if (prepared) {
|
||||||
loadControl.update(this, downstreamPositionUs, getNextLoadPositionUs(), true);
|
// Update the load control again to indicate that we're now loading.
|
||||||
|
loadControl.update(this, downstreamPositionUs, getNextLoadPositionUs(), true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -688,8 +682,8 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
|
|||||||
if (isPendingReset()) {
|
if (isPendingReset()) {
|
||||||
return pendingResetPositionUs;
|
return pendingResetPositionUs;
|
||||||
} else {
|
} else {
|
||||||
return loadingFinished || (prepared && enabledTrackCount == 0) ? -1
|
return loadingFinished ? -1 : (currentTsLoadable != null ? currentTsLoadable.endTimeUs
|
||||||
: currentTsLoadable != null ? currentTsLoadable.endTimeUs : previousTsLoadable.endTimeUs;
|
: previousTsLoadable.endTimeUs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user