blob: 4f8dc7253acd724b60e0a1c4d821c0b70e862993 [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,
35 SELECTED_CONNECTION_DESTROYED
36 };
37
38 IceControllerEvent(const Type& _type) // NOLINT: runtime/explicit
39 : type(_type) {}
40 std::string ToString() const;
41
42 Type type;
43 int dampening_delay = 0;
44};
45
46// Defines the interface for a module that control
47// - which connection to ping
48// - which connection to use
49// - which connection to prune
50//
51// P2PTransportChannel creates a |Connection| and adds a const pointer
52// to the IceController using |AddConnection|, i.e the IceController
53// should not call any non-const methods on a Connection.
54//
55// The IceController shall keeps track of all connections added
56// (and not destroyed) and give them back using the connections()-function-
57//
58// When a Connection gets destroyed
59// - signals on Connection::SignalDestroyed
60// - P2PTransportChannel calls IceController::OnConnectionDestroyed
61class IceControllerInterface {
62 public:
63 // This represents the result of a switch call.
64 struct SwitchResult {
65 // Connection that we should (optionally) switch to.
66 absl::optional<const Connection*> connection;
67
68 // Delay in milliseconds when we should resort and try switching again.
69 absl::optional<int> recheck_delay_ms;
70 };
71
72 virtual ~IceControllerInterface() = default;
73
74 // These setters are called when the state of P2PTransportChannel is mutated.
75 virtual void SetIceConfig(const IceConfig& config) = 0;
76 virtual void SetSelectedConnection(const Connection* selected_connection) = 0;
77 virtual void AddConnection(const Connection* connection) = 0;
78 virtual void OnConnectionDestroyed(const Connection* connection) = 0;
79
80 // These are all connections that has been added and not destroyed.
81 virtual rtc::ArrayView<const Connection*> connections() const = 0;
82
83 // Is there a pingable connection ?
84 // This function is used to boot-strap pinging, after this returns true
85 // SelectConnectionToPing() will be called periodically.
86 virtual bool HasPingableConnection() const = 0;
87
88 // Select a connection to Ping, or nullptr if none.
89 virtual std::pair<Connection*, int> SelectConnectionToPing(
90 int64_t last_ping_sent_ms) = 0;
91
92 // Compute the "STUN_ATTR_USE_CANDIDATE" for |conn|.
93 virtual bool GetUseCandidateAttr(const Connection* conn,
94 NominationMode mode,
95 IceMode remote_ice_mode) const = 0;
96
97 // These methods is only added to not have to change all unit tests
98 // that simulate pinging by marking a connection pinged.
99 virtual const Connection* FindNextPingableConnection() = 0;
100 virtual void MarkConnectionPinged(const Connection* con) = 0;
101
102 // Check if we should switch to |connection|.
103 // This method is called for IceControllerEvent's that can switch directly
104 // i.e without resorting.
105 virtual SwitchResult ShouldSwitchConnection(IceControllerEvent reason,
106 const Connection* connection) = 0;
107
108 // Sort connections and check if we should switch.
109 virtual SwitchResult SortAndSwitchConnection(IceControllerEvent reason) = 0;
110
111 // Prune connections.
112 virtual std::vector<const Connection*> PruneConnections() = 0;
113};
114
115} // namespace cricket
116
117#endif // P2P_BASE_ICE_CONTROLLER_INTERFACE_H_