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

Side by Side Diff: webrtc/p2p/base/port.cc

Issue 2722933002: Measure packet loss so we can use it to select ICE candidate pairs. (Closed)
Patch Set: better comments and test names Created 3 years, 9 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 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 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 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 // characteristics. Foundations are used in the frozen algorithm. 125 // characteristics. Foundations are used in the frozen algorithm.
126 static std::string ComputeFoundation(const std::string& type, 126 static std::string ComputeFoundation(const std::string& type,
127 const std::string& protocol, 127 const std::string& protocol,
128 const std::string& relay_protocol, 128 const std::string& relay_protocol,
129 const rtc::SocketAddress& base_address) { 129 const rtc::SocketAddress& base_address) {
130 std::ostringstream ost; 130 std::ostringstream ost;
131 ost << type << base_address.ipaddr().ToString() << protocol << relay_protocol; 131 ost << type << base_address.ipaddr().ToString() << protocol << relay_protocol;
132 return rtc::ToString<uint32_t>(rtc::ComputeCrc32(ost.str())); 132 return rtc::ToString<uint32_t>(rtc::ComputeCrc32(ost.str()));
133 } 133 }
134 134
135 PacketLossEstimator::PacketLossEstimator(int64_t expire_after)
136 : expire_after_(expire_after) {}
137
138 void PacketLossEstimator::ExpectResponse(std::string id, int64_t sent_time) {
139 outstanding_packets_.emplace_back(id, sent_time);
140 }
141
142 void PacketLossEstimator::ReceivedResponse(std::string id,
143 int64_t received_time) {
144 auto iter =
145 std::find_if(outstanding_packets_.begin(), outstanding_packets_.end(),
146 [id](const PacketInfo& packet) { return packet.id == id; });
147
148 // There are several reasons we might not find id:
149 // 1) We already expired this packet (and responses_expected_ was already
150 // incremented).
151 // 2) We already received (and counted) a response with this id.
152 // 3) we never expected to see this id (something else went wrong).
Taylor Brandstetter 2017/03/01 02:26:12 nit: Capitalization of "we"
Zach Stein 2017/03/02 00:02:23 Done.
153
154 if (iter != outstanding_packets_.end()) {
155 responses_expected_ += 1;
156 if (!IsExpired(*iter, received_time)) {
157 responses_received_ += 1;
158 }
159 outstanding_packets_.erase(iter);
160 }
161
162 RemoveExpiredPackets(received_time);
163 }
164
165 void PacketLossEstimator::RemoveExpiredPackets(int64_t now) {
166 auto iter = outstanding_packets_.begin();
167 while (iter != outstanding_packets_.end() && IsExpired(*iter, now)) {
168 responses_expected_ += 1;
169 ++iter;
170 }
171 outstanding_packets_.erase(outstanding_packets_.begin(), iter);
172
173 UpdateResponseRate();
174 }
175
176 void PacketLossEstimator::UpdateResponseRate() {
177 response_rate_ =
178 static_cast<double>(responses_received_) / responses_expected_;
Taylor Brandstetter 2017/03/01 02:26:12 Divide by zero?
Zach Stein 2017/03/02 00:02:23 Done.
179 }
180
181 bool PacketLossEstimator::IsExpired(const PacketInfo& packet,
182 int64_t received_time) const {
183 return packet.sent_time < received_time - expire_after_;
184 }
185
135 Port::Port(rtc::Thread* thread, 186 Port::Port(rtc::Thread* thread,
136 const std::string& type, 187 const std::string& type,
137 rtc::PacketSocketFactory* factory, 188 rtc::PacketSocketFactory* factory,
138 rtc::Network* network, 189 rtc::Network* network,
139 const rtc::IPAddress& ip, 190 const rtc::IPAddress& ip,
140 const std::string& username_fragment, 191 const std::string& username_fragment,
141 const std::string& password) 192 const std::string& password)
142 : thread_(thread), 193 : thread_(thread),
143 factory_(factory), 194 factory_(factory),
144 type_(type), 195 type_(type),
(...skipping 733 matching lines...) Expand 10 before | Expand all | Expand 10 after
878 connected_(true), 929 connected_(true),
879 pruned_(false), 930 pruned_(false),
880 use_candidate_attr_(false), 931 use_candidate_attr_(false),
881 remote_ice_mode_(ICEMODE_FULL), 932 remote_ice_mode_(ICEMODE_FULL),
882 requests_(port->thread()), 933 requests_(port->thread()),
883 rtt_(DEFAULT_RTT), 934 rtt_(DEFAULT_RTT),
884 last_ping_sent_(0), 935 last_ping_sent_(0),
885 last_ping_received_(0), 936 last_ping_received_(0),
886 last_data_received_(0), 937 last_data_received_(0),
887 last_ping_response_received_(0), 938 last_ping_response_received_(0),
939 packet_loss_estimator_(kPortTimeoutDelay),
Taylor Brandstetter 2017/03/01 02:26:12 This value recently became very large (~45 seconds
Zach Stein 2017/03/02 00:02:23 I like this idea. As is, we probably won't get a u
888 reported_(false), 940 reported_(false),
889 state_(IceCandidatePairState::WAITING), 941 state_(IceCandidatePairState::WAITING),
890 receiving_timeout_(WEAK_CONNECTION_RECEIVE_TIMEOUT), 942 receiving_timeout_(WEAK_CONNECTION_RECEIVE_TIMEOUT),
891 time_created_ms_(rtc::TimeMillis()) { 943 time_created_ms_(rtc::TimeMillis()) {
892 // All of our connections start in WAITING state. 944 // All of our connections start in WAITING state.
893 // TODO(mallinath) - Start connections from STATE_FROZEN. 945 // TODO(mallinath) - Start connections from STATE_FROZEN.
894 // Wire up to send stun packets 946 // Wire up to send stun packets
895 requests_.SignalSendPacket.connect(this, &Connection::OnSendStunPacket); 947 requests_.SignalSendPacket.connect(this, &Connection::OnSendStunPacket);
896 LOG_J(LS_INFO, this) << "Connection created"; 948 LOG_J(LS_INFO, this) << "Connection created";
897 } 949 }
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after
1230 UpdateReceiving(now); 1282 UpdateReceiving(now);
1231 if (dead(now)) { 1283 if (dead(now)) {
1232 Destroy(); 1284 Destroy();
1233 } 1285 }
1234 } 1286 }
1235 1287
1236 void Connection::Ping(int64_t now) { 1288 void Connection::Ping(int64_t now) {
1237 last_ping_sent_ = now; 1289 last_ping_sent_ = now;
1238 ConnectionRequest *req = new ConnectionRequest(this); 1290 ConnectionRequest *req = new ConnectionRequest(this);
1239 pings_since_last_response_.push_back(SentPing(req->id(), now, nomination_)); 1291 pings_since_last_response_.push_back(SentPing(req->id(), now, nomination_));
1292 packet_loss_estimator_.ExpectResponse(req->id(), now);
1240 LOG_J(LS_VERBOSE, this) << "Sending STUN ping " 1293 LOG_J(LS_VERBOSE, this) << "Sending STUN ping "
1241 << ", id=" << rtc::hex_encode(req->id()) 1294 << ", id=" << rtc::hex_encode(req->id())
1242 << ", nomination=" << nomination_; 1295 << ", nomination=" << nomination_;
1243 requests_.Send(req); 1296 requests_.Send(req);
1244 state_ = IceCandidatePairState::IN_PROGRESS; 1297 state_ = IceCandidatePairState::IN_PROGRESS;
1245 num_pings_sent_++; 1298 num_pings_sent_++;
1246 } 1299 }
1247 1300
1248 void Connection::ReceivedPing() { 1301 void Connection::ReceivedPing() {
1249 last_ping_received_ = rtc::TimeMillis(); 1302 last_ping_received_ = rtc::TimeMillis();
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
1384 std::string pings; 1437 std::string pings;
1385 PrintPingsSinceLastResponse(&pings, 5); 1438 PrintPingsSinceLastResponse(&pings, 5);
1386 LOG_JV(sev, this) << "Received STUN ping response" 1439 LOG_JV(sev, this) << "Received STUN ping response"
1387 << ", id=" << rtc::hex_encode(request->id()) 1440 << ", id=" << rtc::hex_encode(request->id())
1388 << ", code=0" // Makes logging easier to parse. 1441 << ", code=0" // Makes logging easier to parse.
1389 << ", rtt=" << rtt 1442 << ", rtt=" << rtt
1390 << ", pings_since_last_response=" << pings; 1443 << ", pings_since_last_response=" << pings;
1391 } 1444 }
1392 ReceivedPingResponse(rtt, request->id()); 1445 ReceivedPingResponse(rtt, request->id());
1393 1446
1447 int64_t time_received = rtc::TimeMillis();
1448 packet_loss_estimator_.ReceivedResponse(request->id(), time_received);
1449
1394 stats_.recv_ping_responses++; 1450 stats_.recv_ping_responses++;
1395 1451
1396 MaybeUpdateLocalCandidate(request, response); 1452 MaybeUpdateLocalCandidate(request, response);
1397 } 1453 }
1398 1454
1399 void Connection::OnConnectionRequestErrorResponse(ConnectionRequest* request, 1455 void Connection::OnConnectionRequestErrorResponse(ConnectionRequest* request,
1400 StunMessage* response) { 1456 StunMessage* response) {
1401 const StunErrorCodeAttribute* error_attr = response->GetErrorCode(); 1457 const StunErrorCodeAttribute* error_attr = response->GetErrorCode();
1402 int error_code = STUN_ERROR_GLOBAL_FAILURE; 1458 int error_code = STUN_ERROR_GLOBAL_FAILURE;
1403 if (error_attr) { 1459 if (error_attr) {
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
1612 RTC_DCHECK(sent < 0); 1668 RTC_DCHECK(sent < 0);
1613 error_ = port_->GetError(); 1669 error_ = port_->GetError();
1614 stats_.sent_discarded_packets++; 1670 stats_.sent_discarded_packets++;
1615 } else { 1671 } else {
1616 send_rate_tracker_.AddSamples(sent); 1672 send_rate_tracker_.AddSamples(sent);
1617 } 1673 }
1618 return sent; 1674 return sent;
1619 } 1675 }
1620 1676
1621 } // namespace cricket 1677 } // namespace cricket
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698