Skip to content

Commit

Permalink
feat: pass custom ICE ufrag and pwd as local description init
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
achingbrain and paullouisageneau committed Jun 13, 2024
1 parent a8bf0f9 commit 264585c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
7 changes: 6 additions & 1 deletion include/rtc/peerconnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ struct RTC_CPP_EXPORT DataChannelInit {
string protocol = "";
};

struct RTC_CPP_EXPORT LocalDescriptionInit {
optional<string> iceUfrag;
optional<string> icePwd;
};

class RTC_CPP_EXPORT PeerConnection final : CheshireCat<impl::PeerConnection> {
public:
enum class State : int {
Expand Down Expand Up @@ -90,7 +95,7 @@ class RTC_CPP_EXPORT PeerConnection final : CheshireCat<impl::PeerConnection> {
uint16_t maxDataChannelId() const;
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 addRemoteCandidate(Candidate candidate);
void gatherLocalCandidates(std::vector<IceServer> additionalIceServers = {});
Expand Down
10 changes: 10 additions & 0 deletions src/impl/icetransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ IceTransport::IceTransport(const Configuration &config, candidate_callback candi
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) {
if (server.hostname.empty())
return;
Expand Down Expand Up @@ -569,6 +575,10 @@ IceTransport::IceTransport(const Configuration &config, candidate_callback candi
RecvCallback, this);
}

void IceTransport::setIceAttributes(string, string) {
PLOG_WARNING << "Setting custom ICE attributes is not supported with libnice, please use libjuice";
}

void IceTransport::addIceServer(IceServer server) {
if (server.hostname.empty())
return;
Expand Down
1 change: 1 addition & 0 deletions src/impl/icetransport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class IceTransport : public Transport {
void setRemoteDescription(const Description &description);
bool addRemoteCandidate(const Candidate &candidate);
void gatherLocalCandidates(string mid, std::vector<IceServer> additionalIceServers = {});
void setIceAttributes(string uFrag, string pwd);

optional<string> getLocalAddress() const;
optional<string> getRemoteAddress() const;
Expand Down
7 changes: 6 additions & 1 deletion src/peerconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ bool PeerConnection::hasMedia() const {
return local && local->hasAudioOrVideo();
}

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

Expand Down Expand Up @@ -140,6 +140,11 @@ void PeerConnection::setLocalDescription(Description::Type type) {
if (!iceTransport)
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);
impl()->processLocalDescription(std::move(local));

Expand Down

0 comments on commit 264585c

Please sign in to comment.