matrixion/src/voip/WebRTCSession.h

119 lines
3.2 KiB
C
Raw Normal View History

2021-03-05 02:35:15 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
// SPDX-FileCopyrightText: 2022 Nheko Contributors
2021-03-05 02:35:15 +03:00
//
// SPDX-License-Identifier: GPL-3.0-or-later
2020-07-11 02:19:48 +03:00
#pragma once
#include <string>
#include <vector>
#include <QObject>
#include "mtx/events/voip.hpp"
typedef struct _GstElement GstElement;
2021-02-18 23:55:29 +03:00
class CallDevices;
2020-10-27 20:14:06 +03:00
class QQuickItem;
2020-07-11 02:19:48 +03:00
2020-09-22 19:07:36 +03:00
namespace webrtc {
Q_NAMESPACE
2021-02-18 23:55:29 +03:00
enum class CallType
{
2021-09-18 01:22:33 +03:00
VOICE,
VIDEO,
SCREEN // localUser is sharing screen
2021-02-18 23:55:29 +03:00
};
Q_ENUM_NS(CallType)
2020-09-22 19:07:36 +03:00
enum class State
{
2021-09-18 01:22:33 +03:00
DISCONNECTED,
ICEFAILED,
INITIATING,
INITIATED,
OFFERSENT,
ANSWERSENT,
CONNECTING,
CONNECTED
2020-09-22 19:07:36 +03:00
};
Q_ENUM_NS(State)
}
2020-07-11 02:19:48 +03:00
class WebRTCSession : public QObject
{
2021-09-18 01:22:33 +03:00
Q_OBJECT
2020-07-11 02:19:48 +03:00
public:
2021-09-18 01:22:33 +03:00
static WebRTCSession &instance()
{
static WebRTCSession instance;
return instance;
}
bool havePlugins(bool isVideo, std::string *errorMessage = nullptr);
webrtc::CallType callType() const { return callType_; }
webrtc::State state() const { return state_; }
bool haveLocalPiP() const;
bool isOffering() const { return isOffering_; }
bool isRemoteVideoRecvOnly() const { return isRemoteVideoRecvOnly_; }
bool isRemoteVideoSendOnly() const { return isRemoteVideoSendOnly_; }
bool createOffer(webrtc::CallType, uint32_t shareWindowId);
bool acceptOffer(const std::string &sdp);
bool acceptAnswer(const std::string &sdp);
void acceptICECandidates(const std::vector<mtx::events::msg::CallCandidates::Candidate> &);
bool isMicMuted() const;
bool toggleMicMute();
void toggleLocalPiP();
void end();
void setTurnServers(const std::vector<std::string> &uris) { turnServers_ = uris; }
void setVideoItem(QQuickItem *item) { videoItem_ = item; }
QQuickItem *getVideoItem() const { return videoItem_; }
2020-08-06 00:56:44 +03:00
2020-07-11 02:19:48 +03:00
signals:
2021-09-18 01:22:33 +03:00
void offerCreated(const std::string &sdp,
const std::vector<mtx::events::msg::CallCandidates::Candidate> &);
void answerCreated(const std::string &sdp,
const std::vector<mtx::events::msg::CallCandidates::Candidate> &);
void newICECandidate(const mtx::events::msg::CallCandidates::Candidate &);
void stateChanged(webrtc::State);
2020-07-23 04:15:45 +03:00
private slots:
2021-09-18 01:22:33 +03:00
void setState(webrtc::State state) { state_ = state; }
2020-07-11 02:19:48 +03:00
private:
2021-09-18 01:22:33 +03:00
WebRTCSession();
CallDevices &devices_;
bool initialised_ = false;
bool haveVoicePlugins_ = false;
bool haveVideoPlugins_ = false;
webrtc::CallType callType_ = webrtc::CallType::VOICE;
webrtc::State state_ = webrtc::State::DISCONNECTED;
bool isOffering_ = false;
bool isRemoteVideoRecvOnly_ = false;
bool isRemoteVideoSendOnly_ = false;
QQuickItem *videoItem_ = nullptr;
GstElement *pipe_ = nullptr;
GstElement *webrtc_ = nullptr;
unsigned int busWatchId_ = 0;
std::vector<std::string> turnServers_;
uint32_t shareWindowId_ = 0;
bool init(std::string *errorMessage = nullptr);
bool startPipeline(int opusPayloadType, int vp8PayloadType);
bool createPipeline(int opusPayloadType, int vp8PayloadType);
bool addVideoPipeline(int vp8PayloadType);
void clear();
2020-07-11 02:19:48 +03:00
public:
2021-09-18 01:22:33 +03:00
WebRTCSession(WebRTCSession const &) = delete;
void operator=(WebRTCSession const &) = delete;
2020-07-11 02:19:48 +03:00
};