Commit 4888592c by olly Committed by Oliver Woodman

Fix HLS track selection.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=123851448
parent 94fa2ecf
...@@ -112,7 +112,7 @@ public class HlsChunkSource { ...@@ -112,7 +112,7 @@ public class HlsChunkSource {
evaluation = new Evaluation(); evaluation = new Evaluation();
variantPlaylists = new HlsMediaPlaylist[variants.length]; variantPlaylists = new HlsMediaPlaylist[variants.length];
variantLastPlaylistLoadTimesMs = new long[variants.length]; variantLastPlaylistLoadTimesMs = new long[variants.length];
selectTracks(new int[] {0}); selectTracks(new int[] {0}, false);
} }
/** /**
...@@ -187,11 +187,9 @@ public class HlsChunkSource { ...@@ -187,11 +187,9 @@ public class HlsChunkSource {
* This method should only be called after the source has been prepared. * This method should only be called after the source has been prepared.
* *
* @param tracks The track indices. * @param tracks The track indices.
* @return True if one or more tracks was unselected. False otherwise. * @param isFirstTrackSelection True if this is the first selection, false otherwise.
*/ */
public boolean selectTracks(int[] tracks) { public void selectTracks(int[] tracks, boolean isFirstTrackSelection) {
Variant[] oldEnabledVariants = enabledVariants;
// Construct and sort the enabled variants. // Construct and sort the enabled variants.
enabledVariants = new Variant[tracks.length]; enabledVariants = new Variant[tracks.length];
for (int i = 0; i < tracks.length; i++) { for (int i = 0; i < tracks.length; i++) {
...@@ -217,16 +215,10 @@ public class HlsChunkSource { ...@@ -217,16 +215,10 @@ public class HlsChunkSource {
} }
// TODO[REFACTOR]: We need to disable this at some point. // TODO[REFACTOR]: We need to disable this at some point.
adaptiveFormatEvaluator.enable(formats); adaptiveFormatEvaluator.enable(formats);
} if (!isFirstTrackSelection || !Util.contains(formats, evaluation.format)) {
evaluation.format = null;
if (oldEnabledVariants != null) {
for (Variant oldVariant : oldEnabledVariants) {
if (!Util.contains(enabledVariants, oldVariant)) {
return true;
}
} }
} }
return false;
} }
/** /**
......
...@@ -163,7 +163,7 @@ public final class HlsSampleSource implements SampleSource, ...@@ -163,7 +163,7 @@ public final class HlsSampleSource implements SampleSource,
int enabledTrackStreamWrapperCount = 0; int enabledTrackStreamWrapperCount = 0;
for (int i = 0; i < trackStreamWrappers.length; i++) { for (int i = 0; i < trackStreamWrappers.length; i++) {
selectedTrackCounts[i] += selectTracks(trackStreamWrappers[i], oldStreams, newSelections, selectedTrackCounts[i] += selectTracks(trackStreamWrappers[i], oldStreams, newSelections,
positionUs, newStreams); newStreams);
if (selectedTrackCounts[i] > 0) { if (selectedTrackCounts[i] > 0) {
enabledTrackStreamWrapperCount++; enabledTrackStreamWrapperCount++;
} }
...@@ -176,6 +176,9 @@ public final class HlsSampleSource implements SampleSource, ...@@ -176,6 +176,9 @@ public final class HlsSampleSource implements SampleSource,
enabledTrackStreamWrappers[enabledTrackStreamWrapperCount++] = trackStreamWrappers[i]; enabledTrackStreamWrappers[enabledTrackStreamWrapperCount++] = trackStreamWrappers[i];
} }
} }
if (enabledTrackStreamWrapperCount > 0 && seenFirstTrackSelection && !newSelections.isEmpty()) {
seekToUs(positionUs);
}
seenFirstTrackSelection = true; seenFirstTrackSelection = true;
return newStreams; return newStreams;
} }
...@@ -220,7 +223,7 @@ public final class HlsSampleSource implements SampleSource, ...@@ -220,7 +223,7 @@ public final class HlsSampleSource implements SampleSource,
timestampAdjusterProvider.reset(); timestampAdjusterProvider.reset();
for (HlsTrackStreamWrapper trackStreamWrapper : enabledTrackStreamWrappers) { for (HlsTrackStreamWrapper trackStreamWrapper : enabledTrackStreamWrappers) {
trackStreamWrapper.setReadingEnabled(false); trackStreamWrapper.setReadingEnabled(false);
trackStreamWrapper.seekToUs(positionUs); trackStreamWrapper.restartFrom(positionUs);
} }
} }
...@@ -335,7 +338,7 @@ public final class HlsSampleSource implements SampleSource, ...@@ -335,7 +338,7 @@ public final class HlsSampleSource implements SampleSource,
} }
private int selectTracks(HlsTrackStreamWrapper trackStreamWrapper, private int selectTracks(HlsTrackStreamWrapper trackStreamWrapper,
List<TrackStream> allOldStreams, List<TrackSelection> allNewSelections, long positionUs, List<TrackStream> allOldStreams, List<TrackSelection> allNewSelections,
TrackStream[] allNewStreams) { TrackStream[] allNewStreams) {
// Get the subset of the old streams for the source. // Get the subset of the old streams for the source.
ArrayList<TrackStream> oldStreams = new ArrayList<>(); ArrayList<TrackStream> oldStreams = new ArrayList<>();
...@@ -363,7 +366,7 @@ public final class HlsSampleSource implements SampleSource, ...@@ -363,7 +366,7 @@ public final class HlsSampleSource implements SampleSource,
} }
// Perform the selection. // Perform the selection.
TrackStream[] newStreams = trackStreamWrapper.selectTracks(oldStreams, newSelections, TrackStream[] newStreams = trackStreamWrapper.selectTracks(oldStreams, newSelections,
positionUs); !seenFirstTrackSelection);
for (int j = 0; j < newStreams.length; j++) { for (int j = 0; j < newStreams.length; j++) {
allNewStreams[newSelectionOriginalIndices[j]] = newStreams[j]; allNewStreams[newSelectionOriginalIndices[j]] = newStreams[j];
trackStreamSources.put(newStreams[j], trackStreamWrapper); trackStreamSources.put(newStreams[j], trackStreamWrapper);
......
...@@ -69,7 +69,6 @@ import java.util.List; ...@@ -69,7 +69,6 @@ import java.util.List;
private volatile boolean sampleQueuesBuilt; private volatile boolean sampleQueuesBuilt;
private boolean prepared; private boolean prepared;
private boolean seenFirstTrackSelection;
private boolean readingEnabled; private boolean readingEnabled;
private int enabledTrackCount; private int enabledTrackCount;
private Format downstreamFormat; private Format downstreamFormat;
...@@ -171,7 +170,7 @@ import java.util.List; ...@@ -171,7 +170,7 @@ import java.util.List;
} }
public TrackStream[] selectTracks(List<TrackStream> oldStreams, public TrackStream[] selectTracks(List<TrackStream> oldStreams,
List<TrackSelection> newSelections, long positionUs) { List<TrackSelection> newSelections, boolean isFirstTrackSelection) {
Assertions.checkState(prepared); Assertions.checkState(prepared);
boolean tracksWereEnabled = enabledTrackCount > 0; boolean tracksWereEnabled = enabledTrackCount > 0;
// Unselect old tracks. // Unselect old tracks.
...@@ -180,7 +179,6 @@ import java.util.List; ...@@ -180,7 +179,6 @@ import java.util.List;
setTrackGroupEnabledState(group, false); setTrackGroupEnabledState(group, false);
} }
// Select new tracks. // Select new tracks.
boolean primaryTracksDeselected = false;
TrackStream[] newStreams = new TrackStream[newSelections.size()]; TrackStream[] newStreams = new TrackStream[newSelections.size()];
for (int i = 0; i < newStreams.length; i++) { for (int i = 0; i < newStreams.length; i++) {
TrackSelection selection = newSelections.get(i); TrackSelection selection = newSelections.get(i);
...@@ -189,7 +187,7 @@ import java.util.List; ...@@ -189,7 +187,7 @@ import java.util.List;
setTrackGroupEnabledState(group, true); setTrackGroupEnabledState(group, true);
sampleQueues.valueAt(group).needDownstreamFormat(); sampleQueues.valueAt(group).needDownstreamFormat();
if (group == primaryTrackGroupIndex) { if (group == primaryTrackGroupIndex) {
primaryTracksDeselected |= chunkSource.selectTracks(tracks); chunkSource.selectTracks(tracks, isFirstTrackSelection);
} }
newStreams[i] = new TrackStreamImpl(group); newStreams[i] = new TrackStreamImpl(group);
} }
...@@ -207,15 +205,9 @@ import java.util.List; ...@@ -207,15 +205,9 @@ import java.util.List;
clearState(); clearState();
loadControl.trimAllocator(); loadControl.trimAllocator();
} }
} else { } else if (!tracksWereEnabled) {
if (!tracksWereEnabled) { loadControl.register(this, bufferSizeContribution);
loadControl.register(this, bufferSizeContribution);
}
if (primaryTracksDeselected || (seenFirstTrackSelection && newStreams.length > 0)) {
seekToInternal(positionUs);
}
} }
seenFirstTrackSelection = true;
return newStreams; return newStreams;
} }
...@@ -253,8 +245,17 @@ import java.util.List; ...@@ -253,8 +245,17 @@ import java.util.List;
} }
} }
public void seekToUs(long positionUs) { public void restartFrom(long positionUs) {
seekToInternal(positionUs); lastSeekPositionUs = positionUs;
downstreamPositionUs = positionUs;
pendingResetPositionUs = positionUs;
loadingFinished = false;
if (loader.isLoading()) {
loader.cancelLoading();
} else {
clearState();
maybeStartLoading();
}
} }
public void release() { public void release() {
...@@ -500,17 +501,6 @@ import java.util.List; ...@@ -500,17 +501,6 @@ import java.util.List;
containerFormat.width, containerFormat.height, 0, containerFormat.language); containerFormat.width, containerFormat.height, 0, containerFormat.language);
} }
/**
* Performs a seek. The operation is performed even if the seek is to the current position.
*
* @param positionUs The position to seek to.
*/
private void seekToInternal(long positionUs) {
lastSeekPositionUs = positionUs;
downstreamPositionUs = positionUs;
restartFrom(positionUs);
}
private void discardSamplesForDisabledTracks() { private void discardSamplesForDisabledTracks() {
if (!prepared) { if (!prepared) {
return; return;
...@@ -522,17 +512,6 @@ import java.util.List; ...@@ -522,17 +512,6 @@ import java.util.List;
} }
} }
private void restartFrom(long positionUs) {
pendingResetPositionUs = positionUs;
loadingFinished = false;
if (loader.isLoading()) {
loader.cancelLoading();
} else {
clearState();
maybeStartLoading();
}
}
private void clearState() { private void clearState() {
for (int i = 0; i < sampleQueues.size(); i++) { for (int i = 0; i < sampleQueues.size(); i++) {
sampleQueues.valueAt(i).clear(); sampleQueues.valueAt(i).clear();
......
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