diff --git a/src/util/text-tracks.js b/src/util/text-tracks.js index 5895b61ae..0befdb7a6 100644 --- a/src/util/text-tracks.js +++ b/src/util/text-tracks.js @@ -4,6 +4,68 @@ import window from 'global/window'; import videojs from 'video.js'; +/** + * Create captions text tracks on video.js if they do not exist + * + * @param {Object} inbandTextTracks a reference to current inbandTextTracks + * @param {Object} tech the video.js tech + * @param {Object} captionStream the caption stream to create + * @private + */ +export const createCaptionsTrackIfNotExists = function(inbandTextTracks, tech, captionStream) { + if (!inbandTextTracks[captionStream]) { + tech.trigger({type: 'usage', name: 'hls-608'}); + let track = tech.textTracks().getTrackById(captionStream); + + if (track) { + // Resuse an existing track with a CC# id because this was + // very likely created by videojs-contrib-hls from information + // in the m3u8 for us to use + inbandTextTracks[captionStream] = track; + } else { + // Otherwise, create a track with the default `CC#` label and + // without a language + inbandTextTracks[captionStream] = tech.addRemoteTextTrack({ + kind: 'captions', + id: captionStream, + label: captionStream + }, false).track; + } + } +}; + +/** + * Add caption text track data to a source handler given an array of captions + * + * @param {Object} + * @param {Object} inbandTextTracks the inband text tracks + * @param {Number} timestampOffset the timestamp offset of the source buffer + * @param {Array} captionArray an array of caption data + * @private + */ +export const addCaptionData = function({ + inbandTextTracks, + captionArray, + timestampOffset +}) { + if (!captionArray) { + return; + } + + const Cue = window.WebKitDataCue || window.VTTCue; + + captionArray.forEach((caption) => { + const track = caption.stream; + + inbandTextTracks[track].addCue( + new Cue( + caption.startTime + timestampOffset, + caption.endTime + timestampOffset, + caption.text + )); + }); +}; + /** * Define properties on a cue for backwards compatability, * but warn the user that the way that they are using it @@ -137,38 +199,6 @@ export const addMetadata = ({ }); }; -/** - * Add caption text track data to a source handler given an array of captions - * - * @param {Object} - * @param {Object} inbandTextTracks the inband text tracks - * @param {Number} timestampOffset the timestamp offset of the source buffer - * @param {Array} captionArray an array of caption data - * @private - */ -export const addCaptionData = ({ - inbandTextTracks, - captionArray, - timestampOffset -}) => { - if (!captionArray) { - return; - } - - const Cue = window.WebKitDataCue || window.VTTCue; - - captionArray.forEach((caption) => { - const track = caption.stream; - - inbandTextTracks[track].addCue( - new Cue( - caption.startTime + timestampOffset, - caption.endTime + timestampOffset, - caption.text - )); - }); -}; - /** * Create metadata text track on video.js if it does not exist * @@ -190,37 +220,6 @@ export const createMetadataTrackIfNotExists = (inbandTextTracks, dispatchType, t inbandTextTracks.metadataTrack_.inBandMetadataTrackDispatchType = dispatchType; }; -/** - * Create captions text tracks on video.js if they do not exist - * - * @param {Object} inbandTextTracks a reference to current inbandTextTracks - * @param {Object} tech the video.js tech - * @param {Object} captionStream the caption stream to create - * @private - */ -export const createCaptionsTrackIfNotExists = -(inbandTextTracks, tech, captionStream) => { - if (!inbandTextTracks[captionStream]) { - tech.trigger({type: 'usage', name: 'hls-608'}); - let track = tech.textTracks().getTrackById(captionStream); - - if (track) { - // Resuse an existing track with a CC# id because this was - // very likely created by videojs-contrib-hls from information - // in the m3u8 for us to use - inbandTextTracks[captionStream] = track; - } else { - // Otherwise, create a track with the default `CC#` label and - // without a language - inbandTextTracks[captionStream] = tech.addRemoteTextTrack({ - kind: 'captions', - id: captionStream, - label: captionStream - }, false).track; - } - } -}; - /** * Remove cues from a track on video.js. *