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

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: Addressed Magnus' comments. 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 <android/log.h>
perkj_webrtc 2015/12/07 12:53:50 remove this include.
nisse-webrtc 2015/12/08 11:44:51 Done. And removed corresponding logging.
28 #include "talk/app/webrtc/java/jni/native_handle_impl.h" 29 #include "talk/app/webrtc/java/jni/native_handle_impl.h"
29 30
31 #include "talk/app/webrtc/java/jni/jni_helpers.h"
32 #include "webrtc/base/bind.h"
30 #include "webrtc/base/checks.h" 33 #include "webrtc/base/checks.h"
31 #include "webrtc/base/keep_ref_until_done.h" 34 #include "webrtc/base/keep_ref_until_done.h"
35 #include "webrtc/base/logging.h"
36 #include "webrtc/base/scoped_ptr.h"
32 #include "webrtc/base/scoped_ref_ptr.h" 37 #include "webrtc/base/scoped_ref_ptr.h"
33 38
34 using webrtc::NativeHandleBuffer; 39 using webrtc::NativeHandleBuffer;
35 40
36 namespace webrtc_jni { 41 namespace webrtc_jni {
37 42
43 // Aligning pointer to 64 bytes for improved performance, e.g. use SIMD.
44 static const int kBufferAlignment = 64;
45
38 NativeHandleImpl::NativeHandleImpl(JNIEnv* jni, 46 NativeHandleImpl::NativeHandleImpl(JNIEnv* jni,
39 jint j_oes_texture_id, 47 jint j_oes_texture_id,
40 jfloatArray j_transform_matrix) 48 jfloatArray j_transform_matrix)
41 : oes_texture_id(j_oes_texture_id) { 49 : oes_texture_id(j_oes_texture_id) {
42 RTC_CHECK_EQ(16, jni->GetArrayLength(j_transform_matrix)); 50 RTC_CHECK_EQ(16, jni->GetArrayLength(j_transform_matrix));
43 jfloat* transform_matrix_ptr = 51 jfloat* transform_matrix_ptr =
44 jni->GetFloatArrayElements(j_transform_matrix, nullptr); 52 jni->GetFloatArrayElements(j_transform_matrix, nullptr);
53 // TODO(nisse): Hold on to java float array instead, since it is
54 // used only for callbacks into java.
45 for (int i = 0; i < 16; ++i) { 55 for (int i = 0; i < 16; ++i) {
46 sampling_matrix[i] = transform_matrix_ptr[i]; 56 sampling_matrix[i] = transform_matrix_ptr[i];
47 } 57 }
48 jni->ReleaseFloatArrayElements(j_transform_matrix, transform_matrix_ptr, 0); 58 jni->ReleaseFloatArrayElements(j_transform_matrix, transform_matrix_ptr, 0);
49 } 59 }
50 60
51 AndroidTextureBuffer::AndroidTextureBuffer( 61 AndroidTextureBuffer::AndroidTextureBuffer(
52 int width, 62 int width,
53 int height, 63 int height,
54 const NativeHandleImpl& native_handle, 64 const NativeHandleImpl& native_handle,
65 jobject surface_texture_helper,
55 const rtc::Callback0<void>& no_longer_used) 66 const rtc::Callback0<void>& no_longer_used)
56 : webrtc::NativeHandleBuffer(&native_handle_, width, height), 67 : webrtc::NativeHandleBuffer(&native_handle_, width, height),
57 native_handle_(native_handle), 68 native_handle_(native_handle),
69 surface_texture_helper_(surface_texture_helper),
58 no_longer_used_cb_(no_longer_used) {} 70 no_longer_used_cb_(no_longer_used) {}
59 71
60 AndroidTextureBuffer::~AndroidTextureBuffer() { 72 AndroidTextureBuffer::~AndroidTextureBuffer() {
61 no_longer_used_cb_(); 73 no_longer_used_cb_();
62 } 74 }
63 75
76 #define TAG "AndroidTextureBuffer"
perkj_webrtc 2015/12/07 12:53:50 remove
77 // Alternatively: #define AT_LOG LOG(LS_ERROR)
78 #define ALOGE LOG_TAG(rtc::LS_ERROR, TAG)
79
64 rtc::scoped_refptr<webrtc::VideoFrameBuffer> 80 rtc::scoped_refptr<webrtc::VideoFrameBuffer>
65 AndroidTextureBuffer::NativeToI420Buffer() { 81 AndroidTextureBuffer::NativeToI420Buffer() {
66 RTC_NOTREACHED() 82 ALOGE << "NativeToI420Buffer called";
67 << "AndroidTextureBuffer::NativeToI420Buffer not implemented."; 83 int y_width = (width()+3) / 4;
68 return nullptr; 84 int uv_width = (width()+7) / 8;
85 int stride = 8 * uv_width;
86 int uv_height = (height()+1)/2;
87 size_t size = stride * (height() + uv_height);
88 // The data is owned by the frame, and the normal case is that the
89 // data is deleted by the frame's destructor callback.
90 rtc::scoped_ptr<uint8_t, webrtc::AlignedFreeDeleter> yuv_data(
perkj_webrtc 2015/12/07 12:53:50 Should we have AndroidTextureBuffer take pointer t
nisse-webrtc 2015/12/08 11:44:51 Note that we use WrappedI420Buffer. To use a plain
perkj_webrtc 2015/12/09 09:28:28 Yes, we can have memory backed frames active at th
91 static_cast<uint8_t*>(webrtc::AlignedMalloc(size, kBufferAlignment)));
92 uint8_t *y_data = yuv_data.get();
93 uint8_t *u_data = y_data + height() * stride;
94 uint8_t *v_data = u_data + stride/2;
95
96 rtc::scoped_refptr<webrtc::VideoFrameBuffer> copy =
97 new rtc::RefCountedObject<webrtc::WrappedI420Buffer>(
98 width(), height(),
99 y_data, stride,
100 u_data, stride,
101 v_data, stride,
102 rtc::Bind(&webrtc::AlignedFree, yuv_data.release()));
103
104 ALOGE << "width: " << width() << " height: " << height() <<
105 " stride: " << stride;
106
107 JNIEnv* jni = AttachCurrentThreadIfNeeded();
108 ScopedLocalRefFrame local_ref_frame(jni);
109
110 ALOGE << "Looking up textureToYUV mid";
111 jmethodID transform_mid = GetMethodID(
112 jni,
113 GetObjectClass(jni, surface_texture_helper_),
114 "textureToYUV",
115 "(Ljava/nio/ByteBuffer;IIII[F)V");
116
117 ALOGE << "Creating byte buffer";
118 jobject byte_buffer = jni->NewDirectByteBuffer(y_data, size);
119
120 /* Set u = v = 0. */
121 memset(u_data, 0x80, stride * uv_height);
122
123 // TODO(nisse): Keep java transform matrix around.
124 jfloatArray sampling_matrix = jni->NewFloatArray(16);
125 jni->SetFloatArrayRegion(sampling_matrix, 0, 16,
126 native_handle_.sampling_matrix);
127
128 ALOGE << "Calling java textureToYUV";
129 jni->CallVoidMethod(surface_texture_helper_,
130 transform_mid,
131 byte_buffer, width(), height(), stride,
132 native_handle_.oes_texture_id, sampling_matrix);
133 CHECK_EXCEPTION(jni) << "textureToYUV throwed an exception";
134
135 return copy;
69 } 136 }
70 137
71 rtc::scoped_refptr<AndroidTextureBuffer> AndroidTextureBuffer::CropAndScale( 138 rtc::scoped_refptr<AndroidTextureBuffer> AndroidTextureBuffer::CropAndScale(
72 int cropped_input_width, 139 int cropped_input_width,
73 int cropped_input_height, 140 int cropped_input_height,
74 int dst_widht, 141 int dst_widht,
75 int dst_height) { 142 int dst_height) {
76 // TODO(perkj) Implement cropping. 143 // TODO(perkj) Implement cropping.
77 RTC_CHECK_EQ(cropped_input_width, width_); 144 RTC_CHECK_EQ(cropped_input_width, width_);
78 RTC_CHECK_EQ(cropped_input_height, height_); 145 RTC_CHECK_EQ(cropped_input_height, height_);
79 146
80 // Here we use Bind magic to add a reference count to |this| until the newly 147 // Here we use Bind magic to add a reference count to |this| until the newly
81 // created AndroidTextureBuffer is destructed. ScaledFrameNotInUse will be 148 // created AndroidTextureBuffer is destructed. ScaledFrameNotInUse will be
82 // called that happens and when it finishes, the reference count to |this| 149 // called that happens and when it finishes, the reference count to |this|
83 // will be decreased by one. 150 // will be decreased by one.
84 return new rtc::RefCountedObject<AndroidTextureBuffer>( 151 return new rtc::RefCountedObject<AndroidTextureBuffer>(
85 dst_widht, dst_height, native_handle_, 152 dst_widht, dst_height, native_handle_, surface_texture_helper_,
86 rtc::KeepRefUntilDone(this)); 153 rtc::KeepRefUntilDone(this));
87 } 154 }
88 155
89 } // namespace webrtc_jni 156 } // namespace webrtc_jni
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698