blob: 4f14e00af2e507cb62cd313575cd91e05ac80637 [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(
Bjorn A Mellem0c1c1b42019-05-29 17:34:13 -0700477 cricket::IceTransportInternal* ice,
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700478 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>(
Bjorn A Mellem0c1c1b42019-05-29 17:34:13 -0700488 ice, std::move(datagram_transport), config_.crypto_options,
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700489 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>(
Bjorn A Mellem0c1c1b42019-05-29 17:34:13 -0700497 ice, config_.crypto_options);
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -0800498 } else if (config_.external_transport_factory) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800499 dtls = config_.external_transport_factory->CreateDtlsTransport(
Bjorn A Mellem0c1c1b42019-05-29 17:34:13 -0700500 ice, config_.crypto_options);
Zhi Huange818b6e2018-02-22 15:26:27 -0800501 } else {
Zach Steinc64078f2018-11-27 15:53:01 -0800502 dtls = absl::make_unique<cricket::DtlsTransport>(
Bjorn A Mellem0c1c1b42019-05-29 17:34:13 -0700503 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 =
Bjorn A Mellem0c1c1b42019-05-29 17:34:13 -07001173 CreateDtlsTransport(ice.get(), 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
Bjorn A Mellem0c1c1b42019-05-29 17:34:13 -07001180 std::unique_ptr<cricket::IceTransportInternal> rtcp_ice;
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001181 if (config_.rtcp_mux_policy !=
1182 PeerConnectionInterface::kRtcpMuxPolicyRequire &&
1183 content_info.type == cricket::MediaProtocolType::kRtp) {
Piotr (Peter) Slatala2b5baee2019-01-16 08:25:21 -08001184 RTC_DCHECK(media_transport == nullptr);
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001185 RTC_DCHECK(datagram_transport == nullptr);
Bjorn A Mellem0c1c1b42019-05-29 17:34:13 -07001186 rtcp_ice = CreateIceTransport(content_info.name, /*rtcp=*/true);
1187 rtcp_dtls_transport = CreateDtlsTransport(rtcp_ice.get(),
1188 /*datagram_transport=*/nullptr);
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001189 }
Piotr (Peter) Slatala47dfdca2018-11-16 14:13:58 -08001190
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001191 if (datagram_transport) {
1192 // TODO(sukhanov): We use unencrypted RTP transport over DatagramTransport,
1193 // because MediaTransport encrypts. In the future we may want to
1194 // implement our own version of RtpTransport over MediaTransport, because
1195 // it will give us more control over things like:
1196 // - Fusing
1197 // - Rtp header compression
1198 // - Handling Rtcp feedback.
1199 RTC_LOG(LS_INFO) << "Creating UnencryptedRtpTransport, because datagram "
1200 "transport is used.";
1201 RTC_DCHECK(!rtcp_dtls_transport);
1202 unencrypted_rtp_transport = CreateUnencryptedRtpTransport(
1203 content_info.name, rtp_dtls_transport.get(), rtcp_dtls_transport.get());
1204 } else if (config_.disable_encryption) {
1205 RTC_LOG(LS_INFO)
1206 << "Creating UnencryptedRtpTransport, becayse encryption is disabled.";
Zhi Huange818b6e2018-02-22 15:26:27 -08001207 unencrypted_rtp_transport = CreateUnencryptedRtpTransport(
Zhi Huangd2248f82018-04-10 14:41:03 -07001208 content_info.name, rtp_dtls_transport.get(), rtcp_dtls_transport.get());
Zhi Huange818b6e2018-02-22 15:26:27 -08001209 } else if (!content_desc->cryptos().empty()) {
Zhi Huangd2248f82018-04-10 14:41:03 -07001210 sdes_transport = CreateSdesTransport(
1211 content_info.name, rtp_dtls_transport.get(), rtcp_dtls_transport.get());
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001212 RTC_LOG(LS_INFO) << "Creating SdesTransport.";
Zhi Huange818b6e2018-02-22 15:26:27 -08001213 } else {
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001214 RTC_LOG(LS_INFO) << "Creating DtlsSrtpTransport.";
Zhi Huangd2248f82018-04-10 14:41:03 -07001215 dtls_srtp_transport = CreateDtlsSrtpTransport(
1216 content_info.name, rtp_dtls_transport.get(), rtcp_dtls_transport.get());
Zhi Huange818b6e2018-02-22 15:26:27 -08001217 }
1218
Zhi Huang365381f2018-04-13 16:44:34 -07001219 std::unique_ptr<cricket::JsepTransport> jsep_transport =
Karl Wiberg918f50c2018-07-05 11:40:33 +02001220 absl::make_unique<cricket::JsepTransport>(
Bjorn A Mellem0c1c1b42019-05-29 17:34:13 -07001221 content_info.name, certificate_, std::move(ice), std::move(rtcp_ice),
1222 std::move(unencrypted_rtp_transport), std::move(sdes_transport),
1223 std::move(dtls_srtp_transport), std::move(rtp_dtls_transport),
1224 std::move(rtcp_dtls_transport), std::move(media_transport));
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001225
Zhi Huange818b6e2018-02-22 15:26:27 -08001226 jsep_transport->SignalRtcpMuxActive.connect(
1227 this, &JsepTransportController::UpdateAggregateStates_n);
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -07001228 jsep_transport->SignalMediaTransportStateChanged.connect(
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001229 this, &JsepTransportController::OnMediaTransportStateChanged_n);
Taylor Brandstettercbaa2542018-04-16 16:42:14 -07001230 SetTransportForMid(content_info.name, jsep_transport.get());
Zhi Huange830e682018-03-30 10:48:35 -07001231
Zhi Huangd2248f82018-04-10 14:41:03 -07001232 jsep_transports_by_name_[content_info.name] = std::move(jsep_transport);
1233 UpdateAggregateStates_n();
Zhi Huange830e682018-03-30 10:48:35 -07001234 return RTCError::OK();
Zhi Huange818b6e2018-02-22 15:26:27 -08001235}
1236
1237void JsepTransportController::MaybeDestroyJsepTransport(
1238 const std::string& mid) {
Zhi Huangd2248f82018-04-10 14:41:03 -07001239 auto jsep_transport = GetJsepTransportByName(mid);
1240 if (!jsep_transport) {
1241 return;
1242 }
1243
1244 // Don't destroy the JsepTransport if there are still media sections referring
1245 // to it.
1246 for (const auto& kv : mid_to_transport_) {
1247 if (kv.second == jsep_transport) {
1248 return;
1249 }
1250 }
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -08001251
Zhi Huange830e682018-03-30 10:48:35 -07001252 jsep_transports_by_name_.erase(mid);
Zhi Huange818b6e2018-02-22 15:26:27 -08001253 UpdateAggregateStates_n();
1254}
1255
1256void JsepTransportController::DestroyAllJsepTransports_n() {
1257 RTC_DCHECK(network_thread_->IsCurrent());
Piotr (Peter) Slatalacc8e8bb2018-11-15 08:26:19 -08001258
1259 for (const auto& jsep_transport : jsep_transports_by_name_) {
1260 config_.transport_observer->OnTransportChanged(jsep_transport.first,
1261 nullptr, nullptr, nullptr);
1262 }
1263
Zhi Huange830e682018-03-30 10:48:35 -07001264 jsep_transports_by_name_.clear();
Zhi Huange818b6e2018-02-22 15:26:27 -08001265}
1266
1267void JsepTransportController::SetIceRole_n(cricket::IceRole ice_role) {
1268 RTC_DCHECK(network_thread_->IsCurrent());
1269
1270 ice_role_ = ice_role;
1271 for (auto& dtls : GetDtlsTransports()) {
1272 dtls->ice_transport()->SetIceRole(ice_role_);
1273 }
1274}
1275
1276cricket::IceRole JsepTransportController::DetermineIceRole(
Zhi Huang365381f2018-04-13 16:44:34 -07001277 cricket::JsepTransport* jsep_transport,
Zhi Huange818b6e2018-02-22 15:26:27 -08001278 const cricket::TransportInfo& transport_info,
1279 SdpType type,
1280 bool local) {
1281 cricket::IceRole ice_role = ice_role_;
1282 auto tdesc = transport_info.description;
1283 if (local) {
1284 // The initial offer side may use ICE Lite, in which case, per RFC5245
1285 // Section 5.1.1, the answer side should take the controlling role if it is
1286 // in the full ICE mode.
1287 //
1288 // When both sides use ICE Lite, the initial offer side must take the
1289 // controlling role, and this is the default logic implemented in
1290 // SetLocalDescription in JsepTransportController.
1291 if (jsep_transport->remote_description() &&
1292 jsep_transport->remote_description()->transport_desc.ice_mode ==
1293 cricket::ICEMODE_LITE &&
1294 ice_role_ == cricket::ICEROLE_CONTROLLED &&
1295 tdesc.ice_mode == cricket::ICEMODE_FULL) {
1296 ice_role = cricket::ICEROLE_CONTROLLING;
1297 }
1298
1299 // Older versions of Chrome expect the ICE role to be re-determined when an
1300 // ICE restart occurs, and also don't perform conflict resolution correctly,
1301 // so for now we can't safely stop doing this, unless the application opts
1302 // in by setting |config_.redetermine_role_on_ice_restart_| to false. See:
1303 // https://bugs.chromium.org/p/chromium/issues/detail?id=628676
1304 // TODO(deadbeef): Remove this when these old versions of Chrome reach a low
1305 // enough population.
1306 if (config_.redetermine_role_on_ice_restart &&
1307 jsep_transport->local_description() &&
1308 cricket::IceCredentialsChanged(
1309 jsep_transport->local_description()->transport_desc.ice_ufrag,
1310 jsep_transport->local_description()->transport_desc.ice_pwd,
1311 tdesc.ice_ufrag, tdesc.ice_pwd) &&
1312 // Don't change the ICE role if the remote endpoint is ICE lite; we
1313 // should always be controlling in that case.
1314 (!jsep_transport->remote_description() ||
1315 jsep_transport->remote_description()->transport_desc.ice_mode !=
1316 cricket::ICEMODE_LITE)) {
1317 ice_role = (type == SdpType::kOffer) ? cricket::ICEROLE_CONTROLLING
1318 : cricket::ICEROLE_CONTROLLED;
1319 }
1320 } else {
1321 // If our role is cricket::ICEROLE_CONTROLLED and the remote endpoint
1322 // supports only ice_lite, this local endpoint should take the CONTROLLING
1323 // role.
1324 // TODO(deadbeef): This is a session-level attribute, so it really shouldn't
1325 // be in a TransportDescription in the first place...
1326 if (ice_role_ == cricket::ICEROLE_CONTROLLED &&
1327 tdesc.ice_mode == cricket::ICEMODE_LITE) {
1328 ice_role = cricket::ICEROLE_CONTROLLING;
1329 }
1330
1331 // If we use ICE Lite and the remote endpoint uses the full implementation
1332 // of ICE, the local endpoint must take the controlled role, and the other
1333 // side must be the controlling role.
1334 if (jsep_transport->local_description() &&
1335 jsep_transport->local_description()->transport_desc.ice_mode ==
1336 cricket::ICEMODE_LITE &&
1337 ice_role_ == cricket::ICEROLE_CONTROLLING &&
Zhi Huange830e682018-03-30 10:48:35 -07001338 tdesc.ice_mode == cricket::ICEMODE_FULL) {
Zhi Huange818b6e2018-02-22 15:26:27 -08001339 ice_role = cricket::ICEROLE_CONTROLLED;
1340 }
1341 }
1342
1343 return ice_role;
1344}
1345
1346void JsepTransportController::OnTransportWritableState_n(
1347 rtc::PacketTransportInternal* transport) {
1348 RTC_DCHECK(network_thread_->IsCurrent());
1349 RTC_LOG(LS_INFO) << " Transport " << transport->transport_name()
1350 << " writability changed to " << transport->writable()
1351 << ".";
1352 UpdateAggregateStates_n();
1353}
1354
1355void JsepTransportController::OnTransportReceivingState_n(
1356 rtc::PacketTransportInternal* transport) {
1357 RTC_DCHECK(network_thread_->IsCurrent());
1358 UpdateAggregateStates_n();
1359}
1360
1361void JsepTransportController::OnTransportGatheringState_n(
1362 cricket::IceTransportInternal* transport) {
1363 RTC_DCHECK(network_thread_->IsCurrent());
1364 UpdateAggregateStates_n();
1365}
1366
1367void JsepTransportController::OnTransportCandidateGathered_n(
1368 cricket::IceTransportInternal* transport,
1369 const cricket::Candidate& candidate) {
1370 RTC_DCHECK(network_thread_->IsCurrent());
1371
1372 // We should never signal peer-reflexive candidates.
1373 if (candidate.type() == cricket::PRFLX_PORT_TYPE) {
1374 RTC_NOTREACHED();
1375 return;
1376 }
Steve Antond25828a2018-08-31 13:06:05 -07001377 std::string transport_name = transport->transport_name();
1378 invoker_.AsyncInvoke<void>(
1379 RTC_FROM_HERE, signaling_thread_, [this, transport_name, candidate] {
1380 SignalIceCandidatesGathered(transport_name, {candidate});
1381 });
Zhi Huange818b6e2018-02-22 15:26:27 -08001382}
1383
1384void JsepTransportController::OnTransportCandidatesRemoved_n(
1385 cricket::IceTransportInternal* transport,
1386 const cricket::Candidates& candidates) {
1387 invoker_.AsyncInvoke<void>(
1388 RTC_FROM_HERE, signaling_thread_,
Steve Antond25828a2018-08-31 13:06:05 -07001389 [this, candidates] { SignalIceCandidatesRemoved(candidates); });
Zhi Huange818b6e2018-02-22 15:26:27 -08001390}
1391
1392void JsepTransportController::OnTransportRoleConflict_n(
1393 cricket::IceTransportInternal* transport) {
1394 RTC_DCHECK(network_thread_->IsCurrent());
1395 // Note: since the role conflict is handled entirely on the network thread,
1396 // we don't need to worry about role conflicts occurring on two ports at
1397 // once. The first one encountered should immediately reverse the role.
1398 cricket::IceRole reversed_role = (ice_role_ == cricket::ICEROLE_CONTROLLING)
1399 ? cricket::ICEROLE_CONTROLLED
1400 : cricket::ICEROLE_CONTROLLING;
1401 RTC_LOG(LS_INFO) << "Got role conflict; switching to "
1402 << (reversed_role == cricket::ICEROLE_CONTROLLING
1403 ? "controlling"
1404 : "controlled")
1405 << " role.";
1406 SetIceRole_n(reversed_role);
1407}
1408
1409void JsepTransportController::OnTransportStateChanged_n(
1410 cricket::IceTransportInternal* transport) {
1411 RTC_DCHECK(network_thread_->IsCurrent());
1412 RTC_LOG(LS_INFO) << transport->transport_name() << " Transport "
1413 << transport->component()
1414 << " state changed. Check if state is complete.";
1415 UpdateAggregateStates_n();
1416}
1417
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001418void JsepTransportController::OnMediaTransportStateChanged_n() {
1419 SignalMediaTransportStateChanged();
1420 UpdateAggregateStates_n();
1421}
1422
Zhi Huange818b6e2018-02-22 15:26:27 -08001423void JsepTransportController::UpdateAggregateStates_n() {
1424 RTC_DCHECK(network_thread_->IsCurrent());
1425
1426 auto dtls_transports = GetDtlsTransports();
Alex Loiko9289eda2018-11-23 16:18:59 +00001427 cricket::IceConnectionState new_connection_state =
1428 cricket::kIceConnectionConnecting;
Jonas Olsson635474e2018-10-18 15:58:17 +02001429 PeerConnectionInterface::IceConnectionState new_ice_connection_state =
1430 PeerConnectionInterface::IceConnectionState::kIceConnectionNew;
1431 PeerConnectionInterface::PeerConnectionState new_combined_state =
1432 PeerConnectionInterface::PeerConnectionState::kNew;
Zhi Huange818b6e2018-02-22 15:26:27 -08001433 cricket::IceGatheringState new_gathering_state = cricket::kIceGatheringNew;
Alex Loiko9289eda2018-11-23 16:18:59 +00001434 bool any_failed = false;
1435
1436 // TODO(http://bugs.webrtc.org/9719) If(when) media_transport disables
1437 // dtls_transports entirely, the below line will have to be changed to account
1438 // for the fact that dtls transports might be absent.
1439 bool all_connected = !dtls_transports.empty();
1440 bool all_completed = !dtls_transports.empty();
Zhi Huange818b6e2018-02-22 15:26:27 -08001441 bool any_gathering = false;
1442 bool all_done_gathering = !dtls_transports.empty();
Jonas Olsson635474e2018-10-18 15:58:17 +02001443
1444 std::map<IceTransportState, int> ice_state_counts;
1445 std::map<cricket::DtlsTransportState, int> dtls_state_counts;
1446
Zhi Huange818b6e2018-02-22 15:26:27 -08001447 for (const auto& dtls : dtls_transports) {
Alex Loiko9289eda2018-11-23 16:18:59 +00001448 any_failed = any_failed || dtls->ice_transport()->GetState() ==
1449 cricket::IceTransportState::STATE_FAILED;
1450 all_connected = all_connected && dtls->writable();
1451 all_completed =
1452 all_completed && dtls->writable() &&
1453 dtls->ice_transport()->GetState() ==
1454 cricket::IceTransportState::STATE_COMPLETED &&
1455 dtls->ice_transport()->GetIceRole() == cricket::ICEROLE_CONTROLLING &&
1456 dtls->ice_transport()->gathering_state() ==
1457 cricket::kIceGatheringComplete;
Zhi Huange818b6e2018-02-22 15:26:27 -08001458 any_gathering = any_gathering || dtls->ice_transport()->gathering_state() !=
1459 cricket::kIceGatheringNew;
1460 all_done_gathering =
1461 all_done_gathering && dtls->ice_transport()->gathering_state() ==
1462 cricket::kIceGatheringComplete;
Jonas Olsson635474e2018-10-18 15:58:17 +02001463
1464 dtls_state_counts[dtls->dtls_state()]++;
1465 ice_state_counts[dtls->ice_transport()->GetIceTransportState()]++;
Zhi Huange818b6e2018-02-22 15:26:27 -08001466 }
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -07001467
Alex Loiko9289eda2018-11-23 16:18:59 +00001468 for (auto it = jsep_transports_by_name_.begin();
1469 it != jsep_transports_by_name_.end(); ++it) {
1470 auto jsep_transport = it->second.get();
1471 if (!jsep_transport->media_transport()) {
1472 continue;
1473 }
1474
1475 // There is no 'kIceConnectionDisconnected', so we only need to handle
1476 // connected and completed.
1477 // We treat kClosed as failed, because if it happens before shutting down
1478 // media transports it means that there was a failure.
1479 // MediaTransportInterface allows to flip back and forth between kWritable
1480 // and kPending, but there does not exist an implementation that does that,
1481 // and the contract of jsep transport controller doesn't quite expect that.
1482 // When this happens, we would go from connected to connecting state, but
1483 // this may change in future.
1484 any_failed |= jsep_transport->media_transport_state() ==
1485 webrtc::MediaTransportState::kClosed;
1486 all_completed &= jsep_transport->media_transport_state() ==
1487 webrtc::MediaTransportState::kWritable;
1488 all_connected &= jsep_transport->media_transport_state() ==
1489 webrtc::MediaTransportState::kWritable;
1490 }
1491
1492 if (any_failed) {
1493 new_connection_state = cricket::kIceConnectionFailed;
1494 } else if (all_completed) {
1495 new_connection_state = cricket::kIceConnectionCompleted;
1496 } else if (all_connected) {
1497 new_connection_state = cricket::kIceConnectionConnected;
1498 }
1499 if (ice_connection_state_ != new_connection_state) {
1500 ice_connection_state_ = new_connection_state;
1501 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
1502 [this, new_connection_state] {
1503 SignalIceConnectionState(new_connection_state);
1504 });
1505 }
1506
Jonas Olsson635474e2018-10-18 15:58:17 +02001507 // Compute the current RTCIceConnectionState as described in
1508 // https://www.w3.org/TR/webrtc/#dom-rtciceconnectionstate.
1509 // The PeerConnection is responsible for handling the "closed" state.
1510 int total_ice_checking = ice_state_counts[IceTransportState::kChecking];
1511 int total_ice_connected = ice_state_counts[IceTransportState::kConnected];
1512 int total_ice_completed = ice_state_counts[IceTransportState::kCompleted];
1513 int total_ice_failed = ice_state_counts[IceTransportState::kFailed];
1514 int total_ice_disconnected =
1515 ice_state_counts[IceTransportState::kDisconnected];
1516 int total_ice_closed = ice_state_counts[IceTransportState::kClosed];
1517 int total_ice_new = ice_state_counts[IceTransportState::kNew];
1518 int total_ice = dtls_transports.size();
1519
1520 if (total_ice_failed > 0) {
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001521 // Any RTCIceTransports are in the "failed" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001522 new_ice_connection_state = PeerConnectionInterface::kIceConnectionFailed;
Alex Loiko9289eda2018-11-23 16:18:59 +00001523 } else if (total_ice_disconnected > 0) {
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001524 // None of the previous states apply and any RTCIceTransports are in the
1525 // "disconnected" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001526 new_ice_connection_state =
1527 PeerConnectionInterface::kIceConnectionDisconnected;
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001528 } else if (total_ice_new + total_ice_closed == total_ice) {
1529 // None of the previous states apply and all RTCIceTransports are in the
1530 // "new" or "closed" state, or there are no transports.
1531 new_ice_connection_state = PeerConnectionInterface::kIceConnectionNew;
1532 } else if (total_ice_new + total_ice_checking > 0) {
1533 // None of the previous states apply and any RTCIceTransports are in the
1534 // "new" or "checking" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001535 new_ice_connection_state = PeerConnectionInterface::kIceConnectionChecking;
Jonas Olssonacd8ae72019-02-25 15:26:24 +01001536 } else if (total_ice_completed + total_ice_closed == total_ice ||
1537 all_completed) {
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001538 // None of the previous states apply and all RTCIceTransports are in the
1539 // "completed" or "closed" state.
Jonas Olssonacd8ae72019-02-25 15:26:24 +01001540 //
1541 // TODO(https://bugs.webrtc.org/10356): The all_completed condition is added
1542 // to mimic the behavior of the old ICE connection state, and should be
1543 // removed once we get end-of-candidates signaling in place.
Jonas Olsson635474e2018-10-18 15:58:17 +02001544 new_ice_connection_state = PeerConnectionInterface::kIceConnectionCompleted;
1545 } else if (total_ice_connected + total_ice_completed + total_ice_closed ==
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001546 total_ice) {
1547 // None of the previous states apply and all RTCIceTransports are in the
1548 // "connected", "completed" or "closed" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001549 new_ice_connection_state = PeerConnectionInterface::kIceConnectionConnected;
Jonas Olsson635474e2018-10-18 15:58:17 +02001550 } else {
1551 RTC_NOTREACHED();
1552 }
1553
Alex Loiko9289eda2018-11-23 16:18:59 +00001554 if (standardized_ice_connection_state_ != new_ice_connection_state) {
Jonas Olssonacd8ae72019-02-25 15:26:24 +01001555 if (standardized_ice_connection_state_ ==
1556 PeerConnectionInterface::kIceConnectionChecking &&
1557 new_ice_connection_state ==
1558 PeerConnectionInterface::kIceConnectionCompleted) {
1559 // Ensure that we never skip over the "connected" state.
1560 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_, [this] {
1561 SignalStandardizedIceConnectionState(
1562 PeerConnectionInterface::kIceConnectionConnected);
1563 });
1564 }
Alex Loiko9289eda2018-11-23 16:18:59 +00001565 standardized_ice_connection_state_ = new_ice_connection_state;
Jonas Olsson635474e2018-10-18 15:58:17 +02001566 invoker_.AsyncInvoke<void>(
1567 RTC_FROM_HERE, signaling_thread_, [this, new_ice_connection_state] {
Alex Loiko9289eda2018-11-23 16:18:59 +00001568 SignalStandardizedIceConnectionState(new_ice_connection_state);
Jonas Olsson635474e2018-10-18 15:58:17 +02001569 });
1570 }
1571
1572 // Compute the current RTCPeerConnectionState as described in
1573 // https://www.w3.org/TR/webrtc/#dom-rtcpeerconnectionstate.
1574 // The PeerConnection is responsible for handling the "closed" state.
1575 // Note that "connecting" is only a valid state for DTLS transports while
1576 // "checking", "completed" and "disconnected" are only valid for ICE
1577 // transports.
1578 int total_connected = total_ice_connected +
1579 dtls_state_counts[cricket::DTLS_TRANSPORT_CONNECTED];
1580 int total_dtls_connecting =
1581 dtls_state_counts[cricket::DTLS_TRANSPORT_CONNECTING];
1582 int total_failed =
1583 total_ice_failed + dtls_state_counts[cricket::DTLS_TRANSPORT_FAILED];
1584 int total_closed =
1585 total_ice_closed + dtls_state_counts[cricket::DTLS_TRANSPORT_CLOSED];
1586 int total_new =
1587 total_ice_new + dtls_state_counts[cricket::DTLS_TRANSPORT_NEW];
1588 int total_transports = total_ice * 2;
1589
1590 if (total_failed > 0) {
1591 // Any of the RTCIceTransports or RTCDtlsTransports are in a "failed" state.
1592 new_combined_state = PeerConnectionInterface::PeerConnectionState::kFailed;
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001593 } else if (total_ice_disconnected > 0) {
1594 // None of the previous states apply and any RTCIceTransports or
1595 // RTCDtlsTransports are in the "disconnected" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001596 new_combined_state =
1597 PeerConnectionInterface::PeerConnectionState::kDisconnected;
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001598 } else if (total_new + total_closed == total_transports) {
1599 // None of the previous states apply and all RTCIceTransports and
1600 // RTCDtlsTransports are in the "new" or "closed" state, or there are no
1601 // transports.
1602 new_combined_state = PeerConnectionInterface::PeerConnectionState::kNew;
1603 } else if (total_new + total_dtls_connecting + total_ice_checking > 0) {
1604 // None of the previous states apply and all RTCIceTransports or
1605 // RTCDtlsTransports are in the "new", "connecting" or "checking" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001606 new_combined_state =
1607 PeerConnectionInterface::PeerConnectionState::kConnecting;
1608 } else if (total_connected + total_ice_completed + total_closed ==
Jonas Olsson6a8727b2018-12-07 13:11:44 +01001609 total_transports) {
1610 // None of the previous states apply and all RTCIceTransports and
1611 // RTCDtlsTransports are in the "connected", "completed" or "closed" state.
Jonas Olsson635474e2018-10-18 15:58:17 +02001612 new_combined_state =
1613 PeerConnectionInterface::PeerConnectionState::kConnected;
Jonas Olsson635474e2018-10-18 15:58:17 +02001614 } else {
1615 RTC_NOTREACHED();
1616 }
1617
1618 if (combined_connection_state_ != new_combined_state) {
1619 combined_connection_state_ = new_combined_state;
1620 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
1621 [this, new_combined_state] {
1622 SignalConnectionState(new_combined_state);
1623 });
1624 }
1625
Zhi Huange818b6e2018-02-22 15:26:27 -08001626 if (all_done_gathering) {
1627 new_gathering_state = cricket::kIceGatheringComplete;
1628 } else if (any_gathering) {
1629 new_gathering_state = cricket::kIceGatheringGathering;
1630 }
1631 if (ice_gathering_state_ != new_gathering_state) {
1632 ice_gathering_state_ = new_gathering_state;
Steve Antond25828a2018-08-31 13:06:05 -07001633 invoker_.AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread_,
1634 [this, new_gathering_state] {
1635 SignalIceGatheringState(new_gathering_state);
1636 });
Zhi Huange818b6e2018-02-22 15:26:27 -08001637 }
1638}
1639
1640void JsepTransportController::OnDtlsHandshakeError(
1641 rtc::SSLHandshakeError error) {
1642 SignalDtlsHandshakeError(error);
1643}
1644
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001645absl::optional<cricket::SessionDescription::MediaTransportSetting>
1646JsepTransportController::GenerateOrGetLastMediaTransportOffer() {
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001647 if (media_transport_created_once_ || datagram_transport_created_once_) {
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001648 RTC_LOG(LS_INFO) << "Not regenerating media transport for the new offer in "
1649 "existing session.";
1650 return media_transport_offer_settings_;
1651 }
1652
1653 RTC_LOG(LS_INFO) << "Generating media transport offer!";
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001654
1655 absl::optional<std::string> transport_parameters;
1656
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001657 // Check that media transport is supposed to be used.
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001658 // Note that ICE is not available when media transport is created. It will
1659 // only be available in 'Connect'. This may be a potential server config, if
1660 // we decide to use this peer connection as a caller, not as a callee.
1661 // TODO(sukhanov): Avoid code duplication with CreateMedia/MediaTransport.
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001662 if (config_.use_media_transport_for_media ||
1663 config_.use_media_transport_for_data_channels) {
1664 RTC_DCHECK(config_.media_transport_factory != nullptr);
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001665 RTC_DCHECK(!config_.use_datagram_transport);
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001666 webrtc::MediaTransportSettings settings;
1667 settings.is_caller = true;
1668 settings.pre_shared_key = rtc::CreateRandomString(32);
1669 settings.event_log = config_.event_log;
1670 auto media_transport_or_error =
1671 config_.media_transport_factory->CreateMediaTransport(network_thread_,
1672 settings);
1673
1674 if (media_transport_or_error.ok()) {
1675 offer_media_transport_ = std::move(media_transport_or_error.value());
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001676 transport_parameters =
1677 offer_media_transport_->GetTransportParametersOffer();
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001678 } else {
1679 RTC_LOG(LS_INFO) << "Unable to create media transport, error="
1680 << media_transport_or_error.error().message();
1681 }
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001682 } else if (config_.use_datagram_transport) {
1683 webrtc::MediaTransportSettings settings;
1684 settings.is_caller = true;
1685 settings.pre_shared_key = rtc::CreateRandomString(32);
1686 settings.event_log = config_.event_log;
1687 auto datagram_transport_or_error =
1688 config_.media_transport_factory->CreateDatagramTransport(
1689 network_thread_, settings);
1690
1691 if (datagram_transport_or_error.ok()) {
1692 offer_datagram_transport_ =
1693 std::move(datagram_transport_or_error.value());
1694 transport_parameters =
1695 offer_datagram_transport_->GetTransportParametersOffer();
1696 } else {
1697 RTC_LOG(LS_INFO) << "Unable to create media transport, error="
1698 << datagram_transport_or_error.error().message();
1699 }
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001700 }
1701
Anton Sukhanov316f3ac2019-05-23 15:50:38 -07001702 if (!offer_media_transport_ && !offer_datagram_transport_) {
1703 RTC_LOG(LS_INFO) << "Media and data transports do not exist";
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001704 return absl::nullopt;
1705 }
1706
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001707 if (!transport_parameters) {
1708 RTC_LOG(LS_INFO) << "Media transport didn't generate the offer";
1709 // Media transport didn't generate the offer, and is not supposed to be
1710 // used. Destroy the temporary media transport.
1711 offer_media_transport_ = nullptr;
1712 return absl::nullopt;
1713 }
1714
1715 cricket::SessionDescription::MediaTransportSetting setting;
1716 setting.transport_name = config_.media_transport_factory->GetTransportName();
1717 setting.transport_setting = *transport_parameters;
1718 media_transport_offer_settings_ = setting;
1719 return setting;
1720}
1721
Zhi Huange818b6e2018-02-22 15:26:27 -08001722} // namespace webrtc