连通性检测过程

image.png

P2PTransportChannel::SortConnectionsAndUpdateState

image.png

P2PTransportChannel::MaybeStartPinging

image.png

P2PTransportChannel::CheckAndPing

image.png

BasicIceController::SelectConnectionToPing

H:\webrtc-20210315\webrtc-20210315\webrtc\webrtc-checkout\src\p2p\base\basic_ice_controller.cc
image.png

BasicIceController::FindNextPingableConnection

查找下一个可以ping的连接。

  1. // Returns the next pingable connection to ping.
  2. const Connection* BasicIceController::FindNextPingableConnection() {
  3. int64_t now = rtc::TimeMillis();
  4. // Rule 1: Selected connection takes priority over non-selected ones.
  5. if (selected_connection_ && selected_connection_->connected() &&
  6. selected_connection_->writable() &&
  7. WritableConnectionPastPingInterval(selected_connection_, now)) {
  8. return selected_connection_;
  9. }
  10. // Rule 2: If the channel is weak, we need to find a new writable and
  11. // receiving connection, probably on a different network. If there are lots of
  12. // connections, it may take several seconds between two pings for every
  13. // non-selected connection. This will cause the receiving state of those
  14. // connections to be false, and thus they won't be selected. This is
  15. // problematic for network fail-over. We want to make sure at least one
  16. // connection per network is pinged frequently enough in order for it to be
  17. // selectable. So we prioritize one connection per network.
  18. // Rule 2.1: Among such connections, pick the one with the earliest
  19. // last-ping-sent time.
  20. if (weak()) {
  21. std::vector<const Connection*> pingable_selectable_connections;
  22. absl::c_copy_if(GetBestWritableConnectionPerNetwork(),
  23. std::back_inserter(pingable_selectable_connections),
  24. [this, now](const Connection* conn) {
  25. return WritableConnectionPastPingInterval(conn, now);
  26. });
  27. auto iter = absl::c_min_element(
  28. pingable_selectable_connections,
  29. [](const Connection* conn1, const Connection* conn2) {
  30. return conn1->last_ping_sent() < conn2->last_ping_sent();
  31. });
  32. if (iter != pingable_selectable_connections.end()) {
  33. return *iter;
  34. }
  35. }
  36. // Rule 3: Triggered checks have priority over non-triggered connections.
  37. // Rule 3.1: Among triggered checks, oldest takes precedence.
  38. const Connection* oldest_triggered_check =
  39. FindOldestConnectionNeedingTriggeredCheck(now);
  40. if (oldest_triggered_check) {
  41. return oldest_triggered_check;
  42. }
  43. // Rule 4: Unpinged connections have priority over pinged ones.
  44. RTC_CHECK(connections_.size() ==
  45. pinged_connections_.size() + unpinged_connections_.size());
  46. // If there are unpinged and pingable connections, only ping those.
  47. // Otherwise, treat everything as unpinged.
  48. // TODO(honghaiz): Instead of adding two separate vectors, we can add a state
  49. // "pinged" to filter out unpinged connections.
  50. if (absl::c_none_of(unpinged_connections_,
  51. [this, now](const Connection* conn) {
  52. return this->IsPingable(conn, now);
  53. })) {
  54. unpinged_connections_.insert(pinged_connections_.begin(),
  55. pinged_connections_.end());
  56. pinged_connections_.clear();
  57. }
  58. // Among un-pinged pingable connections, "more pingable" takes precedence.
  59. std::vector<const Connection*> pingable_connections;
  60. absl::c_copy_if(
  61. unpinged_connections_, std::back_inserter(pingable_connections),
  62. [this, now](const Connection* conn) { return IsPingable(conn, now); });
  63. auto iter = absl::c_max_element(
  64. pingable_connections,
  65. [this](const Connection* conn1, const Connection* conn2) {
  66. // Some implementations of max_element
  67. // compare an element with itself.
  68. if (conn1 == conn2) {
  69. return false;
  70. }
  71. return MorePingable(conn1, conn2) == conn2;
  72. });
  73. if (iter != pingable_connections.end()) {
  74. return *iter;
  75. }
  76. return nullptr;
  77. }

P2PTransportChannel::PingConnection

image.png
当前角色是控制方,才会去判断是否提名,和使用candidate属性。

Connection::Ping

image.png

SentPing结构

image.png
nomination为0,不提名;大于0,提名