1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker * Copyright 2018 The WebRTC Project Authors. All rights reserved.
3*d9f75844SAndroid Build Coastguard Worker *
4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker */
10*d9f75844SAndroid Build Coastguard Worker
11*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/regathering_controller.h"
12*d9f75844SAndroid Build Coastguard Worker
13*d9f75844SAndroid Build Coastguard Worker #include "api/task_queue/pending_task_safety_flag.h"
14*d9f75844SAndroid Build Coastguard Worker #include "api/units/time_delta.h"
15*d9f75844SAndroid Build Coastguard Worker
16*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
17*d9f75844SAndroid Build Coastguard Worker
BasicRegatheringController(const Config & config,cricket::IceTransportInternal * ice_transport,rtc::Thread * thread)18*d9f75844SAndroid Build Coastguard Worker BasicRegatheringController::BasicRegatheringController(
19*d9f75844SAndroid Build Coastguard Worker const Config& config,
20*d9f75844SAndroid Build Coastguard Worker cricket::IceTransportInternal* ice_transport,
21*d9f75844SAndroid Build Coastguard Worker rtc::Thread* thread)
22*d9f75844SAndroid Build Coastguard Worker : config_(config), ice_transport_(ice_transport), thread_(thread) {
23*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(thread_);
24*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(thread_);
25*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(ice_transport_);
26*d9f75844SAndroid Build Coastguard Worker ice_transport_->SignalStateChanged.connect(
27*d9f75844SAndroid Build Coastguard Worker this, &BasicRegatheringController::OnIceTransportStateChanged);
28*d9f75844SAndroid Build Coastguard Worker ice_transport->SignalWritableState.connect(
29*d9f75844SAndroid Build Coastguard Worker this, &BasicRegatheringController::OnIceTransportWritableState);
30*d9f75844SAndroid Build Coastguard Worker ice_transport->SignalReceivingState.connect(
31*d9f75844SAndroid Build Coastguard Worker this, &BasicRegatheringController::OnIceTransportReceivingState);
32*d9f75844SAndroid Build Coastguard Worker ice_transport->SignalNetworkRouteChanged.connect(
33*d9f75844SAndroid Build Coastguard Worker this, &BasicRegatheringController::OnIceTransportNetworkRouteChanged);
34*d9f75844SAndroid Build Coastguard Worker }
35*d9f75844SAndroid Build Coastguard Worker
~BasicRegatheringController()36*d9f75844SAndroid Build Coastguard Worker BasicRegatheringController::~BasicRegatheringController() {
37*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(thread_);
38*d9f75844SAndroid Build Coastguard Worker }
39*d9f75844SAndroid Build Coastguard Worker
Start()40*d9f75844SAndroid Build Coastguard Worker void BasicRegatheringController::Start() {
41*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(thread_);
42*d9f75844SAndroid Build Coastguard Worker ScheduleRecurringRegatheringOnFailedNetworks();
43*d9f75844SAndroid Build Coastguard Worker }
44*d9f75844SAndroid Build Coastguard Worker
SetConfig(const Config & config)45*d9f75844SAndroid Build Coastguard Worker void BasicRegatheringController::SetConfig(const Config& config) {
46*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(thread_);
47*d9f75844SAndroid Build Coastguard Worker bool need_reschedule_on_failed_networks =
48*d9f75844SAndroid Build Coastguard Worker pending_regathering_ && (config_.regather_on_failed_networks_interval !=
49*d9f75844SAndroid Build Coastguard Worker config.regather_on_failed_networks_interval);
50*d9f75844SAndroid Build Coastguard Worker config_ = config;
51*d9f75844SAndroid Build Coastguard Worker if (need_reschedule_on_failed_networks) {
52*d9f75844SAndroid Build Coastguard Worker ScheduleRecurringRegatheringOnFailedNetworks();
53*d9f75844SAndroid Build Coastguard Worker }
54*d9f75844SAndroid Build Coastguard Worker }
55*d9f75844SAndroid Build Coastguard Worker
56*d9f75844SAndroid Build Coastguard Worker void BasicRegatheringController::
ScheduleRecurringRegatheringOnFailedNetworks()57*d9f75844SAndroid Build Coastguard Worker ScheduleRecurringRegatheringOnFailedNetworks() {
58*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(thread_);
59*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(config_.regather_on_failed_networks_interval >= 0);
60*d9f75844SAndroid Build Coastguard Worker // Reset pending_regathering_ to cancel any potentially pending tasks.
61*d9f75844SAndroid Build Coastguard Worker pending_regathering_.reset(new ScopedTaskSafety());
62*d9f75844SAndroid Build Coastguard Worker
63*d9f75844SAndroid Build Coastguard Worker thread_->PostDelayedTask(
64*d9f75844SAndroid Build Coastguard Worker SafeTask(pending_regathering_->flag(),
65*d9f75844SAndroid Build Coastguard Worker [this]() {
66*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(thread_);
67*d9f75844SAndroid Build Coastguard Worker // Only regather when the current session is in the CLEARED
68*d9f75844SAndroid Build Coastguard Worker // state (i.e., not running or stopped). It is only
69*d9f75844SAndroid Build Coastguard Worker // possible to enter this state when we gather continually,
70*d9f75844SAndroid Build Coastguard Worker // so there is an implicit check on continual gathering
71*d9f75844SAndroid Build Coastguard Worker // here.
72*d9f75844SAndroid Build Coastguard Worker if (allocator_session_ && allocator_session_->IsCleared()) {
73*d9f75844SAndroid Build Coastguard Worker allocator_session_->RegatherOnFailedNetworks();
74*d9f75844SAndroid Build Coastguard Worker }
75*d9f75844SAndroid Build Coastguard Worker ScheduleRecurringRegatheringOnFailedNetworks();
76*d9f75844SAndroid Build Coastguard Worker }),
77*d9f75844SAndroid Build Coastguard Worker TimeDelta::Millis(config_.regather_on_failed_networks_interval));
78*d9f75844SAndroid Build Coastguard Worker }
79*d9f75844SAndroid Build Coastguard Worker
80*d9f75844SAndroid Build Coastguard Worker } // namespace webrtc
81