blob: dfb99ea44a6349f0cfdcd291d1b0e74e00be4ea6 [file] [log] [blame]
Jonas Oreland09c452e2019-11-20 08:01:021/*
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 Oreland2f74d5f2019-11-22 06:53:2220#include "p2p/base/ice_controller_factory_interface.h"
Jonas Oreland09c452e2019-11-20 08:01:0221#include "p2p/base/ice_controller_interface.h"
Jonas Oreland09c452e2019-11-20 08:01:0222
23namespace cricket {
24
25class BasicIceController : public IceControllerInterface {
26 public:
Jonas Oreland2f74d5f2019-11-22 06:53:2227 explicit BasicIceController(const IceControllerFactoryArgs& args);
Jonas Oreland09c452e2019-11-20 08:01:0228 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 Chenge125a332023-12-02 22:16:5934 rtc::ArrayView<const Connection* const> GetConnections() const override {
35 return connections_;
36 }
Manashi Sarkarc925f502023-12-04 12:10:1537 rtc::ArrayView<const Connection*> connections() const override {
38 return rtc::ArrayView<const Connection*>(
39 const_cast<const Connection**>(connections_.data()),
40 connections_.size());
Jonas Oreland09c452e2019-11-20 08:01:0241 }
42
43 bool HasPingableConnection() const override;
44
Jonas Oreland2f3c0192020-03-26 11:59:4445 PingResult SelectConnectionToPing(int64_t last_ping_sent_ms) override;
46
Jonas Oreland09c452e2019-11-20 08:01:0247 bool GetUseCandidateAttr(const Connection* conn,
48 NominationMode mode,
49 IceMode remote_ice_mode) const override;
50
Sameer Vijaykar781c12e2022-06-02 14:01:1251 SwitchResult ShouldSwitchConnection(IceSwitchReason reason,
Jonas Oreland09c452e2019-11-20 08:01:0252 const Connection* connection) override;
Sameer Vijaykar781c12e2022-06-02 14:01:1253 SwitchResult SortAndSwitchConnection(IceSwitchReason reason) override;
Jonas Oreland09c452e2019-11-20 08:01:0254
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 Titov2dbb4c92021-07-26 13:12:4184 // Between `conn1` and `conn2`, this function returns the one which should
Jonas Oreland09c452e2019-11-20 08:01:0285 // 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 Oreland98e745b2019-11-27 10:02:45105 std::map<const rtc::Network*, const Connection*> GetBestConnectionByNetwork()
106 const;
Jonas Oreland09c452e2019-11-20 08:01:02107 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 Castelli8037fc62024-08-29 13:00:40115 std::optional<rtc::AdapterType> network_preference) const;
Jonas Oreland09c452e2019-11-20 08:01:02116
Artem Titov2dbb4c92021-07-26 13:12:41117 // 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 Oreland09c452e2019-11-20 08:01:02124 int CompareConnectionStates(
125 const Connection* a,
126 const Connection* b,
Florent Castelli8037fc62024-08-29 13:00:40127 std::optional<int64_t> receiving_unchanged_threshold,
Jonas Oreland09c452e2019-11-20 08:01:02128 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 Titov2dbb4c92021-07-26 13:12:41135 // Returns a positive value if `a` is better than `b`.
Jonas Oreland09c452e2019-11-20 08:01:02136 int CompareConnections(const Connection* a,
137 const Connection* b,
Florent Castelli8037fc62024-08-29 13:00:40138 std::optional<int64_t> receiving_unchanged_threshold,
Jonas Oreland09c452e2019-11-20 08:01:02139 bool* missed_receiving_unchanged_threshold) const;
140
Sameer Vijaykar781c12e2022-06-02 14:01:12141 SwitchResult HandleInitialSelectDampening(IceSwitchReason reason,
Jonas Oreland09c452e2019-11-20 08:01:02142 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 Titov2dbb4c92021-07-26 13:12:41151 // `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 Oreland09c452e2019-11-20 08:01:02155 // 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_