mediasoup 3.26.0 → 3.27.1

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 (322) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/node/lib/DataConsumer.js +1 -1
  3. package/node/lib/Router.js +1 -1
  4. package/node/lib/Transport.js +15 -15
  5. package/node/lib/fbs/rtp-parameters/encoding-mapping.d.ts +2 -6
  6. package/node/lib/fbs/rtp-parameters/encoding-mapping.d.ts.map +1 -1
  7. package/node/lib/fbs/rtp-parameters/encoding-mapping.js +7 -19
  8. package/node/lib/fbs/rtp-parameters/rtp-encoding-parameters.d.ts +4 -4
  9. package/node/lib/fbs/rtp-parameters/rtp-encoding-parameters.js +2 -2
  10. package/node/lib/fbs/rtp-stream/bitrate-by-layer.d.ts +5 -5
  11. package/node/lib/fbs/rtp-stream/bitrate-by-layer.d.ts.map +1 -1
  12. package/node/lib/fbs/rtp-stream/bitrate-by-layer.js +3 -3
  13. package/node/lib/fbs/rtp-stream/recv-stats.d.ts +5 -5
  14. package/node/lib/fbs/rtp-stream/recv-stats.d.ts.map +1 -1
  15. package/node/lib/fbs/rtp-stream/recv-stats.js +3 -3
  16. package/node/lib/fbs/rtp-stream/send-stats.d.ts +5 -5
  17. package/node/lib/fbs/rtp-stream/send-stats.d.ts.map +1 -1
  18. package/node/lib/fbs/rtp-stream/send-stats.js +3 -3
  19. package/node/lib/fbs/transport/bwe-trace-info.d.ts +23 -23
  20. package/node/lib/fbs/transport/bwe-trace-info.d.ts.map +1 -1
  21. package/node/lib/fbs/transport/bwe-trace-info.js +15 -15
  22. package/node/lib/fbs/transport/options.d.ts +5 -5
  23. package/node/lib/fbs/transport/options.js +2 -2
  24. package/node/lib/fbs/transport/set-max-incoming-bitrate-request.d.ts +5 -5
  25. package/node/lib/fbs/transport/set-max-incoming-bitrate-request.d.ts.map +1 -1
  26. package/node/lib/fbs/transport/set-max-incoming-bitrate-request.js +3 -3
  27. package/node/lib/fbs/transport/set-max-outgoing-bitrate-request.d.ts +5 -5
  28. package/node/lib/fbs/transport/set-max-outgoing-bitrate-request.d.ts.map +1 -1
  29. package/node/lib/fbs/transport/set-max-outgoing-bitrate-request.js +3 -3
  30. package/node/lib/fbs/transport/set-min-outgoing-bitrate-request.d.ts +5 -5
  31. package/node/lib/fbs/transport/set-min-outgoing-bitrate-request.d.ts.map +1 -1
  32. package/node/lib/fbs/transport/set-min-outgoing-bitrate-request.js +3 -3
  33. package/node/lib/fbs/transport/stats.d.ts +38 -38
  34. package/node/lib/fbs/transport/stats.d.ts.map +1 -1
  35. package/node/lib/fbs/transport/stats.js +25 -25
  36. package/node/lib/ortc.d.ts +0 -1
  37. package/node/lib/ortc.d.ts.map +1 -1
  38. package/node/lib/ortc.js +1 -2
  39. package/node/lib/rtpParametersFbsUtils.d.ts.map +1 -1
  40. package/node/lib/rtpParametersFbsUtils.js +2 -2
  41. package/package.json +9 -9
  42. package/worker/deps/libwebrtc/libwebrtc/call/rtp_transport_controller_send.cc +10 -10
  43. package/worker/deps/libwebrtc/libwebrtc/mediasoup_helpers.h +6 -6
  44. package/worker/deps/libwebrtc/libwebrtc/modules/congestion_controller/goog_cc/alr_detector.cc +1 -1
  45. package/worker/deps/libwebrtc/libwebrtc/modules/pacing/paced_sender.cc +7 -7
  46. package/worker/deps/libwebrtc/libwebrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h +1 -1
  47. package/worker/deps/libwebrtc/libwebrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.cc +1 -1
  48. package/worker/fbs/FBS/meson.build +25 -0
  49. package/worker/fbs/meson.build +15 -18
  50. package/worker/fbs/rtpParameters.fbs +1 -2
  51. package/worker/fbs/rtpStream.fbs +3 -3
  52. package/worker/fbs/transport.fbs +23 -23
  53. package/worker/fuzzer/include/RTC/FuzzerDtlsTransport.hpp +4 -1
  54. package/worker/fuzzer/src/FuzzerUtils.cpp +5 -3
  55. package/worker/fuzzer/src/RTC/FuzzerDtlsTransport.cpp +8 -5
  56. package/worker/fuzzer/src/RTC/FuzzerRateCalculator.cpp +3 -3
  57. package/worker/fuzzer/src/RTC/FuzzerTrendCalculator.cpp +6 -6
  58. package/worker/fuzzer/src/RTC/RTCP/FuzzerFeedbackRtpTransport.cpp +3 -3
  59. package/worker/fuzzer/src/RTC/RTP/FuzzerPacket.cpp +2 -2
  60. package/worker/fuzzer/src/RTC/RTP/FuzzerRetransmissionBuffer.cpp +5 -5
  61. package/worker/fuzzer/src/RTC/RTP/FuzzerRtpStreamSend.cpp +3 -3
  62. package/worker/fuzzer/src/fuzzer.cpp +4 -0
  63. package/worker/include/Channel/ChannelNotification.hpp +3 -3
  64. package/worker/include/Channel/ChannelRequest.hpp +1 -1
  65. package/worker/include/Channel/ChannelSocket.hpp +11 -0
  66. package/worker/include/DepLibUV.hpp +16 -33
  67. package/worker/include/RTC/ActiveSpeakerObserver.hpp +11 -11
  68. package/worker/include/RTC/AudioLevelObserver.hpp +4 -4
  69. package/worker/include/RTC/BWE/AcknowledgedBitrateEstimatorInterface.hpp +55 -0
  70. package/worker/include/RTC/BWE/AimdRateControl.hpp +190 -0
  71. package/worker/include/RTC/BWE/BweTypes.hpp +211 -0
  72. package/worker/include/RTC/BWE/DelayBasedBwe.hpp +173 -0
  73. package/worker/include/RTC/BWE/FeedbackAdapter.hpp +72 -0
  74. package/worker/include/RTC/BWE/InterArrivalDelta.hpp +106 -0
  75. package/worker/include/RTC/BWE/LinkCapacityEstimator.hpp +84 -0
  76. package/worker/include/RTC/BWE/LossBasedController.hpp +520 -0
  77. package/worker/include/RTC/BWE/RobustThroughputEstimator.hpp +94 -0
  78. package/worker/include/RTC/BWE/SendPacketHistory.hpp +137 -0
  79. package/worker/include/RTC/BWE/TargetRateController.hpp +306 -0
  80. package/worker/include/RTC/BWE/TrendlineEstimator.hpp +143 -0
  81. package/worker/include/RTC/BWE/Utils.hpp +39 -0
  82. package/worker/include/RTC/Consts.hpp +6 -6
  83. package/worker/include/RTC/Consumer.hpp +14 -12
  84. package/worker/include/RTC/DataConsumer.hpp +3 -3
  85. package/worker/include/RTC/DataProducer.hpp +3 -3
  86. package/worker/include/RTC/DtlsTransport.hpp +5 -2
  87. package/worker/include/RTC/ICE/IceCandidate.hpp +2 -2
  88. package/worker/include/RTC/ICE/IceServer.hpp +3 -3
  89. package/worker/include/RTC/KeyFrameRequestManager.hpp +3 -3
  90. package/worker/include/RTC/NEW_RTCP/packet/Packet.hpp +1 -1
  91. package/worker/include/RTC/NackGenerator.hpp +12 -12
  92. package/worker/include/RTC/PipeProducerStreamManager.hpp +3 -4
  93. package/worker/include/RTC/PipeTransport.hpp +18 -5
  94. package/worker/include/RTC/PlainTransport.hpp +18 -5
  95. package/worker/include/RTC/PortManager.hpp +4 -4
  96. package/worker/include/RTC/Producer.hpp +21 -12
  97. package/worker/include/RTC/ProducerStreamManager.hpp +12 -6
  98. package/worker/include/RTC/RTCP/Bye.hpp +2 -2
  99. package/worker/include/RTC/RTCP/FeedbackPsRemb.hpp +4 -4
  100. package/worker/include/RTC/RTCP/FeedbackRtpTmmb.hpp +3 -3
  101. package/worker/include/RTC/RTCP/FeedbackRtpTransport.hpp +54 -39
  102. package/worker/include/RTC/RTCP/Packet.hpp +3 -3
  103. package/worker/include/RTC/RTCP/ReceiverReport.hpp +3 -3
  104. package/worker/include/RTC/RTCP/Sdes.hpp +2 -2
  105. package/worker/include/RTC/RTCP/XR.hpp +2 -2
  106. package/worker/include/RTC/RTCP/XrDelaySinceLastRr.hpp +1 -1
  107. package/worker/include/RTC/RTP/Codecs/H264.hpp +1 -1
  108. package/worker/include/RTC/RTP/Codecs/Opus.hpp +2 -2
  109. package/worker/include/RTC/RTP/Codecs/PayloadDescriptorHandler.hpp +3 -3
  110. package/worker/include/RTC/RTP/Codecs/VP8.hpp +2 -2
  111. package/worker/include/RTC/RTP/Codecs/VP9.hpp +2 -2
  112. package/worker/include/RTC/RTP/HeaderExtensionIds.hpp +12 -12
  113. package/worker/include/RTC/RTP/Packet.hpp +13 -13
  114. package/worker/include/RTC/RTP/ProbationGenerator.hpp +1 -1
  115. package/worker/include/RTC/RTP/RetransmissionBuffer.hpp +7 -7
  116. package/worker/include/RTC/RTP/RtpStream.hpp +37 -51
  117. package/worker/include/RTC/RTP/RtpStreamRecv.hpp +59 -41
  118. package/worker/include/RTC/RTP/RtpStreamSend.hpp +20 -14
  119. package/worker/include/RTC/RTP/RtxStream.hpp +12 -13
  120. package/worker/include/RTC/RateCalculator.hpp +50 -44
  121. package/worker/include/RTC/RemoteCaptureTimeEstimator.hpp +5 -4
  122. package/worker/include/RTC/RemoteClockOffsetEstimator.hpp +11 -11
  123. package/worker/include/RTC/Router.hpp +6 -0
  124. package/worker/include/RTC/RtcLogger.hpp +1 -1
  125. package/worker/include/RTC/RtpDictionaries.hpp +10 -10
  126. package/worker/include/RTC/SCTP/association/Association.hpp +56 -28
  127. package/worker/include/RTC/SCTP/association/HeartbeatHandler.hpp +4 -4
  128. package/worker/include/RTC/SCTP/association/StateCookie.hpp +14 -14
  129. package/worker/include/RTC/SCTP/association/StreamResetHandler.hpp +38 -2
  130. package/worker/include/RTC/SCTP/association/TransmissionControlBlock.hpp +12 -12
  131. package/worker/include/RTC/SCTP/association/TransmissionControlBlockContextInterface.hpp +4 -4
  132. package/worker/include/RTC/SCTP/packet/TLV.hpp +1 -1
  133. package/worker/include/RTC/SCTP/packet/parameters/StateCookieParameter.hpp +3 -3
  134. package/worker/include/RTC/SCTP/public/AssociationInterface.hpp +4 -1
  135. package/worker/include/RTC/SCTP/public/AssociationMetrics.hpp +1 -1
  136. package/worker/include/RTC/SCTP/public/SctpOptions.hpp +12 -12
  137. package/worker/include/RTC/SCTP/public/SctpTypes.hpp +6 -6
  138. package/worker/include/RTC/SCTP/rx/ReassemblyQueue.hpp +10 -0
  139. package/worker/include/RTC/SCTP/tx/OutstandingData.hpp +15 -16
  140. package/worker/include/RTC/SCTP/tx/RetransmissionQueue.hpp +7 -8
  141. package/worker/include/RTC/SCTP/tx/RetransmissionTimeout.hpp +20 -16
  142. package/worker/include/RTC/SCTP/tx/RoundRobinSendQueue.hpp +5 -6
  143. package/worker/include/RTC/SCTP/tx/SendQueueInterface.hpp +2 -2
  144. package/worker/include/RTC/SCTP/tx/StreamScheduler.hpp +5 -5
  145. package/worker/include/RTC/SctpDictionaries.hpp +3 -3
  146. package/worker/include/RTC/Serializable.hpp +1 -1
  147. package/worker/include/RTC/SimpleProducerStreamManager.hpp +3 -4
  148. package/worker/include/RTC/SimulcastProducerStreamManager.hpp +6 -7
  149. package/worker/include/RTC/SvcProducerStreamManager.hpp +6 -5
  150. package/worker/include/RTC/TcpConnection.hpp +9 -3
  151. package/worker/include/RTC/TcpServer.hpp +3 -0
  152. package/worker/include/RTC/Transport.hpp +52 -45
  153. package/worker/include/RTC/TransportCongestionControlClient.hpp +30 -25
  154. package/worker/include/RTC/TransportCongestionControlServer.hpp +17 -17
  155. package/worker/include/RTC/TrendCalculator.hpp +5 -5
  156. package/worker/include/RTC/UdpSocket.hpp +11 -2
  157. package/worker/include/RTC/WebRtcServer.hpp +20 -5
  158. package/worker/include/RTC/WebRtcTransport.hpp +35 -10
  159. package/worker/include/Settings.hpp +4 -2
  160. package/worker/include/Shared.hpp +5 -20
  161. package/worker/include/SharedInterface.hpp +6 -28
  162. package/worker/include/Utils.hpp +40 -21
  163. package/worker/include/common.hpp +4 -0
  164. package/worker/include/handles/BackoffTimerHandle.hpp +4 -4
  165. package/worker/include/handles/BackoffTimerHandleInterface.hpp +5 -5
  166. package/worker/include/handles/TcpConnectionHandle.hpp +6 -6
  167. package/worker/include/handles/TcpServerHandle.hpp +1 -1
  168. package/worker/include/handles/TimerHandle.hpp +15 -9
  169. package/worker/include/handles/TimerHandleInterface.hpp +10 -4
  170. package/worker/include/handles/UdpSocketHandle.hpp +3 -3
  171. package/worker/include/handles/UnixStreamSocketHandle.hpp +2 -2
  172. package/worker/meson.build +87 -36
  173. package/worker/meson_options.txt +1 -0
  174. package/worker/mocks/include/MockShared.hpp +24 -23
  175. package/worker/mocks/include/RTC/SCTP/association/MockTransmissionControlBlockContext.hpp +51 -27
  176. package/worker/mocks/include/RTC/SCTP/tx/MockSendQueue.hpp +11 -11
  177. package/worker/mocks/include/handles/MockBackoffTimerHandle.hpp +10 -10
  178. package/worker/mocks/include/handles/MockTimerHandle.hpp +60 -22
  179. package/worker/mocks/src/MockShared.cpp +51 -5
  180. package/worker/mocks/src/handles/MockBackoffTimerHandle.cpp +29 -15
  181. package/worker/mocks/src/handles/MockTimerHandle.cpp +117 -0
  182. package/worker/scripts/clang-scripts.mjs +2 -2
  183. package/worker/src/Channel/ChannelNotification.cpp +5 -6
  184. package/worker/src/Channel/ChannelSocket.cpp +20 -2
  185. package/worker/src/DepLibUV.cpp +4 -5
  186. package/worker/src/RTC/ActiveSpeakerObserver.cpp +59 -47
  187. package/worker/src/RTC/AudioLevelObserver.cpp +7 -7
  188. package/worker/src/RTC/BWE/AimdRateControl.cpp +468 -0
  189. package/worker/src/RTC/BWE/BweTypes.cpp +61 -0
  190. package/worker/src/RTC/BWE/DelayBasedBwe.cpp +275 -0
  191. package/worker/src/RTC/BWE/FeedbackAdapter.cpp +148 -0
  192. package/worker/src/RTC/BWE/InterArrivalDelta.cpp +181 -0
  193. package/worker/src/RTC/BWE/LinkCapacityEstimator.cpp +133 -0
  194. package/worker/src/RTC/BWE/LossBasedController.cpp +1046 -0
  195. package/worker/src/RTC/BWE/RobustThroughputEstimator.cpp +245 -0
  196. package/worker/src/RTC/BWE/SendPacketHistory.cpp +184 -0
  197. package/worker/src/RTC/BWE/TargetRateController.cpp +435 -0
  198. package/worker/src/RTC/BWE/TrendlineEstimator.cpp +311 -0
  199. package/worker/src/RTC/BWE/Utils.cpp +54 -0
  200. package/worker/src/RTC/Consumer.cpp +48 -35
  201. package/worker/src/RTC/DataConsumer.cpp +4 -4
  202. package/worker/src/RTC/DataProducer.cpp +3 -3
  203. package/worker/src/RTC/DirectTransport.cpp +2 -2
  204. package/worker/src/RTC/DtlsTransport.cpp +14 -11
  205. package/worker/src/RTC/ICE/IceServer.cpp +2 -2
  206. package/worker/src/RTC/KeyFrameRequestManager.cpp +16 -12
  207. package/worker/src/RTC/NackGenerator.cpp +12 -12
  208. package/worker/src/RTC/PipeProducerStreamManager.cpp +6 -6
  209. package/worker/src/RTC/PipeTransport.cpp +16 -13
  210. package/worker/src/RTC/PlainTransport.cpp +25 -16
  211. package/worker/src/RTC/Producer.cpp +50 -36
  212. package/worker/src/RTC/RTCP/FeedbackPsRemb.cpp +13 -6
  213. package/worker/src/RTC/RTCP/FeedbackRtpTmmb.cpp +14 -5
  214. package/worker/src/RTC/RTCP/FeedbackRtpTransport.cpp +76 -50
  215. package/worker/src/RTC/RTP/Codecs/VP8.cpp +1 -1
  216. package/worker/src/RTC/RTP/Codecs/VP9.cpp +4 -4
  217. package/worker/src/RTC/RTP/Packet.cpp +9 -10
  218. package/worker/src/RTC/RTP/RetransmissionBuffer.cpp +6 -6
  219. package/worker/src/RTC/RTP/RtpStream.cpp +19 -19
  220. package/worker/src/RTC/RTP/RtpStreamRecv.cpp +146 -81
  221. package/worker/src/RTC/RTP/RtpStreamSend.cpp +49 -44
  222. package/worker/src/RTC/RTP/RtxStream.cpp +7 -10
  223. package/worker/src/RTC/RateCalculator.cpp +99 -136
  224. package/worker/src/RTC/RemoteCaptureTimeEstimator.cpp +13 -13
  225. package/worker/src/RTC/RemoteClockOffsetEstimator.cpp +18 -17
  226. package/worker/src/RTC/Router.cpp +18 -1
  227. package/worker/src/RTC/RtpDictionaries/RtpEncodingParameters.cpp +5 -2
  228. package/worker/src/RTC/SCTP/association/Association.cpp +146 -103
  229. package/worker/src/RTC/SCTP/association/AssociationListenerDeferrer.cpp +4 -4
  230. package/worker/src/RTC/SCTP/association/Capabilities.cpp +1 -1
  231. package/worker/src/RTC/SCTP/association/HeartbeatHandler.cpp +25 -18
  232. package/worker/src/RTC/SCTP/association/StateCookie.cpp +7 -6
  233. package/worker/src/RTC/SCTP/association/StreamResetHandler.cpp +122 -14
  234. package/worker/src/RTC/SCTP/association/TransmissionControlBlock.cpp +28 -26
  235. package/worker/src/RTC/SCTP/packet/chunks/SackChunk.cpp +0 -1
  236. package/worker/src/RTC/SCTP/packet/parameters/StateCookieParameter.cpp +2 -2
  237. package/worker/src/RTC/SCTP/public/AssociationMetrics.cpp +2 -2
  238. package/worker/src/RTC/SCTP/public/SctpOptions.cpp +13 -13
  239. package/worker/src/RTC/SCTP/rx/DataTracker.cpp +1 -1
  240. package/worker/src/RTC/SCTP/rx/ReassemblyQueue.cpp +5 -5
  241. package/worker/src/RTC/SCTP/tx/OutstandingData.cpp +15 -15
  242. package/worker/src/RTC/SCTP/tx/RetransmissionQueue.cpp +15 -15
  243. package/worker/src/RTC/SCTP/tx/RetransmissionTimeout.cpp +29 -26
  244. package/worker/src/RTC/SCTP/tx/RoundRobinSendQueue.cpp +12 -11
  245. package/worker/src/RTC/SCTP/tx/StreamScheduler.cpp +4 -4
  246. package/worker/src/RTC/SctpDictionaries/SctpStreamParameters.cpp +2 -2
  247. package/worker/src/RTC/SimpleProducerStreamManager.cpp +10 -10
  248. package/worker/src/RTC/SimulcastProducerStreamManager.cpp +39 -37
  249. package/worker/src/RTC/SvcProducerStreamManager.cpp +39 -25
  250. package/worker/src/RTC/TcpConnection.cpp +9 -3
  251. package/worker/src/RTC/TcpServer.cpp +7 -2
  252. package/worker/src/RTC/Transport.cpp +266 -307
  253. package/worker/src/RTC/TransportCongestionControlClient.cpp +78 -71
  254. package/worker/src/RTC/TransportCongestionControlServer.cpp +38 -40
  255. package/worker/src/RTC/TrendCalculator.cpp +3 -3
  256. package/worker/src/RTC/UdpSocket.cpp +14 -3
  257. package/worker/src/RTC/WebRtcServer.cpp +20 -14
  258. package/worker/src/RTC/WebRtcTransport.cpp +35 -30
  259. package/worker/src/Settings.cpp +10 -0
  260. package/worker/src/Shared.cpp +2 -2
  261. package/worker/src/Utils/Crypto.cpp +2 -2
  262. package/worker/src/Worker.cpp +5 -1
  263. package/worker/src/handles/BackoffTimerHandle.cpp +24 -11
  264. package/worker/src/handles/TimerHandle.cpp +74 -25
  265. package/worker/src/lib.cpp +6 -0
  266. package/worker/tasks.py +197 -45
  267. package/worker/test/include/RTC/BWE/helpers/LinkSimulator.hpp +117 -0
  268. package/worker/test/src/RTC/BWE/TestAimdRateControl.cpp +400 -0
  269. package/worker/test/src/RTC/BWE/TestDelayBasedBwe.cpp +568 -0
  270. package/worker/test/src/RTC/BWE/TestFeedbackAdapter.cpp +546 -0
  271. package/worker/test/src/RTC/BWE/TestInterArrivalDelta.cpp +301 -0
  272. package/worker/test/src/RTC/BWE/TestLinkCapacityEstimator.cpp +115 -0
  273. package/worker/test/src/RTC/BWE/TestLossBasedController.cpp +1199 -0
  274. package/worker/test/src/RTC/BWE/TestRobustThroughputEstimator.cpp +433 -0
  275. package/worker/test/src/RTC/BWE/TestSendPacketHistory.cpp +259 -0
  276. package/worker/test/src/RTC/BWE/TestTargetRateController.cpp +337 -0
  277. package/worker/test/src/RTC/BWE/TestTrendlineEstimator.cpp +182 -0
  278. package/worker/test/src/RTC/BWE/TestUtils.cpp +61 -0
  279. package/worker/test/src/RTC/BWE/helpers/LinkSimulator.cpp +171 -0
  280. package/worker/test/src/RTC/RTCP/TestFeedbackPsRemb.cpp +1 -1
  281. package/worker/test/src/RTC/RTCP/TestFeedbackRtpTmmb.cpp +1 -1
  282. package/worker/test/src/RTC/RTCP/TestFeedbackRtpTransport.cpp +246 -208
  283. package/worker/test/src/RTC/RTP/TestPacket.cpp +11 -11
  284. package/worker/test/src/RTC/RTP/TestRetransmissionBuffer.cpp +12 -12
  285. package/worker/test/src/RTC/RTP/TestRtpStreamRecv.cpp +23 -16
  286. package/worker/test/src/RTC/RTP/TestRtpStreamSend.cpp +13 -13
  287. package/worker/test/src/RTC/SCTP/association/TestAssociation.cpp +27 -22
  288. package/worker/test/src/RTC/SCTP/association/TestHeartbeatHandler.cpp +21 -15
  289. package/worker/test/src/RTC/SCTP/association/TestStateCookie.cpp +6 -6
  290. package/worker/test/src/RTC/SCTP/association/TestStreamResetHandler.cpp +128 -13
  291. package/worker/test/src/RTC/SCTP/association/TestTransmissionControlBlock.cpp +3 -3
  292. package/worker/test/src/RTC/SCTP/packet/chunks/TestForwardTsnChunk.cpp +2 -2
  293. package/worker/test/src/RTC/SCTP/packet/chunks/TestIForwardTsnChunk.cpp +23 -23
  294. package/worker/test/src/RTC/SCTP/packet/chunks/TestInitChunk.cpp +1 -1
  295. package/worker/test/src/RTC/SCTP/packet/chunks/TestSackChunk.cpp +1 -1
  296. package/worker/test/src/RTC/SCTP/packet/parameters/TestIPv6AddressParameter.cpp +1 -1
  297. package/worker/test/src/RTC/SCTP/rx/TestDataTracker.cpp +37 -37
  298. package/worker/test/src/RTC/SCTP/rx/TestInterleavedReassemblyStreams.cpp +10 -10
  299. package/worker/test/src/RTC/SCTP/rx/TestReassemblyQueue.cpp +1 -1
  300. package/worker/test/src/RTC/SCTP/tx/TestOutstandingData.cpp +93 -93
  301. package/worker/test/src/RTC/SCTP/tx/TestRetransmissionQueue.cpp +247 -247
  302. package/worker/test/src/RTC/SCTP/tx/TestRetransmissionTimeout.cpp +100 -80
  303. package/worker/test/src/RTC/SCTP/tx/TestRoundRobinSendQueue.cpp +163 -163
  304. package/worker/test/src/RTC/SCTP/tx/TestStreamScheduler.cpp +76 -77
  305. package/worker/test/src/RTC/TestConsumer.cpp +8 -3
  306. package/worker/test/src/RTC/TestKeyFrameRequestManager.cpp +3 -3
  307. package/worker/test/src/RTC/TestNackGenerator.cpp +3 -3
  308. package/worker/test/src/RTC/TestPipeProducerStreamManager.cpp +5 -5
  309. package/worker/test/src/RTC/TestRateCalculator.cpp +110 -14
  310. package/worker/test/src/RTC/TestRemoteCaptureTimeEstimator.cpp +22 -17
  311. package/worker/test/src/RTC/TestRemoteClockOffsetEstimator.cpp +87 -85
  312. package/worker/test/src/RTC/TestSimpleProducerStreamManager.cpp +54 -47
  313. package/worker/test/src/RTC/TestSimulcastProducerStreamManager.cpp +83 -69
  314. package/worker/test/src/RTC/TestSvcProducerStreamManager.cpp +76 -65
  315. package/worker/test/src/RTC/TestTransportCongestionControlServer.cpp +72 -66
  316. package/worker/test/src/RTC/TestTransportTuple.cpp +19 -4
  317. package/worker/test/src/TestDepLibUV.cpp +19 -0
  318. package/worker/test/src/Utils/TestTime.cpp +57 -26
  319. package/worker/test/src/testHelpers.cpp +1 -1
  320. package/worker/test/src/tests.cpp +6 -0
  321. package/worker/include/RTC/SenderBandwidthEstimator.hpp +0 -118
  322. package/worker/src/RTC/SenderBandwidthEstimator.cpp +0 -269
