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