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