Browse Source

Generate simplified SDP for libnice

Paul-Louis Ageneau 5 years ago
parent
commit
3deb50e460
3 changed files with 60 additions and 22 deletions
  1. 1 0
      include/rtc/description.hpp
  2. 54 18
      src/description.cpp
  3. 5 4
      src/icetransport.cpp

+ 1 - 0
include/rtc/description.hpp

@@ -63,6 +63,7 @@ public:
 
 
 	operator string() const;
 	operator string() const;
 	string generateSdp(const string &eol) const;
 	string generateSdp(const string &eol) const;
+	string generateDataSdp(const string &eol) const;
 
 
 private:
 private:
 	Type mType;
 	Type mType;

+ 54 - 18
src/description.cpp

@@ -199,9 +199,6 @@ void Description::addMedia(const Description &source) {
 Description::operator string() const { return generateSdp("\r\n"); }
 Description::operator string() const { return generateSdp("\r\n"); }
 
 
 string Description::generateSdp(const string &eol) const {
 string Description::generateSdp(const string &eol) const {
-	if (!mFingerprint)
-		throw std::logic_error("Fingerprint must be set to generate an SDP string");
-
 	std::ostringstream sdp;
 	std::ostringstream sdp;
 
 
 	// Header
 	// Header
@@ -221,20 +218,6 @@ string Description::generateSdp(const string &eol) const {
 			sdp << ' ' << mData.mid;
 			sdp << ' ' << mData.mid;
 	sdp << eol;
 	sdp << eol;
 
 
-	sdp << "a=msid-semantic: WMS" << eol;
-
-	// Common
-	if (!mEnded)
-		sdp << "a=ice-options:trickle" << eol;
-
-	sdp << "a=ice-ufrag:" << mIceUfrag << eol;
-	sdp << "a=ice-pwd:" << mIcePwd << eol;
-	sdp << "a=setup:" << roleToString(mRole) << eol;
-	sdp << "a=tls-id:1" << eol;
-
-	if (mFingerprint)
-		sdp << "a=fingerprint:sha-256 " << *mFingerprint << eol;
-
 	// Non-data media
 	// Non-data media
 	if (!mMedia.empty()) {
 	if (!mMedia.empty()) {
 		// Lip-sync
 		// Lip-sync
@@ -244,7 +227,19 @@ string Description::generateSdp(const string &eol) const {
 		sdp << eol;
 		sdp << eol;
 	}
 	}
 
 
-	// Descriptions and attributes
+	// Session-level attributes
+	sdp << "a=msid-semantic: WMS" << eol;
+	sdp << "a=setup:" << roleToString(mRole) << eol;
+	sdp << "a=ice-ufrag:" << mIceUfrag << eol;
+	sdp << "a=ice-pwd:" << mIcePwd << eol;
+
+	if (!mEnded)
+		sdp << "a=ice-options:trickle" << eol;
+
+	if (mFingerprint)
+		sdp << "a=fingerprint:sha-256 " << *mFingerprint << eol;
+
+	// Media descriptions and attributes
 	for (int i = 0; i < int(mMedia.size() + 1); ++i) {
 	for (int i = 0; i < int(mMedia.size() + 1); ++i) {
 		if (auto it = mMedia.find(i); it != mMedia.end()) {
 		if (auto it = mMedia.find(i); it != mMedia.end()) {
 			// Non-data media
 			// Non-data media
@@ -264,6 +259,7 @@ string Description::generateSdp(const string &eol) const {
 			if (!mMedia.empty())
 			if (!mMedia.empty())
 				sdp << "a=bundle-only" << eol;
 				sdp << "a=bundle-only" << eol;
 			sdp << "a=mid:" << mData.mid << eol;
 			sdp << "a=mid:" << mData.mid << eol;
+			sdp << "a=tls-id:1" << eol;
 			if (mData.sctpPort)
 			if (mData.sctpPort)
 				sdp << "a=sctp-port:" << *mData.sctpPort << eol;
 				sdp << "a=sctp-port:" << *mData.sctpPort << eol;
 			if (mData.maxMessageSize)
 			if (mData.maxMessageSize)
@@ -274,6 +270,46 @@ string Description::generateSdp(const string &eol) const {
 	// Candidates
 	// Candidates
 	for (const auto &candidate : mCandidates)
 	for (const auto &candidate : mCandidates)
 		sdp << string(candidate) << eol;
 		sdp << string(candidate) << eol;
+
+	if (mEnded)
+		sdp << "a=end-of-candidates" << eol;
+
+	return sdp.str();
+}
+
+string Description::generateDataSdp(const string &eol) const {
+	std::ostringstream sdp;
+
+	// Header
+	sdp << "v=0" << eol;
+	sdp << "o=- " << mSessionId << " 0 IN IP4 127.0.0.1" << eol;
+	sdp << "s=-" << eol;
+	sdp << "t=0 0" << eol;
+
+	// Data
+	sdp << "m=application 9 UDP/DTLS/SCTP webrtc-datachannel";
+	sdp << "c=IN IP4 0.0.0.0" << eol;
+	sdp << "a=mid:" << mData.mid << eol;
+	sdp << "a=tls-id:1" << eol;
+	if (mData.sctpPort)
+		sdp << "a=sctp-port:" << *mData.sctpPort << eol;
+	if (mData.maxMessageSize)
+		sdp << "a=max-message-size:" << *mData.maxMessageSize << eol;
+
+	sdp << "a=setup:" << roleToString(mRole) << eol;
+	sdp << "a=ice-ufrag:" << mIceUfrag << eol;
+	sdp << "a=ice-pwd:" << mIcePwd << eol;
+
+	if (!mEnded)
+		sdp << "a=ice-options:trickle" << eol;
+
+	if (mFingerprint)
+		sdp << "a=fingerprint:sha-256 " << *mFingerprint << eol;
+
+	// Candidates
+	for (const auto &candidate : mCandidates)
+		sdp << string(candidate) << eol;
+
 	if (mEnded)
 	if (mEnded)
 		sdp << "a=end-of-candidates" << eol;
 		sdp << "a=end-of-candidates" << eol;
 
 

+ 5 - 4
src/icetransport.cpp

@@ -124,8 +124,8 @@ void IceTransport::setRemoteDescription(const Description &description) {
 	mRole = description.role() == Description::Role::Active ? Description::Role::Passive
 	mRole = description.role() == Description::Role::Active ? Description::Role::Passive
 	                                                        : Description::Role::Active;
 	                                                        : Description::Role::Active;
 	mMid = description.dataMid();
 	mMid = description.dataMid();
-	if (juice_set_remote_description(mAgent.get(), string(description).c_str()) < 0)
-		throw std::runtime_error("Failed to parse remote SDP");
+	if (juice_set_remote_description(mAgent.get(), description.generateDataSdp("\r\n").c_str()) < 0)
+		throw std::runtime_error("Failed to parse ICE settings from remote SDP");
 }
 }
 
 
 bool IceTransport::addRemoteCandidate(const Candidate &candidate) {
 bool IceTransport::addRemoteCandidate(const Candidate &candidate) {
@@ -487,8 +487,9 @@ void IceTransport::setRemoteDescription(const Description &description) {
 	mTrickleTimeout = !description.ended() ? 30s : 0s;
 	mTrickleTimeout = !description.ended() ? 30s : 0s;
 
 
 	// Warning: libnice expects "\n" as end of line
 	// Warning: libnice expects "\n" as end of line
-	if (nice_agent_parse_remote_sdp(mNiceAgent.get(), description.generateSdp("\n").c_str()) < 0)
-		throw std::runtime_error("Failed to parse remote SDP");
+	if (nice_agent_parse_remote_sdp(mNiceAgent.get(), description.generateDataSdp("\n").c_str()) <
+	    0)
+		throw std::runtime_error("Failed to parse ICE settings from remote SDP");
 }
 }
 
 
 bool IceTransport::addRemoteCandidate(const Candidate &candidate) {
 bool IceTransport::addRemoteCandidate(const Candidate &candidate) {