Android: Make VideoCapturer an interface for all VideoCapturers to implement

This CL factors out the interface that AndroidVideoCapturerJni is using to communicate with the Java counterpart. This interface is moved into VideoCapturer. The interface is not touched in this CL, and a follow-up CL is planned to simplify and improve it.

Another change is that the native part of VideoCapturer is created in PeerConnectionFactory.createVideoSource() instead of doing it immediately in the ctor.

BUG=webrtc:5519
R=perkj@webrtc.org

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

Cr-Commit-Position: refs/heads/master@{#11606}
diff --git a/webrtc/api/java/jni/androidvideocapturer_jni.cc b/webrtc/api/java/jni/androidvideocapturer_jni.cc
index 98dfd63..8588dc2 100644
--- a/webrtc/api/java/jni/androidvideocapturer_jni.cc
+++ b/webrtc/api/java/jni/androidvideocapturer_jni.cc
@@ -36,11 +36,11 @@
     jobject j_surface_texture_helper)
     : j_video_capturer_(jni, j_video_capturer),
       j_video_capturer_class_(
-          jni, FindClass(jni, "org/webrtc/VideoCapturerAndroid")),
+          jni, FindClass(jni, "org/webrtc/VideoCapturer")),
       j_observer_class_(
           jni,
           FindClass(jni,
-                    "org/webrtc/VideoCapturerAndroid$NativeObserver")),
+                    "org/webrtc/VideoCapturer$NativeObserver")),
       surface_texture_helper_(new rtc::RefCountedObject<SurfaceTextureHelper>(
           jni, j_surface_texture_helper)),
       capturer_(nullptr) {
@@ -52,8 +52,8 @@
   LOG(LS_INFO) << "AndroidVideoCapturerJni dtor";
   jni()->CallVoidMethod(
       *j_video_capturer_,
-      GetMethodID(jni(), *j_video_capturer_class_, "release", "()V"));
-  CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.release()";
+      GetMethodID(jni(), *j_video_capturer_class_, "dispose", "()V"));
+  CHECK_EXCEPTION(jni()) << "error during VideoCapturer.dispose()";
 }
 
 void AndroidVideoCapturerJni::Start(int width, int height, int framerate,
@@ -76,13 +76,13 @@
   jmethodID m = GetMethodID(
       jni(), *j_video_capturer_class_, "startCapture",
       "(IIILandroid/content/Context;"
-      "Lorg/webrtc/VideoCapturerAndroid$CapturerObserver;)V");
+      "Lorg/webrtc/VideoCapturer$CapturerObserver;)V");
   jni()->CallVoidMethod(*j_video_capturer_,
                         m, width, height,
                         framerate,
                         application_context_,
                         j_frame_observer);
-  CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.startCapture";
+  CHECK_EXCEPTION(jni()) << "error during VideoCapturer.startCapture";
 }
 
 void AndroidVideoCapturerJni::Stop() {
@@ -97,7 +97,7 @@
   jmethodID m = GetMethodID(jni(), *j_video_capturer_class_,
                             "stopCapture", "()V");
   jni()->CallVoidMethod(*j_video_capturer_, m);
-  CHECK_EXCEPTION(jni()) << "error during VideoCapturerAndroid.stopCapture";
+  CHECK_EXCEPTION(jni()) << "error during VideoCapturer.stopCapture";
   LOG(LS_INFO) << "AndroidVideoCapturerJni stop done";
 }
 
@@ -178,7 +178,7 @@
 JNIEnv* AndroidVideoCapturerJni::jni() { return AttachCurrentThreadIfNeeded(); }
 
 JOW(void,
-    VideoCapturerAndroid_00024NativeObserver_nativeOnByteBufferFrameCaptured)
+    VideoCapturer_00024NativeObserver_nativeOnByteBufferFrameCaptured)
     (JNIEnv* jni, jclass, jlong j_capturer, jbyteArray j_frame, jint length,
         jint width, jint height, jint rotation, jlong timestamp) {
   jboolean is_copy = true;
@@ -188,7 +188,7 @@
   jni->ReleaseByteArrayElements(j_frame, bytes, JNI_ABORT);
 }
 
-JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnTextureFrameCaptured)
+JOW(void, VideoCapturer_00024NativeObserver_nativeOnTextureFrameCaptured)
     (JNIEnv* jni, jclass, jlong j_capturer, jint j_width, jint j_height,
         jint j_oes_texture_id, jfloatArray j_transform_matrix,
         jint j_rotation, jlong j_timestamp) {
@@ -198,14 +198,14 @@
                                            j_transform_matrix));
 }
 
-JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeCapturerStarted)
+JOW(void, VideoCapturer_00024NativeObserver_nativeCapturerStarted)
     (JNIEnv* jni, jclass, jlong j_capturer, jboolean j_success) {
   LOG(LS_INFO) << "NativeObserver_nativeCapturerStarted";
   reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnCapturerStarted(
       j_success);
 }
 
-JOW(void, VideoCapturerAndroid_00024NativeObserver_nativeOnOutputFormatRequest)
+JOW(void, VideoCapturer_00024NativeObserver_nativeOnOutputFormatRequest)
     (JNIEnv* jni, jclass, jlong j_capturer, jint j_width, jint j_height,
         jint j_fps) {
   LOG(LS_INFO) << "NativeObserver_nativeOnOutputFormatRequest";
@@ -213,16 +213,4 @@
       j_width, j_height, j_fps);
 }
 
