henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/p2p/base/p2ptransportchannel.h" |
| 12 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 13 | #include <algorithm> |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 14 | #include <iterator> |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 15 | #include <set> |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 16 | |
skvlad | 1d3c7e0 | 2017-01-11 17:50:30 -0800 | [diff] [blame] | 17 | #include "webrtc/api/umametrics.h" |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 18 | #include "webrtc/p2p/base/candidate.h" |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 19 | #include "webrtc/p2p/base/candidatepairinterface.h" |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 20 | #include "webrtc/p2p/base/common.h" |
| 21 | #include "webrtc/p2p/base/relayport.h" // For RELAY_PORT_TYPE. |
| 22 | #include "webrtc/p2p/base/stunport.h" // For STUN_PORT_TYPE. |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 23 | #include "webrtc/rtc_base/checks.h" |
| 24 | #include "webrtc/rtc_base/crc32.h" |
| 25 | #include "webrtc/rtc_base/logging.h" |
| 26 | #include "webrtc/rtc_base/stringencode.h" |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 27 | #include "webrtc/rtc_base/timeutils.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 28 | #include "webrtc/system_wrappers/include/field_trial.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 29 | |
| 30 | namespace { |
| 31 | |
| 32 | // messages for queuing up work for ourselves |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 33 | enum { |
| 34 | MSG_SORT_AND_UPDATE_STATE = 1, |
| 35 | MSG_CHECK_AND_PING, |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 36 | MSG_REGATHER_ON_FAILED_NETWORKS, |
| 37 | MSG_REGATHER_ON_ALL_NETWORKS |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 38 | }; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 39 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 40 | // The minimum improvement in RTT that justifies a switch. |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 41 | const int kMinImprovement = 10; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 42 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 43 | bool IsRelayRelay(const cricket::Connection* conn) { |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 44 | return conn->local_candidate().type() == cricket::RELAY_PORT_TYPE && |
| 45 | conn->remote_candidate().type() == cricket::RELAY_PORT_TYPE; |
| 46 | } |
| 47 | |
| 48 | bool IsUdp(cricket::Connection* conn) { |
| 49 | return conn->local_candidate().relay_protocol() == cricket::UDP_PROTOCOL_NAME; |
| 50 | } |
| 51 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 52 | cricket::PortInterface::CandidateOrigin GetOrigin(cricket::PortInterface* port, |
| 53 | cricket::PortInterface* origin_port) { |
| 54 | if (!origin_port) |
| 55 | return cricket::PortInterface::ORIGIN_MESSAGE; |
| 56 | else if (port == origin_port) |
| 57 | return cricket::PortInterface::ORIGIN_THIS_PORT; |
| 58 | else |
| 59 | return cricket::PortInterface::ORIGIN_OTHER_PORT; |
| 60 | } |
| 61 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 62 | } // unnamed namespace |
| 63 | |
| 64 | namespace cricket { |
| 65 | |
guoweis | b0bb77f | 2015-10-26 15:10:01 -0700 | [diff] [blame] | 66 | // When the socket is unwritable, we will use 10 Kbps (ignoring IP+UDP headers) |
| 67 | // for pinging. When the socket is writable, we will use only 1 Kbps because |
| 68 | // we don't want to degrade the quality on a modem. These numbers should work |
| 69 | // well on a 28.8K modem, which is the slowest connection on which the voice |
| 70 | // quality is reasonable at all. |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 71 | static const int PING_PACKET_SIZE = 60 * 8; |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 72 | |
| 73 | // The next two ping intervals are at the channel level. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 74 | // STRONG_PING_INTERVAL (480ms) is applied when the selected connection is both |
guoweis | b0bb77f | 2015-10-26 15:10:01 -0700 | [diff] [blame] | 75 | // writable and receiving. |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 76 | const int STRONG_PING_INTERVAL = 1000 * PING_PACKET_SIZE / 1000; |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 77 | // WEAK_PING_INTERVAL (48ms) is applied when the selected connection is either |
| 78 | // not writable or not receiving. |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 79 | const int WEAK_PING_INTERVAL = 1000 * PING_PACKET_SIZE / 10000; |
guoweis | b0bb77f | 2015-10-26 15:10:01 -0700 | [diff] [blame] | 80 | |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 81 | // The next two ping intervals are at the connection level. |
| 82 | // Writable connections are pinged at a faster rate while the connections are |
| 83 | // stabilizing or the channel is weak. |
| 84 | const int WEAK_OR_STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL = 900; // ms |
| 85 | // Writable connections are pinged at a slower rate once they are stabilized and |
| 86 | // the channel is strongly connected. |
| 87 | const int STRONG_AND_STABLE_WRITABLE_CONNECTION_PING_INTERVAL = 2500; // ms |
guoweis | b0bb77f | 2015-10-26 15:10:01 -0700 | [diff] [blame] | 88 | |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 89 | static const int MIN_CHECK_RECEIVING_INTERVAL = 50; // ms |
guoweis | b0bb77f | 2015-10-26 15:10:01 -0700 | [diff] [blame] | 90 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 91 | static const int RECEIVING_SWITCHING_DELAY = 1000; // ms |
| 92 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 93 | // We periodically check if any existing networks do not have any connection |
| 94 | // and regather on those networks. |
| 95 | static const int DEFAULT_REGATHER_ON_FAILED_NETWORKS_INTERVAL = 5 * 60 * 1000; |
Honghai Zhang | 895e1a9 | 2016-08-16 16:48:02 -0700 | [diff] [blame] | 96 | |
| 97 | static constexpr int DEFAULT_BACKUP_CONNECTION_PING_INTERVAL = 25 * 1000; |
| 98 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 99 | static constexpr int a_is_better = 1; |
| 100 | static constexpr int b_is_better = -1; |
| 101 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 102 | P2PTransportChannel::P2PTransportChannel(const std::string& transport_name, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 103 | int component, |
mikescarlett | b9dd7c5 | 2016-02-19 20:43:45 -0800 | [diff] [blame] | 104 | PortAllocator* allocator) |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 105 | : transport_name_(transport_name), |
| 106 | component_(component), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 107 | allocator_(allocator), |
johan | 0fd22ef | 2016-09-29 01:19:20 -0700 | [diff] [blame] | 108 | network_thread_(rtc::Thread::Current()), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 109 | incoming_only_(false), |
| 110 | error_(0), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 111 | sort_dirty_(false), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 112 | remote_ice_mode_(ICEMODE_FULL), |
| 113 | ice_role_(ICEROLE_UNKNOWN), |
| 114 | tiebreaker_(0), |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 115 | gathering_state_(kIceGatheringNew), |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 116 | rand_(rtc::SystemTimeNanos()), |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 117 | check_receiving_interval_(MIN_CHECK_RECEIVING_INTERVAL * 5), |
| 118 | config_(MIN_CHECK_RECEIVING_INTERVAL * 50 /* receiving_timeout */, |
Honghai Zhang | 895e1a9 | 2016-08-16 16:48:02 -0700 | [diff] [blame] | 119 | DEFAULT_BACKUP_CONNECTION_PING_INTERVAL, |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 120 | GATHER_ONCE /* continual_gathering_policy */, |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 121 | false /* prioritize_most_likely_candidate_pairs */, |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 122 | STRONG_AND_STABLE_WRITABLE_CONNECTION_PING_INTERVAL, |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 123 | true /* presume_writable_when_fully_relayed */, |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 124 | DEFAULT_REGATHER_ON_FAILED_NETWORKS_INTERVAL, |
| 125 | RECEIVING_SWITCHING_DELAY) { |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 126 | uint32_t weak_ping_interval = ::strtoul( |
guoweis | b0bb77f | 2015-10-26 15:10:01 -0700 | [diff] [blame] | 127 | webrtc::field_trial::FindFullName("WebRTC-StunInterPacketDelay").c_str(), |
| 128 | nullptr, 10); |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 129 | if (weak_ping_interval) { |
| 130 | weak_ping_interval_ = static_cast<int>(weak_ping_interval); |
guoweis | b0bb77f | 2015-10-26 15:10:01 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 133 | |
| 134 | P2PTransportChannel::~P2PTransportChannel() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 135 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // Add the allocator session to our list so that we know which sessions |
| 139 | // are still active. |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 140 | void P2PTransportChannel::AddAllocatorSession( |
| 141 | std::unique_ptr<PortAllocatorSession> session) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 142 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 143 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 144 | session->set_generation(static_cast<uint32_t>(allocator_sessions_.size())); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 145 | session->SignalPortReady.connect(this, &P2PTransportChannel::OnPortReady); |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 146 | session->SignalPortsPruned.connect(this, &P2PTransportChannel::OnPortsPruned); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 147 | session->SignalCandidatesReady.connect( |
| 148 | this, &P2PTransportChannel::OnCandidatesReady); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 149 | session->SignalCandidatesRemoved.connect( |
| 150 | this, &P2PTransportChannel::OnCandidatesRemoved); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 151 | session->SignalCandidatesAllocationDone.connect( |
| 152 | this, &P2PTransportChannel::OnCandidatesAllocationDone); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 153 | if (!allocator_sessions_.empty()) { |
| 154 | allocator_session()->PruneAllPorts(); |
| 155 | } |
| 156 | allocator_sessions_.push_back(std::move(session)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 157 | |
| 158 | // We now only want to apply new candidates that we receive to the ports |
| 159 | // created by this new session because these are replacing those of the |
| 160 | // previous sessions. |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 161 | PruneAllPorts(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void P2PTransportChannel::AddConnection(Connection* connection) { |
| 165 | connections_.push_back(connection); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 166 | unpinged_connections_.insert(connection); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 167 | connection->set_remote_ice_mode(remote_ice_mode_); |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 168 | connection->set_receiving_timeout(config_.receiving_timeout); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 169 | connection->SignalReadPacket.connect( |
| 170 | this, &P2PTransportChannel::OnReadPacket); |
| 171 | connection->SignalReadyToSend.connect( |
| 172 | this, &P2PTransportChannel::OnReadyToSend); |
| 173 | connection->SignalStateChange.connect( |
| 174 | this, &P2PTransportChannel::OnConnectionStateChange); |
| 175 | connection->SignalDestroyed.connect( |
| 176 | this, &P2PTransportChannel::OnConnectionDestroyed); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 177 | connection->SignalNominated.connect(this, &P2PTransportChannel::OnNominated); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 178 | had_connection_ = true; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 181 | // Determines whether we should switch the selected connection to |
| 182 | // |new_connection| based the writable/receiving state, the nomination state, |
| 183 | // and the last data received time. This prevents the controlled side from |
| 184 | // switching the selected connection too frequently when the controlling side |
| 185 | // is doing aggressive nominations. The precedence of the connection switching |
| 186 | // criteria is as follows: |
| 187 | // i) write/receiving/connected states |
| 188 | // ii) For controlled side, |
| 189 | // a) nomination state, |
| 190 | // b) last data received time. |
| 191 | // iii) Lower cost / higher priority. |
| 192 | // iv) rtt. |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 193 | // To further prevent switching to high-cost networks, does not switch to |
| 194 | // a high-cost connection if it is not receiving. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 195 | // TODO(honghaiz): Stop the aggressive nomination on the controlling side and |
| 196 | // implement the ice-renomination option. |
| 197 | bool P2PTransportChannel::ShouldSwitchSelectedConnection( |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 198 | Connection* new_connection, |
| 199 | bool* missed_receiving_unchanged_threshold) const { |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 200 | if (!ReadyToSend(new_connection) || selected_connection_ == new_connection) { |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 201 | return false; |
| 202 | } |
| 203 | |
| 204 | if (selected_connection_ == nullptr) { |
| 205 | return true; |
| 206 | } |
| 207 | |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 208 | // Do not switch to a connection that is not receiving if it has higher cost |
| 209 | // because it may be just spuriously better. |
| 210 | if (new_connection->ComputeNetworkCost() > |
| 211 | selected_connection_->ComputeNetworkCost() && |
| 212 | !new_connection->receiving()) { |
| 213 | return false; |
| 214 | } |
| 215 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 216 | rtc::Optional<int64_t> receiving_unchanged_threshold( |
| 217 | rtc::TimeMillis() - config_.receiving_switching_delay.value_or(0)); |
| 218 | int cmp = CompareConnections(selected_connection_, new_connection, |
| 219 | receiving_unchanged_threshold, |
| 220 | missed_receiving_unchanged_threshold); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 221 | if (cmp != 0) { |
| 222 | return cmp < 0; |
| 223 | } |
| 224 | |
| 225 | // If everything else is the same, switch only if rtt has improved by |
| 226 | // a margin. |
| 227 | return new_connection->rtt() <= selected_connection_->rtt() - kMinImprovement; |
| 228 | } |
| 229 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 230 | bool P2PTransportChannel::MaybeSwitchSelectedConnection( |
| 231 | Connection* new_connection, |
| 232 | const std::string& reason) { |
| 233 | bool missed_receiving_unchanged_threshold = false; |
| 234 | if (ShouldSwitchSelectedConnection(new_connection, |
| 235 | &missed_receiving_unchanged_threshold)) { |
| 236 | LOG(LS_INFO) << "Switching selected connection due to " << reason; |
| 237 | SwitchSelectedConnection(new_connection); |
| 238 | return true; |
| 239 | } |
| 240 | if (missed_receiving_unchanged_threshold && |
| 241 | config_.receiving_switching_delay) { |
| 242 | // If we do not switch to the connection because it missed the receiving |
| 243 | // threshold, the new connection is in a better receiving state than the |
| 244 | // currently selected connection. So we need to re-check whether it needs |
| 245 | // to be switched at a later time. |
| 246 | thread()->PostDelayed(RTC_FROM_HERE, *config_.receiving_switching_delay, |
| 247 | this, MSG_SORT_AND_UPDATE_STATE); |
| 248 | } |
| 249 | return false; |
| 250 | } |
| 251 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 252 | void P2PTransportChannel::SetIceRole(IceRole ice_role) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 253 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 254 | if (ice_role_ != ice_role) { |
| 255 | ice_role_ = ice_role; |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 256 | for (PortInterface* port : ports_) { |
| 257 | port->SetIceRole(ice_role); |
| 258 | } |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 259 | // Update role on pruned ports as well, because they may still have |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 260 | // connections alive that should be using the correct role. |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 261 | for (PortInterface* port : pruned_ports_) { |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 262 | port->SetIceRole(ice_role); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 267 | void P2PTransportChannel::SetIceTiebreaker(uint64_t tiebreaker) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 268 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 269 | if (!ports_.empty() || !pruned_ports_.empty()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 270 | LOG(LS_ERROR) |
| 271 | << "Attempt to change tiebreaker after Port has been allocated."; |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | tiebreaker_ = tiebreaker; |
| 276 | } |
| 277 | |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 278 | IceTransportState P2PTransportChannel::GetState() const { |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 279 | return state_; |
| 280 | } |
| 281 | |
skvlad | d030912 | 2017-02-02 17:18:37 -0800 | [diff] [blame] | 282 | rtc::Optional<int> P2PTransportChannel::GetRttEstimate() { |
| 283 | if (selected_connection_ != nullptr |
| 284 | && selected_connection_->rtt_samples() > 0) { |
| 285 | return rtc::Optional<int>(selected_connection_->rtt()); |
| 286 | } else { |
| 287 | return rtc::Optional<int>(); |
| 288 | } |
| 289 | } |
| 290 | |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 291 | // A channel is considered ICE completed once there is at most one active |
| 292 | // connection per network and at least one active connection. |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 293 | IceTransportState P2PTransportChannel::ComputeState() const { |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 294 | if (!had_connection_) { |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 295 | return IceTransportState::STATE_INIT; |
guoweis@webrtc.org | 8c9ff20 | 2014-12-04 07:56:02 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 298 | std::vector<Connection*> active_connections; |
| 299 | for (Connection* connection : connections_) { |
| 300 | if (connection->active()) { |
| 301 | active_connections.push_back(connection); |
| 302 | } |
| 303 | } |
| 304 | if (active_connections.empty()) { |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 305 | return IceTransportState::STATE_FAILED; |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | std::set<rtc::Network*> networks; |
| 309 | for (Connection* connection : active_connections) { |
| 310 | rtc::Network* network = connection->port()->Network(); |
guoweis@webrtc.org | 8c9ff20 | 2014-12-04 07:56:02 +0000 | [diff] [blame] | 311 | if (networks.find(network) == networks.end()) { |
| 312 | networks.insert(network); |
| 313 | } else { |
| 314 | LOG_J(LS_VERBOSE, this) << "Ice not completed yet for this channel as " |
| 315 | << network->ToString() |
| 316 | << " has more than 1 connection."; |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 317 | return IceTransportState::STATE_CONNECTING; |
guoweis@webrtc.org | 8c9ff20 | 2014-12-04 07:56:02 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
guoweis@webrtc.org | 8c9ff20 | 2014-12-04 07:56:02 +0000 | [diff] [blame] | 320 | |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 321 | return IceTransportState::STATE_COMPLETED; |
guoweis@webrtc.org | 8c9ff20 | 2014-12-04 07:56:02 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 324 | void P2PTransportChannel::SetIceParameters(const IceParameters& ice_params) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 325 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
Honghai Zhang | 9ecb085 | 2016-09-15 16:41:04 -0700 | [diff] [blame] | 326 | LOG(LS_INFO) << "Set ICE ufrag: " << ice_params.ufrag |
| 327 | << " pwd: " << ice_params.pwd << " on transport " |
| 328 | << transport_name(); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 329 | ice_parameters_ = ice_params; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 330 | // Note: Candidate gathering will restart when MaybeStartGathering is next |
| 331 | // called. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 334 | void P2PTransportChannel::SetRemoteIceParameters( |
| 335 | const IceParameters& ice_params) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 336 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 337 | LOG(LS_INFO) << "Remote supports ICE renomination ? " |
| 338 | << ice_params.renomination; |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 339 | IceParameters* current_ice = remote_ice(); |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 340 | if (!current_ice || *current_ice != ice_params) { |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 341 | // Keep the ICE credentials so that newer connections |
| 342 | // are prioritized over the older ones. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 343 | remote_ice_parameters_.push_back(ice_params); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 344 | } |
| 345 | |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 346 | // Update the pwd of remote candidate if needed. |
| 347 | for (RemoteCandidate& candidate : remote_candidates_) { |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 348 | if (candidate.username() == ice_params.ufrag && |
| 349 | candidate.password().empty()) { |
| 350 | candidate.set_password(ice_params.pwd); |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 351 | } |
| 352 | } |
Taylor Brandstetter | 0a1bc53 | 2016-04-19 18:03:26 -0700 | [diff] [blame] | 353 | // We need to update the credentials and generation for any peer reflexive |
| 354 | // candidates. |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 355 | for (Connection* conn : connections_) { |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 356 | conn->MaybeSetRemoteIceParametersAndGeneration( |
| 357 | ice_params, static_cast<int>(remote_ice_parameters_.size() - 1)); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 358 | } |
Taylor Brandstetter | 0a1bc53 | 2016-04-19 18:03:26 -0700 | [diff] [blame] | 359 | // Updating the remote ICE candidate generation could change the sort order. |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 360 | RequestSortAndStateUpdate(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | void P2PTransportChannel::SetRemoteIceMode(IceMode mode) { |
| 364 | remote_ice_mode_ = mode; |
| 365 | } |
| 366 | |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 367 | void P2PTransportChannel::SetIceConfig(const IceConfig& config) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 368 | if (config_.continual_gathering_policy != config.continual_gathering_policy) { |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 369 | if (!allocator_sessions_.empty()) { |
| 370 | LOG(LS_ERROR) << "Trying to change continual gathering policy " |
| 371 | << "when gathering has already started!"; |
| 372 | } else { |
| 373 | config_.continual_gathering_policy = config.continual_gathering_policy; |
| 374 | LOG(LS_INFO) << "Set continual_gathering_policy to " |
| 375 | << config_.continual_gathering_policy; |
| 376 | } |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 377 | } |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 378 | |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 379 | if (config.backup_connection_ping_interval >= 0 && |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 380 | config_.backup_connection_ping_interval != |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 381 | config.backup_connection_ping_interval) { |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 382 | config_.backup_connection_ping_interval = |
| 383 | config.backup_connection_ping_interval; |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 384 | LOG(LS_INFO) << "Set backup connection ping interval to " |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 385 | << config_.backup_connection_ping_interval << " milliseconds."; |
honghaiz | 9009962 | 2015-07-13 12:19:33 -0700 | [diff] [blame] | 386 | } |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 387 | |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 388 | if (config.receiving_timeout >= 0 && |
| 389 | config_.receiving_timeout != config.receiving_timeout) { |
| 390 | config_.receiving_timeout = config.receiving_timeout; |
| 391 | check_receiving_interval_ = |
| 392 | std::max(MIN_CHECK_RECEIVING_INTERVAL, config_.receiving_timeout / 10); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 393 | |
| 394 | for (Connection* connection : connections_) { |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 395 | connection->set_receiving_timeout(config_.receiving_timeout); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 396 | } |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 397 | LOG(LS_INFO) << "Set ICE receiving timeout to " << config_.receiving_timeout |
| 398 | << " milliseconds"; |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 399 | } |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 400 | |
| 401 | config_.prioritize_most_likely_candidate_pairs = |
| 402 | config.prioritize_most_likely_candidate_pairs; |
| 403 | LOG(LS_INFO) << "Set ping most likely connection to " |
| 404 | << config_.prioritize_most_likely_candidate_pairs; |
| 405 | |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 406 | if (config.stable_writable_connection_ping_interval >= 0 && |
| 407 | config_.stable_writable_connection_ping_interval != |
| 408 | config.stable_writable_connection_ping_interval) { |
| 409 | config_.stable_writable_connection_ping_interval = |
| 410 | config.stable_writable_connection_ping_interval; |
| 411 | LOG(LS_INFO) << "Set stable_writable_connection_ping_interval to " |
| 412 | << config_.stable_writable_connection_ping_interval; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 413 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 414 | |
| 415 | if (config.presume_writable_when_fully_relayed != |
| 416 | config_.presume_writable_when_fully_relayed) { |
| 417 | if (!connections_.empty()) { |
| 418 | LOG(LS_ERROR) << "Trying to change 'presume writable' " |
| 419 | << "while connections already exist!"; |
| 420 | } else { |
| 421 | config_.presume_writable_when_fully_relayed = |
| 422 | config.presume_writable_when_fully_relayed; |
| 423 | LOG(LS_INFO) << "Set presume writable when fully relayed to " |
| 424 | << config_.presume_writable_when_fully_relayed; |
| 425 | } |
| 426 | } |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 427 | |
| 428 | if (config.regather_on_failed_networks_interval) { |
| 429 | config_.regather_on_failed_networks_interval = |
| 430 | config.regather_on_failed_networks_interval; |
| 431 | LOG(LS_INFO) << "Set regather_on_failed_networks_interval to " |
| 432 | << *config_.regather_on_failed_networks_interval; |
| 433 | } |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 434 | |
| 435 | if (config.regather_all_networks_interval_range) { |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 436 | // Config validation is assumed to have already happened at the API layer. |
| 437 | RTC_DCHECK(config.continual_gathering_policy != GATHER_ONCE); |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 438 | config_.regather_all_networks_interval_range = |
| 439 | config.regather_all_networks_interval_range; |
| 440 | LOG(LS_INFO) << "Set regather_all_networks_interval_range to " |
| 441 | << config.regather_all_networks_interval_range->ToString(); |
| 442 | } |
| 443 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 444 | if (config.receiving_switching_delay) { |
| 445 | config_.receiving_switching_delay = config.receiving_switching_delay; |
| 446 | LOG(LS_INFO) << "Set receiving_switching_delay to" |
| 447 | << *config_.receiving_switching_delay; |
| 448 | } |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 449 | |
| 450 | if (config_.default_nomination_mode != config.default_nomination_mode) { |
| 451 | config_.default_nomination_mode = config.default_nomination_mode; |
| 452 | LOG(LS_INFO) << "Set default nomination mode to " |
| 453 | << static_cast<int>(config_.default_nomination_mode); |
| 454 | } |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 455 | |
| 456 | if (config_.ice_check_min_interval != config.ice_check_min_interval) { |
| 457 | config_.ice_check_min_interval = config.ice_check_min_interval; |
| 458 | LOG(LS_INFO) << "Set min ping interval to " |
| 459 | << *config_.ice_check_min_interval; |
| 460 | } |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | const IceConfig& P2PTransportChannel::config() const { |
| 464 | return config_; |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 467 | void P2PTransportChannel::SetMetricsObserver( |
| 468 | webrtc::MetricsObserverInterface* observer) { |
| 469 | metrics_observer_ = observer; |
| 470 | } |
| 471 | |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 472 | void P2PTransportChannel::MaybeStartGathering() { |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 473 | if (ice_parameters_.ufrag.empty() || ice_parameters_.pwd.empty()) { |
Honghai Zhang | 9ecb085 | 2016-09-15 16:41:04 -0700 | [diff] [blame] | 474 | LOG(LS_ERROR) << "Cannot gather candidates because ICE parameters are empty" |
| 475 | << " ufrag: " << ice_parameters_.ufrag |
| 476 | << " pwd: " << ice_parameters_.pwd; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 477 | return; |
| 478 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 479 | // Start gathering if we never started before, or if an ICE restart occurred. |
| 480 | if (allocator_sessions_.empty() || |
| 481 | IceCredentialsChanged(allocator_sessions_.back()->ice_ufrag(), |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 482 | allocator_sessions_.back()->ice_pwd(), |
| 483 | ice_parameters_.ufrag, ice_parameters_.pwd)) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 484 | if (gathering_state_ != kIceGatheringGathering) { |
| 485 | gathering_state_ = kIceGatheringGathering; |
| 486 | SignalGatheringState(this); |
| 487 | } |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 488 | |
| 489 | if (metrics_observer_ && !allocator_sessions_.empty()) { |
| 490 | IceRestartState state; |
| 491 | if (writable()) { |
| 492 | state = IceRestartState::CONNECTED; |
| 493 | } else if (IsGettingPorts()) { |
| 494 | state = IceRestartState::CONNECTING; |
| 495 | } else { |
| 496 | state = IceRestartState::DISCONNECTED; |
| 497 | } |
| 498 | metrics_observer_->IncrementEnumCounter( |
| 499 | webrtc::kEnumCounterIceRestart, static_cast<int>(state), |
| 500 | static_cast<int>(IceRestartState::MAX_VALUE)); |
| 501 | } |
| 502 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 503 | // Time for a new allocator. |
| 504 | std::unique_ptr<PortAllocatorSession> pooled_session = |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 505 | allocator_->TakePooledSession(transport_name(), component(), |
| 506 | ice_parameters_.ufrag, |
| 507 | ice_parameters_.pwd); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 508 | if (pooled_session) { |
| 509 | AddAllocatorSession(std::move(pooled_session)); |
| 510 | PortAllocatorSession* raw_pooled_session = |
| 511 | allocator_sessions_.back().get(); |
| 512 | // Process the pooled session's existing candidates/ports, if they exist. |
| 513 | OnCandidatesReady(raw_pooled_session, |
| 514 | raw_pooled_session->ReadyCandidates()); |
| 515 | for (PortInterface* port : allocator_sessions_.back()->ReadyPorts()) { |
| 516 | OnPortReady(raw_pooled_session, port); |
| 517 | } |
| 518 | if (allocator_sessions_.back()->CandidatesAllocationDone()) { |
| 519 | OnCandidatesAllocationDone(raw_pooled_session); |
| 520 | } |
| 521 | } else { |
| 522 | AddAllocatorSession(allocator_->CreateSession( |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 523 | transport_name(), component(), ice_parameters_.ufrag, |
| 524 | ice_parameters_.pwd)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 525 | allocator_sessions_.back()->StartGettingPorts(); |
| 526 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 530 | // A new port is available, attempt to make connections for it |
| 531 | void P2PTransportChannel::OnPortReady(PortAllocatorSession *session, |
| 532 | PortInterface* port) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 533 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 534 | |
| 535 | // Set in-effect options on the new port |
| 536 | for (OptionMap::const_iterator it = options_.begin(); |
| 537 | it != options_.end(); |
| 538 | ++it) { |
| 539 | int val = port->SetOption(it->first, it->second); |
| 540 | if (val < 0) { |
| 541 | LOG_J(LS_WARNING, port) << "SetOption(" << it->first |
| 542 | << ", " << it->second |
| 543 | << ") failed: " << port->GetError(); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | // Remember the ports and candidates, and signal that candidates are ready. |
| 548 | // The session will handle this, and send an initiate/accept/modify message |
| 549 | // if one is pending. |
| 550 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 551 | port->SetIceRole(ice_role_); |
| 552 | port->SetIceTiebreaker(tiebreaker_); |
| 553 | ports_.push_back(port); |
| 554 | port->SignalUnknownAddress.connect( |
| 555 | this, &P2PTransportChannel::OnUnknownAddress); |
| 556 | port->SignalDestroyed.connect(this, &P2PTransportChannel::OnPortDestroyed); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 557 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 558 | port->SignalRoleConflict.connect( |
| 559 | this, &P2PTransportChannel::OnRoleConflict); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 560 | port->SignalSentPacket.connect(this, &P2PTransportChannel::OnSentPacket); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 561 | |
| 562 | // Attempt to create a connection from this new port to all of the remote |
| 563 | // candidates that we were given so far. |
| 564 | |
| 565 | std::vector<RemoteCandidate>::iterator iter; |
| 566 | for (iter = remote_candidates_.begin(); iter != remote_candidates_.end(); |
| 567 | ++iter) { |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 568 | CreateConnection(port, *iter, iter->origin_port()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 571 | SortConnectionsAndUpdateState(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | // A new candidate is available, let listeners know |
| 575 | void P2PTransportChannel::OnCandidatesReady( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 576 | PortAllocatorSession* session, |
| 577 | const std::vector<Candidate>& candidates) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 578 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 579 | for (size_t i = 0; i < candidates.size(); ++i) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 580 | SignalCandidateGathered(this, candidates[i]); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
| 584 | void P2PTransportChannel::OnCandidatesAllocationDone( |
| 585 | PortAllocatorSession* session) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 586 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
deadbeef | b60a819 | 2016-08-24 15:15:00 -0700 | [diff] [blame] | 587 | if (config_.gather_continually()) { |
| 588 | LOG(LS_INFO) << "P2PTransportChannel: " << transport_name() |
| 589 | << ", component " << component() |
| 590 | << " gathering complete, but using continual " |
| 591 | << "gathering so not changing gathering state."; |
| 592 | return; |
| 593 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 594 | gathering_state_ = kIceGatheringComplete; |
| 595 | LOG(LS_INFO) << "P2PTransportChannel: " << transport_name() << ", component " |
| 596 | << component() << " gathering complete"; |
| 597 | SignalGatheringState(this); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | // Handle stun packets |
| 601 | void P2PTransportChannel::OnUnknownAddress( |
| 602 | PortInterface* port, |
| 603 | const rtc::SocketAddress& address, ProtocolType proto, |
| 604 | IceMessage* stun_msg, const std::string &remote_username, |
| 605 | bool port_muxed) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 606 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 607 | |
| 608 | // Port has received a valid stun packet from an address that no Connection |
| 609 | // is currently available for. See if we already have a candidate with the |
| 610 | // address. If it isn't we need to create new candidate for it. |
| 611 | |
Taylor Brandstetter | f7c15a9 | 2016-06-22 13:13:55 -0700 | [diff] [blame] | 612 | const Candidate* candidate = nullptr; |
| 613 | for (const Candidate& c : remote_candidates_) { |
| 614 | if (c.username() == remote_username && c.address() == address && |
| 615 | c.protocol() == ProtoToString(proto)) { |
| 616 | candidate = &c; |
| 617 | break; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 621 | uint32_t remote_generation = 0; |
Taylor Brandstetter | f7c15a9 | 2016-06-22 13:13:55 -0700 | [diff] [blame] | 622 | std::string remote_password; |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 623 | // The STUN binding request may arrive after setRemoteDescription and before |
| 624 | // adding remote candidate, so we need to set the password to the shared |
Taylor Brandstetter | f7c15a9 | 2016-06-22 13:13:55 -0700 | [diff] [blame] | 625 | // password and set the generation if the user name matches. |
| 626 | const IceParameters* ice_param = |
| 627 | FindRemoteIceFromUfrag(remote_username, &remote_generation); |
| 628 | // Note: if not found, the remote_generation will still be 0. |
| 629 | if (ice_param != nullptr) { |
| 630 | remote_password = ice_param->pwd; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 633 | Candidate remote_candidate; |
| 634 | bool remote_candidate_is_new = (candidate == nullptr); |
| 635 | if (!remote_candidate_is_new) { |
| 636 | remote_candidate = *candidate; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 637 | } else { |
| 638 | // Create a new candidate with this address. |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 639 | // The priority of the candidate is set to the PRIORITY attribute |
| 640 | // from the request. |
| 641 | const StunUInt32Attribute* priority_attr = |
| 642 | stun_msg->GetUInt32(STUN_ATTR_PRIORITY); |
| 643 | if (!priority_attr) { |
| 644 | LOG(LS_WARNING) << "P2PTransportChannel::OnUnknownAddress - " |
| 645 | << "No STUN_ATTR_PRIORITY found in the " |
| 646 | << "stun request message"; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 647 | port->SendBindingErrorResponse(stun_msg, address, STUN_ERROR_BAD_REQUEST, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 648 | STUN_ERROR_REASON_BAD_REQUEST); |
| 649 | return; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 650 | } |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 651 | int remote_candidate_priority = priority_attr->value(); |
| 652 | |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 653 | uint16_t network_id = 0; |
| 654 | uint16_t network_cost = 0; |
| 655 | const StunUInt32Attribute* network_attr = |
| 656 | stun_msg->GetUInt32(STUN_ATTR_NETWORK_INFO); |
| 657 | if (network_attr) { |
| 658 | uint32_t network_info = network_attr->value(); |
| 659 | network_id = static_cast<uint16_t>(network_info >> 16); |
| 660 | network_cost = static_cast<uint16_t>(network_info); |
| 661 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 662 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 663 | // RFC 5245 |
| 664 | // If the source transport address of the request does not match any |
| 665 | // existing remote candidates, it represents a new peer reflexive remote |
| 666 | // candidate. |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 667 | remote_candidate = Candidate( |
| 668 | component(), ProtoToString(proto), address, remote_candidate_priority, |
| 669 | remote_username, remote_password, PRFLX_PORT_TYPE, remote_generation, |
| 670 | "", network_id, network_cost); |
guoweis@webrtc.org | 61c1247 | 2015-01-15 06:53:07 +0000 | [diff] [blame] | 671 | |
| 672 | // From RFC 5245, section-7.2.1.3: |
| 673 | // The foundation of the candidate is set to an arbitrary value, different |
| 674 | // from the foundation for all other remote candidates. |
Guo-wei Shieh | 372f2fc | 2015-06-12 10:12:46 -0700 | [diff] [blame] | 675 | remote_candidate.set_foundation( |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 676 | rtc::ToString<uint32_t>(rtc::ComputeCrc32(remote_candidate.id()))); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 679 | // RFC5245, the agent constructs a pair whose local candidate is equal to |
| 680 | // the transport address on which the STUN request was received, and a |
| 681 | // remote candidate equal to the source transport address where the |
| 682 | // request came from. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 683 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 684 | // There shouldn't be an existing connection with this remote address. |
| 685 | // When ports are muxed, this channel might get multiple unknown address |
| 686 | // signals. In that case if the connection is already exists, we should |
| 687 | // simply ignore the signal otherwise send server error. |
| 688 | if (port->GetConnection(remote_candidate.address())) { |
| 689 | if (port_muxed) { |
| 690 | LOG(LS_INFO) << "Connection already exists for peer reflexive " |
| 691 | << "candidate: " << remote_candidate.ToString(); |
| 692 | return; |
| 693 | } else { |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 694 | RTC_NOTREACHED(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 695 | port->SendBindingErrorResponse(stun_msg, address, |
| 696 | STUN_ERROR_SERVER_ERROR, |
| 697 | STUN_ERROR_REASON_SERVER_ERROR); |
| 698 | return; |
| 699 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 700 | } |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 701 | |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 702 | Connection* connection = |
| 703 | port->CreateConnection(remote_candidate, PortInterface::ORIGIN_THIS_PORT); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 704 | if (!connection) { |
Honghai Zhang | d5fff50 | 2016-09-09 20:47:53 -0700 | [diff] [blame] | 705 | // This could happen in some scenarios. For example, a TurnPort may have |
| 706 | // had a refresh request timeout, so it won't create connections. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 707 | port->SendBindingErrorResponse(stun_msg, address, STUN_ERROR_SERVER_ERROR, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 708 | STUN_ERROR_REASON_SERVER_ERROR); |
| 709 | return; |
| 710 | } |
| 711 | |
| 712 | LOG(LS_INFO) << "Adding connection from " |
| 713 | << (remote_candidate_is_new ? "peer reflexive" : "resurrected") |
| 714 | << " candidate: " << remote_candidate.ToString(); |
| 715 | AddConnection(connection); |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 716 | connection->HandleBindingRequest(stun_msg); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 717 | |
| 718 | // Update the list of connections since we just added another. We do this |
| 719 | // after sending the response since it could (in principle) delete the |
| 720 | // connection in question. |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 721 | SortConnectionsAndUpdateState(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | void P2PTransportChannel::OnRoleConflict(PortInterface* port) { |
| 725 | SignalRoleConflict(this); // STUN ping will be sent when SetRole is called |
| 726 | // from Transport. |
| 727 | } |
| 728 | |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 729 | const IceParameters* P2PTransportChannel::FindRemoteIceFromUfrag( |
| 730 | const std::string& ufrag, |
| 731 | uint32_t* generation) { |
| 732 | const auto& params = remote_ice_parameters_; |
| 733 | auto it = std::find_if( |
| 734 | params.rbegin(), params.rend(), |
| 735 | [ufrag](const IceParameters& param) { return param.ufrag == ufrag; }); |
| 736 | if (it == params.rend()) { |
| 737 | // Not found. |
| 738 | return nullptr; |
| 739 | } |
| 740 | *generation = params.rend() - it - 1; |
| 741 | return &(*it); |
| 742 | } |
| 743 | |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 744 | void P2PTransportChannel::OnNominated(Connection* conn) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 745 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
| 746 | RTC_DCHECK(ice_role_ == ICEROLE_CONTROLLED); |
Peter Thatcher | 42af6ca | 2015-05-15 12:23:27 -0700 | [diff] [blame] | 747 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 748 | if (selected_connection_ == conn) { |
| 749 | return; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 750 | } |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 751 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 752 | if (MaybeSwitchSelectedConnection(conn, |
| 753 | "nomination on the controlled side")) { |
| 754 | // Now that we have selected a connection, it is time to prune other |
| 755 | // connections and update the read/write state of the channel. |
| 756 | RequestSortAndStateUpdate(); |
| 757 | } else { |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 758 | LOG(LS_INFO) |
| 759 | << "Not switching the selected connection on controlled side yet: " |
| 760 | << conn->ToString(); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 761 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 762 | } |
| 763 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 764 | void P2PTransportChannel::AddRemoteCandidate(const Candidate& candidate) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 765 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 766 | |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 767 | uint32_t generation = GetRemoteCandidateGeneration(candidate); |
| 768 | // If a remote candidate with a previous generation arrives, drop it. |
| 769 | if (generation < remote_ice_generation()) { |
| 770 | LOG(LS_WARNING) << "Dropping a remote candidate because its ufrag " |
| 771 | << candidate.username() |
| 772 | << " indicates it was for a previous generation."; |
honghaiz | 503726c | 2015-07-31 12:37:38 -0700 | [diff] [blame] | 773 | return; |
| 774 | } |
| 775 | |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 776 | Candidate new_remote_candidate(candidate); |
| 777 | new_remote_candidate.set_generation(generation); |
| 778 | // ICE candidates don't need to have username and password set, but |
| 779 | // the code below this (specifically, ConnectionRequest::Prepare in |
| 780 | // port.cc) uses the remote candidates's username. So, we set it |
| 781 | // here. |
| 782 | if (remote_ice()) { |
| 783 | if (candidate.username().empty()) { |
| 784 | new_remote_candidate.set_username(remote_ice()->ufrag); |
| 785 | } |
| 786 | if (new_remote_candidate.username() == remote_ice()->ufrag) { |
| 787 | if (candidate.password().empty()) { |
| 788 | new_remote_candidate.set_password(remote_ice()->pwd); |
| 789 | } |
| 790 | } else { |
| 791 | // The candidate belongs to the next generation. Its pwd will be set |
| 792 | // when the new remote ICE credentials arrive. |
| 793 | LOG(LS_WARNING) << "A remote candidate arrives with an unknown ufrag: " |
| 794 | << candidate.username(); |
| 795 | } |
| 796 | } |
| 797 | |
deadbeef | 0af180b | 2016-06-21 13:15:32 -0700 | [diff] [blame] | 798 | // If this candidate matches what was thought to be a peer reflexive |
| 799 | // candidate, we need to update the candidate priority/etc. |
| 800 | for (Connection* conn : connections_) { |
| 801 | conn->MaybeUpdatePeerReflexiveCandidate(new_remote_candidate); |
| 802 | } |
| 803 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 804 | // Create connections to this remote candidate. |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 805 | CreateConnections(new_remote_candidate, NULL); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 806 | |
| 807 | // Resort the connections list, which may have new elements. |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 808 | SortConnectionsAndUpdateState(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 811 | void P2PTransportChannel::RemoveRemoteCandidate( |
| 812 | const Candidate& cand_to_remove) { |
| 813 | auto iter = |
| 814 | std::remove_if(remote_candidates_.begin(), remote_candidates_.end(), |
| 815 | [cand_to_remove](const Candidate& candidate) { |
| 816 | return cand_to_remove.MatchesForRemoval(candidate); |
| 817 | }); |
| 818 | if (iter != remote_candidates_.end()) { |
| 819 | LOG(LS_VERBOSE) << "Removed remote candidate " << cand_to_remove.ToString(); |
| 820 | remote_candidates_.erase(iter, remote_candidates_.end()); |
| 821 | } |
| 822 | } |
| 823 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 824 | // Creates connections from all of the ports that we care about to the given |
| 825 | // remote candidate. The return value is true if we created a connection from |
| 826 | // the origin port. |
| 827 | bool P2PTransportChannel::CreateConnections(const Candidate& remote_candidate, |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 828 | PortInterface* origin_port) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 829 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 830 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 831 | // If we've already seen the new remote candidate (in the current candidate |
| 832 | // generation), then we shouldn't try creating connections for it. |
| 833 | // We either already have a connection for it, or we previously created one |
| 834 | // and then later pruned it. If we don't return, the channel will again |
| 835 | // re-create any connections that were previously pruned, which will then |
| 836 | // immediately be re-pruned, churning the network for no purpose. |
| 837 | // This only applies to candidates received over signaling (i.e. origin_port |
| 838 | // is NULL). |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 839 | if (!origin_port && IsDuplicateRemoteCandidate(remote_candidate)) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 840 | // return true to indicate success, without creating any new connections. |
| 841 | return true; |
| 842 | } |
| 843 | |
| 844 | // Add a new connection for this candidate to every port that allows such a |
| 845 | // connection (i.e., if they have compatible protocols) and that does not |
| 846 | // already have a connection to an equivalent candidate. We must be careful |
| 847 | // to make sure that the origin port is included, even if it was pruned, |
| 848 | // since that may be the only port that can create this connection. |
| 849 | bool created = false; |
| 850 | std::vector<PortInterface *>::reverse_iterator it; |
| 851 | for (it = ports_.rbegin(); it != ports_.rend(); ++it) { |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 852 | if (CreateConnection(*it, remote_candidate, origin_port)) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 853 | if (*it == origin_port) |
| 854 | created = true; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | if ((origin_port != NULL) && |
| 859 | std::find(ports_.begin(), ports_.end(), origin_port) == ports_.end()) { |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 860 | if (CreateConnection(origin_port, remote_candidate, origin_port)) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 861 | created = true; |
| 862 | } |
| 863 | |
| 864 | // Remember this remote candidate so that we can add it to future ports. |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 865 | RememberRemoteCandidate(remote_candidate, origin_port); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 866 | |
| 867 | return created; |
| 868 | } |
| 869 | |
| 870 | // Setup a connection object for the local and remote candidate combination. |
| 871 | // And then listen to connection object for changes. |
| 872 | bool P2PTransportChannel::CreateConnection(PortInterface* port, |
| 873 | const Candidate& remote_candidate, |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 874 | PortInterface* origin_port) { |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 875 | if (!port->SupportsProtocol(remote_candidate.protocol())) { |
| 876 | return false; |
| 877 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 878 | // Look for an existing connection with this remote address. If one is not |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 879 | // found or it is found but the existing remote candidate has an older |
| 880 | // generation, then we can create a new connection for this address. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 881 | Connection* connection = port->GetConnection(remote_candidate.address()); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 882 | if (connection == nullptr || |
| 883 | connection->remote_candidate().generation() < |
| 884 | remote_candidate.generation()) { |
| 885 | // Don't create a connection if this is a candidate we received in a |
| 886 | // message and we are not allowed to make outgoing connections. |
| 887 | PortInterface::CandidateOrigin origin = GetOrigin(port, origin_port); |
| 888 | if (origin == PortInterface::ORIGIN_MESSAGE && incoming_only_) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 889 | return false; |
| 890 | } |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 891 | Connection* connection = port->CreateConnection(remote_candidate, origin); |
| 892 | if (!connection) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 893 | return false; |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 894 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 895 | AddConnection(connection); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 896 | LOG_J(LS_INFO, this) << "Created connection with origin=" << origin << ", (" |
| 897 | << connections_.size() << " total)"; |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 898 | return true; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 899 | } |
| 900 | |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 901 | // No new connection was created. |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 902 | // It is not legal to try to change any of the parameters of an existing |
| 903 | // connection; however, the other side can send a duplicate candidate. |
| 904 | if (!remote_candidate.IsEquivalent(connection->remote_candidate())) { |
| 905 | LOG(INFO) << "Attempt to change a remote candidate." |
| 906 | << " Existing remote candidate: " |
| 907 | << connection->remote_candidate().ToString() |
| 908 | << "New remote candidate: " << remote_candidate.ToString(); |
| 909 | } |
| 910 | return false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 911 | } |
| 912 | |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 913 | bool P2PTransportChannel::FindConnection(Connection* connection) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 914 | std::vector<Connection*>::const_iterator citer = |
| 915 | std::find(connections_.begin(), connections_.end(), connection); |
| 916 | return citer != connections_.end(); |
| 917 | } |
| 918 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 919 | uint32_t P2PTransportChannel::GetRemoteCandidateGeneration( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 920 | const Candidate& candidate) { |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 921 | // If the candidate has a ufrag, use it to find the generation. |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 922 | if (!candidate.username().empty()) { |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 923 | uint32_t generation = 0; |
| 924 | if (!FindRemoteIceFromUfrag(candidate.username(), &generation)) { |
| 925 | // If the ufrag is not found, assume the next/future generation. |
| 926 | generation = static_cast<uint32_t>(remote_ice_parameters_.size()); |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 927 | } |
honghaiz | 112fe43 | 2015-12-30 13:32:47 -0800 | [diff] [blame] | 928 | return generation; |
honghaiz | a54a080 | 2015-12-16 18:37:23 -0800 | [diff] [blame] | 929 | } |
| 930 | // If candidate generation is set, use that. |
| 931 | if (candidate.generation() > 0) { |
| 932 | return candidate.generation(); |
| 933 | } |
| 934 | // Otherwise, assume the generation from remote ice parameters. |
| 935 | return remote_ice_generation(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | // Check if remote candidate is already cached. |
| 939 | bool P2PTransportChannel::IsDuplicateRemoteCandidate( |
| 940 | const Candidate& candidate) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 941 | for (size_t i = 0; i < remote_candidates_.size(); ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 942 | if (remote_candidates_[i].IsEquivalent(candidate)) { |
| 943 | return true; |
| 944 | } |
| 945 | } |
| 946 | return false; |
| 947 | } |
| 948 | |
| 949 | // Maintain our remote candidate list, adding this new remote one. |
| 950 | void P2PTransportChannel::RememberRemoteCandidate( |
| 951 | const Candidate& remote_candidate, PortInterface* origin_port) { |
| 952 | // Remove any candidates whose generation is older than this one. The |
| 953 | // presence of a new generation indicates that the old ones are not useful. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 954 | size_t i = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 955 | while (i < remote_candidates_.size()) { |
| 956 | if (remote_candidates_[i].generation() < remote_candidate.generation()) { |
| 957 | LOG(INFO) << "Pruning candidate from old generation: " |
| 958 | << remote_candidates_[i].address().ToSensitiveString(); |
| 959 | remote_candidates_.erase(remote_candidates_.begin() + i); |
| 960 | } else { |
| 961 | i += 1; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | // Make sure this candidate is not a duplicate. |
| 966 | if (IsDuplicateRemoteCandidate(remote_candidate)) { |
| 967 | LOG(INFO) << "Duplicate candidate: " << remote_candidate.ToString(); |
| 968 | return; |
| 969 | } |
| 970 | |
| 971 | // Try this candidate for all future ports. |
| 972 | remote_candidates_.push_back(RemoteCandidate(remote_candidate, origin_port)); |
| 973 | } |
| 974 | |
| 975 | // Set options on ourselves is simply setting options on all of our available |
| 976 | // port objects. |
| 977 | int P2PTransportChannel::SetOption(rtc::Socket::Option opt, int value) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 978 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 979 | OptionMap::iterator it = options_.find(opt); |
| 980 | if (it == options_.end()) { |
| 981 | options_.insert(std::make_pair(opt, value)); |
| 982 | } else if (it->second == value) { |
| 983 | return 0; |
| 984 | } else { |
| 985 | it->second = value; |
| 986 | } |
| 987 | |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 988 | for (PortInterface* port : ports_) { |
| 989 | int val = port->SetOption(opt, value); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 990 | if (val < 0) { |
| 991 | // Because this also occurs deferred, probably no point in reporting an |
| 992 | // error |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 993 | LOG(WARNING) << "SetOption(" << opt << ", " << value |
| 994 | << ") failed: " << port->GetError(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 995 | } |
| 996 | } |
| 997 | return 0; |
| 998 | } |
| 999 | |
pthatcher@webrtc.org | 877ac76 | 2015-02-04 22:03:09 +0000 | [diff] [blame] | 1000 | bool P2PTransportChannel::GetOption(rtc::Socket::Option opt, int* value) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1001 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
pthatcher@webrtc.org | 877ac76 | 2015-02-04 22:03:09 +0000 | [diff] [blame] | 1002 | |
| 1003 | const auto& found = options_.find(opt); |
| 1004 | if (found == options_.end()) { |
| 1005 | return false; |
| 1006 | } |
| 1007 | *value = found->second; |
| 1008 | return true; |
| 1009 | } |
| 1010 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1011 | // Send data to the other side, using our selected connection. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1012 | int P2PTransportChannel::SendPacket(const char *data, size_t len, |
| 1013 | const rtc::PacketOptions& options, |
| 1014 | int flags) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1015 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1016 | if (flags != 0) { |
| 1017 | error_ = EINVAL; |
| 1018 | return -1; |
| 1019 | } |
skvlad | c309e0e | 2016-07-28 17:15:20 -0700 | [diff] [blame] | 1020 | // If we don't think the connection is working yet, return ENOTCONN |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 1021 | // instead of sending a packet that will probably be dropped. |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1022 | if (!ReadyToSend(selected_connection_)) { |
skvlad | c309e0e | 2016-07-28 17:15:20 -0700 | [diff] [blame] | 1023 | error_ = ENOTCONN; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1024 | return -1; |
| 1025 | } |
| 1026 | |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 1027 | last_sent_packet_id_ = options.packet_id; |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1028 | int sent = selected_connection_->Send(data, len, options); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1029 | if (sent <= 0) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1030 | RTC_DCHECK(sent < 0); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1031 | error_ = selected_connection_->GetError(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1032 | } |
| 1033 | return sent; |
| 1034 | } |
| 1035 | |
| 1036 | bool P2PTransportChannel::GetStats(ConnectionInfos *infos) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1037 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1038 | // Gather connection infos. |
| 1039 | infos->clear(); |
| 1040 | |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 1041 | for (Connection* connection : connections_) { |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1042 | ConnectionInfo info = connection->stats(); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1043 | info.best_connection = (selected_connection_ == connection); |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 1044 | infos->push_back(std::move(info)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1045 | connection->set_reported(true); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | return true; |
| 1049 | } |
| 1050 | |
| 1051 | rtc::DiffServCodePoint P2PTransportChannel::DefaultDscpValue() const { |
| 1052 | OptionMap::const_iterator it = options_.find(rtc::Socket::OPT_DSCP); |
| 1053 | if (it == options_.end()) { |
| 1054 | return rtc::DSCP_NO_CHANGE; |
| 1055 | } |
| 1056 | return static_cast<rtc::DiffServCodePoint> (it->second); |
| 1057 | } |
| 1058 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1059 | // Monitor connection states. |
| 1060 | void P2PTransportChannel::UpdateConnectionStates() { |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 1061 | int64_t now = rtc::TimeMillis(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1062 | |
| 1063 | // We need to copy the list of connections since some may delete themselves |
| 1064 | // when we call UpdateState. |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1065 | for (Connection* c : connections_) { |
| 1066 | c->UpdateState(now); |
| 1067 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | // Prepare for best candidate sorting. |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1071 | void P2PTransportChannel::RequestSortAndStateUpdate() { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1072 | if (!sort_dirty_) { |
johan | 0fd22ef | 2016-09-29 01:19:20 -0700 | [diff] [blame] | 1073 | network_thread_->Post(RTC_FROM_HERE, this, MSG_SORT_AND_UPDATE_STATE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1074 | sort_dirty_ = true; |
| 1075 | } |
| 1076 | } |
| 1077 | |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1078 | void P2PTransportChannel::MaybeStartPinging() { |
| 1079 | if (started_pinging_) { |
| 1080 | return; |
| 1081 | } |
| 1082 | |
| 1083 | int64_t now = rtc::TimeMillis(); |
| 1084 | if (std::any_of( |
| 1085 | connections_.begin(), connections_.end(), |
| 1086 | [this, now](const Connection* c) { return IsPingable(c, now); })) { |
| 1087 | LOG_J(LS_INFO, this) << "Have a pingable connection for the first time; " |
| 1088 | << "starting to ping."; |
| 1089 | thread()->Post(RTC_FROM_HERE, this, MSG_CHECK_AND_PING); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1090 | thread()->PostDelayed(RTC_FROM_HERE, |
| 1091 | *config_.regather_on_failed_networks_interval, this, |
| 1092 | MSG_REGATHER_ON_FAILED_NETWORKS); |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 1093 | if (config_.regather_all_networks_interval_range) { |
| 1094 | thread()->PostDelayed(RTC_FROM_HERE, |
| 1095 | SampleRegatherAllNetworksInterval(), this, |
| 1096 | MSG_REGATHER_ON_ALL_NETWORKS); |
| 1097 | } |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1098 | started_pinging_ = true; |
| 1099 | } |
| 1100 | } |
| 1101 | |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1102 | // Compare two connections based on their writing, receiving, and connected |
| 1103 | // states. |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 1104 | int P2PTransportChannel::CompareConnectionStates( |
| 1105 | const Connection* a, |
| 1106 | const Connection* b, |
| 1107 | rtc::Optional<int64_t> receiving_unchanged_threshold, |
| 1108 | bool* missed_receiving_unchanged_threshold) const { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1109 | // First, prefer a connection that's writable or presumed writable over |
| 1110 | // one that's not writable. |
| 1111 | bool a_writable = a->writable() || PresumedWritable(a); |
| 1112 | bool b_writable = b->writable() || PresumedWritable(b); |
| 1113 | if (a_writable && !b_writable) { |
| 1114 | return a_is_better; |
| 1115 | } |
| 1116 | if (!a_writable && b_writable) { |
| 1117 | return b_is_better; |
| 1118 | } |
| 1119 | |
| 1120 | // Sort based on write-state. Better states have lower values. |
| 1121 | if (a->write_state() < b->write_state()) { |
| 1122 | return a_is_better; |
| 1123 | } |
| 1124 | if (b->write_state() < a->write_state()) { |
| 1125 | return b_is_better; |
| 1126 | } |
| 1127 | |
| 1128 | // We prefer a receiving connection to a non-receiving, higher-priority |
| 1129 | // connection when sorting connections and choosing which connection to |
| 1130 | // switch to. |
| 1131 | if (a->receiving() && !b->receiving()) { |
| 1132 | return a_is_better; |
| 1133 | } |
| 1134 | if (!a->receiving() && b->receiving()) { |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 1135 | if (!receiving_unchanged_threshold || |
| 1136 | (a->receiving_unchanged_since() <= *receiving_unchanged_threshold && |
| 1137 | b->receiving_unchanged_since() <= *receiving_unchanged_threshold)) { |
| 1138 | return b_is_better; |
| 1139 | } |
| 1140 | *missed_receiving_unchanged_threshold = true; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | // WARNING: Some complexity here about TCP reconnecting. |
| 1144 | // When a TCP connection fails because of a TCP socket disconnecting, the |
| 1145 | // active side of the connection will attempt to reconnect for 5 seconds while |
| 1146 | // pretending to be writable (the connection is not set to the unwritable |
| 1147 | // state). On the passive side, the connection also remains writable even |
| 1148 | // though it is disconnected, and a new connection is created when the active |
| 1149 | // side connects. At that point, there are two TCP connections on the passive |
| 1150 | // side: 1. the old, disconnected one that is pretending to be writable, and |
| 1151 | // 2. the new, connected one that is maybe not yet writable. For purposes of |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1152 | // pruning, pinging, and selecting the selected connection, we want to treat |
| 1153 | // the new connection as "better" than the old one. We could add a method |
| 1154 | // called something like Connection::ImReallyBadEvenThoughImWritable, but that |
| 1155 | // is equivalent to the existing Connection::connected(), which we already |
| 1156 | // have. So, in code throughout this file, we'll check whether the connection |
| 1157 | // is connected() or not, and if it is not, treat it as "worse" than a |
| 1158 | // connected one, even though it's writable. In the code below, we're doing |
| 1159 | // so to make sure we treat a new writable connection as better than an old |
| 1160 | // disconnected connection. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1161 | |
| 1162 | // In the case where we reconnect TCP connections, the original best |
| 1163 | // connection is disconnected without changing to WRITE_TIMEOUT. In this case, |
| 1164 | // the new connection, when it becomes writable, should have higher priority. |
| 1165 | if (a->write_state() == Connection::STATE_WRITABLE && |
| 1166 | b->write_state() == Connection::STATE_WRITABLE) { |
| 1167 | if (a->connected() && !b->connected()) { |
| 1168 | return a_is_better; |
| 1169 | } |
| 1170 | if (!a->connected() && b->connected()) { |
| 1171 | return b_is_better; |
| 1172 | } |
| 1173 | } |
| 1174 | return 0; |
| 1175 | } |
| 1176 | |
| 1177 | // Compares two connections based only on the candidate and network information. |
| 1178 | // Returns positive if |a| is better than |b|. |
| 1179 | int P2PTransportChannel::CompareConnectionCandidates( |
| 1180 | const Connection* a, |
| 1181 | const Connection* b) const { |
| 1182 | // Prefer lower network cost. |
| 1183 | uint32_t a_cost = a->ComputeNetworkCost(); |
| 1184 | uint32_t b_cost = b->ComputeNetworkCost(); |
| 1185 | // Smaller cost is better. |
| 1186 | if (a_cost < b_cost) { |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1187 | return a_is_better; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1188 | } |
| 1189 | if (a_cost > b_cost) { |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1190 | return b_is_better; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | // Compare connection priority. Lower values get sorted last. |
| 1194 | if (a->priority() > b->priority()) { |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1195 | return a_is_better; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1196 | } |
| 1197 | if (a->priority() < b->priority()) { |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1198 | return b_is_better; |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | // If we're still tied at this point, prefer a younger generation. |
| 1202 | // (Younger generation means a larger generation number). |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 1203 | int cmp = (a->remote_candidate().generation() + a->port()->generation()) - |
| 1204 | (b->remote_candidate().generation() + b->port()->generation()); |
| 1205 | if (cmp != 0) { |
| 1206 | return cmp; |
| 1207 | } |
| 1208 | |
| 1209 | // A periodic regather (triggered by the regather_all_networks_interval_range) |
| 1210 | // will produce candidates that appear the same but would use a new port. We |
| 1211 | // want to use the new candidates and purge the old candidates as they come |
| 1212 | // in, so use the fact that the old ports get pruned immediately to rank the |
| 1213 | // candidates with an active port/remote candidate higher. |
| 1214 | bool a_pruned = IsPortPruned(a->port()) || |
| 1215 | IsRemoteCandidatePruned(a->remote_candidate()); |
| 1216 | bool b_pruned = IsPortPruned(b->port()) || |
| 1217 | IsRemoteCandidatePruned(b->remote_candidate()); |
| 1218 | if (!a_pruned && b_pruned) { |
| 1219 | return a_is_better; |
| 1220 | } |
| 1221 | if (a_pruned && !b_pruned) { |
| 1222 | return b_is_better; |
| 1223 | } |
| 1224 | |
| 1225 | // Otherwise, must be equal |
| 1226 | return 0; |
| 1227 | } |
| 1228 | |
| 1229 | bool P2PTransportChannel::IsPortPruned(const Port* port) const { |
| 1230 | return std::find(ports_.begin(), ports_.end(), port) == ports_.end(); |
| 1231 | } |
| 1232 | |
| 1233 | bool P2PTransportChannel::IsRemoteCandidatePruned(const Candidate& cand) const { |
| 1234 | return std::find(remote_candidates_.begin(), remote_candidates_.end(), cand) |
| 1235 | == remote_candidates_.end(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 1238 | int P2PTransportChannel::CompareConnections( |
| 1239 | const Connection* a, |
| 1240 | const Connection* b, |
| 1241 | rtc::Optional<int64_t> receiving_unchanged_threshold, |
| 1242 | bool* missed_receiving_unchanged_threshold) const { |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1243 | RTC_CHECK(a != nullptr); |
| 1244 | RTC_CHECK(b != nullptr); |
| 1245 | |
| 1246 | // We prefer to switch to a writable and receiving connection over a |
| 1247 | // non-writable or non-receiving connection, even if the latter has |
| 1248 | // been nominated by the controlling side. |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 1249 | int state_cmp = CompareConnectionStates(a, b, receiving_unchanged_threshold, |
| 1250 | missed_receiving_unchanged_threshold); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1251 | if (state_cmp != 0) { |
| 1252 | return state_cmp; |
| 1253 | } |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1254 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1255 | if (ice_role_ == ICEROLE_CONTROLLED) { |
| 1256 | // Compare the connections based on the nomination states and the last data |
| 1257 | // received time if this is on the controlled side. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 1258 | if (a->remote_nomination() > b->remote_nomination()) { |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1259 | return a_is_better; |
| 1260 | } |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 1261 | if (a->remote_nomination() < b->remote_nomination()) { |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1262 | return b_is_better; |
| 1263 | } |
| 1264 | |
| 1265 | if (a->last_data_received() > b->last_data_received()) { |
| 1266 | return a_is_better; |
| 1267 | } |
| 1268 | if (a->last_data_received() < b->last_data_received()) { |
| 1269 | return b_is_better; |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | // Compare the network cost and priority. |
| 1274 | return CompareConnectionCandidates(a, b); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1275 | } |
| 1276 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1277 | bool P2PTransportChannel::PresumedWritable(const Connection* conn) const { |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1278 | return (conn->write_state() == Connection::STATE_WRITE_INIT && |
| 1279 | config_.presume_writable_when_fully_relayed && |
| 1280 | conn->local_candidate().type() == RELAY_PORT_TYPE && |
| 1281 | (conn->remote_candidate().type() == RELAY_PORT_TYPE || |
| 1282 | conn->remote_candidate().type() == PRFLX_PORT_TYPE)); |
| 1283 | } |
| 1284 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1285 | // Sort the available connections to find the best one. We also monitor |
| 1286 | // the number of available connections and the current state. |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1287 | void P2PTransportChannel::SortConnectionsAndUpdateState() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1288 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1289 | |
| 1290 | // Make sure the connection states are up-to-date since this affects how they |
| 1291 | // will be sorted. |
| 1292 | UpdateConnectionStates(); |
| 1293 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1294 | // Any changes after this point will require a re-sort. |
| 1295 | sort_dirty_ = false; |
| 1296 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1297 | // Find the best alternative connection by sorting. It is important to note |
| 1298 | // that amongst equal preference, writable connections, this will choose the |
| 1299 | // one whose estimated latency is lowest. So it is the only one that we |
| 1300 | // need to consider switching to. |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 1301 | // TODO(honghaiz): Don't sort; Just use std::max_element in the right places. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1302 | std::stable_sort(connections_.begin(), connections_.end(), |
| 1303 | [this](const Connection* a, const Connection* b) { |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 1304 | int cmp = CompareConnections( |
| 1305 | a, b, rtc::Optional<int64_t>(), nullptr); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1306 | if (cmp != 0) { |
| 1307 | return cmp > 0; |
| 1308 | } |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1309 | // Otherwise, sort based on latency estimate. |
| 1310 | return a->rtt() < b->rtt(); |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1311 | }); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1312 | |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 1313 | LOG(LS_VERBOSE) << "Sorting " << connections_.size() |
| 1314 | << " available connections:"; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1315 | for (size_t i = 0; i < connections_.size(); ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1316 | LOG(LS_VERBOSE) << connections_[i]->ToString(); |
| 1317 | } |
| 1318 | |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 1319 | Connection* top_connection = |
| 1320 | (connections_.size() > 0) ? connections_[0] : nullptr; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1321 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1322 | // If necessary, switch to the new choice. Note that |top_connection| doesn't |
| 1323 | // have to be writable to become the selected connection although it will |
| 1324 | // have higher priority if it is writable. |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 1325 | MaybeSwitchSelectedConnection(top_connection, "sorting"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1326 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1327 | // The controlled side can prune only if the selected connection has been |
| 1328 | // nominated because otherwise it may prune the connection that will be |
| 1329 | // selected by the controlling side. |
| 1330 | // TODO(honghaiz): This is not enough to prevent a connection from being |
| 1331 | // pruned too early because with aggressive nomination, the controlling side |
| 1332 | // will nominate every connection until it becomes writable. |
| 1333 | if (ice_role_ == ICEROLE_CONTROLLING || |
| 1334 | (selected_connection_ && selected_connection_->nominated())) { |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 1335 | PruneConnections(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | // Check if all connections are timedout. |
| 1339 | bool all_connections_timedout = true; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1340 | for (size_t i = 0; i < connections_.size(); ++i) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1341 | if (connections_[i]->write_state() != Connection::STATE_WRITE_TIMEOUT) { |
| 1342 | all_connections_timedout = false; |
| 1343 | break; |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | // Now update the writable state of the channel with the information we have |
| 1348 | // so far. |
honghaiz | a8e9f5e | 2015-11-11 16:15:07 -0800 | [diff] [blame] | 1349 | if (all_connections_timedout) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1350 | HandleAllTimedOut(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1353 | // Update the state of this channel. |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 1354 | UpdateState(); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1355 | |
| 1356 | // Also possibly start pinging. |
| 1357 | // We could start pinging if: |
| 1358 | // * The first connection was created. |
| 1359 | // * ICE credentials were provided. |
| 1360 | // * A TCP connection became connected. |
| 1361 | MaybeStartPinging(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 1364 | std::map<rtc::Network*, Connection*> |
| 1365 | P2PTransportChannel::GetBestConnectionByNetwork() const { |
| 1366 | // |connections_| has been sorted, so the first one in the list on a given |
| 1367 | // network is the best connection on the network, except that the selected |
| 1368 | // connection is always the best connection on the network. |
| 1369 | std::map<rtc::Network*, Connection*> best_connection_by_network; |
| 1370 | if (selected_connection_) { |
| 1371 | best_connection_by_network[selected_connection_->port()->Network()] = |
| 1372 | selected_connection_; |
| 1373 | } |
| 1374 | // TODO(honghaiz): Need to update this if |connections_| are not sorted. |
| 1375 | for (Connection* conn : connections_) { |
| 1376 | rtc::Network* network = conn->port()->Network(); |
| 1377 | // This only inserts when the network does not exist in the map. |
| 1378 | best_connection_by_network.insert(std::make_pair(network, conn)); |
| 1379 | } |
| 1380 | return best_connection_by_network; |
| 1381 | } |
| 1382 | |
| 1383 | std::vector<Connection*> |
| 1384 | P2PTransportChannel::GetBestWritableConnectionPerNetwork() const { |
| 1385 | std::vector<Connection*> connections; |
| 1386 | for (auto kv : GetBestConnectionByNetwork()) { |
| 1387 | Connection* conn = kv.second; |
| 1388 | if (conn->writable() && conn->connected()) { |
| 1389 | connections.push_back(conn); |
| 1390 | } |
| 1391 | } |
| 1392 | return connections; |
| 1393 | } |
| 1394 | |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 1395 | void P2PTransportChannel::PruneConnections() { |
sprang | 716978d | 2016-10-11 06:43:28 -0700 | [diff] [blame] | 1396 | // We can prune any connection for which there is a connected, writable |
| 1397 | // connection on the same network with better or equal priority. We leave |
| 1398 | // those with better priority just in case they become writable later (at |
| 1399 | // which point, we would prune out the current selected connection). We leave |
| 1400 | // connections on other networks because they may not be using the same |
| 1401 | // resources and they may represent very distinct paths over which we can |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 1402 | // switch. If |best_conn_on_network| is not connected, we may be reconnecting |
| 1403 | // a TCP connection and should not prune connections in this network. |
| 1404 | // See the big comment in CompareConnectionStates. |
deadbeef | 1ee2125 | 2017-06-13 15:49:45 -0700 | [diff] [blame] | 1405 | // |
| 1406 | // An exception is made for connections on an "any address" network, meaning |
| 1407 | // not bound to any specific network interface. We don't want to keep one of |
| 1408 | // these alive as a backup, since it could be using the same network |
| 1409 | // interface as the higher-priority, selected candidate pair. |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 1410 | auto best_connection_by_network = GetBestConnectionByNetwork(); |
| 1411 | for (Connection* conn : connections_) { |
deadbeef | 1ee2125 | 2017-06-13 15:49:45 -0700 | [diff] [blame] | 1412 | Connection* best_conn = selected_connection_; |
| 1413 | if (!rtc::IPIsAny(conn->port()->Network()->ip())) { |
| 1414 | // If the connection is bound to a specific network interface (not an |
| 1415 | // "any address" network), compare it against the best connection for |
| 1416 | // that network interface rather than the best connection overall. This |
| 1417 | // ensures that at least one connection per network will be left |
| 1418 | // unpruned. |
| 1419 | best_conn = best_connection_by_network[conn->port()->Network()]; |
| 1420 | } |
| 1421 | // Do not prune connections if the connection being compared against is |
| 1422 | // weak. Otherwise, it may delete connections prematurely. |
| 1423 | if (best_conn && conn != best_conn && !best_conn->weak() && |
| 1424 | CompareConnectionCandidates(best_conn, conn) >= 0) { |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 1425 | conn->Prune(); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 1426 | } |
| 1427 | } |
| 1428 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1429 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1430 | // Change the selected connection, and let listeners know. |
| 1431 | void P2PTransportChannel::SwitchSelectedConnection(Connection* conn) { |
| 1432 | // Note: if conn is NULL, the previous |selected_connection_| has been |
| 1433 | // destroyed, so don't use it. |
| 1434 | Connection* old_selected_connection = selected_connection_; |
| 1435 | selected_connection_ = conn; |
| 1436 | if (selected_connection_) { |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 1437 | ++nomination_; |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1438 | if (old_selected_connection) { |
| 1439 | LOG_J(LS_INFO, this) << "Previous selected connection: " |
| 1440 | << old_selected_connection->ToString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1441 | } |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1442 | LOG_J(LS_INFO, this) << "New selected connection: " |
| 1443 | << selected_connection_->ToString(); |
| 1444 | SignalRouteChange(this, selected_connection_->remote_candidate()); |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 1445 | // This is a temporary, but safe fix to webrtc issue 5705. |
skvlad | c309e0e | 2016-07-28 17:15:20 -0700 | [diff] [blame] | 1446 | // TODO(honghaiz): Make all ENOTCONN error routed through the transport |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 1447 | // channel so that it knows whether the media channel is allowed to |
| 1448 | // send; then it will only signal ready-to-send if the media channel |
| 1449 | // has been disallowed to send. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1450 | if (selected_connection_->writable() || |
| 1451 | PresumedWritable(selected_connection_)) { |
Honghai Zhang | 82f132c | 2016-03-30 12:55:14 -0700 | [diff] [blame] | 1452 | SignalReadyToSend(this); |
| 1453 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1454 | } else { |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1455 | LOG_J(LS_INFO, this) << "No selected connection"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1456 | } |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1457 | SignalSelectedCandidatePairChanged(this, selected_connection_, |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1458 | last_sent_packet_id_, |
| 1459 | ReadyToSend(selected_connection_)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 1462 | // Warning: UpdateState should eventually be called whenever a connection |
| 1463 | // is added, deleted, or the write state of any connection changes so that the |
| 1464 | // transport controller will get the up-to-date channel state. However it |
| 1465 | // should not be called too often; in the case that multiple connection states |
| 1466 | // change, it should be called after all the connection states have changed. For |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1467 | // example, we call this at the end of SortConnectionsAndUpdateState. |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 1468 | void P2PTransportChannel::UpdateState() { |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 1469 | IceTransportState state = ComputeState(); |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 1470 | if (state_ != state) { |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 1471 | LOG_J(LS_INFO, this) << "Transport channel state changed from " |
| 1472 | << static_cast<int>(state_) << " to " |
| 1473 | << static_cast<int>(state); |
Taylor Brandstetter | 6aefc63 | 2016-05-26 16:08:23 -0700 | [diff] [blame] | 1474 | // Check that the requested transition is allowed. Note that |
| 1475 | // P2PTransportChannel does not (yet) implement a direct mapping of the ICE |
| 1476 | // states from the standard; the difference is covered by |
| 1477 | // TransportController and PeerConnection. |
| 1478 | switch (state_) { |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 1479 | case IceTransportState::STATE_INIT: |
Taylor Brandstetter | 6aefc63 | 2016-05-26 16:08:23 -0700 | [diff] [blame] | 1480 | // TODO(deadbeef): Once we implement end-of-candidates signaling, |
| 1481 | // we shouldn't go from INIT to COMPLETED. |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 1482 | RTC_DCHECK(state == IceTransportState::STATE_CONNECTING || |
| 1483 | state == IceTransportState::STATE_COMPLETED); |
Taylor Brandstetter | 6aefc63 | 2016-05-26 16:08:23 -0700 | [diff] [blame] | 1484 | break; |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 1485 | case IceTransportState::STATE_CONNECTING: |
| 1486 | RTC_DCHECK(state == IceTransportState::STATE_COMPLETED || |
| 1487 | state == IceTransportState::STATE_FAILED); |
Taylor Brandstetter | 6aefc63 | 2016-05-26 16:08:23 -0700 | [diff] [blame] | 1488 | break; |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 1489 | case IceTransportState::STATE_COMPLETED: |
Taylor Brandstetter | 6aefc63 | 2016-05-26 16:08:23 -0700 | [diff] [blame] | 1490 | // TODO(deadbeef): Once we implement end-of-candidates signaling, |
| 1491 | // we shouldn't go from COMPLETED to CONNECTING. |
| 1492 | // Though we *can* go from COMPlETED to FAILED, if consent expires. |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 1493 | RTC_DCHECK(state == IceTransportState::STATE_CONNECTING || |
| 1494 | state == IceTransportState::STATE_FAILED); |
Taylor Brandstetter | 6aefc63 | 2016-05-26 16:08:23 -0700 | [diff] [blame] | 1495 | break; |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 1496 | case IceTransportState::STATE_FAILED: |
Taylor Brandstetter | 6aefc63 | 2016-05-26 16:08:23 -0700 | [diff] [blame] | 1497 | // TODO(deadbeef): Once we implement end-of-candidates signaling, |
| 1498 | // we shouldn't go from FAILED to CONNECTING or COMPLETED. |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 1499 | RTC_DCHECK(state == IceTransportState::STATE_CONNECTING || |
| 1500 | state == IceTransportState::STATE_COMPLETED); |
Taylor Brandstetter | 6aefc63 | 2016-05-26 16:08:23 -0700 | [diff] [blame] | 1501 | break; |
| 1502 | default: |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 1503 | RTC_NOTREACHED(); |
Taylor Brandstetter | 6aefc63 | 2016-05-26 16:08:23 -0700 | [diff] [blame] | 1504 | break; |
| 1505 | } |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 1506 | state_ = state; |
| 1507 | SignalStateChanged(this); |
| 1508 | } |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 1509 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1510 | // If our selected connection is "presumed writable" (TURN-TURN with no |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 1511 | // CreatePermission required), act like we're already writable to the upper |
| 1512 | // layers, so they can start media quicker. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1513 | bool writable = |
| 1514 | selected_connection_ && (selected_connection_->writable() || |
| 1515 | PresumedWritable(selected_connection_)); |
| 1516 | set_writable(writable); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1517 | |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 1518 | bool receiving = false; |
| 1519 | for (const Connection* connection : connections_) { |
| 1520 | if (connection->receiving()) { |
| 1521 | receiving = true; |
| 1522 | break; |
| 1523 | } |
| 1524 | } |
| 1525 | set_receiving(receiving); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 1528 | void P2PTransportChannel::MaybeStopPortAllocatorSessions() { |
| 1529 | if (!IsGettingPorts()) { |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 1530 | return; |
| 1531 | } |
| 1532 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 1533 | for (const auto& session : allocator_sessions_) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1534 | if (session->IsStopped()) { |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 1535 | continue; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1536 | } |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1537 | // If gathering continually, keep the last session running so that |
| 1538 | // it can gather candidates if the networks change. |
| 1539 | if (config_.gather_continually() && session == allocator_sessions_.back()) { |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 1540 | session->ClearGettingPorts(); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1541 | } else { |
| 1542 | session->StopGettingPorts(); |
honghaiz | 98db68f | 2015-09-29 07:58:17 -0700 | [diff] [blame] | 1543 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1544 | } |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 1545 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1546 | |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 1547 | // If all connections timed out, delete them all. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1548 | void P2PTransportChannel::HandleAllTimedOut() { |
honghaiz | 77d0d6e | 2015-10-27 11:34:45 -0700 | [diff] [blame] | 1549 | for (Connection* connection : connections_) { |
| 1550 | connection->Destroy(); |
| 1551 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 1554 | bool P2PTransportChannel::weak() const { |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1555 | return !selected_connection_ || selected_connection_->weak(); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 1556 | } |
| 1557 | |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1558 | bool P2PTransportChannel::ReadyToSend(Connection* connection) const { |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 1559 | // Note that we allow sending on an unreliable connection, because it's |
| 1560 | // possible that it became unreliable simply due to bad chance. |
| 1561 | // So this shouldn't prevent attempting to send media. |
Honghai Zhang | e05bcc2 | 2016-08-16 18:19:14 -0700 | [diff] [blame] | 1562 | return connection != nullptr && |
| 1563 | (connection->writable() || |
| 1564 | connection->write_state() == Connection::STATE_WRITE_UNRELIABLE || |
| 1565 | PresumedWritable(connection)); |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 1566 | } |
| 1567 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1568 | // Handle any queued up requests |
| 1569 | void P2PTransportChannel::OnMessage(rtc::Message *pmsg) { |
| 1570 | switch (pmsg->message_id) { |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1571 | case MSG_SORT_AND_UPDATE_STATE: |
| 1572 | SortConnectionsAndUpdateState(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1573 | break; |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 1574 | case MSG_CHECK_AND_PING: |
| 1575 | OnCheckAndPing(); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 1576 | break; |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1577 | case MSG_REGATHER_ON_FAILED_NETWORKS: |
| 1578 | OnRegatherOnFailedNetworks(); |
| 1579 | break; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 1580 | case MSG_REGATHER_ON_ALL_NETWORKS: |
| 1581 | OnRegatherOnAllNetworks(); |
| 1582 | break; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1583 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 1584 | RTC_NOTREACHED(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1585 | break; |
| 1586 | } |
| 1587 | } |
| 1588 | |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 1589 | // Handle queued up check-and-ping request |
| 1590 | void P2PTransportChannel::OnCheckAndPing() { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1591 | // Make sure the states of the connections are up-to-date (since this affects |
| 1592 | // which ones are pingable). |
| 1593 | UpdateConnectionStates(); |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1594 | // When the selected connection is not receiving or not writable, or any |
| 1595 | // active connection has not been pinged enough times, use the weak ping |
| 1596 | // interval. |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 1597 | bool need_more_pings_at_weak_interval = std::any_of( |
| 1598 | connections_.begin(), connections_.end(), [](Connection* conn) { |
| 1599 | return conn->active() && |
| 1600 | conn->num_pings_sent() < MIN_PINGS_AT_WEAK_PING_INTERVAL; |
| 1601 | }); |
| 1602 | int ping_interval = (weak() || need_more_pings_at_weak_interval) |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 1603 | ? weak_ping_interval() |
| 1604 | : strong_ping_interval(); |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 1605 | if (rtc::TimeMillis() >= last_ping_sent_ms_ + ping_interval) { |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 1606 | Connection* conn = FindNextPingableConnection(); |
| 1607 | if (conn) { |
| 1608 | PingConnection(conn); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 1609 | MarkConnectionPinged(conn); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 1610 | } |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 1611 | } |
Honghai Zhang | 049fbb1 | 2016-03-07 11:13:07 -0800 | [diff] [blame] | 1612 | int delay = std::min(ping_interval, check_receiving_interval_); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1613 | thread()->PostDelayed(RTC_FROM_HERE, delay, this, MSG_CHECK_AND_PING); |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 1614 | } |
| 1615 | |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 1616 | // A connection is considered a backup connection if the channel state |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1617 | // is completed, the connection is not the selected connection and it is active. |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1618 | bool P2PTransportChannel::IsBackupConnection(const Connection* conn) const { |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 1619 | return state_ == IceTransportState::STATE_COMPLETED && |
| 1620 | conn != selected_connection_ && conn->active(); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 1621 | } |
| 1622 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1623 | // Is the connection in a state for us to even consider pinging the other side? |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 1624 | // We consider a connection pingable even if it's not connected because that's |
| 1625 | // how a TCP connection is kicked into reconnecting on the active side. |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1626 | bool P2PTransportChannel::IsPingable(const Connection* conn, |
| 1627 | int64_t now) const { |
Peter Thatcher | 7351f46 | 2015-04-02 16:39:16 -0700 | [diff] [blame] | 1628 | const Candidate& remote = conn->remote_candidate(); |
| 1629 | // We should never get this far with an empty remote ufrag. |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1630 | RTC_DCHECK(!remote.username().empty()); |
Peter Thatcher | 7351f46 | 2015-04-02 16:39:16 -0700 | [diff] [blame] | 1631 | if (remote.username().empty() || remote.password().empty()) { |
| 1632 | // If we don't have an ICE ufrag and pwd, there's no way we can ping. |
| 1633 | return false; |
| 1634 | } |
| 1635 | |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 1636 | // A failed connection will not be pinged. |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 1637 | if (conn->state() == IceCandidatePairState::FAILED) { |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 1638 | return false; |
| 1639 | } |
| 1640 | |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 1641 | // An never connected connection cannot be written to at all, so pinging is |
| 1642 | // out of the question. However, if it has become WRITABLE, it is in the |
| 1643 | // reconnecting state so ping is needed. |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1644 | if (!conn->connected() && !conn->writable()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1645 | return false; |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 1646 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1647 | |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 1648 | // If the channel is weakly connected, ping all connections. |
| 1649 | if (weak()) { |
| 1650 | return true; |
| 1651 | } |
| 1652 | |
| 1653 | // Always ping active connections regardless whether the channel is completed |
| 1654 | // or not, but backup connections are pinged at a slower rate. |
| 1655 | if (IsBackupConnection(conn)) { |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 1656 | return conn->rtt_samples() == 0 || |
| 1657 | (now >= conn->last_ping_response_received() + |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 1658 | config_.backup_connection_ping_interval); |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 1659 | } |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 1660 | // Don't ping inactive non-backup connections. |
| 1661 | if (!conn->active()) { |
| 1662 | return false; |
| 1663 | } |
| 1664 | |
| 1665 | // Do ping unwritable, active connections. |
| 1666 | if (!conn->writable()) { |
| 1667 | return true; |
| 1668 | } |
| 1669 | |
| 1670 | // Ping writable, active connections if it's been long enough since the last |
| 1671 | // ping. |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 1672 | return WritableConnectionPastPingInterval(conn, now); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 1673 | } |
| 1674 | |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 1675 | bool P2PTransportChannel::WritableConnectionPastPingInterval( |
| 1676 | const Connection* conn, |
| 1677 | int64_t now) const { |
| 1678 | int interval = CalculateActiveWritablePingInterval(conn, now); |
| 1679 | return conn->last_ping_sent() + interval <= now; |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 1680 | } |
| 1681 | |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1682 | int P2PTransportChannel::CalculateActiveWritablePingInterval( |
| 1683 | const Connection* conn, |
| 1684 | int64_t now) const { |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 1685 | // Ping each connection at a higher rate at least |
| 1686 | // MIN_PINGS_AT_WEAK_PING_INTERVAL times. |
| 1687 | if (conn->num_pings_sent() < MIN_PINGS_AT_WEAK_PING_INTERVAL) { |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 1688 | return weak_ping_interval(); |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | int stable_interval = config_.stable_writable_connection_ping_interval; |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 1692 | int weak_or_stablizing_interval = std::min( |
| 1693 | stable_interval, WEAK_OR_STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL); |
| 1694 | // If the channel is weak or the connection is not stable yet, use the |
| 1695 | // weak_or_stablizing_interval. |
| 1696 | return (!weak() && conn->stable(now)) ? stable_interval |
| 1697 | : weak_or_stablizing_interval; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 1700 | // Returns the next pingable connection to ping. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1701 | Connection* P2PTransportChannel::FindNextPingableConnection() { |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 1702 | int64_t now = rtc::TimeMillis(); |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 1703 | |
| 1704 | // Rule 1: Selected connection takes priority over non-selected ones. |
| 1705 | if (selected_connection_ && selected_connection_->connected() && |
| 1706 | selected_connection_->writable() && |
| 1707 | WritableConnectionPastPingInterval(selected_connection_, now)) { |
| 1708 | return selected_connection_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1709 | } |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 1710 | |
| 1711 | // Rule 2: If the channel is weak, we need to find a new writable and |
| 1712 | // receiving connection, probably on a different network. If there are lots of |
| 1713 | // connections, it may take several seconds between two pings for every |
| 1714 | // non-selected connection. This will cause the receiving state of those |
| 1715 | // connections to be false, and thus they won't be selected. This is |
| 1716 | // problematic for network fail-over. We want to make sure at least one |
| 1717 | // connection per network is pinged frequently enough in order for it to be |
| 1718 | // selectable. So we prioritize one connection per network. |
| 1719 | // Rule 2.1: Among such connections, pick the one with the earliest |
| 1720 | // last-ping-sent time. |
| 1721 | if (weak()) { |
| 1722 | auto selectable_connections = GetBestWritableConnectionPerNetwork(); |
| 1723 | std::vector<Connection*> pingable_selectable_connections; |
| 1724 | std::copy_if(selectable_connections.begin(), selectable_connections.end(), |
| 1725 | std::back_inserter(pingable_selectable_connections), |
| 1726 | [this, now](Connection* conn) { |
| 1727 | return WritableConnectionPastPingInterval(conn, now); |
| 1728 | }); |
| 1729 | auto iter = std::min_element(pingable_selectable_connections.begin(), |
| 1730 | pingable_selectable_connections.end(), |
| 1731 | [](Connection* conn1, Connection* conn2) { |
| 1732 | return conn1->last_ping_sent() < |
| 1733 | conn2->last_ping_sent(); |
| 1734 | }); |
| 1735 | if (iter != pingable_selectable_connections.end()) { |
| 1736 | return *iter; |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | // Rule 3: Triggered checks have priority over non-triggered connections. |
| 1741 | // Rule 3.1: Among triggered checks, oldest takes precedence. |
| 1742 | Connection* oldest_triggered_check = |
| 1743 | FindOldestConnectionNeedingTriggeredCheck(now); |
| 1744 | if (oldest_triggered_check) { |
| 1745 | return oldest_triggered_check; |
| 1746 | } |
| 1747 | |
| 1748 | // Rule 4: Unpinged connections have priority over pinged ones. |
| 1749 | RTC_CHECK(connections_.size() == |
| 1750 | pinged_connections_.size() + unpinged_connections_.size()); |
| 1751 | // If there are unpinged and pingable connections, only ping those. |
| 1752 | // Otherwise, treat everything as unpinged. |
| 1753 | // TODO(honghaiz): Instead of adding two separate vectors, we can add a state |
| 1754 | // "pinged" to filter out unpinged connections. |
| 1755 | if (std::find_if(unpinged_connections_.begin(), unpinged_connections_.end(), |
| 1756 | [this, now](Connection* conn) { |
| 1757 | return this->IsPingable(conn, now); |
| 1758 | }) == unpinged_connections_.end()) { |
| 1759 | unpinged_connections_.insert(pinged_connections_.begin(), |
| 1760 | pinged_connections_.end()); |
| 1761 | pinged_connections_.clear(); |
| 1762 | } |
| 1763 | |
| 1764 | // Among un-pinged pingable connections, "more pingable" takes precedence. |
| 1765 | std::vector<Connection*> pingable_connections; |
| 1766 | std::copy_if(unpinged_connections_.begin(), unpinged_connections_.end(), |
| 1767 | std::back_inserter(pingable_connections), |
| 1768 | [this, now](Connection* conn) { return IsPingable(conn, now); }); |
| 1769 | auto iter = |
| 1770 | std::max_element(pingable_connections.begin(), pingable_connections.end(), |
solenberg | e08b253 | 2017-01-17 06:18:10 -0800 | [diff] [blame] | 1771 | [this](Connection* conn1, Connection* conn2) { |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 1772 | return MorePingable(conn1, conn2) == conn2; |
| 1773 | }); |
| 1774 | if (iter != pingable_connections.end()) { |
| 1775 | return *iter; |
| 1776 | } |
| 1777 | return nullptr; |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 1778 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1779 | |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 1780 | void P2PTransportChannel::MarkConnectionPinged(Connection* conn) { |
| 1781 | if (conn && pinged_connections_.insert(conn).second) { |
| 1782 | unpinged_connections_.erase(conn); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1783 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | // Apart from sending ping from |conn| this method also updates |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 1787 | // |use_candidate_attr| and |nomination| flags. One of the flags is set to |
| 1788 | // nominate |conn| if this channel is in CONTROLLING. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1789 | void P2PTransportChannel::PingConnection(Connection* conn) { |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 1790 | bool use_candidate_attr = false; |
| 1791 | uint32_t nomination = 0; |
| 1792 | if (ice_role_ == ICEROLE_CONTROLLING) { |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1793 | bool renomination_supported = ice_parameters_.renomination && |
| 1794 | !remote_ice_parameters_.empty() && |
| 1795 | remote_ice_parameters_.back().renomination; |
| 1796 | if (renomination_supported) { |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 1797 | nomination = GetNominationAttr(conn); |
| 1798 | } else { |
| 1799 | use_candidate_attr = |
| 1800 | GetUseCandidateAttr(conn, config_.default_nomination_mode); |
| 1801 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1802 | } |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 1803 | conn->set_nomination(nomination); |
| 1804 | conn->set_use_candidate_attr(use_candidate_attr); |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 1805 | last_ping_sent_ms_ = rtc::TimeMillis(); |
honghaiz | a58ea78 | 2015-09-24 08:13:36 -0700 | [diff] [blame] | 1806 | conn->Ping(last_ping_sent_ms_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 1809 | uint32_t P2PTransportChannel::GetNominationAttr(Connection* conn) const { |
| 1810 | return (conn == selected_connection_) ? nomination_ : 0; |
| 1811 | } |
| 1812 | |
| 1813 | // Nominate a connection based on the NominationMode. |
| 1814 | bool P2PTransportChannel::GetUseCandidateAttr(Connection* conn, |
| 1815 | NominationMode mode) const { |
| 1816 | switch (mode) { |
| 1817 | case NominationMode::REGULAR: |
| 1818 | // TODO(honghaiz): Implement regular nomination. |
| 1819 | return false; |
| 1820 | case NominationMode::AGGRESSIVE: |
| 1821 | if (remote_ice_mode_ == ICEMODE_LITE) { |
| 1822 | return GetUseCandidateAttr(conn, NominationMode::REGULAR); |
| 1823 | } |
| 1824 | return true; |
| 1825 | case NominationMode::SEMI_AGGRESSIVE: { |
| 1826 | // Nominate if |
| 1827 | // a) Remote is in FULL ICE AND |
| 1828 | // a.1) |conn| is the selected connection OR |
| 1829 | // a.2) there is no selected connection OR |
| 1830 | // a.3) the selected connection is unwritable OR |
| 1831 | // a.4) |conn| has higher priority than selected_connection. |
| 1832 | // b) Remote is in LITE ICE AND |
| 1833 | // b.1) |conn| is the selected_connection AND |
| 1834 | // b.2) |conn| is writable. |
| 1835 | bool selected = conn == selected_connection_; |
| 1836 | if (remote_ice_mode_ == ICEMODE_LITE) { |
| 1837 | return selected && conn->writable(); |
| 1838 | } |
| 1839 | bool better_than_selected = |
| 1840 | !selected_connection_ || !selected_connection_->writable() || |
| 1841 | CompareConnectionCandidates(selected_connection_, conn) < 0; |
| 1842 | return selected || better_than_selected; |
| 1843 | } |
| 1844 | default: |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 1845 | RTC_NOTREACHED(); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 1846 | return false; |
| 1847 | } |
| 1848 | } |
| 1849 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1850 | // When a connection's state changes, we need to figure out who to use as |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1851 | // the selected connection again. It could have become usable, or become |
| 1852 | // unusable. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1853 | void P2PTransportChannel::OnConnectionStateChange(Connection* connection) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1854 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1855 | |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 1856 | // May stop the allocator session when at least one connection becomes |
Honghai Zhang | 5a24637 | 2016-05-02 17:28:35 -0700 | [diff] [blame] | 1857 | // strongly connected after starting to get ports and the local candidate of |
| 1858 | // the connection is at the latest generation. It is not enough to check |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 1859 | // that the connection becomes weakly connected because the connection may be |
| 1860 | // changing from (writable, receiving) to (writable, not receiving). |
Honghai Zhang | 5a24637 | 2016-05-02 17:28:35 -0700 | [diff] [blame] | 1861 | bool strongly_connected = !connection->weak(); |
| 1862 | bool latest_generation = connection->local_candidate().generation() >= |
| 1863 | allocator_session()->generation(); |
| 1864 | if (strongly_connected && latest_generation) { |
honghaiz | 9b66957 | 2015-11-04 12:07:44 -0800 | [diff] [blame] | 1865 | MaybeStopPortAllocatorSessions(); |
| 1866 | } |
| 1867 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1868 | // We have to unroll the stack before doing this because we may be changing |
| 1869 | // the state of connections while sorting. |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1870 | RequestSortAndStateUpdate(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | // When a connection is removed, edit it out, and then update our best |
| 1874 | // connection. |
| 1875 | void P2PTransportChannel::OnConnectionDestroyed(Connection* connection) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1876 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1877 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1878 | // Note: the previous selected_connection_ may be destroyed by now, so don't |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1879 | // use it. |
| 1880 | |
| 1881 | // Remove this connection from the list. |
| 1882 | std::vector<Connection*>::iterator iter = |
| 1883 | std::find(connections_.begin(), connections_.end(), connection); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1884 | RTC_DCHECK(iter != connections_.end()); |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 1885 | pinged_connections_.erase(*iter); |
| 1886 | unpinged_connections_.erase(*iter); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1887 | connections_.erase(iter); |
| 1888 | |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 1889 | LOG_J(LS_INFO, this) << "Removed connection " << std::hex << connection |
| 1890 | << std::dec << " (" << connections_.size() |
| 1891 | << " remaining)"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1892 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1893 | // If this is currently the selected connection, then we need to pick a new |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1894 | // one. The call to SortConnectionsAndUpdateState will pick a new one. It |
| 1895 | // looks at the current selected connection in order to avoid switching |
| 1896 | // between fairly similar ones. Since this connection is no longer an option, |
| 1897 | // we can just set selected to nullptr and re-choose a best assuming that |
| 1898 | // there was no selected connection. |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1899 | if (selected_connection_ == connection) { |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 1900 | LOG(LS_INFO) << "Selected connection destroyed. Will choose a new one."; |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 1901 | SwitchSelectedConnection(nullptr); |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1902 | RequestSortAndStateUpdate(); |
| 1903 | } else { |
| 1904 | // If a non-selected connection was destroyed, we don't need to re-sort but |
| 1905 | // we do need to update state, because we could be switching to "failed" or |
| 1906 | // "completed". |
| 1907 | UpdateState(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1908 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1911 | // When a port is destroyed, remove it from our list of ports to use for |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1912 | // connection attempts. |
| 1913 | void P2PTransportChannel::OnPortDestroyed(PortInterface* port) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1914 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1915 | |
deadbeef | dfc4244 | 2016-06-21 14:19:48 -0700 | [diff] [blame] | 1916 | ports_.erase(std::remove(ports_.begin(), ports_.end(), port), ports_.end()); |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 1917 | pruned_ports_.erase( |
| 1918 | std::remove(pruned_ports_.begin(), pruned_ports_.end(), port), |
| 1919 | pruned_ports_.end()); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1920 | LOG(INFO) << "Removed port because it is destroyed: " << ports_.size() |
| 1921 | << " remaining"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 1924 | void P2PTransportChannel::OnPortsPruned( |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1925 | PortAllocatorSession* session, |
| 1926 | const std::vector<PortInterface*>& ports) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1927 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1928 | for (PortInterface* port : ports) { |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 1929 | if (PrunePort(port)) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1930 | LOG(INFO) << "Removed port: " << port->ToString() << " " << ports_.size() |
| 1931 | << " remaining"; |
| 1932 | } |
| 1933 | } |
| 1934 | } |
| 1935 | |
| 1936 | void P2PTransportChannel::OnCandidatesRemoved( |
| 1937 | PortAllocatorSession* session, |
| 1938 | const std::vector<Candidate>& candidates) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1939 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1940 | // Do not signal candidate removals if continual gathering is not enabled, or |
| 1941 | // if this is not the last session because an ICE restart would have signaled |
| 1942 | // the remote side to remove all candidates in previous sessions. |
| 1943 | if (!config_.gather_continually() || session != allocator_session()) { |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 1944 | return; |
| 1945 | } |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1946 | |
| 1947 | std::vector<Candidate> candidates_to_remove; |
| 1948 | for (Candidate candidate : candidates) { |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1949 | candidate.set_transport_name(transport_name()); |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1950 | candidates_to_remove.push_back(candidate); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 1951 | } |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 1952 | SignalCandidatesRemoved(this, candidates_to_remove); |
| 1953 | } |
| 1954 | |
| 1955 | void P2PTransportChannel::OnRegatherOnFailedNetworks() { |
| 1956 | // Only re-gather when the current session is in the CLEARED state (i.e., not |
| 1957 | // running or stopped). It is only possible to enter this state when we gather |
| 1958 | // continually, so there is an implicit check on continual gathering here. |
| 1959 | if (!allocator_sessions_.empty() && allocator_session()->IsCleared()) { |
| 1960 | allocator_session()->RegatherOnFailedNetworks(); |
| 1961 | } |
| 1962 | |
| 1963 | thread()->PostDelayed(RTC_FROM_HERE, |
| 1964 | *config_.regather_on_failed_networks_interval, this, |
| 1965 | MSG_REGATHER_ON_FAILED_NETWORKS); |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 1966 | } |
| 1967 | |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 1968 | void P2PTransportChannel::OnRegatherOnAllNetworks() { |
| 1969 | if (!allocator_sessions_.empty() && allocator_session()->IsCleared()) { |
| 1970 | allocator_session()->RegatherOnAllNetworks(); |
| 1971 | } |
| 1972 | |
| 1973 | thread()->PostDelayed(RTC_FROM_HERE, |
| 1974 | SampleRegatherAllNetworksInterval(), this, |
| 1975 | MSG_REGATHER_ON_ALL_NETWORKS); |
| 1976 | } |
| 1977 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 1978 | void P2PTransportChannel::PruneAllPorts() { |
| 1979 | pruned_ports_.insert(pruned_ports_.end(), ports_.begin(), ports_.end()); |
| 1980 | ports_.clear(); |
| 1981 | } |
| 1982 | |
| 1983 | bool P2PTransportChannel::PrunePort(PortInterface* port) { |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 1984 | auto it = std::find(ports_.begin(), ports_.end(), port); |
| 1985 | // Don't need to do anything if the port has been deleted from the port list. |
| 1986 | if (it == ports_.end()) { |
| 1987 | return false; |
| 1988 | } |
| 1989 | ports_.erase(it); |
Honghai Zhang | 8eeecab | 2016-07-28 13:20:15 -0700 | [diff] [blame] | 1990 | pruned_ports_.push_back(port); |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 1991 | return true; |
| 1992 | } |
| 1993 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1994 | // We data is available, let listeners know |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1995 | void P2PTransportChannel::OnReadPacket(Connection* connection, |
| 1996 | const char* data, |
| 1997 | size_t len, |
| 1998 | const rtc::PacketTime& packet_time) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1999 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2000 | |
| 2001 | // Do not deliver, if packet doesn't belong to the correct transport channel. |
| 2002 | if (!FindConnection(connection)) |
| 2003 | return; |
| 2004 | |
| 2005 | // Let the client know of an incoming packet |
| 2006 | SignalReadPacket(this, data, len, packet_time, 0); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 2007 | |
| 2008 | // May need to switch the sending connection based on the receiving media path |
| 2009 | // if this is the controlled side. |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 2010 | if (ice_role_ == ICEROLE_CONTROLLED) { |
| 2011 | MaybeSwitchSelectedConnection(connection, "data received"); |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 2012 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 2015 | void P2PTransportChannel::OnSentPacket(const rtc::SentPacket& sent_packet) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 2016 | RTC_DCHECK(network_thread_ == rtc::Thread::Current()); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 2017 | |
| 2018 | SignalSentPacket(this, sent_packet); |
| 2019 | } |
| 2020 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2021 | void P2PTransportChannel::OnReadyToSend(Connection* connection) { |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 2022 | if (connection == selected_connection_ && writable()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2023 | SignalReadyToSend(this); |
| 2024 | } |
| 2025 | } |
| 2026 | |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 2027 | // Find "triggered checks". We ping first those connections that have |
| 2028 | // received a ping but have not sent a ping since receiving it |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 2029 | // (last_ping_received > last_ping_sent). But we shouldn't do |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 2030 | // triggered checks if the connection is already writable. |
| 2031 | Connection* P2PTransportChannel::FindOldestConnectionNeedingTriggeredCheck( |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 2032 | int64_t now) { |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 2033 | Connection* oldest_needing_triggered_check = nullptr; |
| 2034 | for (auto conn : connections_) { |
| 2035 | if (!IsPingable(conn, now)) { |
| 2036 | continue; |
| 2037 | } |
| 2038 | bool needs_triggered_check = |
| 2039 | (!conn->writable() && |
| 2040 | conn->last_ping_received() > conn->last_ping_sent()); |
| 2041 | if (needs_triggered_check && |
| 2042 | (!oldest_needing_triggered_check || |
| 2043 | (conn->last_ping_received() < |
| 2044 | oldest_needing_triggered_check->last_ping_received()))) { |
| 2045 | oldest_needing_triggered_check = conn; |
| 2046 | } |
| 2047 | } |
| 2048 | |
| 2049 | if (oldest_needing_triggered_check) { |
| 2050 | LOG(LS_INFO) << "Selecting connection for triggered check: " |
| 2051 | << oldest_needing_triggered_check->ToString(); |
| 2052 | } |
| 2053 | return oldest_needing_triggered_check; |
| 2054 | } |
| 2055 | |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 2056 | Connection* P2PTransportChannel::MostLikelyToWork(Connection* conn1, |
| 2057 | Connection* conn2) { |
| 2058 | bool rr1 = IsRelayRelay(conn1); |
| 2059 | bool rr2 = IsRelayRelay(conn2); |
| 2060 | if (rr1 && !rr2) { |
| 2061 | return conn1; |
| 2062 | } else if (rr2 && !rr1) { |
| 2063 | return conn2; |
| 2064 | } else if (rr1 && rr2) { |
| 2065 | bool udp1 = IsUdp(conn1); |
| 2066 | bool udp2 = IsUdp(conn2); |
| 2067 | if (udp1 && !udp2) { |
| 2068 | return conn1; |
| 2069 | } else if (udp2 && udp1) { |
| 2070 | return conn2; |
| 2071 | } |
| 2072 | } |
| 2073 | return nullptr; |
| 2074 | } |
| 2075 | |
| 2076 | Connection* P2PTransportChannel::LeastRecentlyPinged(Connection* conn1, |
| 2077 | Connection* conn2) { |
| 2078 | if (conn1->last_ping_sent() < conn2->last_ping_sent()) { |
| 2079 | return conn1; |
| 2080 | } |
| 2081 | if (conn1->last_ping_sent() > conn2->last_ping_sent()) { |
| 2082 | return conn2; |
| 2083 | } |
| 2084 | return nullptr; |
| 2085 | } |
| 2086 | |
honghaiz | 7252a00 | 2016-11-08 20:04:09 -0800 | [diff] [blame] | 2087 | Connection* P2PTransportChannel::MorePingable(Connection* conn1, |
| 2088 | Connection* conn2) { |
guoweis | 36f0137 | 2016-03-02 18:02:40 -0800 | [diff] [blame] | 2089 | RTC_DCHECK(conn1 != conn2); |
| 2090 | if (config_.prioritize_most_likely_candidate_pairs) { |
| 2091 | Connection* most_likely_to_work_conn = MostLikelyToWork(conn1, conn2); |
| 2092 | if (most_likely_to_work_conn) { |
| 2093 | return most_likely_to_work_conn; |
| 2094 | } |
| 2095 | } |
| 2096 | |
| 2097 | Connection* least_recently_pinged_conn = LeastRecentlyPinged(conn1, conn2); |
| 2098 | if (least_recently_pinged_conn) { |
| 2099 | return least_recently_pinged_conn; |
| 2100 | } |
| 2101 | |
| 2102 | // During the initial state when nothing has been pinged yet, return the first |
| 2103 | // one in the ordered |connections_|. |
| 2104 | return *(std::find_if(connections_.begin(), connections_.end(), |
| 2105 | [conn1, conn2](Connection* conn) { |
| 2106 | return conn == conn1 || conn == conn2; |
| 2107 | })); |
| 2108 | } |
| 2109 | |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 2110 | void P2PTransportChannel::set_writable(bool writable) { |
| 2111 | if (writable_ == writable) { |
| 2112 | return; |
| 2113 | } |
| 2114 | LOG_J(LS_VERBOSE, this) << "set_writable from:" << writable_ << " to " |
| 2115 | << writable; |
| 2116 | writable_ = writable; |
| 2117 | if (writable_) { |
| 2118 | SignalReadyToSend(this); |
| 2119 | } |
| 2120 | SignalWritableState(this); |
| 2121 | } |
| 2122 | |
| 2123 | void P2PTransportChannel::set_receiving(bool receiving) { |
| 2124 | if (receiving_ == receiving) { |
| 2125 | return; |
| 2126 | } |
| 2127 | receiving_ = receiving; |
| 2128 | SignalReceivingState(this); |
| 2129 | } |
| 2130 | |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 2131 | int P2PTransportChannel::SampleRegatherAllNetworksInterval() { |
| 2132 | auto interval = config_.regather_all_networks_interval_range; |
| 2133 | RTC_DCHECK(interval); |
| 2134 | return rand_.Rand(interval->min(), interval->max()); |
| 2135 | } |
| 2136 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 2137 | } // namespace cricket |