From c8c8cf214ccb458af9642bfad55705b3e56d72c4 Mon Sep 17 00:00:00 2001 From: Vanilagy <1696106+Vanilagy@users.noreply.github.com> Date: Fri, 17 Apr 2026 15:06:18 +0200 Subject: [PATCH] Add first timestamp cache for performance --- src/segmented-input.ts | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/segmented-input.ts b/src/segmented-input.ts index dc6ce0b..a82988d 100644 --- a/src/segmented-input.ts +++ b/src/segmented-input.ts @@ -70,6 +70,8 @@ export abstract class SegmentedInput { firstSegment: Segment | null = null; firstSegmentFirstTimestamps = new WeakMap(); + firstTimestampCache = new WeakMap(); + constructor(input: Input, path: string, trackDeclarations: SegmentedInputTrackDeclaration[] | null) { this.input = input; this.path = path; @@ -152,6 +154,19 @@ export abstract class SegmentedInput { })(); } + // This operation is done a lot and can be semi-expensive, so it's good to have a cache for it + async getFirstTimestampForInput(input: Input) { + const existing = this.firstTimestampCache.get(input); + if (existing !== undefined) { + return existing; + } + + const firstTimestamp = await input.getFirstTimestamp(); + this.firstTimestampCache.set(input, firstTimestamp); + + return firstTimestamp; + } + async getMediaOffset(segment: Segment, input: Input) { const firstSegment = segment.firstSegment ?? segment; @@ -160,7 +175,7 @@ export abstract class SegmentedInput { firstSegmentFirstTimestamp = this.firstSegmentFirstTimestamps.get(firstSegment)!; } else { const firstInput = this.getInputForSegment(firstSegment); - firstSegmentFirstTimestamp = await firstInput.getFirstTimestamp(); + firstSegmentFirstTimestamp = await this.getFirstTimestampForInput(firstInput); this.firstSegmentFirstTimestamps.set(firstSegment, firstSegmentFirstTimestamp); } @@ -168,7 +183,7 @@ export abstract class SegmentedInput { return firstSegment.timestamp - firstSegmentFirstTimestamp; } - const segmentFirstTimestamp = await input.getFirstTimestamp(); + const segmentFirstTimestamp = await this.getFirstTimestampForInput(input); const segmentElapsed = segment.timestamp - firstSegment.timestamp; const inputElapsed = segmentFirstTimestamp - firstSegmentFirstTimestamp; const difference = inputElapsed - segmentElapsed; @@ -403,6 +418,14 @@ class SegmentedInputInputTrackBacking implements InputTrackBacking { return null; } + const segmentAfterThat = await this.segmentedInput.getNextSegment(nextSegment, { + skipLiveWait: options.skipLiveWait, + }); + if (segmentAfterThat) { + const input = this.segmentedInput.getInputForSegment(segmentAfterThat); + void input.getTracks(); + } + return this.createAdjustedPacket(firstPacket, nextSegment, nextTrack); } }