mediasoup 3.27.1 → 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 (203) hide show
  1. package/CHANGELOG.md +14 -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/rtpParameters.fbs +0 -1
  38. package/worker/fuzzer/src/FuzzerUtils.cpp +5 -3
  39. package/worker/fuzzer/src/RTC/RTP/FuzzerPacket.cpp +16 -2
  40. package/worker/include/DepLibUV.hpp +13 -0
  41. package/worker/include/RTC/BWE/AlrDetector.hpp +97 -0
  42. package/worker/include/RTC/BWE/BitrateProber.hpp +217 -0
  43. package/worker/include/RTC/BWE/{Utils.hpp → BitrateUtils.hpp} +3 -3
  44. package/worker/include/RTC/BWE/BweTypes.hpp +170 -2
  45. package/worker/include/RTC/BWE/FeedbackAdapter.hpp +0 -14
  46. package/worker/include/RTC/BWE/InterArrival.hpp +117 -0
  47. package/worker/include/RTC/BWE/IntervalBudget.hpp +81 -0
  48. package/worker/include/RTC/BWE/LossBasedController.hpp +105 -0
  49. package/worker/include/RTC/BWE/OveruseDetector.hpp +71 -0
  50. package/worker/include/RTC/BWE/OveruseEstimator.hpp +115 -0
  51. package/worker/include/RTC/BWE/ProbeBitrateEstimator.hpp +84 -0
  52. package/worker/include/RTC/BWE/ProbeController.hpp +379 -0
  53. package/worker/include/RTC/BWE/ProbePacketGenerator.hpp +130 -0
  54. package/worker/include/RTC/BWE/ProbingScheduler.hpp +187 -0
  55. package/worker/include/RTC/BWE/RembGenerator.hpp +96 -0
  56. package/worker/include/RTC/BWE/RemoteBitrateEstimatorAbsSendTime.hpp +237 -0
  57. package/worker/include/RTC/BWE/RobustThroughputEstimator.hpp +26 -0
  58. package/worker/include/RTC/BWE/SendPacketHistory.hpp +161 -25
  59. package/worker/include/RTC/BWE/TransportWideCcFeedbackGenerator.hpp +146 -0
  60. package/worker/include/RTC/BWE/TrendlineEstimator.hpp +15 -0
  61. package/worker/include/RTC/Consts.hpp +70 -0
  62. package/worker/include/RTC/DataConsumer.hpp +3 -5
  63. package/worker/include/RTC/DirectTransport.hpp +4 -5
  64. package/worker/include/RTC/PipeTransport.hpp +4 -5
  65. package/worker/include/RTC/PlainTransport.hpp +4 -5
  66. package/worker/include/RTC/RTCP/FeedbackPsRemb.hpp +6 -7
  67. package/worker/include/RTC/RTCP/FeedbackRtpTransport.hpp +3 -1
  68. package/worker/include/RTC/RTP/Codecs/Tools.hpp +1 -1
  69. package/worker/include/RTC/RTP/HeaderExtensionIds.hpp +0 -1
  70. package/worker/include/RTC/RTP/Packet.hpp +16 -4
  71. package/worker/include/RTC/RTP/ProbationGenerator.hpp +0 -8
  72. package/worker/include/RTC/RTP/RtpStream.hpp +12 -9
  73. package/worker/include/RTC/RTP/RtpStreamRecv.hpp +22 -4
  74. package/worker/include/RTC/RTP/RtpStreamSend.hpp +4 -2
  75. package/worker/include/RTC/RTP/RtxStream.hpp +10 -4
  76. package/worker/include/RTC/RateCalculator.hpp +103 -24
  77. package/worker/include/RTC/RemoteCaptureTimeEstimator.hpp +33 -3
  78. package/worker/include/RTC/{RtcLogger.hpp → RtcLogger/RtpPacket.hpp} +5 -3
  79. package/worker/include/RTC/RtcLogger/SendBurst.hpp +88 -0
  80. package/worker/include/RTC/RtpDictionaries.hpp +3 -4
  81. package/worker/include/RTC/SCTP/association/Association.hpp +1 -1
  82. package/worker/include/RTC/SCTP/public/AssociationMetrics.hpp +0 -1
  83. package/worker/include/RTC/SCTP/public/SctpTypes.hpp +4 -4
  84. package/worker/include/RTC/SCTP/rx/DataTracker.hpp +1 -1
  85. package/worker/include/RTC/SCTP/rx/ReassemblyQueue.hpp +1 -1
  86. package/worker/include/RTC/SctpDictionaries.hpp +2 -2
  87. package/worker/include/RTC/TcpConnection.hpp +2 -1
  88. package/worker/include/RTC/Transport.hpp +23 -8
  89. package/worker/include/RTC/TransportTuple.hpp +61 -6
  90. package/worker/include/RTC/WebRtcTransport.hpp +4 -5
  91. package/worker/include/Shared.hpp +5 -0
  92. package/worker/include/SharedInterface.hpp +10 -0
  93. package/worker/include/Utils.hpp +72 -3
  94. package/worker/include/common.hpp +12 -4
  95. package/worker/include/handles/SendCallbacks.hpp +34 -0
  96. package/worker/include/handles/TcpConnectionHandle.hpp +4 -12
  97. package/worker/include/handles/UdpSocketHandle.hpp +4 -8
  98. package/worker/meson.build +41 -3
  99. package/worker/meson_options.txt +1 -0
  100. package/worker/mocks/include/MockShared.hpp +16 -2
  101. package/worker/mocks/include/handles/MockBackoffTimerHandle.hpp +1 -1
  102. package/worker/scripts/package-lock.json +6 -6
  103. package/worker/src/RTC/BWE/AimdRateControl.cpp +4 -5
  104. package/worker/src/RTC/BWE/AlrDetector.cpp +80 -0
  105. package/worker/src/RTC/BWE/BitrateProber.cpp +256 -0
  106. package/worker/src/RTC/BWE/{Utils.cpp → BitrateUtils.cpp} +4 -4
  107. package/worker/src/RTC/BWE/DelayBasedBwe.cpp +2 -2
  108. package/worker/src/RTC/BWE/FeedbackAdapter.cpp +20 -37
  109. package/worker/src/RTC/BWE/InterArrival.cpp +216 -0
  110. package/worker/src/RTC/BWE/IntervalBudget.cpp +93 -0
  111. package/worker/src/RTC/BWE/LossBasedController.cpp +189 -41
  112. package/worker/src/RTC/BWE/OveruseDetector.cpp +128 -0
  113. package/worker/src/RTC/BWE/OveruseEstimator.cpp +166 -0
  114. package/worker/src/RTC/BWE/ProbeBitrateEstimator.cpp +222 -0
  115. package/worker/src/RTC/BWE/ProbeController.cpp +645 -0
  116. package/worker/src/RTC/BWE/ProbePacketGenerator.cpp +140 -0
  117. package/worker/src/RTC/BWE/ProbingScheduler.cpp +196 -0
  118. package/worker/src/RTC/BWE/RembGenerator.cpp +169 -0
  119. package/worker/src/RTC/BWE/RemoteBitrateEstimatorAbsSendTime.cpp +497 -0
  120. package/worker/src/RTC/BWE/RobustThroughputEstimator.cpp +47 -0
  121. package/worker/src/RTC/BWE/SendPacketHistory.cpp +173 -50
  122. package/worker/src/RTC/BWE/TargetRateController.cpp +12 -14
  123. package/worker/src/RTC/BWE/TransportWideCcFeedbackGenerator.cpp +377 -0
  124. package/worker/src/RTC/BWE/TrendlineEstimator.cpp +19 -0
  125. package/worker/src/RTC/Consumer.cpp +7 -5
  126. package/worker/src/RTC/DataConsumer.cpp +8 -10
  127. package/worker/src/RTC/DirectTransport.cpp +14 -8
  128. package/worker/src/RTC/DtlsTransport.cpp +8 -8
  129. package/worker/src/RTC/ICE/IceCandidate.cpp +4 -4
  130. package/worker/src/RTC/ICE/IceServer.cpp +1 -1
  131. package/worker/src/RTC/PipeTransport.cpp +13 -8
  132. package/worker/src/RTC/PlainTransport.cpp +13 -8
  133. package/worker/src/RTC/Producer.cpp +14 -31
  134. package/worker/src/RTC/RTCP/Bye.cpp +1 -1
  135. package/worker/src/RTC/RTCP/FeedbackPsRemb.cpp +18 -0
  136. package/worker/src/RTC/RTCP/FeedbackPsRpsi.cpp +2 -2
  137. package/worker/src/RTC/RTCP/FeedbackPsVbcm.cpp +2 -1
  138. package/worker/src/RTC/RTCP/FeedbackRtpTransport.cpp +2 -2
  139. package/worker/src/RTC/RTCP/Sdes.cpp +1 -1
  140. package/worker/src/RTC/RTP/Codecs/DependencyDescriptor.cpp +2 -1
  141. package/worker/src/RTC/RTP/Packet.cpp +26 -11
  142. package/worker/src/RTC/RTP/ProbationGenerator.cpp +5 -6
  143. package/worker/src/RTC/RTP/RtpStream.cpp +1 -1
  144. package/worker/src/RTC/RTP/RtpStreamRecv.cpp +69 -48
  145. package/worker/src/RTC/RTP/RtpStreamSend.cpp +31 -31
  146. package/worker/src/RTC/RTP/RtxStream.cpp +22 -7
  147. package/worker/src/RTC/RateCalculator.cpp +133 -28
  148. package/worker/src/RTC/RemoteCaptureTimeEstimator.cpp +56 -1
  149. package/worker/src/RTC/{RtcLogger.cpp → RtcLogger/RtpPacket.cpp} +2 -2
  150. package/worker/src/RTC/RtcLogger/SendBurst.cpp +124 -0
  151. package/worker/src/RTC/RtpDictionaries/RtpHeaderExtensionUri.cpp +2 -12
  152. package/worker/src/RTC/RtpDictionaries/RtpParameters.cpp +1 -1
  153. package/worker/src/RTC/SCTP/association/Association.cpp +1 -1
  154. package/worker/src/RTC/SCTP/rx/InterleavedReassemblyStreams.cpp +3 -3
  155. package/worker/src/RTC/SCTP/rx/ReassemblyQueue.cpp +2 -1
  156. package/worker/src/RTC/SCTP/rx/TraditionalReassemblyStreams.cpp +4 -4
  157. package/worker/src/RTC/SCTP/tx/RetransmissionTimeout.cpp +4 -4
  158. package/worker/src/RTC/SctpDictionaries/SctpStreamParameters.cpp +6 -10
  159. package/worker/src/RTC/SimulcastProducerStreamManager.cpp +2 -2
  160. package/worker/src/RTC/SrtpSession.cpp +2 -2
  161. package/worker/src/RTC/SvcProducerStreamManager.cpp +2 -2
  162. package/worker/src/RTC/TcpConnection.cpp +2 -2
  163. package/worker/src/RTC/Transport.cpp +111 -93
  164. package/worker/src/RTC/TransportTuple.cpp +4 -4
  165. package/worker/src/RTC/WebRtcServer.cpp +4 -3
  166. package/worker/src/RTC/WebRtcTransport.cpp +20 -13
  167. package/worker/src/Utils/BitStream.cpp +24 -0
  168. package/worker/src/handles/BackoffTimerHandle.cpp +1 -1
  169. package/worker/src/handles/TcpConnectionHandle.cpp +13 -23
  170. package/worker/src/handles/UdpSocketHandle.cpp +14 -21
  171. package/worker/test/include/RTC/BWE/helpers/LinkSimulator.hpp +68 -14
  172. package/worker/test/include/RTC/BWE/helpers/RemoteBitrateEstimatorAbsSendTimeTestHelper.hpp +125 -0
  173. package/worker/test/src/RTC/BWE/TestAlrDetector.cpp +110 -0
  174. package/worker/test/src/RTC/BWE/TestBitrateProber.cpp +418 -0
  175. package/worker/test/src/RTC/BWE/TestBitrateUtils.cpp +61 -0
  176. package/worker/test/src/RTC/BWE/TestDelayBasedBwe.cpp +4 -4
  177. package/worker/test/src/RTC/BWE/TestFeedbackAdapter.cpp +53 -37
  178. package/worker/test/src/RTC/BWE/TestInterArrival.cpp +643 -0
  179. package/worker/test/src/RTC/BWE/TestIntervalBudget.cpp +130 -0
  180. package/worker/test/src/RTC/BWE/TestOveruseDetector.cpp +519 -0
  181. package/worker/test/src/RTC/BWE/TestProbeBitrateEstimator.cpp +266 -0
  182. package/worker/test/src/RTC/BWE/TestProbeController.cpp +1682 -0
  183. package/worker/test/src/RTC/BWE/TestProbePacketGenerator.cpp +265 -0
  184. package/worker/test/src/RTC/BWE/TestProbingScheduler.cpp +260 -0
  185. package/worker/test/src/RTC/BWE/TestRembGenerator.cpp +285 -0
  186. package/worker/test/src/RTC/BWE/TestRemoteBitrateEstimatorAbsSendTime.cpp +414 -0
  187. package/worker/test/src/RTC/BWE/TestSendPacketHistory.cpp +296 -77
  188. package/worker/test/src/RTC/BWE/TestTransportWideCcFeedbackGenerator.cpp +578 -0
  189. package/worker/test/src/RTC/BWE/helpers/LinkSimulator.cpp +133 -29
  190. package/worker/test/src/RTC/BWE/helpers/RemoteBitrateEstimatorAbsSendTimeTestHelper.cpp +625 -0
  191. package/worker/test/src/RTC/RTCP/TestFeedbackPsRemb.cpp +19 -0
  192. package/worker/test/src/RTC/RTP/TestPacket.cpp +58 -4
  193. package/worker/test/src/RTC/RTP/TestProbationGenerator.cpp +3 -2
  194. package/worker/test/src/RTC/RTP/TestRtpStreamRecv.cpp +50 -0
  195. package/worker/test/src/RTC/RTP/TestRtpStreamSend.cpp +59 -0
  196. package/worker/test/src/RTC/TestRateCalculator.cpp +322 -60
  197. package/worker/test/src/RTC/TestRemoteCaptureTimeEstimator.cpp +95 -7
  198. package/worker/test/src/RTC/TestSimpleProducerStreamManager.cpp +34 -16
  199. package/worker/test/src/RTC/TestSimulcastProducerStreamManager.cpp +31 -13
  200. package/worker/test/src/RTC/TestSvcProducerStreamManager.cpp +37 -19
  201. package/worker/test/src/RTC/TestTransportCongestionControlServer.cpp +10 -10
  202. package/worker/test/src/Utils/TestTime.cpp +120 -21
  203. package/worker/test/src/RTC/BWE/TestUtils.cpp +0 -61
