mediasoup 3.26.0 → 3.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (310) hide show
  1. package/CHANGELOG.md +11 -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/rtpParameters.fbs +1 -2
  49. package/worker/fbs/rtpStream.fbs +3 -3
  50. package/worker/fbs/transport.fbs +23 -23
  51. package/worker/fuzzer/include/RTC/FuzzerDtlsTransport.hpp +4 -1
  52. package/worker/fuzzer/src/FuzzerUtils.cpp +5 -3
  53. package/worker/fuzzer/src/RTC/FuzzerDtlsTransport.cpp +8 -5
  54. package/worker/fuzzer/src/RTC/FuzzerRateCalculator.cpp +3 -3
  55. package/worker/fuzzer/src/RTC/FuzzerTrendCalculator.cpp +6 -6
  56. package/worker/fuzzer/src/RTC/RTCP/FuzzerFeedbackRtpTransport.cpp +3 -3
  57. package/worker/fuzzer/src/RTC/RTP/FuzzerPacket.cpp +2 -2
  58. package/worker/fuzzer/src/RTC/RTP/FuzzerRetransmissionBuffer.cpp +5 -5
  59. package/worker/fuzzer/src/RTC/RTP/FuzzerRtpStreamSend.cpp +3 -3
  60. package/worker/fuzzer/src/fuzzer.cpp +4 -0
  61. package/worker/include/Channel/ChannelNotification.hpp +3 -3
  62. package/worker/include/Channel/ChannelRequest.hpp +1 -1
  63. package/worker/include/Channel/ChannelSocket.hpp +11 -0
  64. package/worker/include/DepLibUV.hpp +16 -33
  65. package/worker/include/RTC/ActiveSpeakerObserver.hpp +11 -11
  66. package/worker/include/RTC/AudioLevelObserver.hpp +4 -4
  67. package/worker/include/RTC/BWE/AcknowledgedBitrateEstimatorInterface.hpp +55 -0
  68. package/worker/include/RTC/BWE/AimdRateControl.hpp +190 -0
  69. package/worker/include/RTC/BWE/BweTypes.hpp +211 -0
  70. package/worker/include/RTC/BWE/DelayBasedBwe.hpp +173 -0
  71. package/worker/include/RTC/BWE/FeedbackAdapter.hpp +72 -0
  72. package/worker/include/RTC/BWE/InterArrivalDelta.hpp +106 -0
  73. package/worker/include/RTC/BWE/LinkCapacityEstimator.hpp +84 -0
  74. package/worker/include/RTC/BWE/RobustThroughputEstimator.hpp +94 -0
  75. package/worker/include/RTC/BWE/SendPacketHistory.hpp +137 -0
  76. package/worker/include/RTC/BWE/TrendlineEstimator.hpp +101 -0
  77. package/worker/include/RTC/Consts.hpp +6 -6
  78. package/worker/include/RTC/Consumer.hpp +14 -12
  79. package/worker/include/RTC/DataConsumer.hpp +3 -3
  80. package/worker/include/RTC/DataProducer.hpp +3 -3
  81. package/worker/include/RTC/DtlsTransport.hpp +5 -2
  82. package/worker/include/RTC/ICE/IceCandidate.hpp +2 -2
  83. package/worker/include/RTC/ICE/IceServer.hpp +3 -3
  84. package/worker/include/RTC/KeyFrameRequestManager.hpp +3 -3
  85. package/worker/include/RTC/NEW_RTCP/packet/Packet.hpp +1 -1
  86. package/worker/include/RTC/NackGenerator.hpp +12 -12
  87. package/worker/include/RTC/PipeProducerStreamManager.hpp +3 -4
  88. package/worker/include/RTC/PipeTransport.hpp +18 -5
  89. package/worker/include/RTC/PlainTransport.hpp +18 -5
  90. package/worker/include/RTC/PortManager.hpp +4 -4
  91. package/worker/include/RTC/Producer.hpp +21 -12
  92. package/worker/include/RTC/ProducerStreamManager.hpp +12 -6
  93. package/worker/include/RTC/RTCP/Bye.hpp +2 -2
  94. package/worker/include/RTC/RTCP/FeedbackPsRemb.hpp +4 -4
  95. package/worker/include/RTC/RTCP/FeedbackRtpTmmb.hpp +3 -3
  96. package/worker/include/RTC/RTCP/FeedbackRtpTransport.hpp +54 -39
  97. package/worker/include/RTC/RTCP/Packet.hpp +3 -3
  98. package/worker/include/RTC/RTCP/ReceiverReport.hpp +3 -3
  99. package/worker/include/RTC/RTCP/Sdes.hpp +2 -2
  100. package/worker/include/RTC/RTCP/XR.hpp +2 -2
  101. package/worker/include/RTC/RTCP/XrDelaySinceLastRr.hpp +1 -1
  102. package/worker/include/RTC/RTP/Codecs/H264.hpp +1 -1
  103. package/worker/include/RTC/RTP/Codecs/Opus.hpp +2 -2
  104. package/worker/include/RTC/RTP/Codecs/PayloadDescriptorHandler.hpp +3 -3
  105. package/worker/include/RTC/RTP/Codecs/VP8.hpp +2 -2
  106. package/worker/include/RTC/RTP/Codecs/VP9.hpp +2 -2
  107. package/worker/include/RTC/RTP/HeaderExtensionIds.hpp +12 -12
  108. package/worker/include/RTC/RTP/Packet.hpp +13 -13
  109. package/worker/include/RTC/RTP/ProbationGenerator.hpp +1 -1
  110. package/worker/include/RTC/RTP/RetransmissionBuffer.hpp +7 -7
  111. package/worker/include/RTC/RTP/RtpStream.hpp +37 -51
  112. package/worker/include/RTC/RTP/RtpStreamRecv.hpp +59 -41
  113. package/worker/include/RTC/RTP/RtpStreamSend.hpp +20 -14
  114. package/worker/include/RTC/RTP/RtxStream.hpp +12 -13
  115. package/worker/include/RTC/RateCalculator.hpp +50 -44
  116. package/worker/include/RTC/RemoteCaptureTimeEstimator.hpp +5 -4
  117. package/worker/include/RTC/RemoteClockOffsetEstimator.hpp +11 -11
  118. package/worker/include/RTC/Router.hpp +6 -0
  119. package/worker/include/RTC/RtcLogger.hpp +1 -1
  120. package/worker/include/RTC/RtpDictionaries.hpp +10 -10
  121. package/worker/include/RTC/SCTP/association/Association.hpp +56 -28
  122. package/worker/include/RTC/SCTP/association/HeartbeatHandler.hpp +4 -4
  123. package/worker/include/RTC/SCTP/association/StateCookie.hpp +14 -14
  124. package/worker/include/RTC/SCTP/association/StreamResetHandler.hpp +2 -2
  125. package/worker/include/RTC/SCTP/association/TransmissionControlBlock.hpp +12 -12
  126. package/worker/include/RTC/SCTP/association/TransmissionControlBlockContextInterface.hpp +4 -4
  127. package/worker/include/RTC/SCTP/packet/TLV.hpp +1 -1
  128. package/worker/include/RTC/SCTP/packet/parameters/StateCookieParameter.hpp +3 -3
  129. package/worker/include/RTC/SCTP/public/AssociationInterface.hpp +4 -1
  130. package/worker/include/RTC/SCTP/public/AssociationMetrics.hpp +1 -1
  131. package/worker/include/RTC/SCTP/public/SctpOptions.hpp +12 -12
  132. package/worker/include/RTC/SCTP/public/SctpTypes.hpp +1 -1
  133. package/worker/include/RTC/SCTP/tx/OutstandingData.hpp +15 -16
  134. package/worker/include/RTC/SCTP/tx/RetransmissionQueue.hpp +7 -8
  135. package/worker/include/RTC/SCTP/tx/RetransmissionTimeout.hpp +20 -16
  136. package/worker/include/RTC/SCTP/tx/RoundRobinSendQueue.hpp +5 -6
  137. package/worker/include/RTC/SCTP/tx/SendQueueInterface.hpp +2 -2
  138. package/worker/include/RTC/SCTP/tx/StreamScheduler.hpp +5 -5
  139. package/worker/include/RTC/SctpDictionaries.hpp +3 -3
  140. package/worker/include/RTC/Serializable.hpp +1 -1
  141. package/worker/include/RTC/SimpleProducerStreamManager.hpp +3 -4
  142. package/worker/include/RTC/SimulcastProducerStreamManager.hpp +6 -7
  143. package/worker/include/RTC/SvcProducerStreamManager.hpp +6 -5
  144. package/worker/include/RTC/TcpConnection.hpp +9 -3
  145. package/worker/include/RTC/TcpServer.hpp +3 -0
  146. package/worker/include/RTC/Transport.hpp +52 -45
  147. package/worker/include/RTC/TransportCongestionControlClient.hpp +30 -25
  148. package/worker/include/RTC/TransportCongestionControlServer.hpp +17 -17
  149. package/worker/include/RTC/TrendCalculator.hpp +5 -5
  150. package/worker/include/RTC/UdpSocket.hpp +11 -2
  151. package/worker/include/RTC/WebRtcServer.hpp +20 -5
  152. package/worker/include/RTC/WebRtcTransport.hpp +35 -10
  153. package/worker/include/Settings.hpp +4 -2
  154. package/worker/include/Shared.hpp +5 -20
  155. package/worker/include/SharedInterface.hpp +6 -28
  156. package/worker/include/Utils.hpp +40 -21
  157. package/worker/include/common.hpp +4 -0
  158. package/worker/include/handles/BackoffTimerHandle.hpp +4 -4
  159. package/worker/include/handles/BackoffTimerHandleInterface.hpp +5 -5
  160. package/worker/include/handles/TcpConnectionHandle.hpp +6 -6
  161. package/worker/include/handles/TcpServerHandle.hpp +1 -1
  162. package/worker/include/handles/TimerHandle.hpp +15 -9
  163. package/worker/include/handles/TimerHandleInterface.hpp +10 -4
  164. package/worker/include/handles/UdpSocketHandle.hpp +3 -3
  165. package/worker/include/handles/UnixStreamSocketHandle.hpp +2 -2
  166. package/worker/meson.build +81 -36
  167. package/worker/meson_options.txt +1 -0
  168. package/worker/mocks/include/MockShared.hpp +24 -23
  169. package/worker/mocks/include/RTC/SCTP/association/MockTransmissionControlBlockContext.hpp +51 -27
  170. package/worker/mocks/include/RTC/SCTP/tx/MockSendQueue.hpp +11 -11
  171. package/worker/mocks/include/handles/MockBackoffTimerHandle.hpp +10 -10
  172. package/worker/mocks/include/handles/MockTimerHandle.hpp +60 -22
  173. package/worker/mocks/src/MockShared.cpp +51 -5
  174. package/worker/mocks/src/handles/MockBackoffTimerHandle.cpp +29 -15
  175. package/worker/mocks/src/handles/MockTimerHandle.cpp +117 -0
  176. package/worker/scripts/clang-scripts.mjs +2 -2
  177. package/worker/src/Channel/ChannelNotification.cpp +5 -6
  178. package/worker/src/Channel/ChannelSocket.cpp +20 -2
  179. package/worker/src/DepLibUV.cpp +4 -5
  180. package/worker/src/RTC/ActiveSpeakerObserver.cpp +59 -47
  181. package/worker/src/RTC/AudioLevelObserver.cpp +7 -7
  182. package/worker/src/RTC/BWE/AimdRateControl.cpp +468 -0
  183. package/worker/src/RTC/BWE/BweTypes.cpp +61 -0
  184. package/worker/src/RTC/BWE/DelayBasedBwe.cpp +275 -0
  185. package/worker/src/RTC/BWE/FeedbackAdapter.cpp +148 -0
  186. package/worker/src/RTC/BWE/InterArrivalDelta.cpp +181 -0
  187. package/worker/src/RTC/BWE/LinkCapacityEstimator.cpp +133 -0
  188. package/worker/src/RTC/BWE/RobustThroughputEstimator.cpp +245 -0
  189. package/worker/src/RTC/BWE/SendPacketHistory.cpp +184 -0
  190. package/worker/src/RTC/BWE/TrendlineEstimator.cpp +233 -0
  191. package/worker/src/RTC/Consumer.cpp +48 -35
  192. package/worker/src/RTC/DataConsumer.cpp +4 -4
  193. package/worker/src/RTC/DataProducer.cpp +3 -3
  194. package/worker/src/RTC/DirectTransport.cpp +2 -2
  195. package/worker/src/RTC/DtlsTransport.cpp +14 -11
  196. package/worker/src/RTC/ICE/IceServer.cpp +2 -2
  197. package/worker/src/RTC/KeyFrameRequestManager.cpp +16 -12
  198. package/worker/src/RTC/NackGenerator.cpp +12 -12
  199. package/worker/src/RTC/PipeProducerStreamManager.cpp +6 -6
  200. package/worker/src/RTC/PipeTransport.cpp +16 -13
  201. package/worker/src/RTC/PlainTransport.cpp +25 -16
  202. package/worker/src/RTC/Producer.cpp +50 -36
  203. package/worker/src/RTC/RTCP/FeedbackPsRemb.cpp +13 -6
  204. package/worker/src/RTC/RTCP/FeedbackRtpTmmb.cpp +14 -5
  205. package/worker/src/RTC/RTCP/FeedbackRtpTransport.cpp +76 -50
  206. package/worker/src/RTC/RTP/Codecs/VP8.cpp +1 -1
  207. package/worker/src/RTC/RTP/Codecs/VP9.cpp +4 -4
  208. package/worker/src/RTC/RTP/Packet.cpp +9 -10
  209. package/worker/src/RTC/RTP/RetransmissionBuffer.cpp +6 -6
  210. package/worker/src/RTC/RTP/RtpStream.cpp +19 -19
  211. package/worker/src/RTC/RTP/RtpStreamRecv.cpp +146 -81
  212. package/worker/src/RTC/RTP/RtpStreamSend.cpp +49 -44
  213. package/worker/src/RTC/RTP/RtxStream.cpp +7 -10
  214. package/worker/src/RTC/RateCalculator.cpp +99 -136
  215. package/worker/src/RTC/RemoteCaptureTimeEstimator.cpp +13 -13
  216. package/worker/src/RTC/RemoteClockOffsetEstimator.cpp +18 -17
  217. package/worker/src/RTC/Router.cpp +18 -1
  218. package/worker/src/RTC/RtpDictionaries/RtpEncodingParameters.cpp +5 -2
  219. package/worker/src/RTC/SCTP/association/Association.cpp +131 -102
  220. package/worker/src/RTC/SCTP/association/AssociationListenerDeferrer.cpp +4 -4
  221. package/worker/src/RTC/SCTP/association/Capabilities.cpp +1 -1
  222. package/worker/src/RTC/SCTP/association/HeartbeatHandler.cpp +25 -18
  223. package/worker/src/RTC/SCTP/association/StateCookie.cpp +7 -6
  224. package/worker/src/RTC/SCTP/association/StreamResetHandler.cpp +16 -13
  225. package/worker/src/RTC/SCTP/association/TransmissionControlBlock.cpp +28 -26
  226. package/worker/src/RTC/SCTP/packet/chunks/SackChunk.cpp +0 -1
  227. package/worker/src/RTC/SCTP/packet/parameters/StateCookieParameter.cpp +2 -2
  228. package/worker/src/RTC/SCTP/public/AssociationMetrics.cpp +1 -1
  229. package/worker/src/RTC/SCTP/public/SctpOptions.cpp +13 -13
  230. package/worker/src/RTC/SCTP/rx/DataTracker.cpp +1 -1
  231. package/worker/src/RTC/SCTP/rx/ReassemblyQueue.cpp +5 -5
  232. package/worker/src/RTC/SCTP/tx/OutstandingData.cpp +15 -15
  233. package/worker/src/RTC/SCTP/tx/RetransmissionQueue.cpp +15 -15
  234. package/worker/src/RTC/SCTP/tx/RetransmissionTimeout.cpp +29 -26
  235. package/worker/src/RTC/SCTP/tx/RoundRobinSendQueue.cpp +12 -11
  236. package/worker/src/RTC/SCTP/tx/StreamScheduler.cpp +4 -4
  237. package/worker/src/RTC/SctpDictionaries/SctpStreamParameters.cpp +2 -2
  238. package/worker/src/RTC/SimpleProducerStreamManager.cpp +10 -10
  239. package/worker/src/RTC/SimulcastProducerStreamManager.cpp +39 -37
  240. package/worker/src/RTC/SvcProducerStreamManager.cpp +39 -25
  241. package/worker/src/RTC/TcpConnection.cpp +9 -3
  242. package/worker/src/RTC/TcpServer.cpp +7 -2
  243. package/worker/src/RTC/Transport.cpp +242 -302
  244. package/worker/src/RTC/TransportCongestionControlClient.cpp +78 -71
  245. package/worker/src/RTC/TransportCongestionControlServer.cpp +38 -40
  246. package/worker/src/RTC/TrendCalculator.cpp +3 -3
  247. package/worker/src/RTC/UdpSocket.cpp +14 -3
  248. package/worker/src/RTC/WebRtcServer.cpp +20 -14
  249. package/worker/src/RTC/WebRtcTransport.cpp +35 -30
  250. package/worker/src/Settings.cpp +10 -0
  251. package/worker/src/Shared.cpp +2 -2
  252. package/worker/src/Utils/Crypto.cpp +2 -2
  253. package/worker/src/Worker.cpp +5 -1
  254. package/worker/src/handles/BackoffTimerHandle.cpp +24 -11
  255. package/worker/src/handles/TimerHandle.cpp +74 -25
  256. package/worker/src/lib.cpp +6 -0
  257. package/worker/tasks.py +171 -44
  258. package/worker/test/include/RTC/BWE/helpers/LinkSimulator.hpp +117 -0
  259. package/worker/test/src/RTC/BWE/TestAimdRateControl.cpp +400 -0
  260. package/worker/test/src/RTC/BWE/TestDelayBasedBwe.cpp +568 -0
  261. package/worker/test/src/RTC/BWE/TestFeedbackAdapter.cpp +546 -0
  262. package/worker/test/src/RTC/BWE/TestInterArrivalDelta.cpp +301 -0
  263. package/worker/test/src/RTC/BWE/TestLinkCapacityEstimator.cpp +115 -0
  264. package/worker/test/src/RTC/BWE/TestRobustThroughputEstimator.cpp +433 -0
  265. package/worker/test/src/RTC/BWE/TestSendPacketHistory.cpp +259 -0
  266. package/worker/test/src/RTC/BWE/TestTrendlineEstimator.cpp +137 -0
  267. package/worker/test/src/RTC/BWE/helpers/LinkSimulator.cpp +171 -0
  268. package/worker/test/src/RTC/RTCP/TestFeedbackPsRemb.cpp +1 -1
  269. package/worker/test/src/RTC/RTCP/TestFeedbackRtpTmmb.cpp +1 -1
  270. package/worker/test/src/RTC/RTCP/TestFeedbackRtpTransport.cpp +246 -208
  271. package/worker/test/src/RTC/RTP/TestPacket.cpp +11 -11
  272. package/worker/test/src/RTC/RTP/TestRetransmissionBuffer.cpp +12 -12
  273. package/worker/test/src/RTC/RTP/TestRtpStreamRecv.cpp +23 -16
  274. package/worker/test/src/RTC/RTP/TestRtpStreamSend.cpp +13 -13
  275. package/worker/test/src/RTC/SCTP/association/TestAssociation.cpp +27 -22
  276. package/worker/test/src/RTC/SCTP/association/TestHeartbeatHandler.cpp +21 -15
  277. package/worker/test/src/RTC/SCTP/association/TestStateCookie.cpp +6 -6
  278. package/worker/test/src/RTC/SCTP/association/TestStreamResetHandler.cpp +17 -13
  279. package/worker/test/src/RTC/SCTP/association/TestTransmissionControlBlock.cpp +3 -3
  280. package/worker/test/src/RTC/SCTP/packet/chunks/TestForwardTsnChunk.cpp +2 -2
  281. package/worker/test/src/RTC/SCTP/packet/chunks/TestIForwardTsnChunk.cpp +23 -23
  282. package/worker/test/src/RTC/SCTP/packet/chunks/TestInitChunk.cpp +1 -1
  283. package/worker/test/src/RTC/SCTP/packet/chunks/TestSackChunk.cpp +1 -1
  284. package/worker/test/src/RTC/SCTP/packet/parameters/TestIPv6AddressParameter.cpp +1 -1
  285. package/worker/test/src/RTC/SCTP/rx/TestDataTracker.cpp +37 -37
  286. package/worker/test/src/RTC/SCTP/rx/TestInterleavedReassemblyStreams.cpp +10 -10
  287. package/worker/test/src/RTC/SCTP/rx/TestReassemblyQueue.cpp +1 -1
  288. package/worker/test/src/RTC/SCTP/tx/TestOutstandingData.cpp +93 -93
  289. package/worker/test/src/RTC/SCTP/tx/TestRetransmissionQueue.cpp +247 -247
  290. package/worker/test/src/RTC/SCTP/tx/TestRetransmissionTimeout.cpp +100 -80
  291. package/worker/test/src/RTC/SCTP/tx/TestRoundRobinSendQueue.cpp +163 -163
  292. package/worker/test/src/RTC/SCTP/tx/TestStreamScheduler.cpp +76 -77
  293. package/worker/test/src/RTC/TestConsumer.cpp +8 -3
  294. package/worker/test/src/RTC/TestKeyFrameRequestManager.cpp +3 -3
  295. package/worker/test/src/RTC/TestNackGenerator.cpp +3 -3
  296. package/worker/test/src/RTC/TestPipeProducerStreamManager.cpp +5 -5
  297. package/worker/test/src/RTC/TestRateCalculator.cpp +110 -14
  298. package/worker/test/src/RTC/TestRemoteCaptureTimeEstimator.cpp +22 -17
  299. package/worker/test/src/RTC/TestRemoteClockOffsetEstimator.cpp +87 -85
  300. package/worker/test/src/RTC/TestSimpleProducerStreamManager.cpp +54 -47
  301. package/worker/test/src/RTC/TestSimulcastProducerStreamManager.cpp +83 -69
  302. package/worker/test/src/RTC/TestSvcProducerStreamManager.cpp +76 -65
  303. package/worker/test/src/RTC/TestTransportCongestionControlServer.cpp +72 -66
  304. package/worker/test/src/RTC/TestTransportTuple.cpp +19 -4
  305. package/worker/test/src/TestDepLibUV.cpp +19 -0
  306. package/worker/test/src/Utils/TestTime.cpp +57 -26
  307. package/worker/test/src/testHelpers.cpp +1 -1
  308. package/worker/test/src/tests.cpp +6 -0
  309. package/worker/include/RTC/SenderBandwidthEstimator.hpp +0 -118
  310. package/worker/src/RTC/SenderBandwidthEstimator.cpp +0 -269
