OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <algorithm> | 11 #include <algorithm> |
12 #include <limits> | 12 #include <limits> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "webrtc/modules/remote_bitrate_estimator/include/send_time_history.h" | 16 #include "webrtc/modules/remote_bitrate_estimator/include/send_time_history.h" |
17 #include "webrtc/system_wrappers/interface/clock.h" | 17 #include "webrtc/system_wrappers/interface/clock.h" |
18 | 18 |
19 namespace webrtc { | 19 namespace webrtc { |
| 20 namespace test { |
20 | 21 |
21 static const int kDefaultHistoryLengthMs = 1000; | 22 static const int kDefaultHistoryLengthMs = 1000; |
22 | 23 |
23 class SendTimeHistoryTest : public ::testing::Test { | 24 class SendTimeHistoryTest : public ::testing::Test { |
24 protected: | 25 protected: |
25 SendTimeHistoryTest() : history_(kDefaultHistoryLengthMs), clock_(0) {} | 26 SendTimeHistoryTest() : history_(kDefaultHistoryLengthMs), clock_(0) {} |
26 ~SendTimeHistoryTest() {} | 27 ~SendTimeHistoryTest() {} |
27 | 28 |
28 virtual void SetUp() {} | 29 virtual void SetUp() {} |
29 | 30 |
30 virtual void TearDown() {} | 31 virtual void TearDown() {} |
31 | 32 |
32 SendTimeHistory history_; | 33 SendTimeHistory history_; |
33 webrtc::SimulatedClock clock_; | 34 webrtc::SimulatedClock clock_; |
34 }; | 35 }; |
35 | 36 |
| 37 // Help class extended so we can do EXPECT_EQ and collections. |
| 38 class PacketInfo : public webrtc::PacketInfo { |
| 39 public: |
| 40 PacketInfo() : webrtc::PacketInfo(0, 0, 0, 0, false) {} |
| 41 PacketInfo(int64_t arrival_time_ms, |
| 42 int64_t send_time_ms, |
| 43 uint16_t sequence_number, |
| 44 size_t payload_size, |
| 45 bool was_paced) |
| 46 : webrtc::PacketInfo(arrival_time_ms, |
| 47 send_time_ms, |
| 48 sequence_number, |
| 49 payload_size, |
| 50 was_paced) {} |
| 51 bool operator==(const PacketInfo& other) const { |
| 52 return arrival_time_ms == other.arrival_time_ms && |
| 53 send_time_ms == other.send_time_ms && |
| 54 sequence_number == other.sequence_number && |
| 55 payload_size == other.payload_size && was_paced == other.was_paced; |
| 56 } |
| 57 }; |
| 58 |
36 TEST_F(SendTimeHistoryTest, AddRemoveOne) { | 59 TEST_F(SendTimeHistoryTest, AddRemoveOne) { |
37 const uint16_t kSeqNo = 1; | 60 const uint16_t kSeqNo = 10; |
38 const int64_t kTimestamp = 2; | 61 const PacketInfo kSentPacket = {0, 1, kSeqNo, 1, true}; |
39 history_.AddAndRemoveOldSendTimes(kSeqNo, kTimestamp); | 62 history_.AddAndRemoveOld(kSentPacket); |
40 | 63 |
41 int64_t time = 0; | 64 PacketInfo received_packet = {0, 0, kSeqNo, 0, false}; |
42 EXPECT_TRUE(history_.GetSendTime(kSeqNo, &time, false)); | 65 EXPECT_TRUE(history_.GetInfo(&received_packet, false)); |
43 EXPECT_EQ(kTimestamp, time); | 66 EXPECT_EQ(kSentPacket, received_packet); |
44 | 67 |
45 time = 0; | 68 received_packet = {0, 0, kSeqNo, 0, false}; |
46 EXPECT_TRUE(history_.GetSendTime(kSeqNo, &time, true)); | 69 EXPECT_TRUE(history_.GetInfo(&received_packet, true)); |
47 EXPECT_EQ(kTimestamp, time); | 70 EXPECT_EQ(kSentPacket, received_packet); |
48 | 71 |
49 time = 0; | 72 received_packet = {0, 0, kSeqNo, 0, false}; |
50 EXPECT_FALSE(history_.GetSendTime(kSeqNo, &time, true)); | 73 EXPECT_FALSE(history_.GetInfo(&received_packet, true)); |
| 74 } |
| 75 |
| 76 TEST_F(SendTimeHistoryTest, UpdateSendTime) { |
| 77 const uint16_t kSeqNo = 10; |
| 78 const int64_t kSendTime = 1000; |
| 79 const int64_t kSendTimeUpdated = 2000; |
| 80 const PacketInfo kSentPacket = {0, kSendTime, kSeqNo, 1, true}; |
| 81 const PacketInfo kUpdatedPacket = {0, kSendTimeUpdated, kSeqNo, 1, true}; |
| 82 |
| 83 history_.AddAndRemoveOld(kSentPacket); |
| 84 PacketInfo info = {0, 0, kSeqNo, 0, false}; |
| 85 EXPECT_TRUE(history_.GetInfo(&info, false)); |
| 86 EXPECT_EQ(kSentPacket, info); |
| 87 |
| 88 EXPECT_TRUE(history_.UpdateSendTime(kSeqNo, kSendTimeUpdated)); |
| 89 |
| 90 info = {0, 0, kSeqNo, 0, false}; |
| 91 EXPECT_TRUE(history_.GetInfo(&info, true)); |
| 92 EXPECT_EQ(kUpdatedPacket, info); |
| 93 |
| 94 EXPECT_FALSE(history_.UpdateSendTime(kSeqNo, kSendTimeUpdated)); |
| 95 } |
| 96 |
| 97 TEST_F(SendTimeHistoryTest, PopulatesExpectedFields) { |
| 98 const uint16_t kSeqNo = 10; |
| 99 const int64_t kSendTime = 1000; |
| 100 const int64_t kReceiveTime = 2000; |
| 101 const size_t kPayloadSize = 42; |
| 102 const bool kPaced = true; |
| 103 const PacketInfo kSentPacket = {0, kSendTime, kSeqNo, kPayloadSize, kPaced}; |
| 104 |
| 105 history_.AddAndRemoveOld(kSentPacket); |
| 106 |
| 107 PacketInfo info = {kReceiveTime, 0, kSeqNo, 0, false}; |
| 108 EXPECT_TRUE(history_.GetInfo(&info, true)); |
| 109 EXPECT_EQ(kReceiveTime, info.arrival_time_ms); |
| 110 EXPECT_EQ(kSendTime, info.send_time_ms); |
| 111 EXPECT_EQ(kSeqNo, info.sequence_number); |
| 112 EXPECT_EQ(kPayloadSize, info.payload_size); |
| 113 EXPECT_EQ(kPaced, info.was_paced); |
51 } | 114 } |
52 | 115 |
53 TEST_F(SendTimeHistoryTest, AddThenRemoveOutOfOrder) { | 116 TEST_F(SendTimeHistoryTest, AddThenRemoveOutOfOrder) { |
54 struct Timestamp { | 117 std::vector<PacketInfo> sent_packets; |
55 Timestamp(uint16_t sequence_number, int64_t timestamp) | 118 std::vector<PacketInfo> received_packets; |
56 : sequence_number(sequence_number), timestamp(timestamp) {} | |
57 uint16_t sequence_number; | |
58 int64_t timestamp; | |
59 }; | |
60 std::vector<Timestamp> timestamps; | |
61 const size_t num_items = 100; | 119 const size_t num_items = 100; |
| 120 const size_t kPacketSize = 400; |
| 121 const size_t kTransmissionTime = 1234; |
| 122 const bool kPaced = true; |
62 for (size_t i = 0; i < num_items; ++i) { | 123 for (size_t i = 0; i < num_items; ++i) { |
63 timestamps.push_back( | 124 sent_packets.push_back(PacketInfo(0, static_cast<int64_t>(i), |
64 Timestamp(static_cast<uint16_t>(i), static_cast<int64_t>(i))); | 125 static_cast<uint16_t>(i), kPacketSize, |
| 126 kPaced)); |
| 127 received_packets.push_back( |
| 128 PacketInfo(static_cast<int64_t>(i) + kTransmissionTime, 0, |
| 129 static_cast<uint16_t>(i), kPacketSize, false)); |
65 } | 130 } |
66 std::vector<Timestamp> randomized_timestamps = timestamps; | 131 for (size_t i = 0; i < num_items; ++i) |
67 std::random_shuffle(randomized_timestamps.begin(), | 132 history_.AddAndRemoveOld(sent_packets[i]); |
68 randomized_timestamps.end()); | 133 std::random_shuffle(received_packets.begin(), received_packets.end()); |
69 for (size_t i = 0; i < num_items; ++i) { | 134 for (size_t i = 0; i < num_items; ++i) { |
70 history_.AddAndRemoveOldSendTimes(timestamps[i].sequence_number, | 135 PacketInfo packet = received_packets[i]; |
71 timestamps[i].timestamp); | 136 EXPECT_TRUE(history_.GetInfo(&packet, false)); |
| 137 PacketInfo sent_packet = sent_packets[packet.sequence_number]; |
| 138 sent_packet.arrival_time_ms = packet.arrival_time_ms; |
| 139 EXPECT_EQ(sent_packet, packet); |
| 140 EXPECT_TRUE(history_.GetInfo(&packet, true)); |
72 } | 141 } |
73 for (size_t i = 0; i < num_items; ++i) { | 142 for (PacketInfo packet : sent_packets) |
74 int64_t timestamp; | 143 EXPECT_FALSE(history_.GetInfo(&packet, false)); |
75 EXPECT_TRUE(history_.GetSendTime(randomized_timestamps[i].sequence_number, | |
76 ×tamp, false)); | |
77 EXPECT_EQ(randomized_timestamps[i].timestamp, timestamp); | |
78 EXPECT_TRUE(history_.GetSendTime(randomized_timestamps[i].sequence_number, | |
79 ×tamp, true)); | |
80 } | |
81 for (size_t i = 0; i < num_items; ++i) { | |
82 int64_t timestamp; | |
83 EXPECT_FALSE( | |
84 history_.GetSendTime(timestamps[i].sequence_number, ×tamp, false)); | |
85 } | |
86 } | 144 } |
87 | 145 |
88 TEST_F(SendTimeHistoryTest, HistorySize) { | 146 TEST_F(SendTimeHistoryTest, HistorySize) { |
89 const int kItems = kDefaultHistoryLengthMs / 100; | 147 const int kItems = kDefaultHistoryLengthMs / 100; |
| 148 for (int i = 0; i < kItems; ++i) |
| 149 history_.AddAndRemoveOld(PacketInfo(0, i * 100, i, 0, false)); |
90 for (int i = 0; i < kItems; ++i) { | 150 for (int i = 0; i < kItems; ++i) { |
91 history_.AddAndRemoveOldSendTimes(i, i * 100); | 151 PacketInfo info = {0, 0, static_cast<uint16_t>(i), 0, false}; |
| 152 EXPECT_TRUE(history_.GetInfo(&info, false)); |
| 153 EXPECT_EQ(i * 100, info.send_time_ms); |
92 } | 154 } |
93 int64_t timestamp; | 155 history_.AddAndRemoveOld(PacketInfo(0, kItems * 100, kItems, 0, false)); |
94 for (int i = 0; i < kItems; ++i) { | 156 PacketInfo info = {0, 0, 0, 0, false}; |
95 EXPECT_TRUE(history_.GetSendTime(i, ×tamp, false)); | 157 EXPECT_FALSE(history_.GetInfo(&info, false)); |
96 EXPECT_EQ(i * 100, timestamp); | |
97 } | |
98 history_.AddAndRemoveOldSendTimes(kItems, kItems * 100); | |
99 EXPECT_FALSE(history_.GetSendTime(0, ×tamp, false)); | |
100 for (int i = 1; i < (kItems + 1); ++i) { | 158 for (int i = 1; i < (kItems + 1); ++i) { |
101 EXPECT_TRUE(history_.GetSendTime(i, ×tamp, false)); | 159 info = {0, 0, static_cast<uint16_t>(i), 0, false}; |
102 EXPECT_EQ(i * 100, timestamp); | 160 EXPECT_TRUE(history_.GetInfo(&info, false)); |
| 161 EXPECT_EQ(i * 100, info.send_time_ms); |
103 } | 162 } |
104 } | 163 } |
105 | 164 |
106 TEST_F(SendTimeHistoryTest, HistorySizeWithWraparound) { | 165 TEST_F(SendTimeHistoryTest, HistorySizeWithWraparound) { |
107 const int kMaxSeqNo = std::numeric_limits<uint16_t>::max(); | 166 const uint16_t kMaxSeqNo = std::numeric_limits<uint16_t>::max(); |
108 history_.AddAndRemoveOldSendTimes(kMaxSeqNo - 2, 0); | 167 history_.AddAndRemoveOld(PacketInfo(0, 0, kMaxSeqNo - 2, 0, false)); |
109 history_.AddAndRemoveOldSendTimes(kMaxSeqNo - 1, 100); | 168 history_.AddAndRemoveOld(PacketInfo(0, 100, kMaxSeqNo - 1, 0, false)); |
110 history_.AddAndRemoveOldSendTimes(kMaxSeqNo, 200); | 169 history_.AddAndRemoveOld(PacketInfo(0, 200, kMaxSeqNo, 0, false)); |
111 history_.AddAndRemoveOldSendTimes(0, 1000); | 170 history_.AddAndRemoveOld(PacketInfo(0, kDefaultHistoryLengthMs, 0, 0, false)); |
112 int64_t timestamp; | 171 PacketInfo info = {0, 0, static_cast<uint16_t>(kMaxSeqNo - 2), 0, false}; |
113 EXPECT_FALSE(history_.GetSendTime(kMaxSeqNo - 2, ×tamp, false)); | 172 EXPECT_FALSE(history_.GetInfo(&info, false)); |
114 EXPECT_TRUE(history_.GetSendTime(kMaxSeqNo - 1, ×tamp, false)); | 173 info = {0, 0, static_cast<uint16_t>(kMaxSeqNo - 1), 0, false}; |
115 EXPECT_TRUE(history_.GetSendTime(kMaxSeqNo, ×tamp, false)); | 174 EXPECT_TRUE(history_.GetInfo(&info, false)); |
116 EXPECT_TRUE(history_.GetSendTime(0, ×tamp, false)); | 175 info = {0, 0, static_cast<uint16_t>(kMaxSeqNo), 0, false}; |
| 176 EXPECT_TRUE(history_.GetInfo(&info, false)); |
| 177 info = {0, 0, 0, 0, false}; |
| 178 EXPECT_TRUE(history_.GetInfo(&info, false)); |
117 | 179 |
118 // Create a gap (kMaxSeqNo - 1) -> 0. | 180 // Create a gap (kMaxSeqNo - 1) -> 0. |
119 EXPECT_TRUE(history_.GetSendTime(kMaxSeqNo, ×tamp, true)); | 181 info = {0, 0, kMaxSeqNo, 0, false}; |
| 182 EXPECT_TRUE(history_.GetInfo(&info, true)); |
120 | 183 |
121 history_.AddAndRemoveOldSendTimes(1, 1100); | 184 history_.AddAndRemoveOld(PacketInfo(0, 1100, 1, 0, false)); |
122 | 185 |
123 EXPECT_FALSE(history_.GetSendTime(kMaxSeqNo - 2, ×tamp, false)); | 186 info = {0, 0, static_cast<uint16_t>(kMaxSeqNo - 2), 0, false}; |
124 EXPECT_FALSE(history_.GetSendTime(kMaxSeqNo - 1, ×tamp, false)); | 187 EXPECT_FALSE(history_.GetInfo(&info, false)); |
125 EXPECT_FALSE(history_.GetSendTime(kMaxSeqNo, ×tamp, false)); | 188 info = {0, 0, static_cast<uint16_t>(kMaxSeqNo - 1), 0, false}; |
126 EXPECT_TRUE(history_.GetSendTime(0, ×tamp, false)); | 189 EXPECT_FALSE(history_.GetInfo(&info, false)); |
127 EXPECT_TRUE(history_.GetSendTime(1, ×tamp, false)); | 190 info = {0, 0, kMaxSeqNo, 0, false}; |
| 191 EXPECT_FALSE(history_.GetInfo(&info, false)); |
| 192 info = {0, 0, 0, 0, false}; |
| 193 EXPECT_TRUE(history_.GetInfo(&info, false)); |
| 194 info = {0, 0, 1, 0, false}; |
| 195 EXPECT_TRUE(history_.GetInfo(&info, false)); |
128 } | 196 } |
129 | 197 |
130 TEST_F(SendTimeHistoryTest, InterlievedGetAndRemove) { | 198 TEST_F(SendTimeHistoryTest, InterlievedGetAndRemove) { |
131 const uint16_t kSeqNo = 1; | 199 const uint16_t kSeqNo = 1; |
132 const int64_t kTimestamp = 2; | 200 const int64_t kTimestamp = 2; |
| 201 PacketInfo packets[3] = {{0, kTimestamp, kSeqNo, 0, false}, |
| 202 {0, kTimestamp + 1, kSeqNo + 1, 0, false}, |
| 203 {0, kTimestamp + 2, kSeqNo + 2, 0, false}}; |
133 | 204 |
134 history_.AddAndRemoveOldSendTimes(kSeqNo, kTimestamp); | 205 history_.AddAndRemoveOld(packets[0]); |
135 history_.AddAndRemoveOldSendTimes(kSeqNo + 1, kTimestamp + 1); | 206 history_.AddAndRemoveOld(packets[1]); |
136 | 207 |
137 int64_t time = 0; | 208 PacketInfo info = {0, 0, packets[0].sequence_number, 0, false}; |
138 EXPECT_TRUE(history_.GetSendTime(kSeqNo, &time, true)); | 209 EXPECT_TRUE(history_.GetInfo(&info, true)); |
139 EXPECT_EQ(kTimestamp, time); | 210 EXPECT_EQ(packets[0], info); |
140 | 211 |
141 history_.AddAndRemoveOldSendTimes(kSeqNo + 2, kTimestamp + 2); | 212 history_.AddAndRemoveOld(packets[2]); |
142 | 213 |
143 EXPECT_TRUE(history_.GetSendTime(kSeqNo + 1, &time, true)); | 214 info = {0, 0, packets[1].sequence_number, 0, false}; |
144 EXPECT_EQ(kTimestamp + 1, time); | 215 EXPECT_TRUE(history_.GetInfo(&info, true)); |
145 EXPECT_TRUE(history_.GetSendTime(kSeqNo + 2, &time, true)); | 216 EXPECT_EQ(packets[1], info); |
146 EXPECT_EQ(kTimestamp + 2, time); | 217 |
| 218 info = {0, 0, packets[2].sequence_number, 0, false}; |
| 219 EXPECT_TRUE(history_.GetInfo(&info, true)); |
| 220 EXPECT_EQ(packets[2], info); |
147 } | 221 } |
148 | 222 |
| 223 } // namespace test |
149 } // namespace webrtc | 224 } // namespace webrtc |
OLD | NEW |