xref: /aosp_15_r20/external/webrtc/pc/webrtc_session_description_factory.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 <functional>
17 #include <memory>
18 #include <queue>
19 #include <string>
20 
21 #include "absl/functional/any_invocable.h"
22 #include "api/jsep.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"
34 
35 namespace webrtc {
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 {
42  public:
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,
51       bool dtls_enabled,
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>&)>
55           on_certificate_ready,
56       const FieldTrialsView& field_trials);
57   ~WebRtcSessionDescriptionFactory();
58 
59   WebRtcSessionDescriptionFactory(const WebRtcSessionDescriptionFactory&) =
60       delete;
61   WebRtcSessionDescriptionFactory& operator=(
62       const WebRtcSessionDescriptionFactory&) = delete;
63 
64   static void CopyCandidatesFromSessionDescription(
65       const SessionDescriptionInterface* source_desc,
66       const std::string& content_name,
67       SessionDescriptionInterface* dest_desc);
68 
69   void CreateOffer(
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);
75 
76   void SetSdesPolicy(cricket::SecurePolicy secure_policy);
77   cricket::SecurePolicy SdesPolicy() const;
78 
set_enable_encrypted_rtp_header_extensions(bool enable)79   void set_enable_encrypted_rtp_header_extensions(bool enable) {
80     session_desc_factory_.set_enable_encrypted_rtp_header_extensions(enable);
81   }
82 
set_is_unified_plan(bool is_unified_plan)83   void set_is_unified_plan(bool is_unified_plan) {
84     session_desc_factory_.set_is_unified_plan(is_unified_plan);
85   }
86 
87   // For testing.
waiting_for_certificate_for_testing()88   bool waiting_for_certificate_for_testing() const {
89     return certificate_request_state_ == CERTIFICATE_WAITING;
90   }
91 
92  private:
93   enum CertificateRequestState {
94     CERTIFICATE_NOT_NEEDED,
95     CERTIFICATE_WAITING,
96     CERTIFICATE_SUCCEEDED,
97     CERTIFICATE_FAILED,
98   };
99 
100   struct CreateSessionDescriptionRequest {
101     enum Type {
102       kOffer,
103       kAnswer,
104     };
105 
CreateSessionDescriptionRequestCreateSessionDescriptionRequest106     CreateSessionDescriptionRequest(Type type,
107                                     CreateSessionDescriptionObserver* observer,
108                                     const cricket::MediaSessionOptions& options)
109         : type(type), observer(observer), options(options) {}
110 
111     Type type;
112     rtc::scoped_refptr<CreateSessionDescriptionObserver> observer;
113     cricket::MediaSessionOptions options;
114   };
115 
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,
122       const std::string& error);
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);
129 
130   void OnCertificateRequestFailed();
131   void SetCertificate(rtc::scoped_refptr<rtc::RTCCertificate> certificate);
132 
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_;
144 
145   std::function<void(const rtc::scoped_refptr<rtc::RTCCertificate>&)>
146       on_certificate_ready_;
147   rtc::WeakPtrFactory<WebRtcSessionDescriptionFactory> weak_factory_{this};
148 };
149 }  // namespace webrtc
150 
151 #endif  // PC_WEBRTC_SESSION_DESCRIPTION_FACTORY_H_
152