deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
| 11 | #ifndef WEBRTC_P2P_BASE_TRANSPORTCONTROLLER_H_ |
| 12 | #define WEBRTC_P2P_BASE_TRANSPORTCONTROLLER_H_ |
| 13 | |
| 14 | #include <map> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 15 | #include <memory> |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 19 | #include "webrtc/base/asyncinvoker.h" |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 20 | #include "webrtc/base/sigslot.h" |
| 21 | #include "webrtc/base/sslstreamadapter.h" |
| 22 | #include "webrtc/p2p/base/candidate.h" |
| 23 | #include "webrtc/p2p/base/transport.h" |
| 24 | |
| 25 | namespace rtc { |
| 26 | class Thread; |
| 27 | } |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame^] | 28 | namespace webrtc { |
| 29 | class MetricsObserverInterface; |
| 30 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 31 | |
| 32 | namespace cricket { |
| 33 | |
| 34 | class TransportController : public sigslot::has_slots<>, |
| 35 | public rtc::MessageHandler { |
| 36 | public: |
Taylor Brandstetter | f0bb360 | 2016-08-26 20:59:24 -0700 | [diff] [blame] | 37 | // If |redetermine_role_on_ice_restart| is true, ICE role is redetermined |
| 38 | // upon setting a local transport description that indicates an ICE restart. |
| 39 | // For the constructor that doesn't take this parameter, it defaults to true. |
| 40 | TransportController(rtc::Thread* signaling_thread, |
| 41 | rtc::Thread* network_thread, |
| 42 | PortAllocator* port_allocator, |
| 43 | bool redetermine_role_on_ice_restart); |
| 44 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 45 | TransportController(rtc::Thread* signaling_thread, |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 46 | rtc::Thread* network_thread, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 47 | PortAllocator* port_allocator); |
| 48 | |
| 49 | virtual ~TransportController(); |
| 50 | |
| 51 | rtc::Thread* signaling_thread() const { return signaling_thread_; } |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 52 | rtc::Thread* network_thread() const { return network_thread_; } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 53 | |
| 54 | PortAllocator* port_allocator() const { return port_allocator_; } |
| 55 | |
| 56 | // Can only be set before transports are created. |
| 57 | // TODO(deadbeef): Make this an argument to the constructor once BaseSession |
| 58 | // and WebRtcSession are combined |
| 59 | bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version); |
| 60 | |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 61 | void SetIceConfig(const IceConfig& config); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 62 | void SetIceRole(IceRole ice_role); |
| 63 | |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 64 | bool GetSslRole(const std::string& transport_name, rtc::SSLRole* role); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 65 | |
| 66 | // Specifies the identity to use in this session. |
| 67 | // Can only be called once. |
| 68 | bool SetLocalCertificate( |
| 69 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate); |
| 70 | bool GetLocalCertificate( |
| 71 | const std::string& transport_name, |
| 72 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate); |
| 73 | // Caller owns returned certificate |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 74 | std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate( |
kwiberg | b4d01c4 | 2016-04-06 05:15:06 -0700 | [diff] [blame] | 75 | const std::string& transport_name); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 76 | bool SetLocalTransportDescription(const std::string& transport_name, |
| 77 | const TransportDescription& tdesc, |
| 78 | ContentAction action, |
| 79 | std::string* err); |
| 80 | bool SetRemoteTransportDescription(const std::string& transport_name, |
| 81 | const TransportDescription& tdesc, |
| 82 | ContentAction action, |
| 83 | std::string* err); |
| 84 | // Start gathering candidates for any new transports, or transports doing an |
| 85 | // ICE restart. |
| 86 | void MaybeStartGathering(); |
| 87 | bool AddRemoteCandidates(const std::string& transport_name, |
| 88 | const Candidates& candidates, |
| 89 | std::string* err); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 90 | bool RemoveRemoteCandidates(const Candidates& candidates, std::string* err); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 91 | bool ReadyForRemoteCandidates(const std::string& transport_name); |
| 92 | bool GetStats(const std::string& transport_name, TransportStats* stats); |
| 93 | |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 94 | // Creates a channel if it doesn't exist. Otherwise, increments a reference |
| 95 | // count and returns an existing channel. |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 96 | virtual TransportChannel* CreateTransportChannel_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 97 | const std::string& transport_name, |
| 98 | int component); |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 99 | |
| 100 | // Decrements a channel's reference count, and destroys the channel if |
| 101 | // nothing is referencing it. |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 102 | virtual void DestroyTransportChannel_n(const std::string& transport_name, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 103 | int component); |
| 104 | |
mikescarlett | e774867 | 2016-04-29 20:20:54 -0700 | [diff] [blame] | 105 | void use_quic() { quic_ = true; } |
| 106 | bool quic() const { return quic_; } |
| 107 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 108 | // All of these signals are fired on the signalling thread. |
| 109 | |
| 110 | // If any transport failed => failed, |
| 111 | // Else if all completed => completed, |
| 112 | // Else if all connected => connected, |
| 113 | // Else => connecting |
| 114 | sigslot::signal1<IceConnectionState> SignalConnectionState; |
| 115 | |
| 116 | // Receiving if any transport is receiving |
| 117 | sigslot::signal1<bool> SignalReceiving; |
| 118 | |
| 119 | // If all transports done gathering => complete, |
| 120 | // Else if any are gathering => gathering, |
| 121 | // Else => new |
| 122 | sigslot::signal1<IceGatheringState> SignalGatheringState; |
| 123 | |
| 124 | // (transport_name, candidates) |
| 125 | sigslot::signal2<const std::string&, const Candidates&> |
| 126 | SignalCandidatesGathered; |
| 127 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 128 | sigslot::signal1<const Candidates&> SignalCandidatesRemoved; |
| 129 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 130 | // for unit test |
| 131 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate_for_testing(); |
| 132 | |
zhihuang | d82eee0 | 2016-08-26 11:25:05 -0700 | [diff] [blame] | 133 | sigslot::signal1<rtc::SSLHandshakeError> SignalDtlsHandshakeError; |
| 134 | |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame^] | 135 | void SetMetricsObserver(webrtc::MetricsObserverInterface* metrics_observer); |
| 136 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 137 | protected: |
| 138 | // Protected and virtual so we can override it in unit tests. |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 139 | virtual Transport* CreateTransport_n(const std::string& transport_name); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 140 | |
| 141 | // For unit tests |
| 142 | const std::map<std::string, Transport*>& transports() { return transports_; } |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 143 | Transport* GetTransport_n(const std::string& transport_name); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 144 | |
| 145 | private: |
| 146 | void OnMessage(rtc::Message* pmsg) override; |
| 147 | |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 148 | // It's the Transport that's currently responsible for creating/destroying |
| 149 | // channels, but the TransportController keeps track of how many external |
| 150 | // objects (BaseChannels) reference each channel. |
| 151 | struct RefCountedChannel { |
| 152 | RefCountedChannel() : impl_(nullptr), ref_(0) {} |
| 153 | explicit RefCountedChannel(TransportChannelImpl* impl) |
| 154 | : impl_(impl), ref_(0) {} |
| 155 | |
| 156 | void AddRef() { ++ref_; } |
| 157 | void DecRef() { |
| 158 | ASSERT(ref_ > 0); |
| 159 | --ref_; |
| 160 | } |
| 161 | int ref() const { return ref_; } |
| 162 | |
| 163 | TransportChannelImpl* get() const { return impl_; } |
| 164 | TransportChannelImpl* operator->() const { return impl_; } |
| 165 | |
| 166 | private: |
| 167 | TransportChannelImpl* impl_; |
| 168 | int ref_; |
| 169 | }; |
| 170 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 171 | std::vector<RefCountedChannel>::iterator FindChannel_n( |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 172 | const std::string& transport_name, |
| 173 | int component); |
| 174 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 175 | Transport* GetOrCreateTransport_n(const std::string& transport_name); |
| 176 | void DestroyTransport_n(const std::string& transport_name); |
| 177 | void DestroyAllTransports_n(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 178 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 179 | bool SetSslMaxProtocolVersion_n(rtc::SSLProtocolVersion version); |
| 180 | void SetIceConfig_n(const IceConfig& config); |
| 181 | void SetIceRole_n(IceRole ice_role); |
| 182 | bool GetSslRole_n(const std::string& transport_name, rtc::SSLRole* role); |
| 183 | bool SetLocalCertificate_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 184 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate); |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 185 | bool GetLocalCertificate_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 186 | const std::string& transport_name, |
| 187 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate); |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 188 | std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate_n( |
kwiberg | b4d01c4 | 2016-04-06 05:15:06 -0700 | [diff] [blame] | 189 | const std::string& transport_name); |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 190 | bool SetLocalTransportDescription_n(const std::string& transport_name, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 191 | const TransportDescription& tdesc, |
| 192 | ContentAction action, |
| 193 | std::string* err); |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 194 | bool SetRemoteTransportDescription_n(const std::string& transport_name, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 195 | const TransportDescription& tdesc, |
| 196 | ContentAction action, |
| 197 | std::string* err); |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 198 | void MaybeStartGathering_n(); |
| 199 | bool AddRemoteCandidates_n(const std::string& transport_name, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 200 | const Candidates& candidates, |
| 201 | std::string* err); |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 202 | bool RemoveRemoteCandidates_n(const Candidates& candidates, std::string* err); |
| 203 | bool ReadyForRemoteCandidates_n(const std::string& transport_name); |
| 204 | bool GetStats_n(const std::string& transport_name, TransportStats* stats); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 205 | |
| 206 | // Handlers for signals from Transport. |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 207 | void OnChannelWritableState_n(TransportChannel* channel); |
| 208 | void OnChannelReceivingState_n(TransportChannel* channel); |
| 209 | void OnChannelGatheringState_n(TransportChannelImpl* channel); |
| 210 | void OnChannelCandidateGathered_n(TransportChannelImpl* channel, |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 211 | const Candidate& candidate); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 212 | void OnChannelCandidatesRemoved(const Candidates& candidates); |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 213 | void OnChannelCandidatesRemoved_n(TransportChannelImpl* channel, |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 214 | const Candidates& candidates); |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 215 | void OnChannelRoleConflict_n(TransportChannelImpl* channel); |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 216 | void OnChannelStateChanged_n(TransportChannelImpl* channel); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 217 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 218 | void UpdateAggregateStates_n(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 219 | |
zhihuang | d82eee0 | 2016-08-26 11:25:05 -0700 | [diff] [blame] | 220 | void OnDtlsHandshakeError(rtc::SSLHandshakeError error); |
| 221 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 222 | rtc::Thread* const signaling_thread_ = nullptr; |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 223 | rtc::Thread* const network_thread_ = nullptr; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 224 | typedef std::map<std::string, Transport*> TransportMap; |
| 225 | TransportMap transports_; |
| 226 | |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 227 | std::vector<RefCountedChannel> channels_; |
| 228 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 229 | PortAllocator* const port_allocator_ = nullptr; |
Guo-wei Shieh | a7446d2 | 2016-01-11 15:27:03 -0800 | [diff] [blame] | 230 | rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_12; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 231 | |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 232 | // Aggregate state for TransportChannelImpls. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 233 | IceConnectionState connection_state_ = kIceConnectionConnecting; |
| 234 | bool receiving_ = false; |
| 235 | IceGatheringState gathering_state_ = kIceGatheringNew; |
| 236 | |
| 237 | // TODO(deadbeef): Move the fields below down to the transports themselves |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 238 | IceConfig ice_config_; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 239 | IceRole ice_role_ = ICEROLE_CONTROLLING; |
Taylor Brandstetter | f0bb360 | 2016-08-26 20:59:24 -0700 | [diff] [blame] | 240 | bool redetermine_role_on_ice_restart_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 241 | uint64_t ice_tiebreaker_ = rtc::CreateRandomId64(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 242 | rtc::scoped_refptr<rtc::RTCCertificate> certificate_; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 243 | rtc::AsyncInvoker invoker_; |
mikescarlett | e774867 | 2016-04-29 20:20:54 -0700 | [diff] [blame] | 244 | // True if QUIC is used instead of DTLS. |
| 245 | bool quic_ = false; |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame^] | 246 | |
| 247 | webrtc::MetricsObserverInterface* metrics_observer_ = nullptr; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | } // namespace cricket |
| 251 | |
| 252 | #endif // WEBRTC_P2P_BASE_TRANSPORTCONTROLLER_H_ |