1 // Copyright 2013 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/rtcp/receiver_rtcp_event_subscriber.h"
10 #include "media/cast/rtcp/rtcp_sender.h"
11 #include "media/cast/rtcp/rtcp_utility.h"
12 #include "media/cast/rtcp/test_rtcp_packet_builder.h"
13 #include "media/cast/test/fake_single_thread_task_runner.h"
14 #include "media/cast/transport/cast_transport_defines.h"
15 #include "media/cast/transport/pacing/paced_sender.h"
16 #include "testing/gmock/include/gmock/gmock.h"
22 static const uint32 kSendingSsrc
= 0x12345678;
23 static const uint32 kMediaSsrc
= 0x87654321;
24 static const std::string
kCName("test@10.1.1.1");
27 class TestRtcpTransport
: public transport::PacedPacketSender
{
29 TestRtcpTransport() : packet_count_(0) {}
31 virtual bool SendRtcpPacket(const Packet
& packet
) OVERRIDE
{
32 EXPECT_EQ(expected_packet_
.size(), packet
.size());
33 EXPECT_EQ(0, memcmp(expected_packet_
.data(), packet
.data(), packet
.size()));
38 virtual bool SendPackets(const PacketList
& packets
) OVERRIDE
{ return false; }
40 virtual bool ResendPackets(const PacketList
& packets
) OVERRIDE
{
44 void SetExpectedRtcpPacket(scoped_ptr
<Packet
> packet
) {
45 expected_packet_
.swap(*packet
);
48 int packet_count() const { return packet_count_
; }
51 Packet expected_packet_
;
54 DISALLOW_COPY_AND_ASSIGN(TestRtcpTransport
);
57 class RtcpSenderTest
: public ::testing::Test
{
60 : testing_clock_(new base::SimpleTestTickClock()),
61 task_runner_(new test::FakeSingleThreadTaskRunner(testing_clock_
)),
62 cast_environment_(new CastEnvironment(
63 scoped_ptr
<base::TickClock
>(testing_clock_
).Pass(),
70 GetDefaultCastSenderLoggingConfig())),
71 rtcp_sender_(new RtcpSender(cast_environment_
,
76 base::SimpleTestTickClock
* testing_clock_
; // Owned by CastEnvironment.
77 TestRtcpTransport test_transport_
;
78 scoped_refptr
<test::FakeSingleThreadTaskRunner
> task_runner_
;
79 scoped_refptr
<CastEnvironment
> cast_environment_
;
80 scoped_ptr
<RtcpSender
> rtcp_sender_
;
82 DISALLOW_COPY_AND_ASSIGN(RtcpSenderTest
);
85 TEST_F(RtcpSenderTest
, RtcpReceiverReport
) {
86 // Empty receiver report + c_name.
87 TestRtcpPacketBuilder p1
;
88 p1
.AddRr(kSendingSsrc
, 0);
89 p1
.AddSdesCname(kSendingSsrc
, kCName
);
90 test_transport_
.SetExpectedRtcpPacket(p1
.GetPacket());
92 rtcp_sender_
->SendRtcpFromRtpReceiver(
93 RtcpSender::kRtcpRr
, NULL
, NULL
, NULL
, NULL
);
95 EXPECT_EQ(1, test_transport_
.packet_count());
97 // Receiver report with report block + c_name.
98 TestRtcpPacketBuilder p2
;
99 p2
.AddRr(kSendingSsrc
, 1);
100 p2
.AddRb(kMediaSsrc
);
101 p2
.AddSdesCname(kSendingSsrc
, kCName
);
102 test_transport_
.SetExpectedRtcpPacket(p2
.GetPacket().Pass());
104 transport::RtcpReportBlock report_block
;
105 // Initialize remote_ssrc to a "clearly illegal" value.
106 report_block
.remote_ssrc
= 0xDEAD;
107 report_block
.media_ssrc
= kMediaSsrc
; // SSRC of the RTP packet sender.
108 report_block
.fraction_lost
= kLoss
>> 24;
109 report_block
.cumulative_lost
= kLoss
; // 24 bits valid.
110 report_block
.extended_high_sequence_number
= kExtendedMax
;
111 report_block
.jitter
= kTestJitter
;
112 report_block
.last_sr
= kLastSr
;
113 report_block
.delay_since_last_sr
= kDelayLastSr
;
115 rtcp_sender_
->SendRtcpFromRtpReceiver(
116 RtcpSender::kRtcpRr
, &report_block
, NULL
, NULL
, NULL
);
118 EXPECT_EQ(2, test_transport_
.packet_count());
121 TEST_F(RtcpSenderTest
, RtcpReceiverReportWithRrtr
) {
122 // Receiver report with report block + c_name.
123 TestRtcpPacketBuilder p
;
124 p
.AddRr(kSendingSsrc
, 1);
126 p
.AddSdesCname(kSendingSsrc
, kCName
);
127 p
.AddXrHeader(kSendingSsrc
);
129 test_transport_
.SetExpectedRtcpPacket(p
.GetPacket().Pass());
131 transport::RtcpReportBlock report_block
;
132 // Initialize remote_ssrc to a "clearly illegal" value.
133 report_block
.remote_ssrc
= 0xDEAD;
134 report_block
.media_ssrc
= kMediaSsrc
; // SSRC of the RTP packet sender.
135 report_block
.fraction_lost
= kLoss
>> 24;
136 report_block
.cumulative_lost
= kLoss
; // 24 bits valid.
137 report_block
.extended_high_sequence_number
= kExtendedMax
;
138 report_block
.jitter
= kTestJitter
;
139 report_block
.last_sr
= kLastSr
;
140 report_block
.delay_since_last_sr
= kDelayLastSr
;
142 RtcpReceiverReferenceTimeReport rrtr
;
143 rrtr
.ntp_seconds
= kNtpHigh
;
144 rrtr
.ntp_fraction
= kNtpLow
;
146 rtcp_sender_
->SendRtcpFromRtpReceiver(
147 RtcpSender::kRtcpRr
| RtcpSender::kRtcpRrtr
,
153 EXPECT_EQ(1, test_transport_
.packet_count());
156 TEST_F(RtcpSenderTest
, RtcpReceiverReportWithCast
) {
157 // Receiver report with report block + c_name.
158 TestRtcpPacketBuilder p
;
159 p
.AddRr(kSendingSsrc
, 1);
161 p
.AddSdesCname(kSendingSsrc
, kCName
);
162 p
.AddCast(kSendingSsrc
, kMediaSsrc
);
163 test_transport_
.SetExpectedRtcpPacket(p
.GetPacket().Pass());
165 transport::RtcpReportBlock report_block
;
166 // Initialize remote_ssrc to a "clearly illegal" value.
167 report_block
.remote_ssrc
= 0xDEAD;
168 report_block
.media_ssrc
= kMediaSsrc
; // SSRC of the RTP packet sender.
169 report_block
.fraction_lost
= kLoss
>> 24;
170 report_block
.cumulative_lost
= kLoss
; // 24 bits valid.
171 report_block
.extended_high_sequence_number
= kExtendedMax
;
172 report_block
.jitter
= kTestJitter
;
173 report_block
.last_sr
= kLastSr
;
174 report_block
.delay_since_last_sr
= kDelayLastSr
;
176 RtcpCastMessage
cast_message(kMediaSsrc
);
177 cast_message
.ack_frame_id_
= kAckFrameId
;
178 PacketIdSet missing_packets
;
179 cast_message
.missing_frames_and_packets_
[kLostFrameId
] = missing_packets
;
181 missing_packets
.insert(kLostPacketId1
);
182 missing_packets
.insert(kLostPacketId2
);
183 missing_packets
.insert(kLostPacketId3
);
184 cast_message
.missing_frames_and_packets_
[kFrameIdWithLostPackets
] =
187 rtcp_sender_
->SendRtcpFromRtpReceiver(
188 RtcpSender::kRtcpRr
| RtcpSender::kRtcpCast
,
194 EXPECT_EQ(1, test_transport_
.packet_count());
197 TEST_F(RtcpSenderTest
, RtcpReceiverReportWithRrtraAndCastMessage
) {
198 TestRtcpPacketBuilder p
;
199 p
.AddRr(kSendingSsrc
, 1);
201 p
.AddSdesCname(kSendingSsrc
, kCName
);
202 p
.AddXrHeader(kSendingSsrc
);
204 p
.AddCast(kSendingSsrc
, kMediaSsrc
);
205 test_transport_
.SetExpectedRtcpPacket(p
.GetPacket().Pass());
207 transport::RtcpReportBlock report_block
;
208 // Initialize remote_ssrc to a "clearly illegal" value.
209 report_block
.remote_ssrc
= 0xDEAD;
210 report_block
.media_ssrc
= kMediaSsrc
; // SSRC of the RTP packet sender.
211 report_block
.fraction_lost
= kLoss
>> 24;
212 report_block
.cumulative_lost
= kLoss
; // 24 bits valid.
213 report_block
.extended_high_sequence_number
= kExtendedMax
;
214 report_block
.jitter
= kTestJitter
;
215 report_block
.last_sr
= kLastSr
;
216 report_block
.delay_since_last_sr
= kDelayLastSr
;
218 RtcpReceiverReferenceTimeReport rrtr
;
219 rrtr
.ntp_seconds
= kNtpHigh
;
220 rrtr
.ntp_fraction
= kNtpLow
;
222 RtcpCastMessage
cast_message(kMediaSsrc
);
223 cast_message
.ack_frame_id_
= kAckFrameId
;
224 PacketIdSet missing_packets
;
225 cast_message
.missing_frames_and_packets_
[kLostFrameId
] = missing_packets
;
227 missing_packets
.insert(kLostPacketId1
);
228 missing_packets
.insert(kLostPacketId2
);
229 missing_packets
.insert(kLostPacketId3
);
230 cast_message
.missing_frames_and_packets_
[kFrameIdWithLostPackets
] =
233 rtcp_sender_
->SendRtcpFromRtpReceiver(
234 RtcpSender::kRtcpRr
| RtcpSender::kRtcpRrtr
| RtcpSender::kRtcpCast
,
240 EXPECT_EQ(1, test_transport_
.packet_count());
243 TEST_F(RtcpSenderTest
, RtcpReceiverReportWithRrtrCastMessageAndLog
) {
244 static const uint32 kTimeBaseMs
= 12345678;
245 static const uint32 kTimeDelayMs
= 10;
247 TestRtcpPacketBuilder p
;
248 p
.AddRr(kSendingSsrc
, 1);
250 p
.AddSdesCname(kSendingSsrc
, kCName
);
251 p
.AddXrHeader(kSendingSsrc
);
253 p
.AddCast(kSendingSsrc
, kMediaSsrc
);
254 test_transport_
.SetExpectedRtcpPacket(p
.GetPacket().Pass());
256 transport::RtcpReportBlock report_block
;
257 // Initialize remote_ssrc to a "clearly illegal" value.
258 report_block
.remote_ssrc
= 0xDEAD;
259 report_block
.media_ssrc
= kMediaSsrc
; // SSRC of the RTP packet sender.
260 report_block
.fraction_lost
= kLoss
>> 24;
261 report_block
.cumulative_lost
= kLoss
; // 24 bits valid.
262 report_block
.extended_high_sequence_number
= kExtendedMax
;
263 report_block
.jitter
= kTestJitter
;
264 report_block
.last_sr
= kLastSr
;
265 report_block
.delay_since_last_sr
= kDelayLastSr
;
267 RtcpReceiverReferenceTimeReport rrtr
;
268 rrtr
.ntp_seconds
= kNtpHigh
;
269 rrtr
.ntp_fraction
= kNtpLow
;
271 RtcpCastMessage
cast_message(kMediaSsrc
);
272 cast_message
.ack_frame_id_
= kAckFrameId
;
273 PacketIdSet missing_packets
;
274 cast_message
.missing_frames_and_packets_
[kLostFrameId
] = missing_packets
;
276 missing_packets
.insert(kLostPacketId1
);
277 missing_packets
.insert(kLostPacketId2
);
278 missing_packets
.insert(kLostPacketId3
);
279 cast_message
.missing_frames_and_packets_
[kFrameIdWithLostPackets
] =
282 ReceiverRtcpEventSubscriber
event_subscriber(
283 500, ReceiverRtcpEventSubscriber::kVideoEventSubscriber
);
285 rtcp_sender_
->SendRtcpFromRtpReceiver(
286 RtcpSender::kRtcpRr
| RtcpSender::kRtcpRrtr
| RtcpSender::kRtcpCast
|
287 RtcpSender::kRtcpReceiverLog
,
293 base::SimpleTestTickClock testing_clock
;
294 testing_clock
.Advance(base::TimeDelta::FromMilliseconds(kTimeBaseMs
));
296 p
.AddReceiverLog(kSendingSsrc
);
297 p
.AddReceiverFrameLog(kRtpTimestamp
, 2, kTimeBaseMs
);
298 p
.AddReceiverEventLog(0, 5, 0);
299 p
.AddReceiverEventLog(kLostPacketId1
, 8, kTimeDelayMs
);
301 test_transport_
.SetExpectedRtcpPacket(p
.GetPacket().Pass());
303 FrameEvent frame_event
;
304 frame_event
.rtp_timestamp
= kRtpTimestamp
;
305 frame_event
.type
= kVideoAckSent
;
306 frame_event
.timestamp
= testing_clock
.NowTicks();
307 event_subscriber
.OnReceiveFrameEvent(frame_event
);
308 testing_clock
.Advance(base::TimeDelta::FromMilliseconds(kTimeDelayMs
));
310 PacketEvent packet_event
;
311 packet_event
.rtp_timestamp
= kRtpTimestamp
;
312 packet_event
.type
= kVideoPacketReceived
;
313 packet_event
.timestamp
= testing_clock
.NowTicks();
314 packet_event
.packet_id
= kLostPacketId1
;
315 event_subscriber
.OnReceivePacketEvent(packet_event
);
317 rtcp_sender_
->SendRtcpFromRtpReceiver(
318 RtcpSender::kRtcpRr
| RtcpSender::kRtcpRrtr
| RtcpSender::kRtcpCast
|
319 RtcpSender::kRtcpReceiverLog
,
325 EXPECT_EQ(2, test_transport_
.packet_count());
328 TEST_F(RtcpSenderTest
, RtcpReceiverReportWithOversizedFrameLog
) {
329 static const uint32 kTimeBaseMs
= 12345678;
330 static const uint32 kTimeDelayMs
= 10;
332 TestRtcpPacketBuilder p
;
333 p
.AddRr(kSendingSsrc
, 1);
335 p
.AddSdesCname(kSendingSsrc
, kCName
);
337 transport::RtcpReportBlock report_block
;
338 // Initialize remote_ssrc to a "clearly illegal" value.
339 report_block
.remote_ssrc
= 0xDEAD;
340 report_block
.media_ssrc
= kMediaSsrc
; // SSRC of the RTP packet sender.
341 report_block
.fraction_lost
= kLoss
>> 24;
342 report_block
.cumulative_lost
= kLoss
; // 24 bits valid.
343 report_block
.extended_high_sequence_number
= kExtendedMax
;
344 report_block
.jitter
= kTestJitter
;
345 report_block
.last_sr
= kLastSr
;
346 report_block
.delay_since_last_sr
= kDelayLastSr
;
348 base::SimpleTestTickClock testing_clock
;
349 testing_clock
.Advance(base::TimeDelta::FromMilliseconds(kTimeBaseMs
));
351 p
.AddReceiverLog(kSendingSsrc
);
353 p
.AddReceiverFrameLog(kRtpTimestamp
, 1, kTimeBaseMs
);
354 p
.AddReceiverEventLog(0, 5, 0);
355 p
.AddReceiverFrameLog(
356 kRtpTimestamp
+ 2345, kRtcpMaxReceiverLogMessages
, kTimeBaseMs
);
358 for (size_t i
= 0; i
< kRtcpMaxReceiverLogMessages
; ++i
) {
359 p
.AddReceiverEventLog(
360 kLostPacketId1
, 8, static_cast<uint16
>(kTimeDelayMs
* i
));
363 test_transport_
.SetExpectedRtcpPacket(p
.GetPacket().Pass());
365 ReceiverRtcpEventSubscriber
event_subscriber(
366 500, ReceiverRtcpEventSubscriber::kVideoEventSubscriber
);
367 FrameEvent frame_event
;
368 frame_event
.rtp_timestamp
= kRtpTimestamp
;
369 frame_event
.type
= media::cast::kVideoAckSent
;
370 frame_event
.timestamp
= testing_clock
.NowTicks();
371 event_subscriber
.OnReceiveFrameEvent(frame_event
);
373 for (size_t i
= 0; i
< kRtcpMaxReceiverLogMessages
; ++i
) {
374 PacketEvent packet_event
;
375 packet_event
.rtp_timestamp
= kRtpTimestamp
+ 2345;
376 packet_event
.type
= kVideoPacketReceived
;
377 packet_event
.timestamp
= testing_clock
.NowTicks();
378 packet_event
.packet_id
= kLostPacketId1
;
379 event_subscriber
.OnReceivePacketEvent(packet_event
);
380 testing_clock
.Advance(base::TimeDelta::FromMilliseconds(kTimeDelayMs
));
383 rtcp_sender_
->SendRtcpFromRtpReceiver(
384 RtcpSender::kRtcpRr
| RtcpSender::kRtcpReceiverLog
,
390 EXPECT_EQ(1, test_transport_
.packet_count());
393 TEST_F(RtcpSenderTest
, RtcpReceiverReportWithTooManyLogFrames
) {
394 static const uint32 kTimeBaseMs
= 12345678;
395 static const uint32 kTimeDelayMs
= 10;
397 TestRtcpPacketBuilder p
;
398 p
.AddRr(kSendingSsrc
, 1);
400 p
.AddSdesCname(kSendingSsrc
, kCName
);
402 transport::RtcpReportBlock report_block
;
403 // Initialize remote_ssrc to a "clearly illegal" value.
404 report_block
.remote_ssrc
= 0xDEAD;
405 report_block
.media_ssrc
= kMediaSsrc
; // SSRC of the RTP packet sender.
406 report_block
.fraction_lost
= kLoss
>> 24;
407 report_block
.cumulative_lost
= kLoss
; // 24 bits valid.
408 report_block
.extended_high_sequence_number
= kExtendedMax
;
409 report_block
.jitter
= kTestJitter
;
410 report_block
.last_sr
= kLastSr
;
411 report_block
.delay_since_last_sr
= kDelayLastSr
;
413 base::SimpleTestTickClock testing_clock
;
414 testing_clock
.Advance(base::TimeDelta::FromMilliseconds(kTimeBaseMs
));
416 p
.AddReceiverLog(kSendingSsrc
);
418 for (int i
= 0; i
< 119; ++i
) {
419 p
.AddReceiverFrameLog(kRtpTimestamp
+ i
, 1, kTimeBaseMs
+ i
* kTimeDelayMs
);
420 p
.AddReceiverEventLog(0, 5, 0);
422 test_transport_
.SetExpectedRtcpPacket(p
.GetPacket().Pass());
424 ReceiverRtcpEventSubscriber
event_subscriber(
425 500, ReceiverRtcpEventSubscriber::kVideoEventSubscriber
);
426 for (size_t i
= 0; i
< kRtcpMaxReceiverLogMessages
; ++i
) {
427 FrameEvent frame_event
;
428 frame_event
.rtp_timestamp
= kRtpTimestamp
+ static_cast<int>(i
);
429 frame_event
.type
= media::cast::kVideoAckSent
;
430 frame_event
.timestamp
= testing_clock
.NowTicks();
431 event_subscriber
.OnReceiveFrameEvent(frame_event
);
432 testing_clock
.Advance(base::TimeDelta::FromMilliseconds(kTimeDelayMs
));
435 rtcp_sender_
->SendRtcpFromRtpReceiver(
436 RtcpSender::kRtcpRr
| RtcpSender::kRtcpReceiverLog
,
442 EXPECT_EQ(1, test_transport_
.packet_count());