@@ -3,6 +3,8 @@
3
3
 
4
4
  #include "RTC/RTP/RtpStreamSend.hpp"
5
5
  #include "Logger.hpp"
6
+ #include "RTC/RTCP/FeedbackPsFir.hpp"
7
+ #include "RTC/RTCP/FeedbackPsPli.hpp"
6
8
  #include "RTC/RtpDictionaries.hpp"
7
9
  #include "Utils.hpp"
8
10
  #include <vector>
@@ -14,12 +16,12 @@ namespace RTC
14
16
  /* Static. */
15
17
 
16
18
  // Limit max number of items in the retransmission buffer.
17
- static constexpr size_t RetransmissionBufferMaxItems{ 2500u };
19
+ static constexpr size_t RetransmissionBufferMaxItems{ 2500 };
18
20
  // 17: 16 bit mask + the initial sequence number.
19
- static constexpr size_t MaxRequestedPackets{ 17u };
21
+ static constexpr size_t MaxRequestedPackets{ 17 };
20
22
  static thread_local std::vector<RTP::RetransmissionBuffer::Item*> RetransmissionContainer(
21
23
  MaxRequestedPackets + 1);
22
- static constexpr uint32_t DefaultRtt{ 100u };
24
+ static constexpr int64_t DefaultRttMs{ 100 };
23
25
 
