Skip to content

Commit

Permalink
VTTCue polyfill (#643)
Browse files Browse the repository at this point in the history
This adds a VTTCue polyfill for IE/Edge (3-arg TextTrackCue) and
Toshiba dTV (6-arg TextTrackCue).

Closes #635
  • Loading branch information
jozefchutka authored and joeyparrish committed Jan 19, 2017
1 parent b2db1d1 commit 05d17ca
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 15 deletions.
1 change: 1 addition & 0 deletions build/types/polyfill
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
+../../lib/polyfill/patchedmediakeys_webkit.js
+../../lib/polyfill/promise.js
+../../lib/polyfill/videoplaybackquality.js
+../../lib/polyfill/vttcue.js
7 changes: 1 addition & 6 deletions lib/media/text_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,10 @@ shaka.media.TextEngine.makeCue = function(startTime, endTime, payload) {
return null;
}

return new shaka.media.TextEngine.CueConstructor(
startTime, endTime, payload);
return new VTTCue(startTime, endTime, payload);
};


/** @type {function(new:TextTrackCue, number, number, string)} */
shaka.media.TextEngine.CueConstructor = window.VTTCue || window.TextTrackCue;


/** @override */
shaka.media.TextEngine.prototype.destroy = function() {
if (this.track_) {
Expand Down
111 changes: 111 additions & 0 deletions lib/polyfill/vttcue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

goog.provide('shaka.polyfill.VTTCue');

goog.require('shaka.log');
goog.require('shaka.polyfill.register');


/**
* @namespace shaka.polyfill.VTTCue
*
* @summary A polyfill to provide VTTCue.
*/


/**
* Install the polyfill if needed.
*/
shaka.polyfill.VTTCue.install = function() {
if (window.VTTCue) {
shaka.log.info('Using native VTTCue.');
return;
}

if (!window.TextTrackCue) {
shaka.log.error('VTTCue not available.');
return;
}

var constructorLength = TextTrackCue.length;
if (constructorLength == 3) {
shaka.log.info('Using VTTCue polyfill from 3 argument TextTrackCue.');
window.VTTCue = shaka.polyfill.VTTCue.from3ArgsTextTrackCue_;
} else if (constructorLength == 6) {
shaka.log.info('Using VTTCue polyfill from 6 argument TextTrackCue.');
window.VTTCue = shaka.polyfill.VTTCue.from6ArgsTextTrackCue_;
} else if (shaka.polyfill.VTTCue.canUse3ArgsTextTrackCue_()) {
shaka.log.info('Using VTTCue polyfill from 3 argument TextTrackCue.');
window.VTTCue = shaka.polyfill.VTTCue.from3ArgsTextTrackCue_;
}
};


/**
* Draft spec TextTrackCue with 3 constructor arguments.
* See {@link https://goo.gl/ZXBWZi W3C Working Draft 25 October 2012}.
*
* @param {number} startTime
* @param {number} endTime
* @param {string} text
* @return {TextTrackCue}
* @private
*/
shaka.polyfill.VTTCue.from3ArgsTextTrackCue_ = function(startTime, endTime,
text) {
return new window.TextTrackCue(startTime, endTime, text);
};


/**
* Draft spec TextTrackCue with 6 constructor arguments (5th & 6th are
* optional).
* See {@link https://goo.gl/AYFqUh W3C Working Draft 29 March 2012}.
* Quoting the access to the TextTrackCue object to avoid the compiler
* complaining.
*
* @param {number} startTime
* @param {number} endTime
* @param {string} text
* @return {TextTrackCue}
* @private
*/
shaka.polyfill.VTTCue.from6ArgsTextTrackCue_ = function(startTime, endTime,
text) {
var id = startTime + '-' + endTime + '-' + text;
return new window['TextTrackCue'](id, startTime, endTime, text);
};


/**
* IE10, IE11 and Edge returns TextTrackCue.length = 0 although it accepts 3
* constructor arguments.
*
* @return {boolean}
* @private
*/
shaka.polyfill.VTTCue.canUse3ArgsTextTrackCue_ = function() {
try {
return !!shaka.polyfill.VTTCue.from3ArgsTextTrackCue_(1, 2, '');
} catch (error) {
return false;
}
};


shaka.polyfill.register(shaka.polyfill.VTTCue.install);
1 change: 1 addition & 0 deletions shaka-player.uncompiled.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ goog.require('shaka.polyfill.MediaKeys');
goog.require('shaka.polyfill.MediaSource');
goog.require('shaka.polyfill.Promise');
goog.require('shaka.polyfill.VideoPlaybackQuality');
goog.require('shaka.polyfill.VTTCue');
goog.require('shaka.polyfill.installAll');
goog.require('shaka.util.Error');
10 changes: 5 additions & 5 deletions test/media/ttml_text_parser_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
*/

describe('TtmlTextParser', function() {
var originalCueConstructor;
var originalVTTCue;

beforeAll(function() {
originalCueConstructor = shaka.media.TextEngine.CueConstructor;
originalVTTCue = window.VTTCue;
});

afterAll(function() {
shaka.media.TextEngine.CueConstructor = originalCueConstructor;
window.VTTCue = originalVTTCue;
});

beforeEach(function() {
shaka.media.TextEngine.CueConstructor = function(start, end, text) {
window.VTTCue = function(start, end, text) {
this.startTime = start;
this.endTime = end;
this.text = text;
Expand Down Expand Up @@ -427,7 +427,7 @@ describe('TtmlTextParser', function() {

it('uses a workaround for browsers not supporting align=center', function() {

shaka.media.TextEngine.CueConstructor = function(start, end, text) {
window.VTTCue = function(start, end, text) {
var align = 'middle';
Object.defineProperty(this, 'align', {
get: function() { return align; },
Expand Down
8 changes: 4 additions & 4 deletions test/media/vtt_text_parser_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@

describe('VttTextParser', function() {
var logWarningSpy;
var originalCueConstructor;
var originalVTTCue;

beforeAll(function() {
originalCueConstructor = shaka.media.TextEngine.CueConstructor;
originalVTTCue = window.VTTCue;

logWarningSpy = jasmine.createSpy('shaka.log.warning');
shaka.log.warning = logWarningSpy;
});

afterAll(function() {
shaka.media.TextEngine.CueConstructor = originalCueConstructor;
window.VTTCue = originalVTTCue;
});

beforeEach(function() {
logWarningSpy.calls.reset();
shaka.media.TextEngine.CueConstructor = function(start, end, text) {
window.VTTCue = function(start, end, text) {
this.startTime = start;
this.endTime = end;
this.text = text;
Expand Down

0 comments on commit 05d17ca

Please sign in to comment.