blob: fb421cf0f9b3e44b5e9916c04022402fe59ef314 [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_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 09:29:0918#include "absl/types/optional.h"
Jonas Oreland09c452e2019-11-20 08:01:0219#include "p2p/base/connection.h"
Sameer Vijaykar3382c1c2022-06-02 09:29:0920#include "p2p/base/ice_switch_reason.h"
Jonas Oreland09c452e2019-11-20 08:01:0221#include "p2p/base/ice_transport_internal.h"
Daniel Chenge125a332023-12-02 22:16:5922#include "rtc_base/checks.h"
Sameer Vijaykara2653bc2023-01-26 15:34:0923#include "rtc_base/system/rtc_export.h"
Jonas Oreland09c452e2019-11-20 08:01:0224
25namespace cricket {
26
27struct IceFieldTrials; // Forward declaration to avoid circular dependency.
28
Sameer Vijaykara2653bc2023-01-26 15:34:0929struct RTC_EXPORT IceRecheckEvent {
Sameer Vijaykar2a1accd2022-06-03 09:39:3930 IceRecheckEvent(IceSwitchReason _reason, int _recheck_delay_ms)
Sameer Vijaykar9a912862022-06-03 12:03:1831 : reason(_reason), recheck_delay_ms(_recheck_delay_ms) {}
Sameer Vijaykar3382c1c2022-06-02 09:29:0932
Jonas Oreland09c452e2019-11-20 08:01:0233 std::string ToString() const;
34
Sameer Vijaykar3382c1c2022-06-02 09:29:0935 IceSwitchReason reason;
Sameer Vijaykar9a912862022-06-03 12:03:1836 int recheck_delay_ms;
Jonas Oreland09c452e2019-11-20 08:01:0237};
38
39// Defines the interface for a module that control
40// - which connection to ping
41// - which connection to use
42// - which connection to prune
Jonas Oreland49864b72020-06-05 12:55:3543// - which connection to forget learned state on
Jonas Oreland09c452e2019-11-20 08:01:0244//
Jonas Oreland49864b72020-06-05 12:55:3545// The P2PTransportChannel owns (creates and destroys) Connections,
46// but P2PTransportChannel gives const pointers to the the IceController using
Artem Titov2dbb4c92021-07-26 13:12:4147// `AddConnection`, i.e the IceController should not call any non-const methods
Jonas Oreland49864b72020-06-05 12:55:3548// on a Connection but signal back in the interface if any mutable function
49// shall be called.
Jonas Oreland09c452e2019-11-20 08:01:0250//
Jonas Oreland49864b72020-06-05 12:55:3551// Current these are limited to:
52// Connection::Ping - returned in PingResult
53// Connection::Prune - retuned in PruneConnections
54// Connection::ForgetLearnedState - return in SwitchResult
55//
56// The IceController shall keep track of all connections added
Daniel Chenge125a332023-12-02 22:16:5957// (and not destroyed) and give them back using the GetConnections() function.
Jonas Oreland09c452e2019-11-20 08:01:0258//
59// When a Connection gets destroyed
60// - signals on Connection::SignalDestroyed
61// - P2PTransportChannel calls IceController::OnConnectionDestroyed
62class IceControllerInterface {
63 public:
64 // This represents the result of a switch call.
65 struct SwitchResult {
66 // Connection that we should (optionally) switch to.
67 absl::optional<const Connection*> connection;
68
Jonas Orelandb5aa0a82019-12-03 08:59:1169 // An optional recheck event for when a Switch() should be attempted again.
Sameer Vijaykar2a1accd2022-06-03 09:39:3970 absl::optional<IceRecheckEvent> recheck_event;
Jonas Oreland49864b72020-06-05 12:55:3571
72 // A vector with connection to run ForgetLearnedState on.
73 std::vector<const Connection*> connections_to_forget_state_on;
Jonas Oreland09c452e2019-11-20 08:01:0274 };
75
Jonas Oreland43336002020-03-26 19:59:0376 // This represents the result of a call to SelectConnectionToPing.
77 struct PingResult {
78 PingResult(const Connection* conn, int _recheck_delay_ms)
Tim Na203b5492021-02-05 18:23:5379 : connection(conn ? absl::optional<const Connection*>(conn)
80 : absl::nullopt),
81 recheck_delay_ms(_recheck_delay_ms) {}
Jonas Oreland43336002020-03-26 19:59:0382
Jonas Oreland43336002020-03-26 19:59:0383 // Connection that we should (optionally) ping.
84 const absl::optional<const Connection*> connection;
85
Jonas Orelandfa097a22020-03-27 14:12:5286 // The delay before P2PTransportChannel shall call SelectConnectionToPing()
87 // again.
88 //
89 // Since the IceController determines which connection to ping and
90 // only returns one connection at a time, the recheck_delay_ms does not have
91 // any obvious implication on bitrate for pings. E.g the recheck_delay_ms
92 // will be shorter if there are more connections available.
Jonas Oreland43336002020-03-26 19:59:0393 const int recheck_delay_ms = 0;
94 };
Jonas Oreland2f3c0192020-03-26 11:59:4495
Jonas Oreland09c452e2019-11-20 08:01:0296 virtual ~IceControllerInterface() = default;
97
98 // These setters are called when the state of P2PTransportChannel is mutated.
99 virtual void SetIceConfig(const IceConfig& config) = 0;
100 virtual void SetSelectedConnection(const Connection* selected_connection) = 0;
101 virtual void AddConnection(const Connection* connection) = 0;
102 virtual void OnConnectionDestroyed(const Connection* connection) = 0;
103
104 // These are all connections that has been added and not destroyed.
Daniel Chenge125a332023-12-02 22:16:59105 virtual rtc::ArrayView<const Connection* const> GetConnections() const {
106 // Stub implementation to simplify downstream roll.
107 RTC_CHECK_NOTREACHED();
108 return {};
109 }
110 // TODO(bugs.webrtc.org/15702): Remove this after downstream is cleaned up.
111 virtual rtc::ArrayView<const Connection*> connections() const {
112 // Stub implementation to simplify downstream removal.
113 RTC_CHECK_NOTREACHED();
114 return {};
115 }
Jonas Oreland09c452e2019-11-20 08:01:02116
117 // Is there a pingable connection ?
118 // This function is used to boot-strap pinging, after this returns true
119 // SelectConnectionToPing() will be called periodically.
120 virtual bool HasPingableConnection() const = 0;
121
122 // Select a connection to Ping, or nullptr if none.
Jonas Oreland2f3c0192020-03-26 11:59:44123 virtual PingResult SelectConnectionToPing(int64_t last_ping_sent_ms) = 0;
Jonas Oreland09c452e2019-11-20 08:01:02124
Artem Titov2dbb4c92021-07-26 13:12:41125 // Compute the "STUN_ATTR_USE_CANDIDATE" for `conn`.
Jonas Oreland09c452e2019-11-20 08:01:02126 virtual bool GetUseCandidateAttr(const Connection* conn,
127 NominationMode mode,
128 IceMode remote_ice_mode) const = 0;
129
130 // These methods is only added to not have to change all unit tests
131 // that simulate pinging by marking a connection pinged.
132 virtual const Connection* FindNextPingableConnection() = 0;
133 virtual void MarkConnectionPinged(const Connection* con) = 0;
134
Artem Titov2dbb4c92021-07-26 13:12:41135 // Check if we should switch to `connection`.
Sameer Vijaykar3382c1c2022-06-02 09:29:09136 // This method is called for IceSwitchReasons that can switch directly
Jonas Oreland09c452e2019-11-20 08:01:02137 // i.e without resorting.
Sameer Vijaykar781c12e2022-06-02 14:01:12138 virtual SwitchResult ShouldSwitchConnection(IceSwitchReason reason,
139 const Connection* connection) = 0;
Jonas Oreland09c452e2019-11-20 08:01:02140
141 // Sort connections and check if we should switch.
Sameer Vijaykar781c12e2022-06-02 14:01:12142 virtual SwitchResult SortAndSwitchConnection(IceSwitchReason reason) = 0;
Jonas Oreland09c452e2019-11-20 08:01:02143
144 // Prune connections.
145 virtual std::vector<const Connection*> PruneConnections() = 0;
146};
147
148} // namespace cricket
149
150#endif // P2P_BASE_ICE_CONTROLLER_INTERFACE_H_