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

Update subtitles support #2663

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"dependencies": {
"codem-isoboxer": "0.3.5",
"fast-deep-equal": "1.1.0",
"imsc": "^1.0.1-rc.1"
"imsc": "^1.0.1-rc.2"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/streaming/text/TextController.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ function TextController() {
for (let i = 0; i < fragmentedTracks.length; i++) {
let mediaInfo = fragmentedTracks[i];
if (currentTrackInfo.lang === mediaInfo.lang && currentTrackInfo.index === mediaInfo.index &&
(currentTrackInfo.label ? currentTrackInfo.label === mediaInfo.id : true)) {
(mediaInfo.id ? currentTrackInfo.label === mediaInfo.id : currentTrackInfo.label === mediaInfo.index)) {
let currentFragTrack = mediaController.getCurrentTrackFor(Constants.FRAGMENTED_TEXT, streamController.getActiveStreamInfo());
if (mediaInfo !== currentFragTrack) {
fragmentModel.abortRequests();
Expand Down
119 changes: 67 additions & 52 deletions src/streaming/text/TextTracks.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,21 +258,26 @@ function TextTracks() {
actualVideoTop = newVideoTop + videoOffsetTop;
actualVideoWidth = newVideoWidth;
actualVideoHeight = newVideoHeight;
captionContainer.style.left = actualVideoLeft + 'px';
captionContainer.style.top = actualVideoTop + 'px';
captionContainer.style.width = actualVideoWidth + 'px';
captionContainer.style.height = actualVideoHeight + 'px';

if (captionContainer) {
captionContainer.style.left = actualVideoLeft + 'px';
captionContainer.style.top = actualVideoTop + 'px';
captionContainer.style.width = actualVideoWidth + 'px';
captionContainer.style.height = actualVideoHeight + 'px';
}

// Video view has changed size, so resize any active cues
for (let i = 0; track.activeCues && i < track.activeCues.length; ++i) {
const cue = track.activeCues[i];
cue.scaleCue(cue);
}

if ((fullscreenAttribute && document[fullscreenAttribute]) || displayCCOnTop) {
captionContainer.style.zIndex = topZIndex;
} else {
captionContainer.style.zIndex = null;
if (captionContainer) {
if ((fullscreenAttribute && document[fullscreenAttribute]) || displayCCOnTop) {
captionContainer.style.zIndex = topZIndex;
} else {
captionContainer.style.zIndex = null;
}
}
}
}
Expand Down Expand Up @@ -356,30 +361,32 @@ function TextTracks() {
}

function renderCaption(cue) {
const finalCue = document.createElement('div');
captionContainer.appendChild(finalCue);
previousISDState = renderHTML(cue.isd, finalCue, function (uri) {
const imsc1ImgUrnTester = /^(urn:)(mpeg:[a-z0-9][a-z0-9-]{0,31}:)(subs:)([0-9]+)$/;
const smpteImgUrnTester = /^#(.*)$/;
if (imsc1ImgUrnTester.test(uri)) {
const match = imsc1ImgUrnTester.exec(uri);
const imageId = parseInt(match[4], 10) - 1;
const imageData = btoa(cue.images[imageId]);
const dataUrl = 'data:image/png;base64,' + imageData;
return dataUrl;
} else if (smpteImgUrnTester.test(uri)) {
const match = smpteImgUrnTester.exec(uri);
const imageId = match[1];
const dataUrl = 'data:image/png;base64,' + cue.embeddedImages[imageId];
return dataUrl;
} else {
return null;
}
}, captionContainer.clientHeight, captionContainer.clientWidth, false/*displayForcedOnlyMode*/, function (err) {
logger.info('renderCaption :', err);
//TODO add ErrorHandler management
}, previousISDState, true /*enableRollUp*/);
finalCue.id = cue.cueID;
if (captionContainer) {
const finalCue = document.createElement('div');
captionContainer.appendChild(finalCue);
previousISDState = renderHTML(cue.isd, finalCue, function (uri) {
const imsc1ImgUrnTester = /^(urn:)(mpeg:[a-z0-9][a-z0-9-]{0,31}:)(subs:)([0-9]+)$/;
const smpteImgUrnTester = /^#(.*)$/;
if (imsc1ImgUrnTester.test(uri)) {
const match = imsc1ImgUrnTester.exec(uri);
const imageId = parseInt(match[4], 10) - 1;
const imageData = btoa(cue.images[imageId]);
const dataUrl = 'data:image/png;base64,' + imageData;
return dataUrl;
} else if (smpteImgUrnTester.test(uri)) {
const match = smpteImgUrnTester.exec(uri);
const imageId = match[1];
const dataUrl = 'data:image/png;base64,' + cue.embeddedImages[imageId];
return dataUrl;
} else {
return null;
}
}, captionContainer.clientHeight, captionContainer.clientWidth, false/*displayForcedOnlyMode*/, function (err) {
logger.info('renderCaption :', err);
//TODO add ErrorHandler management
}, previousISDState, true /*enableRollUp*/);
finalCue.id = cue.cueID;
}
}

/*
Expand All @@ -404,7 +411,7 @@ function TextTracks() {
track.cellResolution = currentItem.cellResolution;
track.isFromCEA608 = currentItem.isFromCEA608;

if (currentItem.type === 'html') {
if (currentItem.type === 'html' && captionContainer) {
cue = new Cue(currentItem.start - timeOffset, currentItem.end - timeOffset, '');
cue.cueHTMLElement = currentItem.cueHTMLElement;
cue.isd = currentItem.isd;
Expand Down Expand Up @@ -436,33 +443,41 @@ function TextTracks() {
};

cue.onexit = function () {
const divs = captionContainer.childNodes;
for (let i = 0; i < divs.length; ++i) {
if (divs[i].id === this.cueID) {
logger.debug('Cue exit id:' + divs[i].id);
captionContainer.removeChild(divs[i]);
if (captionContainer) {
const divs = captionContainer.childNodes;
for (let i = 0; i < divs.length; ++i) {
if (divs[i].id === this.cueID) {
logger.debug('Cue exit id:' + divs[i].id);
captionContainer.removeChild(divs[i]);
}
}
}
};
} else {
cue = new Cue(currentItem.start - timeOffset, currentItem.end - timeOffset, currentItem.data);
if (currentItem.styles) {
if (currentItem.styles.align !== undefined && 'align' in cue) {
cue.align = currentItem.styles.align;
}
if (currentItem.styles.line !== undefined && 'line' in cue) {
cue.line = currentItem.styles.line;
}
if (currentItem.styles.position !== undefined && 'position' in cue) {
cue.position = currentItem.styles.position;
}
if (currentItem.styles.size !== undefined && 'size' in cue) {
cue.size = currentItem.styles.size;
if (currentItem.data) {
cue = new Cue(currentItem.start - timeOffset, currentItem.end - timeOffset, currentItem.data);
if (currentItem.styles) {
if (currentItem.styles.align !== undefined && 'align' in cue) {
cue.align = currentItem.styles.align;
}
if (currentItem.styles.line !== undefined && 'line' in cue) {
cue.line = currentItem.styles.line;
}
if (currentItem.styles.position !== undefined && 'position' in cue) {
cue.position = currentItem.styles.position;
}
if (currentItem.styles.size !== undefined && 'size' in cue) {
cue.size = currentItem.styles.size;
}
}
}
}
try {
track.addCue(cue);
if (cue) {
track.addCue(cue);
} else {
logger.error('impossible to display subtitles.');
}
} catch (e) {
// Edge crash, delete everything and start adding again
// @see https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/11979877/
Expand Down