Skip to content

Commit bd58537

Browse files
committed
feat: support different callbacks for different ports
1 parent f3211b1 commit bd58537

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

src/cpp/rtc-wrapper.cpp

+59
Original file line numberDiff line numberDiff line change
@@ -18,6 +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));
2122

2223
return exports;
2324
}
@@ -172,3 +173,61 @@ void RtcWrapper::setSctpSettings(const Napi::CallbackInfo &info)
172173

173174
rtc::SetSctpSettings(settings);
174175
}
176+
177+
void RtcWrapper::onUnhandledStunRequest(const Napi::CallbackInfo &info)
178+
{
179+
PLOG_DEBUG << "onUnhandledStunRequest() called";
180+
Napi::Env env = info.Env();
181+
int length = info.Length();
182+
183+
if (length < 1 || !info[0].IsString())
184+
{
185+
Napi::TypeError::New(env, "host (String) expected").ThrowAsJavaScriptException();
186+
return;
187+
}
188+
Napi::String host = info[0].As<Napi::String>();
189+
190+
if (length < 2 || !info[1].IsNumber())
191+
{
192+
Napi::TypeError::New(env, "port (Number) expected").ThrowAsJavaScriptException();
193+
return;
194+
}
195+
Napi::Number port = info[1].As<Napi::Number>();
196+
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;
202+
}
203+
204+
if (length < 3 || !info[2].IsFunction())
205+
{
206+
Napi::TypeError::New(env, "cb (Function) expected").ThrowAsJavaScriptException();
207+
return;
208+
}
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()];
214+
215+
rtc::OnUnhandledStunRequest(host.ToString(), port.ToNumber(), [&](rtc::UnhandledStunRequest request, void *userPtr) {
216+
PLOG_DEBUG << "mOnUnhandledStunRequestCallback call(1)";
217+
218+
std::unique_ptr<ThreadSafeCallback> &callback = *(std::unique_ptr<ThreadSafeCallback> *)userPtr;
219+
220+
if (callback) {
221+
callback->call([request = std::move(request)](Napi::Env env, std::vector<napi_value> &args) {
222+
Napi::Object reqObj = Napi::Object::New(env);
223+
reqObj.Set("ufrag", request.remoteUfrag.c_str());
224+
reqObj.Set("host", request.remoteHost.c_str());
225+
reqObj.Set("port", request.remotePort);
226+
227+
args = {reqObj};
228+
});
229+
}
230+
231+
PLOG_DEBUG << "mOnUnhandledStunRequestCallback call(2)";
232+
}, ptr);
233+
}

src/cpp/rtc-wrapper.h

+2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ 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);
2122
private:
2223
static inline std::unique_ptr<ThreadSafeCallback> logCallback = nullptr;
24+
static inline std::map<u_int16_t, std::unique_ptr<ThreadSafeCallback>> unboundStunCallbacks;
2325
};
2426

2527
#endif // RTC_WRAPPER_H

src/lib/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +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); }
1112

1213
export interface Audio {
1314
addAudioCodec(payloadType: number, codec: string, profile?: string): void;
@@ -159,6 +160,7 @@ export default {
159160
cleanup,
160161
preload,
161162
setSctpSettings,
163+
onUnhandledStunRequest,
162164
RtcpReceivingSession,
163165
Track,
164166
Video,

0 commit comments

Comments
 (0)