Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(TTML): Fix duplicate cues overlapping segment boundaries #4798

Merged
merged 3 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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