| 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/peer_connection_factory.h" |
| 12 |
| 13 | #include <cstdio> |
| 14 | #include <memory> |
| 15 | #include <utility> |
| 16 | #include <vector> |
| 17 |
| 18 | #include "absl/strings/match.h" |
| 19 | #include "api/fec_controller.h" |
| 20 | #include "api/media_stream_proxy.h" |
| 21 | #include "api/media_stream_track_proxy.h" |
| 22 | #include "api/network_state_predictor.h" |
| 23 | #include "api/peer_connection_factory_proxy.h" |
| 24 | #include "api/peer_connection_proxy.h" |
| 25 | #include "api/rtc_event_log/rtc_event_log.h" |
| 26 | #include "api/transport/field_trial_based_config.h" |
| 27 | #include "api/turn_customizer.h" |
| 28 | #include "api/units/data_rate.h" |
| 29 | #include "api/video_track_source_proxy.h" |
| 30 | #include "media/base/rtp_data_engine.h" |
| 31 | #include "media/sctp/sctp_transport.h" |
| 32 | #include "p2p/base/basic_async_resolver_factory.h" |
| 33 | #include "p2p/base/basic_packet_socket_factory.h" |
| 34 | #include "p2p/base/default_ice_transport_factory.h" |
| 35 | #include "p2p/client/basic_port_allocator.h" |
| 36 | #include "pc/audio_track.h" |
| 37 | #include "pc/local_audio_source.h" |
| 38 | #include "pc/media_stream.h" |
| 39 | #include "pc/peer_connection.h" |
| 40 | #include "pc/rtp_parameters_conversion.h" |
| 41 | #include "pc/video_track.h" |
| 42 | #include "rtc_base/bind.h" |
| 43 | #include "rtc_base/checks.h" |
| 44 | #include "rtc_base/experiments/field_trial_parser.h" |
| 45 | #include "rtc_base/experiments/field_trial_units.h" |
| 46 | #include "rtc_base/numerics/safe_conversions.h" |
| 47 | #include "rtc_base/system/file_wrapper.h" |
| 48 |
| 49 | namespace webrtc { |
| 50 |
| 51 | rtc::scoped_refptr<PeerConnectionFactoryInterface> |
| 52 | CreateModularPeerConnectionFactory( |
| 53 | PeerConnectionFactoryDependencies dependencies) { |
| 54 | rtc::scoped_refptr<PeerConnectionFactory> pc_factory( |
| 55 | new rtc::RefCountedObject<PeerConnectionFactory>( |
| 56 | std::move(dependencies))); |
| 57 | // Call Initialize synchronously but make sure it is executed on |
| 58 | // |signaling_thread|. |
| 59 | MethodCall<PeerConnectionFactory, bool> call( |
| 60 | pc_factory.get(), &PeerConnectionFactory::Initialize); |
| 61 | bool result = call.Marshal(RTC_FROM_HERE, pc_factory->signaling_thread()); |
| 62 |
| 63 | if (!result) { |
| 64 | return nullptr; |
| 65 | } |
| 66 | return PeerConnectionFactoryProxy::Create(pc_factory->signaling_thread(), |
| 67 | pc_factory); |
| 68 | } |
| 69 |
| 70 | PeerConnectionFactory::PeerConnectionFactory( |
| 71 | PeerConnectionFactoryDependencies dependencies) |
| 72 | : wraps_current_thread_(false), |
| 73 | network_thread_(dependencies.network_thread), |
| 74 | worker_thread_(dependencies.worker_thread), |
| 75 | signaling_thread_(dependencies.signaling_thread), |
| 76 | task_queue_factory_(std::move(dependencies.task_queue_factory)), |
| 77 | network_monitor_factory_(std::move(dependencies.network_monitor_factory)), |
| 78 | media_engine_(std::move(dependencies.media_engine)), |
| 79 | call_factory_(std::move(dependencies.call_factory)), |
| 80 | event_log_factory_(std::move(dependencies.event_log_factory)), |
| 81 | fec_controller_factory_(std::move(dependencies.fec_controller_factory)), |
| 82 | network_state_predictor_factory_( |
| 83 | std::move(dependencies.network_state_predictor_factory)), |
| 84 | injected_network_controller_factory_( |
| 85 | std::move(dependencies.network_controller_factory)), |
| 86 | neteq_factory_(std::move(dependencies.neteq_factory)), |
| 87 | trials_(dependencies.trials ? std::move(dependencies.trials) |
| 88 | : std::make_unique<FieldTrialBasedConfig>()) { |
| 89 | if (!network_thread_) { |
| 90 | owned_network_thread_ = rtc::Thread::CreateWithSocketServer(); |
| 91 | owned_network_thread_->SetName("pc_network_thread", nullptr); |
| 92 | owned_network_thread_->Start(); |
| 93 | network_thread_ = owned_network_thread_.get(); |
| 94 | } |
| 95 |
| 96 | if (!worker_thread_) { |
| 97 | owned_worker_thread_ = rtc::Thread::Create(); |
| 98 | owned_worker_thread_->SetName("pc_worker_thread", nullptr); |
| 99 | owned_worker_thread_->Start(); |
| 100 | worker_thread_ = owned_worker_thread_.get(); |
| 101 | } |
| 102 |
| 103 | if (!signaling_thread_) { |
| 104 | signaling_thread_ = rtc::Thread::Current(); |
| 105 | if (!signaling_thread_) { |
| 106 | // If this thread isn't already wrapped by an rtc::Thread, create a |
| 107 | // wrapper and own it in this class. |
| 108 | signaling_thread_ = rtc::ThreadManager::Instance()->WrapCurrentThread(); |
| 109 | wraps_current_thread_ = true; |
| 110 | } |
| 111 | } |
| 112 | signaling_thread_->AllowInvokesToThread(worker_thread_); |
| 113 | signaling_thread_->AllowInvokesToThread(network_thread_); |
| 114 | worker_thread_->AllowInvokesToThread(network_thread_); |
| 115 | network_thread_->DisallowAllInvokes(); |
| 116 | } |
| 117 |
| 118 | PeerConnectionFactory::~PeerConnectionFactory() { |
| 119 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 120 | channel_manager_.reset(nullptr); |
| 121 |
| 122 | // Make sure |worker_thread_| and |signaling_thread_| outlive |
| 123 | // |default_socket_factory_| and |default_network_manager_|. |
| 124 | default_socket_factory_ = nullptr; |
| 125 | default_network_manager_ = nullptr; |
| 126 |
| 127 | if (wraps_current_thread_) |
| 128 | rtc::ThreadManager::Instance()->UnwrapCurrentThread(); |
| 129 | } |
| 130 |
| 131 | bool PeerConnectionFactory::Initialize() { |
| 132 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 133 | rtc::InitRandom(rtc::Time32()); |
| 134 |
| 135 | // If network_monitor_factory_ is non-null, it will be used to create a |
| 136 | // network monitor while on the network thread. |
| 137 | default_network_manager_.reset( |
| 138 | new rtc::BasicNetworkManager(network_monitor_factory_.get())); |
| 139 | if (!default_network_manager_) { |
| 140 | return false; |
| 141 | } |
| 142 |
| 143 | default_socket_factory_.reset( |
| 144 | new rtc::BasicPacketSocketFactory(network_thread_)); |
| 145 | if (!default_socket_factory_) { |
| 146 | return false; |
| 147 | } |
| 148 |
| 149 | channel_manager_ = std::make_unique<cricket::ChannelManager>( |
| 150 | std::move(media_engine_), std::make_unique<cricket::RtpDataEngine>(), |
| 151 | worker_thread_, network_thread_); |
| 152 |
| 153 | channel_manager_->SetVideoRtxEnabled(true); |
| 154 | if (!channel_manager_->Init()) { |
| 155 | return false; |
| 156 | } |
| 157 |
| 158 | return true; |
| 159 | } |
| 160 |
| 161 | void PeerConnectionFactory::SetOptions(const Options& options) { |
| 162 | options_ = options; |
| 163 | } |
| 164 |
| 165 | RtpCapabilities PeerConnectionFactory::GetRtpSenderCapabilities( |
| 166 | cricket::MediaType kind) const { |
| 167 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 168 | switch (kind) { |
| 169 | case cricket::MEDIA_TYPE_AUDIO: { |
| 170 | cricket::AudioCodecs cricket_codecs; |
| 171 | channel_manager_->GetSupportedAudioSendCodecs(&cricket_codecs); |
| 172 | return ToRtpCapabilities( |
| 173 | cricket_codecs, |
| 174 | channel_manager_->GetDefaultEnabledAudioRtpHeaderExtensions()); |
| 175 | } |
| 176 | case cricket::MEDIA_TYPE_VIDEO: { |
| 177 | cricket::VideoCodecs cricket_codecs; |
| 178 | channel_manager_->GetSupportedVideoSendCodecs(&cricket_codecs); |
| 179 | return ToRtpCapabilities( |
| 180 | cricket_codecs, |
| 181 | channel_manager_->GetDefaultEnabledVideoRtpHeaderExtensions()); |
| 182 | } |
| 183 | case cricket::MEDIA_TYPE_DATA: |
| 184 | return RtpCapabilities(); |
| 185 | } |
| 186 | // Not reached; avoids compile warning. |
| 187 | FATAL(); |
| 188 | } |
| 189 |
| 190 | RtpCapabilities PeerConnectionFactory::GetRtpReceiverCapabilities( |
| 191 | cricket::MediaType kind) const { |
| 192 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 193 | switch (kind) { |
| 194 | case cricket::MEDIA_TYPE_AUDIO: { |
| 195 | cricket::AudioCodecs cricket_codecs; |
| 196 | channel_manager_->GetSupportedAudioReceiveCodecs(&cricket_codecs); |
| 197 | return ToRtpCapabilities( |
| 198 | cricket_codecs, |
| 199 | channel_manager_->GetDefaultEnabledAudioRtpHeaderExtensions()); |
| 200 | } |
| 201 | case cricket::MEDIA_TYPE_VIDEO: { |
| 202 | cricket::VideoCodecs cricket_codecs; |
| 203 | channel_manager_->GetSupportedVideoReceiveCodecs(&cricket_codecs); |
| 204 | return ToRtpCapabilities( |
| 205 | cricket_codecs, |
| 206 | channel_manager_->GetDefaultEnabledVideoRtpHeaderExtensions()); |
| 207 | } |
| 208 | case cricket::MEDIA_TYPE_DATA: |
| 209 | return RtpCapabilities(); |
| 210 | } |
| 211 | // Not reached; avoids compile warning. |
| 212 | FATAL(); |
| 213 | } |
| 214 |
| 215 | rtc::scoped_refptr<AudioSourceInterface> |
| 216 | PeerConnectionFactory::CreateAudioSource(const cricket::AudioOptions& options) { |
| 217 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 218 | rtc::scoped_refptr<LocalAudioSource> source( |
| 219 | LocalAudioSource::Create(&options)); |
| 220 | return source; |
| 221 | } |
| 222 |
| 223 | bool PeerConnectionFactory::StartAecDump(FILE* file, int64_t max_size_bytes) { |
| 224 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 225 | return channel_manager_->StartAecDump(FileWrapper(file), max_size_bytes); |
| 226 | } |
| 227 |
| 228 | void PeerConnectionFactory::StopAecDump() { |
| 229 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 230 | channel_manager_->StopAecDump(); |
| 231 | } |
| 232 |
| 233 | rtc::scoped_refptr<PeerConnectionInterface> |
| 234 | PeerConnectionFactory::CreatePeerConnection( |
| 235 | const PeerConnectionInterface::RTCConfiguration& configuration, |
| 236 | std::unique_ptr<cricket::PortAllocator> allocator, |
| 237 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, |
| 238 | PeerConnectionObserver* observer) { |
| 239 | // Convert the legacy API into the new dependency structure. |
| 240 | PeerConnectionDependencies dependencies(observer); |
| 241 | dependencies.allocator = std::move(allocator); |
| 242 | dependencies.cert_generator = std::move(cert_generator); |
| 243 | // Pass that into the new API. |
| 244 | return CreatePeerConnection(configuration, std::move(dependencies)); |
| 245 | } |
| 246 |
| 247 | rtc::scoped_refptr<PeerConnectionInterface> |
| 248 | PeerConnectionFactory::CreatePeerConnection( |
| 249 | const PeerConnectionInterface::RTCConfiguration& configuration, |
| 250 | PeerConnectionDependencies dependencies) { |
| 251 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 252 | RTC_DCHECK(!(dependencies.allocator && dependencies.packet_socket_factory)) |
| 253 | << "You can't set both allocator and packet_socket_factory; " |
| 254 | "the former is going away (see bugs.webrtc.org/7447"; |
| 255 |
| 256 | // Set internal defaults if optional dependencies are not set. |
| 257 | if (!dependencies.cert_generator) { |
| 258 | dependencies.cert_generator = |
| 259 | std::make_unique<rtc::RTCCertificateGenerator>(signaling_thread_, |
| 260 | network_thread_); |
| 261 | } |
| 262 | if (!dependencies.allocator) { |
| 263 | rtc::PacketSocketFactory* packet_socket_factory; |
| 264 | if (dependencies.packet_socket_factory) |
| 265 | packet_socket_factory = dependencies.packet_socket_factory.get(); |
| 266 | else |
| 267 | packet_socket_factory = default_socket_factory_.get(); |
| 268 |
| 269 | dependencies.allocator = std::make_unique<cricket::BasicPortAllocator>( |
| 270 | default_network_manager_.get(), packet_socket_factory, |
| 271 | configuration.turn_customizer); |
| 272 | } |
| 273 |
| 274 | if (!dependencies.async_resolver_factory) { |
| 275 | dependencies.async_resolver_factory = |
| 276 | std::make_unique<webrtc::BasicAsyncResolverFactory>(); |
| 277 | } |
| 278 |
| 279 | if (!dependencies.ice_transport_factory) { |
| 280 | dependencies.ice_transport_factory = |
| 281 | std::make_unique<DefaultIceTransportFactory>(); |
| 282 | } |
| 283 |
| 284 | dependencies.allocator->SetNetworkIgnoreMask(options_.network_ignore_mask); |
| 285 |
| 286 | std::unique_ptr<RtcEventLog> event_log = |
| 287 | worker_thread_->Invoke<std::unique_ptr<RtcEventLog>>( |
| 288 | RTC_FROM_HERE, |
| 289 | rtc::Bind(&PeerConnectionFactory::CreateRtcEventLog_w, this)); |
| 290 |
| 291 | std::unique_ptr<Call> call = worker_thread_->Invoke<std::unique_ptr<Call>>( |
| 292 | RTC_FROM_HERE, |
| 293 | rtc::Bind(&PeerConnectionFactory::CreateCall_w, this, event_log.get())); |
| 294 |
| 295 | rtc::scoped_refptr<PeerConnection> pc( |
| 296 | new rtc::RefCountedObject<PeerConnection>(this, std::move(event_log), |
| 297 | std::move(call))); |
| 298 | ActionsBeforeInitializeForTesting(pc); |
| 299 | if (!pc->Initialize(configuration, std::move(dependencies))) { |
| 300 | return nullptr; |
| 301 | } |
| 302 | return PeerConnectionProxy::Create(signaling_thread(), pc); |
| 303 | } |
| 304 |
| 305 | rtc::scoped_refptr<MediaStreamInterface> |
| 306 | PeerConnectionFactory::CreateLocalMediaStream(const std::string& stream_id) { |
| 307 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 308 | return MediaStreamProxy::Create(signaling_thread_, |
| 309 | MediaStream::Create(stream_id)); |
| 310 | } |
| 311 |
| 312 | rtc::scoped_refptr<VideoTrackInterface> PeerConnectionFactory::CreateVideoTrack( |
| 313 | const std::string& id, |
| 314 | VideoTrackSourceInterface* source) { |
| 315 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 316 | rtc::scoped_refptr<VideoTrackInterface> track( |
| 317 | VideoTrack::Create(id, source, worker_thread_)); |
| 318 | return VideoTrackProxy::Create(signaling_thread_, worker_thread_, track); |
| 319 | } |
| 320 |
| 321 | rtc::scoped_refptr<AudioTrackInterface> PeerConnectionFactory::CreateAudioTrack( |
| 322 | const std::string& id, |
| 323 | AudioSourceInterface* source) { |
| 324 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 325 | rtc::scoped_refptr<AudioTrackInterface> track(AudioTrack::Create(id, source)); |
| 326 | return AudioTrackProxy::Create(signaling_thread_, track); |
| 327 | } |
| 328 |
| 329 | std::unique_ptr<cricket::SctpTransportInternalFactory> |
| 330 | PeerConnectionFactory::CreateSctpTransportInternalFactory() { |
| 331 | #ifdef HAVE_SCTP |
| 332 | return std::make_unique<cricket::SctpTransportFactory>(network_thread()); |
| 333 | #else |
| 334 | return nullptr; |
| 335 | #endif |
| 336 | } |
| 337 |
| 338 | cricket::ChannelManager* PeerConnectionFactory::channel_manager() { |
| 339 | return channel_manager_.get(); |
| 340 | } |
| 341 |
| 342 | std::unique_ptr<RtcEventLog> PeerConnectionFactory::CreateRtcEventLog_w() { |
| 343 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 344 |
| 345 | auto encoding_type = RtcEventLog::EncodingType::Legacy; |
| 346 | if (IsTrialEnabled("WebRTC-RtcEventLogNewFormat")) |
| 347 | encoding_type = RtcEventLog::EncodingType::NewFormat; |
| 348 | return event_log_factory_ |
| 349 | ? event_log_factory_->CreateRtcEventLog(encoding_type) |
| 350 | : std::make_unique<RtcEventLogNull>(); |
| 351 | } |
| 352 |
| 353 | std::unique_ptr<Call> PeerConnectionFactory::CreateCall_w( |
| 354 | RtcEventLog* event_log) { |
| 355 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 356 |
| 357 | webrtc::Call::Config call_config(event_log); |
| 358 | if (!channel_manager_->media_engine() || !call_factory_) { |
| 359 | return nullptr; |
| 360 | } |
| 361 | call_config.audio_state = |
| 362 | channel_manager_->media_engine()->voice().GetAudioState(); |
| 363 |
| 364 | FieldTrialParameter<DataRate> min_bandwidth("min", |
| 365 | DataRate::KilobitsPerSec(30)); |
| 366 | FieldTrialParameter<DataRate> start_bandwidth("start", |
| 367 | DataRate::KilobitsPerSec(300)); |
| 368 | FieldTrialParameter<DataRate> max_bandwidth("max", |
| 369 | DataRate::KilobitsPerSec(2000)); |
| 370 | ParseFieldTrial({&min_bandwidth, &start_bandwidth, &max_bandwidth}, |
| 371 | trials_->Lookup("WebRTC-PcFactoryDefaultBitrates")); |
| 372 |
| 373 | call_config.bitrate_config.min_bitrate_bps = |
| 374 | rtc::saturated_cast<int>(min_bandwidth->bps()); |
| 375 | call_config.bitrate_config.start_bitrate_bps = |
| 376 | rtc::saturated_cast<int>(start_bandwidth->bps()); |
| 377 | call_config.bitrate_config.max_bitrate_bps = |
| 378 | rtc::saturated_cast<int>(max_bandwidth->bps()); |
| 379 |
| 380 | call_config.fec_controller_factory = fec_controller_factory_.get(); |
| 381 | call_config.task_queue_factory = task_queue_factory_.get(); |
| 382 | call_config.network_state_predictor_factory = |
| 383 | network_state_predictor_factory_.get(); |
| 384 | call_config.neteq_factory = neteq_factory_.get(); |
| 385 |
| 386 | if (IsTrialEnabled("WebRTC-Bwe-InjectedCongestionController")) { |
| 387 | RTC_LOG(LS_INFO) << "Using injected network controller factory"; |
| 388 | call_config.network_controller_factory = |
| 389 | injected_network_controller_factory_.get(); |
| 390 | } else { |
| 391 | RTC_LOG(LS_INFO) << "Using default network controller factory"; |
| 392 | } |
| 393 |
| 394 | call_config.trials = trials_.get(); |
| 395 |
| 396 | return std::unique_ptr<Call>(call_factory_->CreateCall(call_config)); |
| 397 | } |
| 398 |
| 399 | bool PeerConnectionFactory::IsTrialEnabled(absl::string_view key) const { |
| 400 | RTC_DCHECK(trials_); |
| 401 | return absl::StartsWith(trials_->Lookup(key), "Enabled"); |
| 402 | } |
| 403 |
| 404 | } // namespace webrtc |
| 405 |