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 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 | 201 |
202 } // unnamed namespace | 202 } // unnamed namespace |
203 | 203 |
204 namespace cricket { | 204 namespace cricket { |
205 | 205 |
206 // When the socket is unwritable, we will use 10 Kbps (ignoring IP+UDP headers) | 206 // When the socket is unwritable, we will use 10 Kbps (ignoring IP+UDP headers) |
207 // for pinging. When the socket is writable, we will use only 1 Kbps because | 207 // for pinging. When the socket is writable, we will use only 1 Kbps because |
208 // we don't want to degrade the quality on a modem. These numbers should work | 208 // we don't want to degrade the quality on a modem. These numbers should work |
209 // well on a 28.8K modem, which is the slowest connection on which the voice | 209 // well on a 28.8K modem, which is the slowest connection on which the voice |
210 // quality is reasonable at all. | 210 // quality is reasonable at all. |
211 static const uint32_t PING_PACKET_SIZE = 60 * 8; | 211 static const int PING_PACKET_SIZE = 60 * 8; |
212 // TODO(honghaiz): Change the word DELAY to INTERVAL whenever appropriate. | 212 // STRONG_PING_INTERVAL (480ms) is applied when the best connection is both |
213 // STRONG_PING_DELAY (480ms) is applied when the best connection is both | |
214 // writable and receiving. | 213 // writable and receiving. |
215 static const uint32_t STRONG_PING_DELAY = 1000 * PING_PACKET_SIZE / 1000; | 214 static const int STRONG_PING_INTERVAL = 1000 * PING_PACKET_SIZE / 1000; |
216 // WEAK_PING_DELAY (48ms) is applied when the best connection is either not | 215 // WEAK_PING_INTERVAL (48ms) is applied when the best connection is either not |
217 // writable or not receiving. | 216 // writable or not receiving. |
218 const uint32_t WEAK_PING_DELAY = 1000 * PING_PACKET_SIZE / 10000; | 217 const int WEAK_PING_INTERVAL = 1000 * PING_PACKET_SIZE / 10000; |
219 | 218 |
220 // If the current best connection is both writable and receiving, then we will | 219 // If the current best connection is both writable and receiving, then we will |
221 // also try hard to make sure it is pinged at this rate (a little less than | 220 // also try hard to make sure it is pinged at this rate (a little less than |
222 // 2 * STRONG_PING_DELAY). | 221 // 2 * STRONG_PING_INTERVAL). |
223 static const uint32_t MAX_CURRENT_STRONG_DELAY = 900; | 222 static const int MAX_CURRENT_STRONG_INTERVAL = 900; // ms |
224 | 223 |
225 static const int MIN_CHECK_RECEIVING_DELAY = 50; // ms | 224 static const int MIN_CHECK_RECEIVING_INTERVAL = 50; // ms |
226 | 225 |
227 P2PTransportChannel::P2PTransportChannel(const std::string& transport_name, | 226 P2PTransportChannel::P2PTransportChannel(const std::string& transport_name, |
228 int component, | 227 int component, |
229 P2PTransport* transport, | 228 P2PTransport* transport, |
230 PortAllocator* allocator) | 229 PortAllocator* allocator) |
231 : P2PTransportChannel(transport_name, component, allocator) {} | 230 : P2PTransportChannel(transport_name, component, allocator) {} |
232 | 231 |
233 P2PTransportChannel::P2PTransportChannel(const std::string& transport_name, | 232 P2PTransportChannel::P2PTransportChannel(const std::string& transport_name, |
234 int component, | 233 int component, |
235 PortAllocator* allocator) | 234 PortAllocator* allocator) |
236 : TransportChannelImpl(transport_name, component), | 235 : TransportChannelImpl(transport_name, component), |
237 allocator_(allocator), | 236 allocator_(allocator), |
238 worker_thread_(rtc::Thread::Current()), | 237 worker_thread_(rtc::Thread::Current()), |
239 incoming_only_(false), | 238 incoming_only_(false), |
240 error_(0), | 239 error_(0), |
241 best_connection_(NULL), | 240 best_connection_(NULL), |
242 pending_best_connection_(NULL), | 241 pending_best_connection_(NULL), |
243 sort_dirty_(false), | 242 sort_dirty_(false), |
244 remote_ice_mode_(ICEMODE_FULL), | 243 remote_ice_mode_(ICEMODE_FULL), |
245 ice_role_(ICEROLE_UNKNOWN), | 244 ice_role_(ICEROLE_UNKNOWN), |
246 tiebreaker_(0), | 245 tiebreaker_(0), |
247 gathering_state_(kIceGatheringNew), | 246 gathering_state_(kIceGatheringNew), |
248 check_receiving_delay_(MIN_CHECK_RECEIVING_DELAY * 5), | 247 check_receiving_interval_(MIN_CHECK_RECEIVING_INTERVAL * 5), |
249 config_(MIN_CHECK_RECEIVING_DELAY * 50 /* receiving_timeout */, | 248 config_(MIN_CHECK_RECEIVING_INTERVAL * 50 /* receiving_timeout */, |
250 0 /* backup_connection_ping_interval */, | 249 0 /* backup_connection_ping_interval */, |
251 false /* gather_continually */, | 250 false /* gather_continually */, |
252 false /* prioritize_most_likely_candidate_pairs */, | 251 false /* prioritize_most_likely_candidate_pairs */, |
253 MAX_CURRENT_STRONG_DELAY /* most_strong_delay */) { | 252 MAX_CURRENT_STRONG_INTERVAL /* max_strong_interval */) { |
254 uint32_t weak_ping_delay = ::strtoul( | 253 uint32_t weak_ping_interval = ::strtoul( |
255 webrtc::field_trial::FindFullName("WebRTC-StunInterPacketDelay").c_str(), | 254 webrtc::field_trial::FindFullName("WebRTC-StunInterPacketDelay").c_str(), |
256 nullptr, 10); | 255 nullptr, 10); |
257 if (weak_ping_delay) { | 256 if (weak_ping_interval) { |
258 weak_ping_delay_ = weak_ping_delay; | 257 weak_ping_interval_ = static_cast<int>(weak_ping_interval); |
259 } | 258 } |
260 } | 259 } |
261 | 260 |
262 P2PTransportChannel::~P2PTransportChannel() { | 261 P2PTransportChannel::~P2PTransportChannel() { |
263 ASSERT(worker_thread_ == rtc::Thread::Current()); | 262 ASSERT(worker_thread_ == rtc::Thread::Current()); |
264 | 263 |
265 for (size_t i = 0; i < allocator_sessions_.size(); ++i) | 264 for (size_t i = 0; i < allocator_sessions_.size(); ++i) |
266 delete allocator_sessions_[i]; | 265 delete allocator_sessions_[i]; |
267 } | 266 } |
268 | 267 |
(...skipping 15 matching lines...) Expand all Loading... |
284 this, &P2PTransportChannel::OnCandidatesReady); | 283 this, &P2PTransportChannel::OnCandidatesReady); |
285 session->SignalCandidatesAllocationDone.connect( | 284 session->SignalCandidatesAllocationDone.connect( |
286 this, &P2PTransportChannel::OnCandidatesAllocationDone); | 285 this, &P2PTransportChannel::OnCandidatesAllocationDone); |
287 session->StartGettingPorts(); | 286 session->StartGettingPorts(); |
288 } | 287 } |
289 | 288 |
290 void P2PTransportChannel::AddConnection(Connection* connection) { | 289 void P2PTransportChannel::AddConnection(Connection* connection) { |
291 connections_.push_back(connection); | 290 connections_.push_back(connection); |
292 unpinged_connections_.insert(connection); | 291 unpinged_connections_.insert(connection); |
293 connection->set_remote_ice_mode(remote_ice_mode_); | 292 connection->set_remote_ice_mode(remote_ice_mode_); |
294 connection->set_receiving_timeout(config_.receiving_timeout_ms); | 293 connection->set_receiving_timeout(config_.receiving_timeout); |
295 connection->SignalReadPacket.connect( | 294 connection->SignalReadPacket.connect( |
296 this, &P2PTransportChannel::OnReadPacket); | 295 this, &P2PTransportChannel::OnReadPacket); |
297 connection->SignalReadyToSend.connect( | 296 connection->SignalReadyToSend.connect( |
298 this, &P2PTransportChannel::OnReadyToSend); | 297 this, &P2PTransportChannel::OnReadyToSend); |
299 connection->SignalStateChange.connect( | 298 connection->SignalStateChange.connect( |
300 this, &P2PTransportChannel::OnConnectionStateChange); | 299 this, &P2PTransportChannel::OnConnectionStateChange); |
301 connection->SignalDestroyed.connect( | 300 connection->SignalDestroyed.connect( |
302 this, &P2PTransportChannel::OnConnectionDestroyed); | 301 this, &P2PTransportChannel::OnConnectionDestroyed); |
303 connection->SignalNominated.connect(this, &P2PTransportChannel::OnNominated); | 302 connection->SignalNominated.connect(this, &P2PTransportChannel::OnNominated); |
304 had_connection_ = true; | 303 had_connection_ = true; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 | 405 |
407 if (config.backup_connection_ping_interval >= 0 && | 406 if (config.backup_connection_ping_interval >= 0 && |
408 config_.backup_connection_ping_interval != | 407 config_.backup_connection_ping_interval != |
409 config.backup_connection_ping_interval) { | 408 config.backup_connection_ping_interval) { |
410 config_.backup_connection_ping_interval = | 409 config_.backup_connection_ping_interval = |
411 config.backup_connection_ping_interval; | 410 config.backup_connection_ping_interval; |
412 LOG(LS_INFO) << "Set backup connection ping interval to " | 411 LOG(LS_INFO) << "Set backup connection ping interval to " |
413 << config_.backup_connection_ping_interval << " milliseconds."; | 412 << config_.backup_connection_ping_interval << " milliseconds."; |
414 } | 413 } |
415 | 414 |
416 if (config.receiving_timeout_ms >= 0 && | 415 if (config.receiving_timeout >= 0 && |
417 config_.receiving_timeout_ms != config.receiving_timeout_ms) { | 416 config_.receiving_timeout != config.receiving_timeout) { |
418 config_.receiving_timeout_ms = config.receiving_timeout_ms; | 417 config_.receiving_timeout = config.receiving_timeout; |
419 check_receiving_delay_ = | 418 check_receiving_interval_ = |
420 std::max(MIN_CHECK_RECEIVING_DELAY, config_.receiving_timeout_ms / 10); | 419 std::max(MIN_CHECK_RECEIVING_INTERVAL, config_.receiving_timeout / 10); |
421 | 420 |
422 for (Connection* connection : connections_) { | 421 for (Connection* connection : connections_) { |
423 connection->set_receiving_timeout(config_.receiving_timeout_ms); | 422 connection->set_receiving_timeout(config_.receiving_timeout); |
424 } | 423 } |
425 LOG(LS_INFO) << "Set ICE receiving timeout to " | 424 LOG(LS_INFO) << "Set ICE receiving timeout to " << config_.receiving_timeout |
426 << config_.receiving_timeout_ms << " milliseconds"; | 425 << " milliseconds"; |
427 } | 426 } |
428 | 427 |
429 config_.prioritize_most_likely_candidate_pairs = | 428 config_.prioritize_most_likely_candidate_pairs = |
430 config.prioritize_most_likely_candidate_pairs; | 429 config.prioritize_most_likely_candidate_pairs; |
431 LOG(LS_INFO) << "Set ping most likely connection to " | 430 LOG(LS_INFO) << "Set ping most likely connection to " |
432 << config_.prioritize_most_likely_candidate_pairs; | 431 << config_.prioritize_most_likely_candidate_pairs; |
433 | 432 |
434 if (config.max_strong_delay >= 0 && | 433 if (config.max_strong_interval >= 0 && |
435 config_.max_strong_delay != config.max_strong_delay) { | 434 config_.max_strong_interval != config.max_strong_interval) { |
436 config_.max_strong_delay = config.max_strong_delay; | 435 config_.max_strong_interval = config.max_strong_interval; |
437 LOG(LS_INFO) << "Set max strong delay to " << config_.max_strong_delay; | 436 LOG(LS_INFO) << "Set max strong interval to " |
| 437 << config_.max_strong_interval; |
438 } | 438 } |
439 } | 439 } |
440 | 440 |
441 const IceConfig& P2PTransportChannel::config() const { | 441 const IceConfig& P2PTransportChannel::config() const { |
442 return config_; | 442 return config_; |
443 } | 443 } |
444 | 444 |
445 // Go into the state of processing candidates, and running in general | 445 // Go into the state of processing candidates, and running in general |
446 void P2PTransportChannel::Connect() { | 446 void P2PTransportChannel::Connect() { |
447 ASSERT(worker_thread_ == rtc::Thread::Current()); | 447 ASSERT(worker_thread_ == rtc::Thread::Current()); |
(...skipping 790 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1238 // Resort the connections based on the new statistics. | 1238 // Resort the connections based on the new statistics. |
1239 SortConnections(); | 1239 SortConnections(); |
1240 } | 1240 } |
1241 | 1241 |
1242 // Handle queued up check-and-ping request | 1242 // Handle queued up check-and-ping request |
1243 void P2PTransportChannel::OnCheckAndPing() { | 1243 void P2PTransportChannel::OnCheckAndPing() { |
1244 // Make sure the states of the connections are up-to-date (since this affects | 1244 // Make sure the states of the connections are up-to-date (since this affects |
1245 // which ones are pingable). | 1245 // which ones are pingable). |
1246 UpdateConnectionStates(); | 1246 UpdateConnectionStates(); |
1247 // When the best connection is either not receiving or not writable, | 1247 // When the best connection is either not receiving or not writable, |
1248 // switch to weak ping delay. | 1248 // switch to weak ping interval. |
1249 int ping_delay = weak() ? weak_ping_delay_ : STRONG_PING_DELAY; | 1249 int ping_interval = weak() ? weak_ping_interval_ : STRONG_PING_INTERVAL; |
1250 if (rtc::Time() >= last_ping_sent_ms_ + ping_delay) { | 1250 if (rtc::Time() >= last_ping_sent_ms_ + ping_interval) { |
1251 Connection* conn = FindNextPingableConnection(); | 1251 Connection* conn = FindNextPingableConnection(); |
1252 if (conn) { | 1252 if (conn) { |
1253 PingConnection(conn); | 1253 PingConnection(conn); |
1254 MarkConnectionPinged(conn); | 1254 MarkConnectionPinged(conn); |
1255 } | 1255 } |
1256 } | 1256 } |
1257 int check_delay = std::min(ping_delay, check_receiving_delay_); | 1257 int delay = std::min(ping_interval, check_receiving_interval_); |
1258 thread()->PostDelayed(check_delay, this, MSG_CHECK_AND_PING); | 1258 thread()->PostDelayed(delay, this, MSG_CHECK_AND_PING); |
1259 } | 1259 } |
1260 | 1260 |
1261 // A connection is considered a backup connection if the channel state | 1261 // A connection is considered a backup connection if the channel state |
1262 // is completed, the connection is not the best connection and it is active. | 1262 // is completed, the connection is not the best connection and it is active. |
1263 bool P2PTransportChannel::IsBackupConnection(Connection* conn) const { | 1263 bool P2PTransportChannel::IsBackupConnection(Connection* conn) const { |
1264 return state_ == STATE_COMPLETED && conn != best_connection_ && | 1264 return state_ == STATE_COMPLETED && conn != best_connection_ && |
1265 conn->active(); | 1265 conn->active(); |
1266 } | 1266 } |
1267 | 1267 |
1268 // Is the connection in a state for us to even consider pinging the other side? | 1268 // Is the connection in a state for us to even consider pinging the other side? |
(...skipping 24 matching lines...) Expand all Loading... |
1293 // or not, but backup connections are pinged at a slower rate. | 1293 // or not, but backup connections are pinged at a slower rate. |
1294 if (IsBackupConnection(conn)) { | 1294 if (IsBackupConnection(conn)) { |
1295 return (now >= conn->last_ping_response_received() + | 1295 return (now >= conn->last_ping_response_received() + |
1296 config_.backup_connection_ping_interval); | 1296 config_.backup_connection_ping_interval); |
1297 } | 1297 } |
1298 return conn->active(); | 1298 return conn->active(); |
1299 } | 1299 } |
1300 | 1300 |
1301 // Returns the next pingable connection to ping. This will be the oldest | 1301 // Returns the next pingable connection to ping. This will be the oldest |
1302 // pingable connection unless we have a connected, writable connection that is | 1302 // pingable connection unless we have a connected, writable connection that is |
1303 // past the maximum acceptable ping delay. When reconnecting a TCP connection, | 1303 // past the maximum acceptable ping interval. When reconnecting a TCP |
1304 // the best connection is disconnected, although still WRITABLE while | 1304 // connection, the best connection is disconnected, although still WRITABLE |
1305 // reconnecting. The newly created connection should be selected as the ping | 1305 // while reconnecting. The newly created connection should be selected as the |
1306 // target to become writable instead. See the big comment in CompareConnections. | 1306 // ping target to become writable instead. See the big comment in |
| 1307 // CompareConnections. |
1307 Connection* P2PTransportChannel::FindNextPingableConnection() { | 1308 Connection* P2PTransportChannel::FindNextPingableConnection() { |
1308 uint32_t now = rtc::Time(); | 1309 uint32_t now = rtc::Time(); |
1309 Connection* conn_to_ping = nullptr; | 1310 Connection* conn_to_ping = nullptr; |
1310 if (best_connection_ && best_connection_->connected() && | 1311 if (best_connection_ && best_connection_->connected() && |
1311 best_connection_->writable() && | 1312 best_connection_->writable() && |
1312 (best_connection_->last_ping_sent() + config_.max_strong_delay <= now)) { | 1313 (best_connection_->last_ping_sent() + config_.max_strong_interval <= |
| 1314 now)) { |
1313 conn_to_ping = best_connection_; | 1315 conn_to_ping = best_connection_; |
1314 } else { | 1316 } else { |
1315 conn_to_ping = FindConnectionToPing(now); | 1317 conn_to_ping = FindConnectionToPing(now); |
1316 } | 1318 } |
1317 return conn_to_ping; | 1319 return conn_to_ping; |
1318 } | 1320 } |
1319 | 1321 |
1320 void P2PTransportChannel::MarkConnectionPinged(Connection* conn) { | 1322 void P2PTransportChannel::MarkConnectionPinged(Connection* conn) { |
1321 if (conn && pinged_connections_.insert(conn).second) { | 1323 if (conn && pinged_connections_.insert(conn).second) { |
1322 unpinged_connections_.erase(conn); | 1324 unpinged_connections_.erase(conn); |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1595 | 1597 |
1596 // During the initial state when nothing has been pinged yet, return the first | 1598 // During the initial state when nothing has been pinged yet, return the first |
1597 // one in the ordered |connections_|. | 1599 // one in the ordered |connections_|. |
1598 return *(std::find_if(connections_.begin(), connections_.end(), | 1600 return *(std::find_if(connections_.begin(), connections_.end(), |
1599 [conn1, conn2](Connection* conn) { | 1601 [conn1, conn2](Connection* conn) { |
1600 return conn == conn1 || conn == conn2; | 1602 return conn == conn1 || conn == conn2; |
1601 })); | 1603 })); |
1602 } | 1604 } |
1603 | 1605 |
1604 } // namespace cricket | 1606 } // namespace cricket |
OLD | NEW |