blob: d232f5ebad4f54ef429abafd8cb5629c7251a990 [file] [log] [blame]
Sami Kalliomaki16032122016-07-20 16:13:08 +02001/*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/api/androidvideotracksource.h"
12
13#include <utility>
14
magjed36d38cb2016-09-09 09:09:46 -070015#include "third_party/libyuv/include/libyuv/convert.h"
16#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
17
Sami Kalliomaki16032122016-07-20 16:13:08 +020018namespace webrtc {
19
20AndroidVideoTrackSource::AndroidVideoTrackSource(rtc::Thread* signaling_thread,
21 JNIEnv* jni,
arsanyb75f2542016-08-31 18:50:52 -070022 jobject j_egl_context,
23 bool is_screencast)
Sami Kalliomaki16032122016-07-20 16:13:08 +020024 : signaling_thread_(signaling_thread),
25 surface_texture_helper_(webrtc_jni::SurfaceTextureHelper::create(
26 jni,
27 "Camera SurfaceTextureHelper",
arsanyb75f2542016-08-31 18:50:52 -070028 j_egl_context)),
29 is_screencast_(is_screencast) {
Sami Kalliomaki16032122016-07-20 16:13:08 +020030 LOG(LS_INFO) << "AndroidVideoTrackSource ctor";
31 worker_thread_checker_.DetachFromThread();
32 camera_thread_checker_.DetachFromThread();
33}
34
35bool AndroidVideoTrackSource::GetStats(AndroidVideoTrackSource::Stats* stats) {
36 rtc::CritScope lock(&stats_crit_);
37
38 if (!stats_) {
39 return false;
40 }
41
42 *stats = *stats_;
43 return true;
44}
45
46void AndroidVideoTrackSource::SetState(SourceState state) {
47 if (rtc::Thread::Current() != signaling_thread_) {
48 invoker_.AsyncInvoke<void>(
49 RTC_FROM_HERE, signaling_thread_,
50 rtc::Bind(&AndroidVideoTrackSource::SetState, this, state));
51 return;
52 }
53
54 if (state_ != state) {
55 state_ = state;
56 FireOnChanged();
57 }
58}
59
60void AndroidVideoTrackSource::AddOrUpdateSink(
61 rtc::VideoSinkInterface<cricket::VideoFrame>* sink,
62 const rtc::VideoSinkWants& wants) {
63 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
64
65 broadcaster_.AddOrUpdateSink(sink, wants);
66 OnSinkWantsChanged(broadcaster_.wants());
67}
68
69void AndroidVideoTrackSource::RemoveSink(
70 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) {
71 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
72
73 broadcaster_.RemoveSink(sink);
74 OnSinkWantsChanged(broadcaster_.wants());
75}
76
77void AndroidVideoTrackSource::OnSinkWantsChanged(
78 const rtc::VideoSinkWants& wants) {
79 {
80 rtc::CritScope lock(&apply_rotation_crit_);
81 apply_rotation_ = wants.rotation_applied;
82 }
83
84 video_adapter_.OnResolutionRequest(wants.max_pixel_count,
85 wants.max_pixel_count_step_up);
86}
87
88void AndroidVideoTrackSource::OnByteBufferFrameCaptured(const void* frame_data,
89 int length,
90 int width,
91 int height,
92 int rotation,
93 int64_t timestamp_ns) {
94 RTC_DCHECK(camera_thread_checker_.CalledOnValidThread());
95 RTC_DCHECK(rotation == 0 || rotation == 90 || rotation == 180 ||
96 rotation == 270);
97
98 int adapted_width;
99 int adapted_height;
100 int crop_width;
101 int crop_height;
102 int crop_x;
103 int crop_y;
104 int64_t translated_camera_time_us;
105
106 if (!AdaptFrame(width, height, timestamp_ns / rtc::kNumNanosecsPerMicrosec,
107 &adapted_width, &adapted_height, &crop_width, &crop_height,
108 &crop_x, &crop_y, &translated_camera_time_us)) {
109 return;
110 }
111
Sami Kalliomaki16032122016-07-20 16:13:08 +0200112 const uint8_t* y_plane = static_cast<const uint8_t*>(frame_data);
113 const uint8_t* uv_plane = y_plane + width * height;
magjed36d38cb2016-09-09 09:09:46 -0700114 const int uv_width = (width + 1) / 2;
Sami Kalliomaki16032122016-07-20 16:13:08 +0200115
116 RTC_CHECK_GE(length, width * height + 2 * uv_width * ((height + 1) / 2));
117
118 // Can only crop at even pixels.
119 crop_x &= ~1;
120 crop_y &= ~1;
magjed36d38cb2016-09-09 09:09:46 -0700121 // Crop just by modifying pointers.
122 y_plane += width * crop_y + crop_x;
123 uv_plane += uv_width * crop_y + crop_x;
Sami Kalliomaki16032122016-07-20 16:13:08 +0200124
magjed36d38cb2016-09-09 09:09:46 -0700125 rtc::scoped_refptr<webrtc::I420Buffer> buffer =
126 buffer_pool_.CreateBuffer(adapted_width, adapted_height);
Sami Kalliomaki16032122016-07-20 16:13:08 +0200127
magjed36d38cb2016-09-09 09:09:46 -0700128 if (adapted_width == crop_width && adapted_height == crop_height) {
129 // No scaling.
130 libyuv::NV12ToI420(
131 y_plane, width,
132 uv_plane, uv_width * 2,
133 buffer->MutableDataY(), buffer->StrideY(),
134 // Swap U and V, since we have NV21, not NV12.
135 buffer->MutableDataV(), buffer->StrideV(),
136 buffer->MutableDataU(), buffer->StrideU(),
137 buffer->width(), buffer->height());
138
139 } else {
140 // Scaling.
141 const int crop_uv_width = (crop_width + 1) / 2;
142 const int crop_uv_height = (crop_height + 1) / 2;
143 unscaled_uv_planes_.resize(crop_uv_width * crop_uv_height * 2);
144
145 NV12ToI420Scale(
146 unscaled_uv_planes_.data(),
147 y_plane, width,
148 uv_plane, uv_width * 2,
149 crop_width, crop_height,
150 buffer->MutableDataY(), buffer->StrideY(),
151 // Swap U and V, since we have NV21, not NV12.
152 buffer->MutableDataV(), buffer->StrideV(),
153 buffer->MutableDataU(), buffer->StrideU(),
154 buffer->width(), buffer->height());
155 }
156
157 // Applying rotation is only supported for legacy reasons, and the performance
158 // for this path is not critical.
159 rtc::CritScope lock(&apply_rotation_crit_);
160 if (apply_rotation_ && rotation != 0) {
161 rtc::scoped_refptr<I420Buffer> rotated_buffer = I420Buffer::Create(
162 rotation == 180 ? buffer->width() : buffer->height(),
163 rotation == 180 ? buffer->height() : buffer->width());
164
165 libyuv::I420Rotate(
166 buffer->DataY(), buffer->StrideY(),
167 buffer->DataU(), buffer->StrideU(),
168 buffer->DataV(), buffer->StrideV(),
169 rotated_buffer->MutableDataY(), rotated_buffer->StrideY(),
170 rotated_buffer->MutableDataU(), rotated_buffer->StrideU(),
171 rotated_buffer->MutableDataV(), rotated_buffer->StrideV(),
172 buffer->width(), buffer->height(),
173 static_cast<libyuv::RotationMode>(rotation));
174
175 buffer = rotated_buffer;
Sami Kalliomaki16032122016-07-20 16:13:08 +0200176 }
177
178 OnFrame(cricket::WebRtcVideoFrame(
179 buffer,
180 apply_rotation_ ? webrtc::kVideoRotation_0
181 : static_cast<webrtc::VideoRotation>(rotation),
Sergey Ulanov19ee1e6eb2016-08-01 13:35:55 -0700182 translated_camera_time_us, 0),
Sami Kalliomaki16032122016-07-20 16:13:08 +0200183 width, height);
184}
185
186void AndroidVideoTrackSource::OnTextureFrameCaptured(
187 int width,
188 int height,
189 int rotation,
190 int64_t timestamp_ns,
191 const webrtc_jni::NativeHandleImpl& handle) {
192 RTC_DCHECK(camera_thread_checker_.CalledOnValidThread());
193 RTC_DCHECK(rotation == 0 || rotation == 90 || rotation == 180 ||
194 rotation == 270);
195
196 int adapted_width;
197 int adapted_height;
198 int crop_width;
199 int crop_height;
200 int crop_x;
201 int crop_y;
202 int64_t translated_camera_time_us;
203
204 if (!AdaptFrame(width, height, timestamp_ns / rtc::kNumNanosecsPerMicrosec,
205 &adapted_width, &adapted_height, &crop_width, &crop_height,
206 &crop_x, &crop_y, &translated_camera_time_us)) {
207 surface_texture_helper_->ReturnTextureFrame();
208 return;
209 }
210
211 webrtc_jni::Matrix matrix = handle.sampling_matrix;
212
213 matrix.Crop(crop_width / static_cast<float>(width),
214 crop_height / static_cast<float>(height),
215 crop_x / static_cast<float>(width),
216 crop_y / static_cast<float>(height));
217
218 rtc::CritScope lock(&apply_rotation_crit_);
219 if (apply_rotation_) {
220 if (rotation == webrtc::kVideoRotation_90 ||
221 rotation == webrtc::kVideoRotation_270) {
222 std::swap(adapted_width, adapted_height);
223 }
224 matrix.Rotate(static_cast<webrtc::VideoRotation>(rotation));
225 }
226
227 OnFrame(cricket::WebRtcVideoFrame(
228 surface_texture_helper_->CreateTextureFrame(
229 adapted_width, adapted_height,
230 webrtc_jni::NativeHandleImpl(handle.oes_texture_id, matrix)),
231 apply_rotation_ ? webrtc::kVideoRotation_0
232 : static_cast<webrtc::VideoRotation>(rotation),
Sergey Ulanov19ee1e6eb2016-08-01 13:35:55 -0700233 translated_camera_time_us, 0),
Sami Kalliomaki16032122016-07-20 16:13:08 +0200234 width, height);
235}
236
237void AndroidVideoTrackSource::OnFrame(const cricket::VideoFrame& frame,
238 int width,
239 int height) {
240 {
241 rtc::CritScope lock(&stats_crit_);
242 stats_ = rtc::Optional<AndroidVideoTrackSource::Stats>({width, height});
243 }
244
245 broadcaster_.OnFrame(frame);
246}
247
248void AndroidVideoTrackSource::OnOutputFormatRequest(int width,
249 int height,
250 int fps) {
251 RTC_DCHECK(camera_thread_checker_.CalledOnValidThread());
252
253 cricket::VideoFormat format(width, height,
254 cricket::VideoFormat::FpsToInterval(fps), 0);
255 video_adapter_.OnOutputFormatRequest(format);
256}
257
258bool AndroidVideoTrackSource::AdaptFrame(int width,
259 int height,
260 int64_t camera_time_us,
261 int* out_width,
262 int* out_height,
263 int* crop_width,
264 int* crop_height,
265 int* crop_x,
266 int* crop_y,
267 int64_t* translated_camera_time_us) {
268 RTC_DCHECK(camera_thread_checker_.CalledOnValidThread());
269
270 int64_t system_time_us = rtc::TimeMicros();
271
272 int64_t offset_us =
273 timestamp_aligner_.UpdateOffset(camera_time_us, system_time_us);
274
275 if (!broadcaster_.frame_wanted()) {
276 return false;
277 }
278
279 if (!video_adapter_.AdaptFrameResolution(
280 width, height, camera_time_us * rtc::kNumNanosecsPerMicrosec,
281 crop_width, crop_height, out_width, out_height)) {
282 // VideoAdapter dropped the frame.
283 return false;
284 }
285 *crop_x = (width - *crop_width) / 2;
286 *crop_y = (height - *crop_height) / 2;
287
288 *translated_camera_time_us = timestamp_aligner_.ClipTimestamp(
289 camera_time_us + offset_us, system_time_us);
290 return true;
291}
292
293} // namespace webrtc