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