blob: 53283bfb167519d99334e97bba5c10cbf789680c [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
13#include <deque>
14#include <queue>
15#include <vector>
16
17#include "absl/memory/memory.h"
18#include "absl/types/optional.h"
19#include "api/test/simulated_network.h"
20#include "rtc_base/criticalsection.h"
21#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.
29class SimulatedNetwork : public NetworkSimulationInterface {
30 public:
31 using Config = NetworkSimulationInterface::SimulatedNetworkConfig;
32 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.
36 void SetConfig(const Config& config);
37 void PauseTransmissionUntil(int64_t until_us);
38
39 // NetworkSimulationInterface
40 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 };
51 rtc::CriticalSection config_lock_;
52
53 // |process_lock| guards the data structures involved in delay and loss
54 // processes, such as the packet queues.
55 rtc::CriticalSection process_lock_;
56 std::queue<PacketInfo> capacity_link_ RTC_GUARDED_BY(process_lock_);
57 Random random_;
58
59 std::deque<PacketInfo> delay_link_;
60
61 // Link configuration.
62 Config config_ RTC_GUARDED_BY(config_lock_);
63 absl::optional<int64_t> pause_transmission_until_us_
64 RTC_GUARDED_BY(config_lock_);
65
66 // Are we currently dropping a burst of packets?
67 bool bursting_;
68
69 // The probability to drop the packet if we are currently dropping a
70 // burst of packet
71 double prob_loss_bursting_ RTC_GUARDED_BY(config_lock_);
72
73 // The probability to drop a burst of packets.
74 double prob_start_bursting_ RTC_GUARDED_BY(config_lock_);
75 int64_t capacity_delay_error_bytes_ = 0;
76};
77
78} // namespace webrtc
79
80#endif // CALL_SIMULATED_NETWORK_H_