blob: 380d345e6f1ab41a1de51893c5d1665f1e2e0ff0 [file] [log] [blame]
stefan@webrtc.org82462aa2014-10-23 11:57:05 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/pacing/bitrate_prober.h"
12#include "test/gtest.h"
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000013
14namespace webrtc {
15
16TEST(BitrateProberTest, VerifyStatesAndTimeBetweenProbes) {
Jonas Olsson24923e82019-03-27 14:19:04 +010017 const FieldTrialBasedConfig config;
18 BitrateProber prober(config);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000019 EXPECT_FALSE(prober.IsProbing());
Jonas Olsson24923e82019-03-27 14:19:04 +010020
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000021 int64_t now_ms = 0;
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +000022 EXPECT_EQ(-1, prober.TimeUntilNextProbe(now_ms));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000023
sergeyu6dbbd892017-01-17 15:07:59 -080024 const int kTestBitrate1 = 900000;
25 const int kTestBitrate2 = 1800000;
26 const int kClusterSize = 5;
27 const int kProbeSize = 1000;
28 const int kMinProbeDurationMs = 15;
29
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -080030 prober.CreateProbeCluster(kTestBitrate1, now_ms, 0);
31 prober.CreateProbeCluster(kTestBitrate2, now_ms, 1);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000032 EXPECT_FALSE(prober.IsProbing());
33
sergeyu6dbbd892017-01-17 15:07:59 -080034 prober.OnIncomingPacket(kProbeSize);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000035 EXPECT_TRUE(prober.IsProbing());
philipelc7bf32a2017-02-17 03:59:43 -080036 EXPECT_EQ(0, prober.CurrentCluster().probe_cluster_id);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000037
Peter Boström0453ef82016-02-16 16:23:08 +010038 // First packet should probe as soon as possible.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000039 EXPECT_EQ(0, prober.TimeUntilNextProbe(now_ms));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000040
sergeyu6dbbd892017-01-17 15:07:59 -080041 for (int i = 0; i < kClusterSize; ++i) {
42 now_ms += prober.TimeUntilNextProbe(now_ms);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000043 EXPECT_EQ(0, prober.TimeUntilNextProbe(now_ms));
philipelc7bf32a2017-02-17 03:59:43 -080044 EXPECT_EQ(0, prober.CurrentCluster().probe_cluster_id);
sergeyu6dbbd892017-01-17 15:07:59 -080045 prober.ProbeSent(now_ms, kProbeSize);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000046 }
sergeyu6dbbd892017-01-17 15:07:59 -080047
48 EXPECT_GE(now_ms, kMinProbeDurationMs);
49 // Verify that the actual bitrate is withing 10% of the target.
50 double bitrate = kProbeSize * (kClusterSize - 1) * 8 * 1000.0 / now_ms;
51 EXPECT_GT(bitrate, kTestBitrate1 * 0.9);
Yves Gerey665174f2018-06-19 15:03:05 +020052 EXPECT_LT(bitrate, kTestBitrate1 * 1.1);
sergeyu6dbbd892017-01-17 15:07:59 -080053
54 now_ms += prober.TimeUntilNextProbe(now_ms);
55 int64_t probe2_started = now_ms;
56
57 for (int i = 0; i < kClusterSize; ++i) {
58 now_ms += prober.TimeUntilNextProbe(now_ms);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000059 EXPECT_EQ(0, prober.TimeUntilNextProbe(now_ms));
philipelc7bf32a2017-02-17 03:59:43 -080060 EXPECT_EQ(1, prober.CurrentCluster().probe_cluster_id);
sergeyu6dbbd892017-01-17 15:07:59 -080061 prober.ProbeSent(now_ms, kProbeSize);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000062 }
63
sergeyu6dbbd892017-01-17 15:07:59 -080064 // Verify that the actual bitrate is withing 10% of the target.
65 int duration = now_ms - probe2_started;
66 EXPECT_GE(duration, kMinProbeDurationMs);
67 bitrate = kProbeSize * (kClusterSize - 1) * 8 * 1000.0 / duration;
68 EXPECT_GT(bitrate, kTestBitrate2 * 0.9);
Yves Gerey665174f2018-06-19 15:03:05 +020069 EXPECT_LT(bitrate, kTestBitrate2 * 1.1);
sergeyu6dbbd892017-01-17 15:07:59 -080070
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +000071 EXPECT_EQ(-1, prober.TimeUntilNextProbe(now_ms));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000072 EXPECT_FALSE(prober.IsProbing());
73}
Peter Boström0453ef82016-02-16 16:23:08 +010074
75TEST(BitrateProberTest, DoesntProbeWithoutRecentPackets) {
Jonas Olsson24923e82019-03-27 14:19:04 +010076 const FieldTrialBasedConfig config;
77 BitrateProber prober(config);
78
Peter Boström0453ef82016-02-16 16:23:08 +010079 int64_t now_ms = 0;
80 EXPECT_EQ(-1, prober.TimeUntilNextProbe(now_ms));
81
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -080082 prober.CreateProbeCluster(900000, now_ms, 0);
Peter Boström0453ef82016-02-16 16:23:08 +010083 EXPECT_FALSE(prober.IsProbing());
84
philipel4a1ec1e2016-08-15 11:51:06 -070085 prober.OnIncomingPacket(1000);
Peter Boström0453ef82016-02-16 16:23:08 +010086 EXPECT_TRUE(prober.IsProbing());
87 EXPECT_EQ(0, prober.TimeUntilNextProbe(now_ms));
isheriffcc5903e2016-10-04 08:29:38 -070088 prober.ProbeSent(now_ms, 1000);
Peter Boström0453ef82016-02-16 16:23:08 +010089 // Let time pass, no large enough packets put into prober.
90 now_ms += 6000;
91 EXPECT_EQ(-1, prober.TimeUntilNextProbe(now_ms));
srteadf4c162017-12-07 11:27:03 +010092 // Check that legacy behaviour where prober is reset in TimeUntilNextProbe is
93 // no longer there. Probes are no longer retried if they are timed out.
philipel4a1ec1e2016-08-15 11:51:06 -070094 prober.OnIncomingPacket(1000);
srteadf4c162017-12-07 11:27:03 +010095 EXPECT_EQ(-1, prober.TimeUntilNextProbe(now_ms));
Peter Boström0453ef82016-02-16 16:23:08 +010096}
97
98TEST(BitrateProberTest, DoesntInitializeProbingForSmallPackets) {
Jonas Olsson24923e82019-03-27 14:19:04 +010099 const FieldTrialBasedConfig config;
100 BitrateProber prober(config);
101
Peter Boström0453ef82016-02-16 16:23:08 +0100102 prober.SetEnabled(true);
103 EXPECT_FALSE(prober.IsProbing());
104
philipel4a1ec1e2016-08-15 11:51:06 -0700105 prober.OnIncomingPacket(100);
Peter Boström0453ef82016-02-16 16:23:08 +0100106 EXPECT_FALSE(prober.IsProbing());
107}
108
isheriffcc5903e2016-10-04 08:29:38 -0700109TEST(BitrateProberTest, VerifyProbeSizeOnHighBitrate) {
Jonas Olsson24923e82019-03-27 14:19:04 +0100110 const FieldTrialBasedConfig config;
111 BitrateProber prober(config);
112
isheriffcc5903e2016-10-04 08:29:38 -0700113 constexpr unsigned kHighBitrateBps = 10000000; // 10 Mbps
114
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -0800115 prober.CreateProbeCluster(kHighBitrateBps, 0, /*cluster_id=*/0);
isheriffcc5903e2016-10-04 08:29:38 -0700116 // Probe size should ensure a minimum of 1 ms interval.
117 EXPECT_GT(prober.RecommendedMinProbeSize(), kHighBitrateBps / 8000);
118}
119
philipelfd58b612017-01-04 07:05:25 -0800120TEST(BitrateProberTest, MinumumNumberOfProbingPackets) {
Jonas Olsson24923e82019-03-27 14:19:04 +0100121 const FieldTrialBasedConfig config;
122 BitrateProber prober(config);
philipelfd58b612017-01-04 07:05:25 -0800123 // Even when probing at a low bitrate we expect a minimum number
124 // of packets to be sent.
125 constexpr int kBitrateBps = 100000; // 100 kbps
126 constexpr int kPacketSizeBytes = 1000;
127
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -0800128 prober.CreateProbeCluster(kBitrateBps, 0, 0);
philipelfd58b612017-01-04 07:05:25 -0800129 prober.OnIncomingPacket(kPacketSizeBytes);
130 for (int i = 0; i < 5; ++i) {
131 EXPECT_TRUE(prober.IsProbing());
132 prober.ProbeSent(0, kPacketSizeBytes);
133 }
134
135 EXPECT_FALSE(prober.IsProbing());
136}
137
138TEST(BitrateProberTest, ScaleBytesUsedForProbing) {
Jonas Olsson24923e82019-03-27 14:19:04 +0100139 const FieldTrialBasedConfig config;
140 BitrateProber prober(config);
philipelfd58b612017-01-04 07:05:25 -0800141 constexpr int kBitrateBps = 10000000; // 10 Mbps
142 constexpr int kPacketSizeBytes = 1000;
143 constexpr int kExpectedBytesSent = kBitrateBps * 15 / 8000;
144
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -0800145 prober.CreateProbeCluster(kBitrateBps, 0, /*cluster_id=*/0);
philipelfd58b612017-01-04 07:05:25 -0800146 prober.OnIncomingPacket(kPacketSizeBytes);
147 int bytes_sent = 0;
148 while (bytes_sent < kExpectedBytesSent) {
stefanf00497c2017-01-27 02:27:33 -0800149 ASSERT_TRUE(prober.IsProbing());
philipelfd58b612017-01-04 07:05:25 -0800150 prober.ProbeSent(0, kPacketSizeBytes);
151 bytes_sent += kPacketSizeBytes;
152 }
153
154 EXPECT_FALSE(prober.IsProbing());
155}
156
Johannes Kron85846672018-11-09 12:39:38 +0100157TEST(BitrateProberTest, HighBitrateProbing) {
Jonas Olsson24923e82019-03-27 14:19:04 +0100158 const FieldTrialBasedConfig config;
159 BitrateProber prober(config);
Johannes Kron85846672018-11-09 12:39:38 +0100160 constexpr int kBitrateBps = 1000000000; // 1 Gbps.
161 constexpr int kPacketSizeBytes = 1000;
162 constexpr int kExpectedBytesSent = (kBitrateBps / 8000) * 15;
163
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -0800164 prober.CreateProbeCluster(kBitrateBps, 0, 0);
Johannes Kron85846672018-11-09 12:39:38 +0100165 prober.OnIncomingPacket(kPacketSizeBytes);
166 int bytes_sent = 0;
167 while (bytes_sent < kExpectedBytesSent) {
168 ASSERT_TRUE(prober.IsProbing());
169 prober.ProbeSent(0, kPacketSizeBytes);
170 bytes_sent += kPacketSizeBytes;
171 }
172
173 EXPECT_FALSE(prober.IsProbing());
174}
175
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100176TEST(BitrateProberTest, ProbeClusterTimeout) {
Jonas Olsson24923e82019-03-27 14:19:04 +0100177 const FieldTrialBasedConfig config;
178 BitrateProber prober(config);
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100179 constexpr int kBitrateBps = 300000; // 300 kbps
180 constexpr int kSmallPacketSize = 20;
181 // Expecting two probe clusters of 5 packets each.
182 constexpr int kExpectedBytesSent = 20 * 2 * 5;
183 constexpr int64_t kTimeoutMs = 5000;
184
185 int64_t now_ms = 0;
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -0800186 prober.CreateProbeCluster(kBitrateBps, now_ms, /*cluster_id=*/0);
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100187 prober.OnIncomingPacket(kSmallPacketSize);
188 EXPECT_FALSE(prober.IsProbing());
189 now_ms += kTimeoutMs;
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -0800190 prober.CreateProbeCluster(kBitrateBps / 10, now_ms, /*cluster_id=*/1);
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100191 prober.OnIncomingPacket(kSmallPacketSize);
192 EXPECT_FALSE(prober.IsProbing());
193 now_ms += 1;
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -0800194 prober.CreateProbeCluster(kBitrateBps / 10, now_ms, /*cluster_id=*/2);
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100195 prober.OnIncomingPacket(kSmallPacketSize);
196 EXPECT_TRUE(prober.IsProbing());
197 int bytes_sent = 0;
198 while (bytes_sent < kExpectedBytesSent) {
199 ASSERT_TRUE(prober.IsProbing());
200 prober.ProbeSent(0, kSmallPacketSize);
201 bytes_sent += kSmallPacketSize;
202 }
203
204 EXPECT_FALSE(prober.IsProbing());
205}
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000206} // namespace webrtc