wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [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 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [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 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <stdint.h> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 15 | #include <memory> |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 16 | #include <queue> |
| 17 | #include <string> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 18 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 19 | #include "api/jsep.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "api/peer_connection_interface.h" |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 21 | #include "api/scoped_refptr.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "p2p/base/transport_description.h" |
| 23 | #include "p2p/base/transport_description_factory.h" |
| 24 | #include "pc/media_session.h" |
| 25 | #include "pc/peer_connection_internal.h" |
| 26 | #include "rtc_base/constructor_magic.h" |
| 27 | #include "rtc_base/message_handler.h" |
| 28 | #include "rtc_base/message_queue.h" |
| 29 | #include "rtc_base/rtc_certificate.h" |
| 30 | #include "rtc_base/rtc_certificate_generator.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 31 | #include "rtc_base/third_party/sigslot/sigslot.h" |
| 32 | #include "rtc_base/thread.h" |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame^] | 33 | #include "rtc_base/unique_id_generator.h" |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 34 | |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 35 | namespace webrtc { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 36 | |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 37 | // DTLS certificate request callback class. |
| 38 | class WebRtcCertificateGeneratorCallback |
| 39 | : public rtc::RTCCertificateGeneratorCallback, |
| 40 | public sigslot::has_slots<> { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 41 | public: |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 42 | // |rtc::RTCCertificateGeneratorCallback| overrides. |
| 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 +0000 | [diff] [blame] | 46 | |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 47 | sigslot::signal0<> SignalRequestFailed; |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 48 | sigslot::signal1<const rtc::scoped_refptr<rtc::RTCCertificate>&> |
| 49 | SignalCertificateReady; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | struct CreateSessionDescriptionRequest { |
| 53 | enum Type { |
| 54 | kOffer, |
| 55 | kAnswer, |
| 56 | }; |
| 57 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [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 +0000 | [diff] [blame] | 62 | |
| 63 | Type type; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 64 | rtc::scoped_refptr<CreateSessionDescriptionObserver> observer; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 65 | cricket::MediaSessionOptions options; |
| 66 | }; |
| 67 | |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [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 +0000 | [diff] [blame] | 73 | class WebRtcSessionDescriptionFactory : public rtc::MessageHandler, |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 74 | public sigslot::has_slots<> { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 75 | public: |
Steve Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 76 | // Can specify either a |cert_generator| or |certificate| to enable DTLS. If |
| 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 +0000 | [diff] [blame] | 80 | WebRtcSessionDescriptionFactory( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 81 | rtc::Thread* signaling_thread, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 82 | cricket::ChannelManager* channel_manager, |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 83 | PeerConnectionInternal* pc, |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 84 | const std::string& session_id, |
Steve Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 85 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame^] | 86 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate, |
| 87 | rtc::UniqueRandomIdGenerator* ssrc_generator); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 88 | virtual ~WebRtcSessionDescriptionFactory(); |
| 89 | |
| 90 | static void CopyCandidatesFromSessionDescription( |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 91 | const SessionDescriptionInterface* source_desc, |
| 92 | const std::string& content_name, |
| 93 | SessionDescriptionInterface* dest_desc); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 94 | |
| 95 | void CreateOffer( |
| 96 | CreateSessionDescriptionObserver* observer, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 97 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 98 | const cricket::MediaSessionOptions& session_options); |
| 99 | void CreateAnswer(CreateSessionDescriptionObserver* observer, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 100 | const cricket::MediaSessionOptions& session_options); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 101 | |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 102 | void SetSdesPolicy(cricket::SecurePolicy secure_policy); |
| 103 | cricket::SecurePolicy SdesPolicy() const; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 104 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 105 | void set_enable_encrypted_rtp_header_extensions(bool enable) { |
| 106 | session_desc_factory_.set_enable_encrypted_rtp_header_extensions(enable); |
| 107 | } |
| 108 | |
Steve Anton | 8f66ddb | 2018-12-10 16:08:05 -0800 | [diff] [blame] | 109 | void set_is_unified_plan(bool is_unified_plan) { |
| 110 | session_desc_factory_.set_is_unified_plan(is_unified_plan); |
| 111 | } |
| 112 | |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 113 | sigslot::signal1<const rtc::scoped_refptr<rtc::RTCCertificate>&> |
| 114 | SignalCertificateReady; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 115 | |
| 116 | // For testing. |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 117 | bool waiting_for_certificate_for_testing() const { |
| 118 | return certificate_request_state_ == CERTIFICATE_WAITING; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 119 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 120 | |
| 121 | private: |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 122 | enum CertificateRequestState { |
| 123 | CERTIFICATE_NOT_NEEDED, |
| 124 | CERTIFICATE_WAITING, |
| 125 | CERTIFICATE_SUCCEEDED, |
| 126 | CERTIFICATE_FAILED, |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | // MessageHandler implementation. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 130 | virtual void OnMessage(rtc::Message* msg); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 131 | |
| 132 | void InternalCreateOffer(CreateSessionDescriptionRequest request); |
| 133 | void InternalCreateAnswer(CreateSessionDescriptionRequest request); |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame] | 134 | // Posts failure notifications for all pending session description requests. |
| 135 | void FailPendingRequests(const std::string& reason); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 136 | void PostCreateSessionDescriptionFailed( |
| 137 | CreateSessionDescriptionObserver* observer, |
| 138 | const std::string& error); |
| 139 | void PostCreateSessionDescriptionSucceeded( |
| 140 | CreateSessionDescriptionObserver* observer, |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 141 | std::unique_ptr<SessionDescriptionInterface> description); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 142 | |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 143 | void OnCertificateRequestFailed(); |
Henrik Boström | d828198 | 2015-08-27 10:12:24 +0200 | [diff] [blame] | 144 | void SetCertificate( |
| 145 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 146 | |
| 147 | std::queue<CreateSessionDescriptionRequest> |
| 148 | create_session_description_requests_; |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame] | 149 | rtc::Thread* const signaling_thread_; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 150 | cricket::TransportDescriptionFactory transport_desc_factory_; |
| 151 | cricket::MediaSessionDescriptionFactory session_desc_factory_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 152 | uint64_t session_version_; |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 153 | const std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator_; |
Steve Anton | d5585ca | 2017-10-23 14:49:26 -0700 | [diff] [blame] | 154 | // TODO(jiayl): remove the dependency on peer connection once bug 2264 is |
| 155 | // fixed. |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 156 | PeerConnectionInternal* const pc_; |
tommi | 0f620f4 | 2015-07-09 03:25:02 -0700 | [diff] [blame] | 157 | const std::string session_id_; |
Henrik Boström | 87713d0 | 2015-08-25 09:53:21 +0200 | [diff] [blame] | 158 | CertificateRequestState certificate_request_state_; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 159 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 160 | RTC_DISALLOW_COPY_AND_ASSIGN(WebRtcSessionDescriptionFactory); |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 161 | }; |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 162 | } // namespace webrtc |
| 163 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 164 | #endif // PC_WEBRTC_SESSION_DESCRIPTION_FACTORY_H_ |