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. */
  9. goog.provide('shaka.text.SimpleTextDisplayer');
  10. goog.require('goog.asserts');
  11. goog.require('shaka.text.Utils');
  12. /**
  13. * A text displayer plugin using the browser's native VTTCue interface.
  14. *
  15. * @implements {shaka.extern.TextDisplayer}
  16. * @export
  17. */
  18. shaka.text.SimpleTextDisplayer = class {
  19. /**
  20. * @param {HTMLMediaElement} video
  21. * @param {string} label
  22. */
  23. constructor(video, label) {
  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. if (track.kind === 'metadata' || track.kind === 'chapters') {
  32. continue;
  33. }
  34. // NOTE: There is no API available to remove a TextTrack from a video
  35. // element.
  36. track.mode = 'disabled';
  37. if (track.label == label) {
  38. this.textTrack_ = track;
  39. }
  40. }
  41. if (!this.textTrack_) {
  42. // As far as I can tell, there is no observable difference between setting
  43. // kind to 'subtitles' or 'captions' when creating the TextTrack object.
  44. // The individual text tracks from the manifest will still have their own
  45. // kinds which can be displayed in the app's UI.
  46. this.textTrack_ = video.addTextTrack('subtitles', label);
  47. }
  48. this.textTrack_.mode = 'hidden';
  49. }
  50. /**
  51. * @override
  52. * @export
  53. */
  54. configure(config) {
  55. // Unused.
  56. }
  57. /**
  58. * @override
  59. * @export
  60. */
  61. remove(start, end) {
  62. // Check that the displayer hasn't been destroyed.
  63. if (!this.textTrack_) {
  64. return false;
  65. }
  66. const removeInRange = (cue) => {
  67. const inside = cue.startTime < end && cue.endTime > start;
  68. return inside;
  69. };
  70. shaka.text.SimpleTextDisplayer.removeWhere_(this.textTrack_, removeInRange);
  71. return true;
  72. }
  73. /**
  74. * @override
  75. * @export
  76. */
  77. append(cues) {
  78. if (!this.textTrack_) {
  79. return;
  80. }
  81. const flattenedCues = shaka.text.Utils.getCuesToFlatten(cues);
  82. // Convert cues.
  83. const textTrackCues = [];
  84. const cuesInTextTrack = this.textTrack_.cues ?
  85. Array.from(this.textTrack_.cues) : [];
  86. for (const inCue of flattenedCues) {
  87. // When a VTT cue spans a segment boundary, the cue will be duplicated
  88. // into two segments.
  89. // To avoid displaying duplicate cues, if the current textTrack cues
  90. // list already contains the cue, skip it.
  91. const containsCue = cuesInTextTrack.some((cueInTextTrack) => {
  92. if (cueInTextTrack.startTime == inCue.startTime &&
  93. cueInTextTrack.endTime == inCue.endTime &&
  94. cueInTextTrack.text == inCue.payload) {
  95. return true;
  96. }
  97. return false;
  98. });
  99. if (!containsCue && inCue.payload) {
  100. const cue =
  101. shaka.text.Utils.mapShakaCueToNativeCue(inCue);
  102. if (cue) {
  103. textTrackCues.push(cue);
  104. }
  105. }
  106. }
  107. // Sort the cues based on start/end times. Make a copy of the array so
  108. // we can get the index in the original ordering. Out of order cues are
  109. // rejected by Edge. See https://bit.ly/2K9VX3s
  110. const sortedCues = textTrackCues.slice().sort((a, b) => {
  111. if (a.startTime != b.startTime) {
  112. return a.startTime - b.startTime;
  113. } else if (a.endTime != b.endTime) {
  114. return a.endTime - b.startTime;
  115. } else {
  116. // The browser will display cues with identical time ranges from the
  117. // bottom up. Reversing the order of equal cues means the first one
  118. // parsed will be at the top, as you would expect.
  119. // See https://github.com/shaka-project/shaka-player/issues/848 for
  120. // more info.
  121. // However, this ordering behavior is part of VTTCue's "line" field.
  122. // Some platforms don't have a real VTTCue and use a polyfill instead.
  123. // When VTTCue is polyfilled or does not support "line", we should _not_
  124. // reverse the order. This occurs on legacy Edge.
  125. // eslint-disable-next-line no-restricted-syntax
  126. if ('line' in VTTCue.prototype) {
  127. // Native VTTCue
  128. return textTrackCues.indexOf(b) - textTrackCues.indexOf(a);
  129. } else {
  130. // Polyfilled VTTCue
  131. return textTrackCues.indexOf(a) - textTrackCues.indexOf(b);
  132. }
  133. }
  134. });
  135. for (const cue of sortedCues) {
  136. this.textTrack_.addCue(cue);
  137. }
  138. }
  139. /**
  140. * @override
  141. * @export
  142. */
  143. destroy() {
  144. if (this.textTrack_) {
  145. const removeIt = (cue) => true;
  146. shaka.text.SimpleTextDisplayer.removeWhere_(this.textTrack_, removeIt);
  147. // NOTE: There is no API available to remove a TextTrack from a video
  148. // element.
  149. this.textTrack_.mode = 'disabled';
  150. }
  151. this.textTrack_ = null;
  152. return Promise.resolve();
  153. }
  154. /**
  155. * @override
  156. * @export
  157. */
  158. isTextVisible() {
  159. if (!this.textTrack_) {
  160. return false;
  161. }
  162. return this.textTrack_.mode == 'showing';
  163. }
  164. /**
  165. * @override
  166. * @export
  167. */
  168. setTextVisibility(on) {
  169. if (this.textTrack_) {
  170. this.textTrack_.mode = on ? 'showing' : 'hidden';
  171. }
  172. }
  173. /**
  174. * Iterate over all the cues in a text track and remove all those for which
  175. * |predicate(cue)| returns true.
  176. *
  177. * @param {!TextTrack} track
  178. * @param {function(!TextTrackCue):boolean} predicate
  179. * @private
  180. */
  181. static removeWhere_(track, predicate) {
  182. // Since |track.cues| can be null if |track.mode| is "disabled", force it to
  183. // something other than "disabled".
  184. //
  185. // If the track is already showing, then we should keep it as showing. But
  186. // if it something else, we will use hidden so that we don't "flash" cues on
  187. // the screen.
  188. const oldState = track.mode;
  189. const tempState = oldState == 'showing' ? 'showing' : 'hidden';
  190. track.mode = tempState;
  191. goog.asserts.assert(
  192. track.cues,
  193. 'Cues should be accessible when mode is set to "' + tempState + '".');
  194. // Create a copy of the list to avoid errors while iterating.
  195. for (const cue of Array.from(track.cues)) {
  196. if (cue && predicate(cue)) {
  197. track.removeCue(cue);
  198. }
  199. }
  200. track.mode = oldState;
  201. }
  202. };