blob: 5ec5e5a722f752b1614a70a5dcd63c9252776576 [file] [log] [blame]
Jonas Oreland09c452e2019-11-20 09:01:02 +01001/*
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 Vijaykar3382c1c2022-06-02 11:29:09 +020018#include "absl/types/optional.h"
Jonas Oreland09c452e2019-11-20 09:01:02 +010019#include "p2p/base/connection.h"
Sameer Vijaykar3382c1c2022-06-02 11:29:09 +020020#include "p2p/base/ice_switch_reason.h"
Jonas Oreland09c452e2019-11-20 09:01:02 +010021#include "p2p/base/ice_transport_internal.h"
22
23namespace cricket {
24
25struct IceFieldTrials; // Forward declaration to avoid circular dependency.
26
Sameer Vijaykar2a1accd2022-06-03 11:39:39 +020027struct IceRecheckEvent {
Sameer Vijaykar3382c1c2022-06-02 11:29:09 +020028 // TODO(bugs.webrtc.org/14125) replace with IceSwitchReason.
Jonas Oreland09c452e2019-11-20 09:01:02 +010029 enum Type {
30 REMOTE_CANDIDATE_GENERATION_CHANGE,
31 NETWORK_PREFERENCE_CHANGE,
32 NEW_CONNECTION_FROM_LOCAL_CANDIDATE,
33 NEW_CONNECTION_FROM_REMOTE_CANDIDATE,
34 NEW_CONNECTION_FROM_UNKNOWN_REMOTE_ADDRESS,
35 NOMINATION_ON_CONTROLLED_SIDE,
36 DATA_RECEIVED,
37 CONNECT_STATE_CHANGE,
Jonas Orelandb5aa0a82019-12-03 09:59:11 +010038 SELECTED_CONNECTION_DESTROYED,
39 // The ICE_CONTROLLER_RECHECK enum value lets an IceController request
40 // P2PTransportChannel to recheck a switch periodically without an event
41 // taking place.
42 ICE_CONTROLLER_RECHECK,
Jonas Oreland09c452e2019-11-20 09:01:02 +010043 };
44
Sameer Vijaykar2a1accd2022-06-03 11:39:39 +020045 [[deprecated("bugs.webrtc.org/14125")]] IceRecheckEvent(
Sameer Vijaykar781c12e2022-06-02 16:01:12 +020046 const Type& _type) // NOLINT: runtime/explicit
Sameer Vijaykar3382c1c2022-06-02 11:29:09 +020047 : type(_type), reason(FromType(_type)) {}
48
Sameer Vijaykar2a1accd2022-06-03 11:39:39 +020049 IceRecheckEvent(IceSwitchReason _reason, int _recheck_delay_ms)
Sameer Vijaykar3382c1c2022-06-02 11:29:09 +020050 : type(FromIceSwitchReason(_reason)),
51 reason(_reason),
52 recheck_delay_ms(_recheck_delay_ms) {}
53
54 static Type FromIceSwitchReason(IceSwitchReason reason);
55 static IceSwitchReason FromType(Type type);
56
Jonas Oreland09c452e2019-11-20 09:01:02 +010057 std::string ToString() const;
58
Sameer Vijaykar3382c1c2022-06-02 11:29:09 +020059 // TODO(bugs.webrtc.org/14125) replace usage with IceSwitchReason.
Jonas Oreland09c452e2019-11-20 09:01:02 +010060 Type type;
Sameer Vijaykar3382c1c2022-06-02 11:29:09 +020061 IceSwitchReason reason;
Jonas Orelandb5aa0a82019-12-03 09:59:11 +010062 int recheck_delay_ms = 0;
Jonas Oreland09c452e2019-11-20 09:01:02 +010063};
64
Sameer Vijaykar2a1accd2022-06-03 11:39:39 +020065// TODO(bugs.webrtc.org/14125): remove.
66using IceControllerEvent = IceRecheckEvent;
67
Jonas Oreland09c452e2019-11-20 09:01:02 +010068// Defines the interface for a module that control
69// - which connection to ping
70// - which connection to use
71// - which connection to prune
Jonas Oreland49864b72020-06-05 14:55:35 +020072// - which connection to forget learned state on
Jonas Oreland09c452e2019-11-20 09:01:02 +010073//
Jonas Oreland49864b72020-06-05 14:55:35 +020074// The P2PTransportChannel owns (creates and destroys) Connections,
75// but P2PTransportChannel gives const pointers to the the IceController using
Artem Titov2dbb4c92021-07-26 15:12:41 +020076// `AddConnection`, i.e the IceController should not call any non-const methods
Jonas Oreland49864b72020-06-05 14:55:35 +020077// on a Connection but signal back in the interface if any mutable function
78// shall be called.
Jonas Oreland09c452e2019-11-20 09:01:02 +010079//
Jonas Oreland49864b72020-06-05 14:55:35 +020080// Current these are limited to:
81// Connection::Ping - returned in PingResult
82// Connection::Prune - retuned in PruneConnections
83// Connection::ForgetLearnedState - return in SwitchResult
84//
85// The IceController shall keep track of all connections added
Jonas Oreland09c452e2019-11-20 09:01:02 +010086// (and not destroyed) and give them back using the connections()-function-
87//
88// When a Connection gets destroyed
89// - signals on Connection::SignalDestroyed
90// - P2PTransportChannel calls IceController::OnConnectionDestroyed
91class IceControllerInterface {
92 public:
93 // This represents the result of a switch call.
94 struct SwitchResult {
95 // Connection that we should (optionally) switch to.
96 absl::optional<const Connection*> connection;
97
Jonas Orelandb5aa0a82019-12-03 09:59:11 +010098 // An optional recheck event for when a Switch() should be attempted again.
Sameer Vijaykar2a1accd2022-06-03 11:39:39 +020099 absl::optional<IceRecheckEvent> recheck_event;
Jonas Oreland49864b72020-06-05 14:55:35 +0200100
101 // A vector with connection to run ForgetLearnedState on.
102 std::vector<const Connection*> connections_to_forget_state_on;
Jonas Oreland09c452e2019-11-20 09:01:02 +0100103 };
104
Jonas Oreland43336002020-03-26 20:59:03 +0100105 // This represents the result of a call to SelectConnectionToPing.
106 struct PingResult {
107 PingResult(const Connection* conn, int _recheck_delay_ms)
Tim Na203b5492021-02-05 10:23:53 -0800108 : connection(conn ? absl::optional<const Connection*>(conn)
109 : absl::nullopt),
110 recheck_delay_ms(_recheck_delay_ms) {}
Jonas Oreland43336002020-03-26 20:59:03 +0100111
Jonas Oreland43336002020-03-26 20:59:03 +0100112 // Connection that we should (optionally) ping.
113 const absl::optional<const Connection*> connection;
114
Jonas Orelandfa097a22020-03-27 15:12:52 +0100115 // The delay before P2PTransportChannel shall call SelectConnectionToPing()
116 // again.
117 //
118 // Since the IceController determines which connection to ping and
119 // only returns one connection at a time, the recheck_delay_ms does not have
120 // any obvious implication on bitrate for pings. E.g the recheck_delay_ms
121 // will be shorter if there are more connections available.
Jonas Oreland43336002020-03-26 20:59:03 +0100122 const int recheck_delay_ms = 0;
123 };
Jonas Oreland2f3c0192020-03-26 12:59:44 +0100124
Jonas Oreland09c452e2019-11-20 09:01:02 +0100125 virtual ~IceControllerInterface() = default;
126
127 // These setters are called when the state of P2PTransportChannel is mutated.
128 virtual void SetIceConfig(const IceConfig& config) = 0;
129 virtual void SetSelectedConnection(const Connection* selected_connection) = 0;
130 virtual void AddConnection(const Connection* connection) = 0;
131 virtual void OnConnectionDestroyed(const Connection* connection) = 0;
132
133 // These are all connections that has been added and not destroyed.
134 virtual rtc::ArrayView<const Connection*> connections() const = 0;
135
136 // Is there a pingable connection ?
137 // This function is used to boot-strap pinging, after this returns true
138 // SelectConnectionToPing() will be called periodically.
139 virtual bool HasPingableConnection() const = 0;
140
141 // Select a connection to Ping, or nullptr if none.
Jonas Oreland2f3c0192020-03-26 12:59:44 +0100142 virtual PingResult SelectConnectionToPing(int64_t last_ping_sent_ms) = 0;
Jonas Oreland09c452e2019-11-20 09:01:02 +0100143
Artem Titov2dbb4c92021-07-26 15:12:41 +0200144 // Compute the "STUN_ATTR_USE_CANDIDATE" for `conn`.
Jonas Oreland09c452e2019-11-20 09:01:02 +0100145 virtual bool GetUseCandidateAttr(const Connection* conn,
146 NominationMode mode,
147 IceMode remote_ice_mode) const = 0;
148
149 // These methods is only added to not have to change all unit tests
150 // that simulate pinging by marking a connection pinged.
151 virtual const Connection* FindNextPingableConnection() = 0;
152 virtual void MarkConnectionPinged(const Connection* con) = 0;
153
Artem Titov2dbb4c92021-07-26 15:12:41 +0200154 // Check if we should switch to `connection`.
Sameer Vijaykar3382c1c2022-06-02 11:29:09 +0200155 // This method is called for IceSwitchReasons that can switch directly
Jonas Oreland09c452e2019-11-20 09:01:02 +0100156 // i.e without resorting.
Sameer Vijaykar781c12e2022-06-02 16:01:12 +0200157 [[deprecated("bugs.webrtc.org/14125")]] virtual SwitchResult
Sameer Vijaykar2a1accd2022-06-03 11:39:39 +0200158 ShouldSwitchConnection(IceRecheckEvent reason, const Connection* connection) {
159 return ShouldSwitchConnection(IceRecheckEvent::FromType(reason.type),
Sameer Vijaykar781c12e2022-06-02 16:01:12 +0200160 connection);
Sameer Vijaykar3382c1c2022-06-02 11:29:09 +0200161 }
Sameer Vijaykar781c12e2022-06-02 16:01:12 +0200162 virtual SwitchResult ShouldSwitchConnection(IceSwitchReason reason,
163 const Connection* connection) = 0;
Jonas Oreland09c452e2019-11-20 09:01:02 +0100164
165 // Sort connections and check if we should switch.
Sameer Vijaykar781c12e2022-06-02 16:01:12 +0200166 [[deprecated("bugs.webrtc.org/14125")]] virtual SwitchResult
Sameer Vijaykar2a1accd2022-06-03 11:39:39 +0200167 SortAndSwitchConnection(IceRecheckEvent reason) {
168 return SortAndSwitchConnection(IceRecheckEvent::FromType(reason.type));
Sameer Vijaykar3382c1c2022-06-02 11:29:09 +0200169 }
Sameer Vijaykar781c12e2022-06-02 16:01:12 +0200170 virtual SwitchResult SortAndSwitchConnection(IceSwitchReason reason) = 0;
Jonas Oreland09c452e2019-11-20 09:01:02 +0100171
172 // Prune connections.
173 virtual std::vector<const Connection*> PruneConnections() = 0;
174};
175
176} // namespace cricket
177
178#endif // P2P_BASE_ICE_CONTROLLER_INTERFACE_H_