summary refs log tree commit diff
path: root/src/CallManager.cpp
diff options
context:
space:
mode:
authortrilene <trilene@runbox.com>2020-12-17 11:25:32 -0500
committerNicolas Werner <nicolas.werner@hotmail.de>2021-01-07 14:04:21 +0100
commit7124024977ccb237547b88b6a96a50ac6838c354 (patch)
treef5a6ca7b69444413b3c806667e72584d40e36689 /src/CallManager.cpp
parentMove call-related properties to CallManager (diff)
downloadnheko-7124024977ccb237547b88b6a96a50ac6838c354.tar.xz
Make call invites less intrusive
Diffstat (limited to 'src/CallManager.cpp')
-rw-r--r--src/CallManager.cpp100
1 files changed, 62 insertions, 38 deletions
diff --git a/src/CallManager.cpp b/src/CallManager.cpp
index cb523bc2..9864b203 100644
--- a/src/CallManager.cpp
+++ b/src/CallManager.cpp
@@ -10,10 +10,9 @@
 #include "CallManager.h"
 #include "ChatPage.h"
 #include "Logging.h"
-#include "MainWindow.h"
 #include "MatrixClient.h"
+#include "UserSettingsPage.h"
 #include "Utils.h"
-#include "dialogs/AcceptCall.h"
 
 #include "mtx/responses/turn_server.hpp"
 
@@ -114,6 +113,22 @@ CallManager::CallManager(QObject *parent)
                 emit newCallState();
         });
 
+        connect(&session_, &WebRTCSession::devicesChanged, this, [this]() {
+                if (ChatPage::instance()->userSettings()->microphone().isEmpty()) {
+                        auto mics = session_.getDeviceNames(false, std::string());
+                        if (!mics.empty())
+                                ChatPage::instance()->userSettings()->setMicrophone(
+                                  QString::fromStdString(mics.front()));
+                }
+                if (ChatPage::instance()->userSettings()->camera().isEmpty()) {
+                        auto cameras = session_.getDeviceNames(true, std::string());
+                        if (!cameras.empty())
+                                ChatPage::instance()->userSettings()->setCamera(
+                                  QString::fromStdString(cameras.front()));
+                }
+                emit devicesChanged();
+        });
+
         connect(&player_,
                 &QMediaPlayer::mediaStatusChanged,
                 this,
@@ -160,7 +175,8 @@ CallManager::sendInvite(const QString &roomid, bool isVideo)
                 return;
         }
 
-        roomid_ = roomid;
+        isVideo_ = isVideo;
+        roomid_  = roomid;
         session_.setTurnServers(turnURIs_);
         generateCallID();
         nhlog::ui()->debug(
@@ -168,16 +184,14 @@ CallManager::sendInvite(const QString &roomid, bool isVideo)
         std::vector<RoomMember> members(cache::getMembers(roomid.toStdString()));
         const RoomMember &callee =
           members.front().user_id == utils::localUser() ? members.back() : members.front();
-        callPartyName_      = callee.display_name.isEmpty() ? callee.user_id : callee.display_name;
+        callParty_          = callee.display_name.isEmpty() ? callee.user_id : callee.display_name;
         callPartyAvatarUrl_ = QString::fromStdString(roomInfo.avatar_url);
-        emit newCallParty();
+        emit newInviteState();
         playRingtone(QUrl("qrc:/media/media/ringback.ogg"), true);
         if (!session_.createOffer(isVideo)) {
                 emit ChatPage::instance()->showNotification("Problem setting up call.");
                 endCall();
         }
-        if (isVideo)
-                emit newVideoCallState();
 }
 
 namespace {
@@ -271,48 +285,41 @@ CallManager::handleEvent(const RoomEvent<CallInvite> &callInviteEvent)
         std::vector<RoomMember> members(cache::getMembers(callInviteEvent.room_id));
         const RoomMember &caller =
           members.front().user_id == utils::localUser() ? members.back() : members.front();
-        callPartyName_      = caller.display_name.isEmpty() ? caller.user_id : caller.display_name;
+        callParty_          = caller.display_name.isEmpty() ? caller.user_id : caller.display_name;
         callPartyAvatarUrl_ = QString::fromStdString(roomInfo.avatar_url);
-        emit newCallParty();
-        auto dialog = new dialogs::AcceptCall(caller.user_id,
-                                              caller.display_name,
-                                              QString::fromStdString(roomInfo.name),
-                                              QString::fromStdString(roomInfo.avatar_url),
-                                              isVideo,
-                                              MainWindow::instance());
-        connect(dialog, &dialogs::AcceptCall::accept, this, [this, callInviteEvent, isVideo]() {
-                MainWindow::instance()->hideOverlay();
-                answerInvite(callInviteEvent.content, isVideo);
-        });
-        connect(dialog, &dialogs::AcceptCall::reject, this, [this]() {
-                MainWindow::instance()->hideOverlay();
-                hangUp();
-        });
-        MainWindow::instance()->showSolidOverlayModal(dialog);
+
+        haveCallInvite_ = true;
+        isVideo_        = isVideo;
+        inviteSDP_      = callInviteEvent.content.sdp;
+        session_.refreshDevices();
+        emit newInviteState();
 }
 
 void