24
26
  /* Instance methods. */
25
27
 
@@ -36,7 +38,7 @@ namespace RTC
36
38
 
37
39
  if (this->params.useNack)
38
40
  {
39
- uint32_t maxRetransmissionDelayMs{ 0 };
41
+ int64_t maxRetransmissionDelayMs{ 0 };
40
42
 
41
43
  switch (params.mimeType.type)
42
44
  {
@@ -74,7 +76,7 @@ namespace RTC
74
76
  {
75
77
  MS_TRACE();
76
78
 
77
- const uint64_t nowMs = this->shared->GetTimeMs();
79
+ const int64_t nowMs = this->shared->GetTimeMs();
78
80
 
79
81
  auto baseStats = RTP::RtpStream::FillBufferStats(builder);
80
82
  auto stats = FBS::RtpStream::CreateSendStats(
@@ -82,7 +84,7 @@ namespace RTC
82
84
  baseStats,
83
85
  this->transmissionCounter.GetPacketCount(),
84
86
  this->transmissionCounter.GetBytes(),
85
- this->transmissionCounter.GetBitrate(nowMs));
87
+ static_cast<uint64_t>(this->transmissionCounter.GetBitrate(nowMs)));
86
88
 
87
89
  return FBS::RtpStream::CreateStats(builder, FBS::RtpStream::StatsData::SendStats, stats.Union());
88
90
  }
@@ -270,17 +272,17 @@ namespace RTC
270
272
  }
