Skip to content
Snippets Groups Projects
WebRTCSession.h 2.53 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;
    
    class WebRTCSession : public QObject
    {
            Q_OBJECT
    
    public:
    
    trilene's avatar
    trilene committed
            enum class State
            {
                    DISCONNECTED,
                    ICEFAILED,
                    INITIATING,
                    INITIATED,
                    OFFERSENT,
                    ANSWERSENT,
                    CONNECTING,
                    CONNECTED
    
    trilene's avatar
    trilene committed
            };
    
    
    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
            }
    
            bool init(std::string *errorMessage = nullptr);
    
    trilene's avatar
    trilene committed
            State state() const { return state_; }
    
    trilene's avatar
    trilene committed
    
            bool createOffer();
            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 toggleMuteAudioSrc(bool &isMuted);
            void end();
    
    
    trilene's avatar
    trilene committed
            void setStunServer(const std::string &stunServer) { stunServer_ = stunServer; }
            void setTurnServers(const std::vector<std::string> &uris) { turnServers_ = uris; }
    
    trilene's avatar
    trilene committed
    
    
            std::vector<std::string> getAudioSourceNames(const std::string &defaultDevice);
            void setAudioSource(int audioDeviceIndex) { audioSourceIndex_ = audioDeviceIndex; }
    
    
    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(WebRTCSession::State); // explicit qualifier necessary for Qt
    
    private slots:
    
    trilene's avatar
    trilene committed
            void setState(State state) { state_ = state; }
    
    trilene's avatar
    trilene committed
    
    private:
    
    trilene's avatar
    trilene committed
            WebRTCSession();
    
    trilene's avatar
    trilene committed
    
    
            bool initialised_        = false;
            State state_             = State::DISCONNECTED;
            GstElement *pipe_        = nullptr;
            GstElement *webrtc_      = nullptr;
            unsigned int busWatchId_ = 0;
    
    trilene's avatar
    trilene committed
            std::string stunServer_;
            std::vector<std::string> turnServers_;
    
            int audioSourceIndex_ = -1;
    
    trilene's avatar
    trilene committed
    
            bool startPipeline(int opusPayloadType);
            bool createPipeline(int opusPayloadType);
    
            void refreshDevices();
    
            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
    };