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 blinking cues when segmented over multiple segments or chunks #4103

Merged
52 changes: 51 additions & 1 deletion src/streaming/text/TextTracks.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ import FactoryMaker from '../../core/FactoryMaker';
import Debug from '../../core/Debug';
import {renderHTML} from 'imsc';

const CUE_PROPS_TO_COMPARE = [
'text',
'images',
'embeddedImages',
'align',
'fontSize',
'id',
'isd',
'line',
'lineAlign',
'lineHeight',
'linePadding',
'position',
'positionAlign',
'region',
'size',
'snapToLines',
'vertical',
];

function TextTracks(config) {

const context = this.context;
Expand Down Expand Up @@ -400,6 +420,34 @@ function TextTracks(config) {
}
}

function _extendLastCue(cue, track) {
if (!track.cues || track.cues.length === 0) {
return false;
}
const prevCue = track.cues[track.cues.length - 1];
// Check previous cue endTime with current cue startTime
// (should we consider an epsilon margin? for example to get around rounding issues)
if (prevCue.endTime !== cue.startTime) {
return false;
}
// Compare cues content
if (!_cuesContentAreEqual(prevCue, cue, CUE_PROPS_TO_COMPARE)) {
return false;
}
prevCue.endTime = cue.endTime;
return true;
}

function _cuesContentAreEqual(cue1, cue2, props) {
for (let i = 0; i < props.length; i++) {
const key = props[i];
if (JSON.stringify(cue1[key]) !== JSON.stringify(cue2[key])) {
return false;
}
};
return true;
}

/*
* Add captions to track, store for later adding, or add captions added before
*/
Expand Down Expand Up @@ -434,7 +482,9 @@ function TextTracks(config) {
}
track.manualCueList.push(cue);
} else {
track.addCue(cue);
if (!_extendLastCue(cue, track)) {
track.addCue(cue);
}
}

}
Expand Down