From 42381390bfbbbef45dfcaa4ed39b33c35b4abe16 Mon Sep 17 00:00:00 2001 From: Dave Kiss <1256071+davekiss@users.noreply.github.com> Date: Tue, 24 Jun 2025 16:26:48 -0400 Subject: [PATCH 1/2] Add DRM chromecast example --- drm-and-chromecast/receiver.js | 37 ++++++++++++++++++++++++++++++++++ drm-and-chromecast/sender.js | 30 +++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 drm-and-chromecast/receiver.js create mode 100644 drm-and-chromecast/sender.js diff --git a/drm-and-chromecast/receiver.js b/drm-and-chromecast/receiver.js new file mode 100644 index 0000000..4766710 --- /dev/null +++ b/drm-and-chromecast/receiver.js @@ -0,0 +1,37 @@ +/** + * DEBUGGING + */ +// https://developers.google.com/cast/docs/debugging/cast_debug_logger +const castDebugLogger = cast.debug.CastDebugLogger.getInstance(); +const LOG_TAG = 'MUX'; +castDebugLogger.setEnabled(true); + +// Debug overlay on tv screen. You don't need this if you're debugging using the cast tool (https://casttool.appspot.com/cactool) as it will show the logs in your browser. +castDebugLogger.showDebugLogs(true); + +castDebugLogger.loggerLevelByTags = { + [LOG_TAG]: cast.framework.LoggerLevel.DEBUG, +}; + +/** + * DRM SUPPORT + */ +context.getPlayerManager().setMediaPlaybackInfoHandler((loadRequest, playbackConfig) => { + const customData = loadRequest.media.customData || {}; + + if (customData.mux && customData.mux.tokens.drm) { + castDebugLogger.debug(LOG_TAG, 'Setting license URL.'); + playbackConfig.licenseUrl = `https://license.mux.com/license/widevine/${customData.mux.playbackId}?token=${customData.mux.tokens.drm}`; + } + + playbackConfig.protectionSystem = cast.framework.ContentProtection.WIDEVINE; + + castDebugLogger.debug(LOG_TAG, 'license url', playbackConfig.licenseUrl); + + return playbackConfig; +}); + +/** + * START LISTENING FOR CASTS + */ +context.start(); \ No newline at end of file diff --git a/drm-and-chromecast/sender.js b/drm-and-chromecast/sender.js new file mode 100644 index 0000000..2d42d8d --- /dev/null +++ b/drm-and-chromecast/sender.js @@ -0,0 +1,30 @@ +function playVideo(context) { + const playbackId = '...'; + const playbackToken = '...'; + const drmToken = '...'; + const mediaUrl = `https://stream.mux.com/${playbackId}.m3u8?token=${playbackToken}`; + let mediaInfo = new chrome.cast.media.MediaInfo(mediaUrl, 'application/x-mpegurl'); + + // Mux HLS URLs with DRM will always use `fmp4` segments. + mediaInfo.hlsSegmentFormat = chrome.cast.media.HlsSegmentFormat.FMP4; + mediaInfo.hlsVideoSegmentFormat = chrome.cast.media.HlsVideoSegmentFormat.FMP4; + + // Send the information needed to create a new license url. + mediaInfo.customData = { + mux: { + playbackId, + tokens: { + drm: drmToken + } + } + } + + const request = new chrome.cast.media.LoadRequest(mediaInfo); + + // Cast the video. + context.getCurrentSession().loadMedia(request).then(() => { + console.log('Load Succeeded'); + }).catch((err) => { + console.log(`Error code: ${errorCode}`); + }); +} \ No newline at end of file From c616f65e471aeb228d21351fd1af3b3453ac54e9 Mon Sep 17 00:00:00 2001 From: Dave Kiss <1256071+davekiss@users.noreply.github.com> Date: Fri, 27 Jun 2025 11:21:16 -0400 Subject: [PATCH 2/2] Update drm-and-chromecast/sender.js Co-authored-by: Aaron Hedges <218751+Dashron@users.noreply.github.com> --- drm-and-chromecast/sender.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/drm-and-chromecast/sender.js b/drm-and-chromecast/sender.js index 2d42d8d..e7b9005 100644 --- a/drm-and-chromecast/sender.js +++ b/drm-and-chromecast/sender.js @@ -27,4 +27,13 @@ function playVideo(context) { }).catch((err) => { console.log(`Error code: ${errorCode}`); }); -} \ No newline at end of file +} +let context = cast.framework.CastContext.getInstance(); +context.addEventListener(cast.framework.CastContextEventType.SESSION_STATE_CHANGED, function(event) { + switch (event.sessionState) { + case cast.framework.SessionState.SESSION_STARTED: + case cast.framework.SessionState.SESSION_RESUMED: + playVideo(context); + break; + } +}); \ No newline at end of file