Android: Generate JNI code for androidvideotracksource

Bug: webrtc:8278
Change-Id: I43b53c68ebaf2c3f9c27ea7ef510d7b016c1df93
Reviewed-on: https://webrtc-review.googlesource.com/23243
Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
Commit-Queue: Magnus Jedvert <magjed@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#20795}
diff --git a/sdk/android/api/org/webrtc/VideoFrame.java b/sdk/android/api/org/webrtc/VideoFrame.java
index 0a5a67e..62d4cf2 100644
--- a/sdk/android/api/org/webrtc/VideoFrame.java
+++ b/sdk/android/api/org/webrtc/VideoFrame.java
@@ -51,6 +51,7 @@
      * Crops a region defined by |cropx|, |cropY|, |cropWidth| and |cropHeight|. Scales it to size
      * |scaleWidth| x |scaleHeight|.
      */
+    @CalledByNative("Buffer")
     Buffer cropAndScale(
         int cropX, int cropY, int cropWidth, int cropHeight, int scaleWidth, int scaleHeight);
   }
diff --git a/sdk/android/src/jni/androidvideotracksource.cc b/sdk/android/src/jni/androidvideotracksource.cc
index ba31f7e..42430c2 100644
--- a/sdk/android/src/jni/androidvideotracksource.cc
+++ b/sdk/android/src/jni/androidvideotracksource.cc
@@ -36,12 +36,6 @@
       is_screencast_(is_screencast) {
   RTC_LOG(LS_INFO) << "AndroidVideoTrackSource ctor";
   camera_thread_checker_.DetachFromThread();
-
-  jclass j_video_frame_buffer_class =
-      FindClass(jni, "org/webrtc/VideoFrame$Buffer");
-  j_crop_and_scale_id_ =
-      jni->GetMethodID(j_video_frame_buffer_class, "cropAndScale",
-                       "(IIIIII)Lorg/webrtc/VideoFrame$Buffer;");
 }
 
 void AndroidVideoTrackSource::SetState(SourceState state) {
@@ -183,12 +177,10 @@
     return;
   }
 
-  jobject j_adapted_video_frame_buffer = jni->CallObjectMethod(
-      j_video_frame_buffer, j_crop_and_scale_id_, crop_x, crop_y, crop_width,
-      crop_height, adapted_width, adapted_height);
-
   rtc::scoped_refptr<VideoFrameBuffer> buffer =
-      AndroidVideoBuffer::Adopt(jni, j_adapted_video_frame_buffer);
+      AndroidVideoBuffer::Create(jni, j_video_frame_buffer)
+          ->CropAndScale(jni, crop_x, crop_y, crop_width, crop_height,
+                         adapted_width, adapted_height);
 
   // AdaptedVideoTrackSource handles applying rotation for I420 frames.
   if (apply_rotation() && rotation != kVideoRotation_0) {
diff --git a/sdk/android/src/jni/androidvideotracksource.h b/sdk/android/src/jni/androidvideotracksource.h
index 641c10f..8836c11 100644
--- a/sdk/android/src/jni/androidvideotracksource.h
+++ b/sdk/android/src/jni/androidvideotracksource.h
@@ -85,8 +85,6 @@
   I420BufferPool buffer_pool_;
   rtc::scoped_refptr<SurfaceTextureHelper> surface_texture_helper_;
   const bool is_screencast_;
-
-  jmethodID j_crop_and_scale_id_;
 };
 
 }  // namespace jni
diff --git a/sdk/android/src/jni/videoframe.cc b/sdk/android/src/jni/videoframe.cc
index 5e24002..c914727 100644
--- a/sdk/android/src/jni/videoframe.cc
+++ b/sdk/android/src/jni/videoframe.cc
@@ -311,6 +311,19 @@
   return *j_video_frame_buffer_;
 }
 
+rtc::scoped_refptr<AndroidVideoBuffer> AndroidVideoBuffer::CropAndScale(
+    JNIEnv* jni,
+    int crop_x,
+    int crop_y,
+    int crop_width,
+    int crop_height,
+    int scale_width,
+    int scale_height) {
+  return Adopt(jni, Java_Buffer_cropAndScale(
+                        jni, *j_video_frame_buffer_, crop_x, crop_y, crop_width,
+                        crop_height, scale_width, scale_height));
+}
+
 VideoFrameBuffer::Type AndroidVideoBuffer::type() const {
   return Type::kNative;
 }
diff --git a/sdk/android/src/jni/videoframe.h b/sdk/android/src/jni/videoframe.h
index c477286..1f10eb4 100644
--- a/sdk/android/src/jni/videoframe.h
+++ b/sdk/android/src/jni/videoframe.h
@@ -120,6 +120,16 @@
 
   jobject video_frame_buffer() const;
 
+  // Crops a region defined by |crop_x|, |crop_y|, |crop_width| and
+  // |crop_height|. Scales it to size |scale_width| x |scale_height|.
+  rtc::scoped_refptr<AndroidVideoBuffer> CropAndScale(JNIEnv* jni,
+                                                      int crop_x,
+                                                      int crop_y,
+                                                      int crop_width,
+                                                      int crop_height,
+                                                      int scale_width,
+                                                      int scale_height);
+
   // Returns an instance of VideoRenderer.I420Frame (deprecated)
   jobject ToJavaI420Frame(JNIEnv* jni, int rotation);