Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 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 | |
| 11 | #ifndef P2P_BASE_ICE_CONTROLLER_INTERFACE_H_ |
| 12 | #define P2P_BASE_ICE_CONTROLLER_INTERFACE_H_ |
| 13 | |
| 14 | #include <string> |
| 15 | #include <utility> |
| 16 | #include <vector> |
| 17 | |
Sameer Vijaykar | 3382c1c | 2022-06-02 09:29:09 | [diff] [blame] | 18 | #include "absl/types/optional.h" |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 19 | #include "p2p/base/connection.h" |
Sameer Vijaykar | 3382c1c | 2022-06-02 09:29:09 | [diff] [blame] | 20 | #include "p2p/base/ice_switch_reason.h" |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 21 | #include "p2p/base/ice_transport_internal.h" |
Daniel Cheng | e125a33 | 2023-12-02 22:16:59 | [diff] [blame] | 22 | #include "rtc_base/checks.h" |
Sameer Vijaykar | a2653bc | 2023-01-26 15:34:09 | [diff] [blame] | 23 | #include "rtc_base/system/rtc_export.h" |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 24 | |
| 25 | namespace cricket { |
| 26 | |
| 27 | struct IceFieldTrials; // Forward declaration to avoid circular dependency. |
| 28 | |
Sameer Vijaykar | a2653bc | 2023-01-26 15:34:09 | [diff] [blame] | 29 | struct RTC_EXPORT IceRecheckEvent { |
Sameer Vijaykar | 2a1accd | 2022-06-03 09:39:39 | [diff] [blame] | 30 | IceRecheckEvent(IceSwitchReason _reason, int _recheck_delay_ms) |
Sameer Vijaykar | 9a91286 | 2022-06-03 12:03:18 | [diff] [blame] | 31 | : reason(_reason), recheck_delay_ms(_recheck_delay_ms) {} |
Sameer Vijaykar | 3382c1c | 2022-06-02 09:29:09 | [diff] [blame] | 32 | |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 33 | std::string ToString() const; |
| 34 | |
Sameer Vijaykar | 3382c1c | 2022-06-02 09:29:09 | [diff] [blame] | 35 | IceSwitchReason reason; |
Sameer Vijaykar | 9a91286 | 2022-06-03 12:03:18 | [diff] [blame] | 36 | int recheck_delay_ms; |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | // Defines the interface for a module that control |
| 40 | // - which connection to ping |
| 41 | // - which connection to use |
| 42 | // - which connection to prune |
Jonas Oreland | 49864b7 | 2020-06-05 12:55:35 | [diff] [blame] | 43 | // - which connection to forget learned state on |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 44 | // |
Jonas Oreland | 49864b7 | 2020-06-05 12:55:35 | [diff] [blame] | 45 | // The P2PTransportChannel owns (creates and destroys) Connections, |
| 46 | // but P2PTransportChannel gives const pointers to the the IceController using |
Artem Titov | 2dbb4c9 | 2021-07-26 13:12:41 | [diff] [blame] | 47 | // `AddConnection`, i.e the IceController should not call any non-const methods |
Jonas Oreland | 49864b7 | 2020-06-05 12:55:35 | [diff] [blame] | 48 | // on a Connection but signal back in the interface if any mutable function |
| 49 | // shall be called. |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 50 | // |
Jonas Oreland | 49864b7 | 2020-06-05 12:55:35 | [diff] [blame] | 51 | // Current these are limited to: |
| 52 | // Connection::Ping - returned in PingResult |
| 53 | // Connection::Prune - retuned in PruneConnections |
| 54 | // Connection::ForgetLearnedState - return in SwitchResult |
| 55 | // |
| 56 | // The IceController shall keep track of all connections added |
Daniel Cheng | e125a33 | 2023-12-02 22:16:59 | [diff] [blame] | 57 | // (and not destroyed) and give them back using the GetConnections() function. |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 58 | // |
| 59 | // When a Connection gets destroyed |
| 60 | // - signals on Connection::SignalDestroyed |
| 61 | // - P2PTransportChannel calls IceController::OnConnectionDestroyed |
| 62 | class IceControllerInterface { |
| 63 | public: |
| 64 | // This represents the result of a switch call. |
| 65 | struct SwitchResult { |
| 66 | // Connection that we should (optionally) switch to. |
| 67 | absl::optional<const Connection*> connection; |
| 68 | |
Jonas Oreland | b5aa0a8 | 2019-12-03 08:59:11 | [diff] [blame] | 69 | // An optional recheck event for when a Switch() should be attempted again. |
Sameer Vijaykar | 2a1accd | 2022-06-03 09:39:39 | [diff] [blame] | 70 | absl::optional<IceRecheckEvent> recheck_event; |
Jonas Oreland | 49864b7 | 2020-06-05 12:55:35 | [diff] [blame] | 71 | |
| 72 | // A vector with connection to run ForgetLearnedState on. |
| 73 | std::vector<const Connection*> connections_to_forget_state_on; |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 74 | }; |
| 75 | |
Jonas Oreland | 4333600 | 2020-03-26 19:59:03 | [diff] [blame] | 76 | // This represents the result of a call to SelectConnectionToPing. |
| 77 | struct PingResult { |
| 78 | PingResult(const Connection* conn, int _recheck_delay_ms) |
Tim Na | 203b549 | 2021-02-05 18:23:53 | [diff] [blame] | 79 | : connection(conn ? absl::optional<const Connection*>(conn) |
| 80 | : absl::nullopt), |
| 81 | recheck_delay_ms(_recheck_delay_ms) {} |
Jonas Oreland | 4333600 | 2020-03-26 19:59:03 | [diff] [blame] | 82 | |
Jonas Oreland | 4333600 | 2020-03-26 19:59:03 | [diff] [blame] | 83 | // Connection that we should (optionally) ping. |
| 84 | const absl::optional<const Connection*> connection; |
| 85 | |
Jonas Oreland | fa097a2 | 2020-03-27 14:12:52 | [diff] [blame] | 86 | // The delay before P2PTransportChannel shall call SelectConnectionToPing() |
| 87 | // again. |
| 88 | // |
| 89 | // Since the IceController determines which connection to ping and |
| 90 | // only returns one connection at a time, the recheck_delay_ms does not have |
| 91 | // any obvious implication on bitrate for pings. E.g the recheck_delay_ms |
| 92 | // will be shorter if there are more connections available. |
Jonas Oreland | 4333600 | 2020-03-26 19:59:03 | [diff] [blame] | 93 | const int recheck_delay_ms = 0; |
| 94 | }; |
Jonas Oreland | 2f3c019 | 2020-03-26 11:59:44 | [diff] [blame] | 95 | |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 96 | virtual ~IceControllerInterface() = default; |
| 97 | |
| 98 | // These setters are called when the state of P2PTransportChannel is mutated. |
| 99 | virtual void SetIceConfig(const IceConfig& config) = 0; |
| 100 | virtual void SetSelectedConnection(const Connection* selected_connection) = 0; |
| 101 | virtual void AddConnection(const Connection* connection) = 0; |
| 102 | virtual void OnConnectionDestroyed(const Connection* connection) = 0; |
| 103 | |
| 104 | // These are all connections that has been added and not destroyed. |
Daniel Cheng | e125a33 | 2023-12-02 22:16:59 | [diff] [blame] | 105 | virtual rtc::ArrayView<const Connection* const> GetConnections() const { |
| 106 | // Stub implementation to simplify downstream roll. |
| 107 | RTC_CHECK_NOTREACHED(); |
| 108 | return {}; |
| 109 | } |
| 110 | // TODO(bugs.webrtc.org/15702): Remove this after downstream is cleaned up. |
| 111 | virtual rtc::ArrayView<const Connection*> connections() const { |
| 112 | // Stub implementation to simplify downstream removal. |
| 113 | RTC_CHECK_NOTREACHED(); |
| 114 | return {}; |
| 115 | } |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 116 | |
| 117 | // Is there a pingable connection ? |
| 118 | // This function is used to boot-strap pinging, after this returns true |
| 119 | // SelectConnectionToPing() will be called periodically. |
| 120 | virtual bool HasPingableConnection() const = 0; |
| 121 | |
| 122 | // Select a connection to Ping, or nullptr if none. |
Jonas Oreland | 2f3c019 | 2020-03-26 11:59:44 | [diff] [blame] | 123 | virtual PingResult SelectConnectionToPing(int64_t last_ping_sent_ms) = 0; |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 124 | |
Artem Titov | 2dbb4c9 | 2021-07-26 13:12:41 | [diff] [blame] | 125 | // Compute the "STUN_ATTR_USE_CANDIDATE" for `conn`. |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 126 | virtual bool GetUseCandidateAttr(const Connection* conn, |
| 127 | NominationMode mode, |
| 128 | IceMode remote_ice_mode) const = 0; |
| 129 | |
| 130 | // These methods is only added to not have to change all unit tests |
| 131 | // that simulate pinging by marking a connection pinged. |
| 132 | virtual const Connection* FindNextPingableConnection() = 0; |
| 133 | virtual void MarkConnectionPinged(const Connection* con) = 0; |
| 134 | |
Artem Titov | 2dbb4c9 | 2021-07-26 13:12:41 | [diff] [blame] | 135 | // Check if we should switch to `connection`. |
Sameer Vijaykar | 3382c1c | 2022-06-02 09:29:09 | [diff] [blame] | 136 | // This method is called for IceSwitchReasons that can switch directly |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 137 | // i.e without resorting. |
Sameer Vijaykar | 781c12e | 2022-06-02 14:01:12 | [diff] [blame] | 138 | virtual SwitchResult ShouldSwitchConnection(IceSwitchReason reason, |
| 139 | const Connection* connection) = 0; |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 140 | |
| 141 | // Sort connections and check if we should switch. |
Sameer Vijaykar | 781c12e | 2022-06-02 14:01:12 | [diff] [blame] | 142 | virtual SwitchResult SortAndSwitchConnection(IceSwitchReason reason) = 0; |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 143 | |
| 144 | // Prune connections. |
| 145 | virtual std::vector<const Connection*> PruneConnections() = 0; |
| 146 | }; |
| 147 | |
| 148 | } // namespace cricket |
| 149 | |
| 150 | #endif // P2P_BASE_ICE_CONTROLLER_INTERFACE_H_ |