blob: af0c797fc8e1278948817cec063c1e31db9b0b1a [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"
Qingsi Wang25ec8882019-11-15 20:33:0522#include "api/ice_transport_interface.h"
Zhi Huange818b6e2018-02-22 23:26:2723#include "api/jsep.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0824#include "api/rtc_error.h"
25#include "api/scoped_refptr.h"
Artem Titovd15a5752021-02-10 13:31:2426#include "api/sequence_checker.h"
Niels Möllerc888ffa2020-07-14 11:21:4227#include "api/transport/data_channel_transport_interface.h"
Bjorn A Mellembc3eebc2019-09-23 21:53:5428#include "media/sctp/sctp_transport_internal.h"
Steve Anton10542f22019-01-11 17:11:0029#include "p2p/base/dtls_transport.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0830#include "p2p/base/dtls_transport_internal.h"
31#include "p2p/base/ice_transport_internal.h"
Steve Anton10542f22019-01-11 17:11:0032#include "p2p/base/p2p_constants.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0833#include "p2p/base/transport_description.h"
Steve Anton10542f22019-01-11 17:11:0034#include "p2p/base/transport_info.h"
35#include "pc/dtls_srtp_transport.h"
36#include "pc/dtls_transport.h"
37#include "pc/rtcp_mux_filter.h"
38#include "pc/rtp_transport.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0839#include "pc/rtp_transport_internal.h"
Bjorn A Mellembc3eebc2019-09-23 21:53:5440#include "pc/sctp_transport.h"
Steve Anton10542f22019-01-11 17:11:0041#include "pc/session_description.h"
Harald Alvestrand0d018412021-11-04 13:52:3142#include "pc/srtp_transport.h"
Steve Anton10542f22019-01-11 17:11:0043#include "pc/transport_stats.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0844#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 17:11:0045#include "rtc_base/rtc_certificate.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0846#include "rtc_base/ssl_fingerprint.h"
Steve Anton10542f22019-01-11 17:11:0047#include "rtc_base/ssl_stream_adapter.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0848#include "rtc_base/thread.h"
49#include "rtc_base/thread_annotations.h"
Zhi Huange818b6e2018-02-22 23:26:2750
51namespace cricket {
52
53class DtlsTransportInternal;
54
55struct JsepTransportDescription {
56 public:
57 JsepTransportDescription();
58 JsepTransportDescription(
59 bool rtcp_mux_enabled,
Zhi Huange818b6e2018-02-22 23:26:2760 const std::vector<int>& encrypted_header_extension_ids,
Zhi Huange830e682018-03-30 17:48:3561 int rtp_abs_sendtime_extn_id,
Niels Möllerdc80aaf2020-06-18 08:10:1762 const TransportDescription& transport_description);
Zhi Huange818b6e2018-02-22 23:26:2763 JsepTransportDescription(const JsepTransportDescription& from);
64 ~JsepTransportDescription();
65
66 JsepTransportDescription& operator=(const JsepTransportDescription& from);
67
68 bool rtcp_mux_enabled = true;
Zhi Huange818b6e2018-02-22 23:26:2769 std::vector<int> encrypted_header_extension_ids;
Zhi Huange830e682018-03-30 17:48:3570 int rtp_abs_sendtime_extn_id = -1;
Zhi Huange818b6e2018-02-22 23:26:2771 // TODO(zhihuang): Add the ICE and DTLS related variables and methods from
72 // TransportDescription and remove this extra layer of abstraction.
73 TransportDescription transport_desc;
74};
75
76// Helper class used by JsepTransportController that processes
77// TransportDescriptions. A TransportDescription represents the
78// transport-specific properties of an SDP m= section, processed according to
79// JSEP. Each transport consists of DTLS and ICE transport channels for RTP
80// (and possibly RTCP, if rtcp-mux isn't used).
81//
Zhi Huang365381f2018-04-13 23:44:3482// On Threading: JsepTransport performs work solely on the network thread, and
Zhi Huange818b6e2018-02-22 23:26:2783// so its methods should only be called on the network thread.
Mirko Bonadei96dca922021-07-10 20:37:4084class JsepTransport {
Zhi Huange818b6e2018-02-22 23:26:2785 public:
Artem Titov880fa812021-07-30 20:30:2386 // `mid` is just used for log statements in order to identify the Transport.
87 // Note that `local_certificate` is allowed to be null since a remote
Zhi Huange818b6e2018-02-22 23:26:2788 // description may be set before a local certificate is generated.
Zhi Huang365381f2018-04-13 23:44:3489 JsepTransport(
Zhi Huange818b6e2018-02-22 23:26:2790 const std::string& mid,
91 const rtc::scoped_refptr<rtc::RTCCertificate>& local_certificate,
Qingsi Wang25ec8882019-11-15 20:33:0592 rtc::scoped_refptr<webrtc::IceTransportInterface> ice_transport,
93 rtc::scoped_refptr<webrtc::IceTransportInterface> rtcp_ice_transport,
Zhi Huange818b6e2018-02-22 23:26:2794 std::unique_ptr<webrtc::RtpTransport> unencrypted_rtp_transport,
95 std::unique_ptr<webrtc::SrtpTransport> sdes_transport,
96 std::unique_ptr<webrtc::DtlsSrtpTransport> dtls_srtp_transport,
97 std::unique_ptr<DtlsTransportInternal> rtp_dtls_transport,
Anton Sukhanov7940da02018-10-10 17:34:4998 std::unique_ptr<DtlsTransportInternal> rtcp_dtls_transport,
Mirko Bonadei96dca922021-07-10 20:37:4099 std::unique_ptr<SctpTransportInternal> sctp_transport,
100 std::function<void()> rtcp_mux_active_callback);
Zhi Huange818b6e2018-02-22 23:26:27101
Mirko Bonadei96dca922021-07-10 20:37:40102 ~JsepTransport();
Zhi Huange818b6e2018-02-22 23:26:27103
Byoungchan Leec065e732022-01-18 00:35:48104 JsepTransport(const JsepTransport&) = delete;
105 JsepTransport& operator=(const JsepTransport&) = delete;
106
Zhi Huange818b6e2018-02-22 23:26:27107 // Returns the MID of this transport. This is only used for logging.
108 const std::string& mid() const { return mid_; }
109
110 // Must be called before applying local session description.
111 // Needed in order to verify the local fingerprint.
112 void SetLocalCertificate(
113 const rtc::scoped_refptr<rtc::RTCCertificate>& local_certificate) {
Harald Alvestrand78a5e962019-04-03 08:42:39114 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27115 local_certificate_ = local_certificate;
116 }
117
118 // Return the local certificate provided by SetLocalCertificate.
119 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const {
Harald Alvestrand78a5e962019-04-03 08:42:39120 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27121 return local_certificate_;
122 }
123
124 webrtc::RTCError SetLocalJsepTransportDescription(
125 const JsepTransportDescription& jsep_description,
Harald Alvestrandd4ad2ef2021-02-05 23:36:39126 webrtc::SdpType type);
Zhi Huange818b6e2018-02-22 23:26:27127
128 // Set the remote TransportDescription to be used by DTLS and ICE channels
129 // that are part of this Transport.
130 webrtc::RTCError SetRemoteJsepTransportDescription(
131 const JsepTransportDescription& jsep_description,
Niels Möller6a48a1d2021-02-05 11:34:14132 webrtc::SdpType type);
133 webrtc::RTCError AddRemoteCandidates(const Candidates& candidates);
Zhi Huange818b6e2018-02-22 23:26:27134
135 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
136 // set, offers should generate new ufrags/passwords until an ICE restart
137 // occurs.
138 //
Artem Titov880fa812021-07-30 20:30:23139 // This and `needs_ice_restart()` must be called on the network thread.
Tomas Gunnarsson20f74562021-02-04 09:22:50140 void SetNeedsIceRestartFlag();
141
Zhi Huange818b6e2018-02-22 23:26:27142 // Returns true if the ICE restart flag above was set, and no ICE restart has
143 // occurred yet for this transport (by applying a local description with
144 // changed ufrag/password).
Tomas Gunnarsson20f74562021-02-04 09:22:50145 bool needs_ice_restart() const {
146 RTC_DCHECK_RUN_ON(network_thread_);
Harald Alvestrand78a5e962019-04-03 08:42:39147 return needs_ice_restart_;
148 }
Zhi Huange818b6e2018-02-22 23:26:27149
Danil Chapovalov66cadcc2018-06-19 14:47:43150 // Returns role if negotiated, or empty absl::optional if it hasn't been
151 // negotiated yet.
Niels Möller6a48a1d2021-02-05 11:34:14152 absl::optional<rtc::SSLRole> GetDtlsRole() const;
Zhi Huange818b6e2018-02-22 23:26:27153
154 // TODO(deadbeef): Make this const. See comment in transportcontroller.h.
Harald Alvestrandd4ad2ef2021-02-05 23:36:39155 bool GetStats(TransportStats* stats);
Zhi Huange818b6e2018-02-22 23:26:27156
157 const JsepTransportDescription* local_description() const {
Harald Alvestrand78a5e962019-04-03 08:42:39158 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27159 return local_description_.get();
160 }
161
162 const JsepTransportDescription* remote_description() const {
Harald Alvestrand78a5e962019-04-03 08:42:39163 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27164 return remote_description_.get();
165 }
166
Niels Möller6a48a1d2021-02-05 11:34:14167 // Returns the rtp transport, if any.
168 webrtc::RtpTransportInternal* rtp_transport() const {
169 if (dtls_srtp_transport_) {
170 return dtls_srtp_transport_.get();
171 }
Harald Alvestrand0d018412021-11-04 13:52:31172 if (sdes_transport_) {
173 return sdes_transport_.get();
174 }
Niels Möller6a48a1d2021-02-05 11:34:14175 if (unencrypted_rtp_transport_) {
176 return unencrypted_rtp_transport_.get();
177 }
178 return nullptr;
Zhi Huange818b6e2018-02-22 23:26:27179 }
180
Niels Möller6a48a1d2021-02-05 11:34:14181 const DtlsTransportInternal* rtp_dtls_transport() const {
Harald Alvestrandad88c882018-11-28 15:47:46182 if (rtp_dtls_transport_) {
183 return rtp_dtls_transport_->internal();
Harald Alvestrandad88c882018-11-28 15:47:46184 }
Niels Möller6a48a1d2021-02-05 11:34:14185 return nullptr;
Zhi Huange818b6e2018-02-22 23:26:27186 }
187
Niels Möller6a48a1d2021-02-05 11:34:14188 DtlsTransportInternal* rtp_dtls_transport() {
189 if (rtp_dtls_transport_) {
190 return rtp_dtls_transport_->internal();
191 }
192 return nullptr;
Harald Alvestrandad88c882018-11-28 15:47:46193 }
194
Harald Alvestrandd4ad2ef2021-02-05 23:36:39195 const DtlsTransportInternal* rtcp_dtls_transport() const {
196 RTC_DCHECK_RUN_ON(network_thread_);
Harald Alvestrandad88c882018-11-28 15:47:46197 if (rtcp_dtls_transport_) {
198 return rtcp_dtls_transport_->internal();
Harald Alvestrandad88c882018-11-28 15:47:46199 }
Niels Möller6a48a1d2021-02-05 11:34:14200 return nullptr;
Harald Alvestrandad88c882018-11-28 15:47:46201 }
202
Harald Alvestrandd4ad2ef2021-02-05 23:36:39203 DtlsTransportInternal* rtcp_dtls_transport() {
204 RTC_DCHECK_RUN_ON(network_thread_);
Harald Alvestrandad88c882018-11-28 15:47:46205 if (rtcp_dtls_transport_) {
206 return rtcp_dtls_transport_->internal();
Harald Alvestrandad88c882018-11-28 15:47:46207 }
Niels Möller6a48a1d2021-02-05 11:34:14208 return nullptr;
Harald Alvestrandad88c882018-11-28 15:47:46209 }
210
Niels Möller6a48a1d2021-02-05 11:34:14211 rtc::scoped_refptr<webrtc::DtlsTransport> RtpDtlsTransport() {
Harald Alvestrandad88c882018-11-28 15:47:46212 return rtp_dtls_transport_;
Zhi Huange818b6e2018-02-22 23:26:27213 }
214
Niels Möller6a48a1d2021-02-05 11:34:14215 rtc::scoped_refptr<webrtc::SctpTransport> SctpTransport() const {
Bjorn A Mellembc3eebc2019-09-23 21:53:54216 return sctp_transport_;
217 }
218
Niels Möllerc888ffa2020-07-14 11:21:42219 // TODO(bugs.webrtc.org/9719): Delete method, update callers to use
220 // SctpTransport() instead.
Niels Möller6a48a1d2021-02-05 11:34:14221 webrtc::DataChannelTransportInterface* data_channel_transport() const {
Fredrik Solenberg5cb3a902022-08-22 09:34:29222 return sctp_transport_.get();
Bjorn A Mellembc3eebc2019-09-23 21:53:54223 }
224
Zhi Huange818b6e2018-02-22 23:26:27225 // TODO(deadbeef): The methods below are only public for testing. Should make
226 // them utility functions or objects so they can be tested independently from
227 // this class.
228
229 // Returns an error if the certificate's identity does not match the
230 // fingerprint, or either is NULL.
231 webrtc::RTCError VerifyCertificateFingerprint(
232 const rtc::RTCCertificate* certificate,
233 const rtc::SSLFingerprint* fingerprint) const;
234
Niels Möller6a48a1d2021-02-05 11:34:14235 void SetActiveResetSrtpParams(bool active_reset_srtp_params);
Zhi Huangb57e1692018-06-12 18:41:11236
Zhi Huange818b6e2018-02-22 23:26:27237 private:
238 bool SetRtcpMux(bool enable, webrtc::SdpType type, ContentSource source);
239
Harald Alvestrandd4ad2ef2021-02-05 23:36:39240 void ActivateRtcpMux() RTC_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27241
Zhi Huange818b6e2018-02-22 23:26:27242 // Negotiates and sets the DTLS parameters based on the current local and
243 // remote transport description, such as the DTLS role to use, and whether
244 // DTLS should be activated.
245 //
246 // Called when an answer TransportDescription is applied.
247 webrtc::RTCError NegotiateAndSetDtlsParameters(
248 webrtc::SdpType local_description_type);
249
250 // Negotiates the DTLS role based off the offer and answer as specified by
251 // RFC 4145, section-4.1. Returns an RTCError if role cannot be determined
252 // from the local description and remote description.
253 webrtc::RTCError NegotiateDtlsRole(
254 webrtc::SdpType local_description_type,
255 ConnectionRole local_connection_role,
256 ConnectionRole remote_connection_role,
Niels Möller6a48a1d2021-02-05 11:34:14257 absl::optional<rtc::SSLRole>* negotiated_dtls_role);
Zhi Huange818b6e2018-02-22 23:26:27258
Zhi Huange818b6e2018-02-22 23:26:27259 // Pushes down the ICE parameters from the remote description.
Steve Anton71ff0732020-01-25 00:28:15260 void SetRemoteIceParameters(const IceParameters& ice_parameters,
261 IceTransportInternal* ice);
Zhi Huange818b6e2018-02-22 23:26:27262
263 // Pushes down the DTLS parameters obtained via negotiation.
Markus Handellc18b7bf2020-05-15 11:03:27264 static webrtc::RTCError SetNegotiatedDtlsParameters(
Zhi Huange818b6e2018-02-22 23:26:27265 DtlsTransportInternal* dtls_transport,
Danil Chapovalov66cadcc2018-06-19 14:47:43266 absl::optional<rtc::SSLRole> dtls_role,
Zhi Huange818b6e2018-02-22 23:26:27267 rtc::SSLFingerprint* remote_fingerprint);
268
269 bool GetTransportStats(DtlsTransportInternal* dtls_transport,
Niels Möller6a48a1d2021-02-05 11:34:14270 int component,
271 TransportStats* stats);
Bjorn A Mellemc85ebbe2019-06-07 17:28:06272
Harald Alvestrand78a5e962019-04-03 08:42:39273 // Owning thread, for safety checks
274 const rtc::Thread* const network_thread_;
Zhi Huange818b6e2018-02-22 23:26:27275 const std::string mid_;
276 // needs-ice-restart bit as described in JSEP.
Tomas Gunnarsson20f74562021-02-04 09:22:50277 bool needs_ice_restart_ RTC_GUARDED_BY(network_thread_) = false;
Harald Alvestrand78a5e962019-04-03 08:42:39278 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_
279 RTC_GUARDED_BY(network_thread_);
280 std::unique_ptr<JsepTransportDescription> local_description_
281 RTC_GUARDED_BY(network_thread_);
282 std::unique_ptr<JsepTransportDescription> remote_description_
283 RTC_GUARDED_BY(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27284
Bjorn A Mellem0c1c1b42019-05-30 00:34:13285 // Ice transport which may be used by any of upper-layer transports (below).
286 // Owned by JsepTransport and guaranteed to outlive the transports below.
Qingsi Wang25ec8882019-11-15 20:33:05287 const rtc::scoped_refptr<webrtc::IceTransportInterface> ice_transport_;
288 const rtc::scoped_refptr<webrtc::IceTransportInterface> rtcp_ice_transport_;
Bjorn A Mellem0c1c1b42019-05-30 00:34:13289
Zhi Huange818b6e2018-02-22 23:26:27290 // To avoid downcasting and make it type safe, keep three unique pointers for
291 // different SRTP mode and only one of these is non-nullptr.
Niels Möllerc5d48102021-02-01 15:13:42292 const std::unique_ptr<webrtc::RtpTransport> unencrypted_rtp_transport_;
Harald Alvestrand0d018412021-11-04 13:52:31293 const std::unique_ptr<webrtc::SrtpTransport> sdes_transport_;
Niels Möllerc5d48102021-02-01 15:13:42294 const std::unique_ptr<webrtc::DtlsSrtpTransport> dtls_srtp_transport_;
Bjorn A Mellemc85ebbe2019-06-07 17:28:06295
Niels Möllerc5d48102021-02-01 15:13:42296 const rtc::scoped_refptr<webrtc::DtlsTransport> rtp_dtls_transport_;
Harald Alvestrandd4ad2ef2021-02-05 23:36:39297 // The RTCP transport is const for all usages, except that it is cleared
298 // when RTCP multiplexing is turned on; this happens on the network thread.
Harald Alvestrand78a5e962019-04-03 08:42:39299 rtc::scoped_refptr<webrtc::DtlsTransport> rtcp_dtls_transport_
Harald Alvestrandd4ad2ef2021-02-05 23:36:39300 RTC_GUARDED_BY(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27301
Niels Möllerc5d48102021-02-01 15:13:42302 const rtc::scoped_refptr<webrtc::SctpTransport> sctp_transport_;
Bjorn A Mellembc3eebc2019-09-23 21:53:54303
Harald Alvestrand78a5e962019-04-03 08:42:39304 RtcpMuxFilter rtcp_mux_negotiator_ RTC_GUARDED_BY(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27305
306 // Cache the encrypted header extension IDs for SDES negoitation.
Harald Alvestrand78a5e962019-04-03 08:42:39307 absl::optional<std::vector<int>> send_extension_ids_
308 RTC_GUARDED_BY(network_thread_);
309 absl::optional<std::vector<int>> recv_extension_ids_
310 RTC_GUARDED_BY(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27311
Mirko Bonadei96dca922021-07-10 20:37:40312 // This is invoked when RTCP-mux becomes active and
Artem Titovcfea2182021-08-09 23:22:31313 // `rtcp_dtls_transport_` is destroyed. The JsepTransportController will
Mirko Bonadei96dca922021-07-10 20:37:40314 // receive the callback and update the aggregate transport states.
315 std::function<void()> rtcp_mux_active_callback_;
Zhi Huange818b6e2018-02-22 23:26:27316};
317
318} // namespace cricket
319
Steve Anton10542f22019-01-11 17:11:00320#endif // PC_JSEP_TRANSPORT_H_