blob: 4085600d2c237fd5b8ec866a6ec8be4c12aee35c [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 Janssonf96b1ca2018-08-07 18:58:05 +020021#include "rtc_base/random.h"
22#include "rtc_base/thread_annotations.h"
23
24namespace webrtc {
25
26// Class simulating a network link. This is a simple and naive solution just
27// faking capacity and adding an extra transport delay in addition to the
28// capacity introduced delay.
Artem Titov8ea1e9d2018-10-04 14:46:31 +020029class SimulatedNetwork : public NetworkBehaviorInterface {
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020030 public:
Artem Titov75e36472018-10-08 12:28:56 +020031 using Config = BuiltInNetworkBehaviorConfig;
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020032 explicit SimulatedNetwork(Config config, uint64_t random_seed = 1);
33 ~SimulatedNetwork() override;
34
35 // Sets a new configuration. This won't affect packets already in the pipe.
Artem Titov4ff63cc2018-08-23 10:54:54 +020036 void SetConfig(const Config& config);
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020037 void PauseTransmissionUntil(int64_t until_us);
38
Artem Titov8ea1e9d2018-10-04 14:46:31 +020039 // NetworkBehaviorInterface
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020040 bool EnqueuePacket(PacketInFlightInfo packet) override;
41 std::vector<PacketDeliveryInfo> DequeueDeliverablePackets(
42 int64_t receive_time_us) override;
43
44 absl::optional<int64_t> NextDeliveryTimeUs() const override;
45
46 private:
47 struct PacketInfo {
48 PacketInFlightInfo packet;
49 int64_t arrival_time_us;
50 };
Christoffer Rodbro813c79b2019-01-31 09:25:12 +010051
52 // Moves packets from capacity- to delay link.
53 void UpdateCapacityQueue(int64_t time_now_us);
54
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020055 rtc::CriticalSection config_lock_;
56
57 // |process_lock| guards the data structures involved in delay and loss
58 // processes, such as the packet queues.
59 rtc::CriticalSection process_lock_;
60 std::queue<PacketInfo> capacity_link_ RTC_GUARDED_BY(process_lock_);
61 Random random_;
62
Sebastian Jansson2cd3b4c2018-11-06 19:18:28 +010063 std::deque<PacketInfo> delay_link_ RTC_GUARDED_BY(process_lock_);
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020064
65 // Link configuration.
66 Config config_ RTC_GUARDED_BY(config_lock_);
67 absl::optional<int64_t> pause_transmission_until_us_
68 RTC_GUARDED_BY(config_lock_);
69
70 // Are we currently dropping a burst of packets?
71 bool bursting_;
72
73 // The probability to drop the packet if we are currently dropping a
74 // burst of packet
75 double prob_loss_bursting_ RTC_GUARDED_BY(config_lock_);
76
77 // The probability to drop a burst of packets.
78 double prob_start_bursting_ RTC_GUARDED_BY(config_lock_);
Christoffer Rodbro813c79b2019-01-31 09:25:12 +010079
80 int64_t queue_size_bytes_ RTC_GUARDED_BY(process_lock_) = 0;
81 int64_t pending_drain_bits_ RTC_GUARDED_BY(process_lock_) = 0;
82 absl::optional<int64_t> last_capacity_link_visit_us_
83 RTC_GUARDED_BY(process_lock_);
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020084};
85
86} // namespace webrtc
87
88#endif // CALL_SIMULATED_NETWORK_H_