Customizing license requests **************************** Modifiers are callbacks on a DRM system in a custom DRM environment. They adapt license and certificate requests/responses and init data to a license server's requirements: dynamic auth tokens, custom payloads, wrapped responses, custom FairPlay content IDs. Code samples: * Custom DRM environment (https://raw.githubusercontent.com/castlabs/prestoplay-web-code-samples/refs/heads/main/examples/drm/license_modifiers.js): registers a DRM environment with license request, license response, certificate request, and content ID modifiers. * Custom FairPlay key request (https://raw.githubusercontent.com/castlabs/prestoplay-web-code-samples/refs/heads/main/examples/drm/fairplay_custom_payload.js): builds the FairPlay key request payload with "transformInitData" and talks to a license server with a JSON API. Do you need modifiers? ====================== Check whether a built-in DRM environment already covers your license server: * With DRMtoday, the DRMtoday environment handles authentication, request format, and FairPlay content IDs for you. * If your license server only needs fixed headers, such as an API key, use the header-based DRM environment and pass them in "customData.headers". Use modifiers when your license server needs more than that, for example: +----------------------------------------------------+----------------------------------------------------+ | Your license server... | Use | |====================================================|====================================================| | needs a token that you fetch or refresh at request | "licenseRequest" | | time | | +----------------------------------------------------+----------------------------------------------------+ | expects the license challenge in a custom request | "licenseRequest" | | body | | +----------------------------------------------------+----------------------------------------------------+ | returns the license wrapped in JSON or another | "licenseResponse" | | format | | +----------------------------------------------------+----------------------------------------------------+ | needs headers on the FairPlay certificate request | "certificateRequest" | +----------------------------------------------------+----------------------------------------------------+ | expects a different FairPlay content ID than the | "extractContentId" | | default | | +----------------------------------------------------+----------------------------------------------------+ | needs a custom FairPlay key request payload | "transformInitData" | +----------------------------------------------------+----------------------------------------------------+ How modifiers work ================== Modifiers belong to a DRM system in a custom DRM environment. Pass them in the "modifiers" object of "drm.DrmEnvironment#setDrmSystem" (https://players.castlabs.com/web/latest/docs/api/clpp.drm.DrmEnvironment.md#setDrmSystem): const environment = new clpp.drm.DrmEnvironment('myDrm'); environment.setDrmSystem(clpp.drm.KeySystem.WIDEVINE, { licenseUrl: 'https://license.example.com/widevine', modifiers: { licenseRequest: (config, request, sessionContext) => { /* ... */ }, licenseResponse: (config, response, sessionContext) => { /* ... */ }, }, }); clpp.drm.registerDrmEnvironment(environment); Each DRM system takes its own set of modifiers, so you can handle Widevine, PlayReady, and FairPlay differently. The available modifiers are defined in "drm.LicenseModifiers" (https://players.castlabs.com/web/latest/docs/api/clpp.drm.LicenseModifiers.md): +-----------------------------------+-----------------------------------+--------------------------------------------------------------------------------------+ | Modifier | Runs | Receives | |===================================|===================================|======================================================================================| | "licenseRequest" | Before a license request is sent. | "net.Request" | | | | (https://players.castlabs.com/web/latest/docs/api/clpp.net.Request.md) | +-----------------------------------+-----------------------------------+--------------------------------------------------------------------------------------+ | "licenseResponse" | After a license response arrives, | "net.Response" | | | before the CDM gets the license. | (https://players.castlabs.com/web/latest/docs/api/clpp.net.Response.md) | +-----------------------------------+-----------------------------------+--------------------------------------------------------------------------------------+ | "certificateRequest" | Before a certificate request is | "net.Request" | | | sent. | (https://players.castlabs.com/web/latest/docs/api/clpp.net.Request.md) | +-----------------------------------+-----------------------------------+--------------------------------------------------------------------------------------+ | "certificateResponse" | After a certificate response | "net.Response" | | | arrives. | (https://players.castlabs.com/web/latest/docs/api/clpp.net.Response.md) | +-----------------------------------+-----------------------------------+--------------------------------------------------------------------------------------+ | "extractContentId" | When the player extracts the | Init data | | | FairPlay content ID from init | | | | data. | | +-----------------------------------+-----------------------------------+--------------------------------------------------------------------------------------+ | "transformInitData" | Before init data is passed to the | "InitDataTransformerParams" | | | CDM. | (https://players.castlabs.com/web/latest/docs/api/clpp.InitDataTransformerParams.md) | +-----------------------------------+-----------------------------------+--------------------------------------------------------------------------------------+ Request and response modifiers change the request or response in place, for example "request.headers" or "response.data". They can also return a promise, and the player waits for it before it continues. To pass data between modifiers of the same media session, store it in "sessionContext.customData". See "SessionContext" (https://players.castlabs.com/web/latest/docs/api/clpp.SessionContext.md). Add authentication to license requests ====================================== If your license server needs a short-lived token, fetch it in the "licenseRequest" modifier and add it as a header. Because the modifier returns a promise, the license request waits until the token is available: environment.setDrmSystem(clpp.drm.KeySystem.WIDEVINE, { licenseUrl: 'https://license.example.com/widevine', modifiers: { licenseRequest: async (config, request) => { // Your own backend call that returns a fresh token. const token = await fetchLicenseToken(); request.headers['Authorization'] = `Bearer ${token}`; }, }, }); Send a custom request and unwrap the response ============================================= Some license servers don't accept the raw license challenge from the CDM. For example, a server might expect the challenge as Base64 in a JSON body and return the license as Base64 in a JSON response. Use "licenseRequest" to build the body and "licenseResponse" to extract the license: const { toStandardBase64, fromBase64 } = clpp.utils.Uint8ArrayUtils; const { toUtf8, fromUtf8 } = clpp.utils.strings; environment.setDrmSystem(clpp.drm.KeySystem.WIDEVINE, { licenseUrl: 'https://license.example.com/widevine', modifiers: { licenseRequest: (config, request) => { const body = { challenge: toStandardBase64(new Uint8Array(request.body)), contentId: 'my-content-id', }; request.headers['Content-Type'] = 'application/json'; request.body = toUtf8(JSON.stringify(body)).buffer; }, licenseResponse: (config, response) => { const { license } = JSON.parse(fromUtf8(response.data)); response.data = fromBase64(license).buffer; }, }, }); Use a custom FairPlay content ID ================================ For FairPlay, the player extracts a content ID from the init data and sends it to the license server. By default, it uses the hostname part of the HLS "#EXT-X-KEY" URI. For this tag, the content ID is "d192ebad-5097": #EXT-X-KEY:METHOD=SAMPLE-AES,URI="skd://d192ebad-5097", KEYFORMAT="com.apple.streamingkeydelivery",KEYFORMATVERSIONS="1" If your FairPlay Streaming (FPS) license server expects a different value, return it from "extractContentId". This example sends only "d192ebad": environment.setDrmSystem(clpp.drm.KeySystem.FAIRPLAY, { licenseUrl: 'https://license.example.com/fairplay', certificateUrl: 'https://license.example.com/fairplay.cer', modifiers: { extractContentId: (config, initData) => { const { extractContentId } = clpp.utils.FairplayUtils; const fullHostname = extractContentId(initData); // d192ebad-5097 return fullHostname.split('-')[0]; // d192ebad }, }, }); See "drm.ContentIdExtractor" (https://players.castlabs.com/web/latest/docs/api/clpp.drm.ContentIdExtractor.md). Build a custom FairPlay key request =================================== If changing the content ID isn't enough, "transformInitData" gives you full control over the payload the CDM uses to generate a key request. Use it when your license server needs a content ID from an external source, such as your own API, or a payload format of its own. The Custom FairPlay key request sample (https://raw.githubusercontent.com/castlabs/prestoplay-web-code-samples/refs/heads/main/examples/drm/fairplay_custom_payload.js) builds the FairPlay payload with a content ID from your own data. It stores the key ID in "sessionContext.customData", so the "licenseRequest" and "licenseResponse" modifiers can use it to talk to a license server with a JSON API. The utilities in "utils.FairplayUtils" (https://players.castlabs.com/web/latest/docs/api/clpp.utils.FairplayUtils.md), "utils.strings" (https://players.castlabs.com/web/latest/docs/api/clpp.utils.strings.md), and "utils.BufferUtils" (https://players.castlabs.com/web/latest/docs/api/clpp.utils.BufferUtils.md) help with this processing. See "drm.InitDataTransformer" (https://players.castlabs.com/web/latest/docs/api/clpp.drm.InitDataTransformer.md) and the EME initialization data spec (https://w3c.github.io/encrypted-media/#initialization-data). Modifiers on Chromecast receivers ================================= In the Chromecast receiver context, the license modifiers have limited functionality, based on the data the Cast Application Framework (CAF) SDK provides: * Request modifiers don't have access to most of the request data, such as "licenseRequestType". * Response modifiers have access only to the license data and headers, not the associated request. * "licenseResponse" can be asynchronous, but "licenseRequest" must be synchronous. See the CAF SDK reference for licenseRequestHandler (https://developers.google.com/cast/docs/reference/web_receiver/cast.framework.PlaybackConfig#licenseRequestHandler), licenseHandler (https://developers.google.com/cast/docs/reference/web_receiver/cast.framework.PlaybackConfig#licenseHandler), NetworkRequestInfo (https://developers.google.com/cast/docs/reference/web_receiver/cast.framework.NetworkRequestInfo), and NetworkResponseInfo (https://developers.google.com/cast/docs/reference/web_receiver/cast.framework.NetworkResponseInfo).