Revert "Omit calls to set composing region when pasting image."
[chromium-blink-merge.git] / media / cast / net / pacing / paced_sender_unittest.cc
blob2387d4278d74df601ccb24f20affcf8ec2273017
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <stdint.h>
7 #include "base/big_endian.h"
8 #include "base/test/simple_test_tick_clock.h"
9 #include "media/cast/logging/logging_impl.h"
10 #include "media/cast/logging/simple_event_subscriber.h"
11 #include "media/cast/net/pacing/paced_sender.h"
12 #include "media/cast/test/fake_single_thread_task_runner.h"
13 #include "testing/gmock/include/gmock/gmock.h"
15 using testing::_;
17 namespace media {
18 namespace cast {
19 namespace {
21 static const uint8 kValue = 123;
22 static const size_t kSize1 = 101;
23 static const size_t kSize2 = 102;
24 static const size_t kSize3 = 103;
25 static const size_t kSize4 = 104;
26 static const size_t kNackSize = 105;
27 static const int64 kStartMillisecond = INT64_C(12345678900000);
28 static const uint32 kVideoSsrc = 0x1234;
29 static const uint32 kAudioSsrc = 0x5678;
31 class TestPacketSender : public PacketSender {
32 public:
33 TestPacketSender() : bytes_sent_(0) {}
35 bool SendPacket(PacketRef packet, const base::Closure& cb) final {
36 EXPECT_FALSE(expected_packet_size_.empty());
37 size_t expected_packet_size = expected_packet_size_.front();
38 expected_packet_size_.pop_front();
39 EXPECT_EQ(expected_packet_size, packet->data.size());
40 bytes_sent_ += packet->data.size();
41 return true;
44 int64 GetBytesSent() final { return bytes_sent_; }
46 void AddExpectedSize(int expected_packet_size, int repeat_count) {
47 for (int i = 0; i < repeat_count; ++i) {
48 expected_packet_size_.push_back(expected_packet_size);
52 public:
53 std::list<int> expected_packet_size_;
54 int64 bytes_sent_;
56 DISALLOW_COPY_AND_ASSIGN(TestPacketSender);
59 class PacedSenderTest : public ::testing::Test {
60 protected:
61 PacedSenderTest() {
62 logging_.AddRawEventSubscriber(&subscriber_);
63 testing_clock_.Advance(
64 base::TimeDelta::FromMilliseconds(kStartMillisecond));
65 task_runner_ = new test::FakeSingleThreadTaskRunner(&testing_clock_);
66 paced_sender_.reset(new PacedSender(kTargetBurstSize,
67 kMaxBurstSize,
68 &testing_clock_,
69 &logging_,
70 &mock_transport_,
71 task_runner_));
72 paced_sender_->RegisterAudioSsrc(kAudioSsrc);
73 paced_sender_->RegisterVideoSsrc(kVideoSsrc);
76 ~PacedSenderTest() override {
77 logging_.RemoveRawEventSubscriber(&subscriber_);
80 static void UpdateCastTransportStatus(CastTransportStatus status) {
81 NOTREACHED();
84 SendPacketVector CreateSendPacketVector(size_t packet_size,
85 int num_of_packets_in_frame,
86 bool audio) {
87 DCHECK_GE(packet_size, 12u);
88 SendPacketVector packets;
89 base::TimeTicks frame_tick = testing_clock_.NowTicks();
90 // Advance the clock so that we don't get the same frame_tick
91 // next time this function is called.
92 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(1));
93 for (int i = 0; i < num_of_packets_in_frame; ++i) {
94 PacketKey key = PacedPacketSender::MakePacketKey(
95 frame_tick,
96 audio ? kAudioSsrc : kVideoSsrc, // ssrc
97 i);
99 PacketRef packet(new base::RefCountedData<Packet>);
100 packet->data.resize(packet_size, kValue);
101 // Write ssrc to packet so that it can be recognized as a
102 // "video frame" for logging purposes.
103 base::BigEndianWriter writer(
104 reinterpret_cast<char*>(&packet->data[8]), 4);
105 bool success = writer.WriteU32(audio ? kAudioSsrc : kVideoSsrc);
106 DCHECK(success);
107 packets.push_back(std::make_pair(key, packet));
109 return packets;
112 // Use this function to drain the packet list in PacedSender without having
113 // to test the pacing implementation details.
114 bool RunUntilEmpty(int max_tries) {
115 for (int i = 0; i < max_tries; i++) {
116 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
117 task_runner_->RunTasks();
118 if (mock_transport_.expected_packet_size_.empty())
119 return true;
120 i++;
123 return mock_transport_.expected_packet_size_.empty();
126 LoggingImpl logging_;
127 SimpleEventSubscriber subscriber_;
128 base::SimpleTestTickClock testing_clock_;
129 TestPacketSender mock_transport_;
130 scoped_refptr<test::FakeSingleThreadTaskRunner> task_runner_;
131 scoped_ptr<PacedSender> paced_sender_;
133 DISALLOW_COPY_AND_ASSIGN(PacedSenderTest);
136 } // namespace
138 TEST_F(PacedSenderTest, PassThroughRtcp) {
139 mock_transport_.AddExpectedSize(kSize1, 2);
140 SendPacketVector packets = CreateSendPacketVector(kSize1, 1, true);
142 EXPECT_TRUE(paced_sender_->SendPackets(packets));
143 EXPECT_TRUE(paced_sender_->ResendPackets(packets, DedupInfo()));
145 mock_transport_.AddExpectedSize(kSize2, 1);
146 Packet tmp(kSize2, kValue);
147 EXPECT_TRUE(paced_sender_->SendRtcpPacket(
149 new base::RefCountedData<Packet>(tmp)));
152 TEST_F(PacedSenderTest, BasicPace) {
153 int num_of_packets = 27;
154 SendPacketVector packets = CreateSendPacketVector(kSize1,
155 num_of_packets,
156 false);
158 mock_transport_.AddExpectedSize(kSize1, 10);
159 EXPECT_TRUE(paced_sender_->SendPackets(packets));
161 // Check that we get the next burst.
162 mock_transport_.AddExpectedSize(kSize1, 10);
164 base::TimeDelta timeout = base::TimeDelta::FromMilliseconds(10);
165 testing_clock_.Advance(timeout);
166 task_runner_->RunTasks();
168 // If we call process too early make sure we don't send any packets.
169 timeout = base::TimeDelta::FromMilliseconds(5);
170 testing_clock_.Advance(timeout);
171 task_runner_->RunTasks();
173 // Check that we get the next burst.
174 mock_transport_.AddExpectedSize(kSize1, 7);
175 testing_clock_.Advance(timeout);
176 task_runner_->RunTasks();
178 // Check that we don't get any more packets.
179 EXPECT_TRUE(RunUntilEmpty(3));
181 std::vector<PacketEvent> packet_events;
182 subscriber_.GetPacketEventsAndReset(&packet_events);
183 EXPECT_EQ(num_of_packets, static_cast<int>(packet_events.size()));
184 int sent_to_network_event_count = 0;
185 for (std::vector<PacketEvent>::iterator it = packet_events.begin();
186 it != packet_events.end();
187 ++it) {
188 if (it->type == PACKET_SENT_TO_NETWORK)
189 sent_to_network_event_count++;
190 else
191 FAIL() << "Got unexpected event type " << CastLoggingToString(it->type);
193 EXPECT_EQ(num_of_packets, sent_to_network_event_count);
196 TEST_F(PacedSenderTest, PaceWithNack) {
197 // Testing what happen when we get multiple NACK requests for a fully lost
198 // frames just as we sent the first packets in a frame.
199 int num_of_packets_in_frame = 12;
200 int num_of_packets_in_nack = 12;
202 SendPacketVector nack_packets =
203 CreateSendPacketVector(kNackSize, num_of_packets_in_nack, false);
205 SendPacketVector first_frame_packets =
206 CreateSendPacketVector(kSize1, num_of_packets_in_frame, false);
208 SendPacketVector second_frame_packets =
209 CreateSendPacketVector(kSize2, num_of_packets_in_frame, true);
211 // Check that the first burst of the frame go out on the wire.
212 mock_transport_.AddExpectedSize(kSize1, 10);
213 EXPECT_TRUE(paced_sender_->SendPackets(first_frame_packets));
215 // Add first NACK request.
216 EXPECT_TRUE(paced_sender_->ResendPackets(nack_packets, DedupInfo()));
218 // Check that we get the first NACK burst.
219 mock_transport_.AddExpectedSize(kNackSize, 10);
220 base::TimeDelta timeout = base::TimeDelta::FromMilliseconds(10);
221 testing_clock_.Advance(timeout);
222 task_runner_->RunTasks();
224 // Add second NACK request.
225 EXPECT_TRUE(paced_sender_->ResendPackets(nack_packets, DedupInfo()));
227 // Check that we get the next NACK burst.
228 mock_transport_.AddExpectedSize(kNackSize, 10);
229 testing_clock_.Advance(timeout);
230 task_runner_->RunTasks();
232 // End of NACK plus two packets from the oldest frame.
233 // Note that two of the NACKs have been de-duped.
234 mock_transport_.AddExpectedSize(kNackSize, 2);
235 mock_transport_.AddExpectedSize(kSize1, 2);
236 testing_clock_.Advance(timeout);
237 task_runner_->RunTasks();
239 // Add second frame.
240 // Make sure we don't delay the second frame due to the previous packets.
241 mock_transport_.AddExpectedSize(kSize2, 10);
242 EXPECT_TRUE(paced_sender_->SendPackets(second_frame_packets));
244 // Last packets of frame 2.
245 mock_transport_.AddExpectedSize(kSize2, 2);
246 testing_clock_.Advance(timeout);
247 task_runner_->RunTasks();
249 // No more packets.
250 EXPECT_TRUE(RunUntilEmpty(5));
252 std::vector<PacketEvent> packet_events;
253 subscriber_.GetPacketEventsAndReset(&packet_events);
254 int expected_video_network_event_count = num_of_packets_in_frame;
255 int expected_video_retransmitted_event_count = 2 * num_of_packets_in_nack;
256 expected_video_retransmitted_event_count -= 2; // 2 packets deduped
257 int expected_audio_network_event_count = num_of_packets_in_frame;
258 EXPECT_EQ(expected_video_network_event_count +
259 expected_video_retransmitted_event_count +
260 expected_audio_network_event_count,
261 static_cast<int>(packet_events.size()));
262 int audio_network_event_count = 0;
263 int video_network_event_count = 0;
264 int video_retransmitted_event_count = 0;
265 for (std::vector<PacketEvent>::iterator it = packet_events.begin();
266 it != packet_events.end();
267 ++it) {
268 if (it->type == PACKET_SENT_TO_NETWORK) {
269 if (it->media_type == VIDEO_EVENT)
270 video_network_event_count++;
271 else
272 audio_network_event_count++;
273 } else if (it->type == PACKET_RETRANSMITTED) {
274 if (it->media_type == VIDEO_EVENT)
275 video_retransmitted_event_count++;
276 } else {
277 FAIL() << "Got unexpected event type " << CastLoggingToString(it->type);
280 EXPECT_EQ(expected_audio_network_event_count, audio_network_event_count);
281 EXPECT_EQ(expected_video_network_event_count, video_network_event_count);
282 EXPECT_EQ(expected_video_retransmitted_event_count,
283 video_retransmitted_event_count);
286 TEST_F(PacedSenderTest, PaceWith60fps) {
287 // Testing what happen when we get multiple NACK requests for a fully lost
288 // frames just as we sent the first packets in a frame.
289 int num_of_packets_in_frame = 17;
291 SendPacketVector first_frame_packets =
292 CreateSendPacketVector(kSize1, num_of_packets_in_frame, false);
294 SendPacketVector second_frame_packets =
295 CreateSendPacketVector(kSize2, num_of_packets_in_frame, false);
297 SendPacketVector third_frame_packets =
298 CreateSendPacketVector(kSize3, num_of_packets_in_frame, false);
300 SendPacketVector fourth_frame_packets =
301 CreateSendPacketVector(kSize4, num_of_packets_in_frame, false);
303 base::TimeDelta timeout_10ms = base::TimeDelta::FromMilliseconds(10);
305 // Check that the first burst of the frame go out on the wire.
306 mock_transport_.AddExpectedSize(kSize1, 10);
307 EXPECT_TRUE(paced_sender_->SendPackets(first_frame_packets));
309 mock_transport_.AddExpectedSize(kSize1, 7);
310 testing_clock_.Advance(timeout_10ms);
311 task_runner_->RunTasks();
313 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(6));
315 // Add second frame, after 16 ms.
316 mock_transport_.AddExpectedSize(kSize2, 3);
317 EXPECT_TRUE(paced_sender_->SendPackets(second_frame_packets));
318 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(4));
320 mock_transport_.AddExpectedSize(kSize2, 10);
321 testing_clock_.Advance(timeout_10ms);
322 task_runner_->RunTasks();
324 mock_transport_.AddExpectedSize(kSize2, 4);
325 testing_clock_.Advance(timeout_10ms);
326 task_runner_->RunTasks();
328 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(3));
330 // Add third frame, after 33 ms.
331 mock_transport_.AddExpectedSize(kSize3, 6);
332 EXPECT_TRUE(paced_sender_->SendPackets(third_frame_packets));
334 mock_transport_.AddExpectedSize(kSize3, 10);
335 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(7));
336 task_runner_->RunTasks();
338 // Add fourth frame, after 50 ms.
339 EXPECT_TRUE(paced_sender_->SendPackets(fourth_frame_packets));
341 mock_transport_.AddExpectedSize(kSize3, 1);
342 mock_transport_.AddExpectedSize(kSize4, 9);
343 testing_clock_.Advance(timeout_10ms);
344 task_runner_->RunTasks();
346 mock_transport_.AddExpectedSize(kSize4, 8);
347 testing_clock_.Advance(timeout_10ms);
348 task_runner_->RunTasks();
350 testing_clock_.Advance(timeout_10ms);
351 task_runner_->RunTasks();
353 testing_clock_.Advance(timeout_10ms);
354 task_runner_->RunTasks();
356 // No more packets.
357 EXPECT_TRUE(RunUntilEmpty(5));
360 TEST_F(PacedSenderTest, SendPriority) {
361 // Actual order to the network is:
362 // 1. Video packets x 10.
363 // 2. RTCP packet x 1.
364 // 3. Audio packet x 1.
365 // 4. Video retransmission packet x 10.
366 // 5. Video packet x 10.
367 mock_transport_.AddExpectedSize(kSize2, 10); // Normal video packets.
368 mock_transport_.AddExpectedSize(kSize3, 1); // RTCP packet.
369 mock_transport_.AddExpectedSize(kSize1, 1); // Audio packet.
370 mock_transport_.AddExpectedSize(kSize4, 10); // Resend video packets.
371 mock_transport_.AddExpectedSize(kSize2, 10); // Normal video packets.
373 paced_sender_->RegisterPrioritySsrc(kAudioSsrc);
375 // Retransmission packets with the earlier timestamp.
376 SendPacketVector resend_packets =
377 CreateSendPacketVector(kSize4, 10, false);
378 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
380 // Send 20 normal video packets. Only 10 will be sent in this
381 // call, the rest will be sitting in the queue waiting for pacing.
382 EXPECT_TRUE(paced_sender_->SendPackets(
383 CreateSendPacketVector(kSize2, 20, false)));
385 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
387 // Send normal audio packet. This is queued and will be sent
388 // earlier than video packets.
389 EXPECT_TRUE(paced_sender_->SendPackets(
390 CreateSendPacketVector(kSize1, 1, true)));
392 // Send RTCP packet. This is queued and will be sent first.
393 EXPECT_TRUE(paced_sender_->SendRtcpPacket(
394 kVideoSsrc,
395 new base::RefCountedData<Packet>(Packet(kSize3, kValue))));
397 // Resend video packets. This is queued and will be sent
398 // earlier than normal video packets.
399 EXPECT_TRUE(paced_sender_->ResendPackets(resend_packets, DedupInfo()));
401 // Roll the clock. Queued packets will be sent in this order:
402 // 1. RTCP packet x 1.
403 // 2. Audio packet x 1.
404 // 3. Video retransmission packet x 10.
405 // 4. Video packet x 10.
406 task_runner_->RunTasks();
407 EXPECT_TRUE(RunUntilEmpty(4));
410 TEST_F(PacedSenderTest, GetLastByteSent) {
411 mock_transport_.AddExpectedSize(kSize1, 4);
413 SendPacketVector packets1 = CreateSendPacketVector(kSize1, 1, true);
414 SendPacketVector packets2 = CreateSendPacketVector(kSize1, 1, false);
416 EXPECT_TRUE(paced_sender_->SendPackets(packets1));
417 EXPECT_EQ(static_cast<int64>(kSize1),
418 paced_sender_->GetLastByteSentForPacket(packets1[0].first));
419 EXPECT_EQ(static_cast<int64>(kSize1),
420 paced_sender_->GetLastByteSentForSsrc(kAudioSsrc));
421 EXPECT_EQ(0, paced_sender_->GetLastByteSentForSsrc(kVideoSsrc));
423 EXPECT_TRUE(paced_sender_->SendPackets(packets2));
424 EXPECT_EQ(static_cast<int64>(2 * kSize1),
425 paced_sender_->GetLastByteSentForPacket(packets2[0].first));
426 EXPECT_EQ(static_cast<int64>(kSize1),
427 paced_sender_->GetLastByteSentForSsrc(kAudioSsrc));
428 EXPECT_EQ(static_cast<int64>(2 * kSize1),
429 paced_sender_->GetLastByteSentForSsrc(kVideoSsrc));
431 EXPECT_TRUE(paced_sender_->ResendPackets(packets1, DedupInfo()));
432 EXPECT_EQ(static_cast<int64>(3 * kSize1),
433 paced_sender_->GetLastByteSentForPacket(packets1[0].first));
434 EXPECT_EQ(static_cast<int64>(3 * kSize1),
435 paced_sender_->GetLastByteSentForSsrc(kAudioSsrc));
436 EXPECT_EQ(static_cast<int64>(2 * kSize1),
437 paced_sender_->GetLastByteSentForSsrc(kVideoSsrc));
439 EXPECT_TRUE(paced_sender_->ResendPackets(packets2, DedupInfo()));
440 EXPECT_EQ(static_cast<int64>(4 * kSize1),
441 paced_sender_->GetLastByteSentForPacket(packets2[0].first));
442 EXPECT_EQ(static_cast<int64>(3 * kSize1),
443 paced_sender_->GetLastByteSentForSsrc(kAudioSsrc));
444 EXPECT_EQ(static_cast<int64>(4 * kSize1),
445 paced_sender_->GetLastByteSentForSsrc(kVideoSsrc));
448 TEST_F(PacedSenderTest, DedupWithResendInterval) {
449 mock_transport_.AddExpectedSize(kSize1, 2);
451 SendPacketVector packets = CreateSendPacketVector(kSize1, 1, true);
452 EXPECT_TRUE(paced_sender_->SendPackets(packets));
453 testing_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
455 DedupInfo dedup_info;
456 dedup_info.resend_interval = base::TimeDelta::FromMilliseconds(20);
458 // This packet will not be sent.
459 EXPECT_TRUE(paced_sender_->ResendPackets(packets, dedup_info));
460 EXPECT_EQ(static_cast<int64>(kSize1), mock_transport_.GetBytesSent());
462 dedup_info.resend_interval = base::TimeDelta::FromMilliseconds(5);
463 EXPECT_TRUE(paced_sender_->ResendPackets(packets, dedup_info));
464 EXPECT_EQ(static_cast<int64>(2 * kSize1), mock_transport_.GetBytesSent());
467 } // namespace cast
468 } // namespace media