blob: b11a18b056a6d578227dc86c793772008f7817f6 [file] [log] [blame]
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +00001/*
leozwang@webrtc.org07c68b92012-02-29 16:09:51 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +00003 *
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/rtp_rtcp/test/testAPI/test_api.h"
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000012
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +000013#include <algorithm>
kwiberg84be5112016-04-27 01:19:58 -070014#include <memory>
Joachim Bauchd3b7ec22018-08-01 10:12:00 +020015#include <string>
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +000016#include <vector>
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
19#include "rtc_base/rate_limiter.h"
20#include "test/null_transport.h"
danilchapb8b6fbb2015-12-10 05:05:27 -080021
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000022namespace webrtc {
danilchap6a6f0892015-12-10 12:39:08 -080023
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000024void LoopBackTransport::SetSendModule(RtpRtcp* rtp_rtcp_module,
25 RTPPayloadRegistry* payload_registry,
26 RtpReceiver* receiver,
27 ReceiveStatistics* receive_statistics) {
28 rtp_rtcp_module_ = rtp_rtcp_module;
29 rtp_payload_registry_ = payload_registry;
30 rtp_receiver_ = receiver;
31 receive_statistics_ = receive_statistics;
32}
33
34void LoopBackTransport::DropEveryNthPacket(int n) {
35 packet_loss_ = n;
36}
37
stefan1d8a5062015-10-02 03:39:33 -070038bool LoopBackTransport::SendRtp(const uint8_t* data,
39 size_t len,
40 const PacketOptions& options) {
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000041 count_++;
42 if (packet_loss_ > 0) {
43 if ((count_ % packet_loss_) == 0) {
pbos2d566682015-09-28 09:59:31 -070044 return true;
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000045 }
46 }
47 RTPHeader header;
kwiberg84be5112016-04-27 01:19:58 -070048 std::unique_ptr<RtpHeaderParser> parser(RtpHeaderParser::Create());
ossub2d1e0d2016-10-05 07:51:44 -070049 if (!parser->Parse(data, len, &header)) {
pbos2d566682015-09-28 09:59:31 -070050 return false;
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000051 }
Karl Wiberg73b60b82017-09-21 15:00:58 +020052 const auto pl =
53 rtp_payload_registry_->PayloadTypeToPayload(header.payloadType);
54 if (!pl) {
pbos2d566682015-09-28 09:59:31 -070055 return false;
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000056 }
ossub2d1e0d2016-10-05 07:51:44 -070057 const uint8_t* payload = data + header.headerLength;
58 RTC_CHECK_GE(len, header.headerLength);
59 const size_t payload_length = len - header.headerLength;
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000060 receive_statistics_->IncomingPacket(header, len, false);
nissebe3e5392017-05-31 07:35:16 -070061 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
Niels Möller22ec9522017-10-05 08:39:15 +020062 pl->typeSpecific);
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000063}
64
pbos2d566682015-09-28 09:59:31 -070065bool LoopBackTransport::SendRtcp(const uint8_t* data, size_t len) {
nisse479d3d72017-09-13 07:53:37 -070066 rtp_rtcp_module_->IncomingRtcpPacket((const uint8_t*)data, len);
pbos2d566682015-09-28 09:59:31 -070067 return true;
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000068}
69
70int32_t TestRtpReceiver::OnReceivedPayloadData(
71 const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +020072 size_t payload_size,
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000073 const webrtc::WebRtcRTPHeader* rtp_header) {
74 EXPECT_LE(payload_size, sizeof(payload_data_));
75 memcpy(payload_data_, payload_data, payload_size);
76 memcpy(&rtp_header_, rtp_header, sizeof(rtp_header_));
77 payload_size_ = payload_size;
78 return 0;
79}
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000080
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +000081class RtpRtcpAPITest : public ::testing::Test {
82 protected:
Erik Språng737336d2016-07-29 12:59:36 +020083 RtpRtcpAPITest()
84 : fake_clock_(123456), retransmission_rate_limiter_(&fake_clock_, 1000) {
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000085 test_csrcs_.push_back(1234);
86 test_csrcs_.push_back(2345);
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000087 test_ssrc_ = 3456;
88 test_timestamp_ = 4567;
89 test_sequence_number_ = 2345;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +000090 }
Danil Chapovalovdd7e2842018-03-09 15:37:03 +000091 ~RtpRtcpAPITest() override = default;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +000092
nisse7d59f6b2017-02-21 03:40:24 -080093 const uint32_t initial_ssrc = 8888;
94
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000095 void SetUp() override {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000096 RtpRtcp::Configuration configuration;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000097 configuration.audio = true;
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000098 configuration.clock = &fake_clock_;
sprang86fd9ed2015-09-29 04:45:43 -070099 configuration.outgoing_transport = &null_transport_;
Erik Språng737336d2016-07-29 12:59:36 +0200100 configuration.retransmission_rate_limiter = &retransmission_rate_limiter_;
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000101 module_.reset(RtpRtcp::CreateRtpRtcp(configuration));
nisse7d59f6b2017-02-21 03:40:24 -0800102 module_->SetSSRC(initial_ssrc);
magjedf3feeff2016-11-25 06:40:25 -0800103 rtp_payload_registry_.reset(new RTPPayloadRegistry());
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000104 }
105
kwiberg84be5112016-04-27 01:19:58 -0700106 std::unique_ptr<RTPPayloadRegistry> rtp_payload_registry_;
kwiberg84be5112016-04-27 01:19:58 -0700107 std::unique_ptr<RtpRtcp> module_;
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000108 uint32_t test_ssrc_;
109 uint32_t test_timestamp_;
110 uint16_t test_sequence_number_;
111 std::vector<uint32_t> test_csrcs_;
112 SimulatedClock fake_clock_;
sprang86fd9ed2015-09-29 04:45:43 -0700113 test::NullTransport null_transport_;
Erik Språng737336d2016-07-29 12:59:36 +0200114 RateLimiter retransmission_rate_limiter_;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000115};
116
117TEST_F(RtpRtcpAPITest, Basic) {
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000118 module_->SetSequenceNumber(test_sequence_number_);
119 EXPECT_EQ(test_sequence_number_, module_->SequenceNumber());
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000120
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000121 module_->SetStartTimestamp(test_timestamp_);
122 EXPECT_EQ(test_timestamp_, module_->StartTimestamp());
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000123
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000124 EXPECT_FALSE(module_->Sending());
125 EXPECT_EQ(0, module_->SetSendingStatus(true));
126 EXPECT_TRUE(module_->Sending());
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000127}
128
nisse284542b2017-01-10 08:58:32 -0800129TEST_F(RtpRtcpAPITest, PacketSize) {
130 module_->SetMaxRtpPacketSize(1234);
131 EXPECT_EQ(1234u, module_->MaxRtpPacketSize());
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000132}
133
134TEST_F(RtpRtcpAPITest, SSRC) {
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000135 module_->SetSSRC(test_ssrc_);
136 EXPECT_EQ(test_ssrc_, module_->SSRC());
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000137}
138
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000139TEST_F(RtpRtcpAPITest, RTCP) {
pbosda903ea2015-10-02 02:36:56 -0700140 EXPECT_EQ(RtcpMode::kOff, module_->RTCP());
141 module_->SetRTCPStatus(RtcpMode::kCompound);
142 EXPECT_EQ(RtcpMode::kCompound, module_->RTCP());
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000143
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000144 EXPECT_EQ(0, module_->SetCNAME("john.doe@test.test"));
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000145
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000146 EXPECT_FALSE(module_->TMMBR());
147 module_->SetTMMBRStatus(true);
148 EXPECT_TRUE(module_->TMMBR());
149 module_->SetTMMBRStatus(false);
150 EXPECT_FALSE(module_->TMMBR());
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000151}
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000152
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000153TEST_F(RtpRtcpAPITest, RtxSender) {
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000154 module_->SetRtxSendStatus(kRtxRetransmitted);
155 EXPECT_EQ(kRtxRetransmitted, module_->RtxSendStatus());
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000156
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000157 module_->SetRtxSendStatus(kRtxOff);
158 EXPECT_EQ(kRtxOff, module_->RtxSendStatus());
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000159
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000160 module_->SetRtxSendStatus(kRtxRetransmitted);
161 EXPECT_EQ(kRtxRetransmitted, module_->RtxSendStatus());
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000162}
163
Joachim Bauchd3b7ec22018-08-01 10:12:00 +0200164TEST_F(RtpRtcpAPITest, LegalMidName) {
165 static const std::string kLegalMidNames[] = {
166 // clang-format off
167 "audio",
168 "audio0",
169 "audio_0",
170 // clang-format on
171 };
172 for (const auto& name : kLegalMidNames) {
173 EXPECT_TRUE(StreamId::IsLegalMidName(name))
174 << "Mid should be legal: " << name;
175 }
176
177 static const std::string kNonLegalMidNames[] = {
178 // clang-format off
179 "",
180 "(audio0)",
181 // clang-format on
182 };
183 for (const auto& name : kNonLegalMidNames) {
184 EXPECT_FALSE(StreamId::IsLegalMidName(name))
185 << "Mid should not be legal: " << name;
186 }
187}
188
189TEST_F(RtpRtcpAPITest, LegalRsidName) {
190 static const std::string kLegalRsidNames[] = {
191 // clang-format off
192 "audio",
193 "audio0",
194 // clang-format on
195 };
196 for (const auto& name : kLegalRsidNames) {
197 EXPECT_TRUE(StreamId::IsLegalRsidName(name))
198 << "Rsid should be legal: " << name;
199 }
200
201 static const std::string kNonLegalRsidNames[] = {
202 // clang-format off
203 "",
204 "audio_0",
205 "(audio0)",
206 // clang-format on
207 };
208 for (const auto& name : kNonLegalRsidNames) {
209 EXPECT_FALSE(StreamId::IsLegalRsidName(name))
210 << "Rsid should not be legal: " << name;
211 }
212}
213
danilchap6a6f0892015-12-10 12:39:08 -0800214} // namespace webrtc