Browse Source

Prevent whitspaces at the end of candidates as they confuse libnice

Paul-Louis Ageneau 4 years ago
parent
commit
f033e4ab8f
2 changed files with 11 additions and 4 deletions
  1. 7 2
      src/candidate.cpp
  2. 4 2
      src/icetransport.cpp

+ 7 - 2
src/candidate.cpp

@@ -20,6 +20,7 @@
 
 #include <algorithm>
 #include <array>
+#include <cctype>
 #include <sstream>
 #include <unordered_map>
 
@@ -93,7 +94,7 @@ void Candidate::parse(string candidate) {
 		throw std::invalid_argument("Invalid candidate format");
 
 	std::getline(iss, mTail);
-	while (!mTail.empty() && mTail.front() == ' ')
+	while (!mTail.empty() && std::isspace(mTail.front()))
 		mTail.erase(mTail.begin());
 
 	if (auto it = TypeMap.find(type); it != TypeMap.end())
@@ -187,7 +188,11 @@ string Candidate::candidate() const {
 	else
 		oss << mNode << sp << mService;
 
-	oss << sp << "typ" << sp << mTypeString << sp << mTail;
+	oss << sp << "typ" << sp << mTypeString;
+
+	if (!mTail.empty())
+		oss << sp << mTail;
+
 	return oss.str();
 }
 

+ 4 - 2
src/icetransport.cpp

@@ -564,12 +564,14 @@ bool IceTransport::addRemoteCandidate(const Candidate &candidate) {
 		return false;
 
 	// Warning: the candidate string must start with "a=candidate:" and it must not end with a
-	// newline, else libnice will reject it.
+	// newline or whitespace, else libnice will reject it.
 	string sdp(candidate);
 	NiceCandidate *cand =
 	    nice_agent_parse_remote_candidate_sdp(mNiceAgent.get(), mStreamId, sdp.c_str());
-	if (!cand)
+	if (!cand) {
+		PLOG_WARNING << "Rejected ICE candidate: " << sdp;
 		return false;
+	}
 
 	GSList *list = g_slist_append(nullptr, cand);
 	int ret = nice_agent_set_remote_candidates(mNiceAgent.get(), mStreamId, 1, list);