xref: /aosp_15_r20/external/webrtc/api/test/network_emulation_manager.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2019 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 #include "api/test/network_emulation_manager.h"
11 
12 #include <utility>
13 
14 #include "call/simulated_network.h"
15 #include "rtc_base/checks.h"
16 
17 namespace webrtc {
18 
AbslParseFlag(absl::string_view text,TimeMode * mode,std::string * error)19 bool AbslParseFlag(absl::string_view text, TimeMode* mode, std::string* error) {
20   if (text == "realtime") {
21     *mode = TimeMode::kRealTime;
22     return true;
23   }
24   if (text == "simulated") {
25     *mode = TimeMode::kSimulated;
26     return true;
27   }
28   *error =
29       "Unknown value for TimeMode enum. Options are 'realtime' or 'simulated'";
30   return false;
31 }
32 
AbslUnparseFlag(TimeMode mode)33 std::string AbslUnparseFlag(TimeMode mode) {
34   switch (mode) {
35     case TimeMode::kRealTime:
36       return "realtime";
37     case TimeMode::kSimulated:
38       return "simulated";
39   }
40   RTC_CHECK_NOTREACHED();
41   return "unknown";
42 }
43 
44 NetworkEmulationManager::SimulatedNetworkNode::Builder&
config(BuiltInNetworkBehaviorConfig config)45 NetworkEmulationManager::SimulatedNetworkNode::Builder::config(
46     BuiltInNetworkBehaviorConfig config) {
47   config_ = config;
48   return *this;
49 }
50 
51 NetworkEmulationManager::SimulatedNetworkNode::Builder&
delay_ms(int queue_delay_ms)52 NetworkEmulationManager::SimulatedNetworkNode::Builder::delay_ms(
53     int queue_delay_ms) {
54   config_.queue_delay_ms = queue_delay_ms;
55   return *this;
56 }
57 
58 NetworkEmulationManager::SimulatedNetworkNode::Builder&
capacity_kbps(int link_capacity_kbps)59 NetworkEmulationManager::SimulatedNetworkNode::Builder::capacity_kbps(
60     int link_capacity_kbps) {
61   config_.link_capacity_kbps = link_capacity_kbps;
62   return *this;
63 }
64 
65 NetworkEmulationManager::SimulatedNetworkNode::Builder&
capacity_Mbps(int link_capacity_Mbps)66 NetworkEmulationManager::SimulatedNetworkNode::Builder::capacity_Mbps(
67     int link_capacity_Mbps) {
68   config_.link_capacity_kbps = link_capacity_Mbps * 1000;
69   return *this;
70 }
71 
72 NetworkEmulationManager::SimulatedNetworkNode::Builder&
loss(double loss_rate)73 NetworkEmulationManager::SimulatedNetworkNode::Builder::loss(double loss_rate) {
74   config_.loss_percent = std::round(loss_rate * 100);
75   return *this;
76 }
77 
78 NetworkEmulationManager::SimulatedNetworkNode::Builder&
packet_queue_length(int max_queue_length_in_packets)79 NetworkEmulationManager::SimulatedNetworkNode::Builder::packet_queue_length(
80     int max_queue_length_in_packets) {
81   config_.queue_length_packets = max_queue_length_in_packets;
82   return *this;
83 }
84 
85 NetworkEmulationManager::SimulatedNetworkNode
Build(uint64_t random_seed) const86 NetworkEmulationManager::SimulatedNetworkNode::Builder::Build(
87     uint64_t random_seed) const {
88   RTC_CHECK(net_);
89   return Build(net_, random_seed);
90 }
91 
92 NetworkEmulationManager::SimulatedNetworkNode
Build(NetworkEmulationManager * net,uint64_t random_seed) const93 NetworkEmulationManager::SimulatedNetworkNode::Builder::Build(
94     NetworkEmulationManager* net,
95     uint64_t random_seed) const {
96   RTC_CHECK(net);
97   RTC_CHECK(net_ == nullptr || net_ == net);
98   SimulatedNetworkNode res;
99   auto behavior = std::make_unique<SimulatedNetwork>(config_, random_seed);
100   res.simulation = behavior.get();
101   res.node = net->CreateEmulatedNode(std::move(behavior));
102   return res;
103 }
104 
105 std::pair<EmulatedNetworkManagerInterface*, EmulatedNetworkManagerInterface*>
CreateEndpointPairWithTwoWayRoutes(const BuiltInNetworkBehaviorConfig & config)106 NetworkEmulationManager::CreateEndpointPairWithTwoWayRoutes(
107     const BuiltInNetworkBehaviorConfig& config) {
108   auto* alice_node = CreateEmulatedNode(config);
109   auto* bob_node = CreateEmulatedNode(config);
110 
111   auto* alice_endpoint = CreateEndpoint(EmulatedEndpointConfig());
112   auto* bob_endpoint = CreateEndpoint(EmulatedEndpointConfig());
113 
114   CreateRoute(alice_endpoint, {alice_node}, bob_endpoint);
115   CreateRoute(bob_endpoint, {bob_node}, alice_endpoint);
116 
117   return {
118       CreateEmulatedNetworkManagerInterface({alice_endpoint}),
119       CreateEmulatedNetworkManagerInterface({bob_endpoint}),
120   };
121 }
122 }  // namespace webrtc
123