21 #include <api/peer_connection_interface.h>
22 #include <rtc_base/strings/json.h>
29 #include <unordered_map>
36 namespace visualization {
37 namespace webrtc_server {
74 class VideoSink :
public rtc::VideoSinkInterface<webrtc::VideoFrame> {
76 VideoSink(webrtc::VideoTrackInterface* track) : track_(track) {
77 track_->AddOrUpdateSink(
this, rtc::VideoSinkWants());
79 virtual ~VideoSink() { track_->RemoveSink(
this); }
82 virtual void OnFrame(
const webrtc::VideoFrame& video_frame) {
83 rtc::scoped_refptr<webrtc::I420BufferInterface> buffer(
84 video_frame.video_frame_buffer()->ToI420());
86 buffer->height(), buffer->width());
90 rtc::scoped_refptr<webrtc::VideoTrackInterface> track_;
93 class SetSessionDescriptionObserver
94 :
public webrtc::SetSessionDescriptionObserver {
96 static SetSessionDescriptionObserver* Create(
97 webrtc::PeerConnectionInterface* pc,
98 std::promise<const webrtc::SessionDescriptionInterface*>&
100 return new rtc::RefCountedObject<SetSessionDescriptionObserver>(
103 virtual void OnSuccess() {
105 if (pc_->local_description()) {
106 promise_.set_value(pc_->local_description());
107 pc_->local_description()->ToString(&sdp);
108 }
else if (pc_->remote_description()) {
109 promise_.set_value(pc_->remote_description());
110 pc_->remote_description()->ToString(&sdp);
113 virtual void OnFailure(webrtc::RTCError error) {
115 promise_.set_value(
nullptr);
119 SetSessionDescriptionObserver(
120 webrtc::PeerConnectionInterface* pc,
121 std::promise<const webrtc::SessionDescriptionInterface*>&
123 : pc_(pc), promise_(promise){};
126 webrtc::PeerConnectionInterface* pc_;
127 std::promise<const webrtc::SessionDescriptionInterface*>& promise_;
130 class CreateSessionDescriptionObserver
131 :
public webrtc::CreateSessionDescriptionObserver {
133 static CreateSessionDescriptionObserver* Create(
134 webrtc::PeerConnectionInterface* pc,
135 std::promise<const webrtc::SessionDescriptionInterface*>&
137 return new rtc::RefCountedObject<CreateSessionDescriptionObserver>(
140 virtual void OnSuccess(webrtc::SessionDescriptionInterface* desc) {
142 desc->ToString(&sdp);
143 pc_->SetLocalDescription(
144 SetSessionDescriptionObserver::Create(pc_, promise_), desc);
146 virtual void OnFailure(webrtc::RTCError error) {
148 promise_.set_value(
nullptr);
152 CreateSessionDescriptionObserver(
153 webrtc::PeerConnectionInterface* pc,
154 std::promise<const webrtc::SessionDescriptionInterface*>&
156 : pc_(pc), promise_(promise){};
159 webrtc::PeerConnectionInterface* pc_;
160 std::promise<const webrtc::SessionDescriptionInterface*>& promise_;
163 class PeerConnectionStatsCollectorCallback
164 :
public webrtc::RTCStatsCollectorCallback {
166 PeerConnectionStatsCollectorCallback() {}
167 void clearReport() { report_.clear(); }
168 Json::Value getReport() {
return report_; }
171 virtual void OnStatsDelivered(
172 const rtc::scoped_refptr<const webrtc::RTCStatsReport>&
174 for (
const webrtc::RTCStats& stats : *report) {
175 Json::Value stats_members;
176 for (
const webrtc::RTCStatsMemberInterface* member :
178 stats_members[member->name()] = member->ValueToString();
180 report_[stats.id()] = stats_members;
187 class DataChannelObserver :
public webrtc::DataChannelObserver {
191 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel,
192 const std::string& peerid)
193 : peer_connection_manager_(peer_connection_manager),
194 data_channel_(data_channel),
196 data_channel_->RegisterObserver(
this);
198 virtual ~DataChannelObserver() { data_channel_->UnregisterObserver(); }
201 virtual void OnStateChange() {
203 const std::string label = data_channel_->label();
204 const std::string state =
205 webrtc::DataChannelInterface::DataStateString(
206 data_channel_->state());
208 "DataChannelObserver::OnStateChange label: {}, state: {}, "
210 label, state, peerid_);
211 std::string msg(label +
" " + state);
212 webrtc::DataBuffer buffer(msg);
213 data_channel_->Send(buffer);
218 if (label ==
"ClientDataChannel" && state ==
"open") {
220 std::lock_guard<std::mutex> mutex_lock(
221 peer_connection_manager_
223 peer_connection_manager_->peerid_data_channel_ready_.insert(
226 peer_connection_manager_->SendInitFramesToPeer(peerid_);
228 if (label ==
"ClientDataChannel" &&
229 (state ==
"closed" || state ==
"closing")) {
230 std::lock_guard<std::mutex> mutex_lock(
231 peer_connection_manager_->peerid_data_channel_mutex_);
232 peer_connection_manager_->peerid_data_channel_ready_.erase(
236 virtual void OnMessage(
const webrtc::DataBuffer& buffer) {
237 std::string msg((
const char*)buffer.data.data(),
240 data_channel_->label(), msg);
244 if (!reply.empty()) {
245 webrtc::DataBuffer buffer(reply);
246 data_channel_->Send(buffer);
252 rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel_;
253 const std::string peerid_;
256 class PeerConnectionObserver :
public webrtc::PeerConnectionObserver {
258 PeerConnectionObserver(
260 const std::string& peerid,
261 const webrtc::PeerConnectionInterface::RTCConfiguration& config,
262 std::unique_ptr<cricket::PortAllocator> port_allocator)
263 : peer_connection_manager_(peer_connection_manager),
265 local_channel_(
nullptr),
266 remote_channel_(
nullptr),
267 ice_candidate_list_(Json::arrayValue),
269 pc_ = peer_connection_manager_->peer_connection_factory_
270 ->CreatePeerConnection(config,
271 std::move(port_allocator),
275 rtc::scoped_refptr<webrtc::DataChannelInterface> channel =
276 pc_->CreateDataChannel(
"ServerDataChannel",
nullptr);
277 local_channel_ =
new DataChannelObserver(
278 peer_connection_manager_, channel, peerid_);
281 stats_callback_ =
new rtc::RefCountedObject<
282 PeerConnectionStatsCollectorCallback>();
285 virtual ~PeerConnectionObserver() {
286 delete local_channel_;
287 delete remote_channel_;
297 Json::Value GetStats() {
298 stats_callback_->clearReport();
299 pc_->GetStats(stats_callback_);
301 while ((stats_callback_->getReport().empty()) && (--
count > 0)) {
302 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
304 return Json::Value(stats_callback_->getReport());
307 rtc::scoped_refptr<webrtc::PeerConnectionInterface>
313 virtual void OnAddStream(
314 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
317 webrtc::VideoTrackVector videoTracks = stream->GetVideoTracks();
318 if (videoTracks.size() > 0) {
319 video_sink_.reset(
new VideoSink(videoTracks.at(0)));
322 virtual void OnRemoveStream(
323 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
326 virtual void OnDataChannel(
327 rtc::scoped_refptr<webrtc::DataChannelInterface> channel) {
329 "PeerConnectionObserver::OnDataChannel peerid: {}",
331 remote_channel_ =
new DataChannelObserver(peer_connection_manager_,
334 virtual void OnRenegotiationNeeded() {
335 std::lock_guard<std::mutex> mutex_lock(
336 peer_connection_manager_->peerid_data_channel_mutex_);
337 peer_connection_manager_->peerid_data_channel_ready_.erase(peerid_);
339 "PeerConnectionObserver::OnRenegotiationNeeded peerid: {}",
342 virtual void OnIceCandidate(
343 const webrtc::IceCandidateInterface* candidate);
345 virtual void OnSignalingChange(
346 webrtc::PeerConnectionInterface::SignalingState state) {
349 virtual void OnIceConnectionChange(
350 webrtc::PeerConnectionInterface::IceConnectionState state) {
352 webrtc::PeerConnectionInterface::kIceConnectionFailed) ||
354 webrtc::PeerConnectionInterface::kIceConnectionClosed)) {
355 ice_candidate_list_.clear();
357 std::thread([
this]() {
358 peer_connection_manager_->HangUp(peerid_);
364 virtual void OnIceGatheringChange(
365 webrtc::PeerConnectionInterface::IceGatheringState) {}
369 const std::string peerid_;
370 rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc_;
371 DataChannelObserver* local_channel_;
372 DataChannelObserver* remote_channel_;
373 Json::Value ice_candidate_list_;
374 rtc::scoped_refptr<PeerConnectionStatsCollectorCallback>
376 std::unique_ptr<VideoSink> video_sink_;
382 const Json::Value& config,
383 const std::string& publish_filter,
384 const std::string& webrtc_udp_port_range);
388 const std::map<std::string, HttpServerRequestHandler::HttpFunction>
393 const Json::Value& json_message);
395 const Json::Value
HangUp(
const std::string& peerid);
396 const Json::Value
Call(
const std::string& peerid,
397 const std::string& window_uid,
398 const std::string& options,
399 const Json::Value& json_message);
406 void OnFrame(
const std::string& window_uid,
407 const std::shared_ptr<core::Tensor>& im);
411 const std::string& window_uid);
413 bool AddStreams(webrtc::PeerConnectionInterface* peer_connection,
414 const std::string& window_uid,
415 const std::string& options);
417 const std::string& window_uid,
418 const std::map<std::string, std::string>& opts);
421 const std::string& peerid);
425 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
429 std::unordered_map<std::string, PeerConnectionObserver*>
437 std::unordered_map<std::string,
438 rtc::scoped_refptr<BitmapTrackSourceInterface>>
443 std::unordered_map<std::string, std::set<std::string>>
452 std::map<std::string, HttpServerRequestHandler::HttpFunction>
func_;
#define LogWarning(...)
Definition: Logging.h:60
#define LogDebug(...)
Definition: Logging.h:79
#define OPEN3D_FUNCTION
Definition: Macro.h:40
Definition: PeerConnectionManager.h:73
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:126
const Json::Value AddIceCandidate(const std::string &peerid, const Json::Value &json_message)
Definition: PeerConnectionManager.cpp:251
std::mutex peerid_data_channel_mutex_
Definition: PeerConnectionManager.h:434
PeerConnectionObserver * CreatePeerConnection(const std::string &peerid)
Definition: PeerConnectionManager.cpp:522
virtual ~PeerConnectionManager()
Definition: PeerConnectionManager.cpp:200
std::mutex window_uid_to_peerids_mutex_
Definition: PeerConnectionManager.h:447
rtc::scoped_refptr< BitmapTrackSourceInterface > CreateVideoSource(const std::string &window_uid, const std::map< std::string, std::string > &opts)
Definition: PeerConnectionManager.cpp:563
bool WindowStillUsed(const std::string &window_uid)
Definition: PeerConnectionManager.cpp:417
std::mutex peerid_to_connection_mutex_
Definition: PeerConnectionManager.h:431
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:305
std::unique_ptr< webrtc::TaskQueueFactory > task_queue_factory_
Definition: PeerConnectionManager.h:424
std::unordered_map< std::string, std::set< std::string > > window_uid_to_peerids_
Definition: PeerConnectionManager.h:444
bool InitializePeerConnection()
Definition: PeerConnectionManager.cpp:516
void SendInitFramesToPeer(const std::string &peerid)
Definition: PeerConnectionManager.cpp:708
std::unordered_map< std::string, std::string > peerid_to_window_uid_
Definition: PeerConnectionManager.h:445
const Json::Value GetIceCandidateList(const std::string &peerid)
Definition: PeerConnectionManager.cpp:499
const Json::Value HangUp(const std::string &peerid)
Definition: PeerConnectionManager.cpp:435
const Json::Value GetMediaList()
Definition: PeerConnectionManager.cpp:203
void OnFrame(const std::string &window_uid, const std::shared_ptr< core::Tensor > &im)
Definition: PeerConnectionManager.cpp:732
std::list< std::string > ice_server_list_
Definition: PeerConnectionManager.h:449
const Json::Value GetIceServers()
Definition: PeerConnectionManager.cpp:217
const std::map< std::string, HttpServerRequestHandler::HttpFunction > GetHttpApi()
Definition: PeerConnectionManager.cpp:494
std::map< std::string, HttpServerRequestHandler::HttpFunction > func_
Definition: PeerConnectionManager.h:452
std::string webrtc_port_range_
Definition: PeerConnectionManager.h:453
std::unordered_map< std::string, PeerConnectionObserver * > peerid_to_connection_
Definition: PeerConnectionManager.h:430
void CloseWindowConnections(const std::string &window_uid)
Definition: PeerConnectionManager.cpp:714
rtc::scoped_refptr< webrtc::PeerConnectionInterface > GetPeerConnection(const std::string &peerid)
Definition: PeerConnectionManager.cpp:241
std::unordered_set< std::string > peerid_data_channel_ready_
Definition: PeerConnectionManager.h:433
const std::regex publish_filter_
Definition: PeerConnectionManager.h:451
rtc::scoped_refptr< BitmapTrackSourceInterface > GetVideoTrackSource(const std::string &window_uid)
Definition: PeerConnectionManager.cpp:696
bool AddStreams(webrtc::PeerConnectionInterface *peer_connection, const std::string &window_uid, const std::string &options)
Definition: PeerConnectionManager.cpp:575
std::mutex window_uid_to_track_source_mutex_
Definition: PeerConnectionManager.h:440
const Json::Value config_
Definition: PeerConnectionManager.h:450
std::unordered_map< std::string, rtc::scoped_refptr< BitmapTrackSourceInterface > > window_uid_to_track_source_
Definition: PeerConnectionManager.h:439
rtc::scoped_refptr< webrtc::PeerConnectionFactoryInterface > peer_connection_factory_
Definition: PeerConnectionManager.h:426
static std::shared_ptr< WebRTCWindowSystem > GetInstance()
Definition: WebRTCWindowSystem.cpp:111
Definition: PinholeCameraIntrinsic.cpp:16