mediasoup 3.27.0 → 3.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 (213) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/node/lib/Consumer.d.ts.map +1 -1
  3. package/node/lib/Consumer.js +4 -51
  4. package/node/lib/Producer.d.ts.map +1 -1
  5. package/node/lib/Producer.js +4 -8
  6. package/node/lib/ProducerTypes.d.ts +4 -2
  7. package/node/lib/ProducerTypes.d.ts.map +1 -1
  8. package/node/lib/Router.d.ts.map +1 -1
  9. package/node/lib/Router.js +4 -0
  10. package/node/lib/Transport.d.ts.map +1 -1
  11. package/node/lib/Transport.js +7 -10
  12. package/node/lib/fbs/rtp-parameters/rtp-header-extension-uri.d.ts +3 -4
  13. package/node/lib/fbs/rtp-parameters/rtp-header-extension-uri.d.ts.map +1 -1
  14. package/node/lib/fbs/rtp-parameters/rtp-header-extension-uri.js +3 -4
  15. package/node/lib/ortc.d.ts +0 -2
  16. package/node/lib/ortc.d.ts.map +1 -1
  17. package/node/lib/ortc.js +11 -21
  18. package/node/lib/rtpMappingFbsUtils.d.ts +6 -0
  19. package/node/lib/rtpMappingFbsUtils.d.ts.map +1 -0
  20. package/node/lib/rtpMappingFbsUtils.js +65 -0
  21. package/node/lib/rtpParametersFbsUtils.d.ts.map +1 -1
  22. package/node/lib/rtpParametersFbsUtils.js +0 -6
  23. package/node/lib/rtpParametersTypes.d.ts +1 -1
  24. package/node/lib/rtpParametersTypes.d.ts.map +1 -1
  25. package/node/lib/rtpStreamFbsUtils.d.ts +4 -0
  26. package/node/lib/rtpStreamFbsUtils.d.ts.map +1 -0
  27. package/node/lib/rtpStreamFbsUtils.js +51 -0
  28. package/node/lib/supportedRtpCapabilities.d.ts.map +1 -1
  29. package/node/lib/supportedRtpCapabilities.js +6 -13
  30. package/node/lib/test/test-Consumer.js +1 -13
  31. package/node/lib/test/test-DataConsumer.js +9 -0
  32. package/node/lib/test/test-DataProducer.js +38 -0
  33. package/node/lib/test/test-PipeTransport.js +27 -29
  34. package/node/lib/test/test-Producer.js +97 -0
  35. package/node/lib/test/test-ortc.js +34 -14
  36. package/package.json +10 -10
  37. package/worker/fbs/FBS/meson.build +25 -0
  38. package/worker/fbs/meson.build +15 -18
  39. package/worker/fbs/rtpParameters.fbs +0 -1
  40. package/worker/fuzzer/src/FuzzerUtils.cpp +5 -3
  41. package/worker/fuzzer/src/RTC/RTP/FuzzerPacket.cpp +16 -2
  42. package/worker/include/DepLibUV.hpp +13 -0
  43. package/worker/include/RTC/BWE/AlrDetector.hpp +97 -0
  44. package/worker/include/RTC/BWE/BitrateProber.hpp +217 -0
  45. package/worker/include/RTC/BWE/BitrateUtils.hpp +39 -0
  46. package/worker/include/RTC/BWE/BweTypes.hpp +171 -3
  47. package/worker/include/RTC/BWE/FeedbackAdapter.hpp +0 -14
  48. package/worker/include/RTC/BWE/InterArrival.hpp +117 -0
  49. package/worker/include/RTC/BWE/IntervalBudget.hpp +81 -0
  50. package/worker/include/RTC/BWE/LossBasedController.hpp +625 -0
  51. package/worker/include/RTC/BWE/OveruseDetector.hpp +71 -0
  52. package/worker/include/RTC/BWE/OveruseEstimator.hpp +115 -0
  53. package/worker/include/RTC/BWE/ProbeBitrateEstimator.hpp +84 -0
  54. package/worker/include/RTC/BWE/ProbeController.hpp +379 -0
  55. package/worker/include/RTC/BWE/ProbePacketGenerator.hpp +130 -0
  56. package/worker/include/RTC/BWE/ProbingScheduler.hpp +187 -0
  57. package/worker/include/RTC/BWE/RembGenerator.hpp +96 -0
  58. package/worker/include/RTC/BWE/RemoteBitrateEstimatorAbsSendTime.hpp +237 -0
  59. package/worker/include/RTC/BWE/RobustThroughputEstimator.hpp +26 -0
  60. package/worker/include/RTC/BWE/SendPacketHistory.hpp +161 -25
  61. package/worker/include/RTC/BWE/TargetRateController.hpp +306 -0
  62. package/worker/include/RTC/BWE/TransportWideCcFeedbackGenerator.hpp +146 -0
  63. package/worker/include/RTC/BWE/TrendlineEstimator.hpp +73 -16
  64. package/worker/include/RTC/Consts.hpp +70 -0
  65. package/worker/include/RTC/DataConsumer.hpp +3 -5
  66. package/worker/include/RTC/DirectTransport.hpp +4 -5
  67. package/worker/include/RTC/PipeTransport.hpp +4 -5
  68. package/worker/include/RTC/PlainTransport.hpp +4 -5
  69. package/worker/include/RTC/RTCP/FeedbackPsRemb.hpp +6 -7
  70. package/worker/include/RTC/RTCP/FeedbackRtpTransport.hpp +3 -1
  71. package/worker/include/RTC/RTP/Codecs/Tools.hpp +1 -1
  72. package/worker/include/RTC/RTP/HeaderExtensionIds.hpp +0 -1
  73. package/worker/include/RTC/RTP/Packet.hpp +16 -4
  74. package/worker/include/RTC/RTP/ProbationGenerator.hpp +0 -8
  75. package/worker/include/RTC/RTP/RtpStream.hpp +12 -9
  76. package/worker/include/RTC/RTP/RtpStreamRecv.hpp +22 -4
  77. package/worker/include/RTC/RTP/RtpStreamSend.hpp +4 -2
  78. package/worker/include/RTC/RTP/RtxStream.hpp +10 -4
  79. package/worker/include/RTC/RateCalculator.hpp +103 -24
  80. package/worker/include/RTC/RemoteCaptureTimeEstimator.hpp +33 -3
  81. package/worker/include/RTC/{RtcLogger.hpp → RtcLogger/RtpPacket.hpp} +5 -3
  82. package/worker/include/RTC/RtcLogger/SendBurst.hpp +88 -0
  83. package/worker/include/RTC/RtpDictionaries.hpp +3 -4
  84. package/worker/include/RTC/SCTP/association/Association.hpp +1 -1
  85. package/worker/include/RTC/SCTP/association/StreamResetHandler.hpp +36 -0
  86. package/worker/include/RTC/SCTP/public/AssociationMetrics.hpp +0 -1
  87. package/worker/include/RTC/SCTP/public/SctpTypes.hpp +9 -9
  88. package/worker/include/RTC/SCTP/rx/DataTracker.hpp +1 -1
  89. package/worker/include/RTC/SCTP/rx/ReassemblyQueue.hpp +11 -1
  90. package/worker/include/RTC/SctpDictionaries.hpp +2 -2
  91. package/worker/include/RTC/TcpConnection.hpp +2 -1
  92. package/worker/include/RTC/Transport.hpp +23 -8
  93. package/worker/include/RTC/TransportTuple.hpp +61 -6
  94. package/worker/include/RTC/WebRtcTransport.hpp +4 -5
  95. package/worker/include/Shared.hpp +5 -0
  96. package/worker/include/SharedInterface.hpp +10 -0
  97. package/worker/include/Utils.hpp +72 -3
  98. package/worker/include/common.hpp +12 -4
  99. package/worker/include/handles/SendCallbacks.hpp +34 -0
  100. package/worker/include/handles/TcpConnectionHandle.hpp +4 -12
  101. package/worker/include/handles/UdpSocketHandle.hpp +4 -8
  102. package/worker/meson.build +45 -1
  103. package/worker/meson_options.txt +1 -0
  104. package/worker/mocks/include/MockShared.hpp +16 -2
  105. package/worker/mocks/include/handles/MockBackoffTimerHandle.hpp +1 -1
  106. package/worker/scripts/package-lock.json +6 -6
  107. package/worker/src/RTC/BWE/AimdRateControl.cpp +4 -5
  108. package/worker/src/RTC/BWE/AlrDetector.cpp +80 -0
  109. package/worker/src/RTC/BWE/BitrateProber.cpp +256 -0
  110. package/worker/src/RTC/BWE/BitrateUtils.cpp +54 -0
  111. package/worker/src/RTC/BWE/DelayBasedBwe.cpp +2 -2
  112. package/worker/src/RTC/BWE/FeedbackAdapter.cpp +20 -37
  113. package/worker/src/RTC/BWE/InterArrival.cpp +216 -0
  114. package/worker/src/RTC/BWE/IntervalBudget.cpp +93 -0
  115. package/worker/src/RTC/BWE/LossBasedController.cpp +1194 -0
  116. package/worker/src/RTC/BWE/OveruseDetector.cpp +128 -0
  117. package/worker/src/RTC/BWE/OveruseEstimator.cpp +166 -0
  118. package/worker/src/RTC/BWE/ProbeBitrateEstimator.cpp +222 -0
  119. package/worker/src/RTC/BWE/ProbeController.cpp +645 -0
  120. package/worker/src/RTC/BWE/ProbePacketGenerator.cpp +140 -0
  121. package/worker/src/RTC/BWE/ProbingScheduler.cpp +196 -0
  122. package/worker/src/RTC/BWE/RembGenerator.cpp +169 -0
  123. package/worker/src/RTC/BWE/RemoteBitrateEstimatorAbsSendTime.cpp +497 -0
  124. package/worker/src/RTC/BWE/RobustThroughputEstimator.cpp +47 -0
  125. package/worker/src/RTC/BWE/SendPacketHistory.cpp +173 -50
  126. package/worker/src/RTC/BWE/TargetRateController.cpp +433 -0
  127. package/worker/src/RTC/BWE/TransportWideCcFeedbackGenerator.cpp +377 -0
  128. package/worker/src/RTC/BWE/TrendlineEstimator.cpp +102 -5
  129. package/worker/src/RTC/Consumer.cpp +7 -5
  130. package/worker/src/RTC/DataConsumer.cpp +8 -10
  131. package/worker/src/RTC/DirectTransport.cpp +14 -8
  132. package/worker/src/RTC/DtlsTransport.cpp +8 -8
  133. package/worker/src/RTC/ICE/IceCandidate.cpp +4 -4
  134. package/worker/src/RTC/ICE/IceServer.cpp +1 -1
  135. package/worker/src/RTC/PipeTransport.cpp +13 -8
  136. package/worker/src/RTC/PlainTransport.cpp +13 -8
  137. package/worker/src/RTC/Producer.cpp +14 -31
  138. package/worker/src/RTC/RTCP/Bye.cpp +1 -1
  139. package/worker/src/RTC/RTCP/FeedbackPsRemb.cpp +18 -0
  140. package/worker/src/RTC/RTCP/FeedbackPsRpsi.cpp +2 -2
  141. package/worker/src/RTC/RTCP/FeedbackPsVbcm.cpp +2 -1
  142. package/worker/src/RTC/RTCP/FeedbackRtpTransport.cpp +2 -2
  143. package/worker/src/RTC/RTCP/Sdes.cpp +1 -1
  144. package/worker/src/RTC/RTP/Codecs/DependencyDescriptor.cpp +2 -1
  145. package/worker/src/RTC/RTP/Packet.cpp +26 -11
  146. package/worker/src/RTC/RTP/ProbationGenerator.cpp +5 -6
  147. package/worker/src/RTC/RTP/RtpStream.cpp +1 -1
  148. package/worker/src/RTC/RTP/RtpStreamRecv.cpp +69 -48
  149. package/worker/src/RTC/RTP/RtpStreamSend.cpp +31 -31
  150. package/worker/src/RTC/RTP/RtxStream.cpp +22 -7
  151. package/worker/src/RTC/RateCalculator.cpp +133 -28
  152. package/worker/src/RTC/RemoteCaptureTimeEstimator.cpp +56 -1
  153. package/worker/src/RTC/{RtcLogger.cpp → RtcLogger/RtpPacket.cpp} +2 -2
  154. package/worker/src/RTC/RtcLogger/SendBurst.cpp +124 -0
  155. package/worker/src/RTC/RtpDictionaries/RtpHeaderExtensionUri.cpp +2 -12
  156. package/worker/src/RTC/RtpDictionaries/RtpParameters.cpp +1 -1
  157. package/worker/src/RTC/SCTP/association/Association.cpp +16 -2
  158. package/worker/src/RTC/SCTP/association/StreamResetHandler.cpp +106 -1
  159. package/worker/src/RTC/SCTP/public/AssociationMetrics.cpp +1 -1
  160. package/worker/src/RTC/SCTP/rx/InterleavedReassemblyStreams.cpp +3 -3
  161. package/worker/src/RTC/SCTP/rx/ReassemblyQueue.cpp +2 -1
  162. package/worker/src/RTC/SCTP/rx/TraditionalReassemblyStreams.cpp +4 -4
  163. package/worker/src/RTC/SCTP/tx/RetransmissionTimeout.cpp +4 -4
  164. package/worker/src/RTC/SctpDictionaries/SctpStreamParameters.cpp +6 -10
  165. package/worker/src/RTC/SimulcastProducerStreamManager.cpp +2 -2
  166. package/worker/src/RTC/SrtpSession.cpp +2 -2
  167. package/worker/src/RTC/SvcProducerStreamManager.cpp +2 -2
  168. package/worker/src/RTC/TcpConnection.cpp +2 -2
  169. package/worker/src/RTC/Transport.cpp +112 -75
  170. package/worker/src/RTC/TransportTuple.cpp +4 -4
  171. package/worker/src/RTC/WebRtcServer.cpp +4 -3
  172. package/worker/src/RTC/WebRtcTransport.cpp +20 -13
  173. package/worker/src/Utils/BitStream.cpp +24 -0
  174. package/worker/src/handles/BackoffTimerHandle.cpp +1 -1
  175. package/worker/src/handles/TcpConnectionHandle.cpp +13 -23
  176. package/worker/src/handles/UdpSocketHandle.cpp +14 -21
  177. package/worker/tasks.py +43 -18
  178. package/worker/test/include/RTC/BWE/helpers/LinkSimulator.hpp +68 -14
  179. package/worker/test/include/RTC/BWE/helpers/RemoteBitrateEstimatorAbsSendTimeTestHelper.hpp +125 -0
  180. package/worker/test/src/RTC/BWE/TestAlrDetector.cpp +110 -0
  181. package/worker/test/src/RTC/BWE/TestBitrateProber.cpp +418 -0
  182. package/worker/test/src/RTC/BWE/TestBitrateUtils.cpp +61 -0
  183. package/worker/test/src/RTC/BWE/TestDelayBasedBwe.cpp +4 -4
  184. package/worker/test/src/RTC/BWE/TestFeedbackAdapter.cpp +53 -37
  185. package/worker/test/src/RTC/BWE/TestInterArrival.cpp +643 -0
  186. package/worker/test/src/RTC/BWE/TestIntervalBudget.cpp +130 -0
  187. package/worker/test/src/RTC/BWE/TestLossBasedController.cpp +1199 -0
  188. package/worker/test/src/RTC/BWE/TestOveruseDetector.cpp +519 -0
  189. package/worker/test/src/RTC/BWE/TestProbeBitrateEstimator.cpp +266 -0
  190. package/worker/test/src/RTC/BWE/TestProbeController.cpp +1682 -0
  191. package/worker/test/src/RTC/BWE/TestProbePacketGenerator.cpp +265 -0
  192. package/worker/test/src/RTC/BWE/TestProbingScheduler.cpp +260 -0
  193. package/worker/test/src/RTC/BWE/TestRembGenerator.cpp +285 -0
  194. package/worker/test/src/RTC/BWE/TestRemoteBitrateEstimatorAbsSendTime.cpp +414 -0
  195. package/worker/test/src/RTC/BWE/TestSendPacketHistory.cpp +296 -77
  196. package/worker/test/src/RTC/BWE/TestTargetRateController.cpp +337 -0
  197. package/worker/test/src/RTC/BWE/TestTransportWideCcFeedbackGenerator.cpp +578 -0
  198. package/worker/test/src/RTC/BWE/TestTrendlineEstimator.cpp +45 -0
  199. package/worker/test/src/RTC/BWE/helpers/LinkSimulator.cpp +133 -29
  200. package/worker/test/src/RTC/BWE/helpers/RemoteBitrateEstimatorAbsSendTimeTestHelper.cpp +625 -0
  201. package/worker/test/src/RTC/RTCP/TestFeedbackPsRemb.cpp +19 -0
  202. package/worker/test/src/RTC/RTP/TestPacket.cpp +58 -4
  203. package/worker/test/src/RTC/RTP/TestProbationGenerator.cpp +3 -2
  204. package/worker/test/src/RTC/RTP/TestRtpStreamRecv.cpp +50 -0
  205. package/worker/test/src/RTC/RTP/TestRtpStreamSend.cpp +59 -0
  206. package/worker/test/src/RTC/SCTP/association/TestStreamResetHandler.cpp +111 -0
  207. package/worker/test/src/RTC/TestRateCalculator.cpp +322 -60
  208. package/worker/test/src/RTC/TestRemoteCaptureTimeEstimator.cpp +95 -7
  209. package/worker/test/src/RTC/TestSimpleProducerStreamManager.cpp +34 -16
  210. package/worker/test/src/RTC/TestSimulcastProducerStreamManager.cpp +31 -13
  211. package/worker/test/src/RTC/TestSvcProducerStreamManager.cpp +37 -19
  212. package/worker/test/src/RTC/TestTransportCongestionControlServer.cpp +10 -10
  213. package/worker/test/src/Utils/TestTime.cpp +120 -21
