Skip to content

Commit

Permalink
fix: Fix ClearKey license on old CDMs (#7816)
Browse files Browse the repository at this point in the history
Backported to v4.12.x
  • Loading branch information
avelad authored and joeyparrish committed Jan 6, 2025
1 parent c4e9d1c commit bf6376a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
47 changes: 46 additions & 1 deletion lib/media/drm_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -1433,7 +1433,12 @@ shaka.media.DrmEngine = class {
}
// NOTE: allowCrossSiteCredentials can be set in a request filter.

if (shaka.util.DrmUtils.isPlayReadyKeySystem(
if (shaka.drm.DrmUtils.isClearKeySystem(
this.currentDrmInfo_.keySystem)) {
this.fixClearKeyRequest_(request, this.currentDrmInfo_);
}

if (shaka.drm.DrmUtils.isPlayReadyKeySystem(
this.currentDrmInfo_.keySystem)) {
this.unpackPlayReadyRequest_(request);
}
Expand Down Expand Up @@ -1584,6 +1589,31 @@ shaka.media.DrmEngine = class {
/** @type{string} */(shaka.util.TXml.getTextContents(challenge)));
}

/**
* Some old ClearKey CDMs don't include the type in the body request.
*
* @param {shaka.extern.Request} request
* @param {shaka.extern.DrmInfo} drmInfo
* @private
*/
fixClearKeyRequest_(request, drmInfo) {
try {
const body = shaka.util.StringUtils.fromBytesAutoDetect(request.body);
if (body) {
const licenseBody =
/** @type {shaka.drm.DrmEngine.ClearKeyLicenceRequestFormat} */ (
JSON.parse(body));
if (!licenseBody.type) {
licenseBody.type = drmInfo.sessionType;
request.body =
shaka.util.StringUtils.toUTF8(JSON.stringify(licenseBody));
}
}
} catch (e) {
shaka.log.info('Error unpacking ClearKey license', e);
}
}

/**
* @param {!Event} event
* @private
Expand Down Expand Up @@ -2603,6 +2633,21 @@ shaka.media.DrmEngine.SessionMetaData;
*/
shaka.media.DrmEngine.PlayerInterface;

/**
* @typedef {{
* kids: !Array.<string>,
* type: string
* }}
*
* @property {!Array.<string>} kids
* An array of key IDs. Each element of the array is the base64url encoding of
* the octet sequence containing the key ID value.
* @property {string} type
* The requested MediaKeySessionType.
* @see https://www.w3.org/TR/encrypted-media/#clear-key-request-format
*/
shaka.drm.DrmEngine.ClearKeyLicenceRequestFormat;

/**
* The amount of time, in seconds, we wait to consider a session closed.
* This allows us to work around Chrome bug https://crbug.com/1108158.
Expand Down
20 changes: 20 additions & 0 deletions lib/util/drm_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,26 @@ shaka.util.DrmUtils = class {
return drmInfo ? drmInfo.keySystem : '';
}

/**
* @param {?string} keySystem
* @return {boolean}
*/
static isClearKeySystem(keySystem) {
return keySystem === 'org.w3.clearkey';
}

/**
* @param {?string} keySystem
* @return {boolean}
*/
static isWidevineKeySystem(keySystem) {
if (keySystem) {
return !!keySystem.match(/^com\.widevine\.alpha/);
}

return false;
}

/**
* @param {?string} keySystem
* @return {boolean}
Expand Down
32 changes: 32 additions & 0 deletions test/util/drm_utils_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,38 @@ describe('DrmUtils', () => {
});
}); // describe('getCommonDrmInfos')

describe('isClearKeySystem', () => {
it('should return true for ClearKey', () => {
expect(shaka.drm.DrmUtils.isClearKeySystem(
'org.w3.clearkey')).toBe(true);
});

it('should return false for non-ClearKey key systems', () => {
expect(shaka.drm.DrmUtils.isClearKeySystem(
'com.widevine.alpha')).toBe(false);
expect(shaka.drm.DrmUtils.isClearKeySystem(
'com.microsoft.playready')).toBe(false);
expect(shaka.drm.DrmUtils.isClearKeySystem(
'com.apple.fps')).toBe(false);
});
});

describe('isWidevineKeySystem', () => {
it('should return true for Widevine', () => {
expect(shaka.drm.DrmUtils.isWidevineKeySystem(
'com.widevine.alpha')).toBe(true);
expect(shaka.drm.DrmUtils.isWidevineKeySystem(
'com.widevine.alpha.anything')).toBe(true);
});

it('should return false for non-Widevine key systems', () => {
expect(shaka.drm.DrmUtils.isWidevineKeySystem(
'com.microsoft.playready')).toBe(false);
expect(shaka.drm.DrmUtils.isWidevineKeySystem(
'com.apple.fps')).toBe(false);
});
});

describe('isPlayReadyKeySystem', () => {
it('should return true for MS & Chromecast PlayReady', () => {
expect(shaka.util.DrmUtils.isPlayReadyKeySystem(
Expand Down

0 comments on commit bf6376a

Please sign in to comment.