blob: fc9ed5ba7e5b3fba5ddfc41ec176817ed77c9359 [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
Zhi Huang365381f2018-04-13 16:44:34 -070011#ifndef PC_JSEPTRANSPORT_H_
12#define PC_JSEPTRANSPORT_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"
Zhi Huange818b6e2018-02-22 15:26:27 -080022#include "p2p/base/dtlstransport.h"
23#include "p2p/base/p2pconstants.h"
24#include "p2p/base/transportinfo.h"
25#include "pc/dtlssrtptransport.h"
26#include "pc/rtcpmuxfilter.h"
27#include "pc/rtptransport.h"
28#include "pc/sessiondescription.h"
29#include "pc/srtpfilter.h"
30#include "pc/srtptransport.h"
31#include "pc/transportstats.h"
32#include "rtc_base/constructormagic.h"
33#include "rtc_base/messagequeue.h"
34#include "rtc_base/rtccertificate.h"
35#include "rtc_base/sigslot.h"
36#include "rtc_base/sslstreamadapter.h"
37
38namespace cricket {
39
40class DtlsTransportInternal;
41
42struct JsepTransportDescription {
43 public:
44 JsepTransportDescription();
45 JsepTransportDescription(
46 bool rtcp_mux_enabled,
47 const std::vector<CryptoParams>& cryptos,
48 const std::vector<int>& encrypted_header_extension_ids,
Zhi Huange830e682018-03-30 10:48:35 -070049 int rtp_abs_sendtime_extn_id,
Zhi Huange818b6e2018-02-22 15:26:27 -080050 const TransportDescription& transport_description);
51 JsepTransportDescription(const JsepTransportDescription& from);
52 ~JsepTransportDescription();
53
54 JsepTransportDescription& operator=(const JsepTransportDescription& from);
55
56 bool rtcp_mux_enabled = true;
57 std::vector<CryptoParams> cryptos;
58 std::vector<int> encrypted_header_extension_ids;
Zhi Huange830e682018-03-30 10:48:35 -070059 int rtp_abs_sendtime_extn_id = -1;
Zhi Huange818b6e2018-02-22 15:26:27 -080060 // TODO(zhihuang): Add the ICE and DTLS related variables and methods from
61 // TransportDescription and remove this extra layer of abstraction.
62 TransportDescription transport_desc;
63};
64
65// Helper class used by JsepTransportController that processes
66// TransportDescriptions. A TransportDescription represents the
67// transport-specific properties of an SDP m= section, processed according to
68// JSEP. Each transport consists of DTLS and ICE transport channels for RTP
69// (and possibly RTCP, if rtcp-mux isn't used).
70//
Zhi Huang365381f2018-04-13 16:44:34 -070071// On Threading: JsepTransport performs work solely on the network thread, and
Zhi Huange818b6e2018-02-22 15:26:27 -080072// so its methods should only be called on the network thread.
Zhi Huang365381f2018-04-13 16:44:34 -070073class JsepTransport : public sigslot::has_slots<> {
Zhi Huange818b6e2018-02-22 15:26:27 -080074 public:
75 // |mid| is just used for log statements in order to identify the Transport.
76 // Note that |local_certificate| is allowed to be null since a remote
77 // description may be set before a local certificate is generated.
Zhi Huang365381f2018-04-13 16:44:34 -070078 JsepTransport(
Zhi Huange818b6e2018-02-22 15:26:27 -080079 const std::string& mid,
80 const rtc::scoped_refptr<rtc::RTCCertificate>& local_certificate,
81 std::unique_ptr<webrtc::RtpTransport> unencrypted_rtp_transport,
82 std::unique_ptr<webrtc::SrtpTransport> sdes_transport,
83 std::unique_ptr<webrtc::DtlsSrtpTransport> dtls_srtp_transport,
84 std::unique_ptr<DtlsTransportInternal> rtp_dtls_transport,
85 std::unique_ptr<DtlsTransportInternal> rtcp_dtls_transport);
86
Zhi Huang365381f2018-04-13 16:44:34 -070087 ~JsepTransport() override;
Zhi Huange818b6e2018-02-22 15:26:27 -080088
89 // Returns the MID of this transport. This is only used for logging.
90 const std::string& mid() const { return mid_; }
91
92 // Must be called before applying local session description.
93 // Needed in order to verify the local fingerprint.
94 void SetLocalCertificate(
95 const rtc::scoped_refptr<rtc::RTCCertificate>& local_certificate) {
96 local_certificate_ = local_certificate;
97 }
98
99 // Return the local certificate provided by SetLocalCertificate.
100 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const {
101 return local_certificate_;
102 }
103
104 webrtc::RTCError SetLocalJsepTransportDescription(
105 const JsepTransportDescription& jsep_description,
106 webrtc::SdpType type);
107
108 // Set the remote TransportDescription to be used by DTLS and ICE channels
109 // that are part of this Transport.
110 webrtc::RTCError SetRemoteJsepTransportDescription(
111 const JsepTransportDescription& jsep_description,
112 webrtc::SdpType type);
113
114 webrtc::RTCError AddRemoteCandidates(const Candidates& candidates);
115
116 // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
117 // set, offers should generate new ufrags/passwords until an ICE restart
118 // occurs.
119 //
120 // This and the below method can be called safely from any thread as long as
121 // SetXTransportDescription is not in progress.
122 void SetNeedsIceRestartFlag();
123 // Returns true if the ICE restart flag above was set, and no ICE restart has
124 // occurred yet for this transport (by applying a local description with
125 // changed ufrag/password).
126 bool needs_ice_restart() const { return needs_ice_restart_; }
127
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200128 // Returns role if negotiated, or empty absl::optional if it hasn't been
129 // negotiated yet.
130 absl::optional<rtc::SSLRole> GetDtlsRole() const;
Zhi Huange818b6e2018-02-22 15:26:27 -0800131
132 // TODO(deadbeef): Make this const. See comment in transportcontroller.h.
133 bool GetStats(TransportStats* stats);
134
135 const JsepTransportDescription* local_description() const {
136 return local_description_.get();
137 }
138
139 const JsepTransportDescription* remote_description() const {
140 return remote_description_.get();
141 }
142
143 webrtc::RtpTransportInternal* rtp_transport() const {
144 if (dtls_srtp_transport_) {
145 return dtls_srtp_transport_.get();
146 } else if (sdes_transport_) {
147 return sdes_transport_.get();
148 } else {
149 return unencrypted_rtp_transport_.get();
150 }
151 }
152
153 DtlsTransportInternal* rtp_dtls_transport() const {
154 return rtp_dtls_transport_.get();
155 }
156
157 DtlsTransportInternal* rtcp_dtls_transport() const {
158 return rtcp_dtls_transport_.get();
159 }
160
161 // This is signaled when RTCP-mux becomes active and
162 // |rtcp_dtls_transport_| is destroyed. The JsepTransportController will
163 // handle the signal and update the aggregate transport states.
164 sigslot::signal<> SignalRtcpMuxActive;
165
166 // TODO(deadbeef): The methods below are only public for testing. Should make
167 // them utility functions or objects so they can be tested independently from
168 // this class.
169
170 // Returns an error if the certificate's identity does not match the
171 // fingerprint, or either is NULL.
172 webrtc::RTCError VerifyCertificateFingerprint(
173 const rtc::RTCCertificate* certificate,
174 const rtc::SSLFingerprint* fingerprint) const;
175
Zhi Huangb57e1692018-06-12 11:41:11 -0700176 void SetActiveResetSrtpParams(bool active_reset_srtp_params);
177
Zhi Huange818b6e2018-02-22 15:26:27 -0800178 private:
179 bool SetRtcpMux(bool enable, webrtc::SdpType type, ContentSource source);
180
181 void ActivateRtcpMux();
182
183 bool SetSdes(const std::vector<CryptoParams>& cryptos,
184 const std::vector<int>& encrypted_extension_ids,
185 webrtc::SdpType type,
186 ContentSource source);
187
188 // Negotiates and sets the DTLS parameters based on the current local and
189 // remote transport description, such as the DTLS role to use, and whether
190 // DTLS should be activated.
191 //
192 // Called when an answer TransportDescription is applied.
193 webrtc::RTCError NegotiateAndSetDtlsParameters(
194 webrtc::SdpType local_description_type);
195
196 // Negotiates the DTLS role based off the offer and answer as specified by
197 // RFC 4145, section-4.1. Returns an RTCError if role cannot be determined
198 // from the local description and remote description.
199 webrtc::RTCError NegotiateDtlsRole(
200 webrtc::SdpType local_description_type,
201 ConnectionRole local_connection_role,
202 ConnectionRole remote_connection_role,
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200203 absl::optional<rtc::SSLRole>* negotiated_dtls_role);
Zhi Huange818b6e2018-02-22 15:26:27 -0800204
205 // Pushes down the ICE parameters from the local description, such
206 // as the ICE ufrag and pwd.
207 void SetLocalIceParameters(IceTransportInternal* ice);
208
209 // Pushes down the ICE parameters from the remote description.
210 void SetRemoteIceParameters(IceTransportInternal* ice);
211
212 // Pushes down the DTLS parameters obtained via negotiation.
213 webrtc::RTCError SetNegotiatedDtlsParameters(
214 DtlsTransportInternal* dtls_transport,
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200215 absl::optional<rtc::SSLRole> dtls_role,
Zhi Huange818b6e2018-02-22 15:26:27 -0800216 rtc::SSLFingerprint* remote_fingerprint);
217
218 bool GetTransportStats(DtlsTransportInternal* dtls_transport,
219 TransportStats* stats);
220
221 const std::string mid_;
222 // needs-ice-restart bit as described in JSEP.
223 bool needs_ice_restart_ = false;
224 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_;
225 std::unique_ptr<JsepTransportDescription> local_description_;
226 std::unique_ptr<JsepTransportDescription> remote_description_;
227
228 // To avoid downcasting and make it type safe, keep three unique pointers for
229 // different SRTP mode and only one of these is non-nullptr.
230 std::unique_ptr<webrtc::RtpTransport> unencrypted_rtp_transport_;
231 std::unique_ptr<webrtc::SrtpTransport> sdes_transport_;
232 std::unique_ptr<webrtc::DtlsSrtpTransport> dtls_srtp_transport_;
233
234 std::unique_ptr<DtlsTransportInternal> rtp_dtls_transport_;
235 std::unique_ptr<DtlsTransportInternal> rtcp_dtls_transport_;
236
237 SrtpFilter sdes_negotiator_;
238 RtcpMuxFilter rtcp_mux_negotiator_;
239
240 // Cache the encrypted header extension IDs for SDES negoitation.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200241 absl::optional<std::vector<int>> send_extension_ids_;
242 absl::optional<std::vector<int>> recv_extension_ids_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800243
Zhi Huang365381f2018-04-13 16:44:34 -0700244 RTC_DISALLOW_COPY_AND_ASSIGN(JsepTransport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800245};
246
247} // namespace cricket
248
Zhi Huang365381f2018-04-13 16:44:34 -0700249#endif // PC_JSEPTRANSPORT_H_