Browse Source

Merge pull request #1207 from achingbrain/feat/allow-overriding-ice-ufrag-and-pwd2

feat: pass custom ICE ufrag and pwd as local description init
Paul-Louis Ageneau 1 year ago
parent
commit
4261b4fd10
4 changed files with 23 additions and 2 deletions
  1. 6 1
      include/rtc/peerconnection.hpp
  2. 10 0
      src/impl/icetransport.cpp
  3. 1 0
      src/impl/icetransport.hpp
  4. 6 1
      src/peerconnection.cpp

+ 6 - 1
include/rtc/peerconnection.hpp

@@ -35,6 +35,11 @@ struct RTC_CPP_EXPORT DataChannelInit {
 	string protocol = "";
 	string protocol = "";
 };
 };
 
 
+struct RTC_CPP_EXPORT LocalDescriptionInit {
+    optional<string> iceUfrag;
+    optional<string> icePwd;
+};
+
 class RTC_CPP_EXPORT PeerConnection final : CheshireCat<impl::PeerConnection> {
 class RTC_CPP_EXPORT PeerConnection final : CheshireCat<impl::PeerConnection> {
 public:
 public:
 	enum class State : int {
 	enum class State : int {
@@ -90,7 +95,7 @@ public:
 	uint16_t maxDataChannelId() const;
 	uint16_t maxDataChannelId() const;
 	bool getSelectedCandidatePair(Candidate *local, Candidate *remote);
 	bool getSelectedCandidatePair(Candidate *local, Candidate *remote);
 
 
-	void setLocalDescription(Description::Type type = Description::Type::Unspec);
+	void setLocalDescription(Description::Type type = Description::Type::Unspec, LocalDescriptionInit init = {});
 	void setRemoteDescription(Description description);
 	void setRemoteDescription(Description description);
 	void addRemoteCandidate(Candidate candidate);
 	void addRemoteCandidate(Candidate candidate);
 	void gatherLocalCandidates(std::vector<IceServer> additionalIceServers = {});
 	void gatherLocalCandidates(std::vector<IceServer> additionalIceServers = {});

+ 10 - 0
src/impl/icetransport.cpp

@@ -140,6 +140,12 @@ IceTransport::IceTransport(const Configuration &config, candidate_callback candi
 			addIceServer(server);
 			addIceServer(server);
 }
 }
 
 
+void IceTransport::setIceAttributes(string uFrag, string pwd) {
+	if (juice_set_local_ice_attributes(mAgent.get(), uFrag.c_str(), pwd.c_str()) < 0) {
+		throw std::invalid_argument("Invalid ICE attributes");
+	}
+}
+
 void IceTransport::addIceServer(IceServer server) {
 void IceTransport::addIceServer(IceServer server) {
 	if (server.hostname.empty())
 	if (server.hostname.empty())
 		return;
 		return;
@@ -569,6 +575,10 @@ IceTransport::IceTransport(const Configuration &config, candidate_callback candi
 	                       RecvCallback, this);
 	                       RecvCallback, this);
 }
 }
 
 
+void IceTransport::setIceAttributes([[maybe_unused]] string uFrag, [[maybe_unused]] string pwd) {
+	PLOG_WARNING << "Setting custom ICE attributes is not supported with libnice, please use libjuice";
+}
+
 void IceTransport::addIceServer(IceServer server) {
 void IceTransport::addIceServer(IceServer server) {
 	if (server.hostname.empty())
 	if (server.hostname.empty())
 		return;
 		return;

+ 1 - 0
src/impl/icetransport.hpp

@@ -51,6 +51,7 @@ public:
 	void setRemoteDescription(const Description &description);
 	void setRemoteDescription(const Description &description);
 	bool addRemoteCandidate(const Candidate &candidate);
 	bool addRemoteCandidate(const Candidate &candidate);
 	void gatherLocalCandidates(string mid, std::vector<IceServer> additionalIceServers = {});
 	void gatherLocalCandidates(string mid, std::vector<IceServer> additionalIceServers = {});
+	void setIceAttributes(string uFrag, string pwd);
 
 
 	optional<string> getLocalAddress() const;
 	optional<string> getLocalAddress() const;
 	optional<string> getRemoteAddress() const;
 	optional<string> getRemoteAddress() const;

+ 6 - 1
src/peerconnection.cpp

@@ -76,7 +76,7 @@ bool PeerConnection::hasMedia() const {
 	return local && local->hasAudioOrVideo();
 	return local && local->hasAudioOrVideo();
 }
 }
 
 
-void PeerConnection::setLocalDescription(Description::Type type) {
+void PeerConnection::setLocalDescription(Description::Type type, LocalDescriptionInit init) {
 	std::unique_lock signalingLock(impl()->signalingMutex);
 	std::unique_lock signalingLock(impl()->signalingMutex);
 	PLOG_VERBOSE << "Setting local description, type=" << Description::typeToString(type);
 	PLOG_VERBOSE << "Setting local description, type=" << Description::typeToString(type);
 
 
@@ -140,6 +140,11 @@ void PeerConnection::setLocalDescription(Description::Type type) {
 	if (!iceTransport)
 	if (!iceTransport)
 		return; // closed
 		return; // closed
 
 
+	if (init.iceUfrag && init.icePwd) {
+		PLOG_DEBUG << "Using custom ICE attributes, ufrag=\"" << init.iceUfrag.value() << "\", pwd=\"" << init.icePwd.value() << "\"";
+		iceTransport->setIceAttributes(init.iceUfrag.value(), init.icePwd.value());
+	}
+
 	Description local = iceTransport->getLocalDescription(type);
 	Description local = iceTransport->getLocalDescription(type);
 	impl()->processLocalDescription(std::move(local));
 	impl()->processLocalDescription(std::move(local));