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