blob: a0dfb8053dbe26c05206404253a991578b51c73c [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
Tomas Gunnarsson4b5d3232020-11-23 09:32:59 +010014#include <memory>
15
Artem Titovc374d112022-06-16 21:27:45 +020016#include "api/task_queue/pending_task_safety_flag.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "p2p/base/ice_transport_internal.h"
18#include "p2p/base/port_allocator.h"
Qingsi Wang1b368942018-06-13 13:54:08 -070019#include "rtc_base/thread.h"
20
21namespace 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
Steve Antonf4172382020-01-27 15:45:02 -080026// methods like RegatherOnFailedNetworks on the PortAllocatorSession when
Qingsi Wang1b368942018-06-13 13:54:08 -070027// regathering is desired.
28//
Qingsi Wang1b368942018-06-13 13:54:08 -070029// "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().
44class BasicRegatheringController : public sigslot::has_slots<> {
45 public:
46 struct Config {
Steve Antonf4172382020-01-27 15:45:02 -080047 int regather_on_failed_networks_interval =
48 cricket::REGATHER_ON_FAILED_NETWORKS_INTERVAL;
Qingsi Wang1b368942018-06-13 13:54:08 -070049 };
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();
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.
74 void OnIceTransportStateChanged(cricket::IceTransportInternal*) {}
75 void OnIceTransportWritableState(rtc::PacketTransportInternal*) {}
76 void OnIceTransportReceivingState(rtc::PacketTransportInternal*) {}
Danil Chapovalov00c71832018-06-15 15:58:38 +020077 void OnIceTransportNetworkRouteChanged(absl::optional<rtc::NetworkRoute>) {}
Qingsi Wang1b368942018-06-13 13:54:08 -070078 // 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();
Qingsi Wang1b368942018-06-13 13:54:08 -070085
Tomas Gunnarsson4b5d3232020-11-23 09:32:59 +010086 // 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_;
Qingsi Wang1b368942018-06-13 13:54:08 -070089 Config config_;
90 cricket::IceTransportInternal* ice_transport_;
91 cricket::PortAllocatorSession* allocator_session_ = nullptr;
Tomas Gunnarsson4b5d3232020-11-23 09:32:59 +010092 rtc::Thread* const thread_;
Qingsi Wang1b368942018-06-13 13:54:08 -070093};
94
95} // namespace webrtc
96
Steve Anton10542f22019-01-11 09:11:00 -080097#endif // P2P_BASE_REGATHERING_CONTROLLER_H_