Debian Codegraph @

src/voip/CallManager.h

raw symbols SCIP
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #pragma once
6
7 #include <string>
8 #include <vector>
9
10 #include <QMediaPlayer>
11 #include <QObject>
12 #include <QQmlEngine>
13 #include <QString>
14 #include <QStringList>
15 #include <QTimer>
16
17 #include "CallDevices.h"
18 #include "WebRTCSession.h"
19 #include "mtx/events/collections.hpp"
20 #include "mtx/events/voip.hpp"
21 #include "voip/ScreenCastPortal.h"
22 #include <mtxclient/utils.hpp>
23
24 namespace mtx::responses {
25 struct TurnServer;
26 }
27
28 class QUrl;
29
30 class CallManager : public QObject
31 {
32 Q_OBJECT
33
34 QML_ELEMENT
35 QML_SINGLETON
36
37 Q_PROPERTY(bool haveCallInvite READ haveCallInvite NOTIFY newInviteState)
38 Q_PROPERTY(bool isOnCall READ isOnCall NOTIFY newCallState)
39 Q_PROPERTY(bool isOnCallOnOtherDevice READ isOnCallOnOtherDevice NOTIFY newCallDeviceState)
40 Q_PROPERTY(webrtc::CallType callType READ callType NOTIFY newInviteState)
41 Q_PROPERTY(
42 webrtc::ScreenShareType screenShareType READ screenShareType NOTIFY screenShareChanged)
43 Q_PROPERTY(webrtc::State callState READ callState NOTIFY newCallState)
44 Q_PROPERTY(QString callParty READ callParty NOTIFY newInviteState)
45 Q_PROPERTY(QString callPartyDisplayName READ callPartyDisplayName NOTIFY newInviteState)
46 Q_PROPERTY(QString callPartyAvatarUrl READ callPartyAvatarUrl NOTIFY newInviteState)
47 Q_PROPERTY(bool isMicMuted READ isMicMuted NOTIFY micMuteChanged)
48 Q_PROPERTY(bool haveLocalPiP READ haveLocalPiP NOTIFY newCallState)
49 Q_PROPERTY(QStringList mics READ mics NOTIFY devicesChanged)
50 Q_PROPERTY(QStringList cameras READ cameras NOTIFY devicesChanged)
51 Q_PROPERTY(bool callsSupported READ callsSupported CONSTANT)
52 Q_PROPERTY(bool screenShareReady READ screenShareReady NOTIFY screenShareChanged)
53
54 public:
55 CallManager(QObject *);
56
57 static CallManager *create(QQmlEngine *qmlEngine, QJSEngine *);
58
59 bool haveCallInvite() const { return haveCallInvite_; }
60 bool isOnCall() const { return (session_.state() != webrtc::State::DISCONNECTED); }
61 bool isOnCallOnOtherDevice() const { return (isOnCallOnOtherDevice_ != ""); }
62 bool checkSharesRoom(QString roomid_, std::string invitee) const;
63 webrtc::CallType callType() const { return callType_; }
64 webrtc::ScreenShareType screenShareType() const { return screenShareType_; }
65 webrtc::State callState() const { return session_.state(); }
66 QString callParty() const { return callParty_; }
67 QString callPartyDisplayName() const { return callPartyDisplayName_; }
68 QString callPartyAvatarUrl() const { return callPartyAvatarUrl_; }
69 bool isMicMuted() const { return session_.isMicMuted(); }
70 bool haveLocalPiP() const { return session_.haveLocalPiP(); }
71 QStringList mics() const { return devices(false); }
72 QStringList cameras() const { return devices(true); }
73 void refreshTurnServer();
74 bool screenShareReady() const;
75
76 static bool callsSupported();
77
78 public slots:
79 void sendInvite(const QString &roomid, webrtc::CallType, unsigned int windowIndex = 0);
80 void syncEvent(const mtx::events::collections::TimelineEvents &event);
81 void toggleMicMute();
82 void toggleLocalPiP() { session_.toggleLocalPiP(); }
83 void acceptInvite();
84 void hangUp(
85 mtx::events::voip::CallHangUp::Reason = mtx::events::voip::CallHangUp::Reason::UserHangUp);
86 void rejectInvite();
87 void setupScreenShareXDP();
88 void setScreenShareType(unsigned int index);
89 void closeScreenShare();
90 QStringList screenShareTypeList();
91 QStringList windowList();
92 void previewWindow(unsigned int windowIndex) const;
93
94 signals:
95 void newMessage(const QString &roomid, const mtx::events::voip::CallInvite &);
96 void newMessage(const QString &roomid, const mtx::events::voip::CallCandidates &);
97 void newMessage(const QString &roomid, const mtx::events::voip::CallAnswer &);
98 void newMessage(const QString &roomid, const mtx::events::voip::CallHangUp &);
99 void newMessage(const QString &roomid, const mtx::events::voip::CallSelectAnswer &);
100 void newMessage(const QString &roomid, const mtx::events::voip::CallReject &);
101 void newMessage(const QString &roomid, const mtx::events::voip::CallNegotiate &);
102 void newInviteState();
103 void newCallState();
104 void newCallDeviceState();
105 void micMuteChanged();
106 void devicesChanged();
107 void turnServerRetrieved(const mtx::responses::TurnServer &);
108 void screenShareChanged();
109
110 private slots:
111 void retrieveTurnServer();
112
113 private:
114 WebRTCSession &session_;
115 QString roomid_;
116 QString callParty_;
117 QString callPartyDisplayName_;
118 QString callPartyAvatarUrl_;
119 std::string callPartyVersion_ = "1";
120 std::string callid_;
121 std::string partyid_ = mtx::client::utils::random_token(8, false);
122 std::string selectedpartyid_ = "";
123 std::string invitee_ = "";
124 const uint32_t timeoutms_ = 120000;
125 webrtc::CallType callType_ = webrtc::CallType::VOICE;
126 webrtc::ScreenShareType screenShareType_ = webrtc::ScreenShareType::X11;
127 bool haveCallInvite_ = false;
128 bool answerSelected_ = false;
129 std::string isOnCallOnOtherDevice_ = "";
130 std::string inviteSDP_;
131 std::vector<mtx::events::voip::CallCandidates::Candidate> remoteICECandidates_;
132 std::vector<std::string> turnURIs_;
133 QTimer turnServerTimer_;
134 QMediaPlayer player_;
135 std::vector<webrtc::ScreenShareType> screenShareTypes_;
136 #ifndef Q_OS_WINDOWS
137 std::vector<std::pair<QString, uint32_t>> windows_;
138 #else
139 std::vector<std::pair<QString, uint64_t>> windows_;
140 #endif
141 std::vector<std::string> rejectCallPartyIDs_;
142
143 template<typename T>
144 bool handleEvent(const mtx::events::collections::TimelineEvents &event);
145 void handleEvent(const mtx::events::RoomEvent<mtx::events::voip::CallInvite> &);
146 void handleEvent(const mtx::events::RoomEvent<mtx::events::voip::CallCandidates> &);
147 void handleEvent(const mtx::events::RoomEvent<mtx::events::voip::CallAnswer> &);
148 void handleEvent(const mtx::events::RoomEvent<mtx::events::voip::CallHangUp> &);
149 void handleEvent(const mtx::events::RoomEvent<mtx::events::voip::CallSelectAnswer> &);
150 void handleEvent(const mtx::events::RoomEvent<mtx::events::voip::CallReject> &);
151 void handleEvent(const mtx::events::RoomEvent<mtx::events::voip::CallNegotiate> &);
152 void answerInvite(const mtx::events::voip::CallInvite &, bool isVideo);
153 void generateCallID();
154 QStringList devices(bool isVideo) const;
155 void clear(bool endAllCalls = true);
156 void endCall(bool endAllCalls = true);
157 void playRingtone(const QUrl &ringtone, bool repeat);
158 void stopRingtone();
159 };