|
| 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 | + |
| 18 | +goog.provide('shaka.polyfill.VTTCue'); |
| 19 | + |
| 20 | +goog.require('shaka.log'); |
| 21 | +goog.require('shaka.polyfill.register'); |
| 22 | + |
| 23 | + |
| 24 | +/** |
| 25 | + * @namespace shaka.polyfill.VTTCue |
| 26 | + * |
| 27 | + * @summary A polyfill to provide VTTCue. |
| 28 | + */ |
| 29 | + |
| 30 | + |
| 31 | +/** |
| 32 | + * Install the polyfill if needed. |
| 33 | + */ |
| 34 | +shaka.polyfill.VTTCue.install = function() { |
| 35 | + if (window.VTTCue) { |
| 36 | + shaka.log.info('Using native VTTCue.'); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + if (!window.TextTrackCue) { |
| 41 | + shaka.log.error('VTTCue not available.'); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + var constructorLength = TextTrackCue.length; |
| 46 | + if (constructorLength == 3) { |
| 47 | + shaka.log.info('Using VTTCue polyfill from 3 argument TextTrackCue.'); |
| 48 | + window.VTTCue = shaka.polyfill.VTTCue.from3ArgsTextTrackCue_; |
| 49 | + } else if (constructorLength == 6) { |
| 50 | + shaka.log.info('Using VTTCue polyfill from 6 argument TextTrackCue.'); |
| 51 | + window.VTTCue = shaka.polyfill.VTTCue.from6ArgsTextTrackCue_; |
| 52 | + } else if (shaka.polyfill.VTTCue.canUse3ArgsTextTrackCue_()) { |
| 53 | + shaka.log.info('Using VTTCue polyfill from 3 argument TextTrackCue.'); |
| 54 | + window.VTTCue = shaka.polyfill.VTTCue.from3ArgsTextTrackCue_; |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | + |
| 59 | +/** |
| 60 | + * Draft spec TextTrackCue with 3 constructor arguments. |
| 61 | + * See {@link https://goo.gl/ZXBWZi W3C Working Draft 25 October 2012}. |
| 62 | + * |
| 63 | + * @param {number} startTime |
| 64 | + * @param {number} endTime |
| 65 | + * @param {string} text |
| 66 | + * @return {TextTrackCue} |
| 67 | + * @private |
| 68 | + */ |
| 69 | +shaka.polyfill.VTTCue.from3ArgsTextTrackCue_ = function(startTime, endTime, |
| 70 | + text) { |
| 71 | + return new window.TextTrackCue(startTime, endTime, text); |
| 72 | +}; |
| 73 | + |
| 74 | + |
| 75 | +/** |
| 76 | + * Draft spec TextTrackCue with 6 constructor arguments (5th & 6th are |
| 77 | + * optional). |
| 78 | + * See {@link https://goo.gl/AYFqUh W3C Working Draft 29 March 2012}. |
| 79 | + * Quoting the access to the TextTrackCue object to avoid the compiler |
| 80 | + * complaining. |
| 81 | + * |
| 82 | + * @param {number} startTime |
| 83 | + * @param {number} endTime |
| 84 | + * @param {string} text |
| 85 | + * @return {TextTrackCue} |
| 86 | + * @private |
| 87 | + */ |
| 88 | +shaka.polyfill.VTTCue.from6ArgsTextTrackCue_ = function(startTime, endTime, |
| 89 | + text) { |
| 90 | + var id = startTime + '-' + endTime + '-' + text; |
| 91 | + return new window['TextTrackCue'](id, startTime, endTime, text); |
| 92 | +}; |
| 93 | + |
| 94 | + |
| 95 | +/** |
| 96 | + * IE10, IE11 and Edge returns TextTrackCue.length = 0 although it accepts 3 |
| 97 | + * constructor arguments. |
| 98 | + * |
| 99 | + * @return {boolean} |
| 100 | + * @private |
| 101 | + */ |
| 102 | +shaka.polyfill.VTTCue.canUse3ArgsTextTrackCue_ = function() { |
| 103 | + try { |
| 104 | + return !!shaka.polyfill.VTTCue.from3ArgsTextTrackCue_(1, 2, ''); |
| 105 | + } catch (error) { |
| 106 | + return false; |
| 107 | + } |
| 108 | +}; |
| 109 | + |
| 110 | + |
| 111 | +shaka.polyfill.register(shaka.polyfill.VTTCue.install); |
0 commit comments