| 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" | |
| 30 #include "webrtc/base/constructormagic.h" | 29 #include "webrtc/base/constructormagic.h" |
| 31 #include "webrtc/base/sigslot.h" | |
| 32 #include "webrtc/p2p/base/candidate.h" | 30 #include "webrtc/p2p/base/candidate.h" |
| 33 #include "webrtc/p2p/base/candidatepairinterface.h" | 31 #include "webrtc/p2p/base/candidatepairinterface.h" |
| 34 #include "webrtc/p2p/base/icetransportinternal.h" | |
| 35 #include "webrtc/p2p/base/portallocator.h" | 32 #include "webrtc/p2p/base/portallocator.h" |
| 36 #include "webrtc/p2p/base/portinterface.h" | 33 #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 IceTransportInternal, | 63 class P2PTransportChannel : public TransportChannelImpl, |
| 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 IceTransportState GetState() const override; | 72 TransportChannelState 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_; } | |
| 77 void SetIceRole(IceRole role) override; | 73 void SetIceRole(IceRole role) override; |
| 78 IceRole GetIceRole() const override { return ice_role_; } | 74 IceRole GetIceRole() const override { return ice_role_; } |
| 79 void SetIceTiebreaker(uint64_t tiebreaker) override; | 75 void SetIceTiebreaker(uint64_t tiebreaker) override; |
| 80 void SetIceParameters(const IceParameters& ice_params) override; | 76 void SetIceParameters(const IceParameters& ice_params) override; |
| 81 void SetRemoteIceParameters(const IceParameters& ice_params) override; | 77 void SetRemoteIceParameters(const IceParameters& ice_params) override; |
| 82 void SetRemoteIceMode(IceMode mode) override; | 78 void SetRemoteIceMode(IceMode mode) override; |
| 83 // TODO(deadbeef): Deprecated. Remove when Chromium's | 79 // TODO(deadbeef): Deprecated. Remove when Chromium's |
| 84 // IceTransportChannel does not depend on this. | 80 // IceTransportChannel does not depend on this. |
| 85 void Connect() {} | 81 void Connect() {} |
| 86 void MaybeStartGathering() override; | 82 void MaybeStartGathering() override; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 115 const Connection* selected_connection() const { return selected_connection_; } | 111 const Connection* selected_connection() const { return selected_connection_; } |
| 116 void set_incoming_only(bool value) { incoming_only_ = value; } | 112 void set_incoming_only(bool value) { incoming_only_ = value; } |
| 117 | 113 |
| 118 // Note: These are only for testing purpose. | 114 // Note: These are only for testing purpose. |
| 119 // |ports_| and |pruned_ports| should not be changed from outside. | 115 // |ports_| and |pruned_ports| should not be changed from outside. |
| 120 const std::vector<PortInterface*>& ports() { return ports_; } | 116 const std::vector<PortInterface*>& ports() { return ports_; } |
| 121 const std::vector<PortInterface*>& pruned_ports() { return pruned_ports_; } | 117 const std::vector<PortInterface*>& pruned_ports() { return pruned_ports_; } |
| 122 | 118 |
| 123 IceMode remote_ice_mode() const { return remote_ice_mode_; } | 119 IceMode remote_ice_mode() const { return remote_ice_mode_; } |
| 124 | 120 |
| 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 |
| 125 void PruneAllPorts(); | 172 void PruneAllPorts(); |
| 126 int receiving_timeout() const { return config_.receiving_timeout; } | 173 int receiving_timeout() const { return config_.receiving_timeout; } |
| 127 int check_receiving_interval() const { return check_receiving_interval_; } | 174 int check_receiving_interval() const { return check_receiving_interval_; } |
| 128 | 175 |
| 129 // Helper method used only in unittest. | 176 // Helper method used only in unittest. |
| 130 rtc::DiffServCodePoint DefaultDscpValue() const; | 177 rtc::DiffServCodePoint DefaultDscpValue() const; |
| 131 | 178 |
| 132 // Public for unit tests. | 179 // Public for unit tests. |
| 133 Connection* FindNextPingableConnection(); | 180 Connection* FindNextPingableConnection(); |
| 134 void MarkConnectionPinged(Connection* conn); | 181 void MarkConnectionPinged(Connection* conn); |
| 135 | 182 |
| 136 // Public for unit tests. | 183 // Public for unit tests. |
| 137 const std::vector<Connection*>& connections() const { return connections_; } | 184 const std::vector<Connection*>& connections() const { return connections_; } |
| 138 | 185 |
| 139 // Public for unit tests. | 186 // Public for unit tests. |
| 140 PortAllocatorSession* allocator_session() { | 187 PortAllocatorSession* allocator_session() { |
| 141 return allocator_sessions_.back().get(); | 188 return allocator_sessions_.back().get(); |
| 142 } | 189 } |
| 143 | 190 |
| 144 // Public for unit tests. | 191 // Public for unit tests. |
| 145 const std::vector<RemoteCandidate>& remote_candidates() const { | 192 const std::vector<RemoteCandidate>& remote_candidates() const { |
| 146 return remote_candidates_; | 193 return remote_candidates_; |
| 147 } | 194 } |
| 148 | 195 |
| 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 } | |
| 157 | |
| 158 private: | 196 private: |
| 159 rtc::Thread* thread() const { return network_thread_; } | 197 rtc::Thread* thread() const { return network_thread_; } |
| 160 bool IsGettingPorts() { return allocator_session()->IsGettingPorts(); } | 198 bool IsGettingPorts() { return allocator_session()->IsGettingPorts(); } |
| 161 | 199 |
| 162 // A transport channel is weak if the current best connection is either | 200 // A transport channel is weak if the current best connection is either |
| 163 // not receiving or not writable, or if there is no best connection at all. | 201 // not receiving or not writable, or if there is no best connection at all. |
| 164 bool weak() const; | 202 bool weak() const; |
| 165 // Returns true if it's possible to send packets on |connection|. | 203 // Returns true if it's possible to send packets on |connection|. |
| 166 bool ReadyToSend(Connection* connection) const; | 204 bool ReadyToSend(Connection* connection) const; |
| 167 void UpdateConnectionStates(); | 205 void UpdateConnectionStates(); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 194 rtc::Optional<int64_t> receiving_unchanged_threshold, | 232 rtc::Optional<int64_t> receiving_unchanged_threshold, |
| 195 bool* missed_receiving_unchanged_threshold) const; | 233 bool* missed_receiving_unchanged_threshold) const; |
| 196 | 234 |
| 197 bool PresumedWritable(const cricket::Connection* conn) const; | 235 bool PresumedWritable(const cricket::Connection* conn) const; |
| 198 | 236 |
| 199 void SortConnectionsAndUpdateState(); | 237 void SortConnectionsAndUpdateState(); |
| 200 void SwitchSelectedConnection(Connection* conn); | 238 void SwitchSelectedConnection(Connection* conn); |
| 201 void UpdateState(); | 239 void UpdateState(); |
| 202 void HandleAllTimedOut(); | 240 void HandleAllTimedOut(); |
| 203 void MaybeStopPortAllocatorSessions(); | 241 void MaybeStopPortAllocatorSessions(); |
| 204 IceTransportState ComputeState() const; | 242 TransportChannelState ComputeState() const; |
| 205 | 243 |
| 206 Connection* GetBestConnectionOnNetwork(rtc::Network* network) const; | 244 Connection* GetBestConnectionOnNetwork(rtc::Network* network) const; |
| 207 bool CreateConnections(const Candidate& remote_candidate, | 245 bool CreateConnections(const Candidate& remote_candidate, |
| 208 PortInterface* origin_port); | 246 PortInterface* origin_port); |
| 209 bool CreateConnection(PortInterface* port, | 247 bool CreateConnection(PortInterface* port, |
| 210 const Candidate& remote_candidate, | 248 const Candidate& remote_candidate, |
| 211 PortInterface* origin_port); | 249 PortInterface* origin_port); |
| 212 bool FindConnection(cricket::Connection* connection) const; | 250 bool FindConnection(cricket::Connection* connection) const; |
| 213 | 251 |
| 214 uint32_t GetRemoteCandidateGeneration(const Candidate& candidate); | 252 uint32_t GetRemoteCandidateGeneration(const Candidate& candidate); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 const IceParameters* FindRemoteIceFromUfrag(const std::string& ufrag, | 340 const IceParameters* FindRemoteIceFromUfrag(const std::string& ufrag, |
| 303 uint32_t* generation); | 341 uint32_t* generation); |
| 304 // Returns the index of the latest remote ICE parameters, or 0 if no remote | 342 // Returns the index of the latest remote ICE parameters, or 0 if no remote |
| 305 // ICE parameters have been received. | 343 // ICE parameters have been received. |
| 306 uint32_t remote_ice_generation() { | 344 uint32_t remote_ice_generation() { |
| 307 return remote_ice_parameters_.empty() | 345 return remote_ice_parameters_.empty() |
| 308 ? 0 | 346 ? 0 |
| 309 : static_cast<uint32_t>(remote_ice_parameters_.size() - 1); | 347 : static_cast<uint32_t>(remote_ice_parameters_.size() - 1); |
| 310 } | 348 } |
| 311 | 349 |
| 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_; | |
| 319 PortAllocator* allocator_; | 350 PortAllocator* allocator_; |
| 320 rtc::Thread* network_thread_; | 351 rtc::Thread* network_thread_; |
| 321 bool incoming_only_; | 352 bool incoming_only_; |
| 322 int error_; | 353 int error_; |
| 323 std::vector<std::unique_ptr<PortAllocatorSession>> allocator_sessions_; | 354 std::vector<std::unique_ptr<PortAllocatorSession>> allocator_sessions_; |
| 324 // |ports_| contains ports that are used to form new connections when | 355 // |ports_| contains ports that are used to form new connections when |
| 325 // new remote candidates are added. | 356 // new remote candidates are added. |
| 326 std::vector<PortInterface*> ports_; | 357 std::vector<PortInterface*> ports_; |
| 327 // |pruned_ports_| contains ports that have been removed from |ports_| and | 358 // |pruned_ports_| contains ports that have been removed from |ports_| and |
| 328 // are not being used to form new connections, but that aren't yet destroyed. | 359 // are not being used to form new connections, but that aren't yet destroyed. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 349 IceParameters ice_parameters_; | 380 IceParameters ice_parameters_; |
| 350 std::vector<IceParameters> remote_ice_parameters_; | 381 std::vector<IceParameters> remote_ice_parameters_; |
| 351 IceMode remote_ice_mode_; | 382 IceMode remote_ice_mode_; |
| 352 IceRole ice_role_; | 383 IceRole ice_role_; |
| 353 uint64_t tiebreaker_; | 384 uint64_t tiebreaker_; |
| 354 IceGatheringState gathering_state_; | 385 IceGatheringState gathering_state_; |
| 355 | 386 |
| 356 int check_receiving_interval_; | 387 int check_receiving_interval_; |
| 357 int64_t last_ping_sent_ms_ = 0; | 388 int64_t last_ping_sent_ms_ = 0; |
| 358 int weak_ping_interval_ = WEAK_PING_INTERVAL; | 389 int weak_ping_interval_ = WEAK_PING_INTERVAL; |
| 359 IceTransportState state_ = IceTransportState::STATE_INIT; | 390 TransportChannelState state_ = TransportChannelState::STATE_INIT; |
| 360 IceConfig config_; | 391 IceConfig config_; |
| 361 int last_sent_packet_id_ = -1; // -1 indicates no packet was sent before. | 392 int last_sent_packet_id_ = -1; // -1 indicates no packet was sent before. |
| 362 bool started_pinging_ = false; | 393 bool started_pinging_ = false; |
| 363 // The value put in the "nomination" attribute for the next nominated | 394 // The value put in the "nomination" attribute for the next nominated |
| 364 // connection. A zero-value indicates the connection will not be nominated. | 395 // connection. A zero-value indicates the connection will not be nominated. |
| 365 uint32_t nomination_ = 0; | 396 uint32_t nomination_ = 0; |
| 366 bool receiving_ = false; | |
| 367 bool writable_ = false; | |
| 368 | 397 |
| 369 webrtc::MetricsObserverInterface* metrics_observer_ = nullptr; | 398 webrtc::MetricsObserverInterface* metrics_observer_ = nullptr; |
| 370 | 399 |
| 371 RTC_DISALLOW_COPY_AND_ASSIGN(P2PTransportChannel); | 400 RTC_DISALLOW_COPY_AND_ASSIGN(P2PTransportChannel); |
| 372 }; | 401 }; |
| 373 | 402 |
| 374 } // namespace cricket | 403 } // namespace cricket |
| 375 | 404 |
| 376 #endif // WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_ | 405 #endif // WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_ |
| OLD | NEW |