blob: 765504459fe7db96cc8d6232f06f33e6e060281b [file] [log] [blame]
philipel863a8262016-06-17 09:21:34 -07001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/congestion_controller/delay_based_bwe.h"
12#include "modules/congestion_controller/delay_based_bwe_unittest_helper.h"
13#include "modules/pacing/paced_sender.h"
14#include "rtc_base/constructormagic.h"
15#include "system_wrappers/include/clock.h"
16#include "test/field_trial.h"
17#include "test/gtest.h"
philipel863a8262016-06-17 09:21:34 -070018
19namespace webrtc {
20
stefan5e12d362016-07-11 01:44:02 -070021namespace {
philipelb5feb2e2017-03-09 07:01:58 -080022constexpr int kNumProbesCluster0 = 5;
23constexpr int kNumProbesCluster1 = 8;
24const PacedPacketInfo kPacingInfo0(0, kNumProbesCluster0, 2000);
25const PacedPacketInfo kPacingInfo1(1, kNumProbesCluster1, 4000);
terelius37647302017-06-27 07:50:31 -070026constexpr float kTargetUtilizationFraction = 0.95f;
stefan5e12d362016-07-11 01:44:02 -070027} // namespace
philipel863a8262016-06-17 09:21:34 -070028
stefan80fba252017-04-18 06:45:12 -070029TEST_F(DelayBasedBweTest, NoCrashEmptyFeedback) {
30 std::vector<PacketFeedback> packet_feedback_vector;
tschumim3fae6282017-06-11 23:57:17 -070031 bitrate_estimator_->IncomingPacketFeedbackVector(packet_feedback_vector,
32 rtc::Optional<uint32_t>());
stefan80fba252017-04-18 06:45:12 -070033}
34
35TEST_F(DelayBasedBweTest, NoCrashOnlyLostFeedback) {
36 std::vector<PacketFeedback> packet_feedback_vector;
37 packet_feedback_vector.push_back(
38 PacketFeedback(-1, -1, 0, 1500, PacedPacketInfo()));
39 packet_feedback_vector.push_back(
40 PacketFeedback(-1, -1, 1, 1500, PacedPacketInfo()));
tschumim3fae6282017-06-11 23:57:17 -070041 bitrate_estimator_->IncomingPacketFeedbackVector(packet_feedback_vector,
42 rtc::Optional<uint32_t>());
stefan80fba252017-04-18 06:45:12 -070043}
44
stefan5e12d362016-07-11 01:44:02 -070045TEST_F(DelayBasedBweTest, ProbeDetection) {
philipel863a8262016-06-17 09:21:34 -070046 int64_t now_ms = clock_.TimeInMilliseconds();
stefan5e12d362016-07-11 01:44:02 -070047 uint16_t seq_num = 0;
philipel863a8262016-06-17 09:21:34 -070048
49 // First burst sent at 8 * 1000 / 10 = 800 kbps.
philipelb5feb2e2017-03-09 07:01:58 -080050 for (int i = 0; i < kNumProbesCluster0; ++i) {
philipel863a8262016-06-17 09:21:34 -070051 clock_.AdvanceTimeMilliseconds(10);
52 now_ms = clock_.TimeInMilliseconds();
philipel8aadd502017-02-23 02:56:13 -080053 IncomingFeedback(now_ms, now_ms, seq_num++, 1000, kPacingInfo0);
philipel863a8262016-06-17 09:21:34 -070054 }
Stefan Holmer280de9e2016-09-30 10:06:51 +020055 EXPECT_TRUE(bitrate_observer_.updated());
philipel863a8262016-06-17 09:21:34 -070056
57 // Second burst sent at 8 * 1000 / 5 = 1600 kbps.
philipelb5feb2e2017-03-09 07:01:58 -080058 for (int i = 0; i < kNumProbesCluster1; ++i) {
philipel863a8262016-06-17 09:21:34 -070059 clock_.AdvanceTimeMilliseconds(5);
60 now_ms = clock_.TimeInMilliseconds();
philipel8aadd502017-02-23 02:56:13 -080061 IncomingFeedback(now_ms, now_ms, seq_num++, 1000, kPacingInfo1);
philipel863a8262016-06-17 09:21:34 -070062 }
63
Stefan Holmer280de9e2016-09-30 10:06:51 +020064 EXPECT_TRUE(bitrate_observer_.updated());
65 EXPECT_GT(bitrate_observer_.latest_bitrate(), 1500000u);
philipel863a8262016-06-17 09:21:34 -070066}
67
stefan5e12d362016-07-11 01:44:02 -070068TEST_F(DelayBasedBweTest, ProbeDetectionNonPacedPackets) {
philipel863a8262016-06-17 09:21:34 -070069 int64_t now_ms = clock_.TimeInMilliseconds();
stefan5e12d362016-07-11 01:44:02 -070070 uint16_t seq_num = 0;
philipel863a8262016-06-17 09:21:34 -070071 // First burst sent at 8 * 1000 / 10 = 800 kbps, but with every other packet
72 // not being paced which could mess things up.
philipelb5feb2e2017-03-09 07:01:58 -080073 for (int i = 0; i < kNumProbesCluster0; ++i) {
philipel863a8262016-06-17 09:21:34 -070074 clock_.AdvanceTimeMilliseconds(5);
75 now_ms = clock_.TimeInMilliseconds();
philipel8aadd502017-02-23 02:56:13 -080076 IncomingFeedback(now_ms, now_ms, seq_num++, 1000, kPacingInfo0);
philipel863a8262016-06-17 09:21:34 -070077 // Non-paced packet, arriving 5 ms after.
78 clock_.AdvanceTimeMilliseconds(5);
philipel8aadd502017-02-23 02:56:13 -080079 IncomingFeedback(now_ms, now_ms, seq_num++, 100, PacedPacketInfo());
philipel863a8262016-06-17 09:21:34 -070080 }
81
Stefan Holmer280de9e2016-09-30 10:06:51 +020082 EXPECT_TRUE(bitrate_observer_.updated());
83 EXPECT_GT(bitrate_observer_.latest_bitrate(), 800000u);
philipel863a8262016-06-17 09:21:34 -070084}
85
stefan5e12d362016-07-11 01:44:02 -070086TEST_F(DelayBasedBweTest, ProbeDetectionFasterArrival) {
philipel863a8262016-06-17 09:21:34 -070087 int64_t now_ms = clock_.TimeInMilliseconds();
stefan5e12d362016-07-11 01:44:02 -070088 uint16_t seq_num = 0;
philipel863a8262016-06-17 09:21:34 -070089 // First burst sent at 8 * 1000 / 10 = 800 kbps.
90 // Arriving at 8 * 1000 / 5 = 1600 kbps.
91 int64_t send_time_ms = 0;
philipelb5feb2e2017-03-09 07:01:58 -080092 for (int i = 0; i < kNumProbesCluster0; ++i) {
philipel863a8262016-06-17 09:21:34 -070093 clock_.AdvanceTimeMilliseconds(1);
94 send_time_ms += 10;
95 now_ms = clock_.TimeInMilliseconds();
philipel8aadd502017-02-23 02:56:13 -080096 IncomingFeedback(now_ms, send_time_ms, seq_num++, 1000, kPacingInfo0);
philipel863a8262016-06-17 09:21:34 -070097 }
98
Stefan Holmer280de9e2016-09-30 10:06:51 +020099 EXPECT_FALSE(bitrate_observer_.updated());
philipel863a8262016-06-17 09:21:34 -0700100}
101
stefan5e12d362016-07-11 01:44:02 -0700102TEST_F(DelayBasedBweTest, ProbeDetectionSlowerArrival) {
philipel863a8262016-06-17 09:21:34 -0700103 int64_t now_ms = clock_.TimeInMilliseconds();
stefan5e12d362016-07-11 01:44:02 -0700104 uint16_t seq_num = 0;
philipel863a8262016-06-17 09:21:34 -0700105 // First burst sent at 8 * 1000 / 5 = 1600 kbps.
106 // Arriving at 8 * 1000 / 7 = 1142 kbps.
terelius37647302017-06-27 07:50:31 -0700107 // Since the receive rate is significantly below the send rate, we expect to
108 // use 95% of the estimated capacity.
philipel863a8262016-06-17 09:21:34 -0700109 int64_t send_time_ms = 0;
philipelb5feb2e2017-03-09 07:01:58 -0800110 for (int i = 0; i < kNumProbesCluster1; ++i) {
philipel863a8262016-06-17 09:21:34 -0700111 clock_.AdvanceTimeMilliseconds(7);
112 send_time_ms += 5;
113 now_ms = clock_.TimeInMilliseconds();
philipel8aadd502017-02-23 02:56:13 -0800114 IncomingFeedback(now_ms, send_time_ms, seq_num++, 1000, kPacingInfo1);
philipel863a8262016-06-17 09:21:34 -0700115 }
116
Stefan Holmer280de9e2016-09-30 10:06:51 +0200117 EXPECT_TRUE(bitrate_observer_.updated());
terelius37647302017-06-27 07:50:31 -0700118 EXPECT_NEAR(bitrate_observer_.latest_bitrate(),
119 kTargetUtilizationFraction * 1140000u, 10000u);
philipel863a8262016-06-17 09:21:34 -0700120}
121
stefan5e12d362016-07-11 01:44:02 -0700122TEST_F(DelayBasedBweTest, ProbeDetectionSlowerArrivalHighBitrate) {
philipel863a8262016-06-17 09:21:34 -0700123 int64_t now_ms = clock_.TimeInMilliseconds();
stefan5e12d362016-07-11 01:44:02 -0700124 uint16_t seq_num = 0;
philipel863a8262016-06-17 09:21:34 -0700125 // Burst sent at 8 * 1000 / 1 = 8000 kbps.
126 // Arriving at 8 * 1000 / 2 = 4000 kbps.
terelius37647302017-06-27 07:50:31 -0700127 // Since the receive rate is significantly below the send rate, we expect to
128 // use 95% of the estimated capacity.
philipel863a8262016-06-17 09:21:34 -0700129 int64_t send_time_ms = 0;
philipelb5feb2e2017-03-09 07:01:58 -0800130 for (int i = 0; i < kNumProbesCluster1; ++i) {
philipel863a8262016-06-17 09:21:34 -0700131 clock_.AdvanceTimeMilliseconds(2);
132 send_time_ms += 1;
133 now_ms = clock_.TimeInMilliseconds();
philipel8aadd502017-02-23 02:56:13 -0800134 IncomingFeedback(now_ms, send_time_ms, seq_num++, 1000, kPacingInfo1);
philipel863a8262016-06-17 09:21:34 -0700135 }
136
Stefan Holmer280de9e2016-09-30 10:06:51 +0200137 EXPECT_TRUE(bitrate_observer_.updated());
terelius37647302017-06-27 07:50:31 -0700138 EXPECT_NEAR(bitrate_observer_.latest_bitrate(),
139 kTargetUtilizationFraction * 4000000u, 10000u);
philipel863a8262016-06-17 09:21:34 -0700140}
141
alexnarestb335e312017-09-19 12:00:32 -0700142TEST_F(DelayBasedBweTest, GetExpectedBwePeriodMs) {
143 int64_t default_interval_ms = bitrate_estimator_->GetExpectedBwePeriodMs();
144 EXPECT_GT(default_interval_ms, 0);
145 CapacityDropTestHelper(1, true, 333, 0);
146 int64_t interval_ms = bitrate_estimator_->GetExpectedBwePeriodMs();
147 EXPECT_GT(interval_ms, 0);
148 EXPECT_NE(interval_ms, default_interval_ms);
149}
150
stefan5e12d362016-07-11 01:44:02 -0700151TEST_F(DelayBasedBweTest, InitialBehavior) {
michaelt8490f8a2017-04-20 10:10:10 -0700152 InitialBehaviorTestHelper(730000);
stefan5e12d362016-07-11 01:44:02 -0700153}
154
155TEST_F(DelayBasedBweTest, RateIncreaseReordering) {
michaelt8490f8a2017-04-20 10:10:10 -0700156 RateIncreaseReorderingTestHelper(730000);
stefan5e12d362016-07-11 01:44:02 -0700157}
stefan5e12d362016-07-11 01:44:02 -0700158TEST_F(DelayBasedBweTest, RateIncreaseRtpTimestamps) {
michaelt8490f8a2017-04-20 10:10:10 -0700159 RateIncreaseRtpTimestampsTestHelper(627);
stefan5e12d362016-07-11 01:44:02 -0700160}
161
162TEST_F(DelayBasedBweTest, CapacityDropOneStream) {
michaelt8490f8a2017-04-20 10:10:10 -0700163 CapacityDropTestHelper(1, false, 300, 0);
stefan5e12d362016-07-11 01:44:02 -0700164}
165
166TEST_F(DelayBasedBweTest, CapacityDropPosOffsetChange) {
stefan76d9c9c2017-04-01 06:51:09 -0700167 CapacityDropTestHelper(1, false, 867, 30000);
stefan5e12d362016-07-11 01:44:02 -0700168}
169
170TEST_F(DelayBasedBweTest, CapacityDropNegOffsetChange) {
michaelt8490f8a2017-04-20 10:10:10 -0700171 CapacityDropTestHelper(1, false, 933, -30000);
stefan5e12d362016-07-11 01:44:02 -0700172}
173
174TEST_F(DelayBasedBweTest, CapacityDropOneStreamWrap) {
stefan76d9c9c2017-04-01 06:51:09 -0700175 CapacityDropTestHelper(1, true, 333, 0);
stefan5e12d362016-07-11 01:44:02 -0700176}
michaelt8490f8a2017-04-20 10:10:10 -0700177
stefan5e12d362016-07-11 01:44:02 -0700178TEST_F(DelayBasedBweTest, TestTimestampGrouping) {
179 TestTimestampGroupingTestHelper();
180}
181
182TEST_F(DelayBasedBweTest, TestShortTimeoutAndWrap) {
183 // Simulate a client leaving and rejoining the call after 35 seconds. This
184 // will make abs send time wrap, so if streams aren't timed out properly
185 // the next 30 seconds of packets will be out of order.
186 TestWrappingHelper(35);
187}
188
189TEST_F(DelayBasedBweTest, TestLongTimeoutAndWrap) {
190 // Simulate a client leaving and rejoining the call after some multiple of
191 // 64 seconds later. This will cause a zero difference in abs send times due
192 // to the wrap, but a big difference in arrival time, if streams aren't
193 // properly timed out.
194 TestWrappingHelper(10 * 64);
philipel863a8262016-06-17 09:21:34 -0700195}
Stefan Holmer492ee282016-10-27 17:19:20 +0200196
tereliusa9521e22017-07-04 04:52:58 -0700197TEST_F(DelayBasedBweTest, TestInitialOveruse) {
198 const uint32_t kStartBitrate = 300e3;
199 const uint32_t kInitialCapacityBps = 200e3;
200 const uint32_t kDummySsrc = 0;
201 // High FPS to ensure that we send a lot of packets in a short time.
202 const int kFps = 90;
203
204 stream_generator_->AddStream(new test::RtpStream(kFps, kStartBitrate));
205 stream_generator_->set_capacity_bps(kInitialCapacityBps);
206
207 // Needed to initialize the AimdRateControl.
208 bitrate_estimator_->SetStartBitrate(kStartBitrate);
209
210 // Produce 30 frames (in 1/3 second) and give them to the estimator.
211 uint32_t bitrate_bps = kStartBitrate;
212 bool seen_overuse = false;
213 for (int i = 0; i < 30; ++i) {
214 bool overuse = GenerateAndProcessFrame(kDummySsrc, bitrate_bps);
215 // The purpose of this test is to ensure that we back down even if we don't
216 // have any acknowledged bitrate estimate yet. Hence, if the test works
217 // as expected, we should not have a measured bitrate yet.
218 EXPECT_FALSE(acknowledged_bitrate_estimator_->bitrate_bps().has_value());
219 if (overuse) {
220 EXPECT_TRUE(bitrate_observer_.updated());
221 EXPECT_NEAR(bitrate_observer_.latest_bitrate(), kStartBitrate / 2, 15000);
222 bitrate_bps = bitrate_observer_.latest_bitrate();
223 seen_overuse = true;
224 break;
225 } else if (bitrate_observer_.updated()) {
226 bitrate_bps = bitrate_observer_.latest_bitrate();
227 bitrate_observer_.Reset();
228 }
229 }
230 EXPECT_TRUE(seen_overuse);
231 EXPECT_NEAR(bitrate_observer_.latest_bitrate(), kStartBitrate / 2, 15000);
232}
233
philipel863a8262016-06-17 09:21:34 -0700234} // namespace webrtc