Commit fa04c713 by olly Committed by Oliver Woodman

Discard all sample data for disabled tracks.

There's a more nuanced version of this where we actually
disable on the loading side, but it's quite tricky to get
the threading just right. I have a change that I'll
probably manage to clean up and send out at some point.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=120106967
parent 1e4a3c16
...@@ -146,17 +146,10 @@ public class DefaultTrackOutput implements TrackOutput { ...@@ -146,17 +146,10 @@ public class DefaultTrackOutput implements TrackOutput {
} }
/** /**
* Discards samples from the queue up to the specified time. * Skips all currently buffered samples.
*
* @param timeUs The time up to which samples should be discarded, in microseconds.
*/ */
public void discardUntil(long timeUs) { public void skipAllSamples() {
while (rollingBuffer.peekSample(sampleBuffer) && sampleBuffer.timeUs < timeUs) { rollingBuffer.skipAllSamples();
rollingBuffer.skipSample();
// We're discarding one or more samples. A subsequent read will need to start at a keyframe.
needKeyframe = true;
}
lastReadTimeUs = Long.MIN_VALUE;
} }
/** /**
......
...@@ -415,7 +415,7 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu ...@@ -415,7 +415,7 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu
@Override @Override
public void continueBuffering(long playbackPositionUs) { public void continueBuffering(long playbackPositionUs) {
downstreamPositionUs = playbackPositionUs; downstreamPositionUs = playbackPositionUs;
discardSamplesForDisabledTracks(downstreamPositionUs); discardSamplesForDisabledTracks();
} }
@Override @Override
...@@ -647,10 +647,10 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu ...@@ -647,10 +647,10 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu
return true; return true;
} }
private void discardSamplesForDisabledTracks(long timeUs) { private void discardSamplesForDisabledTracks() {
for (int i = 0; i < trackEnabledStates.length; i++) { for (int i = 0; i < trackEnabledStates.length; i++) {
if (!trackEnabledStates[i]) { if (!trackEnabledStates[i]) {
sampleQueues.valueAt(i).discardUntil(timeUs); sampleQueues.valueAt(i).skipAllSamples();
} }
} }
} }
......
...@@ -153,6 +153,17 @@ import java.util.concurrent.LinkedBlockingDeque; ...@@ -153,6 +153,17 @@ import java.util.concurrent.LinkedBlockingDeque;
} }
/** /**
* Skips all currently buffered samples.
*/
public void skipAllSamples() {
long nextOffset = infoQueue.skipAllSamples();
if (nextOffset == -1) {
return;
}
dropDownstreamTo(nextOffset);
}
/**
* Attempts to skip to the keyframe before the specified time, if it's present in the buffer. * Attempts to skip to the keyframe before the specified time, if it's present in the buffer.
* *
* @param timeUs The seek time. * @param timeUs The seek time.
...@@ -534,6 +545,25 @@ import java.util.concurrent.LinkedBlockingDeque; ...@@ -534,6 +545,25 @@ import java.util.concurrent.LinkedBlockingDeque;
} }
/** /**
* Skips all currently buffered samples.
*
* @return The absolute position of the first byte in the rolling buffer that may still be
* required after skipping the samples, or -1 if no samples were skipped.
*/
public synchronized long skipAllSamples() {
if (queueSize == 0) {
return -1;
}
relativeReadIndex = (relativeReadIndex + queueSize) % capacity;
absoluteReadIndex += queueSize;
queueSize = 0;
int lastReadIndex = (relativeReadIndex == 0 ? capacity : relativeReadIndex) - 1;
return sizes[lastReadIndex] + offsets[lastReadIndex];
}
/**
* Attempts to locate the keyframe before the specified time, if it's present in the buffer. * Attempts to locate the keyframe before the specified time, if it's present in the buffer.
* *
* @param timeUs The seek time. * @param timeUs The seek time.
......
...@@ -191,16 +191,15 @@ public final class HlsExtractorWrapper implements ExtractorOutput { ...@@ -191,16 +191,15 @@ public final class HlsExtractorWrapper implements ExtractorOutput {
} }
/** /**
* Discards samples for the specified track up to the specified time. * Discards all samples for the specified track.
* <p> * <p>
* This method must only be called after the extractor has been prepared. * This method must only be called after the extractor has been prepared.
* *
* @param track The track from which samples should be discarded. * @param track The track from which samples should be discarded.
* @param timeUs The time up to which samples should be discarded, in microseconds.
*/ */
public void discardUntil(int track, long timeUs) { public void discardSamplesForTrack(int track) {
Assertions.checkState(isPrepared()); Assertions.checkState(isPrepared());
sampleQueues.valueAt(track).discardUntil(timeUs); sampleQueues.valueAt(track).skipAllSamples();
} }
/** /**
......
...@@ -224,7 +224,7 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback { ...@@ -224,7 +224,7 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
public void continueBuffering(long playbackPositionUs) { public void continueBuffering(long playbackPositionUs) {
downstreamPositionUs = playbackPositionUs; downstreamPositionUs = playbackPositionUs;
if (!extractors.isEmpty()) { if (!extractors.isEmpty()) {
discardSamplesForDisabledTracks(getCurrentExtractor(), downstreamPositionUs); discardSamplesForDisabledTracks(getCurrentExtractor());
} }
if (!loader.isLoading()) { if (!loader.isLoading()) {
maybeStartLoading(); maybeStartLoading();
...@@ -570,13 +570,13 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback { ...@@ -570,13 +570,13 @@ public final class HlsSampleSource implements SampleSource, Loader.Callback {
return extractor; return extractor;
} }
private void discardSamplesForDisabledTracks(HlsExtractorWrapper extractor, long timeUs) { private void discardSamplesForDisabledTracks(HlsExtractorWrapper extractor) {
if (!extractor.isPrepared()) { if (!extractor.isPrepared()) {
return; return;
} }
for (int i = 0; i < groupEnabledStates.length; i++) { for (int i = 0; i < groupEnabledStates.length; i++) {
if (!groupEnabledStates[i]) { if (!groupEnabledStates[i]) {
extractor.discardUntil(i, timeUs); extractor.discardSamplesForTrack(i);
} }
} }
} }
......
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