blob: 93604a179f5b59c19a3ab6ecf85366f1c5a9a692 [file] [log] [blame]
Zhi Huange818b6e2018-02-22 23:26:271/*
2 * Copyright 2018 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 17:11:0011#ifndef PC_JSEP_TRANSPORT_H_
12#define PC_JSEP_TRANSPORT_H_
Zhi Huange818b6e2018-02-22 23:26:2713
Mirko Bonadei96dca922021-07-10 20:37:4014#include <functional>
Zhi Huange818b6e2018-02-22 23:26:2715#include <map>
16#include <memory>
17#include <string>
18#include <vector>
19
Danil Chapovalov66cadcc2018-06-19 14:47:4320#include "absl/types/optional.h"
Zhi Huange818b6e2018-02-22 23:26:2721#include "api/candidate.h"
Harald Alvestrand0d018412021-11-04 13:52:3122#include "api/crypto_params.h"
Qingsi Wang25ec8882019-11-15 20:33:0523#include "api/ice_transport_interface.h"
Zhi Huange818b6e2018-02-22 23:26:2724#include "api/jsep.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0825#include "api/rtc_error.h"
26#include "api/scoped_refptr.h"
Artem Titovd15a5752021-02-10 13:31:2427#include "api/sequence_checker.h"
Niels Möllerc888ffa2020-07-14 11:21:4228#include "api/transport/data_channel_transport_interface.h"
Bjorn A Mellembc3eebc2019-09-23 21:53:5429#include "media/sctp/sctp_transport_internal.h"
Steve Anton10542f22019-01-11 17:11:0030#include "p2p/base/dtls_transport.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0831#include "p2p/base/dtls_transport_internal.h"
32#include "p2p/base/ice_transport_internal.h"
Steve Anton10542f22019-01-11 17:11:0033#include "p2p/base/p2p_constants.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0834#include "p2p/base/transport_description.h"
Steve Anton10542f22019-01-11 17:11:0035#include "p2p/base/transport_info.h"
36#include "pc/dtls_srtp_transport.h"
37#include "pc/dtls_transport.h"
38#include "pc/rtcp_mux_filter.h"
39#include "pc/rtp_transport.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0840#include "pc/rtp_transport_internal.h"
Bjorn A Mellembc3eebc2019-09-23 21:53:5441#include "pc/sctp_transport.h"
Steve Anton10542f22019-01-11 17:11:0042#include "pc/session_description.h"
Harald Alvestrand0d018412021-11-04 13:52:3143#include "pc/srtp_filter.h"
44#include "pc/srtp_transport.h"
Steve Anton10542f22019-01-11 17:11:0045#include "pc/transport_stats.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0846#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 17:11:0047#include "rtc_base/rtc_certificate.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0848#include "rtc_base/ssl_fingerprint.h"
Steve Anton10542f22019-01-11 17:11:0049#include "rtc_base/ssl_stream_adapter.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0850#include "rtc_base/thread.h"
51#include "rtc_base/thread_annotations.h"
Zhi Huange818b6e2018-02-22 23:26:2752
53namespace cricket {
54
55class DtlsTransportInternal;
56
57struct JsepTransportDescription {
58 public:
59 JsepTransportDescription();
60 JsepTransportDescription(
61 bool rtcp_mux_enabled,
Harald Alvestrand0d018412021-11-04 13:52:3162 const std::vector<CryptoParams>& cryptos,
Zhi Huange818b6e2018-02-22 23:26:2763 const std::vector<int>& encrypted_header_extension_ids,
Zhi Huange830e682018-03-30 17:48:3564 int rtp_abs_sendtime_extn_id,
Niels Möllerdc80aaf2020-06-18 08:10:1765 const TransportDescription& transport_description);
Zhi Huange818b6e2018-02-22 23:26:2766 JsepTransportDescription(const JsepTransportDescription& from);
67 ~JsepTransportDescription();
68
69 JsepTransportDescription& operator=(const JsepTransportDescription& from);
70
71 bool rtcp_mux_enabled = true;
Harald Alvestrand0d018412021-11-04 13:52:3172 std::vector<CryptoParams> cryptos;
Zhi Huange818b6e2018-02-22 23:26:2773 std::vector<int> encrypted_header_extension_ids;
Zhi Huange830e682018-03-30 17:48:3574 int rtp_abs_sendtime_extn_id = -1;
Zhi Huange818b6e2018-02-22 23:26:2775 // TODO(zhihuang): Add the ICE and DTLS related variables and methods from
76 // TransportDescription and remove this extra layer of abstraction.
77 TransportDescription transport_desc;
78};
79
80// Helper class used by JsepTransportController that processes
81// TransportDescriptions. A TransportDescription represents the
82// transport-specific properties of an SDP m= section, processed according to
83// JSEP. Each transport consists of DTLS and ICE transport channels for RTP
84// (and possibly RTCP, if rtcp-mux isn't used).
85//
Zhi Huang365381f2018-04-13 23:44:3486// On Threading: JsepTransport performs work solely on the network thread, and
Zhi Huange818b6e2018-02-22 23:26:2787// so its methods should only be called on the network thread.
Mirko Bonadei96dca922021-07-10 20:37:4088class JsepTransport {
Zhi Huange818b6e2018-02-22 23:26:2789 public:
Artem Titov880fa812021-07-30 20:30:2390 // `mid` is just used for log statements in order to identify the Transport.
91 // Note that `local_certificate` is allowed to be null since a remote
Zhi Huange818b6e2018-02-22 23:26:2792 // description may be set before a local certificate is generated.
Zhi Huang365381f2018-04-13 23:44:3493 JsepTransport(
Zhi Huange818b6e2018-02-22 23:26:2794 const std::string& mid,
95 const rtc::scoped_refptr<rtc::RTCCertificate>& local_certificate,
Qingsi Wang25ec8882019-11-15 20:33:0596 rtc::scoped_refptr<webrtc::IceTransportInterface> ice_transport,
97 rtc::scoped_refptr<webrtc::IceTransportInterface> rtcp_ice_transport,
Zhi Huange818b6e2018-02-22 23:26:2798 std::unique_ptr<webrtc::RtpTransport> unencrypted_rtp_transport,
99 std::unique_ptr<webrtc::SrtpTransport> sdes_transport,
100 std::unique_ptr<webrtc::DtlsSrtpTransport> dtls_srtp_transport,
101 std::unique_ptr<DtlsTransportInternal> rtp_dtls_transport,
Anton Sukhanov7940da02018-10-10 17:34:49102 std::unique_ptr<DtlsTransportInternal> rtcp_dtls_transport,
Mirko Bonadei96dca922021-07-10 20:37:40103 std::unique_ptr<SctpTransportInternal> sctp_transport,
104 std::function<void()> rtcp_mux_active_callback);
Zhi Huange818b6e2018-02-22 23:26:27105
Mirko Bonadei96dca922021-07-10 20:37:40106 ~JsepTransport();
Zhi Huange818b6e2018-02-22 23:26:27107
Byoungchan Leec065e732022-01-18 00:35:48108 JsepTransport(const JsepTransport&) = delete;
109 JsepTransport& operator=(const JsepTransport&) = delete;
110
Zhi Huange818b6e2018-02-22 23:26:27111 // Returns the MID of this transport. This is only used for logging.
112 const std::string& mid() const { return mid_; }
113
114 // Must be called before applying local session description.
115 // Needed in order to verify the local fingerprint.
116 void SetLocalCertificate(
117 const rtc::scoped_refptr<rtc::RTCCertificate>& local_certificate) {
Harald Alvestrand78a5e962019-04-03 08:42:39118 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27119 local_certificate_ = local_certificate;
120 }
121
122 // Return the local certificate provided by SetLocalCertificate.
123 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const {
Harald Alvestrand78a5e962019-04-03 08:42:39124 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27125 return local_certificate_;
126 }
127
128 webrtc::RTCError SetLocalJsepTransportDescription(
129 const JsepTransportDescription& jsep_description,
Harald Alvestrandd4ad2ef2021-02-05 23:36:39130 webrtc::SdpType type);
Zhi Huange818b6e2018-02-22 23:26:27131
132 // Set the remote TransportDescription to be used by DTLS and ICE channels
133 // that are part of this Transport.
134 webrtc::RTCError SetRemoteJsepTransportDescription(
135 const JsepTransportDescription& jsep_description,
Niels Möller6a48a1d2021-02-05 11:34:14136 webrtc::SdpType type);
137 webrtc::RTCError AddRemoteCandidates(const Candidates& candidates);
Zhi Huange818b6e2018-02-22 23:26:27138
139 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
140 // set, offers should generate new ufrags/passwords until an ICE restart
141 // occurs.
142 //
Artem Titov880fa812021-07-30 20:30:23143 // This and `needs_ice_restart()` must be called on the network thread.
Tomas Gunnarsson20f74562021-02-04 09:22:50144 void SetNeedsIceRestartFlag();
145
Zhi Huange818b6e2018-02-22 23:26:27146 // Returns true if the ICE restart flag above was set, and no ICE restart has
147 // occurred yet for this transport (by applying a local description with
148 // changed ufrag/password).
Tomas Gunnarsson20f74562021-02-04 09:22:50149 bool needs_ice_restart() const {
150 RTC_DCHECK_RUN_ON(network_thread_);
Harald Alvestrand78a5e962019-04-03 08:42:39151 return needs_ice_restart_;
152 }
Zhi Huange818b6e2018-02-22 23:26:27153
Danil Chapovalov66cadcc2018-06-19 14:47:43154 // Returns role if negotiated, or empty absl::optional if it hasn't been
155 // negotiated yet.
Niels Möller6a48a1d2021-02-05 11:34:14156 absl::optional<rtc::SSLRole> GetDtlsRole() const;
Zhi Huange818b6e2018-02-22 23:26:27157
158 // TODO(deadbeef): Make this const. See comment in transportcontroller.h.
Harald Alvestrandd4ad2ef2021-02-05 23:36:39159 bool GetStats(TransportStats* stats);
Zhi Huange818b6e2018-02-22 23:26:27160
161 const JsepTransportDescription* local_description() const {
Harald Alvestrand78a5e962019-04-03 08:42:39162 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27163 return local_description_.get();
164 }
165
166 const JsepTransportDescription* remote_description() const {
Harald Alvestrand78a5e962019-04-03 08:42:39167 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27168 return remote_description_.get();
169 }
170
Niels Möller6a48a1d2021-02-05 11:34:14171 // Returns the rtp transport, if any.
172 webrtc::RtpTransportInternal* rtp_transport() const {
173 if (dtls_srtp_transport_) {
174 return dtls_srtp_transport_.get();
175 }
Harald Alvestrand0d018412021-11-04 13:52:31176 if (sdes_transport_) {
177 return sdes_transport_.get();
178 }
Niels Möller6a48a1d2021-02-05 11:34:14179 if (unencrypted_rtp_transport_) {
180 return unencrypted_rtp_transport_.get();
181 }
182 return nullptr;
Zhi Huange818b6e2018-02-22 23:26:27183 }
184
Niels Möller6a48a1d2021-02-05 11:34:14185 const DtlsTransportInternal* rtp_dtls_transport() const {
Harald Alvestrandad88c882018-11-28 15:47:46186 if (rtp_dtls_transport_) {
187 return rtp_dtls_transport_->internal();
Harald Alvestrandad88c882018-11-28 15:47:46188 }
Niels Möller6a48a1d2021-02-05 11:34:14189 return nullptr;
Zhi Huange818b6e2018-02-22 23:26:27190 }
191
Niels Möller6a48a1d2021-02-05 11:34:14192 DtlsTransportInternal* rtp_dtls_transport() {
193 if (rtp_dtls_transport_) {
194 return rtp_dtls_transport_->internal();
195 }
196 return nullptr;
Harald Alvestrandad88c882018-11-28 15:47:46197 }
198
Harald Alvestrandd4ad2ef2021-02-05 23:36:39199 const DtlsTransportInternal* rtcp_dtls_transport() const {
200 RTC_DCHECK_RUN_ON(network_thread_);
Harald Alvestrandad88c882018-11-28 15:47:46201 if (rtcp_dtls_transport_) {
202 return rtcp_dtls_transport_->internal();
Harald Alvestrandad88c882018-11-28 15:47:46203 }
Niels Möller6a48a1d2021-02-05 11:34:14204 return nullptr;
Harald Alvestrandad88c882018-11-28 15:47:46205 }
206
Harald Alvestrandd4ad2ef2021-02-05 23:36:39207 DtlsTransportInternal* rtcp_dtls_transport() {
208 RTC_DCHECK_RUN_ON(network_thread_);
Harald Alvestrandad88c882018-11-28 15:47:46209 if (rtcp_dtls_transport_) {
210 return rtcp_dtls_transport_->internal();
Harald Alvestrandad88c882018-11-28 15:47:46211 }
Niels Möller6a48a1d2021-02-05 11:34:14212 return nullptr;
Harald Alvestrandad88c882018-11-28 15:47:46213 }
214
Niels Möller6a48a1d2021-02-05 11:34:14215 rtc::scoped_refptr<webrtc::DtlsTransport> RtpDtlsTransport() {
Harald Alvestrandad88c882018-11-28 15:47:46216 return rtp_dtls_transport_;
Zhi Huange818b6e2018-02-22 23:26:27217 }
218
Niels Möller6a48a1d2021-02-05 11:34:14219 rtc::scoped_refptr<webrtc::SctpTransport> SctpTransport() const {
Bjorn A Mellembc3eebc2019-09-23 21:53:54220 return sctp_transport_;
221 }
222
Niels Möllerc888ffa2020-07-14 11:21:42223 // TODO(bugs.webrtc.org/9719): Delete method, update callers to use
224 // SctpTransport() instead.
Niels Möller6a48a1d2021-02-05 11:34:14225 webrtc::DataChannelTransportInterface* data_channel_transport() const {
Niels Möllerc888ffa2020-07-14 11:21:42226 if (sctp_data_channel_transport_) {
Bjorn A Mellembc3eebc2019-09-23 21:53:54227 return sctp_data_channel_transport_.get();
228 }
Niels Möllerc888ffa2020-07-14 11:21:42229 return nullptr;
Bjorn A Mellembc3eebc2019-09-23 21:53:54230 }
231
Zhi Huange818b6e2018-02-22 23:26:27232 // TODO(deadbeef): The methods below are only public for testing. Should make
233 // them utility functions or objects so they can be tested independently from
234 // this class.
235
236 // Returns an error if the certificate's identity does not match the
237 // fingerprint, or either is NULL.
238 webrtc::RTCError VerifyCertificateFingerprint(
239 const rtc::RTCCertificate* certificate,
240 const rtc::SSLFingerprint* fingerprint) const;
241
Niels Möller6a48a1d2021-02-05 11:34:14242 void SetActiveResetSrtpParams(bool active_reset_srtp_params);
Zhi Huangb57e1692018-06-12 18:41:11243
Zhi Huange818b6e2018-02-22 23:26:27244 private:
245 bool SetRtcpMux(bool enable, webrtc::SdpType type, ContentSource source);
246
Harald Alvestrandd4ad2ef2021-02-05 23:36:39247 void ActivateRtcpMux() RTC_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27248
Harald Alvestrand0d018412021-11-04 13:52:31249 bool SetSdes(const std::vector<CryptoParams>& cryptos,
250 const std::vector<int>& encrypted_extension_ids,
251 webrtc::SdpType type,
252 ContentSource source);
253
Zhi Huange818b6e2018-02-22 23:26:27254 // Negotiates and sets the DTLS parameters based on the current local and
255 // remote transport description, such as the DTLS role to use, and whether
256 // DTLS should be activated.
257 //
258 // Called when an answer TransportDescription is applied.
259 webrtc::RTCError NegotiateAndSetDtlsParameters(
260 webrtc::SdpType local_description_type);
261
262 // Negotiates the DTLS role based off the offer and answer as specified by
263 // RFC 4145, section-4.1. Returns an RTCError if role cannot be determined
264 // from the local description and remote description.
265 webrtc::RTCError NegotiateDtlsRole(
266 webrtc::SdpType local_description_type,
267 ConnectionRole local_connection_role,
268 ConnectionRole remote_connection_role,
Niels Möller6a48a1d2021-02-05 11:34:14269 absl::optional<rtc::SSLRole>* negotiated_dtls_role);
Zhi Huange818b6e2018-02-22 23:26:27270
Zhi Huange818b6e2018-02-22 23:26:27271 // Pushes down the ICE parameters from the remote description.
Steve Anton71ff0732020-01-25 00:28:15272 void SetRemoteIceParameters(const IceParameters& ice_parameters,
273 IceTransportInternal* ice);
Zhi Huange818b6e2018-02-22 23:26:27274
275 // Pushes down the DTLS parameters obtained via negotiation.
Markus Handellc18b7bf2020-05-15 11:03:27276 static webrtc::RTCError SetNegotiatedDtlsParameters(
Zhi Huange818b6e2018-02-22 23:26:27277 DtlsTransportInternal* dtls_transport,
Danil Chapovalov66cadcc2018-06-19 14:47:43278 absl::optional<rtc::SSLRole> dtls_role,
Zhi Huange818b6e2018-02-22 23:26:27279 rtc::SSLFingerprint* remote_fingerprint);
280
281 bool GetTransportStats(DtlsTransportInternal* dtls_transport,
Niels Möller6a48a1d2021-02-05 11:34:14282 int component,
283 TransportStats* stats);
Bjorn A Mellemc85ebbe2019-06-07 17:28:06284
Harald Alvestrand78a5e962019-04-03 08:42:39285 // Owning thread, for safety checks
286 const rtc::Thread* const network_thread_;
Zhi Huange818b6e2018-02-22 23:26:27287 const std::string mid_;
288 // needs-ice-restart bit as described in JSEP.
Tomas Gunnarsson20f74562021-02-04 09:22:50289 bool needs_ice_restart_ RTC_GUARDED_BY(network_thread_) = false;
Harald Alvestrand78a5e962019-04-03 08:42:39290 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_
291 RTC_GUARDED_BY(network_thread_);
292 std::unique_ptr<JsepTransportDescription> local_description_
293 RTC_GUARDED_BY(network_thread_);
294 std::unique_ptr<JsepTransportDescription> remote_description_
295 RTC_GUARDED_BY(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27296
Bjorn A Mellem0c1c1b42019-05-30 00:34:13297 // Ice transport which may be used by any of upper-layer transports (below).
298 // Owned by JsepTransport and guaranteed to outlive the transports below.
Qingsi Wang25ec8882019-11-15 20:33:05299 const rtc::scoped_refptr<webrtc::IceTransportInterface> ice_transport_;
300 const rtc::scoped_refptr<webrtc::IceTransportInterface> rtcp_ice_transport_;
Bjorn A Mellem0c1c1b42019-05-30 00:34:13301
Zhi Huange818b6e2018-02-22 23:26:27302 // To avoid downcasting and make it type safe, keep three unique pointers for
303 // different SRTP mode and only one of these is non-nullptr.
Niels Möllerc5d48102021-02-01 15:13:42304 const std::unique_ptr<webrtc::RtpTransport> unencrypted_rtp_transport_;
Harald Alvestrand0d018412021-11-04 13:52:31305 const std::unique_ptr<webrtc::SrtpTransport> sdes_transport_;
Niels Möllerc5d48102021-02-01 15:13:42306 const std::unique_ptr<webrtc::DtlsSrtpTransport> dtls_srtp_transport_;
Bjorn A Mellemc85ebbe2019-06-07 17:28:06307
Niels Möllerc5d48102021-02-01 15:13:42308 const rtc::scoped_refptr<webrtc::DtlsTransport> rtp_dtls_transport_;
Harald Alvestrandd4ad2ef2021-02-05 23:36:39309 // The RTCP transport is const for all usages, except that it is cleared
310 // when RTCP multiplexing is turned on; this happens on the network thread.
Harald Alvestrand78a5e962019-04-03 08:42:39311 rtc::scoped_refptr<webrtc::DtlsTransport> rtcp_dtls_transport_
Harald Alvestrandd4ad2ef2021-02-05 23:36:39312 RTC_GUARDED_BY(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27313
Niels Möllerc5d48102021-02-01 15:13:42314 const std::unique_ptr<webrtc::DataChannelTransportInterface>
315 sctp_data_channel_transport_;
316 const rtc::scoped_refptr<webrtc::SctpTransport> sctp_transport_;
Bjorn A Mellembc3eebc2019-09-23 21:53:54317
Harald Alvestrand0d018412021-11-04 13:52:31318 SrtpFilter sdes_negotiator_ RTC_GUARDED_BY(network_thread_);
Harald Alvestrand78a5e962019-04-03 08:42:39319 RtcpMuxFilter rtcp_mux_negotiator_ RTC_GUARDED_BY(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27320
321 // Cache the encrypted header extension IDs for SDES negoitation.
Harald Alvestrand78a5e962019-04-03 08:42:39322 absl::optional<std::vector<int>> send_extension_ids_
323 RTC_GUARDED_BY(network_thread_);
324 absl::optional<std::vector<int>> recv_extension_ids_
325 RTC_GUARDED_BY(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27326
Mirko Bonadei96dca922021-07-10 20:37:40327 // This is invoked when RTCP-mux becomes active and
Artem Titovcfea2182021-08-09 23:22:31328 // `rtcp_dtls_transport_` is destroyed. The JsepTransportController will
Mirko Bonadei96dca922021-07-10 20:37:40329 // receive the callback and update the aggregate transport states.
330 std::function<void()> rtcp_mux_active_callback_;
Zhi Huange818b6e2018-02-22 23:26:27331};
332
333} // namespace cricket
334
Steve Anton10542f22019-01-11 17:11:00335#endif // PC_JSEP_TRANSPORT_H_