-JOW(jlong, VideoCapturerAndroid_nativeCreateVideoCapturer)
-    (JNIEnv* jni, jclass,
-     jobject j_video_capturer, jobject j_surface_texture_helper) {
-  rtc::scoped_refptr<webrtc::AndroidVideoCapturerDelegate> delegate =
-      new rtc::RefCountedObject<AndroidVideoCapturerJni>(
-          jni, j_video_capturer, j_surface_texture_helper);
-  rtc::scoped_ptr<cricket::VideoCapturer> capturer(
-      new webrtc::AndroidVideoCapturer(delegate));
-  // Caller takes ownership of the cricket::VideoCapturer* pointer.
-  return jlongFromPointer(capturer.release());
-}
-
 }  // namespace webrtc_jni
diff --git a/webrtc/api/java/jni/androidvideocapturer_jni.h b/webrtc/api/java/jni/androidvideocapturer_jni.h
index 16210b1..9b3bb4f 100644
--- a/webrtc/api/java/jni/androidvideocapturer_jni.h
+++ b/webrtc/api/java/jni/androidvideocapturer_jni.h
@@ -42,7 +42,7 @@
 
   std::string GetSupportedFormats() override;
 
-  // Called from VideoCapturerAndroid::NativeObserver on a Java thread.
+  // Called from VideoCapturer::NativeObserver on a Java thread.
   void OnCapturerStarted(bool success);
   void OnMemoryBufferFrame(void* video_frame, int length, int width,
                            int height, int rotation, int64_t timestamp_ns);
diff --git a/webrtc/api/java/jni/classreferenceholder.cc b/webrtc/api/java/jni/classreferenceholder.cc
index 2f17db3..d6c7d56 100644
--- a/webrtc/api/java/jni/classreferenceholder.cc
+++ b/webrtc/api/java/jni/classreferenceholder.cc
@@ -90,8 +90,8 @@
   LoadClass(jni, "org/webrtc/StatsReport");
   LoadClass(jni, "org/webrtc/StatsReport$Value");
   LoadClass(jni, "org/webrtc/SurfaceTextureHelper");
-  LoadClass(jni, "org/webrtc/VideoCapturerAndroid");
-  LoadClass(jni, "org/webrtc/VideoCapturerAndroid$NativeObserver");
+  LoadClass(jni, "org/webrtc/VideoCapturer");
+  LoadClass(jni, "org/webrtc/VideoCapturer$NativeObserver");
   LoadClass(jni, "org/webrtc/VideoRenderer$I420Frame");
   LoadClass(jni, "org/webrtc/VideoTrack");
 }
diff --git a/webrtc/api/java/jni/peerconnection_jni.cc b/webrtc/api/java/jni/peerconnection_jni.cc
index 3997805..f7b9e90 100644
--- a/webrtc/api/java/jni/peerconnection_jni.cc
+++ b/webrtc/api/java/jni/peerconnection_jni.cc
@@ -910,10 +910,6 @@
   CHECK_RELEASE(reinterpret_cast<MediaSourceInterface*>(j_p));
 }
 
-JOW(void, VideoCapturer_free)(JNIEnv*, jclass, jlong j_p) {
-  delete reinterpret_cast<cricket::VideoCapturer*>(j_p);
-}
-
 JOW(void, VideoRenderer_freeWrappedVideoRenderer)(JNIEnv*, jclass, jlong j_p) {
   delete reinterpret_cast<JavaVideoRendererWrapper*>(j_p);
 }
@@ -1216,16 +1212,27 @@
 }
 
 JOW(jlong, PeerConnectionFactory_nativeCreateVideoSource)(
-    JNIEnv* jni, jclass, jlong native_factory, jlong native_capturer,
+    JNIEnv* jni, jclass, jlong native_factory, jobject j_video_capturer,
     jobject j_constraints) {
+  // Create a cricket::VideoCapturer from |j_video_capturer|.
+  jobject j_surface_texture_helper = jni->CallObjectMethod(
+      j_video_capturer,
+      GetMethodID(jni, FindClass(jni, "org/webrtc/VideoCapturer"),
+                  "getSurfaceTextureHelper",
+                  "()Lorg/webrtc/SurfaceTextureHelper;"));
+  rtc::scoped_refptr<webrtc::AndroidVideoCapturerDelegate> delegate =
+      new rtc::RefCountedObject<AndroidVideoCapturerJni>(
+          jni, j_video_capturer, j_surface_texture_helper);
+  rtc::scoped_ptr<cricket::VideoCapturer> capturer(
+      new webrtc::AndroidVideoCapturer(delegate));
+  // Create a webrtc::VideoSourceInterface from the cricket::VideoCapturer,
+  // native factory and constraints.
   scoped_ptr<ConstraintsWrapper> constraints(
       new ConstraintsWrapper(jni, j_constraints));
   rtc::scoped_refptr<PeerConnectionFactoryInterface> factory(
       factoryFromJava(native_factory));
   rtc::scoped_refptr<VideoSourceInterface> source(
-      factory->CreateVideoSource(
-          reinterpret_cast<cricket::VideoCapturer*>(native_capturer),
-          constraints.get()));
+      factory->CreateVideoSource(capturer.release(), constraints.get()));
   return (jlong)source.release();
 }