Skip to content

Commit

Permalink
Seek at a chunk level when seeking to chunk start positions
Browse files Browse the repository at this point in the history
This avoids issues that can arise due to the slight discrepancies between
chunk start times (obtained from the manifest of segment index) and
the timestamps of the samples contained within those chunks.
  • Loading branch information
garethfenn committed Jan 26, 2024
1 parent b930b40 commit 2eeb54e
Showing 1 changed file with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,19 @@ public boolean seekToUs(long positionUs, boolean forceReset) {
return true;
}

// Detect whether the seek is to the start of a chunk that's at least partially buffered.
@Nullable HlsMediaChunk seekToMediaChunk = null;
for (int i = 0; i < mediaChunks.size(); i++) {
HlsMediaChunk mediaChunk = mediaChunks.get(i);
long mediaChunkStartTimeUs = mediaChunk.startTimeUs;
if (mediaChunkStartTimeUs == positionUs) {
seekToMediaChunk = mediaChunk;
break;
}
}

// If we're not forced to reset, try and seek within the buffer.
if (sampleQueuesBuilt && !forceReset && seekInsideBufferUs(positionUs)) {
if (sampleQueuesBuilt && !forceReset && seekInsideBufferUs(positionUs, seekToMediaChunk)) {
return false;
}

Expand Down Expand Up @@ -1470,13 +1481,19 @@ private boolean isPendingReset() {
* Attempts to seek to the specified position within the sample queues.
*
* @param positionUs The seek position in microseconds.
* @param chunk Optionally the chunk to seek to.
* @return Whether the in-buffer seek was successful.
*/
private boolean seekInsideBufferUs(long positionUs) {
private boolean seekInsideBufferUs(long positionUs, @Nullable HlsMediaChunk chunk) {
int sampleQueueCount = sampleQueues.length;
for (int i = 0; i < sampleQueueCount; i++) {
SampleQueue sampleQueue = sampleQueues[i];
boolean seekInsideQueue = sampleQueue.seekTo(positionUs, /* allowTimeBeyondBuffer= */ false);
boolean seekInsideQueue;
if (chunk != null) {
seekInsideQueue = sampleQueue.seekTo(chunk.getFirstSampleIndex(i));
} else {
seekInsideQueue = sampleQueue.seekTo(positionUs, /* allowTimeBeyondBuffer= */ false);
}
// If we have AV tracks then an in-queue seek is successful if the seek into every AV queue
// is successful. We ignore whether seeks within non-AV queues are successful in this case, as
// they may be sparse or poorly interleaved. If we only have non-AV tracks then a seek is
Expand Down

0 comments on commit 2eeb54e

Please sign in to comment.