blob: be8dd8af705ee5b431a7e26fb4b2f9aaa00153d9 [file] [log] [blame]
deadbeefcbecd352015-09-23 11:50:27 -07001/*
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
Zhi Huangb5261582017-09-29 10:51:43 -070011#ifndef PC_TRANSPORTCONTROLLER_H_
12#define PC_TRANSPORTCONTROLLER_H_
deadbeefcbecd352015-09-23 11:50:27 -070013
14#include <map>
jbauch555604a2016-04-26 03:13:22 -070015#include <memory>
deadbeefcbecd352015-09-23 11:50:27 -070016#include <string>
17#include <vector>
18
Patrik Höglunde2d6a062017-10-05 14:53:33 +020019#include "api/candidate.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "p2p/base/dtlstransport.h"
21#include "p2p/base/jseptransport.h"
22#include "p2p/base/p2ptransportchannel.h"
23#include "rtc_base/asyncinvoker.h"
24#include "rtc_base/constructormagic.h"
25#include "rtc_base/refcountedobject.h"
26#include "rtc_base/sigslot.h"
27#include "rtc_base/sslstreamadapter.h"
deadbeefcbecd352015-09-23 11:50:27 -070028
29namespace rtc {
30class Thread;
deadbeef5bd5ca32017-02-10 11:31:50 -080031class PacketTransportInternal;
Zhi Huangb5261582017-09-29 10:51:43 -070032} // namespace rtc
33
Honghai Zhangd93f50c2016-10-05 11:47:22 -070034namespace webrtc {
35class MetricsObserverInterface;
Zhi Huangb5261582017-09-29 10:51:43 -070036} // namespace webrtc
deadbeefcbecd352015-09-23 11:50:27 -070037
38namespace cricket {
39
40class TransportController : public sigslot::has_slots<>,
41 public rtc::MessageHandler {
42 public:
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -070043 // If |redetermine_role_on_ice_restart| is true, ICE role is redetermined
44 // upon setting a local transport description that indicates an ICE restart.
45 // For the constructor that doesn't take this parameter, it defaults to true.
deadbeef7914b8c2017-04-21 03:23:33 -070046 //
47 // |crypto_options| is used to determine if created DTLS transports negotiate
48 // GCM crypto suites or not.
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -070049 TransportController(rtc::Thread* signaling_thread,
50 rtc::Thread* network_thread,
51 PortAllocator* port_allocator,
deadbeef7914b8c2017-04-21 03:23:33 -070052 bool redetermine_role_on_ice_restart,
53 const rtc::CryptoOptions& crypto_options);
deadbeefcbecd352015-09-23 11:50:27 -070054
55 virtual ~TransportController();
56
57 rtc::Thread* signaling_thread() const { return signaling_thread_; }
Danil Chapovalov7f216b72016-05-12 09:20:31 +020058 rtc::Thread* network_thread() const { return network_thread_; }
deadbeefcbecd352015-09-23 11:50:27 -070059
60 PortAllocator* port_allocator() const { return port_allocator_; }
61
62 // Can only be set before transports are created.
63 // TODO(deadbeef): Make this an argument to the constructor once BaseSession
64 // and WebRtcSession are combined
65 bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version);
66
honghaiz1f429e32015-09-28 07:57:34 -070067 void SetIceConfig(const IceConfig& config);
deadbeefcbecd352015-09-23 11:50:27 -070068 void SetIceRole(IceRole ice_role);
69
deadbeefd1a38b52016-12-10 13:15:33 -080070 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
71 // set, offers should generate new ufrags/passwords until an ICE restart
72 // occurs.
73 void SetNeedsIceRestartFlag();
74 // Returns true if the ICE restart flag above was set, and no ICE restart has
75 // occurred yet for this transport (by applying a local description with
76 // changed ufrag/password). If the transport has been deleted as a result of
77 // bundling, returns false.
78 bool NeedsIceRestart(const std::string& transport_name) const;
79
deadbeef49f34fd2016-12-06 16:22:06 -080080 bool GetSslRole(const std::string& transport_name, rtc::SSLRole* role) const;
deadbeefcbecd352015-09-23 11:50:27 -070081
82 // Specifies the identity to use in this session.
83 // Can only be called once.
84 bool SetLocalCertificate(
85 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
86 bool GetLocalCertificate(
87 const std::string& transport_name,
deadbeef49f34fd2016-12-06 16:22:06 -080088 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const;
89 // Caller owns returned certificate. This method mainly exists for stats
90 // reporting.
jbauch555604a2016-04-26 03:13:22 -070091 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate(
deadbeef49f34fd2016-12-06 16:22:06 -080092 const std::string& transport_name) const;
deadbeefcbecd352015-09-23 11:50:27 -070093 bool SetLocalTransportDescription(const std::string& transport_name,
94 const TransportDescription& tdesc,
95 ContentAction action,
96 std::string* err);
97 bool SetRemoteTransportDescription(const std::string& transport_name,
98 const TransportDescription& tdesc,
99 ContentAction action,
100 std::string* err);
101 // Start gathering candidates for any new transports, or transports doing an
102 // ICE restart.
103 void MaybeStartGathering();
104 bool AddRemoteCandidates(const std::string& transport_name,
105 const Candidates& candidates,
106 std::string* err);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700107 bool RemoveRemoteCandidates(const Candidates& candidates, std::string* err);
deadbeef49f34fd2016-12-06 16:22:06 -0800108 bool ReadyForRemoteCandidates(const std::string& transport_name) const;
109 // TODO(deadbeef): GetStats isn't const because all the way down to
110 // OpenSSLStreamAdapter,
111 // GetSslCipherSuite and GetDtlsSrtpCryptoSuite are not const. Fix this.
deadbeefcbecd352015-09-23 11:50:27 -0700112 bool GetStats(const std::string& transport_name, TransportStats* stats);
deadbeef49f34fd2016-12-06 16:22:06 -0800113 void SetMetricsObserver(webrtc::MetricsObserverInterface* metrics_observer);
deadbeefcbecd352015-09-23 11:50:27 -0700114
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700115 // Creates a channel if it doesn't exist. Otherwise, increments a reference
116 // count and returns an existing channel.
zhihuangb2cdd932017-01-19 16:54:25 -0800117 DtlsTransportInternal* CreateDtlsTransport(const std::string& transport_name,
118 int component);
119 virtual DtlsTransportInternal* CreateDtlsTransport_n(
deadbeefcbecd352015-09-23 11:50:27 -0700120 const std::string& transport_name,
121 int component);
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700122
123 // Decrements a channel's reference count, and destroys the channel if
124 // nothing is referencing it.
zhihuangb2cdd932017-01-19 16:54:25 -0800125 virtual void DestroyDtlsTransport(const std::string& transport_name,
126 int component);
127 virtual void DestroyDtlsTransport_n(const std::string& transport_name,
128 int component);
deadbeefcbecd352015-09-23 11:50:27 -0700129
mikescarlette7748672016-04-29 20:20:54 -0700130 void use_quic() { quic_ = true; }
131 bool quic() const { return quic_; }
132
deadbeef49f34fd2016-12-06 16:22:06 -0800133 // TODO(deadbeef): Remove all for_testing methods!
134 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate_for_testing()
135 const {
136 return certificate_;
137 }
138 std::vector<std::string> transport_names_for_testing();
zhihuangb2cdd932017-01-19 16:54:25 -0800139 std::vector<DtlsTransportInternal*> channels_for_testing();
140 DtlsTransportInternal* get_channel_for_testing(
deadbeef49f34fd2016-12-06 16:22:06 -0800141 const std::string& transport_name,
142 int component);
143
deadbeefcbecd352015-09-23 11:50:27 -0700144 // All of these signals are fired on the signalling thread.
145
146 // If any transport failed => failed,
147 // Else if all completed => completed,
148 // Else if all connected => connected,
149 // Else => connecting
150 sigslot::signal1<IceConnectionState> SignalConnectionState;
151
152 // Receiving if any transport is receiving
153 sigslot::signal1<bool> SignalReceiving;
154
155 // If all transports done gathering => complete,
156 // Else if any are gathering => gathering,
157 // Else => new
158 sigslot::signal1<IceGatheringState> SignalGatheringState;
159
160 // (transport_name, candidates)
161 sigslot::signal2<const std::string&, const Candidates&>
162 SignalCandidatesGathered;
163
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700164 sigslot::signal1<const Candidates&> SignalCandidatesRemoved;
165
zhihuangd82eee02016-08-26 11:25:05 -0700166 sigslot::signal1<rtc::SSLHandshakeError> SignalDtlsHandshakeError;
167
deadbeefcbecd352015-09-23 11:50:27 -0700168 protected:
deadbeef49f34fd2016-12-06 16:22:06 -0800169 // TODO(deadbeef): Get rid of these virtual methods. Used by
170 // FakeTransportController currently, but FakeTransportController shouldn't
171 // even be functioning by subclassing TransportController.
zhihuangd06adf62017-01-12 15:58:31 -0800172 virtual IceTransportInternal* CreateIceTransportChannel_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800173 const std::string& transport_name,
174 int component);
zhihuangb2cdd932017-01-19 16:54:25 -0800175 virtual DtlsTransportInternal* CreateDtlsTransportChannel_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800176 const std::string& transport_name,
177 int component,
zhihuangd06adf62017-01-12 15:58:31 -0800178 IceTransportInternal* ice);
deadbeefcbecd352015-09-23 11:50:27 -0700179
180 private:
181 void OnMessage(rtc::Message* pmsg) override;
182
deadbeef62802a12016-12-13 16:38:36 -0800183 class ChannelPair;
184 typedef rtc::RefCountedObject<ChannelPair> RefCountedChannel;
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700185
deadbeef49f34fd2016-12-06 16:22:06 -0800186 // Helper functions to get a channel or transport, or iterator to it (in case
187 // it needs to be erased).
deadbeef62802a12016-12-13 16:38:36 -0800188 std::vector<RefCountedChannel*>::iterator GetChannelIterator_n(
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700189 const std::string& transport_name,
190 int component);
deadbeef62802a12016-12-13 16:38:36 -0800191 std::vector<RefCountedChannel*>::const_iterator GetChannelIterator_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800192 const std::string& transport_name,
193 int component) const;
deadbeefd1a38b52016-12-10 13:15:33 -0800194 const JsepTransport* GetJsepTransport(
deadbeef49f34fd2016-12-06 16:22:06 -0800195 const std::string& transport_name) const;
deadbeefd1a38b52016-12-10 13:15:33 -0800196 JsepTransport* GetJsepTransport(const std::string& transport_name);
deadbeef49f34fd2016-12-06 16:22:06 -0800197 const RefCountedChannel* GetChannel_n(const std::string& transport_name,
198 int component) const;
199 RefCountedChannel* GetChannel_n(const std::string& transport_name,
200 int component);
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700201
deadbeefd1a38b52016-12-10 13:15:33 -0800202 JsepTransport* GetOrCreateJsepTransport(const std::string& transport_name);
deadbeef49f34fd2016-12-06 16:22:06 -0800203 void DestroyAllChannels_n();
deadbeefcbecd352015-09-23 11:50:27 -0700204
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200205 bool SetSslMaxProtocolVersion_n(rtc::SSLProtocolVersion version);
206 void SetIceConfig_n(const IceConfig& config);
207 void SetIceRole_n(IceRole ice_role);
deadbeef49f34fd2016-12-06 16:22:06 -0800208 bool GetSslRole_n(const std::string& transport_name,
209 rtc::SSLRole* role) const;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200210 bool SetLocalCertificate_n(
deadbeefcbecd352015-09-23 11:50:27 -0700211 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200212 bool GetLocalCertificate_n(
deadbeefcbecd352015-09-23 11:50:27 -0700213 const std::string& transport_name,
deadbeef49f34fd2016-12-06 16:22:06 -0800214 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200215 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800216 const std::string& transport_name) const;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200217 bool SetLocalTransportDescription_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700218 const TransportDescription& tdesc,
219 ContentAction action,
220 std::string* err);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200221 bool SetRemoteTransportDescription_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700222 const TransportDescription& tdesc,
223 ContentAction action,
224 std::string* err);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200225 void MaybeStartGathering_n();
226 bool AddRemoteCandidates_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700227 const Candidates& candidates,
228 std::string* err);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200229 bool RemoveRemoteCandidates_n(const Candidates& candidates, std::string* err);
deadbeef49f34fd2016-12-06 16:22:06 -0800230 bool ReadyForRemoteCandidates_n(const std::string& transport_name) const;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200231 bool GetStats_n(const std::string& transport_name, TransportStats* stats);
deadbeef49f34fd2016-12-06 16:22:06 -0800232 void SetMetricsObserver_n(webrtc::MetricsObserverInterface* metrics_observer);
deadbeefcbecd352015-09-23 11:50:27 -0700233
234 // Handlers for signals from Transport.
deadbeef5bd5ca32017-02-10 11:31:50 -0800235 void OnChannelWritableState_n(rtc::PacketTransportInternal* transport);
236 void OnChannelReceivingState_n(rtc::PacketTransportInternal* transport);
zhihuangb2cdd932017-01-19 16:54:25 -0800237 void OnChannelGatheringState_n(IceTransportInternal* channel);
238 void OnChannelCandidateGathered_n(IceTransportInternal* channel,
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700239 const Candidate& candidate);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700240 void OnChannelCandidatesRemoved(const Candidates& candidates);
zhihuangb2cdd932017-01-19 16:54:25 -0800241 void OnChannelCandidatesRemoved_n(IceTransportInternal* channel,
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700242 const Candidates& candidates);
zhihuangb2cdd932017-01-19 16:54:25 -0800243 void OnChannelRoleConflict_n(IceTransportInternal* channel);
244 void OnChannelStateChanged_n(IceTransportInternal* channel);
deadbeefcbecd352015-09-23 11:50:27 -0700245
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200246 void UpdateAggregateStates_n();
deadbeefcbecd352015-09-23 11:50:27 -0700247
zhihuangd82eee02016-08-26 11:25:05 -0700248 void OnDtlsHandshakeError(rtc::SSLHandshakeError error);
249
deadbeefcbecd352015-09-23 11:50:27 -0700250 rtc::Thread* const signaling_thread_ = nullptr;
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200251 rtc::Thread* const network_thread_ = nullptr;
deadbeef57fd7262016-12-06 15:28:55 -0800252 PortAllocator* const port_allocator_ = nullptr;
deadbeef49f34fd2016-12-06 16:22:06 -0800253
254 std::map<std::string, std::unique_ptr<JsepTransport>> transports_;
deadbeef62802a12016-12-13 16:38:36 -0800255 std::vector<RefCountedChannel*> channels_;
deadbeef57fd7262016-12-06 15:28:55 -0800256
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700257 // Aggregate state for TransportChannelImpls.
deadbeefcbecd352015-09-23 11:50:27 -0700258 IceConnectionState connection_state_ = kIceConnectionConnecting;
259 bool receiving_ = false;
260 IceGatheringState gathering_state_ = kIceGatheringNew;
261
honghaiz1f429e32015-09-28 07:57:34 -0700262 IceConfig ice_config_;
deadbeefcbecd352015-09-23 11:50:27 -0700263 IceRole ice_role_ = ICEROLE_CONTROLLING;
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -0700264 bool redetermine_role_on_ice_restart_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200265 uint64_t ice_tiebreaker_ = rtc::CreateRandomId64();
deadbeef7914b8c2017-04-21 03:23:33 -0700266 rtc::CryptoOptions crypto_options_;
deadbeef49f34fd2016-12-06 16:22:06 -0800267 rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefcbecd352015-09-23 11:50:27 -0700268 rtc::scoped_refptr<rtc::RTCCertificate> certificate_;
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700269 rtc::AsyncInvoker invoker_;
mikescarlette7748672016-04-29 20:20:54 -0700270 // True if QUIC is used instead of DTLS.
271 bool quic_ = false;
Honghai Zhangd93f50c2016-10-05 11:47:22 -0700272
273 webrtc::MetricsObserverInterface* metrics_observer_ = nullptr;
deadbeef62802a12016-12-13 16:38:36 -0800274
275 RTC_DISALLOW_COPY_AND_ASSIGN(TransportController);
deadbeefcbecd352015-09-23 11:50:27 -0700276};
277
278} // namespace cricket
279
Zhi Huangb5261582017-09-29 10:51:43 -0700280#endif // PC_TRANSPORTCONTROLLER_H_