webrtc_session_description_factory.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #ifndef PC_WEBRTC_SESSION_DESCRIPTION_FACTORY_H_
  11. #define PC_WEBRTC_SESSION_DESCRIPTION_FACTORY_H_
  12. #include <stdint.h>
  13. #include <memory>
  14. #include <queue>
  15. #include <string>
  16. #include "api/jsep.h"
  17. #include "api/peer_connection_interface.h"
  18. #include "api/scoped_refptr.h"
  19. #include "p2p/base/transport_description.h"
  20. #include "p2p/base/transport_description_factory.h"
  21. #include "pc/media_session.h"
  22. #include "pc/peer_connection_internal.h"
  23. #include "rtc_base/constructor_magic.h"
  24. #include "rtc_base/message_handler.h"
  25. #include "rtc_base/rtc_certificate.h"
  26. #include "rtc_base/rtc_certificate_generator.h"
  27. #include "rtc_base/third_party/sigslot/sigslot.h"
  28. #include "rtc_base/thread.h"
  29. #include "rtc_base/unique_id_generator.h"
  30. namespace webrtc {
  31. // DTLS certificate request callback class.
  32. class WebRtcCertificateGeneratorCallback
  33. : public rtc::RTCCertificateGeneratorCallback,
  34. public sigslot::has_slots<> {
  35. public:
  36. // |rtc::RTCCertificateGeneratorCallback| overrides.
  37. void OnSuccess(
  38. const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override;
  39. void OnFailure() override;
  40. sigslot::signal0<> SignalRequestFailed;
  41. sigslot::signal1<const rtc::scoped_refptr<rtc::RTCCertificate>&>
  42. SignalCertificateReady;
  43. };
  44. struct CreateSessionDescriptionRequest {
  45. enum Type {
  46. kOffer,
  47. kAnswer,
  48. };
  49. CreateSessionDescriptionRequest(Type type,
  50. CreateSessionDescriptionObserver* observer,
  51. const cricket::MediaSessionOptions& options)
  52. : type(type), observer(observer), options(options) {}
  53. Type type;
  54. rtc::scoped_refptr<CreateSessionDescriptionObserver> observer;
  55. cricket::MediaSessionOptions options;
  56. };
  57. // This class is used to create offer/answer session description. Certificates
  58. // for WebRtcSession/DTLS are either supplied at construction or generated
  59. // asynchronously. It queues the create offer/answer request until the
  60. // certificate generation has completed, i.e. when OnCertificateRequestFailed or
  61. // OnCertificateReady is called.
  62. class WebRtcSessionDescriptionFactory : public rtc::MessageHandler,
  63. public sigslot::has_slots<> {
  64. public:
  65. // Can specify either a |cert_generator| or |certificate| to enable DTLS. If
  66. // a certificate generator is given, starts generating the certificate
  67. // asynchronously. If a certificate is given, will use that for identifying
  68. // over DTLS. If neither is specified, DTLS is disabled.
  69. WebRtcSessionDescriptionFactory(
  70. rtc::Thread* signaling_thread,
  71. cricket::ChannelManager* channel_manager,
  72. PeerConnectionInternal* pc,
  73. const std::string& session_id,
  74. std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
  75. const rtc::scoped_refptr<rtc::RTCCertificate>& certificate,
  76. rtc::UniqueRandomIdGenerator* ssrc_generator);
  77. virtual ~WebRtcSessionDescriptionFactory();
  78. static void CopyCandidatesFromSessionDescription(
  79. const SessionDescriptionInterface* source_desc,
  80. const std::string& content_name,
  81. SessionDescriptionInterface* dest_desc);
  82. void CreateOffer(
  83. CreateSessionDescriptionObserver* observer,
  84. const PeerConnectionInterface::RTCOfferAnswerOptions& options,
  85. const cricket::MediaSessionOptions& session_options);
  86. void CreateAnswer(CreateSessionDescriptionObserver* observer,
  87. const cricket::MediaSessionOptions& session_options);
  88. void SetSdesPolicy(cricket::SecurePolicy secure_policy);
  89. cricket::SecurePolicy SdesPolicy() const;
  90. void set_enable_encrypted_rtp_header_extensions(bool enable) {
  91. session_desc_factory_.set_enable_encrypted_rtp_header_extensions(enable);
  92. }
  93. void set_is_unified_plan(bool is_unified_plan) {
  94. session_desc_factory_.set_is_unified_plan(is_unified_plan);
  95. }
  96. sigslot::signal1<const rtc::scoped_refptr<rtc::RTCCertificate>&>
  97. SignalCertificateReady;
  98. // For testing.
  99. bool waiting_for_certificate_for_testing() const {
  100. return certificate_request_state_ == CERTIFICATE_WAITING;
  101. }
  102. private:
  103. enum CertificateRequestState {
  104. CERTIFICATE_NOT_NEEDED,
  105. CERTIFICATE_WAITING,
  106. CERTIFICATE_SUCCEEDED,
  107. CERTIFICATE_FAILED,
  108. };
  109. // MessageHandler implementation.
  110. virtual void OnMessage(rtc::Message* msg);
  111. void InternalCreateOffer(CreateSessionDescriptionRequest request);
  112. void InternalCreateAnswer(CreateSessionDescriptionRequest request);
  113. // Posts failure notifications for all pending session description requests.
  114. void FailPendingRequests(const std::string& reason);
  115. void PostCreateSessionDescriptionFailed(
  116. CreateSessionDescriptionObserver* observer,
  117. const std::string& error);
  118. void PostCreateSessionDescriptionSucceeded(
  119. CreateSessionDescriptionObserver* observer,
  120. std::unique_ptr<SessionDescriptionInterface> description);
  121. void OnCertificateRequestFailed();
  122. void SetCertificate(
  123. const rtc::scoped_refptr<rtc::RTCCertificate>& certificate);
  124. std::queue<CreateSessionDescriptionRequest>
  125. create_session_description_requests_;
  126. rtc::Thread* const signaling_thread_;
  127. cricket::TransportDescriptionFactory transport_desc_factory_;
  128. cricket::MediaSessionDescriptionFactory session_desc_factory_;
  129. uint64_t session_version_;
  130. const std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator_;
  131. // TODO(jiayl): remove the dependency on peer connection once bug 2264 is
  132. // fixed.
  133. PeerConnectionInternal* const pc_;
  134. const std::string session_id_;
  135. CertificateRequestState certificate_request_state_;
  136. RTC_DISALLOW_COPY_AND_ASSIGN(WebRtcSessionDescriptionFactory);
  137. };
  138. } // namespace webrtc
  139. #endif // PC_WEBRTC_SESSION_DESCRIPTION_FACTORY_H_