niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
mallinath@webrtc.org | 12984f0 | 2012-02-16 18:18:21 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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.org | e39de16 | 2011-12-27 23:45:30 +0000 | [diff] [blame] | 12 | |
andrew@webrtc.org | c1354bd | 2012-07-27 18:21:16 +0000 | [diff] [blame] | 13 | #include "common_video/libyuv/include/webrtc_libyuv.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | #include "critical_section_wrapper.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | #include "module_common_types.h" |
perkj@webrtc.org | 0cc68dc | 2011-09-12 08:53:36 +0000 | [diff] [blame] | 16 | #include "ref_count.h" |
mikhal@webrtc.org | e39de16 | 2011-12-27 23:45:30 +0000 | [diff] [blame] | 17 | #include "tick_util.h" |
| 18 | #include "trace.h" |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 +0000 | [diff] [blame^] | 19 | #include "trace_event.h" |
mikhal@webrtc.org | e39de16 | 2011-12-27 23:45:30 +0000 | [diff] [blame] | 20 | #include "video_capture_config.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | |
elham@webrtc.org | 5f49dba | 2012-04-23 21:24:02 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | namespace webrtc |
| 25 | { |
perkj@webrtc.org | 0cc68dc | 2011-09-12 08:53:36 +0000 | [diff] [blame] | 26 | namespace videocapturemodule |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | { |
perkj@webrtc.org | 0cc68dc | 2011-09-12 08:53:36 +0000 | [diff] [blame] | 28 | VideoCaptureModule* VideoCaptureImpl::Create( |
| 29 | const WebRtc_Word32 id, |
| 30 | VideoCaptureExternal*& externalCapture) |
| 31 | { |
| 32 | RefCountImpl<VideoCaptureImpl>* implementation = |
| 33 | new RefCountImpl<VideoCaptureImpl>(id); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | externalCapture = implementation; |
| 35 | return implementation; |
| 36 | } |
| 37 | |
leozwang@webrtc.org | 1745e93 | 2012-03-01 16:30:40 +0000 | [diff] [blame] | 38 | const char* VideoCaptureImpl::CurrentDeviceName() const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | { |
| 40 | return _deviceUniqueId; |
| 41 | } |
| 42 | |
| 43 | WebRtc_Word32 VideoCaptureImpl::ChangeUniqueId(const WebRtc_Word32 id) |
| 44 | { |
| 45 | _id = id; |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | // returns the number of milliseconds until the module want a worker thread to call Process |
| 50 | WebRtc_Word32 VideoCaptureImpl::TimeUntilNextProcess() |
| 51 | { |
perkj@webrtc.org | c2fde80 | 2012-08-08 14:01:09 +0000 | [diff] [blame] | 52 | CriticalSectionScoped cs(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 53 | |
| 54 | WebRtc_Word32 timeToNormalProcess = kProcessInterval |
| 55 | - (WebRtc_Word32)((TickTime::Now() - _lastProcessTime).Milliseconds()); |
mikhal@webrtc.org | 9a5b904 | 2012-10-19 16:44:35 +0000 | [diff] [blame] | 56 | |
| 57 | return timeToNormalProcess; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // Process any pending tasks such as timeouts |
| 61 | WebRtc_Word32 VideoCaptureImpl::Process() |
| 62 | { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 63 | CriticalSectionScoped cs(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 64 | |
| 65 | const TickTime now = TickTime::Now(); |
| 66 | _lastProcessTime = TickTime::Now(); |
| 67 | |
| 68 | // Handle No picture alarm |
| 69 | |
| 70 | if (_lastProcessFrameCount.Ticks() == _incomingFrameTimes[0].Ticks() && |
| 71 | _captureAlarm != Raised) |
| 72 | { |
| 73 | if (_noPictureAlarmCallBack && _captureCallBack) |
| 74 | { |
| 75 | _captureAlarm = Raised; |
| 76 | _captureCallBack->OnNoPictureAlarm(_id, _captureAlarm); |
| 77 | } |
| 78 | } |
| 79 | else if (_lastProcessFrameCount.Ticks() != _incomingFrameTimes[0].Ticks() && |
| 80 | _captureAlarm != Cleared) |
| 81 | { |
| 82 | if (_noPictureAlarmCallBack && _captureCallBack) |
| 83 | { |
| 84 | _captureAlarm = Cleared; |
| 85 | _captureCallBack->OnNoPictureAlarm(_id, _captureAlarm); |
| 86 | |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // Handle frame rate callback |
| 91 | if ((now - _lastFrameRateCallbackTime).Milliseconds() |
| 92 | > kFrameRateCallbackInterval) |
| 93 | { |
| 94 | if (_frameRateCallBack && _captureCallBack) |
| 95 | { |
| 96 | const WebRtc_UWord32 frameRate = CalculateFrameRate(now); |
| 97 | _captureCallBack->OnCaptureFrameRate(_id, frameRate); |
| 98 | } |
| 99 | _lastFrameRateCallbackTime = now; // Can be set by EnableFrameRateCallback |
| 100 | |
| 101 | } |
| 102 | |
| 103 | _lastProcessFrameCount = _incomingFrameTimes[0]; |
| 104 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | VideoCaptureImpl::VideoCaptureImpl(const WebRtc_Word32 id) |
| 109 | : _id(id), _deviceUniqueId(NULL), _apiCs(*CriticalSectionWrapper::CreateCriticalSection()), |
| 110 | _captureDelay(0), _requestedCapability(), |
| 111 | _callBackCs(*CriticalSectionWrapper::CreateCriticalSection()), |
| 112 | _lastProcessTime(TickTime::Now()), |
| 113 | _lastFrameRateCallbackTime(TickTime::Now()), _frameRateCallBack(false), |
| 114 | _noPictureAlarmCallBack(false), _captureAlarm(Cleared), _setCaptureDelay(0), |
mallinath@webrtc.org | 12984f0 | 2012-02-16 18:18:21 +0000 | [diff] [blame] | 115 | _dataCallBack(NULL), _captureCallBack(NULL), |
mflodman@webrtc.org | 29d75b3 | 2011-11-01 17:10:49 +0000 | [diff] [blame] | 116 | _lastProcessFrameCount(TickTime::Now()), _rotateFrame(kRotateNone), |
| 117 | last_capture_time_(TickTime::MillisecondTimestamp()) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 118 | |
| 119 | { |
| 120 | _requestedCapability.width = kDefaultWidth; |
| 121 | _requestedCapability.height = kDefaultHeight; |
| 122 | _requestedCapability.maxFPS = 30; |
| 123 | _requestedCapability.rawType = kVideoI420; |
| 124 | _requestedCapability.codecType = kVideoCodecUnknown; |
| 125 | memset(_incomingFrameTimes, 0, sizeof(_incomingFrameTimes)); |
| 126 | } |
| 127 | |
| 128 | VideoCaptureImpl::~VideoCaptureImpl() |
| 129 | { |
| 130 | DeRegisterCaptureDataCallback(); |
| 131 | DeRegisterCaptureCallback(); |
| 132 | delete &_callBackCs; |
| 133 | delete &_apiCs; |
| 134 | |
| 135 | if (_deviceUniqueId) |
| 136 | delete[] _deviceUniqueId; |
| 137 | } |
| 138 | |
| 139 | WebRtc_Word32 VideoCaptureImpl::RegisterCaptureDataCallback( |
| 140 | VideoCaptureDataCallback& dataCallBack) |
| 141 | { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 142 | CriticalSectionScoped cs(&_apiCs); |
| 143 | CriticalSectionScoped cs2(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | _dataCallBack = &dataCallBack; |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | WebRtc_Word32 VideoCaptureImpl::DeRegisterCaptureDataCallback() |
| 150 | { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 151 | CriticalSectionScoped cs(&_apiCs); |
| 152 | CriticalSectionScoped cs2(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 153 | _dataCallBack = NULL; |
| 154 | return 0; |
| 155 | } |
| 156 | WebRtc_Word32 VideoCaptureImpl::RegisterCaptureCallback(VideoCaptureFeedBack& callBack) |
| 157 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 158 | |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 159 | CriticalSectionScoped cs(&_apiCs); |
| 160 | CriticalSectionScoped cs2(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 161 | _captureCallBack = &callBack; |
| 162 | return 0; |
| 163 | } |
| 164 | WebRtc_Word32 VideoCaptureImpl::DeRegisterCaptureCallback() |
| 165 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 166 | |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 167 | CriticalSectionScoped cs(&_apiCs); |
| 168 | CriticalSectionScoped cs2(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 169 | _captureCallBack = NULL; |
| 170 | return 0; |
| 171 | |
| 172 | } |
| 173 | WebRtc_Word32 VideoCaptureImpl::SetCaptureDelay(WebRtc_Word32 delayMS) |
| 174 | { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 175 | CriticalSectionScoped cs(&_apiCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 176 | _captureDelay = delayMS; |
| 177 | return 0; |
| 178 | } |
| 179 | WebRtc_Word32 VideoCaptureImpl::CaptureDelay() |
| 180 | { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 181 | CriticalSectionScoped cs(&_apiCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 182 | return _setCaptureDelay; |
| 183 | } |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 184 | |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 185 | WebRtc_Word32 VideoCaptureImpl::DeliverCapturedFrame(I420VideoFrame& |
mikhal@webrtc.org | e83d311 | 2012-10-29 15:59:40 +0000 | [diff] [blame] | 186 | captureFrame, WebRtc_Word64 capture_time) { |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 187 | UpdateFrameCount(); // frame count used for local frame rate callback. |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 188 | |
| 189 | const bool callOnCaptureDelayChanged = _setCaptureDelay != _captureDelay; |
| 190 | // Capture delay changed |
| 191 | if (_setCaptureDelay != _captureDelay) { |
| 192 | _setCaptureDelay = _captureDelay; |
| 193 | } |
| 194 | |
| 195 | // Set the capture time |
| 196 | if (capture_time != 0) { |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 197 | captureFrame.set_render_time_ms(capture_time); |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 198 | } |
| 199 | else { |
stefan@webrtc.org | 7da3459 | 2013-04-09 14:56:29 +0000 | [diff] [blame] | 200 | captureFrame.set_render_time_ms(TickTime::MillisecondTimestamp()); |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 201 | } |
| 202 | |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 +0000 | [diff] [blame^] | 203 | TRACE_EVENT1("webrtc", "VC::DeliverCapturedFrame", |
| 204 | "capture_time", capture_time); |
| 205 | |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 206 | if (captureFrame.render_time_ms() == last_capture_time_) { |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 207 | // We don't allow the same capture time for two frames, drop this one. |
| 208 | return -1; |
| 209 | } |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 210 | last_capture_time_ = captureFrame.render_time_ms(); |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 211 | |
| 212 | if (_dataCallBack) { |
| 213 | if (callOnCaptureDelayChanged) { |
| 214 | _dataCallBack->OnCaptureDelayChanged(_id, _captureDelay); |
| 215 | } |
mikhal@webrtc.org | e83d311 | 2012-10-29 15:59:40 +0000 | [diff] [blame] | 216 | _dataCallBack->OnIncomingCapturedFrame(_id, captureFrame); |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | WebRtc_Word32 VideoCaptureImpl::DeliverEncodedCapturedFrame( |
mikhal@webrtc.org | ac993fe | 2012-11-07 17:18:04 +0000 | [diff] [blame] | 223 | VideoFrame& captureFrame, WebRtc_Word64 capture_time, |
| 224 | VideoCodecType codecType) { |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 225 | UpdateFrameCount(); // frame count used for local frame rate callback. |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 226 | |
| 227 | const bool callOnCaptureDelayChanged = _setCaptureDelay != _captureDelay; |
| 228 | // Capture delay changed |
| 229 | if (_setCaptureDelay != _captureDelay) { |
| 230 | _setCaptureDelay = _captureDelay; |
| 231 | } |
| 232 | |
| 233 | // Set the capture time |
| 234 | if (capture_time != 0) { |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 235 | captureFrame.SetRenderTime(capture_time); |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 236 | } |
| 237 | else { |
| 238 | captureFrame.SetRenderTime(TickTime::MillisecondTimestamp()); |
| 239 | } |
| 240 | |
mflodman@webrtc.org | 29d75b3 | 2011-11-01 17:10:49 +0000 | [diff] [blame] | 241 | if (captureFrame.RenderTimeMs() == last_capture_time_) { |
| 242 | // We don't allow the same capture time for two frames, drop this one. |
| 243 | return -1; |
| 244 | } |
| 245 | last_capture_time_ = captureFrame.RenderTimeMs(); |
| 246 | |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 247 | if (_dataCallBack) { |
| 248 | if (callOnCaptureDelayChanged) { |
| 249 | _dataCallBack->OnCaptureDelayChanged(_id, _captureDelay); |
| 250 | } |
mikhal@webrtc.org | ac993fe | 2012-11-07 17:18:04 +0000 | [diff] [blame] | 251 | _dataCallBack->OnIncomingCapturedEncodedFrame(_id, captureFrame, codecType); |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
mflodman@webrtc.org | 4f9e44f | 2012-02-23 09:00:26 +0000 | [diff] [blame] | 257 | WebRtc_Word32 VideoCaptureImpl::IncomingFrame( |
| 258 | WebRtc_UWord8* videoFrame, |
| 259 | WebRtc_Word32 videoFrameLength, |
| 260 | const VideoCaptureCapability& frameInfo, |
| 261 | WebRtc_Word64 captureTime/*=0*/) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 262 | { |
| 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.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 269 | CriticalSectionScoped cs(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 270 | |
| 271 | const WebRtc_Word32 width = frameInfo.width; |
| 272 | const WebRtc_Word32 height = frameInfo.height; |
| 273 | |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 +0000 | [diff] [blame^] | 274 | TRACE_EVENT1("webrtc", "VC::IncomingFrame", "capture_time", captureTime); |
| 275 | |
mflodman@webrtc.org | 4f9e44f | 2012-02-23 09:00:26 +0000 | [diff] [blame] | 276 | if (frameInfo.codecType == kVideoCodecUnknown) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 277 | { |
mflodman@webrtc.org | 4f9e44f | 2012-02-23 09:00:26 +0000 | [diff] [blame] | 278 | // Not encoded, convert to I420. |
mikhal@webrtc.org | e39de16 | 2011-12-27 23:45:30 +0000 | [diff] [blame] | 279 | const VideoType commonVideoType = |
mflodman@webrtc.org | 4f9e44f | 2012-02-23 09:00:26 +0000 | [diff] [blame] | 280 | RawVideoTypeToCommonVideoVideoType(frameInfo.rawType); |
| 281 | |
| 282 | if (frameInfo.rawType != kVideoMJPEG && |
elham@webrtc.org | 5f49dba | 2012-04-23 21:24:02 +0000 | [diff] [blame] | 283 | CalcBufferSize(commonVideoType, width, |
| 284 | abs(height)) != videoFrameLength) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 285 | { |
| 286 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id, |
mflodman@webrtc.org | 4f9e44f | 2012-02-23 09:00:26 +0000 | [diff] [blame] | 287 | "Wrong incoming frame length."); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 288 | return -1; |
| 289 | } |
| 290 | |
mikhal@webrtc.org | 4c4d01d | 2012-11-21 22:18:32 +0000 | [diff] [blame] | 291 | int stride_y = width; |
| 292 | int stride_uv = (width + 1) / 2; |
mikhal@webrtc.org | 0f34fd7 | 2012-11-19 21:15:35 +0000 | [diff] [blame] | 293 | 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.org | 4c4d01d | 2012-11-21 22:18:32 +0000 | [diff] [blame] | 300 | // TODO(mikhal): Update correct aligned stride values. |
| 301 | //Calc16ByteAlignedStride(target_width, &stride_y, &stride_uv); |
mikhal@webrtc.org | 2f4ff89 | 2012-09-24 21:09:54 +0000 | [diff] [blame] | 302 | // 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). |
mikhal@webrtc.org | 0f34fd7 | 2012-11-19 21:15:35 +0000 | [diff] [blame] | 305 | int ret = _captureFrame.CreateEmptyFrame(target_width, |
| 306 | abs(target_height), |
mikhal@webrtc.org | 91a0340 | 2012-10-30 19:19:32 +0000 | [diff] [blame] | 307 | stride_y, |
| 308 | stride_uv, stride_uv); |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 309 | if (ret < 0) |
| 310 | { |
| 311 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id, |
| 312 | "Failed to allocate I420 frame."); |
| 313 | return -1; |
| 314 | } |
mikhal@webrtc.org | a58888d | 2012-01-04 19:23:24 +0000 | [diff] [blame] | 315 | const int conversionResult = ConvertToI420(commonVideoType, |
| 316 | videoFrame, |
| 317 | 0, 0, // No cropping |
| 318 | width, height, |
mflodman@webrtc.org | f381119 | 2012-02-27 08:10:17 +0000 | [diff] [blame] | 319 | videoFrameLength, |
mikhal@webrtc.org | a58888d | 2012-01-04 19:23:24 +0000 | [diff] [blame] | 320 | _rotateFrame, |
mikhal@webrtc.org | 2f4ff89 | 2012-09-24 21:09:54 +0000 | [diff] [blame] | 321 | &_captureFrame); |
mikhal@webrtc.org | 2ab104e | 2011-12-09 02:46:22 +0000 | [diff] [blame] | 322 | if (conversionResult < 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 323 | { |
| 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 | } |
mikhal@webrtc.org | e83d311 | 2012-10-29 15:59:40 +0000 | [diff] [blame] | 329 | DeliverCapturedFrame(_captureFrame, captureTime); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 330 | } |
| 331 | else // Encoded format |
| 332 | { |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 333 | if (_capture_encoded_frame.CopyFrame(videoFrameLength, videoFrame) != 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 334 | { |
| 335 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id, |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 336 | "Failed to copy captured frame of length %d", |
| 337 | static_cast<int>(videoFrameLength)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 338 | } |
mikhal@webrtc.org | ac993fe | 2012-11-07 17:18:04 +0000 | [diff] [blame] | 339 | DeliverEncodedCapturedFrame(_capture_encoded_frame, captureTime, |
| 340 | frameInfo.codecType); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 341 | } |
| 342 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 343 | const WebRtc_UWord32 processTime = |
| 344 | (WebRtc_UWord32)(TickTime::Now() - startProcessTime).Milliseconds(); |
| 345 | if (processTime > 10) // If the process time is too long MJPG will not work well. |
| 346 | { |
| 347 | WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideoCapture, _id, |
| 348 | "Too long processing time of Incoming frame: %ums", |
| 349 | (unsigned int) processTime); |
| 350 | } |
| 351 | |
| 352 | return 0; |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 353 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 354 | |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 355 | WebRtc_Word32 VideoCaptureImpl::IncomingFrameI420( |
| 356 | const VideoFrameI420& video_frame, WebRtc_Word64 captureTime) { |
| 357 | |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 358 | CriticalSectionScoped cs(&_callBackCs); |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 359 | int size_y = video_frame.height * video_frame.y_pitch; |
mikhal@webrtc.org | 701567a | 2012-11-09 18:45:12 +0000 | [diff] [blame] | 360 | int size_u = video_frame.u_pitch * ((video_frame.height + 1) / 2); |
| 361 | int size_v = video_frame.v_pitch * ((video_frame.height + 1) / 2); |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 362 | // TODO(mikhal): Can we use Swap here? This will do a memcpy. |
| 363 | int ret = _captureFrame.CreateFrame(size_y, video_frame.y_plane, |
| 364 | size_u, video_frame.u_plane, |
| 365 | size_v, video_frame.v_plane, |
| 366 | video_frame.width, video_frame.height, |
| 367 | video_frame.y_pitch, video_frame.u_pitch, |
| 368 | video_frame.v_pitch); |
| 369 | if (ret < 0) { |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 370 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id, |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 371 | "Failed to create I420VideoFrame"); |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 372 | return -1; |
| 373 | } |
| 374 | |
mikhal@webrtc.org | e83d311 | 2012-10-29 15:59:40 +0000 | [diff] [blame] | 375 | DeliverCapturedFrame(_captureFrame, captureTime); |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 376 | |
| 377 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 378 | } |
| 379 | |
mikhal@webrtc.org | 0f34fd7 | 2012-11-19 21:15:35 +0000 | [diff] [blame] | 380 | WebRtc_Word32 VideoCaptureImpl::SetCaptureRotation(VideoCaptureRotation |
| 381 | rotation) { |
| 382 | CriticalSectionScoped cs(&_apiCs); |
| 383 | CriticalSectionScoped cs2(&_callBackCs); |
| 384 | switch (rotation){ |
| 385 | case kCameraRotate0: |
| 386 | _rotateFrame = kRotateNone; |
| 387 | break; |
| 388 | case kCameraRotate90: |
| 389 | _rotateFrame = kRotate90; |
| 390 | break; |
| 391 | case kCameraRotate180: |
| 392 | _rotateFrame = kRotate180; |
| 393 | break; |
| 394 | case kCameraRotate270: |
| 395 | _rotateFrame = kRotate270; |
| 396 | break; |
| 397 | } |
| 398 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 399 | } |
| 400 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 401 | WebRtc_Word32 VideoCaptureImpl::EnableFrameRateCallback(const bool enable) |
| 402 | { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 403 | CriticalSectionScoped cs(&_apiCs); |
| 404 | CriticalSectionScoped cs2(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 405 | _frameRateCallBack = enable; |
| 406 | if (enable) |
| 407 | { |
| 408 | _lastFrameRateCallbackTime = TickTime::Now(); |
| 409 | } |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | WebRtc_Word32 VideoCaptureImpl::EnableNoPictureAlarm(const bool enable) |
| 414 | { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 415 | CriticalSectionScoped cs(&_apiCs); |
| 416 | CriticalSectionScoped cs2(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 417 | _noPictureAlarmCallBack = enable; |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | void VideoCaptureImpl::UpdateFrameCount() |
| 422 | { |
| 423 | if (_incomingFrameTimes[0].MicrosecondTimestamp() == 0) |
| 424 | { |
| 425 | // first no shift |
| 426 | } |
| 427 | else |
| 428 | { |
| 429 | // shift |
| 430 | for (int i = (kFrameRateCountHistorySize - 2); i >= 0; i--) |
| 431 | { |
| 432 | _incomingFrameTimes[i + 1] = _incomingFrameTimes[i]; |
| 433 | } |
| 434 | } |
| 435 | _incomingFrameTimes[0] = TickTime::Now(); |
| 436 | } |
| 437 | |
| 438 | WebRtc_UWord32 VideoCaptureImpl::CalculateFrameRate(const TickTime& now) |
| 439 | { |
| 440 | WebRtc_Word32 num = 0; |
| 441 | WebRtc_Word32 nrOfFrames = 0; |
| 442 | for (num = 1; num < (kFrameRateCountHistorySize - 1); num++) |
| 443 | { |
| 444 | if (_incomingFrameTimes[num].Ticks() <= 0 |
| 445 | || (now - _incomingFrameTimes[num]).Milliseconds() > kFrameRateHistoryWindowMs) // don't use data older than 2sec |
| 446 | { |
| 447 | break; |
| 448 | } |
| 449 | else |
| 450 | { |
| 451 | nrOfFrames++; |
| 452 | } |
| 453 | } |
| 454 | if (num > 1) |
| 455 | { |
| 456 | WebRtc_Word64 diff = (now - _incomingFrameTimes[num - 1]).Milliseconds(); |
| 457 | if (diff > 0) |
| 458 | { |
| 459 | return WebRtc_UWord32((nrOfFrames * 1000.0f / diff) + 0.5f); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | return nrOfFrames; |
| 464 | } |
perkj@webrtc.org | 0cc68dc | 2011-09-12 08:53:36 +0000 | [diff] [blame] | 465 | } // namespace videocapturemodule |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 466 | } // namespace webrtc |