summary refs log tree commit diff
path: root/src/WebRTCSession.h
blob: 56d76fa83b0d81d629eb2cf9b5c1a4b2dac3895f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#pragma once

#include <string>
#include <vector>

#include <QObject>

#include "mtx/events/voip.hpp"

typedef struct _GList GList;
typedef struct _GstElement GstElement;

class WebRTCSession : public QObject
{
        Q_OBJECT

public:
        enum class State
        {
                DISCONNECTED,
                ICEFAILED,
                INITIATING,
                INITIATED,
                OFFERSENT,
                ANSWERSENT,
                CONNECTING,
                CONNECTED
        };

        static WebRTCSession &instance()
        {
                static WebRTCSession instance;
                return instance;
        }

        bool init(std::string *errorMessage = nullptr);
        State state() const { return state_; }

        bool createOffer();
        bool acceptOffer(const std::string &sdp);
        bool acceptAnswer(const std::string &sdp);
        void acceptICECandidates(const std::vector<mtx::events::msg::CallCandidates::Candidate> &);

        bool toggleMuteAudioSrc(bool &isMuted);
        void end();

        void setStunServer(const std::string &stunServer) { stunServer_ = stunServer; }
        void setTurnServers(const std::vector<std::string> &uris) { turnServers_ = uris; }

        std::vector<std::string> getAudioSourceNames(const std::string &defaultDevice);
        void setAudioSource(int audioDeviceIndex) { audioSourceIndex_ = audioDeviceIndex; }

signals:
        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(WebRTCSession::State); // explicit qualifier necessary for Qt

private slots:
        void setState(State state) { state_ = state; }

private:
        WebRTCSession();

        bool initialised_   = false;
        State state_        = State::DISCONNECTED;
        GstElement *pipe_   = nullptr;
        GstElement *webrtc_ = nullptr;
        std::string stunServer_;
        std::vector<std::string> turnServers_;
        GList *audioSources_  = nullptr;
        int audioSourceIndex_ = -1;

        bool startPipeline(int opusPayloadType);
        bool createPipeline(int opusPayloadType);
        void refreshDevices();

public:
        WebRTCSession(WebRTCSession const &) = delete;
        void operator=(WebRTCSession const &) = delete;
};