werift 0.25.2 → 0.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (568) hide show
  1. package/README.md +63 -0
  2. package/THIRD_PARTY_LICENSES_LIBWEBRTC.txt +58 -0
  3. package/lib/common/src/asn1.d.ts +42 -0
  4. package/lib/common/src/asn1.js +163 -0
  5. package/lib/common/src/asn1.js.map +1 -0
  6. package/lib/common/src/binary.d.ts +1 -3
  7. package/lib/common/src/binary.js +8 -6
  8. package/lib/common/src/binary.js.map +1 -1
  9. package/lib/common/src/binaryData.js +2 -1
  10. package/lib/common/src/binaryData.js.map +1 -1
  11. package/lib/common/src/index.d.ts +6 -5
  12. package/lib/common/src/index.js +6 -5
  13. package/lib/common/src/index.js.map +1 -1
  14. package/lib/common/src/log.d.ts +12 -1
  15. package/lib/common/src/log.js +142 -12
  16. package/lib/common/src/log.js.map +1 -1
  17. package/lib/common/src/network.js.map +1 -1
  18. package/lib/common/src/queue.d.ts +41 -0
  19. package/lib/common/src/queue.js +146 -0
  20. package/lib/common/src/queue.js.map +1 -0
  21. package/lib/common/src/transport.d.ts +15 -2
  22. package/lib/common/src/transport.js +108 -19
  23. package/lib/common/src/transport.js.map +1 -1
  24. package/lib/dtls/src/cipher/create.js.map +1 -1
  25. package/lib/dtls/src/cipher/ec.js +8 -8
  26. package/lib/dtls/src/cipher/ec.js.map +1 -1
  27. package/lib/dtls/src/cipher/namedCurve.js +6 -7
  28. package/lib/dtls/src/cipher/namedCurve.js.map +1 -1
  29. package/lib/dtls/src/cipher/prf.d.ts +2 -2
  30. package/lib/dtls/src/cipher/prf.js +29 -8
  31. package/lib/dtls/src/cipher/prf.js.map +1 -1
  32. package/lib/dtls/src/cipher/suites/aead.d.ts +1 -1
  33. package/lib/dtls/src/context/certificate.d.ts +32 -0
  34. package/lib/dtls/src/context/certificate.js +54 -0
  35. package/lib/dtls/src/context/certificate.js.map +1 -0
  36. package/lib/dtls/src/context/cipher.d.ts +13 -7
  37. package/lib/dtls/src/context/cipher.js +58 -106
  38. package/lib/dtls/src/context/cipher.js.map +1 -1
  39. package/lib/dtls/src/context/dtls.js +4 -2
  40. package/lib/dtls/src/context/dtls.js.map +1 -1
  41. package/lib/dtls/src/context/transport.d.ts +4 -0
  42. package/lib/dtls/src/context/transport.js +16 -1
  43. package/lib/dtls/src/context/transport.js.map +1 -1
  44. package/lib/dtls/src/flight/client/flight1.js +2 -1
  45. package/lib/dtls/src/flight/client/flight1.js.map +1 -1
  46. package/lib/dtls/src/flight/client/flight5.js +4 -2
  47. package/lib/dtls/src/flight/client/flight5.js.map +1 -1
  48. package/lib/dtls/src/flight/flight.d.ts +1 -1
  49. package/lib/dtls/src/flight/flight.js +16 -5
  50. package/lib/dtls/src/flight/flight.js.map +1 -1
  51. package/lib/dtls/src/flight/server/flight2.js.map +1 -1
  52. package/lib/dtls/src/flight/server/flight4.d.ts +2 -1
  53. package/lib/dtls/src/flight/server/flight4.js +12 -4
  54. package/lib/dtls/src/flight/server/flight4.js.map +1 -1
  55. package/lib/dtls/src/flight/server/flight6.js +2 -1
  56. package/lib/dtls/src/flight/server/flight6.js.map +1 -1
  57. package/lib/dtls/src/handshake/const.d.ts +17 -13
  58. package/lib/dtls/src/handshake/const.js +16 -14
  59. package/lib/dtls/src/handshake/const.js.map +1 -1
  60. package/lib/dtls/src/handshake/extensions/signature.d.ts +1 -1
  61. package/lib/dtls/src/handshake/extensions/signature.js.map +1 -1
  62. package/lib/dtls/src/handshake/message/alert.js +5 -3
  63. package/lib/dtls/src/handshake/message/alert.js.map +1 -1
  64. package/lib/dtls/src/handshake/message/certificate.js +3 -2
  65. package/lib/dtls/src/handshake/message/certificate.js.map +1 -1
  66. package/lib/dtls/src/handshake/message/changeCipherSpec.js +4 -4
  67. package/lib/dtls/src/handshake/message/changeCipherSpec.js.map +1 -1
  68. package/lib/dtls/src/handshake/message/client/certificateVerify.d.ts +1 -1
  69. package/lib/dtls/src/handshake/message/client/certificateVerify.js +5 -3
  70. package/lib/dtls/src/handshake/message/client/certificateVerify.js.map +1 -1
  71. package/lib/dtls/src/handshake/message/client/hello.js +15 -8
  72. package/lib/dtls/src/handshake/message/client/hello.js.map +1 -1
  73. package/lib/dtls/src/handshake/message/client/keyExchange.js +3 -2
  74. package/lib/dtls/src/handshake/message/client/keyExchange.js.map +1 -1
  75. package/lib/dtls/src/handshake/message/finished.js +2 -1
  76. package/lib/dtls/src/handshake/message/finished.js.map +1 -1
  77. package/lib/dtls/src/handshake/message/server/certificateRequest.d.ts +1 -1
  78. package/lib/dtls/src/handshake/message/server/certificateRequest.js +7 -4
  79. package/lib/dtls/src/handshake/message/server/certificateRequest.js.map +1 -1
  80. package/lib/dtls/src/handshake/message/server/hello.d.ts +1 -1
  81. package/lib/dtls/src/handshake/message/server/hello.js +14 -8
  82. package/lib/dtls/src/handshake/message/server/hello.js.map +1 -1
  83. package/lib/dtls/src/handshake/message/server/helloDone.js +1 -1
  84. package/lib/dtls/src/handshake/message/server/helloDone.js.map +1 -1
  85. package/lib/dtls/src/handshake/message/server/helloVerifyRequest.js +5 -3
  86. package/lib/dtls/src/handshake/message/server/helloVerifyRequest.js.map +1 -1
  87. package/lib/dtls/src/handshake/message/server/keyExchange.d.ts +1 -1
  88. package/lib/dtls/src/handshake/message/server/keyExchange.js +17 -9
  89. package/lib/dtls/src/handshake/message/server/keyExchange.js.map +1 -1
  90. package/lib/dtls/src/handshake/random.d.ts +1 -1
  91. package/lib/dtls/src/handshake/random.js +7 -4
  92. package/lib/dtls/src/handshake/random.js.map +1 -1
  93. package/lib/dtls/src/imports/common.d.ts +1 -0
  94. package/lib/dtls/src/imports/common.js +1 -0
  95. package/lib/dtls/src/imports/common.js.map +1 -1
  96. package/lib/dtls/src/index.d.ts +2 -2
  97. package/lib/dtls/src/index.js +2 -2
  98. package/lib/dtls/src/index.js.map +1 -1
  99. package/lib/dtls/src/record/const.d.ts +43 -33
  100. package/lib/dtls/src/record/const.js +43 -36
  101. package/lib/dtls/src/record/const.js.map +1 -1
  102. package/lib/dtls/src/record/message/fragment.d.ts +1 -1
  103. package/lib/dtls/src/record/message/fragment.js +15 -9
  104. package/lib/dtls/src/record/message/fragment.js.map +1 -1
  105. package/lib/dtls/src/record/message/header.js +12 -7
  106. package/lib/dtls/src/record/message/header.js.map +1 -1
  107. package/lib/dtls/src/record/message/plaintext.js +4 -2
  108. package/lib/dtls/src/record/message/plaintext.js.map +1 -1
  109. package/lib/dtls/src/record/receive.js +2 -2
  110. package/lib/dtls/src/record/receive.js.map +1 -1
  111. package/lib/dtls/src/server.js +1 -1
  112. package/lib/dtls/src/server.js.map +1 -1
  113. package/lib/dtls/src/socket.d.ts +2 -2
  114. package/lib/dtls/src/socket.js +6 -4
  115. package/lib/dtls/src/socket.js.map +1 -1
  116. package/lib/ice/src/candidate.d.ts +1 -1
  117. package/lib/ice/src/candidate.js +24 -12
  118. package/lib/ice/src/candidate.js.map +1 -1
  119. package/lib/ice/src/dns/lookup.d.ts +10 -1
  120. package/lib/ice/src/dns/lookup.js +42 -7
  121. package/lib/ice/src/dns/lookup.js.map +1 -1
  122. package/lib/ice/src/exceptions.d.ts +46 -0
  123. package/lib/ice/src/exceptions.js +92 -5
  124. package/lib/ice/src/exceptions.js.map +1 -1
  125. package/lib/ice/src/helper.d.ts +1 -1
  126. package/lib/ice/src/ice.d.ts +191 -5
  127. package/lib/ice/src/ice.js +1048 -156
  128. package/lib/ice/src/ice.js.map +1 -1
  129. package/lib/ice/src/iceBase.d.ts +174 -10
  130. package/lib/ice/src/iceBase.js +165 -12
  131. package/lib/ice/src/iceBase.js.map +1 -1
  132. package/lib/ice/src/index.d.ts +5 -5
  133. package/lib/ice/src/index.js +5 -5
  134. package/lib/ice/src/index.js.map +1 -1
  135. package/lib/ice/src/internal/resolveHostAddresses.d.ts +26 -0
  136. package/lib/ice/src/internal/resolveHostAddresses.js +45 -0
  137. package/lib/ice/src/internal/resolveHostAddresses.js.map +1 -0
  138. package/lib/ice/src/stun/protocol.d.ts +3 -1
  139. package/lib/ice/src/stun/protocol.js +16 -0
  140. package/lib/ice/src/stun/protocol.js.map +1 -1
  141. package/lib/ice/src/stun/tcpProtocol.d.ts +2 -0
  142. package/lib/ice/src/stun/tcpProtocol.js +16 -1
  143. package/lib/ice/src/stun/tcpProtocol.js.map +1 -1
  144. package/lib/ice/src/stun/transaction.d.ts +35 -1
  145. package/lib/ice/src/stun/transaction.js +93 -18
  146. package/lib/ice/src/stun/transaction.js.map +1 -1
  147. package/lib/ice/src/turn/credential.d.ts +49 -0
  148. package/lib/ice/src/turn/credential.js +78 -0
  149. package/lib/ice/src/turn/credential.js.map +1 -0
  150. package/lib/ice/src/turn/protocol.d.ts +97 -7
  151. package/lib/ice/src/turn/protocol.js +357 -55
  152. package/lib/ice/src/turn/protocol.js.map +1 -1
  153. package/lib/ice/src/utils.js.map +1 -1
  154. package/lib/ice-server/src/stun/attributes.d.ts +2 -0
  155. package/lib/ice-server/src/stun/attributes.js +7 -1
  156. package/lib/ice-server/src/stun/attributes.js.map +1 -1
  157. package/lib/ice-server/src/stun/const.d.ts +24 -16
  158. package/lib/ice-server/src/stun/const.js +22 -18
  159. package/lib/ice-server/src/stun/const.js.map +1 -1
  160. package/lib/ice-server/src/stun/message.d.ts +3 -1
  161. package/lib/ice-server/src/stun/message.js +41 -6
  162. package/lib/ice-server/src/stun/message.js.map +1 -1
  163. package/lib/ice-server/src/turn/auth.d.ts +1 -1
  164. package/lib/index.mjs +25252 -12664
  165. package/lib/nonstandard/index.mjs +11015 -8906
  166. package/lib/polyfill/dom.mjs +30779 -18386
  167. package/lib/polyfill/index.mjs +30779 -18386
  168. package/lib/rtp/src/codec/assertPayload.d.ts +4 -0
  169. package/lib/rtp/src/codec/assertPayload.js +12 -0
  170. package/lib/rtp/src/codec/assertPayload.js.map +1 -0
  171. package/lib/rtp/src/codec/av1.js +2 -0
  172. package/lib/rtp/src/codec/av1.js.map +1 -1
  173. package/lib/rtp/src/codec/h264.d.ts +1 -1
  174. package/lib/rtp/src/codec/h264.js +35 -43
  175. package/lib/rtp/src/codec/h264.js.map +1 -1
  176. package/lib/rtp/src/codec/vp8.js +11 -5
  177. package/lib/rtp/src/codec/vp8.js.map +1 -1
  178. package/lib/rtp/src/codec/vp9.js +12 -0
  179. package/lib/rtp/src/codec/vp9.js.map +1 -1
  180. package/lib/rtp/src/extra/container/mp4/container.js +2 -1
  181. package/lib/rtp/src/extra/container/mp4/container.js.map +1 -1
  182. package/lib/rtp/src/extra/container/mp4/exp-golomb.js +2 -1
  183. package/lib/rtp/src/extra/container/mp4/exp-golomb.js.map +1 -1
  184. package/lib/rtp/src/extra/container/mp4/h264.d.ts +21 -17
  185. package/lib/rtp/src/extra/container/mp4/h264.js +26 -24
  186. package/lib/rtp/src/extra/container/mp4/h264.js.map +1 -1
  187. package/lib/rtp/src/extra/container/ogg/index.d.ts +1 -0
  188. package/lib/rtp/src/extra/container/ogg/index.js +1 -0
  189. package/lib/rtp/src/extra/container/ogg/index.js.map +1 -1
  190. package/lib/rtp/src/extra/container/ogg/writer.d.ts +90 -0
  191. package/lib/rtp/src/extra/container/ogg/writer.js +300 -0
  192. package/lib/rtp/src/extra/container/ogg/writer.js.map +1 -0
  193. package/lib/rtp/src/extra/container/webm/container.d.ts +1 -1
  194. package/lib/rtp/src/extra/container/webm/ebml/ebml.d.ts +6 -0
  195. package/lib/rtp/src/extra/container/webm/ebml/ebml.js +26 -4
  196. package/lib/rtp/src/extra/container/webm/ebml/ebml.js.map +1 -1
  197. package/lib/rtp/src/extra/processor/depacketizer.d.ts +6 -1
  198. package/lib/rtp/src/extra/processor/depacketizer.js +29 -3
  199. package/lib/rtp/src/extra/processor/depacketizer.js.map +1 -1
  200. package/lib/rtp/src/extra/processor/depacketizerCallback.d.ts +1 -11
  201. package/lib/rtp/src/extra/processor/depacketizerTransformer.d.ts +1 -1
  202. package/lib/rtp/src/extra/processor/dtx.js +4 -2
  203. package/lib/rtp/src/extra/processor/dtx.js.map +1 -1
  204. package/lib/rtp/src/extra/processor/dtxCallback.d.ts +1 -11
  205. package/lib/rtp/src/extra/processor/index.d.ts +2 -0
  206. package/lib/rtp/src/extra/processor/index.js +2 -0
  207. package/lib/rtp/src/extra/processor/index.js.map +1 -1
  208. package/lib/rtp/src/extra/processor/interface.d.ts +16 -11
  209. package/lib/rtp/src/extra/processor/interface.js.map +1 -1
  210. package/lib/rtp/src/extra/processor/jitterBuffer.d.ts +29 -6
  211. package/lib/rtp/src/extra/processor/jitterBuffer.js +101 -111
  212. package/lib/rtp/src/extra/processor/jitterBuffer.js.map +1 -1
  213. package/lib/rtp/src/extra/processor/jitterBufferCallback.d.ts +1 -11
  214. package/lib/rtp/src/extra/processor/jitterBufferTransformer.d.ts +1 -1
  215. package/lib/rtp/src/extra/processor/jitterBufferTransformer.js +2 -1
  216. package/lib/rtp/src/extra/processor/jitterBufferTransformer.js.map +1 -1
  217. package/lib/rtp/src/extra/processor/lipsync.d.ts +63 -0
  218. package/lib/rtp/src/extra/processor/lipsync.js +202 -51
  219. package/lib/rtp/src/extra/processor/lipsync.js.map +1 -1
  220. package/lib/rtp/src/extra/processor/mp4.d.ts +0 -6
  221. package/lib/rtp/src/extra/processor/mp4.js +7 -4
  222. package/lib/rtp/src/extra/processor/mp4.js.map +1 -1
  223. package/lib/rtp/src/extra/processor/mute.js +4 -2
  224. package/lib/rtp/src/extra/processor/mute.js.map +1 -1
  225. package/lib/rtp/src/extra/processor/nack.d.ts +1 -1
  226. package/lib/rtp/src/extra/processor/nack.js +5 -3
  227. package/lib/rtp/src/extra/processor/nack.js.map +1 -1
  228. package/lib/rtp/src/extra/processor/nackHandlerCallback.d.ts +1 -11
  229. package/lib/rtp/src/extra/processor/ntpTime.js +4 -6
  230. package/lib/rtp/src/extra/processor/ntpTime.js.map +1 -1
  231. package/lib/rtp/src/extra/processor/ntpTimeCallback.d.ts +1 -11
  232. package/lib/rtp/src/extra/processor/ogg.d.ts +72 -0
  233. package/lib/rtp/src/extra/processor/ogg.js +221 -0
  234. package/lib/rtp/src/extra/processor/ogg.js.map +1 -0
  235. package/lib/rtp/src/extra/processor/oggCallback.d.ts +21 -0
  236. package/lib/rtp/src/extra/processor/oggCallback.js +97 -0
  237. package/lib/rtp/src/extra/processor/oggCallback.js.map +1 -0
  238. package/lib/rtp/src/extra/processor/rtcpCallback.d.ts +1 -1
  239. package/lib/rtp/src/extra/processor/rtcpCallback.js.map +1 -1
  240. package/lib/rtp/src/extra/processor/rtpCallback.d.ts +1 -1
  241. package/lib/rtp/src/extra/processor/rtpCallback.js +3 -2
  242. package/lib/rtp/src/extra/processor/rtpCallback.js.map +1 -1
  243. package/lib/rtp/src/extra/processor/rtpStream.d.ts +2 -2
  244. package/lib/rtp/src/extra/processor/rtpStream.js +2 -1
  245. package/lib/rtp/src/extra/processor/rtpStream.js.map +1 -1
  246. package/lib/rtp/src/extra/processor/rtpTime.js +2 -1
  247. package/lib/rtp/src/extra/processor/rtpTime.js.map +1 -1
  248. package/lib/rtp/src/extra/processor/rtpTimeCallback.d.ts +1 -11
  249. package/lib/rtp/src/extra/processor/webm.d.ts +27 -0
  250. package/lib/rtp/src/extra/processor/webm.js +57 -23
  251. package/lib/rtp/src/extra/processor/webm.js.map +1 -1
  252. package/lib/rtp/src/extra/processor/webmCallback.js +6 -1
  253. package/lib/rtp/src/extra/processor/webmCallback.js.map +1 -1
  254. package/lib/rtp/src/extra/processor/webmStream.js.map +1 -1
  255. package/lib/rtp/src/helper.d.ts +2 -2
  256. package/lib/rtp/src/helper.js +2 -6
  257. package/lib/rtp/src/helper.js.map +1 -1
  258. package/lib/rtp/src/index.d.ts +10 -2
  259. package/lib/rtp/src/index.js +10 -2
  260. package/lib/rtp/src/index.js.map +1 -1
  261. package/lib/rtp/src/rtcp/psfb/fullIntraRequest.d.ts +4 -0
  262. package/lib/rtp/src/rtcp/psfb/fullIntraRequest.js +6 -1
  263. package/lib/rtp/src/rtcp/psfb/fullIntraRequest.js.map +1 -1
  264. package/lib/rtp/src/rtcp/rtcp.js +9 -4
  265. package/lib/rtp/src/rtcp/rtcp.js.map +1 -1
  266. package/lib/rtp/src/rtcp/rtpfb/transportWideSequence.d.ts +58 -0
  267. package/lib/rtp/src/rtcp/rtpfb/transportWideSequence.js +143 -0
  268. package/lib/rtp/src/rtcp/rtpfb/transportWideSequence.js.map +1 -0
  269. package/lib/rtp/src/rtcp/rtpfb/twcc.d.ts +35 -13
  270. package/lib/rtp/src/rtcp/rtpfb/twcc.js +132 -54
  271. package/lib/rtp/src/rtcp/rtpfb/twcc.js.map +1 -1
  272. package/lib/rtp/src/rtcp/rtpfb/twccReceiveTiming.d.ts +13 -0
  273. package/lib/rtp/src/rtcp/rtpfb/twccReceiveTiming.js +29 -0
  274. package/lib/rtp/src/rtcp/rtpfb/twccReceiveTiming.js.map +1 -0
  275. package/lib/rtp/src/rtcp/rtpfb/twccReferenceTime.d.ts +41 -0
  276. package/lib/rtp/src/rtcp/rtpfb/twccReferenceTime.js +89 -0
  277. package/lib/rtp/src/rtcp/rtpfb/twccReferenceTime.js.map +1 -0
  278. package/lib/rtp/src/rtcp/sr.d.ts +4 -3
  279. package/lib/rtp/src/rtcp/sr.js +4 -2
  280. package/lib/rtp/src/rtcp/sr.js.map +1 -1
  281. package/lib/rtp/src/rtp/continuity.d.ts +36 -0
  282. package/lib/rtp/src/rtp/continuity.js +100 -0
  283. package/lib/rtp/src/rtp/continuity.js.map +1 -0
  284. package/lib/rtp/src/rtp/headerExtension.js +41 -21
  285. package/lib/rtp/src/rtp/headerExtension.js.map +1 -1
  286. package/lib/rtp/src/rtp/padding.d.ts +17 -0
  287. package/lib/rtp/src/rtp/padding.js +37 -0
  288. package/lib/rtp/src/rtp/padding.js.map +1 -0
  289. package/lib/rtp/src/rtp/red/encoder.d.ts +10 -0
  290. package/lib/rtp/src/rtp/red/encoder.js +14 -1
  291. package/lib/rtp/src/rtp/red/encoder.js.map +1 -1
  292. package/lib/rtp/src/rtp/red/packet.js +24 -8
  293. package/lib/rtp/src/rtp/red/packet.js.map +1 -1
  294. package/lib/rtp/src/rtp/rtp.d.ts +24 -1
  295. package/lib/rtp/src/rtp/rtp.js +70 -29
  296. package/lib/rtp/src/rtp/rtp.js.map +1 -1
  297. package/lib/rtp/src/rtp/rtx.js +3 -0
  298. package/lib/rtp/src/rtp/rtx.js.map +1 -1
  299. package/lib/rtp/src/rtp/sendHeaderExtensions.d.ts +36 -0
  300. package/lib/rtp/src/rtp/sendHeaderExtensions.js +85 -0
  301. package/lib/rtp/src/rtp/sendHeaderExtensions.js.map +1 -0
  302. package/lib/rtp/src/srtp/cipher/ctr.d.ts +2 -2
  303. package/lib/rtp/src/srtp/cipher/ctr.js +5 -3
  304. package/lib/rtp/src/srtp/cipher/ctr.js.map +1 -1
  305. package/lib/rtp/src/srtp/cipher/gcm.d.ts +1 -1
  306. package/lib/rtp/src/srtp/cipher/gcm.js +1 -1
  307. package/lib/rtp/src/srtp/cipher/gcm.js.map +1 -1
  308. package/lib/rtp/src/srtp/cipher/index.js +8 -4
  309. package/lib/rtp/src/srtp/cipher/index.js.map +1 -1
  310. package/lib/rtp/src/srtp/context/context.d.ts +29 -1
  311. package/lib/rtp/src/srtp/context/context.js +84 -27
  312. package/lib/rtp/src/srtp/context/context.js.map +1 -1
  313. package/lib/rtp/src/srtp/context/srtcp.d.ts +1 -1
  314. package/lib/rtp/src/srtp/context/srtcp.js +33 -0
  315. package/lib/rtp/src/srtp/context/srtcp.js.map +1 -1
  316. package/lib/rtp/src/srtp/context/srtp.d.ts +1 -1
  317. package/lib/rtp/src/srtp/context/srtp.js +36 -4
  318. package/lib/rtp/src/srtp/context/srtp.js.map +1 -1
  319. package/lib/rtp/src/srtp/error.d.ts +8 -0
  320. package/lib/rtp/src/srtp/error.js +13 -1
  321. package/lib/rtp/src/srtp/error.js.map +1 -1
  322. package/lib/rtp/src/srtp/packet.js +4 -1
  323. package/lib/rtp/src/srtp/packet.js.map +1 -1
  324. package/lib/rtp/src/srtp/replay.d.ts +24 -0
  325. package/lib/rtp/src/srtp/replay.js +75 -0
  326. package/lib/rtp/src/srtp/replay.js.map +1 -0
  327. package/lib/rtp/src/srtp/session.js +2 -1
  328. package/lib/rtp/src/srtp/session.js.map +1 -1
  329. package/lib/rtp/src/srtp/srtcp.js +2 -1
  330. package/lib/rtp/src/srtp/srtcp.js.map +1 -1
  331. package/lib/rtp/src/srtp/srtp.js +2 -1
  332. package/lib/rtp/src/srtp/srtp.js.map +1 -1
  333. package/lib/rtp/src/util.js +2 -1
  334. package/lib/rtp/src/util.js.map +1 -1
  335. package/lib/sctp/src/chunk.d.ts +5 -0
  336. package/lib/sctp/src/chunk.js +31 -8
  337. package/lib/sctp/src/chunk.js.map +1 -1
  338. package/lib/sctp/src/const.d.ts +25 -17
  339. package/lib/sctp/src/const.js +23 -19
  340. package/lib/sctp/src/const.js.map +1 -1
  341. package/lib/sctp/src/index.d.ts +4 -3
  342. package/lib/sctp/src/index.js +10 -8
  343. package/lib/sctp/src/index.js.map +1 -1
  344. package/lib/sctp/src/param.d.ts +6 -2
  345. package/lib/sctp/src/param.js +22 -8
  346. package/lib/sctp/src/param.js.map +1 -1
  347. package/lib/sctp/src/sctp.d.ts +91 -2
  348. package/lib/sctp/src/sctp.js +700 -134
  349. package/lib/sctp/src/sctp.js.map +1 -1
  350. package/lib/sctp/src/transport.js +4 -2
  351. package/lib/sctp/src/transport.js.map +1 -1
  352. package/lib/webrtc/src/api/peerConfig.d.ts +307 -0
  353. package/lib/webrtc/src/api/peerConfig.js +291 -0
  354. package/lib/webrtc/src/api/peerConfig.js.map +1 -0
  355. package/lib/webrtc/src/api/peerConnectionEvents.d.ts +36 -0
  356. package/lib/webrtc/src/api/peerConnectionEvents.js +44 -0
  357. package/lib/webrtc/src/api/peerConnectionEvents.js.map +1 -0
  358. package/lib/webrtc/src/dataChannel.d.ts +11 -1
  359. package/lib/webrtc/src/dataChannel.js +25 -5
  360. package/lib/webrtc/src/dataChannel.js.map +1 -1
  361. package/lib/webrtc/src/index.d.ts +2 -2
  362. package/lib/webrtc/src/index.js +2 -2
  363. package/lib/webrtc/src/index.js.map +1 -1
  364. package/lib/webrtc/src/media/index.d.ts +11 -1
  365. package/lib/webrtc/src/media/index.js +71 -1
  366. package/lib/webrtc/src/media/index.js.map +1 -1
  367. package/lib/webrtc/src/media/receiver/nack.js +5 -5
  368. package/lib/webrtc/src/media/receiver/nack.js.map +1 -1
  369. package/lib/webrtc/src/media/receiver/receiverTwcc.d.ts +61 -1
  370. package/lib/webrtc/src/media/receiver/receiverTwcc.js +218 -63
  371. package/lib/webrtc/src/media/receiver/receiverTwcc.js.map +1 -1
  372. package/lib/webrtc/src/media/router.d.ts +130 -6
  373. package/lib/webrtc/src/media/router.js +325 -29
  374. package/lib/webrtc/src/media/router.js.map +1 -1
  375. package/lib/webrtc/src/media/rtpReceiver.d.ts +137 -14
  376. package/lib/webrtc/src/media/rtpReceiver.js +420 -44
  377. package/lib/webrtc/src/media/rtpReceiver.js.map +1 -1
  378. package/lib/webrtc/src/media/rtpSender.d.ts +232 -65
  379. package/lib/webrtc/src/media/rtpSender.js +784 -448
  380. package/lib/webrtc/src/media/rtpSender.js.map +1 -1
  381. package/lib/webrtc/src/media/rtpTransceiver.d.ts +95 -4
  382. package/lib/webrtc/src/media/rtpTransceiver.js +121 -9
  383. package/lib/webrtc/src/media/rtpTransceiver.js.map +1 -1
  384. package/lib/webrtc/src/media/sender/bandwidthEstimator.d.ts +249 -0
  385. package/lib/webrtc/src/media/sender/bandwidthEstimator.js +144 -0
  386. package/lib/webrtc/src/media/sender/bandwidthEstimator.js.map +1 -0
  387. package/lib/webrtc/src/media/sender/createBandwidthEstimator.d.ts +20 -0
  388. package/lib/webrtc/src/media/sender/createBandwidthEstimator.js +26 -0
  389. package/lib/webrtc/src/media/sender/createBandwidthEstimator.js.map +1 -0
  390. package/lib/webrtc/src/media/sender/estimatorBinding.d.ts +70 -0
  391. package/lib/webrtc/src/media/sender/estimatorBinding.js +175 -0
  392. package/lib/webrtc/src/media/sender/estimatorBinding.js.map +1 -0
  393. package/lib/webrtc/src/media/sender/estimators/disabledBwe.d.ts +18 -0
  394. package/lib/webrtc/src/media/sender/estimators/disabledBwe.js +44 -0
  395. package/lib/webrtc/src/media/sender/estimators/disabledBwe.js.map +1 -0
  396. package/lib/webrtc/src/media/sender/estimators/gcc/acknowledgedBitrateEstimator.d.ts +79 -0
  397. package/lib/webrtc/src/media/sender/estimators/gcc/acknowledgedBitrateEstimator.js +289 -0
  398. package/lib/webrtc/src/media/sender/estimators/gcc/acknowledgedBitrateEstimator.js.map +1 -0
  399. package/lib/webrtc/src/media/sender/estimators/gcc/aimdRateControl.d.ts +87 -0
  400. package/lib/webrtc/src/media/sender/estimators/gcc/aimdRateControl.js +429 -0
  401. package/lib/webrtc/src/media/sender/estimators/gcc/aimdRateControl.js.map +1 -0
  402. package/lib/webrtc/src/media/sender/estimators/gcc/alrDetector.d.ts +21 -0
  403. package/lib/webrtc/src/media/sender/estimators/gcc/alrDetector.js +84 -0
  404. package/lib/webrtc/src/media/sender/estimators/gcc/alrDetector.js.map +1 -0
  405. package/lib/webrtc/src/media/sender/estimators/gcc/bandwidthLimitedCause.d.ts +37 -0
  406. package/lib/webrtc/src/media/sender/estimators/gcc/bandwidthLimitedCause.js +74 -0
  407. package/lib/webrtc/src/media/sender/estimators/gcc/bandwidthLimitedCause.js.map +1 -0
  408. package/lib/webrtc/src/media/sender/estimators/gcc/constants.d.ts +383 -0
  409. package/lib/webrtc/src/media/sender/estimators/gcc/constants.js +408 -0
  410. package/lib/webrtc/src/media/sender/estimators/gcc/constants.js.map +1 -0
  411. package/lib/webrtc/src/media/sender/estimators/gcc/gccBwe.d.ts +406 -0
  412. package/lib/webrtc/src/media/sender/estimators/gcc/gccBwe.js +1391 -0
  413. package/lib/webrtc/src/media/sender/estimators/gcc/gccBwe.js.map +1 -0
  414. package/lib/webrtc/src/media/sender/estimators/gcc/index.d.ts +19 -0
  415. package/lib/webrtc/src/media/sender/estimators/gcc/index.js +75 -0
  416. package/lib/webrtc/src/media/sender/estimators/gcc/index.js.map +1 -0
  417. package/lib/webrtc/src/media/sender/estimators/gcc/interArrivalDelta.d.ts +68 -0
  418. package/lib/webrtc/src/media/sender/estimators/gcc/interArrivalDelta.js +201 -0
  419. package/lib/webrtc/src/media/sender/estimators/gcc/interArrivalDelta.js.map +1 -0
  420. package/lib/webrtc/src/media/sender/estimators/gcc/intervalBudget.d.ts +26 -0
  421. package/lib/webrtc/src/media/sender/estimators/gcc/intervalBudget.js +72 -0
  422. package/lib/webrtc/src/media/sender/estimators/gcc/intervalBudget.js.map +1 -0
  423. package/lib/webrtc/src/media/sender/estimators/gcc/linkCapacityEstimator.d.ts +41 -0
  424. package/lib/webrtc/src/media/sender/estimators/gcc/linkCapacityEstimator.js +99 -0
  425. package/lib/webrtc/src/media/sender/estimators/gcc/linkCapacityEstimator.js.map +1 -0
  426. package/lib/webrtc/src/media/sender/estimators/gcc/lossBasedBwe.d.ts +247 -0
  427. package/lib/webrtc/src/media/sender/estimators/gcc/lossBasedBwe.js +1042 -0
  428. package/lib/webrtc/src/media/sender/estimators/gcc/lossBasedBwe.js.map +1 -0
  429. package/lib/webrtc/src/media/sender/estimators/gcc/overuseDetector.d.ts +26 -0
  430. package/lib/webrtc/src/media/sender/estimators/gcc/overuseDetector.js +132 -0
  431. package/lib/webrtc/src/media/sender/estimators/gcc/overuseDetector.js.map +1 -0
  432. package/lib/webrtc/src/media/sender/estimators/gcc/probeController.d.ts +305 -0
  433. package/lib/webrtc/src/media/sender/estimators/gcc/probeController.js +1073 -0
  434. package/lib/webrtc/src/media/sender/estimators/gcc/probeController.js.map +1 -0
  435. package/lib/webrtc/src/media/sender/estimators/gcc/rttBasedBackoff.d.ts +60 -0
  436. package/lib/webrtc/src/media/sender/estimators/gcc/rttBasedBackoff.js +151 -0
  437. package/lib/webrtc/src/media/sender/estimators/gcc/rttBasedBackoff.js.map +1 -0
  438. package/lib/webrtc/src/media/sender/estimators/gcc/trendlineEstimator.d.ts +44 -0
  439. package/lib/webrtc/src/media/sender/estimators/gcc/trendlineEstimator.js +246 -0
  440. package/lib/webrtc/src/media/sender/estimators/gcc/trendlineEstimator.js.map +1 -0
  441. package/lib/webrtc/src/media/sender/estimators/index.d.ts +7 -0
  442. package/lib/webrtc/src/media/sender/estimators/index.js +19 -0
  443. package/lib/webrtc/src/media/sender/estimators/index.js.map +1 -0
  444. package/lib/webrtc/src/media/sender/estimators/lightweightBwe.d.ts +64 -0
  445. package/lib/webrtc/src/media/sender/estimators/lightweightBwe.js +216 -0
  446. package/lib/webrtc/src/media/sender/estimators/lightweightBwe.js.map +1 -0
  447. package/lib/webrtc/src/media/sender/estimators/twccFeedbackAdapter.d.ts +117 -0
  448. package/lib/webrtc/src/media/sender/estimators/twccFeedbackAdapter.js +250 -0
  449. package/lib/webrtc/src/media/sender/estimators/twccFeedbackAdapter.js.map +1 -0
  450. package/lib/webrtc/src/media/sender/outgoingQueue.d.ts +40 -0
  451. package/lib/webrtc/src/media/sender/outgoingQueue.js +153 -0
  452. package/lib/webrtc/src/media/sender/outgoingQueue.js.map +1 -0
  453. package/lib/webrtc/src/media/sender/padding.d.ts +45 -0
  454. package/lib/webrtc/src/media/sender/padding.js +134 -0
  455. package/lib/webrtc/src/media/sender/padding.js.map +1 -0
  456. package/lib/webrtc/src/media/sender/pendingRtp.d.ts +35 -0
  457. package/lib/webrtc/src/media/sender/pendingRtp.js +120 -0
  458. package/lib/webrtc/src/media/sender/pendingRtp.js.map +1 -0
  459. package/lib/webrtc/src/media/sender/rtpWire.d.ts +13 -0
  460. package/lib/webrtc/src/media/sender/rtpWire.js +38 -0
  461. package/lib/webrtc/src/media/sender/rtpWire.js.map +1 -0
  462. package/lib/webrtc/src/media/sender/sendPacer.d.ts +27 -0
  463. package/lib/webrtc/src/media/sender/sendPacer.js +110 -0
  464. package/lib/webrtc/src/media/sender/sendPacer.js.map +1 -0
  465. package/lib/webrtc/src/media/sender/senderBWE.d.ts +8 -32
  466. package/lib/webrtc/src/media/sender/senderBWE.js +5 -142
  467. package/lib/webrtc/src/media/sender/senderBWE.js.map +1 -1
  468. package/lib/webrtc/src/media/sender/senderStats.d.ts +100 -0
  469. package/lib/webrtc/src/media/sender/senderStats.js +322 -0
  470. package/lib/webrtc/src/media/sender/senderStats.js.map +1 -0
  471. package/lib/webrtc/src/media/track.d.ts +21 -3
  472. package/lib/webrtc/src/media/track.js +24 -6
  473. package/lib/webrtc/src/media/track.js.map +1 -1
  474. package/lib/webrtc/src/negotiation/bundleTopology.d.ts +103 -0
  475. package/lib/webrtc/src/negotiation/bundleTopology.js +384 -0
  476. package/lib/webrtc/src/negotiation/bundleTopology.js.map +1 -0
  477. package/lib/webrtc/src/negotiation/descriptionValidation.d.ts +51 -0
  478. package/lib/webrtc/src/negotiation/descriptionValidation.js +267 -0
  479. package/lib/webrtc/src/negotiation/descriptionValidation.js.map +1 -0
  480. package/lib/webrtc/src/negotiation/iceRestartRequest.d.ts +21 -0
  481. package/lib/webrtc/src/negotiation/iceRestartRequest.js +61 -0
  482. package/lib/webrtc/src/negotiation/iceRestartRequest.js.map +1 -0
  483. package/lib/webrtc/src/negotiation/internalState.d.ts +34 -0
  484. package/lib/webrtc/src/negotiation/internalState.js +21 -0
  485. package/lib/webrtc/src/negotiation/internalState.js.map +1 -0
  486. package/lib/webrtc/src/negotiation/negotiationNeeded.d.ts +44 -0
  487. package/lib/webrtc/src/negotiation/negotiationNeeded.js +119 -0
  488. package/lib/webrtc/src/negotiation/negotiationNeeded.js.map +1 -0
  489. package/lib/webrtc/src/negotiation/remoteCandidates.d.ts +86 -0
  490. package/lib/webrtc/src/negotiation/remoteCandidates.js +349 -0
  491. package/lib/webrtc/src/negotiation/remoteCandidates.js.map +1 -0
  492. package/lib/webrtc/src/negotiation/remoteMediaApplication.d.ts +71 -0
  493. package/lib/webrtc/src/negotiation/remoteMediaApplication.js +414 -0
  494. package/lib/webrtc/src/negotiation/remoteMediaApplication.js.map +1 -0
  495. package/lib/webrtc/src/negotiation/transportActivation.d.ts +59 -0
  496. package/lib/webrtc/src/negotiation/transportActivation.js +356 -0
  497. package/lib/webrtc/src/negotiation/transportActivation.js.map +1 -0
  498. package/lib/webrtc/src/negotiationTransaction.d.ts +240 -0
  499. package/lib/webrtc/src/negotiationTransaction.js +604 -0
  500. package/lib/webrtc/src/negotiationTransaction.js.map +1 -0
  501. package/lib/webrtc/src/nonstandard/dummyMedia.js +2 -1
  502. package/lib/webrtc/src/nonstandard/dummyMedia.js.map +1 -1
  503. package/lib/webrtc/src/nonstandard/index.d.ts +1 -1
  504. package/lib/webrtc/src/nonstandard/index.js +1 -1
  505. package/lib/webrtc/src/nonstandard/index.js.map +1 -1
  506. package/lib/webrtc/src/nonstandard/navigator.js +4 -3
  507. package/lib/webrtc/src/nonstandard/navigator.js.map +1 -1
  508. package/lib/webrtc/src/nonstandard/recorder/index.d.ts +32 -8
  509. package/lib/webrtc/src/nonstandard/recorder/index.js +81 -20
  510. package/lib/webrtc/src/nonstandard/recorder/index.js.map +1 -1
  511. package/lib/webrtc/src/nonstandard/recorder/writer/index.d.ts +10 -8
  512. package/lib/webrtc/src/nonstandard/recorder/writer/index.js +2 -1
  513. package/lib/webrtc/src/nonstandard/recorder/writer/index.js.map +1 -1
  514. package/lib/webrtc/src/nonstandard/recorder/writer/ogg.d.ts +32 -0
  515. package/lib/webrtc/src/nonstandard/recorder/writer/ogg.js +185 -0
  516. package/lib/webrtc/src/nonstandard/recorder/writer/ogg.js.map +1 -0
  517. package/lib/webrtc/src/nonstandard/recorder/writer/webm.d.ts +2 -2
  518. package/lib/webrtc/src/nonstandard/recorder/writer/webm.js +10 -3
  519. package/lib/webrtc/src/nonstandard/recorder/writer/webm.js.map +1 -1
  520. package/lib/webrtc/src/nonstandard/userMedia/packetizer.d.ts +2 -2
  521. package/lib/webrtc/src/nonstandard/userMedia/packetizer.js.map +1 -1
  522. package/lib/webrtc/src/nonstandard/userMedia.d.ts +2 -2
  523. package/lib/webrtc/src/nonstandard/userMedia.js +4 -3
  524. package/lib/webrtc/src/nonstandard/userMedia.js.map +1 -1
  525. package/lib/webrtc/src/peerConnection.d.ts +176 -238
  526. package/lib/webrtc/src/peerConnection.js +1042 -913
  527. package/lib/webrtc/src/peerConnection.js.map +1 -1
  528. package/lib/webrtc/src/polyfill/api.d.ts +5 -5
  529. package/lib/webrtc/src/polyfill/api.js.map +1 -1
  530. package/lib/webrtc/src/polyfill/index.js.map +1 -1
  531. package/lib/webrtc/src/polyfill/install.js +1 -1
  532. package/lib/webrtc/src/polyfill/install.js.map +1 -1
  533. package/lib/webrtc/src/polyfill/mediaDevices.js +2 -1
  534. package/lib/webrtc/src/polyfill/mediaDevices.js.map +1 -1
  535. package/lib/webrtc/src/polyfill/registers/mp4Webm.js.map +1 -1
  536. package/lib/webrtc/src/polyfill/registers/rtpRtcp.js.map +1 -1
  537. package/lib/webrtc/src/polyfill/selectSettings.js.map +1 -1
  538. package/lib/webrtc/src/polyfill/sourceIo.js.map +1 -1
  539. package/lib/webrtc/src/sctpManager.d.ts +97 -1
  540. package/lib/webrtc/src/sctpManager.js +189 -9
  541. package/lib/webrtc/src/sctpManager.js.map +1 -1
  542. package/lib/webrtc/src/sdp.js +12 -6
  543. package/lib/webrtc/src/sdp.js.map +1 -1
  544. package/lib/webrtc/src/sdpManager.d.ts +55 -11
  545. package/lib/webrtc/src/sdpManager.js +364 -87
  546. package/lib/webrtc/src/sdpManager.js.map +1 -1
  547. package/lib/webrtc/src/secureTransportManager.d.ts +89 -28
  548. package/lib/webrtc/src/secureTransportManager.js +232 -53
  549. package/lib/webrtc/src/secureTransportManager.js.map +1 -1
  550. package/lib/webrtc/src/transceiverManager.d.ts +62 -18
  551. package/lib/webrtc/src/transceiverManager.js +272 -107
  552. package/lib/webrtc/src/transceiverManager.js.map +1 -1
  553. package/lib/webrtc/src/transport/dtls.d.ts +64 -3
  554. package/lib/webrtc/src/transport/dtls.js +166 -42
  555. package/lib/webrtc/src/transport/dtls.js.map +1 -1
  556. package/lib/webrtc/src/transport/ice.d.ts +199 -28
  557. package/lib/webrtc/src/transport/ice.js +666 -71
  558. package/lib/webrtc/src/transport/ice.js.map +1 -1
  559. package/lib/webrtc/src/transport/iceCandidateAttribute.d.ts +98 -0
  560. package/lib/webrtc/src/transport/iceCandidateAttribute.js +129 -0
  561. package/lib/webrtc/src/transport/iceCandidateAttribute.js.map +1 -0
  562. package/lib/webrtc/src/transport/sctp.d.ts +6 -1
  563. package/lib/webrtc/src/transport/sctp.js +149 -37
  564. package/lib/webrtc/src/transport/sctp.js.map +1 -1
  565. package/lib/webrtc/src/utils.d.ts +39 -6
  566. package/lib/webrtc/src/utils.js +57 -17
  567. package/lib/webrtc/src/utils.js.map +1 -1
  568. package/package.json +16 -15
