Skip to content
Snippets Groups Projects
WebRTCSession.h 3.33 KiB
Newer Older
  • Learn to ignore specific revisions
  • trilene's avatar
    trilene committed
    #pragma once
    
    #include <string>
    #include <vector>
    
    #include <QObject>
    
    #include "mtx/events/voip.hpp"
    
    typedef struct _GstElement GstElement;
    
    trilene's avatar
    trilene committed
    class QQuickItem;
    
    trilene's avatar
    trilene committed
    
    
    trilene's avatar
    trilene committed
    namespace webrtc {
    Q_NAMESPACE
    
    enum class State
    {
            DISCONNECTED,
            ICEFAILED,
            INITIATING,
            INITIATED,
            OFFERSENT,
            ANSWERSENT,
            CONNECTING,
            CONNECTED
    
    };
    Q_ENUM_NS(State)
    
    }
    
    
    trilene's avatar
    trilene committed
    class WebRTCSession : public QObject
    {
            Q_OBJECT
    
    public:
    
    trilene's avatar
    trilene committed
            static WebRTCSession &instance()
    
    trilene's avatar
    trilene committed
            {
    
    trilene's avatar
    trilene committed
                    static WebRTCSession instance;
                    return instance;
    
    trilene's avatar
    trilene committed
            }
    
    
    trilene's avatar
    trilene committed
            bool havePlugins(bool isVideo, std::string *errorMessage = nullptr);
    
    trilene's avatar
    trilene committed
            webrtc::State state() const { return state_; }
    
    trilene's avatar
    trilene committed
            bool isVideo() const { return isVideo_; }
            bool isOffering() const { return isOffering_; }
            bool isRemoteVideoRecvOnly() const { return isRemoteVideoRecvOnly_; }
    
    trilene's avatar
    trilene committed
    
    
    trilene's avatar
    trilene committed
            bool createOffer(bool isVideo);
    
    trilene's avatar
    trilene committed
            bool acceptOffer(const std::string &sdp);
            bool acceptAnswer(const std::string &sdp);
    
    trilene's avatar
    trilene committed
            void acceptICECandidates(const std::vector<mtx::events::msg::CallCandidates::Candidate> &);
    
    trilene's avatar
    trilene committed
    
    
            bool isMicMuted() const;
            bool toggleMicMute();
    
    trilene's avatar
    trilene committed
            void end();
    
    
    trilene's avatar
    trilene committed
            void setTurnServers(const std::vector<std::string> &uris) { turnServers_ = uris; }
    
    trilene's avatar
    trilene committed
    
    
    trilene's avatar
    trilene committed
            void refreshDevices();
            std::vector<std::string> getDeviceNames(bool isVideo,
                                                    const std::string &defaultDevice) const;
            std::vector<std::string> getResolutions(const std::string &cameraName) const;
            std::vector<std::string> getFrameRates(const std::string &cameraName,
                                                   const std::string &resolution) const;
    
            void setVideoItem(QQuickItem *item) { videoItem_ = item; }
            QQuickItem *getVideoItem() const { return videoItem_; }
    
    trilene's avatar
    trilene committed
    signals:
    
    trilene's avatar
    trilene committed
            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 &);
    
    trilene's avatar
    trilene committed
            void stateChanged(webrtc::State);
    
    trilene's avatar
    trilene committed
    
    private slots:
    
    trilene's avatar
    trilene committed
            void setState(webrtc::State state) { state_ = state; }
    
    trilene's avatar
    trilene committed
    
    private:
    
    trilene's avatar
    trilene committed
            WebRTCSession();
    
    trilene's avatar
    trilene committed
    
    
    trilene's avatar
    trilene committed
            bool initialised_           = false;
            bool haveVoicePlugins_      = false;
            bool haveVideoPlugins_      = false;
            webrtc::State state_        = webrtc::State::DISCONNECTED;
            bool isVideo_               = false;
            bool isOffering_            = false;
            bool isRemoteVideoRecvOnly_ = false;
            QQuickItem *videoItem_      = nullptr;
            GstElement *pipe_           = nullptr;
            GstElement *webrtc_         = nullptr;
            unsigned int busWatchId_    = 0;
    
    trilene's avatar
    trilene committed
            std::vector<std::string> turnServers_;
    
    
    trilene's avatar
    trilene committed
            bool init(std::string *errorMessage = nullptr);
            bool startPipeline(int opusPayloadType, int vp8PayloadType);
            bool createPipeline(int opusPayloadType, int vp8PayloadType);
            bool addVideoPipeline(int vp8PayloadType);
    
            void startDeviceMonitor();
    
    trilene's avatar
    trilene committed
    
    public:
    
    trilene's avatar
    trilene committed
            WebRTCSession(WebRTCSession const &) = delete;
            void operator=(WebRTCSession const &) = delete;
    
    trilene's avatar
    trilene committed
    };