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
@@ -0,0 +1,433 @@
1
+ #define MS_CLASS "RTC::BWE::TargetRateController"
2
+ // #define MS_LOG_DEV_LEVEL 3
3
+
4
+ #include "RTC/BWE/TargetRateController.hpp"
5
+ #include "Logger.hpp"
6
+ #include "RTC/BWE/BitrateUtils.hpp"
7
+ #include "RTC/Consts.hpp"
8
+
9
+ namespace RTC
10
+ {
11
+ namespace BWE
12
+ {
13
+ /* Static. */
14
+
15
+ // How far back the lowest target is looked for when increasing.
16
+ static constexpr int64_t BweIncreaseIntervalUs{ 1000 * 1000 };
17
+ // Minimum time between two decreases caused by loss, on top of a round trip
18
+ // time.
19
+ static constexpr int64_t BweDecreaseIntervalUs{ 300 * 1000 };
20
+ // How long the target is allowed to just follow its bounds after the first
21
+ // loss report.
22
+ static constexpr int64_t StartPhaseUs{ 2000 * 1000 };
23
+ // Packets a loss report must cover before its fraction of lost packets says
24
+ // anything.
25
+ static constexpr int64_t LimitNumPackets{ 20 };
26
+ // Age over which the latest loss report is not acted upon anymore. Reports
27
+ // are expected within [0.5, 1.5] s intervals, so this is 1.2 times the
28
+ // longest interval that still counts as uniform.
29
+ static constexpr int64_t MaxLossReportAgeUs{ 6000 * 1000 };
30
+
31
+ /* Instance methods. */
32
+
33
+ TargetRateController::TargetRateController()
34
+ : TargetRateController(TargetRateControllerOptions{})
35
+ {
36
+ MS_TRACE();
37
+ }
38
+
39
+ TargetRateController::TargetRateController(TargetRateControllerOptions options)
40
+ : options(options),
41
+ minBitrateConfigured(Consts::BweMinBitrate),
42
+ maxBitrateConfigured(Consts::BweMaxBitrate)
43
+ {
44
+ MS_TRACE();
45
+
46
+ this->lossBasedController.SetBitrateLimits(
47
+ this->minBitrateConfigured, this->maxBitrateConfigured);
48
+ }
49
+
50
+ int64_t TargetRateController::GetTargetBitrate() const
51
+ {
52
+ MS_TRACE();
53
+
54
+ int64_t target = this->currentTarget;
55
+
56
+ // Unless it has been told to drive the target, the bound asked for by the
57
+ // receiver caps only the value handed out, which is why it's applied here
58
+ // and not in GetUpperLimit().
59
+ if (!this->options.disableReceiverLimitCapsOnly)
60
+ {
61
+ target = std::min(target, this->receiverLimit);
62
+ }
63
+
64
+ return std::max(this->minBitrateConfigured, target);
65
+ }
66
+
67
+ void TargetRateController::OnRouteChange()
68
+ {
69
+ MS_TRACE();
70
+
71
+ this->lostPacketsSinceLastLossUpdate = 0;
72
+ this->expectedPacketsSinceLastLossUpdate = 0;
73
+ this->currentTarget = 0;
74
+ this->minBitrateConfigured = Consts::BweMinBitrate;
75
+ this->maxBitrateConfigured = Consts::BweMaxBitrate;
76
+ this->hasDecreasedSinceLastFractionLoss = false;
77
+ this->lastFractionLost = 0;
78
+ this->lastRttUs = 0;
79
+ this->receiverLimit = Types::BitrateInfinite;
80
+ this->delayBasedLimit = Types::BitrateInfinite;
81
+ this->firstLossReportAtUs.reset();
82
+ this->lastLossReportAtUs.reset();
83
+ this->lastDecreaseAtUs.reset();
84
+
85
+ // A loss controller that is allowed to take over during the start phase
86
+ // would otherwise carry what it learnt about the previous path into the
87
+ // start phase that is about to begin again.
88
+ if (this->lossBasedController.IsUsedInStartPhase())
89
+ {
90
+ this->lossBasedController.Reset();
91
+ }
92
+ }
93
+
94
+ void TargetRateController::SetBitrateLimits(int64_t minBitrate, int64_t maxBitrate)
95
+ {
96
+ MS_TRACE();
97
+
98
+ this->minBitrateConfigured = std::max(minBitrate, Consts::BweMinBitrate);
99
+
100
+ // No maximum at all is not a maximum of every bitrate there is, it means
101
+ // that the one this controller picks for itself applies.
102
+ if (maxBitrate > 0 && maxBitrate != Types::BitrateInfinite)
103
+ {
104
+ this->maxBitrateConfigured = std::max(this->minBitrateConfigured, maxBitrate);
105
+ }
106
+ else
107
+ {
108
+ this->maxBitrateConfigured = Consts::BweMaxBitrate;
109
+ }
110
+
111
+ this->lossBasedController.SetBitrateLimits(
112
+ this->minBitrateConfigured, this->maxBitrateConfigured);
113
+ }
114
+
115
+ void TargetRateController::SetAcknowledgedBitrate(int64_t acknowledgedBitrate)
116
+ {
117
+ MS_TRACE();
118
+
119
+ // Not knowing what the link delivers is not the same as it delivering
120
+ // nothing, so the last figure that was known is kept instead.
121
+ if (acknowledgedBitrate == Types::BitrateInfinite)
122
+ {
123
+ return;
124
+ }
125
+
126
+ // NOTE: Nothing else in here looks at it, it's only of use to the loss
127
+ // controller.
128
+ this->lossBasedController.SetAcknowledgedBitrate(acknowledgedBitrate);
129
+ }
130
+
131
+ void TargetRateController::UpdateLossBasedController(
132
+ const std::vector<Types::PacketResult>& packetResults, bool inAlr, int64_t nowUs)
133
+ {
134
+ MS_TRACE();
135
+
136
+ this->lossBasedController.UpdateBitrateEstimate(packetResults, this->delayBasedLimit, inAlr);
137
+
138
+ Update(nowUs);
139
+ }
140
+
141
+ void TargetRateController::SetSendBitrate(int64_t bitrate)
142
+ {
143
+ MS_TRACE();
144
+
145
+ // Drop the bound of the delay based path so that it cannot cap the given
146
+ // value.
147
+ this->delayBasedLimit = Types::BitrateInfinite;
148
+
149
+ // NOTE: A bitrate outside the configured bounds is brought into them
150
+ // rather than rejected, since this value comes from the application.
151
+ SetTargetBitrate(bitrate);
152
+
153
+ // Clear the history so that the new value is grown from directly instead
154
+ // of from whatever the target was before.
155
+ this->minBitrateHistory.clear();
156
+ }
157
+
158
+ void TargetRateController::SetDelayBasedEstimate(int64_t bitrate)
159
+ {
160
+ MS_TRACE();
161
+
162
+ this->delayBasedLimit = bitrate == 0 ? Types::BitrateInfinite : bitrate;
163
+
164
+ ApplyTargetLimits();
165
+ }
166
+
167
+ void TargetRateController::SetReceiverEstimate(int64_t bitrate)
168
+ {
169
+ MS_TRACE();
170
+
171
+ this->receiverLimit = bitrate == 0 ? Types::BitrateInfinite : bitrate;
172
+
173
+ ApplyTargetLimits();
174
+ }
175
+
176
+ void TargetRateController::UpdatePacketsLost(int64_t lostPackets, int64_t totalPackets, int64_t nowUs)
177
+ {
178
+ MS_TRACE();
179
+
180
+ if (!this->firstLossReportAtUs.has_value())
181
+ {
182
+ this->firstLossReportAtUs = nowUs;
183
+ }
184
+
185
+ if (totalPackets <= 0)
186
+ {
187
+ return;
188
+ }
189
+
190
+ const int64_t expectedPackets = this->expectedPacketsSinceLastLossUpdate + totalPackets;
191
+
192
+ // Accumulate reports until they cover enough packets.
193
+ if (expectedPackets < LimitNumPackets)
194
+ {
195
+ this->expectedPacketsSinceLastLossUpdate = expectedPackets;
196
+ this->lostPacketsSinceLastLossUpdate += lostPackets;
197
+
198
+ return;
199
+ }
200
+
201
+ this->hasDecreasedSinceLastFractionLoss = false;
202
+
203
+ // NOTE: The reported fraction is in 1/256 units, hence the shift.
204
+ const int64_t lostQ8 =
205
+ std::max<int64_t>(this->lostPacketsSinceLastLossUpdate + lostPackets, 0) << 8;
206
+
207
+ this->lastFractionLost = static_cast<uint8_t>(std::min<int64_t>(lostQ8 / expectedPackets, 255));
208
+
209
+ this->lostPacketsSinceLastLossUpdate = 0;
210
+ this->expectedPacketsSinceLastLossUpdate = 0;
211
+ this->lastLossReportAtUs = nowUs;
212
+
213
+ Update(nowUs);
214
+ }
215
+
216
+ void TargetRateController::UpdateRtt(int64_t rttUs)
217
+ {
218
+ MS_TRACE();
219
+
220
+ // A round trip time cannot always be computed, since it needs a sender
221
+ // report to refer to.
222
+ if (rttUs > 0)
223
+ {
224
+ this->lastRttUs = rttUs;
225
+ }
226
+ }
227
+
228
+ void TargetRateController::UpdatePropagationRtt(int64_t propagationRttUs, int64_t nowUs)
229
+ {
230
+ MS_TRACE();
231
+
232
+ this->rttBackoff.lastPropagationRttAtUs = nowUs;
233
+ this->rttBackoff.lastPropagationRttUs = propagationRttUs;
234
+ }
235
+
236
+ void TargetRateController::OnPacketSent(int64_t sentAtUs)
237
+ {
238
+ MS_TRACE();
239
+
240
+ this->rttBackoff.lastPacketSentAtUs = sentAtUs;
241
+ }
242
+
243
+ void TargetRateController::Update(int64_t nowUs)
244
+ {
245
+ MS_TRACE();
246
+
247
+ // A round trip time this long means that the queues of the network are
248
+ // full, which neither loss nor delay may have noticed yet.
249
+ if (IsRttAboveLimit())
250
+ {
251
+ const bool mayDecrease =
252
+ !this->lastDecreaseAtUs.has_value() ||
253
+ nowUs - this->lastDecreaseAtUs.value() >= this->options.rttBackoffDropIntervalUs;
254
+
255
+ if (mayDecrease && this->currentTarget > this->options.rttBackoffBitrateFloor)
256
+ {
257
+ this->lastDecreaseAtUs = nowUs;
258
+
259
+ const auto bitrate = std::max<int64_t>(
260
+ BitrateUtils::ApplyBitrateFactor(
261
+ this->currentTarget, this->options.rttBackoffDropFraction),
262
+ this->options.rttBackoffBitrateFloor);
263
+
264
+ SetTargetBitrate(bitrate);
265
+ }
266
+ else
267
+ {
268
+ ApplyTargetLimits();
269
+ }
270
+
271
+ return;
272
+ }
273
+
274
+ // The bounds are trusted during the first seconds as long as no loss has
275
+ // been reported, so that the initial probing can raise the target.
276
+ if (this->lastFractionLost == 0 && IsInStartPhase(nowUs) && !this->lossBasedController.IsReadyToUseInStartPhase())
277
+ {
278
+ int64_t bitrate = this->currentTarget;
279
+
280
+ if (this->receiverLimit != Types::BitrateInfinite)
281
+ {
282
+ bitrate = std::max(this->receiverLimit, bitrate);
283
+ }
284
+
285
+ if (this->delayBasedLimit != Types::BitrateInfinite)
286
+ {
287
+ bitrate = std::max(this->delayBasedLimit, bitrate);
288
+ }
289
+
290
+ if (bitrate != this->currentTarget)
291
+ {
292
+ // Take the target being left behind as the value to grow from, since
293
+ // the new one was not reached by increasing.
294
+ this->minBitrateHistory.clear();
295
+ this->minBitrateHistory.emplace_back(nowUs, this->currentTarget);
296
+
297
+ SetTargetBitrate(bitrate);
298
+
299
+ return;
300
+ }
301
+ }
302
+
303
+ UpdateMinBitrateHistory(nowUs);
304
+
305
+ // Once the loss controller can tell congestion from the loss the link has
306
+ // by itself, its estimate replaces the coarse rules below.
307
+ if (this->lossBasedController.IsReady())
308
+ {
309
+ const auto result = this->lossBasedController.GetResult();
310
+
311
+ this->lossBasedState = result.state;
312
+
313
+ SetTargetBitrate(result.bitrate);
314
+
315
+ return;
316
+ }
317
+
318
+ // No loss report has covered enough packets yet.
319
+ if (!this->lastLossReportAtUs.has_value())
320
+ {
321
+ ApplyTargetLimits();
322
+
323
+ return;
324
+ }
325
+
326
+ if (nowUs - this->lastLossReportAtUs.value() < MaxLossReportAgeUs)
327
+ {
328
+ const double loss = this->lastFractionLost / 256.0;
329
+
330
+ if (this->currentTarget < this->options.bitrateThreshold || loss <= this->options.lowLossThreshold)
331
+ {
332
+ // Increase by 8% of the lowest target of the last second. Growing
333
+ // from the lowest value instead of from the current one lets a sender
334
+ // that was throttled ramp up a second faster.
335
+ // Add a bit on top of the 8%, which is what keeps the target from
336
+ // getting stuck at low bitrates and is negligible at high ones.
337
+ const auto bitrate = BitrateUtils::AddBitrates(
338
+ BitrateUtils::ApplyBitrateFactor(this->minBitrateHistory.front().second, 1.08), 1000);
339
+
340
+ SetTargetBitrate(bitrate);
341
+
342
+ return;
343
+ }
344
+
345
+ // Loss between both thresholds means doing nothing at all.
346
+ if (this->currentTarget > this->options.bitrateThreshold && loss > this->options.highLossThreshold)
347
+ {
348
+ const bool mayDecrease =
349
+ !this->lastDecreaseAtUs.has_value() ||
350
+ nowUs - this->lastDecreaseAtUs.value() >= BweDecreaseIntervalUs + this->lastRttUs;
351
+
352
+ if (!this->hasDecreasedSinceLastFractionLoss && mayDecrease)
353
+ {
354
+ this->lastDecreaseAtUs = nowUs;
355
+
356
+ // The new bitrate is the current one times (1 - 0.5 * lossRate),
357
+ // where the reported fraction is 256 times that loss rate.
358
+ const auto bitrate = static_cast<int64_t>(
359
+ (this->currentTarget * static_cast<double>(512 - this->lastFractionLost)) / 512.0);
360
+
361
+ this->hasDecreasedSinceLastFractionLoss = true;
362
+
363
+ SetTargetBitrate(bitrate);
364
+
365
+ return;
366
+ }
367
+ }
368
+ }
369
+
370
+ ApplyTargetLimits();
371
+ }
372
+
373
+ bool TargetRateController::IsInStartPhase(int64_t nowUs) const
374
+ {
375
+ MS_TRACE();
376
+
377
+ return !this->firstLossReportAtUs.has_value() ||
378
+ nowUs - this->firstLossReportAtUs.value() < StartPhaseUs;
379
+ }
380
+
381
+ void TargetRateController::UpdateMinBitrateHistory(int64_t nowUs)
382
+ {
383
+ MS_TRACE();
384
+
385
+ // Drop the points that fell out of the window. The window is closed a
386
+ // millisecond early, so that a target which has only just aged out of it
387
+ // doesn't hold the increase back.
388
+ while (!this->minBitrateHistory.empty() &&
389
+ nowUs - this->minBitrateHistory.front().first + 1000 > BweIncreaseIntervalUs)
390
+ {
391
+ this->minBitrateHistory.pop_front();
392
+ }
393
+
394
+ // Sliding window minimum: drop the points not lower than the current
395
+ // target before pushing it, since they can never be the minimum again.
396
+ while (!this->minBitrateHistory.empty() &&
397
+ this->currentTarget <= this->minBitrateHistory.back().second)
398
+ {
399
+ this->minBitrateHistory.pop_back();
400
+ }
401
+
402
+ this->minBitrateHistory.emplace_back(nowUs, this->currentTarget);
403
+ }
404
+
405
+ int64_t TargetRateController::GetUpperLimit() const
406
+ {
407
+ MS_TRACE();
408
+
409
+ int64_t upperLimit = this->delayBasedLimit;
410
+
411
+ if (this->options.disableReceiverLimitCapsOnly)
412
+ {
413
+ upperLimit = std::min(upperLimit, this->receiverLimit);
414
+ }
415
+
416
+ return std::min(upperLimit, this->maxBitrateConfigured);
417
+ }
418
+
419
+ void TargetRateController::SetTargetBitrate(int64_t bitrate)
420
+ {
421
+ MS_TRACE();
422
+
423
+ this->currentTarget = std::max(this->minBitrateConfigured, std::min(bitrate, GetUpperLimit()));
424
+ }
425
+
426
+ void TargetRateController::ApplyTargetLimits()
427
+ {
428
+ MS_TRACE();
429
+
430
+ SetTargetBitrate(this->currentTarget);
431
+ }
432
+ } // namespace BWE
433
+ } // namespace RTC