xref: /aosp_15_r20/external/webrtc/p2p/base/regathering_controller.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2018 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_REGATHERING_CONTROLLER_H_
12 #define P2P_BASE_REGATHERING_CONTROLLER_H_
13 
14 #include <memory>
15 
16 #include "api/task_queue/pending_task_safety_flag.h"
17 #include "p2p/base/ice_transport_internal.h"
18 #include "p2p/base/port_allocator.h"
19 #include "rtc_base/thread.h"
20 
21 namespace webrtc {
22 
23 // Controls regathering of candidates for the ICE transport passed into it,
24 // reacting to signals like SignalWritableState, SignalNetworkRouteChange, etc.,
25 // using methods like GetStats to get additional information, and calling
26 // methods like RegatherOnFailedNetworks on the PortAllocatorSession when
27 // regathering is desired.
28 //
29 // "Regathering" is defined as gathering additional candidates within a single
30 // ICE generation (or in other words, PortAllocatorSession), and is possible
31 // when "continual gathering" is enabled. This may allow connectivity to be
32 // maintained and/or restored without a full ICE restart.
33 //
34 // Regathering will only begin after PortAllocationSession is set via
35 // set_allocator_session. This should be called any time the "active"
36 // PortAllocatorSession is changed (in other words, when an ICE restart occurs),
37 // so that candidates are gathered for the "current" ICE generation.
38 //
39 // All methods of BasicRegatheringController should be called on the same
40 // thread as the one passed to the constructor, and this thread should be the
41 // same one where PortAllocatorSession runs, which is also identical to the
42 // network thread of the ICE transport, as given by
43 // P2PTransportChannel::thread().
44 class BasicRegatheringController : public sigslot::has_slots<> {
45  public:
46   struct Config {
47     int regather_on_failed_networks_interval =
48         cricket::REGATHER_ON_FAILED_NETWORKS_INTERVAL;
49   };
50 
51   BasicRegatheringController() = delete;
52   BasicRegatheringController(const Config& config,
53                              cricket::IceTransportInternal* ice_transport,
54                              rtc::Thread* thread);
55   ~BasicRegatheringController() override;
56   // TODO(qingsi): Remove this method after implementing a new signal in
57   // P2PTransportChannel and reacting to that signal for the initial schedules
58   // of regathering.
59   void Start();
set_allocator_session(cricket::PortAllocatorSession * allocator_session)60   void set_allocator_session(cricket::PortAllocatorSession* allocator_session) {
61     allocator_session_ = allocator_session;
62   }
63   // Setting a different config of the regathering interval range on all
64   // networks cancels and reschedules the recurring schedules, if any, of
65   // regathering on all networks. The same applies to the change of the
66   // regathering interval on the failed networks. This rescheduling behavior is
67   // seperately defined for the two config parameters.
68   void SetConfig(const Config& config);
69 
70  private:
71   // TODO(qingsi): Implement the following methods and use methods from the ICE
72   // transport like GetStats to get additional information for the decision
73   // making in regathering.
OnIceTransportStateChanged(cricket::IceTransportInternal *)74   void OnIceTransportStateChanged(cricket::IceTransportInternal*) {}
OnIceTransportWritableState(rtc::PacketTransportInternal *)75   void OnIceTransportWritableState(rtc::PacketTransportInternal*) {}
OnIceTransportReceivingState(rtc::PacketTransportInternal *)76   void OnIceTransportReceivingState(rtc::PacketTransportInternal*) {}
OnIceTransportNetworkRouteChanged(absl::optional<rtc::NetworkRoute>)77   void OnIceTransportNetworkRouteChanged(absl::optional<rtc::NetworkRoute>) {}
78   // Schedules delayed and repeated regathering of local candidates on failed
79   // networks, where the delay in milliseconds is given by the config. Each
80   // repetition is separated by the same delay. When scheduled, all previous
81   // schedules are canceled.
82   void ScheduleRecurringRegatheringOnFailedNetworks();
83   // Cancels regathering scheduled by ScheduleRecurringRegatheringOnAllNetworks.
84   void CancelScheduledRecurringRegatheringOnAllNetworks();
85 
86   // We use a flag to be able to cancel pending regathering operations when
87   // the object goes out of scope or the config changes.
88   std::unique_ptr<ScopedTaskSafety> pending_regathering_;
89   Config config_;
90   cricket::IceTransportInternal* ice_transport_;
91   cricket::PortAllocatorSession* allocator_session_ = nullptr;
92   rtc::Thread* const thread_;
93 };
94 
95 }  // namespace webrtc
96 
97 #endif  // P2P_BASE_REGATHERING_CONTROLLER_H_
98