blob: e978027092c7793dd3ce430d9bb73ef3775d2ede [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.
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 };
51 rtc::CriticalSection config_lock_;
Sebastian Jansson2cd3b4c2018-11-06 19:18:28 +010052 bool reset_capacity_delay_error_ RTC_GUARDED_BY(config_lock_) = false;
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020053
54 // |process_lock| guards the data structures involved in delay and loss
55 // processes, such as the packet queues.
56 rtc::CriticalSection process_lock_;
57 std::queue<PacketInfo> capacity_link_ RTC_GUARDED_BY(process_lock_);
58 Random random_;
59
Sebastian Jansson2cd3b4c2018-11-06 19:18:28 +010060 std::deque<PacketInfo> delay_link_ RTC_GUARDED_BY(process_lock_);
Sebastian Janssonf96b1ca2018-08-07 18:58:05 +020061
62 // Link configuration.
63 Config config_ RTC_GUARDED_BY(config_lock_);
64 absl::optional<int64_t> pause_transmission_until_us_
65 RTC_GUARDED_BY(config_lock_);
66
67 // Are we currently dropping a burst of packets?
68 bool bursting_;
69
70 // The probability to drop the packet if we are currently dropping a
71 // burst of packet
72 double prob_loss_bursting_ RTC_GUARDED_BY(config_lock_);
73
74 // The probability to drop a burst of packets.
75 double prob_start_bursting_ RTC_GUARDED_BY(config_lock_);
76 int64_t capacity_delay_error_bytes_ = 0;
77};
78
79} // namespace webrtc
80
81#endif // CALL_SIMULATED_NETWORK_H_