-
-
Notifications
You must be signed in to change notification settings - Fork 391
/
Copy pathicetransport.hpp
127 lines (100 loc) · 3.98 KB
/
icetransport.hpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Copyright (c) 2019-2020 Paul-Louis Ageneau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#ifndef RTC_IMPL_ICE_TRANSPORT_H
#define RTC_IMPL_ICE_TRANSPORT_H
#include "candidate.hpp"
#include "common.hpp"
#include "configuration.hpp"
#include "description.hpp"
#include "global.hpp"
#include "peerconnection.hpp"
#include "transport.hpp"
#if !USE_NICE
#include <juice/juice.h>
#else
#include <nice/agent.h>
#endif
#include <atomic>
#include <chrono>
#include <mutex>
#include <thread>
namespace rtc::impl {
class IceTransport : public Transport {
public:
static void Init();
static void Cleanup();
enum class GatheringState { New = 0, InProgress = 1, Complete = 2 };
using candidate_callback = std::function<void(const Candidate &candidate)>;
using gathering_state_callback = std::function<void(GatheringState state)>;
IceTransport(const Configuration &config, candidate_callback candidateCallback,
state_callback stateChangeCallback,
gathering_state_callback gatheringStateChangeCallback);
~IceTransport();
Description::Role role() const;
GatheringState gatheringState() const;
Description getLocalDescription(Description::Type type) const;
void setRemoteDescription(const Description &description);
bool addRemoteCandidate(const Candidate &candidate);
void gatherLocalCandidates(string mid, std::vector<IceServer> additionalIceServers = {});
void setIceAttributes(string uFrag, string pwd);
optional<string> getLocalAddress() const;
optional<string> getRemoteAddress() const;
bool send(message_ptr message) override; // false if dropped
bool getSelectedCandidatePair(Candidate *local, Candidate *remote);
private:
bool outgoing(message_ptr message) override;
void changeGatheringState(GatheringState state);
void processStateChange(unsigned int state);
void processCandidate(const string &candidate);
void processGatheringDone();
void processTimeout();
void addIceServer(IceServer server);
Description::Role mRole;
string mMid;
std::chrono::milliseconds mTrickleTimeout;
std::atomic<GatheringState> mGatheringState;
candidate_callback mCandidateCallback;
gathering_state_callback mGatheringStateChangeCallback;
#if !USE_NICE
unique_ptr<juice_agent_t, void (*)(juice_agent_t *)> mAgent;
int mTurnServersAdded = 0;
static void StateChangeCallback(juice_agent_t *agent, juice_state_t state, void *user_ptr);
static void CandidateCallback(juice_agent_t *agent, const char *sdp, void *user_ptr);
static void GatheringDoneCallback(juice_agent_t *agent, void *user_ptr);
static void RecvCallback(juice_agent_t *agent, const char *data, size_t size, void *user_ptr);
static void LogCallback(juice_log_level_t level, const char *message);
#else
class MainLoopWrapper {
public:
MainLoopWrapper();
~MainLoopWrapper();
GMainLoop *get() const;
private:
unique_ptr<GMainLoop, void (*)(GMainLoop *)> mMainLoop;
std::thread mThread;
};
static MainLoopWrapper *MainLoop;
unique_ptr<NiceAgent, void (*)(NiceAgent *)> mNiceAgent;
uint32_t mStreamId = 0;
guint mTimeoutId = 0;
std::mutex mOutgoingMutex;
unsigned int mOutgoingDscp;
static string AddressToString(const NiceAddress &addr);
static void CandidateCallback(NiceAgent *agent, NiceCandidate *candidate, gpointer userData);
static void GatheringDoneCallback(NiceAgent *agent, guint streamId, gpointer userData);
static void StateChangeCallback(NiceAgent *agent, guint streamId, guint componentId,
guint state, gpointer userData);
static void RecvCallback(NiceAgent *agent, guint stream_id, guint component_id, guint len,
gchar *buf, gpointer userData);
static gboolean TimeoutCallback(gpointer userData);
static void LogCallback(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message,
gpointer user_data);
#endif
};
} // namespace rtc::impl
#endif