blob: 630848f6e6e34177779203a4a3baaad8dc8c9dc3 [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
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080033typedef std::vector<Candidate> Candidates;
34
Alex Loiko9289eda2018-11-23 16:18:59 +000035enum IceConnectionState {
36 kIceConnectionConnecting = 0,
37 kIceConnectionFailed,
38 kIceConnectionConnected, // Writable, but still checking one or more
39 // connections
40 kIceConnectionCompleted,
41};
42
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080043// TODO(deadbeef): Unify with PeerConnectionInterface::IceConnectionState
44// once /talk/ and /webrtc/ are combined, and also switch to ENUM_NAME naming
45// style.
46enum IceGatheringState {
47 kIceGatheringNew = 0,
48 kIceGatheringGathering,
49 kIceGatheringComplete,
50};
51
52enum ContinualGatheringPolicy {
53 // All port allocator sessions will stop after a writable connection is found.
54 GATHER_ONCE = 0,
55 // The most recent port allocator session will keep on running.
56 GATHER_CONTINUALLY,
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080057};
58
59// ICE Nomination mode.
60enum class NominationMode {
61 REGULAR, // Nominate once per ICE restart (Not implemented yet).
62 AGGRESSIVE, // Nominate every connection except that it will behave as if
63 // REGULAR when the remote is an ICE-LITE endpoint.
64 SEMI_AGGRESSIVE // Our current implementation of the nomination algorithm.
65 // The details are described in P2PTransportChannel.
66};
67
68// Information about ICE configuration.
Danil Chapovalov00c71832018-06-15 15:58:38 +020069// TODO(deadbeef): Use absl::optional to represent unset values, instead of
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080070// -1.
71struct IceConfig {
72 // The ICE connection receiving timeout value in milliseconds.
Danil Chapovalov00c71832018-06-15 15:58:38 +020073 absl::optional<int> receiving_timeout;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080074 // Time interval in milliseconds to ping a backup connection when the ICE
75 // channel is strongly connected.
Danil Chapovalov00c71832018-06-15 15:58:38 +020076 absl::optional<int> backup_connection_ping_interval;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080077
78 ContinualGatheringPolicy continual_gathering_policy = GATHER_ONCE;
79
80 bool gather_continually() const {
Qingsi Wang241d0c12018-06-05 16:44:10 -070081 return continual_gathering_policy == GATHER_CONTINUALLY;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080082 }
83
84 // Whether we should prioritize Relay/Relay candidate when nothing
85 // is writable yet.
86 bool prioritize_most_likely_candidate_pairs = false;
87
88 // Writable connections are pinged at a slower rate once stablized.
Danil Chapovalov00c71832018-06-15 15:58:38 +020089 absl::optional<int> stable_writable_connection_ping_interval;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080090
91 // If set to true, this means the ICE transport should presume TURN-to-TURN
92 // candidate pairs will succeed, even before a binding response is received.
93 bool presume_writable_when_fully_relayed = false;
94
Qingsi Wang1fe119f2019-05-31 16:55:33 -070095 // If true, after the ICE transport type (as the candidate filter used by the
96 // port allocator) is changed such that new types of ICE candidates are
97 // allowed by the new filter, e.g. from CF_RELAY to CF_ALL, candidates that
98 // have been gathered by the ICE transport but filtered out and not signaled
99 // to the upper layers, will be surfaced.
100 bool surface_ice_candidates_on_ice_transport_type_changed = false;
101
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800102 // Interval to check on all networks and to perform ICE regathering on any
103 // active network having no connection on it.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200104 absl::optional<int> regather_on_failed_networks_interval;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800105
106 // Interval to perform ICE regathering on all networks
107 // The delay in milliseconds is sampled from the uniform distribution [a, b]
Danil Chapovalov00c71832018-06-15 15:58:38 +0200108 absl::optional<rtc::IntervalRange> regather_all_networks_interval_range;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800109
110 // The time period in which we will not switch the selected connection
111 // when a new connection becomes receiving but the selected connection is not
112 // in case that the selected connection may become receiving soon.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200113 absl::optional<int> receiving_switching_delay;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800114
115 // TODO(honghaiz): Change the default to regular nomination.
116 // Default nomination mode if the remote does not support renomination.
117 NominationMode default_nomination_mode = NominationMode::SEMI_AGGRESSIVE;
118
Qingsi Wange6826d22018-03-08 14:55:14 -0800119 // The interval in milliseconds at which ICE checks (STUN pings) will be sent
120 // for a candidate pair when it is both writable and receiving (strong
121 // connectivity). This parameter overrides the default value given by
122 // |STRONG_PING_INTERVAL| in p2ptransport.h if set.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200123 absl::optional<int> ice_check_interval_strong_connectivity;
Qingsi Wange6826d22018-03-08 14:55:14 -0800124 // The interval in milliseconds at which ICE checks (STUN pings) will be sent
125 // for a candidate pair when it is either not writable or not receiving (weak
126 // connectivity). This parameter overrides the default value given by
127 // |WEAK_PING_INTERVAL| in p2ptransport.h if set.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200128 absl::optional<int> ice_check_interval_weak_connectivity;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800129 // ICE checks (STUN pings) will not be sent at higher rate (lower interval)
130 // than this, no matter what other settings there are.
131 // Measure in milliseconds.
Qingsi Wange6826d22018-03-08 14:55:14 -0800132 //
133 // Note that this parameter overrides both the above check intervals for
134 // candidate pairs with strong or weak connectivity, if either of the above
135 // interval is shorter than the min interval.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200136 absl::optional<int> ice_check_min_interval;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700137 // The min time period for which a candidate pair must wait for response to
138 // connectivity checks before it becomes unwritable. This parameter
139 // overrides the default value given by |CONNECTION_WRITE_CONNECT_TIMEOUT|
140 // in port.h if set, when determining the writability of a candidate pair.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200141 absl::optional<int> ice_unwritable_timeout;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700142
143 // The min number of connectivity checks that a candidate pair must sent
144 // without receiving response before it becomes unwritable. This parameter
145 // overrides the default value given by |CONNECTION_WRITE_CONNECT_FAILURES| in
146 // port.h if set, when determining the writability of a candidate pair.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200147 absl::optional<int> ice_unwritable_min_checks;
Jiawei Ou9d4fd5552018-12-06 23:30:17 -0800148
149 // The min time period for which a candidate pair must wait for response to
150 // connectivity checks it becomes inactive. This parameter overrides the
151 // default value given by |CONNECTION_WRITE_TIMEOUT| in port.h if set, when
152 // determining the writability of a candidate pair.
153 absl::optional<int> ice_inactive_timeout;
154
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800155 // The interval in milliseconds at which STUN candidates will resend STUN
156 // binding requests to keep NAT bindings open.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200157 absl::optional<int> stun_keepalive_interval;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800158
Danil Chapovalov00c71832018-06-15 15:58:38 +0200159 absl::optional<rtc::AdapterType> network_preference;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800160
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800161 IceConfig();
162 IceConfig(int receiving_timeout_ms,
163 int backup_connection_ping_interval,
164 ContinualGatheringPolicy gathering_policy,
165 bool prioritize_most_likely_candidate_pairs,
166 int stable_writable_connection_ping_interval_ms,
167 bool presume_writable_when_fully_relayed,
168 int regather_on_failed_networks_interval_ms,
Qingsi Wang866e08d2018-03-22 17:54:23 -0700169 int receiving_switching_delay_ms);
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800170 ~IceConfig();
Qingsi Wang866e08d2018-03-22 17:54:23 -0700171
172 // Helper getters for parameters with implementation-specific default value.
173 // By convention, parameters with default value are represented by
Danil Chapovalov00c71832018-06-15 15:58:38 +0200174 // absl::optional and setting a parameter to null restores its default value.
Qingsi Wang866e08d2018-03-22 17:54:23 -0700175 int receiving_timeout_or_default() const;
176 int backup_connection_ping_interval_or_default() const;
177 int stable_writable_connection_ping_interval_or_default() const;
178 int regather_on_failed_networks_interval_or_default() const;
179 int receiving_switching_delay_or_default() const;
180 int ice_check_interval_strong_connectivity_or_default() const;
181 int ice_check_interval_weak_connectivity_or_default() const;
182 int ice_check_min_interval_or_default() const;
183 int ice_unwritable_timeout_or_default() const;
184 int ice_unwritable_min_checks_or_default() const;
Jiawei Ou9d4fd5552018-12-06 23:30:17 -0800185 int ice_inactive_timeout_or_default() const;
Qingsi Wang866e08d2018-03-22 17:54:23 -0700186 int stun_keepalive_interval_or_default() const;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800187};
188
zhihuangb2cdd932017-01-19 16:54:25 -0800189// TODO(zhihuang): Replace this with
190// PeerConnectionInterface::IceConnectionState.
zhihuangd06adf62017-01-12 15:58:31 -0800191enum class IceTransportState {
zhihuang86abd6f2016-12-19 11:54:05 -0800192 STATE_INIT,
193 STATE_CONNECTING, // Will enter this state once a connection is created
194 STATE_COMPLETED,
zhihuangd06adf62017-01-12 15:58:31 -0800195 STATE_FAILED
zhihuang86abd6f2016-12-19 11:54:05 -0800196};
197
198// TODO(zhihuang): Remove this once it's no longer used in
199// remoting/protocol/libjingle_transport_factory.cc
200enum IceProtocolType {
201 ICEPROTO_RFC5245 // Standard RFC 5245 version of ICE.
202};
203
204// IceTransportInternal is an internal abstract class that does ICE.
205// Once the public interface is supported,
Niels Möllerc6ec4b12018-10-24 14:10:53 +0200206// (https://www.w3.org/TR/webrtc/#rtcicetransport)
zhihuang86abd6f2016-12-19 11:54:05 -0800207// the IceTransportInterface will be split from this class.
Mirko Bonadeiac194142018-10-22 17:08:37 +0200208class RTC_EXPORT IceTransportInternal : public rtc::PacketTransportInternal {
zhihuang86abd6f2016-12-19 11:54:05 -0800209 public:
Steve Anton33f69db2017-10-30 10:01:15 -0700210 IceTransportInternal();
211 ~IceTransportInternal() override;
zhihuang86abd6f2016-12-19 11:54:05 -0800212
Jonas Olsson81125f02018-10-09 10:52:04 +0200213 // TODO(bugs.webrtc.org/9308): Remove GetState once all uses have been
214 // migrated to GetIceTransportState.
zhihuangd06adf62017-01-12 15:58:31 -0800215 virtual IceTransportState GetState() const = 0;
Jonas Olsson81125f02018-10-09 10:52:04 +0200216 virtual webrtc::IceTransportState GetIceTransportState() const = 0;
zhihuang86abd6f2016-12-19 11:54:05 -0800217
zhihuang86abd6f2016-12-19 11:54:05 -0800218 virtual int component() const = 0;
219
220 virtual IceRole GetIceRole() const = 0;
221
222 virtual void SetIceRole(IceRole role) = 0;
223
224 virtual void SetIceTiebreaker(uint64_t tiebreaker) = 0;
225
226 // TODO(zhihuang): Remove this once it's no longer called in
227 // remoting/protocol/libjingle_transport_factory.cc
228 virtual void SetIceProtocolType(IceProtocolType type) {}
229
230 virtual void SetIceCredentials(const std::string& ice_ufrag,
Steve Anton33f69db2017-10-30 10:01:15 -0700231 const std::string& ice_pwd);
zhihuang86abd6f2016-12-19 11:54:05 -0800232
233 virtual void SetRemoteIceCredentials(const std::string& ice_ufrag,
Steve Anton33f69db2017-10-30 10:01:15 -0700234 const std::string& ice_pwd);
zhihuang86abd6f2016-12-19 11:54:05 -0800235
236 // The ufrag and pwd in |ice_params| must be set
237 // before candidate gathering can start.
238 virtual void SetIceParameters(const IceParameters& ice_params) = 0;
239
240 virtual void SetRemoteIceParameters(const IceParameters& ice_params) = 0;
241
242 virtual void SetRemoteIceMode(IceMode mode) = 0;
243
244 virtual void SetIceConfig(const IceConfig& config) = 0;
245
246 // Start gathering candidates if not already started, or if an ICE restart
247 // occurred.
248 virtual void MaybeStartGathering() = 0;
249
zhihuang86abd6f2016-12-19 11:54:05 -0800250 virtual void AddRemoteCandidate(const Candidate& candidate) = 0;
251
252 virtual void RemoveRemoteCandidate(const Candidate& candidate) = 0;
253
Harald Alvestrand4241cf52019-01-21 09:29:42 +0100254 virtual void RemoveAllRemoteCandidates() = 0;
255
zhihuang86abd6f2016-12-19 11:54:05 -0800256 virtual IceGatheringState gathering_state() const = 0;
257
zhihuangd06adf62017-01-12 15:58:31 -0800258 // Returns the current stats for this connection.
Qingsi Wang72a43a12018-02-20 16:03:18 -0800259 virtual bool GetStats(ConnectionInfos* candidate_pair_stats_list,
260 CandidateStatsList* candidate_stats_list) = 0;
zhihuangd06adf62017-01-12 15:58:31 -0800261
skvladd0309122017-02-02 17:18:37 -0800262 // Returns RTT estimate over the currently active connection, or an empty
Danil Chapovalov00c71832018-06-15 15:58:38 +0200263 // absl::optional if there is none.
264 virtual absl::optional<int> GetRttEstimate() = 0;
skvladd0309122017-02-02 17:18:37 -0800265
Harald Alvestrand4241cf52019-01-21 09:29:42 +0100266 virtual const Connection* selected_connection() const = 0;
267
zhihuang86abd6f2016-12-19 11:54:05 -0800268 sigslot::signal1<IceTransportInternal*> SignalGatheringState;
269
270 // Handles sending and receiving of candidates.
271 sigslot::signal2<IceTransportInternal*, const Candidate&>
272 SignalCandidateGathered;
273
Eldar Relloda13ea22019-06-01 12:23:43 +0300274 sigslot::signal2<IceTransportInternal*, const IceCandidateErrorEvent&>
275 SignalCandidateError;
276
zhihuang86abd6f2016-12-19 11:54:05 -0800277 sigslot::signal2<IceTransportInternal*, const Candidates&>
278 SignalCandidatesRemoved;
279
Zhi Huang942bc2e2017-11-13 13:26:07 -0800280 // Deprecated by PacketTransportInternal::SignalNetworkRouteChanged.
zhihuang86abd6f2016-12-19 11:54:05 -0800281 // This signal occurs when there is a change in the way that packets are
282 // being routed, i.e. to a different remote location. The candidate
283 // indicates where and how we are currently sending media.
Zhi Huang942bc2e2017-11-13 13:26:07 -0800284 // TODO(zhihuang): Update the Chrome remoting to use the new
285 // SignalNetworkRouteChanged.
zhihuang86abd6f2016-12-19 11:54:05 -0800286 sigslot::signal2<IceTransportInternal*, const Candidate&> SignalRouteChange;
287
Alex Drake00c7ecf2019-08-06 10:54:47 -0700288 sigslot::signal1<const cricket::CandidatePairChangeEvent&>
289 SignalCandidatePairChanged;
290
zhihuangd06adf62017-01-12 15:58:31 -0800291 // Invoked when there is conflict in the ICE role between local and remote
292 // agents.
293 sigslot::signal1<IceTransportInternal*> SignalRoleConflict;
294
295 // Emitted whenever the transport state changed.
Jonas Olsson81125f02018-10-09 10:52:04 +0200296 // TODO(bugs.webrtc.org/9308): Remove once all uses have migrated to the new
297 // IceTransportState.
zhihuangd06adf62017-01-12 15:58:31 -0800298 sigslot::signal1<IceTransportInternal*> SignalStateChanged;
299
Jonas Olsson81125f02018-10-09 10:52:04 +0200300 // Emitted whenever the new standards-compliant transport state changed.
301 sigslot::signal1<IceTransportInternal*> SignalIceTransportStateChanged;
302
zhihuang86abd6f2016-12-19 11:54:05 -0800303 // Invoked when the transport is being destroyed.
304 sigslot::signal1<IceTransportInternal*> SignalDestroyed;
zhihuang86abd6f2016-12-19 11:54:05 -0800305};
306
307} // namespace cricket
zhihuangd06adf62017-01-12 15:58:31 -0800308
Steve Anton10542f22019-01-11 09:11:00 -0800309#endif // P2P_BASE_ICE_TRANSPORT_INTERNAL_H_