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 |
(...skipping 15 matching lines...) Loading... |
26 FakeNetworkPipe::FakeNetworkPipe(Clock* clock, | 26 FakeNetworkPipe::FakeNetworkPipe(Clock* clock, |
27 const FakeNetworkPipe::Config& config) | 27 const FakeNetworkPipe::Config& config) |
28 : FakeNetworkPipe(clock, config, 1) {} | 28 : FakeNetworkPipe(clock, config, 1) {} |
29 | 29 |
30 FakeNetworkPipe::FakeNetworkPipe(Clock* clock, | 30 FakeNetworkPipe::FakeNetworkPipe(Clock* clock, |
31 const FakeNetworkPipe::Config& config, | 31 const FakeNetworkPipe::Config& config, |
32 uint64_t seed) | 32 uint64_t seed) |
33 : clock_(clock), | 33 : clock_(clock), |
34 packet_receiver_(NULL), | 34 packet_receiver_(NULL), |
35 random_(seed), | 35 random_(seed), |
36 config_(config), | 36 config_(), |
37 dropped_packets_(0), | 37 dropped_packets_(0), |
38 sent_packets_(0), | 38 sent_packets_(0), |
39 total_packet_delay_(0), | 39 total_packet_delay_(0), |
40 bursting_(false), | 40 bursting_(false), |
41 next_process_time_(clock_->TimeInMilliseconds()), | 41 next_process_time_(clock_->TimeInMilliseconds()), |
42 last_log_time_(clock_->TimeInMilliseconds()) { | 42 last_log_time_(clock_->TimeInMilliseconds()) { |
43 double prob_loss = config.loss_percent / 100.0; | 43 SetConfig(config); |
44 if (config_.avg_burst_loss_length == -1) { | |
45 // Uniform loss | |
46 prob_loss_bursting_ = prob_loss; | |
47 prob_start_bursting_ = prob_loss; | |
48 } else { | |
49 // Lose packets according to a gilbert-elliot model. | |
50 int avg_burst_loss_length = config.avg_burst_loss_length; | |
51 int min_avg_burst_loss_length = std::ceil(prob_loss / (1 - prob_loss)); | |
52 | |
53 RTC_CHECK_GT(avg_burst_loss_length, min_avg_burst_loss_length) | |
54 << "For a total packet loss of " << config.loss_percent << "%% then" | |
55 << " avg_burst_loss_length must be " << min_avg_burst_loss_length + 1 | |
56 << " or higher."; | |
57 | |
58 prob_loss_bursting_ = (1.0 - 1.0 / avg_burst_loss_length); | |
59 prob_start_bursting_ = prob_loss / (1 - prob_loss) / avg_burst_loss_length; | |
60 } | |
61 } | 44 } |
62 | 45 |
63 FakeNetworkPipe::~FakeNetworkPipe() { | 46 FakeNetworkPipe::~FakeNetworkPipe() { |
64 while (!capacity_link_.empty()) { | 47 while (!capacity_link_.empty()) { |
65 delete capacity_link_.front(); | 48 delete capacity_link_.front(); |
66 capacity_link_.pop(); | 49 capacity_link_.pop(); |
67 } | 50 } |
68 while (!delay_link_.empty()) { | 51 while (!delay_link_.empty()) { |
69 delete *delay_link_.begin(); | 52 delete *delay_link_.begin(); |
70 delay_link_.erase(delay_link_.begin()); | 53 delay_link_.erase(delay_link_.begin()); |
71 } | 54 } |
72 } | 55 } |
73 | 56 |
74 void FakeNetworkPipe::SetReceiver(PacketReceiver* receiver) { | 57 void FakeNetworkPipe::SetReceiver(PacketReceiver* receiver) { |
75 packet_receiver_ = receiver; | 58 packet_receiver_ = receiver; |
76 } | 59 } |
77 | 60 |
78 void FakeNetworkPipe::SetConfig(const FakeNetworkPipe::Config& config) { | 61 void FakeNetworkPipe::SetConfig(const FakeNetworkPipe::Config& config) { |
79 rtc::CritScope crit(&lock_); | 62 rtc::CritScope crit(&lock_); |
80 config_ = config; // Shallow copy of the struct. | 63 config_ = config; // Shallow copy of the struct. |
| 64 double prob_loss = config.loss_percent / 100.0; |
| 65 if (config_.avg_burst_loss_length == -1) { |
| 66 // Uniform loss |
| 67 prob_loss_bursting_ = prob_loss; |
| 68 prob_start_bursting_ = prob_loss; |
| 69 } else { |
| 70 // Lose packets according to a gilbert-elliot model. |
| 71 int avg_burst_loss_length = config.avg_burst_loss_length; |
| 72 int min_avg_burst_loss_length = std::ceil(prob_loss / (1 - prob_loss)); |
| 73 |
| 74 RTC_CHECK_GT(avg_burst_loss_length, min_avg_burst_loss_length) |
| 75 << "For a total packet loss of " << config.loss_percent << "%% then" |
| 76 << " avg_burst_loss_length must be " << min_avg_burst_loss_length + 1 |
| 77 << " or higher."; |
| 78 |
| 79 prob_loss_bursting_ = (1.0 - 1.0 / avg_burst_loss_length); |
| 80 prob_start_bursting_ = prob_loss / (1 - prob_loss) / avg_burst_loss_length; |
| 81 } |
81 } | 82 } |
82 | 83 |
83 void FakeNetworkPipe::SendPacket(const uint8_t* data, size_t data_length) { | 84 void FakeNetworkPipe::SendPacket(const uint8_t* data, size_t data_length) { |
84 // A NULL packet_receiver_ means that this pipe will terminate the flow of | 85 // A NULL packet_receiver_ means that this pipe will terminate the flow of |
85 // packets. | 86 // packets. |
86 if (packet_receiver_ == NULL) | 87 if (packet_receiver_ == NULL) |
87 return; | 88 return; |
88 rtc::CritScope crit(&lock_); | 89 rtc::CritScope crit(&lock_); |
89 if (config_.queue_length_packets > 0 && | 90 if (config_.queue_length_packets > 0 && |
90 capacity_link_.size() >= config_.queue_length_packets) { | 91 capacity_link_.size() >= config_.queue_length_packets) { |
(...skipping 114 matching lines...) Loading... |
205 int64_t FakeNetworkPipe::TimeUntilNextProcess() const { | 206 int64_t FakeNetworkPipe::TimeUntilNextProcess() const { |
206 rtc::CritScope crit(&lock_); | 207 rtc::CritScope crit(&lock_); |
207 const int64_t kDefaultProcessIntervalMs = 5; | 208 const int64_t kDefaultProcessIntervalMs = 5; |
208 if (capacity_link_.empty() || delay_link_.empty()) | 209 if (capacity_link_.empty() || delay_link_.empty()) |
209 return kDefaultProcessIntervalMs; | 210 return kDefaultProcessIntervalMs; |
210 return std::max<int64_t>(next_process_time_ - clock_->TimeInMilliseconds(), | 211 return std::max<int64_t>(next_process_time_ - clock_->TimeInMilliseconds(), |
211 0); | 212 0); |
212 } | 213 } |
213 | 214 |
214 } // namespace webrtc | 215 } // namespace webrtc |
OLD | NEW |