node-datachannel 0.28.0 → 0.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/.clang-format +17 -0
  2. package/.prettierignore +3 -0
  3. package/API.md +30 -0
  4. package/BULDING.md +2 -1
  5. package/CMakeLists.txt +7 -2
  6. package/dist/cjs/lib/index.cjs +4 -1
  7. package/dist/cjs/lib/index.cjs.map +1 -1
  8. package/dist/cjs/polyfill/Events.cjs.map +1 -1
  9. package/dist/cjs/polyfill/RTCDataChannel.cjs +18 -5
  10. package/dist/cjs/polyfill/RTCDataChannel.cjs.map +1 -1
  11. package/dist/cjs/polyfill/RTCDtlsTransport.cjs.map +1 -1
  12. package/dist/cjs/polyfill/RTCPeerConnection.cjs +6 -1
  13. package/dist/cjs/polyfill/RTCPeerConnection.cjs.map +1 -1
  14. package/dist/cjs/polyfill/RTCSctpTransport.cjs.map +1 -1
  15. package/dist/esm/lib/index.mjs +4 -2
  16. package/dist/esm/lib/index.mjs.map +1 -1
  17. package/dist/esm/polyfill/Events.mjs.map +1 -1
  18. package/dist/esm/polyfill/RTCDataChannel.mjs +18 -5
  19. package/dist/esm/polyfill/RTCDataChannel.mjs.map +1 -1
  20. package/dist/esm/polyfill/RTCDtlsTransport.mjs.map +1 -1
  21. package/dist/esm/polyfill/RTCPeerConnection.mjs +6 -1
  22. package/dist/esm/polyfill/RTCPeerConnection.mjs.map +1 -1
  23. package/dist/esm/polyfill/RTCSctpTransport.mjs.map +1 -1
  24. package/dist/types/lib/index.d.ts +14 -3
  25. package/dist/types/lib/types.d.ts +24 -1
  26. package/dist/types/polyfill/RTCPeerConnection.d.ts +1 -2
  27. package/package.json +4 -2
  28. package/src/cpp/data-channel-wrapper.cpp +432 -411
  29. package/src/cpp/data-channel-wrapper.h +0 -2
  30. package/src/cpp/ice-udp-mux-listener-wrapper.cpp +157 -0
  31. package/src/cpp/ice-udp-mux-listener-wrapper.h +43 -0
  32. package/src/cpp/main.cpp +12 -10
  33. package/src/cpp/media-audio-wrapper.cpp +291 -294
  34. package/src/cpp/media-audio-wrapper.h +1 -3
  35. package/src/cpp/media-direction.cpp +32 -32
  36. package/src/cpp/media-direction.h +1 -1
  37. package/src/cpp/media-rtcpreceivingsession-wrapper.cpp +24 -28
  38. package/src/cpp/media-rtcpreceivingsession-wrapper.h +0 -5
  39. package/src/cpp/media-track-wrapper.cpp +350 -339
  40. package/src/cpp/media-track-wrapper.h +0 -3
  41. package/src/cpp/media-video-wrapper.cpp +312 -313
  42. package/src/cpp/media-video-wrapper.h +1 -3
  43. package/src/cpp/peer-connection-wrapper.cpp +1097 -984
  44. package/src/cpp/peer-connection-wrapper.h +1 -2
  45. package/src/cpp/rtc-wrapper.cpp +142 -138
  46. package/src/cpp/rtc-wrapper.h +8 -10
  47. package/src/cpp/thread-safe-callback.cpp +39 -50
  48. package/src/cpp/thread-safe-callback.h +26 -28
  49. package/src/cpp/web-socket-server-wrapper.cpp +205 -199
  50. package/src/cpp/web-socket-server-wrapper.h +0 -4
  51. package/src/cpp/web-socket-wrapper.cpp +625 -598
  52. package/src/cpp/web-socket-wrapper.h +0 -3
  53. package/src/lib/index.ts +14 -1
  54. package/src/lib/types.ts +26 -0
  55. package/src/polyfill/Events.ts +4 -1
  56. package/src/polyfill/RTCDataChannel.ts +24 -5
  57. package/src/polyfill/RTCDtlsTransport.ts +1 -1
  58. package/src/polyfill/RTCPeerConnection.ts +9 -3
  59. package/src/polyfill/RTCSctpTransport.ts +2 -2
@@ -1,9 +1,7 @@
1
1
  #ifndef DATA_CHANNEL_WRAPPER_H
2
2
  #define DATA_CHANNEL_WRAPPER_H
