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