regathering_controller.h 4.6 KB

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