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 30, 2024
1 parent 03d35e3 commit 173dbc8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ public TrackGroup getTrackGroup() {
return trackGroup;
}

/** Returns if the chunk source has independent segments. */
public boolean hasIndependentSegments() {
return independentSegments;
}

/**
* Sets the current track selection.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,21 @@ 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;
if (chunkSource.hasIndependentSegments()) {
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 +1483,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 173dbc8

Please sign in to comment.