blob: 290ac4a2db77821573d80fe2e696702ffce1b648 [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
19#include "api/candidate.h"
20#include "api/jsep.h"
21#include "api/optional.h"
22#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
128 // Returns role if negotiated, or empty Optional if it hasn't been negotiated
129 // yet.
130 rtc::Optional<rtc::SSLRole> GetDtlsRole() const;
131
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
176 private:
177 bool SetRtcpMux(bool enable, webrtc::SdpType type, ContentSource source);
178
179 void ActivateRtcpMux();
180
181 bool SetSdes(const std::vector<CryptoParams>& cryptos,
182 const std::vector<int>& encrypted_extension_ids,
183 webrtc::SdpType type,
184 ContentSource source);
185
186 // Negotiates and sets the DTLS parameters based on the current local and
187 // remote transport description, such as the DTLS role to use, and whether
188 // DTLS should be activated.
189 //
190 // Called when an answer TransportDescription is applied.
191 webrtc::RTCError NegotiateAndSetDtlsParameters(
192 webrtc::SdpType local_description_type);
193
194 // Negotiates the DTLS role based off the offer and answer as specified by
195 // RFC 4145, section-4.1. Returns an RTCError if role cannot be determined
196 // from the local description and remote description.
197 webrtc::RTCError NegotiateDtlsRole(
198 webrtc::SdpType local_description_type,
199 ConnectionRole local_connection_role,
200 ConnectionRole remote_connection_role,
201 rtc::Optional<rtc::SSLRole>* negotiated_dtls_role);
202
203 // Pushes down the ICE parameters from the local description, such
204 // as the ICE ufrag and pwd.
205 void SetLocalIceParameters(IceTransportInternal* ice);
206
207 // Pushes down the ICE parameters from the remote description.
208 void SetRemoteIceParameters(IceTransportInternal* ice);
209
210 // Pushes down the DTLS parameters obtained via negotiation.
211 webrtc::RTCError SetNegotiatedDtlsParameters(
212 DtlsTransportInternal* dtls_transport,
213 rtc::Optional<rtc::SSLRole> dtls_role,
214 rtc::SSLFingerprint* remote_fingerprint);
215
216 bool GetTransportStats(DtlsTransportInternal* dtls_transport,
217 TransportStats* stats);
218
219 const std::string mid_;
220 // needs-ice-restart bit as described in JSEP.
221 bool needs_ice_restart_ = false;
222 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_;
223 std::unique_ptr<JsepTransportDescription> local_description_;
224 std::unique_ptr<JsepTransportDescription> remote_description_;
225
226 // To avoid downcasting and make it type safe, keep three unique pointers for
227 // different SRTP mode and only one of these is non-nullptr.
228 std::unique_ptr<webrtc::RtpTransport> unencrypted_rtp_transport_;
229 std::unique_ptr<webrtc::SrtpTransport> sdes_transport_;
230 std::unique_ptr<webrtc::DtlsSrtpTransport> dtls_srtp_transport_;
231
232 std::unique_ptr<DtlsTransportInternal> rtp_dtls_transport_;
233 std::unique_ptr<DtlsTransportInternal> rtcp_dtls_transport_;
234
235 SrtpFilter sdes_negotiator_;
236 RtcpMuxFilter rtcp_mux_negotiator_;
237
238 // Cache the encrypted header extension IDs for SDES negoitation.
239 rtc::Optional<std::vector<int>> send_extension_ids_;
240 rtc::Optional<std::vector<int>> recv_extension_ids_;
241
Zhi Huang365381f2018-04-13 16:44:34 -0700242 RTC_DISALLOW_COPY_AND_ASSIGN(JsepTransport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800243};
244
245} // namespace cricket
246
Zhi Huang365381f2018-04-13 16:44:34 -0700247#endif // PC_JSEPTRANSPORT_H_