1 /*
2 * Copyright (c) 2018 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/pcc/pcc_network_controller.h"
12
13 #include <memory>
14
15 #include "modules/congestion_controller/pcc/pcc_factory.h"
16 #include "test/gmock.h"
17 #include "test/gtest.h"
18 #include "test/scenario/scenario.h"
19
20 using ::testing::AllOf;
21 using ::testing::Field;
22 using ::testing::Ge;
23 using ::testing::Le;
24 using ::testing::Matcher;
25 using ::testing::Property;
26
27 namespace webrtc {
28 namespace test {
29 namespace {
30
31 const DataRate kInitialBitrate = DataRate::KilobitsPerSec(60);
32 const Timestamp kDefaultStartTime = Timestamp::Millis(10000000);
33
34 constexpr double kDataRateMargin = 0.20;
35 constexpr double kMinDataRateFactor = 1 - kDataRateMargin;
36 constexpr double kMaxDataRateFactor = 1 + kDataRateMargin;
TargetRateCloseTo(DataRate rate)37 inline Matcher<TargetTransferRate> TargetRateCloseTo(DataRate rate) {
38 DataRate min_data_rate = rate * kMinDataRateFactor;
39 DataRate max_data_rate = rate * kMaxDataRateFactor;
40 return Field(&TargetTransferRate::target_rate,
41 AllOf(Ge(min_data_rate), Le(max_data_rate)));
42 }
43
InitialConfig(int starting_bandwidth_kbps=kInitialBitrate.kbps (),int min_data_rate_kbps=0,int max_data_rate_kbps=5* kInitialBitrate.kbps ())44 NetworkControllerConfig InitialConfig(
45 int starting_bandwidth_kbps = kInitialBitrate.kbps(),
46 int min_data_rate_kbps = 0,
47 int max_data_rate_kbps = 5 * kInitialBitrate.kbps()) {
48 NetworkControllerConfig config;
49 config.constraints.at_time = kDefaultStartTime;
50 config.constraints.min_data_rate =
51 DataRate::KilobitsPerSec(min_data_rate_kbps);
52 config.constraints.max_data_rate =
53 DataRate::KilobitsPerSec(max_data_rate_kbps);
54 config.constraints.starting_rate =
55 DataRate::KilobitsPerSec(starting_bandwidth_kbps);
56 return config;
57 }
58
InitialProcessInterval()59 ProcessInterval InitialProcessInterval() {
60 ProcessInterval process_interval;
61 process_interval.at_time = kDefaultStartTime;
62 return process_interval;
63 }
64
65 } // namespace
66
TEST(PccNetworkControllerTest,SendsConfigurationOnFirstProcess)67 TEST(PccNetworkControllerTest, SendsConfigurationOnFirstProcess) {
68 std::unique_ptr<NetworkControllerInterface> controller_;
69 controller_.reset(new pcc::PccNetworkController(InitialConfig()));
70
71 NetworkControlUpdate update =
72 controller_->OnProcessInterval(InitialProcessInterval());
73 EXPECT_THAT(*update.target_rate, TargetRateCloseTo(kInitialBitrate));
74 EXPECT_THAT(*update.pacer_config,
75 Property(&PacerConfig::data_rate, Ge(kInitialBitrate)));
76 }
77
TEST(PccNetworkControllerTest,UpdatesTargetSendRate)78 TEST(PccNetworkControllerTest, UpdatesTargetSendRate) {
79 PccNetworkControllerFactory factory;
80 Scenario s("pcc_unit/updates_rate", false);
81 CallClientConfig config;
82 config.transport.cc_factory = &factory;
83 config.transport.rates.min_rate = DataRate::KilobitsPerSec(10);
84 config.transport.rates.max_rate = DataRate::KilobitsPerSec(1500);
85 config.transport.rates.start_rate = DataRate::KilobitsPerSec(300);
86 auto send_net = s.CreateMutableSimulationNode([](NetworkSimulationConfig* c) {
87 c->bandwidth = DataRate::KilobitsPerSec(500);
88 c->delay = TimeDelta::Millis(100);
89 });
90 auto ret_net = s.CreateMutableSimulationNode(
91 [](NetworkSimulationConfig* c) { c->delay = TimeDelta::Millis(100); });
92
93 auto* client = s.CreateClient("send", config);
94 auto* route = s.CreateRoutes(client, {send_net->node()},
95 s.CreateClient("return", CallClientConfig()),
96 {ret_net->node()});
97 VideoStreamConfig video;
98 video.stream.use_rtx = false;
99 s.CreateVideoStream(route->forward(), video);
100 s.RunFor(TimeDelta::Seconds(30));
101 EXPECT_NEAR(client->target_rate().kbps(), 450, 100);
102 send_net->UpdateConfig([](NetworkSimulationConfig* c) {
103 c->bandwidth = DataRate::KilobitsPerSec(800);
104 c->delay = TimeDelta::Millis(100);
105 });
106 s.RunFor(TimeDelta::Seconds(20));
107 EXPECT_NEAR(client->target_rate().kbps(), 750, 150);
108 send_net->UpdateConfig([](NetworkSimulationConfig* c) {
109 c->bandwidth = DataRate::KilobitsPerSec(200);
110 c->delay = TimeDelta::Millis(200);
111 });
112 ret_net->UpdateConfig(
113 [](NetworkSimulationConfig* c) { c->delay = TimeDelta::Millis(200); });
114 s.RunFor(TimeDelta::Seconds(35));
115 EXPECT_NEAR(client->target_rate().kbps(), 170, 50);
116 }
117
118 } // namespace test
119 } // namespace webrtc
120