Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: talk/app/webrtc/java/jni/native_handle_impl.cc

Issue 1460703002: Implement AndroidTextureBuffer::NativeToI420. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fix comment nits. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 uv_width = (width()+7) / 8;
67 << "AndroidTextureBuffer::NativeToI420Buffer not implemented."; 75 int stride = 8 * uv_width;
68 return nullptr; 76 int uv_height = (height()+1)/2;
77 size_t size = stride * (height() + uv_height);
78 // The data is owned by the frame, and the normal case is that the
79 // data is deleted by the frame's destructor callback.
80 //
81 // TODO(nisse): Use an I420BufferPool. We then need to extend that
82 // class, and I420Buffer, to support our memory layout.
83 rtc::scoped_ptr<uint8_t, webrtc::AlignedFreeDeleter> yuv_data(
84 static_cast<uint8_t*>(webrtc::AlignedMalloc(size, kBufferAlignment)));
85 // See SurfaceTextureHelper.java for the required layout.
86 uint8_t* y_data = yuv_data.get();
87 uint8_t* u_data = y_data + height() * stride;
88 uint8_t* v_data = u_data + stride/2;
89
90 rtc::scoped_refptr<webrtc::VideoFrameBuffer> copy =
91 new rtc::RefCountedObject<webrtc::WrappedI420Buffer>(
92 width(), height(),
93 y_data, stride,
94 u_data, stride,
95 v_data, stride,
96 rtc::Bind(&webrtc::AlignedFree, yuv_data.release()));
97
98 JNIEnv* jni = AttachCurrentThreadIfNeeded();
99 ScopedLocalRefFrame local_ref_frame(jni);
100
101 jmethodID transform_mid = GetMethodID(
102 jni,
103 GetObjectClass(jni, surface_texture_helper_),
104 "textureToYUV",
105 "(Ljava/nio/ByteBuffer;IIII[F)V");
106
107 jobject byte_buffer = jni->NewDirectByteBuffer(y_data, size);
108
109 // TODO(nisse): Keep java transform matrix around.
110 jfloatArray sampling_matrix = jni->NewFloatArray(16);
111 jni->SetFloatArrayRegion(sampling_matrix, 0, 16,
112 native_handle_.sampling_matrix);
113
114 jni->CallVoidMethod(surface_texture_helper_,
115 transform_mid,
116 byte_buffer, width(), height(), stride,
117 native_handle_.oes_texture_id, sampling_matrix);
118 CHECK_EXCEPTION(jni) << "textureToYUV throwed an exception";
119
120 return copy;
69 } 121 }
70 122
71 rtc::scoped_refptr<AndroidTextureBuffer> AndroidTextureBuffer::CropAndScale( 123 rtc::scoped_refptr<AndroidTextureBuffer> AndroidTextureBuffer::CropAndScale(
72 int cropped_input_width, 124 int cropped_input_width,
73 int cropped_input_height, 125 int cropped_input_height,
74 int dst_widht, 126 int dst_widht,
75 int dst_height) { 127 int dst_height) {
76 // TODO(perkj) Implement cropping. 128 // TODO(perkj) Implement cropping.
77 RTC_CHECK_EQ(cropped_input_width, width_); 129 RTC_CHECK_EQ(cropped_input_width, width_);
78 RTC_CHECK_EQ(cropped_input_height, height_); 130 RTC_CHECK_EQ(cropped_input_height, height_);
79 131
80 // Here we use Bind magic to add a reference count to |this| until the newly 132 // Here we use Bind magic to add a reference count to |this| until the newly
81 // created AndroidTextureBuffer is destructed. ScaledFrameNotInUse will be 133 // created AndroidTextureBuffer is destructed. ScaledFrameNotInUse will be
82 // called that happens and when it finishes, the reference count to |this| 134 // called that happens and when it finishes, the reference count to |this|
83 // will be decreased by one. 135 // will be decreased by one.
84 return new rtc::RefCountedObject<AndroidTextureBuffer>( 136 return new rtc::RefCountedObject<AndroidTextureBuffer>(
85 dst_widht, dst_height, native_handle_, 137 dst_widht, dst_height, native_handle_, surface_texture_helper_,
86 rtc::KeepRefUntilDone(this)); 138 rtc::KeepRefUntilDone(this));
87 } 139 }
88 140
89 } // namespace webrtc_jni 141 } // namespace webrtc_jni
OLDNEW
« no previous file with comments | « talk/app/webrtc/java/jni/native_handle_impl.h ('k') | talk/app/webrtc/java/jni/peerconnection_jni.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698