Chore(android): refactor drm props (#3846)

This commit is contained in:
Olivier Bouillet 2024-06-10 22:42:18 +02:00 committed by GitHub
parent 3a4a13011a
commit cfb5b1cd31
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 84 additions and 43 deletions

View File

@ -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<String> = emptyArray<String>()
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<String?>()
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<String>()
drm.drmLicenseHeader = drmKeyRequestPropertiesList.toArray(array)
}
} else {
return null
}
}
return drm
}
}
}

View File

@ -110,6 +110,7 @@ import androidx.media3.ui.LegacyPlayerControlView;
import com.brentvatne.common.api.BufferConfig; import com.brentvatne.common.api.BufferConfig;
import com.brentvatne.common.api.BufferingStrategy; import com.brentvatne.common.api.BufferingStrategy;
import com.brentvatne.common.api.ControlsConfig; import com.brentvatne.common.api.ControlsConfig;
import com.brentvatne.common.api.DRMProps;
import com.brentvatne.common.api.ResizeMode; import com.brentvatne.common.api.ResizeMode;
import com.brentvatne.common.api.SideLoadedTextTrack; import com.brentvatne.common.api.SideLoadedTextTrack;
import com.brentvatne.common.api.SideLoadedTextTrackList; import com.brentvatne.common.api.SideLoadedTextTrackList;
@ -238,9 +239,7 @@ public class ReactExoplayerView extends FrameLayout implements
private float mProgressUpdateInterval = 250.0f; private float mProgressUpdateInterval = 250.0f;
private boolean playInBackground = false; private boolean playInBackground = false;
private boolean mReportBandwidth = false; private boolean mReportBandwidth = false;
private UUID drmUUID = null; private DRMProps drmProps;
private String drmLicenseUrl = null;
private String[] drmLicenseHeader = null;
private boolean controls; private boolean controls;
private Uri adTagUrl; private Uri adTagUrl;
@ -774,10 +773,11 @@ public class ReactExoplayerView extends FrameLayout implements
private DrmSessionManager initializePlayerDrm(ReactExoplayerView self) { private DrmSessionManager initializePlayerDrm(ReactExoplayerView self) {
DrmSessionManager drmSessionManager = null; DrmSessionManager drmSessionManager = null;
if (self.drmUUID != null) { if (self.drmProps != null) {
try { try {
drmSessionManager = self.buildDrmSessionManager(self.drmUUID, self.drmLicenseUrl, drmSessionManager = self.buildDrmSessionManager(self.drmProps.getDrmUUID(),
self.drmLicenseHeader); self.drmProps.getDrmLicenseServer(),
self.drmProps.getDrmLicenseHeader());
} catch (UnsupportedDrmException e) { } catch (UnsupportedDrmException e) {
int errorStringId = Util.SDK_INT < 18 ? R.string.error_drm_not_supported int errorStringId = Util.SDK_INT < 18 ? R.string.error_drm_not_supported
: (e.reason == UnsupportedDrmException.REASON_UNSUPPORTED_SCHEME : (e.reason == UnsupportedDrmException.REASON_UNSUPPORTED_SCHEME
@ -794,7 +794,7 @@ public class ReactExoplayerView extends FrameLayout implements
return; return;
} }
DrmSessionManager drmSessionManager = initializePlayerDrm(this); 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 // Failed to intialize DRM session manager - cannot continue
DebugLog.e(TAG, "Failed to initialize DRM Session Manager Framework!"); 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"); 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) { public void setUseTextureView(boolean useTextureView) {
boolean finallyUseTextureView = useTextureView && this.drmUUID == null; boolean finallyUseTextureView = useTextureView && drmProps == null;
exoPlayerView.setUseTextureView(finallyUseTextureView); exoPlayerView.setUseTextureView(finallyUseTextureView);
} }
@ -2256,16 +2256,11 @@ public class ReactExoplayerView extends FrameLayout implements
initializePlayer(); initializePlayer();
} }
public void setDrmType(UUID drmType) { public void setDrm(DRMProps drmProps) {
this.drmUUID = drmType; this.drmProps = drmProps;
} if (drmProps != null && drmProps.getDrmType() != null) {
this.drmProps.setDrmUUID(Util.getDrmUuid(drmProps.getDrmType()));
public void setDrmLicenseUrl(String licenseUrl){ }
this.drmLicenseUrl = licenseUrl;
}
public void setDrmLicenseHeader(String[] header){
this.drmLicenseHeader = header;
} }
@Override @Override

View File

@ -12,6 +12,7 @@ import androidx.media3.common.util.Util;
import com.brentvatne.common.api.BufferConfig; import com.brentvatne.common.api.BufferConfig;
import com.brentvatne.common.api.BufferingStrategy; import com.brentvatne.common.api.BufferingStrategy;
import com.brentvatne.common.api.ControlsConfig; import com.brentvatne.common.api.ControlsConfig;
import com.brentvatne.common.api.DRMProps;
import com.brentvatne.common.api.ResizeMode; import com.brentvatne.common.api.ResizeMode;
import com.brentvatne.common.api.SideLoadedTextTrackList; import com.brentvatne.common.api.SideLoadedTextTrackList;
import com.brentvatne.common.api.Source; import com.brentvatne.common.api.Source;
@ -39,9 +40,7 @@ public class ReactExoplayerViewManager extends ViewGroupManager<ReactExoplayerVi
private static final String PROP_SRC = "src"; private static final String PROP_SRC = "src";
private static final String PROP_AD_TAG_URL = "adTagUrl"; private static final String PROP_AD_TAG_URL = "adTagUrl";
private static final String PROP_DRM = "drm"; private static final String PROP_DRM = "drm";
private static final String PROP_DRM_TYPE = "type"; private static final String PROP_SRC_HEADERS = "requestHeaders";
private static final String PROP_DRM_LICENSE_SERVER = "licenseServer";
private static final String PROP_DRM_HEADERS = "headers";
private static final String PROP_RESIZE_MODE = "resizeMode"; private static final String PROP_RESIZE_MODE = "resizeMode";
private static final String PROP_REPEAT = "repeat"; private static final String PROP_REPEAT = "repeat";
private static final String PROP_SELECTED_AUDIO_TRACK = "selectedAudioTrack"; private static final String PROP_SELECTED_AUDIO_TRACK = "selectedAudioTrack";
@ -117,28 +116,9 @@ public class ReactExoplayerViewManager extends ViewGroupManager<ReactExoplayerVi
@ReactProp(name = PROP_DRM) @ReactProp(name = PROP_DRM)
public void setDRM(final ReactExoplayerView videoView, @Nullable ReadableMap drm) { public void setDRM(final ReactExoplayerView videoView, @Nullable ReadableMap drm) {
if (drm != null && drm.hasKey(PROP_DRM_TYPE)) { DRMProps drmProps = DRMProps.parse(drm);
String drmType = ReactBridgeUtils.safeGetString(drm, PROP_DRM_TYPE); videoView.setDrm(drmProps);
String drmLicenseServer = ReactBridgeUtils.safeGetString(drm, PROP_DRM_LICENSE_SERVER); videoView.setUseTextureView(false);
ReadableArray drmHeadersArray = ReactBridgeUtils.safeGetArray(drm, PROP_DRM_HEADERS);
if (drmType != null && drmLicenseServer != null && Util.getDrmUuid(drmType) != null) {
UUID drmUUID = Util.getDrmUuid(drmType);
videoView.setDrmType(drmUUID);
videoView.setDrmLicenseUrl(drmLicenseServer);
if (drmHeadersArray != null) {
ArrayList<String> 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);
}
}
} }
@ReactProp(name = PROP_SRC) @ReactProp(name = PROP_SRC)