Intel® Collaboration Suite for WebRTC  version 4.3.1
Open WebRTC Toolkit (OWT) Client SDK for Windows*
conferenceclient.h
1 // Copyright (C) <2018> Intel Corporation
2 //
3 // SPDX-License-Identifier: Apache-2.0
4 #ifndef OWT_CONFERENCE_CONFERENCECLIENT_H_
5 #define OWT_CONFERENCE_CONFERENCECLIENT_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/clientconfiguration.h"
14 #include "owt/base/connectionstats.h"
15 #include "owt/base/options.h"
16 #include "owt/base/stream.h"
17 #include "owt/base/exception.h"
18 #include "owt/conference/conferencepublication.h"
19 #include "owt/conference/conferencesubscription.h"
20 #include "owt/conference/streamupdateobserver.h"
21 #include "owt/conference/subscribeoptions.h"
22 namespace sio{
23  class message;
24 }
25 namespace webrtc{
26  class StatsReport;
27 }
28 namespace owt {
29 namespace base {
30  struct PeerConnectionChannelConfiguration;
31 }
32 }
33 namespace owt {
34 namespace conference {
35 class Participant;
36 using namespace owt::base;
44 class RemoteMixedStream;
45 class ConferencePeerConnectionChannel;
46 class ConferenceSocketSignalingChannel;
53  public:
57  virtual void OnLeft() {}
58 };
60 class Participant {
61  friend class ConferenceInfo;
62  public:
63  Participant(std::string id, std::string role, std::string user_id)
64  : id_(id)
65  , role_(role)
66  , user_id_(user_id) {}
67  virtual ~Participant() {}
69  void AddObserver(ParticipantObserver& observer);
71  void RemoveObserver(ParticipantObserver& observer);
73  std::string Id() const { return id_; }
75  std::string Role() const {return role_; }
77  std::string UserId() const { return user_id_; }
78  protected:
80  void Id(std::string id) { id_ = id; }
82  void Role(std::string role) { role_ = role; }
84  void UserId(std::string user_id) { user_id_ = user_id; }
86  void TriggerOnParticipantLeft();
87  private:
88  std::string id_;
89  std::string role_;
90  std::string user_id_;
91  mutable std::mutex observer_mutex_;
92  std::vector<std::reference_wrapper<ParticipantObserver>> observers_;
93 };
99  friend class ConferenceClient;
100  public:
101  ConferenceInfo() {}
102  virtual ~ConferenceInfo() {}
104  std::vector<std::shared_ptr<RemoteStream>> RemoteStreams() const {
105  return remote_streams_;
106  }
108  std::vector<std::shared_ptr<Participant>> Participants() const {
109  return participants_;
110  }
112  std::string Id() const { return id_; }
114  std::shared_ptr<Participant> Self() const {
115  return self_;
116  }
117  protected:
118  // Add participant.
119  void AddParticipant(std::shared_ptr<Participant> participant);
120  // Remove participant.
121  void RemoveParticipantById(const std::string& id);
122  // Add remote stream.
123  void AddOrUpdateStream(std::shared_ptr<RemoteStream> remote_stream, bool &updated);
124  // Remove remote stream.
125  void RemoveStreamById(const std::string& stream_id);
126  // Trigger participant left event.
127  void TriggerOnParticipantLeft(const std::string& participant_id);
128  // Trigger stream ended event.
129  void TriggerOnStreamEnded(const std::string& stream_id);
130  // Trigger stream updated event.
131  void TriggerOnStreamUpdated(const std::string& stream_id);
132  // Trigger stream mute/unmute events
133  void TriggerOnStreamMuteOrUnmute(const std::string& stream_id, owt::base::TrackKind track_kind, bool muted);
134  private:
135  bool ParticipantPresent(const std::string& participant_id);
136  bool RemoteStreamPresent(const std::string& stream_id);
137  std::string id_; // Unique id that identifies the conference.
138  mutable std::mutex participants_mutex_;
139  std::vector<std::shared_ptr<Participant>> participants_; // Participants in the conference
140  mutable std::mutex remote_streams_mutex_;
141  std::vector<std::shared_ptr<RemoteStream>> remote_streams_; // Remote streams in the conference.
142  std::shared_ptr<Participant> self_; // Self participant in the conference.
143 };
145 class ConferenceSocketSignalingChannelObserver {
146  public:
147  virtual ~ConferenceSocketSignalingChannelObserver(){}
148  virtual void OnUserJoined(std::shared_ptr<sio::message> user) = 0;
149  virtual void OnUserLeft(std::shared_ptr<sio::message> user) = 0;
150  virtual void OnStreamAdded(std::shared_ptr<sio::message> stream) = 0;
151  virtual void OnStreamRemoved(std::shared_ptr<sio::message> stream) = 0;
152  virtual void OnStreamUpdated(std::shared_ptr<sio::message> stream) = 0;
153  virtual void OnServerDisconnected() = 0;
154  virtual void OnCustomMessage(std::string& from, std::string& message, std::string& to) = 0;
155  virtual void OnSignalingMessage(std::shared_ptr<sio::message> message) = 0;
156  virtual void OnStreamError(std::shared_ptr<sio::message> stream) = 0;
157  // Notify the ID for a published/subscribed stream.
158  virtual void OnStreamId(const std::string& id, const std::string& label) = 0;
159  virtual void OnSubscriptionId(const std::string& subscription_id,
160  const std::string& stream_id) = 0;
161 };
162 // ConferencePeerConnectionChannel callback interface.
163 // Usually, ConferenceClient should implement these methods and notify
164 // application.
165 class ConferencePeerConnectionChannelObserver {
166  public:
167  virtual ~ConferencePeerConnectionChannelObserver(){}
168  // Triggered when an unrecoverable error happened. Error may reported by MCU
169  // or detected by client. Currently, only errors from MCU are handled.
170  virtual void OnStreamError(
171  std::shared_ptr<Stream> stream,
172  std::shared_ptr<const Exception> exception) = 0;
173 };
175 class ConferenceClientObserver {
177  public:
182  virtual void OnStreamAdded(
183  std::shared_ptr<RemoteStream> stream){}
188  virtual void OnStreamAdded(
189  std::shared_ptr<RemoteMixedStream> stream){}
197  virtual void OnMessageReceived(std::string& message,
198  std::string& sender_id,
199  std::string& to){}
204  virtual void OnParticipantJoined(std::shared_ptr<Participant>){}
208  virtual void OnServerDisconnected(){}
209 };
210 
212 class ConferenceClient final
213  : ConferenceSocketSignalingChannelObserver,
214  ConferencePeerConnectionChannelObserver,
215  public std::enable_shared_from_this<ConferenceClient> {
216  friend class ConferencePublication;
217  friend class ConferenceSubscription;
218  public:
223  static std::shared_ptr<ConferenceClient> Create(
224  const ConferenceClientConfiguration& configuration);
225  ~ConferenceClient();
227  void AddObserver(ConferenceClientObserver& observer);
229  void RemoveObserver(ConferenceClientObserver& observer);
236  void Join(
237  const std::string& token,
238  std::function<void(std::shared_ptr<ConferenceInfo>)> on_success,
239  std::function<void(std::unique_ptr<Exception>)> on_failure);
243  void Leave(
244  std::function<void()> on_success,
245  std::function<void(std::unique_ptr<Exception>)> on_failure);
250  void Publish(
251  std::shared_ptr<LocalStream> stream,
252  std::function<void(std::shared_ptr<ConferencePublication>)> on_success,
253  std::function<void(std::unique_ptr<Exception>)> on_failure);
259  void Publish(
260  std::shared_ptr<LocalStream> stream,
261  const PublishOptions& options,
262  std::function<void(std::shared_ptr<ConferencePublication>)> on_success,
263  std::function<void(std::unique_ptr<Exception>)> on_failure);
269  void Subscribe(
270  std::shared_ptr<RemoteStream> stream,
271  std::function<void(std::shared_ptr<ConferenceSubscription>)> on_success,
272  std::function<void(std::unique_ptr<Exception>)> on_failure);
279  void Subscribe(
280  std::shared_ptr<RemoteStream> stream,
281  const SubscribeOptions& options,
282  std::function<void(std::shared_ptr<ConferenceSubscription>)> on_success,
283  std::function<void(std::unique_ptr<Exception>)> on_failure);
288  void Send(
289  const std::string& message,
290  std::function<void()> on_success,
291  std::function<void(std::unique_ptr<Exception>)> on_failure);
297  void Send(
298  const std::string& message,
299  const std::string& receiver,
300  std::function<void()> on_success,
301  std::function<void(std::unique_ptr<Exception>)> on_failure);
302  protected:
303  ConferenceClient(const ConferenceClientConfiguration& configuration);
304  // Implementing ConferenceSocketSignalingChannelObserver.
305  virtual void OnStreamAdded(std::shared_ptr<sio::message> stream) override;
306  virtual void OnCustomMessage(std::string& from,
307  std::string& message,
308  std::string& to) override;
309  virtual void OnSignalingMessage(
310  std::shared_ptr<sio::message> stream) override;
311  virtual void OnUserJoined(std::shared_ptr<sio::message> user) override;
312  virtual void OnUserLeft(std::shared_ptr<sio::message> user) override;
313  virtual void OnStreamRemoved(std::shared_ptr<sio::message> stream) override;
314  virtual void OnStreamUpdated(std::shared_ptr<sio::message> stream) override;
315  virtual void OnStreamError(std::shared_ptr<sio::message> stream) override;
316  virtual void OnServerDisconnected() override;
317  virtual void OnStreamId(const std::string& id,
318  const std::string& publish_stream_label) override;
319  virtual void OnSubscriptionId(const std::string& subscription_id,
320  const std::string& stream_id) override;
321  // Implementing ConferencePeerConnectionChannelObserver.
322  virtual void OnStreamError(
323  std::shared_ptr<Stream> stream,
324  std::shared_ptr<const Exception> exception) override;
325  // Provide access for Publication and Subscription instances.
330  void UnPublish(
331  const std::string& session_id,
332  std::function<void()> on_success,
333  std::function<void(std::unique_ptr<Exception>)> on_failure);
338  void UnSubscribe(
339  const std::string& session_id,
340  std::function<void()> on_success,
341  std::function<void(std::unique_ptr<Exception>)> on_failure);
345  void UpdateSubscription(
346  const std::string& session_id,
347  const std::string& stream_id,
348  const SubscriptionUpdateOptions& option,
349  std::function<void()> on_success,
350  std::function<void(std::unique_ptr<Exception>)> on_failure);
354  void GetConnectionStats(
355  const std::string& session_id,
356  std::function<void(std::shared_ptr<ConnectionStats>)> on_success,
357  std::function<void(std::unique_ptr<Exception>)> on_failure);
358  void GetStats(
359  const std::string& session_id,
360  std::function<void(
361  const std::vector<const webrtc::StatsReport*>& reports)> on_success,
362  std::function<void(std::unique_ptr<Exception>)> on_failure);
366  void Mute(
367  const std::string& session_id,
368  TrackKind track_kind,
369  std::function<void()> on_success,
370  std::function<void(std::unique_ptr<Exception>)> on_failure);
374  void Unmute(
375  const std::string& session_id,
376  TrackKind track_kind,
377  std::function<void()> on_success,
378  std::function<void(std::unique_ptr<Exception>)> on_failure);
379  private:
382  bool CheckNullPointer(
383  uintptr_t pointer,
384  std::function<void(std::unique_ptr<Exception>)> on_failure);
385  bool CheckNullPointer(
386  uintptr_t pointer,
387  const std::string& failure_message,
388  std::function<void(std::unique_ptr<Exception>)> on_failure);
391  bool CheckSignalingChannelOnline(
392  std::function<void(std::unique_ptr<Exception>)> on_failure);
393  PeerConnectionChannelConfiguration GetPeerConnectionChannelConfiguration()
394  const;
395  // Get the |ConferencePeerConnectionChannel| instance associated with specific
396  // |session_id|. Return |nullptr| if not found.
397  std::shared_ptr<ConferencePeerConnectionChannel>
398  GetConferencePeerConnectionChannel(const std::string& session_id) const;
399  void TriggerOnUserJoined(std::shared_ptr<sio::message> user_info, bool joining = false);
400  void TriggerOnUserLeft(std::shared_ptr<sio::message> user_info);
401  void TriggerOnStreamAdded(std::shared_ptr<sio::message> stream_info, bool joining = false);
402  void TriggerOnStreamRemoved(std::shared_ptr<sio::message> stream_info);
403  void TriggerOnStreamUpdated(std::shared_ptr<sio::message> stream_info);
404  void TriggerOnStreamError(std::shared_ptr<Stream> stream,
405  std::shared_ptr<const Exception> exception);
406  // Return true if |user_info| is correct, and |*participant| points to the participant
407  // object
408  bool ParseUser(std::shared_ptr<sio::message> user_info, Participant** participant) const;
409  void ParseStreamInfo(std::shared_ptr<sio::message> stream_info, bool joining = false);
410  std::unordered_map<std::string, std::string> AttributesFromStreamInfo(
411  std::shared_ptr<sio::message> stream_info);
412  std::function<void()> RunInEventQueue(std::function<void()> func);
413  // Check if all characters are base 64 allowed or '='.
414  bool IsBase64EncodedString(const std::string str) const;
416  void AddStreamUpdateObserver(ConferenceStreamUpdateObserver& observer);
418  void RemoveStreamUpdateObserver(ConferenceStreamUpdateObserver& observer);
419  enum StreamType: int;
420  ConferenceClientConfiguration configuration_;
421  // Queue for callbacks and events. Shared among ConferenceClient and all of
422  // it's ConferencePeerConnectionChannel.
423  std::shared_ptr<rtc::TaskQueue> event_queue_;
424  std::shared_ptr<ConferenceSocketSignalingChannel> signaling_channel_;
425  std::mutex observer_mutex_;
426  bool signaling_channel_connected_;
427  // Key publish(session) ID from server, value is MediaStream's label
428  std::unordered_map<std::string, std::string> publish_id_label_map_;
429  // Store the peer connection channels created.
430  std::vector<std::shared_ptr<ConferencePeerConnectionChannel>>
431  publish_pcs_;
432  mutable std::mutex publish_pcs_mutex_;
433  // Key is subcription ID from server.
434  std::vector<std::shared_ptr<ConferencePeerConnectionChannel>>
435  subscribe_pcs_;
436  // Key is subscription ID, value is streamID.
437  std::unordered_map<std::string, std::string> subscribe_id_label_map_;
438  mutable std::mutex subscribe_pcs_mutex_;
439  // Key is the stream ID(publication ID or mixed stream ID).
440  std::unordered_map<std::string, std::shared_ptr<RemoteStream>>
441  added_streams_;
442  std::unordered_map<std::string, StreamType> added_stream_type_;
443  mutable std::mutex conference_info_mutex_;
444  // Store current conference info.
445  std::shared_ptr<ConferenceInfo> current_conference_info_;
446  // Capturing observer in |event_queue_| is not 100% safe although above queue
447  // is excepted to be ended after ConferenceClient is destroyed.
448  std::vector<std::reference_wrapper<ConferenceClientObserver>> observers_;
449  mutable std::mutex stream_update_observer_mutex_;
450  std::vector <std::reference_wrapper<ConferenceStreamUpdateObserver>> stream_update_observers_;
451 };
452 }
453 }
454 #endif // OWT_CONFERENCE_CONFERENCECLIENT_H_
std::vector< std::shared_ptr< RemoteStream > > RemoteStreams() const
Current remote streams in the conference.
Definition: conferenceclient.h:104
Information about the conference.
Definition: conferenceclient.h:98
std::string UserId() const
Get the participant&#39;s user id.
Definition: conferenceclient.h:77
An asynchronous class for app to communicate with a conference in MCU.
Definition: conferenceclient.h:212
std::string Id() const
Get the participant&#39;s ID.
Definition: conferenceclient.h:73
std::vector< std::shared_ptr< Participant > > Participants() const
Current participant list in the conference.
Definition: conferenceclient.h:108
Observer for OWTConferenceClient.
Definition: conferenceclient.h:176
Definition: conferenceclient.h:22
This class represent a mixed remote stream.
Definition: remotemixedstream.h:17
Participant represents one conference client in a conference room.
Definition: conferenceclient.h:60
Definition: stream.h:17
Observer interface for participant.
Definition: conferenceclient.h:52
std::string Id() const
Conference ID.
Definition: conferenceclient.h:112
Definition: audioplayerinterface.h:8
Client configurations.
Definition: clientconfiguration.h:13
virtual void OnMessageReceived(std::string &message, std::string &sender_id, std::string &to)
Triggers when a message is received.
Definition: conferenceclient.h:197
Definition: audioplayerinterface.h:9
Configuration for creating a ConferenceClient.
Definition: conferenceclient.h:43
virtual void OnStreamAdded(std::shared_ptr< RemoteStream > stream)
Triggers when a stream is added.
Definition: conferenceclient.h:182
virtual void OnParticipantJoined(std::shared_ptr< Participant >)
Triggers when a participant joined conference.
Definition: conferenceclient.h:204
Definition: conferencepublication.h:25
Publish options describing encoding settings.
Definition: options.h:51
virtual void OnLeft()
Participant leave event callback.
Definition: conferenceclient.h:57
std::string Role() const
Get the participant&#39;s role.
Definition: conferenceclient.h:75
std::shared_ptr< Participant > Self() const
The participant info of current conference client.
Definition: conferenceclient.h:114
Subscribe options.
Definition: subscribeoptions.h:44
virtual void OnStreamAdded(std::shared_ptr< RemoteMixedStream > stream)
Triggers when a mixed stream is added.
Definition: conferenceclient.h:188
Subscription update option used by subscription&#39;s ApplyOptions API.
Definition: subscribeoptions.h:65
virtual void OnServerDisconnected()
Triggers when server is disconnected.
Definition: conferenceclient.h:208
Definition: conferencesubscription.h:25