@@ -1,4 +1,3 @@
1
- #include "DepLibUV.hpp"
2
1
  #include "RTC/RTP/Codecs/PayloadDescriptorHandler.hpp"
3
2
  #include "RTC/RTP/RtpStreamRecv.hpp"
4
3
  #include "RTC/SimpleProducerStreamManager.hpp"
@@ -139,12 +138,20 @@ namespace
139
138
  // RtpStreamRecvListener must outlive the RtpStreamRecv.
140
139
  RtpStreamRecvListener streamRecvListener; // NOLINT(readability-identifier-naming)
141
140
 
141
+ // Milliseconds between the packets fed by feedRtpStreamRecv() below.
142
+ constexpr int64_t PacketSpacingMs{ 1 };
143
+
144
+ // Current time, which the helpers below move so that traffic spans a period
145
+ // instead of happening all at once.
146
+ // NOLINTNEXTLINE(readability-identifier-naming)
147
+ int64_t nowUs{ 1000000000000 };
148
+
142
149
  // NOLINTNEXTLINE(readability-identifier-naming)
143
150
  mocks::MockShared shared(/*getTimeUs*/
144
151
  []() -> int64_t
145
152
  {
146
- return DepLibUV::GetTimeUs();
147
- }); // NOLINT(readability-identifier-naming)
153
+ return nowUs;
154
+ });
148
155
 
