Skip to content

Commit

Permalink
Remove VTTRegion support.
Browse files Browse the repository at this point in the history
Browser support is currently buggy and limited, so until they fix it,
this removes VTTRegion support.

Closes #1584

Backported to v2.4.x

Change-Id: Ia5bb34dd3ea94737adcdd1d441201bb97b303f81
  • Loading branch information
TheModMaker authored and joeyparrish committed Oct 8, 2018
1 parent a78134c commit c681f3a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 182 deletions.
88 changes: 4 additions & 84 deletions lib/text/simple_text_displayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ goog.provide('shaka.text.SimpleTextDisplayer');
goog.require('goog.asserts');
goog.require('shaka.log');
goog.require('shaka.text.Cue');
goog.require('shaka.text.CueRegion');
goog.require('shaka.util.Functional');



/**
Expand All @@ -45,9 +42,6 @@ shaka.text.SimpleTextDisplayer = function(video) {
/** @private {TextTrack} */
this.textTrack_ = null;

/** @private {HTMLMediaElement} */
this.video_ = video;

// TODO: Test that in all cases, the built-in CC controls in the video element
// are toggling our TextTrack.

Expand Down Expand Up @@ -100,22 +94,13 @@ shaka.text.SimpleTextDisplayer.prototype.remove = function(start, end) {
* @export
*/
shaka.text.SimpleTextDisplayer.prototype.append = function(cues) {
// Convert regions.
let vttRegions = [];
if (window.VTTRegion) {
let regions = cues.map((cue) => cue.region);
regions = regions.filter(shaka.util.Functional.isNotDuplicate);

for (let i = 0; i < regions.length; i++) {
let region = this.convertToVttRegion_(regions[i]);
vttRegions.push(region);
}
}
const convertToTextTrackCue =
shaka.text.SimpleTextDisplayer.convertToTextTrackCue_;

// Convert cues.
let textTrackCues = [];
for (let i = 0; i < cues.length; i++) {
let cue = this.convertToTextTrackCue_(cues[i], vttRegions);
let cue = convertToTextTrackCue(cues[i]);
if (cue) {
textTrackCues.push(cue);
}
Expand Down Expand Up @@ -155,7 +140,6 @@ shaka.text.SimpleTextDisplayer.prototype.destroy = function() {
}

this.textTrack_ = null;
this.video_ = null;
return Promise.resolve();
};

Expand All @@ -180,12 +164,10 @@ shaka.text.SimpleTextDisplayer.prototype.setTextVisibility = function(on) {

/**
* @param {!shakaExtern.Cue} shakaCue
* @param {!Array.<!VTTRegion>} regions
* @return {TextTrackCue}
* @private
*/
shaka.text.SimpleTextDisplayer.prototype.convertToTextTrackCue_ =
function(shakaCue, regions) {
shaka.text.SimpleTextDisplayer.convertToTextTrackCue_ = function(shakaCue) {
if (shakaCue.startTime >= shakaCue.endTime) {
// IE/Edge will throw in this case.
// See issue #501
Expand Down Expand Up @@ -241,72 +223,10 @@ shaka.text.SimpleTextDisplayer.prototype.convertToTextTrackCue_ =
vttCue.position = shakaCue.position;
}

if (shakaCue.region.id.length) {
let regionsWithId =
regions.filter((reg) => reg.id == shakaCue.region.id);

if (regionsWithId.length) {
vttCue.region = regionsWithId[0];
}
}

return vttCue;
};


/**
* @param {!shakaExtern.CueRegion} shakaRegion
* @return {VTTRegion}
* @private
*/
shaka.text.SimpleTextDisplayer.prototype.convertToVttRegion_ =
function(shakaRegion) {
goog.asserts.assert(window.VTTRegion != null,
'VTTRegions should be supported!');

let region = new VTTRegion();
const CueRegion = shaka.text.CueRegion;
let videoWidth = this.video_.offsetWidth;
let videoHeight = this.video_.offsetHeight;

region.id = shakaRegion.id;
region.regionAnchorX = shakaRegion.regionAnchorX;
region.regionAnchorY = shakaRegion.regionAnchorY;
region.scroll = shakaRegion.scroll;
if (shakaRegion.heightUnits == CueRegion.units.LINES) {
// VTTRegion only supports height in lines via the 'lines' property.
region.lines = shakaRegion.height;
}

goog.asserts.assert(shakaRegion.widthUnits != CueRegion.units.LINES,
'Width should be set either in percentage or pixels!');

if (shakaRegion.widthUnits == CueRegion.units.PX) {
// VTTRegion expects the values to be given in percentage of the video
// height and width.
region.width = shakaRegion.width * 100 / videoWidth;
} else {
region.width = shakaRegion.width;
}

goog.asserts.assert(shakaRegion.viewportAnchorUnits != CueRegion.units.LINES,
'Anchors should be set either in percentage or pixels!');
if (shakaRegion.viewportAnchorUnits == CueRegion.units.PX) {
// VTTRegion expects the values to be given in percentage of the video
// height and width.
region.viewportAnchorX =
shakaRegion.viewportAnchorX * 100 / videoWidth;
region.viewportAnchorY =
shakaRegion.viewportAnchorY * 100 / videoHeight;
} else {
region.viewportAnchorX = shakaRegion.viewportAnchorX;
region.viewportAnchorY = shakaRegion.viewportAnchorY;
}

return region;
};


/**
* Iterate over all the cues in a text track and remove all those for which
* |predicate(cue)| returns true.
Expand Down
98 changes: 0 additions & 98 deletions test/text/simple_text_displayer_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

describe('SimpleTextDisplayer', function() {
const originalVTTCue = window.VTTCue;
const originalVTTRegion = window.VTTRegion;
const Cue = shaka.text.Cue;
const CueRegion = shaka.text.CueRegion;
const SimpleTextDisplayer = shaka.text.SimpleTextDisplayer;

/** @type {!shaka.test.FakeVideo} */
Expand Down Expand Up @@ -50,17 +48,10 @@ describe('SimpleTextDisplayer', function() {
this.text = text;
}
window.VTTCue = /** @type {?} */(FakeVTTCue);

/**
* @constructor
*/
function FakeVTTRegion() {}
window.VTTRegion = /** @type {?} */(FakeVTTRegion);
});

afterAll(function() {
window.VTTCue = originalVTTCue;
window.VTTRegion = originalVTTRegion;
});

describe('append', function() {
Expand Down Expand Up @@ -287,65 +278,6 @@ describe('SimpleTextDisplayer', function() {
});
});

describe('ConvertToVttRegion', function() {
it('converts shaka.text.CueRegions to VTTRegions', function() {
let region1 = new shaka.text.CueRegion();
region1.id = 'reg1';
region1.width = 50;
region1.scroll = CueRegion.scrollMode.UP;
region1.height = 3;
region1.heightUnits = CueRegion.units.LINES;

let cue1 = new shaka.text.Cue(20, 40, 'Test');
cue1.region = region1;

verifyHelper(
[
{
start: 20,
end: 40,
text: 'Test',
region: {
viewportAnchorX: 0,
viewportAnchorY: 0,
regionAnchorX: 0,
regionAnchorY: 0,
width: 50,
lines: 3,
scroll: 'up'
}
}
], [cue1]);
});

it('converts pixel values to percentage', function() {
let region1 = new shaka.text.CueRegion();
region1.id = 'reg1';
region1.width = 500;
region1.widthUnits = CueRegion.units.PX;
region1.viewportAnchorX = 100;
region1.viewportAnchorY = 100;
region1.viewportAnchorUnits = CueRegion.units.PX;

let cue1 = new shaka.text.Cue(20, 40, 'Test');
cue1.region = region1;

verifyHelper(
[
{
start: 20,
end: 40,
text: 'Test',
region: {
viewportAnchorX: 10,
viewportAnchorY: 10,
width: 50
}
}
], [cue1]);
});
});

function createFakeCue(startTime, endTime) {
return {startTime: startTime, endTime: endTime};
}
Expand Down Expand Up @@ -385,36 +317,6 @@ describe('SimpleTextDisplayer', function() {
if ('position' in vttCues[i]) {
expect(result[i].position).toBe(vttCues[i].position);
}
if ('region' in vttCues[i]) {
verifyRegion(result[i].region, vttCues[i].region);
}
}
}

function verifyRegion(actual, expected) {
if ('id' in expected) {
expect(actual.id).toBe(expected.id);
}
if ('width' in expected) {
expect(actual.width).toBe(expected.width);
}
if ('lines' in expected) {
expect(actual.lines).toBe(expected.lines);
}
if ('regionAnchorX' in expected) {
expect(actual.regionAnchorX).toBe(expected.regionAnchorX);
}
if ('regionAnchorY' in expected) {
expect(actual.regionAnchorY).toBe(expected.regionAnchorY);
}
if ('viewportAnchorX' in expected) {
expect(actual.viewportAnchorX).toBe(expected.viewportAnchorX);
}
if ('viewportAnchorY' in expected) {
expect(actual.viewportAnchorY).toBe(expected.viewportAnchorY);
}
if ('scroll' in expected) {
expect(actual.scroll).toBe(expected.scroll);
}
}
});

0 comments on commit c681f3a

Please sign in to comment.