Android: Remove use of EGLContexts in PeerConnectionFactory

Since the legacy video codecs seem to be around for some time more, we
need to make them injectable and provide a migration path for clients
that still use them so that we can clean up PeerConnectionFactory.

This CL moves the creation of EglContexts into the legacy codec
factories. Clients can then migrate to setEGLContext() instead of using
setVideoHwAccelerationOptions().

Bug: webrtc:9502
Change-Id: I608607b32db73ce3df7704a061e66d9d53946af5
Reviewed-on: https://webrtc-review.googlesource.com/87941
Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
Commit-Queue: Magnus Jedvert <magjed@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23934}
diff --git a/sdk/android/api/org/webrtc/MediaCodecVideoDecoder.java b/sdk/android/api/org/webrtc/MediaCodecVideoDecoder.java
index f0ec00e..fc72189 100644
--- a/sdk/android/api/org/webrtc/MediaCodecVideoDecoder.java
+++ b/sdk/android/api/org/webrtc/MediaCodecVideoDecoder.java
@@ -36,6 +36,7 @@
 // Java-side of peerconnection.cc:MediaCodecVideoDecoder.
 // This class is an implementation detail of the Java PeerConnection API.
 @SuppressWarnings("deprecation")
+@Deprecated
 public class MediaCodecVideoDecoder {
   // This class is constructed, operated, and destroyed by its C++ incarnation,
   // so the class and its methods have non-public visibility.  The API this
@@ -79,6 +80,7 @@
   private static int codecErrors = 0;
   // List of disabled codec types - can be set from application.
   private static Set<String> hwDecoderDisabledTypes = new HashSet<String>();
+  @Nullable private static EglBase eglBase;
 
   @Nullable private Thread mediaCodecThread;
   @Nullable private MediaCodec mediaCodec;
@@ -141,7 +143,6 @@
   private int sliceHeight;
   private boolean hasDecodedFirstFrame;
   private final Queue<TimeStamps> decodeStartTimeMs = new ArrayDeque<TimeStamps>();
-  private boolean useSurface;
 
   // The below variables are only used when decoding to a Surface.
   @Nullable private TextureListener textureListener;
@@ -157,6 +158,28 @@
     void onMediaCodecVideoDecoderCriticalError(int codecErrors);
   }
 
+  /** Set EGL context used by HW decoding. The EGL context must be shared with the remote render. */
+  public static void setEglContext(EglBase.Context eglContext) {
+    if (eglBase != null) {
+      Logging.w(TAG, "Egl context already set.");
+      eglBase.release();
+    }
+    eglBase = EglBase.create(eglContext);
+  }
+
+  /** Dispose the EGL context used by HW decoding. */
+  public static void disposeEglContext() {
+    if (eglBase != null) {
+      eglBase.release();
+      eglBase = null;
+    }
+  }
+
+  @CalledByNative
+  static boolean useSurface() {
+    return eglBase != null;
+  }
+
   public static void setErrorCallback(MediaCodecVideoDecoderErrorCallback errorCallback) {
     Logging.d(TAG, "Set error callback");
     MediaCodecVideoDecoder.errorCallback = errorCallback;
@@ -323,16 +346,13 @@
     }
   }
 
-  // Pass null in |eglContext| to configure the codec for ByteBuffer output.
   @CalledByNativeUnchecked
-  private boolean initDecode(
-      VideoCodecType type, int width, int height, @Nullable EglBase.Context eglContext) {
+  private boolean initDecode(VideoCodecType type, int width, int height) {
     if (mediaCodecThread != null) {
       throw new RuntimeException("initDecode: Forgot to release()?");
     }
 
     String mime = null;
-    useSurface = (eglContext != null);
     String[] supportedCodecPrefixes = null;
     if (type == VideoCodecType.VIDEO_CODEC_VP8) {
       mime = VP8_MIME_TYPE;
@@ -351,8 +371,9 @@
       throw new RuntimeException("Cannot find HW decoder for " + type);
     }
 
-    Logging.d(TAG, "Java initDecode: " + type + " : " + width + " x " + height + ". Color: 0x"
-            + Integer.toHexString(properties.colorFormat) + ". Use Surface: " + useSurface);
+    Logging.d(TAG,
+        "Java initDecode: " + type + " : " + width + " x " + height + ". Color: 0x"
+            + Integer.toHexString(properties.colorFormat) + ". Use Surface: " + useSurface());
 
     runningInstance = this; // Decoder is now running and can be queried for stack traces.
     mediaCodecThread = Thread.currentThread();
@@ -362,10 +383,10 @@
       stride = width;
       sliceHeight = height;
 
-      if (useSurface) {
+      if (useSurface()) {
         @Nullable
-        final SurfaceTextureHelper surfaceTextureHelper =
-            SurfaceTextureHelper.create("Decoder SurfaceTextureHelper", eglContext);
+        final SurfaceTextureHelper surfaceTextureHelper = SurfaceTextureHelper.create(
+            "Decoder SurfaceTextureHelper", eglBase.getEglBaseContext());
         if (surfaceTextureHelper != null) {
           textureListener = new TextureListener(surfaceTextureHelper);
           textureListener.setSize(width, height);
@@ -374,7 +395,7 @@
       }
 
       MediaFormat format = MediaFormat.createVideoFormat(mime, width, height);
-      if (!useSurface) {
+      if (!useSurface()) {
         format.setInteger(MediaFormat.KEY_COLOR_FORMAT, properties.colorFormat);
       }
       Logging.d(TAG, "  Format: " + format);
@@ -461,7 +482,7 @@
     mediaCodec = null;
     mediaCodecThread = null;
     runningInstance = null;
-    if (useSurface) {
+    if (useSurface()) {
       surface.release();
       surface = null;
       textureListener.release();
@@ -773,7 +794,7 @@
             textureListener.setSize(width, height);
           }
 
-          if (!useSurface && format.containsKey(MediaFormat.KEY_COLOR_FORMAT)) {
+          if (!useSurface() && format.containsKey(MediaFormat.KEY_COLOR_FORMAT)) {
             colorFormat = format.getInteger(MediaFormat.KEY_COLOR_FORMAT);
             Logging.d(TAG, "Color: 0x" + Integer.toHexString(colorFormat));
             if (!supportedColorList.contains(colorFormat)) {
@@ -817,7 +838,7 @@
   @CalledByNativeUnchecked
   private @Nullable DecodedTextureBuffer dequeueTextureBuffer(int dequeueTimeoutMs) {
     checkOnMediaCodecThread();
-    if (!useSurface) {
+    if (!useSurface()) {
       throw new IllegalStateException("dequeueTexture() called for byte buffer decoding.");
     }
     DecodedOutputBuffer outputBuffer = dequeueOutputBuffer(dequeueTimeoutMs);
@@ -881,7 +902,7 @@
   private void returnDecodedOutputBuffer(int index)
       throws IllegalStateException, MediaCodec.CodecException {
     checkOnMediaCodecThread();
-    if (useSurface) {
+    if (useSurface()) {
       throw new IllegalStateException("returnDecodedOutputBuffer() called for surface decoding.");
     }
     mediaCodec.releaseOutputBuffer(index, false /* render */);