271
273
  }
272
274
 
273
- void RtpStreamSend::ReceiveRtcpReceiverReport(RTC::RTCP::ReceiverReport* report)
275
+ void RtpStreamSend::ReceiveRtcpReceiverReport(RTC::RTCP::ReceiverReport* report, int64_t receivedAtUs)
274
276
  {
275
277
  MS_TRACE();
276
278
 
277
279
  /* Calculate RTT. */
278
280
 
279
- // Get the NTP representation of the current timestamp.
280
- const uint64_t nowMs = this->shared->GetTimeMs();
281
- auto ntp = Utils::Time::TimeMs2Ntp(nowMs + this->shared->GetNtpOffsetMs());
281
+ // Get the NTP representation of the time at which the Receiver Report
282
+ // arrived, which is what the round trip is measured against.
283
+ auto ntp = Utils::Time::TimeUs2Ntp(receivedAtUs + this->shared->GetNtpOffsetUs());
282
284
 
283
- // Get the compact NTP representation of the current timestamp.
285
+ // Get the compact NTP representation of the arrival time.
284
286
  uint32_t compactNtp = (ntp.seconds & 0x0000FFFF) << 16;
285
287
 
286
288
  compactNtp |= (ntp.fractions & 0xFFFF0000) >> 16;
@@ -289,21 +291,20 @@ namespace RTC
289
291
  const uint32_t dlsr = report->GetDelaySinceLastSenderReport();
290
292
 
291
293
  // RTT in 1/2^16 second fractions.
292
- uint32_t rtt{ 0 };
294
+ uint32_t rttCompactNtp{ 0 };
293
295
 
294
296
  // If no Sender Report was received by the remote endpoint yet, ignore lastSr
295
297
  // and dlsr values in the Receiver Report.
296
298
  if (lastSr && dlsr && (compactNtp > dlsr + lastSr))
297
299
  {
298
- rtt = compactNtp - dlsr - lastSr;
300
+ rttCompactNtp = compactNtp - dlsr - lastSr;
299
301
  }
300
302
 
301
- // RTT in milliseconds.
302
- this->rtt = static_cast<float>(rtt >> 16) * 1000;
303
- this->rtt += (static_cast<float>(rtt & 0x0000FFFF) / 65536) * 1000;
303
+ this->rttMs = static_cast<float>(rttCompactNtp >> 16) * 1000;
304
+ this->rttMs += (static_cast<float>(rttCompactNtp & 0x0000FFFF) / 65536) * 1000;
304
305
 
305
306
  // Avoid negative RTT value since it doesn't make sense.
306
- this->rtt = std::max(this->rtt, 0.0f);
307
+ this->rttMs = std::max(this->rttMs, 0.0f);
307
308
 
308
309
  this->packetsLost = report->GetTotalLost();
309
310
  this->fractionLost = report->GetFractionLost();
@@ -313,7 +314,8 @@ namespace RTC
313
314
  UpdateScore(report);
314
315
  }
