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