mirror of
https://github.com/shaka-project/shaka-player.git
synced 2026-06-15 16:06:41 +03:00
c70367dc97
Closes #796. Closes #923. Change-Id: Ifc2017b40a0fb570103f0fed7bc130aa24819e9f
226 lines
5.9 KiB
JavaScript
226 lines
5.9 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2016 Google Inc.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
goog.provide('shaka.text.SimpleTextDisplayer');
|
|
|
|
goog.require('shaka.log');
|
|
|
|
|
|
|
|
/**
|
|
* <p>
|
|
* This defines the default text displayer plugin. An instance of this
|
|
* class is used when no custom displayer is given.
|
|
* </p>
|
|
* <p>
|
|
* This class simply converts shaka.text.Cue objects to
|
|
* TextTrackCues and feeds them to the browser.
|
|
* </p>
|
|
*
|
|
* @param {HTMLMediaElement} video
|
|
* @constructor
|
|
* @struct
|
|
* @implements {shakaExtern.TextDisplayer}
|
|
* @export
|
|
*/
|
|
shaka.text.SimpleTextDisplayer = function(video) {
|
|
/** @private {TextTrack} */
|
|
this.textTrack_ = null;
|
|
|
|
// TODO: test that in all cases, the built-in CC controls in the video element
|
|
// are toggling our TextTrack.
|
|
|
|
// If the video element has TextTracks, disable them. If we see one that
|
|
// was created by a previous instance of Shaka Player, reuse it.
|
|
for (var i = 0; i < video.textTracks.length; ++i) {
|
|
var track = video.textTracks[i];
|
|
track.mode = 'disabled';
|
|
|
|
if (track.label == shaka.text.SimpleTextDisplayer.TextTrackLabel_) {
|
|
this.textTrack_ = track;
|
|
}
|
|
}
|
|
|
|
if (!this.textTrack_) {
|
|
// As far as I can tell, there is no observable difference between setting
|
|
// kind to 'subtitles' or 'captions' when creating the TextTrack object.
|
|
// The individual text tracks from the manifest will still have their own
|
|
// kinds which can be displayed in the app's UI.
|
|
this.textTrack_ = video.addTextTrack(
|
|
'subtitles', shaka.text.SimpleTextDisplayer.TextTrackLabel_);
|
|
}
|
|
this.textTrack_.mode = 'hidden';
|
|
};
|
|
|
|
|
|
/**
|
|
* @override
|
|
* @export
|
|
*/
|
|
shaka.text.SimpleTextDisplayer.prototype.remove = function(start, end) {
|
|
// Check that the displayer hasn't been destroyed.
|
|
if (!this.textTrack_) return false;
|
|
|
|
this.removeWhere_(function(cue) {
|
|
if (cue.startTime >= end || cue.endTime <= start) {
|
|
// Outside the remove range. Hang on to it.
|
|
return false;
|
|
}
|
|
return true;
|
|
});
|
|
|
|
return true;
|
|
};
|
|
|
|
|
|
/**
|
|
* @override
|
|
* @export
|
|
*/
|
|
shaka.text.SimpleTextDisplayer.prototype.append = function(cues) {
|
|
var textTrackCues = [];
|
|
for (var i = 0; i < cues.length; i++) {
|
|
var cue = this.convertToTextTrackCue_(cues[i]);
|
|
if (cue)
|
|
textTrackCues.push(cue);
|
|
}
|
|
|
|
textTrackCues.forEach(function(cue) {
|
|
this.textTrack_.addCue(cue);
|
|
}.bind(this));
|
|
};
|
|
|
|
|
|
/**
|
|
* @override
|
|
* @export
|
|
*/
|
|
shaka.text.SimpleTextDisplayer.prototype.destroy = function() {
|
|
if (this.textTrack_) {
|
|
this.removeWhere_(function(cue) { return true; });
|
|
}
|
|
|
|
this.textTrack_ = null;
|
|
return Promise.resolve();
|
|
};
|
|
|
|
|
|
/**
|
|
* @override
|
|
* @export
|
|
*/
|
|
shaka.text.SimpleTextDisplayer.prototype.isTextVisible = function() {
|
|
return this.textTrack_.mode == 'showing';
|
|
};
|
|
|
|
|
|
/**
|
|
* @override
|
|
* @export
|
|
*/
|
|
shaka.text.SimpleTextDisplayer.prototype.setTextVisibility = function(on) {
|
|
this.textTrack_.mode = on ? 'showing' : 'hidden';
|
|
};
|
|
|
|
|
|
/**
|
|
* @param {!shaka.text.Cue} shakaCue
|
|
* @return {TextTrackCue}
|
|
* @private
|
|
*/
|
|
shaka.text.SimpleTextDisplayer.prototype.convertToTextTrackCue_ =
|
|
function(shakaCue) {
|
|
if (shakaCue.startTime >= shakaCue.endTime) {
|
|
// IE/Edge will throw in this case.
|
|
// See issue #501
|
|
shaka.log.warning('Invalid cue times: ' + shakaCue.startTime +
|
|
' - ' + shakaCue.endTime);
|
|
return null;
|
|
}
|
|
|
|
var Cue = shaka.text.Cue;
|
|
var vttCue = new VTTCue(shakaCue.startTime,
|
|
shakaCue.endTime,
|
|
shakaCue.payload);
|
|
|
|
// NOTE: positionAlign and lineAlign settings are not supported by Chrome
|
|
// at the moment, so setting them will have no effect.
|
|
// The bug on chromium to implement them:
|
|
// https://bugs.chromium.org/p/chromium/issues/detail?id=633690
|
|
|
|
vttCue.lineAlign = shakaCue.lineAlign;
|
|
vttCue.positionAlign = shakaCue.positionAlign;
|
|
vttCue.size = shakaCue.size;
|
|
vttCue.align = shakaCue.textAlign;
|
|
|
|
if (shakaCue.textAlign == 'center' && vttCue.align != 'center') {
|
|
// Workaround for a Chrome bug http://crbug.com/663797
|
|
// Chrome does not support align = 'center'
|
|
vttCue.position = 'auto';
|
|
vttCue.align = 'middle';
|
|
}
|
|
|
|
if (shakaCue.writingDirection == Cue.writingDirection.VERTICAL_LEFT)
|
|
vttCue.vertical = 'lr';
|
|
else if (shakaCue.writingDirection == Cue.writingDirection.VERTICAL_RIGHT)
|
|
vttCue.vertical = 'rl';
|
|
|
|
// snapToLines flag is true by default
|
|
if (shakaCue.lineInterpretation == Cue.lineInterpretation.PERCENTAGE)
|
|
vttCue.snapToLines = false;
|
|
|
|
if (shakaCue.line)
|
|
vttCue.line = shakaCue.line;
|
|
|
|
if (shakaCue.position)
|
|
vttCue.position = shakaCue.position;
|
|
|
|
return vttCue;
|
|
};
|
|
|
|
|
|
/**
|
|
* Remove all cues for which the matching function returns true.
|
|
*
|
|
* @param {function(!TextTrackCue):boolean} predicate
|
|
* @private
|
|
*/
|
|
shaka.text.SimpleTextDisplayer.prototype.removeWhere_ = function(predicate) {
|
|
var cues = this.textTrack_.cues;
|
|
var removeMe = [];
|
|
|
|
// Remove these in another loop to avoid mutating the TextTrackCueList
|
|
// while iterating over it. This allows us to avoid making assumptions
|
|
// about whether or not this.textTrack_.remove() will alter that list.
|
|
for (var i = 0; i < cues.length; ++i) {
|
|
if (predicate(cues[i])) {
|
|
removeMe.push(cues[i]);
|
|
}
|
|
}
|
|
|
|
for (var i = 0; i < removeMe.length; ++i) {
|
|
this.textTrack_.removeCue(removeMe[i]);
|
|
}
|
|
};
|
|
|
|
|
|
/**
|
|
* @const {string}
|
|
* @private
|
|
*/
|
|
shaka.text.SimpleTextDisplayer.TextTrackLabel_ = 'Shaka Player TextTrack';
|