Skip to content

Commit

Permalink
feat: support setting ICE ufrag and pwd, and missing config values
Browse files Browse the repository at this point in the history
Updates the implementation to match the latest libdatachannel with
features for setting ICE ufrag/pwd and reading the remote cert
fingerprint.

Also adds pass-through for missing config values.
  • Loading branch information
achingbrain committed Jan 14, 2025
1 parent b0caa62 commit 798dbe3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
59 changes: 57 additions & 2 deletions src/cpp/peer-connection-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Napi::Object PeerConnectionWrapper::Init(Napi::Env env, Napi::Object exports)
InstanceMethod("setRemoteDescription", &PeerConnectionWrapper::setRemoteDescription),
InstanceMethod("localDescription", &PeerConnectionWrapper::localDescription),
InstanceMethod("remoteDescription", &PeerConnectionWrapper::remoteDescription),
InstanceMethod("remoteFingerprint", &PeerConnectionWrapper::remoteFingerprint),
InstanceMethod("addRemoteCandidate", &PeerConnectionWrapper::addRemoteCandidate),
InstanceMethod("createDataChannel", &PeerConnectionWrapper::createDataChannel),
InstanceMethod("addTrack", &PeerConnectionWrapper::addTrack),
Expand Down Expand Up @@ -214,6 +215,10 @@ PeerConnectionWrapper::PeerConnectionWrapper(const Napi::CallbackInfo &info) : N
if (config.Get("disableAutoNegotiation").IsBoolean())
rtcConfig.disableAutoNegotiation = config.Get("disableAutoNegotiation").As<Napi::Boolean>();

// disableAutoGathering option
if (config.Get("disableAutoGathering").IsBoolean())
rtcConfig.disableAutoGathering = config.Get("disableAutoGathering").As<Napi::Boolean>();

// forceMediaTransport option
if (config.Get("forceMediaTransport").IsBoolean())
rtcConfig.forceMediaTransport = config.Get("forceMediaTransport").As<Napi::Boolean>();
Expand Down Expand Up @@ -331,6 +336,7 @@ void PeerConnectionWrapper::setLocalDescription(const Napi::CallbackInfo &info)
}

rtc::Description::Type type = rtc::Description::Type::Unspec;
rtc::LocalDescriptionInit init;

// optional
if (length > 0)
Expand All @@ -356,7 +362,29 @@ void PeerConnectionWrapper::setLocalDescription(const Napi::CallbackInfo &info)
type = rtc::Description::Type::Rollback;
}

mRtcPeerConnPtr->setLocalDescription(type);
// optional
if (length > 1)
{
PLOG_DEBUG << "setLocalDescription() called with LocalDescriptionInit";

if (info[1].IsObject())
{
PLOG_DEBUG << "setLocalDescription() called with LocalDescriptionInit as object";
Napi::Object obj = info[1].As<Napi::Object>();

if (obj.Get("iceUfrag").IsString()) {
PLOG_DEBUG << "setLocalDescription() has ufrag";
init.iceUfrag = obj.Get("iceUfrag").As<Napi::String>();
}

if (obj.Get("icePwd").IsString()) {
PLOG_DEBUG << "setLocalDescription() has password";
init.icePwd = obj.Get("icePwd").As<Napi::String>();
}
}
}

mRtcPeerConnPtr->setLocalDescription(type, init);
}

void PeerConnectionWrapper::setRemoteDescription(const Napi::CallbackInfo &info)
Expand Down Expand Up @@ -1002,7 +1030,34 @@ Napi::Value PeerConnectionWrapper::maxMessageSize(const Napi::CallbackInfo &info

try
{
return Napi::Number::New(env, mRtcPeerConnPtr->remoteMaxMessageSize());
return Napi::Array::New(env, mRtcPeerConnPtr->remoteMaxMessageSize());
}
catch (std::exception &ex)
{
Napi::Error::New(env, std::string("libdatachannel error: ") + ex.what()).ThrowAsJavaScriptException();
return Napi::Number::New(info.Env(), 0);
}
}

Napi::Value PeerConnectionWrapper::remoteFingerprint(const Napi::CallbackInfo &info)
{
PLOG_DEBUG << "remoteFingerprints() called";
Napi::Env env = info.Env();

if (!mRtcPeerConnPtr)
{
return Napi::Number::New(info.Env(), 0);
}

try
{
auto fingerprint = mRtcPeerConnPtr->remoteFingerprint();

Napi::Object fingerprintObject = Napi::Object::New(env);
fingerprintObject.Set("value", fingerprint.value);
fingerprintObject.Set("algorithm", rtc::CertificateFingerprint::AlgorithmIdentifier(fingerprint.algorithm));

return fingerprintObject;
}
catch (std::exception &ex)
{
Expand Down
1 change: 1 addition & 0 deletions src/cpp/peer-connection-wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class PeerConnectionWrapper : public Napi::ObjectWrap<PeerConnectionWrapper>
Napi::Value iceState(const Napi::CallbackInfo &info);
Napi::Value signalingState(const Napi::CallbackInfo &info);
Napi::Value gatheringState(const Napi::CallbackInfo &info);
Napi::Value remoteFingerprint(const Napi::CallbackInfo &info);

// Callbacks
void onLocalDescription(const Napi::CallbackInfo &info);
Expand Down

0 comments on commit 798dbe3

Please sign in to comment.