-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add CDM detection module (#98)
- Loading branch information
1 parent
579df4c
commit 33dfe13
Showing
5 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import window from 'global/window'; | ||
|
||
const genericConfig = [{ | ||
initDataTypes: ['cenc'], | ||
audioCapabilities: [{ | ||
contentType: 'audio/mp4;codecs="mp4a.40.2"' | ||
}], | ||
videoCapabilities: [{ | ||
contentType: 'video/mp4;codecs="avc1.42E01E"' | ||
}] | ||
}]; | ||
|
||
const keySystems = [ | ||
// Fairplay | ||
// Requires different config than other CDMs | ||
{ | ||
keySystem: 'com.apple.fps', | ||
supportedConfig: [{ | ||
initDataTypes: ['sinf'], | ||
videoCapabilities: [{ | ||
contentType: 'video/mp4' | ||
}] | ||
}] | ||
}, | ||
// Playready | ||
{ | ||
keySystem: 'com.microsoft.playready.recommendation', | ||
supportedConfig: genericConfig | ||
}, | ||
// Widevine | ||
{ | ||
keySystem: 'com.widevine.alpha', | ||
supportedConfig: genericConfig | ||
}, | ||
// Clear | ||
{ | ||
keySystem: 'org.w3.clearkey', | ||
supportedConfig: genericConfig | ||
} | ||
]; | ||
|
||
// Asynchronously detect the list of supported CDMs by requesting key system access | ||
// when possible, otherwise rely on browser-specific EME API feature detection. | ||
export const detectSupportedCDMs = () => { | ||
const Promise = window.Promise; | ||
const results = { | ||
fairplay: Boolean(window.WebKitMediaKeys), | ||
playready: false, | ||
widevine: false, | ||
clearkey: false | ||
}; | ||
|
||
if (!window.MediaKeys || !window.navigator.requestMediaKeySystemAccess) { | ||
return Promise.resolve(results); | ||
} | ||
|
||
return Promise.all(keySystems.map(({keySystem, supportedConfig}) => { | ||
return window.navigator.requestMediaKeySystemAccess(keySystem, supportedConfig).catch(() => {}); | ||
})).then(([fairplay, playready, widevine, clearkey]) => { | ||
results.fairplay = Boolean(fairplay); | ||
results.playready = Boolean(playready); | ||
results.widevine = Boolean(widevine); | ||
results.clearkey = Boolean(clearkey); | ||
|
||
return results; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import QUnit from 'qunit'; | ||
import window from 'global/window'; | ||
import videojs from 'video.js'; | ||
import {detectSupportedCDMs } from '../src/cdm.js'; | ||
|
||
// `IS_CHROMIUM` and `IS_WINDOWS` are newer Video.js features, so add fallback just in case | ||
const IS_CHROMIUM = videojs.browser.IS_CHROMIUM || (/Chrome|CriOS/i).test(window.navigator.userAgent); | ||
const IS_WINDOWS = videojs.browser.IS_WINDOWS || (/Windows/i).test(window.navigator.userAgent); | ||
|
||
QUnit.module('videojs-contrib-eme CDM Module'); | ||
|
||
QUnit.test('detectSupportedCDMs() returns a Promise', function(assert) { | ||
const promise = detectSupportedCDMs(); | ||
|
||
assert.ok(promise.then); | ||
}); | ||
|
||
// NOTE: This test is not future-proof. It verifies that the CDM detect function | ||
// works as expected given browser's *current* CDM support. If that support changes, | ||
// this test may need updating. | ||
QUnit.test('detectSupportedCDMs() promise resolves correctly on different browsers', function(assert) { | ||
const done = assert.async(); | ||
const promise = detectSupportedCDMs(); | ||
|
||
promise.then((result) => { | ||
// Currently, widevine and clearkey don't work in headless Chrome, so we can't verify cdm support in | ||
// the remote Video.js test environment. However, it can be verified if testing locally in a real browser. | ||
// Headless Chrome bug: https://bugs.chromium.org/p/chromium/issues/detail?id=788662 | ||
if (videojs.browser.IS_CHROME) { | ||
assert.equal(result.fairplay, false, 'fairplay not supported in Chrome'); | ||
assert.equal(result.playready, false, 'playready not supported in Chrome'); | ||
|
||
// Uncomment if testing locally in actual browser | ||
// assert.equal(result.clearkey, true, 'clearkey is supported in Chrome'); | ||
// assert.equal(result.widevine, true, 'widevine is supported in Chrome'); | ||
} | ||
|
||
// Widevine requires a plugin in Ubuntu Firefox so it also does not work in the remote Video.js test environment | ||
if (videojs.browser.IS_FIREFOX) { | ||
assert.equal(result.fairplay, false, 'fairplay not supported in FF'); | ||
assert.equal(result.playready, false, 'playready not supported in FF'); | ||
assert.equal(result.clearkey, true, 'clearkey is supported in FF'); | ||
|
||
// Uncomment if testing locally in actual browser | ||
// assert.equal(result.widevine, true, 'widevine is supported in Chrome and FF'); | ||
} | ||
|
||
if (videojs.browser.IS_ANY_SAFARI) { | ||
assert.deepEqual(result, { | ||
fairplay: true, | ||
clearkey: true, | ||
playready: false, | ||
widevine: false | ||
}, 'fairplay support reported in Safari'); | ||
} | ||
|
||
if (videojs.browser.IS_EDGE && IS_CHROMIUM && !IS_WINDOWS) { | ||
assert.deepEqual(result, { | ||
fairplay: false, | ||
playready: false, | ||
widevine: true, | ||
clearkey: true | ||
}, 'widevine support reported in non-Windows Chromium Edge'); | ||
} | ||
|
||
if (videojs.browser.IS_EDGE && IS_CHROMIUM && IS_WINDOWS) { | ||
assert.deepEqual(result, { | ||
fairplay: false, | ||
playready: true, | ||
widevine: true, | ||
clearkey: true | ||
}, 'widevine and playready support reported in Windows Chromium Edge'); | ||
} | ||
|
||
done(); | ||
}).catch(done); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters