| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 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/sdk/android/src/jni/androidvideotracksource.h" | 11 #include "webrtc/sdk/android/src/jni/androidvideotracksource.h" |
| 12 | 12 |
| 13 #include <utility> | 13 #include <utility> |
| 14 | 14 |
| 15 #include "webrtc/rtc_base/logging.h" | 15 #include "webrtc/rtc_base/logging.h" |
| 16 #include "webrtc/sdk/android/src/jni/classreferenceholder.h" | 16 #include "webrtc/sdk/android/src/jni/classreferenceholder.h" |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 // MediaCodec wants resolution to be divisible by 2. | 19 // MediaCodec wants resolution to be divisible by 2. |
| 20 const int kRequiredResolutionAlignment = 2; | 20 const int kRequiredResolutionAlignment = 2; |
| 21 } | 21 } |
| 22 | 22 |
| 23 namespace webrtc { | 23 namespace webrtc { |
| 24 namespace jni { |
| 24 | 25 |
| 25 AndroidVideoTrackSource::AndroidVideoTrackSource( | 26 AndroidVideoTrackSource::AndroidVideoTrackSource( |
| 26 rtc::Thread* signaling_thread, | 27 rtc::Thread* signaling_thread, |
| 27 JNIEnv* jni, | 28 JNIEnv* jni, |
| 28 jobject j_surface_texture_helper, | 29 jobject j_surface_texture_helper, |
| 29 bool is_screencast) | 30 bool is_screencast) |
| 30 : AdaptedVideoTrackSource(kRequiredResolutionAlignment), | 31 : AdaptedVideoTrackSource(kRequiredResolutionAlignment), |
| 31 signaling_thread_(signaling_thread), | 32 signaling_thread_(signaling_thread), |
| 32 surface_texture_helper_( | 33 surface_texture_helper_(new rtc::RefCountedObject<SurfaceTextureHelper>( |
| 33 new rtc::RefCountedObject<webrtc_jni::SurfaceTextureHelper>( | 34 jni, |
| 34 jni, | 35 j_surface_texture_helper)), |
| 35 j_surface_texture_helper)), | |
| 36 video_buffer_factory_(jni), | 36 video_buffer_factory_(jni), |
| 37 is_screencast_(is_screencast) { | 37 is_screencast_(is_screencast) { |
| 38 LOG(LS_INFO) << "AndroidVideoTrackSource ctor"; | 38 LOG(LS_INFO) << "AndroidVideoTrackSource ctor"; |
| 39 camera_thread_checker_.DetachFromThread(); | 39 camera_thread_checker_.DetachFromThread(); |
| 40 | 40 |
| 41 jclass j_video_frame_buffer_class = | 41 jclass j_video_frame_buffer_class = |
| 42 webrtc_jni::FindClass(jni, "org/webrtc/VideoFrame$Buffer"); | 42 FindClass(jni, "org/webrtc/VideoFrame$Buffer"); |
| 43 j_crop_and_scale_id_ = | 43 j_crop_and_scale_id_ = |
| 44 jni->GetMethodID(j_video_frame_buffer_class, "cropAndScale", | 44 jni->GetMethodID(j_video_frame_buffer_class, "cropAndScale", |
| 45 "(IIIIII)Lorg/webrtc/VideoFrame$Buffer;"); | 45 "(IIIIII)Lorg/webrtc/VideoFrame$Buffer;"); |
| 46 } | 46 } |
| 47 | 47 |
| 48 void AndroidVideoTrackSource::SetState(SourceState state) { | 48 void AndroidVideoTrackSource::SetState(SourceState state) { |
| 49 if (rtc::Thread::Current() != signaling_thread_) { | 49 if (rtc::Thread::Current() != signaling_thread_) { |
| 50 invoker_.AsyncInvoke<void>( | 50 invoker_.AsyncInvoke<void>( |
| 51 RTC_FROM_HERE, signaling_thread_, | 51 RTC_FROM_HERE, signaling_thread_, |
| 52 rtc::Bind(&AndroidVideoTrackSource::SetState, this, state)); | 52 rtc::Bind(&AndroidVideoTrackSource::SetState, this, state)); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 buffer->StrideU(), buffer->width(), buffer->height()); | 108 buffer->StrideU(), buffer->width(), buffer->height()); |
| 109 | 109 |
| 110 OnFrame(VideoFrame(buffer, rotation, translated_camera_time_us)); | 110 OnFrame(VideoFrame(buffer, rotation, translated_camera_time_us)); |
| 111 } | 111 } |
| 112 | 112 |
| 113 void AndroidVideoTrackSource::OnTextureFrameCaptured( | 113 void AndroidVideoTrackSource::OnTextureFrameCaptured( |
| 114 int width, | 114 int width, |
| 115 int height, | 115 int height, |
| 116 VideoRotation rotation, | 116 VideoRotation rotation, |
| 117 int64_t timestamp_ns, | 117 int64_t timestamp_ns, |
| 118 const webrtc_jni::NativeHandleImpl& handle) { | 118 const NativeHandleImpl& handle) { |
| 119 RTC_DCHECK(camera_thread_checker_.CalledOnValidThread()); | 119 RTC_DCHECK(camera_thread_checker_.CalledOnValidThread()); |
| 120 | 120 |
| 121 int64_t camera_time_us = timestamp_ns / rtc::kNumNanosecsPerMicrosec; | 121 int64_t camera_time_us = timestamp_ns / rtc::kNumNanosecsPerMicrosec; |
| 122 int64_t translated_camera_time_us = | 122 int64_t translated_camera_time_us = |
| 123 timestamp_aligner_.TranslateTimestamp(camera_time_us, rtc::TimeMicros()); | 123 timestamp_aligner_.TranslateTimestamp(camera_time_us, rtc::TimeMicros()); |
| 124 | 124 |
| 125 int adapted_width; | 125 int adapted_width; |
| 126 int adapted_height; | 126 int adapted_height; |
| 127 int crop_width; | 127 int crop_width; |
| 128 int crop_height; | 128 int crop_height; |
| 129 int crop_x; | 129 int crop_x; |
| 130 int crop_y; | 130 int crop_y; |
| 131 | 131 |
| 132 if (!AdaptFrame(width, height, camera_time_us, &adapted_width, | 132 if (!AdaptFrame(width, height, camera_time_us, &adapted_width, |
| 133 &adapted_height, &crop_width, &crop_height, &crop_x, | 133 &adapted_height, &crop_width, &crop_height, &crop_x, |
| 134 &crop_y)) { | 134 &crop_y)) { |
| 135 surface_texture_helper_->ReturnTextureFrame(); | 135 surface_texture_helper_->ReturnTextureFrame(); |
| 136 return; | 136 return; |
| 137 } | 137 } |
| 138 | 138 |
| 139 webrtc_jni::Matrix matrix = handle.sampling_matrix; | 139 Matrix matrix = handle.sampling_matrix; |
| 140 | 140 |
| 141 matrix.Crop(crop_width / static_cast<float>(width), | 141 matrix.Crop(crop_width / static_cast<float>(width), |
| 142 crop_height / static_cast<float>(height), | 142 crop_height / static_cast<float>(height), |
| 143 crop_x / static_cast<float>(width), | 143 crop_x / static_cast<float>(width), |
| 144 crop_y / static_cast<float>(height)); | 144 crop_y / static_cast<float>(height)); |
| 145 | 145 |
| 146 // Note that apply_rotation() may change under our feet, so we should only | 146 // Note that apply_rotation() may change under our feet, so we should only |
| 147 // check once. | 147 // check once. |
| 148 if (apply_rotation()) { | 148 if (apply_rotation()) { |
| 149 if (rotation == kVideoRotation_90 || rotation == kVideoRotation_270) { | 149 if (rotation == kVideoRotation_90 || rotation == kVideoRotation_270) { |
| 150 std::swap(adapted_width, adapted_height); | 150 std::swap(adapted_width, adapted_height); |
| 151 } | 151 } |
| 152 matrix.Rotate(rotation); | 152 matrix.Rotate(rotation); |
| 153 rotation = kVideoRotation_0; | 153 rotation = kVideoRotation_0; |
| 154 } | 154 } |
| 155 | 155 |
| 156 OnFrame(VideoFrame( | 156 OnFrame(VideoFrame(surface_texture_helper_->CreateTextureFrame( |
| 157 surface_texture_helper_->CreateTextureFrame( | 157 adapted_width, adapted_height, |
| 158 adapted_width, adapted_height, | 158 NativeHandleImpl(handle.oes_texture_id, matrix)), |
| 159 webrtc_jni::NativeHandleImpl(handle.oes_texture_id, matrix)), | 159 rotation, translated_camera_time_us)); |
| 160 rotation, translated_camera_time_us)); | |
| 161 } | 160 } |
| 162 | 161 |
| 163 void AndroidVideoTrackSource::OnFrameCaptured(JNIEnv* jni, | 162 void AndroidVideoTrackSource::OnFrameCaptured(JNIEnv* jni, |
| 164 int width, | 163 int width, |
| 165 int height, | 164 int height, |
| 166 int64_t timestamp_ns, | 165 int64_t timestamp_ns, |
| 167 VideoRotation rotation, | 166 VideoRotation rotation, |
| 168 jobject j_video_frame_buffer) { | 167 jobject j_video_frame_buffer) { |
| 169 RTC_DCHECK(camera_thread_checker_.CalledOnValidThread()); | 168 RTC_DCHECK(camera_thread_checker_.CalledOnValidThread()); |
| 170 | 169 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 | 201 |
| 203 void AndroidVideoTrackSource::OnOutputFormatRequest(int width, | 202 void AndroidVideoTrackSource::OnOutputFormatRequest(int width, |
| 204 int height, | 203 int height, |
| 205 int fps) { | 204 int fps) { |
| 206 cricket::VideoFormat format(width, height, | 205 cricket::VideoFormat format(width, height, |
| 207 cricket::VideoFormat::FpsToInterval(fps), 0); | 206 cricket::VideoFormat::FpsToInterval(fps), 0); |
| 208 video_adapter()->OnOutputFormatRequest(format); | 207 video_adapter()->OnOutputFormatRequest(format); |
| 209 } | 208 } |
| 210 | 209 |
| 211 } // namespace webrtc | 210 } // namespace webrtc |
| 211 } // namespace webrtc |
| OLD | NEW |