315
316
 
316
- void RtpStreamSend::ReceiveRtcpXrReceiverReferenceTime(RTC::RTCP::ReceiverReferenceTime* report)
317
+ void RtpStreamSend::ReceiveRtcpXrReceiverReferenceTime(
318
+ RTC::RTCP::ReceiverReferenceTime* report, int64_t receivedAtUs)
317
319
  {
318
320
  MS_TRACE();
319
321
 
@@ -322,12 +324,12 @@ namespace RTC
322
324
  compactNtp += report->GetNtpFrac() >> 16;
323
325
 
324
326
  this->lastReceiverReferenceTime = ReceiverReferenceTime{
325
- .compactNtp = compactNtp,
326
- .receivedMs = this->shared->GetTimeMs(),
327
+ .compactNtp = compactNtp,
328
+ .receivedAtUs = receivedAtUs,
327
329
  };
328
330
  }
329
331
 
330
- RTC::RTCP::SenderReport* RtpStreamSend::GetRtcpSenderReport(uint64_t nowMs)
332
+ RTC::RTCP::SenderReport* RtpStreamSend::GetRtcpSenderReport(int64_t nowUs)
331
333
  {
332
334
  MS_TRACE();
333
335
 
@@ -338,22 +340,22 @@ namespace RTC
338
340
 
339
341
  // A stream that stopped sending cannot tell where its RTP timeline is now, and
340
342
  // extrapolating would claim RTP timestamps of packets never sent.
341
- if (nowMs - this->maxPacketMs > RtpStreamSend::MaxSenderReportReferenceAgeMs)
343
+ if ((nowUs - this->maxPacketAtUs) / 1000 > RtpStreamSend::MaxSenderReportReferenceAgeMs)
342
344
  {
343
345
  return nullptr;
344
346
  }
345
347
 
346
- auto ntp = Utils::Time::TimeMs2Ntp(nowMs + this->shared->GetNtpOffsetMs());
348
+ auto ntp = Utils::Time::TimeUs2Ntp(nowUs + this->shared->GetNtpOffsetUs());
347
349
  auto* report = new RTC::RTCP::SenderReport();
348
350
 
349
351
  // Calculate TS difference between now and the instant at which the media in the
350
352
  // packet holding the highest RTP timestamp was captured, falling back to the
351
353
  // instant that packet was seen while the capture instant cannot be told.
352
- const uint64_t referenceMs = this->maxPacketCaptureMs.value_or(this->maxPacketMs);
354
+ const int64_t referenceUs = this->maxPacketCaptureAtUs.value_or(this->maxPacketAtUs);
353
355
  // NOTE: The capture instant is an estimation, so it may land ahead of now.
354
- const uint64_t diffMs = nowMs > referenceMs ? nowMs - referenceMs : 0;
355
- const uint64_t diffTs = diffMs * GetClockRate() / 1000;
356
- const auto rtpTs = static_cast<uint32_t>(this->maxPacketTs + diffTs);
356
+ const int64_t diffUs = nowUs > referenceUs ? nowUs - referenceUs : 0;
357
+ const int64_t diffTs = (diffUs * GetClockRate()) / 1000000;
358
+ const auto rtpTs = static_cast<uint32_t>(this->maxPacketTs + diffTs);
357
359
 
358
360
  report->SetSsrc(GetSsrc());
359
361
  report->SetPacketCount(this->transmissionCounter.GetPacketCount());
@@ -363,15 +365,18 @@ namespace RTC
363
365
  report->SetRtpTs(rtpTs);
364
366
 
365
367
  // Update info about last Sender Report.
368
+ //
369
+ // NOTE: It is the very instant announced in the report above, so that the
370
+ // mapping means the same thing here and in a receive stream.
366
371
  this->lastSenderReportMapping = RTP::RtpStream::SenderReportMapping{
367
- .ntpMs = nowMs,
372
+ .ntpUs = nowUs + this->shared->GetNtpOffsetUs(),
368
373
  .ts = rtpTs,
369
374
  };
370
375
 
371
376
  return report;
372
377
  }
373
378
 
374
- RTC::RTCP::DelaySinceLastRr::SsrcInfo* RtpStreamSend::GetRtcpXrDelaySinceLastRrSsrcInfo(uint64_t nowMs)
379
+ RTC::RTCP::DelaySinceLastRr::SsrcInfo* RtpStreamSend::GetRtcpXrDelaySinceLastRrSsrcInfo(int64_t nowUs)
375
380
  {
376
381
  MS_TRACE();
377
382
 
@@ -381,12 +386,12 @@ namespace RTC
381
386
  }
382
387
 
383
388
  const auto& receiverReferenceTime = this->lastReceiverReferenceTime.value();
384
- // Get delay in milliseconds.
385
- auto delayMs = static_cast<uint32_t>(nowMs - receiverReferenceTime.receivedMs);
389
+ // Get delay in microseconds.
390
+ const int64_t delayUs = nowUs - receiverReferenceTime.receivedAtUs;
386
391
  // Express delay in units of 1/65536 seconds.
387
- uint32_t dlrr = (delayMs / 1000) << 16;
392
+ auto dlrr = static_cast<uint32_t>((delayUs / 1000000) << 16);
388
393
 
389
- dlrr |= uint32_t{ (delayMs % 1000) * 65536 / 1000 };
394
+ dlrr |= static_cast<uint32_t>(((delayUs % 1000000) * 65536) / 1000000);
390
395
 
391
396
  auto* ssrcInfo = new RTC::RTCP::DelaySinceLastRr::SsrcInfo();
392
397
 
@@ -430,23 +435,23 @@ namespace RTC
430
435
  MS_TRACE();
431
436
  }
432
437
 
433
- uint32_t RtpStreamSend::GetBitrate(
434
- uint64_t /*nowMs*/, uint8_t /*spatialLayer*/, uint8_t /*temporalLayer*/)
438
+ int64_t RtpStreamSend::GetBitrate(
439
+ int64_t /*nowMs*/, uint8_t /*spatialLayer*/, uint8_t /*temporalLayer*/)
435
440
  {
436
441
  MS_TRACE();
437
442
 
438
443
  MS_ABORT("invalid method call");
439
444
  }
440
445
 
441
- uint32_t RtpStreamSend::GetSpatialLayerBitrate(uint64_t /*nowMs*/, uint8_t /*spatialLayer*/)
446
+ int64_t RtpStreamSend::GetSpatialLayerBitrate(int64_t /*nowMs*/, uint8_t /*spatialLayer*/)
442
447
  {
443
448
  MS_TRACE();
444
449
 
445
450
  MS_ABORT("invalid method call");
446
451
  }
447
452
 
448
- uint32_t RtpStreamSend::GetLayerBitrate(
449
- uint64_t /*nowMs*/, uint8_t /*spatialLayer*/, uint8_t /*temporalLayer*/)
453
+ int64_t RtpStreamSend::GetLayerBitrate(
454
+ int64_t /*nowMs*/, uint8_t /*spatialLayer*/, uint8_t /*temporalLayer*/)
450
455
  {
451
456
  MS_TRACE();
452
457
 
@@ -480,9 +485,9 @@ namespace RTC
480
485
  }
