1
0
mirror of https://github.com/videojs/video.js.git synced 2025-04-21 12:17:11 +02:00
video.js/src/tracks.js

780 lines
23 KiB
JavaScript
Raw Normal View History

2012-03-16 12:29:38 -07:00
// TEXT TRACKS
// Text tracks are tracks of timed text events.
// Captions - text displayed over the video for the hearing impared
// Subtitles - text displayed over the video for those who don't understand langauge in the video
// Chapters - text displayed in a menu allowing the user to jump to particular points (chapters) in the video
// Descriptions (not supported yet) - audio descriptions that are read back to the user by a screen reading device
// Player Track Functions - Functions add to the player object for easier access to tracks
_V_.merge(_V_.Player.prototype, {
2011-09-30 17:28:43 -07:00
// Add an array of text tracks. captions, subtitles, chapters, descriptions
2012-03-16 12:29:38 -07:00
// Track objects will be stored in the player.textTracks array
addTextTracks: function(trackObjects){
var tracks = this.textTracks = (this.textTracks) ? this.textTracks : [],
2012-03-16 12:29:38 -07:00
i = 0, j = trackObjects.length, track, Kind;
2011-09-30 17:28:43 -07:00
for (;i<j;i++) {
// HTML5 Spec says default to subtitles.
2012-03-16 12:29:38 -07:00
// Uppercase (uc) first letter to match class names
Kind = _V_.uc(trackObjects[i].kind || "subtitles");
2011-09-30 17:28:43 -07:00
// Create correct texttrack class. CaptionsTrack, etc.
track = new _V_[Kind + "Track"](this, trackObjects[i]);
tracks.push(track);
2011-09-30 17:28:43 -07:00
2012-03-16 12:29:38 -07:00
// If track.default is set, start showing immediately
// TODO: Add a process to deterime the best track to show for the specific kind
// Incase there are mulitple defaulted tracks of the same kind
// Or the user has a set preference of a specific language that should override the default
if (track['default']) {
this.ready(_V_.proxy(track, track.show));
}
}
2012-03-16 12:29:38 -07:00
// Return the track so it can be appended to the display component
return this;
},
2012-03-16 12:29:38 -07:00
// Show a text track
// disableSameKind: disable all other tracks of the same kind. Value should be a track kind (captions, etc.)
showTextTrack: function(id, disableSameKind){
var tracks = this.textTracks,
i = 0,
j = tracks.length,
2012-03-16 12:29:38 -07:00
track, showTrack, kind;
2012-03-16 12:29:38 -07:00
// Find Track with same ID
for (;i<j;i++) {
track = tracks[i];
2012-03-16 12:29:38 -07:00
if (track.id === id) {
track.show();
showTrack = track;
// Disable tracks of the same kind
} else if (disableSameKind && track.kind == disableSameKind && track.mode > 0) {
track.disable();
}
}
2012-03-16 12:29:38 -07:00
// Get track kind from shown track or disableSameKind
kind = (showTrack) ? showTrack.kind : ((disableSameKind) ? disableSameKind : false);
// Trigger trackchange event, captionstrackchange, subtitlestrackchange, etc.
if (kind) {
this.trigger(kind+"trackchange");
2012-03-16 12:29:38 -07:00
}
return this;
}
});
2012-03-16 12:29:38 -07:00
// Track Class
// Contains track methods for loading, showing, parsing cues of tracks
_V_.Track = _V_.Component.extend({
init: function(player, options){
this._super(player, options);
2012-03-16 12:29:38 -07:00
// Apply track info to track object
// Options will often be a track element
_V_.merge(this, {
// Build ID if one doesn't exist
id: options.id || ("vjs_" + options.kind + "_" + options.language + "_" + _V_.guid++),
src: options.src,
// If default is used, subtitles/captions to start showing
"default": options["default"], // 'default' is reserved-ish
title: options.title,
2012-03-16 12:29:38 -07:00
// Language - two letter string to represent track language, e.g. "en" for English
// readonly attribute DOMString language;
language: options.srclang,
2012-03-16 12:29:38 -07:00
// Track label e.g. "English"
// readonly attribute DOMString label;
label: options.label,
2011-09-30 17:28:43 -07:00
2012-03-16 12:29:38 -07:00
// All cues of the track. Cues have a startTime, endTime, text, and other properties.
// readonly attribute TextTrackCueList cues;
cues: [],
2012-03-16 12:29:38 -07:00
// ActiveCues is all cues that are currently showing
// readonly attribute TextTrackCueList activeCues;
activeCues: [],
2012-03-16 12:29:38 -07:00
// ReadyState describes if the text file has been loaded
// const unsigned short NONE = 0;
// const unsigned short LOADING = 1;
// const unsigned short LOADED = 2;
// const unsigned short ERROR = 3;
// readonly attribute unsigned short readyState;
readyState: 0,
2012-03-16 12:29:38 -07:00
// Mode describes if the track is showing, hidden, or disabled
// const unsigned short OFF = 0;
2012-03-16 12:29:38 -07:00
// const unsigned short HIDDEN = 1; (still triggering cuechange events, but not visible)
// const unsigned short SHOWING = 2;
// attribute unsigned short mode;
2012-03-16 12:29:38 -07:00
mode: 0
});
},
2012-03-16 12:29:38 -07:00
// Create basic div to hold cue text
createElement: function(){
return this._super("div", {
className: "vjs-" + this.kind + " vjs-text-track"
});
},
// Show: Mode Showing (2)
// Indicates that the text track is active. If no attempt has yet been made to obtain the track's cues, the user agent will perform such an attempt momentarily.
// The user agent is maintaining a list of which cues are active, and events are being fired accordingly.
// In addition, for text tracks whose kind is subtitles or captions, the cues are being displayed over the video as appropriate;
// for text tracks whose kind is descriptions, the user agent is making the cues available to the user in a non-visual fashion;
// and for text tracks whose kind is chapters, the user agent is making available to the user a mechanism by which the user can navigate to any point in the media resource by selecting a cue.
// The showing by default state is used in conjunction with the default attribute on track elements to indicate that the text track was enabled due to that attribute.
// This allows the user agent to override the state if a later track is discovered that is more appropriate per the user's preferences.
show: function(){
this.activate();
this.mode = 2;
// Show element.
this._super();
},
// Hide: Mode Hidden (1)
// Indicates that the text track is active, but that the user agent is not actively displaying the cues.
// If no attempt has yet been made to obtain the track's cues, the user agent will perform such an attempt momentarily.
// The user agent is maintaining a list of which cues are active, and events are being fired accordingly.
hide: function(){
// When hidden, cues are still triggered. Disable to stop triggering.
this.activate();
this.mode = 1;
// Hide element.
this._super();
},
// Disable: Mode Off/Disable (0)
// Indicates that the text track is not active. Other than for the purposes of exposing the track in the DOM, the user agent is ignoring the text track.
// No cues are active, no events are fired, and the user agent will not attempt to obtain the track's cues.
disable: function(){
// If showing, hide.
if (this.mode == 2) { this.hide(); }
// Stop triggering cues
this.deactivate();
// Switch Mode to Off
this.mode = 0;
},
2012-03-16 12:29:38 -07:00
// Turn on cue tracking. Tracks that are showing OR hidden are active.
activate: function(){
2012-03-16 12:29:38 -07:00
// Load text file if it hasn't been yet.
if (this.readyState == 0) { this.load(); }
// Only activate if not already active.
if (this.mode == 0) {
// Update current cue on timeupdate
// Using unique ID for proxy function so other tracks don't remove listener
this.player.on("timeupdate", this.proxy(this.update, this.id));
// Reset cue time on media end
this.player.on("ended", this.proxy(this.reset, this.id));
// Add to display
2012-03-16 12:29:38 -07:00
if (this.kind == "captions" || this.kind == "subtitles") {
this.player.textTrackDisplay.addComponent(this);
}
}
},
2012-03-16 12:29:38 -07:00
// Turn off cue tracking.
deactivate: function(){
// Using unique ID for proxy function so other tracks don't remove listener
this.player.off("timeupdate", this.proxy(this.update, this.id));
this.player.off("ended", this.proxy(this.reset, this.id));
this.reset(); // Reset
// Remove from display
this.player.textTrackDisplay.removeComponent(this);
},
// A readiness state
// One of the following:
//
// Not loaded
// Indicates that the text track is known to exist (e.g. it has been declared with a track element), but its cues have not been obtained.
//
// Loading
// Indicates that the text track is loading and there have been no fatal errors encountered so far. Further cues might still be added to the track.
//
// Loaded
// Indicates that the text track has been loaded with no fatal errors. No new cues will be added to the track except if the text track corresponds to a MutableTextTrack object.
//
// Failed to load
// Indicates that the text track was enabled, but when the user agent attempted to obtain it, this failed in some way (e.g. URL could not be resolved, network error, unknown text track format). Some or all of the cues are likely missing and will not be obtained.
load: function(){
// Only load if not loaded yet.
if (this.readyState == 0) {
2012-03-16 12:29:38 -07:00
this.readyState = 1;
_V_.get(this.src, this.proxy(this.parseCues), this.proxy(this.onError));
}
},
onError: function(err){
this.error = err;
2012-03-16 12:29:38 -07:00
this.readyState = 3;
this.trigger("error");
},
2011-09-30 17:28:43 -07:00
2012-03-16 12:29:38 -07:00
// Parse the WebVTT text format for cue times.
// TODO: Separate parser into own class so alternative timed text formats can be used. (TTML, DFXP)
2011-09-30 17:28:43 -07:00
parseCues: function(srcContent) {
var cue, time, text,
lines = srcContent.split("\n"),
line = "", id;
2011-09-30 17:28:43 -07:00
for (var i=1, j=lines.length; i<j; i++) {
// Line 0 should be 'WEBVTT', so skipping i=0
2011-09-30 17:28:43 -07:00
line = _V_.trim(lines[i]); // Trim whitespace and linebreaks
2011-09-30 17:28:43 -07:00
if (line) { // Loop until a line with content
// First line could be an optional cue ID
// Check if line has the time separator
if (line.indexOf("-->") == -1) {
id = line;
// Advance to next line for timing.
line = _V_.trim(lines[++i]);
} else {
id = this.cues.length;
}
2011-09-30 17:28:43 -07:00
// First line - Number
cue = {
id: id, // Cue Number
2011-09-30 17:28:43 -07:00
index: this.cues.length // Position in Array
};
// Timing line
2011-09-30 17:28:43 -07:00
time = line.split(" --> ");
cue.startTime = this.parseCueTime(time[0]);
cue.endTime = this.parseCueTime(time[1]);
// Additional lines - Cue Text
text = [];
// Loop until a blank line or end of lines
// Assumeing trim("") returns false for blank lines
while (lines[++i] && (line = _V_.trim(lines[i]))) {
2011-09-30 17:28:43 -07:00
text.push(line);
}
2011-09-30 17:28:43 -07:00
cue.text = text.join('<br/>');
// Add this cue
this.cues.push(cue);
}
}
2012-03-16 12:29:38 -07:00
this.readyState = 2;
this.trigger("loaded");
2011-09-30 17:28:43 -07:00
},
parseCueTime: function(timeText) {
var parts = timeText.split(':'),
time = 0,
hours, minutes, other, seconds, ms, flags;
// Check if optional hours place is included
// 00:00:00.000 vs. 00:00.000
if (parts.length == 3) {
hours = parts[0];
minutes = parts[1];
other = parts[2];
} else {
hours = 0;
minutes = parts[0];
other = parts[1];
}
// Break other (seconds, milliseconds, and flags) by spaces
// TODO: Make additional cue layout settings work with flags
other = other.split(/\s+/)
// Remove seconds. Seconds is the first part before any spaces.
seconds = other.splice(0,1)[0];
// Could use either . or , for decimal
seconds = seconds.split(/\.|,/);
// Get milliseconds
ms = parseFloat(seconds[1]);
seconds = seconds[0];
2011-09-30 17:28:43 -07:00
// hours => seconds
time += parseFloat(hours) * 3600;
2011-09-30 17:28:43 -07:00
// minutes => seconds
time += parseFloat(minutes) * 60;
// Add seconds
time += parseFloat(seconds);
// Add milliseconds
2011-09-30 17:28:43 -07:00
if (ms) { time += ms/1000; }
2011-09-30 17:28:43 -07:00
return time;
},
2012-03-16 12:29:38 -07:00
// Update active cues whenever timeupdate events are triggered on the player.
2011-09-30 17:28:43 -07:00
update: function(){
if (this.cues.length > 0) {
// Get curent player time
2011-09-30 17:28:43 -07:00
var time = this.player.currentTime();
// Check if the new time is outside the time box created by the the last update.
if (this.prevChange === undefined || time < this.prevChange || this.nextChange <= time) {
var cues = this.cues,
// Create a new time box for this state.
2012-03-16 12:29:38 -07:00
newNextChange = this.player.duration(), // Start at beginning of the timeline
newPrevChange = 0, // Start at end
reverse = false, // Set the direction of the loop through the cues. Optimized the cue check.
newCues = [], // Store new active cues.
// Store where in the loop the current active cues are, to provide a smart starting point for the next loop.
firstActiveIndex, lastActiveIndex,
html = "", // Create cue text HTML to add to the display
cue, i, j; // Loop vars
// Check if time is going forwards or backwards (scrubbing/rewinding)
// If we know the direction we can optimize the starting position and direction of the loop through the cues array.
2012-03-16 12:29:38 -07:00
if (time >= this.nextChange || this.nextChange === undefined) { // NextChange should happen
// Forwards, so start at the index of the first active cue and loop forward
i = (this.firstActiveIndex !== undefined) ? this.firstActiveIndex : 0;
} else {
// Backwards, so start at the index of the last active cue and loop backward
reverse = true;
2012-03-16 12:29:38 -07:00
i = (this.lastActiveIndex !== undefined) ? this.lastActiveIndex : cues.length - 1;
}
2011-09-30 17:28:43 -07:00
while (true) { // Loop until broken
cue = cues[i];
// Cue ended at this point
if (cue.endTime <= time) {
2012-03-16 12:29:38 -07:00
newPrevChange = Math.max(newPrevChange, cue.endTime);
if (cue.active) {
cue.active = false;
2011-09-30 17:28:43 -07:00
}
2012-03-16 12:29:38 -07:00
// No earlier cues should have an active start time.
// Nevermind. Assume first cue could have a duration the same as the video.
// In that case we need to loop all the way back to the beginning.
// if (reverse && cue.startTime) { break; }
// Cue hasn't started
} else if (time < cue.startTime) {
2012-03-16 12:29:38 -07:00
newNextChange = Math.min(newNextChange, cue.startTime);
if (cue.active) {
cue.active = false;
}
// No later cues should have an active start time.
2012-03-16 12:29:38 -07:00
if (!reverse) { break; }
// Cue is current
2012-03-16 12:29:38 -07:00
} else {
if (reverse) {
// Add cue to front of array to keep in time order
newCues.splice(0,0,cue);
// If in reverse, the first current cue is our lastActiveCue
if (lastActiveIndex === undefined) { lastActiveIndex = i; }
firstActiveIndex = i;
} else {
// Add cue to end of array
newCues.push(cue);
// If forward, the first current cue is our firstActiveIndex
if (firstActiveIndex === undefined) { firstActiveIndex = i; }
lastActiveIndex = i;
2011-09-30 17:28:43 -07:00
}
2012-03-16 12:29:38 -07:00
newNextChange = Math.min(newNextChange, cue.endTime);
newPrevChange = Math.max(newPrevChange, cue.startTime);
cue.active = true;
}
if (reverse) {
2012-03-16 12:29:38 -07:00
// Reverse down the array of cues, break if at first
if (i === 0) { break; } else { i--; }
} else {
2012-03-16 12:29:38 -07:00
// Walk up the array fo cues, break if at last
if (i === cues.length - 1) { break; } else { i++; }
2011-09-30 17:28:43 -07:00
}
2011-09-30 17:28:43 -07:00
}
2012-03-16 12:29:38 -07:00
this.activeCues = newCues;
this.nextChange = newNextChange;
this.prevChange = newPrevChange;
this.firstActiveIndex = firstActiveIndex;
this.lastActiveIndex = lastActiveIndex;
2012-03-16 12:29:38 -07:00
this.updateDisplay();
this.trigger("cuechange");
2011-09-30 17:28:43 -07:00
}
}
2012-03-16 12:29:38 -07:00
},
2012-03-16 12:29:38 -07:00
// Add cue HTML to display
updateDisplay: function(){
var cues = this.activeCues,
html = "",
i=0,j=cues.length;
for (;i<j;i++) {
html += "<span class='vjs-tt-cue'>"+cues[i].text+"</span>";
}
2012-03-16 12:29:38 -07:00
this.el.innerHTML = html;
},
2012-03-16 12:29:38 -07:00
// Set all loop helper values back
reset: function(){
this.nextChange = 0;
this.prevChange = this.player.duration();
this.firstActiveIndex = 0;
this.lastActiveIndex = 0;
}
});
2012-03-16 12:29:38 -07:00
// Create specific track types
_V_.CaptionsTrack = _V_.Track.extend({
kind: "captions"
});
_V_.SubtitlesTrack = _V_.Track.extend({
kind: "subtitles"
});
2012-03-16 12:29:38 -07:00
_V_.ChaptersTrack = _V_.Track.extend({
kind: "chapters"
});
2012-03-16 12:29:38 -07:00
/* Text Track Display
================================================================================ */
// Global container for both subtitle and captions text. Simple div container.
_V_.TextTrackDisplay = _V_.Component.extend({
createElement: function(){
return this._super("div", {
2012-03-16 12:29:38 -07:00
className: "vjs-text-track-display"
});
2012-03-16 12:29:38 -07:00
}
});
/* Text Track Menu Items
2012-03-16 12:29:38 -07:00
================================================================================ */
_V_.TextTrackMenuItem = _V_.MenuItem.extend({
2012-03-16 12:29:38 -07:00
init: function(player, options){
var track = this.track = options.track;
2012-03-16 12:29:38 -07:00
// Modify options for parent MenuItem class's init.
options.label = track.label;
options.selected = track["default"];
this._super(player, options);
this.player.on(track.kind + "trackchange", _V_.proxy(this, this.update));
2012-03-16 12:29:38 -07:00
},
onClick: function(){
this._super();
this.player.showTextTrack(this.track.id, this.track.kind);
},
update: function(){
if (this.track.mode == 2) {
this.selected(true);
} else {
2012-03-16 12:29:38 -07:00
this.selected(false);
}
2012-03-16 12:29:38 -07:00
}
2012-03-16 12:29:38 -07:00
});
_V_.OffTextTrackMenuItem = _V_.TextTrackMenuItem.extend({
init: function(player, options){
// Create pseudo track info
// Requires options.kind
options.track = { kind: options.kind, player: player, label: "Off" }
this._super(player, options);
},
2012-03-16 12:29:38 -07:00
onClick: function(){
this._super();
this.player.showTextTrack(this.track.id, this.track.kind);
},
update: function(){
var tracks = this.player.textTracks,
2012-03-16 12:29:38 -07:00
i=0, j=tracks.length, track,
off = true;
for (;i<j;i++) {
track = tracks[i];
2012-03-16 12:29:38 -07:00
if (track.kind == this.track.kind && track.mode == 2) {
off = false;
}
}
2012-03-16 12:29:38 -07:00
if (off) {
this.selected(true);
} else {
this.selected(false);
}
}
});
/* Captions Button
================================================================================ */
_V_.TextTrackButton = _V_.Button.extend({
init: function(player, options){
this._super(player, options);
this.menu = this.createMenu();
if (this.items.length === 0) {
this.hide();
}
},
2012-03-16 12:29:38 -07:00
createMenu: function(){
var menu = new _V_.Menu(this.player);
// Add a title list item to the top
menu.el.appendChild(_V_.createElement("li", {
className: "vjs-menu-title",
innerHTML: _V_.uc(this.kind)
}));
// Add an OFF menu item to turn all tracks off
menu.addItem(new _V_.OffTextTrackMenuItem(this.player, { kind: this.kind }))
this.items = this.createItems();
// Add menu items to the menu
this.each(this.items, function(item){
menu.addItem(item);
});
// Add list to element
this.addComponent(menu);
return menu;
},
// Create a menu item for each text track
createItems: function(){
var items = [];
this.each(this.player.textTracks, function(track){
if (track.kind === this.kind) {
items.push(new _V_.TextTrackMenuItem(this.player, {
track: track
}));
}
2012-03-16 12:29:38 -07:00
});
return items;
},
buildCSSClass: function(){
2012-03-16 12:29:38 -07:00
return this.className + " vjs-menu-button " + this._super();
},
// Focus - Add keyboard functionality to element
onFocus: function(){
// Show the menu, and keep showing when the menu items are in focus
this.menu.lockShowing();
// this.menu.el.style.display = "block";
// When tabbing through, the menu should hide when focus goes from the last menu item to the next tabbed element.
_V_.one(this.menu.el.childNodes[this.menu.el.childNodes.length - 1], "blur", this.proxy(function(){
this.menu.unlockShowing();
}));
},
// Can't turn off list display that we turned on with focus, because list would go away.
2012-03-16 12:29:38 -07:00
onBlur: function(){},
onClick: function(){
// When you click the button it adds focus, which will show the menu indefinitely.
// So we'll remove focus when the mouse leaves the button.
// Focus is needed for tab navigation.
this.one("mouseout", this.proxy(function(){
this.menu.unlockShowing();
this.el.blur();
}));
}
});
_V_.CaptionsButton = _V_.TextTrackButton.extend({
kind: "captions",
buttonText: "Captions",
className: "vjs-captions-button"
});
_V_.SubtitlesButton = _V_.TextTrackButton.extend({
kind: "subtitles",
buttonText: "Subtitles",
className: "vjs-subtitles-button"
});
2012-03-16 12:29:38 -07:00
// Chapters act much differently than other text tracks
// Cues are navigation vs. other tracks of alternative languages
_V_.ChaptersButton = _V_.TextTrackButton.extend({
kind: "chapters",
buttonText: "Chapters",
className: "vjs-chapters-button",
// Create a menu item for each text track
createItems: function(chaptersTrack){
var items = [];
this.each(this.player.textTracks, function(track){
if (track.kind === this.kind) {
items.push(new _V_.TextTrackMenuItem(this.player, {
track: track
}));
}
});
return items;
},
createMenu: function(){
var tracks = this.player.textTracks,
i = 0,
j = tracks.length,
track, chaptersTrack,
items = this.items = [];
for (;i<j;i++) {
track = tracks[i];
if (track.kind == this.kind && track["default"]) {
if (track.readyState < 2) {
this.chaptersTrack = track;
track.on("loaded", this.proxy(this.createMenu));
2012-03-16 12:29:38 -07:00
return;
} else {
chaptersTrack = track;
break;
}
}
}
var menu = this.menu = new _V_.Menu(this.player);
menu.el.appendChild(_V_.createElement("li", {
className: "vjs-menu-title",
innerHTML: _V_.uc(this.kind)
}));
if (chaptersTrack) {
var cues = chaptersTrack.cues,
i = 0, j = cues.length, cue, mi;
for (;i<j;i++) {
cue = cues[i];
mi = new _V_.ChaptersTrackMenuItem(this.player, {
track: chaptersTrack,
cue: cue
});
items.push(mi);
menu.addComponent(mi);
}
}
// Add list to element
this.addComponent(menu);
if (this.items.length > 0) {
this.show();
}
return menu;
}
});
_V_.ChaptersTrackMenuItem = _V_.MenuItem.extend({
init: function(player, options){
var track = this.track = options.track,
cue = this.cue = options.cue,
currentTime = player.currentTime();
// Modify options for parent MenuItem class's init.
options.label = cue.text;
options.selected = (cue.startTime <= currentTime && currentTime < cue.endTime);
this._super(player, options);
track.on("cuechange", _V_.proxy(this, this.update));
2012-03-16 12:29:38 -07:00
},
onClick: function(){
this._super();
this.player.currentTime(this.cue.startTime);
this.update(this.cue.startTime);
},
update: function(time){
var cue = this.cue,
currentTime = this.player.currentTime();
2012-03-16 12:29:38 -07:00
// _V_.log(currentTime, cue.startTime);
if (cue.startTime <= currentTime && currentTime < cue.endTime) {
this.selected(true);
} else {
this.selected(false);
}
}
});
// Add Buttons to controlBar
_V_.merge(_V_.ControlBar.prototype.options.components, {
"subtitlesButton": {},
2012-03-16 12:29:38 -07:00
"captionsButton": {},
"chaptersButton": {}
});
// _V_.Cue = _V_.Component.extend({
// init: function(player, options){
// this._super(player, options);
// }
// });