Commit f9fa54cd by olly Committed by Oliver Woodman

Have seekTo return new position directly.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=125476407
parent e71cdb1b
...@@ -377,8 +377,8 @@ import java.util.concurrent.atomic.AtomicInteger; ...@@ -377,8 +377,8 @@ import java.util.concurrent.atomic.AtomicInteger;
TraceUtil.beginSection("doSomeWork"); TraceUtil.beginSection("doSomeWork");
if (enabledRenderers.length > 0) { if (enabledRenderers.length > 0) {
// Process reset if there is one, else update the position. // Process a source discontinuity if there is one, else update the position.
if (!checkForSourceResetInternal()) { if (!checkForSourceDiscontinuityInternal()) {
updatePositionUs(); updatePositionUs();
sampleSource = timeline.getSampleSource(); sampleSource = timeline.getSampleSource();
} }
...@@ -442,21 +442,21 @@ import java.util.concurrent.atomic.AtomicInteger; ...@@ -442,21 +442,21 @@ import java.util.concurrent.atomic.AtomicInteger;
} }
} }
private void seekToInternal(int sourceIndex, long positionMs) throws ExoPlaybackException { private void seekToInternal(int sourceIndex, long seekPositionMs) throws ExoPlaybackException {
try { try {
if (positionMs == (positionUs / 1000)) { if (seekPositionMs == (positionUs / 1000)) {
// Seek is to the current position. Do nothing. // Seek is to the current position. Do nothing.
return; return;
} }
long seekPositionUs = seekPositionMs * 1000;
rebuffering = false; rebuffering = false;
positionUs = positionMs * 1000;
internalPositionUs = sourceOffsetUs + positionUs;
standaloneMediaClock.stop(); standaloneMediaClock.stop();
standaloneMediaClock.setPositionUs(internalPositionUs);
sampleSource = timeline.seekTo(sourceIndex, positionUs); sampleSource = timeline.seekToSource(sourceIndex);
if (sampleSource == null) { if (sampleSource == null) {
// The source isn't prepared. // The source isn't prepared.
setNewSourcePositionInternal(seekPositionUs);
return; return;
} }
...@@ -464,9 +464,10 @@ import java.util.concurrent.atomic.AtomicInteger; ...@@ -464,9 +464,10 @@ import java.util.concurrent.atomic.AtomicInteger;
for (TrackRenderer renderer : enabledRenderers) { for (TrackRenderer renderer : enabledRenderers) {
ensureStopped(renderer); ensureStopped(renderer);
} }
checkForSourceResetInternal(); seekPositionUs = sampleSource.seekToUs(seekPositionUs);
} }
setNewSourcePositionInternal(seekPositionUs);
resumeInternal(); resumeInternal();
} finally { } finally {
pendingSeekCount.decrementAndGet(); pendingSeekCount.decrementAndGet();
...@@ -496,17 +497,22 @@ import java.util.concurrent.atomic.AtomicInteger; ...@@ -496,17 +497,22 @@ import java.util.concurrent.atomic.AtomicInteger;
handler.sendEmptyMessage(MSG_DO_SOME_WORK); handler.sendEmptyMessage(MSG_DO_SOME_WORK);
} }
private boolean checkForSourceResetInternal() throws ExoPlaybackException { private boolean checkForSourceDiscontinuityInternal() throws ExoPlaybackException {
long resetPositionUs = sampleSource.readReset(); long newSourcePositionUs = sampleSource.readDiscontinuity();
if (resetPositionUs == C.UNSET_TIME_US) { if (newSourcePositionUs == C.UNSET_TIME_US) {
return false; return false;
} }
internalPositionUs = sourceOffsetUs + resetPositionUs; setNewSourcePositionInternal(newSourcePositionUs);
return true;
}
private void setNewSourcePositionInternal(long sourcePositionUs) throws ExoPlaybackException {
positionUs = sourcePositionUs;
internalPositionUs = sourceOffsetUs + sourcePositionUs;
standaloneMediaClock.setPositionUs(internalPositionUs); standaloneMediaClock.setPositionUs(internalPositionUs);
for (TrackRenderer renderer : enabledRenderers) { for (TrackRenderer renderer : enabledRenderers) {
renderer.reset(internalPositionUs); renderer.reset(internalPositionUs);
} }
return true;
} }
private void stopInternal() { private void stopInternal() {
...@@ -672,8 +678,6 @@ import java.util.concurrent.atomic.AtomicInteger; ...@@ -672,8 +678,6 @@ import java.util.concurrent.atomic.AtomicInteger;
playingSourceEndPositionUs = C.UNSET_TIME_US; playingSourceEndPositionUs = C.UNSET_TIME_US;
} else if (playingSource.nextSource != null && playingSource.nextSource.prepared) { } else if (playingSource.nextSource != null && playingSource.nextSource.prepared) {
readingSource = playingSource.nextSource; readingSource = playingSource.nextSource;
// Suppress reading a reset so that the transition can be seamless.
readingSource.sampleSource.readReset();
// Replace enabled renderers' TrackStreams if they will continue to be enabled when the // Replace enabled renderers' TrackStreams if they will continue to be enabled when the
// new source starts playing, so that the transition can be seamless. // new source starts playing, so that the transition can be seamless.
TrackSelectionArray newTrackSelections = readingSource.trackSelections; TrackSelectionArray newTrackSelections = readingSource.trackSelections;
...@@ -711,7 +715,7 @@ import java.util.concurrent.atomic.AtomicInteger; ...@@ -711,7 +715,7 @@ import java.util.concurrent.atomic.AtomicInteger;
return playingSource.sampleSource; return playingSource.sampleSource;
} }
public SampleSource seekTo(int sourceIndex, long sourcePositionUs) throws ExoPlaybackException { public SampleSource seekToSource(int sourceIndex) throws ExoPlaybackException {
// Clear the timeline, but keep the requested source if it is already prepared. // Clear the timeline, but keep the requested source if it is already prepared.
Source source = playingSource; Source source = playingSource;
Source newPlayingSource = null; Source newPlayingSource = null;
...@@ -729,10 +733,8 @@ import java.util.concurrent.atomic.AtomicInteger; ...@@ -729,10 +733,8 @@ import java.util.concurrent.atomic.AtomicInteger;
setPlayingSource(newPlayingSource, sourceOffsetUs); setPlayingSource(newPlayingSource, sourceOffsetUs);
bufferingSource = playingSource; bufferingSource = playingSource;
bufferingSourceOffsetUs = sourceOffsetUs; bufferingSourceOffsetUs = sourceOffsetUs;
if (playingSource.hasEnabledTracks) {
sampleSource.seekToUs(sourcePositionUs);
}
} else { } else {
// TODO[REFACTOR]: Presumably we need to disable the renderers somewhere in here?
playingSource = null; playingSource = null;
readingSource = null; readingSource = null;
bufferingSource = null; bufferingSource = null;
......
...@@ -124,17 +124,14 @@ public final class MultiSampleSource implements SampleSource { ...@@ -124,17 +124,14 @@ public final class MultiSampleSource implements SampleSource {
} }
@Override @Override
public long readReset() { public long readDiscontinuity() {
long resetPositionUs = C.UNSET_TIME_US;
for (SampleSource source : enabledSources) { for (SampleSource source : enabledSources) {
long childResetPositionUs = source.readReset(); if (source.readDiscontinuity() != C.UNSET_TIME_US) {
if (resetPositionUs == C.UNSET_TIME_US) { // Children are not allowed to report discontinuities.
resetPositionUs = childResetPositionUs; throw new IllegalStateException("Child reported discontinuity");
} else if (childResetPositionUs != C.UNSET_TIME_US) {
resetPositionUs = Math.min(resetPositionUs, childResetPositionUs);
} }
} }
return resetPositionUs; return C.UNSET_TIME_US;
} }
@Override @Override
...@@ -150,10 +147,15 @@ public final class MultiSampleSource implements SampleSource { ...@@ -150,10 +147,15 @@ public final class MultiSampleSource implements SampleSource {
} }
@Override @Override
public void seekToUs(long positionUs) { public long seekToUs(long positionUs) {
for (SampleSource source : enabledSources) { positionUs = enabledSources[0].seekToUs(positionUs);
source.seekToUs(positionUs); for (int i = 1; i < enabledSources.length; i++) {
// Additional sources must seek to the same position.
if (enabledSources[i].seekToUs(positionUs) != positionUs) {
throw new IllegalStateException("Children seeked to different positions");
}
} }
return positionUs;
} }
@Override @Override
......
...@@ -88,12 +88,15 @@ public interface SampleSource { ...@@ -88,12 +88,15 @@ public interface SampleSource {
void continueBuffering(long positionUs); void continueBuffering(long positionUs);
/** /**
* Attempts to read a pending reset. * Attempts to read a discontinuity.
* <p>
* After this method has returned a value other than {@link C#UNSET_TIME_US}, all
* {@link TrackStream}s provided by the source are guaranteed to start from a key frame.
* *
* @return If a reset was read then the playback position in microseconds after the reset. Else * @return If a discontinuity was read then the playback position in microseconds after the
* {@link C#UNSET_TIME_US}. * discontinuity. Else {@link C#UNSET_TIME_US}.
*/ */
long readReset(); long readDiscontinuity();
/** /**
* Returns an estimate of the position up to which data is buffered for the enabled tracks. * Returns an estimate of the position up to which data is buffered for the enabled tracks.
...@@ -106,13 +109,17 @@ public interface SampleSource { ...@@ -106,13 +109,17 @@ public interface SampleSource {
long getBufferedPositionUs(); long getBufferedPositionUs();
/** /**
* Seeks to the specified position in microseconds. * Attempts to seek to the specified position in microseconds.
* <p>
* After this method has been called, all {@link TrackStream}s provided by the source are
* guaranteed to start from a key frame.
* <p> * <p>
* This method should only be called when at least one track is selected. * This method should only be called when at least one track is selected.
* *
* @param positionUs The seek position in microseconds. * @param positionUs The seek position in microseconds.
* @return The actual position to which the source was seeked, in microseconds.
*/ */
void seekToUs(long positionUs); long seekToUs(long positionUs);
/** /**
* Releases the source. * Releases the source.
......
...@@ -74,7 +74,6 @@ public final class SingleSampleSource implements SampleSource, TrackStream, ...@@ -74,7 +74,6 @@ public final class SingleSampleSource implements SampleSource, TrackStream,
private final EventListener eventListener; private final EventListener eventListener;
private final int eventSourceId; private final int eventSourceId;
private long pendingResetPositionUs;
private boolean loadingFinished; private boolean loadingFinished;
private int streamState; private int streamState;
...@@ -140,7 +139,6 @@ public final class SingleSampleSource implements SampleSource, TrackStream, ...@@ -140,7 +139,6 @@ public final class SingleSampleSource implements SampleSource, TrackStream,
if (!newSelections.isEmpty()) { if (!newSelections.isEmpty()) {
newStreams[0] = this; newStreams[0] = this;
streamState = STREAM_STATE_SEND_FORMAT; streamState = STREAM_STATE_SEND_FORMAT;
pendingResetPositionUs = C.UNSET_TIME_US;
maybeStartLoading(); maybeStartLoading();
} }
return newStreams; return newStreams;
...@@ -157,11 +155,11 @@ public final class SingleSampleSource implements SampleSource, TrackStream, ...@@ -157,11 +155,11 @@ public final class SingleSampleSource implements SampleSource, TrackStream,
} }
@Override @Override
public void seekToUs(long positionUs) { public long seekToUs(long positionUs) {
if (streamState == STREAM_STATE_END_OF_STREAM) { if (streamState == STREAM_STATE_END_OF_STREAM) {
pendingResetPositionUs = positionUs;
streamState = STREAM_STATE_SEND_SAMPLE; streamState = STREAM_STATE_SEND_SAMPLE;
} }
return positionUs;
} }
@Override @Override
...@@ -182,10 +180,8 @@ public final class SingleSampleSource implements SampleSource, TrackStream, ...@@ -182,10 +180,8 @@ public final class SingleSampleSource implements SampleSource, TrackStream,
} }
@Override @Override
public long readReset() { public long readDiscontinuity() {
long resetPositionUs = pendingResetPositionUs; return C.UNSET_TIME_US;
pendingResetPositionUs = C.UNSET_TIME_US;
return resetPositionUs;
} }
@Override @Override
......
...@@ -92,8 +92,6 @@ public final class DashSampleSource implements SampleSource { ...@@ -92,8 +92,6 @@ public final class DashSampleSource implements SampleSource {
private long elapsedRealtimeOffset; private long elapsedRealtimeOffset;
private TrackGroupArray trackGroups; private TrackGroupArray trackGroups;
private int[] trackGroupAdaptationSetIndices; private int[] trackGroupAdaptationSetIndices;
private boolean pendingReset;
private long lastSeekPositionUs;
private ChunkTrackStream<DashChunkSource>[] trackStreams; private ChunkTrackStream<DashChunkSource>[] trackStreams;
public DashSampleSource(Uri manifestUri, DataSourceFactory dataSourceFactory, public DashSampleSource(Uri manifestUri, DataSourceFactory dataSourceFactory,
...@@ -184,14 +182,7 @@ public final class DashSampleSource implements SampleSource { ...@@ -184,14 +182,7 @@ public final class DashSampleSource implements SampleSource {
} }
@Override @Override
public long readReset() { public long readDiscontinuity() {
if (pendingReset) {
pendingReset = false;
for (ChunkTrackStream<DashChunkSource> trackStream : trackStreams) {
trackStream.setReadingEnabled(true);
}
return lastSeekPositionUs;
}
return C.UNSET_TIME_US; return C.UNSET_TIME_US;
} }
...@@ -208,13 +199,11 @@ public final class DashSampleSource implements SampleSource { ...@@ -208,13 +199,11 @@ public final class DashSampleSource implements SampleSource {
} }
@Override @Override
public void seekToUs(long positionUs) { public long seekToUs(long positionUs) {
lastSeekPositionUs = positionUs;
pendingReset = true;
for (ChunkTrackStream<DashChunkSource> trackStream : trackStreams) { for (ChunkTrackStream<DashChunkSource> trackStream : trackStreams) {
trackStream.setReadingEnabled(false);
trackStream.seekToUs(positionUs); trackStream.seekToUs(positionUs);
} }
return positionUs;
} }
@Override @Override
......
...@@ -388,7 +388,7 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu ...@@ -388,7 +388,7 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu
} }
@Override @Override
public long readReset() { public long readDiscontinuity() {
if (notifyReset) { if (notifyReset) {
notifyReset = false; notifyReset = false;
return lastSeekPositionUs; return lastSeekPositionUs;
...@@ -410,7 +410,7 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu ...@@ -410,7 +410,7 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu
} }
@Override @Override
public void seekToUs(long positionUs) { public long seekToUs(long positionUs) {
// Treat all seeks into non-seekable media as being to t=0. // Treat all seeks into non-seekable media as being to t=0.
positionUs = seekMap.isSeekable() ? positionUs : 0; positionUs = seekMap.isSeekable() ? positionUs : 0;
lastSeekPositionUs = positionUs; lastSeekPositionUs = positionUs;
...@@ -426,6 +426,7 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu ...@@ -426,6 +426,7 @@ public final class ExtractorSampleSource implements SampleSource, ExtractorOutpu
if (!seekInsideBuffer) { if (!seekInsideBuffer) {
restartFrom(positionUs); restartFrom(positionUs);
} }
return positionUs;
} }
@Override @Override
......
...@@ -80,8 +80,6 @@ public final class HlsSampleSource implements SampleSource, ...@@ -80,8 +80,6 @@ public final class HlsSampleSource implements SampleSource,
private int[] selectedTrackCounts; private int[] selectedTrackCounts;
private HlsTrackStreamWrapper[] trackStreamWrappers; private HlsTrackStreamWrapper[] trackStreamWrappers;
private HlsTrackStreamWrapper[] enabledTrackStreamWrappers; private HlsTrackStreamWrapper[] enabledTrackStreamWrappers;
private boolean pendingReset;
private long lastSeekPositionUs;
public HlsSampleSource(Uri manifestUri, DataSourceFactory dataSourceFactory, public HlsSampleSource(Uri manifestUri, DataSourceFactory dataSourceFactory,
BandwidthMeter bandwidthMeter, Handler eventHandler, BandwidthMeter bandwidthMeter, Handler eventHandler,
...@@ -192,14 +190,7 @@ public final class HlsSampleSource implements SampleSource, ...@@ -192,14 +190,7 @@ public final class HlsSampleSource implements SampleSource,
} }
@Override @Override
public long readReset() { public long readDiscontinuity() {
if (pendingReset) {
pendingReset = false;
for (HlsTrackStreamWrapper trackStreamWrapper : trackStreamWrappers) {
trackStreamWrapper.setReadingEnabled(true);
}
return lastSeekPositionUs;
}
return C.UNSET_TIME_US; return C.UNSET_TIME_US;
} }
...@@ -216,16 +207,14 @@ public final class HlsSampleSource implements SampleSource, ...@@ -216,16 +207,14 @@ public final class HlsSampleSource implements SampleSource,
} }
@Override @Override
public void seekToUs(long positionUs) { public long seekToUs(long positionUs) {
// Treat all seeks into non-seekable media as being to t=0. // Treat all seeks into non-seekable media as being to t=0.
positionUs = isLive ? 0 : positionUs; positionUs = isLive ? 0 : positionUs;
lastSeekPositionUs = positionUs;
pendingReset = true;
timestampAdjusterProvider.reset(); timestampAdjusterProvider.reset();
for (HlsTrackStreamWrapper trackStreamWrapper : enabledTrackStreamWrappers) { for (HlsTrackStreamWrapper trackStreamWrapper : enabledTrackStreamWrappers) {
trackStreamWrapper.setReadingEnabled(false);
trackStreamWrapper.restartFrom(positionUs); trackStreamWrapper.restartFrom(positionUs);
} }
return positionUs;
} }
@Override @Override
......
...@@ -81,8 +81,6 @@ public final class SmoothStreamingSampleSource implements SampleSource, ...@@ -81,8 +81,6 @@ public final class SmoothStreamingSampleSource implements SampleSource,
private TrackEncryptionBox[] trackEncryptionBoxes; private TrackEncryptionBox[] trackEncryptionBoxes;
private TrackGroupArray trackGroups; private TrackGroupArray trackGroups;
private int[] trackGroupElementIndices; private int[] trackGroupElementIndices;
private boolean pendingReset;
private long lastSeekPositionUs;
private ChunkTrackStream<SmoothStreamingChunkSource>[] trackStreams; private ChunkTrackStream<SmoothStreamingChunkSource>[] trackStreams;
...@@ -171,14 +169,7 @@ public final class SmoothStreamingSampleSource implements SampleSource, ...@@ -171,14 +169,7 @@ public final class SmoothStreamingSampleSource implements SampleSource,
} }
@Override @Override
public long readReset() { public long readDiscontinuity() {
if (pendingReset) {
pendingReset = false;
for (ChunkTrackStream<SmoothStreamingChunkSource> trackStream : trackStreams) {
trackStream.setReadingEnabled(true);
}
return lastSeekPositionUs;
}
return C.UNSET_TIME_US; return C.UNSET_TIME_US;
} }
...@@ -195,13 +186,11 @@ public final class SmoothStreamingSampleSource implements SampleSource, ...@@ -195,13 +186,11 @@ public final class SmoothStreamingSampleSource implements SampleSource,
} }
@Override @Override
public void seekToUs(long positionUs) { public long seekToUs(long positionUs) {
lastSeekPositionUs = positionUs;
pendingReset = true;
for (ChunkTrackStream<SmoothStreamingChunkSource> trackStream : trackStreams) { for (ChunkTrackStream<SmoothStreamingChunkSource> trackStream : trackStreams) {
trackStream.setReadingEnabled(false);
trackStream.seekToUs(positionUs); trackStream.seekToUs(positionUs);
} }
return positionUs;
} }
@Override @Override
......
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