| 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 | #ifndef CALL_SIMULATED_NETWORK_H_ |
| 11 | #define CALL_SIMULATED_NETWORK_H_ |
| 12 |
| 13 | #include <stdint.h> |
| 14 |
| 15 | #include <deque> |
| 16 | #include <queue> |
| 17 | #include <vector> |
| 18 |
| 19 | #include "absl/types/optional.h" |
| 20 | #include "api/sequence_checker.h" |
| 21 | #include "api/test/simulated_network.h" |
| 22 | #include "api/units/data_size.h" |
| 23 | #include "api/units/timestamp.h" |
| 24 | #include "rtc_base/race_checker.h" |
| 25 | #include "rtc_base/random.h" |
| 26 | #include "rtc_base/synchronization/mutex.h" |
| 27 | #include "rtc_base/thread_annotations.h" |
| 28 |
| 29 | namespace webrtc { |
| 30 |
| 31 | // Class simulating a network link. This is a simple and naive solution just |
| 32 | // faking capacity and adding an extra transport delay in addition to the |
| 33 | // capacity introduced delay. |
| 34 | class SimulatedNetwork : public SimulatedNetworkInterface { |
| 35 | public: |
| 36 | using Config = BuiltInNetworkBehaviorConfig; |
| 37 | explicit SimulatedNetwork(Config config, uint64_t random_seed = 1); |
| 38 | ~SimulatedNetwork() override; |
| 39 |
| 40 | // Sets a new configuration. This won't affect packets already in the pipe. |
| 41 | void SetConfig(const Config& config) override; |
| 42 | void UpdateConfig(std::function<void(BuiltInNetworkBehaviorConfig*)> |
| 43 | config_modifier) override; |
| 44 | void PauseTransmissionUntil(int64_t until_us) override; |
| 45 |
| 46 | // NetworkBehaviorInterface |
| 47 | bool EnqueuePacket(PacketInFlightInfo packet) override; |
| 48 | std::vector<PacketDeliveryInfo> DequeueDeliverablePackets( |
| 49 | int64_t receive_time_us) override; |
| 50 |
| 51 | absl::optional<int64_t> NextDeliveryTimeUs() const override; |
| 52 |
| 53 | private: |
| 54 | struct PacketInfo { |
| 55 | PacketInFlightInfo packet; |
| 56 | int64_t arrival_time_us; |
| 57 | }; |
| 58 | // Contains current configuration state. |
| 59 | struct ConfigState { |
| 60 | // Static link configuration. |
| 61 | Config config; |
| 62 | // The probability to drop the packet if we are currently dropping a |
| 63 | // burst of packet |
| 64 | double prob_loss_bursting; |
| 65 | // The probability to drop a burst of packets. |
| 66 | double prob_start_bursting; |
| 67 | // Used for temporary delay spikes. |
| 68 | int64_t pause_transmission_until_us = 0; |
| 69 | }; |
| 70 |
| 71 | // Moves packets from capacity- to delay link. |
| 72 | void UpdateCapacityQueue(ConfigState state, int64_t time_now_us) |
| 73 | RTC_RUN_ON(&process_checker_); |
| 74 | ConfigState GetConfigState() const; |
| 75 |
| 76 | mutable Mutex config_lock_; |
| 77 |
| 78 | // `process_checker_` guards the data structures involved in delay and loss |
| 79 | // processes, such as the packet queues. |
| 80 | rtc::RaceChecker process_checker_; |
| 81 | std::queue<PacketInfo> capacity_link_ RTC_GUARDED_BY(process_checker_); |
| 82 | Random random_; |
| 83 |
| 84 | std::deque<PacketInfo> delay_link_ RTC_GUARDED_BY(process_checker_); |
| 85 |
| 86 | ConfigState config_state_ RTC_GUARDED_BY(config_lock_); |
| 87 |
| 88 | // Are we currently dropping a burst of packets? |
| 89 | bool bursting_; |
| 90 |
| 91 | int64_t queue_size_bytes_ RTC_GUARDED_BY(process_checker_) = 0; |
| 92 | int64_t pending_drain_bits_ RTC_GUARDED_BY(process_checker_) = 0; |
| 93 | absl::optional<int64_t> last_capacity_link_visit_us_ |
| 94 | RTC_GUARDED_BY(process_checker_); |
| 95 | absl::optional<int64_t> next_process_time_us_ |
| 96 | RTC_GUARDED_BY(process_checker_); |
| 97 | }; |
| 98 |
| 99 | } // namespace webrtc |
| 100 |
| 101 | #endif // CALL_SIMULATED_NETWORK_H_ |
| 102 |