Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(508)

Side by Side Diff: webrtc/test/fake_network_pipe.cc

Issue 1606183002: Allow packets to be reordered in the fake network pipe. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Comments Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 18 matching lines...) Expand all
29 double uniform2 = (rand() + 1.0) / (RAND_MAX + 1.0); // NOLINT 29 double uniform2 = (rand() + 1.0) / (RAND_MAX + 1.0); // NOLINT
30 return static_cast<int>(mean_delay_ms + standard_deviation_ms * 30 return static_cast<int>(mean_delay_ms + standard_deviation_ms *
31 sqrt(-2 * log(uniform1)) * cos(2 * kPi * uniform2)); 31 sqrt(-2 * log(uniform1)) * cos(2 * kPi * uniform2));
32 } 32 }
33 33
34 static bool UniformLoss(int loss_percent) { 34 static bool UniformLoss(int loss_percent) {
35 int outcome = rand() % 100; 35 int outcome = rand() % 100;
36 return outcome < loss_percent; 36 return outcome < loss_percent;
37 } 37 }
38 38
39 class NetworkPacket {
40 public:
41 NetworkPacket(const uint8_t* data, size_t length, int64_t send_time,
42 int64_t arrival_time)
43 : data_(NULL),
44 data_length_(length),
45 send_time_(send_time),
46 arrival_time_(arrival_time) {
47 data_ = new uint8_t[length];
48 memcpy(data_, data, length);
49 }
50 ~NetworkPacket() {
51 delete [] data_;
52 }
53
54 uint8_t* data() const { return data_; }
55 size_t data_length() const { return data_length_; }
56 int64_t send_time() const { return send_time_; }
57 int64_t arrival_time() const { return arrival_time_; }
58 void IncrementArrivalTime(int64_t extra_delay) {
59 arrival_time_+= extra_delay;
60 }
61
62 private:
63 // The packet data.
64 uint8_t* data_;
65 // Length of data_.
66 size_t data_length_;
67 // The time the packet was sent out on the network.
68 const int64_t send_time_;
69 // The time the packet should arrive at the reciver.
70 int64_t arrival_time_;
71 };
72
73 FakeNetworkPipe::FakeNetworkPipe(Clock* clock, 39 FakeNetworkPipe::FakeNetworkPipe(Clock* clock,
74 const FakeNetworkPipe::Config& config) 40 const FakeNetworkPipe::Config& config)
75 : clock_(clock), 41 : clock_(clock),
76 packet_receiver_(NULL), 42 packet_receiver_(NULL),
77 config_(config), 43 config_(config),
78 dropped_packets_(0), 44 dropped_packets_(0),
79 sent_packets_(0), 45 sent_packets_(0),
80 total_packet_delay_(0), 46 total_packet_delay_(0),
81 next_process_time_(clock_->TimeInMilliseconds()) {} 47 next_process_time_(clock_->TimeInMilliseconds()) {}
82 48
83 FakeNetworkPipe::~FakeNetworkPipe() { 49 FakeNetworkPipe::~FakeNetworkPipe() {
84 while (!capacity_link_.empty()) { 50 while (!capacity_link_.empty()) {
85 delete capacity_link_.front(); 51 delete capacity_link_.front();
86 capacity_link_.pop(); 52 capacity_link_.pop();
87 } 53 }
88 while (!delay_link_.empty()) { 54 while (!delay_link_.empty()) {
89 delete delay_link_.front(); 55 delete *delay_link_.begin();
90 delay_link_.pop(); 56 delay_link_.erase(delay_link_.begin());
91 } 57 }
92 } 58 }
93 59
94 void FakeNetworkPipe::SetReceiver(PacketReceiver* receiver) { 60 void FakeNetworkPipe::SetReceiver(PacketReceiver* receiver) {
95 packet_receiver_ = receiver; 61 packet_receiver_ = receiver;
96 } 62 }
97 63
98 void FakeNetworkPipe::SetConfig(const FakeNetworkPipe::Config& config) { 64 void FakeNetworkPipe::SetConfig(const FakeNetworkPipe::Config& config) {
99 rtc::CritScope crit(&lock_); 65 rtc::CritScope crit(&lock_);
100 config_ = config; // Shallow copy of the struct. 66 config_ = config; // Shallow copy of the struct.
(...skipping 15 matching lines...) Expand all
116 int64_t time_now = clock_->TimeInMilliseconds(); 82 int64_t time_now = clock_->TimeInMilliseconds();
117 83
118 // Delay introduced by the link capacity. 84 // Delay introduced by the link capacity.
119 int64_t capacity_delay_ms = 0; 85 int64_t capacity_delay_ms = 0;
120 if (config_.link_capacity_kbps > 0) 86 if (config_.link_capacity_kbps > 0)
121 capacity_delay_ms = data_length / (config_.link_capacity_kbps / 8); 87 capacity_delay_ms = data_length / (config_.link_capacity_kbps / 8);
122 int64_t network_start_time = time_now; 88 int64_t network_start_time = time_now;
123 89
124 // Check if there already are packets on the link and change network start 90 // Check if there already are packets on the link and change network start
125 // time if there is. 91 // time if there is.
126 if (capacity_link_.size() > 0) 92 if (!capacity_link_.empty())
127 network_start_time = capacity_link_.back()->arrival_time(); 93 network_start_time = capacity_link_.back()->arrival_time();
128 94
129 int64_t arrival_time = network_start_time + capacity_delay_ms; 95 int64_t arrival_time = network_start_time + capacity_delay_ms;
130 NetworkPacket* packet = new NetworkPacket(data, data_length, time_now, 96 NetworkPacket* packet = new NetworkPacket(data, data_length, time_now,
131 arrival_time); 97 arrival_time);
132 capacity_link_.push(packet); 98 capacity_link_.push(packet);
133 } 99 }
134 100
135 float FakeNetworkPipe::PercentageLoss() { 101 float FakeNetworkPipe::PercentageLoss() {
136 rtc::CritScope crit(&lock_); 102 rtc::CritScope crit(&lock_);
(...skipping 11 matching lines...) Expand all
148 114
149 return total_packet_delay_ / static_cast<int>(sent_packets_); 115 return total_packet_delay_ / static_cast<int>(sent_packets_);
150 } 116 }
151 117
152 void FakeNetworkPipe::Process() { 118 void FakeNetworkPipe::Process() {
153 int64_t time_now = clock_->TimeInMilliseconds(); 119 int64_t time_now = clock_->TimeInMilliseconds();
154 std::queue<NetworkPacket*> packets_to_deliver; 120 std::queue<NetworkPacket*> packets_to_deliver;
155 { 121 {
156 rtc::CritScope crit(&lock_); 122 rtc::CritScope crit(&lock_);
157 // Check the capacity link first. 123 // Check the capacity link first.
158 while (capacity_link_.size() > 0 && 124 while (!capacity_link_.empty() &&
159 time_now >= capacity_link_.front()->arrival_time()) { 125 time_now >= capacity_link_.front()->arrival_time()) {
160 // Time to get this packet. 126 // Time to get this packet.
161 NetworkPacket* packet = capacity_link_.front(); 127 NetworkPacket* packet = capacity_link_.front();
162 capacity_link_.pop(); 128 capacity_link_.pop();
163 129
164 // Packets are randomly dropped after being affected by the bottleneck. 130 // Packets are randomly dropped after being affected by the bottleneck.
165 if (UniformLoss(config_.loss_percent)) { 131 if (UniformLoss(config_.loss_percent)) {
166 delete packet; 132 delete packet;
167 continue; 133 continue;
168 } 134 }
169 135
170 // Add extra delay and jitter, but make sure the arrival time is not 136 int arrival_time_jitter = GaussianRandom(config_.queue_delay_ms,
171 // earlier than the last packet in the queue.
172 int extra_delay = GaussianRandom(config_.queue_delay_ms,
173 config_.delay_standard_deviation_ms); 137 config_.delay_standard_deviation_ms);
174 if (delay_link_.size() > 0 && 138
175 packet->arrival_time() + extra_delay < 139 // If reordering is not allowed then adjust arrival_time_jitter
176 delay_link_.back()->arrival_time()) { 140 // to make sure all packets are sent in order.
177 extra_delay = delay_link_.back()->arrival_time() - 141 if (!config_.allow_reordering &&
142 !delay_link_.empty() &&
143 packet->arrival_time() + arrival_time_jitter <
144 (*delay_link_.rbegin())->arrival_time()) {
145 arrival_time_jitter = (*delay_link_.rbegin())->arrival_time() -
178 packet->arrival_time(); 146 packet->arrival_time();
179 } 147 }
180 packet->IncrementArrivalTime(extra_delay); 148 packet->IncrementArrivalTime(arrival_time_jitter);
181 if (packet->arrival_time() < next_process_time_) 149 if (packet->arrival_time() < next_process_time_)
182 next_process_time_ = packet->arrival_time(); 150 next_process_time_ = packet->arrival_time();
183 delay_link_.push(packet); 151 delay_link_.insert(packet);
184 } 152 }
185 153
186 // Check the extra delay queue. 154 // Check the extra delay queue.
187 while (delay_link_.size() > 0 && 155 while (!delay_link_.empty() &&
188 time_now >= delay_link_.front()->arrival_time()) { 156 time_now >= (*delay_link_.begin())->arrival_time()) {
189 // Deliver this packet. 157 // Deliver this packet.
190 NetworkPacket* packet = delay_link_.front(); 158 NetworkPacket* packet = *delay_link_.begin();
191 packets_to_deliver.push(packet); 159 packets_to_deliver.push(packet);
192 delay_link_.pop(); 160 delay_link_.erase(delay_link_.begin());
193 // |time_now| might be later than when the packet should have arrived, due 161 // |time_now| might be later than when the packet should have arrived, due
194 // to NetworkProcess being called too late. For stats, use the time it 162 // to NetworkProcess being called too late. For stats, use the time it
195 // should have been on the link. 163 // should have been on the link.
196 total_packet_delay_ += packet->arrival_time() - packet->send_time(); 164 total_packet_delay_ += packet->arrival_time() - packet->send_time();
197 } 165 }
198 sent_packets_ += packets_to_deliver.size(); 166 sent_packets_ += packets_to_deliver.size();
199 } 167 }
200 while (!packets_to_deliver.empty()) { 168 while (!packets_to_deliver.empty()) {
201 NetworkPacket* packet = packets_to_deliver.front(); 169 NetworkPacket* packet = packets_to_deliver.front();
202 packets_to_deliver.pop(); 170 packets_to_deliver.pop();
203 packet_receiver_->DeliverPacket(MediaType::ANY, packet->data(), 171 packet_receiver_->DeliverPacket(MediaType::ANY, packet->data(),
204 packet->data_length(), PacketTime()); 172 packet->data_length(), PacketTime());
205 delete packet; 173 delete packet;
206 } 174 }
207 } 175 }
208 176
209 int64_t FakeNetworkPipe::TimeUntilNextProcess() const { 177 int64_t FakeNetworkPipe::TimeUntilNextProcess() const {
210 rtc::CritScope crit(&lock_); 178 rtc::CritScope crit(&lock_);
211 const int64_t kDefaultProcessIntervalMs = 30; 179 const int64_t kDefaultProcessIntervalMs = 30;
212 if (capacity_link_.size() == 0 || delay_link_.size() == 0) 180 if (capacity_link_.empty() || delay_link_.empty())
213 return kDefaultProcessIntervalMs; 181 return kDefaultProcessIntervalMs;
214 return std::max<int64_t>(next_process_time_ - clock_->TimeInMilliseconds(), 182 return std::max<int64_t>(next_process_time_ - clock_->TimeInMilliseconds(),
215 0); 183 0);
216 } 184 }
217 185
218 } // namespace webrtc 186 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698