Skip to content

Commit

Permalink
Pull out segment index updates from offsetStream
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyparrish committed Jan 10, 2023
1 parent 20bcc65 commit b4b43f7
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions lib/hls/hls_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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) {
Expand Down

0 comments on commit b4b43f7

Please sign in to comment.