This cl change so that we use EGL14 where it is supported and EGL10 otherwise. The idea is to make this agnostic to an application and for WebRTC except in EGLBase.

The reason we want to use EGL14 is to be able to use EGLExt.eglPresentationTimeANDROID when writing textures to MediaEncoder.

BUG=webrtc:4993
TBR=glaznew@webrtc.org

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

Cr-Commit-Position: refs/heads/master@{#10864}
diff --git a/talk/app/webrtc/java/jni/androidmediadecoder_jni.cc b/talk/app/webrtc/java/jni/androidmediadecoder_jni.cc
index 4554e7b..fa11196 100644
--- a/talk/app/webrtc/java/jni/androidmediadecoder_jni.cc
+++ b/talk/app/webrtc/java/jni/androidmediadecoder_jni.cc
@@ -858,7 +858,7 @@
       render_egl_context_ = NULL;
     } else {
       jclass j_egl_context_class =
-          FindClass(jni, "javax/microedition/khronos/egl/EGLContext");
+          FindClass(jni, "org/webrtc/EglBase$Context");
       if (!jni->IsInstanceOf(render_egl_context_, j_egl_context_class)) {
         ALOGE << "Wrong EGL Context.";
         jni->DeleteGlobalRef(render_egl_context_);
diff --git a/talk/app/webrtc/java/jni/androidmediaencoder_jni.cc b/talk/app/webrtc/java/jni/androidmediaencoder_jni.cc
index 37eeb6c..92ec4f0 100644
--- a/talk/app/webrtc/java/jni/androidmediaencoder_jni.cc
+++ b/talk/app/webrtc/java/jni/androidmediaencoder_jni.cc
@@ -278,7 +278,7 @@
       *j_media_codec_video_encoder_class_,
       "initEncode",
       "(Lorg/webrtc/MediaCodecVideoEncoder$VideoCodecType;"
-      "IIIILjavax/microedition/khronos/egl/EGLContext;)Z");
+      "IIIILorg/webrtc/EglBase$Context;)Z");
   j_get_input_buffers_method_ = GetMethodID(
       jni,
       *j_media_codec_video_encoder_class_,
@@ -1123,7 +1123,7 @@
       egl_context_ = NULL;
     } else {
       jclass j_egl_context_class =
-          FindClass(jni, "javax/microedition/khronos/egl/EGLContext");
+          FindClass(jni, "org/webrtc/EglBase$Context");
       if (!jni->IsInstanceOf(egl_context_, j_egl_context_class)) {
         ALOGE << "Wrong EGL Context.";
         jni->DeleteGlobalRef(egl_context_);
diff --git a/talk/app/webrtc/java/jni/classreferenceholder.cc b/talk/app/webrtc/java/jni/classreferenceholder.cc
index 13883be..ea56c25 100644
--- a/talk/app/webrtc/java/jni/classreferenceholder.cc
+++ b/talk/app/webrtc/java/jni/classreferenceholder.cc
@@ -79,6 +79,7 @@
   LoadClass(jni, "org/webrtc/VideoCapturerAndroid");
   LoadClass(jni, "org/webrtc/VideoCapturerAndroid$NativeObserver");
   LoadClass(jni, "org/webrtc/EglBase");
+  LoadClass(jni, "org/webrtc/EglBase$Context");
   LoadClass(jni, "org/webrtc/NetworkMonitor");
   LoadClass(jni, "org/webrtc/MediaCodecVideoEncoder");
   LoadClass(jni, "org/webrtc/MediaCodecVideoEncoder$OutputBufferInfo");
diff --git a/talk/app/webrtc/java/jni/peerconnection_jni.cc b/talk/app/webrtc/java/jni/peerconnection_jni.cc
index 194f9ff..560b61c 100644
--- a/talk/app/webrtc/java/jni/peerconnection_jni.cc
+++ b/talk/app/webrtc/java/jni/peerconnection_jni.cc
@@ -1338,12 +1338,40 @@
   OwnedFactoryAndThreads* owned_factory =
       reinterpret_cast<OwnedFactoryAndThreads*>(native_factory);
 
+  // TODO(perkj): In order to not break existing applications we need to
+  // check if |local_egl_context| or |remote_egl_context| is an
+  // EGL10 context. If so, create an EGLBase10.EGL10Context instead.
+  // Remove this once existing applications has been updated.
+  jobject local_eglbase_context = local_egl_context;
+  jobject remote_eglbase_context = remote_egl_context;
+
+  jclass j_egl10_context_class =
+      FindClass(jni, "javax/microedition/khronos/egl/EGLContext");
+  jclass j_eglbase_context_class =
+      FindClass(jni, "org/webrtc/EglBase$Context");
+
+  jmethodID j_eglbase_context_ctor = GetMethodID(
+      jni, j_eglbase_context_class,
+      "<init>", "(Ljavax/microedition/khronos/egl/EGLContext;)V");
+  if (local_egl_context != nullptr &&
+      jni->IsInstanceOf(local_egl_context, j_egl10_context_class)) {
+    local_eglbase_context = jni->NewObject(
+        j_eglbase_context_class, j_eglbase_context_ctor,
+        local_egl_context);
+  }
+  if (remote_egl_context != nullptr &&
+      jni->IsInstanceOf(remote_egl_context, j_egl10_context_class)) {
+    remote_eglbase_context = jni->NewObject(
+        j_eglbase_context_class, j_eglbase_context_ctor,
+        remote_egl_context);
+  }
+
   MediaCodecVideoEncoderFactory* encoder_factory =
       static_cast<MediaCodecVideoEncoderFactory*>
           (owned_factory->encoder_factory());
   if (encoder_factory) {
     LOG(LS_INFO) << "Set EGL context for HW encoding.";
-    encoder_factory->SetEGLContext(jni, local_egl_context);
+    encoder_factory->SetEGLContext(jni, local_eglbase_context);
   }
 
   MediaCodecVideoDecoderFactory* decoder_factory =
@@ -1351,7 +1379,7 @@
           (owned_factory->decoder_factory());
   if (decoder_factory) {
     LOG(LS_INFO) << "Set EGL context for HW decoding.";
-    decoder_factory->SetEGLContext(jni, remote_egl_context);
+    decoder_factory->SetEGLContext(jni, remote_eglbase_context);
   }
 #endif
 }
diff --git a/talk/app/webrtc/java/jni/surfacetexturehelper_jni.cc b/talk/app/webrtc/java/jni/surfacetexturehelper_jni.cc
index 65c1737..8fd42a0 100644
--- a/talk/app/webrtc/java/jni/surfacetexturehelper_jni.cc
+++ b/talk/app/webrtc/java/jni/surfacetexturehelper_jni.cc
@@ -47,7 +47,7 @@
               GetStaticMethodID(jni,
                                 *j_surface_texture_helper_class_,
                                 "create",
-                                "(Ljavax/microedition/khronos/egl/EGLContext;)"
+                                "(Lorg/webrtc/EglBase$Context;)"
                                 "Lorg/webrtc/SurfaceTextureHelper;"),
               egl_shared_context)),
       j_return_texture_method_(GetMethodID(jni,