diff --git a/android/src/main/java/com/brentvatne/common/api/DRMProps.kt b/android/src/main/java/com/brentvatne/common/api/DRMProps.kt new file mode 100644 index 00000000..a26d048a --- /dev/null +++ b/android/src/main/java/com/brentvatne/common/api/DRMProps.kt @@ -0,0 +1,66 @@ +package com.brentvatne.common.api + +import com.brentvatne.common.toolbox.ReactBridgeUtils.safeGetArray +import com.brentvatne.common.toolbox.ReactBridgeUtils.safeGetString +import com.facebook.react.bridge.ReadableMap +import java.util.UUID + +/** + * Class representing DRM props for host. + * Only generic code here, no reference to the player. + */ +class DRMProps { + /** + * string version of configured UUID for drm prop + */ + var drmType: String? = null + + /** + * Configured UUID for drm prop + */ + var drmUUID: UUID? = null + + /** + * DRM license server to be used + */ + var drmLicenseServer: String? = null + + /** + * DRM Http Header to access to license server + */ + var drmLicenseHeader: Array = emptyArray() + companion object { + private const val PROP_DRM_TYPE = "type" + private const val PROP_DRM_LICENSE_SERVER = "licenseServer" + private const val PROP_DRM_HEADERS = "headers" + private const val PROP_DRM_HEADERS_KEY = "key" + private const val PROP_DRM_HEADERS_VALUE = "value" + + /** parse the source ReadableMap received from app */ + @JvmStatic + fun parse(src: ReadableMap?): DRMProps? { + var drm: DRMProps? = null + if (src != null && src.hasKey(PROP_DRM_TYPE)) { + drm = DRMProps() + drm.drmType = safeGetString(src, PROP_DRM_TYPE) + drm.drmLicenseServer = safeGetString(src, PROP_DRM_LICENSE_SERVER) + val drmHeadersArray = safeGetArray(src, PROP_DRM_HEADERS) + if (drm.drmType != null && drm.drmLicenseServer != null) { + if (drmHeadersArray != null) { + val drmKeyRequestPropertiesList = ArrayList() + for (i in 0 until drmHeadersArray.size()) { + val current = drmHeadersArray.getMap(i) + drmKeyRequestPropertiesList.add(safeGetString(current, PROP_DRM_HEADERS_KEY)) + drmKeyRequestPropertiesList.add(safeGetString(current, PROP_DRM_HEADERS_VALUE)) + } + val array = emptyArray() + drm.drmLicenseHeader = drmKeyRequestPropertiesList.toArray(array) + } + } else { + return null + } + } + return drm + } + } +} diff --git a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java index 69ef919b..b306dcf3 100644 --- a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -110,6 +110,7 @@ import androidx.media3.ui.LegacyPlayerControlView; import com.brentvatne.common.api.BufferConfig; import com.brentvatne.common.api.BufferingStrategy; import com.brentvatne.common.api.ControlsConfig; +import com.brentvatne.common.api.DRMProps; import com.brentvatne.common.api.ResizeMode; import com.brentvatne.common.api.SideLoadedTextTrack; import com.brentvatne.common.api.SideLoadedTextTrackList; @@ -238,9 +239,7 @@ public class ReactExoplayerView extends FrameLayout implements private float mProgressUpdateInterval = 250.0f; private boolean playInBackground = false; private boolean mReportBandwidth = false; - private UUID drmUUID = null; - private String drmLicenseUrl = null; - private String[] drmLicenseHeader = null; + private DRMProps drmProps; private boolean controls; private Uri adTagUrl; @@ -774,10 +773,11 @@ public class ReactExoplayerView extends FrameLayout implements private DrmSessionManager initializePlayerDrm(ReactExoplayerView self) { DrmSessionManager drmSessionManager = null; - if (self.drmUUID != null) { + if (self.drmProps != null) { try { - drmSessionManager = self.buildDrmSessionManager(self.drmUUID, self.drmLicenseUrl, - self.drmLicenseHeader); + drmSessionManager = self.buildDrmSessionManager(self.drmProps.getDrmUUID(), + self.drmProps.getDrmLicenseServer(), + self.drmProps.getDrmLicenseHeader()); } catch (UnsupportedDrmException e) { int errorStringId = Util.SDK_INT < 18 ? R.string.error_drm_not_supported : (e.reason == UnsupportedDrmException.REASON_UNSUPPORTED_SCHEME @@ -794,7 +794,7 @@ public class ReactExoplayerView extends FrameLayout implements return; } DrmSessionManager drmSessionManager = initializePlayerDrm(this); - if (drmSessionManager == null && drmUUID != null) { + if (drmSessionManager == null && drmProps != null && drmProps.getDrmUUID() != null) { // Failed to intialize DRM session manager - cannot continue DebugLog.e(TAG, "Failed to initialize DRM Session Manager Framework!"); eventEmitter.error("Failed to initialize DRM Session Manager Framework!", new Exception("DRM Session Manager Framework failure!"), "3003"); @@ -2229,7 +2229,7 @@ public class ReactExoplayerView extends FrameLayout implements } public void setUseTextureView(boolean useTextureView) { - boolean finallyUseTextureView = useTextureView && this.drmUUID == null; + boolean finallyUseTextureView = useTextureView && drmProps == null; exoPlayerView.setUseTextureView(finallyUseTextureView); } @@ -2256,16 +2256,11 @@ public class ReactExoplayerView extends FrameLayout implements initializePlayer(); } - public void setDrmType(UUID drmType) { - this.drmUUID = drmType; - } - - public void setDrmLicenseUrl(String licenseUrl){ - this.drmLicenseUrl = licenseUrl; - } - - public void setDrmLicenseHeader(String[] header){ - this.drmLicenseHeader = header; + public void setDrm(DRMProps drmProps) { + this.drmProps = drmProps; + if (drmProps != null && drmProps.getDrmType() != null) { + this.drmProps.setDrmUUID(Util.getDrmUuid(drmProps.getDrmType())); + } } @Override diff --git a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java index 8c34a2ac..c43349b9 100644 --- a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java +++ b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java @@ -12,6 +12,7 @@ import androidx.media3.common.util.Util; import com.brentvatne.common.api.BufferConfig; import com.brentvatne.common.api.BufferingStrategy; import com.brentvatne.common.api.ControlsConfig; +import com.brentvatne.common.api.DRMProps; import com.brentvatne.common.api.ResizeMode; import com.brentvatne.common.api.SideLoadedTextTrackList; import com.brentvatne.common.api.Source; @@ -39,9 +40,7 @@ public class ReactExoplayerViewManager extends ViewGroupManager drmKeyRequestPropertiesList = new ArrayList<>(); - for (int i = 0; i < drmHeadersArray.size(); i++) { - ReadableMap current = drmHeadersArray.getMap(i); - String key = current.hasKey("key") ? current.getString("key") : null; - String value = current.hasKey("value") ? current.getString("value") : null; - drmKeyRequestPropertiesList.add(key); - drmKeyRequestPropertiesList.add(value); - } - videoView.setDrmLicenseHeader(drmKeyRequestPropertiesList.toArray(new String[0])); - } - videoView.setUseTextureView(false); - } - } + DRMProps drmProps = DRMProps.parse(drm); + videoView.setDrm(drmProps); + videoView.setUseTextureView(false); } @ReactProp(name = PROP_SRC)