Browse Source

Added type and role string to Description

Paul-Louis Ageneau 6 years ago
parent
commit
f54a4100a3
3 changed files with 50 additions and 20 deletions
  1. 8 1
      include/rtc/description.hpp
  2. 39 14
      src/description.cpp
  3. 3 5
      src/rtc.cpp

+ 8 - 1
include/rtc/description.hpp

@@ -34,10 +34,13 @@ public:
 	enum class Type { Unspec = 0, Offer = 1, Answer = 2 };
 	enum class Type { Unspec = 0, Offer = 1, Answer = 2 };
 	enum class Role { ActPass = 0, Passive = 1, Active = 2 };
 	enum class Role { ActPass = 0, Passive = 1, Active = 2 };
 
 
-	Description(const string &sdp, Type type = Type::Unspec, Role role = Role::ActPass);
+	Description(const string &sdp, const string &typeString = "");
+	Description(const string &sdp, Type type, Role role);
 
 
 	Type type() const;
 	Type type() const;
+	string typeString() const;
 	Role role() const;
 	Role role() const;
+	string roleString() const;
 	std::optional<string> fingerprint() const;
 	std::optional<string> fingerprint() const;
 	std::optional<uint16_t> sctpPort() const;
 	std::optional<uint16_t> sctpPort() const;
 
 
@@ -57,6 +60,10 @@ private:
 	std::optional<uint16_t> mSctpPort;
 	std::optional<uint16_t> mSctpPort;
 	std::vector<Candidate> mCandidates;
 	std::vector<Candidate> mCandidates;
 	bool mTrickle;
 	bool mTrickle;
+
+	static Type stringToType(const string &typeString);
+	static string typeToString(Type type);
+	static string roleToString(Role role);
 };
 };
 
 
 } // namespace rtc
 } // namespace rtc

+ 39 - 14
src/description.cpp

@@ -44,6 +44,9 @@ inline void trim_end(string &str) {
 
 
 namespace rtc {
 namespace rtc {
 
 
+Description::Description(const string &sdp, const string &typeString)
+    : Description(sdp, stringToType(typeString), Description::Role::ActPass) {}
+
 Description::Description(const string &sdp, Type type, Role role)
 Description::Description(const string &sdp, Type type, Role role)
     : mType(type), mRole(role), mMid("0"), mIceUfrag("0"), mIcePwd("0"), mTrickle(true) {
     : mType(type), mRole(role), mMid("0"), mIceUfrag("0"), mIcePwd("0"), mTrickle(true) {
 	if (mType == Type::Answer && mRole == Role::ActPass)
 	if (mType == Type::Answer && mRole == Role::ActPass)
@@ -88,8 +91,12 @@ Description::Description(const string &sdp, Type type, Role role)
 
 
 Description::Type Description::type() const { return mType; }
 Description::Type Description::type() const { return mType; }
 
 
+string Description::typeString() const { return typeToString(mType); }
+
 Description::Role Description::role() const { return mRole; }
 Description::Role Description::role() const { return mRole; }
 
 
+string Description::roleString() const { return roleToString(mRole); }
+
 std::optional<string> Description::fingerprint() const { return mFingerprint; }
 std::optional<string> Description::fingerprint() const { return mFingerprint; }
 
 
 std::optional<uint16_t> Description::sctpPort() const { return mSctpPort; }
 std::optional<uint16_t> Description::sctpPort() const { return mSctpPort; }
@@ -111,19 +118,6 @@ Description::operator string() const {
 	if (!mFingerprint)
 	if (!mFingerprint)
 		throw std::logic_error("Fingerprint must be set to generate a SDP");
 		throw std::logic_error("Fingerprint must be set to generate a SDP");
 
 
-	string roleStr;
-	switch (mRole) {
-	case Role::Active:
-		roleStr = "active";
-		break;
-	case Role::Passive:
-		roleStr = "passive";
-		break;
-	default:
-		roleStr = "actpass";
-		break;
-	}
-
 	std::ostringstream sdp;
 	std::ostringstream sdp;
 	sdp << "v=0\n";
 	sdp << "v=0\n";
 	sdp << "o=- " << mSessionId << " 0 IN IP4 0.0.0.0\n";
 	sdp << "o=- " << mSessionId << " 0 IN IP4 0.0.0.0\n";
@@ -136,7 +130,7 @@ Description::operator string() const {
 	if (mTrickle)
 	if (mTrickle)
 		sdp << "a=ice-options:trickle\n";
 		sdp << "a=ice-options:trickle\n";
 	sdp << "a=mid:" << mMid << "\n";
 	sdp << "a=mid:" << mMid << "\n";
-	sdp << "a=setup:" << roleStr << "\n";
+	sdp << "a=setup:" << roleToString(mRole) << "\n";
 	sdp << "a=dtls-id:1\n";
 	sdp << "a=dtls-id:1\n";
 	if (mFingerprint)
 	if (mFingerprint)
 		sdp << "a=fingerprint:sha-256 " << *mFingerprint << "\n";
 		sdp << "a=fingerprint:sha-256 " << *mFingerprint << "\n";
@@ -153,6 +147,37 @@ Description::operator string() const {
 	return sdp.str();
 	return sdp.str();
 }
 }
 
 
+Description::Type Description::stringToType(const string &typeString) {
+	if (typeString == "offer")
+		return Type::Offer;
+	else if (typeString == "answer")
+		return Type::Answer;
+	else
+		return Type::Unspec;
+}
+
+string Description::typeToString(Type type) {
+	switch (type) {
+	case Type::Offer:
+		return "offer";
+	case Type::Answer:
+		return "answer";
+	default:
+		return "";
+	}
+}
+
+string Description::roleToString(Role role) {
+	switch (role) {
+	case Role::Active:
+		return "active";
+	case Role::Passive:
+		return "passive";
+	default:
+		return "actpass";
+	}
+}
+
 } // namespace rtc
 } // namespace rtc
 
 
 std::ostream &operator<<(std::ostream &out, const rtc::Description &description) {
 std::ostream &operator<<(std::ostream &out, const rtc::Description &description) {

+ 3 - 5
src/rtc.cpp

@@ -85,8 +85,8 @@ void rtcSetLocalDescriptionCallback(int pc, void (*descriptionCallback)(const ch
 		void *userPointer = nullptr;
 		void *userPointer = nullptr;
 		if (auto jt = userPointerMap.find(pc); jt != userPointerMap.end())
 		if (auto jt = userPointerMap.find(pc); jt != userPointerMap.end())
 			userPointer = jt->second;
 			userPointer = jt->second;
-		string type = description.type() == Description::Type::Answer ? "answer" : "offer";
-		descriptionCallback(string(description).c_str(), type.c_str(), userPointer);
+		descriptionCallback(string(description).c_str(), description.typeString().c_str(),
+		                    userPointer);
 	});
 	});
 }
 }
 
 
@@ -115,9 +115,7 @@ void rtcSetRemoteDescription(int pc, const char *sdp, const char *type) {
 	if (it == peerConnectionMap.end())
 	if (it == peerConnectionMap.end())
 		return;
 		return;
 
 
-	auto t =
-	    type && string(type) == "answer" ? Description::Type::Answer : Description::Type::Offer;
-	it->second->setRemoteDescription(Description(string(sdp), t));
+	it->second->setRemoteDescription(Description(string(sdp), string(type)));
 }
 }
 
 
 void rtcAddRemoteCandidate(int pc, const char *candidate, const char *mid) {
 void rtcAddRemoteCandidate(int pc, const char *candidate, const char *mid) {