Qingsi Wang | 1b36894 | 2018-06-13 20:54:08 | [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 17:11:00 | [diff] [blame] | 11 | #include "p2p/base/regathering_controller.h" |
Artem Titov | c374d11 | 2022-06-16 19:27:45 | [diff] [blame] | 12 | |
Danil Chapovalov | 7b19036 | 2022-07-07 12:13:02 | [diff] [blame] | 13 | #include "api/task_queue/pending_task_safety_flag.h" |
| 14 | #include "api/units/time_delta.h" |
Qingsi Wang | 1b36894 | 2018-06-13 20:54:08 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | |
Qingsi Wang | 1b36894 | 2018-06-13 20:54:08 | [diff] [blame] | 18 | BasicRegatheringController::BasicRegatheringController( |
| 19 | const Config& config, |
| 20 | cricket::IceTransportInternal* ice_transport, |
| 21 | rtc::Thread* thread) |
Steve Anton | f417238 | 2020-01-27 23:45:02 | [diff] [blame] | 22 | : config_(config), ice_transport_(ice_transport), thread_(thread) { |
Niels Möller | 83830f3 | 2022-05-20 07:12:57 | [diff] [blame] | 23 | RTC_DCHECK(thread_); |
Tomas Gunnarsson | 4b5d323 | 2020-11-23 08:32:59 | [diff] [blame] | 24 | RTC_DCHECK_RUN_ON(thread_); |
Qingsi Wang | 1b36894 | 2018-06-13 20:54:08 | [diff] [blame] | 25 | RTC_DCHECK(ice_transport_); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 26 | ice_transport_->SignalStateChanged.connect( |
Qingsi Wang | 1b36894 | 2018-06-13 20:54:08 | [diff] [blame] | 27 | this, &BasicRegatheringController::OnIceTransportStateChanged); |
| 28 | ice_transport->SignalWritableState.connect( |
| 29 | this, &BasicRegatheringController::OnIceTransportWritableState); |
| 30 | ice_transport->SignalReceivingState.connect( |
| 31 | this, &BasicRegatheringController::OnIceTransportReceivingState); |
| 32 | ice_transport->SignalNetworkRouteChanged.connect( |
| 33 | this, &BasicRegatheringController::OnIceTransportNetworkRouteChanged); |
| 34 | } |
| 35 | |
Tomas Gunnarsson | 4b5d323 | 2020-11-23 08:32:59 | [diff] [blame] | 36 | BasicRegatheringController::~BasicRegatheringController() { |
| 37 | RTC_DCHECK_RUN_ON(thread_); |
| 38 | } |
Qingsi Wang | 1b36894 | 2018-06-13 20:54:08 | [diff] [blame] | 39 | |
| 40 | void BasicRegatheringController::Start() { |
Tomas Gunnarsson | 4b5d323 | 2020-11-23 08:32:59 | [diff] [blame] | 41 | RTC_DCHECK_RUN_ON(thread_); |
Qingsi Wang | 1b36894 | 2018-06-13 20:54:08 | [diff] [blame] | 42 | ScheduleRecurringRegatheringOnFailedNetworks(); |
Qingsi Wang | 1b36894 | 2018-06-13 20:54:08 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void BasicRegatheringController::SetConfig(const Config& config) { |
Tomas Gunnarsson | 4b5d323 | 2020-11-23 08:32:59 | [diff] [blame] | 46 | RTC_DCHECK_RUN_ON(thread_); |
| 47 | bool need_reschedule_on_failed_networks = |
| 48 | pending_regathering_ && (config_.regather_on_failed_networks_interval != |
| 49 | config.regather_on_failed_networks_interval); |
Qingsi Wang | 1b36894 | 2018-06-13 20:54:08 | [diff] [blame] | 50 | config_ = config; |
Tomas Gunnarsson | 4b5d323 | 2020-11-23 08:32:59 | [diff] [blame] | 51 | if (need_reschedule_on_failed_networks) { |
Qingsi Wang | 1b36894 | 2018-06-13 20:54:08 | [diff] [blame] | 52 | ScheduleRecurringRegatheringOnFailedNetworks(); |
| 53 | } |
| 54 | } |
| 55 | |
Qingsi Wang | 1b36894 | 2018-06-13 20:54:08 | [diff] [blame] | 56 | void BasicRegatheringController:: |
| 57 | ScheduleRecurringRegatheringOnFailedNetworks() { |
Tomas Gunnarsson | 4b5d323 | 2020-11-23 08:32:59 | [diff] [blame] | 58 | RTC_DCHECK_RUN_ON(thread_); |
Qingsi Wang | 1b36894 | 2018-06-13 20:54:08 | [diff] [blame] | 59 | RTC_DCHECK(config_.regather_on_failed_networks_interval >= 0); |
Tomas Gunnarsson | 4b5d323 | 2020-11-23 08:32:59 | [diff] [blame] | 60 | // Reset pending_regathering_ to cancel any potentially pending tasks. |
| 61 | pending_regathering_.reset(new ScopedTaskSafety()); |
| 62 | |
| 63 | thread_->PostDelayedTask( |
Danil Chapovalov | 7b19036 | 2022-07-07 12:13:02 | [diff] [blame] | 64 | SafeTask(pending_regathering_->flag(), |
| 65 | [this]() { |
| 66 | RTC_DCHECK_RUN_ON(thread_); |
| 67 | // Only regather when the current session is in the CLEARED |
| 68 | // state (i.e., not running or stopped). It is only |
| 69 | // possible to enter this state when we gather continually, |
| 70 | // so there is an implicit check on continual gathering |
| 71 | // here. |
| 72 | if (allocator_session_ && allocator_session_->IsCleared()) { |
| 73 | allocator_session_->RegatherOnFailedNetworks(); |
| 74 | } |
| 75 | ScheduleRecurringRegatheringOnFailedNetworks(); |
| 76 | }), |
| 77 | TimeDelta::Millis(config_.regather_on_failed_networks_interval)); |
Qingsi Wang | 1b36894 | 2018-06-13 20:54:08 | [diff] [blame] | 78 | } |
| 79 | |
Qingsi Wang | 1b36894 | 2018-06-13 20:54:08 | [diff] [blame] | 80 | } // namespace webrtc |