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
@@ -2,6 +2,7 @@
2
2
  #define MS_RTC_BWE_SEND_PACKET_HISTORY_HPP
3
3
 
4
4
  #include "common.hpp"
5
+ #include "Utils/UnwrappedSequenceNumber.hpp"
5
6
  #include "RTC/BWE/BweTypes.hpp"
6
7
  #include <map>
7
8
  #include <tuple>
@@ -37,14 +38,113 @@ namespace RTC
37
38
  int64_t windowDurationUs{ 60 * 1000 * 1000 };
38
39
  };
39
40
 
40
- private:
41
+ /**
42
+ * Everything about a packet that this history has to be told, since none
43
+ * of it can be read back from the bytes that go on the wire.
44
+ */
45
+ struct AddPacketOptions
46
+ {
47
+ uint32_t ssrc{ 0 };
48
+ /**
49
+ * RTP sequence number of the packet, which is what a feedback that
50
+ * doesn't carry the one above identifies it by.
51
+ */
52
+ uint16_t seq{ 0 };
53
+ /**
54
+ * Size of the packet including the overhead up to the IP layer (bytes).
55
+ *
56
+ * @remarks
57
+ * - What the link has to carry is what the estimation is about, so the
58
+ * length of the packet on its own falls short. The overhead of the
59
+ * path it takes is what `RTC::TransportTuple::GetPacketOverhead()`
60
+ * answers.
61
+ */
62
+ size_t size{ 0 };
63
+ /**
64
+ * Whether it's an audio packet. False for video, padding and RTX.
65
+ */
66
+ bool isAudio{ false };
67
+ bool isRetransmission{ false };
68
+ /**
69
+ * SSRC of the packet a retransmission carries, which is the SSRC above
70
+ * itself when it goes without RTX. No value when the packet isn't a
71
+ * retransmission.
72
+ */
73
+ std::optional<uint32_t> originalSsrc;
74
+ /**
75
+ * Whether the packet is marked as ECT(1), which asks the network to
76
+ * report congestion instead of dropping the packet.
77
+ *
78
+ * @see https://www.rfc-editor.org/rfc/rfc9331.html
79
+ */
80
+ bool sentWithEct1{ false };
81
+ /**
82
+ * The burst the packet belongs to, or no value if it isn't part of one.
83
+ */
84
+ std::optional<Types::ProbeCluster> probeCluster;
85
+ /**
86
+ * Time at which the packet is taken note of, which is not yet the time
87
+ * at which it was sent.
88
+ */
89
+ int64_t createdAtUs{ 0 };
90
+ };
91
+
92
+ /**
93
+ * A packet as this history holds it, which is what it was sent with plus
94
+ * what identifies it on the wire.
95
+ */
41
96
  struct SentPacketEntry
42
97
  {
43
98
  Types::SentPacket sentPacket;
99
+ /**
100
+ * Time at which the packet was taken note of, which is what makes it
101
+ * fall out of the window. It comes before the packet was sent, and by
102
+ * an unbounded amount when the socket cannot take the datagram right
103
+ * away.
104
+ */
105
+ int64_t createdAtUs{ 0 };
44
106
  uint32_t ssrc{ 0 };
45
107
  uint16_t seq{ 0 };
108
+ bool isRetransmission{ false };
109
+ /**
110
+ * Whether what a feedback reports as the arrival time of this packet
111
+ * cannot be told from that of another one, which happens when the very
112
+ * same SSRC and RTP sequence number were sent more than once.
113
+ *
114
+ * @remark
115
+ * - Nothing in this estimator reads it. It is only of use to SCReAM,
116
+ * another way of estimating the bandwidth, which discards the delay
117
+ * of a packet it cannot tell apart.
118
+ *
119
+ * @link https://datatracker.ietf.org/doc/draft-johansson-ccwg-rfc8298bis-screamv2
120
+ */
121
+ bool ambiguousReceiveTime{ false };
122
+ /**
123
+ * Whether the packet was sent marked as ECT(1), which asks the network
124
+ * to report congestion instead of dropping the packet.
125
+ *
126
+ * @remark
127
+ * - Only a feedback that reports ECN carries it back, which is the one
128
+ * of RFC 8888 and not the transport-cc one.
129
+ *
130
+ * @see https://datatracker.ietf.org/doc/html/rfc8888
131
+ * @see https://www.rfc-editor.org/rfc/rfc9331.html
132
+ */
133
+ bool sentWithEct1{ false };
134
+ /**
135
+ * Whether a feedback already reported this packet as lost, which is
136
+ * what tells a first loss from the same loss reported again.
137
+ *
138
+ * @remark
139
+ * - Only a feedback that reports on a whole range of packets makes use
140
+ * of it, which is the one of RFC 8888 and not the transport-cc one.
141
+ *
142
+ * @see https://datatracker.ietf.org/doc/html/rfc8888
143
+ */
144
+ bool previouslyReportedLost{ false };
46
145
  };
