[Javascript] Encrypted Media Extensions API

https://developer.mozilla.org/en-US/docs/Web/API/Encrypted_Media_Extensions_API

1. Video screenshot blocking

when you render video through a DRM‐protected pipeline via Encrypted Media Extensions. By requesting a “hardware‐secure” video session, the browser/OS will render on a protected surface that blocks screen captures.

const video = document.querySelector('video')
navigator
	requestMediaKeySystemAccess('com.widevine.alpha', [{
		initDataTypes: ['cenc'],
		videoCapabilities: [{
			contentType: 'video/mp4; codecs="avc1.42E01E"',
			robustness: 'HW_SECURE_ALL'  // enforce protected output
		}]
	}])
.then(keySystemAccess => keySystemAccess.createMediaKeys())
.then(mediaKeys => video.setMediaKeys(mediaKeys))
.then(() => {
	video.addEventListener('encrypted', async event => {
		const session = mediaKeys.createSession()
		await session.generateRequest(event.initDataType, event.initData)
		const license = await fetch('/license', {
			method: 'POST',
			body: event.initData
		}).then(r => r.arrayBuffer())
		await session.update(license)
	})
})
.catch(err => console.error('EME setup failed', err))

 

2. Pay-per-view, rentals & subscription gating
Hook your license server into MediaKeySession to enforce pay-per-view or time-limited rentals. You can issue licenses that expire after a window, or revoke them on demand via session.close() or session.remove() developer.mozilla.org.

 

3. Offline playback (persistent licenses)
Request persistentState: 'required' and sessionTypes: ['persistent-license'] to let users download content and play it offline. The CDM stores the license locally, so your app can play without re-fetching developer.mozilla.org.

navigator.requestMediaKeySystemAccess('com.widevine.alpha', [{
  initDataTypes: ['cenc'],
  videoCapabilities: [{ contentType: 'video/mp4; codecs="avc1.42E01E"' }],
  persistentState: 'required',
  sessionTypes: ['persistent-license']
}])
// …create keys, handle encrypted event…

 

4. Multi-DRM & fallback
Enumerate multiple key systems (Widevine, PlayReady, FairPlay) so you serve the right stream on each browser/device—no more “sorry, Safari doesn’t work” messages 

 

5. Selective quality & HDCP enforcement
In videoCapabilities you can set robustness: 'HW_SECURE_ALL' (or ‘SW_SECURE_DECODE’) to ensure HD or 4K only plays over protected pipelines (HDCP-compliant). If the client can’t meet that, you fall back to lower-res or block playback.

 

6. Analytics & auditing
Listen to keystatuseschange events to track when keys become usable, expire, or are revoked—feed that back into your telemetry for usage patterns or compliance reporting developer.mozilla.org.

 

7. Audio DRM
EME works for audio too—perfect for a premium music-streaming service where you need to lock down high-bitrate tracks or special editions.

 

8. Forensic watermark triggers
Embed unique user IDs or timestamps in the license response so the CDM can trigger watermarking in the video/audio pipeline downstream—helping you trace leaks back to a source.

 

9. Custom key-exchange workflows
Use MediaKeyMessageEvent.messageType (e.g. ‘license-request’, ‘license-renewal’, ‘individualization-request’) to hook into complex licensing schemes—group-based entitlements, multi-stage attestation, hardware-backed key provisioning, etc.

posted @ 2025-06-23 02:18  Zhentiw  阅读(27)  评论(0)    收藏  举报