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 | |
pbos@webrtc.org | a9b74ad | 2013-07-12 10:03:52 +0000 | [diff] [blame^] | 11 | #include "webrtc/modules/video_capture/video_capture_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
elham@webrtc.org | 5f49dba | 2012-04-23 21:24:02 +0000 | [diff] [blame] | 13 | #include <stdlib.h> |
| 14 | |
pbos@webrtc.org | a9b74ad | 2013-07-12 10:03:52 +0000 | [diff] [blame^] | 15 | #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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | namespace webrtc |
| 26 | { |
perkj@webrtc.org | 0cc68dc | 2011-09-12 08:53:36 +0000 | [diff] [blame] | 27 | namespace videocapturemodule |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | { |
perkj@webrtc.org | 0cc68dc | 2011-09-12 08:53:36 +0000 | [diff] [blame] | 29 | VideoCaptureModule* VideoCaptureImpl::Create( |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 30 | const int32_t id, |
perkj@webrtc.org | 0cc68dc | 2011-09-12 08:53:36 +0000 | [diff] [blame] | 31 | VideoCaptureExternal*& externalCapture) |
| 32 | { |
| 33 | RefCountImpl<VideoCaptureImpl>* implementation = |
| 34 | new RefCountImpl<VideoCaptureImpl>(id); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | externalCapture = implementation; |
| 36 | return implementation; |
| 37 | } |
| 38 | |
leozwang@webrtc.org | 1745e93 | 2012-03-01 16:30:40 +0000 | [diff] [blame] | 39 | const char* VideoCaptureImpl::CurrentDeviceName() const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | { |
| 41 | return _deviceUniqueId; |
| 42 | } |
| 43 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 44 | int32_t VideoCaptureImpl::ChangeUniqueId(const int32_t id) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | { |
| 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.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 51 | int32_t VideoCaptureImpl::TimeUntilNextProcess() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 52 | { |
perkj@webrtc.org | c2fde80 | 2012-08-08 14:01:09 +0000 | [diff] [blame] | 53 | CriticalSectionScoped cs(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 55 | int32_t timeToNormalProcess = kProcessInterval |
| 56 | - (int32_t)((TickTime::Now() - _lastProcessTime).Milliseconds()); |
mikhal@webrtc.org | 9a5b904 | 2012-10-19 16:44:35 +0000 | [diff] [blame] | 57 | |
| 58 | return timeToNormalProcess; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // Process any pending tasks such as timeouts |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 62 | int32_t VideoCaptureImpl::Process() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 63 | { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 64 | CriticalSectionScoped cs(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 65 | |
| 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.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 97 | const uint32_t frameRate = CalculateFrameRate(now); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | _captureCallBack->OnCaptureFrameRate(_id, frameRate); |
| 99 | } |
| 100 | _lastFrameRateCallbackTime = now; // Can be set by EnableFrameRateCallback |
| 101 | |
| 102 | } |
| 103 | |
| 104 | _lastProcessFrameCount = _incomingFrameTimes[0]; |
| 105 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 109 | VideoCaptureImpl::VideoCaptureImpl(const int32_t id) |
pbos@webrtc.org | 504af45 | 2013-07-02 10:15:43 +0000 | [diff] [blame] | 110 | : _id(id), |
| 111 | _deviceUniqueId(NULL), |
| 112 | _apiCs(*CriticalSectionWrapper::CreateCriticalSection()), |
| 113 | _captureDelay(0), |
| 114 | _requestedCapability(), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 115 | _callBackCs(*CriticalSectionWrapper::CreateCriticalSection()), |
| 116 | _lastProcessTime(TickTime::Now()), |
pbos@webrtc.org | 504af45 | 2013-07-02 10:15:43 +0000 | [diff] [blame] | 117 | _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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 130 | _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 | |
| 138 | VideoCaptureImpl::~VideoCaptureImpl() |
| 139 | { |
| 140 | DeRegisterCaptureDataCallback(); |
| 141 | DeRegisterCaptureCallback(); |
| 142 | delete &_callBackCs; |
| 143 | delete &_apiCs; |
| 144 | |
| 145 | if (_deviceUniqueId) |
| 146 | delete[] _deviceUniqueId; |
| 147 | } |
| 148 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 149 | int32_t VideoCaptureImpl::RegisterCaptureDataCallback( |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 150 | VideoCaptureDataCallback& dataCallBack) |
| 151 | { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 152 | CriticalSectionScoped cs(&_apiCs); |
| 153 | CriticalSectionScoped cs2(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 154 | _dataCallBack = &dataCallBack; |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 159 | int32_t VideoCaptureImpl::DeRegisterCaptureDataCallback() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 160 | { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 161 | CriticalSectionScoped cs(&_apiCs); |
| 162 | CriticalSectionScoped cs2(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 163 | _dataCallBack = NULL; |
| 164 | return 0; |
| 165 | } |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 166 | int32_t VideoCaptureImpl::RegisterCaptureCallback(VideoCaptureFeedBack& callBack) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 167 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 168 | |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 169 | CriticalSectionScoped cs(&_apiCs); |
| 170 | CriticalSectionScoped cs2(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 171 | _captureCallBack = &callBack; |
| 172 | return 0; |
| 173 | } |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 174 | int32_t VideoCaptureImpl::DeRegisterCaptureCallback() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 175 | { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 176 | |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 177 | CriticalSectionScoped cs(&_apiCs); |
| 178 | CriticalSectionScoped cs2(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 179 | _captureCallBack = NULL; |
| 180 | return 0; |
| 181 | |
| 182 | } |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 183 | int32_t VideoCaptureImpl::SetCaptureDelay(int32_t delayMS) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 184 | { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 185 | CriticalSectionScoped cs(&_apiCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 186 | _captureDelay = delayMS; |
| 187 | return 0; |
| 188 | } |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 189 | int32_t VideoCaptureImpl::CaptureDelay() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 190 | { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 191 | CriticalSectionScoped cs(&_apiCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 192 | return _setCaptureDelay; |
| 193 | } |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 194 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 195 | int32_t VideoCaptureImpl::DeliverCapturedFrame(I420VideoFrame& captureFrame, |
| 196 | int64_t capture_time) { |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 197 | UpdateFrameCount(); // frame count used for local frame rate callback. |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 198 | |
| 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.org | 504af45 | 2013-07-02 10:15:43 +0000 | [diff] [blame] | 207 | 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.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 214 | } |
| 215 | |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 216 | if (captureFrame.render_time_ms() == last_capture_time_) { |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 217 | // We don't allow the same capture time for two frames, drop this one. |
| 218 | return -1; |
| 219 | } |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 220 | last_capture_time_ = captureFrame.render_time_ms(); |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 221 | |
| 222 | if (_dataCallBack) { |
| 223 | if (callOnCaptureDelayChanged) { |
| 224 | _dataCallBack->OnCaptureDelayChanged(_id, _captureDelay); |
| 225 | } |
mikhal@webrtc.org | e83d311 | 2012-10-29 15:59:40 +0000 | [diff] [blame] | 226 | _dataCallBack->OnIncomingCapturedFrame(_id, captureFrame); |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 232 | int32_t VideoCaptureImpl::IncomingFrame( |
| 233 | uint8_t* videoFrame, |
| 234 | int32_t videoFrameLength, |
mflodman@webrtc.org | 4f9e44f | 2012-02-23 09:00:26 +0000 | [diff] [blame] | 235 | const VideoCaptureCapability& frameInfo, |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 236 | int64_t captureTime/*=0*/) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 237 | { |
| 238 | WEBRTC_TRACE(webrtc::kTraceStream, webrtc::kTraceVideoCapture, _id, |
| 239 | "IncomingFrame width %d, height %d", (int) frameInfo.width, |
| 240 | (int) frameInfo.height); |
| 241 | |
| 242 | TickTime startProcessTime = TickTime::Now(); |
| 243 | |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 244 | CriticalSectionScoped cs(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 245 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 246 | const int32_t width = frameInfo.width; |
| 247 | const int32_t height = frameInfo.height; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 248 | |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 +0000 | [diff] [blame] | 249 | TRACE_EVENT1("webrtc", "VC::IncomingFrame", "capture_time", captureTime); |
| 250 | |
mflodman@webrtc.org | 4f9e44f | 2012-02-23 09:00:26 +0000 | [diff] [blame] | 251 | if (frameInfo.codecType == kVideoCodecUnknown) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 252 | { |
mflodman@webrtc.org | 4f9e44f | 2012-02-23 09:00:26 +0000 | [diff] [blame] | 253 | // Not encoded, convert to I420. |
mikhal@webrtc.org | e39de16 | 2011-12-27 23:45:30 +0000 | [diff] [blame] | 254 | const VideoType commonVideoType = |
mflodman@webrtc.org | 4f9e44f | 2012-02-23 09:00:26 +0000 | [diff] [blame] | 255 | RawVideoTypeToCommonVideoVideoType(frameInfo.rawType); |
| 256 | |
| 257 | if (frameInfo.rawType != kVideoMJPEG && |
elham@webrtc.org | 5f49dba | 2012-04-23 21:24:02 +0000 | [diff] [blame] | 258 | CalcBufferSize(commonVideoType, width, |
| 259 | abs(height)) != videoFrameLength) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 260 | { |
| 261 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id, |
mflodman@webrtc.org | 4f9e44f | 2012-02-23 09:00:26 +0000 | [diff] [blame] | 262 | "Wrong incoming frame length."); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 263 | return -1; |
| 264 | } |
| 265 | |
mikhal@webrtc.org | 4c4d01d | 2012-11-21 22:18:32 +0000 | [diff] [blame] | 266 | int stride_y = width; |
| 267 | int stride_uv = (width + 1) / 2; |
mikhal@webrtc.org | 0f34fd7 | 2012-11-19 21:15:35 +0000 | [diff] [blame] | 268 | int target_width = width; |
| 269 | int target_height = height; |
| 270 | // Rotating resolution when for 90/270 degree rotations. |
| 271 | if (_rotateFrame == kRotate90 || _rotateFrame == kRotate270) { |
| 272 | target_width = abs(height); |
| 273 | target_height = width; |
| 274 | } |
mikhal@webrtc.org | 4c4d01d | 2012-11-21 22:18:32 +0000 | [diff] [blame] | 275 | // TODO(mikhal): Update correct aligned stride values. |
| 276 | //Calc16ByteAlignedStride(target_width, &stride_y, &stride_uv); |
mikhal@webrtc.org | 2f4ff89 | 2012-09-24 21:09:54 +0000 | [diff] [blame] | 277 | // Setting absolute height (in case it was negative). |
| 278 | // In Windows, the image starts bottom left, instead of top left. |
| 279 | // Setting a negative source height, inverts the image (within LibYuv). |
mikhal@webrtc.org | 0f34fd7 | 2012-11-19 21:15:35 +0000 | [diff] [blame] | 280 | int ret = _captureFrame.CreateEmptyFrame(target_width, |
| 281 | abs(target_height), |
mikhal@webrtc.org | 91a0340 | 2012-10-30 19:19:32 +0000 | [diff] [blame] | 282 | stride_y, |
| 283 | stride_uv, stride_uv); |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 284 | if (ret < 0) |
| 285 | { |
| 286 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id, |
| 287 | "Failed to allocate I420 frame."); |
| 288 | return -1; |
| 289 | } |
mikhal@webrtc.org | a58888d | 2012-01-04 19:23:24 +0000 | [diff] [blame] | 290 | const int conversionResult = ConvertToI420(commonVideoType, |
| 291 | videoFrame, |
| 292 | 0, 0, // No cropping |
| 293 | width, height, |
mflodman@webrtc.org | f381119 | 2012-02-27 08:10:17 +0000 | [diff] [blame] | 294 | videoFrameLength, |
mikhal@webrtc.org | a58888d | 2012-01-04 19:23:24 +0000 | [diff] [blame] | 295 | _rotateFrame, |
mikhal@webrtc.org | 2f4ff89 | 2012-09-24 21:09:54 +0000 | [diff] [blame] | 296 | &_captureFrame); |
mikhal@webrtc.org | 2ab104e | 2011-12-09 02:46:22 +0000 | [diff] [blame] | 297 | if (conversionResult < 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 298 | { |
| 299 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id, |
| 300 | "Failed to convert capture frame from type %d to I420", |
| 301 | frameInfo.rawType); |
| 302 | return -1; |
| 303 | } |
mikhal@webrtc.org | e83d311 | 2012-10-29 15:59:40 +0000 | [diff] [blame] | 304 | DeliverCapturedFrame(_captureFrame, captureTime); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 305 | } |
| 306 | else // Encoded format |
| 307 | { |
mflodman@webrtc.org | 3ba883f | 2013-06-07 13:57:57 +0000 | [diff] [blame] | 308 | assert(false); |
| 309 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 310 | } |
| 311 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 312 | const uint32_t processTime = |
| 313 | (uint32_t)(TickTime::Now() - startProcessTime).Milliseconds(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 314 | if (processTime > 10) // If the process time is too long MJPG will not work well. |
| 315 | { |
| 316 | WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceVideoCapture, _id, |
| 317 | "Too long processing time of Incoming frame: %ums", |
| 318 | (unsigned int) processTime); |
| 319 | } |
| 320 | |
| 321 | return 0; |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 322 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 323 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 324 | int32_t VideoCaptureImpl::IncomingFrameI420( |
| 325 | const VideoFrameI420& video_frame, int64_t captureTime) { |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 326 | |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 327 | CriticalSectionScoped cs(&_callBackCs); |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 328 | int size_y = video_frame.height * video_frame.y_pitch; |
mikhal@webrtc.org | 701567a | 2012-11-09 18:45:12 +0000 | [diff] [blame] | 329 | int size_u = video_frame.u_pitch * ((video_frame.height + 1) / 2); |
| 330 | 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] | 331 | // TODO(mikhal): Can we use Swap here? This will do a memcpy. |
| 332 | int ret = _captureFrame.CreateFrame(size_y, video_frame.y_plane, |
| 333 | size_u, video_frame.u_plane, |
| 334 | size_v, video_frame.v_plane, |
| 335 | video_frame.width, video_frame.height, |
| 336 | video_frame.y_pitch, video_frame.u_pitch, |
| 337 | video_frame.v_pitch); |
| 338 | if (ret < 0) { |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 339 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id, |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 340 | "Failed to create I420VideoFrame"); |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 341 | return -1; |
| 342 | } |
| 343 | |
mikhal@webrtc.org | e83d311 | 2012-10-29 15:59:40 +0000 | [diff] [blame] | 344 | DeliverCapturedFrame(_captureFrame, captureTime); |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 345 | |
| 346 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 347 | } |
| 348 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 349 | int32_t VideoCaptureImpl::SetCaptureRotation(VideoCaptureRotation rotation) { |
mikhal@webrtc.org | 0f34fd7 | 2012-11-19 21:15:35 +0000 | [diff] [blame] | 350 | CriticalSectionScoped cs(&_apiCs); |
| 351 | CriticalSectionScoped cs2(&_callBackCs); |
| 352 | switch (rotation){ |
| 353 | case kCameraRotate0: |
| 354 | _rotateFrame = kRotateNone; |
| 355 | break; |
| 356 | case kCameraRotate90: |
| 357 | _rotateFrame = kRotate90; |
| 358 | break; |
| 359 | case kCameraRotate180: |
| 360 | _rotateFrame = kRotate180; |
| 361 | break; |
| 362 | case kCameraRotate270: |
| 363 | _rotateFrame = kRotate270; |
| 364 | break; |
| 365 | } |
| 366 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 367 | } |
| 368 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 369 | int32_t VideoCaptureImpl::EnableFrameRateCallback(const bool enable) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 370 | { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 371 | CriticalSectionScoped cs(&_apiCs); |
| 372 | CriticalSectionScoped cs2(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 373 | _frameRateCallBack = enable; |
| 374 | if (enable) |
| 375 | { |
| 376 | _lastFrameRateCallbackTime = TickTime::Now(); |
| 377 | } |
| 378 | return 0; |
| 379 | } |
| 380 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 381 | int32_t VideoCaptureImpl::EnableNoPictureAlarm(const bool enable) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 382 | { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 383 | CriticalSectionScoped cs(&_apiCs); |
| 384 | CriticalSectionScoped cs2(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 385 | _noPictureAlarmCallBack = enable; |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | void VideoCaptureImpl::UpdateFrameCount() |
| 390 | { |
| 391 | if (_incomingFrameTimes[0].MicrosecondTimestamp() == 0) |
| 392 | { |
| 393 | // first no shift |
| 394 | } |
| 395 | else |
| 396 | { |
| 397 | // shift |
| 398 | for (int i = (kFrameRateCountHistorySize - 2); i >= 0; i--) |
| 399 | { |
| 400 | _incomingFrameTimes[i + 1] = _incomingFrameTimes[i]; |
| 401 | } |
| 402 | } |
| 403 | _incomingFrameTimes[0] = TickTime::Now(); |
| 404 | } |
| 405 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 406 | uint32_t VideoCaptureImpl::CalculateFrameRate(const TickTime& now) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 407 | { |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 408 | int32_t num = 0; |
| 409 | int32_t nrOfFrames = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 410 | for (num = 1; num < (kFrameRateCountHistorySize - 1); num++) |
| 411 | { |
| 412 | if (_incomingFrameTimes[num].Ticks() <= 0 |
| 413 | || (now - _incomingFrameTimes[num]).Milliseconds() > kFrameRateHistoryWindowMs) // don't use data older than 2sec |
| 414 | { |
| 415 | break; |
| 416 | } |
| 417 | else |
| 418 | { |
| 419 | nrOfFrames++; |
| 420 | } |
| 421 | } |
| 422 | if (num > 1) |
| 423 | { |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 424 | int64_t diff = (now - _incomingFrameTimes[num - 1]).Milliseconds(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 425 | if (diff > 0) |
| 426 | { |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 427 | return uint32_t((nrOfFrames * 1000.0f / diff) + 0.5f); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
| 431 | return nrOfFrames; |
| 432 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 433 | } // namespace videocapturemodule |
| 434 | } // namespace webrtc |