47
146
 
147
+ private:
48
148
  struct SsrcAndSeq
49
149
  {
50
150
  bool operator<(const SsrcAndSeq& other) const
@@ -62,23 +162,58 @@ namespace RTC
62
162
  explicit SendPacketHistory(SendPacketHistoryOptions options);
63
163
 
64
164
  /**
65
- * Take note of a packet that has just been sent.
165
+ * Take note of a packet that is about to be sent.
166
+ *
167
+ * @returns The sequence number given to the packet, whose lowest 16 bits
168
+ * are what has to go into the transport-wide sequence number RTP header
169
+ * extension, and which is what a feedback resolves into.
170
+ */
171
+ int64_t AddPacket(const AddPacketOptions& options);
172
+
173
+ /**
174
+ * Recover the sequence number this history gave a packet out of the
175
+ * lowest 16 bits of it that the wire carries.
176
+ *
177
+ * @remarks
178
+ * - Each number is resolved against the previous one resolved, be it of a
179
+ * packet taken note of or of another feedback, which is what carries the
180
+ * count across a turn of the wire's 16 bits. So one further than half
181
+ * that range away from the previous cannot be told apart from the same
182
+ * number a turn later.
183
+ */
184
+ int64_t UnwrapSequenceNumber(uint16_t wrappedSequenceNumber);
185
+
186
+ /**
187
+ * Take note that the packet with the given sequence number has left.
66
188
  *
67
- * @param size - Size of the packet including overhead up to the IP layer
68
- * (bytes).
69
- * @param audio - Whether it's an audio packet. False for video, padding
70
- * and RTX.
71
- * @returns The sequence number given to the packet, which is what a
72
- * feedback has to be resolved into.
189
+ * @returns The packet as it was sent, or no value if this history doesn't
190
+ * hold it anymore or it had already left.
191
+ *
192
+ * @remarks
193
+ * - This is what makes the packet count as in flight, and what settles how
194
+ * many bytes were in flight when it left.
195
+ * - A packet sent under a sequence number that already left is not taken
196
+ * note of twice, so that its bytes are not counted twice either.
197
+ */
198
+ std::optional<Types::SentPacket> ProcessSentPacket(int64_t sequenceNumber, int64_t sentAtUs);
199
+
200
+ /**
201
+ * Take note of bytes that left without being tracked one by one, which is
202
+ * what packets carrying no sequence number of this history amount to.
203
+ *
204
+ * @remarks
205
+ * - They are held until the next tracked packet leaves and are attributed
206
+ * to it, so that whoever measures the acknowledged bitrate doesn't
207
+ * believe the link carried less than it did.
73
208
  */
74
- int64_t AddPacket(uint32_t ssrc, uint16_t seq, size_t size, bool audio, int64_t sentAtUs);
209
+ void ProcessSentUntrackedPacket(size_t size, int64_t sentAtUs);
75
210
 
76
211
  /**
77
212
  * Resolve what a feedback reported about a single packet.
78
213
  *
79
214
  * @param received - Whether the feedback reported the packet as received.
80
- * @returns The packet as it was sent, or no value if this history doesn't
81
- * hold it anymore.
215
+ * @returns The packet as this history holds it, or no value if it doesn't
216
+ * hold it anymore or it was never reported as sent.
82
217
  *
83
218
  * @remarks
84
219
  * - Every packet up to the given one stops counting as in flight, since
@@ -86,24 +221,13 @@ namespace RTC
86
221
  * - A packet reported as received is dropped, while one reported as lost
87
222
  * is kept, since a later feedback may still report it as received.
88
223
  */
89
- std::optional<Types::SentPacket> RetrievePacket(int64_t sequenceNumber, bool received);
224
+ std::optional<SentPacketEntry> RetrievePacket(int64_t sequenceNumber, bool received);
90
225
 
91
226
  /**
92
227
  * Same, for a feedback that identifies the packet by its SSRC and RTP
93
228
  * sequence number rather than by the sequence number given to it.
94
229
  */
95
- std::optional<Types::SentPacket> RetrievePacket(uint32_t ssrc, uint16_t seq, bool received);
96
-
97
- /**
98
- * Sequence number this history gave to the latest packet added, or no
99
- * value if no packet was ever added.
100
- *
101
- * @remarks
102
- * - It's the sequence number of this history, not the RTP one, so it
103
- * never wraps around. It's also the highest a feedback carrying only
104
- * the lowest bits of it can possibly stand for.
105
- */
106
- std::optional<int64_t> GetLastSequenceNumber() const;
230
+ std::optional<SentPacketEntry> RetrievePacket(uint32_t ssrc, uint16_t seq, bool received);
107
231
 
108
232
  /**
109
233
  * Bytes that were sent and no feedback has reported on yet.
@@ -126,10 +250,22 @@ namespace RTC
126
250
  const SendPacketHistoryOptions options;
127
251
  std::map<int64_t, SentPacketEntry> history;
128
252
  std::map<SsrcAndSeq, int64_t> ssrcAndSeqToSequenceNumber;
129
- int64_t nextSequenceNumber{ 0 };
253
+ // Hands out the sequence numbers and recovers them out of the lowest bits
254
+ // the wire carries, which keeps both to a single count.
255
+ Utils::UnwrappedSequenceNumber<uint16_t>::Unwrapper sequenceNumberUnwrapper;
256
+ uint16_t nextWrappedSequenceNumber{ 0 };
257
+ // Highest sequence number ever taken note of, or no value if none was.
258
+ std::optional<int64_t> highestSequenceNumber;
130
259
  // Sequence numbers are never negative, so this sits below all of them.
131
260
  int64_t lastAckSequenceNumber{ -1 };
132
261
  size_t outstandingBytes{ 0 };
262
+ // Bytes that left untracked and no packet has been attributed yet.
263
+ size_t pendingUntrackedBytes{ 0 };
264
+ // Time at which the latest tracked packet left, or no value if none has.
265
+ std::optional<int64_t> lastSendTimeUs;
266
+ // Time at which the latest untracked bytes left, or no value if none
267
+ // have.
268
+ std::optional<int64_t> lastUntrackedSendTimeUs;
133
269
  };
134
270
  } // namespace BWE
135
271
  } // namespace RTC
@@ -0,0 +1,306 @@
1
+ #ifndef MS_RTC_BWE_TARGET_RATE_CONTROLLER_HPP
2
+ #define MS_RTC_BWE_TARGET_RATE_CONTROLLER_HPP
3
+
4
+ #include "common.hpp"
5
+ #include "RTC/BWE/BweTypes.hpp"
6
+ #include "RTC/BWE/LossBasedController.hpp"
7
+ #include <deque>
8
+ #include <vector>
9
+
10
+ namespace RTC
11
+ {
12
+ namespace BWE
13
+ {
14
+ /**
15
+ * Holds the target bitrate and every bound that applies to it.
16
+ *
17
+ * The delay based path, the receiver of the media and the application each
18
+ * produce a limit rather than a bitrate, so something has to hold the value
19
+ * they bound and decide how it moves within them. That is this class, and it
20
+ * also owns the two inputs that move the target down on their own: a round
21
+ * trip time long enough to mean that the queues of the network are full, and
22
+ * the loss the receiver reports.
23
+ *
24
+ * How loss moves the target depends on how much is known about it. Until the
25
+ * loss controller has seen enough feedback to tell congestion from the loss a
26
+ * link has by itself, this class applies rules of its own on the fraction of
27
+ * packets reported as lost: increase while it stays under 2%, leave the target
28
+ * alone up to 10%, and decrease it above that. They are coarse on purpose, and
29
+ * their weakness is precisely what they cannot tell apart, so on a link with
30
+ * steady loss above 2% they hold the target still and above 10% they drive it
31
+ * to the minimum. As soon as the loss controller is ready, its estimate
32
+ * replaces them.
33
+ *
34
+ * @remarks
35
+ * - This class is a port of the SendSideBandwidthEstimation class in
36
+ * libwebrtc (renamed to a better name).
37
+ */
38
+ class TargetRateController
39
+ {
40
+ public:
41
+ struct TargetRateControllerOptions
42
+ {
43
+ /**
44
+ * Reported loss under which the bitrate is increased.
45
+ */
46
+ double lowLossThreshold{ 0.02 };
47
+ /**
48
+ * Reported loss over which the bitrate is decreased.
49
+ */
50
+ double highLossThreshold{ 0.1 };
51
+ /**
52
+ * Bitrate under which loss is ignored altogether, since at a low enough
53
+ * bitrate loss is unlikely to be caused by the traffic itself.
54
+ */
55
+ int64_t bitrateThreshold{ 0 };
56
+ /**
57
+ * Whether the bound asked for by the receiver takes part in how the
58
+ * target moves, rather than only capping the value this controller
59
+ * hands out.
60
+ */
61
+ bool disableReceiverLimitCapsOnly{ false };
62
+ /**
63
+ * Round trip time over which the bitrate is dropped regardless of loss.
64
+ * A round trip time this long means the queues of the network are full.
65
+ */
66
+ int64_t maxRttUs{ 3 * 1000 * 1000 };
67
+ /**
68
+ * Fraction of the target the bitrate is dropped to on each of those
69
+ * drops.
70
+ */
71
+ double rttBackoffDropFraction{ 0.8 };
72
+ /**
73
+ * How often the bitrate may be dropped due to the round trip time.
74
+ */
75
+ int64_t rttBackoffDropIntervalUs{ 1000 * 1000 };
76
+ /**
77
+ * Bitrate the round trip time backoff never drops below.
78
+ */
79
+ int64_t rttBackoffBitrateFloor{ 5000 };
80
+ };
81
+
82
+ private:
83
+ /**
84
+ * Drops the bitrate when the round trip time stays long enough to mean
85
+ * that the queues of the network are full, which is a congestion signal
86
+ * that loss and delay may both miss.
87
+ */
88
+ struct RttBackoff
89
+ {
90
+ /**
91
+ * Round trip time corrected for the sender being idle. Without the
92
+ * correction a sender that stops transmitting would keep reporting the
93
+ * last measured value forever.
94
+ */
95
+ int64_t GetCorrectedRttUs() const
96
+ {
97
+ int64_t timeoutCorrectionUs{ 0 };
98
+
99
+ // While either instant is missing there is nothing to correct: no
100
+ // feedback has arrived, or nothing that gets feedback has been sent.
101
+ if (this->lastPacketSentAtUs.has_value() && this->lastPropagationRttAtUs.has_value())
102
+ {
103
+ timeoutCorrectionUs = std::max<int64_t>(
104
+ this->lastPacketSentAtUs.value() - this->lastPropagationRttAtUs.value(), 0);
105
+ }
106
+
107
+ return timeoutCorrectionUs + this->lastPropagationRttUs;
108
+ }
109
+
110
+ std::optional<int64_t> lastPropagationRttAtUs;
111
+ std::optional<int64_t> lastPacketSentAtUs;
112
+ int64_t lastPropagationRttUs{ 0 };
113
+ };
114
+
115
+ public:
116
+ TargetRateController();
117
+
118
+ explicit TargetRateController(TargetRateControllerOptions options);
119
+
120
+ /**
121
+ * Bitrate this controller may produce, already within every bound.
122
+ */
123
+ int64_t GetTargetBitrate() const;
124
+
125
+ /**
126
+ * Latest fraction of lost packets reported by the receiver, in 1/256 units.
127
+ */
128
+ uint8_t GetFractionLost() const
129
+ {
130
+ return this->lastFractionLost;
131
+ }
132
+
133
+ int64_t GetRttUs() const
134
+ {
135
+ return this->lastRttUs;
136
+ }
137
+
138
+ /**
139
+ * Whether the round trip time is long enough to be taken as congestion on
140
+ * its own.
141
+ */
142
+ bool IsRttAboveLimit() const
143
+ {
144
+ return this->rttBackoff.GetCorrectedRttUs() > this->options.maxRttUs;
145
+ }
146
+
147
+ /**
148
+ * Forget everything learnt about the network, which is what a transport
149
+ * that is not going through the same path anymore calls for.
150
+ *
151
+ * @remarks
152
+ * - The bounds set by the application are dropped too, so they have to be
153
+ * set again afterwards.
154
+ */
155
+ void OnRouteChange();
156
+
157
+ /**
158
+ * Bounds set by the application.
159
+ *
160
+ * @remarks
161
+ * - A max bitrate of zero means no limit.
162
+ */
163
+ void SetBitrateLimits(int64_t minBitrate, int64_t maxBitrate);
164
+
165
+ /**
166
+ * Place the target at the given bitrate, dropping the bound that could
167
+ * hold it back and the history that limits how fast it grows.
168
+ */
169
+ void SetSendBitrate(int64_t bitrate);
170
+
171
+ /**
172
+ * Upper bound produced by the delay based path.
173
+ */
174
+ void SetDelayBasedEstimate(int64_t bitrate);
175
+
176
+ /**
177
+ * Upper bound asked for by the receiver of the media, which reaches us as
178
+ * a REMB message.
179
+ *
180
+ * @remarks
181
+ * - A bitrate of zero means no limit.
182
+ */
183
+ void SetReceiverEstimate(int64_t bitrate);
184
+
185
+ /**
186
+ * Feed a loss report of the receiver.
187
+ *
188
+ * @remarks
189
+ * - Reports are accumulated until they cover enough packets, so most calls
190
+ * change nothing.
191
+ */
192
+ void UpdatePacketsLost(int64_t lostPackets, int64_t totalPackets, int64_t nowUs);
193
+
194
+ void UpdateRtt(int64_t rttUs);
195
+
196
+ /**
197
+ * Feed the round trip time the round trip time backoff watches, which is
198
+ * the one measured by the transport feedback rather than by RTCP.
199
+ */
200
+ void UpdatePropagationRtt(int64_t propagationRttUs, int64_t nowUs);
201
+
202
+ /**
203
+ * Tell that a packet that will be reported back has just been sent.
204
+ */
205
+ void OnPacketSent(int64_t sentAtUs);
206
+
207
+ /**
208
+ * Reconsider the target. Called after every loss report and periodically,
209
+ * since the increase depends on how much time has elapsed.
210
+ */
211
+ void Update(int64_t nowUs);
212
+
213
+ /**
214
+ * Bitrate the network is known to be delivering.
215
+ */
216
+ void SetAcknowledgedBitrate(int64_t acknowledgedBitrate);
217
+
218
+ /**
219
+ * Feed the results of a feedback message to the loss controller, which is
220
+ * the only thing in here that looks at packets one by one.
221
+ *
222
+ * @param inAlr - Whether the sender is not sending enough to fill the link.
223
+ */
224
+ void UpdateLossBasedController(
225
+ const std::vector<Types::PacketResult>& packetResults, bool inAlr, int64_t nowUs);
226
+
227
+ /**
228
+ * What the loss controller is doing with the target, which tells whether
229
+ * probing makes sense.
230
+ */
231
+ LossBasedController::State GetLossBasedState() const
232
+ {
233
+ return this->lossBasedState;
234
+ }
235
+
236
+ private:
237
+ /**
238
+ * Whether the target is still allowed to just follow the bounds because
239
+ * not enough is known about the network yet.
240
+ */
241
+ bool IsInStartPhase(int64_t nowUs) const;
242
+
243
+ /**
244
+ * Lowest bitrate the target has had during the last second, which is what
245
+ * the increase grows from.
246
+ */
247
+ void UpdateMinBitrateHistory(int64_t nowUs);
248
+
249
+ /**
250
+ * Upper bound of every kind that applies to the target.
251
+ */
252
+ int64_t GetUpperLimit() const;
253
+
254
+ /**
255
+ * Set the target to the given bitrate, keeping it within its bounds.
256
+ */
257
+ void SetTargetBitrate(int64_t bitrate);
258
+
259
+ /**
260
+ * Reapply the bounds to the current target, which is needed when one of
261
+ * them changes.
262
+ */
263
+ void ApplyTargetLimits();
264
+
265
+ private:
266
+ // Passed by argument.
267
+ const TargetRateControllerOptions options;
268
+ // Others.
269
+ // Tells congestion from the loss the link has by itself, and takes over
270
+ // the target as soon as it has enough observations.
271
+ LossBasedController lossBasedController;
272
+ LossBasedController::State lossBasedState{ LossBasedController::State::DELAY_BASED_ESTIMATE };
273
+ struct RttBackoff rttBackoff;
274
+ // Lowest target of the last second, as a sliding window minimum of
275
+ // (instant in us, bitrate in bps) pairs.
276
+ std::deque<std::pair<int64_t, int64_t>> minBitrateHistory;
277
+ // Loss reports too small to tell anything on their own, accumulated until
278
+ // they cover enough packets.
279
+ int64_t lostPacketsSinceLastLossUpdate{ 0 };
280
+ int64_t expectedPacketsSinceLastLossUpdate{ 0 };
281
+ int64_t currentTarget{ 0 };
282
+ int64_t minBitrateConfigured{ 0 };
283
+ int64_t maxBitrateConfigured{ 0 };
284
+ // Instant of the first loss report ever received, which is when the start
285
+ // phase begins.
286
+ std::optional<int64_t> firstLossReportAtUs;
287
+ // Instant of the latest loss report that covered enough packets to tell
288
+ // something.
289
+ std::optional<int64_t> lastLossReportAtUs;
290
+ // Instant at which the target was last decreased, by the loss rules or by
291
+ // the round trip time backoff.
292
+ std::optional<int64_t> lastDecreaseAtUs;
293
+ // Whether the target has already been decreased for the loss currently
294
+ // reported, so that a same report doesn't decrease it twice.
295
+ bool hasDecreasedSinceLastFractionLoss{ false };
296
+ uint8_t lastFractionLost{ 0 };
297
+ int64_t lastRttUs{ 0 };
298
+ // Upper bound asked for by the receiver of the media.
299
+ int64_t receiverLimit{ Types::BitrateInfinite };
300
+ // Upper bound produced by the delay based path.
301
+ int64_t delayBasedLimit{ Types::BitrateInfinite };
302
+ };
303
+ } // namespace BWE
304
+ } // namespace RTC
305
+
306
+ #endif
@@ -0,0 +1,146 @@
1
+ #ifndef MS_RTC_BWE_TRANSPORT_WIDE_CC_FEEDBACK_GENERATOR_HPP
2
+ #define MS_RTC_BWE_TRANSPORT_WIDE_CC_FEEDBACK_GENERATOR_HPP
3
+
4
+ #include "common.hpp"
5
+ #include "handles/TimerHandleInterface.hpp"
6
+ #include "RTC/RTCP/FeedbackRtpTransport.hpp"
7
+ #include "RTC/RTP/Packet.hpp"
8
+ #include "RTC/RateCalculator.hpp"
9
+ #include "RTC/SeqManager.hpp"
10
+ #include "SharedInterface.hpp"
11
+ #include <map>
12
+
13
+ namespace RTC
14
+ {
15
+ namespace BWE
16
+ {
17
+ /**
18
+ * Tells the remote sender when each of its packets arrived, which is what
19
+ * lets that sender estimate the link towards us.
20
+ *
21
+ * Nothing is estimated here. The arrival time of every packet carrying the
22
+ * transport wide sequence number is kept, and a feedback packet with as many
23
+ * of them as fit is emitted periodically. Turning those arrival times into a
24
+ * bitrate is the remote sender's job.
25
+ *
26
+ * How often the feedback is emitted follows the bitrate coming in, so that
27
+ * what reporting costs stays a small share of what is being reported on.
28
+ */
29
+ class TransportWideCcFeedbackGenerator : public TimerHandleInterface::Listener
30
+ {
31
+ public:
32
+ class Listener
33
+ {
34
+ public:
35
+ virtual ~Listener() = default;
36
+
37
+ public:
38
+ /**
39
+ * A feedback packet is ready for the remote sender.
40
+ *
41
+ * @param packet - Packet to send, only valid for the duration of this
42
+ * call.
43
+ */
44
+ virtual void OnTransportWideCcFeedbackGeneratorSendRtcpPacket(
45
+ TransportWideCcFeedbackGenerator* transportWideCcFeedbackGenerator,
46
+ RTC::RTCP::FeedbackRtpTransportPacket* packet) = 0;
47
+ };
48
+
49
+ public:
50
+ TransportWideCcFeedbackGenerator(
51
+ Listener* listener, SharedInterface* shared, size_t maxRtcpPacketLen);
52
+ ~TransportWideCcFeedbackGenerator() override = default;
53
+
54
+ TransportWideCcFeedbackGenerator(const TransportWideCcFeedbackGenerator&) = delete;
55
+ TransportWideCcFeedbackGenerator& operator=(const TransportWideCcFeedbackGenerator&) = delete;
56
+
57
+ /**
58
+ * Feed a received RTP packet, which is ignored unless it carries the
59
+ * transport wide sequence number extension.
60
+ *
61
+ * @param arrivalTimeUs - Instant the packet arrived.
62
+ */
63
+ void IncomingPacket(int64_t arrivalTimeUs, const RTC::RTP::Packet* packet);
64
+
65
+ #ifdef MS_TEST
66
+ public:
67
+ #else
68
+ private:
69
+ #endif
70
+ /**
71
+ * Report everything received since the last time, in as many feedback
72
+ * packets as it takes.
73
+ */
74
+ void FillAndSendFeedback();
75
+
76
+ /**
77
+ * How often feedback should be emitted (ms) to report on the given
78
+ * incoming bitrate (bps) without taking more than a small share of it.
79
+ */
80
+ static int64_t ComputeSendIntervalMs(int64_t incomingBitrate);
81
+
82
+ /**
83
+ * How often feedback is being emitted right now (ms).
84
+ */
85
+ int64_t GetSendIntervalMs() const
86
+ {
87
+ return this->sendIntervalMs;
88
+ }
89
+
90
+ private:
91
+ /**
92
+ * Hand the feedback packet being filled to the listener.
93
+ *
94
+ * @returns Whether it was handed over, which does not happen when it holds
95
+ * nothing that can be put on the wire.
96
+ */
97
+ bool SendFeedback();
98
+
99
+ /**
100
+ * Forget the arrival times too old to be reported, keeping the one the
101
+ * next feedback starts at.
102
+ */
103
+ void MayDropOldPacketArrivalTimes(uint16_t seqNum, int64_t arrivalTimeUs);
104
+
105
+ /**
106
+ * Start a new feedback packet, carrying on the given count.
107
+ */
108
+ void ResetFeedbackPacket(uint8_t feedbackPacketCount);
109
+
110
+ /**
111
+ * Work out how often feedback should be emitted out of the bitrate coming
112
+ * in, and reprogram the timer if the answer changed.
113
+ */
114
+ void MayUpdateSendInterval(int64_t nowMs);
115
+
116
+ /* Pure virtual methods inherited from TimerHandleInterface::Listener. */
117
+ public:
118
+ void OnTimer(TimerHandleInterface* timer) override;
119
+
120
+ private:
121
+ // Largest an RTCP packet may be on this transport (bytes).
122
+ const size_t maxRtcpPacketLen;
123
+
124
+ // Passed by argument.
125
+ Listener* listener{ nullptr };
126
+ SharedInterface* shared{ nullptr };
127
+ // Allocated by this.
128
+ const std::unique_ptr<TimerHandleInterface> sendPeriodicTimer;
129
+ std::unique_ptr<RTC::RTCP::FeedbackRtpTransportPacket> feedbackPacket;
130
+ // Bitrate coming in, which is what decides how often feedback is emitted.
131
+ RTC::RtpDataCounter incomingDataCounter;
132
+ // How often feedback is emitted (ms).
133
+ int64_t sendIntervalMs;
134
+ uint8_t feedbackPacketCount{ 0 };
135
+ uint32_t feedbackMediaSsrc{ 0 };
136
+ // Sequence number the next feedback starts reporting at, or no value
137
+ // until the first packet is seen.
138
+ std::optional<uint16_t> feedbackWideSeqNumStart;
139
+ // Instant each packet arrived (us), indexed by its transport wide
140
+ // sequence number.
141
+ std::map<uint16_t, int64_t, RTC::SeqManager<uint16_t>::SeqLowerThan> mapPacketArrivalTimes;
142
+ };
143
+ } // namespace BWE
144
+ } // namespace RTC
145
+
146
+ #endif