blob: f3da45a7874258f13a3babc52a069e7bf117f84e [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"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "p2p/base/packet_transport_internal.h"
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080022#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "p2p/base/transport_description.h"
Yves Gerey3e707812018-11-28 16:47:49 +010024#include "rtc_base/network_constants.h"
Mirko Bonadeiac194142018-10-22 17:08:37 +020025#include "rtc_base/system/rtc_export.h"
Yves Gerey3e707812018-11-28 16:47:49 +010026#include "rtc_base/third_party/sigslot/sigslot.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "rtc_base/time_utils.h"
zhihuang86abd6f2016-12-19 11:54:05 -080028
zhihuang86abd6f2016-12-19 11:54:05 -080029namespace cricket {
30
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080031typedef std::vector<Candidate> Candidates;
32
Alex Loiko9289eda2018-11-23 16:18:59 +000033enum IceConnectionState {
34 kIceConnectionConnecting = 0,
35 kIceConnectionFailed,
36 kIceConnectionConnected, // Writable, but still checking one or more
37 // connections
38 kIceConnectionCompleted,
39};
40
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080041// TODO(deadbeef): Unify with PeerConnectionInterface::IceConnectionState
42// once /talk/ and /webrtc/ are combined, and also switch to ENUM_NAME naming
43// style.
44enum IceGatheringState {
45 kIceGatheringNew = 0,
46 kIceGatheringGathering,
47 kIceGatheringComplete,
48};
49
50enum ContinualGatheringPolicy {
51 // All port allocator sessions will stop after a writable connection is found.
52 GATHER_ONCE = 0,
53 // The most recent port allocator session will keep on running.
54 GATHER_CONTINUALLY,
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080055};
56
57// ICE Nomination mode.
58enum class NominationMode {
59 REGULAR, // Nominate once per ICE restart (Not implemented yet).
60 AGGRESSIVE, // Nominate every connection except that it will behave as if
61 // REGULAR when the remote is an ICE-LITE endpoint.
62 SEMI_AGGRESSIVE // Our current implementation of the nomination algorithm.
63 // The details are described in P2PTransportChannel.
64};
65
66// Information about ICE configuration.
Danil Chapovalov00c71832018-06-15 15:58:38 +020067// TODO(deadbeef): Use absl::optional to represent unset values, instead of
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080068// -1.
69struct IceConfig {
70 // The ICE connection receiving timeout value in milliseconds.
Danil Chapovalov00c71832018-06-15 15:58:38 +020071 absl::optional<int> receiving_timeout;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080072 // Time interval in milliseconds to ping a backup connection when the ICE
73 // channel is strongly connected.
Danil Chapovalov00c71832018-06-15 15:58:38 +020074 absl::optional<int> backup_connection_ping_interval;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080075
76 ContinualGatheringPolicy continual_gathering_policy = GATHER_ONCE;
77
78 bool gather_continually() const {
Qingsi Wang241d0c12018-06-05 16:44:10 -070079 return continual_gathering_policy == GATHER_CONTINUALLY;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080080 }
81
82 // Whether we should prioritize Relay/Relay candidate when nothing
83 // is writable yet.
84 bool prioritize_most_likely_candidate_pairs = false;
85
86 // Writable connections are pinged at a slower rate once stablized.
Danil Chapovalov00c71832018-06-15 15:58:38 +020087 absl::optional<int> stable_writable_connection_ping_interval;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080088
89 // If set to true, this means the ICE transport should presume TURN-to-TURN
90 // candidate pairs will succeed, even before a binding response is received.
91 bool presume_writable_when_fully_relayed = false;
92
93 // Interval to check on all networks and to perform ICE regathering on any
94 // active network having no connection on it.
Danil Chapovalov00c71832018-06-15 15:58:38 +020095 absl::optional<int> regather_on_failed_networks_interval;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -080096
97 // Interval to perform ICE regathering on all networks
98 // The delay in milliseconds is sampled from the uniform distribution [a, b]
Danil Chapovalov00c71832018-06-15 15:58:38 +020099 absl::optional<rtc::IntervalRange> regather_all_networks_interval_range;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800100
101 // The time period in which we will not switch the selected connection
102 // when a new connection becomes receiving but the selected connection is not
103 // in case that the selected connection may become receiving soon.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200104 absl::optional<int> receiving_switching_delay;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800105
106 // TODO(honghaiz): Change the default to regular nomination.
107 // Default nomination mode if the remote does not support renomination.
108 NominationMode default_nomination_mode = NominationMode::SEMI_AGGRESSIVE;
109
Qingsi Wange6826d22018-03-08 14:55:14 -0800110 // The interval in milliseconds at which ICE checks (STUN pings) will be sent
111 // for a candidate pair when it is both writable and receiving (strong
112 // connectivity). This parameter overrides the default value given by
113 // |STRONG_PING_INTERVAL| in p2ptransport.h if set.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200114 absl::optional<int> ice_check_interval_strong_connectivity;
Qingsi Wange6826d22018-03-08 14:55:14 -0800115 // The interval in milliseconds at which ICE checks (STUN pings) will be sent
116 // for a candidate pair when it is either not writable or not receiving (weak
117 // connectivity). This parameter overrides the default value given by
118 // |WEAK_PING_INTERVAL| in p2ptransport.h if set.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200119 absl::optional<int> ice_check_interval_weak_connectivity;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800120 // ICE checks (STUN pings) will not be sent at higher rate (lower interval)
121 // than this, no matter what other settings there are.
122 // Measure in milliseconds.
Qingsi Wange6826d22018-03-08 14:55:14 -0800123 //
124 // Note that this parameter overrides both the above check intervals for
125 // candidate pairs with strong or weak connectivity, if either of the above
126 // interval is shorter than the min interval.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200127 absl::optional<int> ice_check_min_interval;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700128 // The min time period for which a candidate pair must wait for response to
129 // connectivity checks before it becomes unwritable. This parameter
130 // overrides the default value given by |CONNECTION_WRITE_CONNECT_TIMEOUT|
131 // in port.h if set, when determining the writability of a candidate pair.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200132 absl::optional<int> ice_unwritable_timeout;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700133
134 // The min number of connectivity checks that a candidate pair must sent
135 // without receiving response before it becomes unwritable. This parameter
136 // overrides the default value given by |CONNECTION_WRITE_CONNECT_FAILURES| in
137 // port.h if set, when determining the writability of a candidate pair.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200138 absl::optional<int> ice_unwritable_min_checks;
Jiawei Ou9d4fd5552018-12-06 23:30:17 -0800139
140 // The min time period for which a candidate pair must wait for response to
141 // connectivity checks it becomes inactive. This parameter overrides the
142 // default value given by |CONNECTION_WRITE_TIMEOUT| in port.h if set, when
143 // determining the writability of a candidate pair.
144 absl::optional<int> ice_inactive_timeout;
145
Qingsi Wangdb53f8e2018-02-20 14:45:49 -0800146 // The interval in milliseconds at which STUN candidates will resend STUN
147 // binding requests to keep NAT bindings open.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200148 absl::optional<int> stun_keepalive_interval;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800149
Danil Chapovalov00c71832018-06-15 15:58:38 +0200150 absl::optional<rtc::AdapterType> network_preference;
Qingsi Wang9a5c6f82018-02-01 10:38:40 -0800151
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800152 IceConfig();
153 IceConfig(int receiving_timeout_ms,
154 int backup_connection_ping_interval,
155 ContinualGatheringPolicy gathering_policy,
156 bool prioritize_most_likely_candidate_pairs,
157 int stable_writable_connection_ping_interval_ms,
158 bool presume_writable_when_fully_relayed,
159 int regather_on_failed_networks_interval_ms,
Qingsi Wang866e08d2018-03-22 17:54:23 -0700160 int receiving_switching_delay_ms);
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800161 ~IceConfig();
Qingsi Wang866e08d2018-03-22 17:54:23 -0700162
163 // Helper getters for parameters with implementation-specific default value.
164 // By convention, parameters with default value are represented by
Danil Chapovalov00c71832018-06-15 15:58:38 +0200165 // absl::optional and setting a parameter to null restores its default value.
Qingsi Wang866e08d2018-03-22 17:54:23 -0700166 int receiving_timeout_or_default() const;
167 int backup_connection_ping_interval_or_default() const;
168 int stable_writable_connection_ping_interval_or_default() const;
169 int regather_on_failed_networks_interval_or_default() const;
170 int receiving_switching_delay_or_default() const;
171 int ice_check_interval_strong_connectivity_or_default() const;
172 int ice_check_interval_weak_connectivity_or_default() const;
173 int ice_check_min_interval_or_default() const;
174 int ice_unwritable_timeout_or_default() const;
175 int ice_unwritable_min_checks_or_default() const;
Jiawei Ou9d4fd5552018-12-06 23:30:17 -0800176 int ice_inactive_timeout_or_default() const;
Qingsi Wang866e08d2018-03-22 17:54:23 -0700177 int stun_keepalive_interval_or_default() const;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800178};
179
zhihuangb2cdd932017-01-19 16:54:25 -0800180// TODO(zhihuang): Replace this with
181// PeerConnectionInterface::IceConnectionState.
zhihuangd06adf62017-01-12 15:58:31 -0800182enum class IceTransportState {
zhihuang86abd6f2016-12-19 11:54:05 -0800183 STATE_INIT,
184 STATE_CONNECTING, // Will enter this state once a connection is created
185 STATE_COMPLETED,
zhihuangd06adf62017-01-12 15:58:31 -0800186 STATE_FAILED
zhihuang86abd6f2016-12-19 11:54:05 -0800187};
188
189// TODO(zhihuang): Remove this once it's no longer used in
190// remoting/protocol/libjingle_transport_factory.cc
191enum IceProtocolType {
192 ICEPROTO_RFC5245 // Standard RFC 5245 version of ICE.
193};
194
195// IceTransportInternal is an internal abstract class that does ICE.
196// Once the public interface is supported,
Niels Möllerc6ec4b12018-10-24 14:10:53 +0200197// (https://www.w3.org/TR/webrtc/#rtcicetransport)
zhihuang86abd6f2016-12-19 11:54:05 -0800198// the IceTransportInterface will be split from this class.
Mirko Bonadeiac194142018-10-22 17:08:37 +0200199class RTC_EXPORT IceTransportInternal : public rtc::PacketTransportInternal {
zhihuang86abd6f2016-12-19 11:54:05 -0800200 public:
Steve Anton33f69db2017-10-30 10:01:15 -0700201 IceTransportInternal();
202 ~IceTransportInternal() override;
zhihuang86abd6f2016-12-19 11:54:05 -0800203
Jonas Olsson81125f02018-10-09 10:52:04 +0200204 // TODO(bugs.webrtc.org/9308): Remove GetState once all uses have been
205 // migrated to GetIceTransportState.
zhihuangd06adf62017-01-12 15:58:31 -0800206 virtual IceTransportState GetState() const = 0;
Jonas Olsson81125f02018-10-09 10:52:04 +0200207 virtual webrtc::IceTransportState GetIceTransportState() const = 0;
zhihuang86abd6f2016-12-19 11:54:05 -0800208
zhihuang86abd6f2016-12-19 11:54:05 -0800209 virtual int component() const = 0;
210
211 virtual IceRole GetIceRole() const = 0;
212
213 virtual void SetIceRole(IceRole role) = 0;
214
215 virtual void SetIceTiebreaker(uint64_t tiebreaker) = 0;
216
217 // TODO(zhihuang): Remove this once it's no longer called in
218 // remoting/protocol/libjingle_transport_factory.cc
219 virtual void SetIceProtocolType(IceProtocolType type) {}
220
221 virtual void SetIceCredentials(const std::string& ice_ufrag,
Steve Anton33f69db2017-10-30 10:01:15 -0700222 const std::string& ice_pwd);
zhihuang86abd6f2016-12-19 11:54:05 -0800223
224 virtual void SetRemoteIceCredentials(const std::string& ice_ufrag,
Steve Anton33f69db2017-10-30 10:01:15 -0700225 const std::string& ice_pwd);
zhihuang86abd6f2016-12-19 11:54:05 -0800226
227 // The ufrag and pwd in |ice_params| must be set
228 // before candidate gathering can start.
229 virtual void SetIceParameters(const IceParameters& ice_params) = 0;
230
231 virtual void SetRemoteIceParameters(const IceParameters& ice_params) = 0;
232
233 virtual void SetRemoteIceMode(IceMode mode) = 0;
234
235 virtual void SetIceConfig(const IceConfig& config) = 0;
236
237 // Start gathering candidates if not already started, or if an ICE restart
238 // occurred.
239 virtual void MaybeStartGathering() = 0;
240
zhihuang86abd6f2016-12-19 11:54:05 -0800241 virtual void AddRemoteCandidate(const Candidate& candidate) = 0;
242
243 virtual void RemoveRemoteCandidate(const Candidate& candidate) = 0;
244
245 virtual IceGatheringState gathering_state() const = 0;
246
zhihuangd06adf62017-01-12 15:58:31 -0800247 // Returns the current stats for this connection.
Qingsi Wang72a43a12018-02-20 16:03:18 -0800248 virtual bool GetStats(ConnectionInfos* candidate_pair_stats_list,
249 CandidateStatsList* candidate_stats_list) = 0;
zhihuangd06adf62017-01-12 15:58:31 -0800250
skvladd0309122017-02-02 17:18:37 -0800251 // Returns RTT estimate over the currently active connection, or an empty
Danil Chapovalov00c71832018-06-15 15:58:38 +0200252 // absl::optional if there is none.
253 virtual absl::optional<int> GetRttEstimate() = 0;
skvladd0309122017-02-02 17:18:37 -0800254
zhihuang86abd6f2016-12-19 11:54:05 -0800255 sigslot::signal1<IceTransportInternal*> SignalGatheringState;
256
257 // Handles sending and receiving of candidates.
258 sigslot::signal2<IceTransportInternal*, const Candidate&>
259 SignalCandidateGathered;
260
261 sigslot::signal2<IceTransportInternal*, const Candidates&>
262 SignalCandidatesRemoved;
263
Zhi Huang942bc2e2017-11-13 13:26:07 -0800264 // Deprecated by PacketTransportInternal::SignalNetworkRouteChanged.
zhihuang86abd6f2016-12-19 11:54:05 -0800265 // This signal occurs when there is a change in the way that packets are
266 // being routed, i.e. to a different remote location. The candidate
267 // indicates where and how we are currently sending media.
Zhi Huang942bc2e2017-11-13 13:26:07 -0800268 // TODO(zhihuang): Update the Chrome remoting to use the new
269 // SignalNetworkRouteChanged.
zhihuang86abd6f2016-12-19 11:54:05 -0800270 sigslot::signal2<IceTransportInternal*, const Candidate&> SignalRouteChange;
271
zhihuangd06adf62017-01-12 15:58:31 -0800272 // Invoked when there is conflict in the ICE role between local and remote
273 // agents.
274 sigslot::signal1<IceTransportInternal*> SignalRoleConflict;
275
276 // Emitted whenever the transport state changed.
Jonas Olsson81125f02018-10-09 10:52:04 +0200277 // TODO(bugs.webrtc.org/9308): Remove once all uses have migrated to the new
278 // IceTransportState.
zhihuangd06adf62017-01-12 15:58:31 -0800279 sigslot::signal1<IceTransportInternal*> SignalStateChanged;
280
Jonas Olsson81125f02018-10-09 10:52:04 +0200281 // Emitted whenever the new standards-compliant transport state changed.
282 sigslot::signal1<IceTransportInternal*> SignalIceTransportStateChanged;
283
zhihuang86abd6f2016-12-19 11:54:05 -0800284 // Invoked when the transport is being destroyed.
285 sigslot::signal1<IceTransportInternal*> SignalDestroyed;
zhihuang86abd6f2016-12-19 11:54:05 -0800286};
287
288} // namespace cricket
zhihuangd06adf62017-01-12 15:58:31 -0800289
Steve Anton10542f22019-01-11 09:11:00 -0800290#endif // P2P_BASE_ICE_TRANSPORT_INTERNAL_H_