Skip to content

Commit 264585c

Browse files
feat: pass custom ICE ufrag and pwd as local description init
Adds an optional second argument to `rtc::PeerConnection::setLocalDescription` that can contain an ICE ufrag and pwd that if passed will be used in place of the randomly generated versions. Refs: #1166 Refs: #1201 (comment) Co-authored-by: Paul-Louis Ageneau <paul-louis@ageneau.org>
1 parent a8bf0f9 commit 264585c

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

include/rtc/peerconnection.hpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ struct RTC_CPP_EXPORT DataChannelInit {
3535
string protocol = "";
3636
};
3737

38+
struct RTC_CPP_EXPORT LocalDescriptionInit {
39+
optional<string> iceUfrag;
40+
optional<string> icePwd;
41+
};
42+
3843
class RTC_CPP_EXPORT PeerConnection final : CheshireCat<impl::PeerConnection> {
3944
public:
4045
enum class State : int {
@@ -90,7 +95,7 @@ class RTC_CPP_EXPORT PeerConnection final : CheshireCat<impl::PeerConnection> {
9095
uint16_t maxDataChannelId() const;
9196
bool getSelectedCandidatePair(Candidate *local, Candidate *remote);
9297

93-
void setLocalDescription(Description::Type type = Description::Type::Unspec);
98+
void setLocalDescription(Description::Type type = Description::Type::Unspec, LocalDescriptionInit init = {});
9499
void setRemoteDescription(Description description);
95100
void addRemoteCandidate(Candidate candidate);
96101
void gatherLocalCandidates(std::vector<IceServer> additionalIceServers = {});

src/impl/icetransport.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ IceTransport::IceTransport(const Configuration &config, candidate_callback candi
140140
addIceServer(server);
141141
}
142142

143+
void IceTransport::setIceAttributes(string uFrag, string pwd) {
144+
if (juice_set_local_ice_attributes(mAgent.get(), uFrag.c_str(), pwd.c_str()) < 0) {
145+
throw std::invalid_argument("Invalid ICE attributes");
146+
}
147+
}
148+
143149
void IceTransport::addIceServer(IceServer server) {
144150
if (server.hostname.empty())
145151
return;
@@ -569,6 +575,10 @@ IceTransport::IceTransport(const Configuration &config, candidate_callback candi
569575
RecvCallback, this);
570576
}
571577

578+
void IceTransport::setIceAttributes(string, string) {
579+
PLOG_WARNING << "Setting custom ICE attributes is not supported with libnice, please use libjuice";
580+
}
581+
572582
void IceTransport::addIceServer(IceServer server) {
573583
if (server.hostname.empty())
574584
return;

src/impl/icetransport.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class IceTransport : public Transport {
5151
void setRemoteDescription(const Description &description);
5252
bool addRemoteCandidate(const Candidate &candidate);
5353
void gatherLocalCandidates(string mid, std::vector<IceServer> additionalIceServers = {});
54+
void setIceAttributes(string uFrag, string pwd);
5455

5556
optional<string> getLocalAddress() const;
5657
optional<string> getRemoteAddress() const;

src/peerconnection.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ bool PeerConnection::hasMedia() const {
7676
return local && local->hasAudioOrVideo();
7777
}
7878

79-
void PeerConnection::setLocalDescription(Description::Type type) {
79+
void PeerConnection::setLocalDescription(Description::Type type, LocalDescriptionInit init) {
8080
std::unique_lock signalingLock(impl()->signalingMutex);
8181
PLOG_VERBOSE << "Setting local description, type=" << Description::typeToString(type);
8282

@@ -140,6 +140,11 @@ void PeerConnection::setLocalDescription(Description::Type type) {
140140
if (!iceTransport)
141141
return; // closed
142142

143+
if (init.iceUfrag && init.icePwd) {
144+
PLOG_DEBUG << "Using custom ICE attributes, ufrag=\"" << init.iceUfrag.value() << "\", pwd=\"" << init.icePwd.value() << "\"";
145+
iceTransport->setIceAttributes(init.iceUfrag.value(), init.icePwd.value());
146+
}
147+
143148
Description local = iceTransport->getLocalDescription(type);
144149
impl()->processLocalDescription(std::move(local));
145150

0 commit comments

Comments
 (0)