1 /* 2 * Copyright 2019 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 package org.webrtc; 12 13 import android.support.annotation.Nullable; 14 15 /** 16 * Lightweight abstraction for an object that can receive video frames, process them, and pass them 17 * on to another object. This object is also allowed to observe capturer start/stop. 18 */ 19 public interface VideoProcessor extends CapturerObserver { 20 public static class FrameAdaptationParameters { 21 public final int cropX; 22 public final int cropY; 23 public final int cropWidth; 24 public final int cropHeight; 25 public final int scaleWidth; 26 public final int scaleHeight; 27 public final long timestampNs; 28 public final boolean drop; 29 FrameAdaptationParameters(int cropX, int cropY, int cropWidth, int cropHeight, int scaleWidth, int scaleHeight, long timestampNs, boolean drop)30 public FrameAdaptationParameters(int cropX, int cropY, int cropWidth, int cropHeight, 31 int scaleWidth, int scaleHeight, long timestampNs, boolean drop) { 32 this.cropX = cropX; 33 this.cropY = cropY; 34 this.cropWidth = cropWidth; 35 this.cropHeight = cropHeight; 36 this.scaleWidth = scaleWidth; 37 this.scaleHeight = scaleHeight; 38 this.timestampNs = timestampNs; 39 this.drop = drop; 40 } 41 } 42 43 /** 44 * This is a chance to access an unadapted frame. The default implementation applies the 45 * adaptation and forwards the frame to {@link #onFrameCaptured(VideoFrame)}. 46 */ onFrameCaptured(VideoFrame frame, FrameAdaptationParameters parameters)47 default void onFrameCaptured(VideoFrame frame, FrameAdaptationParameters parameters) { 48 VideoFrame adaptedFrame = applyFrameAdaptationParameters(frame, parameters); 49 if (adaptedFrame != null) { 50 onFrameCaptured(adaptedFrame); 51 adaptedFrame.release(); 52 } 53 } 54 55 /** 56 * Set the sink that receives the output from this processor. Null can be passed in to unregister 57 * a sink. 58 */ setSink(@ullable VideoSink sink)59 void setSink(@Nullable VideoSink sink); 60 61 /** 62 * Applies the frame adaptation parameters to a frame. Returns null if the frame is meant to be 63 * dropped. Returns a new frame. The caller is responsible for releasing the returned frame. 64 */ applyFrameAdaptationParameters( VideoFrame frame, FrameAdaptationParameters parameters)65 public static @Nullable VideoFrame applyFrameAdaptationParameters( 66 VideoFrame frame, FrameAdaptationParameters parameters) { 67 if (parameters.drop) { 68 return null; 69 } 70 71 final VideoFrame.Buffer adaptedBuffer = 72 frame.getBuffer().cropAndScale(parameters.cropX, parameters.cropY, parameters.cropWidth, 73 parameters.cropHeight, parameters.scaleWidth, parameters.scaleHeight); 74 return new VideoFrame(adaptedBuffer, frame.getRotation(), parameters.timestampNs); 75 } 76 } 77