blob: 0c95e7fa76a3e3ed558627ea16db61db3c76ba68 [file] [log] [blame]
Stefan Holmere5904162015-03-26 11:11:06 +01001/*
2 * Copyright (c) 2015 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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "modules/pacing/packet_router.h"
12
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <cstddef>
14#include <cstdint>
Mirko Bonadei317a1f02019-09-17 17:06:18 +020015#include <memory>
Erik Språngf6468d22019-07-05 16:53:43 +020016#include <utility>
Stefan Holmere5904162015-03-26 11:11:06 +010017
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "api/units/time_delta.h"
Erik Språngf6468d22019-07-05 16:53:43 +020019#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/rtp_rtcp/mocks/mock_rtp_rtcp.h"
21#include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
22#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/fake_clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "test/gmock.h"
25#include "test/gtest.h"
Stefan Holmere5904162015-03-26 11:11:06 +010026
Stefan Holmere5904162015-03-26 11:11:06 +010027namespace webrtc {
28
eladalon822ff2b2017-08-01 06:30:28 -070029// TODO(eladalon): Restructure and/or replace the existing monolithic tests
30// (only some of the test are monolithic) according to the new
31// guidelines - small tests for one thing at a time.
32// (I'm not removing any tests during CL, so as to demonstrate no regressions.)
33
eladalonb1338fe2017-08-01 09:36:19 -070034namespace {
danilchap47085372017-08-10 06:03:57 -070035
36using ::testing::_;
37using ::testing::AnyNumber;
38using ::testing::AtLeast;
39using ::testing::Field;
40using ::testing::Gt;
41using ::testing::Le;
42using ::testing::NiceMock;
Erik Språngf6468d22019-07-05 16:53:43 +020043using ::testing::Property;
danilchap47085372017-08-10 06:03:57 -070044using ::testing::Return;
danilchap47085372017-08-10 06:03:57 -070045using ::testing::SaveArg;
46
eladalonb1338fe2017-08-01 09:36:19 -070047constexpr int kProbeMinProbes = 5;
48constexpr int kProbeMinBytes = 1000;
49
eladalonb1338fe2017-08-01 09:36:19 -070050} // namespace
eladalon822ff2b2017-08-01 06:30:28 -070051
Erik Språng4208a132019-08-26 08:58:45 +020052class PacketRouterTest : public ::testing::Test {
53 public:
54 PacketRouterTest() {
Danil Chapovalova74e4772019-09-12 17:53:04 +020055 extension_manager.Register<TransportSequenceNumber>(/*id=*/1);
Erik Språng4208a132019-08-26 08:58:45 +020056 }
eladalon32040ef2017-08-02 06:29:00 -070057
Erik Språng4208a132019-08-26 08:58:45 +020058 protected:
59 std::unique_ptr<RtpPacketToSend> BuildRtpPacket(uint32_t ssrc) {
60 std::unique_ptr<RtpPacketToSend> packet =
Mirko Bonadei317a1f02019-09-17 17:06:18 +020061 std::make_unique<RtpPacketToSend>(&extension_manager);
Erik Språng4208a132019-08-26 08:58:45 +020062 packet->SetSsrc(ssrc);
63 return packet;
64 }
eladalon32040ef2017-08-02 06:29:00 -070065
Erik Språng4208a132019-08-26 08:58:45 +020066 PacketRouter packet_router_;
67 RtpHeaderExtensionMap extension_manager;
68};
eladalon32040ef2017-08-02 06:29:00 -070069
Erik Språng4208a132019-08-26 08:58:45 +020070TEST_F(PacketRouterTest, Sanity_NoModuleRegistered_GeneratePadding) {
eladalon32040ef2017-08-02 06:29:00 -070071 constexpr size_t bytes = 300;
72 const PacedPacketInfo paced_info(1, kProbeMinProbes, kProbeMinBytes);
73
Erik Språng4208a132019-08-26 08:58:45 +020074 EXPECT_TRUE(packet_router_.GeneratePadding(bytes).empty());
eladalon32040ef2017-08-02 06:29:00 -070075}
76
Erik Språng4208a132019-08-26 08:58:45 +020077TEST_F(PacketRouterTest, Sanity_NoModuleRegistered_OnReceiveBitrateChanged) {
eladalon32040ef2017-08-02 06:29:00 -070078 const std::vector<uint32_t> ssrcs = {1, 2, 3};
79 constexpr uint32_t bitrate_bps = 10000;
80
Erik Språng4208a132019-08-26 08:58:45 +020081 packet_router_.OnReceiveBitrateChanged(ssrcs, bitrate_bps);
eladalon32040ef2017-08-02 06:29:00 -070082}
83
Erik Språng4208a132019-08-26 08:58:45 +020084TEST_F(PacketRouterTest, Sanity_NoModuleRegistered_SendRemb) {
eladalon32040ef2017-08-02 06:29:00 -070085 const std::vector<uint32_t> ssrcs = {1, 2, 3};
86 constexpr uint32_t bitrate_bps = 10000;
87
Erik Språng4208a132019-08-26 08:58:45 +020088 EXPECT_FALSE(packet_router_.SendRemb(bitrate_bps, ssrcs));
eladalon32040ef2017-08-02 06:29:00 -070089}
90
Erik Språng4208a132019-08-26 08:58:45 +020091TEST_F(PacketRouterTest, Sanity_NoModuleRegistered_SendTransportFeedback) {
Per Kjellanderee153c92019-10-10 16:43:46 +020092 std::vector<std::unique_ptr<rtcp::RtcpPacket>> feedback;
93 feedback.push_back(std::make_unique<rtcp::TransportFeedback>());
eladalon32040ef2017-08-02 06:29:00 -070094
Per Kjellanderee153c92019-10-10 16:43:46 +020095 EXPECT_FALSE(packet_router_.SendCombinedRtcpPacket(std::move(feedback)));
eladalon32040ef2017-08-02 06:29:00 -070096}
97
Erik Språng4208a132019-08-26 08:58:45 +020098TEST_F(PacketRouterTest, GeneratePaddingPicksCorrectModule) {
Erik Språng478cb462019-06-26 15:49:27 +020099 // Two RTP modules. The first (prioritized due to rtx) isn't sending media so
100 // should not be called.
101 const uint16_t kSsrc1 = 1234;
102 const uint16_t kSsrc2 = 4567;
103
104 NiceMock<MockRtpRtcp> rtp_1;
105 ON_CALL(rtp_1, RtxSendStatus()).WillByDefault(Return(kRtxRedundantPayloads));
106 ON_CALL(rtp_1, SSRC()).WillByDefault(Return(kSsrc1));
Mirko Bonadei999a72a2019-07-12 17:33:46 +0000107 ON_CALL(rtp_1, SupportsPadding).WillByDefault(Return(false));
Erik Språng478cb462019-06-26 15:49:27 +0200108
109 NiceMock<MockRtpRtcp> rtp_2;
110 ON_CALL(rtp_2, RtxSendStatus()).WillByDefault(Return(kRtxOff));
111 ON_CALL(rtp_2, SSRC()).WillByDefault(Return(kSsrc2));
Mirko Bonadei999a72a2019-07-12 17:33:46 +0000112 ON_CALL(rtp_2, SupportsPadding).WillByDefault(Return(true));
Erik Språng478cb462019-06-26 15:49:27 +0200113
Erik Språng4208a132019-08-26 08:58:45 +0200114 packet_router_.AddSendRtpModule(&rtp_1, false);
115 packet_router_.AddSendRtpModule(&rtp_2, false);
Erik Språng478cb462019-06-26 15:49:27 +0200116
117 const size_t kPaddingSize = 123;
Erik Språngf6468d22019-07-05 16:53:43 +0200118 const size_t kExpectedPaddingPackets = 1;
Erik Språng478cb462019-06-26 15:49:27 +0200119 EXPECT_CALL(rtp_1, GeneratePadding(_)).Times(0);
Erik Språngf6468d22019-07-05 16:53:43 +0200120 EXPECT_CALL(rtp_2, GeneratePadding(kPaddingSize))
121 .WillOnce([&](size_t padding_size) {
122 return std::vector<std::unique_ptr<RtpPacketToSend>>(
123 kExpectedPaddingPackets);
124 });
Erik Språng4208a132019-08-26 08:58:45 +0200125 auto generated_padding = packet_router_.GeneratePadding(kPaddingSize);
Erik Språngf6468d22019-07-05 16:53:43 +0200126 EXPECT_EQ(generated_padding.size(), kExpectedPaddingPackets);
Erik Språng478cb462019-06-26 15:49:27 +0200127
Erik Språng4208a132019-08-26 08:58:45 +0200128 packet_router_.RemoveSendRtpModule(&rtp_1);
129 packet_router_.RemoveSendRtpModule(&rtp_2);
Erik Språng478cb462019-06-26 15:49:27 +0200130}
131
Erik Språng4208a132019-08-26 08:58:45 +0200132TEST_F(PacketRouterTest, PadsOnLastActiveMediaStream) {
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200133 const uint16_t kSsrc1 = 1234;
134 const uint16_t kSsrc2 = 4567;
135 const uint16_t kSsrc3 = 8901;
136
137 // First two rtp modules send media and have rtx.
138 NiceMock<MockRtpRtcp> rtp_1;
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200139 EXPECT_CALL(rtp_1, SSRC()).WillRepeatedly(Return(kSsrc1));
Mirko Bonadei999a72a2019-07-12 17:33:46 +0000140 EXPECT_CALL(rtp_1, SupportsPadding).WillRepeatedly(Return(true));
141 EXPECT_CALL(rtp_1, SupportsRtxPayloadPadding).WillRepeatedly(Return(true));
Erik Språng4208a132019-08-26 08:58:45 +0200142 EXPECT_CALL(rtp_1, TrySendPacket).WillRepeatedly(Return(false));
143 EXPECT_CALL(
144 rtp_1,
145 TrySendPacket(
146 ::testing::Pointee(Property(&RtpPacketToSend::Ssrc, kSsrc1)), _))
147 .WillRepeatedly(Return(true));
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200148
149 NiceMock<MockRtpRtcp> rtp_2;
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200150 EXPECT_CALL(rtp_2, SSRC()).WillRepeatedly(Return(kSsrc2));
Mirko Bonadei999a72a2019-07-12 17:33:46 +0000151 EXPECT_CALL(rtp_2, SupportsPadding).WillRepeatedly(Return(true));
152 EXPECT_CALL(rtp_2, SupportsRtxPayloadPadding).WillRepeatedly(Return(true));
Erik Språng4208a132019-08-26 08:58:45 +0200153 EXPECT_CALL(rtp_2, TrySendPacket).WillRepeatedly(Return(false));
154 EXPECT_CALL(
155 rtp_2,
156 TrySendPacket(
157 ::testing::Pointee(Property(&RtpPacketToSend::Ssrc, kSsrc2)), _))
158 .WillRepeatedly(Return(true));
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200159
160 // Third module is sending media, but does not support rtx.
161 NiceMock<MockRtpRtcp> rtp_3;
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200162 EXPECT_CALL(rtp_3, SSRC()).WillRepeatedly(Return(kSsrc3));
Mirko Bonadei999a72a2019-07-12 17:33:46 +0000163 EXPECT_CALL(rtp_3, SupportsPadding).WillRepeatedly(Return(true));
Erik Språng4208a132019-08-26 08:58:45 +0200164 EXPECT_CALL(rtp_3, SupportsRtxPayloadPadding).WillRepeatedly(Return(false));
165 EXPECT_CALL(rtp_3, TrySendPacket).WillRepeatedly(Return(false));
166 EXPECT_CALL(
167 rtp_3,
168 TrySendPacket(
169 ::testing::Pointee(Property(&RtpPacketToSend::Ssrc, kSsrc3)), _))
170 .WillRepeatedly(Return(true));
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200171
Erik Språng4208a132019-08-26 08:58:45 +0200172 packet_router_.AddSendRtpModule(&rtp_1, false);
173 packet_router_.AddSendRtpModule(&rtp_2, false);
174 packet_router_.AddSendRtpModule(&rtp_3, false);
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200175
176 const size_t kPaddingBytes = 100;
177
178 // Initially, padding will be sent on last added rtp module that sends media
179 // and supports rtx.
Erik Språng4208a132019-08-26 08:58:45 +0200180 EXPECT_CALL(rtp_2, GeneratePadding(kPaddingBytes))
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200181 .Times(1)
Erik Språngc06aef22019-10-17 13:02:27 +0200182 .WillOnce([&](size_t target_size_bytes) {
183 std::vector<std::unique_ptr<RtpPacketToSend>> packets;
184 packets.push_back(BuildRtpPacket(kSsrc2));
185 return packets;
Erik Språng4208a132019-08-26 08:58:45 +0200186 });
187 packet_router_.GeneratePadding(kPaddingBytes);
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200188
189 // Send media on first module. Padding should be sent on that module.
Erik Språng4208a132019-08-26 08:58:45 +0200190 packet_router_.SendPacket(BuildRtpPacket(kSsrc1), PacedPacketInfo());
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200191
Erik Språng4208a132019-08-26 08:58:45 +0200192 EXPECT_CALL(rtp_1, GeneratePadding(kPaddingBytes))
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200193 .Times(1)
Erik Språngc06aef22019-10-17 13:02:27 +0200194 .WillOnce([&](size_t target_size_bytes) {
195 std::vector<std::unique_ptr<RtpPacketToSend>> packets;
196 packets.push_back(BuildRtpPacket(kSsrc1));
197 return packets;
Erik Språng4208a132019-08-26 08:58:45 +0200198 });
199 packet_router_.GeneratePadding(kPaddingBytes);
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200200
201 // Send media on second module. Padding should be sent there.
Erik Språng4208a132019-08-26 08:58:45 +0200202 packet_router_.SendPacket(BuildRtpPacket(kSsrc2), PacedPacketInfo());
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200203
Erik Språngc06aef22019-10-17 13:02:27 +0200204 // If the last active module is removed, and no module sends media before
205 // the next padding request, and arbitrary module will be selected.
Erik Språngfbe84ef2019-10-17 11:17:06 +0000206 packet_router_.RemoveSendRtpModule(&rtp_2);
Erik Språngc06aef22019-10-17 13:02:27 +0200207
208 // Send on and then remove all remaining modules.
209 RtpRtcp* last_send_module;
Erik Språng4208a132019-08-26 08:58:45 +0200210 EXPECT_CALL(rtp_1, GeneratePadding(kPaddingBytes))
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200211 .Times(1)
Erik Språngc06aef22019-10-17 13:02:27 +0200212 .WillOnce([&](size_t target_size_bytes) {
213 last_send_module = &rtp_1;
214 std::vector<std::unique_ptr<RtpPacketToSend>> packets;
215 packets.push_back(BuildRtpPacket(kSsrc1));
216 return packets;
Erik Språng4208a132019-08-26 08:58:45 +0200217 });
Erik Språng4208a132019-08-26 08:58:45 +0200218 EXPECT_CALL(rtp_3, GeneratePadding(kPaddingBytes))
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200219 .Times(1)
Erik Språngc06aef22019-10-17 13:02:27 +0200220 .WillOnce([&](size_t target_size_bytes) {
221 last_send_module = &rtp_3;
222 std::vector<std::unique_ptr<RtpPacketToSend>> packets;
223 packets.push_back(BuildRtpPacket(kSsrc3));
224 return packets;
Erik Språng4208a132019-08-26 08:58:45 +0200225 });
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200226
Erik Språngc06aef22019-10-17 13:02:27 +0200227 for (int i = 0; i < 2; ++i) {
228 last_send_module = nullptr;
229 packet_router_.GeneratePadding(kPaddingBytes);
230 EXPECT_NE(last_send_module, nullptr);
231 packet_router_.RemoveSendRtpModule(last_send_module);
232 }
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200233}
234
Erik Språng5f01bf62019-10-16 17:06:34 +0200235TEST_F(PacketRouterTest, AllocatesTransportSequenceNumbers) {
sprang867fb522015-08-03 04:38:41 -0700236 const uint16_t kStartSeq = 0xFFF0;
237 const size_t kNumPackets = 32;
Erik Språng5f01bf62019-10-16 17:06:34 +0200238 const uint16_t kSsrc1 = 1234;
sprang867fb522015-08-03 04:38:41 -0700239
Erik Språng5f01bf62019-10-16 17:06:34 +0200240 PacketRouter packet_router(kStartSeq - 1);
241 NiceMock<MockRtpRtcp> rtp_1;
242 EXPECT_CALL(rtp_1, SSRC()).WillRepeatedly(Return(kSsrc1));
243 EXPECT_CALL(rtp_1, TrySendPacket).WillRepeatedly(Return(true));
244 packet_router.AddSendRtpModule(&rtp_1, false);
sprang867fb522015-08-03 04:38:41 -0700245
246 for (size_t i = 0; i < kNumPackets; ++i) {
Erik Språng5f01bf62019-10-16 17:06:34 +0200247 auto packet = BuildRtpPacket(kSsrc1);
248 EXPECT_TRUE(packet->ReserveExtension<TransportSequenceNumber>());
249 packet_router.SendPacket(std::move(packet), PacedPacketInfo());
sprang867fb522015-08-03 04:38:41 -0700250 uint32_t expected_unwrapped_seq = static_cast<uint32_t>(kStartSeq) + i;
Erik Språng5f01bf62019-10-16 17:06:34 +0200251 EXPECT_EQ(static_cast<uint16_t>(expected_unwrapped_seq & 0xFFFF),
252 packet_router.CurrentTransportSequenceNumber());
sprang867fb522015-08-03 04:38:41 -0700253 }
Erik Språng5f01bf62019-10-16 17:06:34 +0200254
255 packet_router.RemoveSendRtpModule(&rtp_1);
sprang867fb522015-08-03 04:38:41 -0700256}
stefanbba9dec2016-02-01 04:39:55 -0800257
Erik Språng4208a132019-08-26 08:58:45 +0200258TEST_F(PacketRouterTest, SendTransportFeedback) {
eladalon6c9556e2017-07-10 03:33:00 -0700259 NiceMock<MockRtpRtcp> rtp_1;
260 NiceMock<MockRtpRtcp> rtp_2;
eladalonb1338fe2017-08-01 09:36:19 -0700261
Per Kjellanderee153c92019-10-10 16:43:46 +0200262 ON_CALL(rtp_1, RTCP()).WillByDefault(Return(RtcpMode::kCompound));
263 ON_CALL(rtp_2, RTCP()).WillByDefault(Return(RtcpMode::kCompound));
264
Erik Språng4208a132019-08-26 08:58:45 +0200265 packet_router_.AddSendRtpModule(&rtp_1, false);
266 packet_router_.AddReceiveRtpModule(&rtp_2, false);
stefanbba9dec2016-02-01 04:39:55 -0800267
Per Kjellanderee153c92019-10-10 16:43:46 +0200268 std::vector<std::unique_ptr<rtcp::RtcpPacket>> feedback;
269 feedback.push_back(std::make_unique<rtcp::TransportFeedback>());
270 EXPECT_CALL(rtp_1, SendCombinedRtcpPacket).Times(1);
271 packet_router_.SendCombinedRtcpPacket(std::move(feedback));
Erik Språng4208a132019-08-26 08:58:45 +0200272 packet_router_.RemoveSendRtpModule(&rtp_1);
Per Kjellanderee153c92019-10-10 16:43:46 +0200273 EXPECT_CALL(rtp_2, SendCombinedRtcpPacket).Times(1);
274 std::vector<std::unique_ptr<rtcp::RtcpPacket>> new_feedback;
275 new_feedback.push_back(std::make_unique<rtcp::TransportFeedback>());
276 packet_router_.SendCombinedRtcpPacket(std::move(new_feedback));
Erik Språng4208a132019-08-26 08:58:45 +0200277 packet_router_.RemoveReceiveRtpModule(&rtp_2);
278}
279
280TEST_F(PacketRouterTest, SendPacketWithoutTransportSequenceNumbers) {
Erik Språngfbe84ef2019-10-17 11:17:06 +0000281 const uint16_t kSsrc1 = 1234;
Erik Språngc06aef22019-10-17 13:02:27 +0200282 NiceMock<MockRtpRtcp> rtp_1;
Erik Språng4208a132019-08-26 08:58:45 +0200283 ON_CALL(rtp_1, SendingMedia).WillByDefault(Return(true));
284 ON_CALL(rtp_1, SSRC).WillByDefault(Return(kSsrc1));
Erik Språngc06aef22019-10-17 13:02:27 +0200285 packet_router_.AddSendRtpModule(&rtp_1, false);
Erik Språng4208a132019-08-26 08:58:45 +0200286
287 // Send a packet without TransportSequenceNumber extension registered,
288 // packets sent should not have the extension set.
289 RtpHeaderExtensionMap extension_manager;
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200290 auto packet = std::make_unique<RtpPacketToSend>(&extension_manager);
Erik Språng4208a132019-08-26 08:58:45 +0200291 packet->SetSsrc(kSsrc1);
292 EXPECT_CALL(
293 rtp_1,
294 TrySendPacket(
295 Property(&RtpPacketToSend::HasExtension<TransportSequenceNumber>,
296 false),
297 _))
298 .WillOnce(Return(true));
299 packet_router_.SendPacket(std::move(packet), PacedPacketInfo());
300
301 packet_router_.RemoveSendRtpModule(&rtp_1);
302}
303
304TEST_F(PacketRouterTest, SendPacketAssignsTransportSequenceNumbers) {
305 NiceMock<MockRtpRtcp> rtp_1;
306 NiceMock<MockRtpRtcp> rtp_2;
307
Erik Språng4208a132019-08-26 08:58:45 +0200308 const uint16_t kSsrc1 = 1234;
309 const uint16_t kSsrc2 = 2345;
310
311 ON_CALL(rtp_1, SSRC).WillByDefault(Return(kSsrc1));
312 ON_CALL(rtp_2, SSRC).WillByDefault(Return(kSsrc2));
313
Erik Språngc06aef22019-10-17 13:02:27 +0200314 packet_router_.AddSendRtpModule(&rtp_1, false);
315 packet_router_.AddSendRtpModule(&rtp_2, false);
316
Erik Språng4208a132019-08-26 08:58:45 +0200317 // Transport sequence numbers start at 1, for historical reasons.
318 uint16_t transport_sequence_number = 1;
319
320 auto packet = BuildRtpPacket(kSsrc1);
321 EXPECT_TRUE(packet->ReserveExtension<TransportSequenceNumber>());
322 EXPECT_CALL(
323 rtp_1,
324 TrySendPacket(
325 Property(&RtpPacketToSend::GetExtension<TransportSequenceNumber>,
326 transport_sequence_number),
327 _))
328 .WillOnce(Return(true));
329 packet_router_.SendPacket(std::move(packet), PacedPacketInfo());
330
331 ++transport_sequence_number;
332 packet = BuildRtpPacket(kSsrc2);
333 EXPECT_TRUE(packet->ReserveExtension<TransportSequenceNumber>());
334
Erik Språng4208a132019-08-26 08:58:45 +0200335 EXPECT_CALL(
336 rtp_2,
337 TrySendPacket(
338 Property(&RtpPacketToSend::GetExtension<TransportSequenceNumber>,
339 transport_sequence_number),
340 _))
341 .WillOnce(Return(true));
342 packet_router_.SendPacket(std::move(packet), PacedPacketInfo());
343
344 packet_router_.RemoveSendRtpModule(&rtp_1);
345 packet_router_.RemoveSendRtpModule(&rtp_2);
stefanbba9dec2016-02-01 04:39:55 -0800346}
nisse05843312017-04-18 23:38:35 -0700347
eladalon822ff2b2017-08-01 06:30:28 -0700348#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
Erik Språng4208a132019-08-26 08:58:45 +0200349TEST_F(PacketRouterTest, DoubleRegistrationOfSendModuleDisallowed) {
eladalon822ff2b2017-08-01 06:30:28 -0700350 NiceMock<MockRtpRtcp> module;
351
352 constexpr bool remb_candidate = false; // Value irrelevant.
Erik Språng4208a132019-08-26 08:58:45 +0200353 packet_router_.AddSendRtpModule(&module, remb_candidate);
354 EXPECT_DEATH(packet_router_.AddSendRtpModule(&module, remb_candidate), "");
eladalon822ff2b2017-08-01 06:30:28 -0700355
356 // Test tear-down
Erik Språng4208a132019-08-26 08:58:45 +0200357 packet_router_.RemoveSendRtpModule(&module);
eladalon822ff2b2017-08-01 06:30:28 -0700358}
359
Erik Språng4208a132019-08-26 08:58:45 +0200360TEST_F(PacketRouterTest, DoubleRegistrationOfReceiveModuleDisallowed) {
eladalon822ff2b2017-08-01 06:30:28 -0700361 NiceMock<MockRtpRtcp> module;
362
363 constexpr bool remb_candidate = false; // Value irrelevant.
Erik Språng4208a132019-08-26 08:58:45 +0200364 packet_router_.AddReceiveRtpModule(&module, remb_candidate);
365 EXPECT_DEATH(packet_router_.AddReceiveRtpModule(&module, remb_candidate), "");
eladalon822ff2b2017-08-01 06:30:28 -0700366
367 // Test tear-down
Erik Språng4208a132019-08-26 08:58:45 +0200368 packet_router_.RemoveReceiveRtpModule(&module);
eladalon822ff2b2017-08-01 06:30:28 -0700369}
370
Erik Språng4208a132019-08-26 08:58:45 +0200371TEST_F(PacketRouterTest, RemovalOfNeverAddedSendModuleDisallowed) {
eladalon822ff2b2017-08-01 06:30:28 -0700372 NiceMock<MockRtpRtcp> module;
373
Erik Språng4208a132019-08-26 08:58:45 +0200374 EXPECT_DEATH(packet_router_.RemoveSendRtpModule(&module), "");
eladalon822ff2b2017-08-01 06:30:28 -0700375}
376
Erik Språng4208a132019-08-26 08:58:45 +0200377TEST_F(PacketRouterTest, RemovalOfNeverAddedReceiveModuleDisallowed) {
eladalon822ff2b2017-08-01 06:30:28 -0700378 NiceMock<MockRtpRtcp> module;
379
Erik Språng4208a132019-08-26 08:58:45 +0200380 EXPECT_DEATH(packet_router_.RemoveReceiveRtpModule(&module), "");
eladalon822ff2b2017-08-01 06:30:28 -0700381}
382#endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
383
nisse05843312017-04-18 23:38:35 -0700384TEST(PacketRouterRembTest, LowerEstimateToSendRemb) {
385 rtc::ScopedFakeClock clock;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200386 NiceMock<MockRtpRtcp> rtp;
nisse05843312017-04-18 23:38:35 -0700387 PacketRouter packet_router;
388
eladalon822ff2b2017-08-01 06:30:28 -0700389 packet_router.AddSendRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700390
391 uint32_t bitrate_estimate = 456;
392 const std::vector<uint32_t> ssrcs = {1234};
393
nisse05843312017-04-18 23:38:35 -0700394 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
395
396 // Call OnReceiveBitrateChanged twice to get a first estimate.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200397 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200398 EXPECT_CALL(rtp, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700399 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
400
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200401 // Lower the estimate with more than 3% to trigger a call to SetRemb right
nisse05843312017-04-18 23:38:35 -0700402 // away.
403 bitrate_estimate = bitrate_estimate - 100;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200404 EXPECT_CALL(rtp, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700405 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
406
nisse05843312017-04-18 23:38:35 -0700407 packet_router.RemoveSendRtpModule(&rtp);
408}
409
410TEST(PacketRouterRembTest, VerifyIncreasingAndDecreasing) {
411 rtc::ScopedFakeClock clock;
eladalon6c9556e2017-07-10 03:33:00 -0700412 NiceMock<MockRtpRtcp> rtp;
nisse05843312017-04-18 23:38:35 -0700413 PacketRouter packet_router;
eladalon822ff2b2017-08-01 06:30:28 -0700414 packet_router.AddSendRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700415
416 uint32_t bitrate_estimate[] = {456, 789};
417 std::vector<uint32_t> ssrcs = {1234, 5678};
418
nisse05843312017-04-18 23:38:35 -0700419 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate[0]);
420
421 // Call OnReceiveBitrateChanged twice to get a first estimate.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200422 EXPECT_CALL(rtp, SetRemb(bitrate_estimate[0], ssrcs)).Times(1);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200423 clock.AdvanceTime(TimeDelta::ms(1000));
nisse05843312017-04-18 23:38:35 -0700424 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate[0]);
425
426 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate[1] + 100);
427
428 // Lower the estimate to trigger a callback.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200429 EXPECT_CALL(rtp, SetRemb(bitrate_estimate[1], ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700430 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate[1]);
431
432 packet_router.RemoveSendRtpModule(&rtp);
433}
434
435TEST(PacketRouterRembTest, NoRembForIncreasedBitrate) {
436 rtc::ScopedFakeClock clock;
eladalon6c9556e2017-07-10 03:33:00 -0700437 NiceMock<MockRtpRtcp> rtp;
nisse05843312017-04-18 23:38:35 -0700438 PacketRouter packet_router;
eladalon822ff2b2017-08-01 06:30:28 -0700439 packet_router.AddSendRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700440
441 uint32_t bitrate_estimate = 456;
442 std::vector<uint32_t> ssrcs = {1234, 5678};
443
nisse05843312017-04-18 23:38:35 -0700444 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
445
446 // Call OnReceiveBitrateChanged twice to get a first estimate.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200447 EXPECT_CALL(rtp, SetRemb(bitrate_estimate, ssrcs)).Times(1);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200448 clock.AdvanceTime(TimeDelta::ms(1000));
nisse05843312017-04-18 23:38:35 -0700449 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
450
451 // Increased estimate shouldn't trigger a callback right away.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200452 EXPECT_CALL(rtp, SetRemb(_, _)).Times(0);
nisse05843312017-04-18 23:38:35 -0700453 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate + 1);
454
455 // Decreasing the estimate less than 3% shouldn't trigger a new callback.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200456 EXPECT_CALL(rtp, SetRemb(_, _)).Times(0);
nisse05843312017-04-18 23:38:35 -0700457 int lower_estimate = bitrate_estimate * 98 / 100;
458 packet_router.OnReceiveBitrateChanged(ssrcs, lower_estimate);
459
460 packet_router.RemoveSendRtpModule(&rtp);
461}
462
463TEST(PacketRouterRembTest, ChangeSendRtpModule) {
464 rtc::ScopedFakeClock clock;
eladalon6c9556e2017-07-10 03:33:00 -0700465 NiceMock<MockRtpRtcp> rtp_send;
466 NiceMock<MockRtpRtcp> rtp_recv;
nisse05843312017-04-18 23:38:35 -0700467 PacketRouter packet_router;
eladalon822ff2b2017-08-01 06:30:28 -0700468 packet_router.AddSendRtpModule(&rtp_send, true);
469 packet_router.AddReceiveRtpModule(&rtp_recv, true);
nisse05843312017-04-18 23:38:35 -0700470
471 uint32_t bitrate_estimate = 456;
472 std::vector<uint32_t> ssrcs = {1234, 5678};
473
nisse05843312017-04-18 23:38:35 -0700474 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
475
476 // Call OnReceiveBitrateChanged twice to get a first estimate.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200477 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200478 EXPECT_CALL(rtp_send, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700479 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
480
481 // Decrease estimate to trigger a REMB.
482 bitrate_estimate = bitrate_estimate - 100;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200483 EXPECT_CALL(rtp_send, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700484 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
485
486 // Remove the sending module -> should get remb on the second module.
487 packet_router.RemoveSendRtpModule(&rtp_send);
488
nisse05843312017-04-18 23:38:35 -0700489 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
490
491 bitrate_estimate = bitrate_estimate - 100;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200492 EXPECT_CALL(rtp_recv, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700493 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
494
495 packet_router.RemoveReceiveRtpModule(&rtp_recv);
496}
497
498TEST(PacketRouterRembTest, OnlyOneRembForRepeatedOnReceiveBitrateChanged) {
499 rtc::ScopedFakeClock clock;
eladalon6c9556e2017-07-10 03:33:00 -0700500 NiceMock<MockRtpRtcp> rtp;
nisse05843312017-04-18 23:38:35 -0700501 PacketRouter packet_router;
eladalon822ff2b2017-08-01 06:30:28 -0700502 packet_router.AddSendRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700503
504 uint32_t bitrate_estimate = 456;
505 const std::vector<uint32_t> ssrcs = {1234};
506
nisse05843312017-04-18 23:38:35 -0700507 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
508
509 // Call OnReceiveBitrateChanged twice to get a first estimate.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200510 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200511 EXPECT_CALL(rtp, SetRemb(_, _)).Times(1);
nisse05843312017-04-18 23:38:35 -0700512 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
513
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200514 // Lower the estimate, should trigger a call to SetRemb right away.
nisse05843312017-04-18 23:38:35 -0700515 bitrate_estimate = bitrate_estimate - 100;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200516 EXPECT_CALL(rtp, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700517 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
518
519 // Call OnReceiveBitrateChanged again, this should not trigger a new callback.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200520 EXPECT_CALL(rtp, SetRemb(_, _)).Times(0);
nisse05843312017-04-18 23:38:35 -0700521 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
522 packet_router.RemoveSendRtpModule(&rtp);
523}
524
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200525TEST(PacketRouterRembTest, SetMaxDesiredReceiveBitrateLimitsSetRemb) {
danilchap47085372017-08-10 06:03:57 -0700526 rtc::ScopedFakeClock clock;
527 PacketRouter packet_router;
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200528 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200529 NiceMock<MockRtpRtcp> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700530 constexpr bool remb_candidate = true;
531 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700532
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100533 const int64_t cap_bitrate = 100000;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200534 EXPECT_CALL(remb_sender, SetRemb(Le(cap_bitrate), _)).Times(AtLeast(1));
535 EXPECT_CALL(remb_sender, SetRemb(Gt(cap_bitrate), _)).Times(0);
danilchap47085372017-08-10 06:03:57 -0700536
537 const std::vector<uint32_t> ssrcs = {1234};
538 packet_router.SetMaxDesiredReceiveBitrate(cap_bitrate);
539 packet_router.OnReceiveBitrateChanged(ssrcs, cap_bitrate + 5000);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200540 clock.AdvanceTime(TimeDelta::ms(1000));
danilchap47085372017-08-10 06:03:57 -0700541 packet_router.OnReceiveBitrateChanged(ssrcs, cap_bitrate - 5000);
542
543 // Test tear-down.
544 packet_router.RemoveSendRtpModule(&remb_sender);
545}
546
547TEST(PacketRouterRembTest,
548 SetMaxDesiredReceiveBitrateTriggersRembWhenMoreRestrictive) {
549 rtc::ScopedFakeClock clock;
550 PacketRouter packet_router;
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200551 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200552 NiceMock<MockRtpRtcp> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700553 constexpr bool remb_candidate = true;
554 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700555
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100556 const int64_t measured_bitrate_bps = 150000;
557 const int64_t cap_bitrate_bps = measured_bitrate_bps - 5000;
danilchap47085372017-08-10 06:03:57 -0700558 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200559 EXPECT_CALL(remb_sender, SetRemb(measured_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700560 packet_router.OnReceiveBitrateChanged(ssrcs, measured_bitrate_bps);
561
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200562 EXPECT_CALL(remb_sender, SetRemb(cap_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700563 packet_router.SetMaxDesiredReceiveBitrate(cap_bitrate_bps);
564
565 // Test tear-down.
566 packet_router.RemoveSendRtpModule(&remb_sender);
567}
568
569TEST(PacketRouterRembTest,
570 SetMaxDesiredReceiveBitrateDoesNotTriggerRembWhenAsRestrictive) {
571 rtc::ScopedFakeClock clock;
572 PacketRouter packet_router;
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200573 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200574 NiceMock<MockRtpRtcp> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700575 constexpr bool remb_candidate = true;
576 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700577
578 const uint32_t measured_bitrate_bps = 150000;
579 const uint32_t cap_bitrate_bps = measured_bitrate_bps;
580 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200581 EXPECT_CALL(remb_sender, SetRemb(measured_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700582 packet_router.OnReceiveBitrateChanged(ssrcs, measured_bitrate_bps);
583
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200584 EXPECT_CALL(remb_sender, SetRemb(_, _)).Times(0);
danilchap47085372017-08-10 06:03:57 -0700585 packet_router.SetMaxDesiredReceiveBitrate(cap_bitrate_bps);
586
587 // Test tear-down.
588 packet_router.RemoveSendRtpModule(&remb_sender);
589}
590
591TEST(PacketRouterRembTest,
592 SetMaxDesiredReceiveBitrateDoesNotTriggerRembWhenLessRestrictive) {
593 rtc::ScopedFakeClock clock;
594 PacketRouter packet_router;
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200595 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200596 NiceMock<MockRtpRtcp> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700597 constexpr bool remb_candidate = true;
598 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700599
600 const uint32_t measured_bitrate_bps = 150000;
601 const uint32_t cap_bitrate_bps = measured_bitrate_bps + 500;
602 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200603 EXPECT_CALL(remb_sender, SetRemb(measured_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700604 packet_router.OnReceiveBitrateChanged(ssrcs, measured_bitrate_bps);
605
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200606 EXPECT_CALL(remb_sender, SetRemb(_, _)).Times(0);
danilchap47085372017-08-10 06:03:57 -0700607 packet_router.SetMaxDesiredReceiveBitrate(cap_bitrate_bps);
608
609 // Test tear-down.
610 packet_router.RemoveSendRtpModule(&remb_sender);
611}
612
613TEST(PacketRouterRembTest,
614 SetMaxDesiredReceiveBitrateTriggersRembWhenNoRecentMeasure) {
615 rtc::ScopedFakeClock clock;
616 PacketRouter packet_router;
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200617 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200618 NiceMock<MockRtpRtcp> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700619 constexpr bool remb_candidate = true;
620 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700621
622 const uint32_t measured_bitrate_bps = 150000;
623 const uint32_t cap_bitrate_bps = measured_bitrate_bps + 5000;
624 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200625 EXPECT_CALL(remb_sender, SetRemb(measured_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700626 packet_router.OnReceiveBitrateChanged(ssrcs, measured_bitrate_bps);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200627 clock.AdvanceTime(TimeDelta::ms(1000));
danilchap47085372017-08-10 06:03:57 -0700628
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200629 EXPECT_CALL(remb_sender, SetRemb(cap_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700630 packet_router.SetMaxDesiredReceiveBitrate(cap_bitrate_bps);
631
632 // Test tear-down.
633 packet_router.RemoveSendRtpModule(&remb_sender);
634}
635
636TEST(PacketRouterRembTest,
637 SetMaxDesiredReceiveBitrateTriggersRembWhenNoMeasures) {
638 rtc::ScopedFakeClock clock;
639 PacketRouter packet_router;
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200640 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200641 NiceMock<MockRtpRtcp> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700642 constexpr bool remb_candidate = true;
643 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700644
645 // Set cap.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200646 EXPECT_CALL(remb_sender, SetRemb(100000, _)).Times(1);
danilchap47085372017-08-10 06:03:57 -0700647 packet_router.SetMaxDesiredReceiveBitrate(100000);
648 // Increase cap.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200649 EXPECT_CALL(remb_sender, SetRemb(200000, _)).Times(1);
danilchap47085372017-08-10 06:03:57 -0700650 packet_router.SetMaxDesiredReceiveBitrate(200000);
651 // Decrease cap.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200652 EXPECT_CALL(remb_sender, SetRemb(150000, _)).Times(1);
danilchap47085372017-08-10 06:03:57 -0700653 packet_router.SetMaxDesiredReceiveBitrate(150000);
654
655 // Test tear-down.
656 packet_router.RemoveSendRtpModule(&remb_sender);
657}
658
nisse05843312017-04-18 23:38:35 -0700659// Only register receiving modules and make sure we fallback to trigger a REMB
660// packet on this one.
661TEST(PacketRouterRembTest, NoSendingRtpModule) {
662 rtc::ScopedFakeClock clock;
eladalon6c9556e2017-07-10 03:33:00 -0700663 NiceMock<MockRtpRtcp> rtp;
nisse05843312017-04-18 23:38:35 -0700664 PacketRouter packet_router;
665
eladalon822ff2b2017-08-01 06:30:28 -0700666 packet_router.AddReceiveRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700667
668 uint32_t bitrate_estimate = 456;
669 const std::vector<uint32_t> ssrcs = {1234};
670
nisse05843312017-04-18 23:38:35 -0700671 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
672
673 // Call OnReceiveBitrateChanged twice to get a first estimate.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200674 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200675 EXPECT_CALL(rtp, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700676 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
677
678 // Lower the estimate to trigger a new packet REMB packet.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200679 EXPECT_CALL(rtp, SetRemb(bitrate_estimate - 100, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700680 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate - 100);
681
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200682 EXPECT_CALL(rtp, UnsetRemb()).Times(1);
nisse05843312017-04-18 23:38:35 -0700683 packet_router.RemoveReceiveRtpModule(&rtp);
684}
685
eladalon822ff2b2017-08-01 06:30:28 -0700686TEST(PacketRouterRembTest, NonCandidateSendRtpModuleNotUsedForRemb) {
687 rtc::ScopedFakeClock clock;
688 PacketRouter packet_router;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200689 NiceMock<MockRtpRtcp> module;
eladalon822ff2b2017-08-01 06:30:28 -0700690
691 constexpr bool remb_candidate = false;
692
693 packet_router.AddSendRtpModule(&module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700694
695 constexpr uint32_t bitrate_estimate = 456;
696 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200697 EXPECT_CALL(module, SetRemb(_, _)).Times(0);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200698 clock.AdvanceTime(TimeDelta::ms(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700699 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
700
701 // Test tear-down
702 packet_router.RemoveSendRtpModule(&module);
703}
704
705TEST(PacketRouterRembTest, CandidateSendRtpModuleUsedForRemb) {
706 rtc::ScopedFakeClock clock;
707 PacketRouter packet_router;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200708 NiceMock<MockRtpRtcp> module;
eladalon822ff2b2017-08-01 06:30:28 -0700709
710 constexpr bool remb_candidate = true;
711
712 packet_router.AddSendRtpModule(&module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700713
714 constexpr uint32_t bitrate_estimate = 456;
715 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200716 EXPECT_CALL(module, SetRemb(bitrate_estimate, ssrcs)).Times(1);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200717 clock.AdvanceTime(TimeDelta::ms(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700718 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
719
720 // Test tear-down
721 packet_router.RemoveSendRtpModule(&module);
722}
723
724TEST(PacketRouterRembTest, NonCandidateReceiveRtpModuleNotUsedForRemb) {
725 rtc::ScopedFakeClock clock;
726 PacketRouter packet_router;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200727 NiceMock<MockRtpRtcp> module;
eladalon822ff2b2017-08-01 06:30:28 -0700728
729 constexpr bool remb_candidate = false;
730
731 packet_router.AddReceiveRtpModule(&module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700732
733 constexpr uint32_t bitrate_estimate = 456;
734 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200735 EXPECT_CALL(module, SetRemb(_, _)).Times(0);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200736 clock.AdvanceTime(TimeDelta::ms(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700737 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
738
739 // Test tear-down
740 packet_router.RemoveReceiveRtpModule(&module);
741}
742
743TEST(PacketRouterRembTest, CandidateReceiveRtpModuleUsedForRemb) {
744 rtc::ScopedFakeClock clock;
745 PacketRouter packet_router;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200746 NiceMock<MockRtpRtcp> module;
eladalon822ff2b2017-08-01 06:30:28 -0700747
748 constexpr bool remb_candidate = true;
749
750 packet_router.AddReceiveRtpModule(&module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700751
752 constexpr uint32_t bitrate_estimate = 456;
753 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200754 EXPECT_CALL(module, SetRemb(bitrate_estimate, ssrcs)).Times(1);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200755 clock.AdvanceTime(TimeDelta::ms(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700756 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
757
758 // Test tear-down
759 packet_router.RemoveReceiveRtpModule(&module);
760}
761
762TEST(PacketRouterRembTest,
763 SendCandidatePreferredOverReceiveCandidate_SendModuleAddedFirst) {
764 rtc::ScopedFakeClock clock;
765 PacketRouter packet_router;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200766 NiceMock<MockRtpRtcp> send_module;
767 NiceMock<MockRtpRtcp> receive_module;
eladalon822ff2b2017-08-01 06:30:28 -0700768
769 constexpr bool remb_candidate = true;
770
771 // Send module added - activated.
772 packet_router.AddSendRtpModule(&send_module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700773
774 // Receive module added - the send module remains the active one.
775 packet_router.AddReceiveRtpModule(&receive_module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700776
777 constexpr uint32_t bitrate_estimate = 456;
778 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200779 EXPECT_CALL(send_module, SetRemb(bitrate_estimate, ssrcs)).Times(1);
780 EXPECT_CALL(receive_module, SetRemb(_, _)).Times(0);
eladalon822ff2b2017-08-01 06:30:28 -0700781
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200782 clock.AdvanceTime(TimeDelta::ms(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700783 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
784
785 // Test tear-down
786 packet_router.RemoveReceiveRtpModule(&receive_module);
787 packet_router.RemoveSendRtpModule(&send_module);
788}
789
790TEST(PacketRouterRembTest,
791 SendCandidatePreferredOverReceiveCandidate_ReceiveModuleAddedFirst) {
792 rtc::ScopedFakeClock clock;
793 PacketRouter packet_router;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200794 NiceMock<MockRtpRtcp> send_module;
795 NiceMock<MockRtpRtcp> receive_module;
eladalon822ff2b2017-08-01 06:30:28 -0700796
797 constexpr bool remb_candidate = true;
798
799 // Receive module added - activated.
800 packet_router.AddReceiveRtpModule(&receive_module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700801
802 // Send module added - replaces receive module as active.
803 packet_router.AddSendRtpModule(&send_module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700804
805 constexpr uint32_t bitrate_estimate = 456;
806 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200807 EXPECT_CALL(send_module, SetRemb(bitrate_estimate, ssrcs)).Times(1);
808 EXPECT_CALL(receive_module, SetRemb(_, _)).Times(0);
eladalon822ff2b2017-08-01 06:30:28 -0700809
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200810 clock.AdvanceTime(TimeDelta::ms(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700811 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
812
813 // Test tear-down
814 packet_router.RemoveReceiveRtpModule(&receive_module);
815 packet_router.RemoveSendRtpModule(&send_module);
816}
817
818TEST(PacketRouterRembTest, ReceiveModuleTakesOverWhenLastSendModuleRemoved) {
819 rtc::ScopedFakeClock clock;
820 PacketRouter packet_router;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200821 NiceMock<MockRtpRtcp> send_module;
822 NiceMock<MockRtpRtcp> receive_module;
eladalon822ff2b2017-08-01 06:30:28 -0700823
824 constexpr bool remb_candidate = true;
825
826 // Send module active, receive module inactive.
827 packet_router.AddSendRtpModule(&send_module, remb_candidate);
828 packet_router.AddReceiveRtpModule(&receive_module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700829
830 // Send module removed - receive module becomes active.
831 packet_router.RemoveSendRtpModule(&send_module);
eladalon822ff2b2017-08-01 06:30:28 -0700832 constexpr uint32_t bitrate_estimate = 456;
833 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200834 EXPECT_CALL(send_module, SetRemb(_, _)).Times(0);
835 EXPECT_CALL(receive_module, SetRemb(bitrate_estimate, ssrcs)).Times(1);
eladalon822ff2b2017-08-01 06:30:28 -0700836
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200837 clock.AdvanceTime(TimeDelta::ms(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700838 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
839
840 // Test tear-down
841 packet_router.RemoveReceiveRtpModule(&receive_module);
842}
843
Stefan Holmere5904162015-03-26 11:11:06 +0100844} // namespace webrtc