1 #ifndef OPENTERA_WEBRTC_NATIVE_CLIENT_WEBRTC_CLIENT_H
2 #define OPENTERA_WEBRTC_NATIVE_CLIENT_WEBRTC_CLIENT_H
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>
14 #include <api/peer_connection_interface.h>
15 #include <api/scoped_refptr.h>
30 std::unique_ptr<rtc::Thread> m_internalClientThread;
34 std::map<std::string, Client> m_roomClientsById;
35 std::vector<std::string> m_alreadyAcceptedCalls;
37 std::function<void()> m_onSignalingConnectionOpened;
38 std::function<void()> m_onSignalingConnectionClosed;
39 std::function<void(
const std::string&)> m_onSignalingConnectionError;
41 std::function<void(
const std::vector<RoomClient>&)> m_onRoomClientsChanged;
43 std::function<bool(
const Client&)> m_callAcceptor;
44 std::function<void(
const Client&)> m_onCallRejected;
46 std::function<void(
const Client&)> m_onClientConnected;
47 std::function<void(
const Client&)> m_onClientDisconnected;
48 std::function<void(
const Client&)> m_onClientConnectionFailed;
50 std::function<void(
const std::string& error)> m_onError;
52 std::function<void(
const std::string& log)> m_logger;
54 std::unique_ptr<rtc::Thread> m_networkThread;
55 std::unique_ptr<rtc::Thread> m_workerThread;
56 std::unique_ptr<rtc::Thread> m_signalingThread;
58 bool m_destructorCalled;
61 std::unique_ptr<SignalingClient> m_signalingClient;
63 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> m_peerConnectionFactory;
64 std::map<std::string, std::unique_ptr<PeerConnectionHandler>> m_peerConnectionHandlersById;
66 rtc::scoped_refptr<OpenteraAudioDeviceModule> m_audioDeviceModule;
67 rtc::scoped_refptr<webrtc::AudioProcessing> m_audioProcessing;
86 void callIds(
const std::vector<std::string>& ids);
113 void setOnError(
const std::function<
void(
const std::string& error)>& callback);
115 void setLogger(
const std::function<
void(
const std::string& message)>& callback);
118 template<
class T,
class... Types>
119 void invokeIfCallable(
const std::function<T>& f, Types... args);
121 void log(
const std::string& message);
123 virtual std::unique_ptr<PeerConnectionHandler>
124 createPeerConnectionHandler(
const std::string&
id,
const Client& peerClient,
bool isCaller) = 0;
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();
131 rtc::Thread* getInternalClientThread();
134 void connectSignalingClientCallbacks();
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,
143 const std::string& sdp);
145 void closeAllConnections();
147 bool getCallAcceptance(
const std::string&
id);
149 std::unique_ptr<PeerConnectionHandler>
150 createConnection(
const std::string& peerId,
const Client& peerClient,
bool isCaller);
151 void removeConnection(
const std::string&
id);
160 return callSync(m_internalClientThread.get(), [
this]() { return m_signalingClient->isConnected(); });
169 return callSync(m_internalClientThread.get(), [
this]() { return !m_peerConnectionHandlersById.empty(); });
178 return callSync(m_internalClientThread.get(), [
this]() { return m_signalingClient->sessionId(); });
191 m_internalClientThread.get(),
194 auto clientIt = m_roomClientsById.find(id);
195 if (clientIt != m_roomClientsById.end())
197 const auto& client = clientIt->second;
199 m_peerConnectionHandlersById.find(client.id()) != m_peerConnectionHandlersById.end() ||
200 client.id() == this->id();
201 return RoomClient(client, isConnected);
217 inline void WebrtcClient::setOnSignalingConnectionOpened(
const std::function<
void()>& callback)
219 callSync(m_internalClientThread.get(), [
this, &callback]() { m_onSignalingConnectionOpened = callback; });
229 inline void WebrtcClient::setOnSignalingConnectionClosed(
const std::function<
void()>& callback)
231 callSync(m_internalClientThread.get(), [
this, &callback]() { m_onSignalingConnectionClosed = callback; });
246 inline void WebrtcClient::setOnSignalingConnectionError(
const std::function<
void(
const std::string&)>& callback)
248 callSync(m_internalClientThread.get(), [
this, &callback]() { m_onSignalingConnectionError = callback; });
264 WebrtcClient::setOnRoomClientsChanged(
const std::function<
void(
const std::vector<RoomClient>&)>& callback)
266 callSync(m_internalClientThread.get(), [
this, &callback]() { m_onRoomClientsChanged = callback; });
284 inline void WebrtcClient::setCallAcceptor(
const std::function<
bool(
const Client&)>& callback)
286 callSync(m_internalClientThread.get(), [
this, &callback]() { m_callAcceptor = callback; });
301 inline void WebrtcClient::setOnCallRejected(
const std::function<
void(
const Client&)>& callback)
303 callSync(m_internalClientThread.get(), [
this, &callback]() { m_onCallRejected = callback; });
318 inline void WebrtcClient::setOnClientConnected(
const std::function<
void(
const Client&)>& callback)
320 callSync(m_internalClientThread.get(), [
this, &callback]() { m_onClientConnected = callback; });
335 inline void WebrtcClient::setOnClientDisconnected(
const std::function<
void(
const Client&)>& callback)
337 callSync(m_internalClientThread.get(), [
this, &callback]() { m_onClientDisconnected = callback; });
352 inline void WebrtcClient::setOnClientConnectionFailed(
const std::function<
void(
const Client&)>& callback)
354 callSync(m_internalClientThread.get(), [
this, &callback]() { m_onClientConnectionFailed = callback; });
369 inline void WebrtcClient::setOnError(
const std::function<
void(
const std::string& error)>& callback)
371 callSync(m_internalClientThread.get(), [
this, &callback]() { m_onError = callback; });
386 inline void WebrtcClient::setLogger(
const std::function<
void(
const std::string& message)>& callback)
388 callSync(m_internalClientThread.get(), [
this, &callback]() { m_logger = callback; });
391 template<
class T,
class... Types>
392 void WebrtcClient::invokeIfCallable(
const std::function<T>& f, Types... args)
395 m_internalClientThread.get(),
405 inline void WebrtcClient::log(
const std::string& message)
408 m_internalClientThread.get(),
418 inline rtc::Thread* WebrtcClient::getInternalClientThread() {
return m_internalClientThread.get(); }
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