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
@@ -0,0 +1,81 @@
1
+ #ifndef MS_RTC_BWE_INTERVAL_BUDGET_HPP
2
+ #define MS_RTC_BWE_INTERVAL_BUDGET_HPP
3
+
4
+ #include "common.hpp"
5
+
6
+ namespace RTC
7
+ {
8
+ namespace BWE
9
+ {
10
+ /**
11
+ * Tracks how much of a bitrate is being used over a sliding span of time.
12
+ *
13
+ * The budget is a number of bytes that refills at a given bitrate and is
14
+ * spent as bytes are sent, bounded in both directions by what that bitrate
15
+ * delivers over a fixed window. Its level relative to that bound is the
16
+ * answer to whether the sender is keeping up with the bitrate it was given:
17
+ * a full budget means nothing has been sent for a while, and an empty one
18
+ * means the bitrate is being used whole.
19
+ *
20
+ * The bound in the negative direction is what keeps a long burst from
21
+ * building a debt that would take an unbounded time to pay back.
22
+ */
23
+ class IntervalBudget
24
+ {
25
+ public:
26
+ explicit IntervalBudget(int64_t initialTargetBitrate);
27
+
28
+ /**
29
+ * @param canBuildUpUnderuse - Whether what an interval leaves unspent is
30
+ * available to the next one. When false, an interval that spent nothing
31
+ * leaves the budget at just what that one interval refills, instead of
32
+ * adding to what was already there.
33
+ */
34
+ IntervalBudget(int64_t initialTargetBitrate, bool canBuildUpUnderuse);
35
+
36
+ /**
37
+ * Set the bitrate the budget refills at (bps).
38
+ */
39
+ void SetTargetBitrate(int64_t targetBitrate);
40
+
41
+ int64_t GetTargetBitrate() const
42
+ {
43
+ return this->targetBitrate;
44
+ }
45
+
46
+ /**
47
+ * Refill the budget with what the target bitrate delivers over the given
48
+ * span of time.
49
+ */
50
+ void IncreaseBudget(int64_t deltaTimeUs);
51
+
52
+ /**
53
+ * Spend the given number of bytes.
54
+ */
55
+ void UseBudget(int64_t bytes);
56
+
57
+ /**
58
+ * Bytes still available, which is zero while the budget is in debt.
59
+ */
60
+ int64_t GetBytesRemaining() const;
61
+
62
+ /**
63
+ * Level of the budget as a fraction of what the window holds, from -1
64
+ * when the debt is at its bound to 1 when the budget is full.
65
+ */
66
+ double GetBudgetRatio() const;
67
+
68
+ private:
69
+ // Passed by argument.
70
+ const bool canBuildUpUnderuse;
71
+ // Others.
72
+ int64_t targetBitrate{ 0 };
73
+ // Bytes the target bitrate delivers over the window, which is what bounds
74
+ // the budget in both directions.
75
+ int64_t maxBytesInBudget{ 0 };
76
+ int64_t bytesRemaining{ 0 };
77
+ };
78
+ } // namespace BWE
79
+ } // namespace RTC
80
+
81
+ #endif
@@ -40,42 +40,75 @@ namespace RTC
40
40
  class LossBasedController
