Skip to content

Commit

Permalink
Prevent skipping when seeking back to segment
Browse files Browse the repository at this point in the history
  • Loading branch information
thornbill committed Oct 13, 2024
1 parent 9942fb7 commit aef9482
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/apps/stable/features/playback/utils/mediaSegmentManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import { MediaSegmentAction } from '../constants/mediaSegmentAction';

class MediaSegmentManager extends PlaybackSubscriber {
private hasSegments = false;
private lastIndex = 0;
private isLastSegmentIgnored = false;
private lastSegmentIndex = 0;
private lastTime = -1;
private mediaSegmentTypeActions: Record<Partial<MediaSegmentType>, MediaSegmentAction> | undefined;
private mediaSegments: MediaSegmentDto[] = [];

Expand All @@ -43,25 +45,34 @@ class MediaSegmentManager extends PlaybackSubscriber {

const action = this.mediaSegmentTypeActions[mediaSegment.Type];
if (action === MediaSegmentAction.Skip) {
// Perform skip
if (mediaSegment.EndTicks) {
// Ignore segment if playback progress has passed the segment's start time
if (mediaSegment.StartTicks !== undefined && this.lastTime > mediaSegment.StartTicks) {
console.info('[MediaSegmentManager] ignoring skipping segment that has been seeked back into', mediaSegment);
this.isLastSegmentIgnored = true;
return;
} else if (mediaSegment.EndTicks) {
// If there is an end time, seek to it
// Do not skip if duration < 1s to avoid slow stream changes
if (mediaSegment.StartTicks && mediaSegment.EndTicks - mediaSegment.StartTicks < TICKS_PER_SECOND) {
console.info('[MediaSegmentManager] ignoring skipping segment with duration <1s', mediaSegment);
this.isLastSegmentIgnored = true;
return;
}

console.debug('[MediaSegmentManager] skipping to %s ms', mediaSegment.EndTicks / TICKS_PER_MILLISECOND);
this.playbackManager.seek(mediaSegment.EndTicks, this.player);
} else {
// If there is no end time, skip to the next track
console.debug('[MediaSegmentManager] skipping to next item in queue');
this.playbackManager.nextTrack(this.player);
}
}
}

onPlayerPlaybackStart(_e: Event, state: PlayerState) {
this.lastIndex = 0;
this.isLastSegmentIgnored = false;
this.lastSegmentIndex = 0;
this.lastTime = -1;
this.hasSegments = !!state.MediaSource?.HasSegments;

const itemId = state.MediaSource?.Id;
Expand Down Expand Up @@ -96,16 +107,23 @@ class MediaSegmentManager extends PlaybackSubscriber {
onPlayerTimeUpdate() {
if (this.hasSegments && this.mediaSegments.length) {
const time = this.playbackManager.currentTime(this.player) * TICKS_PER_MILLISECOND;
const currentSegmentDetails = findCurrentSegment(this.mediaSegments, time, this.lastIndex);
if (currentSegmentDetails) {
const currentSegmentDetails = findCurrentSegment(this.mediaSegments, time, this.lastSegmentIndex);
if (
// The current time falls within a segment
currentSegmentDetails
// and the last segment is not ignored or the segment index has changed
&& (!this.isLastSegmentIgnored || this.lastSegmentIndex !== currentSegmentDetails.index)
) {
console.debug(
'[MediaSegmentManager] found %s segment at %s ms',
currentSegmentDetails.segment.Type,
time / TICKS_PER_MILLISECOND,
currentSegmentDetails);
this.isLastSegmentIgnored = false;
this.performAction(currentSegmentDetails.segment);
this.lastIndex = currentSegmentDetails.index;
this.lastSegmentIndex = currentSegmentDetails.index;
}
this.lastTime = time;
}
}
}
Expand Down

0 comments on commit aef9482

Please sign in to comment.