OLD | NEW |
---|---|
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 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
210 // well on a 28.8K modem, which is the slowest connection on which the voice | 210 // well on a 28.8K modem, which is the slowest connection on which the voice |
211 // quality is reasonable at all. | 211 // quality is reasonable at all. |
212 static const int PING_PACKET_SIZE = 60 * 8; | 212 static const int PING_PACKET_SIZE = 60 * 8; |
213 // STRONG_PING_INTERVAL (480ms) is applied when the best connection is both | 213 // STRONG_PING_INTERVAL (480ms) is applied when the best connection is both |
214 // writable and receiving. | 214 // writable and receiving. |
215 static const int STRONG_PING_INTERVAL = 1000 * PING_PACKET_SIZE / 1000; | 215 static const int STRONG_PING_INTERVAL = 1000 * PING_PACKET_SIZE / 1000; |
216 // WEAK_PING_INTERVAL (48ms) is applied when the best connection is either not | 216 // WEAK_PING_INTERVAL (48ms) is applied when the best connection is either not |
217 // writable or not receiving. | 217 // writable or not receiving. |
218 const int WEAK_PING_INTERVAL = 1000 * PING_PACKET_SIZE / 10000; | 218 const int WEAK_PING_INTERVAL = 1000 * PING_PACKET_SIZE / 10000; |
219 | 219 |
220 // If the current best connection is both writable and receiving, then we will | 220 // Writable connections are pinged at a faster rate while stabilizing. |
221 // also try hard to make sure it is pinged at this rate (a little less than | 221 static const int STABLIZING_WRITABLE_CONNECTION_PING_INTERVAL = 900; // ms |
222 // 2 * STRONG_PING_INTERVAL). | 222 |
223 static const int MAX_CURRENT_STRONG_INTERVAL = 900; // ms | 223 // Writable connections are pinged at a slower rate once stablized. |
224 static const int STABLE_WRITABLE_CONNECTION_PING_INTERVAL = 2500; // ms | |
224 | 225 |
225 static const int MIN_CHECK_RECEIVING_INTERVAL = 50; // ms | 226 static const int MIN_CHECK_RECEIVING_INTERVAL = 50; // ms |
226 | 227 |
227 P2PTransportChannel::P2PTransportChannel(const std::string& transport_name, | 228 P2PTransportChannel::P2PTransportChannel(const std::string& transport_name, |
228 int component, | 229 int component, |
229 P2PTransport* transport, | 230 P2PTransport* transport, |
230 PortAllocator* allocator) | 231 PortAllocator* allocator) |
231 : P2PTransportChannel(transport_name, component, allocator) {} | 232 : P2PTransportChannel(transport_name, component, allocator) {} |
232 | 233 |
233 P2PTransportChannel::P2PTransportChannel(const std::string& transport_name, | 234 P2PTransportChannel::P2PTransportChannel(const std::string& transport_name, |
234 int component, | 235 int component, |
235 PortAllocator* allocator) | 236 PortAllocator* allocator) |
236 : TransportChannelImpl(transport_name, component), | 237 : TransportChannelImpl(transport_name, component), |
237 allocator_(allocator), | 238 allocator_(allocator), |
238 worker_thread_(rtc::Thread::Current()), | 239 worker_thread_(rtc::Thread::Current()), |
239 incoming_only_(false), | 240 incoming_only_(false), |
240 error_(0), | 241 error_(0), |
241 best_connection_(NULL), | 242 best_connection_(NULL), |
242 pending_best_connection_(NULL), | 243 pending_best_connection_(NULL), |
243 sort_dirty_(false), | 244 sort_dirty_(false), |
244 remote_ice_mode_(ICEMODE_FULL), | 245 remote_ice_mode_(ICEMODE_FULL), |
245 ice_role_(ICEROLE_UNKNOWN), | 246 ice_role_(ICEROLE_UNKNOWN), |
246 tiebreaker_(0), | 247 tiebreaker_(0), |
247 gathering_state_(kIceGatheringNew), | 248 gathering_state_(kIceGatheringNew), |
248 check_receiving_interval_(MIN_CHECK_RECEIVING_INTERVAL * 5), | 249 check_receiving_interval_(MIN_CHECK_RECEIVING_INTERVAL * 5), |
249 config_(MIN_CHECK_RECEIVING_INTERVAL * 50 /* receiving_timeout */, | 250 config_(MIN_CHECK_RECEIVING_INTERVAL * 50 /* receiving_timeout */, |
250 0 /* backup_connection_ping_interval */, | 251 0 /* backup_connection_ping_interval */, |
251 false /* gather_continually */, | 252 false /* gather_continually */, |
252 false /* prioritize_most_likely_candidate_pairs */, | 253 false /* prioritize_most_likely_candidate_pairs */, |
253 MAX_CURRENT_STRONG_INTERVAL /* max_strong_interval */) { | 254 STABLE_WRITABLE_CONNECTION_PING_INTERVAL) { |
254 uint32_t weak_ping_interval = ::strtoul( | 255 uint32_t weak_ping_interval = ::strtoul( |
255 webrtc::field_trial::FindFullName("WebRTC-StunInterPacketDelay").c_str(), | 256 webrtc::field_trial::FindFullName("WebRTC-StunInterPacketDelay").c_str(), |
256 nullptr, 10); | 257 nullptr, 10); |
257 if (weak_ping_interval) { | 258 if (weak_ping_interval) { |
258 weak_ping_interval_ = static_cast<int>(weak_ping_interval); | 259 weak_ping_interval_ = static_cast<int>(weak_ping_interval); |
259 } | 260 } |
260 } | 261 } |
261 | 262 |
262 P2PTransportChannel::~P2PTransportChannel() { | 263 P2PTransportChannel::~P2PTransportChannel() { |
263 ASSERT(worker_thread_ == rtc::Thread::Current()); | 264 ASSERT(worker_thread_ == rtc::Thread::Current()); |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
425 } | 426 } |
426 LOG(LS_INFO) << "Set ICE receiving timeout to " << config_.receiving_timeout | 427 LOG(LS_INFO) << "Set ICE receiving timeout to " << config_.receiving_timeout |
427 << " milliseconds"; | 428 << " milliseconds"; |
428 } | 429 } |
429 | 430 |
430 config_.prioritize_most_likely_candidate_pairs = | 431 config_.prioritize_most_likely_candidate_pairs = |
431 config.prioritize_most_likely_candidate_pairs; | 432 config.prioritize_most_likely_candidate_pairs; |
432 LOG(LS_INFO) << "Set ping most likely connection to " | 433 LOG(LS_INFO) << "Set ping most likely connection to " |
433 << config_.prioritize_most_likely_candidate_pairs; | 434 << config_.prioritize_most_likely_candidate_pairs; |
434 | 435 |
435 if (config.max_strong_interval >= 0 && | 436 if (config.stable_writable_connection_ping_interval >= 0 && |
436 config_.max_strong_interval != config.max_strong_interval) { | 437 config_.stable_writable_connection_ping_interval != |
437 config_.max_strong_interval = config.max_strong_interval; | 438 config.stable_writable_connection_ping_interval) { |
438 LOG(LS_INFO) << "Set max strong interval to " | 439 config_.stable_writable_connection_ping_interval = |
439 << config_.max_strong_interval; | 440 config.stable_writable_connection_ping_interval; |
441 LOG(LS_INFO) << "Set stable_writable_connection_ping_interval to " | |
442 << config_.stable_writable_connection_ping_interval; | |
440 } | 443 } |
441 } | 444 } |
442 | 445 |
443 const IceConfig& P2PTransportChannel::config() const { | 446 const IceConfig& P2PTransportChannel::config() const { |
444 return config_; | 447 return config_; |
445 } | 448 } |
446 | 449 |
447 // Go into the state of processing candidates, and running in general | 450 // Go into the state of processing candidates, and running in general |
448 void P2PTransportChannel::Connect() { | 451 void P2PTransportChannel::Connect() { |
449 ASSERT(worker_thread_ == rtc::Thread::Current()); | 452 ASSERT(worker_thread_ == rtc::Thread::Current()); |
(...skipping 924 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1374 if (weak()) { | 1377 if (weak()) { |
1375 return true; | 1378 return true; |
1376 } | 1379 } |
1377 | 1380 |
1378 // Always ping active connections regardless whether the channel is completed | 1381 // Always ping active connections regardless whether the channel is completed |
1379 // or not, but backup connections are pinged at a slower rate. | 1382 // or not, but backup connections are pinged at a slower rate. |
1380 if (IsBackupConnection(conn)) { | 1383 if (IsBackupConnection(conn)) { |
1381 return (now >= conn->last_ping_response_received() + | 1384 return (now >= conn->last_ping_response_received() + |
1382 config_.backup_connection_ping_interval); | 1385 config_.backup_connection_ping_interval); |
1383 } | 1386 } |
1384 return conn->active(); | 1387 |
1388 // Don't ping inactive non-backup connections. | |
1389 if (!conn->active()) { | |
1390 return false; | |
1391 } | |
1392 | |
1393 // Do ping unwritable, active connections. | |
1394 if (!conn->writable()) { | |
1395 return true; | |
1396 } | |
1397 | |
1398 // Each connection ping sufficiently. | |
1399 if (conn->num_pings_sent() <= MIN_PINGS_AT_WEAK_PING_INTERVAL) { | |
1400 return true; | |
1401 } | |
1402 | |
1403 // Ping writable, active connections if it's been long enough since the last | |
1404 // ping. | |
1405 int ping_interval = config_.stable_writable_connection_ping_interval; | |
1406 // A stablized connection gets a slower ping rate. | |
1407 // A connection is stablized if and only if: | |
1408 // 1. Has sent enough initial pings for RTT to converge. | |
1409 // 2. Is receiving ping response. | |
1410 bool stable = conn->num_pings_sent() > MIN_PINGS_TO_STABILIZE && | |
1411 (conn->pings_since_last_response().size() == 0 || | |
1412 now <= conn->last_ping_response_received() + | |
1413 STABLIZING_WRITABLE_CONNECTION_PING_INTERVAL); | |
1414 if (!stable) { | |
1415 ping_interval = | |
1416 std::min(ping_interval, STABLIZING_WRITABLE_CONNECTION_PING_INTERVAL); | |
1417 } | |
1418 return (now >= conn->last_ping_sent() + ping_interval); | |
1419 } | |
1420 | |
1421 bool P2PTransportChannel::IsBestConnectionPingable() { | |
1422 if (!best_connection_ || !best_connection_->connected() || | |
1423 !best_connection_->writable()) { | |
1424 return false; | |
1425 } | |
1426 | |
1427 int interval = | |
1428 best_connection_->num_pings_sent() <= MIN_PINGS_TO_STABILIZE | |
1429 ? std::min(STABLE_WRITABLE_CONNECTION_PING_INTERVAL, | |
1430 config_.stable_writable_connection_ping_interval) | |
honghaiz3
2016/06/03 22:54:07
Should this be the same as the ping_interval calcu
Zhi Huang
2016/06/03 23:56:22
Yes, it should be.
| |
1431 : config_.stable_writable_connection_ping_interval; | |
1432 return best_connection_->last_ping_sent() + interval <= rtc::TimeMillis(); | |
honghaiz3
2016/06/03 22:54:07
You could pass in now from the parameter to avoid
Zhi Huang
2016/06/03 23:56:22
Done.
| |
1385 } | 1433 } |
1386 | 1434 |
1387 // Returns the next pingable connection to ping. This will be the oldest | 1435 // Returns the next pingable connection to ping. This will be the oldest |
1388 // pingable connection unless we have a connected, writable connection that is | 1436 // pingable connection unless we have a connected, writable connection that is |
1389 // past the maximum acceptable ping interval. When reconnecting a TCP | 1437 // past the writable ping interval. When reconnecting a TCP |
1390 // connection, the best connection is disconnected, although still WRITABLE | 1438 // connection, the best connection is disconnected, although still WRITABLE |
1391 // while reconnecting. The newly created connection should be selected as the | 1439 // while reconnecting. The newly created connection should be selected as the |
1392 // ping target to become writable instead. See the big comment in | 1440 // ping target to become writable instead. See the big comment in |
1393 // CompareConnections. | 1441 // CompareConnections. |
1394 Connection* P2PTransportChannel::FindNextPingableConnection() { | 1442 Connection* P2PTransportChannel::FindNextPingableConnection() { |
1395 int64_t now = rtc::TimeMillis(); | 1443 int64_t now = rtc::TimeMillis(); |
1396 Connection* conn_to_ping = nullptr; | 1444 Connection* conn_to_ping = nullptr; |
1397 if (best_connection_ && best_connection_->connected() && | 1445 if (IsBestConnectionPingable()) { |
1398 best_connection_->writable() && | |
1399 (best_connection_->last_ping_sent() + config_.max_strong_interval <= | |
1400 now)) { | |
1401 conn_to_ping = best_connection_; | 1446 conn_to_ping = best_connection_; |
1402 } else { | 1447 } else { |
1403 conn_to_ping = FindConnectionToPing(now); | 1448 conn_to_ping = FindConnectionToPing(now); |
1404 } | 1449 } |
1405 return conn_to_ping; | 1450 return conn_to_ping; |
1406 } | 1451 } |
1407 | 1452 |
1408 void P2PTransportChannel::MarkConnectionPinged(Connection* conn) { | 1453 void P2PTransportChannel::MarkConnectionPinged(Connection* conn) { |
1409 if (conn && pinged_connections_.insert(conn).second) { | 1454 if (conn && pinged_connections_.insert(conn).second) { |
1410 unpinged_connections_.erase(conn); | 1455 unpinged_connections_.erase(conn); |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1688 | 1733 |
1689 // During the initial state when nothing has been pinged yet, return the first | 1734 // During the initial state when nothing has been pinged yet, return the first |
1690 // one in the ordered |connections_|. | 1735 // one in the ordered |connections_|. |
1691 return *(std::find_if(connections_.begin(), connections_.end(), | 1736 return *(std::find_if(connections_.begin(), connections_.end(), |
1692 [conn1, conn2](Connection* conn) { | 1737 [conn1, conn2](Connection* conn) { |
1693 return conn == conn1 || conn == conn2; | 1738 return conn == conn1 || conn == conn2; |
1694 })); | 1739 })); |
1695 } | 1740 } |
1696 | 1741 |
1697 } // namespace cricket | 1742 } // namespace cricket |
OLD | NEW |