blob: 54a76dc3e597ca7d62a04f52211615a30afbe1b0 [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/thread.h"
18
19namespace 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
Steve Antonf4172382020-01-27 15:45:02 -080024// methods like RegatherOnFailedNetworks on the PortAllocatorSession when
Qingsi Wang1b368942018-06-13 13:54:08 -070025// regathering is desired.
26//
Qingsi Wang1b368942018-06-13 13:54:08 -070027// "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().
42class BasicRegatheringController : public sigslot::has_slots<> {
43 public:
44 struct Config {
Steve Antonf4172382020-01-27 15:45:02 -080045 int regather_on_failed_networks_interval =
46 cricket::REGATHER_ON_FAILED_NETWORKS_INTERVAL;
Qingsi Wang1b368942018-06-13 13:54:08 -070047 };
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();
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.
72 void OnIceTransportStateChanged(cricket::IceTransportInternal*) {}
73 void OnIceTransportWritableState(rtc::PacketTransportInternal*) {}
74 void OnIceTransportReceivingState(rtc::PacketTransportInternal*) {}
Danil Chapovalov00c71832018-06-15 15:58:38 +020075 void OnIceTransportNetworkRouteChanged(absl::optional<rtc::NetworkRoute>) {}
Qingsi Wang1b368942018-06-13 13:54:08 -070076 // 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
Steve Antonf4172382020-01-27 15:45:02 -080087 // The following method perform the actual regathering, if the recent port
88 // allocator session has done the initial gathering.
89 void RegatherOnFailedNetworksIfDoneGathering();
Qingsi Wang1b368942018-06-13 13:54:08 -070090
91 Config config_;
92 cricket::IceTransportInternal* ice_transport_;
93 cricket::PortAllocatorSession* allocator_session_ = nullptr;
Qingsi Wang1b368942018-06-13 13:54:08 -070094 bool has_recurring_schedule_on_failed_networks_ = false;
95 rtc::Thread* thread_;
Qingsi Wang1b368942018-06-13 13:54:08 -070096 rtc::AsyncInvoker invoker_for_failed_networks_;
Qingsi Wang1b368942018-06-13 13:54:08 -070097};
98
99} // namespace webrtc
100
Steve Anton10542f22019-01-11 09:11:00 -0800101#endif // P2P_BASE_REGATHERING_CONTROLLER_H_