blob: 5163e016b3834f632f8ffec2294f04c12cbbf86e [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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/jsep_transport_controller.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080012
13#include <algorithm>
14#include <memory>
15#include <utility>
16
Zach Steinc64078f2018-11-27 15:53:01 -080017#include "absl/memory/memory.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080018#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "pc/srtp_filter.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080020#include "rtc_base/bind.h"
21#include "rtc_base/checks.h"
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -070022#include "rtc_base/key_derivation.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080023#include "rtc_base/thread.h"
24
25using webrtc::SdpType;
26
27namespace {
28
Zhi Huange818b6e2018-02-22 15:26:27 -080029webrtc::RTCError VerifyCandidate(const cricket::Candidate& cand) {
30 // No address zero.
31 if (cand.address().IsNil() || cand.address().IsAnyIP()) {
32 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
33 "candidate has address of zero");
34 }
35
36 // Disallow all ports below 1024, except for 80 and 443 on public addresses.
37 int port = cand.address().port();
38 if (cand.protocol() == cricket::TCP_PROTOCOL_NAME &&
39 (cand.tcptype() == cricket::TCPTYPE_ACTIVE_STR || port == 0)) {
40 // Expected for active-only candidates per
41 // http://tools.ietf.org/html/rfc6544#section-4.5 so no error.
42 // Libjingle clients emit port 0, in "active" mode.
43 return webrtc::RTCError::OK();
44 }
45 if (port < 1024) {
46 if ((port != 80) && (port != 443)) {
47 return webrtc::RTCError(
48 webrtc::RTCErrorType::INVALID_PARAMETER,
49 "candidate has port below 1024, but not 80 or 443");
50 }
51
52 if (cand.address().IsPrivateIP()) {
53 return webrtc::RTCError(
54 webrtc::RTCErrorType::INVALID_PARAMETER,
55 "candidate has port of 80 or 443 with private IP address");
56 }
57 }
58
59 return webrtc::RTCError::OK();
60}
61
62webrtc::RTCError VerifyCandidates(const cricket::Candidates& candidates) {
63 for (const cricket::Candidate& candidate : candidates) {
64 webrtc::RTCError error = VerifyCandidate(candidate);
65 if (!error.ok()) {
66 return error;
67 }
68 }
69 return webrtc::RTCError::OK();
70}
71
72} // namespace
73
74namespace webrtc {
75
76JsepTransportController::JsepTransportController(
77 rtc::Thread* signaling_thread,
78 rtc::Thread* network_thread,
79 cricket::PortAllocator* port_allocator,
Zach Steine20867f2018-08-02 13:20:15 -070080 AsyncResolverFactory* async_resolver_factory,
Zhi Huange818b6e2018-02-22 15:26:27 -080081 Config config)
82 : signaling_thread_(signaling_thread),
83 network_thread_(network_thread),
84 port_allocator_(port_allocator),
Zach Steine20867f2018-08-02 13:20:15 -070085 async_resolver_factory_(async_resolver_factory),
Zhi Huang365381f2018-04-13 16:44:34 -070086 config_(config) {
87 // The |transport_observer| is assumed to be non-null.
88 RTC_DCHECK(config_.transport_observer);
89}
Zhi Huange818b6e2018-02-22 15:26:27 -080090
91JsepTransportController::~JsepTransportController() {
92 // Channel destructors may try to send packets, so this needs to happen on
93 // the network thread.
94 network_thread_->Invoke<void>(
95 RTC_FROM_HERE,
96 rtc::Bind(&JsepTransportController::DestroyAllJsepTransports_n, this));
97}
98
99RTCError JsepTransportController::SetLocalDescription(
100 SdpType type,
101 const cricket::SessionDescription* description) {
102 if (!network_thread_->IsCurrent()) {
103 return network_thread_->Invoke<RTCError>(
104 RTC_FROM_HERE, [=] { return SetLocalDescription(type, description); });
105 }
106
107 if (!initial_offerer_.has_value()) {
108 initial_offerer_.emplace(type == SdpType::kOffer);
109 if (*initial_offerer_) {
110 SetIceRole_n(cricket::ICEROLE_CONTROLLING);
111 } else {
112 SetIceRole_n(cricket::ICEROLE_CONTROLLED);
113 }
114 }
115 return ApplyDescription_n(/*local=*/true, type, description);
116}
117
118RTCError JsepTransportController::SetRemoteDescription(
119 SdpType type,
120 const cricket::SessionDescription* description) {
121 if (!network_thread_->IsCurrent()) {
122 return network_thread_->Invoke<RTCError>(
123 RTC_FROM_HERE, [=] { return SetRemoteDescription(type, description); });
124 }
125
126 return ApplyDescription_n(/*local=*/false, type, description);
127}
128
129RtpTransportInternal* JsepTransportController::GetRtpTransport(
130 const std::string& mid) const {
Zhi Huange830e682018-03-30 10:48:35 -0700131 auto jsep_transport = GetJsepTransportForMid(mid);
Zhi Huange818b6e2018-02-22 15:26:27 -0800132 if (!jsep_transport) {
133 return nullptr;
134 }
135 return jsep_transport->rtp_transport();
136}
137
Anton Sukhanov7940da02018-10-10 10:34:49 -0700138MediaTransportInterface* JsepTransportController::GetMediaTransport(
139 const std::string& mid) const {
140 auto jsep_transport = GetJsepTransportForMid(mid);
141 if (!jsep_transport) {
142 return nullptr;
143 }
144 return jsep_transport->media_transport();
145}
146
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800147MediaTransportState JsepTransportController::GetMediaTransportState(
148 const std::string& mid) const {
149 auto jsep_transport = GetJsepTransportForMid(mid);
150 if (!jsep_transport) {
151 return MediaTransportState::kPending;
152 }
153 return jsep_transport->media_transport_state();
154}
155
Zhi Huange818b6e2018-02-22 15:26:27 -0800156cricket::DtlsTransportInternal* JsepTransportController::GetDtlsTransport(
Harald Alvestrandad88c882018-11-28 16:47:46 +0100157 const std::string& mid) {
Zhi Huange830e682018-03-30 10:48:35 -0700158 auto jsep_transport = GetJsepTransportForMid(mid);
Zhi Huange818b6e2018-02-22 15:26:27 -0800159 if (!jsep_transport) {
160 return nullptr;
161 }
162 return jsep_transport->rtp_dtls_transport();
163}
164
Harald Alvestrandad88c882018-11-28 16:47:46 +0100165const cricket::DtlsTransportInternal*
166JsepTransportController::GetRtcpDtlsTransport(const std::string& mid) const {
Zhi Huange830e682018-03-30 10:48:35 -0700167 auto jsep_transport = GetJsepTransportForMid(mid);
Zhi Huange818b6e2018-02-22 15:26:27 -0800168 if (!jsep_transport) {
169 return nullptr;
170 }
171 return jsep_transport->rtcp_dtls_transport();
172}
173
Harald Alvestrandad88c882018-11-28 16:47:46 +0100174rtc::scoped_refptr<webrtc::DtlsTransportInterface>
175JsepTransportController::LookupDtlsTransportByMid(const std::string& mid) {
176 auto jsep_transport = GetJsepTransportForMid(mid);
177 if (!jsep_transport) {
178 return nullptr;
179 }
180 return jsep_transport->RtpDtlsTransport();
181}
182
Zhi Huange818b6e2018-02-22 15:26:27 -0800183void JsepTransportController::SetIceConfig(const cricket::IceConfig& config) {
184 if (!network_thread_->IsCurrent()) {
185 network_thread_->Invoke<void>(RTC_FROM_HERE, [&] { SetIceConfig(config); });
186 return;
187 }
188
189 ice_config_ = config;
190 for (auto& dtls : GetDtlsTransports()) {
191 dtls->ice_transport()->SetIceConfig(ice_config_);
192 }
193}
194
195void JsepTransportController::SetNeedsIceRestartFlag() {
Zhi Huange830e682018-03-30 10:48:35 -0700196 for (auto& kv : jsep_transports_by_name_) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800197 kv.second->SetNeedsIceRestartFlag();
198 }
199}
200
201bool JsepTransportController::NeedsIceRestart(
202 const std::string& transport_name) const {
Zhi Huang365381f2018-04-13 16:44:34 -0700203 const cricket::JsepTransport* transport =
Zhi Huange830e682018-03-30 10:48:35 -0700204 GetJsepTransportByName(transport_name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800205 if (!transport) {
206 return false;
207 }
208 return transport->needs_ice_restart();
209}
210
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200211absl::optional<rtc::SSLRole> JsepTransportController::GetDtlsRole(
Zhi Huange830e682018-03-30 10:48:35 -0700212 const std::string& mid) const {
Zhi Huange818b6e2018-02-22 15:26:27 -0800213 if (!network_thread_->IsCurrent()) {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200214 return network_thread_->Invoke<absl::optional<rtc::SSLRole>>(
Zhi Huange830e682018-03-30 10:48:35 -0700215 RTC_FROM_HERE, [&] { return GetDtlsRole(mid); });
Zhi Huange818b6e2018-02-22 15:26:27 -0800216 }
217
Zhi Huang365381f2018-04-13 16:44:34 -0700218 const cricket::JsepTransport* t = GetJsepTransportForMid(mid);
Zhi Huange818b6e2018-02-22 15:26:27 -0800219 if (!t) {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200220 return absl::optional<rtc::SSLRole>();
Zhi Huange818b6e2018-02-22 15:26:27 -0800221 }
222 return t->GetDtlsRole();
223}
224
225bool JsepTransportController::SetLocalCertificate(
226 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
227 if (!network_thread_->IsCurrent()) {
228 return network_thread_->Invoke<bool>(
229 RTC_FROM_HERE, [&] { return SetLocalCertificate(certificate); });
230 }
231
232 // Can't change a certificate, or set a null certificate.
233 if (certificate_ || !certificate) {
234 return false;
235 }
236 certificate_ = certificate;
237
238 // Set certificate for JsepTransport, which verifies it matches the
239 // fingerprint in SDP, and DTLS transport.
240 // Fallback from DTLS to SDES is not supported.
Zhi Huange830e682018-03-30 10:48:35 -0700241 for (auto& kv : jsep_transports_by_name_) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800242 kv.second->SetLocalCertificate(certificate_);
243 }
244 for (auto& dtls : GetDtlsTransports()) {
245 bool set_cert_success = dtls->SetLocalCertificate(certificate_);
246 RTC_DCHECK(set_cert_success);
247 }
248 return true;
249}
250
251rtc::scoped_refptr<rtc::RTCCertificate>
252JsepTransportController::GetLocalCertificate(
253 const std::string& transport_name) const {
254 if (!network_thread_->IsCurrent()) {
255 return network_thread_->Invoke<rtc::scoped_refptr<rtc::RTCCertificate>>(
256 RTC_FROM_HERE, [&] { return GetLocalCertificate(transport_name); });
257 }
258
Zhi Huang365381f2018-04-13 16:44:34 -0700259 const cricket::JsepTransport* t = GetJsepTransportByName(transport_name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800260 if (!t) {
261 return nullptr;
262 }
263 return t->GetLocalCertificate();
264}
265
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800266std::unique_ptr<rtc::SSLCertChain>
267JsepTransportController::GetRemoteSSLCertChain(
Zhi Huange818b6e2018-02-22 15:26:27 -0800268 const std::string& transport_name) const {
269 if (!network_thread_->IsCurrent()) {
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800270 return network_thread_->Invoke<std::unique_ptr<rtc::SSLCertChain>>(
271 RTC_FROM_HERE, [&] { return GetRemoteSSLCertChain(transport_name); });
Zhi Huange818b6e2018-02-22 15:26:27 -0800272 }
273
Zhi Huange830e682018-03-30 10:48:35 -0700274 // Get the certificate from the RTP transport's DTLS handshake. Should be
275 // identical to the RTCP transport's, since they were given the same remote
Zhi Huange818b6e2018-02-22 15:26:27 -0800276 // fingerprint.
Zhi Huange830e682018-03-30 10:48:35 -0700277 auto jsep_transport = GetJsepTransportByName(transport_name);
278 if (!jsep_transport) {
279 return nullptr;
280 }
281 auto dtls = jsep_transport->rtp_dtls_transport();
Zhi Huange818b6e2018-02-22 15:26:27 -0800282 if (!dtls) {
283 return nullptr;
284 }
285
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800286 return dtls->GetRemoteSSLCertChain();
Zhi Huange818b6e2018-02-22 15:26:27 -0800287}
288
289void JsepTransportController::MaybeStartGathering() {
290 if (!network_thread_->IsCurrent()) {
291 network_thread_->Invoke<void>(RTC_FROM_HERE,
292 [&] { MaybeStartGathering(); });
293 return;
294 }
295
296 for (auto& dtls : GetDtlsTransports()) {
297 dtls->ice_transport()->MaybeStartGathering();
298 }
299}
300
301RTCError JsepTransportController::AddRemoteCandidates(
302 const std::string& transport_name,
303 const cricket::Candidates& candidates) {
304 if (!network_thread_->IsCurrent()) {
305 return network_thread_->Invoke<RTCError>(RTC_FROM_HERE, [&] {
306 return AddRemoteCandidates(transport_name, candidates);
307 });
308 }
309
310 // Verify each candidate before passing down to the transport layer.
311 RTCError error = VerifyCandidates(candidates);
312 if (!error.ok()) {
313 return error;
314 }
Zhi Huange830e682018-03-30 10:48:35 -0700315 auto jsep_transport = GetJsepTransportByName(transport_name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800316 if (!jsep_transport) {
Zhi Huange830e682018-03-30 10:48:35 -0700317 RTC_LOG(LS_WARNING) << "Not adding candidate because the JsepTransport "
318 "doesn't exist. Ignore it.";
319 return RTCError::OK();
Zhi Huange818b6e2018-02-22 15:26:27 -0800320 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800321 return jsep_transport->AddRemoteCandidates(candidates);
322}
323
324RTCError JsepTransportController::RemoveRemoteCandidates(
325 const cricket::Candidates& candidates) {
326 if (!network_thread_->IsCurrent()) {
327 return network_thread_->Invoke<RTCError>(
328 RTC_FROM_HERE, [&] { return RemoveRemoteCandidates(candidates); });
329 }
330
331 // Verify each candidate before passing down to the transport layer.
332 RTCError error = VerifyCandidates(candidates);
333 if (!error.ok()) {
334 return error;
335 }
336
337 std::map<std::string, cricket::Candidates> candidates_by_transport_name;
338 for (const cricket::Candidate& cand : candidates) {
339 if (!cand.transport_name().empty()) {
340 candidates_by_transport_name[cand.transport_name()].push_back(cand);
341 } else {
342 RTC_LOG(LS_ERROR) << "Not removing candidate because it does not have a "
343 "transport name set: "
344 << cand.ToString();
345 }
346 }
347
348 for (const auto& kv : candidates_by_transport_name) {
349 const std::string& transport_name = kv.first;
350 const cricket::Candidates& candidates = kv.second;
Zhi Huang365381f2018-04-13 16:44:34 -0700351 cricket::JsepTransport* jsep_transport =
Zhi Huange830e682018-03-30 10:48:35 -0700352 GetJsepTransportByName(transport_name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800353 if (!jsep_transport) {
Zhi Huange830e682018-03-30 10:48:35 -0700354 RTC_LOG(LS_WARNING)
355 << "Not removing candidate because the JsepTransport doesn't exist.";
356 continue;
Zhi Huange818b6e2018-02-22 15:26:27 -0800357 }
358 for (const cricket::Candidate& candidate : candidates) {
Harald Alvestrandad88c882018-11-28 16:47:46 +0100359 cricket::DtlsTransportInternal* dtls =
360 candidate.component() == cricket::ICE_CANDIDATE_COMPONENT_RTP
361 ? jsep_transport->rtp_dtls_transport()
362 : jsep_transport->rtcp_dtls_transport();
Zhi Huange818b6e2018-02-22 15:26:27 -0800363 if (dtls) {
364 dtls->ice_transport()->RemoveRemoteCandidate(candidate);
365 }
366 }
367 }
368 return RTCError::OK();
369}
370
371bool JsepTransportController::GetStats(const std::string& transport_name,
372 cricket::TransportStats* stats) {
373 if (!network_thread_->IsCurrent()) {
374 return network_thread_->Invoke<bool>(
375 RTC_FROM_HERE, [=] { return GetStats(transport_name, stats); });
376 }
377
Zhi Huang365381f2018-04-13 16:44:34 -0700378 cricket::JsepTransport* transport = GetJsepTransportByName(transport_name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800379 if (!transport) {
380 return false;
381 }
382 return transport->GetStats(stats);
383}
384
Zhi Huangb57e1692018-06-12 11:41:11 -0700385void JsepTransportController::SetActiveResetSrtpParams(
386 bool active_reset_srtp_params) {
387 if (!network_thread_->IsCurrent()) {
388 network_thread_->Invoke<void>(RTC_FROM_HERE, [=] {
389 SetActiveResetSrtpParams(active_reset_srtp_params);
390 });
391 return;
392 }
393
394 RTC_LOG(INFO)
395 << "Updating the active_reset_srtp_params for JsepTransportController: "
396 << active_reset_srtp_params;
397 config_.active_reset_srtp_params = active_reset_srtp_params;
398 for (auto& kv : jsep_transports_by_name_) {
399 kv.second->SetActiveResetSrtpParams(active_reset_srtp_params);
400 }
401}
402
Piotr (Peter) Slatala97fc11f2018-10-18 12:57:59 -0700403void JsepTransportController::SetMediaTransportFactory(
404 MediaTransportFactory* media_transport_factory) {
405 RTC_DCHECK(media_transport_factory == config_.media_transport_factory ||
406 jsep_transports_by_name_.empty())
407 << "You can only call SetMediaTransportFactory before "
408 "JsepTransportController created its first transport.";
409 config_.media_transport_factory = media_transport_factory;
410}
411
Zhi Huange818b6e2018-02-22 15:26:27 -0800412std::unique_ptr<cricket::DtlsTransportInternal>
413JsepTransportController::CreateDtlsTransport(const std::string& transport_name,
414 bool rtcp) {
415 RTC_DCHECK(network_thread_->IsCurrent());
416 int component = rtcp ? cricket::ICE_CANDIDATE_COMPONENT_RTCP
417 : cricket::ICE_CANDIDATE_COMPONENT_RTP;
418
419 std::unique_ptr<cricket::DtlsTransportInternal> dtls;
420 if (config_.external_transport_factory) {
421 auto ice = config_.external_transport_factory->CreateIceTransport(
422 transport_name, component);
423 dtls = config_.external_transport_factory->CreateDtlsTransport(
424 std::move(ice), config_.crypto_options);
425 } else {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200426 auto ice = absl::make_unique<cricket::P2PTransportChannel>(
Zach Steine20867f2018-08-02 13:20:15 -0700427 transport_name, component, port_allocator_, async_resolver_factory_,
428 config_.event_log);
Zach Steinc64078f2018-11-27 15:53:01 -0800429 dtls = absl::make_unique<cricket::DtlsTransport>(
430 std::move(ice), config_.crypto_options, config_.event_log);
Zhi Huange818b6e2018-02-22 15:26:27 -0800431 }
432
433 RTC_DCHECK(dtls);
434 dtls->SetSslMaxProtocolVersion(config_.ssl_max_version);
Zhi Huange818b6e2018-02-22 15:26:27 -0800435 dtls->ice_transport()->SetIceRole(ice_role_);
436 dtls->ice_transport()->SetIceTiebreaker(ice_tiebreaker_);
437 dtls->ice_transport()->SetIceConfig(ice_config_);
438 if (certificate_) {
439 bool set_cert_success = dtls->SetLocalCertificate(certificate_);
440 RTC_DCHECK(set_cert_success);
441 }
442
443 // Connect to signals offered by the DTLS and ICE transport.
444 dtls->SignalWritableState.connect(
445 this, &JsepTransportController::OnTransportWritableState_n);
446 dtls->SignalReceivingState.connect(
447 this, &JsepTransportController::OnTransportReceivingState_n);
448 dtls->SignalDtlsHandshakeError.connect(
449 this, &JsepTransportController::OnDtlsHandshakeError);
450 dtls->ice_transport()->SignalGatheringState.connect(
451 this, &JsepTransportController::OnTransportGatheringState_n);
452 dtls->ice_transport()->SignalCandidateGathered.connect(
453 this, &JsepTransportController::OnTransportCandidateGathered_n);
454 dtls->ice_transport()->SignalCandidatesRemoved.connect(
455 this, &JsepTransportController::OnTransportCandidatesRemoved_n);
456 dtls->ice_transport()->SignalRoleConflict.connect(
457 this, &JsepTransportController::OnTransportRoleConflict_n);
458 dtls->ice_transport()->SignalStateChanged.connect(
459 this, &JsepTransportController::OnTransportStateChanged_n);
460 return dtls;
461}
462
463std::unique_ptr<webrtc::RtpTransport>
464JsepTransportController::CreateUnencryptedRtpTransport(
465 const std::string& transport_name,
466 rtc::PacketTransportInternal* rtp_packet_transport,
467 rtc::PacketTransportInternal* rtcp_packet_transport) {
468 RTC_DCHECK(network_thread_->IsCurrent());
Zhi Huange830e682018-03-30 10:48:35 -0700469 auto unencrypted_rtp_transport =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200470 absl::make_unique<RtpTransport>(rtcp_packet_transport == nullptr);
Zhi Huange830e682018-03-30 10:48:35 -0700471 unencrypted_rtp_transport->SetRtpPacketTransport(rtp_packet_transport);
472 if (rtcp_packet_transport) {
473 unencrypted_rtp_transport->SetRtcpPacketTransport(rtcp_packet_transport);
474 }
475 return unencrypted_rtp_transport;
Zhi Huange818b6e2018-02-22 15:26:27 -0800476}
477
478std::unique_ptr<webrtc::SrtpTransport>
479JsepTransportController::CreateSdesTransport(
480 const std::string& transport_name,
Zhi Huange830e682018-03-30 10:48:35 -0700481 cricket::DtlsTransportInternal* rtp_dtls_transport,
482 cricket::DtlsTransportInternal* rtcp_dtls_transport) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800483 RTC_DCHECK(network_thread_->IsCurrent());
Zhi Huange818b6e2018-02-22 15:26:27 -0800484 auto srtp_transport =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200485 absl::make_unique<webrtc::SrtpTransport>(rtcp_dtls_transport == nullptr);
Zhi Huange830e682018-03-30 10:48:35 -0700486 RTC_DCHECK(rtp_dtls_transport);
487 srtp_transport->SetRtpPacketTransport(rtp_dtls_transport);
488 if (rtcp_dtls_transport) {
489 srtp_transport->SetRtcpPacketTransport(rtcp_dtls_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800490 }
491 if (config_.enable_external_auth) {
492 srtp_transport->EnableExternalAuth();
493 }
494 return srtp_transport;
495}
496
497std::unique_ptr<webrtc::DtlsSrtpTransport>
498JsepTransportController::CreateDtlsSrtpTransport(
499 const std::string& transport_name,
500 cricket::DtlsTransportInternal* rtp_dtls_transport,
501 cricket::DtlsTransportInternal* rtcp_dtls_transport) {
502 RTC_DCHECK(network_thread_->IsCurrent());
Karl Wiberg918f50c2018-07-05 11:40:33 +0200503 auto dtls_srtp_transport = absl::make_unique<webrtc::DtlsSrtpTransport>(
Zhi Huang365381f2018-04-13 16:44:34 -0700504 rtcp_dtls_transport == nullptr);
Zhi Huang27f3bf52018-03-26 21:37:23 -0700505 if (config_.enable_external_auth) {
Zhi Huang365381f2018-04-13 16:44:34 -0700506 dtls_srtp_transport->EnableExternalAuth();
Zhi Huang27f3bf52018-03-26 21:37:23 -0700507 }
Zhi Huang97d5e5b2018-03-27 00:09:01 +0000508
Zhi Huange818b6e2018-02-22 15:26:27 -0800509 dtls_srtp_transport->SetDtlsTransports(rtp_dtls_transport,
510 rtcp_dtls_transport);
Zhi Huangb57e1692018-06-12 11:41:11 -0700511 dtls_srtp_transport->SetActiveResetSrtpParams(
512 config_.active_reset_srtp_params);
Jonas Olsson635474e2018-10-18 15:58:17 +0200513 dtls_srtp_transport->SignalDtlsStateChange.connect(
514 this, &JsepTransportController::UpdateAggregateStates_n);
Zhi Huange818b6e2018-02-22 15:26:27 -0800515 return dtls_srtp_transport;
516}
517
518std::vector<cricket::DtlsTransportInternal*>
519JsepTransportController::GetDtlsTransports() {
520 std::vector<cricket::DtlsTransportInternal*> dtls_transports;
Zhi Huange830e682018-03-30 10:48:35 -0700521 for (auto it = jsep_transports_by_name_.begin();
522 it != jsep_transports_by_name_.end(); ++it) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800523 auto jsep_transport = it->second.get();
524 RTC_DCHECK(jsep_transport);
525 if (jsep_transport->rtp_dtls_transport()) {
526 dtls_transports.push_back(jsep_transport->rtp_dtls_transport());
527 }
528
529 if (jsep_transport->rtcp_dtls_transport()) {
530 dtls_transports.push_back(jsep_transport->rtcp_dtls_transport());
531 }
532 }
533 return dtls_transports;
534}
535
Zhi Huange818b6e2018-02-22 15:26:27 -0800536RTCError JsepTransportController::ApplyDescription_n(
537 bool local,
538 SdpType type,
539 const cricket::SessionDescription* description) {
540 RTC_DCHECK(network_thread_->IsCurrent());
541 RTC_DCHECK(description);
542
543 if (local) {
544 local_desc_ = description;
545 } else {
546 remote_desc_ = description;
547 }
548
Zhi Huange830e682018-03-30 10:48:35 -0700549 RTCError error;
Zhi Huangd2248f82018-04-10 14:41:03 -0700550 error = ValidateAndMaybeUpdateBundleGroup(local, type, description);
Zhi Huange830e682018-03-30 10:48:35 -0700551 if (!error.ok()) {
552 return error;
Zhi Huange818b6e2018-02-22 15:26:27 -0800553 }
554
555 std::vector<int> merged_encrypted_extension_ids;
556 if (bundle_group_) {
557 merged_encrypted_extension_ids =
558 MergeEncryptedHeaderExtensionIdsForBundle(description);
559 }
560
561 for (const cricket::ContentInfo& content_info : description->contents()) {
562 // Don't create transports for rejected m-lines and bundled m-lines."
563 if (content_info.rejected ||
564 (IsBundled(content_info.name) && content_info.name != *bundled_mid())) {
565 continue;
566 }
Anton Sukhanov7940da02018-10-10 10:34:49 -0700567 error = MaybeCreateJsepTransport(local, content_info);
Zhi Huange830e682018-03-30 10:48:35 -0700568 if (!error.ok()) {
569 return error;
570 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800571 }
572
573 RTC_DCHECK(description->contents().size() ==
574 description->transport_infos().size());
575 for (size_t i = 0; i < description->contents().size(); ++i) {
576 const cricket::ContentInfo& content_info = description->contents()[i];
577 const cricket::TransportInfo& transport_info =
578 description->transport_infos()[i];
579 if (content_info.rejected) {
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700580 HandleRejectedContent(content_info, description);
Zhi Huange818b6e2018-02-22 15:26:27 -0800581 continue;
582 }
583
584 if (IsBundled(content_info.name) && content_info.name != *bundled_mid()) {
Zhi Huang365381f2018-04-13 16:44:34 -0700585 if (!HandleBundledContent(content_info)) {
586 return RTCError(RTCErrorType::INVALID_PARAMETER,
587 "Failed to process the bundled m= section.");
588 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800589 continue;
590 }
591
Zhi Huange830e682018-03-30 10:48:35 -0700592 error = ValidateContent(content_info);
593 if (!error.ok()) {
594 return error;
595 }
596
Zhi Huange818b6e2018-02-22 15:26:27 -0800597 std::vector<int> extension_ids;
Taylor Brandstetter0ab56512018-04-12 10:30:48 -0700598 if (bundled_mid() && content_info.name == *bundled_mid()) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800599 extension_ids = merged_encrypted_extension_ids;
600 } else {
601 extension_ids = GetEncryptedHeaderExtensionIds(content_info);
602 }
603
Zhi Huange830e682018-03-30 10:48:35 -0700604 int rtp_abs_sendtime_extn_id =
605 GetRtpAbsSendTimeHeaderExtensionId(content_info);
606
Zhi Huang365381f2018-04-13 16:44:34 -0700607 cricket::JsepTransport* transport =
Zhi Huange830e682018-03-30 10:48:35 -0700608 GetJsepTransportForMid(content_info.name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800609 RTC_DCHECK(transport);
610
611 SetIceRole_n(DetermineIceRole(transport, transport_info, type, local));
612
Zhi Huange818b6e2018-02-22 15:26:27 -0800613 cricket::JsepTransportDescription jsep_description =
614 CreateJsepTransportDescription(content_info, transport_info,
Zhi Huange830e682018-03-30 10:48:35 -0700615 extension_ids, rtp_abs_sendtime_extn_id);
Zhi Huange818b6e2018-02-22 15:26:27 -0800616 if (local) {
617 error =
618 transport->SetLocalJsepTransportDescription(jsep_description, type);
619 } else {
620 error =
621 transport->SetRemoteJsepTransportDescription(jsep_description, type);
622 }
623
624 if (!error.ok()) {
625 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
626 "Failed to apply the description for " +
627 content_info.name + ": " + error.message());
628 }
629 }
630 return RTCError::OK();
631}
632
Zhi Huangd2248f82018-04-10 14:41:03 -0700633RTCError JsepTransportController::ValidateAndMaybeUpdateBundleGroup(
634 bool local,
635 SdpType type,
Zhi Huange830e682018-03-30 10:48:35 -0700636 const cricket::SessionDescription* description) {
637 RTC_DCHECK(description);
Zhi Huangd2248f82018-04-10 14:41:03 -0700638 const cricket::ContentGroup* new_bundle_group =
639 description->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
640
641 // The BUNDLE group containing a MID that no m= section has is invalid.
642 if (new_bundle_group) {
643 for (auto content_name : new_bundle_group->content_names()) {
644 if (!description->GetContentByName(content_name)) {
645 return RTCError(RTCErrorType::INVALID_PARAMETER,
646 "The BUNDLE group contains MID:" + content_name +
647 " matching no m= section.");
648 }
649 }
650 }
651
652 if (type == SdpType::kAnswer) {
653 const cricket::ContentGroup* offered_bundle_group =
654 local ? remote_desc_->GetGroupByName(cricket::GROUP_TYPE_BUNDLE)
655 : local_desc_->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
656
657 if (new_bundle_group) {
658 // The BUNDLE group in answer should be a subset of offered group.
659 for (auto content_name : new_bundle_group->content_names()) {
660 if (!offered_bundle_group ||
661 !offered_bundle_group->HasContentName(content_name)) {
662 return RTCError(RTCErrorType::INVALID_PARAMETER,
663 "The BUNDLE group in answer contains a MID that was "
664 "not in the offered group.");
665 }
666 }
667 }
668
669 if (bundle_group_) {
670 for (auto content_name : bundle_group_->content_names()) {
671 // An answer that removes m= sections from pre-negotiated BUNDLE group
672 // without rejecting it, is invalid.
673 if (!new_bundle_group ||
674 !new_bundle_group->HasContentName(content_name)) {
675 auto* content_info = description->GetContentByName(content_name);
676 if (!content_info || !content_info->rejected) {
677 return RTCError(RTCErrorType::INVALID_PARAMETER,
678 "Answer cannot remove m= section " + content_name +
679 " from already-established BUNDLE group.");
680 }
681 }
682 }
683 }
684 }
685
686 if (config_.bundle_policy ==
687 PeerConnectionInterface::kBundlePolicyMaxBundle &&
688 !description->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
689 return RTCError(RTCErrorType::INVALID_PARAMETER,
690 "max-bundle is used but no bundle group found.");
691 }
692
693 if (ShouldUpdateBundleGroup(type, description)) {
Zhi Huangd2248f82018-04-10 14:41:03 -0700694 bundle_group_ = *new_bundle_group;
695 }
Zhi Huange830e682018-03-30 10:48:35 -0700696
697 if (!bundled_mid()) {
698 return RTCError::OK();
699 }
700
701 auto bundled_content = description->GetContentByName(*bundled_mid());
702 if (!bundled_content) {
703 return RTCError(
704 RTCErrorType::INVALID_PARAMETER,
705 "An m= section associated with the BUNDLE-tag doesn't exist.");
706 }
707
708 // If the |bundled_content| is rejected, other contents in the bundle group
709 // should be rejected.
710 if (bundled_content->rejected) {
711 for (auto content_name : bundle_group_->content_names()) {
712 auto other_content = description->GetContentByName(content_name);
713 if (!other_content->rejected) {
714 return RTCError(
715 RTCErrorType::INVALID_PARAMETER,
716 "The m= section:" + content_name + " should be rejected.");
717 }
718 }
719 }
720
721 return RTCError::OK();
722}
723
724RTCError JsepTransportController::ValidateContent(
725 const cricket::ContentInfo& content_info) {
726 if (config_.rtcp_mux_policy ==
727 PeerConnectionInterface::kRtcpMuxPolicyRequire &&
728 content_info.type == cricket::MediaProtocolType::kRtp &&
729 !content_info.media_description()->rtcp_mux()) {
730 return RTCError(RTCErrorType::INVALID_PARAMETER,
731 "The m= section:" + content_info.name +
732 " is invalid. RTCP-MUX is not "
733 "enabled when it is required.");
734 }
735 return RTCError::OK();
736}
737
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700738void JsepTransportController::HandleRejectedContent(
Zhi Huangd2248f82018-04-10 14:41:03 -0700739 const cricket::ContentInfo& content_info,
740 const cricket::SessionDescription* description) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800741 // If the content is rejected, let the
742 // BaseChannel/SctpTransport change the RtpTransport/DtlsTransport first,
Zhi Huang365381f2018-04-13 16:44:34 -0700743 // then destroy the cricket::JsepTransport.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700744 RemoveTransportForMid(content_info.name);
Zhi Huange830e682018-03-30 10:48:35 -0700745 if (content_info.name == bundled_mid()) {
746 for (auto content_name : bundle_group_->content_names()) {
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700747 RemoveTransportForMid(content_name);
Zhi Huange830e682018-03-30 10:48:35 -0700748 }
749 bundle_group_.reset();
750 } else if (IsBundled(content_info.name)) {
751 // Remove the rejected content from the |bundle_group_|.
Zhi Huange818b6e2018-02-22 15:26:27 -0800752 bundle_group_->RemoveContentName(content_info.name);
Zhi Huange830e682018-03-30 10:48:35 -0700753 // Reset the bundle group if nothing left.
754 if (!bundle_group_->FirstContentName()) {
755 bundle_group_.reset();
756 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800757 }
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700758 MaybeDestroyJsepTransport(content_info.name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800759}
760
Zhi Huang365381f2018-04-13 16:44:34 -0700761bool JsepTransportController::HandleBundledContent(
Zhi Huange818b6e2018-02-22 15:26:27 -0800762 const cricket::ContentInfo& content_info) {
Zhi Huangd2248f82018-04-10 14:41:03 -0700763 auto jsep_transport = GetJsepTransportByName(*bundled_mid());
764 RTC_DCHECK(jsep_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800765 // If the content is bundled, let the
766 // BaseChannel/SctpTransport change the RtpTransport/DtlsTransport first,
Zhi Huang365381f2018-04-13 16:44:34 -0700767 // then destroy the cricket::JsepTransport.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700768 if (SetTransportForMid(content_info.name, jsep_transport)) {
Piotr (Peter) Slatala10aeb2a2018-11-14 10:57:24 -0800769 // TODO(bugs.webrtc.org/9719) For media transport this is far from ideal,
770 // because it means that we first create media transport and start
771 // connecting it, and then we destroy it. We will need to address it before
772 // video path is enabled.
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(),
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800790 jsep_transport->rtp_dtls_transport(), jsep_transport->media_transport());
Zhi Huangd2248f82018-04-10 14:41:03 -0700791}
792
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700793void JsepTransportController::RemoveTransportForMid(const std::string& mid) {
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800794 bool ret = config_.transport_observer->OnTransportChanged(mid, nullptr,
795 nullptr, nullptr);
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700796 // 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
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700847 if (!config_.crypto_options.srtp.enable_encrypted_rtp_header_extensions) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800848 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
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -0800929std::unique_ptr<webrtc::MediaTransportInterface>
930JsepTransportController::MaybeCreateMediaTransport(
931 const cricket::ContentInfo& content_info,
Anton Sukhanov7940da02018-10-10 10:34:49 -0700932 bool local,
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -0800933 cricket::IceTransportInternal* ice_transport) {
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -0700934 absl::optional<cricket::CryptoParams> selected_crypto_for_media_transport;
935 if (content_info.media_description() &&
936 !content_info.media_description()->cryptos().empty()) {
937 // Order of cryptos is deterministic (rfc4568, 5.1.1), so we just select the
938 // first one (in fact the first one should be the most preferred one.) We
939 // ignore the HMAC size, as media transport crypto settings currently don't
940 // expose HMAC size, nor crypto protocol for that matter.
941 selected_crypto_for_media_transport =
942 content_info.media_description()->cryptos()[0];
943 }
944
Anton Sukhanov7940da02018-10-10 10:34:49 -0700945 if (config_.media_transport_factory != nullptr) {
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -0700946 if (!selected_crypto_for_media_transport.has_value()) {
947 RTC_LOG(LS_WARNING) << "a=cryto line was not found in the offer. Most "
948 "likely you did not enable SDES. "
949 "Make sure to pass config.enable_dtls_srtp=false "
950 "to RTCConfiguration. "
951 "Cannot continue with media transport. Falling "
952 "back to RTP. is_local="
953 << local;
Anton Sukhanov7940da02018-10-10 10:34:49 -0700954
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -0700955 // Remove media_transport_factory from config, because we don't want to
956 // use it on the subsequent call (for the other side of the offer).
957 config_.media_transport_factory = nullptr;
958 } else {
959 // Note that we ignore here lifetime and length.
960 // In fact we take those bits (inline, lifetime and length) and keep it as
961 // part of key derivation.
962 //
963 // Technically, we are also not following rfc4568, which requires us to
964 // send and answer with the key that we chose. In practice, for media
965 // transport, the current approach should be sufficient (we take the key
966 // that sender offered, and caller assumes we will use it. We are not
967 // signaling back that we indeed used it.)
968 std::unique_ptr<rtc::KeyDerivation> key_derivation =
969 rtc::KeyDerivation::Create(rtc::KeyDerivationAlgorithm::HKDF_SHA256);
970 const std::string label = "MediaTransportLabel";
971 constexpr int kDerivedKeyByteSize = 32;
Anton Sukhanov7940da02018-10-10 10:34:49 -0700972
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -0700973 int key_len, salt_len;
974 if (!rtc::GetSrtpKeyAndSaltLengths(
975 rtc::SrtpCryptoSuiteFromName(
976 selected_crypto_for_media_transport.value().cipher_suite),
977 &key_len, &salt_len)) {
978 RTC_CHECK(false) << "Cannot set up secure media transport";
979 }
980 rtc::ZeroOnFreeBuffer<uint8_t> raw_key(key_len + salt_len);
981
982 cricket::SrtpFilter::ParseKeyParams(
983 selected_crypto_for_media_transport.value().key_params,
984 raw_key.data(), raw_key.size());
985 absl::optional<rtc::ZeroOnFreeBuffer<uint8_t>> key =
986 key_derivation->DeriveKey(
987 raw_key,
988 /*salt=*/nullptr,
989 rtc::ArrayView<const uint8_t>(
990 reinterpret_cast<const uint8_t*>(label.data()), label.size()),
991 kDerivedKeyByteSize);
992
993 // We want to crash the app if we don't have a key, and not silently fall
994 // back to the unsecure communication.
995 RTC_CHECK(key.has_value());
996 MediaTransportSettings settings;
997 settings.is_caller = local;
998 settings.pre_shared_key =
999 std::string(reinterpret_cast<const char*>(key.value().data()),
1000 key.value().size());
Piotr (Peter) Slatala0c022502018-12-28 10:39:39 -08001001 settings.event_log = config_.event_log;
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -07001002 auto media_transport_result =
1003 config_.media_transport_factory->CreateMediaTransport(
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001004 ice_transport, network_thread_, settings);
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -07001005
1006 // TODO(sukhanov): Proper error handling.
1007 RTC_CHECK(media_transport_result.ok());
1008
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001009 return media_transport_result.MoveValue();
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -07001010 }
Anton Sukhanov7940da02018-10-10 10:34:49 -07001011 }
1012
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001013 return nullptr;
1014}
1015
1016RTCError JsepTransportController::MaybeCreateJsepTransport(
1017 bool local,
1018 const cricket::ContentInfo& content_info) {
1019 RTC_DCHECK(network_thread_->IsCurrent());
1020 cricket::JsepTransport* transport = GetJsepTransportByName(content_info.name);
1021 if (transport) {
1022 return RTCError::OK();
1023 }
1024
1025 const cricket::MediaContentDescription* content_desc =
1026 static_cast<const cricket::MediaContentDescription*>(
1027 content_info.description);
1028 if (certificate_ && !content_desc->cryptos().empty()) {
1029 return RTCError(RTCErrorType::INVALID_PARAMETER,
1030 "SDES and DTLS-SRTP cannot be enabled at the same time.");
1031 }
1032
1033 std::unique_ptr<cricket::DtlsTransportInternal> rtp_dtls_transport =
1034 CreateDtlsTransport(content_info.name, /*rtcp =*/false);
1035
1036 std::unique_ptr<cricket::DtlsTransportInternal> rtcp_dtls_transport;
1037 std::unique_ptr<RtpTransport> unencrypted_rtp_transport;
1038 std::unique_ptr<SrtpTransport> sdes_transport;
1039 std::unique_ptr<DtlsSrtpTransport> dtls_srtp_transport;
1040 std::unique_ptr<MediaTransportInterface> media_transport;
1041
1042 if (config_.rtcp_mux_policy !=
1043 PeerConnectionInterface::kRtcpMuxPolicyRequire &&
1044 content_info.type == cricket::MediaProtocolType::kRtp) {
1045 rtcp_dtls_transport =
1046 CreateDtlsTransport(content_info.name, /*rtcp =*/true);
1047 }
1048 media_transport = MaybeCreateMediaTransport(
1049 content_info, local, rtp_dtls_transport->ice_transport());
1050
Anton Sukhanov7940da02018-10-10 10:34:49 -07001051 // TODO(sukhanov): Do not create RTP/RTCP transports if media transport is
1052 // used.
Zhi Huange818b6e2018-02-22 15:26:27 -08001053 if (config_.disable_encryption) {
1054 unencrypted_rtp_transport = CreateUnencryptedRtpTransport(
Zhi Huangd2248f82018-04-10 14:41:03 -07001055 content_info.name, rtp_dtls_transport.get(), rtcp_dtls_transport.get());
Zhi Huange818b6e2018-02-22 15:26:27 -08001056 } else if (!content_desc->cryptos().empty()) {
Zhi Huangd2248f82018-04-10 14:41:03 -07001057 sdes_transport = CreateSdesTransport(
1058 content_info.name, rtp_dtls_transport.get(), rtcp_dtls_transport.get());
Zhi Huange818b6e2018-02-22 15:26:27 -08001059 } else {
Zhi Huangd2248f82018-04-10 14:41:03 -07001060 dtls_srtp_transport = CreateDtlsSrtpTransport(
1061 content_info.name, rtp_dtls_transport.get(), rtcp_dtls_transport.get());
Zhi Huange818b6e2018-02-22 15:26:27 -08001062 }
1063
Zhi Huang365381f2018-04-13 16:44:34 -07001064 std::unique_ptr<cricket::JsepTransport> jsep_transport =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001065 absl::make_unique<cricket::JsepTransport>(
Zhi Huangd2248f82018-04-10 14:41:03 -07001066 content_info.name, certificate_, std::move(unencrypted_rtp_transport),
Zhi Huange818b6e2018-02-22 15:26:27 -08001067 std::move(sdes_transport), std::move(dtls_srtp_transport),
Anton Sukhanov7940da02018-10-10 10:34:49 -07001068 std::move(rtp_dtls_transport), std::move(rtcp_dtls_transport),
1069 std::move(media_transport));
Zhi Huange818b6e2018-02-22 15:26:27 -08001070 jsep_transport->SignalRtcpMuxActive.connect(
1071 this, &JsepTransportController::UpdateAggregateStates_n);
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -07001072 jsep_transport->SignalMediaTransportStateChanged.connect(
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001073 this, &JsepTransportController::OnMediaTransportStateChanged_n);
Taylor Brandstettercbaa2542018-04-16 16:42:14 -07001074 SetTransportForMid(content_info.name, jsep_transport.get());
Zhi Huange830e682018-03-30 10:48:35 -07001075
Zhi Huangd2248f82018-04-10 14:41:03 -07001076 jsep_transports_by_name_[content_info.name] = std::move(jsep_transport);
1077 UpdateAggregateStates_n();
Zhi Huange830e682018-03-30 10:48:35 -07001078 return RTCError::OK();
Zhi Huange818b6e2018-02-22 15:26:27 -08001079}
1080
1081void JsepTransportController::MaybeDestroyJsepTransport(
1082 const std::string& mid) {
Zhi Huangd2248f82018-04-10 14:41:03 -07001083 auto jsep_transport = GetJsepTransportByName(mid);
1084 if (!jsep_transport) {
1085 return;
1086 }
1087
1088 // Don't destroy the JsepTransport if there are still media sections referring
1089 // to it.
1090 for (const auto& kv : mid_to_transport_) {
1091 if (kv.second == jsep_transport) {
1092 return;
1093 }
1094 }
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -08001095
Zhi Huange830e682018-03-30 10:48:35 -07001096 jsep_transports_by_name_.erase(mid);
Zhi Huange818b6e2018-02-22 15:26:27 -08001097 UpdateAggregateStates_n();
1098}
1099
1100void JsepTransportController::DestroyAllJsepTransports_n() {
1101 RTC_DCHECK(network_thread_->IsCurrent());
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -08001102
1103 for (const auto& jsep_transport : jsep_transports_by_name_) {
1104 config_.transport_observer->OnTransportChanged(jsep_transport.first,
1105 nullptr, nullptr, nullptr);
1106 }
1107
Zhi Huange830e682018-03-30 10:48:35 -07001108 jsep_transports_by_name_.clear();
Zhi Huange818b6e2018-02-22 15:26:27 -08001109}
1110
1111void JsepTransportController::SetIceRole_n(cricket::IceRole ice_role) {
1112 RTC_DCHECK(network_thread_->IsCurrent());
1113
1114 ice_role_ = ice_role;
1115 for (auto& dtls : GetDtlsTransports()) {
1116 dtls->ice_transport()->SetIceRole(ice_role_);
1117 }
1118}
1119
1120cricket::IceRole JsepTransportController::DetermineIceRole(
Zhi Huang365381f2018-04-13 16:44:34 -07001121 cricket::JsepTransport* jsep_transport,
Zhi Huange818b6e2018-02-22 15:26:27 -08001122 const cricket::TransportInfo& transport_info,
1123 SdpType type,
1124 bool local) {
1125 cricket::IceRole ice_role = ice_role_;
1126 auto tdesc = transport_info.description;
1127 if (local) {
1128 // The initial offer side may use ICE Lite, in which case, per RFC5245
1129 // Section 5.1.1, the answer side should take the controlling role if it is
1130 // in the full ICE mode.
1131 //
1132 // When both sides use ICE Lite, the initial offer side must take the
1133 // controlling role, and this is the default logic implemented in
1134 // SetLocalDescription in JsepTransportController.
1135 if (jsep_transport->remote_description() &&
1136 jsep_transport->remote_description()->transport_desc.ice_mode ==
1137 cricket::ICEMODE_LITE &&
1138 ice_role_ == cricket::ICEROLE_CONTROLLED &&
1139 tdesc.ice_mode == cricket::ICEMODE_FULL) {
1140 ice_role = cricket::ICEROLE_CONTROLLING;
1141 }
1142
1143 // Older versions of Chrome expect the ICE role to be re-determined when an
1144 // ICE restart occurs, and also don't perform conflict resolution correctly,
1145 // so for now we can't safely stop doing this, unless the application opts
1146 // in by setting |config_.redetermine_role_on_ice_restart_| to false. See:
1147 // https://bugs.chromium.org/p/chromium/issues/detail?id=628676
1148 // TODO(deadbeef): Remove this when these old versions of Chrome reach a low
1149 // enough population.
1150 if (config_.redetermine_role_on_ice_restart &&
1151 jsep_transport->local_description() &&
1152 cricket::IceCredentialsChanged(
1153 jsep_transport->local_description()->transport_desc.ice_ufrag,
1154 jsep_transport->local_description()->transport_desc.ice_pwd,
1155 tdesc.ice_ufrag, tdesc.ice_pwd) &&
1156 // Don't change the ICE role if the remote endpoint is ICE lite; we
1157 // should always be controlling in that case.
1158 (!jsep_transport->remote_description() ||
1159 jsep_transport->remote_description()->transport_desc.ice_mode !=
1160 cricket::ICEMODE_LITE)) {
1161 ice_role = (type == SdpType::kOffer) ? cricket::ICEROLE_CONTROLLING
1162 : cricket::ICEROLE_CONTROLLED;
1163 }
1164 } else {
1165 // If our role is cricket::ICEROLE_CONTROLLED and the remote endpoint
1166 // supports only ice_lite, this local endpoint should take the CONTROLLING
1167 // role.
1168 // TODO(deadbeef): This is a session-level attribute, so it really shouldn't
1169 // be in a TransportDescription in the first place...
1170 if (ice_role_ == cricket::ICEROLE_CONTROLLED &&
1171 tdesc.ice_mode == cricket::ICEMODE_LITE) {
1172 ice_role = cricket::ICEROLE_CONTROLLING;
1173 }
1174
1175 // If we use ICE Lite and the remote endpoint uses the full implementation
1176 // of ICE, the local endpoint must take the controlled role, and the other
1177 // side must be the controlling role.
1178 if (jsep_transport->local_description() &&
1179 jsep_transport->local_description()->transport_desc.ice_mode ==
1180 cricket::ICEMODE_LITE &&
1181 ice_role_ == cricket::ICEROLE_CONTROLLING &&
Zhi Huange830e682018-03-30 10:48:35 -07001182 tdesc.ice_mode == cricket::ICEMODE_FULL) {
Zhi Huange818b6e2018-02-22 15:26:27 -08001183 ice_role = cricket::ICEROLE_CONTROLLED;
1184 }
1185 }
1186
1187 return ice_role;
1188}
1189
1190void JsepTransportController::OnTransportWritableState_n(
1191 rtc::PacketTransportInternal* transport) {
1192 RTC_DCHECK(network_thread_->IsCurrent());
1193 RTC_LOG(LS_INFO) << " Transport " << transport->transport_name()
1194 << " writability changed to " << transport->writable()
1195 << ".";
1196 UpdateAggregateStates_n();
1197}
1198
1199void JsepTransportController::OnTransportReceivingState_n(
1200 rtc::PacketTransportInternal* transport) {
1201 RTC_DCHECK(network_thread_->IsCurrent());
1202 UpdateAggregateStates_n();
1203}
1204
1205void JsepTransportController::OnTransportGatheringState_n(
1206 cricket::IceTransportInternal* transport) {
1207 RTC_DCHECK(network_thread_->IsCurrent());
1208 UpdateAggregateStates_n();
1209}
1210
1211void JsepTransportController::OnTransportCandidateGathered_n(
1212 cricket::IceTransportInternal* transport,
1213 const cricket::Candidate& candidate) {
1214 RTC_DCHECK(network_thread_->IsCurrent());
1215
1216 // We should never signal peer-reflexive candidates.
1217 if (candidate.type() == cricket::PRFLX_PORT_TYPE) {
1218 RTC_NOTREACHED();
1219 return;
1220 }
Steve Antond25828a2018-08-31 13:06:05 -07001221 std::string transport_name = transport->transport_name();
1222 invoker_.AsyncInvoke<void>(
1223 RTC_FROM_HERE, signaling_thread_, [this, transport_name, candidate] {
1224 SignalIceCandidatesGathered(transport_name, {candidate});
1225 });
Zhi Huange818b6e2018-02-22 15:26:27 -08001226}
1227
1228void JsepTransportController::OnTransportCandidatesRemoved_n(
1229 cricket::IceTransportInternal* transport,
1230 const cricket::Candidates& candidates) {
1231 invoker_.AsyncInvoke<void>(
1232 RTC_FROM_HERE, signaling_thread_,
Steve Antond25828a2018-08-31 13:06:05 -07001233 [this, candidates] { SignalIceCandidatesRemoved(candidates); });
Zhi Huange818b6e2018-02-22 15:26:27 -08001234}
1235
1236void JsepTransportController::OnTransportRoleConflict_n(
1237 cricket::IceTransportInternal* transport) {
1238 RTC_DCHECK(network_thread_->IsCurrent());
1239 // Note: since the role conflict is handled entirely on the network thread,
1240 // we don't need to worry about role conflicts occurring on two ports at
1241 // once. The first one encountered should immediately reverse the role.
1242 cricket::IceRole reversed_role = (ice_role_ == cricket::ICEROLE_CONTROLLING)
1243 ? cricket::ICEROLE_CONTROLLED
1244 : cricket::ICEROLE_CONTROLLING;
1245 RTC_LOG(LS_INFO) << "Got role conflict; switching to "
1246 << (reversed_role == cricket::ICEROLE_CONTROLLING
1247 ? "controlling"
1248 : "controlled")
1249 << " role.";
1250 SetIceRole_n(reversed_role);
1251}
1252
1253void JsepTransportController::OnTransportStateChanged_n(
1254 cricket::IceTransportInternal* transport) {
1255 RTC_DCHECK(network_thread_->IsCurrent());
1256 RTC_LOG(LS_INFO) << transport->transport_name() << " Transport "
1257 << transport->component()
1258 << " state changed. Check if state is complete.";
1259 UpdateAggregateStates_n();
1260}
1261
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001262void JsepTransportController::OnMediaTransportStateChanged_n() {
1263 SignalMediaTransportStateChanged();
1264 UpdateAggregateStates_n();
1265}
1266
Zhi Huange818b6e2018-02-22 15:26:27 -08001267void JsepTransportController::UpdateAggregateStates_n() {
1268 RTC_DCHECK(network_thread_->IsCurrent());
1269
1270 auto dtls_transports = GetDtlsTransports();
Alex Loiko9289eda2018-11-23 16:18:59 +00001271 cricket::IceConnectionState new_connection_state =
1272 cricket::kIceConnectionConnecting;
Jonas Olsson635474e2018-10-18 15:58:17 +02001273 PeerConnectionInterface::IceConnectionState new_ice_connection_state =
1274 PeerConnectionInterface::IceConnectionState::kIceConnectionNew;
1275 PeerConnectionInterface::PeerConnectionState new_combined_state =
1276 PeerConnectionInterface::PeerConnectionState::kNew;
Zhi Huange818b6e2018-02-22 15:26:27 -08001277 cricket::IceGatheringState new_gathering_state = cricket::kIceGatheringNew;
Alex Loiko9289eda2018-11-23 16:18:59 +00001278 bool any_failed = false;
1279
1280 // TODO(http://bugs.webrtc.org/9719) If(when) media_transport disables
1281 // dtls_transports entirely, the below line will have to be changed to account
1282 // for the fact that dtls transports might be absent.
1283 bool all_connected = !dtls_transports.empty();
1284 bool all_completed = !dtls_transports.empty();
Zhi Huange818b6e2018-02-22 15:26:27 -08001285 bool any_gathering = false;
1286 bool all_done_gathering = !dtls_transports.empty();
Jonas Olsson635474e2018-10-18 15:58:17 +02001287
1288 std::map<IceTransportState, int> ice_state_counts;
1289 std::map<cricket::DtlsTransportState, int> dtls_state_counts;
1290
Zhi Huange818b6e2018-02-22 15:26:27 -08001291 for (const auto& dtls : dtls_transports) {
Alex Loiko9289eda2018-11-23 16:18:59 +00001292 any_failed = any_failed || dtls->ice_transport()->GetState() ==
1293 cricket::IceTransportState::STATE_FAILED;
1294 all_connected = all_connected && dtls->writable();
1295 all_completed =
1296 all_completed && dtls->writable() &&
1297 dtls->ice_transport()->GetState() ==
1298 cricket::IceTransportState::STATE_COMPLETED &&
1299 dtls->ice_transport()->GetIceRole() == cricket::ICEROLE_CONTROLLING &&
1300 dtls->ice_transport()->gathering_state() ==
1301 cricket::kIceGatheringComplete;
Zhi Huange818b6e2018-02-22 15:26:27 -08001302 any_gathering = any_gathering || dtls->ice_transport()->gathering_state() !=
1303 cricket::kIceGatheringNew;
1304 all_done_gathering =
1305 all_done_gathering && dtls->ice_transport()->gathering_state() ==
1306 cricket::kIceGatheringComplete;
Jonas Olsson635474e2018-10-18 15:58:17 +02001307
1308 dtls_state_counts[dtls->dtls_state()]++;
1309 ice_state_counts[dtls->ice_transport()->GetIceTransportState()]++;
Zhi Huange818b6e2018-02-22 15:26:27 -08001310 }
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -07001311
Alex Loiko9289eda2018-11-23 16:18:59 +00001312 for (auto it = jsep_transports_by_name_.begin();
1313 it != jsep_transports_by_name_.end(); ++it) {
1314 auto jsep_transport = it->second.get();
1315 if (!jsep_transport->media_transport()) {
1316 continue;
1317 }
1318
1319 // There is no 'kIceConnectionDisconnected', so we only need to handle
1320 // connected and completed.
1321 // We treat kClosed as failed, because if it happens before shutting down
1322 // media transports it means that there was a failure.
1323 // MediaTransportInterface allows to flip back and forth between kWritable
1324 // and kPending, but there does not exist an implementation that does that,
1325 // and the contract of jsep transport controller doesn't quite expect that.
1326 // When this happens, we would go from connected to connecting state, but
1327 // this may change in future.
1328 any_failed |= jsep_transport->media_transport_state() ==
1329 webrtc::MediaTransportState::kClosed;
1330 all_completed &= jsep_transport->media_transport_state() ==
1331 webrtc::MediaTransportState::kWritable;
1332 all_connected &= jsep_transport->media_transport_state() ==
1333 webrtc::MediaTransportState::kWritable;
1334 }
1335
1336 if (any_failed) {
1337 new_connection_state = cricket::kIceConnectionFailed;
1338 } else if (all_completed) {
1339 new_connection_state = cricket::kIceConnectionCompleted;
1340 } else if (all_connected) {
1341 new_connection_state = cricket::kIceConnectionConnected;
1342 }
1343 if (ice_connection_state_ != new_connection_state) {
1344 ice_connection_state_ = new_connection_state;
1345 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
1346 [this, new_connection_state] {
1347 SignalIceConnectionState(new_connection_state);
1348 });
1349 }
1350
Jonas Olsson635474e2018-10-18 15:58:17 +02001351 // Compute the current RTCIceConnectionState as described in
1352 // https://www.w3.org/TR/webrtc/#dom-rtciceconnectionstate.
1353 // The PeerConnection is responsible for handling the "closed" state.
1354 int total_ice_checking = ice_state_counts[IceTransportState::kChecking];
1355 int total_ice_connected = ice_state_counts[IceTransportState::kConnected];
1356 int total_ice_completed = ice_state_counts[IceTransportState::kCompleted];
1357 int total_ice_failed = ice_state_counts[IceTransportState::kFailed];
1358 int total_ice_disconnected =
1359 ice_state_counts[IceTransportState::kDisconnected];
1360 int total_ice_closed = ice_state_counts[IceTransportState::kClosed];
1361 int total_ice_new = ice_state_counts[IceTransportState::kNew];
1362 int total_ice = dtls_transports.size();
1363
1364 if (total_ice_failed > 0) {
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001365 // Any RTCIceTransports are in the "failed" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001366 new_ice_connection_state = PeerConnectionInterface::kIceConnectionFailed;
Alex Loiko9289eda2018-11-23 16:18:59 +00001367 } else if (total_ice_disconnected > 0) {
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001368 // None of the previous states apply and any RTCIceTransports are in the
1369 // "disconnected" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001370 new_ice_connection_state =
1371 PeerConnectionInterface::kIceConnectionDisconnected;
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001372 } else if (total_ice_new + total_ice_closed == total_ice) {
1373 // None of the previous states apply and all RTCIceTransports are in the
1374 // "new" or "closed" state, or there are no transports.
1375 new_ice_connection_state = PeerConnectionInterface::kIceConnectionNew;
1376 } else if (total_ice_new + total_ice_checking > 0) {
1377 // None of the previous states apply and any RTCIceTransports are in the
1378 // "new" or "checking" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001379 new_ice_connection_state = PeerConnectionInterface::kIceConnectionChecking;
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001380 } else if (total_ice_completed + total_ice_closed == total_ice) {
1381 // None of the previous states apply and all RTCIceTransports are in the
1382 // "completed" or "closed" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001383 new_ice_connection_state = PeerConnectionInterface::kIceConnectionCompleted;
1384 } else if (total_ice_connected + total_ice_completed + total_ice_closed ==
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001385 total_ice) {
1386 // None of the previous states apply and all RTCIceTransports are in the
1387 // "connected", "completed" or "closed" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001388 new_ice_connection_state = PeerConnectionInterface::kIceConnectionConnected;
Jonas Olsson635474e2018-10-18 15:58:17 +02001389 } else {
1390 RTC_NOTREACHED();
1391 }
1392
Alex Loiko9289eda2018-11-23 16:18:59 +00001393 if (standardized_ice_connection_state_ != new_ice_connection_state) {
1394 standardized_ice_connection_state_ = new_ice_connection_state;
Jonas Olsson635474e2018-10-18 15:58:17 +02001395 invoker_.AsyncInvoke<void>(
1396 RTC_FROM_HERE, signaling_thread_, [this, new_ice_connection_state] {
Alex Loiko9289eda2018-11-23 16:18:59 +00001397 SignalStandardizedIceConnectionState(new_ice_connection_state);
Jonas Olsson635474e2018-10-18 15:58:17 +02001398 });
1399 }
1400
1401 // Compute the current RTCPeerConnectionState as described in
1402 // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnectionstate.
1403 // The PeerConnection is responsible for handling the "closed" state.
1404 // Note that "connecting" is only a valid state for DTLS transports while
1405 // "checking", "completed" and "disconnected" are only valid for ICE
1406 // transports.
1407 int total_connected = total_ice_connected +
1408 dtls_state_counts[cricket::DTLS_TRANSPORT_CONNECTED];
1409 int total_dtls_connecting =
1410 dtls_state_counts[cricket::DTLS_TRANSPORT_CONNECTING];
1411 int total_failed =
1412 total_ice_failed + dtls_state_counts[cricket::DTLS_TRANSPORT_FAILED];
1413 int total_closed =
1414 total_ice_closed + dtls_state_counts[cricket::DTLS_TRANSPORT_CLOSED];
1415 int total_new =
1416 total_ice_new + dtls_state_counts[cricket::DTLS_TRANSPORT_NEW];
1417 int total_transports = total_ice * 2;
1418
1419 if (total_failed > 0) {
1420 // Any of the RTCIceTransports or RTCDtlsTransports are in a "failed" state.
1421 new_combined_state = PeerConnectionInterface::PeerConnectionState::kFailed;
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001422 } else if (total_ice_disconnected > 0) {
1423 // None of the previous states apply and any RTCIceTransports or
1424 // RTCDtlsTransports are in the "disconnected" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001425 new_combined_state =
1426 PeerConnectionInterface::PeerConnectionState::kDisconnected;
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001427 } else if (total_new + total_closed == total_transports) {
1428 // None of the previous states apply and all RTCIceTransports and
1429 // RTCDtlsTransports are in the "new" or "closed" state, or there are no
1430 // transports.
1431 new_combined_state = PeerConnectionInterface::PeerConnectionState::kNew;
1432 } else if (total_new + total_dtls_connecting + total_ice_checking > 0) {
1433 // None of the previous states apply and all RTCIceTransports or
1434 // RTCDtlsTransports are in the "new", "connecting" or "checking" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001435 new_combined_state =
1436 PeerConnectionInterface::PeerConnectionState::kConnecting;
1437 } else if (total_connected + total_ice_completed + total_closed ==
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001438 total_transports) {
1439 // None of the previous states apply and all RTCIceTransports and
1440 // RTCDtlsTransports are in the "connected", "completed" or "closed" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001441 new_combined_state =
1442 PeerConnectionInterface::PeerConnectionState::kConnected;
Jonas Olsson635474e2018-10-18 15:58:17 +02001443 } else {
1444 RTC_NOTREACHED();
1445 }
1446
1447 if (combined_connection_state_ != new_combined_state) {
1448 combined_connection_state_ = new_combined_state;
1449 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
1450 [this, new_combined_state] {
1451 SignalConnectionState(new_combined_state);
1452 });
1453 }
1454
Zhi Huange818b6e2018-02-22 15:26:27 -08001455 if (all_done_gathering) {
1456 new_gathering_state = cricket::kIceGatheringComplete;
1457 } else if (any_gathering) {
1458 new_gathering_state = cricket::kIceGatheringGathering;
1459 }
1460 if (ice_gathering_state_ != new_gathering_state) {
1461 ice_gathering_state_ = new_gathering_state;
Steve Antond25828a2018-08-31 13:06:05 -07001462 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
1463 [this, new_gathering_state] {
1464 SignalIceGatheringState(new_gathering_state);
1465 });
Zhi Huange818b6e2018-02-22 15:26:27 -08001466 }
1467}
1468
1469void JsepTransportController::OnDtlsHandshakeError(
1470 rtc::SSLHandshakeError error) {
1471 SignalDtlsHandshakeError(error);
1472}
1473
1474} // namespace webrtc