blob: 89b2107c5a4f5a1a4b3889eddd3c2f894b3858ff [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>
zhihuang86abd6f2016-12-19 11:54:05 -080015#include <string>
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080016#include <vector>
zhihuang86abd6f2016-12-19 11:54:05 -080017
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "absl/types/optional.h"
Patrik Höglunde2d6a062017-10-05 14:53:33 +020019#include "api/candidate.h"
Jonas Olsson81125f02018-10-09 10:52:04 +020020#include "api/transport/enums.h"
Jonas Orelande8e7d7b2019-05-29 09:30:55 +020021#include "p2p/base/connection.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "p2p/base/packet_transport_internal.h"
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080023#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "p2p/base/transport_description.h"
Yves Gerey3e707812018-11-28 16:47:49 +010025#include "rtc_base/network_constants.h"
Mirko Bonadeiac194142018-10-22 17:08:37 +020026#include "rtc_base/system/rtc_export.h"
Yves Gerey3e707812018-11-28 16:47:49 +010027#include "rtc_base/third_party/sigslot/sigslot.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "rtc_base/time_utils.h"
zhihuang86abd6f2016-12-19 11:54:05 -080029
zhihuang86abd6f2016-12-19 11:54:05 -080030namespace cricket {
31
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080032typedef std::vector<Candidate> Candidates;
33
Alex Loiko9289eda2018-11-23 16:18:59 +000034enum IceConnectionState {
35 kIceConnectionConnecting = 0,
36 kIceConnectionFailed,
37 kIceConnectionConnected, // Writable, but still checking one or more
38 // connections
39 kIceConnectionCompleted,
40};
41
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080042// TODO(deadbeef): Unify with PeerConnectionInterface::IceConnectionState
43// once /talk/ and /webrtc/ are combined, and also switch to ENUM_NAME naming
44// style.
45enum IceGatheringState {
46 kIceGatheringNew = 0,
47 kIceGatheringGathering,
48 kIceGatheringComplete,
49};
50
51enum ContinualGatheringPolicy {
52 // All port allocator sessions will stop after a writable connection is found.
53 GATHER_ONCE = 0,
54 // The most recent port allocator session will keep on running.
55 GATHER_CONTINUALLY,
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080056};
57
58// ICE Nomination mode.
59enum class NominationMode {
60 REGULAR, // Nominate once per ICE restart (Not implemented yet).
61 AGGRESSIVE, // Nominate every connection except that it will behave as if
62 // REGULAR when the remote is an ICE-LITE endpoint.
63 SEMI_AGGRESSIVE // Our current implementation of the nomination algorithm.
64 // The details are described in P2PTransportChannel.
65};
66
67// Information about ICE configuration.
Danil Chapovalov00c71832018-06-15 15:58:38 +020068// TODO(deadbeef): Use absl::optional to represent unset values, instead of
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080069// -1.
70struct IceConfig {
71 // The ICE connection receiving timeout value in milliseconds.
Danil Chapovalov00c71832018-06-15 15:58:38 +020072 absl::optional<int> receiving_timeout;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080073 // Time interval in milliseconds to ping a backup connection when the ICE
74 // channel is strongly connected.
Danil Chapovalov00c71832018-06-15 15:58:38 +020075 absl::optional<int> backup_connection_ping_interval;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080076
77 ContinualGatheringPolicy continual_gathering_policy = GATHER_ONCE;
78
79 bool gather_continually() const {
Qingsi Wang241d0c12018-06-05 16:44:10 -070080 return continual_gathering_policy == GATHER_CONTINUALLY;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080081 }
82
83 // Whether we should prioritize Relay/Relay candidate when nothing
84 // is writable yet.
85 bool prioritize_most_likely_candidate_pairs = false;
86
87 // Writable connections are pinged at a slower rate once stablized.
Danil Chapovalov00c71832018-06-15 15:58:38 +020088 absl::optional<int> stable_writable_connection_ping_interval;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080089
90 // If set to true, this means the ICE transport should presume TURN-to-TURN
91 // candidate pairs will succeed, even before a binding response is received.
92 bool presume_writable_when_fully_relayed = false;
93
Qingsi Wang1fe119f2019-05-31 16:55:33 -070094 // If true, after the ICE transport type (as the candidate filter used by the
95 // port allocator) is changed such that new types of ICE candidates are
96 // allowed by the new filter, e.g. from CF_RELAY to CF_ALL, candidates that
97 // have been gathered by the ICE transport but filtered out and not signaled
98 // to the upper layers, will be surfaced.
99 bool surface_ice_candidates_on_ice_transport_type_changed = false;
100
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800101 // Interval to check on all networks and to perform ICE regathering on any
102 // active network having no connection on it.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200103 absl::optional<int> regather_on_failed_networks_interval;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800104
105 // Interval to perform ICE regathering on all networks
106 // The delay in milliseconds is sampled from the uniform distribution [a, b]
Danil Chapovalov00c71832018-06-15 15:58:38 +0200107 absl::optional<rtc::IntervalRange> regather_all_networks_interval_range;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800108
109 // The time period in which we will not switch the selected connection
110 // when a new connection becomes receiving but the selected connection is not
111 // in case that the selected connection may become receiving soon.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200112 absl::optional<int> receiving_switching_delay;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800113
114 // TODO(honghaiz): Change the default to regular nomination.
115 // Default nomination mode if the remote does not support renomination.
116 NominationMode default_nomination_mode = NominationMode::SEMI_AGGRESSIVE;
117
Qingsi Wange6826d22018-03-08 14:55:14 -0800118 // The interval in milliseconds at which ICE checks (STUN pings) will be sent
119 // for a candidate pair when it is both writable and receiving (strong
120 // connectivity). This parameter overrides the default value given by
121 // |STRONG_PING_INTERVAL| in p2ptransport.h if set.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200122 absl::optional<int> ice_check_interval_strong_connectivity;
Qingsi Wange6826d22018-03-08 14:55:14 -0800123 // The interval in milliseconds at which ICE checks (STUN pings) will be sent
124 // for a candidate pair when it is either not writable or not receiving (weak
125 // connectivity). This parameter overrides the default value given by
126 // |WEAK_PING_INTERVAL| in p2ptransport.h if set.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200127 absl::optional<int> ice_check_interval_weak_connectivity;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800128 // ICE checks (STUN pings) will not be sent at higher rate (lower interval)
129 // than this, no matter what other settings there are.
130 // Measure in milliseconds.
Qingsi Wange6826d22018-03-08 14:55:14 -0800131 //
132 // Note that this parameter overrides both the above check intervals for
133 // candidate pairs with strong or weak connectivity, if either of the above
134 // interval is shorter than the min interval.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200135 absl::optional<int> ice_check_min_interval;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700136 // The min time period for which a candidate pair must wait for response to
137 // connectivity checks before it becomes unwritable. This parameter
138 // overrides the default value given by |CONNECTION_WRITE_CONNECT_TIMEOUT|
139 // in port.h if set, when determining the writability of a candidate pair.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200140 absl::optional<int> ice_unwritable_timeout;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700141
142 // The min number of connectivity checks that a candidate pair must sent
143 // without receiving response before it becomes unwritable. This parameter
144 // overrides the default value given by |CONNECTION_WRITE_CONNECT_FAILURES| in
145 // port.h if set, when determining the writability of a candidate pair.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200146 absl::optional<int> ice_unwritable_min_checks;
Jiawei Ou9d4fd5552018-12-06 23:30:17 -0800147
148 // The min time period for which a candidate pair must wait for response to
149 // connectivity checks it becomes inactive. This parameter overrides the
150 // default value given by |CONNECTION_WRITE_TIMEOUT| in port.h if set, when
151 // determining the writability of a candidate pair.
152 absl::optional<int> ice_inactive_timeout;
153
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800154 // The interval in milliseconds at which STUN candidates will resend STUN
155 // binding requests to keep NAT bindings open.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200156 absl::optional<int> stun_keepalive_interval;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800157
Danil Chapovalov00c71832018-06-15 15:58:38 +0200158 absl::optional<rtc::AdapterType> network_preference;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800159
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800160 IceConfig();
161 IceConfig(int receiving_timeout_ms,
162 int backup_connection_ping_interval,
163 ContinualGatheringPolicy gathering_policy,
164 bool prioritize_most_likely_candidate_pairs,
165 int stable_writable_connection_ping_interval_ms,
166 bool presume_writable_when_fully_relayed,
167 int regather_on_failed_networks_interval_ms,
Qingsi Wang866e08d2018-03-22 17:54:23 -0700168 int receiving_switching_delay_ms);
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800169 ~IceConfig();
Qingsi Wang866e08d2018-03-22 17:54:23 -0700170
171 // Helper getters for parameters with implementation-specific default value.
172 // By convention, parameters with default value are represented by
Danil Chapovalov00c71832018-06-15 15:58:38 +0200173 // absl::optional and setting a parameter to null restores its default value.
Qingsi Wang866e08d2018-03-22 17:54:23 -0700174 int receiving_timeout_or_default() const;
175 int backup_connection_ping_interval_or_default() const;
176 int stable_writable_connection_ping_interval_or_default() const;
177 int regather_on_failed_networks_interval_or_default() const;
178 int receiving_switching_delay_or_default() const;
179 int ice_check_interval_strong_connectivity_or_default() const;
180 int ice_check_interval_weak_connectivity_or_default() const;
181 int ice_check_min_interval_or_default() const;
182 int ice_unwritable_timeout_or_default() const;
183 int ice_unwritable_min_checks_or_default() const;
Jiawei Ou9d4fd5552018-12-06 23:30:17 -0800184 int ice_inactive_timeout_or_default() const;
Qingsi Wang866e08d2018-03-22 17:54:23 -0700185 int stun_keepalive_interval_or_default() const;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800186};
187
zhihuangb2cdd932017-01-19 16:54:25 -0800188// TODO(zhihuang): Replace this with
189// PeerConnectionInterface::IceConnectionState.
zhihuangd06adf62017-01-12 15:58:31 -0800190enum class IceTransportState {
zhihuang86abd6f2016-12-19 11:54:05 -0800191 STATE_INIT,
192 STATE_CONNECTING, // Will enter this state once a connection is created
193 STATE_COMPLETED,
zhihuangd06adf62017-01-12 15:58:31 -0800194 STATE_FAILED
zhihuang86abd6f2016-12-19 11:54:05 -0800195};
196
197// TODO(zhihuang): Remove this once it's no longer used in
198// remoting/protocol/libjingle_transport_factory.cc
199enum IceProtocolType {
200 ICEPROTO_RFC5245 // Standard RFC 5245 version of ICE.
201};
202
203// IceTransportInternal is an internal abstract class that does ICE.
204// Once the public interface is supported,
Niels Möllerc6ec4b12018-10-24 14:10:53 +0200205// (https://www.w3.org/TR/webrtc/#rtcicetransport)
zhihuang86abd6f2016-12-19 11:54:05 -0800206// the IceTransportInterface will be split from this class.
Mirko Bonadeiac194142018-10-22 17:08:37 +0200207class RTC_EXPORT IceTransportInternal : public rtc::PacketTransportInternal {
zhihuang86abd6f2016-12-19 11:54:05 -0800208 public:
Steve Anton33f69db2017-10-30 10:01:15 -0700209 IceTransportInternal();
210 ~IceTransportInternal() override;
zhihuang86abd6f2016-12-19 11:54:05 -0800211
Jonas Olsson81125f02018-10-09 10:52:04 +0200212 // TODO(bugs.webrtc.org/9308): Remove GetState once all uses have been
213 // migrated to GetIceTransportState.
zhihuangd06adf62017-01-12 15:58:31 -0800214 virtual IceTransportState GetState() const = 0;
Jonas Olsson81125f02018-10-09 10:52:04 +0200215 virtual webrtc::IceTransportState GetIceTransportState() const = 0;
zhihuang86abd6f2016-12-19 11:54:05 -0800216
zhihuang86abd6f2016-12-19 11:54:05 -0800217 virtual int component() const = 0;
218
219 virtual IceRole GetIceRole() const = 0;
220
221 virtual void SetIceRole(IceRole role) = 0;
222
223 virtual void SetIceTiebreaker(uint64_t tiebreaker) = 0;
224
225 // TODO(zhihuang): Remove this once it's no longer called in
226 // remoting/protocol/libjingle_transport_factory.cc
227 virtual void SetIceProtocolType(IceProtocolType type) {}
228
229 virtual void SetIceCredentials(const std::string& ice_ufrag,
Steve Anton33f69db2017-10-30 10:01:15 -0700230 const std::string& ice_pwd);
zhihuang86abd6f2016-12-19 11:54:05 -0800231
232 virtual void SetRemoteIceCredentials(const std::string& ice_ufrag,
Steve Anton33f69db2017-10-30 10:01:15 -0700233 const std::string& ice_pwd);
zhihuang86abd6f2016-12-19 11:54:05 -0800234
235 // The ufrag and pwd in |ice_params| must be set
236 // before candidate gathering can start.
237 virtual void SetIceParameters(const IceParameters& ice_params) = 0;
238
239 virtual void SetRemoteIceParameters(const IceParameters& ice_params) = 0;
240
241 virtual void SetRemoteIceMode(IceMode mode) = 0;
242
243 virtual void SetIceConfig(const IceConfig& config) = 0;
244
245 // Start gathering candidates if not already started, or if an ICE restart
246 // occurred.
247 virtual void MaybeStartGathering() = 0;
248
zhihuang86abd6f2016-12-19 11:54:05 -0800249 virtual void AddRemoteCandidate(const Candidate& candidate) = 0;
250
251 virtual void RemoveRemoteCandidate(const Candidate& candidate) = 0;
252
Harald Alvestrand4241cf52019-01-21 09:29:42 +0100253 virtual void RemoveAllRemoteCandidates() = 0;
254
zhihuang86abd6f2016-12-19 11:54:05 -0800255 virtual IceGatheringState gathering_state() const = 0;
256
zhihuangd06adf62017-01-12 15:58:31 -0800257 // Returns the current stats for this connection.
Qingsi Wang72a43a12018-02-20 16:03:18 -0800258 virtual bool GetStats(ConnectionInfos* candidate_pair_stats_list,
259 CandidateStatsList* candidate_stats_list) = 0;
zhihuangd06adf62017-01-12 15:58:31 -0800260
skvladd0309122017-02-02 17:18:37 -0800261 // Returns RTT estimate over the currently active connection, or an empty
Danil Chapovalov00c71832018-06-15 15:58:38 +0200262 // absl::optional if there is none.
263 virtual absl::optional<int> GetRttEstimate() = 0;
skvladd0309122017-02-02 17:18:37 -0800264
Harald Alvestrand4241cf52019-01-21 09:29:42 +0100265 virtual const Connection* selected_connection() const = 0;
266
zhihuang86abd6f2016-12-19 11:54:05 -0800267 sigslot::signal1<IceTransportInternal*> SignalGatheringState;
268
269 // Handles sending and receiving of candidates.
270 sigslot::signal2<IceTransportInternal*, const Candidate&>
271 SignalCandidateGathered;
272
273 sigslot::signal2<IceTransportInternal*, const Candidates&>
274 SignalCandidatesRemoved;
275
Zhi Huang942bc2e2017-11-13 13:26:07 -0800276 // Deprecated by PacketTransportInternal::SignalNetworkRouteChanged.
zhihuang86abd6f2016-12-19 11:54:05 -0800277 // This signal occurs when there is a change in the way that packets are
278 // being routed, i.e. to a different remote location. The candidate
279 // indicates where and how we are currently sending media.
Zhi Huang942bc2e2017-11-13 13:26:07 -0800280 // TODO(zhihuang): Update the Chrome remoting to use the new
281 // SignalNetworkRouteChanged.
zhihuang86abd6f2016-12-19 11:54:05 -0800282 sigslot::signal2<IceTransportInternal*, const Candidate&> SignalRouteChange;
283
zhihuangd06adf62017-01-12 15:58:31 -0800284 // Invoked when there is conflict in the ICE role between local and remote
285 // agents.
286 sigslot::signal1<IceTransportInternal*> SignalRoleConflict;
287
288 // Emitted whenever the transport state changed.
Jonas Olsson81125f02018-10-09 10:52:04 +0200289 // TODO(bugs.webrtc.org/9308): Remove once all uses have migrated to the new
290 // IceTransportState.
zhihuangd06adf62017-01-12 15:58:31 -0800291 sigslot::signal1<IceTransportInternal*> SignalStateChanged;
292
Jonas Olsson81125f02018-10-09 10:52:04 +0200293 // Emitted whenever the new standards-compliant transport state changed.
294 sigslot::signal1<IceTransportInternal*> SignalIceTransportStateChanged;
295
zhihuang86abd6f2016-12-19 11:54:05 -0800296 // Invoked when the transport is being destroyed.
297 sigslot::signal1<IceTransportInternal*> SignalDestroyed;
zhihuang86abd6f2016-12-19 11:54:05 -0800298};
299
300} // namespace cricket
zhihuangd06adf62017-01-12 15:58:31 -0800301
Steve Anton10542f22019-01-11 09:11:00 -0800302#endif // P2P_BASE_ICE_TRANSPORT_INTERNAL_H_