Patrik Höglund | b6b29e0 | 2018-06-21 14:58:01 | [diff] [blame] | 1 | /* |
| 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 Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 16 | |
Patrik Höglund | b6b29e0 | 2018-06-21 14:58:01 | [diff] [blame] | 17 | #include <deque> |
| 18 | #include <queue> |
| 19 | #include <vector> |
| 20 | |
Danil Chapovalov | 065a52a | 2018-07-09 08:58:54 | [diff] [blame] | 21 | #include "absl/types/optional.h" |
Patrik Höglund | b6b29e0 | 2018-06-21 14:58:01 | [diff] [blame] | 22 | #include "rtc_base/random.h" |
| 23 | #include "rtc_base/thread_annotations.h" |
| 24 | |
| 25 | namespace webrtc { |
| 26 | |
| 27 | struct 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 | |
| 37 | struct 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 Titov | 666fb32 | 2018-10-08 09:31:09 | [diff] [blame] | 45 | // BuiltInNetworkBehaviorConfig is a built-in network behavior configuration |
| 46 | // for built-in network behavior that will be used by WebRTC if no custom |
Artem Titov | 24ee167 | 2018-10-04 11:48:48 | [diff] [blame] | 47 | // NetworkBehaviorInterface is provided. |
Artem Titov | 666fb32 | 2018-10-08 09:31:09 | [diff] [blame] | 48 | struct BuiltInNetworkBehaviorConfig { |
Per Kjellander | 8623c75 | 2021-02-15 10:34:10 | [diff] [blame] | 49 | // Queue length in number of packets. |
Artem Titov | e9721f2 | 2018-08-16 09:41:44 | [diff] [blame] | 50 | size_t queue_length_packets = 0; |
| 51 | // Delay in addition to capacity induced delay. |
| 52 | int queue_delay_ms = 0; |
| 53 | // Standard deviation of the extra delay. |
| 54 | int delay_standard_deviation_ms = 0; |
| 55 | // Link capacity in kbps. |
| 56 | int link_capacity_kbps = 0; |
| 57 | // Random packet loss. |
| 58 | int loss_percent = 0; |
| 59 | // If packets are allowed to be reordered. |
| 60 | bool allow_reordering = false; |
| 61 | // The average length of a burst of lost packets. |
| 62 | int avg_burst_loss_length = -1; |
Sebastian Jansson | 8c8feb9 | 2019-01-29 14:59:17 | [diff] [blame] | 63 | // Additional bytes to add to packet size. |
| 64 | int packet_overhead = 0; |
Artem Titov | e9721f2 | 2018-08-16 09:41:44 | [diff] [blame] | 65 | }; |
| 66 | |
Artem Titov | 24ee167 | 2018-10-04 11:48:48 | [diff] [blame] | 67 | class NetworkBehaviorInterface { |
Patrik Höglund | b6b29e0 | 2018-06-21 14:58:01 | [diff] [blame] | 68 | public: |
Patrik Höglund | b6b29e0 | 2018-06-21 14:58:01 | [diff] [blame] | 69 | virtual bool EnqueuePacket(PacketInFlightInfo packet_info) = 0; |
| 70 | // Retrieves all packets that should be delivered by the given receive time. |
| 71 | virtual std::vector<PacketDeliveryInfo> DequeueDeliverablePackets( |
| 72 | int64_t receive_time_us) = 0; |
Artem Titov | c8e202f | 2018-08-27 12:59:29 | [diff] [blame] | 73 | // Returns time in microseconds when caller should call |
| 74 | // DequeueDeliverablePackets to get next set of packets to deliver. |
Patrik Höglund | b6b29e0 | 2018-06-21 14:58:01 | [diff] [blame] | 75 | virtual absl::optional<int64_t> NextDeliveryTimeUs() const = 0; |
Artem Titov | 24ee167 | 2018-10-04 11:48:48 | [diff] [blame] | 76 | virtual ~NetworkBehaviorInterface() = default; |
Patrik Höglund | b6b29e0 | 2018-06-21 14:58:01 | [diff] [blame] | 77 | }; |
| 78 | |
Sebastian Jansson | cec2433 | 2019-12-04 13:26:50 | [diff] [blame] | 79 | // Class simulating a network link. This is a simple and naive solution just |
| 80 | // faking capacity and adding an extra transport delay in addition to the |
| 81 | // capacity introduced delay. |
| 82 | class SimulatedNetworkInterface : public NetworkBehaviorInterface { |
| 83 | public: |
| 84 | // Sets a new configuration. This won't affect packets already in the pipe. |
| 85 | virtual void SetConfig(const BuiltInNetworkBehaviorConfig& config) = 0; |
Sebastian Jansson | 89eb0bb | 2020-03-13 16:47:38 | [diff] [blame] | 86 | virtual void UpdateConfig( |
| 87 | std::function<void(BuiltInNetworkBehaviorConfig*)> config_modifier) = 0; |
Sebastian Jansson | cec2433 | 2019-12-04 13:26:50 | [diff] [blame] | 88 | virtual void PauseTransmissionUntil(int64_t until_us) = 0; |
| 89 | }; |
| 90 | |
Patrik Höglund | b6b29e0 | 2018-06-21 14:58:01 | [diff] [blame] | 91 | } // namespace webrtc |
| 92 | |
| 93 | #endif // API_TEST_SIMULATED_NETWORK_H_ |