1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/test/simple_test_tick_clock.h"
8 #include "media/cast/cast_defines.h"
9 #include "media/cast/congestion_control/congestion_control.h"
10 #include "testing/gtest/include/gtest/gtest.h"
15 static const uint32 kMaxBitrateConfigured = 5000000;
16 static const uint32 kMinBitrateConfigured = 500000;
17 static const uint32 kStartBitrate = 2000000;
18 static const int64 kStartMillisecond = INT64_C(12345678900000);
19 static const int64 kRttMs = 20;
20 static const int64 kAckRateMs = 33;
22 class CongestionControlTest : public ::testing::Test {
24 CongestionControlTest()
25 : congestion_control_(&testing_clock_,
26 kDefaultCongestionControlBackOff,
27 kMaxBitrateConfigured,
28 kMinBitrateConfigured,
30 testing_clock_.Advance(
31 base::TimeDelta::FromMilliseconds(kStartMillisecond));
34 // Returns the last bitrate of the run.
35 uint32 RunWithOneLossEventPerSecond(int fps,
37 int runtime_in_seconds) {
38 const base::TimeDelta rtt = base::TimeDelta::FromMilliseconds(rtt_ms);
39 const base::TimeDelta ack_rate =
40 base::TimeDelta::FromMilliseconds(INT64_C(1000) / fps);
41 uint32 new_bitrate = 0;
42 EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));
44 for (int seconds = 0; seconds < runtime_in_seconds; ++seconds) {
45 for (int i = 1; i < fps; ++i) {
46 testing_clock_.Advance(ack_rate);
47 congestion_control_.OnAck(rtt, &new_bitrate);
49 EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
54 base::SimpleTestTickClock testing_clock_;
55 CongestionControl congestion_control_;
57 DISALLOW_COPY_AND_ASSIGN(CongestionControlTest);
60 TEST_F(CongestionControlTest, Max) {
61 uint32 new_bitrate = 0;
62 const base::TimeDelta rtt = base::TimeDelta::FromMilliseconds(kRttMs);
63 const base::TimeDelta ack_rate =
64 base::TimeDelta::FromMilliseconds(kAckRateMs);
65 EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));
67 uint32 expected_increase_bitrate = 0;
69 // Expected time is 5 seconds. 500000 - 2000000 = 5 * 1500 * 8 * (1000 / 20).
70 for (int i = 0; i < 151; ++i) {
71 testing_clock_.Advance(ack_rate);
72 EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
73 expected_increase_bitrate += 1500 * 8 * kAckRateMs / kRttMs;
74 EXPECT_EQ(kStartBitrate + expected_increase_bitrate, new_bitrate);
76 testing_clock_.Advance(ack_rate);
77 EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
78 EXPECT_EQ(kMaxBitrateConfigured, new_bitrate);
81 TEST_F(CongestionControlTest, Min) {
82 uint32 new_bitrate = 0;
83 const base::TimeDelta rtt = base::TimeDelta::FromMilliseconds(kRttMs);
84 const base::TimeDelta ack_rate =
85 base::TimeDelta::FromMilliseconds(kAckRateMs);
86 EXPECT_FALSE(congestion_control_.OnNack(rtt, &new_bitrate));
88 uint32 expected_decrease_bitrate = kStartBitrate;
90 // Expected number is 10. 2000 * 0.875^10 <= 500.
91 for (int i = 0; i < 10; ++i) {
92 testing_clock_.Advance(ack_rate);
93 EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
94 expected_decrease_bitrate = static_cast<uint32>(
95 expected_decrease_bitrate * kDefaultCongestionControlBackOff);
96 EXPECT_EQ(expected_decrease_bitrate, new_bitrate);
98 testing_clock_.Advance(ack_rate);
99 EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
100 EXPECT_EQ(kMinBitrateConfigured, new_bitrate);
103 TEST_F(CongestionControlTest, Timing) {
104 const base::TimeDelta rtt = base::TimeDelta::FromMilliseconds(kRttMs);
105 const base::TimeDelta ack_rate =
106 base::TimeDelta::FromMilliseconds(kAckRateMs);
107 uint32 new_bitrate = 0;
108 uint32 expected_bitrate = kStartBitrate;
110 EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));
112 testing_clock_.Advance(ack_rate);
113 EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
114 expected_bitrate += 1500 * 8 * kAckRateMs / kRttMs;
115 EXPECT_EQ(expected_bitrate, new_bitrate);
117 // We should back immediately.
118 EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
120 static_cast<uint32>(expected_bitrate * kDefaultCongestionControlBackOff);
121 EXPECT_EQ(expected_bitrate, new_bitrate);
123 // Less than one RTT have passed don't back again.
124 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
125 EXPECT_FALSE(congestion_control_.OnNack(rtt, &new_bitrate));
127 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
128 EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
130 static_cast<uint32>(expected_bitrate * kDefaultCongestionControlBackOff);
131 EXPECT_EQ(expected_bitrate, new_bitrate);
133 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
134 EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));
135 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
136 EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
137 expected_bitrate += 1500 * 8 * 20 / kRttMs;
138 EXPECT_EQ(expected_bitrate, new_bitrate);
140 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
141 EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));
142 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
143 EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
144 expected_bitrate += 1500 * 8 * 20 / kRttMs;
145 EXPECT_EQ(expected_bitrate, new_bitrate);
147 // Test long elapsed time (300 ms).
148 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(300));
149 EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
150 expected_bitrate += 1500 * 8 * 100 / kRttMs;
151 EXPECT_EQ(expected_bitrate, new_bitrate);
153 // Test many short elapsed time (1 ms).
154 for (int i = 0; i < 19; ++i) {
155 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(1));
156 EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));
158 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(1));
159 EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
160 expected_bitrate += 1500 * 8 * 20 / kRttMs;
161 EXPECT_EQ(expected_bitrate, new_bitrate);
164 TEST_F(CongestionControlTest, Convergence24fps) {
165 EXPECT_GE(RunWithOneLossEventPerSecond(24, kRttMs, 100), UINT32_C(3000000));
168 TEST_F(CongestionControlTest, Convergence24fpsLongRtt) {
169 EXPECT_GE(RunWithOneLossEventPerSecond(24, 100, 100), UINT32_C(500000));
172 TEST_F(CongestionControlTest, Convergence60fps) {
173 EXPECT_GE(RunWithOneLossEventPerSecond(60, kRttMs, 100), UINT32_C(3500000));
176 TEST_F(CongestionControlTest, Convergence60fpsLongRtt) {
177 EXPECT_GE(RunWithOneLossEventPerSecond(60, 100, 100), UINT32_C(500000));