blob: 3fba61f74d0dc3c290a04aa03151bc99d7cac157 [file] [log] [blame]
Patrik Höglundb6b29e02018-06-21 16:58:01 +02001/*
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#ifndef API_TEST_SIMULATED_NETWORK_H_
12#define API_TEST_SIMULATED_NETWORK_H_
13
14#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
Patrik Höglundb6b29e02018-06-21 16:58:01 +020017#include <deque>
18#include <queue>
19#include <vector>
20
Danil Chapovalov065a52a2018-07-09 10:58:54 +020021#include "absl/types/optional.h"
Patrik Höglundb6b29e02018-06-21 16:58:01 +020022#include "rtc_base/random.h"
23#include "rtc_base/thread_annotations.h"
24
25namespace webrtc {
26
27struct PacketInFlightInfo {
28 PacketInFlightInfo(size_t size, int64_t send_time_us, uint64_t packet_id)
29 : size(size), send_time_us(send_time_us), packet_id(packet_id) {}
30
31 size_t size;
32 int64_t send_time_us;
33 // Unique identifier for the packet in relation to other packets in flight.
34 uint64_t packet_id;
35};
36
37struct PacketDeliveryInfo {
38 static constexpr int kNotReceived = -1;
39 PacketDeliveryInfo(PacketInFlightInfo source, int64_t receive_time_us)
40 : receive_time_us(receive_time_us), packet_id(source.packet_id) {}
41 int64_t receive_time_us;
42 uint64_t packet_id;
43};
44
Artem Titov666fb322018-10-08 11:31:09 +020045// BuiltInNetworkBehaviorConfig is a built-in network behavior configuration
46// for built-in network behavior that will be used by WebRTC if no custom
Artem Titov24ee1672018-10-04 13:48:48 +020047// NetworkBehaviorInterface is provided.
Artem Titov666fb322018-10-08 11:31:09 +020048struct BuiltInNetworkBehaviorConfig {
49 BuiltInNetworkBehaviorConfig() {}
Artem Titove9721f22018-08-16 11:41:44 +020050 // Queue length in number of packets.
51 size_t queue_length_packets = 0;
52 // Delay in addition to capacity induced delay.
53 int queue_delay_ms = 0;
54 // Standard deviation of the extra delay.
55 int delay_standard_deviation_ms = 0;
56 // Link capacity in kbps.
57 int link_capacity_kbps = 0;
58 // Random packet loss.
59 int loss_percent = 0;
60 // If packets are allowed to be reordered.
61 bool allow_reordering = false;
62 // The average length of a burst of lost packets.
63 int avg_burst_loss_length = -1;
Sebastian Jansson8c8feb92019-01-29 15:59:17 +010064 // Additional bytes to add to packet size.
65 int packet_overhead = 0;
Sebastian Jansson2b08e312019-02-25 10:24:46 +010066 // Enable CoDel active queue management.
67 bool codel_active_queue_management = false;
Artem Titove9721f22018-08-16 11:41:44 +020068};
69
Artem Titov24ee1672018-10-04 13:48:48 +020070class NetworkBehaviorInterface {
Patrik Höglundb6b29e02018-06-21 16:58:01 +020071 public:
Patrik Höglundb6b29e02018-06-21 16:58:01 +020072 virtual bool EnqueuePacket(PacketInFlightInfo packet_info) = 0;
73 // Retrieves all packets that should be delivered by the given receive time.
74 virtual std::vector<PacketDeliveryInfo> DequeueDeliverablePackets(
75 int64_t receive_time_us) = 0;
Artem Titovc8e202f2018-08-27 14:59:29 +020076 // Returns time in microseconds when caller should call
77 // DequeueDeliverablePackets to get next set of packets to deliver.
Patrik Höglundb6b29e02018-06-21 16:58:01 +020078 virtual absl::optional<int64_t> NextDeliveryTimeUs() const = 0;
Artem Titov24ee1672018-10-04 13:48:48 +020079 virtual ~NetworkBehaviorInterface() = default;
Patrik Höglundb6b29e02018-06-21 16:58:01 +020080};
81
Sebastian Janssoncec24332019-12-04 14:26:50 +010082// Class simulating a network link. This is a simple and naive solution just
83// faking capacity and adding an extra transport delay in addition to the
84// capacity introduced delay.
85class SimulatedNetworkInterface : public NetworkBehaviorInterface {
86 public:
87 // Sets a new configuration. This won't affect packets already in the pipe.
88 virtual void SetConfig(const BuiltInNetworkBehaviorConfig& config) = 0;
Sebastian Jansson89eb0bb2020-03-13 17:47:38 +010089 virtual void UpdateConfig(
90 std::function<void(BuiltInNetworkBehaviorConfig*)> config_modifier) = 0;
Sebastian Janssoncec24332019-12-04 14:26:50 +010091 virtual void PauseTransmissionUntil(int64_t until_us) = 0;
92};
93
Patrik Höglundb6b29e02018-06-21 16:58:01 +020094} // namespace webrtc
95
96#endif // API_TEST_SIMULATED_NETWORK_H_