Jonas Oreland | 09c452e | 2019-11-20 09:01:02 +0100 | [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 | |
| 20 | #include "p2p/base/ice_controller_interface.h" |
| 21 | #include "p2p/base/p2p_transport_channel.h" |
| 22 | |
| 23 | namespace cricket { |
| 24 | |
| 25 | class BasicIceController : public IceControllerInterface { |
| 26 | public: |
| 27 | BasicIceController( |
| 28 | std::function<IceTransportState()> ice_transport_state_func, |
| 29 | std::function<IceRole()> ice_role_func, |
| 30 | std::function<bool(const Connection*)> is_candidated_pruned_func, |
| 31 | const IceFieldTrials*); |
| 32 | virtual ~BasicIceController(); |
| 33 | |
| 34 | void SetIceConfig(const IceConfig& config) override; |
| 35 | void SetSelectedConnection(const Connection* selected_connection) override; |
| 36 | void AddConnection(const Connection* connection) override; |
| 37 | void OnConnectionDestroyed(const Connection* connection) override; |
| 38 | rtc::ArrayView<const Connection*> connections() const override { |
| 39 | return rtc::ArrayView<const Connection*>( |
| 40 | const_cast<const Connection**>(connections_.data()), |
| 41 | connections_.size()); |
| 42 | } |
| 43 | |
| 44 | bool HasPingableConnection() const override; |
| 45 | |
| 46 | std::pair<Connection*, int> SelectConnectionToPing( |
| 47 | int64_t last_ping_sent_ms) override; |
| 48 | bool GetUseCandidateAttr(const Connection* conn, |
| 49 | NominationMode mode, |
| 50 | IceMode remote_ice_mode) const override; |
| 51 | |
| 52 | SwitchResult ShouldSwitchConnection(IceControllerEvent reason, |
| 53 | const Connection* connection) override; |
| 54 | SwitchResult SortAndSwitchConnection(IceControllerEvent reason) override; |
| 55 | |
| 56 | std::vector<const Connection*> PruneConnections() override; |
| 57 | |
| 58 | // These methods are only for tests. |
| 59 | const Connection* FindNextPingableConnection() override; |
| 60 | void MarkConnectionPinged(const Connection* conn) override; |
| 61 | |
| 62 | private: |
| 63 | // A transport channel is weak if the current best connection is either |
| 64 | // not receiving or not writable, or if there is no best connection at all. |
| 65 | bool weak() const { |
| 66 | return !selected_connection_ || selected_connection_->weak(); |
| 67 | } |
| 68 | |
| 69 | int weak_ping_interval() const { |
| 70 | return std::max(config_.ice_check_interval_weak_connectivity_or_default(), |
| 71 | config_.ice_check_min_interval_or_default()); |
| 72 | } |
| 73 | |
| 74 | int strong_ping_interval() const { |
| 75 | return std::max(config_.ice_check_interval_strong_connectivity_or_default(), |
| 76 | config_.ice_check_min_interval_or_default()); |
| 77 | } |
| 78 | |
| 79 | int check_receiving_interval() const { |
| 80 | return std::max(MIN_CHECK_RECEIVING_INTERVAL, |
| 81 | config_.receiving_timeout_or_default() / 10); |
| 82 | } |
| 83 | |
| 84 | const Connection* FindOldestConnectionNeedingTriggeredCheck(int64_t now); |
| 85 | // Between |conn1| and |conn2|, this function returns the one which should |
| 86 | // be pinged first. |
| 87 | const Connection* MorePingable(const Connection* conn1, |
| 88 | const Connection* conn2); |
| 89 | // Select the connection which is Relay/Relay. If both of them are, |
| 90 | // UDP relay protocol takes precedence. |
| 91 | const Connection* MostLikelyToWork(const Connection* conn1, |
| 92 | const Connection* conn2); |
| 93 | // Compare the last_ping_sent time and return the one least recently pinged. |
| 94 | const Connection* LeastRecentlyPinged(const Connection* conn1, |
| 95 | const Connection* conn2); |
| 96 | |
| 97 | bool IsPingable(const Connection* conn, int64_t now) const; |
| 98 | bool IsBackupConnection(const Connection* conn) const; |
| 99 | // Whether a writable connection is past its ping interval and needs to be |
| 100 | // pinged again. |
| 101 | bool WritableConnectionPastPingInterval(const Connection* conn, |
| 102 | int64_t now) const; |
| 103 | int CalculateActiveWritablePingInterval(const Connection* conn, |
| 104 | int64_t now) const; |
| 105 | |
| 106 | std::map<rtc::Network*, const Connection*> GetBestConnectionByNetwork() const; |
| 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, |
| 115 | absl::optional<rtc::AdapterType> network_preference) const; |
| 116 | |
| 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. |
| 124 | int CompareConnectionStates( |
| 125 | const Connection* a, |
| 126 | const Connection* b, |
| 127 | absl::optional<int64_t> receiving_unchanged_threshold, |
| 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(). |
| 135 | // Returns a positive value if |a| is better than |b|. |
| 136 | int CompareConnections(const Connection* a, |
| 137 | const Connection* b, |
| 138 | absl::optional<int64_t> receiving_unchanged_threshold, |
| 139 | bool* missed_receiving_unchanged_threshold) const; |
| 140 | |
| 141 | SwitchResult HandleInitialSelectDampening(IceControllerEvent reason, |
| 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 | |
| 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 |
| 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_ |