OLD | NEW |
---|---|
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
11 * this list of conditions and the following disclaimer in the documentation | 11 * this list of conditions and the following disclaimer in the documentation |
12 * and/or other materials provided with the distribution. | 12 * and/or other materials provided with the distribution. |
13 * 3. The name of the author may not be used to endorse or promote products | 13 * 3. The name of the author may not be used to endorse or promote products |
14 * derived from this software without specific prior written permission. | 14 * derived from this software without specific prior written permission. |
15 * | 15 * |
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 */ | 26 */ |
27 | 27 |
28 #include "talk/app/webrtc/java/jni/native_handle_impl.h" | 28 #include "talk/app/webrtc/java/jni/native_handle_impl.h" |
29 | 29 |
30 #include "talk/app/webrtc/java/jni/jni_helpers.h" | |
31 #include "webrtc/base/bind.h" | |
30 #include "webrtc/base/checks.h" | 32 #include "webrtc/base/checks.h" |
31 #include "webrtc/base/keep_ref_until_done.h" | 33 #include "webrtc/base/keep_ref_until_done.h" |
34 #include "webrtc/base/scoped_ptr.h" | |
32 #include "webrtc/base/scoped_ref_ptr.h" | 35 #include "webrtc/base/scoped_ref_ptr.h" |
33 | 36 |
34 using webrtc::NativeHandleBuffer; | 37 using webrtc::NativeHandleBuffer; |
35 | 38 |
36 namespace webrtc_jni { | 39 namespace webrtc_jni { |
37 | 40 |
41 // Aligning pointer to 64 bytes for improved performance, e.g. use SIMD. | |
42 static const int kBufferAlignment = 64; | |
43 | |
38 NativeHandleImpl::NativeHandleImpl(JNIEnv* jni, | 44 NativeHandleImpl::NativeHandleImpl(JNIEnv* jni, |
39 jint j_oes_texture_id, | 45 jint j_oes_texture_id, |
40 jfloatArray j_transform_matrix) | 46 jfloatArray j_transform_matrix) |
41 : oes_texture_id(j_oes_texture_id) { | 47 : oes_texture_id(j_oes_texture_id) { |
42 RTC_CHECK_EQ(16, jni->GetArrayLength(j_transform_matrix)); | 48 RTC_CHECK_EQ(16, jni->GetArrayLength(j_transform_matrix)); |
43 jfloat* transform_matrix_ptr = | 49 jfloat* transform_matrix_ptr = |
44 jni->GetFloatArrayElements(j_transform_matrix, nullptr); | 50 jni->GetFloatArrayElements(j_transform_matrix, nullptr); |
51 // TODO(nisse): Hold on to java float array instead, since it is | |
perkj_webrtc
2015/12/09 09:28:28
remove this todo. It will not be true when I land
nisse-webrtc
2015/12/09 12:02:21
Done.
| |
52 // used only for callbacks into java. | |
45 for (int i = 0; i < 16; ++i) { | 53 for (int i = 0; i < 16; ++i) { |
46 sampling_matrix[i] = transform_matrix_ptr[i]; | 54 sampling_matrix[i] = transform_matrix_ptr[i]; |
47 } | 55 } |
48 jni->ReleaseFloatArrayElements(j_transform_matrix, transform_matrix_ptr, 0); | 56 jni->ReleaseFloatArrayElements(j_transform_matrix, transform_matrix_ptr, 0); |
49 } | 57 } |
50 | 58 |
51 AndroidTextureBuffer::AndroidTextureBuffer( | 59 AndroidTextureBuffer::AndroidTextureBuffer( |
52 int width, | 60 int width, |
53 int height, | 61 int height, |
54 const NativeHandleImpl& native_handle, | 62 const NativeHandleImpl& native_handle, |
63 jobject surface_texture_helper, | |
55 const rtc::Callback0<void>& no_longer_used) | 64 const rtc::Callback0<void>& no_longer_used) |
56 : webrtc::NativeHandleBuffer(&native_handle_, width, height), | 65 : webrtc::NativeHandleBuffer(&native_handle_, width, height), |
57 native_handle_(native_handle), | 66 native_handle_(native_handle), |
67 surface_texture_helper_(surface_texture_helper), | |
58 no_longer_used_cb_(no_longer_used) {} | 68 no_longer_used_cb_(no_longer_used) {} |
59 | 69 |
60 AndroidTextureBuffer::~AndroidTextureBuffer() { | 70 AndroidTextureBuffer::~AndroidTextureBuffer() { |
61 no_longer_used_cb_(); | 71 no_longer_used_cb_(); |
62 } | 72 } |
63 | 73 |
64 rtc::scoped_refptr<webrtc::VideoFrameBuffer> | 74 rtc::scoped_refptr<webrtc::VideoFrameBuffer> |
65 AndroidTextureBuffer::NativeToI420Buffer() { | 75 AndroidTextureBuffer::NativeToI420Buffer() { |
66 RTC_NOTREACHED() | 76 int y_width = (width()+3) / 4; |
perkj_webrtc
2015/12/09 09:28:28
please add comment about the memory layout.
nisse-webrtc
2015/12/09 12:02:22
Done, but I put the comment in SurfaceTextureHelpe
| |
67 << "AndroidTextureBuffer::NativeToI420Buffer not implemented."; | 77 int uv_width = (width()+7) / 8; |
68 return nullptr; | 78 int stride = 8 * uv_width; |
79 int uv_height = (height()+1)/2; | |
80 size_t size = stride * (height() + uv_height); | |
81 // The data is owned by the frame, and the normal case is that the | |
82 // data is deleted by the frame's destructor callback. | |
perkj_webrtc
2015/12/09 09:28:28
Add TODO to not allocate on each frame. I think we
nisse-webrtc
2015/12/09 12:02:22
Done.
| |
83 rtc::scoped_ptr<uint8_t, webrtc::AlignedFreeDeleter> yuv_data( | |
84 static_cast<uint8_t*>(webrtc::AlignedMalloc(size, kBufferAlignment))); | |
85 uint8_t* y_data = yuv_data.get(); | |
86 uint8_t* u_data = y_data + height() * stride; | |
87 uint8_t* v_data = u_data + stride/2; | |
88 | |
89 rtc::scoped_refptr<webrtc::VideoFrameBuffer> copy = | |
90 new rtc::RefCountedObject<webrtc::WrappedI420Buffer>( | |
91 width(), height(), | |
92 y_data, stride, | |
93 u_data, stride, | |
94 v_data, stride, | |
95 rtc::Bind(&webrtc::AlignedFree, yuv_data.release())); | |
96 | |
97 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | |
98 ScopedLocalRefFrame local_ref_frame(jni); | |
99 | |
100 jmethodID transform_mid = GetMethodID( | |
101 jni, | |
102 GetObjectClass(jni, surface_texture_helper_), | |
103 "textureToYUV", | |
104 "(Ljava/nio/ByteBuffer;IIII[F)V"); | |
105 | |
106 jobject byte_buffer = jni->NewDirectByteBuffer(y_data, size); | |
107 | |
108 /* Set u = v = 0. */ | |
109 memset(u_data, 0x80, stride * uv_height); | |
110 | |
111 // TODO(nisse): Keep java transform matrix around. | |
112 jfloatArray sampling_matrix = jni->NewFloatArray(16); | |
113 jni->SetFloatArrayRegion(sampling_matrix, 0, 16, | |
114 native_handle_.sampling_matrix); | |
115 | |
116 jni->CallVoidMethod(surface_texture_helper_, | |
117 transform_mid, | |
118 byte_buffer, width(), height(), stride, | |
119 native_handle_.oes_texture_id, sampling_matrix); | |
120 CHECK_EXCEPTION(jni) << "textureToYUV throwed an exception"; | |
121 | |
122 return copy; | |
69 } | 123 } |
70 | 124 |
71 rtc::scoped_refptr<AndroidTextureBuffer> AndroidTextureBuffer::CropAndScale( | 125 rtc::scoped_refptr<AndroidTextureBuffer> AndroidTextureBuffer::CropAndScale( |
72 int cropped_input_width, | 126 int cropped_input_width, |
73 int cropped_input_height, | 127 int cropped_input_height, |
74 int dst_widht, | 128 int dst_widht, |
75 int dst_height) { | 129 int dst_height) { |
76 // TODO(perkj) Implement cropping. | 130 // TODO(perkj) Implement cropping. |
77 RTC_CHECK_EQ(cropped_input_width, width_); | 131 RTC_CHECK_EQ(cropped_input_width, width_); |
78 RTC_CHECK_EQ(cropped_input_height, height_); | 132 RTC_CHECK_EQ(cropped_input_height, height_); |
79 | 133 |
80 // Here we use Bind magic to add a reference count to |this| until the newly | 134 // Here we use Bind magic to add a reference count to |this| until the newly |
81 // created AndroidTextureBuffer is destructed. ScaledFrameNotInUse will be | 135 // created AndroidTextureBuffer is destructed. ScaledFrameNotInUse will be |
82 // called that happens and when it finishes, the reference count to |this| | 136 // called that happens and when it finishes, the reference count to |this| |
83 // will be decreased by one. | 137 // will be decreased by one. |
84 return new rtc::RefCountedObject<AndroidTextureBuffer>( | 138 return new rtc::RefCountedObject<AndroidTextureBuffer>( |
85 dst_widht, dst_height, native_handle_, | 139 dst_widht, dst_height, native_handle_, surface_texture_helper_, |
86 rtc::KeepRefUntilDone(this)); | 140 rtc::KeepRefUntilDone(this)); |
87 } | 141 } |
88 | 142 |
89 } // namespace webrtc_jni | 143 } // namespace webrtc_jni |
OLD | NEW |