Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "p2p/base/regathering_controller.h" |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 12 | |
| 13 | namespace webrtc { |
| 14 | |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 15 | BasicRegatheringController::BasicRegatheringController( |
| 16 | const Config& config, |
| 17 | cricket::IceTransportInternal* ice_transport, |
| 18 | rtc::Thread* thread) |
Steve Anton | f417238 | 2020-01-27 15:45:02 -0800 | [diff] [blame^] | 19 | : config_(config), ice_transport_(ice_transport), thread_(thread) { |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 20 | RTC_DCHECK(ice_transport_); |
| 21 | RTC_DCHECK(thread_); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 22 | ice_transport_->SignalStateChanged.connect( |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 23 | 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 | |
| 32 | BasicRegatheringController::~BasicRegatheringController() = default; |
| 33 | |
| 34 | void BasicRegatheringController::Start() { |
| 35 | ScheduleRecurringRegatheringOnFailedNetworks(); |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void BasicRegatheringController::SetConfig(const Config& config) { |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 39 | 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 Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 44 | if (need_cancel_and_reschedule_on_failed_networks) { |
| 45 | CancelScheduledRecurringRegatheringOnFailedNetworks(); |
| 46 | ScheduleRecurringRegatheringOnFailedNetworks(); |
| 47 | } |
| 48 | } |
| 49 | |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 50 | void 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 Anton | f417238 | 2020-01-27 15:45:02 -0800 | [diff] [blame^] | 56 | RTC_FROM_HERE, thread_, |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 57 | rtc::Bind( |
| 58 | &BasicRegatheringController::RegatherOnFailedNetworksIfDoneGathering, |
Steve Anton | f417238 | 2020-01-27 15:45:02 -0800 | [diff] [blame^] | 59 | this), |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 60 | config_.regather_on_failed_networks_interval); |
| 61 | } |
| 62 | |
Steve Anton | f417238 | 2020-01-27 15:45:02 -0800 | [diff] [blame^] | 63 | void BasicRegatheringController::RegatherOnFailedNetworksIfDoneGathering() { |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 64 | // 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 Anton | f417238 | 2020-01-27 15:45:02 -0800 | [diff] [blame^] | 70 | ScheduleRecurringRegatheringOnFailedNetworks(); |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void BasicRegatheringController:: |
| 74 | CancelScheduledRecurringRegatheringOnFailedNetworks() { |
| 75 | invoker_for_failed_networks_.Clear(); |
| 76 | has_recurring_schedule_on_failed_networks_ = false; |
| 77 | } |
| 78 | |
Qingsi Wang | 1b36894 | 2018-06-13 13:54:08 -0700 | [diff] [blame] | 79 | } // namespace webrtc |