481
486
 
482
487
  // Look for each requested packet.
483
- const uint64_t nowMs = this->shared->GetTimeMs();
484
- const uint16_t rtt = (this->rtt > 0.0f ? this->rtt : DefaultRtt);
485
- uint16_t currentSeq = seq;
488
+ const int64_t nowMs = this->shared->GetTimeMs();
489
+ const int64_t rttMs = (this->rttMs > 0.0f ? this->rttMs : DefaultRttMs);
490
+ uint16_t currentSeq = seq;
486
491
  bool requested{ true };
487
492
  size_t containerIdx{ 0 };
488
493
 
@@ -507,14 +512,14 @@ namespace RTC
507
512
  // Do nothing.
508
513
  }
509
514
  // Don't resent the packet if it was resent in the last RTT ms.
510
- else if (item->resentAtMs != 0u && nowMs - item->resentAtMs <= static_cast<uint64_t>(rtt))
515
+ else if (item->resentAtMs != 0 && nowMs - item->resentAtMs <= rttMs)
511
516
  {
512
517
  MS_DEBUG_TAG(
513
518
  rtx,
514
519
  "ignoring retransmission for a packet already resent in the last RTT ms "
515
- "[seq:%" PRIu16 ", rtt:%" PRIu16 "]",
520
+ "[seq:%" PRIu16 ", rtt:%" PRIi64 " ms]",
516
521
  item->sequenceNumber,
517
- rtt);
522
+ rttMs);
518
523
  }
519
524
  // Stored packet is valid for retransmission. Resend it.
520
525
  else
@@ -57,7 +57,6 @@ namespace RTC
57
57
  this->started = true;
58
58
  this->maxSeq = seq - 1;
59
59
  this->maxPacketTs = packet->GetTimestamp();
60
- this->maxPacketMs = this->shared->GetTimeMs();
61
60
  }
62
61
 
63
62
  // If not a valid packet ignore it.
@@ -76,7 +75,6 @@ namespace RTC
76
75
  if (Utils::Number::IsHigherThan<uint32_t>(packet->GetTimestamp(), this->maxPacketTs))
77
76
  {
78
77
  this->maxPacketTs = packet->GetTimestamp();
79
- this->maxPacketMs = this->shared->GetTimeMs();
80
78
  }
81
79
 
82
80
  // Increase packet count.
@@ -141,12 +139,12 @@ namespace RTC
141
139
  if (this->lastSenderReportTiming.has_value())
142
140
  {
143
141
  const auto& senderReportTiming = this->lastSenderReportTiming.value();
144
- // Get delay in milliseconds.
145
- const uint32_t delayMs = this->shared->GetTimeMs() - senderReportTiming.receivedMs;
142
+ // Get delay in microseconds.
143
+ const int64_t delayUs = this->shared->GetTimeUs() - senderReportTiming.receivedAtUs;
146
144
  // Express delay in units of 1/65536 seconds.
147
- uint32_t dlsr = (delayMs / 1000) << 16;
145
+ auto dlsr = static_cast<uint32_t>((delayUs / 1000000) << 16);
148
146
 
149
- dlsr |= uint32_t{ (delayMs % 1000) * 65536 / 1000 };
147
+ dlsr |= static_cast<uint32_t>(((delayUs % 1000000) * 65536) / 1000000);
150
148
 
151
149
  report->SetDelaySinceLastSenderReport(dlsr);
152
150
  report->SetLastSenderReport(senderReportTiming.compactNtp);
@@ -160,7 +158,7 @@ namespace RTC
160
158
  return report;
161
159
  }
162
160
 
163
- void RtxStream::ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report)
161
+ void RtxStream::ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report, int64_t receivedAtUs)
164
162
  {
165
163
  MS_TRACE();
166
164
 
@@ -169,8 +167,8 @@ namespace RTC
169
167
  compactNtp += report->GetNtpFrac() >> 16;
170
168
 
171
169
  this->lastSenderReportTiming = SenderReportTiming{
172
- .compactNtp = compactNtp,
173
- .receivedMs = this->shared->GetTimeMs(),
170
+ .compactNtp = compactNtp,
171
+ .receivedAtUs = receivedAtUs,
174
172
  };
175
173
  }
176
174
 
@@ -215,7 +213,6 @@ namespace RTC
215
213
  InitSeq(seq);
216
214
 
217
215
  this->maxPacketTs = packet->GetTimestamp();
218
- this->maxPacketMs = this->shared->GetTimeMs();
219
216
  }
220
217
  else
