ExoPlayer 整合

本文件提供佇列與 DRM 整合支援總覽。

DRM 強化功能

ExoPlayer Cast Demo 已更新,可透過結構化方法,使用 ExoPlayer 的 MediaInfo 將 DRM 設定傳送至接收器應用程式。Cast 範例也會使用包含本總覽中相同程式碼的示範接收器,可讓您測試 DRM 支援。不過,如要投放 DRM 保護內容,請建構並代管自己的網路接收器

開始之前,建議您先熟悉 Google CastExoPlayer 的 DRM 支援說明文件。本總覽將說明如何將 ExoPlayer DRM 設定連接至網路接收端。如要瞭解如何在 ExoPlayer 中使用 DRM,請參閱官方 ExoPlayer 網站

提供 DRM 設定

ExoPlayer 試用版應用程式包含程式碼範例,示範如何在 MediaItem 中提供 DRM 設定。您可以設定的四個選項如下:

  • 標頭 - 為擷取 DRM 授權而套用至 HTTPS 要求的標頭字典。
  • 授權網址:用來取得授權的網址。
  • 保護系統 - 用於保護內容的 DRM 保護配置,例如 Widevine。

您提供給 ExoPlayer 的 DRM 設定會在載入要求中,以 MediaInformation 物件的 customData 屬性的形式傳送至接收器應用程式。根據預設,此屬性的名稱為 exoPlayerConfig,符合以下定義。

/**
 * Extended configuration settings for ExoPlayer.
 */
ExoPlayerConfig class {
   constructor() {
    /**
     * Dictionary of headers to apply to the license request.
     * @type {!Object|undefined}
     */
    this.headers;

    /**
     * The URL for your DRM server.
     * @type {string|undefined}
     */
    this.licenseUrl;

    /**
     * Preferred protection system to use for decrypting content.
     * @type {!cast.framework.ContentProtection|undefined}
     */
    this.protectionSystem;

    /**
     * Indicates whether CORS Access-Control requests should be made using
     * credentials such as cookies or authorization headers.
     *
     * If withCredentials is set to true then Access-Control-Allow-Origin cannot
     * be set to '*'.
     * @type {boolean|undefined}
     */
    this.withCredentials;
  }
}

初始設定

視您使用的數位版權管理解決方案而定,您可能需要設定 licenseRequestHandlermediaPlaybackInfoHandlerlicenseRequestHandler 可讓您自訂 CAF 向授權金鑰伺服器要求授權的方式。舉例來說,如果部分內容必須使用不同的授權伺服器網址,mediaPlaybackInfoHandler 可讓您針對每個媒體項目修改 PlaybackConfig

如要從每個載入要求物件擷取 ExoPlayerConfig 的副本,請在 Web Receiver SDK 應用程式中建立載入要求攔截器。

第一步是註冊處理常式,然後啟動 Cast 應用程式。

const context = cast.framework.CastReceiverContext.getInstance();
const playbackConfig = new cast.framework.PlaybackConfig();

playbackConfig.licenseRequestHandler =
    licenseRequestHandler;
context.getPlayerManager().setMediaPlaybackInfoHandler(
    mediaPlaybackInfoHandler);
context.getPlayerManager().setMessageInterceptor(
    cast.framework.messages.MessageType.LOAD,
    loadInterceptor);

// starts the Cast application
context.start({playbackConfig: playbackConfig});

載入要求攔截器

載入要求攔截器是一種回呼,可讓您在 CAF 嘗試載入媒體項目之前,先查看及修改投放載入要求。重要的是,系統會在授權要求處理常式和媒體播放資訊處理常式之前呼叫此物件。

系統會傳遞載入要求攔截器的 LoadRequestData 物件,該物件包含應用程式傳送的 Exo Player 設定。您可以將此物件儲存為全域變數,以便在授權要求處理常式和媒體播放資訊處理常式中使用。

loadInterceptor(loadRequestData) {
    // not every load request will have a customData object
    if (loadRequestData.media && loadRequestData.media.customData &&
            loadRequestData.media.customData['exoPlayerConfig']) {
        // exoPlayerConfig is a global variable here
        exoPlayerConfig =
                loadRequestData.media.customData['exoPlayerConfig'];
    }

    // you must return the loadRequestData object
    return loadRequestData;
}

授權要求處理常式

授權要求處理常式可讓您自訂 Web 接收器對授權伺服器發出的 HTTPS 要求。處理常式會傳遞 NetworkRequestInfo 物件,您可以使用該物件新增 HTTP 標頭、加入 Cookie,甚至是修改網址。處理常式應會傳回此物件。

例如,如果您需要在授權要求中加入自訂標頭,可以建立類似下方的授權要求處理常式:

licenseRequestHandler(networkRequestInfo) {
    if (!exoPlayerConfig) {
        return networkRequestInfo;
    }

    networkRequestInfo.headers =
            exoPlayerConfig.headers ? exoPlayerConfig.headers : undefined;

    return networkRequestInfo;
}

媒體播放資訊處理常式

媒體播放資訊處理常式可讓您變更每個媒體項目的播放設定。這個處理常式會傳遞 LoadRequestDataPlaybackConfig,您應傳回播放設定。系統會在載入每個投放項目前呼叫媒體播放資訊處理常式。如果您擁有個別內容授權網址,可以在載入前變更這些網址和防護系統。

mediaPlaybackInfoHandler(loadRequest, playbackConfig) {
    if (!exoPlayerConfig) {
        return;
    }

    playbackConfig.licenseUrl = exoPlayerConfig.licenseUrl ?
            exoPlayerConfig.licenseUrl :
            undefined;
    playbackConfig.protectionSystem = exoPlayerConfig.protectionSystem ?
            exoPlayerConfig.protectionSystem :
            undefined;

    return playbackConfig;
}

其他資源

每項 DRM 實作都是自訂,此程式碼僅供參考。您應諮詢 DRM 供應商,確定您已在 ExoPlayer 和投放應用程式中正確導入 DRM。

ExoPlayer 網站提供最新的說明文件和公告。如要回報 ExoPlayer 及其 Cast 整合的問題,請前往 ExoPlayer 的 GitHub 存放區。