2 * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
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.
10 #include "api/test/network_emulation_manager.h"
14 #include "call/simulated_network.h"
15 #include "rtc_base/checks.h"
19 bool AbslParseFlag(absl::string_view text
, TimeMode
* mode
, std::string
* error
) {
20 if (text
== "realtime") {
21 *mode
= TimeMode::kRealTime
;
24 if (text
== "simulated") {
25 *mode
= TimeMode::kSimulated
;
29 "Unknown value for TimeMode enum. Options are 'realtime' or 'simulated'";
33 std::string
AbslUnparseFlag(TimeMode mode
) {
35 case TimeMode::kRealTime
:
37 case TimeMode::kSimulated
:
40 RTC_CHECK_NOTREACHED();
44 NetworkEmulationManager::SimulatedNetworkNode::Builder
&
45 NetworkEmulationManager::SimulatedNetworkNode::Builder::config(
46 BuiltInNetworkBehaviorConfig config
) {
51 NetworkEmulationManager::SimulatedNetworkNode::Builder
&
52 NetworkEmulationManager::SimulatedNetworkNode::Builder::delay_ms(
54 config_
.queue_delay_ms
= queue_delay_ms
;
58 NetworkEmulationManager::SimulatedNetworkNode::Builder
&
59 NetworkEmulationManager::SimulatedNetworkNode::Builder::capacity_kbps(
60 int link_capacity_kbps
) {
61 config_
.link_capacity_kbps
= link_capacity_kbps
;
65 NetworkEmulationManager::SimulatedNetworkNode::Builder
&
66 NetworkEmulationManager::SimulatedNetworkNode::Builder::capacity_Mbps(
67 int link_capacity_Mbps
) {
68 config_
.link_capacity_kbps
= link_capacity_Mbps
* 1000;
72 NetworkEmulationManager::SimulatedNetworkNode::Builder
&
73 NetworkEmulationManager::SimulatedNetworkNode::Builder::loss(double loss_rate
) {
74 config_
.loss_percent
= std::round(loss_rate
* 100);
78 NetworkEmulationManager::SimulatedNetworkNode::Builder
&
79 NetworkEmulationManager::SimulatedNetworkNode::Builder::packet_queue_length(
80 int max_queue_length_in_packets
) {
81 config_
.queue_length_packets
= max_queue_length_in_packets
;
85 NetworkEmulationManager::SimulatedNetworkNode
86 NetworkEmulationManager::SimulatedNetworkNode::Builder::Build(
87 uint64_t random_seed
) const {
89 return Build(net_
, random_seed
);
92 NetworkEmulationManager::SimulatedNetworkNode
93 NetworkEmulationManager::SimulatedNetworkNode::Builder::Build(
94 NetworkEmulationManager
* net
,
95 uint64_t random_seed
) const {
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
));
105 std::pair
<EmulatedNetworkManagerInterface
*, EmulatedNetworkManagerInterface
*>
106 NetworkEmulationManager::CreateEndpointPairWithTwoWayRoutes(
107 const BuiltInNetworkBehaviorConfig
& config
) {
108 auto* alice_node
= CreateEmulatedNode(config
);
109 auto* bob_node
= CreateEmulatedNode(config
);
111 auto* alice_endpoint
= CreateEndpoint(EmulatedEndpointConfig());
112 auto* bob_endpoint
= CreateEndpoint(EmulatedEndpointConfig());
114 CreateRoute(alice_endpoint
, {alice_node
}, bob_endpoint
);
115 CreateRoute(bob_endpoint
, {bob_node
}, alice_endpoint
);
118 CreateEmulatedNetworkManagerInterface({alice_endpoint
}),
119 CreateEmulatedNetworkManagerInterface({bob_endpoint
}),
122 } // namespace webrtc