Source: lib/text/simple_text_displayer.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @fileoverview
  8. * @suppress {missingRequire} TODO(b/152540451): this shouldn't be needed
  9. */
  10. goog.provide('shaka.text.SimpleTextDisplayer');
  11. goog.require('goog.asserts');
  12. goog.require('shaka.Deprecate');
  13. goog.require('shaka.log');
  14. goog.require('shaka.text.Cue');
  15. /**
  16. * A text displayer plugin using the browser's native VTTCue interface.
  17. *
  18. * @implements {shaka.extern.TextDisplayer}
  19. * @export
  20. */
  21. shaka.text.SimpleTextDisplayer = class {
  22. /** @param {HTMLMediaElement} video */
  23. constructor(video) {
  24. /** @private {TextTrack} */
  25. this.textTrack_ = null;
  26. // TODO: Test that in all cases, the built-in CC controls in the video
  27. // element are toggling our TextTrack.
  28. // If the video element has TextTracks, disable them. If we see one that
  29. // was created by a previous instance of Shaka Player, reuse it.
  30. for (const track of Array.from(video.textTracks)) {
  31. // NOTE: There is no API available to remove a TextTrack from a video
  32. // element.
  33. track.mode = 'disabled';
  34. if (track.label == shaka.Player.TextTrackLabel) {
  35. this.textTrack_ = track;
  36. }
  37. }
  38. if (!this.textTrack_) {
  39. // As far as I can tell, there is no observable difference between setting
  40. // kind to 'subtitles' or 'captions' when creating the TextTrack object.
  41. // The individual text tracks from the manifest will still have their own
  42. // kinds which can be displayed in the app's UI.
  43. this.textTrack_ = video.addTextTrack(
  44. 'subtitles', shaka.Player.TextTrackLabel);
  45. }
  46. this.textTrack_.mode = 'hidden';
  47. }
  48. /**
  49. * @override
  50. * @export
  51. */
  52. remove(start, end) {
  53. // Check that the displayer hasn't been destroyed.
  54. if (!this.textTrack_) {
  55. return false;
  56. }
  57. const removeInRange = (cue) => {
  58. const inside = cue.startTime < end && cue.endTime > start;
  59. return inside;
  60. };
  61. shaka.text.SimpleTextDisplayer.removeWhere_(this.textTrack_, removeInRange);
  62. return true;
  63. }
  64. /**
  65. * @override
  66. * @export
  67. */
  68. append(cues) {
  69. // Flatten nested cue payloads recursively. If a cue has nested cues,
  70. // their contents should be combined and replace the payload of the parent.
  71. const flattenPayload = (cue) => {
  72. // Handle styles (currently bold/italics/underline).
  73. // TODO add support for color rendering.
  74. const openStyleTags = [];
  75. const bold = cue.fontWeight >= shaka.text.Cue.fontWeight.BOLD;
  76. const italics = cue.fontStyle == shaka.text.Cue.fontStyle.ITALIC;
  77. const underline = cue.textDecoration.includes(
  78. shaka.text.Cue.textDecoration.UNDERLINE);
  79. if (bold) {
  80. openStyleTags.push('b');
  81. }
  82. if (italics) {
  83. openStyleTags.push('i');
  84. }
  85. if (underline) {
  86. openStyleTags.push('u');
  87. }
  88. // Prefix opens tags, suffix closes tags in reverse order of opening.
  89. const prefixStyleTags = openStyleTags.reduce((acc, tag) => {
  90. return `${acc}<${tag}>`;
  91. }, '');
  92. const suffixStyleTags = openStyleTags.reduceRight((acc, tag) => {
  93. return `${acc}</${tag}>`;
  94. }, '');
  95. if (cue.lineBreak || cue.spacer) {
  96. if (cue.spacer) {
  97. shaka.Deprecate.deprecateFeature(4,
  98. 'shaka.extern.Cue',
  99. 'Please use lineBreak instead of spacer.');
  100. }
  101. // This is a vertical lineBreak, so insert a newline.
  102. return '\n';
  103. } else if (cue.nestedCues.length) {
  104. return cue.nestedCues.map(flattenPayload).join('');
  105. } else {
  106. // This is a real cue.
  107. return prefixStyleTags + cue.payload + suffixStyleTags;
  108. }
  109. };
  110. // We don't want to modify the array or objects passed in, since we don't
  111. // technically own them. So we build a new array and replace certain items
  112. // in it if they need to be flattened.
  113. const flattenedCues = cues.map((cue) => {
  114. if (cue.nestedCues.length) {
  115. const flatCue = cue.clone();
  116. flatCue.nestedCues = [];
  117. flatCue.payload = flattenPayload(cue);
  118. return flatCue;
  119. } else {
  120. return cue;
  121. }
  122. });
  123. // Convert cues.
  124. const textTrackCues = [];
  125. const cuesInTextTrack = this.textTrack_.cues ?
  126. Array.from(this.textTrack_.cues) : [];
  127. for (const inCue of flattenedCues) {
  128. // When a VTT cue spans a segment boundary, the cue will be duplicated
  129. // into two segments.
  130. // To avoid displaying duplicate cues, if the current textTrack cues
  131. // list already contains the cue, skip it.
  132. const containsCue = cuesInTextTrack.some((cueInTextTrack) => {
  133. if (cueInTextTrack.startTime == inCue.startTime &&
  134. cueInTextTrack.endTime == inCue.endTime &&
  135. cueInTextTrack.text == inCue.payload) {
  136. return true;
  137. }
  138. return false;
  139. });
  140. if (!containsCue) {
  141. const cue =
  142. shaka.text.SimpleTextDisplayer.convertToTextTrackCue_(inCue);
  143. if (cue) {
  144. textTrackCues.push(cue);
  145. }
  146. }
  147. }
  148. // Sort the cues based on start/end times. Make a copy of the array so
  149. // we can get the index in the original ordering. Out of order cues are
  150. // rejected by Edge. See https://bit.ly/2K9VX3s
  151. const sortedCues = textTrackCues.slice().sort((a, b) => {
  152. if (a.startTime != b.startTime) {
  153. return a.startTime - b.startTime;
  154. } else if (a.endTime != b.endTime) {
  155. return a.endTime - b.startTime;
  156. } else {
  157. // The browser will display cues with identical time ranges from the
  158. // bottom up. Reversing the order of equal cues means the first one
  159. // parsed will be at the top, as you would expect.
  160. // See https://github.com/google/shaka-player/issues/848 for more info.
  161. // However, this ordering behavior is part of VTTCue's "line" field.
  162. // Some platforms don't have a real VTTCue and use a polyfill instead.
  163. // When VTTCue is polyfilled or does not support "line", we should _not_
  164. // reverse the order. This occurs on legacy Edge.
  165. // eslint-disable-next-line no-restricted-syntax
  166. if ('line' in VTTCue.prototype) {
  167. // Native VTTCue
  168. return textTrackCues.indexOf(b) - textTrackCues.indexOf(a);
  169. } else {
  170. // Polyfilled VTTCue
  171. return textTrackCues.indexOf(a) - textTrackCues.indexOf(b);
  172. }
  173. }
  174. });
  175. for (const cue of sortedCues) {
  176. this.textTrack_.addCue(cue);
  177. }
  178. }
  179. /**
  180. * @override
  181. * @export
  182. */
  183. destroy() {
  184. if (this.textTrack_) {
  185. const removeIt = (cue) => true;
  186. shaka.text.SimpleTextDisplayer.removeWhere_(this.textTrack_, removeIt);
  187. // NOTE: There is no API available to remove a TextTrack from a video
  188. // element.
  189. this.textTrack_.mode = 'disabled';
  190. }
  191. this.textTrack_ = null;
  192. return Promise.resolve();
  193. }
  194. /**
  195. * @override
  196. * @export
  197. */
  198. isTextVisible() {
  199. return this.textTrack_.mode == 'showing';
  200. }
  201. /**
  202. * @override
  203. * @export
  204. */
  205. setTextVisibility(on) {
  206. this.textTrack_.mode = on ? 'showing' : 'hidden';
  207. }
  208. /**
  209. * @param {!shaka.extern.Cue} shakaCue
  210. * @return {TextTrackCue}
  211. * @private
  212. */
  213. static convertToTextTrackCue_(shakaCue) {
  214. if (shakaCue.startTime >= shakaCue.endTime) {
  215. // Edge will throw in this case.
  216. // See issue #501
  217. shaka.log.warning('Invalid cue times: ' + shakaCue.startTime +
  218. ' - ' + shakaCue.endTime);
  219. return null;
  220. }
  221. const Cue = shaka.text.Cue;
  222. /** @type {VTTCue} */
  223. const vttCue = new VTTCue(
  224. shakaCue.startTime,
  225. shakaCue.endTime,
  226. shakaCue.payload);
  227. // NOTE: positionAlign and lineAlign settings are not supported by Chrome
  228. // at the moment, so setting them will have no effect.
  229. // The bug on chromium to implement them:
  230. // https://bugs.chromium.org/p/chromium/issues/detail?id=633690
  231. vttCue.lineAlign = shakaCue.lineAlign;
  232. vttCue.positionAlign = shakaCue.positionAlign;
  233. if (shakaCue.size) {
  234. vttCue.size = shakaCue.size;
  235. }
  236. try {
  237. // Safari 10 seems to throw on align='center'.
  238. vttCue.align = shakaCue.textAlign;
  239. } catch (exception) {}
  240. if (shakaCue.textAlign == 'center' && vttCue.align != 'center') {
  241. // We want vttCue.position = 'auto'. By default, |position| is set to
  242. // "auto". If we set it to "auto" safari will throw an exception, so we
  243. // must rely on the default value.
  244. vttCue.align = 'middle';
  245. }
  246. if (shakaCue.writingMode ==
  247. Cue.writingMode.VERTICAL_LEFT_TO_RIGHT) {
  248. vttCue.vertical = 'lr';
  249. } else if (shakaCue.writingMode ==
  250. Cue.writingMode.VERTICAL_RIGHT_TO_LEFT) {
  251. vttCue.vertical = 'rl';
  252. }
  253. // snapToLines flag is true by default
  254. if (shakaCue.lineInterpretation == Cue.lineInterpretation.PERCENTAGE) {
  255. vttCue.snapToLines = false;
  256. }
  257. if (shakaCue.line != null) {
  258. vttCue.line = shakaCue.line;
  259. }
  260. if (shakaCue.position != null) {
  261. vttCue.position = shakaCue.position;
  262. }
  263. return vttCue;
  264. }
  265. /**
  266. * Iterate over all the cues in a text track and remove all those for which
  267. * |predicate(cue)| returns true.
  268. *
  269. * @param {!TextTrack} track
  270. * @param {function(!TextTrackCue):boolean} predicate
  271. * @private
  272. */
  273. static removeWhere_(track, predicate) {
  274. // Since |track.cues| can be null if |track.mode| is "disabled", force it to
  275. // something other than "disabled".
  276. //
  277. // If the track is already showing, then we should keep it as showing. But
  278. // if it something else, we will use hidden so that we don't "flash" cues on
  279. // the screen.
  280. const oldState = track.mode;
  281. const tempState = oldState == 'showing' ? 'showing' : 'hidden';
  282. track.mode = tempState;
  283. goog.asserts.assert(
  284. track.cues,
  285. 'Cues should be accessible when mode is set to "' + tempState + '".');
  286. // Create a copy of the list to avoid errors while iterating.
  287. for (const cue of Array.from(track.cues)) {
  288. if (cue && predicate(cue)) {
  289. track.removeCue(cue);
  290. }
  291. }
  292. track.mode = oldState;
  293. }
  294. };