Jonas Oreland | 6d83592 | 2019-03-18 10:59:40 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 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 "media/engine/unhandled_packets_buffer.h" |
| 12 | |
| 13 | #include <memory> |
| 14 | #include "absl/memory/memory.h" |
| 15 | #include "test/gmock.h" |
| 16 | #include "test/gtest.h" |
| 17 | |
| 18 | using ::testing::_; |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | rtc::CopyOnWriteBuffer Create(int n) { |
| 23 | return rtc::CopyOnWriteBuffer(std::to_string(n)); |
| 24 | } |
| 25 | |
| 26 | constexpr int64_t kPacketTimeUs = 122; |
| 27 | |
| 28 | } // namespace |
| 29 | |
| 30 | namespace cricket { |
| 31 | |
| 32 | TEST(UnhandledPacketsBuffer, NoPackets) { |
| 33 | UnhandledPacketsBuffer buff; |
| 34 | buff.AddPacket(2, kPacketTimeUs, Create(3)); |
| 35 | |
| 36 | std::vector<uint32_t> ssrcs = {3}; |
| 37 | std::vector<rtc::CopyOnWriteBuffer> packets; |
| 38 | buff.BackfillPackets(ssrcs, [&packets](uint32_t ssrc, int64_t packet_time_us, |
| 39 | rtc::CopyOnWriteBuffer packet) { |
| 40 | packets.push_back(packet); |
| 41 | }); |
| 42 | EXPECT_EQ(0u, packets.size()); |
| 43 | } |
| 44 | |
| 45 | TEST(UnhandledPacketsBuffer, OnePacket) { |
| 46 | UnhandledPacketsBuffer buff; |
| 47 | buff.AddPacket(2, kPacketTimeUs, Create(3)); |
| 48 | |
| 49 | std::vector<uint32_t> ssrcs = {2}; |
| 50 | std::vector<rtc::CopyOnWriteBuffer> packets; |
| 51 | buff.BackfillPackets(ssrcs, [&packets](uint32_t ssrc, int64_t packet_time_us, |
| 52 | rtc::CopyOnWriteBuffer packet) { |
| 53 | packets.push_back(packet); |
| 54 | }); |
| 55 | ASSERT_EQ(1u, packets.size()); |
| 56 | EXPECT_EQ(Create(3), packets[0]); |
| 57 | } |
| 58 | |
| 59 | TEST(UnhandledPacketsBuffer, TwoPacketsTwoSsrcs) { |
| 60 | UnhandledPacketsBuffer buff; |
| 61 | buff.AddPacket(2, kPacketTimeUs, Create(3)); |
| 62 | buff.AddPacket(3, kPacketTimeUs, Create(4)); |
| 63 | |
| 64 | std::vector<uint32_t> ssrcs = {2, 3}; |
| 65 | std::vector<rtc::CopyOnWriteBuffer> packets; |
| 66 | buff.BackfillPackets(ssrcs, [&packets](uint32_t ssrc, int64_t packet_time_us, |
| 67 | rtc::CopyOnWriteBuffer packet) { |
| 68 | packets.push_back(packet); |
| 69 | }); |
| 70 | ASSERT_EQ(2u, packets.size()); |
| 71 | EXPECT_EQ(Create(3), packets[0]); |
| 72 | EXPECT_EQ(Create(4), packets[1]); |
| 73 | } |
| 74 | |
| 75 | TEST(UnhandledPacketsBuffer, TwoPacketsTwoSsrcsOneMatch) { |
| 76 | UnhandledPacketsBuffer buff; |
| 77 | buff.AddPacket(2, kPacketTimeUs, Create(3)); |
| 78 | buff.AddPacket(3, kPacketTimeUs, Create(4)); |
| 79 | |
| 80 | std::vector<uint32_t> ssrcs = {3}; |
| 81 | buff.BackfillPackets(ssrcs, [](uint32_t ssrc, int64_t packet_time_us, |
| 82 | rtc::CopyOnWriteBuffer packet) { |
| 83 | EXPECT_EQ(ssrc, 3u); |
| 84 | EXPECT_EQ(Create(4), packet); |
| 85 | }); |
| 86 | |
| 87 | std::vector<uint32_t> ssrcs_again = {2}; |
| 88 | buff.BackfillPackets(ssrcs, [](uint32_t ssrc, int64_t packet_time_us, |
| 89 | rtc::CopyOnWriteBuffer packet) { |
| 90 | EXPECT_EQ(ssrc, 2u); |
| 91 | EXPECT_EQ(Create(3), packet); |
| 92 | }); |
| 93 | } |
| 94 | |
| 95 | TEST(UnhandledPacketsBuffer, Full) { |
| 96 | const size_t cnt = UnhandledPacketsBuffer::kMaxStashedPackets; |
| 97 | UnhandledPacketsBuffer buff; |
| 98 | for (size_t i = 0; i < cnt; i++) { |
| 99 | buff.AddPacket(2, kPacketTimeUs, Create(i)); |
| 100 | } |
| 101 | |
| 102 | std::vector<uint32_t> ssrcs = {2}; |
| 103 | std::vector<rtc::CopyOnWriteBuffer> packets; |
| 104 | buff.BackfillPackets(ssrcs, [&packets](uint32_t ssrc, int64_t packet_time_us, |
| 105 | rtc::CopyOnWriteBuffer packet) { |
| 106 | packets.push_back(packet); |
| 107 | }); |
| 108 | ASSERT_EQ(cnt, packets.size()); |
| 109 | for (size_t i = 0; i < cnt; i++) { |
| 110 | EXPECT_EQ(Create(i), packets[i]); |
| 111 | } |
| 112 | |
| 113 | // Add a packet after backfill and check that it comes back. |
| 114 | buff.AddPacket(23, kPacketTimeUs, Create(1001)); |
| 115 | buff.BackfillPackets(ssrcs, [](uint32_t ssrc, int64_t packet_time_us, |
| 116 | rtc::CopyOnWriteBuffer packet) { |
| 117 | EXPECT_EQ(ssrc, 23u); |
| 118 | EXPECT_EQ(Create(1001), packet); |
| 119 | }); |
| 120 | } |
| 121 | |
| 122 | TEST(UnhandledPacketsBuffer, Wrap) { |
| 123 | UnhandledPacketsBuffer buff; |
| 124 | size_t cnt = UnhandledPacketsBuffer::kMaxStashedPackets + 10; |
| 125 | for (size_t i = 0; i < cnt; i++) { |
| 126 | buff.AddPacket(2, kPacketTimeUs, Create(i)); |
| 127 | } |
| 128 | |
| 129 | std::vector<uint32_t> ssrcs = {2}; |
| 130 | std::vector<rtc::CopyOnWriteBuffer> packets; |
| 131 | buff.BackfillPackets(ssrcs, [&packets](uint32_t ssrc, int64_t packet_time_us, |
| 132 | rtc::CopyOnWriteBuffer packet) { |
| 133 | packets.push_back(packet); |
| 134 | }); |
| 135 | for (size_t i = 0; i < packets.size(); i++) { |
| 136 | EXPECT_EQ(Create(i + 10), packets[i]); |
| 137 | } |
| 138 | |
| 139 | // Add a packet after backfill and check that it comes back. |
| 140 | buff.AddPacket(23, kPacketTimeUs, Create(1001)); |
| 141 | buff.BackfillPackets(ssrcs, [](uint32_t ssrc, int64_t packet_time_us, |
| 142 | rtc::CopyOnWriteBuffer packet) { |
| 143 | EXPECT_EQ(ssrc, 23u); |
| 144 | EXPECT_EQ(Create(1001), packet); |
| 145 | }); |
| 146 | } |
| 147 | |
| 148 | TEST(UnhandledPacketsBuffer, Interleaved) { |
| 149 | UnhandledPacketsBuffer buff; |
| 150 | buff.AddPacket(2, kPacketTimeUs, Create(2)); |
| 151 | buff.AddPacket(3, kPacketTimeUs, Create(3)); |
| 152 | |
| 153 | std::vector<uint32_t> ssrcs = {2}; |
| 154 | buff.BackfillPackets(ssrcs, [](uint32_t ssrc, int64_t packet_time_us, |
| 155 | rtc::CopyOnWriteBuffer packet) { |
| 156 | EXPECT_EQ(ssrc, 2u); |
| 157 | EXPECT_EQ(Create(2), packet); |
| 158 | }); |
| 159 | |
| 160 | buff.AddPacket(4, kPacketTimeUs, Create(4)); |
| 161 | |
| 162 | ssrcs = {3}; |
| 163 | buff.BackfillPackets(ssrcs, [](uint32_t ssrc, int64_t packet_time_us, |
| 164 | rtc::CopyOnWriteBuffer packet) { |
| 165 | EXPECT_EQ(ssrc, 3u); |
| 166 | EXPECT_EQ(Create(3), packet); |
| 167 | }); |
| 168 | } |
| 169 | |
| 170 | } // namespace cricket |