From 4662c65c834c99ade9476a95f96941c483bcfddb Mon Sep 17 00:00:00 2001 From: Garrett Date: Fri, 12 May 2017 12:12:15 -0700 Subject: [PATCH] feat: Get video playback quality (#16) --- package.json | 2 +- src/js/index.js | 23 ++++++++++++++ test/index.test.js | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index b6dc698..2564eeb 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/js/index.js b/src/js/index.js index 295085c..1480fad 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -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 diff --git a/test/index.test.js b/test/index.test.js index 9e0658e..0763335 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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'; @@ -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; +});