• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "sdk/android/src/jni/video_frame.h"
12 
13 #include <memory>
14 
15 #include "api/scoped_refptr.h"
16 #include "common_video/include/video_frame_buffer.h"
17 #include "rtc_base/bind.h"
18 #include "rtc_base/checks.h"
19 #include "rtc_base/keep_ref_until_done.h"
20 #include "rtc_base/logging.h"
21 #include "rtc_base/time_utils.h"
22 #include "sdk/android/generated_video_jni/VideoFrame_jni.h"
23 #include "sdk/android/src/jni/jni_helpers.h"
24 #include "sdk/android/src/jni/wrapped_native_i420_buffer.h"
25 
26 namespace webrtc {
27 namespace jni {
28 
29 namespace {
30 
31 class AndroidVideoI420Buffer : public I420BufferInterface {
32  public:
33   // Adopts and takes ownership of the Java VideoFrame.Buffer. I.e. retain()
34   // will not be called, but release() will be called when the returned
35   // AndroidVideoBuffer is destroyed.
36   static rtc::scoped_refptr<AndroidVideoI420Buffer> Adopt(
37       JNIEnv* jni,
38       int width,
39       int height,
40       const JavaRef<jobject>& j_video_frame_buffer);
41 
42  protected:
43   // Should not be called directly. Adopts the buffer. Use Adopt() instead for
44   // clarity.
45   AndroidVideoI420Buffer(JNIEnv* jni,
46                          int width,
47                          int height,
48                          const JavaRef<jobject>& j_video_frame_buffer);
49   ~AndroidVideoI420Buffer() override;
50 
51  private:
DataY() const52   const uint8_t* DataY() const override { return data_y_; }
DataU() const53   const uint8_t* DataU() const override { return data_u_; }
DataV() const54   const uint8_t* DataV() const override { return data_v_; }
55 
StrideY() const56   int StrideY() const override { return stride_y_; }
StrideU() const57   int StrideU() const override { return stride_u_; }
StrideV() const58   int StrideV() const override { return stride_v_; }
59 
width() const60   int width() const override { return width_; }
height() const61   int height() const override { return height_; }
62 
63   const int width_;
64   const int height_;
65   // Holds a VideoFrame.I420Buffer.
66   const ScopedJavaGlobalRef<jobject> j_video_frame_buffer_;
67 
68   const uint8_t* data_y_;
69   const uint8_t* data_u_;
70   const uint8_t* data_v_;
71   int stride_y_;
72   int stride_u_;
73   int stride_v_;
74 };
75 
Adopt(JNIEnv * jni,int width,int height,const JavaRef<jobject> & j_video_frame_buffer)76 rtc::scoped_refptr<AndroidVideoI420Buffer> AndroidVideoI420Buffer::Adopt(
77     JNIEnv* jni,
78     int width,
79     int height,
80     const JavaRef<jobject>& j_video_frame_buffer) {
81   return new rtc::RefCountedObject<AndroidVideoI420Buffer>(
82       jni, width, height, j_video_frame_buffer);
83 }
84 
AndroidVideoI420Buffer(JNIEnv * jni,int width,int height,const JavaRef<jobject> & j_video_frame_buffer)85 AndroidVideoI420Buffer::AndroidVideoI420Buffer(
86     JNIEnv* jni,
87     int width,
88     int height,
89     const JavaRef<jobject>& j_video_frame_buffer)
90     : width_(width),
91       height_(height),
92       j_video_frame_buffer_(jni, j_video_frame_buffer) {
93   ScopedJavaLocalRef<jobject> j_data_y =
94       Java_I420Buffer_getDataY(jni, j_video_frame_buffer);
95   ScopedJavaLocalRef<jobject> j_data_u =
96       Java_I420Buffer_getDataU(jni, j_video_frame_buffer);
97   ScopedJavaLocalRef<jobject> j_data_v =
98       Java_I420Buffer_getDataV(jni, j_video_frame_buffer);
99 
100   data_y_ =
101       static_cast<const uint8_t*>(jni->GetDirectBufferAddress(j_data_y.obj()));
102   data_u_ =
103       static_cast<const uint8_t*>(jni->GetDirectBufferAddress(j_data_u.obj()));
104   data_v_ =
105       static_cast<const uint8_t*>(jni->GetDirectBufferAddress(j_data_v.obj()));
106 
107   stride_y_ = Java_I420Buffer_getStrideY(jni, j_video_frame_buffer);
108   stride_u_ = Java_I420Buffer_getStrideU(jni, j_video_frame_buffer);
109   stride_v_ = Java_I420Buffer_getStrideV(jni, j_video_frame_buffer);
110 }
111 
~AndroidVideoI420Buffer()112 AndroidVideoI420Buffer::~AndroidVideoI420Buffer() {
113   JNIEnv* jni = AttachCurrentThreadIfNeeded();
114   Java_Buffer_release(jni, j_video_frame_buffer_);
115 }
116 
117 }  // namespace
118 
GetJavaVideoFrameTimestampNs(JNIEnv * jni,const JavaRef<jobject> & j_video_frame)119 int64_t GetJavaVideoFrameTimestampNs(JNIEnv* jni,
120                                      const JavaRef<jobject>& j_video_frame) {
121   return Java_VideoFrame_getTimestampNs(jni, j_video_frame);
122 }
123 
Adopt(JNIEnv * jni,const JavaRef<jobject> & j_video_frame_buffer)124 rtc::scoped_refptr<AndroidVideoBuffer> AndroidVideoBuffer::Adopt(
125     JNIEnv* jni,
126     const JavaRef<jobject>& j_video_frame_buffer) {
127   return new rtc::RefCountedObject<AndroidVideoBuffer>(jni,
128                                                        j_video_frame_buffer);
129 }
130 
Create(JNIEnv * jni,const JavaRef<jobject> & j_video_frame_buffer)131 rtc::scoped_refptr<AndroidVideoBuffer> AndroidVideoBuffer::Create(
132     JNIEnv* jni,
133     const JavaRef<jobject>& j_video_frame_buffer) {
134   Java_Buffer_retain(jni, j_video_frame_buffer);
135   return Adopt(jni, j_video_frame_buffer);
136 }
137 
AndroidVideoBuffer(JNIEnv * jni,const JavaRef<jobject> & j_video_frame_buffer)138 AndroidVideoBuffer::AndroidVideoBuffer(
139     JNIEnv* jni,
140     const JavaRef<jobject>& j_video_frame_buffer)
141     : width_(Java_Buffer_getWidth(jni, j_video_frame_buffer)),
142       height_(Java_Buffer_getHeight(jni, j_video_frame_buffer)),
143       j_video_frame_buffer_(jni, j_video_frame_buffer) {}
144 
~AndroidVideoBuffer()145 AndroidVideoBuffer::~AndroidVideoBuffer() {
146   JNIEnv* jni = AttachCurrentThreadIfNeeded();
147   Java_Buffer_release(jni, j_video_frame_buffer_);
148 }
149 
video_frame_buffer() const150 const ScopedJavaGlobalRef<jobject>& AndroidVideoBuffer::video_frame_buffer()
151     const {
152   return j_video_frame_buffer_;
153 }
154 
CropAndScale(JNIEnv * jni,int crop_x,int crop_y,int crop_width,int crop_height,int scale_width,int scale_height)155 rtc::scoped_refptr<AndroidVideoBuffer> AndroidVideoBuffer::CropAndScale(
156     JNIEnv* jni,
157     int crop_x,
158     int crop_y,
159     int crop_width,
160     int crop_height,
161     int scale_width,
162     int scale_height) {
163   return Adopt(jni, Java_Buffer_cropAndScale(jni, j_video_frame_buffer_, crop_x,
164                                              crop_y, crop_width, crop_height,
165                                              scale_width, scale_height));
166 }
167 
type() const168 VideoFrameBuffer::Type AndroidVideoBuffer::type() const {
169   return Type::kNative;
170 }
171 
width() const172 int AndroidVideoBuffer::width() const {
173   return width_;
174 }
175 
height() const176 int AndroidVideoBuffer::height() const {
177   return height_;
178 }
179 
ToI420()180 rtc::scoped_refptr<I420BufferInterface> AndroidVideoBuffer::ToI420() {
181   JNIEnv* jni = AttachCurrentThreadIfNeeded();
182   ScopedJavaLocalRef<jobject> j_i420_buffer =
183       Java_Buffer_toI420(jni, j_video_frame_buffer_);
184 
185   // We don't need to retain the buffer because toI420 returns a new object that
186   // we are assumed to take the ownership of.
187   return AndroidVideoI420Buffer::Adopt(jni, width_, height_, j_i420_buffer);
188 }
189 
JavaToNativeFrame(JNIEnv * jni,const JavaRef<jobject> & j_video_frame,uint32_t timestamp_rtp)190 VideoFrame JavaToNativeFrame(JNIEnv* jni,
191                              const JavaRef<jobject>& j_video_frame,
192                              uint32_t timestamp_rtp) {
193   ScopedJavaLocalRef<jobject> j_video_frame_buffer =
194       Java_VideoFrame_getBuffer(jni, j_video_frame);
195   int rotation = Java_VideoFrame_getRotation(jni, j_video_frame);
196   int64_t timestamp_ns = Java_VideoFrame_getTimestampNs(jni, j_video_frame);
197   rtc::scoped_refptr<AndroidVideoBuffer> buffer =
198       AndroidVideoBuffer::Create(jni, j_video_frame_buffer);
199   return VideoFrame::Builder()
200       .set_video_frame_buffer(buffer)
201       .set_timestamp_rtp(timestamp_rtp)
202       .set_timestamp_ms(timestamp_ns / rtc::kNumNanosecsPerMillisec)
203       .set_rotation(static_cast<VideoRotation>(rotation))
204       .build();
205 }
206 
NativeToJavaVideoFrame(JNIEnv * jni,const VideoFrame & frame)207 ScopedJavaLocalRef<jobject> NativeToJavaVideoFrame(JNIEnv* jni,
208                                                    const VideoFrame& frame) {
209   rtc::scoped_refptr<VideoFrameBuffer> buffer = frame.video_frame_buffer();
210 
211   if (buffer->type() == VideoFrameBuffer::Type::kNative) {
212     AndroidVideoBuffer* android_buffer =
213         static_cast<AndroidVideoBuffer*>(buffer.get());
214     ScopedJavaLocalRef<jobject> j_video_frame_buffer(
215         jni, android_buffer->video_frame_buffer());
216     Java_Buffer_retain(jni, j_video_frame_buffer);
217     return Java_VideoFrame_Constructor(
218         jni, j_video_frame_buffer, static_cast<jint>(frame.rotation()),
219         static_cast<jlong>(frame.timestamp_us() *
220                            rtc::kNumNanosecsPerMicrosec));
221   } else {
222     return Java_VideoFrame_Constructor(
223         jni, WrapI420Buffer(jni, buffer->ToI420()),
224         static_cast<jint>(frame.rotation()),
225         static_cast<jlong>(frame.timestamp_us() *
226                            rtc::kNumNanosecsPerMicrosec));
227   }
228 }
229 
ReleaseJavaVideoFrame(JNIEnv * jni,const JavaRef<jobject> & j_video_frame)230 void ReleaseJavaVideoFrame(JNIEnv* jni, const JavaRef<jobject>& j_video_frame) {
231   Java_VideoFrame_release(jni, j_video_frame);
232 }
233 
234 }  // namespace jni
235 }  // namespace webrtc
236