blob: bf5b8ea6fb0190af4c2beb6077e38006ea00479e [file] [log] [blame]
Sameer Vijaykar52dd1a52022-09-19 11:11:111/*
2 * Copyright 2022 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_WRAPPING_ACTIVE_ICE_CONTROLLER_H_
12#define P2P_BASE_WRAPPING_ACTIVE_ICE_CONTROLLER_H_
13
14#include <memory>
Florent Castelli8037fc62024-08-29 13:00:4015#include <optional>
Sameer Vijaykar52dd1a52022-09-19 11:11:1116
Sameer Vijaykar52dd1a52022-09-19 11:11:1117#include "api/task_queue/pending_task_safety_flag.h"
18#include "p2p/base/active_ice_controller_interface.h"
19#include "p2p/base/connection.h"
20#include "p2p/base/ice_agent_interface.h"
21#include "p2p/base/ice_controller_factory_interface.h"
22#include "p2p/base/ice_controller_interface.h"
23#include "p2p/base/ice_switch_reason.h"
24#include "p2p/base/ice_transport_internal.h"
25#include "p2p/base/transport_description.h"
26#include "rtc_base/thread.h"
27#include "rtc_base/thread_annotations.h"
28
29namespace cricket {
30
31// WrappingActiveIceController provides the functionality of a legacy passive
32// ICE controller but packaged as an active ICE Controller.
33class WrappingActiveIceController : public ActiveIceControllerInterface {
34 public:
35 // Constructs an active ICE controller wrapping an already constructed legacy
36 // ICE controller. Does not take ownership of the ICE agent, which must
37 // already exist and outlive the ICE controller.
38 WrappingActiveIceController(IceAgentInterface* ice_agent,
39 std::unique_ptr<IceControllerInterface> wrapped);
40 // Constructs an active ICE controller that wraps over a legacy ICE
41 // controller. The legacy ICE controller is constructed through a factory, if
42 // one is supplied. If not, a default BasicIceController is wrapped instead.
43 // Does not take ownership of the ICE agent, which must already exist and
44 // outlive the ICE controller.
45 WrappingActiveIceController(
46 IceAgentInterface* ice_agent,
47 IceControllerFactoryInterface* wrapped_factory,
48 const IceControllerFactoryArgs& wrapped_factory_args);
49 virtual ~WrappingActiveIceController();
50
51 void SetIceConfig(const IceConfig& config) override;
52 bool GetUseCandidateAttribute(const Connection* connection,
53 NominationMode mode,
54 IceMode remote_ice_mode) const override;
55
56 void OnConnectionAdded(const Connection* connection) override;
57 void OnConnectionPinged(const Connection* connection) override;
58 void OnConnectionUpdated(const Connection* connection) override;
59 void OnConnectionSwitched(const Connection* connection) override;
60 void OnConnectionDestroyed(const Connection* connection) override;
61
62 void OnSortAndSwitchRequest(IceSwitchReason reason) override;
63 void OnImmediateSortAndSwitchRequest(IceSwitchReason reason) override;
64 bool OnImmediateSwitchRequest(IceSwitchReason reason,
65 const Connection* selected) override;
66
67 // Only for unit tests
68 const Connection* FindNextPingableConnection() override;
69
70 private:
71 void MaybeStartPinging();
72 void SelectAndPingConnection();
73 void HandlePingResult(IceControllerInterface::PingResult result);
74
75 void SortAndSwitchToBestConnection(IceSwitchReason reason);
76 void HandleSwitchResult(IceSwitchReason reason_for_switch,
77 IceControllerInterface::SwitchResult result);
78 void UpdateStateOnConnectionsResorted();
79
80 void PruneConnections();
81
82 rtc::Thread* const network_thread_;
83 webrtc::ScopedTaskSafety task_safety_;
84
85 bool started_pinging_ RTC_GUARDED_BY(network_thread_) = false;
86 bool sort_pending_ RTC_GUARDED_BY(network_thread_) = false;
87 const Connection* selected_connection_ RTC_GUARDED_BY(network_thread_) =
88 nullptr;
89
90 std::unique_ptr<IceControllerInterface> wrapped_
91 RTC_GUARDED_BY(network_thread_);
92 IceAgentInterface& agent_ RTC_GUARDED_BY(network_thread_);
93};
94
95} // namespace cricket
96
97#endif // P2P_BASE_WRAPPING_ACTIVE_ICE_CONTROLLER_H_