VEX-6779: React Native Video buildDrmSessionManager crash (#23)
Move DRM initialization logic into a separate thread to prevent ANRs.
This commit is contained in:
commit
4d17096630
@ -7,6 +7,7 @@ import android.content.Context;
|
||||
import android.media.AudioManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.Message;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
@ -500,6 +501,9 @@ class ReactExoplayerView extends FrameLayout implements
|
||||
}
|
||||
|
||||
private void stopBufferCheckTimer() {
|
||||
if (this.bufferCheckTimer == null) {
|
||||
return;
|
||||
}
|
||||
this.bufferCheckTimer.cancel();
|
||||
this.bufferCheckTimer = null;
|
||||
}
|
||||
@ -512,6 +516,50 @@ class ReactExoplayerView extends FrameLayout implements
|
||||
public void run() {
|
||||
try {
|
||||
if (player == null) {
|
||||
// Initialize core configuration and listeners
|
||||
initializePlayerCore(self);
|
||||
}
|
||||
|
||||
if (playerNeedsSource && srcUri != null) {
|
||||
exoPlayerView.invalidateAspectRatio();
|
||||
// DRM session manager creation must be done on a different thread to prevent crashes so we start a new thread
|
||||
ExecutorService es = Executors.newSingleThreadExecutor();
|
||||
es.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// DRM initialization must run on a different thread
|
||||
DrmSessionManager drmSessionManager = initializePlayerDrm(self);
|
||||
if (drmSessionManager == null) {
|
||||
// Failed to intialize DRM session manager - cannot continue
|
||||
return;
|
||||
}
|
||||
// Initialize handler to run on the main thread
|
||||
new Handler(Looper.getMainLooper()).post(new Runnable () {
|
||||
@Override
|
||||
public void run () {
|
||||
// Source initialization must run on the main thread
|
||||
initializePlayerSource(self, drmSessionManager);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
} else if (srcUri != null) {
|
||||
initializePlayerSource(self, null);
|
||||
}
|
||||
|
||||
|
||||
} catch (Exception ex) {
|
||||
self.playerNeedsSource = true;
|
||||
Log.e("ExoPlayer Exception", "Failed to initialize Player!");
|
||||
Log.e("ExoPlayer Exception", ex.toString());
|
||||
eventEmitter.error(ex.toString(), ex, "1001");
|
||||
}
|
||||
}
|
||||
}, 1);
|
||||
|
||||
}
|
||||
|
||||
private void initializePlayerCore(ReactExoplayerView self) {
|
||||
ExoTrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory();
|
||||
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
|
||||
trackSelector.setParameters(trackSelector.buildUponParameters()
|
||||
@ -548,25 +596,25 @@ class ReactExoplayerView extends FrameLayout implements
|
||||
PlaybackParameters params = new PlaybackParameters(rate, 1f);
|
||||
player.setPlaybackParameters(params);
|
||||
}
|
||||
if (playerNeedsSource && srcUri != null) {
|
||||
exoPlayerView.invalidateAspectRatio();
|
||||
|
||||
// DRM
|
||||
private DrmSessionManager initializePlayerDrm(ReactExoplayerView self) {
|
||||
DrmSessionManager drmSessionManager = null;
|
||||
if (self.drmUUID != null) {
|
||||
try {
|
||||
drmSessionManager = buildDrmSessionManager(self.drmUUID, self.drmLicenseUrl,
|
||||
drmSessionManager = self.buildDrmSessionManager(self.drmUUID, self.drmLicenseUrl,
|
||||
self.drmLicenseHeader);
|
||||
} catch (UnsupportedDrmException e) {
|
||||
int errorStringId = Util.SDK_INT < 18 ? R.string.error_drm_not_supported
|
||||
: (e.reason == UnsupportedDrmException.REASON_UNSUPPORTED_SCHEME
|
||||
? R.string.error_drm_unsupported_scheme : R.string.error_drm_unknown);
|
||||
eventEmitter.error(getResources().getString(errorStringId), e, "3003");
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
// End DRM
|
||||
return drmSessionManager;
|
||||
}
|
||||
|
||||
private void initializePlayerSource(ReactExoplayerView self, DrmSessionManager drmSessionManager) {
|
||||
ArrayList<MediaSource> mediaSourceList = buildTextSources();
|
||||
MediaSource videoSource = buildMediaSource(srcUri, extension, drmSessionManager);
|
||||
MediaSource mediaSource;
|
||||
@ -590,36 +638,32 @@ class ReactExoplayerView extends FrameLayout implements
|
||||
reLayout(exoPlayerView);
|
||||
eventEmitter.loadStart();
|
||||
loadVideoStarted = true;
|
||||
|
||||
finishPlayerInitialization();
|
||||
}
|
||||
|
||||
private void finishPlayerInitialization() {
|
||||
// Initializing the playerControlView
|
||||
initializePlayerControl();
|
||||
setControls(controls);
|
||||
applyModifiers();
|
||||
startBufferCheckTimer();
|
||||
|
||||
} catch (Exception ex) {
|
||||
self.playerNeedsSource = true;
|
||||
Log.e("ExoPlayer Exception", "Failed to initialize Player!");
|
||||
Log.e("ExoPlayer Exception", ex.toString());
|
||||
eventEmitter.error(ex.toString(), ex, "1001");
|
||||
}
|
||||
}
|
||||
}, 1);
|
||||
|
||||
}
|
||||
|
||||
private DrmSessionManager buildDrmSessionManager(UUID uuid,
|
||||
String licenseUrl, String[] keyRequestPropertiesArray) throws UnsupportedDrmException {
|
||||
private DrmSessionManager buildDrmSessionManager(UUID uuid, String licenseUrl, String[] keyRequestPropertiesArray) throws UnsupportedDrmException {
|
||||
return buildDrmSessionManager(uuid, licenseUrl, keyRequestPropertiesArray, 0);
|
||||
}
|
||||
|
||||
private DrmSessionManager buildDrmSessionManager(UUID uuid, String licenseUrl, String[] keyRequestPropertiesArray, int retryCount) throws UnsupportedDrmException {
|
||||
if (Util.SDK_INT < 18) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
HttpMediaDrmCallback drmCallback = new HttpMediaDrmCallback(licenseUrl,
|
||||
buildHttpDataSourceFactory(false));
|
||||
if (keyRequestPropertiesArray != null) {
|
||||
for (int i = 0; i < keyRequestPropertiesArray.length - 1; i += 2) {
|
||||
drmCallback.setKeyRequestProperty(keyRequestPropertiesArray[i],
|
||||
keyRequestPropertiesArray[i + 1]);
|
||||
drmCallback.setKeyRequestProperty(keyRequestPropertiesArray[i], keyRequestPropertiesArray[i + 1]);
|
||||
}
|
||||
}
|
||||
FrameworkMediaDrm mediaDrm = FrameworkMediaDrm.newInstance(uuid);
|
||||
@ -627,8 +671,19 @@ class ReactExoplayerView extends FrameLayout implements
|
||||
// When DRM fails using L1 we want to switch to L3
|
||||
mediaDrm.setPropertyString("securityLevel", "L3");
|
||||
}
|
||||
return new DefaultDrmSessionManager(uuid,
|
||||
mediaDrm, drmCallback, null, false, 3);
|
||||
return new DefaultDrmSessionManager(uuid, mediaDrm, drmCallback, null, false, 3);
|
||||
} catch(UnsupportedDrmException ex) {
|
||||
// Unsupported DRM exceptions are handled by the calling method
|
||||
throw ex;
|
||||
} catch (Exception ex) {
|
||||
if (retryCount < 3) {
|
||||
// Attempt retry 3 times in case where the OS Media DRM Framework fails for whatever reason
|
||||
return buildDrmSessionManager(uuid, licenseUrl, keyRequestPropertiesArray, ++retryCount);
|
||||
}
|
||||
// Handle the unknow exception and emit to JS
|
||||
eventEmitter.error(ex.toString(), ex, "3006");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private MediaSource buildMediaSource(Uri uri, String overrideExtension, DrmSessionManager drmSessionManager) {
|
||||
|
Loading…
Reference in New Issue
Block a user