149
156
  std::unique_ptr<RTC::SimpleProducerStreamManager> createManager(
150
157
  MockListener* listener,
@@ -179,7 +186,9 @@ namespace
179
186
  std::addressof(streamRecvListener), std::addressof(shared), params, 0u, false);
180
187
  }
181
188
 
182
- // Feed packets into the RtpStreamRecv so GetBitrate() returns non-zero.
189
+ // Feed packets into the RtpStreamRecv so GetBitrate() returns non-zero. They
190
+ // are spread in time because a bitrate is measured over the period its samples
191
+ // span, and samples taken all at once span none.
183
192
  void feedRtpStreamRecv(RTC::RTP::RtpStreamRecv* rtpStream, RTC::RTP::Packet* packet, uint16_t count)
184
193
  {
185
194
  auto firstSeq = static_cast<uint16_t>(packet->GetSequenceNumber() + 1);
@@ -187,18 +196,27 @@ namespace
187
196
 
188
197
  for (uint16_t seq = firstSeq; Utils::Number::IsLowerThan<uint16_t>(seq, lastSeq); ++seq)
189
198
  {
199
+ if (seq != firstSeq)
200
+ {
201
+ nowUs += PacketSpacingMs * 1000;
202
+ }
203
+
190
204
  packet->SetSequenceNumber(seq);
191
205
  rtpStream->ReceivePacket(packet, shared.GetTimeUs());
192
206
  }
193
207
 
194
- const int64_t nowMs = DepLibUV::GetTimeMs();
208
+ // The packets span the gaps between them plus the millisecond the first one
209
+ // was taken in.
210
+ const auto periodMs = static_cast<double>(((count - 1) * PacketSpacingMs) + 1);
195
211
 
196
- // bitrate (bps) = totalBytes * 8000 / windowSizeMs.
197
- // windowSizeMs for RtpStreamRecv is 2500.
198
- const auto expectedBitrate =
199
- static_cast<int64_t>(std::trunc((count * packet->GetLength() * 8000.0f / 2500) + 0.5f));
212
+ // bitrate (bps) = bytes * 8000 / periodMs, where the packet that starts the
213
+ // period is left out of the bytes because the period does not cover the time
214
+ // it took to arrive.
215
+ const auto expectedBitrate = static_cast<int64_t>(std::trunc(
216
+ ((static_cast<double>(count - 1) * static_cast<double>(packet->GetLength()) * 8000.0) / periodMs) +
217
+ 0.5));
200
218
 
201
- REQUIRE(rtpStream->GetBitrate(nowMs) == expectedBitrate);
219
+ REQUIRE(rtpStream->GetBitrate(shared.GetTimeMs()) == expectedBitrate);
202
220
  }
