blob: 94b5b194ff0977d9235d4e23af65f8d94daa0d32 [file] [log] [blame]
zhihuang86abd6f2016-12-19 11:54:05 -08001/*
2 * Copyright 2016 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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef P2P_BASE_ICE_TRANSPORT_INTERNAL_H_
12#define P2P_BASE_ICE_TRANSPORT_INTERNAL_H_
zhihuang86abd6f2016-12-19 11:54:05 -080013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
zhihuang86abd6f2016-12-19 11:54:05 -080016#include <string>
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080017#include <vector>
zhihuang86abd6f2016-12-19 11:54:05 -080018
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "absl/types/optional.h"
Patrik Höglunde2d6a062017-10-05 14:53:33 +020020#include "api/candidate.h"
Jonas Olsson81125f02018-10-09 10:52:04 +020021#include "api/transport/enums.h"
Jonas Orelande8e7d7b2019-05-29 09:30:55 +020022#include "p2p/base/connection.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "p2p/base/packet_transport_internal.h"
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080024#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "p2p/base/transport_description.h"
Yves Gerey3e707812018-11-28 16:47:49 +010026#include "rtc_base/network_constants.h"
Mirko Bonadeiac194142018-10-22 17:08:37 +020027#include "rtc_base/system/rtc_export.h"
Yves Gerey3e707812018-11-28 16:47:49 +010028#include "rtc_base/third_party/sigslot/sigslot.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "rtc_base/time_utils.h"
zhihuang86abd6f2016-12-19 11:54:05 -080030
zhihuang86abd6f2016-12-19 11:54:05 -080031namespace cricket {
32
Jonas Oreland149dc722019-08-28 08:10:27 +020033struct IceTransportStats {
34 CandidateStatsList candidate_stats_list;
35 ConnectionInfos connection_infos;
36 // Number of times the selected candidate pair has changed
37 // Initially 0 and 1 once the first candidate pair has been selected.
38 // The counter is increase also when "unselecting" a connection.
39 uint32_t selected_candidate_pair_changes = 0;
40};
41
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080042typedef std::vector<Candidate> Candidates;
43
Alex Loiko9289eda2018-11-23 16:18:59 +000044enum IceConnectionState {
45 kIceConnectionConnecting = 0,
46 kIceConnectionFailed,
47 kIceConnectionConnected, // Writable, but still checking one or more
48 // connections
49 kIceConnectionCompleted,
50};
51
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080052// TODO(deadbeef): Unify with PeerConnectionInterface::IceConnectionState
53// once /talk/ and /webrtc/ are combined, and also switch to ENUM_NAME naming
54// style.
55enum IceGatheringState {
56 kIceGatheringNew = 0,
57 kIceGatheringGathering,
58 kIceGatheringComplete,
59};
60
61enum ContinualGatheringPolicy {
62 // All port allocator sessions will stop after a writable connection is found.
63 GATHER_ONCE = 0,
64 // The most recent port allocator session will keep on running.
65 GATHER_CONTINUALLY,
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080066};
67
68// ICE Nomination mode.
69enum class NominationMode {
70 REGULAR, // Nominate once per ICE restart (Not implemented yet).
71 AGGRESSIVE, // Nominate every connection except that it will behave as if
72 // REGULAR when the remote is an ICE-LITE endpoint.
73 SEMI_AGGRESSIVE // Our current implementation of the nomination algorithm.
74 // The details are described in P2PTransportChannel.
75};
76
77// Information about ICE configuration.
Danil Chapovalov00c71832018-06-15 15:58:38 +020078// TODO(deadbeef): Use absl::optional to represent unset values, instead of
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080079// -1.
80struct IceConfig {
81 // The ICE connection receiving timeout value in milliseconds.
Danil Chapovalov00c71832018-06-15 15:58:38 +020082 absl::optional<int> receiving_timeout;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080083 // Time interval in milliseconds to ping a backup connection when the ICE
84 // channel is strongly connected.
Danil Chapovalov00c71832018-06-15 15:58:38 +020085 absl::optional<int> backup_connection_ping_interval;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080086
87 ContinualGatheringPolicy continual_gathering_policy = GATHER_ONCE;
88
89 bool gather_continually() const {
Qingsi Wang241d0c12018-06-05 16:44:10 -070090 return continual_gathering_policy == GATHER_CONTINUALLY;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080091 }
92
93 // Whether we should prioritize Relay/Relay candidate when nothing
94 // is writable yet.
95 bool prioritize_most_likely_candidate_pairs = false;
96
97 // Writable connections are pinged at a slower rate once stablized.
Danil Chapovalov00c71832018-06-15 15:58:38 +020098 absl::optional<int> stable_writable_connection_ping_interval;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080099
100 // If set to true, this means the ICE transport should presume TURN-to-TURN
101 // candidate pairs will succeed, even before a binding response is received.
102 bool presume_writable_when_fully_relayed = false;
103
Qingsi Wang1fe119f2019-05-31 16:55:33 -0700104 // If true, after the ICE transport type (as the candidate filter used by the
105 // port allocator) is changed such that new types of ICE candidates are
106 // allowed by the new filter, e.g. from CF_RELAY to CF_ALL, candidates that
107 // have been gathered by the ICE transport but filtered out and not signaled
108 // to the upper layers, will be surfaced.
109 bool surface_ice_candidates_on_ice_transport_type_changed = false;
110
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800111 // Interval to check on all networks and to perform ICE regathering on any
112 // active network having no connection on it.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200113 absl::optional<int> regather_on_failed_networks_interval;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800114
115 // Interval to perform ICE regathering on all networks
116 // The delay in milliseconds is sampled from the uniform distribution [a, b]
Danil Chapovalov00c71832018-06-15 15:58:38 +0200117 absl::optional<rtc::IntervalRange> regather_all_networks_interval_range;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800118
119 // The time period in which we will not switch the selected connection
120 // when a new connection becomes receiving but the selected connection is not
121 // in case that the selected connection may become receiving soon.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200122 absl::optional<int> receiving_switching_delay;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800123
124 // TODO(honghaiz): Change the default to regular nomination.
125 // Default nomination mode if the remote does not support renomination.
126 NominationMode default_nomination_mode = NominationMode::SEMI_AGGRESSIVE;
127
Qingsi Wange6826d22018-03-08 14:55:14 -0800128 // The interval in milliseconds at which ICE checks (STUN pings) will be sent
129 // for a candidate pair when it is both writable and receiving (strong
130 // connectivity). This parameter overrides the default value given by
131 // |STRONG_PING_INTERVAL| in p2ptransport.h if set.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200132 absl::optional<int> ice_check_interval_strong_connectivity;
Qingsi Wange6826d22018-03-08 14:55:14 -0800133 // The interval in milliseconds at which ICE checks (STUN pings) will be sent
134 // for a candidate pair when it is either not writable or not receiving (weak
135 // connectivity). This parameter overrides the default value given by
136 // |WEAK_PING_INTERVAL| in p2ptransport.h if set.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200137 absl::optional<int> ice_check_interval_weak_connectivity;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800138 // ICE checks (STUN pings) will not be sent at higher rate (lower interval)
139 // than this, no matter what other settings there are.
140 // Measure in milliseconds.
Qingsi Wange6826d22018-03-08 14:55:14 -0800141 //
142 // Note that this parameter overrides both the above check intervals for
143 // candidate pairs with strong or weak connectivity, if either of the above
144 // interval is shorter than the min interval.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200145 absl::optional<int> ice_check_min_interval;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700146 // The min time period for which a candidate pair must wait for response to
147 // connectivity checks before it becomes unwritable. This parameter
148 // overrides the default value given by |CONNECTION_WRITE_CONNECT_TIMEOUT|
149 // in port.h if set, when determining the writability of a candidate pair.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200150 absl::optional<int> ice_unwritable_timeout;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700151
152 // The min number of connectivity checks that a candidate pair must sent
153 // without receiving response before it becomes unwritable. This parameter
154 // overrides the default value given by |CONNECTION_WRITE_CONNECT_FAILURES| in
155 // port.h if set, when determining the writability of a candidate pair.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200156 absl::optional<int> ice_unwritable_min_checks;
Jiawei Ou9d4fd5552018-12-06 23:30:17 -0800157
158 // The min time period for which a candidate pair must wait for response to
159 // connectivity checks it becomes inactive. This parameter overrides the
160 // default value given by |CONNECTION_WRITE_TIMEOUT| in port.h if set, when
161 // determining the writability of a candidate pair.
162 absl::optional<int> ice_inactive_timeout;
163
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800164 // The interval in milliseconds at which STUN candidates will resend STUN
165 // binding requests to keep NAT bindings open.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200166 absl::optional<int> stun_keepalive_interval;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800167
Danil Chapovalov00c71832018-06-15 15:58:38 +0200168 absl::optional<rtc::AdapterType> network_preference;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800169
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800170 IceConfig();
171 IceConfig(int receiving_timeout_ms,
172 int backup_connection_ping_interval,
173 ContinualGatheringPolicy gathering_policy,
174 bool prioritize_most_likely_candidate_pairs,
175 int stable_writable_connection_ping_interval_ms,
176 bool presume_writable_when_fully_relayed,
177 int regather_on_failed_networks_interval_ms,
Qingsi Wang866e08d2018-03-22 17:54:23 -0700178 int receiving_switching_delay_ms);
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800179 ~IceConfig();
Qingsi Wang866e08d2018-03-22 17:54:23 -0700180
181 // Helper getters for parameters with implementation-specific default value.
182 // By convention, parameters with default value are represented by
Danil Chapovalov00c71832018-06-15 15:58:38 +0200183 // absl::optional and setting a parameter to null restores its default value.
Qingsi Wang866e08d2018-03-22 17:54:23 -0700184 int receiving_timeout_or_default() const;
185 int backup_connection_ping_interval_or_default() const;
186 int stable_writable_connection_ping_interval_or_default() const;
187 int regather_on_failed_networks_interval_or_default() const;
188 int receiving_switching_delay_or_default() const;
189 int ice_check_interval_strong_connectivity_or_default() const;
190 int ice_check_interval_weak_connectivity_or_default() const;
191 int ice_check_min_interval_or_default() const;
192 int ice_unwritable_timeout_or_default() const;
193 int ice_unwritable_min_checks_or_default() const;
Jiawei Ou9d4fd5552018-12-06 23:30:17 -0800194 int ice_inactive_timeout_or_default() const;
Qingsi Wang866e08d2018-03-22 17:54:23 -0700195 int stun_keepalive_interval_or_default() const;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800196};
197
zhihuangb2cdd932017-01-19 16:54:25 -0800198// TODO(zhihuang): Replace this with
199// PeerConnectionInterface::IceConnectionState.
zhihuangd06adf62017-01-12 15:58:31 -0800200enum class IceTransportState {
zhihuang86abd6f2016-12-19 11:54:05 -0800201 STATE_INIT,
202 STATE_CONNECTING, // Will enter this state once a connection is created
203 STATE_COMPLETED,
zhihuangd06adf62017-01-12 15:58:31 -0800204 STATE_FAILED
zhihuang86abd6f2016-12-19 11:54:05 -0800205};
206
207// TODO(zhihuang): Remove this once it's no longer used in
208// remoting/protocol/libjingle_transport_factory.cc
209enum IceProtocolType {
210 ICEPROTO_RFC5245 // Standard RFC 5245 version of ICE.
211};
212
213// IceTransportInternal is an internal abstract class that does ICE.
214// Once the public interface is supported,
Niels Möllerc6ec4b12018-10-24 14:10:53 +0200215// (https://www.w3.org/TR/webrtc/#rtcicetransport)
zhihuang86abd6f2016-12-19 11:54:05 -0800216// the IceTransportInterface will be split from this class.
Mirko Bonadeiac194142018-10-22 17:08:37 +0200217class RTC_EXPORT IceTransportInternal : public rtc::PacketTransportInternal {
zhihuang86abd6f2016-12-19 11:54:05 -0800218 public:
Steve Anton33f69db2017-10-30 10:01:15 -0700219 IceTransportInternal();
220 ~IceTransportInternal() override;
zhihuang86abd6f2016-12-19 11:54:05 -0800221
Jonas Olsson81125f02018-10-09 10:52:04 +0200222 // TODO(bugs.webrtc.org/9308): Remove GetState once all uses have been
223 // migrated to GetIceTransportState.
zhihuangd06adf62017-01-12 15:58:31 -0800224 virtual IceTransportState GetState() const = 0;
Jonas Olsson81125f02018-10-09 10:52:04 +0200225 virtual webrtc::IceTransportState GetIceTransportState() const = 0;
zhihuang86abd6f2016-12-19 11:54:05 -0800226
zhihuang86abd6f2016-12-19 11:54:05 -0800227 virtual int component() const = 0;
228
229 virtual IceRole GetIceRole() const = 0;
230
231 virtual void SetIceRole(IceRole role) = 0;
232
233 virtual void SetIceTiebreaker(uint64_t tiebreaker) = 0;
234
235 // TODO(zhihuang): Remove this once it's no longer called in
236 // remoting/protocol/libjingle_transport_factory.cc
237 virtual void SetIceProtocolType(IceProtocolType type) {}
238
239 virtual void SetIceCredentials(const std::string& ice_ufrag,
Steve Anton33f69db2017-10-30 10:01:15 -0700240 const std::string& ice_pwd);
zhihuang86abd6f2016-12-19 11:54:05 -0800241
242 virtual void SetRemoteIceCredentials(const std::string& ice_ufrag,
Steve Anton33f69db2017-10-30 10:01:15 -0700243 const std::string& ice_pwd);
zhihuang86abd6f2016-12-19 11:54:05 -0800244
245 // The ufrag and pwd in |ice_params| must be set
246 // before candidate gathering can start.
247 virtual void SetIceParameters(const IceParameters& ice_params) = 0;
248
249 virtual void SetRemoteIceParameters(const IceParameters& ice_params) = 0;
250
251 virtual void SetRemoteIceMode(IceMode mode) = 0;
252
253 virtual void SetIceConfig(const IceConfig& config) = 0;
254
255 // Start gathering candidates if not already started, or if an ICE restart
256 // occurred.
257 virtual void MaybeStartGathering() = 0;
258
zhihuang86abd6f2016-12-19 11:54:05 -0800259 virtual void AddRemoteCandidate(const Candidate& candidate) = 0;
260
261 virtual void RemoveRemoteCandidate(const Candidate& candidate) = 0;
262
Harald Alvestrand4241cf52019-01-21 09:29:42 +0100263 virtual void RemoveAllRemoteCandidates() = 0;
264
zhihuang86abd6f2016-12-19 11:54:05 -0800265 virtual IceGatheringState gathering_state() const = 0;
266
zhihuangd06adf62017-01-12 15:58:31 -0800267 // Returns the current stats for this connection.
Jonas Oreland149dc722019-08-28 08:10:27 +0200268 virtual bool GetStats(IceTransportStats* ice_transport_stats) = 0;
zhihuangd06adf62017-01-12 15:58:31 -0800269
skvladd0309122017-02-02 17:18:37 -0800270 // Returns RTT estimate over the currently active connection, or an empty
Danil Chapovalov00c71832018-06-15 15:58:38 +0200271 // absl::optional if there is none.
272 virtual absl::optional<int> GetRttEstimate() = 0;
skvladd0309122017-02-02 17:18:37 -0800273
Qingsi Wange5defb12019-08-15 11:01:53 -0700274 // TODO(qingsi): Remove this method once Chrome does not depend on it anymore.
Harald Alvestrand4241cf52019-01-21 09:29:42 +0100275 virtual const Connection* selected_connection() const = 0;
276
Qingsi Wange5defb12019-08-15 11:01:53 -0700277 // Returns the selected candidate pair, or an empty absl::optional if there is
278 // none.
279 virtual absl::optional<const CandidatePair> GetSelectedCandidatePair()
280 const = 0;
281
zhihuang86abd6f2016-12-19 11:54:05 -0800282 sigslot::signal1<IceTransportInternal*> SignalGatheringState;
283
284 // Handles sending and receiving of candidates.
285 sigslot::signal2<IceTransportInternal*, const Candidate&>
286 SignalCandidateGathered;
287
Eldar Relloda13ea22019-06-01 12:23:43 +0300288 sigslot::signal2<IceTransportInternal*, const IceCandidateErrorEvent&>
289 SignalCandidateError;
290
zhihuang86abd6f2016-12-19 11:54:05 -0800291 sigslot::signal2<IceTransportInternal*, const Candidates&>
292 SignalCandidatesRemoved;
293
Zhi Huang942bc2e2017-11-13 13:26:07 -0800294 // Deprecated by PacketTransportInternal::SignalNetworkRouteChanged.
zhihuang86abd6f2016-12-19 11:54:05 -0800295 // This signal occurs when there is a change in the way that packets are
296 // being routed, i.e. to a different remote location. The candidate
297 // indicates where and how we are currently sending media.
Zhi Huang942bc2e2017-11-13 13:26:07 -0800298 // TODO(zhihuang): Update the Chrome remoting to use the new
299 // SignalNetworkRouteChanged.
zhihuang86abd6f2016-12-19 11:54:05 -0800300 sigslot::signal2<IceTransportInternal*, const Candidate&> SignalRouteChange;
301
Alex Drake00c7ecf2019-08-06 10:54:47 -0700302 sigslot::signal1<const cricket::CandidatePairChangeEvent&>
303 SignalCandidatePairChanged;
304
zhihuangd06adf62017-01-12 15:58:31 -0800305 // Invoked when there is conflict in the ICE role between local and remote
306 // agents.
307 sigslot::signal1<IceTransportInternal*> SignalRoleConflict;
308
309 // Emitted whenever the transport state changed.
Jonas Olsson81125f02018-10-09 10:52:04 +0200310 // TODO(bugs.webrtc.org/9308): Remove once all uses have migrated to the new
311 // IceTransportState.
zhihuangd06adf62017-01-12 15:58:31 -0800312 sigslot::signal1<IceTransportInternal*> SignalStateChanged;
313
Jonas Olsson81125f02018-10-09 10:52:04 +0200314 // Emitted whenever the new standards-compliant transport state changed.
315 sigslot::signal1<IceTransportInternal*> SignalIceTransportStateChanged;
316
zhihuang86abd6f2016-12-19 11:54:05 -0800317 // Invoked when the transport is being destroyed.
318 sigslot::signal1<IceTransportInternal*> SignalDestroyed;
zhihuang86abd6f2016-12-19 11:54:05 -0800319};
320
321} // namespace cricket
zhihuangd06adf62017-01-12 15:58:31 -0800322
Steve Anton10542f22019-01-11 09:11:00 -0800323#endif // P2P_BASE_ICE_TRANSPORT_INTERNAL_H_