41
41
  {
42
42
  public:
43
+ /**
44
+ * @remarks
45
+ * - Every constraint documented below is checked by the constructor and
46
+ * aborts when it doesn't hold. These options are set from C++ alone,
47
+ * never from the network nor from the API, so breaking one of them is a
48
+ * programming error.
49
+ */
43
50
  struct LossBasedControllerOptions
44
51
  {
45
52
  /**
46
53
  * Factors applied to the current estimate to build the candidates that
47
54
  * are tried on every update.
55
+ *
56
+ * @remarks
57
+ * - Every factor must be positive.
58
+ * - At least one candidate other than the current estimate has to be
59
+ * reachable, so unless `appendAcknowledgedRateCandidate` or
60
+ * `appendDelayBasedEstimateCandidate` is set, one of these factors
61
+ * must differ from 1.
48
62
  */
49
63
  std::vector<double> candidateFactors{ 1.02, 1.0, 0.95 };
50
64
  /**
51
65
  * How much the estimate may grow over the acknowledged bitrate while
52
66
  * the link is loss limited.
67
+ *
68
+ * @remarks
69
+ * - It must be greater than 1, since it's a bound above that bitrate.
53
70
  */
54
71
  double bitrateRampupUpperBoundFactor{ 1.5 };
55
72
  /**
56
73
  * The same while the estimate is being held, which is more conservative
57
74
  * because holding means that a higher bitrate already caused loss.
75
+ *
76
+ * @remarks
77
+ * - It must be greater than 1, for the same reason.
58
78
  */
59
79
  double bitrateRampupUpperBoundFactorInHold{ 1.2 };
60
80
  /**
61
81
  * How far below the held bitrate the acknowledged one has to be for the
62
82
  * factor above to be the one applied.
83
+ *
84
+ * @remarks
85
+ * - It must not be negative.
63
86
  */
64
87
  double bitrateRampupHoldThreshold{ 1.3 };
65
88
  /**
66
89
  * How much of the acknowledged bitrate the estimate may grow by on top
67
90
  * of its usual bound, the longer the more time has passed since it was
68
91
  * last reduced. Zero disables that acceleration.
92
+ *
93
+ * @remarks
94
+ * - It must not be negative.
69
95
  */
70
96
  double rampupAccelerationMaxFactor{ 0.0 };
71
97
  /**
72
98
  * Time since the last reduction at which that acceleration is at its
73
99
  * fullest.
100
+ *
101
+ * @remarks
102
+ * - It must be positive, since it's what the elapsed time is measured
103
+ * against.
74
104
  */
75
105
  int64_t rampupAccelerationMaxoutTimeUs{ 60 * 1000 * 1000 };
76
106
  /**
77
107
  * Weight given to a higher bitrate when choosing among candidates, so
78
108
  * that observations which cannot tell them apart don't settle low.
109
+ *
110
+ * @remarks
111
+ * - It must not be negative.
79
112
  */
80
113
  double higherBitrateBiasFactor{ 0.0002 };
81
114
  /**
@@ -86,46 +119,78 @@ namespace RTC
86
119
  /**
87
120
  * Observed loss at which the bias above is gone, since at that much loss
88
121
  * preferring a higher bitrate is not defensible anymore.
122
+ *
123
+ * @remarks
124
+ * - Being a loss rate, it must be in [0, 1).
89
125
  */
90
126
  double lossThresholdOfHighBitratePreference{ 0.2 };
91
127
  /**
92
128
  * How abruptly that bias fades as the observed loss approaches the
93
129
  * threshold above.
130
+ *
131
+ * @remarks
132
+ * - It must be in (0, 1]. Zero is excluded because it divides.
94
133
  */
95
134
  double bitratePreferenceSmoothingFactor{ 0.002 };
96
135
  /**
97
136
  * Lowest inherent loss that may be estimated for a link.
137
+ *
138
+ * @remarks
139
+ * - Being a loss rate, it must be in [0, 1).
98
140
  */
99
141
  double inherentLossLowerBound{ 1.0e-3 };
100
142
  /**
101
143
  * Inherent loss that may be estimated at an unbounded bitrate, which
102
144
  * grows as the bitrate gets lower by the balance below.
145
+ *
146
+ * @remarks
147
+ * - It must be below 1 and never below `inherentLossLowerBound`, since
148
+ * the two of them are the ends of the same range.
103
149
  */
104
150
  double inherentLossUpperBoundOffset{ 0.05 };
105
151
  /**
106
152
  * Bitrate at which the inherent loss upper bound grows by one, so that a
107
153
  * low bitrate is allowed to be explained by a lossy link.
154
+ *
155
+ * @remarks
156
+ * - It must be positive, since it divides.
108
157
  */
109
158
  int64_t inherentLossUpperBoundBitrateBalance{ 100000 };
110
159
  /**
111
160
  * Inherent loss assumed before anything has been observed.
161
+ *
162
+ * @remarks
163
+ * - Being a loss rate, it must be in [0, 1).
112
164
  */
113
165
  double initialInherentLossEstimate{ 0.01 };
114
166
  /**
115
167
  * Steps of Newton's method applied to each candidate.
168
+ *
169
+ * @remarks
170
+ * - It must be positive, or no candidate would ever be refined.
116
171
  */
117
172
  int64_t newtonIterations{ 1 };
118
173
  /**
119
174
  * Fraction of the step that each of those iterations takes.
175
+ *
176
+ * @remarks
177
+ * - It must be positive, or each iteration would stand still.
120
178
  */
121
179
  double newtonStepSize{ 0.75 };
122
180
  /**
123
181
  * Whether the acknowledged bitrate is tried as a candidate of its own.
182
+ *
183
+ * @remarks
184
+ * - See the constraint on `candidateFactors` about there always being a
185
+ * candidate to choose from.
124
186
  */
125
187
  bool appendAcknowledgedRateCandidate{ true };
126
188
  /**
127
189
  * Whether the delay based estimate is tried as a candidate of its own
128
190
  * while it's above the current one.
191
+ *
192
+ * @remarks
193
+ * - The same constraint as `appendAcknowledgedRateCandidate`.
129
194
  */
130
195
  bool appendDelayBasedEstimateCandidate{ true };
131
196
  /**
@@ -136,14 +201,25 @@ namespace RTC
136
201
  bool appendUpperBoundCandidateInAlr{ false };
137
202
  /**
138
203
  * Shortest span of send times that an observation may cover.
204
+ *
205
+ * @remarks
206
+ * - It must be positive, or every packet would close an observation.
139
207
  */
140
208
  int64_t observationDurationLowerBoundUs{ 250 * 1000 };
141
209
  /**
142
210
  * Observations kept, which is how far back the estimate looks.
211
+ *
212
+ * @remarks
213
+ * - It must be at least 2. It's also what every observation is indexed
214
+ * modulo, so an empty window would divide by zero.
143
215
  */
144
216
  int64_t observationWindowSize{ 15 };
145
217
  /**
146
218
  * Observations needed before this controller may be used at all.
219
+ *
220
+ * @remarks
221
+ * - It must be positive, or the controller would take the estimate over
222
+ * having observed nothing.
147
223
  */
148
224
  int64_t minNumObservations{ 3 };
149
225
  /**
@@ -151,6 +227,7 @@ namespace RTC
151
227
  * of the previous observation.
152
228
  *
153
229
  * @remarks
230
+ * - It must be in [0, 1).
154
231
  * - Zero means no smoothing at all, which is what libwebrtc does. A
155
232
  * sender of a couple of streams has a sending rate that only changes
156
233
  * when it decides so, while ours is the sum of many streams whose
@@ -160,38 +237,66 @@ namespace RTC
160
237
  /**
161
238
  * How much weight each observation loses per observation of age when
162
239
  * looking for the most likely pair of values.
240
+ *
241
+ * @remarks
242
+ * - It must be in (0, 1]. One means that age doesn't weigh at all, and
243
+ * zero is excluded because it would leave every observation but the
244
+ * latest one weightless.
163
245
  */
164
246
  double temporalWeightFactor{ 0.9 };
165
247
  /**
166
248
  * The same, for the average observed loss that bounds the estimate right
167
249
  * away.
250
+ *
251
+ * @remarks
252
+ * - It must be in (0, 1], for the same reason.
168
253
  */
169
254
  double immediateUpperBoundTemporalWeightFactor{ 0.9 };
170
255
  /**
171
256
  * Observed loss under which no immediate upper bound is applied.
257
+ *
258
+ * @remarks
259
+ * - Being a loss rate, it must be in [0, 1).
172
260
  */
173
261
  double immediateUpperBoundLossOffset{ 0.05 };
174
262
  /**
175
263
  * Bitrate the immediate upper bound allows per unit of observed loss
176
264
  * over the offset above.
265
+ *
266
+ * @remarks
267
+ * - It must be positive, since it multiplies a bound that would
268
+ * otherwise collapse to nothing.
177
269
  */
178
270
  int64_t immediateUpperBoundBitrateBalance{ 100000 };
179
271
  /**
180
272
  * How much of the acknowledged bitrate the estimate may never go below.
273
+ *
274
+ * @remarks
275
+ * - It must not be negative.
181
276
  */
182
277
  double lowerBoundByAckedRateFactor{ 1.0 };
183
278
  /**
184
279
  * Fraction of the acknowledged bitrate taken as a candidate when backing
185
280
  * off.
281
+ *
282
+ * @remarks
283
+ * - It must not be greater than 1, since backing off to more than what
284
+ * is acknowledged is not backing off.
186
285
  */
187
286
  double bitrateBackoffLowerBoundFactor{ 1.0 };
188
287
  /**
189
288
  * How much the estimate may grow within the window that follows a
190
289
  * decrease.
290
+ *
291
+ * @remarks
292
+ * - It must be positive.
191
293
  */
192
294
  double maxIncreaseFactor{ 1.3 };
193
295
  /**
194
296
  * How long that window lasts.
297
+ *
298
+ * @remarks
299
+ * - It must be positive.
195
300
  */
196
301
  int64_t delayedIncreaseWindowUs{ 300 * 1000 };
197
302
  /**
@@ -0,0 +1,71 @@
1
+ #ifndef MS_RTC_BWE_OVERUSE_DETECTOR_HPP
2
+ #define MS_RTC_BWE_OVERUSE_DETECTOR_HPP
3
+
4
+ #include "common.hpp"
5
+ #include "RTC/BWE/BweTypes.hpp"
6
+
7
+ namespace RTC
8
+ {
9
+ namespace BWE
10
+ {
11
+ /**
12
+ * Decides whether the network is congested from how much the delay between
13
+ * groups of packets is growing.
14
+ *
15
+ * The trend it is given is compared against a threshold that adapts itself
16
+ * to the noise observed, and congestion is only declared once the condition
17
+ * has persisted, which is what keeps an isolated jitter spike from bringing
18
+ * the estimate down.
19
+ *
20
+ * @remarks
21
+ * - This is what the receiving side uses, paired with `OveruseEstimator`.
22
+ * The sending side has `TrendlineEstimator`, which does both jobs at once
23
+ * and shares this very threshold machinery.
24
+ */
25
+ class OveruseDetector
26
+ {
27
+ public:
28
+ OveruseDetector() = default;
29
+
30
+ ~OveruseDetector() = default;
31
+
32
+ OveruseDetector(const OveruseDetector&) = delete;
33
+ OveruseDetector& operator=(const OveruseDetector&) = delete;
34
+
35
+ /**
36
+ * Feed what the estimator makes of the latest pair of groups.
37
+ *
38
+ * @param offsetMs - Part of the delay between both groups that the traffic
39
+ * itself doesn't explain.
40
+ * @param sendDeltaMs - Time between the send times of both groups, which
41
+ * is how long this sample is worth.
42
+ * @param numOfDeltas - Number of samples that offset is based on.
43
+ * @param nowUs - Current time.
44
+ *
45
+ * @returns What the network is taken to be doing after this sample.
46
+ */
47
+ Types::BandwidthUsage Detect(
48
+ double offsetMs, double sendDeltaMs, int64_t numOfDeltas, int64_t nowUs);
49
+
50
+ Types::BandwidthUsage GetState() const
51
+ {
52
+ return this->state;
53
+ }
54
+
55
+ private:
56
+ void UpdateThreshold(double modifiedOffsetMs, int64_t nowUs);
57
+
58
+ private:
59
+ double threshold{ 12.5 };
60
+ std::optional<int64_t> lastThresholdUpdateAtUs;
61
+ double prevOffsetMs{ 0.0 };
62
+ // How long the trend has been above the threshold, or no value while it
63
+ // isn't.
64
+ std::optional<double> timeOverUsingMs;
65
+ int64_t overuseCounter{ 0 };
66
+ Types::BandwidthUsage state{ Types::BandwidthUsage::NORMAL };
67
+ };
68
+ } // namespace BWE
69
+ } // namespace RTC
70
+
71
+ #endif
@@ -0,0 +1,115 @@
1
+ #ifndef MS_RTC_BWE_OVERUSE_ESTIMATOR_HPP
2
+ #define MS_RTC_BWE_OVERUSE_ESTIMATOR_HPP
3
+
4
+ #include "common.hpp"
5
+ #include "RTC/BWE/BweTypes.hpp"
6
+ #include <deque>
7
+
8
+ namespace RTC
9
+ {
10
+ namespace BWE
11
+ {
12
+ /**
13
+ * Estimates how much the delay between groups of packets is growing, and how
14
+ * noisy that measure is.
15
+ *
16
+ * It fits a two variable model to what each pair of groups showed: how much
17
+ * of the extra delay is explained by the group being bigger, and how much is
18
+ * left over once that is taken out. The second one is the interesting part,
19
+ * since a delay that grows without the traffic growing is a queue filling up
20
+ * somewhere.
21
+ *
22
+ * Both are tracked with a Kalman filter, which is what lets the estimate
23
+ * follow the network while telling apart a real trend from the jitter that
24
+ * every link has. The noise it measures along the way is what the detector
25
+ * compares the trend against.
26
+ *
27
+ * @remarks
28
+ * - This is what the receiving side uses, where the only thing known about
29
+ * when a packet was sent is the timestamp it carries. The sending side has
30
+ * the real instants, so it uses `TrendlineEstimator` instead, which fits a
31
+ * line rather than filtering.
32
+ */
33
+ class OveruseEstimator
34
+ {
35
+ public:
36
+ OveruseEstimator() = default;
37
+
38
+ ~OveruseEstimator() = default;
39
+
40
+ OveruseEstimator(const OveruseEstimator&) = delete;
41
+ OveruseEstimator& operator=(const OveruseEstimator&) = delete;
42
+
43
+ /**
44
+ * Feed the deltas between two consecutive groups of packets.
45
+ *
46
+ * @param arrivalDeltaMs - Time elapsed between the arrival of both groups.
47
+ * @param sendDeltaMs - Time elapsed between the send times of both groups.
48
+ * @param sizeDelta - Difference of size between both groups (bytes).
49
+ * @param currentHypothesis - What the detector makes of the network right
50
+ * now, which decides whether this sample is used to measure the noise
51
+ * and how much the filter is allowed to move.
52
+ */
53
+ void Update(
54
+ double arrivalDeltaMs,
55
+ double sendDeltaMs,
56
+ int64_t sizeDelta,
57
+ Types::BandwidthUsage currentHypothesis);
58
+
59
+ /**
60
+ * Estimated variance of the noise (ms^2).
61
+ */
62
+ double GetVarNoiseMs2() const
63
+ {
64
+ return this->varNoiseMs2;
65
+ }
66
+
67
+ /**
68
+ * Estimated part of the delay between groups that the traffic itself
69
+ * doesn't explain (ms).
70
+ */
71
+ double GetOffsetMs() const
72
+ {
73
+ return this->offsetMs;
74
+ }
75
+
76
+ /**
77
+ * Number of samples the current estimate is based on.
78
+ */
79
+ int64_t GetNumOfDeltas() const
80
+ {
81
+ return this->numOfDeltas;
82
+ }
83
+
84
+ private:
85
+ /**
86
+ * Shortest send delta of the recent history, which is what the noise
87
+ * estimate is scaled by so that it doesn't depend on how often packets
88
+ * come.
89
+ */
90
+ double UpdateMinFramePeriod(double sendDeltaMs);
91
+
92
+ void UpdateNoiseEstimate(double residual, double minFramePeriodMs, bool stableState);
93
+
94
+ private:
95
+ int64_t numOfDeltas{ 0 };
96
+ // How much of the extra delay a bigger group explains (ms per byte).
97
+ double slopeMsPerByte{ 8.0 / 512.0 };
98
+ // What is left of it once that is taken out (ms).
99
+ double offsetMs{ 0.0 };
100
+ double prevOffsetMs{ 0.0 };
101
+ // Covariance of the two values above, and how much they are allowed to
102
+ // move on their own between samples.
103
+ double e[2][2]{
104
+ { 100.0, 0.0 },
105
+ { 0.0, 1e-1 }
106
+ };
107
+ double processNoise[2]{ 1e-13, 1e-3 };
108
+ double avgNoiseMs{ 0.0 };
109
+ double varNoiseMs2{ 50.0 };
110
+ std::deque<double> sendDeltaHistMs;
111
+ };
112
+ } // namespace BWE
113
+ } // namespace RTC
114
+
115
+ #endif
@@ -0,0 +1,84 @@
1
+ #ifndef MS_RTC_BWE_PROBE_BITRATE_ESTIMATOR_HPP
2
+ #define MS_RTC_BWE_PROBE_BITRATE_ESTIMATOR_HPP
3
+
4
+ #include "common.hpp"
5
+ #include "RTC/BWE/BweTypes.hpp"
6
+ #include <ankerl/unordered_dense.h>
7
+
8
+ namespace RTC
9
+ {
10
+ namespace BWE
11
+ {
12
+ /**
13
+ * Measures what a probe actually achieved.
14
+ *
15
+ * A probe is a burst of packets sent on purpose above the current estimate,
16
+ * and the point of it is to learn the capacity of the link directly instead
17
+ * of inferring it from the delay. This class gathers the feedback of the
18
+ * packets of each burst and, once enough of the burst has come back, says at
19
+ * what bitrate it was delivered.
20
+ *
21
+ * The answer is the lower of the rate it was sent at and the rate it
22
+ * arrived at, because a burst that arrives faster than it was sent measures
23
+ * the sender and not the link. And when it arrives clearly slower than it
24
+ * was sent, that means the burst found the real capacity, so the answer is
25
+ * taken slightly below what arrived in order not to overuse it right away.
26
+ *
27
+ * A burst that too little of came back, that was sent or received over too
28
+ * long a span, or that arrived far faster than it was sent, is discarded
29
+ * rather than believed.
30
+ */
31
+ class ProbeBitrateEstimator
32
+ {
33
+ private:
34
+ /**
35
+ * What has been gathered so far about a burst.
36
+ */
37
+ struct AggregatedCluster
38
+ {
39
+ int64_t numProbes{ 0 };
40
+ std::optional<int64_t> firstSendTimeUs;
41
+ std::optional<int64_t> lastSendTimeUs;
42
+ std::optional<int64_t> firstReceiveTimeUs;
43
+ std::optional<int64_t> lastReceiveTimeUs;
44
+ // Size of the packet sent last, which the span of send times doesn't
45
+ // cover.
46
+ int64_t sizeLastSend{ 0 };
47
+ // Size of the packet received first, which the span of receive times
48
+ // doesn't cover.
49
+ int64_t sizeFirstReceive{ 0 };
50
+ int64_t sizeTotal{ 0 };
51
+ };
52
+
53
+ public:
54
+ /**
55
+ * Feed the feedback of a packet that belongs to a probe.
56
+ *
57
+ * @returns The bitrate the probe achieved (bps), or no value while its
58
+ * burst cannot be trusted yet or at all.
59
+ */
60
+ std::optional<int64_t> HandleProbeAndEstimateBitrate(const Types::PacketResult& packetResult);
61
+
62
+ /**
63
+ * Take the latest measured bitrate (bps), leaving none behind, so that a
64
+ * probe is never acted upon twice.
65
+ */
66
+ std::optional<int64_t> FetchAndResetLastEstimatedBitrate();
67
+
68
+ private:
69
+ /**
70
+ * Forget the bursts whose last packet arrived long enough ago that they
71
+ * cannot receive anything else.
72
+ */
73
+ void EraseOldClusters(int64_t nowUs);
74
+
75
+ private:
76
+ // Bursts being gathered, by their id. Nothing looks at them in any
77
+ // particular order.
78
+ ankerl::unordered_dense::map<int64_t, AggregatedCluster> clusters;
79
+ std::optional<int64_t> estimatedBitrate;
80
+ };
81
+ } // namespace BWE
82
+ } // namespace RTC
83
+
84
+ #endif