-CallManager::answerInvite(const CallInvite &invite, bool isVideo)
+CallManager::acceptInvite()
 {
+        if (!haveCallInvite_)
+                return;
+
         stopRingtone();
         std::string errorMessage;
         if (!session_.havePlugins(false, &errorMessage) ||
-            (isVideo && !session_.havePlugins(true, &errorMessage))) {
+            (isVideo_ && !session_.havePlugins(true, &errorMessage))) {
                 emit ChatPage::instance()->showNotification(QString::fromStdString(errorMessage));
                 hangUp();
                 return;
         }
 
         session_.setTurnServers(turnURIs_);
-        if (!session_.acceptOffer(invite.sdp)) {
+        if (!session_.acceptOffer(inviteSDP_)) {
                 emit ChatPage::instance()->showNotification("Problem setting up call.");
                 hangUp();
                 return;
         }
         session_.acceptICECandidates(remoteICECandidates_);
         remoteICECandidates_.clear();
-        if (isVideo)
-                emit newVideoCallState();
+        haveCallInvite_ = false;
+        emit newInviteState();
 }
 
 void
@@ -348,7 +355,8 @@ CallManager::handleEvent(const RoomEvent<CallAnswer> &callAnswerEvent)
             callid_ == callAnswerEvent.content.call_id) {
                 emit ChatPage::instance()->showNotification("Call answered on another device.");
                 stopRingtone();
-                MainWindow::instance()->hideOverlay();
+                haveCallInvite_ = false;
+                emit newInviteState();
                 return;
         }
 
@@ -369,10 +377,8 @@ CallManager::handleEvent(const RoomEvent<CallHangUp> &callHangUpEvent)
                            callHangUpReasonString(callHangUpEvent.content.reason),
                            callHangUpEvent.sender);
 
-        if (callid_ == callHangUpEvent.content.call_id) {
-                MainWindow::instance()->hideOverlay();
+        if (callid_ == callHangUpEvent.content.call_id)
                 endCall();
-        }
 }
 
 void
@@ -392,6 +398,23 @@ CallManager::callsSupported() const
 #endif
 }
 
+QStringList
+CallManager::devices(bool isVideo) const
+{
+        QStringList ret;
+        const QString &defaultDevice = isVideo ? ChatPage::instance()->userSettings()->camera()
+                                               : ChatPage::instance()->userSettings()->microphone();
+        std::vector<std::string> devices =
+          session_.getDeviceNames(isVideo, defaultDevice.toStdString());
+        ret.reserve(devices.size());
+        std::transform(devices.cbegin(),
+                       devices.cend(),
+                       std::back_inserter(ret),
+                       [](const auto &d) { return QString::fromStdString(d); });
+
+        return ret;
+}
+
 void
 CallManager::generateCallID()
 {
@@ -404,9 +427,13 @@ void
 CallManager::clear()
 {
         roomid_.clear();
-        callPartyName_.clear();
+        callParty_.clear();
         callPartyAvatarUrl_.clear();
         callid_.clear();
+        isVideo_        = false;
+        haveCallInvite_ = false;
+        emit newInviteState();
+        inviteSDP_.clear();
         remoteICECandidates_.clear();
 }
 
@@ -414,11 +441,8 @@ void
 CallManager::endCall()
 {
         stopRingtone();
-        clear();
-        bool isVideo = session_.isVideo();
         session_.end();
-        if (isVideo)
-                emit newVideoCallState();
+        clear();
 }
 
 void