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

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: 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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 // Add extra delay and jitter, but make sure the arrival time is not
171 // earlier than the last packet in the queue. 137 // earlier than the last packet in the queue.
stefan-webrtc 2016/01/20 10:57:33 Half of this comment should be moved down to line
philipel 2016/01/20 14:22:57 Done.
172 int extra_delay = GaussianRandom(config_.queue_delay_ms, 138 int extra_delay = GaussianRandom(config_.queue_delay_ms,
stefan-webrtc 2016/01/20 10:57:33 I think it would be good to rename this random_jit
philipel 2016/01/20 14:22:57 Renamed to arrival_time_jitter.
173 config_.delay_standard_deviation_ms); 139 config_.delay_standard_deviation_ms);
174 if (delay_link_.size() > 0 && 140 if (!config_.allow_reordering &&
141 delay_link_.size() > 0 &&
stefan-webrtc 2016/01/20 10:57:33 !delay_link_.empty()
philipel 2016/01/20 14:22:56 Done.
175 packet->arrival_time() + extra_delay < 142 packet->arrival_time() + extra_delay <
176 delay_link_.back()->arrival_time()) { 143 (*delay_link_.rbegin())->arrival_time()) {
177 extra_delay = delay_link_.back()->arrival_time() - 144 extra_delay = (*delay_link_.rbegin())->arrival_time() -
178 packet->arrival_time(); 145 packet->arrival_time();
179 } 146 }
180 packet->IncrementArrivalTime(extra_delay); 147 packet->IncrementArrivalTime(extra_delay);
181 if (packet->arrival_time() < next_process_time_) 148 if (packet->arrival_time() < next_process_time_)
182 next_process_time_ = packet->arrival_time(); 149 next_process_time_ = packet->arrival_time();
183 delay_link_.push(packet); 150 delay_link_.insert(packet);
184 } 151 }
185 152
186 // Check the extra delay queue. 153 // Check the extra delay queue.
187 while (delay_link_.size() > 0 && 154 while (delay_link_.size() > 0 &&
188 time_now >= delay_link_.front()->arrival_time()) { 155 time_now >= (*delay_link_.begin())->arrival_time()) {
189 // Deliver this packet. 156 // Deliver this packet.
190 NetworkPacket* packet = delay_link_.front(); 157 NetworkPacket* packet = *delay_link_.begin();
191 packets_to_deliver.push(packet); 158 packets_to_deliver.push(packet);
192 delay_link_.pop(); 159 delay_link_.erase(delay_link_.begin());
193 // |time_now| might be later than when the packet should have arrived, due 160 // |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 161 // to NetworkProcess being called too late. For stats, use the time it
195 // should have been on the link. 162 // should have been on the link.
196 total_packet_delay_ += packet->arrival_time() - packet->send_time(); 163 total_packet_delay_ += packet->arrival_time() - packet->send_time();
197 } 164 }
198 sent_packets_ += packets_to_deliver.size(); 165 sent_packets_ += packets_to_deliver.size();
199 } 166 }
200 while (!packets_to_deliver.empty()) { 167 while (!packets_to_deliver.empty()) {
201 NetworkPacket* packet = packets_to_deliver.front(); 168 NetworkPacket* packet = packets_to_deliver.front();
202 packets_to_deliver.pop(); 169 packets_to_deliver.pop();
203 packet_receiver_->DeliverPacket(MediaType::ANY, packet->data(), 170 packet_receiver_->DeliverPacket(MediaType::ANY, packet->data(),
204 packet->data_length(), PacketTime()); 171 packet->data_length(), PacketTime());
205 delete packet; 172 delete packet;
206 } 173 }
207 } 174 }
208 175
209 int64_t FakeNetworkPipe::TimeUntilNextProcess() const { 176 int64_t FakeNetworkPipe::TimeUntilNextProcess() const {
210 rtc::CritScope crit(&lock_); 177 rtc::CritScope crit(&lock_);
211 const int64_t kDefaultProcessIntervalMs = 30; 178 const int64_t kDefaultProcessIntervalMs = 30;
212 if (capacity_link_.size() == 0 || delay_link_.size() == 0) 179 if (capacity_link_.size() == 0 || delay_link_.size() == 0)
213 return kDefaultProcessIntervalMs; 180 return kDefaultProcessIntervalMs;
214 return std::max<int64_t>(next_process_time_ - clock_->TimeInMilliseconds(), 181 return std::max<int64_t>(next_process_time_ - clock_->TimeInMilliseconds(),
215 0); 182 0);
216 } 183 }
217 184
218 } // namespace webrtc 185 } // namespace webrtc
OLDNEW
« webrtc/test/fake_network_pipe.h ('K') | « webrtc/test/fake_network_pipe.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698