blob: fce21be6e68c662577d2ed2f7d99c39535f48b39 [file] [log] [blame]
Zhi Huange818b6e2018-02-22 15:26:27 -08001/*
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 09:11:00 -080011#ifndef PC_JSEP_TRANSPORT_H_
12#define PC_JSEP_TRANSPORT_H_
Zhi Huange818b6e2018-02-22 15:26:27 -080013
14#include <map>
15#include <memory>
16#include <string>
17#include <vector>
18
Danil Chapovalov66cadcc2018-06-19 16:47:43 +020019#include "absl/types/optional.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080020#include "api/candidate.h"
21#include "api/jsep.h"
Anton Sukhanov7940da02018-10-10 10:34:49 -070022#include "api/media_transport_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "p2p/base/dtls_transport.h"
24#include "p2p/base/p2p_constants.h"
25#include "p2p/base/transport_info.h"
26#include "pc/dtls_srtp_transport.h"
27#include "pc/dtls_transport.h"
28#include "pc/rtcp_mux_filter.h"
29#include "pc/rtp_transport.h"
30#include "pc/session_description.h"
31#include "pc/srtp_filter.h"
32#include "pc/srtp_transport.h"
33#include "pc/transport_stats.h"
34#include "rtc_base/constructor_magic.h"
35#include "rtc_base/message_queue.h"
36#include "rtc_base/rtc_certificate.h"
37#include "rtc_base/ssl_stream_adapter.h"
Artem Titove41c4332018-07-25 15:04:28 +020038#include "rtc_base/third_party/sigslot/sigslot.h"
Harald Alvestrand78a5e962019-04-03 10:42:39 +020039#include "rtc_base/thread_checker.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080040
41namespace cricket {
42
43class DtlsTransportInternal;
44
45struct JsepTransportDescription {
46 public:
47 JsepTransportDescription();
48 JsepTransportDescription(
49 bool rtcp_mux_enabled,
50 const std::vector<CryptoParams>& cryptos,
51 const std::vector<int>& encrypted_header_extension_ids,
Zhi Huange830e682018-03-30 10:48:35 -070052 int rtp_abs_sendtime_extn_id,
Zhi Huange818b6e2018-02-22 15:26:27 -080053 const TransportDescription& transport_description);
54 JsepTransportDescription(const JsepTransportDescription& from);
55 ~JsepTransportDescription();
56
57 JsepTransportDescription& operator=(const JsepTransportDescription& from);
58
59 bool rtcp_mux_enabled = true;
60 std::vector<CryptoParams> cryptos;
61 std::vector<int> encrypted_header_extension_ids;
Zhi Huange830e682018-03-30 10:48:35 -070062 int rtp_abs_sendtime_extn_id = -1;
Zhi Huange818b6e2018-02-22 15:26:27 -080063 // TODO(zhihuang): Add the ICE and DTLS related variables and methods from
64 // TransportDescription and remove this extra layer of abstraction.
65 TransportDescription transport_desc;
66};
67
68// Helper class used by JsepTransportController that processes
69// TransportDescriptions. A TransportDescription represents the
70// transport-specific properties of an SDP m= section, processed according to
71// JSEP. Each transport consists of DTLS and ICE transport channels for RTP
72// (and possibly RTCP, if rtcp-mux isn't used).
73//
Zhi Huang365381f2018-04-13 16:44:34 -070074// On Threading: JsepTransport performs work solely on the network thread, and
Zhi Huange818b6e2018-02-22 15:26:27 -080075// so its methods should only be called on the network thread.
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -070076class JsepTransport : public sigslot::has_slots<>,
77 public webrtc::MediaTransportStateCallback {
Zhi Huange818b6e2018-02-22 15:26:27 -080078 public:
79 // |mid| is just used for log statements in order to identify the Transport.
80 // Note that |local_certificate| is allowed to be null since a remote
81 // description may be set before a local certificate is generated.
Anton Sukhanov7940da02018-10-10 10:34:49 -070082 //
83 // |media_trasport| is optional (experimental). If available it will be used
84 // to send / receive encoded audio and video frames instead of RTP.
85 // Currently |media_transport| can co-exist with RTP / RTCP transports.
Zhi Huang365381f2018-04-13 16:44:34 -070086 JsepTransport(
Zhi Huange818b6e2018-02-22 15:26:27 -080087 const std::string& mid,
88 const rtc::scoped_refptr<rtc::RTCCertificate>& local_certificate,
89 std::unique_ptr<webrtc::RtpTransport> unencrypted_rtp_transport,
90 std::unique_ptr<webrtc::SrtpTransport> sdes_transport,
91 std::unique_ptr<webrtc::DtlsSrtpTransport> dtls_srtp_transport,
92 std::unique_ptr<DtlsTransportInternal> rtp_dtls_transport,
Anton Sukhanov7940da02018-10-10 10:34:49 -070093 std::unique_ptr<DtlsTransportInternal> rtcp_dtls_transport,
94 std::unique_ptr<webrtc::MediaTransportInterface> media_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -080095
Zhi Huang365381f2018-04-13 16:44:34 -070096 ~JsepTransport() override;
Zhi Huange818b6e2018-02-22 15:26:27 -080097
98 // Returns the MID of this transport. This is only used for logging.
99 const std::string& mid() const { return mid_; }
100
101 // Must be called before applying local session description.
102 // Needed in order to verify the local fingerprint.
103 void SetLocalCertificate(
104 const rtc::scoped_refptr<rtc::RTCCertificate>& local_certificate) {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200105 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800106 local_certificate_ = local_certificate;
107 }
108
109 // Return the local certificate provided by SetLocalCertificate.
110 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200111 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800112 return local_certificate_;
113 }
114
115 webrtc::RTCError SetLocalJsepTransportDescription(
116 const JsepTransportDescription& jsep_description,
117 webrtc::SdpType type);
118
119 // Set the remote TransportDescription to be used by DTLS and ICE channels
120 // that are part of this Transport.
121 webrtc::RTCError SetRemoteJsepTransportDescription(
122 const JsepTransportDescription& jsep_description,
123 webrtc::SdpType type);
Zhi Huange818b6e2018-02-22 15:26:27 -0800124 webrtc::RTCError AddRemoteCandidates(const Candidates& candidates);
125
126 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
127 // set, offers should generate new ufrags/passwords until an ICE restart
128 // occurs.
129 //
130 // This and the below method can be called safely from any thread as long as
131 // SetXTransportDescription is not in progress.
132 void SetNeedsIceRestartFlag();
133 // Returns true if the ICE restart flag above was set, and no ICE restart has
134 // occurred yet for this transport (by applying a local description with
135 // changed ufrag/password).
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200136 bool needs_ice_restart() const {
137 rtc::CritScope scope(&accessor_lock_);
138 return needs_ice_restart_;
139 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800140
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200141 // Returns role if negotiated, or empty absl::optional if it hasn't been
142 // negotiated yet.
143 absl::optional<rtc::SSLRole> GetDtlsRole() const;
Zhi Huange818b6e2018-02-22 15:26:27 -0800144
145 // TODO(deadbeef): Make this const. See comment in transportcontroller.h.
146 bool GetStats(TransportStats* stats);
147
148 const JsepTransportDescription* local_description() const {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200149 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800150 return local_description_.get();
151 }
152
153 const JsepTransportDescription* remote_description() const {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200154 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800155 return remote_description_.get();
156 }
157
158 webrtc::RtpTransportInternal* rtp_transport() const {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200159 // This method is called from the signaling thread, which means
160 // that a race is possible, making safety analysis complex.
161 // After fixing, this method should be marked "network thread only".
Zhi Huange818b6e2018-02-22 15:26:27 -0800162 if (dtls_srtp_transport_) {
163 return dtls_srtp_transport_.get();
164 } else if (sdes_transport_) {
165 return sdes_transport_.get();
166 } else {
167 return unencrypted_rtp_transport_.get();
168 }
169 }
170
Harald Alvestrandad88c882018-11-28 16:47:46 +0100171 const DtlsTransportInternal* rtp_dtls_transport() const {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200172 rtc::CritScope scope(&accessor_lock_);
Harald Alvestrandad88c882018-11-28 16:47:46 +0100173 if (rtp_dtls_transport_) {
174 return rtp_dtls_transport_->internal();
175 } else {
176 return nullptr;
177 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800178 }
179
Harald Alvestrandad88c882018-11-28 16:47:46 +0100180 DtlsTransportInternal* rtp_dtls_transport() {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200181 rtc::CritScope scope(&accessor_lock_);
Harald Alvestrandad88c882018-11-28 16:47:46 +0100182 if (rtp_dtls_transport_) {
183 return rtp_dtls_transport_->internal();
184 } else {
185 return nullptr;
186 }
187 }
188
189 const DtlsTransportInternal* rtcp_dtls_transport() const {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200190 rtc::CritScope scope(&accessor_lock_);
Harald Alvestrandad88c882018-11-28 16:47:46 +0100191 if (rtcp_dtls_transport_) {
192 return rtcp_dtls_transport_->internal();
193 } else {
194 return nullptr;
195 }
196 }
197
198 DtlsTransportInternal* rtcp_dtls_transport() {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200199 rtc::CritScope scope(&accessor_lock_);
Harald Alvestrandad88c882018-11-28 16:47:46 +0100200 if (rtcp_dtls_transport_) {
201 return rtcp_dtls_transport_->internal();
202 } else {
203 return nullptr;
204 }
205 }
206
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +0100207 rtc::scoped_refptr<webrtc::DtlsTransport> RtpDtlsTransport() {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200208 rtc::CritScope scope(&accessor_lock_);
Harald Alvestrandad88c882018-11-28 16:47:46 +0100209 return rtp_dtls_transport_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800210 }
211
Anton Sukhanov7940da02018-10-10 10:34:49 -0700212 // Returns media transport, if available.
213 // Note that media transport is owned by jseptransport and the pointer
214 // to media transport will becomes invalid after destruction of jseptransport.
215 webrtc::MediaTransportInterface* media_transport() const {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200216 rtc::CritScope scope(&accessor_lock_);
Anton Sukhanov7940da02018-10-10 10:34:49 -0700217 return media_transport_.get();
218 }
219
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700220 // Returns datagram transport, if available.
221 webrtc::DatagramTransportInterface* datagram_transport() const {
222 rtc::CritScope scope(&accessor_lock_);
223 return rtp_dtls_transport_->internal()->datagram_transport();
224 }
225
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -0700226 // Returns the latest media transport state.
227 webrtc::MediaTransportState media_transport_state() const {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200228 rtc::CritScope scope(&accessor_lock_);
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -0700229 return media_transport_state_;
230 }
231
Zhi Huange818b6e2018-02-22 15:26:27 -0800232 // This is signaled when RTCP-mux becomes active and
233 // |rtcp_dtls_transport_| is destroyed. The JsepTransportController will
234 // handle the signal and update the aggregate transport states.
235 sigslot::signal<> SignalRtcpMuxActive;
236
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -0700237 // This is signaled for changes in |media_transport_| state.
238 sigslot::signal<> SignalMediaTransportStateChanged;
239
Zhi Huange818b6e2018-02-22 15:26:27 -0800240 // TODO(deadbeef): The methods below are only public for testing. Should make
241 // them utility functions or objects so they can be tested independently from
242 // this class.
243
244 // Returns an error if the certificate's identity does not match the
245 // fingerprint, or either is NULL.
246 webrtc::RTCError VerifyCertificateFingerprint(
247 const rtc::RTCCertificate* certificate,
248 const rtc::SSLFingerprint* fingerprint) const;
249
Zhi Huangb57e1692018-06-12 11:41:11 -0700250 void SetActiveResetSrtpParams(bool active_reset_srtp_params);
251
Zhi Huange818b6e2018-02-22 15:26:27 -0800252 private:
253 bool SetRtcpMux(bool enable, webrtc::SdpType type, ContentSource source);
254
255 void ActivateRtcpMux();
256
257 bool SetSdes(const std::vector<CryptoParams>& cryptos,
258 const std::vector<int>& encrypted_extension_ids,
259 webrtc::SdpType type,
260 ContentSource source);
261
262 // Negotiates and sets the DTLS parameters based on the current local and
263 // remote transport description, such as the DTLS role to use, and whether
264 // DTLS should be activated.
265 //
266 // Called when an answer TransportDescription is applied.
267 webrtc::RTCError NegotiateAndSetDtlsParameters(
268 webrtc::SdpType local_description_type);
269
270 // Negotiates the DTLS role based off the offer and answer as specified by
271 // RFC 4145, section-4.1. Returns an RTCError if role cannot be determined
272 // from the local description and remote description.
273 webrtc::RTCError NegotiateDtlsRole(
274 webrtc::SdpType local_description_type,
275 ConnectionRole local_connection_role,
276 ConnectionRole remote_connection_role,
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200277 absl::optional<rtc::SSLRole>* negotiated_dtls_role);
Zhi Huange818b6e2018-02-22 15:26:27 -0800278
279 // Pushes down the ICE parameters from the local description, such
280 // as the ICE ufrag and pwd.
281 void SetLocalIceParameters(IceTransportInternal* ice);
282
283 // Pushes down the ICE parameters from the remote description.
284 void SetRemoteIceParameters(IceTransportInternal* ice);
285
286 // Pushes down the DTLS parameters obtained via negotiation.
287 webrtc::RTCError SetNegotiatedDtlsParameters(
288 DtlsTransportInternal* dtls_transport,
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200289 absl::optional<rtc::SSLRole> dtls_role,
Zhi Huange818b6e2018-02-22 15:26:27 -0800290 rtc::SSLFingerprint* remote_fingerprint);
291
292 bool GetTransportStats(DtlsTransportInternal* dtls_transport,
293 TransportStats* stats);
294
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -0700295 // Invoked whenever the state of the media transport changes.
296 void OnStateChanged(webrtc::MediaTransportState state) override;
297
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200298 // Owning thread, for safety checks
299 const rtc::Thread* const network_thread_;
300 // Critical scope for fields accessed off-thread
301 // TODO(https://bugs.webrtc.org/10300): Stop doing this.
302 rtc::CriticalSection accessor_lock_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800303 const std::string mid_;
304 // needs-ice-restart bit as described in JSEP.
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200305 bool needs_ice_restart_ RTC_GUARDED_BY(accessor_lock_) = false;
306 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_
307 RTC_GUARDED_BY(network_thread_);
308 std::unique_ptr<JsepTransportDescription> local_description_
309 RTC_GUARDED_BY(network_thread_);
310 std::unique_ptr<JsepTransportDescription> remote_description_
311 RTC_GUARDED_BY(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800312
313 // To avoid downcasting and make it type safe, keep three unique pointers for
314 // different SRTP mode and only one of these is non-nullptr.
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200315 // Since these are const, the variables don't need locks;
316 // accessing the objects depends on the objects' thread safety contract.
317 const std::unique_ptr<webrtc::RtpTransport> unencrypted_rtp_transport_;
318 const std::unique_ptr<webrtc::SrtpTransport> sdes_transport_;
319 const std::unique_ptr<webrtc::DtlsSrtpTransport> dtls_srtp_transport_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800320
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200321 rtc::scoped_refptr<webrtc::DtlsTransport> rtp_dtls_transport_
322 RTC_GUARDED_BY(accessor_lock_);
323 rtc::scoped_refptr<webrtc::DtlsTransport> rtcp_dtls_transport_
324 RTC_GUARDED_BY(accessor_lock_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800325
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200326 SrtpFilter sdes_negotiator_ RTC_GUARDED_BY(network_thread_);
327 RtcpMuxFilter rtcp_mux_negotiator_ RTC_GUARDED_BY(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800328
329 // Cache the encrypted header extension IDs for SDES negoitation.
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200330 absl::optional<std::vector<int>> send_extension_ids_
331 RTC_GUARDED_BY(network_thread_);
332 absl::optional<std::vector<int>> recv_extension_ids_
333 RTC_GUARDED_BY(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800334
Anton Sukhanov7940da02018-10-10 10:34:49 -0700335 // Optional media transport (experimental).
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200336 std::unique_ptr<webrtc::MediaTransportInterface> media_transport_
337 RTC_GUARDED_BY(accessor_lock_);
Anton Sukhanov7940da02018-10-10 10:34:49 -0700338
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -0700339 // If |media_transport_| is provided, this variable represents the state of
340 // media transport.
Anton Sukhanov316f3ac2019-05-23 15:50:38 -0700341 //
342 // NOTE: datagram transport state is handled by DatagramDtlsAdaptor, because
343 // DatagramDtlsAdaptor owns DatagramTransport. This state only represents
344 // media transport.
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200345 webrtc::MediaTransportState media_transport_state_
346 RTC_GUARDED_BY(accessor_lock_) = webrtc::MediaTransportState::kPending;
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -0700347
Zhi Huang365381f2018-04-13 16:44:34 -0700348 RTC_DISALLOW_COPY_AND_ASSIGN(JsepTransport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800349};
350
351} // namespace cricket
352
Steve Anton10542f22019-01-11 09:11:00 -0800353#endif // PC_JSEP_TRANSPORT_H_