OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 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 #ifndef WEBRTC_P2P_BASE_TRANSPORTCONTROLLER_H_ | 11 #ifndef WEBRTC_P2P_BASE_TRANSPORTCONTROLLER_H_ |
12 #define WEBRTC_P2P_BASE_TRANSPORTCONTROLLER_H_ | 12 #define WEBRTC_P2P_BASE_TRANSPORTCONTROLLER_H_ |
13 | 13 |
14 #include <map> | 14 #include <map> |
15 #include <memory> | 15 #include <memory> |
16 #include <string> | 16 #include <string> |
17 #include <vector> | 17 #include <vector> |
18 | 18 |
19 #include "webrtc/base/asyncinvoker.h" | 19 #include "webrtc/base/asyncinvoker.h" |
20 #include "webrtc/base/sigslot.h" | 20 #include "webrtc/base/sigslot.h" |
21 #include "webrtc/base/sslstreamadapter.h" | 21 #include "webrtc/base/sslstreamadapter.h" |
22 #include "webrtc/p2p/base/candidate.h" | 22 #include "webrtc/p2p/base/candidate.h" |
23 #include "webrtc/p2p/base/transport.h" | 23 #include "webrtc/p2p/base/dtlstransportchannel.h" |
| 24 #include "webrtc/p2p/base/jseptransport.h" |
| 25 #include "webrtc/p2p/base/p2ptransportchannel.h" |
24 | 26 |
25 namespace rtc { | 27 namespace rtc { |
26 class Thread; | 28 class Thread; |
27 class PacketTransportInterface; | 29 class PacketTransportInterface; |
28 } | 30 } |
29 namespace webrtc { | 31 namespace webrtc { |
30 class MetricsObserverInterface; | 32 class MetricsObserverInterface; |
31 } | 33 } |
32 | 34 |
33 namespace cricket { | 35 namespace cricket { |
(...skipping 21 matching lines...) Expand all Loading... |
55 PortAllocator* port_allocator() const { return port_allocator_; } | 57 PortAllocator* port_allocator() const { return port_allocator_; } |
56 | 58 |
57 // Can only be set before transports are created. | 59 // Can only be set before transports are created. |
58 // TODO(deadbeef): Make this an argument to the constructor once BaseSession | 60 // TODO(deadbeef): Make this an argument to the constructor once BaseSession |
59 // and WebRtcSession are combined | 61 // and WebRtcSession are combined |
60 bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version); | 62 bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version); |
61 | 63 |
62 void SetIceConfig(const IceConfig& config); | 64 void SetIceConfig(const IceConfig& config); |
63 void SetIceRole(IceRole ice_role); | 65 void SetIceRole(IceRole ice_role); |
64 | 66 |
65 bool GetSslRole(const std::string& transport_name, rtc::SSLRole* role); | 67 bool GetSslRole(const std::string& transport_name, rtc::SSLRole* role) const; |
66 | 68 |
67 // Specifies the identity to use in this session. | 69 // Specifies the identity to use in this session. |
68 // Can only be called once. | 70 // Can only be called once. |
69 bool SetLocalCertificate( | 71 bool SetLocalCertificate( |
70 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate); | 72 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate); |
71 bool GetLocalCertificate( | 73 bool GetLocalCertificate( |
72 const std::string& transport_name, | 74 const std::string& transport_name, |
73 rtc::scoped_refptr<rtc::RTCCertificate>* certificate); | 75 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const; |
74 // Caller owns returned certificate | 76 // Caller owns returned certificate. This method mainly exists for stats |
| 77 // reporting. |
75 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate( | 78 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate( |
76 const std::string& transport_name); | 79 const std::string& transport_name) const; |
77 bool SetLocalTransportDescription(const std::string& transport_name, | 80 bool SetLocalTransportDescription(const std::string& transport_name, |
78 const TransportDescription& tdesc, | 81 const TransportDescription& tdesc, |
79 ContentAction action, | 82 ContentAction action, |
80 std::string* err); | 83 std::string* err); |
81 bool SetRemoteTransportDescription(const std::string& transport_name, | 84 bool SetRemoteTransportDescription(const std::string& transport_name, |
82 const TransportDescription& tdesc, | 85 const TransportDescription& tdesc, |
83 ContentAction action, | 86 ContentAction action, |
84 std::string* err); | 87 std::string* err); |
85 // Start gathering candidates for any new transports, or transports doing an | 88 // Start gathering candidates for any new transports, or transports doing an |
86 // ICE restart. | 89 // ICE restart. |
87 void MaybeStartGathering(); | 90 void MaybeStartGathering(); |
88 bool AddRemoteCandidates(const std::string& transport_name, | 91 bool AddRemoteCandidates(const std::string& transport_name, |
89 const Candidates& candidates, | 92 const Candidates& candidates, |
90 std::string* err); | 93 std::string* err); |
91 bool RemoveRemoteCandidates(const Candidates& candidates, std::string* err); | 94 bool RemoveRemoteCandidates(const Candidates& candidates, std::string* err); |
92 bool ReadyForRemoteCandidates(const std::string& transport_name); | 95 bool ReadyForRemoteCandidates(const std::string& transport_name) const; |
| 96 // TODO(deadbeef): GetStats isn't const because all the way down to |
| 97 // OpenSSLStreamAdapter, |
| 98 // GetSslCipherSuite and GetDtlsSrtpCryptoSuite are not const. Fix this. |
93 bool GetStats(const std::string& transport_name, TransportStats* stats); | 99 bool GetStats(const std::string& transport_name, TransportStats* stats); |
| 100 void SetMetricsObserver(webrtc::MetricsObserverInterface* metrics_observer); |
94 | 101 |
95 // Creates a channel if it doesn't exist. Otherwise, increments a reference | 102 // Creates a channel if it doesn't exist. Otherwise, increments a reference |
96 // count and returns an existing channel. | 103 // count and returns an existing channel. |
97 virtual TransportChannel* CreateTransportChannel_n( | 104 virtual TransportChannel* CreateTransportChannel_n( |
98 const std::string& transport_name, | 105 const std::string& transport_name, |
99 int component); | 106 int component); |
100 | 107 |
101 // Decrements a channel's reference count, and destroys the channel if | 108 // Decrements a channel's reference count, and destroys the channel if |
102 // nothing is referencing it. | 109 // nothing is referencing it. |
103 virtual void DestroyTransportChannel_n(const std::string& transport_name, | 110 virtual void DestroyTransportChannel_n(const std::string& transport_name, |
104 int component); | 111 int component); |
105 | 112 |
106 void use_quic() { quic_ = true; } | 113 void use_quic() { quic_ = true; } |
107 bool quic() const { return quic_; } | 114 bool quic() const { return quic_; } |
108 | 115 |
| 116 // TODO(deadbeef): Remove all for_testing methods! |
| 117 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate_for_testing() |
| 118 const { |
| 119 return certificate_; |
| 120 } |
| 121 std::vector<std::string> transport_names_for_testing(); |
| 122 std::vector<TransportChannelImpl*> channels_for_testing(); |
| 123 TransportChannelImpl* get_channel_for_testing( |
| 124 const std::string& transport_name, |
| 125 int component); |
| 126 |
109 // All of these signals are fired on the signalling thread. | 127 // All of these signals are fired on the signalling thread. |
110 | 128 |
111 // If any transport failed => failed, | 129 // If any transport failed => failed, |
112 // Else if all completed => completed, | 130 // Else if all completed => completed, |
113 // Else if all connected => connected, | 131 // Else if all connected => connected, |
114 // Else => connecting | 132 // Else => connecting |
115 sigslot::signal1<IceConnectionState> SignalConnectionState; | 133 sigslot::signal1<IceConnectionState> SignalConnectionState; |
116 | 134 |
117 // Receiving if any transport is receiving | 135 // Receiving if any transport is receiving |
118 sigslot::signal1<bool> SignalReceiving; | 136 sigslot::signal1<bool> SignalReceiving; |
119 | 137 |
120 // If all transports done gathering => complete, | 138 // If all transports done gathering => complete, |
121 // Else if any are gathering => gathering, | 139 // Else if any are gathering => gathering, |
122 // Else => new | 140 // Else => new |
123 sigslot::signal1<IceGatheringState> SignalGatheringState; | 141 sigslot::signal1<IceGatheringState> SignalGatheringState; |
124 | 142 |
125 // (transport_name, candidates) | 143 // (transport_name, candidates) |
126 sigslot::signal2<const std::string&, const Candidates&> | 144 sigslot::signal2<const std::string&, const Candidates&> |
127 SignalCandidatesGathered; | 145 SignalCandidatesGathered; |
128 | 146 |
129 sigslot::signal1<const Candidates&> SignalCandidatesRemoved; | 147 sigslot::signal1<const Candidates&> SignalCandidatesRemoved; |
130 | 148 |
131 // for unit test | |
132 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate_for_testing(); | |
133 | |
134 sigslot::signal1<rtc::SSLHandshakeError> SignalDtlsHandshakeError; | 149 sigslot::signal1<rtc::SSLHandshakeError> SignalDtlsHandshakeError; |
135 | 150 |
136 void SetMetricsObserver(webrtc::MetricsObserverInterface* metrics_observer); | |
137 | |
138 protected: | 151 protected: |
139 // Protected and virtual so we can override it in unit tests. | 152 // TODO(deadbeef): Get rid of these virtual methods. Used by |
140 virtual Transport* CreateTransport_n(const std::string& transport_name); | 153 // FakeTransportController currently, but FakeTransportController shouldn't |
141 | 154 // even be functioning by subclassing TransportController. |
142 // For unit tests | 155 virtual TransportChannelImpl* CreateIceTransportChannel_n( |
143 const std::map<std::string, Transport*>& transports() { return transports_; } | 156 const std::string& transport_name, |
144 Transport* GetTransport_n(const std::string& transport_name); | 157 int component); |
| 158 virtual TransportChannelImpl* CreateDtlsTransportChannel_n( |
| 159 const std::string& transport_name, |
| 160 int component, |
| 161 TransportChannelImpl* ice); |
145 | 162 |
146 private: | 163 private: |
147 void OnMessage(rtc::Message* pmsg) override; | 164 void OnMessage(rtc::Message* pmsg) override; |
148 | 165 |
149 // It's the Transport that's currently responsible for creating/destroying | 166 // This structure groups the DTLS and ICE channels, and helps keep track of |
150 // channels, but the TransportController keeps track of how many external | 167 // how many external objects (BaseChannels) reference each channel. |
151 // objects (BaseChannels) reference each channel. | |
152 struct RefCountedChannel { | 168 struct RefCountedChannel { |
153 RefCountedChannel() : impl_(nullptr), ref_(0) {} | 169 RefCountedChannel() = default; |
154 explicit RefCountedChannel(TransportChannelImpl* impl) | 170 // TODO(deadbeef): Change the types of |dtls| and |ice| to |
155 : impl_(impl), ref_(0) {} | 171 // DtlsTransportChannelWrapper and P2PTransportChannelWrapper, |
| 172 // once TransportChannelImpl is removed. |
| 173 explicit RefCountedChannel(TransportChannelImpl* dtls, |
| 174 TransportChannelImpl* ice) |
| 175 : ice_(ice), dtls_(dtls), ref_(0) {} |
156 | 176 |
157 void AddRef() { ++ref_; } | 177 void AddRef() { ++ref_; } |
158 void DecRef() { | 178 void DecRef() { |
159 ASSERT(ref_ > 0); | 179 ASSERT(ref_ > 0); |
160 --ref_; | 180 --ref_; |
161 } | 181 } |
162 int ref() const { return ref_; } | 182 int ref() const { return ref_; } |
163 | 183 |
164 TransportChannelImpl* get() const { return impl_; } | 184 // Currently, all ICE-related calls still go through this DTLS channel. But |
165 TransportChannelImpl* operator->() const { return impl_; } | 185 // that will change once we get rid of TransportChannelImpl, and the DTLS |
| 186 // channel interface no longer includes ICE-specific methods. |
| 187 const TransportChannelImpl* dtls() const { return dtls_.get(); } |
| 188 TransportChannelImpl* dtls() { return dtls_.get(); } |
| 189 const TransportChannelImpl* ice() const { return ice_.get(); } |
| 190 TransportChannelImpl* ice() { return ice_.get(); } |
166 | 191 |
167 private: | 192 private: |
168 TransportChannelImpl* impl_; | 193 std::unique_ptr<TransportChannelImpl> ice_; |
169 int ref_; | 194 std::unique_ptr<TransportChannelImpl> dtls_; |
| 195 int ref_ = 0; |
170 }; | 196 }; |
171 | 197 |
172 std::vector<RefCountedChannel>::iterator FindChannel_n( | 198 // Helper functions to get a channel or transport, or iterator to it (in case |
| 199 // it needs to be erased). |
| 200 std::vector<RefCountedChannel>::iterator GetChannelIterator_n( |
173 const std::string& transport_name, | 201 const std::string& transport_name, |
174 int component); | 202 int component); |
| 203 std::vector<RefCountedChannel>::const_iterator GetChannelIterator_n( |
| 204 const std::string& transport_name, |
| 205 int component) const; |
| 206 const JsepTransport* GetJsepTransport_n( |
| 207 const std::string& transport_name) const; |
| 208 JsepTransport* GetJsepTransport_n(const std::string& transport_name); |
| 209 const RefCountedChannel* GetChannel_n(const std::string& transport_name, |
| 210 int component) const; |
| 211 RefCountedChannel* GetChannel_n(const std::string& transport_name, |
| 212 int component); |
175 | 213 |
176 Transport* GetOrCreateTransport_n(const std::string& transport_name); | 214 JsepTransport* GetOrCreateJsepTransport_n(const std::string& transport_name); |
177 void DestroyTransport_n(const std::string& transport_name); | 215 void DestroyAllChannels_n(); |
178 void DestroyAllTransports_n(); | |
179 | 216 |
180 bool SetSslMaxProtocolVersion_n(rtc::SSLProtocolVersion version); | 217 bool SetSslMaxProtocolVersion_n(rtc::SSLProtocolVersion version); |
181 void SetIceConfig_n(const IceConfig& config); | 218 void SetIceConfig_n(const IceConfig& config); |
182 void SetIceRole_n(IceRole ice_role); | 219 void SetIceRole_n(IceRole ice_role); |
183 bool GetSslRole_n(const std::string& transport_name, rtc::SSLRole* role); | 220 bool GetSslRole_n(const std::string& transport_name, |
| 221 rtc::SSLRole* role) const; |
184 bool SetLocalCertificate_n( | 222 bool SetLocalCertificate_n( |
185 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate); | 223 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate); |
186 bool GetLocalCertificate_n( | 224 bool GetLocalCertificate_n( |
187 const std::string& transport_name, | 225 const std::string& transport_name, |
188 rtc::scoped_refptr<rtc::RTCCertificate>* certificate); | 226 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const; |
189 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate_n( | 227 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate_n( |
190 const std::string& transport_name); | 228 const std::string& transport_name) const; |
191 bool SetLocalTransportDescription_n(const std::string& transport_name, | 229 bool SetLocalTransportDescription_n(const std::string& transport_name, |
192 const TransportDescription& tdesc, | 230 const TransportDescription& tdesc, |
193 ContentAction action, | 231 ContentAction action, |
194 std::string* err); | 232 std::string* err); |
195 bool SetRemoteTransportDescription_n(const std::string& transport_name, | 233 bool SetRemoteTransportDescription_n(const std::string& transport_name, |
196 const TransportDescription& tdesc, | 234 const TransportDescription& tdesc, |
197 ContentAction action, | 235 ContentAction action, |
198 std::string* err); | 236 std::string* err); |
199 void MaybeStartGathering_n(); | 237 void MaybeStartGathering_n(); |
200 bool AddRemoteCandidates_n(const std::string& transport_name, | 238 bool AddRemoteCandidates_n(const std::string& transport_name, |
201 const Candidates& candidates, | 239 const Candidates& candidates, |
202 std::string* err); | 240 std::string* err); |
203 bool RemoveRemoteCandidates_n(const Candidates& candidates, std::string* err); | 241 bool RemoveRemoteCandidates_n(const Candidates& candidates, std::string* err); |
204 bool ReadyForRemoteCandidates_n(const std::string& transport_name); | 242 bool ReadyForRemoteCandidates_n(const std::string& transport_name) const; |
205 bool GetStats_n(const std::string& transport_name, TransportStats* stats); | 243 bool GetStats_n(const std::string& transport_name, TransportStats* stats); |
| 244 void SetMetricsObserver_n(webrtc::MetricsObserverInterface* metrics_observer); |
206 | 245 |
207 // Handlers for signals from Transport. | 246 // Handlers for signals from Transport. |
208 void OnChannelWritableState_n(rtc::PacketTransportInterface* transport); | 247 void OnChannelWritableState_n(rtc::PacketTransportInterface* transport); |
209 void OnChannelReceivingState_n(rtc::PacketTransportInterface* transport); | 248 void OnChannelReceivingState_n(rtc::PacketTransportInterface* transport); |
210 void OnChannelGatheringState_n(TransportChannelImpl* channel); | 249 void OnChannelGatheringState_n(TransportChannelImpl* channel); |
211 void OnChannelCandidateGathered_n(TransportChannelImpl* channel, | 250 void OnChannelCandidateGathered_n(TransportChannelImpl* channel, |
212 const Candidate& candidate); | 251 const Candidate& candidate); |
213 void OnChannelCandidatesRemoved(const Candidates& candidates); | 252 void OnChannelCandidatesRemoved(const Candidates& candidates); |
214 void OnChannelCandidatesRemoved_n(TransportChannelImpl* channel, | 253 void OnChannelCandidatesRemoved_n(TransportChannelImpl* channel, |
215 const Candidates& candidates); | 254 const Candidates& candidates); |
216 void OnChannelRoleConflict_n(TransportChannelImpl* channel); | 255 void OnChannelRoleConflict_n(TransportChannelImpl* channel); |
217 void OnChannelStateChanged_n(TransportChannelImpl* channel); | 256 void OnChannelStateChanged_n(TransportChannelImpl* channel); |
218 | 257 |
219 void UpdateAggregateStates_n(); | 258 void UpdateAggregateStates_n(); |
220 | 259 |
221 void OnDtlsHandshakeError(rtc::SSLHandshakeError error); | 260 void OnDtlsHandshakeError(rtc::SSLHandshakeError error); |
222 | 261 |
223 rtc::Thread* const signaling_thread_ = nullptr; | 262 rtc::Thread* const signaling_thread_ = nullptr; |
224 rtc::Thread* const network_thread_ = nullptr; | 263 rtc::Thread* const network_thread_ = nullptr; |
225 typedef std::map<std::string, Transport*> TransportMap; | 264 PortAllocator* const port_allocator_ = nullptr; |
226 TransportMap transports_; | |
227 | 265 |
| 266 std::map<std::string, std::unique_ptr<JsepTransport>> transports_; |
228 std::vector<RefCountedChannel> channels_; | 267 std::vector<RefCountedChannel> channels_; |
229 | 268 |
230 PortAllocator* const port_allocator_ = nullptr; | |
231 rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_12; | |
232 | |
233 // Aggregate state for TransportChannelImpls. | 269 // Aggregate state for TransportChannelImpls. |
234 IceConnectionState connection_state_ = kIceConnectionConnecting; | 270 IceConnectionState connection_state_ = kIceConnectionConnecting; |
235 bool receiving_ = false; | 271 bool receiving_ = false; |
236 IceGatheringState gathering_state_ = kIceGatheringNew; | 272 IceGatheringState gathering_state_ = kIceGatheringNew; |
237 | 273 |
238 // TODO(deadbeef): Move the fields below down to the transports themselves | |
239 IceConfig ice_config_; | 274 IceConfig ice_config_; |
240 IceRole ice_role_ = ICEROLE_CONTROLLING; | 275 IceRole ice_role_ = ICEROLE_CONTROLLING; |
241 bool redetermine_role_on_ice_restart_; | 276 bool redetermine_role_on_ice_restart_; |
242 uint64_t ice_tiebreaker_ = rtc::CreateRandomId64(); | 277 uint64_t ice_tiebreaker_ = rtc::CreateRandomId64(); |
| 278 rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_12; |
243 rtc::scoped_refptr<rtc::RTCCertificate> certificate_; | 279 rtc::scoped_refptr<rtc::RTCCertificate> certificate_; |
244 rtc::AsyncInvoker invoker_; | 280 rtc::AsyncInvoker invoker_; |
245 // True if QUIC is used instead of DTLS. | 281 // True if QUIC is used instead of DTLS. |
246 bool quic_ = false; | 282 bool quic_ = false; |
247 | 283 |
248 webrtc::MetricsObserverInterface* metrics_observer_ = nullptr; | 284 webrtc::MetricsObserverInterface* metrics_observer_ = nullptr; |
249 }; | 285 }; |
250 | 286 |
251 } // namespace cricket | 287 } // namespace cricket |
252 | 288 |
253 #endif // WEBRTC_P2P_BASE_TRANSPORTCONTROLLER_H_ | 289 #endif // WEBRTC_P2P_BASE_TRANSPORTCONTROLLER_H_ |
OLD | NEW |