Home | History | Annotate | Download | only in test
      1 /*
      2  *  Copyright (c) 2013 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 
     11 #include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_framework.h"
     12 
     13 #include <numeric>
     14 
     15 #include "testing/gtest/include/gtest/gtest.h"
     16 #include "webrtc/base/constructormagic.h"
     17 #include "webrtc/modules/remote_bitrate_estimator/test/packet.h"
     18 #include "webrtc/modules/remote_bitrate_estimator/test/packet_sender.h"
     19 #include "webrtc/test/testsupport/fileutils.h"
     20 
     21 namespace webrtc {
     22 namespace testing {
     23 namespace bwe {
     24 
     25 static bool IsSequenceNumberSorted(const Packets& packets) {
     26   PacketsConstIt last_it = packets.begin();
     27   for (PacketsConstIt it = last_it; it != packets.end(); ++it) {
     28     const MediaPacket* packet = static_cast<const MediaPacket*>(*it);
     29     const MediaPacket* last_packet = static_cast<const MediaPacket*>(*last_it);
     30     if (IsNewerSequenceNumber(last_packet->header().sequenceNumber,
     31                               packet->header().sequenceNumber)) {
     32       return false;
     33     }
     34     last_it = it;
     35   }
     36   return true;
     37 }
     38 
     39 TEST(BweTestFramework_PacketTest, IsTimeSorted) {
     40   Packets packets;
     41   // Insert some packets in order...
     42   EXPECT_TRUE(IsTimeSorted(packets));
     43 
     44   packets.push_back(new MediaPacket(100, 0));
     45   EXPECT_TRUE(IsTimeSorted(packets));
     46 
     47   packets.push_back(new MediaPacket(110, 0));
     48   EXPECT_TRUE(IsTimeSorted(packets));
     49 
     50   // ...and one out-of-order...
     51   packets.push_back(new MediaPacket(100, 0));
     52   EXPECT_FALSE(IsTimeSorted(packets));
     53 
     54   // ...remove the out-of-order packet, insert another in-order packet.
     55   delete packets.back();
     56   packets.pop_back();
     57   packets.push_back(new MediaPacket(120, 0));
     58   EXPECT_TRUE(IsTimeSorted(packets));
     59 
     60   for (auto* packet : packets)
     61     delete packet;
     62 }
     63 
     64 TEST(BweTestFramework_PacketTest, IsSequenceNumberSorted) {
     65   Packets packets;
     66   // Insert some packets in order...
     67   EXPECT_TRUE(IsSequenceNumberSorted(packets));
     68 
     69   packets.push_back(new MediaPacket(0, 100));
     70   EXPECT_TRUE(IsSequenceNumberSorted(packets));
     71 
     72   packets.push_back(new MediaPacket(0, 110));
     73   EXPECT_TRUE(IsSequenceNumberSorted(packets));
     74 
     75   // ...and one out-of-order...
     76   packets.push_back(new MediaPacket(0, 100));
     77   EXPECT_FALSE(IsSequenceNumberSorted(packets));
     78 
     79   // ...remove the out-of-order packet, insert another in-order packet.
     80   delete packets.back();
     81   packets.pop_back();
     82   packets.push_back(new MediaPacket(0, 120));
     83   EXPECT_TRUE(IsSequenceNumberSorted(packets));
     84 
     85   for (auto* packet : packets)
     86     delete packet;
     87 }
     88 
     89 TEST(BweTestFramework_StatsTest, Mean) {
     90   Stats<int32_t> stats;
     91   EXPECT_EQ(0, stats.GetMean());
     92 
     93   stats.Push(1);
     94   stats.Push(3);
     95   EXPECT_EQ(2, stats.GetMean());
     96 
     97   // Integer division rounds (1+3-3)/3 to 0.
     98   stats.Push(-3);
     99   EXPECT_EQ(0, stats.GetMean());
    100 }
    101 
    102 TEST(BweTestFramework_StatsTest, Variance) {
    103   Stats<int32_t> stats;
    104   EXPECT_EQ(0, stats.GetVariance());
    105 
    106   // Mean is 2 ; ((1-2)*(1-2)+(3-2)*(3-2))/2 = (1+1)/2 = 1
    107   stats.Push(1);
    108   stats.Push(3);
    109   EXPECT_EQ(1, stats.GetVariance());
    110 
    111   // Integer division rounds 26/3 to 8
    112   // Mean is 0 ; (1*1+3*3+(-4)*(-4))/3 = (1+9+16)/3 = 8
    113   stats.Push(-4);
    114   EXPECT_EQ(8, stats.GetVariance());
    115 }
    116 
    117 TEST(BweTestFramework_StatsTest, StdDev) {
    118   Stats<int32_t> stats;
    119   EXPECT_EQ(0, stats.GetStdDev());
    120 
    121   // Variance is 1 ; sqrt(1) = 1
    122   stats.Push(1);
    123   stats.Push(3);
    124   EXPECT_EQ(1, stats.GetStdDev());
    125 
    126   // Variance is 8 ; sqrt(8) = 2 with integers.
    127   stats.Push(-4);
    128   EXPECT_EQ(2, stats.GetStdDev());
    129 }
    130 
    131 TEST(BweTestFramework_StatsTest, MinMax) {
    132   Stats<int32_t> stats;
    133   EXPECT_EQ(0, stats.GetMin());
    134   EXPECT_EQ(0, stats.GetMax());
    135 
    136   stats.Push(1);
    137   EXPECT_EQ(1, stats.GetMin());
    138   EXPECT_EQ(1, stats.GetMax());
    139 
    140   stats.Push(3);
    141   EXPECT_EQ(1, stats.GetMin());
    142   EXPECT_EQ(3, stats.GetMax());
    143 
    144   stats.Push(-4);
    145   EXPECT_EQ(-4, stats.GetMin());
    146   EXPECT_EQ(3, stats.GetMax());
    147 }
    148 
    149 class BweTestFramework_RateCounterFilterTest : public ::testing::Test {
    150  public:
    151   BweTestFramework_RateCounterFilterTest()
    152       : filter_(NULL, 0, "", ""), now_ms_(0) {}
    153   virtual ~BweTestFramework_RateCounterFilterTest() {}
    154 
    155  protected:
    156   void TestRateCounter(int64_t run_for_ms, uint32_t payload_bits,
    157                        uint32_t expected_pps, uint32_t expected_bps) {
    158     Packets packets;
    159     RTPHeader header;
    160     // "Send" a packet every 10 ms.
    161     for (int64_t i = 0; i < run_for_ms; i += 10, now_ms_ += 10) {
    162       packets.push_back(
    163           new MediaPacket(0, now_ms_ * 1000, payload_bits / 8, header));
    164     }
    165     filter_.RunFor(run_for_ms, &packets);
    166     ASSERT_TRUE(IsTimeSorted(packets));
    167     EXPECT_EQ(expected_pps, filter_.packets_per_second());
    168     EXPECT_EQ(expected_bps, filter_.bits_per_second());
    169 
    170     for (auto* packet : packets)
    171       delete packet;
    172   }
    173 
    174  private:
    175   RateCounterFilter filter_;
    176   int64_t now_ms_;
    177 
    178   RTC_DISALLOW_COPY_AND_ASSIGN(BweTestFramework_RateCounterFilterTest);
    179 };
    180 
    181 TEST_F(BweTestFramework_RateCounterFilterTest, Short) {
    182   // 100ms, 100 bytes per packet, should result in 10 pps and 8 kbps. We're
    183   // generating one packet every 10 ms ; 10 * 800 = 8k
    184   TestRateCounter(100, 800, 10, 8000);
    185 }
    186 
    187 TEST_F(BweTestFramework_RateCounterFilterTest, Medium) {
    188   // 100ms, like above.
    189   TestRateCounter(100, 800, 10, 8000);
    190   // 1000ms, 100 bpp, should result in 100 pps and 80 kbps. We're still
    191   // generating packets every 10 ms.
    192   TestRateCounter(900, 800, 100, 80000);
    193 }
    194 
    195 TEST_F(BweTestFramework_RateCounterFilterTest, Long) {
    196   // 100ms, 1000ms, like above.
    197   TestRateCounter(100, 800, 10, 8000);
    198   TestRateCounter(900, 800, 100, 80000);
    199   // 2000ms, should only see rate of last second, so 100 pps, and 40 kbps now.
    200   TestRateCounter(1000, 400, 100, 40000);
    201   // 2500ms, half a second with zero payload size. We should get same pps as
    202   // before, but kbps should drop to half of previous rate.
    203   TestRateCounter(500, 0, 100, 20000);
    204   // Another half second with zero payload size. Now the kbps rate should drop
    205   // to zero.
    206   TestRateCounter(500, 0, 100, 0);
    207   // Increate payload size again. 200 * 100 * 0.5 = 10 kbps.
    208   TestRateCounter(500, 200, 100, 10000);
    209 }
    210 
    211 static void TestLossFilter(float loss_percent, bool zero_tolerance) {
    212   LossFilter filter(NULL, 0);
    213   filter.SetLoss(loss_percent);
    214   Packets::size_type sent_packets = 0;
    215   Packets::size_type remaining_packets = 0;
    216 
    217   // No input should yield no output
    218   {
    219     Packets packets;
    220     sent_packets += packets.size();
    221     filter.RunFor(0, &packets);
    222     ASSERT_TRUE(IsTimeSorted(packets));
    223     ASSERT_TRUE(IsSequenceNumberSorted(packets));
    224     remaining_packets += packets.size();
    225     EXPECT_EQ(0u, sent_packets);
    226     EXPECT_EQ(0u, remaining_packets);
    227     for (auto* packet : packets)
    228       delete packet;
    229   }
    230 
    231   // Generate and process 10000 packets in different batch sizes (some empty)
    232   for (int i = 0; i < 2225; ++i) {
    233     Packets packets;
    234     for (int j = 0; j < i % 10; ++j)
    235       packets.push_back(new MediaPacket(i, i));
    236     sent_packets += packets.size();
    237     filter.RunFor(0, &packets);
    238     ASSERT_TRUE(IsTimeSorted(packets));
    239     ASSERT_TRUE(IsSequenceNumberSorted(packets));
    240     remaining_packets += packets.size();
    241     for (auto* packet : packets)
    242       delete packet;
    243   }
    244 
    245   float loss_fraction = 0.01f * (100.0f - loss_percent);
    246   Packets::size_type expected_packets = loss_fraction * sent_packets;
    247   if (zero_tolerance) {
    248     EXPECT_EQ(expected_packets, remaining_packets);
    249   } else {
    250     // Require within 1% of expected
    251     EXPECT_NEAR(expected_packets, remaining_packets, 100);
    252   }
    253 }
    254 
    255 TEST(BweTestFramework_LossFilterTest, Loss0) {
    256   // With 0% loss, the result should be exact (no loss).
    257   TestLossFilter(0.0f, true);
    258 }
    259 
    260 TEST(BweTestFramework_LossFilterTest, Loss10) {
    261   TestLossFilter(10.0f, false);
    262 }
    263 
    264 TEST(BweTestFramework_LossFilterTest, Loss50) {
    265   TestLossFilter(50.0f, false);
    266 }
    267 
    268 TEST(BweTestFramework_LossFilterTest, Loss100) {
    269   // With 100% loss, the result should be exact (no packets out).
    270   TestLossFilter(100.0f, true);
    271 }
    272 
    273 class BweTestFramework_DelayFilterTest : public ::testing::Test {
    274  public:
    275   BweTestFramework_DelayFilterTest()
    276       : filter_(NULL, 0), now_ms_(0), sequence_number_(0) {}
    277   virtual ~BweTestFramework_DelayFilterTest() {
    278     for (auto* packet : accumulated_packets_)
    279       delete packet;
    280   }
    281 
    282  protected:
    283   void TestDelayFilter(int64_t run_for_ms, uint32_t in_packets,
    284                        uint32_t out_packets) {
    285     Packets packets;
    286     for (uint32_t i = 0; i < in_packets; ++i) {
    287       packets.push_back(new MediaPacket(
    288           now_ms_ * 1000 + (sequence_number_ >> 4), sequence_number_));
    289       sequence_number_++;
    290     }
    291     filter_.RunFor(run_for_ms, &packets);
    292     ASSERT_TRUE(IsTimeSorted(packets));
    293     ASSERT_TRUE(IsSequenceNumberSorted(packets));
    294     for (PacketsConstIt it = packets.begin(); it != packets.end(); ++it) {
    295       EXPECT_LE(now_ms_ * 1000, (*it)->send_time_us());
    296     }
    297     EXPECT_EQ(out_packets, packets.size());
    298     accumulated_packets_.splice(accumulated_packets_.end(), packets);
    299     now_ms_ += run_for_ms;
    300   }
    301 
    302   void TestDelayFilter(int64_t delay_ms) {
    303     filter_.SetOneWayDelayMs(delay_ms);
    304     TestDelayFilter(1, 0, 0);    // No input should yield no output
    305 
    306     // Single packet
    307     TestDelayFilter(0, 1, 1);
    308     TestDelayFilter(delay_ms, 0, 0);
    309 
    310     for (int i = 0; i < delay_ms; ++i) {
    311       filter_.SetOneWayDelayMs(i);
    312       TestDelayFilter(1, 10, 10);
    313     }
    314     TestDelayFilter(0, 0, 0);
    315     TestDelayFilter(delay_ms, 0, 0);
    316 
    317     // Wait a little longer - should still see no output
    318     TestDelayFilter(delay_ms, 0, 0);
    319 
    320     for (int i = 1; i < delay_ms + 1; ++i) {
    321       filter_.SetOneWayDelayMs(i);
    322       TestDelayFilter(1, 5, 5);
    323     }
    324     TestDelayFilter(0, 0, 0);
    325     filter_.SetOneWayDelayMs(2 * delay_ms);
    326     TestDelayFilter(1, 0, 0);
    327     TestDelayFilter(delay_ms, 13, 13);
    328     TestDelayFilter(delay_ms, 0, 0);
    329 
    330     // Wait a little longer - should still see no output
    331     TestDelayFilter(delay_ms, 0, 0);
    332 
    333     for (int i = 0; i < 2 * delay_ms; ++i) {
    334       filter_.SetOneWayDelayMs(2 * delay_ms - i - 1);
    335       TestDelayFilter(1, 5, 5);
    336     }
    337     TestDelayFilter(0, 0, 0);
    338     filter_.SetOneWayDelayMs(0);
    339     TestDelayFilter(0, 7, 7);
    340 
    341     ASSERT_TRUE(IsTimeSorted(accumulated_packets_));
    342     ASSERT_TRUE(IsSequenceNumberSorted(accumulated_packets_));
    343   }
    344 
    345   DelayFilter filter_;
    346   Packets accumulated_packets_;
    347 
    348  private:
    349   int64_t now_ms_;
    350   uint16_t sequence_number_;
    351 
    352   RTC_DISALLOW_COPY_AND_ASSIGN(BweTestFramework_DelayFilterTest);
    353 };
    354 
    355 TEST_F(BweTestFramework_DelayFilterTest, Delay0) {
    356   TestDelayFilter(1, 0, 0);    // No input should yield no output
    357   TestDelayFilter(1, 10, 10);  // Expect no delay (delay time is zero)
    358   TestDelayFilter(1, 0, 0);    // Check no packets are still in buffer
    359   filter_.SetOneWayDelayMs(0);
    360   TestDelayFilter(1, 5, 5);    // Expect no delay (delay time is zero)
    361   TestDelayFilter(1, 0, 0);    // Check no packets are still in buffer
    362 }
    363 
    364 TEST_F(BweTestFramework_DelayFilterTest, Delay1) {
    365   TestDelayFilter(1);
    366 }
    367 
    368 TEST_F(BweTestFramework_DelayFilterTest, Delay2) {
    369   TestDelayFilter(2);
    370 }
    371 
    372 TEST_F(BweTestFramework_DelayFilterTest, Delay20) {
    373   TestDelayFilter(20);
    374 }
    375 
    376 TEST_F(BweTestFramework_DelayFilterTest, Delay100) {
    377   TestDelayFilter(100);
    378 }
    379 
    380 TEST_F(BweTestFramework_DelayFilterTest, JumpToZeroDelay) {
    381   DelayFilter delay(NULL, 0);
    382   Packets acc;
    383   Packets packets;
    384 
    385   // Delay a bunch of packets, accumulate them to the 'acc' list.
    386   delay.SetOneWayDelayMs(100.0f);
    387   for (uint32_t i = 0; i < 10; ++i) {
    388     packets.push_back(new MediaPacket(i * 100, i));
    389   }
    390   delay.RunFor(1000, &packets);
    391   acc.splice(acc.end(), packets);
    392   ASSERT_TRUE(IsTimeSorted(acc));
    393   ASSERT_TRUE(IsSequenceNumberSorted(acc));
    394 
    395   // Drop delay to zero, send a few more packets through the delay, append them
    396   // to the 'acc' list and verify that it is all sorted.
    397   delay.SetOneWayDelayMs(0.0f);
    398   for (uint32_t i = 10; i < 50; ++i) {
    399     packets.push_back(new MediaPacket(i * 100, i));
    400   }
    401   delay.RunFor(1000, &packets);
    402   acc.splice(acc.end(), packets);
    403   ASSERT_TRUE(IsTimeSorted(acc));
    404   ASSERT_TRUE(IsSequenceNumberSorted(acc));
    405 
    406   for (auto* packet : acc)
    407     delete packet;
    408 }
    409 
    410 TEST_F(BweTestFramework_DelayFilterTest, IncreasingDelay) {
    411   // Gradually increase delay.
    412   for (int i = 1; i < 50; i += 4) {
    413     TestDelayFilter(i);
    414   }
    415   // Reach a steady state.
    416   filter_.SetOneWayDelayMs(100);
    417   TestDelayFilter(1, 20, 20);
    418   TestDelayFilter(2, 0, 0);
    419   TestDelayFilter(99, 20, 20);
    420   // Drop delay back down to zero.
    421   filter_.SetOneWayDelayMs(0);
    422   TestDelayFilter(1, 100, 100);
    423   TestDelayFilter(23010, 0, 0);
    424   ASSERT_TRUE(IsTimeSorted(accumulated_packets_));
    425   ASSERT_TRUE(IsSequenceNumberSorted(accumulated_packets_));
    426 }
    427 
    428 static void TestJitterFilter(int64_t max_jitter_ms) {
    429   JitterFilter filter(NULL, 0);
    430   filter.SetMaxJitter(max_jitter_ms);
    431 
    432   int64_t now_ms = 0;
    433   uint16_t sequence_number = 0;
    434 
    435   // Generate packets, add jitter to them, accumulate the altered packets.
    436   Packets original;
    437   Packets jittered;
    438   for (uint32_t i = 0; i < 1000; ++i) {
    439     Packets packets;
    440     for (uint32_t j = 0; j < i % 100; ++j) {
    441       packets.push_back(new MediaPacket(now_ms * 1000, sequence_number));
    442       original.push_back(new MediaPacket(now_ms * 1000, sequence_number));
    443       ++sequence_number;
    444       now_ms += 5 * max_jitter_ms;
    445     }
    446     filter.RunFor(max_jitter_ms, &packets);
    447     jittered.splice(jittered.end(), packets);
    448   }
    449 
    450   // Jittered packets should still be in order.
    451   ASSERT_TRUE(IsTimeSorted(original));
    452   ASSERT_TRUE(IsTimeSorted(jittered));
    453   ASSERT_TRUE(IsSequenceNumberSorted(original));
    454   ASSERT_TRUE(IsSequenceNumberSorted(jittered));
    455   EXPECT_EQ(original.size(), jittered.size());
    456 
    457   // Make sure jittered and original packets are in same order. Collect time
    458   // difference (jitter) in stats, then check that mean jitter is close to zero
    459   // and standard deviation of jitter is what we set it to.
    460   Stats<double> jitter_us;
    461   int64_t max_jitter_obtained_us = 0;
    462   for (PacketsIt it1 = original.begin(), it2 = jittered.begin();
    463        it1 != original.end() && it2 != jittered.end(); ++it1, ++it2) {
    464     const MediaPacket* packet1 = static_cast<const MediaPacket*>(*it1);
    465     const MediaPacket* packet2 = static_cast<const MediaPacket*>(*it2);
    466     EXPECT_EQ(packet1->header().sequenceNumber,
    467               packet2->header().sequenceNumber);
    468     max_jitter_obtained_us =
    469         std::max(max_jitter_obtained_us,
    470                  packet2->send_time_us() - packet1->send_time_us());
    471     jitter_us.Push(packet2->send_time_us() - packet1->send_time_us());
    472   }
    473   EXPECT_NEAR(filter.MeanUs(), jitter_us.GetMean(),
    474               max_jitter_ms * 1000.0 * 0.01);
    475   EXPECT_NEAR(max_jitter_ms * 1000.0, max_jitter_obtained_us,
    476               max_jitter_ms * 1000.0 * 0.01);
    477   for (auto* packet : original)
    478     delete packet;
    479   for (auto* packet : jittered)
    480     delete packet;
    481 }
    482 
    483 TEST(BweTestFramework_JitterFilterTest, Jitter0) {
    484   TestJitterFilter(0);
    485 }
    486 
    487 TEST(BweTestFramework_JitterFilterTest, Jitter1) {
    488   TestJitterFilter(1);
    489 }
    490 
    491 TEST(BweTestFramework_JitterFilterTest, Jitter5) {
    492   TestJitterFilter(5);
    493 }
    494 
    495 TEST(BweTestFramework_JitterFilterTest, Jitter10) {
    496   TestJitterFilter(10);
    497 }
    498 
    499 TEST(BweTestFramework_JitterFilterTest, Jitter1031) {
    500   TestJitterFilter(1031);
    501 }
    502 
    503 static void TestReorderFilter(uint16_t reorder_percent) {
    504   const uint16_t kPacketCount = 10000;
    505 
    506   // Generate packets with 10 ms interval.
    507   Packets packets;
    508   int64_t now_ms = 0;
    509   uint16_t sequence_number = 1;
    510   for (uint16_t i = 0; i < kPacketCount; ++i, now_ms += 10) {
    511     packets.push_back(new MediaPacket(now_ms * 1000, sequence_number++));
    512   }
    513   ASSERT_TRUE(IsTimeSorted(packets));
    514   ASSERT_TRUE(IsSequenceNumberSorted(packets));
    515 
    516   // Reorder packets, verify that send times are still in order.
    517   ReorderFilter filter(NULL, 0);
    518   filter.SetReorder(reorder_percent);
    519   filter.RunFor(now_ms, &packets);
    520   ASSERT_TRUE(IsTimeSorted(packets));
    521 
    522   // We measure the amount of reordering by summing the distance by which out-
    523   // of-order packets have been moved in the stream.
    524   uint16_t distance = 0;
    525   uint16_t last_sequence_number = 0;
    526   for (auto* packet : packets) {
    527     const MediaPacket* media_packet = static_cast<const MediaPacket*>(packet);
    528     uint16_t sequence_number = media_packet->header().sequenceNumber;
    529     // The expected position for sequence number s is in position s-1.
    530     if (sequence_number < last_sequence_number) {
    531       distance += last_sequence_number - sequence_number;
    532     }
    533     last_sequence_number = sequence_number;
    534   }
    535 
    536   // The probability that two elements are swapped is p = reorder_percent / 100.
    537   double p = static_cast<double>(reorder_percent) / 100;
    538   // The expected number of swaps we perform is p * (PacketCount - 1),
    539   // and each swap increases the distance by one.
    540   double mean = p * (kPacketCount - 1);
    541   // If pair i is chosen to be swapped with probability p, the variance for that
    542   // pair is p * (1 - p). Since there are (kPacketCount - 1) independent pairs,
    543   // the variance for the number of swaps is (kPacketCount - 1) * p * (1 - p).
    544   double std_deviation = sqrt((kPacketCount - 1) * p * (1 - p));
    545   EXPECT_NEAR(mean, distance, 3 * std_deviation);
    546 
    547   for (auto* packet : packets)
    548     delete packet;
    549 }
    550 
    551 TEST(BweTestFramework_ReorderFilterTest, Reorder0) {
    552   // For 0% reordering, no packets should have been moved, so result is exact.
    553   TestReorderFilter(0);
    554 }
    555 
    556 TEST(BweTestFramework_ReorderFilterTest, Reorder10) {
    557   TestReorderFilter(10);
    558 }
    559 
    560 TEST(BweTestFramework_ReorderFilterTest, Reorder20) {
    561   TestReorderFilter(20);
    562 }
    563 
    564 TEST(BweTestFramework_ReorderFilterTest, Reorder50) {
    565   TestReorderFilter(50);
    566 }
    567 
    568 TEST(BweTestFramework_ReorderFilterTest, Reorder70) {
    569   TestReorderFilter(70);
    570 }
    571 
    572 TEST(BweTestFramework_ReorderFilterTest, Reorder100) {
    573   // Note that because the implementation works by optionally swapping two
    574   // adjacent packets, when the likelihood of a swap is 1.0, a swap will always
    575   // occur, so the stream will be in order except for the first packet, which
    576   // has been moved to the end. Therefore we expect the result to be exact here.
    577   TestReorderFilter(100.0);
    578 }
    579 
    580 class BweTestFramework_ChokeFilterTest : public ::testing::Test {
    581  public:
    582   BweTestFramework_ChokeFilterTest()
    583     : now_ms_(0),
    584       sequence_number_(0),
    585       output_packets_(),
    586       send_times_us_() {
    587   }
    588   virtual ~BweTestFramework_ChokeFilterTest() {
    589     for (auto* packet : output_packets_)
    590       delete packet;
    591   }
    592 
    593  protected:
    594   void TestChoke(PacketProcessor* filter,
    595                  int64_t run_for_ms,
    596                  uint32_t packets_to_generate,
    597                  size_t expected_kbit_transmitted) {
    598     // Generate a bunch of packets, apply choke, verify output is ordered.
    599     Packets packets;
    600     RTPHeader header;
    601     for (uint32_t i = 0; i < packets_to_generate; ++i) {
    602       int64_t send_time_ms = now_ms_ + (i * run_for_ms) / packets_to_generate;
    603       header.sequenceNumber = sequence_number_++;
    604       // Payload is 1000 bits.
    605       packets.push_back(new MediaPacket(0, send_time_ms * 1000, 125, header));
    606       send_times_us_.push_back(send_time_ms * 1000);
    607     }
    608     ASSERT_TRUE(IsTimeSorted(packets));
    609     filter->RunFor(run_for_ms, &packets);
    610     now_ms_ += run_for_ms;
    611     output_packets_.splice(output_packets_.end(), packets);
    612     ASSERT_TRUE(IsTimeSorted(output_packets_));
    613     ASSERT_TRUE(IsSequenceNumberSorted(output_packets_));
    614 
    615     // Sum up the transmitted bytes up until the current time.
    616     size_t bytes_transmitted = 0;
    617     while (!output_packets_.empty()) {
    618       const Packet* packet = output_packets_.front();
    619       if (packet->send_time_us() > now_ms_ * 1000) {
    620         break;
    621       }
    622       bytes_transmitted += packet->payload_size();
    623       delete output_packets_.front();
    624       output_packets_.pop_front();
    625     }
    626     EXPECT_EQ(expected_kbit_transmitted, (bytes_transmitted * 8 + 500) / 1000);
    627   }
    628 
    629   void CheckMaxDelay(int64_t max_delay_ms) {
    630     for (const auto* packet : output_packets_) {
    631       const MediaPacket* media_packet = static_cast<const MediaPacket*>(packet);
    632       int64_t delay_us = media_packet->send_time_us() -
    633                          send_times_us_[media_packet->header().sequenceNumber];
    634       EXPECT_GE(max_delay_ms * 1000, delay_us);
    635     }
    636   }
    637 
    638  private:
    639   int64_t now_ms_;
    640   uint16_t sequence_number_;
    641   Packets output_packets_;
    642   std::vector<int64_t> send_times_us_;
    643 
    644   RTC_DISALLOW_COPY_AND_ASSIGN(BweTestFramework_ChokeFilterTest);
    645 };
    646 
    647 TEST_F(BweTestFramework_ChokeFilterTest, NoQueue) {
    648   const int kCapacityKbps = 10;
    649   const size_t kPacketSizeBytes = 125;
    650   const int64_t kExpectedSendTimeUs =
    651       (kPacketSizeBytes * 8 * 1000 + kCapacityKbps / 2) / kCapacityKbps;
    652   uint16_t sequence_number = 0;
    653   int64_t send_time_us = 0;
    654   ChokeFilter filter(NULL, 0);
    655   filter.set_capacity_kbps(10);
    656   Packets packets;
    657   RTPHeader header;
    658   for (int i = 0; i < 2; ++i) {
    659     header.sequenceNumber = sequence_number++;
    660     // Payload is 1000 bits.
    661     packets.push_back(
    662         new MediaPacket(0, send_time_us, kPacketSizeBytes, header));
    663     // Packets are sent far enough a part plus an extra millisecond so that they
    664     // will never be in the choke queue at the same time.
    665     send_time_us += kExpectedSendTimeUs + 1000;
    666   }
    667   ASSERT_TRUE(IsTimeSorted(packets));
    668   filter.RunFor(2 * kExpectedSendTimeUs + 1000, &packets);
    669   EXPECT_EQ(kExpectedSendTimeUs, packets.front()->send_time_us());
    670   delete packets.front();
    671   packets.pop_front();
    672   EXPECT_EQ(2 * kExpectedSendTimeUs + 1000, packets.front()->send_time_us());
    673   delete packets.front();
    674   packets.pop_front();
    675 }
    676 
    677 TEST_F(BweTestFramework_ChokeFilterTest, Short) {
    678   // 100ms, 100 packets, 10 kbps choke -> 1 kbit of data should have propagated.
    679   // That is actually just a single packet, since each packet has 1000 bits of
    680   // payload.
    681   ChokeFilter filter(NULL, 0);
    682   filter.set_capacity_kbps(10);
    683   TestChoke(&filter, 100, 100, 1);
    684 }
    685 
    686 TEST_F(BweTestFramework_ChokeFilterTest, Medium) {
    687   // 100ms, 10 packets, 10 kbps choke -> 1 packet through, or 1 kbit.
    688   ChokeFilter filter(NULL, 0);
    689   filter.set_capacity_kbps(10);
    690   TestChoke(&filter, 100, 10, 1);
    691   // 200ms, no new packets -> another packet through.
    692   TestChoke(&filter, 100, 0, 1);
    693   // 1000ms, no new packets -> 8 more packets.
    694   TestChoke(&filter, 800, 0, 8);
    695   // 2000ms, no new packets -> queue is empty so no output.
    696   TestChoke(&filter, 1000, 0, 0);
    697 }
    698 
    699 TEST_F(BweTestFramework_ChokeFilterTest, Long) {
    700   // 100ms, 100 packets in queue, 10 kbps choke -> 1 packet through, or 1 kbit.
    701   ChokeFilter filter(NULL, 0);
    702   filter.set_capacity_kbps(10);
    703   TestChoke(&filter, 100, 100, 1);
    704   // 200ms, no input, another packet through.
    705   TestChoke(&filter, 100, 0, 1);
    706   // 1000ms, no input, 8 packets through.
    707   TestChoke(&filter, 800, 0, 8);
    708   // 10000ms, no input, raise choke to 100 kbps. Remaining 90 packets in queue
    709   // should be propagated, for a total of 90 kbps.
    710   filter.set_capacity_kbps(100);
    711   TestChoke(&filter, 9000, 0, 90);
    712   // 10100ms, 20 more packets -> 10 packets or 10 kbit through.
    713   TestChoke(&filter, 100, 20, 10);
    714   // 10300ms, 10 more packets -> 20 packets out.
    715   TestChoke(&filter, 200, 10, 20);
    716   // 11300ms, no input, queue should be empty.
    717   filter.set_capacity_kbps(10);
    718   TestChoke(&filter, 1000, 0, 0);
    719 }
    720 
    721 TEST_F(BweTestFramework_ChokeFilterTest, MaxDelay) {
    722   // 10 kbps choke, 500 ms delay cap
    723   ChokeFilter filter(NULL, 0);
    724   filter.set_capacity_kbps(10);
    725   filter.set_max_delay_ms(500);
    726   // 100ms, 100 packets in queue, 10 kbps choke -> 1 packet through, or 1 kbit.
    727   TestChoke(&filter, 100, 100, 1);
    728   CheckMaxDelay(500);
    729   // 500ms, no input, 4 more packets through.
    730   TestChoke(&filter, 400, 0, 4);
    731   // 10000ms, no input, remaining packets should have been dropped.
    732   TestChoke(&filter, 9500, 0, 0);
    733 
    734   // 100 ms delay cap
    735   filter.set_max_delay_ms(100);
    736   // 10100ms, 50 more packets -> 1 packets or 1 kbit through.
    737   TestChoke(&filter, 100, 50, 1);
    738   CheckMaxDelay(100);
    739   // 20000ms, no input, remaining packets in queue should have been dropped.
    740   TestChoke(&filter, 9900, 0, 0);
    741 
    742   // Reset delay cap (0 is no cap) and verify no packets are dropped.
    743   filter.set_capacity_kbps(10);
    744   filter.set_max_delay_ms(0);
    745   TestChoke(&filter, 100, 100, 1);
    746   TestChoke(&filter, 9900, 0, 99);
    747 }
    748 
    749 TEST_F(BweTestFramework_ChokeFilterTest, ShortTrace) {
    750   // According to the input file 6 packets should be transmitted within
    751   // 100 milliseconds.
    752   TraceBasedDeliveryFilter filter(NULL, 0);
    753   ASSERT_TRUE(filter.Init(test::ResourcePath("synthetic-trace", "rx")));
    754   TestChoke(&filter, 100, 100, 6);
    755 }
    756 
    757 TEST_F(BweTestFramework_ChokeFilterTest, ShortTraceTwoWraps) {
    758   // According to the input file 19 packets should be transmitted within
    759   // 280 milliseconds (at the wrapping point two packets are sent back to back).
    760   TraceBasedDeliveryFilter filter(NULL, 0);
    761   ASSERT_TRUE(filter.Init(test::ResourcePath("synthetic-trace", "rx")));
    762   TestChoke(&filter, 280, 100, 19);
    763 }
    764 
    765 TEST_F(BweTestFramework_ChokeFilterTest, ShortTraceMaxDelay) {
    766   TraceBasedDeliveryFilter filter(NULL, 0);
    767   filter.set_max_delay_ms(25);
    768   ASSERT_TRUE(filter.Init(test::ResourcePath("synthetic-trace", "rx")));
    769   // Uses all slots up to 110 ms. Several packets are being dropped.
    770   TestChoke(&filter, 110, 20, 9);
    771   CheckMaxDelay(25);
    772   // Simulate enough time for the next slot (at 135 ms) to be used. This makes
    773   // sure that a slot isn't missed between runs.
    774   TestChoke(&filter, 25, 1, 1);
    775 }
    776 
    777 void TestVideoSender(VideoSender* sender,
    778                      int64_t run_for_ms,
    779                      uint32_t expected_packets,
    780                      uint32_t expected_payload_size,
    781                      size_t expected_total_payload_size) {
    782   assert(sender);
    783   Packets packets;
    784   sender->RunFor(run_for_ms, &packets);
    785   ASSERT_TRUE(IsTimeSorted(packets));
    786   ASSERT_TRUE(IsSequenceNumberSorted(packets));
    787   EXPECT_EQ(expected_packets, packets.size());
    788 
    789   int64_t send_time_us = -1;
    790   size_t total_payload_size = 0;
    791   uint32_t absolute_send_time = 0;
    792   uint32_t absolute_send_time_wraps = 0;
    793   uint32_t rtp_timestamp = 0;
    794   uint32_t rtp_timestamp_wraps = 0;
    795 
    796   for (const auto* packet : packets) {
    797     const MediaPacket* media_packet = static_cast<const MediaPacket*>(packet);
    798     EXPECT_LE(send_time_us, media_packet->send_time_us());
    799     send_time_us = media_packet->send_time_us();
    800     if (sender->source()->max_payload_size_bytes() !=
    801         media_packet->payload_size()) {
    802       EXPECT_EQ(expected_payload_size, media_packet->payload_size());
    803     }
    804     total_payload_size += media_packet->payload_size();
    805     if (absolute_send_time >
    806         media_packet->header().extension.absoluteSendTime) {
    807       absolute_send_time_wraps++;
    808     }
    809     absolute_send_time = media_packet->header().extension.absoluteSendTime;
    810     if (rtp_timestamp > media_packet->header().timestamp) {
    811       rtp_timestamp_wraps++;
    812     }
    813     rtp_timestamp = media_packet->header().timestamp;
    814   }
    815 
    816   EXPECT_EQ(expected_total_payload_size, total_payload_size);
    817   EXPECT_GE(1u, absolute_send_time_wraps);
    818   EXPECT_GE(1u, rtp_timestamp_wraps);
    819 
    820   for (auto* packet : packets)
    821     delete packet;
    822 }
    823 
    824 // Random {-1, 0, +1} ms was added to frame timestamps.
    825 
    826 TEST(BweTestFramework_VideoSenderTest, Fps1Kbps80_1s) {
    827   // 1 fps, 80 kbps
    828   VideoSource source(0, 1.0f, 80, 0x1234, 0);
    829   VideoSender sender(NULL, &source, kNullEstimator);
    830   EXPECT_EQ(80000u, source.bits_per_second());
    831   // We're at 1 fps, so all packets should be generated on first call, giving 10
    832   // packets of each 1000 bytes, total 10000 bytes.
    833   TestVideoSender(&sender, 1, 9, 400, 10000);
    834   // 998ms, should see no output here.
    835   TestVideoSender(&sender, 997, 0, 0, 0);
    836   // 1001ms, should get data for one more frame.
    837   TestVideoSender(&sender, 3, 9, 400, 10000);
    838   // 1998ms, should see no output here.
    839   TestVideoSender(&sender, 997, 0, 0, 0);
    840   // 2001ms, one more frame.
    841   TestVideoSender(&sender, 3, 9, 400, 10000);
    842   // 2998ms, should see nothing.
    843   TestVideoSender(&sender, 997, 0, 0, 0);
    844 }
    845 
    846 TEST(BweTestFramework_VideoSenderTest, Fps1Kbps80_1s_Offset) {
    847   // 1 fps, 80 kbps, offset 0.5 of a frame period, ==0.5s in this case.
    848   VideoSource source(0, 1.0f, 80, 0x1234, 500);
    849   VideoSender sender(NULL, &source, kNullEstimator);
    850   EXPECT_EQ(80000u, source.bits_per_second());
    851   // 498ms, no output.
    852   TestVideoSender(&sender, 498, 0, 0, 0);
    853   // 501ms, first frame (this is the offset we set), 10 packets of 1000 bytes.
    854   TestVideoSender(&sender, 3, 9, 400, 10000);
    855   // 1498ms, nothing.
    856   TestVideoSender(&sender, 997, 0, 0, 0);
    857   // 1501ms, second frame.
    858   TestVideoSender(&sender, 3, 9, 400, 10000);
    859   // 2498ms, nothing.
    860   TestVideoSender(&sender, 997, 0, 0, 0);
    861   // 2501ms, third frame.
    862   TestVideoSender(&sender, 3, 9, 400, 10000);
    863   // 3498ms, nothing.
    864   TestVideoSender(&sender, 997, 0, 0, 0);
    865 }
    866 
    867 TEST(BweTestFramework_VideoSenderTest, Fps50Kpbs80_11s) {
    868   // 50 fps, 80 kbps.
    869   VideoSource source(0, 50.0f, 80, 0x1234, 0);
    870   VideoSender sender(NULL, &source, kNullEstimator);
    871   EXPECT_EQ(80000u, source.bits_per_second());
    872   // 9981, should see 500 frames, 200 byte payloads, total 100000 bytes.
    873   TestVideoSender(&sender, 9981, 500, 200, 100000);
    874   // 9998ms, nothing.
    875   TestVideoSender(&sender, 17, 0, 0, 0);
    876   // 10001ms, 501st frame as a single packet.
    877   TestVideoSender(&sender, 3, 1, 200, 200);
    878   // 10981ms, 49 more frames.
    879   TestVideoSender(&sender, 981, 49, 200, 9800);
    880   // 10998ms, nothing.
    881   TestVideoSender(&sender, 17, 0, 0, 0);
    882 }
    883 
    884 TEST(BweTestFramework_VideoSenderTest, Fps20Kpbs120_1s) {
    885   // 20 fps, 120 kbps.
    886   VideoSource source(0, 20.0f, 120, 0x1234, 0);
    887   VideoSender sender(NULL, &source, kNullEstimator);
    888   EXPECT_EQ(120000u, source.bits_per_second());
    889   // 451ms, 10 frames with 750 byte payloads, total 7500 bytes.
    890   TestVideoSender(&sender, 451, 10, 750, 7500);
    891   // 498ms, nothing.
    892   TestVideoSender(&sender, 47, 0, 0, 0);
    893   // 501ms, one more frame.
    894   TestVideoSender(&sender, 3, 1, 750, 750);
    895   // 951ms, 9 more frames.
    896   TestVideoSender(&sender, 450, 9, 750, 6750);
    897   // 998ms, nothing.
    898   TestVideoSender(&sender, 47, 0, 0, 0);
    899 }
    900 
    901 TEST(BweTestFramework_VideoSenderTest, Fps25Kbps820_20s) {
    902   // 25 fps, 820 kbps.
    903   VideoSource source(0, 25.0f, 820, 0x1234, 0);
    904   VideoSender sender(NULL, &source, kNullEstimator);
    905   EXPECT_EQ(820000u, source.bits_per_second());
    906   // 9961ms, 250 frames. 820 kbps = 102500 bytes/s, so total should be 1025000.
    907   // Each frame is 102500/25=4100 bytes, or 5 packets (4 @1000 bytes, 1 @100),
    908   // so packet count should be 5*250=1250 and last packet of each frame has
    909   // 100 bytes of payload.
    910   TestVideoSender(&sender, 9961, 1000, 500, 1025000);
    911   // 9998ms, nothing.
    912   TestVideoSender(&sender, 37, 0, 0, 0);
    913   // 19961ms, 250 more frames.
    914   TestVideoSender(&sender, 9963, 1000, 500, 1025000);
    915   // 19998ms, nothing.
    916   TestVideoSender(&sender, 37, 0, 0, 0);
    917   // 20001ms, one more frame, as described above (25fps == 40ms/frame).
    918   TestVideoSender(&sender, 3, 4, 500, 4100);
    919   // 20038ms, nothing.
    920   TestVideoSender(&sender, 37, 0, 0, 0);
    921 }
    922 
    923 TEST(BweTestFramework_VideoSenderTest, TestAppendInOrder) {
    924   // 1 fps, 80 kbps, 250ms offset.
    925   VideoSource source1(0, 1.0f, 80, 0x1234, 250);
    926   VideoSender sender1(NULL, &source1, kNullEstimator);
    927   EXPECT_EQ(80000u, source1.bits_per_second());
    928   Packets packets;
    929   // Generate some packets, verify they are sorted.
    930   sender1.RunFor(999, &packets);
    931   ASSERT_TRUE(IsTimeSorted(packets));
    932   ASSERT_TRUE(IsSequenceNumberSorted(packets));
    933   EXPECT_EQ(9u, packets.size());
    934   // Generate some more packets and verify they are appended to end of list.
    935   sender1.RunFor(1000, &packets);
    936   ASSERT_TRUE(IsTimeSorted(packets));
    937   ASSERT_TRUE(IsSequenceNumberSorted(packets));
    938   EXPECT_EQ(18u, packets.size());
    939 
    940   // Another sender, 2 fps, 160 kbps, 150ms offset
    941   VideoSource source2(0, 2.0f, 160, 0x2234, 150);
    942   VideoSender sender2(NULL, &source2, kNullEstimator);
    943   EXPECT_EQ(160000u, source2.bits_per_second());
    944   // Generate some packets, verify that they are merged with the packets already
    945   // on the list.
    946   sender2.RunFor(999, &packets);
    947   ASSERT_TRUE(IsTimeSorted(packets));
    948   EXPECT_EQ(36u, packets.size());
    949   // Generate some more.
    950   sender2.RunFor(1000, &packets);
    951   ASSERT_TRUE(IsTimeSorted(packets));
    952   EXPECT_EQ(54u, packets.size());
    953 
    954   for (auto* packet : packets)
    955     delete packet;
    956 }
    957 
    958 TEST(BweTestFramework_VideoSenderTest, FeedbackIneffective) {
    959   VideoSource source(0, 25.0f, 820, 0x1234, 0);
    960   VideoSender sender(NULL, &source, kNullEstimator);
    961 
    962   EXPECT_EQ(820000u, source.bits_per_second());
    963   TestVideoSender(&sender, 9961, 1000, 500, 1025000);
    964 
    965   // Make sure feedback has no effect on a regular video sender.
    966   RembFeedback* feedback = new RembFeedback(0, 0, 0, 512000, RTCPReportBlock());
    967   Packets packets;
    968   packets.push_back(feedback);
    969   sender.RunFor(0, &packets);
    970   EXPECT_EQ(820000u, source.bits_per_second());
    971   TestVideoSender(&sender, 10000, 1000, 500, 1025000);
    972 }
    973 
    974 TEST(BweTestFramework_AdaptiveVideoSenderTest, FeedbackChangesBitrate) {
    975   AdaptiveVideoSource source(0, 25.0f, 820, 0x1234, 0);
    976   VideoSender sender(NULL, &source, kRembEstimator);
    977   EXPECT_EQ(820000u, source.bits_per_second());
    978   TestVideoSender(&sender, 9961, 1000, 500, 1025000);
    979 
    980   // Make sure we can reduce the bitrate.
    981   RembFeedback* feedback = new RembFeedback(0, 0, 0, 512000, RTCPReportBlock());
    982   Packets packets;
    983   packets.push_back(feedback);
    984   sender.RunFor(0, &packets);
    985   EXPECT_EQ(512000u, source.bits_per_second());
    986   TestVideoSender(&sender, 10000, 750, 160, 640000);
    987 
    988   // Increase the bitrate to the initial bitrate and verify that the output is
    989   // the same.
    990   feedback = new RembFeedback(0, 0, 0, 820000, RTCPReportBlock());
    991   packets.push_back(feedback);
    992   sender.RunFor(10000, &packets);
    993   EXPECT_EQ(820000u, source.bits_per_second());
    994 
    995   for (auto* packet : packets)
    996     delete packet;
    997 }
    998 
    999 TEST(BweTestFramework_AdaptiveVideoSenderTest, Paced_FeedbackChangesBitrate) {
   1000   AdaptiveVideoSource source(0, 25.0f, 820, 0x1234, 0);
   1001   PacedVideoSender sender(NULL, &source, kRembEstimator);
   1002   EXPECT_EQ(820000u, source.bits_per_second());
   1003   TestVideoSender(&sender, 9998, 1000, 500, 1025000);
   1004 
   1005   // Make sure we can reduce the bitrate.
   1006   RembFeedback* feedback = new RembFeedback(0, 1, 0, 512000, RTCPReportBlock());
   1007   Packets packets;
   1008   packets.push_back(feedback);
   1009   sender.RunFor(10000, &packets);
   1010   ASSERT_EQ(512000u, source.bits_per_second());
   1011   TestVideoSender(&sender, 10000, 750, 160, 640000);
   1012 
   1013   // Increase the bitrate to the initial bitrate and verify that the output is
   1014   // the same.
   1015   feedback = new RembFeedback(0, 0, 0, 820000, RTCPReportBlock());
   1016   packets.push_back(feedback);
   1017   sender.RunFor(10000, &packets);
   1018   EXPECT_EQ(820000u, source.bits_per_second());
   1019 
   1020   for (auto* packet : packets)
   1021     delete packet;
   1022 }
   1023 }  // namespace bwe
   1024 }  // namespace testing
   1025 }  // namespace webrtc
   1026