Skip to content

Commit

Permalink
expose maxDataChannelId and expand candidate-info
Browse files Browse the repository at this point in the history
  • Loading branch information
murat-dogan committed Sep 17, 2023
1 parent 05531dc commit 4843bab
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ export interface SelectedCandidateInfo {
port: number;
type: string;
transportType: string;
candidate: string;
mid: string;
priority: number;
}

// Must be same as rtc enum class Direction
Expand Down Expand Up @@ -232,6 +235,7 @@ export class PeerConnection {
bytesReceived(): number;
rtt(): number;
getSelectedCandidatePair(): { local: SelectedCandidateInfo; remote: SelectedCandidateInfo } | null;
maxDataChannelId(): number;
}

export class DataChannelStream extends stream.Duplex {
Expand Down
28 changes: 28 additions & 0 deletions src/peer-connection-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Napi::Object PeerConnectionWrapper::Init(Napi::Env env, Napi::Object exports)
InstanceMethod("bytesReceived", &PeerConnectionWrapper::bytesReceived),
InstanceMethod("rtt", &PeerConnectionWrapper::rtt),
InstanceMethod("getSelectedCandidatePair", &PeerConnectionWrapper::getSelectedCandidatePair),
InstanceMethod("maxDataChannelId", &PeerConnectionWrapper::maxDataChannelId),
});

constructor = Napi::Persistent(func);
Expand Down Expand Up @@ -984,11 +985,17 @@ Napi::Value PeerConnectionWrapper::getSelectedCandidatePair(const Napi::Callback
localObj.Set("port", local.port().value_or(0));
localObj.Set("type", candidateTypeToString(local.type()));
localObj.Set("transportType", candidateTransportTypeToString(local.transportType()));
localObj.Set("candidate", local.candidate());
localObj.Set("mid", local.mid());
localObj.Set("priority", local.priority());

remoteObj.Set("address", remote.address().value_or("?"));
remoteObj.Set("port", remote.port().value_or(0));
remoteObj.Set("type", candidateTypeToString(remote.type()));
remoteObj.Set("transportType", candidateTransportTypeToString(remote.transportType()));
remoteObj.Set("candidate", remote.candidate());
remoteObj.Set("mid", remote.mid());
remoteObj.Set("priority", remote.priority());

retvalue.Set("local", localObj);
retvalue.Set("remote", remoteObj);
Expand All @@ -1002,6 +1009,27 @@ Napi::Value PeerConnectionWrapper::getSelectedCandidatePair(const Napi::Callback
}
}

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

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

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

std::string PeerConnectionWrapper::candidateTypeToString(const rtc::Candidate::Type &type)
{
PLOG_DEBUG << "candidateTypeToString() called";
Expand Down
1 change: 1 addition & 0 deletions src/peer-connection-wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class PeerConnectionWrapper : public Napi::ObjectWrap<PeerConnectionWrapper>
Napi::Value bytesReceived(const Napi::CallbackInfo &info);
Napi::Value rtt(const Napi::CallbackInfo &info);
Napi::Value getSelectedCandidatePair(const Napi::CallbackInfo &info);
Napi::Value maxDataChannelId(const Napi::CallbackInfo &info);

// Close all existing Peer Connections
static void CloseAll();
Expand Down

0 comments on commit 4843bab

Please sign in to comment.