Move frame adaptation inside video processor.

Bug: webrtc:10530
Change-Id: Iba6a91bf3e1ec4b2821b554e9e28fd2ead662723
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/131947
Reviewed-by: Magnus Jedvert <magjed@webrtc.org>
Commit-Queue: Sami Kalliomäki <sakal@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#27542}
diff --git a/sdk/android/api/org/webrtc/VideoProcessor.java b/sdk/android/api/org/webrtc/VideoProcessor.java
index 1a2aca5..3a89090 100644
--- a/sdk/android/api/org/webrtc/VideoProcessor.java
+++ b/sdk/android/api/org/webrtc/VideoProcessor.java
@@ -17,9 +17,60 @@
  * on to another object. This object is also allowed to observe capturer start/stop.
  */
 public interface VideoProcessor extends CapturerObserver {
+  public static class FrameAdaptationParameters {
+    public final int cropX;
+    public final int cropY;
+    public final int cropWidth;
+    public final int cropHeight;
+    public final int scaleWidth;
+    public final int scaleHeight;
+    public final long timestampNs;
+    public final boolean drop;
+
+    public FrameAdaptationParameters(int cropX, int cropY, int cropWidth, int cropHeight,
+        int scaleWidth, int scaleHeight, long timestampNs, boolean drop) {
+      this.cropX = cropX;
+      this.cropY = cropY;
+      this.cropWidth = cropWidth;
+      this.cropHeight = cropHeight;
+      this.scaleWidth = scaleWidth;
+      this.scaleHeight = scaleHeight;
+      this.timestampNs = timestampNs;
+      this.drop = drop;
+    }
+  }
+
+  /**
+   * This is a chance to access an unadapted frame. The default implementation applies the
+   * adaptation and forwards the frame to {@link #onFrameCaptured(VideoFrame)}.
+   */
+  default void onFrameCaptured(VideoFrame frame, FrameAdaptationParameters parameters) {
+    VideoFrame adaptedFrame = applyFrameAdaptationParameters(frame, parameters);
+    if (adaptedFrame != null) {
+      onFrameCaptured(adaptedFrame);
+      adaptedFrame.release();
+    }
+  }
+
   /**
    * Set the sink that receives the output from this processor. Null can be passed in to unregister
    * a sink. After this call returns, no frames should be delivered to an unregistered sink.
    */
   void setSink(@Nullable VideoSink sink);
+
+  /**
+   * Applies the frame adaptation parameters to a frame. Returns null if the frame is meant to be
+   * dropped. Returns a new frame. The caller is responsible for releasing the returned frame.
+   */
+  public static @Nullable VideoFrame applyFrameAdaptationParameters(
+      VideoFrame frame, FrameAdaptationParameters parameters) {
+    if (parameters.drop) {
+      return null;
+    }
+
+    final VideoFrame.Buffer adaptedBuffer =
+        frame.getBuffer().cropAndScale(parameters.cropX, parameters.cropY, parameters.cropWidth,
+            parameters.cropHeight, parameters.scaleWidth, parameters.scaleHeight);
+    return new VideoFrame(adaptedBuffer, frame.getRotation(), parameters.timestampNs);
+  }
 }
diff --git a/sdk/android/api/org/webrtc/VideoSource.java b/sdk/android/api/org/webrtc/VideoSource.java
index 847a7bd..1b40231 100644
--- a/sdk/android/api/org/webrtc/VideoSource.java
+++ b/sdk/android/api/org/webrtc/VideoSource.java
@@ -59,28 +59,20 @@
 
     @Override
     public void onFrameCaptured(VideoFrame frame) {
-      final NativeAndroidVideoTrackSource.FrameAdaptationParameters parameters =
+      final VideoProcessor.FrameAdaptationParameters parameters =
           nativeAndroidVideoTrackSource.adaptFrame(frame);
-      if (parameters == null) {
-        // Drop frame.
-        return;
-      }
-
-      final VideoFrame.Buffer adaptedBuffer =
-          frame.getBuffer().cropAndScale(parameters.cropX, parameters.cropY, parameters.cropWidth,
-              parameters.cropHeight, parameters.scaleWidth, parameters.scaleHeight);
-      final VideoFrame adaptedFrame =
-          new VideoFrame(adaptedBuffer, frame.getRotation(), parameters.timestampNs);
-
       synchronized (videoProcessorLock) {
         if (videoProcessor != null) {
-          videoProcessor.onFrameCaptured(adaptedFrame);
-          adaptedBuffer.release();
+          videoProcessor.onFrameCaptured(frame, parameters);
           return;
         }
       }
-      nativeAndroidVideoTrackSource.onFrameCaptured(adaptedFrame);
-      adaptedBuffer.release();
+
+      VideoFrame adaptedFrame = VideoProcessor.applyFrameAdaptationParameters(frame, parameters);
+      if (adaptedFrame != null) {
+        nativeAndroidVideoTrackSource.onFrameCaptured(adaptedFrame);
+        adaptedFrame.release();
+      }
     }
   };