Skip to content

Commit

Permalink
fix(TTML): Fix duplicate cues overlapping segment boundaries (#4798)
Browse files Browse the repository at this point in the history
Closes #4631
  • Loading branch information
joeyparrish committed Dec 8, 2022
1 parent d27f023 commit 9faf848
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 96 deletions.
17 changes: 11 additions & 6 deletions lib/text/ttml_text_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ shaka.text.TtmlTextParser = class {
}

const cue = TtmlTextParser.parseCue_(
body, time.periodStart, rateInfo, metadataElements, styles,
body, time, rateInfo, metadataElements, styles,
regionElements, cueRegions, whitespaceTrim,
cellResolutionInfo, /* parentCueElement= */ null,
/* isContent= */ false);
Expand All @@ -157,7 +157,7 @@ shaka.text.TtmlTextParser = class {
* Parses a TTML node into a Cue.
*
* @param {!Node} cueNode
* @param {number} offset
* @param {shaka.extern.TextParser.TimeContext} timeContext
* @param {!shaka.text.TtmlTextParser.RateInfo_} rateInfo
* @param {!Array.<!Element>} metadataElements
* @param {!Array.<!Element>} styles
Expand All @@ -171,7 +171,7 @@ shaka.text.TtmlTextParser = class {
* @private
*/
static parseCue_(
cueNode, offset, rateInfo, metadataElements, styles, regionElements,
cueNode, timeContext, rateInfo, metadataElements, styles, regionElements,
cueRegions, whitespaceTrim, cellResolution, parentCueElement, isContent) {
/** @type {Element} */
let cueElement;
Expand Down Expand Up @@ -236,7 +236,7 @@ shaka.text.TtmlTextParser = class {
for (const childNode of cueElement.childNodes) {
const nestedCue = shaka.text.TtmlTextParser.parseCue_(
childNode,
offset,
timeContext,
rateInfo,
metadataElements,
styles,
Expand Down Expand Up @@ -293,15 +293,20 @@ shaka.text.TtmlTextParser = class {
if (start == null) {
start = 0;
}
start += offset;
start += timeContext.periodStart;

// If end is null, that means the duration is effectively infinite.
if (end == null) {
end = Infinity;
} else {
end += offset;
end += timeContext.periodStart;
}

// Clip times to segment boundaries.
// https://github.com/shaka-project/shaka-player/issues/4631
start = Math.max(start, timeContext.segmentStart);
end = Math.min(end, timeContext.segmentEnd);

if (!hasTimeAttributes && nestedCues.length > 0) {
// If no time is defined for this cue, base the timing information on
// the time of the nested cues. In the case of multiple nested cues with
Expand Down
10 changes: 6 additions & 4 deletions test/text/mp4_ttml_parser_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ describe('Mp4TtmlParser', () => {
it('handles media segments with multiple mdats', () => {
const parser = new shaka.text.Mp4TtmlParser();
parser.parseInit(ttmlInitSegment);
const time = {periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0};
const time =
{periodStart: 0, segmentStart: 0, segmentEnd: 60, vttOffset: 0};
const ret = parser.parseMedia(ttmlSegmentMultipleMDAT, time);
// Bodies.
expect(ret.length).toBe(2);
Expand All @@ -55,9 +56,9 @@ describe('Mp4TtmlParser', () => {

it('accounts for offset', () => {
const time1 =
{periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0};
{periodStart: 0, segmentStart: 0, segmentEnd: 70, vttOffset: 0};
const time2 =
{periodStart: 7, segmentStart: 0, segmentEnd: 0, vttOffset: 7};
{periodStart: 7, segmentStart: 0, segmentEnd: 70, vttOffset: 7};

const parser = new shaka.text.Mp4TtmlParser();
parser.parseInit(ttmlInitSegment);
Expand Down Expand Up @@ -161,7 +162,8 @@ describe('Mp4TtmlParser', () => {
];
const parser = new shaka.text.Mp4TtmlParser();
parser.parseInit(ttmlInitSegment);
const time = {periodStart: 0, segmentStart: 0, segmentEnd: 0, vttOffset: 0};
const time =
{periodStart: 0, segmentStart: 0, segmentEnd: 60, vttOffset: 0};
const result = parser.parseMedia(ttmlSegment, time);
shaka.test.TtmlUtils.verifyHelper(
cues, result, {startTime: 23, endTime: 53.5});
Expand Down
Loading

0 comments on commit 9faf848

Please sign in to comment.