| #include "MediaManager.h" |
| 6 | #include "platform/PlatformInterface.h" |
| 7 | #include "StaticThreads.h" |
| 8 |
| 9 | namespace tgcalls { |
| 10 |
| 11 | VideoCaptureInterfaceObject::VideoCaptureInterfaceObject(std::string deviceId, bool isScreenCapture, std::shared_ptr<PlatformContext> platformContext, Threads &threads) |
| 12 | : _videoSource(PlatformInterface::SharedInstance()->makeVideoSource(threads.getMediaThread(), threads.getWorkerThread(), isScreenCapture)), _platformContext(platformContext) { |
| 13 | switchToDevice(deviceId, isScreenCapture); |
| 14 | } |
| 15 |
| 16 | VideoCaptureInterfaceObject::~VideoCaptureInterfaceObject() { |
| 17 | if (_videoCapturer) { |
| 18 | _videoCapturer->setUncroppedOutput(nullptr); |
| 19 | } |
| 20 | } |
| 21 |
| 22 | webrtc::scoped_refptr<webrtc::VideoTrackSourceInterface> VideoCaptureInterfaceObject::source() { |
| 23 | return _videoSource; |
| 24 | } |
| 25 |
| 26 | int VideoCaptureInterfaceObject::getRotation() { |
| 27 | if (_videoCapturer) { |
| 28 | return _videoCapturer->getRotation(); |
| 29 | } else { |
| 30 | return 0; |
| 31 | } |
| 32 | } |
| 33 |
| 34 | bool VideoCaptureInterfaceObject::isScreenCapture() { |
| 35 | return _isScreenCapture; |
| 36 | } |
| 37 |
| 38 | void VideoCaptureInterfaceObject::switchToDevice(std::string deviceId, bool isScreenCapture) { |
| 39 | if (_videoCapturer) { |
| 40 | _videoCapturer->setUncroppedOutput(nullptr); |
| 41 | } |
| 42 | _isScreenCapture = isScreenCapture; |
| 43 | if (_videoSource) { |
| 44 | //this should outlive the capturer |
| 45 | _videoCapturer = nullptr; |
| 46 | _videoCapturer = PlatformInterface::SharedInstance()->makeVideoCapturer(_videoSource, deviceId, [this](VideoState state) { |
| 47 | if (this->_stateUpdated) { |
| 48 | this->_stateUpdated(state); |
| 49 | } |
| 50 | if (this->_onIsActiveUpdated) { |
| 51 | switch (state) { |
| 52 | case VideoState::Active: { |
| 53 | this->_onIsActiveUpdated(true); |
| 54 | break; |
| 55 | } |
| 56 | default: { |
| 57 | this->_onIsActiveUpdated(false); |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | }, [this](PlatformCaptureInfo info) { |
| 63 | if (this->_shouldBeAdaptedToReceiverAspectRate != info.shouldBeAdaptedToReceiverAspectRate) { |
| 64 | this->_shouldBeAdaptedToReceiverAspectRate = info.shouldBeAdaptedToReceiverAspectRate; |
| 65 | } |
| 66 | if (this->_rotationUpdated) { |
| 67 | this->_rotationUpdated(info.rotation); |
| 68 | } |
| 69 | this->updateAspectRateAdaptation(); |
| 70 | }, _platformContext, _videoCapturerResolution); |
| 71 | } |
| 72 | if (_videoCapturer) { |
| 73 | if (_preferredAspectRatio > 0) { |
| 74 | _videoCapturer->setPreferredCaptureAspectRatio(_preferredAspectRatio); |
| 75 | } |
| 76 | // if (const auto currentUncroppedSink = _currentUncroppedSink.lock()) { |
| 77 | _videoCapturer->setUncroppedOutput(_currentUncroppedSink); |
| 78 | // } |
| 79 | if (_onFatalError) { |
| 80 | _videoCapturer->setOnFatalError(_onFatalError); |
| 81 | } |
| 82 | if (_onPause) { |
| 83 | _videoCapturer->setOnPause(_onPause); |
| 84 | } |
| 85 | _videoCapturer->setState(_state); |
| 86 | } |
| 87 | } |
| 88 |
| 89 | void VideoCaptureInterfaceObject::withNativeImplementation(std::function<void(void *)> completion) { |
| 90 | if (_videoCapturer) { |
| 91 | _videoCapturer->withNativeImplementation(completion); |
| 92 | } else { |
| 93 | completion(nullptr); |
| 94 | } |
| 95 | } |
| 96 |
| 97 | void VideoCaptureInterfaceObject::setState(VideoState state) { |
| 98 | if (_state != state) { |
| 99 | _state = state; |
| 100 | if (_videoCapturer) { |
| 101 | _videoCapturer->setState(state); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 |
| 106 | void VideoCaptureInterfaceObject::setPreferredAspectRatio(float aspectRatio) { |
| 107 | _preferredAspectRatio = aspectRatio; |
| 108 | updateAspectRateAdaptation(); |
| 109 | } |
| 110 |
| 111 | void VideoCaptureInterfaceObject::updateAspectRateAdaptation() { |
| 112 | if (_videoCapturer) { |
| 113 | if (_videoCapturerResolution.first != 0 && _videoCapturerResolution.second != 0) { |
| 114 | if (_preferredAspectRatio > 0.01 && _shouldBeAdaptedToReceiverAspectRate) { |
| 115 | float originalWidth = (float)_videoCapturerResolution.first; |
| 116 | float originalHeight = (float)_videoCapturerResolution.second; |
| 117 |
| 118 | float aspectRatio = _preferredAspectRatio; |
| 119 |
| 120 | float width = (originalWidth > aspectRatio * originalHeight) |
| 121 | ? int(std::round(aspectRatio * originalHeight)) |
| 122 | : originalWidth; |
| 123 | float height = (originalWidth > aspectRatio * originalHeight) |
| 124 | ? originalHeight |
| 125 | : int(std::round(originalHeight / aspectRatio)); |
| 126 |
| 127 | PlatformInterface::SharedInstance()->adaptVideoSource(_videoSource, (int)width, (int)height, 25); |
| 128 | } else { |
| 129 | PlatformInterface::SharedInstance()->adaptVideoSource(_videoSource, _videoCapturerResolution.first, _videoCapturerResolution.second, 25); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 |
| 135 | void VideoCaptureInterfaceObject::setOutput(std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) { |
| 136 | if (_videoCapturer) { |
| 137 | _videoCapturer->setUncroppedOutput(sink); |
| 138 | } |
| 139 | _currentUncroppedSink = sink; |
| 140 | } |
| 141 |
| 142 | void VideoCaptureInterfaceObject::setOnFatalError(std::function<void()> error) { |
| 143 | if (_videoCapturer) { |
| 144 | _videoCapturer->setOnFatalError(error); |
| 145 | } |
| 146 | _onFatalError = error; |
| 147 | } |
| 148 | void VideoCaptureInterfaceObject::setOnPause(std::function<void(bool)> pause) { |
| 149 | if (_videoCapturer) { |
| 150 | _videoCapturer->setOnPause(pause); |
| 151 | } |
| 152 | _onPause = pause; |
| 153 | } |
| 154 |
| 155 | void VideoCaptureInterfaceObject::setOnIsActiveUpdated(std::function<void(bool)> onIsActiveUpdated) { |
| 156 | _onIsActiveUpdated = onIsActiveUpdated; |
| 157 | } |
| 158 |
| 159 | void VideoCaptureInterfaceObject::setStateUpdated(std::function<void(VideoState)> stateUpdated) { |
| 160 | _stateUpdated = stateUpdated; |
| 161 | } |
| 162 |
| 163 | void VideoCaptureInterfaceObject::setRotationUpdated(std::function<void(int)> rotationUpdated) { |
| 164 | _rotationUpdated = rotationUpdated; |
| 165 | } |
| 166 |
| 167 | VideoCaptureInterfaceImpl::VideoCaptureInterfaceImpl(std::string deviceId, bool isScreenCapture, std::shared_ptr<PlatformContext> platformContext, std::shared_ptr<Threads> threads) : |
| 168 | _platformContext(platformContext), |
| 169 | _impl(threads->getMediaThread(), [deviceId, isScreenCapture, platformContext, threads]() { |
| 170 | return std::make_shared<VideoCaptureInterfaceObject>(deviceId, isScreenCapture, platformContext, *threads); |
| 171 | }) { |
| 172 | } |
| 173 |
| 174 | VideoCaptureInterfaceImpl::~VideoCaptureInterfaceImpl() = default; |
| 175 |
| 176 | void VideoCaptureInterfaceImpl::switchToDevice(std::string deviceId, bool isScreenCapture) { |
| 177 | _impl.perform([deviceId, isScreenCapture](VideoCaptureInterfaceObject *impl) { |
| 178 | impl->switchToDevice(deviceId, isScreenCapture); |
| 179 | }); |
| 180 | } |
| 181 |
| 182 | void VideoCaptureInterfaceImpl::withNativeImplementation(std::function<void(void *)> completion) { |
| 183 | _impl.perform([completion](VideoCaptureInterfaceObject *impl) { |
| 184 | impl->withNativeImplementation(completion); |
| 185 | }); |
| 186 | } |
| 187 |
| 188 | void VideoCaptureInterfaceImpl::setState(VideoState state) { |
| 189 | _impl.perform([state](VideoCaptureInterfaceObject *impl) { |
| 190 | impl->setState(state); |
| 191 | }); |
| 192 | } |
| 193 |
| 194 | void VideoCaptureInterfaceImpl::setPreferredAspectRatio(float aspectRatio) { |
| 195 | _impl.perform([aspectRatio](VideoCaptureInterfaceObject *impl) { |
| 196 | impl->setPreferredAspectRatio(aspectRatio); |
| 197 | }); |
| 198 | } |
| 199 | void VideoCaptureInterfaceImpl::setOnFatalError(std::function<void()> error) { |
| 200 | _impl.perform([error](VideoCaptureInterfaceObject *impl) { |
| 201 | impl->setOnFatalError(error); |
| 202 | }); |
| 203 | } |
| 204 | void VideoCaptureInterfaceImpl::setOnPause(std::function<void(bool)> pause) { |
| 205 | _impl.perform([pause](VideoCaptureInterfaceObject *impl) { |
| 206 | impl->setOnPause(pause); |
| 207 | }); |
| 208 | } |
| 209 |
| 210 | void VideoCaptureInterfaceImpl::setOnIsActiveUpdated(std::function<void(bool)> onIsActiveUpdated) { |
| 211 | _impl.perform([onIsActiveUpdated](VideoCaptureInterfaceObject *impl) { |
| 212 | impl->setOnIsActiveUpdated(onIsActiveUpdated); |
| 213 | }); |
| 214 | } |
| 215 |
| 216 | void VideoCaptureInterfaceImpl::setOutput(std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) { |
| 217 | _impl.perform([sink](VideoCaptureInterfaceObject *impl) { |
| 218 | impl->setOutput(sink); |
| 219 | }); |
| 220 | } |
| 221 |
| 222 | std::shared_ptr<PlatformContext> VideoCaptureInterfaceImpl::getPlatformContext() { |
| 223 | return _platformContext; |
| 224 | } |
| 225 |
| 226 | ThreadLocalObject<VideoCaptureInterfaceObject> *VideoCaptureInterfaceImpl::object() { |
| 227 | return &_impl; |
| 228 | } |
| 229 |
| 230 | } // namespace tgcalls |
| 231 |