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 "modules/rtp_rtcp/test/testAPI/test_api.h"
12 
13 #include <algorithm>
14 #include <memory>
15 #include <vector>
16 
17 #include "rtc_base/checks.h"
18 #include "rtc_base/rate_limiter.h"
19 #include "test/null_transport.h"
20 
21 namespace webrtc {
22 
SetSendModule(RtpRtcp * rtp_rtcp_module,RTPPayloadRegistry * payload_registry,RtpReceiver * receiver,ReceiveStatistics * receive_statistics)23 void LoopBackTransport::SetSendModule(RtpRtcp* rtp_rtcp_module,
24                                       RTPPayloadRegistry* payload_registry,
25                                       RtpReceiver* receiver,
26                                       ReceiveStatistics* receive_statistics) {
27   rtp_rtcp_module_ = rtp_rtcp_module;
28   rtp_payload_registry_ = payload_registry;
29   rtp_receiver_ = receiver;
30   receive_statistics_ = receive_statistics;
31 }
32 
DropEveryNthPacket(int n)33 void LoopBackTransport::DropEveryNthPacket(int n) {
34   packet_loss_ = n;
35 }
36 
SendRtp(const uint8_t * data,size_t len,const PacketOptions & options)37 bool LoopBackTransport::SendRtp(const uint8_t* data,
38                                 size_t len,
39                                 const PacketOptions& options) {
40   count_++;
41   if (packet_loss_ > 0) {
42     if ((count_ % packet_loss_) == 0) {
43       return true;
44     }
45   }
46   RTPHeader header;
47   std::unique_ptr<RtpHeaderParser> parser(RtpHeaderParser::Create());
48   if (!parser->Parse(data, len, &header)) {
49     return false;
50   }
51   const auto pl =
52       rtp_payload_registry_->PayloadTypeToPayload(header.payloadType);
53   if (!pl) {
54     return false;
55   }
56   const uint8_t* payload = data + header.headerLength;
57   RTC_CHECK_GE(len, header.headerLength);
58   const size_t payload_length = len - header.headerLength;
59   receive_statistics_->IncomingPacket(header, len, false);
60   return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
61                                           pl->typeSpecific);
62 }
63 
SendRtcp(const uint8_t * data,size_t len)64 bool LoopBackTransport::SendRtcp(const uint8_t* data, size_t len) {
65   rtp_rtcp_module_->IncomingRtcpPacket((const uint8_t*)data, len);
66   return true;
67 }
68 
OnReceivedPayloadData(const uint8_t * payload_data,size_t payload_size,const webrtc::WebRtcRTPHeader * rtp_header)69 int32_t TestRtpReceiver::OnReceivedPayloadData(
70     const uint8_t* payload_data,
71     size_t payload_size,
72     const webrtc::WebRtcRTPHeader* rtp_header) {
73   EXPECT_LE(payload_size, sizeof(payload_data_));
74   memcpy(payload_data_, payload_data, payload_size);
75   memcpy(&rtp_header_, rtp_header, sizeof(rtp_header_));
76   payload_size_ = payload_size;
77   return 0;
78 }
79 
80 class RtpRtcpAPITest : public ::testing::Test {
81  protected:
RtpRtcpAPITest()82   RtpRtcpAPITest()
83       : fake_clock_(123456), retransmission_rate_limiter_(&fake_clock_, 1000) {
84     test_csrcs_.push_back(1234);
85     test_csrcs_.push_back(2345);
86     test_ssrc_ = 3456;
87     test_timestamp_ = 4567;
88     test_sequence_number_ = 2345;
89   }
~RtpRtcpAPITest()90   ~RtpRtcpAPITest() {}
91 
92   const uint32_t initial_ssrc = 8888;
93 
SetUp()94   void SetUp() override {
95     RtpRtcp::Configuration configuration;
96     configuration.audio = true;
97     configuration.clock = &fake_clock_;
98     configuration.outgoing_transport = &null_transport_;
99     configuration.retransmission_rate_limiter = &retransmission_rate_limiter_;
100     module_.reset(RtpRtcp::CreateRtpRtcp(configuration));
101     module_->SetSSRC(initial_ssrc);
102     rtp_payload_registry_.reset(new RTPPayloadRegistry());
103   }
104 
105   std::unique_ptr<RTPPayloadRegistry> rtp_payload_registry_;
106   std::unique_ptr<RtpRtcp> module_;
107   uint32_t test_ssrc_;
108   uint32_t test_timestamp_;
109   uint16_t test_sequence_number_;
110   std::vector<uint32_t> test_csrcs_;
111   SimulatedClock fake_clock_;
112   test::NullTransport null_transport_;
113   RateLimiter retransmission_rate_limiter_;
114 };
115 
TEST_F(RtpRtcpAPITest,Basic)116 TEST_F(RtpRtcpAPITest, Basic) {
117   module_->SetSequenceNumber(test_sequence_number_);
118   EXPECT_EQ(test_sequence_number_, module_->SequenceNumber());
119 
120   module_->SetStartTimestamp(test_timestamp_);
121   EXPECT_EQ(test_timestamp_, module_->StartTimestamp());
122 
123   EXPECT_FALSE(module_->Sending());
124   EXPECT_EQ(0, module_->SetSendingStatus(true));
125   EXPECT_TRUE(module_->Sending());
126 }
127 
TEST_F(RtpRtcpAPITest,PacketSize)128 TEST_F(RtpRtcpAPITest, PacketSize) {
129   module_->SetMaxRtpPacketSize(1234);
130   EXPECT_EQ(1234u, module_->MaxRtpPacketSize());
131 }
132 
TEST_F(RtpRtcpAPITest,SSRC)133 TEST_F(RtpRtcpAPITest, SSRC) {
134   module_->SetSSRC(test_ssrc_);
135   EXPECT_EQ(test_ssrc_, module_->SSRC());
136 }
137 
TEST_F(RtpRtcpAPITest,RTCP)138 TEST_F(RtpRtcpAPITest, RTCP) {
139   EXPECT_EQ(RtcpMode::kOff, module_->RTCP());
140   module_->SetRTCPStatus(RtcpMode::kCompound);
141   EXPECT_EQ(RtcpMode::kCompound, module_->RTCP());
142 
143   EXPECT_EQ(0, module_->SetCNAME("john.doe@test.test"));
144 
145   EXPECT_FALSE(module_->TMMBR());
146   module_->SetTMMBRStatus(true);
147   EXPECT_TRUE(module_->TMMBR());
148   module_->SetTMMBRStatus(false);
149   EXPECT_FALSE(module_->TMMBR());
150 }
151 
TEST_F(RtpRtcpAPITest,RtxSender)152 TEST_F(RtpRtcpAPITest, RtxSender) {
153   module_->SetRtxSendStatus(kRtxRetransmitted);
154   EXPECT_EQ(kRtxRetransmitted, module_->RtxSendStatus());
155 
156   module_->SetRtxSendStatus(kRtxOff);
157   EXPECT_EQ(kRtxOff, module_->RtxSendStatus());
158 
159   module_->SetRtxSendStatus(kRtxRetransmitted);
160   EXPECT_EQ(kRtxRetransmitted, module_->RtxSendStatus());
161 }
162 
163 }  // namespace webrtc
164