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