Overte C++ Documentation
WebRTCDataChannels.h
1 //
2 // WebRTCDataChannels.h
3 // libraries/networking/src/webrtc
4 //
5 // Created by David Rowe on 21 May 2021.
6 // Copyright 2021 Vircadia contributors.
7 //
8 
9 #ifndef overte_WebRTCDataChannels_h
10 #define overte_WebRTCDataChannels_h
11 
12 #include <shared/WebRTC.h>
13 
14 #if defined(WEBRTC_DATA_CHANNELS)
15 
16 
17 #include <QObject>
18 #include <QHash>
19 
20 #undef emit // Avoid conflict between Qt signals/slots and the WebRTC library's.
21 #include <api/peer_connection_interface.h>
22 #define emit
23 
24 #include "../NodeType.h"
25 #include "../SockAddr.h"
26 
27 class WebRTCDataChannels;
28 class WDCConnection;
29 
30 
33 
35 class WDCSetSessionDescriptionObserver : public webrtc::SetSessionDescriptionObserver {
36 public:
37 
39  void OnSuccess() override;
40 
43  void OnFailure(webrtc::RTCError error) override;
44 };
45 
46 
48 class WDCCreateSessionDescriptionObserver : public webrtc::CreateSessionDescriptionObserver {
49 public:
50 
54 
57  void OnSuccess(webrtc::SessionDescriptionInterface* desc) override;
58 
61  void OnFailure(webrtc::RTCError error) override;
62 
63 private:
64  WDCConnection* _parent;
65 };
66 
67 
69 class WDCPeerConnectionObserver : public webrtc::PeerConnectionObserver {
70 public:
71 
75 
78  void OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState newState) override;
79 
81  void OnRenegotiationNeeded() override;
82 
85  void OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState newState) override;
86 
89  void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override;
90 
93  virtual void OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState newState) override;
94 
97  virtual void OnStandardizedIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState newState) override;
98 
101  void OnDataChannel(rtc::scoped_refptr<webrtc::DataChannelInterface> dataChannel) override;
102 
105  void OnConnectionChange(webrtc::PeerConnectionInterface::PeerConnectionState newState) override;
106 
107 private:
108  WDCConnection* _parent;
109 };
110 
111 
113 class WDCDataChannelObserver : public webrtc::DataChannelObserver {
114 public:
115 
119 
121  void OnStateChange() override;
122 
125  void OnMessage(const webrtc::DataBuffer& buffer) override;
126 
127 private:
128  WDCConnection* _parent;
129 };
130 
131 
135 
136 public:
137 
141  WDCConnection(WebRTCDataChannels* parent, const QString& dataChannelID);
142 
145  QString getDataChannelID() const { return _dataChannelID; }
146 
147 
150  void setRemoteDescription(QJsonObject& description);
151 
153  void createAnswer();
154 
157  void sendAnswer(webrtc::SessionDescriptionInterface* description);
158 
161  void setLocalDescription(webrtc::SessionDescriptionInterface* description);
162 
165  void addIceCandidate(QJsonObject& data);
166 
169  void sendIceCandidate(const webrtc::IceCandidateInterface* candidate);
170 
173  void onPeerConnectionStateChanged(webrtc::PeerConnectionInterface::PeerConnectionState state);
174 
177  void onDataChannelOpened(rtc::scoped_refptr<webrtc::DataChannelInterface> dataChannel);
178 
181 
182 
185  void onDataChannelMessageReceived(const webrtc::DataBuffer& buffer);
186 
189  qint64 getBufferedAmount() const;
190 
191 
195  bool sendDataMessage(const webrtc::DataBuffer& buffer);
196 
198  void closePeerConnection();
199 
200 private:
201  WebRTCDataChannels* _parent;
202  QString _dataChannelID;
203 
204  rtc::scoped_refptr<WDCSetSessionDescriptionObserver> _setSessionDescriptionObserver { nullptr };
205  rtc::scoped_refptr<WDCCreateSessionDescriptionObserver> _createSessionDescriptionObserver { nullptr };
206 
207  std::shared_ptr<WDCDataChannelObserver> _dataChannelObserver { nullptr };
208  rtc::scoped_refptr<webrtc::DataChannelInterface> _dataChannel { nullptr };
209 
210  std::shared_ptr<WDCPeerConnectionObserver> _peerConnectionObserver { nullptr };
211  rtc::scoped_refptr<webrtc::PeerConnectionInterface> _peerConnection { nullptr };
212 };
213 
214 
230 class WebRTCDataChannels : public QObject {
231  Q_OBJECT
232 
233 public:
234 
237  WebRTCDataChannels(QObject* parent);
238 
241 
245  return _nodeType;
246  }
247 
249  void reset();
250 
254  void onDataChannelOpened(WDCConnection* connection, const QString& dataChannelID);
255 
258  void sendSignalingMessage(const QJsonObject& message);
259 
263  void emitDataMessage(const QString& dataChannelID, const QByteArray& byteArray);
264 
269  bool sendDataMessage(const SockAddr& destination, const QByteArray& message);
270 
274  qint64 getBufferedAmount(const SockAddr& address) const;
275 
279  rtc::scoped_refptr<webrtc::PeerConnectionInterface> createPeerConnection(
280  const std::shared_ptr<WDCPeerConnectionObserver> peerConnectionObserver);
281 
287  void closePeerConnection(WDCConnection* connection);
288 
289 public slots:
290 
293  void onSignalingMessage(const QJsonObject& message);
294 
298  void closePeerConnectionNow(WDCConnection* connection);
299 
300 signals:
301 
305  void signalingMessage(const QJsonObject& message);
306 
311  void dataMessage(const SockAddr& address, const QByteArray& byteArray);
312 
317 
318 private:
319 
320  QObject* _parent;
321 
322  NodeType_t _nodeType { NodeType::Unassigned };
323 
324  std::unique_ptr<rtc::Thread> _rtcNetworkThread { nullptr };
325  std::unique_ptr<rtc::Thread> _rtcWorkerThread { nullptr };
326  std::unique_ptr<rtc::Thread> _rtcSignalingThread { nullptr };
327 
328  rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> _peerConnectionFactory { nullptr };
329 
330  QHash<QString, WDCConnection*> _connectionsByID; // <client data channel ID, WDCConnection>
331  // The client's WebSocket IP and port is used as the data channel ID to uniquely identify each.
332  // The WebSocket IP address and port is formatted as "n.n.n.n:n", the same as used in WebRTCSignalingServer.
333 };
334 
335 
337 
338 #endif // WEBRTC_DATA_CHANNELS
339 
340 #endif // overte_WebRTCDataChannels_h
quint8 NodeType_t
An 8-bit value identifying the type of a node - domain server, audio mixer, etc.
Definition: NodeType.h:22
A WebRTC data channel connection.
Definition: WebRTCDataChannels.h:134
void closePeerConnection()
Closes the WebRTC peer connection.
Definition: WebRTCDataChannels.cpp:422
void addIceCandidate(QJsonObject &data)
Adds an ICE candidate received from the remote client via the signaling channel.
Definition: WebRTCDataChannels.cpp:265
void setRemoteDescription(QJsonObject &description)
Sets the remote session description received from the remote client via the signaling channel.
Definition: WebRTCDataChannels.cpp:204
void onDataChannelStateChanged()
Handles a change in the state of the WebRTC data channel.
Definition: WebRTCDataChannels.cpp:351
void setLocalDescription(webrtc::SessionDescriptionInterface *description)
Sets the local session description on the WebRTC data channel being connected.
Definition: WebRTCDataChannels.cpp:257
QString getDataChannelID() const
Gets the data channel ID.
Definition: WebRTCDataChannels.h:145
void sendIceCandidate(const webrtc::IceCandidateInterface *candidate)
Sends an ICE candidate to the remote client via the signaling channel.
Definition: WebRTCDataChannels.cpp:288
WDCConnection(WebRTCDataChannels *parent, const QString &dataChannelID)
Constructs a new WDCConnection and opens a WebRTC data connection.
Definition: WebRTCDataChannels.cpp:186
void onDataChannelMessageReceived(const webrtc::DataBuffer &buffer)
Handles a message being received on the WebRTC data channel.
Definition: WebRTCDataChannels.cpp:368
void createAnswer()
Creates an answer to an offer received from the remote client via the signaling channel.
Definition: WebRTCDataChannels.cpp:226
qint64 getBufferedAmount() const
Gets the number of bytes waiting to be sent on the WebRTC data channel.
Definition: WebRTCDataChannels.cpp:393
bool sendDataMessage(const webrtc::DataBuffer &buffer)
Sends a message on the WebRTC data channel.
Definition: WebRTCDataChannels.cpp:402
void onDataChannelOpened(rtc::scoped_refptr< webrtc::DataChannelInterface > dataChannel)
Handles the WebRTC data channel being opened.
Definition: WebRTCDataChannels.cpp:329
void sendAnswer(webrtc::SessionDescriptionInterface *description)
Sends an answer to the remote client via the signaling channel.
Definition: WebRTCDataChannels.cpp:234
void onPeerConnectionStateChanged(webrtc::PeerConnectionInterface::PeerConnectionState state)
Monitors the peer connection state.
Definition: WebRTCDataChannels.cpp:315
A WebRTC create session description observer.
Definition: WebRTCDataChannels.h:48
void OnSuccess(webrtc::SessionDescriptionInterface *desc) override
The call to CreateAnswer succeeded.
Definition: WebRTCDataChannels.cpp:53
void OnFailure(webrtc::RTCError error) override
The call to CreateAnswer failed.
Definition: WebRTCDataChannels.cpp:61
WDCCreateSessionDescriptionObserver(WDCConnection *parent)
Constructs a session description observer.
Definition: WebRTCDataChannels.cpp:49
A WebRTC data channel observer.
Definition: WebRTCDataChannels.h:113
void OnStateChange() override
The data channel state changed.
Definition: WebRTCDataChannels.cpp:171
void OnMessage(const webrtc::DataBuffer &buffer) override
A data channel message was received.
Definition: WebRTCDataChannels.cpp:178
WDCDataChannelObserver(WDCConnection *parent)
Constructs a data channel observer.
Definition: WebRTCDataChannels.cpp:167
A WebRTC peer connection observer.
Definition: WebRTCDataChannels.h:69
void OnRenegotiationNeeded() override
Called when renegotiation is needed. For example, an ICE restart has begun.
Definition: WebRTCDataChannels.cpp:86
void OnDataChannel(rtc::scoped_refptr< webrtc::DataChannelInterface > dataChannel) override
Called when a remote peer opens a data channel.
Definition: WebRTCDataChannels.cpp:143
void OnConnectionChange(webrtc::PeerConnectionInterface::PeerConnectionState newState) override
Called when the peer connection state changes.
Definition: WebRTCDataChannels.cpp:150
void OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState newState) override
Called when the ICE gather state changes.
Definition: WebRTCDataChannels.cpp:92
virtual void OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState newState) override
Called when the legacy ICE connection state changes.
Definition: WebRTCDataChannels.cpp:110
void OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState newState) override
Called when the SignalingState changes.
Definition: WebRTCDataChannels.cpp:72
virtual void OnStandardizedIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState newState) override
Called when the standards-compliant ICE connection state changes.
Definition: WebRTCDataChannels.cpp:126
void OnIceCandidate(const webrtc::IceCandidateInterface *candidate) override
Called when a new ICE candidate has been gathered.
Definition: WebRTCDataChannels.cpp:103
WDCPeerConnectionObserver(WDCConnection *parent)
Constructs a peer connection observer.
Definition: WebRTCDataChannels.cpp:68
A WebRTC session description observer.
Definition: WebRTCDataChannels.h:35
void OnSuccess() override
The call to SetLocalDescription or SetRemoteDescription succeeded.
Definition: WebRTCDataChannels.cpp:36
void OnFailure(webrtc::RTCError error) override
The call to SetLocalDescription or SetRemoteDescription failed.
Definition: WebRTCDataChannels.cpp:42
Manages WebRTC data channels on the domain server or an assignment client that Interface clients can ...
Definition: WebRTCDataChannels.h:230
void closePeerConnection(WDCConnection *connection)
Initiates closing the peer connection for a WebRTC data channel.
Definition: WebRTCDataChannels.cpp:624
void sendSignalingMessage(const QJsonObject &message)
Emits a signalingMessage to be sent to the Interface client.
Definition: WebRTCDataChannels.cpp:550
rtc::scoped_refptr< webrtc::PeerConnectionInterface > createPeerConnection(const std::shared_ptr< WDCPeerConnectionObserver > peerConnectionObserver)
Creates a new WebRTC peer connection for connecting to an Interface client.
Definition: WebRTCDataChannels.cpp:599
NodeType_t getNodeType()
Gets the type of node that the WebRTCDataChannels object is being used in.
Definition: WebRTCDataChannels.h:244
void onDataChannelOpened(WDCConnection *connection, const QString &dataChannelID)
Handles a WebRTC data channel opening.
Definition: WebRTCDataChannels.cpp:493
bool sendDataMessage(const SockAddr &destination, const QByteArray &message)
Sends a data message to an Interface client.
Definition: WebRTCDataChannels.cpp:571
WebRTCDataChannels(QObject *parent)
Constructs a new WebRTCDataChannels object.
Definition: WebRTCDataChannels.cpp:435
void signalingMessage(const QJsonObject &message)
A WebRTC signaling message to be sent to the Interface client.
~WebRTCDataChannels()
Destroys a WebRTCDataChannels object.
Definition: WebRTCDataChannels.cpp:467
void emitDataMessage(const QString &dataChannelID, const QByteArray &byteArray)
Emits a dataMessage received from the Interface client.
Definition: WebRTCDataChannels.cpp:557
qint64 getBufferedAmount(const SockAddr &address) const
Gets the number of bytes waiting to be sent on a data channel.
Definition: WebRTCDataChannels.cpp:587
void closePeerConnectionSoon(WDCConnection *connection)
Signals that the peer connection for a WebRTC data channel should be closed.
void dataMessage(const SockAddr &address, const QByteArray &byteArray)
A WebRTC data message received from the Interface client.
void closePeerConnectionNow(WDCConnection *connection)
Closes the peer connection for a WebRTC data channel.
Definition: WebRTCDataChannels.cpp:635
void onSignalingMessage(const QJsonObject &message)
Handles a WebRTC signaling message received from the Interface client.
Definition: WebRTCDataChannels.cpp:500
void reset()
Immediately closes all connections and resets the socket.
Definition: WebRTCDataChannels.cpp:481