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 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 15 | #include "webrtc/api/video/i420_buffer.h" |
Peter Boström | 1d19441 | 2016-03-21 16:44:31 +0100 | [diff] [blame] | 16 | #include "webrtc/base/refcount.h" |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 17 | #include "webrtc/base/timeutils.h" |
tommi | e4f9650 | 2015-10-20 23:00:48 -0700 | [diff] [blame] | 18 | #include "webrtc/base/trace_event.h" |
pbos@webrtc.org | a9b74ad | 2013-07-12 10:03:52 +0000 | [diff] [blame] | 19 | #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 20 | #include "webrtc/modules/include/module_common_types.h" |
pbos@webrtc.org | a9b74ad | 2013-07-12 10:03:52 +0000 | [diff] [blame] | 21 | #include "webrtc/modules/video_capture/video_capture_config.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 22 | #include "webrtc/system_wrappers/include/clock.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 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( |
Peter Boström | 1d19441 | 2016-03-21 16:44:31 +0100 | [diff] [blame] | 28 | VideoCaptureExternal*& externalCapture) { |
| 29 | rtc::scoped_refptr<VideoCaptureImpl> implementation( |
nisse | b29b9c8 | 2016-12-12 00:22:56 -0800 | [diff] [blame] | 30 | new rtc::RefCountedObject<VideoCaptureImpl>()); |
Peter Boström | 1d19441 | 2016-03-21 16:44:31 +0100 | [diff] [blame] | 31 | externalCapture = implementation.get(); |
| 32 | return implementation; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | } |
| 34 | |
leozwang@webrtc.org | 1745e93 | 2012-03-01 16:30:40 +0000 | [diff] [blame] | 35 | const char* VideoCaptureImpl::CurrentDeviceName() const |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 36 | { |
| 37 | return _deviceUniqueId; |
| 38 | } |
| 39 | |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 40 | // static |
| 41 | int32_t VideoCaptureImpl::RotationFromDegrees(int degrees, |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame] | 42 | VideoRotation* rotation) { |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 43 | switch (degrees) { |
| 44 | case 0: |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame] | 45 | *rotation = kVideoRotation_0; |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 46 | return 0; |
| 47 | case 90: |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame] | 48 | *rotation = kVideoRotation_90; |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 49 | return 0; |
| 50 | case 180: |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame] | 51 | *rotation = kVideoRotation_180; |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 52 | return 0; |
| 53 | case 270: |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame] | 54 | *rotation = kVideoRotation_270; |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 55 | return 0; |
| 56 | default: |
| 57 | return -1;; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // static |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame] | 62 | int32_t VideoCaptureImpl::RotationInDegrees(VideoRotation rotation, |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 63 | int* degrees) { |
| 64 | switch (rotation) { |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame] | 65 | case kVideoRotation_0: |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 66 | *degrees = 0; |
| 67 | return 0; |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame] | 68 | case kVideoRotation_90: |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 69 | *degrees = 90; |
| 70 | return 0; |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame] | 71 | case kVideoRotation_180: |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 72 | *degrees = 180; |
| 73 | return 0; |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame] | 74 | case kVideoRotation_270: |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 +0000 | [diff] [blame] | 75 | *degrees = 270; |
| 76 | return 0; |
| 77 | } |
| 78 | return -1; |
| 79 | } |
| 80 | |
nisse | b29b9c8 | 2016-12-12 00:22:56 -0800 | [diff] [blame] | 81 | VideoCaptureImpl::VideoCaptureImpl() |
| 82 | : _deviceUniqueId(NULL), |
pbos@webrtc.org | 504af45 | 2013-07-02 10:15:43 +0000 | [diff] [blame] | 83 | _requestedCapability(), |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 84 | _lastProcessTimeNanos(rtc::TimeNanos()), |
| 85 | _lastFrameRateCallbackTimeNanos(rtc::TimeNanos()), |
pbos@webrtc.org | 504af45 | 2013-07-02 10:15:43 +0000 | [diff] [blame] | 86 | _dataCallBack(NULL), |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 87 | _lastProcessFrameTimeNanos(rtc::TimeNanos()), |
guoweis@webrtc.org | 59140d6 | 2015-03-09 17:07:31 +0000 | [diff] [blame] | 88 | _rotateFrame(kVideoRotation_0), |
deadbeef | f5629ad | 2016-03-18 11:38:26 -0700 | [diff] [blame] | 89 | apply_rotation_(false) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | _requestedCapability.width = kDefaultWidth; |
| 91 | _requestedCapability.height = kDefaultHeight; |
| 92 | _requestedCapability.maxFPS = 30; |
| 93 | _requestedCapability.rawType = kVideoI420; |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 94 | memset(_incomingFrameTimesNanos, 0, sizeof(_incomingFrameTimesNanos)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | VideoCaptureImpl::~VideoCaptureImpl() |
| 98 | { |
| 99 | DeRegisterCaptureDataCallback(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | if (_deviceUniqueId) |
| 101 | delete[] _deviceUniqueId; |
| 102 | } |
| 103 | |
mallinath@webrtc.org | 7433a08 | 2014-01-29 00:56:02 +0000 | [diff] [blame] | 104 | void VideoCaptureImpl::RegisterCaptureDataCallback( |
nisse | b29b9c8 | 2016-12-12 00:22:56 -0800 | [diff] [blame] | 105 | rtc::VideoSinkInterface<VideoFrame>* dataCallBack) { |
kthelgason | ff046c7 | 2017-03-31 02:03:55 -0700 | [diff] [blame^] | 106 | rtc::CritScope cs(&_apiCs); |
nisse | b29b9c8 | 2016-12-12 00:22:56 -0800 | [diff] [blame] | 107 | _dataCallBack = dataCallBack; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 108 | } |
| 109 | |
mallinath@webrtc.org | 7433a08 | 2014-01-29 00:56:02 +0000 | [diff] [blame] | 110 | void VideoCaptureImpl::DeRegisterCaptureDataCallback() { |
kthelgason | ff046c7 | 2017-03-31 02:03:55 -0700 | [diff] [blame^] | 111 | rtc::CritScope cs(&_apiCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 112 | _dataCallBack = NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 113 | } |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 114 | int32_t VideoCaptureImpl::DeliverCapturedFrame(VideoFrame& captureFrame) { |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 115 | UpdateFrameCount(); // frame count used for local frame rate callback. |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 116 | |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 117 | if (_dataCallBack) { |
nisse | b29b9c8 | 2016-12-12 00:22:56 -0800 | [diff] [blame] | 118 | _dataCallBack->OnFrame(captureFrame); |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 124 | int32_t VideoCaptureImpl::IncomingFrame( |
| 125 | uint8_t* videoFrame, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 126 | size_t videoFrameLength, |
mflodman@webrtc.org | 4f9e44f | 2012-02-23 09:00:26 +0000 | [diff] [blame] | 127 | const VideoCaptureCapability& frameInfo, |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 128 | int64_t captureTime/*=0*/) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 129 | { |
kthelgason | ff046c7 | 2017-03-31 02:03:55 -0700 | [diff] [blame^] | 130 | rtc::CritScope cs(&_apiCs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 131 | |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 132 | const int32_t width = frameInfo.width; |
| 133 | const int32_t height = frameInfo.height; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 134 | |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 +0000 | [diff] [blame] | 135 | TRACE_EVENT1("webrtc", "VC::IncomingFrame", "capture_time", captureTime); |
| 136 | |
nisse | 1e32122 | 2017-02-20 23:27:37 -0800 | [diff] [blame] | 137 | // Not encoded, convert to I420. |
| 138 | const VideoType commonVideoType = |
| 139 | RawVideoTypeToCommonVideoVideoType(frameInfo.rawType); |
| 140 | |
| 141 | if (frameInfo.rawType != kVideoMJPEG && |
| 142 | CalcBufferSize(commonVideoType, width, |
| 143 | abs(height)) != videoFrameLength) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | { |
nisse | 1e32122 | 2017-02-20 23:27:37 -0800 | [diff] [blame] | 145 | LOG(LS_ERROR) << "Wrong incoming frame length."; |
mflodman@webrtc.org | 3ba883f | 2013-06-07 13:57:57 +0000 | [diff] [blame] | 146 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 147 | } |
| 148 | |
nisse | 1e32122 | 2017-02-20 23:27:37 -0800 | [diff] [blame] | 149 | int stride_y = width; |
| 150 | int stride_uv = (width + 1) / 2; |
| 151 | int target_width = width; |
| 152 | int target_height = height; |
| 153 | |
| 154 | // SetApplyRotation doesn't take any lock. Make a local copy here. |
| 155 | bool apply_rotation = apply_rotation_; |
| 156 | |
| 157 | if (apply_rotation) { |
| 158 | // Rotating resolution when for 90/270 degree rotations. |
| 159 | if (_rotateFrame == kVideoRotation_90 || |
| 160 | _rotateFrame == kVideoRotation_270) { |
| 161 | target_width = abs(height); |
| 162 | target_height = width; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // Setting absolute height (in case it was negative). |
| 167 | // In Windows, the image starts bottom left, instead of top left. |
| 168 | // Setting a negative source height, inverts the image (within LibYuv). |
| 169 | |
| 170 | // TODO(nisse): Use a pool? |
| 171 | rtc::scoped_refptr<I420Buffer> buffer = I420Buffer::Create( |
| 172 | target_width, abs(target_height), stride_y, stride_uv, stride_uv); |
| 173 | const int conversionResult = ConvertToI420( |
| 174 | commonVideoType, videoFrame, 0, 0, // No cropping |
| 175 | width, height, videoFrameLength, |
| 176 | apply_rotation ? _rotateFrame : kVideoRotation_0, buffer.get()); |
| 177 | if (conversionResult < 0) |
| 178 | { |
| 179 | LOG(LS_ERROR) << "Failed to convert capture frame from type " |
| 180 | << frameInfo.rawType << "to I420."; |
| 181 | return -1; |
| 182 | } |
| 183 | |
| 184 | VideoFrame captureFrame( |
| 185 | buffer, 0, rtc::TimeMillis(), |
| 186 | !apply_rotation ? _rotateFrame : kVideoRotation_0); |
| 187 | captureFrame.set_ntp_time_ms(captureTime); |
| 188 | |
| 189 | DeliverCapturedFrame(captureFrame); |
| 190 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 191 | return 0; |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 +0000 | [diff] [blame] | 192 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 193 | |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 +0000 | [diff] [blame] | 194 | int32_t VideoCaptureImpl::SetCaptureRotation(VideoRotation rotation) { |
kthelgason | ff046c7 | 2017-03-31 02:03:55 -0700 | [diff] [blame^] | 195 | rtc::CritScope cs(&_apiCs); |
guoweis@webrtc.org | 59140d6 | 2015-03-09 17:07:31 +0000 | [diff] [blame] | 196 | _rotateFrame = rotation; |
mikhal@webrtc.org | 0f34fd7 | 2012-11-19 21:15:35 +0000 | [diff] [blame] | 197 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 198 | } |
| 199 | |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 200 | bool VideoCaptureImpl::SetApplyRotation(bool enable) { |
Guo-wei Shieh | 64c1e8c | 2015-04-01 15:33:06 -0700 | [diff] [blame] | 201 | // We can't take any lock here as it'll cause deadlock with IncomingFrame. |
| 202 | |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 203 | // The effect of this is the last caller wins. |
| 204 | apply_rotation_ = enable; |
| 205 | return true; |
| 206 | } |
| 207 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 208 | void VideoCaptureImpl::UpdateFrameCount() |
| 209 | { |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 210 | if (_incomingFrameTimesNanos[0] / rtc::kNumNanosecsPerMicrosec == 0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 211 | { |
| 212 | // first no shift |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | // shift |
| 217 | for (int i = (kFrameRateCountHistorySize - 2); i >= 0; i--) |
| 218 | { |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 219 | _incomingFrameTimesNanos[i + 1] = _incomingFrameTimesNanos[i]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 220 | } |
| 221 | } |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 222 | _incomingFrameTimesNanos[0] = rtc::TimeNanos(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 225 | uint32_t VideoCaptureImpl::CalculateFrameRate(int64_t now_ns) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 226 | { |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 227 | int32_t num = 0; |
| 228 | int32_t nrOfFrames = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 229 | for (num = 1; num < (kFrameRateCountHistorySize - 1); num++) |
| 230 | { |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 231 | if (_incomingFrameTimesNanos[num] <= 0 || |
| 232 | (now_ns - _incomingFrameTimesNanos[num]) / |
| 233 | rtc::kNumNanosecsPerMillisec > |
| 234 | kFrameRateHistoryWindowMs) // don't use data older than 2sec |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 235 | { |
| 236 | break; |
| 237 | } |
| 238 | else |
| 239 | { |
| 240 | nrOfFrames++; |
| 241 | } |
| 242 | } |
| 243 | if (num > 1) |
| 244 | { |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 245 | int64_t diff = (now_ns - _incomingFrameTimesNanos[num - 1]) / |
| 246 | rtc::kNumNanosecsPerMillisec; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 247 | if (diff > 0) |
| 248 | { |
pbos@webrtc.org | dfc5bb9 | 2013-04-10 08:23:13 +0000 | [diff] [blame] | 249 | return uint32_t((nrOfFrames * 1000.0f / diff) + 0.5f); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | return nrOfFrames; |
| 254 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 255 | } // namespace videocapturemodule |
| 256 | } // namespace webrtc |