1 /* 2 * Copyright 2015 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 #ifndef SDK_ANDROID_SRC_JNI_VIDEO_FRAME_H_ 12 #define SDK_ANDROID_SRC_JNI_VIDEO_FRAME_H_ 13 14 #include <jni.h> 15 16 #include "api/video/video_frame.h" 17 #include "api/video/video_frame_buffer.h" 18 #include "api/video/video_rotation.h" 19 #include "rtc_base/callback.h" 20 #include "sdk/android/src/jni/jni_helpers.h" 21 22 namespace webrtc { 23 namespace jni { 24 25 class AndroidVideoBuffer : public VideoFrameBuffer { 26 public: 27 // Creates a native VideoFrameBuffer from a Java VideoFrame.Buffer. 28 static rtc::scoped_refptr<AndroidVideoBuffer> Create( 29 JNIEnv* jni, 30 const JavaRef<jobject>& j_video_frame_buffer); 31 32 // Similar to the Create() above, but adopts and takes ownership of the Java 33 // VideoFrame.Buffer. I.e. retain() will not be called, but release() will be 34 // called when the returned AndroidVideoBuffer is destroyed. 35 static rtc::scoped_refptr<AndroidVideoBuffer> Adopt( 36 JNIEnv* jni, 37 const JavaRef<jobject>& j_video_frame_buffer); 38 39 ~AndroidVideoBuffer() override; 40 41 const ScopedJavaGlobalRef<jobject>& video_frame_buffer() const; 42 43 // Crops a region defined by |crop_x|, |crop_y|, |crop_width| and 44 // |crop_height|. Scales it to size |scale_width| x |scale_height|. 45 rtc::scoped_refptr<AndroidVideoBuffer> CropAndScale(JNIEnv* jni, 46 int crop_x, 47 int crop_y, 48 int crop_width, 49 int crop_height, 50 int scale_width, 51 int scale_height); 52 53 protected: 54 // Should not be called directly. Adopts the Java VideoFrame.Buffer. Use 55 // Create() or Adopt() instead for clarity. 56 AndroidVideoBuffer(JNIEnv* jni, const JavaRef<jobject>& j_video_frame_buffer); 57 58 private: 59 Type type() const override; 60 int width() const override; 61 int height() const override; 62 63 rtc::scoped_refptr<I420BufferInterface> ToI420() override; 64 65 const int width_; 66 const int height_; 67 // Holds a VideoFrame.Buffer. 68 const ScopedJavaGlobalRef<jobject> j_video_frame_buffer_; 69 }; 70 71 VideoFrame JavaToNativeFrame(JNIEnv* jni, 72 const JavaRef<jobject>& j_video_frame, 73 uint32_t timestamp_rtp); 74 75 // NOTE: Returns a new video frame that has to be released by calling 76 // ReleaseJavaVideoFrame. 77 ScopedJavaLocalRef<jobject> NativeToJavaVideoFrame(JNIEnv* jni, 78 const VideoFrame& frame); 79 void ReleaseJavaVideoFrame(JNIEnv* jni, const JavaRef<jobject>& j_video_frame); 80 81 int64_t GetJavaVideoFrameTimestampNs(JNIEnv* jni, 82 const JavaRef<jobject>& j_video_frame); 83 84 } // namespace jni 85 } // namespace webrtc 86 87 #endif // SDK_ANDROID_SRC_JNI_VIDEO_FRAME_H_ 88