blob: 0d0153281eac1480a5dd07190cc117b6d976d5d1 [file] [log] [blame]
glaznev@webrtc.org18c92472015-02-18 18:42:55 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
glaznev@webrtc.org18c92472015-02-18 18:42:55 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
glaznev@webrtc.org18c92472015-02-18 18:42:55 +00009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#ifndef WEBRTC_API_JAVA_JNI_NATIVE_HANDLE_IMPL_H_
12#define WEBRTC_API_JAVA_JNI_NATIVE_HANDLE_IMPL_H_
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000013
Magnus Jedvert6781ea42015-10-02 13:56:04 +020014#include <jni.h>
15
kjellander6f8ce062015-11-16 13:52:24 -080016#include "webrtc/common_video/include/video_frame_buffer.h"
Per71f5a9a2015-12-11 09:32:37 +010017#include "webrtc/common_video/rotation.h"
magjed52a30e32015-10-12 06:53:20 -070018
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000019namespace webrtc_jni {
20
nisse47ac4622016-05-25 08:47:01 -070021// Open gl texture matrix, in column-major order. Operations are
22// in-place.
23class Matrix {
24 public:
25 Matrix(JNIEnv* jni, jfloatArray a);
26
27 jfloatArray ToJava(JNIEnv* jni);
28
29 // Crop arguments are relative to original size.
30 void Crop(float cropped_width,
31 float cropped_height,
32 float crop_x,
33 float crop_y);
34
35 void Rotate(webrtc::VideoRotation rotation);
36
37 private:
38 static void Multiply(const float a[16], const float b[16], float result[16]);
39 float elem_[16];
40};
41
Per9b3f56e2015-04-09 13:44:16 +020042// Wrapper for texture object.
Per488e75f2015-11-19 10:43:36 +010043struct NativeHandleImpl {
44 NativeHandleImpl(JNIEnv* jni,
45 jint j_oes_texture_id,
46 jfloatArray j_transform_matrix);
Per9b3f56e2015-04-09 13:44:16 +020047
nisse47ac4622016-05-25 08:47:01 -070048 NativeHandleImpl(int id, const Matrix& matrix);
49
Magnus Jedvert91b348c2015-10-07 22:57:06 +020050 const int oes_texture_id;
nisse47ac4622016-05-25 08:47:01 -070051 Matrix sampling_matrix;
Peter Boströmeb66e802015-06-05 11:08:03 +020052};
53
magjed52a30e32015-10-12 06:53:20 -070054class AndroidTextureBuffer : public webrtc::NativeHandleBuffer {
55 public:
56 AndroidTextureBuffer(int width,
57 int height,
Per488e75f2015-11-19 10:43:36 +010058 const NativeHandleImpl& native_handle,
nissec490e012015-12-10 06:23:33 -080059 jobject surface_texture_helper,
magjed52a30e32015-10-12 06:53:20 -070060 const rtc::Callback0<void>& no_longer_used);
61 ~AndroidTextureBuffer();
62 rtc::scoped_refptr<VideoFrameBuffer> NativeToI420Buffer() override;
63
Magnus Jedverta3002db2016-05-13 12:51:04 +020064 // First crop, then scale to dst resolution, and then rotate.
65 rtc::scoped_refptr<AndroidTextureBuffer> CropScaleAndRotate(
66 int cropped_width,
67 int cropped_height,
nisse47ac4622016-05-25 08:47:01 -070068 int crop_x,
69 int crop_y,
Magnus Jedverta3002db2016-05-13 12:51:04 +020070 int dst_width,
Per71f5a9a2015-12-11 09:32:37 +010071 int dst_height,
72 webrtc::VideoRotation rotation);
Pera3c20bb2015-11-26 13:41:44 +010073
magjed52a30e32015-10-12 06:53:20 -070074 private:
Per488e75f2015-11-19 10:43:36 +010075 NativeHandleImpl native_handle_;
nissec490e012015-12-10 06:23:33 -080076 // Raw object pointer, relying on the caller, i.e.,
77 // AndroidVideoCapturerJni or the C++ SurfaceTextureHelper, to keep
78 // a global reference. TODO(nisse): Make this a reference to the C++
79 // SurfaceTextureHelper instead, but that requires some refactoring
80 // of AndroidVideoCapturerJni.
81 jobject surface_texture_helper_;
magjed52a30e32015-10-12 06:53:20 -070082 rtc::Callback0<void> no_longer_used_cb_;
83};
84
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000085} // namespace webrtc_jni
86
Henrik Kjellander15583c12016-02-10 10:53:12 +010087#endif // WEBRTC_API_JAVA_JNI_NATIVE_HANDLE_IMPL_H_