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