blob: c241cdb1c7660ae300d88f00b06d8f9e827a2080 [file] [log] [blame]
Artem Titov7bf8c7f2019-03-15 15:00:37 +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
11#ifndef API_TEST_NETWORK_EMULATION_MANAGER_H_
12#define API_TEST_NETWORK_EMULATION_MANAGER_H_
13
14#include <memory>
15#include <vector>
16
17#include "api/test/simulated_network.h"
Artem Titov806299e2019-04-12 12:17:19 +020018#include "api/units/data_rate.h"
19#include "api/units/data_size.h"
20#include "api/units/timestamp.h"
Artem Titov7bf8c7f2019-03-15 15:00:37 +010021#include "rtc_base/network.h"
22#include "rtc_base/thread.h"
23
24namespace webrtc {
25
26// This API is still in development and can be changed without prior notice.
27
28// These classes are forward declared here, because they used as handles, to
29// make it possible for client code to operate with these abstractions and build
30// required network configuration. With forward declaration here implementation
31// is more readable, than with interfaces approach and cause user needn't any
32// API methods on these abstractions it is acceptable here.
33
34// EmulatedNetworkNode is an abstraction for some network in the real world,
35// like 3G network between peers, or Wi-Fi for one peer and LTE for another.
36// Multiple networks can be joined into chain emulating a network path from
37// one peer to another.
38class EmulatedNetworkNode;
39// EmulatedEndpoint is and abstraction for network interface on device.
40class EmulatedEndpoint;
41// EmulatedRoute is handle for single route from one network interface on one
42// peer device to another network interface on another peer device.
43class EmulatedRoute;
44
45struct EmulatedEndpointConfig {
46 enum class IpAddressFamily { kIpv4, kIpv6 };
47
48 IpAddressFamily generated_ip_family = IpAddressFamily::kIpv4;
49 // If specified will be used as IP address for endpoint node. Must be unique
50 // among all created nodes.
51 absl::optional<rtc::IPAddress> ip;
Artem Titove5cc85b2019-03-28 12:11:09 +010052 // Should endpoint be enabled or not, when it will be created.
53 // Enabled endpoints will be available for webrtc to send packets.
54 bool start_as_enabled = true;
55};
56
Artem Titov806299e2019-04-12 12:17:19 +020057struct EmulatedNetworkStats {
58 int64_t packets_sent = 0;
59 DataSize bytes_sent = DataSize::Zero();
60 // Total amount of packets received with or without destination.
61 int64_t packets_received = 0;
62 // Total amount of bytes in received packets.
63 DataSize bytes_received = DataSize::Zero();
64 // Total amount of packets that were received, but no destination was found.
65 int64_t packets_dropped = 0;
66 // Total amount of bytes in dropped packets.
67 DataSize bytes_dropped = DataSize::Zero();
68
69 DataSize first_received_packet_size = DataSize::Zero();
70 DataSize first_sent_packet_size = DataSize::Zero();
71
72 Timestamp first_packet_sent_time = Timestamp::PlusInfinity();
73 Timestamp last_packet_sent_time = Timestamp::PlusInfinity();
74 Timestamp first_packet_received_time = Timestamp::PlusInfinity();
75 Timestamp last_packet_received_time = Timestamp::PlusInfinity();
76
77 DataRate AverageSendRate() const {
78 RTC_DCHECK_GE(packets_sent, 2);
79 return (bytes_sent - first_sent_packet_size) /
80 (last_packet_sent_time - first_packet_sent_time);
81 }
82 DataRate AverageReceiveRate() const {
83 RTC_DCHECK_GE(packets_received, 2);
84 return (bytes_received - first_received_packet_size) /
85 (last_packet_received_time - first_packet_received_time);
86 }
87};
88
Artem Titove5cc85b2019-03-28 12:11:09 +010089// Provide interface to obtain all required objects to inject network emulation
Artem Titov806299e2019-04-12 12:17:19 +020090// layer into PeerConnection. Also contains information about network interfaces
91// accessible by PeerConnection.
Artem Titove5cc85b2019-03-28 12:11:09 +010092class EmulatedNetworkManagerInterface {
93 public:
94 virtual ~EmulatedNetworkManagerInterface() = default;
95
96 virtual rtc::Thread* network_thread() = 0;
97 virtual rtc::NetworkManager* network_manager() = 0;
Artem Titov806299e2019-04-12 12:17:19 +020098
99 // Returns summarized network stats for endpoints for this manager.
100 virtual void GetStats(
101 std::function<void(EmulatedNetworkStats)> stats_callback) const = 0;
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100102};
103
104// Provides an API for creating and configuring emulated network layer.
105// All objects returned by this API are owned by NetworkEmulationManager itself
106// and will be deleted when manager will be deleted.
107class NetworkEmulationManager {
108 public:
109 virtual ~NetworkEmulationManager() = default;
110
111 // Creates an emulated network node, which represents single network in
112 // the emulated network layer.
113 virtual EmulatedNetworkNode* CreateEmulatedNode(
Artem Titov48b1b182019-07-05 13:09:48 +0200114 BuiltInNetworkBehaviorConfig config) = 0;
115 virtual EmulatedNetworkNode* CreateEmulatedNode(
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100116 std::unique_ptr<NetworkBehaviorInterface> network_behavior) = 0;
117
118 // Creates an emulated endpoint, which represents single network interface on
119 // the peer's device.
120 virtual EmulatedEndpoint* CreateEndpoint(EmulatedEndpointConfig config) = 0;
Artem Titove5cc85b2019-03-28 12:11:09 +0100121 // Enable emulated endpoint to make it available for webrtc.
122 // Caller mustn't enable currently enabled endpoint.
123 virtual void EnableEndpoint(EmulatedEndpoint* endpoint) = 0;
124 // Disable emulated endpoint to make it unavailable for webrtc.
125 // Caller mustn't disable currently disabled endpoint.
126 virtual void DisableEndpoint(EmulatedEndpoint* endpoint) = 0;
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100127
128 // Creates a route between endpoints going through specified network nodes.
129 // This route is single direction only and describe how traffic that was
130 // sent by network interface |from| have to be delivered to the network
Artem Titovff393122019-04-05 11:19:52 +0200131 // interface |to|. Return object can be used to remove created route. The
132 // route must contains at least one network node inside it.
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100133 //
Artem Titovff393122019-04-05 11:19:52 +0200134 // Assume that E{0-9} are endpoints and N{0-9} are network nodes, then
135 // creation of the route have to follow these rules:
136 // 1. A route consists of a source endpoint, an ordered list of one or
137 // more network nodes, and a destination endpoint.
138 // 2. If (E1, ..., E2) is a route, then E1 != E2.
139 // In other words, the source and the destination may not be the same.
140 // 3. Given two simultaneously existing routes (E1, ..., E2) and
141 // (E3, ..., E4), either E1 != E3 or E2 != E4.
142 // In other words, there may be at most one route from any given source
143 // endpoint to any given destination endpoint.
144 // 4. Given two simultaneously existing routes (E1, ..., N1, ..., E2)
145 // and (E3, ..., N2, ..., E4), either N1 != N2 or E2 != E4.
146 // In other words, a network node may not belong to two routes that lead
147 // to the same destination endpoint.
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100148 virtual EmulatedRoute* CreateRoute(
149 EmulatedEndpoint* from,
150 const std::vector<EmulatedNetworkNode*>& via_nodes,
151 EmulatedEndpoint* to) = 0;
152 // Removes route previously created by CreateRoute(...).
153 // Caller mustn't call this function with route, that have been already
154 // removed earlier.
155 virtual void ClearRoute(EmulatedRoute* route) = 0;
156
Artem Titove5cc85b2019-03-28 12:11:09 +0100157 // Creates EmulatedNetworkManagerInterface which can be used then to inject
158 // network emulation layer into PeerConnection. |endpoints| - are available
159 // network interfaces for PeerConnection. If endpoint is enabled, it will be
160 // immediately available for PeerConnection, otherwise user will be able to
161 // enable endpoint later to make it available for PeerConnection.
162 virtual EmulatedNetworkManagerInterface*
163 CreateEmulatedNetworkManagerInterface(
Artem Titov7bf8c7f2019-03-15 15:00:37 +0100164 const std::vector<EmulatedEndpoint*>& endpoints) = 0;
165};
166
167} // namespace webrtc
168
169#endif // API_TEST_NETWORK_EMULATION_MANAGER_H_