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