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 
11 #include "p2p/base/regathering_controller.h"
12 
13 namespace webrtc {
14 
BasicRegatheringController(const Config & config,cricket::IceTransportInternal * ice_transport,rtc::Thread * thread)15 BasicRegatheringController::BasicRegatheringController(
16     const Config& config,
17     cricket::IceTransportInternal* ice_transport,
18     rtc::Thread* thread)
19     : config_(config), ice_transport_(ice_transport), thread_(thread) {
20   RTC_DCHECK(ice_transport_);
21   RTC_DCHECK(thread_);
22   ice_transport_->SignalStateChanged.connect(
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 
Start()34 void BasicRegatheringController::Start() {
35   ScheduleRecurringRegatheringOnFailedNetworks();
36 }
37 
SetConfig(const Config & config)38 void BasicRegatheringController::SetConfig(const Config& config) {
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;
44   if (need_cancel_and_reschedule_on_failed_networks) {
45     CancelScheduledRecurringRegatheringOnFailedNetworks();
46     ScheduleRecurringRegatheringOnFailedNetworks();
47   }
48 }
49 
50 void BasicRegatheringController::
ScheduleRecurringRegatheringOnFailedNetworks()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>(
56       RTC_FROM_HERE, thread_,
57       rtc::Bind(
58           &BasicRegatheringController::RegatherOnFailedNetworksIfDoneGathering,
59           this),
60       config_.regather_on_failed_networks_interval);
61 }
62 
RegatherOnFailedNetworksIfDoneGathering()63 void BasicRegatheringController::RegatherOnFailedNetworksIfDoneGathering() {
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   }
70   ScheduleRecurringRegatheringOnFailedNetworks();
71 }
72 
73 void BasicRegatheringController::
CancelScheduledRecurringRegatheringOnFailedNetworks()74     CancelScheduledRecurringRegatheringOnFailedNetworks() {
75   invoker_for_failed_networks_.Clear();
76   has_recurring_schedule_on_failed_networks_ = false;
77 }
78 
79 }  // namespace webrtc
80