blob: b360f7917ec0d7eb2f72129e1d76caf177cdcdac [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef P2P_BASE_ICETRANSPORTINTERNAL_H_
12#define P2P_BASE_ICETRANSPORTINTERNAL_H_
zhihuang86abd6f2016-12-19 11:54:05 -080013
14#include <string>
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080015#include <vector>
zhihuang86abd6f2016-12-19 11:54:05 -080016
Patrik Höglunde2d6a062017-10-05 14:53:33 +020017#include "api/candidate.h"
Jonas Olsson81125f02018-10-09 10:52:04 +020018#include "api/transport/enums.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "p2p/base/candidatepairinterface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "p2p/base/packettransportinternal.h"
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080021#include "p2p/base/port.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "p2p/base/transportdescription.h"
Mirko Bonadeiac194142018-10-22 17:08:37 +020023#include "rtc_base/system/rtc_export.h"
zhihuang86abd6f2016-12-19 11:54:05 -080024
zhihuang86abd6f2016-12-19 11:54:05 -080025namespace cricket {
26
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080027typedef std::vector<Candidate> Candidates;
28
29enum IceConnectionState {
30 kIceConnectionConnecting = 0,
31 kIceConnectionFailed,
32 kIceConnectionConnected, // Writable, but still checking one or more
33 // connections
34 kIceConnectionCompleted,
35};
36
37// TODO(deadbeef): Unify with PeerConnectionInterface::IceConnectionState
38// once /talk/ and /webrtc/ are combined, and also switch to ENUM_NAME naming
39// style.
40enum IceGatheringState {
41 kIceGatheringNew = 0,
42 kIceGatheringGathering,
43 kIceGatheringComplete,
44};
45
46enum ContinualGatheringPolicy {
47 // All port allocator sessions will stop after a writable connection is found.
48 GATHER_ONCE = 0,
49 // The most recent port allocator session will keep on running.
50 GATHER_CONTINUALLY,
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080051};
52
53// ICE Nomination mode.
54enum class NominationMode {
55 REGULAR, // Nominate once per ICE restart (Not implemented yet).
56 AGGRESSIVE, // Nominate every connection except that it will behave as if
57 // REGULAR when the remote is an ICE-LITE endpoint.
58 SEMI_AGGRESSIVE // Our current implementation of the nomination algorithm.
59 // The details are described in P2PTransportChannel.
60};
61
62// Information about ICE configuration.
Danil Chapovalov00c71832018-06-15 15:58:38 +020063// TODO(deadbeef): Use absl::optional to represent unset values, instead of
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080064// -1.
65struct IceConfig {
66 // The ICE connection receiving timeout value in milliseconds.
Danil Chapovalov00c71832018-06-15 15:58:38 +020067 absl::optional<int> receiving_timeout;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080068 // Time interval in milliseconds to ping a backup connection when the ICE
69 // channel is strongly connected.
Danil Chapovalov00c71832018-06-15 15:58:38 +020070 absl::optional<int> backup_connection_ping_interval;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080071
72 ContinualGatheringPolicy continual_gathering_policy = GATHER_ONCE;
73
74 bool gather_continually() const {
Qingsi Wang241d0c12018-06-05 16:44:10 -070075 return continual_gathering_policy == GATHER_CONTINUALLY;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080076 }
77
78 // Whether we should prioritize Relay/Relay candidate when nothing
79 // is writable yet.
80 bool prioritize_most_likely_candidate_pairs = false;
81
82 // Writable connections are pinged at a slower rate once stablized.
Danil Chapovalov00c71832018-06-15 15:58:38 +020083 absl::optional<int> stable_writable_connection_ping_interval;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080084
85 // If set to true, this means the ICE transport should presume TURN-to-TURN
86 // candidate pairs will succeed, even before a binding response is received.
87 bool presume_writable_when_fully_relayed = false;
88
89 // Interval to check on all networks and to perform ICE regathering on any
90 // active network having no connection on it.
Danil Chapovalov00c71832018-06-15 15:58:38 +020091 absl::optional<int> regather_on_failed_networks_interval;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080092
93 // Interval to perform ICE regathering on all networks
94 // The delay in milliseconds is sampled from the uniform distribution [a, b]
Danil Chapovalov00c71832018-06-15 15:58:38 +020095 absl::optional<rtc::IntervalRange> regather_all_networks_interval_range;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080096
97 // The time period in which we will not switch the selected connection
98 // when a new connection becomes receiving but the selected connection is not
99 // in case that the selected connection may become receiving soon.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200100 absl::optional<int> receiving_switching_delay;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800101
102 // TODO(honghaiz): Change the default to regular nomination.
103 // Default nomination mode if the remote does not support renomination.
104 NominationMode default_nomination_mode = NominationMode::SEMI_AGGRESSIVE;
105
Qingsi Wange6826d22018-03-08 14:55:14 -0800106 // The interval in milliseconds at which ICE checks (STUN pings) will be sent
107 // for a candidate pair when it is both writable and receiving (strong
108 // connectivity). This parameter overrides the default value given by
109 // |STRONG_PING_INTERVAL| in p2ptransport.h if set.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200110 absl::optional<int> ice_check_interval_strong_connectivity;
Qingsi Wange6826d22018-03-08 14:55:14 -0800111 // The interval in milliseconds at which ICE checks (STUN pings) will be sent
112 // for a candidate pair when it is either not writable or not receiving (weak
113 // connectivity). This parameter overrides the default value given by
114 // |WEAK_PING_INTERVAL| in p2ptransport.h if set.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200115 absl::optional<int> ice_check_interval_weak_connectivity;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800116 // ICE checks (STUN pings) will not be sent at higher rate (lower interval)
117 // than this, no matter what other settings there are.
118 // Measure in milliseconds.
Qingsi Wange6826d22018-03-08 14:55:14 -0800119 //
120 // Note that this parameter overrides both the above check intervals for
121 // candidate pairs with strong or weak connectivity, if either of the above
122 // interval is shorter than the min interval.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200123 absl::optional<int> ice_check_min_interval;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700124 // The min time period for which a candidate pair must wait for response to
125 // connectivity checks before it becomes unwritable. This parameter
126 // overrides the default value given by |CONNECTION_WRITE_CONNECT_TIMEOUT|
127 // in port.h if set, when determining the writability of a candidate pair.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200128 absl::optional<int> ice_unwritable_timeout;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700129
130 // The min number of connectivity checks that a candidate pair must sent
131 // without receiving response before it becomes unwritable. This parameter
132 // overrides the default value given by |CONNECTION_WRITE_CONNECT_FAILURES| in
133 // port.h if set, when determining the writability of a candidate pair.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200134 absl::optional<int> ice_unwritable_min_checks;
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800135 // The interval in milliseconds at which STUN candidates will resend STUN
136 // binding requests to keep NAT bindings open.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200137 absl::optional<int> stun_keepalive_interval;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800138
Danil Chapovalov00c71832018-06-15 15:58:38 +0200139 absl::optional<rtc::AdapterType> network_preference;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800140
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800141 IceConfig();
142 IceConfig(int receiving_timeout_ms,
143 int backup_connection_ping_interval,
144 ContinualGatheringPolicy gathering_policy,
145 bool prioritize_most_likely_candidate_pairs,
146 int stable_writable_connection_ping_interval_ms,
147 bool presume_writable_when_fully_relayed,
148 int regather_on_failed_networks_interval_ms,
Qingsi Wang866e08d2018-03-22 17:54:23 -0700149 int receiving_switching_delay_ms);
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800150 ~IceConfig();
Qingsi Wang866e08d2018-03-22 17:54:23 -0700151
152 // Helper getters for parameters with implementation-specific default value.
153 // By convention, parameters with default value are represented by
Danil Chapovalov00c71832018-06-15 15:58:38 +0200154 // absl::optional and setting a parameter to null restores its default value.
Qingsi Wang866e08d2018-03-22 17:54:23 -0700155 int receiving_timeout_or_default() const;
156 int backup_connection_ping_interval_or_default() const;
157 int stable_writable_connection_ping_interval_or_default() const;
158 int regather_on_failed_networks_interval_or_default() const;
159 int receiving_switching_delay_or_default() const;
160 int ice_check_interval_strong_connectivity_or_default() const;
161 int ice_check_interval_weak_connectivity_or_default() const;
162 int ice_check_min_interval_or_default() const;
163 int ice_unwritable_timeout_or_default() const;
164 int ice_unwritable_min_checks_or_default() const;
165 int stun_keepalive_interval_or_default() const;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800166};
167
zhihuangb2cdd932017-01-19 16:54:25 -0800168// TODO(zhihuang): Replace this with
169// PeerConnectionInterface::IceConnectionState.
zhihuangd06adf62017-01-12 15:58:31 -0800170enum class IceTransportState {
zhihuang86abd6f2016-12-19 11:54:05 -0800171 STATE_INIT,
172 STATE_CONNECTING, // Will enter this state once a connection is created
173 STATE_COMPLETED,
zhihuangd06adf62017-01-12 15:58:31 -0800174 STATE_FAILED
zhihuang86abd6f2016-12-19 11:54:05 -0800175};
176
177// TODO(zhihuang): Remove this once it's no longer used in
178// remoting/protocol/libjingle_transport_factory.cc
179enum IceProtocolType {
180 ICEPROTO_RFC5245 // Standard RFC 5245 version of ICE.
181};
182
183// IceTransportInternal is an internal abstract class that does ICE.
184// Once the public interface is supported,
185// (https://www.w3.org/TR/webrtc/#rtcicetransport-interface)
186// the IceTransportInterface will be split from this class.
Mirko Bonadeiac194142018-10-22 17:08:37 +0200187class RTC_EXPORT IceTransportInternal : public rtc::PacketTransportInternal {
zhihuang86abd6f2016-12-19 11:54:05 -0800188 public:
Steve Anton33f69db2017-10-30 10:01:15 -0700189 IceTransportInternal();
190 ~IceTransportInternal() override;
zhihuang86abd6f2016-12-19 11:54:05 -0800191
Jonas Olsson81125f02018-10-09 10:52:04 +0200192 // TODO(bugs.webrtc.org/9308): Remove GetState once all uses have been
193 // migrated to GetIceTransportState.
zhihuangd06adf62017-01-12 15:58:31 -0800194 virtual IceTransportState GetState() const = 0;
Jonas Olsson81125f02018-10-09 10:52:04 +0200195 virtual webrtc::IceTransportState GetIceTransportState() const = 0;
zhihuang86abd6f2016-12-19 11:54:05 -0800196
zhihuang86abd6f2016-12-19 11:54:05 -0800197 virtual int component() const = 0;
198
199 virtual IceRole GetIceRole() const = 0;
200
201 virtual void SetIceRole(IceRole role) = 0;
202
203 virtual void SetIceTiebreaker(uint64_t tiebreaker) = 0;
204
205 // TODO(zhihuang): Remove this once it's no longer called in
206 // remoting/protocol/libjingle_transport_factory.cc
207 virtual void SetIceProtocolType(IceProtocolType type) {}
208
209 virtual void SetIceCredentials(const std::string& ice_ufrag,
Steve Anton33f69db2017-10-30 10:01:15 -0700210 const std::string& ice_pwd);
zhihuang86abd6f2016-12-19 11:54:05 -0800211
212 virtual void SetRemoteIceCredentials(const std::string& ice_ufrag,
Steve Anton33f69db2017-10-30 10:01:15 -0700213 const std::string& ice_pwd);
zhihuang86abd6f2016-12-19 11:54:05 -0800214
215 // The ufrag and pwd in |ice_params| must be set
216 // before candidate gathering can start.
217 virtual void SetIceParameters(const IceParameters& ice_params) = 0;
218
219 virtual void SetRemoteIceParameters(const IceParameters& ice_params) = 0;
220
221 virtual void SetRemoteIceMode(IceMode mode) = 0;
222
223 virtual void SetIceConfig(const IceConfig& config) = 0;
224
225 // Start gathering candidates if not already started, or if an ICE restart
226 // occurred.
227 virtual void MaybeStartGathering() = 0;
228
zhihuang86abd6f2016-12-19 11:54:05 -0800229 virtual void AddRemoteCandidate(const Candidate& candidate) = 0;
230
231 virtual void RemoveRemoteCandidate(const Candidate& candidate) = 0;
232
233 virtual IceGatheringState gathering_state() const = 0;
234
zhihuangd06adf62017-01-12 15:58:31 -0800235 // Returns the current stats for this connection.
Qingsi Wang72a43a12018-02-20 16:03:18 -0800236 virtual bool GetStats(ConnectionInfos* candidate_pair_stats_list,
237 CandidateStatsList* candidate_stats_list) = 0;
zhihuangd06adf62017-01-12 15:58:31 -0800238
skvladd0309122017-02-02 17:18:37 -0800239 // Returns RTT estimate over the currently active connection, or an empty
Danil Chapovalov00c71832018-06-15 15:58:38 +0200240 // absl::optional if there is none.
241 virtual absl::optional<int> GetRttEstimate() = 0;
skvladd0309122017-02-02 17:18:37 -0800242
zhihuang86abd6f2016-12-19 11:54:05 -0800243 sigslot::signal1<IceTransportInternal*> SignalGatheringState;
244
245 // Handles sending and receiving of candidates.
246 sigslot::signal2<IceTransportInternal*, const Candidate&>
247 SignalCandidateGathered;
248
249 sigslot::signal2<IceTransportInternal*, const Candidates&>
250 SignalCandidatesRemoved;
251
Zhi Huang942bc2e2017-11-13 13:26:07 -0800252 // Deprecated by PacketTransportInternal::SignalNetworkRouteChanged.
zhihuang86abd6f2016-12-19 11:54:05 -0800253 // This signal occurs when there is a change in the way that packets are
254 // being routed, i.e. to a different remote location. The candidate
255 // indicates where and how we are currently sending media.
Zhi Huang942bc2e2017-11-13 13:26:07 -0800256 // TODO(zhihuang): Update the Chrome remoting to use the new
257 // SignalNetworkRouteChanged.
zhihuang86abd6f2016-12-19 11:54:05 -0800258 sigslot::signal2<IceTransportInternal*, const Candidate&> SignalRouteChange;
259
zhihuangd06adf62017-01-12 15:58:31 -0800260 // Invoked when there is conflict in the ICE role between local and remote
261 // agents.
262 sigslot::signal1<IceTransportInternal*> SignalRoleConflict;
263
264 // Emitted whenever the transport state changed.
Jonas Olsson81125f02018-10-09 10:52:04 +0200265 // TODO(bugs.webrtc.org/9308): Remove once all uses have migrated to the new
266 // IceTransportState.
zhihuangd06adf62017-01-12 15:58:31 -0800267 sigslot::signal1<IceTransportInternal*> SignalStateChanged;
268
Jonas Olsson81125f02018-10-09 10:52:04 +0200269 // Emitted whenever the new standards-compliant transport state changed.
270 sigslot::signal1<IceTransportInternal*> SignalIceTransportStateChanged;
271
zhihuang86abd6f2016-12-19 11:54:05 -0800272 // Invoked when the transport is being destroyed.
273 sigslot::signal1<IceTransportInternal*> SignalDestroyed;
zhihuang86abd6f2016-12-19 11:54:05 -0800274};
275
276} // namespace cricket
zhihuangd06adf62017-01-12 15:58:31 -0800277
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200278#endif // P2P_BASE_ICETRANSPORTINTERNAL_H_