werift 0.26.0 → 0.28.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 (552) hide show
  1. package/README.md +182 -8
  2. package/THIRD_PARTY_LICENSES_LIBWEBRTC.txt +58 -0
  3. package/lib/common/src/binary.d.ts +1 -3
  4. package/lib/common/src/binary.js +8 -6
  5. package/lib/common/src/binary.js.map +1 -1
  6. package/lib/common/src/binaryData.js +2 -1
  7. package/lib/common/src/binaryData.js.map +1 -1
  8. package/lib/common/src/event.d.ts +26 -0
  9. package/lib/common/src/event.js +87 -2
  10. package/lib/common/src/event.js.map +1 -1
  11. package/lib/common/src/log.js +5 -7
  12. package/lib/common/src/log.js.map +1 -1
  13. package/lib/common/src/network.d.ts +6 -1
  14. package/lib/common/src/network.js +8 -2
  15. package/lib/common/src/network.js.map +1 -1
  16. package/lib/common/src/transport.d.ts +12 -5
  17. package/lib/common/src/transport.js +13 -7
  18. package/lib/common/src/transport.js.map +1 -1
  19. package/lib/dtls/src/alert.d.ts +49 -0
  20. package/lib/dtls/src/alert.js +103 -0
  21. package/lib/dtls/src/alert.js.map +1 -0
  22. package/lib/dtls/src/client.js +15 -1
  23. package/lib/dtls/src/client.js.map +1 -1
  24. package/lib/dtls/src/context/cipher.d.ts +1 -1
  25. package/lib/dtls/src/context/cipher.js +6 -3
  26. package/lib/dtls/src/context/cipher.js.map +1 -1
  27. package/lib/dtls/src/context/dtls.d.ts +2 -0
  28. package/lib/dtls/src/context/dtls.js +11 -2
  29. package/lib/dtls/src/context/dtls.js.map +1 -1
  30. package/lib/dtls/src/context/transport.d.ts +17 -1
  31. package/lib/dtls/src/context/transport.js +40 -3
  32. package/lib/dtls/src/context/transport.js.map +1 -1
  33. package/lib/dtls/src/flight/client/flight1.js +2 -1
  34. package/lib/dtls/src/flight/client/flight1.js.map +1 -1
  35. package/lib/dtls/src/flight/client/flight5.js +8 -3
  36. package/lib/dtls/src/flight/client/flight5.js.map +1 -1
  37. package/lib/dtls/src/flight/flight.d.ts +6 -1
  38. package/lib/dtls/src/flight/flight.js +18 -5
  39. package/lib/dtls/src/flight/flight.js.map +1 -1
  40. package/lib/dtls/src/flight/server/flight4.js +4 -2
  41. package/lib/dtls/src/flight/server/flight4.js.map +1 -1
  42. package/lib/dtls/src/flight/server/flight6.js +6 -2
  43. package/lib/dtls/src/flight/server/flight6.js.map +1 -1
  44. package/lib/dtls/src/handshake/const.d.ts +17 -13
  45. package/lib/dtls/src/handshake/const.js +16 -14
  46. package/lib/dtls/src/handshake/const.js.map +1 -1
  47. package/lib/dtls/src/handshake/message/alert.js +4 -2
  48. package/lib/dtls/src/handshake/message/alert.js.map +1 -1
  49. package/lib/dtls/src/handshake/message/certificate.js +2 -1
  50. package/lib/dtls/src/handshake/message/certificate.js.map +1 -1
  51. package/lib/dtls/src/handshake/message/changeCipherSpec.js +2 -1
  52. package/lib/dtls/src/handshake/message/changeCipherSpec.js.map +1 -1
  53. package/lib/dtls/src/handshake/message/client/certificateVerify.js +4 -2
  54. package/lib/dtls/src/handshake/message/client/certificateVerify.js.map +1 -1
  55. package/lib/dtls/src/handshake/message/client/hello.js +14 -7
  56. package/lib/dtls/src/handshake/message/client/hello.js.map +1 -1
  57. package/lib/dtls/src/handshake/message/client/keyExchange.js +2 -1
  58. package/lib/dtls/src/handshake/message/client/keyExchange.js.map +1 -1
  59. package/lib/dtls/src/handshake/message/finished.js +2 -1
  60. package/lib/dtls/src/handshake/message/finished.js.map +1 -1
  61. package/lib/dtls/src/handshake/message/server/certificateRequest.js +6 -3
  62. package/lib/dtls/src/handshake/message/server/certificateRequest.js.map +1 -1
  63. package/lib/dtls/src/handshake/message/server/hello.js +12 -6
  64. package/lib/dtls/src/handshake/message/server/hello.js.map +1 -1
  65. package/lib/dtls/src/handshake/message/server/helloDone.js.map +1 -1
  66. package/lib/dtls/src/handshake/message/server/helloVerifyRequest.js +4 -2
  67. package/lib/dtls/src/handshake/message/server/helloVerifyRequest.js.map +1 -1
  68. package/lib/dtls/src/handshake/message/server/keyExchange.js +16 -8
  69. package/lib/dtls/src/handshake/message/server/keyExchange.js.map +1 -1
  70. package/lib/dtls/src/handshake/random.d.ts +2 -2
  71. package/lib/dtls/src/handshake/random.js +5 -2
  72. package/lib/dtls/src/handshake/random.js.map +1 -1
  73. package/lib/dtls/src/index.d.ts +3 -1
  74. package/lib/dtls/src/index.js +10 -1
  75. package/lib/dtls/src/index.js.map +1 -1
  76. package/lib/dtls/src/record/const.d.ts +52 -33
  77. package/lib/dtls/src/record/const.js +51 -36
  78. package/lib/dtls/src/record/const.js.map +1 -1
  79. package/lib/dtls/src/record/message/fragment.js +12 -6
  80. package/lib/dtls/src/record/message/fragment.js.map +1 -1
  81. package/lib/dtls/src/record/message/header.js +10 -5
  82. package/lib/dtls/src/record/message/header.js.map +1 -1
  83. package/lib/dtls/src/record/message/plaintext.js +4 -2
  84. package/lib/dtls/src/record/message/plaintext.js.map +1 -1
  85. package/lib/dtls/src/record/receive.d.ts +6 -0
  86. package/lib/dtls/src/record/receive.js +50 -19
  87. package/lib/dtls/src/record/receive.js.map +1 -1
  88. package/lib/dtls/src/server.js +19 -1
  89. package/lib/dtls/src/server.js.map +1 -1
  90. package/lib/dtls/src/socket.d.ts +134 -2
  91. package/lib/dtls/src/socket.js +326 -8
  92. package/lib/dtls/src/socket.js.map +1 -1
  93. package/lib/ice/src/candidate.d.ts +1 -1
  94. package/lib/ice/src/candidate.js +24 -12
  95. package/lib/ice/src/candidate.js.map +1 -1
  96. package/lib/ice/src/candidateError.d.ts +155 -0
  97. package/lib/ice/src/candidateError.js +246 -0
  98. package/lib/ice/src/candidateError.js.map +1 -0
  99. package/lib/ice/src/candidateGatherer.d.ts +127 -0
  100. package/lib/ice/src/candidateGatherer.js +745 -0
  101. package/lib/ice/src/candidateGatherer.js.map +1 -0
  102. package/lib/ice/src/consent.d.ts +49 -0
  103. package/lib/ice/src/consent.js +258 -0
  104. package/lib/ice/src/consent.js.map +1 -0
  105. package/lib/ice/src/dns/lookup.d.ts +10 -1
  106. package/lib/ice/src/dns/lookup.js +42 -7
  107. package/lib/ice/src/dns/lookup.js.map +1 -1
  108. package/lib/ice/src/exceptions.d.ts +21 -5
  109. package/lib/ice/src/exceptions.js +58 -13
  110. package/lib/ice/src/exceptions.js.map +1 -1
  111. package/lib/ice/src/ice.d.ts +254 -58
  112. package/lib/ice/src/ice.js +979 -945
  113. package/lib/ice/src/ice.js.map +1 -1
  114. package/lib/ice/src/iceBase.d.ts +328 -14
  115. package/lib/ice/src/iceBase.js +254 -38
  116. package/lib/ice/src/iceBase.js.map +1 -1
  117. package/lib/ice/src/index.d.ts +1 -0
  118. package/lib/ice/src/index.js +1 -0
  119. package/lib/ice/src/index.js.map +1 -1
  120. package/lib/ice/src/internal/checkMessages.d.ts +41 -0
  121. package/lib/ice/src/internal/checkMessages.js +63 -0
  122. package/lib/ice/src/internal/checkMessages.js.map +1 -0
  123. package/lib/ice/src/internal/defaultRoute.d.ts +20 -0
  124. package/lib/ice/src/internal/defaultRoute.js +134 -0
  125. package/lib/ice/src/internal/defaultRoute.js.map +1 -0
  126. package/lib/ice/src/internal/hostInterfaces.d.ts +17 -0
  127. package/lib/ice/src/internal/hostInterfaces.js +77 -0
  128. package/lib/ice/src/internal/hostInterfaces.js.map +1 -0
  129. package/lib/ice/src/internal/hostPreference.d.ts +24 -0
  130. package/lib/ice/src/internal/hostPreference.js +38 -0
  131. package/lib/ice/src/internal/hostPreference.js.map +1 -0
  132. package/lib/ice/src/internal/resolveHostAddresses.d.ts +56 -8
  133. package/lib/ice/src/internal/resolveHostAddresses.js +127 -24
  134. package/lib/ice/src/internal/resolveHostAddresses.js.map +1 -1
  135. package/lib/ice/src/internal/selectAddresses.d.ts +27 -0
  136. package/lib/ice/src/internal/selectAddresses.js +37 -18
  137. package/lib/ice/src/internal/selectAddresses.js.map +1 -1
  138. package/lib/ice/src/nomination/regularNomination.d.ts +200 -0
  139. package/lib/ice/src/nomination/regularNomination.js +311 -0
  140. package/lib/ice/src/nomination/regularNomination.js.map +1 -0
  141. package/lib/ice/src/nomination/selectionReplacement.d.ts +138 -0
  142. package/lib/ice/src/nomination/selectionReplacement.js +371 -0
  143. package/lib/ice/src/nomination/selectionReplacement.js.map +1 -0
  144. package/lib/ice/src/provisional.d.ts +139 -0
  145. package/lib/ice/src/provisional.js +468 -0
  146. package/lib/ice/src/provisional.js.map +1 -0
  147. package/lib/ice/src/stun/protocol.d.ts +9 -2
  148. package/lib/ice/src/stun/protocol.js +16 -17
  149. package/lib/ice/src/stun/protocol.js.map +1 -1
  150. package/lib/ice/src/stun/tcpProtocol.d.ts +1 -1
  151. package/lib/ice/src/stun/tcpProtocol.js +7 -7
  152. package/lib/ice/src/stun/tcpProtocol.js.map +1 -1
  153. package/lib/ice/src/stun/transaction.d.ts +30 -1
  154. package/lib/ice/src/stun/transaction.js +90 -19
  155. package/lib/ice/src/stun/transaction.js.map +1 -1
  156. package/lib/ice/src/turn/credential.d.ts +49 -0
  157. package/lib/ice/src/turn/credential.js +78 -0
  158. package/lib/ice/src/turn/credential.js.map +1 -0
  159. package/lib/ice/src/turn/protocol.d.ts +31 -10
  160. package/lib/ice/src/turn/protocol.js +127 -51
  161. package/lib/ice/src/turn/protocol.js.map +1 -1
  162. package/lib/ice/src/types/model.d.ts +16 -5
  163. package/lib/ice/src/types/model.js.map +1 -1
  164. package/lib/ice-server/src/stun/attributes.d.ts +2 -0
  165. package/lib/ice-server/src/stun/attributes.js +11 -1
  166. package/lib/ice-server/src/stun/attributes.js.map +1 -1
  167. package/lib/ice-server/src/stun/const.d.ts +44 -17
  168. package/lib/ice-server/src/stun/const.js +31 -19
  169. package/lib/ice-server/src/stun/const.js.map +1 -1
  170. package/lib/ice-server/src/stun/message.d.ts +29 -4
  171. package/lib/ice-server/src/stun/message.js +115 -24
  172. package/lib/ice-server/src/stun/message.js.map +1 -1
  173. package/lib/index.mjs +35121 -22337
  174. package/lib/nonstandard/index.mjs +3169 -1399
  175. package/lib/polyfill/dom.mjs +24650 -12365
  176. package/lib/polyfill/index.mjs +24650 -12365
  177. package/lib/rtp/src/codec/assertPayload.d.ts +4 -0
  178. package/lib/rtp/src/codec/assertPayload.js +12 -0
  179. package/lib/rtp/src/codec/assertPayload.js.map +1 -0
  180. package/lib/rtp/src/codec/av1.js +2 -0
  181. package/lib/rtp/src/codec/av1.js.map +1 -1
  182. package/lib/rtp/src/codec/h264.js +35 -43
  183. package/lib/rtp/src/codec/h264.js.map +1 -1
  184. package/lib/rtp/src/codec/vp8.js +11 -5
  185. package/lib/rtp/src/codec/vp8.js.map +1 -1
  186. package/lib/rtp/src/codec/vp9.js +12 -0
  187. package/lib/rtp/src/codec/vp9.js.map +1 -1
  188. package/lib/rtp/src/extra/container/mp4/container.js +2 -1
  189. package/lib/rtp/src/extra/container/mp4/container.js.map +1 -1
  190. package/lib/rtp/src/extra/container/mp4/exp-golomb.js +2 -1
  191. package/lib/rtp/src/extra/container/mp4/exp-golomb.js.map +1 -1
  192. package/lib/rtp/src/extra/container/mp4/h264.d.ts +21 -17
  193. package/lib/rtp/src/extra/container/mp4/h264.js +26 -24
  194. package/lib/rtp/src/extra/container/mp4/h264.js.map +1 -1
  195. package/lib/rtp/src/extra/container/ogg/index.d.ts +1 -0
  196. package/lib/rtp/src/extra/container/ogg/index.js +1 -0
  197. package/lib/rtp/src/extra/container/ogg/index.js.map +1 -1
  198. package/lib/rtp/src/extra/container/ogg/writer.d.ts +90 -0
  199. package/lib/rtp/src/extra/container/ogg/writer.js +300 -0
  200. package/lib/rtp/src/extra/container/ogg/writer.js.map +1 -0
  201. package/lib/rtp/src/extra/container/webm/ebml/ebml.js +6 -3
  202. package/lib/rtp/src/extra/container/webm/ebml/ebml.js.map +1 -1
  203. package/lib/rtp/src/extra/processor/depacketizer.d.ts +5 -0
  204. package/lib/rtp/src/extra/processor/depacketizer.js +28 -2
  205. package/lib/rtp/src/extra/processor/depacketizer.js.map +1 -1
  206. package/lib/rtp/src/extra/processor/dtx.js +4 -2
  207. package/lib/rtp/src/extra/processor/dtx.js.map +1 -1
  208. package/lib/rtp/src/extra/processor/index.d.ts +2 -0
  209. package/lib/rtp/src/extra/processor/index.js +2 -0
  210. package/lib/rtp/src/extra/processor/index.js.map +1 -1
  211. package/lib/rtp/src/extra/processor/jitterBuffer.d.ts +29 -6
  212. package/lib/rtp/src/extra/processor/jitterBuffer.js +101 -111
  213. package/lib/rtp/src/extra/processor/jitterBuffer.js.map +1 -1
  214. package/lib/rtp/src/extra/processor/jitterBufferTransformer.js +2 -1
  215. package/lib/rtp/src/extra/processor/jitterBufferTransformer.js.map +1 -1
  216. package/lib/rtp/src/extra/processor/lipsync.d.ts +63 -0
  217. package/lib/rtp/src/extra/processor/lipsync.js +202 -51
  218. package/lib/rtp/src/extra/processor/lipsync.js.map +1 -1
  219. package/lib/rtp/src/extra/processor/mp4.d.ts +0 -6
  220. package/lib/rtp/src/extra/processor/mp4.js +6 -3
  221. package/lib/rtp/src/extra/processor/mp4.js.map +1 -1
  222. package/lib/rtp/src/extra/processor/mute.js +4 -2
  223. package/lib/rtp/src/extra/processor/mute.js.map +1 -1
  224. package/lib/rtp/src/extra/processor/nack.js +4 -2
  225. package/lib/rtp/src/extra/processor/nack.js.map +1 -1
  226. package/lib/rtp/src/extra/processor/ntpTime.js +4 -6
  227. package/lib/rtp/src/extra/processor/ntpTime.js.map +1 -1
  228. package/lib/rtp/src/extra/processor/ogg.d.ts +72 -0
  229. package/lib/rtp/src/extra/processor/ogg.js +221 -0
  230. package/lib/rtp/src/extra/processor/ogg.js.map +1 -0
  231. package/lib/rtp/src/extra/processor/oggCallback.d.ts +21 -0
  232. package/lib/rtp/src/extra/processor/oggCallback.js +97 -0
  233. package/lib/rtp/src/extra/processor/oggCallback.js.map +1 -0
  234. package/lib/rtp/src/extra/processor/rtpCallback.js +2 -1
  235. package/lib/rtp/src/extra/processor/rtpCallback.js.map +1 -1
  236. package/lib/rtp/src/extra/processor/rtpStream.js +2 -1
  237. package/lib/rtp/src/extra/processor/rtpStream.js.map +1 -1
  238. package/lib/rtp/src/extra/processor/rtpTime.js +2 -1
  239. package/lib/rtp/src/extra/processor/rtpTime.js.map +1 -1
  240. package/lib/rtp/src/extra/processor/webm.js +14 -7
  241. package/lib/rtp/src/extra/processor/webm.js.map +1 -1
  242. package/lib/rtp/src/extra/processor/webmCallback.js.map +1 -1
  243. package/lib/rtp/src/index.d.ts +8 -0
  244. package/lib/rtp/src/index.js +8 -0
  245. package/lib/rtp/src/index.js.map +1 -1
  246. package/lib/rtp/src/rtcp/psfb/fullIntraRequest.d.ts +4 -0
  247. package/lib/rtp/src/rtcp/psfb/fullIntraRequest.js +4 -0
  248. package/lib/rtp/src/rtcp/psfb/fullIntraRequest.js.map +1 -1
  249. package/lib/rtp/src/rtcp/rtcp.js +8 -4
  250. package/lib/rtp/src/rtcp/rtcp.js.map +1 -1
  251. package/lib/rtp/src/rtcp/rtpfb/transportWideSequence.d.ts +58 -0
  252. package/lib/rtp/src/rtcp/rtpfb/transportWideSequence.js +143 -0
  253. package/lib/rtp/src/rtcp/rtpfb/transportWideSequence.js.map +1 -0
  254. package/lib/rtp/src/rtcp/rtpfb/twcc.d.ts +35 -13
  255. package/lib/rtp/src/rtcp/rtpfb/twcc.js +132 -54
  256. package/lib/rtp/src/rtcp/rtpfb/twcc.js.map +1 -1
  257. package/lib/rtp/src/rtcp/rtpfb/twccReceiveTiming.d.ts +13 -0
  258. package/lib/rtp/src/rtcp/rtpfb/twccReceiveTiming.js +29 -0
  259. package/lib/rtp/src/rtcp/rtpfb/twccReceiveTiming.js.map +1 -0
  260. package/lib/rtp/src/rtcp/rtpfb/twccReferenceTime.d.ts +41 -0
  261. package/lib/rtp/src/rtcp/rtpfb/twccReferenceTime.js +89 -0
  262. package/lib/rtp/src/rtcp/rtpfb/twccReferenceTime.js.map +1 -0
  263. package/lib/rtp/src/rtcp/sr.js +1 -0
  264. package/lib/rtp/src/rtcp/sr.js.map +1 -1
  265. package/lib/rtp/src/rtp/continuity.d.ts +49 -0
  266. package/lib/rtp/src/rtp/continuity.js +120 -0
  267. package/lib/rtp/src/rtp/continuity.js.map +1 -0
  268. package/lib/rtp/src/rtp/headerExtension.js +41 -21
  269. package/lib/rtp/src/rtp/headerExtension.js.map +1 -1
  270. package/lib/rtp/src/rtp/padding.d.ts +17 -0
  271. package/lib/rtp/src/rtp/padding.js +37 -0
  272. package/lib/rtp/src/rtp/padding.js.map +1 -0
  273. package/lib/rtp/src/rtp/red/encoder.d.ts +10 -0
  274. package/lib/rtp/src/rtp/red/encoder.js +14 -1
  275. package/lib/rtp/src/rtp/red/encoder.js.map +1 -1
  276. package/lib/rtp/src/rtp/red/packet.js +24 -8
  277. package/lib/rtp/src/rtp/red/packet.js.map +1 -1
  278. package/lib/rtp/src/rtp/rtp.d.ts +24 -1
  279. package/lib/rtp/src/rtp/rtp.js +70 -29
  280. package/lib/rtp/src/rtp/rtp.js.map +1 -1
  281. package/lib/rtp/src/rtp/rtx.js +3 -0
  282. package/lib/rtp/src/rtp/rtx.js.map +1 -1
  283. package/lib/rtp/src/rtp/sendHeaderExtensions.d.ts +36 -0
  284. package/lib/rtp/src/rtp/sendHeaderExtensions.js +85 -0
  285. package/lib/rtp/src/rtp/sendHeaderExtensions.js.map +1 -0
  286. package/lib/rtp/src/rtp/timelineRewriter.d.ts +178 -0
  287. package/lib/rtp/src/rtp/timelineRewriter.js +417 -0
  288. package/lib/rtp/src/rtp/timelineRewriter.js.map +1 -0
  289. package/lib/rtp/src/srtp/cipher/ctr.js +4 -2
  290. package/lib/rtp/src/srtp/cipher/ctr.js.map +1 -1
  291. package/lib/rtp/src/srtp/cipher/index.js +8 -4
  292. package/lib/rtp/src/srtp/cipher/index.js.map +1 -1
  293. package/lib/rtp/src/srtp/context/context.js +6 -3
  294. package/lib/rtp/src/srtp/context/context.js.map +1 -1
  295. package/lib/rtp/src/srtp/context/srtcp.d.ts +1 -1
  296. package/lib/rtp/src/srtp/context/srtcp.js +33 -0
  297. package/lib/rtp/src/srtp/context/srtcp.js.map +1 -1
  298. package/lib/rtp/src/srtp/context/srtp.d.ts +1 -1
  299. package/lib/rtp/src/srtp/context/srtp.js +34 -10
  300. package/lib/rtp/src/srtp/context/srtp.js.map +1 -1
  301. package/lib/rtp/src/srtp/error.d.ts +8 -0
  302. package/lib/rtp/src/srtp/error.js +13 -1
  303. package/lib/rtp/src/srtp/error.js.map +1 -1
  304. package/lib/rtp/src/srtp/packet.js +4 -1
  305. package/lib/rtp/src/srtp/packet.js.map +1 -1
  306. package/lib/rtp/src/srtp/replay.d.ts +24 -0
  307. package/lib/rtp/src/srtp/replay.js +75 -0
  308. package/lib/rtp/src/srtp/replay.js.map +1 -0
  309. package/lib/rtp/src/srtp/session.js +2 -1
  310. package/lib/rtp/src/srtp/session.js.map +1 -1
  311. package/lib/rtp/src/srtp/srtcp.js +2 -1
  312. package/lib/rtp/src/srtp/srtcp.js.map +1 -1
  313. package/lib/rtp/src/srtp/srtp.js +2 -1
  314. package/lib/rtp/src/srtp/srtp.js.map +1 -1
  315. package/lib/rtp/src/util.js +2 -1
  316. package/lib/rtp/src/util.js.map +1 -1
  317. package/lib/sctp/src/chunk.d.ts +5 -0
  318. package/lib/sctp/src/chunk.js +31 -8
  319. package/lib/sctp/src/chunk.js.map +1 -1
  320. package/lib/sctp/src/const.d.ts +25 -17
  321. package/lib/sctp/src/const.js +23 -19
  322. package/lib/sctp/src/const.js.map +1 -1
  323. package/lib/sctp/src/param.d.ts +6 -2
  324. package/lib/sctp/src/param.js +22 -8
  325. package/lib/sctp/src/param.js.map +1 -1
  326. package/lib/sctp/src/sctp.d.ts +85 -0
  327. package/lib/sctp/src/sctp.js +707 -126
  328. package/lib/sctp/src/sctp.js.map +1 -1
  329. package/lib/sctp/src/transport.js +4 -2
  330. package/lib/sctp/src/transport.js.map +1 -1
  331. package/lib/webrtc/src/api/compatibilityOptions.d.ts +33 -0
  332. package/lib/webrtc/src/api/compatibilityOptions.js +33 -0
  333. package/lib/webrtc/src/api/compatibilityOptions.js.map +1 -0
  334. package/lib/webrtc/src/api/peerConfig.d.ts +385 -28
  335. package/lib/webrtc/src/api/peerConfig.js +121 -14
  336. package/lib/webrtc/src/api/peerConfig.js.map +1 -1
  337. package/lib/webrtc/src/api/peerConnectionEvents.d.ts +77 -1
  338. package/lib/webrtc/src/api/peerConnectionEvents.js +105 -1
  339. package/lib/webrtc/src/api/peerConnectionEvents.js.map +1 -1
  340. package/lib/webrtc/src/dataChannel.d.ts +10 -0
  341. package/lib/webrtc/src/dataChannel.js +24 -4
  342. package/lib/webrtc/src/dataChannel.js.map +1 -1
  343. package/lib/webrtc/src/helper.d.ts +8 -0
  344. package/lib/webrtc/src/helper.js +40 -4
  345. package/lib/webrtc/src/helper.js.map +1 -1
  346. package/lib/webrtc/src/media/codecCompatibility.d.ts +87 -1
  347. package/lib/webrtc/src/media/codecCompatibility.js +338 -12
  348. package/lib/webrtc/src/media/codecCompatibility.js.map +1 -1
  349. package/lib/webrtc/src/media/index.d.ts +10 -0
  350. package/lib/webrtc/src/media/index.js +70 -0
  351. package/lib/webrtc/src/media/index.js.map +1 -1
  352. package/lib/webrtc/src/media/receiver/nack.d.ts +43 -10
  353. package/lib/webrtc/src/media/receiver/nack.js +246 -88
  354. package/lib/webrtc/src/media/receiver/nack.js.map +1 -1
  355. package/lib/webrtc/src/media/receiver/receiverTwcc.d.ts +61 -1
  356. package/lib/webrtc/src/media/receiver/receiverTwcc.js +218 -63
  357. package/lib/webrtc/src/media/receiver/receiverTwcc.js.map +1 -1
  358. package/lib/webrtc/src/media/router.d.ts +241 -40
  359. package/lib/webrtc/src/media/router.js +676 -205
  360. package/lib/webrtc/src/media/router.js.map +1 -1
  361. package/lib/webrtc/src/media/routing/pendingRoutes.d.ts +26 -0
  362. package/lib/webrtc/src/media/routing/pendingRoutes.js +48 -0
  363. package/lib/webrtc/src/media/routing/pendingRoutes.js.map +1 -0
  364. package/lib/webrtc/src/media/routing/ridRouting.d.ts +37 -0
  365. package/lib/webrtc/src/media/routing/ridRouting.js +74 -0
  366. package/lib/webrtc/src/media/routing/ridRouting.js.map +1 -0
  367. package/lib/webrtc/src/media/routing/routeTable.d.ts +60 -0
  368. package/lib/webrtc/src/media/routing/routeTable.js +95 -0
  369. package/lib/webrtc/src/media/routing/routeTable.js.map +1 -0
  370. package/lib/webrtc/src/media/routing/rtcpRouting.d.ts +34 -0
  371. package/lib/webrtc/src/media/routing/rtcpRouting.js +165 -0
  372. package/lib/webrtc/src/media/routing/rtcpRouting.js.map +1 -0
  373. package/lib/webrtc/src/media/routing/unknownSsrc.d.ts +55 -0
  374. package/lib/webrtc/src/media/routing/unknownSsrc.js +117 -0
  375. package/lib/webrtc/src/media/routing/unknownSsrc.js.map +1 -0
  376. package/lib/webrtc/src/media/rtpReceiver.d.ts +252 -2
  377. package/lib/webrtc/src/media/rtpReceiver.js +833 -44
  378. package/lib/webrtc/src/media/rtpReceiver.js.map +1 -1
  379. package/lib/webrtc/src/media/rtpSender.d.ts +191 -70
  380. package/lib/webrtc/src/media/rtpSender.js +631 -448
  381. package/lib/webrtc/src/media/rtpSender.js.map +1 -1
  382. package/lib/webrtc/src/media/rtpTransceiver.d.ts +19 -3
  383. package/lib/webrtc/src/media/rtpTransceiver.js +74 -9
  384. package/lib/webrtc/src/media/rtpTransceiver.js.map +1 -1
  385. package/lib/webrtc/src/media/sender/bandwidthEstimator.d.ts +249 -0
  386. package/lib/webrtc/src/media/sender/bandwidthEstimator.js +144 -0
  387. package/lib/webrtc/src/media/sender/bandwidthEstimator.js.map +1 -0
  388. package/lib/webrtc/src/media/sender/createBandwidthEstimator.d.ts +20 -0
  389. package/lib/webrtc/src/media/sender/createBandwidthEstimator.js +26 -0
  390. package/lib/webrtc/src/media/sender/createBandwidthEstimator.js.map +1 -0
  391. package/lib/webrtc/src/media/sender/estimatorBinding.d.ts +70 -0
  392. package/lib/webrtc/src/media/sender/estimatorBinding.js +175 -0
  393. package/lib/webrtc/src/media/sender/estimatorBinding.js.map +1 -0
  394. package/lib/webrtc/src/media/sender/estimators/disabledBwe.d.ts +18 -0
  395. package/lib/webrtc/src/media/sender/estimators/disabledBwe.js +44 -0
  396. package/lib/webrtc/src/media/sender/estimators/disabledBwe.js.map +1 -0
  397. package/lib/webrtc/src/media/sender/estimators/gcc/acknowledgedBitrateEstimator.d.ts +79 -0
  398. package/lib/webrtc/src/media/sender/estimators/gcc/acknowledgedBitrateEstimator.js +289 -0
  399. package/lib/webrtc/src/media/sender/estimators/gcc/acknowledgedBitrateEstimator.js.map +1 -0
  400. package/lib/webrtc/src/media/sender/estimators/gcc/aimdRateControl.d.ts +87 -0
  401. package/lib/webrtc/src/media/sender/estimators/gcc/aimdRateControl.js +429 -0
  402. package/lib/webrtc/src/media/sender/estimators/gcc/aimdRateControl.js.map +1 -0
  403. package/lib/webrtc/src/media/sender/estimators/gcc/alrDetector.d.ts +21 -0
  404. package/lib/webrtc/src/media/sender/estimators/gcc/alrDetector.js +84 -0
  405. package/lib/webrtc/src/media/sender/estimators/gcc/alrDetector.js.map +1 -0
  406. package/lib/webrtc/src/media/sender/estimators/gcc/bandwidthLimitedCause.d.ts +37 -0
  407. package/lib/webrtc/src/media/sender/estimators/gcc/bandwidthLimitedCause.js +74 -0
  408. package/lib/webrtc/src/media/sender/estimators/gcc/bandwidthLimitedCause.js.map +1 -0
  409. package/lib/webrtc/src/media/sender/estimators/gcc/constants.d.ts +383 -0
  410. package/lib/webrtc/src/media/sender/estimators/gcc/constants.js +408 -0
  411. package/lib/webrtc/src/media/sender/estimators/gcc/constants.js.map +1 -0
  412. package/lib/webrtc/src/media/sender/estimators/gcc/gccBwe.d.ts +406 -0
  413. package/lib/webrtc/src/media/sender/estimators/gcc/gccBwe.js +1391 -0
  414. package/lib/webrtc/src/media/sender/estimators/gcc/gccBwe.js.map +1 -0
  415. package/lib/webrtc/src/media/sender/estimators/gcc/index.d.ts +19 -0
  416. package/lib/webrtc/src/media/sender/estimators/gcc/index.js +75 -0
  417. package/lib/webrtc/src/media/sender/estimators/gcc/index.js.map +1 -0
  418. package/lib/webrtc/src/media/sender/estimators/gcc/interArrivalDelta.d.ts +68 -0
  419. package/lib/webrtc/src/media/sender/estimators/gcc/interArrivalDelta.js +201 -0
  420. package/lib/webrtc/src/media/sender/estimators/gcc/interArrivalDelta.js.map +1 -0
  421. package/lib/webrtc/src/media/sender/estimators/gcc/intervalBudget.d.ts +26 -0
  422. package/lib/webrtc/src/media/sender/estimators/gcc/intervalBudget.js +72 -0
  423. package/lib/webrtc/src/media/sender/estimators/gcc/intervalBudget.js.map +1 -0
  424. package/lib/webrtc/src/media/sender/estimators/gcc/linkCapacityEstimator.d.ts +41 -0
  425. package/lib/webrtc/src/media/sender/estimators/gcc/linkCapacityEstimator.js +99 -0
  426. package/lib/webrtc/src/media/sender/estimators/gcc/linkCapacityEstimator.js.map +1 -0
  427. package/lib/webrtc/src/media/sender/estimators/gcc/lossBasedBwe.d.ts +247 -0
  428. package/lib/webrtc/src/media/sender/estimators/gcc/lossBasedBwe.js +1042 -0
  429. package/lib/webrtc/src/media/sender/estimators/gcc/lossBasedBwe.js.map +1 -0
  430. package/lib/webrtc/src/media/sender/estimators/gcc/overuseDetector.d.ts +26 -0
  431. package/lib/webrtc/src/media/sender/estimators/gcc/overuseDetector.js +132 -0
  432. package/lib/webrtc/src/media/sender/estimators/gcc/overuseDetector.js.map +1 -0
  433. package/lib/webrtc/src/media/sender/estimators/gcc/probeController.d.ts +305 -0
  434. package/lib/webrtc/src/media/sender/estimators/gcc/probeController.js +1073 -0
  435. package/lib/webrtc/src/media/sender/estimators/gcc/probeController.js.map +1 -0
  436. package/lib/webrtc/src/media/sender/estimators/gcc/rttBasedBackoff.d.ts +60 -0
  437. package/lib/webrtc/src/media/sender/estimators/gcc/rttBasedBackoff.js +151 -0
  438. package/lib/webrtc/src/media/sender/estimators/gcc/rttBasedBackoff.js.map +1 -0
  439. package/lib/webrtc/src/media/sender/estimators/gcc/trendlineEstimator.d.ts +44 -0
  440. package/lib/webrtc/src/media/sender/estimators/gcc/trendlineEstimator.js +246 -0
  441. package/lib/webrtc/src/media/sender/estimators/gcc/trendlineEstimator.js.map +1 -0
  442. package/lib/webrtc/src/media/sender/estimators/index.d.ts +7 -0
  443. package/lib/webrtc/src/media/sender/estimators/index.js +19 -0
  444. package/lib/webrtc/src/media/sender/estimators/index.js.map +1 -0
  445. package/lib/webrtc/src/media/sender/estimators/lightweightBwe.d.ts +64 -0
  446. package/lib/webrtc/src/media/sender/estimators/lightweightBwe.js +216 -0
  447. package/lib/webrtc/src/media/sender/estimators/lightweightBwe.js.map +1 -0
  448. package/lib/webrtc/src/media/sender/estimators/twccFeedbackAdapter.d.ts +117 -0
  449. package/lib/webrtc/src/media/sender/estimators/twccFeedbackAdapter.js +250 -0
  450. package/lib/webrtc/src/media/sender/estimators/twccFeedbackAdapter.js.map +1 -0
  451. package/lib/webrtc/src/media/sender/outgoingQueue.d.ts +40 -0
  452. package/lib/webrtc/src/media/sender/outgoingQueue.js +153 -0
  453. package/lib/webrtc/src/media/sender/outgoingQueue.js.map +1 -0
  454. package/lib/webrtc/src/media/sender/padding.d.ts +45 -0
  455. package/lib/webrtc/src/media/sender/padding.js +134 -0
  456. package/lib/webrtc/src/media/sender/padding.js.map +1 -0
  457. package/lib/webrtc/src/media/sender/pendingRtp.d.ts +35 -0
  458. package/lib/webrtc/src/media/sender/pendingRtp.js +120 -0
  459. package/lib/webrtc/src/media/sender/pendingRtp.js.map +1 -0
  460. package/lib/webrtc/src/media/sender/rtpWire.d.ts +13 -0
  461. package/lib/webrtc/src/media/sender/rtpWire.js +38 -0
  462. package/lib/webrtc/src/media/sender/rtpWire.js.map +1 -0
  463. package/lib/webrtc/src/media/sender/sendPacer.d.ts +27 -0
  464. package/lib/webrtc/src/media/sender/sendPacer.js +110 -0
  465. package/lib/webrtc/src/media/sender/sendPacer.js.map +1 -0
  466. package/lib/webrtc/src/media/sender/senderBWE.d.ts +8 -33
  467. package/lib/webrtc/src/media/sender/senderBWE.js +5 -154
  468. package/lib/webrtc/src/media/sender/senderBWE.js.map +1 -1
  469. package/lib/webrtc/src/media/sender/senderReportHistory.d.ts +51 -0
  470. package/lib/webrtc/src/media/sender/senderReportHistory.js +111 -0
  471. package/lib/webrtc/src/media/sender/senderReportHistory.js.map +1 -0
  472. package/lib/webrtc/src/media/sender/senderStats.d.ts +101 -0
  473. package/lib/webrtc/src/media/sender/senderStats.js +317 -0
  474. package/lib/webrtc/src/media/sender/senderStats.js.map +1 -0
  475. package/lib/webrtc/src/media/track.d.ts +29 -2
  476. package/lib/webrtc/src/media/track.js +32 -5
  477. package/lib/webrtc/src/media/track.js.map +1 -1
  478. package/lib/webrtc/src/negotiation/bundleTopology.d.ts +1 -5
  479. package/lib/webrtc/src/negotiation/bundleTopology.js +15 -11
  480. package/lib/webrtc/src/negotiation/bundleTopology.js.map +1 -1
  481. package/lib/webrtc/src/negotiation/descriptionValidation.js +12 -6
  482. package/lib/webrtc/src/negotiation/descriptionValidation.js.map +1 -1
  483. package/lib/webrtc/src/negotiation/negotiationNeeded.js +2 -1
  484. package/lib/webrtc/src/negotiation/negotiationNeeded.js.map +1 -1
  485. package/lib/webrtc/src/negotiation/remoteCandidates.js +10 -5
  486. package/lib/webrtc/src/negotiation/remoteCandidates.js.map +1 -1
  487. package/lib/webrtc/src/negotiation/remoteMediaApplication.d.ts +24 -7
  488. package/lib/webrtc/src/negotiation/remoteMediaApplication.js +67 -27
  489. package/lib/webrtc/src/negotiation/remoteMediaApplication.js.map +1 -1
  490. package/lib/webrtc/src/negotiation/transceiverPairing.d.ts +1 -0
  491. package/lib/webrtc/src/negotiation/transceiverPairing.js +62 -0
  492. package/lib/webrtc/src/negotiation/transceiverPairing.js.map +1 -0
  493. package/lib/webrtc/src/negotiation/transportActivation.d.ts +8 -1
  494. package/lib/webrtc/src/negotiation/transportActivation.js +40 -8
  495. package/lib/webrtc/src/negotiation/transportActivation.js.map +1 -1
  496. package/lib/webrtc/src/negotiationTransaction.d.ts +43 -5
  497. package/lib/webrtc/src/negotiationTransaction.js +129 -13
  498. package/lib/webrtc/src/negotiationTransaction.js.map +1 -1
  499. package/lib/webrtc/src/nonstandard/dummyMedia.js +2 -1
  500. package/lib/webrtc/src/nonstandard/dummyMedia.js.map +1 -1
  501. package/lib/webrtc/src/nonstandard/navigator.js +4 -3
  502. package/lib/webrtc/src/nonstandard/navigator.js.map +1 -1
  503. package/lib/webrtc/src/nonstandard/recorder/index.d.ts +32 -8
  504. package/lib/webrtc/src/nonstandard/recorder/index.js +81 -20
  505. package/lib/webrtc/src/nonstandard/recorder/index.js.map +1 -1
  506. package/lib/webrtc/src/nonstandard/recorder/writer/index.d.ts +9 -7
  507. package/lib/webrtc/src/nonstandard/recorder/writer/index.js +2 -1
  508. package/lib/webrtc/src/nonstandard/recorder/writer/index.js.map +1 -1
  509. package/lib/webrtc/src/nonstandard/recorder/writer/ogg.d.ts +32 -0
  510. package/lib/webrtc/src/nonstandard/recorder/writer/ogg.js +185 -0
  511. package/lib/webrtc/src/nonstandard/recorder/writer/ogg.js.map +1 -0
  512. package/lib/webrtc/src/nonstandard/recorder/writer/webm.js +8 -1
  513. package/lib/webrtc/src/nonstandard/recorder/writer/webm.js.map +1 -1
  514. package/lib/webrtc/src/nonstandard/userMedia.js +2 -1
  515. package/lib/webrtc/src/nonstandard/userMedia.js.map +1 -1
  516. package/lib/webrtc/src/peerConnection.d.ts +108 -6
  517. package/lib/webrtc/src/peerConnection.js +357 -36
  518. package/lib/webrtc/src/peerConnection.js.map +1 -1
  519. package/lib/webrtc/src/polyfill/globals.d.ts +2 -1
  520. package/lib/webrtc/src/polyfill/globals.js.map +1 -1
  521. package/lib/webrtc/src/polyfill/install.js +2 -0
  522. package/lib/webrtc/src/polyfill/install.js.map +1 -1
  523. package/lib/webrtc/src/polyfill/mediaDevices.js +2 -1
  524. package/lib/webrtc/src/polyfill/mediaDevices.js.map +1 -1
  525. package/lib/webrtc/src/sctpManager.d.ts +46 -14
  526. package/lib/webrtc/src/sctpManager.js +92 -25
  527. package/lib/webrtc/src/sctpManager.js.map +1 -1
  528. package/lib/webrtc/src/sdp.d.ts +69 -0
  529. package/lib/webrtc/src/sdp.js +151 -20
  530. package/lib/webrtc/src/sdp.js.map +1 -1
  531. package/lib/webrtc/src/sdpManager.d.ts +85 -4
  532. package/lib/webrtc/src/sdpManager.js +214 -14
  533. package/lib/webrtc/src/sdpManager.js.map +1 -1
  534. package/lib/webrtc/src/secureTransportManager.d.ts +89 -4
  535. package/lib/webrtc/src/secureTransportManager.js +184 -26
  536. package/lib/webrtc/src/secureTransportManager.js.map +1 -1
  537. package/lib/webrtc/src/transceiverManager.d.ts +82 -4
  538. package/lib/webrtc/src/transceiverManager.js +239 -41
  539. package/lib/webrtc/src/transceiverManager.js.map +1 -1
  540. package/lib/webrtc/src/transport/dtls.d.ts +147 -1
  541. package/lib/webrtc/src/transport/dtls.js +446 -50
  542. package/lib/webrtc/src/transport/dtls.js.map +1 -1
  543. package/lib/webrtc/src/transport/ice.d.ts +71 -90
  544. package/lib/webrtc/src/transport/ice.js +250 -42
  545. package/lib/webrtc/src/transport/ice.js.map +1 -1
  546. package/lib/webrtc/src/transport/sctp.d.ts +25 -0
  547. package/lib/webrtc/src/transport/sctp.js +153 -22
  548. package/lib/webrtc/src/transport/sctp.js.map +1 -1
  549. package/lib/webrtc/src/utils.d.ts +69 -3
  550. package/lib/webrtc/src/utils.js +105 -16
  551. package/lib/webrtc/src/utils.js.map +1 -1
  552. package/package.json +9 -6
