Change PeerConnectionFactory.setVideoHwAccelerationOptions to be able to replace Egl context.

BUG=b/27222102
R=glaznev@webrtc.org

Review URL: https://codereview.webrtc.org/1707933003 .

Cr-Commit-Position: refs/heads/master@{#11666}
diff --git a/webrtc/api/java/jni/androidmediadecoder_jni.cc b/webrtc/api/java/jni/androidmediadecoder_jni.cc
index f7966cc..7c303e8 100644
--- a/webrtc/api/java/jni/androidmediadecoder_jni.cc
+++ b/webrtc/api/java/jni/androidmediadecoder_jni.cc
@@ -896,7 +896,10 @@
 void MediaCodecVideoDecoderFactory::SetEGLContext(
     JNIEnv* jni, jobject egl_context) {
   ALOGD << "MediaCodecVideoDecoderFactory::SetEGLContext";
-  RTC_DCHECK(!egl_context_);
+  if (egl_context_) {
+    jni->DeleteGlobalRef(egl_context_);
+    egl_context_ = nullptr;
+  }
   egl_context_ = jni->NewGlobalRef(egl_context);
   if (CheckException(jni)) {
     ALOGE << "error calling NewGlobalRef for EGL Context.";
diff --git a/webrtc/api/java/jni/androidmediaencoder_jni.cc b/webrtc/api/java/jni/androidmediaencoder_jni.cc
index ddfa85a..7fa93de 100644
--- a/webrtc/api/java/jni/androidmediaencoder_jni.cc
+++ b/webrtc/api/java/jni/androidmediaencoder_jni.cc
@@ -1213,7 +1213,10 @@
 void MediaCodecVideoEncoderFactory::SetEGLContext(
     JNIEnv* jni, jobject egl_context) {
   ALOGD << "MediaCodecVideoEncoderFactory::SetEGLContext";
-  RTC_DCHECK(!egl_context_);
+  if (egl_context_) {
+    jni->DeleteGlobalRef(egl_context_);
+    egl_context_ = nullptr;
+  }
   egl_context_ = jni->NewGlobalRef(egl_context);
   if (CheckException(jni)) {
     ALOGE << "error calling NewGlobalRef for EGL Context.";
diff --git a/webrtc/api/java/src/org/webrtc/PeerConnectionFactory.java b/webrtc/api/java/src/org/webrtc/PeerConnectionFactory.java
index 51e593e..17fc7ab 100644
--- a/webrtc/api/java/src/org/webrtc/PeerConnectionFactory.java
+++ b/webrtc/api/java/src/org/webrtc/PeerConnectionFactory.java
@@ -171,8 +171,13 @@
    */
   public void setVideoHwAccelerationOptions(EglBase.Context localEglContext,
       EglBase.Context remoteEglContext) {
-    if (localEglbase != null || remoteEglbase != null) {
-      throw new IllegalStateException("Egl context already set.");
+    if (localEglbase != null) {
+      Logging.w(TAG, "Egl context already set.");
+      localEglbase.release();
+    }
+    if (remoteEglbase != null) {
+      Logging.w(TAG, "Egl context already set.");
+      remoteEglbase.release();
     }
     localEglbase = EglBase.create(localEglContext);
     remoteEglbase = EglBase.create(remoteEglContext);