blob: d05339e1a118ab9ea06a37cc29fc66d79f545bca [file] [log] [blame]
Zhi Huange818b6e2018-02-22 15:26:27 -08001/*
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
11#include "pc/jseptransportcontroller.h"
12
13#include <algorithm>
14#include <memory>
15#include <utility>
16
17#include "p2p/base/port.h"
18#include "rtc_base/bind.h"
19#include "rtc_base/checks.h"
20#include "rtc_base/ptr_util.h"
21#include "rtc_base/thread.h"
22
23using webrtc::SdpType;
24
25namespace {
26
27enum {
28 MSG_ICECONNECTIONSTATE,
29 MSG_ICEGATHERINGSTATE,
30 MSG_ICECANDIDATESGATHERED,
31};
32
33struct CandidatesData : public rtc::MessageData {
34 CandidatesData(const std::string& transport_name,
35 const cricket::Candidates& candidates)
36 : transport_name(transport_name), candidates(candidates) {}
37
38 std::string transport_name;
39 cricket::Candidates candidates;
40};
41
42webrtc::RTCError VerifyCandidate(const cricket::Candidate& cand) {
43 // No address zero.
44 if (cand.address().IsNil() || cand.address().IsAnyIP()) {
45 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
46 "candidate has address of zero");
47 }
48
49 // Disallow all ports below 1024, except for 80 and 443 on public addresses.
50 int port = cand.address().port();
51 if (cand.protocol() == cricket::TCP_PROTOCOL_NAME &&
52 (cand.tcptype() == cricket::TCPTYPE_ACTIVE_STR || port == 0)) {
53 // Expected for active-only candidates per
54 // http://tools.ietf.org/html/rfc6544#section-4.5 so no error.
55 // Libjingle clients emit port 0, in "active" mode.
56 return webrtc::RTCError::OK();
57 }
58 if (port < 1024) {
59 if ((port != 80) && (port != 443)) {
60 return webrtc::RTCError(
61 webrtc::RTCErrorType::INVALID_PARAMETER,
62 "candidate has port below 1024, but not 80 or 443");
63 }
64
65 if (cand.address().IsPrivateIP()) {
66 return webrtc::RTCError(
67 webrtc::RTCErrorType::INVALID_PARAMETER,
68 "candidate has port of 80 or 443 with private IP address");
69 }
70 }
71
72 return webrtc::RTCError::OK();
73}
74
75webrtc::RTCError VerifyCandidates(const cricket::Candidates& candidates) {
76 for (const cricket::Candidate& candidate : candidates) {
77 webrtc::RTCError error = VerifyCandidate(candidate);
78 if (!error.ok()) {
79 return error;
80 }
81 }
82 return webrtc::RTCError::OK();
83}
84
85} // namespace
86
87namespace webrtc {
88
89JsepTransportController::JsepTransportController(
90 rtc::Thread* signaling_thread,
91 rtc::Thread* network_thread,
92 cricket::PortAllocator* port_allocator,
93 Config config)
94 : signaling_thread_(signaling_thread),
95 network_thread_(network_thread),
96 port_allocator_(port_allocator),
Zhi Huang365381f2018-04-13 16:44:34 -070097 config_(config) {
98 // The |transport_observer| is assumed to be non-null.
99 RTC_DCHECK(config_.transport_observer);
100}
Zhi Huange818b6e2018-02-22 15:26:27 -0800101
102JsepTransportController::~JsepTransportController() {
103 // Channel destructors may try to send packets, so this needs to happen on
104 // the network thread.
105 network_thread_->Invoke<void>(
106 RTC_FROM_HERE,
107 rtc::Bind(&JsepTransportController::DestroyAllJsepTransports_n, this));
108}
109
110RTCError JsepTransportController::SetLocalDescription(
111 SdpType type,
112 const cricket::SessionDescription* description) {
113 if (!network_thread_->IsCurrent()) {
114 return network_thread_->Invoke<RTCError>(
115 RTC_FROM_HERE, [=] { return SetLocalDescription(type, description); });
116 }
117
118 if (!initial_offerer_.has_value()) {
119 initial_offerer_.emplace(type == SdpType::kOffer);
120 if (*initial_offerer_) {
121 SetIceRole_n(cricket::ICEROLE_CONTROLLING);
122 } else {
123 SetIceRole_n(cricket::ICEROLE_CONTROLLED);
124 }
125 }
126 return ApplyDescription_n(/*local=*/true, type, description);
127}
128
129RTCError JsepTransportController::SetRemoteDescription(
130 SdpType type,
131 const cricket::SessionDescription* description) {
132 if (!network_thread_->IsCurrent()) {
133 return network_thread_->Invoke<RTCError>(
134 RTC_FROM_HERE, [=] { return SetRemoteDescription(type, description); });
135 }
136
137 return ApplyDescription_n(/*local=*/false, type, description);
138}
139
140RtpTransportInternal* JsepTransportController::GetRtpTransport(
141 const std::string& mid) const {
Zhi Huange830e682018-03-30 10:48:35 -0700142 auto jsep_transport = GetJsepTransportForMid(mid);
Zhi Huange818b6e2018-02-22 15:26:27 -0800143 if (!jsep_transport) {
144 return nullptr;
145 }
146 return jsep_transport->rtp_transport();
147}
148
149cricket::DtlsTransportInternal* JsepTransportController::GetDtlsTransport(
150 const std::string& mid) const {
Zhi Huange830e682018-03-30 10:48:35 -0700151 auto jsep_transport = GetJsepTransportForMid(mid);
Zhi Huange818b6e2018-02-22 15:26:27 -0800152 if (!jsep_transport) {
153 return nullptr;
154 }
155 return jsep_transport->rtp_dtls_transport();
156}
157
158cricket::DtlsTransportInternal* JsepTransportController::GetRtcpDtlsTransport(
159 const std::string& mid) const {
Zhi Huange830e682018-03-30 10:48:35 -0700160 auto jsep_transport = GetJsepTransportForMid(mid);
Zhi Huange818b6e2018-02-22 15:26:27 -0800161 if (!jsep_transport) {
162 return nullptr;
163 }
164 return jsep_transport->rtcp_dtls_transport();
165}
166
167void JsepTransportController::SetIceConfig(const cricket::IceConfig& config) {
168 if (!network_thread_->IsCurrent()) {
169 network_thread_->Invoke<void>(RTC_FROM_HERE, [&] { SetIceConfig(config); });
170 return;
171 }
172
173 ice_config_ = config;
174 for (auto& dtls : GetDtlsTransports()) {
175 dtls->ice_transport()->SetIceConfig(ice_config_);
176 }
177}
178
179void JsepTransportController::SetNeedsIceRestartFlag() {
Zhi Huange830e682018-03-30 10:48:35 -0700180 for (auto& kv : jsep_transports_by_name_) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800181 kv.second->SetNeedsIceRestartFlag();
182 }
183}
184
185bool JsepTransportController::NeedsIceRestart(
186 const std::string& transport_name) const {
Zhi Huang365381f2018-04-13 16:44:34 -0700187 const cricket::JsepTransport* transport =
Zhi Huange830e682018-03-30 10:48:35 -0700188 GetJsepTransportByName(transport_name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800189 if (!transport) {
190 return false;
191 }
192 return transport->needs_ice_restart();
193}
194
195rtc::Optional<rtc::SSLRole> JsepTransportController::GetDtlsRole(
Zhi Huange830e682018-03-30 10:48:35 -0700196 const std::string& mid) const {
Zhi Huange818b6e2018-02-22 15:26:27 -0800197 if (!network_thread_->IsCurrent()) {
198 return network_thread_->Invoke<rtc::Optional<rtc::SSLRole>>(
Zhi Huange830e682018-03-30 10:48:35 -0700199 RTC_FROM_HERE, [&] { return GetDtlsRole(mid); });
Zhi Huange818b6e2018-02-22 15:26:27 -0800200 }
201
Zhi Huang365381f2018-04-13 16:44:34 -0700202 const cricket::JsepTransport* t = GetJsepTransportForMid(mid);
Zhi Huange818b6e2018-02-22 15:26:27 -0800203 if (!t) {
204 return rtc::Optional<rtc::SSLRole>();
205 }
206 return t->GetDtlsRole();
207}
208
209bool JsepTransportController::SetLocalCertificate(
210 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
211 if (!network_thread_->IsCurrent()) {
212 return network_thread_->Invoke<bool>(
213 RTC_FROM_HERE, [&] { return SetLocalCertificate(certificate); });
214 }
215
216 // Can't change a certificate, or set a null certificate.
217 if (certificate_ || !certificate) {
218 return false;
219 }
220 certificate_ = certificate;
221
222 // Set certificate for JsepTransport, which verifies it matches the
223 // fingerprint in SDP, and DTLS transport.
224 // Fallback from DTLS to SDES is not supported.
Zhi Huange830e682018-03-30 10:48:35 -0700225 for (auto& kv : jsep_transports_by_name_) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800226 kv.second->SetLocalCertificate(certificate_);
227 }
228 for (auto& dtls : GetDtlsTransports()) {
229 bool set_cert_success = dtls->SetLocalCertificate(certificate_);
230 RTC_DCHECK(set_cert_success);
231 }
232 return true;
233}
234
235rtc::scoped_refptr<rtc::RTCCertificate>
236JsepTransportController::GetLocalCertificate(
237 const std::string& transport_name) const {
238 if (!network_thread_->IsCurrent()) {
239 return network_thread_->Invoke<rtc::scoped_refptr<rtc::RTCCertificate>>(
240 RTC_FROM_HERE, [&] { return GetLocalCertificate(transport_name); });
241 }
242
Zhi Huang365381f2018-04-13 16:44:34 -0700243 const cricket::JsepTransport* t = GetJsepTransportByName(transport_name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800244 if (!t) {
245 return nullptr;
246 }
247 return t->GetLocalCertificate();
248}
249
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800250std::unique_ptr<rtc::SSLCertChain>
251JsepTransportController::GetRemoteSSLCertChain(
Zhi Huange818b6e2018-02-22 15:26:27 -0800252 const std::string& transport_name) const {
253 if (!network_thread_->IsCurrent()) {
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800254 return network_thread_->Invoke<std::unique_ptr<rtc::SSLCertChain>>(
255 RTC_FROM_HERE, [&] { return GetRemoteSSLCertChain(transport_name); });
Zhi Huange818b6e2018-02-22 15:26:27 -0800256 }
257
Zhi Huange830e682018-03-30 10:48:35 -0700258 // Get the certificate from the RTP transport's DTLS handshake. Should be
259 // identical to the RTCP transport's, since they were given the same remote
Zhi Huange818b6e2018-02-22 15:26:27 -0800260 // fingerprint.
Zhi Huange830e682018-03-30 10:48:35 -0700261 auto jsep_transport = GetJsepTransportByName(transport_name);
262 if (!jsep_transport) {
263 return nullptr;
264 }
265 auto dtls = jsep_transport->rtp_dtls_transport();
Zhi Huange818b6e2018-02-22 15:26:27 -0800266 if (!dtls) {
267 return nullptr;
268 }
269
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800270 return dtls->GetRemoteSSLCertChain();
Zhi Huange818b6e2018-02-22 15:26:27 -0800271}
272
273void JsepTransportController::MaybeStartGathering() {
274 if (!network_thread_->IsCurrent()) {
275 network_thread_->Invoke<void>(RTC_FROM_HERE,
276 [&] { MaybeStartGathering(); });
277 return;
278 }
279
280 for (auto& dtls : GetDtlsTransports()) {
281 dtls->ice_transport()->MaybeStartGathering();
282 }
283}
284
285RTCError JsepTransportController::AddRemoteCandidates(
286 const std::string& transport_name,
287 const cricket::Candidates& candidates) {
288 if (!network_thread_->IsCurrent()) {
289 return network_thread_->Invoke<RTCError>(RTC_FROM_HERE, [&] {
290 return AddRemoteCandidates(transport_name, candidates);
291 });
292 }
293
294 // Verify each candidate before passing down to the transport layer.
295 RTCError error = VerifyCandidates(candidates);
296 if (!error.ok()) {
297 return error;
298 }
Zhi Huange830e682018-03-30 10:48:35 -0700299 auto jsep_transport = GetJsepTransportByName(transport_name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800300 if (!jsep_transport) {
Zhi Huange830e682018-03-30 10:48:35 -0700301 RTC_LOG(LS_WARNING) << "Not adding candidate because the JsepTransport "
302 "doesn't exist. Ignore it.";
303 return RTCError::OK();
Zhi Huange818b6e2018-02-22 15:26:27 -0800304 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800305 return jsep_transport->AddRemoteCandidates(candidates);
306}
307
308RTCError JsepTransportController::RemoveRemoteCandidates(
309 const cricket::Candidates& candidates) {
310 if (!network_thread_->IsCurrent()) {
311 return network_thread_->Invoke<RTCError>(
312 RTC_FROM_HERE, [&] { return RemoveRemoteCandidates(candidates); });
313 }
314
315 // Verify each candidate before passing down to the transport layer.
316 RTCError error = VerifyCandidates(candidates);
317 if (!error.ok()) {
318 return error;
319 }
320
321 std::map<std::string, cricket::Candidates> candidates_by_transport_name;
322 for (const cricket::Candidate& cand : candidates) {
323 if (!cand.transport_name().empty()) {
324 candidates_by_transport_name[cand.transport_name()].push_back(cand);
325 } else {
326 RTC_LOG(LS_ERROR) << "Not removing candidate because it does not have a "
327 "transport name set: "
328 << cand.ToString();
329 }
330 }
331
332 for (const auto& kv : candidates_by_transport_name) {
333 const std::string& transport_name = kv.first;
334 const cricket::Candidates& candidates = kv.second;
Zhi Huang365381f2018-04-13 16:44:34 -0700335 cricket::JsepTransport* jsep_transport =
Zhi Huange830e682018-03-30 10:48:35 -0700336 GetJsepTransportByName(transport_name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800337 if (!jsep_transport) {
Zhi Huange830e682018-03-30 10:48:35 -0700338 RTC_LOG(LS_WARNING)
339 << "Not removing candidate because the JsepTransport doesn't exist.";
340 continue;
Zhi Huange818b6e2018-02-22 15:26:27 -0800341 }
342 for (const cricket::Candidate& candidate : candidates) {
343 auto dtls = candidate.component() == cricket::ICE_CANDIDATE_COMPONENT_RTP
344 ? jsep_transport->rtp_dtls_transport()
345 : jsep_transport->rtcp_dtls_transport();
346 if (dtls) {
347 dtls->ice_transport()->RemoveRemoteCandidate(candidate);
348 }
349 }
350 }
351 return RTCError::OK();
352}
353
354bool JsepTransportController::GetStats(const std::string& transport_name,
355 cricket::TransportStats* stats) {
356 if (!network_thread_->IsCurrent()) {
357 return network_thread_->Invoke<bool>(
358 RTC_FROM_HERE, [=] { return GetStats(transport_name, stats); });
359 }
360
Zhi Huang365381f2018-04-13 16:44:34 -0700361 cricket::JsepTransport* transport = GetJsepTransportByName(transport_name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800362 if (!transport) {
363 return false;
364 }
365 return transport->GetStats(stats);
366}
367
368void JsepTransportController::SetMetricsObserver(
369 webrtc::MetricsObserverInterface* metrics_observer) {
370 if (!network_thread_->IsCurrent()) {
371 network_thread_->Invoke<void>(
372 RTC_FROM_HERE, [=] { SetMetricsObserver(metrics_observer); });
373 return;
374 }
375
376 metrics_observer_ = metrics_observer;
377 for (auto& dtls : GetDtlsTransports()) {
378 dtls->ice_transport()->SetMetricsObserver(metrics_observer);
379 }
380}
381
382std::unique_ptr<cricket::DtlsTransportInternal>
383JsepTransportController::CreateDtlsTransport(const std::string& transport_name,
384 bool rtcp) {
385 RTC_DCHECK(network_thread_->IsCurrent());
386 int component = rtcp ? cricket::ICE_CANDIDATE_COMPONENT_RTCP
387 : cricket::ICE_CANDIDATE_COMPONENT_RTP;
388
389 std::unique_ptr<cricket::DtlsTransportInternal> dtls;
390 if (config_.external_transport_factory) {
391 auto ice = config_.external_transport_factory->CreateIceTransport(
392 transport_name, component);
393 dtls = config_.external_transport_factory->CreateDtlsTransport(
394 std::move(ice), config_.crypto_options);
395 } else {
396 auto ice = rtc::MakeUnique<cricket::P2PTransportChannel>(
397 transport_name, component, port_allocator_);
398 dtls = rtc::MakeUnique<cricket::DtlsTransport>(std::move(ice),
399 config_.crypto_options);
400 }
401
402 RTC_DCHECK(dtls);
403 dtls->SetSslMaxProtocolVersion(config_.ssl_max_version);
404 dtls->ice_transport()->SetMetricsObserver(metrics_observer_);
405 dtls->ice_transport()->SetIceRole(ice_role_);
406 dtls->ice_transport()->SetIceTiebreaker(ice_tiebreaker_);
407 dtls->ice_transport()->SetIceConfig(ice_config_);
408 if (certificate_) {
409 bool set_cert_success = dtls->SetLocalCertificate(certificate_);
410 RTC_DCHECK(set_cert_success);
411 }
412
413 // Connect to signals offered by the DTLS and ICE transport.
414 dtls->SignalWritableState.connect(
415 this, &JsepTransportController::OnTransportWritableState_n);
416 dtls->SignalReceivingState.connect(
417 this, &JsepTransportController::OnTransportReceivingState_n);
418 dtls->SignalDtlsHandshakeError.connect(
419 this, &JsepTransportController::OnDtlsHandshakeError);
420 dtls->ice_transport()->SignalGatheringState.connect(
421 this, &JsepTransportController::OnTransportGatheringState_n);
422 dtls->ice_transport()->SignalCandidateGathered.connect(
423 this, &JsepTransportController::OnTransportCandidateGathered_n);
424 dtls->ice_transport()->SignalCandidatesRemoved.connect(
425 this, &JsepTransportController::OnTransportCandidatesRemoved_n);
426 dtls->ice_transport()->SignalRoleConflict.connect(
427 this, &JsepTransportController::OnTransportRoleConflict_n);
428 dtls->ice_transport()->SignalStateChanged.connect(
429 this, &JsepTransportController::OnTransportStateChanged_n);
430 return dtls;
431}
432
433std::unique_ptr<webrtc::RtpTransport>
434JsepTransportController::CreateUnencryptedRtpTransport(
435 const std::string& transport_name,
436 rtc::PacketTransportInternal* rtp_packet_transport,
437 rtc::PacketTransportInternal* rtcp_packet_transport) {
438 RTC_DCHECK(network_thread_->IsCurrent());
Zhi Huange830e682018-03-30 10:48:35 -0700439 auto unencrypted_rtp_transport =
440 rtc::MakeUnique<RtpTransport>(rtcp_packet_transport == nullptr);
441 unencrypted_rtp_transport->SetRtpPacketTransport(rtp_packet_transport);
442 if (rtcp_packet_transport) {
443 unencrypted_rtp_transport->SetRtcpPacketTransport(rtcp_packet_transport);
444 }
445 return unencrypted_rtp_transport;
Zhi Huange818b6e2018-02-22 15:26:27 -0800446}
447
448std::unique_ptr<webrtc::SrtpTransport>
449JsepTransportController::CreateSdesTransport(
450 const std::string& transport_name,
Zhi Huange830e682018-03-30 10:48:35 -0700451 cricket::DtlsTransportInternal* rtp_dtls_transport,
452 cricket::DtlsTransportInternal* rtcp_dtls_transport) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800453 RTC_DCHECK(network_thread_->IsCurrent());
Zhi Huange818b6e2018-02-22 15:26:27 -0800454 auto srtp_transport =
Zhi Huange830e682018-03-30 10:48:35 -0700455 rtc::MakeUnique<webrtc::SrtpTransport>(rtcp_dtls_transport == nullptr);
456 RTC_DCHECK(rtp_dtls_transport);
457 srtp_transport->SetRtpPacketTransport(rtp_dtls_transport);
458 if (rtcp_dtls_transport) {
459 srtp_transport->SetRtcpPacketTransport(rtcp_dtls_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800460 }
461 if (config_.enable_external_auth) {
462 srtp_transport->EnableExternalAuth();
463 }
464 return srtp_transport;
465}
466
467std::unique_ptr<webrtc::DtlsSrtpTransport>
468JsepTransportController::CreateDtlsSrtpTransport(
469 const std::string& transport_name,
470 cricket::DtlsTransportInternal* rtp_dtls_transport,
471 cricket::DtlsTransportInternal* rtcp_dtls_transport) {
472 RTC_DCHECK(network_thread_->IsCurrent());
Zhi Huang365381f2018-04-13 16:44:34 -0700473 auto dtls_srtp_transport = rtc::MakeUnique<webrtc::DtlsSrtpTransport>(
474 rtcp_dtls_transport == nullptr);
Zhi Huang27f3bf52018-03-26 21:37:23 -0700475 if (config_.enable_external_auth) {
Zhi Huang365381f2018-04-13 16:44:34 -0700476 dtls_srtp_transport->EnableExternalAuth();
Zhi Huang27f3bf52018-03-26 21:37:23 -0700477 }
Zhi Huang97d5e5b2018-03-27 00:09:01 +0000478
Zhi Huange818b6e2018-02-22 15:26:27 -0800479 dtls_srtp_transport->SetDtlsTransports(rtp_dtls_transport,
480 rtcp_dtls_transport);
481 return dtls_srtp_transport;
482}
483
484std::vector<cricket::DtlsTransportInternal*>
485JsepTransportController::GetDtlsTransports() {
486 std::vector<cricket::DtlsTransportInternal*> dtls_transports;
Zhi Huange830e682018-03-30 10:48:35 -0700487 for (auto it = jsep_transports_by_name_.begin();
488 it != jsep_transports_by_name_.end(); ++it) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800489 auto jsep_transport = it->second.get();
490 RTC_DCHECK(jsep_transport);
491 if (jsep_transport->rtp_dtls_transport()) {
492 dtls_transports.push_back(jsep_transport->rtp_dtls_transport());
493 }
494
495 if (jsep_transport->rtcp_dtls_transport()) {
496 dtls_transports.push_back(jsep_transport->rtcp_dtls_transport());
497 }
498 }
499 return dtls_transports;
500}
501
502void JsepTransportController::OnMessage(rtc::Message* pmsg) {
503 RTC_DCHECK(signaling_thread_->IsCurrent());
504
505 switch (pmsg->message_id) {
506 case MSG_ICECONNECTIONSTATE: {
507 rtc::TypedMessageData<cricket::IceConnectionState>* data =
508 static_cast<rtc::TypedMessageData<cricket::IceConnectionState>*>(
509 pmsg->pdata);
510 SignalIceConnectionState(data->data());
511 delete data;
512 break;
513 }
514 case MSG_ICEGATHERINGSTATE: {
515 rtc::TypedMessageData<cricket::IceGatheringState>* data =
516 static_cast<rtc::TypedMessageData<cricket::IceGatheringState>*>(
517 pmsg->pdata);
518 SignalIceGatheringState(data->data());
519 delete data;
520 break;
521 }
522 case MSG_ICECANDIDATESGATHERED: {
523 CandidatesData* data = static_cast<CandidatesData*>(pmsg->pdata);
524 SignalIceCandidatesGathered(data->transport_name, data->candidates);
525 delete data;
526 break;
527 }
528 default:
529 RTC_NOTREACHED();
530 }
531}
532
533RTCError JsepTransportController::ApplyDescription_n(
534 bool local,
535 SdpType type,
536 const cricket::SessionDescription* description) {
537 RTC_DCHECK(network_thread_->IsCurrent());
538 RTC_DCHECK(description);
539
540 if (local) {
541 local_desc_ = description;
542 } else {
543 remote_desc_ = description;
544 }
545
Zhi Huange830e682018-03-30 10:48:35 -0700546 RTCError error;
Zhi Huangd2248f82018-04-10 14:41:03 -0700547 error = ValidateAndMaybeUpdateBundleGroup(local, type, description);
Zhi Huange830e682018-03-30 10:48:35 -0700548 if (!error.ok()) {
549 return error;
Zhi Huange818b6e2018-02-22 15:26:27 -0800550 }
551
552 std::vector<int> merged_encrypted_extension_ids;
553 if (bundle_group_) {
554 merged_encrypted_extension_ids =
555 MergeEncryptedHeaderExtensionIdsForBundle(description);
556 }
557
558 for (const cricket::ContentInfo& content_info : description->contents()) {
559 // Don't create transports for rejected m-lines and bundled m-lines."
560 if (content_info.rejected ||
561 (IsBundled(content_info.name) && content_info.name != *bundled_mid())) {
562 continue;
563 }
Zhi Huangd2248f82018-04-10 14:41:03 -0700564 error = MaybeCreateJsepTransport(content_info);
Zhi Huange830e682018-03-30 10:48:35 -0700565 if (!error.ok()) {
566 return error;
567 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800568 }
569
570 RTC_DCHECK(description->contents().size() ==
571 description->transport_infos().size());
572 for (size_t i = 0; i < description->contents().size(); ++i) {
573 const cricket::ContentInfo& content_info = description->contents()[i];
574 const cricket::TransportInfo& transport_info =
575 description->transport_infos()[i];
576 if (content_info.rejected) {
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700577 HandleRejectedContent(content_info, description);
Zhi Huange818b6e2018-02-22 15:26:27 -0800578 continue;
579 }
580
581 if (IsBundled(content_info.name) && content_info.name != *bundled_mid()) {
Zhi Huang365381f2018-04-13 16:44:34 -0700582 if (!HandleBundledContent(content_info)) {
583 return RTCError(RTCErrorType::INVALID_PARAMETER,
584 "Failed to process the bundled m= section.");
585 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800586 continue;
587 }
588
Zhi Huange830e682018-03-30 10:48:35 -0700589 error = ValidateContent(content_info);
590 if (!error.ok()) {
591 return error;
592 }
593
Zhi Huange818b6e2018-02-22 15:26:27 -0800594 std::vector<int> extension_ids;
Taylor Brandstetter0ab56512018-04-12 10:30:48 -0700595 if (bundled_mid() && content_info.name == *bundled_mid()) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800596 extension_ids = merged_encrypted_extension_ids;
597 } else {
598 extension_ids = GetEncryptedHeaderExtensionIds(content_info);
599 }
600
Zhi Huange830e682018-03-30 10:48:35 -0700601 int rtp_abs_sendtime_extn_id =
602 GetRtpAbsSendTimeHeaderExtensionId(content_info);
603
Zhi Huang365381f2018-04-13 16:44:34 -0700604 cricket::JsepTransport* transport =
Zhi Huange830e682018-03-30 10:48:35 -0700605 GetJsepTransportForMid(content_info.name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800606 RTC_DCHECK(transport);
607
608 SetIceRole_n(DetermineIceRole(transport, transport_info, type, local));
609
Zhi Huange818b6e2018-02-22 15:26:27 -0800610 cricket::JsepTransportDescription jsep_description =
611 CreateJsepTransportDescription(content_info, transport_info,
Zhi Huange830e682018-03-30 10:48:35 -0700612 extension_ids, rtp_abs_sendtime_extn_id);
Zhi Huange818b6e2018-02-22 15:26:27 -0800613 if (local) {
614 error =
615 transport->SetLocalJsepTransportDescription(jsep_description, type);
616 } else {
617 error =
618 transport->SetRemoteJsepTransportDescription(jsep_description, type);
619 }
620
621 if (!error.ok()) {
622 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
623 "Failed to apply the description for " +
624 content_info.name + ": " + error.message());
625 }
626 }
627 return RTCError::OK();
628}
629
Zhi Huangd2248f82018-04-10 14:41:03 -0700630RTCError JsepTransportController::ValidateAndMaybeUpdateBundleGroup(
631 bool local,
632 SdpType type,
Zhi Huange830e682018-03-30 10:48:35 -0700633 const cricket::SessionDescription* description) {
634 RTC_DCHECK(description);
Zhi Huangd2248f82018-04-10 14:41:03 -0700635 const cricket::ContentGroup* new_bundle_group =
636 description->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
637
638 // The BUNDLE group containing a MID that no m= section has is invalid.
639 if (new_bundle_group) {
640 for (auto content_name : new_bundle_group->content_names()) {
641 if (!description->GetContentByName(content_name)) {
642 return RTCError(RTCErrorType::INVALID_PARAMETER,
643 "The BUNDLE group contains MID:" + content_name +
644 " matching no m= section.");
645 }
646 }
647 }
648
649 if (type == SdpType::kAnswer) {
650 const cricket::ContentGroup* offered_bundle_group =
651 local ? remote_desc_->GetGroupByName(cricket::GROUP_TYPE_BUNDLE)
652 : local_desc_->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
653
654 if (new_bundle_group) {
655 // The BUNDLE group in answer should be a subset of offered group.
656 for (auto content_name : new_bundle_group->content_names()) {
657 if (!offered_bundle_group ||
658 !offered_bundle_group->HasContentName(content_name)) {
659 return RTCError(RTCErrorType::INVALID_PARAMETER,
660 "The BUNDLE group in answer contains a MID that was "
661 "not in the offered group.");
662 }
663 }
664 }
665
666 if (bundle_group_) {
667 for (auto content_name : bundle_group_->content_names()) {
668 // An answer that removes m= sections from pre-negotiated BUNDLE group
669 // without rejecting it, is invalid.
670 if (!new_bundle_group ||
671 !new_bundle_group->HasContentName(content_name)) {
672 auto* content_info = description->GetContentByName(content_name);
673 if (!content_info || !content_info->rejected) {
674 return RTCError(RTCErrorType::INVALID_PARAMETER,
675 "Answer cannot remove m= section " + content_name +
676 " from already-established BUNDLE group.");
677 }
678 }
679 }
680 }
681 }
682
683 if (config_.bundle_policy ==
684 PeerConnectionInterface::kBundlePolicyMaxBundle &&
685 !description->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
686 return RTCError(RTCErrorType::INVALID_PARAMETER,
687 "max-bundle is used but no bundle group found.");
688 }
689
690 if (ShouldUpdateBundleGroup(type, description)) {
Taylor Brandstetter0ab56512018-04-12 10:30:48 -0700691 const std::string* new_bundled_mid = new_bundle_group->FirstContentName();
692 if (bundled_mid() && new_bundled_mid &&
693 *bundled_mid() != *new_bundled_mid) {
Zhi Huangd2248f82018-04-10 14:41:03 -0700694 return RTCError(RTCErrorType::UNSUPPORTED_OPERATION,
695 "Changing the negotiated BUNDLE-tag is not supported.");
696 }
697
698 bundle_group_ = *new_bundle_group;
699 }
Zhi Huange830e682018-03-30 10:48:35 -0700700
701 if (!bundled_mid()) {
702 return RTCError::OK();
703 }
704
705 auto bundled_content = description->GetContentByName(*bundled_mid());
706 if (!bundled_content) {
707 return RTCError(
708 RTCErrorType::INVALID_PARAMETER,
709 "An m= section associated with the BUNDLE-tag doesn't exist.");
710 }
711
712 // If the |bundled_content| is rejected, other contents in the bundle group
713 // should be rejected.
714 if (bundled_content->rejected) {
715 for (auto content_name : bundle_group_->content_names()) {
716 auto other_content = description->GetContentByName(content_name);
717 if (!other_content->rejected) {
718 return RTCError(
719 RTCErrorType::INVALID_PARAMETER,
720 "The m= section:" + content_name + " should be rejected.");
721 }
722 }
723 }
724
725 return RTCError::OK();
726}
727
728RTCError JsepTransportController::ValidateContent(
729 const cricket::ContentInfo& content_info) {
730 if (config_.rtcp_mux_policy ==
731 PeerConnectionInterface::kRtcpMuxPolicyRequire &&
732 content_info.type == cricket::MediaProtocolType::kRtp &&
733 !content_info.media_description()->rtcp_mux()) {
734 return RTCError(RTCErrorType::INVALID_PARAMETER,
735 "The m= section:" + content_info.name +
736 " is invalid. RTCP-MUX is not "
737 "enabled when it is required.");
738 }
739 return RTCError::OK();
740}
741
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700742void JsepTransportController::HandleRejectedContent(
Zhi Huangd2248f82018-04-10 14:41:03 -0700743 const cricket::ContentInfo& content_info,
744 const cricket::SessionDescription* description) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800745 // If the content is rejected, let the
746 // BaseChannel/SctpTransport change the RtpTransport/DtlsTransport first,
Zhi Huang365381f2018-04-13 16:44:34 -0700747 // then destroy the cricket::JsepTransport.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700748 RemoveTransportForMid(content_info.name);
Zhi Huange830e682018-03-30 10:48:35 -0700749 if (content_info.name == bundled_mid()) {
750 for (auto content_name : bundle_group_->content_names()) {
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700751 RemoveTransportForMid(content_name);
Zhi Huange830e682018-03-30 10:48:35 -0700752 }
753 bundle_group_.reset();
754 } else if (IsBundled(content_info.name)) {
755 // Remove the rejected content from the |bundle_group_|.
Zhi Huange818b6e2018-02-22 15:26:27 -0800756 bundle_group_->RemoveContentName(content_info.name);
Zhi Huange830e682018-03-30 10:48:35 -0700757 // Reset the bundle group if nothing left.
758 if (!bundle_group_->FirstContentName()) {
759 bundle_group_.reset();
760 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800761 }
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700762 MaybeDestroyJsepTransport(content_info.name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800763}
764
Zhi Huang365381f2018-04-13 16:44:34 -0700765bool JsepTransportController::HandleBundledContent(
Zhi Huange818b6e2018-02-22 15:26:27 -0800766 const cricket::ContentInfo& content_info) {
Zhi Huangd2248f82018-04-10 14:41:03 -0700767 auto jsep_transport = GetJsepTransportByName(*bundled_mid());
768 RTC_DCHECK(jsep_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800769 // If the content is bundled, let the
770 // BaseChannel/SctpTransport change the RtpTransport/DtlsTransport first,
Zhi Huang365381f2018-04-13 16:44:34 -0700771 // then destroy the cricket::JsepTransport.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700772 if (SetTransportForMid(content_info.name, jsep_transport)) {
Zhi Huang365381f2018-04-13 16:44:34 -0700773 MaybeDestroyJsepTransport(content_info.name);
774 return true;
775 }
776 return false;
Zhi Huange818b6e2018-02-22 15:26:27 -0800777}
778
Zhi Huang365381f2018-04-13 16:44:34 -0700779bool JsepTransportController::SetTransportForMid(
Zhi Huangd2248f82018-04-10 14:41:03 -0700780 const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700781 cricket::JsepTransport* jsep_transport) {
Zhi Huang365381f2018-04-13 16:44:34 -0700782 RTC_DCHECK(jsep_transport);
Zhi Huangd2248f82018-04-10 14:41:03 -0700783 if (mid_to_transport_[mid] == jsep_transport) {
Zhi Huang365381f2018-04-13 16:44:34 -0700784 return true;
Zhi Huangd2248f82018-04-10 14:41:03 -0700785 }
786
787 mid_to_transport_[mid] = jsep_transport;
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700788 return config_.transport_observer->OnTransportChanged(
789 mid, jsep_transport->rtp_transport(),
790 jsep_transport->rtp_dtls_transport());
Zhi Huangd2248f82018-04-10 14:41:03 -0700791}
792
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700793void JsepTransportController::RemoveTransportForMid(const std::string& mid) {
794 bool ret =
795 config_.transport_observer->OnTransportChanged(mid, nullptr, nullptr);
796 // Calling OnTransportChanged with nullptr should always succeed, since it is
797 // only expected to fail when adding media to a transport (not removing).
798 RTC_DCHECK(ret);
Zhi Huangd2248f82018-04-10 14:41:03 -0700799 mid_to_transport_.erase(mid);
800}
801
Zhi Huange818b6e2018-02-22 15:26:27 -0800802cricket::JsepTransportDescription
803JsepTransportController::CreateJsepTransportDescription(
804 cricket::ContentInfo content_info,
805 cricket::TransportInfo transport_info,
Zhi Huange830e682018-03-30 10:48:35 -0700806 const std::vector<int>& encrypted_extension_ids,
807 int rtp_abs_sendtime_extn_id) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800808 const cricket::MediaContentDescription* content_desc =
809 static_cast<const cricket::MediaContentDescription*>(
810 content_info.description);
811 RTC_DCHECK(content_desc);
812 bool rtcp_mux_enabled = content_info.type == cricket::MediaProtocolType::kSctp
813 ? true
814 : content_desc->rtcp_mux();
815
816 return cricket::JsepTransportDescription(
817 rtcp_mux_enabled, content_desc->cryptos(), encrypted_extension_ids,
Zhi Huange830e682018-03-30 10:48:35 -0700818 rtp_abs_sendtime_extn_id, transport_info.description);
Zhi Huange818b6e2018-02-22 15:26:27 -0800819}
820
821bool JsepTransportController::ShouldUpdateBundleGroup(
822 SdpType type,
823 const cricket::SessionDescription* description) {
824 if (config_.bundle_policy ==
825 PeerConnectionInterface::kBundlePolicyMaxBundle) {
826 return true;
827 }
828
829 if (type != SdpType::kAnswer) {
830 return false;
831 }
832
833 RTC_DCHECK(local_desc_ && remote_desc_);
834 const cricket::ContentGroup* local_bundle =
835 local_desc_->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
836 const cricket::ContentGroup* remote_bundle =
837 remote_desc_->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
838 return local_bundle && remote_bundle;
839}
840
841std::vector<int> JsepTransportController::GetEncryptedHeaderExtensionIds(
842 const cricket::ContentInfo& content_info) {
843 const cricket::MediaContentDescription* content_desc =
844 static_cast<const cricket::MediaContentDescription*>(
845 content_info.description);
846
847 if (!config_.crypto_options.enable_encrypted_rtp_header_extensions) {
848 return std::vector<int>();
849 }
850
851 std::vector<int> encrypted_header_extension_ids;
852 for (auto extension : content_desc->rtp_header_extensions()) {
853 if (!extension.encrypt) {
854 continue;
855 }
856 auto it = std::find(encrypted_header_extension_ids.begin(),
857 encrypted_header_extension_ids.end(), extension.id);
858 if (it == encrypted_header_extension_ids.end()) {
859 encrypted_header_extension_ids.push_back(extension.id);
860 }
861 }
862 return encrypted_header_extension_ids;
863}
864
865std::vector<int>
866JsepTransportController::MergeEncryptedHeaderExtensionIdsForBundle(
867 const cricket::SessionDescription* description) {
868 RTC_DCHECK(description);
869 RTC_DCHECK(bundle_group_);
870
871 std::vector<int> merged_ids;
872 // Union the encrypted header IDs in the group when bundle is enabled.
873 for (const cricket::ContentInfo& content_info : description->contents()) {
874 if (bundle_group_->HasContentName(content_info.name)) {
875 std::vector<int> extension_ids =
876 GetEncryptedHeaderExtensionIds(content_info);
877 for (int id : extension_ids) {
878 auto it = std::find(merged_ids.begin(), merged_ids.end(), id);
879 if (it == merged_ids.end()) {
880 merged_ids.push_back(id);
881 }
882 }
883 }
884 }
885 return merged_ids;
886}
887
Zhi Huange830e682018-03-30 10:48:35 -0700888int JsepTransportController::GetRtpAbsSendTimeHeaderExtensionId(
Zhi Huange818b6e2018-02-22 15:26:27 -0800889 const cricket::ContentInfo& content_info) {
Zhi Huange830e682018-03-30 10:48:35 -0700890 if (!config_.enable_external_auth) {
891 return -1;
Zhi Huange818b6e2018-02-22 15:26:27 -0800892 }
893
894 const cricket::MediaContentDescription* content_desc =
895 static_cast<const cricket::MediaContentDescription*>(
896 content_info.description);
Zhi Huange830e682018-03-30 10:48:35 -0700897
898 const webrtc::RtpExtension* send_time_extension =
899 webrtc::RtpExtension::FindHeaderExtensionByUri(
900 content_desc->rtp_header_extensions(),
901 webrtc::RtpExtension::kAbsSendTimeUri);
902 return send_time_extension ? send_time_extension->id : -1;
903}
904
Zhi Huang365381f2018-04-13 16:44:34 -0700905const cricket::JsepTransport* JsepTransportController::GetJsepTransportForMid(
Zhi Huange830e682018-03-30 10:48:35 -0700906 const std::string& mid) const {
Zhi Huangd2248f82018-04-10 14:41:03 -0700907 auto it = mid_to_transport_.find(mid);
908 return it == mid_to_transport_.end() ? nullptr : it->second;
Zhi Huange830e682018-03-30 10:48:35 -0700909}
910
Zhi Huang365381f2018-04-13 16:44:34 -0700911cricket::JsepTransport* JsepTransportController::GetJsepTransportForMid(
Zhi Huange830e682018-03-30 10:48:35 -0700912 const std::string& mid) {
Zhi Huangd2248f82018-04-10 14:41:03 -0700913 auto it = mid_to_transport_.find(mid);
914 return it == mid_to_transport_.end() ? nullptr : it->second;
Zhi Huange830e682018-03-30 10:48:35 -0700915}
916
Zhi Huang365381f2018-04-13 16:44:34 -0700917const cricket::JsepTransport* JsepTransportController::GetJsepTransportByName(
Zhi Huange830e682018-03-30 10:48:35 -0700918 const std::string& transport_name) const {
919 auto it = jsep_transports_by_name_.find(transport_name);
920 return (it == jsep_transports_by_name_.end()) ? nullptr : it->second.get();
921}
922
Zhi Huang365381f2018-04-13 16:44:34 -0700923cricket::JsepTransport* JsepTransportController::GetJsepTransportByName(
Zhi Huange830e682018-03-30 10:48:35 -0700924 const std::string& transport_name) {
925 auto it = jsep_transports_by_name_.find(transport_name);
926 return (it == jsep_transports_by_name_.end()) ? nullptr : it->second.get();
927}
928
929RTCError JsepTransportController::MaybeCreateJsepTransport(
Zhi Huange830e682018-03-30 10:48:35 -0700930 const cricket::ContentInfo& content_info) {
931 RTC_DCHECK(network_thread_->IsCurrent());
Zhi Huang365381f2018-04-13 16:44:34 -0700932 cricket::JsepTransport* transport = GetJsepTransportByName(content_info.name);
Zhi Huange830e682018-03-30 10:48:35 -0700933 if (transport) {
934 return RTCError::OK();
935 }
936
937 const cricket::MediaContentDescription* content_desc =
938 static_cast<const cricket::MediaContentDescription*>(
939 content_info.description);
940 if (certificate_ && !content_desc->cryptos().empty()) {
941 return RTCError(RTCErrorType::INVALID_PARAMETER,
942 "SDES and DTLS-SRTP cannot be enabled at the same time.");
943 }
944
Zhi Huange818b6e2018-02-22 15:26:27 -0800945 std::unique_ptr<cricket::DtlsTransportInternal> rtp_dtls_transport =
Zhi Huangd2248f82018-04-10 14:41:03 -0700946 CreateDtlsTransport(content_info.name, /*rtcp =*/false);
Zhi Huange818b6e2018-02-22 15:26:27 -0800947 std::unique_ptr<cricket::DtlsTransportInternal> rtcp_dtls_transport;
Zhi Huange830e682018-03-30 10:48:35 -0700948 if (config_.rtcp_mux_policy !=
949 PeerConnectionInterface::kRtcpMuxPolicyRequire &&
950 content_info.type == cricket::MediaProtocolType::kRtp) {
Zhi Huangd2248f82018-04-10 14:41:03 -0700951 rtcp_dtls_transport =
952 CreateDtlsTransport(content_info.name, /*rtcp =*/true);
Zhi Huange818b6e2018-02-22 15:26:27 -0800953 }
954
955 std::unique_ptr<RtpTransport> unencrypted_rtp_transport;
956 std::unique_ptr<SrtpTransport> sdes_transport;
957 std::unique_ptr<DtlsSrtpTransport> dtls_srtp_transport;
958 if (config_.disable_encryption) {
959 unencrypted_rtp_transport = CreateUnencryptedRtpTransport(
Zhi Huangd2248f82018-04-10 14:41:03 -0700960 content_info.name, rtp_dtls_transport.get(), rtcp_dtls_transport.get());
Zhi Huange818b6e2018-02-22 15:26:27 -0800961 } else if (!content_desc->cryptos().empty()) {
Zhi Huangd2248f82018-04-10 14:41:03 -0700962 sdes_transport = CreateSdesTransport(
963 content_info.name, rtp_dtls_transport.get(), rtcp_dtls_transport.get());
Zhi Huange818b6e2018-02-22 15:26:27 -0800964 } else {
Zhi Huangd2248f82018-04-10 14:41:03 -0700965 dtls_srtp_transport = CreateDtlsSrtpTransport(
966 content_info.name, rtp_dtls_transport.get(), rtcp_dtls_transport.get());
Zhi Huange818b6e2018-02-22 15:26:27 -0800967 }
968
Zhi Huang365381f2018-04-13 16:44:34 -0700969 std::unique_ptr<cricket::JsepTransport> jsep_transport =
970 rtc::MakeUnique<cricket::JsepTransport>(
Zhi Huangd2248f82018-04-10 14:41:03 -0700971 content_info.name, certificate_, std::move(unencrypted_rtp_transport),
Zhi Huange818b6e2018-02-22 15:26:27 -0800972 std::move(sdes_transport), std::move(dtls_srtp_transport),
973 std::move(rtp_dtls_transport), std::move(rtcp_dtls_transport));
974 jsep_transport->SignalRtcpMuxActive.connect(
975 this, &JsepTransportController::UpdateAggregateStates_n);
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700976 SetTransportForMid(content_info.name, jsep_transport.get());
Zhi Huange830e682018-03-30 10:48:35 -0700977
Zhi Huangd2248f82018-04-10 14:41:03 -0700978 jsep_transports_by_name_[content_info.name] = std::move(jsep_transport);
979 UpdateAggregateStates_n();
Zhi Huange830e682018-03-30 10:48:35 -0700980 return RTCError::OK();
Zhi Huange818b6e2018-02-22 15:26:27 -0800981}
982
983void JsepTransportController::MaybeDestroyJsepTransport(
984 const std::string& mid) {
Zhi Huangd2248f82018-04-10 14:41:03 -0700985 auto jsep_transport = GetJsepTransportByName(mid);
986 if (!jsep_transport) {
987 return;
988 }
989
990 // Don't destroy the JsepTransport if there are still media sections referring
991 // to it.
992 for (const auto& kv : mid_to_transport_) {
993 if (kv.second == jsep_transport) {
994 return;
995 }
996 }
Zhi Huange830e682018-03-30 10:48:35 -0700997 jsep_transports_by_name_.erase(mid);
Zhi Huange818b6e2018-02-22 15:26:27 -0800998 UpdateAggregateStates_n();
999}
1000
1001void JsepTransportController::DestroyAllJsepTransports_n() {
1002 RTC_DCHECK(network_thread_->IsCurrent());
Zhi Huange830e682018-03-30 10:48:35 -07001003 jsep_transports_by_name_.clear();
Zhi Huange818b6e2018-02-22 15:26:27 -08001004}
1005
1006void JsepTransportController::SetIceRole_n(cricket::IceRole ice_role) {
1007 RTC_DCHECK(network_thread_->IsCurrent());
1008
1009 ice_role_ = ice_role;
1010 for (auto& dtls : GetDtlsTransports()) {
1011 dtls->ice_transport()->SetIceRole(ice_role_);
1012 }
1013}
1014
1015cricket::IceRole JsepTransportController::DetermineIceRole(
Zhi Huang365381f2018-04-13 16:44:34 -07001016 cricket::JsepTransport* jsep_transport,
Zhi Huange818b6e2018-02-22 15:26:27 -08001017 const cricket::TransportInfo& transport_info,
1018 SdpType type,
1019 bool local) {
1020 cricket::IceRole ice_role = ice_role_;
1021 auto tdesc = transport_info.description;
1022 if (local) {
1023 // The initial offer side may use ICE Lite, in which case, per RFC5245
1024 // Section 5.1.1, the answer side should take the controlling role if it is
1025 // in the full ICE mode.
1026 //
1027 // When both sides use ICE Lite, the initial offer side must take the
1028 // controlling role, and this is the default logic implemented in
1029 // SetLocalDescription in JsepTransportController.
1030 if (jsep_transport->remote_description() &&
1031 jsep_transport->remote_description()->transport_desc.ice_mode ==
1032 cricket::ICEMODE_LITE &&
1033 ice_role_ == cricket::ICEROLE_CONTROLLED &&
1034 tdesc.ice_mode == cricket::ICEMODE_FULL) {
1035 ice_role = cricket::ICEROLE_CONTROLLING;
1036 }
1037
1038 // Older versions of Chrome expect the ICE role to be re-determined when an
1039 // ICE restart occurs, and also don't perform conflict resolution correctly,
1040 // so for now we can't safely stop doing this, unless the application opts
1041 // in by setting |config_.redetermine_role_on_ice_restart_| to false. See:
1042 // https://bugs.chromium.org/p/chromium/issues/detail?id=628676
1043 // TODO(deadbeef): Remove this when these old versions of Chrome reach a low
1044 // enough population.
1045 if (config_.redetermine_role_on_ice_restart &&
1046 jsep_transport->local_description() &&
1047 cricket::IceCredentialsChanged(
1048 jsep_transport->local_description()->transport_desc.ice_ufrag,
1049 jsep_transport->local_description()->transport_desc.ice_pwd,
1050 tdesc.ice_ufrag, tdesc.ice_pwd) &&
1051 // Don't change the ICE role if the remote endpoint is ICE lite; we
1052 // should always be controlling in that case.
1053 (!jsep_transport->remote_description() ||
1054 jsep_transport->remote_description()->transport_desc.ice_mode !=
1055 cricket::ICEMODE_LITE)) {
1056 ice_role = (type == SdpType::kOffer) ? cricket::ICEROLE_CONTROLLING
1057 : cricket::ICEROLE_CONTROLLED;
1058 }
1059 } else {
1060 // If our role is cricket::ICEROLE_CONTROLLED and the remote endpoint
1061 // supports only ice_lite, this local endpoint should take the CONTROLLING
1062 // role.
1063 // TODO(deadbeef): This is a session-level attribute, so it really shouldn't
1064 // be in a TransportDescription in the first place...
1065 if (ice_role_ == cricket::ICEROLE_CONTROLLED &&
1066 tdesc.ice_mode == cricket::ICEMODE_LITE) {
1067 ice_role = cricket::ICEROLE_CONTROLLING;
1068 }
1069
1070 // If we use ICE Lite and the remote endpoint uses the full implementation
1071 // of ICE, the local endpoint must take the controlled role, and the other
1072 // side must be the controlling role.
1073 if (jsep_transport->local_description() &&
1074 jsep_transport->local_description()->transport_desc.ice_mode ==
1075 cricket::ICEMODE_LITE &&
1076 ice_role_ == cricket::ICEROLE_CONTROLLING &&
Zhi Huange830e682018-03-30 10:48:35 -07001077 tdesc.ice_mode == cricket::ICEMODE_FULL) {
Zhi Huange818b6e2018-02-22 15:26:27 -08001078 ice_role = cricket::ICEROLE_CONTROLLED;
1079 }
1080 }
1081
1082 return ice_role;
1083}
1084
1085void JsepTransportController::OnTransportWritableState_n(
1086 rtc::PacketTransportInternal* transport) {
1087 RTC_DCHECK(network_thread_->IsCurrent());
1088 RTC_LOG(LS_INFO) << " Transport " << transport->transport_name()
1089 << " writability changed to " << transport->writable()
1090 << ".";
1091 UpdateAggregateStates_n();
1092}
1093
1094void JsepTransportController::OnTransportReceivingState_n(
1095 rtc::PacketTransportInternal* transport) {
1096 RTC_DCHECK(network_thread_->IsCurrent());
1097 UpdateAggregateStates_n();
1098}
1099
1100void JsepTransportController::OnTransportGatheringState_n(
1101 cricket::IceTransportInternal* transport) {
1102 RTC_DCHECK(network_thread_->IsCurrent());
1103 UpdateAggregateStates_n();
1104}
1105
1106void JsepTransportController::OnTransportCandidateGathered_n(
1107 cricket::IceTransportInternal* transport,
1108 const cricket::Candidate& candidate) {
1109 RTC_DCHECK(network_thread_->IsCurrent());
1110
1111 // We should never signal peer-reflexive candidates.
1112 if (candidate.type() == cricket::PRFLX_PORT_TYPE) {
1113 RTC_NOTREACHED();
1114 return;
1115 }
1116 std::vector<cricket::Candidate> candidates;
1117 candidates.push_back(candidate);
1118 CandidatesData* data =
1119 new CandidatesData(transport->transport_name(), candidates);
1120 signaling_thread_->Post(RTC_FROM_HERE, this, MSG_ICECANDIDATESGATHERED, data);
1121}
1122
1123void JsepTransportController::OnTransportCandidatesRemoved_n(
1124 cricket::IceTransportInternal* transport,
1125 const cricket::Candidates& candidates) {
1126 invoker_.AsyncInvoke<void>(
1127 RTC_FROM_HERE, signaling_thread_,
1128 rtc::Bind(&JsepTransportController::OnTransportCandidatesRemoved, this,
1129 candidates));
1130}
1131
1132void JsepTransportController::OnTransportCandidatesRemoved(
1133 const cricket::Candidates& candidates) {
1134 RTC_DCHECK(signaling_thread_->IsCurrent());
1135 SignalIceCandidatesRemoved(candidates);
1136}
1137
1138void JsepTransportController::OnTransportRoleConflict_n(
1139 cricket::IceTransportInternal* transport) {
1140 RTC_DCHECK(network_thread_->IsCurrent());
1141 // Note: since the role conflict is handled entirely on the network thread,
1142 // we don't need to worry about role conflicts occurring on two ports at
1143 // once. The first one encountered should immediately reverse the role.
1144 cricket::IceRole reversed_role = (ice_role_ == cricket::ICEROLE_CONTROLLING)
1145 ? cricket::ICEROLE_CONTROLLED
1146 : cricket::ICEROLE_CONTROLLING;
1147 RTC_LOG(LS_INFO) << "Got role conflict; switching to "
1148 << (reversed_role == cricket::ICEROLE_CONTROLLING
1149 ? "controlling"
1150 : "controlled")
1151 << " role.";
1152 SetIceRole_n(reversed_role);
1153}
1154
1155void JsepTransportController::OnTransportStateChanged_n(
1156 cricket::IceTransportInternal* transport) {
1157 RTC_DCHECK(network_thread_->IsCurrent());
1158 RTC_LOG(LS_INFO) << transport->transport_name() << " Transport "
1159 << transport->component()
1160 << " state changed. Check if state is complete.";
1161 UpdateAggregateStates_n();
1162}
1163
1164void JsepTransportController::UpdateAggregateStates_n() {
1165 RTC_DCHECK(network_thread_->IsCurrent());
1166
1167 auto dtls_transports = GetDtlsTransports();
1168 cricket::IceConnectionState new_connection_state =
1169 cricket::kIceConnectionConnecting;
1170 cricket::IceGatheringState new_gathering_state = cricket::kIceGatheringNew;
1171 bool any_failed = false;
1172 bool all_connected = !dtls_transports.empty();
1173 bool all_completed = !dtls_transports.empty();
1174 bool any_gathering = false;
1175 bool all_done_gathering = !dtls_transports.empty();
1176 for (const auto& dtls : dtls_transports) {
1177 any_failed = any_failed || dtls->ice_transport()->GetState() ==
1178 cricket::IceTransportState::STATE_FAILED;
1179 all_connected = all_connected && dtls->writable();
1180 all_completed =
1181 all_completed && dtls->writable() &&
1182 dtls->ice_transport()->GetState() ==
1183 cricket::IceTransportState::STATE_COMPLETED &&
1184 dtls->ice_transport()->GetIceRole() == cricket::ICEROLE_CONTROLLING &&
1185 dtls->ice_transport()->gathering_state() ==
1186 cricket::kIceGatheringComplete;
1187 any_gathering = any_gathering || dtls->ice_transport()->gathering_state() !=
1188 cricket::kIceGatheringNew;
1189 all_done_gathering =
1190 all_done_gathering && dtls->ice_transport()->gathering_state() ==
1191 cricket::kIceGatheringComplete;
1192 }
1193 if (any_failed) {
1194 new_connection_state = cricket::kIceConnectionFailed;
1195 } else if (all_completed) {
1196 new_connection_state = cricket::kIceConnectionCompleted;
1197 } else if (all_connected) {
1198 new_connection_state = cricket::kIceConnectionConnected;
1199 }
1200 if (ice_connection_state_ != new_connection_state) {
1201 ice_connection_state_ = new_connection_state;
1202 signaling_thread_->Post(
1203 RTC_FROM_HERE, this, MSG_ICECONNECTIONSTATE,
1204 new rtc::TypedMessageData<cricket::IceConnectionState>(
1205 new_connection_state));
1206 }
1207
1208 if (all_done_gathering) {
1209 new_gathering_state = cricket::kIceGatheringComplete;
1210 } else if (any_gathering) {
1211 new_gathering_state = cricket::kIceGatheringGathering;
1212 }
1213 if (ice_gathering_state_ != new_gathering_state) {
1214 ice_gathering_state_ = new_gathering_state;
1215 signaling_thread_->Post(
1216 RTC_FROM_HERE, this, MSG_ICEGATHERINGSTATE,
1217 new rtc::TypedMessageData<cricket::IceGatheringState>(
1218 new_gathering_state));
1219 }
1220}
1221
1222void JsepTransportController::OnDtlsHandshakeError(
1223 rtc::SSLHandshakeError error) {
1224 SignalDtlsHandshakeError(error);
1225}
1226
1227} // namespace webrtc