Chromium Code Reviews| 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 |
| 11 // P2PTransportChannel wraps up the state management of the connection between | 11 // P2PTransportChannel wraps up the state management of the connection between |
| 12 // two P2P clients. Clients have candidate ports for connecting, and | 12 // two P2P clients. Clients have candidate ports for connecting, and |
| 13 // connections which are combinations of candidates from each end (Alice and | 13 // connections which are combinations of candidates from each end (Alice and |
| 14 // Bob each have candidates, one candidate from Alice and one candidate from | 14 // Bob each have candidates, one candidate from Alice and one candidate from |
| 15 // Bob are used to make a connection, repeat to make many connections). | 15 // Bob are used to make a connection, repeat to make many connections). |
| 16 // | 16 // |
| 17 // When all of the available connections become invalid (non-writable), we | 17 // When all of the available connections become invalid (non-writable), we |
| 18 // kick off a process of determining more candidates and more connections. | 18 // kick off a process of determining more candidates and more connections. |
| 19 // | 19 // |
| 20 #ifndef WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_ | 20 #ifndef WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_ |
| 21 #define WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_ | 21 #define WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_ |
| 22 | 22 |
| 23 #include <map> | 23 #include <map> |
| 24 #include <memory> | 24 #include <memory> |
| 25 #include <set> | 25 #include <set> |
| 26 #include <string> | 26 #include <string> |
| 27 #include <vector> | 27 #include <vector> |
| 28 | 28 |
| 29 #include "webrtc/base/asyncpacketsocket.h" | |
| 29 #include "webrtc/base/constructormagic.h" | 30 #include "webrtc/base/constructormagic.h" |
| 31 #include "webrtc/base/sigslot.h" | |
| 30 #include "webrtc/p2p/base/candidate.h" | 32 #include "webrtc/p2p/base/candidate.h" |
| 31 #include "webrtc/p2p/base/candidatepairinterface.h" | 33 #include "webrtc/p2p/base/candidatepairinterface.h" |
| 34 #include "webrtc/p2p/base/icetransportinternal.h" | |
| 32 #include "webrtc/p2p/base/portallocator.h" | 35 #include "webrtc/p2p/base/portallocator.h" |
| 33 #include "webrtc/p2p/base/portinterface.h" | 36 #include "webrtc/p2p/base/portinterface.h" |
| 34 #include "webrtc/p2p/base/transportchannelimpl.h" | |
| 35 #include "webrtc/base/asyncpacketsocket.h" | |
| 36 #include "webrtc/base/sigslot.h" | |
| 37 | 37 |
| 38 namespace cricket { | 38 namespace cricket { |
| 39 | 39 |
| 40 // Enum for UMA metrics, used to record whether the channel is | 40 // Enum for UMA metrics, used to record whether the channel is |
| 41 // connected/connecting/disconnected when ICE restart happens. | 41 // connected/connecting/disconnected when ICE restart happens. |
| 42 enum class IceRestartState { CONNECTING, CONNECTED, DISCONNECTED, MAX_VALUE }; | 42 enum class IceRestartState { CONNECTING, CONNECTED, DISCONNECTED, MAX_VALUE }; |
| 43 | 43 |
| 44 extern const int WEAK_PING_INTERVAL; | 44 extern const int WEAK_PING_INTERVAL; |
| 45 extern const int WEAK_OR_STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL; | 45 extern const int WEAK_OR_STABILIZING_WRITABLE_CONNECTION_PING_INTERVAL; |
| 46 extern const int STRONG_AND_STABLE_WRITABLE_CONNECTION_PING_INTERVAL; | 46 extern const int STRONG_AND_STABLE_WRITABLE_CONNECTION_PING_INTERVAL; |
| 47 static const int MIN_PINGS_AT_WEAK_PING_INTERVAL = 3; | 47 static const int MIN_PINGS_AT_WEAK_PING_INTERVAL = 3; |
| 48 | 48 |
| 49 // Adds the port on which the candidate originated. | 49 // Adds the port on which the candidate originated. |
| 50 class RemoteCandidate : public Candidate { | 50 class RemoteCandidate : public Candidate { |
| 51 public: | 51 public: |
| 52 RemoteCandidate(const Candidate& c, PortInterface* origin_port) | 52 RemoteCandidate(const Candidate& c, PortInterface* origin_port) |
| 53 : Candidate(c), origin_port_(origin_port) {} | 53 : Candidate(c), origin_port_(origin_port) {} |
| 54 | 54 |
| 55 PortInterface* origin_port() { return origin_port_; } | 55 PortInterface* origin_port() { return origin_port_; } |
| 56 | 56 |
| 57 private: | 57 private: |
| 58 PortInterface* origin_port_; | 58 PortInterface* origin_port_; |
| 59 }; | 59 }; |
| 60 | 60 |
| 61 // P2PTransportChannel manages the candidates and connection process to keep | 61 // P2PTransportChannel manages the candidates and connection process to keep |
| 62 // two P2P clients connected to each other. | 62 // two P2P clients connected to each other. |
| 63 class P2PTransportChannel : public TransportChannelImpl, | 63 class P2PTransportChannel : public IceTransportInternal, |
| 64 public rtc::MessageHandler { | 64 public rtc::MessageHandler { |
| 65 public: | 65 public: |
| 66 P2PTransportChannel(const std::string& transport_name, | 66 P2PTransportChannel(const std::string& transport_name, |
| 67 int component, | 67 int component, |
| 68 PortAllocator* allocator); | 68 PortAllocator* allocator); |
| 69 virtual ~P2PTransportChannel(); | 69 virtual ~P2PTransportChannel(); |
| 70 | 70 |
| 71 // From TransportChannelImpl: | 71 // From TransportChannelImpl: |
| 72 TransportChannelState GetState() const override; | 72 TransportState GetState() const override; |
| 73 const std::string& transport_name() const override { return transport_name_; } | |
| 74 int component() const override { return component_; } | |
| 75 bool writable() const override { return writable_; } | |
| 76 bool receiving() const override { return receiving_; } | |
| 73 void SetIceRole(IceRole role) override; | 77 void SetIceRole(IceRole role) override; |
| 74 IceRole GetIceRole() const override { return ice_role_; } | 78 IceRole GetIceRole() const override { return ice_role_; } |
| 75 void SetIceTiebreaker(uint64_t tiebreaker) override; | 79 void SetIceTiebreaker(uint64_t tiebreaker) override; |
| 76 void SetIceParameters(const IceParameters& ice_params) override; | 80 void SetIceParameters(const IceParameters& ice_params) override; |
| 77 void SetRemoteIceParameters(const IceParameters& ice_params) override; | 81 void SetRemoteIceParameters(const IceParameters& ice_params) override; |
| 78 void SetRemoteIceMode(IceMode mode) override; | 82 void SetRemoteIceMode(IceMode mode) override; |
| 79 // TODO(deadbeef): Deprecated. Remove when Chromium's | 83 // TODO(deadbeef): Deprecated. Remove when Chromium's |
| 80 // IceTransportChannel does not depend on this. | 84 // IceTransportChannel does not depend on this. |
| 81 void Connect() {} | 85 void Connect() {} |
| 82 void MaybeStartGathering() override; | 86 void MaybeStartGathering() override; |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 111 const Connection* selected_connection() const { return selected_connection_; } | 115 const Connection* selected_connection() const { return selected_connection_; } |
| 112 void set_incoming_only(bool value) { incoming_only_ = value; } | 116 void set_incoming_only(bool value) { incoming_only_ = value; } |
| 113 | 117 |
| 114 // Note: These are only for testing purpose. | 118 // Note: These are only for testing purpose. |
| 115 // |ports_| and |pruned_ports| should not be changed from outside. | 119 // |ports_| and |pruned_ports| should not be changed from outside. |
| 116 const std::vector<PortInterface*>& ports() { return ports_; } | 120 const std::vector<PortInterface*>& ports() { return ports_; } |
| 117 const std::vector<PortInterface*>& pruned_ports() { return pruned_ports_; } | 121 const std::vector<PortInterface*>& pruned_ports() { return pruned_ports_; } |
| 118 | 122 |
| 119 IceMode remote_ice_mode() const { return remote_ice_mode_; } | 123 IceMode remote_ice_mode() const { return remote_ice_mode_; } |
| 120 | 124 |
| 121 // DTLS methods. | |
| 122 bool IsDtlsActive() const override { return false; } | |
| 123 | |
| 124 // Default implementation. | |
| 125 bool GetSslRole(rtc::SSLRole* role) const override { return false; } | |
| 126 | |
| 127 bool SetSslRole(rtc::SSLRole role) override { return false; } | |
| 128 | |
| 129 // Set up the ciphers to use for DTLS-SRTP. | |
| 130 bool SetSrtpCryptoSuites(const std::vector<int>& ciphers) override { | |
| 131 return false; | |
| 132 } | |
| 133 | |
| 134 // Find out which DTLS-SRTP cipher was negotiated. | |
| 135 bool GetSrtpCryptoSuite(int* cipher) override { return false; } | |
| 136 | |
| 137 // Find out which DTLS cipher was negotiated. | |
| 138 bool GetSslCipherSuite(int* cipher) override { return false; } | |
| 139 | |
| 140 // Returns null because the channel is not encrypted by default. | |
| 141 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override { | |
| 142 return nullptr; | |
| 143 } | |
| 144 | |
| 145 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate() | |
| 146 const override { | |
| 147 return nullptr; | |
| 148 } | |
| 149 | |
| 150 // Allows key material to be extracted for external encryption. | |
| 151 bool ExportKeyingMaterial(const std::string& label, | |
| 152 const uint8_t* context, | |
| 153 size_t context_len, | |
| 154 bool use_context, | |
| 155 uint8_t* result, | |
| 156 size_t result_len) override { | |
| 157 return false; | |
| 158 } | |
| 159 | |
| 160 bool SetLocalCertificate( | |
| 161 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override { | |
| 162 return false; | |
| 163 } | |
| 164 | |
| 165 // Set DTLS Remote fingerprint. Must be after local identity set. | |
| 166 bool SetRemoteFingerprint(const std::string& digest_alg, | |
| 167 const uint8_t* digest, | |
| 168 size_t digest_len) override { | |
| 169 return false; | |
| 170 } | |
| 171 | |
| 172 void PruneAllPorts(); | 125 void PruneAllPorts(); |
| 173 int receiving_timeout() const { return config_.receiving_timeout; } | 126 int receiving_timeout() const { return config_.receiving_timeout; } |
| 174 int check_receiving_interval() const { return check_receiving_interval_; } | 127 int check_receiving_interval() const { return check_receiving_interval_; } |
| 175 | 128 |
| 176 // Helper method used only in unittest. | 129 // Helper method used only in unittest. |
| 177 rtc::DiffServCodePoint DefaultDscpValue() const; | 130 rtc::DiffServCodePoint DefaultDscpValue() const; |
| 178 | 131 |
| 179 // Public for unit tests. | 132 // Public for unit tests. |
| 180 Connection* FindNextPingableConnection(); | 133 Connection* FindNextPingableConnection(); |
| 181 void MarkConnectionPinged(Connection* conn); | 134 void MarkConnectionPinged(Connection* conn); |
| 182 | 135 |
| 183 // Public for unit tests. | 136 // Public for unit tests. |
| 184 const std::vector<Connection*>& connections() const { return connections_; } | 137 const std::vector<Connection*>& connections() const { return connections_; } |
| 185 | 138 |
| 186 // Public for unit tests. | 139 // Public for unit tests. |
| 187 PortAllocatorSession* allocator_session() { | 140 PortAllocatorSession* allocator_session() { |
| 188 return allocator_sessions_.back().get(); | 141 return allocator_sessions_.back().get(); |
| 189 } | 142 } |
| 190 | 143 |
| 191 // Public for unit tests. | 144 // Public for unit tests. |
| 192 const std::vector<RemoteCandidate>& remote_candidates() const { | 145 const std::vector<RemoteCandidate>& remote_candidates() const { |
| 193 return remote_candidates_; | 146 return remote_candidates_; |
| 194 } | 147 } |
| 195 | 148 |
| 149 std::string ToString() const { | |
| 150 const char RECEIVING_ABBREV[2] = {'_', 'R'}; | |
| 151 const char WRITABLE_ABBREV[2] = {'_', 'W'}; | |
| 152 std::stringstream ss; | |
| 153 ss << "Channel[" << transport_name_ << "|" << component_ << "|" | |
| 154 << RECEIVING_ABBREV[receiving_] << WRITABLE_ABBREV[writable_] << "]"; | |
| 155 return ss.str(); | |
| 156 } | |
|
Taylor Brandstetter
2016/12/20 03:37:26
Is this meant to override debug_string?
Zhi Huang
2016/12/20 20:17:43
This is only used for libjingle's logging. (LOG_J)
| |
| 157 | |
| 196 private: | 158 private: |
| 197 rtc::Thread* thread() const { return network_thread_; } | 159 rtc::Thread* thread() const { return network_thread_; } |
| 198 bool IsGettingPorts() { return allocator_session()->IsGettingPorts(); } | 160 bool IsGettingPorts() { return allocator_session()->IsGettingPorts(); } |
| 199 | 161 |
| 200 // A transport channel is weak if the current best connection is either | 162 // A transport channel is weak if the current best connection is either |
| 201 // not receiving or not writable, or if there is no best connection at all. | 163 // not receiving or not writable, or if there is no best connection at all. |
| 202 bool weak() const; | 164 bool weak() const; |
| 203 // Returns true if it's possible to send packets on |connection|. | 165 // Returns true if it's possible to send packets on |connection|. |
| 204 bool ReadyToSend(Connection* connection) const; | 166 bool ReadyToSend(Connection* connection) const; |
| 205 void UpdateConnectionStates(); | 167 void UpdateConnectionStates(); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 232 rtc::Optional<int64_t> receiving_unchanged_threshold, | 194 rtc::Optional<int64_t> receiving_unchanged_threshold, |
| 233 bool* missed_receiving_unchanged_threshold) const; | 195 bool* missed_receiving_unchanged_threshold) const; |
| 234 | 196 |
| 235 bool PresumedWritable(const cricket::Connection* conn) const; | 197 bool PresumedWritable(const cricket::Connection* conn) const; |
| 236 | 198 |
| 237 void SortConnectionsAndUpdateState(); | 199 void SortConnectionsAndUpdateState(); |
| 238 void SwitchSelectedConnection(Connection* conn); | 200 void SwitchSelectedConnection(Connection* conn); |
| 239 void UpdateState(); | 201 void UpdateState(); |
| 240 void HandleAllTimedOut(); | 202 void HandleAllTimedOut(); |
| 241 void MaybeStopPortAllocatorSessions(); | 203 void MaybeStopPortAllocatorSessions(); |
| 242 TransportChannelState ComputeState() const; | 204 TransportState ComputeState() const; |
| 243 | 205 |
| 244 Connection* GetBestConnectionOnNetwork(rtc::Network* network) const; | 206 Connection* GetBestConnectionOnNetwork(rtc::Network* network) const; |
| 245 bool CreateConnections(const Candidate& remote_candidate, | 207 bool CreateConnections(const Candidate& remote_candidate, |
| 246 PortInterface* origin_port); | 208 PortInterface* origin_port); |
| 247 bool CreateConnection(PortInterface* port, | 209 bool CreateConnection(PortInterface* port, |
| 248 const Candidate& remote_candidate, | 210 const Candidate& remote_candidate, |
| 249 PortInterface* origin_port); | 211 PortInterface* origin_port); |
| 250 bool FindConnection(cricket::Connection* connection) const; | 212 bool FindConnection(cricket::Connection* connection) const; |
| 251 | 213 |
| 252 uint32_t GetRemoteCandidateGeneration(const Candidate& candidate); | 214 uint32_t GetRemoteCandidateGeneration(const Candidate& candidate); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 340 const IceParameters* FindRemoteIceFromUfrag(const std::string& ufrag, | 302 const IceParameters* FindRemoteIceFromUfrag(const std::string& ufrag, |
| 341 uint32_t* generation); | 303 uint32_t* generation); |
| 342 // Returns the index of the latest remote ICE parameters, or 0 if no remote | 304 // Returns the index of the latest remote ICE parameters, or 0 if no remote |
| 343 // ICE parameters have been received. | 305 // ICE parameters have been received. |
| 344 uint32_t remote_ice_generation() { | 306 uint32_t remote_ice_generation() { |
| 345 return remote_ice_parameters_.empty() | 307 return remote_ice_parameters_.empty() |
| 346 ? 0 | 308 ? 0 |
| 347 : static_cast<uint32_t>(remote_ice_parameters_.size() - 1); | 309 : static_cast<uint32_t>(remote_ice_parameters_.size() - 1); |
| 348 } | 310 } |
| 349 | 311 |
| 312 // Sets the writable state, signaling if necessary. | |
| 313 void set_writable(bool writable); | |
| 314 // Sets the receiving state, signaling if necessary. | |
| 315 void set_receiving(bool receiving); | |
| 316 | |
| 317 std::string transport_name_; | |
| 318 int component_; | |
| 350 PortAllocator* allocator_; | 319 PortAllocator* allocator_; |
| 351 rtc::Thread* network_thread_; | 320 rtc::Thread* network_thread_; |
| 352 bool incoming_only_; | 321 bool incoming_only_; |
| 353 int error_; | 322 int error_; |
| 354 std::vector<std::unique_ptr<PortAllocatorSession>> allocator_sessions_; | 323 std::vector<std::unique_ptr<PortAllocatorSession>> allocator_sessions_; |
| 355 // |ports_| contains ports that are used to form new connections when | 324 // |ports_| contains ports that are used to form new connections when |
| 356 // new remote candidates are added. | 325 // new remote candidates are added. |
| 357 std::vector<PortInterface*> ports_; | 326 std::vector<PortInterface*> ports_; |
| 358 // |pruned_ports_| contains ports that have been removed from |ports_| and | 327 // |pruned_ports_| contains ports that have been removed from |ports_| and |
| 359 // are not being used to form new connections, but that aren't yet destroyed. | 328 // are not being used to form new connections, but that aren't yet destroyed. |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 380 IceParameters ice_parameters_; | 349 IceParameters ice_parameters_; |
| 381 std::vector<IceParameters> remote_ice_parameters_; | 350 std::vector<IceParameters> remote_ice_parameters_; |
| 382 IceMode remote_ice_mode_; | 351 IceMode remote_ice_mode_; |
| 383 IceRole ice_role_; | 352 IceRole ice_role_; |
| 384 uint64_t tiebreaker_; | 353 uint64_t tiebreaker_; |
| 385 IceGatheringState gathering_state_; | 354 IceGatheringState gathering_state_; |
| 386 | 355 |
| 387 int check_receiving_interval_; | 356 int check_receiving_interval_; |
| 388 int64_t last_ping_sent_ms_ = 0; | 357 int64_t last_ping_sent_ms_ = 0; |
| 389 int weak_ping_interval_ = WEAK_PING_INTERVAL; | 358 int weak_ping_interval_ = WEAK_PING_INTERVAL; |
| 390 TransportChannelState state_ = TransportChannelState::STATE_INIT; | 359 TransportState state_ = TransportState::STATE_INIT; |
| 391 IceConfig config_; | 360 IceConfig config_; |
| 392 int last_sent_packet_id_ = -1; // -1 indicates no packet was sent before. | 361 int last_sent_packet_id_ = -1; // -1 indicates no packet was sent before. |
| 393 bool started_pinging_ = false; | 362 bool started_pinging_ = false; |
| 394 // The value put in the "nomination" attribute for the next nominated | 363 // The value put in the "nomination" attribute for the next nominated |
| 395 // connection. A zero-value indicates the connection will not be nominated. | 364 // connection. A zero-value indicates the connection will not be nominated. |
| 396 uint32_t nomination_ = 0; | 365 uint32_t nomination_ = 0; |
| 366 bool receiving_ = false; | |
| 367 bool writable_ = false; | |
| 397 | 368 |
| 398 webrtc::MetricsObserverInterface* metrics_observer_ = nullptr; | 369 webrtc::MetricsObserverInterface* metrics_observer_ = nullptr; |
| 399 | 370 |
| 400 RTC_DISALLOW_COPY_AND_ASSIGN(P2PTransportChannel); | 371 RTC_DISALLOW_COPY_AND_ASSIGN(P2PTransportChannel); |
| 401 }; | 372 }; |
| 402 | 373 |
| 403 } // namespace cricket | 374 } // namespace cricket |
| 404 | 375 |
| 405 #endif // WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_ | 376 #endif // WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_ |
| OLD | NEW |