Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / media / cast / congestion_control / congestion_control_unittest.cc
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.
4
5 #include <stdint.h>
6
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"
11
12 namespace media {
13 namespace cast {
14
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;
21
22 class CongestionControlTest : public ::testing::Test {
23  protected:
24   CongestionControlTest()
25       : congestion_control_(&testing_clock_,
26                             kDefaultCongestionControlBackOff,
27                             kMaxBitrateConfigured,
28                             kMinBitrateConfigured,
29                             kStartBitrate) {
30     testing_clock_.Advance(
31         base::TimeDelta::FromMilliseconds(kStartMillisecond));
32   }
33
34   // Returns the last bitrate of the run.
35   uint32 RunWithOneLossEventPerSecond(int fps,
36                                       int rtt_ms,
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));
43
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);
48       }
49       EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
50     }
51     return new_bitrate;
52   }
53
54   base::SimpleTestTickClock testing_clock_;
55   CongestionControl congestion_control_;
56
57   DISALLOW_COPY_AND_ASSIGN(CongestionControlTest);
58 };
59
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));
66
67   uint32 expected_increase_bitrate = 0;
68
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);
75   }
76   testing_clock_.Advance(ack_rate);
77   EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
78   EXPECT_EQ(kMaxBitrateConfigured, new_bitrate);
79 }
80
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));
87
88   uint32 expected_decrease_bitrate = kStartBitrate;
89
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);
97   }
98   testing_clock_.Advance(ack_rate);
99   EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
100   EXPECT_EQ(kMinBitrateConfigured, new_bitrate);
101 }
102
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;
109
110   EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));
111
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);
116
117   // We should back immediately.
118   EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
119   expected_bitrate =
120       static_cast<uint32>(expected_bitrate * kDefaultCongestionControlBackOff);
121   EXPECT_EQ(expected_bitrate, new_bitrate);
122
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));
126
127   testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
128   EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
129   expected_bitrate =
130       static_cast<uint32>(expected_bitrate * kDefaultCongestionControlBackOff);
131   EXPECT_EQ(expected_bitrate, new_bitrate);
132
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);
139
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);
146
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);
152
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));
157   }
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);
162 }
163
164 TEST_F(CongestionControlTest, Convergence24fps) {
165   EXPECT_GE(RunWithOneLossEventPerSecond(24, kRttMs, 100), UINT32_C(3000000));
166 }
167
168 TEST_F(CongestionControlTest, Convergence24fpsLongRtt) {
169   EXPECT_GE(RunWithOneLossEventPerSecond(24, 100, 100), UINT32_C(500000));
170 }
171
172 TEST_F(CongestionControlTest, Convergence60fps) {
173   EXPECT_GE(RunWithOneLossEventPerSecond(60, kRttMs, 100), UINT32_C(3500000));
174 }
175
176 TEST_F(CongestionControlTest, Convergence60fpsLongRtt) {
177   EXPECT_GE(RunWithOneLossEventPerSecond(60, 100, 100), UINT32_C(500000));
178 }
179
180 }  // namespace cast
181 }  // namespace media