blob: 10cf98b3ddb6c0ec619d88783feb88da97e59a1a [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) {
Erik Språnged1fb192020-06-30 11:53:37 +000071 constexpr DataSize bytes = DataSize::Bytes(300);
eladalon32040ef2017-08-02 06:29:00 -070072 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ång1e51a382019-12-11 16:47:09 +010098TEST_F(PacketRouterTest, GeneratePaddingPrioritizesRtx) {
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
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200104 NiceMock<MockRtpRtcpInterface> rtp_1;
Erik Språng478cb462019-06-26 15:49:27 +0200105 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
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200109 NiceMock<MockRtpRtcpInterface> rtp_2;
Erik Språng478cb462019-06-26 15:49:27 +0200110 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ånged1fb192020-06-30 11:53:37 +0000125 auto generated_padding =
126 packet_router_.GeneratePadding(DataSize::Bytes(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ång1e51a382019-12-11 16:47:09 +0100133TEST_F(PacketRouterTest, GeneratePaddingPrioritizesVideo) {
134 // Two RTP modules. Neither support RTX, both support padding,
135 // but the first one is for audio and second for video.
136 const uint16_t kSsrc1 = 1234;
137 const uint16_t kSsrc2 = 4567;
138 const size_t kPaddingSize = 123;
139 const size_t kExpectedPaddingPackets = 1;
140
141 auto generate_padding = [&](size_t padding_size) {
142 return std::vector<std::unique_ptr<RtpPacketToSend>>(
143 kExpectedPaddingPackets);
144 };
145
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200146 NiceMock<MockRtpRtcpInterface> audio_module;
Erik Språng1e51a382019-12-11 16:47:09 +0100147 ON_CALL(audio_module, RtxSendStatus()).WillByDefault(Return(kRtxOff));
148 ON_CALL(audio_module, SSRC()).WillByDefault(Return(kSsrc1));
149 ON_CALL(audio_module, SupportsPadding).WillByDefault(Return(true));
150 ON_CALL(audio_module, IsAudioConfigured).WillByDefault(Return(true));
151
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200152 NiceMock<MockRtpRtcpInterface> video_module;
Erik Språng1e51a382019-12-11 16:47:09 +0100153 ON_CALL(video_module, RtxSendStatus()).WillByDefault(Return(kRtxOff));
154 ON_CALL(video_module, SSRC()).WillByDefault(Return(kSsrc2));
155 ON_CALL(video_module, SupportsPadding).WillByDefault(Return(true));
156 ON_CALL(video_module, IsAudioConfigured).WillByDefault(Return(false));
157
158 // First add only the audio module. Since this is the only choice we have,
159 // padding should be sent on the audio ssrc.
160 packet_router_.AddSendRtpModule(&audio_module, false);
161 EXPECT_CALL(audio_module, GeneratePadding(kPaddingSize))
162 .WillOnce(generate_padding);
Erik Språnged1fb192020-06-30 11:53:37 +0000163 packet_router_.GeneratePadding(DataSize::Bytes(kPaddingSize));
Erik Språng1e51a382019-12-11 16:47:09 +0100164
165 // Add the video module, this should now be prioritized since we cannot
166 // guarantee that audio packets will be included in the BWE.
167 packet_router_.AddSendRtpModule(&video_module, false);
168 EXPECT_CALL(audio_module, GeneratePadding).Times(0);
169 EXPECT_CALL(video_module, GeneratePadding(kPaddingSize))
170 .WillOnce(generate_padding);
Erik Språnged1fb192020-06-30 11:53:37 +0000171 packet_router_.GeneratePadding(DataSize::Bytes(kPaddingSize));
Erik Språng1e51a382019-12-11 16:47:09 +0100172
173 // Remove and the add audio module again. Module order shouldn't matter;
174 // video should still be prioritized.
175 packet_router_.RemoveSendRtpModule(&audio_module);
176 packet_router_.AddSendRtpModule(&audio_module, false);
177 EXPECT_CALL(audio_module, GeneratePadding).Times(0);
178 EXPECT_CALL(video_module, GeneratePadding(kPaddingSize))
179 .WillOnce(generate_padding);
Erik Språnged1fb192020-06-30 11:53:37 +0000180 packet_router_.GeneratePadding(DataSize::Bytes(kPaddingSize));
Erik Språng1e51a382019-12-11 16:47:09 +0100181
182 // Remove and the video module, we should fall back to padding on the
183 // audio module again.
184 packet_router_.RemoveSendRtpModule(&video_module);
185 EXPECT_CALL(audio_module, GeneratePadding(kPaddingSize))
186 .WillOnce(generate_padding);
Erik Språnged1fb192020-06-30 11:53:37 +0000187 packet_router_.GeneratePadding(DataSize::Bytes(kPaddingSize));
Erik Språng1e51a382019-12-11 16:47:09 +0100188
189 packet_router_.RemoveSendRtpModule(&audio_module);
190}
191
Erik Språng4208a132019-08-26 08:58:45 +0200192TEST_F(PacketRouterTest, PadsOnLastActiveMediaStream) {
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200193 const uint16_t kSsrc1 = 1234;
194 const uint16_t kSsrc2 = 4567;
195 const uint16_t kSsrc3 = 8901;
196
197 // First two rtp modules send media and have rtx.
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200198 NiceMock<MockRtpRtcpInterface> rtp_1;
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200199 EXPECT_CALL(rtp_1, SSRC()).WillRepeatedly(Return(kSsrc1));
Mirko Bonadei999a72a2019-07-12 17:33:46 +0000200 EXPECT_CALL(rtp_1, SupportsPadding).WillRepeatedly(Return(true));
201 EXPECT_CALL(rtp_1, SupportsRtxPayloadPadding).WillRepeatedly(Return(true));
Erik Språng4208a132019-08-26 08:58:45 +0200202 EXPECT_CALL(rtp_1, TrySendPacket).WillRepeatedly(Return(false));
203 EXPECT_CALL(
204 rtp_1,
205 TrySendPacket(
206 ::testing::Pointee(Property(&RtpPacketToSend::Ssrc, kSsrc1)), _))
207 .WillRepeatedly(Return(true));
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200208
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200209 NiceMock<MockRtpRtcpInterface> rtp_2;
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200210 EXPECT_CALL(rtp_2, SSRC()).WillRepeatedly(Return(kSsrc2));
Mirko Bonadei999a72a2019-07-12 17:33:46 +0000211 EXPECT_CALL(rtp_2, SupportsPadding).WillRepeatedly(Return(true));
212 EXPECT_CALL(rtp_2, SupportsRtxPayloadPadding).WillRepeatedly(Return(true));
Erik Språng4208a132019-08-26 08:58:45 +0200213 EXPECT_CALL(rtp_2, TrySendPacket).WillRepeatedly(Return(false));
214 EXPECT_CALL(
215 rtp_2,
216 TrySendPacket(
217 ::testing::Pointee(Property(&RtpPacketToSend::Ssrc, kSsrc2)), _))
218 .WillRepeatedly(Return(true));
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200219
220 // Third module is sending media, but does not support rtx.
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200221 NiceMock<MockRtpRtcpInterface> rtp_3;
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200222 EXPECT_CALL(rtp_3, SSRC()).WillRepeatedly(Return(kSsrc3));
Mirko Bonadei999a72a2019-07-12 17:33:46 +0000223 EXPECT_CALL(rtp_3, SupportsPadding).WillRepeatedly(Return(true));
Erik Språng4208a132019-08-26 08:58:45 +0200224 EXPECT_CALL(rtp_3, SupportsRtxPayloadPadding).WillRepeatedly(Return(false));
225 EXPECT_CALL(rtp_3, TrySendPacket).WillRepeatedly(Return(false));
226 EXPECT_CALL(
227 rtp_3,
228 TrySendPacket(
229 ::testing::Pointee(Property(&RtpPacketToSend::Ssrc, kSsrc3)), _))
230 .WillRepeatedly(Return(true));
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200231
Erik Språng4208a132019-08-26 08:58:45 +0200232 packet_router_.AddSendRtpModule(&rtp_1, false);
233 packet_router_.AddSendRtpModule(&rtp_2, false);
234 packet_router_.AddSendRtpModule(&rtp_3, false);
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200235
236 const size_t kPaddingBytes = 100;
237
238 // Initially, padding will be sent on last added rtp module that sends media
239 // and supports rtx.
Erik Språng4208a132019-08-26 08:58:45 +0200240 EXPECT_CALL(rtp_2, GeneratePadding(kPaddingBytes))
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200241 .Times(1)
Erik Språngc06aef22019-10-17 13:02:27 +0200242 .WillOnce([&](size_t target_size_bytes) {
243 std::vector<std::unique_ptr<RtpPacketToSend>> packets;
244 packets.push_back(BuildRtpPacket(kSsrc2));
245 return packets;
Erik Språng4208a132019-08-26 08:58:45 +0200246 });
Erik Språnged1fb192020-06-30 11:53:37 +0000247 packet_router_.GeneratePadding(DataSize::Bytes(kPaddingBytes));
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200248
249 // Send media on first module. Padding should be sent on that module.
Erik Språng4208a132019-08-26 08:58:45 +0200250 packet_router_.SendPacket(BuildRtpPacket(kSsrc1), PacedPacketInfo());
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200251
Erik Språng4208a132019-08-26 08:58:45 +0200252 EXPECT_CALL(rtp_1, GeneratePadding(kPaddingBytes))
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200253 .Times(1)
Erik Språngc06aef22019-10-17 13:02:27 +0200254 .WillOnce([&](size_t target_size_bytes) {
255 std::vector<std::unique_ptr<RtpPacketToSend>> packets;
256 packets.push_back(BuildRtpPacket(kSsrc1));
257 return packets;
Erik Språng4208a132019-08-26 08:58:45 +0200258 });
Erik Språnged1fb192020-06-30 11:53:37 +0000259 packet_router_.GeneratePadding(DataSize::Bytes(kPaddingBytes));
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200260
261 // Send media on second module. Padding should be sent there.
Erik Språng4208a132019-08-26 08:58:45 +0200262 packet_router_.SendPacket(BuildRtpPacket(kSsrc2), PacedPacketInfo());
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200263
Erik Språngc06aef22019-10-17 13:02:27 +0200264 // If the last active module is removed, and no module sends media before
265 // the next padding request, and arbitrary module will be selected.
Erik Språngfbe84ef2019-10-17 11:17:06 +0000266 packet_router_.RemoveSendRtpModule(&rtp_2);
Erik Språngc06aef22019-10-17 13:02:27 +0200267
268 // Send on and then remove all remaining modules.
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200269 RtpRtcpInterface* last_send_module;
Erik Språng4208a132019-08-26 08:58:45 +0200270 EXPECT_CALL(rtp_1, GeneratePadding(kPaddingBytes))
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200271 .Times(1)
Erik Språngc06aef22019-10-17 13:02:27 +0200272 .WillOnce([&](size_t target_size_bytes) {
273 last_send_module = &rtp_1;
274 std::vector<std::unique_ptr<RtpPacketToSend>> packets;
275 packets.push_back(BuildRtpPacket(kSsrc1));
276 return packets;
Erik Språng4208a132019-08-26 08:58:45 +0200277 });
Erik Språng4208a132019-08-26 08:58:45 +0200278 EXPECT_CALL(rtp_3, GeneratePadding(kPaddingBytes))
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200279 .Times(1)
Erik Språngc06aef22019-10-17 13:02:27 +0200280 .WillOnce([&](size_t target_size_bytes) {
281 last_send_module = &rtp_3;
282 std::vector<std::unique_ptr<RtpPacketToSend>> packets;
283 packets.push_back(BuildRtpPacket(kSsrc3));
284 return packets;
Erik Språng4208a132019-08-26 08:58:45 +0200285 });
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200286
Erik Språngc06aef22019-10-17 13:02:27 +0200287 for (int i = 0; i < 2; ++i) {
288 last_send_module = nullptr;
Erik Språnged1fb192020-06-30 11:53:37 +0000289 packet_router_.GeneratePadding(DataSize::Bytes(kPaddingBytes));
Erik Språngc06aef22019-10-17 13:02:27 +0200290 EXPECT_NE(last_send_module, nullptr);
291 packet_router_.RemoveSendRtpModule(last_send_module);
292 }
Erik Språng8b7ca4a2018-05-17 13:43:35 +0200293}
294
Erik Språng5f01bf62019-10-16 17:06:34 +0200295TEST_F(PacketRouterTest, AllocatesTransportSequenceNumbers) {
sprang867fb522015-08-03 04:38:41 -0700296 const uint16_t kStartSeq = 0xFFF0;
297 const size_t kNumPackets = 32;
Erik Språng5f01bf62019-10-16 17:06:34 +0200298 const uint16_t kSsrc1 = 1234;
sprang867fb522015-08-03 04:38:41 -0700299
Erik Språng5f01bf62019-10-16 17:06:34 +0200300 PacketRouter packet_router(kStartSeq - 1);
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200301 NiceMock<MockRtpRtcpInterface> rtp_1;
Erik Språng5f01bf62019-10-16 17:06:34 +0200302 EXPECT_CALL(rtp_1, SSRC()).WillRepeatedly(Return(kSsrc1));
303 EXPECT_CALL(rtp_1, TrySendPacket).WillRepeatedly(Return(true));
304 packet_router.AddSendRtpModule(&rtp_1, false);
sprang867fb522015-08-03 04:38:41 -0700305
306 for (size_t i = 0; i < kNumPackets; ++i) {
Erik Språng5f01bf62019-10-16 17:06:34 +0200307 auto packet = BuildRtpPacket(kSsrc1);
308 EXPECT_TRUE(packet->ReserveExtension<TransportSequenceNumber>());
309 packet_router.SendPacket(std::move(packet), PacedPacketInfo());
sprang867fb522015-08-03 04:38:41 -0700310 uint32_t expected_unwrapped_seq = static_cast<uint32_t>(kStartSeq) + i;
Erik Språng5f01bf62019-10-16 17:06:34 +0200311 EXPECT_EQ(static_cast<uint16_t>(expected_unwrapped_seq & 0xFFFF),
312 packet_router.CurrentTransportSequenceNumber());
sprang867fb522015-08-03 04:38:41 -0700313 }
Erik Språng5f01bf62019-10-16 17:06:34 +0200314
315 packet_router.RemoveSendRtpModule(&rtp_1);
sprang867fb522015-08-03 04:38:41 -0700316}
stefanbba9dec2016-02-01 04:39:55 -0800317
Erik Språng4208a132019-08-26 08:58:45 +0200318TEST_F(PacketRouterTest, SendTransportFeedback) {
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200319 NiceMock<MockRtpRtcpInterface> rtp_1;
320 NiceMock<MockRtpRtcpInterface> rtp_2;
eladalonb1338fe2017-08-01 09:36:19 -0700321
Per Kjellanderee153c92019-10-10 16:43:46 +0200322 ON_CALL(rtp_1, RTCP()).WillByDefault(Return(RtcpMode::kCompound));
323 ON_CALL(rtp_2, RTCP()).WillByDefault(Return(RtcpMode::kCompound));
324
Erik Språng4208a132019-08-26 08:58:45 +0200325 packet_router_.AddSendRtpModule(&rtp_1, false);
326 packet_router_.AddReceiveRtpModule(&rtp_2, false);
stefanbba9dec2016-02-01 04:39:55 -0800327
Per Kjellanderee153c92019-10-10 16:43:46 +0200328 std::vector<std::unique_ptr<rtcp::RtcpPacket>> feedback;
329 feedback.push_back(std::make_unique<rtcp::TransportFeedback>());
330 EXPECT_CALL(rtp_1, SendCombinedRtcpPacket).Times(1);
331 packet_router_.SendCombinedRtcpPacket(std::move(feedback));
Erik Språng4208a132019-08-26 08:58:45 +0200332 packet_router_.RemoveSendRtpModule(&rtp_1);
Per Kjellanderee153c92019-10-10 16:43:46 +0200333 EXPECT_CALL(rtp_2, SendCombinedRtcpPacket).Times(1);
334 std::vector<std::unique_ptr<rtcp::RtcpPacket>> new_feedback;
335 new_feedback.push_back(std::make_unique<rtcp::TransportFeedback>());
336 packet_router_.SendCombinedRtcpPacket(std::move(new_feedback));
Erik Språng4208a132019-08-26 08:58:45 +0200337 packet_router_.RemoveReceiveRtpModule(&rtp_2);
338}
339
340TEST_F(PacketRouterTest, SendPacketWithoutTransportSequenceNumbers) {
Erik Språngfbe84ef2019-10-17 11:17:06 +0000341 const uint16_t kSsrc1 = 1234;
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200342 NiceMock<MockRtpRtcpInterface> rtp_1;
Erik Språng4208a132019-08-26 08:58:45 +0200343 ON_CALL(rtp_1, SendingMedia).WillByDefault(Return(true));
344 ON_CALL(rtp_1, SSRC).WillByDefault(Return(kSsrc1));
Erik Språngc06aef22019-10-17 13:02:27 +0200345 packet_router_.AddSendRtpModule(&rtp_1, false);
Erik Språng4208a132019-08-26 08:58:45 +0200346
347 // Send a packet without TransportSequenceNumber extension registered,
348 // packets sent should not have the extension set.
349 RtpHeaderExtensionMap extension_manager;
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200350 auto packet = std::make_unique<RtpPacketToSend>(&extension_manager);
Erik Språng4208a132019-08-26 08:58:45 +0200351 packet->SetSsrc(kSsrc1);
352 EXPECT_CALL(
353 rtp_1,
354 TrySendPacket(
355 Property(&RtpPacketToSend::HasExtension<TransportSequenceNumber>,
356 false),
357 _))
358 .WillOnce(Return(true));
359 packet_router_.SendPacket(std::move(packet), PacedPacketInfo());
360
361 packet_router_.RemoveSendRtpModule(&rtp_1);
362}
363
364TEST_F(PacketRouterTest, SendPacketAssignsTransportSequenceNumbers) {
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200365 NiceMock<MockRtpRtcpInterface> rtp_1;
366 NiceMock<MockRtpRtcpInterface> rtp_2;
Erik Språng4208a132019-08-26 08:58:45 +0200367
Erik Språng4208a132019-08-26 08:58:45 +0200368 const uint16_t kSsrc1 = 1234;
369 const uint16_t kSsrc2 = 2345;
370
371 ON_CALL(rtp_1, SSRC).WillByDefault(Return(kSsrc1));
372 ON_CALL(rtp_2, SSRC).WillByDefault(Return(kSsrc2));
373
Erik Språngc06aef22019-10-17 13:02:27 +0200374 packet_router_.AddSendRtpModule(&rtp_1, false);
375 packet_router_.AddSendRtpModule(&rtp_2, false);
376
Erik Språng4208a132019-08-26 08:58:45 +0200377 // Transport sequence numbers start at 1, for historical reasons.
378 uint16_t transport_sequence_number = 1;
379
380 auto packet = BuildRtpPacket(kSsrc1);
381 EXPECT_TRUE(packet->ReserveExtension<TransportSequenceNumber>());
382 EXPECT_CALL(
383 rtp_1,
384 TrySendPacket(
385 Property(&RtpPacketToSend::GetExtension<TransportSequenceNumber>,
386 transport_sequence_number),
387 _))
388 .WillOnce(Return(true));
389 packet_router_.SendPacket(std::move(packet), PacedPacketInfo());
390
391 ++transport_sequence_number;
392 packet = BuildRtpPacket(kSsrc2);
393 EXPECT_TRUE(packet->ReserveExtension<TransportSequenceNumber>());
394
Erik Språng4208a132019-08-26 08:58:45 +0200395 EXPECT_CALL(
396 rtp_2,
397 TrySendPacket(
398 Property(&RtpPacketToSend::GetExtension<TransportSequenceNumber>,
399 transport_sequence_number),
400 _))
401 .WillOnce(Return(true));
402 packet_router_.SendPacket(std::move(packet), PacedPacketInfo());
403
404 packet_router_.RemoveSendRtpModule(&rtp_1);
405 packet_router_.RemoveSendRtpModule(&rtp_2);
stefanbba9dec2016-02-01 04:39:55 -0800406}
nisse05843312017-04-18 23:38:35 -0700407
eladalon822ff2b2017-08-01 06:30:28 -0700408#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
Tommia5e07cc2020-05-26 21:40:37 +0200409using PacketRouterDeathTest = PacketRouterTest;
410TEST_F(PacketRouterDeathTest, DoubleRegistrationOfSendModuleDisallowed) {
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200411 NiceMock<MockRtpRtcpInterface> module;
eladalon822ff2b2017-08-01 06:30:28 -0700412
413 constexpr bool remb_candidate = false; // Value irrelevant.
Erik Språng4208a132019-08-26 08:58:45 +0200414 packet_router_.AddSendRtpModule(&module, remb_candidate);
415 EXPECT_DEATH(packet_router_.AddSendRtpModule(&module, remb_candidate), "");
eladalon822ff2b2017-08-01 06:30:28 -0700416
417 // Test tear-down
Erik Språng4208a132019-08-26 08:58:45 +0200418 packet_router_.RemoveSendRtpModule(&module);
eladalon822ff2b2017-08-01 06:30:28 -0700419}
420
Tommia5e07cc2020-05-26 21:40:37 +0200421TEST_F(PacketRouterDeathTest, DoubleRegistrationOfReceiveModuleDisallowed) {
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200422 NiceMock<MockRtpRtcpInterface> module;
eladalon822ff2b2017-08-01 06:30:28 -0700423
424 constexpr bool remb_candidate = false; // Value irrelevant.
Erik Språng4208a132019-08-26 08:58:45 +0200425 packet_router_.AddReceiveRtpModule(&module, remb_candidate);
426 EXPECT_DEATH(packet_router_.AddReceiveRtpModule(&module, remb_candidate), "");
eladalon822ff2b2017-08-01 06:30:28 -0700427
428 // Test tear-down
Erik Språng4208a132019-08-26 08:58:45 +0200429 packet_router_.RemoveReceiveRtpModule(&module);
eladalon822ff2b2017-08-01 06:30:28 -0700430}
431
Tommia5e07cc2020-05-26 21:40:37 +0200432TEST_F(PacketRouterDeathTest, RemovalOfNeverAddedSendModuleDisallowed) {
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200433 NiceMock<MockRtpRtcpInterface> module;
eladalon822ff2b2017-08-01 06:30:28 -0700434
Erik Språng4208a132019-08-26 08:58:45 +0200435 EXPECT_DEATH(packet_router_.RemoveSendRtpModule(&module), "");
eladalon822ff2b2017-08-01 06:30:28 -0700436}
437
Tommia5e07cc2020-05-26 21:40:37 +0200438TEST_F(PacketRouterDeathTest, RemovalOfNeverAddedReceiveModuleDisallowed) {
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200439 NiceMock<MockRtpRtcpInterface> module;
eladalon822ff2b2017-08-01 06:30:28 -0700440
Erik Språng4208a132019-08-26 08:58:45 +0200441 EXPECT_DEATH(packet_router_.RemoveReceiveRtpModule(&module), "");
eladalon822ff2b2017-08-01 06:30:28 -0700442}
443#endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
444
nisse05843312017-04-18 23:38:35 -0700445TEST(PacketRouterRembTest, LowerEstimateToSendRemb) {
446 rtc::ScopedFakeClock clock;
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200447 NiceMock<MockRtpRtcpInterface> rtp;
nisse05843312017-04-18 23:38:35 -0700448 PacketRouter packet_router;
449
eladalon822ff2b2017-08-01 06:30:28 -0700450 packet_router.AddSendRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700451
452 uint32_t bitrate_estimate = 456;
453 const std::vector<uint32_t> ssrcs = {1234};
454
nisse05843312017-04-18 23:38:35 -0700455 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
456
457 // Call OnReceiveBitrateChanged twice to get a first estimate.
Danil Chapovalov55284022020-02-07 14:53:52 +0100458 clock.AdvanceTime(TimeDelta::Millis(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200459 EXPECT_CALL(rtp, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700460 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
461
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200462 // Lower the estimate with more than 3% to trigger a call to SetRemb right
nisse05843312017-04-18 23:38:35 -0700463 // away.
464 bitrate_estimate = bitrate_estimate - 100;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200465 EXPECT_CALL(rtp, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700466 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
467
nisse05843312017-04-18 23:38:35 -0700468 packet_router.RemoveSendRtpModule(&rtp);
469}
470
471TEST(PacketRouterRembTest, VerifyIncreasingAndDecreasing) {
472 rtc::ScopedFakeClock clock;
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200473 NiceMock<MockRtpRtcpInterface> rtp;
nisse05843312017-04-18 23:38:35 -0700474 PacketRouter packet_router;
eladalon822ff2b2017-08-01 06:30:28 -0700475 packet_router.AddSendRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700476
477 uint32_t bitrate_estimate[] = {456, 789};
478 std::vector<uint32_t> ssrcs = {1234, 5678};
479
nisse05843312017-04-18 23:38:35 -0700480 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate[0]);
481
482 // Call OnReceiveBitrateChanged twice to get a first estimate.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200483 EXPECT_CALL(rtp, SetRemb(bitrate_estimate[0], ssrcs)).Times(1);
Danil Chapovalov55284022020-02-07 14:53:52 +0100484 clock.AdvanceTime(TimeDelta::Millis(1000));
nisse05843312017-04-18 23:38:35 -0700485 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate[0]);
486
487 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate[1] + 100);
488
489 // Lower the estimate to trigger a callback.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200490 EXPECT_CALL(rtp, SetRemb(bitrate_estimate[1], ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700491 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate[1]);
492
493 packet_router.RemoveSendRtpModule(&rtp);
494}
495
496TEST(PacketRouterRembTest, NoRembForIncreasedBitrate) {
497 rtc::ScopedFakeClock clock;
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200498 NiceMock<MockRtpRtcpInterface> 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 std::vector<uint32_t> ssrcs = {1234, 5678};
504
nisse05843312017-04-18 23:38:35 -0700505 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
506
507 // Call OnReceiveBitrateChanged twice to get a first estimate.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200508 EXPECT_CALL(rtp, SetRemb(bitrate_estimate, ssrcs)).Times(1);
Danil Chapovalov55284022020-02-07 14:53:52 +0100509 clock.AdvanceTime(TimeDelta::Millis(1000));
nisse05843312017-04-18 23:38:35 -0700510 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
511
512 // Increased estimate shouldn't trigger a callback right away.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200513 EXPECT_CALL(rtp, SetRemb(_, _)).Times(0);
nisse05843312017-04-18 23:38:35 -0700514 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate + 1);
515
516 // Decreasing the estimate less than 3% shouldn't trigger a new callback.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200517 EXPECT_CALL(rtp, SetRemb(_, _)).Times(0);
nisse05843312017-04-18 23:38:35 -0700518 int lower_estimate = bitrate_estimate * 98 / 100;
519 packet_router.OnReceiveBitrateChanged(ssrcs, lower_estimate);
520
521 packet_router.RemoveSendRtpModule(&rtp);
522}
523
524TEST(PacketRouterRembTest, ChangeSendRtpModule) {
525 rtc::ScopedFakeClock clock;
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200526 NiceMock<MockRtpRtcpInterface> rtp_send;
527 NiceMock<MockRtpRtcpInterface> rtp_recv;
nisse05843312017-04-18 23:38:35 -0700528 PacketRouter packet_router;
eladalon822ff2b2017-08-01 06:30:28 -0700529 packet_router.AddSendRtpModule(&rtp_send, true);
530 packet_router.AddReceiveRtpModule(&rtp_recv, true);
nisse05843312017-04-18 23:38:35 -0700531
532 uint32_t bitrate_estimate = 456;
533 std::vector<uint32_t> ssrcs = {1234, 5678};
534
nisse05843312017-04-18 23:38:35 -0700535 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
536
537 // Call OnReceiveBitrateChanged twice to get a first estimate.
Danil Chapovalov55284022020-02-07 14:53:52 +0100538 clock.AdvanceTime(TimeDelta::Millis(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200539 EXPECT_CALL(rtp_send, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700540 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
541
542 // Decrease estimate to trigger a REMB.
543 bitrate_estimate = bitrate_estimate - 100;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200544 EXPECT_CALL(rtp_send, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700545 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
546
547 // Remove the sending module -> should get remb on the second module.
548 packet_router.RemoveSendRtpModule(&rtp_send);
549
nisse05843312017-04-18 23:38:35 -0700550 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
551
552 bitrate_estimate = bitrate_estimate - 100;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200553 EXPECT_CALL(rtp_recv, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700554 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
555
556 packet_router.RemoveReceiveRtpModule(&rtp_recv);
557}
558
559TEST(PacketRouterRembTest, OnlyOneRembForRepeatedOnReceiveBitrateChanged) {
560 rtc::ScopedFakeClock clock;
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200561 NiceMock<MockRtpRtcpInterface> rtp;
nisse05843312017-04-18 23:38:35 -0700562 PacketRouter packet_router;
eladalon822ff2b2017-08-01 06:30:28 -0700563 packet_router.AddSendRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700564
565 uint32_t bitrate_estimate = 456;
566 const std::vector<uint32_t> ssrcs = {1234};
567
nisse05843312017-04-18 23:38:35 -0700568 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
569
570 // Call OnReceiveBitrateChanged twice to get a first estimate.
Danil Chapovalov55284022020-02-07 14:53:52 +0100571 clock.AdvanceTime(TimeDelta::Millis(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200572 EXPECT_CALL(rtp, SetRemb(_, _)).Times(1);
nisse05843312017-04-18 23:38:35 -0700573 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
574
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200575 // Lower the estimate, should trigger a call to SetRemb right away.
nisse05843312017-04-18 23:38:35 -0700576 bitrate_estimate = bitrate_estimate - 100;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200577 EXPECT_CALL(rtp, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700578 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
579
580 // Call OnReceiveBitrateChanged again, this should not trigger a new callback.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200581 EXPECT_CALL(rtp, SetRemb(_, _)).Times(0);
nisse05843312017-04-18 23:38:35 -0700582 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
583 packet_router.RemoveSendRtpModule(&rtp);
584}
585
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200586TEST(PacketRouterRembTest, SetMaxDesiredReceiveBitrateLimitsSetRemb) {
danilchap47085372017-08-10 06:03:57 -0700587 rtc::ScopedFakeClock clock;
588 PacketRouter packet_router;
Danil Chapovalov55284022020-02-07 14:53:52 +0100589 clock.AdvanceTime(TimeDelta::Millis(1000));
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200590 NiceMock<MockRtpRtcpInterface> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700591 constexpr bool remb_candidate = true;
592 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700593
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100594 const int64_t cap_bitrate = 100000;
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200595 EXPECT_CALL(remb_sender, SetRemb(Le(cap_bitrate), _)).Times(AtLeast(1));
596 EXPECT_CALL(remb_sender, SetRemb(Gt(cap_bitrate), _)).Times(0);
danilchap47085372017-08-10 06:03:57 -0700597
598 const std::vector<uint32_t> ssrcs = {1234};
599 packet_router.SetMaxDesiredReceiveBitrate(cap_bitrate);
600 packet_router.OnReceiveBitrateChanged(ssrcs, cap_bitrate + 5000);
Danil Chapovalov55284022020-02-07 14:53:52 +0100601 clock.AdvanceTime(TimeDelta::Millis(1000));
danilchap47085372017-08-10 06:03:57 -0700602 packet_router.OnReceiveBitrateChanged(ssrcs, cap_bitrate - 5000);
603
604 // Test tear-down.
605 packet_router.RemoveSendRtpModule(&remb_sender);
606}
607
608TEST(PacketRouterRembTest,
609 SetMaxDesiredReceiveBitrateTriggersRembWhenMoreRestrictive) {
610 rtc::ScopedFakeClock clock;
611 PacketRouter packet_router;
Danil Chapovalov55284022020-02-07 14:53:52 +0100612 clock.AdvanceTime(TimeDelta::Millis(1000));
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200613 NiceMock<MockRtpRtcpInterface> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700614 constexpr bool remb_candidate = true;
615 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700616
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100617 const int64_t measured_bitrate_bps = 150000;
618 const int64_t cap_bitrate_bps = measured_bitrate_bps - 5000;
danilchap47085372017-08-10 06:03:57 -0700619 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200620 EXPECT_CALL(remb_sender, SetRemb(measured_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700621 packet_router.OnReceiveBitrateChanged(ssrcs, measured_bitrate_bps);
622
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200623 EXPECT_CALL(remb_sender, SetRemb(cap_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700624 packet_router.SetMaxDesiredReceiveBitrate(cap_bitrate_bps);
625
626 // Test tear-down.
627 packet_router.RemoveSendRtpModule(&remb_sender);
628}
629
630TEST(PacketRouterRembTest,
631 SetMaxDesiredReceiveBitrateDoesNotTriggerRembWhenAsRestrictive) {
632 rtc::ScopedFakeClock clock;
633 PacketRouter packet_router;
Danil Chapovalov55284022020-02-07 14:53:52 +0100634 clock.AdvanceTime(TimeDelta::Millis(1000));
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200635 NiceMock<MockRtpRtcpInterface> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700636 constexpr bool remb_candidate = true;
637 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700638
639 const uint32_t measured_bitrate_bps = 150000;
640 const uint32_t cap_bitrate_bps = measured_bitrate_bps;
641 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200642 EXPECT_CALL(remb_sender, SetRemb(measured_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700643 packet_router.OnReceiveBitrateChanged(ssrcs, measured_bitrate_bps);
644
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200645 EXPECT_CALL(remb_sender, SetRemb(_, _)).Times(0);
danilchap47085372017-08-10 06:03:57 -0700646 packet_router.SetMaxDesiredReceiveBitrate(cap_bitrate_bps);
647
648 // Test tear-down.
649 packet_router.RemoveSendRtpModule(&remb_sender);
650}
651
652TEST(PacketRouterRembTest,
653 SetMaxDesiredReceiveBitrateDoesNotTriggerRembWhenLessRestrictive) {
654 rtc::ScopedFakeClock clock;
655 PacketRouter packet_router;
Danil Chapovalov55284022020-02-07 14:53:52 +0100656 clock.AdvanceTime(TimeDelta::Millis(1000));
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200657 NiceMock<MockRtpRtcpInterface> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700658 constexpr bool remb_candidate = true;
659 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700660
661 const uint32_t measured_bitrate_bps = 150000;
662 const uint32_t cap_bitrate_bps = measured_bitrate_bps + 500;
663 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200664 EXPECT_CALL(remb_sender, SetRemb(measured_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700665 packet_router.OnReceiveBitrateChanged(ssrcs, measured_bitrate_bps);
666
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200667 EXPECT_CALL(remb_sender, SetRemb(_, _)).Times(0);
danilchap47085372017-08-10 06:03:57 -0700668 packet_router.SetMaxDesiredReceiveBitrate(cap_bitrate_bps);
669
670 // Test tear-down.
671 packet_router.RemoveSendRtpModule(&remb_sender);
672}
673
674TEST(PacketRouterRembTest,
675 SetMaxDesiredReceiveBitrateTriggersRembWhenNoRecentMeasure) {
676 rtc::ScopedFakeClock clock;
677 PacketRouter packet_router;
Danil Chapovalov55284022020-02-07 14:53:52 +0100678 clock.AdvanceTime(TimeDelta::Millis(1000));
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200679 NiceMock<MockRtpRtcpInterface> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700680 constexpr bool remb_candidate = true;
681 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700682
683 const uint32_t measured_bitrate_bps = 150000;
684 const uint32_t cap_bitrate_bps = measured_bitrate_bps + 5000;
685 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200686 EXPECT_CALL(remb_sender, SetRemb(measured_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700687 packet_router.OnReceiveBitrateChanged(ssrcs, measured_bitrate_bps);
Danil Chapovalov55284022020-02-07 14:53:52 +0100688 clock.AdvanceTime(TimeDelta::Millis(1000));
danilchap47085372017-08-10 06:03:57 -0700689
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200690 EXPECT_CALL(remb_sender, SetRemb(cap_bitrate_bps, _));
danilchap47085372017-08-10 06:03:57 -0700691 packet_router.SetMaxDesiredReceiveBitrate(cap_bitrate_bps);
692
693 // Test tear-down.
694 packet_router.RemoveSendRtpModule(&remb_sender);
695}
696
697TEST(PacketRouterRembTest,
698 SetMaxDesiredReceiveBitrateTriggersRembWhenNoMeasures) {
699 rtc::ScopedFakeClock clock;
700 PacketRouter packet_router;
Danil Chapovalov55284022020-02-07 14:53:52 +0100701 clock.AdvanceTime(TimeDelta::Millis(1000));
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200702 NiceMock<MockRtpRtcpInterface> remb_sender;
danilchap47085372017-08-10 06:03:57 -0700703 constexpr bool remb_candidate = true;
704 packet_router.AddSendRtpModule(&remb_sender, remb_candidate);
danilchap47085372017-08-10 06:03:57 -0700705
706 // Set cap.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200707 EXPECT_CALL(remb_sender, SetRemb(100000, _)).Times(1);
danilchap47085372017-08-10 06:03:57 -0700708 packet_router.SetMaxDesiredReceiveBitrate(100000);
709 // Increase cap.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200710 EXPECT_CALL(remb_sender, SetRemb(200000, _)).Times(1);
danilchap47085372017-08-10 06:03:57 -0700711 packet_router.SetMaxDesiredReceiveBitrate(200000);
712 // Decrease cap.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200713 EXPECT_CALL(remb_sender, SetRemb(150000, _)).Times(1);
danilchap47085372017-08-10 06:03:57 -0700714 packet_router.SetMaxDesiredReceiveBitrate(150000);
715
716 // Test tear-down.
717 packet_router.RemoveSendRtpModule(&remb_sender);
718}
719
nisse05843312017-04-18 23:38:35 -0700720// Only register receiving modules and make sure we fallback to trigger a REMB
721// packet on this one.
722TEST(PacketRouterRembTest, NoSendingRtpModule) {
723 rtc::ScopedFakeClock clock;
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200724 NiceMock<MockRtpRtcpInterface> rtp;
nisse05843312017-04-18 23:38:35 -0700725 PacketRouter packet_router;
726
eladalon822ff2b2017-08-01 06:30:28 -0700727 packet_router.AddReceiveRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700728
729 uint32_t bitrate_estimate = 456;
730 const std::vector<uint32_t> ssrcs = {1234};
731
nisse05843312017-04-18 23:38:35 -0700732 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
733
734 // Call OnReceiveBitrateChanged twice to get a first estimate.
Danil Chapovalov55284022020-02-07 14:53:52 +0100735 clock.AdvanceTime(TimeDelta::Millis(1000));
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200736 EXPECT_CALL(rtp, SetRemb(bitrate_estimate, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700737 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
738
739 // Lower the estimate to trigger a new packet REMB packet.
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200740 EXPECT_CALL(rtp, SetRemb(bitrate_estimate - 100, ssrcs)).Times(1);
nisse05843312017-04-18 23:38:35 -0700741 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate - 100);
742
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200743 EXPECT_CALL(rtp, UnsetRemb()).Times(1);
nisse05843312017-04-18 23:38:35 -0700744 packet_router.RemoveReceiveRtpModule(&rtp);
745}
746
eladalon822ff2b2017-08-01 06:30:28 -0700747TEST(PacketRouterRembTest, NonCandidateSendRtpModuleNotUsedForRemb) {
748 rtc::ScopedFakeClock clock;
749 PacketRouter packet_router;
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200750 NiceMock<MockRtpRtcpInterface> module;
eladalon822ff2b2017-08-01 06:30:28 -0700751
752 constexpr bool remb_candidate = false;
753
754 packet_router.AddSendRtpModule(&module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700755
756 constexpr uint32_t bitrate_estimate = 456;
757 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200758 EXPECT_CALL(module, SetRemb(_, _)).Times(0);
Danil Chapovalov55284022020-02-07 14:53:52 +0100759 clock.AdvanceTime(TimeDelta::Millis(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700760 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
761
762 // Test tear-down
763 packet_router.RemoveSendRtpModule(&module);
764}
765
766TEST(PacketRouterRembTest, CandidateSendRtpModuleUsedForRemb) {
767 rtc::ScopedFakeClock clock;
768 PacketRouter packet_router;
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200769 NiceMock<MockRtpRtcpInterface> module;
eladalon822ff2b2017-08-01 06:30:28 -0700770
771 constexpr bool remb_candidate = true;
772
773 packet_router.AddSendRtpModule(&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(module, SetRemb(bitrate_estimate, ssrcs)).Times(1);
Danil Chapovalov55284022020-02-07 14:53:52 +0100778 clock.AdvanceTime(TimeDelta::Millis(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700779 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
780
781 // Test tear-down
782 packet_router.RemoveSendRtpModule(&module);
783}
784
785TEST(PacketRouterRembTest, NonCandidateReceiveRtpModuleNotUsedForRemb) {
786 rtc::ScopedFakeClock clock;
787 PacketRouter packet_router;
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200788 NiceMock<MockRtpRtcpInterface> module;
eladalon822ff2b2017-08-01 06:30:28 -0700789
790 constexpr bool remb_candidate = false;
791
792 packet_router.AddReceiveRtpModule(&module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700793
794 constexpr uint32_t bitrate_estimate = 456;
795 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200796 EXPECT_CALL(module, SetRemb(_, _)).Times(0);
Danil Chapovalov55284022020-02-07 14:53:52 +0100797 clock.AdvanceTime(TimeDelta::Millis(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700798 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
799
800 // Test tear-down
801 packet_router.RemoveReceiveRtpModule(&module);
802}
803
804TEST(PacketRouterRembTest, CandidateReceiveRtpModuleUsedForRemb) {
805 rtc::ScopedFakeClock clock;
806 PacketRouter packet_router;
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200807 NiceMock<MockRtpRtcpInterface> module;
eladalon822ff2b2017-08-01 06:30:28 -0700808
809 constexpr bool remb_candidate = true;
810
811 packet_router.AddReceiveRtpModule(&module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700812
813 constexpr uint32_t bitrate_estimate = 456;
814 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200815 EXPECT_CALL(module, SetRemb(bitrate_estimate, ssrcs)).Times(1);
Danil Chapovalov55284022020-02-07 14:53:52 +0100816 clock.AdvanceTime(TimeDelta::Millis(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700817 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
818
819 // Test tear-down
820 packet_router.RemoveReceiveRtpModule(&module);
821}
822
823TEST(PacketRouterRembTest,
824 SendCandidatePreferredOverReceiveCandidate_SendModuleAddedFirst) {
825 rtc::ScopedFakeClock clock;
826 PacketRouter packet_router;
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200827 NiceMock<MockRtpRtcpInterface> send_module;
828 NiceMock<MockRtpRtcpInterface> receive_module;
eladalon822ff2b2017-08-01 06:30:28 -0700829
830 constexpr bool remb_candidate = true;
831
832 // Send module added - activated.
833 packet_router.AddSendRtpModule(&send_module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700834
835 // Receive module added - the send module remains the active one.
836 packet_router.AddReceiveRtpModule(&receive_module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700837
838 constexpr uint32_t bitrate_estimate = 456;
839 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200840 EXPECT_CALL(send_module, SetRemb(bitrate_estimate, ssrcs)).Times(1);
841 EXPECT_CALL(receive_module, SetRemb(_, _)).Times(0);
eladalon822ff2b2017-08-01 06:30:28 -0700842
Danil Chapovalov55284022020-02-07 14:53:52 +0100843 clock.AdvanceTime(TimeDelta::Millis(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700844 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
845
846 // Test tear-down
847 packet_router.RemoveReceiveRtpModule(&receive_module);
848 packet_router.RemoveSendRtpModule(&send_module);
849}
850
851TEST(PacketRouterRembTest,
852 SendCandidatePreferredOverReceiveCandidate_ReceiveModuleAddedFirst) {
853 rtc::ScopedFakeClock clock;
854 PacketRouter packet_router;
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200855 NiceMock<MockRtpRtcpInterface> send_module;
856 NiceMock<MockRtpRtcpInterface> receive_module;
eladalon822ff2b2017-08-01 06:30:28 -0700857
858 constexpr bool remb_candidate = true;
859
860 // Receive module added - activated.
861 packet_router.AddReceiveRtpModule(&receive_module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700862
863 // Send module added - replaces receive module as active.
864 packet_router.AddSendRtpModule(&send_module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700865
866 constexpr uint32_t bitrate_estimate = 456;
867 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200868 EXPECT_CALL(send_module, SetRemb(bitrate_estimate, ssrcs)).Times(1);
869 EXPECT_CALL(receive_module, SetRemb(_, _)).Times(0);
eladalon822ff2b2017-08-01 06:30:28 -0700870
Danil Chapovalov55284022020-02-07 14:53:52 +0100871 clock.AdvanceTime(TimeDelta::Millis(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700872 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
873
874 // Test tear-down
875 packet_router.RemoveReceiveRtpModule(&receive_module);
876 packet_router.RemoveSendRtpModule(&send_module);
877}
878
879TEST(PacketRouterRembTest, ReceiveModuleTakesOverWhenLastSendModuleRemoved) {
880 rtc::ScopedFakeClock clock;
881 PacketRouter packet_router;
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200882 NiceMock<MockRtpRtcpInterface> send_module;
883 NiceMock<MockRtpRtcpInterface> receive_module;
eladalon822ff2b2017-08-01 06:30:28 -0700884
885 constexpr bool remb_candidate = true;
886
887 // Send module active, receive module inactive.
888 packet_router.AddSendRtpModule(&send_module, remb_candidate);
889 packet_router.AddReceiveRtpModule(&receive_module, remb_candidate);
eladalon822ff2b2017-08-01 06:30:28 -0700890
891 // Send module removed - receive module becomes active.
892 packet_router.RemoveSendRtpModule(&send_module);
eladalon822ff2b2017-08-01 06:30:28 -0700893 constexpr uint32_t bitrate_estimate = 456;
894 const std::vector<uint32_t> ssrcs = {1234};
Danil Chapovalov51e21aa2017-10-10 17:46:26 +0200895 EXPECT_CALL(send_module, SetRemb(_, _)).Times(0);
896 EXPECT_CALL(receive_module, SetRemb(bitrate_estimate, ssrcs)).Times(1);
eladalon822ff2b2017-08-01 06:30:28 -0700897
Danil Chapovalov55284022020-02-07 14:53:52 +0100898 clock.AdvanceTime(TimeDelta::Millis(1000));
eladalon822ff2b2017-08-01 06:30:28 -0700899 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
900
901 // Test tear-down
902 packet_router.RemoveReceiveRtpModule(&receive_module);
903}
904
Stefan Holmere5904162015-03-26 11:11:06 +0100905} // namespace webrtc