• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 #include "modules/remote_bitrate_estimator/aimd_rate_control.h"
11 
12 #include <memory>
13 
14 #include "api/transport/field_trial_based_config.h"
15 #include "api/units/data_rate.h"
16 #include "system_wrappers/include/clock.h"
17 #include "test/field_trial.h"
18 #include "test/gtest.h"
19 
20 namespace webrtc {
21 namespace {
22 
23 constexpr int64_t kClockInitialTime = 123456;
24 
25 constexpr int kMinBwePeriodMs = 2000;
26 constexpr int kDefaultPeriodMs = 3000;
27 constexpr int kMaxBwePeriodMs = 50000;
28 
29 // After an overuse, we back off to 85% to the received bitrate.
30 constexpr double kFractionAfterOveruse = 0.85;
31 
32 struct AimdRateControlStates {
33   std::unique_ptr<AimdRateControl> aimd_rate_control;
34   std::unique_ptr<SimulatedClock> simulated_clock;
35   FieldTrialBasedConfig field_trials;
36 };
37 
CreateAimdRateControlStates(bool send_side=false)38 AimdRateControlStates CreateAimdRateControlStates(bool send_side = false) {
39   AimdRateControlStates states;
40   states.aimd_rate_control.reset(
41       new AimdRateControl(&states.field_trials, send_side));
42   states.simulated_clock.reset(new SimulatedClock(kClockInitialTime));
43   return states;
44 }
OptionalRateFromOptionalBps(absl::optional<int> bitrate_bps)45 absl::optional<DataRate> OptionalRateFromOptionalBps(
46     absl::optional<int> bitrate_bps) {
47   if (bitrate_bps) {
48     return DataRate::BitsPerSec(*bitrate_bps);
49   } else {
50     return absl::nullopt;
51   }
52 }
UpdateRateControl(const AimdRateControlStates & states,const BandwidthUsage & bandwidth_usage,absl::optional<uint32_t> throughput_estimate,int64_t now_ms)53 void UpdateRateControl(const AimdRateControlStates& states,
54                        const BandwidthUsage& bandwidth_usage,
55                        absl::optional<uint32_t> throughput_estimate,
56                        int64_t now_ms) {
57   RateControlInput input(bandwidth_usage,
58                          OptionalRateFromOptionalBps(throughput_estimate));
59   states.aimd_rate_control->Update(&input, Timestamp::Millis(now_ms));
60 }
SetEstimate(const AimdRateControlStates & states,int bitrate_bps)61 void SetEstimate(const AimdRateControlStates& states, int bitrate_bps) {
62   states.aimd_rate_control->SetEstimate(DataRate::BitsPerSec(bitrate_bps),
63                                         states.simulated_clock->CurrentTime());
64 }
65 
66 }  // namespace
67 
TEST(AimdRateControlTest,MinNearMaxIncreaseRateOnLowBandwith)68 TEST(AimdRateControlTest, MinNearMaxIncreaseRateOnLowBandwith) {
69   auto states = CreateAimdRateControlStates();
70   constexpr int kBitrate = 30000;
71   SetEstimate(states, kBitrate);
72   EXPECT_EQ(4000,
73             states.aimd_rate_control->GetNearMaxIncreaseRateBpsPerSecond());
74 }
75 
TEST(AimdRateControlTest,NearMaxIncreaseRateIs5kbpsOn90kbpsAnd200msRtt)76 TEST(AimdRateControlTest, NearMaxIncreaseRateIs5kbpsOn90kbpsAnd200msRtt) {
77   auto states = CreateAimdRateControlStates();
78   constexpr int kBitrate = 90000;
79   SetEstimate(states, kBitrate);
80   EXPECT_EQ(5000,
81             states.aimd_rate_control->GetNearMaxIncreaseRateBpsPerSecond());
82 }
83 
TEST(AimdRateControlTest,NearMaxIncreaseRateIs5kbpsOn60kbpsAnd100msRtt)84 TEST(AimdRateControlTest, NearMaxIncreaseRateIs5kbpsOn60kbpsAnd100msRtt) {
85   auto states = CreateAimdRateControlStates();
86   constexpr int kBitrate = 60000;
87   SetEstimate(states, kBitrate);
88   states.aimd_rate_control->SetRtt(TimeDelta::Millis(100));
89   EXPECT_EQ(5000,
90             states.aimd_rate_control->GetNearMaxIncreaseRateBpsPerSecond());
91 }
92 
TEST(AimdRateControlTest,GetIncreaseRateAndBandwidthPeriod)93 TEST(AimdRateControlTest, GetIncreaseRateAndBandwidthPeriod) {
94   // Smoothing experiment disabled
95   auto states = CreateAimdRateControlStates();
96   constexpr int kBitrate = 300000;
97   SetEstimate(states, kBitrate);
98   UpdateRateControl(states, BandwidthUsage::kBwOverusing, kBitrate,
99                     states.simulated_clock->TimeInMilliseconds());
100   EXPECT_NEAR(14000,
101               states.aimd_rate_control->GetNearMaxIncreaseRateBpsPerSecond(),
102               1000);
103   EXPECT_EQ(kDefaultPeriodMs,
104             states.aimd_rate_control->GetExpectedBandwidthPeriod().ms());
105 }
106 
TEST(AimdRateControlTest,BweLimitedByAckedBitrate)107 TEST(AimdRateControlTest, BweLimitedByAckedBitrate) {
108   auto states = CreateAimdRateControlStates();
109   constexpr int kAckedBitrate = 10000;
110   SetEstimate(states, kAckedBitrate);
111   while (states.simulated_clock->TimeInMilliseconds() - kClockInitialTime <
112          20000) {
113     UpdateRateControl(states, BandwidthUsage::kBwNormal, kAckedBitrate,
114                       states.simulated_clock->TimeInMilliseconds());
115     states.simulated_clock->AdvanceTimeMilliseconds(100);
116   }
117   ASSERT_TRUE(states.aimd_rate_control->ValidEstimate());
118   EXPECT_EQ(static_cast<uint32_t>(1.5 * kAckedBitrate + 10000),
119             states.aimd_rate_control->LatestEstimate().bps());
120 }
121 
TEST(AimdRateControlTest,BweNotLimitedByDecreasingAckedBitrate)122 TEST(AimdRateControlTest, BweNotLimitedByDecreasingAckedBitrate) {
123   auto states = CreateAimdRateControlStates();
124   constexpr int kAckedBitrate = 100000;
125   SetEstimate(states, kAckedBitrate);
126   while (states.simulated_clock->TimeInMilliseconds() - kClockInitialTime <
127          20000) {
128     UpdateRateControl(states, BandwidthUsage::kBwNormal, kAckedBitrate,
129                       states.simulated_clock->TimeInMilliseconds());
130     states.simulated_clock->AdvanceTimeMilliseconds(100);
131   }
132   ASSERT_TRUE(states.aimd_rate_control->ValidEstimate());
133   // If the acked bitrate decreases the BWE shouldn't be reduced to 1.5x
134   // what's being acked, but also shouldn't get to increase more.
135   uint32_t prev_estimate = states.aimd_rate_control->LatestEstimate().bps();
136   UpdateRateControl(states, BandwidthUsage::kBwNormal, kAckedBitrate / 2,
137                     states.simulated_clock->TimeInMilliseconds());
138   uint32_t new_estimate = states.aimd_rate_control->LatestEstimate().bps();
139   EXPECT_NEAR(new_estimate, static_cast<uint32_t>(1.5 * kAckedBitrate + 10000),
140               2000);
141   EXPECT_EQ(new_estimate, prev_estimate);
142 }
143 
TEST(AimdRateControlTest,DefaultPeriodUntilFirstOveruse)144 TEST(AimdRateControlTest, DefaultPeriodUntilFirstOveruse) {
145   // Smoothing experiment disabled
146   auto states = CreateAimdRateControlStates();
147   states.aimd_rate_control->SetStartBitrate(DataRate::KilobitsPerSec(300));
148   EXPECT_EQ(kDefaultPeriodMs,
149             states.aimd_rate_control->GetExpectedBandwidthPeriod().ms());
150   states.simulated_clock->AdvanceTimeMilliseconds(100);
151   UpdateRateControl(states, BandwidthUsage::kBwOverusing, 280000,
152                     states.simulated_clock->TimeInMilliseconds());
153   EXPECT_NE(kDefaultPeriodMs,
154             states.aimd_rate_control->GetExpectedBandwidthPeriod().ms());
155 }
156 
TEST(AimdRateControlTest,ExpectedPeriodAfter20kbpsDropAnd5kbpsIncrease)157 TEST(AimdRateControlTest, ExpectedPeriodAfter20kbpsDropAnd5kbpsIncrease) {
158   auto states = CreateAimdRateControlStates();
159   constexpr int kInitialBitrate = 110000;
160   SetEstimate(states, kInitialBitrate);
161   states.simulated_clock->AdvanceTimeMilliseconds(100);
162   // Make the bitrate drop by 20 kbps to get to 90 kbps.
163   // The rate increase at 90 kbps should be 5 kbps, so the period should be 4 s.
164   constexpr int kAckedBitrate =
165       (kInitialBitrate - 20000) / kFractionAfterOveruse;
166   UpdateRateControl(states, BandwidthUsage::kBwOverusing, kAckedBitrate,
167                     states.simulated_clock->TimeInMilliseconds());
168   EXPECT_EQ(5000,
169             states.aimd_rate_control->GetNearMaxIncreaseRateBpsPerSecond());
170   EXPECT_EQ(4000, states.aimd_rate_control->GetExpectedBandwidthPeriod().ms());
171 }
172 
TEST(AimdRateControlTest,BandwidthPeriodIsNotBelowMin)173 TEST(AimdRateControlTest, BandwidthPeriodIsNotBelowMin) {
174   auto states = CreateAimdRateControlStates();
175   constexpr int kInitialBitrate = 10000;
176   SetEstimate(states, kInitialBitrate);
177   states.simulated_clock->AdvanceTimeMilliseconds(100);
178   // Make a small (1.5 kbps) bitrate drop to 8.5 kbps.
179   UpdateRateControl(states, BandwidthUsage::kBwOverusing, kInitialBitrate - 1,
180                     states.simulated_clock->TimeInMilliseconds());
181   EXPECT_EQ(kMinBwePeriodMs,
182             states.aimd_rate_control->GetExpectedBandwidthPeriod().ms());
183 }
184 
TEST(AimdRateControlTest,BandwidthPeriodIsNotAboveMaxNoSmoothingExp)185 TEST(AimdRateControlTest, BandwidthPeriodIsNotAboveMaxNoSmoothingExp) {
186   auto states = CreateAimdRateControlStates();
187   constexpr int kInitialBitrate = 10010000;
188   SetEstimate(states, kInitialBitrate);
189   states.simulated_clock->AdvanceTimeMilliseconds(100);
190   // Make a large (10 Mbps) bitrate drop to 10 kbps.
191   constexpr int kAckedBitrate = 10000 / kFractionAfterOveruse;
192   UpdateRateControl(states, BandwidthUsage::kBwOverusing, kAckedBitrate,
193                     states.simulated_clock->TimeInMilliseconds());
194   EXPECT_EQ(kMaxBwePeriodMs,
195             states.aimd_rate_control->GetExpectedBandwidthPeriod().ms());
196 }
197 
TEST(AimdRateControlTest,SendingRateBoundedWhenThroughputNotEstimated)198 TEST(AimdRateControlTest, SendingRateBoundedWhenThroughputNotEstimated) {
199   auto states = CreateAimdRateControlStates();
200   constexpr int kInitialBitrateBps = 123000;
201   UpdateRateControl(states, BandwidthUsage::kBwNormal, kInitialBitrateBps,
202                     states.simulated_clock->TimeInMilliseconds());
203   // AimdRateControl sets the initial bit rate to what it receives after
204   // five seconds has passed.
205   // TODO(bugs.webrtc.org/9379): The comment in the AimdRateControl does not
206   // match the constant.
207   constexpr int kInitializationTimeMs = 5000;
208   states.simulated_clock->AdvanceTimeMilliseconds(kInitializationTimeMs + 1);
209   UpdateRateControl(states, BandwidthUsage::kBwNormal, kInitialBitrateBps,
210                     states.simulated_clock->TimeInMilliseconds());
211   for (int i = 0; i < 100; ++i) {
212     UpdateRateControl(states, BandwidthUsage::kBwNormal, absl::nullopt,
213                       states.simulated_clock->TimeInMilliseconds());
214     states.simulated_clock->AdvanceTimeMilliseconds(100);
215   }
216   EXPECT_LE(states.aimd_rate_control->LatestEstimate().bps(),
217             kInitialBitrateBps * 1.5 + 10000);
218 }
219 
TEST(AimdRateControlTest,EstimateDoesNotIncreaseInAlr)220 TEST(AimdRateControlTest, EstimateDoesNotIncreaseInAlr) {
221   // When alr is detected, the delay based estimator is not allowed to increase
222   // bwe since there will be no feedback from the network if the new estimate
223   // is correct.
224   test::ScopedFieldTrials override_field_trials(
225       "WebRTC-DontIncreaseDelayBasedBweInAlr/Enabled/");
226   auto states = CreateAimdRateControlStates(/*send_side=*/true);
227   constexpr int kInitialBitrateBps = 123000;
228   SetEstimate(states, kInitialBitrateBps);
229   states.aimd_rate_control->SetInApplicationLimitedRegion(true);
230   UpdateRateControl(states, BandwidthUsage::kBwNormal, kInitialBitrateBps,
231                     states.simulated_clock->TimeInMilliseconds());
232   ASSERT_EQ(states.aimd_rate_control->LatestEstimate().bps(),
233             kInitialBitrateBps);
234 
235   for (int i = 0; i < 100; ++i) {
236     UpdateRateControl(states, BandwidthUsage::kBwNormal, absl::nullopt,
237                       states.simulated_clock->TimeInMilliseconds());
238     states.simulated_clock->AdvanceTimeMilliseconds(100);
239   }
240   EXPECT_EQ(states.aimd_rate_control->LatestEstimate().bps(),
241             kInitialBitrateBps);
242 }
243 
TEST(AimdRateControlTest,SetEstimateIncreaseBweInAlr)244 TEST(AimdRateControlTest, SetEstimateIncreaseBweInAlr) {
245   test::ScopedFieldTrials override_field_trials(
246       "WebRTC-DontIncreaseDelayBasedBweInAlr/Enabled/");
247   auto states = CreateAimdRateControlStates(/*send_side=*/true);
248   constexpr int kInitialBitrateBps = 123000;
249   SetEstimate(states, kInitialBitrateBps);
250   states.aimd_rate_control->SetInApplicationLimitedRegion(true);
251   ASSERT_EQ(states.aimd_rate_control->LatestEstimate().bps(),
252             kInitialBitrateBps);
253   SetEstimate(states, 2 * kInitialBitrateBps);
254   EXPECT_EQ(states.aimd_rate_control->LatestEstimate().bps(),
255             2 * kInitialBitrateBps);
256 }
257 
TEST(AimdRateControlTest,SetEstimateUpperLimitedByNetworkEstimate)258 TEST(AimdRateControlTest, SetEstimateUpperLimitedByNetworkEstimate) {
259   auto states = CreateAimdRateControlStates(/*send_side=*/true);
260   NetworkStateEstimate network_estimate;
261   network_estimate.link_capacity_upper = DataRate::KilobitsPerSec(400);
262   states.aimd_rate_control->SetNetworkStateEstimate(network_estimate);
263   SetEstimate(states, 500'000);
264   EXPECT_EQ(states.aimd_rate_control->LatestEstimate(),
265             network_estimate.link_capacity_upper);
266 }
267 
TEST(AimdRateControlTest,SetEstimateLowerLimitedByNetworkEstimate)268 TEST(AimdRateControlTest, SetEstimateLowerLimitedByNetworkEstimate) {
269   auto states = CreateAimdRateControlStates(/*send_side=*/true);
270   NetworkStateEstimate network_estimate;
271   network_estimate.link_capacity_lower = DataRate::KilobitsPerSec(400);
272   states.aimd_rate_control->SetNetworkStateEstimate(network_estimate);
273   SetEstimate(states, 100'000);
274   // 0.85 is default backoff factor. (`beta_`)
275   EXPECT_EQ(states.aimd_rate_control->LatestEstimate(),
276             network_estimate.link_capacity_lower * 0.85);
277 }
278 
TEST(AimdRateControlTest,SetEstimateIgnoredIfLowerThanNetworkEstimateAndCurrent)279 TEST(AimdRateControlTest,
280      SetEstimateIgnoredIfLowerThanNetworkEstimateAndCurrent) {
281   auto states = CreateAimdRateControlStates(/*send_side=*/true);
282   SetEstimate(states, 200'000);
283   ASSERT_EQ(states.aimd_rate_control->LatestEstimate().kbps(), 200);
284   NetworkStateEstimate network_estimate;
285   network_estimate.link_capacity_lower = DataRate::KilobitsPerSec(400);
286   states.aimd_rate_control->SetNetworkStateEstimate(network_estimate);
287   // Ignore the next SetEstimate, since the estimate is lower than 85% of
288   // the network estimate.
289   SetEstimate(states, 100'000);
290   EXPECT_EQ(states.aimd_rate_control->LatestEstimate().kbps(), 200);
291 }
292 
TEST(AimdRateControlTest,SetEstimateIgnoresNetworkEstimatesLowerThanCurrent)293 TEST(AimdRateControlTest, SetEstimateIgnoresNetworkEstimatesLowerThanCurrent) {
294   test::ScopedFieldTrials override_field_trials(
295       "WebRTC-Bwe-EstimateBoundedIncrease/"
296       "ratio:0.85,ignore_acked:true,ignore_decr:true/");
297   auto states = CreateAimdRateControlStates(/*send_side=*/true);
298   states.aimd_rate_control->SetStartBitrate(DataRate::KilobitsPerSec(30));
299   NetworkStateEstimate network_estimate;
300   network_estimate.link_capacity_upper = DataRate::KilobitsPerSec(400);
301   states.aimd_rate_control->SetNetworkStateEstimate(network_estimate);
302   SetEstimate(states, 500'000);
303   ASSERT_EQ(states.aimd_rate_control->LatestEstimate(),
304             network_estimate.link_capacity_upper * 0.85);
305 
306   NetworkStateEstimate lower_network_estimate;
307   lower_network_estimate.link_capacity_upper = DataRate::KilobitsPerSec(300);
308   states.aimd_rate_control->SetNetworkStateEstimate(lower_network_estimate);
309   SetEstimate(states, 500'000);
310   EXPECT_EQ(states.aimd_rate_control->LatestEstimate(),
311             network_estimate.link_capacity_upper * 0.85);
312 }
313 
TEST(AimdRateControlTest,EstimateIncreaseWhileNotInAlr)314 TEST(AimdRateControlTest, EstimateIncreaseWhileNotInAlr) {
315   // Allow the estimate to increase as long as alr is not detected to ensure
316   // tha BWE can not get stuck at a certain bitrate.
317   test::ScopedFieldTrials override_field_trials(
318       "WebRTC-DontIncreaseDelayBasedBweInAlr/Enabled/");
319   auto states = CreateAimdRateControlStates(/*send_side=*/true);
320   constexpr int kInitialBitrateBps = 123000;
321   SetEstimate(states, kInitialBitrateBps);
322   states.aimd_rate_control->SetInApplicationLimitedRegion(false);
323   UpdateRateControl(states, BandwidthUsage::kBwNormal, kInitialBitrateBps,
324                     states.simulated_clock->TimeInMilliseconds());
325   for (int i = 0; i < 100; ++i) {
326     UpdateRateControl(states, BandwidthUsage::kBwNormal, absl::nullopt,
327                       states.simulated_clock->TimeInMilliseconds());
328     states.simulated_clock->AdvanceTimeMilliseconds(100);
329   }
330   EXPECT_GT(states.aimd_rate_control->LatestEstimate().bps(),
331             kInitialBitrateBps);
332 }
333 
TEST(AimdRateControlTest,EstimateNotLimitedByNetworkEstimateIfDisabled)334 TEST(AimdRateControlTest, EstimateNotLimitedByNetworkEstimateIfDisabled) {
335   test::ScopedFieldTrials override_field_trials(
336       "WebRTC-Bwe-EstimateBoundedIncrease/Disabled/");
337   auto states = CreateAimdRateControlStates(/*send_side=*/true);
338   constexpr int kInitialBitrateBps = 123000;
339   SetEstimate(states, kInitialBitrateBps);
340   states.aimd_rate_control->SetInApplicationLimitedRegion(false);
341   NetworkStateEstimate network_estimate;
342   network_estimate.link_capacity_upper = DataRate::KilobitsPerSec(150);
343   states.aimd_rate_control->SetNetworkStateEstimate(network_estimate);
344 
345   for (int i = 0; i < 100; ++i) {
346     UpdateRateControl(states, BandwidthUsage::kBwNormal, absl::nullopt,
347                       states.simulated_clock->TimeInMilliseconds());
348     states.simulated_clock->AdvanceTimeMilliseconds(100);
349   }
350   EXPECT_GT(states.aimd_rate_control->LatestEstimate(),
351             network_estimate.link_capacity_upper);
352 }
353 
TEST(AimdRateControlTest,EstimateSlowlyIncreaseToUpperLinkCapacityEstimateIfConfigured)354 TEST(AimdRateControlTest,
355      EstimateSlowlyIncreaseToUpperLinkCapacityEstimateIfConfigured) {
356   // Even if alr is detected, the delay based estimator is allowed to increase
357   // up to a percentage of upper link capacity.
358   test::ScopedFieldTrials override_field_trials(
359       "WebRTC-Bwe-EstimateBoundedIncrease/"
360       "ratio:0.85,ignore_acked:true,immediate_incr:false/"
361       "WebRTC-DontIncreaseDelayBasedBweInAlr/Enabled/");
362   auto states = CreateAimdRateControlStates(/*send_side=*/true);
363   constexpr int kInitialBitrateBps = 123000;
364   SetEstimate(states, kInitialBitrateBps);
365   states.aimd_rate_control->SetInApplicationLimitedRegion(true);
366 
367   NetworkStateEstimate network_estimate;
368   network_estimate.link_capacity_upper = DataRate::KilobitsPerSec(200);
369   states.aimd_rate_control->SetNetworkStateEstimate(network_estimate);
370   for (int i = 0; i < 10; ++i) {
371     UpdateRateControl(states, BandwidthUsage::kBwNormal, absl::nullopt,
372                       states.simulated_clock->TimeInMilliseconds());
373     states.simulated_clock->AdvanceTimeMilliseconds(100);
374     EXPECT_LT(states.aimd_rate_control->LatestEstimate(),
375               network_estimate.link_capacity_upper * 0.85);
376   }
377   for (int i = 0; i < 50; ++i) {
378     UpdateRateControl(states, BandwidthUsage::kBwNormal, absl::nullopt,
379                       states.simulated_clock->TimeInMilliseconds());
380     states.simulated_clock->AdvanceTimeMilliseconds(100);
381   }
382   EXPECT_EQ(states.aimd_rate_control->LatestEstimate(),
383             network_estimate.link_capacity_upper * 0.85);
384 }
385 
TEST(AimdRateControlTest,EstimateImmediatelyIncreaseToUpperLinkCapacityEstimateIfConfigured)386 TEST(AimdRateControlTest,
387      EstimateImmediatelyIncreaseToUpperLinkCapacityEstimateIfConfigured) {
388   // Even if alr is detected, the delay based estimator is allowed to increase
389   // up to a percentage of upper link capacity.
390   test::ScopedFieldTrials override_field_trials(
391       "WebRTC-Bwe-EstimateBoundedIncrease/"
392       "ratio:0.85,ignore_acked:true,immediate_incr:true/"
393       "WebRTC-DontIncreaseDelayBasedBweInAlr/Enabled/");
394   auto states = CreateAimdRateControlStates(/*send_side=*/true);
395   constexpr int kInitialBitrateBps = 123000;
396   SetEstimate(states, kInitialBitrateBps);
397   states.aimd_rate_control->SetInApplicationLimitedRegion(true);
398 
399   NetworkStateEstimate network_estimate;
400   network_estimate.link_capacity_upper = DataRate::KilobitsPerSec(200);
401   states.aimd_rate_control->SetNetworkStateEstimate(network_estimate);
402   UpdateRateControl(states, BandwidthUsage::kBwNormal, absl::nullopt,
403                     states.simulated_clock->TimeInMilliseconds());
404   EXPECT_EQ(states.aimd_rate_control->LatestEstimate(),
405             network_estimate.link_capacity_upper * 0.85);
406 }
407 
TEST(AimdRateControlTest,EstimateNotLoweredByNetworkEstimate)408 TEST(AimdRateControlTest, EstimateNotLoweredByNetworkEstimate) {
409   // The delay based estimator is allowed to increase up to a percentage of
410   // upper link capacity but does not decrease unless the delay detector
411   // discover an overuse.
412   test::ScopedFieldTrials override_field_trials(
413       "WebRTC-Bwe-EstimateBoundedIncrease/"
414       "ratio:0.85,ignore_acked:true,ignore_decr:true/"
415       "WebRTC-DontIncreaseDelayBasedBweInAlr/Enabled/");
416   auto states = CreateAimdRateControlStates(/*send_side=*/true);
417   constexpr int kInitialBitrateBps = 123000;
418   constexpr int kEstimatedThroughputBps = 30'000;
419   SetEstimate(states, kInitialBitrateBps);
420 
421   NetworkStateEstimate network_estimate;
422   network_estimate.link_capacity_upper = DataRate::KilobitsPerSec(200);
423   states.aimd_rate_control->SetNetworkStateEstimate(network_estimate);
424   for (int i = 0; i < 100; ++i) {
425     UpdateRateControl(states, BandwidthUsage::kBwNormal,
426                       kEstimatedThroughputBps,
427                       states.simulated_clock->TimeInMilliseconds());
428     states.simulated_clock->AdvanceTimeMilliseconds(100);
429   }
430   DataRate estimate_after_increase = states.aimd_rate_control->LatestEstimate();
431   ASSERT_EQ(estimate_after_increase,
432             network_estimate.link_capacity_upper * 0.85);
433 
434   // A lower network estimate does not decrease the estimate immediately,
435   // but the estimate is not allowed to increase.
436   network_estimate.link_capacity_upper = DataRate::KilobitsPerSec(100);
437   network_estimate.link_capacity_lower = DataRate::KilobitsPerSec(80);
438   states.aimd_rate_control->SetNetworkStateEstimate(network_estimate);
439   for (int i = 0; i < 10; ++i) {
440     UpdateRateControl(states, BandwidthUsage::kBwNormal,
441                       kEstimatedThroughputBps,
442                       states.simulated_clock->TimeInMilliseconds());
443     states.simulated_clock->AdvanceTimeMilliseconds(100);
444     EXPECT_EQ(states.aimd_rate_control->LatestEstimate(),
445               estimate_after_increase);
446   }
447 
448   // If the detector detects and overuse, BWE drops to a value relative the
449   // network estimate.
450   UpdateRateControl(states, BandwidthUsage::kBwOverusing,
451                     kEstimatedThroughputBps,
452                     states.simulated_clock->TimeInMilliseconds());
453   EXPECT_LT(states.aimd_rate_control->LatestEstimate(),
454             network_estimate.link_capacity_lower);
455   EXPECT_GT(states.aimd_rate_control->LatestEstimate().bps(),
456             kEstimatedThroughputBps);
457 }
458 
TEST(AimdRateControlTest,EstimateDoesNotIncreaseInAlrIfNetworkEstimateNotSet)459 TEST(AimdRateControlTest, EstimateDoesNotIncreaseInAlrIfNetworkEstimateNotSet) {
460   // When alr is detected, the delay based estimator is not allowed to increase
461   // bwe since there will be no feedback from the network if the new estimate
462   // is correct.
463   test::ScopedFieldTrials override_field_trials(
464       "WebRTC-Bwe-EstimateBoundedIncrease/ratio:0.85,ignore_acked:true/"
465       "WebRTC-DontIncreaseDelayBasedBweInAlr/Enabled/");
466   auto states = CreateAimdRateControlStates(/*send_side=*/true);
467   constexpr int kInitialBitrateBps = 123000;
468   SetEstimate(states, kInitialBitrateBps);
469   states.aimd_rate_control->SetInApplicationLimitedRegion(true);
470   UpdateRateControl(states, BandwidthUsage::kBwNormal, kInitialBitrateBps,
471                     states.simulated_clock->TimeInMilliseconds());
472   ASSERT_EQ(states.aimd_rate_control->LatestEstimate().bps(),
473             kInitialBitrateBps);
474 
475   for (int i = 0; i < 100; ++i) {
476     UpdateRateControl(states, BandwidthUsage::kBwNormal, absl::nullopt,
477                       states.simulated_clock->TimeInMilliseconds());
478     states.simulated_clock->AdvanceTimeMilliseconds(100);
479   }
480   EXPECT_EQ(states.aimd_rate_control->LatestEstimate().bps(),
481             kInitialBitrateBps);
482 }
483 
484 }  // namespace webrtc
485