From 4b5d323d2fee20f9cf24e6385ac9589cbcc04150 Mon Sep 17 00:00:00 2001 From: Tomas Gunnarsson Date: Mon, 23 Nov 2020 09:32:59 +0100 Subject: [PATCH] Remove dependency on AsyncInvoker from BasicRegatheringController Bug: webrtc:11988 Change-Id: I5ddf80d02af3bc5879c5ff299b040729206f5dba Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/194338 Commit-Queue: Tommi Reviewed-by: Harald Alvestrand Reviewed-by: Jonas Oreland Cr-Commit-Position: refs/heads/master@{#32671} --- p2p/base/regathering_controller.cc | 61 +++++++++++++++--------------- p2p/base/regathering_controller.h | 18 ++++----- 2 files changed, 37 insertions(+), 42 deletions(-) diff --git a/p2p/base/regathering_controller.cc b/p2p/base/regathering_controller.cc index fe38a3e4d4..293e9dbcfd 100644 --- a/p2p/base/regathering_controller.cc +++ b/p2p/base/regathering_controller.cc @@ -9,6 +9,7 @@ */ #include "p2p/base/regathering_controller.h" +#include "rtc_base/task_utils/to_queued_task.h" namespace webrtc { @@ -17,8 +18,8 @@ BasicRegatheringController::BasicRegatheringController( cricket::IceTransportInternal* ice_transport, rtc::Thread* thread) : config_(config), ice_transport_(ice_transport), thread_(thread) { + RTC_DCHECK_RUN_ON(thread_); RTC_DCHECK(ice_transport_); - RTC_DCHECK(thread_); ice_transport_->SignalStateChanged.connect( this, &BasicRegatheringController::OnIceTransportStateChanged); ice_transport->SignalWritableState.connect( @@ -29,51 +30,49 @@ BasicRegatheringController::BasicRegatheringController( this, &BasicRegatheringController::OnIceTransportNetworkRouteChanged); } -BasicRegatheringController::~BasicRegatheringController() = default; +BasicRegatheringController::~BasicRegatheringController() { + RTC_DCHECK_RUN_ON(thread_); +} void BasicRegatheringController::Start() { + RTC_DCHECK_RUN_ON(thread_); ScheduleRecurringRegatheringOnFailedNetworks(); } void BasicRegatheringController::SetConfig(const Config& config) { - bool need_cancel_and_reschedule_on_failed_networks = - has_recurring_schedule_on_failed_networks_ && - (config_.regather_on_failed_networks_interval != - config.regather_on_failed_networks_interval); + RTC_DCHECK_RUN_ON(thread_); + bool need_reschedule_on_failed_networks = + pending_regathering_ && (config_.regather_on_failed_networks_interval != + config.regather_on_failed_networks_interval); config_ = config; - if (need_cancel_and_reschedule_on_failed_networks) { - CancelScheduledRecurringRegatheringOnFailedNetworks(); + if (need_reschedule_on_failed_networks) { ScheduleRecurringRegatheringOnFailedNetworks(); } } void BasicRegatheringController:: ScheduleRecurringRegatheringOnFailedNetworks() { + RTC_DCHECK_RUN_ON(thread_); RTC_DCHECK(config_.regather_on_failed_networks_interval >= 0); - CancelScheduledRecurringRegatheringOnFailedNetworks(); - has_recurring_schedule_on_failed_networks_ = true; - invoker_for_failed_networks_.AsyncInvokeDelayed( - RTC_FROM_HERE, thread_, - rtc::Bind( - &BasicRegatheringController::RegatherOnFailedNetworksIfDoneGathering, - this), + // Reset pending_regathering_ to cancel any potentially pending tasks. + pending_regathering_.reset(new ScopedTaskSafety()); + + thread_->PostDelayedTask( + ToQueuedTask(*pending_regathering_.get(), + [this]() { + RTC_DCHECK_RUN_ON(thread_); + // Only regather when the current session is in the CLEARED + // state (i.e., not running or stopped). It is only + // possible to enter this state when we gather continually, + // so there is an implicit check on continual gathering + // here. + if (allocator_session_ && + allocator_session_->IsCleared()) { + allocator_session_->RegatherOnFailedNetworks(); + } + ScheduleRecurringRegatheringOnFailedNetworks(); + }), config_.regather_on_failed_networks_interval); } -void BasicRegatheringController::RegatherOnFailedNetworksIfDoneGathering() { - // Only regather when the current session is in the CLEARED state (i.e., not - // running or stopped). It is only possible to enter this state when we gather - // continually, so there is an implicit check on continual gathering here. - if (allocator_session_ && allocator_session_->IsCleared()) { - allocator_session_->RegatherOnFailedNetworks(); - } - ScheduleRecurringRegatheringOnFailedNetworks(); -} - -void BasicRegatheringController:: - CancelScheduledRecurringRegatheringOnFailedNetworks() { - invoker_for_failed_networks_.Clear(); - has_recurring_schedule_on_failed_networks_ = false; -} - } // namespace webrtc diff --git a/p2p/base/regathering_controller.h b/p2p/base/regathering_controller.h index 54a76dc3e5..116d820a82 100644 --- a/p2p/base/regathering_controller.h +++ b/p2p/base/regathering_controller.h @@ -11,9 +11,11 @@ #ifndef P2P_BASE_REGATHERING_CONTROLLER_H_ #define P2P_BASE_REGATHERING_CONTROLLER_H_ +#include + #include "p2p/base/ice_transport_internal.h" #include "p2p/base/port_allocator.h" -#include "rtc_base/async_invoker.h" +#include "rtc_base/task_utils/pending_task_safety_flag.h" #include "rtc_base/thread.h" namespace webrtc { @@ -80,20 +82,14 @@ class BasicRegatheringController : public sigslot::has_slots<> { void ScheduleRecurringRegatheringOnFailedNetworks(); // Cancels regathering scheduled by ScheduleRecurringRegatheringOnAllNetworks. void CancelScheduledRecurringRegatheringOnAllNetworks(); - // Cancels regathering scheduled by - // ScheduleRecurringRegatheringOnFailedNetworks. - void CancelScheduledRecurringRegatheringOnFailedNetworks(); - - // The following method perform the actual regathering, if the recent port - // allocator session has done the initial gathering. - void RegatherOnFailedNetworksIfDoneGathering(); + // We use a flag to be able to cancel pending regathering operations when + // the object goes out of scope or the config changes. + std::unique_ptr pending_regathering_; Config config_; cricket::IceTransportInternal* ice_transport_; cricket::PortAllocatorSession* allocator_session_ = nullptr; - bool has_recurring_schedule_on_failed_networks_ = false; - rtc::Thread* thread_; - rtc::AsyncInvoker invoker_for_failed_networks_; + rtc::Thread* const thread_; }; } // namespace webrtc