Skip to content

Commit bdee61e

Browse files
authored
Merge branch 'master' into fix/use-binarytype-correctly
2 parents 1b7152d + 3183538 commit bdee61e

17 files changed

+353
-13
lines changed

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ test/wpt-tests/wpt
22
vcpkg
33
out
44
.webpack
5+
build
6+
dist
7+
prebuilds

API.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,19 @@ export interface RtcConfig {
1616
bindAddress?: string;
1717
enableIceTcp?: boolean;
1818
enableIceUdpMux?: boolean;
19+
disableAutoNegotiation?: boolean;
20+
disableFingerprintVerification?: boolean;
21+
disableAutoGathering?: boolean;
22+
forceMediaTransport?: boolean;
1923
portRangeBegin?: number;
2024
portRangeEnd?: number;
2125
maxMessageSize?: number;
2226
mtu?: number;
2327
iceTransportPolicy?: TransportPolicy;
2428
disableFingerprintVerification?: boolean;
29+
certificatePemFile?: string;
30+
keyPemFile?: string;
31+
keyPemPass?: string;
2532
}
2633
2734
export const enum RelayType {
@@ -69,6 +76,29 @@ export const enum DescriptionType {
6976
}
7077
```
7178

79+
**setLocalDescription: (sdp: string, init?: LocalDescriptionInit) => void**
80+
81+
Set Local Description and optionally the ICE ufrag/pwd to use. These should not
82+
be set as they will be generated automatically as per the spec.
83+
84+
```
85+
export interface LocalDescriptionInit {
86+
iceUfrag?: string;
87+
icePwd?: string;
88+
}
89+
```
90+
91+
**remoteFingerprint: () => CertificateFingerprint**
92+
93+
Returns the certificate fingerprint used by the remote peer
94+
95+
```
96+
export interface CertificateFingerprint {
97+
value: string;
98+
algorithm: 'sha-1' | 'sha-224' | 'sha-256' | 'sha-384' | 'sha-512' | 'md5' | 'md2';
99+
}
100+
```
101+
72102
**addRemoteCandidate: (candidate: string, mid: string) => void**
73103

74104
Add remote candidate info

BULDING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Other Options
2525
```
2626

2727
Compile without Media and Websocket
28+
2829
```sh
2930
npx cmake-js clean
3031
npx cmake-js configure --CDNO_MEDIA=ON --CDNO_WEBSOCKET=ON

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ include(FetchContent)
3232
FetchContent_Declare(
3333
libdatachannel
3434
GIT_REPOSITORY https://github.com/paullouisageneau/libdatachannel.git
35-
GIT_TAG "aa57f4285936a878fb96f4f84085ee84d20c3397" # v0.22.2+
35+
GIT_TAG "v0.23.0"
3636
)
3737

3838
option(NO_MEDIA "Disable media transport support in libdatachannel" OFF)
@@ -50,6 +50,7 @@ endif()
5050
set(SRC_FILES
5151
src/cpp/rtc-wrapper.cpp
5252
src/cpp/data-channel-wrapper.cpp
53+
src/cpp/ice-udp-mux-listener-wrapper.cpp
5354
src/cpp/peer-connection-wrapper.cpp
5455
src/cpp/thread-safe-callback.cpp
5556
src/cpp/main.cpp

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-datachannel",
3-
"version": "0.28.0",
3+
"version": "0.29.0-dev",
44
"description": "WebRTC For Node.js and Electron. libdatachannel node bindings.",
55
"main": "./dist/cjs/lib/index.cjs",
66
"module": "./dist/esm/lib/index.mjs",
@@ -50,7 +50,8 @@
5050
"wpt:server": "cd test/wpt-tests/wpt && ./wpt serve",
5151
"wpt:test": "ts-node test/wpt-tests/index.ts",
5252
"_prebuild": "prebuild -r napi --backend cmake-js",
53-
"prepack": "npm run build:tsc"
53+
"prepack": "npm run build:tsc",
54+
"prettier": "prettier --write ."
5455
},
5556
"binary": {
5657
"napi_versions": [
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#include "ice-udp-mux-listener-wrapper.h"
2+
3+
#include "plog/Log.h"
4+
5+
#include <cctype>
6+
#include <sstream>
7+
8+
Napi::FunctionReference IceUdpMuxListenerWrapper::constructor = Napi::FunctionReference();
9+
std::unordered_set<IceUdpMuxListenerWrapper *> IceUdpMuxListenerWrapper::instances;
10+
11+
void IceUdpMuxListenerWrapper::StopAll()
12+
{
13+
PLOG_DEBUG << "IceUdpMuxListenerWrapper StopAll() called";
14+
auto copy(instances);
15+
for (auto inst : copy)
16+
inst->doCleanup();
17+
}
18+
19+
Napi::Object IceUdpMuxListenerWrapper::Init(Napi::Env env, Napi::Object exports)
20+
{
21+
Napi::HandleScope scope(env);
22+
23+
Napi::Function func = DefineClass(
24+
env,
25+
"IceUdpMuxListener",
26+
{
27+
InstanceMethod("stop", &IceUdpMuxListenerWrapper::stop),
28+
InstanceMethod("onUnhandledStunRequest", &IceUdpMuxListenerWrapper::onUnhandledStunRequest),
29+
InstanceMethod("port", &IceUdpMuxListenerWrapper::port),
30+
InstanceMethod("address", &IceUdpMuxListenerWrapper::address)
31+
});
32+
33+
// If this is not the first call, we don't want to reassign the constructor (hot-reload problem)
34+
if(constructor.IsEmpty())
35+
{
36+
constructor = Napi::Persistent(func);
37+
constructor.SuppressDestruct();
38+
}
39+
40+
exports.Set("IceUdpMuxListener", func);
41+
return exports;
42+
}
43+
44+
IceUdpMuxListenerWrapper::IceUdpMuxListenerWrapper(const Napi::CallbackInfo &info) : Napi::ObjectWrap<IceUdpMuxListenerWrapper>(info)
45+
{
46+
PLOG_DEBUG << "IceUdpMuxListenerWrapper Constructor called";
47+
Napi::Env env = info.Env();
48+
int length = info.Length();
49+
50+
// We expect (Number, String?) as param
51+
if (length > 0 && info[0].IsNumber()) {
52+
// Port
53+
mPort = info[0].As<Napi::Number>().ToNumber().Uint32Value();
54+
} else {
55+
Napi::TypeError::New(env, "Port (Number) and optional Address (String) expected").ThrowAsJavaScriptException();
56+
return;
57+
}
58+
59+
if (length > 1 && info[1].IsString()) {
60+
// Address
61+
mAddress = info[1].As<Napi::String>().ToString();
62+
}
63+
64+
iceUdpMuxListenerPtr = std::make_unique<rtc::IceUdpMuxListener>(mPort, mAddress);
65+
instances.insert(this);
66+
}
67+
68+
IceUdpMuxListenerWrapper::~IceUdpMuxListenerWrapper()
69+
{
70+
PLOG_DEBUG << "IceUdpMuxListenerWrapper Destructor called";
71+
doCleanup();
72+
}
73+
74+
void IceUdpMuxListenerWrapper::doCleanup()
75+
{
76+
PLOG_DEBUG << "IceUdpMuxListenerWrapper::doCleanup() called";
77+
78+
if (iceUdpMuxListenerPtr)
79+
{
80+
iceUdpMuxListenerPtr->stop();
81+
iceUdpMuxListenerPtr.reset();
82+
}
83+
84+
mOnUnhandledStunRequestCallback.reset();
85+
instances.erase(this);
86+
}
87+
88+
Napi::Value IceUdpMuxListenerWrapper::port(const Napi::CallbackInfo &info)
89+
{
90+
Napi::Env env = info.Env();
91+
92+
return Napi::Number::New(env, mPort);
93+
}
94+
95+
Napi::Value IceUdpMuxListenerWrapper::address(const Napi::CallbackInfo &info)
96+
{
97+
Napi::Env env = info.Env();
98+
99+
if (!mAddress.has_value()) {
100+
return env.Undefined();
101+
}
102+
103+
return Napi::String::New(env, mAddress.value());
104+
}
105+
106+
void IceUdpMuxListenerWrapper::stop(const Napi::CallbackInfo &info)
107+
{
108+
PLOG_DEBUG << "IceUdpMuxListenerWrapper::stop() called";
109+
doCleanup();
110+
}
111+
112+
void IceUdpMuxListenerWrapper::onUnhandledStunRequest(const Napi::CallbackInfo &info)
113+
{
114+
PLOG_DEBUG << "IceUdpMuxListenerWrapper::onUnhandledStunRequest() called";
115+
Napi::Env env = info.Env();
116+
int length = info.Length();
117+
118+
if (!iceUdpMuxListenerPtr)
119+
{
120+
Napi::Error::New(env, "IceUdpMuxListenerWrapper::onUnhandledStunRequest() called on destroyed IceUdpMuxListener").ThrowAsJavaScriptException();
121+
return;
122+
}
123+
124+
if (length < 1 || !info[0].IsFunction())
125+
{
126+
Napi::TypeError::New(env, "Function expected").ThrowAsJavaScriptException();
127+
return;
128+
}
129+
130+
// Callback
131+
mOnUnhandledStunRequestCallback = std::make_unique<ThreadSafeCallback>(info[0].As<Napi::Function>());
132+
133+
iceUdpMuxListenerPtr->OnUnhandledStunRequest([&](rtc::IceUdpMuxRequest request)
134+
{
135+
PLOG_DEBUG << "IceUdpMuxListenerWrapper::onUnhandledStunRequest() IceUdpMuxCallback call(1)";
136+
137+
if (mOnUnhandledStunRequestCallback) {
138+
mOnUnhandledStunRequestCallback->call([request = std::move(request)](Napi::Env env, std::vector<napi_value> &args) {
139+
Napi::Object reqObj = Napi::Object::New(env);
140+
reqObj.Set("ufrag", request.remoteUfrag.c_str());
141+
reqObj.Set("host", request.remoteAddress.c_str());
142+
reqObj.Set("port", request.remotePort);
143+
144+
args = {reqObj};
145+
});
146+
}
147+
148+
PLOG_DEBUG << "IceUdpMuxListenerWrapper::onUnhandledStunRequest() IceUdpMuxCallback call(2)";
149+
});
150+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef ICE_UDP_MUX_LISTENER_WRAPPER_H
2+
#define ICE_UDP_MUX_LISTENER_WRAPPER_H
3+
4+
#include <napi.h>
5+
#include <rtc/rtc.hpp>
6+
#include <unordered_set>
7+
8+
#include "thread-safe-callback.h"
9+
10+
class IceUdpMuxListenerWrapper : public Napi::ObjectWrap<IceUdpMuxListenerWrapper>
11+
{
12+
public:
13+
static Napi::Object Init(Napi::Env env, Napi::Object exports);
14+
IceUdpMuxListenerWrapper(const Napi::CallbackInfo &info);
15+
~IceUdpMuxListenerWrapper();
16+
17+
// Functions
18+
void stop(const Napi::CallbackInfo &info);
19+
void onUnhandledStunRequest(const Napi::CallbackInfo &info);
20+
21+
// Stop listening on all ports
22+
static void StopAll();
23+
24+
// Properties
25+
Napi::Value port(const Napi::CallbackInfo &info);
26+
Napi::Value address(const Napi::CallbackInfo &info);
27+
Napi::Value unhandledStunRequestCallback(const Napi::CallbackInfo &info);
28+
29+
// Callback Ptrs
30+
std::unique_ptr<ThreadSafeCallback> mOnUnhandledStunRequestCallback = nullptr;
31+
32+
private:
33+
static Napi::FunctionReference constructor;
34+
static std::unordered_set<IceUdpMuxListenerWrapper *> instances;
35+
36+
void doCleanup();
37+
38+
std::optional<std::string> mAddress;
39+
uint16_t mPort;
40+
std::unique_ptr<rtc::IceUdpMuxListener> iceUdpMuxListenerPtr = nullptr;
41+
};
42+
43+
#endif // ICE_UDP_MUX_LISTENER_WRAPPER_H

src/cpp/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "rtc-wrapper.h"
33
#include "peer-connection-wrapper.h"
44
#include "data-channel-wrapper.h"
5+
#include "ice-udp-mux-listener-wrapper.h"
56

67
#if RTC_ENABLE_MEDIA == 1
78
#include "media-rtcpreceivingsession-wrapper.h"
@@ -27,6 +28,7 @@ Napi::Object InitAll(Napi::Env env, Napi::Object exports)
2728
#endif
2829

2930
DataChannelWrapper::Init(env, exports);
31+
IceUdpMuxListenerWrapper::Init(env, exports);
3032
PeerConnectionWrapper::Init(env, exports);
3133

3234
#if RTC_ENABLE_WEBSOCKET == 1

0 commit comments

Comments
 (0)