Source: lib/polyfill/vttcue.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.polyfill.VTTCue');
  18. goog.require('shaka.log');
  19. goog.require('shaka.polyfill.register');
  20. /**
  21. * @namespace shaka.polyfill.VTTCue
  22. *
  23. * @summary A polyfill to provide VTTCue.
  24. */
  25. /**
  26. * Install the polyfill if needed.
  27. */
  28. shaka.polyfill.VTTCue.install = function() {
  29. if (window.VTTCue) {
  30. shaka.log.info('Using native VTTCue.');
  31. return;
  32. }
  33. if (!window.TextTrackCue) {
  34. shaka.log.error('VTTCue not available.');
  35. return;
  36. }
  37. let constructorLength = TextTrackCue.length;
  38. if (constructorLength == 3) {
  39. shaka.log.info('Using VTTCue polyfill from 3 argument TextTrackCue.');
  40. window.VTTCue = shaka.polyfill.VTTCue.from3ArgsTextTrackCue_;
  41. } else if (constructorLength == 6) {
  42. shaka.log.info('Using VTTCue polyfill from 6 argument TextTrackCue.');
  43. window.VTTCue = shaka.polyfill.VTTCue.from6ArgsTextTrackCue_;
  44. } else if (shaka.polyfill.VTTCue.canUse3ArgsTextTrackCue_()) {
  45. shaka.log.info('Using VTTCue polyfill from 3 argument TextTrackCue.');
  46. window.VTTCue = shaka.polyfill.VTTCue.from3ArgsTextTrackCue_;
  47. }
  48. };
  49. /**
  50. * Draft spec TextTrackCue with 3 constructor arguments.
  51. * @see {@link https://goo.gl/ZXBWZi W3C Working Draft 25 October 2012}.
  52. *
  53. * @param {number} startTime
  54. * @param {number} endTime
  55. * @param {string} text
  56. * @return {TextTrackCue}
  57. * @private
  58. */
  59. shaka.polyfill.VTTCue.from3ArgsTextTrackCue_ = function(startTime, endTime,
  60. text) {
  61. return new window.TextTrackCue(startTime, endTime, text);
  62. };
  63. /**
  64. * Draft spec TextTrackCue with 6 constructor arguments (5th & 6th are
  65. * optional).
  66. * @see {@link https://goo.gl/AYFqUh W3C Working Draft 29 March 2012}.
  67. *
  68. * @param {number} startTime
  69. * @param {number} endTime
  70. * @param {string} text
  71. * @return {TextTrackCue}
  72. * @private
  73. */
  74. shaka.polyfill.VTTCue.from6ArgsTextTrackCue_ = function(startTime, endTime,
  75. text) {
  76. let id = startTime + '-' + endTime + '-' + text;
  77. // Quoting the access to the TextTrackCue object to satisfy the compiler.
  78. return new window['TextTrackCue'](id, startTime, endTime, text);
  79. };
  80. /**
  81. * IE10, IE11 and Edge return TextTrackCue.length = 0, although they accept 3
  82. * constructor arguments.
  83. *
  84. * @return {boolean}
  85. * @private
  86. */
  87. shaka.polyfill.VTTCue.canUse3ArgsTextTrackCue_ = function() {
  88. try {
  89. return !!shaka.polyfill.VTTCue.from3ArgsTextTrackCue_(1, 2, '');
  90. } catch (error) {
  91. return false;
  92. }
  93. };
  94. shaka.polyfill.register(shaka.polyfill.VTTCue.install);