OpenTera WebRTC API (C++) 1.2.5
WebrtcClient.h
1 #ifndef OPENTERA_WEBRTC_NATIVE_CLIENT_WEBRTC_CLIENT_H
2 #define OPENTERA_WEBRTC_NATIVE_CLIENT_WEBRTC_CLIENT_H
3 
4 #include <OpenteraWebrtcNativeClient/Configurations/WebrtcConfiguration.h>
5 #include <OpenteraWebrtcNativeClient/Configurations/VideoStreamConfiguration.h>
6 #include <OpenteraWebrtcNativeClient/Signaling/SignalingClient.h>
7 #include <OpenteraWebrtcNativeClient/Utils/ClassMacro.h>
8 #include <OpenteraWebrtcNativeClient/Utils/Client.h>
9 #include <OpenteraWebrtcNativeClient/Utils/IceServer.h>
10 #include <OpenteraWebrtcNativeClient/Handlers/PeerConnectionHandler.h>
11 #include <OpenteraWebrtcNativeClient/Utils/FunctionTask.h>
12 #include <OpenteraWebrtcNativeClient/OpenteraAudioDeviceModule.h>
13 
14 #include <api/peer_connection_interface.h>
15 #include <api/scoped_refptr.h>
16 
17 #include <functional>
18 #include <map>
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 namespace opentera
24 {
29  {
30  std::unique_ptr<rtc::Thread> m_internalClientThread;
31 
32  WebrtcConfiguration m_webrtcConfiguration;
33 
34  std::map<std::string, Client> m_roomClientsById;
35  std::vector<std::string> m_alreadyAcceptedCalls;
36 
37  std::function<void()> m_onSignalingConnectionOpened;
38  std::function<void()> m_onSignalingConnectionClosed;
39  std::function<void(const std::string&)> m_onSignalingConnectionError;
40 
41  std::function<void(const std::vector<RoomClient>&)> m_onRoomClientsChanged;
42 
43  std::function<bool(const Client&)> m_callAcceptor;
44  std::function<void(const Client&)> m_onCallRejected;
45 
46  std::function<void(const Client&)> m_onClientConnected;
47  std::function<void(const Client&)> m_onClientDisconnected;
48  std::function<void(const Client&)> m_onClientConnectionFailed;
49 
50  std::function<void(const std::string& error)> m_onError;
51 
52  std::function<void(const std::string& log)> m_logger;
53 
54  std::unique_ptr<rtc::Thread> m_networkThread;
55  std::unique_ptr<rtc::Thread> m_workerThread;
56  std::unique_ptr<rtc::Thread> m_signalingThread;
57 
58  bool m_destructorCalled;
59 
60  protected:
61  std::unique_ptr<SignalingClient> m_signalingClient;
62 
63  rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> m_peerConnectionFactory;
64  std::map<std::string, std::unique_ptr<PeerConnectionHandler>> m_peerConnectionHandlersById;
65 
66  rtc::scoped_refptr<OpenteraAudioDeviceModule> m_audioDeviceModule;
67  rtc::scoped_refptr<webrtc::AudioProcessing> m_audioProcessing;
68 
69  public:
71  SignalingServerConfiguration&& signalingServerConfiguration,
72  WebrtcConfiguration&& webrtcConfiguration,
73  VideoStreamConfiguration&& videoStreamConfiguration);
74  virtual ~WebrtcClient();
75 
76  DECLARE_NOT_COPYABLE(WebrtcClient);
77  DECLARE_NOT_MOVABLE(WebrtcClient);
78 
79  void setTlsVerificationEnabled(bool isEnabled);
80 
81  void connect();
82  void close();
83  void closeSync();
84 
85  void callAll();
86  void callIds(const std::vector<std::string>& ids);
87 
88  void hangUpAll();
90 
91  bool isConnected();
92  bool isRtcConnected();
93  std::string id();
94 
95  std::vector<std::string> getConnectedRoomClientIds();
96 
97  RoomClient getRoomClient(const std::string& id);
98  std::vector<RoomClient> getRoomClients();
99 
100  void setOnSignalingConnectionOpened(const std::function<void()>& callback);
101  void setOnSignalingConnectionClosed(const std::function<void()>& callback);
102  void setOnSignalingConnectionError(const std::function<void(const std::string&)>& callback);
103 
104  void setOnRoomClientsChanged(const std::function<void(const std::vector<RoomClient>&)>& callback);
105 
106  void setCallAcceptor(const std::function<bool(const Client&)>& callback);
107  void setOnCallRejected(const std::function<void(const Client&)>& callback);
108 
109  void setOnClientConnected(const std::function<void(const Client&)>& callback);
110  void setOnClientDisconnected(const std::function<void(const Client&)>& callback);
111  void setOnClientConnectionFailed(const std::function<void(const Client&)>& callback);
112 
113  void setOnError(const std::function<void(const std::string& error)>& callback);
114 
115  void setLogger(const std::function<void(const std::string& message)>& callback);
116 
117  protected:
118  template<class T, class... Types>
119  void invokeIfCallable(const std::function<T>& f, Types... args);
120 
121  void log(const std::string& message);
122 
123  virtual std::unique_ptr<PeerConnectionHandler>
124  createPeerConnectionHandler(const std::string& id, const Client& peerClient, bool isCaller) = 0;
125 
126  std::function<void(const std::string&)> getOnErrorFunction();
127  std::function<void(const Client&)> getOnClientConnectedFunction();
128  std::function<void(const Client&)> getOnClientDisconnectedFunction();
129  std::function<void(const Client&)> getOnClientConnectionFailedFunction();
130 
131  rtc::Thread* getInternalClientThread();
132 
133  private:
134  void connectSignalingClientCallbacks();
135 
136  void makePeerCall(const std::string& id);
137  void receivePeerCall(const std::string& fromId, const std::string& sdp);
138  void receivePeerCallAnswer(const std::string& fromId, const std::string& sdp);
139  void receiveIceCandidate(
140  const std::string& fromId,
141  const std::string& sdpMid,
142  int sdpMLineIndex,
143  const std::string& sdp);
144 
145  void closeAllConnections();
146 
147  bool getCallAcceptance(const std::string& id);
148 
149  std::unique_ptr<PeerConnectionHandler>
150  createConnection(const std::string& peerId, const Client& peerClient, bool isCaller);
151  void removeConnection(const std::string& id);
152  };
153 
159  {
160  return callSync(m_internalClientThread.get(), [this]() { return m_signalingClient->isConnected(); });
161  }
162 
168  {
169  return callSync(m_internalClientThread.get(), [this]() { return !m_peerConnectionHandlersById.empty(); });
170  }
171 
176  inline std::string WebrtcClient::id()
177  {
178  return callSync(m_internalClientThread.get(), [this]() { return m_signalingClient->sessionId(); });
179  }
180 
188  inline RoomClient WebrtcClient::getRoomClient(const std::string& id)
189  {
190  return callSync(
191  m_internalClientThread.get(),
192  [this, &id]()
193  {
194  auto clientIt = m_roomClientsById.find(id);
195  if (clientIt != m_roomClientsById.end())
196  {
197  const auto& client = clientIt->second;
198  bool isConnected =
199  m_peerConnectionHandlersById.find(client.id()) != m_peerConnectionHandlersById.end() ||
200  client.id() == this->id();
201  return RoomClient(client, isConnected);
202  }
203  else
204  {
205  return RoomClient();
206  }
207  });
208  }
209 
217  inline void WebrtcClient::setOnSignalingConnectionOpened(const std::function<void()>& callback)
218  {
219  callSync(m_internalClientThread.get(), [this, &callback]() { m_onSignalingConnectionOpened = callback; });
220  }
221 
229  inline void WebrtcClient::setOnSignalingConnectionClosed(const std::function<void()>& callback)
230  {
231  callSync(m_internalClientThread.get(), [this, &callback]() { m_onSignalingConnectionClosed = callback; });
232  }
233 
246  inline void WebrtcClient::setOnSignalingConnectionError(const std::function<void(const std::string&)>& callback)
247  {
248  callSync(m_internalClientThread.get(), [this, &callback]() { m_onSignalingConnectionError = callback; });
249  }
250 
263  inline void
264  WebrtcClient::setOnRoomClientsChanged(const std::function<void(const std::vector<RoomClient>&)>& callback)
265  {
266  callSync(m_internalClientThread.get(), [this, &callback]() { m_onRoomClientsChanged = callback; });
267  }
268 
284  inline void WebrtcClient::setCallAcceptor(const std::function<bool(const Client&)>& callback)
285  {
286  callSync(m_internalClientThread.get(), [this, &callback]() { m_callAcceptor = callback; });
287  }
288 
301  inline void WebrtcClient::setOnCallRejected(const std::function<void(const Client&)>& callback)
302  {
303  callSync(m_internalClientThread.get(), [this, &callback]() { m_onCallRejected = callback; });
304  }
305 
318  inline void WebrtcClient::setOnClientConnected(const std::function<void(const Client&)>& callback)
319  {
320  callSync(m_internalClientThread.get(), [this, &callback]() { m_onClientConnected = callback; });
321  }
322 
335  inline void WebrtcClient::setOnClientDisconnected(const std::function<void(const Client&)>& callback)
336  {
337  callSync(m_internalClientThread.get(), [this, &callback]() { m_onClientDisconnected = callback; });
338  }
339 
352  inline void WebrtcClient::setOnClientConnectionFailed(const std::function<void(const Client&)>& callback)
353  {
354  callSync(m_internalClientThread.get(), [this, &callback]() { m_onClientConnectionFailed = callback; });
355  }
356 
369  inline void WebrtcClient::setOnError(const std::function<void(const std::string& error)>& callback)
370  {
371  callSync(m_internalClientThread.get(), [this, &callback]() { m_onError = callback; });
372  }
373 
386  inline void WebrtcClient::setLogger(const std::function<void(const std::string& message)>& callback)
387  {
388  callSync(m_internalClientThread.get(), [this, &callback]() { m_logger = callback; });
389  }
390 
391  template<class T, class... Types>
392  void WebrtcClient::invokeIfCallable(const std::function<T>& f, Types... args)
393  {
394  callAsync(
395  m_internalClientThread.get(),
396  [=]()
397  {
398  if (f)
399  {
400  f(args...);
401  }
402  });
403  }
404 
405  inline void WebrtcClient::log(const std::string& message)
406  {
407  callAsync(
408  m_internalClientThread.get(),
409  [=]()
410  {
411  if (m_logger)
412  {
413  m_logger(message);
414  }
415  });
416  }
417 
418  inline rtc::Thread* WebrtcClient::getInternalClientThread() { return m_internalClientThread.get(); }
419 }
420 
421 #endif
Represents a peer client.
Definition: Client.h:14
Represents a room client.
Definition: Client.h:68
Represents a signaling server configuration.
Definition: SignalingServerConfiguration.h:14
Represents a video stream configuration.
Definition: VideoStreamConfiguration.h:26
Represents the base class of DataChannelClient and StreamClient.
Definition: WebrtcClient.h:29
void callAll()
Calls all room clients.
Definition: WebrtcClient.cpp:120
RoomClient getRoomClient(const std::string &id)
Returns the room client that matches with the specified id. If no room client matches with the id,...
Definition: WebrtcClient.h:188
void setOnClientDisconnected(const std::function< void(const Client &)> &callback)
Sets the callback that is called when a client peer connection closes.
Definition: WebrtcClient.h:335
void setOnRoomClientsChanged(const std::function< void(const std::vector< RoomClient > &)> &callback)
Sets the callback that is called when the room client changes.
Definition: WebrtcClient.h:264
void closeSync()
Closes all client connections (synchronous).
Definition: WebrtcClient.cpp:106
std::vector< std::string > getConnectedRoomClientIds()
Returns the connected room client ids.
Definition: WebrtcClient.cpp:177
void closeAllRoomPeerConnections()
Closes all room peer connections.
Definition: WebrtcClient.cpp:168
bool isRtcConnected()
Indicates if the client is connected to at least one client (RTCPeerConnection).
Definition: WebrtcClient.h:167
void setOnSignalingConnectionClosed(const std::function< void()> &callback)
Sets the callback that is called when the signaling connection closes.
Definition: WebrtcClient.h:229
void hangUpAll()
Hangs up all clients.
Definition: WebrtcClient.cpp:154
void setLogger(const std::function< void(const std::string &message)> &callback)
Sets the callback that is used to log information.
Definition: WebrtcClient.h:386
void setTlsVerificationEnabled(bool isEnabled)
Definition: WebrtcClient.cpp:68
bool isConnected()
Indicates if the client is connected to the signaling server.
Definition: WebrtcClient.h:158
void setOnClientConnected(const std::function< void(const Client &)> &callback)
Sets the callback that is called when a client peer connection opens.
Definition: WebrtcClient.h:318
std::vector< RoomClient > getRoomClients()
Returns the room clients.
Definition: WebrtcClient.cpp:197
void setOnClientConnectionFailed(const std::function< void(const Client &)> &callback)
Sets the callback that is called when a client peer connection fails.
Definition: WebrtcClient.h:352
std::string id()
Returns the client id.
Definition: WebrtcClient.h:176
void setOnSignalingConnectionOpened(const std::function< void()> &callback)
Sets the callback that is called when the signaling connection opens.
Definition: WebrtcClient.h:217
void close()
Closes all client connections (asynchronous).
Definition: WebrtcClient.cpp:92
void setOnSignalingConnectionError(const std::function< void(const std::string &)> &callback)
Sets the callback that is called when a signaling connection error occurs.
Definition: WebrtcClient.h:246
void connect()
Connects the client the signaling server.
Definition: WebrtcClient.cpp:78
void setOnCallRejected(const std::function< void(const Client &)> &callback)
Sets the callback that is called when a call is rejected.
Definition: WebrtcClient.h:301
void callIds(const std::vector< std::string > &ids)
Calls the specified clients.
Definition: WebrtcClient.cpp:140
void setCallAcceptor(const std::function< bool(const Client &)> &callback)
Sets the callback that is used to accept or reject a call.
Definition: WebrtcClient.h:284
void setOnError(const std::function< void(const std::string &error)> &callback)
Sets the callback that is called when an error occurs.
Definition: WebrtcClient.h:369
Represents a WebRTC peer connection configuration.
Definition: WebrtcConfiguration.h:14