blob: 0afa9aabeed2a4164f2c23bcdf7d7aa14b6a6640 [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"
nisse368f5cf2017-04-05 05:00:33 -070016#include "webrtc/base/logging.h"
Peter Boström1d194412016-03-21 16:44:31 +010017#include "webrtc/base/refcount.h"
Niels Möllerd28db7f2016-05-10 16:31:47 +020018#include "webrtc/base/timeutils.h"
tommie4f96502015-10-20 23:00:48 -070019#include "webrtc/base/trace_event.h"
pbos@webrtc.orga9b74ad2013-07-12 10:03:52 +000020#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010021#include "webrtc/modules/include/module_common_types.h"
pbos@webrtc.orga9b74ad2013-07-12 10:03:52 +000022#include "webrtc/modules/video_capture/video_capture_config.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010023#include "webrtc/system_wrappers/include/clock.h"
pbos@webrtc.orga9b74ad2013-07-12 10:03:52 +000024
Peter Boström1d194412016-03-21 16:44:31 +010025namespace webrtc {
26namespace videocapturemodule {
27rtc::scoped_refptr<VideoCaptureModule> VideoCaptureImpl::Create(
Peter Boström1d194412016-03-21 16:44:31 +010028 VideoCaptureExternal*& externalCapture) {
29 rtc::scoped_refptr<VideoCaptureImpl> implementation(
nisseb29b9c82016-12-12 00:22:56 -080030 new rtc::RefCountedObject<VideoCaptureImpl>());
Peter Boström1d194412016-03-21 16:44:31 +010031 externalCapture = implementation.get();
32 return implementation;
niklase@google.com470e71d2011-07-07 08:21:25 +000033}
34
ilnik00d802b2017-04-11 10:34:31 -070035const char* VideoCaptureImpl::CurrentDeviceName() const {
36 return _deviceUniqueId;
niklase@google.com470e71d2011-07-07 08:21:25 +000037}
38
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000039// static
40int32_t VideoCaptureImpl::RotationFromDegrees(int degrees,
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000041 VideoRotation* rotation) {
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000042 switch (degrees) {
43 case 0:
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000044 *rotation = kVideoRotation_0;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000045 return 0;
46 case 90:
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000047 *rotation = kVideoRotation_90;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000048 return 0;
49 case 180:
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000050 *rotation = kVideoRotation_180;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000051 return 0;
52 case 270:
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000053 *rotation = kVideoRotation_270;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000054 return 0;
55 default:
56 return -1;;
57 }
58}
59
60// static
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000061int32_t VideoCaptureImpl::RotationInDegrees(VideoRotation rotation,
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000062 int* degrees) {
63 switch (rotation) {
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000064 case kVideoRotation_0:
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000065 *degrees = 0;
66 return 0;
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000067 case kVideoRotation_90:
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000068 *degrees = 90;
69 return 0;
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000070 case kVideoRotation_180:
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000071 *degrees = 180;
72 return 0;
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000073 case kVideoRotation_270:
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000074 *degrees = 270;
75 return 0;
76 }
77 return -1;
78}
79
nisseb29b9c82016-12-12 00:22:56 -080080VideoCaptureImpl::VideoCaptureImpl()
81 : _deviceUniqueId(NULL),
pbos@webrtc.org504af452013-07-02 10:15:43 +000082 _requestedCapability(),
Niels Möllerd28db7f2016-05-10 16:31:47 +020083 _lastProcessTimeNanos(rtc::TimeNanos()),
84 _lastFrameRateCallbackTimeNanos(rtc::TimeNanos()),
pbos@webrtc.org504af452013-07-02 10:15:43 +000085 _dataCallBack(NULL),
Niels Möllerd28db7f2016-05-10 16:31:47 +020086 _lastProcessFrameTimeNanos(rtc::TimeNanos()),
guoweis@webrtc.org59140d62015-03-09 17:07:31 +000087 _rotateFrame(kVideoRotation_0),
deadbeeff5629ad2016-03-18 11:38:26 -070088 apply_rotation_(false) {
niklase@google.com470e71d2011-07-07 08:21:25 +000089 _requestedCapability.width = kDefaultWidth;
90 _requestedCapability.height = kDefaultHeight;
91 _requestedCapability.maxFPS = 30;
92 _requestedCapability.rawType = kVideoI420;
Niels Möllerd28db7f2016-05-10 16:31:47 +020093 memset(_incomingFrameTimesNanos, 0, sizeof(_incomingFrameTimesNanos));
niklase@google.com470e71d2011-07-07 08:21:25 +000094}
95
96VideoCaptureImpl::~VideoCaptureImpl()
97{
98 DeRegisterCaptureDataCallback();
niklase@google.com470e71d2011-07-07 08:21:25 +000099 if (_deviceUniqueId)
100 delete[] _deviceUniqueId;
101}
102
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000103void VideoCaptureImpl::RegisterCaptureDataCallback(
nisseb29b9c82016-12-12 00:22:56 -0800104 rtc::VideoSinkInterface<VideoFrame>* dataCallBack) {
kthelgasonff046c72017-03-31 02:03:55 -0700105 rtc::CritScope cs(&_apiCs);
nisseb29b9c82016-12-12 00:22:56 -0800106 _dataCallBack = dataCallBack;
niklase@google.com470e71d2011-07-07 08:21:25 +0000107}
108
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000109void VideoCaptureImpl::DeRegisterCaptureDataCallback() {
kthelgasonff046c72017-03-31 02:03:55 -0700110 rtc::CritScope cs(&_apiCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000111 _dataCallBack = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000112}
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700113int32_t VideoCaptureImpl::DeliverCapturedFrame(VideoFrame& captureFrame) {
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000114 UpdateFrameCount(); // frame count used for local frame rate callback.
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000115
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000116 if (_dataCallBack) {
nisseb29b9c82016-12-12 00:22:56 -0800117 _dataCallBack->OnFrame(captureFrame);
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000118 }
119
120 return 0;
121}
122
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000123int32_t VideoCaptureImpl::IncomingFrame(
124 uint8_t* videoFrame,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000125 size_t videoFrameLength,
mflodman@webrtc.org4f9e44f2012-02-23 09:00:26 +0000126 const VideoCaptureCapability& frameInfo,
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000127 int64_t captureTime/*=0*/)
niklase@google.com470e71d2011-07-07 08:21:25 +0000128{
kthelgasonff046c72017-03-31 02:03:55 -0700129 rtc::CritScope cs(&_apiCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000130
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000131 const int32_t width = frameInfo.width;
132 const int32_t height = frameInfo.height;
niklase@google.com470e71d2011-07-07 08:21:25 +0000133
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000134 TRACE_EVENT1("webrtc", "VC::IncomingFrame", "capture_time", captureTime);
135
nisse1e321222017-02-20 23:27:37 -0800136 // Not encoded, convert to I420.
137 const VideoType commonVideoType =
ilnik00d802b2017-04-11 10:34:31 -0700138 RawVideoTypeToCommonVideoVideoType(frameInfo.rawType);
nisse1e321222017-02-20 23:27:37 -0800139
140 if (frameInfo.rawType != kVideoMJPEG &&
ilnik00d802b2017-04-11 10:34:31 -0700141 CalcBufferSize(commonVideoType, width, abs(height)) !=
142 videoFrameLength) {
143 LOG(LS_ERROR) << "Wrong incoming frame length.";
144 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000145 }
146
nisse1e321222017-02-20 23:27:37 -0800147 int stride_y = width;
148 int stride_uv = (width + 1) / 2;
149 int target_width = width;
150 int target_height = height;
151
152 // SetApplyRotation doesn't take any lock. Make a local copy here.
153 bool apply_rotation = apply_rotation_;
154
155 if (apply_rotation) {
156 // Rotating resolution when for 90/270 degree rotations.
157 if (_rotateFrame == kVideoRotation_90 ||
158 _rotateFrame == kVideoRotation_270) {
159 target_width = abs(height);
160 target_height = width;
161 }
162 }
163
164 // Setting absolute height (in case it was negative).
165 // In Windows, the image starts bottom left, instead of top left.
166 // Setting a negative source height, inverts the image (within LibYuv).
167
168 // TODO(nisse): Use a pool?
169 rtc::scoped_refptr<I420Buffer> buffer = I420Buffer::Create(
170 target_width, abs(target_height), stride_y, stride_uv, stride_uv);
171 const int conversionResult = ConvertToI420(
172 commonVideoType, videoFrame, 0, 0, // No cropping
173 width, height, videoFrameLength,
174 apply_rotation ? _rotateFrame : kVideoRotation_0, buffer.get());
ilnik00d802b2017-04-11 10:34:31 -0700175 if (conversionResult < 0) {
nisse1e321222017-02-20 23:27:37 -0800176 LOG(LS_ERROR) << "Failed to convert capture frame from type "
177 << frameInfo.rawType << "to I420.";
ilnik00d802b2017-04-11 10:34:31 -0700178 return -1;
nisse1e321222017-02-20 23:27:37 -0800179 }
180
ilnik00d802b2017-04-11 10:34:31 -0700181 VideoFrame captureFrame(buffer, 0, rtc::TimeMillis(),
182 !apply_rotation ? _rotateFrame : kVideoRotation_0);
nisse1e321222017-02-20 23:27:37 -0800183 captureFrame.set_ntp_time_ms(captureTime);
184
185 DeliverCapturedFrame(captureFrame);
186
niklase@google.com470e71d2011-07-07 08:21:25 +0000187 return 0;
wu@webrtc.orgf10ea312011-10-14 17:16:04 +0000188}
niklase@google.com470e71d2011-07-07 08:21:25 +0000189
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +0000190int32_t VideoCaptureImpl::SetCaptureRotation(VideoRotation rotation) {
kthelgasonff046c72017-03-31 02:03:55 -0700191 rtc::CritScope cs(&_apiCs);
guoweis@webrtc.org59140d62015-03-09 17:07:31 +0000192 _rotateFrame = rotation;
mikhal@webrtc.org0f34fd72012-11-19 21:15:35 +0000193 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000194}
195
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000196bool VideoCaptureImpl::SetApplyRotation(bool enable) {
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700197 // We can't take any lock here as it'll cause deadlock with IncomingFrame.
198
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000199 // The effect of this is the last caller wins.
200 apply_rotation_ = enable;
201 return true;
202}
203
ilnik00d802b2017-04-11 10:34:31 -0700204void VideoCaptureImpl::UpdateFrameCount() {
205 if (_incomingFrameTimesNanos[0] / rtc::kNumNanosecsPerMicrosec == 0) {
206 // first no shift
207 } else {
208 // shift
209 for (int i = (kFrameRateCountHistorySize - 2); i >= 0; --i) {
210 _incomingFrameTimesNanos[i + 1] = _incomingFrameTimesNanos[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000211 }
ilnik00d802b2017-04-11 10:34:31 -0700212 }
213 _incomingFrameTimesNanos[0] = rtc::TimeNanos();
niklase@google.com470e71d2011-07-07 08:21:25 +0000214}
215
ilnik00d802b2017-04-11 10:34:31 -0700216uint32_t VideoCaptureImpl::CalculateFrameRate(int64_t now_ns) {
217 int32_t num = 0;
218 int32_t nrOfFrames = 0;
219 for (num = 1; num < (kFrameRateCountHistorySize - 1); ++num) {
220 if (_incomingFrameTimesNanos[num] <= 0 ||
221 (now_ns - _incomingFrameTimesNanos[num]) /
222 rtc::kNumNanosecsPerMillisec >
223 kFrameRateHistoryWindowMs) { // don't use data older than 2sec
224 break;
225 } else {
226 nrOfFrames++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000227 }
ilnik00d802b2017-04-11 10:34:31 -0700228 }
229 if (num > 1) {
230 int64_t diff = (now_ns - _incomingFrameTimesNanos[num - 1]) /
231 rtc::kNumNanosecsPerMillisec;
232 if (diff > 0) {
233 return uint32_t((nrOfFrames * 1000.0f / diff) + 0.5f);
niklase@google.com470e71d2011-07-07 08:21:25 +0000234 }
ilnik00d802b2017-04-11 10:34:31 -0700235 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000236
ilnik00d802b2017-04-11 10:34:31 -0700237 return nrOfFrames;
niklase@google.com470e71d2011-07-07 08:21:25 +0000238}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000239} // namespace videocapturemodule
240} // namespace webrtc