blob: 479c0ae6afb06162af755d0c082e3ba3d45e47af [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
14#include <map>
15#include <memory>
16#include <string>
17#include <vector>
18
Danil Chapovalov66cadcc2018-06-19 14:47:4319#include "absl/types/optional.h"
Zhi Huange818b6e2018-02-22 23:26:2720#include "api/candidate.h"
Anton Sukhanov292ce4e2019-06-03 20:00:2421#include "api/datagram_transport_interface.h"
Zhi Huange818b6e2018-02-22 23:26:2722#include "api/jsep.h"
Anton Sukhanov7940da02018-10-10 17:34:4923#include "api/media_transport_interface.h"
Steve Anton10542f22019-01-11 17:11:0024#include "p2p/base/dtls_transport.h"
25#include "p2p/base/p2p_constants.h"
26#include "p2p/base/transport_info.h"
Bjorn A Mellemc85ebbe2019-06-07 17:28:0627#include "pc/composite_rtp_transport.h"
Steve Anton10542f22019-01-11 17:11:0028#include "pc/dtls_srtp_transport.h"
29#include "pc/dtls_transport.h"
30#include "pc/rtcp_mux_filter.h"
31#include "pc/rtp_transport.h"
32#include "pc/session_description.h"
33#include "pc/srtp_filter.h"
34#include "pc/srtp_transport.h"
35#include "pc/transport_stats.h"
36#include "rtc_base/constructor_magic.h"
37#include "rtc_base/message_queue.h"
38#include "rtc_base/rtc_certificate.h"
39#include "rtc_base/ssl_stream_adapter.h"
Artem Titove41c4332018-07-25 13:04:2840#include "rtc_base/third_party/sigslot/sigslot.h"
Harald Alvestrand78a5e962019-04-03 08:42:3941#include "rtc_base/thread_checker.h"
Zhi Huange818b6e2018-02-22 23:26:2742
43namespace cricket {
44
45class DtlsTransportInternal;
46
47struct JsepTransportDescription {
48 public:
49 JsepTransportDescription();
50 JsepTransportDescription(
51 bool rtcp_mux_enabled,
52 const std::vector<CryptoParams>& cryptos,
53 const std::vector<int>& encrypted_header_extension_ids,
Zhi Huange830e682018-03-30 17:48:3554 int rtp_abs_sendtime_extn_id,
Zhi Huange818b6e2018-02-22 23:26:2755 const TransportDescription& transport_description);
56 JsepTransportDescription(const JsepTransportDescription& from);
57 ~JsepTransportDescription();
58
59 JsepTransportDescription& operator=(const JsepTransportDescription& from);
60
61 bool rtcp_mux_enabled = true;
62 std::vector<CryptoParams> cryptos;
63 std::vector<int> encrypted_header_extension_ids;
Zhi Huange830e682018-03-30 17:48:3564 int rtp_abs_sendtime_extn_id = -1;
Zhi Huange818b6e2018-02-22 23:26:2765 // TODO(zhihuang): Add the ICE and DTLS related variables and methods from
66 // TransportDescription and remove this extra layer of abstraction.
67 TransportDescription transport_desc;
68};
69
70// Helper class used by JsepTransportController that processes
71// TransportDescriptions. A TransportDescription represents the
72// transport-specific properties of an SDP m= section, processed according to
73// JSEP. Each transport consists of DTLS and ICE transport channels for RTP
74// (and possibly RTCP, if rtcp-mux isn't used).
75//
Zhi Huang365381f2018-04-13 23:44:3476// On Threading: JsepTransport performs work solely on the network thread, and
Zhi Huange818b6e2018-02-22 23:26:2777// so its methods should only be called on the network thread.
Piotr (Peter) Slatala4eb41122018-11-01 14:26:0378class JsepTransport : public sigslot::has_slots<>,
79 public webrtc::MediaTransportStateCallback {
Zhi Huange818b6e2018-02-22 23:26:2780 public:
81 // |mid| is just used for log statements in order to identify the Transport.
82 // Note that |local_certificate| is allowed to be null since a remote
83 // description may be set before a local certificate is generated.
Anton Sukhanov7940da02018-10-10 17:34:4984 //
85 // |media_trasport| is optional (experimental). If available it will be used
86 // to send / receive encoded audio and video frames instead of RTP.
87 // Currently |media_transport| can co-exist with RTP / RTCP transports.
Zhi Huang365381f2018-04-13 23:44:3488 JsepTransport(
Zhi Huange818b6e2018-02-22 23:26:2789 const std::string& mid,
90 const rtc::scoped_refptr<rtc::RTCCertificate>& local_certificate,
Bjorn A Mellem0c1c1b42019-05-30 00:34:1391 std::unique_ptr<cricket::IceTransportInternal> ice_transport,
92 std::unique_ptr<cricket::IceTransportInternal> rtcp_ice_transport,
Zhi Huange818b6e2018-02-22 23:26:2793 std::unique_ptr<webrtc::RtpTransport> unencrypted_rtp_transport,
94 std::unique_ptr<webrtc::SrtpTransport> sdes_transport,
95 std::unique_ptr<webrtc::DtlsSrtpTransport> dtls_srtp_transport,
Bjorn A Mellemc85ebbe2019-06-07 17:28:0696 std::unique_ptr<webrtc::RtpTransport> datagram_rtp_transport,
Zhi Huange818b6e2018-02-22 23:26:2797 std::unique_ptr<DtlsTransportInternal> rtp_dtls_transport,
Anton Sukhanov7940da02018-10-10 17:34:4998 std::unique_ptr<DtlsTransportInternal> rtcp_dtls_transport,
Bjorn A Mellemc85ebbe2019-06-07 17:28:0699 std::unique_ptr<DtlsTransportInternal> datagram_dtls_transport,
Anton Sukhanov292ce4e2019-06-03 20:00:24100 std::unique_ptr<webrtc::MediaTransportInterface> media_transport,
101 std::unique_ptr<webrtc::DatagramTransportInterface> datagram_transport);
Zhi Huange818b6e2018-02-22 23:26:27102
Zhi Huang365381f2018-04-13 23:44:34103 ~JsepTransport() override;
Zhi Huange818b6e2018-02-22 23:26:27104
105 // Returns the MID of this transport. This is only used for logging.
106 const std::string& mid() const { return mid_; }
107
108 // Must be called before applying local session description.
109 // Needed in order to verify the local fingerprint.
110 void SetLocalCertificate(
111 const rtc::scoped_refptr<rtc::RTCCertificate>& local_certificate) {
Harald Alvestrand78a5e962019-04-03 08:42:39112 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27113 local_certificate_ = local_certificate;
114 }
115
116 // Return the local certificate provided by SetLocalCertificate.
117 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const {
Harald Alvestrand78a5e962019-04-03 08:42:39118 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27119 return local_certificate_;
120 }
121
122 webrtc::RTCError SetLocalJsepTransportDescription(
123 const JsepTransportDescription& jsep_description,
124 webrtc::SdpType type);
125
126 // Set the remote TransportDescription to be used by DTLS and ICE channels
127 // that are part of this Transport.
128 webrtc::RTCError SetRemoteJsepTransportDescription(
129 const JsepTransportDescription& jsep_description,
130 webrtc::SdpType type);
Zhi Huange818b6e2018-02-22 23:26:27131 webrtc::RTCError AddRemoteCandidates(const Candidates& candidates);
132
133 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
134 // set, offers should generate new ufrags/passwords until an ICE restart
135 // occurs.
136 //
137 // This and the below method can be called safely from any thread as long as
138 // SetXTransportDescription is not in progress.
139 void SetNeedsIceRestartFlag();
140 // Returns true if the ICE restart flag above was set, and no ICE restart has
141 // occurred yet for this transport (by applying a local description with
142 // changed ufrag/password).
Harald Alvestrand78a5e962019-04-03 08:42:39143 bool needs_ice_restart() const {
144 rtc::CritScope scope(&accessor_lock_);
145 return needs_ice_restart_;
146 }
Zhi Huange818b6e2018-02-22 23:26:27147
Danil Chapovalov66cadcc2018-06-19 14:47:43148 // Returns role if negotiated, or empty absl::optional if it hasn't been
149 // negotiated yet.
150 absl::optional<rtc::SSLRole> GetDtlsRole() const;
Zhi Huange818b6e2018-02-22 23:26:27151
Bjorn A Mellemc85ebbe2019-06-07 17:28:06152 absl::optional<OpaqueTransportParameters> GetTransportParameters() const;
153
Zhi Huange818b6e2018-02-22 23:26:27154 // TODO(deadbeef): Make this const. See comment in transportcontroller.h.
155 bool GetStats(TransportStats* stats);
156
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
167 webrtc::RtpTransportInternal* rtp_transport() const {
Bjorn A Mellemc85ebbe2019-06-07 17:28:06168 rtc::CritScope scope(&accessor_lock_);
169 if (composite_rtp_transport_) {
170 return composite_rtp_transport_.get();
171 } else if (datagram_rtp_transport_) {
172 return datagram_rtp_transport_.get();
Zhi Huange818b6e2018-02-22 23:26:27173 } else {
Bjorn A Mellemc85ebbe2019-06-07 17:28:06174 return default_rtp_transport();
Zhi Huange818b6e2018-02-22 23:26:27175 }
176 }
177
Harald Alvestrandad88c882018-11-28 15:47:46178 const DtlsTransportInternal* rtp_dtls_transport() const {
Harald Alvestrand78a5e962019-04-03 08:42:39179 rtc::CritScope scope(&accessor_lock_);
Harald Alvestrandad88c882018-11-28 15:47:46180 if (rtp_dtls_transport_) {
181 return rtp_dtls_transport_->internal();
182 } else {
183 return nullptr;
184 }
Zhi Huange818b6e2018-02-22 23:26:27185 }
186
Harald Alvestrandad88c882018-11-28 15:47:46187 DtlsTransportInternal* rtp_dtls_transport() {
Harald Alvestrand78a5e962019-04-03 08:42:39188 rtc::CritScope scope(&accessor_lock_);
Harald Alvestrandad88c882018-11-28 15:47:46189 if (rtp_dtls_transport_) {
190 return rtp_dtls_transport_->internal();
191 } else {
192 return nullptr;
193 }
194 }
195
196 const DtlsTransportInternal* rtcp_dtls_transport() const {
Harald Alvestrand78a5e962019-04-03 08:42:39197 rtc::CritScope scope(&accessor_lock_);
Harald Alvestrandad88c882018-11-28 15:47:46198 if (rtcp_dtls_transport_) {
199 return rtcp_dtls_transport_->internal();
200 } else {
201 return nullptr;
202 }
203 }
204
205 DtlsTransportInternal* rtcp_dtls_transport() {
Harald Alvestrand78a5e962019-04-03 08:42:39206 rtc::CritScope scope(&accessor_lock_);
Harald Alvestrandad88c882018-11-28 15:47:46207 if (rtcp_dtls_transport_) {
208 return rtcp_dtls_transport_->internal();
209 } else {
210 return nullptr;
211 }
212 }
213
Harald Alvestrand4a7b3ac2019-01-17 09:39:40214 rtc::scoped_refptr<webrtc::DtlsTransport> RtpDtlsTransport() {
Harald Alvestrand78a5e962019-04-03 08:42:39215 rtc::CritScope scope(&accessor_lock_);
Harald Alvestrandad88c882018-11-28 15:47:46216 return rtp_dtls_transport_;
Zhi Huange818b6e2018-02-22 23:26:27217 }
218
Anton Sukhanov7940da02018-10-10 17:34:49219 // Returns media transport, if available.
220 // Note that media transport is owned by jseptransport and the pointer
221 // to media transport will becomes invalid after destruction of jseptransport.
222 webrtc::MediaTransportInterface* media_transport() const {
Harald Alvestrand78a5e962019-04-03 08:42:39223 rtc::CritScope scope(&accessor_lock_);
Anton Sukhanov7940da02018-10-10 17:34:49224 return media_transport_.get();
225 }
226
Anton Sukhanov316f3ac2019-05-23 22:50:38227 // Returns datagram transport, if available.
228 webrtc::DatagramTransportInterface* datagram_transport() const {
229 rtc::CritScope scope(&accessor_lock_);
Anton Sukhanov292ce4e2019-06-03 20:00:24230 return datagram_transport_.get();
Anton Sukhanov316f3ac2019-05-23 22:50:38231 }
232
Piotr (Peter) Slatala4eb41122018-11-01 14:26:03233 // Returns the latest media transport state.
234 webrtc::MediaTransportState media_transport_state() const {
Harald Alvestrand78a5e962019-04-03 08:42:39235 rtc::CritScope scope(&accessor_lock_);
Piotr (Peter) Slatala4eb41122018-11-01 14:26:03236 return media_transport_state_;
237 }
238
Zhi Huange818b6e2018-02-22 23:26:27239 // This is signaled when RTCP-mux becomes active and
240 // |rtcp_dtls_transport_| is destroyed. The JsepTransportController will
241 // handle the signal and update the aggregate transport states.
242 sigslot::signal<> SignalRtcpMuxActive;
243
Piotr (Peter) Slatala4eb41122018-11-01 14:26:03244 // This is signaled for changes in |media_transport_| state.
245 sigslot::signal<> SignalMediaTransportStateChanged;
246
Zhi Huange818b6e2018-02-22 23:26:27247 // TODO(deadbeef): The methods below are only public for testing. Should make
248 // them utility functions or objects so they can be tested independently from
249 // this class.
250
251 // Returns an error if the certificate's identity does not match the
252 // fingerprint, or either is NULL.
253 webrtc::RTCError VerifyCertificateFingerprint(
254 const rtc::RTCCertificate* certificate,
255 const rtc::SSLFingerprint* fingerprint) const;
256
Zhi Huangb57e1692018-06-12 18:41:11257 void SetActiveResetSrtpParams(bool active_reset_srtp_params);
258
Zhi Huange818b6e2018-02-22 23:26:27259 private:
260 bool SetRtcpMux(bool enable, webrtc::SdpType type, ContentSource source);
261
262 void ActivateRtcpMux();
263
264 bool SetSdes(const std::vector<CryptoParams>& cryptos,
265 const std::vector<int>& encrypted_extension_ids,
266 webrtc::SdpType type,
267 ContentSource source);
268
269 // Negotiates and sets the DTLS parameters based on the current local and
270 // remote transport description, such as the DTLS role to use, and whether
271 // DTLS should be activated.
272 //
273 // Called when an answer TransportDescription is applied.
274 webrtc::RTCError NegotiateAndSetDtlsParameters(
275 webrtc::SdpType local_description_type);
276
277 // Negotiates the DTLS role based off the offer and answer as specified by
278 // RFC 4145, section-4.1. Returns an RTCError if role cannot be determined
279 // from the local description and remote description.
280 webrtc::RTCError NegotiateDtlsRole(
281 webrtc::SdpType local_description_type,
282 ConnectionRole local_connection_role,
283 ConnectionRole remote_connection_role,
Danil Chapovalov66cadcc2018-06-19 14:47:43284 absl::optional<rtc::SSLRole>* negotiated_dtls_role);
Zhi Huange818b6e2018-02-22 23:26:27285
286 // Pushes down the ICE parameters from the local description, such
287 // as the ICE ufrag and pwd.
288 void SetLocalIceParameters(IceTransportInternal* ice);
289
290 // Pushes down the ICE parameters from the remote description.
291 void SetRemoteIceParameters(IceTransportInternal* ice);
292
293 // Pushes down the DTLS parameters obtained via negotiation.
294 webrtc::RTCError SetNegotiatedDtlsParameters(
295 DtlsTransportInternal* dtls_transport,
Danil Chapovalov66cadcc2018-06-19 14:47:43296 absl::optional<rtc::SSLRole> dtls_role,
Zhi Huange818b6e2018-02-22 23:26:27297 rtc::SSLFingerprint* remote_fingerprint);
298
299 bool GetTransportStats(DtlsTransportInternal* dtls_transport,
300 TransportStats* stats);
301
Piotr (Peter) Slatala4eb41122018-11-01 14:26:03302 // Invoked whenever the state of the media transport changes.
303 void OnStateChanged(webrtc::MediaTransportState state) override;
304
Bjorn A Mellemc85ebbe2019-06-07 17:28:06305 // Deactivates, signals removal, and deletes |composite_rtp_transport_| if the
306 // current state of negotiation is sufficient to determine which rtp_transport
307 // to use.
308 void NegotiateRtpTransport(webrtc::SdpType type) RTC_RUN_ON(network_thread_);
309
310 // Returns the default (non-datagram) rtp transport, if any.
311 webrtc::RtpTransportInternal* default_rtp_transport() const
312 RTC_EXCLUSIVE_LOCKS_REQUIRED(accessor_lock_) {
313 if (dtls_srtp_transport_) {
314 return dtls_srtp_transport_.get();
315 } else if (sdes_transport_) {
316 return sdes_transport_.get();
317 } else if (unencrypted_rtp_transport_) {
318 return unencrypted_rtp_transport_.get();
319 } else {
320 return nullptr;
321 }
322 }
323
Harald Alvestrand78a5e962019-04-03 08:42:39324 // Owning thread, for safety checks
325 const rtc::Thread* const network_thread_;
326 // Critical scope for fields accessed off-thread
327 // TODO(https://bugs.webrtc.org/10300): Stop doing this.
328 rtc::CriticalSection accessor_lock_;
Zhi Huange818b6e2018-02-22 23:26:27329 const std::string mid_;
330 // needs-ice-restart bit as described in JSEP.
Harald Alvestrand78a5e962019-04-03 08:42:39331 bool needs_ice_restart_ RTC_GUARDED_BY(accessor_lock_) = false;
332 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_
333 RTC_GUARDED_BY(network_thread_);
334 std::unique_ptr<JsepTransportDescription> local_description_
335 RTC_GUARDED_BY(network_thread_);
336 std::unique_ptr<JsepTransportDescription> remote_description_
337 RTC_GUARDED_BY(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27338
Bjorn A Mellem0c1c1b42019-05-30 00:34:13339 // Ice transport which may be used by any of upper-layer transports (below).
340 // Owned by JsepTransport and guaranteed to outlive the transports below.
341 const std::unique_ptr<cricket::IceTransportInternal> ice_transport_;
342 const std::unique_ptr<cricket::IceTransportInternal> rtcp_ice_transport_;
343
Zhi Huange818b6e2018-02-22 23:26:27344 // To avoid downcasting and make it type safe, keep three unique pointers for
345 // different SRTP mode and only one of these is non-nullptr.
Bjorn A Mellemc85ebbe2019-06-07 17:28:06346 std::unique_ptr<webrtc::RtpTransport> unencrypted_rtp_transport_
347 RTC_GUARDED_BY(accessor_lock_);
348 std::unique_ptr<webrtc::SrtpTransport> sdes_transport_
349 RTC_GUARDED_BY(accessor_lock_);
350 std::unique_ptr<webrtc::DtlsSrtpTransport> dtls_srtp_transport_
351 RTC_GUARDED_BY(accessor_lock_);
352 std::unique_ptr<webrtc::RtpTransport> datagram_rtp_transport_
353 RTC_GUARDED_BY(accessor_lock_);
354
355 // If multiple RTP transports are in use, |composite_rtp_transport_| will be
356 // passed to callers. This is only valid for offer-only, receive-only
357 // scenarios, as it is not possible for the composite to correctly choose
358 // which transport to use for sending.
359 std::unique_ptr<webrtc::CompositeRtpTransport> composite_rtp_transport_
360 RTC_GUARDED_BY(accessor_lock_);
Zhi Huange818b6e2018-02-22 23:26:27361
Harald Alvestrand78a5e962019-04-03 08:42:39362 rtc::scoped_refptr<webrtc::DtlsTransport> rtp_dtls_transport_
363 RTC_GUARDED_BY(accessor_lock_);
364 rtc::scoped_refptr<webrtc::DtlsTransport> rtcp_dtls_transport_
365 RTC_GUARDED_BY(accessor_lock_);
Bjorn A Mellemc85ebbe2019-06-07 17:28:06366 rtc::scoped_refptr<webrtc::DtlsTransport> datagram_dtls_transport_
367 RTC_GUARDED_BY(accessor_lock_);
Zhi Huange818b6e2018-02-22 23:26:27368
Harald Alvestrand78a5e962019-04-03 08:42:39369 SrtpFilter sdes_negotiator_ RTC_GUARDED_BY(network_thread_);
370 RtcpMuxFilter rtcp_mux_negotiator_ RTC_GUARDED_BY(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27371
372 // Cache the encrypted header extension IDs for SDES negoitation.
Harald Alvestrand78a5e962019-04-03 08:42:39373 absl::optional<std::vector<int>> send_extension_ids_
374 RTC_GUARDED_BY(network_thread_);
375 absl::optional<std::vector<int>> recv_extension_ids_
376 RTC_GUARDED_BY(network_thread_);
Zhi Huange818b6e2018-02-22 23:26:27377
Anton Sukhanov7940da02018-10-10 17:34:49378 // Optional media transport (experimental).
Harald Alvestrand78a5e962019-04-03 08:42:39379 std::unique_ptr<webrtc::MediaTransportInterface> media_transport_
380 RTC_GUARDED_BY(accessor_lock_);
Anton Sukhanov7940da02018-10-10 17:34:49381
Anton Sukhanov292ce4e2019-06-03 20:00:24382 // Optional datagram transport (experimental).
383 std::unique_ptr<webrtc::DatagramTransportInterface> datagram_transport_
384 RTC_GUARDED_BY(accessor_lock_);
385
Piotr (Peter) Slatala4eb41122018-11-01 14:26:03386 // If |media_transport_| is provided, this variable represents the state of
387 // media transport.
Anton Sukhanov316f3ac2019-05-23 22:50:38388 //
389 // NOTE: datagram transport state is handled by DatagramDtlsAdaptor, because
390 // DatagramDtlsAdaptor owns DatagramTransport. This state only represents
391 // media transport.
Harald Alvestrand78a5e962019-04-03 08:42:39392 webrtc::MediaTransportState media_transport_state_
393 RTC_GUARDED_BY(accessor_lock_) = webrtc::MediaTransportState::kPending;
Piotr (Peter) Slatala4eb41122018-11-01 14:26:03394
Zhi Huang365381f2018-04-13 23:44:34395 RTC_DISALLOW_COPY_AND_ASSIGN(JsepTransport);
Zhi Huange818b6e2018-02-22 23:26:27396};
397
398} // namespace cricket
399
Steve Anton10542f22019-01-11 17:11:00400#endif // PC_JSEP_TRANSPORT_H_