| 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 |
| 11 #include "webrtc/p2p/base/p2ptransportchannel.h" | 11 #include "webrtc/p2p/base/p2ptransportchannel.h" |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 #include <set> | 14 #include <set> |
| 15 #include "webrtc/p2p/base/common.h" | 15 #include "webrtc/p2p/base/common.h" |
| 16 #include "webrtc/p2p/base/relayport.h" // For RELAY_PORT_TYPE. | 16 #include "webrtc/p2p/base/relayport.h" // For RELAY_PORT_TYPE. |
| 17 #include "webrtc/p2p/base/stunport.h" // For STUN_PORT_TYPE. | 17 #include "webrtc/p2p/base/stunport.h" // For STUN_PORT_TYPE. |
| 18 #include "webrtc/base/common.h" | 18 #include "webrtc/base/common.h" |
| 19 #include "webrtc/base/crc32.h" | 19 #include "webrtc/base/crc32.h" |
| 20 #include "webrtc/base/logging.h" | 20 #include "webrtc/base/logging.h" |
| 21 #include "webrtc/base/stringencode.h" | 21 #include "webrtc/base/stringencode.h" |
| 22 #include "webrtc/system_wrappers/interface/field_trial.h" | |
| 23 | 22 |
| 24 namespace { | 23 namespace { |
| 25 | 24 |
| 26 // messages for queuing up work for ourselves | 25 // messages for queuing up work for ourselves |
| 27 enum { MSG_SORT = 1, MSG_CHECK_AND_PING }; | 26 enum { MSG_SORT = 1, MSG_CHECK_AND_PING }; |
| 28 | 27 |
| 28 // When the socket is unwritable, we will use 10 Kbps (ignoring IP+UDP headers) |
| 29 // for pinging. When the socket is writable, we will use only 1 Kbps because |
| 30 // we don't want to degrade the quality on a modem. These numbers should work |
| 31 // well on a 28.8K modem, which is the slowest connection on which the voice |
| 32 // quality is reasonable at all. |
| 33 static const uint32_t PING_PACKET_SIZE = 60 * 8; |
| 34 // STRONG_PING_DELAY (480ms) is applied when the best connection is both |
| 35 // writable and receiving. |
| 36 static const uint32_t STRONG_PING_DELAY = 1000 * PING_PACKET_SIZE / 1000; |
| 37 // WEAK_PING_DELAY (48ms) is applied when the best connection is either not |
| 38 // writable or not receiving. |
| 39 static const uint32_t WEAK_PING_DELAY = 1000 * PING_PACKET_SIZE / 10000; |
| 40 |
| 41 // If the current best connection is both writable and receiving, then we will |
| 42 // also try hard to make sure it is pinged at this rate (a little less than |
| 43 // 2 * STRONG_PING_DELAY). |
| 44 static const uint32_t MAX_CURRENT_STRONG_DELAY = 900; |
| 45 |
| 46 static const int MIN_CHECK_RECEIVING_DELAY = 50; // ms |
| 47 |
| 29 // The minimum improvement in RTT that justifies a switch. | 48 // The minimum improvement in RTT that justifies a switch. |
| 30 static const double kMinImprovement = 10; | 49 static const double kMinImprovement = 10; |
| 31 | 50 |
| 32 cricket::PortInterface::CandidateOrigin GetOrigin(cricket::PortInterface* port, | 51 cricket::PortInterface::CandidateOrigin GetOrigin(cricket::PortInterface* port, |
| 33 cricket::PortInterface* origin_port) { | 52 cricket::PortInterface* origin_port) { |
| 34 if (!origin_port) | 53 if (!origin_port) |
| 35 return cricket::PortInterface::ORIGIN_MESSAGE; | 54 return cricket::PortInterface::ORIGIN_MESSAGE; |
| 36 else if (port == origin_port) | 55 else if (port == origin_port) |
| 37 return cricket::PortInterface::ORIGIN_THIS_PORT; | 56 return cricket::PortInterface::ORIGIN_THIS_PORT; |
| 38 else | 57 else |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 return prefs_cmp < 0; | 193 return prefs_cmp < 0; |
| 175 } | 194 } |
| 176 | 195 |
| 177 return b_conn->rtt() <= a_conn->rtt() + kMinImprovement; | 196 return b_conn->rtt() <= a_conn->rtt() + kMinImprovement; |
| 178 } | 197 } |
| 179 | 198 |
| 180 } // unnamed namespace | 199 } // unnamed namespace |
| 181 | 200 |
| 182 namespace cricket { | 201 namespace cricket { |
| 183 | 202 |
| 184 // When the socket is unwritable, we will use 10 Kbps (ignoring IP+UDP headers) | |
| 185 // for pinging. When the socket is writable, we will use only 1 Kbps because | |
| 186 // we don't want to degrade the quality on a modem. These numbers should work | |
| 187 // well on a 28.8K modem, which is the slowest connection on which the voice | |
| 188 // quality is reasonable at all. | |
| 189 static const uint32_t PING_PACKET_SIZE = 60 * 8; | |
| 190 // STRONG_PING_DELAY (480ms) is applied when the best connection is both | |
| 191 // writable and receiving. | |
| 192 static const uint32_t STRONG_PING_DELAY = 1000 * PING_PACKET_SIZE / 1000; | |
| 193 // WEAK_PING_DELAY (48ms) is applied when the best connection is either not | |
| 194 // writable or not receiving. | |
| 195 const uint32_t WEAK_PING_DELAY = 1000 * PING_PACKET_SIZE / 10000; | |
| 196 | |
| 197 // If the current best connection is both writable and receiving, then we will | |
| 198 // also try hard to make sure it is pinged at this rate (a little less than | |
| 199 // 2 * STRONG_PING_DELAY). | |
| 200 static const uint32_t MAX_CURRENT_STRONG_DELAY = 900; | |
| 201 | |
| 202 static const int MIN_CHECK_RECEIVING_DELAY = 50; // ms | |
| 203 | |
| 204 | |
| 205 P2PTransportChannel::P2PTransportChannel(const std::string& transport_name, | 203 P2PTransportChannel::P2PTransportChannel(const std::string& transport_name, |
| 206 int component, | 204 int component, |
| 207 P2PTransport* transport, | 205 P2PTransport* transport, |
| 208 PortAllocator* allocator) | 206 PortAllocator* allocator) |
| 209 : TransportChannelImpl(transport_name, component), | 207 : TransportChannelImpl(transport_name, component), |
| 210 transport_(transport), | 208 transport_(transport), |
| 211 allocator_(allocator), | 209 allocator_(allocator), |
| 212 worker_thread_(rtc::Thread::Current()), | 210 worker_thread_(rtc::Thread::Current()), |
| 213 incoming_only_(false), | 211 incoming_only_(false), |
| 214 error_(0), | 212 error_(0), |
| 215 best_connection_(NULL), | 213 best_connection_(NULL), |
| 216 pending_best_connection_(NULL), | 214 pending_best_connection_(NULL), |
| 217 sort_dirty_(false), | 215 sort_dirty_(false), |
| 218 was_writable_(false), | 216 was_writable_(false), |
| 219 remote_ice_mode_(ICEMODE_FULL), | 217 remote_ice_mode_(ICEMODE_FULL), |
| 220 ice_role_(ICEROLE_UNKNOWN), | 218 ice_role_(ICEROLE_UNKNOWN), |
| 221 tiebreaker_(0), | 219 tiebreaker_(0), |
| 222 remote_candidate_generation_(0), | 220 remote_candidate_generation_(0), |
| 223 gathering_state_(kIceGatheringNew), | 221 gathering_state_(kIceGatheringNew), |
| 224 check_receiving_delay_(MIN_CHECK_RECEIVING_DELAY * 5), | 222 check_receiving_delay_(MIN_CHECK_RECEIVING_DELAY * 5), |
| 225 receiving_timeout_(MIN_CHECK_RECEIVING_DELAY * 50) { | 223 receiving_timeout_(MIN_CHECK_RECEIVING_DELAY * 50) {} |
| 226 uint32_t weak_ping_delay = ::strtoul( | |
| 227 webrtc::field_trial::FindFullName("WebRTC-StunInterPacketDelay").c_str(), | |
| 228 nullptr, 10); | |
| 229 if (weak_ping_delay) { | |
| 230 weak_ping_delay_ = weak_ping_delay; | |
| 231 } | |
| 232 } | |
| 233 | 224 |
| 234 P2PTransportChannel::~P2PTransportChannel() { | 225 P2PTransportChannel::~P2PTransportChannel() { |
| 235 ASSERT(worker_thread_ == rtc::Thread::Current()); | 226 ASSERT(worker_thread_ == rtc::Thread::Current()); |
| 236 | 227 |
| 237 for (size_t i = 0; i < allocator_sessions_.size(); ++i) | 228 for (size_t i = 0; i < allocator_sessions_.size(); ++i) |
| 238 delete allocator_sessions_[i]; | 229 delete allocator_sessions_[i]; |
| 239 } | 230 } |
| 240 | 231 |
| 241 // Add the allocator session to our list so that we know which sessions | 232 // Add the allocator session to our list so that we know which sessions |
| 242 // are still active. | 233 // are still active. |
| (...skipping 916 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1159 SortConnections(); | 1150 SortConnections(); |
| 1160 } | 1151 } |
| 1161 | 1152 |
| 1162 // Handle queued up check-and-ping request | 1153 // Handle queued up check-and-ping request |
| 1163 void P2PTransportChannel::OnCheckAndPing() { | 1154 void P2PTransportChannel::OnCheckAndPing() { |
| 1164 // Make sure the states of the connections are up-to-date (since this affects | 1155 // Make sure the states of the connections are up-to-date (since this affects |
| 1165 // which ones are pingable). | 1156 // which ones are pingable). |
| 1166 UpdateConnectionStates(); | 1157 UpdateConnectionStates(); |
| 1167 // When the best connection is either not receiving or not writable, | 1158 // When the best connection is either not receiving or not writable, |
| 1168 // switch to weak ping delay. | 1159 // switch to weak ping delay. |
| 1169 int ping_delay = weak() ? weak_ping_delay_ : STRONG_PING_DELAY; | 1160 int ping_delay = weak() ? WEAK_PING_DELAY : STRONG_PING_DELAY; |
| 1170 if (rtc::Time() >= last_ping_sent_ms_ + ping_delay) { | 1161 if (rtc::Time() >= last_ping_sent_ms_ + ping_delay) { |
| 1171 Connection* conn = FindNextPingableConnection(); | 1162 Connection* conn = FindNextPingableConnection(); |
| 1172 if (conn) { | 1163 if (conn) { |
| 1173 PingConnection(conn); | 1164 PingConnection(conn); |
| 1174 } | 1165 } |
| 1175 } | 1166 } |
| 1176 int check_delay = std::min(ping_delay, check_receiving_delay_); | 1167 int check_delay = std::min(ping_delay, check_receiving_delay_); |
| 1177 thread()->PostDelayed(check_delay, this, MSG_CHECK_AND_PING); | 1168 thread()->PostDelayed(check_delay, this, MSG_CHECK_AND_PING); |
| 1178 } | 1169 } |
| 1179 | 1170 |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1373 SignalSentPacket(this, sent_packet); | 1364 SignalSentPacket(this, sent_packet); |
| 1374 } | 1365 } |
| 1375 | 1366 |
| 1376 void P2PTransportChannel::OnReadyToSend(Connection* connection) { | 1367 void P2PTransportChannel::OnReadyToSend(Connection* connection) { |
| 1377 if (connection == best_connection_ && writable()) { | 1368 if (connection == best_connection_ && writable()) { |
| 1378 SignalReadyToSend(this); | 1369 SignalReadyToSend(this); |
| 1379 } | 1370 } |
| 1380 } | 1371 } |
| 1381 | 1372 |
| 1382 } // namespace cricket | 1373 } // namespace cricket |
| OLD | NEW |