221
218
  {
@@ -3,129 +3,76 @@
3
3
 
4
4
  #include "RTC/RateCalculator.hpp"
5
5
  #include "Logger.hpp"
6
- #include "Utils.hpp"
7
- #include <cmath> // std::trunc()
8
- #include <cstring> // std::memset()
6
+ #include <cmath> // std::trunc()
7
+ #include <limits> // std::numeric_limits()
9
8
 
10
9
  namespace RTC
11
10
  {
12
- RateCalculator::RateCalculator(size_t windowSizeMs, float scale, uint16_t windowItems)
13
- : windowSizeMs(windowSizeMs),
14
- scale(scale),
15
- windowItems(windowItems),
16
- itemSizeMs(std::max(windowSizeMs / windowItems, size_t{ 1 }))
11
+ RateCalculator::RateCalculator(int64_t windowSizeMs, float scale, uint16_t windowItems)
17
12
  {
18
13
  MS_TRACE();
19
14
 
20
- this->buffer.resize(windowItems);
15
+ // Clamp the given values so every derived value is safe to use.
16
+ this->windowSizeMs = std::max<int64_t>(windowSizeMs, 1);
21
17
 
22
- std::memset(
23
- static_cast<void*>(std::addressof(this->buffer.front())),
24
- 0,
25
- sizeof(BufferItem) * this->buffer.size());
18
+ const int64_t items = std::max<int64_t>(windowItems, 1);
19
+
20
+ // Item granularity, rounded up so that `items` items always suffice to cover
21
+ // the window.
22
+ this->itemSizeMs = (this->windowSizeMs + items - 1) / items;
23
+
24
+ // Number of items needed to cover the whole window, rounded up. It is never
25
+ // higher than `items`, and it guarantees that in-window data can never
26
+ // overrun the ring. The window it spans overshoots windowSizeMs by less than
27
+ // one item, which is inherent to splitting the window into items.
28
+ this->buffer.resize(
29
+ static_cast<size_t>((this->windowSizeMs + this->itemSizeMs - 1) / this->itemSizeMs));
30
+
31
+ this->rateScale = static_cast<double>(scale) / static_cast<double>(this->windowSizeMs);
26
32
  }
27
33
 
28
- void RateCalculator::Update(size_t size, uint64_t nowMs)
34
+ void RateCalculator::Update(size_t size, int64_t nowMs)
29
35
  {
30
36
  MS_TRACE();
31
37
 
32
- // Ignore too old data. Should never happen.
33
- if (this->oldestItemStartTimeMs.has_value() && Utils::Number::IsLowerThan<uint64_t>(nowMs, *this->oldestItemStartTimeMs))
38
+ // Ignore data older than the window. Should never happen.
39
+ if (!SlideWindow(nowMs))
34
40
  {
35
- MS_WARN_DEV("nowMs < this->oldestItemStartTimeMs, should never happen");
41
+ MS_WARN_DEV("given nowMs is older than the current window, ignoring data");
36
42
 
37
43
  return;
38
44
  }
39
45
 
40
- // Increase bytes.
41
- this->bytes += size;
42
-
43
- RemoveOldData(nowMs);
44
-
45
- // If the elapsed time from the newest item start time is greater than the
46
- // item size (in milliseconds), increase the item index.
47
- if (
48
- this->newestItemIndex < 0 || !this->newestItemStartTimeMs.has_value() ||
49
- Utils::Number::IsHigherOrEqualThan<uint64_t>(
50
- nowMs - *this->newestItemStartTimeMs, this->itemSizeMs))
51
- {
52
- this->newestItemIndex++;
53
- this->newestItemStartTimeMs = nowMs;
54
-
55
- if (this->newestItemIndex >= this->windowItems)
56
- {
57
- MS_DEBUG_DEV("this->newestItemIndex >= this->windowItems, setting this->newestItemIndex = 0");
58
-
59
- this->newestItemIndex = 0;
60
- }
61
-
62
- // Advance oldestItemIndex if buffer is full.
63
- // NOTE: This avoids a crash:
64
- // https://github.com/versatica/mediasoup/issues/1316
65
- if (this->newestItemIndex == this->oldestItemIndex && this->oldestItemIndex != -1)
66
- {
67
- if (++this->oldestItemIndex >= this->windowItems)
68
- {
69
- this->oldestItemIndex = 0;
70
- }
71
- }
72
-
73
- MS_ASSERT(
74
- this->newestItemIndex != this->oldestItemIndex || this->oldestItemIndex == -1,
75
- "newest index overlaps with the oldest one [newestItemIndex:%" PRIi32
76
- ", oldestItemIndex:%" PRIi32 "]",
77
- this->newestItemIndex,
78
- this->oldestItemIndex);
79
-
80
- // Set the newest item.
81
- BufferItem& item = this->buffer[this->newestItemIndex];
82
-
83
- item.count = size;
84
- item.timeMs = nowMs;
85
- }
86
- else
87
- {
88
- // Update the newest item.
89
- BufferItem& item = this->buffer[this->newestItemIndex];
90
-
91
- item.count += size;
92
- }
93
-
94
- // Set the oldest item index and time, if not set.
95
- if (this->oldestItemIndex < 0)
96
- {
97
- MS_DEBUG_DEV(
98
- "this->oldestItemIndex < 0, setting this->oldestItemIndex and this->oldestItemStartTimeMs");
99
-
100
- this->oldestItemIndex = this->newestItemIndex;
101
- this->oldestItemStartTimeMs = nowMs;
102
- }
103
-
46
+ this->buffer[this->newestItemIndex] += size;
104
47
  this->totalCount += size;
105
-
106
- // Reset `lastRate` and `lastTimeMs` so `GetRate()` will calculate rate
107
- // again even if called with same now in the same loop iteration.
108
- this->lastRate = 0;
109
- this->lastTimeMs = std::nullopt;
48
+ this->bytes += size;
110
49
  }
111
50
 
112
- uint32_t RateCalculator::GetRate(uint64_t nowMs)
51
+ int64_t RateCalculator::GetRate(int64_t nowMs)
113
52
  {
114
53
  MS_TRACE();
115
54
 
116
- if (this->lastTimeMs.has_value() && nowMs == *this->lastTimeMs)
55
+ // If both keys match, the memoized rate is still exact. `lastRate` is a pure
56
+ // function of `totalCount`, so the value is right, and no expiration can be
57
+ // pending: SlideWindow() already ran for this very `nowMs` and
58
+ // `newestItemStartTimeMs` only moves forward afterwards, while the initial and
59
+ // post Reset() state has an empty ring anyway.
60
+ if (nowMs == this->lastTimeMs && this->totalCount == this->lastTotalCount)
117
61
  {
118
- MS_DEBUG_DEV("nowMs == this->lastTimeMs, early return");
62
+ MS_DEBUG_DEV("nothing changed since the latest call, early return");
119
63
 
120
64
  return this->lastRate;
121
65
  }
122
66
 
123
- RemoveOldData(nowMs);
67
+ SlideWindow(nowMs);
124
68
 
125
- const float scale = this->scale / this->windowSizeMs;
69
+ const double rate = std::trunc((static_cast<double>(this->totalCount) * this->rateScale) + 0.5);
126
70
 
127
- this->lastTimeMs = nowMs;
128
- this->lastRate = static_cast<uint32_t>(std::trunc((this->totalCount * scale) + 0.5f));
71
+ // NOTE: Must be read after SlideWindow(), which may have expired data.
72
+ this->lastTotalCount = this->totalCount;
73
+ this->lastTimeMs = nowMs;
74
+ this->lastRate =
75
+ static_cast<int64_t>(std::min(rate, static_cast<double>(std::numeric_limits<int64_t>::max())));
129
76
 
130
77
  return this->lastRate;
131
78
  }
@@ -134,73 +81,89 @@ namespace RTC
134
81
  {
135
82
  MS_TRACE();
136
83
 
137
- std::memset(
138
- static_cast<void*>(std::addressof(this->buffer.front())),
139
- 0,
140
- sizeof(BufferItem) * this->buffer.size());
141
-
142
- this->newestItemStartTimeMs = std::nullopt;
143
- this->newestItemIndex = -1;
144
- this->oldestItemStartTimeMs = std::nullopt;
145
- this->oldestItemIndex = -1;
146
- this->totalCount = 0u;
147
- this->lastRate = 0u;
148
- this->lastTimeMs = std::nullopt;
84
+ std::ranges::fill(this->buffer, 0);
85
+
86
+ this->newestItemIndex = 0;
87
+ this->newestItemStartTimeMs = 0;
88
+ this->totalCount = 0;
89
+ this->lastRate = 0;
90
+ this->lastTimeMs = 0;
91
+ this->lastTotalCount = 0;
149
92
  }
150
93
 
151
- void RateCalculator::RemoveOldData(uint64_t nowMs)
94
+ /**
95
+ * Expires the items that no longer belong to the window ending at `nowMs`,
96
+ * and makes the item holding `nowMs` the newest one.
97
+ *
98
+ * Returns false if `nowMs` is so far in the past that it lies outside of the
99
+ * window, in which case nothing is modified.
100
+ */
101
+ bool RateCalculator::SlideWindow(int64_t nowMs)
152
102
  {
153
103
  MS_TRACE();
154
104
 
155
- if (!this->oldestItemStartTimeMs.has_value())
105
+ // Time elapsed since the newest item started, negative when `nowMs` lies in
106
+ // the past.
107
+ const int64_t elapsedMs = nowMs - this->newestItemStartTimeMs;
108
+
109
+ // `nowMs` is older than the whole window.
110
+ if (elapsedMs <= -this->windowSizeMs)
156
111
  {
157
- return;
112
+ return false;
158
113
  }
159
114
 
160
- // No item set.
161
- if (this->newestItemIndex < 0 || this->oldestItemIndex < 0)
115
+ // `nowMs` belongs to the newest item, or to an already existing one still
116
+ // within the window, so there is nothing to expire.
117
+ if (elapsedMs < this->itemSizeMs)
162
118
  {
163
- return;
119
+ return true;
164
120
  }
165
121
 
166
- const uint64_t newOldestTime = nowMs - this->windowSizeMs;
122
+ // NOTE: Positive since `elapsedMs` is not lower than `itemSizeMs` here.
123
+ const int64_t steps = elapsedMs / this->itemSizeMs;
167
124
 
168
- // Oldest item already removed.
169
- if (Utils::Number::IsLowerThan<uint64_t>(newOldestTime, *this->oldestItemStartTimeMs))
125
+ // A whole window elapsed since the newest item, so every item is gone.
126
+ if (std::cmp_greater_equal(steps, this->buffer.size()))
170
127
  {
171
- return;
172
- }
128
+ MS_DEBUG_DEV("a whole window elapsed, resetting every item");
173
129
 
174
- // A whole window size time has elapsed since last entry. Reset the buffer.
175
- if (
176
- this->newestItemStartTimeMs.has_value() &&
177
- Utils::Number::IsHigherOrEqualThan<uint64_t>(newOldestTime, *this->newestItemStartTimeMs))
178
- {
179
- MS_DEBUG_DEV("newOldestTime >= this->newestItemStartTimeMs, resetting the buffer");
130
+ // NOTE: totalCount is the sum of every item, so a zero total means that
131
+ // the ring is already zeroed.
132
+ if (this->totalCount != 0)
133
+ {
134
+ std::ranges::fill(this->buffer, 0);
135
+
136
+ this->totalCount = 0;
137
+ }
180
138
 
181
- Reset();
139
+ this->newestItemIndex = 0;
140
+ this->newestItemStartTimeMs = nowMs;
182
141
 
183
- return;
142
+ return true;
184
143
  }
185
144
 
186
- while (Utils::Number::IsHigherOrEqualThan<uint64_t>(newOldestTime, *this->oldestItemStartTimeMs))
145
+ // Walk the ring forward. Every item being passed holds the count of exactly
146
+ // buffer.size() items ago, which is now out of the window.
147
+ for (int64_t i{ 0 }; i < steps; ++i)
187
148
  {
188
- BufferItem& oldestItem = this->buffer[this->oldestItemIndex];
189
-
190
- this->totalCount -= oldestItem.count;
191
-
192
- oldestItem.count = 0u;
193
- oldestItem.timeMs = 0u;
194
-
195
- if (++this->oldestItemIndex >= this->windowItems)
149
+ if (++this->newestItemIndex == this->buffer.size())
196
150
  {
197
- this->oldestItemIndex = 0;
151
+ this->newestItemIndex = 0;
198
152
  }
199
153
 
200
- const BufferItem& newOldestItem = this->buffer[this->oldestItemIndex];
154
+ this->totalCount -= this->buffer[this->newestItemIndex];
201
155
 
202
- this->oldestItemStartTimeMs = newOldestItem.timeMs;
156
+ this->buffer[this->newestItemIndex] = 0;
203
157
  }
158
+
159
+ // Advance by whole items rather than jumping to `nowMs`. The window is
160
+ // derived from item geometry (buffer.size() * itemSizeMs) instead of from per
161
+ // item timestamps, so absorbing the `elapsedMs % itemSizeMs` remainder here
162
+ // would stretch items past itemSizeMs, making the ring span more time than
163
+ // windowSizeMs and hence over-report the rate.
164
+ this->newestItemStartTimeMs += steps * this->itemSizeMs;
165
+
166
+ return true;
204
167
  }
205
168
 
206
169
  void RtpDataCounter::Update(const RTC::RTP::Packet* packet)
@@ -31,29 +31,29 @@ namespace RTC
31
31
  rtcp,
32
32
  "capture instant source set to %s",
33
33
  this->source == RemoteCaptureTimeEstimator::Source::ABS_CAPTURE_TIME ? "abs-capture-time"
34
- : "Sender Report");
34
+ : "Sender Report");
35
35
  }
