From 4d2041ae48b4f7cf9149cd2e5da7101408f99d94 Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 21 Aug 2020 15:49:51 +0400 Subject: [PATCH] Share video capturer between call and settings. --- Telegram/SourceFiles/calls/calls_call.cpp | 3 +- Telegram/SourceFiles/calls/calls_call.h | 4 ++- Telegram/SourceFiles/calls/calls_instance.cpp | 12 +++++++ Telegram/SourceFiles/calls/calls_instance.h | 2 ++ .../SourceFiles/settings/settings_calls.cpp | 31 ++++++++++++++----- 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/Telegram/SourceFiles/calls/calls_call.cpp b/Telegram/SourceFiles/calls/calls_call.cpp index a7b019363..096f9d4a9 100644 --- a/Telegram/SourceFiles/calls/calls_call.cpp +++ b/Telegram/SourceFiles/calls/calls_call.cpp @@ -373,8 +373,7 @@ void Call::setupOutgoingVideo() { #ifndef DESKTOP_APP_DISABLE_WEBRTC_INTEGRATION Assert(state == Webrtc::VideoState::Active); if (!_videoCapture) { - _videoCapture = tgcalls::VideoCaptureInterface::Create( - Core::App().settings().callVideoInputDeviceId().toStdString()); + _videoCapture = _delegate->getVideoCapture(); _videoCapture->setOutput(_videoOutgoing->sink()); } if (_instance) { diff --git a/Telegram/SourceFiles/calls/calls_call.h b/Telegram/SourceFiles/calls/calls_call.h index a8c42f24e..139a037ad 100644 --- a/Telegram/SourceFiles/calls/calls_call.h +++ b/Telegram/SourceFiles/calls/calls_call.h @@ -68,7 +68,9 @@ public: Ended, }; virtual void playSound(Sound sound) = 0; - virtual void requestPermissionsOrFail(Fn result) = 0; + virtual void requestPermissionsOrFail(Fn onSuccess) = 0; + virtual auto getVideoCapture() + -> std::shared_ptr = 0; virtual ~Delegate() = default; diff --git a/Telegram/SourceFiles/calls/calls_instance.cpp b/Telegram/SourceFiles/calls/calls_instance.cpp index 90c938b0c..e99958109 100644 --- a/Telegram/SourceFiles/calls/calls_instance.cpp +++ b/Telegram/SourceFiles/calls/calls_instance.cpp @@ -24,6 +24,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mainwidget.h" #include "mtproto/mtproto_config.h" #include "boxes/rate_call_box.h" +#include "tgcalls/VideoCaptureInterface.h" #include "app.h" namespace Calls { @@ -344,4 +345,15 @@ void Instance::requestPermissionOrFail(Platform::PermissionType type, Fn } } +std::shared_ptr Instance::getVideoCapture() { + if (auto result = _videoCapture.lock()) { + return result; + } + auto result = std::shared_ptr( + tgcalls::VideoCaptureInterface::Create( + Core::App().settings().callVideoInputDeviceId().toStdString())); + _videoCapture = result; + return result; +} + } // namespace Calls diff --git a/Telegram/SourceFiles/calls/calls_instance.h b/Telegram/SourceFiles/calls/calls_instance.h index c983e1476..a50f99343 100644 --- a/Telegram/SourceFiles/calls/calls_instance.h +++ b/Telegram/SourceFiles/calls/calls_instance.h @@ -43,6 +43,7 @@ public: void showInfoPanel(not_null call); [[nodiscard]] Call *currentCall() const; [[nodiscard]] rpl::producer currentCallValue() const; + std::shared_ptr getVideoCapture() override; [[nodiscard]] bool isQuitPrevent(); @@ -78,6 +79,7 @@ private: crl::time _lastServerConfigUpdateTime = 0; base::weak_ptr _serverConfigRequestSession; + std::weak_ptr _videoCapture; std::unique_ptr _currentCall; rpl::event_stream _currentCallChanges; diff --git a/Telegram/SourceFiles/settings/settings_calls.cpp b/Telegram/SourceFiles/settings/settings_calls.cpp index 594e30436..7a8e7d3d2 100644 --- a/Telegram/SourceFiles/settings/settings_calls.cpp +++ b/Telegram/SourceFiles/settings/settings_calls.cpp @@ -99,14 +99,16 @@ void Calls::setupContent() { const auto cameras = Webrtc::GetVideoInputList(); if (!cameras.empty()) { - auto capturerOwner = tgcalls::VideoCaptureInterface::Create( - settings.callVideoInputDeviceId().toStdString()); + const auto hasCall = (Core::App().calls().currentCall() != nullptr); + + auto capturerOwner = Core::App().calls().getVideoCapture(); const auto capturer = capturerOwner.get(); content->lifetime().add([owner = std::move(capturerOwner)]{}); const auto track = content->lifetime().make_state( - Webrtc::VideoState::Active); - capturer->setOutput(track->sink()); + (hasCall + ? Webrtc::VideoState::Inactive + : Webrtc::VideoState::Active)); const auto currentCameraName = [&] { const auto i = ranges::find( @@ -181,17 +183,30 @@ void Calls::setupContent() { track->renderNextFrame( ) | rpl::start_with_next([=] { const auto size = track->frameSize(); - if (size.isEmpty()) { + if (size.isEmpty() || Core::App().calls().currentCall()) { return; } const auto width = bubbleWrap->width(); const auto use = (width - 2 * padding); - bubbleWrap->resize( - width, - top + ((use * size.height()) / size.width()) + bottom); + const auto height = std::min( + ((use * size.height()) / size.width()), + (use * 480) / 640); + bubbleWrap->resize(width, top + height + bottom); bubbleWrap->update(); }, bubbleWrap->lifetime()); + Core::App().calls().currentCallValue( + ) | rpl::start_with_next([=](::Calls::Call *value) { + if (value) { + track->setState(Webrtc::VideoState::Inactive); + bubbleWrap->resize(bubbleWrap->width(), 0); + } else { + capturer->setPreferredAspectRatio(0.); + track->setState(Webrtc::VideoState::Active); + capturer->setOutput(track->sink()); + } + }, content->lifetime()); + AddSkip(content); AddDivider(content); }