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