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.

124 lines
5.8 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. #ifndef P2P_BASE_REGATHERING_CONTROLLER_H_
  11. #define P2P_BASE_REGATHERING_CONTROLLER_H_
  12. #include "p2p/base/ice_transport_internal.h"
  13. #include "p2p/base/port_allocator.h"
  14. #include "rtc_base/async_invoker.h"
  15. #include "rtc_base/random.h"
  16. #include "rtc_base/thread.h"
  17. namespace webrtc {
  18. // Controls regathering of candidates for the ICE transport passed into it,
  19. // reacting to signals like SignalWritableState, SignalNetworkRouteChange, etc.,
  20. // using methods like GetStats to get additional information, and calling
  21. // methods like RegatherOnAllNetworks on the PortAllocatorSession when
  22. // regathering is desired.
  23. //
  24. // TODO(qingsi): Add the description of behavior when autonomous regathering is
  25. // implemented.
  26. //
  27. // "Regathering" is defined as gathering additional candidates within a single
  28. // ICE generation (or in other words, PortAllocatorSession), and is possible
  29. // when "continual gathering" is enabled. This may allow connectivity to be
  30. // maintained and/or restored without a full ICE restart.
  31. //
  32. // Regathering will only begin after PortAllocationSession is set via
  33. // set_allocator_session. This should be called any time the "active"
  34. // PortAllocatorSession is changed (in other words, when an ICE restart occurs),
  35. // so that candidates are gathered for the "current" ICE generation.
  36. //
  37. // All methods of BasicRegatheringController should be called on the same
  38. // thread as the one passed to the constructor, and this thread should be the
  39. // same one where PortAllocatorSession runs, which is also identical to the
  40. // network thread of the ICE transport, as given by
  41. // P2PTransportChannel::thread().
  42. class BasicRegatheringController : public sigslot::has_slots<> {
  43. public:
  44. struct Config {
  45. Config(const absl::optional<rtc::IntervalRange>&
  46. regather_on_all_networks_interval_range,
  47. int regather_on_failed_networks_interval);
  48. Config(const Config& other);
  49. ~Config();
  50. Config& operator=(const Config& other);
  51. absl::optional<rtc::IntervalRange> regather_on_all_networks_interval_range;
  52. int regather_on_failed_networks_interval;
  53. };
  54. BasicRegatheringController() = delete;
  55. BasicRegatheringController(const Config& config,
  56. cricket::IceTransportInternal* ice_transport,
  57. rtc::Thread* thread);
  58. ~BasicRegatheringController() override;
  59. // TODO(qingsi): Remove this method after implementing a new signal in
  60. // P2PTransportChannel and reacting to that signal for the initial schedules
  61. // of regathering.
  62. void Start();
  63. void set_allocator_session(cricket::PortAllocatorSession* allocator_session) {
  64. allocator_session_ = allocator_session;
  65. }
  66. // Setting a different config of the regathering interval range on all
  67. // networks cancels and reschedules the recurring schedules, if any, of
  68. // regathering on all networks. The same applies to the change of the
  69. // regathering interval on the failed networks. This rescheduling behavior is
  70. // seperately defined for the two config parameters.
  71. void SetConfig(const Config& config);
  72. private:
  73. // TODO(qingsi): Implement the following methods and use methods from the ICE
  74. // transport like GetStats to get additional information for the decision
  75. // making in regathering.
  76. void OnIceTransportStateChanged(cricket::IceTransportInternal*) {}
  77. void OnIceTransportWritableState(rtc::PacketTransportInternal*) {}
  78. void OnIceTransportReceivingState(rtc::PacketTransportInternal*) {}
  79. void OnIceTransportNetworkRouteChanged(absl::optional<rtc::NetworkRoute>) {}
  80. // Schedules delayed and repeated regathering of local candidates on all
  81. // networks, where the delay in milliseconds is randomly sampled from the
  82. // range in the config. The delay of each repetition is independently sampled
  83. // from the same range. When scheduled, all previous schedules are canceled.
  84. void ScheduleRecurringRegatheringOnAllNetworks();
  85. // Schedules delayed and repeated regathering of local candidates on failed
  86. // networks, where the delay in milliseconds is given by the config. Each
  87. // repetition is separated by the same delay. When scheduled, all previous
  88. // schedules are canceled.
  89. void ScheduleRecurringRegatheringOnFailedNetworks();
  90. // Cancels regathering scheduled by ScheduleRecurringRegatheringOnAllNetworks.
  91. void CancelScheduledRecurringRegatheringOnAllNetworks();
  92. // Cancels regathering scheduled by
  93. // ScheduleRecurringRegatheringOnFailedNetworks.
  94. void CancelScheduledRecurringRegatheringOnFailedNetworks();
  95. rtc::Thread* thread() const { return thread_; }
  96. // The following two methods perform the actual regathering, if the recent
  97. // port allocator session has done the initial gathering.
  98. void RegatherOnAllNetworksIfDoneGathering(bool repeated);
  99. void RegatherOnFailedNetworksIfDoneGathering(bool repeated);
  100. // Samples a delay from the uniform distribution in the given range.
  101. int SampleRegatherAllNetworksInterval(const rtc::IntervalRange& range);
  102. Config config_;
  103. cricket::IceTransportInternal* ice_transport_;
  104. cricket::PortAllocatorSession* allocator_session_ = nullptr;
  105. bool has_recurring_schedule_on_all_networks_ = false;
  106. bool has_recurring_schedule_on_failed_networks_ = false;
  107. rtc::Thread* thread_;
  108. rtc::AsyncInvoker invoker_for_all_networks_;
  109. rtc::AsyncInvoker invoker_for_failed_networks_;
  110. // Used to generate random intervals for regather_all_networks_interval_range.
  111. Random rand_;
  112. };
  113. } // namespace webrtc
  114. #endif // P2P_BASE_REGATHERING_CONTROLLER_H_