From b4b43f756f335ab41ffda3e4f3e33f86776d7d72 Mon Sep 17 00:00:00 2001 From: Joey Parrish Date: Wed, 4 Jan 2023 13:15:18 -0800 Subject: [PATCH] Pull out segment index updates from offsetStream --- lib/hls/hls_parser.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/hls/hls_parser.js b/lib/hls/hls_parser.js index fc442ba5944..f0a74c5742a 100644 --- a/lib/hls/hls_parser.js +++ b/lib/hls/hls_parser.js @@ -489,7 +489,11 @@ shaka.hls.HlsParser = class { // Now adjust timestamps back to begin at 0. const segmentN = segmentIndex.earliestReference(); if (segmentN) { - this.offsetStream_(streamInfo, -segmentN.startTime); + const streamOffset = -segmentN.startTime; + // Modify all SegmentReferences equally. + streamInfo.stream.segmentIndex.offset(streamOffset); + // Update other parts of streamInfo the same way. + this.offsetStreamInfo_(streamInfo, streamOffset); } } } @@ -531,22 +535,26 @@ shaka.hls.HlsParser = class { for (const streamInfo of this.uriToStreamInfosMap_.values()) { const segmentIndex = streamInfo.stream.segmentIndex; if (segmentIndex != null) { + // A segment's startTime should be based on its syncTime vs the lowest + // syncTime across all streams. The earliest segment sync time from + // any stream will become presentation time 0. If two streams start + // e.g. 6 seconds apart in syncTime, then their first segments will + // also start 6 seconds apart in presentation time. + const segment0 = segmentIndex.earliestReference(); if (segment0.syncTime == null) { shaka.log.alwaysError('Missing EXT-X-PROGRAM-DATE-TIME for stream', streamInfo.verbatimMediaPlaylistUri, 'Expect AV sync issues!'); } else { - // The first segment's target startTime should be based entirely on - // its syncTime. The rest of the stream will be based on that - // starting point. The earliest segment sync time from any stream - // will become presentation time 0. If two streams start e.g. 6 - // seconds apart in syncTime, then their first segments will also - // start 6 seconds apart in presentation time. + // Stream metadata are offset by a fixed amount based on the + // first segment. const segment0TargetTime = segment0.syncTime - lowestSyncTime; const streamOffset = segment0TargetTime - segment0.startTime; - this.offsetStream_(streamInfo, streamOffset); + // Modify all SegmentReferences equally. + streamInfo.stream.segmentIndex.offset(streamOffset); + this.offsetStreamInfo_(streamInfo, streamOffset); } } } @@ -557,13 +565,13 @@ shaka.hls.HlsParser = class { * @param {number} offset * @private */ - offsetStream_(streamInfo, offset) { - streamInfo.stream.segmentIndex.offset(offset); - + offsetStreamInfo_(streamInfo, offset) { + // Adjust our accounting of the maximum timestamp. streamInfo.maxTimestamp += offset; goog.asserts.assert(streamInfo.maxTimestamp >= 0, 'Negative maxTimestamp after adjustment!'); + // Update our map from sequence number to start time. const mediaSequenceToStartTime = this.getMediaSequenceToStartTimeFor_(streamInfo); for (const [key, value] of mediaSequenceToStartTime) {