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