blob: 6ef1b0df7626330f87024827ef8ea558bc4816a2 [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
11#include <list>
kwiberg22feaa32016-03-17 09:17:43 -070012#include <memory>
Stefan Holmere5904162015-03-26 11:11:06 +010013
Henrik Kjellander0b9e29c2015-11-16 11:12:24 +010014#include "webrtc/modules/pacing/packet_router.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010015#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
Stefan Holmere5904162015-03-26 11:11:06 +010016#include "webrtc/modules/rtp_rtcp/mocks/mock_rtp_rtcp.h"
stefanbba9dec2016-02-01 04:39:55 -080017#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020018#include "webrtc/rtc_base/checks.h"
19#include "webrtc/rtc_base/fakeclock.h"
kwibergac9f8762016-09-30 22:29:43 -070020#include "webrtc/test/gmock.h"
21#include "webrtc/test/gtest.h"
Stefan Holmere5904162015-03-26 11:11:06 +010022
23using ::testing::_;
24using ::testing::AnyNumber;
philipelc7bf32a2017-02-17 03:59:43 -080025using ::testing::Field;
Stefan Holmere5904162015-03-26 11:11:06 +010026using ::testing::NiceMock;
27using ::testing::Return;
eladalon822ff2b2017-08-01 06:30:28 -070028using ::testing::ReturnPointee;
29using ::testing::SaveArg;
Stefan Holmere5904162015-03-26 11:11:06 +010030
31namespace webrtc {
32
eladalon822ff2b2017-08-01 06:30:28 -070033// TODO(eladalon): Restructure and/or replace the existing monolithic tests
34// (only some of the test are monolithic) according to the new
35// guidelines - small tests for one thing at a time.
36// (I'm not removing any tests during CL, so as to demonstrate no regressions.)
37
38class MockRtpRtcpWithRembTracking : public MockRtpRtcp {
39 public:
40 MockRtpRtcpWithRembTracking() {
41 ON_CALL(*this, SetREMBStatus(_)).WillByDefault(SaveArg<0>(&remb_));
42 ON_CALL(*this, REMB()).WillByDefault(ReturnPointee(&remb_));
43 }
44
45 private:
46 bool remb_ = false;
47};
48
Stefan Holmere5904162015-03-26 11:11:06 +010049class PacketRouterTest : public ::testing::Test {
50 public:
51 PacketRouterTest() : packet_router_(new PacketRouter()) {}
52 protected:
eladalon822ff2b2017-08-01 06:30:28 -070053 static constexpr int kProbeMinProbes = 5;
54 static constexpr int kProbeMinBytes = 1000;
kwiberg22feaa32016-03-17 09:17:43 -070055 const std::unique_ptr<PacketRouter> packet_router_;
Stefan Holmere5904162015-03-26 11:11:06 +010056};
57
58TEST_F(PacketRouterTest, TimeToSendPacket) {
eladalon6c9556e2017-07-10 03:33:00 -070059 NiceMock<MockRtpRtcp> rtp_1;
60 NiceMock<MockRtpRtcp> rtp_2;
eladalon822ff2b2017-08-01 06:30:28 -070061 packet_router_->AddSendRtpModule(&rtp_1, false);
62 packet_router_->AddSendRtpModule(&rtp_2, false);
Stefan Holmere5904162015-03-26 11:11:06 +010063
64 const uint16_t kSsrc1 = 1234;
65 uint16_t sequence_number = 17;
66 uint64_t timestamp = 7890;
67 bool retransmission = false;
68
69 // Send on the first module by letting rtp_1 be sending with correct ssrc.
70 EXPECT_CALL(rtp_1, SendingMedia()).Times(1).WillOnce(Return(true));
71 EXPECT_CALL(rtp_1, SSRC()).Times(1).WillOnce(Return(kSsrc1));
philipelc7bf32a2017-02-17 03:59:43 -080072 EXPECT_CALL(rtp_1, TimeToSendPacket(
73 kSsrc1, sequence_number, timestamp, retransmission,
74 Field(&PacedPacketInfo::probe_cluster_id, 1)))
Stefan Holmere5904162015-03-26 11:11:06 +010075 .Times(1)
76 .WillOnce(Return(true));
philipela1ed0b32016-06-01 06:31:17 -070077 EXPECT_CALL(rtp_2, TimeToSendPacket(_, _, _, _, _)).Times(0);
philipelc7bf32a2017-02-17 03:59:43 -080078 EXPECT_TRUE(packet_router_->TimeToSendPacket(
79 kSsrc1, sequence_number, timestamp, retransmission,
80 PacedPacketInfo(1, kProbeMinProbes, kProbeMinBytes)));
Stefan Holmere5904162015-03-26 11:11:06 +010081
82 // Send on the second module by letting rtp_2 be sending, but not rtp_1.
83 ++sequence_number;
84 timestamp += 30;
85 retransmission = true;
86 const uint16_t kSsrc2 = 4567;
87 EXPECT_CALL(rtp_1, SendingMedia()).Times(1).WillOnce(Return(false));
88 EXPECT_CALL(rtp_2, SendingMedia()).Times(1).WillOnce(Return(true));
89 EXPECT_CALL(rtp_2, SSRC()).Times(1).WillOnce(Return(kSsrc2));
philipela1ed0b32016-06-01 06:31:17 -070090 EXPECT_CALL(rtp_1, TimeToSendPacket(_, _, _, _, _)).Times(0);
philipelc7bf32a2017-02-17 03:59:43 -080091 EXPECT_CALL(rtp_2, TimeToSendPacket(
92 kSsrc2, sequence_number, timestamp, retransmission,
93 Field(&PacedPacketInfo::probe_cluster_id, 2)))
Stefan Holmere5904162015-03-26 11:11:06 +010094 .Times(1)
95 .WillOnce(Return(true));
philipelc7bf32a2017-02-17 03:59:43 -080096 EXPECT_TRUE(packet_router_->TimeToSendPacket(
97 kSsrc2, sequence_number, timestamp, retransmission,
98 PacedPacketInfo(2, kProbeMinProbes, kProbeMinBytes)));
Stefan Holmere5904162015-03-26 11:11:06 +010099
100 // No module is sending, hence no packet should be sent.
101 EXPECT_CALL(rtp_1, SendingMedia()).Times(1).WillOnce(Return(false));
philipela1ed0b32016-06-01 06:31:17 -0700102 EXPECT_CALL(rtp_1, TimeToSendPacket(_, _, _, _, _)).Times(0);
Stefan Holmere5904162015-03-26 11:11:06 +0100103 EXPECT_CALL(rtp_2, SendingMedia()).Times(1).WillOnce(Return(false));
philipela1ed0b32016-06-01 06:31:17 -0700104 EXPECT_CALL(rtp_2, TimeToSendPacket(_, _, _, _, _)).Times(0);
philipelc7bf32a2017-02-17 03:59:43 -0800105 EXPECT_TRUE(packet_router_->TimeToSendPacket(
106 kSsrc1, sequence_number, timestamp, retransmission,
107 PacedPacketInfo(1, kProbeMinProbes, kProbeMinBytes)));
Stefan Holmere5904162015-03-26 11:11:06 +0100108
109 // Add a packet with incorrect ssrc and test it's dropped in the router.
110 EXPECT_CALL(rtp_1, SendingMedia()).Times(1).WillOnce(Return(true));
111 EXPECT_CALL(rtp_1, SSRC()).Times(1).WillOnce(Return(kSsrc1));
112 EXPECT_CALL(rtp_2, SendingMedia()).Times(1).WillOnce(Return(true));
113 EXPECT_CALL(rtp_2, SSRC()).Times(1).WillOnce(Return(kSsrc2));
philipela1ed0b32016-06-01 06:31:17 -0700114 EXPECT_CALL(rtp_1, TimeToSendPacket(_, _, _, _, _)).Times(0);
115 EXPECT_CALL(rtp_2, TimeToSendPacket(_, _, _, _, _)).Times(0);
philipelc7bf32a2017-02-17 03:59:43 -0800116 EXPECT_TRUE(packet_router_->TimeToSendPacket(
117 kSsrc1 + kSsrc2, sequence_number, timestamp, retransmission,
118 PacedPacketInfo(1, kProbeMinProbes, kProbeMinBytes)));
Stefan Holmere5904162015-03-26 11:11:06 +0100119
nissefdbfdc92017-03-31 05:44:52 -0700120 packet_router_->RemoveSendRtpModule(&rtp_1);
Stefan Holmere5904162015-03-26 11:11:06 +0100121
122 // rtp_1 has been removed, try sending a packet on that ssrc and make sure
123 // it is dropped as expected by not expecting any calls to rtp_1.
124 EXPECT_CALL(rtp_2, SendingMedia()).Times(1).WillOnce(Return(true));
125 EXPECT_CALL(rtp_2, SSRC()).Times(1).WillOnce(Return(kSsrc2));
philipela1ed0b32016-06-01 06:31:17 -0700126 EXPECT_CALL(rtp_2, TimeToSendPacket(_, _, _, _, _)).Times(0);
philipelc7bf32a2017-02-17 03:59:43 -0800127 EXPECT_TRUE(packet_router_->TimeToSendPacket(
128 kSsrc1, sequence_number, timestamp, retransmission,
129 PacedPacketInfo(PacedPacketInfo::kNotAProbe, kProbeMinBytes,
130 kProbeMinBytes)));
Stefan Holmere5904162015-03-26 11:11:06 +0100131
nissefdbfdc92017-03-31 05:44:52 -0700132 packet_router_->RemoveSendRtpModule(&rtp_2);
Stefan Holmere5904162015-03-26 11:11:06 +0100133}
134
135TEST_F(PacketRouterTest, TimeToSendPadding) {
sprang867fb522015-08-03 04:38:41 -0700136 const uint16_t kSsrc1 = 1234;
137 const uint16_t kSsrc2 = 4567;
138
eladalon6c9556e2017-07-10 03:33:00 -0700139 NiceMock<MockRtpRtcp> rtp_1;
stefan16b02212017-01-27 07:12:16 -0800140 EXPECT_CALL(rtp_1, RtxSendStatus()).WillOnce(Return(kRtxOff));
sprang867fb522015-08-03 04:38:41 -0700141 EXPECT_CALL(rtp_1, SSRC()).WillRepeatedly(Return(kSsrc1));
eladalon6c9556e2017-07-10 03:33:00 -0700142 NiceMock<MockRtpRtcp> rtp_2;
stefan16b02212017-01-27 07:12:16 -0800143 // rtp_2 will be prioritized for padding.
144 EXPECT_CALL(rtp_2, RtxSendStatus()).WillOnce(Return(kRtxRedundantPayloads));
sprang867fb522015-08-03 04:38:41 -0700145 EXPECT_CALL(rtp_2, SSRC()).WillRepeatedly(Return(kSsrc2));
eladalon822ff2b2017-08-01 06:30:28 -0700146 packet_router_->AddSendRtpModule(&rtp_1, false);
147 packet_router_->AddSendRtpModule(&rtp_2, false);
Stefan Holmere5904162015-03-26 11:11:06 +0100148
sprang867fb522015-08-03 04:38:41 -0700149 // Default configuration, sending padding on all modules sending media,
stefan16b02212017-01-27 07:12:16 -0800150 // ordered by priority (based on rtx mode).
Stefan Holmere5904162015-03-26 11:11:06 +0100151 const size_t requested_padding_bytes = 1000;
152 const size_t sent_padding_bytes = 890;
stefan16b02212017-01-27 07:12:16 -0800153 EXPECT_CALL(rtp_2, SendingMedia()).Times(1).WillOnce(Return(true));
stefan53b6cc32017-02-03 08:13:57 -0800154 EXPECT_CALL(rtp_2, HasBweExtensions()).Times(1).WillOnce(Return(true));
philipelc7bf32a2017-02-17 03:59:43 -0800155 EXPECT_CALL(rtp_2,
156 TimeToSendPadding(requested_padding_bytes,
157 Field(&PacedPacketInfo::probe_cluster_id, 111)))
Stefan Holmere5904162015-03-26 11:11:06 +0100158 .Times(1)
159 .WillOnce(Return(sent_padding_bytes));
stefan16b02212017-01-27 07:12:16 -0800160 EXPECT_CALL(rtp_1, SendingMedia()).Times(1).WillOnce(Return(true));
stefan53b6cc32017-02-03 08:13:57 -0800161 EXPECT_CALL(rtp_1, HasBweExtensions()).Times(1).WillOnce(Return(true));
philipelc7bf32a2017-02-17 03:59:43 -0800162 EXPECT_CALL(rtp_1,
163 TimeToSendPadding(requested_padding_bytes - sent_padding_bytes,
164 Field(&PacedPacketInfo::probe_cluster_id, 111)))
sprang867fb522015-08-03 04:38:41 -0700165 .Times(1)
166 .WillOnce(Return(requested_padding_bytes - sent_padding_bytes));
167 EXPECT_EQ(requested_padding_bytes,
philipelc7bf32a2017-02-17 03:59:43 -0800168 packet_router_->TimeToSendPadding(
169 requested_padding_bytes,
170 PacedPacketInfo(111, kProbeMinBytes, kProbeMinBytes)));
Stefan Holmere5904162015-03-26 11:11:06 +0100171
stefan16b02212017-01-27 07:12:16 -0800172 // Let only the lower priority module be sending and verify the padding
173 // request is routed there.
174 EXPECT_CALL(rtp_2, SendingMedia()).Times(1).WillOnce(Return(false));
175 EXPECT_CALL(rtp_2, TimeToSendPadding(requested_padding_bytes, _)).Times(0);
176 EXPECT_CALL(rtp_1, SendingMedia()).Times(1).WillOnce(Return(true));
stefan53b6cc32017-02-03 08:13:57 -0800177 EXPECT_CALL(rtp_1, HasBweExtensions()).Times(1).WillOnce(Return(true));
stefan16b02212017-01-27 07:12:16 -0800178 EXPECT_CALL(rtp_1, TimeToSendPadding(_, _))
Stefan Holmere5904162015-03-26 11:11:06 +0100179 .Times(1)
180 .WillOnce(Return(sent_padding_bytes));
181 EXPECT_EQ(sent_padding_bytes,
philipelc7bf32a2017-02-17 03:59:43 -0800182 packet_router_->TimeToSendPadding(
183 requested_padding_bytes,
184 PacedPacketInfo(PacedPacketInfo::kNotAProbe, kProbeMinBytes,
185 kProbeMinBytes)));
Stefan Holmere5904162015-03-26 11:11:06 +0100186
187 // No sending module at all.
188 EXPECT_CALL(rtp_1, SendingMedia()).Times(1).WillOnce(Return(false));
philipela1ed0b32016-06-01 06:31:17 -0700189 EXPECT_CALL(rtp_1, TimeToSendPadding(requested_padding_bytes, _)).Times(0);
Stefan Holmere5904162015-03-26 11:11:06 +0100190 EXPECT_CALL(rtp_2, SendingMedia()).Times(1).WillOnce(Return(false));
philipela1ed0b32016-06-01 06:31:17 -0700191 EXPECT_CALL(rtp_2, TimeToSendPadding(_, _)).Times(0);
philipelc7bf32a2017-02-17 03:59:43 -0800192 EXPECT_EQ(0u,
193 packet_router_->TimeToSendPadding(
194 requested_padding_bytes,
195 PacedPacketInfo(PacedPacketInfo::kNotAProbe, kProbeMinBytes,
196 kProbeMinBytes)));
Stefan Holmere5904162015-03-26 11:11:06 +0100197
stefan53b6cc32017-02-03 08:13:57 -0800198 // Only one module has BWE extensions.
199 EXPECT_CALL(rtp_1, SendingMedia()).Times(1).WillOnce(Return(true));
200 EXPECT_CALL(rtp_1, HasBweExtensions()).Times(1).WillOnce(Return(false));
201 EXPECT_CALL(rtp_1, TimeToSendPadding(requested_padding_bytes, _)).Times(0);
202 EXPECT_CALL(rtp_2, SendingMedia()).Times(1).WillOnce(Return(true));
203 EXPECT_CALL(rtp_2, HasBweExtensions()).Times(1).WillOnce(Return(true));
204 EXPECT_CALL(rtp_2, TimeToSendPadding(requested_padding_bytes, _))
205 .Times(1)
206 .WillOnce(Return(sent_padding_bytes));
207 EXPECT_EQ(sent_padding_bytes,
philipelc7bf32a2017-02-17 03:59:43 -0800208 packet_router_->TimeToSendPadding(
209 requested_padding_bytes,
210 PacedPacketInfo(PacedPacketInfo::kNotAProbe, kProbeMinBytes,
211 kProbeMinBytes)));
stefan53b6cc32017-02-03 08:13:57 -0800212
nissefdbfdc92017-03-31 05:44:52 -0700213 packet_router_->RemoveSendRtpModule(&rtp_1);
Stefan Holmere5904162015-03-26 11:11:06 +0100214
215 // rtp_1 has been removed, try sending padding and make sure rtp_1 isn't asked
216 // to send by not expecting any calls. Instead verify rtp_2 is called.
217 EXPECT_CALL(rtp_2, SendingMedia()).Times(1).WillOnce(Return(true));
stefan53b6cc32017-02-03 08:13:57 -0800218 EXPECT_CALL(rtp_2, HasBweExtensions()).Times(1).WillOnce(Return(true));
philipela1ed0b32016-06-01 06:31:17 -0700219 EXPECT_CALL(rtp_2, TimeToSendPadding(requested_padding_bytes, _)).Times(1);
philipelc7bf32a2017-02-17 03:59:43 -0800220 EXPECT_EQ(0u,
221 packet_router_->TimeToSendPadding(
222 requested_padding_bytes,
223 PacedPacketInfo(PacedPacketInfo::kNotAProbe, kProbeMinBytes,
224 kProbeMinBytes)));
Stefan Holmere5904162015-03-26 11:11:06 +0100225
nissefdbfdc92017-03-31 05:44:52 -0700226 packet_router_->RemoveSendRtpModule(&rtp_2);
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100227}
228
229TEST_F(PacketRouterTest, SenderOnlyFunctionsRespectSendingMedia) {
eladalon6c9556e2017-07-10 03:33:00 -0700230 NiceMock<MockRtpRtcp> rtp;
eladalon822ff2b2017-08-01 06:30:28 -0700231 packet_router_->AddSendRtpModule(&rtp, false);
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100232 static const uint16_t kSsrc = 1234;
233 EXPECT_CALL(rtp, SSRC()).WillRepeatedly(Return(kSsrc));
234 EXPECT_CALL(rtp, SendingMedia()).WillRepeatedly(Return(false));
235
236 // Verify that TimeToSendPacket does not end up in a receiver.
philipela1ed0b32016-06-01 06:31:17 -0700237 EXPECT_CALL(rtp, TimeToSendPacket(_, _, _, _, _)).Times(0);
philipelc7bf32a2017-02-17 03:59:43 -0800238 EXPECT_TRUE(packet_router_->TimeToSendPacket(
239 kSsrc, 1, 1, false, PacedPacketInfo(PacedPacketInfo::kNotAProbe,
240 kProbeMinBytes, kProbeMinBytes)));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100241 // Verify that TimeToSendPadding does not end up in a receiver.
philipela1ed0b32016-06-01 06:31:17 -0700242 EXPECT_CALL(rtp, TimeToSendPadding(_, _)).Times(0);
philipelc7bf32a2017-02-17 03:59:43 -0800243 EXPECT_EQ(0u,
244 packet_router_->TimeToSendPadding(
245 200, PacedPacketInfo(PacedPacketInfo::kNotAProbe,
246 kProbeMinBytes, kProbeMinBytes)));
Peter Boström3dd5d1d2016-02-25 16:56:48 +0100247
nissefdbfdc92017-03-31 05:44:52 -0700248 packet_router_->RemoveSendRtpModule(&rtp);
Stefan Holmere5904162015-03-26 11:11:06 +0100249}
sprang867fb522015-08-03 04:38:41 -0700250
251TEST_F(PacketRouterTest, AllocateSequenceNumbers) {
252 const uint16_t kStartSeq = 0xFFF0;
253 const size_t kNumPackets = 32;
254
255 packet_router_->SetTransportWideSequenceNumber(kStartSeq - 1);
256
257 for (size_t i = 0; i < kNumPackets; ++i) {
258 uint16_t seq = packet_router_->AllocateSequenceNumber();
259 uint32_t expected_unwrapped_seq = static_cast<uint32_t>(kStartSeq) + i;
260 EXPECT_EQ(static_cast<uint16_t>(expected_unwrapped_seq & 0xFFFF), seq);
261 }
262}
stefanbba9dec2016-02-01 04:39:55 -0800263
nisse05843312017-04-18 23:38:35 -0700264TEST_F(PacketRouterTest, SendTransportFeedback) {
eladalon6c9556e2017-07-10 03:33:00 -0700265 NiceMock<MockRtpRtcp> rtp_1;
266 NiceMock<MockRtpRtcp> rtp_2;
eladalon822ff2b2017-08-01 06:30:28 -0700267 packet_router_->AddSendRtpModule(&rtp_1, false);
268 packet_router_->AddReceiveRtpModule(&rtp_2, false);
stefanbba9dec2016-02-01 04:39:55 -0800269
270 rtcp::TransportFeedback feedback;
nisse05843312017-04-18 23:38:35 -0700271 EXPECT_CALL(rtp_1, SendFeedbackPacket(_)).Times(1).WillOnce(Return(true));
272 packet_router_->SendTransportFeedback(&feedback);
nissefdbfdc92017-03-31 05:44:52 -0700273 packet_router_->RemoveSendRtpModule(&rtp_1);
nisse05843312017-04-18 23:38:35 -0700274 EXPECT_CALL(rtp_2, SendFeedbackPacket(_)).Times(1).WillOnce(Return(true));
275 packet_router_->SendTransportFeedback(&feedback);
nissefdbfdc92017-03-31 05:44:52 -0700276 packet_router_->RemoveReceiveRtpModule(&rtp_2);
stefanbba9dec2016-02-01 04:39:55 -0800277}
nisse05843312017-04-18 23:38:35 -0700278
eladalon822ff2b2017-08-01 06:30:28 -0700279#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
280TEST_F(PacketRouterTest, DoubleRegistrationOfSendModuleDisallowed) {
281 NiceMock<MockRtpRtcp> module;
282
283 constexpr bool remb_candidate = false; // Value irrelevant.
284 packet_router_->AddSendRtpModule(&module, remb_candidate);
285 EXPECT_DEATH(packet_router_->AddSendRtpModule(&module, remb_candidate), "");
286
287 // Test tear-down
288 packet_router_->RemoveSendRtpModule(&module);
289}
290
291TEST_F(PacketRouterTest, DoubleRegistrationOfReceiveModuleDisallowed) {
292 NiceMock<MockRtpRtcp> module;
293
294 constexpr bool remb_candidate = false; // Value irrelevant.
295 packet_router_->AddReceiveRtpModule(&module, remb_candidate);
296 EXPECT_DEATH(packet_router_->AddReceiveRtpModule(&module, remb_candidate),
297 "");
298
299 // Test tear-down
300 packet_router_->RemoveReceiveRtpModule(&module);
301}
302
303TEST_F(PacketRouterTest, RemovalOfNeverAddedSendModuleDisallowed) {
304 NiceMock<MockRtpRtcp> module;
305
306 EXPECT_DEATH(packet_router_->RemoveSendRtpModule(&module), "");
307}
308
309TEST_F(PacketRouterTest, RemovalOfNeverAddedReceiveModuleDisallowed) {
310 NiceMock<MockRtpRtcp> module;
311
312 EXPECT_DEATH(packet_router_->RemoveReceiveRtpModule(&module), "");
313}
314#endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
315
316// TODO(eladalon): Remove this test; it should be covered by:
317// 1. SendCandidatePreferredOverReceiveCandidate_SendModuleAddedFirst
318// 2. SendCandidatePreferredOverReceiveCandidate_ReceiveModuleAddedFirst
319// 3. LowerEstimateToSendRemb
320// (Not removing in this CL to prove it doesn't break this test.)
nisse05843312017-04-18 23:38:35 -0700321TEST(PacketRouterRembTest, PreferSendModuleOverReceiveModule) {
322 rtc::ScopedFakeClock clock;
eladalon822ff2b2017-08-01 06:30:28 -0700323 NiceMock<MockRtpRtcpWithRembTracking> rtp_recv;
324 NiceMock<MockRtpRtcpWithRembTracking> rtp_send;
nisse05843312017-04-18 23:38:35 -0700325 PacketRouter packet_router;
326
eladalon822ff2b2017-08-01 06:30:28 -0700327 packet_router.AddReceiveRtpModule(&rtp_recv, true);
328 ASSERT_TRUE(rtp_recv.REMB());
nisse05843312017-04-18 23:38:35 -0700329
330 const uint32_t bitrate_estimate = 456;
331 const std::vector<uint32_t> ssrcs = {1234};
332
nisse05843312017-04-18 23:38:35 -0700333 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
334
335 // Call OnReceiveBitrateChanged twice to get a first estimate.
336 clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1000));
337 EXPECT_CALL(rtp_recv, SetREMBData(bitrate_estimate, ssrcs)).Times(1);
338 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
339
340 // Add a send module, which should be preferred over the receive module.
eladalon822ff2b2017-08-01 06:30:28 -0700341 packet_router.AddSendRtpModule(&rtp_send, true);
342 EXPECT_FALSE(rtp_recv.REMB());
343 EXPECT_TRUE(rtp_send.REMB());
nisse05843312017-04-18 23:38:35 -0700344
345 // Lower bitrate to send another REMB packet.
346 EXPECT_CALL(rtp_send, SetREMBData(bitrate_estimate - 100, ssrcs)).Times(1);
347 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate - 100);
348
nisse05843312017-04-18 23:38:35 -0700349 packet_router.RemoveSendRtpModule(&rtp_send);
eladalon822ff2b2017-08-01 06:30:28 -0700350 EXPECT_TRUE(rtp_recv.REMB());
351 EXPECT_FALSE(rtp_send.REMB());
352
nisse05843312017-04-18 23:38:35 -0700353 packet_router.RemoveReceiveRtpModule(&rtp_recv);
354}
355
356TEST(PacketRouterRembTest, LowerEstimateToSendRemb) {
357 rtc::ScopedFakeClock clock;
eladalon822ff2b2017-08-01 06:30:28 -0700358 NiceMock<MockRtpRtcpWithRembTracking> rtp;
nisse05843312017-04-18 23:38:35 -0700359 PacketRouter packet_router;
360
eladalon822ff2b2017-08-01 06:30:28 -0700361 packet_router.AddSendRtpModule(&rtp, true);
362 EXPECT_TRUE(rtp.REMB());
nisse05843312017-04-18 23:38:35 -0700363
364 uint32_t bitrate_estimate = 456;
365 const std::vector<uint32_t> ssrcs = {1234};
366
nisse05843312017-04-18 23:38:35 -0700367 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
368
369 // Call OnReceiveBitrateChanged twice to get a first estimate.
370 clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1000));
371 EXPECT_CALL(rtp, SetREMBData(bitrate_estimate, ssrcs)).Times(1);
372 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
373
374 // Lower the estimate with more than 3% to trigger a call to SetREMBData right
375 // away.
376 bitrate_estimate = bitrate_estimate - 100;
377 EXPECT_CALL(rtp, SetREMBData(bitrate_estimate, ssrcs)).Times(1);
378 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
379
nisse05843312017-04-18 23:38:35 -0700380 packet_router.RemoveSendRtpModule(&rtp);
eladalon822ff2b2017-08-01 06:30:28 -0700381 EXPECT_FALSE(rtp.REMB());
nisse05843312017-04-18 23:38:35 -0700382}
383
384TEST(PacketRouterRembTest, VerifyIncreasingAndDecreasing) {
385 rtc::ScopedFakeClock clock;
eladalon6c9556e2017-07-10 03:33:00 -0700386 NiceMock<MockRtpRtcp> rtp;
nisse05843312017-04-18 23:38:35 -0700387 PacketRouter packet_router;
eladalon822ff2b2017-08-01 06:30:28 -0700388 packet_router.AddSendRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700389
390 uint32_t bitrate_estimate[] = {456, 789};
391 std::vector<uint32_t> ssrcs = {1234, 5678};
392
393 ON_CALL(rtp, REMB()).WillByDefault(Return(true));
394 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate[0]);
395
396 // Call OnReceiveBitrateChanged twice to get a first estimate.
397 EXPECT_CALL(rtp, SetREMBData(bitrate_estimate[0], ssrcs)).Times(1);
398 clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1000));
399 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate[0]);
400
401 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate[1] + 100);
402
403 // Lower the estimate to trigger a callback.
404 EXPECT_CALL(rtp, SetREMBData(bitrate_estimate[1], ssrcs)).Times(1);
405 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate[1]);
406
407 packet_router.RemoveSendRtpModule(&rtp);
408}
409
410TEST(PacketRouterRembTest, NoRembForIncreasedBitrate) {
411 rtc::ScopedFakeClock clock;
eladalon6c9556e2017-07-10 03:33:00 -0700412 NiceMock<MockRtpRtcp> rtp;
nisse05843312017-04-18 23:38:35 -0700413 PacketRouter packet_router;
eladalon822ff2b2017-08-01 06:30:28 -0700414 packet_router.AddSendRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700415
416 uint32_t bitrate_estimate = 456;
417 std::vector<uint32_t> ssrcs = {1234, 5678};
418
419 ON_CALL(rtp, REMB()).WillByDefault(Return(true));
420 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
421
422 // Call OnReceiveBitrateChanged twice to get a first estimate.
423 EXPECT_CALL(rtp, SetREMBData(bitrate_estimate, ssrcs)).Times(1);
424 clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1000));
425 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
426
427 // Increased estimate shouldn't trigger a callback right away.
428 EXPECT_CALL(rtp, SetREMBData(_, _)).Times(0);
429 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate + 1);
430
431 // Decreasing the estimate less than 3% shouldn't trigger a new callback.
432 EXPECT_CALL(rtp, SetREMBData(_, _)).Times(0);
433 int lower_estimate = bitrate_estimate * 98 / 100;
434 packet_router.OnReceiveBitrateChanged(ssrcs, lower_estimate);
435
436 packet_router.RemoveSendRtpModule(&rtp);
437}
438
439TEST(PacketRouterRembTest, ChangeSendRtpModule) {
440 rtc::ScopedFakeClock clock;
eladalon6c9556e2017-07-10 03:33:00 -0700441 NiceMock<MockRtpRtcp> rtp_send;
442 NiceMock<MockRtpRtcp> rtp_recv;
nisse05843312017-04-18 23:38:35 -0700443 PacketRouter packet_router;
eladalon822ff2b2017-08-01 06:30:28 -0700444 packet_router.AddSendRtpModule(&rtp_send, true);
445 packet_router.AddReceiveRtpModule(&rtp_recv, true);
nisse05843312017-04-18 23:38:35 -0700446
447 uint32_t bitrate_estimate = 456;
448 std::vector<uint32_t> ssrcs = {1234, 5678};
449
450 ON_CALL(rtp_send, REMB()).WillByDefault(Return(true));
451 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
452
453 // Call OnReceiveBitrateChanged twice to get a first estimate.
454 clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1000));
455 EXPECT_CALL(rtp_send, SetREMBData(bitrate_estimate, ssrcs)).Times(1);
456 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
457
458 // Decrease estimate to trigger a REMB.
459 bitrate_estimate = bitrate_estimate - 100;
460 EXPECT_CALL(rtp_send, SetREMBData(bitrate_estimate, ssrcs)).Times(1);
461 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
462
463 // Remove the sending module -> should get remb on the second module.
464 packet_router.RemoveSendRtpModule(&rtp_send);
465
466 ON_CALL(rtp_send, REMB()).WillByDefault(Return(false));
467 ON_CALL(rtp_recv, REMB()).WillByDefault(Return(true));
468
469 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
470
471 bitrate_estimate = bitrate_estimate - 100;
472 EXPECT_CALL(rtp_recv, SetREMBData(bitrate_estimate, ssrcs)).Times(1);
473 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
474
475 packet_router.RemoveReceiveRtpModule(&rtp_recv);
476}
477
478TEST(PacketRouterRembTest, OnlyOneRembForRepeatedOnReceiveBitrateChanged) {
479 rtc::ScopedFakeClock clock;
eladalon6c9556e2017-07-10 03:33:00 -0700480 NiceMock<MockRtpRtcp> rtp;
nisse05843312017-04-18 23:38:35 -0700481 PacketRouter packet_router;
eladalon822ff2b2017-08-01 06:30:28 -0700482 packet_router.AddSendRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700483
484 uint32_t bitrate_estimate = 456;
485 const std::vector<uint32_t> ssrcs = {1234};
486
487 ON_CALL(rtp, REMB()).WillByDefault(Return(true));
488 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
489
490 // Call OnReceiveBitrateChanged twice to get a first estimate.
491 clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1000));
492 EXPECT_CALL(rtp, SetREMBData(_, _)).Times(1);
493 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
494
495 // Lower the estimate, should trigger a call to SetREMBData right away.
496 bitrate_estimate = bitrate_estimate - 100;
497 EXPECT_CALL(rtp, SetREMBData(bitrate_estimate, ssrcs)).Times(1);
498 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
499
500 // Call OnReceiveBitrateChanged again, this should not trigger a new callback.
501 EXPECT_CALL(rtp, SetREMBData(_, _)).Times(0);
502 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
503 packet_router.RemoveSendRtpModule(&rtp);
504}
505
506// Only register receiving modules and make sure we fallback to trigger a REMB
507// packet on this one.
508TEST(PacketRouterRembTest, NoSendingRtpModule) {
509 rtc::ScopedFakeClock clock;
eladalon6c9556e2017-07-10 03:33:00 -0700510 NiceMock<MockRtpRtcp> rtp;
nisse05843312017-04-18 23:38:35 -0700511 PacketRouter packet_router;
512
513 EXPECT_CALL(rtp, SetREMBStatus(true)).Times(1);
eladalon822ff2b2017-08-01 06:30:28 -0700514 packet_router.AddReceiveRtpModule(&rtp, true);
nisse05843312017-04-18 23:38:35 -0700515
516 uint32_t bitrate_estimate = 456;
517 const std::vector<uint32_t> ssrcs = {1234};
518
519 ON_CALL(rtp, REMB()).WillByDefault(Return(true));
520 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
521
522 // Call OnReceiveBitrateChanged twice to get a first estimate.
523 clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1000));
524 EXPECT_CALL(rtp, SetREMBData(bitrate_estimate, ssrcs)).Times(1);
525 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
526
527 // Lower the estimate to trigger a new packet REMB packet.
528 EXPECT_CALL(rtp, SetREMBData(bitrate_estimate - 100, ssrcs)).Times(1);
529 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate - 100);
530
531 EXPECT_CALL(rtp, SetREMBStatus(false)).Times(1);
532 packet_router.RemoveReceiveRtpModule(&rtp);
533}
534
eladalon822ff2b2017-08-01 06:30:28 -0700535TEST(PacketRouterRembTest, NonCandidateSendRtpModuleNotUsedForRemb) {
536 rtc::ScopedFakeClock clock;
537 PacketRouter packet_router;
538 NiceMock<MockRtpRtcpWithRembTracking> module;
539
540 constexpr bool remb_candidate = false;
541
542 packet_router.AddSendRtpModule(&module, remb_candidate);
543 EXPECT_FALSE(module.REMB());
544
545 constexpr uint32_t bitrate_estimate = 456;
546 const std::vector<uint32_t> ssrcs = {1234};
547 EXPECT_CALL(module, SetREMBData(_, _)).Times(0);
548 clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1000));
549 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
550
551 // Test tear-down
552 packet_router.RemoveSendRtpModule(&module);
553}
554
555TEST(PacketRouterRembTest, CandidateSendRtpModuleUsedForRemb) {
556 rtc::ScopedFakeClock clock;
557 PacketRouter packet_router;
558 NiceMock<MockRtpRtcpWithRembTracking> module;
559
560 constexpr bool remb_candidate = true;
561
562 packet_router.AddSendRtpModule(&module, remb_candidate);
563 EXPECT_TRUE(module.REMB());
564
565 constexpr uint32_t bitrate_estimate = 456;
566 const std::vector<uint32_t> ssrcs = {1234};
567 EXPECT_CALL(module, SetREMBData(bitrate_estimate, ssrcs)).Times(1);
568 clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1000));
569 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
570
571 // Test tear-down
572 packet_router.RemoveSendRtpModule(&module);
573}
574
575TEST(PacketRouterRembTest, NonCandidateReceiveRtpModuleNotUsedForRemb) {
576 rtc::ScopedFakeClock clock;
577 PacketRouter packet_router;
578 NiceMock<MockRtpRtcpWithRembTracking> module;
579
580 constexpr bool remb_candidate = false;
581
582 packet_router.AddReceiveRtpModule(&module, remb_candidate);
583 ASSERT_FALSE(module.REMB());
584
585 constexpr uint32_t bitrate_estimate = 456;
586 const std::vector<uint32_t> ssrcs = {1234};
587 EXPECT_CALL(module, SetREMBData(_, _)).Times(0);
588 clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1000));
589 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
590
591 // Test tear-down
592 packet_router.RemoveReceiveRtpModule(&module);
593}
594
595TEST(PacketRouterRembTest, CandidateReceiveRtpModuleUsedForRemb) {
596 rtc::ScopedFakeClock clock;
597 PacketRouter packet_router;
598 NiceMock<MockRtpRtcpWithRembTracking> module;
599
600 constexpr bool remb_candidate = true;
601
602 packet_router.AddReceiveRtpModule(&module, remb_candidate);
603 EXPECT_TRUE(module.REMB());
604
605 constexpr uint32_t bitrate_estimate = 456;
606 const std::vector<uint32_t> ssrcs = {1234};
607 EXPECT_CALL(module, SetREMBData(bitrate_estimate, ssrcs)).Times(1);
608 clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1000));
609 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
610
611 // Test tear-down
612 packet_router.RemoveReceiveRtpModule(&module);
613}
614
615TEST(PacketRouterRembTest,
616 SendCandidatePreferredOverReceiveCandidate_SendModuleAddedFirst) {
617 rtc::ScopedFakeClock clock;
618 PacketRouter packet_router;
619 NiceMock<MockRtpRtcpWithRembTracking> send_module;
620 NiceMock<MockRtpRtcpWithRembTracking> receive_module;
621
622 constexpr bool remb_candidate = true;
623
624 // Send module added - activated.
625 packet_router.AddSendRtpModule(&send_module, remb_candidate);
626 ASSERT_TRUE(send_module.REMB());
627
628 // Receive module added - the send module remains the active one.
629 packet_router.AddReceiveRtpModule(&receive_module, remb_candidate);
630 EXPECT_TRUE(send_module.REMB());
631 EXPECT_FALSE(receive_module.REMB());
632
633 constexpr uint32_t bitrate_estimate = 456;
634 const std::vector<uint32_t> ssrcs = {1234};
635 EXPECT_CALL(send_module, SetREMBData(bitrate_estimate, ssrcs)).Times(1);
636 EXPECT_CALL(receive_module, SetREMBData(_, _)).Times(0);
637
638 clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1000));
639 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
640
641 // Test tear-down
642 packet_router.RemoveReceiveRtpModule(&receive_module);
643 packet_router.RemoveSendRtpModule(&send_module);
644}
645
646TEST(PacketRouterRembTest,
647 SendCandidatePreferredOverReceiveCandidate_ReceiveModuleAddedFirst) {
648 rtc::ScopedFakeClock clock;
649 PacketRouter packet_router;
650 NiceMock<MockRtpRtcpWithRembTracking> send_module;
651 NiceMock<MockRtpRtcpWithRembTracking> receive_module;
652
653 constexpr bool remb_candidate = true;
654
655 // Receive module added - activated.
656 packet_router.AddReceiveRtpModule(&receive_module, remb_candidate);
657 ASSERT_TRUE(receive_module.REMB());
658
659 // Send module added - replaces receive module as active.
660 packet_router.AddSendRtpModule(&send_module, remb_candidate);
661 EXPECT_FALSE(receive_module.REMB());
662 EXPECT_TRUE(send_module.REMB());
663
664 constexpr uint32_t bitrate_estimate = 456;
665 const std::vector<uint32_t> ssrcs = {1234};
666 EXPECT_CALL(send_module, SetREMBData(bitrate_estimate, ssrcs)).Times(1);
667 EXPECT_CALL(receive_module, SetREMBData(_, _)).Times(0);
668
669 clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1000));
670 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
671
672 // Test tear-down
673 packet_router.RemoveReceiveRtpModule(&receive_module);
674 packet_router.RemoveSendRtpModule(&send_module);
675}
676
677TEST(PacketRouterRembTest, ReceiveModuleTakesOverWhenLastSendModuleRemoved) {
678 rtc::ScopedFakeClock clock;
679 PacketRouter packet_router;
680 NiceMock<MockRtpRtcpWithRembTracking> send_module;
681 NiceMock<MockRtpRtcpWithRembTracking> receive_module;
682
683 constexpr bool remb_candidate = true;
684
685 // Send module active, receive module inactive.
686 packet_router.AddSendRtpModule(&send_module, remb_candidate);
687 packet_router.AddReceiveRtpModule(&receive_module, remb_candidate);
688 ASSERT_TRUE(send_module.REMB());
689 ASSERT_FALSE(receive_module.REMB());
690
691 // Send module removed - receive module becomes active.
692 packet_router.RemoveSendRtpModule(&send_module);
693 EXPECT_FALSE(send_module.REMB());
694 EXPECT_TRUE(receive_module.REMB());
695 constexpr uint32_t bitrate_estimate = 456;
696 const std::vector<uint32_t> ssrcs = {1234};
697 EXPECT_CALL(send_module, SetREMBData(_, _)).Times(0);
698 EXPECT_CALL(receive_module, SetREMBData(bitrate_estimate, ssrcs)).Times(1);
699
700 clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1000));
701 packet_router.OnReceiveBitrateChanged(ssrcs, bitrate_estimate);
702
703 // Test tear-down
704 packet_router.RemoveReceiveRtpModule(&receive_module);
705}
706
Stefan Holmere5904162015-03-26 11:11:06 +0100707} // namespace webrtc