Intel® Collaboration Suite for WebRTC  version 4.3.1
Open WebRTC Toolkit (OWT) Client SDK for Windows*
p2pclient.h
1 // Copyright (C) <2018> Intel Corporation
2 //
3 // SPDX-License-Identifier: Apache-2.0
4 #ifndef OWT_P2P_P2PCLIENT_H_
5 #define OWT_P2P_P2PCLIENT_H_
6 #include <functional>
7 #include <memory>
8 #include <mutex>
9 #include <unordered_map>
10 #include <vector>
11 #include <set>
12 #include "owt/base/commontypes.h"
13 #include "owt/base/connectionstats.h"
14 #include "owt/base/stream.h"
15 #include "owt/p2p/p2ppublication.h"
16 #include "owt/p2p/p2psignalingchannelinterface.h"
17 #include "owt/p2p/p2psignalingsenderinterface.h"
18 #include "owt/base/clientconfiguration.h"
19 #include "owt/base/globalconfiguration.h"
20 namespace rtc {
21  class TaskQueue;
22 }
23 namespace owt {
24 namespace base {
25  struct PeerConnectionChannelConfiguration;
26 }
27 namespace p2p{
34  std::vector<AudioEncodingParameters> audio_encodings;
35  std::vector<VideoEncodingParameters> video_encodings;
36 };
37 class P2PPeerConnectionChannelObserverCppImpl;
38 class P2PPeerConnectionChannel;
41  public:
47  virtual void OnMessageReceived(const std::string& remote_user_id,
48  const std::string message){}
53  virtual void OnStreamAdded(
54  std::shared_ptr<owt::base::RemoteStream> stream){}
59  virtual void OnServerDisconnected(){}
60 };
62 class P2PClient final
63  : protected P2PSignalingSenderInterface,
65  public std::enable_shared_from_this<P2PClient> {
66  friend class P2PPublication;
67  friend class P2PPeerConnectionChannelObserverCppImpl;
68  public:
74  P2PClient(P2PClientConfiguration& configuration,
75  std::shared_ptr<P2PSignalingChannelInterface> signaling_channel);
80  void AddObserver(P2PClientObserver& observer);
84  void RemoveObserver(P2PClientObserver& observer);
96  void Connect(const std::string& host,
97  const std::string& token,
98  std::function<void(const std::string&)> on_success,
99  std::function<void(std::unique_ptr<Exception>)> on_failure);
109  void Disconnect(std::function<void()> on_success,
110  std::function<void(std::unique_ptr<Exception>)> on_failure);
115  void AddAllowedRemoteId(const std::string& target_id);
126  void RemoveAllowedRemoteId(const std::string& target_id,
127  std::function<void()> on_success,
128  std::function<void(std::unique_ptr<Exception>)> on_failure);
141  void Stop(const std::string& target_id,
142  std::function<void()> on_success,
143  std::function<void(std::unique_ptr<Exception>)> on_failure);
156  void Publish(const std::string& target_id,
157  std::shared_ptr<owt::base::LocalStream> stream,
158  std::function<void(std::shared_ptr<P2PPublication>)> on_success,
159  std::function<void(std::unique_ptr<Exception>)> on_failure);
171  void Send(const std::string& target_id,
172  const std::string& message,
173  std::function<void()> on_success,
174  std::function<void(std::unique_ptr<Exception>)> on_failure);
186  void GetConnectionStats(
187  const std::string& target_id,
188  std::function<void(std::shared_ptr<owt::base::ConnectionStats>)> on_success,
189  std::function<void(std::unique_ptr<Exception>)> on_failure);
191  void SetLocalId(const std::string& local_id);
193  protected:
194  // Implement P2PSignalingSenderInterface
195  virtual void SendSignalingMessage(const std::string& message,
196  const std::string& remote_id,
197  std::function<void()> on_success,
198  std::function<void(std::unique_ptr<Exception>)> on_failure);
199  // Implement P2PSignalingChannelObserver
200  virtual void OnSignalingMessage(const std::string& message, const std::string& sender);
201  virtual void OnServerDisconnected();
202  // Handle events from P2PPeerConnectionChannel
203  // Triggered when the WebRTC session is ended.
204  virtual void OnStopped(const std::string& remote_id);
205  // Triggered when remote user send data via data channel.
206  // Currently, data is string.
207  virtual void OnMessageReceived(const std::string& remote_id,
208  const std::string& message);
209  // Triggered when a new stream is added.
210  virtual void OnStreamAdded(
211  std::shared_ptr<owt::base::RemoteStream> stream);
212  private:
213  void Unpublish(const std::string& target_id,
214  std::shared_ptr<LocalStream> stream,
215  std::function<void()> on_success,
216  std::function<void(std::unique_ptr<Exception>)> on_failure);
217  std::shared_ptr<P2PPeerConnectionChannel> GetPeerConnectionChannel(
218  const std::string& target_id);
219  bool IsPeerConnectionChannelCreated(const std::string& target_id);
220  owt::base::PeerConnectionChannelConfiguration GetPeerConnectionChannelConfiguration();
221  // Queue for callbacks and events. Shared among P2PClient and all of it's
222  // P2PPeerConnectionChannel.
223  std::shared_ptr<rtc::TaskQueue> event_queue_;
224  std::shared_ptr<P2PSignalingChannelInterface> signaling_channel_;
225  std::unordered_map<std::string, std::shared_ptr<P2PPeerConnectionChannel>>
226  pc_channels_;
227  std::mutex pc_channels_mutex_;
228  std::vector<std::shared_ptr<P2PPeerConnectionChannel>> removed_pc_channels_;
229  std::mutex removed_pc_channels_mutex_;
230  std::string local_id_;
231  std::vector<std::reference_wrapper<P2PClientObserver>> observers_;
232  P2PClientConfiguration configuration_;
233  mutable std::mutex remote_ids_mutex_;
234  std::vector<std::string> allowed_remote_ids_;
235 };
236 }
237 }
238 #endif // OWT_P2P_P2PCLIENT_H_
Definition: conferencepublication.h:12
virtual void OnServerDisconnected()
This function will be invoked when client is disconnected from signaling server.
Definition: p2pclient.h:59
void RemoveObserver(P2PClientObserver &observer)
void Stop(const std::string &target_id, std::function< void()> on_success, std::function< void(std::unique_ptr< Exception >)> on_failure)
Stop a WebRTC session.
Observer for P2PClient.
Definition: p2pclient.h:40
void Publish(const std::string &target_id, std::shared_ptr< owt::base::LocalStream > stream, std::function< void(std::shared_ptr< P2PPublication >)> on_success, std::function< void(std::unique_ptr< Exception >)> on_failure)
Publish a stream to the remote client.
Definition: audioplayerinterface.h:8
virtual void OnMessageReceived(const std::string &remote_user_id, const std::string message)
This function will be invoked when received data from a remote user.
Definition: p2pclient.h:47
Client configurations.
Definition: clientconfiguration.h:13
void AddObserver(P2PClientObserver &observer)
void RemoveAllowedRemoteId(const std::string &target_id, std::function< void()> on_success, std::function< void(std::unique_ptr< Exception >)> on_failure)
Remove a remote user from the allowed list to stop a WebRTC session.
void AddAllowedRemoteId(const std::string &target_id)
Add a remote user to the allowed list to start a WebRTC session.
Definition: p2ppublication.h:18
Signaling channel will notify observer when event triggers.
Definition: p2psignalingchannelinterface.h:17
virtual void OnStreamAdded(std::shared_ptr< owt::base::RemoteStream > stream)
This function will be invoked when a remote stream is available.
Definition: p2pclient.h:53
Configuration for P2PClient This configuration is used while creating P2PClient. Changing this config...
Definition: p2pclient.h:33
void Connect(const std::string &host, const std::string &token, std::function< void(const std::string &)> on_success, std::function< void(std::unique_ptr< Exception >)> on_failure)
Connect to the signaling server.
An async client for P2P WebRTC sessions.
Definition: p2pclient.h:62
void Send(const std::string &target_id, const std::string &message, std::function< void()> on_success, std::function< void(std::unique_ptr< Exception >)> on_failure)
Send a message to remote client.
void Disconnect(std::function< void()> on_success, std::function< void(std::unique_ptr< Exception >)> on_failure)
Disconnect from the signaling server. It will stop all active WebRTC sessions.
P2PClient(P2PClientConfiguration &configuration, std::shared_ptr< P2PSignalingChannelInterface > signaling_channel)
Init a P2PClient instance with speficied signaling channel.
void GetConnectionStats(const std::string &target_id, std::function< void(std::shared_ptr< owt::base::ConnectionStats >)> on_success, std::function< void(std::unique_ptr< Exception >)> on_failure)
Get the connection statistowt with target client.