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 <tommi@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Jonas Oreland <jonaso@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#32671}
This commit is contained in:
Tomas Gunnarsson 2020-11-23 09:32:59 +01:00 committed by Commit Bot
parent af6ea0c3ab
commit 4b5d323d2f
2 changed files with 37 additions and 42 deletions

View File

@ -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<void>(
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

View File

@ -11,9 +11,11 @@
#ifndef P2P_BASE_REGATHERING_CONTROLLER_H_
#define P2P_BASE_REGATHERING_CONTROLLER_H_
#include <memory>
#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<ScopedTaskSafety> 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