xref: /aosp_15_r20/external/webrtc/pc/peer_connection_factory.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 
2 /*
3  *  Copyright 2011 The WebRTC project authors. All Rights Reserved.
4  *
5  *  Use of this source code is governed by a BSD-style license
6  *  that can be found in the LICENSE file in the root of the source
7  *  tree. An additional intellectual property rights grant can be found
8  *  in the file PATENTS.  All contributing project authors may
9  *  be found in the AUTHORS file in the root of the source tree.
10  */
11 
12 #ifndef PC_PEER_CONNECTION_FACTORY_H_
13 #define PC_PEER_CONNECTION_FACTORY_H_
14 
15 #include <stdint.h>
16 #include <stdio.h>
17 
18 #include <memory>
19 #include <string>
20 
21 #include "absl/strings/string_view.h"
22 #include "api/audio_options.h"
23 #include "api/fec_controller.h"
24 #include "api/field_trials_view.h"
25 #include "api/media_stream_interface.h"
26 #include "api/media_types.h"
27 #include "api/metronome/metronome.h"
28 #include "api/neteq/neteq_factory.h"
29 #include "api/network_state_predictor.h"
30 #include "api/peer_connection_interface.h"
31 #include "api/rtc_error.h"
32 #include "api/rtc_event_log/rtc_event_log.h"
33 #include "api/rtc_event_log/rtc_event_log_factory_interface.h"
34 #include "api/rtp_parameters.h"
35 #include "api/scoped_refptr.h"
36 #include "api/sequence_checker.h"
37 #include "api/task_queue/task_queue_factory.h"
38 #include "api/transport/network_control.h"
39 #include "api/transport/sctp_transport_factory_interface.h"
40 #include "call/call.h"
41 #include "call/rtp_transport_controller_send_factory_interface.h"
42 #include "p2p/base/port_allocator.h"
43 #include "pc/connection_context.h"
44 #include "rtc_base/checks.h"
45 #include "rtc_base/rtc_certificate_generator.h"
46 #include "rtc_base/thread.h"
47 #include "rtc_base/thread_annotations.h"
48 
49 namespace rtc {
50 class BasicNetworkManager;
51 class BasicPacketSocketFactory;
52 }  // namespace rtc
53 
54 namespace webrtc {
55 
56 class RtcEventLog;
57 
58 class PeerConnectionFactory : public PeerConnectionFactoryInterface {
59  public:
60   // Creates a PeerConnectionFactory. It returns nullptr on initialization
61   // error.
62   //
63   // The Dependencies structure allows simple management of all new
64   // dependencies being added to the PeerConnectionFactory.
65   static rtc::scoped_refptr<PeerConnectionFactory> Create(
66       PeerConnectionFactoryDependencies dependencies);
67 
68   void SetOptions(const Options& options) override;
69 
70   RTCErrorOr<rtc::scoped_refptr<PeerConnectionInterface>>
71   CreatePeerConnectionOrError(
72       const PeerConnectionInterface::RTCConfiguration& configuration,
73       PeerConnectionDependencies dependencies) override;
74 
75   RtpCapabilities GetRtpSenderCapabilities(
76       cricket::MediaType kind) const override;
77 
78   RtpCapabilities GetRtpReceiverCapabilities(
79       cricket::MediaType kind) const override;
80 
81   rtc::scoped_refptr<MediaStreamInterface> CreateLocalMediaStream(
82       const std::string& stream_id) override;
83 
84   rtc::scoped_refptr<AudioSourceInterface> CreateAudioSource(
85       const cricket::AudioOptions& options) override;
86 
87   rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
88       const std::string& id,
89       VideoTrackSourceInterface* video_source) override;
90 
91   rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
92       const std::string& id,
93       AudioSourceInterface* audio_source) override;
94 
95   bool StartAecDump(FILE* file, int64_t max_size_bytes) override;
96   void StopAecDump() override;
97 
sctp_transport_factory()98   SctpTransportFactoryInterface* sctp_transport_factory() {
99     return context_->sctp_transport_factory();
100   }
101 
signaling_thread()102   rtc::Thread* signaling_thread() const {
103     // This method can be called on a different thread when the factory is
104     // created in CreatePeerConnectionFactory().
105     return context_->signaling_thread();
106   }
107 
worker_thread()108   rtc::Thread* worker_thread() const { return context_->worker_thread(); }
109 
options()110   const Options& options() const {
111     RTC_DCHECK_RUN_ON(signaling_thread());
112     return options_;
113   }
114 
field_trials()115   const FieldTrialsView& field_trials() const {
116     return context_->field_trials();
117   }
118 
119   cricket::MediaEngineInterface* media_engine() const;
120 
121  protected:
122   // Constructor used by the static Create() method. Modifies the dependencies.
123   PeerConnectionFactory(rtc::scoped_refptr<ConnectionContext> context,
124                         PeerConnectionFactoryDependencies* dependencies);
125 
126   // Constructor for use in testing. Ignores the possibility of initialization
127   // failure. The dependencies are passed in by std::move().
128   explicit PeerConnectionFactory(
129       PeerConnectionFactoryDependencies dependencies);
130 
131   virtual ~PeerConnectionFactory();
132 
133  private:
network_thread()134   rtc::Thread* network_thread() const { return context_->network_thread(); }
135 
136   bool IsTrialEnabled(absl::string_view key) const;
137 
138   std::unique_ptr<RtcEventLog> CreateRtcEventLog_w();
139   std::unique_ptr<Call> CreateCall_w(
140       RtcEventLog* event_log,
141       const FieldTrialsView& field_trials,
142       const PeerConnectionInterface::RTCConfiguration& configuration);
143 
144   rtc::scoped_refptr<ConnectionContext> context_;
145   PeerConnectionFactoryInterface::Options options_
146       RTC_GUARDED_BY(signaling_thread());
147   std::unique_ptr<TaskQueueFactory> task_queue_factory_;
148   std::unique_ptr<RtcEventLogFactoryInterface> event_log_factory_;
149   std::unique_ptr<FecControllerFactoryInterface> fec_controller_factory_;
150   std::unique_ptr<NetworkStatePredictorFactoryInterface>
151       network_state_predictor_factory_;
152   std::unique_ptr<NetworkControllerFactoryInterface>
153       injected_network_controller_factory_;
154   std::unique_ptr<NetEqFactory> neteq_factory_;
155   const std::unique_ptr<RtpTransportControllerSendFactoryInterface>
156       transport_controller_send_factory_;
157   std::unique_ptr<Metronome> metronome_ RTC_GUARDED_BY(worker_thread());
158 };
159 
160 }  // namespace webrtc
161 
162 #endif  // PC_PEER_CONNECTION_FACTORY_H_
163