Skip to content

Commit

Permalink
Handle initial RTSP seek
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 469143613
  • Loading branch information
claincly authored and marcbaechinger committed Oct 19, 2022
1 parent 010ecec commit c611435
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ public int read(ExtractorInput input, PositionHolder seekPosition) throws IOExce
@Override
public void seek(long nextRtpTimestamp, long playbackStartTimeUs) {
synchronized (lock) {
if (!isSeekPending) {
// Sets the isSeekPending flag, in the case preSeek() is not called, when seeking does not
// require RTSP message exchange. For example, playing back with non-zero start position.
isSeekPending = true;
}
this.nextRtpTimestamp = nextRtpTimestamp;
this.playbackStartTimeUs = playbackStartTimeUs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ public long selectTracks(
}

trackSelected = true;
if (positionUs != 0) {
// Track selection is performed only once in RTSP streams.
requestedSeekPositionUs = positionUs;
pendingSeekPositionUs = positionUs;
pendingSeekPositionUsForTcpRetry = positionUs;
}
maybeSetupTracks();
return positionUs;
}
Expand Down Expand Up @@ -271,7 +277,6 @@ public long seekToUs(long positionUs) {
// 2b.2. If RTSP PLAY (for the first seek) has not been sent, the new seek position will be
// used in the following PLAY request.

// TODO(internal: b/198620566) Handle initial seek.
// TODO(internal: b/213153670) Handle dropped seek position.
if (getBufferedPositionUs() == 0 && !isUsingRtpTcp) {
// Stores the seek position for later, if no RTP packet is received when using UDP.
Expand Down Expand Up @@ -569,7 +574,13 @@ public void onUpstreamFormatChanged(Format format) {

@Override
public void onRtspSetupCompleted() {
rtspClient.startPlayback(/* offsetMs= */ 0);
long offsetMs = 0;
if (pendingSeekPositionUs != C.TIME_UNSET) {
offsetMs = Util.usToMs(pendingSeekPositionUs);
} else if (pendingSeekPositionUsForTcpRetry != C.TIME_UNSET) {
offsetMs = Util.usToMs(pendingSeekPositionUsForTcpRetry);
}
rtspClient.startPlayback(offsetMs);
}

@Override
Expand Down Expand Up @@ -608,6 +619,7 @@ public void onPlaybackStarted(
if (isSeekPending() && pendingSeekPositionUs == requestedSeekPositionUs) {
// Seek loadable only when all pending seeks are processed, or SampleQueues will report
// inconsistent bufferedPosition.
// Seeks to the start position when the initial seek position is set.
dataLoadable.seekToUs(startPositionUs, trackTiming.rtpTimestamp);
}
}
Expand All @@ -622,7 +634,7 @@ public void onPlaybackStarted(
pendingSeekPositionUs = C.TIME_UNSET;
seekToUs(requestedSeekPositionUs);
}
} else if (pendingSeekPositionUsForTcpRetry != C.TIME_UNSET) {
} else if (pendingSeekPositionUsForTcpRetry != C.TIME_UNSET && isUsingRtpTcp) {
seekToUs(pendingSeekPositionUsForTcpRetry);
pendingSeekPositionUsForTcpRetry = C.TIME_UNSET;
}
Expand Down

0 comments on commit c611435

Please sign in to comment.