| #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 | #include <stdint.h> |
| 11 | #include <memory> |
| 12 | #include <algorithm> |
| 13 |
| 14 | namespace tgcalls { |
| 15 |
| 16 | VideoCameraCapturer::VideoCameraCapturer(const CreateTag &) { |
| 17 | } |
| 18 |
| 19 | VideoCameraCapturer::~VideoCameraCapturer() { |
| 20 | destroy(); |
| 21 | } |
| 22 |
| 23 | bool VideoCameraCapturer::init( |
| 24 | size_t width, |
| 25 | size_t height, |
| 26 | size_t target_fps, |
| 27 | size_t capture_device_index) { |
| 28 | std::unique_ptr<webrtc::VideoCaptureModule::DeviceInfo> device_info( |
| 29 | webrtc::VideoCaptureFactory::CreateDeviceInfo()); |
| 30 |
| 31 | char device_name[256]; |
| 32 | char unique_name[256]; |
| 33 | if (device_info->GetDeviceName(static_cast<uint32_t>(capture_device_index), |
| 34 | device_name, sizeof(device_name), unique_name, |
| 35 | sizeof(unique_name)) != 0) { |
| 36 | destroy(); |
| 37 | return false; |
| 38 | } |
| 39 |
| 40 | _module = webrtc::VideoCaptureFactory::Create(unique_name); |
| 41 | if (!_module) { |
| 42 | return false; |
| 43 | } |
| 44 | _module->RegisterCaptureDataCallback(this); |
| 45 |
| 46 | device_info->GetCapability(_module->CurrentDeviceName(), 0, _capability); |
| 47 |
| 48 | _capability.width = static_cast<int32_t>(width); |
| 49 | _capability.height = static_cast<int32_t>(height); |
| 50 | _capability.maxFPS = static_cast<int32_t>(target_fps); |
| 51 | _capability.videoType = webrtc::VideoType::kI420; |
| 52 |
| 53 | if (_module->StartCapture(_capability) != 0) { |
| 54 | destroy(); |
| 55 | return false; |
| 56 | } |
| 57 |
| 58 | RTC_CHECK(_module->CaptureStarted()); |
| 59 |
| 60 | return true; |
| 61 | } |
| 62 |
| 63 | void VideoCameraCapturer::setState(VideoState state) { |
| 64 | if (_state == state) { |
| 65 | return; |
| 66 | } |
| 67 | _state = state; |
| 68 | if (_state == VideoState::Inactive) { |
| 69 | _module->StopCapture(); |
| 70 | } else { |
| 71 | _module->StartCapture(_capability); |
| 72 | } |
| 73 | } |
| 74 |
| 75 | void VideoCameraCapturer::setPreferredCaptureAspectRatio(float aspectRatio) { |
| 76 | _aspectRatio = aspectRatio; |
| 77 | } |
| 78 |
| 79 | std::unique_ptr<VideoCameraCapturer> VideoCameraCapturer::Create( |
| 80 | size_t width, |
| 81 | size_t height, |
| 82 | size_t target_fps, |
| 83 | size_t capture_device_index) { |
| 84 | auto result = std::make_unique<VideoCameraCapturer>(CreateTag{}); |
| 85 | if (!result->init(width, height, target_fps, capture_device_index)) { |
| 86 | RTC_LOG(LS_WARNING) |
| 87 | << "Failed to create VideoCameraCapturer(" |
| 88 | << "w = " << width << ", " |
| 89 | << "h = " << height << ", " |
| 90 | << "fps = " << target_fps << ")"; |
| 91 | return nullptr; |
| 92 | } |
| 93 | return result; |
| 94 | } |
| 95 |
| 96 | void VideoCameraCapturer::destroy() { |
| 97 | if (!_module) { |
| 98 | return; |
| 99 | } |
| 100 |
| 101 | _module->StopCapture(); |
| 102 | _module->DeRegisterCaptureDataCallback(); |
| 103 | _module = nullptr; |
| 104 | } |
| 105 |
| 106 | void VideoCameraCapturer::OnFrame(const webrtc::VideoFrame &frame) { |
| 107 | if (_state != VideoState::Active) { |
| 108 | return; |
| 109 | } |
| 110 | //int cropped_width = 0; |
| 111 | //int cropped_height = 0; |
| 112 | //int out_width = 0; |
| 113 | //int out_height = 0; |
| 114 |
| 115 | //if (!_videoAdapter.AdaptFrameResolution( |
| 116 | // frame.width(), frame.height(), frame.timestamp_us() * 1000, |
| 117 | // &cropped_width, &cropped_height, &out_width, &out_height)) { |
| 118 | // // Drop frame in order to respect frame rate constraint. |
| 119 | // return; |
| 120 | //} |
| 121 | //if (out_height != frame.height() || out_width != frame.width()) { |
| 122 | // // Video adapter has requested a down-scale. Allocate a new buffer and |
| 123 | // // return scaled version. |
| 124 | // // For simplicity, only scale here without cropping. |
| 125 | // rtc::scoped_refptr<webrtc::I420Buffer> scaled_buffer = |
| 126 | // webrtc::I420Buffer::Create(out_width, out_height); |
| 127 | // scaled_buffer->ScaleFrom(*frame.video_frame_buffer()->ToI420()); |
| 128 | // webrtc::VideoFrame::Builder new_frame_builder = |
| 129 | // webrtc::VideoFrame::Builder() |
| 130 | // .set_video_frame_buffer(scaled_buffer) |
| 131 | // .set_rotation(webrtc::kVideoRotation_0) |
| 132 | // .set_timestamp_us(frame.timestamp_us()) |
| 133 | // .set_id(frame.id()); |
| 134 | // if (frame.has_update_rect()) { |
| 135 | // webrtc::VideoFrame::UpdateRect new_rect = frame.update_rect().ScaleWithFrame( |
| 136 | // frame.width(), frame.height(), 0, 0, frame.width(), frame.height(), |
| 137 | // out_width, out_height); |
| 138 | // new_frame_builder.set_update_rect(new_rect); |
| 139 | // } |
| 140 | // _broadcaster.OnFrame(new_frame_builder.build()); |
| 141 |
| 142 | //} else { |
| 143 | // // No adaptations needed, just return the frame as is. |
| 144 | // _broadcaster.OnFrame(frame); |
| 145 | //} |
| 146 |
| 147 | if (_aspectRatio <= 0.001) { |
| 148 | _broadcaster.OnFrame(frame); |
| 149 | return; |
| 150 | } |
| 151 | const auto originalWidth = frame.width(); |
| 152 | const auto originalHeight = frame.height(); |
| 153 | auto width = (originalWidth > _aspectRatio * originalHeight) |
| 154 | ? int(std::round(_aspectRatio * originalHeight)) |
| 155 | : originalWidth; |
| 156 | auto height = (originalWidth > _aspectRatio * originalHeight) |
| 157 | ? originalHeight |
| 158 | : int(std::round(originalHeight / _aspectRatio)); |
| 159 | if ((width >= originalWidth && height >= originalHeight) || !width || !height) { |
| 160 | _broadcaster.OnFrame(frame); |
| 161 | return; |
| 162 | } |
| 163 |
| 164 | width &= ~int(1); |
| 165 | height &= ~int(1); |
| 166 | const auto left = (originalWidth - width) / 2; |
| 167 | const auto top = (originalHeight - height) / 2; |
| 168 | rtc::scoped_refptr<webrtc::I420Buffer> croppedBuffer = |
| 169 | webrtc::I420Buffer::Create(width, height); |
| 170 | croppedBuffer->CropAndScaleFrom( |
| 171 | *frame.video_frame_buffer()->ToI420(), |
| 172 | left, |
| 173 | top, |
| 174 | width, |
| 175 | height); |
| 176 | webrtc::VideoFrame::Builder croppedBuilder = |
| 177 | webrtc::VideoFrame::Builder() |
| 178 | .set_video_frame_buffer(croppedBuffer) |
| 179 | .set_rotation(webrtc::kVideoRotation_0) |
| 180 | .set_timestamp_us(frame.timestamp_us()) |
| 181 | .set_id(frame.id()); |
| 182 | if (frame.has_update_rect()) { |
| 183 | croppedBuilder.set_update_rect(frame.update_rect().ScaleWithFrame( |
| 184 | frame.width(), |
| 185 | frame.height(), |
| 186 | left, |
| 187 | top, |
| 188 | width, |
| 189 | height, |
| 190 | width, |
| 191 | height)); |
| 192 | } |
| 193 | _broadcaster.OnFrame(croppedBuilder.build()); |
| 194 | } |
| 195 |
| 196 | void VideoCameraCapturer::AddOrUpdateSink( |
| 197 | rtc::VideoSinkInterface<webrtc::VideoFrame> *sink, |
| 198 | const rtc::VideoSinkWants &wants) { |
| 199 | _broadcaster.AddOrUpdateSink(sink, wants); |
| 200 | updateVideoAdapter(); |
| 201 | } |
| 202 |
| 203 | void VideoCameraCapturer::RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame> *sink) { |
| 204 | _broadcaster.RemoveSink(sink); |
| 205 | updateVideoAdapter(); |
| 206 | } |
| 207 |
| 208 | void VideoCameraCapturer::updateVideoAdapter() { |
| 209 | //_videoAdapter.OnSinkWants(_broadcaster.wants()); |
| 210 | } |
| 211 |
| 212 | } // namespace tgcalls |
| 213 |