| #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, std::shared_ptr<PlatformContext> platformContext, Threads &threads) |
| 13 | : _videoSource(PlatformInterface::SharedInstance()->makeVideoSource(threads.getMediaThread(), threads.getWorkerThread())) { |
| 14 | _platformContext = platformContext; |
| 15 |
| 16 | switchToDevice(deviceId); |
| 17 | } |
| 18 |
| 19 | VideoCaptureInterfaceObject::~VideoCaptureInterfaceObject() { |
| 20 | if (_videoCapturer && _currentUncroppedSink != nullptr) { |
| 21 | _videoCapturer->setUncroppedOutput(nullptr); |
| 22 | } |
| 23 | } |
| 24 |
| 25 | webrtc::VideoTrackSourceInterface *VideoCaptureInterfaceObject::source() { |
| 26 | return _videoSource; |
| 27 | } |
| 28 |
| 29 | void VideoCaptureInterfaceObject::switchToDevice(std::string deviceId) { |
| 30 | if (_videoCapturer && _currentUncroppedSink) { |
| 31 | _videoCapturer->setUncroppedOutput(nullptr); |
| 32 | } |
| 33 | if (_videoSource) { |
| 34 | //this should outlive the capturer |
| 35 | _videoCapturer = NULL; |
| 36 | _videoCapturer = PlatformInterface::SharedInstance()->makeVideoCapturer(_videoSource, deviceId, [this](VideoState state) { |
| 37 | if (this->_stateUpdated) { |
| 38 | this->_stateUpdated(state); |
| 39 | } |
| 40 | }, [this](PlatformCaptureInfo info) { |
| 41 | if (this->_shouldBeAdaptedToReceiverAspectRate != info.shouldBeAdaptedToReceiverAspectRate) { |
| 42 | this->_shouldBeAdaptedToReceiverAspectRate = info.shouldBeAdaptedToReceiverAspectRate; |
| 43 | this->updateAspectRateAdaptation(); |
| 44 | } |
| 45 | }, _platformContext, _videoCapturerResolution); |
| 46 | } |
| 47 | if (_videoCapturer) { |
| 48 | // if (_preferredAspectRatio > 0) { |
| 49 | // _videoCapturer->setPreferredCaptureAspectRatio(_preferredAspectRatio); |
| 50 | // } |
| 51 | if (_currentUncroppedSink) { |
| 52 | _videoCapturer->setUncroppedOutput(_currentUncroppedSink); |
| 53 | } |
| 54 | _videoCapturer->setState(_state); |
| 55 | } |
| 56 | } |
| 57 |
| 58 | void VideoCaptureInterfaceObject::setState(VideoState state) { |
| 59 | if (_state != state) { |
| 60 | _state = state; |
| 61 | if (_videoCapturer) { |
| 62 | _videoCapturer->setState(state); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 |
| 67 | void VideoCaptureInterfaceObject::setPreferredAspectRatio(float aspectRatio) { |
| 68 | _preferredAspectRatio = aspectRatio; |
| 69 | updateAspectRateAdaptation(); |
| 70 | } |
| 71 |
| 72 | void VideoCaptureInterfaceObject::updateAspectRateAdaptation() { |
| 73 | if (_videoCapturer) { |
| 74 | if (_videoCapturerResolution.first != 0 && _videoCapturerResolution.second != 0) { |
| 75 | if (_preferredAspectRatio > 0.01 && _shouldBeAdaptedToReceiverAspectRate) { |
| 76 | float originalWidth = (float)_videoCapturerResolution.first; |
| 77 | float originalHeight = (float)_videoCapturerResolution.second; |
| 78 |
| 79 | float aspectRatio = _preferredAspectRatio; |
| 80 |
| 81 | float width = (originalWidth > aspectRatio * originalHeight) |
| 82 | ? int(std::round(aspectRatio * originalHeight)) |
| 83 | : originalWidth; |
| 84 | float height = (originalWidth > aspectRatio * originalHeight) |
| 85 | ? originalHeight |
| 86 | : int(std::round(originalHeight / aspectRatio)); |
| 87 |
| 88 | PlatformInterface::SharedInstance()->adaptVideoSource(_videoSource, (int)width, (int)height, 30); |
| 89 | } else { |
| 90 | PlatformInterface::SharedInstance()->adaptVideoSource(_videoSource, _videoCapturerResolution.first, _videoCapturerResolution.second, 30); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 |
| 96 | void VideoCaptureInterfaceObject::setOutput(std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) { |
| 97 | if (_videoCapturer) { |
| 98 | _videoCapturer->setUncroppedOutput(sink); |
| 99 | } |
| 100 | _currentUncroppedSink = sink; |
| 101 | } |
| 102 |
| 103 | void VideoCaptureInterfaceObject::setStateUpdated(std::function<void(VideoState)> stateUpdated) { |
| 104 | _stateUpdated = stateUpdated; |
| 105 | } |
| 106 |
| 107 | VideoCaptureInterfaceImpl::VideoCaptureInterfaceImpl(std::string deviceId, |
| 108 | std::shared_ptr<PlatformContext> platformContext, std::shared_ptr<Threads> threads) : |
| 109 | _platformContext(platformContext), |
| 110 | _impl(threads->getMediaThread(), [deviceId, platformContext, threads]() { |
| 111 | return new VideoCaptureInterfaceObject(deviceId, platformContext, *threads); |
| 112 | }) { |
| 113 | } |
| 114 |
| 115 | VideoCaptureInterfaceImpl::~VideoCaptureInterfaceImpl() = default; |
| 116 |
| 117 | void VideoCaptureInterfaceImpl::switchToDevice(std::string deviceId) { |
| 118 | _impl.perform(RTC_FROM_HERE, [deviceId](VideoCaptureInterfaceObject *impl) { |
| 119 | impl->switchToDevice(deviceId); |
| 120 | }); |
| 121 | } |
| 122 |
| 123 | void VideoCaptureInterfaceImpl::setState(VideoState state) { |
| 124 | _impl.perform(RTC_FROM_HERE, [state](VideoCaptureInterfaceObject *impl) { |
| 125 | impl->setState(state); |
| 126 | }); |
| 127 | } |
| 128 |
| 129 | void VideoCaptureInterfaceImpl::setPreferredAspectRatio(float aspectRatio) { |
| 130 | _impl.perform(RTC_FROM_HERE, [aspectRatio](VideoCaptureInterfaceObject *impl) { |
| 131 | impl->setPreferredAspectRatio(aspectRatio); |
| 132 | }); |
| 133 | } |
| 134 |
| 135 | void VideoCaptureInterfaceImpl::setOutput(std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) { |
| 136 | _impl.perform(RTC_FROM_HERE, [sink](VideoCaptureInterfaceObject *impl) { |
| 137 | impl->setOutput(sink); |
| 138 | }); |
| 139 | } |
| 140 |
| 141 | std::shared_ptr<PlatformContext> VideoCaptureInterfaceImpl::getPlatformContext() { |
| 142 | return _platformContext; |
| 143 | } |
| 144 |
| 145 | ThreadLocalObject<VideoCaptureInterfaceObject> *VideoCaptureInterfaceImpl::object() { |
| 146 | return &_impl; |
| 147 | } |
| 148 |
| 149 | } // namespace tgcalls |
| 150 |