blob: 90730cd98429e75dd97db3f116273254e2b18073 [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
tommie4f96502015-10-20 23:00:48 -070015#include "webrtc/base/trace_event.h"
pbos@webrtc.orga9b74ad2013-07-12 10:03:52 +000016#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010017#include "webrtc/modules/include/module_common_types.h"
pbos@webrtc.orga9b74ad2013-07-12 10:03:52 +000018#include "webrtc/modules/video_capture/video_capture_config.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010019#include "webrtc/system_wrappers/include/clock.h"
20#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
21#include "webrtc/system_wrappers/include/logging.h"
22#include "webrtc/system_wrappers/include/ref_count.h"
23#include "webrtc/system_wrappers/include/tick_util.h"
pbos@webrtc.orga9b74ad2013-07-12 10:03:52 +000024
niklase@google.com470e71d2011-07-07 08:21:25 +000025namespace webrtc
26{
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000027
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +000028namespace videocapturemodule
niklase@google.com470e71d2011-07-07 08:21:25 +000029{
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +000030VideoCaptureModule* VideoCaptureImpl::Create(
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000031 const int32_t id,
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +000032 VideoCaptureExternal*& externalCapture)
33{
34 RefCountImpl<VideoCaptureImpl>* implementation =
35 new RefCountImpl<VideoCaptureImpl>(id);
niklase@google.com470e71d2011-07-07 08:21:25 +000036 externalCapture = implementation;
37 return implementation;
38}
39
leozwang@webrtc.org1745e932012-03-01 16:30:40 +000040const char* VideoCaptureImpl::CurrentDeviceName() const
niklase@google.com470e71d2011-07-07 08:21:25 +000041{
42 return _deviceUniqueId;
43}
44
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000045// static
46int32_t VideoCaptureImpl::RotationFromDegrees(int degrees,
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000047 VideoRotation* rotation) {
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000048 switch (degrees) {
49 case 0:
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000050 *rotation = kVideoRotation_0;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000051 return 0;
52 case 90:
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000053 *rotation = kVideoRotation_90;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000054 return 0;
55 case 180:
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000056 *rotation = kVideoRotation_180;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000057 return 0;
58 case 270:
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000059 *rotation = kVideoRotation_270;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000060 return 0;
61 default:
62 return -1;;
63 }
64}
65
66// static
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000067int32_t VideoCaptureImpl::RotationInDegrees(VideoRotation rotation,
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000068 int* degrees) {
69 switch (rotation) {
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000070 case kVideoRotation_0:
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000071 *degrees = 0;
72 return 0;
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000073 case kVideoRotation_90:
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000074 *degrees = 90;
75 return 0;
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000076 case kVideoRotation_180:
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000077 *degrees = 180;
78 return 0;
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +000079 case kVideoRotation_270:
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000080 *degrees = 270;
81 return 0;
82 }
83 return -1;
84}
85
niklase@google.com470e71d2011-07-07 08:21:25 +000086// returns the number of milliseconds until the module want a worker thread to call Process
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000087int64_t VideoCaptureImpl::TimeUntilNextProcess()
niklase@google.com470e71d2011-07-07 08:21:25 +000088{
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +000089 CriticalSectionScoped cs(&_callBackCs);
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000090 const int64_t kProcessIntervalMs = 300;
91 return kProcessIntervalMs -
92 (TickTime::Now() - _lastProcessTime).Milliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +000093}
94
95// Process any pending tasks such as timeouts
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000096int32_t VideoCaptureImpl::Process()
niklase@google.com470e71d2011-07-07 08:21:25 +000097{
mflodman@webrtc.org7845d072012-03-08 08:09:17 +000098 CriticalSectionScoped cs(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +000099
100 const TickTime now = TickTime::Now();
101 _lastProcessTime = TickTime::Now();
102
103 // Handle No picture alarm
104
105 if (_lastProcessFrameCount.Ticks() == _incomingFrameTimes[0].Ticks() &&
106 _captureAlarm != Raised)
107 {
108 if (_noPictureAlarmCallBack && _captureCallBack)
109 {
110 _captureAlarm = Raised;
111 _captureCallBack->OnNoPictureAlarm(_id, _captureAlarm);
112 }
113 }
114 else if (_lastProcessFrameCount.Ticks() != _incomingFrameTimes[0].Ticks() &&
115 _captureAlarm != Cleared)
116 {
117 if (_noPictureAlarmCallBack && _captureCallBack)
118 {
119 _captureAlarm = Cleared;
120 _captureCallBack->OnNoPictureAlarm(_id, _captureAlarm);
121
122 }
123 }
124
125 // Handle frame rate callback
126 if ((now - _lastFrameRateCallbackTime).Milliseconds()
127 > kFrameRateCallbackInterval)
128 {
129 if (_frameRateCallBack && _captureCallBack)
130 {
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000131 const uint32_t frameRate = CalculateFrameRate(now);
niklase@google.com470e71d2011-07-07 08:21:25 +0000132 _captureCallBack->OnCaptureFrameRate(_id, frameRate);
133 }
134 _lastFrameRateCallbackTime = now; // Can be set by EnableFrameRateCallback
135
136 }
137
138 _lastProcessFrameCount = _incomingFrameTimes[0];
139
niklase@google.com470e71d2011-07-07 08:21:25 +0000140 return 0;
141}
142
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000143VideoCaptureImpl::VideoCaptureImpl(const int32_t id)
pbos@webrtc.org504af452013-07-02 10:15:43 +0000144 : _id(id),
145 _deviceUniqueId(NULL),
146 _apiCs(*CriticalSectionWrapper::CreateCriticalSection()),
147 _captureDelay(0),
148 _requestedCapability(),
niklase@google.com470e71d2011-07-07 08:21:25 +0000149 _callBackCs(*CriticalSectionWrapper::CreateCriticalSection()),
150 _lastProcessTime(TickTime::Now()),
pbos@webrtc.org504af452013-07-02 10:15:43 +0000151 _lastFrameRateCallbackTime(TickTime::Now()),
152 _frameRateCallBack(false),
153 _noPictureAlarmCallBack(false),
154 _captureAlarm(Cleared),
155 _setCaptureDelay(0),
156 _dataCallBack(NULL),
157 _captureCallBack(NULL),
158 _lastProcessFrameCount(TickTime::Now()),
guoweis@webrtc.org59140d62015-03-09 17:07:31 +0000159 _rotateFrame(kVideoRotation_0),
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000160 apply_rotation_(true) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000161 _requestedCapability.width = kDefaultWidth;
162 _requestedCapability.height = kDefaultHeight;
163 _requestedCapability.maxFPS = 30;
164 _requestedCapability.rawType = kVideoI420;
165 _requestedCapability.codecType = kVideoCodecUnknown;
166 memset(_incomingFrameTimes, 0, sizeof(_incomingFrameTimes));
167}
168
169VideoCaptureImpl::~VideoCaptureImpl()
170{
171 DeRegisterCaptureDataCallback();
172 DeRegisterCaptureCallback();
173 delete &_callBackCs;
174 delete &_apiCs;
175
176 if (_deviceUniqueId)
177 delete[] _deviceUniqueId;
178}
179
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000180void VideoCaptureImpl::RegisterCaptureDataCallback(
181 VideoCaptureDataCallback& dataCallBack) {
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000182 CriticalSectionScoped cs(&_apiCs);
183 CriticalSectionScoped cs2(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000184 _dataCallBack = &dataCallBack;
niklase@google.com470e71d2011-07-07 08:21:25 +0000185}
186
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000187void VideoCaptureImpl::DeRegisterCaptureDataCallback() {
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000188 CriticalSectionScoped cs(&_apiCs);
189 CriticalSectionScoped cs2(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000190 _dataCallBack = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000191}
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000192void VideoCaptureImpl::RegisterCaptureCallback(VideoCaptureFeedBack& callBack) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000193
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000194 CriticalSectionScoped cs(&_apiCs);
195 CriticalSectionScoped cs2(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000196 _captureCallBack = &callBack;
niklase@google.com470e71d2011-07-07 08:21:25 +0000197}
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000198void VideoCaptureImpl::DeRegisterCaptureCallback() {
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000200 CriticalSectionScoped cs(&_apiCs);
201 CriticalSectionScoped cs2(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000202 _captureCallBack = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000203}
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000204void VideoCaptureImpl::SetCaptureDelay(int32_t delayMS) {
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000205 CriticalSectionScoped cs(&_apiCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000206 _captureDelay = delayMS;
niklase@google.com470e71d2011-07-07 08:21:25 +0000207}
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000208int32_t VideoCaptureImpl::CaptureDelay()
niklase@google.com470e71d2011-07-07 08:21:25 +0000209{
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000210 CriticalSectionScoped cs(&_apiCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000211 return _setCaptureDelay;
212}
wu@webrtc.orgf10ea312011-10-14 17:16:04 +0000213
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700214int32_t VideoCaptureImpl::DeliverCapturedFrame(VideoFrame& captureFrame) {
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000215 UpdateFrameCount(); // frame count used for local frame rate callback.
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000216
217 const bool callOnCaptureDelayChanged = _setCaptureDelay != _captureDelay;
218 // Capture delay changed
219 if (_setCaptureDelay != _captureDelay) {
220 _setCaptureDelay = _captureDelay;
221 }
222
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000223 if (_dataCallBack) {
224 if (callOnCaptureDelayChanged) {
225 _dataCallBack->OnCaptureDelayChanged(_id, _captureDelay);
226 }
mikhal@webrtc.orge83d3112012-10-29 15:59:40 +0000227 _dataCallBack->OnIncomingCapturedFrame(_id, captureFrame);
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000228 }
229
230 return 0;
231}
232
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000233int32_t VideoCaptureImpl::IncomingFrame(
234 uint8_t* videoFrame,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000235 size_t videoFrameLength,
mflodman@webrtc.org4f9e44f2012-02-23 09:00:26 +0000236 const VideoCaptureCapability& frameInfo,
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000237 int64_t captureTime/*=0*/)
niklase@google.com470e71d2011-07-07 08:21:25 +0000238{
fischman@webrtc.org42694c52014-06-06 18:28:28 +0000239 CriticalSectionScoped cs(&_apiCs);
240 CriticalSectionScoped cs2(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000241
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000242 const int32_t width = frameInfo.width;
243 const int32_t height = frameInfo.height;
niklase@google.com470e71d2011-07-07 08:21:25 +0000244
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000245 TRACE_EVENT1("webrtc", "VC::IncomingFrame", "capture_time", captureTime);
246
mflodman@webrtc.org4f9e44f2012-02-23 09:00:26 +0000247 if (frameInfo.codecType == kVideoCodecUnknown)
niklase@google.com470e71d2011-07-07 08:21:25 +0000248 {
mflodman@webrtc.org4f9e44f2012-02-23 09:00:26 +0000249 // Not encoded, convert to I420.
mikhal@webrtc.orge39de162011-12-27 23:45:30 +0000250 const VideoType commonVideoType =
mflodman@webrtc.org4f9e44f2012-02-23 09:00:26 +0000251 RawVideoTypeToCommonVideoVideoType(frameInfo.rawType);
252
253 if (frameInfo.rawType != kVideoMJPEG &&
elham@webrtc.org5f49dba2012-04-23 21:24:02 +0000254 CalcBufferSize(commonVideoType, width,
255 abs(height)) != videoFrameLength)
niklase@google.com470e71d2011-07-07 08:21:25 +0000256 {
pbos@webrtc.org3d5cb332014-05-14 08:42:07 +0000257 LOG(LS_ERROR) << "Wrong incoming frame length.";
niklase@google.com470e71d2011-07-07 08:21:25 +0000258 return -1;
259 }
260
mikhal@webrtc.org4c4d01d2012-11-21 22:18:32 +0000261 int stride_y = width;
262 int stride_uv = (width + 1) / 2;
mikhal@webrtc.org0f34fd72012-11-19 21:15:35 +0000263 int target_width = width;
264 int target_height = height;
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000265
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700266 // SetApplyRotation doesn't take any lock. Make a local copy here.
267 bool apply_rotation = apply_rotation_;
268
269 if (apply_rotation) {
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000270 // Rotating resolution when for 90/270 degree rotations.
guoweis@webrtc.org59140d62015-03-09 17:07:31 +0000271 if (_rotateFrame == kVideoRotation_90 ||
272 _rotateFrame == kVideoRotation_270) {
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000273 target_width = abs(height);
274 target_height = width;
275 }
mikhal@webrtc.org0f34fd72012-11-19 21:15:35 +0000276 }
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000277
mikhal@webrtc.org4c4d01d2012-11-21 22:18:32 +0000278 // TODO(mikhal): Update correct aligned stride values.
279 //Calc16ByteAlignedStride(target_width, &stride_y, &stride_uv);
mikhal@webrtc.org2f4ff892012-09-24 21:09:54 +0000280 // Setting absolute height (in case it was negative).
281 // In Windows, the image starts bottom left, instead of top left.
282 // Setting a negative source height, inverts the image (within LibYuv).
sheu@chromium.org5dd2ecb2013-10-31 23:41:04 +0000283 int ret = _captureFrame.CreateEmptyFrame(target_width,
284 abs(target_height),
285 stride_y,
286 stride_uv, stride_uv);
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000287 if (ret < 0)
288 {
pbos@webrtc.org3d5cb332014-05-14 08:42:07 +0000289 LOG(LS_ERROR) << "Failed to create empty frame, this should only "
290 "happen due to bad parameters.";
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000291 return -1;
292 }
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000293 const int conversionResult = ConvertToI420(
294 commonVideoType, videoFrame, 0, 0, // No cropping
295 width, height, videoFrameLength,
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700296 apply_rotation ? _rotateFrame : kVideoRotation_0, &_captureFrame);
mikhal@webrtc.org2ab104e2011-12-09 02:46:22 +0000297 if (conversionResult < 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000298 {
pbos@webrtc.org3d5cb332014-05-14 08:42:07 +0000299 LOG(LS_ERROR) << "Failed to convert capture frame from type "
300 << frameInfo.rawType << "to I420.";
niklase@google.com470e71d2011-07-07 08:21:25 +0000301 return -1;
302 }
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000303
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700304 if (!apply_rotation) {
guoweis@webrtc.org59140d62015-03-09 17:07:31 +0000305 _captureFrame.set_rotation(_rotateFrame);
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000306 } else {
307 _captureFrame.set_rotation(kVideoRotation_0);
308 }
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000309 _captureFrame.set_ntp_time_ms(captureTime);
310 _captureFrame.set_render_time_ms(TickTime::MillisecondTimestamp());
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000311
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000312 DeliverCapturedFrame(_captureFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000313 }
314 else // Encoded format
315 {
mflodman@webrtc.org3ba883f2013-06-07 13:57:57 +0000316 assert(false);
317 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000318 }
319
niklase@google.com470e71d2011-07-07 08:21:25 +0000320 return 0;
wu@webrtc.orgf10ea312011-10-14 17:16:04 +0000321}
niklase@google.com470e71d2011-07-07 08:21:25 +0000322
guoweis@webrtc.org5a7dc392015-02-13 14:31:26 +0000323int32_t VideoCaptureImpl::SetCaptureRotation(VideoRotation rotation) {
mikhal@webrtc.org0f34fd72012-11-19 21:15:35 +0000324 CriticalSectionScoped cs(&_apiCs);
325 CriticalSectionScoped cs2(&_callBackCs);
guoweis@webrtc.org59140d62015-03-09 17:07:31 +0000326 _rotateFrame = rotation;
mikhal@webrtc.org0f34fd72012-11-19 21:15:35 +0000327 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000328}
329
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000330void VideoCaptureImpl::EnableFrameRateCallback(const bool enable) {
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000331 CriticalSectionScoped cs(&_apiCs);
332 CriticalSectionScoped cs2(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000333 _frameRateCallBack = enable;
334 if (enable)
335 {
336 _lastFrameRateCallbackTime = TickTime::Now();
337 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000338}
339
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000340bool VideoCaptureImpl::SetApplyRotation(bool enable) {
Guo-wei Shieh64c1e8c2015-04-01 15:33:06 -0700341 // We can't take any lock here as it'll cause deadlock with IncomingFrame.
342
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000343 // The effect of this is the last caller wins.
344 apply_rotation_ = enable;
345 return true;
346}
347
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000348void VideoCaptureImpl::EnableNoPictureAlarm(const bool enable) {
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000349 CriticalSectionScoped cs(&_apiCs);
350 CriticalSectionScoped cs2(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000351 _noPictureAlarmCallBack = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +0000352}
353
354void VideoCaptureImpl::UpdateFrameCount()
355{
356 if (_incomingFrameTimes[0].MicrosecondTimestamp() == 0)
357 {
358 // first no shift
359 }
360 else
361 {
362 // shift
363 for (int i = (kFrameRateCountHistorySize - 2); i >= 0; i--)
364 {
365 _incomingFrameTimes[i + 1] = _incomingFrameTimes[i];
366 }
367 }
368 _incomingFrameTimes[0] = TickTime::Now();
369}
370
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000371uint32_t VideoCaptureImpl::CalculateFrameRate(const TickTime& now)
niklase@google.com470e71d2011-07-07 08:21:25 +0000372{
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000373 int32_t num = 0;
374 int32_t nrOfFrames = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000375 for (num = 1; num < (kFrameRateCountHistorySize - 1); num++)
376 {
377 if (_incomingFrameTimes[num].Ticks() <= 0
378 || (now - _incomingFrameTimes[num]).Milliseconds() > kFrameRateHistoryWindowMs) // don't use data older than 2sec
379 {
380 break;
381 }
382 else
383 {
384 nrOfFrames++;
385 }
386 }
387 if (num > 1)
388 {
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000389 int64_t diff = (now - _incomingFrameTimes[num - 1]).Milliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000390 if (diff > 0)
391 {
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000392 return uint32_t((nrOfFrames * 1000.0f / diff) + 0.5f);
niklase@google.com470e71d2011-07-07 08:21:25 +0000393 }
394 }
395
396 return nrOfFrames;
397}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000398} // namespace videocapturemodule
399} // namespace webrtc