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

230 lines
7.8 KiB
C++
Raw Normal View History

2020-10-01 01:59:32 +00:00
#include <tgnet/FileLog.h>
2020-08-14 16:58:22 +00:00
#include "VideoCaptureInterfaceImpl.h"
#include "VideoCapturerInterface.h"
#include "Manager.h"
#include "MediaManager.h"
#include "platform/PlatformInterface.h"
2021-03-19 10:25:58 +00:00
#include "StaticThreads.h"
2020-08-14 16:58:22 +00:00
namespace tgcalls {
2021-03-19 10:25:58 +00:00
VideoCaptureInterfaceObject::VideoCaptureInterfaceObject(std::string deviceId, std::shared_ptr<PlatformContext> platformContext, Threads &threads)
2021-06-25 00:43:10 +00:00
: _videoSource(PlatformInterface::SharedInstance()->makeVideoSource(threads.getMediaThread(), threads.getWorkerThread(), deviceId == "screen")) {
2020-08-14 16:58:22 +00:00
_platformContext = platformContext;
2020-10-01 01:59:32 +00:00
switchToDevice(deviceId);
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
_videoCapturer->setUncroppedOutput(nullptr);
}
}
2020-10-01 01:59:32 +00:00
webrtc::VideoTrackSourceInterface *VideoCaptureInterfaceObject::source() {
return _videoSource;
}
2021-06-25 00:43:10 +00:00
int VideoCaptureInterfaceObject::getRotation() {
if (_videoCapturer) {
return _videoCapturer->getRotation();
} else {
return 0;
}
}
2020-10-01 01:59:32 +00:00
void VideoCaptureInterfaceObject::switchToDevice(std::string deviceId) {
2021-06-25 00:43:10 +00:00
if (_videoCapturer && _currentUncroppedSink != nullptr) {
2020-08-14 16:58:22 +00:00
_videoCapturer->setUncroppedOutput(nullptr);
}
2020-08-15 21:06:36 +00:00
if (_videoSource) {
2020-10-01 01:59:32 +00:00
//this should outlive the capturer
2021-03-19 10:25:58 +00:00
_videoCapturer = NULL;
2020-10-01 01:59:32 +00:00
_videoCapturer = PlatformInterface::SharedInstance()->makeVideoCapturer(_videoSource, deviceId, [this](VideoState state) {
2020-08-15 21:06:36 +00:00
if (this->_stateUpdated) {
this->_stateUpdated(state);
}
2021-06-25 00:43:10 +00:00
if (this->_onIsActiveUpdated) {
switch (state) {
case VideoState::Active: {
this->_onIsActiveUpdated(true);
break;
}
default: {
this->_onIsActiveUpdated(false);
break;
}
}
}
2020-10-01 01:59:32 +00:00
}, [this](PlatformCaptureInfo info) {
if (this->_shouldBeAdaptedToReceiverAspectRate != info.shouldBeAdaptedToReceiverAspectRate) {
this->_shouldBeAdaptedToReceiverAspectRate = info.shouldBeAdaptedToReceiverAspectRate;
}
2021-06-25 00:43:10 +00:00
if (this->_rotationUpdated) {
this->_rotationUpdated(info.rotation);
}
this->updateAspectRateAdaptation();
2020-10-01 01:59:32 +00:00
}, _platformContext, _videoCapturerResolution);
2020-08-15 21:06:36 +00:00
}
if (_videoCapturer) {
2021-06-25 00:43:10 +00:00
// if (_preferredAspectRatio > 0) {
// _videoCapturer->setPreferredCaptureAspectRatio(_preferredAspectRatio);
// }
2020-08-15 21:06:36 +00:00
if (_currentUncroppedSink) {
_videoCapturer->setUncroppedOutput(_currentUncroppedSink);
2020-08-14 16:58:22 +00:00
}
2021-06-25 00:43:10 +00:00
if (_onFatalError) {
_videoCapturer->setOnFatalError(_onFatalError);
}
if (_onPause) {
_videoCapturer->setOnPause(_onPause);
}
2020-08-15 21:06:36 +00:00
_videoCapturer->setState(_state);
}
2020-08-14 16:58:22 +00:00
}
2021-06-25 00:43:10 +00:00
void VideoCaptureInterfaceObject::withNativeImplementation(std::function<void(void *)> completion) {
if (_videoCapturer) {
_videoCapturer->withNativeImplementation(completion);
} else {
completion(nullptr);
}
}
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-10-01 01:59:32 +00:00
_preferredAspectRatio = aspectRatio;
updateAspectRateAdaptation();
}
void VideoCaptureInterfaceObject::updateAspectRateAdaptation() {
if (_videoCapturer) {
if (_videoCapturerResolution.first != 0 && _videoCapturerResolution.second != 0) {
if (_preferredAspectRatio > 0.01 && _shouldBeAdaptedToReceiverAspectRate) {
float originalWidth = (float)_videoCapturerResolution.first;
float originalHeight = (float)_videoCapturerResolution.second;
float aspectRatio = _preferredAspectRatio;
float width = (originalWidth > aspectRatio * originalHeight)
? int(std::round(aspectRatio * originalHeight))
: originalWidth;
float height = (originalWidth > aspectRatio * originalHeight)
? originalHeight
: int(std::round(originalHeight / aspectRatio));
2021-06-25 00:43:10 +00:00
PlatformInterface::SharedInstance()->adaptVideoSource(_videoSource, (int)width, (int)height, 25);
2020-10-01 01:59:32 +00:00
} else {
2021-06-25 00:43:10 +00:00
PlatformInterface::SharedInstance()->adaptVideoSource(_videoSource, _videoCapturerResolution.first, _videoCapturerResolution.second, 25);
2020-10-01 01:59:32 +00:00
}
2020-08-15 21:06:36 +00:00
}
2020-10-01 01:59:32 +00:00
}
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;
}
2021-06-25 00:43:10 +00:00
void VideoCaptureInterfaceObject::setOnFatalError(std::function<void()> error) {
if (_videoCapturer) {
_videoCapturer->setOnFatalError(error);
}
_onFatalError = error;
}
void VideoCaptureInterfaceObject::setOnPause(std::function<void(bool)> pause) {
if (_videoCapturer) {
_videoCapturer->setOnPause(pause);
}
_onPause = pause;
}
void VideoCaptureInterfaceObject::setOnIsActiveUpdated(std::function<void(bool)> onIsActiveUpdated) {
_onIsActiveUpdated = onIsActiveUpdated;
}
2020-08-14 16:58:22 +00:00
void VideoCaptureInterfaceObject::setStateUpdated(std::function<void(VideoState)> stateUpdated) {
_stateUpdated = stateUpdated;
}
2021-06-25 00:43:10 +00:00
void VideoCaptureInterfaceObject::setRotationUpdated(std::function<void(int)> rotationUpdated) {
_rotationUpdated = rotationUpdated;
}
2021-03-19 10:25:58 +00:00
VideoCaptureInterfaceImpl::VideoCaptureInterfaceImpl(std::string deviceId,
std::shared_ptr<PlatformContext> platformContext, std::shared_ptr<Threads> threads) :
2020-10-01 01:59:32 +00:00
_platformContext(platformContext),
2021-03-19 10:25:58 +00:00
_impl(threads->getMediaThread(), [deviceId, platformContext, threads]() {
return new VideoCaptureInterfaceObject(deviceId, platformContext, *threads);
2020-08-14 16:58:22 +00:00
}) {
}
VideoCaptureInterfaceImpl::~VideoCaptureInterfaceImpl() = default;
2020-10-01 01:59:32 +00:00
void VideoCaptureInterfaceImpl::switchToDevice(std::string deviceId) {
_impl.perform(RTC_FROM_HERE, [deviceId](VideoCaptureInterfaceObject *impl) {
impl->switchToDevice(deviceId);
2020-08-14 16:58:22 +00:00
});
}
2021-06-25 00:43:10 +00:00
void VideoCaptureInterfaceImpl::withNativeImplementation(std::function<void(void *)> completion) {
_impl.perform(RTC_FROM_HERE, [completion](VideoCaptureInterfaceObject *impl) {
impl->withNativeImplementation(completion);
});
}
2020-08-14 16:58:22 +00:00
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);
});
}
2021-06-25 00:43:10 +00:00
void VideoCaptureInterfaceImpl::setOnFatalError(std::function<void()> error) {
_impl.perform(RTC_FROM_HERE, [error](VideoCaptureInterfaceObject *impl) {
impl->setOnFatalError(error);
});
}
void VideoCaptureInterfaceImpl::setOnPause(std::function<void(bool)> pause) {
_impl.perform(RTC_FROM_HERE, [pause](VideoCaptureInterfaceObject *impl) {
impl->setOnPause(pause);
});
}
void VideoCaptureInterfaceImpl::setOnIsActiveUpdated(std::function<void(bool)> onIsActiveUpdated) {
_impl.perform(RTC_FROM_HERE, [onIsActiveUpdated](VideoCaptureInterfaceObject *impl) {
impl->setOnIsActiveUpdated(onIsActiveUpdated);
});
}
2020-08-14 16:58:22 +00:00
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-10-01 01:59:32 +00:00
} // namespace tgcalls