From b4f8ceaef6234f8de1d71786083ad44e53d69d8f Mon Sep 17 00:00:00 2001 From: Marcin Toczko Date: Wed, 24 Apr 2019 19:59:24 +0200 Subject: [PATCH] fix #2157 WEBVTT without X-TIMESTAMP-MAP (#2179) Avoid offsetting cue times with localTime if EXT-X-TIMESTAMP-MAP is not present --- src/utils/webvtt-parser.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/utils/webvtt-parser.js b/src/utils/webvtt-parser.js index 379a2c0e398..3077e70f1dd 100644 --- a/src/utils/webvtt-parser.js +++ b/src/utils/webvtt-parser.js @@ -13,7 +13,7 @@ const cueString2millis = function (timeString) { let hours = timeString.length > 9 ? parseInt(timeString.substr(0, timeString.indexOf(':'))) : 0; if (!Number.isFinite(ts) || !Number.isFinite(secs) || !Number.isFinite(mins) || !Number.isFinite(hours)) { - return -1; + throw Error(`Malformed X-TIMESTAMP-MAP: Local:${timeString}`); } ts += 1000 * secs; @@ -72,6 +72,7 @@ const WebVTTParser = { let cues = []; let parsingError; let inHeader = true; + let timestampMap = false; // let VTTCue = VTTCue || window.TextTrackCue; // Create parser object using VTTCue with TextTrackCue fallback on certain browsers. @@ -97,8 +98,10 @@ const WebVTTParser = { cueOffset = presentationTime - vttCCs.presentationOffset; } - cue.startTime += cueOffset - localTime; - cue.endTime += cueOffset - localTime; + if (timestampMap) { + cue.startTime += cueOffset - localTime; + cue.endTime += cueOffset - localTime; + } // Create a unique hash id for a cue based on start/end times and text. // This helps timeline-controller to avoid showing repeated captions. @@ -130,6 +133,7 @@ const WebVTTParser = { if (startsWith(line, 'X-TIMESTAMP-MAP=')) { // Once found, no more are allowed anyway, so stop searching. inHeader = false; + timestampMap = true; // Extract LOCAL and MPEGTS. line.substr(16).split(',').forEach(timestamp => { if (startsWith(timestamp, 'LOCAL:')) { @@ -149,12 +153,9 @@ const WebVTTParser = { localTime = cueString2millis(cueTime) / 1000; // Convert MPEGTS to seconds from 90kHz. presentationTime = mpegTs / 90000; - - if (localTime === -1) { - parsingError = new Error(`Malformed X-TIMESTAMP-MAP: ${line}`); - } } catch (e) { - parsingError = new Error(`Malformed X-TIMESTAMP-MAP: ${line}`); + timestampMap = false; + parsingError = e; } // Return without parsing X-TIMESTAMP-MAP line. return;