OLD | NEW |
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 |
11 #include "webrtc/test/fake_network_pipe.h" | 11 #include "webrtc/test/fake_network_pipe.h" |
12 | 12 |
13 #include <assert.h> | 13 #include <assert.h> |
14 #include <math.h> | 14 #include <math.h> |
15 #include <string.h> | 15 #include <string.h> |
| 16 |
16 #include <algorithm> | 17 #include <algorithm> |
| 18 #include <cmath> |
17 | 19 |
18 #include "webrtc/call.h" | 20 #include "webrtc/call.h" |
19 #include "webrtc/system_wrappers/include/clock.h" | 21 #include "webrtc/system_wrappers/include/clock.h" |
20 | 22 |
21 namespace webrtc { | 23 namespace webrtc { |
22 | 24 |
23 FakeNetworkPipe::FakeNetworkPipe(Clock* clock, | 25 FakeNetworkPipe::FakeNetworkPipe(Clock* clock, |
24 const FakeNetworkPipe::Config& config) | 26 const FakeNetworkPipe::Config& config) |
25 : FakeNetworkPipe(clock, config, 1) {} | 27 : FakeNetworkPipe(clock, config, 1) {} |
26 | 28 |
27 FakeNetworkPipe::FakeNetworkPipe(Clock* clock, | 29 FakeNetworkPipe::FakeNetworkPipe(Clock* clock, |
28 const FakeNetworkPipe::Config& config, | 30 const FakeNetworkPipe::Config& config, |
29 uint64_t seed) | 31 uint64_t seed) |
30 : clock_(clock), | 32 : clock_(clock), |
31 packet_receiver_(NULL), | 33 packet_receiver_(NULL), |
32 random_(seed), | 34 random_(seed), |
33 config_(config), | 35 config_(config), |
34 dropped_packets_(0), | 36 dropped_packets_(0), |
35 sent_packets_(0), | 37 sent_packets_(0), |
36 total_packet_delay_(0), | 38 total_packet_delay_(0), |
37 next_process_time_(clock_->TimeInMilliseconds()) {} | 39 bursting_(false), |
| 40 next_process_time_(clock_->TimeInMilliseconds()) { |
| 41 double prob_loss = config.loss_percent / 100.0; |
| 42 if (config_.avg_burst_loss_length == -1) { |
| 43 // Uniform loss |
| 44 prob_loss_bursting_ = prob_loss; |
| 45 prob_start_bursting_ = prob_loss; |
| 46 } else { |
| 47 // Lose packets according to a gilbert-elliot model. |
| 48 int avg_burst_loss_length = config.avg_burst_loss_length; |
| 49 int min_avg_burst_loss_length = std::ceil(prob_loss / (1 - prob_loss)); |
| 50 |
| 51 RTC_CHECK_GT(avg_burst_loss_length, min_avg_burst_loss_length) |
| 52 << "For a total packet loss of " << config.loss_percent << "%% then" |
| 53 << " avg_burst_loss_length must be " << min_avg_burst_loss_length + 1 |
| 54 << " or higher."; |
| 55 |
| 56 prob_loss_bursting_ = (1.0 - 1.0 / avg_burst_loss_length); |
| 57 prob_start_bursting_ = prob_loss / (1 - prob_loss) / avg_burst_loss_length; |
| 58 } |
| 59 } |
38 | 60 |
39 FakeNetworkPipe::~FakeNetworkPipe() { | 61 FakeNetworkPipe::~FakeNetworkPipe() { |
40 while (!capacity_link_.empty()) { | 62 while (!capacity_link_.empty()) { |
41 delete capacity_link_.front(); | 63 delete capacity_link_.front(); |
42 capacity_link_.pop(); | 64 capacity_link_.pop(); |
43 } | 65 } |
44 while (!delay_link_.empty()) { | 66 while (!delay_link_.empty()) { |
45 delete *delay_link_.begin(); | 67 delete *delay_link_.begin(); |
46 delay_link_.erase(delay_link_.begin()); | 68 delay_link_.erase(delay_link_.begin()); |
47 } | 69 } |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 std::queue<NetworkPacket*> packets_to_deliver; | 133 std::queue<NetworkPacket*> packets_to_deliver; |
112 { | 134 { |
113 rtc::CritScope crit(&lock_); | 135 rtc::CritScope crit(&lock_); |
114 // Check the capacity link first. | 136 // Check the capacity link first. |
115 while (!capacity_link_.empty() && | 137 while (!capacity_link_.empty() && |
116 time_now >= capacity_link_.front()->arrival_time()) { | 138 time_now >= capacity_link_.front()->arrival_time()) { |
117 // Time to get this packet. | 139 // Time to get this packet. |
118 NetworkPacket* packet = capacity_link_.front(); | 140 NetworkPacket* packet = capacity_link_.front(); |
119 capacity_link_.pop(); | 141 capacity_link_.pop(); |
120 | 142 |
121 // Packets are randomly dropped after being affected by the bottleneck. | 143 // Drop packets at an average rate of |config_.loss_percent| with |
122 if (random_.Rand(100) < static_cast<uint32_t>(config_.loss_percent)) { | 144 // and average loss burst length of |config_.avg_burst_loss_length|. |
| 145 if ((bursting_ && random_.Rand<double>() < prob_loss_bursting_) || |
| 146 (!bursting_ && random_.Rand<double>() < prob_start_bursting_)) { |
| 147 bursting_ = true; |
123 delete packet; | 148 delete packet; |
124 continue; | 149 continue; |
| 150 } else { |
| 151 bursting_ = false; |
125 } | 152 } |
126 | 153 |
127 int arrival_time_jitter = random_.Gaussian( | 154 int arrival_time_jitter = random_.Gaussian( |
128 config_.queue_delay_ms, config_.delay_standard_deviation_ms); | 155 config_.queue_delay_ms, config_.delay_standard_deviation_ms); |
129 | 156 |
130 // If reordering is not allowed then adjust arrival_time_jitter | 157 // If reordering is not allowed then adjust arrival_time_jitter |
131 // to make sure all packets are sent in order. | 158 // to make sure all packets are sent in order. |
132 if (!config_.allow_reordering && !delay_link_.empty() && | 159 if (!config_.allow_reordering && !delay_link_.empty() && |
133 packet->arrival_time() + arrival_time_jitter < | 160 packet->arrival_time() + arrival_time_jitter < |
134 (*delay_link_.rbegin())->arrival_time()) { | 161 (*delay_link_.rbegin())->arrival_time()) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 int64_t FakeNetworkPipe::TimeUntilNextProcess() const { | 194 int64_t FakeNetworkPipe::TimeUntilNextProcess() const { |
168 rtc::CritScope crit(&lock_); | 195 rtc::CritScope crit(&lock_); |
169 const int64_t kDefaultProcessIntervalMs = 30; | 196 const int64_t kDefaultProcessIntervalMs = 30; |
170 if (capacity_link_.empty() || delay_link_.empty()) | 197 if (capacity_link_.empty() || delay_link_.empty()) |
171 return kDefaultProcessIntervalMs; | 198 return kDefaultProcessIntervalMs; |
172 return std::max<int64_t>(next_process_time_ - clock_->TimeInMilliseconds(), | 199 return std::max<int64_t>(next_process_time_ - clock_->TimeInMilliseconds(), |
173 0); | 200 0); |
174 } | 201 } |
175 | 202 |
176 } // namespace webrtc | 203 } // namespace webrtc |
OLD | NEW |