blob: 602c90aac1530cf93a6b140425b4937ce1b93869 [file] [log] [blame]
Sebastian Janssoncec24332019-12-04 14:26:50 +01001/*
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 <utility>
11
12#include "api/test/network_emulation_manager.h"
13#include "call/simulated_network.h"
14
15namespace webrtc {
16
17NetworkEmulationManager::SimulatedNetworkNode::Builder&
18NetworkEmulationManager::SimulatedNetworkNode::Builder::config(
19 BuiltInNetworkBehaviorConfig config) {
20 config_ = config;
21 return *this;
22}
23
24NetworkEmulationManager::SimulatedNetworkNode::Builder&
25NetworkEmulationManager::SimulatedNetworkNode::Builder::delay_ms(
26 int queue_delay_ms) {
27 config_.queue_delay_ms = queue_delay_ms;
28 return *this;
29}
30
31NetworkEmulationManager::SimulatedNetworkNode::Builder&
32NetworkEmulationManager::SimulatedNetworkNode::Builder::capacity_kbps(
33 int link_capacity_kbps) {
34 config_.link_capacity_kbps = link_capacity_kbps;
35 return *this;
36}
37
38NetworkEmulationManager::SimulatedNetworkNode::Builder&
39NetworkEmulationManager::SimulatedNetworkNode::Builder::capacity_Mbps(
40 int link_capacity_Mbps) {
41 config_.link_capacity_kbps = link_capacity_Mbps * 1000;
42 return *this;
43}
44
45NetworkEmulationManager::SimulatedNetworkNode::Builder&
46NetworkEmulationManager::SimulatedNetworkNode::Builder::loss(double loss_rate) {
47 config_.loss_percent = std::round(loss_rate * 100);
48 return *this;
49}
50
Sebastian Jansson39272982019-12-11 19:29:57 +010051NetworkEmulationManager::SimulatedNetworkNode::Builder&
52NetworkEmulationManager::SimulatedNetworkNode::Builder::packet_queue_length(
53 int max_queue_length_in_packets) {
54 config_.queue_length_packets = max_queue_length_in_packets;
55 return *this;
56}
57
Sebastian Janssoncec24332019-12-04 14:26:50 +010058NetworkEmulationManager::SimulatedNetworkNode
59NetworkEmulationManager::SimulatedNetworkNode::Builder::Build() const {
Sebastian Janssonce911262019-12-11 19:08:40 +010060 RTC_CHECK(net_);
61 return Build(net_);
62}
63
64NetworkEmulationManager::SimulatedNetworkNode
65NetworkEmulationManager::SimulatedNetworkNode::Builder::Build(
66 NetworkEmulationManager* net) const {
67 RTC_CHECK(net);
68 RTC_CHECK(net_ == nullptr || net_ == net);
Sebastian Janssoncec24332019-12-04 14:26:50 +010069 SimulatedNetworkNode res;
70 auto behavior = std::make_unique<SimulatedNetwork>(config_);
71 res.simulation = behavior.get();
Sebastian Janssonce911262019-12-11 19:08:40 +010072 res.node = net->CreateEmulatedNode(std::move(behavior));
Sebastian Janssoncec24332019-12-04 14:26:50 +010073 return res;
74}
75} // namespace webrtc