blob: 1239201a6c5aafcc10033a5e9dd50913e4b60dd2 [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ångfbe84ef2019-10-17 11:17:06 +0000182 .WillOnce([](size_t target_size_bytes) {
183 return std::vector<std::unique_ptr<RtpPacketToSend>>();
Erik Språng4208a132019-08-26 08:58:45 +0200184 });
185 packet_router_.GeneratePadding(kPaddingBytes);
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200186
187 // Send media on first module. Padding should be sent on that module.
Erik Språng4208a132019-08-26 08:58:45 +0200188 packet_router_.SendPacket(BuildRtpPacket(kSsrc1), PacedPacketInfo());
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200189
Erik Språng4208a132019-08-26 08:58:45 +0200190 EXPECT_CALL(rtp_1, GeneratePadding(kPaddingBytes))
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200191 .Times(1)
Erik Språngfbe84ef2019-10-17 11:17:06 +0000192 .WillOnce([](size_t target_size_bytes) {
193 return std::vector<std::unique_ptr<RtpPacketToSend>>();
Erik Språng4208a132019-08-26 08:58:45 +0200194 });
195 packet_router_.GeneratePadding(kPaddingBytes);
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200196
197 // Send media on second module. Padding should be sent there.
Erik Språng4208a132019-08-26 08:58:45 +0200198 packet_router_.SendPacket(BuildRtpPacket(kSsrc2), PacedPacketInfo());
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200199
Erik Språngfbe84ef2019-10-17 11:17:06 +0000200 EXPECT_CALL(rtp_2, GeneratePadding(kPaddingBytes))
201 .Times(1)
202 .WillOnce([](size_t target_size_bytes) {
203 return std::vector<std::unique_ptr<RtpPacketToSend>>();
204 });
205 packet_router_.GeneratePadding(kPaddingBytes);
Erik Språng96f3de02019-10-17 13:02:27 +0200206
Erik Språngfbe84ef2019-10-17 11:17:06 +0000207 // Remove second module, padding should now fall back to first module.
208 packet_router_.RemoveSendRtpModule(&rtp_2);
Erik Språng4208a132019-08-26 08:58:45 +0200209 EXPECT_CALL(rtp_1, GeneratePadding(kPaddingBytes))
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200210 .Times(1)
Erik Språngfbe84ef2019-10-17 11:17:06 +0000211 .WillOnce([](size_t target_size_bytes) {
212 return std::vector<std::unique_ptr<RtpPacketToSend>>();
Erik Språng4208a132019-08-26 08:58:45 +0200213 });
Erik Språngfbe84ef2019-10-17 11:17:06 +0000214 packet_router_.GeneratePadding(kPaddingBytes);
215
216 // Remove first module too, leaving only the one without rtx.
217 packet_router_.RemoveSendRtpModule(&rtp_1);
218
Erik Språng4208a132019-08-26 08:58:45 +0200219 EXPECT_CALL(rtp_3, GeneratePadding(kPaddingBytes))
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200220 .Times(1)
Erik Språngfbe84ef2019-10-17 11:17:06 +0000221 .WillOnce([](size_t target_size_bytes) {
222 return std::vector<std::unique_ptr<RtpPacketToSend>>();
Erik Språng4208a132019-08-26 08:58:45 +0200223 });
Erik Språngfbe84ef2019-10-17 11:17:06 +0000224 packet_router_.GeneratePadding(kPaddingBytes);
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200225
Erik Språngfbe84ef2019-10-17 11:17:06 +0000226 packet_router_.RemoveSendRtpModule(&rtp_3);
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200227}
228
Erik Språng5f01bf62019-10-16 17:06:34 +0200229TEST_F(PacketRouterTest, AllocatesTransportSequenceNumbers) {
sprang867fb522015-08-03 04:38:41 -0700230 const uint16_t kStartSeq = 0xFFF0;
231 const size_t kNumPackets = 32;
Erik Språng5f01bf62019-10-16 17:06:34 +0200232 const uint16_t kSsrc1 = 1234;
sprang867fb522015-08-03 04:38:41 -0700233
Erik Språng5f01bf62019-10-16 17:06:34 +0200234 PacketRouter packet_router(kStartSeq - 1);
235 NiceMock<MockRtpRtcp> rtp_1;
236 EXPECT_CALL(rtp_1, SSRC()).WillRepeatedly(Return(kSsrc1));
237 EXPECT_CALL(rtp_1, TrySendPacket).WillRepeatedly(Return(true));
238 packet_router.AddSendRtpModule(&rtp_1, false);
sprang867fb522015-08-03 04:38:41 -0700239
240 for (size_t i = 0; i < kNumPackets; ++i) {
Erik Språng5f01bf62019-10-16 17:06:34 +0200241 auto packet = BuildRtpPacket(kSsrc1);
242 EXPECT_TRUE(packet->ReserveExtension<TransportSequenceNumber>());
243 packet_router.SendPacket(std::move(packet), PacedPacketInfo());
sprang867fb522015-08-03 04:38:41 -0700244 uint32_t expected_unwrapped_seq = static_cast<uint32_t>(kStartSeq) + i;
Erik Språng5f01bf62019-10-16 17:06:34 +0200245 EXPECT_EQ(static_cast<uint16_t>(expected_unwrapped_seq & 0xFFFF),
246 packet_router.CurrentTransportSequenceNumber());
sprang867fb522015-08-03 04:38:41 -0700247 }
Erik Språng5f01bf62019-10-16 17:06:34 +0200248
249 packet_router.RemoveSendRtpModule(&rtp_1);
sprang867fb522015-08-03 04:38:41 -0700250}
stefanbba9dec2016-02-01 04:39:55 -0800251
Erik Språng4208a132019-08-26 08:58:45 +0200252TEST_F(PacketRouterTest, SendTransportFeedback) {
eladalon6c9556e2017-07-10 03:33:00 -0700253 NiceMock<MockRtpRtcp> rtp_1;
254 NiceMock<MockRtpRtcp> rtp_2;
eladalonb1338fe2017-08-01 09:36:19 -0700255
Per Kjellanderee153c92019-10-10 16:43:46 +0200256 ON_CALL(rtp_1, RTCP()).WillByDefault(Return(RtcpMode::kCompound));
257 ON_CALL(rtp_2, RTCP()).WillByDefault(Return(RtcpMode::kCompound));
258
Erik Språng4208a132019-08-26 08:58:45 +0200259 packet_router_.AddSendRtpModule(&rtp_1, false);
260 packet_router_.AddReceiveRtpModule(&rtp_2, false);
stefanbba9dec2016-02-01 04:39:55 -0800261
Per Kjellanderee153c92019-10-10 16:43:46 +0200262 std::vector<std::unique_ptr<rtcp::RtcpPacket>> feedback;
263 feedback.push_back(std::make_unique<rtcp::TransportFeedback>());
264 EXPECT_CALL(rtp_1, SendCombinedRtcpPacket).Times(1);
265 packet_router_.SendCombinedRtcpPacket(std::move(feedback));
Erik Språng4208a132019-08-26 08:58:45 +0200266 packet_router_.RemoveSendRtpModule(&rtp_1);
Per Kjellanderee153c92019-10-10 16:43:46 +0200267 EXPECT_CALL(rtp_2, SendCombinedRtcpPacket).Times(1);
268 std::vector<std::unique_ptr<rtcp::RtcpPacket>> new_feedback;
269 new_feedback.push_back(std::make_unique<rtcp::TransportFeedback>());
270 packet_router_.SendCombinedRtcpPacket(std::move(new_feedback));
Erik Språng4208a132019-08-26 08:58:45 +0200271 packet_router_.RemoveReceiveRtpModule(&rtp_2);
272}
273
274TEST_F(PacketRouterTest, SendPacketWithoutTransportSequenceNumbers) {
Erik Språng96f3de02019-10-17 13:02:27 +0200275 NiceMock<MockRtpRtcp> rtp_1;
Erik Språngfbe84ef2019-10-17 11:17:06 +0000276 packet_router_.AddSendRtpModule(&rtp_1, false);
277
278 const uint16_t kSsrc1 = 1234;
Erik Språng4208a132019-08-26 08:58:45 +0200279 ON_CALL(rtp_1, SendingMedia).WillByDefault(Return(true));
280 ON_CALL(rtp_1, SSRC).WillByDefault(Return(kSsrc1));
281
282 // Send a packet without TransportSequenceNumber extension registered,
283 // packets sent should not have the extension set.
284 RtpHeaderExtensionMap extension_manager;
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200285 auto packet = std::make_unique<RtpPacketToSend>(&extension_manager);
Erik Språng4208a132019-08-26 08:58:45 +0200286 packet->SetSsrc(kSsrc1);
287 EXPECT_CALL(
288 rtp_1,
289 TrySendPacket(
290 Property(&RtpPacketToSend::HasExtension<TransportSequenceNumber>,
291 false),
292 _))
293 .WillOnce(Return(true));
294 packet_router_.SendPacket(std::move(packet), PacedPacketInfo());
295
296 packet_router_.RemoveSendRtpModule(&rtp_1);
297}
298
299TEST_F(PacketRouterTest, SendPacketAssignsTransportSequenceNumbers) {
300 NiceMock<MockRtpRtcp> rtp_1;
301 NiceMock<MockRtpRtcp> rtp_2;
302
Erik Språngfbe84ef2019-10-17 11:17:06 +0000303 packet_router_.AddSendRtpModule(&rtp_1, false);
304 packet_router_.AddSendRtpModule(&rtp_2, false);
305
Erik Språng4208a132019-08-26 08:58:45 +0200306 const uint16_t kSsrc1 = 1234;
307 const uint16_t kSsrc2 = 2345;
308
309 ON_CALL(rtp_1, SSRC).WillByDefault(Return(kSsrc1));
310 ON_CALL(rtp_2, SSRC).WillByDefault(Return(kSsrc2));
311
312 // Transport sequence numbers start at 1, for historical reasons.
313 uint16_t transport_sequence_number = 1;
314
315 auto packet = BuildRtpPacket(kSsrc1);
316 EXPECT_TRUE(packet->ReserveExtension<TransportSequenceNumber>());
317 EXPECT_CALL(
318 rtp_1,
319 TrySendPacket(
320 Property(&RtpPacketToSend::GetExtension<TransportSequenceNumber>,
321 transport_sequence_number),
322 _))
323 .WillOnce(Return(true));
324 packet_router_.SendPacket(std::move(packet), PacedPacketInfo());
325
326 ++transport_sequence_number;
327 packet = BuildRtpPacket(kSsrc2);
328 EXPECT_TRUE(packet->ReserveExtension<TransportSequenceNumber>());
329
Erik Språngfbe84ef2019-10-17 11:17:06 +0000330 // There will be a failed attempt to send on kSsrc1 before trying
331 // the correct RTP module.
332 EXPECT_CALL(rtp_1, TrySendPacket).WillOnce(Return(false));
Erik Språng4208a132019-08-26 08:58:45 +0200333 EXPECT_CALL(
334 rtp_2,
335 TrySendPacket(
336 Property(&RtpPacketToSend::GetExtension<TransportSequenceNumber>,
337 transport_sequence_number),
338 _))
339 .WillOnce(Return(true));
340 packet_router_.SendPacket(std::move(packet), PacedPacketInfo());
341
342 packet_router_.RemoveSendRtpModule(&rtp_1);
343 packet_router_.RemoveSendRtpModule(&rtp_2);
stefanbba9dec2016-02-01 04:39:55 -0800344}
nisse05843312017-04-18 23:38:35 -0700345
eladalon822ff2b2017-08-01 06:30:28 -0700346#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
Erik Språng4208a132019-08-26 08:58:45 +0200347TEST_F(PacketRouterTest, DoubleRegistrationOfSendModuleDisallowed) {
eladalon822ff2b2017-08-01 06:30:28 -0700348 NiceMock<MockRtpRtcp> module;
349
350 constexpr bool remb_candidate = false; // Value irrelevant.
Erik Språng4208a132019-08-26 08:58:45 +0200351 packet_router_.AddSendRtpModule(&module, remb_candidate);
352 EXPECT_DEATH(packet_router_.AddSendRtpModule(&module, remb_candidate), "");
eladalon822ff2b2017-08-01 06:30:28 -0700353
354 // Test tear-down
Erik Språng4208a132019-08-26 08:58:45 +0200355 packet_router_.RemoveSendRtpModule(&module);
eladalon822ff2b2017-08-01 06:30:28 -0700356}
357
Erik Språng4208a132019-08-26 08:58:45 +0200358TEST_F(PacketRouterTest, DoubleRegistrationOfReceiveModuleDisallowed) {
eladalon822ff2b2017-08-01 06:30:28 -0700359 NiceMock<MockRtpRtcp> module;
360
361 constexpr bool remb_candidate = false; // Value irrelevant.
Erik Språng4208a132019-08-26 08:58:45 +0200362 packet_router_.AddReceiveRtpModule(&module, remb_candidate);
363 EXPECT_DEATH(packet_router_.AddReceiveRtpModule(&module, remb_candidate), "");
eladalon822ff2b2017-08-01 06:30:28 -0700364
365 // Test tear-down
Erik Språng4208a132019-08-26 08:58:45 +0200366 packet_router_.RemoveReceiveRtpModule(&module);
eladalon822ff2b2017-08-01 06:30:28 -0700367}
368
Erik Språng4208a132019-08-26 08:58:45 +0200369TEST_F(PacketRouterTest, RemovalOfNeverAddedSendModuleDisallowed) {
eladalon822ff2b2017-08-01 06:30:28 -0700370 NiceMock<MockRtpRtcp> module;
371
Erik Språng4208a132019-08-26 08:58:45 +0200372 EXPECT_DEATH(packet_router_.RemoveSendRtpModule(&module), "");
eladalon822ff2b2017-08-01 06:30:28 -0700373}
374
Erik Språng4208a132019-08-26 08:58:45 +0200375TEST_F(PacketRouterTest, RemovalOfNeverAddedReceiveModuleDisallowed) {
eladalon822ff2b2017-08-01 06:30:28 -0700376 NiceMock<MockRtpRtcp> module;
377
Erik Språng4208a132019-08-26 08:58:45 +0200378 EXPECT_DEATH(packet_router_.RemoveReceiveRtpModule(&module), "");
eladalon822ff2b2017-08-01 06:30:28 -0700379}
380#endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
381
nisse05843312017-04-18 23:38:35 -0700382TEST(PacketRouterRembTest, LowerEstimateToSendRemb) {
383 rtc::ScopedFakeClock clock;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200384 NiceMock<MockRtpRtcp> rtp;
nisse05843312017-04-18 23:38:35 -0700385 PacketRouter packet_router;
386
eladalon822ff2b2017-08-01 06:30:28 -0700387 packet_router.AddSendRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700388
389 uint32_t bitrate_estimate = 456;
390 const std::vector<uint32_t> ssrcs = {1234};
391
nisse05843312017-04-18 23:38:35 -0700392 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
393
394 // Call OnReceiveBitrateChanged twice to get a first estimate.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200395 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200396 EXPECT_CALL(rtp, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700397 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
398
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200399 // Lower the estimate with more than 3% to trigger a call to SetRemb right
nisse05843312017-04-18 23:38:35 -0700400 // away.
401 bitrate_estimate = bitrate_estimate - 100;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200402 EXPECT_CALL(rtp, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700403 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
404
nisse05843312017-04-18 23:38:35 -0700405 packet_router.RemoveSendRtpModule(&rtp);
406}
407
408TEST(PacketRouterRembTest, VerifyIncreasingAndDecreasing) {
409 rtc::ScopedFakeClock clock;
eladalon6c9556e2017-07-10 03:33:00 -0700410 NiceMock<MockRtpRtcp> rtp;
nisse05843312017-04-18 23:38:35 -0700411 PacketRouter packet_router;
eladalon822ff2b2017-08-01 06:30:28 -0700412 packet_router.AddSendRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700413
414 uint32_t bitrate_estimate[] = {456, 789};
415 std::vector<uint32_t> ssrcs = {1234, 5678};
416
nisse05843312017-04-18 23:38:35 -0700417 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate[0]);
418
419 // Call OnReceiveBitrateChanged twice to get a first estimate.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200420 EXPECT_CALL(rtp, SetRemb(bitrate_estimate[0], ssrcs)).Times(1);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200421 clock.AdvanceTime(TimeDelta::ms(1000));
nisse05843312017-04-18 23:38:35 -0700422 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate[0]);
423
424 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate[1] + 100);
425
426 // Lower the estimate to trigger a callback.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200427 EXPECT_CALL(rtp, SetRemb(bitrate_estimate[1], ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700428 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate[1]);
429
430 packet_router.RemoveSendRtpModule(&rtp);
431}
432
433TEST(PacketRouterRembTest, NoRembForIncreasedBitrate) {
434 rtc::ScopedFakeClock clock;
eladalon6c9556e2017-07-10 03:33:00 -0700435 NiceMock<MockRtpRtcp> rtp;
nisse05843312017-04-18 23:38:35 -0700436 PacketRouter packet_router;
eladalon822ff2b2017-08-01 06:30:28 -0700437 packet_router.AddSendRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700438
439 uint32_t bitrate_estimate = 456;
440 std::vector<uint32_t> ssrcs = {1234, 5678};
441
nisse05843312017-04-18 23:38:35 -0700442 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
443
444 // Call OnReceiveBitrateChanged twice to get a first estimate.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200445 EXPECT_CALL(rtp, SetRemb(bitrate_estimate, ssrcs)).Times(1);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200446 clock.AdvanceTime(TimeDelta::ms(1000));
nisse05843312017-04-18 23:38:35 -0700447 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
448
449 // Increased estimate shouldn't trigger a callback right away.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200450 EXPECT_CALL(rtp, SetRemb(_, _)).Times(0);
nisse05843312017-04-18 23:38:35 -0700451 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate + 1);
452
453 // Decreasing the estimate less than 3% shouldn't trigger a new callback.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200454 EXPECT_CALL(rtp, SetRemb(_, _)).Times(0);
nisse05843312017-04-18 23:38:35 -0700455 int lower_estimate = bitrate_estimate * 98 / 100;
456 packet_router.OnReceiveBitrateChanged(ssrcs, lower_estimate);
457
458 packet_router.RemoveSendRtpModule(&rtp);
459}
460
461TEST(PacketRouterRembTest, ChangeSendRtpModule) {
462 rtc::ScopedFakeClock clock;
eladalon6c9556e2017-07-10 03:33:00 -0700463 NiceMock<MockRtpRtcp> rtp_send;
464 NiceMock<MockRtpRtcp> rtp_recv;
nisse05843312017-04-18 23:38:35 -0700465 PacketRouter packet_router;
eladalon822ff2b2017-08-01 06:30:28 -0700466 packet_router.AddSendRtpModule(&rtp_send, true);
467 packet_router.AddReceiveRtpModule(&rtp_recv, true);
nisse05843312017-04-18 23:38:35 -0700468
469 uint32_t bitrate_estimate = 456;
470 std::vector<uint32_t> ssrcs = {1234, 5678};
471
nisse05843312017-04-18 23:38:35 -0700472 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
473
474 // Call OnReceiveBitrateChanged twice to get a first estimate.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200475 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200476 EXPECT_CALL(rtp_send, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700477 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
478
479 // Decrease estimate to trigger a REMB.
480 bitrate_estimate = bitrate_estimate - 100;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200481 EXPECT_CALL(rtp_send, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700482 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
483
484 // Remove the sending module -> should get remb on the second module.
485 packet_router.RemoveSendRtpModule(&rtp_send);
486
nisse05843312017-04-18 23:38:35 -0700487 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
488
489 bitrate_estimate = bitrate_estimate - 100;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200490 EXPECT_CALL(rtp_recv, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700491 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
492
493 packet_router.RemoveReceiveRtpModule(&rtp_recv);
494}
495
496TEST(PacketRouterRembTest, OnlyOneRembForRepeatedOnReceiveBitrateChanged) {
497 rtc::ScopedFakeClock clock;
eladalon6c9556e2017-07-10 03:33:00 -0700498 NiceMock<MockRtpRtcp> rtp;
nisse05843312017-04-18 23:38:35 -0700499 PacketRouter packet_router;
eladalon822ff2b2017-08-01 06:30:28 -0700500 packet_router.AddSendRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700501
502 uint32_t bitrate_estimate = 456;
503 const std::vector<uint32_t> ssrcs = {1234};
504
nisse05843312017-04-18 23:38:35 -0700505 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
506
507 // Call OnReceiveBitrateChanged twice to get a first estimate.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200508 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200509 EXPECT_CALL(rtp, SetRemb(_, _)).Times(1);
nisse05843312017-04-18 23:38:35 -0700510 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
511
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200512 // Lower the estimate, should trigger a call to SetRemb right away.
nisse05843312017-04-18 23:38:35 -0700513 bitrate_estimate = bitrate_estimate - 100;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200514 EXPECT_CALL(rtp, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700515 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
516
517 // Call OnReceiveBitrateChanged again, this should not trigger a new callback.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200518 EXPECT_CALL(rtp, SetRemb(_, _)).Times(0);
nisse05843312017-04-18 23:38:35 -0700519 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
520 packet_router.RemoveSendRtpModule(&rtp);
521}
522
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200523TEST(PacketRouterRembTest, SetMaxDesiredReceiveBitrateLimitsSetRemb) {
danilchap47085372017-08-10 06:03:57 -0700524 rtc::ScopedFakeClock clock;
525 PacketRouter packet_router;
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200526 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200527 NiceMock<MockRtpRtcp> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700528 constexpr bool remb_candidate = true;
529 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700530
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100531 const int64_t cap_bitrate = 100000;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200532 EXPECT_CALL(remb_sender, SetRemb(Le(cap_bitrate), _)).Times(AtLeast(1));
533 EXPECT_CALL(remb_sender, SetRemb(Gt(cap_bitrate), _)).Times(0);
danilchap47085372017-08-10 06:03:57 -0700534
535 const std::vector<uint32_t> ssrcs = {1234};
536 packet_router.SetMaxDesiredReceiveBitrate(cap_bitrate);
537 packet_router.OnReceiveBitrateChanged(ssrcs, cap_bitrate + 5000);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200538 clock.AdvanceTime(TimeDelta::ms(1000));
danilchap47085372017-08-10 06:03:57 -0700539 packet_router.OnReceiveBitrateChanged(ssrcs, cap_bitrate - 5000);
540
541 // Test tear-down.
542 packet_router.RemoveSendRtpModule(&remb_sender);
543}
544
545TEST(PacketRouterRembTest,
546 SetMaxDesiredReceiveBitrateTriggersRembWhenMoreRestrictive) {
547 rtc::ScopedFakeClock clock;
548 PacketRouter packet_router;
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200549 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200550 NiceMock<MockRtpRtcp> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700551 constexpr bool remb_candidate = true;
552 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700553
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100554 const int64_t measured_bitrate_bps = 150000;
555 const int64_t cap_bitrate_bps = measured_bitrate_bps - 5000;
danilchap47085372017-08-10 06:03:57 -0700556 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200557 EXPECT_CALL(remb_sender, SetRemb(measured_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700558 packet_router.OnReceiveBitrateChanged(ssrcs, measured_bitrate_bps);
559
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200560 EXPECT_CALL(remb_sender, SetRemb(cap_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700561 packet_router.SetMaxDesiredReceiveBitrate(cap_bitrate_bps);
562
563 // Test tear-down.
564 packet_router.RemoveSendRtpModule(&remb_sender);
565}
566
567TEST(PacketRouterRembTest,
568 SetMaxDesiredReceiveBitrateDoesNotTriggerRembWhenAsRestrictive) {
569 rtc::ScopedFakeClock clock;
570 PacketRouter packet_router;
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200571 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200572 NiceMock<MockRtpRtcp> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700573 constexpr bool remb_candidate = true;
574 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700575
576 const uint32_t measured_bitrate_bps = 150000;
577 const uint32_t cap_bitrate_bps = measured_bitrate_bps;
578 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200579 EXPECT_CALL(remb_sender, SetRemb(measured_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700580 packet_router.OnReceiveBitrateChanged(ssrcs, measured_bitrate_bps);
581
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200582 EXPECT_CALL(remb_sender, SetRemb(_, _)).Times(0);
danilchap47085372017-08-10 06:03:57 -0700583 packet_router.SetMaxDesiredReceiveBitrate(cap_bitrate_bps);
584
585 // Test tear-down.
586 packet_router.RemoveSendRtpModule(&remb_sender);
587}
588
589TEST(PacketRouterRembTest,
590 SetMaxDesiredReceiveBitrateDoesNotTriggerRembWhenLessRestrictive) {
591 rtc::ScopedFakeClock clock;
592 PacketRouter packet_router;
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200593 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200594 NiceMock<MockRtpRtcp> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700595 constexpr bool remb_candidate = true;
596 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700597
598 const uint32_t measured_bitrate_bps = 150000;
599 const uint32_t cap_bitrate_bps = measured_bitrate_bps + 500;
600 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200601 EXPECT_CALL(remb_sender, SetRemb(measured_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700602 packet_router.OnReceiveBitrateChanged(ssrcs, measured_bitrate_bps);
603
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200604 EXPECT_CALL(remb_sender, SetRemb(_, _)).Times(0);
danilchap47085372017-08-10 06:03:57 -0700605 packet_router.SetMaxDesiredReceiveBitrate(cap_bitrate_bps);
606
607 // Test tear-down.
608 packet_router.RemoveSendRtpModule(&remb_sender);
609}
610
611TEST(PacketRouterRembTest,
612 SetMaxDesiredReceiveBitrateTriggersRembWhenNoRecentMeasure) {
613 rtc::ScopedFakeClock clock;
614 PacketRouter packet_router;
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200615 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200616 NiceMock<MockRtpRtcp> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700617 constexpr bool remb_candidate = true;
618 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700619
620 const uint32_t measured_bitrate_bps = 150000;
621 const uint32_t cap_bitrate_bps = measured_bitrate_bps + 5000;
622 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200623 EXPECT_CALL(remb_sender, SetRemb(measured_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700624 packet_router.OnReceiveBitrateChanged(ssrcs, measured_bitrate_bps);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200625 clock.AdvanceTime(TimeDelta::ms(1000));
danilchap47085372017-08-10 06:03:57 -0700626
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200627 EXPECT_CALL(remb_sender, SetRemb(cap_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700628 packet_router.SetMaxDesiredReceiveBitrate(cap_bitrate_bps);
629
630 // Test tear-down.
631 packet_router.RemoveSendRtpModule(&remb_sender);
632}
633
634TEST(PacketRouterRembTest,
635 SetMaxDesiredReceiveBitrateTriggersRembWhenNoMeasures) {
636 rtc::ScopedFakeClock clock;
637 PacketRouter packet_router;
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200638 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200639 NiceMock<MockRtpRtcp> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700640 constexpr bool remb_candidate = true;
641 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700642
643 // Set cap.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200644 EXPECT_CALL(remb_sender, SetRemb(100000, _)).Times(1);
danilchap47085372017-08-10 06:03:57 -0700645 packet_router.SetMaxDesiredReceiveBitrate(100000);
646 // Increase cap.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200647 EXPECT_CALL(remb_sender, SetRemb(200000, _)).Times(1);
danilchap47085372017-08-10 06:03:57 -0700648 packet_router.SetMaxDesiredReceiveBitrate(200000);
649 // Decrease cap.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200650 EXPECT_CALL(remb_sender, SetRemb(150000, _)).Times(1);
danilchap47085372017-08-10 06:03:57 -0700651 packet_router.SetMaxDesiredReceiveBitrate(150000);
652
653 // Test tear-down.
654 packet_router.RemoveSendRtpModule(&remb_sender);
655}
656
nisse05843312017-04-18 23:38:35 -0700657// Only register receiving modules and make sure we fallback to trigger a REMB
658// packet on this one.
659TEST(PacketRouterRembTest, NoSendingRtpModule) {
660 rtc::ScopedFakeClock clock;
eladalon6c9556e2017-07-10 03:33:00 -0700661 NiceMock<MockRtpRtcp> rtp;
nisse05843312017-04-18 23:38:35 -0700662 PacketRouter packet_router;
663
eladalon822ff2b2017-08-01 06:30:28 -0700664 packet_router.AddReceiveRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700665
666 uint32_t bitrate_estimate = 456;
667 const std::vector<uint32_t> ssrcs = {1234};
668
nisse05843312017-04-18 23:38:35 -0700669 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
670
671 // Call OnReceiveBitrateChanged twice to get a first estimate.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200672 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200673 EXPECT_CALL(rtp, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700674 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
675
676 // Lower the estimate to trigger a new packet REMB packet.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200677 EXPECT_CALL(rtp, SetRemb(bitrate_estimate - 100, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700678 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate - 100);
679
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200680 EXPECT_CALL(rtp, UnsetRemb()).Times(1);
nisse05843312017-04-18 23:38:35 -0700681 packet_router.RemoveReceiveRtpModule(&rtp);
682}
683
eladalon822ff2b2017-08-01 06:30:28 -0700684TEST(PacketRouterRembTest, NonCandidateSendRtpModuleNotUsedForRemb) {
685 rtc::ScopedFakeClock clock;
686 PacketRouter packet_router;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200687 NiceMock<MockRtpRtcp> module;
eladalon822ff2b2017-08-01 06:30:28 -0700688
689 constexpr bool remb_candidate = false;
690
691 packet_router.AddSendRtpModule(&module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700692
693 constexpr uint32_t bitrate_estimate = 456;
694 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200695 EXPECT_CALL(module, SetRemb(_, _)).Times(0);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200696 clock.AdvanceTime(TimeDelta::ms(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700697 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
698
699 // Test tear-down
700 packet_router.RemoveSendRtpModule(&module);
701}
702
703TEST(PacketRouterRembTest, CandidateSendRtpModuleUsedForRemb) {
704 rtc::ScopedFakeClock clock;
705 PacketRouter packet_router;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200706 NiceMock<MockRtpRtcp> module;
eladalon822ff2b2017-08-01 06:30:28 -0700707
708 constexpr bool remb_candidate = true;
709
710 packet_router.AddSendRtpModule(&module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700711
712 constexpr uint32_t bitrate_estimate = 456;
713 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200714 EXPECT_CALL(module, SetRemb(bitrate_estimate, ssrcs)).Times(1);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200715 clock.AdvanceTime(TimeDelta::ms(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700716 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
717
718 // Test tear-down
719 packet_router.RemoveSendRtpModule(&module);
720}
721
722TEST(PacketRouterRembTest, NonCandidateReceiveRtpModuleNotUsedForRemb) {
723 rtc::ScopedFakeClock clock;
724 PacketRouter packet_router;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200725 NiceMock<MockRtpRtcp> module;
eladalon822ff2b2017-08-01 06:30:28 -0700726
727 constexpr bool remb_candidate = false;
728
729 packet_router.AddReceiveRtpModule(&module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700730
731 constexpr uint32_t bitrate_estimate = 456;
732 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200733 EXPECT_CALL(module, SetRemb(_, _)).Times(0);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200734 clock.AdvanceTime(TimeDelta::ms(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700735 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
736
737 // Test tear-down
738 packet_router.RemoveReceiveRtpModule(&module);
739}
740
741TEST(PacketRouterRembTest, CandidateReceiveRtpModuleUsedForRemb) {
742 rtc::ScopedFakeClock clock;
743 PacketRouter packet_router;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200744 NiceMock<MockRtpRtcp> module;
eladalon822ff2b2017-08-01 06:30:28 -0700745
746 constexpr bool remb_candidate = true;
747
748 packet_router.AddReceiveRtpModule(&module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700749
750 constexpr uint32_t bitrate_estimate = 456;
751 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200752 EXPECT_CALL(module, SetRemb(bitrate_estimate, ssrcs)).Times(1);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200753 clock.AdvanceTime(TimeDelta::ms(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700754 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
755
756 // Test tear-down
757 packet_router.RemoveReceiveRtpModule(&module);
758}
759
760TEST(PacketRouterRembTest,
761 SendCandidatePreferredOverReceiveCandidate_SendModuleAddedFirst) {
762 rtc::ScopedFakeClock clock;
763 PacketRouter packet_router;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200764 NiceMock<MockRtpRtcp> send_module;
765 NiceMock<MockRtpRtcp> receive_module;
eladalon822ff2b2017-08-01 06:30:28 -0700766
767 constexpr bool remb_candidate = true;
768
769 // Send module added - activated.
770 packet_router.AddSendRtpModule(&send_module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700771
772 // Receive module added - the send module remains the active one.
773 packet_router.AddReceiveRtpModule(&receive_module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700774
775 constexpr uint32_t bitrate_estimate = 456;
776 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200777 EXPECT_CALL(send_module, SetRemb(bitrate_estimate, ssrcs)).Times(1);
778 EXPECT_CALL(receive_module, SetRemb(_, _)).Times(0);
eladalon822ff2b2017-08-01 06:30:28 -0700779
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200780 clock.AdvanceTime(TimeDelta::ms(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700781 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
782
783 // Test tear-down
784 packet_router.RemoveReceiveRtpModule(&receive_module);
785 packet_router.RemoveSendRtpModule(&send_module);
786}
787
788TEST(PacketRouterRembTest,
789 SendCandidatePreferredOverReceiveCandidate_ReceiveModuleAddedFirst) {
790 rtc::ScopedFakeClock clock;
791 PacketRouter packet_router;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200792 NiceMock<MockRtpRtcp> send_module;
793 NiceMock<MockRtpRtcp> receive_module;
eladalon822ff2b2017-08-01 06:30:28 -0700794
795 constexpr bool remb_candidate = true;
796
797 // Receive module added - activated.
798 packet_router.AddReceiveRtpModule(&receive_module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700799
800 // Send module added - replaces receive module as active.
801 packet_router.AddSendRtpModule(&send_module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700802
803 constexpr uint32_t bitrate_estimate = 456;
804 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200805 EXPECT_CALL(send_module, SetRemb(bitrate_estimate, ssrcs)).Times(1);
806 EXPECT_CALL(receive_module, SetRemb(_, _)).Times(0);
eladalon822ff2b2017-08-01 06:30:28 -0700807
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200808 clock.AdvanceTime(TimeDelta::ms(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700809 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
810
811 // Test tear-down
812 packet_router.RemoveReceiveRtpModule(&receive_module);
813 packet_router.RemoveSendRtpModule(&send_module);
814}
815
816TEST(PacketRouterRembTest, ReceiveModuleTakesOverWhenLastSendModuleRemoved) {
817 rtc::ScopedFakeClock clock;
818 PacketRouter packet_router;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200819 NiceMock<MockRtpRtcp> send_module;
820 NiceMock<MockRtpRtcp> receive_module;
eladalon822ff2b2017-08-01 06:30:28 -0700821
822 constexpr bool remb_candidate = true;
823
824 // Send module active, receive module inactive.
825 packet_router.AddSendRtpModule(&send_module, remb_candidate);
826 packet_router.AddReceiveRtpModule(&receive_module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700827
828 // Send module removed - receive module becomes active.
829 packet_router.RemoveSendRtpModule(&send_module);
eladalon822ff2b2017-08-01 06:30:28 -0700830 constexpr uint32_t bitrate_estimate = 456;
831 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200832 EXPECT_CALL(send_module, SetRemb(_, _)).Times(0);
833 EXPECT_CALL(receive_module, SetRemb(bitrate_estimate, ssrcs)).Times(1);
eladalon822ff2b2017-08-01 06:30:28 -0700834
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200835 clock.AdvanceTime(TimeDelta::ms(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700836 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
837
838 // Test tear-down
839 packet_router.RemoveReceiveRtpModule(&receive_module);
840}
841
Stefan Holmere5904162015-03-26 11:11:06 +0100842} // namespace webrtc