blob: 0f314039d5e753fc604c2478846a80cb43040d69 [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
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -0700220 // Returns the latest media transport state.
221 webrtc::MediaTransportState media_transport_state() const {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200222 rtc::CritScope scope(&accessor_lock_);
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -0700223 return media_transport_state_;
224 }
225
Zhi Huange818b6e2018-02-22 15:26:27 -0800226 // This is signaled when RTCP-mux becomes active and
227 // |rtcp_dtls_transport_| is destroyed. The JsepTransportController will
228 // handle the signal and update the aggregate transport states.
229 sigslot::signal<> SignalRtcpMuxActive;
230
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -0700231 // This is signaled for changes in |media_transport_| state.
232 sigslot::signal<> SignalMediaTransportStateChanged;
233
Zhi Huange818b6e2018-02-22 15:26:27 -0800234 // TODO(deadbeef): The methods below are only public for testing. Should make
235 // them utility functions or objects so they can be tested independently from
236 // this class.
237
238 // Returns an error if the certificate's identity does not match the
239 // fingerprint, or either is NULL.
240 webrtc::RTCError VerifyCertificateFingerprint(
241 const rtc::RTCCertificate* certificate,
242 const rtc::SSLFingerprint* fingerprint) const;
243
Zhi Huangb57e1692018-06-12 11:41:11 -0700244 void SetActiveResetSrtpParams(bool active_reset_srtp_params);
245
Zhi Huange818b6e2018-02-22 15:26:27 -0800246 private:
247 bool SetRtcpMux(bool enable, webrtc::SdpType type, ContentSource source);
248
249 void ActivateRtcpMux();
250
251 bool SetSdes(const std::vector<CryptoParams>& cryptos,
252 const std::vector<int>& encrypted_extension_ids,
253 webrtc::SdpType type,
254 ContentSource source);
255
256 // Negotiates and sets the DTLS parameters based on the current local and
257 // remote transport description, such as the DTLS role to use, and whether
258 // DTLS should be activated.
259 //
260 // Called when an answer TransportDescription is applied.
261 webrtc::RTCError NegotiateAndSetDtlsParameters(
262 webrtc::SdpType local_description_type);
263
264 // Negotiates the DTLS role based off the offer and answer as specified by
265 // RFC 4145, section-4.1. Returns an RTCError if role cannot be determined
266 // from the local description and remote description.
267 webrtc::RTCError NegotiateDtlsRole(
268 webrtc::SdpType local_description_type,
269 ConnectionRole local_connection_role,
270 ConnectionRole remote_connection_role,
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200271 absl::optional<rtc::SSLRole>* negotiated_dtls_role);
Zhi Huange818b6e2018-02-22 15:26:27 -0800272
273 // Pushes down the ICE parameters from the local description, such
274 // as the ICE ufrag and pwd.
275 void SetLocalIceParameters(IceTransportInternal* ice);
276
277 // Pushes down the ICE parameters from the remote description.
278 void SetRemoteIceParameters(IceTransportInternal* ice);
279
280 // Pushes down the DTLS parameters obtained via negotiation.
281 webrtc::RTCError SetNegotiatedDtlsParameters(
282 DtlsTransportInternal* dtls_transport,
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200283 absl::optional<rtc::SSLRole> dtls_role,
Zhi Huange818b6e2018-02-22 15:26:27 -0800284 rtc::SSLFingerprint* remote_fingerprint);
285
286 bool GetTransportStats(DtlsTransportInternal* dtls_transport,
287 TransportStats* stats);
288
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -0700289 // Invoked whenever the state of the media transport changes.
290 void OnStateChanged(webrtc::MediaTransportState state) override;
291
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200292 // Owning thread, for safety checks
293 const rtc::Thread* const network_thread_;
294 // Critical scope for fields accessed off-thread
295 // TODO(https://bugs.webrtc.org/10300): Stop doing this.
296 rtc::CriticalSection accessor_lock_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800297 const std::string mid_;
298 // needs-ice-restart bit as described in JSEP.
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200299 bool needs_ice_restart_ RTC_GUARDED_BY(accessor_lock_) = false;
300 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_
301 RTC_GUARDED_BY(network_thread_);
302 std::unique_ptr<JsepTransportDescription> local_description_
303 RTC_GUARDED_BY(network_thread_);
304 std::unique_ptr<JsepTransportDescription> remote_description_
305 RTC_GUARDED_BY(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800306
307 // To avoid downcasting and make it type safe, keep three unique pointers for
308 // different SRTP mode and only one of these is non-nullptr.
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200309 // Since these are const, the variables don't need locks;
310 // accessing the objects depends on the objects' thread safety contract.
311 const std::unique_ptr<webrtc::RtpTransport> unencrypted_rtp_transport_;
312 const std::unique_ptr<webrtc::SrtpTransport> sdes_transport_;
313 const std::unique_ptr<webrtc::DtlsSrtpTransport> dtls_srtp_transport_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800314
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200315 rtc::scoped_refptr<webrtc::DtlsTransport> rtp_dtls_transport_
316 RTC_GUARDED_BY(accessor_lock_);
317 rtc::scoped_refptr<webrtc::DtlsTransport> rtcp_dtls_transport_
318 RTC_GUARDED_BY(accessor_lock_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800319
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200320 SrtpFilter sdes_negotiator_ RTC_GUARDED_BY(network_thread_);
321 RtcpMuxFilter rtcp_mux_negotiator_ RTC_GUARDED_BY(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800322
323 // Cache the encrypted header extension IDs for SDES negoitation.
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200324 absl::optional<std::vector<int>> send_extension_ids_
325 RTC_GUARDED_BY(network_thread_);
326 absl::optional<std::vector<int>> recv_extension_ids_
327 RTC_GUARDED_BY(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800328
Anton Sukhanov7940da02018-10-10 10:34:49 -0700329 // Optional media transport (experimental).
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200330 std::unique_ptr<webrtc::MediaTransportInterface> media_transport_
331 RTC_GUARDED_BY(accessor_lock_);
Anton Sukhanov7940da02018-10-10 10:34:49 -0700332
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -0700333 // If |media_transport_| is provided, this variable represents the state of
334 // media transport.
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200335 webrtc::MediaTransportState media_transport_state_
336 RTC_GUARDED_BY(accessor_lock_) = webrtc::MediaTransportState::kPending;
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -0700337
Zhi Huang365381f2018-04-13 16:44:34 -0700338 RTC_DISALLOW_COPY_AND_ASSIGN(JsepTransport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800339};
340
341} // namespace cricket
342
Steve Anton10542f22019-01-11 09:11:00 -0800343#endif // PC_JSEP_TRANSPORT_H_