1 /*
2  *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3  *
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
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  *
10  * This file includes unit tests for the RtcpPacket.
11  */
12 
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 
16 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet.h"
17 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/app.h"
18 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/bye.h"
19 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
20 #include "webrtc/test/rtcp_packet_parser.h"
21 
22 using ::testing::ElementsAre;
23 
24 using webrtc::rtcp::App;
25 using webrtc::rtcp::Bye;
26 using webrtc::rtcp::Dlrr;
27 using webrtc::rtcp::Fir;
28 using webrtc::rtcp::RawPacket;
29 using webrtc::rtcp::ReceiverReport;
30 using webrtc::rtcp::Remb;
31 using webrtc::rtcp::ReportBlock;
32 using webrtc::rtcp::Rpsi;
33 using webrtc::rtcp::Rrtr;
34 using webrtc::rtcp::Sdes;
35 using webrtc::rtcp::SenderReport;
36 using webrtc::rtcp::VoipMetric;
37 using webrtc::rtcp::Xr;
38 using webrtc::test::RtcpPacketParser;
39 
40 namespace webrtc {
41 
42 const uint32_t kSenderSsrc = 0x12345678;
43 const uint32_t kRemoteSsrc = 0x23456789;
44 
TEST(RtcpPacketTest,Sr)45 TEST(RtcpPacketTest, Sr) {
46   SenderReport sr;
47   sr.From(kSenderSsrc);
48   sr.WithNtpSec(0x11111111);
49   sr.WithNtpFrac(0x22222222);
50   sr.WithRtpTimestamp(0x33333333);
51   sr.WithPacketCount(0x44444444);
52   sr.WithOctetCount(0x55555555);
53 
54   rtc::scoped_ptr<RawPacket> packet(sr.Build());
55   RtcpPacketParser parser;
56   parser.Parse(packet->Buffer(), packet->Length());
57 
58   EXPECT_EQ(1, parser.sender_report()->num_packets());
59   EXPECT_EQ(kSenderSsrc, parser.sender_report()->Ssrc());
60   EXPECT_EQ(0x11111111U, parser.sender_report()->NtpSec());
61   EXPECT_EQ(0x22222222U, parser.sender_report()->NtpFrac());
62   EXPECT_EQ(0x33333333U, parser.sender_report()->RtpTimestamp());
63   EXPECT_EQ(0x44444444U, parser.sender_report()->PacketCount());
64   EXPECT_EQ(0x55555555U, parser.sender_report()->OctetCount());
65   EXPECT_EQ(0, parser.report_block()->num_packets());
66 }
67 
TEST(RtcpPacketTest,SrWithOneReportBlock)68 TEST(RtcpPacketTest, SrWithOneReportBlock) {
69   ReportBlock rb;
70   rb.To(kRemoteSsrc);
71 
72   SenderReport sr;
73   sr.From(kSenderSsrc);
74   EXPECT_TRUE(sr.WithReportBlock(rb));
75 
76   rtc::scoped_ptr<RawPacket> packet(sr.Build());
77   RtcpPacketParser parser;
78   parser.Parse(packet->Buffer(), packet->Length());
79   EXPECT_EQ(1, parser.sender_report()->num_packets());
80   EXPECT_EQ(kSenderSsrc, parser.sender_report()->Ssrc());
81   EXPECT_EQ(1, parser.report_block()->num_packets());
82   EXPECT_EQ(kRemoteSsrc, parser.report_block()->Ssrc());
83 }
84 
TEST(RtcpPacketTest,SrWithTwoReportBlocks)85 TEST(RtcpPacketTest, SrWithTwoReportBlocks) {
86   ReportBlock rb1;
87   rb1.To(kRemoteSsrc);
88   ReportBlock rb2;
89   rb2.To(kRemoteSsrc + 1);
90 
91   SenderReport sr;
92   sr.From(kSenderSsrc);
93   EXPECT_TRUE(sr.WithReportBlock(rb1));
94   EXPECT_TRUE(sr.WithReportBlock(rb2));
95 
96   rtc::scoped_ptr<RawPacket> packet(sr.Build());
97   RtcpPacketParser parser;
98   parser.Parse(packet->Buffer(), packet->Length());
99   EXPECT_EQ(1, parser.sender_report()->num_packets());
100   EXPECT_EQ(kSenderSsrc, parser.sender_report()->Ssrc());
101   EXPECT_EQ(2, parser.report_block()->num_packets());
102   EXPECT_EQ(1, parser.report_blocks_per_ssrc(kRemoteSsrc));
103   EXPECT_EQ(1, parser.report_blocks_per_ssrc(kRemoteSsrc + 1));
104 }
105 
TEST(RtcpPacketTest,SrWithTooManyReportBlocks)106 TEST(RtcpPacketTest, SrWithTooManyReportBlocks) {
107   SenderReport sr;
108   sr.From(kSenderSsrc);
109   const int kMaxReportBlocks = (1 << 5) - 1;
110   ReportBlock rb;
111   for (int i = 0; i < kMaxReportBlocks; ++i) {
112     rb.To(kRemoteSsrc + i);
113     EXPECT_TRUE(sr.WithReportBlock(rb));
114   }
115   rb.To(kRemoteSsrc + kMaxReportBlocks);
116   EXPECT_FALSE(sr.WithReportBlock(rb));
117 }
118 
TEST(RtcpPacketTest,AppWithNoData)119 TEST(RtcpPacketTest, AppWithNoData) {
120   App app;
121   app.WithSubType(30);
122   uint32_t name = 'n' << 24;
123   name += 'a' << 16;
124   name += 'm' << 8;
125   name += 'e';
126   app.WithName(name);
127 
128   rtc::scoped_ptr<RawPacket> packet(app.Build());
129   RtcpPacketParser parser;
130   parser.Parse(packet->Buffer(), packet->Length());
131   EXPECT_EQ(1, parser.app()->num_packets());
132   EXPECT_EQ(30U, parser.app()->SubType());
133   EXPECT_EQ(name, parser.app()->Name());
134   EXPECT_EQ(0, parser.app_item()->num_packets());
135 }
136 
TEST(RtcpPacketTest,App)137 TEST(RtcpPacketTest, App) {
138   App app;
139   app.From(kSenderSsrc);
140   app.WithSubType(30);
141   uint32_t name = 'n' << 24;
142   name += 'a' << 16;
143   name += 'm' << 8;
144   name += 'e';
145   app.WithName(name);
146   const char kData[] = {'t', 'e', 's', 't', 'd', 'a', 't', 'a'};
147   const size_t kDataLength = sizeof(kData) / sizeof(kData[0]);
148   app.WithData((const uint8_t*)kData, kDataLength);
149 
150   rtc::scoped_ptr<RawPacket> packet(app.Build());
151   RtcpPacketParser parser;
152   parser.Parse(packet->Buffer(), packet->Length());
153   EXPECT_EQ(1, parser.app()->num_packets());
154   EXPECT_EQ(30U, parser.app()->SubType());
155   EXPECT_EQ(name, parser.app()->Name());
156   EXPECT_EQ(1, parser.app_item()->num_packets());
157   EXPECT_EQ(kDataLength, parser.app_item()->DataLength());
158   EXPECT_EQ(0, strncmp(kData, (const char*)parser.app_item()->Data(),
159       parser.app_item()->DataLength()));
160 }
161 
TEST(RtcpPacketTest,SdesWithOneChunk)162 TEST(RtcpPacketTest, SdesWithOneChunk) {
163   Sdes sdes;
164   EXPECT_TRUE(sdes.WithCName(kSenderSsrc, "alice@host"));
165 
166   rtc::scoped_ptr<RawPacket> packet(sdes.Build());
167   RtcpPacketParser parser;
168   parser.Parse(packet->Buffer(), packet->Length());
169   EXPECT_EQ(1, parser.sdes()->num_packets());
170   EXPECT_EQ(1, parser.sdes_chunk()->num_packets());
171   EXPECT_EQ(kSenderSsrc, parser.sdes_chunk()->Ssrc());
172   EXPECT_EQ("alice@host", parser.sdes_chunk()->Cname());
173 }
174 
TEST(RtcpPacketTest,SdesWithMultipleChunks)175 TEST(RtcpPacketTest, SdesWithMultipleChunks) {
176   Sdes sdes;
177   EXPECT_TRUE(sdes.WithCName(kSenderSsrc, "a"));
178   EXPECT_TRUE(sdes.WithCName(kSenderSsrc + 1, "ab"));
179   EXPECT_TRUE(sdes.WithCName(kSenderSsrc + 2, "abc"));
180   EXPECT_TRUE(sdes.WithCName(kSenderSsrc + 3, "abcd"));
181   EXPECT_TRUE(sdes.WithCName(kSenderSsrc + 4, "abcde"));
182   EXPECT_TRUE(sdes.WithCName(kSenderSsrc + 5, "abcdef"));
183 
184   rtc::scoped_ptr<RawPacket> packet(sdes.Build());
185   RtcpPacketParser parser;
186   parser.Parse(packet->Buffer(), packet->Length());
187   EXPECT_EQ(1, parser.sdes()->num_packets());
188   EXPECT_EQ(6, parser.sdes_chunk()->num_packets());
189   EXPECT_EQ(kSenderSsrc + 5, parser.sdes_chunk()->Ssrc());
190   EXPECT_EQ("abcdef", parser.sdes_chunk()->Cname());
191 }
192 
TEST(RtcpPacketTest,SdesWithTooManyChunks)193 TEST(RtcpPacketTest, SdesWithTooManyChunks) {
194   Sdes sdes;
195   const int kMaxChunks = (1 << 5) - 1;
196   for (int i = 0; i < kMaxChunks; ++i) {
197     uint32_t ssrc = kSenderSsrc + i;
198     std::ostringstream oss;
199     oss << "cname" << i;
200     EXPECT_TRUE(sdes.WithCName(ssrc, oss.str()));
201   }
202   EXPECT_FALSE(sdes.WithCName(kSenderSsrc + kMaxChunks, "foo"));
203 }
204 
TEST(RtcpPacketTest,CnameItemWithEmptyString)205 TEST(RtcpPacketTest, CnameItemWithEmptyString) {
206   Sdes sdes;
207   EXPECT_TRUE(sdes.WithCName(kSenderSsrc, ""));
208 
209   rtc::scoped_ptr<RawPacket> packet(sdes.Build());
210   RtcpPacketParser parser;
211   parser.Parse(packet->Buffer(), packet->Length());
212   EXPECT_EQ(1, parser.sdes()->num_packets());
213   EXPECT_EQ(1, parser.sdes_chunk()->num_packets());
214   EXPECT_EQ(kSenderSsrc, parser.sdes_chunk()->Ssrc());
215   EXPECT_EQ("", parser.sdes_chunk()->Cname());
216 }
217 
TEST(RtcpPacketTest,Rpsi)218 TEST(RtcpPacketTest, Rpsi) {
219   Rpsi rpsi;
220   // 1000001 (7 bits = 1 byte in native string).
221   const uint64_t kPictureId = 0x41;
222   const uint16_t kNumberOfValidBytes = 1;
223   rpsi.WithPayloadType(100);
224   rpsi.WithPictureId(kPictureId);
225 
226   rtc::scoped_ptr<RawPacket> packet(rpsi.Build());
227   RtcpPacketParser parser;
228   parser.Parse(packet->Buffer(), packet->Length());
229   EXPECT_EQ(100, parser.rpsi()->PayloadType());
230   EXPECT_EQ(kNumberOfValidBytes * 8, parser.rpsi()->NumberOfValidBits());
231   EXPECT_EQ(kPictureId, parser.rpsi()->PictureId());
232 }
233 
TEST(RtcpPacketTest,RpsiWithTwoByteNativeString)234 TEST(RtcpPacketTest, RpsiWithTwoByteNativeString) {
235   Rpsi rpsi;
236   // |1 0000001 (7 bits = 1 byte in native string).
237   const uint64_t kPictureId = 0x81;
238   const uint16_t kNumberOfValidBytes = 2;
239   rpsi.WithPictureId(kPictureId);
240 
241   rtc::scoped_ptr<RawPacket> packet(rpsi.Build());
242   RtcpPacketParser parser;
243   parser.Parse(packet->Buffer(), packet->Length());
244   EXPECT_EQ(kNumberOfValidBytes * 8, parser.rpsi()->NumberOfValidBits());
245   EXPECT_EQ(kPictureId, parser.rpsi()->PictureId());
246 }
247 
TEST(RtcpPacketTest,RpsiWithThreeByteNativeString)248 TEST(RtcpPacketTest, RpsiWithThreeByteNativeString) {
249   Rpsi rpsi;
250   // 10000|00 100000|0 1000000 (7 bits = 1 byte in native string).
251   const uint64_t kPictureId = 0x102040;
252   const uint16_t kNumberOfValidBytes = 3;
253   rpsi.WithPictureId(kPictureId);
254 
255   rtc::scoped_ptr<RawPacket> packet(rpsi.Build());
256   RtcpPacketParser parser;
257   parser.Parse(packet->Buffer(), packet->Length());
258   EXPECT_EQ(kNumberOfValidBytes * 8, parser.rpsi()->NumberOfValidBits());
259   EXPECT_EQ(kPictureId, parser.rpsi()->PictureId());
260 }
261 
TEST(RtcpPacketTest,RpsiWithFourByteNativeString)262 TEST(RtcpPacketTest, RpsiWithFourByteNativeString) {
263   Rpsi rpsi;
264   // 1000|001 00001|01 100001|1 1000010 (7 bits = 1 byte in native string).
265   const uint64_t kPictureId = 0x84161C2;
266   const uint16_t kNumberOfValidBytes = 4;
267   rpsi.WithPictureId(kPictureId);
268 
269   rtc::scoped_ptr<RawPacket> packet(rpsi.Build());
270   RtcpPacketParser parser;
271   parser.Parse(packet->Buffer(), packet->Length());
272   EXPECT_EQ(kNumberOfValidBytes * 8, parser.rpsi()->NumberOfValidBits());
273   EXPECT_EQ(kPictureId, parser.rpsi()->PictureId());
274 }
275 
TEST(RtcpPacketTest,RpsiWithMaxPictureId)276 TEST(RtcpPacketTest, RpsiWithMaxPictureId) {
277   Rpsi rpsi;
278   // 1 1111111| 1111111 1|111111 11|11111 111|1111 1111|111 11111|
279   // 11 111111|1 1111111 (7 bits = 1 byte in native string).
280   const uint64_t kPictureId = 0xffffffffffffffff;
281   const uint16_t kNumberOfValidBytes = 10;
282   rpsi.WithPictureId(kPictureId);
283 
284   rtc::scoped_ptr<RawPacket> packet(rpsi.Build());
285   RtcpPacketParser parser;
286   parser.Parse(packet->Buffer(), packet->Length());
287   EXPECT_EQ(kNumberOfValidBytes * 8, parser.rpsi()->NumberOfValidBits());
288   EXPECT_EQ(kPictureId, parser.rpsi()->PictureId());
289 }
290 
TEST(RtcpPacketTest,Fir)291 TEST(RtcpPacketTest, Fir) {
292   Fir fir;
293   fir.From(kSenderSsrc);
294   fir.To(kRemoteSsrc);
295   fir.WithCommandSeqNum(123);
296 
297   rtc::scoped_ptr<RawPacket> packet(fir.Build());
298   RtcpPacketParser parser;
299   parser.Parse(packet->Buffer(), packet->Length());
300   EXPECT_EQ(1, parser.fir()->num_packets());
301   EXPECT_EQ(kSenderSsrc, parser.fir()->Ssrc());
302   EXPECT_EQ(1, parser.fir_item()->num_packets());
303   EXPECT_EQ(kRemoteSsrc, parser.fir_item()->Ssrc());
304   EXPECT_EQ(123U, parser.fir_item()->SeqNum());
305 }
306 
TEST(RtcpPacketTest,BuildWithTooSmallBuffer)307 TEST(RtcpPacketTest, BuildWithTooSmallBuffer) {
308   ReportBlock rb;
309   ReceiverReport rr;
310   rr.From(kSenderSsrc);
311   EXPECT_TRUE(rr.WithReportBlock(rb));
312 
313   const size_t kRrLength = 8;
314   const size_t kReportBlockLength = 24;
315 
316   // No packet.
317   class Verifier : public rtcp::RtcpPacket::PacketReadyCallback {
318     void OnPacketReady(uint8_t* data, size_t length) override {
319       ADD_FAILURE() << "Packet should not fit within max size.";
320     }
321   } verifier;
322   const size_t kBufferSize = kRrLength + kReportBlockLength - 1;
323   uint8_t buffer[kBufferSize];
324   EXPECT_FALSE(rr.BuildExternalBuffer(buffer, kBufferSize, &verifier));
325 }
326 
TEST(RtcpPacketTest,Remb)327 TEST(RtcpPacketTest, Remb) {
328   Remb remb;
329   remb.From(kSenderSsrc);
330   remb.AppliesTo(kRemoteSsrc);
331   remb.AppliesTo(kRemoteSsrc + 1);
332   remb.AppliesTo(kRemoteSsrc + 2);
333   remb.WithBitrateBps(261011);
334 
335   rtc::scoped_ptr<RawPacket> packet(remb.Build());
336   RtcpPacketParser parser;
337   parser.Parse(packet->Buffer(), packet->Length());
338   EXPECT_EQ(1, parser.psfb_app()->num_packets());
339   EXPECT_EQ(kSenderSsrc, parser.psfb_app()->Ssrc());
340   EXPECT_EQ(1, parser.remb_item()->num_packets());
341   EXPECT_EQ(261011, parser.remb_item()->last_bitrate_bps());
342   std::vector<uint32_t> ssrcs = parser.remb_item()->last_ssrc_list();
343   EXPECT_EQ(kRemoteSsrc, ssrcs[0]);
344   EXPECT_EQ(kRemoteSsrc + 1, ssrcs[1]);
345   EXPECT_EQ(kRemoteSsrc + 2, ssrcs[2]);
346 }
347 
TEST(RtcpPacketTest,XrWithNoReportBlocks)348 TEST(RtcpPacketTest, XrWithNoReportBlocks) {
349   Xr xr;
350   xr.From(kSenderSsrc);
351 
352   rtc::scoped_ptr<RawPacket> packet(xr.Build());
353   RtcpPacketParser parser;
354   parser.Parse(packet->Buffer(), packet->Length());
355   EXPECT_EQ(1, parser.xr_header()->num_packets());
356   EXPECT_EQ(kSenderSsrc, parser.xr_header()->Ssrc());
357 }
358 
TEST(RtcpPacketTest,XrWithRrtr)359 TEST(RtcpPacketTest, XrWithRrtr) {
360   Rrtr rrtr;
361   rrtr.WithNtp(NtpTime(0x11111111, 0x22222222));
362   Xr xr;
363   xr.From(kSenderSsrc);
364   EXPECT_TRUE(xr.WithRrtr(&rrtr));
365 
366   rtc::scoped_ptr<RawPacket> packet(xr.Build());
367   RtcpPacketParser parser;
368   parser.Parse(packet->Buffer(), packet->Length());
369   EXPECT_EQ(1, parser.xr_header()->num_packets());
370   EXPECT_EQ(kSenderSsrc, parser.xr_header()->Ssrc());
371   EXPECT_EQ(1, parser.rrtr()->num_packets());
372   EXPECT_EQ(0x11111111U, parser.rrtr()->NtpSec());
373   EXPECT_EQ(0x22222222U, parser.rrtr()->NtpFrac());
374 }
375 
TEST(RtcpPacketTest,XrWithTwoRrtrBlocks)376 TEST(RtcpPacketTest, XrWithTwoRrtrBlocks) {
377   Rrtr rrtr1;
378   rrtr1.WithNtp(NtpTime(0x11111111, 0x22222222));
379   Rrtr rrtr2;
380   rrtr2.WithNtp(NtpTime(0x33333333, 0x44444444));
381   Xr xr;
382   xr.From(kSenderSsrc);
383   EXPECT_TRUE(xr.WithRrtr(&rrtr1));
384   EXPECT_TRUE(xr.WithRrtr(&rrtr2));
385 
386   rtc::scoped_ptr<RawPacket> packet(xr.Build());
387   RtcpPacketParser parser;
388   parser.Parse(packet->Buffer(), packet->Length());
389   EXPECT_EQ(1, parser.xr_header()->num_packets());
390   EXPECT_EQ(kSenderSsrc, parser.xr_header()->Ssrc());
391   EXPECT_EQ(2, parser.rrtr()->num_packets());
392   EXPECT_EQ(0x33333333U, parser.rrtr()->NtpSec());
393   EXPECT_EQ(0x44444444U, parser.rrtr()->NtpFrac());
394 }
395 
TEST(RtcpPacketTest,XrWithDlrrWithOneSubBlock)396 TEST(RtcpPacketTest, XrWithDlrrWithOneSubBlock) {
397   Dlrr dlrr;
398   EXPECT_TRUE(dlrr.WithDlrrItem(0x11111111, 0x22222222, 0x33333333));
399   Xr xr;
400   xr.From(kSenderSsrc);
401   EXPECT_TRUE(xr.WithDlrr(&dlrr));
402 
403   rtc::scoped_ptr<RawPacket> packet(xr.Build());
404   RtcpPacketParser parser;
405   parser.Parse(packet->Buffer(), packet->Length());
406   EXPECT_EQ(1, parser.xr_header()->num_packets());
407   EXPECT_EQ(kSenderSsrc, parser.xr_header()->Ssrc());
408   EXPECT_EQ(1, parser.dlrr()->num_packets());
409   EXPECT_EQ(1, parser.dlrr_items()->num_packets());
410   EXPECT_EQ(0x11111111U, parser.dlrr_items()->Ssrc(0));
411   EXPECT_EQ(0x22222222U, parser.dlrr_items()->LastRr(0));
412   EXPECT_EQ(0x33333333U, parser.dlrr_items()->DelayLastRr(0));
413 }
414 
TEST(RtcpPacketTest,XrWithDlrrWithTwoSubBlocks)415 TEST(RtcpPacketTest, XrWithDlrrWithTwoSubBlocks) {
416   Dlrr dlrr;
417   EXPECT_TRUE(dlrr.WithDlrrItem(0x11111111, 0x22222222, 0x33333333));
418   EXPECT_TRUE(dlrr.WithDlrrItem(0x44444444, 0x55555555, 0x66666666));
419   Xr xr;
420   xr.From(kSenderSsrc);
421   EXPECT_TRUE(xr.WithDlrr(&dlrr));
422 
423   rtc::scoped_ptr<RawPacket> packet(xr.Build());
424   RtcpPacketParser parser;
425   parser.Parse(packet->Buffer(), packet->Length());
426   EXPECT_EQ(1, parser.xr_header()->num_packets());
427   EXPECT_EQ(kSenderSsrc, parser.xr_header()->Ssrc());
428   EXPECT_EQ(1, parser.dlrr()->num_packets());
429   EXPECT_EQ(2, parser.dlrr_items()->num_packets());
430   EXPECT_EQ(0x11111111U, parser.dlrr_items()->Ssrc(0));
431   EXPECT_EQ(0x22222222U, parser.dlrr_items()->LastRr(0));
432   EXPECT_EQ(0x33333333U, parser.dlrr_items()->DelayLastRr(0));
433   EXPECT_EQ(0x44444444U, parser.dlrr_items()->Ssrc(1));
434   EXPECT_EQ(0x55555555U, parser.dlrr_items()->LastRr(1));
435   EXPECT_EQ(0x66666666U, parser.dlrr_items()->DelayLastRr(1));
436 }
437 
TEST(RtcpPacketTest,DlrrWithTooManySubBlocks)438 TEST(RtcpPacketTest, DlrrWithTooManySubBlocks) {
439   const int kMaxItems = 100;
440   Dlrr dlrr;
441   for (int i = 0; i < kMaxItems; ++i)
442     EXPECT_TRUE(dlrr.WithDlrrItem(i, i, i));
443   EXPECT_FALSE(dlrr.WithDlrrItem(kMaxItems, kMaxItems, kMaxItems));
444 }
445 
TEST(RtcpPacketTest,XrWithTwoDlrrBlocks)446 TEST(RtcpPacketTest, XrWithTwoDlrrBlocks) {
447   Dlrr dlrr1;
448   EXPECT_TRUE(dlrr1.WithDlrrItem(0x11111111, 0x22222222, 0x33333333));
449   Dlrr dlrr2;
450   EXPECT_TRUE(dlrr2.WithDlrrItem(0x44444444, 0x55555555, 0x66666666));
451   Xr xr;
452   xr.From(kSenderSsrc);
453   EXPECT_TRUE(xr.WithDlrr(&dlrr1));
454   EXPECT_TRUE(xr.WithDlrr(&dlrr2));
455 
456   rtc::scoped_ptr<RawPacket> packet(xr.Build());
457   RtcpPacketParser parser;
458   parser.Parse(packet->Buffer(), packet->Length());
459   EXPECT_EQ(1, parser.xr_header()->num_packets());
460   EXPECT_EQ(kSenderSsrc, parser.xr_header()->Ssrc());
461   EXPECT_EQ(2, parser.dlrr()->num_packets());
462   EXPECT_EQ(2, parser.dlrr_items()->num_packets());
463   EXPECT_EQ(0x11111111U, parser.dlrr_items()->Ssrc(0));
464   EXPECT_EQ(0x22222222U, parser.dlrr_items()->LastRr(0));
465   EXPECT_EQ(0x33333333U, parser.dlrr_items()->DelayLastRr(0));
466   EXPECT_EQ(0x44444444U, parser.dlrr_items()->Ssrc(1));
467   EXPECT_EQ(0x55555555U, parser.dlrr_items()->LastRr(1));
468   EXPECT_EQ(0x66666666U, parser.dlrr_items()->DelayLastRr(1));
469 }
470 
TEST(RtcpPacketTest,XrWithVoipMetric)471 TEST(RtcpPacketTest, XrWithVoipMetric) {
472   RTCPVoIPMetric metric;
473   metric.lossRate = 1;
474   metric.discardRate = 2;
475   metric.burstDensity = 3;
476   metric.gapDensity = 4;
477   metric.burstDuration = 0x1111;
478   metric.gapDuration = 0x2222;
479   metric.roundTripDelay = 0x3333;
480   metric.endSystemDelay = 0x4444;
481   metric.signalLevel = 5;
482   metric.noiseLevel = 6;
483   metric.RERL = 7;
484   metric.Gmin = 8;
485   metric.Rfactor = 9;
486   metric.extRfactor = 10;
487   metric.MOSLQ = 11;
488   metric.MOSCQ = 12;
489   metric.RXconfig = 13;
490   metric.JBnominal = 0x5555;
491   metric.JBmax = 0x6666;
492   metric.JBabsMax = 0x7777;
493   VoipMetric metric_block;
494   metric_block.To(kRemoteSsrc);
495   metric_block.WithVoipMetric(metric);
496   Xr xr;
497   xr.From(kSenderSsrc);
498   EXPECT_TRUE(xr.WithVoipMetric(&metric_block));
499 
500   rtc::scoped_ptr<RawPacket> packet(xr.Build());
501   RtcpPacketParser parser;
502   parser.Parse(packet->Buffer(), packet->Length());
503   EXPECT_EQ(1, parser.xr_header()->num_packets());
504   EXPECT_EQ(kSenderSsrc, parser.xr_header()->Ssrc());
505   EXPECT_EQ(1, parser.voip_metric()->num_packets());
506   EXPECT_EQ(kRemoteSsrc, parser.voip_metric()->Ssrc());
507   EXPECT_EQ(1, parser.voip_metric()->LossRate());
508   EXPECT_EQ(2, parser.voip_metric()->DiscardRate());
509   EXPECT_EQ(3, parser.voip_metric()->BurstDensity());
510   EXPECT_EQ(4, parser.voip_metric()->GapDensity());
511   EXPECT_EQ(0x1111, parser.voip_metric()->BurstDuration());
512   EXPECT_EQ(0x2222, parser.voip_metric()->GapDuration());
513   EXPECT_EQ(0x3333, parser.voip_metric()->RoundTripDelay());
514   EXPECT_EQ(0x4444, parser.voip_metric()->EndSystemDelay());
515   EXPECT_EQ(5, parser.voip_metric()->SignalLevel());
516   EXPECT_EQ(6, parser.voip_metric()->NoiseLevel());
517   EXPECT_EQ(7, parser.voip_metric()->Rerl());
518   EXPECT_EQ(8, parser.voip_metric()->Gmin());
519   EXPECT_EQ(9, parser.voip_metric()->Rfactor());
520   EXPECT_EQ(10, parser.voip_metric()->ExtRfactor());
521   EXPECT_EQ(11, parser.voip_metric()->MosLq());
522   EXPECT_EQ(12, parser.voip_metric()->MosCq());
523   EXPECT_EQ(13, parser.voip_metric()->RxConfig());
524   EXPECT_EQ(0x5555, parser.voip_metric()->JbNominal());
525   EXPECT_EQ(0x6666, parser.voip_metric()->JbMax());
526   EXPECT_EQ(0x7777, parser.voip_metric()->JbAbsMax());
527 }
528 
TEST(RtcpPacketTest,XrWithMultipleReportBlocks)529 TEST(RtcpPacketTest, XrWithMultipleReportBlocks) {
530   Rrtr rrtr;
531   Dlrr dlrr;
532   EXPECT_TRUE(dlrr.WithDlrrItem(1, 2, 3));
533   VoipMetric metric;
534   Xr xr;
535   xr.From(kSenderSsrc);
536   EXPECT_TRUE(xr.WithRrtr(&rrtr));
537   EXPECT_TRUE(xr.WithDlrr(&dlrr));
538   EXPECT_TRUE(xr.WithVoipMetric(&metric));
539 
540   rtc::scoped_ptr<RawPacket> packet(xr.Build());
541   RtcpPacketParser parser;
542   parser.Parse(packet->Buffer(), packet->Length());
543   EXPECT_EQ(1, parser.xr_header()->num_packets());
544   EXPECT_EQ(kSenderSsrc, parser.xr_header()->Ssrc());
545   EXPECT_EQ(1, parser.rrtr()->num_packets());
546   EXPECT_EQ(1, parser.dlrr()->num_packets());
547   EXPECT_EQ(1, parser.dlrr_items()->num_packets());
548   EXPECT_EQ(1, parser.voip_metric()->num_packets());
549 }
550 
TEST(RtcpPacketTest,DlrrWithoutItemNotIncludedInPacket)551 TEST(RtcpPacketTest, DlrrWithoutItemNotIncludedInPacket) {
552   Rrtr rrtr;
553   Dlrr dlrr;
554   VoipMetric metric;
555   Xr xr;
556   xr.From(kSenderSsrc);
557   EXPECT_TRUE(xr.WithRrtr(&rrtr));
558   EXPECT_TRUE(xr.WithDlrr(&dlrr));
559   EXPECT_TRUE(xr.WithVoipMetric(&metric));
560 
561   rtc::scoped_ptr<RawPacket> packet(xr.Build());
562   RtcpPacketParser parser;
563   parser.Parse(packet->Buffer(), packet->Length());
564   EXPECT_EQ(1, parser.xr_header()->num_packets());
565   EXPECT_EQ(kSenderSsrc, parser.xr_header()->Ssrc());
566   EXPECT_EQ(1, parser.rrtr()->num_packets());
567   EXPECT_EQ(0, parser.dlrr()->num_packets());
568   EXPECT_EQ(1, parser.voip_metric()->num_packets());
569 }
570 
TEST(RtcpPacketTest,XrWithTooManyBlocks)571 TEST(RtcpPacketTest, XrWithTooManyBlocks) {
572   const int kMaxBlocks = 50;
573   Xr xr;
574 
575   Rrtr rrtr;
576   for (int i = 0; i < kMaxBlocks; ++i)
577     EXPECT_TRUE(xr.WithRrtr(&rrtr));
578   EXPECT_FALSE(xr.WithRrtr(&rrtr));
579 
580   Dlrr dlrr;
581   for (int i = 0; i < kMaxBlocks; ++i)
582     EXPECT_TRUE(xr.WithDlrr(&dlrr));
583   EXPECT_FALSE(xr.WithDlrr(&dlrr));
584 
585   VoipMetric voip_metric;
586   for (int i = 0; i < kMaxBlocks; ++i)
587     EXPECT_TRUE(xr.WithVoipMetric(&voip_metric));
588   EXPECT_FALSE(xr.WithVoipMetric(&voip_metric));
589 }
590 
591 }  // namespace webrtc
592