wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 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. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef PC_WEBRTC_SESSION_DESCRIPTION_FACTORY_H_ |
| 12 | #define PC_WEBRTC_SESSION_DESCRIPTION_FACTORY_H_ |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 14 | #include <stdint.h> |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 15 | |
| 16 | #include <functional> |
jbauch | 555604a | 2016-04-26 10:13:22 | [diff] [blame] | 17 | #include <memory> |
Steve Anton | 36b29d1 | 2017-10-30 16:57:42 | [diff] [blame] | 18 | #include <queue> |
| 19 | #include <string> |
jbauch | 555604a | 2016-04-26 10:13:22 | [diff] [blame] | 20 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 21 | #include "api/jsep.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 22 | #include "api/peer_connection_interface.h" |
Mirko Bonadei | d970807 | 2019-01-25 19:26:48 | [diff] [blame] | 23 | #include "api/scoped_refptr.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 24 | #include "p2p/base/transport_description.h" |
| 25 | #include "p2p/base/transport_description_factory.h" |
| 26 | #include "pc/media_session.h" |
Harald Alvestrand | f01bd6c | 2020-10-23 13:30:46 | [diff] [blame] | 27 | #include "pc/sdp_state_provider.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 28 | #include "rtc_base/message_handler.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 29 | #include "rtc_base/rtc_certificate.h" |
| 30 | #include "rtc_base/rtc_certificate_generator.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 31 | #include "rtc_base/third_party/sigslot/sigslot.h" |
| 32 | #include "rtc_base/thread.h" |
Harald Alvestrand | d89ce53 | 2020-10-21 08:59:22 | [diff] [blame] | 33 | #include "rtc_base/thread_message.h" |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 34 | #include "rtc_base/unique_id_generator.h" |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 35 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 36 | namespace webrtc { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 37 | |
Henrik Boström | d03c23b | 2016-06-01 09:44:18 | [diff] [blame] | 38 | // DTLS certificate request callback class. |
| 39 | class WebRtcCertificateGeneratorCallback |
Harald Alvestrand | 5aaec9d | 2020-11-09 20:30:19 | [diff] [blame] | 40 | : public rtc::RTCCertificateGeneratorCallback { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 41 | public: |
Artem Titov | cfea218 | 2021-08-09 23:22:31 | [diff] [blame] | 42 | // `rtc::RTCCertificateGeneratorCallback` overrides. |
Henrik Boström | d03c23b | 2016-06-01 09:44:18 | [diff] [blame] | 43 | void OnSuccess( |
| 44 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override; |
| 45 | void OnFailure() override; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 46 | |
Henrik Boström | d03c23b | 2016-06-01 09:44:18 | [diff] [blame] | 47 | sigslot::signal0<> SignalRequestFailed; |
Henrik Boström | d828198 | 2015-08-27 08:12:24 | [diff] [blame] | 48 | sigslot::signal1<const rtc::scoped_refptr<rtc::RTCCertificate>&> |
| 49 | SignalCertificateReady; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | struct CreateSessionDescriptionRequest { |
| 53 | enum Type { |
| 54 | kOffer, |
| 55 | kAnswer, |
| 56 | }; |
| 57 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 58 | CreateSessionDescriptionRequest(Type type, |
| 59 | CreateSessionDescriptionObserver* observer, |
| 60 | const cricket::MediaSessionOptions& options) |
| 61 | : type(type), observer(observer), options(options) {} |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 62 | |
| 63 | Type type; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 64 | rtc::scoped_refptr<CreateSessionDescriptionObserver> observer; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 65 | cricket::MediaSessionOptions options; |
| 66 | }; |
| 67 | |
Henrik Boström | d03c23b | 2016-06-01 09:44:18 | [diff] [blame] | 68 | // This class is used to create offer/answer session description. Certificates |
| 69 | // for WebRtcSession/DTLS are either supplied at construction or generated |
| 70 | // asynchronously. It queues the create offer/answer request until the |
| 71 | // certificate generation has completed, i.e. when OnCertificateRequestFailed or |
| 72 | // OnCertificateReady is called. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 73 | class WebRtcSessionDescriptionFactory : public rtc::MessageHandler, |
Henrik Boström | 5e56c59 | 2015-08-11 08:33:13 | [diff] [blame] | 74 | public sigslot::has_slots<> { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 75 | public: |
Artem Titov | 880fa81 | 2021-07-30 20:30:23 | [diff] [blame] | 76 | // Can specify either a `cert_generator` or `certificate` to enable DTLS. If |
Steve Anton | d5585ca | 2017-10-23 21:49:26 | [diff] [blame] | 77 | // a certificate generator is given, starts generating the certificate |
| 78 | // asynchronously. If a certificate is given, will use that for identifying |
| 79 | // over DTLS. If neither is specified, DTLS is disabled. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 80 | WebRtcSessionDescriptionFactory( |
Jonas Oreland | ed99dae | 2022-03-09 08:28:10 | [diff] [blame] | 81 | ConnectionContext* context, |
Harald Alvestrand | f01bd6c | 2020-10-23 13:30:46 | [diff] [blame] | 82 | const SdpStateProvider* sdp_info, |
Henrik Boström | d03c23b | 2016-06-01 09:44:18 | [diff] [blame] | 83 | const std::string& session_id, |
Harald Alvestrand | d89ce53 | 2020-10-21 08:59:22 | [diff] [blame] | 84 | bool dtls_enabled, |
Steve Anton | d5585ca | 2017-10-23 21:49:26 | [diff] [blame] | 85 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 86 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate, |
Harald Alvestrand | a094787 | 2020-11-09 14:15:00 | [diff] [blame] | 87 | std::function<void(const rtc::scoped_refptr<rtc::RTCCertificate>&)> |
Jonas Oreland | 6c7f984 | 2022-04-19 15:24:10 | [diff] [blame] | 88 | on_certificate_ready, |
| 89 | const FieldTrialsView& field_trials); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 90 | virtual ~WebRtcSessionDescriptionFactory(); |
| 91 | |
Byoungchan Lee | c065e73 | 2022-01-18 00:35:48 | [diff] [blame] | 92 | WebRtcSessionDescriptionFactory(const WebRtcSessionDescriptionFactory&) = |
| 93 | delete; |
| 94 | WebRtcSessionDescriptionFactory& operator=( |
| 95 | const WebRtcSessionDescriptionFactory&) = delete; |
| 96 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 97 | static void CopyCandidatesFromSessionDescription( |
deadbeef | 0ed85b2 | 2016-02-24 01:24:52 | [diff] [blame] | 98 | const SessionDescriptionInterface* source_desc, |
| 99 | const std::string& content_name, |
| 100 | SessionDescriptionInterface* dest_desc); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 101 | |
| 102 | void CreateOffer( |
| 103 | CreateSessionDescriptionObserver* observer, |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 104 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 105 | const cricket::MediaSessionOptions& session_options); |
| 106 | void CreateAnswer(CreateSessionDescriptionObserver* observer, |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 107 | const cricket::MediaSessionOptions& session_options); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 108 | |
Harald Alvestrand | 0d01841 | 2021-11-04 13:52:31 | [diff] [blame] | 109 | void SetSdesPolicy(cricket::SecurePolicy secure_policy); |
| 110 | cricket::SecurePolicy SdesPolicy() const; |
| 111 | |
jbauch | 5869f50 | 2017-06-29 19:31:36 | [diff] [blame] | 112 | void set_enable_encrypted_rtp_header_extensions(bool enable) { |
| 113 | session_desc_factory_.set_enable_encrypted_rtp_header_extensions(enable); |
| 114 | } |
| 115 | |
Steve Anton | 8f66ddb | 2018-12-11 00:08:05 | [diff] [blame] | 116 | void set_is_unified_plan(bool is_unified_plan) { |
| 117 | session_desc_factory_.set_is_unified_plan(is_unified_plan); |
| 118 | } |
| 119 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 120 | // For testing. |
Henrik Boström | 87713d0 | 2015-08-25 07:53:21 | [diff] [blame] | 121 | bool waiting_for_certificate_for_testing() const { |
| 122 | return certificate_request_state_ == CERTIFICATE_WAITING; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 | [diff] [blame] | 123 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 124 | |
| 125 | private: |
Henrik Boström | 87713d0 | 2015-08-25 07:53:21 | [diff] [blame] | 126 | enum CertificateRequestState { |
| 127 | CERTIFICATE_NOT_NEEDED, |
| 128 | CERTIFICATE_WAITING, |
| 129 | CERTIFICATE_SUCCEEDED, |
| 130 | CERTIFICATE_FAILED, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | // MessageHandler implementation. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 | [diff] [blame] | 134 | virtual void OnMessage(rtc::Message* msg); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 135 | |
| 136 | void InternalCreateOffer(CreateSessionDescriptionRequest request); |
| 137 | void InternalCreateAnswer(CreateSessionDescriptionRequest request); |
tommi | 0f620f4 | 2015-07-09 10:25:02 | [diff] [blame] | 138 | // Posts failure notifications for all pending session description requests. |
| 139 | void FailPendingRequests(const std::string& reason); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 140 | void PostCreateSessionDescriptionFailed( |
| 141 | CreateSessionDescriptionObserver* observer, |
| 142 | const std::string& error); |
| 143 | void PostCreateSessionDescriptionSucceeded( |
| 144 | CreateSessionDescriptionObserver* observer, |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 145 | std::unique_ptr<SessionDescriptionInterface> description); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 146 | |
Henrik Boström | d03c23b | 2016-06-01 09:44:18 | [diff] [blame] | 147 | void OnCertificateRequestFailed(); |
Henrik Boström | d828198 | 2015-08-27 08:12:24 | [diff] [blame] | 148 | void SetCertificate( |
| 149 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 150 | |
| 151 | std::queue<CreateSessionDescriptionRequest> |
| 152 | create_session_description_requests_; |
tommi | 0f620f4 | 2015-07-09 10:25:02 | [diff] [blame] | 153 | rtc::Thread* const signaling_thread_; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 154 | cricket::TransportDescriptionFactory transport_desc_factory_; |
| 155 | cricket::MediaSessionDescriptionFactory session_desc_factory_; |
Peter Boström | 0c4e06b | 2015-10-07 10:23:21 | [diff] [blame] | 156 | uint64_t session_version_; |
Henrik Boström | d03c23b | 2016-06-01 09:44:18 | [diff] [blame] | 157 | const std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator_; |
Harald Alvestrand | f01bd6c | 2020-10-23 13:30:46 | [diff] [blame] | 158 | const SdpStateProvider* sdp_info_; |
tommi | 0f620f4 | 2015-07-09 10:25:02 | [diff] [blame] | 159 | const std::string session_id_; |
Henrik Boström | 87713d0 | 2015-08-25 07:53:21 | [diff] [blame] | 160 | CertificateRequestState certificate_request_state_; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 161 | |
Harald Alvestrand | a094787 | 2020-11-09 14:15:00 | [diff] [blame] | 162 | std::function<void(const rtc::scoped_refptr<rtc::RTCCertificate>&)> |
| 163 | on_certificate_ready_; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 164 | }; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 | [diff] [blame] | 165 | } // namespace webrtc |
| 166 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 167 | #endif // PC_WEBRTC_SESSION_DESCRIPTION_FACTORY_H_ |