werift 0.22.1 → 0.22.3

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 (244) hide show
  1. package/lib/common/src/binary.d.ts +8 -7
  2. package/lib/common/src/binary.js +5 -2
  3. package/lib/common/src/binary.js.map +1 -1
  4. package/lib/common/src/event.d.ts +1 -0
  5. package/lib/common/src/event.js +5 -0
  6. package/lib/common/src/event.js.map +1 -1
  7. package/lib/common/src/log.d.ts +1 -2
  8. package/lib/common/src/transport.d.ts +4 -1
  9. package/lib/common/src/transport.js +35 -14
  10. package/lib/common/src/transport.js.map +1 -1
  11. package/lib/dtls/src/cipher/create.d.ts +2 -1
  12. package/lib/dtls/src/cipher/create.js.map +1 -1
  13. package/lib/dtls/src/cipher/prf.d.ts +12 -12
  14. package/lib/dtls/src/cipher/suites/abstract.d.ts +2 -1
  15. package/lib/dtls/src/cipher/suites/abstract.js.map +1 -1
  16. package/lib/dtls/src/cipher/suites/aead.d.ts +3 -2
  17. package/lib/dtls/src/cipher/suites/aead.js +34 -41
  18. package/lib/dtls/src/cipher/suites/aead.js.map +1 -1
  19. package/lib/dtls/src/context/cipher.d.ts +4 -4
  20. package/lib/dtls/src/context/cipher.js +25 -15
  21. package/lib/dtls/src/context/cipher.js.map +1 -1
  22. package/lib/dtls/src/context/srtp.d.ts +3 -3
  23. package/lib/dtls/src/context/srtp.js.map +1 -1
  24. package/lib/dtls/src/flight/client/flight5.js.map +1 -1
  25. package/lib/dtls/src/flight/server/flight2.js.map +1 -1
  26. package/lib/dtls/src/handshake/extensions/ellipticCurves.d.ts +1 -1
  27. package/lib/dtls/src/handshake/extensions/renegotiationIndication.d.ts +2 -2
  28. package/lib/dtls/src/handshake/extensions/signature.d.ts +2 -2
  29. package/lib/dtls/src/handshake/extensions/useSrtp.d.ts +1 -1
  30. package/lib/dtls/src/handshake/extensions/useSrtp.js +4 -7
  31. package/lib/dtls/src/handshake/extensions/useSrtp.js.map +1 -1
  32. package/lib/dtls/src/handshake/message/alert.d.ts +1 -1
  33. package/lib/dtls/src/handshake/message/certificate.d.ts +1 -1
  34. package/lib/dtls/src/handshake/message/changeCipherSpec.d.ts +1 -1
  35. package/lib/dtls/src/handshake/message/client/certificateVerify.d.ts +1 -1
  36. package/lib/dtls/src/handshake/message/client/hello.d.ts +1 -1
  37. package/lib/dtls/src/handshake/message/client/keyExchange.d.ts +1 -1
  38. package/lib/dtls/src/handshake/message/finished.d.ts +1 -1
  39. package/lib/dtls/src/handshake/message/server/certificateRequest.d.ts +1 -1
  40. package/lib/dtls/src/handshake/message/server/hello.d.ts +1 -1
  41. package/lib/dtls/src/handshake/message/server/helloDone.d.ts +1 -1
  42. package/lib/dtls/src/handshake/message/server/helloVerifyRequest.d.ts +1 -1
  43. package/lib/dtls/src/handshake/message/server/keyExchange.d.ts +1 -1
  44. package/lib/dtls/src/handshake/random.d.ts +3 -3
  45. package/lib/dtls/src/record/message/fragment.d.ts +1 -1
  46. package/lib/dtls/src/record/message/header.d.ts +2 -19
  47. package/lib/dtls/src/record/message/header.js +1 -60
  48. package/lib/dtls/src/record/message/header.js.map +1 -1
  49. package/lib/dtls/src/record/message/plaintext.d.ts +6 -27
  50. package/lib/dtls/src/record/message/plaintext.js +34 -16
  51. package/lib/dtls/src/record/message/plaintext.js.map +1 -1
  52. package/lib/dtls/src/socket.d.ts +4 -4
  53. package/lib/dtls/src/socket.js.map +1 -1
  54. package/lib/dtls/src/util/binary.d.ts +1 -1
  55. package/lib/ice/src/candidate.js +1 -5
  56. package/lib/ice/src/candidate.js.map +1 -1
  57. package/lib/ice/src/helper.d.ts +1 -1
  58. package/lib/ice/src/ice.d.ts +4 -3
  59. package/lib/ice/src/ice.js +93 -72
  60. package/lib/ice/src/ice.js.map +1 -1
  61. package/lib/ice/src/iceBase.d.ts +21 -2
  62. package/lib/ice/src/iceBase.js +47 -1
  63. package/lib/ice/src/iceBase.js.map +1 -1
  64. package/lib/ice/src/stun/attributes.d.ts +2 -2
  65. package/lib/ice/src/stun/attributes.js +48 -29
  66. package/lib/ice/src/stun/attributes.js.map +1 -1
  67. package/lib/ice/src/stun/message.d.ts +2 -2
  68. package/lib/ice/src/stun/message.js +22 -23
  69. package/lib/ice/src/stun/message.js.map +1 -1
  70. package/lib/ice/src/stun/protocol.d.ts +2 -2
  71. package/lib/ice/src/stun/protocol.js +6 -0
  72. package/lib/ice/src/stun/protocol.js.map +1 -1
  73. package/lib/ice/src/turn/protocol.d.ts +2 -2
  74. package/lib/ice/src/turn/protocol.js +3 -0
  75. package/lib/ice/src/turn/protocol.js.map +1 -1
  76. package/lib/index.mjs +2707 -1883
  77. package/lib/nonstandard/index.mjs +737 -698
  78. package/lib/rtp/src/codec/av1.d.ts +2 -2
  79. package/lib/rtp/src/codec/opus.d.ts +1 -1
  80. package/lib/rtp/src/extra/container/mp4/container.js +18 -8
  81. package/lib/rtp/src/extra/container/mp4/container.js.map +1 -1
  82. package/lib/rtp/src/extra/container/mp4/h264.d.ts +2 -2
  83. package/lib/rtp/src/extra/container/mp4/sps-parser.d.ts +1 -1
  84. package/lib/rtp/src/extra/container/ogg/parser.d.ts +1 -1
  85. package/lib/rtp/src/extra/container/webm/container.d.ts +9 -9
  86. package/lib/rtp/src/extra/container/webm/container.js +24 -12
  87. package/lib/rtp/src/extra/container/webm/container.js.map +1 -1
  88. package/lib/rtp/src/extra/container/webm/ebml/ebml.d.ts +35 -1
  89. package/lib/rtp/src/extra/container/webm/ebml/ebml.js +75 -1
  90. package/lib/rtp/src/extra/container/webm/ebml/ebml.js.map +1 -1
  91. package/lib/rtp/src/extra/container/webm/ebml/id.d.ts +222 -222
  92. package/lib/rtp/src/extra/container/webm/index.d.ts +1 -0
  93. package/lib/rtp/src/extra/container/webm/index.js +1 -0
  94. package/lib/rtp/src/extra/container/webm/index.js.map +1 -1
  95. package/lib/rtp/src/extra/container/webm/util.d.ts +8 -0
  96. package/lib/rtp/src/extra/container/webm/util.js +100 -0
  97. package/lib/rtp/src/extra/container/webm/util.js.map +1 -0
  98. package/lib/rtp/src/extra/processor/depacketizerCallback.d.ts +1 -1
  99. package/lib/rtp/src/extra/processor/depacketizerTransformer.d.ts +6 -1
  100. package/lib/rtp/src/extra/processor/dtx.d.ts +1 -1
  101. package/lib/rtp/src/extra/processor/dtx.js +1 -1
  102. package/lib/rtp/src/extra/processor/dtx.js.map +1 -1
  103. package/lib/rtp/src/extra/processor/dtxCallback.d.ts +1 -1
  104. package/lib/rtp/src/extra/processor/interface.d.ts +1 -1
  105. package/lib/rtp/src/extra/processor/jitterBufferCallback.d.ts +1 -1
  106. package/lib/rtp/src/extra/processor/lipsync.js +1 -1
  107. package/lib/rtp/src/extra/processor/lipsync.js.map +1 -1
  108. package/lib/rtp/src/extra/processor/mp4.js +3 -0
  109. package/lib/rtp/src/extra/processor/mp4.js.map +1 -1
  110. package/lib/rtp/src/extra/processor/mute.d.ts +1 -1
  111. package/lib/rtp/src/extra/processor/mute.js +1 -1
  112. package/lib/rtp/src/extra/processor/mute.js.map +1 -1
  113. package/lib/rtp/src/extra/processor/nackHandlerCallback.d.ts +1 -1
  114. package/lib/rtp/src/extra/processor/ntpTime.d.ts +1 -1
  115. package/lib/rtp/src/extra/processor/ntpTime.js +1 -1
  116. package/lib/rtp/src/extra/processor/ntpTime.js.map +1 -1
  117. package/lib/rtp/src/extra/processor/ntpTimeCallback.d.ts +1 -1
  118. package/lib/rtp/src/extra/processor/rtpTimeCallback.d.ts +1 -1
  119. package/lib/rtp/src/extra/processor/webm.d.ts +3 -3
  120. package/lib/rtp/src/extra/processor/webm.js.map +1 -1
  121. package/lib/rtp/src/extra/processor/webmStream.d.ts +2 -2
  122. package/lib/rtp/src/extra/processor/webmStream.js.map +1 -1
  123. package/lib/rtp/src/helper.d.ts +1 -1
  124. package/lib/rtp/src/rtcp/header.d.ts +2 -1
  125. package/lib/rtp/src/rtcp/header.js +10 -0
  126. package/lib/rtp/src/rtcp/header.js.map +1 -1
  127. package/lib/rtp/src/rtcp/psfb/fullIntraRequest.d.ts +1 -1
  128. package/lib/rtp/src/rtcp/psfb/index.d.ts +2 -2
  129. package/lib/rtp/src/rtcp/psfb/index.js +2 -2
  130. package/lib/rtp/src/rtcp/psfb/index.js.map +1 -1
  131. package/lib/rtp/src/rtcp/psfb/pictureLossIndication.d.ts +1 -1
  132. package/lib/rtp/src/rtcp/psfb/remb.d.ts +1 -1
  133. package/lib/rtp/src/rtcp/rr.d.ts +2 -2
  134. package/lib/rtp/src/rtcp/rr.js +2 -2
  135. package/lib/rtp/src/rtcp/rr.js.map +1 -1
  136. package/lib/rtp/src/rtcp/rtcp.d.ts +0 -1
  137. package/lib/rtp/src/rtcp/rtcp.js +0 -10
  138. package/lib/rtp/src/rtcp/rtcp.js.map +1 -1
  139. package/lib/rtp/src/rtcp/rtpfb/const.d.ts +1 -0
  140. package/lib/rtp/src/rtcp/rtpfb/const.js +5 -0
  141. package/lib/rtp/src/rtcp/rtpfb/const.js.map +1 -0
  142. package/lib/rtp/src/rtcp/rtpfb/index.d.ts +1 -1
  143. package/lib/rtp/src/rtcp/rtpfb/index.js +2 -1
  144. package/lib/rtp/src/rtcp/rtpfb/index.js.map +1 -1
  145. package/lib/rtp/src/rtcp/rtpfb/nack.d.ts +1 -1
  146. package/lib/rtp/src/rtcp/rtpfb/nack.js +2 -2
  147. package/lib/rtp/src/rtcp/rtpfb/nack.js.map +1 -1
  148. package/lib/rtp/src/rtcp/rtpfb/twcc.d.ts +4 -4
  149. package/lib/rtp/src/rtcp/sdes.d.ts +4 -4
  150. package/lib/rtp/src/rtcp/sdes.js +2 -2
  151. package/lib/rtp/src/rtcp/sdes.js.map +1 -1
  152. package/lib/rtp/src/rtcp/sr.d.ts +2 -2
  153. package/lib/rtp/src/rtcp/sr.js +2 -2
  154. package/lib/rtp/src/rtcp/sr.js.map +1 -1
  155. package/lib/rtp/src/rtp/headerExtension.d.ts +6 -6
  156. package/lib/rtp/src/rtp/red/packet.d.ts +2 -2
  157. package/lib/rtp/src/rtp/rtp.d.ts +2 -2
  158. package/lib/rtp/src/srtp/cipher/ctr.d.ts +4 -4
  159. package/lib/rtp/src/srtp/cipher/gcm.d.ts +4 -4
  160. package/lib/rtp/src/srtp/const.d.ts +3 -3
  161. package/lib/rtp/src/srtp/const.js.map +1 -1
  162. package/lib/rtp/src/srtp/context/context.d.ts +10 -10
  163. package/lib/rtp/src/srtp/context/context.js +10 -4
  164. package/lib/rtp/src/srtp/context/context.js.map +1 -1
  165. package/lib/rtp/src/srtp/context/srtcp.d.ts +3 -3
  166. package/lib/rtp/src/srtp/context/srtcp.js.map +1 -1
  167. package/lib/rtp/src/srtp/context/srtp.d.ts +3 -3
  168. package/lib/rtp/src/srtp/context/srtp.js.map +1 -1
  169. package/lib/rtp/src/srtp/srtcp.d.ts +2 -2
  170. package/lib/rtp/src/srtp/srtp.d.ts +2 -2
  171. package/lib/sctp/src/chunk.d.ts +12 -12
  172. package/lib/sctp/src/chunk.js +87 -66
  173. package/lib/sctp/src/chunk.js.map +1 -1
  174. package/lib/sctp/src/param.d.ts +3 -3
  175. package/lib/sctp/src/param.js +4 -5
  176. package/lib/sctp/src/param.js.map +1 -1
  177. package/lib/sctp/src/sctp.d.ts +3 -1
  178. package/lib/sctp/src/sctp.js +58 -15
  179. package/lib/sctp/src/sctp.js.map +1 -1
  180. package/lib/webrtc/src/dataChannel.d.ts +15 -4
  181. package/lib/webrtc/src/dataChannel.js +42 -7
  182. package/lib/webrtc/src/dataChannel.js.map +1 -1
  183. package/lib/webrtc/src/index.d.ts +1 -0
  184. package/lib/webrtc/src/index.js +1 -0
  185. package/lib/webrtc/src/index.js.map +1 -1
  186. package/lib/webrtc/src/media/index.d.ts +2 -0
  187. package/lib/webrtc/src/media/index.js +2 -0
  188. package/lib/webrtc/src/media/index.js.map +1 -1
  189. package/lib/webrtc/src/media/parameters.d.ts +2 -2
  190. package/lib/webrtc/src/media/parameters.js.map +1 -1
  191. package/lib/webrtc/src/media/receiver/nack.js +3 -7
  192. package/lib/webrtc/src/media/receiver/nack.js.map +1 -1
  193. package/lib/webrtc/src/media/receiver/receiverTwcc.d.ts +2 -2
  194. package/lib/webrtc/src/media/receiver/receiverTwcc.js +1 -1
  195. package/lib/webrtc/src/media/receiver/receiverTwcc.js.map +1 -1
  196. package/lib/webrtc/src/media/router.js +2 -1
  197. package/lib/webrtc/src/media/router.js.map +1 -1
  198. package/lib/webrtc/src/media/rtpReceiver.d.ts +3 -3
  199. package/lib/webrtc/src/media/rtpReceiver.js +53 -4
  200. package/lib/webrtc/src/media/rtpReceiver.js.map +1 -1
  201. package/lib/webrtc/src/media/rtpSender.d.ts +6 -0
  202. package/lib/webrtc/src/media/rtpSender.js +64 -26
  203. package/lib/webrtc/src/media/rtpSender.js.map +1 -1
  204. package/lib/webrtc/src/media/rtpTransceiver.d.ts +12 -9
  205. package/lib/webrtc/src/media/rtpTransceiver.js +30 -26
  206. package/lib/webrtc/src/media/rtpTransceiver.js.map +1 -1
  207. package/lib/webrtc/src/media/stats.d.ts +279 -0
  208. package/lib/webrtc/src/media/stats.js +35 -1
  209. package/lib/webrtc/src/media/stats.js.map +1 -1
  210. package/lib/webrtc/src/media/track.js +3 -3
  211. package/lib/webrtc/src/media/track.js.map +1 -1
  212. package/lib/webrtc/src/nonstandard/recorder/writer/webm.js.map +1 -1
  213. package/lib/webrtc/src/nonstandard/userMedia.js +2 -2
  214. package/lib/webrtc/src/nonstandard/userMedia.js.map +1 -1
  215. package/lib/webrtc/src/peerConnection.d.ts +35 -64
  216. package/lib/webrtc/src/peerConnection.js +337 -1034
  217. package/lib/webrtc/src/peerConnection.js.map +1 -1
  218. package/lib/webrtc/src/sctpManager.d.ts +27 -0
  219. package/lib/webrtc/src/sctpManager.js +147 -0
  220. package/lib/webrtc/src/sctpManager.js.map +1 -0
  221. package/lib/webrtc/src/sdp.d.ts +3 -2
  222. package/lib/webrtc/src/sdp.js +17 -7
  223. package/lib/webrtc/src/sdp.js.map +1 -1
  224. package/lib/webrtc/src/sdpManager.d.ts +78 -0
  225. package/lib/webrtc/src/sdpManager.js +391 -0
  226. package/lib/webrtc/src/sdpManager.js.map +1 -0
  227. package/lib/webrtc/src/secureTransportManager.d.ts +56 -0
  228. package/lib/webrtc/src/secureTransportManager.js +349 -0
  229. package/lib/webrtc/src/secureTransportManager.js.map +1 -0
  230. package/lib/webrtc/src/transceiverManager.d.ts +40 -0
  231. package/lib/webrtc/src/transceiverManager.js +355 -0
  232. package/lib/webrtc/src/transceiverManager.js.map +1 -0
  233. package/lib/webrtc/src/transport/dtls.d.ts +18 -6
  234. package/lib/webrtc/src/transport/dtls.js +128 -13
  235. package/lib/webrtc/src/transport/dtls.js.map +1 -1
  236. package/lib/webrtc/src/transport/ice.d.ts +24 -0
  237. package/lib/webrtc/src/transport/ice.js +88 -2
  238. package/lib/webrtc/src/transport/ice.js.map +1 -1
  239. package/lib/webrtc/src/transport/sctp.js +13 -26
  240. package/lib/webrtc/src/transport/sctp.js.map +1 -1
  241. package/lib/webrtc/src/utils.d.ts +9 -4
  242. package/lib/webrtc/src/utils.js +34 -13
  243. package/lib/webrtc/src/utils.js.map +1 -1
  244. package/package.json +12 -26
