Skip to content

Commit

Permalink
Move all buffer discard to MediaPeriod.discardBuffer
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ojw28 committed Nov 13, 2017
1 parent 67bbbed commit afe6f66
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ && shouldKeepPeriodHolder(periodId, periodPositionUs, periodHolder)) {
setPlayingPeriodHolder(newPlayingPeriodHolder);
if (playingPeriodHolder.hasEnabledTracks) {
periodPositionUs = playingPeriodHolder.mediaPeriod.seekToUs(periodPositionUs);
playingPeriodHolder.mediaPeriod.discardBuffer(periodPositionUs);
}
resetRendererPosition(periodPositionUs);
maybeContinueLoading();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,6 @@ private boolean seekInsideBufferUs(long positionUs) {
if (!seekInsideQueue && (trackIsAudioVideoFlags[i] || !haveAudioVideoTracks)) {
return false;
}
sampleQueue.discardToRead();
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,17 @@ public ChunkSampleStream(int primaryTrackType, int[] embeddedTrackTypes, T chunk
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.
*/
public void discardEmbeddedTracksTo(long positionUs) {
public void discardBuffer(long positionUs) {
primarySampleQueue.discardTo(positionUs, false, true);
for (int i = 0; i < embeddedSampleQueues.length; i++) {
embeddedSampleQueues[i].discardTo(positionUs, true, embeddedTracksSelected[i]);
}
discardDownstreamMediaChunks(primarySampleQueue.getFirstIndex());
}

/**
Expand Down Expand Up @@ -189,16 +185,15 @@ public long getBufferedPositionUs() {
*/
public void seekToUs(long positionUs) {
lastSeekPositionUs = positionUs;
primarySampleQueue.rewind();
// If we're not pending a reset, see if we can seek within the primary sample queue.
boolean seekInsideBuffer = !isPendingReset() && (primarySampleQueue.advanceTo(positionUs, true,
positionUs < getNextLoadPositionUs()) != SampleQueue.ADVANCE_FAILED);
if (seekInsideBuffer) {
// We succeeded. Discard samples and corresponding chunks prior to the seek position.
discardDownstreamMediaChunks(primarySampleQueue.getReadIndex());
primarySampleQueue.discardToRead();
for (SampleQueue embeddedSampleQueue : embeddedSampleQueues) {
embeddedSampleQueue.rewind();
embeddedSampleQueue.discardTo(positionUs, true, false);
embeddedSampleQueue.advanceTo(positionUs, true, false);
}
} else {
// We failed, and need to restart.
Expand Down Expand Up @@ -261,13 +256,8 @@ public int readData(FormatHolder formatHolder, DecoderInputBuffer buffer,
if (isPendingReset()) {
return C.RESULT_NOTHING_READ;
}
discardDownstreamMediaChunks(primarySampleQueue.getReadIndex());
int result = primarySampleQueue.read(formatHolder, buffer, formatRequired, loadingFinished,
return primarySampleQueue.read(formatHolder, buffer, formatRequired, loadingFinished,
lastSeekPositionUs);
if (result == C.RESULT_BUFFER_READ) {
primarySampleQueue.discardToRead();
}
return result;
}

@Override
Expand All @@ -282,7 +272,6 @@ public int skipData(long positionUs) {
skipCount = 0;
}
}
primarySampleQueue.discardToRead();
return skipCount;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ private void selectEmbeddedSampleStreams(TrackSelection[] selections,
@Override
public void discardBuffer(long positionUs) {
for (ChunkSampleStream<DashChunkSource> sampleStream : sampleStreams) {
sampleStream.discardEmbeddedTracksTo(positionUs);
sampleStream.discardBuffer(positionUs);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,6 @@ private boolean seekInsideBufferUs(long positionUs) {
if (!seekInsideQueue && (sampleQueueIsAudioVideoFlags[i] || !haveAudioVideoSampleQueues)) {
return false;
}
sampleQueue.discardToRead();
}
return true;
}
Expand Down

0 comments on commit afe6f66

Please sign in to comment.