blob: 84d28795ddabd239387be6a22ba1e1bea11c562a [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
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// P2PTransportChannel wraps up the state management of the connection between
12// two P2P clients. Clients have candidate ports for connecting, and
13// connections which are combinations of candidates from each end (Alice and
14// Bob each have candidates, one candidate from Alice and one candidate from
15// Bob are used to make a connection, repeat to make many connections).
16//
17// When all of the available connections become invalid (non-writable), we
18// kick off a process of determining more candidates and more connections.
19//
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#ifndef P2P_BASE_P2PTRANSPORTCHANNEL_H_
21#define P2P_BASE_P2PTRANSPORTCHANNEL_H_
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000022
Qingsi Wange6826d22018-03-08 14:55:14 -080023#include <algorithm>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000024#include <map>
kwiberg3ec46792016-04-27 07:22:53 -070025#include <memory>
guoweis36f01372016-03-02 18:02:40 -080026#include <set>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000027#include <string>
28#include <vector>
kwiberg4485ffb2016-04-26 08:14:39 -070029
Zach Steine20867f2018-08-02 13:20:15 -070030#include "api/asyncresolverfactory.h"
Patrik Höglunde2d6a062017-10-05 14:53:33 +020031#include "api/candidate.h"
Qingsi Wangdea68892018-03-27 10:55:21 -070032#include "api/rtcerror.h"
Qingsi Wang93a84392018-01-30 17:13:09 -080033#include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair_config.h"
34#include "logging/rtc_event_log/icelogger.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "p2p/base/candidatepairinterface.h"
36#include "p2p/base/icetransportinternal.h"
Qingsi Wang866e08d2018-03-22 17:54:23 -070037#include "p2p/base/p2pconstants.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "p2p/base/portallocator.h"
39#include "p2p/base/portinterface.h"
Qingsi Wang1b368942018-06-13 13:54:08 -070040#include "p2p/base/regatheringcontroller.h"
Qingsi Wang502db3d2018-05-16 17:01:37 -070041#include "rtc_base/asyncinvoker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "rtc_base/asyncpacketsocket.h"
43#include "rtc_base/constructormagic.h"
Jonas Olsson941a07c2018-09-13 10:07:07 +020044#include "rtc_base/strings/string_builder.h"
Artem Titove41c4332018-07-25 15:04:28 +020045#include "rtc_base/third_party/sigslot/sigslot.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000046
Qingsi Wang93a84392018-01-30 17:13:09 -080047namespace webrtc {
48class RtcEventLog;
49} // namespace webrtc
50
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000051namespace cricket {
52
Honghai Zhangd93f50c2016-10-05 11:47:22 -070053// Enum for UMA metrics, used to record whether the channel is
54// connected/connecting/disconnected when ICE restart happens.
55enum class IceRestartState { CONNECTING, CONNECTED, DISCONNECTED, MAX_VALUE };
56
honghaiz524ecc22016-05-25 12:48:31 -070057static const int MIN_PINGS_AT_WEAK_PING_INTERVAL = 3;
guoweisb0bb77f2015-10-26 15:10:01 -070058
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080059bool IceCredentialsChanged(const std::string& old_ufrag,
60 const std::string& old_pwd,
61 const std::string& new_ufrag,
62 const std::string& new_pwd);
63
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000064// Adds the port on which the candidate originated.
65class RemoteCandidate : public Candidate {
66 public:
67 RemoteCandidate(const Candidate& c, PortInterface* origin_port)
68 : Candidate(c), origin_port_(origin_port) {}
69
70 PortInterface* origin_port() { return origin_port_; }
71
72 private:
73 PortInterface* origin_port_;
74};
75
76// P2PTransportChannel manages the candidates and connection process to keep
77// two P2P clients connected to each other.
Qingsi Wang502db3d2018-05-16 17:01:37 -070078class P2PTransportChannel : public IceTransportInternal {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000079 public:
Zach Steine20867f2018-08-02 13:20:15 -070080 // For testing only.
81 // TODO(zstein): Remove once AsyncResolverFactory is required.
82 P2PTransportChannel(const std::string& transport_name,
83 int component,
84 PortAllocator* allocator);
deadbeefcbecd352015-09-23 11:50:27 -070085 P2PTransportChannel(const std::string& transport_name,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000086 int component,
Qingsi Wang93a84392018-01-30 17:13:09 -080087 PortAllocator* allocator,
Zach Steine20867f2018-08-02 13:20:15 -070088 webrtc::AsyncResolverFactory* async_resolver_factory,
Qingsi Wang93a84392018-01-30 17:13:09 -080089 webrtc::RtcEventLog* event_log = nullptr);
Steve Anton33f69db2017-10-30 10:01:15 -070090 ~P2PTransportChannel() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000091
92 // From TransportChannelImpl:
zhihuangd06adf62017-01-12 15:58:31 -080093 IceTransportState GetState() const override;
Jonas Olsson81125f02018-10-09 10:52:04 +020094 webrtc::IceTransportState GetIceTransportState() const override;
95
Steve Anton33f69db2017-10-30 10:01:15 -070096 const std::string& transport_name() const override;
97 int component() const override;
98 bool writable() const override;
99 bool receiving() const override;
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200100 void SetIceRole(IceRole role) override;
Steve Anton33f69db2017-10-30 10:01:15 -0700101 IceRole GetIceRole() const override;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200102 void SetIceTiebreaker(uint64_t tiebreaker) override;
Honghai Zhang4cedf2b2016-08-31 08:18:11 -0700103 void SetIceParameters(const IceParameters& ice_params) override;
104 void SetRemoteIceParameters(const IceParameters& ice_params) override;
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200105 void SetRemoteIceMode(IceMode mode) override;
deadbeef886815b2016-06-29 15:21:04 -0700106 // TODO(deadbeef): Deprecated. Remove when Chromium's
107 // IceTransportChannel does not depend on this.
108 void Connect() {}
deadbeefcbecd352015-09-23 11:50:27 -0700109 void MaybeStartGathering() override;
Steve Anton33f69db2017-10-30 10:01:15 -0700110 IceGatheringState gathering_state() const override;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700111 void ResolveHostnameCandidate(const Candidate& candidate);
deadbeefcbecd352015-09-23 11:50:27 -0700112 void AddRemoteCandidate(const Candidate& candidate) override;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700113 void RemoveRemoteCandidate(const Candidate& candidate) override;
Honghai Zhang049fbb12016-03-07 11:13:07 -0800114 // Sets the parameters in IceConfig. We do not set them blindly. Instead, we
115 // only update the parameter if it is considered set in |config|. For example,
116 // a negative value of receiving_timeout will be considered "not set" and we
117 // will not use it to update the respective parameter in |config_|.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200118 // TODO(deadbeef): Use absl::optional instead of negative values.
honghaiz1f429e32015-09-28 07:57:34 -0700119 void SetIceConfig(const IceConfig& config) override;
guoweis36f01372016-03-02 18:02:40 -0800120 const IceConfig& config() const;
Qingsi Wangdea68892018-03-27 10:55:21 -0700121 static webrtc::RTCError ValidateIceConfig(const IceConfig& config);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000122
123 // From TransportChannel:
deadbeefcbecd352015-09-23 11:50:27 -0700124 int SendPacket(const char* data,
125 size_t len,
126 const rtc::PacketOptions& options,
127 int flags) override;
Henrik Boströmf3ecdb92015-09-08 12:11:54 +0200128 int SetOption(rtc::Socket::Option opt, int value) override;
129 bool GetOption(rtc::Socket::Option opt, int* value) override;
Steve Anton33f69db2017-10-30 10:01:15 -0700130 int GetError() override;
Qingsi Wang72a43a12018-02-20 16:03:18 -0800131 bool GetStats(std::vector<ConnectionInfo>* candidate_pair_stats_list,
132 std::vector<CandidateStats>* candidate_stats_list) override;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200133 absl::optional<int> GetRttEstimate() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000134
Honghai Zhang8cd7f222016-06-23 13:44:34 -0700135 // TODO(honghaiz): Remove this method once the reference of it in
136 // Chromoting is removed.
137 const Connection* best_connection() const { return selected_connection_; }
138
Honghai Zhang572b0942016-06-23 12:26:57 -0700139 const Connection* selected_connection() const { return selected_connection_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000140 void set_incoming_only(bool value) { incoming_only_ = value; }
141
Honghai Zhanga74363c2016-07-28 18:06:15 -0700142 // Note: These are only for testing purpose.
143 // |ports_| and |pruned_ports| should not be changed from outside.
Peter Thatcher1fe120a2015-06-10 11:33:17 -0700144 const std::vector<PortInterface*>& ports() { return ports_; }
Honghai Zhanga74363c2016-07-28 18:06:15 -0700145 const std::vector<PortInterface*>& pruned_ports() { return pruned_ports_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000146
147 IceMode remote_ice_mode() const { return remote_ice_mode_; }
148
Honghai Zhanga74363c2016-07-28 18:06:15 -0700149 void PruneAllPorts();
Qingsi Wang866e08d2018-03-22 17:54:23 -0700150 int check_receiving_interval() const;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200151 absl::optional<rtc::NetworkRoute> network_route() const override;
Zhi Huang942bc2e2017-11-13 13:26:07 -0800152
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000153 // Helper method used only in unittest.
154 rtc::DiffServCodePoint DefaultDscpValue() const;
155
Peter Thatcher7351f462015-04-02 16:39:16 -0700156 // Public for unit tests.
157 Connection* FindNextPingableConnection();
guoweis36f01372016-03-02 18:02:40 -0800158 void MarkConnectionPinged(Connection* conn);
Peter Thatcher7351f462015-04-02 16:39:16 -0700159
honghaiz77d0d6e2015-10-27 11:34:45 -0700160 // Public for unit tests.
161 const std::vector<Connection*>& connections() const { return connections_; }
162
honghaiz9b669572015-11-04 12:07:44 -0800163 // Public for unit tests.
Qingsi Wang1b368942018-06-13 13:54:08 -0700164 PortAllocatorSession* allocator_session() const {
165 if (allocator_sessions_.empty()) {
166 return nullptr;
167 }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700168 return allocator_sessions_.back().get();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000169 }
170
honghaiz112fe432015-12-30 13:32:47 -0800171 // Public for unit tests.
172 const std::vector<RemoteCandidate>& remote_candidates() const {
173 return remote_candidates_;
174 }
175
zhihuangd06adf62017-01-12 15:58:31 -0800176 std::string ToString() const {
Jonas Olsson941a07c2018-09-13 10:07:07 +0200177 const std::string RECEIVING_ABBREV[2] = {"_", "R"};
178 const std::string WRITABLE_ABBREV[2] = {"_", "W"};
179 rtc::StringBuilder ss;
zhihuangd06adf62017-01-12 15:58:31 -0800180 ss << "Channel[" << transport_name_ << "|" << component_ << "|"
181 << RECEIVING_ABBREV[receiving_] << WRITABLE_ABBREV[writable_] << "]";
Jonas Olsson84df1c72018-09-14 16:59:32 +0200182 return ss.Release();
zhihuangd06adf62017-01-12 15:58:31 -0800183 }
184
honghaiz9b669572015-11-04 12:07:44 -0800185 private:
johan0fd22ef2016-09-29 01:19:20 -0700186 rtc::Thread* thread() const { return network_thread_; }
Qingsi Wang1b368942018-06-13 13:54:08 -0700187
honghaiz9b669572015-11-04 12:07:44 -0800188 bool IsGettingPorts() { return allocator_session()->IsGettingPorts(); }
189
honghaiza58ea782015-09-24 08:13:36 -0700190 // A transport channel is weak if the current best connection is either
191 // not receiving or not writable, or if there is no best connection at all.
Honghai Zhang2b342bf2015-09-30 09:51:58 -0700192 bool weak() const;
skvlad51072462017-02-02 11:50:14 -0800193
194 int weak_ping_interval() const {
Qingsi Wang866e08d2018-03-22 17:54:23 -0700195 return std::max(config_.ice_check_interval_weak_connectivity_or_default(),
196 config_.ice_check_min_interval_or_default());
skvlad51072462017-02-02 11:50:14 -0800197 }
198
199 int strong_ping_interval() const {
Qingsi Wang866e08d2018-03-22 17:54:23 -0700200 return std::max(config_.ice_check_interval_strong_connectivity_or_default(),
201 config_.ice_check_min_interval_or_default());
skvlad51072462017-02-02 11:50:14 -0800202 }
203
Honghai Zhange05bcc22016-08-16 18:19:14 -0700204 // Returns true if it's possible to send packets on |connection|.
205 bool ReadyToSend(Connection* connection) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000206 void UpdateConnectionStates();
Qingsi Wang10a0e512018-05-16 13:37:03 -0700207 void RequestSortAndStateUpdate(const std::string& reason_to_sort);
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700208 // Start pinging if we haven't already started, and we now have a connection
209 // that's pingable.
210 void MaybeStartPinging();
deadbeef14f97f52016-06-22 17:14:15 -0700211
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800212 int CompareCandidatePairNetworks(
213 const Connection* a,
214 const Connection* b,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200215 absl::optional<rtc::AdapterType> network_preference) const;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800216
honghaiz9ad0db52016-07-14 19:30:28 -0700217 // The methods below return a positive value if |a| is preferable to |b|,
218 // a negative value if |b| is preferable, and 0 if they're equally preferable.
219 // If |receiving_unchanged_threshold| is set, then when |b| is receiving and
220 // |a| is not, returns a negative value only if |b| has been in receiving
221 // state and |a| has been in not receiving state since
222 // |receiving_unchanged_threshold| and sets
223 // |missed_receiving_unchanged_threshold| to true otherwise.
224 int CompareConnectionStates(
225 const cricket::Connection* a,
226 const cricket::Connection* b,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200227 absl::optional<int64_t> receiving_unchanged_threshold,
honghaiz9ad0db52016-07-14 19:30:28 -0700228 bool* missed_receiving_unchanged_threshold) const;
deadbeef14f97f52016-06-22 17:14:15 -0700229 int CompareConnectionCandidates(const cricket::Connection* a,
230 const cricket::Connection* b) const;
Honghai Zhang572b0942016-06-23 12:26:57 -0700231 // Compares two connections based on the connection states
232 // (writable/receiving/connected), nomination states, last data received time,
233 // and static preferences. Does not include latency. Used by both sorting
234 // and ShouldSwitchSelectedConnection().
235 // Returns a positive value if |a| is better than |b|.
deadbeef14f97f52016-06-22 17:14:15 -0700236 int CompareConnections(const cricket::Connection* a,
honghaiz9ad0db52016-07-14 19:30:28 -0700237 const cricket::Connection* b,
Danil Chapovalov00c71832018-06-15 15:58:38 +0200238 absl::optional<int64_t> receiving_unchanged_threshold,
honghaiz9ad0db52016-07-14 19:30:28 -0700239 bool* missed_receiving_unchanged_threshold) const;
Honghai Zhang572b0942016-06-23 12:26:57 -0700240
deadbeef14f97f52016-06-22 17:14:15 -0700241 bool PresumedWritable(const cricket::Connection* conn) const;
242
Qingsi Wang10a0e512018-05-16 13:37:03 -0700243 void SortConnectionsAndUpdateState(const std::string& reason_to_sort);
Honghai Zhang572b0942016-06-23 12:26:57 -0700244 void SwitchSelectedConnection(Connection* conn);
Honghai Zhang381b4212015-12-04 12:24:03 -0800245 void UpdateState();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000246 void HandleAllTimedOut();
honghaiz9b669572015-11-04 12:07:44 -0800247 void MaybeStopPortAllocatorSessions();
Jonas Olsson81125f02018-10-09 10:52:04 +0200248
249 // ComputeIceTransportState computes the RTCIceTransportState as described in
250 // https://w3c.github.io/webrtc-pc/#dom-rtcicetransportstate. ComputeState
251 // computes the value we currently export as RTCIceTransportState.
252 // TODO(bugs.webrtc.org/9308): Remove ComputeState once it's no longer used.
zhihuangd06adf62017-01-12 15:58:31 -0800253 IceTransportState ComputeState() const;
Jonas Olsson81125f02018-10-09 10:52:04 +0200254 webrtc::IceTransportState ComputeIceTransportState() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000255
sprang716978d2016-10-11 06:43:28 -0700256 Connection* GetBestConnectionOnNetwork(rtc::Network* network) const;
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700257 bool CreateConnections(const Candidate& remote_candidate,
258 PortInterface* origin_port);
259 bool CreateConnection(PortInterface* port,
260 const Candidate& remote_candidate,
261 PortInterface* origin_port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000262 bool FindConnection(cricket::Connection* connection) const;
263
Peter Boström0c4e06b2015-10-07 12:23:21 +0200264 uint32_t GetRemoteCandidateGeneration(const Candidate& candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000265 bool IsDuplicateRemoteCandidate(const Candidate& candidate);
266 void RememberRemoteCandidate(const Candidate& remote_candidate,
267 PortInterface* origin_port);
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700268 bool IsPingable(const Connection* conn, int64_t now) const;
honghaiz7252a002016-11-08 20:04:09 -0800269 // Whether a writable connection is past its ping interval and needs to be
270 // pinged again.
271 bool WritableConnectionPastPingInterval(const Connection* conn,
272 int64_t now) const;
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700273 int CalculateActiveWritablePingInterval(const Connection* conn,
274 int64_t now) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000275 void PingConnection(Connection* conn);
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700276 void AddAllocatorSession(std::unique_ptr<PortAllocatorSession> session);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000277 void AddConnection(Connection* connection);
278
Yves Gerey665174f2018-06-19 15:03:05 +0200279 void OnPortReady(PortAllocatorSession* session, PortInterface* port);
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700280 void OnPortsPruned(PortAllocatorSession* session,
281 const std::vector<PortInterface*>& ports);
Yves Gerey665174f2018-06-19 15:03:05 +0200282 void OnCandidatesReady(PortAllocatorSession* session,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000283 const std::vector<Candidate>& candidates);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700284 void OnCandidatesRemoved(PortAllocatorSession* session,
285 const std::vector<Candidate>& candidates);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000286 void OnCandidatesAllocationDone(PortAllocatorSession* session);
287 void OnUnknownAddress(PortInterface* port,
288 const rtc::SocketAddress& addr,
289 ProtocolType proto,
290 IceMessage* stun_msg,
291 const std::string& remote_username,
292 bool port_muxed);
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700293
294 // When a port is destroyed, remove it from both lists |ports_|
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700295 // and |pruned_ports_|.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000296 void OnPortDestroyed(PortInterface* port);
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700297 // When pruning a port, move it from |ports_| to |pruned_ports_|.
Honghai Zhang5622c5e2016-07-01 13:59:29 -0700298 // Returns true if the port is found and removed from |ports_|.
Honghai Zhanga74363c2016-07-28 18:06:15 -0700299 bool PrunePort(PortInterface* port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000300 void OnRoleConflict(PortInterface* port);
301
302 void OnConnectionStateChange(Connection* connection);
Yves Gerey665174f2018-06-19 15:03:05 +0200303 void OnReadPacket(Connection* connection,
304 const char* data,
305 size_t len,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000306 const rtc::PacketTime& packet_time);
Stefan Holmer55674ff2016-01-14 15:49:16 +0100307 void OnSentPacket(const rtc::SentPacket& sent_packet);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000308 void OnReadyToSend(Connection* connection);
Yves Gerey665174f2018-06-19 15:03:05 +0200309 void OnConnectionDestroyed(Connection* connection);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000310
honghaiz5a3acd82015-08-20 15:53:17 -0700311 void OnNominated(Connection* conn);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000312
Qingsi Wang502db3d2018-05-16 17:01:37 -0700313 void CheckAndPing();
Peter Thatcher54360512015-07-08 11:08:35 -0700314
Bjorn Terelius59b4e3e2018-05-30 17:14:08 +0200315 void LogCandidatePairConfig(Connection* conn,
316 webrtc::IceCandidatePairConfigType type);
Qingsi Wang93a84392018-01-30 17:13:09 -0800317
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700318 uint32_t GetNominationAttr(Connection* conn) const;
319 bool GetUseCandidateAttr(Connection* conn, NominationMode mode) const;
320
honghaiz9ad0db52016-07-14 19:30:28 -0700321 // Returns true if we should switch to the new connection.
322 // sets |missed_receiving_unchanged_threshold| to true if either
323 // the selected connection or the new connection missed its
324 // receiving-unchanged-threshold.
325 bool ShouldSwitchSelectedConnection(
326 Connection* new_connection,
327 bool* missed_receiving_unchanged_threshold) const;
328 // Returns true if the new_connection is selected for transmission.
329 bool MaybeSwitchSelectedConnection(Connection* new_connection,
330 const std::string& reason);
honghaiz7252a002016-11-08 20:04:09 -0800331 // Gets the best connection for each network.
332 std::map<rtc::Network*, Connection*> GetBestConnectionByNetwork() const;
333 std::vector<Connection*> GetBestWritableConnectionPerNetwork() const;
honghaiz5a3acd82015-08-20 15:53:17 -0700334 void PruneConnections();
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700335 bool IsBackupConnection(const Connection* conn) const;
honghaiz5a3acd82015-08-20 15:53:17 -0700336
honghaiz34b11eb2016-03-16 08:55:44 -0700337 Connection* FindOldestConnectionNeedingTriggeredCheck(int64_t now);
guoweis36f01372016-03-02 18:02:40 -0800338 // Between |conn1| and |conn2|, this function returns the one which should
339 // be pinged first.
honghaiz7252a002016-11-08 20:04:09 -0800340 Connection* MorePingable(Connection* conn1, Connection* conn2);
guoweis36f01372016-03-02 18:02:40 -0800341 // Select the connection which is Relay/Relay. If both of them are,
342 // UDP relay protocol takes precedence.
343 Connection* MostLikelyToWork(Connection* conn1, Connection* conn2);
344 // Compare the last_ping_sent time and return the one least recently pinged.
345 Connection* LeastRecentlyPinged(Connection* conn1, Connection* conn2);
346
honghaiza54a0802015-12-16 18:37:23 -0800347 // Returns the latest remote ICE parameters or nullptr if there are no remote
348 // ICE parameters yet.
349 IceParameters* remote_ice() {
350 return remote_ice_parameters_.empty() ? nullptr
351 : &remote_ice_parameters_.back();
352 }
honghaiz112fe432015-12-30 13:32:47 -0800353 // Returns the remote IceParameters and generation that match |ufrag|
354 // if found, and returns nullptr otherwise.
355 const IceParameters* FindRemoteIceFromUfrag(const std::string& ufrag,
356 uint32_t* generation);
honghaiza54a0802015-12-16 18:37:23 -0800357 // Returns the index of the latest remote ICE parameters, or 0 if no remote
358 // ICE parameters have been received.
359 uint32_t remote_ice_generation() {
360 return remote_ice_parameters_.empty()
361 ? 0
362 : static_cast<uint32_t>(remote_ice_parameters_.size() - 1);
363 }
364
Steve Anton300bf8e2017-07-14 10:13:10 -0700365 // Indicates if the given local port has been pruned.
366 bool IsPortPruned(const Port* port) const;
367
368 // Indicates if the given remote candidate has been pruned.
369 bool IsRemoteCandidatePruned(const Candidate& cand) const;
370
zhihuangd06adf62017-01-12 15:58:31 -0800371 // Sets the writable state, signaling if necessary.
372 void set_writable(bool writable);
373 // Sets the receiving state, signaling if necessary.
374 void set_receiving(bool receiving);
375
376 std::string transport_name_;
377 int component_;
deadbeefcbecd352015-09-23 11:50:27 -0700378 PortAllocator* allocator_;
Zach Steine20867f2018-08-02 13:20:15 -0700379 webrtc::AsyncResolverFactory* async_resolver_factory_;
johan0fd22ef2016-09-29 01:19:20 -0700380 rtc::Thread* network_thread_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000381 bool incoming_only_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000382 int error_;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700383 std::vector<std::unique_ptr<PortAllocatorSession>> allocator_sessions_;
deadbeefdfc42442016-06-21 14:19:48 -0700384 // |ports_| contains ports that are used to form new connections when
385 // new remote candidates are added.
386 std::vector<PortInterface*> ports_;
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700387 // |pruned_ports_| contains ports that have been removed from |ports_| and
deadbeefdfc42442016-06-21 14:19:48 -0700388 // are not being used to form new connections, but that aren't yet destroyed.
389 // They may have existing connections, and they still fire signals such as
390 // SignalUnknownAddress.
Honghai Zhang8eeecab2016-07-28 13:20:15 -0700391 std::vector<PortInterface*> pruned_ports_;
guoweis36f01372016-03-02 18:02:40 -0800392
393 // |connections_| is a sorted list with the first one always be the
Honghai Zhang572b0942016-06-23 12:26:57 -0700394 // |selected_connection_| when it's not nullptr. The combination of
guoweis36f01372016-03-02 18:02:40 -0800395 // |pinged_connections_| and |unpinged_connections_| has the same
396 // connections as |connections_|. These 2 sets maintain whether a
397 // connection should be pinged next or not.
Yves Gerey665174f2018-06-19 15:03:05 +0200398 std::vector<Connection*> connections_;
guoweis36f01372016-03-02 18:02:40 -0800399 std::set<Connection*> pinged_connections_;
400 std::set<Connection*> unpinged_connections_;
401
Honghai Zhang572b0942016-06-23 12:26:57 -0700402 Connection* selected_connection_ = nullptr;
guoweis36f01372016-03-02 18:02:40 -0800403
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000404 std::vector<RemoteCandidate> remote_candidates_;
405 bool sort_dirty_; // indicates whether another sort is needed right now
deadbeefcbecd352015-09-23 11:50:27 -0700406 bool had_connection_ = false; // if connections_ has ever been nonempty
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000407 typedef std::map<rtc::Socket::Option, int> OptionMap;
408 OptionMap options_;
Honghai Zhang4cedf2b2016-08-31 08:18:11 -0700409 IceParameters ice_parameters_;
honghaiza54a0802015-12-16 18:37:23 -0800410 std::vector<IceParameters> remote_ice_parameters_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000411 IceMode remote_ice_mode_;
412 IceRole ice_role_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200413 uint64_t tiebreaker_;
deadbeefcbecd352015-09-23 11:50:27 -0700414 IceGatheringState gathering_state_;
Qingsi Wang1b368942018-06-13 13:54:08 -0700415 std::unique_ptr<webrtc::BasicRegatheringController> regathering_controller_;
honghaiz34b11eb2016-03-16 08:55:44 -0700416 int64_t last_ping_sent_ms_ = 0;
Honghai Zhang049fbb12016-03-07 11:13:07 -0800417 int weak_ping_interval_ = WEAK_PING_INTERVAL;
Jonas Olsson81125f02018-10-09 10:52:04 +0200418 // TODO(jonasolsson): Remove state_ and rename standardized_state_ once state_
419 // is no longer used to compute the ICE connection state.
zhihuangd06adf62017-01-12 15:58:31 -0800420 IceTransportState state_ = IceTransportState::STATE_INIT;
Jonas Olsson81125f02018-10-09 10:52:04 +0200421 webrtc::IceTransportState standardized_state_ =
422 webrtc::IceTransportState::kNew;
guoweis36f01372016-03-02 18:02:40 -0800423 IceConfig config_;
Honghai Zhang52dce732016-03-31 12:37:31 -0700424 int last_sent_packet_id_ = -1; // -1 indicates no packet was sent before.
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700425 bool started_pinging_ = false;
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700426 // The value put in the "nomination" attribute for the next nominated
427 // connection. A zero-value indicates the connection will not be nominated.
428 uint32_t nomination_ = 0;
zhihuangd06adf62017-01-12 15:58:31 -0800429 bool receiving_ = false;
430 bool writable_ = false;
Peter Thatcher54360512015-07-08 11:08:35 -0700431
Qingsi Wang502db3d2018-05-16 17:01:37 -0700432 rtc::AsyncInvoker invoker_;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200433 absl::optional<rtc::NetworkRoute> network_route_;
Qingsi Wang93a84392018-01-30 17:13:09 -0800434 webrtc::IceEventLog ice_event_log_;
435
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700436 struct CandidateAndResolver final {
437 CandidateAndResolver(const Candidate& candidate,
438 rtc::AsyncResolverInterface* resolver);
439 ~CandidateAndResolver();
440 Candidate candidate_;
441 rtc::AsyncResolverInterface* resolver_;
442 };
443 std::vector<CandidateAndResolver> resolvers_;
444 void FinishAddingRemoteCandidate(const Candidate& new_remote_candidate);
445 void OnCandidateResolved(rtc::AsyncResolverInterface* resolver);
446 void AddRemoteCandidateWithResolver(Candidate candidate,
447 rtc::AsyncResolverInterface* resolver);
448
henrikg3c089d72015-09-16 05:37:44 -0700449 RTC_DISALLOW_COPY_AND_ASSIGN(P2PTransportChannel);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000450};
451
452} // namespace cricket
453
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200454#endif // P2P_BASE_P2PTRANSPORTCHANNEL_H_