Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "pc/jsep_transport_controller.h" |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
| 14 | #include <memory> |
| 15 | #include <utility> |
| 16 | |
Zach Stein | c64078f | 2018-11-27 15:53:01 -0800 | [diff] [blame] | 17 | #include "absl/memory/memory.h" |
Piotr (Peter) Slatala | 2b5baee | 2019-01-16 08:25:21 -0800 | [diff] [blame] | 18 | #include "p2p/base/ice_transport_internal.h" |
| 19 | #include "p2p/base/no_op_dtls_transport.h" |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 20 | #include "p2p/base/port.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "pc/srtp_filter.h" |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 22 | #include "rtc_base/bind.h" |
| 23 | #include "rtc_base/checks.h" |
Piotr (Peter) Slatala | 9f95625 | 2018-10-31 08:25:26 -0700 | [diff] [blame] | 24 | #include "rtc_base/key_derivation.h" |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 25 | #include "rtc_base/thread.h" |
| 26 | |
| 27 | using webrtc::SdpType; |
| 28 | |
| 29 | namespace { |
| 30 | |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 31 | webrtc::RTCError VerifyCandidate(const cricket::Candidate& cand) { |
| 32 | // No address zero. |
| 33 | if (cand.address().IsNil() || cand.address().IsAnyIP()) { |
| 34 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 35 | "candidate has address of zero"); |
| 36 | } |
| 37 | |
| 38 | // Disallow all ports below 1024, except for 80 and 443 on public addresses. |
| 39 | int port = cand.address().port(); |
| 40 | if (cand.protocol() == cricket::TCP_PROTOCOL_NAME && |
| 41 | (cand.tcptype() == cricket::TCPTYPE_ACTIVE_STR || port == 0)) { |
| 42 | // Expected for active-only candidates per |
| 43 | // http://tools.ietf.org/html/rfc6544#section-4.5 so no error. |
| 44 | // Libjingle clients emit port 0, in "active" mode. |
| 45 | return webrtc::RTCError::OK(); |
| 46 | } |
| 47 | if (port < 1024) { |
| 48 | if ((port != 80) && (port != 443)) { |
| 49 | return webrtc::RTCError( |
| 50 | webrtc::RTCErrorType::INVALID_PARAMETER, |
| 51 | "candidate has port below 1024, but not 80 or 443"); |
| 52 | } |
| 53 | |
| 54 | if (cand.address().IsPrivateIP()) { |
| 55 | return webrtc::RTCError( |
| 56 | webrtc::RTCErrorType::INVALID_PARAMETER, |
| 57 | "candidate has port of 80 or 443 with private IP address"); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return webrtc::RTCError::OK(); |
| 62 | } |
| 63 | |
| 64 | webrtc::RTCError VerifyCandidates(const cricket::Candidates& candidates) { |
| 65 | for (const cricket::Candidate& candidate : candidates) { |
| 66 | webrtc::RTCError error = VerifyCandidate(candidate); |
| 67 | if (!error.ok()) { |
| 68 | return error; |
| 69 | } |
| 70 | } |
| 71 | return webrtc::RTCError::OK(); |
| 72 | } |
| 73 | |
| 74 | } // namespace |
| 75 | |
| 76 | namespace webrtc { |
| 77 | |
| 78 | JsepTransportController::JsepTransportController( |
| 79 | rtc::Thread* signaling_thread, |
| 80 | rtc::Thread* network_thread, |
| 81 | cricket::PortAllocator* port_allocator, |
Zach Stein | e20867f | 2018-08-02 13:20:15 -0700 | [diff] [blame] | 82 | AsyncResolverFactory* async_resolver_factory, |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 83 | Config config) |
| 84 | : signaling_thread_(signaling_thread), |
| 85 | network_thread_(network_thread), |
| 86 | port_allocator_(port_allocator), |
Zach Stein | e20867f | 2018-08-02 13:20:15 -0700 | [diff] [blame] | 87 | async_resolver_factory_(async_resolver_factory), |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 88 | config_(config) { |
| 89 | // The |transport_observer| is assumed to be non-null. |
| 90 | RTC_DCHECK(config_.transport_observer); |
| 91 | } |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 92 | |
| 93 | JsepTransportController::~JsepTransportController() { |
| 94 | // Channel destructors may try to send packets, so this needs to happen on |
| 95 | // the network thread. |
| 96 | network_thread_->Invoke<void>( |
| 97 | RTC_FROM_HERE, |
| 98 | rtc::Bind(&JsepTransportController::DestroyAllJsepTransports_n, this)); |
| 99 | } |
| 100 | |
| 101 | RTCError JsepTransportController::SetLocalDescription( |
| 102 | SdpType type, |
| 103 | const cricket::SessionDescription* description) { |
| 104 | if (!network_thread_->IsCurrent()) { |
| 105 | return network_thread_->Invoke<RTCError>( |
| 106 | RTC_FROM_HERE, [=] { return SetLocalDescription(type, description); }); |
| 107 | } |
| 108 | |
| 109 | if (!initial_offerer_.has_value()) { |
| 110 | initial_offerer_.emplace(type == SdpType::kOffer); |
| 111 | if (*initial_offerer_) { |
| 112 | SetIceRole_n(cricket::ICEROLE_CONTROLLING); |
| 113 | } else { |
| 114 | SetIceRole_n(cricket::ICEROLE_CONTROLLED); |
| 115 | } |
| 116 | } |
| 117 | return ApplyDescription_n(/*local=*/true, type, description); |
| 118 | } |
| 119 | |
| 120 | RTCError JsepTransportController::SetRemoteDescription( |
| 121 | SdpType type, |
| 122 | const cricket::SessionDescription* description) { |
| 123 | if (!network_thread_->IsCurrent()) { |
| 124 | return network_thread_->Invoke<RTCError>( |
| 125 | RTC_FROM_HERE, [=] { return SetRemoteDescription(type, description); }); |
| 126 | } |
| 127 | |
| 128 | return ApplyDescription_n(/*local=*/false, type, description); |
| 129 | } |
| 130 | |
| 131 | RtpTransportInternal* JsepTransportController::GetRtpTransport( |
| 132 | const std::string& mid) const { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 133 | auto jsep_transport = GetJsepTransportForMid(mid); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 134 | if (!jsep_transport) { |
| 135 | return nullptr; |
| 136 | } |
| 137 | return jsep_transport->rtp_transport(); |
| 138 | } |
| 139 | |
Anton Sukhanov | 7940da0 | 2018-10-10 10:34:49 -0700 | [diff] [blame] | 140 | MediaTransportInterface* JsepTransportController::GetMediaTransport( |
| 141 | const std::string& mid) const { |
| 142 | auto jsep_transport = GetJsepTransportForMid(mid); |
| 143 | if (!jsep_transport) { |
| 144 | return nullptr; |
| 145 | } |
| 146 | return jsep_transport->media_transport(); |
| 147 | } |
| 148 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 149 | MediaTransportState JsepTransportController::GetMediaTransportState( |
| 150 | const std::string& mid) const { |
| 151 | auto jsep_transport = GetJsepTransportForMid(mid); |
| 152 | if (!jsep_transport) { |
| 153 | return MediaTransportState::kPending; |
| 154 | } |
| 155 | return jsep_transport->media_transport_state(); |
| 156 | } |
| 157 | |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 158 | cricket::DtlsTransportInternal* JsepTransportController::GetDtlsTransport( |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 159 | const std::string& mid) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 160 | auto jsep_transport = GetJsepTransportForMid(mid); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 161 | if (!jsep_transport) { |
| 162 | return nullptr; |
| 163 | } |
| 164 | return jsep_transport->rtp_dtls_transport(); |
| 165 | } |
| 166 | |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 167 | const cricket::DtlsTransportInternal* |
| 168 | JsepTransportController::GetRtcpDtlsTransport(const std::string& mid) const { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 169 | auto jsep_transport = GetJsepTransportForMid(mid); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 170 | if (!jsep_transport) { |
| 171 | return nullptr; |
| 172 | } |
| 173 | return jsep_transport->rtcp_dtls_transport(); |
| 174 | } |
| 175 | |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 176 | rtc::scoped_refptr<webrtc::DtlsTransport> |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 177 | JsepTransportController::LookupDtlsTransportByMid(const std::string& mid) { |
| 178 | auto jsep_transport = GetJsepTransportForMid(mid); |
| 179 | if (!jsep_transport) { |
| 180 | return nullptr; |
| 181 | } |
| 182 | return jsep_transport->RtpDtlsTransport(); |
| 183 | } |
| 184 | |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 185 | void JsepTransportController::SetIceConfig(const cricket::IceConfig& config) { |
| 186 | if (!network_thread_->IsCurrent()) { |
| 187 | network_thread_->Invoke<void>(RTC_FROM_HERE, [&] { SetIceConfig(config); }); |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | ice_config_ = config; |
| 192 | for (auto& dtls : GetDtlsTransports()) { |
| 193 | dtls->ice_transport()->SetIceConfig(ice_config_); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void JsepTransportController::SetNeedsIceRestartFlag() { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 198 | for (auto& kv : jsep_transports_by_name_) { |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 199 | kv.second->SetNeedsIceRestartFlag(); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | bool JsepTransportController::NeedsIceRestart( |
| 204 | const std::string& transport_name) const { |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 205 | const cricket::JsepTransport* transport = |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 206 | GetJsepTransportByName(transport_name); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 207 | if (!transport) { |
| 208 | return false; |
| 209 | } |
| 210 | return transport->needs_ice_restart(); |
| 211 | } |
| 212 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 213 | absl::optional<rtc::SSLRole> JsepTransportController::GetDtlsRole( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 214 | const std::string& mid) const { |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 215 | if (!network_thread_->IsCurrent()) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 216 | return network_thread_->Invoke<absl::optional<rtc::SSLRole>>( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 217 | RTC_FROM_HERE, [&] { return GetDtlsRole(mid); }); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 220 | const cricket::JsepTransport* t = GetJsepTransportForMid(mid); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 221 | if (!t) { |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 222 | return absl::optional<rtc::SSLRole>(); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 223 | } |
| 224 | return t->GetDtlsRole(); |
| 225 | } |
| 226 | |
| 227 | bool JsepTransportController::SetLocalCertificate( |
| 228 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 229 | if (!network_thread_->IsCurrent()) { |
| 230 | return network_thread_->Invoke<bool>( |
| 231 | RTC_FROM_HERE, [&] { return SetLocalCertificate(certificate); }); |
| 232 | } |
| 233 | |
| 234 | // Can't change a certificate, or set a null certificate. |
| 235 | if (certificate_ || !certificate) { |
| 236 | return false; |
| 237 | } |
| 238 | certificate_ = certificate; |
| 239 | |
| 240 | // Set certificate for JsepTransport, which verifies it matches the |
| 241 | // fingerprint in SDP, and DTLS transport. |
| 242 | // Fallback from DTLS to SDES is not supported. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 243 | for (auto& kv : jsep_transports_by_name_) { |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 244 | kv.second->SetLocalCertificate(certificate_); |
| 245 | } |
| 246 | for (auto& dtls : GetDtlsTransports()) { |
| 247 | bool set_cert_success = dtls->SetLocalCertificate(certificate_); |
| 248 | RTC_DCHECK(set_cert_success); |
| 249 | } |
| 250 | return true; |
| 251 | } |
| 252 | |
| 253 | rtc::scoped_refptr<rtc::RTCCertificate> |
| 254 | JsepTransportController::GetLocalCertificate( |
| 255 | const std::string& transport_name) const { |
| 256 | if (!network_thread_->IsCurrent()) { |
| 257 | return network_thread_->Invoke<rtc::scoped_refptr<rtc::RTCCertificate>>( |
| 258 | RTC_FROM_HERE, [&] { return GetLocalCertificate(transport_name); }); |
| 259 | } |
| 260 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 261 | const cricket::JsepTransport* t = GetJsepTransportByName(transport_name); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 262 | if (!t) { |
| 263 | return nullptr; |
| 264 | } |
| 265 | return t->GetLocalCertificate(); |
| 266 | } |
| 267 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 268 | std::unique_ptr<rtc::SSLCertChain> |
| 269 | JsepTransportController::GetRemoteSSLCertChain( |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 270 | const std::string& transport_name) const { |
| 271 | if (!network_thread_->IsCurrent()) { |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 272 | return network_thread_->Invoke<std::unique_ptr<rtc::SSLCertChain>>( |
| 273 | RTC_FROM_HERE, [&] { return GetRemoteSSLCertChain(transport_name); }); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 276 | // Get the certificate from the RTP transport's DTLS handshake. Should be |
| 277 | // identical to the RTCP transport's, since they were given the same remote |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 278 | // fingerprint. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 279 | auto jsep_transport = GetJsepTransportByName(transport_name); |
| 280 | if (!jsep_transport) { |
| 281 | return nullptr; |
| 282 | } |
| 283 | auto dtls = jsep_transport->rtp_dtls_transport(); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 284 | if (!dtls) { |
| 285 | return nullptr; |
| 286 | } |
| 287 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 288 | return dtls->GetRemoteSSLCertChain(); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | void JsepTransportController::MaybeStartGathering() { |
| 292 | if (!network_thread_->IsCurrent()) { |
| 293 | network_thread_->Invoke<void>(RTC_FROM_HERE, |
| 294 | [&] { MaybeStartGathering(); }); |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | for (auto& dtls : GetDtlsTransports()) { |
| 299 | dtls->ice_transport()->MaybeStartGathering(); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | RTCError JsepTransportController::AddRemoteCandidates( |
| 304 | const std::string& transport_name, |
| 305 | const cricket::Candidates& candidates) { |
| 306 | if (!network_thread_->IsCurrent()) { |
| 307 | return network_thread_->Invoke<RTCError>(RTC_FROM_HERE, [&] { |
| 308 | return AddRemoteCandidates(transport_name, candidates); |
| 309 | }); |
| 310 | } |
| 311 | |
| 312 | // Verify each candidate before passing down to the transport layer. |
| 313 | RTCError error = VerifyCandidates(candidates); |
| 314 | if (!error.ok()) { |
| 315 | return error; |
| 316 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 317 | auto jsep_transport = GetJsepTransportByName(transport_name); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 318 | if (!jsep_transport) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 319 | RTC_LOG(LS_WARNING) << "Not adding candidate because the JsepTransport " |
| 320 | "doesn't exist. Ignore it."; |
| 321 | return RTCError::OK(); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 322 | } |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 323 | return jsep_transport->AddRemoteCandidates(candidates); |
| 324 | } |
| 325 | |
| 326 | RTCError JsepTransportController::RemoveRemoteCandidates( |
| 327 | const cricket::Candidates& candidates) { |
| 328 | if (!network_thread_->IsCurrent()) { |
| 329 | return network_thread_->Invoke<RTCError>( |
| 330 | RTC_FROM_HERE, [&] { return RemoveRemoteCandidates(candidates); }); |
| 331 | } |
| 332 | |
| 333 | // Verify each candidate before passing down to the transport layer. |
| 334 | RTCError error = VerifyCandidates(candidates); |
| 335 | if (!error.ok()) { |
| 336 | return error; |
| 337 | } |
| 338 | |
| 339 | std::map<std::string, cricket::Candidates> candidates_by_transport_name; |
| 340 | for (const cricket::Candidate& cand : candidates) { |
| 341 | if (!cand.transport_name().empty()) { |
| 342 | candidates_by_transport_name[cand.transport_name()].push_back(cand); |
| 343 | } else { |
| 344 | RTC_LOG(LS_ERROR) << "Not removing candidate because it does not have a " |
| 345 | "transport name set: " |
| 346 | << cand.ToString(); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | for (const auto& kv : candidates_by_transport_name) { |
| 351 | const std::string& transport_name = kv.first; |
| 352 | const cricket::Candidates& candidates = kv.second; |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 353 | cricket::JsepTransport* jsep_transport = |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 354 | GetJsepTransportByName(transport_name); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 355 | if (!jsep_transport) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 356 | RTC_LOG(LS_WARNING) |
| 357 | << "Not removing candidate because the JsepTransport doesn't exist."; |
| 358 | continue; |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 359 | } |
| 360 | for (const cricket::Candidate& candidate : candidates) { |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 361 | cricket::DtlsTransportInternal* dtls = |
| 362 | candidate.component() == cricket::ICE_CANDIDATE_COMPONENT_RTP |
| 363 | ? jsep_transport->rtp_dtls_transport() |
| 364 | : jsep_transport->rtcp_dtls_transport(); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 365 | if (dtls) { |
| 366 | dtls->ice_transport()->RemoveRemoteCandidate(candidate); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | return RTCError::OK(); |
| 371 | } |
| 372 | |
| 373 | bool JsepTransportController::GetStats(const std::string& transport_name, |
| 374 | cricket::TransportStats* stats) { |
| 375 | if (!network_thread_->IsCurrent()) { |
| 376 | return network_thread_->Invoke<bool>( |
| 377 | RTC_FROM_HERE, [=] { return GetStats(transport_name, stats); }); |
| 378 | } |
| 379 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 380 | cricket::JsepTransport* transport = GetJsepTransportByName(transport_name); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 381 | if (!transport) { |
| 382 | return false; |
| 383 | } |
| 384 | return transport->GetStats(stats); |
| 385 | } |
| 386 | |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 387 | void JsepTransportController::SetActiveResetSrtpParams( |
| 388 | bool active_reset_srtp_params) { |
| 389 | if (!network_thread_->IsCurrent()) { |
| 390 | network_thread_->Invoke<void>(RTC_FROM_HERE, [=] { |
| 391 | SetActiveResetSrtpParams(active_reset_srtp_params); |
| 392 | }); |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | RTC_LOG(INFO) |
| 397 | << "Updating the active_reset_srtp_params for JsepTransportController: " |
| 398 | << active_reset_srtp_params; |
| 399 | config_.active_reset_srtp_params = active_reset_srtp_params; |
| 400 | for (auto& kv : jsep_transports_by_name_) { |
| 401 | kv.second->SetActiveResetSrtpParams(active_reset_srtp_params); |
| 402 | } |
| 403 | } |
| 404 | |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 13:31:15 -0800 | [diff] [blame] | 405 | void JsepTransportController::SetMediaTransportSettings( |
| 406 | bool use_media_transport_for_media, |
| 407 | bool use_media_transport_for_data_channels) { |
| 408 | RTC_DCHECK(use_media_transport_for_media == |
| 409 | config_.use_media_transport_for_media || |
Piotr (Peter) Slatala | 97fc11f | 2018-10-18 12:57:59 -0700 | [diff] [blame] | 410 | jsep_transports_by_name_.empty()) |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 13:31:15 -0800 | [diff] [blame] | 411 | << "You can only change media transport configuration before creating " |
| 412 | "the first transport."; |
| 413 | |
| 414 | RTC_DCHECK(use_media_transport_for_data_channels == |
| 415 | config_.use_media_transport_for_data_channels || |
| 416 | jsep_transports_by_name_.empty()) |
| 417 | << "You can only change media transport configuration before creating " |
| 418 | "the first transport."; |
| 419 | |
| 420 | config_.use_media_transport_for_media = use_media_transport_for_media; |
| 421 | config_.use_media_transport_for_data_channels = |
| 422 | use_media_transport_for_data_channels; |
Piotr (Peter) Slatala | 97fc11f | 2018-10-18 12:57:59 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Piotr (Peter) Slatala | 2b5baee | 2019-01-16 08:25:21 -0800 | [diff] [blame] | 425 | std::unique_ptr<cricket::IceTransportInternal> |
| 426 | JsepTransportController::CreateIceTransport(const std::string transport_name, |
| 427 | bool rtcp) { |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 428 | int component = rtcp ? cricket::ICE_CANDIDATE_COMPONENT_RTCP |
| 429 | : cricket::ICE_CANDIDATE_COMPONENT_RTP; |
| 430 | |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 431 | if (config_.external_transport_factory) { |
Piotr (Peter) Slatala | 2b5baee | 2019-01-16 08:25:21 -0800 | [diff] [blame] | 432 | return config_.external_transport_factory->CreateIceTransport( |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 433 | transport_name, component); |
Piotr (Peter) Slatala | 2b5baee | 2019-01-16 08:25:21 -0800 | [diff] [blame] | 434 | } else { |
| 435 | return absl::make_unique<cricket::P2PTransportChannel>( |
| 436 | transport_name, component, port_allocator_, async_resolver_factory_, |
| 437 | config_.event_log); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | std::unique_ptr<cricket::DtlsTransportInternal> |
| 442 | JsepTransportController::CreateDtlsTransport( |
| 443 | std::unique_ptr<cricket::IceTransportInternal> ice) { |
| 444 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 445 | |
| 446 | std::unique_ptr<cricket::DtlsTransportInternal> dtls; |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 13:31:15 -0800 | [diff] [blame] | 447 | // If media transport is used for both media and data channels, |
| 448 | // then we don't need to create DTLS. |
| 449 | // Otherwise, DTLS is still created. |
| 450 | if (is_media_transport_factory_enabled_ && config_.media_transport_factory && |
| 451 | config_.use_media_transport_for_media && |
| 452 | config_.use_media_transport_for_data_channels) { |
Piotr (Peter) Slatala | 2b5baee | 2019-01-16 08:25:21 -0800 | [diff] [blame] | 453 | dtls = absl::make_unique<cricket::NoOpDtlsTransport>( |
| 454 | std::move(ice), config_.crypto_options); |
| 455 | } else if (config_.external_transport_factory) { |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 456 | dtls = config_.external_transport_factory->CreateDtlsTransport( |
| 457 | std::move(ice), config_.crypto_options); |
| 458 | } else { |
Zach Stein | c64078f | 2018-11-27 15:53:01 -0800 | [diff] [blame] | 459 | dtls = absl::make_unique<cricket::DtlsTransport>( |
| 460 | std::move(ice), config_.crypto_options, config_.event_log); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | RTC_DCHECK(dtls); |
| 464 | dtls->SetSslMaxProtocolVersion(config_.ssl_max_version); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 465 | dtls->ice_transport()->SetIceRole(ice_role_); |
| 466 | dtls->ice_transport()->SetIceTiebreaker(ice_tiebreaker_); |
| 467 | dtls->ice_transport()->SetIceConfig(ice_config_); |
| 468 | if (certificate_) { |
| 469 | bool set_cert_success = dtls->SetLocalCertificate(certificate_); |
| 470 | RTC_DCHECK(set_cert_success); |
| 471 | } |
| 472 | |
| 473 | // Connect to signals offered by the DTLS and ICE transport. |
| 474 | dtls->SignalWritableState.connect( |
| 475 | this, &JsepTransportController::OnTransportWritableState_n); |
| 476 | dtls->SignalReceivingState.connect( |
| 477 | this, &JsepTransportController::OnTransportReceivingState_n); |
| 478 | dtls->SignalDtlsHandshakeError.connect( |
| 479 | this, &JsepTransportController::OnDtlsHandshakeError); |
| 480 | dtls->ice_transport()->SignalGatheringState.connect( |
| 481 | this, &JsepTransportController::OnTransportGatheringState_n); |
| 482 | dtls->ice_transport()->SignalCandidateGathered.connect( |
| 483 | this, &JsepTransportController::OnTransportCandidateGathered_n); |
| 484 | dtls->ice_transport()->SignalCandidatesRemoved.connect( |
| 485 | this, &JsepTransportController::OnTransportCandidatesRemoved_n); |
| 486 | dtls->ice_transport()->SignalRoleConflict.connect( |
| 487 | this, &JsepTransportController::OnTransportRoleConflict_n); |
| 488 | dtls->ice_transport()->SignalStateChanged.connect( |
| 489 | this, &JsepTransportController::OnTransportStateChanged_n); |
Jonas Olsson | 7a6739e | 2019-01-15 16:31:55 +0100 | [diff] [blame] | 490 | dtls->ice_transport()->SignalIceTransportStateChanged.connect( |
| 491 | this, &JsepTransportController::OnTransportStateChanged_n); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 492 | return dtls; |
| 493 | } |
| 494 | |
| 495 | std::unique_ptr<webrtc::RtpTransport> |
| 496 | JsepTransportController::CreateUnencryptedRtpTransport( |
| 497 | const std::string& transport_name, |
| 498 | rtc::PacketTransportInternal* rtp_packet_transport, |
| 499 | rtc::PacketTransportInternal* rtcp_packet_transport) { |
| 500 | RTC_DCHECK(network_thread_->IsCurrent()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 501 | auto unencrypted_rtp_transport = |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 502 | absl::make_unique<RtpTransport>(rtcp_packet_transport == nullptr); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 503 | unencrypted_rtp_transport->SetRtpPacketTransport(rtp_packet_transport); |
| 504 | if (rtcp_packet_transport) { |
| 505 | unencrypted_rtp_transport->SetRtcpPacketTransport(rtcp_packet_transport); |
| 506 | } |
| 507 | return unencrypted_rtp_transport; |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | std::unique_ptr<webrtc::SrtpTransport> |
| 511 | JsepTransportController::CreateSdesTransport( |
| 512 | const std::string& transport_name, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 513 | cricket::DtlsTransportInternal* rtp_dtls_transport, |
| 514 | cricket::DtlsTransportInternal* rtcp_dtls_transport) { |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 515 | RTC_DCHECK(network_thread_->IsCurrent()); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 516 | auto srtp_transport = |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 517 | absl::make_unique<webrtc::SrtpTransport>(rtcp_dtls_transport == nullptr); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 518 | RTC_DCHECK(rtp_dtls_transport); |
| 519 | srtp_transport->SetRtpPacketTransport(rtp_dtls_transport); |
| 520 | if (rtcp_dtls_transport) { |
| 521 | srtp_transport->SetRtcpPacketTransport(rtcp_dtls_transport); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 522 | } |
| 523 | if (config_.enable_external_auth) { |
| 524 | srtp_transport->EnableExternalAuth(); |
| 525 | } |
| 526 | return srtp_transport; |
| 527 | } |
| 528 | |
| 529 | std::unique_ptr<webrtc::DtlsSrtpTransport> |
| 530 | JsepTransportController::CreateDtlsSrtpTransport( |
| 531 | const std::string& transport_name, |
| 532 | cricket::DtlsTransportInternal* rtp_dtls_transport, |
| 533 | cricket::DtlsTransportInternal* rtcp_dtls_transport) { |
| 534 | RTC_DCHECK(network_thread_->IsCurrent()); |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 535 | auto dtls_srtp_transport = absl::make_unique<webrtc::DtlsSrtpTransport>( |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 536 | rtcp_dtls_transport == nullptr); |
Zhi Huang | 27f3bf5 | 2018-03-26 21:37:23 -0700 | [diff] [blame] | 537 | if (config_.enable_external_auth) { |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 538 | dtls_srtp_transport->EnableExternalAuth(); |
Zhi Huang | 27f3bf5 | 2018-03-26 21:37:23 -0700 | [diff] [blame] | 539 | } |
Zhi Huang | 97d5e5b | 2018-03-27 00:09:01 +0000 | [diff] [blame] | 540 | |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 541 | dtls_srtp_transport->SetDtlsTransports(rtp_dtls_transport, |
| 542 | rtcp_dtls_transport); |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 543 | dtls_srtp_transport->SetActiveResetSrtpParams( |
| 544 | config_.active_reset_srtp_params); |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 545 | dtls_srtp_transport->SignalDtlsStateChange.connect( |
| 546 | this, &JsepTransportController::UpdateAggregateStates_n); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 547 | return dtls_srtp_transport; |
| 548 | } |
| 549 | |
| 550 | std::vector<cricket::DtlsTransportInternal*> |
| 551 | JsepTransportController::GetDtlsTransports() { |
| 552 | std::vector<cricket::DtlsTransportInternal*> dtls_transports; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 553 | for (auto it = jsep_transports_by_name_.begin(); |
| 554 | it != jsep_transports_by_name_.end(); ++it) { |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 555 | auto jsep_transport = it->second.get(); |
| 556 | RTC_DCHECK(jsep_transport); |
| 557 | if (jsep_transport->rtp_dtls_transport()) { |
| 558 | dtls_transports.push_back(jsep_transport->rtp_dtls_transport()); |
| 559 | } |
| 560 | |
| 561 | if (jsep_transport->rtcp_dtls_transport()) { |
| 562 | dtls_transports.push_back(jsep_transport->rtcp_dtls_transport()); |
| 563 | } |
| 564 | } |
| 565 | return dtls_transports; |
| 566 | } |
| 567 | |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 568 | RTCError JsepTransportController::ApplyDescription_n( |
| 569 | bool local, |
| 570 | SdpType type, |
| 571 | const cricket::SessionDescription* description) { |
| 572 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 573 | RTC_DCHECK(description); |
| 574 | |
| 575 | if (local) { |
| 576 | local_desc_ = description; |
| 577 | } else { |
| 578 | remote_desc_ = description; |
| 579 | } |
| 580 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 581 | RTCError error; |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 582 | error = ValidateAndMaybeUpdateBundleGroup(local, type, description); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 583 | if (!error.ok()) { |
| 584 | return error; |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | std::vector<int> merged_encrypted_extension_ids; |
| 588 | if (bundle_group_) { |
| 589 | merged_encrypted_extension_ids = |
| 590 | MergeEncryptedHeaderExtensionIdsForBundle(description); |
| 591 | } |
| 592 | |
| 593 | for (const cricket::ContentInfo& content_info : description->contents()) { |
| 594 | // Don't create transports for rejected m-lines and bundled m-lines." |
| 595 | if (content_info.rejected || |
| 596 | (IsBundled(content_info.name) && content_info.name != *bundled_mid())) { |
| 597 | continue; |
| 598 | } |
Anton Sukhanov | 7940da0 | 2018-10-10 10:34:49 -0700 | [diff] [blame] | 599 | error = MaybeCreateJsepTransport(local, content_info); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 600 | if (!error.ok()) { |
| 601 | return error; |
| 602 | } |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | RTC_DCHECK(description->contents().size() == |
| 606 | description->transport_infos().size()); |
| 607 | for (size_t i = 0; i < description->contents().size(); ++i) { |
| 608 | const cricket::ContentInfo& content_info = description->contents()[i]; |
| 609 | const cricket::TransportInfo& transport_info = |
| 610 | description->transport_infos()[i]; |
| 611 | if (content_info.rejected) { |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 612 | HandleRejectedContent(content_info, description); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 613 | continue; |
| 614 | } |
| 615 | |
| 616 | if (IsBundled(content_info.name) && content_info.name != *bundled_mid()) { |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 617 | if (!HandleBundledContent(content_info)) { |
| 618 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 619 | "Failed to process the bundled m= section."); |
| 620 | } |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 621 | continue; |
| 622 | } |
| 623 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 624 | error = ValidateContent(content_info); |
| 625 | if (!error.ok()) { |
| 626 | return error; |
| 627 | } |
| 628 | |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 629 | std::vector<int> extension_ids; |
Taylor Brandstetter | 0ab5651 | 2018-04-12 10:30:48 -0700 | [diff] [blame] | 630 | if (bundled_mid() && content_info.name == *bundled_mid()) { |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 631 | extension_ids = merged_encrypted_extension_ids; |
| 632 | } else { |
| 633 | extension_ids = GetEncryptedHeaderExtensionIds(content_info); |
| 634 | } |
| 635 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 636 | int rtp_abs_sendtime_extn_id = |
| 637 | GetRtpAbsSendTimeHeaderExtensionId(content_info); |
| 638 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 639 | cricket::JsepTransport* transport = |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 640 | GetJsepTransportForMid(content_info.name); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 641 | RTC_DCHECK(transport); |
| 642 | |
| 643 | SetIceRole_n(DetermineIceRole(transport, transport_info, type, local)); |
| 644 | |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 645 | cricket::JsepTransportDescription jsep_description = |
| 646 | CreateJsepTransportDescription(content_info, transport_info, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 647 | extension_ids, rtp_abs_sendtime_extn_id); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 648 | if (local) { |
| 649 | error = |
| 650 | transport->SetLocalJsepTransportDescription(jsep_description, type); |
| 651 | } else { |
| 652 | error = |
| 653 | transport->SetRemoteJsepTransportDescription(jsep_description, type); |
| 654 | } |
| 655 | |
| 656 | if (!error.ok()) { |
| 657 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 658 | "Failed to apply the description for " + |
| 659 | content_info.name + ": " + error.message()); |
| 660 | } |
| 661 | } |
| 662 | return RTCError::OK(); |
| 663 | } |
| 664 | |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 665 | RTCError JsepTransportController::ValidateAndMaybeUpdateBundleGroup( |
| 666 | bool local, |
| 667 | SdpType type, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 668 | const cricket::SessionDescription* description) { |
| 669 | RTC_DCHECK(description); |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 670 | const cricket::ContentGroup* new_bundle_group = |
| 671 | description->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 672 | |
| 673 | // The BUNDLE group containing a MID that no m= section has is invalid. |
| 674 | if (new_bundle_group) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 675 | for (const auto& content_name : new_bundle_group->content_names()) { |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 676 | if (!description->GetContentByName(content_name)) { |
| 677 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 678 | "The BUNDLE group contains MID:" + content_name + |
| 679 | " matching no m= section."); |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | if (type == SdpType::kAnswer) { |
| 685 | const cricket::ContentGroup* offered_bundle_group = |
| 686 | local ? remote_desc_->GetGroupByName(cricket::GROUP_TYPE_BUNDLE) |
| 687 | : local_desc_->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 688 | |
| 689 | if (new_bundle_group) { |
| 690 | // The BUNDLE group in answer should be a subset of offered group. |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 691 | for (const auto& content_name : new_bundle_group->content_names()) { |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 692 | if (!offered_bundle_group || |
| 693 | !offered_bundle_group->HasContentName(content_name)) { |
| 694 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 695 | "The BUNDLE group in answer contains a MID that was " |
| 696 | "not in the offered group."); |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | if (bundle_group_) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 702 | for (const auto& content_name : bundle_group_->content_names()) { |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 703 | // An answer that removes m= sections from pre-negotiated BUNDLE group |
| 704 | // without rejecting it, is invalid. |
| 705 | if (!new_bundle_group || |
| 706 | !new_bundle_group->HasContentName(content_name)) { |
| 707 | auto* content_info = description->GetContentByName(content_name); |
| 708 | if (!content_info || !content_info->rejected) { |
| 709 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 710 | "Answer cannot remove m= section " + content_name + |
| 711 | " from already-established BUNDLE group."); |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | if (config_.bundle_policy == |
| 719 | PeerConnectionInterface::kBundlePolicyMaxBundle && |
| 720 | !description->HasGroup(cricket::GROUP_TYPE_BUNDLE)) { |
| 721 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 722 | "max-bundle is used but no bundle group found."); |
| 723 | } |
| 724 | |
| 725 | if (ShouldUpdateBundleGroup(type, description)) { |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 726 | bundle_group_ = *new_bundle_group; |
| 727 | } |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 728 | |
| 729 | if (!bundled_mid()) { |
| 730 | return RTCError::OK(); |
| 731 | } |
| 732 | |
| 733 | auto bundled_content = description->GetContentByName(*bundled_mid()); |
| 734 | if (!bundled_content) { |
| 735 | return RTCError( |
| 736 | RTCErrorType::INVALID_PARAMETER, |
| 737 | "An m= section associated with the BUNDLE-tag doesn't exist."); |
| 738 | } |
| 739 | |
| 740 | // If the |bundled_content| is rejected, other contents in the bundle group |
| 741 | // should be rejected. |
| 742 | if (bundled_content->rejected) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 743 | for (const auto& content_name : bundle_group_->content_names()) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 744 | auto other_content = description->GetContentByName(content_name); |
| 745 | if (!other_content->rejected) { |
| 746 | return RTCError( |
| 747 | RTCErrorType::INVALID_PARAMETER, |
| 748 | "The m= section:" + content_name + " should be rejected."); |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | return RTCError::OK(); |
| 754 | } |
| 755 | |
| 756 | RTCError JsepTransportController::ValidateContent( |
| 757 | const cricket::ContentInfo& content_info) { |
| 758 | if (config_.rtcp_mux_policy == |
| 759 | PeerConnectionInterface::kRtcpMuxPolicyRequire && |
| 760 | content_info.type == cricket::MediaProtocolType::kRtp && |
| 761 | !content_info.media_description()->rtcp_mux()) { |
| 762 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 763 | "The m= section:" + content_info.name + |
| 764 | " is invalid. RTCP-MUX is not " |
| 765 | "enabled when it is required."); |
| 766 | } |
| 767 | return RTCError::OK(); |
| 768 | } |
| 769 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 770 | void JsepTransportController::HandleRejectedContent( |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 771 | const cricket::ContentInfo& content_info, |
| 772 | const cricket::SessionDescription* description) { |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 773 | // If the content is rejected, let the |
| 774 | // BaseChannel/SctpTransport change the RtpTransport/DtlsTransport first, |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 775 | // then destroy the cricket::JsepTransport. |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 776 | RemoveTransportForMid(content_info.name); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 777 | if (content_info.name == bundled_mid()) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 778 | for (const auto& content_name : bundle_group_->content_names()) { |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 779 | RemoveTransportForMid(content_name); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 780 | } |
| 781 | bundle_group_.reset(); |
| 782 | } else if (IsBundled(content_info.name)) { |
| 783 | // Remove the rejected content from the |bundle_group_|. |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 784 | bundle_group_->RemoveContentName(content_info.name); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 785 | // Reset the bundle group if nothing left. |
| 786 | if (!bundle_group_->FirstContentName()) { |
| 787 | bundle_group_.reset(); |
| 788 | } |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 789 | } |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 790 | MaybeDestroyJsepTransport(content_info.name); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 791 | } |
| 792 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 793 | bool JsepTransportController::HandleBundledContent( |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 794 | const cricket::ContentInfo& content_info) { |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 795 | auto jsep_transport = GetJsepTransportByName(*bundled_mid()); |
| 796 | RTC_DCHECK(jsep_transport); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 797 | // If the content is bundled, let the |
| 798 | // BaseChannel/SctpTransport change the RtpTransport/DtlsTransport first, |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 799 | // then destroy the cricket::JsepTransport. |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 800 | if (SetTransportForMid(content_info.name, jsep_transport)) { |
Piotr (Peter) Slatala | 10aeb2a | 2018-11-14 10:57:24 -0800 | [diff] [blame] | 801 | // TODO(bugs.webrtc.org/9719) For media transport this is far from ideal, |
| 802 | // because it means that we first create media transport and start |
| 803 | // connecting it, and then we destroy it. We will need to address it before |
| 804 | // video path is enabled. |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 805 | MaybeDestroyJsepTransport(content_info.name); |
| 806 | return true; |
| 807 | } |
| 808 | return false; |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 809 | } |
| 810 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 811 | bool JsepTransportController::SetTransportForMid( |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 812 | const std::string& mid, |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 813 | cricket::JsepTransport* jsep_transport) { |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 814 | RTC_DCHECK(jsep_transport); |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 815 | if (mid_to_transport_[mid] == jsep_transport) { |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 816 | return true; |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | mid_to_transport_[mid] = jsep_transport; |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 820 | return config_.transport_observer->OnTransportChanged( |
| 821 | mid, jsep_transport->rtp_transport(), |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 08:26:19 -0800 | [diff] [blame] | 822 | jsep_transport->rtp_dtls_transport(), jsep_transport->media_transport()); |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 825 | void JsepTransportController::RemoveTransportForMid(const std::string& mid) { |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 08:26:19 -0800 | [diff] [blame] | 826 | bool ret = config_.transport_observer->OnTransportChanged(mid, nullptr, |
| 827 | nullptr, nullptr); |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 828 | // Calling OnTransportChanged with nullptr should always succeed, since it is |
| 829 | // only expected to fail when adding media to a transport (not removing). |
| 830 | RTC_DCHECK(ret); |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 831 | mid_to_transport_.erase(mid); |
| 832 | } |
| 833 | |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 834 | cricket::JsepTransportDescription |
| 835 | JsepTransportController::CreateJsepTransportDescription( |
| 836 | cricket::ContentInfo content_info, |
| 837 | cricket::TransportInfo transport_info, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 838 | const std::vector<int>& encrypted_extension_ids, |
| 839 | int rtp_abs_sendtime_extn_id) { |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 840 | const cricket::MediaContentDescription* content_desc = |
| 841 | static_cast<const cricket::MediaContentDescription*>( |
| 842 | content_info.description); |
| 843 | RTC_DCHECK(content_desc); |
| 844 | bool rtcp_mux_enabled = content_info.type == cricket::MediaProtocolType::kSctp |
| 845 | ? true |
| 846 | : content_desc->rtcp_mux(); |
| 847 | |
| 848 | return cricket::JsepTransportDescription( |
| 849 | rtcp_mux_enabled, content_desc->cryptos(), encrypted_extension_ids, |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 850 | rtp_abs_sendtime_extn_id, transport_info.description); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | bool JsepTransportController::ShouldUpdateBundleGroup( |
| 854 | SdpType type, |
| 855 | const cricket::SessionDescription* description) { |
| 856 | if (config_.bundle_policy == |
| 857 | PeerConnectionInterface::kBundlePolicyMaxBundle) { |
| 858 | return true; |
| 859 | } |
| 860 | |
| 861 | if (type != SdpType::kAnswer) { |
| 862 | return false; |
| 863 | } |
| 864 | |
| 865 | RTC_DCHECK(local_desc_ && remote_desc_); |
| 866 | const cricket::ContentGroup* local_bundle = |
| 867 | local_desc_->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 868 | const cricket::ContentGroup* remote_bundle = |
| 869 | remote_desc_->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 870 | return local_bundle && remote_bundle; |
| 871 | } |
| 872 | |
| 873 | std::vector<int> JsepTransportController::GetEncryptedHeaderExtensionIds( |
| 874 | const cricket::ContentInfo& content_info) { |
| 875 | const cricket::MediaContentDescription* content_desc = |
| 876 | static_cast<const cricket::MediaContentDescription*>( |
| 877 | content_info.description); |
| 878 | |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 879 | if (!config_.crypto_options.srtp.enable_encrypted_rtp_header_extensions) { |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 880 | return std::vector<int>(); |
| 881 | } |
| 882 | |
| 883 | std::vector<int> encrypted_header_extension_ids; |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 884 | for (const auto& extension : content_desc->rtp_header_extensions()) { |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 885 | if (!extension.encrypt) { |
| 886 | continue; |
| 887 | } |
| 888 | auto it = std::find(encrypted_header_extension_ids.begin(), |
| 889 | encrypted_header_extension_ids.end(), extension.id); |
| 890 | if (it == encrypted_header_extension_ids.end()) { |
| 891 | encrypted_header_extension_ids.push_back(extension.id); |
| 892 | } |
| 893 | } |
| 894 | return encrypted_header_extension_ids; |
| 895 | } |
| 896 | |
| 897 | std::vector<int> |
| 898 | JsepTransportController::MergeEncryptedHeaderExtensionIdsForBundle( |
| 899 | const cricket::SessionDescription* description) { |
| 900 | RTC_DCHECK(description); |
| 901 | RTC_DCHECK(bundle_group_); |
| 902 | |
| 903 | std::vector<int> merged_ids; |
| 904 | // Union the encrypted header IDs in the group when bundle is enabled. |
| 905 | for (const cricket::ContentInfo& content_info : description->contents()) { |
| 906 | if (bundle_group_->HasContentName(content_info.name)) { |
| 907 | std::vector<int> extension_ids = |
| 908 | GetEncryptedHeaderExtensionIds(content_info); |
| 909 | for (int id : extension_ids) { |
| 910 | auto it = std::find(merged_ids.begin(), merged_ids.end(), id); |
| 911 | if (it == merged_ids.end()) { |
| 912 | merged_ids.push_back(id); |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | } |
| 917 | return merged_ids; |
| 918 | } |
| 919 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 920 | int JsepTransportController::GetRtpAbsSendTimeHeaderExtensionId( |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 921 | const cricket::ContentInfo& content_info) { |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 922 | if (!config_.enable_external_auth) { |
| 923 | return -1; |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | const cricket::MediaContentDescription* content_desc = |
| 927 | static_cast<const cricket::MediaContentDescription*>( |
| 928 | content_info.description); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 929 | |
| 930 | const webrtc::RtpExtension* send_time_extension = |
| 931 | webrtc::RtpExtension::FindHeaderExtensionByUri( |
| 932 | content_desc->rtp_header_extensions(), |
| 933 | webrtc::RtpExtension::kAbsSendTimeUri); |
| 934 | return send_time_extension ? send_time_extension->id : -1; |
| 935 | } |
| 936 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 937 | const cricket::JsepTransport* JsepTransportController::GetJsepTransportForMid( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 938 | const std::string& mid) const { |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 939 | auto it = mid_to_transport_.find(mid); |
| 940 | return it == mid_to_transport_.end() ? nullptr : it->second; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 943 | cricket::JsepTransport* JsepTransportController::GetJsepTransportForMid( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 944 | const std::string& mid) { |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 945 | auto it = mid_to_transport_.find(mid); |
| 946 | return it == mid_to_transport_.end() ? nullptr : it->second; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 947 | } |
| 948 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 949 | const cricket::JsepTransport* JsepTransportController::GetJsepTransportByName( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 950 | const std::string& transport_name) const { |
| 951 | auto it = jsep_transports_by_name_.find(transport_name); |
| 952 | return (it == jsep_transports_by_name_.end()) ? nullptr : it->second.get(); |
| 953 | } |
| 954 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 955 | cricket::JsepTransport* JsepTransportController::GetJsepTransportByName( |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 956 | const std::string& transport_name) { |
| 957 | auto it = jsep_transports_by_name_.find(transport_name); |
| 958 | return (it == jsep_transports_by_name_.end()) ? nullptr : it->second.get(); |
| 959 | } |
| 960 | |
Piotr (Peter) Slatala | 47dfdca | 2018-11-16 14:13:58 -0800 | [diff] [blame] | 961 | std::unique_ptr<webrtc::MediaTransportInterface> |
| 962 | JsepTransportController::MaybeCreateMediaTransport( |
| 963 | const cricket::ContentInfo& content_info, |
Anton Sukhanov | 7940da0 | 2018-10-10 10:34:49 -0700 | [diff] [blame] | 964 | bool local, |
Piotr (Peter) Slatala | 47dfdca | 2018-11-16 14:13:58 -0800 | [diff] [blame] | 965 | cricket::IceTransportInternal* ice_transport) { |
Piotr (Peter) Slatala | 63a176b | 2019-01-25 08:25:33 -0800 | [diff] [blame] | 966 | if (!is_media_transport_factory_enabled_) { |
| 967 | return nullptr; |
| 968 | } |
| 969 | if (config_.media_transport_factory == nullptr) { |
| 970 | return nullptr; |
| 971 | } |
| 972 | |
Piotr (Peter) Slatala | 55b91b9 | 2019-01-25 13:31:15 -0800 | [diff] [blame] | 973 | if (!config_.use_media_transport_for_media && |
| 974 | !config_.use_media_transport_for_data_channels) { |
| 975 | return nullptr; |
| 976 | } |
| 977 | |
Piotr (Peter) Slatala | 9f95625 | 2018-10-31 08:25:26 -0700 | [diff] [blame] | 978 | absl::optional<cricket::CryptoParams> selected_crypto_for_media_transport; |
| 979 | if (content_info.media_description() && |
| 980 | !content_info.media_description()->cryptos().empty()) { |
| 981 | // Order of cryptos is deterministic (rfc4568, 5.1.1), so we just select the |
| 982 | // first one (in fact the first one should be the most preferred one.) We |
| 983 | // ignore the HMAC size, as media transport crypto settings currently don't |
| 984 | // expose HMAC size, nor crypto protocol for that matter. |
| 985 | selected_crypto_for_media_transport = |
| 986 | content_info.media_description()->cryptos()[0]; |
| 987 | } |
| 988 | |
Piotr (Peter) Slatala | 63a176b | 2019-01-25 08:25:33 -0800 | [diff] [blame] | 989 | if (!selected_crypto_for_media_transport.has_value()) { |
| 990 | RTC_LOG(LS_WARNING) << "a=cryto line was not found in the offer. Most " |
| 991 | "likely you did not enable SDES. " |
| 992 | "Make sure to pass config.enable_dtls_srtp=false " |
| 993 | "to RTCConfiguration. " |
| 994 | "Cannot continue with media transport. Falling " |
| 995 | "back to RTP. is_local=" |
| 996 | << local; |
Anton Sukhanov | 7940da0 | 2018-10-10 10:34:49 -0700 | [diff] [blame] | 997 | |
Piotr (Peter) Slatala | 63a176b | 2019-01-25 08:25:33 -0800 | [diff] [blame] | 998 | // Remove media_transport_factory from config, because we don't want to |
| 999 | // use it on the subsequent call (for the other side of the offer). |
| 1000 | is_media_transport_factory_enabled_ = false; |
| 1001 | return nullptr; |
Anton Sukhanov | 7940da0 | 2018-10-10 10:34:49 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
Piotr (Peter) Slatala | 63a176b | 2019-01-25 08:25:33 -0800 | [diff] [blame] | 1004 | // Note that we ignore here lifetime and length. |
| 1005 | // In fact we take those bits (inline, lifetime and length) and keep it as |
| 1006 | // part of key derivation. |
| 1007 | // |
| 1008 | // Technically, we are also not following rfc4568, which requires us to |
| 1009 | // send and answer with the key that we chose. In practice, for media |
| 1010 | // transport, the current approach should be sufficient (we take the key |
| 1011 | // that sender offered, and caller assumes we will use it. We are not |
| 1012 | // signaling back that we indeed used it.) |
| 1013 | std::unique_ptr<rtc::KeyDerivation> key_derivation = |
| 1014 | rtc::KeyDerivation::Create(rtc::KeyDerivationAlgorithm::HKDF_SHA256); |
| 1015 | const std::string label = "MediaTransportLabel"; |
| 1016 | constexpr int kDerivedKeyByteSize = 32; |
| 1017 | |
| 1018 | int key_len, salt_len; |
| 1019 | if (!rtc::GetSrtpKeyAndSaltLengths( |
| 1020 | rtc::SrtpCryptoSuiteFromName( |
| 1021 | selected_crypto_for_media_transport.value().cipher_suite), |
| 1022 | &key_len, &salt_len)) { |
| 1023 | RTC_CHECK(false) << "Cannot set up secure media transport"; |
| 1024 | } |
| 1025 | rtc::ZeroOnFreeBuffer<uint8_t> raw_key(key_len + salt_len); |
| 1026 | |
| 1027 | cricket::SrtpFilter::ParseKeyParams( |
| 1028 | selected_crypto_for_media_transport.value().key_params, raw_key.data(), |
| 1029 | raw_key.size()); |
| 1030 | absl::optional<rtc::ZeroOnFreeBuffer<uint8_t>> key = |
| 1031 | key_derivation->DeriveKey( |
| 1032 | raw_key, |
| 1033 | /*salt=*/nullptr, |
| 1034 | rtc::ArrayView<const uint8_t>( |
| 1035 | reinterpret_cast<const uint8_t*>(label.data()), label.size()), |
| 1036 | kDerivedKeyByteSize); |
| 1037 | |
| 1038 | // We want to crash the app if we don't have a key, and not silently fall |
| 1039 | // back to the unsecure communication. |
| 1040 | RTC_CHECK(key.has_value()); |
| 1041 | MediaTransportSettings settings; |
| 1042 | settings.is_caller = local; |
| 1043 | settings.pre_shared_key = std::string( |
| 1044 | reinterpret_cast<const char*>(key.value().data()), key.value().size()); |
| 1045 | settings.event_log = config_.event_log; |
| 1046 | auto media_transport_result = |
| 1047 | config_.media_transport_factory->CreateMediaTransport( |
| 1048 | ice_transport, network_thread_, settings); |
| 1049 | |
| 1050 | // TODO(sukhanov): Proper error handling. |
| 1051 | RTC_CHECK(media_transport_result.ok()); |
| 1052 | |
| 1053 | return media_transport_result.MoveValue(); |
Piotr (Peter) Slatala | 47dfdca | 2018-11-16 14:13:58 -0800 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | RTCError JsepTransportController::MaybeCreateJsepTransport( |
| 1057 | bool local, |
| 1058 | const cricket::ContentInfo& content_info) { |
| 1059 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 1060 | cricket::JsepTransport* transport = GetJsepTransportByName(content_info.name); |
| 1061 | if (transport) { |
| 1062 | return RTCError::OK(); |
| 1063 | } |
| 1064 | |
| 1065 | const cricket::MediaContentDescription* content_desc = |
| 1066 | static_cast<const cricket::MediaContentDescription*>( |
| 1067 | content_info.description); |
| 1068 | if (certificate_ && !content_desc->cryptos().empty()) { |
| 1069 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 1070 | "SDES and DTLS-SRTP cannot be enabled at the same time."); |
| 1071 | } |
| 1072 | |
Piotr (Peter) Slatala | 2b5baee | 2019-01-16 08:25:21 -0800 | [diff] [blame] | 1073 | std::unique_ptr<cricket::IceTransportInternal> ice = |
| 1074 | CreateIceTransport(content_info.name, /*rtcp=*/false); |
| 1075 | |
| 1076 | std::unique_ptr<MediaTransportInterface> media_transport = |
| 1077 | MaybeCreateMediaTransport(content_info, local, ice.get()); |
| 1078 | |
Piotr (Peter) Slatala | 47dfdca | 2018-11-16 14:13:58 -0800 | [diff] [blame] | 1079 | std::unique_ptr<cricket::DtlsTransportInternal> rtp_dtls_transport = |
Piotr (Peter) Slatala | 2b5baee | 2019-01-16 08:25:21 -0800 | [diff] [blame] | 1080 | CreateDtlsTransport(std::move(ice)); |
Piotr (Peter) Slatala | 47dfdca | 2018-11-16 14:13:58 -0800 | [diff] [blame] | 1081 | |
| 1082 | std::unique_ptr<cricket::DtlsTransportInternal> rtcp_dtls_transport; |
| 1083 | std::unique_ptr<RtpTransport> unencrypted_rtp_transport; |
| 1084 | std::unique_ptr<SrtpTransport> sdes_transport; |
| 1085 | std::unique_ptr<DtlsSrtpTransport> dtls_srtp_transport; |
Piotr (Peter) Slatala | 47dfdca | 2018-11-16 14:13:58 -0800 | [diff] [blame] | 1086 | |
| 1087 | if (config_.rtcp_mux_policy != |
| 1088 | PeerConnectionInterface::kRtcpMuxPolicyRequire && |
| 1089 | content_info.type == cricket::MediaProtocolType::kRtp) { |
Piotr (Peter) Slatala | 2b5baee | 2019-01-16 08:25:21 -0800 | [diff] [blame] | 1090 | RTC_DCHECK(media_transport == nullptr); |
| 1091 | rtcp_dtls_transport = CreateDtlsTransport( |
| 1092 | CreateIceTransport(content_info.name, /*rtcp=*/true)); |
Piotr (Peter) Slatala | 47dfdca | 2018-11-16 14:13:58 -0800 | [diff] [blame] | 1093 | } |
Piotr (Peter) Slatala | 47dfdca | 2018-11-16 14:13:58 -0800 | [diff] [blame] | 1094 | |
Anton Sukhanov | 7940da0 | 2018-10-10 10:34:49 -0700 | [diff] [blame] | 1095 | // TODO(sukhanov): Do not create RTP/RTCP transports if media transport is |
Piotr (Peter) Slatala | 2b5baee | 2019-01-16 08:25:21 -0800 | [diff] [blame] | 1096 | // used, and remove the no-op dtls transport when that's done. |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1097 | if (config_.disable_encryption) { |
| 1098 | unencrypted_rtp_transport = CreateUnencryptedRtpTransport( |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 1099 | content_info.name, rtp_dtls_transport.get(), rtcp_dtls_transport.get()); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1100 | } else if (!content_desc->cryptos().empty()) { |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 1101 | sdes_transport = CreateSdesTransport( |
| 1102 | content_info.name, rtp_dtls_transport.get(), rtcp_dtls_transport.get()); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1103 | } else { |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 1104 | dtls_srtp_transport = CreateDtlsSrtpTransport( |
| 1105 | content_info.name, rtp_dtls_transport.get(), rtcp_dtls_transport.get()); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1106 | } |
| 1107 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 1108 | std::unique_ptr<cricket::JsepTransport> jsep_transport = |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 1109 | absl::make_unique<cricket::JsepTransport>( |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 1110 | content_info.name, certificate_, std::move(unencrypted_rtp_transport), |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1111 | std::move(sdes_transport), std::move(dtls_srtp_transport), |
Anton Sukhanov | 7940da0 | 2018-10-10 10:34:49 -0700 | [diff] [blame] | 1112 | std::move(rtp_dtls_transport), std::move(rtcp_dtls_transport), |
| 1113 | std::move(media_transport)); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1114 | jsep_transport->SignalRtcpMuxActive.connect( |
| 1115 | this, &JsepTransportController::UpdateAggregateStates_n); |
Piotr (Peter) Slatala | 4eb4112 | 2018-11-01 07:26:03 -0700 | [diff] [blame] | 1116 | jsep_transport->SignalMediaTransportStateChanged.connect( |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 1117 | this, &JsepTransportController::OnMediaTransportStateChanged_n); |
Taylor Brandstetter | cbaa254 | 2018-04-16 16:42:14 -0700 | [diff] [blame] | 1118 | SetTransportForMid(content_info.name, jsep_transport.get()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1119 | |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 1120 | jsep_transports_by_name_[content_info.name] = std::move(jsep_transport); |
| 1121 | UpdateAggregateStates_n(); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1122 | return RTCError::OK(); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | void JsepTransportController::MaybeDestroyJsepTransport( |
| 1126 | const std::string& mid) { |
Zhi Huang | d2248f8 | 2018-04-10 14:41:03 -0700 | [diff] [blame] | 1127 | auto jsep_transport = GetJsepTransportByName(mid); |
| 1128 | if (!jsep_transport) { |
| 1129 | return; |
| 1130 | } |
| 1131 | |
| 1132 | // Don't destroy the JsepTransport if there are still media sections referring |
| 1133 | // to it. |
| 1134 | for (const auto& kv : mid_to_transport_) { |
| 1135 | if (kv.second == jsep_transport) { |
| 1136 | return; |
| 1137 | } |
| 1138 | } |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 08:26:19 -0800 | [diff] [blame] | 1139 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1140 | jsep_transports_by_name_.erase(mid); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1141 | UpdateAggregateStates_n(); |
| 1142 | } |
| 1143 | |
| 1144 | void JsepTransportController::DestroyAllJsepTransports_n() { |
| 1145 | RTC_DCHECK(network_thread_->IsCurrent()); |
Piotr (Peter) Slatala | cc8e8bb | 2018-11-15 08:26:19 -0800 | [diff] [blame] | 1146 | |
| 1147 | for (const auto& jsep_transport : jsep_transports_by_name_) { |
| 1148 | config_.transport_observer->OnTransportChanged(jsep_transport.first, |
| 1149 | nullptr, nullptr, nullptr); |
| 1150 | } |
| 1151 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1152 | jsep_transports_by_name_.clear(); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | void JsepTransportController::SetIceRole_n(cricket::IceRole ice_role) { |
| 1156 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 1157 | |
| 1158 | ice_role_ = ice_role; |
| 1159 | for (auto& dtls : GetDtlsTransports()) { |
| 1160 | dtls->ice_transport()->SetIceRole(ice_role_); |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | cricket::IceRole JsepTransportController::DetermineIceRole( |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 1165 | cricket::JsepTransport* jsep_transport, |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1166 | const cricket::TransportInfo& transport_info, |
| 1167 | SdpType type, |
| 1168 | bool local) { |
| 1169 | cricket::IceRole ice_role = ice_role_; |
| 1170 | auto tdesc = transport_info.description; |
| 1171 | if (local) { |
| 1172 | // The initial offer side may use ICE Lite, in which case, per RFC5245 |
| 1173 | // Section 5.1.1, the answer side should take the controlling role if it is |
| 1174 | // in the full ICE mode. |
| 1175 | // |
| 1176 | // When both sides use ICE Lite, the initial offer side must take the |
| 1177 | // controlling role, and this is the default logic implemented in |
| 1178 | // SetLocalDescription in JsepTransportController. |
| 1179 | if (jsep_transport->remote_description() && |
| 1180 | jsep_transport->remote_description()->transport_desc.ice_mode == |
| 1181 | cricket::ICEMODE_LITE && |
| 1182 | ice_role_ == cricket::ICEROLE_CONTROLLED && |
| 1183 | tdesc.ice_mode == cricket::ICEMODE_FULL) { |
| 1184 | ice_role = cricket::ICEROLE_CONTROLLING; |
| 1185 | } |
| 1186 | |
| 1187 | // Older versions of Chrome expect the ICE role to be re-determined when an |
| 1188 | // ICE restart occurs, and also don't perform conflict resolution correctly, |
| 1189 | // so for now we can't safely stop doing this, unless the application opts |
| 1190 | // in by setting |config_.redetermine_role_on_ice_restart_| to false. See: |
| 1191 | // https://bugs.chromium.org/p/chromium/issues/detail?id=628676 |
| 1192 | // TODO(deadbeef): Remove this when these old versions of Chrome reach a low |
| 1193 | // enough population. |
| 1194 | if (config_.redetermine_role_on_ice_restart && |
| 1195 | jsep_transport->local_description() && |
| 1196 | cricket::IceCredentialsChanged( |
| 1197 | jsep_transport->local_description()->transport_desc.ice_ufrag, |
| 1198 | jsep_transport->local_description()->transport_desc.ice_pwd, |
| 1199 | tdesc.ice_ufrag, tdesc.ice_pwd) && |
| 1200 | // Don't change the ICE role if the remote endpoint is ICE lite; we |
| 1201 | // should always be controlling in that case. |
| 1202 | (!jsep_transport->remote_description() || |
| 1203 | jsep_transport->remote_description()->transport_desc.ice_mode != |
| 1204 | cricket::ICEMODE_LITE)) { |
| 1205 | ice_role = (type == SdpType::kOffer) ? cricket::ICEROLE_CONTROLLING |
| 1206 | : cricket::ICEROLE_CONTROLLED; |
| 1207 | } |
| 1208 | } else { |
| 1209 | // If our role is cricket::ICEROLE_CONTROLLED and the remote endpoint |
| 1210 | // supports only ice_lite, this local endpoint should take the CONTROLLING |
| 1211 | // role. |
| 1212 | // TODO(deadbeef): This is a session-level attribute, so it really shouldn't |
| 1213 | // be in a TransportDescription in the first place... |
| 1214 | if (ice_role_ == cricket::ICEROLE_CONTROLLED && |
| 1215 | tdesc.ice_mode == cricket::ICEMODE_LITE) { |
| 1216 | ice_role = cricket::ICEROLE_CONTROLLING; |
| 1217 | } |
| 1218 | |
| 1219 | // If we use ICE Lite and the remote endpoint uses the full implementation |
| 1220 | // of ICE, the local endpoint must take the controlled role, and the other |
| 1221 | // side must be the controlling role. |
| 1222 | if (jsep_transport->local_description() && |
| 1223 | jsep_transport->local_description()->transport_desc.ice_mode == |
| 1224 | cricket::ICEMODE_LITE && |
| 1225 | ice_role_ == cricket::ICEROLE_CONTROLLING && |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 1226 | tdesc.ice_mode == cricket::ICEMODE_FULL) { |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1227 | ice_role = cricket::ICEROLE_CONTROLLED; |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | return ice_role; |
| 1232 | } |
| 1233 | |
| 1234 | void JsepTransportController::OnTransportWritableState_n( |
| 1235 | rtc::PacketTransportInternal* transport) { |
| 1236 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 1237 | RTC_LOG(LS_INFO) << " Transport " << transport->transport_name() |
| 1238 | << " writability changed to " << transport->writable() |
| 1239 | << "."; |
| 1240 | UpdateAggregateStates_n(); |
| 1241 | } |
| 1242 | |
| 1243 | void JsepTransportController::OnTransportReceivingState_n( |
| 1244 | rtc::PacketTransportInternal* transport) { |
| 1245 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 1246 | UpdateAggregateStates_n(); |
| 1247 | } |
| 1248 | |
| 1249 | void JsepTransportController::OnTransportGatheringState_n( |
| 1250 | cricket::IceTransportInternal* transport) { |
| 1251 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 1252 | UpdateAggregateStates_n(); |
| 1253 | } |
| 1254 | |
| 1255 | void JsepTransportController::OnTransportCandidateGathered_n( |
| 1256 | cricket::IceTransportInternal* transport, |
| 1257 | const cricket::Candidate& candidate) { |
| 1258 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 1259 | |
| 1260 | // We should never signal peer-reflexive candidates. |
| 1261 | if (candidate.type() == cricket::PRFLX_PORT_TYPE) { |
| 1262 | RTC_NOTREACHED(); |
| 1263 | return; |
| 1264 | } |
Steve Anton | d25828a | 2018-08-31 13:06:05 -0700 | [diff] [blame] | 1265 | std::string transport_name = transport->transport_name(); |
| 1266 | invoker_.AsyncInvoke<void>( |
| 1267 | RTC_FROM_HERE, signaling_thread_, [this, transport_name, candidate] { |
| 1268 | SignalIceCandidatesGathered(transport_name, {candidate}); |
| 1269 | }); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | void JsepTransportController::OnTransportCandidatesRemoved_n( |
| 1273 | cricket::IceTransportInternal* transport, |
| 1274 | const cricket::Candidates& candidates) { |
| 1275 | invoker_.AsyncInvoke<void>( |
| 1276 | RTC_FROM_HERE, signaling_thread_, |
Steve Anton | d25828a | 2018-08-31 13:06:05 -0700 | [diff] [blame] | 1277 | [this, candidates] { SignalIceCandidatesRemoved(candidates); }); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | void JsepTransportController::OnTransportRoleConflict_n( |
| 1281 | cricket::IceTransportInternal* transport) { |
| 1282 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 1283 | // Note: since the role conflict is handled entirely on the network thread, |
| 1284 | // we don't need to worry about role conflicts occurring on two ports at |
| 1285 | // once. The first one encountered should immediately reverse the role. |
| 1286 | cricket::IceRole reversed_role = (ice_role_ == cricket::ICEROLE_CONTROLLING) |
| 1287 | ? cricket::ICEROLE_CONTROLLED |
| 1288 | : cricket::ICEROLE_CONTROLLING; |
| 1289 | RTC_LOG(LS_INFO) << "Got role conflict; switching to " |
| 1290 | << (reversed_role == cricket::ICEROLE_CONTROLLING |
| 1291 | ? "controlling" |
| 1292 | : "controlled") |
| 1293 | << " role."; |
| 1294 | SetIceRole_n(reversed_role); |
| 1295 | } |
| 1296 | |
| 1297 | void JsepTransportController::OnTransportStateChanged_n( |
| 1298 | cricket::IceTransportInternal* transport) { |
| 1299 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 1300 | RTC_LOG(LS_INFO) << transport->transport_name() << " Transport " |
| 1301 | << transport->component() |
| 1302 | << " state changed. Check if state is complete."; |
| 1303 | UpdateAggregateStates_n(); |
| 1304 | } |
| 1305 | |
Bjorn Mellem | 175aa2e | 2018-11-08 11:23:22 -0800 | [diff] [blame] | 1306 | void JsepTransportController::OnMediaTransportStateChanged_n() { |
| 1307 | SignalMediaTransportStateChanged(); |
| 1308 | UpdateAggregateStates_n(); |
| 1309 | } |
| 1310 | |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1311 | void JsepTransportController::UpdateAggregateStates_n() { |
| 1312 | RTC_DCHECK(network_thread_->IsCurrent()); |
| 1313 | |
| 1314 | auto dtls_transports = GetDtlsTransports(); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 1315 | cricket::IceConnectionState new_connection_state = |
| 1316 | cricket::kIceConnectionConnecting; |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1317 | PeerConnectionInterface::IceConnectionState new_ice_connection_state = |
| 1318 | PeerConnectionInterface::IceConnectionState::kIceConnectionNew; |
| 1319 | PeerConnectionInterface::PeerConnectionState new_combined_state = |
| 1320 | PeerConnectionInterface::PeerConnectionState::kNew; |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1321 | cricket::IceGatheringState new_gathering_state = cricket::kIceGatheringNew; |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 1322 | bool any_failed = false; |
| 1323 | |
| 1324 | // TODO(http://bugs.webrtc.org/9719) If(when) media_transport disables |
| 1325 | // dtls_transports entirely, the below line will have to be changed to account |
| 1326 | // for the fact that dtls transports might be absent. |
| 1327 | bool all_connected = !dtls_transports.empty(); |
| 1328 | bool all_completed = !dtls_transports.empty(); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1329 | bool any_gathering = false; |
| 1330 | bool all_done_gathering = !dtls_transports.empty(); |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1331 | |
| 1332 | std::map<IceTransportState, int> ice_state_counts; |
| 1333 | std::map<cricket::DtlsTransportState, int> dtls_state_counts; |
| 1334 | |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1335 | for (const auto& dtls : dtls_transports) { |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 1336 | any_failed = any_failed || dtls->ice_transport()->GetState() == |
| 1337 | cricket::IceTransportState::STATE_FAILED; |
| 1338 | all_connected = all_connected && dtls->writable(); |
| 1339 | all_completed = |
| 1340 | all_completed && dtls->writable() && |
| 1341 | dtls->ice_transport()->GetState() == |
| 1342 | cricket::IceTransportState::STATE_COMPLETED && |
| 1343 | dtls->ice_transport()->GetIceRole() == cricket::ICEROLE_CONTROLLING && |
| 1344 | dtls->ice_transport()->gathering_state() == |
| 1345 | cricket::kIceGatheringComplete; |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1346 | any_gathering = any_gathering || dtls->ice_transport()->gathering_state() != |
| 1347 | cricket::kIceGatheringNew; |
| 1348 | all_done_gathering = |
| 1349 | all_done_gathering && dtls->ice_transport()->gathering_state() == |
| 1350 | cricket::kIceGatheringComplete; |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1351 | |
| 1352 | dtls_state_counts[dtls->dtls_state()]++; |
| 1353 | ice_state_counts[dtls->ice_transport()->GetIceTransportState()]++; |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1354 | } |
Piotr (Peter) Slatala | 4eb4112 | 2018-11-01 07:26:03 -0700 | [diff] [blame] | 1355 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 1356 | for (auto it = jsep_transports_by_name_.begin(); |
| 1357 | it != jsep_transports_by_name_.end(); ++it) { |
| 1358 | auto jsep_transport = it->second.get(); |
| 1359 | if (!jsep_transport->media_transport()) { |
| 1360 | continue; |
| 1361 | } |
| 1362 | |
| 1363 | // There is no 'kIceConnectionDisconnected', so we only need to handle |
| 1364 | // connected and completed. |
| 1365 | // We treat kClosed as failed, because if it happens before shutting down |
| 1366 | // media transports it means that there was a failure. |
| 1367 | // MediaTransportInterface allows to flip back and forth between kWritable |
| 1368 | // and kPending, but there does not exist an implementation that does that, |
| 1369 | // and the contract of jsep transport controller doesn't quite expect that. |
| 1370 | // When this happens, we would go from connected to connecting state, but |
| 1371 | // this may change in future. |
| 1372 | any_failed |= jsep_transport->media_transport_state() == |
| 1373 | webrtc::MediaTransportState::kClosed; |
| 1374 | all_completed &= jsep_transport->media_transport_state() == |
| 1375 | webrtc::MediaTransportState::kWritable; |
| 1376 | all_connected &= jsep_transport->media_transport_state() == |
| 1377 | webrtc::MediaTransportState::kWritable; |
| 1378 | } |
| 1379 | |
| 1380 | if (any_failed) { |
| 1381 | new_connection_state = cricket::kIceConnectionFailed; |
| 1382 | } else if (all_completed) { |
| 1383 | new_connection_state = cricket::kIceConnectionCompleted; |
| 1384 | } else if (all_connected) { |
| 1385 | new_connection_state = cricket::kIceConnectionConnected; |
| 1386 | } |
| 1387 | if (ice_connection_state_ != new_connection_state) { |
| 1388 | ice_connection_state_ = new_connection_state; |
| 1389 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_, |
| 1390 | [this, new_connection_state] { |
| 1391 | SignalIceConnectionState(new_connection_state); |
| 1392 | }); |
| 1393 | } |
| 1394 | |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1395 | // Compute the current RTCIceConnectionState as described in |
| 1396 | // https://www.w3.org/TR/webrtc/#dom-rtciceconnectionstate. |
| 1397 | // The PeerConnection is responsible for handling the "closed" state. |
| 1398 | int total_ice_checking = ice_state_counts[IceTransportState::kChecking]; |
| 1399 | int total_ice_connected = ice_state_counts[IceTransportState::kConnected]; |
| 1400 | int total_ice_completed = ice_state_counts[IceTransportState::kCompleted]; |
| 1401 | int total_ice_failed = ice_state_counts[IceTransportState::kFailed]; |
| 1402 | int total_ice_disconnected = |
| 1403 | ice_state_counts[IceTransportState::kDisconnected]; |
| 1404 | int total_ice_closed = ice_state_counts[IceTransportState::kClosed]; |
| 1405 | int total_ice_new = ice_state_counts[IceTransportState::kNew]; |
| 1406 | int total_ice = dtls_transports.size(); |
| 1407 | |
| 1408 | if (total_ice_failed > 0) { |
Jonas Olsson | 6a8727b | 2018-12-07 13:11:44 +0100 | [diff] [blame] | 1409 | // Any RTCIceTransports are in the "failed" state. |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1410 | new_ice_connection_state = PeerConnectionInterface::kIceConnectionFailed; |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 1411 | } else if (total_ice_disconnected > 0) { |
Jonas Olsson | 6a8727b | 2018-12-07 13:11:44 +0100 | [diff] [blame] | 1412 | // None of the previous states apply and any RTCIceTransports are in the |
| 1413 | // "disconnected" state. |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1414 | new_ice_connection_state = |
| 1415 | PeerConnectionInterface::kIceConnectionDisconnected; |
Jonas Olsson | 6a8727b | 2018-12-07 13:11:44 +0100 | [diff] [blame] | 1416 | } else if (total_ice_new + total_ice_closed == total_ice) { |
| 1417 | // None of the previous states apply and all RTCIceTransports are in the |
| 1418 | // "new" or "closed" state, or there are no transports. |
| 1419 | new_ice_connection_state = PeerConnectionInterface::kIceConnectionNew; |
| 1420 | } else if (total_ice_new + total_ice_checking > 0) { |
| 1421 | // None of the previous states apply and any RTCIceTransports are in the |
| 1422 | // "new" or "checking" state. |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1423 | new_ice_connection_state = PeerConnectionInterface::kIceConnectionChecking; |
Jonas Olsson | 6a8727b | 2018-12-07 13:11:44 +0100 | [diff] [blame] | 1424 | } else if (total_ice_completed + total_ice_closed == total_ice) { |
| 1425 | // None of the previous states apply and all RTCIceTransports are in the |
| 1426 | // "completed" or "closed" state. |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1427 | new_ice_connection_state = PeerConnectionInterface::kIceConnectionCompleted; |
| 1428 | } else if (total_ice_connected + total_ice_completed + total_ice_closed == |
Jonas Olsson | 6a8727b | 2018-12-07 13:11:44 +0100 | [diff] [blame] | 1429 | total_ice) { |
| 1430 | // None of the previous states apply and all RTCIceTransports are in the |
| 1431 | // "connected", "completed" or "closed" state. |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1432 | new_ice_connection_state = PeerConnectionInterface::kIceConnectionConnected; |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1433 | } else { |
| 1434 | RTC_NOTREACHED(); |
| 1435 | } |
| 1436 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 1437 | if (standardized_ice_connection_state_ != new_ice_connection_state) { |
| 1438 | standardized_ice_connection_state_ = new_ice_connection_state; |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1439 | invoker_.AsyncInvoke<void>( |
| 1440 | RTC_FROM_HERE, signaling_thread_, [this, new_ice_connection_state] { |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 +0000 | [diff] [blame] | 1441 | SignalStandardizedIceConnectionState(new_ice_connection_state); |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1442 | }); |
| 1443 | } |
| 1444 | |
| 1445 | // Compute the current RTCPeerConnectionState as described in |
| 1446 | // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnectionstate. |
| 1447 | // The PeerConnection is responsible for handling the "closed" state. |
| 1448 | // Note that "connecting" is only a valid state for DTLS transports while |
| 1449 | // "checking", "completed" and "disconnected" are only valid for ICE |
| 1450 | // transports. |
| 1451 | int total_connected = total_ice_connected + |
| 1452 | dtls_state_counts[cricket::DTLS_TRANSPORT_CONNECTED]; |
| 1453 | int total_dtls_connecting = |
| 1454 | dtls_state_counts[cricket::DTLS_TRANSPORT_CONNECTING]; |
| 1455 | int total_failed = |
| 1456 | total_ice_failed + dtls_state_counts[cricket::DTLS_TRANSPORT_FAILED]; |
| 1457 | int total_closed = |
| 1458 | total_ice_closed + dtls_state_counts[cricket::DTLS_TRANSPORT_CLOSED]; |
| 1459 | int total_new = |
| 1460 | total_ice_new + dtls_state_counts[cricket::DTLS_TRANSPORT_NEW]; |
| 1461 | int total_transports = total_ice * 2; |
| 1462 | |
| 1463 | if (total_failed > 0) { |
| 1464 | // Any of the RTCIceTransports or RTCDtlsTransports are in a "failed" state. |
| 1465 | new_combined_state = PeerConnectionInterface::PeerConnectionState::kFailed; |
Jonas Olsson | 6a8727b | 2018-12-07 13:11:44 +0100 | [diff] [blame] | 1466 | } else if (total_ice_disconnected > 0) { |
| 1467 | // None of the previous states apply and any RTCIceTransports or |
| 1468 | // RTCDtlsTransports are in the "disconnected" state. |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1469 | new_combined_state = |
| 1470 | PeerConnectionInterface::PeerConnectionState::kDisconnected; |
Jonas Olsson | 6a8727b | 2018-12-07 13:11:44 +0100 | [diff] [blame] | 1471 | } else if (total_new + total_closed == total_transports) { |
| 1472 | // None of the previous states apply and all RTCIceTransports and |
| 1473 | // RTCDtlsTransports are in the "new" or "closed" state, or there are no |
| 1474 | // transports. |
| 1475 | new_combined_state = PeerConnectionInterface::PeerConnectionState::kNew; |
| 1476 | } else if (total_new + total_dtls_connecting + total_ice_checking > 0) { |
| 1477 | // None of the previous states apply and all RTCIceTransports or |
| 1478 | // RTCDtlsTransports are in the "new", "connecting" or "checking" state. |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1479 | new_combined_state = |
| 1480 | PeerConnectionInterface::PeerConnectionState::kConnecting; |
| 1481 | } else if (total_connected + total_ice_completed + total_closed == |
Jonas Olsson | 6a8727b | 2018-12-07 13:11:44 +0100 | [diff] [blame] | 1482 | total_transports) { |
| 1483 | // None of the previous states apply and all RTCIceTransports and |
| 1484 | // RTCDtlsTransports are in the "connected", "completed" or "closed" state. |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1485 | new_combined_state = |
| 1486 | PeerConnectionInterface::PeerConnectionState::kConnected; |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 1487 | } else { |
| 1488 | RTC_NOTREACHED(); |
| 1489 | } |
| 1490 | |
| 1491 | if (combined_connection_state_ != new_combined_state) { |
| 1492 | combined_connection_state_ = new_combined_state; |
| 1493 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_, |
| 1494 | [this, new_combined_state] { |
| 1495 | SignalConnectionState(new_combined_state); |
| 1496 | }); |
| 1497 | } |
| 1498 | |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1499 | if (all_done_gathering) { |
| 1500 | new_gathering_state = cricket::kIceGatheringComplete; |
| 1501 | } else if (any_gathering) { |
| 1502 | new_gathering_state = cricket::kIceGatheringGathering; |
| 1503 | } |
| 1504 | if (ice_gathering_state_ != new_gathering_state) { |
| 1505 | ice_gathering_state_ = new_gathering_state; |
Steve Anton | d25828a | 2018-08-31 13:06:05 -0700 | [diff] [blame] | 1506 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_, |
| 1507 | [this, new_gathering_state] { |
| 1508 | SignalIceGatheringState(new_gathering_state); |
| 1509 | }); |
Zhi Huang | e818b6e | 2018-02-22 15:26:27 -0800 | [diff] [blame] | 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | void JsepTransportController::OnDtlsHandshakeError( |
| 1514 | rtc::SSLHandshakeError error) { |
| 1515 | SignalDtlsHandshakeError(error); |
| 1516 | } |
| 1517 | |
| 1518 | } // namespace webrtc |