blob: 234aea3b57dc6026100dc81ef176b95c1ec3530e [file] [log] [blame]
Qingsi Wang1b368942018-06-13 13:54:08 -07001/*
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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef P2P_BASE_REGATHERING_CONTROLLER_H_
12#define P2P_BASE_REGATHERING_CONTROLLER_H_
Qingsi Wang1b368942018-06-13 13:54:08 -070013
Steve Anton10542f22019-01-11 09:11:00 -080014#include "p2p/base/ice_transport_internal.h"
15#include "p2p/base/port_allocator.h"
16#include "rtc_base/async_invoker.h"
Qingsi Wang1b368942018-06-13 13:54:08 -070017#include "rtc_base/random.h"
18#include "rtc_base/thread.h"
19
20namespace webrtc {
21
22// Controls regathering of candidates for the ICE transport passed into it,
23// reacting to signals like SignalWritableState, SignalNetworkRouteChange, etc.,
24// using methods like GetStats to get additional information, and calling
25// methods like RegatherOnAllNetworks on the PortAllocatorSession when
26// regathering is desired.
27//
28// TODO(qingsi): Add the description of behavior when autonomous regathering is
29// implemented.
30//
31// "Regathering" is defined as gathering additional candidates within a single
32// ICE generation (or in other words, PortAllocatorSession), and is possible
33// when "continual gathering" is enabled. This may allow connectivity to be
34// maintained and/or restored without a full ICE restart.
35//
36// Regathering will only begin after PortAllocationSession is set via
37// set_allocator_session. This should be called any time the "active"
38// PortAllocatorSession is changed (in other words, when an ICE restart occurs),
39// so that candidates are gathered for the "current" ICE generation.
40//
41// All methods of BasicRegatheringController should be called on the same
42// thread as the one passed to the constructor, and this thread should be the
43// same one where PortAllocatorSession runs, which is also identical to the
44// network thread of the ICE transport, as given by
45// P2PTransportChannel::thread().
46class BasicRegatheringController : public sigslot::has_slots<> {
47 public:
48 struct Config {
Danil Chapovalov00c71832018-06-15 15:58:38 +020049 Config(const absl::optional<rtc::IntervalRange>&
Qingsi Wang1b368942018-06-13 13:54:08 -070050 regather_on_all_networks_interval_range,
51 int regather_on_failed_networks_interval);
52 Config(const Config& other);
53 ~Config();
54 Config& operator=(const Config& other);
Danil Chapovalov00c71832018-06-15 15:58:38 +020055 absl::optional<rtc::IntervalRange> regather_on_all_networks_interval_range;
Qingsi Wang1b368942018-06-13 13:54:08 -070056 int regather_on_failed_networks_interval;
57 };
58
59 BasicRegatheringController() = delete;
60 BasicRegatheringController(const Config& config,
61 cricket::IceTransportInternal* ice_transport,
62 rtc::Thread* thread);
63 ~BasicRegatheringController() override;
64 // TODO(qingsi): Remove this method after implementing a new signal in
65 // P2PTransportChannel and reacting to that signal for the initial schedules
66 // of regathering.
67 void Start();
68 void set_allocator_session(cricket::PortAllocatorSession* allocator_session) {
69 allocator_session_ = allocator_session;
70 }
71 // Setting a different config of the regathering interval range on all
72 // networks cancels and reschedules the recurring schedules, if any, of
73 // regathering on all networks. The same applies to the change of the
74 // regathering interval on the failed networks. This rescheduling behavior is
75 // seperately defined for the two config parameters.
76 void SetConfig(const Config& config);
77
78 private:
79 // TODO(qingsi): Implement the following methods and use methods from the ICE
80 // transport like GetStats to get additional information for the decision
81 // making in regathering.
82 void OnIceTransportStateChanged(cricket::IceTransportInternal*) {}
83 void OnIceTransportWritableState(rtc::PacketTransportInternal*) {}
84 void OnIceTransportReceivingState(rtc::PacketTransportInternal*) {}
Danil Chapovalov00c71832018-06-15 15:58:38 +020085 void OnIceTransportNetworkRouteChanged(absl::optional<rtc::NetworkRoute>) {}
Qingsi Wang1b368942018-06-13 13:54:08 -070086 // Schedules delayed and repeated regathering of local candidates on all
87 // networks, where the delay in milliseconds is randomly sampled from the
88 // range in the config. The delay of each repetition is independently sampled
89 // from the same range. When scheduled, all previous schedules are canceled.
90 void ScheduleRecurringRegatheringOnAllNetworks();
91 // Schedules delayed and repeated regathering of local candidates on failed
92 // networks, where the delay in milliseconds is given by the config. Each
93 // repetition is separated by the same delay. When scheduled, all previous
94 // schedules are canceled.
95 void ScheduleRecurringRegatheringOnFailedNetworks();
96 // Cancels regathering scheduled by ScheduleRecurringRegatheringOnAllNetworks.
97 void CancelScheduledRecurringRegatheringOnAllNetworks();
98 // Cancels regathering scheduled by
99 // ScheduleRecurringRegatheringOnFailedNetworks.
100 void CancelScheduledRecurringRegatheringOnFailedNetworks();
101
102 rtc::Thread* thread() const { return thread_; }
103 // The following two methods perform the actual regathering, if the recent
104 // port allocator session has done the initial gathering.
105 void RegatherOnAllNetworksIfDoneGathering(bool repeated);
106 void RegatherOnFailedNetworksIfDoneGathering(bool repeated);
107 // Samples a delay from the uniform distribution in the given range.
108 int SampleRegatherAllNetworksInterval(const rtc::IntervalRange& range);
109
110 Config config_;
111 cricket::IceTransportInternal* ice_transport_;
112 cricket::PortAllocatorSession* allocator_session_ = nullptr;
113 bool has_recurring_schedule_on_all_networks_ = false;
114 bool has_recurring_schedule_on_failed_networks_ = false;
115 rtc::Thread* thread_;
116 rtc::AsyncInvoker invoker_for_all_networks_;
117 rtc::AsyncInvoker invoker_for_failed_networks_;
118 // Used to generate random intervals for regather_all_networks_interval_range.
119 Random rand_;
120};
121
122} // namespace webrtc
123
Steve Anton10542f22019-01-11 09:11:00 -0800124#endif // P2P_BASE_REGATHERING_CONTROLLER_H_