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