@lng2004/node-datachannel 0.31.0-20251228

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 (171) hide show
  1. package/.clang-format +17 -0
  2. package/.editorconfig +12 -0
  3. package/.eslintignore +8 -0
  4. package/.eslintrc.json +27 -0
  5. package/.gitmodules +3 -0
  6. package/.prettierignore +7 -0
  7. package/.prettierrc +13 -0
  8. package/API.md +247 -0
  9. package/BULDING.md +33 -0
  10. package/CMakeLists.txt +134 -0
  11. package/LICENSE +373 -0
  12. package/README.md +130 -0
  13. package/dist/cjs/index.cjs +11 -0
  14. package/dist/cjs/index.cjs.map +1 -0
  15. package/dist/cjs/lib/datachannel-stream.cjs +101 -0
  16. package/dist/cjs/lib/datachannel-stream.cjs.map +1 -0
  17. package/dist/cjs/lib/index.cjs +88 -0
  18. package/dist/cjs/lib/index.cjs.map +1 -0
  19. package/dist/cjs/lib/node-datachannel.cjs +8 -0
  20. package/dist/cjs/lib/node-datachannel.cjs.map +1 -0
  21. package/dist/cjs/lib/websocket-server.cjs +45 -0
  22. package/dist/cjs/lib/websocket-server.cjs.map +1 -0
  23. package/dist/cjs/lib/websocket.cjs +8 -0
  24. package/dist/cjs/lib/websocket.cjs.map +1 -0
  25. package/dist/cjs/polyfill/Events.cjs +86 -0
  26. package/dist/cjs/polyfill/Events.cjs.map +1 -0
  27. package/dist/cjs/polyfill/Exception.cjs +34 -0
  28. package/dist/cjs/polyfill/Exception.cjs.map +1 -0
  29. package/dist/cjs/polyfill/RTCCertificate.cjs +34 -0
  30. package/dist/cjs/polyfill/RTCCertificate.cjs.map +1 -0
  31. package/dist/cjs/polyfill/RTCDataChannel.cjs +214 -0
  32. package/dist/cjs/polyfill/RTCDataChannel.cjs.map +1 -0
  33. package/dist/cjs/polyfill/RTCDtlsTransport.cjs +53 -0
  34. package/dist/cjs/polyfill/RTCDtlsTransport.cjs.map +1 -0
  35. package/dist/cjs/polyfill/RTCError.cjs +83 -0
  36. package/dist/cjs/polyfill/RTCError.cjs.map +1 -0
  37. package/dist/cjs/polyfill/RTCIceCandidate.cjs +132 -0
  38. package/dist/cjs/polyfill/RTCIceCandidate.cjs.map +1 -0
  39. package/dist/cjs/polyfill/RTCIceTransport.cjs +94 -0
  40. package/dist/cjs/polyfill/RTCIceTransport.cjs.map +1 -0
  41. package/dist/cjs/polyfill/RTCPeerConnection.cjs +434 -0
  42. package/dist/cjs/polyfill/RTCPeerConnection.cjs.map +1 -0
  43. package/dist/cjs/polyfill/RTCSctpTransport.cjs +59 -0
  44. package/dist/cjs/polyfill/RTCSctpTransport.cjs.map +1 -0
  45. package/dist/cjs/polyfill/RTCSessionDescription.cjs +45 -0
  46. package/dist/cjs/polyfill/RTCSessionDescription.cjs.map +1 -0
  47. package/dist/cjs/polyfill/index.cjs +42 -0
  48. package/dist/cjs/polyfill/index.cjs.map +1 -0
  49. package/dist/esm/index.mjs +8 -0
  50. package/dist/esm/index.mjs.map +1 -0
  51. package/dist/esm/lib/datachannel-stream.mjs +78 -0
  52. package/dist/esm/lib/datachannel-stream.mjs.map +1 -0
  53. package/dist/esm/lib/index.mjs +62 -0
  54. package/dist/esm/lib/index.mjs.map +1 -0
  55. package/dist/esm/lib/node-datachannel.mjs +12 -0
  56. package/dist/esm/lib/node-datachannel.mjs.map +1 -0
  57. package/dist/esm/lib/websocket-server.mjs +43 -0
  58. package/dist/esm/lib/websocket-server.mjs.map +1 -0
  59. package/dist/esm/lib/websocket.mjs +6 -0
  60. package/dist/esm/lib/websocket.mjs.map +1 -0
  61. package/dist/esm/polyfill/Events.mjs +82 -0
  62. package/dist/esm/polyfill/Events.mjs.map +1 -0
  63. package/dist/esm/polyfill/Exception.mjs +28 -0
  64. package/dist/esm/polyfill/Exception.mjs.map +1 -0
  65. package/dist/esm/polyfill/RTCCertificate.mjs +30 -0
  66. package/dist/esm/polyfill/RTCCertificate.mjs.map +1 -0
  67. package/dist/esm/polyfill/RTCDataChannel.mjs +210 -0
  68. package/dist/esm/polyfill/RTCDataChannel.mjs.map +1 -0
  69. package/dist/esm/polyfill/RTCDtlsTransport.mjs +49 -0
  70. package/dist/esm/polyfill/RTCDtlsTransport.mjs.map +1 -0
  71. package/dist/esm/polyfill/RTCError.mjs +79 -0
  72. package/dist/esm/polyfill/RTCError.mjs.map +1 -0
  73. package/dist/esm/polyfill/RTCIceCandidate.mjs +128 -0
  74. package/dist/esm/polyfill/RTCIceCandidate.mjs.map +1 -0
  75. package/dist/esm/polyfill/RTCIceTransport.mjs +89 -0
  76. package/dist/esm/polyfill/RTCIceTransport.mjs.map +1 -0
  77. package/dist/esm/polyfill/RTCPeerConnection.mjs +430 -0
  78. package/dist/esm/polyfill/RTCPeerConnection.mjs.map +1 -0
  79. package/dist/esm/polyfill/RTCSctpTransport.mjs +55 -0
  80. package/dist/esm/polyfill/RTCSctpTransport.mjs.map +1 -0
  81. package/dist/esm/polyfill/RTCSessionDescription.mjs +41 -0
  82. package/dist/esm/polyfill/RTCSessionDescription.mjs.map +1 -0
  83. package/dist/esm/polyfill/index.mjs +27 -0
  84. package/dist/esm/polyfill/index.mjs.map +1 -0
  85. package/dist/types/lib/datachannel-stream.d.ts +24 -0
  86. package/dist/types/lib/index.d.ts +235 -0
  87. package/dist/types/lib/types.d.ts +118 -0
  88. package/dist/types/lib/websocket-server.d.ts +13 -0
  89. package/dist/types/lib/websocket.d.ts +25 -0
  90. package/dist/types/polyfill/Events.d.ts +15 -0
  91. package/dist/types/polyfill/RTCCertificate.d.ts +9 -0
  92. package/dist/types/polyfill/RTCDataChannel.d.ts +29 -0
  93. package/dist/types/polyfill/RTCDtlsTransport.d.ts +15 -0
  94. package/dist/types/polyfill/RTCError.d.ts +17 -0
  95. package/dist/types/polyfill/RTCIceCandidate.d.ts +21 -0
  96. package/dist/types/polyfill/RTCIceTransport.d.ts +30 -0
  97. package/dist/types/polyfill/RTCPeerConnection.d.ts +64 -0
  98. package/dist/types/polyfill/RTCSctpTransport.d.ts +15 -0
  99. package/dist/types/polyfill/RTCSessionDescription.d.ts +10 -0
  100. package/dist/types/polyfill/index.d.ts +26 -0
  101. package/jest.config.ts +14 -0
  102. package/package.json +121 -0
  103. package/rollup.config.mjs +72 -0
  104. package/src/cpp/data-channel-wrapper.cpp +530 -0
  105. package/src/cpp/data-channel-wrapper.h +63 -0
  106. package/src/cpp/ice-udp-mux-listener-wrapper.cpp +157 -0
  107. package/src/cpp/ice-udp-mux-listener-wrapper.h +43 -0
  108. package/src/cpp/main.cpp +58 -0
  109. package/src/cpp/media-audio-wrapper.cpp +457 -0
  110. package/src/cpp/media-audio-wrapper.h +52 -0
  111. package/src/cpp/media-av1packetization.cpp +24 -0
  112. package/src/cpp/media-av1packetization.h +11 -0
  113. package/src/cpp/media-av1rtppacketizer-wrapper.cpp +126 -0
  114. package/src/cpp/media-av1rtppacketizer-wrapper.h +29 -0
  115. package/src/cpp/media-direction.cpp +43 -0
  116. package/src/cpp/media-direction.h +10 -0
  117. package/src/cpp/media-h264rtppacketizer-wrapper.cpp +126 -0
  118. package/src/cpp/media-h264rtppacketizer-wrapper.h +30 -0
  119. package/src/cpp/media-h265rtppacketizer-wrapper.cpp +126 -0
  120. package/src/cpp/media-h265rtppacketizer-wrapper.h +30 -0
  121. package/src/cpp/media-h26xseparator.cpp +32 -0
  122. package/src/cpp/media-h26xseparator.h +11 -0
  123. package/src/cpp/media-mediahandler-helper.cpp +31 -0
  124. package/src/cpp/media-mediahandler-helper.h +11 -0
  125. package/src/cpp/media-pacinghandler-wrapper.cpp +79 -0
  126. package/src/cpp/media-pacinghandler-wrapper.h +28 -0
  127. package/src/cpp/media-rtcpnackresponder-wrapper.cpp +68 -0
  128. package/src/cpp/media-rtcpnackresponder-wrapper.h +28 -0
  129. package/src/cpp/media-rtcpreceivingsession-wrapper.cpp +57 -0
  130. package/src/cpp/media-rtcpreceivingsession-wrapper.h +28 -0
  131. package/src/cpp/media-rtcpsrreporter-wrapper.cpp +93 -0
  132. package/src/cpp/media-rtcpsrreporter-wrapper.h +30 -0
  133. package/src/cpp/media-rtppacketizationconfig-wrapper.cpp +167 -0
  134. package/src/cpp/media-rtppacketizationconfig-wrapper.h +35 -0
  135. package/src/cpp/media-rtppacketizer-wrapper.cpp +95 -0
  136. package/src/cpp/media-rtppacketizer-wrapper.h +30 -0
  137. package/src/cpp/media-track-wrapper.cpp +458 -0
  138. package/src/cpp/media-track-wrapper.h +61 -0
  139. package/src/cpp/media-video-wrapper.cpp +526 -0
  140. package/src/cpp/media-video-wrapper.h +56 -0
  141. package/src/cpp/peer-connection-wrapper.cpp +1298 -0
  142. package/src/cpp/peer-connection-wrapper.h +89 -0
  143. package/src/cpp/rtc-wrapper.cpp +205 -0
  144. package/src/cpp/rtc-wrapper.h +24 -0
  145. package/src/cpp/thread-safe-callback.cpp +57 -0
  146. package/src/cpp/thread-safe-callback.h +47 -0
  147. package/src/cpp/web-socket-server-wrapper.cpp +275 -0
  148. package/src/cpp/web-socket-server-wrapper.h +41 -0
  149. package/src/cpp/web-socket-wrapper.cpp +796 -0
  150. package/src/cpp/web-socket-wrapper.h +63 -0
  151. package/src/index.ts +9 -0
  152. package/src/lib/datachannel-stream.ts +100 -0
  153. package/src/lib/index.ts +283 -0
  154. package/src/lib/node-datachannel.ts +3 -0
  155. package/src/lib/types.ts +168 -0
  156. package/src/lib/websocket-server.ts +37 -0
  157. package/src/lib/websocket.ts +26 -0
  158. package/src/polyfill/Events.ts +82 -0
  159. package/src/polyfill/Exception.ts +37 -0
  160. package/src/polyfill/README.md +41 -0
  161. package/src/polyfill/RTCCertificate.ts +21 -0
  162. package/src/polyfill/RTCDataChannel.ts +225 -0
  163. package/src/polyfill/RTCDtlsTransport.ts +46 -0
  164. package/src/polyfill/RTCError.ts +78 -0
  165. package/src/polyfill/RTCIceCandidate.ts +128 -0
  166. package/src/polyfill/RTCIceTransport.ts +90 -0
  167. package/src/polyfill/RTCPeerConnection.ts +527 -0
  168. package/src/polyfill/RTCSctpTransport.ts +51 -0
  169. package/src/polyfill/RTCSessionDescription.ts +41 -0
  170. package/src/polyfill/index.ts +38 -0
  171. package/tsconfig.json +21 -0
