1 /*
2 * Copyright (c) 2014 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
11 #include "modules/congestion_controller/goog_cc/send_side_bandwidth_estimation.h"
12
13 #include "api/rtc_event_log/rtc_event.h"
14 #include "logging/rtc_event_log/events/rtc_event_bwe_update_loss_based.h"
15 #include "logging/rtc_event_log/mock/mock_rtc_event_log.h"
16 #include "test/gmock.h"
17 #include "test/gtest.h"
18
19 namespace webrtc {
20
21 MATCHER(LossBasedBweUpdateWithBitrateOnly, "") {
22 if (arg->GetType() != RtcEvent::Type::BweUpdateLossBased) {
23 return false;
24 }
25 auto bwe_event = static_cast<RtcEventBweUpdateLossBased*>(arg);
26 return bwe_event->bitrate_bps() > 0 && bwe_event->fraction_loss() == 0;
27 }
28
29 MATCHER(LossBasedBweUpdateWithBitrateAndLossFraction, "") {
30 if (arg->GetType() != RtcEvent::Type::BweUpdateLossBased) {
31 return false;
32 }
33 auto bwe_event = static_cast<RtcEventBweUpdateLossBased*>(arg);
34 return bwe_event->bitrate_bps() > 0 && bwe_event->fraction_loss() > 0;
35 }
36
TestProbing(bool use_delay_based)37 void TestProbing(bool use_delay_based) {
38 ::testing::NiceMock<MockRtcEventLog> event_log;
39 SendSideBandwidthEstimation bwe(&event_log);
40 int64_t now_ms = 0;
41 bwe.SetMinMaxBitrate(DataRate::BitsPerSec(100000),
42 DataRate::BitsPerSec(1500000));
43 bwe.SetSendBitrate(DataRate::BitsPerSec(200000), Timestamp::Millis(now_ms));
44
45 const int kRembBps = 1000000;
46 const int kSecondRembBps = kRembBps + 500000;
47
48 bwe.UpdatePacketsLost(/*packets_lost=*/0, /*number_of_packets=*/1,
49 Timestamp::Millis(now_ms));
50 bwe.UpdateRtt(TimeDelta::Millis(50), Timestamp::Millis(now_ms));
51
52 // Initial REMB applies immediately.
53 if (use_delay_based) {
54 bwe.UpdateDelayBasedEstimate(Timestamp::Millis(now_ms),
55 DataRate::BitsPerSec(kRembBps));
56 } else {
57 bwe.UpdateReceiverEstimate(Timestamp::Millis(now_ms),
58 DataRate::BitsPerSec(kRembBps));
59 }
60 bwe.UpdateEstimate(Timestamp::Millis(now_ms));
61 EXPECT_EQ(kRembBps, bwe.target_rate().bps());
62
63 // Second REMB doesn't apply immediately.
64 now_ms += 2001;
65 if (use_delay_based) {
66 bwe.UpdateDelayBasedEstimate(Timestamp::Millis(now_ms),
67 DataRate::BitsPerSec(kSecondRembBps));
68 } else {
69 bwe.UpdateReceiverEstimate(Timestamp::Millis(now_ms),
70 DataRate::BitsPerSec(kSecondRembBps));
71 }
72 bwe.UpdateEstimate(Timestamp::Millis(now_ms));
73 EXPECT_EQ(kRembBps, bwe.target_rate().bps());
74 }
75
TEST(SendSideBweTest,InitialRembWithProbing)76 TEST(SendSideBweTest, InitialRembWithProbing) {
77 TestProbing(false);
78 }
79
TEST(SendSideBweTest,InitialDelayBasedBweWithProbing)80 TEST(SendSideBweTest, InitialDelayBasedBweWithProbing) {
81 TestProbing(true);
82 }
83
TEST(SendSideBweTest,DoesntReapplyBitrateDecreaseWithoutFollowingRemb)84 TEST(SendSideBweTest, DoesntReapplyBitrateDecreaseWithoutFollowingRemb) {
85 MockRtcEventLog event_log;
86 EXPECT_CALL(event_log, LogProxy(LossBasedBweUpdateWithBitrateOnly()))
87 .Times(1);
88 EXPECT_CALL(event_log,
89 LogProxy(LossBasedBweUpdateWithBitrateAndLossFraction()))
90 .Times(1);
91 SendSideBandwidthEstimation bwe(&event_log);
92 static const int kMinBitrateBps = 100000;
93 static const int kInitialBitrateBps = 1000000;
94 int64_t now_ms = 1000;
95 bwe.SetMinMaxBitrate(DataRate::BitsPerSec(kMinBitrateBps),
96 DataRate::BitsPerSec(1500000));
97 bwe.SetSendBitrate(DataRate::BitsPerSec(kInitialBitrateBps),
98 Timestamp::Millis(now_ms));
99
100 static const uint8_t kFractionLoss = 128;
101 static const int64_t kRttMs = 50;
102 now_ms += 10000;
103
104 EXPECT_EQ(kInitialBitrateBps, bwe.target_rate().bps());
105 EXPECT_EQ(0, bwe.fraction_loss());
106 EXPECT_EQ(0, bwe.round_trip_time().ms());
107
108 // Signal heavy loss to go down in bitrate.
109 bwe.UpdatePacketsLost(/*packets_lost=*/50, /*number_of_packets=*/100,
110 Timestamp::Millis(now_ms));
111 bwe.UpdateRtt(TimeDelta::Millis(kRttMs), Timestamp::Millis(now_ms));
112
113 // Trigger an update 2 seconds later to not be rate limited.
114 now_ms += 1000;
115 bwe.UpdateEstimate(Timestamp::Millis(now_ms));
116 EXPECT_LT(bwe.target_rate().bps(), kInitialBitrateBps);
117 // Verify that the obtained bitrate isn't hitting the min bitrate, or this
118 // test doesn't make sense. If this ever happens, update the thresholds or
119 // loss rates so that it doesn't hit min bitrate after one bitrate update.
120 EXPECT_GT(bwe.target_rate().bps(), kMinBitrateBps);
121 EXPECT_EQ(kFractionLoss, bwe.fraction_loss());
122 EXPECT_EQ(kRttMs, bwe.round_trip_time().ms());
123
124 // Triggering an update shouldn't apply further downgrade nor upgrade since
125 // there's no intermediate receiver block received indicating whether this is
126 // currently good or not.
127 int last_bitrate_bps = bwe.target_rate().bps();
128 // Trigger an update 2 seconds later to not be rate limited (but it still
129 // shouldn't update).
130 now_ms += 1000;
131 bwe.UpdateEstimate(Timestamp::Millis(now_ms));
132
133 EXPECT_EQ(last_bitrate_bps, bwe.target_rate().bps());
134 // The old loss rate should still be applied though.
135 EXPECT_EQ(kFractionLoss, bwe.fraction_loss());
136 EXPECT_EQ(kRttMs, bwe.round_trip_time().ms());
137 }
138
TEST(SendSideBweTest,SettingSendBitrateOverridesDelayBasedEstimate)139 TEST(SendSideBweTest, SettingSendBitrateOverridesDelayBasedEstimate) {
140 ::testing::NiceMock<MockRtcEventLog> event_log;
141 SendSideBandwidthEstimation bwe(&event_log);
142 static const int kMinBitrateBps = 10000;
143 static const int kMaxBitrateBps = 10000000;
144 static const int kInitialBitrateBps = 300000;
145 static const int kDelayBasedBitrateBps = 350000;
146 static const int kForcedHighBitrate = 2500000;
147
148 int64_t now_ms = 0;
149
150 bwe.SetMinMaxBitrate(DataRate::BitsPerSec(kMinBitrateBps),
151 DataRate::BitsPerSec(kMaxBitrateBps));
152 bwe.SetSendBitrate(DataRate::BitsPerSec(kInitialBitrateBps),
153 Timestamp::Millis(now_ms));
154
155 bwe.UpdateDelayBasedEstimate(Timestamp::Millis(now_ms),
156 DataRate::BitsPerSec(kDelayBasedBitrateBps));
157 bwe.UpdateEstimate(Timestamp::Millis(now_ms));
158 EXPECT_GE(bwe.target_rate().bps(), kInitialBitrateBps);
159 EXPECT_LE(bwe.target_rate().bps(), kDelayBasedBitrateBps);
160
161 bwe.SetSendBitrate(DataRate::BitsPerSec(kForcedHighBitrate),
162 Timestamp::Millis(now_ms));
163 EXPECT_EQ(bwe.target_rate().bps(), kForcedHighBitrate);
164 }
165
166 } // namespace webrtc
167