@@ -30,40 +30,58 @@ const common_1 = require("../imports/common");
30
30
  const rtp_1 = require("../imports/rtp");
31
31
  const utils_1 = require("../utils");
32
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");
33
39
  const senderBWE_1 = require("./sender/senderBWE");
40
+ const senderStats_1 = require("./sender/senderStats");
41
+ const sendPacer_1 = require("./sender/sendPacer");
34
42
  const stats_1 = require("./stats");
35
43
  const track_1 = require("./track");
36
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. */
37
46
  const RTP_HISTORY_SIZE = 128;
38
- const DEFAULT_PENDING_RTP_MAX_LENGTH = 256;
39
- const RTT_ALPHA = 0.85;
40
- function resolvePendingRtpOptions(pendingRtp) {
41
- if (pendingRtp === true) {
42
- return { enabled: true, maxLength: DEFAULT_PENDING_RTP_MAX_LENGTH };
43
- }
44
- if (pendingRtp == undefined || pendingRtp === false) {
45
- return { enabled: false, maxLength: DEFAULT_PENDING_RTP_MAX_LENGTH };
46
- }
47
- const maxLength = typeof pendingRtp.maxLength === "number" &&
48
- Number.isFinite(pendingRtp.maxLength) &&
49
- pendingRtp.maxLength >= 1
50
- ? Math.floor(pendingRtp.maxLength)
51
- : DEFAULT_PENDING_RTP_MAX_LENGTH;
52
- return { enabled: pendingRtp.enabled ?? true, maxLength };
53
- }
54
- function freezeRtpContinuityOffsets(lastOutputSeq, lastOutputTimestamp, firstInputSeq, firstInputTimestamp, timestampStep) {
55
- return {
56
- seqOffset: (0, common_1.uint16Add)((0, common_1.uint16Add)(lastOutputSeq, 1), -firstInputSeq),
57
- timestampOffset: (0, common_1.uint32Add)((0, common_1.uint32Add)(lastOutputTimestamp, timestampStep), -firstInputTimestamp),
58
- };
59
- }
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.
60
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
+ }
61
79
  constructor(trackOrKind, options = {}) {
62
80
  Object.defineProperty(this, "trackOrKind", {
63
81
  enumerable: true,
64
82
  configurable: true,
65
83
  writable: true,
66
- value: trackOrKind
84
+ value: void 0
67
85
  });
68
86
  Object.defineProperty(this, "type", {
69
87
  enumerable: true,
@@ -119,191 +137,195 @@ class RTCRtpSender {
119
137
  writable: true,
120
138
  value: new common_1.Event()
121
139
  });
122
- /** Fires for a FIR addressed to this sender with a new sequence number (RFC 5104 4.3.1). */
123
- Object.defineProperty(this, "onFullIntraRequest", {
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", {
124
145
  enumerable: true,
125
146
  configurable: true,
126
147
  writable: true,
127
- value: new common_1.Event()
148
+ value: void 0
128
149
  });
129
150
  /**
130
- * Fires for every key frame request addressed to this sender: each PLI, and each FIR
131
- * that is not a retransmission. Fires in addition to onPictureLossIndication / onFullIntraRequest.
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.
132
156
  */
133
- Object.defineProperty(this, "onKeyFrameRequest", {
157
+ Object.defineProperty(this, "onAvailableBitrate", {
134
158
  enumerable: true,
135
159
  configurable: true,
136
160
  writable: true,
137
161
  value: new common_1.Event()
138
162
  });
139
- Object.defineProperty(this, "senderBWE", {
140
- enumerable: true,
141
- configurable: true,
142
- writable: true,
143
- value: new senderBWE_1.SenderBandwidthEstimator()
144
- });
145
- Object.defineProperty(this, "cname", {
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", {
146
168
  enumerable: true,
147
169
  configurable: true,
148
170
  writable: true,
149
- value: void 0
171
+ value: new common_1.Event()
150
172
  });
151
- Object.defineProperty(this, "mid", {
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", {
152
183
  enumerable: true,
153
184
  configurable: true,
154
185
  writable: true,
155
- value: void 0
186
+ value: true
156
187
  });
157
- Object.defineProperty(this, "rtpStreamId", {
188
+ /** Token-bucket pacer for **media** (not probe) rate enforcement. */
189
+ Object.defineProperty(this, "pacer", {
158
190
  enumerable: true,
159
191
  configurable: true,
160
192
  writable: true,
161
- value: void 0
193
+ value: new sendPacer_1.MediaPacer()
162
194
  });
163
- Object.defineProperty(this, "repairedRtpStreamId", {
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", {
164
200
  enumerable: true,
165
201
  configurable: true,
166
202
  writable: true,
167
- value: void 0
203
+ value: 0
168
204
  });
169
- Object.defineProperty(this, "rtxPayloadType", {
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", {
170
212
  enumerable: true,
171
213
  configurable: true,
172
214
  writable: true,
173
215
  value: void 0
174
216
  });
175
- Object.defineProperty(this, "rtxSequenceNumber", {
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", {
176
223
  enumerable: true,
177
224
  configurable: true,
178
225
  writable: true,
179
- value: (0, common_1.random16)()
226
+ value: false
180
227
  });
181
- Object.defineProperty(this, "redRedundantPayloadType", {
228
+ Object.defineProperty(this, "padding", {
182
229
  enumerable: true,
183
230
  configurable: true,
184
231
  writable: true,
185
232
  value: void 0
186
233
  });
187
- Object.defineProperty(this, "_redDistance", {
188
- enumerable: true,
189
- configurable: true,
190
- writable: true,
191
- value: 2
192
- });
193
- Object.defineProperty(this, "redEncoder", {
234
+ /** Fires for a FIR addressed to this sender with a new sequence number (RFC 5104 4.3.1). */
235
+ Object.defineProperty(this, "onFullIntraRequest", {
194
236
  enumerable: true,
195
237
  configurable: true,
196
238
  writable: true,
197
- value: new rtp_1.RedEncoder(this._redDistance)
239
+ value: new common_1.Event()
198
240
  });
199
- Object.defineProperty(this, "headerExtensions", {
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", {
200
246
  enumerable: true,
201
247
  configurable: true,
202
248
  writable: true,
203
- value: []
249
+ value: new common_1.Event()
204
250
  });
205
- Object.defineProperty(this, "disposeTrack", {
251
+ Object.defineProperty(this, "cname", {
206
252
  enumerable: true,
207
253
  configurable: true,
208
254
  writable: true,
209
255
  value: void 0
210
256
  });
211
- Object.defineProperty(this, "sendEncodings", {
212
- enumerable: true,
213
- configurable: true,
214
- writable: true,
215
- value: [{}]
216
- });
217
- // # stats
218
- Object.defineProperty(this, "lastSRtimestamp", {
257
+ Object.defineProperty(this, "mid", {
219
258
  enumerable: true,
220
259
  configurable: true,
221
260
  writable: true,
222
261
  value: void 0
223
262
  });
224
- Object.defineProperty(this, "lastSentSRTimestamp", {
263
+ Object.defineProperty(this, "rtpStreamId", {
225
264
  enumerable: true,
226
265
  configurable: true,
227
266
  writable: true,
228
267
  value: void 0
229
268
  });
230
- Object.defineProperty(this, "ntpTimestamp", {
231
- enumerable: true,
232
- configurable: true,
233
- writable: true,
234
- value: 0n
235
- });
236
- Object.defineProperty(this, "rtpTimestamp", {
237
- enumerable: true,
238
- configurable: true,
239
- writable: true,
240
- value: 0
241
- });
242
- Object.defineProperty(this, "octetCount", {
269
+ Object.defineProperty(this, "repairedRtpStreamId", {
243
270
  enumerable: true,
244
271
  configurable: true,
245
272
  writable: true,
246
- value: 0
273
+ value: void 0
247
274
  });
248
- Object.defineProperty(this, "packetCount", {
275
+ Object.defineProperty(this, "rtxPayloadType", {
249
276
  enumerable: true,
250
277
  configurable: true,
251
278
  writable: true,
252
- value: 0
279
+ value: void 0
253
280
  });
254
- Object.defineProperty(this, "headerBytesSent", {
281
+ Object.defineProperty(this, "rtxSequenceNumber", {
255
282
  enumerable: true,
256
283
  configurable: true,
257
284
  writable: true,
258
- value: 0
285
+ value: (0, common_1.random16)()
259
286
  });
260
- Object.defineProperty(this, "rtt", {
287
+ Object.defineProperty(this, "redRedundantPayloadType", {
261
288
  enumerable: true,
262
289
  configurable: true,
263
290
  writable: true,
264
291
  value: void 0
265
292
  });
266
- Object.defineProperty(this, "totalRoundTripTime", {
267
- enumerable: true,
268
- configurable: true,
269
- writable: true,
270
- value: 0
271
- });
272
- Object.defineProperty(this, "roundTripTimeMeasurements", {
293
+ Object.defineProperty(this, "_redDistance", {
273
294
  enumerable: true,
274
295
  configurable: true,
275
296
  writable: true,
276
- value: 0
297
+ value: 2
277
298
  });
278
- Object.defineProperty(this, "retransmittedPacketsSent", {
299
+ Object.defineProperty(this, "redEncoder", {
279
300
  enumerable: true,
280
301
  configurable: true,
281
302
  writable: true,
282
- value: 0
303
+ value: new rtp_1.RedEncoder(this._redDistance)
283
304
  });
284
- Object.defineProperty(this, "retransmittedBytesSent", {
305
+ Object.defineProperty(this, "headerExtensions", {
285
306
  enumerable: true,
286
307
  configurable: true,
287
308
  writable: true,
288
- value: 0
309
+ value: []
289
310
  });
290
- Object.defineProperty(this, "nackCount", {
311
+ Object.defineProperty(this, "disposeTrack", {
291
312
  enumerable: true,
292
313
  configurable: true,
293
314
  writable: true,
294
- value: 0
315
+ value: void 0
295
316
  });
296
- Object.defineProperty(this, "pliCount", {
317
+ Object.defineProperty(this, "sendEncodings", {
297
318
  enumerable: true,
298
319
  configurable: true,
299
320
  writable: true,
300
- value: 0
321
+ value: [{}]
301
322
  });
302
- Object.defineProperty(this, "firCount", {
323
+ // # stats
324
+ Object.defineProperty(this, "stats", {
303
325
  enumerable: true,
304
326
  configurable: true,
305
327
  writable: true,
306
- value: 0
328
+ value: new senderStats_1.SenderStatistics()
307
329
  });
308
330
  /**
309
331
  * Last FIR sequence number per (FIR sender SSRC, FCI target SSRC): RFC 5104
@@ -315,18 +337,6 @@ class RTCRtpSender {
315
337
  writable: true,
316
338
  value: new Map()
317
339
  });
318
- Object.defineProperty(this, "remotePacketsLost", {
319
- enumerable: true,
320
- configurable: true,
321
- writable: true,
322
- value: void 0
323
- });
324
- Object.defineProperty(this, "remoteFractionLost", {
325
- enumerable: true,
326
- configurable: true,
327
- writable: true,
328
- value: void 0
329
- });
330
340
  Object.defineProperty(this, "receiverEstimatedMaxBitrate", {
331
341
  enumerable: true,
332
342
  configurable: true,
@@ -334,41 +344,11 @@ class RTCRtpSender {
334
344
  value: 0n
335
345
  });
336
346
  // rtp
337
- Object.defineProperty(this, "sequenceNumber", {
338
- enumerable: true,
339
- configurable: true,
340
- writable: true,
341
- value: void 0
342
- });
343
- Object.defineProperty(this, "timestamp", {
344
- enumerable: true,
345
- configurable: true,
346
- writable: true,
347
- value: void 0
348
- });
349
- Object.defineProperty(this, "timestampOffset", {
350
- enumerable: true,
351
- configurable: true,
352
- writable: true,
353
- value: 0
354
- });
355
- Object.defineProperty(this, "seqOffset", {
356
- enumerable: true,
357
- configurable: true,
358
- writable: true,
359
- value: 0
360
- });
361
- Object.defineProperty(this, "rtpContinuityPending", {
362
- enumerable: true,
363
- configurable: true,
364
- writable: true,
365
- value: false
366
- });
367
- Object.defineProperty(this, "pendingTimestampStep", {
347
+ Object.defineProperty(this, "continuity", {
368
348
  enumerable: true,
369
349
  configurable: true,
370
350
  writable: true,
371
- value: 1
351
+ value: new rtp_1.RtpContinuity()
372
352
  });
373
353
  Object.defineProperty(this, "rtpCache", {
374
354
  enumerable: true,
@@ -380,21 +360,28 @@ class RTCRtpSender {
380
360
  enumerable: true,
381
361
  configurable: true,
382
362
  writable: true,
383
- value: []
363
+ value: void 0
384
364
  });
385
- 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", {
386
372
  enumerable: true,
387
373
  configurable: true,
388
374
  writable: true,
389
- value: false
375
+ value: 0
390
376
  });
391
- Object.defineProperty(this, "pendingRtpEnabled", {
377
+ /** Aborted on every source epoch bump to wake probe / pacing waits. */
378
+ Object.defineProperty(this, "sourceEpochCancel", {
392
379
  enumerable: true,
393
380
  configurable: true,
394
381
  writable: true,
395
- value: void 0
382
+ value: new AbortController()
396
383
  });
397
- Object.defineProperty(this, "pendingRtpMaxLength", {
384
+ Object.defineProperty(this, "pendingRtpEnabled", {
398
385
  enumerable: true,
399
386
  configurable: true,
400
387
  writable: true,
@@ -481,16 +468,32 @@ class RTCRtpSender {
481
468
  writable: true,
482
469
  value: false
483
470
  });
484
- const pendingRtp = resolvePendingRtpOptions(options.pendingRtp);
471
+ this.trackOrKind = trackOrKind;
472
+ const pendingRtp = (0, pendingRtp_1.resolvePendingRtpOptions)(options.pendingRtp);
485
473
  this.pendingRtpEnabled = pendingRtp.enabled;
486
- this.pendingRtpMaxLength = pendingRtp.maxLength;
474
+ this.pendingRtp = new pendingRtp_1.PendingRtpQueue(pendingRtp.maxLength);
487
475
  this.kind =
488
476
  typeof this.trackOrKind === "string"
489
477
  ? this.trackOrKind
490
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();
491
493
  if (typeof trackOrKind !== "string") {
492
494
  this.registerTrack(trackOrKind);
493
495
  }
496
+ this.bwe.syncProcessTimer();
494
497
  }
495
498
  get transport() {
496
499
  return this.dtlsTransport ?? null;
@@ -502,7 +505,8 @@ class RTCRtpSender {
502
505
  this.streamIds = value ? [value] : [];
503
506
  }
504
507
  setDtlsTransport(dtlsTransport) {
505
- if (this.dtlsTransport) {
508
+ const previous = this.dtlsTransport;
509
+ if (previous) {
506
510
  this.dtlsDisposer.forEach((dispose) => dispose());
507
511
  }
508
512
  this.dtlsTransport = dtlsTransport;
@@ -512,14 +516,80 @@ class RTCRtpSender {
512
516
  this.dtlsDisposer = [];
513
517
  return;
514
518
  }
519
+ if (previous && previous !== dtlsTransport) {
520
+ this.resetBweForNewTransport();
521
+ }
515
522
  this.dtlsDisposer = [
516
523
  this.dtlsTransport.onStateChange.subscribe((state) => {
524
+ this.syncNetworkAvailability();
517
525
  if (state === "connected") {
518
526
  this.onReady.execute();
519
527
  void this.drainPendingRtp();
520
528
  }
521
529
  }).unSubscribe,
522
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;
523
593
  }
524
594
  get redDistance() {
525
595
  return this._redDistance;
@@ -577,6 +647,7 @@ class RTCRtpSender {
577
647
  this.cname = params.rtcp?.cname;
578
648
  this.mid = params.muxId;
579
649
  this.headerExtensions = params.headerExtensions;
650
+ // Keep a RID set via sendEncodings/setParameters when SDP carries none.
580
651
  this.rtpStreamId = params.rtpStreamId ?? this.rtpStreamId;
581
652
  this.repairedRtpStreamId = params.repairedRtpStreamId;
582
653
  this.negotiatedCodecs = [...params.codecs];
@@ -608,8 +679,12 @@ class RTCRtpSender {
608
679
  if (this.sendSuppressed === suppressed)
609
680
  return;
610
681
  this.sendSuppressed = suppressed;
611
- if (suppressed)
612
- this.discardPendingRtp();
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();
687
+ }
613
688
  else
614
689
  void this.drainPendingRtp();
615
690
  }
@@ -619,66 +694,12 @@ class RTCRtpSender {
619
694
  this.dtlsTransport?.state === "connected" &&
620
695
  !!this.codec);
621
696
  }
622
- settlePendingRtp(item, error) {
623
- if (error == undefined) {
624
- item.resolve();
625
- return;
626
- }
627
- item.reject(error);
628
- }
629
- discardPendingRtp() {
630
- const dropped = this.pendingRtp.splice(0);
631
- for (const item of dropped) {
632
- this.settlePendingRtp(item);
633
- }
634
- }
635
- enqueuePendingRtp(rtp, resolve, reject) {
636
- const packet = Buffer.isBuffer(rtp)
637
- ? rtp_1.RtpPacket.deSerialize(rtp)
638
- : rtp.clone();
639
- this.pendingRtp.push({ packet, resolve, reject });
640
- while (this.pendingRtp.length > this.pendingRtpMaxLength) {
641
- const dropped = this.pendingRtp.shift();
642
- if (dropped) {
643
- this.settlePendingRtp(dropped);
644
- }
645
- }
646
- }
647
697
  /**
648
- * Send queued RTP once DTLS is connected and a codec is set. Used only when
649
- * pending RTP is enabled: `writeRtp` / `sendRtp` may run before ICE/DTLS
650
- * completes; dropping those packets would lose the start of a media source.
651
- * `drainingPendingRtp` blocks re-entry while `dispatchRtp` is awaited;
652
- * packets enqueued during that wait are drained by the recursive call after
653
- * 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.
654
700
  */
655
- async drainPendingRtp() {
656
- if (this.drainingPendingRtp) {
657
- return;
658
- }
659
- this.drainingPendingRtp = true;
660
- try {
661
- while (this.pendingRtp.length > 0 && this.canSendRtp()) {
662
- const item = this.pendingRtp.shift();
663
- if (!this.canSendRtp()) {
664
- this.settlePendingRtp(item);
665
- continue;
666
- }
667
- try {
668
- await this.dispatchRtp(item.packet);
669
- this.settlePendingRtp(item);
670
- }
671
- catch (error) {
672
- this.settlePendingRtp(item, error);
673
- }
674
- }
675
- }
676
- finally {
677
- this.drainingPendingRtp = false;
678
- }
679
- if (this.pendingRtp.length > 0 && this.canSendRtp()) {
680
- await this.drainPendingRtp();
681
- }
701
+ drainPendingRtp() {
702
+ return this.pendingRtp.drain(() => this.canSendRtp(), (packet, sourceEpoch) => this.dispatchMedia(packet, sourceEpoch));
682
703
  }
683
704
  registerTrack(track) {
684
705
  if (track.stopped)
@@ -734,9 +755,12 @@ class RTCRtpSender {
734
755
  if (incompatible) {
735
756
  throw (0, errors_1.createWebRtcDomException)("InvalidModificationError", `Track codec ${sourceCodecs.map((codec) => codec.mimeType).join(", ")} is incompatible with negotiated codec ${incompatible.mimeType}`);
736
757
  }
737
- if (this.sequenceNumber != undefined) {
758
+ if (this.continuity.sequenceNumber != undefined) {
738
759
  this.scheduleRtpContinuity();
739
760
  }
761
+ else {
762
+ this.invalidateQueuedSource();
763
+ }
740
764
  this.registerTrack(track);
741
765
  log("replaceTrack", "ssrc", track.ssrc, "rid", track.rid);
742
766
  }
@@ -745,8 +769,9 @@ class RTCRtpSender {
745
769
  * The same sender can resume sending after `replaceTrack(track)`.
746
770
  */
747
771
  detachTrack() {
748
- this.rtpContinuityPending = false;
749
- this.discardPendingRtp();
772
+ this.continuity.cancel();
773
+ this.pendingRtp.discard();
774
+ this.invalidateQueuedSource();
750
775
  if (this.disposeTrack) {
751
776
  this.disposeTrack();
752
777
  this.disposeTrack = undefined;
@@ -760,10 +785,15 @@ class RTCRtpSender {
760
785
  for (const dispose of this.dtlsDisposer)
761
786
  dispose();
762
787
  this.dtlsDisposer = [];
763
- this.rtpContinuityPending = false;
764
- this.discardPendingRtp();
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();
765
793
  this.rtcpRunning = false;
766
794
  this.rtcpCancel.abort();
795
+ this.stats.clearSenderReports();
796
+ this.bwe.close();
767
797
  if (this.disposeTrack) {
768
798
  this.disposeTrack();
769
799
  this.disposeTrack = undefined;
@@ -771,6 +801,8 @@ class RTCRtpSender {
771
801
  if (!keepTrack) {
772
802
  this.track = null;
773
803
  }
804
+ this.dtlsDisposer.forEach((dispose) => dispose());
805
+ this.dtlsDisposer = [];
774
806
  }
775
807
  async runRtcp() {
776
808
  if (this.rtcpRunning || this.stopped)
@@ -781,19 +813,7 @@ class RTCRtpSender {
781
813
  await (0, promises_1.setTimeout)(500 + Math.random() * 1000, undefined, {
782
814
  signal: this.rtcpCancel.signal,
783
815
  });
784
- const packets = [
785
- new rtp_1.RtcpSrPacket({
786
- ssrc: this.ssrc,
787
- senderInfo: new rtp_1.RtcpSenderInfo({
788
- ntpTimestamp: this.ntpTimestamp,
789
- rtpTimestamp: this.rtpTimestamp,
790
- packetCount: this.packetCount,
791
- octetCount: this.octetCount,
792
- }),
793
- }),
794
- ];
795
- this.lastSRtimestamp = (0, utils_1.compactNtp)(this.ntpTimestamp);
796
- this.lastSentSRTimestamp = (0, utils_1.timestampSeconds)();
816
+ const packets = [this.buildSenderReport()];
797
817
  if (this.cname) {
798
818
  packets.push(new rtp_1.RtcpSourceDescriptionPacket({
799
819
  chunks: [
@@ -817,21 +837,93 @@ class RTCRtpSender {
817
837
  }
818
838
  catch (error) { }
819
839
  }
840
+ /** Sender Report for the instant `nowMonotonicMs` ({@link SenderStatistics.buildSenderReport}). */
841
+ buildSenderReport(nowMonotonicMs = performance.now()) {
842
+ return this.stats.buildSenderReport(this.ssrc, nowMonotonicMs);
843
+ }
820
844
  /**
821
- * Send an RTP packet. Pending RTP is disabled by default: the packet is
822
- * written immediately if DTLS is connected and a codec is set, otherwise
823
- * dropped.
824
- *
825
- * When pending RTP is enabled, the packet is queued until it can be sent.
826
- * The returned promise then settles for this packet only:
827
- * - resolve: DTLS send completed, or the packet was dropped by `stop()`,
828
- * `replaceTrack(null)`, or pending-queue overflow
829
- * - reject: DTLS send threw while writing this packet
845
+ * Schedule RTP continuity rewrite for the next packet that is actually sent.
846
+ * The header argument is kept for API compatibility and is not used to compute
847
+ * offsets. `discontinuity` does not change sequence or timestamp mapping.
848
+ * `timestampStep` (default 1) is the only way to choose the timestamp increment
849
+ * at the source-switch boundary.
830
850
  *
831
- * Later `sendRtp()` calls stay pending until their own packet is sent or
832
- * dropped, even if a drain is already in progress.
851
+ * Use this (or `MediaStreamTrack.notifySourceChanged`) when the RTP written
852
+ * into this sender's track switches to another upstream source: the next
853
+ * packet continues from the last sent one (`seq + 1`, `timestamp + step`)
854
+ * and queued packets of the old source are dropped. Apps that send RTP
855
+ * without `RTCRtpSender` (for example through `SrtpSession`) can use
856
+ * `RtpTimelineRewriter` from werift-rtp instead.
857
+ */
858
+ replaceRTP(header, discontinuity = false, timestampStep = 1) {
859
+ this.scheduleRtpContinuity(timestampStep);
860
+ log("replaceRTP", this.continuity.sequenceNumber, header.sequenceNumber, discontinuity, timestampStep);
861
+ }
862
+ /**
863
+ * Start a new source epoch and drop queued media of the previous source.
864
+ * A packet already past sequence allocation (in its DTLS write) completes;
865
+ * one still waiting on pacing/probe is dropped by {@link sendRtpInternal}.
866
+ */
867
+ invalidateQueuedSource() {
868
+ this.sourceEpoch++;
869
+ // Release the running FIFO task if it is a stale packet parked in a
870
+ // probe / pacing wait, so new-source media is not held behind it.
871
+ this.sourceEpochCancel.abort();
872
+ this.sourceEpochCancel = new AbortController();
873
+ this.outgoing.dropStaleSource(this.sourceEpoch);
874
+ this.pendingRtp.dropStaleSource(this.sourceEpoch);
875
+ }
876
+ isStaleSource(sourceEpoch) {
877
+ return sourceEpoch !== undefined && sourceEpoch !== this.sourceEpoch;
878
+ }
879
+ /**
880
+ * Abort signal for a send wait: sender stop, estimator swap, plus source
881
+ * switch for media.
882
+ */
883
+ sendWaitSignal(sourceEpoch) {
884
+ const signals = [this.rtcpCancel.signal, this.bwe.generationSignal];
885
+ if (sourceEpoch !== undefined) {
886
+ signals.push(this.sourceEpochCancel.signal);
887
+ }
888
+ return AbortSignal.any(signals);
889
+ }
890
+ /** A waiting send may still go out: not stopped, same estimator, same source. */
891
+ isSendAlive(generation, sourceEpoch) {
892
+ return (!this.stopped &&
893
+ generation === this.bwe.generation &&
894
+ !this.isStaleSource(sourceEpoch));
895
+ }
896
+ scheduleRtpContinuity(timestampStep = 1) {
897
+ this.invalidateQueuedSource();
898
+ this.continuity.schedule(timestampStep);
899
+ this.rtpCache = [];
900
+ }
901
+ dispatchMedia(packet, sourceEpoch) {
902
+ return this.dispatchOutgoing(() => this.sendRtpInternal(packet, {
903
+ injectProbePadding: true,
904
+ sourceEpoch,
905
+ }), { kind: "media", bytes: (0, rtpWire_1.estimateOutgoingBytes)(packet), sourceEpoch });
906
+ }
907
+ /**
908
+ * Lightweight / disabled estimators neither pace nor probe, so RTP is written
909
+ * straight through as before the BWE abstraction: no FIFO, no queue limit,
910
+ * and no wait before the DTLS write. Pacing estimators (GCC) go through
911
+ * {@link outgoing}. A FIFO still draining after an estimator swap
912
+ * keeps its order by queuing behind it.
913
+ */
914
+ dispatchOutgoing(task, meta) {
915
+ if (!this.outgoing.running && !(0, bandwidthEstimator_1.needsPacedSendPath)(this.bwe.estimator)) {
916
+ return task();
917
+ }
918
+ return this.outgoing.enqueue(task, meta);
919
+ }
920
+ /**
921
+ * Send immediately by default or queue until DTLS is ready when pending RTP
922
+ * is enabled. Each call settles only after its own packet is sent or dropped.
833
923
  */
834
924
  async sendRtp(rtp) {
925
+ // Drop (do not queue) while the negotiated direction does not send, so
926
+ // nothing is flushed in a burst when sending resumes.
835
927
  if (this.stopped || this.sendSuppressed) {
836
928
  return;
837
929
  }
@@ -839,127 +931,299 @@ class RTCRtpSender {
839
931
  if (!this.canSendRtp()) {
840
932
  return;
841
933
  }
842
- await this.dispatchRtp(rtp);
934
+ // Own the packet before any pacing wait: a track shared by several
935
+ // senders hands every subscriber the same RtpPacket, and header
936
+ // rewrites (SSRC/seq/extensions) must not leak across senders.
937
+ const packet = Buffer.isBuffer(rtp)
938
+ ? rtp_1.RtpPacket.deSerialize(rtp)
939
+ : rtp.clone();
940
+ await this.dispatchMedia(packet, this.sourceEpoch);
843
941
  return;
844
942
  }
845
943
  await new Promise((resolve, reject) => {
846
- this.enqueuePendingRtp(rtp, resolve, reject);
944
+ this.pendingRtp.enqueue(rtp, this.sourceEpoch, resolve, reject);
847
945
  void this.drainPendingRtp();
848
946
  });
849
947
  }
948
+ /** True when transport-wide CC header extension is negotiated. */
949
+ isTransportCcNegotiated() {
950
+ return this.headerExtensions.some((e) => e.uri === rtp_1.RTP_EXTENSION_URI.transportWideCC);
951
+ }
850
952
  /**
851
- * Schedule RTP continuity rewrite for the next packet that is actually sent.
852
- * The header argument is kept for API compatibility and is not used to compute
853
- * offsets. `discontinuity` does not change sequence or timestamp mapping.
854
- * `timestampStep` (default 1) is the only way to choose the timestamp increment
855
- * at the source-switch boundary.
953
+ * Padding needs an estimator that asks for it, TWCC so feedback can see it,
954
+ * and a negotiated RTX stream to carry it. Without RTX no padding is sent
955
+ * (see `GCC_KNOWN_DIFFERENCES`): media sequence numbers stay untouched.
856
956
  */
857
- replaceRTP(header, discontinuity = false, timestampStep = 1) {
858
- this.scheduleRtpContinuity(timestampStep);
859
- log("replaceRTP", this.sequenceNumber, header.sequenceNumber, discontinuity, timestampStep);
957
+ canSendPadding() {
958
+ return (!this.sendSuppressed &&
959
+ (0, bandwidthEstimator_1.isProbePacingController)(this.bwe.estimator) &&
960
+ this.rtxPayloadType !== undefined &&
961
+ this.isTransportCcNegotiated());
860
962
  }
861
- scheduleRtpContinuity(timestampStep = 1) {
862
- this.rtpContinuityPending = true;
863
- this.pendingTimestampStep = timestampStep;
864
- this.rtpCache = [];
963
+ /**
964
+ * Inject RTP padding packets while a GCC probe cluster is active and media
965
+ * alone has not yet filled min packets / min bitrate×duration.
966
+ *
967
+ * Responsibility split:
968
+ * - Estimator (`ProbeController` / `pendingProbePaddingPackets`) decides need
969
+ * - Sender generates padding-only RTP on the RTX SSRC with a TWCC seq +
970
+ * `isProbation` and paces them
971
+ */
972
+ async maybeInjectProbePadding() {
973
+ if (!this.canSendPadding()) {
974
+ return 0;
975
+ }
976
+ if (this.nestOutgoingPadding) {
977
+ return this.padding.drainProbe();
978
+ }
979
+ return ((await this.outgoing.enqueue(() => this.padding.drainProbe(), {
980
+ kind: "padding",
981
+ bytes: this.bwe.estimator.probePaddingPacketBytes,
982
+ })) ?? 0);
865
983
  }
866
- async dispatchRtp(rtp) {
867
- if (!this.canSendRtp()) {
868
- return;
984
+ /**
985
+ * pin padding_rate while LossBased is `kIncreaseUsingPadding`.
986
+ * Regular RTP padding (not probe/probation). Probe padding takes priority.
987
+ */
988
+ async maybeInjectLossPadding() {
989
+ if (!this.canSendPadding()) {
990
+ return 0;
869
991
  }
870
- const codec = this.codec;
871
- if (!codec) {
992
+ return ((await this.outgoing.enqueue(() => this.padding.drainLoss(), {
993
+ kind: "padding",
994
+ bytes: this.bwe.estimator.probePaddingPacketBytes,
995
+ })) ?? 0);
996
+ }
997
+ createPaddingInjector() {
998
+ const sender = this;
999
+ return new padding_1.PaddingInjector({
1000
+ get estimator() {
1001
+ return sender.bwe.estimator;
1002
+ },
1003
+ get generation() {
1004
+ return sender.bwe.generation;
1005
+ },
1006
+ canDrain: () => !sender.stopped &&
1007
+ sender.dtlsTransport?.state === "connected" &&
1008
+ !!sender.codec &&
1009
+ sender.canSendPadding(),
1010
+ isCancelled: (generation) => sender.stopped ||
1011
+ sender.sendSuppressed ||
1012
+ generation !== sender.bwe.generation,
1013
+ hasQueuedRealtimeMedia: () => sender.outgoing.hasQueuedRealtimeMedia(),
1014
+ sendPadding: (packetBytes, probe) => sender.sendRtpInternal(sender.createPaddingRtpPacket(packetBytes), {
1015
+ injectProbePadding: false,
1016
+ forceProbeTag: probe,
1017
+ isProbePadding: true,
1018
+ }),
1019
+ });
1020
+ }
1021
+ /** Padding-only RTP on the RTX SSRC / payload type ({@link createRtxPaddingPacket}). */
1022
+ createPaddingRtpPacket(paddingSize) {
1023
+ return (0, rtp_1.createRtxPaddingPacket)({
1024
+ paddingSize,
1025
+ timestamp: this.continuity.timestamp ?? 0,
1026
+ payloadType: this.rtxPayloadType,
1027
+ ssrc: this.rtxSsrc,
1028
+ });
1029
+ }
1030
+ async sendRtpInternal(rtp, opts = {}) {
1031
+ if (this.stopped)
1032
+ return;
1033
+ // Not sending in the negotiated direction: drop media and padding.
1034
+ // NACK retransmissions keep develop's behavior.
1035
+ if (this.sendSuppressed && !opts.isRetransmission)
1036
+ return;
1037
+ if (this.dtlsTransport.state !== "connected" || !this.codec) {
872
1038
  return;
873
1039
  }
874
- rtp = Buffer.isBuffer(rtp) ? rtp_1.RtpPacket.deSerialize(rtp) : rtp.clone();
1040
+ const sendClockRate = (this.sendPrimaryCodec ?? this.codec).clockRate;
1041
+ rtp = Buffer.isBuffer(rtp) ? rtp_1.RtpPacket.deSerialize(rtp) : rtp;
875
1042
  const { header, payload } = rtp;
1043
+ // Capture BWE generation at send start. After await sendRtp, only deliver
1044
+ // SentInfo when the generation is unchanged — mid-send setBandwidthEstimator
1045
+ // must not pollute the new clean estimator with packets planned under the old one.
1046
+ const sendGeneration = this.bwe.generation;
1047
+ const estimatorAtStart = this.bwe.estimator;
1048
+ const twccOn = this.isTransportCcNegotiated();
1049
+ // Lightweight / disabled: no probe reservation or pacing wait (direct path).
1050
+ const paced = (0, bandwidthEstimator_1.needsPacedSendPath)(estimatorAtStart);
1051
+ // Stale source: drop before touching probe reservation or pacing budget.
1052
+ if (this.isStaleSource(opts.sourceEpoch)) {
1053
+ return;
1054
+ }
1055
+ // pin CurrentCluster: reserve probe id **before** the async send so a
1056
+ // concurrent completion cannot re-attribute this packet to the next cluster.
1057
+ let reservedClusterId;
1058
+ const wantsProbe = opts.forceProbeTag === true || estimatorAtStart.shouldTagProbePacket();
1059
+ if (paced && twccOn && wantsProbe) {
1060
+ const reservation = estimatorAtStart.reserveOutgoingProbe((0, utils_1.milliTime)());
1061
+ if (reservation) {
1062
+ reservedClusterId = reservation.clusterId;
1063
+ if (!(await this.awaitProbeSendTime(reservation.nextSendTimeMs, sendGeneration, opts.sourceEpoch))) {
1064
+ return;
1065
+ }
1066
+ }
1067
+ else if (opts.forceProbeTag) {
1068
+ // Cluster already filled / discarded — do not emit untagged padding.
1069
+ return;
1070
+ }
1071
+ }
876
1072
  const inputSequenceNumber = header.sequenceNumber;
877
1073
  const inputTimestamp = header.timestamp;
878
- if (this.rtpContinuityPending) {
879
- if (this.sequenceNumber != undefined && this.timestamp != undefined) {
880
- const offsets = freezeRtpContinuityOffsets(this.sequenceNumber, this.timestamp, inputSequenceNumber, inputTimestamp, this.pendingTimestampStep);
881
- this.seqOffset = offsets.seqOffset;
882
- this.timestampOffset = offsets.timestampOffset;
883
- }
884
- this.rtpContinuityPending = false;
885
- this.pendingTimestampStep = 1;
1074
+ if (opts.isProbePadding) {
1075
+ // Padding is only ever sent on the negotiated RTX stream.
1076
+ if (this.rtxPayloadType === undefined)
1077
+ return;
1078
+ header.ssrc = this.rtxSsrc;
1079
+ header.payloadType = this.rtxPayloadType;
886
1080
  }
887
- header.ssrc = this.ssrc;
888
- header.payloadType = codec.payloadType;
889
- header.timestamp = (0, common_1.uint32Add)(header.timestamp, this.timestampOffset);
890
- header.sequenceNumber = (0, common_1.uint16Add)(header.sequenceNumber, this.seqOffset);
891
- this.timestamp = header.timestamp;
892
- this.sequenceNumber = header.sequenceNumber;
893
- const ntpTimestamp = (0, utils_1.ntpTime)();
894
- const originalHeaderExtensions = [...header.extensions];
895
- header.extensions = this.headerExtensions
896
- .map((extension) => {
897
- const payload = (() => {
898
- switch (extension.uri) {
899
- case rtp_1.RTP_EXTENSION_URI.sdesMid:
900
- if (this.mid) {
901
- return (0, rtp_1.serializeSdesMid)(this.mid);
902
- }
903
- return;
904
- // todo : sender simulcast unsupported now
905
- case rtp_1.RTP_EXTENSION_URI.sdesRTPStreamID:
906
- if (this.rtpStreamId) {
907
- return (0, rtp_1.serializeSdesRTPStreamID)(this.rtpStreamId);
908
- }
909
- return;
910
- // todo : sender simulcast unsupported now
911
- case rtp_1.RTP_EXTENSION_URI.repairedRtpStreamId:
912
- if (this.repairedRtpStreamId) {
913
- return (0, rtp_1.serializeRepairedRtpStreamId)(this.repairedRtpStreamId);
914
- }
915
- return;
916
- case rtp_1.RTP_EXTENSION_URI.transportWideCC:
917
- this.dtlsTransport.transportSequenceNumber = (0, common_1.uint16Add)(this.dtlsTransport.transportSequenceNumber, 1);
918
- return (0, rtp_1.serializeTransportWideCC)(this.dtlsTransport.transportSequenceNumber);
919
- case rtp_1.RTP_EXTENSION_URI.absSendTime:
920
- return (0, rtp_1.serializeAbsSendTime)(ntpTimestamp);
921
- }
922
- })();
923
- if (payload)
924
- return { id: extension.id, payload };
925
- })
926
- .filter((v) => v);
927
- for (const ext of originalHeaderExtensions) {
928
- const exist = header.extensions.find((v) => v.id === ext.id);
929
- if (exist) {
930
- exist.payload = ext.payload;
1081
+ else if (!opts.isRetransmission) {
1082
+ header.ssrc = this.ssrc;
1083
+ header.payloadType = this.codec.payloadType;
1084
+ header.timestamp = this.continuity.mapTimestamp(inputTimestamp);
1085
+ }
1086
+ header.extensions = (0, rtp_1.buildSendHeaderExtensions)(header.extensions, this.headerExtensions, {
1087
+ mid: this.mid,
1088
+ rtpStreamId: this.rtpStreamId,
1089
+ repairedRtpStreamId: this.repairedRtpStreamId,
1090
+ });
1091
+ const useRed = !!this.redRedundantPayloadType &&
1092
+ !opts.isProbePadding &&
1093
+ !opts.isRetransmission;
1094
+ const estimatedPayloadLen = useRed
1095
+ ? this.redEncoder.serializedLengthWith({
1096
+ block: payload,
1097
+ timestamp: header.timestamp,
1098
+ blockPT: this.redRedundantPayloadType,
1099
+ })
1100
+ : payload.length;
1101
+ const rfcPaddingLen = header.padding && header.paddingSize > 0 ? header.paddingSize : 0;
1102
+ const constructedBytes = header.serializeSize + estimatedPayloadLen + rfcPaddingLen;
1103
+ if (paced &&
1104
+ reservedClusterId === undefined &&
1105
+ this.mediaPacingEnabled &&
1106
+ twccOn &&
1107
+ estimatorAtStart.getPacingBitrateBps() > 0) {
1108
+ if (!(await this.awaitPacingBudget(constructedBytes, sendGeneration, opts.sourceEpoch))) {
1109
+ return;
931
1110
  }
932
- else {
933
- header.extensions.push(ext);
1111
+ }
1112
+ if (this.stopped || sendGeneration !== this.bwe.generation) {
1113
+ return;
1114
+ }
1115
+ // Direction changed to not sending while this packet waited.
1116
+ if (this.sendSuppressed && !opts.isRetransmission) {
1117
+ return;
1118
+ }
1119
+ // Accepted under a replaced/detached source: never send it, and never let
1120
+ // it consume the new source's continuity offset.
1121
+ if (this.isStaleSource(opts.sourceEpoch)) {
1122
+ return;
1123
+ }
1124
+ if (opts.isProbePadding) {
1125
+ // RTX owns its sequence space, so padding never shifts media numbers.
1126
+ header.sequenceNumber = this.rtxSequenceNumber;
1127
+ this.rtxSequenceNumber = (0, common_1.uint16Add)(this.rtxSequenceNumber, 1);
1128
+ header.timestamp = this.continuity.timestamp ?? header.timestamp;
1129
+ }
1130
+ else if (!opts.isRetransmission) {
1131
+ if (this.continuity.assign(header, inputSequenceNumber, inputTimestamp)) {
1132
+ // ソース境界を SR の RTP/NTP 基準にも反映する。
1133
+ this.stats.resetClockAnchor();
934
1134
  }
935
1135
  }
936
- header.extensions = header.extensions.sort((a, b) => a.id - b.id);
937
- this.ntpTimestamp = ntpTimestamp;
938
- this.rtpTimestamp = header.timestamp;
939
- this.octetCount += payload.length;
940
- this.headerBytesSent += header.serializeSize;
941
- this.packetCount = (0, common_1.uint32Add)(this.packetCount, 1);
942
- this.rtpCache[header.sequenceNumber % RTP_HISTORY_SIZE] = rtp;
943
1136
  let rtpPayload = payload;
944
- if (this.redRedundantPayloadType) {
1137
+ if (useRed) {
945
1138
  this.redEncoder.push({
946
1139
  block: rtpPayload,
947
1140
  timestamp: header.timestamp,
948
1141
  blockPT: this.redRedundantPayloadType,
949
1142
  });
950
- const red = this.redEncoder.build();
951
- rtpPayload = red.serialize();
1143
+ rtpPayload = this.redEncoder.build().serialize();
1144
+ }
1145
+ const payloadOctetsForSr = rtpPayload.length;
1146
+ if (header.padding && header.paddingSize > 0) {
1147
+ rtpPayload = (0, rtp_1.appendRfc3550Padding)(rtpPayload, header.paddingSize);
1148
+ }
1149
+ const packetWideSeq = (0, rtp_1.stampHopByHopExtensions)(header, this.headerExtensions, {
1150
+ twccOn,
1151
+ allocateTransportWideSequence: () => this.dtlsTransport.allocateTransportWideSequence(),
1152
+ ntpTimestamp: (0, utils_1.ntpTime)(),
1153
+ });
1154
+ if (opts.isProbePadding) {
1155
+ // RTX padding: not part of the media SSRC's SR packet / octet counts.
1156
+ }
1157
+ else if (opts.isRetransmission) {
1158
+ this.stats.recordRetransmissionSent(header.serializeSize, payload.length);
1159
+ }
1160
+ else {
1161
+ this.stats.recordMediaSent(header.serializeSize, payloadOctetsForSr);
1162
+ if (!(0, rtp_1.isPaddingOnlyRtpPacket)(rtp)) {
1163
+ this.rtpCache[header.sequenceNumber % RTP_HISTORY_SIZE] = rtp;
1164
+ }
1165
+ }
1166
+ const sendingAtMs = (0, utils_1.milliTime)();
1167
+ const { size, sendingAtMs: enqueueAtMs } = await (0, rtpWire_1.sendDtlsRtp)(this.dtlsTransport, rtpPayload, header, sendingAtMs);
1168
+ const sentAtMs = (0, utils_1.milliTime)();
1169
+ if (!opts.isProbePadding &&
1170
+ !opts.isRetransmission &&
1171
+ !(0, rtp_1.isPaddingOnlyRtpPacket)(rtp)) {
1172
+ this.stats.updateClockAnchor(header.timestamp, sendClockRate);
952
1173
  }
953
- const size = await this.dtlsTransport.sendRtp(rtpPayload, header);
954
1174
  this.runRtcp();
955
- const millitime = (0, utils_1.milliTime)();
956
- const sentInfo = {
957
- wideSeq: this.dtlsTransport.transportSequenceNumber,
958
- size,
959
- sendingAtMs: millitime,
960
- sentAtMs: millitime,
961
- };
962
- this.senderBWE.rtpPacketSent(sentInfo);
1175
+ // BWE / TWCC only when transport-cc is negotiated — otherwise wideSeq would
1176
+ // not advance and probe padding would be useless / harmful.
1177
+ // Generation match: estimator was not swapped (or reset) while this packet
1178
+ // was in flight. Mismatch → discard (do not feed disposed or new clean BWE).
1179
+ if (twccOn &&
1180
+ packetWideSeq !== undefined &&
1181
+ sendGeneration === this.bwe.generation) {
1182
+ const priorUnacked = this.pendingUntrackedBytes;
1183
+ this.pendingUntrackedBytes = 0;
1184
+ const sentInfo = {
1185
+ wideSeq: packetWideSeq,
1186
+ size,
1187
+ sendingAtMs: enqueueAtMs,
1188
+ sentAtMs,
1189
+ isProbation: reservedClusterId !== undefined,
1190
+ probeClusterId: reservedClusterId,
1191
+ priorUnackedBytes: priorUnacked,
1192
+ isRetransmission: opts.isRetransmission === true,
1193
+ };
1194
+ estimatorAtStart.rtpPacketSent(sentInfo);
1195
+ }
1196
+ else if (twccOn && sendGeneration === this.bwe.generation) {
1197
+ // Same generation but no wideSeq (should be rare). Do not fold
1198
+ // previous-generation bytes into the next estimator.
1199
+ this.pendingUntrackedBytes += size;
1200
+ }
1201
+ if (opts.injectProbePadding &&
1202
+ twccOn &&
1203
+ (0, bandwidthEstimator_1.isProbePacingController)(estimatorAtStart) &&
1204
+ sendGeneration === this.bwe.generation) {
1205
+ this.nestOutgoingPadding = true;
1206
+ try {
1207
+ await this.maybeInjectProbePadding();
1208
+ }
1209
+ finally {
1210
+ this.nestOutgoingPadding = false;
1211
+ }
1212
+ }
1213
+ }
1214
+ /** pin BitrateProber next_probe_time wait ({@link waitForProbeSendTime}). */
1215
+ awaitProbeSendTime(nextSendTimeMs, generation, sourceEpoch) {
1216
+ return (0, sendPacer_1.waitForProbeSendTime)(nextSendTimeMs, () => this.isSendAlive(generation, sourceEpoch), () => this.sendWaitSignal(sourceEpoch));
1217
+ }
1218
+ /**
1219
+ * Token-bucket wait against {@link pacingBitrateBps} for **media**.
1220
+ * Probe packets use {@link awaitProbeSendTime} instead.
1221
+ * Returns false if the sender is stopped, the estimator is swapped
1222
+ * (`generation` changes), or the packet's source is replaced/detached while
1223
+ * waiting (budget is not consumed then).
1224
+ */
1225
+ awaitPacingBudget(packetBytes, generation, sourceEpoch) {
1226
+ return this.pacer.acquire(packetBytes, this.pacingBitrateBps, () => this.isSendAlive(generation, sourceEpoch), () => this.sendWaitSignal(sourceEpoch));
963
1227
  }
964
1228
  /**
965
1229
  * @param firEntries Internal: the FCI entries of a FIR the router resolved to
@@ -975,22 +1239,10 @@ class RTCRtpSender {
975
1239
  packet.reports
976
1240
  .filter((report) => report.ssrc === this.ssrc)
977
1241
  .forEach((report) => {
978
- this.remotePacketsLost = report.packetsLost;
979
- this.remoteFractionLost = report.fractionLost / 256;
980
- if (this.lastSRtimestamp === report.lsr && report.dlsr) {
981
- if (this.lastSentSRTimestamp) {
982
- const rtt = (0, utils_1.timestampSeconds)() -
983
- this.lastSentSRTimestamp -
984
- report.dlsr / 65536;
985
- this.totalRoundTripTime += rtt;
986
- this.roundTripTimeMeasurements++;
987
- if (this.rtt === undefined) {
988
- this.rtt = rtt;
989
- }
990
- else {
991
- this.rtt = RTT_ALPHA * this.rtt + (1 - RTT_ALPHA) * rtt;
992
- }
993
- }
1242
+ const rawRttSeconds = this.stats.recordReceptionReport(report);
1243
+ // Only unsmoothed RTT goes to AIMD.
1244
+ if (rawRttSeconds !== undefined && rawRttSeconds > 0) {
1245
+ this.bwe.estimator.setRoundTripTime(rawRttSeconds * 1000);
994
1246
  }
995
1247
  });
996
1248
  }
@@ -1008,21 +1260,30 @@ class RTCRtpSender {
1008
1260
  case rtp_1.GenericNack.count:
1009
1261
  {
1010
1262
  const feedback = packet.feedback;
1011
- this.nackCount++;
1263
+ this.stats.nackCount++;
1012
1264
  feedback.lost.forEach(async (seqNum) => {
1013
1265
  let packet = this.rtpCache[seqNum % RTP_HISTORY_SIZE];
1014
1266
  if (packet && packet.header.sequenceNumber !== seqNum) {
1015
1267
  packet = undefined;
1016
1268
  }
1269
+ if (packet && (0, rtp_1.isPaddingOnlyRtpPacket)(packet)) {
1270
+ packet = undefined;
1271
+ }
1017
1272
  if (packet) {
1018
1273
  if (this.rtxPayloadType != undefined) {
1019
1274
  packet = (0, rtp_1.wrapRtx)(packet, this.rtxPayloadType, this.rtxSequenceNumber, this.rtxSsrc);
1020
1275
  this.rtxSequenceNumber = (0, common_1.uint16Add)(this.rtxSequenceNumber, 1);
1021
1276
  }
1022
- this.retransmittedPacketsSent++;
1023
- this.retransmittedBytesSent += packet.payload.length;
1024
- this.headerBytesSent += packet.header.serializeSize;
1025
- await this.dtlsTransport.sendRtp(packet.payload, packet.header);
1277
+ const retransmission = packet;
1278
+ // Route through sendRtpInternal: new TWCC seq, pacing,
1279
+ // BWE SentInfo. Do not reuse the original transport-wide seq.
1280
+ await this.dispatchOutgoing(() => this.sendRtpInternal(retransmission, {
1281
+ injectProbePadding: false,
1282
+ isRetransmission: true,
1283
+ }), {
1284
+ kind: "rtx",
1285
+ bytes: (0, rtpWire_1.estimateOutgoingBytes)(retransmission),
1286
+ });
1026
1287
  }
1027
1288
  });
1028
1289
  this.onGenericNack.execute(feedback);
@@ -1043,7 +1304,7 @@ class RTCRtpSender {
1043
1304
  break;
1044
1305
  case rtp_1.PictureLossIndication.count:
1045
1306
  {
1046
- this.pliCount++;
1307
+ this.stats.pliCount++;
1047
1308
  this.onPictureLossIndication.execute();
1048
1309
  this.onKeyFrameRequest.execute("pli");
1049
1310
  }
@@ -1057,7 +1318,7 @@ class RTCRtpSender {
1057
1318
  break;
1058
1319
  // W3C webrtc-stats: firCount is the number of FIR packets received,
1059
1320
  // however many FCI entries of the packet resolve to this sender
1060
- this.firCount++;
1321
+ this.stats.firCount++;
1061
1322
  // an entry with the same sequence number as the last one for its
1062
1323
  // (sender, target) pair is a retransmission (RFC 5104 4.3.1.1)
1063
1324
  let isNew = false;
@@ -1092,100 +1353,22 @@ class RTCRtpSender {
1092
1353
  this.setSendEncodings(params.encodings);
1093
1354
  }
1094
1355
  }
1095
- get outboundRtpStatsId() {
1096
- return (0, stats_1.generateStatsId)("outbound-rtp", this.trackId);
1097
- }
1098
- get mediaSourceStatsId() {
1099
- return (0, stats_1.generateStatsId)("media-source", this.trackId);
1100
- }
1101
- get remoteInboundRtpStatsId() {
1102
- return (0, stats_1.generateStatsId)("remote-inbound-rtp", this.trackId);
1103
- }
1104
1356
  getStatsRootIds() {
1105
- return [this.outboundRtpStatsId];
1357
+ return [(0, senderStats_1.senderStatsIds)(this.trackId).outboundRtp];
1106
1358
  }
1107
1359
  collectStats(timestamp) {
1108
- const stats = [];
1109
- const transportId = this.dtlsTransport
1110
- ? (0, stats_1.generateStatsId)("transport", this.dtlsTransport.id)
1111
- : undefined;
1112
- const codecId = this.codec && transportId
1113
- ? (0, stats_1.generateCodecStatsId)(transportId, this.codec.payloadType, this.trackId)
1114
- : undefined;
1115
- // Outbound RTP stats
1116
- const outboundRtpStats = {
1117
- type: "outbound-rtp",
1118
- id: this.outboundRtpStatsId,
1119
- timestamp,
1360
+ return this.stats.collect({
1361
+ trackId: this.trackId,
1120
1362
  ssrc: this.ssrc,
1121
1363
  kind: this.kind,
1122
- transportId,
1123
- codecId,
1124
1364
  mid: this.mid,
1125
- packetsSent: this.packetCount,
1126
- bytesSent: this.octetCount,
1127
- headerBytesSent: this.headerBytesSent,
1128
- retransmittedPacketsSent: this.retransmittedPacketsSent || undefined,
1129
- retransmittedBytesSent: this.retransmittedBytesSent || undefined,
1365
+ dtlsTransport: this.dtlsTransport,
1366
+ codec: this.codec,
1130
1367
  rtxSsrc: this.rtxPayloadType ? this.rtxSsrc : undefined,
1131
- mediaSourceId: this.track ? this.mediaSourceStatsId : undefined,
1132
- remoteId: this.rtt !== undefined ||
1133
- this.remotePacketsLost !== undefined ||
1134
- this.remoteFractionLost !== undefined
1135
- ? this.remoteInboundRtpStatsId
1368
+ trackIdentifier: this.track
1369
+ ? (this.track.id ?? this.trackId)
1136
1370
  : undefined,
1137
- nackCount: this.nackCount || undefined,
1138
- pliCount: this.pliCount || undefined,
1139
- firCount: this.firCount || undefined,
1140
- };
1141
- stats.push(outboundRtpStats);
1142
- // Media source stats
1143
- if (this.track) {
1144
- const mediaSourceStats = {
1145
- type: "media-source",
1146
- id: this.mediaSourceStatsId,
1147
- timestamp,
1148
- trackIdentifier: this.track.id ?? this.trackId,
1149
- kind: this.kind,
1150
- };
1151
- stats.push(mediaSourceStats);
1152
- }
1153
- if (this.codec && transportId) {
1154
- const codecStats = {
1155
- type: "codec",
1156
- id: codecId,
1157
- timestamp,
1158
- payloadType: this.codec.payloadType,
1159
- transportId,
1160
- mimeType: this.codec.mimeType,
1161
- clockRate: this.codec.clockRate,
1162
- channels: this.codec.channels,
1163
- sdpFmtpLine: this.codec.parameters,
1164
- };
1165
- stats.push(codecStats);
1166
- }
1167
- // Remote inbound RTP stats (if we have RTT)
1168
- if (this.rtt !== undefined ||
1169
- this.remotePacketsLost !== undefined ||
1170
- this.remoteFractionLost !== undefined) {
1171
- const remoteInboundStats = {
1172
- type: "remote-inbound-rtp",
1173
- id: this.remoteInboundRtpStatsId,
1174
- timestamp,
1175
- ssrc: this.ssrc,
1176
- kind: this.kind,
1177
- transportId,
1178
- codecId: outboundRtpStats.codecId,
1179
- localId: outboundRtpStats.id,
1180
- roundTripTime: this.rtt,
1181
- totalRoundTripTime: this.totalRoundTripTime,
1182
- roundTripTimeMeasurements: this.roundTripTimeMeasurements,
1183
- packetsLost: this.remotePacketsLost,
1184
- fractionLost: this.remoteFractionLost,
1185
- };
1186
- stats.push(remoteInboundStats);
1187
- }
1188
- return stats;
1371
+ }, timestamp);
1189
1372
  }
1190
1373
  async getStats() {
1191
1374
  const timestamp = (0, stats_1.getStatsTimestamp)();