blob: 6fe8c95784071091b476f49c0baacbd76aaddf08 [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
magjed1a7ef1f2016-09-17 02:39:03 -070015#include "third_party/libyuv/include/libyuv/rotate.h"
16
Sami Kalliomaki16032122016-07-20 16:13:08 +020017namespace webrtc {
18
19AndroidVideoTrackSource::AndroidVideoTrackSource(rtc::Thread* signaling_thread,
20 JNIEnv* jni,
arsanyb75f2542016-08-31 18:50:52 -070021 jobject j_egl_context,
22 bool is_screencast)
Sami Kalliomaki16032122016-07-20 16:13:08 +020023 : signaling_thread_(signaling_thread),
24 surface_texture_helper_(webrtc_jni::SurfaceTextureHelper::create(
25 jni,
26 "Camera SurfaceTextureHelper",
arsanyb75f2542016-08-31 18:50:52 -070027 j_egl_context)),
28 is_screencast_(is_screencast) {
Sami Kalliomaki16032122016-07-20 16:13:08 +020029 LOG(LS_INFO) << "AndroidVideoTrackSource ctor";
30 worker_thread_checker_.DetachFromThread();
31 camera_thread_checker_.DetachFromThread();
32}
33
34bool AndroidVideoTrackSource::GetStats(AndroidVideoTrackSource::Stats* stats) {
35 rtc::CritScope lock(&stats_crit_);
36
37 if (!stats_) {
38 return false;
39 }
40
41 *stats = *stats_;
42 return true;
43}
44
45void AndroidVideoTrackSource::SetState(SourceState state) {
46 if (rtc::Thread::Current() != signaling_thread_) {
47 invoker_.AsyncInvoke<void>(
48 RTC_FROM_HERE, signaling_thread_,
49 rtc::Bind(&AndroidVideoTrackSource::SetState, this, state));
50 return;
51 }
52
53 if (state_ != state) {
54 state_ = state;
55 FireOnChanged();
56 }
57}
58
59void AndroidVideoTrackSource::AddOrUpdateSink(
60 rtc::VideoSinkInterface<cricket::VideoFrame>* sink,
61 const rtc::VideoSinkWants& wants) {
62 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
63
64 broadcaster_.AddOrUpdateSink(sink, wants);
65 OnSinkWantsChanged(broadcaster_.wants());
66}
67
68void AndroidVideoTrackSource::RemoveSink(
69 rtc::VideoSinkInterface<cricket::VideoFrame>* sink) {
70 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
71
72 broadcaster_.RemoveSink(sink);
73 OnSinkWantsChanged(broadcaster_.wants());
74}
75
76void AndroidVideoTrackSource::OnSinkWantsChanged(
77 const rtc::VideoSinkWants& wants) {
78 {
79 rtc::CritScope lock(&apply_rotation_crit_);
80 apply_rotation_ = wants.rotation_applied;
81 }
82
83 video_adapter_.OnResolutionRequest(wants.max_pixel_count,
84 wants.max_pixel_count_step_up);
85}
86
87void AndroidVideoTrackSource::OnByteBufferFrameCaptured(const void* frame_data,
88 int length,
89 int width,
90 int height,
91 int rotation,
92 int64_t timestamp_ns) {
93 RTC_DCHECK(camera_thread_checker_.CalledOnValidThread());
94 RTC_DCHECK(rotation == 0 || rotation == 90 || rotation == 180 ||
95 rotation == 270);
96
97 int adapted_width;
98 int adapted_height;
99 int crop_width;
100 int crop_height;
101 int crop_x;
102 int crop_y;
103 int64_t translated_camera_time_us;
104
105 if (!AdaptFrame(width, height, timestamp_ns / rtc::kNumNanosecsPerMicrosec,
106 &adapted_width, &adapted_height, &crop_width, &crop_height,
107 &crop_x, &crop_y, &translated_camera_time_us)) {
108 return;
109 }
110
Sami Kalliomaki16032122016-07-20 16:13:08 +0200111 const uint8_t* y_plane = static_cast<const uint8_t*>(frame_data);
112 const uint8_t* uv_plane = y_plane + width * height;
magjed1a7ef1f2016-09-17 02:39:03 -0700113 const int uv_width = (width + 1) / 2;
Sami Kalliomaki16032122016-07-20 16:13:08 +0200114
115 RTC_CHECK_GE(length, width * height + 2 * uv_width * ((height + 1) / 2));
116
117 // Can only crop at even pixels.
118 crop_x &= ~1;
119 crop_y &= ~1;
magjed1a7ef1f2016-09-17 02:39:03 -0700120 // Crop just by modifying pointers.
121 y_plane += width * crop_y + crop_x;
122 uv_plane += uv_width * crop_y + crop_x;
Sami Kalliomaki16032122016-07-20 16:13:08 +0200123
magjed1a7ef1f2016-09-17 02:39:03 -0700124 rtc::scoped_refptr<webrtc::I420Buffer> buffer =
125 buffer_pool_.CreateBuffer(adapted_width, adapted_height);
126
127 nv12toi420_scaler_.NV12ToI420Scale(
128 y_plane, width,
129 uv_plane, uv_width * 2,
130 crop_width, crop_height,
131 buffer->MutableDataY(), buffer->StrideY(),
jackychened0b0db2016-09-09 16:15:11 -0700132 // Swap U and V, since we have NV21, not NV12.
magjed1a7ef1f2016-09-17 02:39:03 -0700133 buffer->MutableDataV(), buffer->StrideV(),
134 buffer->MutableDataU(), buffer->StrideU(),
135 buffer->width(), buffer->height());
Sami Kalliomaki16032122016-07-20 16:13:08 +0200136
magjed1a7ef1f2016-09-17 02:39:03 -0700137 // Applying rotation is only supported for legacy reasons, and the performance
138 // for this path is not critical.
139 rtc::CritScope lock(&apply_rotation_crit_);
140 if (apply_rotation_ && rotation != 0) {
141 rtc::scoped_refptr<I420Buffer> rotated_buffer =
142 rotation == 180 ? I420Buffer::Create(buffer->width(), buffer->height())
143 : I420Buffer::Create(buffer->height(), buffer->width());
144
145 libyuv::I420Rotate(
146 buffer->DataY(), buffer->StrideY(),
147 buffer->DataU(), buffer->StrideU(),
148 buffer->DataV(), buffer->StrideV(),
149 rotated_buffer->MutableDataY(), rotated_buffer->StrideY(),
150 rotated_buffer->MutableDataU(), rotated_buffer->StrideU(),
151 rotated_buffer->MutableDataV(), rotated_buffer->StrideV(),
152 buffer->width(), buffer->height(),
153 static_cast<libyuv::RotationMode>(rotation));
154
155 buffer = rotated_buffer;
Sami Kalliomaki16032122016-07-20 16:13:08 +0200156 }
157
158 OnFrame(cricket::WebRtcVideoFrame(
159 buffer,
160 apply_rotation_ ? webrtc::kVideoRotation_0
161 : static_cast<webrtc::VideoRotation>(rotation),
Sergey Ulanov19ee1e6eb2016-08-01 13:35:55 -0700162 translated_camera_time_us, 0),
Sami Kalliomaki16032122016-07-20 16:13:08 +0200163 width, height);
164}
165
166void AndroidVideoTrackSource::OnTextureFrameCaptured(
167 int width,
168 int height,
169 int rotation,
170 int64_t timestamp_ns,
171 const webrtc_jni::NativeHandleImpl& handle) {
172 RTC_DCHECK(camera_thread_checker_.CalledOnValidThread());
173 RTC_DCHECK(rotation == 0 || rotation == 90 || rotation == 180 ||
174 rotation == 270);
175
176 int adapted_width;
177 int adapted_height;
178 int crop_width;
179 int crop_height;
180 int crop_x;
181 int crop_y;
182 int64_t translated_camera_time_us;
183
184 if (!AdaptFrame(width, height, timestamp_ns / rtc::kNumNanosecsPerMicrosec,
185 &adapted_width, &adapted_height, &crop_width, &crop_height,
186 &crop_x, &crop_y, &translated_camera_time_us)) {
187 surface_texture_helper_->ReturnTextureFrame();
188 return;
189 }
190
191 webrtc_jni::Matrix matrix = handle.sampling_matrix;
192
193 matrix.Crop(crop_width / static_cast<float>(width),
194 crop_height / static_cast<float>(height),
195 crop_x / static_cast<float>(width),
196 crop_y / static_cast<float>(height));
197
198 rtc::CritScope lock(&apply_rotation_crit_);
199 if (apply_rotation_) {
200 if (rotation == webrtc::kVideoRotation_90 ||
201 rotation == webrtc::kVideoRotation_270) {
202 std::swap(adapted_width, adapted_height);
203 }
204 matrix.Rotate(static_cast<webrtc::VideoRotation>(rotation));
205 }
206
207 OnFrame(cricket::WebRtcVideoFrame(
208 surface_texture_helper_->CreateTextureFrame(
209 adapted_width, adapted_height,
210 webrtc_jni::NativeHandleImpl(handle.oes_texture_id, matrix)),
211 apply_rotation_ ? webrtc::kVideoRotation_0
212 : static_cast<webrtc::VideoRotation>(rotation),
Sergey Ulanov19ee1e6eb2016-08-01 13:35:55 -0700213 translated_camera_time_us, 0),
Sami Kalliomaki16032122016-07-20 16:13:08 +0200214 width, height);
215}
216
217void AndroidVideoTrackSource::OnFrame(const cricket::VideoFrame& frame,
218 int width,
219 int height) {
220 {
221 rtc::CritScope lock(&stats_crit_);
222 stats_ = rtc::Optional<AndroidVideoTrackSource::Stats>({width, height});
223 }
224
225 broadcaster_.OnFrame(frame);
226}
227
228void AndroidVideoTrackSource::OnOutputFormatRequest(int width,
229 int height,
230 int fps) {
Sami Kalliomaki16032122016-07-20 16:13:08 +0200231 cricket::VideoFormat format(width, height,
232 cricket::VideoFormat::FpsToInterval(fps), 0);
233 video_adapter_.OnOutputFormatRequest(format);
234}
235
236bool AndroidVideoTrackSource::AdaptFrame(int width,
237 int height,
238 int64_t camera_time_us,
239 int* out_width,
240 int* out_height,
241 int* crop_width,
242 int* crop_height,
243 int* crop_x,
244 int* crop_y,
245 int64_t* translated_camera_time_us) {
246 RTC_DCHECK(camera_thread_checker_.CalledOnValidThread());
247
248 int64_t system_time_us = rtc::TimeMicros();
nissea0758482016-09-14 00:37:00 -0700249 *translated_camera_time_us =
250 timestamp_aligner_.TranslateTimestamp(camera_time_us, system_time_us);
Sami Kalliomaki16032122016-07-20 16:13:08 +0200251
252 if (!broadcaster_.frame_wanted()) {
253 return false;
254 }
255
256 if (!video_adapter_.AdaptFrameResolution(
257 width, height, camera_time_us * rtc::kNumNanosecsPerMicrosec,
258 crop_width, crop_height, out_width, out_height)) {
259 // VideoAdapter dropped the frame.
260 return false;
261 }
262 *crop_x = (width - *crop_width) / 2;
263 *crop_y = (height - *crop_height) / 2;
264
Sami Kalliomaki16032122016-07-20 16:13:08 +0200265 return true;
266}
267
268} // namespace webrtc