@@ -8,18 +8,37 @@ namespace bweHelpers
8
8
  {
9
9
  /* Static. */
10
10
 
11
- // Largest packet a frame is split into (bytes).
12
- static constexpr size_t Mtu{ 1200 };
11
+ // Lowest capacity the link can be given, which is what keeps the transmission
12
+ // time below from dividing by a capacity of zero bits per millisecond.
13
+ static constexpr int64_t MinCapacityBps{ 1000 };
14
+ // Offset added to the send times so that they don't share the origin with the
15
+ // arrival ones, which would hide a mistake mixing both references.
16
+ static constexpr int64_t SendSideOffsetUs{ 1000 * 1000 };
13
17
 
14
18
  /* Instance methods. */
15
19
 
16
- RtpStream::RtpStream(int fps, int64_t bitrateBps) : fps(fps), bitrateBps(bitrateBps)
20
+ RtpStream::RtpStream(
21
+ int fps, int64_t bitrateBps, uint32_t ssrc, uint32_t frequency, uint32_t rtpTimestampOffset)
22
+ : fps(fps),
23
+ ssrc(ssrc),
24
+ frequency(frequency),
25
+ rtpTimestampOffset(rtpTimestampOffset),
26
+ bitrateBps(bitrateBps)
17
27
  {
18
28
  MS_TRACE();
19
29
 
20
30
  MS_ASSERT(fps > 0, "fps must be greater than zero [fps:%d]", fps);
21
31
  }
22
32
 
33
+ void RtpStream::SetBitrateBps(int64_t bitrateBps)
34
+ {
35
+ MS_TRACE();
36
+
37
+ MS_ASSERT(bitrateBps >= 0, "bitrate must not be negative [bitrate:%" PRIi64 "]", bitrateBps);
38
+
39
+ this->bitrateBps = bitrateBps;
40
+ }
41
+
23
42
  int64_t RtpStream::GenerateFrame(
24
43
  int64_t timeNowUs,
25
44
  int64_t& nextSequenceNumber,
@@ -27,38 +46,54 @@ namespace bweHelpers
27
46
  {
28
47
  MS_TRACE();
29
48
 
30
- if (timeNowUs < this->nextRtpTimeUs)
31
- {
32
- return this->nextRtpTimeUs;
33
- }
49
+ std::vector<Packet> packets;
34
50
 
35
- const size_t bitsPerFrame = (this->bitrateBps + (this->fps / 2)) / this->fps;
36
- const size_t numPackets = std::max<size_t>((bitsPerFrame + (4 * Mtu)) / (8 * Mtu), 1);
37
- const size_t payloadSize = (bitsPerFrame + (4 * numPackets)) / (8 * numPackets);
51
+ const int64_t nextRtpTimeUs = GenerateFrame(timeNowUs, packets);
38
52
 
39
- for (size_t idx{ 0 }; idx < numPackets; ++idx)
53
+ for (const auto& packet : packets)
40
54
  {
41
55
  RTC::BWE::Types::PacketResult packetResult;
42
56
 
43
- packetResult.sentPacket.sendTimeUs = timeNowUs + SendSideOffsetUs;
44
- packetResult.sentPacket.size = payloadSize;
57
+ packetResult.sentPacket.sendTimeUs = packet.sendTimeUs;
58
+ packetResult.sentPacket.size = packet.size;
45
59
  packetResult.sentPacket.sequenceNumber = nextSequenceNumber++;
46
60
 
47
61
  packetResults.push_back(packetResult);
48
62
  }
49
63
 
50
- this->nextRtpTimeUs = timeNowUs + ((1000000 + (this->fps / 2)) / this->fps);
51
-
52
- return this->nextRtpTimeUs;
64
+ return nextRtpTimeUs;
53
65
  }
54
66
 
55
- void RtpStream::SetBitrateBps(int64_t bitrateBps)
67
+ int64_t RtpStream::GenerateFrame(int64_t timeNowUs, std::vector<Packet>& packets)
56
68
  {
57
69
  MS_TRACE();
58
70
 
59
- MS_ASSERT(bitrateBps >= 0, "bitrate must not be negative [bitrate:%" PRIi64 "]", bitrateBps);
71
+ if (timeNowUs < this->nextRtpTimeUs)
72
+ {
73
+ return this->nextRtpTimeUs;
74
+ }
60
75
 
61
- this->bitrateBps = bitrateBps;
76
+ const size_t bitsPerFrame = (this->bitrateBps + (this->fps / 2)) / this->fps;
77
+ const size_t numPackets = std::max<size_t>((bitsPerFrame + (4 * Mtu)) / (8 * Mtu), 1);
78
+ const size_t payloadSize = (bitsPerFrame + (4 * numPackets)) / (8 * numPackets);
79
+ const int64_t sendTimeUs = timeNowUs + SendSideOffsetUs;
80
+ const auto rtpTimestamp =
81
+ this->rtpTimestampOffset +
82
+ static_cast<uint32_t>((((this->frequency / 1000) * sendTimeUs) + 500) / 1000);
83
+
84
+ for (size_t idx{ 0 }; idx < numPackets; ++idx)
85
+ {
86
+ packets.push_back(
87
+ Packet{ .ssrc = this->ssrc,
88
+ .rtpTimestamp = rtpTimestamp,
89
+ .sendTimeUs = sendTimeUs,
90
+ .arrivalTimeUs = 0,
91
+ .size = payloadSize });
92
+ }
93
+
94
+ this->nextRtpTimeUs = timeNowUs + ((1000000 + (this->fps / 2)) / this->fps);
95
+
96
+ return this->nextRtpTimeUs;
62
97
  }
63
98
 
64
99
  bool RtpStream::Compare(const std::unique_ptr<RtpStream>& lhs, const std::unique_ptr<RtpStream>& rhs)
@@ -72,6 +107,11 @@ namespace bweHelpers
72
107
  : capacityBps(capacityBps), prevArrivalTimeUs(timeNowUs)
73
108
  {
74
109
  MS_TRACE();
110
+
111
+ MS_ASSERT(
112
+ capacityBps >= MinCapacityBps,
113
+ "capacity must be at least one bit per millisecond [capacity:%" PRIi64 "]",
114
+ capacityBps);
75
115
  }
76
116
 
77
117
  void LinkSimulator::AddStream(std::unique_ptr<RtpStream> stream)
@@ -86,11 +126,28 @@ namespace bweHelpers
86
126
  MS_TRACE();
87
127
 
88
128
  MS_ASSERT(
89
- capacityBps > 0, "capacity must be greater than zero [capacity:%" PRIi64 "]", capacityBps);
129
+ capacityBps >= MinCapacityBps,
130
+ "capacity must be at least one bit per millisecond [capacity:%" PRIi64 "]",
131
+ capacityBps);
90
132
 
91
133
  this->capacityBps = capacityBps;
92
134
  }