@@ -193,7 +193,10 @@ var BitStream = class {
193
193
  }
194
194
  return this;
195
195
  }
196
- readBits(bits, bitBuffer) {
196
+ readBits(bits) {
197
+ return this._readBits(bits);
198
+ }
199
+ _readBits(bits, bitBuffer) {
197
200
  if (typeof bitBuffer == "undefined") {
198
201
  bitBuffer = 0;
199
202
  }
@@ -214,7 +217,7 @@ var BitStream = class {
214
217
  }
215
218
  bits -= bitsConsumed;
216
219
  bitBuffer = bitBuffer << bitsConsumed | partial;
217
- return bits > 0 ? this.readBits(bits, bitBuffer) : bitBuffer;
220
+ return bits > 0 ? this._readBits(bits, bitBuffer) : bitBuffer;
218
221
  }
219
222
  seekTo(bitPos) {
220
223
  this.position = bitPos / 8 | 0;
@@ -438,6 +441,11 @@ var Event = class {
438
441
  };
439
442
  return { unSubscribe, disposer };
440
443
  };
444
+ pipe(e) {
445
+ this.subscribe((...args) => {
446
+ e.execute(...args);
447
+ });
448
+ }
441
449
  queuingSubscribe = (execute, complete, error) => {
442
450
  if (this.ended) throw new Error("event completed");
443
451
  const id = this.event.eventId;
@@ -522,6 +530,7 @@ var EventDisposer = class {
522
530
  import {
523
531
  createSocket as createSocket2
524
532
  } from "dgram";
533
+ import net from "net";
525
534
  import { connect } from "net";
526
535
  var log = debug("werift-ice:packages/ice/src/transport.ts");
527
536
  var UdpTransport = class _UdpTransport {
@@ -546,6 +555,7 @@ var UdpTransport = class _UdpTransport {
546
555
  rinfo;
547
556
  onData = () => {
548
557
  };
558
+ closed = false;
549
559
  static async init(type, options = {}) {
550
560
  const transport = new _UdpTransport(type, options);
551
561
  await transport.init();
@@ -571,17 +581,23 @@ var UdpTransport = class _UdpTransport {
571
581
  }
572
582
  await new Promise((r) => this.socket.once("listening", r));
573
583
  }
574
- send = (data, addr) => new Promise((r, f) => {
575
- addr = addr ?? [this.rinfo?.address, this.rinfo?.port];
576
- this.socket.send(data, addr[1], addr[0], (error) => {
577
- if (error) {
578
- log("send error", addr, data);
579
- f(error);
580
- } else {
581
- r();
582
- }
583
- });
584
- });
584
+ send = async (data, addr) => {
585
+ if (addr && !net.isIP(addr[0])) {
586
+ return new Promise((r, f) => {
587
+ this.socket.send(data, addr[1], addr[0], (error) => {
588
+ if (error) {
589
+ log("send error", addr, data);
590
+ f(error);
591
+ } else {
592
+ r();
593
+ }
594
+ });
595
+ });
596
+ } else {
597
+ addr = addr ?? [this.rinfo?.address, this.rinfo?.port];
598
+ this.socket.send(data, addr[1], addr[0]);
599
+ }
600
+ };
585
601
  get address() {
586
602
  return this.socket.address();
587
603
  }
@@ -592,6 +608,7 @@ var UdpTransport = class _UdpTransport {
592
608
  return this.socket.address().port;
593
609
  }
594
610
  close = () => new Promise((r) => {
611
+ this.closed = true;
595
612
  this.socket.once("close", r);
596
613
  try {
597
614
  this.socket.close();
@@ -818,7 +835,7 @@ var Mp4Container = class {
818
835
  throw new Error("bufferFrame missing");
819
836
  }
820
837
  const duration = frame.timestamp - bufferFrame.timestamp;
821
- const buffer2 = new Uint8Array(bufferFrame.byteLength);
838
+ const buffer2 = new ArrayBuffer(bufferFrame.byteLength);
822
839
  bufferFrame.copyTo(buffer2);
823
840
  this.#mp4.addSample(track, buffer2, {
824
841
  duration,
@@ -2202,6 +2219,13 @@ var float = (num) => bytes(float32bit(num));
2202
2219
  var vintEncodedNumber = (num) => {
2203
2220
  return bytes(vintEncode(numberToByteArray(num, getEBMLByteLength(num))));
2204
2221
  };
2222
+ var decodeVintEncodedNumber = (buf, offset = 0) => {
2223
+ const { value, length, unknown } = vintDecode(buf, offset);
2224
+ if (unknown || value === void 0) {
2225
+ throw new Error("decodeVintEncodedNumber: value is unknown size sentinel");
2226
+ }
2227
+ return { value, length };
2228
+ };
2205
2229
  var string = (str) => {
2206
2230
  return bytes(stringToByteArray(str));
2207
2231
  };
@@ -2260,6 +2284,45 @@ var vintEncode = (byteArray) => {
2260
2284
  var getSizeMask = (byteLength) => {
2261
2285
  return 128 >> byteLength - 1;
2262
2286
  };
2287
+ var vintDecode = (buf, offset = 0) => {
2288
+ if (offset >= buf.length) {
2289
+ throw new Error("vintDecode: offset out of range");
2290
+ }
2291
+ const first = buf[offset];
2292
+ if (first === 0) {
2293
+ throw new Error("vintDecode: invalid first byte 0x00 (no leading 1 bit)");
2294
+ }
2295
+ let length = 0;
2296
+ for (let i = 0; i < 8; i++) {
2297
+ const mask = 128 >> i;
2298
+ if (first & mask) {
2299
+ length = i + 1;
2300
+ break;
2301
+ }
2302
+ }
2303
+ if (length === 0) {
2304
+ throw new Error("vintDecode: could not determine length");
2305
+ }
2306
+ if (offset + length > buf.length) {
2307
+ throw new Error("vintDecode: insufficient bytes for declared length");
2308
+ }
2309
+ const lengthMarker = getSizeMask(length);
2310
+ let valueBig = BigInt(first & ~lengthMarker);
2311
+ for (let i = 1; i < length; i++) {
2312
+ valueBig = valueBig << 8n | BigInt(buf[offset + i]);
2313
+ }
2314
+ const allOnes = (1n << BigInt(7 * length)) - 1n;
2315
+ const unknown = valueBig === allOnes;
2316
+ let value;
2317
+ if (unknown) {
2318
+ value = void 0;
2319
+ } else if (valueBig <= BigInt(Number.MAX_SAFE_INTEGER)) {
2320
+ value = Number(valueBig);
2321
+ } else {
2322
+ value = valueBig;
2323
+ }
2324
+ return { value, length, unknown };
2325
+ };
2263
2326
 
2264
2327
  // ../rtp/src/extra/container/webm/ebml/id.ts
2265
2328
  var ID = {
@@ -2530,17 +2593,19 @@ var WEBMContainer = class {
2530
2593
  if (kind === "video") {
2531
2594
  width ??= 640;
2532
2595
  height ??= 360;
2533
- roll ??= 0;
2534
- trackElements.push(
2535
- element(ID.Video, [
2536
- element(ID.PixelWidth, number(width)),
2537
- element(ID.PixelHeight, number(height)),
2596
+ const videoElements = [
2597
+ element(ID.PixelWidth, number(width)),
2598
+ element(ID.PixelHeight, number(height))
2599
+ ];
2600
+ if (roll !== void 0) {
2601
+ videoElements.push(
2538
2602
  element(ID.Projection, [
2539
2603
  element(ID.ProjectionType, number(0)),
2540
2604
  element(ID.ProjectionPoseRoll, float(roll))
2541
2605
  ])
2542
- ])
2543
- );
2606
+ );
2607
+ }
2608
+ trackElements.push(element(ID.Video, videoElements));
2544
2609
  } else {
2545
2610
  trackElements.push(
2546
2611
  element(ID.Audio, [
@@ -2678,6 +2743,59 @@ var containerSupportedCodecs = [
2678
2743
  ];
2679
2744
  var millisecond = 1e6;
2680
2745
 
2746
+ // ../rtp/src/extra/container/webm/util.ts
2747
+ function serializeSimpleBlock(frame, isKeyframe, trackNumber, relativeTimestamp) {
2748
+ const elementId = Buffer.from([163]);
2749
+ const contentSize = vintEncodedNumber(
2750
+ 1 + 2 + 1 + frame.length
2751
+ ).bytes;
2752
+ const keyframe = isKeyframe ? 1 : 0;
2753
+ const flags = new BitWriter2(8).set(keyframe).set(0, 3).set(0).set(0, 2).set(0);
2754
+ const simpleBlock = Buffer.concat([
2755
+ elementId,
2756
+ contentSize,
2757
+ vintEncodedNumber(trackNumber).bytes,
2758
+ new BufferChain(2).writeInt16BE(relativeTimestamp).buffer,
2759
+ new BufferChain(1).writeUInt8(flags.value).buffer,
2760
+ frame
2761
+ ]);
2762
+ return simpleBlock;
2763
+ }
2764
+ function deserializeSimpleBlocks(data) {
2765
+ const frames = [];
2766
+ let position = 0;
2767
+ while (position < data.length) {
2768
+ const elementId = data[position];
2769
+ if (elementId !== 163) {
2770
+ throw new Error(`unexpected element id 0x${elementId.toString(16)}`);
2771
+ }
2772
+ position += 1;
2773
+ const { value: contentSize, length: contentSizeLength } = decodeVintEncodedNumber(data, position);
2774
+ position += contentSizeLength;
2775
+ const { value: trackNumber, length: trackNumberLength } = decodeVintEncodedNumber(data, position);
2776
+ position += trackNumberLength;
2777
+ const timecode = data.readInt16BE(position);
2778
+ position += 2;
2779
+ const flags = new BitStream(data.subarray(position, position + 1));
2780
+ const isKeyframe = flags.readBits(1) === 1;
2781
+ position += 1;
2782
+ const metaSize = trackNumberLength + 2 + 1;
2783
+ const remaining = Number(contentSize) - metaSize;
2784
+ if (remaining < 0) {
2785
+ throw new Error("invalid simple block size");
2786
+ }
2787
+ const frame = data.subarray(position, position + remaining);
2788
+ frames.push({
2789
+ data: frame,
2790
+ trackNumber: Number(trackNumber),
2791
+ isKeyframe,
2792
+ timecode
2793
+ });
2794
+ position += remaining;
2795
+ }
2796
+ return frames;
2797
+ }
2798
+
2681
2799
  // ../rtp/src/srtp/const.ts
2682
2800
  var ProtectionProfileAes128CmHmacSha1_80 = 1;
2683
2801
  var ProtectionProfileAeadAes128Gcm = 7;
@@ -2745,6 +2863,16 @@ var RtcpHeader = class _RtcpHeader {
2745
2863
  constructor(props = {}) {
2746
2864
  Object.assign(this, props);
2747
2865
  }
2866
+ static serialize(type, count, payload, length) {
2867
+ const header = new _RtcpHeader({
2868
+ type,
2869
+ count,
2870
+ version: 2,
2871
+ length
2872
+ });
2873
+ const buf = header.serialize();
2874
+ return Buffer.concat([buf, payload]);
2875
+ }
2748
2876
  serialize() {
2749
2877
  const v_p_rc = new BitWriter(8);
2750
2878
  v_p_rc.set(2, 0, this.version);
@@ -2762,6 +2890,156 @@ var RtcpHeader = class _RtcpHeader {
2762
2890
  }
2763
2891
  };
2764
2892
 
2893
+ // ../rtp/src/rtcp/psfb/fullIntraRequest.ts
2894
+ var FullIntraRequest = class _FullIntraRequest {
2895
+ static count = 4;
2896
+ count = _FullIntraRequest.count;
2897
+ senderSsrc;
2898
+ mediaSsrc;
2899
+ fir = [];
2900
+ constructor(props = {}) {
2901
+ Object.assign(this, props);
2902
+ }
2903
+ get length() {
2904
+ return Math.floor(this.serialize().length / 4 - 1);
2905
+ }
2906
+ static deSerialize(data) {
2907
+ const [senderSsrc, mediaSsrc] = bufferReader(data, [4, 4]);
2908
+ const fir = [];
2909
+ for (let i = 8; i < data.length; i += 8) {
2910
+ fir.push({ ssrc: data.readUInt32BE(i), sequenceNumber: data[i + 4] });
2911
+ }
2912
+ return new _FullIntraRequest({ senderSsrc, mediaSsrc, fir });
2913
+ }
2914
+ serialize() {
2915
+ const ssrcs = bufferWriter([4, 4], [this.senderSsrc, this.mediaSsrc]);
2916
+ const fir = Buffer.alloc(this.fir.length * 8);
2917
+ this.fir.forEach(({ ssrc, sequenceNumber }, i) => {
2918
+ fir.writeUInt32BE(ssrc, i * 8);
2919
+ fir[i * 8 + 4] = sequenceNumber;
2920
+ });
2921
+ return Buffer.concat([ssrcs, fir]);
2922
+ }
2923
+ };
2924
+
2925
+ // ../rtp/src/rtcp/psfb/pictureLossIndication.ts
2926
+ var PictureLossIndication = class _PictureLossIndication {
2927
+ static count = 1;
2928
+ count = _PictureLossIndication.count;
2929
+ length = 2;
2930
+ senderSsrc;
2931
+ mediaSsrc;
2932
+ constructor(props = {}) {
2933
+ Object.assign(this, props);
2934
+ }
2935
+ static deSerialize(data) {
2936
+ const [senderSsrc, mediaSsrc] = bufferReader(data, [4, 4]);
2937
+ return new _PictureLossIndication({ senderSsrc, mediaSsrc });
2938
+ }
2939
+ serialize() {
2940
+ return bufferWriter([4, 4], [this.senderSsrc, this.mediaSsrc]);
2941
+ }
2942
+ };
2943
+
2944
+ // ../rtp/src/rtcp/psfb/remb.ts
2945
+ var ReceiverEstimatedMaxBitrate = class _ReceiverEstimatedMaxBitrate {
2946
+ static count = 15;
2947
+ length;
2948
+ count = _ReceiverEstimatedMaxBitrate.count;
2949
+ senderSsrc;
2950
+ mediaSsrc;
2951
+ uniqueID = "REMB";
2952
+ ssrcNum = 0;
2953
+ brExp;
2954
+ brMantissa;
2955
+ bitrate;
2956
+ ssrcFeedbacks = [];
2957
+ constructor(props = {}) {
2958
+ Object.assign(this, props);
2959
+ }
2960
+ static deSerialize(data) {
2961
+ const [senderSsrc, mediaSsrc, uniqueID, ssrcNum, e_m] = bufferReader(
2962
+ data,
2963
+ [4, 4, 4, 1, 1]
2964
+ );
2965
+ const brExp = getBit(e_m, 0, 6);
2966
+ const brMantissa = (getBit(e_m, 6, 2) << 16) + (data[14] << 8) + data[15];
2967
+ const bitrate = brExp > 46 ? 18446744073709551615n : BigInt(brMantissa) << BigInt(brExp);
2968
+ const ssrcFeedbacks = [];
2969
+ for (let i = 16; i < data.length; i += 4) {
2970
+ const feedback = data.slice(i).readUIntBE(0, 4);
2971
+ ssrcFeedbacks.push(feedback);
2972
+ }
2973
+ return new _ReceiverEstimatedMaxBitrate({
2974
+ senderSsrc,
2975
+ mediaSsrc,
2976
+ uniqueID: bufferWriter([4], [uniqueID]).toString(),
2977
+ ssrcNum,
2978
+ brExp,
2979
+ brMantissa,
2980
+ ssrcFeedbacks,
2981
+ bitrate
2982
+ });
2983
+ }
2984
+ serialize() {
2985
+ const constant = Buffer.concat([
2986
+ bufferWriter([4, 4], [this.senderSsrc, this.mediaSsrc]),
2987
+ Buffer.from(this.uniqueID),
2988
+ bufferWriter([1], [this.ssrcNum])
2989
+ ]);
2990
+ const writer = new BitWriter(24);
2991
+ writer.set(6, 0, this.brExp).set(18, 6, this.brMantissa);
2992
+ const feedbacks = Buffer.concat(
2993
+ this.ssrcFeedbacks.map((feedback) => bufferWriter([4], [feedback]))
2994
+ );
2995
+ const buf = Buffer.concat([
2996
+ constant,
2997
+ bufferWriter([3], [writer.value]),
2998
+ feedbacks
2999
+ ]);
3000
+ this.length = buf.length / 4;
3001
+ return buf;
3002
+ }
3003
+ };
3004
+
3005
+ // ../rtp/src/rtcp/psfb/index.ts
3006
+ var log3 = debug("werift-rtp: /rtcp/psfb/index");
3007
+ var RtcpPayloadSpecificFeedback = class _RtcpPayloadSpecificFeedback {
3008
+ static type = 206;
3009
+ type = _RtcpPayloadSpecificFeedback.type;
3010
+ feedback;
3011
+ constructor(props = {}) {
3012
+ Object.assign(this, props);
3013
+ }
3014
+ serialize() {
3015
+ const payload = this.feedback.serialize();
3016
+ return RtcpHeader.serialize(
3017
+ this.type,
3018
+ this.feedback.count,
3019
+ payload,
3020
+ this.feedback.length
3021
+ );
3022
+ }
3023
+ static deSerialize(data, header) {
3024
+ let feedback;
3025
+ switch (header.count) {
3026
+ case FullIntraRequest.count:
3027
+ feedback = FullIntraRequest.deSerialize(data);
3028
+ break;
3029
+ case PictureLossIndication.count:
3030
+ feedback = PictureLossIndication.deSerialize(data);
3031
+ break;
3032
+ case ReceiverEstimatedMaxBitrate.count:
3033
+ feedback = ReceiverEstimatedMaxBitrate.deSerialize(data);
3034
+ break;
3035
+ default:
3036
+ log3("unknown psfb packet", header.count);
3037
+ break;
3038
+ }
3039
+ return new _RtcpPayloadSpecificFeedback({ feedback });
3040
+ }
3041
+ };
3042
+
2765
3043
  // ../rtp/src/rtcp/rr.ts
2766
3044
  var RtcpRrPacket = class _RtcpRrPacket {
2767
3045
  ssrc = 0;
@@ -2777,7 +3055,7 @@ var RtcpRrPacket = class _RtcpRrPacket {
2777
3055
  payload,
2778
3056
  ...this.reports.map((report) => report.serialize())
2779
3057
  ]);
2780
- return RtcpPacketConverter.serialize(
3058
+ return RtcpHeader.serialize(
2781
3059
  _RtcpRrPacket.type,
2782
3060
  this.reports.length,
2783
3061
  payload,
@@ -2855,6 +3133,9 @@ var RtcpReceiverInfo = class _RtcpReceiverInfo {
2855
3133
  }
2856
3134
  };
2857
3135
 
3136
+ // ../rtp/src/rtcp/rtpfb/const.ts
3137
+ var RtcpTransportLayerFeedbackType = 205;
3138
+
2858
3139
  // ../rtp/src/rtcp/rtpfb/nack.ts
2859
3140
  var GenericNack = class _GenericNack {
2860
3141
  static count = 1;
@@ -2874,7 +3155,7 @@ var GenericNack = class _GenericNack {
2874
3155
  Object.assign(this, props);
2875
3156
  if (!this.header) {
2876
3157
  this.header = new RtcpHeader({
2877
- type: RtcpTransportLayerFeedback.type,
3158
+ type: RtcpTransportLayerFeedbackType,
2878
3159
  count: this.count,
2879
3160
  version: 2
2880
3161
  });
@@ -2926,7 +3207,7 @@ var GenericNack = class _GenericNack {
2926
3207
  };
2927
3208
 
2928
3209
  // ../rtp/src/rtcp/rtpfb/twcc.ts
2929
- var log3 = debug("werift/rtp/rtcp/rtpfb/twcc");
3210
+ var log4 = debug("werift/rtp/rtcp/rtpfb/twcc");
2930
3211
  var TransportWideCC = class _TransportWideCC {
2931
3212
  static count = 15;
2932
3213
  count = _TransportWideCC.count;
@@ -3070,7 +3351,7 @@ var TransportWideCC = class _TransportWideCC {
3070
3351
  try {
3071
3352
  return delta.serialize();
3072
3353
  } catch (error) {
3073
- log3(error?.message);
3354
+ log4(error?.message);
3074
3355
  return void 0;
3075
3356
  }
3076
3357
  }).filter((v) => v)
@@ -3147,19 +3428,19 @@ var StatusVectorChunk = class _StatusVectorChunk {
3147
3428
  const type = 1 /* TypeTCCStatusVectorChunk */;
3148
3429
  let symbolSize = getBit(data[0], 1, 1);
3149
3430
  const symbolList = [];
3150
- function range6(n, cb) {
3431
+ function range(n, cb) {
3151
3432
  for (let i = 0; i < n; i++) {
3152
3433
  cb(i);
3153
3434
  }
3154
3435
  }
3155
3436
  switch (symbolSize) {
3156
3437
  case 0:
3157
- range6(6, (i) => symbolList.push(getBit(data[0], 2 + i, 1)));
3158
- range6(8, (i) => symbolList.push(getBit(data[1], i, 1)));
3438
+ range(6, (i) => symbolList.push(getBit(data[0], 2 + i, 1)));
3439
+ range(8, (i) => symbolList.push(getBit(data[1], i, 1)));
3159
3440
  break;
3160
3441
  case 1:
3161
- range6(3, (i) => symbolList.push(getBit(data[0], 2 + i * 2, 2)));
3162
- range6(4, (i) => symbolList.push(getBit(data[1], i * 2, 2)));
3442
+ range(3, (i) => symbolList.push(getBit(data[0], 2 + i * 2, 2)));
3443
+ range(4, (i) => symbolList.push(getBit(data[1], i * 2, 2)));
3163
3444
  break;
3164
3445
  default:
3165
3446
  symbolSize = (getBit(data[0], 2, 6) << 8) + data[1];
@@ -3253,9 +3534,9 @@ var PacketResult = class {
3253
3534
  };
3254
3535
 
3255
3536
  // ../rtp/src/rtcp/rtpfb/index.ts
3256
- var log4 = debug("werift-rtp:packages/rtp/rtcp/rtpfb/index");
3537
+ var log5 = debug("werift-rtp:packages/rtp/rtcp/rtpfb/index");
3257
3538
  var RtcpTransportLayerFeedback = class _RtcpTransportLayerFeedback {
3258
- static type = 205;
3539
+ static type = RtcpTransportLayerFeedbackType;
3259
3540
  type = _RtcpTransportLayerFeedback.type;
3260
3541
  feedback;
3261
3542
  header;
@@ -3276,7 +3557,7 @@ var RtcpTransportLayerFeedback = class _RtcpTransportLayerFeedback {
3276
3557
  feedback = TransportWideCC.deSerialize(data, header);
3277
3558
  break;
3278
3559
  default:
3279
- log4("unknown rtpfb packet", header.count);
3560
+ log5("unknown rtpfb packet", header.count);
3280
3561
  break;
3281
3562
  }
3282
3563
  return new _RtcpTransportLayerFeedback({ feedback, header });
@@ -3300,7 +3581,7 @@ var RtcpSourceDescriptionPacket = class _RtcpSourceDescriptionPacket {
3300
3581
  let payload = Buffer.concat(this.chunks.map((chunk) => chunk.serialize()));
3301
3582
  while (payload.length % 4)
3302
3583
  payload = Buffer.concat([payload, Buffer.from([0])]);
3303
- return RtcpPacketConverter.serialize(
3584
+ return RtcpHeader.serialize(
3304
3585
  this.type,
3305
3586
  this.chunks.length,
3306
3587
  payload,
@@ -3406,7 +3687,7 @@ var RtcpSrPacket = class _RtcpSrPacket {
3406
3687
  payload,
3407
3688
  ...this.reports.map((report) => report.serialize())
3408
3689
  ]);
3409
- return RtcpPacketConverter.serialize(
3690
+ return RtcpHeader.serialize(
3410
3691
  _RtcpSrPacket.type,
3411
3692
  this.reports.length,
3412
3693
  payload,
@@ -3446,238 +3727,78 @@ var RtcpSenderInfo = class _RtcpSenderInfo {
3446
3727
  return bufferWriter(
3447
3728
  [8, 4, 4, 4],
3448
3729
  [this.ntpTimestamp, this.rtpTimestamp, this.packetCount, this.octetCount]
3449
- );
3450
- }
3451
- static deSerialize(data) {
3452
- const [ntpTimestamp, rtpTimestamp, packetCount, octetCount] = bufferReader(
3453
- data,
3454
- [8, 4, 4, 4]
3455
- );
3456
- return new _RtcpSenderInfo({
3457
- ntpTimestamp,
3458
- rtpTimestamp,
3459
- packetCount,
3460
- octetCount
3461
- });
3462
- }
3463
- };
3464
- var ntpTime2Sec = (ntp) => {
3465
- const [ntpSec, ntpMsec] = bufferReader(bufferWriter([8], [ntp]), [4, 4]);
3466
- return Number(`${ntpSec}.${ntpMsec}`);
3467
- };
3468
-
3469
- // ../rtp/src/rtcp/rtcp.ts
3470
- var log5 = debug("werift-rtp:packages/rtp/src/rtcp/rtcp.ts");
3471
- var RtcpPacketConverter = class {
3472
- static serialize(type, count, payload, length) {
3473
- const header = new RtcpHeader({
3474
- type,
3475
- count,
3476
- version: 2,
3477
- length
3478
- });
3479
- const buf = header.serialize();
3480
- return Buffer.concat([buf, payload]);
3481
- }
3482
- static deSerialize(data) {
3483
- let pos = 0;
3484
- const packets = [];
3485
- while (pos < data.length) {
3486
- const header = RtcpHeader.deSerialize(
3487
- data.subarray(pos, pos + RTCP_HEADER_SIZE)
3488
- );
3489
- pos += RTCP_HEADER_SIZE;
3490
- let payload = data.subarray(pos);
3491
- pos += header.length * 4;
3492
- if (header.padding) {
3493
- payload = payload.subarray(0, payload.length - payload.subarray(-1)[0]);
3494
- }
3495
- try {
3496
- switch (header.type) {
3497
- case RtcpSrPacket.type:
3498
- packets.push(RtcpSrPacket.deSerialize(payload, header.count));
3499
- break;
3500
- case RtcpRrPacket.type:
3501
- packets.push(RtcpRrPacket.deSerialize(payload, header.count));
3502
- break;
3503
- case RtcpSourceDescriptionPacket.type:
3504
- packets.push(
3505
- RtcpSourceDescriptionPacket.deSerialize(payload, header)
3506
- );
3507
- break;
3508
- case RtcpTransportLayerFeedback.type:
3509
- packets.push(
3510
- RtcpTransportLayerFeedback.deSerialize(payload, header)
3511
- );
3512
- break;
3513
- case RtcpPayloadSpecificFeedback.type:
3514
- packets.push(
3515
- RtcpPayloadSpecificFeedback.deSerialize(payload, header)
3516
- );
3517
- break;
3518
- default:
3519
- break;
3520
- }
3521
- } catch (error) {
3522
- log5("deSerialize RTCP", error);
3523
- }
3524
- }
3525
- return packets;
3526
- }
3527
- };
3528
- function isRtcp(buf) {
3529
- return buf.length >= 2 && buf[1] >= 192 && buf[1] <= 208;
3530
- }
3531
-
3532
- // ../rtp/src/rtcp/psfb/fullIntraRequest.ts
3533
- var FullIntraRequest = class _FullIntraRequest {
3534
- static count = 4;
3535
- count = _FullIntraRequest.count;
3536
- senderSsrc;
3537
- mediaSsrc;
3538
- fir = [];
3539
- constructor(props = {}) {
3540
- Object.assign(this, props);
3541
- }
3542
- get length() {
3543
- return Math.floor(this.serialize().length / 4 - 1);
3544
- }
3545
- static deSerialize(data) {
3546
- const [senderSsrc, mediaSsrc] = bufferReader(data, [4, 4]);
3547
- const fir = [];
3548
- for (let i = 8; i < data.length; i += 8) {
3549
- fir.push({ ssrc: data.readUInt32BE(i), sequenceNumber: data[i + 4] });
3550
- }
3551
- return new _FullIntraRequest({ senderSsrc, mediaSsrc, fir });
3552
- }
3553
- serialize() {
3554
- const ssrcs = bufferWriter([4, 4], [this.senderSsrc, this.mediaSsrc]);
3555
- const fir = Buffer.alloc(this.fir.length * 8);
3556
- this.fir.forEach(({ ssrc, sequenceNumber }, i) => {
3557
- fir.writeUInt32BE(ssrc, i * 8);
3558
- fir[i * 8 + 4] = sequenceNumber;
3559
- });
3560
- return Buffer.concat([ssrcs, fir]);
3561
- }
3562
- };
3563
-
3564
- // ../rtp/src/rtcp/psfb/pictureLossIndication.ts
3565
- var PictureLossIndication = class _PictureLossIndication {
3566
- static count = 1;
3567
- count = _PictureLossIndication.count;
3568
- length = 2;
3569
- senderSsrc;
3570
- mediaSsrc;
3571
- constructor(props = {}) {
3572
- Object.assign(this, props);
3573
- }
3574
- static deSerialize(data) {
3575
- const [senderSsrc, mediaSsrc] = bufferReader(data, [4, 4]);
3576
- return new _PictureLossIndication({ senderSsrc, mediaSsrc });
3577
- }
3578
- serialize() {
3579
- return bufferWriter([4, 4], [this.senderSsrc, this.mediaSsrc]);
3580
- }
3581
- };
3582
-
3583
- // ../rtp/src/rtcp/psfb/remb.ts
3584
- var ReceiverEstimatedMaxBitrate = class _ReceiverEstimatedMaxBitrate {
3585
- static count = 15;
3586
- length;
3587
- count = _ReceiverEstimatedMaxBitrate.count;
3588
- senderSsrc;
3589
- mediaSsrc;
3590
- uniqueID = "REMB";
3591
- ssrcNum = 0;
3592
- brExp;
3593
- brMantissa;
3594
- bitrate;
3595
- ssrcFeedbacks = [];
3596
- constructor(props = {}) {
3597
- Object.assign(this, props);
3598
- }
3599
- static deSerialize(data) {
3600
- const [senderSsrc, mediaSsrc, uniqueID, ssrcNum, e_m] = bufferReader(
3601
- data,
3602
- [4, 4, 4, 1, 1]
3603
- );
3604
- const brExp = getBit(e_m, 0, 6);
3605
- const brMantissa = (getBit(e_m, 6, 2) << 16) + (data[14] << 8) + data[15];
3606
- const bitrate = brExp > 46 ? 18446744073709551615n : BigInt(brMantissa) << BigInt(brExp);
3607
- const ssrcFeedbacks = [];
3608
- for (let i = 16; i < data.length; i += 4) {
3609
- const feedback = data.slice(i).readUIntBE(0, 4);
3610
- ssrcFeedbacks.push(feedback);
3611
- }
3612
- return new _ReceiverEstimatedMaxBitrate({
3613
- senderSsrc,
3614
- mediaSsrc,
3615
- uniqueID: bufferWriter([4], [uniqueID]).toString(),
3616
- ssrcNum,
3617
- brExp,
3618
- brMantissa,
3619
- ssrcFeedbacks,
3620
- bitrate
3621
- });
3622
- }
3623
- serialize() {
3624
- const constant = Buffer.concat([
3625
- bufferWriter([4, 4], [this.senderSsrc, this.mediaSsrc]),
3626
- Buffer.from(this.uniqueID),
3627
- bufferWriter([1], [this.ssrcNum])
3628
- ]);
3629
- const writer = new BitWriter(24);
3630
- writer.set(6, 0, this.brExp).set(18, 6, this.brMantissa);
3631
- const feedbacks = Buffer.concat(
3632
- this.ssrcFeedbacks.map((feedback) => bufferWriter([4], [feedback]))
3633
- );
3634
- const buf = Buffer.concat([
3635
- constant,
3636
- bufferWriter([3], [writer.value]),
3637
- feedbacks
3638
- ]);
3639
- this.length = buf.length / 4;
3640
- return buf;
3641
- }
3642
- };
3643
-
3644
- // ../rtp/src/rtcp/psfb/index.ts
3645
- var log6 = debug("werift-rtp: /rtcp/psfb/index");
3646
- var RtcpPayloadSpecificFeedback = class _RtcpPayloadSpecificFeedback {
3647
- static type = 206;
3648
- type = _RtcpPayloadSpecificFeedback.type;
3649
- feedback;
3650
- constructor(props = {}) {
3651
- Object.assign(this, props);
3730
+ );
3652
3731
  }
3653
- serialize() {
3654
- const payload = this.feedback.serialize();
3655
- return RtcpPacketConverter.serialize(
3656
- this.type,
3657
- this.feedback.count,
3658
- payload,
3659
- this.feedback.length
3732
+ static deSerialize(data) {
3733
+ const [ntpTimestamp, rtpTimestamp, packetCount, octetCount] = bufferReader(
3734
+ data,
3735
+ [8, 4, 4, 4]
3660
3736
  );
3737
+ return new _RtcpSenderInfo({
3738
+ ntpTimestamp,
3739
+ rtpTimestamp,
3740
+ packetCount,
3741
+ octetCount
3742
+ });
3661
3743
  }
3662
- static deSerialize(data, header) {
3663
- let feedback;
3664
- switch (header.count) {
3665
- case FullIntraRequest.count:
3666
- feedback = FullIntraRequest.deSerialize(data);
3667
- break;
3668
- case PictureLossIndication.count:
3669
- feedback = PictureLossIndication.deSerialize(data);
3670
- break;
3671
- case ReceiverEstimatedMaxBitrate.count:
3672
- feedback = ReceiverEstimatedMaxBitrate.deSerialize(data);
3673
- break;
3674
- default:
3675
- log6("unknown psfb packet", header.count);
3676
- break;
3744
+ };
3745
+ var ntpTime2Sec = (ntp) => {
3746
+ const [ntpSec, ntpMsec] = bufferReader(bufferWriter([8], [ntp]), [4, 4]);
3747
+ return Number(`${ntpSec}.${ntpMsec}`);
3748
+ };
3749
+
3750
+ // ../rtp/src/rtcp/rtcp.ts
3751
+ var log6 = debug("werift-rtp:packages/rtp/src/rtcp/rtcp.ts");
3752
+ var RtcpPacketConverter = class {
3753
+ static deSerialize(data) {
3754
+ let pos = 0;
3755
+ const packets = [];
3756
+ while (pos < data.length) {
3757
+ const header = RtcpHeader.deSerialize(
3758
+ data.subarray(pos, pos + RTCP_HEADER_SIZE)
3759
+ );
3760
+ pos += RTCP_HEADER_SIZE;
3761
+ let payload = data.subarray(pos);
3762
+ pos += header.length * 4;
3763
+ if (header.padding) {
3764
+ payload = payload.subarray(0, payload.length - payload.subarray(-1)[0]);
3765
+ }
3766
+ try {
3767
+ switch (header.type) {
3768
+ case RtcpSrPacket.type:
3769
+ packets.push(RtcpSrPacket.deSerialize(payload, header.count));
3770
+ break;
3771
+ case RtcpRrPacket.type:
3772
+ packets.push(RtcpRrPacket.deSerialize(payload, header.count));
3773
+ break;
3774
+ case RtcpSourceDescriptionPacket.type:
3775
+ packets.push(
3776
+ RtcpSourceDescriptionPacket.deSerialize(payload, header)
3777
+ );
3778
+ break;
3779
+ case RtcpTransportLayerFeedback.type:
3780
+ packets.push(
3781
+ RtcpTransportLayerFeedback.deSerialize(payload, header)
3782
+ );
3783
+ break;
3784
+ case RtcpPayloadSpecificFeedback.type:
3785
+ packets.push(
3786
+ RtcpPayloadSpecificFeedback.deSerialize(payload, header)
3787
+ );
3788
+ break;
3789
+ default:
3790
+ break;
3791
+ }
3792
+ } catch (error) {
3793
+ log6("deSerialize RTCP", error);
3794
+ }
3677
3795
  }
3678
- return new _RtcpPayloadSpecificFeedback({ feedback });
3796
+ return packets;
3679
3797
  }
3680
3798
  };
3799
+ function isRtcp(buf) {
3800
+ return buf.length >= 2 && buf[1] >= 192 && buf[1] <= 208;
3801
+ }
3681
3802
 
3682
3803
  // ../rtp/src/rtp/headerExtension.ts
3683
3804
  var RTP_EXTENSION_URI = {
@@ -4022,6 +4143,7 @@ var RtpHeader = class _RtpHeader {
4022
4143
  h.extensionLength = extensionLength;
4023
4144
  currOffset += 2;
4024
4145
  switch (h.extensionProfile) {
4146
+ // RFC 8285 RTP One Byte Header Extension
4025
4147
  case ExtensionProfiles.OneByte:
4026
4148
  {
4027
4149
  const end = currOffset + extensionLength;
@@ -4045,6 +4167,7 @@ var RtpHeader = class _RtpHeader {
4045
4167
  }
4046
4168
  }
4047
4169
  break;
4170
+ // RFC 8285 RTP Two Byte Header Extension
4048
4171
  case ExtensionProfiles.TwoByte:
4049
4172
  {
4050
4173
  const end = currOffset + extensionLength;
@@ -4589,7 +4712,7 @@ var Context = class {
4589
4712
  sessionSalt = Buffer.concat([sessionSalt, Buffer.from([0, 0])]);
4590
4713
  const block = new AES.AES(this.masterKey);
4591
4714
  sessionSalt = Buffer.from(block.encrypt(sessionSalt));
4592
- return sessionSalt.slice(0, 14);
4715
+ return sessionSalt.subarray(0, 14);
4593
4716
  }
4594
4717
  generateSessionAuthTag(label) {
4595
4718
  const sessionAuthTag = Buffer.from(this.masterSalt);
@@ -4605,12 +4728,18 @@ var Context = class {
4605
4728
  for (let i = labelAndIndexOverKdr.length - 1, j = sessionAuthTag.length - 1; i >= 0; i--, j--) {
4606
4729
  sessionAuthTag[j] = sessionAuthTag[j] ^ labelAndIndexOverKdr[i];
4607
4730
  }
4608
- let firstRun = Buffer.concat([sessionAuthTag, Buffer.from([0, 0])]);
4609
- let secondRun = Buffer.concat([sessionAuthTag, Buffer.from([0, 1])]);
4731
+ let firstRun = Buffer.concat([
4732
+ sessionAuthTag,
4733
+ Buffer.from([0, 0])
4734
+ ]);
4735
+ let secondRun = Buffer.concat([
4736
+ sessionAuthTag,
4737
+ Buffer.from([0, 1])
4738
+ ]);
4610
4739
  const block = new AES.AES(this.masterKey);
4611
4740
  firstRun = Buffer.from(block.encrypt(firstRun));
4612
4741
  secondRun = Buffer.from(block.encrypt(secondRun));
4613
- return Buffer.concat([firstRun, secondRun.slice(0, 4)]);
4742
+ return Buffer.concat([firstRun, secondRun.subarray(0, 4)]);
4614
4743
  }
4615
4744
  getSrtpSsrcState(ssrc) {
4616
4745
  let s = this.srtpSSRCStates[ssrc];
@@ -5023,7 +5152,7 @@ var DtxBase = class {
5023
5152
  this.ptime = ptime;
5024
5153
  this.dummyPacket = dummyPacket;
5025
5154
  }
5026
- id = randomUUID();
5155
+ id = randomUUID().toString();
5027
5156
  previousTimestamp;
5028
5157
  fillCount = 0;
5029
5158
  internalStats = {};
@@ -5280,7 +5409,7 @@ var LipsyncBase = class {
5280
5409
  this.videoBuffer = [...new Array(this.bufferLength)].map(() => []);
5281
5410
  this.ptime = this.options.ptime ?? 20;
5282
5411
  }
5283
- id = randomUUID2();
5412
+ id = randomUUID2().toString();
5284
5413
  bufferLength;
5285
5414
  /**ms */
5286
5415
  baseTime;
@@ -5395,10 +5524,10 @@ var LipsyncBase = class {
5395
5524
  if (frame.time < this.lastCommittedTime) {
5396
5525
  return;
5397
5526
  }
5398
- const now2 = Date.now();
5527
+ const now = Date.now();
5399
5528
  const gap = 5e3;
5400
5529
  const lastCommittedElapsed = frame.time - this.lastCommittedTime;
5401
- const lastFrameReceivedElapsed = now2 - this.lastFrameReceivedAt;
5530
+ const lastFrameReceivedElapsed = now - this.lastFrameReceivedAt;
5402
5531
  if (gap < lastFrameReceivedElapsed && lastCommittedElapsed < gap) {
5403
5532
  this.internalStats["invalidFrameTime"] = {
5404
5533
  count: (this.internalStats["invalidFrameTime"]?.count ?? 0) + 1,
@@ -5408,7 +5537,7 @@ var LipsyncBase = class {
5408
5537
  };
5409
5538
  return;
5410
5539
  }
5411
- this.lastFrameReceivedAt = now2;
5540
+ this.lastFrameReceivedAt = now;
5412
5541
  const elapsed = frame.time - this.baseTime;
5413
5542
  const index = int(elapsed / this.bufferDuration) % this.bufferLength;
5414
5543
  buffer2[index].push({
@@ -5417,9 +5546,9 @@ var LipsyncBase = class {
5417
5546
  });
5418
5547
  const diff = frame.time - this.lastExecutionTime;
5419
5548
  if (diff >= this.interval) {
5420
- const times2 = int(diff / this.bufferDuration) - 1;
5549
+ const times = int(diff / this.bufferDuration) - 1;
5421
5550
  this.lastExecutionTime = this.currentTimestamp;
5422
- for (let i = 0; i < times2; i++) {
5551
+ for (let i = 0; i < times; i++) {
5423
5552
  this.executeTask();
5424
5553
  this.lastExecutionTime += this.bufferDuration;
5425
5554
  }
@@ -5508,7 +5637,9 @@ var MP4Base = class {
5508
5637
  if (!this.container.audioTrack) {
5509
5638
  this.container.write({
5510
5639
  codec: track.codec,
5511
- description: buffer2ArrayBuffer(OpusRtpPayload.createCodecPrivate()),
5640
+ description: buffer2ArrayBuffer(
5641
+ OpusRtpPayload.createCodecPrivate()
5642
+ ),
5512
5643
  numberOfChannels: 2,
5513
5644
  sampleRate: track.clockRate,
5514
5645
  track: "audio"
@@ -5644,7 +5775,7 @@ var MuteHandlerBase = class {
5644
5775
  this.bufferLength = this.props.bufferLength * 2;
5645
5776
  this.buffer = [...new Array(this.bufferLength)].map(() => []);
5646
5777
  }
5647
- id = randomUUID3();
5778
+ id = randomUUID3().toString();
5648
5779
  buffer;
5649
5780
  index = 0;
5650
5781
  ended = false;
@@ -5713,10 +5844,10 @@ var MuteHandlerBase = class {
5713
5844
  if (frame.time < this.lastCommittedTime) {
5714
5845
  return [];
5715
5846
  }
5716
- const now2 = Date.now();
5847
+ const now = Date.now();
5717
5848
  const gap = 5e3;
5718
5849
  const lastCommittedElapsed = frame.time - this.lastCommittedTime;
5719
- const lastFrameReceivedElapsed = now2 - this.lastFrameReceivedAt;
5850
+ const lastFrameReceivedElapsed = now - this.lastFrameReceivedAt;
5720
5851
  if (gap < lastFrameReceivedElapsed && lastCommittedElapsed < gap) {
5721
5852
  this.internalStats["invalidFrameTime"] = {
5722
5853
  count: (this.internalStats["invalidFrameTime"]?.count ?? 0) + 1,
@@ -5726,15 +5857,15 @@ var MuteHandlerBase = class {
5726
5857
  };
5727
5858
  return [];
5728
5859
  }
5729
- this.lastFrameReceivedAt = now2;
5860
+ this.lastFrameReceivedAt = now;
5730
5861
  const elapsed = frame.time - this.baseTime;
5731
5862
  const index = int(elapsed / this.bufferDuration) % this.bufferLength;
5732
5863
  this.buffer[index].push(frame);
5733
5864
  const lastExecution = frame.time - this.lastExecutionTime;
5734
5865
  if (lastExecution >= this.interval) {
5735
- const times2 = int(lastExecution / this.bufferDuration) - 1;
5866
+ const times = int(lastExecution / this.bufferDuration) - 1;
5736
5867
  this.lastExecutionTime = this.currentTimestamp;
5737
- for (let i = 0; i < times2; i++) {
5868
+ for (let i = 0; i < times; i++) {
5738
5869
  this.executeTask();
5739
5870
  this.lastExecutionTime += this.bufferDuration;
5740
5871
  }
@@ -6210,7 +6341,7 @@ var NtpTimeBase = class {
6210
6341
  constructor(clockRate) {
6211
6342
  this.clockRate = clockRate;
6212
6343
  }
6213
- id = randomUUID4();
6344
+ id = randomUUID4().toString();
6214
6345
  baseNtpTimestamp;
6215
6346
  baseRtpTimestamp;
6216
6347
  latestNtpTimestamp;
@@ -6601,16 +6732,15 @@ var WebmStream = class extends WebmBase {
6601
6732
  };
6602
6733
 
6603
6734
  // src/nonstandard/navigator.ts
6604
- import { randomBytes as randomBytes4 } from "crypto";
6735
+ import { randomBytes as randomBytes5 } from "crypto";
6605
6736
  import { createSocket as createSocket3 } from "dgram";
6606
- import { jspack as jspack11 } from "@shinyoshiaki/jspack";
6737
+ import { jspack as jspack8 } from "@shinyoshiaki/jspack";
6607
6738
 
6608
6739
  // ../dtls/src/context/cipher.ts
6609
- import nodeCrypto, { createSign } from "crypto";
6740
+ import { createSign, randomBytes as randomBytes3, webcrypto } from "crypto";
6610
6741
  import { Certificate, PrivateKey } from "@fidm/x509";
6611
6742
  import * as x509 from "@peculiar/x509";
6612
- import { decode, encode, types as types2 } from "@shinyoshiaki/binary-data";
6613
- import { addYears } from "date-fns";
6743
+ import { encode, types } from "@shinyoshiaki/binary-data";
6614
6744
 
6615
6745
  // ../dtls/src/cipher/const.ts
6616
6746
  var SignatureAlgorithm = {
@@ -6789,22 +6919,8 @@ var AbstractCipher = class {
6789
6919
  }
6790
6920
  };
6791
6921
 
6792
- // ../dtls/src/handshake/binary.ts
6793
- import { types } from "@shinyoshiaki/binary-data";
6794
- var { uint16be, uint24be, buffer, array, uint8, string: string2 } = types;
6795
- var Extension = {
6796
- type: uint16be,
6797
- data: buffer(uint16be)
6798
- };
6799
- var ExtensionList = array(Extension, uint16be, "bytes");
6800
- var ASN11Cert = buffer(uint24be);
6801
- var ClientCertificateType = uint8;
6802
- var DistinguishedName = string2(uint16be);
6803
- var SignatureHashAlgorithm = { hash: uint8, signature: uint8 };
6804
- var ProtocolVersion = { major: uint8, minor: uint8 };
6805
-
6806
6922
  // ../dtls/src/context/cipher.ts
6807
- var crypto = nodeCrypto.webcrypto;
6923
+ var crypto = webcrypto;
6808
6924
  x509.cryptoProvider.set(crypto);
6809
6925
  var CipherContext = class {
6810
6926
  constructor(sessionType, certPem, keyPem, signatureHashAlgorithm) {
@@ -6880,10 +6996,10 @@ var CipherContext = class {
6880
6996
  "verify"
6881
6997
  ]);
6882
6998
  const cert = await x509.X509CertificateGenerator.createSelfSigned({
6883
- serialNumber: nodeCrypto.randomBytes(8).toString("hex"),
6999
+ serialNumber: randomBytes3(8).toString("hex"),
6884
7000
  name: "C=AU, ST=Some-State, O=Internet Widgits Pty Ltd",
6885
7001
  notBefore: /* @__PURE__ */ new Date(),
6886
- notAfter: addYears(Date.now(), 10),
7002
+ notAfter: new Date(Date.now() + 10 * 365 * 24 * 60 * 60 * 1e3),
6887
7003
  signingAlgorithm: alg,
6888
7004
  keys
6889
7005
  });
@@ -6896,12 +7012,10 @@ var CipherContext = class {
6896
7012
  };
6897
7013
  encryptPacket(pkt) {
6898
7014
  const header = pkt.recordLayerHeader;
7015
+ const version = header.protocolVersion.major << 8 | header.protocolVersion.minor;
6899
7016
  const enc = this.cipher.encrypt(this.sessionType, pkt.fragment, {
6900
7017
  type: header.contentType,
6901
- version: decode(
6902
- Buffer.from(encode(header.protocolVersion, ProtocolVersion).slice()),
6903
- { version: types2.uint16be }
6904
- ).version,
7018
+ version,
6905
7019
  epoch: header.epoch,
6906
7020
  sequenceNumber: header.sequenceNumber
6907
7021
  });
@@ -6911,12 +7025,10 @@ var CipherContext = class {
6911
7025
  }
6912
7026
  decryptPacket(pkt) {
6913
7027
  const header = pkt.recordLayerHeader;
7028
+ const version = header.protocolVersion.major << 8 | header.protocolVersion.minor;
6914
7029
  const dec = this.cipher.decrypt(this.sessionType, pkt.fragment, {
6915
7030
  type: header.contentType,
6916
- version: decode(
6917
- Buffer.from(encode(header.protocolVersion, ProtocolVersion).slice()),
6918
- { version: types2.uint16be }
6919
- ).version,
7031
+ version,
6920
7032
  epoch: header.epoch,
6921
7033
  sequenceNumber: header.sequenceNumber
6922
7034
  });
@@ -6960,7 +7072,7 @@ var CipherContext = class {
6960
7072
  curve: namedCurve,
6961
7073
  len: publicKey.length
6962
7074
  },
6963
- { type: types2.uint8, curve: types2.uint16be, len: types2.uint8 }
7075
+ { type: types.uint8, curve: types.uint16be, len: types.uint8 }
6964
7076
  ).slice()
6965
7077
  );
6966
7078
  return Buffer.concat([clientRandom, serverRandom, serverParams, publicKey]);
@@ -6978,10 +7090,10 @@ var SrtpContext2 = class {
6978
7090
  };
6979
7091
 
6980
7092
  // ../dtls/src/handshake/message/client/hello.ts
6981
- import { decode as decode4, encode as encode4, types as types5 } from "@shinyoshiaki/binary-data";
7093
+ import { decode as decode3, encode as encode4, types as types5 } from "@shinyoshiaki/binary-data";
6982
7094
 
6983
7095
  // ../dtls/src/record/message/fragment.ts
6984
- import { decode as decode2, encode as encode2, types as types3 } from "@shinyoshiaki/binary-data";
7096
+ import { decode, encode as encode2, types as types2 } from "@shinyoshiaki/binary-data";
6985
7097
 
6986
7098
  // ../dtls/src/helper.ts
6987
7099
  var dumpBuffer2 = (data) => "0x" + data.toString("hex").replace(/(.)(.)/g, "$1$2 ").split(" ").filter((s) => s != void 0 && s.length > 0).join(",0x");
@@ -7006,12 +7118,12 @@ var FragmentedHandshake = class _FragmentedHandshake {
7006
7118
  this.fragment = fragment;
7007
7119
  }
7008
7120
  static spec = {
7009
- msg_type: types3.uint8,
7010
- length: types3.uint24be,
7011
- message_seq: types3.uint16be,
7012
- fragment_offset: types3.uint24be,
7013
- fragment_length: types3.uint24be,
7014
- fragment: types3.buffer((context) => context.current.fragment_length)
7121
+ msg_type: types2.uint8,
7122
+ length: types2.uint24be,
7123
+ message_seq: types2.uint16be,
7124
+ fragment_offset: types2.uint24be,
7125
+ fragment_length: types2.uint24be,
7126
+ fragment: types2.buffer((context) => context.current.fragment_length)
7015
7127
  };
7016
7128
  get summary() {
7017
7129
  return getObjectSummary(this);
@@ -7028,7 +7140,7 @@ var FragmentedHandshake = class _FragmentedHandshake {
7028
7140
  }
7029
7141
  static deSerialize(buf) {
7030
7142
  return new _FragmentedHandshake(
7031
- ...Object.values(decode2(buf, _FragmentedHandshake.spec))
7143
+ ...Object.values(decode(buf, _FragmentedHandshake.spec))
7032
7144
  );
7033
7145
  }
7034
7146
  serialize() {
@@ -7105,11 +7217,25 @@ var FragmentedHandshake = class _FragmentedHandshake {
7105
7217
  }
7106
7218
  };
7107
7219
 
7220
+ // ../dtls/src/handshake/binary.ts
7221
+ import { types as types3 } from "@shinyoshiaki/binary-data";
7222
+ var { uint16be, uint24be, buffer, array, uint8, string: string2 } = types3;
7223
+ var Extension = {
7224
+ type: uint16be,
7225
+ data: buffer(uint16be)
7226
+ };
7227
+ var ExtensionList = array(Extension, uint16be, "bytes");
7228
+ var ASN11Cert = buffer(uint24be);
7229
+ var ClientCertificateType = uint8;
7230
+ var DistinguishedName = string2(uint16be);
7231
+ var SignatureHashAlgorithm = { hash: uint8, signature: uint8 };
7232
+ var ProtocolVersion = { major: uint8, minor: uint8 };
7233
+
7108
7234
  // ../dtls/src/handshake/random.ts
7109
- import { randomBytes as randomBytes3 } from "crypto";
7110
- import { decode as decode3, encode as encode3, types as types4 } from "@shinyoshiaki/binary-data";
7235
+ import { randomBytes as randomBytes4 } from "crypto";
7236
+ import { decode as decode2, encode as encode3, types as types4 } from "@shinyoshiaki/binary-data";
7111
7237
  var DtlsRandom = class _DtlsRandom {
7112
- constructor(gmt_unix_time = Math.floor(Date.now() / 1e3), random_bytes = randomBytes3(28)) {
7238
+ constructor(gmt_unix_time = Math.floor(Date.now() / 1e3), random_bytes = randomBytes4(28)) {
7113
7239
  this.gmt_unix_time = gmt_unix_time;
7114
7240
  this.random_bytes = random_bytes;
7115
7241
  }
@@ -7119,7 +7245,7 @@ var DtlsRandom = class _DtlsRandom {
7119
7245
  };
7120
7246
  static deSerialize(buf) {
7121
7247
  return new _DtlsRandom(
7122
- ...Object.values(decode3(buf, _DtlsRandom.spec))
7248
+ ...Object.values(decode2(buf, _DtlsRandom.spec))
7123
7249
  );
7124
7250
  }
7125
7251
  static from(spec) {
@@ -7166,7 +7292,7 @@ var ClientHello = class _ClientHello {
7166
7292
  }
7167
7293
  static deSerialize(buf) {
7168
7294
  return new _ClientHello(
7169
- ...Object.values(decode4(buf, _ClientHello.spec))
7295
+ ...Object.values(decode3(buf, _ClientHello.spec))
7170
7296
  );
7171
7297
  }
7172
7298
  serialize() {
@@ -7186,45 +7312,8 @@ var ClientHello = class _ClientHello {
7186
7312
  }
7187
7313
  };
7188
7314
 
7189
- // ../dtls/src/record/message/plaintext.ts
7190
- import { decode as decode6, encode as encode6, types as types7 } from "@shinyoshiaki/binary-data";
7191
-
7192
7315
  // ../dtls/src/record/message/header.ts
7193
- import { decode as decode5, encode as encode5, types as types6 } from "@shinyoshiaki/binary-data";
7194
- var DtlsPlaintextHeader = class _DtlsPlaintextHeader {
7195
- constructor(contentType, protocolVersion, epoch, sequenceNumber, contentLen) {
7196
- this.contentType = contentType;
7197
- this.protocolVersion = protocolVersion;
7198
- this.epoch = epoch;
7199
- this.sequenceNumber = sequenceNumber;
7200
- this.contentLen = contentLen;
7201
- }
7202
- static spec = {
7203
- contentType: types6.uint8,
7204
- protocolVersion: ProtocolVersion,
7205
- epoch: types6.uint16be,
7206
- sequenceNumber: types6.uint48be,
7207
- contentLen: types6.uint16be
7208
- };
7209
- static createEmpty() {
7210
- return new _DtlsPlaintextHeader(
7211
- void 0,
7212
- void 0,
7213
- void 0,
7214
- void 0,
7215
- void 0
7216
- );
7217
- }
7218
- static deSerialize(buf) {
7219
- return new _DtlsPlaintextHeader(
7220
- ...Object.values(decode5(buf, _DtlsPlaintextHeader.spec))
7221
- );
7222
- }
7223
- serialize() {
7224
- const res = encode5(this, _DtlsPlaintextHeader.spec).slice();
7225
- return Buffer.from(res);
7226
- }
7227
- };
7316
+ import { decode as decode4, encode as encode5, types as types6 } from "@shinyoshiaki/binary-data";
7228
7317
  var MACHeader = class _MACHeader {
7229
7318
  constructor(epoch, sequenceNumber, contentType, protocolVersion, contentLen) {
7230
7319
  this.epoch = epoch;
@@ -7251,7 +7340,7 @@ var MACHeader = class _MACHeader {
7251
7340
  }
7252
7341
  static deSerialize(buf) {
7253
7342
  return new _MACHeader(
7254
- ...Object.values(decode5(buf, _MACHeader.spec))
7343
+ ...Object.values(decode4(buf, _MACHeader.spec))
7255
7344
  );
7256
7345
  }
7257
7346
  serialize() {
@@ -7260,48 +7349,6 @@ var MACHeader = class _MACHeader {
7260
7349
  }
7261
7350
  };
7262
7351
 
7263
- // ../dtls/src/record/message/plaintext.ts
7264
- var DtlsPlaintext = class _DtlsPlaintext {
7265
- constructor(recordLayerHeader, fragment) {
7266
- this.recordLayerHeader = recordLayerHeader;
7267
- this.fragment = fragment;
7268
- }
7269
- static spec = {
7270
- recordLayerHeader: DtlsPlaintextHeader.spec,
7271
- fragment: types7.buffer(
7272
- (context) => context.current.recordLayerHeader.contentLen
7273
- )
7274
- };
7275
- get summary() {
7276
- return {
7277
- header: this.recordLayerHeader,
7278
- fragment: dumpBuffer2(this.fragment)
7279
- };
7280
- }
7281
- static createEmpty() {
7282
- return new _DtlsPlaintext(void 0, void 0);
7283
- }
7284
- static deSerialize(buf) {
7285
- const r = new _DtlsPlaintext(
7286
- ...Object.values(decode6(buf, _DtlsPlaintext.spec))
7287
- );
7288
- return r;
7289
- }
7290
- serialize() {
7291
- const res = encode6(this, _DtlsPlaintext.spec).slice();
7292
- return Buffer.from(res);
7293
- }
7294
- computeMACHeader() {
7295
- return new MACHeader(
7296
- this.recordLayerHeader.epoch,
7297
- this.recordLayerHeader.sequenceNumber,
7298
- this.recordLayerHeader.contentType,
7299
- this.recordLayerHeader.protocolVersion,
7300
- this.recordLayerHeader.contentLen
7301
- ).serialize();
7302
- }
7303
- };
7304
-
7305
7352
  // ../dtls/src/flight/flight.ts
7306
7353
  var warn = debug("werift-dtls : packages/dtls/src/flight/flight.ts : warn");
7307
7354
  var err = debug("werift-dtls : packages/dtls/src/flight/flight.ts : err");
@@ -7383,18 +7430,7 @@ function createECDHEPSKKeyExchange() {
7383
7430
  }
7384
7431
 
7385
7432
  // ../dtls/src/cipher/suites/aead.ts
7386
- import * as crypto2 from "crypto";
7387
- import { createDecode as createDecode2, encode as encode7, types as types8 } from "@shinyoshiaki/binary-data";
7388
- var { uint8: uint82, uint16be: uint16be2, uint48be } = types8;
7389
- var ContentType2 = uint82;
7390
- var ProtocolVersion2 = uint16be2;
7391
- var AEADAdditionalData = {
7392
- epoch: uint16be2,
7393
- sequence: uint48be,
7394
- type: ContentType2,
7395
- version: ProtocolVersion2,
7396
- length: uint16be2
7397
- };
7433
+ import { createCipheriv as createCipheriv4, createDecipheriv as createDecipheriv3 } from "crypto";
7398
7434
  var err2 = debug(
7399
7435
  "werift-dtls : packages/dtls/src/cipher/suites/aead.ts : err"
7400
7436
  );
@@ -7441,22 +7477,10 @@ var AEADCipher = class extends AbstractCipher {
7441
7477
  iv.writeUInt16BE(header.epoch, this.nonceImplicitLength);
7442
7478
  iv.writeUIntBE(header.sequenceNumber, this.nonceImplicitLength + 2, 6);
7443
7479
  const explicitNonce = iv.slice(this.nonceImplicitLength);
7444
- const additionalData = {
7445
- epoch: header.epoch,
7446
- sequence: header.sequenceNumber,
7447
- type: header.type,
7448
- version: header.version,
7449
- length: data.length
7450
- };
7451
- const additionalBuffer = encode7(additionalData, AEADAdditionalData).slice();
7452
- const cipher = crypto2.createCipheriv(
7453
- this.blockAlgorithm,
7454
- writeKey,
7455
- iv,
7456
- {
7457
- authTagLength: this.authTagLength
7458
- }
7459
- );
7480
+ const additionalBuffer = this.encodeAdditionalBuffer(header, data.length);
7481
+ const cipher = createCipheriv4(this.blockAlgorithm, writeKey, iv, {
7482
+ authTagLength: this.authTagLength
7483
+ });
7460
7484
  cipher.setAAD(additionalBuffer, {
7461
7485
  plaintextLength: data.length
7462
7486
  });
@@ -7465,6 +7489,15 @@ var AEADCipher = class extends AbstractCipher {
7465
7489
  const authTag = cipher.getAuthTag();
7466
7490
  return Buffer.concat([explicitNonce, headPart, finalPart, authTag]);
7467
7491
  }
7492
+ encodeAdditionalBuffer(header, dataLength) {
7493
+ const additionalBuffer = Buffer.alloc(13);
7494
+ additionalBuffer.writeUInt16BE(header.epoch, 0);
7495
+ additionalBuffer.writeUintBE(header.sequenceNumber, 2, 6);
7496
+ additionalBuffer.writeUInt8(header.type, 8);
7497
+ additionalBuffer.writeUInt16BE(header.version, 9);
7498
+ additionalBuffer.writeUInt16BE(dataLength, 11);
7499
+ return additionalBuffer;
7500
+ }
7468
7501
  /**
7469
7502
  * Decrypt message.
7470
7503
  */
@@ -7473,27 +7506,20 @@ var AEADCipher = class extends AbstractCipher {
7473
7506
  const iv = isClient ? this.serverNonce : this.clientNonce;
7474
7507
  const writeKey = isClient ? this.serverWriteKey : this.clientWriteKey;
7475
7508
  if (!iv || !writeKey) throw new Error();
7476
- const final = createDecode2(data);
7477
- const explicitNonce = final.readBuffer(this.nonceExplicitLength);
7509
+ const explicitNonce = data.subarray(0, this.nonceExplicitLength);
7478
7510
  explicitNonce.copy(iv, this.nonceImplicitLength);
7479
- const encrypted = final.readBuffer(final.length - this.authTagLength);
7480
- const authTag = final.readBuffer(this.authTagLength);
7481
- const additionalData = {
7482
- epoch: header.epoch,
7483
- sequence: header.sequenceNumber,
7484
- type: header.type,
7485
- version: header.version,
7486
- length: encrypted.length
7487
- };
7488
- const additionalBuffer = encode7(additionalData, AEADAdditionalData).slice();
7489
- const decipher = crypto2.createDecipheriv(
7490
- this.blockAlgorithm,
7491
- writeKey,
7492
- iv,
7493
- {
7494
- authTagLength: this.authTagLength
7495
- }
7511
+ const encrypted = data.subarray(
7512
+ this.nonceExplicitLength,
7513
+ data.length - this.authTagLength
7514
+ );
7515
+ const authTag = data.subarray(data.length - this.authTagLength);
7516
+ const additionalBuffer = this.encodeAdditionalBuffer(
7517
+ header,
7518
+ encrypted.length
7496
7519
  );
7520
+ const decipher = createDecipheriv3(this.blockAlgorithm, writeKey, iv, {
7521
+ authTagLength: this.authTagLength
7522
+ });
7497
7523
  decipher.setAuthTag(authTag);
7498
7524
  decipher.setAAD(additionalBuffer, {
7499
7525
  plaintextLength: encrypted.length
@@ -7710,12 +7736,12 @@ var ExtendedMasterSecret = class {
7710
7736
  };
7711
7737
 
7712
7738
  // ../dtls/src/handshake/extensions/renegotiationIndication.ts
7713
- import { decode as decode7, encode as encode8, types as types9 } from "@shinyoshiaki/binary-data";
7739
+ import { decode as decode5, encode as encode6, types as types7 } from "@shinyoshiaki/binary-data";
7714
7740
  var RenegotiationIndication = class _RenegotiationIndication {
7715
7741
  static type = 65281;
7716
7742
  static spec = {
7717
- type: types9.uint16be,
7718
- data: types9.uint8
7743
+ type: types7.uint16be,
7744
+ data: types7.uint8
7719
7745
  };
7720
7746
  type = _RenegotiationIndication.type;
7721
7747
  data = 0;
@@ -7728,11 +7754,11 @@ var RenegotiationIndication = class _RenegotiationIndication {
7728
7754
  }
7729
7755
  static deSerialize(buf) {
7730
7756
  return new _RenegotiationIndication(
7731
- decode7(buf, _RenegotiationIndication.spec)
7757
+ decode5(buf, _RenegotiationIndication.spec)
7732
7758
  );
7733
7759
  }
7734
7760
  serialize() {
7735
- const res = encode8(this, _RenegotiationIndication.spec).slice();
7761
+ const res = encode6(this, _RenegotiationIndication.spec).slice();
7736
7762
  return Buffer.from(res);
7737
7763
  }
7738
7764
  get extension() {
@@ -7744,14 +7770,13 @@ var RenegotiationIndication = class _RenegotiationIndication {
7744
7770
  };
7745
7771
 
7746
7772
  // ../dtls/src/handshake/extensions/useSrtp.ts
7747
- import { decode as decode8, encode as encode9, types as types10 } from "@shinyoshiaki/binary-data";
7748
- import times from "lodash/times.js";
7773
+ import { decode as decode6, encode as encode7, types as types8 } from "@shinyoshiaki/binary-data";
7749
7774
  var UseSRTP = class _UseSRTP {
7750
7775
  static type = 14;
7751
7776
  // 9. IANA Considerations
7752
7777
  static spec = {
7753
- type: types10.uint16be,
7754
- data: types10.buffer(types10.uint16be)
7778
+ type: types8.uint16be,
7779
+ data: types8.buffer(types8.uint16be)
7755
7780
  };
7756
7781
  type = _UseSRTP.type;
7757
7782
  data = Buffer.from([]);
@@ -7768,11 +7793,12 @@ var UseSRTP = class _UseSRTP {
7768
7793
  return v;
7769
7794
  }
7770
7795
  static deSerialize(buf) {
7771
- const useSrtp = new _UseSRTP(decode8(buf, _UseSRTP.spec));
7796
+ const useSrtp = new _UseSRTP(decode6(buf, _UseSRTP.spec));
7772
7797
  const profileLength = useSrtp.data.readUInt16BE();
7773
- const profiles = times(profileLength / 2).map((i) => {
7774
- return useSrtp.data.readUInt16BE(i * 2 + 2);
7775
- });
7798
+ const profiles = new Array(profileLength / 2);
7799
+ for (let i = 0; i < profiles.length; i++) {
7800
+ profiles[i] = useSrtp.data.readUInt16BE(i * 2 + 2);
7801
+ }
7776
7802
  useSrtp.profiles = profiles;
7777
7803
  useSrtp.mki = useSrtp.data.slice(profileLength + 2);
7778
7804
  return useSrtp;
@@ -7790,7 +7816,7 @@ var UseSRTP = class _UseSRTP {
7790
7816
  this.mki
7791
7817
  ]);
7792
7818
  this.data = data;
7793
- const res = encode9(this, _UseSRTP.spec).slice();
7819
+ const res = encode7(this, _UseSRTP.spec).slice();
7794
7820
  return Buffer.from(res);
7795
7821
  }
7796
7822
  static fromData(buf) {
@@ -7808,7 +7834,7 @@ var UseSRTP = class _UseSRTP {
7808
7834
  };
7809
7835
 
7810
7836
  // ../dtls/src/handshake/message/certificate.ts
7811
- import { decode as decode9, encode as encode10, types as types11 } from "@shinyoshiaki/binary-data";
7837
+ import { decode as decode7, encode as encode8, types as types9 } from "@shinyoshiaki/binary-data";
7812
7838
  var Certificate2 = class _Certificate {
7813
7839
  constructor(certificateList) {
7814
7840
  this.certificateList = certificateList;
@@ -7816,18 +7842,18 @@ var Certificate2 = class _Certificate {
7816
7842
  msgType = 11 /* certificate_11 */;
7817
7843
  messageSeq;
7818
7844
  static spec = {
7819
- certificateList: types11.array(ASN11Cert, types11.uint24be, "bytes")
7845
+ certificateList: types9.array(ASN11Cert, types9.uint24be, "bytes")
7820
7846
  };
7821
7847
  static createEmpty() {
7822
7848
  return new _Certificate(void 0);
7823
7849
  }
7824
7850
  static deSerialize(buf) {
7825
7851
  return new _Certificate(
7826
- ...Object.values(decode9(buf, _Certificate.spec))
7852
+ ...Object.values(decode7(buf, _Certificate.spec))
7827
7853
  );
7828
7854
  }
7829
7855
  serialize() {
7830
- const res = encode10(this, _Certificate.spec).slice();
7856
+ const res = encode8(this, _Certificate.spec).slice();
7831
7857
  return Buffer.from(res);
7832
7858
  }
7833
7859
  toFragment() {
@@ -7844,30 +7870,30 @@ var Certificate2 = class _Certificate {
7844
7870
  };
7845
7871
 
7846
7872
  // ../dtls/src/handshake/message/changeCipherSpec.ts
7847
- import { decode as decode10, encode as encode11, types as types12 } from "@shinyoshiaki/binary-data";
7873
+ import { decode as decode8, encode as encode9, types as types10 } from "@shinyoshiaki/binary-data";
7848
7874
  var ChangeCipherSpec = class _ChangeCipherSpec {
7849
7875
  constructor(type = 1) {
7850
7876
  this.type = type;
7851
7877
  }
7852
7878
  static spec = {
7853
- type: types12.uint8
7879
+ type: types10.uint8
7854
7880
  };
7855
7881
  static createEmpty() {
7856
7882
  return new _ChangeCipherSpec();
7857
7883
  }
7858
7884
  static deSerialize(buf) {
7859
7885
  return new _ChangeCipherSpec(
7860
- ...Object.values(decode10(buf, _ChangeCipherSpec.spec))
7886
+ ...Object.values(decode8(buf, _ChangeCipherSpec.spec))
7861
7887
  );
7862
7888
  }
7863
7889
  serialize() {
7864
- const res = encode11(this, _ChangeCipherSpec.spec).slice();
7890
+ const res = encode9(this, _ChangeCipherSpec.spec).slice();
7865
7891
  return Buffer.from(res);
7866
7892
  }
7867
7893
  };
7868
7894
 
7869
7895
  // ../dtls/src/handshake/message/client/certificateVerify.ts
7870
- import { decode as decode11, encode as encode12, types as types13 } from "@shinyoshiaki/binary-data";
7896
+ import { decode as decode9, encode as encode10, types as types11 } from "@shinyoshiaki/binary-data";
7871
7897
  var CertificateVerify = class _CertificateVerify {
7872
7898
  constructor(algorithm, signature) {
7873
7899
  this.algorithm = algorithm;
@@ -7876,20 +7902,20 @@ var CertificateVerify = class _CertificateVerify {
7876
7902
  msgType = 15 /* certificate_verify_15 */;
7877
7903
  messageSeq;
7878
7904
  static spec = {
7879
- algorithm: types13.uint16be,
7880
- signature: types13.buffer(types13.uint16be)
7905
+ algorithm: types11.uint16be,
7906
+ signature: types11.buffer(types11.uint16be)
7881
7907
  };
7882
7908
  static createEmpty() {
7883
7909
  return new _CertificateVerify(void 0, void 0);
7884
7910
  }
7885
7911
  static deSerialize(buf) {
7886
- const res = decode11(buf, _CertificateVerify.spec);
7912
+ const res = decode9(buf, _CertificateVerify.spec);
7887
7913
  return new _CertificateVerify(
7888
7914
  ...Object.values(res)
7889
7915
  );
7890
7916
  }
7891
7917
  serialize() {
7892
- const res = encode12(this, _CertificateVerify.spec).slice();
7918
+ const res = encode10(this, _CertificateVerify.spec).slice();
7893
7919
  return Buffer.from(res);
7894
7920
  }
7895
7921
  toFragment() {
@@ -7906,7 +7932,7 @@ var CertificateVerify = class _CertificateVerify {
7906
7932
  };
7907
7933
 
7908
7934
  // ../dtls/src/handshake/message/client/keyExchange.ts
7909
- import { decode as decode12, encode as encode13, types as types14 } from "@shinyoshiaki/binary-data";
7935
+ import { decode as decode10, encode as encode11, types as types12 } from "@shinyoshiaki/binary-data";
7910
7936
  var ClientKeyExchange = class _ClientKeyExchange {
7911
7937
  constructor(publicKey) {
7912
7938
  this.publicKey = publicKey;
@@ -7914,19 +7940,19 @@ var ClientKeyExchange = class _ClientKeyExchange {
7914
7940
  msgType = 16 /* client_key_exchange_16 */;
7915
7941
  messageSeq;
7916
7942
  static spec = {
7917
- publicKey: types14.buffer(types14.uint8)
7943
+ publicKey: types12.buffer(types12.uint8)
7918
7944
  };
7919
7945
  static createEmpty() {
7920
7946
  return new _ClientKeyExchange(void 0);
7921
7947
  }
7922
7948
  static deSerialize(buf) {
7923
- const res = decode12(buf, _ClientKeyExchange.spec);
7949
+ const res = decode10(buf, _ClientKeyExchange.spec);
7924
7950
  return new _ClientKeyExchange(
7925
7951
  ...Object.values(res)
7926
7952
  );
7927
7953
  }
7928
7954
  serialize() {
7929
- const res = encode13(this, _ClientKeyExchange.spec).slice();
7955
+ const res = encode11(this, _ClientKeyExchange.spec).slice();
7930
7956
  return Buffer.from(res);
7931
7957
  }
7932
7958
  toFragment() {
@@ -7943,7 +7969,7 @@ var ClientKeyExchange = class _ClientKeyExchange {
7943
7969
  };
7944
7970
 
7945
7971
  // ../dtls/src/handshake/message/server/certificateRequest.ts
7946
- import { decode as decode13, encode as encode14, types as types15 } from "@shinyoshiaki/binary-data";
7972
+ import { decode as decode11, encode as encode12, types as types13 } from "@shinyoshiaki/binary-data";
7947
7973
  var ServerCertificateRequest = class _ServerCertificateRequest {
7948
7974
  constructor(certificateTypes2, signatures2, authorities) {
7949
7975
  this.certificateTypes = certificateTypes2;
@@ -7953,9 +7979,9 @@ var ServerCertificateRequest = class _ServerCertificateRequest {
7953
7979
  msgType = 13 /* certificate_request_13 */;
7954
7980
  messageSeq;
7955
7981
  static spec = {
7956
- certificateTypes: types15.array(ClientCertificateType, types15.uint8, "bytes"),
7957
- signatures: types15.array(SignatureHashAlgorithm, types15.uint16be, "bytes"),
7958
- authorities: types15.array(DistinguishedName, types15.uint16be, "bytes")
7982
+ certificateTypes: types13.array(ClientCertificateType, types13.uint8, "bytes"),
7983
+ signatures: types13.array(SignatureHashAlgorithm, types13.uint16be, "bytes"),
7984
+ authorities: types13.array(DistinguishedName, types13.uint16be, "bytes")
7959
7985
  };
7960
7986
  static createEmpty() {
7961
7987
  return new _ServerCertificateRequest(
@@ -7966,11 +7992,11 @@ var ServerCertificateRequest = class _ServerCertificateRequest {
7966
7992
  }
7967
7993
  static deSerialize(buf) {
7968
7994
  return new _ServerCertificateRequest(
7969
- ...Object.values(decode13(buf, _ServerCertificateRequest.spec))
7995
+ ...Object.values(decode11(buf, _ServerCertificateRequest.spec))
7970
7996
  );
7971
7997
  }
7972
7998
  serialize() {
7973
- const res = encode14(this, _ServerCertificateRequest.spec).slice();
7999
+ const res = encode12(this, _ServerCertificateRequest.spec).slice();
7974
8000
  return Buffer.from(res);
7975
8001
  }
7976
8002
  toFragment() {
@@ -7987,7 +8013,7 @@ var ServerCertificateRequest = class _ServerCertificateRequest {
7987
8013
  };
7988
8014
 
7989
8015
  // ../dtls/src/handshake/message/server/hello.ts
7990
- import { decode as decode14, encode as encode15, types as types16 } from "@shinyoshiaki/binary-data";
8016
+ import { decode as decode12, encode as encode13, types as types14 } from "@shinyoshiaki/binary-data";
7991
8017
  var ServerHello = class _ServerHello {
7992
8018
  constructor(serverVersion, random, sessionId, cipherSuite, compressionMethod, extensions) {
7993
8019
  this.serverVersion = serverVersion;
@@ -8002,9 +8028,9 @@ var ServerHello = class _ServerHello {
8002
8028
  static spec = {
8003
8029
  serverVersion: ProtocolVersion,
8004
8030
  random: DtlsRandom.spec,
8005
- sessionId: types16.buffer(types16.uint8),
8006
- cipherSuite: types16.uint16be,
8007
- compressionMethod: types16.uint8
8031
+ sessionId: types14.buffer(types14.uint8),
8032
+ cipherSuite: types14.uint16be,
8033
+ compressionMethod: types14.uint8
8008
8034
  };
8009
8035
  static createEmpty() {
8010
8036
  return new _ServerHello(
@@ -8017,7 +8043,7 @@ var ServerHello = class _ServerHello {
8017
8043
  );
8018
8044
  }
8019
8045
  static deSerialize(buf) {
8020
- const res = decode14(buf, _ServerHello.spec);
8046
+ const res = decode12(buf, _ServerHello.spec);
8021
8047
  const cls = new _ServerHello(
8022
8048
  ...Object.values(res)
8023
8049
  );
@@ -8025,14 +8051,14 @@ var ServerHello = class _ServerHello {
8025
8051
  if (expect.length < buf.length) {
8026
8052
  return new _ServerHello(
8027
8053
  ...Object.values(
8028
- decode14(buf, { ..._ServerHello.spec, extensions: ExtensionList })
8054
+ decode12(buf, { ..._ServerHello.spec, extensions: ExtensionList })
8029
8055
  )
8030
8056
  );
8031
8057
  }
8032
8058
  return cls;
8033
8059
  }
8034
8060
  serialize() {
8035
- const res = this.extensions === void 0 ? encode15(this, _ServerHello.spec).slice() : encode15(this, {
8061
+ const res = this.extensions === void 0 ? encode13(this, _ServerHello.spec).slice() : encode13(this, {
8036
8062
  ..._ServerHello.spec,
8037
8063
  extensions: ExtensionList
8038
8064
  }).slice();
@@ -8052,15 +8078,15 @@ var ServerHello = class _ServerHello {
8052
8078
  };
8053
8079
 
8054
8080
  // ../dtls/src/handshake/message/server/helloDone.ts
8055
- import { decode as decode15, encode as encode16 } from "@shinyoshiaki/binary-data";
8081
+ import { decode as decode13, encode as encode14 } from "@shinyoshiaki/binary-data";
8056
8082
 
8057
8083
  // ../dtls/src/handshake/message/server/keyExchange.ts
8058
- import { decode as decode16, types as types17 } from "@shinyoshiaki/binary-data";
8084
+ import { decode as decode14, types as types15 } from "@shinyoshiaki/binary-data";
8059
8085
 
8060
8086
  // ../dtls/src/util/binary.ts
8061
- import { encode as encode17 } from "@shinyoshiaki/binary-data";
8087
+ import { encode as encode15 } from "@shinyoshiaki/binary-data";
8062
8088
  function encodeBuffer(obj, spec) {
8063
- return Buffer.from(encode17(obj, spec).slice());
8089
+ return Buffer.from(encode15(obj, spec).slice());
8064
8090
  }
8065
8091
 
8066
8092
  // ../dtls/src/handshake/message/server/keyExchange.ts
@@ -8078,14 +8104,14 @@ var ServerKeyExchange = class _ServerKeyExchange {
8078
8104
  msgType = 12 /* server_key_exchange_12 */;
8079
8105
  messageSeq;
8080
8106
  static spec = {
8081
- ellipticCurveType: types17.uint8,
8082
- namedCurve: types17.uint16be,
8083
- publicKeyLength: types17.uint8,
8084
- publicKey: types17.buffer((ctx) => ctx.current.publicKeyLength),
8085
- hashAlgorithm: types17.uint8,
8086
- signatureAlgorithm: types17.uint8,
8087
- signatureLength: types17.uint16be,
8088
- signature: types17.buffer((ctx) => ctx.current.signatureLength)
8107
+ ellipticCurveType: types15.uint8,
8108
+ namedCurve: types15.uint16be,
8109
+ publicKeyLength: types15.uint8,
8110
+ publicKey: types15.buffer((ctx) => ctx.current.publicKeyLength),
8111
+ hashAlgorithm: types15.uint8,
8112
+ signatureAlgorithm: types15.uint8,
8113
+ signatureLength: types15.uint16be,
8114
+ signature: types15.buffer((ctx) => ctx.current.signatureLength)
8089
8115
  };
8090
8116
  static createEmpty() {
8091
8117
  return new _ServerKeyExchange(
@@ -8100,7 +8126,7 @@ var ServerKeyExchange = class _ServerKeyExchange {
8100
8126
  );
8101
8127
  }
8102
8128
  static deSerialize(buf) {
8103
- const res = decode16(buf, _ServerKeyExchange.spec);
8129
+ const res = decode14(buf, _ServerKeyExchange.spec);
8104
8130
  return new _ServerKeyExchange(
8105
8131
  ...Object.values(res)
8106
8132
  );
@@ -8181,7 +8207,7 @@ handlers[14 /* server_hello_done_14 */] = ({ dtls }) => (msg) => {
8181
8207
  };
8182
8208
 
8183
8209
  // ../dtls/src/handshake/message/server/helloVerifyRequest.ts
8184
- import { decode as decode17, encode as encode18, types as types18 } from "@shinyoshiaki/binary-data";
8210
+ import { decode as decode15, encode as encode16, types as types16 } from "@shinyoshiaki/binary-data";
8185
8211
  var ServerHelloVerifyRequest = class _ServerHelloVerifyRequest {
8186
8212
  constructor(serverVersion, cookie) {
8187
8213
  this.serverVersion = serverVersion;
@@ -8191,18 +8217,18 @@ var ServerHelloVerifyRequest = class _ServerHelloVerifyRequest {
8191
8217
  messageSeq;
8192
8218
  static spec = {
8193
8219
  serverVersion: ProtocolVersion,
8194
- cookie: types18.buffer(types18.uint8)
8220
+ cookie: types16.buffer(types16.uint8)
8195
8221
  };
8196
8222
  static createEmpty() {
8197
8223
  return new _ServerHelloVerifyRequest(void 0, void 0);
8198
8224
  }
8199
8225
  static deSerialize(buf) {
8200
8226
  return new _ServerHelloVerifyRequest(
8201
- ...Object.values(decode17(buf, _ServerHelloVerifyRequest.spec))
8227
+ ...Object.values(decode15(buf, _ServerHelloVerifyRequest.spec))
8202
8228
  );
8203
8229
  }
8204
8230
  serialize() {
8205
- const res = encode18(this, _ServerHelloVerifyRequest.spec).slice();
8231
+ const res = encode16(this, _ServerHelloVerifyRequest.spec).slice();
8206
8232
  return Buffer.from(res);
8207
8233
  }
8208
8234
  get version() {
@@ -8225,18 +8251,18 @@ var ServerHelloVerifyRequest = class _ServerHelloVerifyRequest {
8225
8251
  };
8226
8252
 
8227
8253
  // ../dtls/src/socket.ts
8228
- import { decode as decode21, types as types22 } from "@shinyoshiaki/binary-data";
8254
+ import { decode as decode19, types as types20 } from "@shinyoshiaki/binary-data";
8229
8255
 
8230
8256
  // ../dtls/src/context/dtls.ts
8231
8257
  var log12 = debug("werift-dtls : packages/dtls/src/context/dtls.ts : log");
8232
8258
 
8233
8259
  // ../dtls/src/handshake/extensions/ellipticCurves.ts
8234
- import { decode as decode18, encode as encode19, types as types19 } from "@shinyoshiaki/binary-data";
8260
+ import { decode as decode16, encode as encode17, types as types17 } from "@shinyoshiaki/binary-data";
8235
8261
  var EllipticCurves = class _EllipticCurves {
8236
8262
  static type = 10;
8237
8263
  static spec = {
8238
- type: types19.uint16be,
8239
- data: types19.array(types19.uint16be, types19.uint16be, "bytes")
8264
+ type: types17.uint16be,
8265
+ data: types17.array(types17.uint16be, types17.uint16be, "bytes")
8240
8266
  };
8241
8267
  type = _EllipticCurves.type;
8242
8268
  data = [];
@@ -8249,14 +8275,14 @@ var EllipticCurves = class _EllipticCurves {
8249
8275
  static fromData(buf) {
8250
8276
  return new _EllipticCurves({
8251
8277
  type: _EllipticCurves.type,
8252
- data: decode18(buf, _EllipticCurves.spec.data)
8278
+ data: decode16(buf, _EllipticCurves.spec.data)
8253
8279
  });
8254
8280
  }
8255
8281
  static deSerialize(buf) {
8256
- return new _EllipticCurves(decode18(buf, _EllipticCurves.spec));
8282
+ return new _EllipticCurves(decode16(buf, _EllipticCurves.spec));
8257
8283
  }
8258
8284
  serialize() {
8259
- return Buffer.from(encode19(this, _EllipticCurves.spec).slice());
8285
+ return Buffer.from(encode17(this, _EllipticCurves.spec).slice());
8260
8286
  }
8261
8287
  get extension() {
8262
8288
  return {
@@ -8267,14 +8293,14 @@ var EllipticCurves = class _EllipticCurves {
8267
8293
  };
8268
8294
 
8269
8295
  // ../dtls/src/handshake/extensions/signature.ts
8270
- import { decode as decode19, encode as encode20, types as types20 } from "@shinyoshiaki/binary-data";
8296
+ import { decode as decode17, encode as encode18, types as types18 } from "@shinyoshiaki/binary-data";
8271
8297
  var Signature = class _Signature {
8272
8298
  static type = 13;
8273
8299
  static spec = {
8274
- type: types20.uint16be,
8275
- data: types20.array(
8276
- { hash: types20.uint8, signature: types20.uint8 },
8277
- types20.uint16be,
8300
+ type: types18.uint16be,
8301
+ data: types18.array(
8302
+ { hash: types18.uint8, signature: types18.uint8 },
8303
+ types18.uint16be,
8278
8304
  "bytes"
8279
8305
  )
8280
8306
  };
@@ -8288,10 +8314,10 @@ var Signature = class _Signature {
8288
8314
  return v;
8289
8315
  }
8290
8316
  static deSerialize(buf) {
8291
- return new _Signature(decode19(buf, _Signature.spec));
8317
+ return new _Signature(decode17(buf, _Signature.spec));
8292
8318
  }
8293
8319
  serialize() {
8294
- const res = encode20(this, _Signature.spec).slice();
8320
+ const res = encode18(this, _Signature.spec).slice();
8295
8321
  return Buffer.from(res);
8296
8322
  }
8297
8323
  static fromData(buf) {
@@ -8308,23 +8334,23 @@ var Signature = class _Signature {
8308
8334
  };
8309
8335
 
8310
8336
  // ../dtls/src/handshake/message/alert.ts
8311
- import { decode as decode20, encode as encode21, types as types21 } from "@shinyoshiaki/binary-data";
8337
+ import { decode as decode18, encode as encode19, types as types19 } from "@shinyoshiaki/binary-data";
8312
8338
  var Alert = class _Alert {
8313
8339
  constructor(level, description) {
8314
8340
  this.level = level;
8315
8341
  this.description = description;
8316
8342
  }
8317
8343
  static spec = {
8318
- level: types21.uint8,
8319
- description: types21.uint8
8344
+ level: types19.uint8,
8345
+ description: types19.uint8
8320
8346
  };
8321
8347
  static deSerialize(buf) {
8322
8348
  return new _Alert(
8323
- ...Object.values(decode20(buf, _Alert.spec))
8349
+ ...Object.values(decode18(buf, _Alert.spec))
8324
8350
  );
8325
8351
  }
8326
8352
  serialize() {
8327
- const res = encode21(this, _Alert.spec).slice();
8353
+ const res = encode19(this, _Alert.spec).slice();
8328
8354
  return Buffer.from(res);
8329
8355
  }
8330
8356
  };
@@ -8396,32 +8422,32 @@ var IPV4_PROTOCOL = 1;
8396
8422
  var IPV6_PROTOCOL = 2;
8397
8423
 
8398
8424
  // ../ice/src/stun/message.ts
8399
- import { jspack as jspack4 } from "@shinyoshiaki/jspack";
8400
8425
  import crc32 from "buffer-crc32";
8401
8426
 
8402
8427
  // ../ice/src/stun/attributes.ts
8403
- import { jspack as jspack3 } from "@shinyoshiaki/jspack";
8404
8428
  import * as Int64 from "int64-buffer";
8405
8429
  import nodeIp from "ip";
8406
- import range from "lodash/range.js";
8407
8430
  function packAddress(value) {
8408
8431
  const [address] = value;
8409
8432
  const protocol = nodeIp.isV4Format(address) ? IPV4_PROTOCOL : IPV6_PROTOCOL;
8410
- return Buffer.concat([
8411
- Buffer.from(jspack3.Pack("!BBH", [0, protocol, value[1]])),
8412
- nodeIp.toBuffer(address)
8413
- ]);
8433
+ const buffer2 = Buffer.alloc(4);
8434
+ buffer2.writeUInt8(0, 0);
8435
+ buffer2.writeUInt8(protocol, 1);
8436
+ buffer2.writeUInt16BE(value[1], 2);
8437
+ return Buffer.concat([buffer2, nodeIp.toBuffer(address)]);
8414
8438
  }
8415
8439
  function unpackErrorCode(data) {
8416
8440
  if (data.length < 4) throw new Error("STUN error code is less than 4 bytes");
8417
- const [, codeHigh, codeLow] = jspack3.Unpack("!HBB", data.slice(0, 4));
8441
+ const codeHigh = data.readUInt8(2);
8442
+ const codeLow = data.readUInt8(3);
8418
8443
  const reason = data.slice(4).toString("utf8");
8419
8444
  return [codeHigh * 100 + codeLow, reason];
8420
8445
  }
8421
8446
  function unpackAddress(data) {
8422
8447
  if (data.length < 4)
8423
8448
  throw new Error("STUN address length is less than 4 bytes");
8424
- const [, protocol, port] = jspack3.Unpack("!BBH", data.slice(0, 4));
8449
+ const protocol = data.readUInt8(1);
8450
+ const port = data.readUInt16BE(2);
8425
8451
  const address = data.slice(4);
8426
8452
  switch (protocol) {
8427
8453
  case IPV4_PROTOCOL:
@@ -8436,13 +8462,13 @@ function unpackAddress(data) {
8436
8462
  throw new Error("STUN address has unknown protocol");
8437
8463
  }
8438
8464
  }
8465
+ var cookieBuffer = Buffer.alloc(6);
8466
+ cookieBuffer.writeUInt16BE(COOKIE >> 16, 0);
8467
+ cookieBuffer.writeUInt32BE(COOKIE, 2);
8439
8468
  function xorAddress(data, transactionId) {
8440
- const xPad = [
8441
- ...jspack3.Pack("!HI", [COOKIE >> 16, COOKIE]),
8442
- ...transactionId
8443
- ];
8469
+ const xPad = [...cookieBuffer, ...transactionId];
8444
8470
  let xData = data.slice(0, 2);
8445
- for (const i of range(2, data.length)) {
8471
+ for (let i = 2; i < data.length; i++) {
8446
8472
  const num = data[i] ^ xPad[i - 2];
8447
8473
  const buf = Buffer.alloc(1);
8448
8474
  buf.writeUIntBE(num, 0, 1);
@@ -8454,22 +8480,28 @@ function unpackXorAddress(data, transactionId) {
8454
8480
  return unpackAddress(xorAddress(data, transactionId));
8455
8481
  }
8456
8482
  function packErrorCode(value) {
8457
- const pack = Buffer.from(
8458
- jspack3.Pack("!HBB", [0, Math.floor(value[0] / 100), value[0] % 100])
8459
- );
8460
- const encode22 = Buffer.from(value[1], "utf8");
8461
- return Buffer.concat([pack, encode22]);
8483
+ const buffer2 = Buffer.alloc(4);
8484
+ buffer2.writeUInt16BE(0, 0);
8485
+ buffer2.writeUInt8(Math.floor(value[0] / 100), 2);
8486
+ buffer2.writeUInt8(value[0] % 100, 3);
8487
+ const encode20 = Buffer.from(value[1], "utf8");
8488
+ return Buffer.concat([buffer2, encode20]);
8462
8489
  }
8463
8490
  function packXorAddress(value, transactionId) {
8464
8491
  return xorAddress(packAddress(value), transactionId);
8465
8492
  }
8466
- var packUnsigned = (value) => Buffer.from(jspack3.Pack("!I", [value]));
8467
- var unpackUnsigned = (data) => jspack3.Unpack("!I", data)[0];
8468
- var packUnsignedShort = (value) => Buffer.concat([
8469
- Buffer.from(jspack3.Pack("!H", [value])),
8470
- Buffer.from("\0\0")
8471
- ]);
8472
- var unpackUnsignedShort = (data) => jspack3.Unpack("!H", data.slice(0, 2))[0];
8493
+ var packUnsigned = (value) => {
8494
+ const buffer2 = Buffer.alloc(4);
8495
+ buffer2.writeUInt32BE(value, 0);
8496
+ return buffer2;
8497
+ };
8498
+ var unpackUnsigned = (data) => data.readUInt32BE(0);
8499
+ var packUnsignedShort = (value) => {
8500
+ const buffer2 = Buffer.alloc(4);
8501
+ buffer2.writeUInt16BE(value, 0);
8502
+ return buffer2;
8503
+ };
8504
+ var unpackUnsignedShort = (data) => data.readUInt16BE(0);
8473
8505
  var packUnsigned64 = (value) => {
8474
8506
  return new Int64.Int64BE(value.toString()).toBuffer();
8475
8507
  };
@@ -8531,15 +8563,12 @@ var log20 = debug("werift-ice:packages/ice/src/stun/transaction.ts");
8531
8563
  var log21 = debug("werift-ice : packages/ice/src/stun/protocol.ts");
8532
8564
 
8533
8565
  // ../ice/src/turn/protocol.ts
8534
- import { jspack as jspack5 } from "@shinyoshiaki/jspack";
8566
+ import { jspack as jspack3 } from "@shinyoshiaki/jspack";
8535
8567
  var log22 = debug("werift-ice:packages/ice/src/turn/protocol.ts");
8536
8568
 
8537
- // ../ice/src/candidate.ts
8538
- import range2 from "lodash/range.js";
8539
-
8540
8569
  // ../ice/src/ice.ts
8541
8570
  import * as Int642 from "int64-buffer";
8542
- import isEqual from "lodash/isEqual.js";
8571
+ import isEqual from "fast-deep-equal";
8543
8572
 
8544
8573
  // ../ice/src/dns/lookup.ts
8545
8574
  import mdns from "multicast-dns";
@@ -8656,16 +8685,6 @@ var supportedAudioCodecs = supportedCodecs.filter(
8656
8685
  (codec) => codec.toLowerCase().startsWith("audio/")
8657
8686
  );
8658
8687
 
8659
- // src/media/rtpReceiver.ts
8660
- import { v4 as uuid3 } from "uuid";
8661
-
8662
- // src/utils.ts
8663
- import mergeWith from "lodash/mergeWith.js";
8664
- import now from "nano-time";
8665
-
8666
- // src/media/rtpTransceiver.ts
8667
- import * as uuid from "uuid";
8668
-
8669
8688
  // src/const.ts
8670
8689
  var DTLS_ROLE_SETUP = {
8671
8690
  auto: "actpass",
@@ -8679,11 +8698,15 @@ var DTLS_SETUP_ROLE = Object.keys(
8679
8698
  acc[key] = cur;
8680
8699
  return acc;
8681
8700
  }, {});
8701
+ var SRTP_PROFILE = {
8702
+ SRTP_AES128_CM_HMAC_SHA1_80: ProtectionProfileAes128CmHmacSha1_80,
8703
+ SRTP_AEAD_AES_128_GCM: ProtectionProfileAeadAes128Gcm
8704
+ };
8682
8705
 
8683
8706
  // src/media/track.ts
8684
- import { v4 as v42 } from "uuid";
8707
+ import { randomUUID as randomUUID5 } from "crypto";
8685
8708
  var MediaStreamTrack = class extends EventTarget2 {
8686
- uuid = v42();
8709
+ uuid = randomUUID5().toString();
8687
8710
  /**MediaStream ID*/
8688
8711
  streamId;
8689
8712
  remote = false;
@@ -8738,7 +8761,7 @@ var MediaStream = class {
8738
8761
  } else {
8739
8762
  Object.assign(this, props);
8740
8763
  }
8741
- this.id ??= v42();
8764
+ this.id ??= randomUUID5().toString();
8742
8765
  }
8743
8766
  addTrack(track) {
8744
8767
  track.streamId = this.id;
@@ -8757,10 +8780,11 @@ var MediaStream = class {
8757
8780
 
8758
8781
  // src/utils.ts
8759
8782
  var log26 = debug("werift:packages/webrtc/src/utils.ts");
8783
+ var milliTime = Date.now;
8784
+ var startupTimestampInMicroseconds = BigInt(Date.now()) * 1000n - process.hrtime.bigint() / 1000n;
8760
8785
  var createSelfSignedCertificate = CipherContext.createSelfSignedCertificateWithKey;
8761
8786
 
8762
8787
  // src/media/receiver/nack.ts
8763
- import range3 from "lodash/range.js";
8764
8788
  var log27 = debug("werift:packages/webrtc/src/media/receiver/nack.ts");
8765
8789
  var LOST_SIZE2 = 30 * 5;
8766
8790
 
@@ -8772,23 +8796,18 @@ import * as Int643 from "int64-buffer";
8772
8796
 
8773
8797
  // src/transport/dtls.ts
8774
8798
  import { Certificate as Certificate3, PrivateKey as PrivateKey2 } from "@fidm/x509";
8775
- import { v4 as v43 } from "uuid";
8776
8799
  var log29 = debug("werift:packages/webrtc/src/transport/dtls.ts");
8777
8800
 
8778
8801
  // src/transport/ice.ts
8779
- import { v4 as v44 } from "uuid";
8780
8802
  var log30 = debug("werift:packages/webrtc/src/transport/ice.ts");
8781
8803
 
8782
8804
  // src/transport/sctp.ts
8783
- import { jspack as jspack9 } from "@shinyoshiaki/jspack";
8784
- import * as uuid2 from "uuid";
8805
+ import { jspack as jspack6 } from "@shinyoshiaki/jspack";
8785
8806
 
8786
8807
  // ../sctp/src/sctp.ts
8787
- import { jspack as jspack8 } from "@shinyoshiaki/jspack";
8788
- import range5 from "lodash/range.js";
8808
+ import { jspack as jspack5 } from "@shinyoshiaki/jspack";
8789
8809
 
8790
8810
  // ../sctp/src/chunk.ts
8791
- import { jspack as jspack6 } from "@shinyoshiaki/jspack";
8792
8811
  import crc32c from "turbo-crc32/crc32c.js";
8793
8812
  var Chunk = class _Chunk {
8794
8813
  constructor(flags = 0, _body = Buffer.from("")) {
@@ -8807,10 +8826,12 @@ var Chunk = class _Chunk {
8807
8826
  }
8808
8827
  get bytes() {
8809
8828
  if (!this.body) throw new Error();
8829
+ const header = Buffer.alloc(4);
8830
+ header.writeUInt8(this.type, 0);
8831
+ header.writeUInt8(this.flags, 1);
8832
+ header.writeUInt16BE(this.body.length + 4, 2);
8810
8833
  const data = Buffer.concat([
8811
- Buffer.from(
8812
- jspack6.Pack("!BBH", [this.type, this.flags, this.body.length + 4])
8813
- ),
8834
+ header,
8814
8835
  this.body,
8815
8836
  ...[...Array(padL(this.body.length))].map(() => Buffer.from("\0"))
8816
8837
  ]);
@@ -8822,13 +8843,11 @@ var BaseInitChunk = class extends Chunk {
8822
8843
  super(flags, body);
8823
8844
  this.flags = flags;
8824
8845
  if (body) {
8825
- [
8826
- this.initiateTag,
8827
- this.advertisedRwnd,
8828
- this.outboundStreams,
8829
- this.inboundStreams,
8830
- this.initialTsn
8831
- ] = jspack6.Unpack("!LLHHL", body);
8846
+ this.initiateTag = body.readUInt32BE(0);
8847
+ this.advertisedRwnd = body.readUInt32BE(4);
8848
+ this.outboundStreams = body.readUInt16BE(8);
8849
+ this.inboundStreams = body.readUInt16BE(10);
8850
+ this.initialTsn = body.readUInt32BE(12);
8832
8851
  this.params = decodeParams(body.slice(16));
8833
8852
  } else {
8834
8853
  this.initiateTag = 0;
@@ -8846,17 +8865,13 @@ var BaseInitChunk = class extends Chunk {
8846
8865
  initialTsn;
8847
8866
  params;
8848
8867
  get body() {
8849
- let body = Buffer.from(
8850
- jspack6.Pack("!LLHHL", [
8851
- this.initiateTag,
8852
- this.advertisedRwnd,
8853
- this.outboundStreams,
8854
- this.inboundStreams,
8855
- this.initialTsn
8856
- ])
8857
- );
8858
- body = Buffer.concat([body, encodeParams(this.params)]);
8859
- return body;
8868
+ const body = Buffer.alloc(16);
8869
+ body.writeUInt32BE(this.initiateTag, 0);
8870
+ body.writeUInt32BE(this.advertisedRwnd, 4);
8871
+ body.writeUInt16BE(this.outboundStreams, 8);
8872
+ body.writeUInt16BE(this.inboundStreams, 10);
8873
+ body.writeUInt32BE(this.initialTsn, 12);
8874
+ return Buffer.concat([body, encodeParams(this.params)]);
8860
8875
  }
8861
8876
  };
8862
8877
  var InitChunk = class _InitChunk extends BaseInitChunk {
@@ -8876,12 +8891,10 @@ var ForwardTsnChunk = class _ForwardTsnChunk extends Chunk {
8876
8891
  super(flags, body);
8877
8892
  this.flags = flags;
8878
8893
  if (body) {
8879
- this.cumulativeTsn = jspack6.Unpack("!L", body)[0];
8894
+ this.cumulativeTsn = body.readUInt32BE(0);
8880
8895
  let pos = 4;
8881
8896
  while (pos < body.length) {
8882
- this.streams.push(
8883
- jspack6.Unpack("!HH", body.slice(pos))
8884
- );
8897
+ this.streams.push([body.readUInt16BE(pos), body.readUInt16BE(pos + 2)]);
8885
8898
  pos += 4;
8886
8899
  }
8887
8900
  } else {
@@ -8897,12 +8910,16 @@ var ForwardTsnChunk = class _ForwardTsnChunk extends Chunk {
8897
8910
  set body(_) {
8898
8911
  }
8899
8912
  get body() {
8900
- const body = Buffer.from(jspack6.Pack("!L", [this.cumulativeTsn]));
8913
+ const body = Buffer.alloc(4);
8914
+ body.writeUInt32BE(this.cumulativeTsn, 0);
8901
8915
  return Buffer.concat([
8902
8916
  body,
8903
- ...this.streams.map(
8904
- ([id, seq]) => Buffer.from(jspack6.Pack("!HH", [id, seq]))
8905
- )
8917
+ ...this.streams.map(([id, seq]) => {
8918
+ const streamBuffer = Buffer.alloc(4);
8919
+ streamBuffer.writeUInt16BE(id, 0);
8920
+ streamBuffer.writeUInt16BE(seq, 2);
8921
+ return streamBuffer;
8922
+ })
8906
8923
  ]);
8907
8924
  }
8908
8925
  };
@@ -8911,7 +8928,10 @@ var DataChunk = class _DataChunk extends Chunk {
8911
8928
  super(flags, body);
8912
8929
  this.flags = flags;
8913
8930
  if (body) {
8914
- [this.tsn, this.streamId, this.streamSeqNum, this.protocol] = jspack6.Unpack("!LHHL", body);
8931
+ this.tsn = body.readUInt32BE(0);
8932
+ this.streamId = body.readUInt16BE(4);
8933
+ this.streamSeqNum = body.readUInt16BE(6);
8934
+ this.protocol = body.readUInt32BE(8);
8915
8935
  this.userData = body.slice(12);
8916
8936
  }
8917
8937
  }
@@ -8935,20 +8955,15 @@ var DataChunk = class _DataChunk extends Chunk {
8935
8955
  sentTime;
8936
8956
  get bytes() {
8937
8957
  const length = 16 + this.userData.length;
8938
- let data = Buffer.concat([
8939
- Buffer.from(
8940
- jspack6.Pack("!BBHLHHL", [
8941
- this.type,
8942
- this.flags,
8943
- length,
8944
- this.tsn,
8945
- this.streamId,
8946
- this.streamSeqNum,
8947
- this.protocol
8948
- ])
8949
- ),
8950
- this.userData
8951
- ]);
8958
+ const header = Buffer.alloc(16);
8959
+ header.writeUInt8(this.type, 0);
8960
+ header.writeUInt8(this.flags, 1);
8961
+ header.writeUInt16BE(length, 2);
8962
+ header.writeUInt32BE(this.tsn, 4);
8963
+ header.writeUInt16BE(this.streamId, 8);
8964
+ header.writeUInt16BE(this.streamSeqNum, 10);
8965
+ header.writeUInt32BE(this.protocol, 12);
8966
+ let data = Buffer.concat([header, this.userData]);
8952
8967
  if (length % 4) {
8953
8968
  data = Buffer.concat([
8954
8969
  data,
@@ -9041,18 +9056,17 @@ var SackChunk = class _SackChunk extends Chunk {
9041
9056
  super(flags, body);
9042
9057
  this.flags = flags;
9043
9058
  if (body) {
9044
- const [cumulativeTsn, advertisedRwnd, nbGaps, nbDuplicates] = jspack6.Unpack("!LLHH", body);
9045
- this.cumulativeTsn = cumulativeTsn;
9046
- this.advertisedRwnd = advertisedRwnd;
9059
+ this.cumulativeTsn = body.readUInt32BE(0);
9060
+ this.advertisedRwnd = body.readUInt32BE(4);
9061
+ const nbGaps = body.readUInt16BE(8);
9062
+ const nbDuplicates = body.readUInt16BE(10);
9047
9063
  let pos = 12;
9048
9064
  [...Array(nbGaps)].forEach(() => {
9049
- this.gaps.push(
9050
- jspack6.Unpack("!HH", body.slice(pos))
9051
- );
9065
+ this.gaps.push([body.readUInt16BE(pos), body.readUInt16BE(pos + 2)]);
9052
9066
  pos += 4;
9053
9067
  });
9054
9068
  [...Array(nbDuplicates)].forEach(() => {
9055
- this.duplicates.push(jspack6.Unpack("!L", body.slice(pos))[0]);
9069
+ this.duplicates.push(body.readUInt32BE(pos));
9056
9070
  pos += 4;
9057
9071
  });
9058
9072
  }
@@ -9067,24 +9081,30 @@ var SackChunk = class _SackChunk extends Chunk {
9067
9081
  advertisedRwnd = 0;
9068
9082
  get bytes() {
9069
9083
  const length = 16 + 4 * (this.gaps.length + this.duplicates.length);
9070
- let data = Buffer.from(
9071
- jspack6.Pack("!BBHLLHH", [
9072
- this.type,
9073
- this.flags,
9074
- length,
9075
- this.cumulativeTsn,
9076
- this.advertisedRwnd,
9077
- this.gaps.length,
9078
- this.duplicates.length
9079
- ])
9080
- );
9081
- data = Buffer.concat([
9082
- data,
9083
- ...this.gaps.map((gap) => Buffer.from(jspack6.Pack("!HH", gap)))
9084
+ const header = Buffer.alloc(16);
9085
+ header.writeUInt8(this.type, 0);
9086
+ header.writeUInt8(this.flags, 1);
9087
+ header.writeUInt16BE(length, 2);
9088
+ header.writeUInt32BE(this.cumulativeTsn, 4);
9089
+ header.writeUInt32BE(this.advertisedRwnd, 8);
9090
+ header.writeUInt16BE(this.gaps.length, 12);
9091
+ header.writeUInt16BE(this.duplicates.length, 14);
9092
+ let data = Buffer.concat([
9093
+ header,
9094
+ ...this.gaps.map((gap) => {
9095
+ const gapBuffer = Buffer.alloc(4);
9096
+ gapBuffer.writeUInt16BE(gap[0], 0);
9097
+ gapBuffer.writeUInt16BE(gap[1], 2);
9098
+ return gapBuffer;
9099
+ })
9084
9100
  ]);
9085
9101
  data = Buffer.concat([
9086
9102
  data,
9087
- ...this.duplicates.map((tsn) => Buffer.from(jspack6.Pack("!L", [tsn])))
9103
+ ...this.duplicates.map((tsn) => {
9104
+ const tsnBuffer = Buffer.alloc(4);
9105
+ tsnBuffer.writeUInt32BE(tsn, 0);
9106
+ return tsnBuffer;
9107
+ })
9088
9108
  ]);
9089
9109
  return data;
9090
9110
  }
@@ -9094,7 +9114,7 @@ var ShutdownChunk = class _ShutdownChunk extends Chunk {
9094
9114
  super(flags, body);
9095
9115
  this.flags = flags;
9096
9116
  if (body) {
9097
- this.cumulativeTsn = jspack6.Unpack("!L", body)[0];
9117
+ this.cumulativeTsn = body.readUInt32BE(0);
9098
9118
  }
9099
9119
  }
9100
9120
  static type = 7;
@@ -9103,7 +9123,9 @@ var ShutdownChunk = class _ShutdownChunk extends Chunk {
9103
9123
  }
9104
9124
  cumulativeTsn = 0;
9105
9125
  get body() {
9106
- return Buffer.from(jspack6.Pack("!L", [this.cumulativeTsn]));
9126
+ const body = Buffer.alloc(4);
9127
+ body.writeUInt32BE(this.cumulativeTsn, 0);
9128
+ return body;
9107
9129
  }
9108
9130
  };
9109
9131
  var ShutdownAckChunk = class _ShutdownAckChunk extends Chunk {
@@ -9151,12 +9173,10 @@ function encodeParams(params) {
9151
9173
  let padding = Buffer.from("");
9152
9174
  params.forEach(([type, value]) => {
9153
9175
  const length = value.length + 4;
9154
- body = Buffer.concat([
9155
- body,
9156
- padding,
9157
- Buffer.from(jspack6.Pack("!HH", [type, length])),
9158
- value
9159
- ]);
9176
+ const paramHeader = Buffer.alloc(4);
9177
+ paramHeader.writeUInt16BE(type, 0);
9178
+ paramHeader.writeUInt16BE(length, 2);
9179
+ body = Buffer.concat([body, padding, paramHeader, value]);
9160
9180
  padding = Buffer.concat(
9161
9181
  [...Array(padL(length))].map(() => Buffer.from("\0"))
9162
9182
  );
@@ -9167,7 +9187,8 @@ function decodeParams(body) {
9167
9187
  const params = [];
9168
9188
  let pos = 0;
9169
9189
  while (pos <= body.length - 4) {
9170
- const [type, length] = jspack6.Unpack("!HH", body.slice(pos));
9190
+ const type = body.readUInt16BE(pos);
9191
+ const length = body.readUInt16BE(pos + 2);
9171
9192
  params.push([type, body.slice(pos + 4, pos + length)]);
9172
9193
  pos += length + padL(length);
9173
9194
  }
@@ -9175,8 +9196,7 @@ function decodeParams(body) {
9175
9196
  }
9176
9197
 
9177
9198
  // ../sctp/src/param.ts
9178
- import { jspack as jspack7 } from "@shinyoshiaki/jspack";
9179
- import range4 from "lodash/range.js";
9199
+ import { jspack as jspack4 } from "@shinyoshiaki/jspack";
9180
9200
 
9181
9201
  // ../sctp/src/sctp.ts
9182
9202
  var log31 = debug("werift/sctp/sctp");
@@ -9194,41 +9214,56 @@ var log33 = debug("werift:packages/webrtc/src/media/rtpReceiver.ts");
9194
9214
  var log34 = debug("werift:packages/webrtc/src/media/router.ts");
9195
9215
 
9196
9216
  // src/media/rtpSender.ts
9197
- import { jspack as jspack10 } from "@shinyoshiaki/jspack";
9198
- import * as uuid4 from "uuid";
9217
+ import { jspack as jspack7 } from "@shinyoshiaki/jspack";
9199
9218
  var log35 = debug("werift:packages/webrtc/src/media/rtpSender.ts");
9200
9219
 
9220
+ // src/sctpManager.ts
9221
+ var log36 = debug("werift:packages/webrtc/src/transport/sctpManager.ts");
9222
+
9223
+ // src/secureTransportManager.ts
9224
+ var log37 = debug(
9225
+ "werift:packages/webrtc/src/transport/secureTransportManager.ts"
9226
+ );
9227
+ var srtpProfiles = [
9228
+ SRTP_PROFILE.SRTP_AEAD_AES_128_GCM,
9229
+ // prefer
9230
+ SRTP_PROFILE.SRTP_AES128_CM_HMAC_SHA1_80
9231
+ ];
9232
+
9201
9233
  // src/peerConnection.ts
9202
- import cloneDeep from "lodash/cloneDeep.js";
9203
- import isEqual2 from "lodash/isEqual.js";
9204
- import * as uuid5 from "uuid";
9205
- var log36 = debug("werift:packages/webrtc/src/peerConnection.ts");
9206
- var defaultPeerConfig = {
9207
- codecs: {
9208
- audio: [useOPUS(), usePCMU()],
9209
- video: [useVP8()]
9210
- },
9211
- headerExtensions: {
9212
- audio: [],
9213
- video: []
9214
- },
9215
- iceTransportPolicy: "all",
9216
- iceServers: [{ urls: "stun:stun.l.google.com:19302" }],
9217
- icePortRange: void 0,
9218
- iceInterfaceAddresses: void 0,
9219
- iceAdditionalHostAddresses: void 0,
9220
- iceUseIpv4: true,
9221
- iceUseIpv6: true,
9222
- iceFilterStunResponse: void 0,
9223
- iceFilterCandidatePair: void 0,
9224
- icePasswordPrefix: void 0,
9225
- iceUseLinkLocalAddress: void 0,
9226
- dtls: {},
9227
- bundlePolicy: "max-compat",
9228
- debug: {},
9229
- midSuffix: false,
9230
- forceTurnTCP: false
9231
- };
9234
+ var log38 = debug("werift:packages/webrtc/src/peerConnection.ts");
9235
+ function generateDefaultPeerConfig() {
9236
+ return {
9237
+ codecs: {
9238
+ audio: [useOPUS(), usePCMU()],
9239
+ video: [useVP8()]
9240
+ },
9241
+ headerExtensions: {
9242
+ audio: [],
9243
+ video: []
9244
+ },
9245
+ iceTransportPolicy: "all",
9246
+ iceServers: [{ urls: "stun:stun.l.google.com:19302" }],
9247
+ icePortRange: void 0,
9248
+ iceInterfaceAddresses: void 0,
9249
+ iceAdditionalHostAddresses: void 0,
9250
+ iceUseIpv4: true,
9251
+ iceUseIpv6: true,
9252
+ iceFilterStunResponse: void 0,
9253
+ iceFilterCandidatePair: void 0,
9254
+ icePasswordPrefix: void 0,
9255
+ iceUseLinkLocalAddress: void 0,
9256
+ dtls: {},
9257
+ bundlePolicy: "max-compat",
9258
+ debug: {},
9259
+ midSuffix: false,
9260
+ forceTurnTCP: false
9261
+ };
9262
+ }
9263
+ var defaultPeerConfig = generateDefaultPeerConfig();
9264
+
9265
+ // src/transceiverManager.ts
9266
+ var log39 = debug("werift:packages/webrtc/src/media/rtpTransceiverManager.ts");
9232
9267
 
9233
9268
  // src/nonstandard/navigator.ts
9234
9269
  var Navigator = class {
@@ -9251,7 +9286,7 @@ var MediaDevices = class extends EventTarget {
9251
9286
  if (video) {
9252
9287
  this.video?.onReceiveRtp.subscribe((rtp) => {
9253
9288
  const cloned = rtp.clone();
9254
- cloned.header.ssrc = jspack11.Unpack("!L", randomBytes4(4))[0];
9289
+ cloned.header.ssrc = jspack8.Unpack("!L", randomBytes5(4))[0];
9255
9290
  video.onReceiveRtp.execute(cloned);
9256
9291
  });
9257
9292
  }
@@ -9259,7 +9294,7 @@ var MediaDevices = class extends EventTarget {
9259
9294
  if (audio) {
9260
9295
  this.audio?.onReceiveRtp.subscribe((rtp) => {
9261
9296
  const cloned = rtp.clone();
9262
- cloned.header.ssrc = jspack11.Unpack("!L", randomBytes4(4))[0];
9297
+ cloned.header.ssrc = jspack8.Unpack("!L", randomBytes5(4))[0];
9263
9298
  audio.onReceiveRtp.execute(cloned);
9264
9299
  });
9265
9300
  }
@@ -9490,9 +9525,9 @@ var MediaRecorder = class {
9490
9525
 
9491
9526
  // src/nonstandard/userMedia.ts
9492
9527
  import { exec } from "child_process";
9528
+ import { randomUUID as randomUUID6 } from "crypto";
9493
9529
  import { createSocket as createSocket4 } from "dgram";
9494
9530
  import { setImmediate as setImmediate2 } from "timers/promises";
9495
- import { v4 as v48 } from "uuid";
9496
9531
  var getUserMedia = async ({
9497
9532
  path: path2,
9498
9533
  loop,
@@ -9527,7 +9562,7 @@ var MediaPlayer = class {
9527
9562
  this.setupTrack(props.audioPort, this.audio);
9528
9563
  this.setupTrack(props.videoPort, this.video);
9529
9564
  }
9530
- streamId = v48();
9565
+ streamId = randomUUID6().toString();
9531
9566
  audio = new MediaStreamTrack({ kind: "audio", streamId: this.streamId });
9532
9567
  video = new MediaStreamTrack({ kind: "video", streamId: this.streamId });
9533
9568
  process;
@@ -9712,10 +9747,12 @@ export {
9712
9747
  createBufferWriter,
9713
9748
  dePacketizeRtpPackets,
9714
9749
  debug,
9750
+ decodeVintEncodedNumber,
9715
9751
  depacketizeTransformer,
9716
9752
  depacketizerCodecs,
9717
9753
  deserializeAbsSendTime,
9718
9754
  deserializeAudioLevelIndication,
9755
+ deserializeSimpleBlocks,
9719
9756
  deserializeString,
9720
9757
  deserializeUint16BE,
9721
9758
  deserializeVideoOrientation,
@@ -9761,6 +9798,7 @@ export {
9761
9798
  serializeRepairedRtpStreamId,
9762
9799
  serializeSdesMid,
9763
9800
  serializeSdesRTPStreamID,
9801
+ serializeSimpleBlock,
9764
9802
  serializeTransportWideCC,
9765
9803
  string,
9766
9804
  stringToByteArray,
@@ -9775,6 +9813,7 @@ export {
9775
9813
  uint8Add,
9776
9814
  unknownSizeElement,
9777
9815
  unwrapRtx,
9816
+ vintDecode,
9778
9817
  vintEncode,
9779
9818
  vintEncodedNumber,
9780
9819
  wrapRtx