3
3
 
4
- #include <iostream>
5
4
  #include <string>
6
- #include <variant>
7
5
  #include <memory>
8
6
  #include <unordered_set>
9
7
 
@@ -0,0 +1,157 @@
1
+ #include "ice-udp-mux-listener-wrapper.h"
2
+
3
+ #include "plog/Log.h"
4
+
5
+ #include <cctype>
6
+
7
+ Napi::FunctionReference IceUdpMuxListenerWrapper::constructor = Napi::FunctionReference();
8
+ std::unordered_set<IceUdpMuxListenerWrapper *> IceUdpMuxListenerWrapper::instances;
9
+
10
+ void IceUdpMuxListenerWrapper::StopAll()
11
+ {
12
+ PLOG_DEBUG << "IceUdpMuxListenerWrapper StopAll() called";
13
+ auto copy(instances);
14
+ for (auto inst : copy)
15
+ inst->doCleanup();
16
+ }
17
+
18
+ Napi::Object IceUdpMuxListenerWrapper::Init(Napi::Env env, Napi::Object exports)
19
+ {
20
+ Napi::HandleScope scope(env);
21
+
22
+ Napi::Function func =
23
+ DefineClass(env, "IceUdpMuxListener",
24
+ {InstanceMethod("stop", &IceUdpMuxListenerWrapper::stop),
25
+ InstanceMethod("onUnhandledStunRequest", &IceUdpMuxListenerWrapper::onUnhandledStunRequest),
26
+ InstanceMethod("port", &IceUdpMuxListenerWrapper::port),
27
+ InstanceMethod("address", &IceUdpMuxListenerWrapper::address)});
28
+
29
+ // If this is not the first call, we don't want to reassign the constructor (hot-reload problem)
30
+ if (constructor.IsEmpty())
31
+ {
32
+ constructor = Napi::Persistent(func);
33
+ constructor.SuppressDestruct();
34
+ }
35
+
36
+ exports.Set("IceUdpMuxListener", func);
37
+ return exports;
38
+ }
39
+
40
+ IceUdpMuxListenerWrapper::IceUdpMuxListenerWrapper(const Napi::CallbackInfo &info)
41
+ : Napi::ObjectWrap<IceUdpMuxListenerWrapper>(info)
42
+ {
43
+ PLOG_DEBUG << "IceUdpMuxListenerWrapper Constructor called";
44
+ Napi::Env env = info.Env();
45
+ int length = info.Length();
46
+
47
+ // We expect (Number, String?) as param
48
+ if (length > 0 && info[0].IsNumber())
49
+ {
50
+ // Port
51
+ mPort = info[0].As<Napi::Number>().ToNumber().Uint32Value();
52
+ }
53
+ else
54
+ {
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
+ {
61
+ // Address
62
+ mAddress = info[1].As<Napi::String>().ToString();
63
+ }
64
+
65
+ iceUdpMuxListenerPtr = std::make_unique<rtc::IceUdpMuxListener>(mPort, mAddress);
66
+ instances.insert(this);
67
+ }
68
+
69
+ IceUdpMuxListenerWrapper::~IceUdpMuxListenerWrapper()
70
+ {
71
+ PLOG_DEBUG << "IceUdpMuxListenerWrapper Destructor called";
72
+ doCleanup();
73
+ }
74
+
75
+ void IceUdpMuxListenerWrapper::doCleanup()
76
+ {
77
+ PLOG_DEBUG << "IceUdpMuxListenerWrapper::doCleanup() called";
78
+
79
+ if (iceUdpMuxListenerPtr)
80
+ {
81
+ iceUdpMuxListenerPtr->stop();
82
+ iceUdpMuxListenerPtr.reset();
83
+ }
84
+
85
+ mOnUnhandledStunRequestCallback.reset();
86
+ instances.erase(this);
87
+ }
88
+
89
+ Napi::Value IceUdpMuxListenerWrapper::port(const Napi::CallbackInfo &info)
90
+ {
91
+ Napi::Env env = info.Env();
92
+
93
+ return Napi::Number::New(env, mPort);
94
+ }
95
+
96
+ Napi::Value IceUdpMuxListenerWrapper::address(const Napi::CallbackInfo &info)
97
+ {
98
+ Napi::Env env = info.Env();
99
+
100
+ if (!mAddress.has_value())
101
+ {
102
+ return env.Undefined();
103
+ }
104
+
105
+ return Napi::String::New(env, mAddress.value());
106
+ }
107
+
108
+ void IceUdpMuxListenerWrapper::stop(const Napi::CallbackInfo &info)
109
+ {
110
+ PLOG_DEBUG << "IceUdpMuxListenerWrapper::stop() called";
111
+ doCleanup();
112
+ }
113
+
114
+ void IceUdpMuxListenerWrapper::onUnhandledStunRequest(const Napi::CallbackInfo &info)
115
+ {
116
+ PLOG_DEBUG << "IceUdpMuxListenerWrapper::onUnhandledStunRequest() called";
117
+ Napi::Env env = info.Env();
118
+ int length = info.Length();
119
+
120
+ if (!iceUdpMuxListenerPtr)
121
+ {
122
+ Napi::Error::New(env, "IceUdpMuxListenerWrapper::onUnhandledStunRequest() called on destroyed IceUdpMuxListener")
123
+ .ThrowAsJavaScriptException();
124
+ return;
125
+ }
126
+
127
+ if (length < 1 || !info[0].IsFunction())
128
+ {
129
+ Napi::TypeError::New(env, "Function expected").ThrowAsJavaScriptException();
130
+ return;
131
+ }
132
+
133
+ // Callback
134
+ mOnUnhandledStunRequestCallback = std::make_unique<ThreadSafeCallback>(info[0].As<Napi::Function>());
135
+
136
+ iceUdpMuxListenerPtr->OnUnhandledStunRequest(
137
+ [&](rtc::IceUdpMuxRequest request)
138
+ {
139
+ PLOG_DEBUG << "IceUdpMuxListenerWrapper::onUnhandledStunRequest() IceUdpMuxCallback call(1)";
140
+
141
+ if (mOnUnhandledStunRequestCallback)
142
+ {
143
+ mOnUnhandledStunRequestCallback->call(
144
+ [request = std::move(request)](Napi::Env env, std::vector<napi_value> &args)
145
+ {
146
+ Napi::Object reqObj = Napi::Object::New(env);
147
+ reqObj.Set("ufrag", request.remoteUfrag.c_str());
148
+ reqObj.Set("host", request.remoteAddress.c_str());
149
+ reqObj.Set("port", request.remotePort);
150
+
151
+ args = {reqObj};
152
+ });
153
+ }
154
+
155
+ PLOG_DEBUG << "IceUdpMuxListenerWrapper::onUnhandledStunRequest() IceUdpMuxCallback call(2)";
156
+ });
157
+ }
@@ -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
package/src/cpp/main.cpp CHANGED
@@ -2,6 +2,7 @@
2
2
  #include "rtc-wrapper.h"
3
3
  #include "peer-connection-wrapper.h"
4
4
  #include "data-channel-wrapper.h"
5
+ #include "ice-udp-mux-listener-wrapper.h"
5
6
 
6
7
  #if RTC_ENABLE_MEDIA == 1
7
8
  #include "media-rtcpreceivingsession-wrapper.h"
@@ -17,24 +18,25 @@
17
18
 
18
19
  Napi::Object InitAll(Napi::Env env, Napi::Object exports)
19
20
  {
20
- RtcWrapper::Init(env, exports);
21
+ RtcWrapper::Init(env, exports);
21
22
 
22
23
  #if RTC_ENABLE_MEDIA == 1
23
- RtcpReceivingSessionWrapper::Init(env, exports);
24
- TrackWrapper::Init(env, exports);
25
- VideoWrapper::Init(env, exports);
26
- AudioWrapper::Init(env, exports);
24
+ RtcpReceivingSessionWrapper::Init(env, exports);
25
+ TrackWrapper::Init(env, exports);
26
+ VideoWrapper::Init(env, exports);
27
+ AudioWrapper::Init(env, exports);
27
28
  #endif
28
29
 
29
- DataChannelWrapper::Init(env, exports);
30
- PeerConnectionWrapper::Init(env, exports);
30
+ DataChannelWrapper::Init(env, exports);
31
+ IceUdpMuxListenerWrapper::Init(env, exports);
32
+ PeerConnectionWrapper::Init(env, exports);
31
33
 
32
34
  #if RTC_ENABLE_WEBSOCKET == 1
33
- WebSocketWrapper::Init(env, exports);
34
- WebSocketServerWrapper::Init(env, exports);
35
+ WebSocketWrapper::Init(env, exports);
36
+ WebSocketServerWrapper::Init(env, exports);
35
37
  #endif
36
38
 
37
- return exports;
39
+ return exports;
38
40
  }
39
41
 
40
42
  NODE_API_MODULE(nodeDataChannel, InitAll)