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); |
45 for (int i = 0; i < 16; ++i) { | 51 for (int i = 0; i < 16; ++i) { |
46 sampling_matrix[i] = transform_matrix_ptr[i]; | 52 sampling_matrix[i] = transform_matrix_ptr[i]; |
47 } | 53 } |
48 jni->ReleaseFloatArrayElements(j_transform_matrix, transform_matrix_ptr, 0); | 54 jni->ReleaseFloatArrayElements(j_transform_matrix, transform_matrix_ptr, 0); |
49 } | 55 } |
50 | 56 |
51 AndroidTextureBuffer::AndroidTextureBuffer( | 57 AndroidTextureBuffer::AndroidTextureBuffer( |
52 int width, | 58 int width, |
53 int height, | 59 int height, |
54 const NativeHandleImpl& native_handle, | 60 const NativeHandleImpl& native_handle, |
61 jobject surface_texture_helper, | |
55 const rtc::Callback0<void>& no_longer_used) | 62 const rtc::Callback0<void>& no_longer_used) |
56 : webrtc::NativeHandleBuffer(&native_handle_, width, height), | 63 : webrtc::NativeHandleBuffer(&native_handle_, width, height), |
57 native_handle_(native_handle), | 64 native_handle_(native_handle), |
65 surface_texture_helper_(surface_texture_helper), | |
58 no_longer_used_cb_(no_longer_used) {} | 66 no_longer_used_cb_(no_longer_used) {} |
59 | 67 |
60 AndroidTextureBuffer::~AndroidTextureBuffer() { | 68 AndroidTextureBuffer::~AndroidTextureBuffer() { |
61 no_longer_used_cb_(); | 69 no_longer_used_cb_(); |
62 } | 70 } |
63 | 71 |
64 rtc::scoped_refptr<webrtc::VideoFrameBuffer> | 72 rtc::scoped_refptr<webrtc::VideoFrameBuffer> |
65 AndroidTextureBuffer::NativeToI420Buffer() { | 73 AndroidTextureBuffer::NativeToI420Buffer() { |
66 RTC_NOTREACHED() | 74 int y_width = (width()+3) / 4; |
67 << "AndroidTextureBuffer::NativeToI420Buffer not implemented."; | 75 int uv_width = (width()+7) / 8; |
68 return nullptr; | 76 int stride = 8 * uv_width; |
77 int uv_height = (height()+1)/2; | |
78 size_t size = stride * (height() + uv_height); | |
79 // The data is owned by the frame, and the normal case is that the | |
80 // data is deleted by the frame's destructor callback. | |
81 // | |
82 // TODO(nisse): Use an I420BufferPool. We then need to extend that | |
83 // class, and I420Buffer, to support our memory layout. | |
84 rtc::scoped_ptr<uint8_t, webrtc::AlignedFreeDeleter> yuv_data( | |
85 static_cast<uint8_t*>(webrtc::AlignedMalloc(size, kBufferAlignment))); | |
86 // See SurfaceTextureHelper.java for the required layout. | |
87 uint8_t* y_data = yuv_data.get(); | |
88 uint8_t* u_data = y_data + height() * stride; | |
89 uint8_t* v_data = u_data + stride/2; | |
90 | |
91 rtc::scoped_refptr<webrtc::VideoFrameBuffer> copy = | |
92 new rtc::RefCountedObject<webrtc::WrappedI420Buffer>( | |
93 width(), height(), | |
94 y_data, stride, | |
95 u_data, stride, | |
96 v_data, stride, | |
97 rtc::Bind(&webrtc::AlignedFree, yuv_data.release())); | |
98 | |
99 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | |
100 ScopedLocalRefFrame local_ref_frame(jni); | |
101 | |
102 jmethodID transform_mid = GetMethodID( | |
103 jni, | |
104 GetObjectClass(jni, surface_texture_helper_), | |
105 "textureToYUV", | |
106 "(Ljava/nio/ByteBuffer;IIII[F)V"); | |
107 | |
108 jobject byte_buffer = jni->NewDirectByteBuffer(y_data, size); | |
109 | |
110 /* Set u = v = 0. */ | |
111 memset(u_data, 0x80, stride * uv_height); | |
perkj_webrtc
2015/12/09 13:57:36
remove
| |
112 | |
113 // TODO(nisse): Keep java transform matrix around. | |
114 jfloatArray sampling_matrix = jni->NewFloatArray(16); | |
115 jni->SetFloatArrayRegion(sampling_matrix, 0, 16, | |
116 native_handle_.sampling_matrix); | |
117 | |
118 jni->CallVoidMethod(surface_texture_helper_, | |
119 transform_mid, | |
120 byte_buffer, width(), height(), stride, | |
121 native_handle_.oes_texture_id, sampling_matrix); | |
122 CHECK_EXCEPTION(jni) << "textureToYUV throwed an exception"; | |
123 | |
124 return copy; | |
69 } | 125 } |
70 | 126 |
71 rtc::scoped_refptr<AndroidTextureBuffer> AndroidTextureBuffer::CropAndScale( | 127 rtc::scoped_refptr<AndroidTextureBuffer> AndroidTextureBuffer::CropAndScale( |
72 int cropped_input_width, | 128 int cropped_input_width, |
73 int cropped_input_height, | 129 int cropped_input_height, |
74 int dst_widht, | 130 int dst_widht, |
75 int dst_height) { | 131 int dst_height) { |
76 // TODO(perkj) Implement cropping. | 132 // TODO(perkj) Implement cropping. |
77 RTC_CHECK_EQ(cropped_input_width, width_); | 133 RTC_CHECK_EQ(cropped_input_width, width_); |
78 RTC_CHECK_EQ(cropped_input_height, height_); | 134 RTC_CHECK_EQ(cropped_input_height, height_); |
79 | 135 |
80 // Here we use Bind magic to add a reference count to |this| until the newly | 136 // Here we use Bind magic to add a reference count to |this| until the newly |
81 // created AndroidTextureBuffer is destructed. ScaledFrameNotInUse will be | 137 // created AndroidTextureBuffer is destructed. ScaledFrameNotInUse will be |
82 // called that happens and when it finishes, the reference count to |this| | 138 // called that happens and when it finishes, the reference count to |this| |
83 // will be decreased by one. | 139 // will be decreased by one. |
84 return new rtc::RefCountedObject<AndroidTextureBuffer>( | 140 return new rtc::RefCountedObject<AndroidTextureBuffer>( |
85 dst_widht, dst_height, native_handle_, | 141 dst_widht, dst_height, native_handle_, surface_texture_helper_, |
86 rtc::KeepRefUntilDone(this)); | 142 rtc::KeepRefUntilDone(this)); |
87 } | 143 } |
88 | 144 |
89 } // namespace webrtc_jni | 145 } // namespace webrtc_jni |
OLD | NEW |