1 /* 2 * Copyright 2013 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_WEBRTC_SESSION_DESCRIPTION_FACTORY_H_ 12 #define PC_WEBRTC_SESSION_DESCRIPTION_FACTORY_H_ 13 14 #include <stdint.h> 15 16 #include <memory> 17 #include <queue> 18 #include <string> 19 20 #include "api/jsep.h" 21 #include "api/peer_connection_interface.h" 22 #include "api/scoped_refptr.h" 23 #include "p2p/base/transport_description.h" 24 #include "p2p/base/transport_description_factory.h" 25 #include "pc/media_session.h" 26 #include "pc/peer_connection_internal.h" 27 #include "rtc_base/constructor_magic.h" 28 #include "rtc_base/message_handler.h" 29 #include "rtc_base/rtc_certificate.h" 30 #include "rtc_base/rtc_certificate_generator.h" 31 #include "rtc_base/third_party/sigslot/sigslot.h" 32 #include "rtc_base/thread.h" 33 #include "rtc_base/unique_id_generator.h" 34 35 namespace webrtc { 36 37 // DTLS certificate request callback class. 38 class WebRtcCertificateGeneratorCallback 39 : public rtc::RTCCertificateGeneratorCallback, 40 public sigslot::has_slots<> { 41 public: 42 // |rtc::RTCCertificateGeneratorCallback| overrides. 43 void OnSuccess( 44 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override; 45 void OnFailure() override; 46 47 sigslot::signal0<> SignalRequestFailed; 48 sigslot::signal1<const rtc::scoped_refptr<rtc::RTCCertificate>&> 49 SignalCertificateReady; 50 }; 51 52 struct CreateSessionDescriptionRequest { 53 enum Type { 54 kOffer, 55 kAnswer, 56 }; 57 CreateSessionDescriptionRequestCreateSessionDescriptionRequest58 CreateSessionDescriptionRequest(Type type, 59 CreateSessionDescriptionObserver* observer, 60 const cricket::MediaSessionOptions& options) 61 : type(type), observer(observer), options(options) {} 62 63 Type type; 64 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer; 65 cricket::MediaSessionOptions options; 66 }; 67 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. 73 class WebRtcSessionDescriptionFactory : public rtc::MessageHandler, 74 public sigslot::has_slots<> { 75 public: 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. 80 WebRtcSessionDescriptionFactory( 81 rtc::Thread* signaling_thread, 82 cricket::ChannelManager* channel_manager, 83 PeerConnectionInternal* pc, 84 const std::string& session_id, 85 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, 86 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate, 87 rtc::UniqueRandomIdGenerator* ssrc_generator); 88 virtual ~WebRtcSessionDescriptionFactory(); 89 90 static void CopyCandidatesFromSessionDescription( 91 const SessionDescriptionInterface* source_desc, 92 const std::string& content_name, 93 SessionDescriptionInterface* dest_desc); 94 95 void CreateOffer( 96 CreateSessionDescriptionObserver* observer, 97 const PeerConnectionInterface::RTCOfferAnswerOptions& options, 98 const cricket::MediaSessionOptions& session_options); 99 void CreateAnswer(CreateSessionDescriptionObserver* observer, 100 const cricket::MediaSessionOptions& session_options); 101 102 void SetSdesPolicy(cricket::SecurePolicy secure_policy); 103 cricket::SecurePolicy SdesPolicy() const; 104 set_enable_encrypted_rtp_header_extensions(bool enable)105 void set_enable_encrypted_rtp_header_extensions(bool enable) { 106 session_desc_factory_.set_enable_encrypted_rtp_header_extensions(enable); 107 } 108 set_is_unified_plan(bool is_unified_plan)109 void set_is_unified_plan(bool is_unified_plan) { 110 session_desc_factory_.set_is_unified_plan(is_unified_plan); 111 } 112 113 sigslot::signal1<const rtc::scoped_refptr<rtc::RTCCertificate>&> 114 SignalCertificateReady; 115 116 // For testing. waiting_for_certificate_for_testing()117 bool waiting_for_certificate_for_testing() const { 118 return certificate_request_state_ == CERTIFICATE_WAITING; 119 } 120 121 private: 122 enum CertificateRequestState { 123 CERTIFICATE_NOT_NEEDED, 124 CERTIFICATE_WAITING, 125 CERTIFICATE_SUCCEEDED, 126 CERTIFICATE_FAILED, 127 }; 128 129 // MessageHandler implementation. 130 virtual void OnMessage(rtc::Message* msg); 131 132 void InternalCreateOffer(CreateSessionDescriptionRequest request); 133 void InternalCreateAnswer(CreateSessionDescriptionRequest request); 134 // Posts failure notifications for all pending session description requests. 135 void FailPendingRequests(const std::string& reason); 136 void PostCreateSessionDescriptionFailed( 137 CreateSessionDescriptionObserver* observer, 138 const std::string& error); 139 void PostCreateSessionDescriptionSucceeded( 140 CreateSessionDescriptionObserver* observer, 141 std::unique_ptr<SessionDescriptionInterface> description); 142 143 void OnCertificateRequestFailed(); 144 void SetCertificate( 145 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate); 146 147 std::queue<CreateSessionDescriptionRequest> 148 create_session_description_requests_; 149 rtc::Thread* const signaling_thread_; 150 cricket::TransportDescriptionFactory transport_desc_factory_; 151 cricket::MediaSessionDescriptionFactory session_desc_factory_; 152 uint64_t session_version_; 153 const std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator_; 154 // TODO(jiayl): remove the dependency on peer connection once bug 2264 is 155 // fixed. 156 PeerConnectionInternal* const pc_; 157 const std::string session_id_; 158 CertificateRequestState certificate_request_state_; 159 160 RTC_DISALLOW_COPY_AND_ASSIGN(WebRtcSessionDescriptionFactory); 161 }; 162 } // namespace webrtc 163 164 #endif // PC_WEBRTC_SESSION_DESCRIPTION_FACTORY_H_ 165