Skip to content

Commit 122950d

Browse files
committed
chore: align with libdatachannel
1 parent bd58537 commit 122950d

File tree

6 files changed

+42
-52
lines changed

6 files changed

+42
-52
lines changed

src/cpp/rtc-wrapper.cpp

+30-27
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Napi::Object RtcWrapper::Init(Napi::Env env, Napi::Object exports)
1818
exports.Set("cleanup", Napi::Function::New(env, &RtcWrapper::cleanup));
1919
exports.Set("preload", Napi::Function::New(env, &RtcWrapper::preload));
2020
exports.Set("setSctpSettings", Napi::Function::New(env, &RtcWrapper::setSctpSettings));
21-
exports.Set("onUnhandledStunRequest", Napi::Function::New(env, &RtcWrapper::onUnhandledStunRequest));
21+
exports.Set("listenIceUdpMux", Napi::Function::New(env, &RtcWrapper::listenIceUdpMux));
2222

2323
return exports;
2424
}
@@ -174,48 +174,46 @@ void RtcWrapper::setSctpSettings(const Napi::CallbackInfo &info)
174174
rtc::SetSctpSettings(settings);
175175
}
176176

177-
void RtcWrapper::onUnhandledStunRequest(const Napi::CallbackInfo &info)
177+
void RtcWrapper::listenIceUdpMux(const Napi::CallbackInfo &info)
178178
{
179-
PLOG_DEBUG << "onUnhandledStunRequest() called";
179+
PLOG_DEBUG << "listenIceUdpMux() called";
180180
Napi::Env env = info.Env();
181181
int length = info.Length();
182182

183-
if (length < 1 || !info[0].IsString())
183+
if (length < 1 || !info[0].IsNumber())
184184
{
185-
Napi::TypeError::New(env, "host (String) expected").ThrowAsJavaScriptException();
185+
Napi::TypeError::New(env, "port (Number) expected").ThrowAsJavaScriptException();
186186
return;
187187
}
188-
Napi::String host = info[0].As<Napi::String>();
188+
int port = info[0].As<Napi::Number>().ToNumber();
189189

190-
if (length < 2 || !info[1].IsNumber())
190+
Napi::Function listener;
191+
if (length > 1 && info[1].IsFunction())
191192
{
192-
Napi::TypeError::New(env, "port (Number) expected").ThrowAsJavaScriptException();
193-
return;
193+
listener = info[1].As<Napi::Function>();
194194
}
195-
Napi::Number port = info[1].As<Napi::Number>();
196195

197-
// unbind listener if cb is null, undefined, or was omitted
198-
if (length == 2 || (info[2].IsNull() || info[2].IsUndefined())) {
199-
unboundStunCallbacks.erase(port.ToNumber().Uint32Value());
200-
rtc::OnUnhandledStunRequest(host.ToString(), port.ToNumber());
201-
return;
196+
std::string host;
197+
if (length > 2 && info[2].IsString())
198+
{
199+
host = info[2].As<Napi::String>().ToString();
202200
}
203201

204-
if (length < 3 || !info[2].IsFunction())
202+
// unbind listener if cb is null, undefined, or was omitted
203+
if (!listener)
205204
{
206-
Napi::TypeError::New(env, "cb (Function) expected").ThrowAsJavaScriptException();
205+
std::lock_guard<std::mutex> guard(iceUdpMuxListenersMutex);
206+
iceUdpMuxListeners.erase(port);
207+
rtc::ListenIceUdpMux(port, nullptr, host);
207208
return;
208209
}
209-
Napi::Function cb = info[2].As<Napi::Function>();
210-
211-
std::unique_ptr<ThreadSafeCallback> callback = std::make_unique<ThreadSafeCallback>(cb);
212-
unboundStunCallbacks[port.ToNumber().Uint32Value()] = std::move(callback);
213-
void * ptr = &unboundStunCallbacks[port.ToNumber().Uint32Value()];
214210

215-
rtc::OnUnhandledStunRequest(host.ToString(), port.ToNumber(), [&](rtc::UnhandledStunRequest request, void *userPtr) {
216-
PLOG_DEBUG << "mOnUnhandledStunRequestCallback call(1)";
211+
auto uniqueCallback = std::make_unique<ThreadSafeCallback>(listener);
212+
auto callback = std::shared_ptr<ThreadSafeCallback>(std::move(uniqueCallback));
217213

218-
std::unique_ptr<ThreadSafeCallback> &callback = *(std::unique_ptr<ThreadSafeCallback> *)userPtr;
214+
rtc::IceUdpMuxCallback iceUdpMuxCallback = [callback](rtc::IceUdpMuxRequest request)
215+
{
216+
PLOG_DEBUG << "listenIceUdpMux IceUdpMuxCallback call(1)";
219217

220218
if (callback) {
221219
callback->call([request = std::move(request)](Napi::Env env, std::vector<napi_value> &args) {
@@ -228,6 +226,11 @@ void RtcWrapper::onUnhandledStunRequest(const Napi::CallbackInfo &info)
228226
});
229227
}
230228

231-
PLOG_DEBUG << "mOnUnhandledStunRequestCallback call(2)";
232-
}, ptr);
229+
PLOG_DEBUG << "listenIceUdpMux IceUdpMuxCallback call(2)";
230+
};
231+
232+
std::lock_guard<std::mutex> guard(iceUdpMuxListenersMutex);
233+
iceUdpMuxListeners[port] = std::move(iceUdpMuxCallback);
234+
235+
rtc::ListenIceUdpMux(port, &iceUdpMuxListeners[port], host);
233236
}

src/cpp/rtc-wrapper.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ class RtcWrapper
1818
static void initLogger(const Napi::CallbackInfo &info);
1919
static void cleanup(const Napi::CallbackInfo &info);
2020
static void setSctpSettings(const Napi::CallbackInfo &info);
21-
static void onUnhandledStunRequest(const Napi::CallbackInfo &info);
21+
static void listenIceUdpMux(const Napi::CallbackInfo &info);
2222
private:
2323
static inline std::unique_ptr<ThreadSafeCallback> logCallback = nullptr;
24-
static inline std::map<u_int16_t, std::unique_ptr<ThreadSafeCallback>> unboundStunCallbacks;
24+
static inline std::mutex iceUdpMuxListenersMutex;
25+
static inline std::map<u_int16_t, rtc::IceUdpMuxCallback> iceUdpMuxListeners;
2526
};
2627

