-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Prettier * Other modernizations including XHR->Fetch * Apply suggestions from code review Co-authored-by: Jean-Yves Perrier <jypenator@gmail.com> * Remove IE shim * Update imscjs-demo/js/index.js Co-authored-by: Jean-Yves Perrier <jypenator@gmail.com> * Update imscjs-demo/js/index.js Co-authored-by: Pierre-Anthony Lemieux <pal@sandflow.com> --------- Co-authored-by: Jean-Yves Perrier <jypenator@gmail.com> Co-authored-by: Pierre-Anthony Lemieux <pal@sandflow.com>
- Loading branch information
1 parent
943d004
commit 22903a1
Showing
1 changed file
with
31 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,51 @@ | ||
|
||
function initTTML() { | ||
var renderDiv = document.getElementById("render-div"); | ||
var myVideo = document.getElementById("imscVideo"); | ||
//Track object generated by the first <track> child element | ||
var myTrack = myVideo.textTracks[0]; | ||
//Src is only available from the <track> DOM element, not the obj | ||
var ttmlUrl = myVideo.getElementsByTagName("track")[0].src; | ||
//The track is disabled by default, 'hidden' does not display VTT Text | ||
//but fires the events | ||
async function initTTML() { | ||
const renderDiv = document.getElementById("render-div"); | ||
const myVideo = document.getElementById("imscVideo"); | ||
// Track object generated by the first <track> child element | ||
const myTrack = myVideo.textTracks[0]; | ||
// The source is only available from the <track> DOM element, not the object | ||
const ttmlUrl = myVideo.getElementsByTagName("track")[0].src; | ||
// The track is disabled by default, 'hidden' does not display VTT Text | ||
// but fires the events | ||
myTrack.mode = "hidden"; | ||
|
||
var client = new XMLHttpRequest(); | ||
|
||
client.open('GET', ttmlUrl); | ||
client.onreadystatechange = function () { | ||
initTrack(client.responseText); | ||
} | ||
client.send(); | ||
|
||
const response = await fetch(ttmlUrl); | ||
initTrack(await response.text()); | ||
|
||
function initTrack(text) { | ||
var imscDoc = imsc.fromXML(text); | ||
var timeEvents = imscDoc.getMediaTimeEvents(); | ||
//create cue per timed event and render isd | ||
for (var i = 0; i < timeEvents.length; i++) { | ||
//Edge/IE implement "Generic Cue", other browsers VTTCue | ||
var Cue = window.VTTCue || window.TextTrackCue; | ||
const imscDoc = imsc.fromXML(text); | ||
const timeEvents = imscDoc.getMediaTimeEvents(); | ||
// Since `TextTrackCue`/`VTTCue` does not support TTML natively, we create one (empty) | ||
// cue per event on the timeline of the TTML document. When the cue is triggered | ||
// (`enter` event), we render the TTML document corresponding to the cue time, | ||
// which is called an Intermediary Synchronic Document (ISD), into `renderDiv`. | ||
for (let i = 0; i < timeEvents.length; i++) { | ||
let myCue; | ||
if (i < timeEvents.length - 1) { | ||
//We have to provide empty string as VTTText | ||
var myCue = new Cue(timeEvents[i], timeEvents[i + 1], ""); | ||
// We have to provide an empty string as the VTTText | ||
myCue = new VTTCue(timeEvents[i], timeEvents[i + 1], ""); | ||
} else { | ||
/* | ||
"End" time of last "imsc event" is end of video | ||
*/ | ||
var myCue = new Cue(timeEvents[i], myVideo.duration, ""); | ||
// "End" time of the last "imsc event" is the end of the video | ||
myCue = new VTTCue(timeEvents[i], myVideo.duration, ""); | ||
} | ||
myCue.onenter = function () { | ||
myCue.addEventListener("enter", function () { | ||
clearSubFromScreen(); | ||
var myIsd = imsc.generateISD(imscDoc, this.startTime); | ||
const myIsd = imsc.generateISD(imscDoc, this.startTime); | ||
imsc.renderHTML(myIsd, renderDiv); | ||
}; | ||
myCue.onexit = function () { | ||
}); | ||
myCue.addEventListener("exit", function () { | ||
clearSubFromScreen(); | ||
}; | ||
var r = myTrack.addCue(myCue); | ||
}); | ||
const r = myTrack.addCue(myCue); | ||
} | ||
} | ||
|
||
function clearSubFromScreen() { | ||
var subtitleActive = renderDiv.getElementsByTagName("div")[0]; | ||
const subtitleActive = renderDiv.getElementsByTagName("div")[0]; | ||
if (subtitleActive) { | ||
renderDiv.removeChild(subtitleActive); | ||
} | ||
} | ||
|
||
} | ||
|
||
window.addEventListener("load", initTTML); | ||
addEventListener("load", initTTML); |