Skip to content
This repository has been archived by the owner on Dec 10, 2020. It is now read-only.

Commit

Permalink
feat: Get video playback quality (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
gesinger authored and gkatsev committed May 12, 2017
1 parent 1d4570b commit 4662c65
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"dependencies": {
"global": "^4.3.2",
"video.js": "^6.0.1",
"videojs-swf": "^5.2.0"
"videojs-swf": "^5.4.0"
},
"devDependencies": {
"husky": "^0.13.1",
Expand Down
23 changes: 23 additions & 0 deletions src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,29 @@ class Flash extends Tech {
return false;
}

/**
* Gets available media playback quality metrics as specified by the W3C's Media
* Playback Quality API.
*
* @see [Spec]{@link https://wicg.github.io/media-playback-quality}
*
* @return {Object}
* An object with supported media playback quality metrics
*/
getVideoPlaybackQuality() {
const videoPlaybackQuality = this.el_.vjs_getProperty('getVideoPlaybackQuality');

if (window.performance && typeof window.performance.now === 'function') {
videoPlaybackQuality.creationTime = window.performance.now();
} else if (window.performance &&
window.performance.timing &&
typeof window.performance.timing.navigationStart === 'number') {
videoPlaybackQuality.creationTime =
window.Date.now() - window.performance.timing.navigationStart;
}

return videoPlaybackQuality;
}
}

// Create setters and getters for attributes
Expand Down
76 changes: 76 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Flash from '../src/js';
import {createTimeRange} from 'video.js';
import {document} from 'global';
import {window} from 'global/window';
import sinon from 'sinon';
import QUnit from 'qunitjs';

Expand Down Expand Up @@ -261,3 +262,78 @@ QUnit.test('duration returns NaN, Infinity or duration according to the HTML sta
'duration returns duration property when readyState' +
' and duration property are both higher than 0');
});

QUnit.test('getVideoPlaybackQuality API exists', function(assert) {
const propertyCalls = [];
const videoPlaybackQuality = { test: 'test' };
const mockFlash = {
el_: {
/* eslint-disable camelcase */
vjs_getProperty(attr) {
propertyCalls.push(attr);
return videoPlaybackQuality;
}
/* eslint-enable camelcase */
}
};

assert.deepEqual(Flash.prototype.getVideoPlaybackQuality.call(mockFlash),
videoPlaybackQuality,
'called to get property from flash');
assert.equal(propertyCalls.length, 1, 'only one property call');
assert.equal(propertyCalls[0],
'getVideoPlaybackQuality',
'called for getVideoPlaybackQuality');
});

QUnit.test('getVideoPlaybackQuality uses best available creationTime', function(assert) {
const origPerformance = window.performance;
const origDate = window.Date;
const videoPlaybackQuality = {};
const mockFlash = {
el_: {
/* eslint-disable camelcase */
vjs_getProperty(attr) {
return videoPlaybackQuality;
}
/* eslint-enable camelcase */
}
};

window.performance = void 0;
assert.notOk(Flash.prototype.getVideoPlaybackQuality.call(mockFlash).creationTime,
'no creationTime when no performance API available');

window.performance = {
timing: {}
};
assert.notOk(Flash.prototype.getVideoPlaybackQuality.call(mockFlash).creationTime,
'no creationTime when performance API insufficient');

window.performance = {
now: () => 4
};
assert.equal(Flash.prototype.getVideoPlaybackQuality.call(mockFlash).creationTime,
4,
'creationTime is performance.now when available');

window.Date = {
now: () => 10
};
window.performance = {
timing: {
navigationStart: 3
}
};
assert.equal(Flash.prototype.getVideoPlaybackQuality.call(mockFlash).creationTime,
7,
'creationTime uses Date.now() - navigationStart when available');

window.performance.now = () => 4;
assert.equal(Flash.prototype.getVideoPlaybackQuality.call(mockFlash).creationTime,
4,
'creationTime prioritizes performance.now when available');

window.Date = origDate;
window.performance = origPerformance;
});

0 comments on commit 4662c65

Please sign in to comment.