blob: ead2b1568a52e9a1dec8d8d346710a2fa6b71b25 [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
pbos@webrtc.orga9b74ad2013-07-12 10:03:52 +000011#include "webrtc/modules/video_capture/video_capture_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
elham@webrtc.org5f49dba2012-04-23 21:24:02 +000013#include <stdlib.h>
14
nisseaf916892017-01-10 07:44:26 -080015#include "webrtc/api/video/i420_buffer.h"
Peter Boström1d194412016-03-21 16:44:31 +010016#include "webrtc/base/refcount.h"
Niels Möllerd28db7f2016-05-10 16:31:47 +020017#include "webrtc/base/timeutils.h"
tommie4f96502015-10-20 23:00:48 -070018#include "webrtc/base/trace_event.h"
pbos@webrtc.orga9b74ad2013-07-12 10:03:52 +000019#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010020#include "webrtc/modules/include/module_common_types.h"
pbos@webrtc.orga9b74ad2013-07-12 10:03:52 +000021#include "webrtc/modules/video_capture/video_capture_config.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010022#include "webrtc/system_wrappers/include/clock.h"
23#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
24#include "webrtc/system_wrappers/include/logging.h"
pbos@webrtc.orga9b74ad2013-07-12 10:03:52 +000025
Peter Boström1d194412016-03-21 16:44:31 +010026namespace webrtc {
27namespace videocapturemodule {
28rtc::scoped_refptr<VideoCaptureModule> VideoCaptureImpl::Create(
Peter Boström1d194412016-03-21 16:44:31 +010029 VideoCaptureExternal*& externalCapture) {
30 rtc::scoped_refptr<VideoCaptureImpl> implementation(
nisseb29b9c82016-12-12 00:22:56 -080031 new rtc::RefCountedObject<VideoCaptureImpl>());
Peter Boström1d194412016-03-21 16:44:31 +010032 externalCapture = implementation.get();
33 return implementation;
niklase@google.com470e71d2011-07-07 08:21:25 +000034}
35
leozwang@webrtc.org1745e932012-03-01 16:30:40 +000036const char* VideoCaptureImpl::CurrentDeviceName() const
niklase@google.com470e71d2011-07-07 08:21:25 +000037{
38 return _deviceUniqueId;
39}
40
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000041// static
42int32_t VideoCaptureImpl::RotationFromDegrees(int degrees,
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000043 VideoRotation* rotation) {
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000044 switch (degrees) {
45 case 0:
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000046 *rotation = kVideoRotation_0;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000047 return 0;
48 case 90:
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000049 *rotation = kVideoRotation_90;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000050 return 0;
51 case 180:
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000052 *rotation = kVideoRotation_180;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000053 return 0;
54 case 270:
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000055 *rotation = kVideoRotation_270;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000056 return 0;
57 default:
58 return -1;;
59 }
60}
61
62// static
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000063int32_t VideoCaptureImpl::RotationInDegrees(VideoRotation rotation,
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000064 int* degrees) {
65 switch (rotation) {
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000066 case kVideoRotation_0:
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000067 *degrees = 0;
68 return 0;
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000069 case kVideoRotation_90:
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000070 *degrees = 90;
71 return 0;
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000072 case kVideoRotation_180:
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000073 *degrees = 180;
74 return 0;
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000075 case kVideoRotation_270:
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000076 *degrees = 270;
77 return 0;
78 }
79 return -1;
80}
81
nisseb29b9c82016-12-12 00:22:56 -080082VideoCaptureImpl::VideoCaptureImpl()
83 : _deviceUniqueId(NULL),
pbos@webrtc.org504af452013-07-02 10:15:43 +000084 _apiCs(*CriticalSectionWrapper::CreateCriticalSection()),
pbos@webrtc.org504af452013-07-02 10:15:43 +000085 _requestedCapability(),
Niels Möllerd28db7f2016-05-10 16:31:47 +020086 _lastProcessTimeNanos(rtc::TimeNanos()),
87 _lastFrameRateCallbackTimeNanos(rtc::TimeNanos()),
pbos@webrtc.org504af452013-07-02 10:15:43 +000088 _dataCallBack(NULL),
Niels Möllerd28db7f2016-05-10 16:31:47 +020089 _lastProcessFrameTimeNanos(rtc::TimeNanos()),
guoweis@webrtc.org59140d62015-03-09 17:07:31 +000090 _rotateFrame(kVideoRotation_0),
deadbeeff5629ad2016-03-18 11:38:26 -070091 apply_rotation_(false) {
niklase@google.com470e71d2011-07-07 08:21:25 +000092 _requestedCapability.width = kDefaultWidth;
93 _requestedCapability.height = kDefaultHeight;
94 _requestedCapability.maxFPS = 30;
95 _requestedCapability.rawType = kVideoI420;
Niels Möllerd28db7f2016-05-10 16:31:47 +020096 memset(_incomingFrameTimesNanos, 0, sizeof(_incomingFrameTimesNanos));
niklase@google.com470e71d2011-07-07 08:21:25 +000097}
98
99VideoCaptureImpl::~VideoCaptureImpl()
100{
101 DeRegisterCaptureDataCallback();
niklase@google.com470e71d2011-07-07 08:21:25 +0000102 delete &_apiCs;
103
104 if (_deviceUniqueId)
105 delete[] _deviceUniqueId;
106}
107
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000108void VideoCaptureImpl::RegisterCaptureDataCallback(
nisseb29b9c82016-12-12 00:22:56 -0800109 rtc::VideoSinkInterface<VideoFrame>* dataCallBack) {
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000110 CriticalSectionScoped cs(&_apiCs);
nisseb29b9c82016-12-12 00:22:56 -0800111 _dataCallBack = dataCallBack;
niklase@google.com470e71d2011-07-07 08:21:25 +0000112}
113
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000114void VideoCaptureImpl::DeRegisterCaptureDataCallback() {
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000115 CriticalSectionScoped cs(&_apiCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000116 _dataCallBack = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000117}
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700118int32_t VideoCaptureImpl::DeliverCapturedFrame(VideoFrame& captureFrame) {
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000119 UpdateFrameCount(); // frame count used for local frame rate callback.
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000120
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000121 if (_dataCallBack) {
nisseb29b9c82016-12-12 00:22:56 -0800122 _dataCallBack->OnFrame(captureFrame);
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000123 }
124
125 return 0;
126}
127
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000128int32_t VideoCaptureImpl::IncomingFrame(
129 uint8_t* videoFrame,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000130 size_t videoFrameLength,
mflodman@webrtc.org4f9e44f2012-02-23 09:00:26 +0000131 const VideoCaptureCapability& frameInfo,
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000132 int64_t captureTime/*=0*/)
niklase@google.com470e71d2011-07-07 08:21:25 +0000133{
fischman@webrtc.org42694c52014-06-06 18:28:28 +0000134 CriticalSectionScoped cs(&_apiCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000135
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000136 const int32_t width = frameInfo.width;
137 const int32_t height = frameInfo.height;
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000139 TRACE_EVENT1("webrtc", "VC::IncomingFrame", "capture_time", captureTime);
140
nisse1e321222017-02-20 23:27:37 -0800141 // Not encoded, convert to I420.
142 const VideoType commonVideoType =
143 RawVideoTypeToCommonVideoVideoType(frameInfo.rawType);
144
145 if (frameInfo.rawType != kVideoMJPEG &&
146 CalcBufferSize(commonVideoType, width,
147 abs(height)) != videoFrameLength)
niklase@google.com470e71d2011-07-07 08:21:25 +0000148 {
nisse1e321222017-02-20 23:27:37 -0800149 LOG(LS_ERROR) << "Wrong incoming frame length.";
mflodman@webrtc.org3ba883f2013-06-07 13:57:57 +0000150 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000151 }
152
nisse1e321222017-02-20 23:27:37 -0800153 int stride_y = width;
154 int stride_uv = (width + 1) / 2;
155 int target_width = width;
156 int target_height = height;
157
158 // SetApplyRotation doesn't take any lock. Make a local copy here.
159 bool apply_rotation = apply_rotation_;
160
161 if (apply_rotation) {
162 // Rotating resolution when for 90/270 degree rotations.
163 if (_rotateFrame == kVideoRotation_90 ||
164 _rotateFrame == kVideoRotation_270) {
165 target_width = abs(height);
166 target_height = width;
167 }
168 }
169
170 // Setting absolute height (in case it was negative).
171 // In Windows, the image starts bottom left, instead of top left.
172 // Setting a negative source height, inverts the image (within LibYuv).
173
174 // TODO(nisse): Use a pool?
175 rtc::scoped_refptr<I420Buffer> buffer = I420Buffer::Create(
176 target_width, abs(target_height), stride_y, stride_uv, stride_uv);
177 const int conversionResult = ConvertToI420(
178 commonVideoType, videoFrame, 0, 0, // No cropping
179 width, height, videoFrameLength,
180 apply_rotation ? _rotateFrame : kVideoRotation_0, buffer.get());
181 if (conversionResult < 0)
182 {
183 LOG(LS_ERROR) << "Failed to convert capture frame from type "
184 << frameInfo.rawType << "to I420.";
185 return -1;
186 }
187
188 VideoFrame captureFrame(
189 buffer, 0, rtc::TimeMillis(),
190 !apply_rotation ? _rotateFrame : kVideoRotation_0);
191 captureFrame.set_ntp_time_ms(captureTime);
192
193 DeliverCapturedFrame(captureFrame);
194
niklase@google.com470e71d2011-07-07 08:21:25 +0000195 return 0;
wu@webrtc.orgf10ea312011-10-14 17:16:04 +0000196}
niklase@google.com470e71d2011-07-07 08:21:25 +0000197
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +0000198int32_t VideoCaptureImpl::SetCaptureRotation(VideoRotation rotation) {
mikhal@webrtc.org0f34fd72012-11-19 21:15:35 +0000199 CriticalSectionScoped cs(&_apiCs);
guoweis@webrtc.org59140d62015-03-09 17:07:31 +0000200 _rotateFrame = rotation;
mikhal@webrtc.org0f34fd72012-11-19 21:15:35 +0000201 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000202}
203
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000204bool VideoCaptureImpl::SetApplyRotation(bool enable) {
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700205 // We can't take any lock here as it'll cause deadlock with IncomingFrame.
206
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000207 // The effect of this is the last caller wins.
208 apply_rotation_ = enable;
209 return true;
210}
211
niklase@google.com470e71d2011-07-07 08:21:25 +0000212void VideoCaptureImpl::UpdateFrameCount()
213{
Niels Möllerd28db7f2016-05-10 16:31:47 +0200214 if (_incomingFrameTimesNanos[0] / rtc::kNumNanosecsPerMicrosec == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000215 {
216 // first no shift
217 }
218 else
219 {
220 // shift
221 for (int i = (kFrameRateCountHistorySize - 2); i >= 0; i--)
222 {
Niels Möllerd28db7f2016-05-10 16:31:47 +0200223 _incomingFrameTimesNanos[i + 1] = _incomingFrameTimesNanos[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000224 }
225 }
Niels Möllerd28db7f2016-05-10 16:31:47 +0200226 _incomingFrameTimesNanos[0] = rtc::TimeNanos();
niklase@google.com470e71d2011-07-07 08:21:25 +0000227}
228
Niels Möllerd28db7f2016-05-10 16:31:47 +0200229uint32_t VideoCaptureImpl::CalculateFrameRate(int64_t now_ns)
niklase@google.com470e71d2011-07-07 08:21:25 +0000230{
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000231 int32_t num = 0;
232 int32_t nrOfFrames = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000233 for (num = 1; num < (kFrameRateCountHistorySize - 1); num++)
234 {
Niels Möllerd28db7f2016-05-10 16:31:47 +0200235 if (_incomingFrameTimesNanos[num] <= 0 ||
236 (now_ns - _incomingFrameTimesNanos[num]) /
237 rtc::kNumNanosecsPerMillisec >
238 kFrameRateHistoryWindowMs) // don't use data older than 2sec
niklase@google.com470e71d2011-07-07 08:21:25 +0000239 {
240 break;
241 }
242 else
243 {
244 nrOfFrames++;
245 }
246 }
247 if (num > 1)
248 {
Niels Möllerd28db7f2016-05-10 16:31:47 +0200249 int64_t diff = (now_ns - _incomingFrameTimesNanos[num - 1]) /
250 rtc::kNumNanosecsPerMillisec;
niklase@google.com470e71d2011-07-07 08:21:25 +0000251 if (diff > 0)
252 {
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000253 return uint32_t((nrOfFrames * 1000.0f / diff) + 0.5f);
niklase@google.com470e71d2011-07-07 08:21:25 +0000254 }
255 }
256
257 return nrOfFrames;
258}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000259} // namespace videocapturemodule
260} // namespace webrtc