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" |
stefan@webrtc.org | b8e7f4c | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 21 | #include "webrtc/system_wrappers/interface/clock.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | |
elham@webrtc.org | 5f49dba | 2012-04-23 21:24:02 +0000 | [diff] [blame] | 23 | #include <stdlib.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 | |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 +0000 | [diff] [blame] | 216 | TRACE_EVENT1("webrtc", "VC::DeliverCapturedFrame", |
| 217 | "capture_time", capture_time); |
| 218 | |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 219 | if (captureFrame.render_time_ms() == last_capture_time_) { |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 220 | // We don't allow the same capture time for two frames, drop this one. |
| 221 | return -1; |
| 222 | } |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 223 | last_capture_time_ = captureFrame.render_time_ms(); |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 224 | |
| 225 | if (_dataCallBack) { |
| 226 | if (callOnCaptureDelayChanged) { |
| 227 | _dataCallBack->OnCaptureDelayChanged(_id, _captureDelay); |
| 228 | } |
mikhal@webrtc.org | e83d311 | 2012-10-29 15:59:40 +0000 | [diff] [blame] | 229 | _dataCallBack->OnIncomingCapturedFrame(_id, captureFrame); |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 235 | int32_t VideoCaptureImpl::IncomingFrame( |
| 236 | uint8_t* videoFrame, |
| 237 | int32_t videoFrameLength, |
mflodman@webrtc.org | 4f9e44f | 2012-02-23 09:00:26 +0000 | [diff] [blame] | 238 | const VideoCaptureCapability& frameInfo, |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 239 | int64_t captureTime/*=0*/) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 240 | { |
| 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.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 247 | CriticalSectionScoped cs(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 248 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 249 | const int32_t width = frameInfo.width; |
| 250 | const int32_t height = frameInfo.height; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 251 | |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 +0000 | [diff] [blame] | 252 | TRACE_EVENT1("webrtc", "VC::IncomingFrame", "capture_time", captureTime); |
| 253 | |
mflodman@webrtc.org | 4f9e44f | 2012-02-23 09:00:26 +0000 | [diff] [blame] | 254 | if (frameInfo.codecType == kVideoCodecUnknown) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 255 | { |
mflodman@webrtc.org | 4f9e44f | 2012-02-23 09:00:26 +0000 | [diff] [blame] | 256 | // Not encoded, convert to I420. |
mikhal@webrtc.org | e39de16 | 2011-12-27 23:45:30 +0000 | [diff] [blame] | 257 | const VideoType commonVideoType = |
mflodman@webrtc.org | 4f9e44f | 2012-02-23 09:00:26 +0000 | [diff] [blame] | 258 | RawVideoTypeToCommonVideoVideoType(frameInfo.rawType); |
| 259 | |
| 260 | if (frameInfo.rawType != kVideoMJPEG && |
elham@webrtc.org | 5f49dba | 2012-04-23 21:24:02 +0000 | [diff] [blame] | 261 | CalcBufferSize(commonVideoType, width, |
| 262 | abs(height)) != videoFrameLength) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 263 | { |
| 264 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id, |
mflodman@webrtc.org | 4f9e44f | 2012-02-23 09:00:26 +0000 | [diff] [blame] | 265 | "Wrong incoming frame length."); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 266 | return -1; |
| 267 | } |
| 268 | |
mikhal@webrtc.org | 4c4d01d | 2012-11-21 22:18:32 +0000 | [diff] [blame] | 269 | int stride_y = width; |
| 270 | int stride_uv = (width + 1) / 2; |
mikhal@webrtc.org | 0f34fd7 | 2012-11-19 21:15:35 +0000 | [diff] [blame] | 271 | 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.org | 4c4d01d | 2012-11-21 22:18:32 +0000 | [diff] [blame] | 278 | // TODO(mikhal): Update correct aligned stride values. |
| 279 | //Calc16ByteAlignedStride(target_width, &stride_y, &stride_uv); |
mikhal@webrtc.org | 2f4ff89 | 2012-09-24 21:09:54 +0000 | [diff] [blame] | 280 | // 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.org | 0f34fd7 | 2012-11-19 21:15:35 +0000 | [diff] [blame] | 283 | int ret = _captureFrame.CreateEmptyFrame(target_width, |
| 284 | abs(target_height), |
mikhal@webrtc.org | 91a0340 | 2012-10-30 19:19:32 +0000 | [diff] [blame] | 285 | stride_y, |
| 286 | stride_uv, stride_uv); |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 287 | if (ret < 0) |
| 288 | { |
| 289 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id, |
| 290 | "Failed to allocate I420 frame."); |
| 291 | return -1; |
| 292 | } |
mikhal@webrtc.org | a58888d | 2012-01-04 19:23:24 +0000 | [diff] [blame] | 293 | const int conversionResult = ConvertToI420(commonVideoType, |
| 294 | videoFrame, |
| 295 | 0, 0, // No cropping |
| 296 | width, height, |
mflodman@webrtc.org | f381119 | 2012-02-27 08:10:17 +0000 | [diff] [blame] | 297 | videoFrameLength, |
mikhal@webrtc.org | a58888d | 2012-01-04 19:23:24 +0000 | [diff] [blame] | 298 | _rotateFrame, |
mikhal@webrtc.org | 2f4ff89 | 2012-09-24 21:09:54 +0000 | [diff] [blame] | 299 | &_captureFrame); |
mikhal@webrtc.org | 2ab104e | 2011-12-09 02:46:22 +0000 | [diff] [blame] | 300 | if (conversionResult < 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 301 | { |
| 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.org | e83d311 | 2012-10-29 15:59:40 +0000 | [diff] [blame] | 307 | DeliverCapturedFrame(_captureFrame, captureTime); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 308 | } |
| 309 | else // Encoded format |
| 310 | { |
mflodman@webrtc.org | 3ba883f | 2013-06-07 13:57:57 +0000 | [diff] [blame] | 311 | assert(false); |
| 312 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 313 | } |
| 314 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 315 | const uint32_t processTime = |
| 316 | (uint32_t)(TickTime::Now() - startProcessTime).Milliseconds(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 317 | 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.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 325 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 326 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 327 | int32_t VideoCaptureImpl::IncomingFrameI420( |
| 328 | const VideoFrameI420& video_frame, int64_t captureTime) { |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 329 | |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 330 | CriticalSectionScoped cs(&_callBackCs); |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 331 | int size_y = video_frame.height * video_frame.y_pitch; |
mikhal@webrtc.org | 701567a | 2012-11-09 18:45:12 +0000 | [diff] [blame] | 332 | 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.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 334 | // 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.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 342 | WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCapture, _id, |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 343 | "Failed to create I420VideoFrame"); |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 344 | return -1; |
| 345 | } |
| 346 | |
mikhal@webrtc.org | e83d311 | 2012-10-29 15:59:40 +0000 | [diff] [blame] | 347 | DeliverCapturedFrame(_captureFrame, captureTime); |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 348 | |
| 349 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 350 | } |
| 351 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 352 | int32_t VideoCaptureImpl::SetCaptureRotation(VideoCaptureRotation rotation) { |
mikhal@webrtc.org | 0f34fd7 | 2012-11-19 21:15:35 +0000 | [diff] [blame] | 353 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 370 | } |
| 371 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 372 | int32_t VideoCaptureImpl::EnableFrameRateCallback(const bool enable) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 373 | { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 374 | CriticalSectionScoped cs(&_apiCs); |
| 375 | CriticalSectionScoped cs2(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 376 | _frameRateCallBack = enable; |
| 377 | if (enable) |
| 378 | { |
| 379 | _lastFrameRateCallbackTime = TickTime::Now(); |
| 380 | } |
| 381 | return 0; |
| 382 | } |
| 383 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 384 | int32_t VideoCaptureImpl::EnableNoPictureAlarm(const bool enable) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 385 | { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 386 | CriticalSectionScoped cs(&_apiCs); |
| 387 | CriticalSectionScoped cs2(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 388 | _noPictureAlarmCallBack = enable; |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | void 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.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 409 | uint32_t VideoCaptureImpl::CalculateFrameRate(const TickTime& now) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 410 | { |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 411 | int32_t num = 0; |
| 412 | int32_t nrOfFrames = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 413 | 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.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 427 | int64_t diff = (now - _incomingFrameTimes[num - 1]).Milliseconds(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 428 | if (diff > 0) |
| 429 | { |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 430 | return uint32_t((nrOfFrames * 1000.0f / diff) + 0.5f); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
| 434 | return nrOfFrames; |
| 435 | } |
perkj@webrtc.org | 0cc68dc | 2011-09-12 08:53:36 +0000 | [diff] [blame] | 436 | } // namespace videocapturemodule |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 437 | } // namespace webrtc |