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

[v3] DRM: Only check cached MediaKeySystemAccess compatibility with most wanted key system #1596

Closed
Closed
Changes from all commits
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
104 changes: 49 additions & 55 deletions src/core/decrypt/find_key_system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ interface IKeySystemType {
}

/**
* @param {Array.<Object>} keySystems
* @param {Object} keySystem
* @param {Object} askedConfiguration
* @param {MediaKeySystemAccess} currentKeySystemAccess
* @param {Object} currentKeySystemOptions
* @returns {null|Object}
*/
function checkCachedMediaKeySystemAccess(
keySystems: IKeySystemOption[],
keySystem: IKeySystemOption,
askedConfiguration: MediaKeySystemConfiguration,
currentKeySystemAccess: MediaKeySystemAccess | ICustomMediaKeySystemAccess,
currentKeySystemOptions: IKeySystemOption,
Expand All @@ -96,37 +96,31 @@ function checkCachedMediaKeySystemAccess(
return null;
}

const firstCompatibleOption = keySystems.filter((ks) => {
// TODO Do it with MediaKeySystemAccess.prototype.keySystem instead
if (ks.type !== currentKeySystemOptions.type) {
return false;
}

if (
(ks.persistentLicense === true || ks.persistentStateRequired === true) &&
mksConfiguration.persistentState !== "required"
) {
return false;
}

if (
ks.distinctiveIdentifierRequired === true &&
mksConfiguration.distinctiveIdentifier !== "required"
) {
return false;
}
// TODO Do it with MediaKeySystemAccess.prototype.keySystem instead
if (keySystem.type !== currentKeySystemOptions.type) {
return null;
}

return true;
})[0];
if (
(keySystem.persistentLicense === true ||
keySystem.persistentStateRequired === true) &&
mksConfiguration.persistentState !== "required"
) {
return null;
}

if (firstCompatibleOption != null) {
return {
keySystemOptions: firstCompatibleOption,
keySystemAccess: currentKeySystemAccess,
askedConfiguration,
};
if (
keySystem.distinctiveIdentifierRequired === true &&
mksConfiguration.distinctiveIdentifier !== "required"
) {
return null;
}
return null;

return {
keySystemOptions: keySystem,
keySystemAccess: currentKeySystemAccess,
askedConfiguration,
};
}

/**
Expand Down Expand Up @@ -302,31 +296,6 @@ export default function getMediaKeySystemAccess(
cancelSignal: CancellationSignal,
): Promise<IFoundMediaKeySystemAccessEvent> {
log.info("DRM: Searching for compatible MediaKeySystemAccess");
const currentState = MediaKeysInfosStore.getState(mediaElement);
if (currentState !== null) {
if (eme.implementation === currentState.emeImplementation.implementation) {
// Fast way to find a compatible keySystem if the currently loaded
// one as exactly the same compatibility options.
const cachedKeySystemAccess = checkCachedMediaKeySystemAccess(
keySystemsConfigs,
currentState.askedConfiguration,
currentState.mediaKeySystemAccess,
currentState.keySystemOptions,
);
if (cachedKeySystemAccess !== null) {
log.info("DRM: Found cached compatible keySystem");
return Promise.resolve({
type: "reuse-media-key-system-access" as const,
value: {
mediaKeySystemAccess: cachedKeySystemAccess.keySystemAccess,
askedConfiguration: cachedKeySystemAccess.askedConfiguration,
options: cachedKeySystemAccess.keySystemOptions,
},
});
}
}
}

/**
* Array of set keySystems for this content.
* Each item of this array is an object containing the following keys:
Expand Down Expand Up @@ -388,6 +357,31 @@ export default function getMediaKeySystemAccess(

const { keyName, keyType, keySystemOptions } = keySystemsType[index];

const currentState = MediaKeysInfosStore.getState(mediaElement);
if (currentState !== null) {
if (eme.implementation === currentState.emeImplementation.implementation) {
// Fast way to find a compatible keySystem if the currently loaded
// one as exactly the same compatibility options.
const cachedKeySystemAccess = checkCachedMediaKeySystemAccess(
keySystemOptions,
currentState.askedConfiguration,
currentState.mediaKeySystemAccess,
currentState.keySystemOptions,
);
if (cachedKeySystemAccess !== null) {
log.info("DRM: Found cached compatible keySystem");
return Promise.resolve({
type: "reuse-media-key-system-access" as const,
value: {
mediaKeySystemAccess: cachedKeySystemAccess.keySystemAccess,
askedConfiguration: cachedKeySystemAccess.askedConfiguration,
options: cachedKeySystemAccess.keySystemOptions,
},
});
}
}
}

const keySystemConfigurations = buildKeySystemConfigurations(
keyName,
keyType,
Expand Down
Loading