Commit afe6f667 by olly Committed by Oliver Woodman

Move all buffer discard to MediaPeriod.discardBuffer

This is a step toward retaining a back-buffer in a way that
works for all MediaSource implementations. It's not possible
to adjust the discardBuffer calls in ExoPlayerImplInternal
to discard up to (position - backBufferDurationUs). Next steps
are to:

1. Find an appropriate place to specify the back buffer value,
   to be passed to the discardBuffer calls. I guess the
   LoadControl is the appropriate place to define such values.
2. Enhance discardBuffer to support a toKeyframe argument to
   pass through to discardTo.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=175565363
parent 67bbbed9
...@@ -732,6 +732,7 @@ import java.io.IOException; ...@@ -732,6 +732,7 @@ import java.io.IOException;
setPlayingPeriodHolder(newPlayingPeriodHolder); setPlayingPeriodHolder(newPlayingPeriodHolder);
if (playingPeriodHolder.hasEnabledTracks) { if (playingPeriodHolder.hasEnabledTracks) {
periodPositionUs = playingPeriodHolder.mediaPeriod.seekToUs(periodPositionUs); periodPositionUs = playingPeriodHolder.mediaPeriod.seekToUs(periodPositionUs);
playingPeriodHolder.mediaPeriod.discardBuffer(periodPositionUs);
} }
resetRendererPosition(periodPositionUs); resetRendererPosition(periodPositionUs);
maybeContinueLoading(); maybeContinueLoading();
......
...@@ -569,7 +569,6 @@ import java.util.Arrays; ...@@ -569,7 +569,6 @@ import java.util.Arrays;
if (!seekInsideQueue && (trackIsAudioVideoFlags[i] || !haveAudioVideoTracks)) { if (!seekInsideQueue && (trackIsAudioVideoFlags[i] || !haveAudioVideoTracks)) {
return false; return false;
} }
sampleQueue.discardToRead();
} }
return true; return true;
} }
......
...@@ -110,21 +110,17 @@ public class ChunkSampleStream<T extends ChunkSource> implements SampleStream, S ...@@ -110,21 +110,17 @@ public class ChunkSampleStream<T extends ChunkSource> implements SampleStream, S
lastSeekPositionUs = positionUs; lastSeekPositionUs = positionUs;
} }
// TODO: Generalize this method to also discard from the primary sample queue and stop discarding
// from this queue in readData and skipData. This will cause samples to be kept in the queue until
// they've been rendered, rather than being discarded as soon as they're read by the renderer.
// This will make in-buffer seeks more likely when seeking slightly forward from the current
// position. This change will need handling with care, in particular when considering removal of
// chunks from the front of the mediaChunks list.
/** /**
* Discards buffered media for embedded tracks, up to the specified position. * Discards buffered media up to the specified position.
* *
* @param positionUs The position to discard up to, in microseconds. * @param positionUs The position to discard up to, in microseconds.
*/ */
public void discardEmbeddedTracksTo(long positionUs) { public void discardBuffer(long positionUs) {
primarySampleQueue.discardTo(positionUs, false, true);
for (int i = 0; i < embeddedSampleQueues.length; i++) { for (int i = 0; i < embeddedSampleQueues.length; i++) {
embeddedSampleQueues[i].discardTo(positionUs, true, embeddedTracksSelected[i]); embeddedSampleQueues[i].discardTo(positionUs, true, embeddedTracksSelected[i]);
} }
discardDownstreamMediaChunks(primarySampleQueue.getFirstIndex());
} }
/** /**
...@@ -189,16 +185,15 @@ public class ChunkSampleStream<T extends ChunkSource> implements SampleStream, S ...@@ -189,16 +185,15 @@ public class ChunkSampleStream<T extends ChunkSource> implements SampleStream, S
*/ */
public void seekToUs(long positionUs) { public void seekToUs(long positionUs) {
lastSeekPositionUs = positionUs; lastSeekPositionUs = positionUs;
primarySampleQueue.rewind();
// If we're not pending a reset, see if we can seek within the primary sample queue. // If we're not pending a reset, see if we can seek within the primary sample queue.
boolean seekInsideBuffer = !isPendingReset() && (primarySampleQueue.advanceTo(positionUs, true, boolean seekInsideBuffer = !isPendingReset() && (primarySampleQueue.advanceTo(positionUs, true,
positionUs < getNextLoadPositionUs()) != SampleQueue.ADVANCE_FAILED); positionUs < getNextLoadPositionUs()) != SampleQueue.ADVANCE_FAILED);
if (seekInsideBuffer) { if (seekInsideBuffer) {
// We succeeded. Discard samples and corresponding chunks prior to the seek position. // We succeeded. Discard samples and corresponding chunks prior to the seek position.
discardDownstreamMediaChunks(primarySampleQueue.getReadIndex());
primarySampleQueue.discardToRead();
for (SampleQueue embeddedSampleQueue : embeddedSampleQueues) { for (SampleQueue embeddedSampleQueue : embeddedSampleQueues) {
embeddedSampleQueue.rewind(); embeddedSampleQueue.rewind();
embeddedSampleQueue.discardTo(positionUs, true, false); embeddedSampleQueue.advanceTo(positionUs, true, false);
} }
} else { } else {
// We failed, and need to restart. // We failed, and need to restart.
...@@ -261,13 +256,8 @@ public class ChunkSampleStream<T extends ChunkSource> implements SampleStream, S ...@@ -261,13 +256,8 @@ public class ChunkSampleStream<T extends ChunkSource> implements SampleStream, S
if (isPendingReset()) { if (isPendingReset()) {
return C.RESULT_NOTHING_READ; return C.RESULT_NOTHING_READ;
} }
discardDownstreamMediaChunks(primarySampleQueue.getReadIndex()); return primarySampleQueue.read(formatHolder, buffer, formatRequired, loadingFinished,
int result = primarySampleQueue.read(formatHolder, buffer, formatRequired, loadingFinished,
lastSeekPositionUs); lastSeekPositionUs);
if (result == C.RESULT_BUFFER_READ) {
primarySampleQueue.discardToRead();
}
return result;
} }
@Override @Override
...@@ -282,7 +272,6 @@ public class ChunkSampleStream<T extends ChunkSource> implements SampleStream, S ...@@ -282,7 +272,6 @@ public class ChunkSampleStream<T extends ChunkSource> implements SampleStream, S
skipCount = 0; skipCount = 0;
} }
} }
primarySampleQueue.discardToRead();
return skipCount; return skipCount;
} }
......
...@@ -261,7 +261,7 @@ import java.util.Map; ...@@ -261,7 +261,7 @@ import java.util.Map;
@Override @Override
public void discardBuffer(long positionUs) { public void discardBuffer(long positionUs) {
for (ChunkSampleStream<DashChunkSource> sampleStream : sampleStreams) { for (ChunkSampleStream<DashChunkSource> sampleStream : sampleStreams) {
sampleStream.discardEmbeddedTracksTo(positionUs); sampleStream.discardBuffer(positionUs);
} }
} }
......
...@@ -792,7 +792,6 @@ import java.util.LinkedList; ...@@ -792,7 +792,6 @@ import java.util.LinkedList;
if (!seekInsideQueue && (sampleQueueIsAudioVideoFlags[i] || !haveAudioVideoSampleQueues)) { if (!seekInsideQueue && (sampleQueueIsAudioVideoFlags[i] || !haveAudioVideoSampleQueues)) {
return false; return false;
} }
sampleQueue.discardToRead();
} }
return true; return true;
} }
......
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