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