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