blob: fadf8f4240f54efc88472b570a02c9d52a82bcd4 [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
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +000011#include "webrtc/modules/rtp_rtcp/test/testAPI/test_api.h"
12
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +000013#include <algorithm>
kwiberg84be5112016-04-27 01:19:58 -070014#include <memory>
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +000015#include <vector>
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +000016
Edward Lemurc20978e2017-07-06 19:44:34 +020017#include "webrtc/rtc_base/checks.h"
18#include "webrtc/rtc_base/rate_limiter.h"
danilchapb8b6fbb2015-12-10 05:05:27 -080019#include "webrtc/test/null_transport.h"
20
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000021namespace webrtc {
danilchap6a6f0892015-12-10 12:39:08 -080022
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000023void 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
33void LoopBackTransport::DropEveryNthPacket(int n) {
34 packet_loss_ = n;
35}
36
stefan1d8a5062015-10-02 03:39:33 -070037bool LoopBackTransport::SendRtp(const uint8_t* data,
38 size_t len,
39 const PacketOptions& options) {
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000040 count_++;
41 if (packet_loss_ > 0) {
42 if ((count_ % packet_loss_) == 0) {
pbos2d566682015-09-28 09:59:31 -070043 return true;
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000044 }
45 }
46 RTPHeader header;
kwiberg84be5112016-04-27 01:19:58 -070047 std::unique_ptr<RtpHeaderParser> parser(RtpHeaderParser::Create());
ossub2d1e0d2016-10-05 07:51:44 -070048 if (!parser->Parse(data, len, &header)) {
pbos2d566682015-09-28 09:59:31 -070049 return false;
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000050 }
51 PayloadUnion payload_specific;
52 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
53 &payload_specific)) {
pbos2d566682015-09-28 09:59:31 -070054 return false;
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000055 }
ossub2d1e0d2016-10-05 07:51:44 -070056 const uint8_t* payload = data + header.headerLength;
57 RTC_CHECK_GE(len, header.headerLength);
58 const size_t payload_length = len - header.headerLength;
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000059 receive_statistics_->IncomingPacket(header, len, false);
nissebe3e5392017-05-31 07:35:16 -070060 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
61 payload_specific, true);
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000062}
63
pbos2d566682015-09-28 09:59:31 -070064bool LoopBackTransport::SendRtcp(const uint8_t* data, size_t len) {
nisse479d3d72017-09-13 07:53:37 -070065 rtp_rtcp_module_->IncomingRtcpPacket((const uint8_t*)data, len);
pbos2d566682015-09-28 09:59:31 -070066 return true;
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000067}
68
69int32_t TestRtpReceiver::OnReceivedPayloadData(
70 const uint8_t* payload_data,
Peter Boström02083222016-06-14 12:52:54 +020071 size_t payload_size,
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000072 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}
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000079
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +000080class RtpRtcpAPITest : public ::testing::Test {
81 protected:
Erik Språng737336d2016-07-29 12:59:36 +020082 RtpRtcpAPITest()
83 : fake_clock_(123456), retransmission_rate_limiter_(&fake_clock_, 1000) {
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000084 test_csrcs_.push_back(1234);
85 test_csrcs_.push_back(2345);
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000086 test_ssrc_ = 3456;
87 test_timestamp_ = 4567;
88 test_sequence_number_ = 2345;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +000089 }
90 ~RtpRtcpAPITest() {}
91
nisse7d59f6b2017-02-21 03:40:24 -080092 const uint32_t initial_ssrc = 8888;
93
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000094 void SetUp() override {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000095 RtpRtcp::Configuration configuration;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000096 configuration.audio = true;
changbin.shao@intel.com9691b362015-01-20 05:42:52 +000097 configuration.clock = &fake_clock_;
sprang86fd9ed2015-09-29 04:45:43 -070098 configuration.outgoing_transport = &null_transport_;
Erik Språng737336d2016-07-29 12:59:36 +020099 configuration.retransmission_rate_limiter = &retransmission_rate_limiter_;
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000100 module_.reset(RtpRtcp::CreateRtpRtcp(configuration));
nisse7d59f6b2017-02-21 03:40:24 -0800101 module_->SetSSRC(initial_ssrc);
magjedf3feeff2016-11-25 06:40:25 -0800102 rtp_payload_registry_.reset(new RTPPayloadRegistry());
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000103 }
104
kwiberg84be5112016-04-27 01:19:58 -0700105 std::unique_ptr<RTPPayloadRegistry> rtp_payload_registry_;
kwiberg84be5112016-04-27 01:19:58 -0700106 std::unique_ptr<RtpRtcp> module_;
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000107 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_;
sprang86fd9ed2015-09-29 04:45:43 -0700112 test::NullTransport null_transport_;
Erik Språng737336d2016-07-29 12:59:36 +0200113 RateLimiter retransmission_rate_limiter_;
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000114};
115
116TEST_F(RtpRtcpAPITest, Basic) {
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000117 module_->SetSequenceNumber(test_sequence_number_);
118 EXPECT_EQ(test_sequence_number_, module_->SequenceNumber());
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000119
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000120 module_->SetStartTimestamp(test_timestamp_);
121 EXPECT_EQ(test_timestamp_, module_->StartTimestamp());
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000122
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000123 EXPECT_FALSE(module_->Sending());
124 EXPECT_EQ(0, module_->SetSendingStatus(true));
125 EXPECT_TRUE(module_->Sending());
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000126}
127
nisse284542b2017-01-10 08:58:32 -0800128TEST_F(RtpRtcpAPITest, PacketSize) {
129 module_->SetMaxRtpPacketSize(1234);
130 EXPECT_EQ(1234u, module_->MaxRtpPacketSize());
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000131}
132
133TEST_F(RtpRtcpAPITest, SSRC) {
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000134 module_->SetSSRC(test_ssrc_);
135 EXPECT_EQ(test_ssrc_, module_->SSRC());
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000136}
137
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000138TEST_F(RtpRtcpAPITest, RTCP) {
pbosda903ea2015-10-02 02:36:56 -0700139 EXPECT_EQ(RtcpMode::kOff, module_->RTCP());
140 module_->SetRTCPStatus(RtcpMode::kCompound);
141 EXPECT_EQ(RtcpMode::kCompound, module_->RTCP());
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000142
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000143 EXPECT_EQ(0, module_->SetCNAME("john.doe@test.test"));
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000144
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000145 EXPECT_FALSE(module_->TMMBR());
146 module_->SetTMMBRStatus(true);
147 EXPECT_TRUE(module_->TMMBR());
148 module_->SetTMMBRStatus(false);
149 EXPECT_FALSE(module_->TMMBR());
pwestin@webrtc.org8281e7d2012-01-10 14:09:18 +0000150}
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000151
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000152TEST_F(RtpRtcpAPITest, RtxSender) {
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000153 module_->SetRtxSendStatus(kRtxRetransmitted);
154 EXPECT_EQ(kRtxRetransmitted, module_->RtxSendStatus());
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000155
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000156 module_->SetRtxSendStatus(kRtxOff);
157 EXPECT_EQ(kRtxOff, module_->RtxSendStatus());
pbos@webrtc.org0b0c2412015-01-13 14:15:15 +0000158
changbin.shao@intel.com9691b362015-01-20 05:42:52 +0000159 module_->SetRtxSendStatus(kRtxRetransmitted);
160 EXPECT_EQ(kRtxRetransmitted, module_->RtxSendStatus());
mikhal@webrtc.orgbda7f302013-03-15 23:21:52 +0000161}
162
danilchap6a6f0892015-12-10 12:39:08 -0800163} // namespace webrtc