203
221
  } // namespace
204
222
 
@@ -411,7 +429,7 @@ SCENARIO("SimpleProducerStreamManager", "[rtp][producerstreammanager][simple]")
411
429
  // Feed packets so the stream has non-zero bitrate.
412
430
  feedRtpStreamRecv(rtpStream.get(), packet.get(), 100);
413
431
 
414
- const int64_t nowMs = DepLibUV::GetTimeMs();
432
+ const int64_t nowMs = shared.GetTimeMs();
415
433
  const auto streamBitrate = rtpStream->GetBitrate(nowMs);
416
434
  const auto usedBitrate = manager->IncreaseLayer(
417
435
  /*bitrate*/ streamBitrate + 1, /*considerLoss*/ false, /*lossPercentage*/ 0.0f, nowMs);
@@ -432,7 +450,7 @@ SCENARIO("SimpleProducerStreamManager", "[rtp][producerstreammanager][simple]")
432
450
  // Feed packets so the stream has non-zero bitrate.
433
451
  feedRtpStreamRecv(rtpStream.get(), packet.get(), 100);
434
452
 
435
- const int64_t nowMs = DepLibUV::GetTimeMs();
453
+ const int64_t nowMs = shared.GetTimeMs();
436
454
  const auto streamBitrate = rtpStream->GetBitrate(nowMs);
437
455
  const int64_t availableBitrate = streamBitrate - 1;
438
456
  auto usedBitrate = manager->IncreaseLayer(
@@ -454,7 +472,7 @@ SCENARIO("SimpleProducerStreamManager", "[rtp][producerstreammanager][simple]")
454
472
  // Feed packets so the stream has non-zero bitrate.
455
473
  feedRtpStreamRecv(rtpStream.get(), packet.get(), 100);
456
474
 
457
- const int64_t nowMs = DepLibUV::GetTimeMs();
475
+ const int64_t nowMs = shared.GetTimeMs();
458
476
 
459
477
  // First call claims bitrate.
460
478
  auto usedBitrate = manager->IncreaseLayer(
@@ -482,7 +500,7 @@ SCENARIO("SimpleProducerStreamManager", "[rtp][producerstreammanager][simple]")
482
500
  // Feed packets so the stream has non-zero bitrate.
483
501
  feedRtpStreamRecv(rtpStream.get(), packet.get(), 100);
484
502
 
485
- const int64_t nowMs = DepLibUV::GetTimeMs();
503
+ const int64_t nowMs = shared.GetTimeMs();
486
504
 
487
505
  // First iteration: claim bitrate and apply.
488
506
  manager->IncreaseLayer(
@@ -509,7 +527,7 @@ SCENARIO("SimpleProducerStreamManager", "[rtp][producerstreammanager][simple]")
509
527
  // Feed packets so the stream has non-zero bitrate.
510
528
  feedRtpStreamRecv(rtpStream.get(), packet.get(), 100);
511
529
 
512
- const int64_t nowMs = DepLibUV::GetTimeMs();
530
+ const int64_t nowMs = shared.GetTimeMs();
513
531
  const auto streamBitrate = rtpStream->GetBitrate(nowMs);
514
532
  const auto desiredBitrate = manager->GetDesiredBitrate(nowMs);
515
533
 
@@ -530,7 +548,7 @@ SCENARIO("SimpleProducerStreamManager", "[rtp][producerstreammanager][simple]")
530
548
  // Feed packets so the stream has non-zero bitrate.
531
549
  feedRtpStreamRecv(rtpStream.get(), packet.get(), 100);
532
550
 
533
- const int64_t nowMs = DepLibUV::GetTimeMs();
551
+ const int64_t nowMs = shared.GetTimeMs();
534
552
  auto desiredBitrate = manager->GetDesiredBitrate(nowMs);
535
553
 
536
554
  REQUIRE(desiredBitrate == 0);
@@ -1,4 +1,3 @@
1
- #include "DepLibUV.hpp"
2
1
  #include "RTC/RTCP/SenderReport.hpp"
3
2
  #include "RTC/RTP/Codecs/PayloadDescriptorHandler.hpp"
4
3
  #include "RTC/RTP/RtpStreamRecv.hpp"
@@ -148,12 +147,20 @@ namespace
148
147
  // RtpStreamRecvListener must outlive the RtpStreamRecv.
149
148
  RtpStreamRecvListener streamRecvListener; // NOLINT(readability-identifier-naming)
150
149
 
150
+ // Milliseconds between the packets fed by feedRtpStreamRecv() below.
151
+ constexpr int64_t PacketSpacingMs{ 1 };
152
+
153
+ // Current time, which the helpers below move so that traffic spans a period
154
+ // instead of happening all at once.
155
+ // NOLINTNEXTLINE(readability-identifier-naming)
156
+ int64_t nowUs{ 1000000000000 };
157
+
151
158
  // NOLINTNEXTLINE(readability-identifier-naming)
152
159
  mocks::MockShared shared(/*getTimeUs*/
153
160
  []() -> int64_t
154
161
  {
155
- return DepLibUV::GetTimeUs();
156
- }); // NOLINT(readability-identifier-naming)
162
+ return nowUs;
163
+ });
157
164
 
158
165
  std::unique_ptr<RTC::SimulcastProducerStreamManager> createManager(
159
166
  MockListener* listener,
@@ -198,7 +205,9 @@ namespace
198
205
  std::addressof(streamRecvListener), std::addressof(shared), params, 0u, false);
199
206
  }
200
207
 
