blob: 9d53a9115741c1dbe956263cf7c1b3edceffa105 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
mallinath@webrtc.org12984f02012-02-16 18:18:21 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "modules/video_capture/video_capture_impl.h"
12
elham@webrtc.org5f49dba2012-04-23 21:24:02 +000013#include <stdlib.h>
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <string.h>
elham@webrtc.org5f49dba2012-04-23 21:24:02 +000015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/video/i420_buffer.h"
Yves Gerey3e707812018-11-28 16:47:49 +010017#include "api/video/video_frame_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "common_video/libyuv/include/webrtc_libyuv.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/video_capture/video_capture_config.h"
20#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/ref_counted_object.h"
22#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/trace_event.h"
Mirko Bonadei65432062017-12-11 09:32:13 +010024#include "third_party/libyuv/include/libyuv.h"
pbos@webrtc.orga9b74ad2013-07-12 10:03:52 +000025
Peter Boström1d194412016-03-21 16:44:31 +010026namespace webrtc {
27namespace videocapturemodule {
niklase@google.com470e71d2011-07-07 08:21:25 +000028
ilnik00d802b2017-04-11 10:34:31 -070029const char* VideoCaptureImpl::CurrentDeviceName() const {
30 return _deviceUniqueId;
niklase@google.com470e71d2011-07-07 08:21:25 +000031}
32
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000033// static
34int32_t VideoCaptureImpl::RotationFromDegrees(int degrees,
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000035 VideoRotation* rotation) {
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000036 switch (degrees) {
37 case 0:
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000038 *rotation = kVideoRotation_0;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000039 return 0;
40 case 90:
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000041 *rotation = kVideoRotation_90;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000042 return 0;
43 case 180:
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000044 *rotation = kVideoRotation_180;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000045 return 0;
46 case 270:
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000047 *rotation = kVideoRotation_270;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000048 return 0;
49 default:
Mirko Bonadei72c42502017-11-09 09:33:23 +010050 return -1;
51 ;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000052 }
53}
54
55// static
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000056int32_t VideoCaptureImpl::RotationInDegrees(VideoRotation rotation,
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000057 int* degrees) {
58 switch (rotation) {
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000059 case kVideoRotation_0:
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000060 *degrees = 0;
61 return 0;
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000062 case kVideoRotation_90:
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000063 *degrees = 90;
64 return 0;
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000065 case kVideoRotation_180:
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000066 *degrees = 180;
67 return 0;
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000068 case kVideoRotation_270:
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000069 *degrees = 270;
70 return 0;
71 }
72 return -1;
73}
74
nisseb29b9c82016-12-12 00:22:56 -080075VideoCaptureImpl::VideoCaptureImpl()
76 : _deviceUniqueId(NULL),
pbos@webrtc.org504af452013-07-02 10:15:43 +000077 _requestedCapability(),
Niels Möllerd28db7f2016-05-10 16:31:47 +020078 _lastProcessTimeNanos(rtc::TimeNanos()),
79 _lastFrameRateCallbackTimeNanos(rtc::TimeNanos()),
pbos@webrtc.org504af452013-07-02 10:15:43 +000080 _dataCallBack(NULL),
Niels Möllerd28db7f2016-05-10 16:31:47 +020081 _lastProcessFrameTimeNanos(rtc::TimeNanos()),
guoweis@webrtc.org59140d62015-03-09 17:07:31 +000082 _rotateFrame(kVideoRotation_0),
deadbeeff5629ad2016-03-18 11:38:26 -070083 apply_rotation_(false) {
Mirko Bonadei72c42502017-11-09 09:33:23 +010084 _requestedCapability.width = kDefaultWidth;
85 _requestedCapability.height = kDefaultHeight;
86 _requestedCapability.maxFPS = 30;
87 _requestedCapability.videoType = VideoType::kI420;
88 memset(_incomingFrameTimesNanos, 0, sizeof(_incomingFrameTimesNanos));
niklase@google.com470e71d2011-07-07 08:21:25 +000089}
90
Mirko Bonadei72c42502017-11-09 09:33:23 +010091VideoCaptureImpl::~VideoCaptureImpl() {
92 DeRegisterCaptureDataCallback();
93 if (_deviceUniqueId)
94 delete[] _deviceUniqueId;
niklase@google.com470e71d2011-07-07 08:21:25 +000095}
96
mallinath@webrtc.org7433a082014-01-29 00:56:02 +000097void VideoCaptureImpl::RegisterCaptureDataCallback(
nisseb29b9c82016-12-12 00:22:56 -080098 rtc::VideoSinkInterface<VideoFrame>* dataCallBack) {
Mirko Bonadei72c42502017-11-09 09:33:23 +010099 rtc::CritScope cs(&_apiCs);
100 _dataCallBack = dataCallBack;
niklase@google.com470e71d2011-07-07 08:21:25 +0000101}
102
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000103void VideoCaptureImpl::DeRegisterCaptureDataCallback() {
Mirko Bonadei72c42502017-11-09 09:33:23 +0100104 rtc::CritScope cs(&_apiCs);
105 _dataCallBack = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000106}
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700107int32_t VideoCaptureImpl::DeliverCapturedFrame(VideoFrame& captureFrame) {
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000108 UpdateFrameCount(); // frame count used for local frame rate callback.
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000109
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000110 if (_dataCallBack) {
nisseb29b9c82016-12-12 00:22:56 -0800111 _dataCallBack->OnFrame(captureFrame);
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000112 }
113
114 return 0;
115}
116
Mirko Bonadei72c42502017-11-09 09:33:23 +0100117int32_t VideoCaptureImpl::IncomingFrame(uint8_t* videoFrame,
118 size_t videoFrameLength,
119 const VideoCaptureCapability& frameInfo,
120 int64_t captureTime /*=0*/) {
121 rtc::CritScope cs(&_apiCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000122
Mirko Bonadei72c42502017-11-09 09:33:23 +0100123 const int32_t width = frameInfo.width;
124 const int32_t height = frameInfo.height;
niklase@google.com470e71d2011-07-07 08:21:25 +0000125
Mirko Bonadei72c42502017-11-09 09:33:23 +0100126 TRACE_EVENT1("webrtc", "VC::IncomingFrame", "capture_time", captureTime);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000127
Mirko Bonadei72c42502017-11-09 09:33:23 +0100128 // Not encoded, convert to I420.
129 if (frameInfo.videoType != VideoType::kMJPEG &&
130 CalcBufferSize(frameInfo.videoType, width, abs(height)) !=
131 videoFrameLength) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100132 RTC_LOG(LS_ERROR) << "Wrong incoming frame length.";
Mirko Bonadei72c42502017-11-09 09:33:23 +0100133 return -1;
134 }
135
136 int stride_y = width;
137 int stride_uv = (width + 1) / 2;
138 int target_width = width;
Robert Bares0eb7d3ff2018-10-28 00:16:33 +0000139 int target_height = abs(height);
Mirko Bonadei72c42502017-11-09 09:33:23 +0100140
141 // SetApplyRotation doesn't take any lock. Make a local copy here.
142 bool apply_rotation = apply_rotation_;
143
144 if (apply_rotation) {
145 // Rotating resolution when for 90/270 degree rotations.
146 if (_rotateFrame == kVideoRotation_90 ||
147 _rotateFrame == kVideoRotation_270) {
148 target_width = abs(height);
149 target_height = width;
niklase@google.com470e71d2011-07-07 08:21:25 +0000150 }
Mirko Bonadei72c42502017-11-09 09:33:23 +0100151 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000152
Mirko Bonadei72c42502017-11-09 09:33:23 +0100153 // Setting absolute height (in case it was negative).
154 // In Windows, the image starts bottom left, instead of top left.
155 // Setting a negative source height, inverts the image (within LibYuv).
nisse1e321222017-02-20 23:27:37 -0800156
Mirko Bonadei72c42502017-11-09 09:33:23 +0100157 // TODO(nisse): Use a pool?
158 rtc::scoped_refptr<I420Buffer> buffer = I420Buffer::Create(
Robert Bares0eb7d3ff2018-10-28 00:16:33 +0000159 target_width, target_height, stride_y, stride_uv, stride_uv);
mallikarjun8212e555b2017-11-15 14:35:56 +0530160
161 libyuv::RotationMode rotation_mode = libyuv::kRotate0;
162 if (apply_rotation) {
163 switch (_rotateFrame) {
164 case kVideoRotation_0:
165 rotation_mode = libyuv::kRotate0;
166 break;
167 case kVideoRotation_90:
168 rotation_mode = libyuv::kRotate90;
169 break;
170 case kVideoRotation_180:
171 rotation_mode = libyuv::kRotate180;
172 break;
173 case kVideoRotation_270:
174 rotation_mode = libyuv::kRotate270;
175 break;
176 }
177 }
178
179 const int conversionResult = libyuv::ConvertToI420(
180 videoFrame, videoFrameLength, buffer.get()->MutableDataY(),
181 buffer.get()->StrideY(), buffer.get()->MutableDataU(),
182 buffer.get()->StrideU(), buffer.get()->MutableDataV(),
183 buffer.get()->StrideV(), 0, 0, // No Cropping
184 width, height, target_width, target_height, rotation_mode,
185 ConvertVideoType(frameInfo.videoType));
Mirko Bonadei72c42502017-11-09 09:33:23 +0100186 if (conversionResult < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100187 RTC_LOG(LS_ERROR) << "Failed to convert capture frame from type "
188 << static_cast<int>(frameInfo.videoType) << "to I420.";
Mirko Bonadei72c42502017-11-09 09:33:23 +0100189 return -1;
190 }
nisse1e321222017-02-20 23:27:37 -0800191
Artem Titov1ebfb6a2019-01-03 23:49:37 +0100192 VideoFrame captureFrame =
193 VideoFrame::Builder()
194 .set_video_frame_buffer(buffer)
195 .set_timestamp_rtp(0)
196 .set_timestamp_ms(rtc::TimeMillis())
197 .set_rotation(!apply_rotation ? _rotateFrame : kVideoRotation_0)
198 .build();
Mirko Bonadei72c42502017-11-09 09:33:23 +0100199 captureFrame.set_ntp_time_ms(captureTime);
nisse1e321222017-02-20 23:27:37 -0800200
Mirko Bonadei72c42502017-11-09 09:33:23 +0100201 DeliverCapturedFrame(captureFrame);
nisse1e321222017-02-20 23:27:37 -0800202
Mirko Bonadei72c42502017-11-09 09:33:23 +0100203 return 0;
wu@webrtc.orgf10ea312011-10-14 17:16:04 +0000204}
niklase@google.com470e71d2011-07-07 08:21:25 +0000205
Mirko Bonadei5aec1f62018-08-29 13:27:15 +0200206int32_t VideoCaptureImpl::StartCapture(
207 const VideoCaptureCapability& capability) {
208 _requestedCapability = capability;
209 return -1;
210}
211
212int32_t VideoCaptureImpl::StopCapture() {
213 return -1;
214}
215
216bool VideoCaptureImpl::CaptureStarted() {
217 return false;
218}
219
220int32_t VideoCaptureImpl::CaptureSettings(
221 VideoCaptureCapability& /*settings*/) {
222 return -1;
223}
224
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +0000225int32_t VideoCaptureImpl::SetCaptureRotation(VideoRotation rotation) {
kthelgasonff046c72017-03-31 02:03:55 -0700226 rtc::CritScope cs(&_apiCs);
guoweis@webrtc.org59140d62015-03-09 17:07:31 +0000227 _rotateFrame = rotation;
mikhal@webrtc.org0f34fd72012-11-19 21:15:35 +0000228 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000229}
230
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000231bool VideoCaptureImpl::SetApplyRotation(bool enable) {
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700232 // We can't take any lock here as it'll cause deadlock with IncomingFrame.
233
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000234 // The effect of this is the last caller wins.
235 apply_rotation_ = enable;
236 return true;
237}
238
Mirko Bonadei5aec1f62018-08-29 13:27:15 +0200239bool VideoCaptureImpl::GetApplyRotation() {
240 return apply_rotation_;
241}
242
ilnik00d802b2017-04-11 10:34:31 -0700243void VideoCaptureImpl::UpdateFrameCount() {
244 if (_incomingFrameTimesNanos[0] / rtc::kNumNanosecsPerMicrosec == 0) {
245 // first no shift
246 } else {
247 // shift
248 for (int i = (kFrameRateCountHistorySize - 2); i >= 0; --i) {
249 _incomingFrameTimesNanos[i + 1] = _incomingFrameTimesNanos[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000250 }
ilnik00d802b2017-04-11 10:34:31 -0700251 }
252 _incomingFrameTimesNanos[0] = rtc::TimeNanos();
niklase@google.com470e71d2011-07-07 08:21:25 +0000253}
254
ilnik00d802b2017-04-11 10:34:31 -0700255uint32_t VideoCaptureImpl::CalculateFrameRate(int64_t now_ns) {
256 int32_t num = 0;
257 int32_t nrOfFrames = 0;
258 for (num = 1; num < (kFrameRateCountHistorySize - 1); ++num) {
259 if (_incomingFrameTimesNanos[num] <= 0 ||
260 (now_ns - _incomingFrameTimesNanos[num]) /
261 rtc::kNumNanosecsPerMillisec >
262 kFrameRateHistoryWindowMs) { // don't use data older than 2sec
263 break;
264 } else {
265 nrOfFrames++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000266 }
ilnik00d802b2017-04-11 10:34:31 -0700267 }
268 if (num > 1) {
269 int64_t diff = (now_ns - _incomingFrameTimesNanos[num - 1]) /
270 rtc::kNumNanosecsPerMillisec;
271 if (diff > 0) {
272 return uint32_t((nrOfFrames * 1000.0f / diff) + 0.5f);
niklase@google.com470e71d2011-07-07 08:21:25 +0000273 }
ilnik00d802b2017-04-11 10:34:31 -0700274 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000275
ilnik00d802b2017-04-11 10:34:31 -0700276 return nrOfFrames;
niklase@google.com470e71d2011-07-07 08:21:25 +0000277}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000278} // namespace videocapturemodule
279} // namespace webrtc