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 | |
Zhi Huang | b526158 | 2017-09-29 10:51:43 -0700 | [diff] [blame] | 11 | #include "pc/transportcontroller.h" |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 12 | |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 13 | #include <algorithm> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 14 | #include <memory> |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "p2p/base/port.h" |
| 17 | #include "rtc_base/bind.h" |
| 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/thread.h" |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 20 | |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 21 | namespace { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 22 | |
| 23 | enum { |
| 24 | MSG_ICECONNECTIONSTATE, |
| 25 | MSG_RECEIVING, |
| 26 | MSG_ICEGATHERINGSTATE, |
| 27 | MSG_CANDIDATESGATHERED, |
| 28 | }; |
| 29 | |
| 30 | struct CandidatesData : public rtc::MessageData { |
| 31 | CandidatesData(const std::string& transport_name, |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 32 | const cricket::Candidates& candidates) |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 33 | : transport_name(transport_name), candidates(candidates) {} |
| 34 | |
| 35 | std::string transport_name; |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 36 | cricket::Candidates candidates; |
| 37 | }; |
| 38 | |
Zhi Huang | b526158 | 2017-09-29 10:51:43 -0700 | [diff] [blame] | 39 | } // namespace |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 40 | |
| 41 | namespace 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. |
| 45 | class TransportController::ChannelPair { |
| 46 | public: |
| 47 | // TODO(deadbeef): Change the types of |dtls| and |ice| to |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 48 | // DtlsTransport and P2PTransportChannelWrapper, once TransportChannelImpl is |
| 49 | // removed. |
| 50 | ChannelPair(DtlsTransportInternal* dtls, IceTransportInternal* ice) |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 51 | : 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. |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 56 | const DtlsTransportInternal* dtls() const { return dtls_.get(); } |
| 57 | DtlsTransportInternal* dtls() { return dtls_.get(); } |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 58 | const IceTransportInternal* ice() const { return ice_.get(); } |
| 59 | IceTransportInternal* ice() { return ice_.get(); } |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 60 | |
| 61 | private: |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 62 | std::unique_ptr<IceTransportInternal> ice_; |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 63 | std::unique_ptr<DtlsTransportInternal> dtls_; |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 64 | |
| 65 | RTC_DISALLOW_COPY_AND_ASSIGN(ChannelPair); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
deadbeef | 7914b8c | 2017-04-21 03:23:33 -0700 | [diff] [blame] | 68 | TransportController::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) |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 74 | : signaling_thread_(signaling_thread), |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 75 | network_thread_(network_thread), |
Taylor Brandstetter | f0bb360 | 2016-08-26 20:59:24 -0700 | [diff] [blame] | 76 | port_allocator_(port_allocator), |
deadbeef | 7914b8c | 2017-04-21 03:23:33 -0700 | [diff] [blame] | 77 | redetermine_role_on_ice_restart_(redetermine_role_on_ice_restart), |
| 78 | crypto_options_(crypto_options) {} |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 79 | |
| 80 | TransportController::~TransportController() { |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 81 | // Channel destructors may try to send packets, so this needs to happen on |
| 82 | // the network thread. |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 83 | network_thread_->Invoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 84 | RTC_FROM_HERE, |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 85 | rtc::Bind(&TransportController::DestroyAllChannels_n, this)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | bool TransportController::SetSslMaxProtocolVersion( |
| 89 | rtc::SSLProtocolVersion version) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 90 | return network_thread_->Invoke<bool>( |
| 91 | RTC_FROM_HERE, rtc::Bind(&TransportController::SetSslMaxProtocolVersion_n, |
| 92 | this, version)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 93 | } |
| 94 | |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 95 | void TransportController::SetIceConfig(const IceConfig& config) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 96 | network_thread_->Invoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 97 | RTC_FROM_HERE, |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 98 | rtc::Bind(&TransportController::SetIceConfig_n, this, config)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void TransportController::SetIceRole(IceRole ice_role) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 102 | network_thread_->Invoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 103 | RTC_FROM_HERE, |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 104 | rtc::Bind(&TransportController::SetIceRole_n, this, ice_role)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 105 | } |
| 106 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 107 | void TransportController::SetNeedsIceRestartFlag() { |
| 108 | for (auto& kv : transports_) { |
| 109 | kv.second->SetNeedsIceRestartFlag(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | bool 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 Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 122 | bool TransportController::GetSslRole(const std::string& transport_name, |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 123 | rtc::SSLRole* role) const { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 124 | return network_thread_->Invoke<bool>( |
| 125 | RTC_FROM_HERE, rtc::Bind(&TransportController::GetSslRole_n, this, |
| 126 | transport_name, role)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | bool TransportController::SetLocalCertificate( |
| 130 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 131 | return network_thread_->Invoke<bool>( |
| 132 | RTC_FROM_HERE, rtc::Bind(&TransportController::SetLocalCertificate_n, |
| 133 | this, certificate)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | bool TransportController::GetLocalCertificate( |
| 137 | const std::string& transport_name, |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 138 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 139 | if (network_thread_->IsCurrent()) { |
| 140 | return GetLocalCertificate_n(transport_name, certificate); |
| 141 | } |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 142 | return network_thread_->Invoke<bool>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 143 | RTC_FROM_HERE, rtc::Bind(&TransportController::GetLocalCertificate_n, |
| 144 | this, transport_name, certificate)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 145 | } |
| 146 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 147 | std::unique_ptr<rtc::SSLCertificate> |
kwiberg | b4d01c4 | 2016-04-06 05:15:06 -0700 | [diff] [blame] | 148 | TransportController::GetRemoteSSLCertificate( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 149 | const std::string& transport_name) const { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 150 | if (network_thread_->IsCurrent()) { |
| 151 | return GetRemoteSSLCertificate_n(transport_name); |
| 152 | } |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 153 | return network_thread_->Invoke<std::unique_ptr<rtc::SSLCertificate>>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 154 | RTC_FROM_HERE, rtc::Bind(&TransportController::GetRemoteSSLCertificate_n, |
| 155 | this, transport_name)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | bool TransportController::SetLocalTransportDescription( |
| 159 | const std::string& transport_name, |
| 160 | const TransportDescription& tdesc, |
| 161 | ContentAction action, |
| 162 | std::string* err) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 163 | return network_thread_->Invoke<bool>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 164 | RTC_FROM_HERE, |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 165 | rtc::Bind(&TransportController::SetLocalTransportDescription_n, this, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 166 | transport_name, tdesc, action, err)); |
| 167 | } |
| 168 | |
| 169 | bool TransportController::SetRemoteTransportDescription( |
| 170 | const std::string& transport_name, |
| 171 | const TransportDescription& tdesc, |
| 172 | ContentAction action, |
| 173 | std::string* err) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 174 | return network_thread_->Invoke<bool>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 175 | RTC_FROM_HERE, |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 176 | rtc::Bind(&TransportController::SetRemoteTransportDescription_n, this, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 177 | transport_name, tdesc, action, err)); |
| 178 | } |
| 179 | |
| 180 | void TransportController::MaybeStartGathering() { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 181 | network_thread_->Invoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 182 | RTC_FROM_HERE, |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 183 | rtc::Bind(&TransportController::MaybeStartGathering_n, this)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | bool TransportController::AddRemoteCandidates(const std::string& transport_name, |
| 187 | const Candidates& candidates, |
| 188 | std::string* err) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 189 | return network_thread_->Invoke<bool>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 190 | RTC_FROM_HERE, rtc::Bind(&TransportController::AddRemoteCandidates_n, |
| 191 | this, transport_name, candidates, err)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 194 | bool TransportController::RemoveRemoteCandidates(const Candidates& candidates, |
| 195 | std::string* err) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 196 | return network_thread_->Invoke<bool>( |
| 197 | RTC_FROM_HERE, rtc::Bind(&TransportController::RemoveRemoteCandidates_n, |
| 198 | this, candidates, err)); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 199 | } |
| 200 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 201 | bool TransportController::ReadyForRemoteCandidates( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 202 | const std::string& transport_name) const { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 203 | return network_thread_->Invoke<bool>( |
| 204 | RTC_FROM_HERE, rtc::Bind(&TransportController::ReadyForRemoteCandidates_n, |
| 205 | this, transport_name)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | bool TransportController::GetStats(const std::string& transport_name, |
| 209 | TransportStats* stats) { |
hbos | df6075a | 2016-12-19 04:58:02 -0800 | [diff] [blame] | 210 | if (network_thread_->IsCurrent()) { |
| 211 | return GetStats_n(transport_name, stats); |
| 212 | } |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 213 | return network_thread_->Invoke<bool>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 214 | RTC_FROM_HERE, |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 215 | rtc::Bind(&TransportController::GetStats_n, this, transport_name, stats)); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 216 | } |
| 217 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 218 | void 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 | |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 225 | DtlsTransportInternal* TransportController::CreateDtlsTransport( |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 226 | const std::string& transport_name, |
| 227 | int component) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 228 | return network_thread_->Invoke<DtlsTransportInternal*>( |
| 229 | RTC_FROM_HERE, rtc::Bind(&TransportController::CreateDtlsTransport_n, |
zhihuang | f5b251b | 2017-01-12 19:37:48 -0800 | [diff] [blame] | 230 | this, transport_name, component)); |
| 231 | } |
| 232 | |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 233 | DtlsTransportInternal* TransportController::CreateDtlsTransport_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 234 | const std::string& transport_name, |
| 235 | int component) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 236 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 237 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 238 | RefCountedChannel* existing_channel = GetChannel_n(transport_name, component); |
| 239 | if (existing_channel) { |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 240 | // Channel already exists; increment reference count and return. |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 241 | existing_channel->AddRef(); |
| 242 | return existing_channel->dtls(); |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | // Need to create a new channel. |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 246 | JsepTransport* transport = GetOrCreateJsepTransport(transport_name); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 247 | |
| 248 | // Create DTLS channel wrapping ICE channel, and configure it. |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 249 | IceTransportInternal* ice = |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 250 | CreateIceTransportChannel_n(transport_name, component); |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 251 | DtlsTransportInternal* dtls = |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 252 | CreateDtlsTransportChannel_n(transport_name, component, ice); |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 253 | 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_); |
zhihuang | b19012e | 2017-09-19 13:47:59 -0700 | [diff] [blame] | 257 | if (certificate_) { |
| 258 | bool set_cert_success = dtls->SetLocalCertificate(certificate_); |
| 259 | RTC_DCHECK(set_cert_success); |
| 260 | } |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 261 | |
| 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 Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 266 | this, &TransportController::OnChannelWritableState_n); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 267 | dtls->SignalReceivingState.connect( |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 268 | this, &TransportController::OnChannelReceivingState_n); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 269 | dtls->SignalDtlsHandshakeError.connect( |
zhihuang | d82eee0 | 2016-08-26 11:25:05 -0700 | [diff] [blame] | 270 | this, &TransportController::OnDtlsHandshakeError); |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 271 | 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); |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 281 | RefCountedChannel* new_pair = new RefCountedChannel(dtls, ice); |
| 282 | new_pair->AddRef(); |
| 283 | channels_.insert(channels_.end(), new_pair); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 284 | bool channel_added = transport->AddChannel(dtls, component); |
| 285 | RTC_DCHECK(channel_added); |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 286 | // Adding a channel could cause aggregate state to change. |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 287 | UpdateAggregateStates_n(); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 288 | return dtls; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 289 | } |
| 290 | |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 291 | void TransportController::DestroyDtlsTransport( |
deadbeef | bad5dad | 2017-01-17 18:32:35 -0800 | [diff] [blame] | 292 | const std::string& transport_name, |
| 293 | int component) { |
| 294 | network_thread_->Invoke<void>( |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 295 | RTC_FROM_HERE, rtc::Bind(&TransportController::DestroyDtlsTransport_n, |
deadbeef | bad5dad | 2017-01-17 18:32:35 -0800 | [diff] [blame] | 296 | this, transport_name, component)); |
| 297 | } |
| 298 | |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 299 | void TransportController::DestroyDtlsTransport_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 300 | const std::string& transport_name, |
| 301 | int component) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 302 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 303 | auto it = GetChannelIterator_n(transport_name, component); |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 304 | if (it == channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 305 | RTC_LOG(LS_WARNING) << "Attempting to delete " << transport_name |
| 306 | << " TransportChannel " << component |
| 307 | << ", which doesn't exist."; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 308 | return; |
| 309 | } |
Niels Möller | 6f72f56 | 2017-10-19 13:15:17 +0200 | [diff] [blame] | 310 | // 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 Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 314 | return; |
| 315 | } |
deadbeef | 57fd726 | 2016-12-06 15:28:55 -0800 | [diff] [blame] | 316 | channels_.erase(it); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 317 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 318 | JsepTransport* t = GetJsepTransport(transport_name); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 319 | bool channel_removed = t->RemoveChannel(component); |
| 320 | RTC_DCHECK(channel_removed); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 321 | // Just as we create a Transport when its first channel is created, |
| 322 | // we delete it when its last channel is deleted. |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 323 | if (!t->HasChannels()) { |
| 324 | transports_.erase(transport_name); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 325 | } |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 326 | // Removing a channel could cause aggregate state to change. |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 327 | UpdateAggregateStates_n(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 328 | } |
| 329 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 330 | std::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); |
mikescarlett | e774867 | 2016-04-29 20:20:54 -0700 | [diff] [blame] | 334 | } |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 335 | return ret; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 336 | } |
| 337 | |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 338 | std::vector<DtlsTransportInternal*> |
| 339 | TransportController::channels_for_testing() { |
| 340 | std::vector<DtlsTransportInternal*> ret; |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 341 | for (RefCountedChannel* channel : channels_) { |
| 342 | ret.push_back(channel->dtls()); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 343 | } |
| 344 | return ret; |
| 345 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 346 | |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 347 | DtlsTransportInternal* TransportController::get_channel_for_testing( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 348 | const std::string& transport_name, |
| 349 | int component) { |
| 350 | RefCountedChannel* ch = GetChannel_n(transport_name, component); |
| 351 | return ch ? ch->dtls() : nullptr; |
| 352 | } |
| 353 | |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 354 | IceTransportInternal* TransportController::CreateIceTransportChannel_n( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 355 | const std::string& transport_name, |
| 356 | int component) { |
| 357 | return new P2PTransportChannel(transport_name, component, port_allocator_); |
| 358 | } |
| 359 | |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 360 | DtlsTransportInternal* TransportController::CreateDtlsTransportChannel_n( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 361 | const std::string&, |
| 362 | int, |
zhihuang | d06adf6 | 2017-01-12 15:58:31 -0800 | [diff] [blame] | 363 | IceTransportInternal* ice) { |
deadbeef | 7914b8c | 2017-04-21 03:23:33 -0700 | [diff] [blame] | 364 | DtlsTransport* dtls = new DtlsTransport(ice, crypto_options_); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 365 | dtls->SetSslMaxProtocolVersion(ssl_max_version_); |
| 366 | return dtls; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | void 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: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 401 | RTC_NOTREACHED(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 405 | std::vector<TransportController::RefCountedChannel*>::iterator |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 406 | TransportController::GetChannelIterator_n(const std::string& transport_name, |
| 407 | int component) { |
| 408 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 409 | 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 Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 415 | } |
| 416 | |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 417 | std::vector<TransportController::RefCountedChannel*>::const_iterator |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 418 | TransportController::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(), |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 423 | [transport_name, component](const RefCountedChannel* channel) { |
| 424 | return channel->dtls()->transport_name() == transport_name && |
| 425 | channel->dtls()->component() == component; |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 426 | }); |
| 427 | } |
| 428 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 429 | const JsepTransport* TransportController::GetJsepTransport( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 430 | const std::string& transport_name) const { |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 431 | auto it = transports_.find(transport_name); |
| 432 | return (it == transports_.end()) ? nullptr : it->second.get(); |
| 433 | } |
| 434 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 435 | JsepTransport* TransportController::GetJsepTransport( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 436 | const std::string& transport_name) { |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 437 | auto it = transports_.find(transport_name); |
| 438 | return (it == transports_.end()) ? nullptr : it->second.get(); |
| 439 | } |
| 440 | |
| 441 | const 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); |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 446 | return (it == channels_.end()) ? nullptr : *it; |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | TransportController::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); |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 454 | return (it == channels_.end()) ? nullptr : *it; |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 455 | } |
| 456 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 457 | JsepTransport* TransportController::GetOrCreateJsepTransport( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 458 | const std::string& transport_name) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 459 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 460 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 461 | JsepTransport* transport = GetJsepTransport(transport_name); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 462 | if (transport) { |
| 463 | return transport; |
| 464 | } |
| 465 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 466 | transport = new JsepTransport(transport_name, certificate_); |
| 467 | transports_[transport_name] = std::unique_ptr<JsepTransport>(transport); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 468 | return transport; |
| 469 | } |
| 470 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 471 | void TransportController::DestroyAllChannels_n() { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 472 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 473 | transports_.clear(); |
Niels Möller | 6f72f56 | 2017-10-19 13:15:17 +0200 | [diff] [blame] | 474 | // TODO(nisse): If |channels_| were a vector of scoped_refptr, we |
| 475 | // wouldn't need this strange hack. |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 476 | 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öller | 6f72f56 | 2017-10-19 13:15:17 +0200 | [diff] [blame] | 480 | while (channel->Release() == |
| 481 | rtc::RefCountReleaseStatus::kOtherRefsRemained) { |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 482 | } |
| 483 | } |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 484 | channels_.clear(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 487 | bool TransportController::SetSslMaxProtocolVersion_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 488 | rtc::SSLProtocolVersion version) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 489 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 490 | |
| 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 Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 500 | void TransportController::SetIceConfig_n(const IceConfig& config) { |
| 501 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 502 | |
honghaiz | 1f429e3 | 2015-09-28 07:57:34 -0700 | [diff] [blame] | 503 | ice_config_ = config; |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 504 | for (auto& channel : channels_) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 505 | channel->dtls()->ice_transport()->SetIceConfig(ice_config_); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 509 | void TransportController::SetIceRole_n(IceRole ice_role) { |
| 510 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 511 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 512 | ice_role_ = ice_role; |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 513 | for (auto& channel : channels_) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 514 | channel->dtls()->ice_transport()->SetIceRole(ice_role_); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 518 | bool TransportController::GetSslRole_n(const std::string& transport_name, |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 519 | rtc::SSLRole* role) const { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 520 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 521 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 522 | const JsepTransport* t = GetJsepTransport(transport_name); |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 523 | if (!t) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 524 | return false; |
| 525 | } |
deadbeef | d8cfa1a | 2017-03-27 10:33:26 -0700 | [diff] [blame] | 526 | rtc::Optional<rtc::SSLRole> current_role = t->GetSslRole(); |
| 527 | if (!current_role) { |
| 528 | return false; |
| 529 | } |
| 530 | *role = *current_role; |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 531 | return true; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 534 | bool TransportController::SetLocalCertificate_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 535 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 536 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 537 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 538 | // Can't change a certificate, or set a null certificate. |
| 539 | if (certificate_ || !certificate) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 540 | return false; |
| 541 | } |
| 542 | certificate_ = certificate; |
| 543 | |
deadbeef | 8662f94 | 2017-01-20 21:20:51 -0800 | [diff] [blame] | 544 | // Set certificate for JsepTransport, which verifies it matches the |
zhihuang | b19012e | 2017-09-19 13:47:59 -0700 | [diff] [blame] | 545 | // fingerprint in SDP, and DTLS transport. |
| 546 | // Fallback from DTLS to SDES is not supported. |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 547 | for (auto& kv : transports_) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 548 | kv.second->SetLocalCertificate(certificate_); |
| 549 | } |
zhihuang | b19012e | 2017-09-19 13:47:59 -0700 | [diff] [blame] | 550 | for (auto& channel : channels_) { |
| 551 | bool set_cert_success = channel->dtls()->SetLocalCertificate(certificate_); |
| 552 | RTC_DCHECK(set_cert_success); |
| 553 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 554 | return true; |
| 555 | } |
| 556 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 557 | bool TransportController::GetLocalCertificate_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 558 | const std::string& transport_name, |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 559 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) const { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 560 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 561 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 562 | const JsepTransport* t = GetJsepTransport(transport_name); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 563 | if (!t) { |
| 564 | return false; |
| 565 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 566 | return t->GetLocalCertificate(certificate); |
| 567 | } |
| 568 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 569 | std::unique_ptr<rtc::SSLCertificate> |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 570 | TransportController::GetRemoteSSLCertificate_n( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 571 | const std::string& transport_name) const { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 572 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 573 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 574 | // 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) { |
kwiberg | b4d01c4 | 2016-04-06 05:15:06 -0700 | [diff] [blame] | 579 | return nullptr; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 580 | } |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 581 | return ch->dtls()->GetRemoteSSLCertificate(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 584 | bool TransportController::SetLocalTransportDescription_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 585 | const std::string& transport_name, |
| 586 | const TransportDescription& tdesc, |
| 587 | ContentAction action, |
| 588 | std::string* err) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 589 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 590 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 591 | JsepTransport* transport = GetJsepTransport(transport_name); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 592 | 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 | |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 600 | // 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 Brandstetter | f0bb360 | 2016-08-26 20:59:24 -0700 | [diff] [blame] | 602 | // 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. |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 604 | // 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 Brandstetter | f0bb360 | 2016-08-26 20:59:24 -0700 | [diff] [blame] | 607 | if (redetermine_role_on_ice_restart_ && transport->local_description() && |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 608 | IceCredentialsChanged(transport->local_description()->ice_ufrag, |
| 609 | transport->local_description()->ice_pwd, |
deadbeef | 897d08e | 2017-04-20 00:57:25 -0700 | [diff] [blame] | 610 | 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)) { |
deadbeef | 91042f8 | 2016-07-15 17:48:13 -0700 | [diff] [blame] | 615 | IceRole new_ice_role = |
| 616 | (action == CA_OFFER) ? ICEROLE_CONTROLLING : ICEROLE_CONTROLLED; |
| 617 | SetIceRole(new_ice_role); |
| 618 | } |
| 619 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 620 | RTC_LOG(LS_INFO) << "Set local transport description on " << transport_name; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 621 | return transport->SetLocalTransportDescription(tdesc, action, err); |
| 622 | } |
| 623 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 624 | bool TransportController::SetRemoteTransportDescription_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 625 | const std::string& transport_name, |
| 626 | const TransportDescription& tdesc, |
| 627 | ContentAction action, |
| 628 | std::string* err) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 629 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 630 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 631 | // 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 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 639 | JsepTransport* transport = GetJsepTransport(transport_name); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 640 | 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 Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 648 | RTC_LOG(LS_INFO) << "Set remote transport description on " << transport_name; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 649 | return transport->SetRemoteTransportDescription(tdesc, action, err); |
| 650 | } |
| 651 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 652 | void TransportController::MaybeStartGathering_n() { |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 653 | for (auto& channel : channels_) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 654 | channel->dtls()->ice_transport()->MaybeStartGathering(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 658 | bool TransportController::AddRemoteCandidates_n( |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 659 | const std::string& transport_name, |
| 660 | const Candidates& candidates, |
| 661 | std::string* err) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 662 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 663 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 664 | // Verify each candidate before passing down to the transport layer. |
| 665 | if (!VerifyCandidates(candidates, err)) { |
| 666 | return false; |
| 667 | } |
| 668 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 669 | JsepTransport* transport = GetJsepTransport(transport_name); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 670 | 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 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 676 | 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 | } |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 684 | channel->dtls()->ice_transport()->AddRemoteCandidate(candidate); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 685 | } |
| 686 | return true; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 689 | bool TransportController::RemoveRemoteCandidates_n(const Candidates& candidates, |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 690 | std::string* err) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 691 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 692 | |
| 693 | // Verify each candidate before passing down to the transport layer. |
| 694 | if (!VerifyCandidates(candidates, err)) { |
| 695 | return false; |
| 696 | } |
| 697 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 698 | std::map<std::string, Candidates> candidates_by_transport_name; |
| 699 | for (const Candidate& cand : candidates) { |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 700 | if (!cand.transport_name().empty()) { |
| 701 | candidates_by_transport_name[cand.transport_name()].push_back(cand); |
| 702 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 703 | RTC_LOG(LS_ERROR) << "Not removing candidate because it does not have a " |
| 704 | "transport name set: " |
| 705 | << cand.ToString(); |
Steve Anton | f1c6db1 | 2017-10-13 11:13:35 -0700 | [diff] [blame] | 706 | } |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | bool result = true; |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 710 | for (const auto& kv : candidates_by_transport_name) { |
| 711 | const std::string& transport_name = kv.first; |
| 712 | const Candidates& candidates = kv.second; |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 713 | JsepTransport* transport = GetJsepTransport(transport_name); |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 714 | 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 | } |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 719 | for (const Candidate& candidate : candidates) { |
| 720 | RefCountedChannel* channel = |
| 721 | GetChannel_n(transport_name, candidate.component()); |
| 722 | if (channel) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 723 | channel->dtls()->ice_transport()->RemoveRemoteCandidate(candidate); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 724 | } |
| 725 | } |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 726 | } |
| 727 | return result; |
| 728 | } |
| 729 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 730 | bool TransportController::ReadyForRemoteCandidates_n( |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 731 | const std::string& transport_name) const { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 732 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 733 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 734 | const JsepTransport* transport = GetJsepTransport(transport_name); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 735 | if (!transport) { |
| 736 | return false; |
| 737 | } |
| 738 | return transport->ready_for_remote_candidates(); |
| 739 | } |
| 740 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 741 | bool TransportController::GetStats_n(const std::string& transport_name, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 742 | TransportStats* stats) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 743 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 744 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 745 | JsepTransport* transport = GetJsepTransport(transport_name); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 746 | if (!transport) { |
| 747 | return false; |
| 748 | } |
| 749 | return transport->GetStats(stats); |
| 750 | } |
| 751 | |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 752 | void TransportController::SetMetricsObserver_n( |
| 753 | webrtc::MetricsObserverInterface* metrics_observer) { |
| 754 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 755 | metrics_observer_ = metrics_observer; |
| 756 | for (auto& channel : channels_) { |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 757 | channel->dtls()->ice_transport()->SetMetricsObserver(metrics_observer); |
deadbeef | 49f34fd | 2016-12-06 16:22:06 -0800 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | |
johan | d89ab14 | 2016-10-25 10:50:32 -0700 | [diff] [blame] | 761 | void TransportController::OnChannelWritableState_n( |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 762 | rtc::PacketTransportInternal* transport) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 763 | RTC_DCHECK(network_thread_->IsCurrent()); |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 764 | RTC_LOG(LS_INFO) << " Transport " << transport->transport_name() |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 765 | << " writability changed to " << transport->writable() |
| 766 | << "."; |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 767 | UpdateAggregateStates_n(); |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 768 | } |
| 769 | |
johan | 15ca8f6 | 2016-11-01 01:47:41 -0700 | [diff] [blame] | 770 | void TransportController::OnChannelReceivingState_n( |
deadbeef | 5bd5ca3 | 2017-02-10 11:31:50 -0800 | [diff] [blame] | 771 | rtc::PacketTransportInternal* transport) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 772 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 773 | UpdateAggregateStates_n(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 776 | void TransportController::OnChannelGatheringState_n( |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 777 | IceTransportInternal* channel) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 778 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 779 | UpdateAggregateStates_n(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 782 | void TransportController::OnChannelCandidateGathered_n( |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 783 | IceTransportInternal* channel, |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 784 | const Candidate& candidate) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 785 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 786 | |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 787 | // We should never signal peer-reflexive candidates. |
| 788 | if (candidate.type() == PRFLX_PORT_TYPE) { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 789 | RTC_NOTREACHED(); |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 790 | return; |
| 791 | } |
| 792 | std::vector<Candidate> candidates; |
| 793 | candidates.push_back(candidate); |
| 794 | CandidatesData* data = |
| 795 | new CandidatesData(channel->transport_name(), candidates); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 796 | signaling_thread_->Post(RTC_FROM_HERE, this, MSG_CANDIDATESGATHERED, data); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 799 | void TransportController::OnChannelCandidatesRemoved_n( |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 800 | IceTransportInternal* channel, |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 801 | const Candidates& candidates) { |
| 802 | invoker_.AsyncInvoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 803 | RTC_FROM_HERE, signaling_thread_, |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 804 | rtc::Bind(&TransportController::OnChannelCandidatesRemoved, this, |
| 805 | candidates)); |
| 806 | } |
| 807 | |
| 808 | void TransportController::OnChannelCandidatesRemoved( |
| 809 | const Candidates& candidates) { |
| 810 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 811 | SignalCandidatesRemoved(candidates); |
| 812 | } |
| 813 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 814 | void TransportController::OnChannelRoleConflict_n( |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 815 | IceTransportInternal* channel) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 816 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | 1c20610 | 2016-05-27 13:34:37 -0700 | [diff] [blame] | 817 | // 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. |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 820 | IceRole reversed_role = (ice_role_ == ICEROLE_CONTROLLING) |
| 821 | ? ICEROLE_CONTROLLED |
| 822 | : ICEROLE_CONTROLLING; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 823 | RTC_LOG(LS_INFO) << "Got role conflict; switching to " |
| 824 | << (reversed_role == ICEROLE_CONTROLLING ? "controlling" |
| 825 | : "controlled") |
| 826 | << " role."; |
deadbeef | 1c20610 | 2016-05-27 13:34:37 -0700 | [diff] [blame] | 827 | SetIceRole_n(reversed_role); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 830 | void TransportController::OnChannelStateChanged_n( |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 831 | IceTransportInternal* channel) { |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 832 | RTC_DCHECK(network_thread_->IsCurrent()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 833 | RTC_LOG(LS_INFO) << channel->transport_name() << " TransportChannel " |
| 834 | << channel->component() |
| 835 | << " state changed. Check if state is complete."; |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 836 | UpdateAggregateStates_n(); |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 837 | } |
| 838 | |
Danil Chapovalov | 7f216b7 | 2016-05-12 09:20:31 +0200 | [diff] [blame] | 839 | void TransportController::UpdateAggregateStates_n() { |
| 840 | RTC_DCHECK(network_thread_->IsCurrent()); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 841 | |
| 842 | IceConnectionState new_connection_state = kIceConnectionConnecting; |
| 843 | IceGatheringState new_gathering_state = kIceGatheringNew; |
| 844 | bool any_receiving = false; |
| 845 | bool any_failed = false; |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 846 | bool all_connected = !channels_.empty(); |
| 847 | bool all_completed = !channels_.empty(); |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 848 | bool any_gathering = false; |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 849 | bool all_done_gathering = !channels_.empty(); |
| 850 | for (const auto& channel : channels_) { |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 851 | any_receiving = any_receiving || channel->dtls()->receiving(); |
Zhi Huang | b526158 | 2017-09-29 10:51:43 -0700 | [diff] [blame] | 852 | any_failed = any_failed || channel->dtls()->ice_transport()->GetState() == |
| 853 | IceTransportState::STATE_FAILED; |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 854 | all_connected = all_connected && channel->dtls()->writable(); |
Taylor Brandstetter | c4d3a5d | 2015-09-30 10:32:59 -0700 | [diff] [blame] | 855 | all_completed = |
deadbeef | 62802a1 | 2016-12-13 16:38:36 -0800 | [diff] [blame] | 856 | all_completed && channel->dtls()->writable() && |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 857 | 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; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 862 | any_gathering = |
zhihuang | b2cdd93 | 2017-01-19 16:54:25 -0800 | [diff] [blame] | 863 | 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; |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 868 | } |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 869 | 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 Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 879 | RTC_FROM_HERE, this, MSG_ICECONNECTIONSTATE, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 880 | new rtc::TypedMessageData<IceConnectionState>(new_connection_state)); |
| 881 | } |
| 882 | |
| 883 | if (receiving_ != any_receiving) { |
| 884 | receiving_ = any_receiving; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 885 | signaling_thread_->Post(RTC_FROM_HERE, this, MSG_RECEIVING, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 886 | 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 Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 897 | RTC_FROM_HERE, this, MSG_ICEGATHERINGSTATE, |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 898 | new rtc::TypedMessageData<IceGatheringState>(new_gathering_state)); |
| 899 | } |
| 900 | } |
| 901 | |
zhihuang | d82eee0 | 2016-08-26 11:25:05 -0700 | [diff] [blame] | 902 | void TransportController::OnDtlsHandshakeError(rtc::SSLHandshakeError error) { |
| 903 | SignalDtlsHandshakeError(error); |
| 904 | } |
| 905 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 906 | } // namespace cricket |