Home | History | Annotate | Download | only in source
      1 /*
      2  *  Copyright (c) 2012 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 
     11 
     12 /*
     13  * This file includes unit tests for the RTCPSender.
     14  */
     15 
     16 #include "testing/gmock/include/gmock/gmock.h"
     17 #include "testing/gtest/include/gtest/gtest.h"
     18 
     19 #include "webrtc/common_types.h"
     20 #include "webrtc/modules/rtp_rtcp/source/rtcp_sender.h"
     21 #include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.h"
     22 #include "webrtc/test/rtcp_packet_parser.h"
     23 
     24 using ::testing::ElementsAre;
     25 
     26 namespace webrtc {
     27 
     28 TEST(NACKStringBuilderTest, TestCase1) {
     29   NACKStringBuilder builder;
     30   builder.PushNACK(5);
     31   builder.PushNACK(7);
     32   builder.PushNACK(9);
     33   builder.PushNACK(10);
     34   builder.PushNACK(11);
     35   builder.PushNACK(12);
     36   builder.PushNACK(15);
     37   builder.PushNACK(18);
     38   builder.PushNACK(19);
     39   EXPECT_EQ(std::string("5,7,9-12,15,18-19"), builder.GetResult());
     40 }
     41 
     42 TEST(NACKStringBuilderTest, TestCase2) {
     43   NACKStringBuilder builder;
     44   builder.PushNACK(5);
     45   builder.PushNACK(6);
     46   builder.PushNACK(7);
     47   builder.PushNACK(9);
     48   builder.PushNACK(10);
     49   builder.PushNACK(11);
     50   builder.PushNACK(12);
     51   builder.PushNACK(15);
     52   builder.PushNACK(18);
     53   builder.PushNACK(19);
     54   EXPECT_EQ(std::string("5-7,9-12,15,18-19"), builder.GetResult());
     55 }
     56 
     57 TEST(NACKStringBuilderTest, TestCase3) {
     58   NACKStringBuilder builder;
     59   builder.PushNACK(5);
     60   builder.PushNACK(7);
     61   builder.PushNACK(9);
     62   builder.PushNACK(10);
     63   builder.PushNACK(11);
     64   builder.PushNACK(12);
     65   builder.PushNACK(15);
     66   builder.PushNACK(18);
     67   builder.PushNACK(19);
     68   builder.PushNACK(21);
     69   EXPECT_EQ(std::string("5,7,9-12,15,18-19,21"), builder.GetResult());
     70 }
     71 
     72 TEST(NACKStringBuilderTest, TestCase4) {
     73   NACKStringBuilder builder;
     74   builder.PushNACK(5);
     75   builder.PushNACK(7);
     76   builder.PushNACK(8);
     77   builder.PushNACK(9);
     78   builder.PushNACK(10);
     79   builder.PushNACK(11);
     80   builder.PushNACK(12);
     81   builder.PushNACK(15);
     82   builder.PushNACK(18);
     83   builder.PushNACK(19);
     84   EXPECT_EQ(std::string("5,7-12,15,18-19"), builder.GetResult());
     85 }
     86 
     87 TEST(NACKStringBuilderTest, TestCase5) {
     88   NACKStringBuilder builder;
     89   builder.PushNACK(5);
     90   builder.PushNACK(7);
     91   builder.PushNACK(9);
     92   builder.PushNACK(10);
     93   builder.PushNACK(11);
     94   builder.PushNACK(12);
     95   builder.PushNACK(15);
     96   builder.PushNACK(16);
     97   builder.PushNACK(18);
     98   builder.PushNACK(19);
     99   EXPECT_EQ(std::string("5,7,9-12,15-16,18-19"), builder.GetResult());
    100 }
    101 
    102 TEST(NACKStringBuilderTest, TestCase6) {
    103   NACKStringBuilder builder;
    104   builder.PushNACK(5);
    105   builder.PushNACK(7);
    106   builder.PushNACK(9);
    107   builder.PushNACK(10);
    108   builder.PushNACK(11);
    109   builder.PushNACK(12);
    110   builder.PushNACK(15);
    111   builder.PushNACK(16);
    112   builder.PushNACK(17);
    113   builder.PushNACK(18);
    114   builder.PushNACK(19);
    115   EXPECT_EQ(std::string("5,7,9-12,15-19"), builder.GetResult());
    116 }
    117 
    118 TEST(NACKStringBuilderTest, TestCase7) {
    119   NACKStringBuilder builder;
    120   builder.PushNACK(5);
    121   builder.PushNACK(6);
    122   builder.PushNACK(7);
    123   builder.PushNACK(8);
    124   builder.PushNACK(11);
    125   builder.PushNACK(12);
    126   builder.PushNACK(13);
    127   builder.PushNACK(14);
    128   builder.PushNACK(15);
    129   EXPECT_EQ(std::string("5-8,11-15"), builder.GetResult());
    130 }
    131 
    132 TEST(NACKStringBuilderTest, TestCase8) {
    133   NACKStringBuilder builder;
    134   builder.PushNACK(5);
    135   builder.PushNACK(7);
    136   builder.PushNACK(9);
    137   builder.PushNACK(11);
    138   builder.PushNACK(15);
    139   builder.PushNACK(17);
    140   builder.PushNACK(19);
    141   EXPECT_EQ(std::string("5,7,9,11,15,17,19"), builder.GetResult());
    142 }
    143 
    144 TEST(NACKStringBuilderTest, TestCase9) {
    145   NACKStringBuilder builder;
    146   builder.PushNACK(5);
    147   builder.PushNACK(6);
    148   builder.PushNACK(7);
    149   builder.PushNACK(8);
    150   builder.PushNACK(9);
    151   builder.PushNACK(10);
    152   builder.PushNACK(11);
    153   builder.PushNACK(12);
    154   EXPECT_EQ(std::string("5-12"), builder.GetResult());
    155 }
    156 
    157 TEST(NACKStringBuilderTest, TestCase10) {
    158   NACKStringBuilder builder;
    159   builder.PushNACK(5);
    160   EXPECT_EQ(std::string("5"), builder.GetResult());
    161 }
    162 
    163 TEST(NACKStringBuilderTest, TestCase11) {
    164   NACKStringBuilder builder;
    165   EXPECT_EQ(std::string(""), builder.GetResult());
    166 }
    167 
    168 TEST(NACKStringBuilderTest, TestCase12) {
    169   NACKStringBuilder builder;
    170   builder.PushNACK(5);
    171   builder.PushNACK(6);
    172   EXPECT_EQ(std::string("5-6"), builder.GetResult());
    173 }
    174 
    175 TEST(NACKStringBuilderTest, TestCase13) {
    176   NACKStringBuilder builder;
    177   builder.PushNACK(5);
    178   builder.PushNACK(6);
    179   builder.PushNACK(9);
    180   EXPECT_EQ(std::string("5-6,9"), builder.GetResult());
    181 }
    182 
    183 class RtcpPacketTypeCounterObserverImpl : public RtcpPacketTypeCounterObserver {
    184  public:
    185   RtcpPacketTypeCounterObserverImpl() : ssrc_(0) {}
    186   virtual ~RtcpPacketTypeCounterObserverImpl() {}
    187   void RtcpPacketTypesCounterUpdated(
    188       uint32_t ssrc,
    189       const RtcpPacketTypeCounter& packet_counter) override {
    190     ssrc_ = ssrc;
    191     counter_ = packet_counter;
    192   }
    193   uint32_t ssrc_;
    194   RtcpPacketTypeCounter counter_;
    195 };
    196 
    197 class TestTransport : public Transport,
    198                       public NullRtpData {
    199  public:
    200   TestTransport() {}
    201 
    202   bool SendRtp(const uint8_t* /*data*/,
    203                size_t /*len*/,
    204                const PacketOptions& options) override {
    205     return false;
    206   }
    207   bool SendRtcp(const uint8_t* data, size_t len) override {
    208     parser_.Parse(static_cast<const uint8_t*>(data), len);
    209     return true;
    210   }
    211   int OnReceivedPayloadData(const uint8_t* payload_data,
    212                             const size_t payload_size,
    213                             const WebRtcRTPHeader* rtp_header) override {
    214     return 0;
    215   }
    216   test::RtcpPacketParser parser_;
    217 };
    218 
    219 namespace {
    220 static const uint32_t kSenderSsrc = 0x11111111;
    221 static const uint32_t kRemoteSsrc = 0x22222222;
    222 }
    223 
    224 class RtcpSenderTest : public ::testing::Test {
    225  protected:
    226   RtcpSenderTest()
    227       : clock_(1335900000),
    228         receive_statistics_(ReceiveStatistics::Create(&clock_)) {
    229     RtpRtcp::Configuration configuration;
    230     configuration.audio = false;
    231     configuration.clock = &clock_;
    232     configuration.outgoing_transport = &test_transport_;
    233 
    234     rtp_rtcp_impl_.reset(new ModuleRtpRtcpImpl(configuration));
    235     rtcp_sender_.reset(new RTCPSender(false, &clock_, receive_statistics_.get(),
    236                                       nullptr, &test_transport_));
    237     rtcp_sender_->SetSSRC(kSenderSsrc);
    238     rtcp_sender_->SetRemoteSSRC(kRemoteSsrc);
    239   }
    240 
    241   void InsertIncomingPacket(uint32_t remote_ssrc, uint16_t seq_num) {
    242     RTPHeader header;
    243     header.ssrc = remote_ssrc;
    244     header.sequenceNumber = seq_num;
    245     header.timestamp = 12345;
    246     header.headerLength = 12;
    247     size_t kPacketLength = 100;
    248     receive_statistics_->IncomingPacket(header, kPacketLength, false);
    249   }
    250 
    251   test::RtcpPacketParser* parser() { return &test_transport_.parser_; }
    252 
    253   RTCPSender::FeedbackState feedback_state() {
    254     return rtp_rtcp_impl_->GetFeedbackState();
    255   }
    256 
    257   SimulatedClock clock_;
    258   TestTransport test_transport_;
    259   rtc::scoped_ptr<ReceiveStatistics> receive_statistics_;
    260   rtc::scoped_ptr<ModuleRtpRtcpImpl> rtp_rtcp_impl_;
    261   rtc::scoped_ptr<RTCPSender> rtcp_sender_;
    262 };
    263 
    264 TEST_F(RtcpSenderTest, SetRtcpStatus) {
    265   EXPECT_EQ(RtcpMode::kOff, rtcp_sender_->Status());
    266   rtcp_sender_->SetRTCPStatus(RtcpMode::kReducedSize);
    267   EXPECT_EQ(RtcpMode::kReducedSize, rtcp_sender_->Status());
    268 }
    269 
    270 TEST_F(RtcpSenderTest, SetSendingStatus) {
    271   EXPECT_FALSE(rtcp_sender_->Sending());
    272   EXPECT_EQ(0, rtcp_sender_->SetSendingStatus(feedback_state(), true));
    273   EXPECT_TRUE(rtcp_sender_->Sending());
    274 }
    275 
    276 TEST_F(RtcpSenderTest, NoPacketSentIfOff) {
    277   rtcp_sender_->SetRTCPStatus(RtcpMode::kOff);
    278   EXPECT_EQ(-1, rtcp_sender_->SendRTCP(feedback_state(), kRtcpSr));
    279 }
    280 
    281 TEST_F(RtcpSenderTest, SendSr) {
    282   const uint32_t kPacketCount = 0x12345;
    283   const uint32_t kOctetCount = 0x23456;
    284   rtcp_sender_->SetRTCPStatus(RtcpMode::kReducedSize);
    285   RTCPSender::FeedbackState feedback_state = rtp_rtcp_impl_->GetFeedbackState();
    286   feedback_state.packets_sent = kPacketCount;
    287   feedback_state.media_bytes_sent = kOctetCount;
    288   uint32_t ntp_secs;
    289   uint32_t ntp_frac;
    290   clock_.CurrentNtp(ntp_secs, ntp_frac);
    291   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state, kRtcpSr));
    292   EXPECT_EQ(1, parser()->sender_report()->num_packets());
    293   EXPECT_EQ(kSenderSsrc, parser()->sender_report()->Ssrc());
    294   EXPECT_EQ(ntp_secs, parser()->sender_report()->NtpSec());
    295   EXPECT_EQ(ntp_frac, parser()->sender_report()->NtpFrac());
    296   EXPECT_EQ(kPacketCount, parser()->sender_report()->PacketCount());
    297   EXPECT_EQ(kOctetCount, parser()->sender_report()->OctetCount());
    298   EXPECT_EQ(0, parser()->report_block()->num_packets());
    299 }
    300 
    301 TEST_F(RtcpSenderTest, SendRr) {
    302   rtcp_sender_->SetRTCPStatus(RtcpMode::kReducedSize);
    303   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpRr));
    304   EXPECT_EQ(1, parser()->receiver_report()->num_packets());
    305   EXPECT_EQ(kSenderSsrc, parser()->receiver_report()->Ssrc());
    306   EXPECT_EQ(0, parser()->report_block()->num_packets());
    307 }
    308 
    309 TEST_F(RtcpSenderTest, SendRrWithOneReportBlock) {
    310   const uint16_t kSeqNum = 11111;
    311   InsertIncomingPacket(kRemoteSsrc, kSeqNum);
    312   rtcp_sender_->SetRTCPStatus(RtcpMode::kCompound);
    313   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpRr));
    314   EXPECT_EQ(1, parser()->receiver_report()->num_packets());
    315   EXPECT_EQ(kSenderSsrc, parser()->receiver_report()->Ssrc());
    316   EXPECT_EQ(1, parser()->report_block()->num_packets());
    317   EXPECT_EQ(kRemoteSsrc, parser()->report_block()->Ssrc());
    318   EXPECT_EQ(0U, parser()->report_block()->FractionLost());
    319   EXPECT_EQ(0U, parser()->report_block()->CumPacketLost());
    320   EXPECT_EQ(kSeqNum, parser()->report_block()->ExtHighestSeqNum());
    321 }
    322 
    323 TEST_F(RtcpSenderTest, SendRrWithTwoReportBlocks) {
    324   const uint16_t kSeqNum = 11111;
    325   InsertIncomingPacket(kRemoteSsrc, kSeqNum);
    326   InsertIncomingPacket(kRemoteSsrc + 1, kSeqNum + 1);
    327   rtcp_sender_->SetRTCPStatus(RtcpMode::kCompound);
    328   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpRr));
    329   EXPECT_EQ(1, parser()->receiver_report()->num_packets());
    330   EXPECT_EQ(kSenderSsrc, parser()->receiver_report()->Ssrc());
    331   EXPECT_EQ(2, parser()->report_block()->num_packets());
    332   EXPECT_EQ(1, parser()->report_blocks_per_ssrc(kRemoteSsrc));
    333   EXPECT_EQ(1, parser()->report_blocks_per_ssrc(kRemoteSsrc + 1));
    334 }
    335 
    336 TEST_F(RtcpSenderTest, SendSdes) {
    337   rtcp_sender_->SetRTCPStatus(RtcpMode::kReducedSize);
    338   EXPECT_EQ(0, rtcp_sender_->SetCNAME("alice@host"));
    339   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpSdes));
    340   EXPECT_EQ(1, parser()->sdes()->num_packets());
    341   EXPECT_EQ(1, parser()->sdes_chunk()->num_packets());
    342   EXPECT_EQ(kSenderSsrc, parser()->sdes_chunk()->Ssrc());
    343   EXPECT_EQ("alice@host", parser()->sdes_chunk()->Cname());
    344 }
    345 
    346 TEST_F(RtcpSenderTest, SdesIncludedInCompoundPacket) {
    347   rtcp_sender_->SetRTCPStatus(RtcpMode::kCompound);
    348   EXPECT_EQ(0, rtcp_sender_->SetCNAME("alice@host"));
    349   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpReport));
    350   EXPECT_EQ(1, parser()->receiver_report()->num_packets());
    351   EXPECT_EQ(1, parser()->sdes()->num_packets());
    352   EXPECT_EQ(1, parser()->sdes_chunk()->num_packets());
    353 }
    354 
    355 TEST_F(RtcpSenderTest, SendBye) {
    356   rtcp_sender_->SetRTCPStatus(RtcpMode::kReducedSize);
    357   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpBye));
    358   EXPECT_EQ(1, parser()->bye()->num_packets());
    359   EXPECT_EQ(kSenderSsrc, parser()->bye()->Ssrc());
    360 }
    361 
    362 TEST_F(RtcpSenderTest, StopSendingTriggersBye) {
    363   rtcp_sender_->SetRTCPStatus(RtcpMode::kReducedSize);
    364   EXPECT_EQ(0, rtcp_sender_->SetSendingStatus(feedback_state(), true));
    365   EXPECT_EQ(0, rtcp_sender_->SetSendingStatus(feedback_state(), false));
    366   EXPECT_EQ(1, parser()->bye()->num_packets());
    367   EXPECT_EQ(kSenderSsrc, parser()->bye()->Ssrc());
    368 }
    369 
    370 TEST_F(RtcpSenderTest, SendApp) {
    371   const uint8_t kSubType = 30;
    372   uint32_t name = 'n' << 24;
    373   name += 'a' << 16;
    374   name += 'm' << 8;
    375   name += 'e';
    376   const uint8_t kData[] = {'t', 'e', 's', 't', 'd', 'a', 't', 'a'};
    377   const uint16_t kDataLength = sizeof(kData) / sizeof(kData[0]);
    378   EXPECT_EQ(0, rtcp_sender_->SetApplicationSpecificData(kSubType, name, kData,
    379                                                         kDataLength));
    380   rtcp_sender_->SetRTCPStatus(RtcpMode::kReducedSize);
    381   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpApp));
    382   EXPECT_EQ(1, parser()->app()->num_packets());
    383   EXPECT_EQ(kSubType, parser()->app()->SubType());
    384   EXPECT_EQ(name, parser()->app()->Name());
    385   EXPECT_EQ(1, parser()->app_item()->num_packets());
    386   EXPECT_EQ(kDataLength, parser()->app_item()->DataLength());
    387   EXPECT_EQ(0, strncmp(reinterpret_cast<const char*>(kData),
    388       reinterpret_cast<const char*>(parser()->app_item()->Data()),
    389       parser()->app_item()->DataLength()));
    390 }
    391 
    392 TEST_F(RtcpSenderTest, SendEmptyApp) {
    393   const uint8_t kSubType = 30;
    394   const uint32_t kName = 0x6E616D65;
    395 
    396   EXPECT_EQ(
    397       0, rtcp_sender_->SetApplicationSpecificData(kSubType, kName, nullptr, 0));
    398 
    399   rtcp_sender_->SetRTCPStatus(RtcpMode::kReducedSize);
    400   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpApp));
    401   EXPECT_EQ(1, parser()->app()->num_packets());
    402   EXPECT_EQ(kSubType, parser()->app()->SubType());
    403   EXPECT_EQ(kName, parser()->app()->Name());
    404   EXPECT_EQ(0, parser()->app_item()->num_packets());
    405 }
    406 
    407 TEST_F(RtcpSenderTest, SetInvalidApplicationSpecificData) {
    408   const uint8_t kData[] = {'t', 'e', 's', 't', 'd', 'a', 't'};
    409   const uint16_t kInvalidDataLength = sizeof(kData) / sizeof(kData[0]);
    410   EXPECT_EQ(-1, rtcp_sender_->SetApplicationSpecificData(
    411       0, 0, kData, kInvalidDataLength));  // Should by multiple of 4.
    412 }
    413 
    414 TEST_F(RtcpSenderTest, SendFirNonRepeat) {
    415   rtcp_sender_->SetRTCPStatus(RtcpMode::kReducedSize);
    416   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpFir));
    417   EXPECT_EQ(1, parser()->fir()->num_packets());
    418   EXPECT_EQ(kSenderSsrc, parser()->fir()->Ssrc());
    419   EXPECT_EQ(1, parser()->fir_item()->num_packets());
    420   EXPECT_EQ(kRemoteSsrc, parser()->fir_item()->Ssrc());
    421   uint8_t seq = parser()->fir_item()->SeqNum();
    422   // Sends non-repeat FIR as default.
    423   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpFir));
    424   EXPECT_EQ(2, parser()->fir()->num_packets());
    425   EXPECT_EQ(2, parser()->fir_item()->num_packets());
    426   EXPECT_EQ(seq + 1, parser()->fir_item()->SeqNum());
    427 }
    428 
    429 TEST_F(RtcpSenderTest, SendFirRepeat) {
    430   rtcp_sender_->SetRTCPStatus(RtcpMode::kReducedSize);
    431   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpFir));
    432   EXPECT_EQ(1, parser()->fir()->num_packets());
    433   EXPECT_EQ(1, parser()->fir_item()->num_packets());
    434   uint8_t seq = parser()->fir_item()->SeqNum();
    435   const bool kRepeat = true;
    436   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpFir, 0, nullptr,
    437                                       kRepeat));
    438   EXPECT_EQ(2, parser()->fir()->num_packets());
    439   EXPECT_EQ(2, parser()->fir_item()->num_packets());
    440   EXPECT_EQ(seq, parser()->fir_item()->SeqNum());
    441 }
    442 
    443 TEST_F(RtcpSenderTest, SendPli) {
    444   rtcp_sender_->SetRTCPStatus(RtcpMode::kReducedSize);
    445   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpPli));
    446   EXPECT_EQ(1, parser()->pli()->num_packets());
    447   EXPECT_EQ(kSenderSsrc, parser()->pli()->Ssrc());
    448   EXPECT_EQ(kRemoteSsrc, parser()->pli()->MediaSsrc());
    449 }
    450 
    451 TEST_F(RtcpSenderTest, SendRpsi) {
    452   const uint64_t kPictureId = 0x41;
    453   const int8_t kPayloadType = 100;
    454   rtcp_sender_->SetRTCPStatus(RtcpMode::kReducedSize);
    455   RTCPSender::FeedbackState feedback_state = rtp_rtcp_impl_->GetFeedbackState();
    456   feedback_state.send_payload_type = kPayloadType;
    457   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state, kRtcpRpsi, 0, nullptr,
    458                                       false, kPictureId));
    459   EXPECT_EQ(kPayloadType, parser()->rpsi()->PayloadType());
    460   EXPECT_EQ(kPictureId, parser()->rpsi()->PictureId());
    461 }
    462 
    463 TEST_F(RtcpSenderTest, SendSli) {
    464   const uint16_t kFirstMb = 0;
    465   const uint16_t kNumberOfMb = 0x1FFF;
    466   const uint8_t kPictureId = 60;
    467   rtcp_sender_->SetRTCPStatus(RtcpMode::kReducedSize);
    468   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpSli, 0, nullptr,
    469                                       false, kPictureId));
    470   EXPECT_EQ(1, parser()->sli()->num_packets());
    471   EXPECT_EQ(kSenderSsrc, parser()->sli()->Ssrc());
    472   EXPECT_EQ(kRemoteSsrc, parser()->sli()->MediaSsrc());
    473   EXPECT_EQ(1, parser()->sli_item()->num_packets());
    474   EXPECT_EQ(kFirstMb, parser()->sli_item()->FirstMb());
    475   EXPECT_EQ(kNumberOfMb, parser()->sli_item()->NumberOfMb());
    476   EXPECT_EQ(kPictureId, parser()->sli_item()->PictureId());
    477 }
    478 
    479 TEST_F(RtcpSenderTest, SendNack) {
    480   rtcp_sender_->SetRTCPStatus(RtcpMode::kReducedSize);
    481   const uint16_t kList[] = {0, 1, 16};
    482   const int32_t kListLength = sizeof(kList) / sizeof(kList[0]);
    483   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpNack, kListLength,
    484                                       kList));
    485   EXPECT_EQ(1, parser()->nack()->num_packets());
    486   EXPECT_EQ(kSenderSsrc, parser()->nack()->Ssrc());
    487   EXPECT_EQ(kRemoteSsrc, parser()->nack()->MediaSsrc());
    488   EXPECT_EQ(1, parser()->nack_item()->num_packets());
    489   EXPECT_THAT(parser()->nack_item()->last_nack_list(), ElementsAre(0, 1, 16));
    490 }
    491 
    492 TEST_F(RtcpSenderTest, SendRemb) {
    493   const int kBitrate = 261011;
    494   std::vector<uint32_t> ssrcs;
    495   ssrcs.push_back(kRemoteSsrc);
    496   ssrcs.push_back(kRemoteSsrc + 1);
    497   rtcp_sender_->SetRTCPStatus(RtcpMode::kReducedSize);
    498   rtcp_sender_->SetREMBData(kBitrate, ssrcs);
    499   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpRemb));
    500   EXPECT_EQ(1, parser()->psfb_app()->num_packets());
    501   EXPECT_EQ(kSenderSsrc, parser()->psfb_app()->Ssrc());
    502   EXPECT_EQ(1, parser()->remb_item()->num_packets());
    503   EXPECT_EQ(kBitrate, parser()->remb_item()->last_bitrate_bps());
    504   EXPECT_THAT(parser()->remb_item()->last_ssrc_list(),
    505               ElementsAre(kRemoteSsrc, kRemoteSsrc + 1));
    506 }
    507 
    508 TEST_F(RtcpSenderTest, RembIncludedInCompoundPacketIfEnabled) {
    509   const int kBitrate = 261011;
    510   std::vector<uint32_t> ssrcs;
    511   ssrcs.push_back(kRemoteSsrc);
    512   rtcp_sender_->SetRTCPStatus(RtcpMode::kCompound);
    513   rtcp_sender_->SetREMBStatus(true);
    514   EXPECT_TRUE(rtcp_sender_->REMB());
    515   rtcp_sender_->SetREMBData(kBitrate, ssrcs);
    516   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpReport));
    517   EXPECT_EQ(1, parser()->psfb_app()->num_packets());
    518   EXPECT_EQ(1, parser()->remb_item()->num_packets());
    519   // REMB should be included in each compound packet.
    520   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpReport));
    521   EXPECT_EQ(2, parser()->psfb_app()->num_packets());
    522   EXPECT_EQ(2, parser()->remb_item()->num_packets());
    523 }
    524 
    525 TEST_F(RtcpSenderTest, RembNotIncludedInCompoundPacketIfNotEnabled) {
    526   const int kBitrate = 261011;
    527   std::vector<uint32_t> ssrcs;
    528   ssrcs.push_back(kRemoteSsrc);
    529   rtcp_sender_->SetRTCPStatus(RtcpMode::kCompound);
    530   rtcp_sender_->SetREMBData(kBitrate, ssrcs);
    531   EXPECT_FALSE(rtcp_sender_->REMB());
    532   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpReport));
    533   EXPECT_EQ(0, parser()->psfb_app()->num_packets());
    534 }
    535 
    536 TEST_F(RtcpSenderTest, SendXrWithVoipMetric) {
    537   rtcp_sender_->SetRTCPStatus(RtcpMode::kReducedSize);
    538   RTCPVoIPMetric metric;
    539   metric.lossRate = 1;
    540   metric.discardRate = 2;
    541   metric.burstDensity = 3;
    542   metric.gapDensity = 4;
    543   metric.burstDuration = 0x1111;
    544   metric.gapDuration = 0x2222;
    545   metric.roundTripDelay = 0x3333;
    546   metric.endSystemDelay = 0x4444;
    547   metric.signalLevel = 5;
    548   metric.noiseLevel = 6;
    549   metric.RERL = 7;
    550   metric.Gmin = 8;
    551   metric.Rfactor = 9;
    552   metric.extRfactor = 10;
    553   metric.MOSLQ = 11;
    554   metric.MOSCQ = 12;
    555   metric.RXconfig = 13;
    556   metric.JBnominal = 0x5555;
    557   metric.JBmax = 0x6666;
    558   metric.JBabsMax = 0x7777;
    559   EXPECT_EQ(0, rtcp_sender_->SetRTCPVoIPMetrics(&metric));
    560   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpXrVoipMetric));
    561   EXPECT_EQ(1, parser()->xr_header()->num_packets());
    562   EXPECT_EQ(kSenderSsrc, parser()->xr_header()->Ssrc());
    563   EXPECT_EQ(1, parser()->voip_metric()->num_packets());
    564   EXPECT_EQ(kRemoteSsrc, parser()->voip_metric()->Ssrc());
    565   EXPECT_EQ(metric.lossRate, parser()->voip_metric()->LossRate());
    566   EXPECT_EQ(metric.discardRate, parser()->voip_metric()->DiscardRate());
    567   EXPECT_EQ(metric.burstDensity, parser()->voip_metric()->BurstDensity());
    568   EXPECT_EQ(metric.gapDensity, parser()->voip_metric()->GapDensity());
    569   EXPECT_EQ(metric.burstDuration, parser()->voip_metric()->BurstDuration());
    570   EXPECT_EQ(metric.gapDuration, parser()->voip_metric()->GapDuration());
    571   EXPECT_EQ(metric.roundTripDelay, parser()->voip_metric()->RoundTripDelay());
    572   EXPECT_EQ(metric.endSystemDelay, parser()->voip_metric()->EndSystemDelay());
    573   EXPECT_EQ(metric.signalLevel, parser()->voip_metric()->SignalLevel());
    574   EXPECT_EQ(metric.noiseLevel, parser()->voip_metric()->NoiseLevel());
    575   EXPECT_EQ(metric.RERL, parser()->voip_metric()->Rerl());
    576   EXPECT_EQ(metric.Gmin, parser()->voip_metric()->Gmin());
    577   EXPECT_EQ(metric.Rfactor, parser()->voip_metric()->Rfactor());
    578   EXPECT_EQ(metric.extRfactor, parser()->voip_metric()->ExtRfactor());
    579   EXPECT_EQ(metric.MOSLQ, parser()->voip_metric()->MosLq());
    580   EXPECT_EQ(metric.MOSCQ, parser()->voip_metric()->MosCq());
    581   EXPECT_EQ(metric.RXconfig, parser()->voip_metric()->RxConfig());
    582   EXPECT_EQ(metric.JBnominal, parser()->voip_metric()->JbNominal());
    583   EXPECT_EQ(metric.JBmax, parser()->voip_metric()->JbMax());
    584   EXPECT_EQ(metric.JBabsMax, parser()->voip_metric()->JbAbsMax());
    585 }
    586 
    587 TEST_F(RtcpSenderTest, SendXrWithDlrr) {
    588   rtcp_sender_->SetRTCPStatus(RtcpMode::kCompound);
    589   RTCPSender::FeedbackState feedback_state = rtp_rtcp_impl_->GetFeedbackState();
    590   feedback_state.has_last_xr_rr = true;
    591   RtcpReceiveTimeInfo last_xr_rr;
    592   last_xr_rr.sourceSSRC = 0x11111111;
    593   last_xr_rr.lastRR = 0x22222222;
    594   last_xr_rr.delaySinceLastRR = 0x33333333;
    595   feedback_state.last_xr_rr = last_xr_rr;
    596   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state, kRtcpReport));
    597   EXPECT_EQ(1, parser()->xr_header()->num_packets());
    598   EXPECT_EQ(kSenderSsrc, parser()->xr_header()->Ssrc());
    599   EXPECT_EQ(1, parser()->dlrr()->num_packets());
    600   EXPECT_EQ(1, parser()->dlrr_items()->num_packets());
    601   EXPECT_EQ(last_xr_rr.sourceSSRC, parser()->dlrr_items()->Ssrc(0));
    602   EXPECT_EQ(last_xr_rr.lastRR, parser()->dlrr_items()->LastRr(0));
    603   EXPECT_EQ(last_xr_rr.delaySinceLastRR,
    604             parser()->dlrr_items()->DelayLastRr(0));
    605 }
    606 
    607 TEST_F(RtcpSenderTest, SendXrWithRrtr) {
    608   rtcp_sender_->SetRTCPStatus(RtcpMode::kCompound);
    609   EXPECT_EQ(0, rtcp_sender_->SetSendingStatus(feedback_state(), false));
    610   rtcp_sender_->SendRtcpXrReceiverReferenceTime(true);
    611   uint32_t ntp_secs;
    612   uint32_t ntp_frac;
    613   clock_.CurrentNtp(ntp_secs, ntp_frac);
    614   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpReport));
    615   EXPECT_EQ(1, parser()->xr_header()->num_packets());
    616   EXPECT_EQ(kSenderSsrc, parser()->xr_header()->Ssrc());
    617   EXPECT_EQ(0, parser()->dlrr()->num_packets());
    618   EXPECT_EQ(1, parser()->rrtr()->num_packets());
    619   EXPECT_EQ(ntp_secs, parser()->rrtr()->NtpSec());
    620   EXPECT_EQ(ntp_frac, parser()->rrtr()->NtpFrac());
    621 }
    622 
    623 TEST_F(RtcpSenderTest, TestNoXrRrtrSentIfSending) {
    624   rtcp_sender_->SetRTCPStatus(RtcpMode::kCompound);
    625   EXPECT_EQ(0, rtcp_sender_->SetSendingStatus(feedback_state(), true));
    626   rtcp_sender_->SendRtcpXrReceiverReferenceTime(true);
    627   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpReport));
    628   EXPECT_EQ(0, parser()->xr_header()->num_packets());
    629   EXPECT_EQ(0, parser()->rrtr()->num_packets());
    630 }
    631 
    632 TEST_F(RtcpSenderTest, TestNoXrRrtrSentIfNotEnabled) {
    633   rtcp_sender_->SetRTCPStatus(RtcpMode::kCompound);
    634   EXPECT_EQ(0, rtcp_sender_->SetSendingStatus(feedback_state(), false));
    635   rtcp_sender_->SendRtcpXrReceiverReferenceTime(false);
    636   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpReport));
    637   EXPECT_EQ(0, parser()->xr_header()->num_packets());
    638   EXPECT_EQ(0, parser()->rrtr()->num_packets());
    639 }
    640 
    641 TEST_F(RtcpSenderTest, TestSendTimeOfXrRrtr) {
    642   rtcp_sender_->SetRTCPStatus(RtcpMode::kCompound);
    643   RTCPSender::FeedbackState feedback_state = rtp_rtcp_impl_->GetFeedbackState();
    644   EXPECT_EQ(0, rtcp_sender_->SetSendingStatus(feedback_state, false));
    645   rtcp_sender_->SendRtcpXrReceiverReferenceTime(true);
    646   uint32_t ntp_sec;
    647   uint32_t ntp_frac;
    648   clock_.CurrentNtp(ntp_sec, ntp_frac);
    649   uint32_t initial_mid_ntp = RTCPUtility::MidNtp(ntp_sec, ntp_frac);
    650 
    651   // No packet sent.
    652   int64_t time_ms;
    653   EXPECT_FALSE(rtcp_sender_->SendTimeOfXrRrReport(initial_mid_ntp, &time_ms));
    654 
    655   // Send XR RR packets.
    656   for (int i = 0; i <= RTCP_NUMBER_OF_SR; ++i) {
    657     EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state, kRtcpReport));
    658     EXPECT_EQ(i + 1, test_transport_.parser_.rrtr()->num_packets());
    659     clock_.CurrentNtp(ntp_sec, ntp_frac);
    660     uint32_t mid_ntp = RTCPUtility::MidNtp(ntp_sec, ntp_frac);
    661     EXPECT_TRUE(rtcp_sender_->SendTimeOfXrRrReport(mid_ntp, &time_ms));
    662     EXPECT_EQ(clock_.CurrentNtpInMilliseconds(), time_ms);
    663     clock_.AdvanceTimeMilliseconds(1000);
    664   }
    665   // The first report should no longer be stored.
    666   EXPECT_FALSE(rtcp_sender_->SendTimeOfXrRrReport(initial_mid_ntp, &time_ms));
    667 }
    668 
    669 TEST_F(RtcpSenderTest, TestRegisterRtcpPacketTypeObserver) {
    670   RtcpPacketTypeCounterObserverImpl observer;
    671   rtcp_sender_.reset(new RTCPSender(false, &clock_, receive_statistics_.get(),
    672                                     &observer, &test_transport_));
    673   rtcp_sender_->SetRemoteSSRC(kRemoteSsrc);
    674   rtcp_sender_->SetRTCPStatus(RtcpMode::kReducedSize);
    675   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpPli));
    676   EXPECT_EQ(1, parser()->pli()->num_packets());
    677   EXPECT_EQ(kRemoteSsrc, observer.ssrc_);
    678   EXPECT_EQ(1U, observer.counter_.pli_packets);
    679   EXPECT_EQ(clock_.TimeInMilliseconds(),
    680             observer.counter_.first_packet_time_ms);
    681 }
    682 
    683 TEST_F(RtcpSenderTest, SendTmmbr) {
    684   const unsigned int kBitrateBps = 312000;
    685   rtcp_sender_->SetRTCPStatus(RtcpMode::kReducedSize);
    686   rtcp_sender_->SetTargetBitrate(kBitrateBps);
    687   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpTmmbr));
    688   EXPECT_EQ(1, parser()->tmmbr()->num_packets());
    689   EXPECT_EQ(kSenderSsrc, parser()->tmmbr()->Ssrc());
    690   EXPECT_EQ(1, parser()->tmmbr_item()->num_packets());
    691   EXPECT_EQ(kBitrateBps / 1000, parser()->tmmbr_item()->BitrateKbps());
    692   // TODO(asapersson): tmmbr_item()->Overhead() looks broken, always zero.
    693 }
    694 
    695 TEST_F(RtcpSenderTest, TmmbrIncludedInCompoundPacketIfEnabled) {
    696   const unsigned int kBitrateBps = 312000;
    697   rtcp_sender_->SetRTCPStatus(RtcpMode::kCompound);
    698   EXPECT_FALSE(rtcp_sender_->TMMBR());
    699   rtcp_sender_->SetTMMBRStatus(true);
    700   EXPECT_TRUE(rtcp_sender_->TMMBR());
    701   rtcp_sender_->SetTargetBitrate(kBitrateBps);
    702   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpReport));
    703   EXPECT_EQ(1, parser()->tmmbr()->num_packets());
    704   EXPECT_EQ(1, parser()->tmmbr_item()->num_packets());
    705   // TMMBR should be included in each compound packet.
    706   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpReport));
    707   EXPECT_EQ(2, parser()->tmmbr()->num_packets());
    708   EXPECT_EQ(2, parser()->tmmbr_item()->num_packets());
    709 
    710   rtcp_sender_->SetTMMBRStatus(false);
    711   EXPECT_FALSE(rtcp_sender_->TMMBR());
    712 }
    713 
    714 TEST_F(RtcpSenderTest, SendTmmbn) {
    715   rtcp_sender_->SetRTCPStatus(RtcpMode::kCompound);
    716   TMMBRSet bounding_set;
    717   bounding_set.VerifyAndAllocateSet(1);
    718   const uint32_t kBitrateKbps = 32768;
    719   const uint32_t kPacketOh = 40;
    720   const uint32_t kSourceSsrc = 12345;
    721   bounding_set.AddEntry(kBitrateKbps, kPacketOh, kSourceSsrc);
    722   EXPECT_EQ(0, rtcp_sender_->SetTMMBN(&bounding_set, 0));
    723   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpSr));
    724   EXPECT_EQ(1, parser()->sender_report()->num_packets());
    725   EXPECT_EQ(1, parser()->tmmbn()->num_packets());
    726   EXPECT_EQ(kSenderSsrc, parser()->tmmbn()->Ssrc());
    727   EXPECT_EQ(1, parser()->tmmbn_items()->num_packets());
    728   EXPECT_EQ(kBitrateKbps, parser()->tmmbn_items()->BitrateKbps(0));
    729   EXPECT_EQ(kPacketOh, parser()->tmmbn_items()->Overhead(0));
    730   EXPECT_EQ(kSourceSsrc, parser()->tmmbn_items()->Ssrc(0));
    731 }
    732 
    733 // This test is written to verify actual behaviour. It does not seem
    734 // to make much sense to send an empty TMMBN, since there is no place
    735 // to put an actual limit here. It's just information that no limit
    736 // is set, which is kind of the starting assumption.
    737 // See http://code.google.com/p/webrtc/issues/detail?id=468 for one
    738 // situation where this caused confusion.
    739 TEST_F(RtcpSenderTest, SendsTmmbnIfSetAndEmpty) {
    740   rtcp_sender_->SetRTCPStatus(RtcpMode::kCompound);
    741   TMMBRSet bounding_set;
    742   EXPECT_EQ(0, rtcp_sender_->SetTMMBN(&bounding_set, 3));
    743   EXPECT_EQ(0, rtcp_sender_->SendRTCP(feedback_state(), kRtcpSr));
    744   EXPECT_EQ(1, parser()->sender_report()->num_packets());
    745   EXPECT_EQ(1, parser()->tmmbn()->num_packets());
    746   EXPECT_EQ(kSenderSsrc, parser()->tmmbn()->Ssrc());
    747   EXPECT_EQ(0, parser()->tmmbn_items()->num_packets());
    748 }
    749 
    750 TEST_F(RtcpSenderTest, SendCompoundPliRemb) {
    751   const int kBitrate = 261011;
    752   std::vector<uint32_t> ssrcs;
    753   ssrcs.push_back(kRemoteSsrc);
    754   rtcp_sender_->SetRTCPStatus(RtcpMode::kCompound);
    755   rtcp_sender_->SetREMBData(kBitrate, ssrcs);
    756   std::set<RTCPPacketType> packet_types;
    757   packet_types.insert(kRtcpRemb);
    758   packet_types.insert(kRtcpPli);
    759   EXPECT_EQ(0, rtcp_sender_->SendCompoundRTCP(feedback_state(), packet_types));
    760   EXPECT_EQ(1, parser()->remb_item()->num_packets());
    761   EXPECT_EQ(1, parser()->pli()->num_packets());
    762 }
    763 
    764 }  // namespace webrtc
    765