jsep_transport.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  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. #ifndef PC_JSEP_TRANSPORT_H_
  11. #define PC_JSEP_TRANSPORT_H_
  12. #include <map>
  13. #include <memory>
  14. #include <string>
  15. #include <vector>
  16. #include "absl/types/optional.h"
  17. #include "api/candidate.h"
  18. #include "api/ice_transport_interface.h"
  19. #include "api/jsep.h"
  20. #include "api/transport/data_channel_transport_interface.h"
  21. #include "media/sctp/sctp_transport_internal.h"
  22. #include "p2p/base/dtls_transport.h"
  23. #include "p2p/base/p2p_constants.h"
  24. #include "p2p/base/transport_info.h"
  25. #include "pc/composite_rtp_transport.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/sctp_transport.h"
  31. #include "pc/session_description.h"
  32. #include "pc/srtp_filter.h"
  33. #include "pc/srtp_transport.h"
  34. #include "pc/transport_stats.h"
  35. #include "rtc_base/constructor_magic.h"
  36. #include "rtc_base/rtc_certificate.h"
  37. #include "rtc_base/ssl_stream_adapter.h"
  38. #include "rtc_base/third_party/sigslot/sigslot.h"
  39. #include "rtc_base/thread_checker.h"
  40. namespace cricket {
  41. class DtlsTransportInternal;
  42. struct 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. int rtp_abs_sendtime_extn_id,
  50. const TransportDescription& transport_description);
  51. JsepTransportDescription(const JsepTransportDescription& from);
  52. ~JsepTransportDescription();
  53. JsepTransportDescription& operator=(const JsepTransportDescription& from);
  54. bool rtcp_mux_enabled = true;
  55. std::vector<CryptoParams> cryptos;
  56. std::vector<int> encrypted_header_extension_ids;
  57. int rtp_abs_sendtime_extn_id = -1;
  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. // Helper class used by JsepTransportController that processes
  63. // TransportDescriptions. A TransportDescription represents the
  64. // transport-specific properties of an SDP m= section, processed according to
  65. // JSEP. Each transport consists of DTLS and ICE transport channels for RTP
  66. // (and possibly RTCP, if rtcp-mux isn't used).
  67. //
  68. // On Threading: JsepTransport performs work solely on the network thread, and
  69. // so its methods should only be called on the network thread.
  70. class JsepTransport : public sigslot::has_slots<> {
  71. public:
  72. // |mid| is just used for log statements in order to identify the Transport.
  73. // Note that |local_certificate| is allowed to be null since a remote
  74. // description may be set before a local certificate is generated.
  75. JsepTransport(
  76. const std::string& mid,
  77. const rtc::scoped_refptr<rtc::RTCCertificate>& local_certificate,
  78. rtc::scoped_refptr<webrtc::IceTransportInterface> ice_transport,
  79. rtc::scoped_refptr<webrtc::IceTransportInterface> rtcp_ice_transport,
  80. std::unique_ptr<webrtc::RtpTransport> unencrypted_rtp_transport,
  81. std::unique_ptr<webrtc::SrtpTransport> sdes_transport,
  82. std::unique_ptr<webrtc::DtlsSrtpTransport> dtls_srtp_transport,
  83. std::unique_ptr<webrtc::RtpTransportInternal> datagram_rtp_transport,
  84. std::unique_ptr<DtlsTransportInternal> rtp_dtls_transport,
  85. std::unique_ptr<DtlsTransportInternal> rtcp_dtls_transport,
  86. std::unique_ptr<SctpTransportInternal> sctp_transport);
  87. ~JsepTransport() override;
  88. // Returns the MID of this transport. This is only used for logging.
  89. const std::string& mid() const { return mid_; }
  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. RTC_DCHECK_RUN_ON(network_thread_);
  95. local_certificate_ = local_certificate;
  96. }
  97. // Return the local certificate provided by SetLocalCertificate.
  98. rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const {
  99. RTC_DCHECK_RUN_ON(network_thread_);
  100. return local_certificate_;
  101. }
  102. webrtc::RTCError SetLocalJsepTransportDescription(
  103. const JsepTransportDescription& jsep_description,
  104. webrtc::SdpType type) RTC_LOCKS_EXCLUDED(accessor_lock_);
  105. // Set the remote TransportDescription to be used by DTLS and ICE channels
  106. // that are part of this Transport.
  107. webrtc::RTCError SetRemoteJsepTransportDescription(
  108. const JsepTransportDescription& jsep_description,
  109. webrtc::SdpType type) RTC_LOCKS_EXCLUDED(accessor_lock_);
  110. webrtc::RTCError AddRemoteCandidates(const Candidates& candidates)
  111. RTC_LOCKS_EXCLUDED(accessor_lock_);
  112. // Set the "needs-ice-restart" flag as described in JSEP. After the flag is
  113. // set, offers should generate new ufrags/passwords until an ICE restart
  114. // occurs.
  115. //
  116. // This and the below method can be called safely from any thread as long as
  117. // SetXTransportDescription is not in progress.
  118. void SetNeedsIceRestartFlag() RTC_LOCKS_EXCLUDED(accessor_lock_);
  119. // Returns true if the ICE restart flag above was set, and no ICE restart has
  120. // occurred yet for this transport (by applying a local description with
  121. // changed ufrag/password).
  122. bool needs_ice_restart() const RTC_LOCKS_EXCLUDED(accessor_lock_) {
  123. rtc::CritScope scope(&accessor_lock_);
  124. return needs_ice_restart_;
  125. }
  126. // Returns role if negotiated, or empty absl::optional if it hasn't been
  127. // negotiated yet.
  128. absl::optional<rtc::SSLRole> GetDtlsRole() const
  129. RTC_LOCKS_EXCLUDED(accessor_lock_);
  130. // TODO(deadbeef): Make this const. See comment in transportcontroller.h.
  131. bool GetStats(TransportStats* stats) RTC_LOCKS_EXCLUDED(accessor_lock_);
  132. const JsepTransportDescription* local_description() const {
  133. RTC_DCHECK_RUN_ON(network_thread_);
  134. return local_description_.get();
  135. }
  136. const JsepTransportDescription* remote_description() const {
  137. RTC_DCHECK_RUN_ON(network_thread_);
  138. return remote_description_.get();
  139. }
  140. webrtc::RtpTransportInternal* rtp_transport() const
  141. RTC_LOCKS_EXCLUDED(accessor_lock_) {
  142. rtc::CritScope scope(&accessor_lock_);
  143. if (composite_rtp_transport_) {
  144. return composite_rtp_transport_.get();
  145. } else if (datagram_rtp_transport_) {
  146. return datagram_rtp_transport_.get();
  147. } else {
  148. return default_rtp_transport();
  149. }
  150. }
  151. const DtlsTransportInternal* rtp_dtls_transport() const
  152. RTC_LOCKS_EXCLUDED(accessor_lock_) {
  153. rtc::CritScope scope(&accessor_lock_);
  154. if (rtp_dtls_transport_) {
  155. return rtp_dtls_transport_->internal();
  156. } else {
  157. return nullptr;
  158. }
  159. }
  160. DtlsTransportInternal* rtp_dtls_transport()
  161. RTC_LOCKS_EXCLUDED(accessor_lock_) {
  162. rtc::CritScope scope(&accessor_lock_);
  163. return rtp_dtls_transport_locked();
  164. }
  165. const DtlsTransportInternal* rtcp_dtls_transport() const
  166. RTC_LOCKS_EXCLUDED(accessor_lock_) {
  167. rtc::CritScope scope(&accessor_lock_);
  168. if (rtcp_dtls_transport_) {
  169. return rtcp_dtls_transport_->internal();
  170. } else {
  171. return nullptr;
  172. }
  173. }
  174. DtlsTransportInternal* rtcp_dtls_transport()
  175. RTC_LOCKS_EXCLUDED(accessor_lock_) {
  176. rtc::CritScope scope(&accessor_lock_);
  177. if (rtcp_dtls_transport_) {
  178. return rtcp_dtls_transport_->internal();
  179. } else {
  180. return nullptr;
  181. }
  182. }
  183. rtc::scoped_refptr<webrtc::DtlsTransport> RtpDtlsTransport()
  184. RTC_LOCKS_EXCLUDED(accessor_lock_) {
  185. rtc::CritScope scope(&accessor_lock_);
  186. return rtp_dtls_transport_;
  187. }
  188. rtc::scoped_refptr<webrtc::SctpTransport> SctpTransport() const
  189. RTC_LOCKS_EXCLUDED(accessor_lock_) {
  190. rtc::CritScope scope(&accessor_lock_);
  191. return sctp_transport_;
  192. }
  193. // TODO(bugs.webrtc.org/9719): Delete method, update callers to use
  194. // SctpTransport() instead.
  195. webrtc::DataChannelTransportInterface* data_channel_transport() const
  196. RTC_LOCKS_EXCLUDED(accessor_lock_) {
  197. rtc::CritScope scope(&accessor_lock_);
  198. if (sctp_data_channel_transport_) {
  199. return sctp_data_channel_transport_.get();
  200. }
  201. return nullptr;
  202. }
  203. // This is signaled when RTCP-mux becomes active and
  204. // |rtcp_dtls_transport_| is destroyed. The JsepTransportController will
  205. // handle the signal and update the aggregate transport states.
  206. sigslot::signal<> SignalRtcpMuxActive;
  207. // TODO(deadbeef): The methods below are only public for testing. Should make
  208. // them utility functions or objects so they can be tested independently from
  209. // this class.
  210. // Returns an error if the certificate's identity does not match the
  211. // fingerprint, or either is NULL.
  212. webrtc::RTCError VerifyCertificateFingerprint(
  213. const rtc::RTCCertificate* certificate,
  214. const rtc::SSLFingerprint* fingerprint) const;
  215. void SetActiveResetSrtpParams(bool active_reset_srtp_params);
  216. private:
  217. DtlsTransportInternal* rtp_dtls_transport_locked()
  218. RTC_EXCLUSIVE_LOCKS_REQUIRED(accessor_lock_) {
  219. if (rtp_dtls_transport_) {
  220. return rtp_dtls_transport_->internal();
  221. } else {
  222. return nullptr;
  223. }
  224. }
  225. bool SetRtcpMux(bool enable, webrtc::SdpType type, ContentSource source);
  226. void ActivateRtcpMux();
  227. bool SetSdes(const std::vector<CryptoParams>& cryptos,
  228. const std::vector<int>& encrypted_extension_ids,
  229. webrtc::SdpType type,
  230. ContentSource source)
  231. RTC_EXCLUSIVE_LOCKS_REQUIRED(accessor_lock_);
  232. // Negotiates and sets the DTLS parameters based on the current local and
  233. // remote transport description, such as the DTLS role to use, and whether
  234. // DTLS should be activated.
  235. //
  236. // Called when an answer TransportDescription is applied.
  237. webrtc::RTCError NegotiateAndSetDtlsParameters(
  238. webrtc::SdpType local_description_type);
  239. // Negotiates the DTLS role based off the offer and answer as specified by
  240. // RFC 4145, section-4.1. Returns an RTCError if role cannot be determined
  241. // from the local description and remote description.
  242. webrtc::RTCError NegotiateDtlsRole(
  243. webrtc::SdpType local_description_type,
  244. ConnectionRole local_connection_role,
  245. ConnectionRole remote_connection_role,
  246. absl::optional<rtc::SSLRole>* negotiated_dtls_role)
  247. RTC_LOCKS_EXCLUDED(accessor_lock_);
  248. // Pushes down the ICE parameters from the remote description.
  249. void SetRemoteIceParameters(const IceParameters& ice_parameters,
  250. IceTransportInternal* ice);
  251. // Pushes down the DTLS parameters obtained via negotiation.
  252. static webrtc::RTCError SetNegotiatedDtlsParameters(
  253. DtlsTransportInternal* dtls_transport,
  254. absl::optional<rtc::SSLRole> dtls_role,
  255. rtc::SSLFingerprint* remote_fingerprint);
  256. bool GetTransportStats(DtlsTransportInternal* dtls_transport,
  257. TransportStats* stats)
  258. RTC_EXCLUSIVE_LOCKS_REQUIRED(accessor_lock_);
  259. // Returns the default (non-datagram) rtp transport, if any.
  260. webrtc::RtpTransportInternal* default_rtp_transport() const
  261. RTC_EXCLUSIVE_LOCKS_REQUIRED(accessor_lock_) {
  262. if (dtls_srtp_transport_) {
  263. return dtls_srtp_transport_.get();
  264. } else if (sdes_transport_) {
  265. return sdes_transport_.get();
  266. } else if (unencrypted_rtp_transport_) {
  267. return unencrypted_rtp_transport_.get();
  268. } else {
  269. return nullptr;
  270. }
  271. }
  272. // Owning thread, for safety checks
  273. const rtc::Thread* const network_thread_;
  274. // Critical scope for fields accessed off-thread
  275. // TODO(https://bugs.webrtc.org/10300): Stop doing this.
  276. rtc::RecursiveCriticalSection accessor_lock_;
  277. const std::string mid_;
  278. // needs-ice-restart bit as described in JSEP.
  279. bool needs_ice_restart_ RTC_GUARDED_BY(accessor_lock_) = false;
  280. rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_
  281. RTC_GUARDED_BY(network_thread_);
  282. std::unique_ptr<JsepTransportDescription> local_description_
  283. RTC_GUARDED_BY(network_thread_);
  284. std::unique_ptr<JsepTransportDescription> remote_description_
  285. RTC_GUARDED_BY(network_thread_);
  286. // Ice transport which may be used by any of upper-layer transports (below).
  287. // Owned by JsepTransport and guaranteed to outlive the transports below.
  288. const rtc::scoped_refptr<webrtc::IceTransportInterface> ice_transport_;
  289. const rtc::scoped_refptr<webrtc::IceTransportInterface> rtcp_ice_transport_;
  290. // To avoid downcasting and make it type safe, keep three unique pointers for
  291. // different SRTP mode and only one of these is non-nullptr.
  292. std::unique_ptr<webrtc::RtpTransport> unencrypted_rtp_transport_
  293. RTC_GUARDED_BY(accessor_lock_);
  294. std::unique_ptr<webrtc::SrtpTransport> sdes_transport_
  295. RTC_GUARDED_BY(accessor_lock_);
  296. std::unique_ptr<webrtc::DtlsSrtpTransport> dtls_srtp_transport_
  297. RTC_GUARDED_BY(accessor_lock_);
  298. // If multiple RTP transports are in use, |composite_rtp_transport_| will be
  299. // passed to callers. This is only valid for offer-only, receive-only
  300. // scenarios, as it is not possible for the composite to correctly choose
  301. // which transport to use for sending.
  302. std::unique_ptr<webrtc::CompositeRtpTransport> composite_rtp_transport_
  303. RTC_GUARDED_BY(accessor_lock_);
  304. rtc::scoped_refptr<webrtc::DtlsTransport> rtp_dtls_transport_
  305. RTC_GUARDED_BY(accessor_lock_);
  306. rtc::scoped_refptr<webrtc::DtlsTransport> rtcp_dtls_transport_
  307. RTC_GUARDED_BY(accessor_lock_);
  308. rtc::scoped_refptr<webrtc::DtlsTransport> datagram_dtls_transport_
  309. RTC_GUARDED_BY(accessor_lock_);
  310. std::unique_ptr<webrtc::DataChannelTransportInterface>
  311. sctp_data_channel_transport_ RTC_GUARDED_BY(accessor_lock_);
  312. rtc::scoped_refptr<webrtc::SctpTransport> sctp_transport_
  313. RTC_GUARDED_BY(accessor_lock_);
  314. SrtpFilter sdes_negotiator_ RTC_GUARDED_BY(network_thread_);
  315. RtcpMuxFilter rtcp_mux_negotiator_ RTC_GUARDED_BY(network_thread_);
  316. // Cache the encrypted header extension IDs for SDES negoitation.
  317. absl::optional<std::vector<int>> send_extension_ids_
  318. RTC_GUARDED_BY(network_thread_);
  319. absl::optional<std::vector<int>> recv_extension_ids_
  320. RTC_GUARDED_BY(network_thread_);
  321. std::unique_ptr<webrtc::RtpTransportInternal> datagram_rtp_transport_
  322. RTC_GUARDED_BY(accessor_lock_);
  323. RTC_DISALLOW_COPY_AND_ASSIGN(JsepTransport);
  324. };
  325. } // namespace cricket
  326. #endif // PC_JSEP_TRANSPORT_H_