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(text): Fix TTML render timing and line break issues #4407

Merged
merged 3 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 21 additions & 9 deletions lib/text/simple_text_displayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,28 @@ shaka.text.SimpleTextDisplayer = class {
// We don't want to modify the array or objects passed in, since we don't
// technically own them. So we build a new array and replace certain items
// in it if they need to be flattened.
const flattenedCues = cues.map((cue) => {
if (cue.nestedCues.length) {
const flatCue = cue.clone();
flatCue.nestedCues = [];
flatCue.payload = flattenPayload(cue);
return flatCue;
} else {
return cue;
// We also don't want to flatten the text payloads starting at a container
// element; otherwise, for containers encapsulating multiple caption lines,
// the lines would merge into a single cue. This is undesirable when a
// subset of the captions are outside of the append time window. To fix
// this, we only call flattenPayload() starting at elements marked as
// isContainer = false.
const getCuesToFlatten = (cues, result) => {
for (const cue of cues) {
if (cue.isContainer) {
// Recurse to find the actual text payload cues.
getCuesToFlatten(cue.nestedCues, result);
} else {
// Flatten the payload.
const flatCue = cue.clone();
flatCue.nestedCues = [];
flatCue.payload = flattenPayload(cue);
result.push(flatCue);
}
}
});
return result;
};
const flattenedCues = getCuesToFlatten(cues, []);

// Convert cues.
const textTrackCues = [];
Expand Down
27 changes: 27 additions & 0 deletions test/text/simple_text_displayer_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,33 @@ describe('SimpleTextDisplayer', () => {
[shakaCue]);
});

it('flattens nested cue payloads correctly', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test looks great. So I'm not concerned about the validity of the code change, so much as the clarity and readability.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, after the updates based on your other comments I think it should be easier to follow.

const level0ContainerCue = new shaka.text.Cue(10, 30, '');
level0ContainerCue.isContainer = true;

const level1NonContainerCueA = new shaka.text.Cue(10, 20, '');
const level1NonContainerCueB = new shaka.text.Cue(20, 30, '');

// Add a trailing whitespace character to get a space-delimited expected
// result.
const cueANestedCue0 = new shaka.text.Cue(10, 20, 'Cue A Test0 ');
const cueANestedCue1 = new shaka.text.Cue(10, 20, 'Cue A Test1');
const cueBNestedCue0 = new shaka.text.Cue(20, 30, 'Cue B Test0 ');
const cueBNestedCue1 = new shaka.text.Cue(20, 30, 'Cue B Test1');

level1NonContainerCueA.nestedCues = [cueANestedCue0, cueANestedCue1];
level1NonContainerCueB.nestedCues = [cueBNestedCue0, cueBNestedCue1];
level0ContainerCue.nestedCues =
[level1NonContainerCueA, level1NonContainerCueB];

verifyHelper(
[
{startTime: 10, endTime: 20, text: 'Cue A Test0 Cue A Test1'},
{startTime: 20, endTime: 30, text: 'Cue B Test0 Cue B Test1'},
],
[level0ContainerCue]);
});

// Regression test for b/159050711
it('maintains the styles of the parent cue', () => {
const shakaCue = new shaka.text.Cue(10, 20, '');
Expand Down