Skip to content

Commit

Permalink
fix: Fix ClearKey license on old CDMs (#7816)
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad authored Dec 31, 2024
1 parent 626591e commit fe1f35b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
45 changes: 45 additions & 0 deletions lib/drm/drm_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,11 @@ shaka.drm.DrmEngine = class {
}
// NOTE: allowCrossSiteCredentials can be set in a request filter.

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 @@ -1678,6 +1683,31 @@ shaka.drm.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 @@ -2729,6 +2759,21 @@ shaka.drm.DrmEngine.SessionMetaData;
*/
shaka.drm.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
8 changes: 8 additions & 0 deletions lib/drm/drm_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ shaka.drm.DrmUtils = class {
return drmInfo ? drmInfo.keySystem : '';
}

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

/**
* @param {?string} keySystem
* @return {boolean}
Expand Down
16 changes: 16 additions & 0 deletions test/drm/drm_utils_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,22 @@ 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(
Expand Down

0 comments on commit fe1f35b

Please sign in to comment.