blob: 7cf9bbd3bded4d3206e4c171419c66cb75b2b899 [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#include "pc/transportcontroller.h"
deadbeefcbecd352015-09-23 11:50:27 -070012
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -070013#include <algorithm>
jbauch555604a2016-04-26 03:13:22 -070014#include <memory>
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -070015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "p2p/base/port.h"
17#include "rtc_base/bind.h"
18#include "rtc_base/checks.h"
19#include "rtc_base/thread.h"
deadbeefcbecd352015-09-23 11:50:27 -070020
deadbeef62802a12016-12-13 16:38:36 -080021namespace {
deadbeefcbecd352015-09-23 11:50:27 -070022
23enum {
24 MSG_ICECONNECTIONSTATE,
25 MSG_RECEIVING,
26 MSG_ICEGATHERINGSTATE,
27 MSG_CANDIDATESGATHERED,
28};
29
30struct CandidatesData : public rtc::MessageData {
31 CandidatesData(const std::string& transport_name,
deadbeef62802a12016-12-13 16:38:36 -080032 const cricket::Candidates& candidates)
deadbeefcbecd352015-09-23 11:50:27 -070033 : transport_name(transport_name), candidates(candidates) {}
34
35 std::string transport_name;
deadbeef62802a12016-12-13 16:38:36 -080036 cricket::Candidates candidates;
37};
38
Zhi Huangb5261582017-09-29 10:51:43 -070039} // namespace
deadbeef62802a12016-12-13 16:38:36 -080040
41namespace cricket {
42
43// This class groups the DTLS and ICE channels, and helps keep track of
44// how many external objects (BaseChannels) reference each channel.
45class TransportController::ChannelPair {
46 public:
47 // TODO(deadbeef): Change the types of |dtls| and |ice| to
zhihuangb2cdd932017-01-19 16:54:25 -080048 // DtlsTransport and P2PTransportChannelWrapper, once TransportChannelImpl is
49 // removed.
50 ChannelPair(DtlsTransportInternal* dtls, IceTransportInternal* ice)
deadbeef62802a12016-12-13 16:38:36 -080051 : ice_(ice), dtls_(dtls) {}
52
53 // Currently, all ICE-related calls still go through this DTLS channel. But
54 // that will change once we get rid of TransportChannelImpl, and the DTLS
55 // channel interface no longer includes ICE-specific methods.
zhihuangb2cdd932017-01-19 16:54:25 -080056 const DtlsTransportInternal* dtls() const { return dtls_.get(); }
57 DtlsTransportInternal* dtls() { return dtls_.get(); }
zhihuangd06adf62017-01-12 15:58:31 -080058 const IceTransportInternal* ice() const { return ice_.get(); }
59 IceTransportInternal* ice() { return ice_.get(); }
deadbeef62802a12016-12-13 16:38:36 -080060
61 private:
zhihuangd06adf62017-01-12 15:58:31 -080062 std::unique_ptr<IceTransportInternal> ice_;
zhihuangb2cdd932017-01-19 16:54:25 -080063 std::unique_ptr<DtlsTransportInternal> dtls_;
deadbeef62802a12016-12-13 16:38:36 -080064
65 RTC_DISALLOW_COPY_AND_ASSIGN(ChannelPair);
deadbeefcbecd352015-09-23 11:50:27 -070066};
67
deadbeef7914b8c2017-04-21 03:23:33 -070068TransportController::TransportController(
69 rtc::Thread* signaling_thread,
70 rtc::Thread* network_thread,
71 PortAllocator* port_allocator,
72 bool redetermine_role_on_ice_restart,
73 const rtc::CryptoOptions& crypto_options)
deadbeefcbecd352015-09-23 11:50:27 -070074 : signaling_thread_(signaling_thread),
Danil Chapovalov7f216b72016-05-12 09:20:31 +020075 network_thread_(network_thread),
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -070076 port_allocator_(port_allocator),
deadbeef7914b8c2017-04-21 03:23:33 -070077 redetermine_role_on_ice_restart_(redetermine_role_on_ice_restart),
78 crypto_options_(crypto_options) {}
deadbeefcbecd352015-09-23 11:50:27 -070079
80TransportController::~TransportController() {
deadbeef49f34fd2016-12-06 16:22:06 -080081 // Channel destructors may try to send packets, so this needs to happen on
82 // the network thread.
Danil Chapovalov7f216b72016-05-12 09:20:31 +020083 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070084 RTC_FROM_HERE,
deadbeef49f34fd2016-12-06 16:22:06 -080085 rtc::Bind(&TransportController::DestroyAllChannels_n, this));
deadbeefcbecd352015-09-23 11:50:27 -070086}
87
88bool TransportController::SetSslMaxProtocolVersion(
89 rtc::SSLProtocolVersion version) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070090 return network_thread_->Invoke<bool>(
91 RTC_FROM_HERE, rtc::Bind(&TransportController::SetSslMaxProtocolVersion_n,
92 this, version));
deadbeefcbecd352015-09-23 11:50:27 -070093}
94
honghaiz1f429e32015-09-28 07:57:34 -070095void TransportController::SetIceConfig(const IceConfig& config) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +020096 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -070097 RTC_FROM_HERE,
Danil Chapovalov7f216b72016-05-12 09:20:31 +020098 rtc::Bind(&TransportController::SetIceConfig_n, this, config));
deadbeefcbecd352015-09-23 11:50:27 -070099}
100
101void TransportController::SetIceRole(IceRole ice_role) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200102 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700103 RTC_FROM_HERE,
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200104 rtc::Bind(&TransportController::SetIceRole_n, this, ice_role));
deadbeefcbecd352015-09-23 11:50:27 -0700105}
106
deadbeefd1a38b52016-12-10 13:15:33 -0800107void TransportController::SetNeedsIceRestartFlag() {
108 for (auto& kv : transports_) {
109 kv.second->SetNeedsIceRestartFlag();
110 }
111}
112
113bool TransportController::NeedsIceRestart(
114 const std::string& transport_name) const {
115 const JsepTransport* transport = GetJsepTransport(transport_name);
116 if (!transport) {
117 return false;
118 }
119 return transport->NeedsIceRestart();
120}
121
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800122bool TransportController::GetSslRole(const std::string& transport_name,
deadbeef49f34fd2016-12-06 16:22:06 -0800123 rtc::SSLRole* role) const {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700124 return network_thread_->Invoke<bool>(
125 RTC_FROM_HERE, rtc::Bind(&TransportController::GetSslRole_n, this,
126 transport_name, role));
deadbeefcbecd352015-09-23 11:50:27 -0700127}
128
129bool TransportController::SetLocalCertificate(
130 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700131 return network_thread_->Invoke<bool>(
132 RTC_FROM_HERE, rtc::Bind(&TransportController::SetLocalCertificate_n,
133 this, certificate));
deadbeefcbecd352015-09-23 11:50:27 -0700134}
135
136bool TransportController::GetLocalCertificate(
137 const std::string& transport_name,
deadbeef49f34fd2016-12-06 16:22:06 -0800138 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const {
hbosdf6075a2016-12-19 04:58:02 -0800139 if (network_thread_->IsCurrent()) {
140 return GetLocalCertificate_n(transport_name, certificate);
141 }
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200142 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700143 RTC_FROM_HERE, rtc::Bind(&TransportController::GetLocalCertificate_n,
144 this, transport_name, certificate));
deadbeefcbecd352015-09-23 11:50:27 -0700145}
146
jbauch555604a2016-04-26 03:13:22 -0700147std::unique_ptr<rtc::SSLCertificate>
kwibergb4d01c42016-04-06 05:15:06 -0700148TransportController::GetRemoteSSLCertificate(
deadbeef49f34fd2016-12-06 16:22:06 -0800149 const std::string& transport_name) const {
hbosdf6075a2016-12-19 04:58:02 -0800150 if (network_thread_->IsCurrent()) {
151 return GetRemoteSSLCertificate_n(transport_name);
152 }
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200153 return network_thread_->Invoke<std::unique_ptr<rtc::SSLCertificate>>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700154 RTC_FROM_HERE, rtc::Bind(&TransportController::GetRemoteSSLCertificate_n,
155 this, transport_name));
deadbeefcbecd352015-09-23 11:50:27 -0700156}
157
158bool TransportController::SetLocalTransportDescription(
159 const std::string& transport_name,
160 const TransportDescription& tdesc,
161 ContentAction action,
162 std::string* err) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200163 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700164 RTC_FROM_HERE,
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200165 rtc::Bind(&TransportController::SetLocalTransportDescription_n, this,
deadbeefcbecd352015-09-23 11:50:27 -0700166 transport_name, tdesc, action, err));
167}
168
169bool TransportController::SetRemoteTransportDescription(
170 const std::string& transport_name,
171 const TransportDescription& tdesc,
172 ContentAction action,
173 std::string* err) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200174 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700175 RTC_FROM_HERE,
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200176 rtc::Bind(&TransportController::SetRemoteTransportDescription_n, this,
deadbeefcbecd352015-09-23 11:50:27 -0700177 transport_name, tdesc, action, err));
178}
179
180void TransportController::MaybeStartGathering() {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200181 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700182 RTC_FROM_HERE,
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200183 rtc::Bind(&TransportController::MaybeStartGathering_n, this));
deadbeefcbecd352015-09-23 11:50:27 -0700184}
185
186bool TransportController::AddRemoteCandidates(const std::string& transport_name,
187 const Candidates& candidates,
188 std::string* err) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200189 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700190 RTC_FROM_HERE, rtc::Bind(&TransportController::AddRemoteCandidates_n,
191 this, transport_name, candidates, err));
deadbeefcbecd352015-09-23 11:50:27 -0700192}
193
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700194bool TransportController::RemoveRemoteCandidates(const Candidates& candidates,
195 std::string* err) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700196 return network_thread_->Invoke<bool>(
197 RTC_FROM_HERE, rtc::Bind(&TransportController::RemoveRemoteCandidates_n,
198 this, candidates, err));
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700199}
200
deadbeefcbecd352015-09-23 11:50:27 -0700201bool TransportController::ReadyForRemoteCandidates(
deadbeef49f34fd2016-12-06 16:22:06 -0800202 const std::string& transport_name) const {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700203 return network_thread_->Invoke<bool>(
204 RTC_FROM_HERE, rtc::Bind(&TransportController::ReadyForRemoteCandidates_n,
205 this, transport_name));
deadbeefcbecd352015-09-23 11:50:27 -0700206}
207
208bool TransportController::GetStats(const std::string& transport_name,
209 TransportStats* stats) {
hbosdf6075a2016-12-19 04:58:02 -0800210 if (network_thread_->IsCurrent()) {
211 return GetStats_n(transport_name, stats);
212 }
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200213 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700214 RTC_FROM_HERE,
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200215 rtc::Bind(&TransportController::GetStats_n, this, transport_name, stats));
deadbeefcbecd352015-09-23 11:50:27 -0700216}
217
deadbeef49f34fd2016-12-06 16:22:06 -0800218void TransportController::SetMetricsObserver(
219 webrtc::MetricsObserverInterface* metrics_observer) {
220 return network_thread_->Invoke<void>(
221 RTC_FROM_HERE, rtc::Bind(&TransportController::SetMetricsObserver_n, this,
222 metrics_observer));
223}
224
zhihuangb2cdd932017-01-19 16:54:25 -0800225DtlsTransportInternal* TransportController::CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -0800226 const std::string& transport_name,
227 int component) {
zhihuangb2cdd932017-01-19 16:54:25 -0800228 return network_thread_->Invoke<DtlsTransportInternal*>(
229 RTC_FROM_HERE, rtc::Bind(&TransportController::CreateDtlsTransport_n,
zhihuangf5b251b2017-01-12 19:37:48 -0800230 this, transport_name, component));
231}
232
zhihuangb2cdd932017-01-19 16:54:25 -0800233DtlsTransportInternal* TransportController::CreateDtlsTransport_n(
deadbeefcbecd352015-09-23 11:50:27 -0700234 const std::string& transport_name,
235 int component) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200236 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700237
deadbeef49f34fd2016-12-06 16:22:06 -0800238 RefCountedChannel* existing_channel = GetChannel_n(transport_name, component);
239 if (existing_channel) {
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700240 // Channel already exists; increment reference count and return.
deadbeef49f34fd2016-12-06 16:22:06 -0800241 existing_channel->AddRef();
242 return existing_channel->dtls();
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700243 }
244
245 // Need to create a new channel.
deadbeefd1a38b52016-12-10 13:15:33 -0800246 JsepTransport* transport = GetOrCreateJsepTransport(transport_name);
deadbeef49f34fd2016-12-06 16:22:06 -0800247
248 // Create DTLS channel wrapping ICE channel, and configure it.
zhihuangd06adf62017-01-12 15:58:31 -0800249 IceTransportInternal* ice =
deadbeef49f34fd2016-12-06 16:22:06 -0800250 CreateIceTransportChannel_n(transport_name, component);
zhihuangb2cdd932017-01-19 16:54:25 -0800251 DtlsTransportInternal* dtls =
deadbeef49f34fd2016-12-06 16:22:06 -0800252 CreateDtlsTransportChannel_n(transport_name, component, ice);
zhihuangb2cdd932017-01-19 16:54:25 -0800253 dtls->ice_transport()->SetMetricsObserver(metrics_observer_);
254 dtls->ice_transport()->SetIceRole(ice_role_);
255 dtls->ice_transport()->SetIceTiebreaker(ice_tiebreaker_);
256 dtls->ice_transport()->SetIceConfig(ice_config_);
zhihuangb19012e2017-09-19 13:47:59 -0700257 if (certificate_) {
258 bool set_cert_success = dtls->SetLocalCertificate(certificate_);
259 RTC_DCHECK(set_cert_success);
260 }
deadbeef49f34fd2016-12-06 16:22:06 -0800261
262 // Connect to signals offered by the channels. Currently, the DTLS channel
263 // forwards signals from the ICE channel, so we only need to connect to the
264 // DTLS channel. In the future this won't be the case.
265 dtls->SignalWritableState.connect(
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200266 this, &TransportController::OnChannelWritableState_n);
deadbeef49f34fd2016-12-06 16:22:06 -0800267 dtls->SignalReceivingState.connect(
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200268 this, &TransportController::OnChannelReceivingState_n);
deadbeef49f34fd2016-12-06 16:22:06 -0800269 dtls->SignalDtlsHandshakeError.connect(
zhihuangd82eee02016-08-26 11:25:05 -0700270 this, &TransportController::OnDtlsHandshakeError);
zhihuangb2cdd932017-01-19 16:54:25 -0800271 dtls->ice_transport()->SignalGatheringState.connect(
272 this, &TransportController::OnChannelGatheringState_n);
273 dtls->ice_transport()->SignalCandidateGathered.connect(
274 this, &TransportController::OnChannelCandidateGathered_n);
275 dtls->ice_transport()->SignalCandidatesRemoved.connect(
276 this, &TransportController::OnChannelCandidatesRemoved_n);
277 dtls->ice_transport()->SignalRoleConflict.connect(
278 this, &TransportController::OnChannelRoleConflict_n);
279 dtls->ice_transport()->SignalStateChanged.connect(
280 this, &TransportController::OnChannelStateChanged_n);
deadbeef62802a12016-12-13 16:38:36 -0800281 RefCountedChannel* new_pair = new RefCountedChannel(dtls, ice);
282 new_pair->AddRef();
283 channels_.insert(channels_.end(), new_pair);
deadbeef49f34fd2016-12-06 16:22:06 -0800284 bool channel_added = transport->AddChannel(dtls, component);
285 RTC_DCHECK(channel_added);
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700286 // Adding a channel could cause aggregate state to change.
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200287 UpdateAggregateStates_n();
deadbeef49f34fd2016-12-06 16:22:06 -0800288 return dtls;
deadbeefcbecd352015-09-23 11:50:27 -0700289}
290
zhihuangb2cdd932017-01-19 16:54:25 -0800291void TransportController::DestroyDtlsTransport(
deadbeefbad5dad2017-01-17 18:32:35 -0800292 const std::string& transport_name,
293 int component) {
294 network_thread_->Invoke<void>(
zhihuangb2cdd932017-01-19 16:54:25 -0800295 RTC_FROM_HERE, rtc::Bind(&TransportController::DestroyDtlsTransport_n,
deadbeefbad5dad2017-01-17 18:32:35 -0800296 this, transport_name, component));
297}
298
zhihuangb2cdd932017-01-19 16:54:25 -0800299void TransportController::DestroyDtlsTransport_n(
deadbeefcbecd352015-09-23 11:50:27 -0700300 const std::string& transport_name,
301 int component) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200302 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef49f34fd2016-12-06 16:22:06 -0800303 auto it = GetChannelIterator_n(transport_name, component);
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700304 if (it == channels_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100305 RTC_LOG(LS_WARNING) << "Attempting to delete " << transport_name
306 << " TransportChannel " << component
307 << ", which doesn't exist.";
deadbeefcbecd352015-09-23 11:50:27 -0700308 return;
309 }
Niels Möller6f72f562017-10-19 13:15:17 +0200310 // Release one reference to the RefCountedChannel, and do additional cleanup
311 // only if it was the last one. Matches the AddRef logic in
312 // CreateDtlsTransport_n.
313 if ((*it)->Release() == rtc::RefCountReleaseStatus::kOtherRefsRemained) {
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700314 return;
315 }
deadbeef57fd7262016-12-06 15:28:55 -0800316 channels_.erase(it);
deadbeef49f34fd2016-12-06 16:22:06 -0800317
deadbeefd1a38b52016-12-10 13:15:33 -0800318 JsepTransport* t = GetJsepTransport(transport_name);
deadbeef49f34fd2016-12-06 16:22:06 -0800319 bool channel_removed = t->RemoveChannel(component);
320 RTC_DCHECK(channel_removed);
deadbeefcbecd352015-09-23 11:50:27 -0700321 // Just as we create a Transport when its first channel is created,
322 // we delete it when its last channel is deleted.
deadbeef49f34fd2016-12-06 16:22:06 -0800323 if (!t->HasChannels()) {
324 transports_.erase(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700325 }
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700326 // Removing a channel could cause aggregate state to change.
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200327 UpdateAggregateStates_n();
deadbeefcbecd352015-09-23 11:50:27 -0700328}
329
deadbeef49f34fd2016-12-06 16:22:06 -0800330std::vector<std::string> TransportController::transport_names_for_testing() {
331 std::vector<std::string> ret;
332 for (const auto& kv : transports_) {
333 ret.push_back(kv.first);
mikescarlette7748672016-04-29 20:20:54 -0700334 }
deadbeef49f34fd2016-12-06 16:22:06 -0800335 return ret;
deadbeefcbecd352015-09-23 11:50:27 -0700336}
337
zhihuangb2cdd932017-01-19 16:54:25 -0800338std::vector<DtlsTransportInternal*>
339TransportController::channels_for_testing() {
340 std::vector<DtlsTransportInternal*> ret;
deadbeef62802a12016-12-13 16:38:36 -0800341 for (RefCountedChannel* channel : channels_) {
342 ret.push_back(channel->dtls());
deadbeef49f34fd2016-12-06 16:22:06 -0800343 }
344 return ret;
345}
deadbeefcbecd352015-09-23 11:50:27 -0700346
zhihuangb2cdd932017-01-19 16:54:25 -0800347DtlsTransportInternal* TransportController::get_channel_for_testing(
deadbeef49f34fd2016-12-06 16:22:06 -0800348 const std::string& transport_name,
349 int component) {
350 RefCountedChannel* ch = GetChannel_n(transport_name, component);
351 return ch ? ch->dtls() : nullptr;
352}
353
zhihuangd06adf62017-01-12 15:58:31 -0800354IceTransportInternal* TransportController::CreateIceTransportChannel_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800355 const std::string& transport_name,
356 int component) {
357 return new P2PTransportChannel(transport_name, component, port_allocator_);
358}
359
zhihuangb2cdd932017-01-19 16:54:25 -0800360DtlsTransportInternal* TransportController::CreateDtlsTransportChannel_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800361 const std::string&,
362 int,
zhihuangd06adf62017-01-12 15:58:31 -0800363 IceTransportInternal* ice) {
deadbeef7914b8c2017-04-21 03:23:33 -0700364 DtlsTransport* dtls = new DtlsTransport(ice, crypto_options_);
deadbeef49f34fd2016-12-06 16:22:06 -0800365 dtls->SetSslMaxProtocolVersion(ssl_max_version_);
366 return dtls;
deadbeefcbecd352015-09-23 11:50:27 -0700367}
368
369void TransportController::OnMessage(rtc::Message* pmsg) {
370 RTC_DCHECK(signaling_thread_->IsCurrent());
371
372 switch (pmsg->message_id) {
373 case MSG_ICECONNECTIONSTATE: {
374 rtc::TypedMessageData<IceConnectionState>* data =
375 static_cast<rtc::TypedMessageData<IceConnectionState>*>(pmsg->pdata);
376 SignalConnectionState(data->data());
377 delete data;
378 break;
379 }
380 case MSG_RECEIVING: {
381 rtc::TypedMessageData<bool>* data =
382 static_cast<rtc::TypedMessageData<bool>*>(pmsg->pdata);
383 SignalReceiving(data->data());
384 delete data;
385 break;
386 }
387 case MSG_ICEGATHERINGSTATE: {
388 rtc::TypedMessageData<IceGatheringState>* data =
389 static_cast<rtc::TypedMessageData<IceGatheringState>*>(pmsg->pdata);
390 SignalGatheringState(data->data());
391 delete data;
392 break;
393 }
394 case MSG_CANDIDATESGATHERED: {
395 CandidatesData* data = static_cast<CandidatesData*>(pmsg->pdata);
396 SignalCandidatesGathered(data->transport_name, data->candidates);
397 delete data;
398 break;
399 }
400 default:
nissec80e7412017-01-11 05:56:46 -0800401 RTC_NOTREACHED();
deadbeefcbecd352015-09-23 11:50:27 -0700402 }
403}
404
deadbeef62802a12016-12-13 16:38:36 -0800405std::vector<TransportController::RefCountedChannel*>::iterator
deadbeef49f34fd2016-12-06 16:22:06 -0800406TransportController::GetChannelIterator_n(const std::string& transport_name,
407 int component) {
408 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef62802a12016-12-13 16:38:36 -0800409 return std::find_if(channels_.begin(), channels_.end(),
410 [transport_name, component](RefCountedChannel* channel) {
411 return channel->dtls()->transport_name() ==
412 transport_name &&
413 channel->dtls()->component() == component;
414 });
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700415}
416
deadbeef62802a12016-12-13 16:38:36 -0800417std::vector<TransportController::RefCountedChannel*>::const_iterator
deadbeef49f34fd2016-12-06 16:22:06 -0800418TransportController::GetChannelIterator_n(const std::string& transport_name,
419 int component) const {
420 RTC_DCHECK(network_thread_->IsCurrent());
421 return std::find_if(
422 channels_.begin(), channels_.end(),
deadbeef62802a12016-12-13 16:38:36 -0800423 [transport_name, component](const RefCountedChannel* channel) {
424 return channel->dtls()->transport_name() == transport_name &&
425 channel->dtls()->component() == component;
deadbeef49f34fd2016-12-06 16:22:06 -0800426 });
427}
428
deadbeefd1a38b52016-12-10 13:15:33 -0800429const JsepTransport* TransportController::GetJsepTransport(
deadbeef49f34fd2016-12-06 16:22:06 -0800430 const std::string& transport_name) const {
deadbeef49f34fd2016-12-06 16:22:06 -0800431 auto it = transports_.find(transport_name);
432 return (it == transports_.end()) ? nullptr : it->second.get();
433}
434
deadbeefd1a38b52016-12-10 13:15:33 -0800435JsepTransport* TransportController::GetJsepTransport(
deadbeef49f34fd2016-12-06 16:22:06 -0800436 const std::string& transport_name) {
deadbeef49f34fd2016-12-06 16:22:06 -0800437 auto it = transports_.find(transport_name);
438 return (it == transports_.end()) ? nullptr : it->second.get();
439}
440
441const TransportController::RefCountedChannel* TransportController::GetChannel_n(
442 const std::string& transport_name,
443 int component) const {
444 RTC_DCHECK(network_thread_->IsCurrent());
445 auto it = GetChannelIterator_n(transport_name, component);
deadbeef62802a12016-12-13 16:38:36 -0800446 return (it == channels_.end()) ? nullptr : *it;
deadbeef49f34fd2016-12-06 16:22:06 -0800447}
448
449TransportController::RefCountedChannel* TransportController::GetChannel_n(
450 const std::string& transport_name,
451 int component) {
452 RTC_DCHECK(network_thread_->IsCurrent());
453 auto it = GetChannelIterator_n(transport_name, component);
deadbeef62802a12016-12-13 16:38:36 -0800454 return (it == channels_.end()) ? nullptr : *it;
deadbeef49f34fd2016-12-06 16:22:06 -0800455}
456
deadbeefd1a38b52016-12-10 13:15:33 -0800457JsepTransport* TransportController::GetOrCreateJsepTransport(
deadbeefcbecd352015-09-23 11:50:27 -0700458 const std::string& transport_name) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200459 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700460
deadbeefd1a38b52016-12-10 13:15:33 -0800461 JsepTransport* transport = GetJsepTransport(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700462 if (transport) {
463 return transport;
464 }
465
deadbeef49f34fd2016-12-06 16:22:06 -0800466 transport = new JsepTransport(transport_name, certificate_);
467 transports_[transport_name] = std::unique_ptr<JsepTransport>(transport);
deadbeefcbecd352015-09-23 11:50:27 -0700468 return transport;
469}
470
deadbeef49f34fd2016-12-06 16:22:06 -0800471void TransportController::DestroyAllChannels_n() {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200472 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700473 transports_.clear();
Niels Möller6f72f562017-10-19 13:15:17 +0200474 // TODO(nisse): If |channels_| were a vector of scoped_refptr, we
475 // wouldn't need this strange hack.
deadbeef62802a12016-12-13 16:38:36 -0800476 for (RefCountedChannel* channel : channels_) {
477 // Even though these objects are normally ref-counted, if
478 // TransportController is deleted while they still have references, just
479 // remove all references.
Niels Möller6f72f562017-10-19 13:15:17 +0200480 while (channel->Release() ==
481 rtc::RefCountReleaseStatus::kOtherRefsRemained) {
deadbeef62802a12016-12-13 16:38:36 -0800482 }
483 }
deadbeef49f34fd2016-12-06 16:22:06 -0800484 channels_.clear();
deadbeefcbecd352015-09-23 11:50:27 -0700485}
486
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200487bool TransportController::SetSslMaxProtocolVersion_n(
deadbeefcbecd352015-09-23 11:50:27 -0700488 rtc::SSLProtocolVersion version) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200489 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700490
491 // Max SSL version can only be set before transports are created.
492 if (!transports_.empty()) {
493 return false;
494 }
495
496 ssl_max_version_ = version;
497 return true;
498}
499
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200500void TransportController::SetIceConfig_n(const IceConfig& config) {
501 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef49f34fd2016-12-06 16:22:06 -0800502
honghaiz1f429e32015-09-28 07:57:34 -0700503 ice_config_ = config;
deadbeef49f34fd2016-12-06 16:22:06 -0800504 for (auto& channel : channels_) {
zhihuangb2cdd932017-01-19 16:54:25 -0800505 channel->dtls()->ice_transport()->SetIceConfig(ice_config_);
deadbeefcbecd352015-09-23 11:50:27 -0700506 }
507}
508
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200509void TransportController::SetIceRole_n(IceRole ice_role) {
510 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef49f34fd2016-12-06 16:22:06 -0800511
deadbeefcbecd352015-09-23 11:50:27 -0700512 ice_role_ = ice_role;
deadbeef49f34fd2016-12-06 16:22:06 -0800513 for (auto& channel : channels_) {
zhihuangb2cdd932017-01-19 16:54:25 -0800514 channel->dtls()->ice_transport()->SetIceRole(ice_role_);
deadbeefcbecd352015-09-23 11:50:27 -0700515 }
516}
517
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200518bool TransportController::GetSslRole_n(const std::string& transport_name,
deadbeef49f34fd2016-12-06 16:22:06 -0800519 rtc::SSLRole* role) const {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200520 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700521
deadbeefd1a38b52016-12-10 13:15:33 -0800522 const JsepTransport* t = GetJsepTransport(transport_name);
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800523 if (!t) {
deadbeefcbecd352015-09-23 11:50:27 -0700524 return false;
525 }
deadbeefd8cfa1a2017-03-27 10:33:26 -0700526 rtc::Optional<rtc::SSLRole> current_role = t->GetSslRole();
527 if (!current_role) {
528 return false;
529 }
530 *role = *current_role;
deadbeef49f34fd2016-12-06 16:22:06 -0800531 return true;
deadbeefcbecd352015-09-23 11:50:27 -0700532}
533
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200534bool TransportController::SetLocalCertificate_n(
deadbeefcbecd352015-09-23 11:50:27 -0700535 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200536 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700537
deadbeef49f34fd2016-12-06 16:22:06 -0800538 // Can't change a certificate, or set a null certificate.
539 if (certificate_ || !certificate) {
deadbeefcbecd352015-09-23 11:50:27 -0700540 return false;
541 }
542 certificate_ = certificate;
543
deadbeef8662f942017-01-20 21:20:51 -0800544 // Set certificate for JsepTransport, which verifies it matches the
zhihuangb19012e2017-09-19 13:47:59 -0700545 // fingerprint in SDP, and DTLS transport.
546 // Fallback from DTLS to SDES is not supported.
deadbeef49f34fd2016-12-06 16:22:06 -0800547 for (auto& kv : transports_) {
deadbeefcbecd352015-09-23 11:50:27 -0700548 kv.second->SetLocalCertificate(certificate_);
549 }
zhihuangb19012e2017-09-19 13:47:59 -0700550 for (auto& channel : channels_) {
551 bool set_cert_success = channel->dtls()->SetLocalCertificate(certificate_);
552 RTC_DCHECK(set_cert_success);
553 }
deadbeefcbecd352015-09-23 11:50:27 -0700554 return true;
555}
556
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200557bool TransportController::GetLocalCertificate_n(
deadbeefcbecd352015-09-23 11:50:27 -0700558 const std::string& transport_name,
deadbeef49f34fd2016-12-06 16:22:06 -0800559 rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200560 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700561
deadbeefd1a38b52016-12-10 13:15:33 -0800562 const JsepTransport* t = GetJsepTransport(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700563 if (!t) {
564 return false;
565 }
deadbeefcbecd352015-09-23 11:50:27 -0700566 return t->GetLocalCertificate(certificate);
567}
568
jbauch555604a2016-04-26 03:13:22 -0700569std::unique_ptr<rtc::SSLCertificate>
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200570TransportController::GetRemoteSSLCertificate_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800571 const std::string& transport_name) const {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200572 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700573
deadbeef49f34fd2016-12-06 16:22:06 -0800574 // Get the certificate from the RTP channel's DTLS handshake. Should be
575 // identical to the RTCP channel's, since they were given the same remote
576 // fingerprint.
577 const RefCountedChannel* ch = GetChannel_n(transport_name, 1);
578 if (!ch) {
kwibergb4d01c42016-04-06 05:15:06 -0700579 return nullptr;
deadbeefcbecd352015-09-23 11:50:27 -0700580 }
deadbeef49f34fd2016-12-06 16:22:06 -0800581 return ch->dtls()->GetRemoteSSLCertificate();
deadbeefcbecd352015-09-23 11:50:27 -0700582}
583
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200584bool TransportController::SetLocalTransportDescription_n(
deadbeefcbecd352015-09-23 11:50:27 -0700585 const std::string& transport_name,
586 const TransportDescription& tdesc,
587 ContentAction action,
588 std::string* err) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200589 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700590
deadbeefd1a38b52016-12-10 13:15:33 -0800591 JsepTransport* transport = GetJsepTransport(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700592 if (!transport) {
593 // If we didn't find a transport, that's not an error;
594 // it could have been deleted as a result of bundling.
595 // TODO(deadbeef): Make callers smarter so they won't attempt to set a
596 // description on a deleted transport.
597 return true;
598 }
599
deadbeef91042f82016-07-15 17:48:13 -0700600 // Older versions of Chrome expect the ICE role to be re-determined when an
601 // ICE restart occurs, and also don't perform conflict resolution correctly,
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -0700602 // so for now we can't safely stop doing this, unless the application opts in
603 // by setting |redetermine_role_on_ice_restart_| to false.
deadbeef91042f82016-07-15 17:48:13 -0700604 // See: https://bugs.chromium.org/p/chromium/issues/detail?id=628676
605 // TODO(deadbeef): Remove this when these old versions of Chrome reach a low
606 // enough population.
Taylor Brandstetterf0bb3602016-08-26 20:59:24 -0700607 if (redetermine_role_on_ice_restart_ && transport->local_description() &&
deadbeef91042f82016-07-15 17:48:13 -0700608 IceCredentialsChanged(transport->local_description()->ice_ufrag,
609 transport->local_description()->ice_pwd,
deadbeef897d08e2017-04-20 00:57:25 -0700610 tdesc.ice_ufrag, tdesc.ice_pwd) &&
611 // Don't change the ICE role if the remote endpoint is ICE lite; we
612 // should always be controlling in that case.
613 (!transport->remote_description() ||
614 transport->remote_description()->ice_mode != ICEMODE_LITE)) {
deadbeef91042f82016-07-15 17:48:13 -0700615 IceRole new_ice_role =
616 (action == CA_OFFER) ? ICEROLE_CONTROLLING : ICEROLE_CONTROLLED;
617 SetIceRole(new_ice_role);
618 }
619
Mirko Bonadei675513b2017-11-09 11:09:25 +0100620 RTC_LOG(LS_INFO) << "Set local transport description on " << transport_name;
deadbeefcbecd352015-09-23 11:50:27 -0700621 return transport->SetLocalTransportDescription(tdesc, action, err);
622}
623
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200624bool TransportController::SetRemoteTransportDescription_n(
deadbeefcbecd352015-09-23 11:50:27 -0700625 const std::string& transport_name,
626 const TransportDescription& tdesc,
627 ContentAction action,
628 std::string* err) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200629 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700630
deadbeef49f34fd2016-12-06 16:22:06 -0800631 // If our role is ICEROLE_CONTROLLED and the remote endpoint supports only
632 // ice_lite, this local endpoint should take the CONTROLLING role.
633 // TODO(deadbeef): This is a session-level attribute, so it really shouldn't
634 // be in a TransportDescription in the first place...
635 if (ice_role_ == ICEROLE_CONTROLLED && tdesc.ice_mode == ICEMODE_LITE) {
636 SetIceRole_n(ICEROLE_CONTROLLING);
637 }
638
deadbeefd1a38b52016-12-10 13:15:33 -0800639 JsepTransport* transport = GetJsepTransport(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700640 if (!transport) {
641 // If we didn't find a transport, that's not an error;
642 // it could have been deleted as a result of bundling.
643 // TODO(deadbeef): Make callers smarter so they won't attempt to set a
644 // description on a deleted transport.
645 return true;
646 }
647
Mirko Bonadei675513b2017-11-09 11:09:25 +0100648 RTC_LOG(LS_INFO) << "Set remote transport description on " << transport_name;
deadbeefcbecd352015-09-23 11:50:27 -0700649 return transport->SetRemoteTransportDescription(tdesc, action, err);
650}
651
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200652void TransportController::MaybeStartGathering_n() {
deadbeef49f34fd2016-12-06 16:22:06 -0800653 for (auto& channel : channels_) {
zhihuangb2cdd932017-01-19 16:54:25 -0800654 channel->dtls()->ice_transport()->MaybeStartGathering();
deadbeefcbecd352015-09-23 11:50:27 -0700655 }
656}
657
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200658bool TransportController::AddRemoteCandidates_n(
deadbeefcbecd352015-09-23 11:50:27 -0700659 const std::string& transport_name,
660 const Candidates& candidates,
661 std::string* err) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200662 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700663
deadbeef49f34fd2016-12-06 16:22:06 -0800664 // Verify each candidate before passing down to the transport layer.
665 if (!VerifyCandidates(candidates, err)) {
666 return false;
667 }
668
deadbeefd1a38b52016-12-10 13:15:33 -0800669 JsepTransport* transport = GetJsepTransport(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700670 if (!transport) {
671 // If we didn't find a transport, that's not an error;
672 // it could have been deleted as a result of bundling.
673 return true;
674 }
675
deadbeef49f34fd2016-12-06 16:22:06 -0800676 for (const Candidate& candidate : candidates) {
677 RefCountedChannel* channel =
678 GetChannel_n(transport_name, candidate.component());
679 if (!channel) {
680 *err = "Candidate has an unknown component: " + candidate.ToString() +
681 " for content: " + transport_name;
682 return false;
683 }
zhihuangb2cdd932017-01-19 16:54:25 -0800684 channel->dtls()->ice_transport()->AddRemoteCandidate(candidate);
deadbeef49f34fd2016-12-06 16:22:06 -0800685 }
686 return true;
deadbeefcbecd352015-09-23 11:50:27 -0700687}
688
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200689bool TransportController::RemoveRemoteCandidates_n(const Candidates& candidates,
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700690 std::string* err) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200691 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef49f34fd2016-12-06 16:22:06 -0800692
693 // Verify each candidate before passing down to the transport layer.
694 if (!VerifyCandidates(candidates, err)) {
695 return false;
696 }
697
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700698 std::map<std::string, Candidates> candidates_by_transport_name;
699 for (const Candidate& cand : candidates) {
Steve Antonf1c6db12017-10-13 11:13:35 -0700700 if (!cand.transport_name().empty()) {
701 candidates_by_transport_name[cand.transport_name()].push_back(cand);
702 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100703 RTC_LOG(LS_ERROR) << "Not removing candidate because it does not have a "
704 "transport name set: "
705 << cand.ToString();
Steve Antonf1c6db12017-10-13 11:13:35 -0700706 }
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700707 }
708
709 bool result = true;
deadbeef49f34fd2016-12-06 16:22:06 -0800710 for (const auto& kv : candidates_by_transport_name) {
711 const std::string& transport_name = kv.first;
712 const Candidates& candidates = kv.second;
deadbeefd1a38b52016-12-10 13:15:33 -0800713 JsepTransport* transport = GetJsepTransport(transport_name);
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700714 if (!transport) {
715 // If we didn't find a transport, that's not an error;
716 // it could have been deleted as a result of bundling.
717 continue;
718 }
deadbeef49f34fd2016-12-06 16:22:06 -0800719 for (const Candidate& candidate : candidates) {
720 RefCountedChannel* channel =
721 GetChannel_n(transport_name, candidate.component());
722 if (channel) {
zhihuangb2cdd932017-01-19 16:54:25 -0800723 channel->dtls()->ice_transport()->RemoveRemoteCandidate(candidate);
deadbeef49f34fd2016-12-06 16:22:06 -0800724 }
725 }
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700726 }
727 return result;
728}
729
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200730bool TransportController::ReadyForRemoteCandidates_n(
deadbeef49f34fd2016-12-06 16:22:06 -0800731 const std::string& transport_name) const {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200732 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700733
deadbeefd1a38b52016-12-10 13:15:33 -0800734 const JsepTransport* transport = GetJsepTransport(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700735 if (!transport) {
736 return false;
737 }
738 return transport->ready_for_remote_candidates();
739}
740
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200741bool TransportController::GetStats_n(const std::string& transport_name,
deadbeefcbecd352015-09-23 11:50:27 -0700742 TransportStats* stats) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200743 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700744
deadbeefd1a38b52016-12-10 13:15:33 -0800745 JsepTransport* transport = GetJsepTransport(transport_name);
deadbeefcbecd352015-09-23 11:50:27 -0700746 if (!transport) {
747 return false;
748 }
749 return transport->GetStats(stats);
750}
751
deadbeef49f34fd2016-12-06 16:22:06 -0800752void TransportController::SetMetricsObserver_n(
753 webrtc::MetricsObserverInterface* metrics_observer) {
754 RTC_DCHECK(network_thread_->IsCurrent());
755 metrics_observer_ = metrics_observer;
756 for (auto& channel : channels_) {
zhihuangb2cdd932017-01-19 16:54:25 -0800757 channel->dtls()->ice_transport()->SetMetricsObserver(metrics_observer);
deadbeef49f34fd2016-12-06 16:22:06 -0800758 }
759}
760
johand89ab142016-10-25 10:50:32 -0700761void TransportController::OnChannelWritableState_n(
deadbeef5bd5ca32017-02-10 11:31:50 -0800762 rtc::PacketTransportInternal* transport) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200763 RTC_DCHECK(network_thread_->IsCurrent());
Zhi Huang942bc2e2017-11-13 13:26:07 -0800764 RTC_LOG(LS_INFO) << " Transport " << transport->transport_name()
Mirko Bonadei675513b2017-11-09 11:09:25 +0100765 << " writability changed to " << transport->writable()
766 << ".";
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200767 UpdateAggregateStates_n();
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700768}
769
johan15ca8f62016-11-01 01:47:41 -0700770void TransportController::OnChannelReceivingState_n(
deadbeef5bd5ca32017-02-10 11:31:50 -0800771 rtc::PacketTransportInternal* transport) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200772 RTC_DCHECK(network_thread_->IsCurrent());
773 UpdateAggregateStates_n();
deadbeefcbecd352015-09-23 11:50:27 -0700774}
775
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200776void TransportController::OnChannelGatheringState_n(
zhihuangb2cdd932017-01-19 16:54:25 -0800777 IceTransportInternal* channel) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200778 RTC_DCHECK(network_thread_->IsCurrent());
779 UpdateAggregateStates_n();
deadbeefcbecd352015-09-23 11:50:27 -0700780}
781
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200782void TransportController::OnChannelCandidateGathered_n(
zhihuangb2cdd932017-01-19 16:54:25 -0800783 IceTransportInternal* channel,
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700784 const Candidate& candidate) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200785 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700786
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700787 // We should never signal peer-reflexive candidates.
788 if (candidate.type() == PRFLX_PORT_TYPE) {
nisseeb4ca4e2017-01-12 02:24:27 -0800789 RTC_NOTREACHED();
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700790 return;
791 }
792 std::vector<Candidate> candidates;
793 candidates.push_back(candidate);
794 CandidatesData* data =
795 new CandidatesData(channel->transport_name(), candidates);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700796 signaling_thread_->Post(RTC_FROM_HERE, this, MSG_CANDIDATESGATHERED, data);
deadbeefcbecd352015-09-23 11:50:27 -0700797}
798
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200799void TransportController::OnChannelCandidatesRemoved_n(
zhihuangb2cdd932017-01-19 16:54:25 -0800800 IceTransportInternal* channel,
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700801 const Candidates& candidates) {
802 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700803 RTC_FROM_HERE, signaling_thread_,
Honghai Zhang7fb69db2016-03-14 11:59:18 -0700804 rtc::Bind(&TransportController::OnChannelCandidatesRemoved, this,
805 candidates));
806}
807
808void TransportController::OnChannelCandidatesRemoved(
809 const Candidates& candidates) {
810 RTC_DCHECK(signaling_thread_->IsCurrent());
811 SignalCandidatesRemoved(candidates);
812}
813
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200814void TransportController::OnChannelRoleConflict_n(
zhihuangb2cdd932017-01-19 16:54:25 -0800815 IceTransportInternal* channel) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200816 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef1c206102016-05-27 13:34:37 -0700817 // Note: since the role conflict is handled entirely on the network thread,
818 // we don't need to worry about role conflicts occurring on two ports at once.
819 // The first one encountered should immediately reverse the role.
deadbeefcbecd352015-09-23 11:50:27 -0700820 IceRole reversed_role = (ice_role_ == ICEROLE_CONTROLLING)
821 ? ICEROLE_CONTROLLED
822 : ICEROLE_CONTROLLING;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100823 RTC_LOG(LS_INFO) << "Got role conflict; switching to "
824 << (reversed_role == ICEROLE_CONTROLLING ? "controlling"
825 : "controlled")
826 << " role.";
deadbeef1c206102016-05-27 13:34:37 -0700827 SetIceRole_n(reversed_role);
deadbeefcbecd352015-09-23 11:50:27 -0700828}
829
Honghai Zhang1590c392016-05-24 13:15:02 -0700830void TransportController::OnChannelStateChanged_n(
zhihuangb2cdd932017-01-19 16:54:25 -0800831 IceTransportInternal* channel) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200832 RTC_DCHECK(network_thread_->IsCurrent());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100833 RTC_LOG(LS_INFO) << channel->transport_name() << " TransportChannel "
834 << channel->component()
835 << " state changed. Check if state is complete.";
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200836 UpdateAggregateStates_n();
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700837}
838
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200839void TransportController::UpdateAggregateStates_n() {
840 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700841
842 IceConnectionState new_connection_state = kIceConnectionConnecting;
843 IceGatheringState new_gathering_state = kIceGatheringNew;
844 bool any_receiving = false;
845 bool any_failed = false;
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700846 bool all_connected = !channels_.empty();
847 bool all_completed = !channels_.empty();
deadbeefcbecd352015-09-23 11:50:27 -0700848 bool any_gathering = false;
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700849 bool all_done_gathering = !channels_.empty();
850 for (const auto& channel : channels_) {
deadbeef62802a12016-12-13 16:38:36 -0800851 any_receiving = any_receiving || channel->dtls()->receiving();
Zhi Huangb5261582017-09-29 10:51:43 -0700852 any_failed = any_failed || channel->dtls()->ice_transport()->GetState() ==
853 IceTransportState::STATE_FAILED;
deadbeef62802a12016-12-13 16:38:36 -0800854 all_connected = all_connected && channel->dtls()->writable();
Taylor Brandstetterc4d3a5d2015-09-30 10:32:59 -0700855 all_completed =
deadbeef62802a12016-12-13 16:38:36 -0800856 all_completed && channel->dtls()->writable() &&
zhihuangb2cdd932017-01-19 16:54:25 -0800857 channel->dtls()->ice_transport()->GetState() ==
858 IceTransportState::STATE_COMPLETED &&
859 channel->dtls()->ice_transport()->GetIceRole() == ICEROLE_CONTROLLING &&
860 channel->dtls()->ice_transport()->gathering_state() ==
861 kIceGatheringComplete;
deadbeefcbecd352015-09-23 11:50:27 -0700862 any_gathering =
zhihuangb2cdd932017-01-19 16:54:25 -0800863 any_gathering ||
864 channel->dtls()->ice_transport()->gathering_state() != kIceGatheringNew;
865 all_done_gathering = all_done_gathering &&
866 channel->dtls()->ice_transport()->gathering_state() ==
867 kIceGatheringComplete;
deadbeefcbecd352015-09-23 11:50:27 -0700868 }
deadbeefcbecd352015-09-23 11:50:27 -0700869 if (any_failed) {
870 new_connection_state = kIceConnectionFailed;
871 } else if (all_completed) {
872 new_connection_state = kIceConnectionCompleted;
873 } else if (all_connected) {
874 new_connection_state = kIceConnectionConnected;
875 }
876 if (connection_state_ != new_connection_state) {
877 connection_state_ = new_connection_state;
878 signaling_thread_->Post(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700879 RTC_FROM_HERE, this, MSG_ICECONNECTIONSTATE,
deadbeefcbecd352015-09-23 11:50:27 -0700880 new rtc::TypedMessageData<IceConnectionState>(new_connection_state));
881 }
882
883 if (receiving_ != any_receiving) {
884 receiving_ = any_receiving;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700885 signaling_thread_->Post(RTC_FROM_HERE, this, MSG_RECEIVING,
deadbeefcbecd352015-09-23 11:50:27 -0700886 new rtc::TypedMessageData<bool>(any_receiving));
887 }
888
889 if (all_done_gathering) {
890 new_gathering_state = kIceGatheringComplete;
891 } else if (any_gathering) {
892 new_gathering_state = kIceGatheringGathering;
893 }
894 if (gathering_state_ != new_gathering_state) {
895 gathering_state_ = new_gathering_state;
896 signaling_thread_->Post(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700897 RTC_FROM_HERE, this, MSG_ICEGATHERINGSTATE,
deadbeefcbecd352015-09-23 11:50:27 -0700898 new rtc::TypedMessageData<IceGatheringState>(new_gathering_state));
899 }
900}
901
zhihuangd82eee02016-08-26 11:25:05 -0700902void TransportController::OnDtlsHandshakeError(rtc::SSLHandshakeError error) {
903 SignalDtlsHandshakeError(error);
904}
905
deadbeefcbecd352015-09-23 11:50:27 -0700906} // namespace cricket