Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Commit d73ea4c

Browse files
authored
Adding remote candidates only after having remote session description. (#478)
1 parent 21ff40c commit d73ea4c

File tree

2 files changed

+62
-33
lines changed

2 files changed

+62
-33
lines changed

talk/owt/sdk/p2p/p2ppeerconnectionchannel.cc

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,17 @@ void P2PPeerConnectionChannel::OnMessageSignal(Json::Value& message) {
503503
&sdp_mline_index);
504504
webrtc::IceCandidateInterface* ice_candidate = webrtc::CreateIceCandidate(
505505
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+
rtc::CritScope cs(&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+
}
507517
}
508518
}
509519
void P2PPeerConnectionChannel::OnMessageTracksAdded(
@@ -561,44 +571,47 @@ void P2PPeerConnectionChannel::OnMessageTrackSources(
561571
void P2PPeerConnectionChannel::OnMessageStreamInfo(Json::Value& stream_info) {
562572
// Stream information is useless in native layer.
563573
}
574+
564575
void P2PPeerConnectionChannel::OnSignalingChange(
565576
PeerConnectionInterface::SignalingState new_state) {
566577
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);
596588
}
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();
600612
}
601613
}
614+
602615
void P2PPeerConnectionChannel::OnAddStream(
603616
rtc::scoped_refptr<MediaStreamInterface> stream) {
604617
Json::Value stream_tracks;
@@ -1247,6 +1260,17 @@ void P2PPeerConnectionChannel::DrainPendingMessages() {
12471260
pending_messages_.clear();
12481261
}
12491262
}
1263+
1264+
void P2PPeerConnectionChannel::DrainPendingRemoteCandidates() {
1265+
rtc::CritScope cs(&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+
12501274
Json::Value P2PPeerConnectionChannel::UaInfo() {
12511275
Json::Value ua;
12521276
// SDK info includes verison and type.

talk/owt/sdk/p2p/p2ppeerconnectionchannel.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ class P2PPeerConnectionChannel : public P2PSignalingReceiverInterface,
158158
// Send all messages in pending message list.
159159
void DrainPendingMessages();
160160
// Cleans all variables associated with last peerconnection.
161+
void DrainPendingRemoteCandidates();
161162
void CleanLastPeerConnection();
162163
// Returns user agent info as JSON object.
163164
Json::Value UaInfo();
@@ -211,6 +212,10 @@ class P2PPeerConnectionChannel : public P2PSignalingReceiverInterface,
211212
pending_messages_;
212213
// Protects |pending_messages_|.
213214
std::mutex pending_messages_mutex_;
215+
rtc::CriticalSection pending_remote_candidates_crit_;
216+
std::vector<std::unique_ptr<webrtc::IceCandidateInterface>>
217+
pending_remote_candidates_
218+
RTC_GUARDED_BY(pending_remote_candidates_crit_);
214219
// Indicates whether remote client supports WebRTC Plan B
215220
// (https://tools.ietf.org/html/draft-uberti-rtcweb-plan-00).
216221
// If plan B is not supported, at most one audio/video track is supported.

0 commit comments

Comments
 (0)