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" |
pbos@webrtc.org | 3d5cb33 | 2014-05-14 08:42:07 +0000 | [diff] [blame] | 20 | #include "webrtc/system_wrappers/interface/logging.h" |
pbos@webrtc.org | a9b74ad | 2013-07-12 10:03:52 +0000 | [diff] [blame] | 21 | #include "webrtc/system_wrappers/interface/ref_count.h" |
| 22 | #include "webrtc/system_wrappers/interface/tick_util.h" |
pbos@webrtc.org | a9b74ad | 2013-07-12 10:03:52 +0000 | [diff] [blame] | 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 | { |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 27 | |
| 28 | // Converting the rotation mode from capturemodule's to I420VideoFrame's define. |
| 29 | VideoRotation ConvertRotation(VideoRotationMode rotation) { |
| 30 | switch (rotation) { |
| 31 | case kRotateNone: |
| 32 | return kVideoRotation_0; |
| 33 | case kRotate90: |
| 34 | return kVideoRotation_90; |
| 35 | case kRotate180: |
| 36 | return kVideoRotation_180; |
| 37 | case kRotate270: |
| 38 | return kVideoRotation_270; |
| 39 | } |
| 40 | assert(false); |
| 41 | return kVideoRotation_0; |
| 42 | } |
| 43 | |
perkj@webrtc.org | 0cc68dc | 2011-09-12 08:53:36 +0000 | [diff] [blame] | 44 | namespace videocapturemodule |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | { |
perkj@webrtc.org | 0cc68dc | 2011-09-12 08:53:36 +0000 | [diff] [blame] | 46 | VideoCaptureModule* VideoCaptureImpl::Create( |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 47 | const int32_t id, |
perkj@webrtc.org | 0cc68dc | 2011-09-12 08:53:36 +0000 | [diff] [blame] | 48 | VideoCaptureExternal*& externalCapture) |
| 49 | { |
| 50 | RefCountImpl<VideoCaptureImpl>* implementation = |
| 51 | new RefCountImpl<VideoCaptureImpl>(id); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 52 | externalCapture = implementation; |
| 53 | return implementation; |
| 54 | } |
| 55 | |
leozwang@webrtc.org | 1745e93 | 2012-03-01 16:30:40 +0000 | [diff] [blame] | 56 | const char* VideoCaptureImpl::CurrentDeviceName() const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | { |
| 58 | return _deviceUniqueId; |
| 59 | } |
| 60 | |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 61 | // static |
| 62 | int32_t VideoCaptureImpl::RotationFromDegrees(int degrees, |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame^] | 63 | VideoRotation* rotation) { |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 64 | switch (degrees) { |
| 65 | case 0: |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame^] | 66 | *rotation = kVideoRotation_0; |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 67 | return 0; |
| 68 | case 90: |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame^] | 69 | *rotation = kVideoRotation_90; |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 70 | return 0; |
| 71 | case 180: |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame^] | 72 | *rotation = kVideoRotation_180; |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 73 | return 0; |
| 74 | case 270: |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame^] | 75 | *rotation = kVideoRotation_270; |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 76 | return 0; |
| 77 | default: |
| 78 | return -1;; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // static |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame^] | 83 | int32_t VideoCaptureImpl::RotationInDegrees(VideoRotation rotation, |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 84 | int* degrees) { |
| 85 | switch (rotation) { |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame^] | 86 | case kVideoRotation_0: |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 87 | *degrees = 0; |
| 88 | return 0; |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame^] | 89 | case kVideoRotation_90: |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 90 | *degrees = 90; |
| 91 | return 0; |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame^] | 92 | case kVideoRotation_180: |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 93 | *degrees = 180; |
| 94 | return 0; |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame^] | 95 | case kVideoRotation_270: |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 96 | *degrees = 270; |
| 97 | return 0; |
| 98 | } |
| 99 | return -1; |
| 100 | } |
| 101 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 102 | // returns the number of milliseconds until the module want a worker thread to call Process |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 103 | int64_t VideoCaptureImpl::TimeUntilNextProcess() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 104 | { |
perkj@webrtc.org | c2fde80 | 2012-08-08 14:01:09 +0000 | [diff] [blame] | 105 | CriticalSectionScoped cs(&_callBackCs); |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 106 | const int64_t kProcessIntervalMs = 300; |
| 107 | return kProcessIntervalMs - |
| 108 | (TickTime::Now() - _lastProcessTime).Milliseconds(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // Process any pending tasks such as timeouts |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 112 | int32_t VideoCaptureImpl::Process() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 113 | { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 114 | CriticalSectionScoped cs(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 115 | |
| 116 | const TickTime now = TickTime::Now(); |
| 117 | _lastProcessTime = TickTime::Now(); |
| 118 | |
| 119 | // Handle No picture alarm |
| 120 | |
| 121 | if (_lastProcessFrameCount.Ticks() == _incomingFrameTimes[0].Ticks() && |
| 122 | _captureAlarm != Raised) |
| 123 | { |
| 124 | if (_noPictureAlarmCallBack && _captureCallBack) |
| 125 | { |
| 126 | _captureAlarm = Raised; |
| 127 | _captureCallBack->OnNoPictureAlarm(_id, _captureAlarm); |
| 128 | } |
| 129 | } |
| 130 | else if (_lastProcessFrameCount.Ticks() != _incomingFrameTimes[0].Ticks() && |
| 131 | _captureAlarm != Cleared) |
| 132 | { |
| 133 | if (_noPictureAlarmCallBack && _captureCallBack) |
| 134 | { |
| 135 | _captureAlarm = Cleared; |
| 136 | _captureCallBack->OnNoPictureAlarm(_id, _captureAlarm); |
| 137 | |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // Handle frame rate callback |
| 142 | if ((now - _lastFrameRateCallbackTime).Milliseconds() |
| 143 | > kFrameRateCallbackInterval) |
| 144 | { |
| 145 | if (_frameRateCallBack && _captureCallBack) |
| 146 | { |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 147 | const uint32_t frameRate = CalculateFrameRate(now); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 148 | _captureCallBack->OnCaptureFrameRate(_id, frameRate); |
| 149 | } |
| 150 | _lastFrameRateCallbackTime = now; // Can be set by EnableFrameRateCallback |
| 151 | |
| 152 | } |
| 153 | |
| 154 | _lastProcessFrameCount = _incomingFrameTimes[0]; |
| 155 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 159 | VideoCaptureImpl::VideoCaptureImpl(const int32_t id) |
pbos@webrtc.org | 504af45 | 2013-07-02 10:15:43 +0000 | [diff] [blame] | 160 | : _id(id), |
| 161 | _deviceUniqueId(NULL), |
| 162 | _apiCs(*CriticalSectionWrapper::CreateCriticalSection()), |
| 163 | _captureDelay(0), |
| 164 | _requestedCapability(), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 165 | _callBackCs(*CriticalSectionWrapper::CreateCriticalSection()), |
| 166 | _lastProcessTime(TickTime::Now()), |
pbos@webrtc.org | 504af45 | 2013-07-02 10:15:43 +0000 | [diff] [blame] | 167 | _lastFrameRateCallbackTime(TickTime::Now()), |
| 168 | _frameRateCallBack(false), |
| 169 | _noPictureAlarmCallBack(false), |
| 170 | _captureAlarm(Cleared), |
| 171 | _setCaptureDelay(0), |
| 172 | _dataCallBack(NULL), |
| 173 | _captureCallBack(NULL), |
| 174 | _lastProcessFrameCount(TickTime::Now()), |
| 175 | _rotateFrame(kRotateNone), |
pbos@webrtc.org | e053629 | 2013-10-21 09:02:30 +0000 | [diff] [blame] | 176 | last_capture_time_(0), |
pbos@webrtc.org | 504af45 | 2013-07-02 10:15:43 +0000 | [diff] [blame] | 177 | delta_ntp_internal_ms_( |
| 178 | Clock::GetRealTimeClock()->CurrentNtpInMilliseconds() - |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 179 | TickTime::MillisecondTimestamp()), |
| 180 | apply_rotation_(true) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 181 | _requestedCapability.width = kDefaultWidth; |
| 182 | _requestedCapability.height = kDefaultHeight; |
| 183 | _requestedCapability.maxFPS = 30; |
| 184 | _requestedCapability.rawType = kVideoI420; |
| 185 | _requestedCapability.codecType = kVideoCodecUnknown; |
| 186 | memset(_incomingFrameTimes, 0, sizeof(_incomingFrameTimes)); |
| 187 | } |
| 188 | |
| 189 | VideoCaptureImpl::~VideoCaptureImpl() |
| 190 | { |
| 191 | DeRegisterCaptureDataCallback(); |
| 192 | DeRegisterCaptureCallback(); |
| 193 | delete &_callBackCs; |
| 194 | delete &_apiCs; |
| 195 | |
| 196 | if (_deviceUniqueId) |
| 197 | delete[] _deviceUniqueId; |
| 198 | } |
| 199 | |
mallinath@webrtc.org | 7433a08 | 2014-01-29 00:56:02 +0000 | [diff] [blame] | 200 | void VideoCaptureImpl::RegisterCaptureDataCallback( |
| 201 | VideoCaptureDataCallback& dataCallBack) { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 202 | CriticalSectionScoped cs(&_apiCs); |
| 203 | CriticalSectionScoped cs2(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 204 | _dataCallBack = &dataCallBack; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 205 | } |
| 206 | |
mallinath@webrtc.org | 7433a08 | 2014-01-29 00:56:02 +0000 | [diff] [blame] | 207 | void VideoCaptureImpl::DeRegisterCaptureDataCallback() { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 208 | CriticalSectionScoped cs(&_apiCs); |
| 209 | CriticalSectionScoped cs2(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 210 | _dataCallBack = NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 211 | } |
mallinath@webrtc.org | 7433a08 | 2014-01-29 00:56:02 +0000 | [diff] [blame] | 212 | void VideoCaptureImpl::RegisterCaptureCallback(VideoCaptureFeedBack& callBack) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 213 | |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 214 | CriticalSectionScoped cs(&_apiCs); |
| 215 | CriticalSectionScoped cs2(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 216 | _captureCallBack = &callBack; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 217 | } |
mallinath@webrtc.org | 7433a08 | 2014-01-29 00:56:02 +0000 | [diff] [blame] | 218 | void VideoCaptureImpl::DeRegisterCaptureCallback() { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 219 | |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 220 | CriticalSectionScoped cs(&_apiCs); |
| 221 | CriticalSectionScoped cs2(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 222 | _captureCallBack = NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 223 | } |
mallinath@webrtc.org | 7433a08 | 2014-01-29 00:56:02 +0000 | [diff] [blame] | 224 | void VideoCaptureImpl::SetCaptureDelay(int32_t delayMS) { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 225 | CriticalSectionScoped cs(&_apiCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 226 | _captureDelay = delayMS; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 227 | } |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 228 | int32_t VideoCaptureImpl::CaptureDelay() |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 229 | { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 230 | CriticalSectionScoped cs(&_apiCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 231 | return _setCaptureDelay; |
| 232 | } |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 233 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 234 | int32_t VideoCaptureImpl::DeliverCapturedFrame(I420VideoFrame& captureFrame, |
| 235 | int64_t capture_time) { |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 236 | UpdateFrameCount(); // frame count used for local frame rate callback. |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 237 | |
| 238 | const bool callOnCaptureDelayChanged = _setCaptureDelay != _captureDelay; |
| 239 | // Capture delay changed |
| 240 | if (_setCaptureDelay != _captureDelay) { |
| 241 | _setCaptureDelay = _captureDelay; |
| 242 | } |
| 243 | |
| 244 | // Set the capture time |
| 245 | if (capture_time != 0) { |
andresp@webrtc.org | 77bf5c2 | 2013-09-04 11:35:43 +0000 | [diff] [blame] | 246 | captureFrame.set_render_time_ms(capture_time - delta_ntp_internal_ms_); |
pbos@webrtc.org | 504af45 | 2013-07-02 10:15:43 +0000 | [diff] [blame] | 247 | } else { |
| 248 | captureFrame.set_render_time_ms(TickTime::MillisecondTimestamp()); |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 249 | } |
| 250 | |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 251 | if (captureFrame.render_time_ms() == last_capture_time_) { |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 252 | // We don't allow the same capture time for two frames, drop this one. |
| 253 | return -1; |
| 254 | } |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 255 | last_capture_time_ = captureFrame.render_time_ms(); |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 256 | |
| 257 | if (_dataCallBack) { |
| 258 | if (callOnCaptureDelayChanged) { |
| 259 | _dataCallBack->OnCaptureDelayChanged(_id, _captureDelay); |
| 260 | } |
mikhal@webrtc.org | e83d311 | 2012-10-29 15:59:40 +0000 | [diff] [blame] | 261 | _dataCallBack->OnIncomingCapturedFrame(_id, captureFrame); |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 267 | int32_t VideoCaptureImpl::IncomingFrame( |
| 268 | uint8_t* videoFrame, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 269 | size_t videoFrameLength, |
mflodman@webrtc.org | 4f9e44f | 2012-02-23 09:00:26 +0000 | [diff] [blame] | 270 | const VideoCaptureCapability& frameInfo, |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 271 | int64_t captureTime/*=0*/) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 272 | { |
fischman@webrtc.org | 42694c5 | 2014-06-06 18:28:28 +0000 | [diff] [blame] | 273 | CriticalSectionScoped cs(&_apiCs); |
| 274 | CriticalSectionScoped cs2(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 275 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 276 | const int32_t width = frameInfo.width; |
| 277 | const int32_t height = frameInfo.height; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 278 | |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 +0000 | [diff] [blame] | 279 | TRACE_EVENT1("webrtc", "VC::IncomingFrame", "capture_time", captureTime); |
| 280 | |
mflodman@webrtc.org | 4f9e44f | 2012-02-23 09:00:26 +0000 | [diff] [blame] | 281 | if (frameInfo.codecType == kVideoCodecUnknown) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 282 | { |
mflodman@webrtc.org | 4f9e44f | 2012-02-23 09:00:26 +0000 | [diff] [blame] | 283 | // Not encoded, convert to I420. |
mikhal@webrtc.org | e39de16 | 2011-12-27 23:45:30 +0000 | [diff] [blame] | 284 | const VideoType commonVideoType = |
mflodman@webrtc.org | 4f9e44f | 2012-02-23 09:00:26 +0000 | [diff] [blame] | 285 | RawVideoTypeToCommonVideoVideoType(frameInfo.rawType); |
| 286 | |
| 287 | if (frameInfo.rawType != kVideoMJPEG && |
elham@webrtc.org | 5f49dba | 2012-04-23 21:24:02 +0000 | [diff] [blame] | 288 | CalcBufferSize(commonVideoType, width, |
| 289 | abs(height)) != videoFrameLength) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 290 | { |
pbos@webrtc.org | 3d5cb33 | 2014-05-14 08:42:07 +0000 | [diff] [blame] | 291 | LOG(LS_ERROR) << "Wrong incoming frame length."; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 292 | return -1; |
| 293 | } |
| 294 | |
mikhal@webrtc.org | 4c4d01d | 2012-11-21 22:18:32 +0000 | [diff] [blame] | 295 | int stride_y = width; |
| 296 | int stride_uv = (width + 1) / 2; |
mikhal@webrtc.org | 0f34fd7 | 2012-11-19 21:15:35 +0000 | [diff] [blame] | 297 | int target_width = width; |
| 298 | int target_height = height; |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 299 | |
| 300 | if (apply_rotation_) { |
| 301 | // Rotating resolution when for 90/270 degree rotations. |
| 302 | if (_rotateFrame == kRotate90 || _rotateFrame == kRotate270) { |
| 303 | target_width = abs(height); |
| 304 | target_height = width; |
| 305 | } |
mikhal@webrtc.org | 0f34fd7 | 2012-11-19 21:15:35 +0000 | [diff] [blame] | 306 | } |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 307 | |
mikhal@webrtc.org | 4c4d01d | 2012-11-21 22:18:32 +0000 | [diff] [blame] | 308 | // TODO(mikhal): Update correct aligned stride values. |
| 309 | //Calc16ByteAlignedStride(target_width, &stride_y, &stride_uv); |
mikhal@webrtc.org | 2f4ff89 | 2012-09-24 21:09:54 +0000 | [diff] [blame] | 310 | // Setting absolute height (in case it was negative). |
| 311 | // In Windows, the image starts bottom left, instead of top left. |
| 312 | // Setting a negative source height, inverts the image (within LibYuv). |
sheu@chromium.org | 5dd2ecb | 2013-10-31 23:41:04 +0000 | [diff] [blame] | 313 | int ret = _captureFrame.CreateEmptyFrame(target_width, |
| 314 | abs(target_height), |
| 315 | stride_y, |
| 316 | stride_uv, stride_uv); |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 317 | if (ret < 0) |
| 318 | { |
pbos@webrtc.org | 3d5cb33 | 2014-05-14 08:42:07 +0000 | [diff] [blame] | 319 | LOG(LS_ERROR) << "Failed to create empty frame, this should only " |
| 320 | "happen due to bad parameters."; |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 321 | return -1; |
| 322 | } |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 323 | const int conversionResult = ConvertToI420( |
| 324 | commonVideoType, videoFrame, 0, 0, // No cropping |
| 325 | width, height, videoFrameLength, |
| 326 | apply_rotation_ ? _rotateFrame : kRotateNone, |
| 327 | &_captureFrame); |
mikhal@webrtc.org | 2ab104e | 2011-12-09 02:46:22 +0000 | [diff] [blame] | 328 | if (conversionResult < 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 329 | { |
pbos@webrtc.org | 3d5cb33 | 2014-05-14 08:42:07 +0000 | [diff] [blame] | 330 | LOG(LS_ERROR) << "Failed to convert capture frame from type " |
| 331 | << frameInfo.rawType << "to I420."; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 332 | return -1; |
| 333 | } |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 334 | |
| 335 | if (!apply_rotation_) { |
| 336 | _captureFrame.set_rotation(ConvertRotation(_rotateFrame)); |
| 337 | } else { |
| 338 | _captureFrame.set_rotation(kVideoRotation_0); |
| 339 | } |
| 340 | |
sheu@chromium.org | 5dd2ecb | 2013-10-31 23:41:04 +0000 | [diff] [blame] | 341 | DeliverCapturedFrame(_captureFrame, captureTime); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 342 | } |
| 343 | else // Encoded format |
| 344 | { |
mflodman@webrtc.org | 3ba883f | 2013-06-07 13:57:57 +0000 | [diff] [blame] | 345 | assert(false); |
| 346 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 347 | } |
| 348 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 349 | return 0; |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 350 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 351 | |
pbos@webrtc.org | 2ffb149 | 2013-11-22 13:10:13 +0000 | [diff] [blame] | 352 | int32_t VideoCaptureImpl::IncomingI420VideoFrame(I420VideoFrame* video_frame, |
| 353 | int64_t captureTime) { |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 354 | |
fischman@webrtc.org | 42694c5 | 2014-06-06 18:28:28 +0000 | [diff] [blame] | 355 | CriticalSectionScoped cs(&_apiCs); |
| 356 | CriticalSectionScoped cs2(&_callBackCs); |
pbos@webrtc.org | 2ffb149 | 2013-11-22 13:10:13 +0000 | [diff] [blame] | 357 | DeliverCapturedFrame(*video_frame, captureTime); |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 358 | |
| 359 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 360 | } |
| 361 | |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame^] | 362 | int32_t VideoCaptureImpl::SetCaptureRotation(VideoRotation rotation) { |
mikhal@webrtc.org | 0f34fd7 | 2012-11-19 21:15:35 +0000 | [diff] [blame] | 363 | CriticalSectionScoped cs(&_apiCs); |
| 364 | CriticalSectionScoped cs2(&_callBackCs); |
| 365 | switch (rotation){ |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame^] | 366 | case kVideoRotation_0: |
mikhal@webrtc.org | 0f34fd7 | 2012-11-19 21:15:35 +0000 | [diff] [blame] | 367 | _rotateFrame = kRotateNone; |
| 368 | break; |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame^] | 369 | case kVideoRotation_90: |
mikhal@webrtc.org | 0f34fd7 | 2012-11-19 21:15:35 +0000 | [diff] [blame] | 370 | _rotateFrame = kRotate90; |
| 371 | break; |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame^] | 372 | case kVideoRotation_180: |
mikhal@webrtc.org | 0f34fd7 | 2012-11-19 21:15:35 +0000 | [diff] [blame] | 373 | _rotateFrame = kRotate180; |
| 374 | break; |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame^] | 375 | case kVideoRotation_270: |
mikhal@webrtc.org | 0f34fd7 | 2012-11-19 21:15:35 +0000 | [diff] [blame] | 376 | _rotateFrame = kRotate270; |
| 377 | break; |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 378 | default: |
| 379 | return -1; |
mikhal@webrtc.org | 0f34fd7 | 2012-11-19 21:15:35 +0000 | [diff] [blame] | 380 | } |
| 381 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 382 | } |
| 383 | |
mallinath@webrtc.org | 7433a08 | 2014-01-29 00:56:02 +0000 | [diff] [blame] | 384 | void VideoCaptureImpl::EnableFrameRateCallback(const bool enable) { |
mflodman@webrtc.org | 7845d07 | 2012-03-08 08:09:17 +0000 | [diff] [blame] | 385 | CriticalSectionScoped cs(&_apiCs); |
| 386 | CriticalSectionScoped cs2(&_callBackCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 387 | _frameRateCallBack = enable; |
| 388 | if (enable) |
| 389 | { |
| 390 | _lastFrameRateCallbackTime = TickTime::Now(); |
| 391 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 392 | } |
| 393 | |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 394 | bool VideoCaptureImpl::SetApplyRotation(bool enable) { |
| 395 | CriticalSectionScoped cs(&_apiCs); |
| 396 | CriticalSectionScoped cs2(&_callBackCs); |
| 397 | // The effect of this is the last caller wins. |
| 398 | apply_rotation_ = enable; |
| 399 | return true; |
| 400 | } |
| 401 | |
mallinath@webrtc.org | 7433a08 | 2014-01-29 00:56:02 +0000 | [diff] [blame] | 402 | void VideoCaptureImpl::EnableNoPictureAlarm(const bool enable) { |
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 | _noPictureAlarmCallBack = enable; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | void VideoCaptureImpl::UpdateFrameCount() |
| 409 | { |
| 410 | if (_incomingFrameTimes[0].MicrosecondTimestamp() == 0) |
| 411 | { |
| 412 | // first no shift |
| 413 | } |
| 414 | else |
| 415 | { |
| 416 | // shift |
| 417 | for (int i = (kFrameRateCountHistorySize - 2); i >= 0; i--) |
| 418 | { |
| 419 | _incomingFrameTimes[i + 1] = _incomingFrameTimes[i]; |
| 420 | } |
| 421 | } |
| 422 | _incomingFrameTimes[0] = TickTime::Now(); |
| 423 | } |
| 424 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 425 | uint32_t VideoCaptureImpl::CalculateFrameRate(const TickTime& now) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 426 | { |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 427 | int32_t num = 0; |
| 428 | int32_t nrOfFrames = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 429 | for (num = 1; num < (kFrameRateCountHistorySize - 1); num++) |
| 430 | { |
| 431 | if (_incomingFrameTimes[num].Ticks() <= 0 |
| 432 | || (now - _incomingFrameTimes[num]).Milliseconds() > kFrameRateHistoryWindowMs) // don't use data older than 2sec |
| 433 | { |
| 434 | break; |
| 435 | } |
| 436 | else |
| 437 | { |
| 438 | nrOfFrames++; |
| 439 | } |
| 440 | } |
| 441 | if (num > 1) |
| 442 | { |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 443 | int64_t diff = (now - _incomingFrameTimes[num - 1]).Milliseconds(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 444 | if (diff > 0) |
| 445 | { |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 446 | return uint32_t((nrOfFrames * 1000.0f / diff) + 0.5f); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
| 450 | return nrOfFrames; |
| 451 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 452 | } // namespace videocapturemodule |
| 453 | } // namespace webrtc |