2 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
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.
11 #ifndef PC_WEBRTC_SESSION_DESCRIPTION_FACTORY_H_
12 #define PC_WEBRTC_SESSION_DESCRIPTION_FACTORY_H_
21 #include "absl/functional/any_invocable.h"
23 #include "api/peer_connection_interface.h"
24 #include "api/scoped_refptr.h"
25 #include "api/task_queue/task_queue_base.h"
26 #include "p2p/base/transport_description.h"
27 #include "p2p/base/transport_description_factory.h"
28 #include "pc/media_session.h"
29 #include "pc/sdp_state_provider.h"
30 #include "rtc_base/rtc_certificate.h"
31 #include "rtc_base/rtc_certificate_generator.h"
32 #include "rtc_base/unique_id_generator.h"
33 #include "rtc_base/weak_ptr.h"
36 // This class is used to create offer/answer session description. Certificates
37 // for WebRtcSession/DTLS are either supplied at construction or generated
38 // asynchronously. It queues the create offer/answer request until the
39 // certificate generation has completed, i.e. when OnCertificateRequestFailed or
40 // OnCertificateReady is called.
41 class WebRtcSessionDescriptionFactory
{
43 // Can specify either a `cert_generator` or `certificate` to enable DTLS. If
44 // a certificate generator is given, starts generating the certificate
45 // asynchronously. If a certificate is given, will use that for identifying
46 // over DTLS. If neither is specified, DTLS is disabled.
47 WebRtcSessionDescriptionFactory(
48 ConnectionContext
* context
,
49 const SdpStateProvider
* sdp_info
,
50 const std::string
& session_id
,
52 std::unique_ptr
<rtc::RTCCertificateGeneratorInterface
> cert_generator
,
53 rtc::scoped_refptr
<rtc::RTCCertificate
> certificate
,
54 std::function
<void(const rtc::scoped_refptr
<rtc::RTCCertificate
>&)>
56 const FieldTrialsView
& field_trials
);
57 ~WebRtcSessionDescriptionFactory();
59 WebRtcSessionDescriptionFactory(const WebRtcSessionDescriptionFactory
&) =
61 WebRtcSessionDescriptionFactory
& operator=(
62 const WebRtcSessionDescriptionFactory
&) = delete;
64 static void CopyCandidatesFromSessionDescription(
65 const SessionDescriptionInterface
* source_desc
,
66 const std::string
& content_name
,
67 SessionDescriptionInterface
* dest_desc
);
70 CreateSessionDescriptionObserver
* observer
,
71 const PeerConnectionInterface::RTCOfferAnswerOptions
& options
,
72 const cricket::MediaSessionOptions
& session_options
);
73 void CreateAnswer(CreateSessionDescriptionObserver
* observer
,
74 const cricket::MediaSessionOptions
& session_options
);
76 void SetSdesPolicy(cricket::SecurePolicy secure_policy
);
77 cricket::SecurePolicy
SdesPolicy() const;
79 void set_enable_encrypted_rtp_header_extensions(bool enable
) {
80 session_desc_factory_
.set_enable_encrypted_rtp_header_extensions(enable
);
83 void set_is_unified_plan(bool is_unified_plan
) {
84 session_desc_factory_
.set_is_unified_plan(is_unified_plan
);
88 bool waiting_for_certificate_for_testing() const {
89 return certificate_request_state_
== CERTIFICATE_WAITING
;
93 enum CertificateRequestState
{
94 CERTIFICATE_NOT_NEEDED
,
96 CERTIFICATE_SUCCEEDED
,
100 struct CreateSessionDescriptionRequest
{
106 CreateSessionDescriptionRequest(Type type
,
107 CreateSessionDescriptionObserver
* observer
,
108 const cricket::MediaSessionOptions
& options
)
109 : type(type
), observer(observer
), options(options
) {}
112 rtc::scoped_refptr
<CreateSessionDescriptionObserver
> observer
;
113 cricket::MediaSessionOptions options
;
116 void InternalCreateOffer(CreateSessionDescriptionRequest request
);
117 void InternalCreateAnswer(CreateSessionDescriptionRequest request
);
118 // Posts failure notifications for all pending session description requests.
119 void FailPendingRequests(const std::string
& reason
);
120 void PostCreateSessionDescriptionFailed(
121 CreateSessionDescriptionObserver
* observer
,
123 void PostCreateSessionDescriptionSucceeded(
124 CreateSessionDescriptionObserver
* observer
,
125 std::unique_ptr
<SessionDescriptionInterface
> description
);
126 // Posts `callback` to `signaling_thread_`, and ensures it will be called no
127 // later than in the destructor.
128 void Post(absl::AnyInvocable
<void() &&> callback
);
130 void OnCertificateRequestFailed();
131 void SetCertificate(rtc::scoped_refptr
<rtc::RTCCertificate
> certificate
);
133 std::queue
<CreateSessionDescriptionRequest
>
134 create_session_description_requests_
;
135 TaskQueueBase
* const signaling_thread_
;
136 cricket::TransportDescriptionFactory transport_desc_factory_
;
137 cricket::MediaSessionDescriptionFactory session_desc_factory_
;
138 uint64_t session_version_
;
139 const std::unique_ptr
<rtc::RTCCertificateGeneratorInterface
> cert_generator_
;
140 const SdpStateProvider
* sdp_info_
;
141 const std::string session_id_
;
142 CertificateRequestState certificate_request_state_
;
143 std::queue
<absl::AnyInvocable
<void() &&>> callbacks_
;
145 std::function
<void(const rtc::scoped_refptr
<rtc::RTCCertificate
>&)>
146 on_certificate_ready_
;
147 rtc::WeakPtrFactory
<WebRtcSessionDescriptionFactory
> weak_factory_
{this};
149 } // namespace webrtc
151 #endif // PC_WEBRTC_SESSION_DESCRIPTION_FACTORY_H_