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 |
17 #include <algorithm> | 17 #include <algorithm> |
18 #include <cmath> | 18 #include <cmath> |
19 | 19 |
20 #include "webrtc/base/logging.h" | 20 #include "webrtc/base/logging.h" |
21 #include "webrtc/call/call.h" | 21 #include "webrtc/call/call.h" |
| 22 #include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h" |
22 #include "webrtc/system_wrappers/include/clock.h" | 23 #include "webrtc/system_wrappers/include/clock.h" |
23 | 24 |
24 namespace webrtc { | 25 namespace webrtc { |
25 | 26 |
26 namespace { | 27 namespace { |
27 constexpr int64_t kDefaultProcessIntervalMs = 5; | 28 constexpr int64_t kDefaultProcessIntervalMs = 5; |
28 } | 29 } |
29 | 30 |
| 31 DemuxerImpl::DemuxerImpl(const std::map<uint8_t, MediaType>& payload_type_map) |
| 32 : packet_receiver_(nullptr), payload_type_map_(payload_type_map) {} |
| 33 |
| 34 void DemuxerImpl::SetReceiver(PacketReceiver* receiver) { |
| 35 packet_receiver_ = receiver; |
| 36 } |
| 37 |
| 38 void DemuxerImpl::DeliverPacket(const NetworkPacket* packet, |
| 39 const PacketTime& packet_time) { |
| 40 // No packet receiver means that this demuxer will terminate the flow of |
| 41 // packets. |
| 42 if (!packet_receiver_) |
| 43 return; |
| 44 const uint8_t* const packet_data = packet->data(); |
| 45 const size_t packet_length = packet->data_length(); |
| 46 MediaType media_type = MediaType::ANY; |
| 47 if (!RtpHeaderParser::IsRtcp(packet_data, packet_length)) { |
| 48 RTC_CHECK_GE(packet_length, 2); |
| 49 const uint8_t payload_type = packet_data[1] & 0x7f; |
| 50 std::map<uint8_t, MediaType>::const_iterator it = |
| 51 payload_type_map_.find(payload_type); |
| 52 RTC_CHECK(it != payload_type_map_.end()) |
| 53 << "payload type " << static_cast<int>(payload_type) << " unknown."; |
| 54 media_type = it->second; |
| 55 } |
| 56 packet_receiver_->DeliverPacket(media_type, packet_data, packet_length, |
| 57 packet_time); |
| 58 } |
| 59 |
30 FakeNetworkPipe::FakeNetworkPipe(Clock* clock, | 60 FakeNetworkPipe::FakeNetworkPipe(Clock* clock, |
31 const FakeNetworkPipe::Config& config, | 61 const FakeNetworkPipe::Config& config, |
32 MediaType media_type) | 62 std::unique_ptr<Demuxer> demuxer) |
33 : FakeNetworkPipe(clock, config, media_type, 1) {} | 63 : FakeNetworkPipe(clock, config, std::move(demuxer), 1) {} |
34 | 64 |
35 FakeNetworkPipe::FakeNetworkPipe(Clock* clock, | 65 FakeNetworkPipe::FakeNetworkPipe(Clock* clock, |
36 const FakeNetworkPipe::Config& config, | 66 const FakeNetworkPipe::Config& config, |
37 MediaType media_type, | 67 std::unique_ptr<Demuxer> demuxer, |
38 uint64_t seed) | 68 uint64_t seed) |
39 : clock_(clock), | 69 : clock_(clock), |
40 media_type_(media_type), | 70 demuxer_(std::move(demuxer)), |
41 packet_receiver_(NULL), | |
42 random_(seed), | 71 random_(seed), |
43 config_(), | 72 config_(), |
44 dropped_packets_(0), | 73 dropped_packets_(0), |
45 sent_packets_(0), | 74 sent_packets_(0), |
46 total_packet_delay_(0), | 75 total_packet_delay_(0), |
47 bursting_(false), | 76 bursting_(false), |
48 next_process_time_(clock_->TimeInMilliseconds()), | 77 next_process_time_(clock_->TimeInMilliseconds()), |
49 last_log_time_(clock_->TimeInMilliseconds()) { | 78 last_log_time_(clock_->TimeInMilliseconds()) { |
50 SetConfig(config); | 79 SetConfig(config); |
51 } | 80 } |
52 | 81 |
53 FakeNetworkPipe::~FakeNetworkPipe() { | 82 FakeNetworkPipe::~FakeNetworkPipe() { |
54 while (!capacity_link_.empty()) { | 83 while (!capacity_link_.empty()) { |
55 delete capacity_link_.front(); | 84 delete capacity_link_.front(); |
56 capacity_link_.pop(); | 85 capacity_link_.pop(); |
57 } | 86 } |
58 while (!delay_link_.empty()) { | 87 while (!delay_link_.empty()) { |
59 delete *delay_link_.begin(); | 88 delete *delay_link_.begin(); |
60 delay_link_.erase(delay_link_.begin()); | 89 delay_link_.erase(delay_link_.begin()); |
61 } | 90 } |
62 } | 91 } |
63 | 92 |
64 void FakeNetworkPipe::SetReceiver(PacketReceiver* receiver) { | 93 void FakeNetworkPipe::SetReceiver(PacketReceiver* receiver) { |
65 packet_receiver_ = receiver; | 94 RTC_CHECK(demuxer_); |
| 95 demuxer_->SetReceiver(receiver); |
66 } | 96 } |
67 | 97 |
68 void FakeNetworkPipe::SetConfig(const FakeNetworkPipe::Config& config) { | 98 void FakeNetworkPipe::SetConfig(const FakeNetworkPipe::Config& config) { |
69 rtc::CritScope crit(&lock_); | 99 rtc::CritScope crit(&lock_); |
70 config_ = config; // Shallow copy of the struct. | 100 config_ = config; // Shallow copy of the struct. |
71 double prob_loss = config.loss_percent / 100.0; | 101 double prob_loss = config.loss_percent / 100.0; |
72 if (config_.avg_burst_loss_length == -1) { | 102 if (config_.avg_burst_loss_length == -1) { |
73 // Uniform loss | 103 // Uniform loss |
74 prob_loss_bursting_ = prob_loss; | 104 prob_loss_bursting_ = prob_loss; |
75 prob_start_bursting_ = prob_loss; | 105 prob_start_bursting_ = prob_loss; |
76 } else { | 106 } else { |
77 // Lose packets according to a gilbert-elliot model. | 107 // Lose packets according to a gilbert-elliot model. |
78 int avg_burst_loss_length = config.avg_burst_loss_length; | 108 int avg_burst_loss_length = config.avg_burst_loss_length; |
79 int min_avg_burst_loss_length = std::ceil(prob_loss / (1 - prob_loss)); | 109 int min_avg_burst_loss_length = std::ceil(prob_loss / (1 - prob_loss)); |
80 | 110 |
81 RTC_CHECK_GT(avg_burst_loss_length, min_avg_burst_loss_length) | 111 RTC_CHECK_GT(avg_burst_loss_length, min_avg_burst_loss_length) |
82 << "For a total packet loss of " << config.loss_percent << "%% then" | 112 << "For a total packet loss of " << config.loss_percent << "%% then" |
83 << " avg_burst_loss_length must be " << min_avg_burst_loss_length + 1 | 113 << " avg_burst_loss_length must be " << min_avg_burst_loss_length + 1 |
84 << " or higher."; | 114 << " or higher."; |
85 | 115 |
86 prob_loss_bursting_ = (1.0 - 1.0 / avg_burst_loss_length); | 116 prob_loss_bursting_ = (1.0 - 1.0 / avg_burst_loss_length); |
87 prob_start_bursting_ = prob_loss / (1 - prob_loss) / avg_burst_loss_length; | 117 prob_start_bursting_ = prob_loss / (1 - prob_loss) / avg_burst_loss_length; |
88 } | 118 } |
89 } | 119 } |
90 | 120 |
91 void FakeNetworkPipe::SendPacket(const uint8_t* data, size_t data_length) { | 121 void FakeNetworkPipe::SendPacket(const uint8_t* data, size_t data_length) { |
92 // A NULL packet_receiver_ means that this pipe will terminate the flow of | 122 RTC_CHECK(demuxer_); |
93 // packets. | |
94 if (packet_receiver_ == NULL) | |
95 return; | |
96 rtc::CritScope crit(&lock_); | 123 rtc::CritScope crit(&lock_); |
97 if (config_.queue_length_packets > 0 && | 124 if (config_.queue_length_packets > 0 && |
98 capacity_link_.size() >= config_.queue_length_packets) { | 125 capacity_link_.size() >= config_.queue_length_packets) { |
99 // Too many packet on the link, drop this one. | 126 // Too many packet on the link, drop this one. |
100 ++dropped_packets_; | 127 ++dropped_packets_; |
101 return; | 128 return; |
102 } | 129 } |
103 | 130 |
104 int64_t time_now = clock_->TimeInMilliseconds(); | 131 int64_t time_now = clock_->TimeInMilliseconds(); |
105 | 132 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 // |time_now| might be later than when the packet should have arrived, due | 222 // |time_now| might be later than when the packet should have arrived, due |
196 // to NetworkProcess being called too late. For stats, use the time it | 223 // to NetworkProcess being called too late. For stats, use the time it |
197 // should have been on the link. | 224 // should have been on the link. |
198 total_packet_delay_ += packet->arrival_time() - packet->send_time(); | 225 total_packet_delay_ += packet->arrival_time() - packet->send_time(); |
199 } | 226 } |
200 sent_packets_ += packets_to_deliver.size(); | 227 sent_packets_ += packets_to_deliver.size(); |
201 } | 228 } |
202 while (!packets_to_deliver.empty()) { | 229 while (!packets_to_deliver.empty()) { |
203 NetworkPacket* packet = packets_to_deliver.front(); | 230 NetworkPacket* packet = packets_to_deliver.front(); |
204 packets_to_deliver.pop(); | 231 packets_to_deliver.pop(); |
205 packet_receiver_->DeliverPacket(media_type_, packet->data(), | 232 demuxer_->DeliverPacket(packet, PacketTime()); |
206 packet->data_length(), PacketTime()); | |
207 delete packet; | 233 delete packet; |
208 } | 234 } |
209 | 235 |
210 next_process_time_ = !delay_link_.empty() | 236 next_process_time_ = !delay_link_.empty() |
211 ? (*delay_link_.begin())->arrival_time() | 237 ? (*delay_link_.begin())->arrival_time() |
212 : time_now + kDefaultProcessIntervalMs; | 238 : time_now + kDefaultProcessIntervalMs; |
213 } | 239 } |
214 | 240 |
215 int64_t FakeNetworkPipe::TimeUntilNextProcess() const { | 241 int64_t FakeNetworkPipe::TimeUntilNextProcess() const { |
216 rtc::CritScope crit(&lock_); | 242 rtc::CritScope crit(&lock_); |
217 return std::max<int64_t>(next_process_time_ - clock_->TimeInMilliseconds(), | 243 return std::max<int64_t>(next_process_time_ - clock_->TimeInMilliseconds(), |
218 0); | 244 0); |
219 } | 245 } |
220 | 246 |
221 } // namespace webrtc | 247 } // namespace webrtc |
OLD | NEW |