blob: 926d71dcc65388853487fa5e67e92c7a50aae101 [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"
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -080018#include "p2p/base/ice_transport_internal.h"
19#include "p2p/base/no_op_dtls_transport.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080020#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "pc/srtp_filter.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080022#include "rtc_base/bind.h"
23#include "rtc_base/checks.h"
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -070024#include "rtc_base/key_derivation.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080025#include "rtc_base/thread.h"
26
27using webrtc::SdpType;
28
29namespace {
30
Zhi Huange818b6e2018-02-22 15:26:27 -080031webrtc::RTCError VerifyCandidate(const cricket::Candidate& cand) {
32 // No address zero.
33 if (cand.address().IsNil() || cand.address().IsAnyIP()) {
34 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
35 "candidate has address of zero");
36 }
37
38 // Disallow all ports below 1024, except for 80 and 443 on public addresses.
39 int port = cand.address().port();
40 if (cand.protocol() == cricket::TCP_PROTOCOL_NAME &&
41 (cand.tcptype() == cricket::TCPTYPE_ACTIVE_STR || port == 0)) {
42 // Expected for active-only candidates per
43 // http://tools.ietf.org/html/rfc6544#section-4.5 so no error.
44 // Libjingle clients emit port 0, in "active" mode.
45 return webrtc::RTCError::OK();
46 }
47 if (port < 1024) {
48 if ((port != 80) && (port != 443)) {
49 return webrtc::RTCError(
50 webrtc::RTCErrorType::INVALID_PARAMETER,
51 "candidate has port below 1024, but not 80 or 443");
52 }
53
54 if (cand.address().IsPrivateIP()) {
55 return webrtc::RTCError(
56 webrtc::RTCErrorType::INVALID_PARAMETER,
57 "candidate has port of 80 or 443 with private IP address");
58 }
59 }
60
61 return webrtc::RTCError::OK();
62}
63
64webrtc::RTCError VerifyCandidates(const cricket::Candidates& candidates) {
65 for (const cricket::Candidate& candidate : candidates) {
66 webrtc::RTCError error = VerifyCandidate(candidate);
67 if (!error.ok()) {
68 return error;
69 }
70 }
71 return webrtc::RTCError::OK();
72}
73
74} // namespace
75
76namespace webrtc {
77
78JsepTransportController::JsepTransportController(
79 rtc::Thread* signaling_thread,
80 rtc::Thread* network_thread,
81 cricket::PortAllocator* port_allocator,
Zach Steine20867f2018-08-02 13:20:15 -070082 AsyncResolverFactory* async_resolver_factory,
Zhi Huange818b6e2018-02-22 15:26:27 -080083 Config config)
84 : signaling_thread_(signaling_thread),
85 network_thread_(network_thread),
86 port_allocator_(port_allocator),
Zach Steine20867f2018-08-02 13:20:15 -070087 async_resolver_factory_(async_resolver_factory),
Zhi Huang365381f2018-04-13 16:44:34 -070088 config_(config) {
89 // The |transport_observer| is assumed to be non-null.
90 RTC_DCHECK(config_.transport_observer);
91}
Zhi Huange818b6e2018-02-22 15:26:27 -080092
93JsepTransportController::~JsepTransportController() {
94 // Channel destructors may try to send packets, so this needs to happen on
95 // the network thread.
96 network_thread_->Invoke<void>(
97 RTC_FROM_HERE,
98 rtc::Bind(&JsepTransportController::DestroyAllJsepTransports_n, this));
99}
100
101RTCError JsepTransportController::SetLocalDescription(
102 SdpType type,
103 const cricket::SessionDescription* description) {
104 if (!network_thread_->IsCurrent()) {
105 return network_thread_->Invoke<RTCError>(
106 RTC_FROM_HERE, [=] { return SetLocalDescription(type, description); });
107 }
108
109 if (!initial_offerer_.has_value()) {
110 initial_offerer_.emplace(type == SdpType::kOffer);
111 if (*initial_offerer_) {
112 SetIceRole_n(cricket::ICEROLE_CONTROLLING);
113 } else {
114 SetIceRole_n(cricket::ICEROLE_CONTROLLED);
115 }
116 }
117 return ApplyDescription_n(/*local=*/true, type, description);
118}
119
120RTCError JsepTransportController::SetRemoteDescription(
121 SdpType type,
122 const cricket::SessionDescription* description) {
123 if (!network_thread_->IsCurrent()) {
124 return network_thread_->Invoke<RTCError>(
125 RTC_FROM_HERE, [=] { return SetRemoteDescription(type, description); });
126 }
127
128 return ApplyDescription_n(/*local=*/false, type, description);
129}
130
131RtpTransportInternal* JsepTransportController::GetRtpTransport(
132 const std::string& mid) const {
Zhi Huange830e682018-03-30 10:48:35 -0700133 auto jsep_transport = GetJsepTransportForMid(mid);
Zhi Huange818b6e2018-02-22 15:26:27 -0800134 if (!jsep_transport) {
135 return nullptr;
136 }
137 return jsep_transport->rtp_transport();
138}
139
Anton Sukhanov7940da02018-10-10 10:34:49 -0700140MediaTransportInterface* JsepTransportController::GetMediaTransport(
141 const std::string& mid) const {
142 auto jsep_transport = GetJsepTransportForMid(mid);
143 if (!jsep_transport) {
144 return nullptr;
145 }
146 return jsep_transport->media_transport();
147}
148
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800149MediaTransportState JsepTransportController::GetMediaTransportState(
150 const std::string& mid) const {
151 auto jsep_transport = GetJsepTransportForMid(mid);
152 if (!jsep_transport) {
153 return MediaTransportState::kPending;
154 }
155 return jsep_transport->media_transport_state();
156}
157
Zhi Huange818b6e2018-02-22 15:26:27 -0800158cricket::DtlsTransportInternal* JsepTransportController::GetDtlsTransport(
Harald Alvestrandad88c882018-11-28 16:47:46 +0100159 const std::string& mid) {
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->rtp_dtls_transport();
165}
166
Harald Alvestrandad88c882018-11-28 16:47:46 +0100167const cricket::DtlsTransportInternal*
168JsepTransportController::GetRtcpDtlsTransport(const std::string& mid) const {
Zhi Huange830e682018-03-30 10:48:35 -0700169 auto jsep_transport = GetJsepTransportForMid(mid);
Zhi Huange818b6e2018-02-22 15:26:27 -0800170 if (!jsep_transport) {
171 return nullptr;
172 }
173 return jsep_transport->rtcp_dtls_transport();
174}
175
Harald Alvestrandad88c882018-11-28 16:47:46 +0100176rtc::scoped_refptr<webrtc::DtlsTransportInterface>
177JsepTransportController::LookupDtlsTransportByMid(const std::string& mid) {
178 auto jsep_transport = GetJsepTransportForMid(mid);
179 if (!jsep_transport) {
180 return nullptr;
181 }
182 return jsep_transport->RtpDtlsTransport();
183}
184
Zhi Huange818b6e2018-02-22 15:26:27 -0800185void JsepTransportController::SetIceConfig(const cricket::IceConfig& config) {
186 if (!network_thread_->IsCurrent()) {
187 network_thread_->Invoke<void>(RTC_FROM_HERE, [&] { SetIceConfig(config); });
188 return;
189 }
190
191 ice_config_ = config;
192 for (auto& dtls : GetDtlsTransports()) {
193 dtls->ice_transport()->SetIceConfig(ice_config_);
194 }
195}
196
197void JsepTransportController::SetNeedsIceRestartFlag() {
Zhi Huange830e682018-03-30 10:48:35 -0700198 for (auto& kv : jsep_transports_by_name_) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800199 kv.second->SetNeedsIceRestartFlag();
200 }
201}
202
203bool JsepTransportController::NeedsIceRestart(
204 const std::string& transport_name) const {
Zhi Huang365381f2018-04-13 16:44:34 -0700205 const cricket::JsepTransport* transport =
Zhi Huange830e682018-03-30 10:48:35 -0700206 GetJsepTransportByName(transport_name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800207 if (!transport) {
208 return false;
209 }
210 return transport->needs_ice_restart();
211}
212
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200213absl::optional<rtc::SSLRole> JsepTransportController::GetDtlsRole(
Zhi Huange830e682018-03-30 10:48:35 -0700214 const std::string& mid) const {
Zhi Huange818b6e2018-02-22 15:26:27 -0800215 if (!network_thread_->IsCurrent()) {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200216 return network_thread_->Invoke<absl::optional<rtc::SSLRole>>(
Zhi Huange830e682018-03-30 10:48:35 -0700217 RTC_FROM_HERE, [&] { return GetDtlsRole(mid); });
Zhi Huange818b6e2018-02-22 15:26:27 -0800218 }
219
Zhi Huang365381f2018-04-13 16:44:34 -0700220 const cricket::JsepTransport* t = GetJsepTransportForMid(mid);
Zhi Huange818b6e2018-02-22 15:26:27 -0800221 if (!t) {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200222 return absl::optional<rtc::SSLRole>();
Zhi Huange818b6e2018-02-22 15:26:27 -0800223 }
224 return t->GetDtlsRole();
225}
226
227bool JsepTransportController::SetLocalCertificate(
228 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
229 if (!network_thread_->IsCurrent()) {
230 return network_thread_->Invoke<bool>(
231 RTC_FROM_HERE, [&] { return SetLocalCertificate(certificate); });
232 }
233
234 // Can't change a certificate, or set a null certificate.
235 if (certificate_ || !certificate) {
236 return false;
237 }
238 certificate_ = certificate;
239
240 // Set certificate for JsepTransport, which verifies it matches the
241 // fingerprint in SDP, and DTLS transport.
242 // Fallback from DTLS to SDES is not supported.
Zhi Huange830e682018-03-30 10:48:35 -0700243 for (auto& kv : jsep_transports_by_name_) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800244 kv.second->SetLocalCertificate(certificate_);
245 }
246 for (auto& dtls : GetDtlsTransports()) {
247 bool set_cert_success = dtls->SetLocalCertificate(certificate_);
248 RTC_DCHECK(set_cert_success);
249 }
250 return true;
251}
252
253rtc::scoped_refptr<rtc::RTCCertificate>
254JsepTransportController::GetLocalCertificate(
255 const std::string& transport_name) const {
256 if (!network_thread_->IsCurrent()) {
257 return network_thread_->Invoke<rtc::scoped_refptr<rtc::RTCCertificate>>(
258 RTC_FROM_HERE, [&] { return GetLocalCertificate(transport_name); });
259 }
260
Zhi Huang365381f2018-04-13 16:44:34 -0700261 const cricket::JsepTransport* t = GetJsepTransportByName(transport_name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800262 if (!t) {
263 return nullptr;
264 }
265 return t->GetLocalCertificate();
266}
267
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800268std::unique_ptr<rtc::SSLCertChain>
269JsepTransportController::GetRemoteSSLCertChain(
Zhi Huange818b6e2018-02-22 15:26:27 -0800270 const std::string& transport_name) const {
271 if (!network_thread_->IsCurrent()) {
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800272 return network_thread_->Invoke<std::unique_ptr<rtc::SSLCertChain>>(
273 RTC_FROM_HERE, [&] { return GetRemoteSSLCertChain(transport_name); });
Zhi Huange818b6e2018-02-22 15:26:27 -0800274 }
275
Zhi Huange830e682018-03-30 10:48:35 -0700276 // Get the certificate from the RTP transport's DTLS handshake. Should be
277 // identical to the RTCP transport's, since they were given the same remote
Zhi Huange818b6e2018-02-22 15:26:27 -0800278 // fingerprint.
Zhi Huange830e682018-03-30 10:48:35 -0700279 auto jsep_transport = GetJsepTransportByName(transport_name);
280 if (!jsep_transport) {
281 return nullptr;
282 }
283 auto dtls = jsep_transport->rtp_dtls_transport();
Zhi Huange818b6e2018-02-22 15:26:27 -0800284 if (!dtls) {
285 return nullptr;
286 }
287
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800288 return dtls->GetRemoteSSLCertChain();
Zhi Huange818b6e2018-02-22 15:26:27 -0800289}
290
291void JsepTransportController::MaybeStartGathering() {
292 if (!network_thread_->IsCurrent()) {
293 network_thread_->Invoke<void>(RTC_FROM_HERE,
294 [&] { MaybeStartGathering(); });
295 return;
296 }
297
298 for (auto& dtls : GetDtlsTransports()) {
299 dtls->ice_transport()->MaybeStartGathering();
300 }
301}
302
303RTCError JsepTransportController::AddRemoteCandidates(
304 const std::string& transport_name,
305 const cricket::Candidates& candidates) {
306 if (!network_thread_->IsCurrent()) {
307 return network_thread_->Invoke<RTCError>(RTC_FROM_HERE, [&] {
308 return AddRemoteCandidates(transport_name, candidates);
309 });
310 }
311
312 // Verify each candidate before passing down to the transport layer.
313 RTCError error = VerifyCandidates(candidates);
314 if (!error.ok()) {
315 return error;
316 }
Zhi Huange830e682018-03-30 10:48:35 -0700317 auto jsep_transport = GetJsepTransportByName(transport_name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800318 if (!jsep_transport) {
Zhi Huange830e682018-03-30 10:48:35 -0700319 RTC_LOG(LS_WARNING) << "Not adding candidate because the JsepTransport "
320 "doesn't exist. Ignore it.";
321 return RTCError::OK();
Zhi Huange818b6e2018-02-22 15:26:27 -0800322 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800323 return jsep_transport->AddRemoteCandidates(candidates);
324}
325
326RTCError JsepTransportController::RemoveRemoteCandidates(
327 const cricket::Candidates& candidates) {
328 if (!network_thread_->IsCurrent()) {
329 return network_thread_->Invoke<RTCError>(
330 RTC_FROM_HERE, [&] { return RemoveRemoteCandidates(candidates); });
331 }
332
333 // Verify each candidate before passing down to the transport layer.
334 RTCError error = VerifyCandidates(candidates);
335 if (!error.ok()) {
336 return error;
337 }
338
339 std::map<std::string, cricket::Candidates> candidates_by_transport_name;
340 for (const cricket::Candidate& cand : candidates) {
341 if (!cand.transport_name().empty()) {
342 candidates_by_transport_name[cand.transport_name()].push_back(cand);
343 } else {
344 RTC_LOG(LS_ERROR) << "Not removing candidate because it does not have a "
345 "transport name set: "
346 << cand.ToString();
347 }
348 }
349
350 for (const auto& kv : candidates_by_transport_name) {
351 const std::string& transport_name = kv.first;
352 const cricket::Candidates& candidates = kv.second;
Zhi Huang365381f2018-04-13 16:44:34 -0700353 cricket::JsepTransport* jsep_transport =
Zhi Huange830e682018-03-30 10:48:35 -0700354 GetJsepTransportByName(transport_name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800355 if (!jsep_transport) {
Zhi Huange830e682018-03-30 10:48:35 -0700356 RTC_LOG(LS_WARNING)
357 << "Not removing candidate because the JsepTransport doesn't exist.";
358 continue;
Zhi Huange818b6e2018-02-22 15:26:27 -0800359 }
360 for (const cricket::Candidate& candidate : candidates) {
Harald Alvestrandad88c882018-11-28 16:47:46 +0100361 cricket::DtlsTransportInternal* dtls =
362 candidate.component() == cricket::ICE_CANDIDATE_COMPONENT_RTP
363 ? jsep_transport->rtp_dtls_transport()
364 : jsep_transport->rtcp_dtls_transport();
Zhi Huange818b6e2018-02-22 15:26:27 -0800365 if (dtls) {
366 dtls->ice_transport()->RemoveRemoteCandidate(candidate);
367 }
368 }
369 }
370 return RTCError::OK();
371}
372
373bool JsepTransportController::GetStats(const std::string& transport_name,
374 cricket::TransportStats* stats) {
375 if (!network_thread_->IsCurrent()) {
376 return network_thread_->Invoke<bool>(
377 RTC_FROM_HERE, [=] { return GetStats(transport_name, stats); });
378 }
379
Zhi Huang365381f2018-04-13 16:44:34 -0700380 cricket::JsepTransport* transport = GetJsepTransportByName(transport_name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800381 if (!transport) {
382 return false;
383 }
384 return transport->GetStats(stats);
385}
386
Zhi Huangb57e1692018-06-12 11:41:11 -0700387void JsepTransportController::SetActiveResetSrtpParams(
388 bool active_reset_srtp_params) {
389 if (!network_thread_->IsCurrent()) {
390 network_thread_->Invoke<void>(RTC_FROM_HERE, [=] {
391 SetActiveResetSrtpParams(active_reset_srtp_params);
392 });
393 return;
394 }
395
396 RTC_LOG(INFO)
397 << "Updating the active_reset_srtp_params for JsepTransportController: "
398 << active_reset_srtp_params;
399 config_.active_reset_srtp_params = active_reset_srtp_params;
400 for (auto& kv : jsep_transports_by_name_) {
401 kv.second->SetActiveResetSrtpParams(active_reset_srtp_params);
402 }
403}
404
Piotr (Peter) Slatala97fc11f2018-10-18 12:57:59 -0700405void JsepTransportController::SetMediaTransportFactory(
406 MediaTransportFactory* media_transport_factory) {
407 RTC_DCHECK(media_transport_factory == config_.media_transport_factory ||
408 jsep_transports_by_name_.empty())
409 << "You can only call SetMediaTransportFactory before "
410 "JsepTransportController created its first transport.";
411 config_.media_transport_factory = media_transport_factory;
412}
413
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -0800414std::unique_ptr<cricket::IceTransportInternal>
415JsepTransportController::CreateIceTransport(const std::string transport_name,
416 bool rtcp) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800417 int component = rtcp ? cricket::ICE_CANDIDATE_COMPONENT_RTCP
418 : cricket::ICE_CANDIDATE_COMPONENT_RTP;
419
Zhi Huange818b6e2018-02-22 15:26:27 -0800420 if (config_.external_transport_factory) {
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -0800421 return config_.external_transport_factory->CreateIceTransport(
Zhi Huange818b6e2018-02-22 15:26:27 -0800422 transport_name, component);
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -0800423 } else {
424 return absl::make_unique<cricket::P2PTransportChannel>(
425 transport_name, component, port_allocator_, async_resolver_factory_,
426 config_.event_log);
427 }
428}
429
430std::unique_ptr<cricket::DtlsTransportInternal>
431JsepTransportController::CreateDtlsTransport(
432 std::unique_ptr<cricket::IceTransportInternal> ice) {
433 RTC_DCHECK(network_thread_->IsCurrent());
434
435 std::unique_ptr<cricket::DtlsTransportInternal> dtls;
436 if (config_.media_transport_factory) {
437 dtls = absl::make_unique<cricket::NoOpDtlsTransport>(
438 std::move(ice), config_.crypto_options);
439 } else if (config_.external_transport_factory) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800440 dtls = config_.external_transport_factory->CreateDtlsTransport(
441 std::move(ice), config_.crypto_options);
442 } else {
Zach Steinc64078f2018-11-27 15:53:01 -0800443 dtls = absl::make_unique<cricket::DtlsTransport>(
444 std::move(ice), config_.crypto_options, config_.event_log);
Zhi Huange818b6e2018-02-22 15:26:27 -0800445 }
446
447 RTC_DCHECK(dtls);
448 dtls->SetSslMaxProtocolVersion(config_.ssl_max_version);
Zhi Huange818b6e2018-02-22 15:26:27 -0800449 dtls->ice_transport()->SetIceRole(ice_role_);
450 dtls->ice_transport()->SetIceTiebreaker(ice_tiebreaker_);
451 dtls->ice_transport()->SetIceConfig(ice_config_);
452 if (certificate_) {
453 bool set_cert_success = dtls->SetLocalCertificate(certificate_);
454 RTC_DCHECK(set_cert_success);
455 }
456
457 // Connect to signals offered by the DTLS and ICE transport.
458 dtls->SignalWritableState.connect(
459 this, &JsepTransportController::OnTransportWritableState_n);
460 dtls->SignalReceivingState.connect(
461 this, &JsepTransportController::OnTransportReceivingState_n);
462 dtls->SignalDtlsHandshakeError.connect(
463 this, &JsepTransportController::OnDtlsHandshakeError);
464 dtls->ice_transport()->SignalGatheringState.connect(
465 this, &JsepTransportController::OnTransportGatheringState_n);
466 dtls->ice_transport()->SignalCandidateGathered.connect(
467 this, &JsepTransportController::OnTransportCandidateGathered_n);
468 dtls->ice_transport()->SignalCandidatesRemoved.connect(
469 this, &JsepTransportController::OnTransportCandidatesRemoved_n);
470 dtls->ice_transport()->SignalRoleConflict.connect(
471 this, &JsepTransportController::OnTransportRoleConflict_n);
472 dtls->ice_transport()->SignalStateChanged.connect(
473 this, &JsepTransportController::OnTransportStateChanged_n);
Jonas Olsson7a6739e2019-01-15 16:31:55 +0100474 dtls->ice_transport()->SignalIceTransportStateChanged.connect(
475 this, &JsepTransportController::OnTransportStateChanged_n);
Zhi Huange818b6e2018-02-22 15:26:27 -0800476 return dtls;
477}
478
479std::unique_ptr<webrtc::RtpTransport>
480JsepTransportController::CreateUnencryptedRtpTransport(
481 const std::string& transport_name,
482 rtc::PacketTransportInternal* rtp_packet_transport,
483 rtc::PacketTransportInternal* rtcp_packet_transport) {
484 RTC_DCHECK(network_thread_->IsCurrent());
Zhi Huange830e682018-03-30 10:48:35 -0700485 auto unencrypted_rtp_transport =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200486 absl::make_unique<RtpTransport>(rtcp_packet_transport == nullptr);
Zhi Huange830e682018-03-30 10:48:35 -0700487 unencrypted_rtp_transport->SetRtpPacketTransport(rtp_packet_transport);
488 if (rtcp_packet_transport) {
489 unencrypted_rtp_transport->SetRtcpPacketTransport(rtcp_packet_transport);
490 }
491 return unencrypted_rtp_transport;
Zhi Huange818b6e2018-02-22 15:26:27 -0800492}
493
494std::unique_ptr<webrtc::SrtpTransport>
495JsepTransportController::CreateSdesTransport(
496 const std::string& transport_name,
Zhi Huange830e682018-03-30 10:48:35 -0700497 cricket::DtlsTransportInternal* rtp_dtls_transport,
498 cricket::DtlsTransportInternal* rtcp_dtls_transport) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800499 RTC_DCHECK(network_thread_->IsCurrent());
Zhi Huange818b6e2018-02-22 15:26:27 -0800500 auto srtp_transport =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200501 absl::make_unique<webrtc::SrtpTransport>(rtcp_dtls_transport == nullptr);
Zhi Huange830e682018-03-30 10:48:35 -0700502 RTC_DCHECK(rtp_dtls_transport);
503 srtp_transport->SetRtpPacketTransport(rtp_dtls_transport);
504 if (rtcp_dtls_transport) {
505 srtp_transport->SetRtcpPacketTransport(rtcp_dtls_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800506 }
507 if (config_.enable_external_auth) {
508 srtp_transport->EnableExternalAuth();
509 }
510 return srtp_transport;
511}
512
513std::unique_ptr<webrtc::DtlsSrtpTransport>
514JsepTransportController::CreateDtlsSrtpTransport(
515 const std::string& transport_name,
516 cricket::DtlsTransportInternal* rtp_dtls_transport,
517 cricket::DtlsTransportInternal* rtcp_dtls_transport) {
518 RTC_DCHECK(network_thread_->IsCurrent());
Karl Wiberg918f50c2018-07-05 11:40:33 +0200519 auto dtls_srtp_transport = absl::make_unique<webrtc::DtlsSrtpTransport>(
Zhi Huang365381f2018-04-13 16:44:34 -0700520 rtcp_dtls_transport == nullptr);
Zhi Huang27f3bf52018-03-26 21:37:23 -0700521 if (config_.enable_external_auth) {
Zhi Huang365381f2018-04-13 16:44:34 -0700522 dtls_srtp_transport->EnableExternalAuth();
Zhi Huang27f3bf52018-03-26 21:37:23 -0700523 }
Zhi Huang97d5e5b2018-03-27 00:09:01 +0000524
Zhi Huange818b6e2018-02-22 15:26:27 -0800525 dtls_srtp_transport->SetDtlsTransports(rtp_dtls_transport,
526 rtcp_dtls_transport);
Zhi Huangb57e1692018-06-12 11:41:11 -0700527 dtls_srtp_transport->SetActiveResetSrtpParams(
528 config_.active_reset_srtp_params);
Jonas Olsson635474e2018-10-18 15:58:17 +0200529 dtls_srtp_transport->SignalDtlsStateChange.connect(
530 this, &JsepTransportController::UpdateAggregateStates_n);
Zhi Huange818b6e2018-02-22 15:26:27 -0800531 return dtls_srtp_transport;
532}
533
534std::vector<cricket::DtlsTransportInternal*>
535JsepTransportController::GetDtlsTransports() {
536 std::vector<cricket::DtlsTransportInternal*> dtls_transports;
Zhi Huange830e682018-03-30 10:48:35 -0700537 for (auto it = jsep_transports_by_name_.begin();
538 it != jsep_transports_by_name_.end(); ++it) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800539 auto jsep_transport = it->second.get();
540 RTC_DCHECK(jsep_transport);
541 if (jsep_transport->rtp_dtls_transport()) {
542 dtls_transports.push_back(jsep_transport->rtp_dtls_transport());
543 }
544
545 if (jsep_transport->rtcp_dtls_transport()) {
546 dtls_transports.push_back(jsep_transport->rtcp_dtls_transport());
547 }
548 }
549 return dtls_transports;
550}
551
Zhi Huange818b6e2018-02-22 15:26:27 -0800552RTCError JsepTransportController::ApplyDescription_n(
553 bool local,
554 SdpType type,
555 const cricket::SessionDescription* description) {
556 RTC_DCHECK(network_thread_->IsCurrent());
557 RTC_DCHECK(description);
558
559 if (local) {
560 local_desc_ = description;
561 } else {
562 remote_desc_ = description;
563 }
564
Zhi Huange830e682018-03-30 10:48:35 -0700565 RTCError error;
Zhi Huangd2248f82018-04-10 14:41:03 -0700566 error = ValidateAndMaybeUpdateBundleGroup(local, type, description);
Zhi Huange830e682018-03-30 10:48:35 -0700567 if (!error.ok()) {
568 return error;
Zhi Huange818b6e2018-02-22 15:26:27 -0800569 }
570
571 std::vector<int> merged_encrypted_extension_ids;
572 if (bundle_group_) {
573 merged_encrypted_extension_ids =
574 MergeEncryptedHeaderExtensionIdsForBundle(description);
575 }
576
577 for (const cricket::ContentInfo& content_info : description->contents()) {
578 // Don't create transports for rejected m-lines and bundled m-lines."
579 if (content_info.rejected ||
580 (IsBundled(content_info.name) && content_info.name != *bundled_mid())) {
581 continue;
582 }
Anton Sukhanov7940da02018-10-10 10:34:49 -0700583 error = MaybeCreateJsepTransport(local, content_info);
Zhi Huange830e682018-03-30 10:48:35 -0700584 if (!error.ok()) {
585 return error;
586 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800587 }
588
589 RTC_DCHECK(description->contents().size() ==
590 description->transport_infos().size());
591 for (size_t i = 0; i < description->contents().size(); ++i) {
592 const cricket::ContentInfo& content_info = description->contents()[i];
593 const cricket::TransportInfo& transport_info =
594 description->transport_infos()[i];
595 if (content_info.rejected) {
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700596 HandleRejectedContent(content_info, description);
Zhi Huange818b6e2018-02-22 15:26:27 -0800597 continue;
598 }
599
600 if (IsBundled(content_info.name) && content_info.name != *bundled_mid()) {
Zhi Huang365381f2018-04-13 16:44:34 -0700601 if (!HandleBundledContent(content_info)) {
602 return RTCError(RTCErrorType::INVALID_PARAMETER,
603 "Failed to process the bundled m= section.");
604 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800605 continue;
606 }
607
Zhi Huange830e682018-03-30 10:48:35 -0700608 error = ValidateContent(content_info);
609 if (!error.ok()) {
610 return error;
611 }
612
Zhi Huange818b6e2018-02-22 15:26:27 -0800613 std::vector<int> extension_ids;
Taylor Brandstetter0ab56512018-04-12 10:30:48 -0700614 if (bundled_mid() && content_info.name == *bundled_mid()) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800615 extension_ids = merged_encrypted_extension_ids;
616 } else {
617 extension_ids = GetEncryptedHeaderExtensionIds(content_info);
618 }
619
Zhi Huange830e682018-03-30 10:48:35 -0700620 int rtp_abs_sendtime_extn_id =
621 GetRtpAbsSendTimeHeaderExtensionId(content_info);
622
Zhi Huang365381f2018-04-13 16:44:34 -0700623 cricket::JsepTransport* transport =
Zhi Huange830e682018-03-30 10:48:35 -0700624 GetJsepTransportForMid(content_info.name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800625 RTC_DCHECK(transport);
626
627 SetIceRole_n(DetermineIceRole(transport, transport_info, type, local));
628
Zhi Huange818b6e2018-02-22 15:26:27 -0800629 cricket::JsepTransportDescription jsep_description =
630 CreateJsepTransportDescription(content_info, transport_info,
Zhi Huange830e682018-03-30 10:48:35 -0700631 extension_ids, rtp_abs_sendtime_extn_id);
Zhi Huange818b6e2018-02-22 15:26:27 -0800632 if (local) {
633 error =
634 transport->SetLocalJsepTransportDescription(jsep_description, type);
635 } else {
636 error =
637 transport->SetRemoteJsepTransportDescription(jsep_description, type);
638 }
639
640 if (!error.ok()) {
641 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
642 "Failed to apply the description for " +
643 content_info.name + ": " + error.message());
644 }
645 }
646 return RTCError::OK();
647}
648
Zhi Huangd2248f82018-04-10 14:41:03 -0700649RTCError JsepTransportController::ValidateAndMaybeUpdateBundleGroup(
650 bool local,
651 SdpType type,
Zhi Huange830e682018-03-30 10:48:35 -0700652 const cricket::SessionDescription* description) {
653 RTC_DCHECK(description);
Zhi Huangd2248f82018-04-10 14:41:03 -0700654 const cricket::ContentGroup* new_bundle_group =
655 description->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
656
657 // The BUNDLE group containing a MID that no m= section has is invalid.
658 if (new_bundle_group) {
659 for (auto content_name : new_bundle_group->content_names()) {
660 if (!description->GetContentByName(content_name)) {
661 return RTCError(RTCErrorType::INVALID_PARAMETER,
662 "The BUNDLE group contains MID:" + content_name +
663 " matching no m= section.");
664 }
665 }
666 }
667
668 if (type == SdpType::kAnswer) {
669 const cricket::ContentGroup* offered_bundle_group =
670 local ? remote_desc_->GetGroupByName(cricket::GROUP_TYPE_BUNDLE)
671 : local_desc_->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
672
673 if (new_bundle_group) {
674 // The BUNDLE group in answer should be a subset of offered group.
675 for (auto content_name : new_bundle_group->content_names()) {
676 if (!offered_bundle_group ||
677 !offered_bundle_group->HasContentName(content_name)) {
678 return RTCError(RTCErrorType::INVALID_PARAMETER,
679 "The BUNDLE group in answer contains a MID that was "
680 "not in the offered group.");
681 }
682 }
683 }
684
685 if (bundle_group_) {
686 for (auto content_name : bundle_group_->content_names()) {
687 // An answer that removes m= sections from pre-negotiated BUNDLE group
688 // without rejecting it, is invalid.
689 if (!new_bundle_group ||
690 !new_bundle_group->HasContentName(content_name)) {
691 auto* content_info = description->GetContentByName(content_name);
692 if (!content_info || !content_info->rejected) {
693 return RTCError(RTCErrorType::INVALID_PARAMETER,
694 "Answer cannot remove m= section " + content_name +
695 " from already-established BUNDLE group.");
696 }
697 }
698 }
699 }
700 }
701
702 if (config_.bundle_policy ==
703 PeerConnectionInterface::kBundlePolicyMaxBundle &&
704 !description->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
705 return RTCError(RTCErrorType::INVALID_PARAMETER,
706 "max-bundle is used but no bundle group found.");
707 }
708
709 if (ShouldUpdateBundleGroup(type, description)) {
Zhi Huangd2248f82018-04-10 14:41:03 -0700710 bundle_group_ = *new_bundle_group;
711 }
Zhi Huange830e682018-03-30 10:48:35 -0700712
713 if (!bundled_mid()) {
714 return RTCError::OK();
715 }
716
717 auto bundled_content = description->GetContentByName(*bundled_mid());
718 if (!bundled_content) {
719 return RTCError(
720 RTCErrorType::INVALID_PARAMETER,
721 "An m= section associated with the BUNDLE-tag doesn't exist.");
722 }
723
724 // If the |bundled_content| is rejected, other contents in the bundle group
725 // should be rejected.
726 if (bundled_content->rejected) {
727 for (auto content_name : bundle_group_->content_names()) {
728 auto other_content = description->GetContentByName(content_name);
729 if (!other_content->rejected) {
730 return RTCError(
731 RTCErrorType::INVALID_PARAMETER,
732 "The m= section:" + content_name + " should be rejected.");
733 }
734 }
735 }
736
737 return RTCError::OK();
738}
739
740RTCError JsepTransportController::ValidateContent(
741 const cricket::ContentInfo& content_info) {
742 if (config_.rtcp_mux_policy ==
743 PeerConnectionInterface::kRtcpMuxPolicyRequire &&
744 content_info.type == cricket::MediaProtocolType::kRtp &&
745 !content_info.media_description()->rtcp_mux()) {
746 return RTCError(RTCErrorType::INVALID_PARAMETER,
747 "The m= section:" + content_info.name +
748 " is invalid. RTCP-MUX is not "
749 "enabled when it is required.");
750 }
751 return RTCError::OK();
752}
753
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700754void JsepTransportController::HandleRejectedContent(
Zhi Huangd2248f82018-04-10 14:41:03 -0700755 const cricket::ContentInfo& content_info,
756 const cricket::SessionDescription* description) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800757 // If the content is rejected, let the
758 // BaseChannel/SctpTransport change the RtpTransport/DtlsTransport first,
Zhi Huang365381f2018-04-13 16:44:34 -0700759 // then destroy the cricket::JsepTransport.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700760 RemoveTransportForMid(content_info.name);
Zhi Huange830e682018-03-30 10:48:35 -0700761 if (content_info.name == bundled_mid()) {
762 for (auto content_name : bundle_group_->content_names()) {
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700763 RemoveTransportForMid(content_name);
Zhi Huange830e682018-03-30 10:48:35 -0700764 }
765 bundle_group_.reset();
766 } else if (IsBundled(content_info.name)) {
767 // Remove the rejected content from the |bundle_group_|.
Zhi Huange818b6e2018-02-22 15:26:27 -0800768 bundle_group_->RemoveContentName(content_info.name);
Zhi Huange830e682018-03-30 10:48:35 -0700769 // Reset the bundle group if nothing left.
770 if (!bundle_group_->FirstContentName()) {
771 bundle_group_.reset();
772 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800773 }
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700774 MaybeDestroyJsepTransport(content_info.name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800775}
776
Zhi Huang365381f2018-04-13 16:44:34 -0700777bool JsepTransportController::HandleBundledContent(
Zhi Huange818b6e2018-02-22 15:26:27 -0800778 const cricket::ContentInfo& content_info) {
Zhi Huangd2248f82018-04-10 14:41:03 -0700779 auto jsep_transport = GetJsepTransportByName(*bundled_mid());
780 RTC_DCHECK(jsep_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800781 // If the content is bundled, let the
782 // BaseChannel/SctpTransport change the RtpTransport/DtlsTransport first,
Zhi Huang365381f2018-04-13 16:44:34 -0700783 // then destroy the cricket::JsepTransport.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700784 if (SetTransportForMid(content_info.name, jsep_transport)) {
Piotr (Peter) Slatala10aeb2a2018-11-14 10:57:24 -0800785 // TODO(bugs.webrtc.org/9719) For media transport this is far from ideal,
786 // because it means that we first create media transport and start
787 // connecting it, and then we destroy it. We will need to address it before
788 // video path is enabled.
Zhi Huang365381f2018-04-13 16:44:34 -0700789 MaybeDestroyJsepTransport(content_info.name);
790 return true;
791 }
792 return false;
Zhi Huange818b6e2018-02-22 15:26:27 -0800793}
794
Zhi Huang365381f2018-04-13 16:44:34 -0700795bool JsepTransportController::SetTransportForMid(
Zhi Huangd2248f82018-04-10 14:41:03 -0700796 const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700797 cricket::JsepTransport* jsep_transport) {
Zhi Huang365381f2018-04-13 16:44:34 -0700798 RTC_DCHECK(jsep_transport);
Zhi Huangd2248f82018-04-10 14:41:03 -0700799 if (mid_to_transport_[mid] == jsep_transport) {
Zhi Huang365381f2018-04-13 16:44:34 -0700800 return true;
Zhi Huangd2248f82018-04-10 14:41:03 -0700801 }
802
803 mid_to_transport_[mid] = jsep_transport;
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700804 return config_.transport_observer->OnTransportChanged(
805 mid, jsep_transport->rtp_transport(),
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800806 jsep_transport->rtp_dtls_transport(), jsep_transport->media_transport());
Zhi Huangd2248f82018-04-10 14:41:03 -0700807}
808
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700809void JsepTransportController::RemoveTransportForMid(const std::string& mid) {
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800810 bool ret = config_.transport_observer->OnTransportChanged(mid, nullptr,
811 nullptr, nullptr);
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700812 // Calling OnTransportChanged with nullptr should always succeed, since it is
813 // only expected to fail when adding media to a transport (not removing).
814 RTC_DCHECK(ret);
Zhi Huangd2248f82018-04-10 14:41:03 -0700815 mid_to_transport_.erase(mid);
816}
817
Zhi Huange818b6e2018-02-22 15:26:27 -0800818cricket::JsepTransportDescription
819JsepTransportController::CreateJsepTransportDescription(
820 cricket::ContentInfo content_info,
821 cricket::TransportInfo transport_info,
Zhi Huange830e682018-03-30 10:48:35 -0700822 const std::vector<int>& encrypted_extension_ids,
823 int rtp_abs_sendtime_extn_id) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800824 const cricket::MediaContentDescription* content_desc =
825 static_cast<const cricket::MediaContentDescription*>(
826 content_info.description);
827 RTC_DCHECK(content_desc);
828 bool rtcp_mux_enabled = content_info.type == cricket::MediaProtocolType::kSctp
829 ? true
830 : content_desc->rtcp_mux();
831
832 return cricket::JsepTransportDescription(
833 rtcp_mux_enabled, content_desc->cryptos(), encrypted_extension_ids,
Zhi Huange830e682018-03-30 10:48:35 -0700834 rtp_abs_sendtime_extn_id, transport_info.description);
Zhi Huange818b6e2018-02-22 15:26:27 -0800835}
836
837bool JsepTransportController::ShouldUpdateBundleGroup(
838 SdpType type,
839 const cricket::SessionDescription* description) {
840 if (config_.bundle_policy ==
841 PeerConnectionInterface::kBundlePolicyMaxBundle) {
842 return true;
843 }
844
845 if (type != SdpType::kAnswer) {
846 return false;
847 }
848
849 RTC_DCHECK(local_desc_ && remote_desc_);
850 const cricket::ContentGroup* local_bundle =
851 local_desc_->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
852 const cricket::ContentGroup* remote_bundle =
853 remote_desc_->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
854 return local_bundle && remote_bundle;
855}
856
857std::vector<int> JsepTransportController::GetEncryptedHeaderExtensionIds(
858 const cricket::ContentInfo& content_info) {
859 const cricket::MediaContentDescription* content_desc =
860 static_cast<const cricket::MediaContentDescription*>(
861 content_info.description);
862
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700863 if (!config_.crypto_options.srtp.enable_encrypted_rtp_header_extensions) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800864 return std::vector<int>();
865 }
866
867 std::vector<int> encrypted_header_extension_ids;
868 for (auto extension : content_desc->rtp_header_extensions()) {
869 if (!extension.encrypt) {
870 continue;
871 }
872 auto it = std::find(encrypted_header_extension_ids.begin(),
873 encrypted_header_extension_ids.end(), extension.id);
874 if (it == encrypted_header_extension_ids.end()) {
875 encrypted_header_extension_ids.push_back(extension.id);
876 }
877 }
878 return encrypted_header_extension_ids;
879}
880
881std::vector<int>
882JsepTransportController::MergeEncryptedHeaderExtensionIdsForBundle(
883 const cricket::SessionDescription* description) {
884 RTC_DCHECK(description);
885 RTC_DCHECK(bundle_group_);
886
887 std::vector<int> merged_ids;
888 // Union the encrypted header IDs in the group when bundle is enabled.
889 for (const cricket::ContentInfo& content_info : description->contents()) {
890 if (bundle_group_->HasContentName(content_info.name)) {
891 std::vector<int> extension_ids =
892 GetEncryptedHeaderExtensionIds(content_info);
893 for (int id : extension_ids) {
894 auto it = std::find(merged_ids.begin(), merged_ids.end(), id);
895 if (it == merged_ids.end()) {
896 merged_ids.push_back(id);
897 }
898 }
899 }
900 }
901 return merged_ids;
902}
903
Zhi Huange830e682018-03-30 10:48:35 -0700904int JsepTransportController::GetRtpAbsSendTimeHeaderExtensionId(
Zhi Huange818b6e2018-02-22 15:26:27 -0800905 const cricket::ContentInfo& content_info) {
Zhi Huange830e682018-03-30 10:48:35 -0700906 if (!config_.enable_external_auth) {
907 return -1;
Zhi Huange818b6e2018-02-22 15:26:27 -0800908 }
909
910 const cricket::MediaContentDescription* content_desc =
911 static_cast<const cricket::MediaContentDescription*>(
912 content_info.description);
Zhi Huange830e682018-03-30 10:48:35 -0700913
914 const webrtc::RtpExtension* send_time_extension =
915 webrtc::RtpExtension::FindHeaderExtensionByUri(
916 content_desc->rtp_header_extensions(),
917 webrtc::RtpExtension::kAbsSendTimeUri);
918 return send_time_extension ? send_time_extension->id : -1;
919}
920
Zhi Huang365381f2018-04-13 16:44:34 -0700921const cricket::JsepTransport* JsepTransportController::GetJsepTransportForMid(
Zhi Huange830e682018-03-30 10:48:35 -0700922 const std::string& mid) const {
Zhi Huangd2248f82018-04-10 14:41:03 -0700923 auto it = mid_to_transport_.find(mid);
924 return it == mid_to_transport_.end() ? nullptr : it->second;
Zhi Huange830e682018-03-30 10:48:35 -0700925}
926
Zhi Huang365381f2018-04-13 16:44:34 -0700927cricket::JsepTransport* JsepTransportController::GetJsepTransportForMid(
Zhi Huange830e682018-03-30 10:48:35 -0700928 const std::string& mid) {
Zhi Huangd2248f82018-04-10 14:41:03 -0700929 auto it = mid_to_transport_.find(mid);
930 return it == mid_to_transport_.end() ? nullptr : it->second;
Zhi Huange830e682018-03-30 10:48:35 -0700931}
932
Zhi Huang365381f2018-04-13 16:44:34 -0700933const cricket::JsepTransport* JsepTransportController::GetJsepTransportByName(
Zhi Huange830e682018-03-30 10:48:35 -0700934 const std::string& transport_name) const {
935 auto it = jsep_transports_by_name_.find(transport_name);
936 return (it == jsep_transports_by_name_.end()) ? nullptr : it->second.get();
937}
938
Zhi Huang365381f2018-04-13 16:44:34 -0700939cricket::JsepTransport* JsepTransportController::GetJsepTransportByName(
Zhi Huange830e682018-03-30 10:48:35 -0700940 const std::string& transport_name) {
941 auto it = jsep_transports_by_name_.find(transport_name);
942 return (it == jsep_transports_by_name_.end()) ? nullptr : it->second.get();
943}
944
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -0800945std::unique_ptr<webrtc::MediaTransportInterface>
946JsepTransportController::MaybeCreateMediaTransport(
947 const cricket::ContentInfo& content_info,
Anton Sukhanov7940da02018-10-10 10:34:49 -0700948 bool local,
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -0800949 cricket::IceTransportInternal* ice_transport) {
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -0700950 absl::optional<cricket::CryptoParams> selected_crypto_for_media_transport;
951 if (content_info.media_description() &&
952 !content_info.media_description()->cryptos().empty()) {
953 // Order of cryptos is deterministic (rfc4568, 5.1.1), so we just select the
954 // first one (in fact the first one should be the most preferred one.) We
955 // ignore the HMAC size, as media transport crypto settings currently don't
956 // expose HMAC size, nor crypto protocol for that matter.
957 selected_crypto_for_media_transport =
958 content_info.media_description()->cryptos()[0];
959 }
960
Anton Sukhanov7940da02018-10-10 10:34:49 -0700961 if (config_.media_transport_factory != nullptr) {
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -0700962 if (!selected_crypto_for_media_transport.has_value()) {
963 RTC_LOG(LS_WARNING) << "a=cryto line was not found in the offer. Most "
964 "likely you did not enable SDES. "
965 "Make sure to pass config.enable_dtls_srtp=false "
966 "to RTCConfiguration. "
967 "Cannot continue with media transport. Falling "
968 "back to RTP. is_local="
969 << local;
Anton Sukhanov7940da02018-10-10 10:34:49 -0700970
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -0700971 // Remove media_transport_factory from config, because we don't want to
972 // use it on the subsequent call (for the other side of the offer).
973 config_.media_transport_factory = nullptr;
974 } else {
975 // Note that we ignore here lifetime and length.
976 // In fact we take those bits (inline, lifetime and length) and keep it as
977 // part of key derivation.
978 //
979 // Technically, we are also not following rfc4568, which requires us to
980 // send and answer with the key that we chose. In practice, for media
981 // transport, the current approach should be sufficient (we take the key
982 // that sender offered, and caller assumes we will use it. We are not
983 // signaling back that we indeed used it.)
984 std::unique_ptr<rtc::KeyDerivation> key_derivation =
985 rtc::KeyDerivation::Create(rtc::KeyDerivationAlgorithm::HKDF_SHA256);
986 const std::string label = "MediaTransportLabel";
987 constexpr int kDerivedKeyByteSize = 32;
Anton Sukhanov7940da02018-10-10 10:34:49 -0700988
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -0700989 int key_len, salt_len;
990 if (!rtc::GetSrtpKeyAndSaltLengths(
991 rtc::SrtpCryptoSuiteFromName(
992 selected_crypto_for_media_transport.value().cipher_suite),
993 &key_len, &salt_len)) {
994 RTC_CHECK(false) << "Cannot set up secure media transport";
995 }
996 rtc::ZeroOnFreeBuffer<uint8_t> raw_key(key_len + salt_len);
997
998 cricket::SrtpFilter::ParseKeyParams(
999 selected_crypto_for_media_transport.value().key_params,
1000 raw_key.data(), raw_key.size());
1001 absl::optional<rtc::ZeroOnFreeBuffer<uint8_t>> key =
1002 key_derivation->DeriveKey(
1003 raw_key,
1004 /*salt=*/nullptr,
1005 rtc::ArrayView<const uint8_t>(
1006 reinterpret_cast<const uint8_t*>(label.data()), label.size()),
1007 kDerivedKeyByteSize);
1008
1009 // We want to crash the app if we don't have a key, and not silently fall
1010 // back to the unsecure communication.
1011 RTC_CHECK(key.has_value());
1012 MediaTransportSettings settings;
1013 settings.is_caller = local;
1014 settings.pre_shared_key =
1015 std::string(reinterpret_cast<const char*>(key.value().data()),
1016 key.value().size());
Piotr (Peter) Slatala0c022502018-12-28 10:39:39 -08001017 settings.event_log = config_.event_log;
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -07001018 auto media_transport_result =
1019 config_.media_transport_factory->CreateMediaTransport(
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001020 ice_transport, network_thread_, settings);
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -07001021
1022 // TODO(sukhanov): Proper error handling.
1023 RTC_CHECK(media_transport_result.ok());
1024
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001025 return media_transport_result.MoveValue();
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -07001026 }
Anton Sukhanov7940da02018-10-10 10:34:49 -07001027 }
1028
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001029 return nullptr;
1030}
1031
1032RTCError JsepTransportController::MaybeCreateJsepTransport(
1033 bool local,
1034 const cricket::ContentInfo& content_info) {
1035 RTC_DCHECK(network_thread_->IsCurrent());
1036 cricket::JsepTransport* transport = GetJsepTransportByName(content_info.name);
1037 if (transport) {
1038 return RTCError::OK();
1039 }
1040
1041 const cricket::MediaContentDescription* content_desc =
1042 static_cast<const cricket::MediaContentDescription*>(
1043 content_info.description);
1044 if (certificate_ && !content_desc->cryptos().empty()) {
1045 return RTCError(RTCErrorType::INVALID_PARAMETER,
1046 "SDES and DTLS-SRTP cannot be enabled at the same time.");
1047 }
1048
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -08001049 std::unique_ptr<cricket::IceTransportInternal> ice =
1050 CreateIceTransport(content_info.name, /*rtcp=*/false);
1051
1052 std::unique_ptr<MediaTransportInterface> media_transport =
1053 MaybeCreateMediaTransport(content_info, local, ice.get());
1054
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001055 std::unique_ptr<cricket::DtlsTransportInternal> rtp_dtls_transport =
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -08001056 CreateDtlsTransport(std::move(ice));
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001057
1058 std::unique_ptr<cricket::DtlsTransportInternal> rtcp_dtls_transport;
1059 std::unique_ptr<RtpTransport> unencrypted_rtp_transport;
1060 std::unique_ptr<SrtpTransport> sdes_transport;
1061 std::unique_ptr<DtlsSrtpTransport> dtls_srtp_transport;
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001062
1063 if (config_.rtcp_mux_policy !=
1064 PeerConnectionInterface::kRtcpMuxPolicyRequire &&
1065 content_info.type == cricket::MediaProtocolType::kRtp) {
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -08001066 RTC_DCHECK(media_transport == nullptr);
1067 rtcp_dtls_transport = CreateDtlsTransport(
1068 CreateIceTransport(content_info.name, /*rtcp=*/true));
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001069 }
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001070
Anton Sukhanov7940da02018-10-10 10:34:49 -07001071 // TODO(sukhanov): Do not create RTP/RTCP transports if media transport is
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -08001072 // used, and remove the no-op dtls transport when that's done.
Zhi Huange818b6e2018-02-22 15:26:27 -08001073 if (config_.disable_encryption) {
1074 unencrypted_rtp_transport = CreateUnencryptedRtpTransport(
Zhi Huangd2248f82018-04-10 14:41:03 -07001075 content_info.name, rtp_dtls_transport.get(), rtcp_dtls_transport.get());
Zhi Huange818b6e2018-02-22 15:26:27 -08001076 } else if (!content_desc->cryptos().empty()) {
Zhi Huangd2248f82018-04-10 14:41:03 -07001077 sdes_transport = CreateSdesTransport(
1078 content_info.name, rtp_dtls_transport.get(), rtcp_dtls_transport.get());
Zhi Huange818b6e2018-02-22 15:26:27 -08001079 } else {
Zhi Huangd2248f82018-04-10 14:41:03 -07001080 dtls_srtp_transport = CreateDtlsSrtpTransport(
1081 content_info.name, rtp_dtls_transport.get(), rtcp_dtls_transport.get());
Zhi Huange818b6e2018-02-22 15:26:27 -08001082 }
1083
Zhi Huang365381f2018-04-13 16:44:34 -07001084 std::unique_ptr<cricket::JsepTransport> jsep_transport =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001085 absl::make_unique<cricket::JsepTransport>(
Zhi Huangd2248f82018-04-10 14:41:03 -07001086 content_info.name, certificate_, std::move(unencrypted_rtp_transport),
Zhi Huange818b6e2018-02-22 15:26:27 -08001087 std::move(sdes_transport), std::move(dtls_srtp_transport),
Anton Sukhanov7940da02018-10-10 10:34:49 -07001088 std::move(rtp_dtls_transport), std::move(rtcp_dtls_transport),
1089 std::move(media_transport));
Zhi Huange818b6e2018-02-22 15:26:27 -08001090 jsep_transport->SignalRtcpMuxActive.connect(
1091 this, &JsepTransportController::UpdateAggregateStates_n);
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -07001092 jsep_transport->SignalMediaTransportStateChanged.connect(
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001093 this, &JsepTransportController::OnMediaTransportStateChanged_n);
Taylor Brandstettercbaa2542018-04-16 16:42:14 -07001094 SetTransportForMid(content_info.name, jsep_transport.get());
Zhi Huange830e682018-03-30 10:48:35 -07001095
Zhi Huangd2248f82018-04-10 14:41:03 -07001096 jsep_transports_by_name_[content_info.name] = std::move(jsep_transport);
1097 UpdateAggregateStates_n();
Zhi Huange830e682018-03-30 10:48:35 -07001098 return RTCError::OK();
Zhi Huange818b6e2018-02-22 15:26:27 -08001099}
1100
1101void JsepTransportController::MaybeDestroyJsepTransport(
1102 const std::string& mid) {
Zhi Huangd2248f82018-04-10 14:41:03 -07001103 auto jsep_transport = GetJsepTransportByName(mid);
1104 if (!jsep_transport) {
1105 return;
1106 }
1107
1108 // Don't destroy the JsepTransport if there are still media sections referring
1109 // to it.
1110 for (const auto& kv : mid_to_transport_) {
1111 if (kv.second == jsep_transport) {
1112 return;
1113 }
1114 }
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -08001115
Zhi Huange830e682018-03-30 10:48:35 -07001116 jsep_transports_by_name_.erase(mid);
Zhi Huange818b6e2018-02-22 15:26:27 -08001117 UpdateAggregateStates_n();
1118}
1119
1120void JsepTransportController::DestroyAllJsepTransports_n() {
1121 RTC_DCHECK(network_thread_->IsCurrent());
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -08001122
1123 for (const auto& jsep_transport : jsep_transports_by_name_) {
1124 config_.transport_observer->OnTransportChanged(jsep_transport.first,
1125 nullptr, nullptr, nullptr);
1126 }
1127
Zhi Huange830e682018-03-30 10:48:35 -07001128 jsep_transports_by_name_.clear();
Zhi Huange818b6e2018-02-22 15:26:27 -08001129}
1130
1131void JsepTransportController::SetIceRole_n(cricket::IceRole ice_role) {
1132 RTC_DCHECK(network_thread_->IsCurrent());
1133
1134 ice_role_ = ice_role;
1135 for (auto& dtls : GetDtlsTransports()) {
1136 dtls->ice_transport()->SetIceRole(ice_role_);
1137 }
1138}
1139
1140cricket::IceRole JsepTransportController::DetermineIceRole(
Zhi Huang365381f2018-04-13 16:44:34 -07001141 cricket::JsepTransport* jsep_transport,
Zhi Huange818b6e2018-02-22 15:26:27 -08001142 const cricket::TransportInfo& transport_info,
1143 SdpType type,
1144 bool local) {
1145 cricket::IceRole ice_role = ice_role_;
1146 auto tdesc = transport_info.description;
1147 if (local) {
1148 // The initial offer side may use ICE Lite, in which case, per RFC5245
1149 // Section 5.1.1, the answer side should take the controlling role if it is
1150 // in the full ICE mode.
1151 //
1152 // When both sides use ICE Lite, the initial offer side must take the
1153 // controlling role, and this is the default logic implemented in
1154 // SetLocalDescription in JsepTransportController.
1155 if (jsep_transport->remote_description() &&
1156 jsep_transport->remote_description()->transport_desc.ice_mode ==
1157 cricket::ICEMODE_LITE &&
1158 ice_role_ == cricket::ICEROLE_CONTROLLED &&
1159 tdesc.ice_mode == cricket::ICEMODE_FULL) {
1160 ice_role = cricket::ICEROLE_CONTROLLING;
1161 }
1162
1163 // Older versions of Chrome expect the ICE role to be re-determined when an
1164 // ICE restart occurs, and also don't perform conflict resolution correctly,
1165 // so for now we can't safely stop doing this, unless the application opts
1166 // in by setting |config_.redetermine_role_on_ice_restart_| to false. See:
1167 // https://bugs.chromium.org/p/chromium/issues/detail?id=628676
1168 // TODO(deadbeef): Remove this when these old versions of Chrome reach a low
1169 // enough population.
1170 if (config_.redetermine_role_on_ice_restart &&
1171 jsep_transport->local_description() &&
1172 cricket::IceCredentialsChanged(
1173 jsep_transport->local_description()->transport_desc.ice_ufrag,
1174 jsep_transport->local_description()->transport_desc.ice_pwd,
1175 tdesc.ice_ufrag, tdesc.ice_pwd) &&
1176 // Don't change the ICE role if the remote endpoint is ICE lite; we
1177 // should always be controlling in that case.
1178 (!jsep_transport->remote_description() ||
1179 jsep_transport->remote_description()->transport_desc.ice_mode !=
1180 cricket::ICEMODE_LITE)) {
1181 ice_role = (type == SdpType::kOffer) ? cricket::ICEROLE_CONTROLLING
1182 : cricket::ICEROLE_CONTROLLED;
1183 }
1184 } else {
1185 // If our role is cricket::ICEROLE_CONTROLLED and the remote endpoint
1186 // supports only ice_lite, this local endpoint should take the CONTROLLING
1187 // role.
1188 // TODO(deadbeef): This is a session-level attribute, so it really shouldn't
1189 // be in a TransportDescription in the first place...
1190 if (ice_role_ == cricket::ICEROLE_CONTROLLED &&
1191 tdesc.ice_mode == cricket::ICEMODE_LITE) {
1192 ice_role = cricket::ICEROLE_CONTROLLING;
1193 }
1194
1195 // If we use ICE Lite and the remote endpoint uses the full implementation
1196 // of ICE, the local endpoint must take the controlled role, and the other
1197 // side must be the controlling role.
1198 if (jsep_transport->local_description() &&
1199 jsep_transport->local_description()->transport_desc.ice_mode ==
1200 cricket::ICEMODE_LITE &&
1201 ice_role_ == cricket::ICEROLE_CONTROLLING &&
Zhi Huange830e682018-03-30 10:48:35 -07001202 tdesc.ice_mode == cricket::ICEMODE_FULL) {
Zhi Huange818b6e2018-02-22 15:26:27 -08001203 ice_role = cricket::ICEROLE_CONTROLLED;
1204 }
1205 }
1206
1207 return ice_role;
1208}
1209
1210void JsepTransportController::OnTransportWritableState_n(
1211 rtc::PacketTransportInternal* transport) {
1212 RTC_DCHECK(network_thread_->IsCurrent());
1213 RTC_LOG(LS_INFO) << " Transport " << transport->transport_name()
1214 << " writability changed to " << transport->writable()
1215 << ".";
1216 UpdateAggregateStates_n();
1217}
1218
1219void JsepTransportController::OnTransportReceivingState_n(
1220 rtc::PacketTransportInternal* transport) {
1221 RTC_DCHECK(network_thread_->IsCurrent());
1222 UpdateAggregateStates_n();
1223}
1224
1225void JsepTransportController::OnTransportGatheringState_n(
1226 cricket::IceTransportInternal* transport) {
1227 RTC_DCHECK(network_thread_->IsCurrent());
1228 UpdateAggregateStates_n();
1229}
1230
1231void JsepTransportController::OnTransportCandidateGathered_n(
1232 cricket::IceTransportInternal* transport,
1233 const cricket::Candidate& candidate) {
1234 RTC_DCHECK(network_thread_->IsCurrent());
1235
1236 // We should never signal peer-reflexive candidates.
1237 if (candidate.type() == cricket::PRFLX_PORT_TYPE) {
1238 RTC_NOTREACHED();
1239 return;
1240 }
Steve Antond25828a2018-08-31 13:06:05 -07001241 std::string transport_name = transport->transport_name();
1242 invoker_.AsyncInvoke<void>(
1243 RTC_FROM_HERE, signaling_thread_, [this, transport_name, candidate] {
1244 SignalIceCandidatesGathered(transport_name, {candidate});
1245 });
Zhi Huange818b6e2018-02-22 15:26:27 -08001246}
1247
1248void JsepTransportController::OnTransportCandidatesRemoved_n(
1249 cricket::IceTransportInternal* transport,
1250 const cricket::Candidates& candidates) {
1251 invoker_.AsyncInvoke<void>(
1252 RTC_FROM_HERE, signaling_thread_,
Steve Antond25828a2018-08-31 13:06:05 -07001253 [this, candidates] { SignalIceCandidatesRemoved(candidates); });
Zhi Huange818b6e2018-02-22 15:26:27 -08001254}
1255
1256void JsepTransportController::OnTransportRoleConflict_n(
1257 cricket::IceTransportInternal* transport) {
1258 RTC_DCHECK(network_thread_->IsCurrent());
1259 // Note: since the role conflict is handled entirely on the network thread,
1260 // we don't need to worry about role conflicts occurring on two ports at
1261 // once. The first one encountered should immediately reverse the role.
1262 cricket::IceRole reversed_role = (ice_role_ == cricket::ICEROLE_CONTROLLING)
1263 ? cricket::ICEROLE_CONTROLLED
1264 : cricket::ICEROLE_CONTROLLING;
1265 RTC_LOG(LS_INFO) << "Got role conflict; switching to "
1266 << (reversed_role == cricket::ICEROLE_CONTROLLING
1267 ? "controlling"
1268 : "controlled")
1269 << " role.";
1270 SetIceRole_n(reversed_role);
1271}
1272
1273void JsepTransportController::OnTransportStateChanged_n(
1274 cricket::IceTransportInternal* transport) {
1275 RTC_DCHECK(network_thread_->IsCurrent());
1276 RTC_LOG(LS_INFO) << transport->transport_name() << " Transport "
1277 << transport->component()
1278 << " state changed. Check if state is complete.";
1279 UpdateAggregateStates_n();
1280}
1281
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001282void JsepTransportController::OnMediaTransportStateChanged_n() {
1283 SignalMediaTransportStateChanged();
1284 UpdateAggregateStates_n();
1285}
1286
Zhi Huange818b6e2018-02-22 15:26:27 -08001287void JsepTransportController::UpdateAggregateStates_n() {
1288 RTC_DCHECK(network_thread_->IsCurrent());
1289
1290 auto dtls_transports = GetDtlsTransports();
Alex Loiko9289eda2018-11-23 16:18:59 +00001291 cricket::IceConnectionState new_connection_state =
1292 cricket::kIceConnectionConnecting;
Jonas Olsson635474e2018-10-18 15:58:17 +02001293 PeerConnectionInterface::IceConnectionState new_ice_connection_state =
1294 PeerConnectionInterface::IceConnectionState::kIceConnectionNew;
1295 PeerConnectionInterface::PeerConnectionState new_combined_state =
1296 PeerConnectionInterface::PeerConnectionState::kNew;
Zhi Huange818b6e2018-02-22 15:26:27 -08001297 cricket::IceGatheringState new_gathering_state = cricket::kIceGatheringNew;
Alex Loiko9289eda2018-11-23 16:18:59 +00001298 bool any_failed = false;
1299
1300 // TODO(http://bugs.webrtc.org/9719) If(when) media_transport disables
1301 // dtls_transports entirely, the below line will have to be changed to account
1302 // for the fact that dtls transports might be absent.
1303 bool all_connected = !dtls_transports.empty();
1304 bool all_completed = !dtls_transports.empty();
Zhi Huange818b6e2018-02-22 15:26:27 -08001305 bool any_gathering = false;
1306 bool all_done_gathering = !dtls_transports.empty();
Jonas Olsson635474e2018-10-18 15:58:17 +02001307
1308 std::map<IceTransportState, int> ice_state_counts;
1309 std::map<cricket::DtlsTransportState, int> dtls_state_counts;
1310
Zhi Huange818b6e2018-02-22 15:26:27 -08001311 for (const auto& dtls : dtls_transports) {
Alex Loiko9289eda2018-11-23 16:18:59 +00001312 any_failed = any_failed || dtls->ice_transport()->GetState() ==
1313 cricket::IceTransportState::STATE_FAILED;
1314 all_connected = all_connected && dtls->writable();
1315 all_completed =
1316 all_completed && dtls->writable() &&
1317 dtls->ice_transport()->GetState() ==
1318 cricket::IceTransportState::STATE_COMPLETED &&
1319 dtls->ice_transport()->GetIceRole() == cricket::ICEROLE_CONTROLLING &&
1320 dtls->ice_transport()->gathering_state() ==
1321 cricket::kIceGatheringComplete;
Zhi Huange818b6e2018-02-22 15:26:27 -08001322 any_gathering = any_gathering || dtls->ice_transport()->gathering_state() !=
1323 cricket::kIceGatheringNew;
1324 all_done_gathering =
1325 all_done_gathering && dtls->ice_transport()->gathering_state() ==
1326 cricket::kIceGatheringComplete;
Jonas Olsson635474e2018-10-18 15:58:17 +02001327
1328 dtls_state_counts[dtls->dtls_state()]++;
1329 ice_state_counts[dtls->ice_transport()->GetIceTransportState()]++;
Zhi Huange818b6e2018-02-22 15:26:27 -08001330 }
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -07001331
Alex Loiko9289eda2018-11-23 16:18:59 +00001332 for (auto it = jsep_transports_by_name_.begin();
1333 it != jsep_transports_by_name_.end(); ++it) {
1334 auto jsep_transport = it->second.get();
1335 if (!jsep_transport->media_transport()) {
1336 continue;
1337 }
1338
1339 // There is no 'kIceConnectionDisconnected', so we only need to handle
1340 // connected and completed.
1341 // We treat kClosed as failed, because if it happens before shutting down
1342 // media transports it means that there was a failure.
1343 // MediaTransportInterface allows to flip back and forth between kWritable
1344 // and kPending, but there does not exist an implementation that does that,
1345 // and the contract of jsep transport controller doesn't quite expect that.
1346 // When this happens, we would go from connected to connecting state, but
1347 // this may change in future.
1348 any_failed |= jsep_transport->media_transport_state() ==
1349 webrtc::MediaTransportState::kClosed;
1350 all_completed &= jsep_transport->media_transport_state() ==
1351 webrtc::MediaTransportState::kWritable;
1352 all_connected &= jsep_transport->media_transport_state() ==
1353 webrtc::MediaTransportState::kWritable;
1354 }
1355
1356 if (any_failed) {
1357 new_connection_state = cricket::kIceConnectionFailed;
1358 } else if (all_completed) {
1359 new_connection_state = cricket::kIceConnectionCompleted;
1360 } else if (all_connected) {
1361 new_connection_state = cricket::kIceConnectionConnected;
1362 }
1363 if (ice_connection_state_ != new_connection_state) {
1364 ice_connection_state_ = new_connection_state;
1365 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
1366 [this, new_connection_state] {
1367 SignalIceConnectionState(new_connection_state);
1368 });
1369 }
1370
Jonas Olsson635474e2018-10-18 15:58:17 +02001371 // Compute the current RTCIceConnectionState as described in
1372 // https://www.w3.org/TR/webrtc/#dom-rtciceconnectionstate.
1373 // The PeerConnection is responsible for handling the "closed" state.
1374 int total_ice_checking = ice_state_counts[IceTransportState::kChecking];
1375 int total_ice_connected = ice_state_counts[IceTransportState::kConnected];
1376 int total_ice_completed = ice_state_counts[IceTransportState::kCompleted];
1377 int total_ice_failed = ice_state_counts[IceTransportState::kFailed];
1378 int total_ice_disconnected =
1379 ice_state_counts[IceTransportState::kDisconnected];
1380 int total_ice_closed = ice_state_counts[IceTransportState::kClosed];
1381 int total_ice_new = ice_state_counts[IceTransportState::kNew];
1382 int total_ice = dtls_transports.size();
1383
1384 if (total_ice_failed > 0) {
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001385 // Any RTCIceTransports are in the "failed" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001386 new_ice_connection_state = PeerConnectionInterface::kIceConnectionFailed;
Alex Loiko9289eda2018-11-23 16:18:59 +00001387 } else if (total_ice_disconnected > 0) {
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001388 // None of the previous states apply and any RTCIceTransports are in the
1389 // "disconnected" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001390 new_ice_connection_state =
1391 PeerConnectionInterface::kIceConnectionDisconnected;
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001392 } else if (total_ice_new + total_ice_closed == total_ice) {
1393 // None of the previous states apply and all RTCIceTransports are in the
1394 // "new" or "closed" state, or there are no transports.
1395 new_ice_connection_state = PeerConnectionInterface::kIceConnectionNew;
1396 } else if (total_ice_new + total_ice_checking > 0) {
1397 // None of the previous states apply and any RTCIceTransports are in the
1398 // "new" or "checking" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001399 new_ice_connection_state = PeerConnectionInterface::kIceConnectionChecking;
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001400 } else if (total_ice_completed + total_ice_closed == total_ice) {
1401 // None of the previous states apply and all RTCIceTransports are in the
1402 // "completed" or "closed" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001403 new_ice_connection_state = PeerConnectionInterface::kIceConnectionCompleted;
1404 } else if (total_ice_connected + total_ice_completed + total_ice_closed ==
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001405 total_ice) {
1406 // None of the previous states apply and all RTCIceTransports are in the
1407 // "connected", "completed" or "closed" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001408 new_ice_connection_state = PeerConnectionInterface::kIceConnectionConnected;
Jonas Olsson635474e2018-10-18 15:58:17 +02001409 } else {
1410 RTC_NOTREACHED();
1411 }
1412
Alex Loiko9289eda2018-11-23 16:18:59 +00001413 if (standardized_ice_connection_state_ != new_ice_connection_state) {
1414 standardized_ice_connection_state_ = new_ice_connection_state;
Jonas Olsson635474e2018-10-18 15:58:17 +02001415 invoker_.AsyncInvoke<void>(
1416 RTC_FROM_HERE, signaling_thread_, [this, new_ice_connection_state] {
Alex Loiko9289eda2018-11-23 16:18:59 +00001417 SignalStandardizedIceConnectionState(new_ice_connection_state);
Jonas Olsson635474e2018-10-18 15:58:17 +02001418 });
1419 }
1420
1421 // Compute the current RTCPeerConnectionState as described in
1422 // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnectionstate.
1423 // The PeerConnection is responsible for handling the "closed" state.
1424 // Note that "connecting" is only a valid state for DTLS transports while
1425 // "checking", "completed" and "disconnected" are only valid for ICE
1426 // transports.
1427 int total_connected = total_ice_connected +
1428 dtls_state_counts[cricket::DTLS_TRANSPORT_CONNECTED];
1429 int total_dtls_connecting =
1430 dtls_state_counts[cricket::DTLS_TRANSPORT_CONNECTING];
1431 int total_failed =
1432 total_ice_failed + dtls_state_counts[cricket::DTLS_TRANSPORT_FAILED];
1433 int total_closed =
1434 total_ice_closed + dtls_state_counts[cricket::DTLS_TRANSPORT_CLOSED];
1435 int total_new =
1436 total_ice_new + dtls_state_counts[cricket::DTLS_TRANSPORT_NEW];
1437 int total_transports = total_ice * 2;
1438
1439 if (total_failed > 0) {
1440 // Any of the RTCIceTransports or RTCDtlsTransports are in a "failed" state.
1441 new_combined_state = PeerConnectionInterface::PeerConnectionState::kFailed;
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001442 } else if (total_ice_disconnected > 0) {
1443 // None of the previous states apply and any RTCIceTransports or
1444 // RTCDtlsTransports are in the "disconnected" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001445 new_combined_state =
1446 PeerConnectionInterface::PeerConnectionState::kDisconnected;
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001447 } else if (total_new + total_closed == total_transports) {
1448 // None of the previous states apply and all RTCIceTransports and
1449 // RTCDtlsTransports are in the "new" or "closed" state, or there are no
1450 // transports.
1451 new_combined_state = PeerConnectionInterface::PeerConnectionState::kNew;
1452 } else if (total_new + total_dtls_connecting + total_ice_checking > 0) {
1453 // None of the previous states apply and all RTCIceTransports or
1454 // RTCDtlsTransports are in the "new", "connecting" or "checking" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001455 new_combined_state =
1456 PeerConnectionInterface::PeerConnectionState::kConnecting;
1457 } else if (total_connected + total_ice_completed + total_closed ==
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001458 total_transports) {
1459 // None of the previous states apply and all RTCIceTransports and
1460 // RTCDtlsTransports are in the "connected", "completed" or "closed" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001461 new_combined_state =
1462 PeerConnectionInterface::PeerConnectionState::kConnected;
Jonas Olsson635474e2018-10-18 15:58:17 +02001463 } else {
1464 RTC_NOTREACHED();
1465 }
1466
1467 if (combined_connection_state_ != new_combined_state) {
1468 combined_connection_state_ = new_combined_state;
1469 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
1470 [this, new_combined_state] {
1471 SignalConnectionState(new_combined_state);
1472 });
1473 }
1474
Zhi Huange818b6e2018-02-22 15:26:27 -08001475 if (all_done_gathering) {
1476 new_gathering_state = cricket::kIceGatheringComplete;
1477 } else if (any_gathering) {
1478 new_gathering_state = cricket::kIceGatheringGathering;
1479 }
1480 if (ice_gathering_state_ != new_gathering_state) {
1481 ice_gathering_state_ = new_gathering_state;
Steve Antond25828a2018-08-31 13:06:05 -07001482 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
1483 [this, new_gathering_state] {
1484 SignalIceGatheringState(new_gathering_state);
1485 });
Zhi Huange818b6e2018-02-22 15:26:27 -08001486 }
1487}
1488
1489void JsepTransportController::OnDtlsHandshakeError(
1490 rtc::SSLHandshakeError error) {
1491 SignalDtlsHandshakeError(error);
1492}
1493
1494} // namespace webrtc