Home | History | Annotate | Download | only in testAPI
      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 #include <algorithm>
     12 #include <vector>
     13 
     14 #include "testing/gmock/include/gmock/gmock.h"
     15 #include "testing/gtest/include/gtest/gtest.h"
     16 #include "webrtc/common_types.h"
     17 #include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h"
     18 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
     19 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
     20 #include "webrtc/modules/rtp_rtcp/source/rtp_receiver_audio.h"
     21 #include "webrtc/modules/rtp_rtcp/test/testAPI/test_api.h"
     22 
     23 using namespace webrtc;
     24 
     25 const uint64_t kTestPictureId = 12345678;
     26 
     27 class RtcpCallback : public RtcpFeedback, public RtcpIntraFrameObserver {
     28  public:
     29   void SetModule(RtpRtcp* module) {
     30     _rtpRtcpModule = module;
     31   };
     32   virtual void OnRTCPPacketTimeout(const int32_t id) {
     33   }
     34   virtual void OnLipSyncUpdate(const int32_t id,
     35                                const int32_t audioVideoOffset) {
     36   };
     37   virtual void OnXRVoIPMetricReceived(
     38       const int32_t id,
     39       const RTCPVoIPMetric* metric) {
     40   };
     41   virtual void OnApplicationDataReceived(const int32_t id,
     42                                          const uint8_t subType,
     43                                          const uint32_t name,
     44                                          const uint16_t length,
     45                                          const uint8_t* data) {
     46     char print_name[5];
     47     print_name[0] = static_cast<char>(name >> 24);
     48     print_name[1] = static_cast<char>(name >> 16);
     49     print_name[2] = static_cast<char>(name >> 8);
     50     print_name[3] = static_cast<char>(name);
     51     print_name[4] = 0;
     52 
     53     EXPECT_STRCASEEQ("test", print_name);
     54   };
     55   virtual void OnReceiveReportReceived(const int32_t id,
     56                                        const uint32_t senderSSRC) {
     57   };
     58   virtual void OnReceivedIntraFrameRequest(uint32_t ssrc) {
     59   };
     60   virtual void OnReceivedSLI(uint32_t ssrc,
     61                              uint8_t pictureId) {
     62     EXPECT_EQ(28, pictureId);
     63   };
     64   virtual void OnReceivedRPSI(uint32_t ssrc,
     65                               uint64_t pictureId) {
     66     EXPECT_EQ(kTestPictureId, pictureId);
     67   };
     68   virtual void OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc) {};
     69  private:
     70   RtpRtcp* _rtpRtcpModule;
     71 };
     72 
     73 class TestRtpFeedback : public NullRtpFeedback {
     74  public:
     75   TestRtpFeedback(RtpRtcp* rtp_rtcp) : rtp_rtcp_(rtp_rtcp) {}
     76   virtual ~TestRtpFeedback() {}
     77 
     78   virtual void OnIncomingSSRCChanged(const int32_t id,
     79                                      const uint32_t ssrc) {
     80     rtp_rtcp_->SetRemoteSSRC(ssrc);
     81   }
     82 
     83  private:
     84   RtpRtcp* rtp_rtcp_;
     85 };
     86 
     87 class RtpRtcpRtcpTest : public ::testing::Test {
     88  protected:
     89   RtpRtcpRtcpTest() : fake_clock(123456) {
     90     test_CSRC[0] = 1234;
     91     test_CSRC[1] = 2345;
     92     test_id = 123;
     93     test_ssrc = 3456;
     94     test_timestamp = 4567;
     95     test_sequence_number = 2345;
     96   }
     97   ~RtpRtcpRtcpTest() {}
     98 
     99   virtual void SetUp() {
    100     receiver = new TestRtpReceiver();
    101     transport1 = new LoopBackTransport();
    102     transport2 = new LoopBackTransport();
    103     myRTCPFeedback1 = new RtcpCallback();
    104     myRTCPFeedback2 = new RtcpCallback();
    105 
    106     receive_statistics1_.reset(ReceiveStatistics::Create(&fake_clock));
    107     receive_statistics2_.reset(ReceiveStatistics::Create(&fake_clock));
    108 
    109     RtpRtcp::Configuration configuration;
    110     configuration.id = test_id;
    111     configuration.audio = true;
    112     configuration.clock = &fake_clock;
    113     configuration.receive_statistics = receive_statistics1_.get();
    114     configuration.outgoing_transport = transport1;
    115     configuration.rtcp_feedback = myRTCPFeedback1;
    116     configuration.intra_frame_callback = myRTCPFeedback1;
    117 
    118     rtp_payload_registry1_.reset(new RTPPayloadRegistry(
    119             RTPPayloadStrategy::CreateStrategy(true)));
    120     rtp_payload_registry2_.reset(new RTPPayloadRegistry(
    121             RTPPayloadStrategy::CreateStrategy(true)));
    122 
    123     module1 = RtpRtcp::CreateRtpRtcp(configuration);
    124 
    125     rtp_feedback1_.reset(new TestRtpFeedback(module1));
    126 
    127     rtp_receiver1_.reset(RtpReceiver::CreateAudioReceiver(
    128         test_id, &fake_clock, NULL, receiver, rtp_feedback1_.get(),
    129         rtp_payload_registry1_.get()));
    130 
    131     configuration.receive_statistics = receive_statistics2_.get();
    132     configuration.id = test_id + 1;
    133     configuration.outgoing_transport = transport2;
    134     configuration.rtcp_feedback = myRTCPFeedback2;
    135     configuration.intra_frame_callback = myRTCPFeedback2;
    136 
    137     module2 = RtpRtcp::CreateRtpRtcp(configuration);
    138 
    139     rtp_feedback2_.reset(new TestRtpFeedback(module2));
    140 
    141     rtp_receiver2_.reset(RtpReceiver::CreateAudioReceiver(
    142         test_id + 1, &fake_clock, NULL, receiver, rtp_feedback2_.get(),
    143         rtp_payload_registry2_.get()));
    144 
    145     transport1->SetSendModule(module2, rtp_payload_registry2_.get(),
    146                               rtp_receiver2_.get(), receive_statistics2_.get());
    147     transport2->SetSendModule(module1, rtp_payload_registry1_.get(),
    148                               rtp_receiver1_.get(), receive_statistics1_.get());
    149     myRTCPFeedback1->SetModule(module1);
    150     myRTCPFeedback2->SetModule(module2);
    151 
    152     EXPECT_EQ(0, module1->SetRTCPStatus(kRtcpCompound));
    153     EXPECT_EQ(0, module2->SetRTCPStatus(kRtcpCompound));
    154 
    155     module2->SetSSRC(test_ssrc + 1);
    156     module1->SetSSRC(test_ssrc);
    157     EXPECT_EQ(0, module1->SetSequenceNumber(test_sequence_number));
    158     EXPECT_EQ(0, module1->SetStartTimestamp(test_timestamp));
    159     EXPECT_EQ(0, module1->SetCSRCs(test_CSRC, 2));
    160     EXPECT_EQ(0, module1->SetCNAME("john.doe (at) test.test"));
    161 
    162     EXPECT_EQ(0, module1->SetSendingStatus(true));
    163 
    164     CodecInst voice_codec;
    165     voice_codec.pltype = 96;
    166     voice_codec.plfreq = 8000;
    167     voice_codec.rate = 64000;
    168     memcpy(voice_codec.plname, "PCMU", 5);
    169 
    170     EXPECT_EQ(0, module1->RegisterSendPayload(voice_codec));
    171     EXPECT_EQ(0, rtp_receiver1_->RegisterReceivePayload(
    172         voice_codec.plname,
    173         voice_codec.pltype,
    174         voice_codec.plfreq,
    175         voice_codec.channels,
    176         (voice_codec.rate < 0) ? 0 : voice_codec.rate));
    177     EXPECT_EQ(0, module2->RegisterSendPayload(voice_codec));
    178     EXPECT_EQ(0, rtp_receiver2_->RegisterReceivePayload(
    179         voice_codec.plname,
    180         voice_codec.pltype,
    181         voice_codec.plfreq,
    182         voice_codec.channels,
    183         (voice_codec.rate < 0) ? 0 : voice_codec.rate));
    184 
    185     // We need to send one RTP packet to get the RTCP packet to be accepted by
    186     // the receiving module.
    187     // send RTP packet with the data "testtest"
    188     const uint8_t test[9] = "testtest";
    189     EXPECT_EQ(0, module1->SendOutgoingData(webrtc::kAudioFrameSpeech, 96,
    190                                            0, -1, test, 8));
    191   }
    192 
    193   virtual void TearDown() {
    194     delete module1;
    195     delete module2;
    196     delete myRTCPFeedback1;
    197     delete myRTCPFeedback2;
    198     delete transport1;
    199     delete transport2;
    200     delete receiver;
    201   }
    202 
    203   int test_id;
    204   scoped_ptr<TestRtpFeedback> rtp_feedback1_;
    205   scoped_ptr<TestRtpFeedback> rtp_feedback2_;
    206   scoped_ptr<ReceiveStatistics> receive_statistics1_;
    207   scoped_ptr<ReceiveStatistics> receive_statistics2_;
    208   scoped_ptr<RTPPayloadRegistry> rtp_payload_registry1_;
    209   scoped_ptr<RTPPayloadRegistry> rtp_payload_registry2_;
    210   scoped_ptr<RtpReceiver> rtp_receiver1_;
    211   scoped_ptr<RtpReceiver> rtp_receiver2_;
    212   RtpRtcp* module1;
    213   RtpRtcp* module2;
    214   TestRtpReceiver* receiver;
    215   LoopBackTransport* transport1;
    216   LoopBackTransport* transport2;
    217   RtcpCallback* myRTCPFeedback1;
    218   RtcpCallback* myRTCPFeedback2;
    219 
    220   uint32_t test_ssrc;
    221   uint32_t test_timestamp;
    222   uint16_t test_sequence_number;
    223   uint32_t test_CSRC[webrtc::kRtpCsrcSize];
    224   SimulatedClock fake_clock;
    225 };
    226 
    227 TEST_F(RtpRtcpRtcpTest, RTCP_PLI_RPSI) {
    228   EXPECT_EQ(0, module1->SendRTCPReferencePictureSelection(kTestPictureId));
    229   EXPECT_EQ(0, module1->SendRTCPSliceLossIndication(156));
    230 }
    231 
    232 TEST_F(RtpRtcpRtcpTest, RTCP_CNAME) {
    233   uint32_t testOfCSRC[webrtc::kRtpCsrcSize];
    234   EXPECT_EQ(2, rtp_receiver2_->CSRCs(testOfCSRC));
    235   EXPECT_EQ(test_CSRC[0], testOfCSRC[0]);
    236   EXPECT_EQ(test_CSRC[1], testOfCSRC[1]);
    237 
    238   // Set cname of mixed.
    239   EXPECT_EQ(0, module1->AddMixedCNAME(test_CSRC[0], "john (at) 192.168.0.1"));
    240   EXPECT_EQ(0, module1->AddMixedCNAME(test_CSRC[1], "jane (at) 192.168.0.2"));
    241 
    242   EXPECT_EQ(-1, module1->RemoveMixedCNAME(test_CSRC[0] + 1));
    243   EXPECT_EQ(0, module1->RemoveMixedCNAME(test_CSRC[1]));
    244   EXPECT_EQ(0, module1->AddMixedCNAME(test_CSRC[1], "jane (at) 192.168.0.2"));
    245 
    246   // send RTCP packet, triggered by timer
    247   fake_clock.AdvanceTimeMilliseconds(7500);
    248   module1->Process();
    249   fake_clock.AdvanceTimeMilliseconds(100);
    250   module2->Process();
    251 
    252   char cName[RTCP_CNAME_SIZE];
    253   EXPECT_EQ(-1, module2->RemoteCNAME(rtp_receiver2_->SSRC() + 1, cName));
    254 
    255   // Check multiple CNAME.
    256   EXPECT_EQ(0, module2->RemoteCNAME(rtp_receiver2_->SSRC(), cName));
    257   EXPECT_EQ(0, strncmp(cName, "john.doe (at) test.test", RTCP_CNAME_SIZE));
    258 
    259   EXPECT_EQ(0, module2->RemoteCNAME(test_CSRC[0], cName));
    260   EXPECT_EQ(0, strncmp(cName, "john (at) 192.168.0.1", RTCP_CNAME_SIZE));
    261 
    262   EXPECT_EQ(0, module2->RemoteCNAME(test_CSRC[1], cName));
    263   EXPECT_EQ(0, strncmp(cName, "jane (at) 192.168.0.2", RTCP_CNAME_SIZE));
    264 
    265   EXPECT_EQ(0, module1->SetSendingStatus(false));
    266 
    267   // Test that BYE clears the CNAME
    268   EXPECT_EQ(-1, module2->RemoteCNAME(rtp_receiver2_->SSRC(), cName));
    269 }
    270 
    271 TEST_F(RtpRtcpRtcpTest, RTCP) {
    272   RTCPReportBlock reportBlock;
    273   reportBlock.remoteSSRC = 1;
    274   reportBlock.sourceSSRC = 2;
    275   reportBlock.cumulativeLost = 1;
    276   reportBlock.delaySinceLastSR = 2;
    277   reportBlock.extendedHighSeqNum = 3;
    278   reportBlock.fractionLost= 4;
    279   reportBlock.jitter = 5;
    280   reportBlock.lastSR = 6;
    281 
    282   // Set report blocks.
    283   EXPECT_EQ(0, module1->AddRTCPReportBlock(test_CSRC[0], &reportBlock));
    284 
    285   reportBlock.lastSR= 7;
    286   EXPECT_EQ(0, module1->AddRTCPReportBlock(test_CSRC[1], &reportBlock));
    287 
    288   uint32_t name = 't' << 24;
    289   name += 'e' << 16;
    290   name += 's' << 8;
    291   name += 't';
    292   EXPECT_EQ(0, module1->SetRTCPApplicationSpecificData(
    293       3,
    294       name,
    295       (const uint8_t *)"test test test test test test test test test"\
    296           " test test test test test test test test test test test test test"\
    297           " test test test test test test test test test test test test test"\
    298           " test test test test test test test test test test test test test"\
    299           " test test test test test test test test test test test test ",
    300           300));
    301 
    302   // send RTCP packet, triggered by timer
    303   fake_clock.AdvanceTimeMilliseconds(7500);
    304   module1->Process();
    305   fake_clock.AdvanceTimeMilliseconds(100);
    306   module2->Process();
    307 
    308   uint32_t receivedNTPsecs = 0;
    309   uint32_t receivedNTPfrac = 0;
    310   uint32_t RTCPArrivalTimeSecs = 0;
    311   uint32_t RTCPArrivalTimeFrac = 0;
    312   EXPECT_EQ(0, module2->RemoteNTP(&receivedNTPsecs,
    313                                   &receivedNTPfrac,
    314                                   &RTCPArrivalTimeSecs,
    315                                   &RTCPArrivalTimeFrac,
    316                                   NULL));
    317 
    318 
    319   // get all report blocks
    320   std::vector<RTCPReportBlock> report_blocks;
    321   EXPECT_EQ(0, module1->RemoteRTCPStat(&report_blocks));
    322   ASSERT_EQ(1u, report_blocks.size());
    323   const RTCPReportBlock& reportBlockReceived = report_blocks[0];
    324 
    325   float secSinceLastReport =
    326       static_cast<float>(reportBlockReceived.delaySinceLastSR) / 65536.0f;
    327   EXPECT_GE(0.101f, secSinceLastReport);
    328   EXPECT_LE(0.100f, secSinceLastReport);
    329   EXPECT_EQ(test_sequence_number, reportBlockReceived.extendedHighSeqNum);
    330   EXPECT_EQ(0, reportBlockReceived.fractionLost);
    331 
    332   EXPECT_EQ(static_cast<uint32_t>(0),
    333             reportBlockReceived.cumulativeLost);
    334 
    335   StreamStatistician *statistician =
    336       receive_statistics2_->GetStatistician(reportBlockReceived.sourceSSRC);
    337   RtcpStatistics stats;
    338   EXPECT_TRUE(statistician->GetStatistics(&stats, true));
    339   EXPECT_EQ(0, stats.fraction_lost);
    340   EXPECT_EQ((uint32_t)0, stats.cumulative_lost);
    341   EXPECT_EQ(test_sequence_number, stats.extended_max_sequence_number);
    342   EXPECT_EQ(reportBlockReceived.jitter, stats.jitter);
    343 
    344   uint16_t RTT;
    345   uint16_t avgRTT;
    346   uint16_t minRTT;
    347   uint16_t maxRTT;
    348 
    349   // Get RoundTripTime.
    350   EXPECT_EQ(0, module1->RTT(test_ssrc + 1, &RTT, &avgRTT, &minRTT, &maxRTT));
    351   EXPECT_GE(10, RTT);
    352   EXPECT_GE(10, avgRTT);
    353   EXPECT_GE(10, minRTT);
    354   EXPECT_GE(10, maxRTT);
    355 
    356   // Set report blocks.
    357   EXPECT_EQ(0, module1->AddRTCPReportBlock(test_CSRC[0], &reportBlock));
    358 
    359   // Test receive report.
    360   EXPECT_EQ(0, module1->SetSendingStatus(false));
    361 
    362   // Send RTCP packet, triggered by timer.
    363   fake_clock.AdvanceTimeMilliseconds(5000);
    364   module1->Process();
    365   module2->Process();
    366 }
    367 
    368 TEST_F(RtpRtcpRtcpTest, RemoteRTCPStatRemote) {
    369   std::vector<RTCPReportBlock> report_blocks;
    370 
    371   EXPECT_EQ(0, module1->RemoteRTCPStat(&report_blocks));
    372   EXPECT_EQ(0u, report_blocks.size());
    373 
    374   // send RTCP packet, triggered by timer
    375   fake_clock.AdvanceTimeMilliseconds(7500);
    376   module1->Process();
    377   fake_clock.AdvanceTimeMilliseconds(100);
    378   module2->Process();
    379 
    380   EXPECT_EQ(0, module1->RemoteRTCPStat(&report_blocks));
    381   ASSERT_EQ(1u, report_blocks.size());
    382 
    383   // |test_ssrc+1| is the SSRC of module2 that send the report.
    384   EXPECT_EQ(test_ssrc+1, report_blocks[0].remoteSSRC);
    385   EXPECT_EQ(test_ssrc, report_blocks[0].sourceSSRC);
    386 
    387   EXPECT_EQ(0u, report_blocks[0].cumulativeLost);
    388   EXPECT_LT(0u, report_blocks[0].delaySinceLastSR);
    389   EXPECT_EQ(test_sequence_number, report_blocks[0].extendedHighSeqNum);
    390   EXPECT_EQ(0u, report_blocks[0].fractionLost);
    391 }
    392