Home | History | Annotate | Download | only in pacing
      1 /*
      2  *  Copyright (c) 2012 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 "testing/gmock/include/gmock/gmock.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 
     14 #include "webrtc/modules/pacing/include/paced_sender.h"
     15 #include "webrtc/system_wrappers/interface/clock.h"
     16 
     17 using testing::_;
     18 using testing::Return;
     19 
     20 namespace webrtc {
     21 namespace test {
     22 
     23 static const int kTargetBitrate = 800;
     24 static const float kPaceMultiplier = 1.5f;
     25 
     26 class MockPacedSenderCallback : public PacedSender::Callback {
     27  public:
     28   MOCK_METHOD4(TimeToSendPacket,
     29       bool(uint32_t ssrc, uint16_t sequence_number, int64_t capture_time_ms,
     30            bool retransmission));
     31   MOCK_METHOD1(TimeToSendPadding,
     32       int(int bytes));
     33 };
     34 
     35 class PacedSenderPadding : public PacedSender::Callback {
     36  public:
     37   PacedSenderPadding() : padding_sent_(0) {}
     38 
     39   bool TimeToSendPacket(uint32_t ssrc, uint16_t sequence_number,
     40                         int64_t capture_time_ms, bool retransmission) {
     41     return true;
     42   }
     43 
     44   int TimeToSendPadding(int bytes) {
     45     const int kPaddingPacketSize = 224;
     46     int num_packets = (bytes + kPaddingPacketSize - 1) / kPaddingPacketSize;
     47     padding_sent_ += kPaddingPacketSize * num_packets;
     48     return kPaddingPacketSize * num_packets;
     49   }
     50 
     51   int padding_sent() { return padding_sent_; }
     52 
     53  private:
     54   int padding_sent_;
     55 };
     56 
     57 class PacedSenderTest : public ::testing::Test {
     58  protected:
     59   PacedSenderTest() : clock_(123456) {
     60     srand(0);
     61     // Need to initialize PacedSender after we initialize clock.
     62     send_bucket_.reset(
     63         new PacedSender(
     64             &clock_, &callback_, kPaceMultiplier * kTargetBitrate, 0));
     65   }
     66 
     67   void SendAndExpectPacket(PacedSender::Priority priority,
     68                            uint32_t ssrc, uint16_t sequence_number,
     69                            int64_t capture_time_ms, int size,
     70                            bool retransmission) {
     71     EXPECT_FALSE(send_bucket_->SendPacket(priority, ssrc,
     72         sequence_number, capture_time_ms, size, retransmission));
     73     EXPECT_CALL(callback_, TimeToSendPacket(
     74         ssrc, sequence_number, capture_time_ms, false))
     75         .Times(1)
     76         .WillRepeatedly(Return(true));
     77   }
     78 
     79   SimulatedClock clock_;
     80   MockPacedSenderCallback callback_;
     81   scoped_ptr<PacedSender> send_bucket_;
     82 };
     83 
     84 TEST_F(PacedSenderTest, QueuePacket) {
     85   uint32_t ssrc = 12345;
     86   uint16_t sequence_number = 1234;
     87   // Due to the multiplicative factor we can send 3 packets not 2 packets.
     88   SendAndExpectPacket(PacedSender::kNormalPriority, ssrc, sequence_number++,
     89                       clock_.TimeInMilliseconds(), 250, false);
     90   SendAndExpectPacket(PacedSender::kNormalPriority, ssrc, sequence_number++,
     91                       clock_.TimeInMilliseconds(), 250, false);
     92   SendAndExpectPacket(PacedSender::kNormalPriority, ssrc, sequence_number++,
     93                       clock_.TimeInMilliseconds(), 250, false);
     94   int64_t queued_packet_timestamp = clock_.TimeInMilliseconds();
     95   EXPECT_FALSE(send_bucket_->SendPacket(PacedSender::kNormalPriority, ssrc,
     96       sequence_number, queued_packet_timestamp, 250, false));
     97   send_bucket_->Process();
     98   EXPECT_EQ(5, send_bucket_->TimeUntilNextProcess());
     99   EXPECT_CALL(callback_, TimeToSendPadding(_)).Times(0);
    100   clock_.AdvanceTimeMilliseconds(4);
    101   EXPECT_EQ(1, send_bucket_->TimeUntilNextProcess());
    102   clock_.AdvanceTimeMilliseconds(1);
    103   EXPECT_EQ(0, send_bucket_->TimeUntilNextProcess());
    104   EXPECT_CALL(callback_, TimeToSendPacket(
    105       ssrc, sequence_number++, queued_packet_timestamp, false))
    106       .Times(1)
    107       .WillRepeatedly(Return(true));
    108   send_bucket_->Process();
    109   sequence_number++;
    110   SendAndExpectPacket(PacedSender::kNormalPriority, ssrc, sequence_number++,
    111                       clock_.TimeInMilliseconds(), 250, false);
    112   SendAndExpectPacket(PacedSender::kNormalPriority, ssrc, sequence_number++,
    113                       clock_.TimeInMilliseconds(), 250, false);
    114   EXPECT_FALSE(send_bucket_->SendPacket(PacedSender::kNormalPriority, ssrc,
    115       sequence_number++, clock_.TimeInMilliseconds(), 250, false));
    116   send_bucket_->Process();
    117 }
    118 
    119 TEST_F(PacedSenderTest, PaceQueuedPackets) {
    120   uint32_t ssrc = 12345;
    121   uint16_t sequence_number = 1234;
    122 
    123   // Due to the multiplicative factor we can send 3 packets not 2 packets.
    124   for (int i = 0; i < 3; ++i) {
    125     SendAndExpectPacket(PacedSender::kNormalPriority, ssrc, sequence_number++,
    126                         clock_.TimeInMilliseconds(), 250, false);
    127   }
    128   for (int j = 0; j < 30; ++j) {
    129     EXPECT_FALSE(send_bucket_->SendPacket(PacedSender::kNormalPriority, ssrc,
    130         sequence_number++, clock_.TimeInMilliseconds(), 250, false));
    131   }
    132   send_bucket_->Process();
    133   EXPECT_CALL(callback_, TimeToSendPadding(_)).Times(0);
    134   for (int k = 0; k < 10; ++k) {
    135     EXPECT_EQ(5, send_bucket_->TimeUntilNextProcess());
    136     clock_.AdvanceTimeMilliseconds(5);
    137     EXPECT_CALL(callback_,
    138         TimeToSendPacket(ssrc, _, _, false))
    139         .Times(3)
    140         .WillRepeatedly(Return(true));
    141     EXPECT_EQ(0, send_bucket_->TimeUntilNextProcess());
    142     EXPECT_EQ(0, send_bucket_->Process());
    143   }
    144   EXPECT_EQ(5, send_bucket_->TimeUntilNextProcess());
    145   clock_.AdvanceTimeMilliseconds(5);
    146   EXPECT_EQ(0, send_bucket_->TimeUntilNextProcess());
    147   EXPECT_EQ(0, send_bucket_->Process());
    148   SendAndExpectPacket(PacedSender::kNormalPriority, ssrc, sequence_number++,
    149                       clock_.TimeInMilliseconds(), 250, false);
    150   SendAndExpectPacket(PacedSender::kNormalPriority, ssrc, sequence_number++,
    151                       clock_.TimeInMilliseconds(), 250, false);
    152   SendAndExpectPacket(PacedSender::kNormalPriority, ssrc, sequence_number++,
    153                       clock_.TimeInMilliseconds(), 250, false);
    154   EXPECT_FALSE(send_bucket_->SendPacket(PacedSender::kNormalPriority, ssrc,
    155       sequence_number, clock_.TimeInMilliseconds(), 250, false));
    156   send_bucket_->Process();
    157 }
    158 
    159 TEST_F(PacedSenderTest, PaceQueuedPacketsWithDuplicates) {
    160   uint32_t ssrc = 12345;
    161   uint16_t sequence_number = 1234;
    162   uint16_t queued_sequence_number;
    163 
    164   // Due to the multiplicative factor we can send 3 packets not 2 packets.
    165   for (int i = 0; i < 3; ++i) {
    166     SendAndExpectPacket(PacedSender::kNormalPriority, ssrc, sequence_number++,
    167                         clock_.TimeInMilliseconds(), 250, false);
    168   }
    169   queued_sequence_number = sequence_number;
    170 
    171   for (int j = 0; j < 30; ++j) {
    172     // Send in duplicate packets.
    173     EXPECT_FALSE(send_bucket_->SendPacket(PacedSender::kNormalPriority, ssrc,
    174         sequence_number, clock_.TimeInMilliseconds(), 250, false));
    175     EXPECT_FALSE(send_bucket_->SendPacket(PacedSender::kNormalPriority, ssrc,
    176         sequence_number++, clock_.TimeInMilliseconds(), 250, false));
    177   }
    178   EXPECT_CALL(callback_, TimeToSendPadding(_)).Times(0);
    179   send_bucket_->Process();
    180   for (int k = 0; k < 10; ++k) {
    181     EXPECT_EQ(5, send_bucket_->TimeUntilNextProcess());
    182     clock_.AdvanceTimeMilliseconds(5);
    183 
    184     for (int i = 0; i < 3; ++i) {
    185       EXPECT_CALL(callback_, TimeToSendPacket(ssrc, queued_sequence_number++,
    186                                               _,
    187                                               false))
    188           .Times(1)
    189           .WillRepeatedly(Return(true));
    190    }
    191     EXPECT_EQ(0, send_bucket_->TimeUntilNextProcess());
    192     EXPECT_EQ(0, send_bucket_->Process());
    193   }
    194   EXPECT_EQ(5, send_bucket_->TimeUntilNextProcess());
    195   clock_.AdvanceTimeMilliseconds(5);
    196   EXPECT_EQ(0, send_bucket_->TimeUntilNextProcess());
    197   EXPECT_EQ(0, send_bucket_->Process());
    198   SendAndExpectPacket(PacedSender::kNormalPriority, ssrc, sequence_number++,
    199                       clock_.TimeInMilliseconds(), 250, false);
    200   SendAndExpectPacket(PacedSender::kNormalPriority, ssrc, sequence_number++,
    201                       clock_.TimeInMilliseconds(), 250, false);
    202   SendAndExpectPacket(PacedSender::kNormalPriority, ssrc, sequence_number++,
    203                       clock_.TimeInMilliseconds(), 250, false);
    204   EXPECT_FALSE(send_bucket_->SendPacket(PacedSender::kNormalPriority, ssrc,
    205       sequence_number++, clock_.TimeInMilliseconds(), 250, false));
    206   send_bucket_->Process();
    207 }
    208 
    209 TEST_F(PacedSenderTest, CanQueuePacketsWithSameSequenceNumberOnDifferentSsrcs) {
    210   uint32_t ssrc = 12345;
    211   uint16_t sequence_number = 1234;
    212 
    213   SendAndExpectPacket(PacedSender::kNormalPriority,
    214                       ssrc,
    215                       sequence_number,
    216                       clock_.TimeInMilliseconds(),
    217                       250,
    218                       false);
    219 
    220   // Expect packet on second ssrc to be queued and sent as well.
    221   SendAndExpectPacket(PacedSender::kNormalPriority,
    222                       ssrc + 1,
    223                       sequence_number,
    224                       clock_.TimeInMilliseconds(),
    225                       250,
    226                       false);
    227 
    228   clock_.AdvanceTimeMilliseconds(1000);
    229   send_bucket_->Process();
    230 }
    231 
    232 TEST_F(PacedSenderTest, Padding) {
    233   uint32_t ssrc = 12345;
    234   uint16_t sequence_number = 1234;
    235 
    236   send_bucket_->UpdateBitrate(kPaceMultiplier * kTargetBitrate, kTargetBitrate);
    237   // Due to the multiplicative factor we can send 3 packets not 2 packets.
    238   SendAndExpectPacket(PacedSender::kNormalPriority, ssrc, sequence_number++,
    239                       clock_.TimeInMilliseconds(), 250, false);
    240   SendAndExpectPacket(PacedSender::kNormalPriority, ssrc, sequence_number++,
    241                       clock_.TimeInMilliseconds(), 250, false);
    242   SendAndExpectPacket(PacedSender::kNormalPriority, ssrc, sequence_number++,
    243                       clock_.TimeInMilliseconds(), 250, false);
    244   // No padding is expected since we have sent too much already.
    245   EXPECT_CALL(callback_, TimeToSendPadding(_)).Times(0);
    246   EXPECT_EQ(5, send_bucket_->TimeUntilNextProcess());
    247   clock_.AdvanceTimeMilliseconds(5);
    248   EXPECT_EQ(0, send_bucket_->TimeUntilNextProcess());
    249   EXPECT_EQ(0, send_bucket_->Process());
    250 
    251   // 5 milliseconds later we have enough budget to send some padding.
    252   EXPECT_CALL(callback_, TimeToSendPadding(250)).Times(1).
    253       WillOnce(Return(250));
    254   EXPECT_EQ(5, send_bucket_->TimeUntilNextProcess());
    255   clock_.AdvanceTimeMilliseconds(5);
    256   EXPECT_EQ(0, send_bucket_->TimeUntilNextProcess());
    257   EXPECT_EQ(0, send_bucket_->Process());
    258 }
    259 
    260 TEST_F(PacedSenderTest, NoPaddingWhenDisabled) {
    261   send_bucket_->SetStatus(false);
    262   send_bucket_->UpdateBitrate(kPaceMultiplier * kTargetBitrate, kTargetBitrate);
    263   // No padding is expected since the pacer is disabled.
    264   EXPECT_CALL(callback_, TimeToSendPadding(_)).Times(0);
    265   EXPECT_EQ(5, send_bucket_->TimeUntilNextProcess());
    266   clock_.AdvanceTimeMilliseconds(5);
    267   EXPECT_EQ(0, send_bucket_->TimeUntilNextProcess());
    268   EXPECT_EQ(0, send_bucket_->Process());
    269   EXPECT_CALL(callback_, TimeToSendPadding(_)).Times(0);
    270   EXPECT_EQ(5, send_bucket_->TimeUntilNextProcess());
    271   clock_.AdvanceTimeMilliseconds(5);
    272   EXPECT_EQ(0, send_bucket_->TimeUntilNextProcess());
    273   EXPECT_EQ(0, send_bucket_->Process());
    274 }
    275 
    276 TEST_F(PacedSenderTest, VerifyPaddingUpToBitrate) {
    277   uint32_t ssrc = 12345;
    278   uint16_t sequence_number = 1234;
    279   int64_t capture_time_ms = 56789;
    280   const int kTimeStep = 5;
    281   const int64_t kBitrateWindow = 100;
    282   send_bucket_->UpdateBitrate(kPaceMultiplier * kTargetBitrate, kTargetBitrate);
    283   int64_t start_time = clock_.TimeInMilliseconds();
    284   while (clock_.TimeInMilliseconds() - start_time < kBitrateWindow) {
    285     SendAndExpectPacket(PacedSender::kNormalPriority, ssrc, sequence_number++,
    286                         capture_time_ms, 250, false);
    287     clock_.AdvanceTimeMilliseconds(kTimeStep);
    288     EXPECT_CALL(callback_, TimeToSendPadding(250)).Times(1).
    289         WillOnce(Return(250));
    290     send_bucket_->Process();
    291   }
    292 }
    293 
    294 TEST_F(PacedSenderTest, VerifyAverageBitrateVaryingMediaPayload) {
    295   uint32_t ssrc = 12345;
    296   uint16_t sequence_number = 1234;
    297   int64_t capture_time_ms = 56789;
    298   const int kTimeStep = 5;
    299   const int64_t kBitrateWindow = 10000;
    300   PacedSenderPadding callback;
    301   send_bucket_.reset(
    302       new PacedSender(&clock_, &callback, kPaceMultiplier * kTargetBitrate, 0));
    303   send_bucket_->UpdateBitrate(kPaceMultiplier * kTargetBitrate, kTargetBitrate);
    304   int64_t start_time = clock_.TimeInMilliseconds();
    305   int media_bytes = 0;
    306   while (clock_.TimeInMilliseconds() - start_time < kBitrateWindow) {
    307     int media_payload = rand() % 100 + 200;  // [200, 300] bytes.
    308     EXPECT_FALSE(send_bucket_->SendPacket(PacedSender::kNormalPriority, ssrc,
    309                                           sequence_number++, capture_time_ms,
    310                                           media_payload, false));
    311     media_bytes += media_payload;
    312     clock_.AdvanceTimeMilliseconds(kTimeStep);
    313     send_bucket_->Process();
    314   }
    315   EXPECT_NEAR(kTargetBitrate, 8 * (media_bytes + callback.padding_sent()) /
    316               kBitrateWindow, 1);
    317 }
    318 
    319 TEST_F(PacedSenderTest, Priority) {
    320   uint32_t ssrc_low_priority = 12345;
    321   uint32_t ssrc = 12346;
    322   uint16_t sequence_number = 1234;
    323   int64_t capture_time_ms = 56789;
    324   int64_t capture_time_ms_low_priority = 1234567;
    325 
    326   // Due to the multiplicative factor we can send 3 packets not 2 packets.
    327   SendAndExpectPacket(PacedSender::kLowPriority, ssrc, sequence_number++,
    328                       capture_time_ms, 250, false);
    329   SendAndExpectPacket(PacedSender::kNormalPriority, ssrc, sequence_number++,
    330                       capture_time_ms, 250, false);
    331   SendAndExpectPacket(PacedSender::kNormalPriority, ssrc, sequence_number++,
    332                       capture_time_ms, 250, false);
    333   send_bucket_->Process();
    334 
    335   // Expect normal and low priority to be queued and high to pass through.
    336   EXPECT_FALSE(send_bucket_->SendPacket(PacedSender::kLowPriority,
    337       ssrc_low_priority, sequence_number++, capture_time_ms_low_priority, 250,
    338       false));
    339   EXPECT_FALSE(send_bucket_->SendPacket(PacedSender::kNormalPriority,
    340       ssrc, sequence_number++, capture_time_ms, 250, false));
    341   EXPECT_FALSE(send_bucket_->SendPacket(PacedSender::kNormalPriority,
    342       ssrc, sequence_number++, capture_time_ms, 250, false));
    343   EXPECT_FALSE(send_bucket_->SendPacket(PacedSender::kHighPriority,
    344       ssrc, sequence_number++, capture_time_ms, 250, false));
    345 
    346   // Expect all high and normal priority to be sent out first.
    347   EXPECT_CALL(callback_, TimeToSendPadding(_)).Times(0);
    348   EXPECT_CALL(callback_, TimeToSendPacket(ssrc, _, capture_time_ms, false))
    349       .Times(3)
    350       .WillRepeatedly(Return(true));
    351 
    352   EXPECT_EQ(5, send_bucket_->TimeUntilNextProcess());
    353   clock_.AdvanceTimeMilliseconds(5);
    354   EXPECT_EQ(0, send_bucket_->TimeUntilNextProcess());
    355   EXPECT_EQ(0, send_bucket_->Process());
    356 
    357   EXPECT_CALL(callback_, TimeToSendPacket(
    358       ssrc_low_priority, _, capture_time_ms_low_priority, false))
    359       .Times(1)
    360       .WillRepeatedly(Return(true));
    361 
    362   EXPECT_EQ(5, send_bucket_->TimeUntilNextProcess());
    363   clock_.AdvanceTimeMilliseconds(5);
    364   EXPECT_EQ(0, send_bucket_->TimeUntilNextProcess());
    365   EXPECT_EQ(0, send_bucket_->Process());
    366 }
    367 
    368 TEST_F(PacedSenderTest, Pause) {
    369   uint32_t ssrc_low_priority = 12345;
    370   uint32_t ssrc = 12346;
    371   uint16_t sequence_number = 1234;
    372   int64_t capture_time_ms = clock_.TimeInMilliseconds();
    373 
    374   EXPECT_EQ(0, send_bucket_->QueueInMs());
    375 
    376   // Due to the multiplicative factor we can send 3 packets not 2 packets.
    377   SendAndExpectPacket(PacedSender::kLowPriority, ssrc, sequence_number++,
    378                       capture_time_ms, 250, false);
    379   SendAndExpectPacket(PacedSender::kNormalPriority, ssrc, sequence_number++,
    380                       capture_time_ms, 250, false);
    381   SendAndExpectPacket(PacedSender::kNormalPriority, ssrc, sequence_number++,
    382                       capture_time_ms, 250, false);
    383   send_bucket_->Process();
    384 
    385   send_bucket_->Pause();
    386 
    387   EXPECT_FALSE(send_bucket_->SendPacket(PacedSender::kNormalPriority,
    388       ssrc, sequence_number++, capture_time_ms, 250, false));
    389   EXPECT_FALSE(send_bucket_->SendPacket(PacedSender::kNormalPriority,
    390       ssrc, sequence_number++, capture_time_ms, 250, false));
    391   EXPECT_FALSE(send_bucket_->SendPacket(PacedSender::kHighPriority,
    392       ssrc, sequence_number++, capture_time_ms, 250, false));
    393 
    394   clock_.AdvanceTimeMilliseconds(10000);
    395   int64_t second_capture_time_ms = clock_.TimeInMilliseconds();
    396 
    397   // Expect everything to be queued.
    398   EXPECT_FALSE(send_bucket_->SendPacket(PacedSender::kLowPriority,
    399       ssrc_low_priority, sequence_number++, second_capture_time_ms, 250,
    400       false));
    401 
    402   EXPECT_EQ(clock_.TimeInMilliseconds() - capture_time_ms,
    403             send_bucket_->QueueInMs());
    404 
    405   // Expect no packet to come out while paused.
    406   EXPECT_CALL(callback_, TimeToSendPadding(_)).Times(0);
    407   EXPECT_CALL(callback_, TimeToSendPacket(_, _, _, _)).Times(0);
    408 
    409   for (int i = 0; i < 10; ++i) {
    410     clock_.AdvanceTimeMilliseconds(5);
    411     EXPECT_EQ(0, send_bucket_->TimeUntilNextProcess());
    412     EXPECT_EQ(0, send_bucket_->Process());
    413   }
    414   // Expect high prio packets to come out first followed by all packets in the
    415   // way they were added.
    416   EXPECT_CALL(callback_, TimeToSendPacket(_, _, capture_time_ms, false))
    417       .Times(3)
    418       .WillRepeatedly(Return(true));
    419   send_bucket_->Resume();
    420 
    421   EXPECT_EQ(5, send_bucket_->TimeUntilNextProcess());
    422   clock_.AdvanceTimeMilliseconds(5);
    423   EXPECT_EQ(0, send_bucket_->TimeUntilNextProcess());
    424   EXPECT_EQ(0, send_bucket_->Process());
    425 
    426   EXPECT_CALL(
    427       callback_, TimeToSendPacket(_, _, second_capture_time_ms, false))
    428       .Times(1)
    429       .WillRepeatedly(Return(true));
    430   EXPECT_EQ(5, send_bucket_->TimeUntilNextProcess());
    431   clock_.AdvanceTimeMilliseconds(5);
    432   EXPECT_EQ(0, send_bucket_->TimeUntilNextProcess());
    433   EXPECT_EQ(0, send_bucket_->Process());
    434   EXPECT_EQ(0, send_bucket_->QueueInMs());
    435 }
    436 
    437 TEST_F(PacedSenderTest, ResendPacket) {
    438   uint32_t ssrc = 12346;
    439   uint16_t sequence_number = 1234;
    440   int64_t capture_time_ms = clock_.TimeInMilliseconds();
    441   EXPECT_EQ(0, send_bucket_->QueueInMs());
    442 
    443   EXPECT_FALSE(send_bucket_->SendPacket(PacedSender::kNormalPriority,
    444                                         ssrc,
    445                                         sequence_number,
    446                                         capture_time_ms,
    447                                         250,
    448                                         false));
    449   clock_.AdvanceTimeMilliseconds(1);
    450   EXPECT_FALSE(send_bucket_->SendPacket(PacedSender::kNormalPriority,
    451                                         ssrc,
    452                                         sequence_number + 1,
    453                                         capture_time_ms + 1,
    454                                         250,
    455                                         false));
    456   clock_.AdvanceTimeMilliseconds(9999);
    457   EXPECT_EQ(clock_.TimeInMilliseconds() - capture_time_ms,
    458             send_bucket_->QueueInMs());
    459   // Fails to send first packet so only one call.
    460   EXPECT_CALL(callback_, TimeToSendPacket(
    461       ssrc, sequence_number, capture_time_ms, false))
    462       .Times(1)
    463       .WillOnce(Return(false));
    464   clock_.AdvanceTimeMilliseconds(10000);
    465   send_bucket_->Process();
    466 
    467   // Queue remains unchanged.
    468   EXPECT_EQ(clock_.TimeInMilliseconds() - capture_time_ms,
    469             send_bucket_->QueueInMs());
    470 
    471   // Fails to send second packet.
    472   EXPECT_CALL(callback_, TimeToSendPacket(
    473       ssrc, sequence_number, capture_time_ms, false))
    474       .Times(1)
    475       .WillOnce(Return(true));
    476   EXPECT_CALL(callback_, TimeToSendPacket(
    477       ssrc, sequence_number + 1, capture_time_ms + 1, false))
    478       .Times(1)
    479       .WillOnce(Return(false));
    480   clock_.AdvanceTimeMilliseconds(10000);
    481   send_bucket_->Process();
    482 
    483   // Queue is reduced by 1 packet.
    484   EXPECT_EQ(clock_.TimeInMilliseconds() - capture_time_ms - 1,
    485             send_bucket_->QueueInMs());
    486 
    487   // Send second packet and queue becomes empty.
    488   EXPECT_CALL(callback_, TimeToSendPacket(
    489       ssrc, sequence_number + 1, capture_time_ms + 1, false))
    490       .Times(1)
    491       .WillOnce(Return(true));
    492   clock_.AdvanceTimeMilliseconds(10000);
    493   send_bucket_->Process();
    494   EXPECT_EQ(0, send_bucket_->QueueInMs());
    495 }
    496 
    497 TEST_F(PacedSenderTest, MaxQueueLength) {
    498   uint32_t ssrc = 12346;
    499   uint16_t sequence_number = 1234;
    500   EXPECT_EQ(0, send_bucket_->QueueInMs());
    501 
    502   send_bucket_->UpdateBitrate(kPaceMultiplier * 30, 0);
    503   for (int i = 0; i < 30; ++i) {
    504     SendAndExpectPacket(PacedSender::kNormalPriority,
    505                         ssrc,
    506                         sequence_number++,
    507                         clock_.TimeInMilliseconds(),
    508                         1200,
    509                         false);
    510   }
    511 
    512   clock_.AdvanceTimeMilliseconds(2001);
    513   SendAndExpectPacket(PacedSender::kNormalPriority,
    514                       ssrc,
    515                       sequence_number++,
    516                       clock_.TimeInMilliseconds(),
    517                       1200,
    518                       false);
    519   EXPECT_EQ(2001, send_bucket_->QueueInMs());
    520   send_bucket_->Process();
    521   EXPECT_EQ(0, send_bucket_->QueueInMs());
    522   clock_.AdvanceTimeMilliseconds(31);
    523 
    524   send_bucket_->Process();
    525 }
    526 
    527 TEST_F(PacedSenderTest, QueueTimeGrowsOverTime) {
    528   uint32_t ssrc = 12346;
    529   uint16_t sequence_number = 1234;
    530   EXPECT_EQ(0, send_bucket_->QueueInMs());
    531 
    532   send_bucket_->UpdateBitrate(kPaceMultiplier * 30, 0);
    533   SendAndExpectPacket(PacedSender::kNormalPriority,
    534                       ssrc,
    535                       sequence_number,
    536                       clock_.TimeInMilliseconds(),
    537                       1200,
    538                       false);
    539 
    540   clock_.AdvanceTimeMilliseconds(500);
    541   EXPECT_EQ(500, send_bucket_->QueueInMs());
    542   send_bucket_->Process();
    543   EXPECT_EQ(0, send_bucket_->QueueInMs());
    544 }
    545 }  // namespace test
    546 }  // namespace webrtc
    547