From 3093ef193e565b906453992c82277d4fc9aef5a0 Mon Sep 17 00:00:00 2001 From: Magnus Jedvert Date: Mon, 19 Jun 2017 15:04:19 +0200 Subject: [PATCH] Android JNI: Clean up AndroidVideoTrackSource and NativeHandleImpl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I'm preparing adding support for Java VideoFrames in AndroidVideoTrackSource. I split out small unrelated clean-ups into this CL in order to make the big CL more focused. Bug: webrtc:7749 Change-Id: Ib261ab8eb055898b39307d4e78935bf60d323820 Reviewed-on: https://chromium-review.googlesource.com/539638 Reviewed-by: Sami Kalliomäki Commit-Queue: Magnus Jedvert Cr-Commit-Position: refs/heads/master@{#18660} --- .../src/jni/androidvideotracksource.cc | 31 +++++++------------ .../android/src/jni/androidvideotracksource.h | 8 ++--- .../src/jni/androidvideotracksource_jni.cc | 13 ++++++-- .../sdk/android/src/jni/native_handle_impl.cc | 4 +-- 4 files changed, 27 insertions(+), 29 deletions(-) diff --git a/webrtc/sdk/android/src/jni/androidvideotracksource.cc b/webrtc/sdk/android/src/jni/androidvideotracksource.cc index fbe9b03cda..c1431bd9ac 100644 --- a/webrtc/sdk/android/src/jni/androidvideotracksource.cc +++ b/webrtc/sdk/android/src/jni/androidvideotracksource.cc @@ -55,11 +55,9 @@ void AndroidVideoTrackSource::OnByteBufferFrameCaptured(const void* frame_data, int length, int width, int height, - int rotation, + VideoRotation rotation, int64_t timestamp_ns) { RTC_DCHECK(camera_thread_checker_.CalledOnValidThread()); - RTC_DCHECK(rotation == 0 || rotation == 90 || rotation == 180 || - rotation == 270); int64_t camera_time_us = timestamp_ns / rtc::kNumNanosecsPerMicrosec; int64_t translated_camera_time_us = @@ -91,7 +89,7 @@ void AndroidVideoTrackSource::OnByteBufferFrameCaptured(const void* frame_data, y_plane += width * crop_y + crop_x; uv_plane += uv_width * crop_y + crop_x; - rtc::scoped_refptr buffer = + rtc::scoped_refptr buffer = buffer_pool_.CreateBuffer(adapted_width, adapted_height); nv12toi420_scaler_.NV12ToI420Scale( @@ -101,19 +99,16 @@ void AndroidVideoTrackSource::OnByteBufferFrameCaptured(const void* frame_data, buffer->MutableDataV(), buffer->StrideV(), buffer->MutableDataU(), buffer->StrideU(), buffer->width(), buffer->height()); - OnFrame(VideoFrame(buffer, static_cast(rotation), - translated_camera_time_us)); + OnFrame(VideoFrame(buffer, rotation, translated_camera_time_us)); } void AndroidVideoTrackSource::OnTextureFrameCaptured( int width, int height, - int rotation, + VideoRotation rotation, int64_t timestamp_ns, const webrtc_jni::NativeHandleImpl& handle) { RTC_DCHECK(camera_thread_checker_.CalledOnValidThread()); - RTC_DCHECK(rotation == 0 || rotation == 90 || rotation == 180 || - rotation == 270); int64_t camera_time_us = timestamp_ns / rtc::kNumNanosecsPerMicrosec; int64_t translated_camera_time_us = @@ -140,25 +135,21 @@ void AndroidVideoTrackSource::OnTextureFrameCaptured( crop_x / static_cast(width), crop_y / static_cast(height)); - // Make a local copy, since value of apply_rotation() may change - // under our feet. - bool do_rotate = apply_rotation(); - - if (do_rotate) { - if (rotation == webrtc::kVideoRotation_90 || - rotation == webrtc::kVideoRotation_270) { + // Note that apply_rotation() may change under our feet, so we should only + // check once. + if (apply_rotation()) { + if (rotation == kVideoRotation_90 || rotation == kVideoRotation_270) { std::swap(adapted_width, adapted_height); } - matrix.Rotate(static_cast(rotation)); + matrix.Rotate(rotation); + rotation = kVideoRotation_0; } OnFrame(VideoFrame( surface_texture_helper_->CreateTextureFrame( adapted_width, adapted_height, webrtc_jni::NativeHandleImpl(handle.oes_texture_id, matrix)), - do_rotate ? webrtc::kVideoRotation_0 - : static_cast(rotation), - translated_camera_time_us)); + rotation, translated_camera_time_us)); } void AndroidVideoTrackSource::OnOutputFormatRequest(int width, diff --git a/webrtc/sdk/android/src/jni/androidvideotracksource.h b/webrtc/sdk/android/src/jni/androidvideotracksource.h index b68a1d51b4..dd4fe97f8e 100644 --- a/webrtc/sdk/android/src/jni/androidvideotracksource.h +++ b/webrtc/sdk/android/src/jni/androidvideotracksource.h @@ -50,12 +50,12 @@ class AndroidVideoTrackSource : public rtc::AdaptedVideoTrackSource { int length, int width, int height, - int rotation, + VideoRotation rotation, int64_t timestamp_ns); void OnTextureFrameCaptured(int width, int height, - int rotation, + VideoRotation rotation, int64_t timestamp_ns, const webrtc_jni::NativeHandleImpl& handle); @@ -73,8 +73,8 @@ class AndroidVideoTrackSource : public rtc::AdaptedVideoTrackSource { SourceState state_; rtc::VideoBroadcaster broadcaster_; rtc::TimestampAligner timestamp_aligner_; - webrtc::NV12ToI420Scaler nv12toi420_scaler_; - webrtc::I420BufferPool buffer_pool_; + NV12ToI420Scaler nv12toi420_scaler_; + I420BufferPool buffer_pool_; rtc::scoped_refptr surface_texture_helper_; const bool is_screencast_; }; diff --git a/webrtc/sdk/android/src/jni/androidvideotracksource_jni.cc b/webrtc/sdk/android/src/jni/androidvideotracksource_jni.cc index 9b2bc6ef6c..519a16d5f5 100644 --- a/webrtc/sdk/android/src/jni/androidvideotracksource_jni.cc +++ b/webrtc/sdk/android/src/jni/androidvideotracksource_jni.cc @@ -8,6 +8,7 @@ * be found in the AUTHORS file in the root of the source tree. */ +#include "webrtc/api/video/video_rotation.h" #include "webrtc/api/videosourceproxy.h" #include "webrtc/base/logging.h" #include "webrtc/sdk/android/src/jni/androidvideotracksource.h" @@ -18,6 +19,12 @@ #define JOW_OBSERVER_METHOD(rettype, name) \ JOW(rettype, AndroidVideoTrackSourceObserver_##name) +static webrtc::VideoRotation jintToVideoRotation(jint rotation) { + RTC_DCHECK(rotation == 0 || rotation == 90 || rotation == 180 || + rotation == 270); + return static_cast(rotation); +} + namespace webrtc_jni { static webrtc::AndroidVideoTrackSource* AndroidVideoTrackSourceFromJavaProxy( @@ -40,8 +47,8 @@ JOW_OBSERVER_METHOD(void, nativeOnByteBufferFrameCaptured) webrtc::AndroidVideoTrackSource* source = AndroidVideoTrackSourceFromJavaProxy(j_source); jbyte* bytes = jni->GetByteArrayElements(j_frame, nullptr); - source->OnByteBufferFrameCaptured(bytes, length, width, height, rotation, - timestamp); + source->OnByteBufferFrameCaptured(bytes, length, width, height, + jintToVideoRotation(rotation), timestamp); jni->ReleaseByteArrayElements(j_frame, bytes, JNI_ABORT); } @@ -58,7 +65,7 @@ JOW_OBSERVER_METHOD(void, nativeOnTextureFrameCaptured) webrtc::AndroidVideoTrackSource* source = AndroidVideoTrackSourceFromJavaProxy(j_source); source->OnTextureFrameCaptured( - j_width, j_height, j_rotation, j_timestamp, + j_width, j_height, jintToVideoRotation(j_rotation), j_timestamp, NativeHandleImpl(jni, j_oes_texture_id, j_transform_matrix)); } diff --git a/webrtc/sdk/android/src/jni/native_handle_impl.cc b/webrtc/sdk/android/src/jni/native_handle_impl.cc index 7d028c0676..e2f33b7c2b 100644 --- a/webrtc/sdk/android/src/jni/native_handle_impl.cc +++ b/webrtc/sdk/android/src/jni/native_handle_impl.cc @@ -169,9 +169,9 @@ int AndroidTextureBuffer::height() const { } rtc::scoped_refptr AndroidTextureBuffer::ToI420() { - int uv_width = (width()+7) / 8; + int uv_width = (width() + 7) / 8; int stride = 8 * uv_width; - int uv_height = (height()+1)/2; + int uv_height = (height() + 1) / 2; size_t size = stride * (height() + uv_height); // The data is owned by the frame, and the normal case is that the // data is deleted by the frame's destructor callback.