ice_controller_interface.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 2019 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_CONTROLLER_INTERFACE_H_
  11. #define P2P_BASE_ICE_CONTROLLER_INTERFACE_H_
  12. #include <string>
  13. #include <utility>
  14. #include <vector>
  15. #include "p2p/base/connection.h"
  16. #include "p2p/base/ice_transport_internal.h"
  17. namespace cricket {
  18. struct IceFieldTrials; // Forward declaration to avoid circular dependency.
  19. struct IceControllerEvent {
  20. enum Type {
  21. REMOTE_CANDIDATE_GENERATION_CHANGE,
  22. NETWORK_PREFERENCE_CHANGE,
  23. NEW_CONNECTION_FROM_LOCAL_CANDIDATE,
  24. NEW_CONNECTION_FROM_REMOTE_CANDIDATE,
  25. NEW_CONNECTION_FROM_UNKNOWN_REMOTE_ADDRESS,
  26. NOMINATION_ON_CONTROLLED_SIDE,
  27. DATA_RECEIVED,
  28. CONNECT_STATE_CHANGE,
  29. SELECTED_CONNECTION_DESTROYED,
  30. // The ICE_CONTROLLER_RECHECK enum value lets an IceController request
  31. // P2PTransportChannel to recheck a switch periodically without an event
  32. // taking place.
  33. ICE_CONTROLLER_RECHECK,
  34. };
  35. IceControllerEvent(const Type& _type) // NOLINT: runtime/explicit
  36. : type(_type) {}
  37. std::string ToString() const;
  38. Type type;
  39. int recheck_delay_ms = 0;
  40. };
  41. // Defines the interface for a module that control
  42. // - which connection to ping
  43. // - which connection to use
  44. // - which connection to prune
  45. // - which connection to forget learned state on
  46. //
  47. // The P2PTransportChannel owns (creates and destroys) Connections,
  48. // but P2PTransportChannel gives const pointers to the the IceController using
  49. // |AddConnection|, i.e the IceController should not call any non-const methods
  50. // on a Connection but signal back in the interface if any mutable function
  51. // shall be called.
  52. //
  53. // Current these are limited to:
  54. // Connection::Ping - returned in PingResult
  55. // Connection::Prune - retuned in PruneConnections
  56. // Connection::ForgetLearnedState - return in SwitchResult
  57. //
  58. // The IceController shall keep track of all connections added
  59. // (and not destroyed) and give them back using the connections()-function-
  60. //
  61. // When a Connection gets destroyed
  62. // - signals on Connection::SignalDestroyed
  63. // - P2PTransportChannel calls IceController::OnConnectionDestroyed
  64. class IceControllerInterface {
  65. public:
  66. // This represents the result of a switch call.
  67. struct SwitchResult {
  68. // Connection that we should (optionally) switch to.
  69. absl::optional<const Connection*> connection;
  70. // An optional recheck event for when a Switch() should be attempted again.
  71. absl::optional<IceControllerEvent> recheck_event;
  72. // A vector with connection to run ForgetLearnedState on.
  73. std::vector<const Connection*> connections_to_forget_state_on;
  74. };
  75. // This represents the result of a call to SelectConnectionToPing.
  76. struct PingResult {
  77. PingResult(const Connection* conn, int _recheck_delay_ms)
  78. : connection(conn), recheck_delay_ms(_recheck_delay_ms) {}
  79. // Connection that we should (optionally) ping.
  80. const absl::optional<const Connection*> connection;
  81. // The delay before P2PTransportChannel shall call SelectConnectionToPing()
  82. // again.
  83. //
  84. // Since the IceController determines which connection to ping and
  85. // only returns one connection at a time, the recheck_delay_ms does not have
  86. // any obvious implication on bitrate for pings. E.g the recheck_delay_ms
  87. // will be shorter if there are more connections available.
  88. const int recheck_delay_ms = 0;
  89. };
  90. virtual ~IceControllerInterface() = default;
  91. // These setters are called when the state of P2PTransportChannel is mutated.
  92. virtual void SetIceConfig(const IceConfig& config) = 0;
  93. virtual void SetSelectedConnection(const Connection* selected_connection) = 0;
  94. virtual void AddConnection(const Connection* connection) = 0;
  95. virtual void OnConnectionDestroyed(const Connection* connection) = 0;
  96. // These are all connections that has been added and not destroyed.
  97. virtual rtc::ArrayView<const Connection*> connections() const = 0;
  98. // Is there a pingable connection ?
  99. // This function is used to boot-strap pinging, after this returns true
  100. // SelectConnectionToPing() will be called periodically.
  101. virtual bool HasPingableConnection() const = 0;
  102. // Select a connection to Ping, or nullptr if none.
  103. virtual PingResult SelectConnectionToPing(int64_t last_ping_sent_ms) = 0;
  104. // Compute the "STUN_ATTR_USE_CANDIDATE" for |conn|.
  105. virtual bool GetUseCandidateAttr(const Connection* conn,
  106. NominationMode mode,
  107. IceMode remote_ice_mode) const = 0;
  108. // These methods is only added to not have to change all unit tests
  109. // that simulate pinging by marking a connection pinged.
  110. virtual const Connection* FindNextPingableConnection() = 0;
  111. virtual void MarkConnectionPinged(const Connection* con) = 0;
  112. // Check if we should switch to |connection|.
  113. // This method is called for IceControllerEvent's that can switch directly
  114. // i.e without resorting.
  115. virtual SwitchResult ShouldSwitchConnection(IceControllerEvent reason,
  116. const Connection* connection) = 0;
  117. // Sort connections and check if we should switch.
  118. virtual SwitchResult SortAndSwitchConnection(IceControllerEvent reason) = 0;
  119. // Prune connections.
  120. virtual std::vector<const Connection*> PruneConnections() = 0;
  121. };
  122. } // namespace cricket
  123. #endif // P2P_BASE_ICE_CONTROLLER_INTERFACE_H_