201
- // Feed packets into the RtpStreamRecv so GetBitrate() returns non-zero.
208
+ // Feed packets into the RtpStreamRecv so GetBitrate() returns non-zero. They
209
+ // are spread in time because a bitrate is measured over the period its samples
210
+ // span, and samples taken all at once span none.
202
211
  void feedRtpStreamRecv(RTC::RTP::RtpStreamRecv* rtpStream, RTC::RTP::Packet* packet, uint16_t count)
203
212
  {
204
213
  auto firstSeq = static_cast<uint16_t>(packet->GetSequenceNumber() + 1);
@@ -206,18 +215,27 @@ namespace
206
215
 
207
216
  for (uint16_t seq = firstSeq; Utils::Number::IsLowerThan<uint16_t>(seq, lastSeq); ++seq)
208
217
  {
218
+ if (seq != firstSeq)
219
+ {
220
+ nowUs += PacketSpacingMs * 1000;
221
+ }
222
+
209
223
  packet->SetSequenceNumber(seq);
210
224
  rtpStream->ReceivePacket(packet, shared.GetTimeUs());
211
225
  }
212
226
 
213
- const int64_t nowMs = DepLibUV::GetTimeMs();
227
+ // The packets span the gaps between them plus the millisecond the first one
228
+ // was taken in.
229
+ const auto periodMs = static_cast<double>(((count - 1) * PacketSpacingMs) + 1);
214
230
 
215
- // bitrate (bps) = totalBytes * 8000 / windowSizeMs.
216
- // windowSizeMs for RtpStreamRecv is 2500.
217
- const auto expectedBitrate =
218
- static_cast<int64_t>(std::trunc((count * packet->GetLength() * 8000.0f / 2500) + 0.5f));
231
+ // bitrate (bps) = bytes * 8000 / periodMs, where the packet that starts the
232
+ // period is left out of the bytes because the period does not cover the time
233
+ // it took to arrive.
234
+ const auto expectedBitrate = static_cast<int64_t>(std::trunc(
235
+ ((static_cast<double>(count - 1) * static_cast<double>(packet->GetLength()) * 8000.0) / periodMs) +
236
+ 0.5));
219
237
 
220
- REQUIRE(rtpStream->GetBitrate(nowMs) == expectedBitrate);
238
+ REQUIRE(rtpStream->GetBitrate(shared.GetTimeMs()) == expectedBitrate);
221
239
  }
222
240
  } // namespace
223
241
 
