| 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 | #include "rtc_base/task_utils/to_queued_task.h" |
| 13 |
| 14 | namespace webrtc { |
| 15 |
| 16 | BasicRegatheringController::BasicRegatheringController( |
| 17 | const Config& config, |
| 18 | cricket::IceTransportInternal* ice_transport, |
| 19 | rtc::Thread* thread) |
| 20 | : config_(config), ice_transport_(ice_transport), thread_(thread) { |
| 21 | RTC_DCHECK_RUN_ON(thread_); |
| 22 | RTC_DCHECK(ice_transport_); |
| 23 | ice_transport_->SignalStateChanged.connect( |
| 24 | this, &BasicRegatheringController::OnIceTransportStateChanged); |
| 25 | ice_transport->SignalWritableState.connect( |
| 26 | this, &BasicRegatheringController::OnIceTransportWritableState); |
| 27 | ice_transport->SignalReceivingState.connect( |
| 28 | this, &BasicRegatheringController::OnIceTransportReceivingState); |
| 29 | ice_transport->SignalNetworkRouteChanged.connect( |
| 30 | this, &BasicRegatheringController::OnIceTransportNetworkRouteChanged); |
| 31 | } |
| 32 |
| 33 | BasicRegatheringController::~BasicRegatheringController() { |
| 34 | RTC_DCHECK_RUN_ON(thread_); |
| 35 | } |
| 36 |
| 37 | void BasicRegatheringController::Start() { |
| 38 | RTC_DCHECK_RUN_ON(thread_); |
| 39 | ScheduleRecurringRegatheringOnFailedNetworks(); |
| 40 | } |
| 41 |
| 42 | void BasicRegatheringController::SetConfig(const Config& config) { |
| 43 | RTC_DCHECK_RUN_ON(thread_); |
| 44 | bool need_reschedule_on_failed_networks = |
| 45 | pending_regathering_ && (config_.regather_on_failed_networks_interval != |
| 46 | config.regather_on_failed_networks_interval); |
| 47 | config_ = config; |
| 48 | if (need_reschedule_on_failed_networks) { |
| 49 | ScheduleRecurringRegatheringOnFailedNetworks(); |
| 50 | } |
| 51 | } |
| 52 |
| 53 | void BasicRegatheringController:: |
| 54 | ScheduleRecurringRegatheringOnFailedNetworks() { |
| 55 | RTC_DCHECK_RUN_ON(thread_); |
| 56 | RTC_DCHECK(config_.regather_on_failed_networks_interval >= 0); |
| 57 | // Reset pending_regathering_ to cancel any potentially pending tasks. |
| 58 | pending_regathering_.reset(new ScopedTaskSafety()); |
| 59 |
| 60 | thread_->PostDelayedTask( |
| 61 | ToQueuedTask(*pending_regathering_.get(), |
| 62 | [this]() { |
| 63 | RTC_DCHECK_RUN_ON(thread_); |
| 64 | // Only regather when the current session is in the CLEARED |
| 65 | // state (i.e., not running or stopped). It is only |
| 66 | // possible to enter this state when we gather continually, |
| 67 | // so there is an implicit check on continual gathering |
| 68 | // here. |
| 69 | if (allocator_session_ && |
| 70 | allocator_session_->IsCleared()) { |
| 71 | allocator_session_->RegatherOnFailedNetworks(); |
| 72 | } |
| 73 | ScheduleRecurringRegatheringOnFailedNetworks(); |
| 74 | }), |
| 75 | config_.regather_on_failed_networks_interval); |
| 76 | } |
| 77 |
| 78 | } // namespace webrtc |
| 79 |