You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "p2p/base/regathering_controller.h"
  11. namespace webrtc {
  12. using Config = BasicRegatheringController::Config;
  13. Config::Config(const absl::optional<rtc::IntervalRange>&
  14. regather_on_all_networks_interval_range,
  15. int regather_on_failed_networks_interval)
  16. : regather_on_all_networks_interval_range(
  17. regather_on_all_networks_interval_range),
  18. regather_on_failed_networks_interval(
  19. regather_on_failed_networks_interval) {}
  20. Config::Config(const Config& other) = default;
  21. Config::~Config() = default;
  22. Config& Config::operator=(const Config& other) = default;
  23. BasicRegatheringController::BasicRegatheringController(
  24. const Config& config,
  25. cricket::IceTransportInternal* ice_transport,
  26. rtc::Thread* thread)
  27. : config_(config),
  28. ice_transport_(ice_transport),
  29. thread_(thread),
  30. rand_(rtc::SystemTimeNanos()) {
  31. RTC_DCHECK(ice_transport_);
  32. RTC_DCHECK(thread_);
  33. ice_transport_->SignalStateChanged.connect(
  34. this, &BasicRegatheringController::OnIceTransportStateChanged);
  35. ice_transport->SignalWritableState.connect(
  36. this, &BasicRegatheringController::OnIceTransportWritableState);
  37. ice_transport->SignalReceivingState.connect(
  38. this, &BasicRegatheringController::OnIceTransportReceivingState);
  39. ice_transport->SignalNetworkRouteChanged.connect(
  40. this, &BasicRegatheringController::OnIceTransportNetworkRouteChanged);
  41. }
  42. BasicRegatheringController::~BasicRegatheringController() = default;
  43. void BasicRegatheringController::Start() {
  44. ScheduleRecurringRegatheringOnFailedNetworks();
  45. if (config_.regather_on_all_networks_interval_range) {
  46. ScheduleRecurringRegatheringOnAllNetworks();
  47. }
  48. }
  49. void BasicRegatheringController::SetConfig(const Config& config) {
  50. bool need_cancel_on_all_networks =
  51. has_recurring_schedule_on_all_networks_ &&
  52. (config_.regather_on_all_networks_interval_range !=
  53. config.regather_on_all_networks_interval_range);
  54. bool need_reschedule_on_all_networks =
  55. config.regather_on_all_networks_interval_range &&
  56. (config_.regather_on_all_networks_interval_range !=
  57. config.regather_on_all_networks_interval_range);
  58. bool need_cancel_and_reschedule_on_failed_networks =
  59. has_recurring_schedule_on_failed_networks_ &&
  60. (config_.regather_on_failed_networks_interval !=
  61. config.regather_on_failed_networks_interval);
  62. config_ = config;
  63. if (need_cancel_on_all_networks) {
  64. CancelScheduledRecurringRegatheringOnAllNetworks();
  65. }
  66. if (need_reschedule_on_all_networks) {
  67. ScheduleRecurringRegatheringOnAllNetworks();
  68. }
  69. if (need_cancel_and_reschedule_on_failed_networks) {
  70. CancelScheduledRecurringRegatheringOnFailedNetworks();
  71. ScheduleRecurringRegatheringOnFailedNetworks();
  72. }
  73. }
  74. void BasicRegatheringController::ScheduleRecurringRegatheringOnAllNetworks() {
  75. RTC_DCHECK(config_.regather_on_all_networks_interval_range &&
  76. config_.regather_on_all_networks_interval_range.value().min() >=
  77. 0);
  78. int delay_ms = SampleRegatherAllNetworksInterval(
  79. config_.regather_on_all_networks_interval_range.value());
  80. CancelScheduledRecurringRegatheringOnAllNetworks();
  81. has_recurring_schedule_on_all_networks_ = true;
  82. invoker_for_all_networks_.AsyncInvokeDelayed<void>(
  83. RTC_FROM_HERE, thread(),
  84. rtc::Bind(
  85. &BasicRegatheringController::RegatherOnAllNetworksIfDoneGathering,
  86. this, true),
  87. delay_ms);
  88. }
  89. void BasicRegatheringController::RegatherOnAllNetworksIfDoneGathering(
  90. bool repeated) {
  91. // Only regather when the current session is in the CLEARED state (i.e., not
  92. // running or stopped). It is only possible to enter this state when we gather
  93. // continually, so there is an implicit check on continual gathering here.
  94. if (allocator_session_ && allocator_session_->IsCleared()) {
  95. allocator_session_->RegatherOnAllNetworks();
  96. }
  97. if (repeated) {
  98. ScheduleRecurringRegatheringOnAllNetworks();
  99. }
  100. }
  101. void BasicRegatheringController::
  102. ScheduleRecurringRegatheringOnFailedNetworks() {
  103. RTC_DCHECK(config_.regather_on_failed_networks_interval >= 0);
  104. CancelScheduledRecurringRegatheringOnFailedNetworks();
  105. has_recurring_schedule_on_failed_networks_ = true;
  106. invoker_for_failed_networks_.AsyncInvokeDelayed<void>(
  107. RTC_FROM_HERE, thread(),
  108. rtc::Bind(
  109. &BasicRegatheringController::RegatherOnFailedNetworksIfDoneGathering,
  110. this, true),
  111. config_.regather_on_failed_networks_interval);
  112. }
  113. void BasicRegatheringController::RegatherOnFailedNetworksIfDoneGathering(
  114. bool repeated) {
  115. // Only regather when the current session is in the CLEARED state (i.e., not
  116. // running or stopped). It is only possible to enter this state when we gather
  117. // continually, so there is an implicit check on continual gathering here.
  118. if (allocator_session_ && allocator_session_->IsCleared()) {
  119. allocator_session_->RegatherOnFailedNetworks();
  120. }
  121. if (repeated) {
  122. ScheduleRecurringRegatheringOnFailedNetworks();
  123. }
  124. }
  125. void BasicRegatheringController::
  126. CancelScheduledRecurringRegatheringOnAllNetworks() {
  127. invoker_for_all_networks_.Clear();
  128. has_recurring_schedule_on_all_networks_ = false;
  129. }
  130. void BasicRegatheringController::
  131. CancelScheduledRecurringRegatheringOnFailedNetworks() {
  132. invoker_for_failed_networks_.Clear();
  133. has_recurring_schedule_on_failed_networks_ = false;
  134. }
  135. int BasicRegatheringController::SampleRegatherAllNetworksInterval(
  136. const rtc::IntervalRange& range) {
  137. return rand_.Rand(range.min(), range.max());
  138. }
  139. } // namespace webrtc