Open3D (C++ API)  0.13.0
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
PeerConnectionManager.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2021 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
27 // Contains source code from
28 // https://github.com/mpromonet/webrtc-streamer
29 //
30 // This software is in the public domain, furnished "as is", without technical
31 // support, and with no warranty, express or implied, as to its usefulness for
32 // any purpose.
33 // ----------------------------------------------------------------------------
34 //
35 // This is a private header. It shall be hidden from Open3D's public API. Do not
36 // put this in Open3D.h.in.
37 
38 #pragma once
39 
40 #include <api/peer_connection_interface.h>
41 #include <rtc_base/strings/json.h>
42 
43 #include <future>
44 #include <mutex>
45 #include <regex>
46 #include <string>
47 #include <thread>
48 #include <unordered_map>
49 
53 
54 namespace open3d {
55 namespace visualization {
56 namespace webrtc_server {
57 
93  class VideoSink : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
94  public:
95  VideoSink(webrtc::VideoTrackInterface* track) : track_(track) {
96  track_->AddOrUpdateSink(this, rtc::VideoSinkWants());
97  }
98  virtual ~VideoSink() { track_->RemoveSink(this); }
99 
100  // VideoSinkInterface implementation
101  virtual void OnFrame(const webrtc::VideoFrame& video_frame) {
102  rtc::scoped_refptr<webrtc::I420BufferInterface> buffer(
103  video_frame.video_frame_buffer()->ToI420());
104  utility::LogDebug("[{}] frame: {}x{}", __FN__, buffer->height(),
105  buffer->width());
106  }
107 
108  protected:
109  rtc::scoped_refptr<webrtc::VideoTrackInterface> track_;
110  };
111 
112  class SetSessionDescriptionObserver
113  : public webrtc::SetSessionDescriptionObserver {
114  public:
115  static SetSessionDescriptionObserver* Create(
116  webrtc::PeerConnectionInterface* pc,
117  std::promise<const webrtc::SessionDescriptionInterface*>&
118  promise) {
119  return new rtc::RefCountedObject<SetSessionDescriptionObserver>(
120  pc, promise);
121  }
122  virtual void OnSuccess() {
123  std::string sdp;
124  if (pc_->local_description()) {
125  promise_.set_value(pc_->local_description());
126  pc_->local_description()->ToString(&sdp);
127  } else if (pc_->remote_description()) {
128  promise_.set_value(pc_->remote_description());
129  pc_->remote_description()->ToString(&sdp);
130  }
131  }
132  virtual void OnFailure(webrtc::RTCError error) {
133  utility::LogWarning("{}", error.message());
134  promise_.set_value(nullptr);
135  }
136 
137  protected:
138  SetSessionDescriptionObserver(
139  webrtc::PeerConnectionInterface* pc,
140  std::promise<const webrtc::SessionDescriptionInterface*>&
141  promise)
142  : pc_(pc), promise_(promise){};
143 
144  private:
145  webrtc::PeerConnectionInterface* pc_;
146  std::promise<const webrtc::SessionDescriptionInterface*>& promise_;
147  };
148 
149  class CreateSessionDescriptionObserver
150  : public webrtc::CreateSessionDescriptionObserver {
151  public:
152  static CreateSessionDescriptionObserver* Create(
153  webrtc::PeerConnectionInterface* pc,
154  std::promise<const webrtc::SessionDescriptionInterface*>&
155  promise) {
156  return new rtc::RefCountedObject<CreateSessionDescriptionObserver>(
157  pc, promise);
158  }
159  virtual void OnSuccess(webrtc::SessionDescriptionInterface* desc) {
160  std::string sdp;
161  desc->ToString(&sdp);
162  pc_->SetLocalDescription(
163  SetSessionDescriptionObserver::Create(pc_, promise_), desc);
164  }
165  virtual void OnFailure(webrtc::RTCError error) {
166  utility::LogWarning("{}", error.message());
167  promise_.set_value(nullptr);
168  }
169 
170  protected:
171  CreateSessionDescriptionObserver(
172  webrtc::PeerConnectionInterface* pc,
173  std::promise<const webrtc::SessionDescriptionInterface*>&
174  promise)
175  : pc_(pc), promise_(promise){};
176 
177  private:
178  webrtc::PeerConnectionInterface* pc_;
179  std::promise<const webrtc::SessionDescriptionInterface*>& promise_;
180  };
181 
182  class PeerConnectionStatsCollectorCallback
183  : public webrtc::RTCStatsCollectorCallback {
184  public:
185  PeerConnectionStatsCollectorCallback() {}
186  void clearReport() { report_.clear(); }
187  Json::Value getReport() { return report_; }
188 
189  protected:
190  virtual void OnStatsDelivered(
191  const rtc::scoped_refptr<const webrtc::RTCStatsReport>&
192  report) {
193  for (const webrtc::RTCStats& stats : *report) {
194  Json::Value stats_members;
195  for (const webrtc::RTCStatsMemberInterface* member :
196  stats.Members()) {
197  stats_members[member->name()] = member->ValueToString();
198  }
199  report_[stats.id()] = stats_members;
200  }
201  }
202 
203  Json::Value report_;
204  };
205 
206  class DataChannelObserver : public webrtc::DataChannelObserver {
207  public:
208  DataChannelObserver(
209  PeerConnectionManager* peer_connection_manager,
210  rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel,
211  const std::string& peerid)
212  : peer_connection_manager_(peer_connection_manager),
213  data_channel_(data_channel),
214  peerid_(peerid) {
215  data_channel_->RegisterObserver(this);
216  }
217  virtual ~DataChannelObserver() { data_channel_->UnregisterObserver(); }
218 
219  // DataChannelObserver interface
220  virtual void OnStateChange() {
221  // Useful to know when the data channel is established.
222  const std::string label = data_channel_->label();
223  const std::string state =
224  webrtc::DataChannelInterface::DataStateString(
225  data_channel_->state());
227  "DataChannelObserver::OnStateChange label: {}, state: {}, "
228  "peerid: {}",
229  label, state, peerid_);
230  std::string msg(label + " " + state);
231  webrtc::DataBuffer buffer(msg);
232  data_channel_->Send(buffer);
233  // ClientDataChannel is established after ServerDataChannel. Once
234  // ClientDataChannel is established, we need to send initial frames
235  // to the client such that the video is not empty. Afterwards,
236  // video frames will only be sent when the GUI redraws.
237  if (label == "ClientDataChannel" && state == "open") {
238  peer_connection_manager_->SendInitFramesToPeer(peerid_);
239  }
240  }
241  virtual void OnMessage(const webrtc::DataBuffer& buffer) {
242  std::string msg((const char*)buffer.data.data(),
243  buffer.data.size());
244  utility::LogDebug("DataChannelObserver::OnMessage: {}, msg: {}.",
245  data_channel_->label(), msg);
246  WebRTCWindowSystem::GetInstance()->OnDataChannelMessage(msg);
247  }
248 
249  protected:
250  PeerConnectionManager* peer_connection_manager_;
251  rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel_;
252  const std::string peerid_;
253  };
254 
255  class PeerConnectionObserver : public webrtc::PeerConnectionObserver {
256  public:
257  PeerConnectionObserver(
258  PeerConnectionManager* peer_connection_manager,
259  const std::string& peerid,
260  const webrtc::PeerConnectionInterface::RTCConfiguration& config,
261  std::unique_ptr<cricket::PortAllocator> port_allocator)
262  : peer_connection_manager_(peer_connection_manager),
263  peerid_(peerid),
264  local_channel_(nullptr),
265  remote_channel_(nullptr),
266  ice_candidate_list_(Json::arrayValue),
267  deleting_(false) {
268  pc_ = peer_connection_manager_->peer_connection_factory_
269  ->CreatePeerConnection(config,
270  std::move(port_allocator),
271  nullptr, this);
272 
273  if (pc_.get()) {
274  rtc::scoped_refptr<webrtc::DataChannelInterface> channel =
275  pc_->CreateDataChannel("ServerDataChannel", nullptr);
276  local_channel_ = new DataChannelObserver(
277  peer_connection_manager_, channel, peerid_);
278  }
279 
280  stats_callback_ = new rtc::RefCountedObject<
281  PeerConnectionStatsCollectorCallback>();
282  };
283 
284  virtual ~PeerConnectionObserver() {
285  delete local_channel_;
286  delete remote_channel_;
287  if (pc_.get()) {
288  // warning: pc->close call OnIceConnectionChange
289  deleting_ = true;
290  pc_->Close();
291  }
292  }
293 
294  Json::Value GetIceCandidateList() { return ice_candidate_list_; }
295 
296  Json::Value GetStats() {
297  stats_callback_->clearReport();
298  pc_->GetStats(stats_callback_);
299  int count = 10;
300  while ((stats_callback_->getReport().empty()) && (--count > 0)) {
301  std::this_thread::sleep_for(std::chrono::milliseconds(1000));
302  }
303  return Json::Value(stats_callback_->getReport());
304  };
305 
306  rtc::scoped_refptr<webrtc::PeerConnectionInterface>
308  return pc_;
309  };
310 
311  // PeerConnectionObserver interface
312  virtual void OnAddStream(
313  rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
314  utility::LogDebug("[{}] GetVideoTracks().size(): {}.", __FN__,
315  stream->GetVideoTracks().size());
316  webrtc::VideoTrackVector videoTracks = stream->GetVideoTracks();
317  if (videoTracks.size() > 0) {
318  video_sink_.reset(new VideoSink(videoTracks.at(0)));
319  }
320  }
321  virtual void OnRemoveStream(
322  rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
323  video_sink_.reset();
324  }
325  virtual void OnDataChannel(
326  rtc::scoped_refptr<webrtc::DataChannelInterface> channel) {
328  "PeerConnectionObserver::OnDataChannel peerid: {}",
329  peerid_);
330  remote_channel_ = new DataChannelObserver(peer_connection_manager_,
331  channel, peerid_);
332  }
333  virtual void OnRenegotiationNeeded() {
335  "PeerConnectionObserver::OnRenegotiationNeeded peerid: {}",
336  peerid_);
337  }
338  virtual void OnIceCandidate(
339  const webrtc::IceCandidateInterface* candidate);
340 
341  virtual void OnSignalingChange(
342  webrtc::PeerConnectionInterface::SignalingState state) {
343  utility::LogDebug("state: {}, peerid: {}", state, peerid_);
344  }
345  virtual void OnIceConnectionChange(
346  webrtc::PeerConnectionInterface::IceConnectionState state) {
347  if ((state ==
348  webrtc::PeerConnectionInterface::kIceConnectionFailed) ||
349  (state ==
350  webrtc::PeerConnectionInterface::kIceConnectionClosed)) {
351  ice_candidate_list_.clear();
352  if (!deleting_) {
353  std::thread([this]() {
354  peer_connection_manager_->HangUp(peerid_);
355  }).detach();
356  }
357  }
358  }
359 
360  virtual void OnIceGatheringChange(
361  webrtc::PeerConnectionInterface::IceGatheringState) {}
362 
363  private:
364  PeerConnectionManager* peer_connection_manager_;
365  const std::string peerid_;
366  rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc_;
367  DataChannelObserver* local_channel_;
368  DataChannelObserver* remote_channel_;
369  Json::Value ice_candidate_list_;
370  rtc::scoped_refptr<PeerConnectionStatsCollectorCallback>
371  stats_callback_;
372  std::unique_ptr<VideoSink> video_sink_;
373  bool deleting_;
374  };
375 
376 public:
377  PeerConnectionManager(const std::list<std::string>& ice_server_list,
378  const Json::Value& config,
379  const std::string& publish_filter,
380  const std::string& webrtc_udp_port_range);
381  virtual ~PeerConnectionManager();
382 
384  const std::map<std::string, HttpServerRequestHandler::HttpFunction>
385  GetHttpApi();
386 
387  const Json::Value GetIceCandidateList(const std::string& peerid);
388  const Json::Value AddIceCandidate(const std::string& peerid,
389  const Json::Value& json_message);
390  const Json::Value GetMediaList();
391  const Json::Value HangUp(const std::string& peerid);
392  const Json::Value Call(const std::string& peerid,
393  const std::string& window_uid,
394  const std::string& options,
395  const Json::Value& json_message);
396  const Json::Value GetIceServers();
397 
398  void SendInitFramesToPeer(const std::string& peerid);
399 
400  void CloseWindowConnections(const std::string& window_uid);
401 
402  void OnFrame(const std::string& window_uid,
403  const std::shared_ptr<core::Tensor>& im);
404 
405 protected:
406  rtc::scoped_refptr<BitmapTrackSourceInterface> GetVideoTrackSource(
407  const std::string& window_uid);
408  PeerConnectionObserver* CreatePeerConnection(const std::string& peerid);
409  bool AddStreams(webrtc::PeerConnectionInterface* peer_connection,
410  const std::string& window_uid,
411  const std::string& options);
412  rtc::scoped_refptr<BitmapTrackSourceInterface> CreateVideoSource(
413  const std::string& window_uid,
414  const std::map<std::string, std::string>& opts);
415  bool WindowStillUsed(const std::string& window_uid);
416  rtc::scoped_refptr<webrtc::PeerConnectionInterface> GetPeerConnection(
417  const std::string& peerid);
418 
419 protected:
420  std::unique_ptr<webrtc::TaskQueueFactory> task_queue_factory_;
421  rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
423 
424  // Each peer has exactly one connection.
425  std::unordered_map<std::string, PeerConnectionObserver*>
428 
429  // Each Window has exactly one TrackSource.
430  std::unordered_map<std::string,
431  rtc::scoped_refptr<BitmapTrackSourceInterface>>
434 
435  // Each Window can be connected to zero, one or more peers.
436  std::unordered_map<std::string, std::set<std::string>>
438  std::unordered_map<std::string, std::string> peerid_to_window_uid_;
439  // Shared by window_uid_to_peerids_ and peerid_to_window_uid_.
441 
442  std::list<std::string> ice_server_list_;
443  const Json::Value config_;
444  const std::regex publish_filter_;
445  std::map<std::string, HttpServerRequestHandler::HttpFunction> func_;
446  std::string webrtc_port_range_;
447 };
448 
449 } // namespace webrtc_server
450 } // namespace visualization
451 } // namespace open3d
rtc::scoped_refptr< webrtc::PeerConnectionFactoryInterface > peer_connection_factory_
Definition: PeerConnectionManager.h:422
std::map< std::string, HttpServerRequestHandler::HttpFunction > func_
Definition: PeerConnectionManager.h:445
virtual ~PeerConnectionManager()
Definition: PeerConnectionManager.cpp:219
const std::regex publish_filter_
Definition: PeerConnectionManager.h:444
rtc::scoped_refptr< BitmapTrackSourceInterface > GetVideoTrackSource(const std::string &window_uid)
Definition: PeerConnectionManager.cpp:703
const Json::Value GetIceServers()
Definition: PeerConnectionManager.cpp:236
std::mutex peerid_to_connection_mutex_
Definition: PeerConnectionManager.h:427
PeerConnectionObserver * CreatePeerConnection(const std::string &peerid)
Definition: PeerConnectionManager.cpp:531
rtc::scoped_refptr< webrtc::PeerConnectionInterface > GetPeerConnection(const std::string &peerid)
Definition: PeerConnectionManager.cpp:260
rtc::scoped_refptr< BitmapTrackSourceInterface > CreateVideoSource(const std::string &window_uid, const std::map< std::string, std::string > &opts)
Definition: PeerConnectionManager.cpp:570
std::unordered_map< std::string, rtc::scoped_refptr< BitmapTrackSourceInterface > > window_uid_to_track_source_
Definition: PeerConnectionManager.h:432
static std::shared_ptr< WebRTCWindowSystem > GetInstance()
Definition: WebRTCWindowSystem.cpp:127
#define LogWarning(...)
Definition: Console.h:95
const Json::Value AddIceCandidate(const std::string &peerid, const Json::Value &json_message)
Definition: PeerConnectionManager.cpp:270
const Json::Value config_
Definition: PeerConnectionManager.h:443
std::unordered_map< std::string, std::string > peerid_to_window_uid_
Definition: PeerConnectionManager.h:438
#define __FN__
Definition: Console.h:57
const Json::Value HangUp(const std::string &peerid)
Definition: PeerConnectionManager.cpp:442
bool WindowStillUsed(const std::string &window_uid)
Definition: PeerConnectionManager.cpp:424
bool AddStreams(webrtc::PeerConnectionInterface *peer_connection, const std::string &window_uid, const std::string &options)
Definition: PeerConnectionManager.cpp:582
const Json::Value GetMediaList()
Definition: PeerConnectionManager.cpp:222
std::mutex window_uid_to_track_source_mutex_
Definition: PeerConnectionManager.h:433
std::mutex window_uid_to_peerids_mutex_
Definition: PeerConnectionManager.h:440
const Json::Value Call(const std::string &peerid, const std::string &window_uid, const std::string &options, const Json::Value &json_message)
Definition: PeerConnectionManager.cpp:312
std::unique_ptr< webrtc::TaskQueueFactory > task_queue_factory_
Definition: PeerConnectionManager.h:420
const Json::Value GetIceCandidateList(const std::string &peerid)
Definition: PeerConnectionManager.cpp:508
bool InitializePeerConnection()
Definition: PeerConnectionManager.cpp:525
std::unordered_map< std::string, std::set< std::string > > window_uid_to_peerids_
Definition: PeerConnectionManager.h:437
#define LogInfo(...)
Definition: Console.h:108
int count
Definition: FilePCD.cpp:61
std::string webrtc_port_range_
Definition: PeerConnectionManager.h:446
Definition: PinholeCameraIntrinsic.cpp:35
#define LogDebug(...)
Definition: Console.h:121
PeerConnectionManager(const std::list< std::string > &ice_server_list, const Json::Value &config, const std::string &publish_filter, const std::string &webrtc_udp_port_range)
Definition: PeerConnectionManager.cpp:145
void OnFrame(const std::string &window_uid, const std::shared_ptr< core::Tensor > &im)
Definition: PeerConnectionManager.cpp:739
void SendInitFramesToPeer(const std::string &peerid)
Definition: PeerConnectionManager.cpp:715
const std::map< std::string, HttpServerRequestHandler::HttpFunction > GetHttpApi()
Definition: PeerConnectionManager.cpp:503
std::unordered_map< std::string, PeerConnectionObserver * > peerid_to_connection_
Definition: PeerConnectionManager.h:426
void CloseWindowConnections(const std::string &window_uid)
Definition: PeerConnectionManager.cpp:721
std::list< std::string > ice_server_list_
Definition: PeerConnectionManager.h:442