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...) Expand all 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 SetConfig(config); |
| 44 } |
| 45 |
| 46 FakeNetworkPipe::~FakeNetworkPipe() { |
| 47 while (!capacity_link_.empty()) { |
| 48 delete capacity_link_.front(); |
| 49 capacity_link_.pop(); |
| 50 } |
| 51 while (!delay_link_.empty()) { |
| 52 delete *delay_link_.begin(); |
| 53 delay_link_.erase(delay_link_.begin()); |
| 54 } |
| 55 } |
| 56 |
| 57 void FakeNetworkPipe::SetReceiver(PacketReceiver* receiver) { |
| 58 packet_receiver_ = receiver; |
| 59 } |
| 60 |
| 61 void FakeNetworkPipe::SetConfig(const FakeNetworkPipe::Config& config) { |
| 62 rtc::CritScope crit(&lock_); |
| 63 config_ = config; // Shallow copy of the struct. |
43 double prob_loss = config.loss_percent / 100.0; | 64 double prob_loss = config.loss_percent / 100.0; |
44 if (config_.avg_burst_loss_length == -1) { | 65 if (config_.avg_burst_loss_length == -1) { |
45 // Uniform loss | 66 // Uniform loss |
46 prob_loss_bursting_ = prob_loss; | 67 prob_loss_bursting_ = prob_loss; |
47 prob_start_bursting_ = prob_loss; | 68 prob_start_bursting_ = prob_loss; |
48 } else { | 69 } else { |
49 // Lose packets according to a gilbert-elliot model. | 70 // Lose packets according to a gilbert-elliot model. |
50 int avg_burst_loss_length = config.avg_burst_loss_length; | 71 int avg_burst_loss_length = config.avg_burst_loss_length; |
51 int min_avg_burst_loss_length = std::ceil(prob_loss / (1 - prob_loss)); | 72 int min_avg_burst_loss_length = std::ceil(prob_loss / (1 - prob_loss)); |
52 | 73 |
53 RTC_CHECK_GT(avg_burst_loss_length, min_avg_burst_loss_length) | 74 RTC_CHECK_GT(avg_burst_loss_length, min_avg_burst_loss_length) |
54 << "For a total packet loss of " << config.loss_percent << "%% then" | 75 << "For a total packet loss of " << config.loss_percent << "%% then" |
55 << " avg_burst_loss_length must be " << min_avg_burst_loss_length + 1 | 76 << " avg_burst_loss_length must be " << min_avg_burst_loss_length + 1 |
56 << " or higher."; | 77 << " or higher."; |
57 | 78 |
58 prob_loss_bursting_ = (1.0 - 1.0 / avg_burst_loss_length); | 79 prob_loss_bursting_ = (1.0 - 1.0 / avg_burst_loss_length); |
59 prob_start_bursting_ = prob_loss / (1 - prob_loss) / avg_burst_loss_length; | 80 prob_start_bursting_ = prob_loss / (1 - prob_loss) / avg_burst_loss_length; |
60 } | 81 } |
61 } | 82 } |
62 | 83 |
63 FakeNetworkPipe::~FakeNetworkPipe() { | |
64 while (!capacity_link_.empty()) { | |
65 delete capacity_link_.front(); | |
66 capacity_link_.pop(); | |
67 } | |
68 while (!delay_link_.empty()) { | |
69 delete *delay_link_.begin(); | |
70 delay_link_.erase(delay_link_.begin()); | |
71 } | |
72 } | |
73 | |
74 void FakeNetworkPipe::SetReceiver(PacketReceiver* receiver) { | |
75 packet_receiver_ = receiver; | |
76 } | |
77 | |
78 void FakeNetworkPipe::SetConfig(const FakeNetworkPipe::Config& config) { | |
79 rtc::CritScope crit(&lock_); | |
80 config_ = config; // Shallow copy of the struct. | |
81 } | |
82 | |
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) { |
91 // Too many packet on the link, drop this one. | 92 // Too many packet on the link, drop this one. |
92 ++dropped_packets_; | 93 ++dropped_packets_; |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 |