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 
11 #include "modules/congestion_controller/goog_cc/probe_bitrate_estimator.h"
12 
13 #include <stddef.h>
14 
15 #include "api/transport/network_types.h"
16 #include "test/gtest.h"
17 
18 namespace webrtc {
19 
20 namespace {
21 constexpr int kDefaultMinProbes = 5;
22 constexpr int kDefaultMinBytes = 5000;
23 constexpr float kTargetUtilizationFraction = 0.95f;
24 }  // anonymous namespace
25 
26 class TestProbeBitrateEstimator : public ::testing::Test {
27  public:
TestProbeBitrateEstimator()28   TestProbeBitrateEstimator() : probe_bitrate_estimator_(nullptr) {}
29 
30   // TODO(philipel): Use PacedPacketInfo when ProbeBitrateEstimator is rewritten
31   //                 to use that information.
AddPacketFeedback(int probe_cluster_id,size_t size_bytes,int64_t send_time_ms,int64_t arrival_time_ms,int min_probes=kDefaultMinProbes,int min_bytes=kDefaultMinBytes)32   void AddPacketFeedback(int probe_cluster_id,
33                          size_t size_bytes,
34                          int64_t send_time_ms,
35                          int64_t arrival_time_ms,
36                          int min_probes = kDefaultMinProbes,
37                          int min_bytes = kDefaultMinBytes) {
38     const Timestamp kReferenceTime = Timestamp::Seconds(1000);
39     PacketResult feedback;
40     feedback.sent_packet.send_time =
41         kReferenceTime + TimeDelta::Millis(send_time_ms);
42     feedback.sent_packet.size = DataSize::Bytes(size_bytes);
43     feedback.sent_packet.pacing_info =
44         PacedPacketInfo(probe_cluster_id, min_probes, min_bytes);
45     feedback.receive_time = kReferenceTime + TimeDelta::Millis(arrival_time_ms);
46     measured_data_rate_ =
47         probe_bitrate_estimator_.HandleProbeAndEstimateBitrate(feedback);
48   }
49 
50  protected:
51   absl::optional<DataRate> measured_data_rate_;
52   ProbeBitrateEstimator probe_bitrate_estimator_;
53 };
54 
TEST_F(TestProbeBitrateEstimator,OneCluster)55 TEST_F(TestProbeBitrateEstimator, OneCluster) {
56   AddPacketFeedback(0, 1000, 0, 10);
57   AddPacketFeedback(0, 1000, 10, 20);
58   AddPacketFeedback(0, 1000, 20, 30);
59   AddPacketFeedback(0, 1000, 30, 40);
60 
61   EXPECT_NEAR(measured_data_rate_->bps(), 800000, 10);
62 }
63 
TEST_F(TestProbeBitrateEstimator,OneClusterTooFewProbes)64 TEST_F(TestProbeBitrateEstimator, OneClusterTooFewProbes) {
65   AddPacketFeedback(0, 2000, 0, 10);
66   AddPacketFeedback(0, 2000, 10, 20);
67   AddPacketFeedback(0, 2000, 20, 30);
68 
69   EXPECT_FALSE(measured_data_rate_);
70 }
71 
TEST_F(TestProbeBitrateEstimator,OneClusterTooFewBytes)72 TEST_F(TestProbeBitrateEstimator, OneClusterTooFewBytes) {
73   const int kMinBytes = 6000;
74   AddPacketFeedback(0, 800, 0, 10, kDefaultMinProbes, kMinBytes);
75   AddPacketFeedback(0, 800, 10, 20, kDefaultMinProbes, kMinBytes);
76   AddPacketFeedback(0, 800, 20, 30, kDefaultMinProbes, kMinBytes);
77   AddPacketFeedback(0, 800, 30, 40, kDefaultMinProbes, kMinBytes);
78   AddPacketFeedback(0, 800, 40, 50, kDefaultMinProbes, kMinBytes);
79 
80   EXPECT_FALSE(measured_data_rate_);
81 }
82 
TEST_F(TestProbeBitrateEstimator,SmallCluster)83 TEST_F(TestProbeBitrateEstimator, SmallCluster) {
84   const int kMinBytes = 1000;
85   AddPacketFeedback(0, 150, 0, 10, kDefaultMinProbes, kMinBytes);
86   AddPacketFeedback(0, 150, 10, 20, kDefaultMinProbes, kMinBytes);
87   AddPacketFeedback(0, 150, 20, 30, kDefaultMinProbes, kMinBytes);
88   AddPacketFeedback(0, 150, 30, 40, kDefaultMinProbes, kMinBytes);
89   AddPacketFeedback(0, 150, 40, 50, kDefaultMinProbes, kMinBytes);
90   AddPacketFeedback(0, 150, 50, 60, kDefaultMinProbes, kMinBytes);
91   EXPECT_NEAR(measured_data_rate_->bps(), 120000, 10);
92 }
93 
TEST_F(TestProbeBitrateEstimator,LargeCluster)94 TEST_F(TestProbeBitrateEstimator, LargeCluster) {
95   const int kMinProbes = 30;
96   const int kMinBytes = 312500;
97 
98   int64_t send_time = 0;
99   int64_t receive_time = 5;
100   for (int i = 0; i < 25; ++i) {
101     AddPacketFeedback(0, 12500, send_time, receive_time, kMinProbes, kMinBytes);
102     ++send_time;
103     ++receive_time;
104   }
105   EXPECT_NEAR(measured_data_rate_->bps(), 100000000, 10);
106 }
107 
TEST_F(TestProbeBitrateEstimator,FastReceive)108 TEST_F(TestProbeBitrateEstimator, FastReceive) {
109   AddPacketFeedback(0, 1000, 0, 15);
110   AddPacketFeedback(0, 1000, 10, 30);
111   AddPacketFeedback(0, 1000, 20, 35);
112   AddPacketFeedback(0, 1000, 30, 40);
113 
114   EXPECT_NEAR(measured_data_rate_->bps(), 800000, 10);
115 }
116 
TEST_F(TestProbeBitrateEstimator,TooFastReceive)117 TEST_F(TestProbeBitrateEstimator, TooFastReceive) {
118   AddPacketFeedback(0, 1000, 0, 19);
119   AddPacketFeedback(0, 1000, 10, 22);
120   AddPacketFeedback(0, 1000, 20, 25);
121   AddPacketFeedback(0, 1000, 40, 27);
122 
123   EXPECT_FALSE(measured_data_rate_);
124 }
125 
TEST_F(TestProbeBitrateEstimator,SlowReceive)126 TEST_F(TestProbeBitrateEstimator, SlowReceive) {
127   AddPacketFeedback(0, 1000, 0, 10);
128   AddPacketFeedback(0, 1000, 10, 40);
129   AddPacketFeedback(0, 1000, 20, 70);
130   AddPacketFeedback(0, 1000, 30, 85);
131   // Expected send rate = 800 kbps, expected receive rate = 320 kbps.
132 
133   EXPECT_NEAR(measured_data_rate_->bps(), kTargetUtilizationFraction * 320000,
134               10);
135 }
136 
TEST_F(TestProbeBitrateEstimator,BurstReceive)137 TEST_F(TestProbeBitrateEstimator, BurstReceive) {
138   AddPacketFeedback(0, 1000, 0, 50);
139   AddPacketFeedback(0, 1000, 10, 50);
140   AddPacketFeedback(0, 1000, 20, 50);
141   AddPacketFeedback(0, 1000, 40, 50);
142 
143   EXPECT_FALSE(measured_data_rate_);
144 }
145 
TEST_F(TestProbeBitrateEstimator,MultipleClusters)146 TEST_F(TestProbeBitrateEstimator, MultipleClusters) {
147   AddPacketFeedback(0, 1000, 0, 10);
148   AddPacketFeedback(0, 1000, 10, 20);
149   AddPacketFeedback(0, 1000, 20, 30);
150   AddPacketFeedback(0, 1000, 40, 60);
151   // Expected send rate = 600 kbps, expected receive rate = 480 kbps.
152   EXPECT_NEAR(measured_data_rate_->bps(), kTargetUtilizationFraction * 480000,
153               10);
154 
155   AddPacketFeedback(0, 1000, 50, 60);
156   // Expected send rate = 640 kbps, expected receive rate = 640 kbps.
157   EXPECT_NEAR(measured_data_rate_->bps(), 640000, 10);
158 
159   AddPacketFeedback(1, 1000, 60, 70);
160   AddPacketFeedback(1, 1000, 65, 77);
161   AddPacketFeedback(1, 1000, 70, 84);
162   AddPacketFeedback(1, 1000, 75, 90);
163   // Expected send rate = 1600 kbps, expected receive rate = 1200 kbps.
164 
165   EXPECT_NEAR(measured_data_rate_->bps(), kTargetUtilizationFraction * 1200000,
166               10);
167 }
168 
TEST_F(TestProbeBitrateEstimator,IgnoreOldClusters)169 TEST_F(TestProbeBitrateEstimator, IgnoreOldClusters) {
170   AddPacketFeedback(0, 1000, 0, 10);
171   AddPacketFeedback(0, 1000, 10, 20);
172   AddPacketFeedback(0, 1000, 20, 30);
173 
174   AddPacketFeedback(1, 1000, 60, 70);
175   AddPacketFeedback(1, 1000, 65, 77);
176   AddPacketFeedback(1, 1000, 70, 84);
177   AddPacketFeedback(1, 1000, 75, 90);
178   // Expected send rate = 1600 kbps, expected receive rate = 1200 kbps.
179 
180   EXPECT_NEAR(measured_data_rate_->bps(), kTargetUtilizationFraction * 1200000,
181               10);
182 
183   // Coming in 6s later
184   AddPacketFeedback(0, 1000, 40 + 6000, 60 + 6000);
185 
186   EXPECT_FALSE(measured_data_rate_);
187 }
188 
TEST_F(TestProbeBitrateEstimator,IgnoreSizeLastSendPacket)189 TEST_F(TestProbeBitrateEstimator, IgnoreSizeLastSendPacket) {
190   AddPacketFeedback(0, 1000, 0, 10);
191   AddPacketFeedback(0, 1000, 10, 20);
192   AddPacketFeedback(0, 1000, 20, 30);
193   AddPacketFeedback(0, 1000, 30, 40);
194   AddPacketFeedback(0, 1500, 40, 50);
195   // Expected send rate = 800 kbps, expected receive rate = 900 kbps.
196 
197   EXPECT_NEAR(measured_data_rate_->bps(), 800000, 10);
198 }
199 
TEST_F(TestProbeBitrateEstimator,IgnoreSizeFirstReceivePacket)200 TEST_F(TestProbeBitrateEstimator, IgnoreSizeFirstReceivePacket) {
201   AddPacketFeedback(0, 1500, 0, 10);
202   AddPacketFeedback(0, 1000, 10, 20);
203   AddPacketFeedback(0, 1000, 20, 30);
204   AddPacketFeedback(0, 1000, 30, 40);
205   // Expected send rate = 933 kbps, expected receive rate = 800 kbps.
206 
207   EXPECT_NEAR(measured_data_rate_->bps(), kTargetUtilizationFraction * 800000,
208               10);
209 }
210 
TEST_F(TestProbeBitrateEstimator,NoLastEstimatedBitrateBps)211 TEST_F(TestProbeBitrateEstimator, NoLastEstimatedBitrateBps) {
212   EXPECT_FALSE(probe_bitrate_estimator_.FetchAndResetLastEstimatedBitrate());
213 }
214 
TEST_F(TestProbeBitrateEstimator,FetchLastEstimatedBitrateBps)215 TEST_F(TestProbeBitrateEstimator, FetchLastEstimatedBitrateBps) {
216   AddPacketFeedback(0, 1000, 0, 10);
217   AddPacketFeedback(0, 1000, 10, 20);
218   AddPacketFeedback(0, 1000, 20, 30);
219   AddPacketFeedback(0, 1000, 30, 40);
220 
221   auto estimated_bitrate =
222       probe_bitrate_estimator_.FetchAndResetLastEstimatedBitrate();
223   EXPECT_TRUE(estimated_bitrate);
224   EXPECT_NEAR(estimated_bitrate->bps(), 800000, 10);
225   EXPECT_FALSE(probe_bitrate_estimator_.FetchAndResetLastEstimatedBitrate());
226 }
227 
228 }  // namespace webrtc
229