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 | 17 |
17 namespace { | 18 namespace { |
18 // MediaCodec wants resolution to be divisible by 2. | 19 // MediaCodec wants resolution to be divisible by 2. |
19 const int kRequiredResolutionAlignment = 2; | 20 const int kRequiredResolutionAlignment = 2; |
20 } | 21 } |
21 | 22 |
22 namespace webrtc { | 23 namespace webrtc { |
23 | 24 |
24 AndroidVideoTrackSource::AndroidVideoTrackSource( | 25 AndroidVideoTrackSource::AndroidVideoTrackSource( |
25 rtc::Thread* signaling_thread, | 26 rtc::Thread* signaling_thread, |
26 JNIEnv* jni, | 27 JNIEnv* jni, |
27 jobject j_surface_texture_helper, | 28 jobject j_surface_texture_helper, |
28 bool is_screencast) | 29 bool is_screencast) |
29 : AdaptedVideoTrackSource(kRequiredResolutionAlignment), | 30 : AdaptedVideoTrackSource(kRequiredResolutionAlignment), |
30 signaling_thread_(signaling_thread), | 31 signaling_thread_(signaling_thread), |
31 surface_texture_helper_( | 32 surface_texture_helper_( |
32 new rtc::RefCountedObject<webrtc_jni::SurfaceTextureHelper>( | 33 new rtc::RefCountedObject<webrtc_jni::SurfaceTextureHelper>( |
33 jni, | 34 jni, |
34 j_surface_texture_helper)), | 35 j_surface_texture_helper)), |
| 36 video_buffer_factory_(jni), |
35 is_screencast_(is_screencast) { | 37 is_screencast_(is_screencast) { |
36 LOG(LS_INFO) << "AndroidVideoTrackSource ctor"; | 38 LOG(LS_INFO) << "AndroidVideoTrackSource ctor"; |
37 camera_thread_checker_.DetachFromThread(); | 39 camera_thread_checker_.DetachFromThread(); |
| 40 |
| 41 jclass j_video_frame_buffer_class = |
| 42 webrtc_jni::FindClass(jni, "org/webrtc/VideoFrame$Buffer"); |
| 43 j_crop_and_scale_id_ = |
| 44 jni->GetMethodID(j_video_frame_buffer_class, "cropAndScale", |
| 45 "(IIIIII)Lorg/webrtc/VideoFrame$Buffer;"); |
38 } | 46 } |
39 | 47 |
40 void AndroidVideoTrackSource::SetState(SourceState state) { | 48 void AndroidVideoTrackSource::SetState(SourceState state) { |
41 if (rtc::Thread::Current() != signaling_thread_) { | 49 if (rtc::Thread::Current() != signaling_thread_) { |
42 invoker_.AsyncInvoke<void>( | 50 invoker_.AsyncInvoke<void>( |
43 RTC_FROM_HERE, signaling_thread_, | 51 RTC_FROM_HERE, signaling_thread_, |
44 rtc::Bind(&AndroidVideoTrackSource::SetState, this, state)); | 52 rtc::Bind(&AndroidVideoTrackSource::SetState, this, state)); |
45 return; | 53 return; |
46 } | 54 } |
47 | 55 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 rotation = kVideoRotation_0; | 153 rotation = kVideoRotation_0; |
146 } | 154 } |
147 | 155 |
148 OnFrame(VideoFrame( | 156 OnFrame(VideoFrame( |
149 surface_texture_helper_->CreateTextureFrame( | 157 surface_texture_helper_->CreateTextureFrame( |
150 adapted_width, adapted_height, | 158 adapted_width, adapted_height, |
151 webrtc_jni::NativeHandleImpl(handle.oes_texture_id, matrix)), | 159 webrtc_jni::NativeHandleImpl(handle.oes_texture_id, matrix)), |
152 rotation, translated_camera_time_us)); | 160 rotation, translated_camera_time_us)); |
153 } | 161 } |
154 | 162 |
| 163 void AndroidVideoTrackSource::OnFrameCaptured(JNIEnv* jni, |
| 164 int width, |
| 165 int height, |
| 166 int64_t timestamp_ns, |
| 167 VideoRotation rotation, |
| 168 jobject j_video_frame_buffer) { |
| 169 RTC_DCHECK(camera_thread_checker_.CalledOnValidThread()); |
| 170 |
| 171 int64_t camera_time_us = timestamp_ns / rtc::kNumNanosecsPerMicrosec; |
| 172 int64_t translated_camera_time_us = |
| 173 timestamp_aligner_.TranslateTimestamp(camera_time_us, rtc::TimeMicros()); |
| 174 |
| 175 int adapted_width; |
| 176 int adapted_height; |
| 177 int crop_width; |
| 178 int crop_height; |
| 179 int crop_x; |
| 180 int crop_y; |
| 181 |
| 182 if (!AdaptFrame(width, height, camera_time_us, &adapted_width, |
| 183 &adapted_height, &crop_width, &crop_height, &crop_x, |
| 184 &crop_y)) { |
| 185 return; |
| 186 } |
| 187 |
| 188 jobject j_adapted_video_frame_buffer = jni->CallObjectMethod( |
| 189 j_video_frame_buffer, j_crop_and_scale_id_, crop_x, crop_y, crop_width, |
| 190 crop_height, adapted_width, adapted_height); |
| 191 |
| 192 rtc::scoped_refptr<VideoFrameBuffer> buffer = |
| 193 video_buffer_factory_.WrapBuffer(jni, j_adapted_video_frame_buffer); |
| 194 |
| 195 // AdaptedVideoTrackSource handles applying rotation for I420 frames. |
| 196 if (apply_rotation()) { |
| 197 buffer = buffer->ToI420(); |
| 198 } |
| 199 |
| 200 OnFrame(VideoFrame(buffer, rotation, translated_camera_time_us)); |
| 201 } |
| 202 |
155 void AndroidVideoTrackSource::OnOutputFormatRequest(int width, | 203 void AndroidVideoTrackSource::OnOutputFormatRequest(int width, |
156 int height, | 204 int height, |
157 int fps) { | 205 int fps) { |
158 cricket::VideoFormat format(width, height, | 206 cricket::VideoFormat format(width, height, |
159 cricket::VideoFormat::FpsToInterval(fps), 0); | 207 cricket::VideoFormat::FpsToInterval(fps), 0); |
160 video_adapter()->OnOutputFormatRequest(format); | 208 video_adapter()->OnOutputFormatRequest(format); |
161 } | 209 } |
162 | 210 |
163 } // namespace webrtc | 211 } // namespace webrtc |
OLD | NEW |