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_BASIC_ICE_CONTROLLER_H_ |
| 12 | #define P2P_BASE_BASIC_ICE_CONTROLLER_H_ |
| 13 | |
| 14 | #include <algorithm> |
| 15 | #include <map> |
| 16 | #include <set> |
| 17 | #include <utility> |
| 18 | #include <vector> |
| 19 | |
Jonas Oreland | 2f74d5f | 2019-11-22 06:53:22 | [diff] [blame] | 20 | #include "p2p/base/ice_controller_factory_interface.h" |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 21 | #include "p2p/base/ice_controller_interface.h" |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 22 | |
| 23 | namespace cricket { |
| 24 | |
| 25 | class BasicIceController : public IceControllerInterface { |
| 26 | public: |
Jonas Oreland | 2f74d5f | 2019-11-22 06:53:22 | [diff] [blame] | 27 | explicit BasicIceController(const IceControllerFactoryArgs& args); |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 28 | virtual ~BasicIceController(); |
| 29 | |
| 30 | void SetIceConfig(const IceConfig& config) override; |
| 31 | void SetSelectedConnection(const Connection* selected_connection) override; |
| 32 | void AddConnection(const Connection* connection) override; |
| 33 | void OnConnectionDestroyed(const Connection* connection) override; |
Daniel Cheng | e125a33 | 2023-12-02 22:16:59 | [diff] [blame] | 34 | rtc::ArrayView<const Connection* const> GetConnections() const override { |
| 35 | return connections_; |
| 36 | } |
Manashi Sarkar | c925f50 | 2023-12-04 12:10:15 | [diff] [blame] | 37 | rtc::ArrayView<const Connection*> connections() const override { |
| 38 | return rtc::ArrayView<const Connection*>( |
| 39 | const_cast<const Connection**>(connections_.data()), |
| 40 | connections_.size()); |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | bool HasPingableConnection() const override; |
| 44 | |
Jonas Oreland | 2f3c019 | 2020-03-26 11:59:44 | [diff] [blame] | 45 | PingResult SelectConnectionToPing(int64_t last_ping_sent_ms) override; |
| 46 | |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 47 | bool GetUseCandidateAttr(const Connection* conn, |
| 48 | NominationMode mode, |
| 49 | IceMode remote_ice_mode) const override; |
| 50 | |
Sameer Vijaykar | 781c12e | 2022-06-02 14:01:12 | [diff] [blame] | 51 | SwitchResult ShouldSwitchConnection(IceSwitchReason reason, |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 52 | const Connection* connection) override; |
Sameer Vijaykar | 781c12e | 2022-06-02 14:01:12 | [diff] [blame] | 53 | SwitchResult SortAndSwitchConnection(IceSwitchReason reason) override; |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 54 | |
| 55 | std::vector<const Connection*> PruneConnections() override; |
| 56 | |
| 57 | // These methods are only for tests. |
| 58 | const Connection* FindNextPingableConnection() override; |
| 59 | void MarkConnectionPinged(const Connection* conn) override; |
| 60 | |
| 61 | private: |
| 62 | // A transport channel is weak if the current best connection is either |
| 63 | // not receiving or not writable, or if there is no best connection at all. |
| 64 | bool weak() const { |
| 65 | return !selected_connection_ || selected_connection_->weak(); |
| 66 | } |
| 67 | |
| 68 | int weak_ping_interval() const { |
| 69 | return std::max(config_.ice_check_interval_weak_connectivity_or_default(), |
| 70 | config_.ice_check_min_interval_or_default()); |
| 71 | } |
| 72 | |
| 73 | int strong_ping_interval() const { |
| 74 | return std::max(config_.ice_check_interval_strong_connectivity_or_default(), |
| 75 | config_.ice_check_min_interval_or_default()); |
| 76 | } |
| 77 | |
| 78 | int check_receiving_interval() const { |
| 79 | return std::max(MIN_CHECK_RECEIVING_INTERVAL, |
| 80 | config_.receiving_timeout_or_default() / 10); |
| 81 | } |
| 82 | |
| 83 | const Connection* FindOldestConnectionNeedingTriggeredCheck(int64_t now); |
Artem Titov | 2dbb4c9 | 2021-07-26 13:12:41 | [diff] [blame] | 84 | // Between `conn1` and `conn2`, this function returns the one which should |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 85 | // be pinged first. |
| 86 | const Connection* MorePingable(const Connection* conn1, |
| 87 | const Connection* conn2); |
| 88 | // Select the connection which is Relay/Relay. If both of them are, |
| 89 | // UDP relay protocol takes precedence. |
| 90 | const Connection* MostLikelyToWork(const Connection* conn1, |
| 91 | const Connection* conn2); |
| 92 | // Compare the last_ping_sent time and return the one least recently pinged. |
| 93 | const Connection* LeastRecentlyPinged(const Connection* conn1, |
| 94 | const Connection* conn2); |
| 95 | |
| 96 | bool IsPingable(const Connection* conn, int64_t now) const; |
| 97 | bool IsBackupConnection(const Connection* conn) const; |
| 98 | // Whether a writable connection is past its ping interval and needs to be |
| 99 | // pinged again. |
| 100 | bool WritableConnectionPastPingInterval(const Connection* conn, |
| 101 | int64_t now) const; |
| 102 | int CalculateActiveWritablePingInterval(const Connection* conn, |
| 103 | int64_t now) const; |
| 104 | |
Jonas Oreland | 98e745b | 2019-11-27 10:02:45 | [diff] [blame] | 105 | std::map<const rtc::Network*, const Connection*> GetBestConnectionByNetwork() |
| 106 | const; |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 107 | std::vector<const Connection*> GetBestWritableConnectionPerNetwork() const; |
| 108 | |
| 109 | bool ReadyToSend(const Connection* connection) const; |
| 110 | bool PresumedWritable(const Connection* conn) const; |
| 111 | |
| 112 | int CompareCandidatePairNetworks( |
| 113 | const Connection* a, |
| 114 | const Connection* b, |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 115 | std::optional<rtc::AdapterType> network_preference) const; |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 116 | |
Artem Titov | 2dbb4c9 | 2021-07-26 13:12:41 | [diff] [blame] | 117 | // The methods below return a positive value if `a` is preferable to `b`, |
| 118 | // a negative value if `b` is preferable, and 0 if they're equally preferable. |
| 119 | // If `receiving_unchanged_threshold` is set, then when `b` is receiving and |
| 120 | // `a` is not, returns a negative value only if `b` has been in receiving |
| 121 | // state and `a` has been in not receiving state since |
| 122 | // `receiving_unchanged_threshold` and sets |
| 123 | // `missed_receiving_unchanged_threshold` to true otherwise. |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 124 | int CompareConnectionStates( |
| 125 | const Connection* a, |
| 126 | const Connection* b, |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 127 | std::optional<int64_t> receiving_unchanged_threshold, |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 128 | bool* missed_receiving_unchanged_threshold) const; |
| 129 | int CompareConnectionCandidates(const Connection* a, |
| 130 | const Connection* b) const; |
| 131 | // Compares two connections based on the connection states |
| 132 | // (writable/receiving/connected), nomination states, last data received time, |
| 133 | // and static preferences. Does not include latency. Used by both sorting |
| 134 | // and ShouldSwitchSelectedConnection(). |
Artem Titov | 2dbb4c9 | 2021-07-26 13:12:41 | [diff] [blame] | 135 | // Returns a positive value if `a` is better than `b`. |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 136 | int CompareConnections(const Connection* a, |
| 137 | const Connection* b, |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 138 | std::optional<int64_t> receiving_unchanged_threshold, |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 139 | bool* missed_receiving_unchanged_threshold) const; |
| 140 | |
Sameer Vijaykar | 781c12e | 2022-06-02 14:01:12 | [diff] [blame] | 141 | SwitchResult HandleInitialSelectDampening(IceSwitchReason reason, |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 142 | const Connection* new_connection); |
| 143 | |
| 144 | std::function<IceTransportState()> ice_transport_state_func_; |
| 145 | std::function<IceRole()> ice_role_func_; |
| 146 | std::function<bool(const Connection*)> is_connection_pruned_func_; |
| 147 | |
| 148 | IceConfig config_; |
| 149 | const IceFieldTrials* field_trials_; |
| 150 | |
Artem Titov | 2dbb4c9 | 2021-07-26 13:12:41 | [diff] [blame] | 151 | // `connections_` is a sorted list with the first one always be the |
| 152 | // `selected_connection_` when it's not nullptr. The combination of |
| 153 | // `pinged_connections_` and `unpinged_connections_` has the same |
| 154 | // connections as `connections_`. These 2 sets maintain whether a |
Jonas Oreland | 09c452e | 2019-11-20 08:01:02 | [diff] [blame] | 155 | // connection should be pinged next or not. |
| 156 | const Connection* selected_connection_ = nullptr; |
| 157 | std::vector<const Connection*> connections_; |
| 158 | std::set<const Connection*> pinged_connections_; |
| 159 | std::set<const Connection*> unpinged_connections_; |
| 160 | |
| 161 | // Timestamp for when we got the first selectable connection. |
| 162 | int64_t initial_select_timestamp_ms_ = 0; |
| 163 | }; |
| 164 | |
| 165 | } // namespace cricket |
| 166 | |
| 167 | #endif // P2P_BASE_BASIC_ICE_CONTROLLER_H_ |