xref: /aosp_15_r20/external/webrtc/p2p/base/ice_controller_interface.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2019 The WebRTC Project Authors. All rights reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef P2P_BASE_ICE_CONTROLLER_INTERFACE_H_
12*d9f75844SAndroid Build Coastguard Worker #define P2P_BASE_ICE_CONTROLLER_INTERFACE_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <string>
15*d9f75844SAndroid Build Coastguard Worker #include <utility>
16*d9f75844SAndroid Build Coastguard Worker #include <vector>
17*d9f75844SAndroid Build Coastguard Worker 
18*d9f75844SAndroid Build Coastguard Worker #include "absl/types/optional.h"
19*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/connection.h"
20*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/ice_switch_reason.h"
21*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/ice_transport_internal.h"
22*d9f75844SAndroid Build Coastguard Worker 
23*d9f75844SAndroid Build Coastguard Worker namespace cricket {
24*d9f75844SAndroid Build Coastguard Worker 
25*d9f75844SAndroid Build Coastguard Worker struct IceFieldTrials;  // Forward declaration to avoid circular dependency.
26*d9f75844SAndroid Build Coastguard Worker 
27*d9f75844SAndroid Build Coastguard Worker struct IceRecheckEvent {
IceRecheckEventIceRecheckEvent28*d9f75844SAndroid Build Coastguard Worker   IceRecheckEvent(IceSwitchReason _reason, int _recheck_delay_ms)
29*d9f75844SAndroid Build Coastguard Worker       : reason(_reason), recheck_delay_ms(_recheck_delay_ms) {}
30*d9f75844SAndroid Build Coastguard Worker 
31*d9f75844SAndroid Build Coastguard Worker   std::string ToString() const;
32*d9f75844SAndroid Build Coastguard Worker 
33*d9f75844SAndroid Build Coastguard Worker   IceSwitchReason reason;
34*d9f75844SAndroid Build Coastguard Worker   int recheck_delay_ms;
35*d9f75844SAndroid Build Coastguard Worker };
36*d9f75844SAndroid Build Coastguard Worker 
37*d9f75844SAndroid Build Coastguard Worker // Defines the interface for a module that control
38*d9f75844SAndroid Build Coastguard Worker // - which connection to ping
39*d9f75844SAndroid Build Coastguard Worker // - which connection to use
40*d9f75844SAndroid Build Coastguard Worker // - which connection to prune
41*d9f75844SAndroid Build Coastguard Worker // - which connection to forget learned state on
42*d9f75844SAndroid Build Coastguard Worker //
43*d9f75844SAndroid Build Coastguard Worker // The P2PTransportChannel owns (creates and destroys) Connections,
44*d9f75844SAndroid Build Coastguard Worker // but P2PTransportChannel gives const pointers to the the IceController using
45*d9f75844SAndroid Build Coastguard Worker // `AddConnection`, i.e the IceController should not call any non-const methods
46*d9f75844SAndroid Build Coastguard Worker // on a Connection but signal back in the interface if any mutable function
47*d9f75844SAndroid Build Coastguard Worker // shall be called.
48*d9f75844SAndroid Build Coastguard Worker //
49*d9f75844SAndroid Build Coastguard Worker // Current these are limited to:
50*d9f75844SAndroid Build Coastguard Worker // Connection::Ping               - returned in PingResult
51*d9f75844SAndroid Build Coastguard Worker // Connection::Prune              - retuned in PruneConnections
52*d9f75844SAndroid Build Coastguard Worker // Connection::ForgetLearnedState - return in SwitchResult
53*d9f75844SAndroid Build Coastguard Worker //
54*d9f75844SAndroid Build Coastguard Worker // The IceController shall keep track of all connections added
55*d9f75844SAndroid Build Coastguard Worker // (and not destroyed) and give them back using the connections()-function-
56*d9f75844SAndroid Build Coastguard Worker //
57*d9f75844SAndroid Build Coastguard Worker // When a Connection gets destroyed
58*d9f75844SAndroid Build Coastguard Worker // - signals on Connection::SignalDestroyed
59*d9f75844SAndroid Build Coastguard Worker // - P2PTransportChannel calls IceController::OnConnectionDestroyed
60*d9f75844SAndroid Build Coastguard Worker class IceControllerInterface {
61*d9f75844SAndroid Build Coastguard Worker  public:
62*d9f75844SAndroid Build Coastguard Worker   // This represents the result of a switch call.
63*d9f75844SAndroid Build Coastguard Worker   struct SwitchResult {
64*d9f75844SAndroid Build Coastguard Worker     // Connection that we should (optionally) switch to.
65*d9f75844SAndroid Build Coastguard Worker     absl::optional<const Connection*> connection;
66*d9f75844SAndroid Build Coastguard Worker 
67*d9f75844SAndroid Build Coastguard Worker     // An optional recheck event for when a Switch() should be attempted again.
68*d9f75844SAndroid Build Coastguard Worker     absl::optional<IceRecheckEvent> recheck_event;
69*d9f75844SAndroid Build Coastguard Worker 
70*d9f75844SAndroid Build Coastguard Worker     // A vector with connection to run ForgetLearnedState on.
71*d9f75844SAndroid Build Coastguard Worker     std::vector<const Connection*> connections_to_forget_state_on;
72*d9f75844SAndroid Build Coastguard Worker   };
73*d9f75844SAndroid Build Coastguard Worker 
74*d9f75844SAndroid Build Coastguard Worker   // This represents the result of a call to SelectConnectionToPing.
75*d9f75844SAndroid Build Coastguard Worker   struct PingResult {
PingResultPingResult76*d9f75844SAndroid Build Coastguard Worker     PingResult(const Connection* conn, int _recheck_delay_ms)
77*d9f75844SAndroid Build Coastguard Worker         : connection(conn ? absl::optional<const Connection*>(conn)
78*d9f75844SAndroid Build Coastguard Worker                           : absl::nullopt),
79*d9f75844SAndroid Build Coastguard Worker           recheck_delay_ms(_recheck_delay_ms) {}
80*d9f75844SAndroid Build Coastguard Worker 
81*d9f75844SAndroid Build Coastguard Worker     // Connection that we should (optionally) ping.
82*d9f75844SAndroid Build Coastguard Worker     const absl::optional<const Connection*> connection;
83*d9f75844SAndroid Build Coastguard Worker 
84*d9f75844SAndroid Build Coastguard Worker     // The delay before P2PTransportChannel shall call SelectConnectionToPing()
85*d9f75844SAndroid Build Coastguard Worker     // again.
86*d9f75844SAndroid Build Coastguard Worker     //
87*d9f75844SAndroid Build Coastguard Worker     // Since the IceController determines which connection to ping and
88*d9f75844SAndroid Build Coastguard Worker     // only returns one connection at a time, the recheck_delay_ms does not have
89*d9f75844SAndroid Build Coastguard Worker     // any obvious implication on bitrate for pings. E.g the recheck_delay_ms
90*d9f75844SAndroid Build Coastguard Worker     // will be shorter if there are more connections available.
91*d9f75844SAndroid Build Coastguard Worker     const int recheck_delay_ms = 0;
92*d9f75844SAndroid Build Coastguard Worker   };
93*d9f75844SAndroid Build Coastguard Worker 
94*d9f75844SAndroid Build Coastguard Worker   virtual ~IceControllerInterface() = default;
95*d9f75844SAndroid Build Coastguard Worker 
96*d9f75844SAndroid Build Coastguard Worker   // These setters are called when the state of P2PTransportChannel is mutated.
97*d9f75844SAndroid Build Coastguard Worker   virtual void SetIceConfig(const IceConfig& config) = 0;
98*d9f75844SAndroid Build Coastguard Worker   virtual void SetSelectedConnection(const Connection* selected_connection) = 0;
99*d9f75844SAndroid Build Coastguard Worker   virtual void AddConnection(const Connection* connection) = 0;
100*d9f75844SAndroid Build Coastguard Worker   virtual void OnConnectionDestroyed(const Connection* connection) = 0;
101*d9f75844SAndroid Build Coastguard Worker 
102*d9f75844SAndroid Build Coastguard Worker   // These are all connections that has been added and not destroyed.
103*d9f75844SAndroid Build Coastguard Worker   virtual rtc::ArrayView<const Connection*> connections() const = 0;
104*d9f75844SAndroid Build Coastguard Worker 
105*d9f75844SAndroid Build Coastguard Worker   // Is there a pingable connection ?
106*d9f75844SAndroid Build Coastguard Worker   // This function is used to boot-strap pinging, after this returns true
107*d9f75844SAndroid Build Coastguard Worker   // SelectConnectionToPing() will be called periodically.
108*d9f75844SAndroid Build Coastguard Worker   virtual bool HasPingableConnection() const = 0;
109*d9f75844SAndroid Build Coastguard Worker 
110*d9f75844SAndroid Build Coastguard Worker   // Select a connection to Ping, or nullptr if none.
111*d9f75844SAndroid Build Coastguard Worker   virtual PingResult SelectConnectionToPing(int64_t last_ping_sent_ms) = 0;
112*d9f75844SAndroid Build Coastguard Worker 
113*d9f75844SAndroid Build Coastguard Worker   // Compute the "STUN_ATTR_USE_CANDIDATE" for `conn`.
114*d9f75844SAndroid Build Coastguard Worker   virtual bool GetUseCandidateAttr(const Connection* conn,
115*d9f75844SAndroid Build Coastguard Worker                                    NominationMode mode,
116*d9f75844SAndroid Build Coastguard Worker                                    IceMode remote_ice_mode) const = 0;
117*d9f75844SAndroid Build Coastguard Worker 
118*d9f75844SAndroid Build Coastguard Worker   // These methods is only added to not have to change all unit tests
119*d9f75844SAndroid Build Coastguard Worker   // that simulate pinging by marking a connection pinged.
120*d9f75844SAndroid Build Coastguard Worker   virtual const Connection* FindNextPingableConnection() = 0;
121*d9f75844SAndroid Build Coastguard Worker   virtual void MarkConnectionPinged(const Connection* con) = 0;
122*d9f75844SAndroid Build Coastguard Worker 
123*d9f75844SAndroid Build Coastguard Worker   // Check if we should switch to `connection`.
124*d9f75844SAndroid Build Coastguard Worker   // This method is called for IceSwitchReasons that can switch directly
125*d9f75844SAndroid Build Coastguard Worker   // i.e without resorting.
126*d9f75844SAndroid Build Coastguard Worker   virtual SwitchResult ShouldSwitchConnection(IceSwitchReason reason,
127*d9f75844SAndroid Build Coastguard Worker                                               const Connection* connection) = 0;
128*d9f75844SAndroid Build Coastguard Worker 
129*d9f75844SAndroid Build Coastguard Worker   // Sort connections and check if we should switch.
130*d9f75844SAndroid Build Coastguard Worker   virtual SwitchResult SortAndSwitchConnection(IceSwitchReason reason) = 0;
131*d9f75844SAndroid Build Coastguard Worker 
132*d9f75844SAndroid Build Coastguard Worker   // Prune connections.
133*d9f75844SAndroid Build Coastguard Worker   virtual std::vector<const Connection*> PruneConnections() = 0;
134*d9f75844SAndroid Build Coastguard Worker };
135*d9f75844SAndroid Build Coastguard Worker 
136*d9f75844SAndroid Build Coastguard Worker }  // namespace cricket
137*d9f75844SAndroid Build Coastguard Worker 
138*d9f75844SAndroid Build Coastguard Worker #endif  // P2P_BASE_ICE_CONTROLLER_INTERFACE_H_
139