Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/api/java/jni/androidvideocapturer_jni.h" | 11 #include "webrtc/api/java/jni/androidvideocapturer_jni.h" |
| 12 #include "webrtc/api/java/jni/classreferenceholder.h" | 12 #include "webrtc/api/java/jni/classreferenceholder.h" |
| 13 #include "webrtc/api/java/jni/native_handle_impl.h" | 13 #include "webrtc/api/java/jni/native_handle_impl.h" |
| 14 #include "webrtc/api/java/jni/surfacetexturehelper_jni.h" | 14 #include "webrtc/api/java/jni/surfacetexturehelper_jni.h" |
| 15 #include "third_party/libyuv/include/libyuv/convert.h" | 15 #include "third_party/libyuv/include/libyuv/convert.h" |
| 16 #include "third_party/libyuv/include/libyuv/scale.h" | |
| 16 #include "webrtc/base/bind.h" | 17 #include "webrtc/base/bind.h" |
| 17 | 18 |
| 18 namespace webrtc_jni { | 19 namespace webrtc_jni { |
| 19 | 20 |
| 20 jobject AndroidVideoCapturerJni::application_context_ = nullptr; | 21 jobject AndroidVideoCapturerJni::application_context_ = nullptr; |
| 21 | 22 |
| 22 // static | 23 // static |
| 23 int AndroidVideoCapturerJni::SetAndroidObjects(JNIEnv* jni, | 24 int AndroidVideoCapturerJni::SetAndroidObjects(JNIEnv* jni, |
| 24 jobject appliction_context) { | 25 jobject appliction_context) { |
| 25 if (application_context_) { | 26 if (application_context_) { |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 162 &webrtc::AndroidVideoCapturer::OnCapturerStarted, | 163 &webrtc::AndroidVideoCapturer::OnCapturerStarted, |
| 163 success); | 164 success); |
| 164 } | 165 } |
| 165 | 166 |
| 166 void AndroidVideoCapturerJni::OnMemoryBufferFrame(void* video_frame, | 167 void AndroidVideoCapturerJni::OnMemoryBufferFrame(void* video_frame, |
| 167 int length, | 168 int length, |
| 168 int width, | 169 int width, |
| 169 int height, | 170 int height, |
| 170 int rotation, | 171 int rotation, |
| 171 int64_t timestamp_ns) { | 172 int64_t timestamp_ns) { |
| 172 const uint8_t* y_plane = static_cast<uint8_t*>(video_frame); | 173 RTC_DCHECK(rotation == 0 || rotation == 90 || rotation == 180 || |
| 173 const uint8_t* vu_plane = y_plane + width * height; | 174 rotation == 270); |
| 175 rtc::CritScope cs(&capturer_lock_); | |
| 176 | |
| 177 int adapted_width; | |
| 178 int adapted_height; | |
| 179 int crop_width; | |
| 180 int crop_height; | |
| 181 int crop_x; | |
| 182 int crop_y; | |
| 183 | |
| 184 if (!capturer_->AdaptFrame(width, height, &adapted_width, &adapted_height, | |
| 185 &crop_width, &crop_height, &crop_x, &crop_y)) { | |
| 186 return; | |
| 187 } | |
| 188 | |
| 189 int rotated_width = crop_width; | |
| 190 int rotated_height = crop_height; | |
| 191 | |
| 192 if (capturer_->apply_rotation() && (rotation == 90 || rotation == 270)) { | |
| 193 std::swap(adapted_width, adapted_height); | |
| 194 std::swap(rotated_width, rotated_height); | |
| 195 } | |
| 174 | 196 |
| 175 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer = | 197 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer = |
| 176 buffer_pool_.CreateBuffer(width, height); | 198 pre_scale_pool_.CreateBuffer(rotated_width, rotated_height); |
| 177 libyuv::NV21ToI420( | 199 |
| 178 y_plane, width, | 200 const uint8_t* y_plane = static_cast<const uint8_t*>(video_frame); |
| 179 vu_plane, width, | 201 const uint8_t* uv_plane = y_plane + width * height; |
| 202 | |
| 203 // Can only crop at even pixels. | |
| 204 crop_x &= ~1; | |
| 205 crop_y &= ~1; | |
| 206 int uv_width = (width + 1)/2; | |
|
magjed_webrtc
2016/05/25 12:36:40
nit: (width + 1) / 2;
| |
| 207 | |
| 208 libyuv::NV12ToI420Rotate( | |
| 209 y_plane + width * crop_y + crop_x, width, | |
| 210 uv_plane + uv_width * crop_y + crop_x, width, | |
| 180 buffer->MutableData(webrtc::kYPlane), buffer->stride(webrtc::kYPlane), | 211 buffer->MutableData(webrtc::kYPlane), buffer->stride(webrtc::kYPlane), |
| 212 // Swap U and V, since we have NV21, not NV12. | |
| 213 buffer->MutableData(webrtc::kVPlane), buffer->stride(webrtc::kVPlane), | |
| 181 buffer->MutableData(webrtc::kUPlane), buffer->stride(webrtc::kUPlane), | 214 buffer->MutableData(webrtc::kUPlane), buffer->stride(webrtc::kUPlane), |
| 182 buffer->MutableData(webrtc::kVPlane), buffer->stride(webrtc::kVPlane), | 215 crop_width, crop_height, static_cast<libyuv::RotationMode>( |
| 183 width, height); | 216 capturer_->apply_rotation() ? rotation : 0)); |
| 184 AsyncCapturerInvoke("OnIncomingFrame", | 217 |
| 185 &webrtc::AndroidVideoCapturer::OnIncomingFrame, | 218 if (adapted_width != rotated_width || adapted_height != rotated_height) { |
| 186 buffer, rotation, timestamp_ns); | 219 rtc::scoped_refptr<webrtc::VideoFrameBuffer> scaled = |
| 220 post_scale_pool_.CreateBuffer(adapted_width, adapted_height); | |
| 221 // TODO(nisse): This should be done by some Scale method in | |
| 222 // I420Buffer, but we can't do that right now, since | |
| 223 // I420BufferPool uses a wrapper object. | |
| 224 if (libyuv::I420Scale(buffer->DataY(), buffer->StrideY(), | |
| 225 buffer->DataU(), buffer->StrideU(), | |
| 226 buffer->DataV(), buffer->StrideV(), | |
| 227 rotated_width, rotated_height, | |
| 228 scaled->MutableDataY(), scaled->StrideY(), | |
| 229 scaled->MutableDataU(), scaled->StrideU(), | |
| 230 scaled->MutableDataV(), scaled->StrideV(), | |
| 231 adapted_width, adapted_height, | |
| 232 libyuv::kFilterBox) < 0) { | |
| 233 LOG(LS_WARNING) << "I420Scale failed"; | |
| 234 return; | |
| 235 } | |
| 236 buffer = scaled; | |
| 237 } | |
| 238 // TODO(nisse): Use microsecond time instead. | |
| 239 capturer_->OnFrame(cricket::WebRtcVideoFrame( | |
| 240 buffer, timestamp_ns, | |
| 241 capturer_->apply_rotation() | |
| 242 ? webrtc::kVideoRotation_0 | |
| 243 : static_cast<webrtc::VideoRotation>(rotation)), | |
| 244 width, height); | |
| 187 } | 245 } |
| 188 | 246 |
| 189 void AndroidVideoCapturerJni::OnTextureFrame(int width, | 247 void AndroidVideoCapturerJni::OnTextureFrame(int width, |
| 190 int height, | 248 int height, |
| 191 int rotation, | 249 int rotation, |
| 192 int64_t timestamp_ns, | 250 int64_t timestamp_ns, |
| 193 const NativeHandleImpl& handle) { | 251 const NativeHandleImpl& handle) { |
| 194 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer( | 252 RTC_DCHECK(rotation == 0 || rotation == 90 || rotation == 180 || |
| 195 surface_texture_helper_->CreateTextureFrame(width, height, handle)); | 253 rotation == 270); |
| 254 rtc::CritScope cs(&capturer_lock_); | |
| 196 | 255 |
| 197 AsyncCapturerInvoke("OnIncomingFrame", | 256 int adapted_width; |
| 198 &webrtc::AndroidVideoCapturer::OnIncomingFrame, | 257 int adapted_height; |
| 199 buffer, rotation, timestamp_ns); | 258 int crop_width; |
| 259 int crop_height; | |
| 260 int crop_x; | |
| 261 int crop_y; | |
| 262 | |
| 263 if (!capturer_->AdaptFrame(width, height, &adapted_width, &adapted_height, | |
| 264 &crop_width, &crop_height, &crop_x, &crop_y)) { | |
| 265 return; | |
| 266 } | |
| 267 | |
| 268 Matrix matrix = handle.sampling_matrix; | |
| 269 | |
| 270 matrix.Crop(crop_width / static_cast<float>(width), | |
| 271 crop_height / static_cast<float>(height), | |
| 272 crop_x / static_cast<float>(width), | |
| 273 crop_y / static_cast<float>(height)); | |
| 274 | |
| 275 if (capturer_->apply_rotation()) { | |
| 276 if (rotation == webrtc::kVideoRotation_90 || | |
| 277 rotation == webrtc::kVideoRotation_270) { | |
| 278 std::swap(adapted_width, adapted_height); | |
| 279 } | |
| 280 matrix.Rotate(static_cast<webrtc::VideoRotation>(rotation)); | |
| 281 } | |
| 282 | |
| 283 // TODO(nisse): Use microsecond time instead. | |
| 284 capturer_->OnFrame( | |
| 285 cricket::WebRtcVideoFrame( | |
| 286 surface_texture_helper_->CreateTextureFrame( | |
| 287 adapted_width, adapted_height, | |
| 288 NativeHandleImpl(handle.oes_texture_id, matrix)), | |
| 289 timestamp_ns, capturer_->apply_rotation() | |
| 290 ? webrtc::kVideoRotation_0 | |
| 291 : static_cast<webrtc::VideoRotation>(rotation)), | |
| 292 width, height); | |
| 200 } | 293 } |
| 201 | 294 |
| 202 void AndroidVideoCapturerJni::OnOutputFormatRequest(int width, | 295 void AndroidVideoCapturerJni::OnOutputFormatRequest(int width, |
| 203 int height, | 296 int height, |
| 204 int fps) { | 297 int fps) { |
| 205 AsyncCapturerInvoke("OnOutputFormatRequest", | 298 AsyncCapturerInvoke("OnOutputFormatRequest", |
| 206 &webrtc::AndroidVideoCapturer::OnOutputFormatRequest, | 299 &webrtc::AndroidVideoCapturer::OnOutputFormatRequest, |
| 207 width, height, fps); | 300 width, height, fps); |
| 208 } | 301 } |
| 209 | 302 |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 239 | 332 |
| 240 JOW(void, VideoCapturer_00024NativeObserver_nativeOnOutputFormatRequest) | 333 JOW(void, VideoCapturer_00024NativeObserver_nativeOnOutputFormatRequest) |
| 241 (JNIEnv* jni, jclass, jlong j_capturer, jint j_width, jint j_height, | 334 (JNIEnv* jni, jclass, jlong j_capturer, jint j_width, jint j_height, |
| 242 jint j_fps) { | 335 jint j_fps) { |
| 243 LOG(LS_INFO) << "NativeObserver_nativeOnOutputFormatRequest"; | 336 LOG(LS_INFO) << "NativeObserver_nativeOnOutputFormatRequest"; |
| 244 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnOutputFormatRequest( | 337 reinterpret_cast<AndroidVideoCapturerJni*>(j_capturer)->OnOutputFormatRequest( |
| 245 j_width, j_height, j_fps); | 338 j_width, j_height, j_fps); |
| 246 } | 339 } |
| 247 | 340 |
| 248 } // namespace webrtc_jni | 341 } // namespace webrtc_jni |
| OLD | NEW |