@@ -23,48 +23,65 @@ Figure 2: Example for round-trip time computation
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
24
  exports.RTCRtpSender = void 0;
25
25
  const crypto_1 = require("crypto");
26
- const crypto_2 = require("crypto");
27
26
  const promises_1 = require("timers/promises");
28
- const common_1 = require("../imports/common");
29
27
  const __1 = require("..");
30
28
  const errors_1 = require("../errors");
29
+ const common_1 = require("../imports/common");
31
30
  const rtp_1 = require("../imports/rtp");
32
31
  const utils_1 = require("../utils");
33
32
  const codecCompatibility_1 = require("./codecCompatibility");
33
+ const bandwidthEstimator_1 = require("./sender/bandwidthEstimator");
34
+ const estimatorBinding_1 = require("./sender/estimatorBinding");
35
+ const outgoingQueue_1 = require("./sender/outgoingQueue");
36
+ const padding_1 = require("./sender/padding");
37
+ const pendingRtp_1 = require("./sender/pendingRtp");
38
+ const rtpWire_1 = require("./sender/rtpWire");
34
39
  const senderBWE_1 = require("./sender/senderBWE");
40
+ const senderStats_1 = require("./sender/senderStats");
41
+ const sendPacer_1 = require("./sender/sendPacer");
35
42
  const stats_1 = require("./stats");
