tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

packet_buffer_unittest.cc (25319B)


      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 // Unit tests for PacketBuffer class.
     12 
     13 #include "modules/audio_coding/neteq/packet_buffer.h"
     14 
     15 #include <algorithm>
     16 #include <cstddef>
     17 #include <cstdint>
     18 #include <memory>
     19 #include <optional>
     20 #include <utility>
     21 #include <vector>
     22 
     23 #include "api/array_view.h"
     24 #include "api/audio_codecs/audio_decoder.h"
     25 #include "api/neteq/tick_timer.h"
     26 #include "modules/audio_coding/neteq/mock/mock_decoder_database.h"
     27 #include "modules/audio_coding/neteq/mock/mock_statistics_calculator.h"
     28 #include "modules/audio_coding/neteq/packet.h"
     29 #include "rtc_base/checks.h"
     30 #include "test/gmock.h"
     31 #include "test/gtest.h"
     32 
     33 using ::testing::_;
     34 using ::testing::InSequence;
     35 using ::testing::MockFunction;
     36 using ::testing::Return;
     37 using ::testing::StrictMock;
     38 
     39 namespace {
     40 class MockEncodedAudioFrame : public webrtc::AudioDecoder::EncodedAudioFrame {
     41 public:
     42  MOCK_METHOD(size_t, Duration, (), (const, override));
     43 
     44  MOCK_METHOD(bool, IsDtxPacket, (), (const, override));
     45 
     46  MOCK_METHOD(std::optional<DecodeResult>,
     47              Decode,
     48              (webrtc::ArrayView<int16_t> decoded),
     49              (const, override));
     50 };
     51 
     52 // Helper class to generate packets. Packets must be deleted by the user.
     53 class PacketGenerator {
     54 public:
     55  PacketGenerator(uint16_t seq_no, uint32_t ts, uint8_t pt, int frame_size);
     56  virtual ~PacketGenerator() {}
     57  void Reset(uint16_t seq_no, uint32_t ts, uint8_t pt, int frame_size);
     58  webrtc::Packet NextPacket(
     59      int payload_size_bytes,
     60      std::unique_ptr<webrtc::AudioDecoder::EncodedAudioFrame> audio_frame);
     61 
     62  uint16_t seq_no_;
     63  uint32_t ts_;
     64  uint8_t pt_;
     65  int frame_size_;
     66 };
     67 
     68 PacketGenerator::PacketGenerator(uint16_t seq_no,
     69                                 uint32_t ts,
     70                                 uint8_t pt,
     71                                 int frame_size) {
     72  Reset(seq_no, ts, pt, frame_size);
     73 }
     74 
     75 void PacketGenerator::Reset(uint16_t seq_no,
     76                            uint32_t ts,
     77                            uint8_t pt,
     78                            int frame_size) {
     79  seq_no_ = seq_no;
     80  ts_ = ts;
     81  pt_ = pt;
     82  frame_size_ = frame_size;
     83 }
     84 
     85 webrtc::Packet PacketGenerator::NextPacket(
     86    int payload_size_bytes,
     87    std::unique_ptr<webrtc::AudioDecoder::EncodedAudioFrame> audio_frame) {
     88  webrtc::Packet packet;
     89  packet.sequence_number = seq_no_;
     90  packet.timestamp = ts_;
     91  packet.payload_type = pt_;
     92  packet.payload.SetSize(payload_size_bytes);
     93  ++seq_no_;
     94  ts_ += frame_size_;
     95  packet.frame = std::move(audio_frame);
     96  return packet;
     97 }
     98 
     99 struct PacketsToInsert {
    100  uint16_t sequence_number;
    101  uint32_t timestamp;
    102  uint8_t payload_type;
    103  bool primary;
    104  // Order of this packet to appear upon extraction, after inserting a series
    105  // of packets. A negative number means that it should have been discarded
    106  // before extraction.
    107  int extract_order;
    108 };
    109 
    110 }  // namespace
    111 
    112 namespace webrtc {
    113 
    114 // Start of test definitions.
    115 
    116 TEST(PacketBuffer, CreateAndDestroy) {
    117  TickTimer tick_timer;
    118  StrictMock<MockStatisticsCalculator> mock_stats(&tick_timer);
    119  PacketBuffer* buffer =
    120      new PacketBuffer(10, &tick_timer, &mock_stats);  // 10 packets.
    121  EXPECT_TRUE(buffer->Empty());
    122  delete buffer;
    123 }
    124 
    125 TEST(PacketBuffer, InsertPacket) {
    126  TickTimer tick_timer;
    127  StrictMock<MockStatisticsCalculator> mock_stats(&tick_timer);
    128  PacketBuffer buffer(10, &tick_timer, &mock_stats);  // 10 packets.
    129  PacketGenerator gen(17u, 4711u, 0, 10);
    130  MockDecoderDatabase decoder_database;
    131 
    132  const int payload_len = 100;
    133  const Packet packet = gen.NextPacket(payload_len, nullptr);
    134  EXPECT_EQ(0, buffer.InsertPacket(/*packet=*/packet.Clone()));
    135  uint32_t next_ts;
    136  EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&next_ts));
    137  EXPECT_EQ(4711u, next_ts);
    138  EXPECT_FALSE(buffer.Empty());
    139  EXPECT_EQ(1u, buffer.NumPacketsInBuffer());
    140  const Packet* next_packet = buffer.PeekNextPacket();
    141  EXPECT_EQ(packet, *next_packet);       // Compare contents.
    142  EXPECT_CALL(decoder_database, Die());  // Called when object is deleted.
    143 
    144  // Do not explicitly flush buffer or delete packet to test that it is deleted
    145  // with the buffer. (Tested with Valgrind or similar tool.)
    146 }
    147 
    148 // Test to flush buffer.
    149 TEST(PacketBuffer, FlushBuffer) {
    150  TickTimer tick_timer;
    151  StrictMock<MockStatisticsCalculator> mock_stats(&tick_timer);
    152  PacketBuffer buffer(10, &tick_timer, &mock_stats);  // 10 packets.
    153  PacketGenerator gen(0, 0, 0, 10);
    154  const int payload_len = 10;
    155  MockDecoderDatabase decoder_database;
    156 
    157  // Insert 10 small packets; should be ok.
    158  for (int i = 0; i < 10; ++i) {
    159    EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacket(/*packet=*/gen.NextPacket(
    160                                     payload_len, nullptr)));
    161  }
    162  EXPECT_EQ(10u, buffer.NumPacketsInBuffer());
    163  EXPECT_FALSE(buffer.Empty());
    164 
    165  EXPECT_CALL(mock_stats, PacketsDiscarded(1)).Times(10);
    166  buffer.Flush();
    167  // Buffer should delete the payloads itself.
    168  EXPECT_EQ(0u, buffer.NumPacketsInBuffer());
    169  EXPECT_TRUE(buffer.Empty());
    170  EXPECT_CALL(decoder_database, Die());  // Called when object is deleted.
    171 }
    172 
    173 // Test to fill the buffer over the limits, and verify that it flushes.
    174 TEST(PacketBuffer, OverfillBuffer) {
    175  TickTimer tick_timer;
    176  StrictMock<MockStatisticsCalculator> mock_stats(&tick_timer);
    177  PacketBuffer buffer(10, &tick_timer, &mock_stats);  // 10 packets.
    178  PacketGenerator gen(0, 0, 0, 10);
    179  MockDecoderDatabase decoder_database;
    180 
    181  // Insert 10 small packets; should be ok.
    182  const int payload_len = 10;
    183  int i;
    184  for (i = 0; i < 10; ++i) {
    185    EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacket(/*packet=*/gen.NextPacket(
    186                                     payload_len, nullptr)));
    187  }
    188  EXPECT_EQ(10u, buffer.NumPacketsInBuffer());
    189  uint32_t next_ts;
    190  EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&next_ts));
    191  EXPECT_EQ(0u, next_ts);  // Expect first inserted packet to be first in line.
    192 
    193  EXPECT_CALL(mock_stats, PacketsDiscarded(1)).Times(10);
    194  const Packet packet = gen.NextPacket(payload_len, nullptr);
    195  // Insert 11th packet; should flush the buffer and insert it after flushing.
    196  EXPECT_EQ(PacketBuffer::kFlushed,
    197            buffer.InsertPacket(/*packet=*/packet.Clone()));
    198  EXPECT_EQ(1u, buffer.NumPacketsInBuffer());
    199  EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&next_ts));
    200  // Expect last inserted packet to be first in line.
    201  EXPECT_EQ(packet.timestamp, next_ts);
    202 
    203  EXPECT_CALL(decoder_database, Die());  // Called when object is deleted.
    204 }
    205 
    206 TEST(PacketBuffer, ExtractOrderRedundancy) {
    207  TickTimer tick_timer;
    208  StrictMock<MockStatisticsCalculator> mock_stats(&tick_timer);
    209  PacketBuffer buffer(100, &tick_timer, &mock_stats);  // 100 packets.
    210  const int kPackets = 18;
    211  const int kFrameSize = 10;
    212  const int kPayloadLength = 10;
    213 
    214  PacketsToInsert packet_facts[kPackets] = {
    215      {.sequence_number = 0xFFFD,
    216       .timestamp = 0xFFFFFFD7,
    217       .payload_type = 0,
    218       .primary = true,
    219       .extract_order = 0},
    220      {.sequence_number = 0xFFFE,
    221       .timestamp = 0xFFFFFFE1,
    222       .payload_type = 0,
    223       .primary = true,
    224       .extract_order = 1},
    225      {.sequence_number = 0xFFFE,
    226       .timestamp = 0xFFFFFFD7,
    227       .payload_type = 1,
    228       .primary = false,
    229       .extract_order = -1},
    230      {.sequence_number = 0xFFFF,
    231       .timestamp = 0xFFFFFFEB,
    232       .payload_type = 0,
    233       .primary = true,
    234       .extract_order = 2},
    235      {.sequence_number = 0xFFFF,
    236       .timestamp = 0xFFFFFFE1,
    237       .payload_type = 1,
    238       .primary = false,
    239       .extract_order = -1},
    240      {.sequence_number = 0x0000,
    241       .timestamp = 0xFFFFFFF5,
    242       .payload_type = 0,
    243       .primary = true,
    244       .extract_order = 3},
    245      {.sequence_number = 0x0000,
    246       .timestamp = 0xFFFFFFEB,
    247       .payload_type = 1,
    248       .primary = false,
    249       .extract_order = -1},
    250      {.sequence_number = 0x0001,
    251       .timestamp = 0xFFFFFFFF,
    252       .payload_type = 0,
    253       .primary = true,
    254       .extract_order = 4},
    255      {.sequence_number = 0x0001,
    256       .timestamp = 0xFFFFFFF5,
    257       .payload_type = 1,
    258       .primary = false,
    259       .extract_order = -1},
    260      {.sequence_number = 0x0002,
    261       .timestamp = 0x0000000A,
    262       .payload_type = 0,
    263       .primary = true,
    264       .extract_order = 5},
    265      {.sequence_number = 0x0002,
    266       .timestamp = 0xFFFFFFFF,
    267       .payload_type = 1,
    268       .primary = false,
    269       .extract_order = -1},
    270      {.sequence_number = 0x0003,
    271       .timestamp = 0x0000000A,
    272       .payload_type = 1,
    273       .primary = false,
    274       .extract_order = -1},
    275      {.sequence_number = 0x0004,
    276       .timestamp = 0x0000001E,
    277       .payload_type = 0,
    278       .primary = true,
    279       .extract_order = 7},
    280      {.sequence_number = 0x0004,
    281       .timestamp = 0x00000014,
    282       .payload_type = 1,
    283       .primary = false,
    284       .extract_order = 6},
    285      {.sequence_number = 0x0005,
    286       .timestamp = 0x0000001E,
    287       .payload_type = 0,
    288       .primary = true,
    289       .extract_order = -1},
    290      {.sequence_number = 0x0005,
    291       .timestamp = 0x00000014,
    292       .payload_type = 1,
    293       .primary = false,
    294       .extract_order = -1},
    295      {.sequence_number = 0x0006,
    296       .timestamp = 0x00000028,
    297       .payload_type = 0,
    298       .primary = true,
    299       .extract_order = 8},
    300      {.sequence_number = 0x0006,
    301       .timestamp = 0x0000001E,
    302       .payload_type = 1,
    303       .primary = false,
    304       .extract_order = -1},
    305  };
    306  MockDecoderDatabase decoder_database;
    307 
    308  const size_t kExpectPacketsInBuffer = 9;
    309 
    310  std::vector<Packet> expect_order(kExpectPacketsInBuffer);
    311 
    312  PacketGenerator gen(0, 0, 0, kFrameSize);
    313 
    314  // Interleaving the EXPECT_CALL sequence with expectations on the MockFunction
    315  // check ensures that exactly one call to PacketsDiscarded happens in each
    316  // DiscardNextPacket call.
    317  InSequence s;
    318  MockFunction<void(int check_point_id)> check;
    319  for (int i = 0; i < kPackets; ++i) {
    320    gen.Reset(packet_facts[i].sequence_number, packet_facts[i].timestamp,
    321              packet_facts[i].payload_type, kFrameSize);
    322    Packet packet = gen.NextPacket(kPayloadLength, nullptr);
    323    packet.priority.codec_level = packet_facts[i].primary ? 0 : 1;
    324    if (packet_facts[i].extract_order < 0) {
    325      if (packet.priority.codec_level > 0) {
    326        EXPECT_CALL(mock_stats, SecondaryPacketsDiscarded(1));
    327      } else {
    328        EXPECT_CALL(mock_stats, PacketsDiscarded(1));
    329      }
    330    }
    331    EXPECT_CALL(check, Call(i));
    332    EXPECT_EQ(PacketBuffer::kOK,
    333              buffer.InsertPacket(/*packet=*/packet.Clone()));
    334    if (packet_facts[i].extract_order >= 0) {
    335      expect_order[packet_facts[i].extract_order] = std::move(packet);
    336    }
    337    check.Call(i);
    338  }
    339 
    340  EXPECT_EQ(kExpectPacketsInBuffer, buffer.NumPacketsInBuffer());
    341 
    342  for (size_t i = 0; i < kExpectPacketsInBuffer; ++i) {
    343    const std::optional<Packet> packet = buffer.GetNextPacket();
    344    EXPECT_EQ(packet, expect_order[i]);  // Compare contents.
    345  }
    346  EXPECT_TRUE(buffer.Empty());
    347  EXPECT_CALL(decoder_database, Die());  // Called when object is deleted.
    348 }
    349 
    350 TEST(PacketBuffer, DiscardPackets) {
    351  TickTimer tick_timer;
    352  StrictMock<MockStatisticsCalculator> mock_stats(&tick_timer);
    353  PacketBuffer buffer(100, &tick_timer, &mock_stats);  // 100 packets.
    354  const uint16_t start_seq_no = 17;
    355  const uint32_t start_ts = 4711;
    356  const uint32_t ts_increment = 10;
    357  PacketGenerator gen(start_seq_no, start_ts, 0, ts_increment);
    358  PacketList list;
    359  const int payload_len = 10;
    360  MockDecoderDatabase decoder_database;
    361 
    362  constexpr int kTotalPackets = 10;
    363  // Insert 10 small packets.
    364  for (int i = 0; i < kTotalPackets; ++i) {
    365    buffer.InsertPacket(/*packet=*/gen.NextPacket(payload_len, nullptr));
    366  }
    367  EXPECT_EQ(10u, buffer.NumPacketsInBuffer());
    368 
    369  uint32_t current_ts = start_ts;
    370 
    371  // Discard them one by one and make sure that the right packets are at the
    372  // front of the buffer.
    373  constexpr int kDiscardPackets = 5;
    374 
    375  // Interleaving the EXPECT_CALL sequence with expectations on the MockFunction
    376  // check ensures that exactly one call to PacketsDiscarded happens in each
    377  // DiscardNextPacket call.
    378  InSequence s;
    379  MockFunction<void(int check_point_id)> check;
    380  for (int i = 0; i < kDiscardPackets; ++i) {
    381    uint32_t ts;
    382    EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&ts));
    383    EXPECT_EQ(current_ts, ts);
    384    EXPECT_CALL(mock_stats, PacketsDiscarded(1));
    385    EXPECT_CALL(check, Call(i));
    386    EXPECT_EQ(PacketBuffer::kOK, buffer.DiscardNextPacket());
    387    current_ts += ts_increment;
    388    check.Call(i);
    389  }
    390 
    391  constexpr int kRemainingPackets = kTotalPackets - kDiscardPackets;
    392  // This will discard all remaining packets but one. The oldest packet is older
    393  // than the indicated horizon_samples, and will thus be left in the buffer.
    394  constexpr size_t kSkipPackets = 1;
    395  EXPECT_CALL(mock_stats, PacketsDiscarded(1))
    396      .Times(kRemainingPackets - kSkipPackets);
    397  EXPECT_CALL(check, Call(17));  // Arbitrary id number.
    398  buffer.DiscardOldPackets(start_ts + kTotalPackets * ts_increment,
    399                           kRemainingPackets * ts_increment);
    400  check.Call(17);  // Same arbitrary id number.
    401 
    402  EXPECT_EQ(kSkipPackets, buffer.NumPacketsInBuffer());
    403  uint32_t ts;
    404  EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&ts));
    405  EXPECT_EQ(current_ts, ts);
    406 
    407  // Discard all remaining packets.
    408  EXPECT_CALL(mock_stats, PacketsDiscarded(kSkipPackets));
    409  buffer.DiscardAllOldPackets(start_ts + kTotalPackets * ts_increment);
    410 
    411  EXPECT_TRUE(buffer.Empty());
    412  EXPECT_CALL(decoder_database, Die());  // Called when object is deleted.
    413 }
    414 
    415 TEST(PacketBuffer, Reordering) {
    416  TickTimer tick_timer;
    417  StrictMock<MockStatisticsCalculator> mock_stats(&tick_timer);
    418  PacketBuffer buffer(100, &tick_timer, &mock_stats);  // 100 packets.
    419  const uint16_t start_seq_no = 17;
    420  const uint32_t start_ts = 4711;
    421  const uint32_t ts_increment = 10;
    422  PacketGenerator gen(start_seq_no, start_ts, 0, ts_increment);
    423  const int payload_len = 10;
    424 
    425  // Generate 10 small packets and insert them into a PacketList. Insert every
    426  // odd packet to the front, and every even packet to the back, thus creating
    427  // a (rather strange) reordering.
    428  PacketList list;
    429  for (int i = 0; i < 10; ++i) {
    430    Packet packet = gen.NextPacket(payload_len, nullptr);
    431    if (i % 2) {
    432      list.push_front(std::move(packet));
    433    } else {
    434      list.push_back(std::move(packet));
    435    }
    436  }
    437 
    438  for (Packet& packet : list) {
    439    EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacket(std::move(packet)));
    440  }
    441  EXPECT_EQ(10u, buffer.NumPacketsInBuffer());
    442 
    443  // Extract them and make sure that come out in the right order.
    444  uint32_t current_ts = start_ts;
    445  for (int i = 0; i < 10; ++i) {
    446    const std::optional<Packet> packet = buffer.GetNextPacket();
    447    ASSERT_TRUE(packet);
    448    EXPECT_EQ(current_ts, packet->timestamp);
    449    current_ts += ts_increment;
    450  }
    451  EXPECT_TRUE(buffer.Empty());
    452 }
    453 
    454 TEST(PacketBuffer, Failures) {
    455  const uint16_t start_seq_no = 17;
    456  const uint32_t start_ts = 4711;
    457  const uint32_t ts_increment = 10;
    458  int payload_len = 100;
    459  PacketGenerator gen(start_seq_no, start_ts, 0, ts_increment);
    460  TickTimer tick_timer;
    461  StrictMock<MockStatisticsCalculator> mock_stats(&tick_timer);
    462 
    463  PacketBuffer buffer(100, &tick_timer, &mock_stats);  // 100 packets.
    464  {
    465    Packet packet = gen.NextPacket(payload_len, nullptr);
    466    packet.payload.Clear();
    467    EXPECT_EQ(PacketBuffer::kInvalidPacket,
    468              buffer.InsertPacket(/*packet=*/std::move(packet)));
    469  }
    470  // Buffer should still be empty. Test all empty-checks.
    471  uint32_t temp_ts;
    472  EXPECT_EQ(PacketBuffer::kBufferEmpty, buffer.NextTimestamp(&temp_ts));
    473  EXPECT_EQ(PacketBuffer::kBufferEmpty,
    474            buffer.NextHigherTimestamp(0, &temp_ts));
    475  EXPECT_EQ(nullptr, buffer.PeekNextPacket());
    476  EXPECT_FALSE(buffer.GetNextPacket());
    477 
    478  // Discarding packets will not invoke mock_stats.PacketDiscarded() because the
    479  // packet buffer is empty.
    480  EXPECT_EQ(PacketBuffer::kBufferEmpty, buffer.DiscardNextPacket());
    481  buffer.DiscardAllOldPackets(0);
    482 }
    483 
    484 // Test packet comparison function.
    485 // The function should return true if the first packet "goes before" the second.
    486 TEST(PacketBuffer, ComparePackets) {
    487  PacketGenerator gen(0, 0, 0, 10);
    488  Packet a(gen.NextPacket(10, nullptr));  // SN = 0, TS = 0.
    489  Packet b(gen.NextPacket(10, nullptr));  // SN = 1, TS = 10.
    490  EXPECT_FALSE(a == b);
    491  EXPECT_TRUE(a != b);
    492  EXPECT_TRUE(a < b);
    493  EXPECT_FALSE(a > b);
    494  EXPECT_TRUE(a <= b);
    495  EXPECT_FALSE(a >= b);
    496 
    497  // Testing wrap-around case; 'a' is earlier but has a larger timestamp value.
    498  a.timestamp = 0xFFFFFFFF - 10;
    499  EXPECT_FALSE(a == b);
    500  EXPECT_TRUE(a != b);
    501  EXPECT_TRUE(a < b);
    502  EXPECT_FALSE(a > b);
    503  EXPECT_TRUE(a <= b);
    504  EXPECT_FALSE(a >= b);
    505 
    506  // Test equal packets.
    507  EXPECT_TRUE(a == a);
    508  EXPECT_FALSE(a != a);
    509  EXPECT_FALSE(a < a);
    510  EXPECT_FALSE(a > a);
    511  EXPECT_TRUE(a <= a);
    512  EXPECT_TRUE(a >= a);
    513 
    514  // Test equal timestamps but different sequence numbers (0 and 1).
    515  a.timestamp = b.timestamp;
    516  EXPECT_FALSE(a == b);
    517  EXPECT_TRUE(a != b);
    518  EXPECT_TRUE(a < b);
    519  EXPECT_FALSE(a > b);
    520  EXPECT_TRUE(a <= b);
    521  EXPECT_FALSE(a >= b);
    522 
    523  // Test equal timestamps but different sequence numbers (32767 and 1).
    524  a.sequence_number = 0xFFFF;
    525  EXPECT_FALSE(a == b);
    526  EXPECT_TRUE(a != b);
    527  EXPECT_TRUE(a < b);
    528  EXPECT_FALSE(a > b);
    529  EXPECT_TRUE(a <= b);
    530  EXPECT_FALSE(a >= b);
    531 
    532  // Test equal timestamps and sequence numbers, but differing priorities.
    533  a.sequence_number = b.sequence_number;
    534  a.priority = {1, 0};
    535  b.priority = {0, 0};
    536  // a after b
    537  EXPECT_FALSE(a == b);
    538  EXPECT_TRUE(a != b);
    539  EXPECT_FALSE(a < b);
    540  EXPECT_TRUE(a > b);
    541  EXPECT_FALSE(a <= b);
    542  EXPECT_TRUE(a >= b);
    543 
    544  Packet c(gen.NextPacket(0, nullptr));  // SN = 2, TS = 20.
    545  Packet d(gen.NextPacket(0, nullptr));  // SN = 3, TS = 20.
    546  c.timestamp = b.timestamp;
    547  d.timestamp = b.timestamp;
    548  c.sequence_number = b.sequence_number;
    549  d.sequence_number = b.sequence_number;
    550  c.priority = {1, 1};
    551  d.priority = {0, 1};
    552  // c after d
    553  EXPECT_FALSE(c == d);
    554  EXPECT_TRUE(c != d);
    555  EXPECT_FALSE(c < d);
    556  EXPECT_TRUE(c > d);
    557  EXPECT_FALSE(c <= d);
    558  EXPECT_TRUE(c >= d);
    559 
    560  // c after a
    561  EXPECT_FALSE(c == a);
    562  EXPECT_TRUE(c != a);
    563  EXPECT_FALSE(c < a);
    564  EXPECT_TRUE(c > a);
    565  EXPECT_FALSE(c <= a);
    566  EXPECT_TRUE(c >= a);
    567 
    568  // c after b
    569  EXPECT_FALSE(c == b);
    570  EXPECT_TRUE(c != b);
    571  EXPECT_FALSE(c < b);
    572  EXPECT_TRUE(c > b);
    573  EXPECT_FALSE(c <= b);
    574  EXPECT_TRUE(c >= b);
    575 
    576  // a after d
    577  EXPECT_FALSE(a == d);
    578  EXPECT_TRUE(a != d);
    579  EXPECT_FALSE(a < d);
    580  EXPECT_TRUE(a > d);
    581  EXPECT_FALSE(a <= d);
    582  EXPECT_TRUE(a >= d);
    583 
    584  // d after b
    585  EXPECT_FALSE(d == b);
    586  EXPECT_TRUE(d != b);
    587  EXPECT_FALSE(d < b);
    588  EXPECT_TRUE(d > b);
    589  EXPECT_FALSE(d <= b);
    590  EXPECT_TRUE(d >= b);
    591 }
    592 
    593 TEST(PacketBuffer, GetSpanSamples) {
    594  constexpr size_t kFrameSizeSamples = 10;
    595  constexpr int kPayloadSizeBytes = 1;  // Does not matter to this test;
    596  constexpr uint32_t kStartTimeStamp = 0xFFFFFFFE;  // Close to wrap around.
    597  constexpr int kSampleRateHz = 48000;
    598  constexpr bool kCountWaitingTime = false;
    599  TickTimer tick_timer;
    600  StrictMock<MockStatisticsCalculator> mock_stats(&tick_timer);
    601  PacketBuffer buffer(3, &tick_timer, &mock_stats);
    602  PacketGenerator gen(0, kStartTimeStamp, 0, kFrameSizeSamples);
    603  MockDecoderDatabase decoder_database;
    604 
    605  Packet packet_1 = gen.NextPacket(kPayloadSizeBytes, nullptr);
    606 
    607  std::unique_ptr<MockEncodedAudioFrame> mock_audio_frame =
    608      std::make_unique<MockEncodedAudioFrame>();
    609  EXPECT_CALL(*mock_audio_frame, Duration())
    610      .WillRepeatedly(Return(kFrameSizeSamples));
    611  Packet packet_2 =
    612      gen.NextPacket(kPayloadSizeBytes, std::move(mock_audio_frame));
    613 
    614  RTC_DCHECK_GT(packet_1.timestamp,
    615                packet_2.timestamp);  // Tmestamp wrapped around.
    616 
    617  EXPECT_EQ(PacketBuffer::kOK,
    618            buffer.InsertPacket(/*packet=*/std::move(packet_1)));
    619 
    620  constexpr size_t kLastDecodedSizeSamples = 2;
    621  // packet_1 has no access to duration, and relies last decoded duration as
    622  // input.
    623  EXPECT_EQ(kLastDecodedSizeSamples,
    624            buffer.GetSpanSamples(kLastDecodedSizeSamples, kSampleRateHz,
    625                                  kCountWaitingTime));
    626 
    627  EXPECT_EQ(PacketBuffer::kOK,
    628            buffer.InsertPacket(/*packet=*/std::move(packet_2)));
    629 
    630  EXPECT_EQ(kFrameSizeSamples * 2,
    631            buffer.GetSpanSamples(0, kSampleRateHz, kCountWaitingTime));
    632 
    633  // packet_2 has access to duration, and ignores last decoded duration as
    634  // input.
    635  EXPECT_EQ(kFrameSizeSamples * 2,
    636            buffer.GetSpanSamples(kLastDecodedSizeSamples, kSampleRateHz,
    637                                  kCountWaitingTime));
    638 }
    639 
    640 TEST(PacketBuffer, GetSpanSamplesCountWaitingTime) {
    641  constexpr size_t kFrameSizeSamples = 10;
    642  constexpr int kPayloadSizeBytes = 1;  // Does not matter to this test;
    643  constexpr uint32_t kStartTimeStamp = 0xFFFFFFFE;  // Close to wrap around.
    644  constexpr int kSampleRateHz = 48000;
    645  constexpr bool kCountWaitingTime = true;
    646  constexpr size_t kLastDecodedSizeSamples = 0;
    647  TickTimer tick_timer;
    648  StrictMock<MockStatisticsCalculator> mock_stats(&tick_timer);
    649  PacketBuffer buffer(3, &tick_timer, &mock_stats);
    650  PacketGenerator gen(0, kStartTimeStamp, 0, kFrameSizeSamples);
    651  MockDecoderDatabase decoder_database;
    652 
    653  Packet packet = gen.NextPacket(kPayloadSizeBytes, nullptr);
    654 
    655  EXPECT_EQ(PacketBuffer::kOK,
    656            buffer.InsertPacket(/*packet=*/std::move(packet)));
    657 
    658  EXPECT_EQ(0u, buffer.GetSpanSamples(kLastDecodedSizeSamples, kSampleRateHz,
    659                                      kCountWaitingTime));
    660 
    661  tick_timer.Increment();
    662  EXPECT_EQ(480u, buffer.GetSpanSamples(0, kSampleRateHz, kCountWaitingTime));
    663 
    664  tick_timer.Increment();
    665  EXPECT_EQ(960u, buffer.GetSpanSamples(0, kSampleRateHz, kCountWaitingTime));
    666 }
    667 
    668 namespace {
    669 void TestIsObsoleteTimestamp(uint32_t limit_timestamp) {
    670  // Check with zero horizon, which implies that the horizon is at 2^31, i.e.,
    671  // half the timestamp range.
    672  static const uint32_t kZeroHorizon = 0;
    673  static const uint32_t k2Pow31Minus1 = 0x7FFFFFFF;
    674  // Timestamp on the limit is not old.
    675  EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
    676      limit_timestamp, limit_timestamp, kZeroHorizon));
    677  // 1 sample behind is old.
    678  EXPECT_TRUE(PacketBuffer::IsObsoleteTimestamp(limit_timestamp - 1,
    679                                                limit_timestamp, kZeroHorizon));
    680  // 2^31 - 1 samples behind is old.
    681  EXPECT_TRUE(PacketBuffer::IsObsoleteTimestamp(limit_timestamp - k2Pow31Minus1,
    682                                                limit_timestamp, kZeroHorizon));
    683  // 1 sample ahead is not old.
    684  EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
    685      limit_timestamp + 1, limit_timestamp, kZeroHorizon));
    686  // If |t1-t2|=2^31 and t1>t2, t2 is older than t1 but not the opposite.
    687  uint32_t other_timestamp = limit_timestamp + (1 << 31);
    688  uint32_t lowest_timestamp = std::min(limit_timestamp, other_timestamp);
    689  uint32_t highest_timestamp = std::max(limit_timestamp, other_timestamp);
    690  EXPECT_TRUE(PacketBuffer::IsObsoleteTimestamp(
    691      lowest_timestamp, highest_timestamp, kZeroHorizon));
    692  EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
    693      highest_timestamp, lowest_timestamp, kZeroHorizon));
    694 
    695  // Fixed horizon at 10 samples.
    696  static const uint32_t kHorizon = 10;
    697  // Timestamp on the limit is not old.
    698  EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(limit_timestamp,
    699                                                 limit_timestamp, kHorizon));
    700  // 1 sample behind is old.
    701  EXPECT_TRUE(PacketBuffer::IsObsoleteTimestamp(limit_timestamp - 1,
    702                                                limit_timestamp, kHorizon));
    703  // 9 samples behind is old.
    704  EXPECT_TRUE(PacketBuffer::IsObsoleteTimestamp(limit_timestamp - 9,
    705                                                limit_timestamp, kHorizon));
    706  // 10 samples behind is not old.
    707  EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(limit_timestamp - 10,
    708                                                 limit_timestamp, kHorizon));
    709  // 2^31 - 1 samples behind is not old.
    710  EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
    711      limit_timestamp - k2Pow31Minus1, limit_timestamp, kHorizon));
    712  // 1 sample ahead is not old.
    713  EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(limit_timestamp + 1,
    714                                                 limit_timestamp, kHorizon));
    715  // 2^31 samples ahead is not old.
    716  EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(limit_timestamp + (1 << 31),
    717                                                 limit_timestamp, kHorizon));
    718 }
    719 }  // namespace
    720 
    721 // Test the IsObsoleteTimestamp method with different limit timestamps.
    722 TEST(PacketBuffer, IsObsoleteTimestamp) {
    723  TestIsObsoleteTimestamp(0);
    724  TestIsObsoleteTimestamp(1);
    725  TestIsObsoleteTimestamp(0xFFFFFFFF);  // -1 in uint32_t.
    726  TestIsObsoleteTimestamp(0x80000000);  // 2^31.
    727  TestIsObsoleteTimestamp(0x80000001);  // 2^31 + 1.
    728  TestIsObsoleteTimestamp(0x7FFFFFFF);  // 2^31 - 1.
    729 }
    730 
    731 }  // namespace webrtc