93
135
 
136
+ void LinkSimulator::SetRtpTimestampOffset(uint32_t ssrc, uint32_t rtpTimestampOffset)
137
+ {
138
+ MS_TRACE();
139
+
140
+ for (auto& stream : this->streams)
141
+ {
142
+ if (stream->GetSsrc() == ssrc)
143
+ {
144
+ stream->SetRtpTimestampOffset(rtpTimestampOffset);
145
+
146
+ break;
147
+ }
148
+ }
149
+ }
150
+
94
151
  void LinkSimulator::SetBitrateBps(int64_t bitrateBps)
95
152
  {
96
153
  MS_TRACE();
@@ -139,22 +196,21 @@ namespace bweHelpers
139
196
  MS_TRACE();
140
197
 
141
198
  MS_ASSERT(packetResults.empty(), "the given vector is not empty");
199
+ MS_ASSERT(!this->streams.empty(), "no stream to generate a frame of");
142
200
  MS_ASSERT(
143
- this->capacityBps > 0,
144
- "capacity must be greater than zero [capacity:%" PRIi64 "]",
201
+ this->capacityBps >= MinCapacityBps,
202
+ "capacity must be at least one bit per millisecond [capacity:%" PRIi64 "]",
145
203
  this->capacityBps);
146
204
 
147
205
  auto it = std::ranges::min_element(this->streams, RtpStream::Compare);
148
206
 
149
- (*it)->GenerateFrame(timeNowUs, nextSequenceNumber, packetResults);
207
+ auto& dueStream = *it;
208
+
209
+ dueStream->GenerateFrame(timeNowUs, nextSequenceNumber, packetResults);
150
210
 
151
211
  for (auto& packetResult : packetResults)
152
212
  {
153
- // Time the link needs to put the packet on the wire.
154
- const int64_t capacityBpUs = this->capacityBps / 1000;
155
- const int64_t requiredNetworkTimeUs =
156
- ((8 * 1000 * static_cast<int64_t>(packetResult.sentPacket.size)) + (capacityBpUs / 2)) /
157
- capacityBpUs;
213
+ const int64_t requiredNetworkTimeUs = GetRequiredNetworkTimeUs(packetResult.sentPacket.size);
158
214
 
159
215
  // A packet arrives once the link is free again, so it queues behind the
160
216
  // previous one whenever the stream is sending above the capacity.
@@ -166,6 +222,54 @@ namespace bweHelpers
166
222
 
167
223
  it = std::ranges::min_element(this->streams, RtpStream::Compare);
168
224
 
169
- return std::max((*it)->GetNextRtpTimeUs(), timeNowUs);
225
+ auto& nextDueStream = *it;
226
+
227
+ return std::max(nextDueStream->GetNextRtpTimeUs(), timeNowUs);
228
+ }
229
+
230
+ int64_t LinkSimulator::GenerateFrame(int64_t timeNowUs, std::vector<Packet>& packets)
231
+ {
232
+ MS_TRACE();
233
+
234
+ MS_ASSERT(packets.empty(), "the given vector is not empty");
235
+ MS_ASSERT(!this->streams.empty(), "no stream to generate a frame of");
236
+ MS_ASSERT(
237
+ this->capacityBps >= MinCapacityBps,
238
+ "capacity must be at least one bit per millisecond [capacity:%" PRIi64 "]",
239
+ this->capacityBps);
240
+
241
+ auto it = std::ranges::min_element(this->streams, RtpStream::Compare);
242
+
243
+ auto& dueStream = *it;
244
+
245
+ dueStream->GenerateFrame(timeNowUs, packets);
246
+
247
+ for (auto& packet : packets)
248
+ {
249
+ const int64_t requiredNetworkTimeUs = GetRequiredNetworkTimeUs(packet.size);
250
+
251
+ // A packet arrives once the link is free again, so it queues behind the
252
+ // previous one whenever the stream is sending above the capacity.
253
+ this->prevArrivalTimeUs =
254
+ std::max(timeNowUs + requiredNetworkTimeUs, this->prevArrivalTimeUs + requiredNetworkTimeUs);
255
+
256
+ packet.arrivalTimeUs = this->prevArrivalTimeUs;
257
+ }
258
+
259
+ it = std::ranges::min_element(this->streams, RtpStream::Compare);
260
+
261
+ auto& nextDueStream = *it;
262
+
263
+ return std::max(nextDueStream->GetNextRtpTimeUs(), timeNowUs);
264
+ }
265
+
266
+ int64_t LinkSimulator::GetRequiredNetworkTimeUs(size_t size) const
267
+ {
268
+ MS_TRACE();
269
+
270
+ // Time the link needs to put a packet of that size on the wire.
271
+ const int64_t capacityBpUs = this->capacityBps / 1000;
272
+
273
+ return ((8 * 1000 * static_cast<int64_t>(size)) + (capacityBpUs / 2)) / capacityBpUs;
170
274
  }
171
275
  } // namespace bweHelpers