@@ -0,0 +1,94 @@
1
+ #ifndef MS_RTC_BWE_ROBUST_THROUGHPUT_ESTIMATOR_HPP
2
+ #define MS_RTC_BWE_ROBUST_THROUGHPUT_ESTIMATOR_HPP
3
+
4
+ #include "common.hpp"
5
+ #include "RTC/BWE/AcknowledgedBitrateEstimatorInterface.hpp"
6
+ #include "RTC/BWE/BweTypes.hpp"
7
+ #include <deque>
8
+
9
+ namespace RTC
10
+ {
11
+ namespace BWE
12
+ {
13
+ /**
14
+ * Measures the acknowledged bitrate over a sliding window of the packets the
15
+ * receiver reported.
16
+ *
17
+ * The estimate is the smaller of the send rate and the receive rate observed
18
+ * over that window, so that it never claims more than what was actually put
19
+ * on the wire. The largest gap between arrivals is replaced by the second
20
+ * largest one before dividing, which is what keeps a short pause followed by
21
+ * a burst of delayed packets from collapsing the estimate.
22
+ */
23
+ class RobustThroughputEstimator : public AcknowledgedBitrateEstimatorInterface
24
+ {
25
+ public:
26
+ struct RobustThroughputEstimatorOptions
27
+ {
28
+ /**
29
+ * Smallest number of packets the window holds.
30
+ */
31
+ size_t windowPackets{ 20 };
32
+ /**
33
+ * Largest number of packets the window holds, so that a high bitrate
34
+ * doesn't make it grow without bound.
35
+ */
36
+ size_t maxWindowPackets{ 500 };
37
+ /**
38
+ * Smallest duration the window covers, so that a low bitrate still gets
39
+ * enough packets to measure.
40
+ */
41
+ int64_t minWindowDurationUs{ 1000 * 1000 };
42
+ /**
43
+ * Largest duration the window covers, so that very old packets don't
44
+ * weigh on the estimate after sending was paused.
45
+ */
46
+ int64_t maxWindowDurationUs{ 5 * 1000 * 1000 };
47
+ /**
48
+ * Number of packets the window needs before it produces an estimate.
49
+ */
50
+ size_t requiredPackets{ 10 };
51
+ /**
52
+ * Weight given to the data that was sent but is not covered by any
53
+ * feedback.
54
+ *
55
+ * @remarks
56
+ * - Set it to 0 when audio is not included in the allocation, and to 1
57
+ * when it is included in the allocation but not in the estimation.
58
+ * Its value is irrelevant once every packet is tracked.
59
+ */
60
+ double unackedWeight{ 1.0 };
61
+ };
62
+
63
+ public:
64
+ RobustThroughputEstimator();
65
+
66
+ explicit RobustThroughputEstimator(RobustThroughputEstimatorOptions options);
67
+
68
+ void IncomingPacketFeedbackVector(const std::vector<Types::PacketResult>& packetResults) override;
69
+
70
+ std::optional<int64_t> GetBitrate() const override;
71
+
72
+ void SetAlr(bool /*inAlr*/) override
73
+ {
74
+ }
75
+
76
+ void SetAlrEndedTimeUs(int64_t /*alrEndedTimeUs*/) override
77
+ {
78
+ }
79
+
80
+ private:
81
+ /**
82
+ * Whether the oldest packet of the window no longer belongs to it.
83
+ */
84
+ bool IsFirstPacketOutsideWindow() const;
85
+
86
+ private:
87
+ const RobustThroughputEstimatorOptions options;
88
+ std::deque<Types::PacketResult> window;
89
+ std::optional<int64_t> latestDiscardedSendTimeUs;
90
+ };
91
+ } // namespace BWE
92
+ } // namespace RTC
93
+
94
+ #endif
@@ -0,0 +1,137 @@
1
+ #ifndef MS_RTC_BWE_SEND_PACKET_HISTORY_HPP
2
+ #define MS_RTC_BWE_SEND_PACKET_HISTORY_HPP
3
+
4
+ #include "common.hpp"
5
+ #include "RTC/BWE/BweTypes.hpp"
6
+ #include <map>
7
+ #include <tuple>
8
+
9
+ namespace RTC
10
+ {
11
+ namespace BWE
12
+ {
13
+ /**
14
+ * Holds every packet that was sent until a feedback reports on it.
15
+ *
16
+ * Packets are indexed by a sequence number of its own, handed out by this
17
+ * history, so that nothing about how a feedback identifies a packet leaks
18
+ * in here. Each packet also keeps its SSRC and RTP sequence number, which
19
+ * is what allows a feedback that identifies packets that way to be resolved
20
+ * against this very same history.
21
+ *
22
+ * It also tracks how many bytes are in flight, which are those that were
23
+ * sent and no feedback has reported on yet.
24
+ *
25
+ * @see https://datatracker.ietf.org/doc/html/draft-holmer-rmcat-transport-wide-cc-extensions
26
+ * @see https://datatracker.ietf.org/doc/html/rfc8888
27
+ */
28
+ class SendPacketHistory
29
+ {
30
+ public:
31
+ struct SendPacketHistoryOptions
32
+ {
33
+ /**
34
+ * Time a packet is kept for, beyond which a feedback reporting on it
35
+ * arrives too late to be of any use.
36
+ */
37
+ int64_t windowDurationUs{ 60 * 1000 * 1000 };
38
+ };
39
+
40
+ private:
41
+ struct SentPacketEntry
42
+ {
43
+ Types::SentPacket sentPacket;
44
+ uint32_t ssrc{ 0 };
45
+ uint16_t seq{ 0 };
46
+ };
47
+
48
+ struct SsrcAndSeq
49
+ {
50
+ bool operator<(const SsrcAndSeq& other) const
51
+ {
52
+ return std::tie(this->ssrc, this->seq) < std::tie(other.ssrc, other.seq);
53
+ }
54
+
55
+ uint32_t ssrc{ 0 };
56
+ uint16_t seq{ 0 };
57
+ };
58
+
59
+ public:
60
+ SendPacketHistory();
61
+
62
+ explicit SendPacketHistory(SendPacketHistoryOptions options);
63
+
64
+ /**
65
+ * Take note of a packet that has just been sent.
66
+ *
67
+ * @param size - Size of the packet including overhead up to the IP layer
68
+ * (bytes).
69
+ * @param audio - Whether it's an audio packet. False for video, padding
70
+ * and RTX.
71
+ * @returns The sequence number given to the packet, which is what a
72
+ * feedback has to be resolved into.
73
+ */
74
+ int64_t AddPacket(uint32_t ssrc, uint16_t seq, size_t size, bool audio, int64_t sentAtUs);
75
+
76
+ /**
77
+ * Resolve what a feedback reported about a single packet.
78
+ *
79
+ * @param received - Whether the feedback reported the packet as received.
80
+ * @returns The packet as it was sent, or no value if this history doesn't
81
+ * hold it anymore.
82
+ *
83
+ * @remarks
84
+ * - Every packet up to the given one stops counting as in flight, since
85
+ * the feedback settles whether it arrived or not.
86
+ * - A packet reported as received is dropped, while one reported as lost
87
+ * is kept, since a later feedback may still report it as received.
88
+ */
89
+ std::optional<Types::SentPacket> RetrievePacket(int64_t sequenceNumber, bool received);
90
+
91
+ /**
92
+ * Same, for a feedback that identifies the packet by its SSRC and RTP
93
+ * sequence number rather than by the sequence number given to it.
94
+ */
95
+ std::optional<Types::SentPacket> RetrievePacket(uint32_t ssrc, uint16_t seq, bool received);
96
+
97
+ /**
98
+ * Sequence number this history gave to the latest packet added, or no
99
+ * value if no packet was ever added.
100
+ *
101
+ * @remarks
102
+ * - It's the sequence number of this history, not the RTP one, so it
103
+ * never wraps around. It's also the highest a feedback carrying only
104
+ * the lowest bits of it can possibly stand for.
105
+ */
106
+ std::optional<int64_t> GetLastSequenceNumber() const;
107
+
108
+ /**
109
+ * Bytes that were sent and no feedback has reported on yet.
110
+ */
111
+ size_t GetOutstandingBytes() const
112
+ {
113
+ return this->outstandingBytes;
114
+ }
115
+
116
+ private:
117
+ void RemoveOutstandingBytes(const SentPacketEntry& entry);
118
+
119
+ /**
120
+ * Drop the mapping of a packet, unless the same SSRC and RTP sequence
121
+ * number were sent again afterwards and hence took it over.
122
+ */
123
+ void RemoveSsrcAndSeq(const SentPacketEntry& entry);
124
+
125
+ private:
126
+ const SendPacketHistoryOptions options;
127
+ std::map<int64_t, SentPacketEntry> history;
128
+ std::map<SsrcAndSeq, int64_t> ssrcAndSeqToSequenceNumber;
129
+ int64_t nextSequenceNumber{ 0 };
130
+ // Sequence numbers are never negative, so this sits below all of them.
131
+ int64_t lastAckSequenceNumber{ -1 };
132
+ size_t outstandingBytes{ 0 };
133
+ };
134
+ } // namespace BWE
135
+ } // namespace RTC
136
+
137
+ #endif
@@ -0,0 +1,306 @@
1
+ #ifndef MS_RTC_BWE_TARGET_RATE_CONTROLLER_HPP
2
+ #define MS_RTC_BWE_TARGET_RATE_CONTROLLER_HPP
3
+
4
+ #include "common.hpp"
5
+ #include "RTC/BWE/BweTypes.hpp"
6
+ #include "RTC/BWE/LossBasedController.hpp"
7
+ #include <deque>
8
+ #include <vector>
9
+
10
+ namespace RTC
11
+ {
12
+ namespace BWE
13
+ {
14
+ /**
15
+ * Holds the target bitrate and every bound that applies to it.
16
+ *
17
+ * The delay based path, the receiver of the media and the application each
18
+ * produce a limit rather than a bitrate, so something has to hold the value
19
+ * they bound and decide how it moves within them. That is this class, and it
20
+ * also owns the two inputs that move the target down on their own: a round
21
+ * trip time long enough to mean that the queues of the network are full, and
22
+ * the loss the receiver reports.
23
+ *
24
+ * How loss moves the target depends on how much is known about it. Until the
25
+ * loss controller has seen enough feedback to tell congestion from the loss a
26
+ * link has by itself, this class applies rules of its own on the fraction of
27
+ * packets reported as lost: increase while it stays under 2%, leave the target
28
+ * alone up to 10%, and decrease it above that. They are coarse on purpose, and
29
+ * their weakness is precisely what they cannot tell apart, so on a link with
30
+ * steady loss above 2% they hold the target still and above 10% they drive it
31
+ * to the minimum. As soon as the loss controller is ready, its estimate
32
+ * replaces them.
33
+ *
34
+ * @remarks
35
+ * - This class is a port of the SendSideBandwidthEstimation class in
36
+ * libwebrtc (renamed to a better name).
37
+ */
38
+ class TargetRateController
39
+ {
40
+ public:
41
+ struct TargetRateControllerOptions
42
+ {
43
+ /**
44
+ * Reported loss under which the bitrate is increased.
45
+ */
46
+ double lowLossThreshold{ 0.02 };
47
+ /**
48
+ * Reported loss over which the bitrate is decreased.
49
+ */
50
+ double highLossThreshold{ 0.1 };
51
+ /**
52
+ * Bitrate under which loss is ignored altogether, since at a low enough
53
+ * bitrate loss is unlikely to be caused by the traffic itself.
54
+ */
55
+ int64_t bitrateThreshold{ 0 };
56
+ /**
57
+ * Whether the bound asked for by the receiver takes part in how the
58
+ * target moves, rather than only capping the value this controller
59
+ * hands out.
60
+ */
61
+ bool disableReceiverLimitCapsOnly{ false };
62
+ /**
63
+ * Round trip time over which the bitrate is dropped regardless of loss.
64
+ * A round trip time this long means the queues of the network are full.
65
+ */
66
+ int64_t maxRttUs{ 3 * 1000 * 1000 };
67
+ /**
68
+ * Fraction of the target the bitrate is dropped to on each of those
69
+ * drops.
70
+ */
71
+ double rttBackoffDropFraction{ 0.8 };
72
+ /**
73
+ * How often the bitrate may be dropped due to the round trip time.
74
+ */
75
+ int64_t rttBackoffDropIntervalUs{ 1000 * 1000 };
76
+ /**
77
+ * Bitrate the round trip time backoff never drops below.
78
+ */
79
+ int64_t rttBackoffBitrateFloor{ 5000 };
80
+ };
81
+
82
+ private:
83
+ /**
84
+ * Drops the bitrate when the round trip time stays long enough to mean
85
+ * that the queues of the network are full, which is a congestion signal
86
+ * that loss and delay may both miss.
87
+ */
88
+ struct RttBackoff
89
+ {
90
+ /**
91
+ * Round trip time corrected for the sender being idle. Without the
92
+ * correction a sender that stops transmitting would keep reporting the
93
+ * last measured value forever.
94
+ */
95
+ int64_t GetCorrectedRttUs() const
96
+ {
97
+ int64_t timeoutCorrectionUs{ 0 };
98
+
99
+ // While either instant is missing there is nothing to correct: no
100
+ // feedback has arrived, or nothing that gets feedback has been sent.
101
+ if (this->lastPacketSentAtUs.has_value() && this->lastPropagationRttAtUs.has_value())
102
+ {
103
+ timeoutCorrectionUs = std::max<int64_t>(
104
+ this->lastPacketSentAtUs.value() - this->lastPropagationRttAtUs.value(), 0);
105
+ }
106
+
107
+ return timeoutCorrectionUs + this->lastPropagationRttUs;
108
+ }
109
+
110
+ std::optional<int64_t> lastPropagationRttAtUs;
111
+ std::optional<int64_t> lastPacketSentAtUs;
112
+ int64_t lastPropagationRttUs{ 0 };
113
+ };
114
+
115
+ public:
116
+ TargetRateController();
117
+
118
+ explicit TargetRateController(TargetRateControllerOptions options);
119
+
120
+ /**
121
+ * Bitrate this controller may produce, already within every bound.
122
+ */
123
+ int64_t GetTargetBitrate() const;
124
+
125
+ /**
126
+ * Latest fraction of lost packets reported by the receiver, in 1/256 units.
127
+ */
128
+ uint8_t GetFractionLost() const
129
+ {
130
+ return this->lastFractionLost;
131
+ }
132
+
133
+ int64_t GetRttUs() const
134
+ {
135
+ return this->lastRttUs;
136
+ }
137
+
138
+ /**
139
+ * Whether the round trip time is long enough to be taken as congestion on
140
+ * its own.
141
+ */
142
+ bool IsRttAboveLimit() const
143
+ {
144
+ return this->rttBackoff.GetCorrectedRttUs() > this->options.maxRttUs;
145
+ }
146
+
147
+ /**
148
+ * Forget everything learnt about the network, which is what a transport
149
+ * that is not going through the same path anymore calls for.
150
+ *
151
+ * @remarks
152
+ * - The bounds set by the application are dropped too, so they have to be
153
+ * set again afterwards.
154
+ */
155
+ void OnRouteChange();
156
+
157
+ /**
158
+ * Bounds set by the application.
159
+ *
160
+ * @remarks
161
+ * - A max bitrate of zero means no limit.
162
+ */
163
+ void SetBitrateLimits(int64_t minBitrate, int64_t maxBitrate);
164
+
165
+ /**
166
+ * Place the target at the given bitrate, dropping the bound that could
167
+ * hold it back and the history that limits how fast it grows.
168
+ */
169
+ void SetSendBitrate(int64_t bitrate);
170
+
171
+ /**
172
+ * Upper bound produced by the delay based path.
173
+ */
174
+ void SetDelayBasedEstimate(int64_t bitrate);
175
+
176
+ /**
177
+ * Upper bound asked for by the receiver of the media, which reaches us as
178
+ * a REMB message.
179
+ *
180
+ * @remarks
181
+ * - A bitrate of zero means no limit.
182
+ */
183
+ void SetReceiverEstimate(int64_t bitrate);
184
+
185
+ /**
186
+ * Feed a loss report of the receiver.
187
+ *
188
+ * @remarks
189
+ * - Reports are accumulated until they cover enough packets, so most calls
190
+ * change nothing.
191
+ */
192
+ void UpdatePacketsLost(int64_t lostPackets, int64_t totalPackets, int64_t nowUs);
193
+
194
+ void UpdateRtt(int64_t rttUs);
195
+
196
+ /**
197
+ * Feed the round trip time the round trip time backoff watches, which is
198
+ * the one measured by the transport feedback rather than by RTCP.
199
+ */
200
+ void UpdatePropagationRtt(int64_t propagationRttUs, int64_t nowUs);
201
+
202
+ /**
203
+ * Tell that a packet that will be reported back has just been sent.
204
+ */
205
+ void OnPacketSent(int64_t sentAtUs);
206
+
207
+ /**
208
+ * Reconsider the target. Called after every loss report and periodically,
209
+ * since the increase depends on how much time has elapsed.
210
+ */
211
+ void Update(int64_t nowUs);
212
+
213
+ /**
214
+ * Bitrate the network is known to be delivering.
215
+ */
216
+ void SetAcknowledgedBitrate(int64_t acknowledgedBitrate);
217
+
218
+ /**
219
+ * Feed the results of a feedback message to the loss controller, which is
220
+ * the only thing in here that looks at packets one by one.
221
+ *
222
+ * @param inAlr - Whether the sender is not sending enough to fill the link.
223
+ */
224
+ void UpdateLossBasedController(
225
+ const std::vector<Types::PacketResult>& packetResults, bool inAlr, int64_t nowUs);
226
+
227
+ /**
228
+ * What the loss controller is doing with the target, which tells whether
229
+ * probing makes sense.
230
+ */
231
+ LossBasedController::State GetLossBasedState() const
232
+ {
233
+ return this->lossBasedState;
234
+ }
235
+
236
+ private:
237
+ /**
238
+ * Whether the target is still allowed to just follow the bounds because
239
+ * not enough is known about the network yet.
240
+ */
241
+ bool IsInStartPhase(int64_t nowUs) const;
242
+
243
+ /**
244
+ * Lowest bitrate the target has had during the last second, which is what
245
+ * the increase grows from.
246
+ */
247
+ void UpdateMinBitrateHistory(int64_t nowUs);
248
+
249
+ /**
250
+ * Upper bound of every kind that applies to the target.
251
+ */
252
+ int64_t GetUpperLimit() const;
253
+
254
+ /**
255
+ * Set the target to the given bitrate, keeping it within its bounds.
256
+ */
257
+ void SetTargetBitrate(int64_t bitrate);
258
+
259
+ /**
260
+ * Reapply the bounds to the current target, which is needed when one of
261
+ * them changes.
262
+ */
263
+ void ApplyTargetLimits();
264
+
265
+ private:
266
+ // Passed by argument.
267
+ const TargetRateControllerOptions options;
268
+ // Others.
269
+ // Tells congestion from the loss the link has by itself, and takes over
270
+ // the target as soon as it has enough observations.
271
+ LossBasedController lossBasedController;
272
+ LossBasedController::State lossBasedState{ LossBasedController::State::DELAY_BASED_ESTIMATE };
273
+ struct RttBackoff rttBackoff;
274
+ // Lowest target of the last second, as a sliding window minimum of
275
+ // (instant in us, bitrate in bps) pairs.
276
+ std::deque<std::pair<int64_t, int64_t>> minBitrateHistory;
277
+ // Loss reports too small to tell anything on their own, accumulated until
278
+ // they cover enough packets.
279
+ int64_t lostPacketsSinceLastLossUpdate{ 0 };
280
+ int64_t expectedPacketsSinceLastLossUpdate{ 0 };
281
+ int64_t currentTarget{ 0 };
282
+ int64_t minBitrateConfigured{ 0 };
283
+ int64_t maxBitrateConfigured{ 0 };
284
+ // Instant of the first loss report ever received, which is when the start
285
+ // phase begins.
286
+ std::optional<int64_t> firstLossReportAtUs;
287
+ // Instant of the latest loss report that covered enough packets to tell
288
+ // something.
289
+ std::optional<int64_t> lastLossReportAtUs;
290
+ // Instant at which the target was last decreased, by the loss rules or by
291
+ // the round trip time backoff.
292
+ std::optional<int64_t> lastDecreaseAtUs;
293
+ // Whether the target has already been decreased for the loss currently
294
+ // reported, so that a same report doesn't decrease it twice.
295
+ bool hasDecreasedSinceLastFractionLoss{ false };
296
+ uint8_t lastFractionLost{ 0 };
297
+ int64_t lastRttUs{ 0 };
298
+ // Upper bound asked for by the receiver of the media.
299
+ int64_t receiverLimit{ Types::BitrateInfinite };
300
+ // Upper bound produced by the delay based path.
301
+ int64_t delayBasedLimit{ Types::BitrateInfinite };
302
+ };
303
+ } // namespace BWE
304
+ } // namespace RTC
305
+
306
+ #endif
@@ -0,0 +1,143 @@
1
+ #ifndef MS_RTC_BWE_TRENDLINE_ESTIMATOR_HPP
2
+ #define MS_RTC_BWE_TRENDLINE_ESTIMATOR_HPP
3
+
4
+ #include "common.hpp"
5
+ #include "RTC/BWE/BweTypes.hpp"
6
+ #include <deque>
7
+
8
+ namespace RTC
9
+ {
10
+ namespace BWE
11
+ {
12
+ /**
13
+ * Detects network congestion from the delay between groups of packets,
14
+ * before any packet is lost.
15
+ *
16
+ * For each pair of consecutive groups it accumulates `arrivalDelta -
17
+ * sendDelta`, smooths it, and fits a least squares line over the last
18
+ * `WindowSize` samples. The slope of that line approximates
19
+ * `(sendRate - capacity) / capacity`, so a positive slope means that queues
20
+ * are filling up somewhere in the network.
21
+ *
22
+ * That slope is compared against a threshold that adapts itself to the
23
+ * observed noise, and overuse is only declared once the condition persists,
24
+ * which is what prevents an isolated jitter spike from bringing the estimate
25
+ * down.
26
+ */
27
+ class TrendlineEstimator
28
+ {
29
+ public:
30
+ struct TrendlineEstimatorOptions
31
+ {
32
+ /**
33
+ * Number of samples of the least squares regression window. A shorter
34
+ * window reacts sooner at the cost of being noisier.
35
+ *
36
+ * @remarks
37
+ * - It must be at least 2, since a line cannot be fitted to a single
38
+ * point.
39
+ */
40
+ size_t windowSize{ 20 };
41
+ /**
42
+ * Whether the samples of the window are kept sorted by arrival time.
43
+ * Feedback should already give them in order, so this is a safety net
44
+ * against a source that doesn't.
45
+ */
46
+ bool enableSort{ false };
47
+ /**
48
+ * Whether the slope is capped by the one that the least delayed packets
49
+ * of both ends of the window describe, which keeps a burst of delayed
50
+ * packets in the middle from being read as a growing queue.
51
+ */
52
+ bool enableCap{ false };
53
+ /**
54
+ * How many packets of the beginning of the window that cap looks at.
55
+ */
56
+ size_t beginningPackets{ 7 };
57
+ /**
58
+ * How many packets of the end of the window that cap looks at.
59
+ */
60
+ size_t endPackets{ 7 };
61
+ /**
62
+ * Slack added to that cap, so that a slope barely above it is not
63
+ * capped.
64
+ */
65
+ double capUncertainty{ 0.0 };
66
+ };
67
+
68
+ private:
69
+ struct PacketTiming
70
+ {
71
+ double arrivalTimeUs;
72
+ double smoothedDelayUs;
73
+ /**
74
+ * Accumulated delay before it's smoothed, which is what the slope cap
75
+ * looks at.
76
+ */
77
+ double rawDelayUs;
78
+ };
79
+
80
+ public:
81
+ TrendlineEstimator();
82
+
83
+ explicit TrendlineEstimator(TrendlineEstimatorOptions options);
84
+
85
+ /**
86
+ * Feed the deltas between two consecutive groups of packets.
87
+ *
88
+ * @param sendDeltaUs - Time elapsed between the send times of both groups.
89
+ * @param arrivalDeltaUs - Time elapsed between the arrival times of both
90
+ * groups.
91
+ * @param arrivalTimeUs - Arrival time of the latest group, in the remote
92
+ * clock reference.
93
+ */
94
+ void Update(int64_t sendDeltaUs, int64_t arrivalDeltaUs, int64_t arrivalTimeUs);
95
+
96
+ /**
97
+ * Current hypothesis about how the network is behaving.
98
+ */
99
+ Types::BandwidthUsage GetState() const
100
+ {
101
+ return this->state;
102
+ }
103
+
104
+ private:
105
+ /**
106
+ * Slope of the least squares line fitted to the samples window, or no
107
+ * value if it cannot be computed.
108
+ */
109
+ std::optional<double> GetLinearFitSlope() const;
110
+
111
+ /**
112
+ * Highest slope that the least delayed samples of both ends of the window
113
+ * describe, or no value if they are too close in time to tell.
114
+ *
115
+ * @remarks
116
+ * - A burst of delayed packets in the middle of the window tilts the
117
+ * fitted line even though the queue never grew, and this is what keeps
118
+ * that from being read as overuse.
119
+ */
120
+ std::optional<double> GetSlopeCap() const;
121
+
122
+ void Detect(double trend, double sendDeltaUs, int64_t arrivalTimeUs);
123
+
124
+ void UpdateThreshold(double modifiedTrend, int64_t arrivalTimeUs);
125
+
126
+ private:
127
+ const TrendlineEstimatorOptions options;
128
+ int numOfDeltas{ 0 };
129
+ std::optional<int64_t> firstArrivalTimeUs;
130
+ double accumulatedDelayUs{ 0 };
131
+ double smoothedDelayUs{ 0 };
132
+ std::deque<PacketTiming> delayHist;
133
+ double threshold{ 12.5 };
134
+ double prevTrend{ 0 };
135
+ std::optional<double> timeOverUsingUs;
136
+ int overuseCounter{ 0 };
137
+ std::optional<int64_t> lastThresholdUpdateAtUs;
138
+ Types::BandwidthUsage state{ Types::BandwidthUsage::NORMAL };
139
+ };
140
+ } // namespace BWE
141
+ } // namespace RTC
142
+
143
+ #endif