basic_ice_controller.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright 2019 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_BASIC_ICE_CONTROLLER_H_
  11. #define P2P_BASE_BASIC_ICE_CONTROLLER_H_
  12. #include <algorithm>
  13. #include <map>
  14. #include <set>
  15. #include <utility>
  16. #include <vector>
  17. #include "p2p/base/ice_controller_factory_interface.h"
  18. #include "p2p/base/ice_controller_interface.h"
  19. #include "p2p/base/p2p_transport_channel.h"
  20. namespace cricket {
  21. class BasicIceController : public IceControllerInterface {
  22. public:
  23. explicit BasicIceController(const IceControllerFactoryArgs& args);
  24. virtual ~BasicIceController();
  25. void SetIceConfig(const IceConfig& config) override;
  26. void SetSelectedConnection(const Connection* selected_connection) override;
  27. void AddConnection(const Connection* connection) override;
  28. void OnConnectionDestroyed(const Connection* connection) override;
  29. rtc::ArrayView<const Connection*> connections() const override {
  30. return rtc::ArrayView<const Connection*>(
  31. const_cast<const Connection**>(connections_.data()),
  32. connections_.size());
  33. }
  34. bool HasPingableConnection() const override;
  35. PingResult SelectConnectionToPing(int64_t last_ping_sent_ms) override;
  36. bool GetUseCandidateAttr(const Connection* conn,
  37. NominationMode mode,
  38. IceMode remote_ice_mode) const override;
  39. SwitchResult ShouldSwitchConnection(IceControllerEvent reason,
  40. const Connection* connection) override;
  41. SwitchResult SortAndSwitchConnection(IceControllerEvent reason) override;
  42. std::vector<const Connection*> PruneConnections() override;
  43. // These methods are only for tests.
  44. const Connection* FindNextPingableConnection() override;
  45. void MarkConnectionPinged(const Connection* conn) override;
  46. private:
  47. // A transport channel is weak if the current best connection is either
  48. // not receiving or not writable, or if there is no best connection at all.
  49. bool weak() const {
  50. return !selected_connection_ || selected_connection_->weak();
  51. }
  52. int weak_ping_interval() const {
  53. return std::max(config_.ice_check_interval_weak_connectivity_or_default(),
  54. config_.ice_check_min_interval_or_default());
  55. }
  56. int strong_ping_interval() const {
  57. return std::max(config_.ice_check_interval_strong_connectivity_or_default(),
  58. config_.ice_check_min_interval_or_default());
  59. }
  60. int check_receiving_interval() const {
  61. return std::max(MIN_CHECK_RECEIVING_INTERVAL,
  62. config_.receiving_timeout_or_default() / 10);
  63. }
  64. const Connection* FindOldestConnectionNeedingTriggeredCheck(int64_t now);
  65. // Between |conn1| and |conn2|, this function returns the one which should
  66. // be pinged first.
  67. const Connection* MorePingable(const Connection* conn1,
  68. const Connection* conn2);
  69. // Select the connection which is Relay/Relay. If both of them are,
  70. // UDP relay protocol takes precedence.
  71. const Connection* MostLikelyToWork(const Connection* conn1,
  72. const Connection* conn2);
  73. // Compare the last_ping_sent time and return the one least recently pinged.
  74. const Connection* LeastRecentlyPinged(const Connection* conn1,
  75. const Connection* conn2);
  76. bool IsPingable(const Connection* conn, int64_t now) const;
  77. bool IsBackupConnection(const Connection* conn) const;
  78. // Whether a writable connection is past its ping interval and needs to be
  79. // pinged again.
  80. bool WritableConnectionPastPingInterval(const Connection* conn,
  81. int64_t now) const;
  82. int CalculateActiveWritablePingInterval(const Connection* conn,
  83. int64_t now) const;
  84. std::map<const rtc::Network*, const Connection*> GetBestConnectionByNetwork()
  85. const;
  86. std::vector<const Connection*> GetBestWritableConnectionPerNetwork() const;
  87. bool ReadyToSend(const Connection* connection) const;
  88. bool PresumedWritable(const Connection* conn) const;
  89. int CompareCandidatePairNetworks(
  90. const Connection* a,
  91. const Connection* b,
  92. absl::optional<rtc::AdapterType> network_preference) const;
  93. // The methods below return a positive value if |a| is preferable to |b|,
  94. // a negative value if |b| is preferable, and 0 if they're equally preferable.
  95. // If |receiving_unchanged_threshold| is set, then when |b| is receiving and
  96. // |a| is not, returns a negative value only if |b| has been in receiving
  97. // state and |a| has been in not receiving state since
  98. // |receiving_unchanged_threshold| and sets
  99. // |missed_receiving_unchanged_threshold| to true otherwise.
  100. int CompareConnectionStates(
  101. const Connection* a,
  102. const Connection* b,
  103. absl::optional<int64_t> receiving_unchanged_threshold,
  104. bool* missed_receiving_unchanged_threshold) const;
  105. int CompareConnectionCandidates(const Connection* a,
  106. const Connection* b) const;
  107. // Compares two connections based on the connection states
  108. // (writable/receiving/connected), nomination states, last data received time,
  109. // and static preferences. Does not include latency. Used by both sorting
  110. // and ShouldSwitchSelectedConnection().
  111. // Returns a positive value if |a| is better than |b|.
  112. int CompareConnections(const Connection* a,
  113. const Connection* b,
  114. absl::optional<int64_t> receiving_unchanged_threshold,
  115. bool* missed_receiving_unchanged_threshold) const;
  116. SwitchResult HandleInitialSelectDampening(IceControllerEvent reason,
  117. const Connection* new_connection);
  118. std::function<IceTransportState()> ice_transport_state_func_;
  119. std::function<IceRole()> ice_role_func_;
  120. std::function<bool(const Connection*)> is_connection_pruned_func_;
  121. IceConfig config_;
  122. const IceFieldTrials* field_trials_;
  123. // |connections_| is a sorted list with the first one always be the
  124. // |selected_connection_| when it's not nullptr. The combination of
  125. // |pinged_connections_| and |unpinged_connections_| has the same
  126. // connections as |connections_|. These 2 sets maintain whether a
  127. // connection should be pinged next or not.
  128. const Connection* selected_connection_ = nullptr;
  129. std::vector<const Connection*> connections_;
  130. std::set<const Connection*> pinged_connections_;
  131. std::set<const Connection*> unpinged_connections_;
  132. // Timestamp for when we got the first selectable connection.
  133. int64_t initial_select_timestamp_ms_ = 0;
  134. };
  135. } // namespace cricket
  136. #endif // P2P_BASE_BASIC_ICE_CONTROLLER_H_