ice_transport_internal.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  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. #ifndef P2P_BASE_ICE_TRANSPORT_INTERNAL_H_
  11. #define P2P_BASE_ICE_TRANSPORT_INTERNAL_H_
  12. #include <stdint.h>
  13. #include <string>
  14. #include <vector>
  15. #include "absl/types/optional.h"
  16. #include "api/candidate.h"
  17. #include "api/transport/enums.h"
  18. #include "p2p/base/connection.h"
  19. #include "p2p/base/packet_transport_internal.h"
  20. #include "p2p/base/port.h"
  21. #include "p2p/base/transport_description.h"
  22. #include "rtc_base/network_constants.h"
  23. #include "rtc_base/system/rtc_export.h"
  24. #include "rtc_base/third_party/sigslot/sigslot.h"
  25. #include "rtc_base/time_utils.h"
  26. namespace cricket {
  27. struct IceTransportStats {
  28. CandidateStatsList candidate_stats_list;
  29. ConnectionInfos connection_infos;
  30. // Number of times the selected candidate pair has changed
  31. // Initially 0 and 1 once the first candidate pair has been selected.
  32. // The counter is increase also when "unselecting" a connection.
  33. uint32_t selected_candidate_pair_changes = 0;
  34. };
  35. typedef std::vector<Candidate> Candidates;
  36. enum IceConnectionState {
  37. kIceConnectionConnecting = 0,
  38. kIceConnectionFailed,
  39. kIceConnectionConnected, // Writable, but still checking one or more
  40. // connections
  41. kIceConnectionCompleted,
  42. };
  43. // TODO(deadbeef): Unify with PeerConnectionInterface::IceConnectionState
  44. // once /talk/ and /webrtc/ are combined, and also switch to ENUM_NAME naming
  45. // style.
  46. enum IceGatheringState {
  47. kIceGatheringNew = 0,
  48. kIceGatheringGathering,
  49. kIceGatheringComplete,
  50. };
  51. enum 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,
  56. };
  57. // ICE Nomination mode.
  58. enum 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. // Information about ICE configuration.
  66. // TODO(deadbeef): Use absl::optional to represent unset values, instead of
  67. // -1.
  68. struct IceConfig {
  69. // The ICE connection receiving timeout value in milliseconds.
  70. absl::optional<int> receiving_timeout;
  71. // Time interval in milliseconds to ping a backup connection when the ICE
  72. // channel is strongly connected.
  73. absl::optional<int> backup_connection_ping_interval;
  74. ContinualGatheringPolicy continual_gathering_policy = GATHER_ONCE;
  75. bool gather_continually() const {
  76. return continual_gathering_policy == GATHER_CONTINUALLY;
  77. }
  78. // Whether we should prioritize Relay/Relay candidate when nothing
  79. // is writable yet.
  80. bool prioritize_most_likely_candidate_pairs = false;
  81. // Writable connections are pinged at a slower rate once stablized.
  82. absl::optional<int> stable_writable_connection_ping_interval;
  83. // If set to true, this means the ICE transport should presume TURN-to-TURN
  84. // candidate pairs will succeed, even before a binding response is received.
  85. bool presume_writable_when_fully_relayed = false;
  86. // If true, after the ICE transport type (as the candidate filter used by the
  87. // port allocator) is changed such that new types of ICE candidates are
  88. // allowed by the new filter, e.g. from CF_RELAY to CF_ALL, candidates that
  89. // have been gathered by the ICE transport but filtered out and not signaled
  90. // to the upper layers, will be surfaced.
  91. bool surface_ice_candidates_on_ice_transport_type_changed = false;
  92. // Interval to check on all networks and to perform ICE regathering on any
  93. // active network having no connection on it.
  94. absl::optional<int> regather_on_failed_networks_interval;
  95. // The time period in which we will not switch the selected connection
  96. // when a new connection becomes receiving but the selected connection is not
  97. // in case that the selected connection may become receiving soon.
  98. absl::optional<int> receiving_switching_delay;
  99. // TODO(honghaiz): Change the default to regular nomination.
  100. // Default nomination mode if the remote does not support renomination.
  101. NominationMode default_nomination_mode = NominationMode::SEMI_AGGRESSIVE;
  102. // The interval in milliseconds at which ICE checks (STUN pings) will be sent
  103. // for a candidate pair when it is both writable and receiving (strong
  104. // connectivity). This parameter overrides the default value given by
  105. // |STRONG_PING_INTERVAL| in p2ptransport.h if set.
  106. absl::optional<int> ice_check_interval_strong_connectivity;
  107. // The interval in milliseconds at which ICE checks (STUN pings) will be sent
  108. // for a candidate pair when it is either not writable or not receiving (weak
  109. // connectivity). This parameter overrides the default value given by
  110. // |WEAK_PING_INTERVAL| in p2ptransport.h if set.
  111. absl::optional<int> ice_check_interval_weak_connectivity;
  112. // ICE checks (STUN pings) will not be sent at higher rate (lower interval)
  113. // than this, no matter what other settings there are.
  114. // Measure in milliseconds.
  115. //
  116. // Note that this parameter overrides both the above check intervals for
  117. // candidate pairs with strong or weak connectivity, if either of the above
  118. // interval is shorter than the min interval.
  119. absl::optional<int> ice_check_min_interval;
  120. // The min time period for which a candidate pair must wait for response to
  121. // connectivity checks before it becomes unwritable. This parameter
  122. // overrides the default value given by |CONNECTION_WRITE_CONNECT_TIMEOUT|
  123. // in port.h if set, when determining the writability of a candidate pair.
  124. absl::optional<int> ice_unwritable_timeout;
  125. // The min number of connectivity checks that a candidate pair must sent
  126. // without receiving response before it becomes unwritable. This parameter
  127. // overrides the default value given by |CONNECTION_WRITE_CONNECT_FAILURES| in
  128. // port.h if set, when determining the writability of a candidate pair.
  129. absl::optional<int> ice_unwritable_min_checks;
  130. // The min time period for which a candidate pair must wait for response to
  131. // connectivity checks it becomes inactive. This parameter overrides the
  132. // default value given by |CONNECTION_WRITE_TIMEOUT| in port.h if set, when
  133. // determining the writability of a candidate pair.
  134. absl::optional<int> ice_inactive_timeout;
  135. // The interval in milliseconds at which STUN candidates will resend STUN
  136. // binding requests to keep NAT bindings open.
  137. absl::optional<int> stun_keepalive_interval;
  138. absl::optional<rtc::AdapterType> network_preference;
  139. IceConfig();
  140. IceConfig(int receiving_timeout_ms,
  141. int backup_connection_ping_interval,
  142. ContinualGatheringPolicy gathering_policy,
  143. bool prioritize_most_likely_candidate_pairs,
  144. int stable_writable_connection_ping_interval_ms,
  145. bool presume_writable_when_fully_relayed,
  146. int regather_on_failed_networks_interval_ms,
  147. int receiving_switching_delay_ms);
  148. ~IceConfig();
  149. // Helper getters for parameters with implementation-specific default value.
  150. // By convention, parameters with default value are represented by
  151. // absl::optional and setting a parameter to null restores its default value.
  152. int receiving_timeout_or_default() const;
  153. int backup_connection_ping_interval_or_default() const;
  154. int stable_writable_connection_ping_interval_or_default() const;
  155. int regather_on_failed_networks_interval_or_default() const;
  156. int receiving_switching_delay_or_default() const;
  157. int ice_check_interval_strong_connectivity_or_default() const;
  158. int ice_check_interval_weak_connectivity_or_default() const;
  159. int ice_check_min_interval_or_default() const;
  160. int ice_unwritable_timeout_or_default() const;
  161. int ice_unwritable_min_checks_or_default() const;
  162. int ice_inactive_timeout_or_default() const;
  163. int stun_keepalive_interval_or_default() const;
  164. };
  165. // TODO(zhihuang): Replace this with
  166. // PeerConnectionInterface::IceConnectionState.
  167. enum class IceTransportState {
  168. STATE_INIT,
  169. STATE_CONNECTING, // Will enter this state once a connection is created
  170. STATE_COMPLETED,
  171. STATE_FAILED
  172. };
  173. // TODO(zhihuang): Remove this once it's no longer used in
  174. // remoting/protocol/libjingle_transport_factory.cc
  175. enum IceProtocolType {
  176. ICEPROTO_RFC5245 // Standard RFC 5245 version of ICE.
  177. };
  178. // IceTransportInternal is an internal abstract class that does ICE.
  179. // Once the public interface is supported,
  180. // (https://www.w3.org/TR/webrtc/#rtcicetransport)
  181. // the IceTransportInterface will be split from this class.
  182. class RTC_EXPORT IceTransportInternal : public rtc::PacketTransportInternal {
  183. public:
  184. IceTransportInternal();
  185. ~IceTransportInternal() override;
  186. // TODO(bugs.webrtc.org/9308): Remove GetState once all uses have been
  187. // migrated to GetIceTransportState.
  188. virtual IceTransportState GetState() const = 0;
  189. virtual webrtc::IceTransportState GetIceTransportState() const = 0;
  190. virtual int component() const = 0;
  191. virtual IceRole GetIceRole() const = 0;
  192. virtual void SetIceRole(IceRole role) = 0;
  193. virtual void SetIceTiebreaker(uint64_t tiebreaker) = 0;
  194. // TODO(zhihuang): Remove this once it's no longer called in
  195. // remoting/protocol/libjingle_transport_factory.cc
  196. virtual void SetIceProtocolType(IceProtocolType type) {}
  197. virtual void SetIceCredentials(const std::string& ice_ufrag,
  198. const std::string& ice_pwd);
  199. virtual void SetRemoteIceCredentials(const std::string& ice_ufrag,
  200. const std::string& ice_pwd);
  201. // The ufrag and pwd in |ice_params| must be set
  202. // before candidate gathering can start.
  203. virtual void SetIceParameters(const IceParameters& ice_params) = 0;
  204. virtual void SetRemoteIceParameters(const IceParameters& ice_params) = 0;
  205. virtual void SetRemoteIceMode(IceMode mode) = 0;
  206. virtual void SetIceConfig(const IceConfig& config) = 0;
  207. // Start gathering candidates if not already started, or if an ICE restart
  208. // occurred.
  209. virtual void MaybeStartGathering() = 0;
  210. virtual void AddRemoteCandidate(const Candidate& candidate) = 0;
  211. virtual void RemoveRemoteCandidate(const Candidate& candidate) = 0;
  212. virtual void RemoveAllRemoteCandidates() = 0;
  213. virtual IceGatheringState gathering_state() const = 0;
  214. // Returns the current stats for this connection.
  215. virtual bool GetStats(IceTransportStats* ice_transport_stats) = 0;
  216. // Returns RTT estimate over the currently active connection, or an empty
  217. // absl::optional if there is none.
  218. virtual absl::optional<int> GetRttEstimate() = 0;
  219. // TODO(qingsi): Remove this method once Chrome does not depend on it anymore.
  220. virtual const Connection* selected_connection() const = 0;
  221. // Returns the selected candidate pair, or an empty absl::optional if there is
  222. // none.
  223. virtual absl::optional<const CandidatePair> GetSelectedCandidatePair()
  224. const = 0;
  225. sigslot::signal1<IceTransportInternal*> SignalGatheringState;
  226. // Handles sending and receiving of candidates.
  227. sigslot::signal2<IceTransportInternal*, const Candidate&>
  228. SignalCandidateGathered;
  229. sigslot::signal2<IceTransportInternal*, const IceCandidateErrorEvent&>
  230. SignalCandidateError;
  231. sigslot::signal2<IceTransportInternal*, const Candidates&>
  232. SignalCandidatesRemoved;
  233. // Deprecated by PacketTransportInternal::SignalNetworkRouteChanged.
  234. // This signal occurs when there is a change in the way that packets are
  235. // being routed, i.e. to a different remote location. The candidate
  236. // indicates where and how we are currently sending media.
  237. // TODO(zhihuang): Update the Chrome remoting to use the new
  238. // SignalNetworkRouteChanged.
  239. sigslot::signal2<IceTransportInternal*, const Candidate&> SignalRouteChange;
  240. sigslot::signal1<const cricket::CandidatePairChangeEvent&>
  241. SignalCandidatePairChanged;
  242. // Invoked when there is conflict in the ICE role between local and remote
  243. // agents.
  244. sigslot::signal1<IceTransportInternal*> SignalRoleConflict;
  245. // Emitted whenever the transport state changed.
  246. // TODO(bugs.webrtc.org/9308): Remove once all uses have migrated to the new
  247. // IceTransportState.
  248. sigslot::signal1<IceTransportInternal*> SignalStateChanged;
  249. // Emitted whenever the new standards-compliant transport state changed.
  250. sigslot::signal1<IceTransportInternal*> SignalIceTransportStateChanged;
  251. // Invoked when the transport is being destroyed.
  252. sigslot::signal1<IceTransportInternal*> SignalDestroyed;
  253. };
  254. } // namespace cricket
  255. #endif // P2P_BASE_ICE_TRANSPORT_INTERNAL_H_