Skip to content

Commit

Permalink
fix(WebVTT): Re-add rollover logic (#7104)
Browse files Browse the repository at this point in the history
Fixes: #6448

Revert 9ab54d1
  • Loading branch information
avelad committed Aug 19, 2024
1 parent b7e543f commit 4546606
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/text/vtt_text_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,17 @@ shaka.text.VttTextParser = class {
shaka.util.Error.Code.INVALID_TEXT_HEADER);
}

const mpegTime = Number(mpegTimeMatch[1]);
let mpegTime = Number(mpegTimeMatch[1]);
const mpegTimescale = shaka.text.VttTextParser.MPEG_TIMESCALE_;

const rolloverSeconds =
shaka.text.VttTextParser.TS_ROLLOVER_ / mpegTimescale;
let segmentStart = time.segmentStart - time.periodStart;
while (segmentStart >= rolloverSeconds) {
segmentStart -= rolloverSeconds;
mpegTime += shaka.text.VttTextParser.TS_ROLLOVER_;
}

return time.periodStart + mpegTime / mpegTimescale - cueTime;
}

Expand Down Expand Up @@ -1007,6 +1015,13 @@ shaka.text.VttTextParser = class {
*/
shaka.text.VttTextParser.MPEG_TIMESCALE_ = 90000;

/**
* At this value, timestamps roll over in TS content.
* @const {number}
* @private
*/
shaka.text.VttTextParser.TS_ROLLOVER_ = 0x200000000;

/**
* @const
* @private {!RegExp}
Expand Down
53 changes: 53 additions & 0 deletions test/text/vtt_text_parser_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,59 @@ describe('VttTextParser', () => {
/* hls= */ true, /* sequenceMode= */ true);
});

it('handles timestamp rollover with X-TIMESTAMP-MAP header', () => {
verifyHelper(
[
{startTime: 95443, endTime: 95445, payload: 'Test'},
],
// 8589870000/90000 = 95443 sec, so expect every timestamp to be 95443
// seconds ahead of what is specified.
'WEBVTT\n' +
'X-TIMESTAMP-MAP=MPEGTS:8589870000,LOCAL:00:00:00.000\n\n' +
'00:00:00.000 --> 00:00:02.000 line:0\n' +
'Test',
// Non-null segmentStart takes precedence over X-TIMESTAMP-MAP.
// This protects us from rollover in the MPEGTS field.
{periodStart: 0, segmentStart: 95440, segmentEnd: 95550, vttOffset: 0},
/* hls= */ true, /* sequenceMode= */ true);

verifyHelper(
[
{startTime: 95552, endTime: 95554, payload: 'Test2'},
],
// 95550 is larger than the roll over timestamp, so the timestamp offset
// gets rolled over.
// (9745408 + 0x200000000) / 90000 = 95552 sec
'WEBVTT\n' +
'X-TIMESTAMP-MAP=MPEGTS:9745408,LOCAL:00:00:00.000\n\n' +
'00:00:00.000 --> 00:00:02.000 line:0\n' +
'Test2',
{periodStart: 0, segmentStart: 95550, segmentEnd: 95560, vttOffset: 0},
/* hls= */ true, /* sequenceMode= */ true);
});

// A mock-up of HLS live subs as seen in b/253104251.
it('handles timestamp rollover and negative offset in HLS live', () => {
// Similar to values seen in b/253104251, for a realistic regression test.
// When using sequence mode on live HLS, we get negative offsets that
// represent the timestamp of our first append in sequence mode.
verifyHelper(
[
{startTime: 3600, endTime: 3602, payload: 'Test'},
],
'WEBVTT\n' +
'X-TIMESTAMP-MAP=MPEGTS:8355814896,LOCAL:00:00:00.000\n\n' +
'00:00:00.000 --> 00:00:02.000 line:0\n' +
'Test',
{
periodStart: -1234567,
segmentStart: 3600,
segmentEnd: 3610,
vttOffset: -1234567,
},
/* hls= */ true, /* sequenceMode= */ true);
});

it('supports global style blocks', () => {
const textShadow = '-1px 0 black, 0 1px black, 1px 0 black, 0 -1px black';
verifyHelper(
Expand Down

0 comments on commit 4546606

Please sign in to comment.