Home | History | Annotate | Download | only in congestion_control
      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 "base/test/simple_test_tick_clock.h"
      6 #include "media/cast/cast_defines.h"
      7 #include "media/cast/congestion_control/congestion_control.h"
      8 #include "testing/gtest/include/gtest/gtest.h"
      9 
     10 namespace media {
     11 namespace cast {
     12 
     13 static const uint32 kMaxBitrateConfigured = 5000000;
     14 static const uint32 kMinBitrateConfigured = 500000;
     15 static const uint32 kStartBitrate = 2000000;
     16 static const int64 kStartMillisecond = GG_INT64_C(12345678900000);
     17 static const int64 kRttMs = 20;
     18 static const int64 kAckRateMs = 33;
     19 
     20 class CongestionControlTest : public ::testing::Test {
     21  protected:
     22   CongestionControlTest()
     23       : congestion_control_(&testing_clock_,
     24                             kDefaultCongestionControlBackOff,
     25                             kMaxBitrateConfigured,
     26                             kMinBitrateConfigured,
     27                             kStartBitrate) {
     28     testing_clock_.Advance(
     29         base::TimeDelta::FromMilliseconds(kStartMillisecond));
     30   }
     31 
     32   // Returns the last bitrate of the run.
     33   uint32 RunWithOneLossEventPerSecond(int fps, int rtt_ms,
     34                                       int runtime_in_seconds) {
     35     const base::TimeDelta rtt = base::TimeDelta::FromMilliseconds(rtt_ms);
     36     const base::TimeDelta ack_rate =
     37         base::TimeDelta::FromMilliseconds(GG_INT64_C(1000) / fps);
     38     uint32 new_bitrate = 0;
     39     EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));
     40 
     41     for (int seconds = 0; seconds < runtime_in_seconds; ++seconds) {
     42       for (int i = 1; i < fps; ++i) {
     43         testing_clock_.Advance(ack_rate);
     44         congestion_control_.OnAck(rtt, &new_bitrate);
     45       }
     46       EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
     47     }
     48     return new_bitrate;
     49   }
     50 
     51   base::SimpleTestTickClock testing_clock_;
     52   CongestionControl congestion_control_;
     53 };
     54 
     55 TEST_F(CongestionControlTest, Max) {
     56   uint32 new_bitrate = 0;
     57   const base::TimeDelta rtt = base::TimeDelta::FromMilliseconds(kRttMs);
     58   const base::TimeDelta ack_rate =
     59       base::TimeDelta::FromMilliseconds(kAckRateMs);
     60   EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));
     61 
     62   uint32 expected_increase_bitrate = 0;
     63 
     64   // Expected time is 5 seconds. 500000 - 2000000 = 5 * 1500 * 8 * (1000 / 20).
     65   for (int i = 0; i < 151; ++i) {
     66     testing_clock_.Advance(ack_rate);
     67     EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
     68     expected_increase_bitrate += 1500 * 8 * kAckRateMs / kRttMs;
     69     EXPECT_EQ(kStartBitrate + expected_increase_bitrate, new_bitrate);
     70   }
     71   testing_clock_.Advance(ack_rate);
     72   EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
     73   EXPECT_EQ(kMaxBitrateConfigured, new_bitrate);
     74 }
     75 
     76 TEST_F(CongestionControlTest, Min) {
     77   uint32 new_bitrate = 0;
     78   const base::TimeDelta rtt = base::TimeDelta::FromMilliseconds(kRttMs);
     79   const base::TimeDelta ack_rate =
     80       base::TimeDelta::FromMilliseconds(kAckRateMs);
     81   EXPECT_FALSE(congestion_control_.OnNack(rtt, &new_bitrate));
     82 
     83   uint32 expected_decrease_bitrate = kStartBitrate;
     84 
     85   // Expected number is 10. 2000 * 0.875^10 <= 500.
     86   for (int i = 0; i < 10; ++i) {
     87     testing_clock_.Advance(ack_rate);
     88     EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
     89     expected_decrease_bitrate = static_cast<uint32>(
     90         expected_decrease_bitrate * kDefaultCongestionControlBackOff);
     91     EXPECT_EQ(expected_decrease_bitrate, new_bitrate);
     92   }
     93   testing_clock_.Advance(ack_rate);
     94   EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
     95   EXPECT_EQ(kMinBitrateConfigured, new_bitrate);
     96 }
     97 
     98 TEST_F(CongestionControlTest, Timing) {
     99   const base::TimeDelta rtt = base::TimeDelta::FromMilliseconds(kRttMs);
    100   const base::TimeDelta ack_rate =
    101      base::TimeDelta::FromMilliseconds(kAckRateMs);
    102   uint32 new_bitrate = 0;
    103   uint32 expected_bitrate = kStartBitrate;
    104 
    105   EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));
    106 
    107   testing_clock_.Advance(ack_rate);
    108   EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
    109   expected_bitrate += 1500 * 8 * kAckRateMs / kRttMs;
    110   EXPECT_EQ(expected_bitrate, new_bitrate);
    111 
    112   // We should back immediately.
    113   EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
    114   expected_bitrate = static_cast<uint32>(
    115       expected_bitrate * kDefaultCongestionControlBackOff);
    116   EXPECT_EQ(expected_bitrate, new_bitrate);
    117 
    118   // Less than one RTT have passed don't back again.
    119   testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
    120   EXPECT_FALSE(congestion_control_.OnNack(rtt, &new_bitrate));
    121 
    122   testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
    123   EXPECT_TRUE(congestion_control_.OnNack(rtt, &new_bitrate));
    124   expected_bitrate = static_cast<uint32>(
    125       expected_bitrate * kDefaultCongestionControlBackOff);
    126   EXPECT_EQ(expected_bitrate, new_bitrate);
    127 
    128   testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
    129   EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));
    130   testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
    131   EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
    132   expected_bitrate += 1500 * 8 * 20 / kRttMs;
    133   EXPECT_EQ(expected_bitrate, new_bitrate);
    134 
    135   testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
    136   EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));
    137   testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
    138   EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
    139   expected_bitrate += 1500 * 8 * 20 / kRttMs;
    140   EXPECT_EQ(expected_bitrate, new_bitrate);
    141 
    142   // Test long elapsed time (300 ms).
    143   testing_clock_.Advance(base::TimeDelta::FromMilliseconds(300));
    144   EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
    145   expected_bitrate += 1500 * 8 * 100 / kRttMs;
    146   EXPECT_EQ(expected_bitrate, new_bitrate);
    147 
    148   // Test many short elapsed time (1 ms).
    149   for (int i = 0; i < 19; ++i) {
    150     testing_clock_.Advance(base::TimeDelta::FromMilliseconds(1));
    151     EXPECT_FALSE(congestion_control_.OnAck(rtt, &new_bitrate));
    152   }
    153   testing_clock_.Advance(base::TimeDelta::FromMilliseconds(1));
    154   EXPECT_TRUE(congestion_control_.OnAck(rtt, &new_bitrate));
    155   expected_bitrate += 1500 * 8 * 20 / kRttMs;
    156   EXPECT_EQ(expected_bitrate, new_bitrate);
    157 }
    158 
    159 TEST_F(CongestionControlTest, Convergence24fps) {
    160   EXPECT_GE(RunWithOneLossEventPerSecond(24, kRttMs, 100),
    161             GG_UINT32_C(3000000));
    162 }
    163 
    164 TEST_F(CongestionControlTest, Convergence24fpsLongRtt) {
    165   EXPECT_GE(RunWithOneLossEventPerSecond(24, 100, 100),
    166             GG_UINT32_C(500000));
    167 }
    168 
    169 TEST_F(CongestionControlTest, Convergence60fps) {
    170   EXPECT_GE(RunWithOneLossEventPerSecond(60, kRttMs, 100),
    171             GG_UINT32_C(3500000));
    172 }
    173 
    174 TEST_F(CongestionControlTest, Convergence60fpsLongRtt) {
    175   EXPECT_GE(RunWithOneLossEventPerSecond(60, 100, 100),
    176             GG_UINT32_C(500000));
    177 }
    178 
    179 }  // namespace cast
    180 }  // namespace media
    181