Skip to content

Commit

Permalink
fix: Filter duplicate cues on text displayer append (#6949)
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad authored Jul 1, 2024
1 parent 88431b6 commit fa9feb3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
18 changes: 17 additions & 1 deletion lib/text/text_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ shaka.text.Utils = class {
*/
static getCuesToFlatten(cues, parentCue) {
const result = [];
for (const cue of cues) {
for (const cue of shaka.text.Utils.removeDuplicates(cues)) {
if (cue.isContainer) {
// Recurse to find the actual text payload cues.
result.push(...shaka.text.Utils.getCuesToFlatten(cue.nestedCues, cue));
Expand All @@ -162,4 +162,20 @@ shaka.text.Utils = class {
}
return result;
}

/**
* @param {!Array.<!shaka.text.Cue>} cues
* @return {!Array.<!shaka.text.Cue>}
*/
static removeDuplicates(cues) {
const uniqueCues = [];
for (const cue of cues) {
const isValid = !uniqueCues.some(
(existingCue) => shaka.text.Cue.equal(cue, existingCue));
if (isValid) {
uniqueCues.push(cue);
}
}
return uniqueCues;
}
};
3 changes: 2 additions & 1 deletion lib/text/ui_text_displayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ goog.require('goog.asserts');
goog.require('shaka.Deprecate');
goog.require('shaka.text.Cue');
goog.require('shaka.text.CueRegion');
goog.require('shaka.text.Utils');
goog.require('shaka.util.Dom');
goog.require('shaka.util.EventManager');
goog.require('shaka.util.Timer');
Expand Down Expand Up @@ -156,7 +157,7 @@ shaka.text.UITextDisplayer = class {
// list growing during the comparisons for duplicate cues.
// See: https://github.com/shaka-project/shaka-player/issues/3018
const cuesList = [...this.cues_];
for (const cue of cues) {
for (const cue of shaka.text.Utils.removeDuplicates(cues)) {
// When a VTT cue spans a segment boundary, the cue will be duplicated
// into two segments.
// To avoid displaying duplicate cues, if the current cue list already
Expand Down
12 changes: 6 additions & 6 deletions test/text/ui_text_displayer_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,8 @@ describe('UITextDisplayer', () => {
// be created.
const cues = [
new shaka.text.Cue(0, 100, ''),
new shaka.text.Cue(0, 100, ''),
new shaka.text.Cue(0, 100, ''),
new shaka.text.Cue(0, 200, ''),
new shaka.text.Cue(0, 300, ''),
];
for (const cue of cues) {
cue.displayAlign = shaka.text.Cue.displayAlign.CENTER;
Expand Down Expand Up @@ -567,8 +567,8 @@ describe('UITextDisplayer', () => {
// be created.
const firstBatchOfCues = [
new shaka.text.Cue(0, 100, ''),
new shaka.text.Cue(0, 100, ''),
new shaka.text.Cue(0, 100, ''),
new shaka.text.Cue(0, 200, ''),
new shaka.text.Cue(0, 300, ''),
];
for (const cue of firstBatchOfCues) {
cue.displayAlign = shaka.text.Cue.displayAlign.CENTER;
Expand All @@ -578,8 +578,8 @@ describe('UITextDisplayer', () => {
// Another batch for the other region
const secondBatchOfCues = [
new shaka.text.Cue(0, 100, ''),
new shaka.text.Cue(0, 100, ''),
new shaka.text.Cue(0, 100, ''),
new shaka.text.Cue(0, 200, ''),
new shaka.text.Cue(0, 300, ''),
];
for (const cue of secondBatchOfCues) {
cue.displayAlign = shaka.text.Cue.displayAlign.CENTER;
Expand Down

0 comments on commit fa9feb3

Please sign in to comment.