36
43
  const track_1 = require("./track");
37
44
  const log = (0, rtp_1.debug)("werift:packages/webrtc/src/media/rtpSender.ts");
45
+ /** NACK history: the last sent media packets by sequence number modulo this. */
38
46
  const RTP_HISTORY_SIZE = 128;
39
- const DEFAULT_PENDING_RTP_MAX_LENGTH = 256;
40
- const RTT_ALPHA = 0.85;
41
- function resolvePendingRtpOptions(pendingRtp) {
42
- if (pendingRtp === true) {
43
- return { enabled: true, maxLength: DEFAULT_PENDING_RTP_MAX_LENGTH };
44
- }
45
- if (pendingRtp == undefined || pendingRtp === false) {
46
- return { enabled: false, maxLength: DEFAULT_PENDING_RTP_MAX_LENGTH };
47
- }
48
- const maxLength = typeof pendingRtp.maxLength === "number" &&
49
- Number.isFinite(pendingRtp.maxLength) &&
50
- pendingRtp.maxLength >= 1
51
- ? Math.floor(pendingRtp.maxLength)
52
- : DEFAULT_PENDING_RTP_MAX_LENGTH;
53
- return { enabled: pendingRtp.enabled ?? true, maxLength };
54
- }
55
- function freezeRtpContinuityOffsets(lastOutputSeq, lastOutputTimestamp, firstInputSeq, firstInputTimestamp, timestampStep) {
56
- return {
57
- seqOffset: (0, common_1.uint16Add)((0, common_1.uint16Add)(lastOutputSeq, 1), -firstInputSeq),
58
- timestampOffset: (0, common_1.uint32Add)((0, common_1.uint32Add)(lastOutputTimestamp, timestampStep), -firstInputTimestamp),
59
- };
60
- }
47
+ // RTCRtpSender sends one media stream: negotiation parameters, track binding,
48
+ // the RTP send path, RTCP feedback and stats. Parts with their own state live
49
+ // in ./sender/ (internal, not exported):
50
+ //
51
+ // - estimatorBinding.ts: the bandwidth estimator, its generation and process timer
52
+ // - outgoingQueue.ts / sendPacer.ts: the GCC send FIFO and pacing
53
+ // - padding.ts: probe / loss padding drain on the RTX stream
54
+ // - pendingRtp.ts: RTP held until DTLS connects
55
+ // - senderStats.ts: counters, reception reports, Sender Reports, stats
56
+ // - rtpWire.ts: send FIFO size estimate and the DTLS write
57
+ // Generic RTP pieces (RtpContinuity, header extension rebuild / hop-by-hop
58
+ // stamping, RTX padding packets, RED size estimate) live in werift-rtp.
61
59
  class RTCRtpSender {
60
+ /**
61
+ * Active send-side bandwidth estimator (TWCC-driven).
62
+ *
63
+ * Default is {@link LightweightBandwidthEstimator} (lightweight cumulative algorithm).
64
+ * Replace only with {@link setBandwidthEstimator} (e.g. `new GccBandwidthEstimator()`).
65
+ *
66
+ * Prefer {@link onAvailableBitrate} on this sender for bitrate notifications that
67
+ * survive estimator swaps. Algorithm-specific events remain on concrete instances.
68
+ *
69
+ * **TypeScript compatibility:** {@link CongestionEventsCompatibility} fields
70
+ * (`onCongestion` / `onCongestionScore`) are present on every estimator so
71
+ * existing `sender.senderBWE.onCongestion.subscribe(...)` still type-checks.
72
+ * They fire only on {@link LightweightBandwidthEstimator}; GCC / disabled are no-ops.
73
+ * Prefer `sender.senderBWE as LightweightBandwidthEstimator` (or
74
+ * {@link isLightweightBandwidthEstimator}) for lightweight-only APIs.
75
+ */
76
+ get senderBWE() {
77
+ return this.bwe.estimator;
78
+ }
62
79
  constructor(trackOrKind, options = {}) {
63
80
  Object.defineProperty(this, "trackOrKind", {
64
81
  enumerable: true,
65
82
  configurable: true,
66
83
  writable: true,
67
- value: trackOrKind
84
+ value: void 0
68
85
  });
69
86
  Object.defineProperty(this, "type", {
70
87
  enumerable: true,
@@ -94,7 +111,7 @@ class RTCRtpSender {
94
111
  enumerable: true,
95
112
  configurable: true,
96
113
  writable: true,
97
- value: (0, crypto_2.randomUUID)().toString()
114
+ value: (0, crypto_1.randomUUID)().toString()
98
115
  });
99
116
  Object.defineProperty(this, "onReady", {
100
117
  enumerable: true,
@@ -120,186 +137,205 @@ class RTCRtpSender {
120
137
  writable: true,
121
138
  value: new common_1.Event()
122
139
  });
123
- Object.defineProperty(this, "senderBWE", {
124
- enumerable: true,
125
- configurable: true,
126
- writable: true,
127
- value: new senderBWE_1.SenderBandwidthEstimator()
128
- });
129
- Object.defineProperty(this, "cname", {
140
+ /**
141
+ * Active send-side bandwidth estimator (TWCC-driven) with its generation
142
+ * and process timer. Replaced only via {@link setBandwidthEstimator}.
143
+ */
144
+ Object.defineProperty(this, "bwe", {
130
145
  enumerable: true,
131
146
  configurable: true,
132
147
  writable: true,
133
148
  value: void 0
134
149
  });
135
- Object.defineProperty(this, "mid", {
150
+ /**
151
+ * Stable recommended send bitrate event (**bps**, change-only).
152
+ * Bridged from the active {@link BandwidthEstimator}; subscriptions survive
153
+ * {@link setBandwidthEstimator} without re-subscribing.
154
+ *
155
+ * Prefer this over `senderBWE.onAvailableBitrate` for application adaptation.
156
+ */
157
+ Object.defineProperty(this, "onAvailableBitrate", {
136
158
  enumerable: true,
137
159
  configurable: true,
138
160
  writable: true,
139
- value: void 0
161
+ value: new common_1.Event()
140
162
  });
141
- Object.defineProperty(this, "rtpStreamId", {
163
+ /**
164
+ * Probe cluster configs (target bps / min packets). Bridged from the active
165
+ * {@link BandwidthEstimator}; GCC fires this, lightweight / disabled never do.
166
+ */
167
+ Object.defineProperty(this, "onProbeClusterConfig", {
142
168
  enumerable: true,
143
169
  configurable: true,
144
170
  writable: true,
145
- value: void 0
171
+ value: new common_1.Event()
146
172
  });
147
- Object.defineProperty(this, "repairedRtpStreamId", {
173
+ /**
174
+ * @internal Sim / test only. When false, skip the **media** token-bucket
175
+ * (pin GetPacingRates). Probe `next_probe_time` waits stay enabled so
176
+ * BitrateProber timing is not disabled by accident.
177
+ *
178
+ * Peer/Chrome sims turn this off during the congestion phase so the
179
+ * synthetic generate rate hits the bottleneck, then turn it back on
180
+ * when the app follows {@link onAvailableBitrate}.
181
+ */
182
+ Object.defineProperty(this, "mediaPacingEnabled", {
148
183
  enumerable: true,
149
184
  configurable: true,
150
185
  writable: true,
151
- value: void 0
186
+ value: true
152
187
  });
153
- Object.defineProperty(this, "rtxPayloadType", {
188
+ /** Token-bucket pacer for **media** (not probe) rate enforcement. */
189
+ Object.defineProperty(this, "pacer", {
154
190
  enumerable: true,
155
191
  configurable: true,
156
192
  writable: true,
157
- value: void 0
193
+ value: new sendPacer_1.MediaPacer()
158
194
  });
159
- Object.defineProperty(this, "rtxSequenceNumber", {
195
+ /**
196
+ * pin `pending_untracked_size_` — bytes sent without TWCC tracking,
197
+ * attached to the next tracked packet as `priorUnackedBytes`.
198
+ */
199
+ Object.defineProperty(this, "pendingUntrackedBytes", {
160
200
  enumerable: true,
161
201
  configurable: true,
162
202
  writable: true,
163
- value: (0, common_1.random16)()
203
+ value: 0
164
204
  });
165
- Object.defineProperty(this, "redRedundantPayloadType", {
205
+ /**
206
+ * GCC send FIFO: reservation → pacing wait → seq / TSN assignment → DTLS write.
207
+ * Probe padding may call {@link sendRtpInternal} on this stack only after
208
+ * the current packet's DTLS write ({@link nestOutgoingPadding}); timer /
209
+ * cluster events always enqueue so they cannot share a DTLS write.
210
+ */
211
+ Object.defineProperty(this, "outgoing", {
166
212
  enumerable: true,
167
213
  configurable: true,
168
214
  writable: true,
169
215
  value: void 0
170
216
  });
171
- Object.defineProperty(this, "_redDistance", {
172
- enumerable: true,
173
- configurable: true,
174
- writable: true,
175
- value: 2
176
- });
177
- Object.defineProperty(this, "redEncoder", {
178
- enumerable: true,
179
- configurable: true,
180
- writable: true,
181
- value: new rtp_1.RedEncoder(this._redDistance)
182
- });
183
- Object.defineProperty(this, "headerExtensions", {
217
+ /**
218
+ * Set only around post-DTLS {@link maybeInjectProbePadding} so padding can
219
+ * drain on the same FIFO task without deadlock. Must stay false during
220
+ * probe/pacing wait, or a cluster event would send in parallel.
221
+ */
222
+ Object.defineProperty(this, "nestOutgoingPadding", {
184
223
  enumerable: true,
185
224
  configurable: true,
186
225
  writable: true,
187
- value: []
226
+ value: false
188
227
  });
189
- Object.defineProperty(this, "disposeTrack", {
228
+ Object.defineProperty(this, "padding", {
190
229
  enumerable: true,
191
230
  configurable: true,
192
231
  writable: true,
193
232
  value: void 0
194
233
  });
195
- Object.defineProperty(this, "sendEncodings", {
234
+ /** Fires for a FIR addressed to this sender with a new sequence number (RFC 5104 4.3.1). */
235
+ Object.defineProperty(this, "onFullIntraRequest", {
196
236
  enumerable: true,
197
237
  configurable: true,
198
238
  writable: true,
199
- value: [{}]
239
+ value: new common_1.Event()
200
240
  });
201
- // # stats
202
- Object.defineProperty(this, "lastSRtimestamp", {
241
+ /**
242
+ * Fires for every key frame request addressed to this sender: each PLI, and each FIR
243
+ * that is not a retransmission. Fires in addition to onPictureLossIndication / onFullIntraRequest.
244
+ */
245
+ Object.defineProperty(this, "onKeyFrameRequest", {
203
246
  enumerable: true,
204
247
  configurable: true,
205
248
  writable: true,
206
- value: void 0
249
+ value: new common_1.Event()
207
250
  });
208
- Object.defineProperty(this, "lastSentSRTimestamp", {
251
+ Object.defineProperty(this, "cname", {
209
252
  enumerable: true,
210
253
  configurable: true,
211
254
  writable: true,
212
255
  value: void 0
213
256
  });
214
- Object.defineProperty(this, "ntpTimestamp", {
215
- enumerable: true,
216
- configurable: true,
217
- writable: true,
218
- value: 0n
219
- });
220
- Object.defineProperty(this, "rtpTimestamp", {
221
- enumerable: true,
222
- configurable: true,
223
- writable: true,
224
- value: 0
225
- });
226
- Object.defineProperty(this, "octetCount", {
257
+ Object.defineProperty(this, "mid", {
227
258
  enumerable: true,
228
259
  configurable: true,
229
260
  writable: true,
230
- value: 0
261
+ value: void 0
231
262
  });
232
- Object.defineProperty(this, "packetCount", {
263
+ Object.defineProperty(this, "rtpStreamId", {
233
264
  enumerable: true,
234
265
  configurable: true,
235
266
  writable: true,
236
- value: 0
267
+ value: void 0
237
268
  });
238
- Object.defineProperty(this, "headerBytesSent", {
269
+ Object.defineProperty(this, "repairedRtpStreamId", {
239
270
  enumerable: true,
240
271
  configurable: true,
241
272
  writable: true,
242
- value: 0
273
+ value: void 0
243
274
  });
244
- Object.defineProperty(this, "rtt", {
275
+ Object.defineProperty(this, "rtxPayloadType", {
245
276
  enumerable: true,
246
277
  configurable: true,
247
278
  writable: true,
248
279
  value: void 0
249
280
  });
250
- Object.defineProperty(this, "totalRoundTripTime", {
281
+ Object.defineProperty(this, "rtxSequenceNumber", {
251
282
  enumerable: true,
252
283
  configurable: true,
253
284
  writable: true,
254
- value: 0
285
+ value: (0, common_1.random16)()
255
286
  });
256
- Object.defineProperty(this, "roundTripTimeMeasurements", {
287
+ Object.defineProperty(this, "redRedundantPayloadType", {
257
288
  enumerable: true,
258
289
  configurable: true,
259
290
  writable: true,
260
- value: 0
291
+ value: void 0
261
292
  });
262
- Object.defineProperty(this, "retransmittedPacketsSent", {
293
+ Object.defineProperty(this, "_redDistance", {
263
294
  enumerable: true,
264
295
  configurable: true,
265
296
  writable: true,
266
- value: 0
297
+ value: 2
267
298
  });
268
- Object.defineProperty(this, "retransmittedBytesSent", {
299
+ Object.defineProperty(this, "redEncoder", {
269
300
  enumerable: true,
270
301
  configurable: true,
271
302
  writable: true,
272
- value: 0
303
+ value: new rtp_1.RedEncoder(this._redDistance)
273
304
  });
274
- Object.defineProperty(this, "nackCount", {
305
+ Object.defineProperty(this, "headerExtensions", {
275
306
  enumerable: true,
276
307
  configurable: true,
277
308
  writable: true,
278
- value: 0
309
+ value: []
279
310
  });
280
- Object.defineProperty(this, "pliCount", {
311
+ Object.defineProperty(this, "disposeTrack", {
281
312
  enumerable: true,
282
313
  configurable: true,
283
314
  writable: true,
284
- value: 0
315
+ value: void 0
285
316
  });
286
- Object.defineProperty(this, "firCount", {
317
+ Object.defineProperty(this, "sendEncodings", {
287
318
  enumerable: true,
288
319
  configurable: true,
289
320
  writable: true,
290
- value: 0
321
+ value: [{}]
291
322
  });
292
- Object.defineProperty(this, "remotePacketsLost", {
323
+ // # stats
324
+ Object.defineProperty(this, "stats", {
293
325
  enumerable: true,
294
326
  configurable: true,
295
327
  writable: true,
296
- value: void 0
328
+ value: new senderStats_1.SenderStatistics()
297
329
  });
298
- Object.defineProperty(this, "remoteFractionLost", {
330
+ /**
331
+ * Last FIR sequence number per (FIR sender SSRC, FCI target SSRC): RFC 5104
332
+ * 4.3.1.1 keeps one sequence number space per pair.
333
+ */
334
+ Object.defineProperty(this, "lastFirSequenceNumbers", {
299
335
  enumerable: true,
300
336
  configurable: true,
301
337
  writable: true,
302
- value: void 0
338
+ value: new Map()
303
339
  });
304
340
  Object.defineProperty(this, "receiverEstimatedMaxBitrate", {
305
341
  enumerable: true,
@@ -308,41 +344,11 @@ class RTCRtpSender {
308
344
  value: 0n
309
345
  });
310
346
  // rtp
311
- Object.defineProperty(this, "sequenceNumber", {
312
- enumerable: true,
313
- configurable: true,
314
- writable: true,
315
- value: void 0
316
- });
317
- Object.defineProperty(this, "timestamp", {
318
- enumerable: true,
319
- configurable: true,
320
- writable: true,
321
- value: void 0
322
- });
323
- Object.defineProperty(this, "timestampOffset", {
324
- enumerable: true,
325
- configurable: true,
326
- writable: true,
327
- value: 0
328
- });
329
- Object.defineProperty(this, "seqOffset", {
330
- enumerable: true,
331
- configurable: true,
332
- writable: true,
333
- value: 0
334
- });
335
- Object.defineProperty(this, "rtpContinuityPending", {
336
- enumerable: true,
337
- configurable: true,
338
- writable: true,
339
- value: false
340
- });
341
- Object.defineProperty(this, "pendingTimestampStep", {
347
+ Object.defineProperty(this, "continuity", {
342
348
  enumerable: true,
343
349
  configurable: true,
344
350
  writable: true,
345
- value: 1
351
+ value: new rtp_1.RtpContinuity()
346
352
  });
347
353
  Object.defineProperty(this, "rtpCache", {
348
354
  enumerable: true,
@@ -354,21 +360,28 @@ class RTCRtpSender {
354
360
  enumerable: true,
355
361
  configurable: true,
356
362
  writable: true,
357
- value: []
363
+ value: void 0
358
364
  });
359
- Object.defineProperty(this, "drainingPendingRtp", {
365
+ /**
366
+ * Bumped on replaceTrack / detachTrack / onSourceChanged. Media accepted
367
+ * under an older epoch is dropped before it can allocate seq/timestamp, so
368
+ * it neither consumes the new source's continuity offset nor leaks out
369
+ * after `replaceTrack(null)`.
370
+ */
371
+ Object.defineProperty(this, "sourceEpoch", {
360
372
  enumerable: true,
361
373
  configurable: true,
362
374
  writable: true,
363
- value: false
375
+ value: 0
364
376
  });
365
- Object.defineProperty(this, "pendingRtpEnabled", {
377
+ /** Aborted on every source epoch bump to wake probe / pacing waits. */
378
+ Object.defineProperty(this, "sourceEpochCancel", {
366
379
  enumerable: true,
367
380
  configurable: true,
368
381
  writable: true,
369
- value: void 0
382
+ value: new AbortController()
370
383
  });
371
- Object.defineProperty(this, "pendingRtpMaxLength", {
384
+ Object.defineProperty(this, "pendingRtpEnabled", {
372
385
  enumerable: true,
373
386
  configurable: true,
374
387
  writable: true,
@@ -392,6 +405,16 @@ class RTCRtpSender {
392
405
  writable: true,
393
406
  value: void 0
394
407
  });
408
+ /**
409
+ * Primary codec a pending remote offer's answer would switch this sender to.
410
+ * `replaceTrack()` must fit it too, since the commit sends the track with it.
411
+ */
412
+ Object.defineProperty(this, "proposedPrimaryCodec", {
413
+ enumerable: true,
414
+ configurable: true,
415
+ writable: true,
416
+ value: void 0
417
+ });
395
418
  Object.defineProperty(this, "dtlsTransport", {
396
419
  enumerable: true,
397
420
  configurable: true,
@@ -434,16 +457,43 @@ class RTCRtpSender {
434
457
  writable: true,
435
458
  value: new AbortController()
436
459
  });
437
- const pendingRtp = resolvePendingRtpOptions(options.pendingRtp);
460
+ /**
461
+ * The negotiated direction in effect (the answer's, or a pranswer's while
462
+ * it is pending) does not include sending: RTP is dropped, not queued.
463
+ * @internal
464
+ */
465
+ Object.defineProperty(this, "sendSuppressed", {
466
+ enumerable: true,
467
+ configurable: true,
468
+ writable: true,
469
+ value: false
470
+ });
471
+ this.trackOrKind = trackOrKind;
472
+ const pendingRtp = (0, pendingRtp_1.resolvePendingRtpOptions)(options.pendingRtp);
438
473
  this.pendingRtpEnabled = pendingRtp.enabled;
439
- this.pendingRtpMaxLength = pendingRtp.maxLength;
474
+ this.pendingRtp = new pendingRtp_1.PendingRtpQueue(pendingRtp.maxLength);
440
475
  this.kind =
441
476
  typeof this.trackOrKind === "string"
442
477
  ? this.trackOrKind
443
478
  : this.trackOrKind.kind;
479
+ this.bwe = new estimatorBinding_1.EstimatorBinding(options?.bandwidthEstimator ?? new senderBWE_1.LightweightBandwidthEstimator(), {
480
+ onAvailableBitrate: (bps) => this.onAvailableBitrate.execute(bps),
481
+ onProbeClusterConfig: (cfg) => {
482
+ this.onProbeClusterConfig.execute(cfg);
483
+ // pin BitrateProber: no prepaid token-bucket credit. Probe packets
484
+ // wait on next_probe_time = started_at + sent_bytes / send_bitrate.
485
+ void this.maybeInjectProbePadding();
486
+ },
487
+ // pin GetPacingRates padding_rate while kIncreaseUsingPadding.
488
+ onProcessTick: () => void this.maybeInjectLossPadding(),
489
+ isStopped: () => this.stopped,
490
+ });
491
+ this.outgoing = new outgoingQueue_1.OutgoingQueue(this.kind, () => (0, bandwidthEstimator_1.needsPacedSendPath)(this.bwe.estimator));
492
+ this.padding = this.createPaddingInjector();
444
493
  if (typeof trackOrKind !== "string") {
445
494
  this.registerTrack(trackOrKind);
446
495
  }
496
+ this.bwe.syncProcessTimer();
447
497
  }
448
498
  get transport() {
449
499
  return this.dtlsTransport ?? null;
@@ -455,18 +505,91 @@ class RTCRtpSender {
455
505
  this.streamIds = value ? [value] : [];
456
506
  }
457
507
  setDtlsTransport(dtlsTransport) {
458
- if (this.dtlsTransport) {
508
+ const previous = this.dtlsTransport;
509
+ if (previous) {
459
510
  this.dtlsDisposer.forEach((dispose) => dispose());
460
511
  }
461
512
  this.dtlsTransport = dtlsTransport;
513
+ // A stopped sender (also one a rollback binds again) sends nothing and
514
+ // does not subscribe to the transport, which may outlive it.
515
+ if (this.stopped) {
516
+ this.dtlsDisposer = [];
517
+ return;
518
+ }
519
+ if (previous && previous !== dtlsTransport) {
520
+ this.resetBweForNewTransport();
521
+ }
462
522
  this.dtlsDisposer = [
463
523
  this.dtlsTransport.onStateChange.subscribe((state) => {
524
+ this.syncNetworkAvailability();
464
525
  if (state === "connected") {
465
526
  this.onReady.execute();
466
527
  void this.drainPendingRtp();
467
528
  }
468
529
  }).unSubscribe,
469
530
  ];
531
+ this.syncNetworkAvailability();
532
+ }
533
+ /**
534
+ * Replace the send-side bandwidth estimator used for TWCC-driven BWE.
535
+ *
536
+ * Default is the lightweight {@link LightweightBandwidthEstimator}. Pass e.g.
537
+ * `new GccBandwidthEstimator()` to use Google Congestion Control.
538
+ *
539
+ * Behavior on swap:
540
+ * 1. Bumps the estimator generation so in-flight sends discard
541
+ * `rtpPacketSent` delivery and in-flight {@link maybeInjectProbePadding}
542
+ * loops exit (cancelled — no packets to disposed / previous estimator).
543
+ * 2. Stops delivering `rtpPacketSent` / `receiveTWCC` to the previous instance.
544
+ * 3. Unbinds the stable {@link onAvailableBitrate} bridge, then `dispose()`/`reset()` the old instance.
545
+ * 4. **Always** `reset()` the injected `impl` so a previously used instance
546
+ * starts clean (no implicit state merge), then rebinds the bridge.
547
+ * 5. Clears {@link pendingUntrackedBytes} and pacing credit so the next
548
+ * estimator's first `SentInfo.priorUnackedBytes` is 0.
549
+ *
550
+ * Subscriptions to {@link onAvailableBitrate} on this sender are preserved.
551
+ * Re-subscribe algorithm-specific events on the new concrete instance.
552
+ */
553
+ setBandwidthEstimator(impl) {
554
+ // Invalidate in-flight media/padding so they do not touch the next estimator
555
+ // (or revive a disposed probe controller after dispose/reset).
556
+ this.bwe.replace(impl);
557
+ this.resetBweSendTransientState();
558
+ this.syncNetworkAvailability();
559
+ this.bwe.syncProcessTimer();
560
+ // New generation may have missed onProbeClusterConfig while the previous
561
+ // drain still held the same-generation re-entry lock.
562
+ void this.maybeInjectProbePadding();
563
+ }
564
+ /**
565
+ * A new DTLS transport has its own transport-wide sequence space and path:
566
+ * drop the estimator's path state ({@link EstimatorBinding.resetForNewRoute},
567
+ * which also bumps the generation so in-flight sends on the old transport
568
+ * do not report their old TSN) and the pacing / unacked leftovers.
569
+ */
570
+ resetBweForNewTransport() {
571
+ this.bwe.resetForNewRoute();
572
+ this.resetBweSendTransientState();
573
+ this.bwe.syncProcessTimer();
574
+ }
575
+ /** Drop pacing leftover and unacked bytes that belonged to the previous BWE generation. */
576
+ resetBweSendTransientState() {
577
+ this.pacer.reset();
578
+ this.pendingUntrackedBytes = 0;
579
+ }
580
+ /** Initial probes wait until DTLS can send; also on a swap onto a connected sender. */
581
+ syncNetworkAvailability() {
582
+ this.bwe.syncNetworkAvailability(this.dtlsTransport?.state === "connected");
583
+ }
584
+ /**
585
+ * Effective send pacing rate (bps): estimator estimate, raised to the active
586
+ * probe target while probing. 0 when unknown. Lightweight returns 0 from
587
+ * {@link BandwidthEstimator.getPacingBitrateBps} so media is unpaced.
588
+ */
589
+ get pacingBitrateBps() {
590
+ const estimator = this.bwe.estimator;
591
+ const paced = estimator.getPacingBitrateBps();
592
+ return paced > 0 ? paced : estimator.availableBitrate;
470
593
  }
471
594
  get redDistance() {
472
595
  return this._redDistance;
@@ -475,27 +598,67 @@ class RTCRtpSender {
475
598
  this._redDistance = n;
476
599
  this.redEncoder.distance = n;
477
600
  }
601
+ /** Internal: the negotiated send parameters, for a negotiation baseline. */
602
+ snapshotSendParams() {
603
+ return {
604
+ cname: this.cname,
605
+ mid: this.mid,
606
+ headerExtensions: [...this.headerExtensions],
607
+ rtpStreamId: this.rtpStreamId,
608
+ repairedRtpStreamId: this.repairedRtpStreamId,
609
+ codec: this.codec,
610
+ negotiatedCodecs: [...this.negotiatedCodecs],
611
+ sendPrimaryCodec: this.sendPrimaryCodec,
612
+ proposedPrimaryCodec: this.proposedPrimaryCodec,
613
+ track: this.track,
614
+ trackCodec: this.track?.codec,
615
+ rtxPayloadType: this.rtxPayloadType,
616
+ redRedundantPayloadType: this.redRedundantPayloadType,
617
+ };
618
+ }
619
+ /**
620
+ * Internal: a pending remote offer proposes `codecs` for the answer while
621
+ * the committed codec keeps sending until the final answer.
622
+ */
623
+ proposeSend(codecs) {
624
+ this.proposedPrimaryCodec = primarySendCodecOf(codecs);
625
+ }
626
+ /** Internal: return to a negotiation baseline's send parameters. */
627
+ restoreSendParams(snapshot) {
628
+ this.cname = snapshot.cname;
629
+ this.mid = snapshot.mid;
630
+ this.headerExtensions = [...snapshot.headerExtensions];
631
+ this.rtpStreamId = snapshot.rtpStreamId;
632
+ this.repairedRtpStreamId = snapshot.repairedRtpStreamId;
633
+ this.codec = snapshot.codec;
634
+ this.negotiatedCodecs = [...snapshot.negotiatedCodecs];
635
+ this.sendPrimaryCodec = snapshot.sendPrimaryCodec;
636
+ this.proposedPrimaryCodec = snapshot.proposedPrimaryCodec;
637
+ if (this.track) {
638
+ // A track attached during the rolled-back negotiation sends with the
639
+ // restored codec, not the one a pranswer applied.
640
+ this.track.codec =
641
+ this.track === snapshot.track ? snapshot.trackCodec : this.codec;
642
+ }
643
+ this.rtxPayloadType = snapshot.rtxPayloadType;
644
+ this.redRedundantPayloadType = snapshot.redRedundantPayloadType;
645
+ }
478
646
  prepareSend(params) {
479
647
  this.cname = params.rtcp?.cname;
480
648
  this.mid = params.muxId;
481
649
  this.headerExtensions = params.headerExtensions;
650
+ // Keep a RID set via sendEncodings/setParameters when SDP carries none.
482
651
  this.rtpStreamId = params.rtpStreamId ?? this.rtpStreamId;
483
652
  this.repairedRtpStreamId = params.repairedRtpStreamId;
484
653
  this.negotiatedCodecs = [...params.codecs];
485
- this.codec =
486
- params.codecs.find((codec) => codec.name.toLowerCase() !== "rtx") ??
487
- params.codecs[0];
488
- const redPrimaryPayloadType = this.codec?.name.toLowerCase() === "red"
489
- ? Number((this.codec.parameters ?? "").split("/")[0])
490
- : undefined;
491
- this.sendPrimaryCodec =
492
- this.codec?.name.toLowerCase() === "red"
493
- ? params.codecs.find((codec) => !["red", "rtx"].includes(codec.name.toLowerCase()) &&
494
- codec.payloadType === redPrimaryPayloadType)
495
- : this.codec;
654
+ this.proposedPrimaryCodec = undefined;
655
+ this.codec = sendCodecOf(params.codecs);
656
+ this.sendPrimaryCodec = primarySendCodecOf(params.codecs);
496
657
  if (this.track) {
497
658
  this.track.codec = this.codec;
498
659
  }
660
+ // Derived from these codecs only: a description without RTX/RED turns
661
+ // them off instead of keeping an earlier negotiation's payload type.
499
662
  this.rtxPayloadType = undefined;
500
663
  this.redRedundantPayloadType = undefined;
501
664
  params.codecs.forEach((codec) => {
@@ -511,69 +674,32 @@ class RTCRtpSender {
511
674
  }
512
675
  void this.drainPendingRtp();
513
676
  }
514
- canSendRtp() {
515
- return (!this.stopped && this.dtlsTransport?.state === "connected" && !!this.codec);
516
- }
517
- settlePendingRtp(item, error) {
518
- if (error == undefined) {
519
- item.resolve();
677
+ /** @internal Follow the transceiver's currentDirection. */
678
+ setSendSuppressed(suppressed) {
679
+ if (this.sendSuppressed === suppressed)
520
680
  return;
681
+ this.sendSuppressed = suppressed;
682
+ if (suppressed) {
683
+ this.pendingRtp.discard();
684
+ // Drop media already accepted into the send FIFO and release a packet
685
+ // parked in a probe / pacing wait (same path as a source switch).
686
+ this.invalidateQueuedSource();
521
687
  }
522
- item.reject(error);
523
- }
524
- discardPendingRtp() {
525
- const dropped = this.pendingRtp.splice(0);
526
- for (const item of dropped) {
527
- this.settlePendingRtp(item);
528
- }
688
+ else
689
+ void this.drainPendingRtp();
529
690
  }
530
- enqueuePendingRtp(rtp, resolve, reject) {
531
- const packet = Buffer.isBuffer(rtp)
532
- ? rtp_1.RtpPacket.deSerialize(rtp)
533
- : rtp.clone();
534
- this.pendingRtp.push({ packet, resolve, reject });
535
- while (this.pendingRtp.length > this.pendingRtpMaxLength) {
536
- const dropped = this.pendingRtp.shift();
537
- if (dropped) {
538
- this.settlePendingRtp(dropped);
539
- }
540
- }
691
+ canSendRtp() {
692
+ return (!this.stopped &&
693
+ !this.sendSuppressed &&
694
+ this.dtlsTransport?.state === "connected" &&
695
+ !!this.codec);
541
696
  }
542
697
  /**
543
- * Send queued RTP once DTLS is connected and a codec is set. Used only when
544
- * pending RTP is enabled: `writeRtp` / `sendRtp` may run before ICE/DTLS
545
- * completes; dropping those packets would lose the start of a media source.
546
- * `drainingPendingRtp` blocks re-entry while `dispatchRtp` is awaited;
547
- * packets enqueued during that wait are drained by the recursive call after
548
- * the flag is cleared.
698
+ * Send queued RTP once DTLS is connected and a codec is set.
699
+ * Drain uses {@link sendRtpInternal} so GCC pacing / TWCC stay on the send path.
549
700
  */
550
- async drainPendingRtp() {
551
- if (this.drainingPendingRtp) {
552
- return;
553
- }
554
- this.drainingPendingRtp = true;
555
- try {
556
- while (this.pendingRtp.length > 0 && this.canSendRtp()) {
557
- const item = this.pendingRtp.shift();
558
- if (!this.canSendRtp()) {
559
- this.settlePendingRtp(item);
560
- continue;
561
- }
562
- try {
563
- await this.dispatchRtp(item.packet);
564
- this.settlePendingRtp(item);
565
- }
566
- catch (error) {
567
- this.settlePendingRtp(item, error);
568
- }
569
- }
570
- }
571
- finally {
572
- this.drainingPendingRtp = false;
573
- }
574
- if (this.pendingRtp.length > 0 && this.canSendRtp()) {
575
- await this.drainPendingRtp();
576
- }
701
+ drainPendingRtp() {
702
+ return this.pendingRtp.drain(() => this.canSendRtp(), (packet, sourceEpoch) => this.dispatchMedia(packet, sourceEpoch));
577
703
  }
578
704
  registerTrack(track) {
579
705
  if (track.stopped)
@@ -620,14 +746,21 @@ class RTCRtpSender {
620
746
  throw new Error("track is ended");
621
747
  (0, track_1.captureTrackSourceCodecs)(track);
622
748
  const sourceCodecs = (0, track_1.getTrackSourceCodecs)(track);
623
- if (this.sendPrimaryCodec != undefined &&
749
+ const incompatible = [
750
+ this.sendPrimaryCodec,
751
+ this.proposedPrimaryCodec,
752
+ ].find((primary) => primary != undefined &&
624
753
  sourceCodecs != undefined &&
625
- !sourceCodecs.some((source) => (0, codecCompatibility_1.isCodecCompatible)(source, this.sendPrimaryCodec))) {
626
- throw (0, errors_1.createWebRtcDomException)("InvalidModificationError", `Track codec ${sourceCodecs.map((codec) => codec.mimeType).join(", ")} is incompatible with negotiated codecs ${this.negotiatedCodecs.map((codec) => codec.mimeType).join(", ")}`);
754
+ !sourceCodecs.some((source) => (0, codecCompatibility_1.isCodecCompatible)(source, primary)));
755
+ if (incompatible) {
756
+ throw (0, errors_1.createWebRtcDomException)("InvalidModificationError", `Track codec ${sourceCodecs.map((codec) => codec.mimeType).join(", ")} is incompatible with negotiated codec ${incompatible.mimeType}`);
627
757
  }
628
- if (this.sequenceNumber != undefined) {
758
+ if (this.continuity.sequenceNumber != undefined) {
629
759
  this.scheduleRtpContinuity();
630
760
  }
761
+ else {
762
+ this.invalidateQueuedSource();
763
+ }
631
764
  this.registerTrack(track);
632
765
  log("replaceTrack", "ssrc", track.ssrc, "rid", track.rid);
633
766
  }
@@ -636,8 +769,9 @@ class RTCRtpSender {
636
769
  * The same sender can resume sending after `replaceTrack(track)`.
637
770
  */
638
771
  detachTrack() {
639
- this.rtpContinuityPending = false;
640
- this.discardPendingRtp();
772
+ this.continuity.cancel();
773
+ this.pendingRtp.discard();
774
+ this.invalidateQueuedSource();
641
775
  if (this.disposeTrack) {
642
776
  this.disposeTrack();
643
777
  this.disposeTrack = undefined;
@@ -646,10 +780,19 @@ class RTCRtpSender {
646
780
  }
647
781
  stop({ keepTrack = false } = {}) {
648
782
  this.stopped = true;
649
- this.rtpContinuityPending = false;
650
- this.discardPendingRtp();
783
+ // A stopped sender sends nothing: release its subscription on the
784
+ // (possibly BUNDLE-shared) DTLS transport, which outlives it.
785
+ for (const dispose of this.dtlsDisposer)
786
+ dispose();
787
+ this.dtlsDisposer = [];
788
+ this.continuity.cancel();
789
+ this.pendingRtp.discard();
790
+ // Invalidate in-flight sendRtp / maybeInjectProbePadding before dispose
791
+ // so they cannot emit padding or revive a disposed estimator.
792
+ this.bwe.bumpGeneration();
651
793
  this.rtcpRunning = false;
652
794
  this.rtcpCancel.abort();
795
+ this.bwe.close();
653
796
  if (this.disposeTrack) {
654
797
  this.disposeTrack();
655
798
  this.disposeTrack = undefined;
@@ -657,6 +800,8 @@ class RTCRtpSender {
657
800
  if (!keepTrack) {
658
801
  this.track = null;
659
802
  }
803
+ this.dtlsDisposer.forEach((dispose) => dispose());
804
+ this.dtlsDisposer = [];
660
805
  }
661
806
  async runRtcp() {
662
807
  if (this.rtcpRunning || this.stopped)
@@ -667,19 +812,7 @@ class RTCRtpSender {
667
812
  await (0, promises_1.setTimeout)(500 + Math.random() * 1000, undefined, {
668
813
  signal: this.rtcpCancel.signal,
669
814
  });
670
- const packets = [
671
- new rtp_1.RtcpSrPacket({
672
- ssrc: this.ssrc,
673
- senderInfo: new rtp_1.RtcpSenderInfo({
674
- ntpTimestamp: this.ntpTimestamp,
675
- rtpTimestamp: this.rtpTimestamp,
676
- packetCount: this.packetCount,
677
- octetCount: this.octetCount,
678
- }),
679
- }),
680
- ];
681
- this.lastSRtimestamp = (0, utils_1.compactNtp)(this.ntpTimestamp);
682
- this.lastSentSRTimestamp = (0, utils_1.timestampSeconds)();
815
+ const packets = [this.buildSenderReport()];
683
816
  if (this.cname) {
684
817
  packets.push(new rtp_1.RtcpSourceDescriptionPacket({
685
818
  chunks: [
@@ -703,151 +836,393 @@ class RTCRtpSender {
703
836
  }
704
837
  catch (error) { }
705
838
  }
839
+ /** Sender Report for the instant `nowMonotonicMs` ({@link SenderStatistics.buildSenderReport}). */
840
+ buildSenderReport(nowMonotonicMs = performance.now()) {
841
+ return this.stats.buildSenderReport(this.ssrc, nowMonotonicMs);
842
+ }
706
843
  /**
707
- * Send an RTP packet. Pending RTP is disabled by default: the packet is
708
- * written immediately if DTLS is connected and a codec is set, otherwise
709
- * dropped.
710
- *
711
- * When pending RTP is enabled, the packet is queued until it can be sent.
712
- * The returned promise then settles for this packet only:
713
- * - resolve: DTLS send completed, or the packet was dropped by `stop()`,
714
- * `replaceTrack(null)`, or pending-queue overflow
715
- * - reject: DTLS send threw while writing this packet
716
- *
717
- * Later `sendRtp()` calls stay pending until their own packet is sent or
718
- * dropped, even if a drain is already in progress.
844
+ * Schedule RTP continuity rewrite for the next packet that is actually sent.
845
+ * The header argument is kept for API compatibility and is not used to compute
846
+ * offsets. `discontinuity` does not change sequence or timestamp mapping.
847
+ * `timestampStep` (default 1) is the only way to choose the timestamp increment
848
+ * at the source-switch boundary.
849
+ */
850
+ replaceRTP(header, discontinuity = false, timestampStep = 1) {
851
+ this.scheduleRtpContinuity(timestampStep);
852
+ log("replaceRTP", this.continuity.sequenceNumber, header.sequenceNumber, discontinuity, timestampStep);
853
+ }
854
+ /**
855
+ * Start a new source epoch and drop queued media of the previous source.
856
+ * A packet already past sequence allocation (in its DTLS write) completes;
857
+ * one still waiting on pacing/probe is dropped by {@link sendRtpInternal}.
858
+ */
859
+ invalidateQueuedSource() {
860
+ this.sourceEpoch++;
861
+ // Release the running FIFO task if it is a stale packet parked in a
862
+ // probe / pacing wait, so new-source media is not held behind it.
863
+ this.sourceEpochCancel.abort();
864
+ this.sourceEpochCancel = new AbortController();
865
+ this.outgoing.dropStaleSource(this.sourceEpoch);
866
+ this.pendingRtp.dropStaleSource(this.sourceEpoch);
867
+ }
868
+ isStaleSource(sourceEpoch) {
869
+ return sourceEpoch !== undefined && sourceEpoch !== this.sourceEpoch;
870
+ }
871
+ /**
872
+ * Abort signal for a send wait: sender stop, estimator swap, plus source
873
+ * switch for media.
874
+ */
875
+ sendWaitSignal(sourceEpoch) {
876
+ const signals = [this.rtcpCancel.signal, this.bwe.generationSignal];
877
+ if (sourceEpoch !== undefined) {
878
+ signals.push(this.sourceEpochCancel.signal);
879
+ }
880
+ return AbortSignal.any(signals);
881
+ }
882
+ /** A waiting send may still go out: not stopped, same estimator, same source. */
883
+ isSendAlive(generation, sourceEpoch) {
884
+ return (!this.stopped &&
885
+ generation === this.bwe.generation &&
886
+ !this.isStaleSource(sourceEpoch));
887
+ }
888
+ scheduleRtpContinuity(timestampStep = 1) {
889
+ this.invalidateQueuedSource();
890
+ this.continuity.schedule(timestampStep);
891
+ this.rtpCache = [];
892
+ }
893
+ dispatchMedia(packet, sourceEpoch) {
894
+ return this.dispatchOutgoing(() => this.sendRtpInternal(packet, {
895
+ injectProbePadding: true,
896
+ sourceEpoch,
897
+ }), { kind: "media", bytes: (0, rtpWire_1.estimateOutgoingBytes)(packet), sourceEpoch });
898
+ }
899
+ /**
900
+ * Lightweight / disabled estimators neither pace nor probe, so RTP is written
901
+ * straight through as before the BWE abstraction: no FIFO, no queue limit,
902
+ * and no wait before the DTLS write. Pacing estimators (GCC) go through
903
+ * {@link outgoing}. A FIFO still draining after an estimator swap
904
+ * keeps its order by queuing behind it.
905
+ */
906
+ dispatchOutgoing(task, meta) {
907
+ if (!this.outgoing.running && !(0, bandwidthEstimator_1.needsPacedSendPath)(this.bwe.estimator)) {
908
+ return task();
909
+ }
910
+ return this.outgoing.enqueue(task, meta);
911
+ }
912
+ /**
913
+ * Send immediately by default or queue until DTLS is ready when pending RTP
914
+ * is enabled. Each call settles only after its own packet is sent or dropped.
719
915
  */
720
916
  async sendRtp(rtp) {
721
- if (this.stopped) {
917
+ // Drop (do not queue) while the negotiated direction does not send, so
918
+ // nothing is flushed in a burst when sending resumes.
919
+ if (this.stopped || this.sendSuppressed) {
722
920
  return;
723
921
  }
724
922
  if (!this.pendingRtpEnabled) {
725
923
  if (!this.canSendRtp()) {
726
924
  return;
727
925
  }
728
- await this.dispatchRtp(rtp);
926
+ // Own the packet before any pacing wait: a track shared by several
927
+ // senders hands every subscriber the same RtpPacket, and header
928
+ // rewrites (SSRC/seq/extensions) must not leak across senders.
929
+ const packet = Buffer.isBuffer(rtp)
930
+ ? rtp_1.RtpPacket.deSerialize(rtp)
931
+ : rtp.clone();
932
+ await this.dispatchMedia(packet, this.sourceEpoch);
729
933
  return;
730
934
  }
731
935
  await new Promise((resolve, reject) => {
732
- this.enqueuePendingRtp(rtp, resolve, reject);
936
+ this.pendingRtp.enqueue(rtp, this.sourceEpoch, resolve, reject);
733
937
  void this.drainPendingRtp();
734
938
  });
735
939
  }
940
+ /** True when transport-wide CC header extension is negotiated. */
941
+ isTransportCcNegotiated() {
942
+ return this.headerExtensions.some((e) => e.uri === rtp_1.RTP_EXTENSION_URI.transportWideCC);
943
+ }
736
944
  /**
737
- * Schedule RTP continuity rewrite for the next packet that is actually sent.
738
- * The header argument is kept for API compatibility and is not used to compute
739
- * offsets. `discontinuity` does not change sequence or timestamp mapping.
740
- * `timestampStep` (default 1) is the only way to choose the timestamp increment
741
- * at the source-switch boundary.
945
+ * Padding needs an estimator that asks for it, TWCC so feedback can see it,
946
+ * and a negotiated RTX stream to carry it. Without RTX no padding is sent
947
+ * (see `GCC_KNOWN_DIFFERENCES`): media sequence numbers stay untouched.
742
948
  */
743
- replaceRTP(header, discontinuity = false, timestampStep = 1) {
744
- this.scheduleRtpContinuity(timestampStep);
745
- log("replaceRTP", this.sequenceNumber, header.sequenceNumber, discontinuity, timestampStep);
949
+ canSendPadding() {
950
+ return (!this.sendSuppressed &&
951
+ (0, bandwidthEstimator_1.isProbePacingController)(this.bwe.estimator) &&
952
+ this.rtxPayloadType !== undefined &&
953
+ this.isTransportCcNegotiated());
746
954
  }
747
- scheduleRtpContinuity(timestampStep = 1) {
748
- this.rtpContinuityPending = true;
749
- this.pendingTimestampStep = timestampStep;
750
- this.rtpCache = [];
955
+ /**
956
+ * Inject RTP padding packets while a GCC probe cluster is active and media
957
+ * alone has not yet filled min packets / min bitrate×duration.
958
+ *
959
+ * Responsibility split:
960
+ * - Estimator (`ProbeController` / `pendingProbePaddingPackets`) decides need
961
+ * - Sender generates padding-only RTP on the RTX SSRC with a TWCC seq +
962
+ * `isProbation` and paces them
963
+ */
964
+ async maybeInjectProbePadding() {
965
+ if (!this.canSendPadding()) {
966
+ return 0;
967
+ }
968
+ if (this.nestOutgoingPadding) {
969
+ return this.padding.drainProbe();
970
+ }
971
+ return ((await this.outgoing.enqueue(() => this.padding.drainProbe(), {
972
+ kind: "padding",
973
+ bytes: this.bwe.estimator.probePaddingPacketBytes,
974
+ })) ?? 0);
751
975
  }
752
- async dispatchRtp(rtp) {
753
- if (!this.canSendRtp()) {
754
- return;
976
+ /**
977
+ * pin padding_rate while LossBased is `kIncreaseUsingPadding`.
978
+ * Regular RTP padding (not probe/probation). Probe padding takes priority.
979
+ */
980
+ async maybeInjectLossPadding() {
981
+ if (!this.canSendPadding()) {
982
+ return 0;
755
983
  }
756
- const codec = this.codec;
757
- if (!codec) {
984
+ return ((await this.outgoing.enqueue(() => this.padding.drainLoss(), {
985
+ kind: "padding",
986
+ bytes: this.bwe.estimator.probePaddingPacketBytes,
987
+ })) ?? 0);
988
+ }
989
+ createPaddingInjector() {
990
+ const sender = this;
991
+ return new padding_1.PaddingInjector({
992
+ get estimator() {
993
+ return sender.bwe.estimator;
994
+ },
995
+ get generation() {
996
+ return sender.bwe.generation;
997
+ },
998
+ canDrain: () => !sender.stopped &&
999
+ sender.dtlsTransport?.state === "connected" &&
1000
+ !!sender.codec &&
1001
+ sender.canSendPadding(),
1002
+ isCancelled: (generation) => sender.stopped ||
1003
+ sender.sendSuppressed ||
1004
+ generation !== sender.bwe.generation,
1005
+ hasQueuedRealtimeMedia: () => sender.outgoing.hasQueuedRealtimeMedia(),
1006
+ sendPadding: (packetBytes, probe) => sender.sendRtpInternal(sender.createPaddingRtpPacket(packetBytes), {
1007
+ injectProbePadding: false,
1008
+ forceProbeTag: probe,
1009
+ isProbePadding: true,
1010
+ }),
1011
+ });
1012
+ }
1013
+ /** Padding-only RTP on the RTX SSRC / payload type ({@link createRtxPaddingPacket}). */
1014
+ createPaddingRtpPacket(paddingSize) {
1015
+ return (0, rtp_1.createRtxPaddingPacket)({
1016
+ paddingSize,
1017
+ timestamp: this.continuity.timestamp ?? 0,
1018
+ payloadType: this.rtxPayloadType,
1019
+ ssrc: this.rtxSsrc,
1020
+ });
1021
+ }
1022
+ async sendRtpInternal(rtp, opts = {}) {
1023
+ if (this.stopped)
1024
+ return;
1025
+ // Not sending in the negotiated direction: drop media and padding.
1026
+ // NACK retransmissions keep develop's behavior.
1027
+ if (this.sendSuppressed && !opts.isRetransmission)
1028
+ return;
1029
+ if (this.dtlsTransport.state !== "connected" || !this.codec) {
758
1030
  return;
759
1031
  }
760
- rtp = Buffer.isBuffer(rtp) ? rtp_1.RtpPacket.deSerialize(rtp) : rtp.clone();
1032
+ const sendClockRate = (this.sendPrimaryCodec ?? this.codec).clockRate;
1033
+ rtp = Buffer.isBuffer(rtp) ? rtp_1.RtpPacket.deSerialize(rtp) : rtp;
761
1034
  const { header, payload } = rtp;
1035
+ // Capture BWE generation at send start. After await sendRtp, only deliver
1036
+ // SentInfo when the generation is unchanged — mid-send setBandwidthEstimator
1037
+ // must not pollute the new clean estimator with packets planned under the old one.
1038
+ const sendGeneration = this.bwe.generation;
1039
+ const estimatorAtStart = this.bwe.estimator;
1040
+ const twccOn = this.isTransportCcNegotiated();
1041
+ // Lightweight / disabled: no probe reservation or pacing wait (direct path).
1042
+ const paced = (0, bandwidthEstimator_1.needsPacedSendPath)(estimatorAtStart);
1043
+ // Stale source: drop before touching probe reservation or pacing budget.
1044
+ if (this.isStaleSource(opts.sourceEpoch)) {
1045
+ return;
1046
+ }
1047
+ // pin CurrentCluster: reserve probe id **before** the async send so a
1048
+ // concurrent completion cannot re-attribute this packet to the next cluster.
1049
+ let reservedClusterId;
1050
+ const wantsProbe = opts.forceProbeTag === true || estimatorAtStart.shouldTagProbePacket();
1051
+ if (paced && twccOn && wantsProbe) {
1052
+ const reservation = estimatorAtStart.reserveOutgoingProbe((0, utils_1.milliTime)());
1053
+ if (reservation) {
1054
+ reservedClusterId = reservation.clusterId;
1055
+ if (!(await this.awaitProbeSendTime(reservation.nextSendTimeMs, sendGeneration, opts.sourceEpoch))) {
1056
+ return;
1057
+ }
1058
+ }
1059
+ else if (opts.forceProbeTag) {
1060
+ // Cluster already filled / discarded — do not emit untagged padding.
1061
+ return;
1062
+ }
1063
+ }
762
1064
  const inputSequenceNumber = header.sequenceNumber;
763
1065
  const inputTimestamp = header.timestamp;
764
- if (this.rtpContinuityPending) {
765
- if (this.sequenceNumber != undefined && this.timestamp != undefined) {
766
- const offsets = freezeRtpContinuityOffsets(this.sequenceNumber, this.timestamp, inputSequenceNumber, inputTimestamp, this.pendingTimestampStep);
767
- this.seqOffset = offsets.seqOffset;
768
- this.timestampOffset = offsets.timestampOffset;
769
- }
770
- this.rtpContinuityPending = false;
771
- this.pendingTimestampStep = 1;
1066
+ if (opts.isProbePadding) {
1067
+ // Padding is only ever sent on the negotiated RTX stream.
1068
+ if (this.rtxPayloadType === undefined)
1069
+ return;
1070
+ header.ssrc = this.rtxSsrc;
1071
+ header.payloadType = this.rtxPayloadType;
772
1072
  }
773
- header.ssrc = this.ssrc;
774
- header.payloadType = codec.payloadType;
775
- header.timestamp = (0, common_1.uint32Add)(header.timestamp, this.timestampOffset);
776
- header.sequenceNumber = (0, common_1.uint16Add)(header.sequenceNumber, this.seqOffset);
777
- this.timestamp = header.timestamp;
778
- this.sequenceNumber = header.sequenceNumber;
779
- const ntpTimestamp = (0, utils_1.ntpTime)();
780
- const originalHeaderExtensions = [...header.extensions];
781
- header.extensions = this.headerExtensions
782
- .map((extension) => {
783
- const payload = (() => {
784
- switch (extension.uri) {
785
- case rtp_1.RTP_EXTENSION_URI.sdesMid:
786
- if (this.mid) {
787
- return (0, rtp_1.serializeSdesMid)(this.mid);
788
- }
789
- return;
790
- // todo : sender simulcast unsupported now
791
- case rtp_1.RTP_EXTENSION_URI.sdesRTPStreamID:
792
- if (this.rtpStreamId) {
793
- return (0, rtp_1.serializeSdesRTPStreamID)(this.rtpStreamId);
794
- }
795
- return;
796
- // todo : sender simulcast unsupported now
797
- case rtp_1.RTP_EXTENSION_URI.repairedRtpStreamId:
798
- if (this.repairedRtpStreamId) {
799
- return (0, rtp_1.serializeRepairedRtpStreamId)(this.repairedRtpStreamId);
800
- }
801
- return;
802
- case rtp_1.RTP_EXTENSION_URI.transportWideCC:
803
- this.dtlsTransport.transportSequenceNumber = (0, common_1.uint16Add)(this.dtlsTransport.transportSequenceNumber, 1);
804
- return (0, rtp_1.serializeTransportWideCC)(this.dtlsTransport.transportSequenceNumber);
805
- case rtp_1.RTP_EXTENSION_URI.absSendTime:
806
- return (0, rtp_1.serializeAbsSendTime)(ntpTimestamp);
807
- }
808
- })();
809
- if (payload)
810
- return { id: extension.id, payload };
811
- })
812
- .filter((v) => v);
813
- for (const ext of originalHeaderExtensions) {
814
- const exist = header.extensions.find((v) => v.id === ext.id);
815
- if (exist) {
816
- exist.payload = ext.payload;
1073
+ else if (!opts.isRetransmission) {
1074
+ header.ssrc = this.ssrc;
1075
+ header.payloadType = this.codec.payloadType;
1076
+ header.timestamp = this.continuity.mapTimestamp(inputTimestamp);
1077
+ }
1078
+ header.extensions = (0, rtp_1.buildSendHeaderExtensions)(header.extensions, this.headerExtensions, {
1079
+ mid: this.mid,
1080
+ rtpStreamId: this.rtpStreamId,
1081
+ repairedRtpStreamId: this.repairedRtpStreamId,
1082
+ });
1083
+ const useRed = !!this.redRedundantPayloadType &&
1084
+ !opts.isProbePadding &&
1085
+ !opts.isRetransmission;
1086
+ const estimatedPayloadLen = useRed
1087
+ ? this.redEncoder.serializedLengthWith({
1088
+ block: payload,
1089
+ timestamp: header.timestamp,
1090
+ blockPT: this.redRedundantPayloadType,
1091
+ })
1092
+ : payload.length;
1093
+ const rfcPaddingLen = header.padding && header.paddingSize > 0 ? header.paddingSize : 0;
1094
+ const constructedBytes = header.serializeSize + estimatedPayloadLen + rfcPaddingLen;
1095
+ if (paced &&
1096
+ reservedClusterId === undefined &&
1097
+ this.mediaPacingEnabled &&
1098
+ twccOn &&
1099
+ estimatorAtStart.getPacingBitrateBps() > 0) {
1100
+ if (!(await this.awaitPacingBudget(constructedBytes, sendGeneration, opts.sourceEpoch))) {
1101
+ return;
817
1102
  }
818
- else {
819
- header.extensions.push(ext);
1103
+ }
1104
+ if (this.stopped || sendGeneration !== this.bwe.generation) {
1105
+ return;
1106
+ }
1107
+ // Direction changed to not sending while this packet waited.
1108
+ if (this.sendSuppressed && !opts.isRetransmission) {
1109
+ return;
1110
+ }
1111
+ // Accepted under a replaced/detached source: never send it, and never let
1112
+ // it consume the new source's continuity offset.
1113
+ if (this.isStaleSource(opts.sourceEpoch)) {
1114
+ return;
1115
+ }
1116
+ if (opts.isProbePadding) {
1117
+ // RTX owns its sequence space, so padding never shifts media numbers.
1118
+ header.sequenceNumber = this.rtxSequenceNumber;
1119
+ this.rtxSequenceNumber = (0, common_1.uint16Add)(this.rtxSequenceNumber, 1);
1120
+ header.timestamp = this.continuity.timestamp ?? header.timestamp;
1121
+ }
1122
+ else if (!opts.isRetransmission) {
1123
+ if (this.continuity.assign(header, inputSequenceNumber, inputTimestamp)) {
1124
+ // ソース境界を SR の RTP/NTP 基準にも反映する。
1125
+ this.stats.resetClockAnchor();
820
1126
  }
821
1127
  }
822
- header.extensions = header.extensions.sort((a, b) => a.id - b.id);
823
- this.ntpTimestamp = ntpTimestamp;
824
- this.rtpTimestamp = header.timestamp;
825
- this.octetCount += payload.length;
826
- this.headerBytesSent += header.serializeSize;
827
- this.packetCount = (0, common_1.uint32Add)(this.packetCount, 1);
828
- this.rtpCache[header.sequenceNumber % RTP_HISTORY_SIZE] = rtp;
829
1128
  let rtpPayload = payload;
830
- if (this.redRedundantPayloadType) {
1129
+ if (useRed) {
831
1130
  this.redEncoder.push({
832
1131
  block: rtpPayload,
833
1132
  timestamp: header.timestamp,
834
1133
  blockPT: this.redRedundantPayloadType,
835
1134
  });
836
- const red = this.redEncoder.build();
837
- rtpPayload = red.serialize();
1135
+ rtpPayload = this.redEncoder.build().serialize();
1136
+ }
1137
+ const payloadOctetsForSr = rtpPayload.length;
1138
+ if (header.padding && header.paddingSize > 0) {
1139
+ rtpPayload = (0, rtp_1.appendRfc3550Padding)(rtpPayload, header.paddingSize);
1140
+ }
1141
+ const packetWideSeq = (0, rtp_1.stampHopByHopExtensions)(header, this.headerExtensions, {
1142
+ twccOn,
1143
+ allocateTransportWideSequence: () => this.dtlsTransport.allocateTransportWideSequence(),
1144
+ ntpTimestamp: (0, utils_1.ntpTime)(),
1145
+ });
1146
+ if (opts.isProbePadding) {
1147
+ // RTX padding: not part of the media SSRC's SR packet / octet counts.
1148
+ }
1149
+ else if (opts.isRetransmission) {
1150
+ this.stats.recordRetransmissionSent(header.serializeSize, payload.length);
1151
+ }
1152
+ else {
1153
+ this.stats.recordMediaSent(header.serializeSize, payloadOctetsForSr);
1154
+ if (!(0, rtp_1.isPaddingOnlyRtpPacket)(rtp)) {
1155
+ this.rtpCache[header.sequenceNumber % RTP_HISTORY_SIZE] = rtp;
1156
+ }
1157
+ }
1158
+ const sendingAtMs = (0, utils_1.milliTime)();
1159
+ const { size, sendingAtMs: enqueueAtMs } = await (0, rtpWire_1.sendDtlsRtp)(this.dtlsTransport, rtpPayload, header, sendingAtMs);
1160
+ const sentAtMs = (0, utils_1.milliTime)();
1161
+ if (!opts.isProbePadding &&
1162
+ !opts.isRetransmission &&
1163
+ !(0, rtp_1.isPaddingOnlyRtpPacket)(rtp)) {
1164
+ this.stats.updateClockAnchor(header.timestamp, sendClockRate);
838
1165
  }
839
- const size = await this.dtlsTransport.sendRtp(rtpPayload, header);
840
1166
  this.runRtcp();
841
- const millitime = (0, utils_1.milliTime)();
842
- const sentInfo = {
843
- wideSeq: this.dtlsTransport.transportSequenceNumber,
844
- size,
845
- sendingAtMs: millitime,
846
- sentAtMs: millitime,
847
- };
848
- this.senderBWE.rtpPacketSent(sentInfo);
1167
+ // BWE / TWCC only when transport-cc is negotiated — otherwise wideSeq would
1168
+ // not advance and probe padding would be useless / harmful.
1169
+ // Generation match: estimator was not swapped (or reset) while this packet
1170
+ // was in flight. Mismatch → discard (do not feed disposed or new clean BWE).
1171
+ if (twccOn &&
1172
+ packetWideSeq !== undefined &&
1173
+ sendGeneration === this.bwe.generation) {
1174
+ const priorUnacked = this.pendingUntrackedBytes;
1175
+ this.pendingUntrackedBytes = 0;
1176
+ const sentInfo = {
1177
+ wideSeq: packetWideSeq,
1178
+ size,
1179
+ sendingAtMs: enqueueAtMs,
1180
+ sentAtMs,
1181
+ isProbation: reservedClusterId !== undefined,
1182
+ probeClusterId: reservedClusterId,
1183
+ priorUnackedBytes: priorUnacked,
1184
+ isRetransmission: opts.isRetransmission === true,
1185
+ };
1186
+ estimatorAtStart.rtpPacketSent(sentInfo);
1187
+ }
1188
+ else if (twccOn && sendGeneration === this.bwe.generation) {
1189
+ // Same generation but no wideSeq (should be rare). Do not fold
1190
+ // previous-generation bytes into the next estimator.
1191
+ this.pendingUntrackedBytes += size;
1192
+ }
1193
+ if (opts.injectProbePadding &&
1194
+ twccOn &&
1195
+ (0, bandwidthEstimator_1.isProbePacingController)(estimatorAtStart) &&
1196
+ sendGeneration === this.bwe.generation) {
1197
+ this.nestOutgoingPadding = true;
1198
+ try {
1199
+ await this.maybeInjectProbePadding();
1200
+ }
1201
+ finally {
1202
+ this.nestOutgoingPadding = false;
1203
+ }
1204
+ }
1205
+ }
1206
+ /** pin BitrateProber next_probe_time wait ({@link waitForProbeSendTime}). */
1207
+ awaitProbeSendTime(nextSendTimeMs, generation, sourceEpoch) {
1208
+ return (0, sendPacer_1.waitForProbeSendTime)(nextSendTimeMs, () => this.isSendAlive(generation, sourceEpoch), () => this.sendWaitSignal(sourceEpoch));
1209
+ }
1210
+ /**
1211
+ * Token-bucket wait against {@link pacingBitrateBps} for **media**.
1212
+ * Probe packets use {@link awaitProbeSendTime} instead.
1213
+ * Returns false if the sender is stopped, the estimator is swapped
1214
+ * (`generation` changes), or the packet's source is replaced/detached while
1215
+ * waiting (budget is not consumed then).
1216
+ */
1217
+ awaitPacingBudget(packetBytes, generation, sourceEpoch) {
1218
+ return this.pacer.acquire(packetBytes, this.pacingBitrateBps, () => this.isSendAlive(generation, sourceEpoch), () => this.sendWaitSignal(sourceEpoch));
849
1219
  }
850
- handleRtcpPacket(rtcpPacket) {
1220
+ /**
1221
+ * @param firEntries Internal: the FCI entries of a FIR the router resolved to
1222
+ * this sender (see `KeyFrameRequestRouting`). Defaults to the entries for
1223
+ * the own SSRC.
1224
+ */
1225
+ handleRtcpPacket(rtcpPacket, firEntries) {
851
1226
  switch (rtcpPacket.type) {
852
1227
  case rtp_1.RtcpSrPacket.type:
853
1228
  case rtp_1.RtcpRrPacket.type:
@@ -856,22 +1231,12 @@ class RTCRtpSender {
856
1231
  packet.reports
857
1232
  .filter((report) => report.ssrc === this.ssrc)
858
1233
  .forEach((report) => {
859
- this.remotePacketsLost = report.packetsLost;
860
- this.remoteFractionLost = report.fractionLost / 256;
861
- if (this.lastSRtimestamp === report.lsr && report.dlsr) {
862
- if (this.lastSentSRTimestamp) {
863
- const rtt = (0, utils_1.timestampSeconds)() -
864
- this.lastSentSRTimestamp -
865
- report.dlsr / 65536;
866
- this.totalRoundTripTime += rtt;
867
- this.roundTripTimeMeasurements++;
868
- if (this.rtt === undefined) {
869
- this.rtt = rtt;
870
- }
871
- else {
872
- this.rtt = RTT_ALPHA * this.rtt + (1 - RTT_ALPHA) * rtt;
873
- }
874
- }
1234
+ const rawRttSeconds = this.stats.recordReceptionReport(report);
1235
+ // pin OnRoundTripTimeUpdate: only **unsmoothed** RTT goes to
1236
+ // AIMD (smoothed updates are discarded in GoogCc).
1237
+ // Lightweight / disabled no-op setRoundTripTime.
1238
+ if (rawRttSeconds !== undefined && rawRttSeconds > 0) {
1239
+ this.bwe.estimator.setRoundTripTime(rawRttSeconds * 1000);
875
1240
  }
876
1241
  });
877
1242
  }
@@ -889,21 +1254,30 @@ class RTCRtpSender {
889
1254
  case rtp_1.GenericNack.count:
890
1255
  {
891
1256
  const feedback = packet.feedback;
892
- this.nackCount++;
1257
+ this.stats.nackCount++;
893
1258
  feedback.lost.forEach(async (seqNum) => {
894
1259
  let packet = this.rtpCache[seqNum % RTP_HISTORY_SIZE];
895
1260
  if (packet && packet.header.sequenceNumber !== seqNum) {
896
1261
  packet = undefined;
897
1262
  }
1263
+ if (packet && (0, rtp_1.isPaddingOnlyRtpPacket)(packet)) {
1264
+ packet = undefined;
1265
+ }
898
1266
  if (packet) {
899
1267
  if (this.rtxPayloadType != undefined) {
900
1268
  packet = (0, rtp_1.wrapRtx)(packet, this.rtxPayloadType, this.rtxSequenceNumber, this.rtxSsrc);
901
1269
  this.rtxSequenceNumber = (0, common_1.uint16Add)(this.rtxSequenceNumber, 1);
902
1270
  }
903
- this.retransmittedPacketsSent++;
904
- this.retransmittedBytesSent += packet.payload.length;
905
- this.headerBytesSent += packet.header.serializeSize;
906
- await this.dtlsTransport.sendRtp(packet.payload, packet.header);
1271
+ const retransmission = packet;
1272
+ // Route through sendRtpInternal: new TWCC seq, pacing,
1273
+ // BWE SentInfo. Do not reuse the original transport-wide seq.
1274
+ await this.dispatchOutgoing(() => this.sendRtpInternal(retransmission, {
1275
+ injectProbePadding: false,
1276
+ isRetransmission: true,
1277
+ }), {
1278
+ kind: "rtx",
1279
+ bytes: (0, rtpWire_1.estimateOutgoingBytes)(retransmission),
1280
+ });
907
1281
  }
908
1282
  });
909
1283
  this.onGenericNack.execute(feedback);
@@ -915,7 +1289,7 @@ class RTCRtpSender {
915
1289
  case rtp_1.RtcpPayloadSpecificFeedback.type:
916
1290
  {
917
1291
  const packet = rtcpPacket;
918
- switch (packet.feedback.count) {
1292
+ switch (packet.feedback?.count) {
919
1293
  case rtp_1.ReceiverEstimatedMaxBitrate.count:
920
1294
  {
921
1295
  const feedback = packet.feedback;
@@ -924,8 +1298,35 @@ class RTCRtpSender {
924
1298
  break;
925
1299
  case rtp_1.PictureLossIndication.count:
926
1300
  {
927
- this.pliCount++;
1301
+ this.stats.pliCount++;
928
1302
  this.onPictureLossIndication.execute();
1303
+ this.onKeyFrameRequest.execute("pli");
1304
+ }
1305
+ break;
1306
+ case rtp_1.FullIntraRequest.count:
1307
+ {
1308
+ const feedback = packet.feedback;
1309
+ const entries = firEntries ??
1310
+ feedback.fir.filter(({ ssrc }) => ssrc === this.ssrc);
1311
+ if (entries.length === 0)
1312
+ break;
1313
+ // W3C webrtc-stats: firCount is the number of FIR packets received,
1314
+ // however many FCI entries of the packet resolve to this sender
1315
+ this.stats.firCount++;
1316
+ // an entry with the same sequence number as the last one for its
1317
+ // (sender, target) pair is a retransmission (RFC 5104 4.3.1.1)
1318
+ let isNew = false;
1319
+ for (const { ssrc, sequenceNumber } of entries) {
1320
+ const key = `${feedback.senderSsrc}:${ssrc}`;
1321
+ if (this.lastFirSequenceNumbers.get(key) !== sequenceNumber) {
1322
+ isNew = true;
1323
+ }
1324
+ this.lastFirSequenceNumbers.set(key, sequenceNumber);
1325
+ }
1326
+ if (isNew) {
1327
+ this.onFullIntraRequest.execute(feedback);
1328
+ this.onKeyFrameRequest.execute("fir");
1329
+ }
929
1330
  }
930
1331
  break;
931
1332
  }
@@ -946,100 +1347,22 @@ class RTCRtpSender {
946
1347
  this.setSendEncodings(params.encodings);
947
1348
  }
948
1349
  }
949
- get outboundRtpStatsId() {
950
- return (0, stats_1.generateStatsId)("outbound-rtp", this.trackId);
951
- }
952
- get mediaSourceStatsId() {
953
- return (0, stats_1.generateStatsId)("media-source", this.trackId);
954
- }
955
- get remoteInboundRtpStatsId() {
956
- return (0, stats_1.generateStatsId)("remote-inbound-rtp", this.trackId);
957
- }
958
1350
  getStatsRootIds() {
959
- return [this.outboundRtpStatsId];
1351
+ return [(0, senderStats_1.senderStatsIds)(this.trackId).outboundRtp];
960
1352
  }
961
1353
  collectStats(timestamp) {
962
- const stats = [];
963
- const transportId = this.dtlsTransport
964
- ? (0, stats_1.generateStatsId)("transport", this.dtlsTransport.id)
965
- : undefined;
966
- const codecId = this.codec && transportId
967
- ? (0, stats_1.generateCodecStatsId)(transportId, this.codec.payloadType, this.trackId)
968
- : undefined;
969
- // Outbound RTP stats
970
- const outboundRtpStats = {
971
- type: "outbound-rtp",
972
- id: this.outboundRtpStatsId,
973
- timestamp,
1354
+ return this.stats.collect({
1355
+ trackId: this.trackId,
974
1356
  ssrc: this.ssrc,
975
1357
  kind: this.kind,
976
- transportId,
977
- codecId,
978
1358
  mid: this.mid,
979
- packetsSent: this.packetCount,
980
- bytesSent: this.octetCount,
981
- headerBytesSent: this.headerBytesSent,
982
- retransmittedPacketsSent: this.retransmittedPacketsSent || undefined,
983
- retransmittedBytesSent: this.retransmittedBytesSent || undefined,
1359
+ dtlsTransport: this.dtlsTransport,
1360
+ codec: this.codec,
984
1361
  rtxSsrc: this.rtxPayloadType ? this.rtxSsrc : undefined,
985
- mediaSourceId: this.track ? this.mediaSourceStatsId : undefined,
986
- remoteId: this.rtt !== undefined ||
987
- this.remotePacketsLost !== undefined ||
988
- this.remoteFractionLost !== undefined
989
- ? this.remoteInboundRtpStatsId
1362
+ trackIdentifier: this.track
1363
+ ? (this.track.id ?? this.trackId)
990
1364
  : undefined,
991
- nackCount: this.nackCount || undefined,
992
- pliCount: this.pliCount || undefined,
993
- firCount: this.firCount || undefined,
994
- };
995
- stats.push(outboundRtpStats);
996
- // Media source stats
997
- if (this.track) {
998
- const mediaSourceStats = {
999
- type: "media-source",
1000
- id: this.mediaSourceStatsId,
1001
- timestamp,
1002
- trackIdentifier: this.track.id ?? this.trackId,
1003
- kind: this.kind,
1004
- };
1005
- stats.push(mediaSourceStats);
1006
- }
1007
- if (this.codec && transportId) {
1008
- const codecStats = {
1009
- type: "codec",
1010
- id: codecId,
1011
- timestamp,
1012
- payloadType: this.codec.payloadType,
1013
- transportId,
1014
- mimeType: this.codec.mimeType,
1015
- clockRate: this.codec.clockRate,
1016
- channels: this.codec.channels,
1017
- sdpFmtpLine: this.codec.parameters,
1018
- };
1019
- stats.push(codecStats);
1020
- }
1021
- // Remote inbound RTP stats (if we have RTT)
1022
- if (this.rtt !== undefined ||
1023
- this.remotePacketsLost !== undefined ||
1024
- this.remoteFractionLost !== undefined) {
1025
- const remoteInboundStats = {
1026
- type: "remote-inbound-rtp",
1027
- id: this.remoteInboundRtpStatsId,
1028
- timestamp,
1029
- ssrc: this.ssrc,
1030
- kind: this.kind,
1031
- transportId,
1032
- codecId: outboundRtpStats.codecId,
1033
- localId: outboundRtpStats.id,
1034
- roundTripTime: this.rtt,
1035
- totalRoundTripTime: this.totalRoundTripTime,
1036
- roundTripTimeMeasurements: this.roundTripTimeMeasurements,
1037
- packetsLost: this.remotePacketsLost,
1038
- fractionLost: this.remoteFractionLost,
1039
- };
1040
- stats.push(remoteInboundStats);
1041
- }
1042
- return stats;
1365
+ }, timestamp);
1043
1366
  }
1044
1367
  async getStats() {
1045
1368
  const timestamp = (0, stats_1.getStatsTimestamp)();
@@ -1051,4 +1374,17 @@ class RTCRtpSender {
1051
1374
  }
1052
1375
  }
1053
1376
  exports.RTCRtpSender = RTCRtpSender;
1377
+ /** The codec RTP is sent with: the first non-RTX codec. */
1378
+ function sendCodecOf(codecs) {
1379
+ return (codecs.find((codec) => codec.name.toLowerCase() !== "rtx") ?? codecs[0]);
1380
+ }
1381
+ /** The media codec RTP carries: RED's primary payload, else the send codec. */
1382
+ function primarySendCodecOf(codecs) {
1383
+ const codec = sendCodecOf(codecs);
1384
+ if (codec?.name.toLowerCase() !== "red")
1385
+ return codec;
1386
+ const primaryPayloadType = Number((codec.parameters ?? "").split("/")[0]);
1387
+ return codecs.find((candidate) => !["red", "rtx"].includes(candidate.name.toLowerCase()) &&
1388
+ candidate.payloadType === primaryPayloadType);
1389
+ }
1054
1390
  //# sourceMappingURL=rtpSender.js.map