mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef TEST_FAKE_NETWORK_PIPE_H_ |
| 12 | #define TEST_FAKE_NETWORK_PIPE_H_ |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 13 | |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 14 | #include <string.h> |
minyue | 20c84cc | 2017-04-10 16:57:57 -0700 | [diff] [blame] | 15 | #include <map> |
| 16 | #include <memory> |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 17 | #include <queue> |
minyue | 20c84cc | 2017-04-10 16:57:57 -0700 | [diff] [blame] | 18 | #include <set> |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 19 | |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 20 | #include "common_types.h" // NOLINT(build/include) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/constructormagic.h" |
| 22 | #include "rtc_base/criticalsection.h" |
| 23 | #include "rtc_base/random.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 24 | #include "typedefs.h" // NOLINT(build/include) |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 28 | class Clock; |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 29 | class PacketReceiver; |
nisse | e5ad5ca | 2017-03-29 23:57:43 -0700 | [diff] [blame] | 30 | enum class MediaType; |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 31 | |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 32 | class NetworkPacket { |
| 33 | public: |
| 34 | NetworkPacket(const uint8_t* data, |
| 35 | size_t length, |
| 36 | int64_t send_time, |
| 37 | int64_t arrival_time) |
| 38 | : data_(new uint8_t[length]), |
| 39 | data_length_(length), |
| 40 | send_time_(send_time), |
| 41 | arrival_time_(arrival_time) { |
| 42 | memcpy(data_.get(), data, length); |
| 43 | } |
| 44 | |
| 45 | uint8_t* data() const { return data_.get(); } |
| 46 | size_t data_length() const { return data_length_; } |
| 47 | int64_t send_time() const { return send_time_; } |
| 48 | int64_t arrival_time() const { return arrival_time_; } |
| 49 | void IncrementArrivalTime(int64_t extra_delay) { |
| 50 | arrival_time_ += extra_delay; |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | // The packet data. |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 55 | std::unique_ptr<uint8_t[]> data_; |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 56 | // Length of data_. |
| 57 | size_t data_length_; |
| 58 | // The time the packet was sent out on the network. |
| 59 | const int64_t send_time_; |
| 60 | // The time the packet should arrive at the receiver. |
| 61 | int64_t arrival_time_; |
| 62 | }; |
| 63 | |
minyue | 20c84cc | 2017-04-10 16:57:57 -0700 | [diff] [blame] | 64 | class Demuxer { |
| 65 | public: |
| 66 | virtual ~Demuxer() = default; |
| 67 | virtual void SetReceiver(PacketReceiver* receiver) = 0; |
| 68 | virtual void DeliverPacket(const NetworkPacket* packet, |
| 69 | const PacketTime& packet_time) = 0; |
| 70 | }; |
| 71 | |
| 72 | class DemuxerImpl final : public Demuxer { |
| 73 | public: |
| 74 | explicit DemuxerImpl(const std::map<uint8_t, MediaType>& payload_type_map); |
| 75 | |
| 76 | void SetReceiver(PacketReceiver* receiver) override; |
| 77 | void DeliverPacket(const NetworkPacket* packet, |
| 78 | const PacketTime& packet_time) override; |
| 79 | |
| 80 | private: |
| 81 | PacketReceiver* packet_receiver_; |
| 82 | const std::map<uint8_t, MediaType> payload_type_map_; |
| 83 | RTC_DISALLOW_COPY_AND_ASSIGN(DemuxerImpl); |
| 84 | }; |
| 85 | |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 86 | // Class faking a network link. This is a simple and naive solution just faking |
| 87 | // capacity and adding an extra transport delay in addition to the capacity |
| 88 | // introduced delay. |
| 89 | |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 90 | class FakeNetworkPipe { |
| 91 | public: |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 92 | struct Config { |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 93 | Config() {} |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 94 | // Queue length in number of packets. |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 95 | size_t queue_length_packets = 0; |
mflodman@webrtc.org | 7acb65a | 2012-12-13 15:53:11 +0000 | [diff] [blame] | 96 | // Delay in addition to capacity induced delay. |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 97 | int queue_delay_ms = 0; |
mflodman@webrtc.org | 7acb65a | 2012-12-13 15:53:11 +0000 | [diff] [blame] | 98 | // Standard deviation of the extra delay. |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 99 | int delay_standard_deviation_ms = 0; |
mflodman@webrtc.org | 7acb65a | 2012-12-13 15:53:11 +0000 | [diff] [blame] | 100 | // Link capacity in kbps. |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 101 | int link_capacity_kbps = 0; |
stefan@webrtc.org | bfe6e08 | 2014-07-31 12:30:18 +0000 | [diff] [blame] | 102 | // Random packet loss. |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 103 | int loss_percent = 0; |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 104 | // If packets are allowed to be reordered. |
| 105 | bool allow_reordering = false; |
philipel | 536378b | 2016-05-31 03:20:23 -0700 | [diff] [blame] | 106 | // The average length of a burst of lost packets. |
| 107 | int avg_burst_loss_length = -1; |
mflodman@webrtc.org | 7acb65a | 2012-12-13 15:53:11 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 110 | FakeNetworkPipe(Clock* clock, |
minyue | 20c84cc | 2017-04-10 16:57:57 -0700 | [diff] [blame] | 111 | const FakeNetworkPipe::Config& config, |
| 112 | std::unique_ptr<Demuxer> demuxer); |
| 113 | FakeNetworkPipe(Clock* clock, |
| 114 | const FakeNetworkPipe::Config& config, |
| 115 | std::unique_ptr<Demuxer> demuxer, |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 116 | uint64_t seed); |
Christoffer Rodbro | d2817d8 | 2017-10-24 16:26:49 +0200 | [diff] [blame] | 117 | virtual ~FakeNetworkPipe(); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 118 | |
henrik.lundin@webrtc.org | c0e9aeb | 2014-02-26 13:34:52 +0000 | [diff] [blame] | 119 | // Sets a new configuration. This won't affect packets already in the pipe. |
| 120 | void SetConfig(const FakeNetworkPipe::Config& config); |
| 121 | |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 122 | // Sends a new packet to the link. |
Christoffer Rodbro | d2817d8 | 2017-10-24 16:26:49 +0200 | [diff] [blame] | 123 | virtual void SendPacket(const uint8_t* packet, size_t packet_length); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 124 | |
minyue | 20c84cc | 2017-04-10 16:57:57 -0700 | [diff] [blame] | 125 | // Must not be called in parallel with SendPacket or Process. |
| 126 | void SetReceiver(PacketReceiver* receiver); |
| 127 | |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 128 | // Processes the network queues and trigger PacketReceiver::IncomingPacket for |
| 129 | // packets ready to be delivered. |
Christoffer Rodbro | d2817d8 | 2017-10-24 16:26:49 +0200 | [diff] [blame] | 130 | virtual void Process(); |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 131 | int64_t TimeUntilNextProcess() const; |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 132 | |
| 133 | // Get statistics. |
| 134 | float PercentageLoss(); |
| 135 | int AverageDelay(); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 136 | size_t dropped_packets() { return dropped_packets_; } |
| 137 | size_t sent_packets() { return sent_packets_; } |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 138 | |
Christoffer Rodbro | d2817d8 | 2017-10-24 16:26:49 +0200 | [diff] [blame] | 139 | protected: |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 140 | Clock* const clock_; |
pbos | 5ad935c | 2016-01-25 03:52:44 -0800 | [diff] [blame] | 141 | rtc::CriticalSection lock_; |
minyue | 20c84cc | 2017-04-10 16:57:57 -0700 | [diff] [blame] | 142 | const std::unique_ptr<Demuxer> demuxer_; |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 143 | std::queue<NetworkPacket*> capacity_link_; |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 144 | Random random_; |
| 145 | |
| 146 | // Since we need to access both the packet with the earliest and latest |
| 147 | // arrival time we need to use a multiset to keep all packets sorted, |
| 148 | // hence, we cannot use a priority queue. |
| 149 | struct PacketArrivalTimeComparator { |
| 150 | bool operator()(const NetworkPacket* p1, const NetworkPacket* p2) { |
| 151 | return p1->arrival_time() < p2->arrival_time(); |
| 152 | } |
| 153 | }; |
| 154 | std::multiset<NetworkPacket*, PacketArrivalTimeComparator> delay_link_; |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 155 | |
| 156 | // Link configuration. |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 157 | Config config_; |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 158 | |
| 159 | // Statistics. |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 160 | size_t dropped_packets_; |
| 161 | size_t sent_packets_; |
Stefan Holmer | ff2a635 | 2016-01-14 10:00:21 +0100 | [diff] [blame] | 162 | int64_t total_packet_delay_; |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 163 | |
philipel | 536378b | 2016-05-31 03:20:23 -0700 | [diff] [blame] | 164 | // Are we currently dropping a burst of packets? |
| 165 | bool bursting_; |
| 166 | |
| 167 | // The probability to drop the packet if we are currently dropping a |
| 168 | // burst of packet |
| 169 | double prob_loss_bursting_; |
| 170 | |
| 171 | // The probability to drop a burst of packets. |
| 172 | double prob_start_bursting_; |
| 173 | |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 174 | int64_t next_process_time_; |
| 175 | |
stefan | e9ad271 | 2017-02-10 06:09:28 -0800 | [diff] [blame] | 176 | int64_t last_log_time_; |
| 177 | |
philipel | 19f5143 | 2017-09-07 09:08:50 -0700 | [diff] [blame] | 178 | int64_t capacity_delay_error_bytes_ = 0; |
| 179 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 180 | RTC_DISALLOW_COPY_AND_ASSIGN(FakeNetworkPipe); |
mflodman@webrtc.org | eaf7cf2 | 2012-12-11 11:47:22 +0000 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | } // namespace webrtc |
| 184 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 185 | #endif // TEST_FAKE_NETWORK_PIPE_H_ |