@@ -1236,7 +1254,7 @@ SCENARIO("SimulcastProducerStreamManager", "[rtp][producerstreammanager][simulca
1236
1254
  packet->SetSsrc(MappedSsrc0);
1237
1255
  feedRtpStreamRecv(rtpStream0.get(), packet.get(), 100);
1238
1256
 
1239
- const int64_t nowMs = DepLibUV::GetTimeMs();
1257
+ const int64_t nowMs = shared.GetTimeMs();
1240
1258
 
1241
1259
  // First call claims bitrate.
1242
1260
  auto usedBitrate = manager->IncreaseLayer(
@@ -1267,7 +1285,7 @@ SCENARIO("SimulcastProducerStreamManager", "[rtp][producerstreammanager][simulca
1267
1285
  packet->SetSsrc(MappedSsrc0);
1268
1286
  feedRtpStreamRecv(rtpStream0.get(), packet.get(), 100);
1269
1287
 
1270
- const int64_t nowMs = DepLibUV::GetTimeMs();
1288
+ const int64_t nowMs = shared.GetTimeMs();
1271
1289
 
1272
1290
  // First iteration: claim layer 0.
1273
1291
  manager->IncreaseLayer(
@@ -1304,7 +1322,7 @@ SCENARIO("SimulcastProducerStreamManager", "[rtp][producerstreammanager][simulca
1304
1322
  packet->SetSsrc(MappedSsrc1);
1305
1323
  feedRtpStreamRecv(rtpStream1.get(), packet.get(), 100);
1306
1324
 
1307
- const int64_t nowMs = DepLibUV::GetTimeMs();
1325
+ const int64_t nowMs = shared.GetTimeMs();
1308
1326
  auto bitrate0 = rtpStream0->GetBitrate(nowMs);
1309
1327
  auto bitrate1 = rtpStream1->GetBitrate(nowMs);
1310
1328
 
@@ -1,4 +1,3 @@
1
- #include "DepLibUV.hpp"
2
1
  #include "RTC/RTP/Codecs/PayloadDescriptorHandler.hpp"
3
2
  #include "RTC/RTP/RtpStreamRecv.hpp"
4
3
  #include "RTC/SvcProducerStreamManager.hpp"
@@ -154,12 +153,20 @@ namespace
154
153
  // RtpStreamRecvListener must outlive the RtpStreamRecv.
155
154
  RtpStreamRecvListener streamRecvListener; // NOLINT(readability-identifier-naming)
156
155
 
156
+ // Milliseconds between the packets fed by feedRtpStreamRecv() below.
157
+ constexpr int64_t PacketSpacingMs{ 1 };
158
+
159
+ // Current time, which the helpers below move so that traffic spans a period
160
+ // instead of happening all at once.
161
+ // NOLINTNEXTLINE(readability-identifier-naming)
162
+ int64_t nowUs{ 1000000000000 };
163
+
157
164
  // NOLINTNEXTLINE(readability-identifier-naming)
158
165
  mocks::MockShared shared(/*getTimeUs*/
159
166
  []() -> int64_t
160
167
  {
161
- return DepLibUV::GetTimeUs();
162
- }); // NOLINT(readability-identifier-naming)
168
+ return nowUs;
169
+ });
163
170
 
164
171
  std::unique_ptr<RTC::SvcProducerStreamManager> createManager(
165
172
  MockListener* listener,
@@ -201,7 +208,9 @@ namespace
201
208
  std::addressof(streamRecvListener), std::addressof(shared), params, 0u, false);
202
209
  }
203
210
 
204
- // Feed packets into the RtpStreamRecv so GetBitrate() returns non-zero.
211
+ // Feed packets into the RtpStreamRecv so GetBitrate() returns non-zero. They
212
+ // are spread in time because a bitrate is measured over the period its samples
213
+ // span, and samples taken all at once span none.
205
214
  void feedRtpStreamRecv(RTC::RTP::RtpStreamRecv* rtpStream, RTC::RTP::Packet* packet, uint16_t count)
206
215
  {
207
216
  auto firstSeq = static_cast<uint16_t>(packet->GetSequenceNumber() + 1);
@@ -209,18 +218,27 @@ namespace
209
218
 
210
219
  for (uint16_t seq = firstSeq; Utils::Number::IsLowerThan<uint16_t>(seq, lastSeq); ++seq)
211
220
  {
221
+ if (seq != firstSeq)
222
+ {
223
+ nowUs += PacketSpacingMs * 1000;
224
+ }
225
+
212
226
  packet->SetSequenceNumber(seq);
213
227
  rtpStream->ReceivePacket(packet, shared.GetTimeUs());
214
228
  }
215
229
 
216
- const int64_t nowMs = DepLibUV::GetTimeMs();
230
+ // The packets span the gaps between them plus the millisecond the first one
231
+ // was taken in.
232
+ const auto periodMs = static_cast<double>(((count - 1) * PacketSpacingMs) + 1);
217
233
 
218
- // bitrate (bps) = totalBytes * 8000 / windowSizeMs.
219
- // windowSizeMs for RtpStreamRecv is 2500.
220
- const auto expectedBitrate =
221
- static_cast<int64_t>(std::trunc((count * packet->GetLength() * 8000.0f / 2500) + 0.5f));
234
+ // bitrate (bps) = bytes * 8000 / periodMs, where the packet that starts the
235
+ // period is left out of the bytes because the period does not cover the time
236
+ // it took to arrive.
237
+ const auto expectedBitrate = static_cast<int64_t>(std::trunc(
238
+ ((static_cast<double>(count - 1) * static_cast<double>(packet->GetLength()) * 8000.0) / periodMs) +
239
+ 0.5));
222
240
 
223
- REQUIRE(rtpStream->GetBitrate(nowMs) == expectedBitrate);
241
+ REQUIRE(rtpStream->GetBitrate(shared.GetTimeMs()) == expectedBitrate);
224
242
  }
225
243
  } // namespace
226
244
 
@@ -943,7 +961,7 @@ SCENARIO("SvcProducerStreamManager", "[rtp][producerstreammanager][svc]")
943
961
 
944
962
  manager->SetExternallyManagedBitrate();
945
963
 
946
- const int64_t nowMs = DepLibUV::GetTimeMs();
964
+ const int64_t nowMs = shared.GetTimeMs();
947
965
  auto usedBitrate = manager->IncreaseLayer(
948
966
  /*bitrate*/ 1000000, /*considerLoss*/ false, /*lossPercentage*/ 0.0f, nowMs);
949
967
 
@@ -960,7 +978,7 @@ SCENARIO("SvcProducerStreamManager", "[rtp][producerstreammanager][svc]")
960
978
  manager->SetExternallyManagedBitrate();
961
979
 
962
980
  // Score is 0 by default (no RTP received + inactivity check disabled).
963
- const int64_t nowMs = DepLibUV::GetTimeMs();
981
+ const int64_t nowMs = shared.GetTimeMs();
964
982
  auto usedBitrate = manager->IncreaseLayer(
965
983
  /*bitrate*/ 1000000, /*considerLoss*/ false, /*lossPercentage*/ 0.0f, nowMs);
966
984
 
@@ -987,7 +1005,7 @@ SCENARIO("SvcProducerStreamManager", "[rtp][producerstreammanager][svc]")
987
1005
  // Feed packets so the stream has non-zero bitrate.
988
1006
  feedRtpStreamRecv(rtpStream.get(), packet.get(), 100);
989
1007
 
990
- const int64_t nowMs = DepLibUV::GetTimeMs();
1008
+ const int64_t nowMs = shared.GetTimeMs();
991
1009
 
992
1010
  // First call claims bitrate.
993
1011
  auto usedBitrate = manager->IncreaseLayer(
@@ -1021,7 +1039,7 @@ SCENARIO("SvcProducerStreamManager", "[rtp][producerstreammanager][svc]")
1021
1039
  // Feed packets so the stream has non-zero bitrate.
1022
1040
  feedRtpStreamRecv(rtpStream.get(), packet.get(), 100);
1023
1041
 
1024
- const int64_t nowMs = DepLibUV::GetTimeMs();
1042
+ const int64_t nowMs = shared.GetTimeMs();
1025
1043
 
1026
1044
  // First iteration.
1027
1045
  manager->IncreaseLayer(
@@ -1082,7 +1100,7 @@ SCENARIO("SvcProducerStreamManager", "[rtp][producerstreammanager][svc]")
1082
1100
  MockListener listener;
1083
1101
  auto manager = createManager(std::addressof(listener));
1084
1102
 
1085
- const int64_t nowMs = DepLibUV::GetTimeMs();
1103
+ const int64_t nowMs = shared.GetTimeMs();
1086
1104
  auto desiredBitrate = manager->GetDesiredBitrate(nowMs);
1087
1105
 
1088
1106
  REQUIRE(desiredBitrate == 0);
@@ -1106,7 +1124,7 @@ SCENARIO("SvcProducerStreamManager", "[rtp][producerstreammanager][svc]")
1106
1124
 
1107
1125
  feedRtpStreamRecv(rtpStream.get(), packet.get(), 100);
1108
1126
 
1109
- const int64_t nowMs = DepLibUV::GetTimeMs();
1127
+ const int64_t nowMs = shared.GetTimeMs();
1110
1128
  auto streamBitrate = rtpStream->GetBitrate(nowMs);
1111
1129
  auto desiredBitrate = manager->GetDesiredBitrate(nowMs);
1112
1130
 
@@ -1132,7 +1150,7 @@ SCENARIO("SvcProducerStreamManager", "[rtp][producerstreammanager][svc]")
1132
1150
  // Feed packets so total stream bitrate > 0.
1133
1151
  feedRtpStreamRecv(rtpStream.get(), packet.get(), 100);
1134
1152
 
1135
- const int64_t nowMs = DepLibUV::GetTimeMs();
1153
+ const int64_t nowMs = shared.GetTimeMs();
1136
1154
  auto desiredBitrate = manager->GetDesiredBitrate(nowMs);
1137
1155
 
1138
1156
  // K-SVC returns the max per-spatial-layer bitrate. With a plain RtpStreamRecv
@@ -1243,7 +1261,7 @@ SCENARIO("SvcProducerStreamManager", "[rtp][producerstreammanager][svc]")
1243
1261
 
1244
1262
  feedRtpStreamRecv(rtpStream.get(), packet.get(), 100);
1245
1263
 
1246
- const int64_t nowMs = DepLibUV::GetTimeMs();
1264
+ const int64_t nowMs = shared.GetTimeMs();
1247
1265
  auto streamBitrate = rtpStream->GetBitrate(nowMs);
1248
1266
 
1249
1267
  // With 1% loss virtualBitrate = 1.08 * bitrate, so we can afford the stream
@@ -1274,7 +1292,7 @@ SCENARIO("SvcProducerStreamManager", "[rtp][producerstreammanager][svc]")
1274
1292
 
1275
1293
  feedRtpStreamRecv(rtpStream.get(), packet.get(), 100);
1276
1294
 
1277
- const int64_t nowMs = DepLibUV::GetTimeMs();
1295
+ const int64_t nowMs = shared.GetTimeMs();
1278
1296
  auto streamBitrate = rtpStream->GetBitrate(nowMs);
1279
1297
 
1280
1298
  // With 50% loss virtualBitrate = (1 - 0.5*0.5) * bitrate = 0.75 * bitrate.
@@ -186,10 +186,10 @@ SCENARIO("TransportCongestionControlServer", "[rtp]")
186
186
  // clang-format off
187
187
  std::vector<TestTransportCongestionControlServerInput> inputs
188
188
  {
189
- { 1u, 1000000 },
190
- { 3u, 1050000 },
191
- { 5u, 1100000 },
192
- { 6u, 1150000 },
189
+ { 1u, 1000000 },
190
+ { 3u, 1050000 },
191
+ { 5u, 1100000 },
192
+ { 6u, 1150000 },
193
193
  };
194
194
 
195
195
  TestResults results
@@ -215,12 +215,12 @@ SCENARIO("TransportCongestionControlServer", "[rtp]")
215
215
  // clang-format off
216
216
  std::vector<TestTransportCongestionControlServerInput> inputs
217
217
  {
218
- { 1u, 1000000 },
219
- { 1u, 1050000 },
220
- { 2u, 1100000 },
221
- { 3u, 1150000 },
222
- { 3u, 1200000 },
223
- { 4u, 1250000 },
218
+ { 1u, 1000000 },
219
+ { 1u, 1050000 },
220
+ { 2u, 1100000 },
221
+ { 3u, 1150000 },
222
+ { 3u, 1200000 },
223
+ { 4u, 1250000 },
224
224
  };
225
225
 
226
226
  TestResults results
@@ -2,39 +2,138 @@
2
2
  #include "DepLibUV.hpp"
3
3
  #include "Utils.hpp"
4
4
  #include <catch2/catch_test_macros.hpp>
5
+ #include <cstdlib> // std::abs()
6
+ #include <limits>
5
7
 
6
8
  SCENARIO("Utils::Time", "[utils][time]")
7
9
  {
8
- SECTION("Ntp2TimeUs()")
10
+ SECTION("TimeUsToMs()")
11
+ {
12
+ REQUIRE(Utils::Time::TimeUsToMs(0) == 0);
13
+ REQUIRE(Utils::Time::TimeUsToMs(1000) == 1);
14
+ REQUIRE(Utils::Time::TimeUsToMs(1499) == 1);
15
+ // Halves upwards.
16
+ REQUIRE(Utils::Time::TimeUsToMs(1500) == 2);
17
+ REQUIRE(Utils::Time::TimeUsToMs(1501) == 2);
18
+ REQUIRE(Utils::Time::TimeUsToMs(3990000000750000) == 3990000000750);
19
+
20
+ // A negative instant rounds the same way, so halves still go upwards rather
21
+ // than towards zero.
22
+ REQUIRE(Utils::Time::TimeUsToMs(-1000) == -1);
23
+ REQUIRE(Utils::Time::TimeUsToMs(-1499) == -1);
24
+ REQUIRE(Utils::Time::TimeUsToMs(-1500) == -1);
25
+ REQUIRE(Utils::Time::TimeUsToMs(-1501) == -2);
26
+ REQUIRE(Utils::Time::TimeUsToMs(-500) == 0);
27
+ REQUIRE(Utils::Time::TimeUsToMs(-501) == -1);
28
+
29
+ // The extremes are rounded rather than overflowing.
30
+ REQUIRE(Utils::Time::TimeUsToMs(std::numeric_limits<int64_t>::max()) == 9223372036854776LL);
31
+ REQUIRE(Utils::Time::TimeUsToMs(std::numeric_limits<int64_t>::min()) == -9223372036854776LL);
32
+ }
33
+
34
+ SECTION("NtpToTimeUs()")
9
35
  {
10
36
  const auto nowUs = DepLibUV::GetTimeUs();
11
- const auto ntp = Utils::Time::TimeUs2Ntp(nowUs);
12
- const auto nowUs2 = Utils::Time::Ntp2TimeUs(ntp);
13
- const auto ntp2 = Utils::Time::TimeUs2Ntp(nowUs2);
37
+ const auto ntp = Utils::Time::TimeUsToNtp(nowUs);
38
+ const auto nowUs2 = Utils::Time::NtpToTimeUs(ntp);
39
+ const auto ntp2 = Utils::Time::TimeUsToNtp(nowUs2);
14
40
 
15
41
  REQUIRE(nowUs2 == nowUs);
16
42
  REQUIRE(ntp2.seconds == ntp.seconds);
17
43
  REQUIRE(ntp2.fractions == ntp.fractions);
18
44
  }
19
45
 
20
- SECTION("TimeUs2Ntp()")
46
+ SECTION("TimeUsToNtp()")
21
47
  {
22
- auto ntp = Utils::Time::TimeUs2Ntp(1500000);
48
+ auto ntp = Utils::Time::TimeUsToNtp(1500000);
23
49
 
24
50
  REQUIRE(ntp.seconds == 1);
25
51
  // Half a second in NTP fractional units.
26
52
  REQUIRE(ntp.fractions == 2147483648);
27
53
 
28
54
  // A real NTP instant, seconds since Jan 1, 1900, which still fits in 32 bits.
29
- ntp = Utils::Time::TimeUs2Ntp(3990000000750000);
55
+ ntp = Utils::Time::TimeUsToNtp(3990000000750000);
30
56
 
31
57
  REQUIRE(ntp.seconds == 3990000000);
32
- REQUIRE(Utils::Time::Ntp2TimeUs(ntp) == 3990000000750000);
58
+ REQUIRE(Utils::Time::NtpToTimeUs(ntp) == 3990000000750000);
33
59
 
34
60
  // Sub-millisecond times are kept.
35
- REQUIRE(Utils::Time::TimeUs2Ntp(1000500).fractions > Utils::Time::TimeUs2Ntp(1000000).fractions);
36
- REQUIRE(Utils::Time::TimeUs2Ntp(1000500).fractions < Utils::Time::TimeUs2Ntp(1001000).fractions);
37
- REQUIRE(Utils::Time::Ntp2TimeUs(Utils::Time::TimeUs2Ntp(1000500)) == 1000500);
61
+ REQUIRE(Utils::Time::TimeUsToNtp(1000500).fractions > Utils::Time::TimeUsToNtp(1000000).fractions);
62
+ REQUIRE(Utils::Time::TimeUsToNtp(1000500).fractions < Utils::Time::TimeUsToNtp(1001000).fractions);
63
+ REQUIRE(Utils::Time::NtpToTimeUs(Utils::Time::TimeUsToNtp(1000500)) == 1000500);
64
+ }
65
+
66
+ // Middle 32 bits of the given NTP timestamp.
67
+ auto toCompactNtp = [](uint32_t seconds, uint32_t fractions) -> uint32_t
68
+ {
69
+ return (seconds << 16) | (fractions >> 16);
70
+ };
71
+
72
+ auto toTimeUs = [](uint32_t seconds, uint32_t fractions) -> int64_t
73
+ {
74
+ return Utils::Time::NtpToTimeUs(Utils::Time::Ntp{ .seconds = seconds, .fractions = fractions });
75
+ };
76
+
77
+ SECTION("CompactNtpIntervalToTimeUs()")
78
+ {
79
+ {
80
+ const int64_t diffUs = toTimeUs(0x12654, 0x64335) - toTimeUs(0x12345, 0x23456);
81
+ const uint32_t compactNtpDiff = toCompactNtp(0x12654, 0x64335) - toCompactNtp(0x12345, 0x23456);
82
+
83
+ REQUIRE(std::abs(Utils::Time::CompactNtpIntervalToTimeUs(compactNtpDiff) - diffUs) <= 1000);
84
+ }
85
+
86
+ // The later timestamp has the lower compact NTP representation, which is fine as
87
+ // long as the difference is computed with unsigned arithmetic.
88
+ {
89
+ const int64_t diffUs = toTimeUs(0x20000, 0x64335) - toTimeUs(0x1ffff, 0x23456);
90
+
91
+ REQUIRE(diffUs > 0);
92
+ REQUIRE(toCompactNtp(0x20000, 0x64335) < toCompactNtp(0x1ffff, 0x23456));
93
+
94
+ const uint32_t compactNtpDiff = toCompactNtp(0x20000, 0x64335) - toCompactNtp(0x1ffff, 0x23456);
95
+
96
+ REQUIRE(std::abs(Utils::Time::CompactNtpIntervalToTimeUs(compactNtpDiff) - diffUs) <= 1000);
97
+ }
98
+
99
+ // A difference close to 2^16 seconds is a negative one.
100
+ {
101
+ const int64_t diffUs = toTimeUs(0x1ffff, 0x64335) - toTimeUs(0x20000, 0x23456);
102
+
103
+ REQUIRE(diffUs < 0);
104
+
105
+ const uint32_t compactNtpDiff = toCompactNtp(0x1ffff, 0x64335) - toCompactNtp(0x20000, 0x23456);
106
+
107
+ REQUIRE(std::abs(Utils::Time::CompactNtpIntervalToTimeUs(compactNtpDiff) - diffUs) <= 1000);
108
+ }
109
+
110
+ // Right in the middle, both +2^15 and -2^15 seconds are valid results.
111
+ REQUIRE(std::abs(Utils::Time::CompactNtpIntervalToTimeUs(0x80000000)) == 0x8000 * 1000000LL);
112
+ }
113
+
114
+ SECTION("CompactNtpRttToTimeUs()")
115
+ {
116
+ // A difference close to 2^15 seconds is still a positive one.
117
+ {
118
+ const int64_t diffUs = toTimeUs(0x17fff, 0xffff5) - toTimeUs(0x10000, 0x00006);
119
+
120
+ REQUIRE(std::abs(diffUs - (((1 << 15) - 1) * 1000000LL)) <= 1000);
121
+
122
+ const uint32_t compactNtpDiff = toCompactNtp(0x17fff, 0xffff5) - toCompactNtp(0x10000, 0x00006);
123
+
124
+ REQUIRE(std::abs(Utils::Time::CompactNtpRttToTimeUs(compactNtpDiff) - diffUs) <= 1000);
125
+ }
126
+
127
+ // A negative round trip time yields 1 millisecond.
128
+ {
129
+ const int64_t diffUs = toTimeUs(0x1ffff, 0x64335) - toTimeUs(0x20000, 0x23456);
130
+
131
+ REQUIRE(diffUs < 0);
132
+
133
+ const uint32_t compactNtpDiff = toCompactNtp(0x1ffff, 0x64335) - toCompactNtp(0x20000, 0x23456);
134
+
135
+ REQUIRE(Utils::Time::CompactNtpRttToTimeUs(compactNtpDiff) == 1000);
136
+ }
38
137
  }
39
138
 
40
139
  SECTION("TimeUsToAbsSendTime()")
@@ -63,25 +162,25 @@ SCENARIO("Utils::Time", "[utils][time]")
63
162
  REQUIRE(Utils::Time::TimeUsToAbsSendTime(-WrapPeriodUs) == 0);
64
163
  }
65
164
 
66
- SECTION("TimeUs2Q32x32()")
165
+ SECTION("TimeUsToQ32x32()")
67
166
  {
68
167
  // A whole second is the fractional unit itself.
69
168
  // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
70
- REQUIRE(Utils::Time::TimeUs2Q32x32(1000000).value() == 4294967296);
169
+ REQUIRE(Utils::Time::TimeUsToQ32x32(1000000).value() == 4294967296);
71
170
  // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
72
- REQUIRE(Utils::Time::TimeUs2Q32x32(-1000000).value() == -4294967296);
171
+ REQUIRE(Utils::Time::TimeUsToQ32x32(-1000000).value() == -4294967296);
73
172
  // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
74
- REQUIRE(Utils::Time::TimeUs2Q32x32(0).value() == 0);
173
+ REQUIRE(Utils::Time::TimeUsToQ32x32(0).value() == 0);
75
174
  // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
76
- REQUIRE(Utils::Time::TimeUs2Q32x32(1000).value() == 4294967);
175
+ REQUIRE(Utils::Time::TimeUsToQ32x32(1000).value() == 4294967);
77
176
 
78
177
  // Seconds are 32 bits wide in the format, so 2^31 seconds no longer fit.
79
178
  constexpr int64_t OutOfRangeUs{ (1LL << 31) * 1000000 };
80
179
 
81
- REQUIRE(Utils::Time::TimeUs2Q32x32(OutOfRangeUs) == std::nullopt);
82
- REQUIRE(Utils::Time::TimeUs2Q32x32(-OutOfRangeUs) == std::nullopt);
83
- REQUIRE(Utils::Time::TimeUs2Q32x32(OutOfRangeUs - 1).has_value());
84
- REQUIRE(Utils::Time::TimeUs2Q32x32(-OutOfRangeUs + 1).has_value());
180
+ REQUIRE(Utils::Time::TimeUsToQ32x32(OutOfRangeUs) == std::nullopt);
181
+ REQUIRE(Utils::Time::TimeUsToQ32x32(-OutOfRangeUs) == std::nullopt);
182
+ REQUIRE(Utils::Time::TimeUsToQ32x32(OutOfRangeUs - 1).has_value());
183
+ REQUIRE(Utils::Time::TimeUsToQ32x32(-OutOfRangeUs + 1).has_value());
85
184
  }
86
185
 
87
186
  SECTION("Q32x32ToTimeUs()")
@@ -93,7 +192,7 @@ SCENARIO("Utils::Time", "[utils][time]")
93
192
  for (const int64_t us : { 1, -1, 1000, -1000, 1000000, -1000000, 123456789, -123456789 })
94
193
  {
95
194
  // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
96
- REQUIRE(Utils::Time::Q32x32ToTimeUs(Utils::Time::TimeUs2Q32x32(us).value()) == us);
195
+ REQUIRE(Utils::Time::Q32x32ToTimeUs(Utils::Time::TimeUsToQ32x32(us).value()) == us);
97
196
  }
98
197
  }
99
198
  }