Browse Source

Properly handle SDP origin line

Paul-Louis Ageneau 4 years ago
parent
commit
37804c0327
2 changed files with 27 additions and 21 deletions
  1. 1 0
      include/rtc/description.hpp
  2. 26 21
      src/description.cpp

+ 1 - 0
include/rtc/description.hpp

@@ -202,6 +202,7 @@ private:
 
 
 	// Session-level attributes
 	// Session-level attributes
 	Role mRole;
 	Role mRole;
+	string mUsername;
 	string mSessionId;
 	string mSessionId;
 	string mIceUfrag, mIcePwd;
 	string mIceUfrag, mIcePwd;
 	std::optional<string> mFingerprint;
 	std::optional<string> mFingerprint;

+ 26 - 21
src/description.cpp

@@ -74,11 +74,6 @@ Description::Description(const string &sdp, Type type, Role role)
     : mType(Type::Unspec), mRole(role) {
     : mType(Type::Unspec), mRole(role) {
 	hintType(type);
 	hintType(type);
 
 
-	auto seed = static_cast<unsigned int>(system_clock::now().time_since_epoch().count());
-	std::default_random_engine generator(seed);
-	std::uniform_int_distribution<uint32_t> uniform;
-	mSessionId = std::to_string(uniform(generator));
-
 	int index = -1;
 	int index = -1;
 	std::shared_ptr<Entry> current;
 	std::shared_ptr<Entry> current;
 	std::istringstream ss(sdp);
 	std::istringstream ss(sdp);
@@ -89,14 +84,14 @@ Description::Description(const string &sdp, Type type, Role role)
 		if (line.empty())
 		if (line.empty())
 			continue;
 			continue;
 
 
-		// Media description line (aka m-line)
-		if (match_prefix(line, "m=")) {
-			++index;
-			string mline = line.substr(2);
-			current = createEntry(std::move(mline), std::to_string(index), Direction::Unknown);
+		if (match_prefix(line, "m=")) { // Media description line (aka m-line)
+			current = createEntry(line.substr(2), std::to_string(++index), Direction::Unknown);
+
+		} else if (match_prefix(line, "o=")) { // Origin line
+			std::istringstream origin(line.substr(2));
+			origin >> mUsername >> mSessionId;
 
 
-			// Attribute line
-		} else if (match_prefix(line, "a=")) {
+		} else if (match_prefix(line, "a=")) { // Attribute line
 			string attr = line.substr(2);
 			string attr = line.substr(2);
 			auto [key, value] = parse_pair(attr);
 			auto [key, value] = parse_pair(attr);
 
 
@@ -139,6 +134,16 @@ Description::Description(const string &sdp, Type type, Role role)
 
 
 	if (mIcePwd.empty())
 	if (mIcePwd.empty())
 		throw std::invalid_argument("Missing ice-pwd parameter in SDP description");
 		throw std::invalid_argument("Missing ice-pwd parameter in SDP description");
+
+	if (mUsername.empty())
+		mUsername = "rtc";
+
+	if (mSessionId.empty()) {
+		auto seed = static_cast<unsigned int>(system_clock::now().time_since_epoch().count());
+		std::default_random_engine generator(seed);
+		std::uniform_int_distribution<uint32_t> uniform;
+		mSessionId = std::to_string(uniform(generator));
+	}
 }
 }
 
 
 Description::Type Description::type() const { return mType; }
 Description::Type Description::type() const { return mType; }
@@ -194,7 +199,7 @@ string Description::generateSdp(string_view eol) const {
 
 
 	// Header
 	// Header
 	sdp << "v=0" << eol;
 	sdp << "v=0" << eol;
-	sdp << "o=- " << mSessionId << " 0 IN IP4 127.0.0.1" << eol;
+	sdp << "o=" << mUsername << " " << mSessionId << " 0 IN IP4 127.0.0.1" << eol;
 	sdp << "s=-" << eol;
 	sdp << "s=-" << eol;
 	sdp << "t=0 0" << eol;
 	sdp << "t=0 0" << eol;
 
 
@@ -258,7 +263,7 @@ string Description::generateApplicationSdp(string_view eol) const {
 
 
 	// Header
 	// Header
 	sdp << "v=0" << eol;
 	sdp << "v=0" << eol;
-	sdp << "o=- " << mSessionId << " 0 IN IP4 127.0.0.1" << eol;
+	sdp << "o=" << mUsername << " " << mSessionId << " 0 IN IP4 127.0.0.1" << eol;
 	sdp << "s=-" << eol;
 	sdp << "s=-" << eol;
 	sdp << "t=0 0" << eol;
 	sdp << "t=0 0" << eol;
 
 
@@ -299,11 +304,12 @@ string Description::generateApplicationSdp(string_view eol) const {
 std::optional<Candidate> Description::defaultCandidate() const {
 std::optional<Candidate> Description::defaultCandidate() const {
 	// Return the first host candidate with highest priority, favoring IPv4
 	// Return the first host candidate with highest priority, favoring IPv4
 	std::optional<Candidate> result;
 	std::optional<Candidate> result;
-	for(const auto &c : mCandidates) {
-		if(c.type() == Candidate::Type::Host) {
-			if(!result
-				|| (result->family() == Candidate::Family::Ipv6 && c.family() == Candidate::Family::Ipv4)
-				|| (result->family() == c.family() && result->priority() < c.priority()))
+	for (const auto &c : mCandidates) {
+		if (c.type() == Candidate::Type::Host) {
+			if (!result ||
+			    (result->family() == Candidate::Family::Ipv6 &&
+			     c.family() == Candidate::Family::Ipv4) ||
+			    (result->family() == c.family() && result->priority() < c.priority()))
 				result.emplace(c);
 				result.emplace(c);
 		}
 		}
 	}
 	}
@@ -544,8 +550,7 @@ Description::Media::Media(const string &sdp) : Entry(sdp, "", Direction::Unknown
 }
 }
 
 
 Description::Media::Media(const string &mline, string mid, Direction dir)
 Description::Media::Media(const string &mline, string mid, Direction dir)
-    : Entry(mline, std::move(mid), dir) {
-}
+    : Entry(mline, std::move(mid), dir) {}
 
 
 string Description::Media::description() const {
 string Description::Media::description() const {
 	std::ostringstream desc;
 	std::ostringstream desc;