blob: 88890a666a35c533d1c3eb41538f39d9c1fb8d47 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "api/video/i420_buffer.h"
16#include "common_video/libyuv/include/webrtc_libyuv.h"
17#include "modules/include/module_common_types.h"
18#include "modules/video_capture/video_capture_config.h"
19#include "rtc_base/logging.h"
20#include "rtc_base/refcount.h"
Niels Möller84255bb2017-10-06 13:43:23 +020021#include "rtc_base/refcountedobject.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/timeutils.h"
23#include "rtc_base/trace_event.h"
24#include "system_wrappers/include/clock.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
ilnik00d802b2017-04-11 10:34:31 -070036const char* VideoCaptureImpl::CurrentDeviceName() const {
37 return _deviceUniqueId;
niklase@google.com470e71d2011-07-07 08:21:25 +000038}
39
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000040// static
41int32_t VideoCaptureImpl::RotationFromDegrees(int degrees,
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000042 VideoRotation* rotation) {
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000043 switch (degrees) {
44 case 0:
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000045 *rotation = kVideoRotation_0;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000046 return 0;
47 case 90:
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000048 *rotation = kVideoRotation_90;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000049 return 0;
50 case 180:
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000051 *rotation = kVideoRotation_180;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000052 return 0;
53 case 270:
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000054 *rotation = kVideoRotation_270;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000055 return 0;
56 default:
Mirko Bonadei72c42502017-11-09 09:33:23 +010057 return -1;
58 ;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000059 }
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 _requestedCapability(),
Niels Möllerd28db7f2016-05-10 16:31:47 +020085 _lastProcessTimeNanos(rtc::TimeNanos()),
86 _lastFrameRateCallbackTimeNanos(rtc::TimeNanos()),
pbos@webrtc.org504af452013-07-02 10:15:43 +000087 _dataCallBack(NULL),
Niels Möllerd28db7f2016-05-10 16:31:47 +020088 _lastProcessFrameTimeNanos(rtc::TimeNanos()),
guoweis@webrtc.org59140d62015-03-09 17:07:31 +000089 _rotateFrame(kVideoRotation_0),
deadbeeff5629ad2016-03-18 11:38:26 -070090 apply_rotation_(false) {
Mirko Bonadei72c42502017-11-09 09:33:23 +010091 _requestedCapability.width = kDefaultWidth;
92 _requestedCapability.height = kDefaultHeight;
93 _requestedCapability.maxFPS = 30;
94 _requestedCapability.videoType = VideoType::kI420;
95 memset(_incomingFrameTimesNanos, 0, sizeof(_incomingFrameTimesNanos));
niklase@google.com470e71d2011-07-07 08:21:25 +000096}
97
Mirko Bonadei72c42502017-11-09 09:33:23 +010098VideoCaptureImpl::~VideoCaptureImpl() {
99 DeRegisterCaptureDataCallback();
100 if (_deviceUniqueId)
101 delete[] _deviceUniqueId;
niklase@google.com470e71d2011-07-07 08:21:25 +0000102}
103
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000104void VideoCaptureImpl::RegisterCaptureDataCallback(
nisseb29b9c82016-12-12 00:22:56 -0800105 rtc::VideoSinkInterface<VideoFrame>* dataCallBack) {
Mirko Bonadei72c42502017-11-09 09:33:23 +0100106 rtc::CritScope cs(&_apiCs);
107 _dataCallBack = dataCallBack;
niklase@google.com470e71d2011-07-07 08:21:25 +0000108}
109
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000110void VideoCaptureImpl::DeRegisterCaptureDataCallback() {
Mirko Bonadei72c42502017-11-09 09:33:23 +0100111 rtc::CritScope cs(&_apiCs);
112 _dataCallBack = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000113}
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700114int32_t VideoCaptureImpl::DeliverCapturedFrame(VideoFrame& captureFrame) {
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000115 UpdateFrameCount(); // frame count used for local frame rate callback.
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000116
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000117 if (_dataCallBack) {
nisseb29b9c82016-12-12 00:22:56 -0800118 _dataCallBack->OnFrame(captureFrame);
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000119 }
120
121 return 0;
122}
123
Mirko Bonadei72c42502017-11-09 09:33:23 +0100124int32_t VideoCaptureImpl::IncomingFrame(uint8_t* videoFrame,
125 size_t videoFrameLength,
126 const VideoCaptureCapability& frameInfo,
127 int64_t captureTime /*=0*/) {
128 rtc::CritScope cs(&_apiCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000129
Mirko Bonadei72c42502017-11-09 09:33:23 +0100130 const int32_t width = frameInfo.width;
131 const int32_t height = frameInfo.height;
niklase@google.com470e71d2011-07-07 08:21:25 +0000132
Mirko Bonadei72c42502017-11-09 09:33:23 +0100133 TRACE_EVENT1("webrtc", "VC::IncomingFrame", "capture_time", captureTime);
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000134
Mirko Bonadei72c42502017-11-09 09:33:23 +0100135 // Not encoded, convert to I420.
136 if (frameInfo.videoType != VideoType::kMJPEG &&
137 CalcBufferSize(frameInfo.videoType, width, abs(height)) !=
138 videoFrameLength) {
139 LOG(LS_ERROR) << "Wrong incoming frame length.";
140 return -1;
141 }
142
143 int stride_y = width;
144 int stride_uv = (width + 1) / 2;
145 int target_width = width;
146 int target_height = height;
147
148 // SetApplyRotation doesn't take any lock. Make a local copy here.
149 bool apply_rotation = apply_rotation_;
150
151 if (apply_rotation) {
152 // Rotating resolution when for 90/270 degree rotations.
153 if (_rotateFrame == kVideoRotation_90 ||
154 _rotateFrame == kVideoRotation_270) {
155 target_width = abs(height);
156 target_height = width;
niklase@google.com470e71d2011-07-07 08:21:25 +0000157 }
Mirko Bonadei72c42502017-11-09 09:33:23 +0100158 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000159
Mirko Bonadei72c42502017-11-09 09:33:23 +0100160 // Setting absolute height (in case it was negative).
161 // In Windows, the image starts bottom left, instead of top left.
162 // Setting a negative source height, inverts the image (within LibYuv).
nisse1e321222017-02-20 23:27:37 -0800163
Mirko Bonadei72c42502017-11-09 09:33:23 +0100164 // TODO(nisse): Use a pool?
165 rtc::scoped_refptr<I420Buffer> buffer = I420Buffer::Create(
166 target_width, abs(target_height), stride_y, stride_uv, stride_uv);
167 const int conversionResult = ConvertToI420(
168 frameInfo.videoType, videoFrame, 0, 0, // No cropping
169 width, height, videoFrameLength,
170 apply_rotation ? _rotateFrame : kVideoRotation_0, buffer.get());
171 if (conversionResult < 0) {
172 LOG(LS_ERROR) << "Failed to convert capture frame from type "
173 << static_cast<int>(frameInfo.videoType) << "to I420.";
174 return -1;
175 }
nisse1e321222017-02-20 23:27:37 -0800176
Mirko Bonadei72c42502017-11-09 09:33:23 +0100177 VideoFrame captureFrame(buffer, 0, rtc::TimeMillis(),
178 !apply_rotation ? _rotateFrame : kVideoRotation_0);
179 captureFrame.set_ntp_time_ms(captureTime);
nisse1e321222017-02-20 23:27:37 -0800180
Mirko Bonadei72c42502017-11-09 09:33:23 +0100181 DeliverCapturedFrame(captureFrame);
nisse1e321222017-02-20 23:27:37 -0800182
Mirko Bonadei72c42502017-11-09 09:33:23 +0100183 return 0;
wu@webrtc.orgf10ea312011-10-14 17:16:04 +0000184}
niklase@google.com470e71d2011-07-07 08:21:25 +0000185
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +0000186int32_t VideoCaptureImpl::SetCaptureRotation(VideoRotation rotation) {
kthelgasonff046c72017-03-31 02:03:55 -0700187 rtc::CritScope cs(&_apiCs);
guoweis@webrtc.org59140d62015-03-09 17:07:31 +0000188 _rotateFrame = rotation;
mikhal@webrtc.org0f34fd72012-11-19 21:15:35 +0000189 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000190}
191
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000192bool VideoCaptureImpl::SetApplyRotation(bool enable) {
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700193 // We can't take any lock here as it'll cause deadlock with IncomingFrame.
194
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000195 // The effect of this is the last caller wins.
196 apply_rotation_ = enable;
197 return true;
198}
199
ilnik00d802b2017-04-11 10:34:31 -0700200void VideoCaptureImpl::UpdateFrameCount() {
201 if (_incomingFrameTimesNanos[0] / rtc::kNumNanosecsPerMicrosec == 0) {
202 // first no shift
203 } else {
204 // shift
205 for (int i = (kFrameRateCountHistorySize - 2); i >= 0; --i) {
206 _incomingFrameTimesNanos[i + 1] = _incomingFrameTimesNanos[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000207 }
ilnik00d802b2017-04-11 10:34:31 -0700208 }
209 _incomingFrameTimesNanos[0] = rtc::TimeNanos();
niklase@google.com470e71d2011-07-07 08:21:25 +0000210}
211
ilnik00d802b2017-04-11 10:34:31 -0700212uint32_t VideoCaptureImpl::CalculateFrameRate(int64_t now_ns) {
213 int32_t num = 0;
214 int32_t nrOfFrames = 0;
215 for (num = 1; num < (kFrameRateCountHistorySize - 1); ++num) {
216 if (_incomingFrameTimesNanos[num] <= 0 ||
217 (now_ns - _incomingFrameTimesNanos[num]) /
218 rtc::kNumNanosecsPerMillisec >
219 kFrameRateHistoryWindowMs) { // don't use data older than 2sec
220 break;
221 } else {
222 nrOfFrames++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000223 }
ilnik00d802b2017-04-11 10:34:31 -0700224 }
225 if (num > 1) {
226 int64_t diff = (now_ns - _incomingFrameTimesNanos[num - 1]) /
227 rtc::kNumNanosecsPerMillisec;
228 if (diff > 0) {
229 return uint32_t((nrOfFrames * 1000.0f / diff) + 0.5f);
niklase@google.com470e71d2011-07-07 08:21:25 +0000230 }
ilnik00d802b2017-04-11 10:34:31 -0700231 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000232
ilnik00d802b2017-04-11 10:34:31 -0700233 return nrOfFrames;
niklase@google.com470e71d2011-07-07 08:21:25 +0000234}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000235} // namespace videocapturemodule
236} // namespace webrtc