blob: 06b4a2089675be6567cdabdc46696a6e46f17477 [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
pbos@webrtc.orga9b74ad2013-07-12 10:03:52 +000015#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
16#include "webrtc/modules/interface/module_common_types.h"
17#include "webrtc/modules/video_capture/video_capture_config.h"
18#include "webrtc/system_wrappers/interface/clock.h"
19#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
20#include "webrtc/system_wrappers/interface/ref_count.h"
21#include "webrtc/system_wrappers/interface/tick_util.h"
22#include "webrtc/system_wrappers/interface/trace.h"
23#include "webrtc/system_wrappers/interface/trace_event.h"
24
niklase@google.com470e71d2011-07-07 08:21:25 +000025namespace webrtc
26{
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +000027namespace videocapturemodule
niklase@google.com470e71d2011-07-07 08:21:25 +000028{
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +000029VideoCaptureModule* VideoCaptureImpl::Create(
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000030 const int32_t id,
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +000031 VideoCaptureExternal*& externalCapture)
32{
33 RefCountImpl<VideoCaptureImpl>* implementation =
34 new RefCountImpl<VideoCaptureImpl>(id);
niklase@google.com470e71d2011-07-07 08:21:25 +000035 externalCapture = implementation;
36 return implementation;
37}
38
leozwang@webrtc.org1745e932012-03-01 16:30:40 +000039const char* VideoCaptureImpl::CurrentDeviceName() const
niklase@google.com470e71d2011-07-07 08:21:25 +000040{
41 return _deviceUniqueId;
42}
43
fischman@webrtc.org4e65e072013-10-03 18:23:13 +000044// static
45int32_t VideoCaptureImpl::RotationFromDegrees(int degrees,
46 VideoCaptureRotation* rotation) {
47 switch (degrees) {
48 case 0:
49 *rotation = kCameraRotate0;
50 return 0;
51 case 90:
52 *rotation = kCameraRotate90;
53 return 0;
54 case 180:
55 *rotation = kCameraRotate180;
56 return 0;
57 case 270:
58 *rotation = kCameraRotate270;
59 return 0;
60 default:
61 return -1;;
62 }
63}
64
65// static
66int32_t VideoCaptureImpl::RotationInDegrees(VideoCaptureRotation rotation,
67 int* degrees) {
68 switch (rotation) {
69 case kCameraRotate0:
70 *degrees = 0;
71 return 0;
72 case kCameraRotate90:
73 *degrees = 90;
74 return 0;
75 case kCameraRotate180:
76 *degrees = 180;
77 return 0;
78 case kCameraRotate270:
79 *degrees = 270;
80 return 0;
81 }
82 return -1;
83}
84
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000085int32_t VideoCaptureImpl::ChangeUniqueId(const int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +000086{
87 _id = id;
88 return 0;
89}
90
91// returns the number of milliseconds until the module want a worker thread to call Process
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000092int32_t VideoCaptureImpl::TimeUntilNextProcess()
niklase@google.com470e71d2011-07-07 08:21:25 +000093{
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +000094 CriticalSectionScoped cs(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +000095
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000096 int32_t timeToNormalProcess = kProcessInterval
97 - (int32_t)((TickTime::Now() - _lastProcessTime).Milliseconds());
mikhal@webrtc.org9a5b9042012-10-19 16:44:35 +000098
99 return timeToNormalProcess;
niklase@google.com470e71d2011-07-07 08:21:25 +0000100}
101
102// Process any pending tasks such as timeouts
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000103int32_t VideoCaptureImpl::Process()
niklase@google.com470e71d2011-07-07 08:21:25 +0000104{
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000105 CriticalSectionScoped cs(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
107 const TickTime now = TickTime::Now();
108 _lastProcessTime = TickTime::Now();
109
110 // Handle No picture alarm
111
112 if (_lastProcessFrameCount.Ticks() == _incomingFrameTimes[0].Ticks() &&
113 _captureAlarm != Raised)
114 {
115 if (_noPictureAlarmCallBack && _captureCallBack)
116 {
117 _captureAlarm = Raised;
118 _captureCallBack->OnNoPictureAlarm(_id, _captureAlarm);
119 }
120 }
121 else if (_lastProcessFrameCount.Ticks() != _incomingFrameTimes[0].Ticks() &&
122 _captureAlarm != Cleared)
123 {
124 if (_noPictureAlarmCallBack && _captureCallBack)
125 {
126 _captureAlarm = Cleared;
127 _captureCallBack->OnNoPictureAlarm(_id, _captureAlarm);
128
129 }
130 }
131
132 // Handle frame rate callback
133 if ((now - _lastFrameRateCallbackTime).Milliseconds()
134 > kFrameRateCallbackInterval)
135 {
136 if (_frameRateCallBack && _captureCallBack)
137 {
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000138 const uint32_t frameRate = CalculateFrameRate(now);
niklase@google.com470e71d2011-07-07 08:21:25 +0000139 _captureCallBack->OnCaptureFrameRate(_id, frameRate);
140 }
141 _lastFrameRateCallbackTime = now; // Can be set by EnableFrameRateCallback
142
143 }
144
145 _lastProcessFrameCount = _incomingFrameTimes[0];
146
niklase@google.com470e71d2011-07-07 08:21:25 +0000147 return 0;
148}
149
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000150VideoCaptureImpl::VideoCaptureImpl(const int32_t id)
pbos@webrtc.org504af452013-07-02 10:15:43 +0000151 : _id(id),
152 _deviceUniqueId(NULL),
153 _apiCs(*CriticalSectionWrapper::CreateCriticalSection()),
154 _captureDelay(0),
155 _requestedCapability(),
niklase@google.com470e71d2011-07-07 08:21:25 +0000156 _callBackCs(*CriticalSectionWrapper::CreateCriticalSection()),
157 _lastProcessTime(TickTime::Now()),
pbos@webrtc.org504af452013-07-02 10:15:43 +0000158 _lastFrameRateCallbackTime(TickTime::Now()),
159 _frameRateCallBack(false),
160 _noPictureAlarmCallBack(false),
161 _captureAlarm(Cleared),
162 _setCaptureDelay(0),
163 _dataCallBack(NULL),
164 _captureCallBack(NULL),
165 _lastProcessFrameCount(TickTime::Now()),
166 _rotateFrame(kRotateNone),
pbos@webrtc.orge0536292013-10-21 09:02:30 +0000167 last_capture_time_(0),
pbos@webrtc.org504af452013-07-02 10:15:43 +0000168 delta_ntp_internal_ms_(
169 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds() -
170 TickTime::MillisecondTimestamp()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000171 _requestedCapability.width = kDefaultWidth;
172 _requestedCapability.height = kDefaultHeight;
173 _requestedCapability.maxFPS = 30;
174 _requestedCapability.rawType = kVideoI420;
175 _requestedCapability.codecType = kVideoCodecUnknown;
176 memset(_incomingFrameTimes, 0, sizeof(_incomingFrameTimes));
177}
178
179VideoCaptureImpl::~VideoCaptureImpl()
180{
181 DeRegisterCaptureDataCallback();
182 DeRegisterCaptureCallback();
183 delete &_callBackCs;
184 delete &_apiCs;
185
186 if (_deviceUniqueId)
187 delete[] _deviceUniqueId;
188}
189
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000190void VideoCaptureImpl::RegisterCaptureDataCallback(
191 VideoCaptureDataCallback& dataCallBack) {
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000192 CriticalSectionScoped cs(&_apiCs);
193 CriticalSectionScoped cs2(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000194 _dataCallBack = &dataCallBack;
niklase@google.com470e71d2011-07-07 08:21:25 +0000195}
196
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000197void VideoCaptureImpl::DeRegisterCaptureDataCallback() {
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000198 CriticalSectionScoped cs(&_apiCs);
199 CriticalSectionScoped cs2(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000200 _dataCallBack = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000201}
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000202void VideoCaptureImpl::RegisterCaptureCallback(VideoCaptureFeedBack& callBack) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000203
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000204 CriticalSectionScoped cs(&_apiCs);
205 CriticalSectionScoped cs2(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000206 _captureCallBack = &callBack;
niklase@google.com470e71d2011-07-07 08:21:25 +0000207}
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000208void VideoCaptureImpl::DeRegisterCaptureCallback() {
niklase@google.com470e71d2011-07-07 08:21:25 +0000209
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000210 CriticalSectionScoped cs(&_apiCs);
211 CriticalSectionScoped cs2(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000212 _captureCallBack = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000213}
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000214void VideoCaptureImpl::SetCaptureDelay(int32_t delayMS) {
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000215 CriticalSectionScoped cs(&_apiCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000216 _captureDelay = delayMS;
niklase@google.com470e71d2011-07-07 08:21:25 +0000217}
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000218int32_t VideoCaptureImpl::CaptureDelay()
niklase@google.com470e71d2011-07-07 08:21:25 +0000219{
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000220 CriticalSectionScoped cs(&_apiCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000221 return _setCaptureDelay;
222}
wu@webrtc.orgf10ea312011-10-14 17:16:04 +0000223
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000224int32_t VideoCaptureImpl::DeliverCapturedFrame(I420VideoFrame& captureFrame,
225 int64_t capture_time) {
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000226 UpdateFrameCount(); // frame count used for local frame rate callback.
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000227
228 const bool callOnCaptureDelayChanged = _setCaptureDelay != _captureDelay;
229 // Capture delay changed
230 if (_setCaptureDelay != _captureDelay) {
231 _setCaptureDelay = _captureDelay;
232 }
233
234 // Set the capture time
235 if (capture_time != 0) {
andresp@webrtc.org77bf5c22013-09-04 11:35:43 +0000236 captureFrame.set_render_time_ms(capture_time - delta_ntp_internal_ms_);
pbos@webrtc.org504af452013-07-02 10:15:43 +0000237 } else {
238 captureFrame.set_render_time_ms(TickTime::MillisecondTimestamp());
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000239 }
240
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000241 if (captureFrame.render_time_ms() == last_capture_time_) {
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000242 // We don't allow the same capture time for two frames, drop this one.
243 return -1;
244 }
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000245 last_capture_time_ = captureFrame.render_time_ms();
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000246
247 if (_dataCallBack) {
248 if (callOnCaptureDelayChanged) {
249 _dataCallBack->OnCaptureDelayChanged(_id, _captureDelay);
250 }
mikhal@webrtc.orge83d3112012-10-29 15:59:40 +0000251 _dataCallBack->OnIncomingCapturedFrame(_id, captureFrame);
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000252 }
253
254 return 0;
255}
256
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000257int32_t VideoCaptureImpl::IncomingFrame(
258 uint8_t* videoFrame,
259 int32_t videoFrameLength,
mflodman@webrtc.org4f9e44f2012-02-23 09:00:26 +0000260 const VideoCaptureCapability& frameInfo,
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000261 int64_t captureTime/*=0*/)
niklase@google.com470e71d2011-07-07 08:21:25 +0000262{
263 WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceVideoCapture, _id,
264 "IncomingFrame width %d, height %d", (int) frameInfo.width,
265 (int) frameInfo.height);
266
267 TickTime startProcessTime = TickTime::Now();
268
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000269 CriticalSectionScoped cs(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000270
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000271 const int32_t width = frameInfo.width;
272 const int32_t height = frameInfo.height;
niklase@google.com470e71d2011-07-07 08:21:25 +0000273
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000274 TRACE_EVENT1("webrtc", "VC::IncomingFrame", "capture_time", captureTime);
275
mflodman@webrtc.org4f9e44f2012-02-23 09:00:26 +0000276 if (frameInfo.codecType == kVideoCodecUnknown)
niklase@google.com470e71d2011-07-07 08:21:25 +0000277 {
mflodman@webrtc.org4f9e44f2012-02-23 09:00:26 +0000278 // Not encoded, convert to I420.
mikhal@webrtc.orge39de162011-12-27 23:45:30 +0000279 const VideoType commonVideoType =
mflodman@webrtc.org4f9e44f2012-02-23 09:00:26 +0000280 RawVideoTypeToCommonVideoVideoType(frameInfo.rawType);
281
282 if (frameInfo.rawType != kVideoMJPEG &&
elham@webrtc.org5f49dba2012-04-23 21:24:02 +0000283 CalcBufferSize(commonVideoType, width,
284 abs(height)) != videoFrameLength)
niklase@google.com470e71d2011-07-07 08:21:25 +0000285 {
286 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id,
mflodman@webrtc.org4f9e44f2012-02-23 09:00:26 +0000287 "Wrong incoming frame length.");
niklase@google.com470e71d2011-07-07 08:21:25 +0000288 return -1;
289 }
290
mikhal@webrtc.org4c4d01d2012-11-21 22:18:32 +0000291 int stride_y = width;
292 int stride_uv = (width + 1) / 2;
mikhal@webrtc.org0f34fd72012-11-19 21:15:35 +0000293 int target_width = width;
294 int target_height = height;
295 // Rotating resolution when for 90/270 degree rotations.
296 if (_rotateFrame == kRotate90 || _rotateFrame == kRotate270) {
297 target_width = abs(height);
298 target_height = width;
299 }
mikhal@webrtc.org4c4d01d2012-11-21 22:18:32 +0000300 // TODO(mikhal): Update correct aligned stride values.
301 //Calc16ByteAlignedStride(target_width, &stride_y, &stride_uv);
mikhal@webrtc.org2f4ff892012-09-24 21:09:54 +0000302 // Setting absolute height (in case it was negative).
303 // In Windows, the image starts bottom left, instead of top left.
304 // Setting a negative source height, inverts the image (within LibYuv).
sheu@chromium.org5dd2ecb2013-10-31 23:41:04 +0000305 int ret = _captureFrame.CreateEmptyFrame(target_width,
306 abs(target_height),
307 stride_y,
308 stride_uv, stride_uv);
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000309 if (ret < 0)
310 {
311 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id,
312 "Failed to allocate I420 frame.");
313 return -1;
314 }
mikhal@webrtc.orga58888d2012-01-04 19:23:24 +0000315 const int conversionResult = ConvertToI420(commonVideoType,
316 videoFrame,
317 0, 0, // No cropping
318 width, height,
mflodman@webrtc.orgf3811192012-02-27 08:10:17 +0000319 videoFrameLength,
mikhal@webrtc.orga58888d2012-01-04 19:23:24 +0000320 _rotateFrame,
sheu@chromium.org5dd2ecb2013-10-31 23:41:04 +0000321 &_captureFrame);
mikhal@webrtc.org2ab104e2011-12-09 02:46:22 +0000322 if (conversionResult < 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000323 {
324 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id,
325 "Failed to convert capture frame from type %d to I420",
326 frameInfo.rawType);
327 return -1;
328 }
sheu@chromium.org5dd2ecb2013-10-31 23:41:04 +0000329 DeliverCapturedFrame(_captureFrame, captureTime);
niklase@google.com470e71d2011-07-07 08:21:25 +0000330 }
331 else // Encoded format
332 {
mflodman@webrtc.org3ba883f2013-06-07 13:57:57 +0000333 assert(false);
334 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000335 }
336
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000337 const uint32_t processTime =
338 (uint32_t)(TickTime::Now() - startProcessTime).Milliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000339 if (processTime > 10) // If the process time is too long MJPG will not work well.
340 {
341 WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideoCapture, _id,
342 "Too long processing time of Incoming frame: %ums",
343 (unsigned int) processTime);
344 }
345
346 return 0;
wu@webrtc.orgf10ea312011-10-14 17:16:04 +0000347}
niklase@google.com470e71d2011-07-07 08:21:25 +0000348
pbos@webrtc.org2ffb1492013-11-22 13:10:13 +0000349int32_t VideoCaptureImpl::IncomingI420VideoFrame(I420VideoFrame* video_frame,
350 int64_t captureTime) {
wu@webrtc.orgf10ea312011-10-14 17:16:04 +0000351
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000352 CriticalSectionScoped cs(&_callBackCs);
pbos@webrtc.org2ffb1492013-11-22 13:10:13 +0000353 DeliverCapturedFrame(*video_frame, captureTime);
wu@webrtc.orgf10ea312011-10-14 17:16:04 +0000354
355 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000356}
357
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000358int32_t VideoCaptureImpl::SetCaptureRotation(VideoCaptureRotation rotation) {
mikhal@webrtc.org0f34fd72012-11-19 21:15:35 +0000359 CriticalSectionScoped cs(&_apiCs);
360 CriticalSectionScoped cs2(&_callBackCs);
361 switch (rotation){
362 case kCameraRotate0:
363 _rotateFrame = kRotateNone;
364 break;
365 case kCameraRotate90:
366 _rotateFrame = kRotate90;
367 break;
368 case kCameraRotate180:
369 _rotateFrame = kRotate180;
370 break;
371 case kCameraRotate270:
372 _rotateFrame = kRotate270;
373 break;
fischman@webrtc.org4e65e072013-10-03 18:23:13 +0000374 default:
375 return -1;
mikhal@webrtc.org0f34fd72012-11-19 21:15:35 +0000376 }
377 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000378}
379
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000380void VideoCaptureImpl::EnableFrameRateCallback(const bool enable) {
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000381 CriticalSectionScoped cs(&_apiCs);
382 CriticalSectionScoped cs2(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000383 _frameRateCallBack = enable;
384 if (enable)
385 {
386 _lastFrameRateCallbackTime = TickTime::Now();
387 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000388}
389
mallinath@webrtc.org7433a082014-01-29 00:56:02 +0000390void VideoCaptureImpl::EnableNoPictureAlarm(const bool enable) {
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000391 CriticalSectionScoped cs(&_apiCs);
392 CriticalSectionScoped cs2(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000393 _noPictureAlarmCallBack = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +0000394}
395
396void VideoCaptureImpl::UpdateFrameCount()
397{
398 if (_incomingFrameTimes[0].MicrosecondTimestamp() == 0)
399 {
400 // first no shift
401 }
402 else
403 {
404 // shift
405 for (int i = (kFrameRateCountHistorySize - 2); i >= 0; i--)
406 {
407 _incomingFrameTimes[i + 1] = _incomingFrameTimes[i];
408 }
409 }
410 _incomingFrameTimes[0] = TickTime::Now();
411}
412
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000413uint32_t VideoCaptureImpl::CalculateFrameRate(const TickTime& now)
niklase@google.com470e71d2011-07-07 08:21:25 +0000414{
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000415 int32_t num = 0;
416 int32_t nrOfFrames = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000417 for (num = 1; num < (kFrameRateCountHistorySize - 1); num++)
418 {
419 if (_incomingFrameTimes[num].Ticks() <= 0
420 || (now - _incomingFrameTimes[num]).Milliseconds() > kFrameRateHistoryWindowMs) // don't use data older than 2sec
421 {
422 break;
423 }
424 else
425 {
426 nrOfFrames++;
427 }
428 }
429 if (num > 1)
430 {
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000431 int64_t diff = (now - _incomingFrameTimes[num - 1]).Milliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000432 if (diff > 0)
433 {
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000434 return uint32_t((nrOfFrames * 1000.0f / diff) + 0.5f);
niklase@google.com470e71d2011-07-07 08:21:25 +0000435 }
436 }
437
438 return nrOfFrames;
439}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000440} // namespace videocapturemodule
441} // namespace webrtc