36
36
 
37
37
  void RemoteCaptureTimeEstimator::SenderReportReceived(const RTC::RTP::RtpStreamRecv* rtpStream)
38
38
  {
39
39
  MS_TRACE();
40
40
 
41
- const auto senderReportMapping = rtpStream->GetSenderReportMapping();
42
- const auto senderReportReceivedMs = rtpStream->GetSenderReportReceivedMs();
41
+ const auto senderReportMapping = rtpStream->GetSenderReportMapping();
42
+ const auto senderReportReceivedAtUs = rtpStream->GetSenderReportReceivedAtUs();
43
43
 
44
44
  // Both are stored together, so either both are set or none of them is.
45
- if (!senderReportMapping.has_value() || !senderReportReceivedMs.has_value())
45
+ if (!senderReportMapping.has_value() || !senderReportReceivedAtUs.has_value())
46
46
  {
47
47
  return;
48
48
  }
49
49
 
50
50
  this->clockOffsetEstimator.AddSenderReport(
51
- senderReportMapping.value().ntpMs,
52
- senderReportReceivedMs.value(),
53
- static_cast<uint32_t>(rtpStream->GetRtt()));
51
+ senderReportMapping.value().ntpUs,
52
+ senderReportReceivedAtUs.value(),
53
+ static_cast<int64_t>(rtpStream->GetRttMs()));
54
54
  }
55
55
 
56
- std::optional<uint64_t> RemoteCaptureTimeEstimator::GetLocalCaptureMs(
56
+ std::optional<int64_t> RemoteCaptureTimeEstimator::GetLocalCaptureAtUs(
57
57
  const RTC::RTP::RtpStreamRecv* rtpStream, uint32_t ts) const
58
58
  {
59
59
  MS_TRACE();
@@ -63,15 +63,15 @@ namespace RTC
63
63
  return std::nullopt;
64
64
  }
65
65
 
66
- const auto remoteCaptureMs = this->source == RemoteCaptureTimeEstimator::Source::ABS_CAPTURE_TIME
67
- ? rtpStream->GetRemoteCaptureMsFromAbsCaptureTime(ts)
68
- : rtpStream->GetRemoteCaptureMsFromSenderReport(ts);
66
+ const auto remoteCaptureAtUs = this->source == RemoteCaptureTimeEstimator::Source::ABS_CAPTURE_TIME
67
+ ? rtpStream->GetRemoteCaptureAtUsFromAbsCaptureTime(ts)
68
+ : rtpStream->GetRemoteCaptureAtUsFromSenderReport(ts);
69
69
 
70
- if (!remoteCaptureMs.has_value())
70
+ if (!remoteCaptureAtUs.has_value())
71
71
  {
72
72
  return std::nullopt;
73
73
  }
74
74
 
75
- return this->clockOffsetEstimator.RemoteMsToLocalMs(remoteCaptureMs.value());
75
+ return this->clockOffsetEstimator.RemoteUsToLocalUs(remoteCaptureAtUs.value());
76
76
  }
77
77
  } // namespace RTC