@@ -0,0 +1,526 @@
1
+ #include "media-video-wrapper.h"
2
+ #include "media-direction.h"
3
+
4
+ Napi::FunctionReference VideoWrapper::constructor = Napi::FunctionReference();
5
+ std::unordered_set<VideoWrapper *> VideoWrapper::instances;
6
+
7
+ Napi::Object VideoWrapper::Init(Napi::Env env, Napi::Object exports)
8
+ {
9
+ Napi::HandleScope scope(env);
10
+
11
+ Napi::Function func =
12
+ Napi::ObjectWrap<VideoWrapper>::DefineClass(env, "Video",
13
+ {
14
+ InstanceValue("media-type-video", Napi::Boolean::New(env, true)),
15
+ InstanceMethod("addVideoCodec", &VideoWrapper::addVideoCodec),
16
+ InstanceMethod("addH264Codec", &VideoWrapper::addH264Codec),
17
+ InstanceMethod("addH265Codec", &VideoWrapper::addH265Codec),
18
+ InstanceMethod("addVP8Codec", &VideoWrapper::addVP8Codec),
19
+ InstanceMethod("addVP9Codec", &VideoWrapper::addVP9Codec),
20
+ InstanceMethod("addAV1Codec", &VideoWrapper::addAV1Codec),
21
+ InstanceMethod("direction", &VideoWrapper::direction),
22
+ InstanceMethod("generateSdp", &VideoWrapper::generateSdp),
23
+ InstanceMethod("mid", &VideoWrapper::mid),
24
+ InstanceMethod("setDirection", &VideoWrapper::setDirection),
25
+ InstanceMethod("description", &VideoWrapper::description),
26
+ InstanceMethod("removeFormat", &VideoWrapper::removeFormat),
27
+ InstanceMethod("addSSRC", &VideoWrapper::addSSRC),
28
+ InstanceMethod("removeSSRC", &VideoWrapper::removeSSRC),
29
+ InstanceMethod("replaceSSRC", &VideoWrapper::replaceSSRC),
30
+ InstanceMethod("hasSSRC", &VideoWrapper::hasSSRC),
31
+ InstanceMethod("getSSRCs", &VideoWrapper::getSSRCs),
32
+ InstanceMethod("getCNameForSsrc", &VideoWrapper::getCNameForSsrc),
33
+ InstanceMethod("setBitrate", &VideoWrapper::setBitrate),
34
+ InstanceMethod("getBitrate", &VideoWrapper::getBitrate),
35
+ InstanceMethod("hasPayloadType", &VideoWrapper::hasPayloadType),
36
+ InstanceMethod("addRTXCodec", &VideoWrapper::addRTXCodec),
37
+ InstanceMethod("addRTPMap", &VideoWrapper::addRTPMap),
38
+ InstanceMethod("parseSdpLine", &VideoWrapper::parseSdpLine),
39
+ });
40
+
41
+ // If this is not the first call, we don't want to reassign the constructor (hot-reload problem)
42
+ if (constructor.IsEmpty())
43
+ {
44
+ constructor = Napi::Persistent(func);
45
+ constructor.SuppressDestruct();
46
+ }
47
+
48
+ exports.Set("Video", func);
49
+ return exports;
50
+ }
51
+
52
+ VideoWrapper::VideoWrapper(const Napi::CallbackInfo &info) : Napi::ObjectWrap<VideoWrapper>(info)
53
+ {
54
+ Napi::Env env = info.Env();
55
+ int length = info.Length();
56
+
57
+ std::string mid = "video";
58
+ rtc::Description::Direction dir = rtc::Description::Direction::Unknown;
59
+
60
+ // optional
61
+ if (length > 0)
62
+ {
63
+ if (!info[0].IsString())
64
+ {
65
+ Napi::TypeError::New(env, "mid (String) expected").ThrowAsJavaScriptException();
66
+ return;
67
+ }
68
+ mid = info[0].As<Napi::String>().ToString();
69
+ }
70
+
71
+ // ootional
72
+ if (length > 1)
73
+ {
74
+ if (!info[1].IsString())
75
+ {
76
+ Napi::TypeError::New(env, "direction (String) expected").ThrowAsJavaScriptException();
77
+ return;
78
+ }
79
+
80
+ std::string dirAsStr = info[1].As<Napi::String>().ToString();
81
+ dir = strToDirection(dirAsStr);
82
+ }
83
+
84
+ mVideoPtr = std::make_unique<rtc::Description::Video>(mid, dir);
85
+
86
+ instances.insert(this);
87
+ }
88
+
89
+ VideoWrapper::~VideoWrapper()
90
+ {
91
+ mVideoPtr.reset();
92
+ instances.erase(this);
93
+ }
94
+
95
+ rtc::Description::Video VideoWrapper::getVideoInstance() { return *(mVideoPtr.get()); }
96
+
97
+ void VideoWrapper::addVideoCodec(const Napi::CallbackInfo &info)
98
+ {
99
+ Napi::Env env = info.Env();
100
+ int length = info.Length();
101
+
102
+ if (length < 2 || !info[0].IsNumber() || !info[1].IsString())
103
+ {
104
+ Napi::TypeError::New(env, "We expect (Number, String, String[optional]) as param").ThrowAsJavaScriptException();
105
+ return;
106
+ }
107
+
108
+ int payloadType = info[0].As<Napi::Number>().ToNumber();
109
+ std::string codec = info[1].As<Napi::String>().ToString();
110
+ std::optional<std::string> profile = std::nullopt;
111
+
112
+ if (length > 2)
113
+ {
114
+ if (!info[2].IsString())
115
+ {
116
+ Napi::TypeError::New(env, "profile (String) expected").ThrowAsJavaScriptException();
117
+ return;
118
+ }
119
+ profile = info[2].As<Napi::String>().ToString();
120
+ }
121
+
122
+ mVideoPtr->addVideoCodec(payloadType, codec, profile);
123
+ }
124
+
125
+ void VideoWrapper::addH264Codec(const Napi::CallbackInfo &info)
126
+ {
127
+ Napi::Env env = info.Env();
128
+ int length = info.Length();
129
+
130
+ if (length < 1 || !info[0].IsNumber())
131
+ {
132
+ Napi::TypeError::New(env, "We expect (Number) as param").ThrowAsJavaScriptException();
133
+ return;
134
+ }
135
+
136
+ int payloadType = info[0].As<Napi::Number>().ToNumber();
137
+ std::string profile = rtc::DEFAULT_H264_VIDEO_PROFILE;
138
+
139
+ if (length > 1)
140
+ {
141
+ if (!info[1].IsString())
142
+ {
143
+ Napi::TypeError::New(env, "profile (String) expected").ThrowAsJavaScriptException();
144
+ return;
145
+ }
146
+ profile = info[1].As<Napi::String>().ToString();
147
+ }
148
+
149
+ mVideoPtr->addH264Codec(payloadType, profile);
150
+ }
151
+
152
+ void VideoWrapper::addH265Codec(const Napi::CallbackInfo &info)
153
+ {
154
+ Napi::Env env = info.Env();
155
+ int length = info.Length();
156
+
157
+ if (length < 1 || !info[0].IsNumber())
158
+ {
159
+ Napi::TypeError::New(env, "We expect (Number) as param").ThrowAsJavaScriptException();
160
+ return;
161
+ }
162
+
163
+ int payloadType = info[0].As<Napi::Number>().ToNumber();
164
+
165
+ mVideoPtr->addH265Codec(payloadType);
166
+ }
167
+
168
+ void VideoWrapper::addVP8Codec(const Napi::CallbackInfo &info)
169
+ {
170
+ Napi::Env env = info.Env();
171
+ int length = info.Length();
172
+
173
+ if (length < 1 || !info[0].IsNumber())
174
+ {
175
+ Napi::TypeError::New(env, "We expect (Number) as param").ThrowAsJavaScriptException();
176
+ return;
177
+ }
178
+
179
+ int payloadType = info[0].As<Napi::Number>().ToNumber();
180
+
181
+ mVideoPtr->addVP8Codec(payloadType);
182
+ }
183
+
184
+ void VideoWrapper::addVP9Codec(const Napi::CallbackInfo &info)
185
+ {
186
+ Napi::Env env = info.Env();
187
+ int length = info.Length();
188
+
189
+ if (length < 1 || !info[0].IsNumber())
190
+ {
191
+ Napi::TypeError::New(env, "We expect (Number) as param").ThrowAsJavaScriptException();
192
+ return;
193
+ }
194
+
195
+ int payloadType = info[0].As<Napi::Number>().ToNumber();
196
+
197
+ mVideoPtr->addVP9Codec(payloadType);
198
+ }
199
+
200
+ void VideoWrapper::addAV1Codec(const Napi::CallbackInfo &info)
201
+ {
202
+ Napi::Env env = info.Env();
203
+ int length = info.Length();
204
+
205
+ if (length < 1 || !info[0].IsNumber())
206
+ {
207
+ Napi::TypeError::New(env, "We expect (Number) as param").ThrowAsJavaScriptException();
208
+ return;
209
+ }
210
+
211
+ int payloadType = info[0].As<Napi::Number>().ToNumber();
212
+
213
+ mVideoPtr->addAV1Codec(payloadType);
214
+ }
215
+
216
+
217
+ Napi::Value VideoWrapper::direction(const Napi::CallbackInfo &info)
218
+ {
219
+ Napi::Env env = info.Env();
220
+ return Napi::String::New(env, directionToStr(mVideoPtr->direction()));
221
+ }
222
+
223
+ Napi::Value VideoWrapper::generateSdp(const Napi::CallbackInfo &info)
224
+ {
225
+ Napi::Env env = info.Env();
226
+ int length = info.Length();
227
+
228
+ if (length < 3 || !info[0].IsString() || !info[1].IsString() || !info[2].IsNumber())
229
+ {
230
+ Napi::TypeError::New(env, "We expect (String, String, Number) as param").ThrowAsJavaScriptException();
231
+ return Napi::String::New(env, "");
232
+ }
233
+
234
+ std::string eol = info[0].As<Napi::String>().ToString();
235
+ std::string addr = info[1].As<Napi::String>().ToString();
236
+ uint16_t port = info[2].As<Napi::Number>().Uint32Value();
237
+
238
+ return Napi::String::New(env, mVideoPtr->generateSdp(eol, addr, port));
239
+ }
240
+
241
+ Napi::Value VideoWrapper::mid(const Napi::CallbackInfo &info)
242
+ {
243
+ Napi::Env env = info.Env();
244
+ return Napi::String::New(env, mVideoPtr->mid());
245
+ }
246
+
247
+ void VideoWrapper::setDirection(const Napi::CallbackInfo &info)
248
+ {
249
+ Napi::Env env = info.Env();
250
+ int length = info.Length();
251
+
252
+ if (length < 1 || !info[0].IsString())
253
+ {
254
+ Napi::TypeError::New(env, "We expect (String) as param").ThrowAsJavaScriptException();
255
+ return;
256
+ }
257
+
258
+ std::string dirAsStr = info[0].As<Napi::String>().ToString();
259
+ rtc::Description::Direction dir = strToDirection(dirAsStr);
260
+ mVideoPtr->setDirection(dir);
261
+ }
262
+
263
+ Napi::Value VideoWrapper::description(const Napi::CallbackInfo &info)
264
+ {
265
+ Napi::Env env = info.Env();
266
+ return Napi::String::New(env, mVideoPtr->description());
267
+ }
268
+
269
+ void VideoWrapper::removeFormat(const Napi::CallbackInfo &info)
270
+ {
271
+ Napi::Env env = info.Env();
272
+ int length = info.Length();
273
+
274
+ if (length < 1 || !info[0].IsString())
275
+ {
276
+ Napi::TypeError::New(env, "We expect (String) as param").ThrowAsJavaScriptException();
277
+ return;
278
+ }
279
+
280
+ std::string fmt = info[0].As<Napi::String>().ToString();
281
+
282
+ mVideoPtr->removeFormat(fmt);
283
+ }
284
+
285
+ void VideoWrapper::addSSRC(const Napi::CallbackInfo &info)
286
+ {
287
+ Napi::Env env = info.Env();
288
+ int length = info.Length();
289
+
290
+ if (length < 1 || !info[0].IsNumber())
291
+ {
292
+ Napi::TypeError::New(env, "We expect (Number, String[optional], String[optional], String[optional]) as param")
293
+ .ThrowAsJavaScriptException();
294
+ return;
295
+ }
296
+
297
+ uint32_t ssrc = static_cast<uint32_t>(info[0].As<Napi::Number>().ToNumber());
298
+ std::optional<std::string> name;
299
+ std::optional<std::string> msid = std::nullopt;
300
+ std::optional<std::string> trackID = std::nullopt;
301
+
302
+ if (length > 1)
303
+ {
304
+ if (!info[1].IsString())
305
+ {
306
+ Napi::TypeError::New(env, "name as String expected").ThrowAsJavaScriptException();
307
+ return;
308
+ }
309
+ name = info[1].As<Napi::String>().ToString();
310
+ }
311
+
312
+ if (length > 2)
313
+ {
314
+ if (!info[2].IsString())
315
+ {
316
+ Napi::TypeError::New(env, "msid as String expected").ThrowAsJavaScriptException();
317
+ return;
318
+ }
319
+ msid = info[2].As<Napi::String>().ToString();
320
+ }
321
+
322
+ if (length > 3)
323
+ {
324
+ if (!info[3].IsString())
325
+ {
326
+ Napi::TypeError::New(env, "trackID as String expected").ThrowAsJavaScriptException();
327
+ return;
328
+ }
329
+ trackID = info[3].As<Napi::String>().ToString();
330
+ }
331
+
332
+ mVideoPtr->addSSRC(ssrc, name, msid, trackID);
333
+ }
334
+
335
+ void VideoWrapper::removeSSRC(const Napi::CallbackInfo &info)
336
+ {
337
+ Napi::Env env = info.Env();
338
+ int length = info.Length();
339
+
340
+ if (length < 1 || !info[0].IsNumber())
341
+ {
342
+ Napi::TypeError::New(env, "We expect (Number) as param").ThrowAsJavaScriptException();
343
+ return;
344
+ }
345
+
346
+ uint32_t ssrc = static_cast<uint32_t>(info[0].As<Napi::Number>().ToNumber());
347
+
348
+ mVideoPtr->removeSSRC(ssrc);
349
+ }
350
+
351
+ void VideoWrapper::replaceSSRC(const Napi::CallbackInfo &info)
352
+ {
353
+ Napi::Env env = info.Env();
354
+ int length = info.Length();
355
+
356
+ if (length < 2 || !info[0].IsNumber() || !info[1].IsNumber())
357
+ {
358
+ Napi::TypeError::New(env,
359
+ "We expect (Number, Number, String[optional], String[optional], String[optional]) as param")
360
+ .ThrowAsJavaScriptException();
361
+ return;
362
+ }
363
+
364
+ uint32_t ssrc = static_cast<uint32_t>(info[0].As<Napi::Number>().ToNumber());
365
+ uint32_t oldSsrc = static_cast<uint32_t>(info[1].As<Napi::Number>().ToNumber());
366
+ std::optional<std::string> name;
367
+ std::optional<std::string> msid = std::nullopt;
368
+ std::optional<std::string> trackID = std::nullopt;
369
+
370
+ if (length > 2)
371
+ {
372
+ if (!info[2].IsString())
373
+ {
374
+ Napi::TypeError::New(env, "name as String expected").ThrowAsJavaScriptException();
375
+ return;
376
+ }
377
+ name = info[2].As<Napi::String>().ToString();
378
+ }
379
+
380
+ if (length > 3)
381
+ {
382
+ if (!info[3].IsString())
383
+ {
384
+ Napi::TypeError::New(env, "msid as String expected").ThrowAsJavaScriptException();
385
+ return;
386
+ }
387
+ msid = info[3].As<Napi::String>().ToString();
388
+ }
389
+
390
+ if (length > 4)
391
+ {
392
+ if (!info[4].IsString())
393
+ {
394
+ Napi::TypeError::New(env, "trackID as String expected").ThrowAsJavaScriptException();
395
+ return;
396
+ }
397
+ trackID = info[4].As<Napi::String>().ToString();
398
+ }
399
+
400
+ mVideoPtr->replaceSSRC(oldSsrc, ssrc, name, msid, trackID);
401
+ }
402
+
403
+ Napi::Value VideoWrapper::hasSSRC(const Napi::CallbackInfo &info)
404
+ {
405
+ Napi::Env env = info.Env();
406
+ int length = info.Length();
407
+
408
+ if (length < 1 || !info[0].IsNumber())
409
+ {
410
+ Napi::TypeError::New(env, "We expect (Number) as param").ThrowAsJavaScriptException();
411
+ return env.Null();
412
+ }
413
+
414
+ uint32_t ssrc = static_cast<uint32_t>(info[0].As<Napi::Number>().ToNumber());
415
+
416
+ return Napi::Boolean::New(env, mVideoPtr->hasSSRC(ssrc));
417
+ }
418
+
419
+ Napi::Value VideoWrapper::getSSRCs(const Napi::CallbackInfo &info)
420
+ {
421
+ Napi::Env env = info.Env();
422
+
423
+ auto list = mVideoPtr->getSSRCs();
424
+
425
+ Napi::Uint32Array napiArr = Napi::Uint32Array::New(env, list.size());
426
+ for (size_t i = 0; i < list.size(); i++)
427
+ napiArr[i] = list[i];
428
+
429
+ return napiArr;
430
+ }
431
+
432
+ Napi::Value VideoWrapper::getCNameForSsrc(const Napi::CallbackInfo &info)
433
+ {
434
+ Napi::Env env = info.Env();
435
+ int length = info.Length();
436
+
437
+ if (length < 1 || !info[0].IsNumber())
438
+ {
439
+ Napi::TypeError::New(env, "We expect (Number) as param").ThrowAsJavaScriptException();
440
+ return env.Null();
441
+ }
442
+
443
+ uint32_t ssrc = static_cast<uint32_t>(info[0].As<Napi::Number>().ToNumber());
444
+
445
+ std::optional<std::string> name = mVideoPtr->getCNameForSsrc(ssrc);
446
+
447
+ if (!name.has_value())
448
+ return env.Null();
449
+
450
+ return Napi::String::New(env, name.value());
451
+ }
452
+
453
+ void VideoWrapper::setBitrate(const Napi::CallbackInfo &info)
454
+ {
455
+ Napi::Env env = info.Env();
456
+ int length = info.Length();
457
+
458
+ if (length < 1 || !info[0].IsNumber())
459
+ {
460
+ Napi::TypeError::New(env, "We expect (Number) as param").ThrowAsJavaScriptException();
461
+ return;
462
+ }
463
+
464
+ unsigned int bitrate = info[0].As<Napi::Number>().ToNumber().Uint32Value();
465
+ mVideoPtr->setBitrate(bitrate);
466
+ }
467
+
468
+ Napi::Value VideoWrapper::getBitrate(const Napi::CallbackInfo &info)
469
+ {
470
+ return Napi::Number::New(info.Env(), mVideoPtr->bitrate());
471
+ }
472
+
473
+ Napi::Value VideoWrapper::hasPayloadType(const Napi::CallbackInfo &info)
474
+ {
475
+ Napi::Env env = info.Env();
476
+ int length = info.Length();
477
+
478
+ if (length < 1 || !info[0].IsNumber())
479
+ {
480
+ Napi::TypeError::New(env, "We expect (Number) as param").ThrowAsJavaScriptException();
481
+ return env.Null();
482
+ }
483
+
484
+ int payloadType = static_cast<int32_t>(info[0].As<Napi::Number>().ToNumber());
485
+
486
+ return Napi::Boolean::New(env, mVideoPtr->hasPayloadType(payloadType));
487
+ }
488
+
489
+ void VideoWrapper::addRTXCodec(const Napi::CallbackInfo &info)
490
+ {
491
+ Napi::Env env = info.Env();
492
+ int length = info.Length();
493
+
494
+ if (length < 3 || !info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber())
495
+ {
496
+ Napi::TypeError::New(env, "We expect (Number,Number,Number) as param").ThrowAsJavaScriptException();
497
+ return;
498
+ }
499
+
500
+ int payloadType = static_cast<int32_t>(info[0].As<Napi::Number>().ToNumber());
501
+ int originalPayloadType = static_cast<int32_t>(info[1].As<Napi::Number>().ToNumber());
502
+ unsigned int clockRate = static_cast<uint32_t>(info[2].As<Napi::Number>().ToNumber());
503
+
504
+ mVideoPtr->addRtxCodec(payloadType, originalPayloadType, clockRate);
505
+ }
506
+
507
+ void VideoWrapper::addRTPMap(const Napi::CallbackInfo &info)
508
+ {
509
+ // mVideoPtr->addRTPMap()
510
+ }
511
+
512
+ void VideoWrapper::parseSdpLine(const Napi::CallbackInfo &info)
513
+ {
514
+ Napi::Env env = info.Env();
515
+ int length = info.Length();
516
+
517
+ if (length < 1 || !info[0].IsString())
518
+ {
519
+ Napi::TypeError::New(env, "We expect (String) as param").ThrowAsJavaScriptException();
520
+ return;
521
+ }
522
+
523
+ std::string line = info[0].As<Napi::String>().ToString();
524
+
525
+ mVideoPtr->parseSdpLine(line);
526
+ }
@@ -0,0 +1,56 @@
1
+ #ifndef MEDIA_VIDEO_WRAPPER_H
2
+ #define MEDIA_VIDEO_WRAPPER_H
3
+
4
+ #include <unordered_set>
5
+
6
+ #include <napi.h>
7
+ #include <rtc/rtc.hpp>
8
+
9
+ class VideoWrapper : public Napi::ObjectWrap<VideoWrapper>
10
+ {
11
+ public:
12
+ static Napi::FunctionReference constructor;
13
+ static Napi::Object Init(Napi::Env env, Napi::Object exports);
14
+ VideoWrapper(const Napi::CallbackInfo &info);
15
+ ~VideoWrapper();
16
+
17
+ rtc::Description::Video getVideoInstance();
18
+
19
+ // Functions
20
+ void addVideoCodec(const Napi::CallbackInfo &info);
21
+ void addH264Codec(const Napi::CallbackInfo &info);
22
+ void addH265Codec(const Napi::CallbackInfo &info);
23
+ void addVP8Codec(const Napi::CallbackInfo &info);
24
+ void addVP9Codec(const Napi::CallbackInfo &info);
25
+ void addAV1Codec(const Napi::CallbackInfo &info);
26
+
27
+ // class Entry
28
+ Napi::Value direction(const Napi::CallbackInfo &info);
29
+ Napi::Value generateSdp(const Napi::CallbackInfo &info);
30
+ Napi::Value mid(const Napi::CallbackInfo &info);
31
+ void setDirection(const Napi::CallbackInfo &info);
32
+
33
+ // class Media
34
+ Napi::Value description(const Napi::CallbackInfo &info);
35
+ void removeFormat(const Napi::CallbackInfo &info);
36
+ void addSSRC(const Napi::CallbackInfo &info);
37
+ void removeSSRC(const Napi::CallbackInfo &info);
38
+ void replaceSSRC(const Napi::CallbackInfo &info);
39
+ Napi::Value hasSSRC(const Napi::CallbackInfo &info);
40
+ Napi::Value getSSRCs(const Napi::CallbackInfo &info);
41
+ Napi::Value getCNameForSsrc(const Napi::CallbackInfo &info);
42
+ void setBitrate(const Napi::CallbackInfo &info);
43
+ Napi::Value getBitrate(const Napi::CallbackInfo &info);
44
+ Napi::Value hasPayloadType(const Napi::CallbackInfo &info);
45
+ void addRTXCodec(const Napi::CallbackInfo &info);
46
+ void addRTPMap(const Napi::CallbackInfo &info);
47
+ void parseSdpLine(const Napi::CallbackInfo &info);
48
+
49
+ // Callbacks
50
+
51
+ private:
52
+ static std::unordered_set<VideoWrapper *> instances;
53
+ std::shared_ptr<rtc::Description::Video> mVideoPtr = nullptr;
54
+ };
55
+
56
+ #endif // MEDIA_VIDEO_WRAPPER_H