blob: 9192dd6c6382bdd284e4aa6dd679d1fa1c78d4cf [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
11#include "video_capture_impl.h"
mikhal@webrtc.orge39de162011-12-27 23:45:30 +000012
andrew@webrtc.orgc1354bd2012-07-27 18:21:16 +000013#include "common_video/libyuv/include/webrtc_libyuv.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000014#include "critical_section_wrapper.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000015#include "module_common_types.h"
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +000016#include "ref_count.h"
mikhal@webrtc.orge39de162011-12-27 23:45:30 +000017#include "tick_util.h"
18#include "trace.h"
hclam@chromium.org806dc3b2013-04-09 19:54:10 +000019#include "trace_event.h"
mikhal@webrtc.orge39de162011-12-27 23:45:30 +000020#include "video_capture_config.h"
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000021#include "webrtc/system_wrappers/interface/clock.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
elham@webrtc.org5f49dba2012-04-23 21:24:02 +000023#include <stdlib.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
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000044int32_t VideoCaptureImpl::ChangeUniqueId(const int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +000045{
46 _id = id;
47 return 0;
48}
49
50// returns the number of milliseconds until the module want a worker thread to call Process
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000051int32_t VideoCaptureImpl::TimeUntilNextProcess()
niklase@google.com470e71d2011-07-07 08:21:25 +000052{
perkj@webrtc.orgc2fde802012-08-08 14:01:09 +000053 CriticalSectionScoped cs(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +000054
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000055 int32_t timeToNormalProcess = kProcessInterval
56 - (int32_t)((TickTime::Now() - _lastProcessTime).Milliseconds());
mikhal@webrtc.org9a5b9042012-10-19 16:44:35 +000057
58 return timeToNormalProcess;
niklase@google.com470e71d2011-07-07 08:21:25 +000059}
60
61// Process any pending tasks such as timeouts
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000062int32_t VideoCaptureImpl::Process()
niklase@google.com470e71d2011-07-07 08:21:25 +000063{
mflodman@webrtc.org7845d072012-03-08 08:09:17 +000064 CriticalSectionScoped cs(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +000065
66 const TickTime now = TickTime::Now();
67 _lastProcessTime = TickTime::Now();
68
69 // Handle No picture alarm
70
71 if (_lastProcessFrameCount.Ticks() == _incomingFrameTimes[0].Ticks() &&
72 _captureAlarm != Raised)
73 {
74 if (_noPictureAlarmCallBack && _captureCallBack)
75 {
76 _captureAlarm = Raised;
77 _captureCallBack->OnNoPictureAlarm(_id, _captureAlarm);
78 }
79 }
80 else if (_lastProcessFrameCount.Ticks() != _incomingFrameTimes[0].Ticks() &&
81 _captureAlarm != Cleared)
82 {
83 if (_noPictureAlarmCallBack && _captureCallBack)
84 {
85 _captureAlarm = Cleared;
86 _captureCallBack->OnNoPictureAlarm(_id, _captureAlarm);
87
88 }
89 }
90
91 // Handle frame rate callback
92 if ((now - _lastFrameRateCallbackTime).Milliseconds()
93 > kFrameRateCallbackInterval)
94 {
95 if (_frameRateCallBack && _captureCallBack)
96 {
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +000097 const uint32_t frameRate = CalculateFrameRate(now);
niklase@google.com470e71d2011-07-07 08:21:25 +000098 _captureCallBack->OnCaptureFrameRate(_id, frameRate);
99 }
100 _lastFrameRateCallbackTime = now; // Can be set by EnableFrameRateCallback
101
102 }
103
104 _lastProcessFrameCount = _incomingFrameTimes[0];
105
niklase@google.com470e71d2011-07-07 08:21:25 +0000106 return 0;
107}
108
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000109VideoCaptureImpl::VideoCaptureImpl(const int32_t id)
pbos@webrtc.org504af452013-07-02 10:15:43 +0000110 : _id(id),
111 _deviceUniqueId(NULL),
112 _apiCs(*CriticalSectionWrapper::CreateCriticalSection()),
113 _captureDelay(0),
114 _requestedCapability(),
niklase@google.com470e71d2011-07-07 08:21:25 +0000115 _callBackCs(*CriticalSectionWrapper::CreateCriticalSection()),
116 _lastProcessTime(TickTime::Now()),
pbos@webrtc.org504af452013-07-02 10:15:43 +0000117 _lastFrameRateCallbackTime(TickTime::Now()),
118 _frameRateCallBack(false),
119 _noPictureAlarmCallBack(false),
120 _captureAlarm(Cleared),
121 _setCaptureDelay(0),
122 _dataCallBack(NULL),
123 _captureCallBack(NULL),
124 _lastProcessFrameCount(TickTime::Now()),
125 _rotateFrame(kRotateNone),
126 last_capture_time_(TickTime::MillisecondTimestamp()),
127 delta_ntp_internal_ms_(
128 Clock::GetRealTimeClock()->CurrentNtpInMilliseconds() -
129 TickTime::MillisecondTimestamp()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000130 _requestedCapability.width = kDefaultWidth;
131 _requestedCapability.height = kDefaultHeight;
132 _requestedCapability.maxFPS = 30;
133 _requestedCapability.rawType = kVideoI420;
134 _requestedCapability.codecType = kVideoCodecUnknown;
135 memset(_incomingFrameTimes, 0, sizeof(_incomingFrameTimes));
136}
137
138VideoCaptureImpl::~VideoCaptureImpl()
139{
140 DeRegisterCaptureDataCallback();
141 DeRegisterCaptureCallback();
142 delete &_callBackCs;
143 delete &_apiCs;
144
145 if (_deviceUniqueId)
146 delete[] _deviceUniqueId;
147}
148
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000149int32_t VideoCaptureImpl::RegisterCaptureDataCallback(
niklase@google.com470e71d2011-07-07 08:21:25 +0000150 VideoCaptureDataCallback& dataCallBack)
151{
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000152 CriticalSectionScoped cs(&_apiCs);
153 CriticalSectionScoped cs2(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000154 _dataCallBack = &dataCallBack;
155
156 return 0;
157}
158
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000159int32_t VideoCaptureImpl::DeRegisterCaptureDataCallback()
niklase@google.com470e71d2011-07-07 08:21:25 +0000160{
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000161 CriticalSectionScoped cs(&_apiCs);
162 CriticalSectionScoped cs2(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000163 _dataCallBack = NULL;
164 return 0;
165}
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000166int32_t VideoCaptureImpl::RegisterCaptureCallback(VideoCaptureFeedBack& callBack)
niklase@google.com470e71d2011-07-07 08:21:25 +0000167{
niklase@google.com470e71d2011-07-07 08:21:25 +0000168
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000169 CriticalSectionScoped cs(&_apiCs);
170 CriticalSectionScoped cs2(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000171 _captureCallBack = &callBack;
172 return 0;
173}
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000174int32_t VideoCaptureImpl::DeRegisterCaptureCallback()
niklase@google.com470e71d2011-07-07 08:21:25 +0000175{
niklase@google.com470e71d2011-07-07 08:21:25 +0000176
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000177 CriticalSectionScoped cs(&_apiCs);
178 CriticalSectionScoped cs2(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000179 _captureCallBack = NULL;
180 return 0;
181
182}
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000183int32_t VideoCaptureImpl::SetCaptureDelay(int32_t delayMS)
niklase@google.com470e71d2011-07-07 08:21:25 +0000184{
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000185 CriticalSectionScoped cs(&_apiCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000186 _captureDelay = delayMS;
187 return 0;
188}
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000189int32_t VideoCaptureImpl::CaptureDelay()
niklase@google.com470e71d2011-07-07 08:21:25 +0000190{
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000191 CriticalSectionScoped cs(&_apiCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000192 return _setCaptureDelay;
193}
wu@webrtc.orgf10ea312011-10-14 17:16:04 +0000194
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000195int32_t VideoCaptureImpl::DeliverCapturedFrame(I420VideoFrame& captureFrame,
196 int64_t capture_time) {
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000197 UpdateFrameCount(); // frame count used for local frame rate callback.
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000198
199 const bool callOnCaptureDelayChanged = _setCaptureDelay != _captureDelay;
200 // Capture delay changed
201 if (_setCaptureDelay != _captureDelay) {
202 _setCaptureDelay = _captureDelay;
203 }
204
205 // Set the capture time
206 if (capture_time != 0) {
pbos@webrtc.org504af452013-07-02 10:15:43 +0000207 int64_t internal_capture_time = TickTime::MillisecondTimestamp();
208 int64_t ntp_time_ms = internal_capture_time + delta_ntp_internal_ms_;
209 int64_t time_since_capture = ntp_time_ms - capture_time;
210 internal_capture_time -= time_since_capture;
211 captureFrame.set_render_time_ms(internal_capture_time);
212 } else {
213 captureFrame.set_render_time_ms(TickTime::MillisecondTimestamp());
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000214 }
215
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000216 TRACE_EVENT1("webrtc", "VC::DeliverCapturedFrame",
217 "capture_time", capture_time);
218
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000219 if (captureFrame.render_time_ms() == last_capture_time_) {
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000220 // We don't allow the same capture time for two frames, drop this one.
221 return -1;
222 }
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000223 last_capture_time_ = captureFrame.render_time_ms();
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000224
225 if (_dataCallBack) {
226 if (callOnCaptureDelayChanged) {
227 _dataCallBack->OnCaptureDelayChanged(_id, _captureDelay);
228 }
mikhal@webrtc.orge83d3112012-10-29 15:59:40 +0000229 _dataCallBack->OnIncomingCapturedFrame(_id, captureFrame);
mikhal@webrtc.org80f14d22012-10-11 15:03:53 +0000230 }
231
232 return 0;
233}
234
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000235int32_t VideoCaptureImpl::IncomingFrame(
236 uint8_t* videoFrame,
237 int32_t videoFrameLength,
mflodman@webrtc.org4f9e44f2012-02-23 09:00:26 +0000238 const VideoCaptureCapability& frameInfo,
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000239 int64_t captureTime/*=0*/)
niklase@google.com470e71d2011-07-07 08:21:25 +0000240{
241 WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceVideoCapture, _id,
242 "IncomingFrame width %d, height %d", (int) frameInfo.width,
243 (int) frameInfo.height);
244
245 TickTime startProcessTime = TickTime::Now();
246
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000247 CriticalSectionScoped cs(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000248
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000249 const int32_t width = frameInfo.width;
250 const int32_t height = frameInfo.height;
niklase@google.com470e71d2011-07-07 08:21:25 +0000251
hclam@chromium.org806dc3b2013-04-09 19:54:10 +0000252 TRACE_EVENT1("webrtc", "VC::IncomingFrame", "capture_time", captureTime);
253
mflodman@webrtc.org4f9e44f2012-02-23 09:00:26 +0000254 if (frameInfo.codecType == kVideoCodecUnknown)
niklase@google.com470e71d2011-07-07 08:21:25 +0000255 {
mflodman@webrtc.org4f9e44f2012-02-23 09:00:26 +0000256 // Not encoded, convert to I420.
mikhal@webrtc.orge39de162011-12-27 23:45:30 +0000257 const VideoType commonVideoType =
mflodman@webrtc.org4f9e44f2012-02-23 09:00:26 +0000258 RawVideoTypeToCommonVideoVideoType(frameInfo.rawType);
259
260 if (frameInfo.rawType != kVideoMJPEG &&
elham@webrtc.org5f49dba2012-04-23 21:24:02 +0000261 CalcBufferSize(commonVideoType, width,
262 abs(height)) != videoFrameLength)
niklase@google.com470e71d2011-07-07 08:21:25 +0000263 {
264 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id,
mflodman@webrtc.org4f9e44f2012-02-23 09:00:26 +0000265 "Wrong incoming frame length.");
niklase@google.com470e71d2011-07-07 08:21:25 +0000266 return -1;
267 }
268
mikhal@webrtc.org4c4d01d2012-11-21 22:18:32 +0000269 int stride_y = width;
270 int stride_uv = (width + 1) / 2;
mikhal@webrtc.org0f34fd72012-11-19 21:15:35 +0000271 int target_width = width;
272 int target_height = height;
273 // Rotating resolution when for 90/270 degree rotations.
274 if (_rotateFrame == kRotate90 || _rotateFrame == kRotate270) {
275 target_width = abs(height);
276 target_height = width;
277 }
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).
mikhal@webrtc.org0f34fd72012-11-19 21:15:35 +0000283 int ret = _captureFrame.CreateEmptyFrame(target_width,
284 abs(target_height),
mikhal@webrtc.org91a03402012-10-30 19:19:32 +0000285 stride_y,
286 stride_uv, stride_uv);
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000287 if (ret < 0)
288 {
289 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id,
290 "Failed to allocate I420 frame.");
291 return -1;
292 }
mikhal@webrtc.orga58888d2012-01-04 19:23:24 +0000293 const int conversionResult = ConvertToI420(commonVideoType,
294 videoFrame,
295 0, 0, // No cropping
296 width, height,
mflodman@webrtc.orgf3811192012-02-27 08:10:17 +0000297 videoFrameLength,
mikhal@webrtc.orga58888d2012-01-04 19:23:24 +0000298 _rotateFrame,
mikhal@webrtc.org2f4ff892012-09-24 21:09:54 +0000299 &_captureFrame);
mikhal@webrtc.org2ab104e2011-12-09 02:46:22 +0000300 if (conversionResult < 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000301 {
302 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id,
303 "Failed to convert capture frame from type %d to I420",
304 frameInfo.rawType);
305 return -1;
306 }
mikhal@webrtc.orge83d3112012-10-29 15:59:40 +0000307 DeliverCapturedFrame(_captureFrame, captureTime);
niklase@google.com470e71d2011-07-07 08:21:25 +0000308 }
309 else // Encoded format
310 {
mflodman@webrtc.org3ba883f2013-06-07 13:57:57 +0000311 assert(false);
312 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000313 }
314
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000315 const uint32_t processTime =
316 (uint32_t)(TickTime::Now() - startProcessTime).Milliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000317 if (processTime > 10) // If the process time is too long MJPG will not work well.
318 {
319 WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideoCapture, _id,
320 "Too long processing time of Incoming frame: %ums",
321 (unsigned int) processTime);
322 }
323
324 return 0;
wu@webrtc.orgf10ea312011-10-14 17:16:04 +0000325}
niklase@google.com470e71d2011-07-07 08:21:25 +0000326
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000327int32_t VideoCaptureImpl::IncomingFrameI420(
328 const VideoFrameI420& video_frame, int64_t captureTime) {
wu@webrtc.orgf10ea312011-10-14 17:16:04 +0000329
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000330 CriticalSectionScoped cs(&_callBackCs);
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000331 int size_y = video_frame.height * video_frame.y_pitch;
mikhal@webrtc.org701567a2012-11-09 18:45:12 +0000332 int size_u = video_frame.u_pitch * ((video_frame.height + 1) / 2);
333 int size_v = video_frame.v_pitch * ((video_frame.height + 1) / 2);
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000334 // TODO(mikhal): Can we use Swap here? This will do a memcpy.
335 int ret = _captureFrame.CreateFrame(size_y, video_frame.y_plane,
336 size_u, video_frame.u_plane,
337 size_v, video_frame.v_plane,
338 video_frame.width, video_frame.height,
339 video_frame.y_pitch, video_frame.u_pitch,
340 video_frame.v_pitch);
341 if (ret < 0) {
wu@webrtc.orgf10ea312011-10-14 17:16:04 +0000342 WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id,
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +0000343 "Failed to create I420VideoFrame");
wu@webrtc.orgf10ea312011-10-14 17:16:04 +0000344 return -1;
345 }
346
mikhal@webrtc.orge83d3112012-10-29 15:59:40 +0000347 DeliverCapturedFrame(_captureFrame, captureTime);
wu@webrtc.orgf10ea312011-10-14 17:16:04 +0000348
349 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000350}
351
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000352int32_t VideoCaptureImpl::SetCaptureRotation(VideoCaptureRotation rotation) {
mikhal@webrtc.org0f34fd72012-11-19 21:15:35 +0000353 CriticalSectionScoped cs(&_apiCs);
354 CriticalSectionScoped cs2(&_callBackCs);
355 switch (rotation){
356 case kCameraRotate0:
357 _rotateFrame = kRotateNone;
358 break;
359 case kCameraRotate90:
360 _rotateFrame = kRotate90;
361 break;
362 case kCameraRotate180:
363 _rotateFrame = kRotate180;
364 break;
365 case kCameraRotate270:
366 _rotateFrame = kRotate270;
367 break;
368 }
369 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000370}
371
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000372int32_t VideoCaptureImpl::EnableFrameRateCallback(const bool enable)
niklase@google.com470e71d2011-07-07 08:21:25 +0000373{
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000374 CriticalSectionScoped cs(&_apiCs);
375 CriticalSectionScoped cs2(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000376 _frameRateCallBack = enable;
377 if (enable)
378 {
379 _lastFrameRateCallbackTime = TickTime::Now();
380 }
381 return 0;
382}
383
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000384int32_t VideoCaptureImpl::EnableNoPictureAlarm(const bool enable)
niklase@google.com470e71d2011-07-07 08:21:25 +0000385{
mflodman@webrtc.org7845d072012-03-08 08:09:17 +0000386 CriticalSectionScoped cs(&_apiCs);
387 CriticalSectionScoped cs2(&_callBackCs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000388 _noPictureAlarmCallBack = enable;
389 return 0;
390}
391
392void VideoCaptureImpl::UpdateFrameCount()
393{
394 if (_incomingFrameTimes[0].MicrosecondTimestamp() == 0)
395 {
396 // first no shift
397 }
398 else
399 {
400 // shift
401 for (int i = (kFrameRateCountHistorySize - 2); i >= 0; i--)
402 {
403 _incomingFrameTimes[i + 1] = _incomingFrameTimes[i];
404 }
405 }
406 _incomingFrameTimes[0] = TickTime::Now();
407}
408
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000409uint32_t VideoCaptureImpl::CalculateFrameRate(const TickTime& now)
niklase@google.com470e71d2011-07-07 08:21:25 +0000410{
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000411 int32_t num = 0;
412 int32_t nrOfFrames = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000413 for (num = 1; num < (kFrameRateCountHistorySize - 1); num++)
414 {
415 if (_incomingFrameTimes[num].Ticks() <= 0
416 || (now - _incomingFrameTimes[num]).Milliseconds() > kFrameRateHistoryWindowMs) // don't use data older than 2sec
417 {
418 break;
419 }
420 else
421 {
422 nrOfFrames++;
423 }
424 }
425 if (num > 1)
426 {
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000427 int64_t diff = (now - _incomingFrameTimes[num - 1]).Milliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000428 if (diff > 0)
429 {
pbos@webrtc.orgdfc5bb92013-04-10 08:23:13 +0000430 return uint32_t((nrOfFrames * 1000.0f / diff) + 0.5f);
niklase@google.com470e71d2011-07-07 08:21:25 +0000431 }
432 }
433
434 return nrOfFrames;
435}
perkj@webrtc.org0cc68dc2011-09-12 08:53:36 +0000436} // namespace videocapturemodule
niklase@google.com470e71d2011-07-07 08:21:25 +0000437} // namespace webrtc