1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker * Copyright 2004 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker *
4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker */
10*d9f75844SAndroid Build Coastguard Worker
11*d9f75844SAndroid Build Coastguard Worker #include "pc/peer_connection_factory.h"
12*d9f75844SAndroid Build Coastguard Worker
13*d9f75844SAndroid Build Coastguard Worker #include <type_traits>
14*d9f75844SAndroid Build Coastguard Worker #include <utility>
15*d9f75844SAndroid Build Coastguard Worker
16*d9f75844SAndroid Build Coastguard Worker #include "absl/strings/match.h"
17*d9f75844SAndroid Build Coastguard Worker #include "api/async_resolver_factory.h"
18*d9f75844SAndroid Build Coastguard Worker #include "api/call/call_factory_interface.h"
19*d9f75844SAndroid Build Coastguard Worker #include "api/fec_controller.h"
20*d9f75844SAndroid Build Coastguard Worker #include "api/ice_transport_interface.h"
21*d9f75844SAndroid Build Coastguard Worker #include "api/network_state_predictor.h"
22*d9f75844SAndroid Build Coastguard Worker #include "api/packet_socket_factory.h"
23*d9f75844SAndroid Build Coastguard Worker #include "api/rtc_event_log/rtc_event_log.h"
24*d9f75844SAndroid Build Coastguard Worker #include "api/sequence_checker.h"
25*d9f75844SAndroid Build Coastguard Worker #include "api/transport/bitrate_settings.h"
26*d9f75844SAndroid Build Coastguard Worker #include "api/units/data_rate.h"
27*d9f75844SAndroid Build Coastguard Worker #include "call/audio_state.h"
28*d9f75844SAndroid Build Coastguard Worker #include "call/rtp_transport_controller_send_factory.h"
29*d9f75844SAndroid Build Coastguard Worker #include "media/base/media_engine.h"
30*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/basic_async_resolver_factory.h"
31*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/basic_packet_socket_factory.h"
32*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/default_ice_transport_factory.h"
33*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/port_allocator.h"
34*d9f75844SAndroid Build Coastguard Worker #include "p2p/client/basic_port_allocator.h"
35*d9f75844SAndroid Build Coastguard Worker #include "pc/audio_track.h"
36*d9f75844SAndroid Build Coastguard Worker #include "pc/local_audio_source.h"
37*d9f75844SAndroid Build Coastguard Worker #include "pc/media_stream.h"
38*d9f75844SAndroid Build Coastguard Worker #include "pc/media_stream_proxy.h"
39*d9f75844SAndroid Build Coastguard Worker #include "pc/media_stream_track_proxy.h"
40*d9f75844SAndroid Build Coastguard Worker #include "pc/peer_connection.h"
41*d9f75844SAndroid Build Coastguard Worker #include "pc/peer_connection_factory_proxy.h"
42*d9f75844SAndroid Build Coastguard Worker #include "pc/peer_connection_proxy.h"
43*d9f75844SAndroid Build Coastguard Worker #include "pc/rtp_parameters_conversion.h"
44*d9f75844SAndroid Build Coastguard Worker #include "pc/session_description.h"
45*d9f75844SAndroid Build Coastguard Worker #include "pc/video_track.h"
46*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h"
47*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/experiments/field_trial_parser.h"
48*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/experiments/field_trial_units.h"
49*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/logging.h"
50*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/numerics/safe_conversions.h"
51*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/rtc_certificate_generator.h"
52*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/system/file_wrapper.h"
53*d9f75844SAndroid Build Coastguard Worker
54*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
55*d9f75844SAndroid Build Coastguard Worker
56*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<PeerConnectionFactoryInterface>
CreateModularPeerConnectionFactory(PeerConnectionFactoryDependencies dependencies)57*d9f75844SAndroid Build Coastguard Worker CreateModularPeerConnectionFactory(
58*d9f75844SAndroid Build Coastguard Worker PeerConnectionFactoryDependencies dependencies) {
59*d9f75844SAndroid Build Coastguard Worker // The PeerConnectionFactory must be created on the signaling thread.
60*d9f75844SAndroid Build Coastguard Worker if (dependencies.signaling_thread &&
61*d9f75844SAndroid Build Coastguard Worker !dependencies.signaling_thread->IsCurrent()) {
62*d9f75844SAndroid Build Coastguard Worker return dependencies.signaling_thread->BlockingCall([&dependencies] {
63*d9f75844SAndroid Build Coastguard Worker return CreateModularPeerConnectionFactory(std::move(dependencies));
64*d9f75844SAndroid Build Coastguard Worker });
65*d9f75844SAndroid Build Coastguard Worker }
66*d9f75844SAndroid Build Coastguard Worker
67*d9f75844SAndroid Build Coastguard Worker auto pc_factory = PeerConnectionFactory::Create(std::move(dependencies));
68*d9f75844SAndroid Build Coastguard Worker if (!pc_factory) {
69*d9f75844SAndroid Build Coastguard Worker return nullptr;
70*d9f75844SAndroid Build Coastguard Worker }
71*d9f75844SAndroid Build Coastguard Worker // Verify that the invocation and the initialization ended up agreeing on the
72*d9f75844SAndroid Build Coastguard Worker // thread.
73*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(pc_factory->signaling_thread());
74*d9f75844SAndroid Build Coastguard Worker return PeerConnectionFactoryProxy::Create(
75*d9f75844SAndroid Build Coastguard Worker pc_factory->signaling_thread(), pc_factory->worker_thread(), pc_factory);
76*d9f75844SAndroid Build Coastguard Worker }
77*d9f75844SAndroid Build Coastguard Worker
78*d9f75844SAndroid Build Coastguard Worker // Static
Create(PeerConnectionFactoryDependencies dependencies)79*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<PeerConnectionFactory> PeerConnectionFactory::Create(
80*d9f75844SAndroid Build Coastguard Worker PeerConnectionFactoryDependencies dependencies) {
81*d9f75844SAndroid Build Coastguard Worker auto context = ConnectionContext::Create(&dependencies);
82*d9f75844SAndroid Build Coastguard Worker if (!context) {
83*d9f75844SAndroid Build Coastguard Worker return nullptr;
84*d9f75844SAndroid Build Coastguard Worker }
85*d9f75844SAndroid Build Coastguard Worker return rtc::make_ref_counted<PeerConnectionFactory>(context, &dependencies);
86*d9f75844SAndroid Build Coastguard Worker }
87*d9f75844SAndroid Build Coastguard Worker
PeerConnectionFactory(rtc::scoped_refptr<ConnectionContext> context,PeerConnectionFactoryDependencies * dependencies)88*d9f75844SAndroid Build Coastguard Worker PeerConnectionFactory::PeerConnectionFactory(
89*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<ConnectionContext> context,
90*d9f75844SAndroid Build Coastguard Worker PeerConnectionFactoryDependencies* dependencies)
91*d9f75844SAndroid Build Coastguard Worker : context_(context),
92*d9f75844SAndroid Build Coastguard Worker task_queue_factory_(std::move(dependencies->task_queue_factory)),
93*d9f75844SAndroid Build Coastguard Worker event_log_factory_(std::move(dependencies->event_log_factory)),
94*d9f75844SAndroid Build Coastguard Worker fec_controller_factory_(std::move(dependencies->fec_controller_factory)),
95*d9f75844SAndroid Build Coastguard Worker network_state_predictor_factory_(
96*d9f75844SAndroid Build Coastguard Worker std::move(dependencies->network_state_predictor_factory)),
97*d9f75844SAndroid Build Coastguard Worker injected_network_controller_factory_(
98*d9f75844SAndroid Build Coastguard Worker std::move(dependencies->network_controller_factory)),
99*d9f75844SAndroid Build Coastguard Worker neteq_factory_(std::move(dependencies->neteq_factory)),
100*d9f75844SAndroid Build Coastguard Worker transport_controller_send_factory_(
101*d9f75844SAndroid Build Coastguard Worker (dependencies->transport_controller_send_factory)
102*d9f75844SAndroid Build Coastguard Worker ? std::move(dependencies->transport_controller_send_factory)
103*d9f75844SAndroid Build Coastguard Worker : std::make_unique<RtpTransportControllerSendFactory>()),
104*d9f75844SAndroid Build Coastguard Worker metronome_(std::move(dependencies->metronome)) {}
105*d9f75844SAndroid Build Coastguard Worker
PeerConnectionFactory(PeerConnectionFactoryDependencies dependencies)106*d9f75844SAndroid Build Coastguard Worker PeerConnectionFactory::PeerConnectionFactory(
107*d9f75844SAndroid Build Coastguard Worker PeerConnectionFactoryDependencies dependencies)
108*d9f75844SAndroid Build Coastguard Worker : PeerConnectionFactory(ConnectionContext::Create(&dependencies),
109*d9f75844SAndroid Build Coastguard Worker &dependencies) {}
110*d9f75844SAndroid Build Coastguard Worker
~PeerConnectionFactory()111*d9f75844SAndroid Build Coastguard Worker PeerConnectionFactory::~PeerConnectionFactory() {
112*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(signaling_thread());
113*d9f75844SAndroid Build Coastguard Worker worker_thread()->BlockingCall([this] {
114*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(worker_thread());
115*d9f75844SAndroid Build Coastguard Worker metronome_ = nullptr;
116*d9f75844SAndroid Build Coastguard Worker });
117*d9f75844SAndroid Build Coastguard Worker }
118*d9f75844SAndroid Build Coastguard Worker
SetOptions(const Options & options)119*d9f75844SAndroid Build Coastguard Worker void PeerConnectionFactory::SetOptions(const Options& options) {
120*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(signaling_thread());
121*d9f75844SAndroid Build Coastguard Worker options_ = options;
122*d9f75844SAndroid Build Coastguard Worker }
123*d9f75844SAndroid Build Coastguard Worker
GetRtpSenderCapabilities(cricket::MediaType kind) const124*d9f75844SAndroid Build Coastguard Worker RtpCapabilities PeerConnectionFactory::GetRtpSenderCapabilities(
125*d9f75844SAndroid Build Coastguard Worker cricket::MediaType kind) const {
126*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(signaling_thread());
127*d9f75844SAndroid Build Coastguard Worker switch (kind) {
128*d9f75844SAndroid Build Coastguard Worker case cricket::MEDIA_TYPE_AUDIO: {
129*d9f75844SAndroid Build Coastguard Worker cricket::AudioCodecs cricket_codecs;
130*d9f75844SAndroid Build Coastguard Worker cricket_codecs = media_engine()->voice().send_codecs();
131*d9f75844SAndroid Build Coastguard Worker auto extensions =
132*d9f75844SAndroid Build Coastguard Worker GetDefaultEnabledRtpHeaderExtensions(media_engine()->voice());
133*d9f75844SAndroid Build Coastguard Worker return ToRtpCapabilities(cricket_codecs, extensions);
134*d9f75844SAndroid Build Coastguard Worker }
135*d9f75844SAndroid Build Coastguard Worker case cricket::MEDIA_TYPE_VIDEO: {
136*d9f75844SAndroid Build Coastguard Worker cricket::VideoCodecs cricket_codecs;
137*d9f75844SAndroid Build Coastguard Worker cricket_codecs = media_engine()->video().send_codecs();
138*d9f75844SAndroid Build Coastguard Worker auto extensions =
139*d9f75844SAndroid Build Coastguard Worker GetDefaultEnabledRtpHeaderExtensions(media_engine()->video());
140*d9f75844SAndroid Build Coastguard Worker return ToRtpCapabilities(cricket_codecs, extensions);
141*d9f75844SAndroid Build Coastguard Worker }
142*d9f75844SAndroid Build Coastguard Worker case cricket::MEDIA_TYPE_DATA:
143*d9f75844SAndroid Build Coastguard Worker return RtpCapabilities();
144*d9f75844SAndroid Build Coastguard Worker case cricket::MEDIA_TYPE_UNSUPPORTED:
145*d9f75844SAndroid Build Coastguard Worker return RtpCapabilities();
146*d9f75844SAndroid Build Coastguard Worker }
147*d9f75844SAndroid Build Coastguard Worker RTC_DLOG(LS_ERROR) << "Got unexpected MediaType " << kind;
148*d9f75844SAndroid Build Coastguard Worker RTC_CHECK_NOTREACHED();
149*d9f75844SAndroid Build Coastguard Worker }
150*d9f75844SAndroid Build Coastguard Worker
GetRtpReceiverCapabilities(cricket::MediaType kind) const151*d9f75844SAndroid Build Coastguard Worker RtpCapabilities PeerConnectionFactory::GetRtpReceiverCapabilities(
152*d9f75844SAndroid Build Coastguard Worker cricket::MediaType kind) const {
153*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(signaling_thread());
154*d9f75844SAndroid Build Coastguard Worker switch (kind) {
155*d9f75844SAndroid Build Coastguard Worker case cricket::MEDIA_TYPE_AUDIO: {
156*d9f75844SAndroid Build Coastguard Worker cricket::AudioCodecs cricket_codecs;
157*d9f75844SAndroid Build Coastguard Worker cricket_codecs = media_engine()->voice().recv_codecs();
158*d9f75844SAndroid Build Coastguard Worker auto extensions =
159*d9f75844SAndroid Build Coastguard Worker GetDefaultEnabledRtpHeaderExtensions(media_engine()->voice());
160*d9f75844SAndroid Build Coastguard Worker return ToRtpCapabilities(cricket_codecs, extensions);
161*d9f75844SAndroid Build Coastguard Worker }
162*d9f75844SAndroid Build Coastguard Worker case cricket::MEDIA_TYPE_VIDEO: {
163*d9f75844SAndroid Build Coastguard Worker cricket::VideoCodecs cricket_codecs =
164*d9f75844SAndroid Build Coastguard Worker media_engine()->video().recv_codecs(context_->use_rtx());
165*d9f75844SAndroid Build Coastguard Worker auto extensions =
166*d9f75844SAndroid Build Coastguard Worker GetDefaultEnabledRtpHeaderExtensions(media_engine()->video());
167*d9f75844SAndroid Build Coastguard Worker return ToRtpCapabilities(cricket_codecs, extensions);
168*d9f75844SAndroid Build Coastguard Worker }
169*d9f75844SAndroid Build Coastguard Worker case cricket::MEDIA_TYPE_DATA:
170*d9f75844SAndroid Build Coastguard Worker return RtpCapabilities();
171*d9f75844SAndroid Build Coastguard Worker case cricket::MEDIA_TYPE_UNSUPPORTED:
172*d9f75844SAndroid Build Coastguard Worker return RtpCapabilities();
173*d9f75844SAndroid Build Coastguard Worker }
174*d9f75844SAndroid Build Coastguard Worker RTC_DLOG(LS_ERROR) << "Got unexpected MediaType " << kind;
175*d9f75844SAndroid Build Coastguard Worker RTC_CHECK_NOTREACHED();
176*d9f75844SAndroid Build Coastguard Worker }
177*d9f75844SAndroid Build Coastguard Worker
178*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<AudioSourceInterface>
CreateAudioSource(const cricket::AudioOptions & options)179*d9f75844SAndroid Build Coastguard Worker PeerConnectionFactory::CreateAudioSource(const cricket::AudioOptions& options) {
180*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(signaling_thread()->IsCurrent());
181*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<LocalAudioSource> source(
182*d9f75844SAndroid Build Coastguard Worker LocalAudioSource::Create(&options));
183*d9f75844SAndroid Build Coastguard Worker return source;
184*d9f75844SAndroid Build Coastguard Worker }
185*d9f75844SAndroid Build Coastguard Worker
StartAecDump(FILE * file,int64_t max_size_bytes)186*d9f75844SAndroid Build Coastguard Worker bool PeerConnectionFactory::StartAecDump(FILE* file, int64_t max_size_bytes) {
187*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(worker_thread());
188*d9f75844SAndroid Build Coastguard Worker return media_engine()->voice().StartAecDump(FileWrapper(file),
189*d9f75844SAndroid Build Coastguard Worker max_size_bytes);
190*d9f75844SAndroid Build Coastguard Worker }
191*d9f75844SAndroid Build Coastguard Worker
StopAecDump()192*d9f75844SAndroid Build Coastguard Worker void PeerConnectionFactory::StopAecDump() {
193*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(worker_thread());
194*d9f75844SAndroid Build Coastguard Worker media_engine()->voice().StopAecDump();
195*d9f75844SAndroid Build Coastguard Worker }
196*d9f75844SAndroid Build Coastguard Worker
media_engine() const197*d9f75844SAndroid Build Coastguard Worker cricket::MediaEngineInterface* PeerConnectionFactory::media_engine() const {
198*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(context_);
199*d9f75844SAndroid Build Coastguard Worker return context_->media_engine();
200*d9f75844SAndroid Build Coastguard Worker }
201*d9f75844SAndroid Build Coastguard Worker
202*d9f75844SAndroid Build Coastguard Worker RTCErrorOr<rtc::scoped_refptr<PeerConnectionInterface>>
CreatePeerConnectionOrError(const PeerConnectionInterface::RTCConfiguration & configuration,PeerConnectionDependencies dependencies)203*d9f75844SAndroid Build Coastguard Worker PeerConnectionFactory::CreatePeerConnectionOrError(
204*d9f75844SAndroid Build Coastguard Worker const PeerConnectionInterface::RTCConfiguration& configuration,
205*d9f75844SAndroid Build Coastguard Worker PeerConnectionDependencies dependencies) {
206*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(signaling_thread());
207*d9f75844SAndroid Build Coastguard Worker
208*d9f75844SAndroid Build Coastguard Worker // Set internal defaults if optional dependencies are not set.
209*d9f75844SAndroid Build Coastguard Worker if (!dependencies.cert_generator) {
210*d9f75844SAndroid Build Coastguard Worker dependencies.cert_generator =
211*d9f75844SAndroid Build Coastguard Worker std::make_unique<rtc::RTCCertificateGenerator>(signaling_thread(),
212*d9f75844SAndroid Build Coastguard Worker network_thread());
213*d9f75844SAndroid Build Coastguard Worker }
214*d9f75844SAndroid Build Coastguard Worker if (!dependencies.allocator) {
215*d9f75844SAndroid Build Coastguard Worker const FieldTrialsView* trials =
216*d9f75844SAndroid Build Coastguard Worker dependencies.trials ? dependencies.trials.get() : &field_trials();
217*d9f75844SAndroid Build Coastguard Worker dependencies.allocator = std::make_unique<cricket::BasicPortAllocator>(
218*d9f75844SAndroid Build Coastguard Worker context_->default_network_manager(), context_->default_socket_factory(),
219*d9f75844SAndroid Build Coastguard Worker configuration.turn_customizer, /*relay_port_factory=*/nullptr, trials);
220*d9f75844SAndroid Build Coastguard Worker dependencies.allocator->SetPortRange(
221*d9f75844SAndroid Build Coastguard Worker configuration.port_allocator_config.min_port,
222*d9f75844SAndroid Build Coastguard Worker configuration.port_allocator_config.max_port);
223*d9f75844SAndroid Build Coastguard Worker dependencies.allocator->set_flags(
224*d9f75844SAndroid Build Coastguard Worker configuration.port_allocator_config.flags);
225*d9f75844SAndroid Build Coastguard Worker }
226*d9f75844SAndroid Build Coastguard Worker
227*d9f75844SAndroid Build Coastguard Worker if (!dependencies.async_resolver_factory) {
228*d9f75844SAndroid Build Coastguard Worker dependencies.async_resolver_factory =
229*d9f75844SAndroid Build Coastguard Worker std::make_unique<webrtc::BasicAsyncResolverFactory>();
230*d9f75844SAndroid Build Coastguard Worker }
231*d9f75844SAndroid Build Coastguard Worker
232*d9f75844SAndroid Build Coastguard Worker if (!dependencies.ice_transport_factory) {
233*d9f75844SAndroid Build Coastguard Worker dependencies.ice_transport_factory =
234*d9f75844SAndroid Build Coastguard Worker std::make_unique<DefaultIceTransportFactory>();
235*d9f75844SAndroid Build Coastguard Worker }
236*d9f75844SAndroid Build Coastguard Worker
237*d9f75844SAndroid Build Coastguard Worker dependencies.allocator->SetNetworkIgnoreMask(options().network_ignore_mask);
238*d9f75844SAndroid Build Coastguard Worker dependencies.allocator->SetVpnList(configuration.vpn_list);
239*d9f75844SAndroid Build Coastguard Worker
240*d9f75844SAndroid Build Coastguard Worker std::unique_ptr<RtcEventLog> event_log =
241*d9f75844SAndroid Build Coastguard Worker worker_thread()->BlockingCall([this] { return CreateRtcEventLog_w(); });
242*d9f75844SAndroid Build Coastguard Worker
243*d9f75844SAndroid Build Coastguard Worker const FieldTrialsView* trials =
244*d9f75844SAndroid Build Coastguard Worker dependencies.trials ? dependencies.trials.get() : &field_trials();
245*d9f75844SAndroid Build Coastguard Worker std::unique_ptr<Call> call =
246*d9f75844SAndroid Build Coastguard Worker worker_thread()->BlockingCall([this, &event_log, trials, &configuration] {
247*d9f75844SAndroid Build Coastguard Worker return CreateCall_w(event_log.get(), *trials, configuration);
248*d9f75844SAndroid Build Coastguard Worker });
249*d9f75844SAndroid Build Coastguard Worker
250*d9f75844SAndroid Build Coastguard Worker auto result = PeerConnection::Create(context_, options_, std::move(event_log),
251*d9f75844SAndroid Build Coastguard Worker std::move(call), configuration,
252*d9f75844SAndroid Build Coastguard Worker std::move(dependencies));
253*d9f75844SAndroid Build Coastguard Worker if (!result.ok()) {
254*d9f75844SAndroid Build Coastguard Worker return result.MoveError();
255*d9f75844SAndroid Build Coastguard Worker }
256*d9f75844SAndroid Build Coastguard Worker // We configure the proxy with a pointer to the network thread for methods
257*d9f75844SAndroid Build Coastguard Worker // that need to be invoked there rather than on the signaling thread.
258*d9f75844SAndroid Build Coastguard Worker // Internally, the proxy object has a member variable named `worker_thread_`
259*d9f75844SAndroid Build Coastguard Worker // which will point to the network thread (and not the factory's
260*d9f75844SAndroid Build Coastguard Worker // worker_thread()). All such methods have thread checks though, so the code
261*d9f75844SAndroid Build Coastguard Worker // should still be clear (outside of macro expansion).
262*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<PeerConnectionInterface> result_proxy =
263*d9f75844SAndroid Build Coastguard Worker PeerConnectionProxy::Create(signaling_thread(), network_thread(),
264*d9f75844SAndroid Build Coastguard Worker result.MoveValue());
265*d9f75844SAndroid Build Coastguard Worker return result_proxy;
266*d9f75844SAndroid Build Coastguard Worker }
267*d9f75844SAndroid Build Coastguard Worker
268*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<MediaStreamInterface>
CreateLocalMediaStream(const std::string & stream_id)269*d9f75844SAndroid Build Coastguard Worker PeerConnectionFactory::CreateLocalMediaStream(const std::string& stream_id) {
270*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(signaling_thread()->IsCurrent());
271*d9f75844SAndroid Build Coastguard Worker return MediaStreamProxy::Create(signaling_thread(),
272*d9f75844SAndroid Build Coastguard Worker MediaStream::Create(stream_id));
273*d9f75844SAndroid Build Coastguard Worker }
274*d9f75844SAndroid Build Coastguard Worker
CreateVideoTrack(const std::string & id,VideoTrackSourceInterface * source)275*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<VideoTrackInterface> PeerConnectionFactory::CreateVideoTrack(
276*d9f75844SAndroid Build Coastguard Worker const std::string& id,
277*d9f75844SAndroid Build Coastguard Worker VideoTrackSourceInterface* source) {
278*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(signaling_thread()->IsCurrent());
279*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<VideoTrackInterface> track = VideoTrack::Create(
280*d9f75844SAndroid Build Coastguard Worker id, rtc::scoped_refptr<VideoTrackSourceInterface>(source),
281*d9f75844SAndroid Build Coastguard Worker worker_thread());
282*d9f75844SAndroid Build Coastguard Worker return VideoTrackProxy::Create(signaling_thread(), worker_thread(), track);
283*d9f75844SAndroid Build Coastguard Worker }
284*d9f75844SAndroid Build Coastguard Worker
CreateAudioTrack(const std::string & id,AudioSourceInterface * source)285*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<AudioTrackInterface> PeerConnectionFactory::CreateAudioTrack(
286*d9f75844SAndroid Build Coastguard Worker const std::string& id,
287*d9f75844SAndroid Build Coastguard Worker AudioSourceInterface* source) {
288*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(signaling_thread()->IsCurrent());
289*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<AudioTrackInterface> track =
290*d9f75844SAndroid Build Coastguard Worker AudioTrack::Create(id, rtc::scoped_refptr<AudioSourceInterface>(source));
291*d9f75844SAndroid Build Coastguard Worker return AudioTrackProxy::Create(signaling_thread(), track);
292*d9f75844SAndroid Build Coastguard Worker }
293*d9f75844SAndroid Build Coastguard Worker
CreateRtcEventLog_w()294*d9f75844SAndroid Build Coastguard Worker std::unique_ptr<RtcEventLog> PeerConnectionFactory::CreateRtcEventLog_w() {
295*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(worker_thread());
296*d9f75844SAndroid Build Coastguard Worker
297*d9f75844SAndroid Build Coastguard Worker auto encoding_type = RtcEventLog::EncodingType::Legacy;
298*d9f75844SAndroid Build Coastguard Worker if (IsTrialEnabled("WebRTC-RtcEventLogNewFormat"))
299*d9f75844SAndroid Build Coastguard Worker encoding_type = RtcEventLog::EncodingType::NewFormat;
300*d9f75844SAndroid Build Coastguard Worker return event_log_factory_ ? event_log_factory_->Create(encoding_type)
301*d9f75844SAndroid Build Coastguard Worker : std::make_unique<RtcEventLogNull>();
302*d9f75844SAndroid Build Coastguard Worker }
303*d9f75844SAndroid Build Coastguard Worker
CreateCall_w(RtcEventLog * event_log,const FieldTrialsView & field_trials,const PeerConnectionInterface::RTCConfiguration & configuration)304*d9f75844SAndroid Build Coastguard Worker std::unique_ptr<Call> PeerConnectionFactory::CreateCall_w(
305*d9f75844SAndroid Build Coastguard Worker RtcEventLog* event_log,
306*d9f75844SAndroid Build Coastguard Worker const FieldTrialsView& field_trials,
307*d9f75844SAndroid Build Coastguard Worker const PeerConnectionInterface::RTCConfiguration& configuration) {
308*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(worker_thread());
309*d9f75844SAndroid Build Coastguard Worker
310*d9f75844SAndroid Build Coastguard Worker webrtc::Call::Config call_config(event_log, network_thread());
311*d9f75844SAndroid Build Coastguard Worker if (!media_engine() || !context_->call_factory()) {
312*d9f75844SAndroid Build Coastguard Worker return nullptr;
313*d9f75844SAndroid Build Coastguard Worker }
314*d9f75844SAndroid Build Coastguard Worker call_config.audio_state = media_engine()->voice().GetAudioState();
315*d9f75844SAndroid Build Coastguard Worker
316*d9f75844SAndroid Build Coastguard Worker FieldTrialParameter<DataRate> min_bandwidth("min",
317*d9f75844SAndroid Build Coastguard Worker DataRate::KilobitsPerSec(30));
318*d9f75844SAndroid Build Coastguard Worker FieldTrialParameter<DataRate> start_bandwidth("start",
319*d9f75844SAndroid Build Coastguard Worker DataRate::KilobitsPerSec(300));
320*d9f75844SAndroid Build Coastguard Worker FieldTrialParameter<DataRate> max_bandwidth("max",
321*d9f75844SAndroid Build Coastguard Worker DataRate::KilobitsPerSec(2000));
322*d9f75844SAndroid Build Coastguard Worker ParseFieldTrial({&min_bandwidth, &start_bandwidth, &max_bandwidth},
323*d9f75844SAndroid Build Coastguard Worker field_trials.Lookup("WebRTC-PcFactoryDefaultBitrates"));
324*d9f75844SAndroid Build Coastguard Worker
325*d9f75844SAndroid Build Coastguard Worker call_config.bitrate_config.min_bitrate_bps =
326*d9f75844SAndroid Build Coastguard Worker rtc::saturated_cast<int>(min_bandwidth->bps());
327*d9f75844SAndroid Build Coastguard Worker call_config.bitrate_config.start_bitrate_bps =
328*d9f75844SAndroid Build Coastguard Worker rtc::saturated_cast<int>(start_bandwidth->bps());
329*d9f75844SAndroid Build Coastguard Worker call_config.bitrate_config.max_bitrate_bps =
330*d9f75844SAndroid Build Coastguard Worker rtc::saturated_cast<int>(max_bandwidth->bps());
331*d9f75844SAndroid Build Coastguard Worker
332*d9f75844SAndroid Build Coastguard Worker call_config.fec_controller_factory = fec_controller_factory_.get();
333*d9f75844SAndroid Build Coastguard Worker call_config.task_queue_factory = task_queue_factory_.get();
334*d9f75844SAndroid Build Coastguard Worker call_config.network_state_predictor_factory =
335*d9f75844SAndroid Build Coastguard Worker network_state_predictor_factory_.get();
336*d9f75844SAndroid Build Coastguard Worker call_config.neteq_factory = neteq_factory_.get();
337*d9f75844SAndroid Build Coastguard Worker
338*d9f75844SAndroid Build Coastguard Worker if (IsTrialEnabled("WebRTC-Bwe-InjectedCongestionController")) {
339*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_INFO) << "Using injected network controller factory";
340*d9f75844SAndroid Build Coastguard Worker call_config.network_controller_factory =
341*d9f75844SAndroid Build Coastguard Worker injected_network_controller_factory_.get();
342*d9f75844SAndroid Build Coastguard Worker } else {
343*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_INFO) << "Using default network controller factory";
344*d9f75844SAndroid Build Coastguard Worker }
345*d9f75844SAndroid Build Coastguard Worker
346*d9f75844SAndroid Build Coastguard Worker call_config.trials = &field_trials;
347*d9f75844SAndroid Build Coastguard Worker call_config.rtp_transport_controller_send_factory =
348*d9f75844SAndroid Build Coastguard Worker transport_controller_send_factory_.get();
349*d9f75844SAndroid Build Coastguard Worker call_config.metronome = metronome_.get();
350*d9f75844SAndroid Build Coastguard Worker call_config.pacer_burst_interval = configuration.pacer_burst_interval;
351*d9f75844SAndroid Build Coastguard Worker return std::unique_ptr<Call>(
352*d9f75844SAndroid Build Coastguard Worker context_->call_factory()->CreateCall(call_config));
353*d9f75844SAndroid Build Coastguard Worker }
354*d9f75844SAndroid Build Coastguard Worker
IsTrialEnabled(absl::string_view key) const355*d9f75844SAndroid Build Coastguard Worker bool PeerConnectionFactory::IsTrialEnabled(absl::string_view key) const {
356*d9f75844SAndroid Build Coastguard Worker return absl::StartsWith(field_trials().Lookup(key), "Enabled");
357*d9f75844SAndroid Build Coastguard Worker }
358*d9f75844SAndroid Build Coastguard Worker
359*d9f75844SAndroid Build Coastguard Worker } // namespace webrtc
360