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) {
114 utility::LogWarning(
"{}", error.message());
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) {
147 utility::LogWarning(
"{}", error.message());
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(),
239 utility::LogDebug(
"DataChannelObserver::OnMessage: {}, msg: {}.",
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_;
295 Json::Value GetIceCandidateList() {
return ice_candidate_list_; }
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>
308 GetPeerConnection() {
313 virtual void OnAddStream(
314 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
315 utility::LogDebug(
"[{}] GetVideoTracks().size(): {}.",
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) {
347 utility::LogDebug(
"state: {}, peerid: {}", state, peerid_);
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_;