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. var 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. * Quoting the access to the TextTrackCue object to avoid the compiler
  68. * complaining.
  69. *
  70. * @param {number} startTime
  71. * @param {number} endTime
  72. * @param {string} text
  73. * @return {TextTrackCue}
  74. * @private
  75. */
  76. shaka.polyfill.VTTCue.from6ArgsTextTrackCue_ = function(startTime, endTime,
  77. text) {
  78. var id = startTime + '-' + endTime + '-' + text;
  79. return new window['TextTrackCue'](id, startTime, endTime, text);
  80. };
  81. /**
  82. * IE10, IE11 and Edge returns TextTrackCue.length = 0 although it accepts 3
  83. * constructor arguments.
  84. *
  85. * @return {boolean}
  86. * @private
  87. */
  88. shaka.polyfill.VTTCue.canUse3ArgsTextTrackCue_ = function() {
  89. try {
  90. return !!shaka.polyfill.VTTCue.from3ArgsTextTrackCue_(1, 2, '');
  91. } catch (error) {
  92. return false;
  93. }
  94. };
  95. shaka.polyfill.register(shaka.polyfill.VTTCue.install);