blob: cc4c79750953d4f7001e582e05232a94dab4bbfa [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"
Henrik Kjellander98f53512015-10-28 18:17:40 +010023#include "webrtc/system_wrappers/include/logging.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
leozwang@webrtc.org1745e932012-03-01 16:30:40 +000035const char* VideoCaptureImpl::CurrentDeviceName() const
niklase@google.com470e71d2011-07-07 08:21:25 +000036{
37 return _deviceUniqueId;
38}
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:
57 return -1;;
58 }
59}
60
61// static
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000062int32_t VideoCaptureImpl::RotationInDegrees(VideoRotation rotation,
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000063 int* degrees) {
64 switch (rotation) {
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000065 case kVideoRotation_0:
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000066 *degrees = 0;
67 return 0;
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000068 case kVideoRotation_90:
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000069 *degrees = 90;
70 return 0;
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000071 case kVideoRotation_180:
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000072 *degrees = 180;
73 return 0;
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000074 case kVideoRotation_270:
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000075 *degrees = 270;
76 return 0;
77 }
78 return -1;
79}
80
nisseb29b9c82016-12-12 00:22:56 -080081VideoCaptureImpl::VideoCaptureImpl()
82 : _deviceUniqueId(NULL),
pbos@webrtc.org504af452013-07-02 10:15:43 +000083 _requestedCapability(),
Niels Möllerd28db7f2016-05-10 16:31:47 +020084 _lastProcessTimeNanos(rtc::TimeNanos()),
85 _lastFrameRateCallbackTimeNanos(rtc::TimeNanos()),
pbos@webrtc.org504af452013-07-02 10:15:43 +000086 _dataCallBack(NULL),
Niels Möllerd28db7f2016-05-10 16:31:47 +020087 _lastProcessFrameTimeNanos(rtc::TimeNanos()),
guoweis@webrtc.org59140d62015-03-09 17:07:31 +000088 _rotateFrame(kVideoRotation_0),
deadbeeff5629ad2016-03-18 11:38:26 -070089 apply_rotation_(false) {
niklase@google.com470e71d2011-07-07 08:21:25 +000090 _requestedCapability.width = kDefaultWidth;
91 _requestedCapability.height = kDefaultHeight;
92 _requestedCapability.maxFPS = 30;
93 _requestedCapability.rawType = kVideoI420;
Niels Möllerd28db7f2016-05-10 16:31:47 +020094 memset(_incomingFrameTimesNanos, 0, sizeof(_incomingFrameTimesNanos));
niklase@google.com470e71d2011-07-07 08:21:25 +000095}
96
97VideoCaptureImpl::~VideoCaptureImpl()
98{
99 DeRegisterCaptureDataCallback();
niklase@google.com470e71d2011-07-07 08:21:25 +0000100 if (_deviceUniqueId)
101 delete[] _deviceUniqueId;
102}
103
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000104void VideoCaptureImpl::RegisterCaptureDataCallback(
nisseb29b9c82016-12-12 00:22:56 -0800105 rtc::VideoSinkInterface<VideoFrame>* dataCallBack) {
kthelgasonff046c72017-03-31 02:03:55 -0700106 rtc::CritScope cs(&_apiCs);
nisseb29b9c82016-12-12 00:22:56 -0800107 _dataCallBack = dataCallBack;
niklase@google.com470e71d2011-07-07 08:21:25 +0000108}
109
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000110void VideoCaptureImpl::DeRegisterCaptureDataCallback() {
kthelgasonff046c72017-03-31 02:03:55 -0700111 rtc::CritScope cs(&_apiCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000112 _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
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000124int32_t VideoCaptureImpl::IncomingFrame(
125 uint8_t* videoFrame,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000126 size_t videoFrameLength,
mflodman@webrtc.org4f9e44f2012-02-23 09:00:26 +0000127 const VideoCaptureCapability& frameInfo,
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000128 int64_t captureTime/*=0*/)
niklase@google.com470e71d2011-07-07 08:21:25 +0000129{
kthelgasonff046c72017-03-31 02:03:55 -0700130 rtc::CritScope cs(&_apiCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000131
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000132 const int32_t width = frameInfo.width;
133 const int32_t height = frameInfo.height;
niklase@google.com470e71d2011-07-07 08:21:25 +0000134
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000135 TRACE_EVENT1("webrtc", "VC::IncomingFrame", "capture_time", captureTime);
136
nisse1e321222017-02-20 23:27:37 -0800137 // Not encoded, convert to I420.
138 const VideoType commonVideoType =
139 RawVideoTypeToCommonVideoVideoType(frameInfo.rawType);
140
141 if (frameInfo.rawType != kVideoMJPEG &&
142 CalcBufferSize(commonVideoType, width,
143 abs(height)) != videoFrameLength)
niklase@google.com470e71d2011-07-07 08:21:25 +0000144 {
nisse1e321222017-02-20 23:27:37 -0800145 LOG(LS_ERROR) << "Wrong incoming frame length.";
mflodman@webrtc.org3ba883f2013-06-07 13:57:57 +0000146 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000147 }
148
nisse1e321222017-02-20 23:27:37 -0800149 int stride_y = width;
150 int stride_uv = (width + 1) / 2;
151 int target_width = width;
152 int target_height = height;
153
154 // SetApplyRotation doesn't take any lock. Make a local copy here.
155 bool apply_rotation = apply_rotation_;
156
157 if (apply_rotation) {
158 // Rotating resolution when for 90/270 degree rotations.
159 if (_rotateFrame == kVideoRotation_90 ||
160 _rotateFrame == kVideoRotation_270) {
161 target_width = abs(height);
162 target_height = width;
163 }
164 }
165
166 // Setting absolute height (in case it was negative).
167 // In Windows, the image starts bottom left, instead of top left.
168 // Setting a negative source height, inverts the image (within LibYuv).
169
170 // TODO(nisse): Use a pool?
171 rtc::scoped_refptr<I420Buffer> buffer = I420Buffer::Create(
172 target_width, abs(target_height), stride_y, stride_uv, stride_uv);
173 const int conversionResult = ConvertToI420(
174 commonVideoType, videoFrame, 0, 0, // No cropping
175 width, height, videoFrameLength,
176 apply_rotation ? _rotateFrame : kVideoRotation_0, buffer.get());
177 if (conversionResult < 0)
178 {
179 LOG(LS_ERROR) << "Failed to convert capture frame from type "
180 << frameInfo.rawType << "to I420.";
181 return -1;
182 }
183
184 VideoFrame captureFrame(
185 buffer, 0, rtc::TimeMillis(),
186 !apply_rotation ? _rotateFrame : kVideoRotation_0);
187 captureFrame.set_ntp_time_ms(captureTime);
188
189 DeliverCapturedFrame(captureFrame);
190
niklase@google.com470e71d2011-07-07 08:21:25 +0000191 return 0;
wu@webrtc.orgf10ea312011-10-14 17:16:04 +0000192}
niklase@google.com470e71d2011-07-07 08:21:25 +0000193
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +0000194int32_t VideoCaptureImpl::SetCaptureRotation(VideoRotation rotation) {
kthelgasonff046c72017-03-31 02:03:55 -0700195 rtc::CritScope cs(&_apiCs);
guoweis@webrtc.org59140d62015-03-09 17:07:31 +0000196 _rotateFrame = rotation;
mikhal@webrtc.org0f34fd72012-11-19 21:15:35 +0000197 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000198}
199
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000200bool VideoCaptureImpl::SetApplyRotation(bool enable) {
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700201 // We can't take any lock here as it'll cause deadlock with IncomingFrame.
202
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000203 // The effect of this is the last caller wins.
204 apply_rotation_ = enable;
205 return true;
206}
207
niklase@google.com470e71d2011-07-07 08:21:25 +0000208void VideoCaptureImpl::UpdateFrameCount()
209{
Niels Möllerd28db7f2016-05-10 16:31:47 +0200210 if (_incomingFrameTimesNanos[0] / rtc::kNumNanosecsPerMicrosec == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000211 {
212 // first no shift
213 }
214 else
215 {
216 // shift
217 for (int i = (kFrameRateCountHistorySize - 2); i >= 0; i--)
218 {
Niels Möllerd28db7f2016-05-10 16:31:47 +0200219 _incomingFrameTimesNanos[i + 1] = _incomingFrameTimesNanos[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000220 }
221 }
Niels Möllerd28db7f2016-05-10 16:31:47 +0200222 _incomingFrameTimesNanos[0] = rtc::TimeNanos();
niklase@google.com470e71d2011-07-07 08:21:25 +0000223}
224
Niels Möllerd28db7f2016-05-10 16:31:47 +0200225uint32_t VideoCaptureImpl::CalculateFrameRate(int64_t now_ns)
niklase@google.com470e71d2011-07-07 08:21:25 +0000226{
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000227 int32_t num = 0;
228 int32_t nrOfFrames = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000229 for (num = 1; num < (kFrameRateCountHistorySize - 1); num++)
230 {
Niels Möllerd28db7f2016-05-10 16:31:47 +0200231 if (_incomingFrameTimesNanos[num] <= 0 ||
232 (now_ns - _incomingFrameTimesNanos[num]) /
233 rtc::kNumNanosecsPerMillisec >
234 kFrameRateHistoryWindowMs) // don't use data older than 2sec
niklase@google.com470e71d2011-07-07 08:21:25 +0000235 {
236 break;
237 }
238 else
239 {
240 nrOfFrames++;
241 }
242 }
243 if (num > 1)
244 {
Niels Möllerd28db7f2016-05-10 16:31:47 +0200245 int64_t diff = (now_ns - _incomingFrameTimesNanos[num - 1]) /
246 rtc::kNumNanosecsPerMillisec;
niklase@google.com470e71d2011-07-07 08:21:25 +0000247 if (diff > 0)
248 {
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000249 return uint32_t((nrOfFrames * 1000.0f / diff) + 0.5f);
niklase@google.com470e71d2011-07-07 08:21:25 +0000250 }
251 }
252
253 return nrOfFrames;
254}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000255} // namespace videocapturemodule
256} // namespace webrtc