Source: lib/polyfill/vttcue.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.polyfill.VTTCue');
  7. goog.require('shaka.log');
  8. goog.require('shaka.polyfill');
  9. /**
  10. * @summary A polyfill to provide VTTCue.
  11. * @export
  12. */
  13. shaka.polyfill.VTTCue = class {
  14. /**
  15. * Install the polyfill if needed.
  16. * @export
  17. */
  18. static install() {
  19. if (window.VTTCue) {
  20. shaka.log.info('Using native VTTCue.');
  21. return;
  22. }
  23. if (!window.TextTrackCue) {
  24. shaka.log.error('VTTCue not available.');
  25. return;
  26. }
  27. /** @type {?function(number, number, string):!TextTrackCue} */
  28. let replacement = null;
  29. const constructorLength = TextTrackCue.length;
  30. if (constructorLength == 3) {
  31. shaka.log.info('Using VTTCue polyfill from 3 argument TextTrackCue.');
  32. replacement = shaka.polyfill.VTTCue.from3ArgsTextTrackCue_;
  33. } else if (constructorLength == 6) {
  34. shaka.log.info('Using VTTCue polyfill from 6 argument TextTrackCue.');
  35. replacement = shaka.polyfill.VTTCue.from6ArgsTextTrackCue_;
  36. } else if (shaka.polyfill.VTTCue.canUse3ArgsTextTrackCue_()) {
  37. shaka.log.info('Using VTTCue polyfill from 3 argument TextTrackCue.');
  38. replacement = shaka.polyfill.VTTCue.from3ArgsTextTrackCue_;
  39. }
  40. if (!replacement) {
  41. shaka.log.error('No recognized signature for TextTrackCue found!');
  42. return;
  43. }
  44. // The polyfilled VTTCue must be callable with "new", but the static methods
  45. // in this class cannot be called that way on legacy Edge. So we must wrap
  46. // the replacement in a plain function.
  47. // eslint-disable-next-line no-restricted-syntax
  48. window['VTTCue'] = function(start, end, text) {
  49. return replacement(start, end, text);
  50. };
  51. }
  52. /**
  53. * Draft spec TextTrackCue with 3 constructor arguments.
  54. * @see {@link https://bit.ly/2IdyKbA W3C Working Draft 25 October 2012}.
  55. *
  56. * @param {number} startTime
  57. * @param {number} endTime
  58. * @param {string} text
  59. * @return {!TextTrackCue}
  60. * @private
  61. */
  62. static from3ArgsTextTrackCue_(startTime, endTime, text) {
  63. return new window.TextTrackCue(startTime, endTime, text);
  64. }
  65. /**
  66. * Draft spec TextTrackCue with 6 constructor arguments (5th & 6th are
  67. * optional).
  68. * @see {@link https://bit.ly/2KaGSP2 W3C Working Draft 29 March 2012}.
  69. *
  70. * @param {number} startTime
  71. * @param {number} endTime
  72. * @param {string} text
  73. * @return {!TextTrackCue}
  74. * @private
  75. */
  76. static from6ArgsTextTrackCue_(startTime, endTime, text) {
  77. const id = startTime + '-' + endTime + '-' + text;
  78. // Quoting the access to the TextTrackCue object to satisfy the compiler.
  79. return new window['TextTrackCue'](id, startTime, endTime, text);
  80. }
  81. /**
  82. * Edge returns TextTrackCue.length = 0, although it accepts 3
  83. * constructor arguments.
  84. *
  85. * @return {boolean}
  86. * @private
  87. */
  88. static canUse3ArgsTextTrackCue_() {
  89. try {
  90. return !!shaka.polyfill.VTTCue.from3ArgsTextTrackCue_(1, 2, '');
  91. } catch (error) {
  92. return false;
  93. }
  94. }
  95. };
  96. shaka.polyfill.register(shaka.polyfill.VTTCue.install);