Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: webrtc/test/fake_network_pipe_unittest.cc

Issue 2794243002: Making FakeNetworkPipe demux audio and video packets. (Closed)
Patch Set: fixing android Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 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 <memory> 11 #include <memory>
12 12
13 #include "webrtc/call/call.h" 13 #include "webrtc/call/call.h"
14 #include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
14 #include "webrtc/system_wrappers/include/clock.h" 15 #include "webrtc/system_wrappers/include/clock.h"
15 #include "webrtc/test/fake_network_pipe.h" 16 #include "webrtc/test/fake_network_pipe.h"
16 #include "webrtc/test/gmock.h" 17 #include "webrtc/test/gmock.h"
17 #include "webrtc/test/gtest.h" 18 #include "webrtc/test/gtest.h"
18 19
19 using ::testing::_; 20 using ::testing::_;
20 using ::testing::AnyNumber; 21 using ::testing::AnyNumber;
21 using ::testing::Return; 22 using ::testing::Return;
22 using ::testing::Invoke; 23 using ::testing::Invoke;
23 24
24 namespace webrtc { 25 namespace webrtc {
25 26
26 class TestReceiver : public PacketReceiver { 27 class TestDemuxer : public Demuxer {
27 public: 28 public:
28 TestReceiver() {} 29 void IncomingPacket(NetworkPacket* packet) {
29 virtual ~TestReceiver() {} 30 DeliverPacket(packet, PacketTime());
30
31 void IncomingPacket(const uint8_t* data, size_t length) {
32 DeliverPacket(MediaType::ANY, data, length, PacketTime());
33 delete [] data;
34 } 31 }
35 32
36 virtual MOCK_METHOD4( 33 MOCK_METHOD1(SetReceiver, void(PacketReceiver* receiver));
34 MOCK_METHOD2(DeliverPacket,
35 void(const NetworkPacket* packet,
36 const PacketTime& packet_time));
37 };
38
39 class ReorderTestDemuxer : public TestDemuxer {
40 public:
41 void DeliverPacket(const NetworkPacket* packet,
42 const PacketTime& packet_time) override {
43 RTC_DCHECK_GE(packet->data_length(), sizeof(int));
44 int seq_num;
45 memcpy(&seq_num, packet->data(), sizeof(int));
46 delivered_sequence_numbers_.push_back(seq_num);
47 }
48 std::vector<int> delivered_sequence_numbers_;
49 };
50
51 class MockReceiver : public PacketReceiver {
52 public:
53 MOCK_METHOD4(
37 DeliverPacket, 54 DeliverPacket,
38 DeliveryStatus(MediaType, const uint8_t*, size_t, const PacketTime&)); 55 DeliveryStatus(MediaType, const uint8_t*, size_t, const PacketTime&));
39 }; 56 };
40 57
41 class ReorderTestReceiver : public TestReceiver {
42 public:
43 ReorderTestReceiver() {}
44 virtual ~ReorderTestReceiver() {}
45
46 DeliveryStatus DeliverPacket(MediaType media_type,
47 const uint8_t* packet,
48 size_t length,
49 const PacketTime& packet_time) override {
50 int seq_num;
51 memcpy(&seq_num, packet, sizeof(int));
52 delivered_sequence_numbers_.push_back(seq_num);
53 return PacketReceiver::DELIVERY_OK;
54 }
55 std::vector<int> delivered_sequence_numbers_;
56 };
57
58 class FakeNetworkPipeTest : public ::testing::Test { 58 class FakeNetworkPipeTest : public ::testing::Test {
59 public: 59 public:
60 FakeNetworkPipeTest() : fake_clock_(12345) {} 60 FakeNetworkPipeTest() : fake_clock_(12345) {}
61 61
62 protected: 62 protected:
63 virtual void SetUp() {
64 receiver_.reset(new TestReceiver());
65 ON_CALL(*receiver_, DeliverPacket(_, _, _, _))
66 .WillByDefault(Return(PacketReceiver::DELIVERY_OK));
67 }
68
69 virtual void TearDown() {
70 }
71
72 void SendPackets(FakeNetworkPipe* pipe, int number_packets, int packet_size) { 63 void SendPackets(FakeNetworkPipe* pipe, int number_packets, int packet_size) {
73 RTC_DCHECK_GE(packet_size, sizeof(int)); 64 RTC_DCHECK_GE(packet_size, sizeof(int));
74 std::unique_ptr<uint8_t[]> packet(new uint8_t[packet_size]); 65 std::unique_ptr<uint8_t[]> packet(new uint8_t[packet_size]);
75 for (int i = 0; i < number_packets; ++i) { 66 for (int i = 0; i < number_packets; ++i) {
76 // Set a sequence number for the packets by 67 // Set a sequence number for the packets by
77 // using the first bytes in the packet. 68 // using the first bytes in the packet.
78 memcpy(packet.get(), &i, sizeof(int)); 69 memcpy(packet.get(), &i, sizeof(int));
79 pipe->SendPacket(packet.get(), packet_size); 70 pipe->SendPacket(packet.get(), packet_size);
80 } 71 }
81 } 72 }
82 73
83 int PacketTimeMs(int capacity_kbps, int packet_size) const { 74 int PacketTimeMs(int capacity_kbps, int packet_size) const {
84 return 8 * packet_size / capacity_kbps; 75 return 8 * packet_size / capacity_kbps;
85 } 76 }
86 77
87 SimulatedClock fake_clock_; 78 SimulatedClock fake_clock_;
88 std::unique_ptr<TestReceiver> receiver_;
89 }; 79 };
90 80
91 void DeleteMemory(uint8_t* data, int length) { delete [] data; }
92
93 // Test the capacity link and verify we get as many packets as we expect. 81 // Test the capacity link and verify we get as many packets as we expect.
94 TEST_F(FakeNetworkPipeTest, CapacityTest) { 82 TEST_F(FakeNetworkPipeTest, CapacityTest) {
95 FakeNetworkPipe::Config config; 83 FakeNetworkPipe::Config config;
96 config.queue_length_packets = 20; 84 config.queue_length_packets = 20;
97 config.link_capacity_kbps = 80; 85 config.link_capacity_kbps = 80;
98 std::unique_ptr<FakeNetworkPipe> pipe( 86 TestDemuxer* demuxer = new TestDemuxer();
99 new FakeNetworkPipe(&fake_clock_, config, MediaType::VIDEO)); 87 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
100 pipe->SetReceiver(receiver_.get()); 88 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
101 89
102 // Add 10 packets of 1000 bytes, = 80 kb, and verify it takes one second to 90 // Add 10 packets of 1000 bytes, = 80 kb, and verify it takes one second to
103 // get through the pipe. 91 // get through the pipe.
104 const int kNumPackets = 10; 92 const int kNumPackets = 10;
105 const int kPacketSize = 1000; 93 const int kPacketSize = 1000;
106 SendPackets(pipe.get(), kNumPackets , kPacketSize); 94 SendPackets(pipe.get(), kNumPackets, kPacketSize);
107 95
108 // Time to get one packet through the link. 96 // Time to get one packet through the link.
109 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps, 97 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps,
110 kPacketSize); 98 kPacketSize);
111 99
112 // Time haven't increased yet, so we souldn't get any packets. 100 // Time haven't increased yet, so we souldn't get any packets.
113 EXPECT_CALL(*receiver_, DeliverPacket(MediaType::VIDEO, _, _, _)).Times(0); 101 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(0);
114 pipe->Process(); 102 pipe->Process();
115 103
116 // Advance enough time to release one packet. 104 // Advance enough time to release one packet.
117 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs); 105 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
118 EXPECT_CALL(*receiver_, DeliverPacket(MediaType::VIDEO, _, _, _)).Times(1); 106 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
119 pipe->Process(); 107 pipe->Process();
120 108
121 // Release all but one packet 109 // Release all but one packet
122 fake_clock_.AdvanceTimeMilliseconds(9 * kPacketTimeMs - 1); 110 fake_clock_.AdvanceTimeMilliseconds(9 * kPacketTimeMs - 1);
123 EXPECT_CALL(*receiver_, DeliverPacket(MediaType::VIDEO, _, _, _)).Times(8); 111 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(8);
124 pipe->Process(); 112 pipe->Process();
125 113
126 // And the last one. 114 // And the last one.
127 fake_clock_.AdvanceTimeMilliseconds(1); 115 fake_clock_.AdvanceTimeMilliseconds(1);
128 EXPECT_CALL(*receiver_, DeliverPacket(MediaType::VIDEO, _, _, _)).Times(1); 116 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
129 pipe->Process(); 117 pipe->Process();
130 } 118 }
131 119
132 // Test the extra network delay. 120 // Test the extra network delay.
133 TEST_F(FakeNetworkPipeTest, ExtraDelayTest) { 121 TEST_F(FakeNetworkPipeTest, ExtraDelayTest) {
134 FakeNetworkPipe::Config config; 122 FakeNetworkPipe::Config config;
135 config.queue_length_packets = 20; 123 config.queue_length_packets = 20;
136 config.queue_delay_ms = 100; 124 config.queue_delay_ms = 100;
137 config.link_capacity_kbps = 80; 125 config.link_capacity_kbps = 80;
138 std::unique_ptr<FakeNetworkPipe> pipe( 126 TestDemuxer* demuxer = new TestDemuxer();
139 new FakeNetworkPipe(&fake_clock_, config, MediaType::AUDIO)); 127 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
140 pipe->SetReceiver(receiver_.get()); 128 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
141 129
142 const int kNumPackets = 2; 130 const int kNumPackets = 2;
143 const int kPacketSize = 1000; 131 const int kPacketSize = 1000;
144 SendPackets(pipe.get(), kNumPackets , kPacketSize); 132 SendPackets(pipe.get(), kNumPackets, kPacketSize);
145 133
146 // Time to get one packet through the link. 134 // Time to get one packet through the link.
147 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps, 135 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps,
148 kPacketSize); 136 kPacketSize);
149 137
150 // Increase more than kPacketTimeMs, but not more than the extra delay. 138 // Increase more than kPacketTimeMs, but not more than the extra delay.
151 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs); 139 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
152 EXPECT_CALL(*receiver_, DeliverPacket(MediaType::AUDIO, _, _, _)).Times(0); 140 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(0);
153 pipe->Process(); 141 pipe->Process();
154 142
155 // Advance the network delay to get the first packet. 143 // Advance the network delay to get the first packet.
156 fake_clock_.AdvanceTimeMilliseconds(config.queue_delay_ms); 144 fake_clock_.AdvanceTimeMilliseconds(config.queue_delay_ms);
157 EXPECT_CALL(*receiver_, DeliverPacket(MediaType::AUDIO, _, _, _)).Times(1); 145 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
158 pipe->Process(); 146 pipe->Process();
159 147
160 // Advance one more kPacketTimeMs to get the last packet. 148 // Advance one more kPacketTimeMs to get the last packet.
161 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs); 149 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
162 EXPECT_CALL(*receiver_, DeliverPacket(MediaType::AUDIO, _, _, _)).Times(1); 150 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
163 pipe->Process(); 151 pipe->Process();
164 } 152 }
165 153
166 // Test the number of buffers and packets are dropped when sending too many 154 // Test the number of buffers and packets are dropped when sending too many
167 // packets too quickly. 155 // packets too quickly.
168 TEST_F(FakeNetworkPipeTest, QueueLengthTest) { 156 TEST_F(FakeNetworkPipeTest, QueueLengthTest) {
169 FakeNetworkPipe::Config config; 157 FakeNetworkPipe::Config config;
170 config.queue_length_packets = 2; 158 config.queue_length_packets = 2;
171 config.link_capacity_kbps = 80; 159 config.link_capacity_kbps = 80;
172 std::unique_ptr<FakeNetworkPipe> pipe( 160 TestDemuxer* demuxer = new TestDemuxer();
173 new FakeNetworkPipe(&fake_clock_, config, MediaType::VIDEO)); 161 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
174 pipe->SetReceiver(receiver_.get()); 162 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
175 163
176 const int kPacketSize = 1000; 164 const int kPacketSize = 1000;
177 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps, 165 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps,
178 kPacketSize); 166 kPacketSize);
179 167
180 // Send three packets and verify only 2 are delivered. 168 // Send three packets and verify only 2 are delivered.
181 SendPackets(pipe.get(), 3, kPacketSize); 169 SendPackets(pipe.get(), 3, kPacketSize);
182 170
183 // Increase time enough to deliver all three packets, verify only two are 171 // Increase time enough to deliver all three packets, verify only two are
184 // delivered. 172 // delivered.
185 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs); 173 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs);
186 EXPECT_CALL(*receiver_, DeliverPacket(MediaType::VIDEO, _, _, _)).Times(2); 174 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(2);
187 pipe->Process(); 175 pipe->Process();
188 } 176 }
189 177
190 // Test we get statistics as expected. 178 // Test we get statistics as expected.
191 TEST_F(FakeNetworkPipeTest, StatisticsTest) { 179 TEST_F(FakeNetworkPipeTest, StatisticsTest) {
192 FakeNetworkPipe::Config config; 180 FakeNetworkPipe::Config config;
193 config.queue_length_packets = 2; 181 config.queue_length_packets = 2;
194 config.queue_delay_ms = 20; 182 config.queue_delay_ms = 20;
195 config.link_capacity_kbps = 80; 183 config.link_capacity_kbps = 80;
196 std::unique_ptr<FakeNetworkPipe> pipe( 184 TestDemuxer* demuxer = new TestDemuxer();
197 new FakeNetworkPipe(&fake_clock_, config, MediaType::VIDEO)); 185 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
198 pipe->SetReceiver(receiver_.get()); 186 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
199 187
200 const int kPacketSize = 1000; 188 const int kPacketSize = 1000;
201 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps, 189 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps,
202 kPacketSize); 190 kPacketSize);
203 191
204 // Send three packets and verify only 2 are delivered. 192 // Send three packets and verify only 2 are delivered.
205 SendPackets(pipe.get(), 3, kPacketSize); 193 SendPackets(pipe.get(), 3, kPacketSize);
206 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs + 194 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs +
207 config.queue_delay_ms); 195 config.queue_delay_ms);
208 196
209 EXPECT_CALL(*receiver_, DeliverPacket(MediaType::VIDEO, _, _, _)).Times(2); 197 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(2);
210 pipe->Process(); 198 pipe->Process();
211 199
212 // Packet 1: kPacketTimeMs + config.queue_delay_ms, 200 // Packet 1: kPacketTimeMs + config.queue_delay_ms,
213 // packet 2: 2 * kPacketTimeMs + config.queue_delay_ms => 170 ms average. 201 // packet 2: 2 * kPacketTimeMs + config.queue_delay_ms => 170 ms average.
214 EXPECT_EQ(pipe->AverageDelay(), 170); 202 EXPECT_EQ(pipe->AverageDelay(), 170);
215 EXPECT_EQ(pipe->sent_packets(), 2u); 203 EXPECT_EQ(pipe->sent_packets(), 2u);
216 EXPECT_EQ(pipe->dropped_packets(), 1u); 204 EXPECT_EQ(pipe->dropped_packets(), 1u);
217 EXPECT_EQ(pipe->PercentageLoss(), 1/3.f); 205 EXPECT_EQ(pipe->PercentageLoss(), 1/3.f);
218 } 206 }
219 207
220 // Change the link capacity half-way through the test and verify that the 208 // Change the link capacity half-way through the test and verify that the
221 // delivery times change accordingly. 209 // delivery times change accordingly.
222 TEST_F(FakeNetworkPipeTest, ChangingCapacityWithEmptyPipeTest) { 210 TEST_F(FakeNetworkPipeTest, ChangingCapacityWithEmptyPipeTest) {
223 FakeNetworkPipe::Config config; 211 FakeNetworkPipe::Config config;
224 config.queue_length_packets = 20; 212 config.queue_length_packets = 20;
225 config.link_capacity_kbps = 80; 213 config.link_capacity_kbps = 80;
226 std::unique_ptr<FakeNetworkPipe> pipe( 214 TestDemuxer* demuxer = new TestDemuxer();
227 new FakeNetworkPipe(&fake_clock_, config, MediaType::VIDEO)); 215 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
228 pipe->SetReceiver(receiver_.get()); 216 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
229 217
230 // Add 10 packets of 1000 bytes, = 80 kb, and verify it takes one second to 218 // Add 10 packets of 1000 bytes, = 80 kb, and verify it takes one second to
231 // get through the pipe. 219 // get through the pipe.
232 const int kNumPackets = 10; 220 const int kNumPackets = 10;
233 const int kPacketSize = 1000; 221 const int kPacketSize = 1000;
234 SendPackets(pipe.get(), kNumPackets, kPacketSize); 222 SendPackets(pipe.get(), kNumPackets, kPacketSize);
235 223
236 // Time to get one packet through the link. 224 // Time to get one packet through the link.
237 int packet_time_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize); 225 int packet_time_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
238 226
239 // Time hasn't increased yet, so we souldn't get any packets. 227 // Time hasn't increased yet, so we souldn't get any packets.
240 EXPECT_CALL(*receiver_, DeliverPacket(MediaType::VIDEO, _, _, _)).Times(0); 228 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(0);
241 pipe->Process(); 229 pipe->Process();
242 230
243 // Advance time in steps to release one packet at a time. 231 // Advance time in steps to release one packet at a time.
244 for (int i = 0; i < kNumPackets; ++i) { 232 for (int i = 0; i < kNumPackets; ++i) {
245 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms); 233 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms);
246 EXPECT_CALL(*receiver_, DeliverPacket(MediaType::VIDEO, _, _, _)).Times(1); 234 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
247 pipe->Process(); 235 pipe->Process();
248 } 236 }
249 237
250 // Change the capacity. 238 // Change the capacity.
251 config.link_capacity_kbps /= 2; // Reduce to 50%. 239 config.link_capacity_kbps /= 2; // Reduce to 50%.
252 pipe->SetConfig(config); 240 pipe->SetConfig(config);
253 241
254 // Add another 10 packets of 1000 bytes, = 80 kb, and verify it takes two 242 // Add another 10 packets of 1000 bytes, = 80 kb, and verify it takes two
255 // seconds to get them through the pipe. 243 // seconds to get them through the pipe.
256 SendPackets(pipe.get(), kNumPackets, kPacketSize); 244 SendPackets(pipe.get(), kNumPackets, kPacketSize);
257 245
258 // Time to get one packet through the link. 246 // Time to get one packet through the link.
259 packet_time_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize); 247 packet_time_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
260 248
261 // Time hasn't increased yet, so we souldn't get any packets. 249 // Time hasn't increased yet, so we souldn't get any packets.
262 EXPECT_CALL(*receiver_, DeliverPacket(MediaType::VIDEO, _, _, _)).Times(0); 250 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(0);
263 pipe->Process(); 251 pipe->Process();
264 252
265 // Advance time in steps to release one packet at a time. 253 // Advance time in steps to release one packet at a time.
266 for (int i = 0; i < kNumPackets; ++i) { 254 for (int i = 0; i < kNumPackets; ++i) {
267 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms); 255 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms);
268 EXPECT_CALL(*receiver_, DeliverPacket(MediaType::VIDEO, _, _, _)).Times(1); 256 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
269 pipe->Process(); 257 pipe->Process();
270 } 258 }
271 259
272 // Check that all the packets were sent. 260 // Check that all the packets were sent.
273 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->sent_packets()); 261 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->sent_packets());
274 fake_clock_.AdvanceTimeMilliseconds(pipe->TimeUntilNextProcess()); 262 fake_clock_.AdvanceTimeMilliseconds(pipe->TimeUntilNextProcess());
275 EXPECT_CALL(*receiver_, DeliverPacket(MediaType::VIDEO, _, _, _)).Times(0); 263 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(0);
276 pipe->Process(); 264 pipe->Process();
277 } 265 }
278 266
279 // Change the link capacity half-way through the test and verify that the 267 // Change the link capacity half-way through the test and verify that the
280 // delivery times change accordingly. 268 // delivery times change accordingly.
281 TEST_F(FakeNetworkPipeTest, ChangingCapacityWithPacketsInPipeTest) { 269 TEST_F(FakeNetworkPipeTest, ChangingCapacityWithPacketsInPipeTest) {
282 FakeNetworkPipe::Config config; 270 FakeNetworkPipe::Config config;
283 config.queue_length_packets = 20; 271 config.queue_length_packets = 20;
284 config.link_capacity_kbps = 80; 272 config.link_capacity_kbps = 80;
285 std::unique_ptr<FakeNetworkPipe> pipe( 273 TestDemuxer* demuxer = new TestDemuxer();
286 new FakeNetworkPipe(&fake_clock_, config, MediaType::AUDIO)); 274 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
287 pipe->SetReceiver(receiver_.get()); 275 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
288 276
289 // Add 10 packets of 1000 bytes, = 80 kb. 277 // Add 10 packets of 1000 bytes, = 80 kb.
290 const int kNumPackets = 10; 278 const int kNumPackets = 10;
291 const int kPacketSize = 1000; 279 const int kPacketSize = 1000;
292 SendPackets(pipe.get(), kNumPackets, kPacketSize); 280 SendPackets(pipe.get(), kNumPackets, kPacketSize);
293 281
294 // Time to get one packet through the link at the initial speed. 282 // Time to get one packet through the link at the initial speed.
295 int packet_time_1_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize); 283 int packet_time_1_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
296 284
297 // Change the capacity. 285 // Change the capacity.
298 config.link_capacity_kbps *= 2; // Double the capacity. 286 config.link_capacity_kbps *= 2; // Double the capacity.
299 pipe->SetConfig(config); 287 pipe->SetConfig(config);
300 288
301 // Add another 10 packets of 1000 bytes, = 80 kb, and verify it takes two 289 // Add another 10 packets of 1000 bytes, = 80 kb, and verify it takes two
302 // seconds to get them through the pipe. 290 // seconds to get them through the pipe.
303 SendPackets(pipe.get(), kNumPackets, kPacketSize); 291 SendPackets(pipe.get(), kNumPackets, kPacketSize);
304 292
305 // Time to get one packet through the link at the new capacity. 293 // Time to get one packet through the link at the new capacity.
306 int packet_time_2_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize); 294 int packet_time_2_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
307 295
308 // Time hasn't increased yet, so we souldn't get any packets. 296 // Time hasn't increased yet, so we souldn't get any packets.
309 EXPECT_CALL(*receiver_, DeliverPacket(MediaType::AUDIO, _, _, _)).Times(0); 297 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(0);
310 pipe->Process(); 298 pipe->Process();
311 299
312 // Advance time in steps to release one packet at a time. 300 // Advance time in steps to release one packet at a time.
313 for (int i = 0; i < kNumPackets; ++i) { 301 for (int i = 0; i < kNumPackets; ++i) {
314 fake_clock_.AdvanceTimeMilliseconds(packet_time_1_ms); 302 fake_clock_.AdvanceTimeMilliseconds(packet_time_1_ms);
315 EXPECT_CALL(*receiver_, DeliverPacket(MediaType::AUDIO, _, _, _)).Times(1); 303 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
316 pipe->Process(); 304 pipe->Process();
317 } 305 }
318 306
319 // Advance time in steps to release one packet at a time. 307 // Advance time in steps to release one packet at a time.
320 for (int i = 0; i < kNumPackets; ++i) { 308 for (int i = 0; i < kNumPackets; ++i) {
321 fake_clock_.AdvanceTimeMilliseconds(packet_time_2_ms); 309 fake_clock_.AdvanceTimeMilliseconds(packet_time_2_ms);
322 EXPECT_CALL(*receiver_, DeliverPacket(MediaType::AUDIO, _, _, _)).Times(1); 310 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
323 pipe->Process(); 311 pipe->Process();
324 } 312 }
325 313
326 // Check that all the packets were sent. 314 // Check that all the packets were sent.
327 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->sent_packets()); 315 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->sent_packets());
328 fake_clock_.AdvanceTimeMilliseconds(pipe->TimeUntilNextProcess()); 316 fake_clock_.AdvanceTimeMilliseconds(pipe->TimeUntilNextProcess());
329 EXPECT_CALL(*receiver_, DeliverPacket(MediaType::AUDIO, _, _, _)).Times(0); 317 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(0);
330 pipe->Process(); 318 pipe->Process();
331 } 319 }
332 320
333 // At first disallow reordering and then allow reordering. 321 // At first disallow reordering and then allow reordering.
334 TEST_F(FakeNetworkPipeTest, DisallowReorderingThenAllowReordering) { 322 TEST_F(FakeNetworkPipeTest, DisallowReorderingThenAllowReordering) {
335 FakeNetworkPipe::Config config; 323 FakeNetworkPipe::Config config;
336 config.queue_length_packets = 1000; 324 config.queue_length_packets = 1000;
337 config.link_capacity_kbps = 800; 325 config.link_capacity_kbps = 800;
338 config.queue_delay_ms = 100; 326 config.queue_delay_ms = 100;
339 config.delay_standard_deviation_ms = 10; 327 config.delay_standard_deviation_ms = 10;
340 std::unique_ptr<FakeNetworkPipe> pipe( 328 ReorderTestDemuxer* demuxer = new ReorderTestDemuxer();
341 new FakeNetworkPipe(&fake_clock_, config, MediaType::VIDEO)); 329 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
342 ReorderTestReceiver* receiver = new ReorderTestReceiver(); 330 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
343 receiver_.reset(receiver);
344 pipe->SetReceiver(receiver_.get());
345 331
346 const uint32_t kNumPackets = 100; 332 const uint32_t kNumPackets = 100;
347 const int kPacketSize = 10; 333 const int kPacketSize = 10;
348 SendPackets(pipe.get(), kNumPackets, kPacketSize); 334 SendPackets(pipe.get(), kNumPackets, kPacketSize);
349 fake_clock_.AdvanceTimeMilliseconds(1000); 335 fake_clock_.AdvanceTimeMilliseconds(1000);
350 pipe->Process(); 336 pipe->Process();
351 337
352 // Confirm that all packets have been delivered in order. 338 // Confirm that all packets have been delivered in order.
353 EXPECT_EQ(kNumPackets, receiver->delivered_sequence_numbers_.size()); 339 EXPECT_EQ(kNumPackets, demuxer->delivered_sequence_numbers_.size());
354 int last_seq_num = -1; 340 int last_seq_num = -1;
355 for (int seq_num : receiver->delivered_sequence_numbers_) { 341 for (int seq_num : demuxer->delivered_sequence_numbers_) {
356 EXPECT_GT(seq_num, last_seq_num); 342 EXPECT_GT(seq_num, last_seq_num);
357 last_seq_num = seq_num; 343 last_seq_num = seq_num;
358 } 344 }
359 345
360 config.allow_reordering = true; 346 config.allow_reordering = true;
361 pipe->SetConfig(config); 347 pipe->SetConfig(config);
362 SendPackets(pipe.get(), kNumPackets, kPacketSize); 348 SendPackets(pipe.get(), kNumPackets, kPacketSize);
363 fake_clock_.AdvanceTimeMilliseconds(1000); 349 fake_clock_.AdvanceTimeMilliseconds(1000);
364 receiver->delivered_sequence_numbers_.clear(); 350 demuxer->delivered_sequence_numbers_.clear();
365 pipe->Process(); 351 pipe->Process();
366 352
367 // Confirm that all packets have been delivered 353 // Confirm that all packets have been delivered
368 // and that reordering has occured. 354 // and that reordering has occured.
369 EXPECT_EQ(kNumPackets, receiver->delivered_sequence_numbers_.size()); 355 EXPECT_EQ(kNumPackets, demuxer->delivered_sequence_numbers_.size());
370 bool reordering_has_occured = false; 356 bool reordering_has_occured = false;
371 last_seq_num = -1; 357 last_seq_num = -1;
372 for (int seq_num : receiver->delivered_sequence_numbers_) { 358 for (int seq_num : demuxer->delivered_sequence_numbers_) {
373 if (last_seq_num > seq_num) { 359 if (last_seq_num > seq_num) {
374 reordering_has_occured = true; 360 reordering_has_occured = true;
375 break; 361 break;
376 } 362 }
377 last_seq_num = seq_num; 363 last_seq_num = seq_num;
378 } 364 }
379 EXPECT_TRUE(reordering_has_occured); 365 EXPECT_TRUE(reordering_has_occured);
380 } 366 }
381 367
382 TEST_F(FakeNetworkPipeTest, BurstLoss) { 368 TEST_F(FakeNetworkPipeTest, BurstLoss) {
383 const int kLossPercent = 5; 369 const int kLossPercent = 5;
384 const int kAvgBurstLength = 3; 370 const int kAvgBurstLength = 3;
385 const int kNumPackets = 10000; 371 const int kNumPackets = 10000;
386 const int kPacketSize = 10; 372 const int kPacketSize = 10;
387 373
388 FakeNetworkPipe::Config config; 374 FakeNetworkPipe::Config config;
389 config.queue_length_packets = kNumPackets; 375 config.queue_length_packets = kNumPackets;
390 config.loss_percent = kLossPercent; 376 config.loss_percent = kLossPercent;
391 config.avg_burst_loss_length = kAvgBurstLength; 377 config.avg_burst_loss_length = kAvgBurstLength;
392 std::unique_ptr<FakeNetworkPipe> pipe( 378 ReorderTestDemuxer* demuxer = new ReorderTestDemuxer();
393 new FakeNetworkPipe(&fake_clock_, config, MediaType::VIDEO)); 379 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
394 ReorderTestReceiver* receiver = new ReorderTestReceiver(); 380 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
395 receiver_.reset(receiver);
396 pipe->SetReceiver(receiver_.get());
397 381
398 SendPackets(pipe.get(), kNumPackets, kPacketSize); 382 SendPackets(pipe.get(), kNumPackets, kPacketSize);
399 fake_clock_.AdvanceTimeMilliseconds(1000); 383 fake_clock_.AdvanceTimeMilliseconds(1000);
400 pipe->Process(); 384 pipe->Process();
401 385
402 // Check that the average loss is |kLossPercent| percent. 386 // Check that the average loss is |kLossPercent| percent.
403 int lost_packets = kNumPackets - receiver->delivered_sequence_numbers_.size(); 387 int lost_packets = kNumPackets - demuxer->delivered_sequence_numbers_.size();
404 double loss_fraction = lost_packets / static_cast<double>(kNumPackets); 388 double loss_fraction = lost_packets / static_cast<double>(kNumPackets);
405 389
406 EXPECT_NEAR(kLossPercent / 100.0, loss_fraction, 0.05); 390 EXPECT_NEAR(kLossPercent / 100.0, loss_fraction, 0.05);
407 391
408 // Find the number of bursts that has occurred. 392 // Find the number of bursts that has occurred.
409 size_t received_packets = receiver->delivered_sequence_numbers_.size(); 393 size_t received_packets = demuxer->delivered_sequence_numbers_.size();
410 int num_bursts = 0; 394 int num_bursts = 0;
411 for (size_t i = 0; i < received_packets - 1; ++i) { 395 for (size_t i = 0; i < received_packets - 1; ++i) {
412 int diff = receiver->delivered_sequence_numbers_[i + 1] - 396 int diff = demuxer->delivered_sequence_numbers_[i + 1] -
413 receiver->delivered_sequence_numbers_[i]; 397 demuxer->delivered_sequence_numbers_[i];
414 if (diff > 1) 398 if (diff > 1)
415 ++num_bursts; 399 ++num_bursts;
416 } 400 }
417 401
418 double average_burst_length = static_cast<double>(lost_packets) / num_bursts; 402 double average_burst_length = static_cast<double>(lost_packets) / num_bursts;
419 403
420 EXPECT_NEAR(kAvgBurstLength, average_burst_length, 0.3); 404 EXPECT_NEAR(kAvgBurstLength, average_burst_length, 0.3);
421 } 405 }
406
407 TEST_F(FakeNetworkPipeTest, SetReceiver) {
408 FakeNetworkPipe::Config config;
409 TestDemuxer* demuxer = new TestDemuxer();
410 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
411 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
412 MockReceiver packet_receiver;
413 EXPECT_CALL(*demuxer, SetReceiver(&packet_receiver)).Times(1);
414 pipe->SetReceiver(&packet_receiver);
415 }
416
417 TEST(DemuxerImplTest, Demuxing) {
418 constexpr uint8_t kVideoPayloadType = 100;
419 constexpr uint8_t kAudioPayloadType = 101;
420 constexpr int64_t kTimeNow = 12345;
421 constexpr int64_t kArrivalTime = kTimeNow - 1;
422 constexpr size_t kPacketSize = 10;
423 DemuxerImpl demuxer({{kVideoPayloadType, MediaType::VIDEO},
424 {kAudioPayloadType, MediaType::AUDIO}});
425
426 MockReceiver mock_receiver;
427 demuxer.SetReceiver(&mock_receiver);
428
429 std::vector<uint8_t> data(kPacketSize);
430 data[1] = kVideoPayloadType;
431 std::unique_ptr<NetworkPacket> packet(
432 new NetworkPacket(&data[0], kPacketSize, kTimeNow, kArrivalTime));
433 EXPECT_CALL(mock_receiver, DeliverPacket(MediaType::VIDEO, _, _, _))
434 .WillOnce(Return(PacketReceiver::DELIVERY_OK));
435 demuxer.DeliverPacket(packet.get(), PacketTime());
436
437 data[1] = kAudioPayloadType;
438 packet.reset(
439 new NetworkPacket(&data[0], kPacketSize, kTimeNow, kArrivalTime));
440 EXPECT_CALL(mock_receiver, DeliverPacket(MediaType::AUDIO, _, _, _))
441 .WillOnce(Return(PacketReceiver::DELIVERY_OK));
442 demuxer.DeliverPacket(packet.get(), PacketTime());
443 }
444
422 } // namespace webrtc 445 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698