OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 14 matching lines...) Expand all Loading... | |
25 #include "webrtc/system_wrappers/include/field_trial.h" | 25 #include "webrtc/system_wrappers/include/field_trial.h" |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 // messages for queuing up work for ourselves | 29 // messages for queuing up work for ourselves |
30 enum { MSG_SORT = 1, MSG_CHECK_AND_PING }; | 30 enum { MSG_SORT = 1, MSG_CHECK_AND_PING }; |
31 | 31 |
32 // The minimum improvement in RTT that justifies a switch. | 32 // The minimum improvement in RTT that justifies a switch. |
33 static const double kMinImprovement = 10; | 33 static const double kMinImprovement = 10; |
34 | 34 |
35 bool IsRelayRelay(cricket::Connection* conn) { | 35 bool IsRelayRelay(const cricket::Connection* conn) { |
36 return conn->local_candidate().type() == cricket::RELAY_PORT_TYPE && | 36 return conn->local_candidate().type() == cricket::RELAY_PORT_TYPE && |
37 conn->remote_candidate().type() == cricket::RELAY_PORT_TYPE; | 37 conn->remote_candidate().type() == cricket::RELAY_PORT_TYPE; |
38 } | 38 } |
39 | 39 |
40 bool IsUdp(cricket::Connection* conn) { | 40 bool IsUdp(cricket::Connection* conn) { |
41 return conn->local_candidate().relay_protocol() == cricket::UDP_PROTOCOL_NAME; | 41 return conn->local_candidate().relay_protocol() == cricket::UDP_PROTOCOL_NAME; |
42 } | 42 } |
43 | 43 |
44 cricket::PortInterface::CandidateOrigin GetOrigin(cricket::PortInterface* port, | 44 cricket::PortInterface::CandidateOrigin GetOrigin(cricket::PortInterface* port, |
45 cricket::PortInterface* origin_port) { | 45 cricket::PortInterface* origin_port) { |
46 if (!origin_port) | 46 if (!origin_port) |
47 return cricket::PortInterface::ORIGIN_MESSAGE; | 47 return cricket::PortInterface::ORIGIN_MESSAGE; |
48 else if (port == origin_port) | 48 else if (port == origin_port) |
49 return cricket::PortInterface::ORIGIN_THIS_PORT; | 49 return cricket::PortInterface::ORIGIN_THIS_PORT; |
50 else | 50 else |
51 return cricket::PortInterface::ORIGIN_OTHER_PORT; | 51 return cricket::PortInterface::ORIGIN_OTHER_PORT; |
52 } | 52 } |
53 | 53 |
54 // Compares two connections based only on the candidate and network information. | |
55 // Returns positive if |a| is better than |b|. | |
56 int CompareConnectionCandidates(cricket::Connection* a, | |
57 cricket::Connection* b) { | |
58 uint32_t a_cost = a->ComputeNetworkCost(); | |
59 uint32_t b_cost = b->ComputeNetworkCost(); | |
60 // Smaller cost is better. | |
61 if (a_cost < b_cost) { | |
62 return 1; | |
63 } | |
64 if (a_cost > b_cost) { | |
65 return -1; | |
66 } | |
67 | |
68 // Compare connection priority. Lower values get sorted last. | |
69 if (a->priority() > b->priority()) | |
70 return 1; | |
71 if (a->priority() < b->priority()) | |
72 return -1; | |
73 | |
74 // If we're still tied at this point, prefer a younger generation. | |
75 return (a->remote_candidate().generation() + a->port()->generation()) - | |
76 (b->remote_candidate().generation() + b->port()->generation()); | |
77 } | |
78 | |
79 // Compare two connections based on their writing, receiving, and connected | |
80 // states. | |
81 int CompareConnectionStates(cricket::Connection* a, cricket::Connection* b) { | |
82 // Sort based on write-state. Better states have lower values. | |
83 if (a->write_state() < b->write_state()) | |
84 return 1; | |
85 if (a->write_state() > b->write_state()) | |
86 return -1; | |
87 | |
88 // We prefer a receiving connection to a non-receiving, higher-priority | |
89 // connection when sorting connections and choosing which connection to | |
90 // switch to. | |
91 if (a->receiving() && !b->receiving()) | |
92 return 1; | |
93 if (!a->receiving() && b->receiving()) | |
94 return -1; | |
95 | |
96 // WARNING: Some complexity here about TCP reconnecting. | |
97 // When a TCP connection fails because of a TCP socket disconnecting, the | |
98 // active side of the connection will attempt to reconnect for 5 seconds while | |
99 // pretending to be writable (the connection is not set to the unwritable | |
100 // state). On the passive side, the connection also remains writable even | |
101 // though it is disconnected, and a new connection is created when the active | |
102 // side connects. At that point, there are two TCP connections on the passive | |
103 // side: 1. the old, disconnected one that is pretending to be writable, and | |
104 // 2. the new, connected one that is maybe not yet writable. For purposes of | |
105 // pruning, pinging, and selecting the best connection, we want to treat the | |
106 // new connection as "better" than the old one. We could add a method called | |
107 // something like Connection::ImReallyBadEvenThoughImWritable, but that is | |
108 // equivalent to the existing Connection::connected(), which we already have. | |
109 // So, in code throughout this file, we'll check whether the connection is | |
110 // connected() or not, and if it is not, treat it as "worse" than a connected | |
111 // one, even though it's writable. In the code below, we're doing so to make | |
112 // sure we treat a new writable connection as better than an old disconnected | |
113 // connection. | |
114 | |
115 // In the case where we reconnect TCP connections, the original best | |
116 // connection is disconnected without changing to WRITE_TIMEOUT. In this case, | |
117 // the new connection, when it becomes writable, should have higher priority. | |
118 if (a->write_state() == cricket::Connection::STATE_WRITABLE && | |
119 b->write_state() == cricket::Connection::STATE_WRITABLE) { | |
120 if (a->connected() && !b->connected()) { | |
121 return 1; | |
122 } | |
123 if (!a->connected() && b->connected()) { | |
124 return -1; | |
125 } | |
126 } | |
127 return 0; | |
128 } | |
129 | |
130 int CompareConnections(cricket::Connection* a, cricket::Connection* b) { | |
131 int state_cmp = CompareConnectionStates(a, b); | |
132 if (state_cmp != 0) { | |
133 return state_cmp; | |
134 } | |
135 // Compare the candidate information. | |
136 return CompareConnectionCandidates(a, b); | |
137 } | |
138 | |
139 // Wraps the comparison connection into a less than operator that puts higher | |
140 // priority writable connections first. | |
141 class ConnectionCompare { | |
142 public: | |
143 bool operator()(const cricket::Connection *ca, | |
144 const cricket::Connection *cb) { | |
145 cricket::Connection* a = const_cast<cricket::Connection*>(ca); | |
146 cricket::Connection* b = const_cast<cricket::Connection*>(cb); | |
147 | |
148 // Compare first on writability and static preferences. | |
149 int cmp = CompareConnections(a, b); | |
150 if (cmp > 0) | |
151 return true; | |
152 if (cmp < 0) | |
153 return false; | |
154 | |
155 // Otherwise, sort based on latency estimate. | |
156 return a->rtt() < b->rtt(); | |
157 | |
158 // Should we bother checking for the last connection that last received | |
159 // data? It would help rendezvous on the connection that is also receiving | |
160 // packets. | |
161 // | |
162 // TODO: Yes we should definitely do this. The TCP protocol gains | |
163 // efficiency by being used bidirectionally, as opposed to two separate | |
164 // unidirectional streams. This test should probably occur before | |
165 // comparison of local prefs (assuming combined prefs are the same). We | |
166 // need to be careful though, not to bounce back and forth with both sides | |
167 // trying to rendevous with the other. | |
168 } | |
169 }; | |
170 | |
171 // Determines whether we should switch between two connections, based first on | |
172 // connection states, static preferences, and then (if those are equal) on | |
173 // latency estimates. | |
174 bool ShouldSwitch(cricket::Connection* a_conn, | |
175 cricket::Connection* b_conn, | |
176 cricket::IceRole ice_role) { | |
177 if (a_conn == b_conn) | |
178 return false; | |
179 | |
180 if (!a_conn || !b_conn) // don't think the latter should happen | |
181 return true; | |
182 | |
183 // We prefer to switch to a writable and receiving connection over a | |
184 // non-writable or non-receiving connection, even if the latter has | |
185 // been nominated by the controlling side. | |
186 int state_cmp = CompareConnectionStates(a_conn, b_conn); | |
187 if (state_cmp != 0) { | |
188 return state_cmp < 0; | |
189 } | |
190 if (ice_role == cricket::ICEROLE_CONTROLLED && a_conn->nominated()) { | |
191 LOG(LS_VERBOSE) << "Controlled side did not switch due to nominated status"; | |
192 return false; | |
193 } | |
194 | |
195 int prefs_cmp = CompareConnectionCandidates(a_conn, b_conn); | |
196 if (prefs_cmp != 0) { | |
197 return prefs_cmp < 0; | |
198 } | |
199 | |
200 return b_conn->rtt() <= a_conn->rtt() + kMinImprovement; | |
201 } | |
202 | |
203 } // unnamed namespace | 54 } // unnamed namespace |
204 | 55 |
205 namespace cricket { | 56 namespace cricket { |
206 | 57 |
207 // When the socket is unwritable, we will use 10 Kbps (ignoring IP+UDP headers) | 58 // When the socket is unwritable, we will use 10 Kbps (ignoring IP+UDP headers) |
208 // for pinging. When the socket is writable, we will use only 1 Kbps because | 59 // for pinging. When the socket is writable, we will use only 1 Kbps because |
209 // we don't want to degrade the quality on a modem. These numbers should work | 60 // we don't want to degrade the quality on a modem. These numbers should work |
210 // well on a 28.8K modem, which is the slowest connection on which the voice | 61 // well on a 28.8K modem, which is the slowest connection on which the voice |
211 // quality is reasonable at all. | 62 // quality is reasonable at all. |
212 static const int PING_PACKET_SIZE = 60 * 8; | 63 static const int PING_PACKET_SIZE = 60 * 8; |
(...skipping 30 matching lines...) Expand all Loading... | |
243 sort_dirty_(false), | 94 sort_dirty_(false), |
244 remote_ice_mode_(ICEMODE_FULL), | 95 remote_ice_mode_(ICEMODE_FULL), |
245 ice_role_(ICEROLE_UNKNOWN), | 96 ice_role_(ICEROLE_UNKNOWN), |
246 tiebreaker_(0), | 97 tiebreaker_(0), |
247 gathering_state_(kIceGatheringNew), | 98 gathering_state_(kIceGatheringNew), |
248 check_receiving_interval_(MIN_CHECK_RECEIVING_INTERVAL * 5), | 99 check_receiving_interval_(MIN_CHECK_RECEIVING_INTERVAL * 5), |
249 config_(MIN_CHECK_RECEIVING_INTERVAL * 50 /* receiving_timeout */, | 100 config_(MIN_CHECK_RECEIVING_INTERVAL * 50 /* receiving_timeout */, |
250 0 /* backup_connection_ping_interval */, | 101 0 /* backup_connection_ping_interval */, |
251 false /* gather_continually */, | 102 false /* gather_continually */, |
252 false /* prioritize_most_likely_candidate_pairs */, | 103 false /* prioritize_most_likely_candidate_pairs */, |
253 MAX_CURRENT_STRONG_INTERVAL /* max_strong_interval */) { | 104 MAX_CURRENT_STRONG_INTERVAL /* max_strong_interval */, |
105 true /* presume_writable_when_fully_relayed */) { | |
254 uint32_t weak_ping_interval = ::strtoul( | 106 uint32_t weak_ping_interval = ::strtoul( |
255 webrtc::field_trial::FindFullName("WebRTC-StunInterPacketDelay").c_str(), | 107 webrtc::field_trial::FindFullName("WebRTC-StunInterPacketDelay").c_str(), |
256 nullptr, 10); | 108 nullptr, 10); |
257 if (weak_ping_interval) { | 109 if (weak_ping_interval) { |
258 weak_ping_interval_ = static_cast<int>(weak_ping_interval); | 110 weak_ping_interval_ = static_cast<int>(weak_ping_interval); |
259 } | 111 } |
260 } | 112 } |
261 | 113 |
262 P2PTransportChannel::~P2PTransportChannel() { | 114 P2PTransportChannel::~P2PTransportChannel() { |
263 ASSERT(worker_thread_ == rtc::Thread::Current()); | 115 ASSERT(worker_thread_ == rtc::Thread::Current()); |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
431 config.prioritize_most_likely_candidate_pairs; | 283 config.prioritize_most_likely_candidate_pairs; |
432 LOG(LS_INFO) << "Set ping most likely connection to " | 284 LOG(LS_INFO) << "Set ping most likely connection to " |
433 << config_.prioritize_most_likely_candidate_pairs; | 285 << config_.prioritize_most_likely_candidate_pairs; |
434 | 286 |
435 if (config.max_strong_interval >= 0 && | 287 if (config.max_strong_interval >= 0 && |
436 config_.max_strong_interval != config.max_strong_interval) { | 288 config_.max_strong_interval != config.max_strong_interval) { |
437 config_.max_strong_interval = config.max_strong_interval; | 289 config_.max_strong_interval = config.max_strong_interval; |
438 LOG(LS_INFO) << "Set max strong interval to " | 290 LOG(LS_INFO) << "Set max strong interval to " |
439 << config_.max_strong_interval; | 291 << config_.max_strong_interval; |
440 } | 292 } |
293 | |
294 if (config.presume_writable_when_fully_relayed != | |
295 config_.presume_writable_when_fully_relayed) { | |
296 if (!connections_.empty()) { | |
297 LOG(LS_ERROR) << "Trying to change 'presume writable' " | |
298 << "while connections already exist!"; | |
299 } else { | |
300 config_.presume_writable_when_fully_relayed = | |
301 config.presume_writable_when_fully_relayed; | |
302 LOG(LS_INFO) << "Set presume writable when fully relayed to " | |
303 << config_.presume_writable_when_fully_relayed; | |
304 } | |
305 } | |
441 } | 306 } |
442 | 307 |
443 const IceConfig& P2PTransportChannel::config() const { | 308 const IceConfig& P2PTransportChannel::config() const { |
444 return config_; | 309 return config_; |
445 } | 310 } |
446 | 311 |
447 // Go into the state of processing candidates, and running in general | 312 // Go into the state of processing candidates, and running in general |
448 void P2PTransportChannel::Connect() { | 313 void P2PTransportChannel::Connect() { |
449 ASSERT(worker_thread_ == rtc::Thread::Current()); | 314 ASSERT(worker_thread_ == rtc::Thread::Current()); |
450 if (ice_ufrag_.empty() || ice_pwd_.empty()) { | 315 if (ice_ufrag_.empty() || ice_pwd_.empty()) { |
(...skipping 605 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1056 } | 921 } |
1057 | 922 |
1058 // Prepare for best candidate sorting. | 923 // Prepare for best candidate sorting. |
1059 void P2PTransportChannel::RequestSort() { | 924 void P2PTransportChannel::RequestSort() { |
1060 if (!sort_dirty_) { | 925 if (!sort_dirty_) { |
1061 worker_thread_->Post(RTC_FROM_HERE, this, MSG_SORT); | 926 worker_thread_->Post(RTC_FROM_HERE, this, MSG_SORT); |
1062 sort_dirty_ = true; | 927 sort_dirty_ = true; |
1063 } | 928 } |
1064 } | 929 } |
1065 | 930 |
931 // Compare two connections based on their writing, receiving, and connected | |
932 // states. | |
933 int P2PTransportChannel::CompareConnectionStates(const Connection* a, | |
934 const Connection* b) const { | |
935 // Sort based on write-state. Better states have lower values. | |
936 if (a->write_state() < b->write_state()) { | |
937 return 1; | |
938 } | |
939 if (b->write_state() < a->write_state()) { | |
940 return -1; | |
941 } | |
942 | |
943 bool a_presumed_writable = PresumedWritable(a); | |
944 bool b_presumed_writable = PresumedWritable(b); | |
945 if (a_presumed_writable != b_presumed_writable) { | |
946 return a_presumed_writable ? 1 : -1; | |
947 } | |
pthatcher1
2016/06/22 16:08:24
To go along with the write state comparison, this
Taylor Brandstetter
2016/06/22 16:18:27
Done.
| |
948 | |
949 // We prefer a receiving connection to a non-receiving, higher-priority | |
950 // connection when sorting connections and choosing which connection to | |
951 // switch to. | |
952 if (a->receiving() && !b->receiving()) { | |
953 return 1; | |
954 } | |
955 if (!a->receiving() && b->receiving()) { | |
956 return -1; | |
957 } | |
958 | |
959 // WARNING: Some complexity here about TCP reconnecting. | |
960 // When a TCP connection fails because of a TCP socket disconnecting, the | |
961 // active side of the connection will attempt to reconnect for 5 seconds while | |
962 // pretending to be writable (the connection is not set to the unwritable | |
963 // state). On the passive side, the connection also remains writable even | |
964 // though it is disconnected, and a new connection is created when the active | |
965 // side connects. At that point, there are two TCP connections on the passive | |
966 // side: 1. the old, disconnected one that is pretending to be writable, and | |
967 // 2. the new, connected one that is maybe not yet writable. For purposes of | |
968 // pruning, pinging, and selecting the best connection, we want to treat the | |
969 // new connection as "better" than the old one. We could add a method called | |
970 // something like Connection::ImReallyBadEvenThoughImWritable, but that is | |
971 // equivalent to the existing Connection::connected(), which we already have. | |
972 // So, in code throughout this file, we'll check whether the connection is | |
973 // connected() or not, and if it is not, treat it as "worse" than a connected | |
974 // one, even though it's writable. In the code below, we're doing so to make | |
975 // sure we treat a new writable connection as better than an old disconnected | |
976 // connection. | |
977 | |
978 // In the case where we reconnect TCP connections, the original best | |
979 // connection is disconnected without changing to WRITE_TIMEOUT. In this case, | |
980 // the new connection, when it becomes writable, should have higher priority. | |
981 if (a->write_state() == Connection::STATE_WRITABLE && | |
982 b->write_state() == Connection::STATE_WRITABLE) { | |
983 if (a->connected() && !b->connected()) { | |
984 return 1; | |
985 } | |
986 if (!a->connected() && b->connected()) { | |
987 return -1; | |
988 } | |
989 } | |
990 return 0; | |
991 } | |
992 | |
993 // Compares two connections based only on the candidate and network information. | |
994 // Returns positive if |a| is better than |b|. | |
995 int P2PTransportChannel::CompareConnectionCandidates( | |
996 const Connection* a, | |
997 const Connection* b) const { | |
998 // Prefer lower network cost. | |
999 uint32_t a_cost = a->ComputeNetworkCost(); | |
1000 uint32_t b_cost = b->ComputeNetworkCost(); | |
1001 // Smaller cost is better. | |
1002 if (a_cost < b_cost) { | |
1003 return 1; | |
1004 } | |
1005 if (a_cost > b_cost) { | |
1006 return -1; | |
1007 } | |
1008 | |
1009 // Compare connection priority. Lower values get sorted last. | |
1010 if (a->priority() > b->priority()) { | |
1011 return 1; | |
1012 } | |
1013 if (a->priority() < b->priority()) { | |
1014 return -1; | |
1015 } | |
1016 | |
1017 // If we're still tied at this point, prefer a younger generation. | |
1018 // (Younger generation means a larger generation number). | |
1019 return (a->remote_candidate().generation() + a->port()->generation()) - | |
1020 (b->remote_candidate().generation() + b->port()->generation()); | |
1021 } | |
1022 | |
1023 int P2PTransportChannel::CompareConnections(const Connection* a, | |
1024 const Connection* b) const { | |
1025 // Compare first on writability and static preferences. | |
1026 int state_cmp = CompareConnectionStates(a, b); | |
1027 if (state_cmp != 0) { | |
1028 return state_cmp; | |
1029 } | |
1030 // Then compare the candidate information. | |
1031 int candidates_cmp = CompareConnectionCandidates(a, b); | |
1032 if (candidates_cmp != 0) { | |
1033 return candidates_cmp; | |
1034 } | |
1035 // Otherwise, compare based on latency estimate. | |
1036 return b->rtt() - a->rtt(); | |
1037 | |
1038 // Should we bother checking for the last connection that last received | |
1039 // data? It would help rendezvous on the connection that is also receiving | |
1040 // packets. | |
1041 // | |
1042 // TODO(deadbeef): Yes we should definitely do this. The TCP protocol gains | |
1043 // efficiency by being used bidirectionally, as opposed to two separate | |
1044 // unidirectional streams. This test should probably occur before | |
1045 // comparison of local prefs (assuming combined prefs are the same). We | |
1046 // need to be careful though, not to bounce back and forth with both sides | |
1047 // trying to rendevous with the other. | |
1048 } | |
1049 | |
1050 bool P2PTransportChannel::PresumedWritable( | |
1051 const cricket::Connection* conn) const { | |
1052 return (conn->write_state() == Connection::STATE_WRITE_INIT && | |
1053 config_.presume_writable_when_fully_relayed && | |
1054 conn->local_candidate().type() == RELAY_PORT_TYPE && | |
1055 (conn->remote_candidate().type() == RELAY_PORT_TYPE || | |
1056 conn->remote_candidate().type() == PRFLX_PORT_TYPE)); | |
1057 } | |
pthatcher1
2016/06/22 16:08:24
This is a really good idea. It's much cleaner and
| |
1058 | |
1059 // Determines whether we should switch between two connections, based first on | |
1060 // connection states, static preferences, and then (if those are equal) on | |
1061 // latency estimates. | |
1062 bool P2PTransportChannel::ShouldSwitchSelectedConnection( | |
1063 const Connection* selected, | |
1064 const Connection* conn) const { | |
1065 if (selected == conn) { | |
1066 return false; | |
1067 } | |
1068 | |
1069 if (!selected || !conn) { // don't think the latter should happen | |
1070 return true; | |
1071 } | |
1072 | |
1073 // We prefer to switch to a writable and receiving connection over a | |
1074 // non-writable or non-receiving connection, even if the latter has | |
1075 // been nominated by the controlling side. | |
1076 int state_cmp = CompareConnectionStates(selected, conn); | |
1077 if (state_cmp != 0) { | |
1078 return state_cmp < 0; | |
1079 } | |
1080 if (ice_role_ == ICEROLE_CONTROLLED && selected->nominated()) { | |
1081 LOG(LS_VERBOSE) << "Controlled side did not switch due to nominated status"; | |
1082 return false; | |
1083 } | |
1084 | |
1085 int prefs_cmp = CompareConnectionCandidates(selected, conn); | |
1086 if (prefs_cmp != 0) { | |
1087 return prefs_cmp < 0; | |
1088 } | |
1089 | |
1090 return selected->rtt() - conn->rtt() >= kMinImprovement; | |
1091 } | |
1092 | |
1066 // Sort the available connections to find the best one. We also monitor | 1093 // Sort the available connections to find the best one. We also monitor |
1067 // the number of available connections and the current state. | 1094 // the number of available connections and the current state. |
1068 void P2PTransportChannel::SortConnections() { | 1095 void P2PTransportChannel::SortConnections() { |
1069 ASSERT(worker_thread_ == rtc::Thread::Current()); | 1096 ASSERT(worker_thread_ == rtc::Thread::Current()); |
1070 | 1097 |
1071 // Make sure the connection states are up-to-date since this affects how they | 1098 // Make sure the connection states are up-to-date since this affects how they |
1072 // will be sorted. | 1099 // will be sorted. |
1073 UpdateConnectionStates(); | 1100 UpdateConnectionStates(); |
1074 | 1101 |
1075 // Any changes after this point will require a re-sort. | 1102 // Any changes after this point will require a re-sort. |
1076 sort_dirty_ = false; | 1103 sort_dirty_ = false; |
1077 | 1104 |
1078 // Find the best alternative connection by sorting. It is important to note | 1105 // Find the best alternative connection by sorting. It is important to note |
1079 // that amongst equal preference, writable connections, this will choose the | 1106 // that amongst equal preference, writable connections, this will choose the |
1080 // one whose estimated latency is lowest. So it is the only one that we | 1107 // one whose estimated latency is lowest. So it is the only one that we |
1081 // need to consider switching to. | 1108 // need to consider switching to. |
1082 ConnectionCompare cmp; | 1109 std::stable_sort(connections_.begin(), connections_.end(), |
1083 std::stable_sort(connections_.begin(), connections_.end(), cmp); | 1110 [this](const Connection* a, const Connection* b) { |
1111 return CompareConnections(a, b) > 0; | |
1112 }); | |
1084 LOG(LS_VERBOSE) << "Sorting " << connections_.size() | 1113 LOG(LS_VERBOSE) << "Sorting " << connections_.size() |
1085 << " available connections:"; | 1114 << " available connections:"; |
1086 for (size_t i = 0; i < connections_.size(); ++i) { | 1115 for (size_t i = 0; i < connections_.size(); ++i) { |
1087 LOG(LS_VERBOSE) << connections_[i]->ToString(); | 1116 LOG(LS_VERBOSE) << connections_[i]->ToString(); |
1088 } | 1117 } |
1089 | 1118 |
1090 Connection* top_connection = | 1119 Connection* top_connection = |
1091 (connections_.size() > 0) ? connections_[0] : nullptr; | 1120 (connections_.size() > 0) ? connections_[0] : nullptr; |
1092 | 1121 |
1093 // If necessary, switch to the new choice. | 1122 // If necessary, switch to the new choice. |
1094 // Note that |top_connection| doesn't have to be writable to become the best | 1123 // Note that |top_connection| doesn't have to be writable to become the best |
1095 // connection although it will have higher priority if it is writable. | 1124 // connection although it will have higher priority if it is writable. |
1096 if (ShouldSwitch(best_connection_, top_connection, ice_role_)) { | 1125 if (ShouldSwitchSelectedConnection(best_connection_, top_connection)) { |
1097 LOG(LS_INFO) << "Switching best connection: " << top_connection->ToString(); | 1126 LOG(LS_INFO) << "Switching best connection: " << top_connection->ToString(); |
1098 SwitchBestConnectionTo(top_connection); | 1127 SwitchBestConnectionTo(top_connection); |
1099 } | 1128 } |
1100 | 1129 |
1101 // Controlled side can prune only if the best connection has been nominated. | 1130 // Controlled side can prune only if the best connection has been nominated. |
1102 // because otherwise it may delete the connection that will be selected by | 1131 // because otherwise it may delete the connection that will be selected by |
1103 // the controlling side. | 1132 // the controlling side. |
1104 if (ice_role_ == ICEROLE_CONTROLLING || best_nominated_connection()) { | 1133 if (ice_role_ == ICEROLE_CONTROLLING || best_nominated_connection()) { |
1105 PruneConnections(); | 1134 PruneConnections(); |
1106 } | 1135 } |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1175 << old_best_connection->ToString(); | 1204 << old_best_connection->ToString(); |
1176 } | 1205 } |
1177 LOG_J(LS_INFO, this) << "New best connection: " | 1206 LOG_J(LS_INFO, this) << "New best connection: " |
1178 << best_connection_->ToString(); | 1207 << best_connection_->ToString(); |
1179 SignalRouteChange(this, best_connection_->remote_candidate()); | 1208 SignalRouteChange(this, best_connection_->remote_candidate()); |
1180 // This is a temporary, but safe fix to webrtc issue 5705. | 1209 // This is a temporary, but safe fix to webrtc issue 5705. |
1181 // TODO(honghaiz): Make all EWOULDBLOCK error routed through the transport | 1210 // TODO(honghaiz): Make all EWOULDBLOCK error routed through the transport |
1182 // channel so that it knows whether the media channel is allowed to | 1211 // channel so that it knows whether the media channel is allowed to |
1183 // send; then it will only signal ready-to-send if the media channel | 1212 // send; then it will only signal ready-to-send if the media channel |
1184 // has been disallowed to send. | 1213 // has been disallowed to send. |
1185 if (best_connection_->writable()) { | 1214 if (best_connection_->writable() || PresumedWritable(best_connection_)) { |
1186 SignalReadyToSend(this); | 1215 SignalReadyToSend(this); |
1187 } | 1216 } |
1188 } else { | 1217 } else { |
1189 LOG_J(LS_INFO, this) << "No best connection"; | 1218 LOG_J(LS_INFO, this) << "No best connection"; |
1190 } | 1219 } |
1191 // TODO(honghaiz): rename best_connection_ with selected_connection_ or | 1220 // TODO(honghaiz): rename best_connection_ with selected_connection_ or |
1192 // selected_candidate pair_. | 1221 // selected_candidate pair_. |
1193 SignalSelectedCandidatePairChanged(this, best_connection_, | 1222 SignalSelectedCandidatePairChanged(this, best_connection_, |
1194 last_sent_packet_id_); | 1223 last_sent_packet_id_); |
1195 } | 1224 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1230 RTC_DCHECK(state == STATE_CONNECTING || state == STATE_COMPLETED); | 1259 RTC_DCHECK(state == STATE_CONNECTING || state == STATE_COMPLETED); |
1231 break; | 1260 break; |
1232 default: | 1261 default: |
1233 RTC_DCHECK(false); | 1262 RTC_DCHECK(false); |
1234 break; | 1263 break; |
1235 } | 1264 } |
1236 state_ = state; | 1265 state_ = state; |
1237 SignalStateChanged(this); | 1266 SignalStateChanged(this); |
1238 } | 1267 } |
1239 | 1268 |
1240 bool writable = best_connection_ && best_connection_->writable(); | 1269 // If our best connection is "presumed writable" (TURN-TURN with no |
1241 set_writable(writable); | 1270 // CreatePermission required), act like we're already writable to the upper |
1271 // layers, so they can start media quicker. | |
1272 set_writable(best_connection_ && (best_connection_->writable() || | |
1273 PresumedWritable(best_connection_))); | |
1242 | 1274 |
1243 bool receiving = false; | 1275 bool receiving = false; |
1244 for (const Connection* connection : connections_) { | 1276 for (const Connection* connection : connections_) { |
1245 if (connection->receiving()) { | 1277 if (connection->receiving()) { |
1246 receiving = true; | 1278 receiving = true; |
1247 break; | 1279 break; |
1248 } | 1280 } |
1249 } | 1281 } |
1250 set_receiving(receiving); | 1282 set_receiving(receiving); |
1251 } | 1283 } |
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1688 | 1720 |
1689 // During the initial state when nothing has been pinged yet, return the first | 1721 // During the initial state when nothing has been pinged yet, return the first |
1690 // one in the ordered |connections_|. | 1722 // one in the ordered |connections_|. |
1691 return *(std::find_if(connections_.begin(), connections_.end(), | 1723 return *(std::find_if(connections_.begin(), connections_.end(), |
1692 [conn1, conn2](Connection* conn) { | 1724 [conn1, conn2](Connection* conn) { |
1693 return conn == conn1 || conn == conn2; | 1725 return conn == conn1 || conn == conn2; |
1694 })); | 1726 })); |
1695 } | 1727 } |
1696 | 1728 |
1697 } // namespace cricket | 1729 } // namespace cricket |
OLD | NEW |