@@ -503,7 +503,17 @@ void P2PPeerConnectionChannel::OnMessageSignal(Json::Value& message) {
503
503
&sdp_mline_index);
504
504
webrtc::IceCandidateInterface* ice_candidate = webrtc::CreateIceCandidate (
505
505
sdp_mid, sdp_mline_index, candidate, nullptr );
506
- peer_connection_->AddIceCandidate (ice_candidate);
506
+ if (peer_connection_->remote_description ()){
507
+ if (!peer_connection_->AddIceCandidate (ice_candidate)) {
508
+ RTC_LOG (LS_WARNING) << " Failed to add remote candidate." ;
509
+ }
510
+ } else {
511
+ webrtc::MutexLock lock (&pending_remote_candidates_crit_);
512
+ pending_remote_candidates_.push_back (
513
+ std::unique_ptr<webrtc::IceCandidateInterface>(ice_candidate));
514
+ RTC_LOG (LS_VERBOSE) << " Remote candidate is stored because remote "
515
+ " session description is missing." ;
516
+ }
507
517
}
508
518
}
509
519
void P2PPeerConnectionChannel::OnMessageTracksAdded (
@@ -561,44 +571,47 @@ void P2PPeerConnectionChannel::OnMessageTrackSources(
561
571
void P2PPeerConnectionChannel::OnMessageStreamInfo (Json::Value& stream_info) {
562
572
// Stream information is useless in native layer.
563
573
}
574
+
564
575
void P2PPeerConnectionChannel::OnSignalingChange (
565
576
PeerConnectionInterface::SignalingState new_state) {
566
577
RTC_LOG (LS_INFO) << " Signaling state changed: " << new_state;
567
- switch (new_state) {
568
- case PeerConnectionInterface::SignalingState::kStable :
569
- if (pending_remote_sdp_) {
570
- scoped_refptr<FunctionalSetRemoteDescriptionObserver> observer =
571
- FunctionalSetRemoteDescriptionObserver::Create (std::bind (
572
- &P2PPeerConnectionChannel::OnSetRemoteDescriptionComplete, this ,
573
- std::placeholders::_1));
574
- std::string sdp_string;
575
- if (!pending_remote_sdp_->ToString (&sdp_string)) {
576
- RTC_LOG (LS_ERROR) << " Error parsing local description." ;
577
- RTC_DCHECK (false );
578
- }
579
- std::vector<AudioCodec> audio_codecs;
580
- for (auto & audio_enc_param : configuration_.audio ) {
581
- audio_codecs.push_back (audio_enc_param.codec .name );
582
- }
583
- sdp_string = SdpUtils::SetPreferAudioCodecs (sdp_string, audio_codecs);
584
- std::vector<VideoCodec> video_codecs;
585
- for (auto & video_enc_param : configuration_.video ) {
586
- video_codecs.push_back (video_enc_param.codec .name );
587
- }
588
- sdp_string = SdpUtils::SetPreferVideoCodecs (sdp_string, video_codecs);
589
- std::unique_ptr<webrtc::SessionDescriptionInterface> new_desc (
590
- webrtc::CreateSessionDescription (pending_remote_sdp_->type (),
591
- sdp_string, nullptr ));
592
- pending_remote_sdp_.reset ();
593
- peer_connection_->SetRemoteDescription (std::move (new_desc), observer);
594
- } else {
595
- CheckWaitedList ();
578
+ if (new_state == PeerConnectionInterface::SignalingState::kStable ) {
579
+ if (pending_remote_sdp_) {
580
+ scoped_refptr<FunctionalSetRemoteDescriptionObserver> observer =
581
+ FunctionalSetRemoteDescriptionObserver::Create (std::bind (
582
+ &P2PPeerConnectionChannel::OnSetRemoteDescriptionComplete, this ,
583
+ std::placeholders::_1));
584
+ std::string sdp_string;
585
+ if (!pending_remote_sdp_->ToString (&sdp_string)) {
586
+ RTC_LOG (LS_ERROR) << " Error parsing local description." ;
587
+ RTC_DCHECK (false );
596
588
}
597
- break ;
598
- default :
599
- break ;
589
+ std::vector<AudioCodec> audio_codecs;
590
+ for (auto & audio_enc_param : configuration_.audio ) {
591
+ audio_codecs.push_back (audio_enc_param.codec .name );
592
+ }
593
+ sdp_string = SdpUtils::SetPreferAudioCodecs (sdp_string, audio_codecs);
594
+ std::vector<VideoCodec> video_codecs;
595
+ for (auto & video_enc_param : configuration_.video ) {
596
+ video_codecs.push_back (video_enc_param.codec .name );
597
+ }
598
+ sdp_string = SdpUtils::SetPreferVideoCodecs (sdp_string, video_codecs);
599
+ std::unique_ptr<webrtc::SessionDescriptionInterface> new_desc (
600
+ webrtc::CreateSessionDescription (pending_remote_sdp_->type (),
601
+ sdp_string, nullptr ));
602
+ pending_remote_sdp_.reset ();
603
+ peer_connection_->SetRemoteDescription (std::move (new_desc), observer);
604
+ } else {
605
+ CheckWaitedList ();
606
+ }
607
+ }
608
+
609
+ if (new_state == PeerConnectionInterface::SignalingState::kStable ||
610
+ new_state == PeerConnectionInterface::SignalingState::kHaveRemoteOffer ) {
611
+ DrainPendingRemoteCandidates ();
600
612
}
601
613
}
614
+
602
615
void P2PPeerConnectionChannel::OnAddStream (
603
616
rtc::scoped_refptr<MediaStreamInterface> stream) {
604
617
Json::Value stream_tracks;
@@ -1247,6 +1260,17 @@ void P2PPeerConnectionChannel::DrainPendingMessages() {
1247
1260
pending_messages_.clear ();
1248
1261
}
1249
1262
}
1263
+
1264
+ void P2PPeerConnectionChannel::DrainPendingRemoteCandidates () {
1265
+ webrtc::MutexLock lock (&pending_remote_candidates_crit_);
1266
+ for (auto & ice_candidate : pending_remote_candidates_) {
1267
+ if (!peer_connection_->AddIceCandidate (ice_candidate.get ())) {
1268
+ RTC_LOG (LS_WARNING) << " Failed to add remote candidate." ;
1269
+ }
1270
+ }
1271
+ pending_remote_candidates_.clear ();
1272
+ }
1273
+
1250
1274
Json::Value P2PPeerConnectionChannel::UaInfo () {
1251
1275
Json::Value ua;
1252
1276
// SDK info includes verison and type.
0 commit comments