Skip to content

Commit

Permalink
Calculate ExtractorSampleSource duration if unknown (playlists #4).
Browse files Browse the repository at this point in the history
When buffering playlist items after the currently-playing one,
continueBuffering should take a position that is negative because the playback
position is before the start of the source being buffered. This change makes
sure that ExtractorSampleSources always have a known duration when they are
fully buffered, which means that the correct (negative) source-relative
position can be calculated.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=124949409
  • Loading branch information
andrewlewis authored and ojw28 committed Jun 15, 2016
1 parent e6267cd commit 66a5c96
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ private void doSomeWork() throws ExoPlaybackException, IOException {
long operationStartTimeMs = SystemClock.elapsedRealtime();
if (sampleSource == null) {
timeline.updateSources();
sampleSource = timeline.getSampleSource(internalPositionUs);
sampleSource = timeline.getSampleSource();
if (sampleSource != null) {
resumeInternal();
} else {
Expand All @@ -380,7 +380,7 @@ private void doSomeWork() throws ExoPlaybackException, IOException {
// Process reset if there is one, else update the position.
if (!checkForSourceResetInternal()) {
updatePositionUs();
sampleSource = timeline.getSampleSource(internalPositionUs);
sampleSource = timeline.getSampleSource();
}
updateBufferedPositionUs();
timeline.updateSources();
Expand All @@ -405,17 +405,17 @@ private void doSomeWork() throws ExoPlaybackException, IOException {
allRenderersReadyOrEnded = allRenderersReadyOrEnded && rendererReadyOrEnded;
}

boolean timelineIsReady = timeline.isReady();
if (allRenderersEnded && (durationUs == C.UNSET_TIME_US || durationUs <= positionUs)) {
setState(ExoPlayer.STATE_ENDED);
stopRenderers();
} else if (state == ExoPlayer.STATE_BUFFERING && allRenderersReadyOrEnded
&& haveSufficientBuffer() && timeline.isReady(internalPositionUs)) {
&& haveSufficientBuffer() && timelineIsReady) {
setState(ExoPlayer.STATE_READY);
if (playWhenReady) {
startRenderers();
}
} else if (state == ExoPlayer.STATE_READY && (!allRenderersReadyOrEnded
|| !timeline.isReady(internalPositionUs))) {
} else if (state == ExoPlayer.STATE_READY && (!allRenderersReadyOrEnded || !timelineIsReady)) {
rebuffering = playWhenReady;
setState(ExoPlayer.STATE_BUFFERING);
stopRenderers();
Expand Down Expand Up @@ -485,9 +485,8 @@ private void resumeInternal() throws ExoPlaybackException {
if (allRenderersEnded && (durationUs == C.UNSET_TIME_US || durationUs <= positionUs)) {
setState(ExoPlayer.STATE_ENDED);
} else {
setState(allRenderersReadyOrEnded && haveSufficientBuffer()
&& timeline.isReady(internalPositionUs) ? ExoPlayer.STATE_READY
: ExoPlayer.STATE_BUFFERING);
setState(allRenderersReadyOrEnded && haveSufficientBuffer() && timeline.isReady()
? ExoPlayer.STATE_READY : ExoPlayer.STATE_BUFFERING);
}

// Start the renderers if ready, and schedule the first piece of work.
Expand Down Expand Up @@ -596,7 +595,7 @@ private final class Timeline {
private Source bufferingSource;

private long playingSourceEndPositionUs;
private long nextSourceOffsetUs;
private long bufferingSourceOffsetUs;

public Timeline() {
rendererWasEnabledFlags = new boolean[renderers.length];
Expand All @@ -620,6 +619,7 @@ public void updateSources() throws ExoPlaybackException, IOException {
Source newSource = new Source(sampleSource, index, renderers.length);
if (bufferingSource != null) {
bufferingSource.nextSource = newSource;
bufferingSourceOffsetUs += bufferingSource.sampleSource.getDurationUs();
}
bufferingSource = newSource;
}
Expand All @@ -637,23 +637,14 @@ public void updateSources() throws ExoPlaybackException, IOException {
bufferingSource.selectTracks(result.first, result.second, startPositionUs);
if (playingSource == null) {
// This is the first prepared source, so start playing it.
sourceOffsetUs = 0;
setPlayingSource(bufferingSource);
setPlayingSource(bufferingSource, 0);
}
}
}

if (bufferingSource.hasEnabledTracks) {
long bufferingPositionUs;
if (bufferingSource == playingSource) {
bufferingPositionUs = internalPositionUs - sourceOffsetUs;
} else if (bufferingSource == readingSource) {
// TODO[playlists]: Make sure continueBuffering supports a negative downstream position.
bufferingPositionUs = internalPositionUs - nextSourceOffsetUs;
} else {
bufferingPositionUs = 0;
}
bufferingSource.sampleSource.continueBuffering(bufferingPositionUs);
long sourcePositionUs = internalPositionUs - bufferingSourceOffsetUs;
bufferingSource.sampleSource.continueBuffering(sourcePositionUs);
}
}

Expand All @@ -669,22 +660,7 @@ public void updateSources() throws ExoPlaybackException, IOException {
}
}
if (playingSourceEndPositionUs == C.UNSET_TIME_US) {
// Calculate the next source's start position in the timeline.
long playingSourceDurationUs = playingSource.sampleSource.getDurationUs();
if (playingSourceDurationUs == C.UNSET_TIME_US) {
// The duration of the current source is unknown, so use the maximum rendered timestamp
// plus a small extra offset to make sure that renderers don't read two buffers with the
// same timestamp.
playingSourceEndPositionUs = 0;
for (TrackRenderer renderer : enabledRenderers) {
playingSourceEndPositionUs =
Math.max(playingSourceEndPositionUs, renderer.getMaximumTimeUs());
}
nextSourceOffsetUs = playingSourceEndPositionUs + 10000;
} else {
playingSourceEndPositionUs = sourceOffsetUs + playingSourceDurationUs;
nextSourceOffsetUs = playingSourceEndPositionUs;
}
playingSourceEndPositionUs = sourceOffsetUs + playingSource.sampleSource.getDurationUs();
}
if (sourceCount != SampleSourceProvider.UNKNOWN_SOURCE_COUNT
&& readingSource.index == sourceCount - 1) {
Expand All @@ -694,9 +670,7 @@ public void updateSources() throws ExoPlaybackException, IOException {
}
readingSource = null;
playingSourceEndPositionUs = C.UNSET_TIME_US;
return;
}
if (playingSource.nextSource != null && playingSource.nextSource.prepared) {
} else if (playingSource.nextSource != null && playingSource.nextSource.prepared) {
readingSource = playingSource.nextSource;
// Suppress reading a reset so that the transition can be seamless.
readingSource.sampleSource.readReset();
Expand All @@ -713,27 +687,26 @@ public void updateSources() throws ExoPlaybackException, IOException {
for (int j = 0; j < formats.length; j++) {
formats[j] = groups.get(selection.group).getFormat(selection.getTrack(j));
}
renderer.replaceTrackStream(formats, readingSource.trackStreams[i], nextSourceOffsetUs);
renderer.replaceTrackStream(formats, readingSource.trackStreams[i],
playingSourceEndPositionUs);
}
}
}
}

public boolean isReady(long positionUs) {
public boolean isReady() {
return playingSourceEndPositionUs == C.UNSET_TIME_US
|| positionUs < playingSourceEndPositionUs || playingSource.nextSource != null;
|| internalPositionUs < playingSourceEndPositionUs || playingSource.nextSource != null;
}

public SampleSource getSampleSource(long positionUs) throws ExoPlaybackException {
public SampleSource getSampleSource() throws ExoPlaybackException {
if (playingSource == null) {
return null;
}
if (readingSource != playingSource && playingSourceEndPositionUs != C.UNSET_TIME_US
&& positionUs >= playingSourceEndPositionUs) {
// Renderers are playing the next source, so update the timeline.
&& internalPositionUs >= playingSourceEndPositionUs) {
playingSource.release();
sourceOffsetUs = nextSourceOffsetUs;
setPlayingSource(readingSource);
setPlayingSource(readingSource, playingSourceEndPositionUs);
}
return playingSource.sampleSource;
}
Expand All @@ -753,15 +726,17 @@ public SampleSource seekTo(int sourceIndex, long sourcePositionUs) throws ExoPla
if (newPlayingSource != null) {
nextSourceIndex = sourceIndex + 1;
newPlayingSource.nextSource = null;
setPlayingSource(newPlayingSource);
setPlayingSource(newPlayingSource, sourceOffsetUs);
bufferingSource = playingSource;
bufferingSourceOffsetUs = sourceOffsetUs;
if (playingSource.hasEnabledTracks) {
sampleSource.seekToUs(sourcePositionUs);
}
} else {
playingSource = null;
readingSource = null;
bufferingSource = null;
bufferingSourceOffsetUs = 0;
durationUs = C.UNSET_TIME_US;
sampleSource = null;
// Set the next source index so that the required source is created in updateSources.
Expand Down Expand Up @@ -803,6 +778,7 @@ public void reselectTracks() throws ExoPlaybackException {
bufferingSource = playingSource;
nextSourceIndex = playingSource.index + 1;
playingSourceEndPositionUs = C.UNSET_TIME_US;
bufferingSourceOffsetUs = sourceOffsetUs;

// Update the track selection for the playing source.
Pair<TrackSelectionArray, Object> result =
Expand Down Expand Up @@ -842,10 +818,10 @@ public void reset() {
readingSource = null;
bufferingSource = null;
durationUs = C.UNSET_TIME_US;
playingSourceEndPositionUs = C.UNSET_TIME_US;
nextSourceIndex = 0;
sourceOffsetUs = 0;
playingSourceEndPositionUs = C.UNSET_TIME_US;
nextSourceOffsetUs = 0;
bufferingSourceOffsetUs = 0;
}

@Override
Expand All @@ -864,8 +840,8 @@ public String toString() {
return sb.toString();
}

private void setPlayingSource(Source source) throws ExoPlaybackException {
playingSourceEndPositionUs = C.UNSET_TIME_US;
private void setPlayingSource(Source source, long offsetUs) throws ExoPlaybackException {
sourceOffsetUs = offsetUs;
durationUs = source.sampleSource.getDurationUs();

// Disable/enable renderers for the new source.
Expand All @@ -875,6 +851,7 @@ private void setPlayingSource(Source source) throws ExoPlaybackException {
}
readingSource = source;
playingSource = source;
playingSourceEndPositionUs = C.UNSET_TIME_US;
enableRenderers(source.trackSelections, enabledRendererCount);

// Update the timeline position for the new source index.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ public interface SampleSource {
boolean prepare(long positionUs) throws IOException;

/**
* Returns the duration of the source.
* Returns the duration of the source in microseconds, or {@link C#UNSET_TIME_US} if not known.
* <p>
* If {@link #getBufferedPositionUs()} returns {@link C#END_OF_SOURCE_US}, the duration is
* guaranteed to be known.
* <p>
* This method should only be called after the source has been prepared.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public abstract class TrackRenderer implements ExoPlayerComponent {
private int state;
private TrackStream stream;
private long streamOffsetUs;
private long maximumTimeUs;
private boolean readEndOfStream;
private boolean streamIsFinal;

Expand Down Expand Up @@ -235,7 +234,6 @@ protected void onStreamChanged(Format[] formats) throws ExoPlaybackException {
*/
/* package */ final void reset(long positionUs) throws ExoPlaybackException {
streamIsFinal = false;
maximumTimeUs = C.UNSET_TIME_US;
onReset(positionUs, false);
}

Expand All @@ -260,14 +258,6 @@ protected void onReset(long positionUs, boolean joining) throws ExoPlaybackExcep
return readEndOfStream;
}

/**
* Returns the maximum buffer timestamp read from the stream since the last reset, or
* {@link C#UNSET_TIME_US} if no buffers have been read.
*/
/* package */ final long getMaximumTimeUs() {
return maximumTimeUs;
}

/**
* Signals to the renderer that the current {@link TrackStream} will be the final one supplied
* before it is next disabled or reset.
Expand Down Expand Up @@ -372,9 +362,6 @@ protected final int readSource(FormatHolder formatHolder, DecoderInputBuffer buf
return streamIsFinal ? TrackStream.BUFFER_READ : TrackStream.NOTHING_READ;
}
buffer.timeUs += streamOffsetUs;
if (buffer.timeUs > maximumTimeUs) {
maximumTimeUs = buffer.timeUs;
}
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ public UnrecognizedInputFormatException(Extractor[] extractors) {

private static final int MIN_RETRY_COUNT_DEFAULT_FOR_MEDIA = -1;

/**
* When the source's duration is unknown, it is calculated by adding this value to the largest
* sample timestamp seen when buffering completes.
*/
private static final long DEFAULT_LAST_SAMPLE_DURATION_US = 10000;

// Lazily initialized default extractor classes in priority order.
private static List<Class<? extends Extractor>> defaultExtractorClasses;

Expand Down Expand Up @@ -397,11 +403,7 @@ public long getBufferedPositionUs() {
} else if (isPendingReset()) {
return pendingResetPositionUs;
} else {
long largestQueuedTimestampUs = Long.MIN_VALUE;
for (DefaultTrackOutput sampleQueue : sampleQueues) {
largestQueuedTimestampUs = Math.max(largestQueuedTimestampUs,
sampleQueue.getLargestQueuedTimestampUs());
}
long largestQueuedTimestampUs = getLargestQueuedTimestampUs();
return largestQueuedTimestampUs == Long.MIN_VALUE ? lastSeekPositionUs
: largestQueuedTimestampUs;
}
Expand Down Expand Up @@ -459,6 +461,11 @@ public void onLoadCompleted(ExtractingLoadable loadable, long elapsedRealtimeMs,
long loadDurationMs) {
copyLengthFromLoader(loadable);
loadingFinished = true;
if (durationUs == C.UNSET_TIME_US) {
long largestQueuedTimestampUs = getLargestQueuedTimestampUs();
durationUs = largestQueuedTimestampUs == Long.MIN_VALUE ? 0
: largestQueuedTimestampUs + DEFAULT_LAST_SAMPLE_DURATION_US;
}
}

@Override
Expand Down Expand Up @@ -579,6 +586,15 @@ private int getExtractedSamplesCount() {
return extractedSamplesCount;
}

private long getLargestQueuedTimestampUs() {
long largestQueuedTimestampUs = Long.MIN_VALUE;
for (DefaultTrackOutput sampleQueue : sampleQueues) {
largestQueuedTimestampUs = Math.max(largestQueuedTimestampUs,
sampleQueue.getLargestQueuedTimestampUs());
}
return largestQueuedTimestampUs;
}

private boolean haveFormatsForAllTracks() {
for (DefaultTrackOutput sampleQueue : sampleQueues) {
if (sampleQueue.getUpstreamFormat() == null) {
Expand Down

0 comments on commit 66a5c96

Please sign in to comment.