1/*
2 * Copyright 2004 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#include "pc/peerconnectionfactory.h"
12
13#include <utility>
14#include <vector>
15
16#include "absl/memory/memory.h"
17#include "api/fec_controller.h"
18#include "api/media_transport_interface.h"
19#include "api/mediaconstraintsinterface.h"
20#include "api/mediastreamproxy.h"
21#include "api/mediastreamtrackproxy.h"
22#include "api/peerconnectionfactoryproxy.h"
23#include "api/peerconnectionproxy.h"
24#include "api/turncustomizer.h"
25#include "api/videosourceproxy.h"
26#include "logging/rtc_event_log/rtc_event_log.h"
27#include "media/base/rtpdataengine.h"
28#include "media/sctp/sctptransport.h"
29#include "pc/rtpparametersconversion.h"
30#include "rtc_base/bind.h"
31#include "rtc_base/checks.h"
32// Adding 'nogncheck' to disable the gn include headers check to support modular
33// WebRTC build targets.
34// TODO(zhihuang): This wouldn't be necessary if the interface and
35// implementation of the media engine were in separate build targets.
36#include "media/engine/webrtcmediaengine.h" // nogncheck
37#include "media/engine/webrtcvideodecoderfactory.h" // nogncheck
38#include "media/engine/webrtcvideoencoderfactory.h" // nogncheck
39#include "modules/audio_device/include/audio_device.h" // nogncheck
40#include "modules/congestion_controller/bbr/bbr_factory.h"
41#include "p2p/base/basicpacketsocketfactory.h"
42#include "p2p/client/basicportallocator.h"
43#include "pc/audiotrack.h"
44#include "pc/localaudiosource.h"
45#include "pc/mediastream.h"
46#include "pc/peerconnection.h"
47#include "pc/videocapturertracksource.h"
48#include "pc/videotrack.h"
49#include "rtc_base/experiments/congestion_controller_experiment.h"
50#include "system_wrappers/include/field_trial.h"
51
52namespace webrtc {
53
54rtc::scoped_refptr<PeerConnectionFactoryInterface>
55CreateModularPeerConnectionFactory(
56 rtc::Thread* network_thread,
57 rtc::Thread* worker_thread,
58 rtc::Thread* signaling_thread,
59 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
60 std::unique_ptr<CallFactoryInterface> call_factory,
61 std::unique_ptr<RtcEventLogFactoryInterface> event_log_factory) {
62 PeerConnectionFactoryDependencies dependencies;
63 dependencies.network_thread = network_thread;
64 dependencies.worker_thread = worker_thread;
65 dependencies.signaling_thread = signaling_thread;
66 dependencies.media_engine = std::move(media_engine);
67 dependencies.call_factory = std::move(call_factory);
68 dependencies.event_log_factory = std::move(event_log_factory);
69 return CreateModularPeerConnectionFactory(std::move(dependencies));
70}
71
72rtc::scoped_refptr<PeerConnectionFactoryInterface>
73CreateModularPeerConnectionFactory(
74 rtc::Thread* network_thread,
75 rtc::Thread* worker_thread,
76 rtc::Thread* signaling_thread,
77 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
78 std::unique_ptr<CallFactoryInterface> call_factory,
79 std::unique_ptr<RtcEventLogFactoryInterface> event_log_factory,
80 std::unique_ptr<FecControllerFactoryInterface> fec_controller_factory,
81 std::unique_ptr<NetworkControllerFactoryInterface>
82 network_controller_factory) {
83 PeerConnectionFactoryDependencies dependencies;
84 dependencies.network_thread = network_thread;
85 dependencies.worker_thread = worker_thread;
86 dependencies.signaling_thread = signaling_thread;
87 dependencies.media_engine = std::move(media_engine);
88 dependencies.call_factory = std::move(call_factory);
89 dependencies.event_log_factory = std::move(event_log_factory);
90 dependencies.fec_controller_factory = std::move(fec_controller_factory);
91 dependencies.network_controller_factory =
92 std::move(network_controller_factory);
93 return CreateModularPeerConnectionFactory(std::move(dependencies));
94}
95
96rtc::scoped_refptr<PeerConnectionFactoryInterface>
97CreateModularPeerConnectionFactory(
98 PeerConnectionFactoryDependencies dependencies) {
99 rtc::scoped_refptr<PeerConnectionFactory> pc_factory(
100 new rtc::RefCountedObject<PeerConnectionFactory>(
101 std::move(dependencies)));
102 // Call Initialize synchronously but make sure it is executed on
103 // |signaling_thread|.
104 MethodCall0<PeerConnectionFactory, bool> call(
105 pc_factory.get(), &PeerConnectionFactory::Initialize);
106 bool result = call.Marshal(RTC_FROM_HERE, pc_factory->signaling_thread());
107
108 if (!result) {
109 return nullptr;
110 }
111 return PeerConnectionFactoryProxy::Create(pc_factory->signaling_thread(),
112 pc_factory);
113}
114
115PeerConnectionFactory::PeerConnectionFactory(
116 rtc::Thread* network_thread,
117 rtc::Thread* worker_thread,
118 rtc::Thread* signaling_thread,
119 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
120 std::unique_ptr<webrtc::CallFactoryInterface> call_factory,
121 std::unique_ptr<RtcEventLogFactoryInterface> event_log_factory)
122 : PeerConnectionFactory(network_thread,
123 worker_thread,
124 signaling_thread,
125 std::move(media_engine),
126 std::move(call_factory),
127 std::move(event_log_factory),
128 nullptr,
129 nullptr) {}
130
131PeerConnectionFactory::PeerConnectionFactory(
132 rtc::Thread* network_thread,
133 rtc::Thread* worker_thread,
134 rtc::Thread* signaling_thread,
135 std::unique_ptr<cricket::MediaEngineInterface> media_engine,
136 std::unique_ptr<webrtc::CallFactoryInterface> call_factory,
137 std::unique_ptr<RtcEventLogFactoryInterface> event_log_factory,
138 std::unique_ptr<FecControllerFactoryInterface> fec_controller_factory,
139 std::unique_ptr<NetworkControllerFactoryInterface>
140 network_controller_factory)
141 : wraps_current_thread_(false),
142 network_thread_(network_thread),
143 worker_thread_(worker_thread),
144 signaling_thread_(signaling_thread),
145 media_engine_(std::move(media_engine)),
146 call_factory_(std::move(call_factory)),
147 event_log_factory_(std::move(event_log_factory)),
148 fec_controller_factory_(std::move(fec_controller_factory)),
149 injected_network_controller_factory_(
150 std::move(network_controller_factory)),
151 bbr_network_controller_factory_(
152 absl::make_unique<BbrNetworkControllerFactory>()) {
153 if (!network_thread_) {
154 owned_network_thread_ = rtc::Thread::CreateWithSocketServer();
155 owned_network_thread_->SetName("pc_network_thread", nullptr);
156 owned_network_thread_->Start();
157 network_thread_ = owned_network_thread_.get();
158 }
159
160 if (!worker_thread_) {
161 owned_worker_thread_ = rtc::Thread::Create();
162 owned_worker_thread_->SetName("pc_worker_thread", nullptr);
163 owned_worker_thread_->Start();
164 worker_thread_ = owned_worker_thread_.get();
165 }
166
167 if (!signaling_thread_) {
168 signaling_thread_ = rtc::Thread::Current();
169 if (!signaling_thread_) {
170 // If this thread isn't already wrapped by an rtc::Thread, create a
171 // wrapper and own it in this class.
172 signaling_thread_ = rtc::ThreadManager::Instance()->WrapCurrentThread();
173 wraps_current_thread_ = true;
174 }
175 }
176
177 // TODO(deadbeef): Currently there is no way to create an external adm in
178 // libjingle source tree. So we can 't currently assert if this is NULL.
179 // RTC_DCHECK(default_adm != NULL);
180}
181
182PeerConnectionFactory::PeerConnectionFactory(
183 PeerConnectionFactoryDependencies dependencies)
184 : PeerConnectionFactory(
185 dependencies.network_thread,
186 dependencies.worker_thread,
187 dependencies.signaling_thread,
188 std::move(dependencies.media_engine),
189 std::move(dependencies.call_factory),
190 std::move(dependencies.event_log_factory),
191 std::move(dependencies.fec_controller_factory),
192 std::move(dependencies.network_controller_factory)) {
193 media_transport_factory_ = std::move(dependencies.media_transport_factory);
194}
195
196PeerConnectionFactory::~PeerConnectionFactory() {
197 RTC_DCHECK(signaling_thread_->IsCurrent());
198 channel_manager_.reset(nullptr);
199
200 // Make sure |worker_thread_| and |signaling_thread_| outlive
201 // |default_socket_factory_| and |default_network_manager_|.
202 default_socket_factory_ = nullptr;
203 default_network_manager_ = nullptr;
204
205 if (wraps_current_thread_)
206 rtc::ThreadManager::Instance()->UnwrapCurrentThread();
207}
208
209bool PeerConnectionFactory::Initialize() {
210 RTC_DCHECK(signaling_thread_->IsCurrent());
211 rtc::InitRandom(rtc::Time32());
212
213 default_network_manager_.reset(new rtc::BasicNetworkManager());
214 if (!default_network_manager_) {
215 return false;
216 }
217
218 default_socket_factory_.reset(
219 new rtc::BasicPacketSocketFactory(network_thread_));
220 if (!default_socket_factory_) {
221 return false;
222 }
223
224 channel_manager_ = absl::make_unique<cricket::ChannelManager>(
225 std::move(media_engine_), absl::make_unique<cricket::RtpDataEngine>(),
226 worker_thread_, network_thread_);
227
228 channel_manager_->SetVideoRtxEnabled(true);
229 if (!channel_manager_->Init()) {
230 return false;
231 }
232
233 return true;
234}
235
236void PeerConnectionFactory::SetOptions(const Options& options) {
237 options_ = options;
238}
239
240RtpCapabilities PeerConnectionFactory::GetRtpSenderCapabilities(
241 cricket::MediaType kind) const {
242 RTC_DCHECK_RUN_ON(signaling_thread_);
243 switch (kind) {
244 case cricket::MEDIA_TYPE_AUDIO: {
245 cricket::AudioCodecs cricket_codecs;
246 cricket::RtpHeaderExtensions cricket_extensions;
247 channel_manager_->GetSupportedAudioSendCodecs(&cricket_codecs);
248 channel_manager_->GetSupportedAudioRtpHeaderExtensions(
249 &cricket_extensions);
250 return ToRtpCapabilities(cricket_codecs, cricket_extensions);
251 }
252 case cricket::MEDIA_TYPE_VIDEO: {
253 cricket::VideoCodecs cricket_codecs;
254 cricket::RtpHeaderExtensions cricket_extensions;
255 channel_manager_->GetSupportedVideoCodecs(&cricket_codecs);
256 channel_manager_->GetSupportedVideoRtpHeaderExtensions(
257 &cricket_extensions);
258 return ToRtpCapabilities(cricket_codecs, cricket_extensions);
259 }
260 case cricket::MEDIA_TYPE_DATA:
261 return RtpCapabilities();
262 }
263 // Not reached; avoids compile warning.
264 RTC_FATAL();
265}
266
267RtpCapabilities PeerConnectionFactory::GetRtpReceiverCapabilities(
268 cricket::MediaType kind) const {
269 RTC_DCHECK_RUN_ON(signaling_thread_);
270 switch (kind) {
271 case cricket::MEDIA_TYPE_AUDIO: {
272 cricket::AudioCodecs cricket_codecs;
273 cricket::RtpHeaderExtensions cricket_extensions;
274 channel_manager_->GetSupportedAudioReceiveCodecs(&cricket_codecs);
275 channel_manager_->GetSupportedAudioRtpHeaderExtensions(
276 &cricket_extensions);
277 return ToRtpCapabilities(cricket_codecs, cricket_extensions);
278 }
279 case cricket::MEDIA_TYPE_VIDEO: {
280 cricket::VideoCodecs cricket_codecs;
281 cricket::RtpHeaderExtensions cricket_extensions;
282 channel_manager_->GetSupportedVideoCodecs(&cricket_codecs);
283 channel_manager_->GetSupportedVideoRtpHeaderExtensions(
284 &cricket_extensions);
285 return ToRtpCapabilities(cricket_codecs, cricket_extensions);
286 }
287 case cricket::MEDIA_TYPE_DATA:
288 return RtpCapabilities();
289 }
290 // Not reached; avoids compile warning.
291 RTC_FATAL();
292}
293
294rtc::scoped_refptr<AudioSourceInterface>
295PeerConnectionFactory::CreateAudioSource(const cricket::AudioOptions& options) {
296 RTC_DCHECK(signaling_thread_->IsCurrent());
297 rtc::scoped_refptr<LocalAudioSource> source(
298 LocalAudioSource::Create(&options));
299 return source;
300}
301
302rtc::scoped_refptr<VideoTrackSourceInterface>
303PeerConnectionFactory::CreateVideoSource(
304 std::unique_ptr<cricket::VideoCapturer> capturer,
305 const MediaConstraintsInterface* constraints) {
306 RTC_DCHECK(signaling_thread_->IsCurrent());
307 rtc::scoped_refptr<VideoTrackSourceInterface> source(
308 VideoCapturerTrackSource::Create(worker_thread_, std::move(capturer),
309 constraints, false));
310 return VideoTrackSourceProxy::Create(signaling_thread_, worker_thread_,
311 source);
312}
313
314rtc::scoped_refptr<VideoTrackSourceInterface>
315PeerConnectionFactory::CreateVideoSource(
316 std::unique_ptr<cricket::VideoCapturer> capturer) {
317 RTC_DCHECK(signaling_thread_->IsCurrent());
318 rtc::scoped_refptr<VideoTrackSourceInterface> source(
319 VideoCapturerTrackSource::Create(worker_thread_, std::move(capturer),
320 false));
321 return VideoTrackSourceProxy::Create(signaling_thread_, worker_thread_,
322 source);
323}
324
325bool PeerConnectionFactory::StartAecDump(rtc::PlatformFile file,
326 int64_t max_size_bytes) {
327 RTC_DCHECK(signaling_thread_->IsCurrent());
328 return channel_manager_->StartAecDump(file, max_size_bytes);
329}
330
331void PeerConnectionFactory::StopAecDump() {
332 RTC_DCHECK(signaling_thread_->IsCurrent());
333 channel_manager_->StopAecDump();
334}
335
336rtc::scoped_refptr<PeerConnectionInterface>
337PeerConnectionFactory::CreatePeerConnection(
338 const PeerConnectionInterface::RTCConfiguration& configuration,
339 std::unique_ptr<cricket::PortAllocator> allocator,
340 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
341 PeerConnectionObserver* observer) {
342 // Convert the legacy API into the new depnedency structure.
343 PeerConnectionDependencies dependencies(observer);
344 dependencies.allocator = std::move(allocator);
345 dependencies.cert_generator = std::move(cert_generator);
346 // Pass that into the new API.
347 return CreatePeerConnection(configuration, std::move(dependencies));
348}
349
350rtc::scoped_refptr<PeerConnectionInterface>
351PeerConnectionFactory::CreatePeerConnection(
352 const PeerConnectionInterface::RTCConfiguration& configuration,
353 PeerConnectionDependencies dependencies) {
354 RTC_DCHECK(signaling_thread_->IsCurrent());
355
356 // Set internal defaults if optional dependencies are not set.
357 if (!dependencies.cert_generator) {
358 dependencies.cert_generator =
359 absl::make_unique<rtc::RTCCertificateGenerator>(signaling_thread_,
360 network_thread_);
361 }
362 if (!dependencies.allocator) {
363 network_thread_->Invoke<void>(RTC_FROM_HERE, [this, &configuration,
364 &dependencies]() {
365 dependencies.allocator = absl::make_unique<cricket::BasicPortAllocator>(
366 default_network_manager_.get(), default_socket_factory_.get(),
367 configuration.turn_customizer);
368 });
369 }
370
371 // TODO(zstein): Once chromium injects its own AsyncResolverFactory, set
372 // |dependencies.async_resolver_factory| to a new
373 // |rtc::BasicAsyncResolverFactory| if no factory is provided.
374
375 network_thread_->Invoke<void>(
376 RTC_FROM_HERE,
377 rtc::Bind(&cricket::PortAllocator::SetNetworkIgnoreMask,
378 dependencies.allocator.get(), options_.network_ignore_mask));
379
380 std::unique_ptr<RtcEventLog> event_log =
381 worker_thread_->Invoke<std::unique_ptr<RtcEventLog>>(
382 RTC_FROM_HERE,
383 rtc::Bind(&PeerConnectionFactory::CreateRtcEventLog_w, this));
384
385 std::unique_ptr<Call> call = worker_thread_->Invoke<std::unique_ptr<Call>>(
386 RTC_FROM_HERE,
387 rtc::Bind(&PeerConnectionFactory::CreateCall_w, this, event_log.get()));
388
389 rtc::scoped_refptr<PeerConnection> pc(
390 new rtc::RefCountedObject<PeerConnection>(this, std::move(event_log),
391 std::move(call)));
392 ActionsBeforeInitializeForTesting(pc);
393 if (!pc->Initialize(configuration, std::move(dependencies))) {
394 return nullptr;
395 }
396 return PeerConnectionProxy::Create(signaling_thread(), pc);
397}
398
399rtc::scoped_refptr<MediaStreamInterface>
400PeerConnectionFactory::CreateLocalMediaStream(const std::string& stream_id) {
401 RTC_DCHECK(signaling_thread_->IsCurrent());
402 return MediaStreamProxy::Create(signaling_thread_,
403 MediaStream::Create(stream_id));
404}
405
406rtc::scoped_refptr<VideoTrackInterface> PeerConnectionFactory::CreateVideoTrack(
407 const std::string& id,
408 VideoTrackSourceInterface* source) {
409 RTC_DCHECK(signaling_thread_->IsCurrent());
410 rtc::scoped_refptr<VideoTrackInterface> track(
411 VideoTrack::Create(id, source, worker_thread_));
412 return VideoTrackProxy::Create(signaling_thread_, worker_thread_, track);
413}
414
415rtc::scoped_refptr<AudioTrackInterface> PeerConnectionFactory::CreateAudioTrack(
416 const std::string& id,
417 AudioSourceInterface* source) {
418 RTC_DCHECK(signaling_thread_->IsCurrent());
419 rtc::scoped_refptr<AudioTrackInterface> track(AudioTrack::Create(id, source));
420 return AudioTrackProxy::Create(signaling_thread_, track);
421}
422
423std::unique_ptr<cricket::SctpTransportInternalFactory>
424PeerConnectionFactory::CreateSctpTransportInternalFactory() {
425#ifdef HAVE_SCTP
426 return absl::make_unique<cricket::SctpTransportFactory>(network_thread());
427#else
428 return nullptr;
429#endif
430}
431
432cricket::ChannelManager* PeerConnectionFactory::channel_manager() {
433 return channel_manager_.get();
434}
435
436rtc::Thread* PeerConnectionFactory::signaling_thread() {
437 // This method can be called on a different thread when the factory is
438 // created in CreatePeerConnectionFactory().
439 return signaling_thread_;
440}
441
442rtc::Thread* PeerConnectionFactory::worker_thread() {
443 return worker_thread_;
444}
445
446rtc::Thread* PeerConnectionFactory::network_thread() {
447 return network_thread_;
448}
449
450std::unique_ptr<RtcEventLog> PeerConnectionFactory::CreateRtcEventLog_w() {
451 RTC_DCHECK_RUN_ON(worker_thread_);
452
453 auto encoding_type = RtcEventLog::EncodingType::Legacy;
454 if (field_trial::IsEnabled("WebRTC-RtcEventLogNewFormat"))
455 encoding_type = RtcEventLog::EncodingType::NewFormat;
456 return event_log_factory_
457 ? event_log_factory_->CreateRtcEventLog(encoding_type)
458 : absl::make_unique<RtcEventLogNullImpl>();
459}
460
461std::unique_ptr<Call> PeerConnectionFactory::CreateCall_w(
462 RtcEventLog* event_log) {
463 RTC_DCHECK_RUN_ON(worker_thread_);
464
465 const int kMinBandwidthBps = 30000;
466 const int kStartBandwidthBps = 300000;
467 const int kMaxBandwidthBps = 2000000;
468
469 webrtc::Call::Config call_config(event_log);
470 if (!channel_manager_->media_engine() || !call_factory_) {
471 return nullptr;
472 }
473 call_config.audio_state =
474 channel_manager_->media_engine()->voice().GetAudioState();
475 call_config.bitrate_config.min_bitrate_bps = kMinBandwidthBps;
476 call_config.bitrate_config.start_bitrate_bps = kStartBandwidthBps;
477 call_config.bitrate_config.max_bitrate_bps = kMaxBandwidthBps;
478
479 call_config.fec_controller_factory = fec_controller_factory_.get();
480
481 if (CongestionControllerExperiment::BbrControllerEnabled()) {
482 RTC_LOG(LS_INFO) << "Using BBR network controller factory";
483 call_config.network_controller_factory =
484 bbr_network_controller_factory_.get();
485 } else if (CongestionControllerExperiment::InjectedControllerEnabled()) {
486 RTC_LOG(LS_INFO) << "Using injected network controller factory";
487 call_config.network_controller_factory =
488 injected_network_controller_factory_.get();
489 } else {
490 RTC_LOG(LS_INFO) << "Using default network controller factory";
491 }
492
493 return std::unique_ptr<Call>(call_factory_->CreateCall(call_config));
494}
495
496} // namespace webrtc
497