@@ -18,6 +18,7 @@ Napi::Object RtcWrapper::Init(Napi::Env env, Napi::Object exports)
18
18
exports.Set (" cleanup" , Napi::Function::New (env, &RtcWrapper::cleanup));
19
19
exports.Set (" preload" , Napi::Function::New (env, &RtcWrapper::preload));
20
20
exports.Set (" setSctpSettings" , Napi::Function::New (env, &RtcWrapper::setSctpSettings));
21
+ exports.Set (" onUnhandledStunRequest" , Napi::Function::New (env, &RtcWrapper::onUnhandledStunRequest));
21
22
22
23
return exports;
23
24
}
@@ -172,3 +173,61 @@ void RtcWrapper::setSctpSettings(const Napi::CallbackInfo &info)
172
173
173
174
rtc::SetSctpSettings (settings);
174
175
}
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
+ }
0 commit comments