Home | History | Annotate | Download | only in source
      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/gtest/include/gtest/gtest.h"
     14 
     15 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet.h"
     16 #include "webrtc/test/rtcp_packet_parser.h"
     17 
     18 using webrtc::rtcp::App;
     19 using webrtc::rtcp::Bye;
     20 using webrtc::rtcp::Dlrr;
     21 using webrtc::rtcp::Empty;
     22 using webrtc::rtcp::Fir;
     23 using webrtc::rtcp::Ij;
     24 using webrtc::rtcp::Nack;
     25 using webrtc::rtcp::Pli;
     26 using webrtc::rtcp::Sdes;
     27 using webrtc::rtcp::SenderReport;
     28 using webrtc::rtcp::Sli;
     29 using webrtc::rtcp::RawPacket;
     30 using webrtc::rtcp::ReceiverReport;
     31 using webrtc::rtcp::Remb;
     32 using webrtc::rtcp::ReportBlock;
     33 using webrtc::rtcp::Rpsi;
     34 using webrtc::rtcp::Rrtr;
     35 using webrtc::rtcp::SenderReport;
     36 using webrtc::rtcp::Tmmbn;
     37 using webrtc::rtcp::Tmmbr;
     38 using webrtc::rtcp::VoipMetric;
     39 using webrtc::rtcp::Xr;
     40 using webrtc::test::RtcpPacketParser;
     41 
     42 namespace webrtc {
     43 
     44 const uint32_t kSenderSsrc = 0x12345678;
     45 const uint32_t kRemoteSsrc = 0x23456789;
     46 
     47 TEST(RtcpPacketTest, Rr) {
     48   ReceiverReport rr;
     49   rr.From(kSenderSsrc);
     50 
     51   RawPacket packet = rr.Build();
     52   RtcpPacketParser parser;
     53   parser.Parse(packet.buffer(), packet.buffer_length());
     54   EXPECT_EQ(1, parser.receiver_report()->num_packets());
     55   EXPECT_EQ(kSenderSsrc, parser.receiver_report()->Ssrc());
     56   EXPECT_EQ(0, parser.report_block()->num_packets());
     57 }
     58 
     59 TEST(RtcpPacketTest, RrWithOneReportBlock) {
     60   ReportBlock rb;
     61   rb.To(kRemoteSsrc);
     62   rb.WithFractionLost(55);
     63   rb.WithCumulativeLost(0x111111);
     64   rb.WithExtHighestSeqNum(0x22222222);
     65   rb.WithJitter(0x33333333);
     66   rb.WithLastSr(0x44444444);
     67   rb.WithDelayLastSr(0x55555555);
     68 
     69   ReceiverReport rr;
     70   rr.From(kSenderSsrc);
     71   rr.WithReportBlock(&rb);
     72 
     73   RawPacket packet = rr.Build();
     74   RtcpPacketParser parser;
     75   parser.Parse(packet.buffer(), packet.buffer_length());
     76   EXPECT_EQ(1, parser.receiver_report()->num_packets());
     77   EXPECT_EQ(kSenderSsrc, parser.receiver_report()->Ssrc());
     78   EXPECT_EQ(1, parser.report_block()->num_packets());
     79   EXPECT_EQ(kRemoteSsrc, parser.report_block()->Ssrc());
     80   EXPECT_EQ(55U, parser.report_block()->FractionLost());
     81   EXPECT_EQ(0x111111U, parser.report_block()->CumPacketLost());
     82   EXPECT_EQ(0x22222222U, parser.report_block()->ExtHighestSeqNum());
     83   EXPECT_EQ(0x33333333U, parser.report_block()->Jitter());
     84   EXPECT_EQ(0x44444444U, parser.report_block()->LastSr());
     85   EXPECT_EQ(0x55555555U, parser.report_block()->DelayLastSr());
     86 }
     87 
     88 TEST(RtcpPacketTest, RrWithTwoReportBlocks) {
     89   ReportBlock rb1;
     90   rb1.To(kRemoteSsrc);
     91   ReportBlock rb2;
     92   rb2.To(kRemoteSsrc + 1);
     93 
     94   ReceiverReport rr;
     95   rr.From(kSenderSsrc);
     96   rr.WithReportBlock(&rb1);
     97   rr.WithReportBlock(&rb2);
     98 
     99   RawPacket packet = rr.Build();
    100   RtcpPacketParser parser;
    101   parser.Parse(packet.buffer(), packet.buffer_length());
    102   EXPECT_EQ(1, parser.receiver_report()->num_packets());
    103   EXPECT_EQ(kSenderSsrc, parser.receiver_report()->Ssrc());
    104   EXPECT_EQ(2, parser.report_block()->num_packets());
    105   EXPECT_EQ(1, parser.report_blocks_per_ssrc(kRemoteSsrc));
    106   EXPECT_EQ(1, parser.report_blocks_per_ssrc(kRemoteSsrc + 1));
    107 }
    108 
    109 TEST(RtcpPacketTest, Sr) {
    110   SenderReport sr;
    111   sr.From(kSenderSsrc);
    112   sr.WithNtpSec(0x11111111);
    113   sr.WithNtpFrac(0x22222222);
    114   sr.WithRtpTimestamp(0x33333333);
    115   sr.WithPacketCount(0x44444444);
    116   sr.WithOctetCount(0x55555555);
    117 
    118   RawPacket packet = sr.Build();
    119   RtcpPacketParser parser;
    120   parser.Parse(packet.buffer(), packet.buffer_length());
    121 
    122   EXPECT_EQ(1, parser.sender_report()->num_packets());
    123   EXPECT_EQ(kSenderSsrc, parser.sender_report()->Ssrc());
    124   EXPECT_EQ(0x11111111U, parser.sender_report()->NtpSec());
    125   EXPECT_EQ(0x22222222U, parser.sender_report()->NtpFrac());
    126   EXPECT_EQ(0x33333333U, parser.sender_report()->RtpTimestamp());
    127   EXPECT_EQ(0x44444444U, parser.sender_report()->PacketCount());
    128   EXPECT_EQ(0x55555555U, parser.sender_report()->OctetCount());
    129   EXPECT_EQ(0, parser.report_block()->num_packets());
    130 }
    131 
    132 TEST(RtcpPacketTest, SrWithOneReportBlock) {
    133   ReportBlock rb;
    134   rb.To(kRemoteSsrc);
    135 
    136   SenderReport sr;
    137   sr.From(kSenderSsrc);
    138   sr.WithReportBlock(&rb);
    139 
    140   RawPacket packet = sr.Build();
    141   RtcpPacketParser parser;
    142   parser.Parse(packet.buffer(), packet.buffer_length());
    143   EXPECT_EQ(1, parser.sender_report()->num_packets());
    144   EXPECT_EQ(kSenderSsrc, parser.sender_report()->Ssrc());
    145   EXPECT_EQ(1, parser.report_block()->num_packets());
    146   EXPECT_EQ(kRemoteSsrc, parser.report_block()->Ssrc());
    147 }
    148 
    149 TEST(RtcpPacketTest, SrWithTwoReportBlocks) {
    150   ReportBlock rb1;
    151   rb1.To(kRemoteSsrc);
    152   ReportBlock rb2;
    153   rb2.To(kRemoteSsrc + 1);
    154 
    155   SenderReport sr;
    156   sr.From(kSenderSsrc);
    157   sr.WithReportBlock(&rb1);
    158   sr.WithReportBlock(&rb2);
    159 
    160   RawPacket packet = sr.Build();
    161   RtcpPacketParser parser;
    162   parser.Parse(packet.buffer(), packet.buffer_length());
    163   EXPECT_EQ(1, parser.sender_report()->num_packets());
    164   EXPECT_EQ(kSenderSsrc, parser.sender_report()->Ssrc());
    165   EXPECT_EQ(2, parser.report_block()->num_packets());
    166   EXPECT_EQ(1, parser.report_blocks_per_ssrc(kRemoteSsrc));
    167   EXPECT_EQ(1, parser.report_blocks_per_ssrc(kRemoteSsrc + 1));
    168 }
    169 
    170 TEST(RtcpPacketTest, IjNoItem) {
    171   Ij ij;
    172 
    173   RawPacket packet = ij.Build();
    174   RtcpPacketParser parser;
    175   parser.Parse(packet.buffer(), packet.buffer_length());
    176   EXPECT_EQ(1, parser.ij()->num_packets());
    177   EXPECT_EQ(0, parser.ij_item()->num_packets());
    178 }
    179 
    180 TEST(RtcpPacketTest, IjOneItem) {
    181   Ij ij;
    182   ij.WithJitterItem(0x11111111);
    183 
    184   RawPacket packet = ij.Build();
    185   RtcpPacketParser parser;
    186   parser.Parse(packet.buffer(), packet.buffer_length());
    187   EXPECT_EQ(1, parser.ij()->num_packets());
    188   EXPECT_EQ(1, parser.ij_item()->num_packets());
    189   EXPECT_EQ(0x11111111U, parser.ij_item()->Jitter());
    190 }
    191 
    192 TEST(RtcpPacketTest, IjTwoItems) {
    193   Ij ij;
    194   ij.WithJitterItem(0x11111111);
    195   ij.WithJitterItem(0x22222222);
    196 
    197   RawPacket packet = ij.Build();
    198   RtcpPacketParser parser;
    199   parser.Parse(packet.buffer(), packet.buffer_length());
    200   EXPECT_EQ(1, parser.ij()->num_packets());
    201   EXPECT_EQ(2, parser.ij_item()->num_packets());
    202   EXPECT_EQ(0x22222222U, parser.ij_item()->Jitter());
    203 }
    204 
    205 TEST(RtcpPacketTest, AppWithNoData) {
    206   App app;
    207   app.WithSubType(30);
    208   uint32_t name = 'n' << 24;
    209   name += 'a' << 16;
    210   name += 'm' << 8;
    211   name += 'e';
    212   app.WithName(name);
    213 
    214   RawPacket packet = app.Build();
    215   RtcpPacketParser parser;
    216   parser.Parse(packet.buffer(), packet.buffer_length());
    217   EXPECT_EQ(1, parser.app()->num_packets());
    218   EXPECT_EQ(30U, parser.app()->SubType());
    219   EXPECT_EQ(name, parser.app()->Name());
    220   EXPECT_EQ(0, parser.app_item()->num_packets());
    221 }
    222 
    223 TEST(RtcpPacketTest, App) {
    224   App app;
    225   app.From(kSenderSsrc);
    226   app.WithSubType(30);
    227   uint32_t name = 'n' << 24;
    228   name += 'a' << 16;
    229   name += 'm' << 8;
    230   name += 'e';
    231   app.WithName(name);
    232   const char kData[] = {'t', 'e', 's', 't', 'd', 'a', 't', 'a'};
    233   const size_t kDataLength = sizeof(kData) / sizeof(kData[0]);
    234   app.WithData((const uint8_t*)kData, kDataLength);
    235 
    236   RawPacket packet = app.Build();
    237   RtcpPacketParser parser;
    238   parser.Parse(packet.buffer(), packet.buffer_length());
    239   EXPECT_EQ(1, parser.app()->num_packets());
    240   EXPECT_EQ(30U, parser.app()->SubType());
    241   EXPECT_EQ(name, parser.app()->Name());
    242   EXPECT_EQ(1, parser.app_item()->num_packets());
    243   EXPECT_EQ(kDataLength, parser.app_item()->DataLength());
    244   EXPECT_EQ(0, strncmp(kData, (const char*)parser.app_item()->Data(),
    245       parser.app_item()->DataLength()));
    246 }
    247 
    248 TEST(RtcpPacketTest, SdesWithOneChunk) {
    249   Sdes sdes;
    250   sdes.WithCName(kSenderSsrc, "alice@host");
    251 
    252   RawPacket packet = sdes.Build();
    253   RtcpPacketParser parser;
    254   parser.Parse(packet.buffer(), packet.buffer_length());
    255   EXPECT_EQ(1, parser.sdes()->num_packets());
    256   EXPECT_EQ(1, parser.sdes_chunk()->num_packets());
    257   EXPECT_EQ(kSenderSsrc, parser.sdes_chunk()->Ssrc());
    258   EXPECT_EQ("alice@host", parser.sdes_chunk()->Cname());
    259 }
    260 
    261 TEST(RtcpPacketTest, SdesWithMultipleChunks) {
    262   Sdes sdes;
    263   sdes.WithCName(kSenderSsrc, "a");
    264   sdes.WithCName(kSenderSsrc + 1, "ab");
    265   sdes.WithCName(kSenderSsrc + 2, "abc");
    266   sdes.WithCName(kSenderSsrc + 3, "abcd");
    267   sdes.WithCName(kSenderSsrc + 4, "abcde");
    268   sdes.WithCName(kSenderSsrc + 5, "abcdef");
    269 
    270   RawPacket packet = sdes.Build();
    271   RtcpPacketParser parser;
    272   parser.Parse(packet.buffer(), packet.buffer_length());
    273   EXPECT_EQ(1, parser.sdes()->num_packets());
    274   EXPECT_EQ(6, parser.sdes_chunk()->num_packets());
    275   EXPECT_EQ(kSenderSsrc + 5, parser.sdes_chunk()->Ssrc());
    276   EXPECT_EQ("abcdef", parser.sdes_chunk()->Cname());
    277 }
    278 
    279 TEST(RtcpPacketTest, CnameItemWithEmptyString) {
    280   Sdes sdes;
    281   sdes.WithCName(kSenderSsrc, "");
    282 
    283   RawPacket packet = sdes.Build();
    284   RtcpPacketParser parser;
    285   parser.Parse(packet.buffer(), packet.buffer_length());
    286   EXPECT_EQ(1, parser.sdes()->num_packets());
    287   EXPECT_EQ(1, parser.sdes_chunk()->num_packets());
    288   EXPECT_EQ(kSenderSsrc, parser.sdes_chunk()->Ssrc());
    289   EXPECT_EQ("", parser.sdes_chunk()->Cname());
    290 }
    291 
    292 TEST(RtcpPacketTest, Pli) {
    293   Pli pli;
    294   pli.From(kSenderSsrc);
    295   pli.To(kRemoteSsrc);
    296 
    297   RawPacket packet = pli.Build();
    298   RtcpPacketParser parser;
    299   parser.Parse(packet.buffer(), packet.buffer_length());
    300   EXPECT_EQ(1, parser.pli()->num_packets());
    301   EXPECT_EQ(kSenderSsrc, parser.pli()->Ssrc());
    302   EXPECT_EQ(kRemoteSsrc, parser.pli()->MediaSsrc());
    303 }
    304 
    305 TEST(RtcpPacketTest, Sli) {
    306   const uint16_t kFirstMb = 7777;
    307   const uint16_t kNumberOfMb = 6666;
    308   const uint8_t kPictureId = 60;
    309   Sli sli;
    310   sli.From(kSenderSsrc);
    311   sli.To(kRemoteSsrc);
    312   sli.WithFirstMb(kFirstMb);
    313   sli.WithNumberOfMb(kNumberOfMb);
    314   sli.WithPictureId(kPictureId);
    315 
    316   RawPacket packet = sli.Build();
    317   RtcpPacketParser parser;
    318   parser.Parse(packet.buffer(), packet.buffer_length());
    319   EXPECT_EQ(1, parser.sli()->num_packets());
    320   EXPECT_EQ(kSenderSsrc, parser.sli()->Ssrc());
    321   EXPECT_EQ(kRemoteSsrc, parser.sli()->MediaSsrc());
    322   EXPECT_EQ(1, parser.sli_item()->num_packets());
    323   EXPECT_EQ(kFirstMb, parser.sli_item()->FirstMb());
    324   EXPECT_EQ(kNumberOfMb, parser.sli_item()->NumberOfMb());
    325   EXPECT_EQ(kPictureId, parser.sli_item()->PictureId());
    326 }
    327 
    328 TEST(RtcpPacketTest, Nack) {
    329   Nack nack;
    330   const uint16_t kList[] = {0, 1, 3, 8, 16};
    331   const uint16_t kListLength = sizeof(kList) / sizeof(kList[0]);
    332   nack.From(kSenderSsrc);
    333   nack.To(kRemoteSsrc);
    334   nack.WithList(kList, kListLength);
    335   RawPacket packet = nack.Build();
    336   RtcpPacketParser parser;
    337   parser.Parse(packet.buffer(), packet.buffer_length());
    338   EXPECT_EQ(1, parser.nack()->num_packets());
    339   EXPECT_EQ(kSenderSsrc, parser.nack()->Ssrc());
    340   EXPECT_EQ(kRemoteSsrc, parser.nack()->MediaSsrc());
    341   EXPECT_EQ(1, parser.nack_item()->num_packets());
    342   std::vector<uint16_t> seqs = parser.nack_item()->last_nack_list();
    343   EXPECT_EQ(kListLength, seqs.size());
    344   for (size_t i = 0; i < kListLength; ++i) {
    345     EXPECT_EQ(kList[i], seqs[i]);
    346   }
    347 }
    348 
    349 TEST(RtcpPacketTest, NackWithWrap) {
    350   Nack nack;
    351   const uint16_t kList[] = {65500, 65516, 65534, 65535, 0, 1, 3, 20, 100};
    352   const uint16_t kListLength = sizeof(kList) / sizeof(kList[0]);
    353   nack.From(kSenderSsrc);
    354   nack.To(kRemoteSsrc);
    355   nack.WithList(kList, kListLength);
    356   RawPacket packet = nack.Build();
    357   RtcpPacketParser parser;
    358   parser.Parse(packet.buffer(), packet.buffer_length());
    359   EXPECT_EQ(1, parser.nack()->num_packets());
    360   EXPECT_EQ(kSenderSsrc, parser.nack()->Ssrc());
    361   EXPECT_EQ(kRemoteSsrc, parser.nack()->MediaSsrc());
    362   EXPECT_EQ(4, parser.nack_item()->num_packets());
    363   std::vector<uint16_t> seqs = parser.nack_item()->last_nack_list();
    364   EXPECT_EQ(kListLength, seqs.size());
    365   for (size_t i = 0; i < kListLength; ++i) {
    366     EXPECT_EQ(kList[i], seqs[i]);
    367   }
    368 }
    369 
    370 TEST(RtcpPacketTest, Rpsi) {
    371   Rpsi rpsi;
    372   // 1000001 (7 bits = 1 byte in native string).
    373   const uint64_t kPictureId = 0x41;
    374   const uint16_t kNumberOfValidBytes = 1;
    375   rpsi.WithPayloadType(100);
    376   rpsi.WithPictureId(kPictureId);
    377 
    378   RawPacket packet = rpsi.Build();
    379   RtcpPacketParser parser;
    380   parser.Parse(packet.buffer(), packet.buffer_length());
    381   EXPECT_EQ(100, parser.rpsi()->PayloadType());
    382   EXPECT_EQ(kNumberOfValidBytes * 8, parser.rpsi()->NumberOfValidBits());
    383   EXPECT_EQ(kPictureId, parser.rpsi()->PictureId());
    384 }
    385 
    386 TEST(RtcpPacketTest, RpsiWithTwoByteNativeString) {
    387   Rpsi rpsi;
    388   // |1 0000001 (7 bits = 1 byte in native string).
    389   const uint64_t kPictureId = 0x81;
    390   const uint16_t kNumberOfValidBytes = 2;
    391   rpsi.WithPictureId(kPictureId);
    392 
    393   RawPacket packet = rpsi.Build();
    394   RtcpPacketParser parser;
    395   parser.Parse(packet.buffer(), packet.buffer_length());
    396   EXPECT_EQ(kNumberOfValidBytes * 8, parser.rpsi()->NumberOfValidBits());
    397   EXPECT_EQ(kPictureId, parser.rpsi()->PictureId());
    398 }
    399 
    400 TEST(RtcpPacketTest, RpsiWithThreeByteNativeString) {
    401   Rpsi rpsi;
    402   // 10000|00 100000|0 1000000 (7 bits = 1 byte in native string).
    403   const uint64_t kPictureId = 0x102040;
    404   const uint16_t kNumberOfValidBytes = 3;
    405   rpsi.WithPictureId(kPictureId);
    406 
    407   RawPacket packet = rpsi.Build();
    408   RtcpPacketParser parser;
    409   parser.Parse(packet.buffer(), packet.buffer_length());
    410   EXPECT_EQ(kNumberOfValidBytes * 8, parser.rpsi()->NumberOfValidBits());
    411   EXPECT_EQ(kPictureId, parser.rpsi()->PictureId());
    412 }
    413 
    414 TEST(RtcpPacketTest, RpsiWithFourByteNativeString) {
    415   Rpsi rpsi;
    416   // 1000|001 00001|01 100001|1 1000010 (7 bits = 1 byte in native string).
    417   const uint64_t kPictureId = 0x84161C2;
    418   const uint16_t kNumberOfValidBytes = 4;
    419   rpsi.WithPictureId(kPictureId);
    420 
    421   RawPacket packet = rpsi.Build();
    422   RtcpPacketParser parser;
    423   parser.Parse(packet.buffer(), packet.buffer_length());
    424   EXPECT_EQ(kNumberOfValidBytes * 8, parser.rpsi()->NumberOfValidBits());
    425   EXPECT_EQ(kPictureId, parser.rpsi()->PictureId());
    426 }
    427 
    428 TEST(RtcpPacketTest, RpsiWithMaxPictureId) {
    429   Rpsi rpsi;
    430   // 1 1111111| 1111111 1|111111 11|11111 111|1111 1111|111 11111|
    431   // 11 111111|1 1111111 (7 bits = 1 byte in native string).
    432   const uint64_t kPictureId = 0xffffffffffffffff;
    433   const uint16_t kNumberOfValidBytes = 10;
    434   rpsi.WithPictureId(kPictureId);
    435 
    436   RawPacket packet = rpsi.Build();
    437   RtcpPacketParser parser;
    438   parser.Parse(packet.buffer(), packet.buffer_length());
    439   EXPECT_EQ(kNumberOfValidBytes * 8, parser.rpsi()->NumberOfValidBits());
    440   EXPECT_EQ(kPictureId, parser.rpsi()->PictureId());
    441 }
    442 
    443 TEST(RtcpPacketTest, Fir) {
    444   Fir fir;
    445   fir.From(kSenderSsrc);
    446   fir.To(kRemoteSsrc);
    447   fir.WithCommandSeqNum(123);
    448 
    449   RawPacket packet = fir.Build();
    450   RtcpPacketParser parser;
    451   parser.Parse(packet.buffer(), packet.buffer_length());
    452   EXPECT_EQ(1, parser.fir()->num_packets());
    453   EXPECT_EQ(kSenderSsrc, parser.fir()->Ssrc());
    454   EXPECT_EQ(1, parser.fir_item()->num_packets());
    455   EXPECT_EQ(kRemoteSsrc, parser.fir_item()->Ssrc());
    456   EXPECT_EQ(123U, parser.fir_item()->SeqNum());
    457 }
    458 
    459 TEST(RtcpPacketTest, AppendPacket) {
    460   Fir fir;
    461   ReportBlock rb;
    462   ReceiverReport rr;
    463   rr.From(kSenderSsrc);
    464   rr.WithReportBlock(&rb);
    465   rr.Append(&fir);
    466 
    467   RawPacket packet = rr.Build();
    468   RtcpPacketParser parser;
    469   parser.Parse(packet.buffer(), packet.buffer_length());
    470   EXPECT_EQ(1, parser.receiver_report()->num_packets());
    471   EXPECT_EQ(kSenderSsrc, parser.receiver_report()->Ssrc());
    472   EXPECT_EQ(1, parser.report_block()->num_packets());
    473   EXPECT_EQ(1, parser.fir()->num_packets());
    474 }
    475 
    476 TEST(RtcpPacketTest, AppendPacketOnEmpty) {
    477   Empty empty;
    478   ReceiverReport rr;
    479   rr.From(kSenderSsrc);
    480   empty.Append(&rr);
    481 
    482   RawPacket packet = empty.Build();
    483   RtcpPacketParser parser;
    484   parser.Parse(packet.buffer(), packet.buffer_length());
    485   EXPECT_EQ(1, parser.receiver_report()->num_packets());
    486   EXPECT_EQ(0, parser.report_block()->num_packets());
    487 }
    488 
    489 TEST(RtcpPacketTest, AppendPacketWithOwnAppendedPacket) {
    490   Fir fir;
    491   Bye bye;
    492   ReportBlock rb;
    493 
    494   ReceiverReport rr;
    495   rr.WithReportBlock(&rb);
    496   rr.Append(&fir);
    497 
    498   SenderReport sr;
    499   sr.Append(&bye);
    500   sr.Append(&rr);
    501 
    502   RawPacket packet = sr.Build();
    503   RtcpPacketParser parser;
    504   parser.Parse(packet.buffer(), packet.buffer_length());
    505   EXPECT_EQ(1, parser.sender_report()->num_packets());
    506   EXPECT_EQ(1, parser.receiver_report()->num_packets());
    507   EXPECT_EQ(1, parser.report_block()->num_packets());
    508   EXPECT_EQ(1, parser.bye()->num_packets());
    509   EXPECT_EQ(1, parser.fir()->num_packets());
    510 }
    511 
    512 TEST(RtcpPacketTest, Bye) {
    513   Bye bye;
    514   bye.From(kSenderSsrc);
    515 
    516   RawPacket packet = bye.Build();
    517   RtcpPacketParser parser;
    518   parser.Parse(packet.buffer(), packet.buffer_length());
    519   EXPECT_EQ(1, parser.bye()->num_packets());
    520   EXPECT_EQ(kSenderSsrc, parser.bye()->Ssrc());
    521 }
    522 
    523 TEST(RtcpPacketTest, ByeWithCsrcs) {
    524   Fir fir;
    525   Bye bye;
    526   bye.From(kSenderSsrc);
    527   bye.WithCsrc(0x22222222);
    528   bye.WithCsrc(0x33333333);
    529   bye.Append(&fir);
    530 
    531   RawPacket packet = bye.Build();
    532   RtcpPacketParser parser;
    533   parser.Parse(packet.buffer(), packet.buffer_length());
    534   EXPECT_EQ(1, parser.bye()->num_packets());
    535   EXPECT_EQ(kSenderSsrc, parser.bye()->Ssrc());
    536   EXPECT_EQ(1, parser.fir()->num_packets());
    537 }
    538 
    539 TEST(RtcpPacketTest, BuildWithInputBuffer) {
    540   Fir fir;
    541   ReportBlock rb;
    542   ReceiverReport rr;
    543   rr.From(kSenderSsrc);
    544   rr.WithReportBlock(&rb);
    545   rr.Append(&fir);
    546 
    547   const size_t kRrLength = 8;
    548   const size_t kReportBlockLength = 24;
    549   const size_t kFirLength = 20;
    550 
    551   size_t len = 0;
    552   uint8_t packet[kRrLength + kReportBlockLength + kFirLength];
    553   rr.Build(packet, &len, kRrLength + kReportBlockLength + kFirLength);
    554 
    555   RtcpPacketParser parser;
    556   parser.Parse(packet, len);
    557   EXPECT_EQ(1, parser.receiver_report()->num_packets());
    558   EXPECT_EQ(1, parser.report_block()->num_packets());
    559   EXPECT_EQ(1, parser.fir()->num_packets());
    560 }
    561 
    562 TEST(RtcpPacketTest, BuildWithTooSmallBuffer) {
    563   ReportBlock rb;
    564   ReceiverReport rr;
    565   rr.From(kSenderSsrc);
    566   rr.WithReportBlock(&rb);
    567 
    568   const size_t kRrLength = 8;
    569   const size_t kReportBlockLength = 24;
    570 
    571   // No packet.
    572   size_t len = 0;
    573   uint8_t packet[kRrLength + kReportBlockLength - 1];
    574   rr.Build(packet, &len, kRrLength + kReportBlockLength - 1);
    575   RtcpPacketParser parser;
    576   parser.Parse(packet, len);
    577   EXPECT_EQ(0U, len);
    578 }
    579 
    580 TEST(RtcpPacketTest, BuildWithTooSmallBuffer_LastBlockFits) {
    581   Fir fir;
    582   ReportBlock rb;
    583   ReceiverReport rr;
    584   rr.From(kSenderSsrc);
    585   rr.WithReportBlock(&rb);
    586   rr.Append(&fir);
    587 
    588   const size_t kRrLength = 8;
    589   const size_t kReportBlockLength = 24;
    590 
    591   size_t len = 0;
    592   uint8_t packet[kRrLength + kReportBlockLength - 1];
    593   rr.Build(packet, &len, kRrLength + kReportBlockLength - 1);
    594   RtcpPacketParser parser;
    595   parser.Parse(packet, len);
    596   EXPECT_EQ(0, parser.receiver_report()->num_packets());
    597   EXPECT_EQ(0, parser.report_block()->num_packets());
    598   EXPECT_EQ(1, parser.fir()->num_packets());
    599 }
    600 
    601 TEST(RtcpPacketTest, Remb) {
    602   Remb remb;
    603   remb.From(kSenderSsrc);
    604   remb.AppliesTo(kRemoteSsrc);
    605   remb.AppliesTo(kRemoteSsrc + 1);
    606   remb.AppliesTo(kRemoteSsrc + 2);
    607   remb.WithBitrateBps(261011);
    608 
    609   RawPacket packet = remb.Build();
    610   RtcpPacketParser parser;
    611   parser.Parse(packet.buffer(), packet.buffer_length());
    612   EXPECT_EQ(1, parser.psfb_app()->num_packets());
    613   EXPECT_EQ(kSenderSsrc, parser.psfb_app()->Ssrc());
    614   EXPECT_EQ(1, parser.remb_item()->num_packets());
    615   EXPECT_EQ(261011, parser.remb_item()->last_bitrate_bps());
    616   std::vector<uint32_t> ssrcs = parser.remb_item()->last_ssrc_list();
    617   EXPECT_EQ(kRemoteSsrc, ssrcs[0]);
    618   EXPECT_EQ(kRemoteSsrc + 1, ssrcs[1]);
    619   EXPECT_EQ(kRemoteSsrc + 2, ssrcs[2]);
    620 }
    621 
    622 TEST(RtcpPacketTest, Tmmbr) {
    623   Tmmbr tmmbr;
    624   tmmbr.From(kSenderSsrc);
    625   tmmbr.To(kRemoteSsrc);
    626   tmmbr.WithBitrateKbps(312);
    627   tmmbr.WithOverhead(60);
    628 
    629   RawPacket packet = tmmbr.Build();
    630   RtcpPacketParser parser;
    631   parser.Parse(packet.buffer(), packet.buffer_length());
    632   EXPECT_EQ(1, parser.tmmbr()->num_packets());
    633   EXPECT_EQ(kSenderSsrc, parser.tmmbr()->Ssrc());
    634   EXPECT_EQ(1, parser.tmmbr_item()->num_packets());
    635   EXPECT_EQ(312U, parser.tmmbr_item()->BitrateKbps());
    636   EXPECT_EQ(60U, parser.tmmbr_item()->Overhead());
    637 }
    638 
    639 TEST(RtcpPacketTest, TmmbnWithNoItem) {
    640   Tmmbn tmmbn;
    641   tmmbn.From(kSenderSsrc);
    642 
    643   RawPacket packet = tmmbn.Build();
    644   RtcpPacketParser parser;
    645   parser.Parse(packet.buffer(), packet.buffer_length());
    646   EXPECT_EQ(1, parser.tmmbn()->num_packets());
    647   EXPECT_EQ(kSenderSsrc, parser.tmmbn()->Ssrc());
    648   EXPECT_EQ(0, parser.tmmbn_items()->num_packets());
    649 }
    650 
    651 TEST(RtcpPacketTest, TmmbnWithOneItem) {
    652   Tmmbn tmmbn;
    653   tmmbn.From(kSenderSsrc);
    654   tmmbn.WithTmmbr(kRemoteSsrc, 312, 60);
    655 
    656   RawPacket packet = tmmbn.Build();
    657   RtcpPacketParser parser;
    658   parser.Parse(packet.buffer(), packet.buffer_length());
    659   EXPECT_EQ(1, parser.tmmbn()->num_packets());
    660   EXPECT_EQ(kSenderSsrc, parser.tmmbn()->Ssrc());
    661   EXPECT_EQ(1, parser.tmmbn_items()->num_packets());
    662   EXPECT_EQ(kRemoteSsrc, parser.tmmbn_items()->Ssrc(0));
    663   EXPECT_EQ(312U, parser.tmmbn_items()->BitrateKbps(0));
    664   EXPECT_EQ(60U, parser.tmmbn_items()->Overhead(0));
    665 }
    666 
    667 TEST(RtcpPacketTest, TmmbnWithTwoItems) {
    668   Tmmbn tmmbn;
    669   tmmbn.From(kSenderSsrc);
    670   tmmbn.WithTmmbr(kRemoteSsrc, 312, 60);
    671   tmmbn.WithTmmbr(kRemoteSsrc + 1, 1288, 40);
    672 
    673   RawPacket packet = tmmbn.Build();
    674   RtcpPacketParser parser;
    675   parser.Parse(packet.buffer(), packet.buffer_length());
    676   EXPECT_EQ(1, parser.tmmbn()->num_packets());
    677   EXPECT_EQ(kSenderSsrc, parser.tmmbn()->Ssrc());
    678   EXPECT_EQ(2, parser.tmmbn_items()->num_packets());
    679   EXPECT_EQ(kRemoteSsrc, parser.tmmbn_items()->Ssrc(0));
    680   EXPECT_EQ(312U, parser.tmmbn_items()->BitrateKbps(0));
    681   EXPECT_EQ(60U, parser.tmmbn_items()->Overhead(0));
    682   EXPECT_EQ(kRemoteSsrc + 1, parser.tmmbn_items()->Ssrc(1));
    683   EXPECT_EQ(1288U, parser.tmmbn_items()->BitrateKbps(1));
    684   EXPECT_EQ(40U, parser.tmmbn_items()->Overhead(1));
    685 }
    686 
    687 TEST(RtcpPacketTest, XrWithNoReportBlocks) {
    688   Xr xr;
    689   xr.From(kSenderSsrc);
    690 
    691   RawPacket packet = xr.Build();
    692   RtcpPacketParser parser;
    693   parser.Parse(packet.buffer(), packet.buffer_length());
    694   EXPECT_EQ(1, parser.xr_header()->num_packets());
    695   EXPECT_EQ(kSenderSsrc, parser.xr_header()->Ssrc());
    696 }
    697 
    698 TEST(RtcpPacketTest, XrWithRrtr) {
    699   Rrtr rrtr;
    700   rrtr.WithNtpSec(0x11111111);
    701   rrtr.WithNtpFrac(0x22222222);
    702   Xr xr;
    703   xr.From(kSenderSsrc);
    704   xr.WithRrtr(&rrtr);
    705 
    706   RawPacket packet = xr.Build();
    707   RtcpPacketParser parser;
    708   parser.Parse(packet.buffer(), packet.buffer_length());
    709   EXPECT_EQ(1, parser.xr_header()->num_packets());
    710   EXPECT_EQ(kSenderSsrc, parser.xr_header()->Ssrc());
    711   EXPECT_EQ(1, parser.rrtr()->num_packets());
    712   EXPECT_EQ(0x11111111U, parser.rrtr()->NtpSec());
    713   EXPECT_EQ(0x22222222U, parser.rrtr()->NtpFrac());
    714 }
    715 
    716 TEST(RtcpPacketTest, XrWithTwoRrtrBlocks) {
    717   Rrtr rrtr1;
    718   rrtr1.WithNtpSec(0x11111111);
    719   rrtr1.WithNtpFrac(0x22222222);
    720   Rrtr rrtr2;
    721   rrtr2.WithNtpSec(0x33333333);
    722   rrtr2.WithNtpFrac(0x44444444);
    723   Xr xr;
    724   xr.From(kSenderSsrc);
    725   xr.WithRrtr(&rrtr1);
    726   xr.WithRrtr(&rrtr2);
    727 
    728   RawPacket packet = xr.Build();
    729   RtcpPacketParser parser;
    730   parser.Parse(packet.buffer(), packet.buffer_length());
    731   EXPECT_EQ(1, parser.xr_header()->num_packets());
    732   EXPECT_EQ(kSenderSsrc, parser.xr_header()->Ssrc());
    733   EXPECT_EQ(2, parser.rrtr()->num_packets());
    734   EXPECT_EQ(0x33333333U, parser.rrtr()->NtpSec());
    735   EXPECT_EQ(0x44444444U, parser.rrtr()->NtpFrac());
    736 }
    737 
    738 TEST(RtcpPacketTest, XrWithDlrrWithOneSubBlock) {
    739   Dlrr dlrr;
    740   dlrr.WithDlrrItem(0x11111111, 0x22222222, 0x33333333);
    741   Xr xr;
    742   xr.From(kSenderSsrc);
    743   xr.WithDlrr(&dlrr);
    744 
    745   RawPacket packet = xr.Build();
    746   RtcpPacketParser parser;
    747   parser.Parse(packet.buffer(), packet.buffer_length());
    748   EXPECT_EQ(1, parser.xr_header()->num_packets());
    749   EXPECT_EQ(kSenderSsrc, parser.xr_header()->Ssrc());
    750   EXPECT_EQ(1, parser.dlrr()->num_packets());
    751   EXPECT_EQ(1, parser.dlrr_items()->num_packets());
    752   EXPECT_EQ(0x11111111U, parser.dlrr_items()->Ssrc(0));
    753   EXPECT_EQ(0x22222222U, parser.dlrr_items()->LastRr(0));
    754   EXPECT_EQ(0x33333333U, parser.dlrr_items()->DelayLastRr(0));
    755 }
    756 
    757 TEST(RtcpPacketTest, XrWithDlrrWithTwoSubBlocks) {
    758   Dlrr dlrr;
    759   dlrr.WithDlrrItem(0x11111111, 0x22222222, 0x33333333);
    760   dlrr.WithDlrrItem(0x44444444, 0x55555555, 0x66666666);
    761   Xr xr;
    762   xr.From(kSenderSsrc);
    763   xr.WithDlrr(&dlrr);
    764 
    765   RawPacket packet = xr.Build();
    766   RtcpPacketParser parser;
    767   parser.Parse(packet.buffer(), packet.buffer_length());
    768   EXPECT_EQ(1, parser.xr_header()->num_packets());
    769   EXPECT_EQ(kSenderSsrc, parser.xr_header()->Ssrc());
    770   EXPECT_EQ(1, parser.dlrr()->num_packets());
    771   EXPECT_EQ(2, parser.dlrr_items()->num_packets());
    772   EXPECT_EQ(0x11111111U, parser.dlrr_items()->Ssrc(0));
    773   EXPECT_EQ(0x22222222U, parser.dlrr_items()->LastRr(0));
    774   EXPECT_EQ(0x33333333U, parser.dlrr_items()->DelayLastRr(0));
    775   EXPECT_EQ(0x44444444U, parser.dlrr_items()->Ssrc(1));
    776   EXPECT_EQ(0x55555555U, parser.dlrr_items()->LastRr(1));
    777   EXPECT_EQ(0x66666666U, parser.dlrr_items()->DelayLastRr(1));
    778 }
    779 
    780 TEST(RtcpPacketTest, XrWithTwoDlrrBlocks) {
    781   Dlrr dlrr1;
    782   dlrr1.WithDlrrItem(0x11111111, 0x22222222, 0x33333333);
    783   Dlrr dlrr2;
    784   dlrr2.WithDlrrItem(0x44444444, 0x55555555, 0x66666666);
    785   Xr xr;
    786   xr.From(kSenderSsrc);
    787   xr.WithDlrr(&dlrr1);
    788   xr.WithDlrr(&dlrr2);
    789 
    790   RawPacket packet = xr.Build();
    791   RtcpPacketParser parser;
    792   parser.Parse(packet.buffer(), packet.buffer_length());
    793   EXPECT_EQ(1, parser.xr_header()->num_packets());
    794   EXPECT_EQ(kSenderSsrc, parser.xr_header()->Ssrc());
    795   EXPECT_EQ(2, parser.dlrr()->num_packets());
    796   EXPECT_EQ(2, parser.dlrr_items()->num_packets());
    797   EXPECT_EQ(0x11111111U, parser.dlrr_items()->Ssrc(0));
    798   EXPECT_EQ(0x22222222U, parser.dlrr_items()->LastRr(0));
    799   EXPECT_EQ(0x33333333U, parser.dlrr_items()->DelayLastRr(0));
    800   EXPECT_EQ(0x44444444U, parser.dlrr_items()->Ssrc(1));
    801   EXPECT_EQ(0x55555555U, parser.dlrr_items()->LastRr(1));
    802   EXPECT_EQ(0x66666666U, parser.dlrr_items()->DelayLastRr(1));
    803 }
    804 
    805 TEST(RtcpPacketTest, XrWithVoipMetric) {
    806   VoipMetric metric;
    807   metric.To(kRemoteSsrc);
    808   metric.LossRate(1);
    809   metric.DiscardRate(2);
    810   metric.BurstDensity(3);
    811   metric.GapDensity(4);
    812   metric.BurstDuration(0x1111);
    813   metric.GapDuration(0x2222);
    814   metric.RoundTripDelay(0x3333);
    815   metric.EndSystemDelay(0x4444);
    816   metric.SignalLevel(5);
    817   metric.NoiseLevel(6);
    818   metric.Rerl(7);
    819   metric.Gmin(8);
    820   metric.Rfactor(9);
    821   metric.ExtRfactor(10);
    822   metric.MosLq(11);
    823   metric.MosCq(12);
    824   metric.RxConfig(13);
    825   metric.JbNominal(0x5555);
    826   metric.JbMax(0x6666);
    827   metric.JbAbsMax(0x7777);
    828 
    829   Xr xr;
    830   xr.From(kSenderSsrc);
    831   xr.WithVoipMetric(&metric);
    832 
    833   RawPacket packet = xr.Build();
    834   RtcpPacketParser parser;
    835   parser.Parse(packet.buffer(), packet.buffer_length());
    836   EXPECT_EQ(1, parser.xr_header()->num_packets());
    837   EXPECT_EQ(kSenderSsrc, parser.xr_header()->Ssrc());
    838   EXPECT_EQ(1, parser.voip_metric()->num_packets());
    839   EXPECT_EQ(kRemoteSsrc, parser.voip_metric()->Ssrc());
    840   EXPECT_EQ(1, parser.voip_metric()->LossRate());
    841   EXPECT_EQ(2, parser.voip_metric()->DiscardRate());
    842   EXPECT_EQ(3, parser.voip_metric()->BurstDensity());
    843   EXPECT_EQ(4, parser.voip_metric()->GapDensity());
    844   EXPECT_EQ(0x1111, parser.voip_metric()->BurstDuration());
    845   EXPECT_EQ(0x2222, parser.voip_metric()->GapDuration());
    846   EXPECT_EQ(0x3333, parser.voip_metric()->RoundTripDelay());
    847   EXPECT_EQ(0x4444, parser.voip_metric()->EndSystemDelay());
    848   EXPECT_EQ(5, parser.voip_metric()->SignalLevel());
    849   EXPECT_EQ(6, parser.voip_metric()->NoiseLevel());
    850   EXPECT_EQ(7, parser.voip_metric()->Rerl());
    851   EXPECT_EQ(8, parser.voip_metric()->Gmin());
    852   EXPECT_EQ(9, parser.voip_metric()->Rfactor());
    853   EXPECT_EQ(10, parser.voip_metric()->ExtRfactor());
    854   EXPECT_EQ(11, parser.voip_metric()->MosLq());
    855   EXPECT_EQ(12, parser.voip_metric()->MosCq());
    856   EXPECT_EQ(13, parser.voip_metric()->RxConfig());
    857   EXPECT_EQ(0x5555, parser.voip_metric()->JbNominal());
    858   EXPECT_EQ(0x6666, parser.voip_metric()->JbMax());
    859   EXPECT_EQ(0x7777, parser.voip_metric()->JbAbsMax());
    860 }
    861 
    862 TEST(RtcpPacketTest, XrWithMultipleReportBlocks) {
    863   Rrtr rrtr;
    864   Dlrr dlrr;
    865   dlrr.WithDlrrItem(1, 2, 3);
    866   VoipMetric metric;
    867   Xr xr;
    868   xr.From(kSenderSsrc);
    869   xr.WithRrtr(&rrtr);
    870   xr.WithDlrr(&dlrr);
    871   xr.WithVoipMetric(&metric);
    872 
    873   RawPacket packet = xr.Build();
    874   RtcpPacketParser parser;
    875   parser.Parse(packet.buffer(), packet.buffer_length());
    876   EXPECT_EQ(1, parser.xr_header()->num_packets());
    877   EXPECT_EQ(kSenderSsrc, parser.xr_header()->Ssrc());
    878   EXPECT_EQ(1, parser.rrtr()->num_packets());
    879   EXPECT_EQ(1, parser.dlrr()->num_packets());
    880   EXPECT_EQ(1, parser.dlrr_items()->num_packets());
    881   EXPECT_EQ(1, parser.voip_metric()->num_packets());
    882 }
    883 
    884 TEST(RtcpPacketTest, DlrrWithoutItemNotIncludedInPacket) {
    885   Rrtr rrtr;
    886   Dlrr dlrr;
    887   VoipMetric metric;
    888   Xr xr;
    889   xr.From(kSenderSsrc);
    890   xr.WithRrtr(&rrtr);
    891   xr.WithDlrr(&dlrr);
    892   xr.WithVoipMetric(&metric);
    893 
    894   RawPacket packet = xr.Build();
    895   RtcpPacketParser parser;
    896   parser.Parse(packet.buffer(), packet.buffer_length());
    897   EXPECT_EQ(1, parser.xr_header()->num_packets());
    898   EXPECT_EQ(kSenderSsrc, parser.xr_header()->Ssrc());
    899   EXPECT_EQ(1, parser.rrtr()->num_packets());
    900   EXPECT_EQ(0, parser.dlrr()->num_packets());
    901   EXPECT_EQ(1, parser.voip_metric()->num_packets());
    902 }
    903 }  // namespace webrtc
    904