From 9449eb34f3ebcf7ac08bde1ee55e5cc26a142217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Moska=C5=82a?= <91079590+moskalakamil@users.noreply.github.com> Date: Wed, 28 May 2025 16:11:57 +0200 Subject: [PATCH] feat(android): allow plugins to override drm session manager (#4558) --- .../com/brentvatne/exoplayer/RNVExoplayerPlugin.kt | 10 ++++++++++ .../com/brentvatne/exoplayer/ReactExoplayerView.java | 5 ++++- .../com/brentvatne/react/ReactNativeVideoManager.kt | 11 +++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/brentvatne/exoplayer/RNVExoplayerPlugin.kt b/android/src/main/java/com/brentvatne/exoplayer/RNVExoplayerPlugin.kt index 0df2d1de..f338fc6c 100644 --- a/android/src/main/java/com/brentvatne/exoplayer/RNVExoplayerPlugin.kt +++ b/android/src/main/java/com/brentvatne/exoplayer/RNVExoplayerPlugin.kt @@ -3,6 +3,7 @@ package com.brentvatne.exoplayer import androidx.media3.common.MediaItem import androidx.media3.datasource.DataSource import androidx.media3.exoplayer.ExoPlayer +import androidx.media3.exoplayer.drm.DrmSessionManager import com.brentvatne.common.api.Source import com.brentvatne.react.RNVPlugin @@ -18,6 +19,15 @@ interface RNVExoplayerPlugin : RNVPlugin { */ fun getDRMManager(): DRMManagerSpec? = null + /** + * Optional function that allows the plugin to override the DrmSessionManager after it has been created. + * This is called after buildDrmSessionManager and allows for final modifications to the DrmSessionManager. + * @param source The media source being initialized. + * @param drmSessionManager The current DrmSessionManager instance. + * @return A modified DrmSessionManager if override is needed, or null to use original. + */ + fun overrideDrmSessionManager(source: Source, drmSessionManager: DrmSessionManager): DrmSessionManager? = null + /** * Optional function that allows the plugin to override the media data source factory, * which is responsible for loading media data. diff --git a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java index d192545c..f9e1a0a9 100644 --- a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -953,7 +953,10 @@ public class ReactExoplayerView extends FrameLayout implements if (drmSessionManager == null) { eventEmitter.onVideoError.invoke("Failed to build DRM session manager", new Exception("DRM session manager is null"), "3007"); } - return drmSessionManager; + + // Allow plugins to override the DrmSessionManager + DrmSessionManager overriddenManager = ReactNativeVideoManager.Companion.getInstance().overrideDrmSessionManager(source, drmSessionManager); + return overriddenManager != null ? overriddenManager : drmSessionManager; } catch (UnsupportedDrmException ex) { // Unsupported DRM exceptions are handled by the calling method throw ex; diff --git a/android/src/main/java/com/brentvatne/react/ReactNativeVideoManager.kt b/android/src/main/java/com/brentvatne/react/ReactNativeVideoManager.kt index 3053ab6a..b2c64868 100644 --- a/android/src/main/java/com/brentvatne/react/ReactNativeVideoManager.kt +++ b/android/src/main/java/com/brentvatne/react/ReactNativeVideoManager.kt @@ -2,6 +2,7 @@ package com.brentvatne.react import androidx.media3.common.MediaItem import androidx.media3.datasource.DataSource +import androidx.media3.exoplayer.drm.DrmSessionManager import com.brentvatne.common.api.Source import com.brentvatne.common.toolbox.DebugLog import com.brentvatne.exoplayer.DRMManagerSpec @@ -78,6 +79,16 @@ class ReactNativeVideoManager : RNVPlugin { // ----------------------- RNV Exoplayer plugin specific methods ----------------------- fun getDRMManager(): DRMManagerSpec? = customDRMManager + fun overrideDrmSessionManager(source: Source, drmSessionManager: DrmSessionManager): DrmSessionManager? { + for (plugin in pluginList) { + if (plugin !is RNVExoplayerPlugin) continue + + val overriddenManager = plugin.overrideDrmSessionManager(source, drmSessionManager) + if (overriddenManager != null) return overriddenManager + } + return null + } + fun overrideMediaDataSourceFactory(source: Source, mediaDataSourceFactory: DataSource.Factory): DataSource.Factory? { for (plugin in pluginList) { if (plugin !is RNVExoplayerPlugin) continue