Source: lib/text/simple_text_displayer.js

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