Skip to content

Commit

Permalink
revert: fix: use in-spec EME for versions of Safari which support it (#…
Browse files Browse the repository at this point in the history
…142) (#145)

This reverts commit 5897655.
  • Loading branch information
gkatsev committed Oct 19, 2021
1 parent f16cf69 commit fdb57e3
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 213 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,6 @@ player.src({
},
keySystems: {
'org.w3.clearkey': {
initDataTypes: ['cenc', 'webm'],
audioContentType: 'audio/webm; codecs="vorbis"',
videoContentType: 'video/webm; codecs="vp9"',
getCertificate: function(emeOptions, callback) {
Expand Down
140 changes: 43 additions & 97 deletions src/eme.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ import { requestPlayreadyLicense } from './playready';
import window from 'global/window';
import {mergeAndRemoveNull} from './utils';
import {httpResponseHandler} from './http-handler.js';
import {
defaultGetCertificate as defaultFairplayGetCertificate,
defaultGetLicense as defaultFairplayGetLicense,
defaultGetContentId as defaultFairplayGetContentId
} from './fairplay';

const isFairplayKeySystem = (str) => str.startsWith('com.apple.fps');

/**
* Returns an array of MediaKeySystemConfigurationObjects provided in the keySystem
Expand All @@ -22,21 +15,16 @@ const isFairplayKeySystem = (str) => str.startsWith('com.apple.fps');
* @return {Object[]}
* Array of MediaKeySystemConfigurationObjects
*/
export const getSupportedConfigurations = (keySystem, keySystemOptions) => {
export const getSupportedConfigurations = (keySystemOptions) => {
if (keySystemOptions.supportedConfigurations) {
return keySystemOptions.supportedConfigurations;
}

const isFairplay = isFairplayKeySystem(keySystem);
// TODO use initDataTypes when appropriate
const supportedConfiguration = {};
const initDataTypes = keySystemOptions.initDataTypes ||
// fairplay requires an explicit initDataTypes
(isFairplay ? ['sinf'] : null);
const audioContentType = keySystemOptions.audioContentType;
const audioRobustness = keySystemOptions.audioRobustness;
const videoContentType = keySystemOptions.videoContentType ||
// fairplay requires an explicit videoCapabilities/videoContentType
(isFairplay ? 'video/mp4' : null);
const videoContentType = keySystemOptions.videoContentType;
const videoRobustness = keySystemOptions.videoRobustness;
const persistentState = keySystemOptions.persistentState;

Expand Down Expand Up @@ -64,10 +52,6 @@ export const getSupportedConfigurations = (keySystem, keySystemOptions) => {
supportedConfiguration.persistentState = persistentState;
}

if (initDataTypes) {
supportedConfiguration.initDataTypes = initDataTypes;
}

return [supportedConfiguration];
};

Expand All @@ -78,7 +62,7 @@ export const getSupportedKeySystem = (keySystems) => {
let promise;

Object.keys(keySystems).forEach((keySystem) => {
const supportedConfigurations = getSupportedConfigurations(keySystem, keySystems[keySystem]);
const supportedConfigurations = getSupportedConfigurations(keySystems[keySystem]);

if (!promise) {
promise =
Expand All @@ -100,10 +84,8 @@ export const makeNewRequest = (requestOptions) => {
options,
getLicense,
removeSession,
eventBus,
contentId
eventBus
} = requestOptions;

const keySession = mediaKeys.createSession();

eventBus.trigger('keysessioncreated');
Expand All @@ -115,8 +97,7 @@ export const makeNewRequest = (requestOptions) => {
if (event.messageType !== 'license-request' && event.messageType !== 'license-renewal') {
return;
}

getLicense(options, event.message, contentId)
getLicense(options, event.message)
.then((license) => {
resolve(keySession.update(license));
})
Expand Down Expand Up @@ -211,27 +192,29 @@ export const addSession = ({
initData,
options,
getLicense,
contentId,
removeSession,
eventBus
}) => {
const sessionData = {
if (video.mediaKeysObject) {
return makeNewRequest({
mediaKeys: video.mediaKeysObject,
initDataType,
initData,
options,
getLicense,
removeSession,
eventBus
});
}

video.pendingSessionData.push({
initDataType,
initData,
options,
getLicense,
removeSession,
eventBus,
contentId
};

if (video.mediaKeysObject) {
sessionData.mediaKeys = video.mediaKeysObject;
return makeNewRequest(sessionData);
}

video.pendingSessionData.push(sessionData);

eventBus
});
return Promise.resolve();
};

Expand Down Expand Up @@ -278,8 +261,7 @@ export const addPendingSessions = ({
options: data.options,
getLicense: data.getLicense,
removeSession: data.removeSession,
eventBus: data.eventBus,
contentId: data.contentId
eventBus: data.eventBus
}));
}

Expand Down Expand Up @@ -310,10 +292,10 @@ export const defaultGetLicense = (keySystemOptions) => (emeOptions, keyMessage,
}, httpResponseHandler(callback, true));
};

const promisifyGetLicense = (keySystem, getLicenseFn, eventBus) => {
return (emeOptions, keyMessage, contentId) => {
const promisifyGetLicense = (getLicenseFn, eventBus) => {
return (emeOptions, keyMessage) => {
return new Promise((resolve, reject) => {
const callback = function(err, license) {
getLicenseFn(emeOptions, keyMessage, (err, license) => {
if (eventBus) {
eventBus.trigger('licenserequestattempted');
}
Expand All @@ -323,13 +305,7 @@ const promisifyGetLicense = (keySystem, getLicenseFn, eventBus) => {
}

resolve(license);
};

if (isFairplayKeySystem(keySystem)) {
getLicenseFn(emeOptions, contentId, new Uint8Array(keyMessage), callback);
} else {
getLicenseFn(emeOptions, keyMessage, callback);
}
});
});
};
};
Expand All @@ -339,36 +315,14 @@ const standardizeKeySystemOptions = (keySystem, keySystemOptions) => {
keySystemOptions = { url: keySystemOptions };
}

if (!keySystemOptions.url && keySystemOptions.licenseUri) {
keySystemOptions.url = keySystemOptions.licenseUri;
}

if (!keySystemOptions.url && !keySystemOptions.getLicense) {
throw new Error(`Missing url/licenseUri or getLicense in ${keySystem} keySystem configuration.`);
}

const isFairplay = isFairplayKeySystem(keySystem);

if (isFairplay && keySystemOptions.certificateUri && !keySystemOptions.getCertificate) {
keySystemOptions.getCertificate = defaultFairplayGetCertificate(keySystemOptions);
}

if (isFairplay && !keySystemOptions.getCertificate) {
throw new Error(`Missing getCertificate or certificateUri in ${keySystem} keySystem configuration.`);
}

if (isFairplay && !keySystemOptions.getContentId) {
keySystemOptions.getContentId = defaultFairplayGetContentId;
throw new Error('Neither URL nor getLicense function provided to get license');
}

if (keySystemOptions.url && !keySystemOptions.getLicense) {
if (keySystem === 'com.microsoft.playready') {
keySystemOptions.getLicense = defaultPlayreadyGetLicense(keySystemOptions);
} else if (isFairplay) {
keySystemOptions.getLicense = defaultFairplayGetLicense(keySystemOptions);
} else {
keySystemOptions.getLicense = defaultGetLicense(keySystemOptions);
}
keySystemOptions.getLicense = keySystem === 'com.microsoft.playready' ?
defaultPlayreadyGetLicense(keySystemOptions) :
defaultGetLicense(keySystemOptions);
}

return keySystemOptions;
Expand All @@ -384,21 +338,6 @@ export const standard5July2016 = ({
eventBus
}) => {
let keySystemPromise = Promise.resolve();
const keySystem = keySystemAccess.keySystem;
let keySystemOptions;

// try catch so that we return a promise rejection
try {
keySystemOptions = standardizeKeySystemOptions(
keySystem,
options.keySystems[keySystem]
);
} catch (e) {
return Promise.reject(e);
}

const contentId = keySystemOptions.getContentId ?
keySystemOptions.getContentId(options, initData) : null;

if (typeof video.mediaKeysObject === 'undefined') {
// Prevent entering this path again.
Expand All @@ -408,10 +347,16 @@ export const standard5July2016 = ({
video.pendingSessionData = [];

let certificate;
let keySystemOptions;

keySystemPromise = new Promise((resolve, reject) => {
// save key system for adding sessions
video.keySystem = keySystem;
video.keySystem = keySystemAccess.keySystem;

keySystemOptions = standardizeKeySystemOptions(
keySystemAccess.keySystem,
options.keySystems[keySystemAccess.keySystem]
);

if (!keySystemOptions.getCertificate) {
resolve(keySystemAccess);
Expand Down Expand Up @@ -447,17 +392,18 @@ export const standard5July2016 = ({
}

return keySystemPromise.then(() => {
// if key system has not been determined then addSession doesn't need getLicense
const getLicense = video.keySystem ?
promisifyGetLicense(keySystem, keySystemOptions.getLicense, eventBus) : null;
const {getLicense} = standardizeKeySystemOptions(
video.keySystem,
options.keySystems[video.keySystem]
);

return addSession({
video,
initDataType,
initData,
options,
getLicense,
contentId,
// if key system has not been determined then addSession doesn't need getLicense
getLicense: video.keySystem ? promisifyGetLicense(getLicense, eventBus) : null,
removeSession,
eventBus
});
Expand Down
2 changes: 1 addition & 1 deletion src/fairplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export const defaultGetCertificate = (fairplayOptions) => {
};
};

export const defaultGetContentId = (emeOptions, initData) => {
const defaultGetContentId = (emeOptions, initData) => {
return getHostnameFromUri(uint8ArrayToString(initData));
};

Expand Down
29 changes: 15 additions & 14 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,18 +225,7 @@ const onPlayerReady = (player, emeError) => {

setupSessions(player);

if (window.MediaKeys) {
// Support EME 05 July 2016
// Chrome 42+, Firefox 47+, Edge, Safari 12.1+ on macOS 10.14+
player.tech_.el_.addEventListener('encrypted', (event) => {
// TODO convert to videojs.log.debug and add back in
// https://github.com/videojs/video.js/pull/4780
// videojs.log('eme', 'Received an \'encrypted\' event');
setupSessions(player);
handleEncryptedEvent(event, getOptions(player), player.eme.sessions, player.tech_)
.catch(emeError);
});
} else if (window.WebKitMediaKeys) {
if (window.WebKitMediaKeys) {
const handleFn = (event) => {
// TODO convert to videojs.log.debug and add back in
// https://github.com/videojs/video.js/pull/4780
Expand Down Expand Up @@ -289,6 +278,18 @@ const onPlayerReady = (player, emeError) => {
}
});

} else if (window.MediaKeys) {
// Support EME 05 July 2016
// Chrome 42+, Firefox 47+, Edge, Safari 12.1+ on macOS 10.14+
player.tech_.el_.addEventListener('encrypted', (event) => {
// TODO convert to videojs.log.debug and add back in
// https://github.com/videojs/video.js/pull/4780
// videojs.log('eme', 'Received an \'encrypted\' event');
setupSessions(player);
handleEncryptedEvent(event, getOptions(player), player.eme.sessions, player.tech_)
.catch(emeError);
});

} else if (window.MSMediaKeys) {
// IE11 Windows 8.1+
// Since IE11 doesn't support promises, we have to use a combination of
Expand Down Expand Up @@ -363,7 +364,7 @@ const eme = function(options = {}) {

setupSessions(player);

if (window.MediaKeys) {
if (player.tech_.el_.setMediaKeys) {
handleEncryptedEvent(mockEncryptedEvent, mergedEmeOptions, player.eme.sessions, player.tech_)
.then(() => callback())
.catch((error) => {
Expand All @@ -372,7 +373,7 @@ const eme = function(options = {}) {
emeError(error);
}
});
} else if (window.MSMediaKeys) {
} else if (player.tech_.el_.msSetMediaKeys) {
const msKeyHandler = (event) => {
player.tech_.off('mskeyadded', msKeyHandler);
player.tech_.off('mskeyerror', msKeyHandler);
Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const stringToUint16Array = (string) => {
};

export const uint8ArrayToString = (array) => {
return String.fromCharCode.apply(null, new Uint8Array(array.buffer || array));
return String.fromCharCode.apply(null, new Uint16Array(array.buffer));
};

export const getHostnameFromUri = (uri) => {
Expand Down

0 comments on commit fdb57e3

Please sign in to comment.