blob: 632eb5dfedd5d69c50b14752665488ccc590c3bf [file] [log] [blame]
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +02001/*
2 * Copyright 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#ifndef CALL_SIMULATED_NETWORK_H_
11#define CALL_SIMULATED_NETWORK_H_
12
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stdint.h>
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020014#include <deque>
15#include <queue>
16#include <vector>
17
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020018#include "absl/types/optional.h"
19#include "api/test/simulated_network.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/critical_section.h"
Sebastian Janssoneceea312019-01-31 11:50:04 +010021#include "rtc_base/race_checker.h"
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020022#include "rtc_base/random.h"
23#include "rtc_base/thread_annotations.h"
Sebastian Janssoneceea312019-01-31 11:50:04 +010024#include "rtc_base/thread_checker.h"
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020025
26namespace webrtc {
27
28// Class simulating a network link. This is a simple and naive solution just
29// faking capacity and adding an extra transport delay in addition to the
30// capacity introduced delay.
Artem Titov8ea1e9d2018-10-04 14:46:31 +020031class SimulatedNetwork : public NetworkBehaviorInterface {
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020032 public:
Artem Titov75e36472018-10-08 12:28:56 +020033 using Config = BuiltInNetworkBehaviorConfig;
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020034 explicit SimulatedNetwork(Config config, uint64_t random_seed = 1);
35 ~SimulatedNetwork() override;
36
37 // Sets a new configuration. This won't affect packets already in the pipe.
Artem Titov4ff63cc2018-08-23 10:54:54 +020038 void SetConfig(const Config& config);
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020039 void PauseTransmissionUntil(int64_t until_us);
40
Artem Titov8ea1e9d2018-10-04 14:46:31 +020041 // NetworkBehaviorInterface
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020042 bool EnqueuePacket(PacketInFlightInfo packet) override;
43 std::vector<PacketDeliveryInfo> DequeueDeliverablePackets(
44 int64_t receive_time_us) override;
45
46 absl::optional<int64_t> NextDeliveryTimeUs() const override;
47
48 private:
49 struct PacketInfo {
50 PacketInFlightInfo packet;
51 int64_t arrival_time_us;
52 };
Sebastian Janssoneceea312019-01-31 11:50:04 +010053 // Contains current configuration state.
54 struct ConfigState {
55 // Static link configuration.
56 Config config;
57 // The probability to drop the packet if we are currently dropping a
58 // burst of packet
59 double prob_loss_bursting;
60 // The probability to drop a burst of packets.
61 double prob_start_bursting;
62 // Used for temporary delay spikes.
63 int64_t pause_transmission_until_us = 0;
64 };
Christoffer Rodbro813c79b2019-01-31 09:25:12 +010065
66 // Moves packets from capacity- to delay link.
Sebastian Janssoneceea312019-01-31 11:50:04 +010067 void UpdateCapacityQueue(ConfigState state, int64_t time_now_us)
68 RTC_RUN_ON(&process_checker_);
69 ConfigState GetConfigState() const;
Christoffer Rodbro813c79b2019-01-31 09:25:12 +010070
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020071 rtc::CriticalSection config_lock_;
72
Sebastian Janssoneceea312019-01-31 11:50:04 +010073 // |process_checker_| guards the data structures involved in delay and loss
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020074 // processes, such as the packet queues.
Sebastian Janssoneceea312019-01-31 11:50:04 +010075 rtc::RaceChecker process_checker_;
76 std::queue<PacketInfo> capacity_link_ RTC_GUARDED_BY(process_checker_);
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020077 Random random_;
78
Sebastian Janssoneceea312019-01-31 11:50:04 +010079 std::deque<PacketInfo> delay_link_ RTC_GUARDED_BY(process_checker_);
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020080
Sebastian Janssoneceea312019-01-31 11:50:04 +010081 ConfigState config_state_ RTC_GUARDED_BY(config_lock_);
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020082
83 // Are we currently dropping a burst of packets?
84 bool bursting_;
85
Sebastian Janssoneceea312019-01-31 11:50:04 +010086 int64_t queue_size_bytes_ RTC_GUARDED_BY(process_checker_) = 0;
87 int64_t pending_drain_bits_ RTC_GUARDED_BY(process_checker_) = 0;
Christoffer Rodbro813c79b2019-01-31 09:25:12 +010088 absl::optional<int64_t> last_capacity_link_visit_us_
Sebastian Janssoneceea312019-01-31 11:50:04 +010089 RTC_GUARDED_BY(process_checker_);
Sebastian Jansson836fee12019-02-08 16:08:10 +010090 absl::optional<int64_t> next_process_time_us_
91 RTC_GUARDED_BY(process_checker_);
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020092};
93
94} // namespace webrtc
95
96#endif // CALL_SIMULATED_NETWORK_H_