| #include "api/video/video_rotation.h" |
| 6 | #include "modules/video_capture/video_capture_factory.h" |
| 7 | #include "rtc_base/checks.h" |
| 8 | #include "rtc_base/logging.h" |
| 9 |
| 10 | #ifdef WEBRTC_LINUX // Breaks compilation on MSVC because of ERROR define. |
| 11 | #include <modules/video_capture/video_capture_options.h> |
| 12 | #endif // WEBRTC_LINUX |
| 13 |
| 14 | #include <stdint.h> |
| 15 | #include <memory> |
| 16 | #include <algorithm> |
| 17 |
| 18 | namespace tgcalls { |
| 19 | namespace { |
| 20 |
| 21 | constexpr auto kPreferredWidth = 1280; |
| 22 | constexpr auto kPreferredHeight = 720; |
| 23 | constexpr auto kPreferredFps = 30; |
| 24 |
| 25 | #ifdef WEBRTC_LINUX |
| 26 | [[nodiscard]] webrtc::VideoCaptureOptions GetVideoCaptureOptions() { |
| 27 | auto result = webrtc::VideoCaptureOptions(); |
| 28 | result.set_allow_v4l2(true); |
| 29 | #ifdef WEBRTC_USE_PIPEWIRE |
| 30 | //result.set_allow_pipewire(true); |
| 31 | // This requires a call to result.Init(callback) |
| 32 | // and waiting for the callback to finish. |
| 33 | #endif |
| 34 | return result; |
| 35 | } |
| 36 | #endif |
| 37 |
| 38 | } // namespace |
| 39 |
| 40 | VideoCameraCapturer::VideoCameraCapturer( |
| 41 | std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) |
| 42 | : _sink(sink) { |
| 43 | } |
| 44 |
| 45 | VideoCameraCapturer::~VideoCameraCapturer() { |
| 46 | destroy(); |
| 47 | } |
| 48 |
| 49 | void VideoCameraCapturer::create() { |
| 50 | _failed = false; |
| 51 |
| 52 | #ifdef WEBRTC_LINUX |
| 53 | auto options = GetVideoCaptureOptions(); |
| 54 | const auto info = std::unique_ptr< |
| 55 | webrtc::VideoCaptureModule::DeviceInfo |
| 56 | >(webrtc::VideoCaptureFactory::CreateDeviceInfo(&options)); |
| 57 | #else // WEBRTC_LINUX |
| 58 | const auto info = std::unique_ptr< |
| 59 | webrtc::VideoCaptureModule::DeviceInfo |
| 60 | >(webrtc::VideoCaptureFactory::CreateDeviceInfo()); |
| 61 | #endif // WEBRTC_LINUX |
| 62 |
| 63 | if (!info) { |
| 64 | failed(); |
| 65 | return; |
| 66 | } |
| 67 | const auto count = info->NumberOfDevices(); |
| 68 | if (count <= 0) { |
| 69 | failed(); |
| 70 | return; |
| 71 | } |
| 72 | const auto getId = [&](int index) { |
| 73 | constexpr auto kLengthLimit = 256; |
| 74 | char name[kLengthLimit] = { 0 }; |
| 75 | char id[kLengthLimit] = { 0 }; |
| 76 | return (info->GetDeviceName(index, name, kLengthLimit, id, kLengthLimit) == 0) |
| 77 | ? std::string(id) |
| 78 | : std::string(); |
| 79 | }; |
| 80 | auto preferredId = std::string(); |
| 81 | for (auto i = 0; i != count; ++i) { |
| 82 | const auto id = getId(i); |
| 83 | if ((_requestedDeviceId == id) |
| 84 | || (preferredId.empty() |
| 85 | && (_requestedDeviceId.empty() || _requestedDeviceId == "default"))) { |
| 86 | preferredId = id; |
| 87 | } |
| 88 | } |
| 89 | if (create(info.get(), preferredId)) { |
| 90 | return; |
| 91 | } |
| 92 | for (auto i = 0; i != count; ++i) { |
| 93 | if (create(info.get(), getId(i))) { |
| 94 | return; |
| 95 | } |
| 96 | } |
| 97 | failed(); |
| 98 | } |
| 99 |
| 100 | void VideoCameraCapturer::failed() { |
| 101 | _failed = true; |
| 102 | if (_error) { |
| 103 | _error(); |
| 104 | } |
| 105 | } |
| 106 |
| 107 | bool VideoCameraCapturer::create( |
| 108 | webrtc::VideoCaptureModule::DeviceInfo *info, |
| 109 | const std::string &deviceId) { |
| 110 | const auto deviceString = deviceId.c_str(); |
| 111 | #ifdef WEBRTC_LINUX |
| 112 | auto options = GetVideoCaptureOptions(); |
| 113 | _module = webrtc::VideoCaptureFactory::Create(&options, deviceString); |
| 114 | #else // WEBRTC_LINUX |
| 115 | _module = webrtc::VideoCaptureFactory::Create(deviceString); |
| 116 | #endif // WEBRTC_LINUX |
| 117 |
| 118 | if (!_module) { |
| 119 | RTC_LOG(LS_ERROR) |
| 120 | << "Failed to create VideoCameraCapturer '" << deviceId << "'."; |
| 121 | return false; |
| 122 | } |
| 123 | _module->RegisterCaptureDataCallback(this); |
| 124 |
| 125 | auto requested = webrtc::VideoCaptureCapability(); |
| 126 | requested.videoType = webrtc::VideoType::kI420; |
| 127 | requested.width = kPreferredWidth; |
| 128 | requested.height = kPreferredHeight; |
| 129 | requested.maxFPS = kPreferredFps; |
| 130 | info->GetBestMatchedCapability( |
| 131 | _module->CurrentDeviceName(), |
| 132 | requested, |
| 133 | _capability); |
| 134 | if (!_capability.width || !_capability.height || !_capability.maxFPS) { |
| 135 | _capability.width = kPreferredWidth; |
| 136 | _capability.height = kPreferredHeight; |
| 137 | _capability.maxFPS = kPreferredFps; |
| 138 | } |
| 139 | #ifndef WEBRTC_WIN |
| 140 | _capability.videoType = webrtc::VideoType::kI420; |
| 141 | #endif // WEBRTC_WIN |
| 142 | if (_module->StartCapture(_capability) != 0) { |
| 143 | RTC_LOG(LS_ERROR) |
| 144 | << "Failed to start VideoCameraCapturer '" << _requestedDeviceId << "'."; |
| 145 | destroy(); |
| 146 | return false; |
| 147 | } |
| 148 | _dimensions = std::make_pair(_capability.width, _capability.height); |
| 149 | return true; |
| 150 | } |
| 151 |
| 152 | void VideoCameraCapturer::setState(VideoState state) { |
| 153 | if (_state == state) { |
| 154 | return; |
| 155 | } |
| 156 | _state = state; |
| 157 | if (_state == VideoState::Active) { |
| 158 | create(); |
| 159 | } else { |
| 160 | destroy(); |
| 161 | } |
| 162 | } |
| 163 |
| 164 | void VideoCameraCapturer::setDeviceId(std::string deviceId) { |
| 165 | if (_requestedDeviceId == deviceId) { |
| 166 | return; |
| 167 | } |
| 168 | destroy(); |
| 169 | _requestedDeviceId = deviceId; |
| 170 | if (_state == VideoState::Active) { |
| 171 | create(); |
| 172 | } |
| 173 | } |
| 174 |
| 175 | void VideoCameraCapturer::setPreferredCaptureAspectRatio(float aspectRatio) { |
| 176 | _aspectRatio = aspectRatio; |
| 177 | } |
| 178 |
| 179 | void VideoCameraCapturer::setOnFatalError(std::function<void()> error) { |
| 180 | _error = std::move(error); |
| 181 | if (_failed && _error) { |
| 182 | _error(); |
| 183 | } |
| 184 | } |
| 185 |
| 186 | std::pair<int, int> VideoCameraCapturer::resolution() const { |
| 187 | return _dimensions; |
| 188 | } |
| 189 |
| 190 | void VideoCameraCapturer::destroy() { |
| 191 | _failed = false; |
| 192 | if (!_module) { |
| 193 | return; |
| 194 | } |
| 195 |
| 196 | _module->StopCapture(); |
| 197 | _module->DeRegisterCaptureDataCallback(); |
| 198 | _module = nullptr; |
| 199 | } |
| 200 |
| 201 | void VideoCameraCapturer::OnFrame(const webrtc::VideoFrame &frame) { |
| 202 | if (_state != VideoState::Active) { |
| 203 | return; |
| 204 | } else if (_aspectRatio <= 0.001) { |
| 205 | _sink->OnFrame(frame); |
| 206 | return; |
| 207 | } |
| 208 | const auto originalWidth = frame.width(); |
| 209 | const auto originalHeight = frame.height(); |
| 210 | auto width = (originalWidth > _aspectRatio * originalHeight) |
| 211 | ? int(std::round(_aspectRatio * originalHeight)) |
| 212 | : originalWidth; |
| 213 | auto height = (originalWidth > _aspectRatio * originalHeight) |
| 214 | ? originalHeight |
| 215 | : int(std::round(originalHeight / _aspectRatio)); |
| 216 | if ((width >= originalWidth && height >= originalHeight) || !width || !height) { |
| 217 | _sink->OnFrame(frame); |
| 218 | return; |
| 219 | } |
| 220 |
| 221 | width &= ~int(1); |
| 222 | height &= ~int(1); |
| 223 | const auto left = (originalWidth - width) / 2; |
| 224 | const auto top = (originalHeight - height) / 2; |
| 225 | rtc::scoped_refptr<webrtc::I420Buffer> croppedBuffer = |
| 226 | webrtc::I420Buffer::Create(width, height); |
| 227 | croppedBuffer->CropAndScaleFrom( |
| 228 | *frame.video_frame_buffer()->ToI420(), |
| 229 | left, |
| 230 | top, |
| 231 | width, |
| 232 | height); |
| 233 | webrtc::VideoFrame::Builder croppedBuilder = |
| 234 | webrtc::VideoFrame::Builder() |
| 235 | .set_video_frame_buffer(croppedBuffer) |
| 236 | .set_rotation(webrtc::kVideoRotation_0) |
| 237 | .set_timestamp_us(frame.timestamp_us()) |
| 238 | .set_id(frame.id()); |
| 239 | if (frame.has_update_rect()) { |
| 240 | croppedBuilder.set_update_rect(frame.update_rect().ScaleWithFrame( |
| 241 | frame.width(), |
| 242 | frame.height(), |
| 243 | left, |
| 244 | top, |
| 245 | width, |
| 246 | height, |
| 247 | width, |
| 248 | height)); |
| 249 | } |
| 250 | _sink->OnFrame(croppedBuilder.build()); |
| 251 | } |
| 252 |
| 253 | } // namespace tgcalls |
| 254 |