blob: 55f1d1cfdbc6d8c388d1a1480240ca4d696a7a04 [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
Zhi Huange818b6e2018-02-22 15:26:27 -080013#include <memory>
14#include <utility>
15
Steve Anton64b626b2019-01-28 17:25:26 -080016#include "absl/algorithm/container.h"
Zach Steinc64078f2018-11-27 15:53:01 -080017#include "absl/memory/memory.h"
Anton Sukhanov316f3ac2019-05-23 15:50:38 -070018#include "api/datagram_transport_interface.h"
19#include "api/media_transport_interface.h"
20#include "p2p/base/datagram_dtls_adaptor.h"
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -080021#include "p2p/base/ice_transport_internal.h"
22#include "p2p/base/no_op_dtls_transport.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080023#include "p2p/base/port.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "pc/srtp_filter.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080025#include "rtc_base/bind.h"
26#include "rtc_base/checks.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080027#include "rtc_base/thread.h"
28
29using webrtc::SdpType;
30
31namespace {
32
Zhi Huange818b6e2018-02-22 15:26:27 -080033webrtc::RTCError VerifyCandidate(const cricket::Candidate& cand) {
34 // No address zero.
35 if (cand.address().IsNil() || cand.address().IsAnyIP()) {
36 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
37 "candidate has address of zero");
38 }
39
40 // Disallow all ports below 1024, except for 80 and 443 on public addresses.
41 int port = cand.address().port();
42 if (cand.protocol() == cricket::TCP_PROTOCOL_NAME &&
43 (cand.tcptype() == cricket::TCPTYPE_ACTIVE_STR || port == 0)) {
44 // Expected for active-only candidates per
45 // http://tools.ietf.org/html/rfc6544#section-4.5 so no error.
46 // Libjingle clients emit port 0, in "active" mode.
47 return webrtc::RTCError::OK();
48 }
49 if (port < 1024) {
50 if ((port != 80) && (port != 443)) {
51 return webrtc::RTCError(
52 webrtc::RTCErrorType::INVALID_PARAMETER,
53 "candidate has port below 1024, but not 80 or 443");
54 }
55
56 if (cand.address().IsPrivateIP()) {
57 return webrtc::RTCError(
58 webrtc::RTCErrorType::INVALID_PARAMETER,
59 "candidate has port of 80 or 443 with private IP address");
60 }
61 }
62
63 return webrtc::RTCError::OK();
64}
65
66webrtc::RTCError VerifyCandidates(const cricket::Candidates& candidates) {
67 for (const cricket::Candidate& candidate : candidates) {
68 webrtc::RTCError error = VerifyCandidate(candidate);
69 if (!error.ok()) {
70 return error;
71 }
72 }
73 return webrtc::RTCError::OK();
74}
75
76} // namespace
77
78namespace webrtc {
79
80JsepTransportController::JsepTransportController(
81 rtc::Thread* signaling_thread,
82 rtc::Thread* network_thread,
83 cricket::PortAllocator* port_allocator,
Zach Steine20867f2018-08-02 13:20:15 -070084 AsyncResolverFactory* async_resolver_factory,
Zhi Huange818b6e2018-02-22 15:26:27 -080085 Config config)
86 : signaling_thread_(signaling_thread),
87 network_thread_(network_thread),
88 port_allocator_(port_allocator),
Zach Steine20867f2018-08-02 13:20:15 -070089 async_resolver_factory_(async_resolver_factory),
Zhi Huang365381f2018-04-13 16:44:34 -070090 config_(config) {
91 // The |transport_observer| is assumed to be non-null.
92 RTC_DCHECK(config_.transport_observer);
93}
Zhi Huange818b6e2018-02-22 15:26:27 -080094
95JsepTransportController::~JsepTransportController() {
96 // Channel destructors may try to send packets, so this needs to happen on
97 // the network thread.
98 network_thread_->Invoke<void>(
99 RTC_FROM_HERE,
100 rtc::Bind(&JsepTransportController::DestroyAllJsepTransports_n, this));
101}
102
103RTCError JsepTransportController::SetLocalDescription(
104 SdpType type,
105 const cricket::SessionDescription* description) {
106 if (!network_thread_->IsCurrent()) {
107 return network_thread_->Invoke<RTCError>(
108 RTC_FROM_HERE, [=] { return SetLocalDescription(type, description); });
109 }
110
111 if (!initial_offerer_.has_value()) {
112 initial_offerer_.emplace(type == SdpType::kOffer);
113 if (*initial_offerer_) {
114 SetIceRole_n(cricket::ICEROLE_CONTROLLING);
115 } else {
116 SetIceRole_n(cricket::ICEROLE_CONTROLLED);
117 }
118 }
119 return ApplyDescription_n(/*local=*/true, type, description);
120}
121
122RTCError JsepTransportController::SetRemoteDescription(
123 SdpType type,
124 const cricket::SessionDescription* description) {
125 if (!network_thread_->IsCurrent()) {
126 return network_thread_->Invoke<RTCError>(
127 RTC_FROM_HERE, [=] { return SetRemoteDescription(type, description); });
128 }
129
130 return ApplyDescription_n(/*local=*/false, type, description);
131}
132
133RtpTransportInternal* JsepTransportController::GetRtpTransport(
134 const std::string& mid) const {
Zhi Huange830e682018-03-30 10:48:35 -0700135 auto jsep_transport = GetJsepTransportForMid(mid);
Zhi Huange818b6e2018-02-22 15:26:27 -0800136 if (!jsep_transport) {
137 return nullptr;
138 }
139 return jsep_transport->rtp_transport();
140}
141
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700142MediaTransportConfig JsepTransportController::GetMediaTransportConfig(
Anton Sukhanov7940da02018-10-10 10:34:49 -0700143 const std::string& mid) const {
144 auto jsep_transport = GetJsepTransportForMid(mid);
145 if (!jsep_transport) {
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700146 return MediaTransportConfig();
147 }
148
149 MediaTransportInterface* media_transport = nullptr;
150 if (config_.use_media_transport_for_media) {
151 media_transport = jsep_transport->media_transport();
152 }
153
154 DatagramTransportInterface* datagram_transport =
155 jsep_transport->datagram_transport();
156
157 // Media transport and datagram transports can not be used together.
158 RTC_DCHECK(!media_transport || !datagram_transport);
159
160 if (media_transport) {
161 return MediaTransportConfig(media_transport);
162 } else if (datagram_transport) {
163 return MediaTransportConfig(
164 /*rtp_max_packet_size=*/datagram_transport->GetLargestDatagramSize());
165 } else {
166 return MediaTransportConfig();
167 }
168}
169
170MediaTransportInterface*
171JsepTransportController::GetMediaTransportForDataChannel(
172 const std::string& mid) const {
173 auto jsep_transport = GetJsepTransportForMid(mid);
174 if (!jsep_transport || !config_.use_media_transport_for_data_channels) {
Anton Sukhanov7940da02018-10-10 10:34:49 -0700175 return nullptr;
176 }
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700177
Anton Sukhanov7940da02018-10-10 10:34:49 -0700178 return jsep_transport->media_transport();
179}
180
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800181MediaTransportState JsepTransportController::GetMediaTransportState(
182 const std::string& mid) const {
183 auto jsep_transport = GetJsepTransportForMid(mid);
184 if (!jsep_transport) {
185 return MediaTransportState::kPending;
186 }
187 return jsep_transport->media_transport_state();
188}
189
Zhi Huange818b6e2018-02-22 15:26:27 -0800190cricket::DtlsTransportInternal* JsepTransportController::GetDtlsTransport(
Harald Alvestrandad88c882018-11-28 16:47:46 +0100191 const std::string& mid) {
Zhi Huange830e682018-03-30 10:48:35 -0700192 auto jsep_transport = GetJsepTransportForMid(mid);
Zhi Huange818b6e2018-02-22 15:26:27 -0800193 if (!jsep_transport) {
194 return nullptr;
195 }
196 return jsep_transport->rtp_dtls_transport();
197}
198
Harald Alvestrandad88c882018-11-28 16:47:46 +0100199const cricket::DtlsTransportInternal*
200JsepTransportController::GetRtcpDtlsTransport(const std::string& mid) const {
Zhi Huange830e682018-03-30 10:48:35 -0700201 auto jsep_transport = GetJsepTransportForMid(mid);
Zhi Huange818b6e2018-02-22 15:26:27 -0800202 if (!jsep_transport) {
203 return nullptr;
204 }
205 return jsep_transport->rtcp_dtls_transport();
206}
207
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +0100208rtc::scoped_refptr<webrtc::DtlsTransport>
Harald Alvestrandad88c882018-11-28 16:47:46 +0100209JsepTransportController::LookupDtlsTransportByMid(const std::string& mid) {
210 auto jsep_transport = GetJsepTransportForMid(mid);
211 if (!jsep_transport) {
212 return nullptr;
213 }
214 return jsep_transport->RtpDtlsTransport();
215}
216
Zhi Huange818b6e2018-02-22 15:26:27 -0800217void JsepTransportController::SetIceConfig(const cricket::IceConfig& config) {
218 if (!network_thread_->IsCurrent()) {
219 network_thread_->Invoke<void>(RTC_FROM_HERE, [&] { SetIceConfig(config); });
220 return;
221 }
222
223 ice_config_ = config;
224 for (auto& dtls : GetDtlsTransports()) {
225 dtls->ice_transport()->SetIceConfig(ice_config_);
226 }
227}
228
229void JsepTransportController::SetNeedsIceRestartFlag() {
Zhi Huange830e682018-03-30 10:48:35 -0700230 for (auto& kv : jsep_transports_by_name_) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800231 kv.second->SetNeedsIceRestartFlag();
232 }
233}
234
235bool JsepTransportController::NeedsIceRestart(
236 const std::string& transport_name) const {
Zhi Huang365381f2018-04-13 16:44:34 -0700237 const cricket::JsepTransport* transport =
Zhi Huange830e682018-03-30 10:48:35 -0700238 GetJsepTransportByName(transport_name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800239 if (!transport) {
240 return false;
241 }
242 return transport->needs_ice_restart();
243}
244
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200245absl::optional<rtc::SSLRole> JsepTransportController::GetDtlsRole(
Zhi Huange830e682018-03-30 10:48:35 -0700246 const std::string& mid) const {
Zhi Huange818b6e2018-02-22 15:26:27 -0800247 if (!network_thread_->IsCurrent()) {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200248 return network_thread_->Invoke<absl::optional<rtc::SSLRole>>(
Zhi Huange830e682018-03-30 10:48:35 -0700249 RTC_FROM_HERE, [&] { return GetDtlsRole(mid); });
Zhi Huange818b6e2018-02-22 15:26:27 -0800250 }
251
Zhi Huang365381f2018-04-13 16:44:34 -0700252 const cricket::JsepTransport* t = GetJsepTransportForMid(mid);
Zhi Huange818b6e2018-02-22 15:26:27 -0800253 if (!t) {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200254 return absl::optional<rtc::SSLRole>();
Zhi Huange818b6e2018-02-22 15:26:27 -0800255 }
256 return t->GetDtlsRole();
257}
258
259bool JsepTransportController::SetLocalCertificate(
260 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
261 if (!network_thread_->IsCurrent()) {
262 return network_thread_->Invoke<bool>(
263 RTC_FROM_HERE, [&] { return SetLocalCertificate(certificate); });
264 }
265
266 // Can't change a certificate, or set a null certificate.
267 if (certificate_ || !certificate) {
268 return false;
269 }
270 certificate_ = certificate;
271
272 // Set certificate for JsepTransport, which verifies it matches the
273 // fingerprint in SDP, and DTLS transport.
274 // Fallback from DTLS to SDES is not supported.
Zhi Huange830e682018-03-30 10:48:35 -0700275 for (auto& kv : jsep_transports_by_name_) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800276 kv.second->SetLocalCertificate(certificate_);
277 }
278 for (auto& dtls : GetDtlsTransports()) {
279 bool set_cert_success = dtls->SetLocalCertificate(certificate_);
280 RTC_DCHECK(set_cert_success);
281 }
282 return true;
283}
284
285rtc::scoped_refptr<rtc::RTCCertificate>
286JsepTransportController::GetLocalCertificate(
287 const std::string& transport_name) const {
288 if (!network_thread_->IsCurrent()) {
289 return network_thread_->Invoke<rtc::scoped_refptr<rtc::RTCCertificate>>(
290 RTC_FROM_HERE, [&] { return GetLocalCertificate(transport_name); });
291 }
292
Zhi Huang365381f2018-04-13 16:44:34 -0700293 const cricket::JsepTransport* t = GetJsepTransportByName(transport_name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800294 if (!t) {
295 return nullptr;
296 }
297 return t->GetLocalCertificate();
298}
299
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800300std::unique_ptr<rtc::SSLCertChain>
301JsepTransportController::GetRemoteSSLCertChain(
Zhi Huange818b6e2018-02-22 15:26:27 -0800302 const std::string& transport_name) const {
303 if (!network_thread_->IsCurrent()) {
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800304 return network_thread_->Invoke<std::unique_ptr<rtc::SSLCertChain>>(
305 RTC_FROM_HERE, [&] { return GetRemoteSSLCertChain(transport_name); });
Zhi Huange818b6e2018-02-22 15:26:27 -0800306 }
307
Zhi Huange830e682018-03-30 10:48:35 -0700308 // Get the certificate from the RTP transport's DTLS handshake. Should be
309 // identical to the RTCP transport's, since they were given the same remote
Zhi Huange818b6e2018-02-22 15:26:27 -0800310 // fingerprint.
Zhi Huange830e682018-03-30 10:48:35 -0700311 auto jsep_transport = GetJsepTransportByName(transport_name);
312 if (!jsep_transport) {
313 return nullptr;
314 }
315 auto dtls = jsep_transport->rtp_dtls_transport();
Zhi Huange818b6e2018-02-22 15:26:27 -0800316 if (!dtls) {
317 return nullptr;
318 }
319
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800320 return dtls->GetRemoteSSLCertChain();
Zhi Huange818b6e2018-02-22 15:26:27 -0800321}
322
323void JsepTransportController::MaybeStartGathering() {
324 if (!network_thread_->IsCurrent()) {
325 network_thread_->Invoke<void>(RTC_FROM_HERE,
326 [&] { MaybeStartGathering(); });
327 return;
328 }
329
330 for (auto& dtls : GetDtlsTransports()) {
331 dtls->ice_transport()->MaybeStartGathering();
332 }
333}
334
335RTCError JsepTransportController::AddRemoteCandidates(
336 const std::string& transport_name,
337 const cricket::Candidates& candidates) {
338 if (!network_thread_->IsCurrent()) {
339 return network_thread_->Invoke<RTCError>(RTC_FROM_HERE, [&] {
340 return AddRemoteCandidates(transport_name, candidates);
341 });
342 }
343
344 // Verify each candidate before passing down to the transport layer.
345 RTCError error = VerifyCandidates(candidates);
346 if (!error.ok()) {
347 return error;
348 }
Zhi Huange830e682018-03-30 10:48:35 -0700349 auto jsep_transport = GetJsepTransportByName(transport_name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800350 if (!jsep_transport) {
Zhi Huange830e682018-03-30 10:48:35 -0700351 RTC_LOG(LS_WARNING) << "Not adding candidate because the JsepTransport "
352 "doesn't exist. Ignore it.";
353 return RTCError::OK();
Zhi Huange818b6e2018-02-22 15:26:27 -0800354 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800355 return jsep_transport->AddRemoteCandidates(candidates);
356}
357
358RTCError JsepTransportController::RemoveRemoteCandidates(
359 const cricket::Candidates& candidates) {
360 if (!network_thread_->IsCurrent()) {
361 return network_thread_->Invoke<RTCError>(
362 RTC_FROM_HERE, [&] { return RemoveRemoteCandidates(candidates); });
363 }
364
365 // Verify each candidate before passing down to the transport layer.
366 RTCError error = VerifyCandidates(candidates);
367 if (!error.ok()) {
368 return error;
369 }
370
371 std::map<std::string, cricket::Candidates> candidates_by_transport_name;
372 for (const cricket::Candidate& cand : candidates) {
373 if (!cand.transport_name().empty()) {
374 candidates_by_transport_name[cand.transport_name()].push_back(cand);
375 } else {
376 RTC_LOG(LS_ERROR) << "Not removing candidate because it does not have a "
377 "transport name set: "
378 << cand.ToString();
379 }
380 }
381
382 for (const auto& kv : candidates_by_transport_name) {
383 const std::string& transport_name = kv.first;
384 const cricket::Candidates& candidates = kv.second;
Zhi Huang365381f2018-04-13 16:44:34 -0700385 cricket::JsepTransport* jsep_transport =
Zhi Huange830e682018-03-30 10:48:35 -0700386 GetJsepTransportByName(transport_name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800387 if (!jsep_transport) {
Zhi Huange830e682018-03-30 10:48:35 -0700388 RTC_LOG(LS_WARNING)
389 << "Not removing candidate because the JsepTransport doesn't exist.";
390 continue;
Zhi Huange818b6e2018-02-22 15:26:27 -0800391 }
392 for (const cricket::Candidate& candidate : candidates) {
Harald Alvestrandad88c882018-11-28 16:47:46 +0100393 cricket::DtlsTransportInternal* dtls =
394 candidate.component() == cricket::ICE_CANDIDATE_COMPONENT_RTP
395 ? jsep_transport->rtp_dtls_transport()
396 : jsep_transport->rtcp_dtls_transport();
Zhi Huange818b6e2018-02-22 15:26:27 -0800397 if (dtls) {
398 dtls->ice_transport()->RemoveRemoteCandidate(candidate);
399 }
400 }
401 }
402 return RTCError::OK();
403}
404
405bool JsepTransportController::GetStats(const std::string& transport_name,
406 cricket::TransportStats* stats) {
407 if (!network_thread_->IsCurrent()) {
408 return network_thread_->Invoke<bool>(
409 RTC_FROM_HERE, [=] { return GetStats(transport_name, stats); });
410 }
411
Zhi Huang365381f2018-04-13 16:44:34 -0700412 cricket::JsepTransport* transport = GetJsepTransportByName(transport_name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800413 if (!transport) {
414 return false;
415 }
416 return transport->GetStats(stats);
417}
418
Zhi Huangb57e1692018-06-12 11:41:11 -0700419void JsepTransportController::SetActiveResetSrtpParams(
420 bool active_reset_srtp_params) {
421 if (!network_thread_->IsCurrent()) {
422 network_thread_->Invoke<void>(RTC_FROM_HERE, [=] {
423 SetActiveResetSrtpParams(active_reset_srtp_params);
424 });
425 return;
426 }
427
428 RTC_LOG(INFO)
429 << "Updating the active_reset_srtp_params for JsepTransportController: "
430 << active_reset_srtp_params;
431 config_.active_reset_srtp_params = active_reset_srtp_params;
432 for (auto& kv : jsep_transports_by_name_) {
433 kv.second->SetActiveResetSrtpParams(active_reset_srtp_params);
434 }
435}
436
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -0800437void JsepTransportController::SetMediaTransportSettings(
438 bool use_media_transport_for_media,
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700439 bool use_media_transport_for_data_channels,
440 bool use_datagram_transport) {
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -0800441 RTC_DCHECK(use_media_transport_for_media ==
442 config_.use_media_transport_for_media ||
Piotr (Peter) Slatala97fc11f2018-10-18 12:57:59 -0700443 jsep_transports_by_name_.empty())
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -0800444 << "You can only change media transport configuration before creating "
445 "the first transport.";
446
447 RTC_DCHECK(use_media_transport_for_data_channels ==
448 config_.use_media_transport_for_data_channels ||
449 jsep_transports_by_name_.empty())
450 << "You can only change media transport configuration before creating "
451 "the first transport.";
452
453 config_.use_media_transport_for_media = use_media_transport_for_media;
454 config_.use_media_transport_for_data_channels =
455 use_media_transport_for_data_channels;
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700456 config_.use_datagram_transport = use_datagram_transport;
Piotr (Peter) Slatala97fc11f2018-10-18 12:57:59 -0700457}
458
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -0800459std::unique_ptr<cricket::IceTransportInternal>
460JsepTransportController::CreateIceTransport(const std::string transport_name,
461 bool rtcp) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800462 int component = rtcp ? cricket::ICE_CANDIDATE_COMPONENT_RTCP
463 : cricket::ICE_CANDIDATE_COMPONENT_RTP;
464
Zhi Huange818b6e2018-02-22 15:26:27 -0800465 if (config_.external_transport_factory) {
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -0800466 return config_.external_transport_factory->CreateIceTransport(
Zhi Huange818b6e2018-02-22 15:26:27 -0800467 transport_name, component);
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -0800468 } else {
469 return absl::make_unique<cricket::P2PTransportChannel>(
470 transport_name, component, port_allocator_, async_resolver_factory_,
471 config_.event_log);
472 }
473}
474
475std::unique_ptr<cricket::DtlsTransportInternal>
476JsepTransportController::CreateDtlsTransport(
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700477 std::unique_ptr<cricket::IceTransportInternal> ice,
478 std::unique_ptr<DatagramTransportInterface> datagram_transport) {
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -0800479 RTC_DCHECK(network_thread_->IsCurrent());
480
481 std::unique_ptr<cricket::DtlsTransportInternal> dtls;
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700482
483 if (datagram_transport) {
484 RTC_DCHECK(config_.use_datagram_transport);
485
486 // Create DTLS wrapper around DatagramTransportInterface.
487 dtls = absl::make_unique<cricket::DatagramDtlsAdaptor>(
488 std::move(ice), std::move(datagram_transport), config_.crypto_options,
489 config_.event_log);
490 } else if (config_.media_transport_factory &&
491 config_.use_media_transport_for_media &&
492 config_.use_media_transport_for_data_channels) {
493 // If media transport is used for both media and data channels,
494 // then we don't need to create DTLS.
495 // Otherwise, DTLS is still created.
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -0800496 dtls = absl::make_unique<cricket::NoOpDtlsTransport>(
497 std::move(ice), config_.crypto_options);
498 } else if (config_.external_transport_factory) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800499 dtls = config_.external_transport_factory->CreateDtlsTransport(
500 std::move(ice), config_.crypto_options);
501 } else {
Zach Steinc64078f2018-11-27 15:53:01 -0800502 dtls = absl::make_unique<cricket::DtlsTransport>(
503 std::move(ice), config_.crypto_options, config_.event_log);
Zhi Huange818b6e2018-02-22 15:26:27 -0800504 }
505
506 RTC_DCHECK(dtls);
507 dtls->SetSslMaxProtocolVersion(config_.ssl_max_version);
Zhi Huange818b6e2018-02-22 15:26:27 -0800508 dtls->ice_transport()->SetIceRole(ice_role_);
509 dtls->ice_transport()->SetIceTiebreaker(ice_tiebreaker_);
510 dtls->ice_transport()->SetIceConfig(ice_config_);
511 if (certificate_) {
512 bool set_cert_success = dtls->SetLocalCertificate(certificate_);
513 RTC_DCHECK(set_cert_success);
514 }
515
516 // Connect to signals offered by the DTLS and ICE transport.
517 dtls->SignalWritableState.connect(
518 this, &JsepTransportController::OnTransportWritableState_n);
519 dtls->SignalReceivingState.connect(
520 this, &JsepTransportController::OnTransportReceivingState_n);
521 dtls->SignalDtlsHandshakeError.connect(
522 this, &JsepTransportController::OnDtlsHandshakeError);
523 dtls->ice_transport()->SignalGatheringState.connect(
524 this, &JsepTransportController::OnTransportGatheringState_n);
525 dtls->ice_transport()->SignalCandidateGathered.connect(
526 this, &JsepTransportController::OnTransportCandidateGathered_n);
527 dtls->ice_transport()->SignalCandidatesRemoved.connect(
528 this, &JsepTransportController::OnTransportCandidatesRemoved_n);
529 dtls->ice_transport()->SignalRoleConflict.connect(
530 this, &JsepTransportController::OnTransportRoleConflict_n);
531 dtls->ice_transport()->SignalStateChanged.connect(
532 this, &JsepTransportController::OnTransportStateChanged_n);
Jonas Olsson7a6739e2019-01-15 16:31:55 +0100533 dtls->ice_transport()->SignalIceTransportStateChanged.connect(
534 this, &JsepTransportController::OnTransportStateChanged_n);
Zhi Huange818b6e2018-02-22 15:26:27 -0800535 return dtls;
536}
537
538std::unique_ptr<webrtc::RtpTransport>
539JsepTransportController::CreateUnencryptedRtpTransport(
540 const std::string& transport_name,
541 rtc::PacketTransportInternal* rtp_packet_transport,
542 rtc::PacketTransportInternal* rtcp_packet_transport) {
543 RTC_DCHECK(network_thread_->IsCurrent());
Zhi Huange830e682018-03-30 10:48:35 -0700544 auto unencrypted_rtp_transport =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200545 absl::make_unique<RtpTransport>(rtcp_packet_transport == nullptr);
Zhi Huange830e682018-03-30 10:48:35 -0700546 unencrypted_rtp_transport->SetRtpPacketTransport(rtp_packet_transport);
547 if (rtcp_packet_transport) {
548 unencrypted_rtp_transport->SetRtcpPacketTransport(rtcp_packet_transport);
549 }
550 return unencrypted_rtp_transport;
Zhi Huange818b6e2018-02-22 15:26:27 -0800551}
552
553std::unique_ptr<webrtc::SrtpTransport>
554JsepTransportController::CreateSdesTransport(
555 const std::string& transport_name,
Zhi Huange830e682018-03-30 10:48:35 -0700556 cricket::DtlsTransportInternal* rtp_dtls_transport,
557 cricket::DtlsTransportInternal* rtcp_dtls_transport) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800558 RTC_DCHECK(network_thread_->IsCurrent());
Zhi Huange818b6e2018-02-22 15:26:27 -0800559 auto srtp_transport =
Karl Wiberg918f50c2018-07-05 11:40:33 +0200560 absl::make_unique<webrtc::SrtpTransport>(rtcp_dtls_transport == nullptr);
Zhi Huange830e682018-03-30 10:48:35 -0700561 RTC_DCHECK(rtp_dtls_transport);
562 srtp_transport->SetRtpPacketTransport(rtp_dtls_transport);
563 if (rtcp_dtls_transport) {
564 srtp_transport->SetRtcpPacketTransport(rtcp_dtls_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800565 }
566 if (config_.enable_external_auth) {
567 srtp_transport->EnableExternalAuth();
568 }
569 return srtp_transport;
570}
571
572std::unique_ptr<webrtc::DtlsSrtpTransport>
573JsepTransportController::CreateDtlsSrtpTransport(
574 const std::string& transport_name,
575 cricket::DtlsTransportInternal* rtp_dtls_transport,
576 cricket::DtlsTransportInternal* rtcp_dtls_transport) {
577 RTC_DCHECK(network_thread_->IsCurrent());
Karl Wiberg918f50c2018-07-05 11:40:33 +0200578 auto dtls_srtp_transport = absl::make_unique<webrtc::DtlsSrtpTransport>(
Zhi Huang365381f2018-04-13 16:44:34 -0700579 rtcp_dtls_transport == nullptr);
Zhi Huang27f3bf52018-03-26 21:37:23 -0700580 if (config_.enable_external_auth) {
Zhi Huang365381f2018-04-13 16:44:34 -0700581 dtls_srtp_transport->EnableExternalAuth();
Zhi Huang27f3bf52018-03-26 21:37:23 -0700582 }
Zhi Huang97d5e5b2018-03-27 00:09:01 +0000583
Zhi Huange818b6e2018-02-22 15:26:27 -0800584 dtls_srtp_transport->SetDtlsTransports(rtp_dtls_transport,
585 rtcp_dtls_transport);
Zhi Huangb57e1692018-06-12 11:41:11 -0700586 dtls_srtp_transport->SetActiveResetSrtpParams(
587 config_.active_reset_srtp_params);
Jonas Olsson635474e2018-10-18 15:58:17 +0200588 dtls_srtp_transport->SignalDtlsStateChange.connect(
589 this, &JsepTransportController::UpdateAggregateStates_n);
Zhi Huange818b6e2018-02-22 15:26:27 -0800590 return dtls_srtp_transport;
591}
592
593std::vector<cricket::DtlsTransportInternal*>
594JsepTransportController::GetDtlsTransports() {
595 std::vector<cricket::DtlsTransportInternal*> dtls_transports;
Zhi Huange830e682018-03-30 10:48:35 -0700596 for (auto it = jsep_transports_by_name_.begin();
597 it != jsep_transports_by_name_.end(); ++it) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800598 auto jsep_transport = it->second.get();
599 RTC_DCHECK(jsep_transport);
600 if (jsep_transport->rtp_dtls_transport()) {
601 dtls_transports.push_back(jsep_transport->rtp_dtls_transport());
602 }
603
604 if (jsep_transport->rtcp_dtls_transport()) {
605 dtls_transports.push_back(jsep_transport->rtcp_dtls_transport());
606 }
607 }
608 return dtls_transports;
609}
610
Zhi Huange818b6e2018-02-22 15:26:27 -0800611RTCError JsepTransportController::ApplyDescription_n(
612 bool local,
613 SdpType type,
614 const cricket::SessionDescription* description) {
615 RTC_DCHECK(network_thread_->IsCurrent());
616 RTC_DCHECK(description);
617
618 if (local) {
619 local_desc_ = description;
620 } else {
621 remote_desc_ = description;
622 }
623
Zhi Huange830e682018-03-30 10:48:35 -0700624 RTCError error;
Zhi Huangd2248f82018-04-10 14:41:03 -0700625 error = ValidateAndMaybeUpdateBundleGroup(local, type, description);
Zhi Huange830e682018-03-30 10:48:35 -0700626 if (!error.ok()) {
627 return error;
Zhi Huange818b6e2018-02-22 15:26:27 -0800628 }
629
630 std::vector<int> merged_encrypted_extension_ids;
631 if (bundle_group_) {
632 merged_encrypted_extension_ids =
633 MergeEncryptedHeaderExtensionIdsForBundle(description);
634 }
635
636 for (const cricket::ContentInfo& content_info : description->contents()) {
637 // Don't create transports for rejected m-lines and bundled m-lines."
638 if (content_info.rejected ||
639 (IsBundled(content_info.name) && content_info.name != *bundled_mid())) {
640 continue;
641 }
Piotr (Peter) Slatala105ded32019-02-27 14:26:15 -0800642 error = MaybeCreateJsepTransport(local, content_info, *description);
Zhi Huange830e682018-03-30 10:48:35 -0700643 if (!error.ok()) {
644 return error;
645 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800646 }
647
648 RTC_DCHECK(description->contents().size() ==
649 description->transport_infos().size());
650 for (size_t i = 0; i < description->contents().size(); ++i) {
651 const cricket::ContentInfo& content_info = description->contents()[i];
652 const cricket::TransportInfo& transport_info =
653 description->transport_infos()[i];
654 if (content_info.rejected) {
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700655 HandleRejectedContent(content_info, description);
Zhi Huange818b6e2018-02-22 15:26:27 -0800656 continue;
657 }
658
659 if (IsBundled(content_info.name) && content_info.name != *bundled_mid()) {
Zhi Huang365381f2018-04-13 16:44:34 -0700660 if (!HandleBundledContent(content_info)) {
661 return RTCError(RTCErrorType::INVALID_PARAMETER,
662 "Failed to process the bundled m= section.");
663 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800664 continue;
665 }
666
Zhi Huange830e682018-03-30 10:48:35 -0700667 error = ValidateContent(content_info);
668 if (!error.ok()) {
669 return error;
670 }
671
Zhi Huange818b6e2018-02-22 15:26:27 -0800672 std::vector<int> extension_ids;
Taylor Brandstetter0ab56512018-04-12 10:30:48 -0700673 if (bundled_mid() && content_info.name == *bundled_mid()) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800674 extension_ids = merged_encrypted_extension_ids;
675 } else {
676 extension_ids = GetEncryptedHeaderExtensionIds(content_info);
677 }
678
Zhi Huange830e682018-03-30 10:48:35 -0700679 int rtp_abs_sendtime_extn_id =
680 GetRtpAbsSendTimeHeaderExtensionId(content_info);
681
Zhi Huang365381f2018-04-13 16:44:34 -0700682 cricket::JsepTransport* transport =
Zhi Huange830e682018-03-30 10:48:35 -0700683 GetJsepTransportForMid(content_info.name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800684 RTC_DCHECK(transport);
685
686 SetIceRole_n(DetermineIceRole(transport, transport_info, type, local));
687
Zhi Huange818b6e2018-02-22 15:26:27 -0800688 cricket::JsepTransportDescription jsep_description =
689 CreateJsepTransportDescription(content_info, transport_info,
Zhi Huange830e682018-03-30 10:48:35 -0700690 extension_ids, rtp_abs_sendtime_extn_id);
Zhi Huange818b6e2018-02-22 15:26:27 -0800691 if (local) {
692 error =
693 transport->SetLocalJsepTransportDescription(jsep_description, type);
694 } else {
695 error =
696 transport->SetRemoteJsepTransportDescription(jsep_description, type);
697 }
698
699 if (!error.ok()) {
700 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
701 "Failed to apply the description for " +
702 content_info.name + ": " + error.message());
703 }
704 }
705 return RTCError::OK();
706}
707
Zhi Huangd2248f82018-04-10 14:41:03 -0700708RTCError JsepTransportController::ValidateAndMaybeUpdateBundleGroup(
709 bool local,
710 SdpType type,
Zhi Huange830e682018-03-30 10:48:35 -0700711 const cricket::SessionDescription* description) {
712 RTC_DCHECK(description);
Zhi Huangd2248f82018-04-10 14:41:03 -0700713 const cricket::ContentGroup* new_bundle_group =
714 description->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
715
716 // The BUNDLE group containing a MID that no m= section has is invalid.
717 if (new_bundle_group) {
Mirko Bonadei739baf02019-01-27 17:29:42 +0100718 for (const auto& content_name : new_bundle_group->content_names()) {
Zhi Huangd2248f82018-04-10 14:41:03 -0700719 if (!description->GetContentByName(content_name)) {
720 return RTCError(RTCErrorType::INVALID_PARAMETER,
721 "The BUNDLE group contains MID:" + content_name +
722 " matching no m= section.");
723 }
724 }
725 }
726
727 if (type == SdpType::kAnswer) {
728 const cricket::ContentGroup* offered_bundle_group =
729 local ? remote_desc_->GetGroupByName(cricket::GROUP_TYPE_BUNDLE)
730 : local_desc_->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
731
732 if (new_bundle_group) {
733 // The BUNDLE group in answer should be a subset of offered group.
Mirko Bonadei739baf02019-01-27 17:29:42 +0100734 for (const auto& content_name : new_bundle_group->content_names()) {
Zhi Huangd2248f82018-04-10 14:41:03 -0700735 if (!offered_bundle_group ||
736 !offered_bundle_group->HasContentName(content_name)) {
737 return RTCError(RTCErrorType::INVALID_PARAMETER,
738 "The BUNDLE group in answer contains a MID that was "
739 "not in the offered group.");
740 }
741 }
742 }
743
744 if (bundle_group_) {
Mirko Bonadei739baf02019-01-27 17:29:42 +0100745 for (const auto& content_name : bundle_group_->content_names()) {
Zhi Huangd2248f82018-04-10 14:41:03 -0700746 // An answer that removes m= sections from pre-negotiated BUNDLE group
747 // without rejecting it, is invalid.
748 if (!new_bundle_group ||
749 !new_bundle_group->HasContentName(content_name)) {
750 auto* content_info = description->GetContentByName(content_name);
751 if (!content_info || !content_info->rejected) {
752 return RTCError(RTCErrorType::INVALID_PARAMETER,
753 "Answer cannot remove m= section " + content_name +
754 " from already-established BUNDLE group.");
755 }
756 }
757 }
758 }
759 }
760
761 if (config_.bundle_policy ==
762 PeerConnectionInterface::kBundlePolicyMaxBundle &&
763 !description->HasGroup(cricket::GROUP_TYPE_BUNDLE)) {
764 return RTCError(RTCErrorType::INVALID_PARAMETER,
765 "max-bundle is used but no bundle group found.");
766 }
767
768 if (ShouldUpdateBundleGroup(type, description)) {
Zhi Huangd2248f82018-04-10 14:41:03 -0700769 bundle_group_ = *new_bundle_group;
770 }
Zhi Huange830e682018-03-30 10:48:35 -0700771
772 if (!bundled_mid()) {
773 return RTCError::OK();
774 }
775
776 auto bundled_content = description->GetContentByName(*bundled_mid());
777 if (!bundled_content) {
778 return RTCError(
779 RTCErrorType::INVALID_PARAMETER,
780 "An m= section associated with the BUNDLE-tag doesn't exist.");
781 }
782
783 // If the |bundled_content| is rejected, other contents in the bundle group
784 // should be rejected.
785 if (bundled_content->rejected) {
Mirko Bonadei739baf02019-01-27 17:29:42 +0100786 for (const auto& content_name : bundle_group_->content_names()) {
Zhi Huange830e682018-03-30 10:48:35 -0700787 auto other_content = description->GetContentByName(content_name);
788 if (!other_content->rejected) {
789 return RTCError(
790 RTCErrorType::INVALID_PARAMETER,
791 "The m= section:" + content_name + " should be rejected.");
792 }
793 }
794 }
795
796 return RTCError::OK();
797}
798
799RTCError JsepTransportController::ValidateContent(
800 const cricket::ContentInfo& content_info) {
801 if (config_.rtcp_mux_policy ==
802 PeerConnectionInterface::kRtcpMuxPolicyRequire &&
803 content_info.type == cricket::MediaProtocolType::kRtp &&
804 !content_info.media_description()->rtcp_mux()) {
805 return RTCError(RTCErrorType::INVALID_PARAMETER,
806 "The m= section:" + content_info.name +
807 " is invalid. RTCP-MUX is not "
808 "enabled when it is required.");
809 }
810 return RTCError::OK();
811}
812
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700813void JsepTransportController::HandleRejectedContent(
Zhi Huangd2248f82018-04-10 14:41:03 -0700814 const cricket::ContentInfo& content_info,
815 const cricket::SessionDescription* description) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800816 // If the content is rejected, let the
817 // BaseChannel/SctpTransport change the RtpTransport/DtlsTransport first,
Zhi Huang365381f2018-04-13 16:44:34 -0700818 // then destroy the cricket::JsepTransport.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700819 RemoveTransportForMid(content_info.name);
Zhi Huange830e682018-03-30 10:48:35 -0700820 if (content_info.name == bundled_mid()) {
Mirko Bonadei739baf02019-01-27 17:29:42 +0100821 for (const auto& content_name : bundle_group_->content_names()) {
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700822 RemoveTransportForMid(content_name);
Zhi Huange830e682018-03-30 10:48:35 -0700823 }
824 bundle_group_.reset();
825 } else if (IsBundled(content_info.name)) {
826 // Remove the rejected content from the |bundle_group_|.
Zhi Huange818b6e2018-02-22 15:26:27 -0800827 bundle_group_->RemoveContentName(content_info.name);
Zhi Huange830e682018-03-30 10:48:35 -0700828 // Reset the bundle group if nothing left.
829 if (!bundle_group_->FirstContentName()) {
830 bundle_group_.reset();
831 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800832 }
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700833 MaybeDestroyJsepTransport(content_info.name);
Zhi Huange818b6e2018-02-22 15:26:27 -0800834}
835
Zhi Huang365381f2018-04-13 16:44:34 -0700836bool JsepTransportController::HandleBundledContent(
Zhi Huange818b6e2018-02-22 15:26:27 -0800837 const cricket::ContentInfo& content_info) {
Zhi Huangd2248f82018-04-10 14:41:03 -0700838 auto jsep_transport = GetJsepTransportByName(*bundled_mid());
839 RTC_DCHECK(jsep_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800840 // If the content is bundled, let the
841 // BaseChannel/SctpTransport change the RtpTransport/DtlsTransport first,
Zhi Huang365381f2018-04-13 16:44:34 -0700842 // then destroy the cricket::JsepTransport.
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700843 if (SetTransportForMid(content_info.name, jsep_transport)) {
Piotr (Peter) Slatala10aeb2a2018-11-14 10:57:24 -0800844 // TODO(bugs.webrtc.org/9719) For media transport this is far from ideal,
845 // because it means that we first create media transport and start
846 // connecting it, and then we destroy it. We will need to address it before
847 // video path is enabled.
Zhi Huang365381f2018-04-13 16:44:34 -0700848 MaybeDestroyJsepTransport(content_info.name);
849 return true;
850 }
851 return false;
Zhi Huange818b6e2018-02-22 15:26:27 -0800852}
853
Zhi Huang365381f2018-04-13 16:44:34 -0700854bool JsepTransportController::SetTransportForMid(
Zhi Huangd2248f82018-04-10 14:41:03 -0700855 const std::string& mid,
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700856 cricket::JsepTransport* jsep_transport) {
Zhi Huang365381f2018-04-13 16:44:34 -0700857 RTC_DCHECK(jsep_transport);
Zhi Huangd2248f82018-04-10 14:41:03 -0700858 if (mid_to_transport_[mid] == jsep_transport) {
Zhi Huang365381f2018-04-13 16:44:34 -0700859 return true;
Zhi Huangd2248f82018-04-10 14:41:03 -0700860 }
861
862 mid_to_transport_[mid] = jsep_transport;
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700863 return config_.transport_observer->OnTransportChanged(
Harald Alvestrandc85328f2019-02-28 07:51:00 +0100864 mid, jsep_transport->rtp_transport(), jsep_transport->RtpDtlsTransport(),
865 jsep_transport->media_transport());
Zhi Huangd2248f82018-04-10 14:41:03 -0700866}
867
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700868void JsepTransportController::RemoveTransportForMid(const std::string& mid) {
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -0800869 bool ret = config_.transport_observer->OnTransportChanged(mid, nullptr,
870 nullptr, nullptr);
Taylor Brandstettercbaa2542018-04-16 16:42:14 -0700871 // Calling OnTransportChanged with nullptr should always succeed, since it is
872 // only expected to fail when adding media to a transport (not removing).
873 RTC_DCHECK(ret);
Zhi Huangd2248f82018-04-10 14:41:03 -0700874 mid_to_transport_.erase(mid);
875}
876
Zhi Huange818b6e2018-02-22 15:26:27 -0800877cricket::JsepTransportDescription
878JsepTransportController::CreateJsepTransportDescription(
879 cricket::ContentInfo content_info,
880 cricket::TransportInfo transport_info,
Zhi Huange830e682018-03-30 10:48:35 -0700881 const std::vector<int>& encrypted_extension_ids,
882 int rtp_abs_sendtime_extn_id) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800883 const cricket::MediaContentDescription* content_desc =
884 static_cast<const cricket::MediaContentDescription*>(
885 content_info.description);
886 RTC_DCHECK(content_desc);
887 bool rtcp_mux_enabled = content_info.type == cricket::MediaProtocolType::kSctp
888 ? true
889 : content_desc->rtcp_mux();
890
891 return cricket::JsepTransportDescription(
892 rtcp_mux_enabled, content_desc->cryptos(), encrypted_extension_ids,
Zhi Huange830e682018-03-30 10:48:35 -0700893 rtp_abs_sendtime_extn_id, transport_info.description);
Zhi Huange818b6e2018-02-22 15:26:27 -0800894}
895
896bool JsepTransportController::ShouldUpdateBundleGroup(
897 SdpType type,
898 const cricket::SessionDescription* description) {
899 if (config_.bundle_policy ==
900 PeerConnectionInterface::kBundlePolicyMaxBundle) {
901 return true;
902 }
903
904 if (type != SdpType::kAnswer) {
905 return false;
906 }
907
908 RTC_DCHECK(local_desc_ && remote_desc_);
909 const cricket::ContentGroup* local_bundle =
910 local_desc_->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
911 const cricket::ContentGroup* remote_bundle =
912 remote_desc_->GetGroupByName(cricket::GROUP_TYPE_BUNDLE);
913 return local_bundle && remote_bundle;
914}
915
916std::vector<int> JsepTransportController::GetEncryptedHeaderExtensionIds(
917 const cricket::ContentInfo& content_info) {
918 const cricket::MediaContentDescription* content_desc =
919 static_cast<const cricket::MediaContentDescription*>(
920 content_info.description);
921
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700922 if (!config_.crypto_options.srtp.enable_encrypted_rtp_header_extensions) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800923 return std::vector<int>();
924 }
925
926 std::vector<int> encrypted_header_extension_ids;
Mirko Bonadei739baf02019-01-27 17:29:42 +0100927 for (const auto& extension : content_desc->rtp_header_extensions()) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800928 if (!extension.encrypt) {
929 continue;
930 }
Steve Anton64b626b2019-01-28 17:25:26 -0800931 if (!absl::c_linear_search(encrypted_header_extension_ids, extension.id)) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800932 encrypted_header_extension_ids.push_back(extension.id);
933 }
934 }
935 return encrypted_header_extension_ids;
936}
937
938std::vector<int>
939JsepTransportController::MergeEncryptedHeaderExtensionIdsForBundle(
940 const cricket::SessionDescription* description) {
941 RTC_DCHECK(description);
942 RTC_DCHECK(bundle_group_);
943
944 std::vector<int> merged_ids;
945 // Union the encrypted header IDs in the group when bundle is enabled.
946 for (const cricket::ContentInfo& content_info : description->contents()) {
947 if (bundle_group_->HasContentName(content_info.name)) {
948 std::vector<int> extension_ids =
949 GetEncryptedHeaderExtensionIds(content_info);
950 for (int id : extension_ids) {
Steve Anton64b626b2019-01-28 17:25:26 -0800951 if (!absl::c_linear_search(merged_ids, id)) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800952 merged_ids.push_back(id);
953 }
954 }
955 }
956 }
957 return merged_ids;
958}
959
Zhi Huange830e682018-03-30 10:48:35 -0700960int JsepTransportController::GetRtpAbsSendTimeHeaderExtensionId(
Zhi Huange818b6e2018-02-22 15:26:27 -0800961 const cricket::ContentInfo& content_info) {
Zhi Huange830e682018-03-30 10:48:35 -0700962 if (!config_.enable_external_auth) {
963 return -1;
Zhi Huange818b6e2018-02-22 15:26:27 -0800964 }
965
966 const cricket::MediaContentDescription* content_desc =
967 static_cast<const cricket::MediaContentDescription*>(
968 content_info.description);
Zhi Huange830e682018-03-30 10:48:35 -0700969
970 const webrtc::RtpExtension* send_time_extension =
971 webrtc::RtpExtension::FindHeaderExtensionByUri(
972 content_desc->rtp_header_extensions(),
973 webrtc::RtpExtension::kAbsSendTimeUri);
974 return send_time_extension ? send_time_extension->id : -1;
975}
976
Zhi Huang365381f2018-04-13 16:44:34 -0700977const cricket::JsepTransport* JsepTransportController::GetJsepTransportForMid(
Zhi Huange830e682018-03-30 10:48:35 -0700978 const std::string& mid) const {
Zhi Huangd2248f82018-04-10 14:41:03 -0700979 auto it = mid_to_transport_.find(mid);
980 return it == mid_to_transport_.end() ? nullptr : it->second;
Zhi Huange830e682018-03-30 10:48:35 -0700981}
982
Zhi Huang365381f2018-04-13 16:44:34 -0700983cricket::JsepTransport* JsepTransportController::GetJsepTransportForMid(
Zhi Huange830e682018-03-30 10:48:35 -0700984 const std::string& mid) {
Zhi Huangd2248f82018-04-10 14:41:03 -0700985 auto it = mid_to_transport_.find(mid);
986 return it == mid_to_transport_.end() ? nullptr : it->second;
Zhi Huange830e682018-03-30 10:48:35 -0700987}
988
Zhi Huang365381f2018-04-13 16:44:34 -0700989const cricket::JsepTransport* JsepTransportController::GetJsepTransportByName(
Zhi Huange830e682018-03-30 10:48:35 -0700990 const std::string& transport_name) const {
991 auto it = jsep_transports_by_name_.find(transport_name);
992 return (it == jsep_transports_by_name_.end()) ? nullptr : it->second.get();
993}
994
Zhi Huang365381f2018-04-13 16:44:34 -0700995cricket::JsepTransport* JsepTransportController::GetJsepTransportByName(
Zhi Huange830e682018-03-30 10:48:35 -0700996 const std::string& transport_name) {
997 auto it = jsep_transports_by_name_.find(transport_name);
998 return (it == jsep_transports_by_name_.end()) ? nullptr : it->second.get();
999}
1000
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001001std::unique_ptr<webrtc::MediaTransportInterface>
1002JsepTransportController::MaybeCreateMediaTransport(
1003 const cricket::ContentInfo& content_info,
Piotr (Peter) Slatala105ded32019-02-27 14:26:15 -08001004 const cricket::SessionDescription& description,
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001005 bool local) {
Piotr (Peter) Slatala63a176b2019-01-25 08:25:33 -08001006 if (config_.media_transport_factory == nullptr) {
1007 return nullptr;
1008 }
1009
Piotr (Peter) Slatala55b91b92019-01-25 13:31:15 -08001010 if (!config_.use_media_transport_for_media &&
1011 !config_.use_media_transport_for_data_channels) {
1012 return nullptr;
1013 }
1014
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001015 // Caller (offerer) media transport.
1016 if (local) {
1017 if (offer_media_transport_) {
1018 RTC_LOG(LS_INFO) << "Offered media transport has now been activated.";
1019 return std::move(offer_media_transport_);
1020 } else {
1021 RTC_LOG(LS_INFO)
1022 << "Not returning media transport. Either SDES wasn't enabled, or "
1023 "media transport didn't return an offer earlier.";
1024 // Offer wasn't generated. Either because media transport didn't want it,
1025 // or because SDES wasn't enabled.
1026 return nullptr;
1027 }
Piotr (Peter) Slatala9f956252018-10-31 08:25:26 -07001028 }
1029
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001030 // Remote offer. If no x-mt lines, do not create media transport.
1031 if (description.MediaTransportSettings().empty()) {
Piotr (Peter) Slatala63a176b2019-01-25 08:25:33 -08001032 return nullptr;
Anton Sukhanov7940da02018-10-10 10:34:49 -07001033 }
1034
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001035 // When bundle is enabled, two JsepTransports are created, and then
1036 // the second transport is destroyed (right away).
1037 // For media transport, we don't want to create the second
1038 // media transport in the first place.
1039 RTC_LOG(LS_INFO) << "Returning new, client media transport.";
Piotr (Peter) Slatala63a176b2019-01-25 08:25:33 -08001040
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001041 RTC_DCHECK(!local)
1042 << "If media transport is used, you must call "
1043 "GenerateOrGetLastMediaTransportOffer before SetLocalDescription. You "
1044 "also "
1045 "must use kRtcpMuxPolicyRequire and kBundlePolicyMaxBundle with media "
1046 "transport.";
Piotr (Peter) Slatala63a176b2019-01-25 08:25:33 -08001047 MediaTransportSettings settings;
1048 settings.is_caller = local;
Piotr (Peter) Slatala01fe3092019-02-15 12:05:50 -08001049 if (config_.use_media_transport_for_media) {
1050 settings.event_log = config_.event_log;
1051 }
Piotr (Peter) Slatala105ded32019-02-27 14:26:15 -08001052
1053 // Assume there is only one media transport (or if more, use the first one).
1054 if (!local && !description.MediaTransportSettings().empty() &&
1055 config_.media_transport_factory->GetTransportName() ==
1056 description.MediaTransportSettings()[0].transport_name) {
1057 settings.remote_transport_parameters =
1058 description.MediaTransportSettings()[0].transport_setting;
1059 }
1060
Piotr (Peter) Slatala63a176b2019-01-25 08:25:33 -08001061 auto media_transport_result =
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001062 config_.media_transport_factory->CreateMediaTransport(network_thread_,
1063 settings);
Piotr (Peter) Slatala63a176b2019-01-25 08:25:33 -08001064
1065 // TODO(sukhanov): Proper error handling.
1066 RTC_CHECK(media_transport_result.ok());
1067
1068 return media_transport_result.MoveValue();
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001069}
1070
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001071// TODO(sukhanov): Refactor to avoid code duplication for Media and Datagram
1072// transports setup.
1073std::unique_ptr<webrtc::DatagramTransportInterface>
1074JsepTransportController::MaybeCreateDatagramTransport(
1075 const cricket::ContentInfo& content_info,
1076 const cricket::SessionDescription& description,
1077 bool local) {
1078 if (config_.media_transport_factory == nullptr) {
1079 return nullptr;
1080 }
1081
1082 if (!config_.use_datagram_transport) {
1083 return nullptr;
1084 }
1085
1086 // Caller (offerer) datagram transport.
1087 if (local) {
1088 if (offer_datagram_transport_) {
1089 RTC_LOG(LS_INFO) << "Offered datagram transport has now been activated.";
1090 return std::move(offer_datagram_transport_);
1091 } else {
1092 RTC_LOG(LS_INFO)
1093 << "Not returning datagram transport. Either SDES wasn't enabled, or "
1094 "datagram transport didn't return an offer earlier.";
1095 return nullptr;
1096 }
1097 }
1098
1099 // Remote offer. If no x-mt lines, do not create datagram transport.
1100 if (description.MediaTransportSettings().empty()) {
1101 return nullptr;
1102 }
1103
1104 // When bundle is enabled, two JsepTransports are created, and then
1105 // the second transport is destroyed (right away).
1106 // For datagram transport, we don't want to create the second
1107 // datagram transport in the first place.
1108 RTC_LOG(LS_INFO) << "Returning new, client datagram transport.";
1109
1110 RTC_DCHECK(!local)
1111 << "If datagram transport is used, you must call "
1112 "GenerateOrGetLastMediaTransportOffer before SetLocalDescription. You "
1113 "also must use kRtcpMuxPolicyRequire and kBundlePolicyMaxBundle with "
1114 "datagram transport.";
1115 MediaTransportSettings settings;
1116 settings.is_caller = local;
1117 settings.event_log = config_.event_log;
1118
1119 // Assume there is only one media transport (or if more, use the first one).
1120 if (!local && !description.MediaTransportSettings().empty() &&
1121 config_.media_transport_factory->GetTransportName() ==
1122 description.MediaTransportSettings()[0].transport_name) {
1123 settings.remote_transport_parameters =
1124 description.MediaTransportSettings()[0].transport_setting;
1125 }
1126
1127 auto datagram_transport_result =
1128 config_.media_transport_factory->CreateDatagramTransport(network_thread_,
1129 settings);
1130
1131 // TODO(sukhanov): Proper error handling.
1132 RTC_CHECK(datagram_transport_result.ok());
1133
1134 return datagram_transport_result.MoveValue();
1135}
1136
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001137RTCError JsepTransportController::MaybeCreateJsepTransport(
1138 bool local,
Piotr (Peter) Slatala105ded32019-02-27 14:26:15 -08001139 const cricket::ContentInfo& content_info,
1140 const cricket::SessionDescription& description) {
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001141 RTC_DCHECK(network_thread_->IsCurrent());
1142 cricket::JsepTransport* transport = GetJsepTransportByName(content_info.name);
1143 if (transport) {
1144 return RTCError::OK();
1145 }
1146
1147 const cricket::MediaContentDescription* content_desc =
1148 static_cast<const cricket::MediaContentDescription*>(
1149 content_info.description);
1150 if (certificate_ && !content_desc->cryptos().empty()) {
1151 return RTCError(RTCErrorType::INVALID_PARAMETER,
1152 "SDES and DTLS-SRTP cannot be enabled at the same time.");
1153 }
1154
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -08001155 std::unique_ptr<cricket::IceTransportInternal> ice =
1156 CreateIceTransport(content_info.name, /*rtcp=*/false);
1157
1158 std::unique_ptr<MediaTransportInterface> media_transport =
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001159 MaybeCreateMediaTransport(content_info, description, local);
1160 if (media_transport) {
1161 media_transport_created_once_ = true;
1162 media_transport->Connect(ice.get());
1163 }
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -08001164
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001165 std::unique_ptr<DatagramTransportInterface> datagram_transport =
1166 MaybeCreateDatagramTransport(content_info, description, local);
1167 if (datagram_transport) {
1168 datagram_transport_created_once_ = true;
1169 datagram_transport->Connect(ice.get());
1170 }
1171
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001172 std::unique_ptr<cricket::DtlsTransportInternal> rtp_dtls_transport =
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001173 CreateDtlsTransport(std::move(ice), std::move(datagram_transport));
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001174
1175 std::unique_ptr<cricket::DtlsTransportInternal> rtcp_dtls_transport;
1176 std::unique_ptr<RtpTransport> unencrypted_rtp_transport;
1177 std::unique_ptr<SrtpTransport> sdes_transport;
1178 std::unique_ptr<DtlsSrtpTransport> dtls_srtp_transport;
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001179
1180 if (config_.rtcp_mux_policy !=
1181 PeerConnectionInterface::kRtcpMuxPolicyRequire &&
1182 content_info.type == cricket::MediaProtocolType::kRtp) {
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -08001183 RTC_DCHECK(media_transport == nullptr);
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001184 RTC_DCHECK(datagram_transport == nullptr);
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -08001185 rtcp_dtls_transport = CreateDtlsTransport(
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001186 CreateIceTransport(content_info.name, /*rtcp=*/true),
1187 /*datagram_transport=*/nullptr);
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001188 }
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001189
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001190 if (datagram_transport) {
1191 // TODO(sukhanov): We use unencrypted RTP transport over DatagramTransport,
1192 // because MediaTransport encrypts. In the future we may want to
1193 // implement our own version of RtpTransport over MediaTransport, because
1194 // it will give us more control over things like:
1195 // - Fusing
1196 // - Rtp header compression
1197 // - Handling Rtcp feedback.
1198 RTC_LOG(LS_INFO) << "Creating UnencryptedRtpTransport, because datagram "
1199 "transport is used.";
1200 RTC_DCHECK(!rtcp_dtls_transport);
1201 unencrypted_rtp_transport = CreateUnencryptedRtpTransport(
1202 content_info.name, rtp_dtls_transport.get(), rtcp_dtls_transport.get());
1203 } else if (config_.disable_encryption) {
1204 RTC_LOG(LS_INFO)
1205 << "Creating UnencryptedRtpTransport, becayse encryption is disabled.";
Zhi Huange818b6e2018-02-22 15:26:27 -08001206 unencrypted_rtp_transport = CreateUnencryptedRtpTransport(
Zhi Huangd2248f82018-04-10 14:41:03 -07001207 content_info.name, rtp_dtls_transport.get(), rtcp_dtls_transport.get());
Zhi Huange818b6e2018-02-22 15:26:27 -08001208 } else if (!content_desc->cryptos().empty()) {
Zhi Huangd2248f82018-04-10 14:41:03 -07001209 sdes_transport = CreateSdesTransport(
1210 content_info.name, rtp_dtls_transport.get(), rtcp_dtls_transport.get());
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001211 RTC_LOG(LS_INFO) << "Creating SdesTransport.";
Zhi Huange818b6e2018-02-22 15:26:27 -08001212 } else {
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001213 RTC_LOG(LS_INFO) << "Creating DtlsSrtpTransport.";
Zhi Huangd2248f82018-04-10 14:41:03 -07001214 dtls_srtp_transport = CreateDtlsSrtpTransport(
1215 content_info.name, rtp_dtls_transport.get(), rtcp_dtls_transport.get());
Zhi Huange818b6e2018-02-22 15:26:27 -08001216 }
1217
Zhi Huang365381f2018-04-13 16:44:34 -07001218 std::unique_ptr<cricket::JsepTransport> jsep_transport =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001219 absl::make_unique<cricket::JsepTransport>(
Zhi Huangd2248f82018-04-10 14:41:03 -07001220 content_info.name, certificate_, std::move(unencrypted_rtp_transport),
Zhi Huange818b6e2018-02-22 15:26:27 -08001221 std::move(sdes_transport), std::move(dtls_srtp_transport),
Anton Sukhanov7940da02018-10-10 10:34:49 -07001222 std::move(rtp_dtls_transport), std::move(rtcp_dtls_transport),
1223 std::move(media_transport));
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001224
Zhi Huange818b6e2018-02-22 15:26:27 -08001225 jsep_transport->SignalRtcpMuxActive.connect(
1226 this, &JsepTransportController::UpdateAggregateStates_n);
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -07001227 jsep_transport->SignalMediaTransportStateChanged.connect(
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001228 this, &JsepTransportController::OnMediaTransportStateChanged_n);
Taylor Brandstettercbaa2542018-04-16 16:42:14 -07001229 SetTransportForMid(content_info.name, jsep_transport.get());
Zhi Huange830e682018-03-30 10:48:35 -07001230
Zhi Huangd2248f82018-04-10 14:41:03 -07001231 jsep_transports_by_name_[content_info.name] = std::move(jsep_transport);
1232 UpdateAggregateStates_n();
Zhi Huange830e682018-03-30 10:48:35 -07001233 return RTCError::OK();
Zhi Huange818b6e2018-02-22 15:26:27 -08001234}
1235
1236void JsepTransportController::MaybeDestroyJsepTransport(
1237 const std::string& mid) {
Zhi Huangd2248f82018-04-10 14:41:03 -07001238 auto jsep_transport = GetJsepTransportByName(mid);
1239 if (!jsep_transport) {
1240 return;
1241 }
1242
1243 // Don't destroy the JsepTransport if there are still media sections referring
1244 // to it.
1245 for (const auto& kv : mid_to_transport_) {
1246 if (kv.second == jsep_transport) {
1247 return;
1248 }
1249 }
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -08001250
Zhi Huange830e682018-03-30 10:48:35 -07001251 jsep_transports_by_name_.erase(mid);
Zhi Huange818b6e2018-02-22 15:26:27 -08001252 UpdateAggregateStates_n();
1253}
1254
1255void JsepTransportController::DestroyAllJsepTransports_n() {
1256 RTC_DCHECK(network_thread_->IsCurrent());
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -08001257
1258 for (const auto& jsep_transport : jsep_transports_by_name_) {
1259 config_.transport_observer->OnTransportChanged(jsep_transport.first,
1260 nullptr, nullptr, nullptr);
1261 }
1262
Zhi Huange830e682018-03-30 10:48:35 -07001263 jsep_transports_by_name_.clear();
Zhi Huange818b6e2018-02-22 15:26:27 -08001264}
1265
1266void JsepTransportController::SetIceRole_n(cricket::IceRole ice_role) {
1267 RTC_DCHECK(network_thread_->IsCurrent());
1268
1269 ice_role_ = ice_role;
1270 for (auto& dtls : GetDtlsTransports()) {
1271 dtls->ice_transport()->SetIceRole(ice_role_);
1272 }
1273}
1274
1275cricket::IceRole JsepTransportController::DetermineIceRole(
Zhi Huang365381f2018-04-13 16:44:34 -07001276 cricket::JsepTransport* jsep_transport,
Zhi Huange818b6e2018-02-22 15:26:27 -08001277 const cricket::TransportInfo& transport_info,
1278 SdpType type,
1279 bool local) {
1280 cricket::IceRole ice_role = ice_role_;
1281 auto tdesc = transport_info.description;
1282 if (local) {
1283 // The initial offer side may use ICE Lite, in which case, per RFC5245
1284 // Section 5.1.1, the answer side should take the controlling role if it is
1285 // in the full ICE mode.
1286 //
1287 // When both sides use ICE Lite, the initial offer side must take the
1288 // controlling role, and this is the default logic implemented in
1289 // SetLocalDescription in JsepTransportController.
1290 if (jsep_transport->remote_description() &&
1291 jsep_transport->remote_description()->transport_desc.ice_mode ==
1292 cricket::ICEMODE_LITE &&
1293 ice_role_ == cricket::ICEROLE_CONTROLLED &&
1294 tdesc.ice_mode == cricket::ICEMODE_FULL) {
1295 ice_role = cricket::ICEROLE_CONTROLLING;
1296 }
1297
1298 // Older versions of Chrome expect the ICE role to be re-determined when an
1299 // ICE restart occurs, and also don't perform conflict resolution correctly,
1300 // so for now we can't safely stop doing this, unless the application opts
1301 // in by setting |config_.redetermine_role_on_ice_restart_| to false. See:
1302 // https://bugs.chromium.org/p/chromium/issues/detail?id=628676
1303 // TODO(deadbeef): Remove this when these old versions of Chrome reach a low
1304 // enough population.
1305 if (config_.redetermine_role_on_ice_restart &&
1306 jsep_transport->local_description() &&
1307 cricket::IceCredentialsChanged(
1308 jsep_transport->local_description()->transport_desc.ice_ufrag,
1309 jsep_transport->local_description()->transport_desc.ice_pwd,
1310 tdesc.ice_ufrag, tdesc.ice_pwd) &&
1311 // Don't change the ICE role if the remote endpoint is ICE lite; we
1312 // should always be controlling in that case.
1313 (!jsep_transport->remote_description() ||
1314 jsep_transport->remote_description()->transport_desc.ice_mode !=
1315 cricket::ICEMODE_LITE)) {
1316 ice_role = (type == SdpType::kOffer) ? cricket::ICEROLE_CONTROLLING
1317 : cricket::ICEROLE_CONTROLLED;
1318 }
1319 } else {
1320 // If our role is cricket::ICEROLE_CONTROLLED and the remote endpoint
1321 // supports only ice_lite, this local endpoint should take the CONTROLLING
1322 // role.
1323 // TODO(deadbeef): This is a session-level attribute, so it really shouldn't
1324 // be in a TransportDescription in the first place...
1325 if (ice_role_ == cricket::ICEROLE_CONTROLLED &&
1326 tdesc.ice_mode == cricket::ICEMODE_LITE) {
1327 ice_role = cricket::ICEROLE_CONTROLLING;
1328 }
1329
1330 // If we use ICE Lite and the remote endpoint uses the full implementation
1331 // of ICE, the local endpoint must take the controlled role, and the other
1332 // side must be the controlling role.
1333 if (jsep_transport->local_description() &&
1334 jsep_transport->local_description()->transport_desc.ice_mode ==
1335 cricket::ICEMODE_LITE &&
1336 ice_role_ == cricket::ICEROLE_CONTROLLING &&
Zhi Huange830e682018-03-30 10:48:35 -07001337 tdesc.ice_mode == cricket::ICEMODE_FULL) {
Zhi Huange818b6e2018-02-22 15:26:27 -08001338 ice_role = cricket::ICEROLE_CONTROLLED;
1339 }
1340 }
1341
1342 return ice_role;
1343}
1344
1345void JsepTransportController::OnTransportWritableState_n(
1346 rtc::PacketTransportInternal* transport) {
1347 RTC_DCHECK(network_thread_->IsCurrent());
1348 RTC_LOG(LS_INFO) << " Transport " << transport->transport_name()
1349 << " writability changed to " << transport->writable()
1350 << ".";
1351 UpdateAggregateStates_n();
1352}
1353
1354void JsepTransportController::OnTransportReceivingState_n(
1355 rtc::PacketTransportInternal* transport) {
1356 RTC_DCHECK(network_thread_->IsCurrent());
1357 UpdateAggregateStates_n();
1358}
1359
1360void JsepTransportController::OnTransportGatheringState_n(
1361 cricket::IceTransportInternal* transport) {
1362 RTC_DCHECK(network_thread_->IsCurrent());
1363 UpdateAggregateStates_n();
1364}
1365
1366void JsepTransportController::OnTransportCandidateGathered_n(
1367 cricket::IceTransportInternal* transport,
1368 const cricket::Candidate& candidate) {
1369 RTC_DCHECK(network_thread_->IsCurrent());
1370
1371 // We should never signal peer-reflexive candidates.
1372 if (candidate.type() == cricket::PRFLX_PORT_TYPE) {
1373 RTC_NOTREACHED();
1374 return;
1375 }
Steve Antond25828a2018-08-31 13:06:05 -07001376 std::string transport_name = transport->transport_name();
1377 invoker_.AsyncInvoke<void>(
1378 RTC_FROM_HERE, signaling_thread_, [this, transport_name, candidate] {
1379 SignalIceCandidatesGathered(transport_name, {candidate});
1380 });
Zhi Huange818b6e2018-02-22 15:26:27 -08001381}
1382
1383void JsepTransportController::OnTransportCandidatesRemoved_n(
1384 cricket::IceTransportInternal* transport,
1385 const cricket::Candidates& candidates) {
1386 invoker_.AsyncInvoke<void>(
1387 RTC_FROM_HERE, signaling_thread_,
Steve Antond25828a2018-08-31 13:06:05 -07001388 [this, candidates] { SignalIceCandidatesRemoved(candidates); });
Zhi Huange818b6e2018-02-22 15:26:27 -08001389}
1390
1391void JsepTransportController::OnTransportRoleConflict_n(
1392 cricket::IceTransportInternal* transport) {
1393 RTC_DCHECK(network_thread_->IsCurrent());
1394 // Note: since the role conflict is handled entirely on the network thread,
1395 // we don't need to worry about role conflicts occurring on two ports at
1396 // once. The first one encountered should immediately reverse the role.
1397 cricket::IceRole reversed_role = (ice_role_ == cricket::ICEROLE_CONTROLLING)
1398 ? cricket::ICEROLE_CONTROLLED
1399 : cricket::ICEROLE_CONTROLLING;
1400 RTC_LOG(LS_INFO) << "Got role conflict; switching to "
1401 << (reversed_role == cricket::ICEROLE_CONTROLLING
1402 ? "controlling"
1403 : "controlled")
1404 << " role.";
1405 SetIceRole_n(reversed_role);
1406}
1407
1408void JsepTransportController::OnTransportStateChanged_n(
1409 cricket::IceTransportInternal* transport) {
1410 RTC_DCHECK(network_thread_->IsCurrent());
1411 RTC_LOG(LS_INFO) << transport->transport_name() << " Transport "
1412 << transport->component()
1413 << " state changed. Check if state is complete.";
1414 UpdateAggregateStates_n();
1415}
1416
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001417void JsepTransportController::OnMediaTransportStateChanged_n() {
1418 SignalMediaTransportStateChanged();
1419 UpdateAggregateStates_n();
1420}
1421
Zhi Huange818b6e2018-02-22 15:26:27 -08001422void JsepTransportController::UpdateAggregateStates_n() {
1423 RTC_DCHECK(network_thread_->IsCurrent());
1424
1425 auto dtls_transports = GetDtlsTransports();
Alex Loiko9289eda2018-11-23 16:18:59 +00001426 cricket::IceConnectionState new_connection_state =
1427 cricket::kIceConnectionConnecting;
Jonas Olsson635474e2018-10-18 15:58:17 +02001428 PeerConnectionInterface::IceConnectionState new_ice_connection_state =
1429 PeerConnectionInterface::IceConnectionState::kIceConnectionNew;
1430 PeerConnectionInterface::PeerConnectionState new_combined_state =
1431 PeerConnectionInterface::PeerConnectionState::kNew;
Zhi Huange818b6e2018-02-22 15:26:27 -08001432 cricket::IceGatheringState new_gathering_state = cricket::kIceGatheringNew;
Alex Loiko9289eda2018-11-23 16:18:59 +00001433 bool any_failed = false;
1434
1435 // TODO(http://bugs.webrtc.org/9719) If(when) media_transport disables
1436 // dtls_transports entirely, the below line will have to be changed to account
1437 // for the fact that dtls transports might be absent.
1438 bool all_connected = !dtls_transports.empty();
1439 bool all_completed = !dtls_transports.empty();
Zhi Huange818b6e2018-02-22 15:26:27 -08001440 bool any_gathering = false;
1441 bool all_done_gathering = !dtls_transports.empty();
Jonas Olsson635474e2018-10-18 15:58:17 +02001442
1443 std::map<IceTransportState, int> ice_state_counts;
1444 std::map<cricket::DtlsTransportState, int> dtls_state_counts;
1445
Zhi Huange818b6e2018-02-22 15:26:27 -08001446 for (const auto& dtls : dtls_transports) {
Alex Loiko9289eda2018-11-23 16:18:59 +00001447 any_failed = any_failed || dtls->ice_transport()->GetState() ==
1448 cricket::IceTransportState::STATE_FAILED;
1449 all_connected = all_connected && dtls->writable();
1450 all_completed =
1451 all_completed && dtls->writable() &&
1452 dtls->ice_transport()->GetState() ==
1453 cricket::IceTransportState::STATE_COMPLETED &&
1454 dtls->ice_transport()->GetIceRole() == cricket::ICEROLE_CONTROLLING &&
1455 dtls->ice_transport()->gathering_state() ==
1456 cricket::kIceGatheringComplete;
Zhi Huange818b6e2018-02-22 15:26:27 -08001457 any_gathering = any_gathering || dtls->ice_transport()->gathering_state() !=
1458 cricket::kIceGatheringNew;
1459 all_done_gathering =
1460 all_done_gathering && dtls->ice_transport()->gathering_state() ==
1461 cricket::kIceGatheringComplete;
Jonas Olsson635474e2018-10-18 15:58:17 +02001462
1463 dtls_state_counts[dtls->dtls_state()]++;
1464 ice_state_counts[dtls->ice_transport()->GetIceTransportState()]++;
Zhi Huange818b6e2018-02-22 15:26:27 -08001465 }
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -07001466
Alex Loiko9289eda2018-11-23 16:18:59 +00001467 for (auto it = jsep_transports_by_name_.begin();
1468 it != jsep_transports_by_name_.end(); ++it) {
1469 auto jsep_transport = it->second.get();
1470 if (!jsep_transport->media_transport()) {
1471 continue;
1472 }
1473
1474 // There is no 'kIceConnectionDisconnected', so we only need to handle
1475 // connected and completed.
1476 // We treat kClosed as failed, because if it happens before shutting down
1477 // media transports it means that there was a failure.
1478 // MediaTransportInterface allows to flip back and forth between kWritable
1479 // and kPending, but there does not exist an implementation that does that,
1480 // and the contract of jsep transport controller doesn't quite expect that.
1481 // When this happens, we would go from connected to connecting state, but
1482 // this may change in future.
1483 any_failed |= jsep_transport->media_transport_state() ==
1484 webrtc::MediaTransportState::kClosed;
1485 all_completed &= jsep_transport->media_transport_state() ==
1486 webrtc::MediaTransportState::kWritable;
1487 all_connected &= jsep_transport->media_transport_state() ==
1488 webrtc::MediaTransportState::kWritable;
1489 }
1490
1491 if (any_failed) {
1492 new_connection_state = cricket::kIceConnectionFailed;
1493 } else if (all_completed) {
1494 new_connection_state = cricket::kIceConnectionCompleted;
1495 } else if (all_connected) {
1496 new_connection_state = cricket::kIceConnectionConnected;
1497 }
1498 if (ice_connection_state_ != new_connection_state) {
1499 ice_connection_state_ = new_connection_state;
1500 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
1501 [this, new_connection_state] {
1502 SignalIceConnectionState(new_connection_state);
1503 });
1504 }
1505
Jonas Olsson635474e2018-10-18 15:58:17 +02001506 // Compute the current RTCIceConnectionState as described in
1507 // https://www.w3.org/TR/webrtc/#dom-rtciceconnectionstate.
1508 // The PeerConnection is responsible for handling the "closed" state.
1509 int total_ice_checking = ice_state_counts[IceTransportState::kChecking];
1510 int total_ice_connected = ice_state_counts[IceTransportState::kConnected];
1511 int total_ice_completed = ice_state_counts[IceTransportState::kCompleted];
1512 int total_ice_failed = ice_state_counts[IceTransportState::kFailed];
1513 int total_ice_disconnected =
1514 ice_state_counts[IceTransportState::kDisconnected];
1515 int total_ice_closed = ice_state_counts[IceTransportState::kClosed];
1516 int total_ice_new = ice_state_counts[IceTransportState::kNew];
1517 int total_ice = dtls_transports.size();
1518
1519 if (total_ice_failed > 0) {
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001520 // Any RTCIceTransports are in the "failed" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001521 new_ice_connection_state = PeerConnectionInterface::kIceConnectionFailed;
Alex Loiko9289eda2018-11-23 16:18:59 +00001522 } else if (total_ice_disconnected > 0) {
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001523 // None of the previous states apply and any RTCIceTransports are in the
1524 // "disconnected" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001525 new_ice_connection_state =
1526 PeerConnectionInterface::kIceConnectionDisconnected;
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001527 } else if (total_ice_new + total_ice_closed == total_ice) {
1528 // None of the previous states apply and all RTCIceTransports are in the
1529 // "new" or "closed" state, or there are no transports.
1530 new_ice_connection_state = PeerConnectionInterface::kIceConnectionNew;
1531 } else if (total_ice_new + total_ice_checking > 0) {
1532 // None of the previous states apply and any RTCIceTransports are in the
1533 // "new" or "checking" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001534 new_ice_connection_state = PeerConnectionInterface::kIceConnectionChecking;
Jonas Olssonacd8ae72019-02-25 15:26:24 +01001535 } else if (total_ice_completed + total_ice_closed == total_ice ||
1536 all_completed) {
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001537 // None of the previous states apply and all RTCIceTransports are in the
1538 // "completed" or "closed" state.
Jonas Olssonacd8ae72019-02-25 15:26:24 +01001539 //
1540 // TODO(https://bugs.webrtc.org/10356): The all_completed condition is added
1541 // to mimic the behavior of the old ICE connection state, and should be
1542 // removed once we get end-of-candidates signaling in place.
Jonas Olsson635474e2018-10-18 15:58:17 +02001543 new_ice_connection_state = PeerConnectionInterface::kIceConnectionCompleted;
1544 } else if (total_ice_connected + total_ice_completed + total_ice_closed ==
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001545 total_ice) {
1546 // None of the previous states apply and all RTCIceTransports are in the
1547 // "connected", "completed" or "closed" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001548 new_ice_connection_state = PeerConnectionInterface::kIceConnectionConnected;
Jonas Olsson635474e2018-10-18 15:58:17 +02001549 } else {
1550 RTC_NOTREACHED();
1551 }
1552
Alex Loiko9289eda2018-11-23 16:18:59 +00001553 if (standardized_ice_connection_state_ != new_ice_connection_state) {
Jonas Olssonacd8ae72019-02-25 15:26:24 +01001554 if (standardized_ice_connection_state_ ==
1555 PeerConnectionInterface::kIceConnectionChecking &&
1556 new_ice_connection_state ==
1557 PeerConnectionInterface::kIceConnectionCompleted) {
1558 // Ensure that we never skip over the "connected" state.
1559 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_, [this] {
1560 SignalStandardizedIceConnectionState(
1561 PeerConnectionInterface::kIceConnectionConnected);
1562 });
1563 }
Alex Loiko9289eda2018-11-23 16:18:59 +00001564 standardized_ice_connection_state_ = new_ice_connection_state;
Jonas Olsson635474e2018-10-18 15:58:17 +02001565 invoker_.AsyncInvoke<void>(
1566 RTC_FROM_HERE, signaling_thread_, [this, new_ice_connection_state] {
Alex Loiko9289eda2018-11-23 16:18:59 +00001567 SignalStandardizedIceConnectionState(new_ice_connection_state);
Jonas Olsson635474e2018-10-18 15:58:17 +02001568 });
1569 }
1570
1571 // Compute the current RTCPeerConnectionState as described in
1572 // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnectionstate.
1573 // The PeerConnection is responsible for handling the "closed" state.
1574 // Note that "connecting" is only a valid state for DTLS transports while
1575 // "checking", "completed" and "disconnected" are only valid for ICE
1576 // transports.
1577 int total_connected = total_ice_connected +
1578 dtls_state_counts[cricket::DTLS_TRANSPORT_CONNECTED];
1579 int total_dtls_connecting =
1580 dtls_state_counts[cricket::DTLS_TRANSPORT_CONNECTING];
1581 int total_failed =
1582 total_ice_failed + dtls_state_counts[cricket::DTLS_TRANSPORT_FAILED];
1583 int total_closed =
1584 total_ice_closed + dtls_state_counts[cricket::DTLS_TRANSPORT_CLOSED];
1585 int total_new =
1586 total_ice_new + dtls_state_counts[cricket::DTLS_TRANSPORT_NEW];
1587 int total_transports = total_ice * 2;
1588
1589 if (total_failed > 0) {
1590 // Any of the RTCIceTransports or RTCDtlsTransports are in a "failed" state.
1591 new_combined_state = PeerConnectionInterface::PeerConnectionState::kFailed;
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001592 } else if (total_ice_disconnected > 0) {
1593 // None of the previous states apply and any RTCIceTransports or
1594 // RTCDtlsTransports are in the "disconnected" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001595 new_combined_state =
1596 PeerConnectionInterface::PeerConnectionState::kDisconnected;
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001597 } else if (total_new + total_closed == total_transports) {
1598 // None of the previous states apply and all RTCIceTransports and
1599 // RTCDtlsTransports are in the "new" or "closed" state, or there are no
1600 // transports.
1601 new_combined_state = PeerConnectionInterface::PeerConnectionState::kNew;
1602 } else if (total_new + total_dtls_connecting + total_ice_checking > 0) {
1603 // None of the previous states apply and all RTCIceTransports or
1604 // RTCDtlsTransports are in the "new", "connecting" or "checking" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001605 new_combined_state =
1606 PeerConnectionInterface::PeerConnectionState::kConnecting;
1607 } else if (total_connected + total_ice_completed + total_closed ==
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001608 total_transports) {
1609 // None of the previous states apply and all RTCIceTransports and
1610 // RTCDtlsTransports are in the "connected", "completed" or "closed" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001611 new_combined_state =
1612 PeerConnectionInterface::PeerConnectionState::kConnected;
Jonas Olsson635474e2018-10-18 15:58:17 +02001613 } else {
1614 RTC_NOTREACHED();
1615 }
1616
1617 if (combined_connection_state_ != new_combined_state) {
1618 combined_connection_state_ = new_combined_state;
1619 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
1620 [this, new_combined_state] {
1621 SignalConnectionState(new_combined_state);
1622 });
1623 }
1624
Zhi Huange818b6e2018-02-22 15:26:27 -08001625 if (all_done_gathering) {
1626 new_gathering_state = cricket::kIceGatheringComplete;
1627 } else if (any_gathering) {
1628 new_gathering_state = cricket::kIceGatheringGathering;
1629 }
1630 if (ice_gathering_state_ != new_gathering_state) {
1631 ice_gathering_state_ = new_gathering_state;
Steve Antond25828a2018-08-31 13:06:05 -07001632 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
1633 [this, new_gathering_state] {
1634 SignalIceGatheringState(new_gathering_state);
1635 });
Zhi Huange818b6e2018-02-22 15:26:27 -08001636 }
1637}
1638
1639void JsepTransportController::OnDtlsHandshakeError(
1640 rtc::SSLHandshakeError error) {
1641 SignalDtlsHandshakeError(error);
1642}
1643
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001644absl::optional<cricket::SessionDescription::MediaTransportSetting>
1645JsepTransportController::GenerateOrGetLastMediaTransportOffer() {
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001646 if (media_transport_created_once_ || datagram_transport_created_once_) {
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001647 RTC_LOG(LS_INFO) << "Not regenerating media transport for the new offer in "
1648 "existing session.";
1649 return media_transport_offer_settings_;
1650 }
1651
1652 RTC_LOG(LS_INFO) << "Generating media transport offer!";
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001653
1654 absl::optional<std::string> transport_parameters;
1655
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001656 // Check that media transport is supposed to be used.
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001657 // Note that ICE is not available when media transport is created. It will
1658 // only be available in 'Connect'. This may be a potential server config, if
1659 // we decide to use this peer connection as a caller, not as a callee.
1660 // TODO(sukhanov): Avoid code duplication with CreateMedia/MediaTransport.
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001661 if (config_.use_media_transport_for_media ||
1662 config_.use_media_transport_for_data_channels) {
1663 RTC_DCHECK(config_.media_transport_factory != nullptr);
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001664 RTC_DCHECK(!config_.use_datagram_transport);
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001665 webrtc::MediaTransportSettings settings;
1666 settings.is_caller = true;
1667 settings.pre_shared_key = rtc::CreateRandomString(32);
1668 settings.event_log = config_.event_log;
1669 auto media_transport_or_error =
1670 config_.media_transport_factory->CreateMediaTransport(network_thread_,
1671 settings);
1672
1673 if (media_transport_or_error.ok()) {
1674 offer_media_transport_ = std::move(media_transport_or_error.value());
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001675 transport_parameters =
1676 offer_media_transport_->GetTransportParametersOffer();
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001677 } else {
1678 RTC_LOG(LS_INFO) << "Unable to create media transport, error="
1679 << media_transport_or_error.error().message();
1680 }
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001681 } else if (config_.use_datagram_transport) {
1682 webrtc::MediaTransportSettings settings;
1683 settings.is_caller = true;
1684 settings.pre_shared_key = rtc::CreateRandomString(32);
1685 settings.event_log = config_.event_log;
1686 auto datagram_transport_or_error =
1687 config_.media_transport_factory->CreateDatagramTransport(
1688 network_thread_, settings);
1689
1690 if (datagram_transport_or_error.ok()) {
1691 offer_datagram_transport_ =
1692 std::move(datagram_transport_or_error.value());
1693 transport_parameters =
1694 offer_datagram_transport_->GetTransportParametersOffer();
1695 } else {
1696 RTC_LOG(LS_INFO) << "Unable to create media transport, error="
1697 << datagram_transport_or_error.error().message();
1698 }
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001699 }
1700
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001701 if (!offer_media_transport_ && !offer_datagram_transport_) {
1702 RTC_LOG(LS_INFO) << "Media and data transports do not exist";
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001703 return absl::nullopt;
1704 }
1705
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001706 if (!transport_parameters) {
1707 RTC_LOG(LS_INFO) << "Media transport didn't generate the offer";
1708 // Media transport didn't generate the offer, and is not supposed to be
1709 // used. Destroy the temporary media transport.
1710 offer_media_transport_ = nullptr;
1711 return absl::nullopt;
1712 }
1713
1714 cricket::SessionDescription::MediaTransportSetting setting;
1715 setting.transport_name = config_.media_transport_factory->GetTransportName();
1716 setting.transport_setting = *transport_parameters;
1717 media_transport_offer_settings_ = setting;
1718 return setting;
1719}
1720
Zhi Huange818b6e2018-02-22 15:26:27 -08001721} // namespace webrtc