blob: 08d76b234f04fa0fdbdcf14ff25f90ba718a2480 [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>
Erik Språngf6468d22019-07-05 16:53:43 +020015#include <utility>
Stefan Holmere5904162015-03-26 11:11:06 +010016
Erik Språngf6468d22019-07-05 16:53:43 +020017#include "absl/memory/memory.h"
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() {
55 const int kTransportSequenceNumberExtensionId = 1;
56 extension_manager.Register(kRtpExtensionTransportSequenceNumber,
57 kTransportSequenceNumberExtensionId);
58 }
eladalon32040ef2017-08-02 06:29:00 -070059
Erik Språng4208a132019-08-26 08:58:45 +020060 protected:
61 std::unique_ptr<RtpPacketToSend> BuildRtpPacket(uint32_t ssrc) {
62 std::unique_ptr<RtpPacketToSend> packet =
63 absl::make_unique<RtpPacketToSend>(&extension_manager);
64 packet->SetSsrc(ssrc);
65 return packet;
66 }
eladalon32040ef2017-08-02 06:29:00 -070067
Erik Språng4208a132019-08-26 08:58:45 +020068 PacketRouter packet_router_;
69 RtpHeaderExtensionMap extension_manager;
70};
eladalon32040ef2017-08-02 06:29:00 -070071
Erik Språng4208a132019-08-26 08:58:45 +020072TEST_F(PacketRouterTest, Sanity_NoModuleRegistered_GeneratePadding) {
eladalon32040ef2017-08-02 06:29:00 -070073 constexpr size_t bytes = 300;
74 const PacedPacketInfo paced_info(1, kProbeMinProbes, kProbeMinBytes);
75
Erik Språng4208a132019-08-26 08:58:45 +020076 EXPECT_TRUE(packet_router_.GeneratePadding(bytes).empty());
eladalon32040ef2017-08-02 06:29:00 -070077}
78
Erik Språng4208a132019-08-26 08:58:45 +020079TEST_F(PacketRouterTest, Sanity_NoModuleRegistered_OnReceiveBitrateChanged) {
eladalon32040ef2017-08-02 06:29:00 -070080 const std::vector<uint32_t> ssrcs = {1, 2, 3};
81 constexpr uint32_t bitrate_bps = 10000;
82
Erik Språng4208a132019-08-26 08:58:45 +020083 packet_router_.OnReceiveBitrateChanged(ssrcs, bitrate_bps);
eladalon32040ef2017-08-02 06:29:00 -070084}
85
Erik Språng4208a132019-08-26 08:58:45 +020086TEST_F(PacketRouterTest, Sanity_NoModuleRegistered_SendRemb) {
eladalon32040ef2017-08-02 06:29:00 -070087 const std::vector<uint32_t> ssrcs = {1, 2, 3};
88 constexpr uint32_t bitrate_bps = 10000;
89
Erik Språng4208a132019-08-26 08:58:45 +020090 EXPECT_FALSE(packet_router_.SendRemb(bitrate_bps, ssrcs));
eladalon32040ef2017-08-02 06:29:00 -070091}
92
Erik Språng4208a132019-08-26 08:58:45 +020093TEST_F(PacketRouterTest, Sanity_NoModuleRegistered_SendTransportFeedback) {
eladalon32040ef2017-08-02 06:29:00 -070094 rtcp::TransportFeedback feedback;
95
Erik Språng4208a132019-08-26 08:58:45 +020096 EXPECT_FALSE(packet_router_.SendTransportFeedback(&feedback));
eladalon32040ef2017-08-02 06:29:00 -070097}
98
Erik Språng4208a132019-08-26 08:58:45 +020099TEST_F(PacketRouterTest, GeneratePaddingPicksCorrectModule) {
Erik Språng478cb462019-06-26 15:49:27 +0200100 // Two RTP modules. The first (prioritized due to rtx) isn't sending media so
101 // should not be called.
102 const uint16_t kSsrc1 = 1234;
103 const uint16_t kSsrc2 = 4567;
104
105 NiceMock<MockRtpRtcp> rtp_1;
106 ON_CALL(rtp_1, RtxSendStatus()).WillByDefault(Return(kRtxRedundantPayloads));
107 ON_CALL(rtp_1, SSRC()).WillByDefault(Return(kSsrc1));
Mirko Bonadei999a72a2019-07-12 17:33:46 +0000108 ON_CALL(rtp_1, SupportsPadding).WillByDefault(Return(false));
Erik Språng478cb462019-06-26 15:49:27 +0200109
110 NiceMock<MockRtpRtcp> rtp_2;
111 ON_CALL(rtp_2, RtxSendStatus()).WillByDefault(Return(kRtxOff));
112 ON_CALL(rtp_2, SSRC()).WillByDefault(Return(kSsrc2));
Mirko Bonadei999a72a2019-07-12 17:33:46 +0000113 ON_CALL(rtp_2, SupportsPadding).WillByDefault(Return(true));
Erik Språng478cb462019-06-26 15:49:27 +0200114
Erik Språng4208a132019-08-26 08:58:45 +0200115 packet_router_.AddSendRtpModule(&rtp_1, false);
116 packet_router_.AddSendRtpModule(&rtp_2, false);
Erik Språng478cb462019-06-26 15:49:27 +0200117
118 const size_t kPaddingSize = 123;
Erik Språngf6468d22019-07-05 16:53:43 +0200119 const size_t kExpectedPaddingPackets = 1;
Erik Språng478cb462019-06-26 15:49:27 +0200120 EXPECT_CALL(rtp_1, GeneratePadding(_)).Times(0);
Erik Språngf6468d22019-07-05 16:53:43 +0200121 EXPECT_CALL(rtp_2, GeneratePadding(kPaddingSize))
122 .WillOnce([&](size_t padding_size) {
123 return std::vector<std::unique_ptr<RtpPacketToSend>>(
124 kExpectedPaddingPackets);
125 });
Erik Språng4208a132019-08-26 08:58:45 +0200126 auto generated_padding = packet_router_.GeneratePadding(kPaddingSize);
Erik Språngf6468d22019-07-05 16:53:43 +0200127 EXPECT_EQ(generated_padding.size(), kExpectedPaddingPackets);
Erik Språng478cb462019-06-26 15:49:27 +0200128
Erik Språng4208a132019-08-26 08:58:45 +0200129 packet_router_.RemoveSendRtpModule(&rtp_1);
130 packet_router_.RemoveSendRtpModule(&rtp_2);
Erik Språng478cb462019-06-26 15:49:27 +0200131}
132
Erik Språng4208a132019-08-26 08:58:45 +0200133TEST_F(PacketRouterTest, PadsOnLastActiveMediaStream) {
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200134 const uint16_t kSsrc1 = 1234;
135 const uint16_t kSsrc2 = 4567;
136 const uint16_t kSsrc3 = 8901;
137
138 // First two rtp modules send media and have rtx.
139 NiceMock<MockRtpRtcp> rtp_1;
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200140 EXPECT_CALL(rtp_1, SSRC()).WillRepeatedly(Return(kSsrc1));
Mirko Bonadei999a72a2019-07-12 17:33:46 +0000141 EXPECT_CALL(rtp_1, SupportsPadding).WillRepeatedly(Return(true));
142 EXPECT_CALL(rtp_1, SupportsRtxPayloadPadding).WillRepeatedly(Return(true));
Erik Språng4208a132019-08-26 08:58:45 +0200143 EXPECT_CALL(rtp_1, TrySendPacket).WillRepeatedly(Return(false));
144 EXPECT_CALL(
145 rtp_1,
146 TrySendPacket(
147 ::testing::Pointee(Property(&RtpPacketToSend::Ssrc, kSsrc1)), _))
148 .WillRepeatedly(Return(true));
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200149
150 NiceMock<MockRtpRtcp> rtp_2;
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200151 EXPECT_CALL(rtp_2, SSRC()).WillRepeatedly(Return(kSsrc2));
Mirko Bonadei999a72a2019-07-12 17:33:46 +0000152 EXPECT_CALL(rtp_2, SupportsPadding).WillRepeatedly(Return(true));
153 EXPECT_CALL(rtp_2, SupportsRtxPayloadPadding).WillRepeatedly(Return(true));
Erik Språng4208a132019-08-26 08:58:45 +0200154 EXPECT_CALL(rtp_2, TrySendPacket).WillRepeatedly(Return(false));
155 EXPECT_CALL(
156 rtp_2,
157 TrySendPacket(
158 ::testing::Pointee(Property(&RtpPacketToSend::Ssrc, kSsrc2)), _))
159 .WillRepeatedly(Return(true));
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200160
161 // Third module is sending media, but does not support rtx.
162 NiceMock<MockRtpRtcp> rtp_3;
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200163 EXPECT_CALL(rtp_3, SSRC()).WillRepeatedly(Return(kSsrc3));
Mirko Bonadei999a72a2019-07-12 17:33:46 +0000164 EXPECT_CALL(rtp_3, SupportsPadding).WillRepeatedly(Return(true));
Erik Språng4208a132019-08-26 08:58:45 +0200165 EXPECT_CALL(rtp_3, SupportsRtxPayloadPadding).WillRepeatedly(Return(false));
166 EXPECT_CALL(rtp_3, TrySendPacket).WillRepeatedly(Return(false));
167 EXPECT_CALL(
168 rtp_3,
169 TrySendPacket(
170 ::testing::Pointee(Property(&RtpPacketToSend::Ssrc, kSsrc3)), _))
171 .WillRepeatedly(Return(true));
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200172
Erik Språng4208a132019-08-26 08:58:45 +0200173 packet_router_.AddSendRtpModule(&rtp_1, false);
174 packet_router_.AddSendRtpModule(&rtp_2, false);
175 packet_router_.AddSendRtpModule(&rtp_3, false);
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200176
177 const size_t kPaddingBytes = 100;
178
179 // Initially, padding will be sent on last added rtp module that sends media
180 // and supports rtx.
Erik Språng4208a132019-08-26 08:58:45 +0200181 EXPECT_CALL(rtp_2, GeneratePadding(kPaddingBytes))
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200182 .Times(1)
Erik Språng4208a132019-08-26 08:58:45 +0200183 .WillOnce([](size_t target_size_bytes) {
184 return std::vector<std::unique_ptr<RtpPacketToSend>>();
185 });
186 packet_router_.GeneratePadding(kPaddingBytes);
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200187
188 // Send media on first module. Padding should be sent on that module.
Erik Språng4208a132019-08-26 08:58:45 +0200189 packet_router_.SendPacket(BuildRtpPacket(kSsrc1), PacedPacketInfo());
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200190
Erik Språng4208a132019-08-26 08:58:45 +0200191 EXPECT_CALL(rtp_1, GeneratePadding(kPaddingBytes))
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200192 .Times(1)
Erik Språng4208a132019-08-26 08:58:45 +0200193 .WillOnce([](size_t target_size_bytes) {
194 return std::vector<std::unique_ptr<RtpPacketToSend>>();
195 });
196 packet_router_.GeneratePadding(kPaddingBytes);
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200197
198 // Send media on second module. Padding should be sent there.
Erik Språng4208a132019-08-26 08:58:45 +0200199 packet_router_.SendPacket(BuildRtpPacket(kSsrc2), PacedPacketInfo());
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200200
Erik Språng4208a132019-08-26 08:58:45 +0200201 EXPECT_CALL(rtp_2, GeneratePadding(kPaddingBytes))
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200202 .Times(1)
Erik Språng4208a132019-08-26 08:58:45 +0200203 .WillOnce([](size_t target_size_bytes) {
204 return std::vector<std::unique_ptr<RtpPacketToSend>>();
205 });
206 packet_router_.GeneratePadding(kPaddingBytes);
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200207
208 // Remove second module, padding should now fall back to first module.
Erik Språng4208a132019-08-26 08:58:45 +0200209 packet_router_.RemoveSendRtpModule(&rtp_2);
210 EXPECT_CALL(rtp_1, GeneratePadding(kPaddingBytes))
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200211 .Times(1)
Erik Språng4208a132019-08-26 08:58:45 +0200212 .WillOnce([](size_t target_size_bytes) {
213 return std::vector<std::unique_ptr<RtpPacketToSend>>();
214 });
215 packet_router_.GeneratePadding(kPaddingBytes);
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200216
217 // Remove first module too, leaving only the one without rtx.
Erik Språng4208a132019-08-26 08:58:45 +0200218 packet_router_.RemoveSendRtpModule(&rtp_1);
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200219
Erik Språng4208a132019-08-26 08:58:45 +0200220 EXPECT_CALL(rtp_3, GeneratePadding(kPaddingBytes))
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200221 .Times(1)
Erik Språng4208a132019-08-26 08:58:45 +0200222 .WillOnce([](size_t target_size_bytes) {
223 return std::vector<std::unique_ptr<RtpPacketToSend>>();
224 });
225 packet_router_.GeneratePadding(kPaddingBytes);
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200226
Erik Språng4208a132019-08-26 08:58:45 +0200227 packet_router_.RemoveSendRtpModule(&rtp_3);
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200228}
229
Erik Språng4208a132019-08-26 08:58:45 +0200230TEST_F(PacketRouterTest, AllocateSequenceNumbers) {
sprang867fb522015-08-03 04:38:41 -0700231 const uint16_t kStartSeq = 0xFFF0;
232 const size_t kNumPackets = 32;
233
Erik Språng4208a132019-08-26 08:58:45 +0200234 packet_router_.SetTransportWideSequenceNumber(kStartSeq - 1);
sprang867fb522015-08-03 04:38:41 -0700235
236 for (size_t i = 0; i < kNumPackets; ++i) {
Erik Språng4208a132019-08-26 08:58:45 +0200237 uint16_t seq = packet_router_.AllocateSequenceNumber();
sprang867fb522015-08-03 04:38:41 -0700238 uint32_t expected_unwrapped_seq = static_cast<uint32_t>(kStartSeq) + i;
239 EXPECT_EQ(static_cast<uint16_t>(expected_unwrapped_seq & 0xFFFF), seq);
240 }
241}
stefanbba9dec2016-02-01 04:39:55 -0800242
Erik Språng4208a132019-08-26 08:58:45 +0200243TEST_F(PacketRouterTest, SendTransportFeedback) {
eladalon6c9556e2017-07-10 03:33:00 -0700244 NiceMock<MockRtpRtcp> rtp_1;
245 NiceMock<MockRtpRtcp> rtp_2;
eladalonb1338fe2017-08-01 09:36:19 -0700246
Erik Språng4208a132019-08-26 08:58:45 +0200247 packet_router_.AddSendRtpModule(&rtp_1, false);
248 packet_router_.AddReceiveRtpModule(&rtp_2, false);
stefanbba9dec2016-02-01 04:39:55 -0800249
250 rtcp::TransportFeedback feedback;
nisse05843312017-04-18 23:38:35 -0700251 EXPECT_CALL(rtp_1, SendFeedbackPacket(_)).Times(1).WillOnce(Return(true));
Erik Språng4208a132019-08-26 08:58:45 +0200252 packet_router_.SendTransportFeedback(&feedback);
253 packet_router_.RemoveSendRtpModule(&rtp_1);
nisse05843312017-04-18 23:38:35 -0700254 EXPECT_CALL(rtp_2, SendFeedbackPacket(_)).Times(1).WillOnce(Return(true));
Erik Språng4208a132019-08-26 08:58:45 +0200255 packet_router_.SendTransportFeedback(&feedback);
256 packet_router_.RemoveReceiveRtpModule(&rtp_2);
257}
258
259TEST_F(PacketRouterTest, SendPacketWithoutTransportSequenceNumbers) {
260 NiceMock<MockRtpRtcp> rtp_1;
261 packet_router_.AddSendRtpModule(&rtp_1, false);
262
263 const uint16_t kSsrc1 = 1234;
264 ON_CALL(rtp_1, SendingMedia).WillByDefault(Return(true));
265 ON_CALL(rtp_1, SSRC).WillByDefault(Return(kSsrc1));
266
267 // Send a packet without TransportSequenceNumber extension registered,
268 // packets sent should not have the extension set.
269 RtpHeaderExtensionMap extension_manager;
270 auto packet = absl::make_unique<RtpPacketToSend>(&extension_manager);
271 packet->SetSsrc(kSsrc1);
272 EXPECT_CALL(
273 rtp_1,
274 TrySendPacket(
275 Property(&RtpPacketToSend::HasExtension<TransportSequenceNumber>,
276 false),
277 _))
278 .WillOnce(Return(true));
279 packet_router_.SendPacket(std::move(packet), PacedPacketInfo());
280
281 packet_router_.RemoveSendRtpModule(&rtp_1);
282}
283
284TEST_F(PacketRouterTest, SendPacketAssignsTransportSequenceNumbers) {
285 NiceMock<MockRtpRtcp> rtp_1;
286 NiceMock<MockRtpRtcp> rtp_2;
287
288 packet_router_.AddSendRtpModule(&rtp_1, false);
289 packet_router_.AddSendRtpModule(&rtp_2, false);
290
291 const uint16_t kSsrc1 = 1234;
292 const uint16_t kSsrc2 = 2345;
293
294 ON_CALL(rtp_1, SSRC).WillByDefault(Return(kSsrc1));
295 ON_CALL(rtp_2, SSRC).WillByDefault(Return(kSsrc2));
296
297 // Transport sequence numbers start at 1, for historical reasons.
298 uint16_t transport_sequence_number = 1;
299
300 auto packet = BuildRtpPacket(kSsrc1);
301 EXPECT_TRUE(packet->ReserveExtension<TransportSequenceNumber>());
302 EXPECT_CALL(
303 rtp_1,
304 TrySendPacket(
305 Property(&RtpPacketToSend::GetExtension<TransportSequenceNumber>,
306 transport_sequence_number),
307 _))
308 .WillOnce(Return(true));
309 packet_router_.SendPacket(std::move(packet), PacedPacketInfo());
310
311 ++transport_sequence_number;
312 packet = BuildRtpPacket(kSsrc2);
313 EXPECT_TRUE(packet->ReserveExtension<TransportSequenceNumber>());
314
315 // There will be a failed attempt to send on kSsrc1 before trying
316 // the correct RTP module.
317 EXPECT_CALL(rtp_1, TrySendPacket).WillOnce(Return(false));
318 EXPECT_CALL(
319 rtp_2,
320 TrySendPacket(
321 Property(&RtpPacketToSend::GetExtension<TransportSequenceNumber>,
322 transport_sequence_number),
323 _))
324 .WillOnce(Return(true));
325 packet_router_.SendPacket(std::move(packet), PacedPacketInfo());
326
327 packet_router_.RemoveSendRtpModule(&rtp_1);
328 packet_router_.RemoveSendRtpModule(&rtp_2);
stefanbba9dec2016-02-01 04:39:55 -0800329}
nisse05843312017-04-18 23:38:35 -0700330
eladalon822ff2b2017-08-01 06:30:28 -0700331#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
Erik Språng4208a132019-08-26 08:58:45 +0200332TEST_F(PacketRouterTest, DoubleRegistrationOfSendModuleDisallowed) {
eladalon822ff2b2017-08-01 06:30:28 -0700333 NiceMock<MockRtpRtcp> module;
334
335 constexpr bool remb_candidate = false; // Value irrelevant.
Erik Språng4208a132019-08-26 08:58:45 +0200336 packet_router_.AddSendRtpModule(&module, remb_candidate);
337 EXPECT_DEATH(packet_router_.AddSendRtpModule(&module, remb_candidate), "");
eladalon822ff2b2017-08-01 06:30:28 -0700338
339 // Test tear-down
Erik Språng4208a132019-08-26 08:58:45 +0200340 packet_router_.RemoveSendRtpModule(&module);
eladalon822ff2b2017-08-01 06:30:28 -0700341}
342
Erik Språng4208a132019-08-26 08:58:45 +0200343TEST_F(PacketRouterTest, DoubleRegistrationOfReceiveModuleDisallowed) {
eladalon822ff2b2017-08-01 06:30:28 -0700344 NiceMock<MockRtpRtcp> module;
345
346 constexpr bool remb_candidate = false; // Value irrelevant.
Erik Språng4208a132019-08-26 08:58:45 +0200347 packet_router_.AddReceiveRtpModule(&module, remb_candidate);
348 EXPECT_DEATH(packet_router_.AddReceiveRtpModule(&module, remb_candidate), "");
eladalon822ff2b2017-08-01 06:30:28 -0700349
350 // Test tear-down
Erik Språng4208a132019-08-26 08:58:45 +0200351 packet_router_.RemoveReceiveRtpModule(&module);
eladalon822ff2b2017-08-01 06:30:28 -0700352}
353
Erik Språng4208a132019-08-26 08:58:45 +0200354TEST_F(PacketRouterTest, RemovalOfNeverAddedSendModuleDisallowed) {
eladalon822ff2b2017-08-01 06:30:28 -0700355 NiceMock<MockRtpRtcp> module;
356
Erik Språng4208a132019-08-26 08:58:45 +0200357 EXPECT_DEATH(packet_router_.RemoveSendRtpModule(&module), "");
eladalon822ff2b2017-08-01 06:30:28 -0700358}
359
Erik Språng4208a132019-08-26 08:58:45 +0200360TEST_F(PacketRouterTest, RemovalOfNeverAddedReceiveModuleDisallowed) {
eladalon822ff2b2017-08-01 06:30:28 -0700361 NiceMock<MockRtpRtcp> module;
362
Erik Språng4208a132019-08-26 08:58:45 +0200363 EXPECT_DEATH(packet_router_.RemoveReceiveRtpModule(&module), "");
eladalon822ff2b2017-08-01 06:30:28 -0700364}
365#endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
366
nisse05843312017-04-18 23:38:35 -0700367TEST(PacketRouterRembTest, LowerEstimateToSendRemb) {
368 rtc::ScopedFakeClock clock;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200369 NiceMock<MockRtpRtcp> rtp;
nisse05843312017-04-18 23:38:35 -0700370 PacketRouter packet_router;
371
eladalon822ff2b2017-08-01 06:30:28 -0700372 packet_router.AddSendRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700373
374 uint32_t bitrate_estimate = 456;
375 const std::vector<uint32_t> ssrcs = {1234};
376
nisse05843312017-04-18 23:38:35 -0700377 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
378
379 // Call OnReceiveBitrateChanged twice to get a first estimate.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200380 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200381 EXPECT_CALL(rtp, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700382 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
383
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200384 // Lower the estimate with more than 3% to trigger a call to SetRemb right
nisse05843312017-04-18 23:38:35 -0700385 // away.
386 bitrate_estimate = bitrate_estimate - 100;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200387 EXPECT_CALL(rtp, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700388 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
389
nisse05843312017-04-18 23:38:35 -0700390 packet_router.RemoveSendRtpModule(&rtp);
391}
392
393TEST(PacketRouterRembTest, VerifyIncreasingAndDecreasing) {
394 rtc::ScopedFakeClock clock;
eladalon6c9556e2017-07-10 03:33:00 -0700395 NiceMock<MockRtpRtcp> rtp;
nisse05843312017-04-18 23:38:35 -0700396 PacketRouter packet_router;
eladalon822ff2b2017-08-01 06:30:28 -0700397 packet_router.AddSendRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700398
399 uint32_t bitrate_estimate[] = {456, 789};
400 std::vector<uint32_t> ssrcs = {1234, 5678};
401
nisse05843312017-04-18 23:38:35 -0700402 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate[0]);
403
404 // Call OnReceiveBitrateChanged twice to get a first estimate.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200405 EXPECT_CALL(rtp, SetRemb(bitrate_estimate[0], ssrcs)).Times(1);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200406 clock.AdvanceTime(TimeDelta::ms(1000));
nisse05843312017-04-18 23:38:35 -0700407 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate[0]);
408
409 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate[1] + 100);
410
411 // Lower the estimate to trigger a callback.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200412 EXPECT_CALL(rtp, SetRemb(bitrate_estimate[1], ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700413 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate[1]);
414
415 packet_router.RemoveSendRtpModule(&rtp);
416}
417
418TEST(PacketRouterRembTest, NoRembForIncreasedBitrate) {
419 rtc::ScopedFakeClock clock;
eladalon6c9556e2017-07-10 03:33:00 -0700420 NiceMock<MockRtpRtcp> rtp;
nisse05843312017-04-18 23:38:35 -0700421 PacketRouter packet_router;
eladalon822ff2b2017-08-01 06:30:28 -0700422 packet_router.AddSendRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700423
424 uint32_t bitrate_estimate = 456;
425 std::vector<uint32_t> ssrcs = {1234, 5678};
426
nisse05843312017-04-18 23:38:35 -0700427 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
428
429 // Call OnReceiveBitrateChanged twice to get a first estimate.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200430 EXPECT_CALL(rtp, SetRemb(bitrate_estimate, ssrcs)).Times(1);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200431 clock.AdvanceTime(TimeDelta::ms(1000));
nisse05843312017-04-18 23:38:35 -0700432 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
433
434 // Increased estimate shouldn't trigger a callback right away.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200435 EXPECT_CALL(rtp, SetRemb(_, _)).Times(0);
nisse05843312017-04-18 23:38:35 -0700436 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate + 1);
437
438 // Decreasing the estimate less than 3% shouldn't trigger a new callback.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200439 EXPECT_CALL(rtp, SetRemb(_, _)).Times(0);
nisse05843312017-04-18 23:38:35 -0700440 int lower_estimate = bitrate_estimate * 98 / 100;
441 packet_router.OnReceiveBitrateChanged(ssrcs, lower_estimate);
442
443 packet_router.RemoveSendRtpModule(&rtp);
444}
445
446TEST(PacketRouterRembTest, ChangeSendRtpModule) {
447 rtc::ScopedFakeClock clock;
eladalon6c9556e2017-07-10 03:33:00 -0700448 NiceMock<MockRtpRtcp> rtp_send;
449 NiceMock<MockRtpRtcp> rtp_recv;
nisse05843312017-04-18 23:38:35 -0700450 PacketRouter packet_router;
eladalon822ff2b2017-08-01 06:30:28 -0700451 packet_router.AddSendRtpModule(&rtp_send, true);
452 packet_router.AddReceiveRtpModule(&rtp_recv, true);
nisse05843312017-04-18 23:38:35 -0700453
454 uint32_t bitrate_estimate = 456;
455 std::vector<uint32_t> ssrcs = {1234, 5678};
456
nisse05843312017-04-18 23:38:35 -0700457 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
458
459 // Call OnReceiveBitrateChanged twice to get a first estimate.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200460 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200461 EXPECT_CALL(rtp_send, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700462 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
463
464 // Decrease estimate to trigger a REMB.
465 bitrate_estimate = bitrate_estimate - 100;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200466 EXPECT_CALL(rtp_send, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700467 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
468
469 // Remove the sending module -> should get remb on the second module.
470 packet_router.RemoveSendRtpModule(&rtp_send);
471
nisse05843312017-04-18 23:38:35 -0700472 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
473
474 bitrate_estimate = bitrate_estimate - 100;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200475 EXPECT_CALL(rtp_recv, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700476 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
477
478 packet_router.RemoveReceiveRtpModule(&rtp_recv);
479}
480
481TEST(PacketRouterRembTest, OnlyOneRembForRepeatedOnReceiveBitrateChanged) {
482 rtc::ScopedFakeClock clock;
eladalon6c9556e2017-07-10 03:33:00 -0700483 NiceMock<MockRtpRtcp> rtp;
nisse05843312017-04-18 23:38:35 -0700484 PacketRouter packet_router;
eladalon822ff2b2017-08-01 06:30:28 -0700485 packet_router.AddSendRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700486
487 uint32_t bitrate_estimate = 456;
488 const std::vector<uint32_t> ssrcs = {1234};
489
nisse05843312017-04-18 23:38:35 -0700490 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
491
492 // Call OnReceiveBitrateChanged twice to get a first estimate.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200493 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200494 EXPECT_CALL(rtp, SetRemb(_, _)).Times(1);
nisse05843312017-04-18 23:38:35 -0700495 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
496
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200497 // Lower the estimate, should trigger a call to SetRemb right away.
nisse05843312017-04-18 23:38:35 -0700498 bitrate_estimate = bitrate_estimate - 100;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200499 EXPECT_CALL(rtp, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700500 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
501
502 // Call OnReceiveBitrateChanged again, this should not trigger a new callback.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200503 EXPECT_CALL(rtp, SetRemb(_, _)).Times(0);
nisse05843312017-04-18 23:38:35 -0700504 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
505 packet_router.RemoveSendRtpModule(&rtp);
506}
507
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200508TEST(PacketRouterRembTest, SetMaxDesiredReceiveBitrateLimitsSetRemb) {
danilchap47085372017-08-10 06:03:57 -0700509 rtc::ScopedFakeClock clock;
510 PacketRouter packet_router;
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200511 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200512 NiceMock<MockRtpRtcp> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700513 constexpr bool remb_candidate = true;
514 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700515
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100516 const int64_t cap_bitrate = 100000;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200517 EXPECT_CALL(remb_sender, SetRemb(Le(cap_bitrate), _)).Times(AtLeast(1));
518 EXPECT_CALL(remb_sender, SetRemb(Gt(cap_bitrate), _)).Times(0);
danilchap47085372017-08-10 06:03:57 -0700519
520 const std::vector<uint32_t> ssrcs = {1234};
521 packet_router.SetMaxDesiredReceiveBitrate(cap_bitrate);
522 packet_router.OnReceiveBitrateChanged(ssrcs, cap_bitrate + 5000);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200523 clock.AdvanceTime(TimeDelta::ms(1000));
danilchap47085372017-08-10 06:03:57 -0700524 packet_router.OnReceiveBitrateChanged(ssrcs, cap_bitrate - 5000);
525
526 // Test tear-down.
527 packet_router.RemoveSendRtpModule(&remb_sender);
528}
529
530TEST(PacketRouterRembTest,
531 SetMaxDesiredReceiveBitrateTriggersRembWhenMoreRestrictive) {
532 rtc::ScopedFakeClock clock;
533 PacketRouter packet_router;
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200534 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200535 NiceMock<MockRtpRtcp> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700536 constexpr bool remb_candidate = true;
537 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700538
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100539 const int64_t measured_bitrate_bps = 150000;
540 const int64_t cap_bitrate_bps = measured_bitrate_bps - 5000;
danilchap47085372017-08-10 06:03:57 -0700541 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200542 EXPECT_CALL(remb_sender, SetRemb(measured_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700543 packet_router.OnReceiveBitrateChanged(ssrcs, measured_bitrate_bps);
544
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200545 EXPECT_CALL(remb_sender, SetRemb(cap_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700546 packet_router.SetMaxDesiredReceiveBitrate(cap_bitrate_bps);
547
548 // Test tear-down.
549 packet_router.RemoveSendRtpModule(&remb_sender);
550}
551
552TEST(PacketRouterRembTest,
553 SetMaxDesiredReceiveBitrateDoesNotTriggerRembWhenAsRestrictive) {
554 rtc::ScopedFakeClock clock;
555 PacketRouter packet_router;
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200556 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200557 NiceMock<MockRtpRtcp> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700558 constexpr bool remb_candidate = true;
559 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700560
561 const uint32_t measured_bitrate_bps = 150000;
562 const uint32_t cap_bitrate_bps = measured_bitrate_bps;
563 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200564 EXPECT_CALL(remb_sender, SetRemb(measured_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700565 packet_router.OnReceiveBitrateChanged(ssrcs, measured_bitrate_bps);
566
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200567 EXPECT_CALL(remb_sender, SetRemb(_, _)).Times(0);
danilchap47085372017-08-10 06:03:57 -0700568 packet_router.SetMaxDesiredReceiveBitrate(cap_bitrate_bps);
569
570 // Test tear-down.
571 packet_router.RemoveSendRtpModule(&remb_sender);
572}
573
574TEST(PacketRouterRembTest,
575 SetMaxDesiredReceiveBitrateDoesNotTriggerRembWhenLessRestrictive) {
576 rtc::ScopedFakeClock clock;
577 PacketRouter packet_router;
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200578 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200579 NiceMock<MockRtpRtcp> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700580 constexpr bool remb_candidate = true;
581 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700582
583 const uint32_t measured_bitrate_bps = 150000;
584 const uint32_t cap_bitrate_bps = measured_bitrate_bps + 500;
585 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200586 EXPECT_CALL(remb_sender, SetRemb(measured_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700587 packet_router.OnReceiveBitrateChanged(ssrcs, measured_bitrate_bps);
588
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200589 EXPECT_CALL(remb_sender, SetRemb(_, _)).Times(0);
danilchap47085372017-08-10 06:03:57 -0700590 packet_router.SetMaxDesiredReceiveBitrate(cap_bitrate_bps);
591
592 // Test tear-down.
593 packet_router.RemoveSendRtpModule(&remb_sender);
594}
595
596TEST(PacketRouterRembTest,
597 SetMaxDesiredReceiveBitrateTriggersRembWhenNoRecentMeasure) {
598 rtc::ScopedFakeClock clock;
599 PacketRouter packet_router;
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200600 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200601 NiceMock<MockRtpRtcp> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700602 constexpr bool remb_candidate = true;
603 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700604
605 const uint32_t measured_bitrate_bps = 150000;
606 const uint32_t cap_bitrate_bps = measured_bitrate_bps + 5000;
607 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200608 EXPECT_CALL(remb_sender, SetRemb(measured_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700609 packet_router.OnReceiveBitrateChanged(ssrcs, measured_bitrate_bps);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200610 clock.AdvanceTime(TimeDelta::ms(1000));
danilchap47085372017-08-10 06:03:57 -0700611
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200612 EXPECT_CALL(remb_sender, SetRemb(cap_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700613 packet_router.SetMaxDesiredReceiveBitrate(cap_bitrate_bps);
614
615 // Test tear-down.
616 packet_router.RemoveSendRtpModule(&remb_sender);
617}
618
619TEST(PacketRouterRembTest,
620 SetMaxDesiredReceiveBitrateTriggersRembWhenNoMeasures) {
621 rtc::ScopedFakeClock clock;
622 PacketRouter packet_router;
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200623 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200624 NiceMock<MockRtpRtcp> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700625 constexpr bool remb_candidate = true;
626 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700627
628 // Set cap.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200629 EXPECT_CALL(remb_sender, SetRemb(100000, _)).Times(1);
danilchap47085372017-08-10 06:03:57 -0700630 packet_router.SetMaxDesiredReceiveBitrate(100000);
631 // Increase cap.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200632 EXPECT_CALL(remb_sender, SetRemb(200000, _)).Times(1);
danilchap47085372017-08-10 06:03:57 -0700633 packet_router.SetMaxDesiredReceiveBitrate(200000);
634 // Decrease cap.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200635 EXPECT_CALL(remb_sender, SetRemb(150000, _)).Times(1);
danilchap47085372017-08-10 06:03:57 -0700636 packet_router.SetMaxDesiredReceiveBitrate(150000);
637
638 // Test tear-down.
639 packet_router.RemoveSendRtpModule(&remb_sender);
640}
641
nisse05843312017-04-18 23:38:35 -0700642// Only register receiving modules and make sure we fallback to trigger a REMB
643// packet on this one.
644TEST(PacketRouterRembTest, NoSendingRtpModule) {
645 rtc::ScopedFakeClock clock;
eladalon6c9556e2017-07-10 03:33:00 -0700646 NiceMock<MockRtpRtcp> rtp;
nisse05843312017-04-18 23:38:35 -0700647 PacketRouter packet_router;
648
eladalon822ff2b2017-08-01 06:30:28 -0700649 packet_router.AddReceiveRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700650
651 uint32_t bitrate_estimate = 456;
652 const std::vector<uint32_t> ssrcs = {1234};
653
nisse05843312017-04-18 23:38:35 -0700654 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
655
656 // Call OnReceiveBitrateChanged twice to get a first estimate.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200657 clock.AdvanceTime(TimeDelta::ms(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200658 EXPECT_CALL(rtp, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700659 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
660
661 // Lower the estimate to trigger a new packet REMB packet.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200662 EXPECT_CALL(rtp, SetRemb(bitrate_estimate - 100, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700663 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate - 100);
664
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200665 EXPECT_CALL(rtp, UnsetRemb()).Times(1);
nisse05843312017-04-18 23:38:35 -0700666 packet_router.RemoveReceiveRtpModule(&rtp);
667}
668
eladalon822ff2b2017-08-01 06:30:28 -0700669TEST(PacketRouterRembTest, NonCandidateSendRtpModuleNotUsedForRemb) {
670 rtc::ScopedFakeClock clock;
671 PacketRouter packet_router;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200672 NiceMock<MockRtpRtcp> module;
eladalon822ff2b2017-08-01 06:30:28 -0700673
674 constexpr bool remb_candidate = false;
675
676 packet_router.AddSendRtpModule(&module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700677
678 constexpr uint32_t bitrate_estimate = 456;
679 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200680 EXPECT_CALL(module, SetRemb(_, _)).Times(0);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200681 clock.AdvanceTime(TimeDelta::ms(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700682 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
683
684 // Test tear-down
685 packet_router.RemoveSendRtpModule(&module);
686}
687
688TEST(PacketRouterRembTest, CandidateSendRtpModuleUsedForRemb) {
689 rtc::ScopedFakeClock clock;
690 PacketRouter packet_router;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200691 NiceMock<MockRtpRtcp> module;
eladalon822ff2b2017-08-01 06:30:28 -0700692
693 constexpr bool remb_candidate = true;
694
695 packet_router.AddSendRtpModule(&module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700696
697 constexpr uint32_t bitrate_estimate = 456;
698 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200699 EXPECT_CALL(module, SetRemb(bitrate_estimate, ssrcs)).Times(1);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200700 clock.AdvanceTime(TimeDelta::ms(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700701 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
702
703 // Test tear-down
704 packet_router.RemoveSendRtpModule(&module);
705}
706
707TEST(PacketRouterRembTest, NonCandidateReceiveRtpModuleNotUsedForRemb) {
708 rtc::ScopedFakeClock clock;
709 PacketRouter packet_router;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200710 NiceMock<MockRtpRtcp> module;
eladalon822ff2b2017-08-01 06:30:28 -0700711
712 constexpr bool remb_candidate = false;
713
714 packet_router.AddReceiveRtpModule(&module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700715
716 constexpr uint32_t bitrate_estimate = 456;
717 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200718 EXPECT_CALL(module, SetRemb(_, _)).Times(0);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200719 clock.AdvanceTime(TimeDelta::ms(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700720 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
721
722 // Test tear-down
723 packet_router.RemoveReceiveRtpModule(&module);
724}
725
726TEST(PacketRouterRembTest, CandidateReceiveRtpModuleUsedForRemb) {
727 rtc::ScopedFakeClock clock;
728 PacketRouter packet_router;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200729 NiceMock<MockRtpRtcp> module;
eladalon822ff2b2017-08-01 06:30:28 -0700730
731 constexpr bool remb_candidate = true;
732
733 packet_router.AddReceiveRtpModule(&module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700734
735 constexpr uint32_t bitrate_estimate = 456;
736 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200737 EXPECT_CALL(module, SetRemb(bitrate_estimate, ssrcs)).Times(1);
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200738 clock.AdvanceTime(TimeDelta::ms(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700739 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
740
741 // Test tear-down
742 packet_router.RemoveReceiveRtpModule(&module);
743}
744
745TEST(PacketRouterRembTest,
746 SendCandidatePreferredOverReceiveCandidate_SendModuleAddedFirst) {
747 rtc::ScopedFakeClock clock;
748 PacketRouter packet_router;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200749 NiceMock<MockRtpRtcp> send_module;
750 NiceMock<MockRtpRtcp> receive_module;
eladalon822ff2b2017-08-01 06:30:28 -0700751
752 constexpr bool remb_candidate = true;
753
754 // Send module added - activated.
755 packet_router.AddSendRtpModule(&send_module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700756
757 // Receive module added - the send module remains the active one.
758 packet_router.AddReceiveRtpModule(&receive_module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700759
760 constexpr uint32_t bitrate_estimate = 456;
761 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200762 EXPECT_CALL(send_module, SetRemb(bitrate_estimate, ssrcs)).Times(1);
763 EXPECT_CALL(receive_module, SetRemb(_, _)).Times(0);
eladalon822ff2b2017-08-01 06:30:28 -0700764
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200765 clock.AdvanceTime(TimeDelta::ms(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700766 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
767
768 // Test tear-down
769 packet_router.RemoveReceiveRtpModule(&receive_module);
770 packet_router.RemoveSendRtpModule(&send_module);
771}
772
773TEST(PacketRouterRembTest,
774 SendCandidatePreferredOverReceiveCandidate_ReceiveModuleAddedFirst) {
775 rtc::ScopedFakeClock clock;
776 PacketRouter packet_router;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200777 NiceMock<MockRtpRtcp> send_module;
778 NiceMock<MockRtpRtcp> receive_module;
eladalon822ff2b2017-08-01 06:30:28 -0700779
780 constexpr bool remb_candidate = true;
781
782 // Receive module added - activated.
783 packet_router.AddReceiveRtpModule(&receive_module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700784
785 // Send module added - replaces receive module as active.
786 packet_router.AddSendRtpModule(&send_module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700787
788 constexpr uint32_t bitrate_estimate = 456;
789 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200790 EXPECT_CALL(send_module, SetRemb(bitrate_estimate, ssrcs)).Times(1);
791 EXPECT_CALL(receive_module, SetRemb(_, _)).Times(0);
eladalon822ff2b2017-08-01 06:30:28 -0700792
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200793 clock.AdvanceTime(TimeDelta::ms(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700794 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
795
796 // Test tear-down
797 packet_router.RemoveReceiveRtpModule(&receive_module);
798 packet_router.RemoveSendRtpModule(&send_module);
799}
800
801TEST(PacketRouterRembTest, ReceiveModuleTakesOverWhenLastSendModuleRemoved) {
802 rtc::ScopedFakeClock clock;
803 PacketRouter packet_router;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200804 NiceMock<MockRtpRtcp> send_module;
805 NiceMock<MockRtpRtcp> receive_module;
eladalon822ff2b2017-08-01 06:30:28 -0700806
807 constexpr bool remb_candidate = true;
808
809 // Send module active, receive module inactive.
810 packet_router.AddSendRtpModule(&send_module, remb_candidate);
811 packet_router.AddReceiveRtpModule(&receive_module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700812
813 // Send module removed - receive module becomes active.
814 packet_router.RemoveSendRtpModule(&send_module);
eladalon822ff2b2017-08-01 06:30:28 -0700815 constexpr uint32_t bitrate_estimate = 456;
816 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200817 EXPECT_CALL(send_module, SetRemb(_, _)).Times(0);
818 EXPECT_CALL(receive_module, SetRemb(bitrate_estimate, ssrcs)).Times(1);
eladalon822ff2b2017-08-01 06:30:28 -0700819
Sebastian Jansson5f83cf02018-05-08 14:52:22 +0200820 clock.AdvanceTime(TimeDelta::ms(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700821 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
822
823 // Test tear-down
824 packet_router.RemoveReceiveRtpModule(&receive_module);
825}
826
Stefan Holmere5904162015-03-26 11:11:06 +0100827} // namespace webrtc