Source: lib/text/simple_text_displayer.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.text.SimpleTextDisplayer');
  18. goog.require('goog.asserts');
  19. goog.require('shaka.log');
  20. goog.require('shaka.text.Cue');
  21. /**
  22. * <p>
  23. * This defines the default text displayer plugin. An instance of this
  24. * class is used when no custom displayer is given.
  25. * </p>
  26. * <p>
  27. * This class simply converts shaka.text.Cue objects to
  28. * TextTrackCues and feeds them to the browser.
  29. * </p>
  30. *
  31. * @param {HTMLMediaElement} video
  32. * @constructor
  33. * @struct
  34. * @implements {shakaExtern.TextDisplayer}
  35. * @export
  36. */
  37. shaka.text.SimpleTextDisplayer = function(video) {
  38. /** @private {TextTrack} */
  39. this.textTrack_ = null;
  40. // TODO: Test that in all cases, the built-in CC controls in the video element
  41. // are toggling our TextTrack.
  42. // If the video element has TextTracks, disable them. If we see one that
  43. // was created by a previous instance of Shaka Player, reuse it.
  44. for (let i = 0; i < video.textTracks.length; ++i) {
  45. let track = video.textTracks[i];
  46. track.mode = 'disabled';
  47. if (track.label == shaka.text.SimpleTextDisplayer.TextTrackLabel_) {
  48. this.textTrack_ = track;
  49. }
  50. }
  51. if (!this.textTrack_) {
  52. // As far as I can tell, there is no observable difference between setting
  53. // kind to 'subtitles' or 'captions' when creating the TextTrack object.
  54. // The individual text tracks from the manifest will still have their own
  55. // kinds which can be displayed in the app's UI.
  56. this.textTrack_ = video.addTextTrack(
  57. 'subtitles', shaka.text.SimpleTextDisplayer.TextTrackLabel_);
  58. }
  59. this.textTrack_.mode = 'hidden';
  60. };
  61. /**
  62. * @override
  63. * @export
  64. */
  65. shaka.text.SimpleTextDisplayer.prototype.remove = function(start, end) {
  66. // Check that the displayer hasn't been destroyed.
  67. if (!this.textTrack_) return false;
  68. let removeInRange = (cue) => {
  69. let outside = cue.startTime >= end || cue.endTime <= start;
  70. let inside = !outside;
  71. return inside;
  72. };
  73. shaka.text.SimpleTextDisplayer.removeWhere_(this.textTrack_, removeInRange);
  74. return true;
  75. };
  76. /**
  77. * @override
  78. * @export
  79. */
  80. shaka.text.SimpleTextDisplayer.prototype.append = function(cues) {
  81. const convertToTextTrackCue =
  82. shaka.text.SimpleTextDisplayer.convertToTextTrackCue_;
  83. // Convert cues.
  84. let textTrackCues = [];
  85. for (let i = 0; i < cues.length; i++) {
  86. let cue = convertToTextTrackCue(cues[i]);
  87. if (cue) {
  88. textTrackCues.push(cue);
  89. }
  90. }
  91. // Sort the cues based on start/end times. Make a copy of the array so
  92. // we can get the index in the original ordering. Out of order cues are
  93. // rejected by IE/Edge. See https://goo.gl/BirBy9
  94. let sortedCues = textTrackCues.slice().sort(function(a, b) {
  95. if (a.startTime != b.startTime) {
  96. return a.startTime - b.startTime;
  97. } else if (a.endTime != b.endTime) {
  98. return a.endTime - b.startTime;
  99. } else {
  100. // The browser will display cues with identical time ranges from the
  101. // bottom up. Reversing the order of equal cues means the first one
  102. // parsed will be at the top, as you would expect.
  103. // See https://github.com/google/shaka-player/issues/848 for more info.
  104. return textTrackCues.indexOf(b) - textTrackCues.indexOf(a);
  105. }
  106. });
  107. sortedCues.forEach(function(cue) {
  108. this.textTrack_.addCue(cue);
  109. }.bind(this));
  110. };
  111. /**
  112. * @override
  113. * @export
  114. */
  115. shaka.text.SimpleTextDisplayer.prototype.destroy = function() {
  116. if (this.textTrack_) {
  117. let removeIt = (cue) => true;
  118. shaka.text.SimpleTextDisplayer.removeWhere_(this.textTrack_, removeIt);
  119. }
  120. this.textTrack_ = null;
  121. return Promise.resolve();
  122. };
  123. /**
  124. * @override
  125. * @export
  126. */
  127. shaka.text.SimpleTextDisplayer.prototype.isTextVisible = function() {
  128. return this.textTrack_.mode == 'showing';
  129. };
  130. /**
  131. * @override
  132. * @export
  133. */
  134. shaka.text.SimpleTextDisplayer.prototype.setTextVisibility = function(on) {
  135. this.textTrack_.mode = on ? 'showing' : 'hidden';
  136. };
  137. /**
  138. * @param {!shakaExtern.Cue} shakaCue
  139. * @return {TextTrackCue}
  140. * @private
  141. */
  142. shaka.text.SimpleTextDisplayer.convertToTextTrackCue_ = function(shakaCue) {
  143. if (shakaCue.startTime >= shakaCue.endTime) {
  144. // IE/Edge will throw in this case.
  145. // See issue #501
  146. shaka.log.warning('Invalid cue times: ' + shakaCue.startTime +
  147. ' - ' + shakaCue.endTime);
  148. return null;
  149. }
  150. const Cue = shaka.text.Cue;
  151. /** @type {VTTCue} */
  152. let vttCue = new VTTCue(shakaCue.startTime,
  153. shakaCue.endTime,
  154. shakaCue.payload);
  155. // NOTE: positionAlign and lineAlign settings are not supported by Chrome
  156. // at the moment, so setting them will have no effect.
  157. // The bug on chromium to implement them:
  158. // https://bugs.chromium.org/p/chromium/issues/detail?id=633690
  159. vttCue.lineAlign = shakaCue.lineAlign;
  160. vttCue.positionAlign = shakaCue.positionAlign;
  161. vttCue.size = shakaCue.size;
  162. try {
  163. // Safari 10 seems to throw on align='center'.
  164. vttCue.align = shakaCue.textAlign;
  165. } catch (exception) {}
  166. if (shakaCue.textAlign == 'center' && vttCue.align != 'center') {
  167. // We want vttCue.position = 'auto'. By default, |position| is set to
  168. // "auto". If we set it to "auto" safari will throw an exception, so we
  169. // must rely on the default value.
  170. vttCue.align = 'middle';
  171. }
  172. if (shakaCue.writingDirection ==
  173. Cue.writingDirection.VERTICAL_LEFT_TO_RIGHT) {
  174. vttCue.vertical = 'lr';
  175. } else if (shakaCue.writingDirection ==
  176. Cue.writingDirection.VERTICAL_RIGHT_TO_LEFT) {
  177. vttCue.vertical = 'rl';
  178. }
  179. // snapToLines flag is true by default
  180. if (shakaCue.lineInterpretation == Cue.lineInterpretation.PERCENTAGE) {
  181. vttCue.snapToLines = false;
  182. }
  183. if (shakaCue.line != null) {
  184. vttCue.line = shakaCue.line;
  185. }
  186. if (shakaCue.position != null) {
  187. vttCue.position = shakaCue.position;
  188. }
  189. return vttCue;
  190. };
  191. /**
  192. * Iterate over all the cues in a text track and remove all those for which
  193. * |predicate(cue)| returns true.
  194. *
  195. * @param {!TextTrack} track
  196. * @param {function(!TextTrackCue):boolean} predicate
  197. * @private
  198. */
  199. shaka.text.SimpleTextDisplayer.removeWhere_ = function(track, predicate) {
  200. // Since |track.cues| can be null if |track.mode| is "disabled", force it to
  201. // something other than "disabled".
  202. //
  203. // If the track is already showing, then we should keep it as showing. But if
  204. // it something else, we will use hidden so that we don't "flash" cues on the
  205. // screen.
  206. let oldState = track.mode;
  207. let tempState = oldState == 'showing' ? 'showing' : 'hidden';
  208. track.mode = tempState;
  209. goog.asserts.assert(
  210. track.cues,
  211. 'Cues should be accessible when mode is set to "' + tempState + '".');
  212. // Go backward so that if a removal is done, it should not cause problems
  213. // with future indexing. In the case that the underlying implementation
  214. // returns a copy (and not a shared instance) cache a copy of the tracks.
  215. let cues = track.cues;
  216. for (let i = cues.length - 1; i >= 0; i--) {
  217. let cue = cues[i];
  218. if (cue && predicate(cue)) {
  219. track.removeCue(cue);
  220. }
  221. }
  222. track.mode = oldState;
  223. };
  224. /**
  225. * @const {string}
  226. * @private
  227. */
  228. shaka.text.SimpleTextDisplayer.TextTrackLabel_ = 'Shaka Player TextTrack';