2728
#endif // RTC_WRAPPER_H

src/lib/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function preload(): void { nodeDataChannel.preload(); }
88
export function initLogger(level: LogLevel): void { nodeDataChannel.initLogger(level); }
99
export function cleanup(): void { nodeDataChannel.cleanup(); }
1010
export function setSctpSettings(settings: SctpSettings): void { nodeDataChannel.setSctpSettings(settings); }
11-
export function onUnhandledStunRequest(host: string, port: number, cb?: (req: { ufrag: string, host: string, port: number }) => void): void { nodeDataChannel.onUnhandledStunRequest(host, port, cb); }
11+
export function listenIceUdpMux(port: number, cb?: (req: { ufrag: string, host: string, port: number }) => void, host?: string): void { nodeDataChannel.listenIceUdpMux(port, cb, host); }
1212

1313
export interface Audio {
1414
addAudioCodec(payloadType: number, codec: string, profile?: string): void;
@@ -160,7 +160,7 @@ export default {
160160
cleanup,
161161
preload,
162162
setSctpSettings,
163-
onUnhandledStunRequest,
163+
listenIceUdpMux,
164164
RtcpReceivingSession,
165165
Track,
166166
Video,

src/lib/node-datachannel.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
// @ts-expect-error no types
12
import nodeDataChannel = require('../../build/Release/node_datachannel.node');
23
export default nodeDataChannel;

src/lib/types.ts

+3-19
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ export interface ProxyServer {
4747
password?: string;
4848
}
4949

50-
export const enum RelayType {
51-
TurnUdp = 'TurnUdp',
52-
TurnTcp = 'TurnTcp',
53-
TurnTls = 'TurnTls',
54-
}
50+
export type RelayType = 'TurnUdp' | 'TurnTcp' | 'TurnTls'
5551

5652
export interface IceServer {
5753
hostname: string;
@@ -84,13 +80,7 @@ export interface RtcConfig {
8480
}
8581

8682
// Lowercase to match the description type string from libdatachannel
87-
export enum DescriptionType {
88-
Unspec = 'unspec',
89-
Offer = 'offer',
90-
Answer = 'answer',
91-
Pranswer = 'pranswer',
92-
Rollback = 'rollback',
93-
}
83+
export type DescriptionType = 'unspec' | 'offer' | 'answer' | 'pranswer' | 'rollback'
9484

9585
export type RTCSdpType = 'answer' | 'offer' | 'pranswer' | 'rollback';
9686

@@ -137,10 +127,4 @@ export interface SelectedCandidateInfo {
137127
}
138128

139129
// Must be same as rtc enum class Direction
140-
export const enum Direction {
141-
SendOnly = 'SendOnly',
142-
RecvOnly = 'RecvOnly',
143-
SendRecv = 'SendRecv',
144-
Inactive = 'Inactive',
145-
Unknown = 'Unknown',
146-
}
130+
export type Direction = 'SendOnly' | 'RecvOnly' | 'SendRecv' | 'Inactive' | 'Unknown'

src/polyfill/RTCPeerConnection.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ export default class RTCPeerConnection extends EventTarget implements globalThis
121121

122122
try {
123123
const peerIdentity = (config as any)?.peerIdentity ?? `peer-${getRandomString(7)}`;
124-
this.#peerConnection = new PeerConnection(peerIdentity,
124+
// @ts-expect-error fixme
125+
this.#peerConnection = config.peerConnection ?? new PeerConnection(peerIdentity,
125126
{
126127
...config,
127128
iceServers:
@@ -282,7 +283,7 @@ export default class RTCPeerConnection extends EventTarget implements globalThis
282283
return this.#peerConnection.signalingState();
283284
}
284285

285-
async addIceCandidate(candidate?: globalThis.RTCIceCandidateInit | RTCIceCandidate): Promise<void> {
286+
async addIceCandidate(candidate?: globalThis.RTCIceCandidateInit | null): Promise<void> {
286287
if (!candidate || !candidate.candidate) {
287288
return;
288289
}

0 commit comments

Comments
 (0)