1 /*
2  *  Copyright (c) 2016 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 "modules/congestion_controller/include/receive_side_congestion_controller.h"
12 
13 #include "api/test/network_emulation/create_cross_traffic.h"
14 #include "api/test/network_emulation/cross_traffic.h"
15 #include "modules/pacing/packet_router.h"
16 #include "system_wrappers/include/clock.h"
17 #include "test/gmock.h"
18 #include "test/gtest.h"
19 #include "test/scenario/scenario.h"
20 
21 using ::testing::_;
22 using ::testing::AtLeast;
23 using ::testing::ElementsAre;
24 using ::testing::MockFunction;
25 
26 namespace webrtc {
27 
28 namespace {
29 
30 // Helper to convert some time format to resolution used in absolute send time
31 // header extension, rounded upwards. `t` is the time to convert, in some
32 // resolution. `denom` is the value to divide `t` by to get whole seconds,
33 // e.g. `denom` = 1000 if `t` is in milliseconds.
AbsSendTime(int64_t t,int64_t denom)34 uint32_t AbsSendTime(int64_t t, int64_t denom) {
35   return (((t << 18) + (denom >> 1)) / denom) & 0x00fffffful;
36 }
37 
38 const uint32_t kInitialBitrateBps = 60000;
39 
40 }  // namespace
41 
42 namespace test {
43 
TEST(ReceiveSideCongestionControllerTest,SendsRembWithAbsSendTime)44 TEST(ReceiveSideCongestionControllerTest, SendsRembWithAbsSendTime) {
45   MockFunction<void(std::vector<std::unique_ptr<rtcp::RtcpPacket>>)>
46       feedback_sender;
47   MockFunction<void(uint64_t, std::vector<uint32_t>)> remb_sender;
48   SimulatedClock clock_(123456);
49 
50   ReceiveSideCongestionController controller(
51       &clock_, feedback_sender.AsStdFunction(), remb_sender.AsStdFunction(),
52       nullptr);
53 
54   size_t payload_size = 1000;
55   RTPHeader header;
56   header.ssrc = 0x11eb21c;
57   header.extension.hasAbsoluteSendTime = true;
58 
59   EXPECT_CALL(remb_sender, Call(_, ElementsAre(header.ssrc))).Times(AtLeast(1));
60 
61   for (int i = 0; i < 10; ++i) {
62     clock_.AdvanceTimeMilliseconds((1000 * payload_size) / kInitialBitrateBps);
63     int64_t now_ms = clock_.TimeInMilliseconds();
64     header.extension.absoluteSendTime = AbsSendTime(now_ms, 1000);
65     controller.OnReceivedPacket(now_ms, payload_size, header);
66   }
67 }
68 
TEST(ReceiveSideCongestionControllerTest,SendsRembAfterSetMaxDesiredReceiveBitrate)69 TEST(ReceiveSideCongestionControllerTest,
70      SendsRembAfterSetMaxDesiredReceiveBitrate) {
71   MockFunction<void(std::vector<std::unique_ptr<rtcp::RtcpPacket>>)>
72       feedback_sender;
73   MockFunction<void(uint64_t, std::vector<uint32_t>)> remb_sender;
74   SimulatedClock clock_(123456);
75 
76   ReceiveSideCongestionController controller(
77       &clock_, feedback_sender.AsStdFunction(), remb_sender.AsStdFunction(),
78       nullptr);
79   EXPECT_CALL(remb_sender, Call(123, _));
80   controller.SetMaxDesiredReceiveBitrate(DataRate::BitsPerSec(123));
81 }
82 
TEST(ReceiveSideCongestionControllerTest,ConvergesToCapacity)83 TEST(ReceiveSideCongestionControllerTest, ConvergesToCapacity) {
84   Scenario s("receive_cc_unit/converge");
85   NetworkSimulationConfig net_conf;
86   net_conf.bandwidth = DataRate::KilobitsPerSec(1000);
87   net_conf.delay = TimeDelta::Millis(50);
88   auto* client = s.CreateClient("send", [&](CallClientConfig* c) {
89     c->transport.rates.start_rate = DataRate::KilobitsPerSec(300);
90   });
91 
92   auto* route = s.CreateRoutes(client, {s.CreateSimulationNode(net_conf)},
93                                s.CreateClient("return", CallClientConfig()),
94                                {s.CreateSimulationNode(net_conf)});
95   VideoStreamConfig video;
96   video.stream.packet_feedback = false;
97   s.CreateVideoStream(route->forward(), video);
98   s.RunFor(TimeDelta::Seconds(30));
99   EXPECT_NEAR(client->send_bandwidth().kbps(), 900, 150);
100 }
101 
TEST(ReceiveSideCongestionControllerTest,IsFairToTCP)102 TEST(ReceiveSideCongestionControllerTest, IsFairToTCP) {
103   Scenario s("receive_cc_unit/tcp_fairness");
104   NetworkSimulationConfig net_conf;
105   net_conf.bandwidth = DataRate::KilobitsPerSec(1000);
106   net_conf.delay = TimeDelta::Millis(50);
107   auto* client = s.CreateClient("send", [&](CallClientConfig* c) {
108     c->transport.rates.start_rate = DataRate::KilobitsPerSec(1000);
109   });
110   auto send_net = {s.CreateSimulationNode(net_conf)};
111   auto ret_net = {s.CreateSimulationNode(net_conf)};
112   auto* route = s.CreateRoutes(
113       client, send_net, s.CreateClient("return", CallClientConfig()), ret_net);
114   VideoStreamConfig video;
115   video.stream.packet_feedback = false;
116   s.CreateVideoStream(route->forward(), video);
117   s.net()->StartCrossTraffic(CreateFakeTcpCrossTraffic(
118       s.net()->CreateRoute(send_net), s.net()->CreateRoute(ret_net),
119       FakeTcpConfig()));
120   s.RunFor(TimeDelta::Seconds(30));
121   // For some reason we get outcompeted by TCP here, this should probably be
122   // fixed and a lower bound should be added to the test.
123   EXPECT_LT(client->send_bandwidth().kbps(), 750);
124 }
125 }  // namespace test
126 }  // namespace webrtc
127