Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / cast / net / rtcp / rtcp_builder_unittest.cc
blob6bd9d0581221498358f5e09ac3dda707b391e2a1
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"
17 namespace media {
18 namespace cast {
20 namespace {
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;
37 return report_block;
40 } // namespace
43 class RtcpBuilderTest : public ::testing::Test {
44 protected:
45 RtcpBuilderTest()
46 : rtcp_builder_(new RtcpBuilder(kSendingSsrc)) {}
48 void ExpectPacketEQ(scoped_ptr<Packet> golden_packet,
49 PacketRef packet) {
50 int diffs = 0;
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]) {
57 if (++diffs > 5)
58 break;
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);
73 p2.AddRb(kMediaSsrc);
75 RtcpReportBlock report_block = GetReportBlock();
77 ExpectPacketEQ(
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);
87 p.AddRb(kMediaSsrc);
88 p.AddXrHeader(kSendingSsrc);
89 p.AddXrRrtrBlock();
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(
99 &report_block,
100 &rrtr,
101 NULL,
102 NULL,
103 kDefaultDelay));
106 TEST_F(RtcpBuilderTest, RtcpReceiverReportWithCast) {
107 // Receiver report with report block.
108 TestRtcpPacketBuilder p;
109 p.AddRr(kSendingSsrc, 1);
110 p.AddRb(kMediaSsrc);
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] =
124 missing_packets;
126 ExpectPacketEQ(p.GetPacket().Pass(),
127 rtcp_builder_->BuildRtcpFromReceiver(
128 &report_block,
129 NULL,
130 &cast_message,
131 NULL,
132 kDefaultDelay));
135 TEST_F(RtcpBuilderTest, RtcpReceiverReportWithRrtraAndCastMessage) {
136 TestRtcpPacketBuilder p;
137 p.AddRr(kSendingSsrc, 1);
138 p.AddRb(kMediaSsrc);
139 p.AddXrHeader(kSendingSsrc);
140 p.AddXrRrtrBlock();
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] =
158 missing_packets;
160 ExpectPacketEQ(p.GetPacket().Pass(),
161 rtcp_builder_->BuildRtcpFromReceiver(
162 &report_block,
163 &rrtr,
164 &cast_message,
165 NULL,
166 kDefaultDelay));
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);
175 p.AddRb(kMediaSsrc);
176 p.AddXrHeader(kSendingSsrc);
177 p.AddXrRrtrBlock();
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] =
195 missing_packets;
197 ReceiverRtcpEventSubscriber event_subscriber(500, VIDEO_EVENT);
198 ReceiverRtcpEventSubscriber::RtcpEvents rtcp_events;
200 ExpectPacketEQ(p.GetPacket().Pass(),
201 rtcp_builder_->BuildRtcpFromReceiver(
202 &report_block,
203 &rrtr,
204 &cast_message,
205 &rtcp_events,
206 kDefaultDelay));
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());
234 ExpectPacketEQ(
235 p.GetPacket().Pass(),
236 rtcp_builder_->BuildRtcpFromReceiver(
237 &report_block,
238 &rrtr,
239 &cast_message,
240 &rtcp_events,
241 kDefaultDelay));
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);
250 p.AddRb(kMediaSsrc);
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,
264 num_events,
265 kTimeBaseMs);
266 for (int i = 0; i < num_events; i++) {
267 p.AddReceiverEventLog(
268 kLostPacketId1,
269 PACKET_RECEIVED,
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(
292 &report_block,
293 NULL,
294 NULL,
295 &rtcp_events,
296 kDefaultDelay));
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);
305 p.AddRb(kMediaSsrc);
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(
338 &report_block,
339 NULL,
340 NULL,
341 &rtcp_events,
342 kDefaultDelay));
345 TEST_F(RtcpBuilderTest, RtcpReceiverReportWithOldLogFrames) {
346 static const uint32 kTimeBaseMs = 12345678;
348 TestRtcpPacketBuilder p;
349 p.AddRr(kSendingSsrc, 1);
350 p.AddRb(kMediaSsrc);
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(
385 &report_block,
386 NULL,
387 NULL,
388 &rtcp_events,
389 kDefaultDelay));
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);
406 p.AddRb(kMediaSsrc);
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(
432 &report_block,
433 NULL,
434 NULL,
435 &rtcp_events,
436 kDefaultDelay));
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;
452 // Sender report.
453 TestRtcpPacketBuilder p;
454 p.AddSr(kSendingSsrc, 0);
456 ExpectPacketEQ(p.GetPacket().Pass(),
457 rtcp_builder_->BuildRtcpFromSender(sender_info));
460 } // namespace cast
461 } // namespace media