Nagram/TMessagesProj/jni/voip/tgcalls/VideoCaptureInterfaceImpl.cpp

130 lines
4.2 KiB
C++
Raw Normal View History

2020-08-14 16:58:22 +00:00
#include "VideoCaptureInterfaceImpl.h"
#include "VideoCapturerInterface.h"
#include "Manager.h"
#include "MediaManager.h"
#include "platform/PlatformInterface.h"
namespace tgcalls {
VideoCaptureInterfaceObject::VideoCaptureInterfaceObject(std::shared_ptr<PlatformContext> platformContext) {
_videoSource = PlatformInterface::SharedInstance()->makeVideoSource(Manager::getMediaThread(), MediaManager::getWorkerThread());
_platformContext = platformContext;
//this should outlive the capturer
2020-08-15 21:06:36 +00:00
if (_videoSource) {
_videoCapturer = PlatformInterface::SharedInstance()->makeVideoCapturer(_videoSource, _useFrontCamera, [this](VideoState state) {
if (this->_stateUpdated) {
this->_stateUpdated(state);
}
2020-08-21 23:59:49 +00:00
}, _platformContext, _videoCapturerResolution);
2020-08-15 21:06:36 +00:00
}
2020-08-14 16:58:22 +00:00
}
VideoCaptureInterfaceObject::~VideoCaptureInterfaceObject() {
2020-08-15 21:06:36 +00:00
if (_videoCapturer && _currentUncroppedSink != nullptr) {
2020-08-14 16:58:22 +00:00
//_videoSource->RemoveSink(_currentSink.get());
_videoCapturer->setUncroppedOutput(nullptr);
}
}
void VideoCaptureInterfaceObject::switchCamera() {
_useFrontCamera = !_useFrontCamera;
if (_videoCapturer && _currentUncroppedSink) {
_videoCapturer->setUncroppedOutput(nullptr);
}
2020-08-15 21:06:36 +00:00
if (_videoSource) {
_videoCapturer = PlatformInterface::SharedInstance()->makeVideoCapturer(_videoSource, _useFrontCamera, [this](VideoState state) {
if (this->_stateUpdated) {
this->_stateUpdated(state);
}
}, _platformContext, _videoCapturerResolution);
}
if (_videoCapturer) {
if (_currentUncroppedSink) {
_videoCapturer->setUncroppedOutput(_currentUncroppedSink);
2020-08-14 16:58:22 +00:00
}
2020-08-15 21:06:36 +00:00
_videoCapturer->setState(_state);
}
2020-08-14 16:58:22 +00:00
}
void VideoCaptureInterfaceObject::setState(VideoState state) {
if (_state != state) {
_state = state;
2020-08-15 21:06:36 +00:00
if (_videoCapturer) {
_videoCapturer->setState(state);
}
2020-08-14 16:58:22 +00:00
}
}
void VideoCaptureInterfaceObject::setPreferredAspectRatio(float aspectRatio) {
2020-08-15 21:06:36 +00:00
if (_videoCapturer) {
if (aspectRatio > 0.01 && _videoCapturerResolution.first != 0 && _videoCapturerResolution.second != 0) {
float originalWidth = (float)_videoCapturerResolution.first;
float originalHeight = (float)_videoCapturerResolution.second;
2020-08-21 23:59:49 +00:00
2020-08-15 21:06:36 +00:00
float width = (originalWidth > aspectRatio * originalHeight)
? int(std::round(aspectRatio * originalHeight))
: originalWidth;
float height = (originalWidth > aspectRatio * originalHeight)
? originalHeight
: int(std::round(originalHeight / aspectRatio));
2020-08-21 23:59:49 +00:00
2020-08-15 21:06:36 +00:00
PlatformInterface::SharedInstance()->adaptVideoSource(_videoSource, (int)width, (int)height, 30);
}
}
2020-08-14 16:58:22 +00:00
}
void VideoCaptureInterfaceObject::setOutput(std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) {
2020-08-15 21:06:36 +00:00
if (_videoCapturer) {
_videoCapturer->setUncroppedOutput(sink);
}
2020-08-14 16:58:22 +00:00
_currentUncroppedSink = sink;
}
void VideoCaptureInterfaceObject::setStateUpdated(std::function<void(VideoState)> stateUpdated) {
_stateUpdated = stateUpdated;
}
VideoCaptureInterfaceImpl::VideoCaptureInterfaceImpl(std::shared_ptr<PlatformContext> platformContext) :
2020-08-21 23:59:49 +00:00
_platformContext(platformContext),
_impl(Manager::getMediaThread(), [platformContext]() {
2020-08-14 16:58:22 +00:00
return new VideoCaptureInterfaceObject(platformContext);
}) {
}
VideoCaptureInterfaceImpl::~VideoCaptureInterfaceImpl() = default;
void VideoCaptureInterfaceImpl::switchCamera() {
_impl.perform(RTC_FROM_HERE, [](VideoCaptureInterfaceObject *impl) {
impl->switchCamera();
});
}
void VideoCaptureInterfaceImpl::setState(VideoState state) {
_impl.perform(RTC_FROM_HERE, [state](VideoCaptureInterfaceObject *impl) {
impl->setState(state);
});
}
void VideoCaptureInterfaceImpl::setPreferredAspectRatio(float aspectRatio) {
_impl.perform(RTC_FROM_HERE, [aspectRatio](VideoCaptureInterfaceObject *impl) {
impl->setPreferredAspectRatio(aspectRatio);
});
}
void VideoCaptureInterfaceImpl::setOutput(std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) {
_impl.perform(RTC_FROM_HERE, [sink](VideoCaptureInterfaceObject *impl) {
impl->setOutput(sink);
});
}
2020-08-21 23:59:49 +00:00
std::shared_ptr<PlatformContext> VideoCaptureInterfaceImpl::getPlatformContext() {
return _platformContext;
}
2020-08-14 16:58:22 +00:00
ThreadLocalObject<VideoCaptureInterfaceObject> *VideoCaptureInterfaceImpl::object() {
return &_impl;
}
2020-08-21 23:59:49 +00:00
}// namespace tgcalls