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