blob: fe38a3e4d413ddbaae4a33736c12bb6c9cf645f0 [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#include "p2p/base/regathering_controller.h"
Qingsi Wang1b368942018-06-13 13:54:08 -070012
13namespace webrtc {
14
Qingsi Wang1b368942018-06-13 13:54:08 -070015BasicRegatheringController::BasicRegatheringController(
16 const Config& config,
17 cricket::IceTransportInternal* ice_transport,
18 rtc::Thread* thread)
Steve Antonf4172382020-01-27 15:45:02 -080019 : config_(config), ice_transport_(ice_transport), thread_(thread) {
Qingsi Wang1b368942018-06-13 13:54:08 -070020 RTC_DCHECK(ice_transport_);
21 RTC_DCHECK(thread_);
Alex Loiko9289eda2018-11-23 16:18:59 +000022 ice_transport_->SignalStateChanged.connect(
Qingsi Wang1b368942018-06-13 13:54:08 -070023 this, &BasicRegatheringController::OnIceTransportStateChanged);
24 ice_transport->SignalWritableState.connect(
25 this, &BasicRegatheringController::OnIceTransportWritableState);
26 ice_transport->SignalReceivingState.connect(
27 this, &BasicRegatheringController::OnIceTransportReceivingState);
28 ice_transport->SignalNetworkRouteChanged.connect(
29 this, &BasicRegatheringController::OnIceTransportNetworkRouteChanged);
30}
31
32BasicRegatheringController::~BasicRegatheringController() = default;
33
34void BasicRegatheringController::Start() {
35 ScheduleRecurringRegatheringOnFailedNetworks();
Qingsi Wang1b368942018-06-13 13:54:08 -070036}
37
38void BasicRegatheringController::SetConfig(const Config& config) {
Qingsi Wang1b368942018-06-13 13:54:08 -070039 bool need_cancel_and_reschedule_on_failed_networks =
40 has_recurring_schedule_on_failed_networks_ &&
41 (config_.regather_on_failed_networks_interval !=
42 config.regather_on_failed_networks_interval);
43 config_ = config;
Qingsi Wang1b368942018-06-13 13:54:08 -070044 if (need_cancel_and_reschedule_on_failed_networks) {
45 CancelScheduledRecurringRegatheringOnFailedNetworks();
46 ScheduleRecurringRegatheringOnFailedNetworks();
47 }
48}
49
Qingsi Wang1b368942018-06-13 13:54:08 -070050void BasicRegatheringController::
51 ScheduleRecurringRegatheringOnFailedNetworks() {
52 RTC_DCHECK(config_.regather_on_failed_networks_interval >= 0);
53 CancelScheduledRecurringRegatheringOnFailedNetworks();
54 has_recurring_schedule_on_failed_networks_ = true;
55 invoker_for_failed_networks_.AsyncInvokeDelayed<void>(
Steve Antonf4172382020-01-27 15:45:02 -080056 RTC_FROM_HERE, thread_,
Qingsi Wang1b368942018-06-13 13:54:08 -070057 rtc::Bind(
58 &BasicRegatheringController::RegatherOnFailedNetworksIfDoneGathering,
Steve Antonf4172382020-01-27 15:45:02 -080059 this),
Qingsi Wang1b368942018-06-13 13:54:08 -070060 config_.regather_on_failed_networks_interval);
61}
62
Steve Antonf4172382020-01-27 15:45:02 -080063void BasicRegatheringController::RegatherOnFailedNetworksIfDoneGathering() {
Qingsi Wang1b368942018-06-13 13:54:08 -070064 // Only regather when the current session is in the CLEARED state (i.e., not
65 // running or stopped). It is only possible to enter this state when we gather
66 // continually, so there is an implicit check on continual gathering here.
67 if (allocator_session_ && allocator_session_->IsCleared()) {
68 allocator_session_->RegatherOnFailedNetworks();
69 }
Steve Antonf4172382020-01-27 15:45:02 -080070 ScheduleRecurringRegatheringOnFailedNetworks();
Qingsi Wang1b368942018-06-13 13:54:08 -070071}
72
73void BasicRegatheringController::
74 CancelScheduledRecurringRegatheringOnFailedNetworks() {
75 invoker_for_failed_networks_.Clear();
76 has_recurring_schedule_on_failed_networks_ = false;
77}
78
Qingsi Wang1b368942018-06-13 13:54:08 -070079} // namespace webrtc