2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
2022-01-01 06:57:53 +03:00
|
|
|
// SPDX-FileCopyrightText: 2022 Nheko Contributors
|
2023-01-02 06:25:33 +03:00
|
|
|
// SPDX-FileCopyrightText: 2023 Nheko Contributors
|
2021-03-05 02:35:15 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-07-31 02:59:54 +03:00
|
|
|
#include <algorithm>
|
2020-07-30 01:16:52 +03:00
|
|
|
#include <cctype>
|
2020-07-11 02:19:48 +03:00
|
|
|
#include <chrono>
|
2020-08-01 21:31:10 +03:00
|
|
|
#include <cstdint>
|
2021-02-18 23:55:29 +03:00
|
|
|
#include <cstdlib>
|
2021-02-25 01:07:01 +03:00
|
|
|
#include <memory>
|
2020-07-11 02:19:48 +03:00
|
|
|
|
|
|
|
#include <QMediaPlaylist>
|
|
|
|
#include <QUrl>
|
|
|
|
|
|
|
|
#include "Cache.h"
|
2021-02-07 19:47:47 +03:00
|
|
|
#include "CallDevices.h"
|
2020-08-01 21:31:10 +03:00
|
|
|
#include "CallManager.h"
|
2020-07-11 02:19:48 +03:00
|
|
|
#include "ChatPage.h"
|
|
|
|
#include "Logging.h"
|
|
|
|
#include "MatrixClient.h"
|
2020-12-17 19:25:32 +03:00
|
|
|
#include "UserSettingsPage.h"
|
2020-10-27 19:45:28 +03:00
|
|
|
#include "Utils.h"
|
2020-07-11 02:19:48 +03:00
|
|
|
|
2020-07-26 17:59:50 +03:00
|
|
|
#include "mtx/responses/turn_server.hpp"
|
|
|
|
|
2022-10-14 16:49:05 +03:00
|
|
|
/*
|
|
|
|
* Select Answer when one instance of the client supports v0
|
|
|
|
*/
|
|
|
|
|
2021-02-25 01:07:01 +03:00
|
|
|
#ifdef XCB_AVAILABLE
|
|
|
|
#include <xcb/xcb.h>
|
|
|
|
#include <xcb/xcb_ewmh.h>
|
|
|
|
#endif
|
|
|
|
|
2021-02-25 20:00:55 +03:00
|
|
|
#ifdef GSTREAMER_AVAILABLE
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#include "gst/gst.h"
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-06-27 19:09:31 +03:00
|
|
|
Q_DECLARE_METATYPE(std::vector<mtx::events::voip::CallCandidates::Candidate>)
|
|
|
|
Q_DECLARE_METATYPE(mtx::events::voip::CallCandidates::Candidate)
|
2020-07-11 02:19:48 +03:00
|
|
|
Q_DECLARE_METATYPE(mtx::responses::TurnServer)
|
|
|
|
|
|
|
|
using namespace mtx::events;
|
2022-06-27 19:09:31 +03:00
|
|
|
using namespace mtx::events::voip;
|
2020-07-11 02:19:48 +03:00
|
|
|
|
2021-02-18 23:55:29 +03:00
|
|
|
using webrtc::CallType;
|
2022-06-30 13:33:44 +03:00
|
|
|
//! Session Description Object
|
2022-06-27 19:38:02 +03:00
|
|
|
typedef RTCSessionDescriptionInit SDO;
|
2021-02-18 23:55:29 +03:00
|
|
|
|
2020-07-26 17:59:50 +03:00
|
|
|
namespace {
|
|
|
|
std::vector<std::string>
|
|
|
|
getTurnURIs(const mtx::responses::TurnServer &turnServer);
|
|
|
|
}
|
|
|
|
|
2020-10-28 23:08:17 +03:00
|
|
|
CallManager::CallManager(QObject *parent)
|
2020-10-27 19:45:28 +03:00
|
|
|
: QObject(parent)
|
2020-08-01 21:31:10 +03:00
|
|
|
, session_(WebRTCSession::instance())
|
|
|
|
, turnServerTimer_(this)
|
2020-07-11 02:19:48 +03:00
|
|
|
{
|
2022-06-27 19:09:31 +03:00
|
|
|
qRegisterMetaType<std::vector<mtx::events::voip::CallCandidates::Candidate>>();
|
|
|
|
qRegisterMetaType<mtx::events::voip::CallCandidates::Candidate>();
|
2021-09-18 01:22:33 +03:00
|
|
|
qRegisterMetaType<mtx::responses::TurnServer>();
|
|
|
|
|
|
|
|
connect(
|
|
|
|
&session_,
|
|
|
|
&WebRTCSession::offerCreated,
|
|
|
|
this,
|
|
|
|
[this](const std::string &sdp, const std::vector<CallCandidates::Candidate> &candidates) {
|
|
|
|
nhlog::ui()->debug("WebRTC: call id: {} - sending offer", callid_);
|
2022-10-14 16:49:05 +03:00
|
|
|
emit newMessage(roomid_,
|
|
|
|
CallInvite{callid_,
|
|
|
|
partyid_,
|
|
|
|
SDO{sdp, SDO::Type::Offer},
|
|
|
|
callPartyVersion_,
|
|
|
|
timeoutms_,
|
|
|
|
invitee_});
|
|
|
|
emit newMessage(roomid_,
|
|
|
|
CallCandidates{callid_, partyid_, candidates, callPartyVersion_});
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string callid(callid_);
|
|
|
|
QTimer::singleShot(timeoutms_, this, [this, callid]() {
|
|
|
|
if (session_.state() == webrtc::State::OFFERSENT && callid == callid_) {
|
|
|
|
hangUp(CallHangUp::Reason::InviteTimeOut);
|
2021-12-29 08:01:38 +03:00
|
|
|
emit ChatPage::instance()->showNotification(
|
|
|
|
QStringLiteral("The remote side failed to pick up."));
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-08-01 21:31:10 +03:00
|
|
|
});
|
2021-09-18 01:22:33 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
connect(
|
|
|
|
&session_,
|
|
|
|
&WebRTCSession::answerCreated,
|
|
|
|
this,
|
|
|
|
[this](const std::string &sdp, const std::vector<CallCandidates::Candidate> &candidates) {
|
|
|
|
nhlog::ui()->debug("WebRTC: call id: {} - sending answer", callid_);
|
2022-10-14 16:49:05 +03:00
|
|
|
emit newMessage(
|
|
|
|
roomid_, CallAnswer{callid_, partyid_, callPartyVersion_, SDO{sdp, SDO::Type::Answer}});
|
|
|
|
emit newMessage(roomid_,
|
|
|
|
CallCandidates{callid_, partyid_, candidates, callPartyVersion_});
|
2021-09-18 01:22:33 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
connect(&session_,
|
|
|
|
&WebRTCSession::newICECandidate,
|
|
|
|
this,
|
|
|
|
[this](const CallCandidates::Candidate &candidate) {
|
|
|
|
nhlog::ui()->debug("WebRTC: call id: {} - sending ice candidate", callid_);
|
2022-10-14 16:49:05 +03:00
|
|
|
emit newMessage(roomid_,
|
|
|
|
CallCandidates{callid_, partyid_, {candidate}, callPartyVersion_});
|
2021-09-18 01:22:33 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
connect(&turnServerTimer_, &QTimer::timeout, this, &CallManager::retrieveTurnServer);
|
|
|
|
|
|
|
|
connect(
|
|
|
|
this, &CallManager::turnServerRetrieved, this, [this](const mtx::responses::TurnServer &res) {
|
|
|
|
nhlog::net()->info("TURN server(s) retrieved from homeserver:");
|
|
|
|
nhlog::net()->info("username: {}", res.username);
|
|
|
|
nhlog::net()->info("ttl: {} seconds", res.ttl);
|
|
|
|
for (const auto &u : res.uris)
|
|
|
|
nhlog::net()->info("uri: {}", u);
|
|
|
|
|
|
|
|
// Request new credentials close to expiry
|
|
|
|
// See https://tools.ietf.org/html/draft-uberti-behave-turn-rest-00
|
|
|
|
turnURIs_ = getTurnURIs(res);
|
2022-10-26 02:10:35 +03:00
|
|
|
uint32_t ttl = std::max(res.ttl, std::uint32_t{3600});
|
2021-09-18 01:22:33 +03:00
|
|
|
if (res.ttl < 3600)
|
|
|
|
nhlog::net()->warn("Setting ttl to 1 hour");
|
2022-10-26 02:10:35 +03:00
|
|
|
turnServerTimer_.setInterval(std::chrono::seconds(ttl) * 10 / 9);
|
2021-09-18 01:22:33 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
connect(&session_, &WebRTCSession::stateChanged, this, [this](webrtc::State state) {
|
|
|
|
switch (state) {
|
|
|
|
case webrtc::State::DISCONNECTED:
|
2021-12-29 06:28:08 +03:00
|
|
|
playRingtone(QUrl(QStringLiteral("qrc:/media/media/callend.ogg")), false);
|
2021-09-18 01:22:33 +03:00
|
|
|
clear();
|
|
|
|
break;
|
|
|
|
case webrtc::State::ICEFAILED: {
|
2021-12-29 06:28:08 +03:00
|
|
|
QString error(QStringLiteral("Call connection failed."));
|
2021-09-18 01:22:33 +03:00
|
|
|
if (turnURIs_.empty())
|
2021-12-29 06:28:08 +03:00
|
|
|
error += QLatin1String(" Your homeserver has no configured TURN server.");
|
2021-09-18 01:22:33 +03:00
|
|
|
emit ChatPage::instance()->showNotification(error);
|
|
|
|
hangUp(CallHangUp::Reason::ICEFailed);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
emit newCallState();
|
|
|
|
});
|
2020-08-01 21:31:10 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
connect(
|
|
|
|
&CallDevices::instance(), &CallDevices::devicesChanged, this, &CallManager::devicesChanged);
|
2020-08-01 21:31:10 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
connect(
|
|
|
|
&player_, &QMediaPlayer::mediaStatusChanged, this, [this](QMediaPlayer::MediaStatus status) {
|
|
|
|
if (status == QMediaPlayer::LoadedMedia)
|
|
|
|
player_.play();
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(&player_,
|
|
|
|
QOverload<QMediaPlayer::Error>::of(&QMediaPlayer::error),
|
2021-12-29 00:30:12 +03:00
|
|
|
this,
|
2021-09-18 01:22:33 +03:00
|
|
|
[this](QMediaPlayer::Error error) {
|
|
|
|
stopRingtone();
|
|
|
|
switch (error) {
|
|
|
|
case QMediaPlayer::FormatError:
|
|
|
|
case QMediaPlayer::ResourceError:
|
|
|
|
nhlog::ui()->error("WebRTC: valid ringtone file not found");
|
|
|
|
break;
|
|
|
|
case QMediaPlayer::AccessDeniedError:
|
|
|
|
nhlog::ui()->error("WebRTC: access to ringtone file denied");
|
|
|
|
break;
|
2020-08-01 21:31:10 +03:00
|
|
|
default:
|
2021-09-18 01:22:33 +03:00
|
|
|
nhlog::ui()->error("WebRTC: unable to play ringtone");
|
|
|
|
break;
|
2020-08-01 21:31:10 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
});
|
2020-07-11 02:19:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-02-25 01:07:01 +03:00
|
|
|
CallManager::sendInvite(const QString &roomid, CallType callType, unsigned int windowIndex)
|
2020-07-11 02:19:48 +03:00
|
|
|
{
|
2022-10-14 16:49:05 +03:00
|
|
|
if (isOnCall() || isOnCallOnOtherDevice()) {
|
|
|
|
if (isOnCallOnOtherDevice_ != "")
|
|
|
|
emit ChatPage::instance()->showNotification(
|
|
|
|
QStringLiteral("User is already in a call"));
|
2021-09-18 01:22:33 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto roomInfo = cache::singleRoomInfo(roomid.toStdString());
|
|
|
|
|
|
|
|
std::string errorMessage;
|
2023-02-08 02:54:02 +03:00
|
|
|
if (!session_.havePlugins(
|
|
|
|
callType != CallType::VOICE, callType == CallType::SCREEN, &errorMessage)) {
|
2021-09-18 01:22:33 +03:00
|
|
|
emit ChatPage::instance()->showNotification(QString::fromStdString(errorMessage));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callType_ = callType;
|
|
|
|
roomid_ = roomid;
|
|
|
|
generateCallID();
|
2022-10-14 16:49:05 +03:00
|
|
|
std::vector<RoomMember> members(cache::getMembers(roomid.toStdString()));
|
|
|
|
const RoomMember *callee;
|
|
|
|
if (roomInfo.member_count == 1)
|
|
|
|
callee = &members.front();
|
|
|
|
else if (roomInfo.member_count == 2)
|
|
|
|
callee = members.front().user_id == utils::localUser() ? &members.back() : &members.front();
|
|
|
|
else {
|
|
|
|
emit ChatPage::instance()->showNotification(
|
|
|
|
QStringLiteral("Calls are limited to rooms with less than two members"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callType == CallType::SCREEN) {
|
|
|
|
if (!screenShareSupported())
|
|
|
|
return;
|
|
|
|
if (windows_.empty() || windowIndex >= windows_.size()) {
|
|
|
|
nhlog::ui()->error("WebRTC: window index out of range");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (haveCallInvite_) {
|
|
|
|
nhlog::ui()->debug(
|
|
|
|
"WebRTC: Discarding outbound call for inbound call. localUser is polite party");
|
|
|
|
if (callParty_ == callee->user_id) {
|
|
|
|
if (callType == callType_)
|
|
|
|
acceptInvite();
|
|
|
|
else {
|
|
|
|
emit ChatPage::instance()->showNotification(
|
|
|
|
QStringLiteral("Can't place call. Call types do not match"));
|
|
|
|
emit newMessage(
|
|
|
|
roomid_,
|
|
|
|
CallHangUp{callid_, partyid_, callPartyVersion_, CallHangUp::Reason::UserBusy});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
emit ChatPage::instance()->showNotification(
|
|
|
|
QStringLiteral("Already on a call with a different user"));
|
|
|
|
emit newMessage(
|
|
|
|
roomid_,
|
|
|
|
CallHangUp{callid_, partyid_, callPartyVersion_, CallHangUp::Reason::UserBusy});
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
session_.setTurnServers(turnURIs_);
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string strCallType =
|
|
|
|
callType_ == CallType::VOICE ? "voice" : (callType_ == CallType::VIDEO ? "video" : "screen");
|
2022-10-14 16:49:05 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
nhlog::ui()->debug("WebRTC: call id: {} - creating {} invite", callid_, strCallType);
|
2022-10-14 16:49:05 +03:00
|
|
|
callParty_ = callee->user_id;
|
|
|
|
callPartyDisplayName_ = callee->display_name.isEmpty() ? callee->user_id : callee->display_name;
|
2021-09-18 01:22:33 +03:00
|
|
|
callPartyAvatarUrl_ = QString::fromStdString(roomInfo.avatar_url);
|
2022-10-14 16:49:05 +03:00
|
|
|
invitee_ = callParty_.toStdString();
|
2021-09-18 01:22:33 +03:00
|
|
|
emit newInviteState();
|
2021-12-29 06:28:08 +03:00
|
|
|
playRingtone(QUrl(QStringLiteral("qrc:/media/media/ringback.ogg")), true);
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!session_.createOffer(callType,
|
|
|
|
callType == CallType::SCREEN ? windows_[windowIndex].second : 0)) {
|
2021-12-29 06:28:08 +03:00
|
|
|
emit ChatPage::instance()->showNotification(QStringLiteral("Problem setting up call."));
|
2021-09-18 01:22:33 +03:00
|
|
|
endCall();
|
|
|
|
}
|
2020-07-11 02:19:48 +03:00
|
|
|
}
|
|
|
|
|
2020-07-26 17:59:50 +03:00
|
|
|
namespace {
|
2020-08-01 21:31:10 +03:00
|
|
|
std::string
|
|
|
|
callHangUpReasonString(CallHangUp::Reason reason)
|
2020-07-26 17:59:50 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
switch (reason) {
|
|
|
|
case CallHangUp::Reason::ICEFailed:
|
|
|
|
return "ICE failed";
|
|
|
|
case CallHangUp::Reason::InviteTimeOut:
|
|
|
|
return "Invite time out";
|
2022-06-27 19:38:02 +03:00
|
|
|
case CallHangUp::Reason::ICETimeOut:
|
|
|
|
return "ICE time out";
|
|
|
|
case CallHangUp::Reason::UserHangUp:
|
|
|
|
return "User hung up";
|
|
|
|
case CallHangUp::Reason::UserMediaFailed:
|
|
|
|
return "User media failed";
|
|
|
|
case CallHangUp::Reason::UserBusy:
|
|
|
|
return "User busy";
|
|
|
|
case CallHangUp::Reason::UnknownError:
|
|
|
|
return "Unknown error";
|
2021-09-18 01:22:33 +03:00
|
|
|
default:
|
|
|
|
return "User";
|
|
|
|
}
|
2020-07-26 17:59:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-11 02:19:48 +03:00
|
|
|
void
|
2020-07-26 01:11:11 +03:00
|
|
|
CallManager::hangUp(CallHangUp::Reason reason)
|
2020-07-11 02:19:48 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!callid_.empty()) {
|
|
|
|
nhlog::ui()->debug(
|
|
|
|
"WebRTC: call id: {} - hanging up ({})", callid_, callHangUpReasonString(reason));
|
2022-10-14 16:49:05 +03:00
|
|
|
emit newMessage(roomid_, CallHangUp{callid_, partyid_, callPartyVersion_, reason});
|
2021-09-18 01:22:33 +03:00
|
|
|
endCall();
|
|
|
|
}
|
2020-07-11 02:19:48 +03:00
|
|
|
}
|
|
|
|
|
2020-08-01 21:31:10 +03:00
|
|
|
void
|
|
|
|
CallManager::syncEvent(const mtx::events::collections::TimelineEvents &event)
|
2020-07-11 02:19:48 +03:00
|
|
|
{
|
2020-08-14 02:03:27 +03:00
|
|
|
#ifdef GSTREAMER_AVAILABLE
|
2021-09-18 01:22:33 +03:00
|
|
|
if (handleEvent<CallInvite>(event) || handleEvent<CallCandidates>(event) ||
|
2022-10-14 16:49:05 +03:00
|
|
|
handleEvent<CallNegotiate>(event) || handleEvent<CallSelectAnswer>(event) ||
|
|
|
|
handleEvent<CallAnswer>(event) || handleEvent<CallReject>(event) ||
|
|
|
|
handleEvent<CallHangUp>(event))
|
2021-09-18 01:22:33 +03:00
|
|
|
return;
|
2020-08-14 02:03:27 +03:00
|
|
|
#else
|
2021-09-18 01:22:33 +03:00
|
|
|
(void)event;
|
2020-08-14 02:03:27 +03:00
|
|
|
#endif
|
2020-07-11 02:19:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool
|
2021-02-25 20:00:55 +03:00
|
|
|
CallManager::handleEvent(const mtx::events::collections::TimelineEvents &event)
|
2020-07-11 02:19:48 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (std::holds_alternative<RoomEvent<T>>(event)) {
|
|
|
|
handleEvent(std::get<RoomEvent<T>>(event));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-07-11 02:19:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CallManager::handleEvent(const RoomEvent<CallInvite> &callInviteEvent)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
const char video[] = "m=video";
|
2022-06-27 19:38:02 +03:00
|
|
|
const std::string &sdp = callInviteEvent.content.offer.sdp;
|
2021-09-18 01:22:33 +03:00
|
|
|
bool isVideo = std::search(sdp.cbegin(),
|
|
|
|
sdp.cend(),
|
|
|
|
std::cbegin(video),
|
|
|
|
std::cend(video) - 1,
|
|
|
|
[](unsigned char c1, unsigned char c2) {
|
|
|
|
return std::tolower(c1) == std::tolower(c2);
|
|
|
|
}) != sdp.cend();
|
2022-10-14 16:49:05 +03:00
|
|
|
nhlog::ui()->debug("WebRTC: call id: {} - incoming {} CallInvite from ({},{}) ",
|
2021-09-18 01:22:33 +03:00
|
|
|
callInviteEvent.content.call_id,
|
|
|
|
(isVideo ? "video" : "voice"),
|
2022-10-14 16:49:05 +03:00
|
|
|
callInviteEvent.sender,
|
|
|
|
callInviteEvent.content.party_id);
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
if (callInviteEvent.content.call_id.empty())
|
|
|
|
return;
|
|
|
|
|
2022-10-14 16:49:05 +03:00
|
|
|
if (callInviteEvent.sender == utils::localUser().toStdString()) {
|
|
|
|
if (callInviteEvent.content.party_id == partyid_)
|
|
|
|
return;
|
|
|
|
else {
|
|
|
|
if (callInviteEvent.content.invitee != utils::localUser().toStdString()) {
|
|
|
|
isOnCallOnOtherDevice_ = callInviteEvent.content.call_id;
|
|
|
|
emit newCallDeviceState();
|
|
|
|
nhlog::ui()->debug("WebRTC: User is on call on other device.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
|
2022-10-14 16:49:05 +03:00
|
|
|
auto roomInfo = cache::singleRoomInfo(callInviteEvent.room_id);
|
|
|
|
callPartyVersion_ = callInviteEvent.content.version;
|
|
|
|
|
2022-01-03 00:15:10 +03:00
|
|
|
const QString &ringtone = UserSettings::instance()->ringtone();
|
2022-10-14 16:49:05 +03:00
|
|
|
bool sharesRoom = true;
|
|
|
|
|
|
|
|
std::vector<RoomMember> members(cache::getMembers(callInviteEvent.room_id));
|
|
|
|
const RoomMember &caller =
|
|
|
|
*std::find_if(members.begin(), members.end(), [&](RoomMember member) {
|
|
|
|
return member.user_id.toStdString() == callInviteEvent.sender;
|
|
|
|
});
|
|
|
|
if (isOnCall() || isOnCallOnOtherDevice()) {
|
|
|
|
if (isOnCallOnOtherDevice_ != "")
|
|
|
|
return;
|
|
|
|
if (callParty_.toStdString() == callInviteEvent.sender) {
|
|
|
|
if (session_.state() == webrtc::State::OFFERSENT) {
|
|
|
|
if (callid_ > callInviteEvent.content.call_id) {
|
|
|
|
endCall();
|
|
|
|
callParty_ = caller.user_id;
|
|
|
|
callPartyDisplayName_ =
|
|
|
|
caller.display_name.isEmpty() ? caller.user_id : caller.display_name;
|
|
|
|
callPartyAvatarUrl_ = QString::fromStdString(roomInfo.avatar_url);
|
|
|
|
|
|
|
|
roomid_ = QString::fromStdString(callInviteEvent.room_id);
|
|
|
|
callid_ = callInviteEvent.content.call_id;
|
|
|
|
remoteICECandidates_.clear();
|
|
|
|
haveCallInvite_ = true;
|
|
|
|
callType_ = isVideo ? CallType::VIDEO : CallType::VOICE;
|
|
|
|
inviteSDP_ = callInviteEvent.content.offer.sdp;
|
|
|
|
emit newInviteState();
|
|
|
|
acceptInvite();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (session_.state() < webrtc::State::OFFERSENT)
|
|
|
|
endCall();
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
} else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callPartyVersion_ == "0") {
|
|
|
|
if (roomInfo.member_count != 2) {
|
|
|
|
emit newMessage(QString::fromStdString(callInviteEvent.room_id),
|
|
|
|
CallHangUp{callInviteEvent.content.call_id,
|
|
|
|
partyid_,
|
|
|
|
callPartyVersion_,
|
|
|
|
CallHangUp::Reason::InviteTimeOut});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (caller.user_id == utils::localUser() &&
|
|
|
|
callInviteEvent.content.party_id == partyid_) // remote echo
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (roomInfo.member_count == 2 || // general call in room with two members
|
|
|
|
(roomInfo.member_count == 1 &&
|
|
|
|
partyid_ !=
|
|
|
|
callInviteEvent.content.party_id) || // self call, ring if not the same party_id
|
|
|
|
callInviteEvent.content.invitee == "" || // empty, meant for everyone
|
|
|
|
callInviteEvent.content.invitee ==
|
|
|
|
utils::localUser().toStdString()) // meant specifically for local user
|
|
|
|
{
|
|
|
|
if (roomInfo.member_count > 2) {
|
|
|
|
// check if shares room
|
|
|
|
sharesRoom = checkSharesRoom(QString::fromStdString(callInviteEvent.room_id),
|
|
|
|
callInviteEvent.content.invitee);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
emit newMessage(QString::fromStdString(callInviteEvent.room_id),
|
|
|
|
CallHangUp{callInviteEvent.content.call_id,
|
|
|
|
partyid_,
|
|
|
|
callPartyVersion_,
|
|
|
|
CallHangUp::Reason::InviteTimeOut});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ring if not mute or does not have direct message room
|
|
|
|
if (ringtone != QLatin1String("Mute") && sharesRoom)
|
2021-12-29 08:01:38 +03:00
|
|
|
playRingtone(ringtone == QLatin1String("Default")
|
|
|
|
? QUrl(QStringLiteral("qrc:/media/media/ring.ogg"))
|
|
|
|
: QUrl::fromLocalFile(ringtone),
|
2021-09-18 01:22:33 +03:00
|
|
|
true);
|
|
|
|
|
|
|
|
callParty_ = caller.user_id;
|
|
|
|
callPartyDisplayName_ = caller.display_name.isEmpty() ? caller.user_id : caller.display_name;
|
|
|
|
callPartyAvatarUrl_ = QString::fromStdString(roomInfo.avatar_url);
|
|
|
|
|
2022-10-14 16:49:05 +03:00
|
|
|
roomid_ = QString::fromStdString(callInviteEvent.room_id);
|
|
|
|
callid_ = callInviteEvent.content.call_id;
|
|
|
|
remoteICECandidates_.clear();
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
haveCallInvite_ = true;
|
|
|
|
callType_ = isVideo ? CallType::VIDEO : CallType::VOICE;
|
2022-06-27 19:38:02 +03:00
|
|
|
inviteSDP_ = callInviteEvent.content.offer.sdp;
|
2021-09-18 01:22:33 +03:00
|
|
|
emit newInviteState();
|
2020-07-11 02:19:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-12-17 19:25:32 +03:00
|
|
|
CallManager::acceptInvite()
|
2020-07-11 02:19:48 +03:00
|
|
|
{
|
2022-10-14 16:49:05 +03:00
|
|
|
// if call was accepted/rejected elsewhere and m.call.select_answer is received
|
|
|
|
// before acceptInvite
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!haveCallInvite_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
stopRingtone();
|
|
|
|
std::string errorMessage;
|
2023-02-08 02:54:02 +03:00
|
|
|
if (!session_.havePlugins(
|
|
|
|
callType_ != CallType::VOICE, callType_ == CallType::SCREEN, &errorMessage)) {
|
2021-09-18 01:22:33 +03:00
|
|
|
emit ChatPage::instance()->showNotification(QString::fromStdString(errorMessage));
|
2022-10-14 16:49:05 +03:00
|
|
|
hangUp(CallHangUp::Reason::UserMediaFailed);
|
2021-09-18 01:22:33 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
session_.setTurnServers(turnURIs_);
|
|
|
|
if (!session_.acceptOffer(inviteSDP_)) {
|
2021-12-29 06:28:08 +03:00
|
|
|
emit ChatPage::instance()->showNotification(QStringLiteral("Problem setting up call."));
|
2021-09-18 01:22:33 +03:00
|
|
|
hangUp();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
session_.acceptICECandidates(remoteICECandidates_);
|
|
|
|
remoteICECandidates_.clear();
|
|
|
|
haveCallInvite_ = false;
|
|
|
|
emit newInviteState();
|
2020-07-11 02:19:48 +03:00
|
|
|
}
|
|
|
|
|
2022-10-14 16:49:05 +03:00
|
|
|
void
|
|
|
|
CallManager::rejectInvite()
|
|
|
|
{
|
|
|
|
if (callPartyVersion_ == "0") {
|
|
|
|
hangUp();
|
|
|
|
// send m.call.reject after sending hangup as mentioned in MSC2746
|
|
|
|
emit newMessage(roomid_, CallReject{callid_, partyid_, callPartyVersion_});
|
|
|
|
}
|
|
|
|
if (!callid_.empty()) {
|
|
|
|
nhlog::ui()->debug("WebRTC: call id: {} - rejecting call", callid_);
|
|
|
|
emit newMessage(roomid_, CallReject{callid_, partyid_, callPartyVersion_});
|
|
|
|
endCall(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-11 02:19:48 +03:00
|
|
|
void
|
|
|
|
CallManager::handleEvent(const RoomEvent<CallCandidates> &callCandidatesEvent)
|
|
|
|
{
|
2022-10-14 16:49:05 +03:00
|
|
|
if (callCandidatesEvent.sender == utils::localUser().toStdString() &&
|
|
|
|
callCandidatesEvent.content.party_id == partyid_)
|
2021-09-18 01:22:33 +03:00
|
|
|
return;
|
2022-10-14 16:49:05 +03:00
|
|
|
nhlog::ui()->debug("WebRTC: call id: {} - incoming CallCandidates from ({}, {})",
|
2021-09-18 01:22:33 +03:00
|
|
|
callCandidatesEvent.content.call_id,
|
2022-10-14 16:49:05 +03:00
|
|
|
callCandidatesEvent.sender,
|
|
|
|
callCandidatesEvent.content.party_id);
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
if (callid_ == callCandidatesEvent.content.call_id) {
|
|
|
|
if (isOnCall())
|
|
|
|
session_.acceptICECandidates(callCandidatesEvent.content.candidates);
|
|
|
|
else {
|
|
|
|
// CallInvite has been received and we're awaiting localUser to accept or
|
|
|
|
// reject the call
|
|
|
|
for (const auto &c : callCandidatesEvent.content.candidates)
|
|
|
|
remoteICECandidates_.push_back(c);
|
2020-08-01 21:31:10 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-07-11 02:19:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CallManager::handleEvent(const RoomEvent<CallAnswer> &callAnswerEvent)
|
|
|
|
{
|
2022-10-14 16:49:05 +03:00
|
|
|
nhlog::ui()->debug("WebRTC: call id: {} - incoming CallAnswer from ({}, {})",
|
2021-09-18 01:22:33 +03:00
|
|
|
callAnswerEvent.content.call_id,
|
2022-10-14 16:49:05 +03:00
|
|
|
callAnswerEvent.sender,
|
|
|
|
callAnswerEvent.content.party_id);
|
|
|
|
if (answerSelected_)
|
|
|
|
return;
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
if (callAnswerEvent.sender == utils::localUser().toStdString() &&
|
|
|
|
callid_ == callAnswerEvent.content.call_id) {
|
2022-10-14 16:49:05 +03:00
|
|
|
if (partyid_ == callAnswerEvent.content.party_id)
|
|
|
|
return;
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!isOnCall()) {
|
2021-12-29 08:01:38 +03:00
|
|
|
emit ChatPage::instance()->showNotification(
|
|
|
|
QStringLiteral("Call answered on another device."));
|
2021-09-18 01:22:33 +03:00
|
|
|
stopRingtone();
|
|
|
|
haveCallInvite_ = false;
|
2022-10-14 16:49:05 +03:00
|
|
|
if (callPartyVersion_ != "1") {
|
|
|
|
isOnCallOnOtherDevice_ = callid_;
|
|
|
|
emit newCallDeviceState();
|
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
emit newInviteState();
|
2020-08-01 21:31:10 +03:00
|
|
|
}
|
2022-10-14 16:49:05 +03:00
|
|
|
if (callParty_ != utils::localUser())
|
|
|
|
return;
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-08-01 21:31:10 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (isOnCall() && callid_ == callAnswerEvent.content.call_id) {
|
|
|
|
stopRingtone();
|
2022-06-27 19:38:02 +03:00
|
|
|
if (!session_.acceptAnswer(callAnswerEvent.content.answer.sdp)) {
|
2021-12-29 06:28:08 +03:00
|
|
|
emit ChatPage::instance()->showNotification(QStringLiteral("Problem setting up call."));
|
2021-09-18 01:22:33 +03:00
|
|
|
hangUp();
|
2020-08-01 21:31:10 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2022-10-14 16:49:05 +03:00
|
|
|
emit newMessage(
|
|
|
|
roomid_,
|
|
|
|
CallSelectAnswer{callid_, partyid_, callPartyVersion_, callAnswerEvent.content.party_id});
|
|
|
|
selectedpartyid_ = callAnswerEvent.content.party_id;
|
|
|
|
answerSelected_ = true;
|
2020-07-11 02:19:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CallManager::handleEvent(const RoomEvent<CallHangUp> &callHangUpEvent)
|
|
|
|
{
|
2022-10-14 16:49:05 +03:00
|
|
|
nhlog::ui()->debug("WebRTC: call id: {} - incoming CallHangUp ({}) from ({}, {})",
|
2021-09-18 01:22:33 +03:00
|
|
|
callHangUpEvent.content.call_id,
|
|
|
|
callHangUpReasonString(callHangUpEvent.content.reason),
|
2022-10-14 16:49:05 +03:00
|
|
|
callHangUpEvent.sender,
|
|
|
|
callHangUpEvent.content.party_id);
|
|
|
|
|
|
|
|
if (callid_ == callHangUpEvent.content.call_id ||
|
|
|
|
isOnCallOnOtherDevice_ == callHangUpEvent.content.call_id)
|
|
|
|
endCall();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CallManager::handleEvent(const RoomEvent<CallSelectAnswer> &callSelectAnswerEvent)
|
|
|
|
{
|
|
|
|
nhlog::ui()->debug("WebRTC: call id: {} - incoming CallSelectAnswer from ({}, {})",
|
|
|
|
callSelectAnswerEvent.content.call_id,
|
|
|
|
callSelectAnswerEvent.sender,
|
|
|
|
callSelectAnswerEvent.content.party_id);
|
|
|
|
if (callSelectAnswerEvent.sender == utils::localUser().toStdString()) {
|
|
|
|
if (callSelectAnswerEvent.content.party_id != partyid_) {
|
|
|
|
if (std::find(rejectCallPartyIDs_.begin(),
|
|
|
|
rejectCallPartyIDs_.begin(),
|
|
|
|
callSelectAnswerEvent.content.selected_party_id) !=
|
|
|
|
rejectCallPartyIDs_.end())
|
|
|
|
endCall();
|
|
|
|
else {
|
|
|
|
if (callSelectAnswerEvent.content.selected_party_id == partyid_)
|
|
|
|
return;
|
|
|
|
nhlog::ui()->debug("WebRTC: call id: {} - user is on call with this user!",
|
|
|
|
callSelectAnswerEvent.content.call_id);
|
|
|
|
isOnCallOnOtherDevice_ = callSelectAnswerEvent.content.call_id;
|
|
|
|
emit newCallDeviceState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (callid_ == callSelectAnswerEvent.content.call_id) {
|
|
|
|
if (callSelectAnswerEvent.content.selected_party_id != partyid_) {
|
|
|
|
bool endAllCalls = false;
|
|
|
|
if (std::find(rejectCallPartyIDs_.begin(),
|
|
|
|
rejectCallPartyIDs_.begin(),
|
|
|
|
callSelectAnswerEvent.content.selected_party_id) !=
|
|
|
|
rejectCallPartyIDs_.end())
|
|
|
|
endAllCalls = true;
|
|
|
|
else {
|
|
|
|
isOnCallOnOtherDevice_ = callid_;
|
|
|
|
emit newCallDeviceState();
|
|
|
|
}
|
|
|
|
endCall(endAllCalls);
|
|
|
|
} else if (session_.state() == webrtc::State::DISCONNECTED)
|
|
|
|
endCall();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CallManager::handleEvent(const RoomEvent<CallReject> &callRejectEvent)
|
|
|
|
{
|
|
|
|
nhlog::ui()->debug("WebRTC: call id: {} - incoming CallReject from ({}, {})",
|
|
|
|
callRejectEvent.content.call_id,
|
|
|
|
callRejectEvent.sender,
|
|
|
|
callRejectEvent.content.party_id);
|
|
|
|
if (answerSelected_)
|
|
|
|
return;
|
2020-08-01 21:31:10 +03:00
|
|
|
|
2022-10-14 16:49:05 +03:00
|
|
|
rejectCallPartyIDs_.push_back(callRejectEvent.content.party_id);
|
|
|
|
// check remote echo
|
|
|
|
if (callRejectEvent.sender == utils::localUser().toStdString()) {
|
|
|
|
if (callRejectEvent.content.party_id != partyid_ && callParty_ != utils::localUser())
|
|
|
|
emit ChatPage::instance()->showNotification(
|
|
|
|
QStringLiteral("Call rejected on another device."));
|
2021-09-18 01:22:33 +03:00
|
|
|
endCall();
|
2022-10-14 16:49:05 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callRejectEvent.content.call_id == callid_) {
|
|
|
|
if (session_.state() == webrtc::State::OFFERSENT) {
|
|
|
|
// only accept reject if webrtc is in OFFERSENT state, else call has been accepted
|
|
|
|
emit newMessage(
|
|
|
|
roomid_,
|
|
|
|
CallSelectAnswer{
|
|
|
|
callid_, partyid_, callPartyVersion_, callRejectEvent.content.party_id});
|
|
|
|
endCall();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CallManager::handleEvent(const RoomEvent<CallNegotiate> &callNegotiateEvent)
|
|
|
|
{
|
|
|
|
nhlog::ui()->debug("WebRTC: call id: {} - incoming CallNegotiate from ({}, {})",
|
|
|
|
callNegotiateEvent.content.call_id,
|
|
|
|
callNegotiateEvent.sender,
|
|
|
|
callNegotiateEvent.content.party_id);
|
|
|
|
|
|
|
|
std::string negotiationSDP_ = callNegotiateEvent.content.description.sdp;
|
|
|
|
if (!session_.acceptNegotiation(negotiationSDP_)) {
|
|
|
|
emit ChatPage::instance()->showNotification(QStringLiteral("Problem accepting new SDP"));
|
|
|
|
hangUp();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
session_.acceptICECandidates(remoteICECandidates_);
|
|
|
|
remoteICECandidates_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CallManager::checkSharesRoom(QString roomid, std::string invitee) const
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
IMPLEMENTATION REQUIRED
|
|
|
|
Check if room is shared to determine whether to ring or not.
|
|
|
|
Called from handle callInvite event
|
|
|
|
*/
|
|
|
|
if (roomid.toStdString() != "") {
|
|
|
|
if (invitee == "") {
|
|
|
|
// check all members
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
// check if invitee shares a direct room with local user
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2020-07-11 02:19:48 +03:00
|
|
|
}
|
|
|
|
|
2020-12-10 04:49:48 +03:00
|
|
|
void
|
|
|
|
CallManager::toggleMicMute()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
session_.toggleMicMute();
|
|
|
|
emit micMuteChanged();
|
2020-12-10 04:49:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2021-02-18 23:55:29 +03:00
|
|
|
CallManager::callsSupported()
|
2020-12-10 04:49:48 +03:00
|
|
|
{
|
|
|
|
#ifdef GSTREAMER_AVAILABLE
|
2021-09-18 01:22:33 +03:00
|
|
|
return true;
|
2020-12-10 04:49:48 +03:00
|
|
|
#else
|
2021-09-18 01:22:33 +03:00
|
|
|
return false;
|
2020-12-10 04:49:48 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-02-18 23:55:29 +03:00
|
|
|
bool
|
|
|
|
CallManager::screenShareSupported()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return std::getenv("DISPLAY") && !std::getenv("WAYLAND_DISPLAY");
|
2021-02-18 23:55:29 +03:00
|
|
|
}
|
|
|
|
|
2020-12-17 19:25:32 +03:00
|
|
|
QStringList
|
|
|
|
CallManager::devices(bool isVideo) const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
QStringList ret;
|
2022-01-03 00:15:10 +03:00
|
|
|
const QString &defaultDevice =
|
|
|
|
isVideo ? UserSettings::instance()->camera() : UserSettings::instance()->microphone();
|
2021-09-18 01:22:33 +03:00
|
|
|
std::vector<std::string> devices =
|
|
|
|
CallDevices::instance().names(isVideo, defaultDevice.toStdString());
|
2022-10-26 02:10:35 +03:00
|
|
|
assert(devices.size() < std::numeric_limits<int>::max());
|
|
|
|
ret.reserve(static_cast<int>(devices.size()));
|
2021-09-18 01:22:33 +03:00
|
|
|
std::transform(devices.cbegin(), devices.cend(), std::back_inserter(ret), [](const auto &d) {
|
|
|
|
return QString::fromStdString(d);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
2020-12-17 19:25:32 +03:00
|
|
|
}
|
|
|
|
|
2020-07-11 02:19:48 +03:00
|
|
|
void
|
|
|
|
CallManager::generateCallID()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
using namespace std::chrono;
|
|
|
|
uint64_t ms = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
|
|
|
|
callid_ = "c" + std::to_string(ms);
|
2020-08-01 21:31:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-10-14 16:49:05 +03:00
|
|
|
CallManager::clear(bool endAllCalls)
|
2020-08-01 21:31:10 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
roomid_.clear();
|
|
|
|
callParty_.clear();
|
|
|
|
callPartyDisplayName_.clear();
|
|
|
|
callPartyAvatarUrl_.clear();
|
|
|
|
callid_.clear();
|
|
|
|
callType_ = CallType::VOICE;
|
|
|
|
haveCallInvite_ = false;
|
2022-10-14 16:49:05 +03:00
|
|
|
answerSelected_ = false;
|
|
|
|
if (endAllCalls) {
|
|
|
|
isOnCallOnOtherDevice_ = "";
|
|
|
|
rejectCallPartyIDs_.clear();
|
|
|
|
emit newCallDeviceState();
|
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
emit newInviteState();
|
|
|
|
inviteSDP_.clear();
|
|
|
|
remoteICECandidates_.clear();
|
2020-07-11 02:19:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-10-14 16:49:05 +03:00
|
|
|
CallManager::endCall(bool endAllCalls)
|
2020-07-11 02:19:48 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
stopRingtone();
|
|
|
|
session_.end();
|
2022-10-14 16:49:05 +03:00
|
|
|
clear(endAllCalls);
|
2020-07-11 02:19:48 +03:00
|
|
|
}
|
|
|
|
|
2020-07-31 02:59:54 +03:00
|
|
|
void
|
|
|
|
CallManager::refreshTurnServer()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
turnURIs_.clear();
|
|
|
|
turnServerTimer_.start(2000);
|
2020-07-31 02:59:54 +03:00
|
|
|
}
|
|
|
|
|
2020-07-11 02:19:48 +03:00
|
|
|
void
|
|
|
|
CallManager::retrieveTurnServer()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
http::client()->get_turn_server(
|
|
|
|
[this](const mtx::responses::TurnServer &res, mtx::http::RequestErr err) {
|
|
|
|
if (err) {
|
|
|
|
turnServerTimer_.setInterval(5000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
emit turnServerRetrieved(res);
|
|
|
|
});
|
2020-07-11 02:19:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-11-13 03:55:35 +03:00
|
|
|
CallManager::playRingtone(const QUrl &ringtone, bool repeat)
|
2020-07-26 17:59:50 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
static QMediaPlaylist playlist;
|
|
|
|
playlist.clear();
|
|
|
|
playlist.setPlaybackMode(repeat ? QMediaPlaylist::CurrentItemInLoop
|
|
|
|
: QMediaPlaylist::CurrentItemOnce);
|
|
|
|
playlist.addMedia(ringtone);
|
|
|
|
player_.setVolume(100);
|
|
|
|
player_.setPlaylist(&playlist);
|
2020-07-26 17:59:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CallManager::stopRingtone()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
player_.setPlaylist(nullptr);
|
2020-07-26 17:59:50 +03:00
|
|
|
}
|
|
|
|
|
2021-02-25 01:07:01 +03:00
|
|
|
QStringList
|
|
|
|
CallManager::windowList()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
windows_.clear();
|
|
|
|
windows_.push_back({tr("Entire screen"), 0});
|
2021-02-25 01:07:01 +03:00
|
|
|
|
|
|
|
#ifdef XCB_AVAILABLE
|
2021-09-18 01:22:33 +03:00
|
|
|
std::unique_ptr<xcb_connection_t, std::function<void(xcb_connection_t *)>> connection(
|
|
|
|
xcb_connect(nullptr, nullptr), [](xcb_connection_t *c) { xcb_disconnect(c); });
|
|
|
|
if (xcb_connection_has_error(connection.get())) {
|
|
|
|
nhlog::ui()->error("Failed to connect to X server");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
xcb_ewmh_connection_t ewmh;
|
|
|
|
if (!xcb_ewmh_init_atoms_replies(
|
|
|
|
&ewmh, xcb_ewmh_init_atoms(connection.get(), &ewmh), nullptr)) {
|
|
|
|
nhlog::ui()->error("Failed to connect to EWMH server");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
std::unique_ptr<xcb_ewmh_connection_t, std::function<void(xcb_ewmh_connection_t *)>>
|
|
|
|
ewmhconnection(&ewmh, [](xcb_ewmh_connection_t *c) { xcb_ewmh_connection_wipe(c); });
|
|
|
|
|
|
|
|
for (int i = 0; i < ewmh.nb_screens; i++) {
|
|
|
|
xcb_ewmh_get_windows_reply_t clients;
|
|
|
|
if (!xcb_ewmh_get_client_list_reply(
|
|
|
|
&ewmh, xcb_ewmh_get_client_list(&ewmh, i), &clients, nullptr)) {
|
|
|
|
nhlog::ui()->error("Failed to request window list");
|
|
|
|
return {};
|
2021-02-25 01:07:01 +03:00
|
|
|
}
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (uint32_t w = 0; w < clients.windows_len; w++) {
|
|
|
|
xcb_window_t window = clients.windows[w];
|
2021-02-25 01:07:01 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
std::string name;
|
|
|
|
xcb_ewmh_get_utf8_strings_reply_t data;
|
|
|
|
auto getName = [](xcb_ewmh_get_utf8_strings_reply_t *r) {
|
|
|
|
std::string name(r->strings, r->strings_len);
|
|
|
|
xcb_ewmh_get_utf8_strings_reply_wipe(r);
|
|
|
|
return name;
|
|
|
|
};
|
2021-02-25 01:07:01 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
xcb_get_property_cookie_t cookie = xcb_ewmh_get_wm_name(&ewmh, window);
|
|
|
|
if (xcb_ewmh_get_wm_name_reply(&ewmh, cookie, &data, nullptr))
|
|
|
|
name = getName(&data);
|
2021-02-25 01:07:01 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
cookie = xcb_ewmh_get_wm_visible_name(&ewmh, window);
|
|
|
|
if (xcb_ewmh_get_wm_visible_name_reply(&ewmh, cookie, &data, nullptr))
|
|
|
|
name = getName(&data);
|
2021-02-25 01:07:01 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
windows_.push_back({QString::fromStdString(name), window});
|
2021-02-25 01:07:01 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
xcb_ewmh_get_windows_reply_wipe(&clients);
|
|
|
|
}
|
2021-02-25 01:07:01 +03:00
|
|
|
#endif
|
2021-09-18 01:22:33 +03:00
|
|
|
QStringList ret;
|
2022-10-26 02:10:35 +03:00
|
|
|
assert(windows_.size() < std::numeric_limits<int>::max());
|
|
|
|
ret.reserve(static_cast<int>(windows_.size()));
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &w : windows_)
|
|
|
|
ret.append(w.first);
|
2021-02-25 01:07:01 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return ret;
|
2021-02-25 01:07:01 +03:00
|
|
|
}
|
|
|
|
|
2021-02-25 20:00:55 +03:00
|
|
|
#ifdef GSTREAMER_AVAILABLE
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
GstElement *pipe_ = nullptr;
|
|
|
|
unsigned int busWatchId_ = 0;
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
newBusMessage(GstBus *bus G_GNUC_UNUSED, GstMessage *msg, gpointer G_GNUC_UNUSED)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
switch (GST_MESSAGE_TYPE(msg)) {
|
|
|
|
case GST_MESSAGE_EOS:
|
|
|
|
if (pipe_) {
|
|
|
|
gst_element_set_state(GST_ELEMENT(pipe_), GST_STATE_NULL);
|
|
|
|
gst_object_unref(pipe_);
|
|
|
|
pipe_ = nullptr;
|
|
|
|
}
|
|
|
|
if (busWatchId_) {
|
|
|
|
g_source_remove(busWatchId_);
|
|
|
|
busWatchId_ = 0;
|
2021-02-25 20:00:55 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return TRUE;
|
2021-02-25 20:00:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
CallManager::previewWindow(unsigned int index) const
|
|
|
|
{
|
|
|
|
#ifdef GSTREAMER_AVAILABLE
|
2021-09-18 01:22:33 +03:00
|
|
|
if (windows_.empty() || index >= windows_.size() || !gst_is_initialized())
|
|
|
|
return;
|
|
|
|
|
|
|
|
GstElement *ximagesrc = gst_element_factory_make("ximagesrc", nullptr);
|
|
|
|
if (!ximagesrc) {
|
|
|
|
nhlog::ui()->error("Failed to create ximagesrc");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GstElement *videoconvert = gst_element_factory_make("videoconvert", nullptr);
|
|
|
|
GstElement *videoscale = gst_element_factory_make("videoscale", nullptr);
|
|
|
|
GstElement *capsfilter = gst_element_factory_make("capsfilter", nullptr);
|
|
|
|
GstElement *ximagesink = gst_element_factory_make("ximagesink", nullptr);
|
|
|
|
|
|
|
|
g_object_set(ximagesrc, "use-damage", FALSE, nullptr);
|
|
|
|
g_object_set(ximagesrc, "show-pointer", FALSE, nullptr);
|
|
|
|
g_object_set(ximagesrc, "xid", windows_[index].second, nullptr);
|
|
|
|
|
|
|
|
GstCaps *caps = gst_caps_new_simple(
|
|
|
|
"video/x-raw", "width", G_TYPE_INT, 480, "height", G_TYPE_INT, 360, nullptr);
|
|
|
|
g_object_set(capsfilter, "caps", caps, nullptr);
|
|
|
|
gst_caps_unref(caps);
|
|
|
|
|
|
|
|
pipe_ = gst_pipeline_new(nullptr);
|
|
|
|
gst_bin_add_many(
|
|
|
|
GST_BIN(pipe_), ximagesrc, videoconvert, videoscale, capsfilter, ximagesink, nullptr);
|
|
|
|
if (!gst_element_link_many(
|
|
|
|
ximagesrc, videoconvert, videoscale, capsfilter, ximagesink, nullptr)) {
|
|
|
|
nhlog::ui()->error("Failed to link preview window elements");
|
|
|
|
gst_object_unref(pipe_);
|
|
|
|
pipe_ = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (gst_element_set_state(pipe_, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
|
|
|
|
nhlog::ui()->error("Unable to start preview pipeline");
|
|
|
|
gst_object_unref(pipe_);
|
|
|
|
pipe_ = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(pipe_));
|
|
|
|
busWatchId_ = gst_bus_add_watch(bus, newBusMessage, nullptr);
|
|
|
|
gst_object_unref(bus);
|
2021-02-25 20:00:55 +03:00
|
|
|
#else
|
2021-09-18 01:22:33 +03:00
|
|
|
(void)index;
|
2021-02-25 20:00:55 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-07-26 17:59:50 +03:00
|
|
|
namespace {
|
|
|
|
std::vector<std::string>
|
|
|
|
getTurnURIs(const mtx::responses::TurnServer &turnServer)
|
2020-07-11 02:19:48 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
// gstreamer expects: turn(s)://username:password@host:port?transport=udp(tcp)
|
|
|
|
// where username and password are percent-encoded
|
|
|
|
std::vector<std::string> ret;
|
|
|
|
for (const auto &uri : turnServer.uris) {
|
|
|
|
if (auto c = uri.find(':'); c == std::string::npos) {
|
|
|
|
nhlog::ui()->error("Invalid TURN server uri: {}", uri);
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
std::string scheme = std::string(uri, 0, c);
|
|
|
|
if (scheme != "turn" && scheme != "turns") {
|
|
|
|
nhlog::ui()->error("Invalid TURN server uri: {}", uri);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString encodedUri =
|
|
|
|
QString::fromStdString(scheme) + "://" +
|
|
|
|
QUrl::toPercentEncoding(QString::fromStdString(turnServer.username)) + ":" +
|
|
|
|
QUrl::toPercentEncoding(QString::fromStdString(turnServer.password)) + "@" +
|
|
|
|
QString::fromStdString(std::string(uri, ++c));
|
|
|
|
ret.push_back(encodedUri.toStdString());
|
2020-08-01 21:31:10 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
return ret;
|
2020-07-11 02:19:48 +03:00
|
|
|
}
|
|
|
|
}
|