blob: c907cdda29bac480647e84b3cf7447320aca0751 [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"
Jonas Olssona4d87372019-07-05 19:08:33 +020012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "test/gtest.h"
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000014
15namespace webrtc {
16
17TEST(BitrateProberTest, VerifyStatesAndTimeBetweenProbes) {
Jonas Olsson24923e82019-03-27 14:19:04 +010018 const FieldTrialBasedConfig config;
19 BitrateProber prober(config);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000020 EXPECT_FALSE(prober.IsProbing());
Jonas Olsson24923e82019-03-27 14:19:04 +010021
Erik Språngbb56d4b2019-11-04 13:53:09 +000022 int64_t now_ms = 0;
23 EXPECT_EQ(-1, prober.TimeUntilNextProbe(now_ms));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000024
Erik Språngbb56d4b2019-11-04 13:53:09 +000025 const int kTestBitrate1 = 900000;
26 const int kTestBitrate2 = 1800000;
sergeyu6dbbd892017-01-17 15:07:59 -080027 const int kClusterSize = 5;
28 const int kProbeSize = 1000;
Erik Språngbb56d4b2019-11-04 13:53:09 +000029 const int kMinProbeDurationMs = 15;
sergeyu6dbbd892017-01-17 15:07:59 -080030
Erik Språngbb56d4b2019-11-04 13:53:09 +000031 prober.CreateProbeCluster(kTestBitrate1, now_ms, 0);
32 prober.CreateProbeCluster(kTestBitrate2, now_ms, 1);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000033 EXPECT_FALSE(prober.IsProbing());
34
sergeyu6dbbd892017-01-17 15:07:59 -080035 prober.OnIncomingPacket(kProbeSize);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000036 EXPECT_TRUE(prober.IsProbing());
philipelc7bf32a2017-02-17 03:59:43 -080037 EXPECT_EQ(0, prober.CurrentCluster().probe_cluster_id);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000038
Peter Boström0453ef82016-02-16 16:23:08 +010039 // First packet should probe as soon as possible.
Erik Språngbb56d4b2019-11-04 13:53:09 +000040 EXPECT_EQ(0, prober.TimeUntilNextProbe(now_ms));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000041
sergeyu6dbbd892017-01-17 15:07:59 -080042 for (int i = 0; i < kClusterSize; ++i) {
Erik Språngbb56d4b2019-11-04 13:53:09 +000043 now_ms += prober.TimeUntilNextProbe(now_ms);
44 EXPECT_EQ(0, prober.TimeUntilNextProbe(now_ms));
philipelc7bf32a2017-02-17 03:59:43 -080045 EXPECT_EQ(0, prober.CurrentCluster().probe_cluster_id);
Erik Språngbb56d4b2019-11-04 13:53:09 +000046 prober.ProbeSent(now_ms, kProbeSize);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000047 }
sergeyu6dbbd892017-01-17 15:07:59 -080048
Erik Språngbb56d4b2019-11-04 13:53:09 +000049 EXPECT_GE(now_ms, kMinProbeDurationMs);
sergeyu6dbbd892017-01-17 15:07:59 -080050 // Verify that the actual bitrate is withing 10% of the target.
Erik Språngbb56d4b2019-11-04 13:53:09 +000051 double bitrate = kProbeSize * (kClusterSize - 1) * 8 * 1000.0 / now_ms;
sergeyu6dbbd892017-01-17 15:07:59 -080052 EXPECT_GT(bitrate, kTestBitrate1 * 0.9);
Yves Gerey665174f2018-06-19 15:03:05 +020053 EXPECT_LT(bitrate, kTestBitrate1 * 1.1);
sergeyu6dbbd892017-01-17 15:07:59 -080054
Erik Språngbb56d4b2019-11-04 13:53:09 +000055 now_ms += prober.TimeUntilNextProbe(now_ms);
56 int64_t probe2_started = now_ms;
sergeyu6dbbd892017-01-17 15:07:59 -080057
58 for (int i = 0; i < kClusterSize; ++i) {
Erik Språngbb56d4b2019-11-04 13:53:09 +000059 now_ms += prober.TimeUntilNextProbe(now_ms);
60 EXPECT_EQ(0, prober.TimeUntilNextProbe(now_ms));
philipelc7bf32a2017-02-17 03:59:43 -080061 EXPECT_EQ(1, prober.CurrentCluster().probe_cluster_id);
Erik Språngbb56d4b2019-11-04 13:53:09 +000062 prober.ProbeSent(now_ms, kProbeSize);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000063 }
64
sergeyu6dbbd892017-01-17 15:07:59 -080065 // Verify that the actual bitrate is withing 10% of the target.
Erik Språngbb56d4b2019-11-04 13:53:09 +000066 int duration = now_ms - probe2_started;
67 EXPECT_GE(duration, kMinProbeDurationMs);
68 bitrate = kProbeSize * (kClusterSize - 1) * 8 * 1000.0 / duration;
sergeyu6dbbd892017-01-17 15:07:59 -080069 EXPECT_GT(bitrate, kTestBitrate2 * 0.9);
Yves Gerey665174f2018-06-19 15:03:05 +020070 EXPECT_LT(bitrate, kTestBitrate2 * 1.1);
sergeyu6dbbd892017-01-17 15:07:59 -080071
Erik Språngbb56d4b2019-11-04 13:53:09 +000072 EXPECT_EQ(-1, prober.TimeUntilNextProbe(now_ms));
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000073 EXPECT_FALSE(prober.IsProbing());
74}
Peter Boström0453ef82016-02-16 16:23:08 +010075
76TEST(BitrateProberTest, DoesntProbeWithoutRecentPackets) {
Jonas Olsson24923e82019-03-27 14:19:04 +010077 const FieldTrialBasedConfig config;
78 BitrateProber prober(config);
79
Erik Språngbb56d4b2019-11-04 13:53:09 +000080 int64_t now_ms = 0;
81 EXPECT_EQ(-1, prober.TimeUntilNextProbe(now_ms));
Peter Boström0453ef82016-02-16 16:23:08 +010082
Erik Språngbb56d4b2019-11-04 13:53:09 +000083 prober.CreateProbeCluster(900000, now_ms, 0);
Peter Boström0453ef82016-02-16 16:23:08 +010084 EXPECT_FALSE(prober.IsProbing());
85
philipel4a1ec1e2016-08-15 11:51:06 -070086 prober.OnIncomingPacket(1000);
Peter Boström0453ef82016-02-16 16:23:08 +010087 EXPECT_TRUE(prober.IsProbing());
Erik Språngbb56d4b2019-11-04 13:53:09 +000088 EXPECT_EQ(0, prober.TimeUntilNextProbe(now_ms));
89 prober.ProbeSent(now_ms, 1000);
Peter Boström0453ef82016-02-16 16:23:08 +010090 // Let time pass, no large enough packets put into prober.
Erik Språngbb56d4b2019-11-04 13:53:09 +000091 now_ms += 6000;
92 EXPECT_EQ(-1, prober.TimeUntilNextProbe(now_ms));
srteadf4c162017-12-07 11:27:03 +010093 // Check that legacy behaviour where prober is reset in TimeUntilNextProbe is
94 // no longer there. Probes are no longer retried if they are timed out.
philipel4a1ec1e2016-08-15 11:51:06 -070095 prober.OnIncomingPacket(1000);
Erik Språngbb56d4b2019-11-04 13:53:09 +000096 EXPECT_EQ(-1, prober.TimeUntilNextProbe(now_ms));
Peter Boström0453ef82016-02-16 16:23:08 +010097}
98
99TEST(BitrateProberTest, DoesntInitializeProbingForSmallPackets) {
Jonas Olsson24923e82019-03-27 14:19:04 +0100100 const FieldTrialBasedConfig config;
101 BitrateProber prober(config);
102
Peter Boström0453ef82016-02-16 16:23:08 +0100103 prober.SetEnabled(true);
104 EXPECT_FALSE(prober.IsProbing());
105
philipel4a1ec1e2016-08-15 11:51:06 -0700106 prober.OnIncomingPacket(100);
Peter Boström0453ef82016-02-16 16:23:08 +0100107 EXPECT_FALSE(prober.IsProbing());
108}
109
isheriffcc5903e2016-10-04 08:29:38 -0700110TEST(BitrateProberTest, VerifyProbeSizeOnHighBitrate) {
Jonas Olsson24923e82019-03-27 14:19:04 +0100111 const FieldTrialBasedConfig config;
112 BitrateProber prober(config);
113
Erik Språngbb56d4b2019-11-04 13:53:09 +0000114 constexpr unsigned kHighBitrateBps = 10000000; // 10 Mbps
isheriffcc5903e2016-10-04 08:29:38 -0700115
Erik Språngbb56d4b2019-11-04 13:53:09 +0000116 prober.CreateProbeCluster(kHighBitrateBps, 0, /*cluster_id=*/0);
isheriffcc5903e2016-10-04 08:29:38 -0700117 // Probe size should ensure a minimum of 1 ms interval.
Erik Språngbb56d4b2019-11-04 13:53:09 +0000118 EXPECT_GT(prober.RecommendedMinProbeSize(), kHighBitrateBps / 8000);
isheriffcc5903e2016-10-04 08:29:38 -0700119}
120
philipelfd58b612017-01-04 07:05:25 -0800121TEST(BitrateProberTest, MinumumNumberOfProbingPackets) {
Jonas Olsson24923e82019-03-27 14:19:04 +0100122 const FieldTrialBasedConfig config;
123 BitrateProber prober(config);
philipelfd58b612017-01-04 07:05:25 -0800124 // Even when probing at a low bitrate we expect a minimum number
125 // of packets to be sent.
Erik Språngbb56d4b2019-11-04 13:53:09 +0000126 constexpr int kBitrateBps = 100000; // 100 kbps
127 constexpr int kPacketSizeBytes = 1000;
philipelfd58b612017-01-04 07:05:25 -0800128
Erik Språngbb56d4b2019-11-04 13:53:09 +0000129 prober.CreateProbeCluster(kBitrateBps, 0, 0);
philipelfd58b612017-01-04 07:05:25 -0800130 prober.OnIncomingPacket(kPacketSizeBytes);
131 for (int i = 0; i < 5; ++i) {
132 EXPECT_TRUE(prober.IsProbing());
Erik Språngbb56d4b2019-11-04 13:53:09 +0000133 prober.ProbeSent(0, kPacketSizeBytes);
philipelfd58b612017-01-04 07:05:25 -0800134 }
135
136 EXPECT_FALSE(prober.IsProbing());
137}
138
139TEST(BitrateProberTest, ScaleBytesUsedForProbing) {
Jonas Olsson24923e82019-03-27 14:19:04 +0100140 const FieldTrialBasedConfig config;
141 BitrateProber prober(config);
Erik Språngbb56d4b2019-11-04 13:53:09 +0000142 constexpr int kBitrateBps = 10000000; // 10 Mbps
143 constexpr int kPacketSizeBytes = 1000;
144 constexpr int kExpectedBytesSent = kBitrateBps * 15 / 8000;
philipelfd58b612017-01-04 07:05:25 -0800145
Erik Språngbb56d4b2019-11-04 13:53:09 +0000146 prober.CreateProbeCluster(kBitrateBps, 0, /*cluster_id=*/0);
philipelfd58b612017-01-04 07:05:25 -0800147 prober.OnIncomingPacket(kPacketSizeBytes);
148 int bytes_sent = 0;
149 while (bytes_sent < kExpectedBytesSent) {
stefanf00497c2017-01-27 02:27:33 -0800150 ASSERT_TRUE(prober.IsProbing());
Erik Språngbb56d4b2019-11-04 13:53:09 +0000151 prober.ProbeSent(0, kPacketSizeBytes);
philipelfd58b612017-01-04 07:05:25 -0800152 bytes_sent += kPacketSizeBytes;
153 }
154
155 EXPECT_FALSE(prober.IsProbing());
156}
157
Johannes Kron85846672018-11-09 12:39:38 +0100158TEST(BitrateProberTest, HighBitrateProbing) {
Jonas Olsson24923e82019-03-27 14:19:04 +0100159 const FieldTrialBasedConfig config;
160 BitrateProber prober(config);
Erik Språngbb56d4b2019-11-04 13:53:09 +0000161 constexpr int kBitrateBps = 1000000000; // 1 Gbps.
162 constexpr int kPacketSizeBytes = 1000;
163 constexpr int kExpectedBytesSent = (kBitrateBps / 8000) * 15;
Johannes Kron85846672018-11-09 12:39:38 +0100164
Erik Språngbb56d4b2019-11-04 13:53:09 +0000165 prober.CreateProbeCluster(kBitrateBps, 0, 0);
Johannes Kron85846672018-11-09 12:39:38 +0100166 prober.OnIncomingPacket(kPacketSizeBytes);
167 int bytes_sent = 0;
168 while (bytes_sent < kExpectedBytesSent) {
169 ASSERT_TRUE(prober.IsProbing());
Erik Språngbb56d4b2019-11-04 13:53:09 +0000170 prober.ProbeSent(0, kPacketSizeBytes);
Johannes Kron85846672018-11-09 12:39:38 +0100171 bytes_sent += kPacketSizeBytes;
172 }
173
174 EXPECT_FALSE(prober.IsProbing());
175}
176
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100177TEST(BitrateProberTest, ProbeClusterTimeout) {
Jonas Olsson24923e82019-03-27 14:19:04 +0100178 const FieldTrialBasedConfig config;
179 BitrateProber prober(config);
Erik Språngbb56d4b2019-11-04 13:53:09 +0000180 constexpr int kBitrateBps = 300000; // 300 kbps
181 constexpr int kSmallPacketSize = 20;
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100182 // Expecting two probe clusters of 5 packets each.
Erik Språngbb56d4b2019-11-04 13:53:09 +0000183 constexpr int kExpectedBytesSent = 20 * 2 * 5;
184 constexpr int64_t kTimeoutMs = 5000;
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100185
Erik Språngbb56d4b2019-11-04 13:53:09 +0000186 int64_t now_ms = 0;
187 prober.CreateProbeCluster(kBitrateBps, now_ms, /*cluster_id=*/0);
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100188 prober.OnIncomingPacket(kSmallPacketSize);
189 EXPECT_FALSE(prober.IsProbing());
Erik Språngbb56d4b2019-11-04 13:53:09 +0000190 now_ms += kTimeoutMs;
191 prober.CreateProbeCluster(kBitrateBps / 10, now_ms, /*cluster_id=*/1);
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100192 prober.OnIncomingPacket(kSmallPacketSize);
193 EXPECT_FALSE(prober.IsProbing());
Erik Språngbb56d4b2019-11-04 13:53:09 +0000194 now_ms += 1;
195 prober.CreateProbeCluster(kBitrateBps / 10, now_ms, /*cluster_id=*/2);
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100196 prober.OnIncomingPacket(kSmallPacketSize);
197 EXPECT_TRUE(prober.IsProbing());
198 int bytes_sent = 0;
199 while (bytes_sent < kExpectedBytesSent) {
200 ASSERT_TRUE(prober.IsProbing());
Erik Språngbb56d4b2019-11-04 13:53:09 +0000201 prober.ProbeSent(0, kSmallPacketSize);
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100202 bytes_sent += kSmallPacketSize;
203 }
204
205 EXPECT_FALSE(prober.IsProbing());
206}
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000207} // namespace webrtc