Move DRM init to a separate thread

This commit is contained in:
Armands Malejevs 2022-03-30 14:29:08 +03:00
parent b6f3652636
commit 88f32ae3c8

View File

@ -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;
@ -547,10 +548,46 @@ class ReactExoplayerView extends FrameLayout implements
PlaybackParameters params = new PlaybackParameters(rate, 1f);
player.setPlaybackParameters(params);
}
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
// DRM initialization must run on a different thread
initializePlayerDrm();
// 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();
}
});
}
});
} else {
finishPlayerInitialization();
}
} 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 initializePlayerDrm() {
DrmSessionManager drmSessionManager = null;
if (self.drmUUID != null) {
try {
@ -568,8 +605,9 @@ class ReactExoplayerView extends FrameLayout implements
// DRM Session Manager failed to instantiate, no other work can be done - error has been handled by the instantiating method
return;
}
// End DRM
}
private void initializePlayerSource() {
ArrayList<MediaSource> mediaSourceList = buildTextSources();
MediaSource videoSource = buildMediaSource(srcUri, extension, drmSessionManager);
MediaSource mediaSource;
@ -593,23 +631,16 @@ 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, int retryCount) throws UnsupportedDrmException {