blob: a33315a3384b21387d3b9ec660ee8229d5e2371c [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
18#include "p2p/base/connection.h"
19#include "p2p/base/ice_transport_internal.h"
20
21namespace cricket {
22
23struct IceFieldTrials; // Forward declaration to avoid circular dependency.
24
25struct IceControllerEvent {
26 enum Type {
27 REMOTE_CANDIDATE_GENERATION_CHANGE,
28 NETWORK_PREFERENCE_CHANGE,
29 NEW_CONNECTION_FROM_LOCAL_CANDIDATE,
30 NEW_CONNECTION_FROM_REMOTE_CANDIDATE,
31 NEW_CONNECTION_FROM_UNKNOWN_REMOTE_ADDRESS,
32 NOMINATION_ON_CONTROLLED_SIDE,
33 DATA_RECEIVED,
34 CONNECT_STATE_CHANGE,
Jonas Orelandb5aa0a82019-12-03 09:59:11 +010035 SELECTED_CONNECTION_DESTROYED,
36 // The ICE_CONTROLLER_RECHECK enum value lets an IceController request
37 // P2PTransportChannel to recheck a switch periodically without an event
38 // taking place.
39 ICE_CONTROLLER_RECHECK,
Jonas Oreland09c452e2019-11-20 09:01:02 +010040 };
41
Sameer Vijaykar9453e5f2022-05-31 11:38:20 +000042 IceControllerEvent(const Type& _type) // NOLINT: runtime/explicit
43 : type(_type) {}
Jonas Oreland09c452e2019-11-20 09:01:02 +010044 std::string ToString() const;
45
46 Type type;
Jonas Orelandb5aa0a82019-12-03 09:59:11 +010047 int recheck_delay_ms = 0;
Jonas Oreland09c452e2019-11-20 09:01:02 +010048};
49
50// Defines the interface for a module that control
51// - which connection to ping
52// - which connection to use
53// - which connection to prune
Jonas Oreland49864b72020-06-05 14:55:35 +020054// - which connection to forget learned state on
Jonas Oreland09c452e2019-11-20 09:01:02 +010055//
Jonas Oreland49864b72020-06-05 14:55:35 +020056// The P2PTransportChannel owns (creates and destroys) Connections,
57// but P2PTransportChannel gives const pointers to the the IceController using
Artem Titov2dbb4c92021-07-26 15:12:41 +020058// `AddConnection`, i.e the IceController should not call any non-const methods
Jonas Oreland49864b72020-06-05 14:55:35 +020059// on a Connection but signal back in the interface if any mutable function
60// shall be called.
Jonas Oreland09c452e2019-11-20 09:01:02 +010061//
Jonas Oreland49864b72020-06-05 14:55:35 +020062// Current these are limited to:
63// Connection::Ping - returned in PingResult
64// Connection::Prune - retuned in PruneConnections
65// Connection::ForgetLearnedState - return in SwitchResult
66//
67// The IceController shall keep track of all connections added
Jonas Oreland09c452e2019-11-20 09:01:02 +010068// (and not destroyed) and give them back using the connections()-function-
69//
70// When a Connection gets destroyed
71// - signals on Connection::SignalDestroyed
72// - P2PTransportChannel calls IceController::OnConnectionDestroyed
73class IceControllerInterface {
74 public:
75 // This represents the result of a switch call.
76 struct SwitchResult {
77 // Connection that we should (optionally) switch to.
78 absl::optional<const Connection*> connection;
79
Jonas Orelandb5aa0a82019-12-03 09:59:11 +010080 // An optional recheck event for when a Switch() should be attempted again.
81 absl::optional<IceControllerEvent> recheck_event;
Jonas Oreland49864b72020-06-05 14:55:35 +020082
83 // A vector with connection to run ForgetLearnedState on.
84 std::vector<const Connection*> connections_to_forget_state_on;
Jonas Oreland09c452e2019-11-20 09:01:02 +010085 };
86
Jonas Oreland43336002020-03-26 20:59:03 +010087 // This represents the result of a call to SelectConnectionToPing.
88 struct PingResult {
89 PingResult(const Connection* conn, int _recheck_delay_ms)
Tim Na203b5492021-02-05 10:23:53 -080090 : connection(conn ? absl::optional<const Connection*>(conn)
91 : absl::nullopt),
92 recheck_delay_ms(_recheck_delay_ms) {}
Jonas Oreland43336002020-03-26 20:59:03 +010093
Jonas Oreland43336002020-03-26 20:59:03 +010094 // Connection that we should (optionally) ping.
95 const absl::optional<const Connection*> connection;
96
Jonas Orelandfa097a22020-03-27 15:12:52 +010097 // The delay before P2PTransportChannel shall call SelectConnectionToPing()
98 // again.
99 //
100 // Since the IceController determines which connection to ping and
101 // only returns one connection at a time, the recheck_delay_ms does not have
102 // any obvious implication on bitrate for pings. E.g the recheck_delay_ms
103 // will be shorter if there are more connections available.
Jonas Oreland43336002020-03-26 20:59:03 +0100104 const int recheck_delay_ms = 0;
105 };
Jonas Oreland2f3c0192020-03-26 12:59:44 +0100106
Jonas Oreland09c452e2019-11-20 09:01:02 +0100107 virtual ~IceControllerInterface() = default;
108
109 // These setters are called when the state of P2PTransportChannel is mutated.
110 virtual void SetIceConfig(const IceConfig& config) = 0;
111 virtual void SetSelectedConnection(const Connection* selected_connection) = 0;
112 virtual void AddConnection(const Connection* connection) = 0;
113 virtual void OnConnectionDestroyed(const Connection* connection) = 0;
114
115 // These are all connections that has been added and not destroyed.
116 virtual rtc::ArrayView<const Connection*> connections() const = 0;
117
118 // Is there a pingable connection ?
119 // This function is used to boot-strap pinging, after this returns true
120 // SelectConnectionToPing() will be called periodically.
121 virtual bool HasPingableConnection() const = 0;
122
123 // Select a connection to Ping, or nullptr if none.
Jonas Oreland2f3c0192020-03-26 12:59:44 +0100124 virtual PingResult SelectConnectionToPing(int64_t last_ping_sent_ms) = 0;
Jonas Oreland09c452e2019-11-20 09:01:02 +0100125
Artem Titov2dbb4c92021-07-26 15:12:41 +0200126 // Compute the "STUN_ATTR_USE_CANDIDATE" for `conn`.
Jonas Oreland09c452e2019-11-20 09:01:02 +0100127 virtual bool GetUseCandidateAttr(const Connection* conn,
128 NominationMode mode,
129 IceMode remote_ice_mode) const = 0;
130
131 // These methods is only added to not have to change all unit tests
132 // that simulate pinging by marking a connection pinged.
133 virtual const Connection* FindNextPingableConnection() = 0;
134 virtual void MarkConnectionPinged(const Connection* con) = 0;
135
Artem Titov2dbb4c92021-07-26 15:12:41 +0200136 // Check if we should switch to `connection`.
Sameer Vijaykar9453e5f2022-05-31 11:38:20 +0000137 // This method is called for IceControllerEvent's that can switch directly
Jonas Oreland09c452e2019-11-20 09:01:02 +0100138 // i.e without resorting.
Sameer Vijaykar9453e5f2022-05-31 11:38:20 +0000139 virtual SwitchResult ShouldSwitchConnection(IceControllerEvent reason,
140 const Connection* connection) = 0;
Jonas Oreland09c452e2019-11-20 09:01:02 +0100141
142 // Sort connections and check if we should switch.
Sameer Vijaykar9453e5f2022-05-31 11:38:20 +0000143 virtual SwitchResult SortAndSwitchConnection(IceControllerEvent reason) = 0;
Jonas Oreland09c452e2019-11-20 09:01:02 +0100144
145 // Prune connections.
146 virtual std::vector<const Connection*> PruneConnections() = 0;
147};
148
149} // namespace cricket
150
151#endif // P2P_BASE_ICE_CONTROLLER_INTERFACE_H_