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 "base/memory/scoped_ptr.h"
6 #include "base/test/simple_test_tick_clock.h"
7 #include "media/cast/cast_defines.h"
8 #include "media/cast/cast_environment.h"
9 #include "media/cast/net/cast_transport_defines.h"
10 #include "media/cast/net/pacing/paced_sender.h"
11 #include "media/cast/net/rtcp/receiver_rtcp_event_subscriber.h"
12 #include "media/cast/net/rtcp/rtcp_builder.h"
13 #include "media/cast/net/rtcp/rtcp_utility.h"
14 #include "media/cast/net/rtcp/test_rtcp_packet_builder.h"
15 #include "testing/gmock/include/gmock/gmock.h"
21 static const uint32 kSendingSsrc
= 0x12345678;
22 static const uint32 kMediaSsrc
= 0x87654321;
23 static const base::TimeDelta kDefaultDelay
=
24 base::TimeDelta::FromMilliseconds(100);
26 RtcpReportBlock
GetReportBlock() {
27 RtcpReportBlock report_block
;
28 // Initialize remote_ssrc to a "clearly illegal" value.
29 report_block
.remote_ssrc
= 0xDEAD;
30 report_block
.media_ssrc
= kMediaSsrc
; // SSRC of the RTP packet sender.
31 report_block
.fraction_lost
= kLoss
>> 24;
32 report_block
.cumulative_lost
= kLoss
; // 24 bits valid.
33 report_block
.extended_high_sequence_number
= kExtendedMax
;
34 report_block
.jitter
= kTestJitter
;
35 report_block
.last_sr
= kLastSr
;
36 report_block
.delay_since_last_sr
= kDelayLastSr
;
43 class RtcpBuilderTest
: public ::testing::Test
{
46 : rtcp_builder_(new RtcpBuilder(kSendingSsrc
)) {}
48 void ExpectPacketEQ(scoped_ptr
<Packet
> golden_packet
,
51 EXPECT_EQ(golden_packet
->size(), packet
->data
.size());
52 if (golden_packet
->size() == packet
->data
.size()) {
53 for (size_t x
= 0; x
< golden_packet
->size(); x
++) {
54 EXPECT_EQ((*golden_packet
)[x
], packet
->data
[x
]) <<
55 "x = " << x
<< " / " << golden_packet
->size();
56 if ((*golden_packet
)[x
] != packet
->data
[x
]) {
64 scoped_ptr
<RtcpBuilder
> rtcp_builder_
;
66 DISALLOW_COPY_AND_ASSIGN(RtcpBuilderTest
);
69 TEST_F(RtcpBuilderTest
, RtcpReceiverReport
) {
70 // Receiver report with report block.
71 TestRtcpPacketBuilder p2
;
72 p2
.AddRr(kSendingSsrc
, 1);
75 RtcpReportBlock report_block
= GetReportBlock();
78 p2
.GetPacket().Pass(),
79 rtcp_builder_
->BuildRtcpFromReceiver(
80 &report_block
, NULL
, NULL
, NULL
, kDefaultDelay
));
83 TEST_F(RtcpBuilderTest
, RtcpReceiverReportWithRrtr
) {
84 // Receiver report with report block.
85 TestRtcpPacketBuilder p
;
86 p
.AddRr(kSendingSsrc
, 1);
88 p
.AddXrHeader(kSendingSsrc
);
91 RtcpReportBlock report_block
= GetReportBlock();
93 RtcpReceiverReferenceTimeReport rrtr
;
94 rrtr
.ntp_seconds
= kNtpHigh
;
95 rrtr
.ntp_fraction
= kNtpLow
;
97 ExpectPacketEQ(p
.GetPacket().Pass(),
98 rtcp_builder_
->BuildRtcpFromReceiver(
106 TEST_F(RtcpBuilderTest
, RtcpReceiverReportWithCast
) {
107 // Receiver report with report block.
108 TestRtcpPacketBuilder p
;
109 p
.AddRr(kSendingSsrc
, 1);
111 p
.AddCast(kSendingSsrc
, kMediaSsrc
, kDefaultDelay
);
113 RtcpReportBlock report_block
= GetReportBlock();
115 RtcpCastMessage
cast_message(kMediaSsrc
);
116 cast_message
.ack_frame_id
= kAckFrameId
;
117 PacketIdSet missing_packets
;
118 cast_message
.missing_frames_and_packets
[kLostFrameId
] = missing_packets
;
120 missing_packets
.insert(kLostPacketId1
);
121 missing_packets
.insert(kLostPacketId2
);
122 missing_packets
.insert(kLostPacketId3
);
123 cast_message
.missing_frames_and_packets
[kFrameIdWithLostPackets
] =
126 ExpectPacketEQ(p
.GetPacket().Pass(),
127 rtcp_builder_
->BuildRtcpFromReceiver(
135 TEST_F(RtcpBuilderTest
, RtcpReceiverReportWithRrtraAndCastMessage
) {
136 TestRtcpPacketBuilder p
;
137 p
.AddRr(kSendingSsrc
, 1);
139 p
.AddXrHeader(kSendingSsrc
);
141 p
.AddCast(kSendingSsrc
, kMediaSsrc
, kDefaultDelay
);
143 RtcpReportBlock report_block
= GetReportBlock();
145 RtcpReceiverReferenceTimeReport rrtr
;
146 rrtr
.ntp_seconds
= kNtpHigh
;
147 rrtr
.ntp_fraction
= kNtpLow
;
149 RtcpCastMessage
cast_message(kMediaSsrc
);
150 cast_message
.ack_frame_id
= kAckFrameId
;
151 PacketIdSet missing_packets
;
152 cast_message
.missing_frames_and_packets
[kLostFrameId
] = missing_packets
;
154 missing_packets
.insert(kLostPacketId1
);
155 missing_packets
.insert(kLostPacketId2
);
156 missing_packets
.insert(kLostPacketId3
);
157 cast_message
.missing_frames_and_packets
[kFrameIdWithLostPackets
] =
160 ExpectPacketEQ(p
.GetPacket().Pass(),
161 rtcp_builder_
->BuildRtcpFromReceiver(
169 TEST_F(RtcpBuilderTest
, RtcpReceiverReportWithRrtrCastMessageAndLog
) {
170 static const uint32 kTimeBaseMs
= 12345678;
171 static const uint32 kTimeDelayMs
= 10;
173 TestRtcpPacketBuilder p
;
174 p
.AddRr(kSendingSsrc
, 1);
176 p
.AddXrHeader(kSendingSsrc
);
178 p
.AddCast(kSendingSsrc
, kMediaSsrc
, kDefaultDelay
);
180 RtcpReportBlock report_block
= GetReportBlock();
182 RtcpReceiverReferenceTimeReport rrtr
;
183 rrtr
.ntp_seconds
= kNtpHigh
;
184 rrtr
.ntp_fraction
= kNtpLow
;
186 RtcpCastMessage
cast_message(kMediaSsrc
);
187 cast_message
.ack_frame_id
= kAckFrameId
;
188 PacketIdSet missing_packets
;
189 cast_message
.missing_frames_and_packets
[kLostFrameId
] = missing_packets
;
191 missing_packets
.insert(kLostPacketId1
);
192 missing_packets
.insert(kLostPacketId2
);
193 missing_packets
.insert(kLostPacketId3
);
194 cast_message
.missing_frames_and_packets
[kFrameIdWithLostPackets
] =
197 ReceiverRtcpEventSubscriber
event_subscriber(500, VIDEO_EVENT
);
198 ReceiverRtcpEventSubscriber::RtcpEvents rtcp_events
;
200 ExpectPacketEQ(p
.GetPacket().Pass(),
201 rtcp_builder_
->BuildRtcpFromReceiver(
208 base::SimpleTestTickClock testing_clock
;
209 testing_clock
.Advance(base::TimeDelta::FromMilliseconds(kTimeBaseMs
));
211 p
.AddReceiverLog(kSendingSsrc
);
212 p
.AddReceiverFrameLog(kRtpTimestamp
, 2, kTimeBaseMs
);
213 p
.AddReceiverEventLog(0, FRAME_ACK_SENT
, 0);
214 p
.AddReceiverEventLog(kLostPacketId1
, PACKET_RECEIVED
, kTimeDelayMs
);
216 FrameEvent frame_event
;
217 frame_event
.rtp_timestamp
= kRtpTimestamp
;
218 frame_event
.type
= FRAME_ACK_SENT
;
219 frame_event
.media_type
= VIDEO_EVENT
;
220 frame_event
.timestamp
= testing_clock
.NowTicks();
221 event_subscriber
.OnReceiveFrameEvent(frame_event
);
222 testing_clock
.Advance(base::TimeDelta::FromMilliseconds(kTimeDelayMs
));
224 PacketEvent packet_event
;
225 packet_event
.rtp_timestamp
= kRtpTimestamp
;
226 packet_event
.type
= PACKET_RECEIVED
;
227 packet_event
.media_type
= VIDEO_EVENT
;
228 packet_event
.timestamp
= testing_clock
.NowTicks();
229 packet_event
.packet_id
= kLostPacketId1
;
230 event_subscriber
.OnReceivePacketEvent(packet_event
);
231 event_subscriber
.GetRtcpEventsWithRedundancy(&rtcp_events
);
232 EXPECT_EQ(2u, rtcp_events
.size());
235 p
.GetPacket().Pass(),
236 rtcp_builder_
->BuildRtcpFromReceiver(
244 TEST_F(RtcpBuilderTest
, RtcpReceiverReportWithOversizedFrameLog
) {
245 static const uint32 kTimeBaseMs
= 12345678;
246 static const uint32 kTimeDelayMs
= 10;
248 TestRtcpPacketBuilder p
;
249 p
.AddRr(kSendingSsrc
, 1);
252 RtcpReportBlock report_block
= GetReportBlock();
254 base::SimpleTestTickClock testing_clock
;
255 testing_clock
.Advance(base::TimeDelta::FromMilliseconds(kTimeBaseMs
));
257 p
.AddReceiverLog(kSendingSsrc
);
259 int num_events
= kMaxEventsPerRTCP
;
261 EXPECT_LE(num_events
, static_cast<int>(kRtcpMaxReceiverLogMessages
));
262 p
.AddReceiverFrameLog(
263 kRtpTimestamp
+ 2345,
266 for (int i
= 0; i
< num_events
; i
++) {
267 p
.AddReceiverEventLog(
270 static_cast<uint16
>(kTimeDelayMs
* i
));
274 ReceiverRtcpEventSubscriber
event_subscriber(500, VIDEO_EVENT
);
276 for (size_t i
= 0; i
< kRtcpMaxReceiverLogMessages
; ++i
) {
277 PacketEvent packet_event
;
278 packet_event
.rtp_timestamp
= kRtpTimestamp
+ 2345;
279 packet_event
.type
= PACKET_RECEIVED
;
280 packet_event
.media_type
= VIDEO_EVENT
;
281 packet_event
.timestamp
= testing_clock
.NowTicks();
282 packet_event
.packet_id
= kLostPacketId1
;
283 event_subscriber
.OnReceivePacketEvent(packet_event
);
284 testing_clock
.Advance(base::TimeDelta::FromMilliseconds(kTimeDelayMs
));
287 ReceiverRtcpEventSubscriber::RtcpEvents rtcp_events
;
288 event_subscriber
.GetRtcpEventsWithRedundancy(&rtcp_events
);
290 ExpectPacketEQ(p
.GetPacket().Pass(),
291 rtcp_builder_
->BuildRtcpFromReceiver(
299 TEST_F(RtcpBuilderTest
, RtcpReceiverReportWithTooManyLogFrames
) {
300 static const uint32 kTimeBaseMs
= 12345678;
301 static const uint32 kTimeDelayMs
= 10;
303 TestRtcpPacketBuilder p
;
304 p
.AddRr(kSendingSsrc
, 1);
307 RtcpReportBlock report_block
= GetReportBlock();
309 base::SimpleTestTickClock testing_clock
;
310 testing_clock
.Advance(base::TimeDelta::FromMilliseconds(kTimeBaseMs
));
312 p
.AddReceiverLog(kSendingSsrc
);
314 int num_events
= kMaxEventsPerRTCP
;
316 for (int i
= 0; i
< num_events
; i
++) {
317 p
.AddReceiverFrameLog(kRtpTimestamp
+ i
, 1, kTimeBaseMs
+ i
* kTimeDelayMs
);
318 p
.AddReceiverEventLog(0, FRAME_ACK_SENT
, 0);
321 ReceiverRtcpEventSubscriber
event_subscriber(500, VIDEO_EVENT
);
323 for (size_t i
= 0; i
< kRtcpMaxReceiverLogMessages
; ++i
) {
324 FrameEvent frame_event
;
325 frame_event
.rtp_timestamp
= kRtpTimestamp
+ static_cast<int>(i
);
326 frame_event
.type
= FRAME_ACK_SENT
;
327 frame_event
.media_type
= VIDEO_EVENT
;
328 frame_event
.timestamp
= testing_clock
.NowTicks();
329 event_subscriber
.OnReceiveFrameEvent(frame_event
);
330 testing_clock
.Advance(base::TimeDelta::FromMilliseconds(kTimeDelayMs
));
333 ReceiverRtcpEventSubscriber::RtcpEvents rtcp_events
;
334 event_subscriber
.GetRtcpEventsWithRedundancy(&rtcp_events
);
336 ExpectPacketEQ(p
.GetPacket().Pass(),
337 rtcp_builder_
->BuildRtcpFromReceiver(
345 TEST_F(RtcpBuilderTest
, RtcpReceiverReportWithOldLogFrames
) {
346 static const uint32 kTimeBaseMs
= 12345678;
348 TestRtcpPacketBuilder p
;
349 p
.AddRr(kSendingSsrc
, 1);
352 RtcpReportBlock report_block
= GetReportBlock();
354 base::SimpleTestTickClock testing_clock
;
355 testing_clock
.Advance(base::TimeDelta::FromMilliseconds(kTimeBaseMs
));
357 p
.AddReceiverLog(kSendingSsrc
);
359 // Log 11 events for a single frame, each |kTimeBetweenEventsMs| apart.
360 // Only last 10 events will be sent because the first event is more than
361 // 4095 milliseconds away from latest event.
362 const int kTimeBetweenEventsMs
= 410;
363 p
.AddReceiverFrameLog(kRtpTimestamp
, 10, kTimeBaseMs
+ kTimeBetweenEventsMs
);
364 for (int i
= 0; i
< 10; ++i
) {
365 p
.AddReceiverEventLog(0, FRAME_ACK_SENT
, i
* kTimeBetweenEventsMs
);
368 ReceiverRtcpEventSubscriber
event_subscriber(500, VIDEO_EVENT
);
369 for (int i
= 0; i
< 11; ++i
) {
370 FrameEvent frame_event
;
371 frame_event
.rtp_timestamp
= kRtpTimestamp
;
372 frame_event
.type
= FRAME_ACK_SENT
;
373 frame_event
.media_type
= VIDEO_EVENT
;
374 frame_event
.timestamp
= testing_clock
.NowTicks();
375 event_subscriber
.OnReceiveFrameEvent(frame_event
);
376 testing_clock
.Advance(
377 base::TimeDelta::FromMilliseconds(kTimeBetweenEventsMs
));
380 ReceiverRtcpEventSubscriber::RtcpEvents rtcp_events
;
381 event_subscriber
.GetRtcpEventsWithRedundancy(&rtcp_events
);
383 ExpectPacketEQ(p
.GetPacket().Pass(),
384 rtcp_builder_
->BuildRtcpFromReceiver(
392 TEST_F(RtcpBuilderTest
, RtcpReceiverReportRedundancy
) {
393 uint32 time_base_ms
= 12345678;
394 int kTimeBetweenEventsMs
= 10;
396 RtcpReportBlock report_block
= GetReportBlock();
398 base::SimpleTestTickClock testing_clock
;
399 testing_clock
.Advance(base::TimeDelta::FromMilliseconds(time_base_ms
));
401 ReceiverRtcpEventSubscriber
event_subscriber(500, VIDEO_EVENT
);
402 size_t packet_count
= kNumResends
* kResendDelay
+ 10;
403 for (size_t i
= 0; i
< packet_count
; i
++) {
404 TestRtcpPacketBuilder p
;
405 p
.AddRr(kSendingSsrc
, 1);
408 p
.AddReceiverLog(kSendingSsrc
);
410 int num_events
= (i
+ kResendDelay
) / kResendDelay
;
411 num_events
= std::min
<int>(num_events
, kNumResends
);
412 p
.AddReceiverFrameLog(kRtpTimestamp
, num_events
,
413 time_base_ms
- (num_events
- 1) * kResendDelay
*
414 kTimeBetweenEventsMs
);
415 for (int i
= 0; i
< num_events
; i
++) {
416 p
.AddReceiverEventLog(0, FRAME_ACK_SENT
,
417 i
* kResendDelay
* kTimeBetweenEventsMs
);
420 FrameEvent frame_event
;
421 frame_event
.rtp_timestamp
= kRtpTimestamp
;
422 frame_event
.type
= FRAME_ACK_SENT
;
423 frame_event
.media_type
= VIDEO_EVENT
;
424 frame_event
.timestamp
= testing_clock
.NowTicks();
425 event_subscriber
.OnReceiveFrameEvent(frame_event
);
427 ReceiverRtcpEventSubscriber::RtcpEvents rtcp_events
;
428 event_subscriber
.GetRtcpEventsWithRedundancy(&rtcp_events
);
430 ExpectPacketEQ(p
.GetPacket().Pass(),
431 rtcp_builder_
->BuildRtcpFromReceiver(
438 testing_clock
.Advance(
439 base::TimeDelta::FromMilliseconds(kTimeBetweenEventsMs
));
440 time_base_ms
+= kTimeBetweenEventsMs
;
444 TEST_F(RtcpBuilderTest
, RtcpSenderReport
) {
445 RtcpSenderInfo sender_info
;
446 sender_info
.ntp_seconds
= kNtpHigh
;
447 sender_info
.ntp_fraction
= kNtpLow
;
448 sender_info
.rtp_timestamp
= kRtpTimestamp
;
449 sender_info
.send_packet_count
= kSendPacketCount
;
450 sender_info
.send_octet_count
= kSendOctetCount
;
453 TestRtcpPacketBuilder p
;
454 p
.AddSr(kSendingSsrc
, 0);
456 ExpectPacketEQ(p
.GetPacket().Pass(),
457 rtcp_builder_
->BuildRtcpFromSender(sender_info
));