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.log');
  13. goog.require('shaka.text.Cue');
  14. /**
  15. * A text displayer plugin using the browser's native VTTCue interface.
  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 nested cue payloads recursively. If a cue has nested cues,
  69. // their contents should be combined and replace the payload of the parent.
  70. const flattenPayload = (cue) => {
  71. // Handle styles (currently bold/italics/underline).
  72. // TODO add support for color rendering.
  73. const openStyleTags = [];
  74. const bold = cue.fontWeight >= shaka.text.Cue.fontWeight.BOLD;
  75. const italics = cue.fontStyle == shaka.text.Cue.fontStyle.ITALIC;
  76. const underline = cue.textDecoration.includes(
  77. shaka.text.Cue.textDecoration.UNDERLINE);
  78. if (bold) {
  79. openStyleTags.push('b');
  80. }
  81. if (italics) {
  82. openStyleTags.push('i');
  83. }
  84. if (underline) {
  85. openStyleTags.push('u');
  86. }
  87. // Prefix opens tags, suffix closes tags in reverse order of opening.
  88. const prefixStyleTags = openStyleTags.reduce((acc, tag) => {
  89. return `${acc}<${tag}>`;
  90. }, '');
  91. const suffixStyleTags = openStyleTags.reduceRight((acc, tag) => {
  92. return `${acc}</${tag}>`;
  93. }, '');
  94. if (cue.lineBreak) {
  95. // This is a vertical lineBreak, so insert a newline.
  96. return '\n';
  97. } else if (cue.nestedCues.length) {
  98. return cue.nestedCues.map(flattenPayload).join('');
  99. } else {
  100. // This is a real cue.
  101. return prefixStyleTags + cue.payload + suffixStyleTags;
  102. }
  103. };
  104. // We don't want to modify the array or objects passed in, since we don't
  105. // technically own them. So we build a new array and replace certain items
  106. // in it if they need to be flattened.
  107. // We also don't want to flatten the text payloads starting at a container
  108. // element; otherwise, for containers encapsulating multiple caption lines,
  109. // the lines would merge into a single cue. This is undesirable when a
  110. // subset of the captions are outside of the append time window. To fix
  111. // this, we only call flattenPayload() starting at elements marked as
  112. // isContainer = false.
  113. const getCuesToFlatten = (cues, result) => {
  114. for (const cue of cues) {
  115. if (cue.isContainer) {
  116. // Recurse to find the actual text payload cues.
  117. getCuesToFlatten(cue.nestedCues, result);
  118. } else {
  119. // Flatten the payload.
  120. const flatCue = cue.clone();
  121. flatCue.nestedCues = [];
  122. flatCue.payload = flattenPayload(cue);
  123. result.push(flatCue);
  124. }
  125. }
  126. return result;
  127. };
  128. const flattenedCues = getCuesToFlatten(cues, []);
  129. // Convert cues.
  130. const textTrackCues = [];
  131. const cuesInTextTrack = this.textTrack_.cues ?
  132. Array.from(this.textTrack_.cues) : [];
  133. for (const inCue of flattenedCues) {
  134. // When a VTT cue spans a segment boundary, the cue will be duplicated
  135. // into two segments.
  136. // To avoid displaying duplicate cues, if the current textTrack cues
  137. // list already contains the cue, skip it.
  138. const containsCue = cuesInTextTrack.some((cueInTextTrack) => {
  139. if (cueInTextTrack.startTime == inCue.startTime &&
  140. cueInTextTrack.endTime == inCue.endTime &&
  141. cueInTextTrack.text == inCue.payload) {
  142. return true;
  143. }
  144. return false;
  145. });
  146. if (!containsCue) {
  147. const cue =
  148. shaka.text.SimpleTextDisplayer.convertToTextTrackCue_(inCue);
  149. if (cue) {
  150. textTrackCues.push(cue);
  151. }
  152. }
  153. }
  154. // Sort the cues based on start/end times. Make a copy of the array so
  155. // we can get the index in the original ordering. Out of order cues are
  156. // rejected by Edge. See https://bit.ly/2K9VX3s
  157. const sortedCues = textTrackCues.slice().sort((a, b) => {
  158. if (a.startTime != b.startTime) {
  159. return a.startTime - b.startTime;
  160. } else if (a.endTime != b.endTime) {
  161. return a.endTime - b.startTime;
  162. } else {
  163. // The browser will display cues with identical time ranges from the
  164. // bottom up. Reversing the order of equal cues means the first one
  165. // parsed will be at the top, as you would expect.
  166. // See https://github.com/shaka-project/shaka-player/issues/848 for
  167. // more info.
  168. // However, this ordering behavior is part of VTTCue's "line" field.
  169. // Some platforms don't have a real VTTCue and use a polyfill instead.
  170. // When VTTCue is polyfilled or does not support "line", we should _not_
  171. // reverse the order. This occurs on legacy Edge.
  172. // eslint-disable-next-line no-restricted-syntax
  173. if ('line' in VTTCue.prototype) {
  174. // Native VTTCue
  175. return textTrackCues.indexOf(b) - textTrackCues.indexOf(a);
  176. } else {
  177. // Polyfilled VTTCue
  178. return textTrackCues.indexOf(a) - textTrackCues.indexOf(b);
  179. }
  180. }
  181. });
  182. for (const cue of sortedCues) {
  183. this.textTrack_.addCue(cue);
  184. }
  185. }
  186. /**
  187. * @override
  188. * @export
  189. */
  190. destroy() {
  191. if (this.textTrack_) {
  192. const removeIt = (cue) => true;
  193. shaka.text.SimpleTextDisplayer.removeWhere_(this.textTrack_, removeIt);
  194. // NOTE: There is no API available to remove a TextTrack from a video
  195. // element.
  196. this.textTrack_.mode = 'disabled';
  197. }
  198. this.textTrack_ = null;
  199. return Promise.resolve();
  200. }
  201. /**
  202. * @override
  203. * @export
  204. */
  205. isTextVisible() {
  206. return this.textTrack_.mode == 'showing';
  207. }
  208. /**
  209. * @override
  210. * @export
  211. */
  212. setTextVisibility(on) {
  213. this.textTrack_.mode = on ? 'showing' : 'hidden';
  214. }
  215. /**
  216. * @param {!shaka.extern.Cue} shakaCue
  217. * @return {TextTrackCue}
  218. * @private
  219. */
  220. static convertToTextTrackCue_(shakaCue) {
  221. if (shakaCue.startTime >= shakaCue.endTime) {
  222. // Edge will throw in this case.
  223. // See issue #501
  224. shaka.log.warning('Invalid cue times: ' + shakaCue.startTime +
  225. ' - ' + shakaCue.endTime);
  226. return null;
  227. }
  228. const Cue = shaka.text.Cue;
  229. /** @type {VTTCue} */
  230. const vttCue = new VTTCue(
  231. shakaCue.startTime,
  232. shakaCue.endTime,
  233. shakaCue.payload);
  234. // NOTE: positionAlign and lineAlign settings are not supported by Chrome
  235. // at the moment, so setting them will have no effect.
  236. // The bug on chromium to implement them:
  237. // https://bugs.chromium.org/p/chromium/issues/detail?id=633690
  238. vttCue.lineAlign = shakaCue.lineAlign;
  239. vttCue.positionAlign = shakaCue.positionAlign;
  240. if (shakaCue.size) {
  241. vttCue.size = shakaCue.size;
  242. }
  243. try {
  244. // Safari 10 seems to throw on align='center'.
  245. vttCue.align = shakaCue.textAlign;
  246. } catch (exception) {}
  247. if (shakaCue.textAlign == 'center' && vttCue.align != 'center') {
  248. // We want vttCue.position = 'auto'. By default, |position| is set to
  249. // "auto". If we set it to "auto" safari will throw an exception, so we
  250. // must rely on the default value.
  251. vttCue.align = 'middle';
  252. }
  253. if (shakaCue.writingMode ==
  254. Cue.writingMode.VERTICAL_LEFT_TO_RIGHT) {
  255. vttCue.vertical = 'lr';
  256. } else if (shakaCue.writingMode ==
  257. Cue.writingMode.VERTICAL_RIGHT_TO_LEFT) {
  258. vttCue.vertical = 'rl';
  259. }
  260. // snapToLines flag is true by default
  261. if (shakaCue.lineInterpretation == Cue.lineInterpretation.PERCENTAGE) {
  262. vttCue.snapToLines = false;
  263. }
  264. if (shakaCue.line != null) {
  265. vttCue.line = shakaCue.line;
  266. }
  267. if (shakaCue.position != null) {
  268. vttCue.position = shakaCue.position;
  269. }
  270. return vttCue;
  271. }
  272. /**
  273. * Iterate over all the cues in a text track and remove all those for which
  274. * |predicate(cue)| returns true.
  275. *
  276. * @param {!TextTrack} track
  277. * @param {function(!TextTrackCue):boolean} predicate
  278. * @private
  279. */
  280. static removeWhere_(track, predicate) {
  281. // Since |track.cues| can be null if |track.mode| is "disabled", force it to
  282. // something other than "disabled".
  283. //
  284. // If the track is already showing, then we should keep it as showing. But
  285. // if it something else, we will use hidden so that we don't "flash" cues on
  286. // the screen.
  287. const oldState = track.mode;
  288. const tempState = oldState == 'showing' ? 'showing' : 'hidden';
  289. track.mode = tempState;
  290. goog.asserts.assert(
  291. track.cues,
  292. 'Cues should be accessible when mode is set to "' + tempState + '".');
  293. // Create a copy of the list to avoid errors while iterating.
  294. for (const cue of Array.from(track.cues)) {
  295. if (cue && predicate(cue)) {
  296. track.removeCue(cue);
  297. }
  298. }
  299. track.mode = oldState;
  300. }
  301. };