Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: text track display overlays a video #8009

Merged
merged 2 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions src/js/tracks/text-track-display.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ function tryUpdateStyle(el, style, rule) {
}
}

/**
* Converts the CSS top/right/bottom/left property numeric value to string in pixels.
*
* @param {number} position
* The CSS top/right/bottom/left property value.
*
* @return {string}
* The CSS property value that was created, like '10px'.
*
* @private
*/
function getCSSPositionValue(position) {
return position ? `${position}px` : '';
}

/**
* The component for displaying text track cues.
*
Expand All @@ -99,11 +114,18 @@ class TextTrackDisplay extends Component {
constructor(player, options, ready) {
super(player, options, ready);

const updateDisplayHandler = (e) => this.updateDisplay(e);
const updateDisplayTextHandler = (e) => this.updateDisplay(e);
const updateDisplayHandler = (e) => {
this.updateDisplayOverlay();
this.updateDisplay(e);
};

player.on('loadstart', (e) => this.toggleDisplay(e));
player.on('texttrackchange', updateDisplayHandler);
player.on('loadedmetadata', (e) => this.preselectTrack(e));
player.on('texttrackchange', updateDisplayTextHandler);
player.on('loadedmetadata', (e) => {
this.updateDisplayOverlay();
this.preselectTrack(e);
});

// This used to be called during player init, but was causing an error
// if a track should show by default and the display hadn't loaded yet.
Expand Down Expand Up @@ -294,6 +316,36 @@ class TextTrackDisplay extends Component {
}
}

/**
* Updates the displayed TextTrack to be sure it overlays the video when a either
* a {@link Player#texttrackchange} or a {@link Player#fullscreenchange} is fired.
*/
updateDisplayOverlay() {
if (!this.player_.videoHeight()) {
return;
}

const playerWidth = this.player_.currentWidth();
const playerHeight = this.player_.currentHeight();
const playerAspectRatio = playerWidth / playerHeight;
const videoAspectRatio = this.player_.videoWidth() / this.player_.videoHeight();
let leftRight = 0;
let topBottom = 0;

if (Math.abs(playerAspectRatio - videoAspectRatio) > 0.01) {
gjanblaszczyk marked this conversation as resolved.
Show resolved Hide resolved
if (playerAspectRatio > videoAspectRatio) {
leftRight = Math.round((playerWidth - playerHeight * videoAspectRatio) / 2);
} else {
topBottom = Math.round((playerHeight - playerWidth / videoAspectRatio) / 2);
}
}

tryUpdateStyle(this.el_, 'top', getCSSPositionValue(topBottom));
tryUpdateStyle(this.el_, 'bottom', getCSSPositionValue(topBottom));
gjanblaszczyk marked this conversation as resolved.
Show resolved Hide resolved
tryUpdateStyle(this.el_, 'left', getCSSPositionValue(leftRight));
tryUpdateStyle(this.el_, 'right', getCSSPositionValue(leftRight));
gjanblaszczyk marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Style {@Link TextTrack} activeCues according to {@Link TextTrackSettings}.
*
Expand Down
59 changes: 59 additions & 0 deletions test/unit/tracks/text-track-display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,4 +410,63 @@ if (!Html5.supportsNativeTextTracks()) {
'colors must be valid hex codes.'
);
});

QUnit.test('text track display should overlay a video', function(assert) {
const tag = document.createElement('video');

tag.width = 320;
tag.height = 180;
const player = TestHelpers.makePlayer({}, tag);
const textTrackDisplay = player.getChild('TextTrackDisplay');
const textTrackDisplayStyle = textTrackDisplay.el().style;

assert.ok(textTrackDisplayStyle.top === '', 'text track display style top defaults to empty string');
assert.ok(textTrackDisplayStyle.bottom === '', 'text track display style bottom defaults to empty string');
assert.ok(textTrackDisplayStyle.left === '', 'text track display style left defaults to empty string');
assert.ok(textTrackDisplayStyle.right === '', 'text track display style right defaults to empty string');

// video aspect ratio equal to NaN
player.tech_.videoWidth = () => 0;
player.tech_.videoHeight = () => 0;

assert.ok(textTrackDisplayStyle.top === '', 'text track display style top defaults to empty string');
assert.ok(textTrackDisplayStyle.bottom === '', 'text track display style bottom defaults to empty string');
assert.ok(textTrackDisplayStyle.left === '', 'text track display style left defaults to empty string');
assert.ok(textTrackDisplayStyle.right === '', 'text track display style right defaults to empty string');

// video aspect ratio 2:1
player.tech_.videoWidth = () => 100;
player.tech_.videoHeight = () => 50;

textTrackDisplay.updateDisplayOverlay();

assert.ok(textTrackDisplayStyle.top === '10px', 'text track display style top equal to10px');
assert.ok(textTrackDisplayStyle.bottom === '10px', 'text track display style bottom equal to 10px');
assert.ok(textTrackDisplayStyle.left === '', 'text track display style left defaults to empty string');
assert.ok(textTrackDisplayStyle.right === '', 'text track display style right defaults to empty string');

// video aspect ratio 4:3
player.tech_.videoWidth = () => 100;
player.tech_.videoHeight = () => 75;

textTrackDisplay.updateDisplayOverlay();

assert.ok(textTrackDisplayStyle.top === '', 'text track display style top defaults to empty string');
assert.ok(textTrackDisplayStyle.bottom === '', 'text track display style bottom defaults to empty string');
assert.ok(textTrackDisplayStyle.left === '40px', 'text track display style left equal to 40px');
assert.ok(textTrackDisplayStyle.right === '40px', 'text track display style right equal to 40px');

// video aspect ratio 16:9
player.tech_.videoWidth = () => 320;
player.tech_.videoHeight = () => 180;

textTrackDisplay.updateDisplayOverlay();

assert.ok(textTrackDisplayStyle.top === '', 'text track display style top defaults to empty string');
assert.ok(textTrackDisplayStyle.bottom === '', 'text track display style bottom defaults to empty string');
assert.ok(textTrackDisplayStyle.left === '', 'text track display style left defaults to empty string');
assert.ok(textTrackDisplayStyle.right === '', 'text track display style right defaults to empty string');

player.dispose();
});
}