niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 1 | /* |
mallinath@webrtc.org | 12984f0 | 2012-02-16 18:18:21 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [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 | |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 11 | #include "modules/video_capture/video_capture_impl.h" |
| 12 | |
elham@webrtc.org | 5f49dba | 2012-04-23 21:24:02 | [diff] [blame] | 13 | #include <stdlib.h> |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 14 | #include <string.h> |
elham@webrtc.org | 5f49dba | 2012-04-23 21:24:02 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 16 | #include "api/video/i420_buffer.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 17 | #include "api/video/video_frame_buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 18 | #include "common_video/libyuv/include/webrtc_libyuv.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 19 | #include "modules/video_capture/video_capture_config.h" |
| 20 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 21 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 22 | #include "rtc_base/trace_event.h" |
Mirko Bonadei | 6543206 | 2017-12-11 08:32:13 | [diff] [blame] | 23 | #include "third_party/libyuv/include/libyuv.h" |
pbos@webrtc.org | a9b74ad | 2013-07-12 10:03:52 | [diff] [blame] | 24 | |
Peter Boström | 1d19441 | 2016-03-21 15:44:31 | [diff] [blame] | 25 | namespace webrtc { |
| 26 | namespace videocapturemodule { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 27 | |
ilnik | 00d802b | 2017-04-11 17:34:31 | [diff] [blame] | 28 | const char* VideoCaptureImpl::CurrentDeviceName() const { |
Andreas Pehrson | eee1039 | 2023-05-22 09:21:33 | [diff] [blame] | 29 | RTC_DCHECK_RUN_ON(&api_checker_); |
ilnik | 00d802b | 2017-04-11 17:34:31 | [diff] [blame] | 30 | return _deviceUniqueId; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 31 | } |
| 32 | |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 | [diff] [blame] | 33 | // static |
| 34 | int32_t VideoCaptureImpl::RotationFromDegrees(int degrees, |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 | [diff] [blame] | 35 | VideoRotation* rotation) { |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 | [diff] [blame] | 36 | switch (degrees) { |
| 37 | case 0: |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 | [diff] [blame] | 38 | *rotation = kVideoRotation_0; |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 | [diff] [blame] | 39 | return 0; |
| 40 | case 90: |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 | [diff] [blame] | 41 | *rotation = kVideoRotation_90; |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 | [diff] [blame] | 42 | return 0; |
| 43 | case 180: |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 | [diff] [blame] | 44 | *rotation = kVideoRotation_180; |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 | [diff] [blame] | 45 | return 0; |
| 46 | case 270: |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 | [diff] [blame] | 47 | *rotation = kVideoRotation_270; |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 | [diff] [blame] | 48 | return 0; |
| 49 | default: |
Mirko Bonadei | 72c4250 | 2017-11-09 08:33:23 | [diff] [blame] | 50 | return -1; |
| 51 | ; |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | // static |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 | [diff] [blame] | 56 | int32_t VideoCaptureImpl::RotationInDegrees(VideoRotation rotation, |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 | [diff] [blame] | 57 | int* degrees) { |
| 58 | switch (rotation) { |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 | [diff] [blame] | 59 | case kVideoRotation_0: |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 | [diff] [blame] | 60 | *degrees = 0; |
| 61 | return 0; |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 | [diff] [blame] | 62 | case kVideoRotation_90: |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 | [diff] [blame] | 63 | *degrees = 90; |
| 64 | return 0; |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 | [diff] [blame] | 65 | case kVideoRotation_180: |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 | [diff] [blame] | 66 | *degrees = 180; |
| 67 | return 0; |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 | [diff] [blame] | 68 | case kVideoRotation_270: |
fischman@webrtc.org | 4e65e07 | 2013-10-03 18:23:13 | [diff] [blame] | 69 | *degrees = 270; |
| 70 | return 0; |
| 71 | } |
| 72 | return -1; |
| 73 | } |
| 74 | |
nisse | b29b9c8 | 2016-12-12 08:22:56 | [diff] [blame] | 75 | VideoCaptureImpl::VideoCaptureImpl() |
| 76 | : _deviceUniqueId(NULL), |
pbos@webrtc.org | 504af45 | 2013-07-02 10:15:43 | [diff] [blame] | 77 | _requestedCapability(), |
Evan Shrubsole | 652c771 | 2025-03-21 09:47:35 | [diff] [blame] | 78 | _lastProcessTimeNanos(TimeNanos()), |
| 79 | _lastFrameRateCallbackTimeNanos(TimeNanos()), |
pbos@webrtc.org | 504af45 | 2013-07-02 10:15:43 | [diff] [blame] | 80 | _dataCallBack(NULL), |
Michael Olbrich | 079e93d | 2022-12-06 15:27:44 | [diff] [blame] | 81 | _rawDataCallBack(NULL), |
Evan Shrubsole | 652c771 | 2025-03-21 09:47:35 | [diff] [blame] | 82 | _lastProcessFrameTimeNanos(TimeNanos()), |
guoweis@webrtc.org | 59140d6 | 2015-03-09 17:07:31 | [diff] [blame] | 83 | _rotateFrame(kVideoRotation_0), |
deadbeef | f5629ad | 2016-03-18 18:38:26 | [diff] [blame] | 84 | apply_rotation_(false) { |
Mirko Bonadei | 72c4250 | 2017-11-09 08:33:23 | [diff] [blame] | 85 | _requestedCapability.width = kDefaultWidth; |
| 86 | _requestedCapability.height = kDefaultHeight; |
| 87 | _requestedCapability.maxFPS = 30; |
| 88 | _requestedCapability.videoType = VideoType::kI420; |
| 89 | memset(_incomingFrameTimesNanos, 0, sizeof(_incomingFrameTimesNanos)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 90 | } |
| 91 | |
Mirko Bonadei | 72c4250 | 2017-11-09 08:33:23 | [diff] [blame] | 92 | VideoCaptureImpl::~VideoCaptureImpl() { |
Andreas Pehrson | eee1039 | 2023-05-22 09:21:33 | [diff] [blame] | 93 | RTC_DCHECK_RUN_ON(&api_checker_); |
Mirko Bonadei | 72c4250 | 2017-11-09 08:33:23 | [diff] [blame] | 94 | DeRegisterCaptureDataCallback(); |
| 95 | if (_deviceUniqueId) |
| 96 | delete[] _deviceUniqueId; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 97 | } |
| 98 | |
mallinath@webrtc.org | 7433a08 | 2014-01-29 00:56:02 | [diff] [blame] | 99 | void VideoCaptureImpl::RegisterCaptureDataCallback( |
Evan Shrubsole | 8f7678f | 2025-04-01 14:23:55 | [diff] [blame] | 100 | VideoSinkInterface<VideoFrame>* dataCallBack) { |
Markus Handell | 4379a7d | 2020-07-13 11:09:15 | [diff] [blame] | 101 | MutexLock lock(&api_lock_); |
Michael Olbrich | 079e93d | 2022-12-06 15:27:44 | [diff] [blame] | 102 | RTC_DCHECK(!_rawDataCallBack); |
Mirko Bonadei | 72c4250 | 2017-11-09 08:33:23 | [diff] [blame] | 103 | _dataCallBack = dataCallBack; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 104 | } |
| 105 | |
Michael Olbrich | 079e93d | 2022-12-06 15:27:44 | [diff] [blame] | 106 | void VideoCaptureImpl::RegisterCaptureDataCallback( |
| 107 | RawVideoSinkInterface* dataCallBack) { |
| 108 | MutexLock lock(&api_lock_); |
| 109 | RTC_DCHECK(!_dataCallBack); |
| 110 | _rawDataCallBack = dataCallBack; |
| 111 | } |
| 112 | |
mallinath@webrtc.org | 7433a08 | 2014-01-29 00:56:02 | [diff] [blame] | 113 | void VideoCaptureImpl::DeRegisterCaptureDataCallback() { |
Markus Handell | 4379a7d | 2020-07-13 11:09:15 | [diff] [blame] | 114 | MutexLock lock(&api_lock_); |
Mirko Bonadei | 72c4250 | 2017-11-09 08:33:23 | [diff] [blame] | 115 | _dataCallBack = NULL; |
Michael Olbrich | 079e93d | 2022-12-06 15:27:44 | [diff] [blame] | 116 | _rawDataCallBack = NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 117 | } |
Miguel Casas-Sanchez | 4765070 | 2015-05-30 00:21:40 | [diff] [blame] | 118 | int32_t VideoCaptureImpl::DeliverCapturedFrame(VideoFrame& captureFrame) { |
Andreas Pehrson | eee1039 | 2023-05-22 09:21:33 | [diff] [blame] | 119 | RTC_CHECK_RUNS_SERIALIZED(&capture_checker_); |
| 120 | |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 | [diff] [blame] | 121 | UpdateFrameCount(); // frame count used for local frame rate callback. |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 | [diff] [blame] | 122 | |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 | [diff] [blame] | 123 | if (_dataCallBack) { |
nisse | b29b9c8 | 2016-12-12 08:22:56 | [diff] [blame] | 124 | _dataCallBack->OnFrame(captureFrame); |
mikhal@webrtc.org | 80f14d2 | 2012-10-11 15:03:53 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
Michael Olbrich | 079e93d | 2022-12-06 15:27:44 | [diff] [blame] | 130 | void VideoCaptureImpl::DeliverRawFrame(uint8_t* videoFrame, |
| 131 | size_t videoFrameLength, |
| 132 | const VideoCaptureCapability& frameInfo, |
| 133 | int64_t captureTime) { |
Andreas Pehrson | eee1039 | 2023-05-22 09:21:33 | [diff] [blame] | 134 | RTC_CHECK_RUNS_SERIALIZED(&capture_checker_); |
| 135 | |
Michael Olbrich | 079e93d | 2022-12-06 15:27:44 | [diff] [blame] | 136 | UpdateFrameCount(); |
| 137 | _rawDataCallBack->OnRawFrame(videoFrame, videoFrameLength, frameInfo, |
| 138 | _rotateFrame, captureTime); |
| 139 | } |
| 140 | |
Mirko Bonadei | 72c4250 | 2017-11-09 08:33:23 | [diff] [blame] | 141 | int32_t VideoCaptureImpl::IncomingFrame(uint8_t* videoFrame, |
| 142 | size_t videoFrameLength, |
| 143 | const VideoCaptureCapability& frameInfo, |
| 144 | int64_t captureTime /*=0*/) { |
Andreas Pehrson | eee1039 | 2023-05-22 09:21:33 | [diff] [blame] | 145 | RTC_CHECK_RUNS_SERIALIZED(&capture_checker_); |
Markus Handell | 4379a7d | 2020-07-13 11:09:15 | [diff] [blame] | 146 | MutexLock lock(&api_lock_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 147 | |
Mirko Bonadei | 72c4250 | 2017-11-09 08:33:23 | [diff] [blame] | 148 | const int32_t width = frameInfo.width; |
| 149 | const int32_t height = frameInfo.height; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 150 | |
Mirko Bonadei | 72c4250 | 2017-11-09 08:33:23 | [diff] [blame] | 151 | TRACE_EVENT1("webrtc", "VC::IncomingFrame", "capture_time", captureTime); |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 | [diff] [blame] | 152 | |
Michael Olbrich | 079e93d | 2022-12-06 15:27:44 | [diff] [blame] | 153 | if (_rawDataCallBack) { |
| 154 | DeliverRawFrame(videoFrame, videoFrameLength, frameInfo, captureTime); |
| 155 | return 0; |
| 156 | } |
| 157 | |
Mirko Bonadei | 72c4250 | 2017-11-09 08:33:23 | [diff] [blame] | 158 | // Not encoded, convert to I420. |
Andreas Pehrson | b1a1740 | 2023-01-27 14:59:44 | [diff] [blame] | 159 | if (frameInfo.videoType != VideoType::kMJPEG) { |
| 160 | // Allow buffers larger than expected. On linux gstreamer allocates buffers |
| 161 | // page-aligned and v4l2loopback passes us the buffer size verbatim which |
| 162 | // for most cases is larger than expected. |
| 163 | // See https://github.com/umlaeute/v4l2loopback/issues/190. |
| 164 | if (auto size = CalcBufferSize(frameInfo.videoType, width, abs(height)); |
| 165 | videoFrameLength < size) { |
| 166 | RTC_LOG(LS_ERROR) << "Wrong incoming frame length. Expected " << size |
| 167 | << ", Got " << videoFrameLength << "."; |
| 168 | return -1; |
| 169 | } |
Mirko Bonadei | 72c4250 | 2017-11-09 08:33:23 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | int stride_y = width; |
| 173 | int stride_uv = (width + 1) / 2; |
| 174 | int target_width = width; |
Robert Bares | 0eb7d3ff | 2018-10-28 00:16:33 | [diff] [blame] | 175 | int target_height = abs(height); |
Mirko Bonadei | 72c4250 | 2017-11-09 08:33:23 | [diff] [blame] | 176 | |
Andreas Pehrson | 8f1903e | 2023-05-17 12:03:17 | [diff] [blame] | 177 | if (apply_rotation_) { |
Mirko Bonadei | 72c4250 | 2017-11-09 08:33:23 | [diff] [blame] | 178 | // Rotating resolution when for 90/270 degree rotations. |
| 179 | if (_rotateFrame == kVideoRotation_90 || |
| 180 | _rotateFrame == kVideoRotation_270) { |
| 181 | target_width = abs(height); |
| 182 | target_height = width; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 183 | } |
Mirko Bonadei | 72c4250 | 2017-11-09 08:33:23 | [diff] [blame] | 184 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 185 | |
Mirko Bonadei | 72c4250 | 2017-11-09 08:33:23 | [diff] [blame] | 186 | // Setting absolute height (in case it was negative). |
| 187 | // In Windows, the image starts bottom left, instead of top left. |
| 188 | // Setting a negative source height, inverts the image (within LibYuv). |
Mirko Bonadei | 72c4250 | 2017-11-09 08:33:23 | [diff] [blame] | 189 | rtc::scoped_refptr<I420Buffer> buffer = I420Buffer::Create( |
Robert Bares | 0eb7d3ff | 2018-10-28 00:16:33 | [diff] [blame] | 190 | target_width, target_height, stride_y, stride_uv, stride_uv); |
mallikarjun82 | 12e555b | 2017-11-15 09:05:56 | [diff] [blame] | 191 | |
| 192 | libyuv::RotationMode rotation_mode = libyuv::kRotate0; |
Andreas Pehrson | 8f1903e | 2023-05-17 12:03:17 | [diff] [blame] | 193 | if (apply_rotation_) { |
mallikarjun82 | 12e555b | 2017-11-15 09:05:56 | [diff] [blame] | 194 | switch (_rotateFrame) { |
| 195 | case kVideoRotation_0: |
| 196 | rotation_mode = libyuv::kRotate0; |
| 197 | break; |
| 198 | case kVideoRotation_90: |
| 199 | rotation_mode = libyuv::kRotate90; |
| 200 | break; |
| 201 | case kVideoRotation_180: |
| 202 | rotation_mode = libyuv::kRotate180; |
| 203 | break; |
| 204 | case kVideoRotation_270: |
| 205 | rotation_mode = libyuv::kRotate270; |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | const int conversionResult = libyuv::ConvertToI420( |
| 211 | videoFrame, videoFrameLength, buffer.get()->MutableDataY(), |
| 212 | buffer.get()->StrideY(), buffer.get()->MutableDataU(), |
| 213 | buffer.get()->StrideU(), buffer.get()->MutableDataV(), |
| 214 | buffer.get()->StrideV(), 0, 0, // No Cropping |
| 215 | width, height, target_width, target_height, rotation_mode, |
| 216 | ConvertVideoType(frameInfo.videoType)); |
Michael Froman | 3e1484e | 2023-08-16 21:31:19 | [diff] [blame] | 217 | if (conversionResult != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 218 | RTC_LOG(LS_ERROR) << "Failed to convert capture frame from type " |
| 219 | << static_cast<int>(frameInfo.videoType) << "to I420."; |
Mirko Bonadei | 72c4250 | 2017-11-09 08:33:23 | [diff] [blame] | 220 | return -1; |
| 221 | } |
nisse | 1e32122 | 2017-02-21 07:27:37 | [diff] [blame] | 222 | |
Artem Titov | 1ebfb6a | 2019-01-03 22:49:37 | [diff] [blame] | 223 | VideoFrame captureFrame = |
| 224 | VideoFrame::Builder() |
| 225 | .set_video_frame_buffer(buffer) |
Per K | 0fa9088 | 2024-03-13 08:52:41 | [diff] [blame] | 226 | .set_rtp_timestamp(0) |
Evan Shrubsole | 652c771 | 2025-03-21 09:47:35 | [diff] [blame] | 227 | .set_timestamp_ms(TimeMillis()) |
Andreas Pehrson | 8f1903e | 2023-05-17 12:03:17 | [diff] [blame] | 228 | .set_rotation(!apply_rotation_ ? _rotateFrame : kVideoRotation_0) |
Artem Titov | 1ebfb6a | 2019-01-03 22:49:37 | [diff] [blame] | 229 | .build(); |
Mirko Bonadei | 72c4250 | 2017-11-09 08:33:23 | [diff] [blame] | 230 | captureFrame.set_ntp_time_ms(captureTime); |
nisse | 1e32122 | 2017-02-21 07:27:37 | [diff] [blame] | 231 | |
Mirko Bonadei | 72c4250 | 2017-11-09 08:33:23 | [diff] [blame] | 232 | DeliverCapturedFrame(captureFrame); |
nisse | 1e32122 | 2017-02-21 07:27:37 | [diff] [blame] | 233 | |
Mirko Bonadei | 72c4250 | 2017-11-09 08:33:23 | [diff] [blame] | 234 | return 0; |
wu@webrtc.org | f10ea31 | 2011-10-14 17:16:04 | [diff] [blame] | 235 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 236 | |
Mirko Bonadei | 5aec1f6 | 2018-08-29 11:27:15 | [diff] [blame] | 237 | int32_t VideoCaptureImpl::StartCapture( |
| 238 | const VideoCaptureCapability& capability) { |
Andreas Pehrson | eee1039 | 2023-05-22 09:21:33 | [diff] [blame] | 239 | RTC_DCHECK_RUN_ON(&api_checker_); |
Mirko Bonadei | 5aec1f6 | 2018-08-29 11:27:15 | [diff] [blame] | 240 | _requestedCapability = capability; |
| 241 | return -1; |
| 242 | } |
| 243 | |
| 244 | int32_t VideoCaptureImpl::StopCapture() { |
| 245 | return -1; |
| 246 | } |
| 247 | |
| 248 | bool VideoCaptureImpl::CaptureStarted() { |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | int32_t VideoCaptureImpl::CaptureSettings( |
| 253 | VideoCaptureCapability& /*settings*/) { |
| 254 | return -1; |
| 255 | } |
| 256 | |
guoweis@webrtc.org | 5a7dc39 | 2015-02-13 14:31:26 | [diff] [blame] | 257 | int32_t VideoCaptureImpl::SetCaptureRotation(VideoRotation rotation) { |
Markus Handell | 4379a7d | 2020-07-13 11:09:15 | [diff] [blame] | 258 | MutexLock lock(&api_lock_); |
guoweis@webrtc.org | 59140d6 | 2015-03-09 17:07:31 | [diff] [blame] | 259 | _rotateFrame = rotation; |
mikhal@webrtc.org | 0f34fd7 | 2012-11-19 21:15:35 | [diff] [blame] | 260 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 261 | } |
| 262 | |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 | [diff] [blame] | 263 | bool VideoCaptureImpl::SetApplyRotation(bool enable) { |
Andreas Pehrson | 8f1903e | 2023-05-17 12:03:17 | [diff] [blame] | 264 | MutexLock lock(&api_lock_); |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 | [diff] [blame] | 265 | apply_rotation_ = enable; |
| 266 | return true; |
| 267 | } |
| 268 | |
Mirko Bonadei | 5aec1f6 | 2018-08-29 11:27:15 | [diff] [blame] | 269 | bool VideoCaptureImpl::GetApplyRotation() { |
Andreas Pehrson | 8f1903e | 2023-05-17 12:03:17 | [diff] [blame] | 270 | MutexLock lock(&api_lock_); |
Mirko Bonadei | 5aec1f6 | 2018-08-29 11:27:15 | [diff] [blame] | 271 | return apply_rotation_; |
| 272 | } |
| 273 | |
ilnik | 00d802b | 2017-04-11 17:34:31 | [diff] [blame] | 274 | void VideoCaptureImpl::UpdateFrameCount() { |
Andreas Pehrson | eee1039 | 2023-05-22 09:21:33 | [diff] [blame] | 275 | RTC_CHECK_RUNS_SERIALIZED(&capture_checker_); |
| 276 | |
Evan Shrubsole | 652c771 | 2025-03-21 09:47:35 | [diff] [blame] | 277 | if (_incomingFrameTimesNanos[0] / kNumNanosecsPerMicrosec == 0) { |
ilnik | 00d802b | 2017-04-11 17:34:31 | [diff] [blame] | 278 | // first no shift |
| 279 | } else { |
| 280 | // shift |
| 281 | for (int i = (kFrameRateCountHistorySize - 2); i >= 0; --i) { |
| 282 | _incomingFrameTimesNanos[i + 1] = _incomingFrameTimesNanos[i]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 283 | } |
ilnik | 00d802b | 2017-04-11 17:34:31 | [diff] [blame] | 284 | } |
Evan Shrubsole | 652c771 | 2025-03-21 09:47:35 | [diff] [blame] | 285 | _incomingFrameTimesNanos[0] = TimeNanos(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 286 | } |
| 287 | |
ilnik | 00d802b | 2017-04-11 17:34:31 | [diff] [blame] | 288 | uint32_t VideoCaptureImpl::CalculateFrameRate(int64_t now_ns) { |
Andreas Pehrson | eee1039 | 2023-05-22 09:21:33 | [diff] [blame] | 289 | RTC_CHECK_RUNS_SERIALIZED(&capture_checker_); |
| 290 | |
ilnik | 00d802b | 2017-04-11 17:34:31 | [diff] [blame] | 291 | int32_t num = 0; |
| 292 | int32_t nrOfFrames = 0; |
| 293 | for (num = 1; num < (kFrameRateCountHistorySize - 1); ++num) { |
| 294 | if (_incomingFrameTimesNanos[num] <= 0 || |
Evan Shrubsole | 652c771 | 2025-03-21 09:47:35 | [diff] [blame] | 295 | (now_ns - _incomingFrameTimesNanos[num]) / kNumNanosecsPerMillisec > |
ilnik | 00d802b | 2017-04-11 17:34:31 | [diff] [blame] | 296 | kFrameRateHistoryWindowMs) { // don't use data older than 2sec |
| 297 | break; |
| 298 | } else { |
| 299 | nrOfFrames++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 300 | } |
ilnik | 00d802b | 2017-04-11 17:34:31 | [diff] [blame] | 301 | } |
| 302 | if (num > 1) { |
Evan Shrubsole | 652c771 | 2025-03-21 09:47:35 | [diff] [blame] | 303 | int64_t diff = |
| 304 | (now_ns - _incomingFrameTimesNanos[num - 1]) / kNumNanosecsPerMillisec; |
ilnik | 00d802b | 2017-04-11 17:34:31 | [diff] [blame] | 305 | if (diff > 0) { |
| 306 | return uint32_t((nrOfFrames * 1000.0f / diff) + 0.5f); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 307 | } |
ilnik | 00d802b | 2017-04-11 17:34:31 | [diff] [blame] | 308 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 309 | |
ilnik | 00d802b | 2017-04-11 17:34:31 | [diff] [blame] | 310 | return nrOfFrames; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 311 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 | [diff] [blame] | 312 | } // namespace videocapturemodule |
| 313 | } // namespace webrtc |