henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2004 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "pc/peer_connection_factory.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
Ying Wang | 0810a7c | 2019-04-10 13:48:24 +0200 | [diff] [blame] | 13 | #include <memory> |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 14 | #include <utility> |
Florent Castelli | 72b751a | 2018-06-28 14:09:33 +0200 | [diff] [blame] | 15 | #include <vector> |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 16 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 17 | #include "absl/memory/memory.h" |
Ying Wang | 0dd1b0a | 2018-02-20 12:50:27 +0100 | [diff] [blame] | 18 | #include "api/fec_controller.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "api/media_stream_proxy.h" |
| 20 | #include "api/media_stream_track_proxy.h" |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 21 | #include "api/media_transport_interface.h" |
Ying Wang | 0810a7c | 2019-04-10 13:48:24 +0200 | [diff] [blame] | 22 | #include "api/network_state_predictor.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "api/peer_connection_factory_proxy.h" |
| 24 | #include "api/peer_connection_proxy.h" |
Danil Chapovalov | 83bbe91 | 2019-08-07 12:24:53 +0200 | [diff] [blame] | 25 | #include "api/rtc_event_log/rtc_event_log.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 26 | #include "api/turn_customizer.h" |
Elad Alon | 9cfdb20 | 2019-08-13 10:48:48 +0200 | [diff] [blame] | 27 | #include "api/units/data_rate.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 28 | #include "api/video_track_source_proxy.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 29 | #include "media/base/rtp_data_engine.h" |
| 30 | #include "media/sctp/sctp_transport.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 31 | #include "p2p/base/basic_packet_socket_factory.h" |
| 32 | #include "p2p/client/basic_port_allocator.h" |
| 33 | #include "pc/audio_track.h" |
| 34 | #include "pc/local_audio_source.h" |
| 35 | #include "pc/media_stream.h" |
| 36 | #include "pc/peer_connection.h" |
Ying Wang | 0810a7c | 2019-04-10 13:48:24 +0200 | [diff] [blame] | 37 | #include "pc/rtp_parameters_conversion.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 38 | #include "pc/video_track.h" |
Ying Wang | 0810a7c | 2019-04-10 13:48:24 +0200 | [diff] [blame] | 39 | #include "rtc_base/bind.h" |
| 40 | #include "rtc_base/checks.h" |
Elad Alon | 9cfdb20 | 2019-08-13 10:48:48 +0200 | [diff] [blame] | 41 | #include "rtc_base/experiments/field_trial_parser.h" |
| 42 | #include "rtc_base/experiments/field_trial_units.h" |
| 43 | #include "rtc_base/numerics/safe_conversions.h" |
Niels Möller | e8e4dc4 | 2019-06-11 14:04:16 +0200 | [diff] [blame] | 44 | #include "rtc_base/system/file_wrapper.h" |
Bjorn Terelius | b26cf2f | 2018-10-26 20:39:33 +0200 | [diff] [blame] | 45 | #include "system_wrappers/include/field_trial.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 46 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 47 | namespace webrtc { |
| 48 | |
kwiberg | 1e4e8cb | 2017-01-31 01:48:08 -0800 | [diff] [blame] | 49 | rtc::scoped_refptr<PeerConnectionFactoryInterface> |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 50 | CreateModularPeerConnectionFactory( |
Benjamin Wright | 5234a49 | 2018-05-29 15:04:32 -0700 | [diff] [blame] | 51 | PeerConnectionFactoryDependencies dependencies) { |
gyzhou | 95aa964 | 2016-12-13 14:06:26 -0800 | [diff] [blame] | 52 | rtc::scoped_refptr<PeerConnectionFactory> pc_factory( |
| 53 | new rtc::RefCountedObject<PeerConnectionFactory>( |
Benjamin Wright | 5234a49 | 2018-05-29 15:04:32 -0700 | [diff] [blame] | 54 | std::move(dependencies))); |
gyzhou | 95aa964 | 2016-12-13 14:06:26 -0800 | [diff] [blame] | 55 | // Call Initialize synchronously but make sure it is executed on |
| 56 | // |signaling_thread|. |
| 57 | MethodCall0<PeerConnectionFactory, bool> call( |
| 58 | pc_factory.get(), &PeerConnectionFactory::Initialize); |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 59 | bool result = call.Marshal(RTC_FROM_HERE, pc_factory->signaling_thread()); |
gyzhou | 95aa964 | 2016-12-13 14:06:26 -0800 | [diff] [blame] | 60 | |
| 61 | if (!result) { |
| 62 | return nullptr; |
| 63 | } |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 64 | return PeerConnectionFactoryProxy::Create(pc_factory->signaling_thread(), |
| 65 | pc_factory); |
kwiberg | 1e4e8cb | 2017-01-31 01:48:08 -0800 | [diff] [blame] | 66 | } |
| 67 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 68 | PeerConnectionFactory::PeerConnectionFactory( |
Danil Chapovalov | f5258be | 2019-03-19 17:45:24 +0100 | [diff] [blame] | 69 | PeerConnectionFactoryDependencies dependencies) |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 70 | : wraps_current_thread_(false), |
Danil Chapovalov | f5258be | 2019-03-19 17:45:24 +0100 | [diff] [blame] | 71 | network_thread_(dependencies.network_thread), |
| 72 | worker_thread_(dependencies.worker_thread), |
| 73 | signaling_thread_(dependencies.signaling_thread), |
Danil Chapovalov | 9435c61 | 2019-04-01 10:33:16 +0200 | [diff] [blame] | 74 | task_queue_factory_(std::move(dependencies.task_queue_factory)), |
Danil Chapovalov | f5258be | 2019-03-19 17:45:24 +0100 | [diff] [blame] | 75 | media_engine_(std::move(dependencies.media_engine)), |
| 76 | call_factory_(std::move(dependencies.call_factory)), |
| 77 | event_log_factory_(std::move(dependencies.event_log_factory)), |
| 78 | fec_controller_factory_(std::move(dependencies.fec_controller_factory)), |
Ying Wang | 0810a7c | 2019-04-10 13:48:24 +0200 | [diff] [blame] | 79 | network_state_predictor_factory_( |
| 80 | std::move(dependencies.network_state_predictor_factory)), |
Sebastian Jansson | dfce03a | 2018-05-18 18:05:10 +0200 | [diff] [blame] | 81 | injected_network_controller_factory_( |
Danil Chapovalov | f5258be | 2019-03-19 17:45:24 +0100 | [diff] [blame] | 82 | std::move(dependencies.network_controller_factory)), |
| 83 | media_transport_factory_( |
| 84 | std::move(dependencies.media_transport_factory)) { |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 85 | if (!network_thread_) { |
| 86 | owned_network_thread_ = rtc::Thread::CreateWithSocketServer(); |
Sebastian Jansson | 13f35ec | 2017-11-13 10:54:45 +0100 | [diff] [blame] | 87 | owned_network_thread_->SetName("pc_network_thread", nullptr); |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 88 | owned_network_thread_->Start(); |
| 89 | network_thread_ = owned_network_thread_.get(); |
| 90 | } |
| 91 | |
| 92 | if (!worker_thread_) { |
| 93 | owned_worker_thread_ = rtc::Thread::Create(); |
Sebastian Jansson | 13f35ec | 2017-11-13 10:54:45 +0100 | [diff] [blame] | 94 | owned_worker_thread_->SetName("pc_worker_thread", nullptr); |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 95 | owned_worker_thread_->Start(); |
| 96 | worker_thread_ = owned_worker_thread_.get(); |
| 97 | } |
| 98 | |
| 99 | if (!signaling_thread_) { |
| 100 | signaling_thread_ = rtc::Thread::Current(); |
| 101 | if (!signaling_thread_) { |
| 102 | // If this thread isn't already wrapped by an rtc::Thread, create a |
| 103 | // wrapper and own it in this class. |
| 104 | signaling_thread_ = rtc::ThreadManager::Instance()->WrapCurrentThread(); |
| 105 | wraps_current_thread_ = true; |
| 106 | } |
| 107 | } |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 09:43:21 -0700 | [diff] [blame] | 108 | } |
Benjamin Wright | 5234a49 | 2018-05-29 15:04:32 -0700 | [diff] [blame] | 109 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 110 | PeerConnectionFactory::~PeerConnectionFactory() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 111 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 112 | channel_manager_.reset(nullptr); |
jiayl@webrtc.org | d83f4ef | 2015-03-13 21:26:12 +0000 | [diff] [blame] | 113 | |
| 114 | // Make sure |worker_thread_| and |signaling_thread_| outlive |
Henrik Boström | cebf0a2 | 2016-06-01 15:45:30 +0200 | [diff] [blame] | 115 | // |default_socket_factory_| and |default_network_manager_|. |
deadbeef | 41b0798 | 2015-12-01 15:01:24 -0800 | [diff] [blame] | 116 | default_socket_factory_ = nullptr; |
| 117 | default_network_manager_ = nullptr; |
jiayl@webrtc.org | d83f4ef | 2015-03-13 21:26:12 +0000 | [diff] [blame] | 118 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 119 | if (wraps_current_thread_) |
| 120 | rtc::ThreadManager::Instance()->UnwrapCurrentThread(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | bool PeerConnectionFactory::Initialize() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 124 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 125 | rtc::InitRandom(rtc::Time32()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 126 | |
deadbeef | 41b0798 | 2015-12-01 15:01:24 -0800 | [diff] [blame] | 127 | default_network_manager_.reset(new rtc::BasicNetworkManager()); |
| 128 | if (!default_network_manager_) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | default_socket_factory_.reset( |
danilchap | e9021a3 | 2016-05-17 01:52:02 -0700 | [diff] [blame] | 133 | new rtc::BasicPacketSocketFactory(network_thread_)); |
deadbeef | 41b0798 | 2015-12-01 15:01:24 -0800 | [diff] [blame] | 134 | if (!default_socket_factory_) { |
| 135 | return false; |
| 136 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 137 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 138 | channel_manager_ = absl::make_unique<cricket::ChannelManager>( |
| 139 | std::move(media_engine_), absl::make_unique<cricket::RtpDataEngine>(), |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 140 | worker_thread_, network_thread_); |
henrika@webrtc.org | 62f6e75 | 2015-02-11 08:38:35 +0000 | [diff] [blame] | 141 | |
stefan@webrtc.org | 85d2794 | 2014-06-09 12:51:39 +0000 | [diff] [blame] | 142 | channel_manager_->SetVideoRtxEnabled(true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 143 | if (!channel_manager_->Init()) { |
| 144 | return false; |
| 145 | } |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 146 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 147 | return true; |
| 148 | } |
| 149 | |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 150 | void PeerConnectionFactory::SetOptions(const Options& options) { |
| 151 | options_ = options; |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Florent Castelli | 72b751a | 2018-06-28 14:09:33 +0200 | [diff] [blame] | 154 | RtpCapabilities PeerConnectionFactory::GetRtpSenderCapabilities( |
| 155 | cricket::MediaType kind) const { |
| 156 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 157 | switch (kind) { |
| 158 | case cricket::MEDIA_TYPE_AUDIO: { |
| 159 | cricket::AudioCodecs cricket_codecs; |
| 160 | cricket::RtpHeaderExtensions cricket_extensions; |
| 161 | channel_manager_->GetSupportedAudioSendCodecs(&cricket_codecs); |
| 162 | channel_manager_->GetSupportedAudioRtpHeaderExtensions( |
| 163 | &cricket_extensions); |
| 164 | return ToRtpCapabilities(cricket_codecs, cricket_extensions); |
| 165 | } |
| 166 | case cricket::MEDIA_TYPE_VIDEO: { |
| 167 | cricket::VideoCodecs cricket_codecs; |
| 168 | cricket::RtpHeaderExtensions cricket_extensions; |
| 169 | channel_manager_->GetSupportedVideoCodecs(&cricket_codecs); |
| 170 | channel_manager_->GetSupportedVideoRtpHeaderExtensions( |
| 171 | &cricket_extensions); |
| 172 | return ToRtpCapabilities(cricket_codecs, cricket_extensions); |
| 173 | } |
| 174 | case cricket::MEDIA_TYPE_DATA: |
| 175 | return RtpCapabilities(); |
| 176 | } |
| 177 | // Not reached; avoids compile warning. |
| 178 | FATAL(); |
| 179 | } |
| 180 | |
| 181 | RtpCapabilities PeerConnectionFactory::GetRtpReceiverCapabilities( |
| 182 | cricket::MediaType kind) const { |
| 183 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 184 | switch (kind) { |
| 185 | case cricket::MEDIA_TYPE_AUDIO: { |
| 186 | cricket::AudioCodecs cricket_codecs; |
| 187 | cricket::RtpHeaderExtensions cricket_extensions; |
| 188 | channel_manager_->GetSupportedAudioReceiveCodecs(&cricket_codecs); |
| 189 | channel_manager_->GetSupportedAudioRtpHeaderExtensions( |
| 190 | &cricket_extensions); |
| 191 | return ToRtpCapabilities(cricket_codecs, cricket_extensions); |
| 192 | } |
| 193 | case cricket::MEDIA_TYPE_VIDEO: { |
| 194 | cricket::VideoCodecs cricket_codecs; |
| 195 | cricket::RtpHeaderExtensions cricket_extensions; |
| 196 | channel_manager_->GetSupportedVideoCodecs(&cricket_codecs); |
| 197 | channel_manager_->GetSupportedVideoRtpHeaderExtensions( |
| 198 | &cricket_extensions); |
| 199 | return ToRtpCapabilities(cricket_codecs, cricket_extensions); |
| 200 | } |
| 201 | case cricket::MEDIA_TYPE_DATA: |
| 202 | return RtpCapabilities(); |
| 203 | } |
| 204 | // Not reached; avoids compile warning. |
| 205 | FATAL(); |
| 206 | } |
| 207 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 208 | rtc::scoped_refptr<AudioSourceInterface> |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 209 | PeerConnectionFactory::CreateAudioSource(const cricket::AudioOptions& options) { |
| 210 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 211 | rtc::scoped_refptr<LocalAudioSource> source( |
deadbeef | 757146b | 2017-02-10 21:26:48 -0800 | [diff] [blame] | 212 | LocalAudioSource::Create(&options)); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 213 | return source; |
| 214 | } |
| 215 | |
Niels Möller | e8e4dc4 | 2019-06-11 14:04:16 +0200 | [diff] [blame] | 216 | bool PeerConnectionFactory::StartAecDump(FILE* file, int64_t max_size_bytes) { |
| 217 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 218 | return channel_manager_->StartAecDump(FileWrapper(file), max_size_bytes); |
| 219 | } |
| 220 | |
ivoc | 797ef12 | 2015-10-22 03:25:41 -0700 | [diff] [blame] | 221 | void PeerConnectionFactory::StopAecDump() { |
| 222 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
| 223 | channel_manager_->StopAecDump(); |
| 224 | } |
| 225 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 226 | rtc::scoped_refptr<PeerConnectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 227 | PeerConnectionFactory::CreatePeerConnection( |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 228 | const PeerConnectionInterface::RTCConfiguration& configuration, |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 229 | std::unique_ptr<cricket::PortAllocator> allocator, |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 230 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 231 | PeerConnectionObserver* observer) { |
Patrik Höglund | 662e31f | 2019-09-05 14:35:04 +0200 | [diff] [blame^] | 232 | // Convert the legacy API into the new dependency structure. |
Benjamin Wright | 6f7e6d6 | 2018-05-02 13:46:31 -0700 | [diff] [blame] | 233 | PeerConnectionDependencies dependencies(observer); |
| 234 | dependencies.allocator = std::move(allocator); |
| 235 | dependencies.cert_generator = std::move(cert_generator); |
| 236 | // Pass that into the new API. |
| 237 | return CreatePeerConnection(configuration, std::move(dependencies)); |
| 238 | } |
| 239 | |
| 240 | rtc::scoped_refptr<PeerConnectionInterface> |
| 241 | PeerConnectionFactory::CreatePeerConnection( |
| 242 | const PeerConnectionInterface::RTCConfiguration& configuration, |
| 243 | PeerConnectionDependencies dependencies) { |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 244 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
Patrik Höglund | 662e31f | 2019-09-05 14:35:04 +0200 | [diff] [blame^] | 245 | RTC_DCHECK(!(dependencies.allocator && dependencies.packet_socket_factory)) |
| 246 | << "You can't set both allocator and packet_socket_factory; " |
| 247 | "the former is going away (see bugs.webrtc.org/7447"; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 248 | |
Benjamin Wright | 6f7e6d6 | 2018-05-02 13:46:31 -0700 | [diff] [blame] | 249 | // Set internal defaults if optional dependencies are not set. |
| 250 | if (!dependencies.cert_generator) { |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 251 | dependencies.cert_generator = |
| 252 | absl::make_unique<rtc::RTCCertificateGenerator>(signaling_thread_, |
| 253 | network_thread_); |
deadbeef | 41b0798 | 2015-12-01 15:01:24 -0800 | [diff] [blame] | 254 | } |
Benjamin Wright | 6f7e6d6 | 2018-05-02 13:46:31 -0700 | [diff] [blame] | 255 | if (!dependencies.allocator) { |
Patrik Höglund | 662e31f | 2019-09-05 14:35:04 +0200 | [diff] [blame^] | 256 | rtc::PacketSocketFactory* packet_socket_factory; |
| 257 | if (dependencies.packet_socket_factory) |
| 258 | packet_socket_factory = dependencies.packet_socket_factory.get(); |
| 259 | else |
| 260 | packet_socket_factory = default_socket_factory_.get(); |
| 261 | |
Qingsi Wang | 7852d29 | 2018-10-31 11:17:07 -0700 | [diff] [blame] | 262 | network_thread_->Invoke<void>(RTC_FROM_HERE, [this, &configuration, |
Patrik Höglund | 662e31f | 2019-09-05 14:35:04 +0200 | [diff] [blame^] | 263 | &dependencies, |
| 264 | &packet_socket_factory]() { |
Qingsi Wang | 7852d29 | 2018-10-31 11:17:07 -0700 | [diff] [blame] | 265 | dependencies.allocator = absl::make_unique<cricket::BasicPortAllocator>( |
Patrik Höglund | 662e31f | 2019-09-05 14:35:04 +0200 | [diff] [blame^] | 266 | default_network_manager_.get(), packet_socket_factory, |
Qingsi Wang | 7852d29 | 2018-10-31 11:17:07 -0700 | [diff] [blame] | 267 | configuration.turn_customizer); |
| 268 | }); |
jonaso | c251cb1 | 2017-08-29 03:20:58 -0700 | [diff] [blame] | 269 | } |
Benjamin Wright | 6f7e6d6 | 2018-05-02 13:46:31 -0700 | [diff] [blame] | 270 | |
Zach Stein | e20867f | 2018-08-02 13:20:15 -0700 | [diff] [blame] | 271 | // TODO(zstein): Once chromium injects its own AsyncResolverFactory, set |
| 272 | // |dependencies.async_resolver_factory| to a new |
| 273 | // |rtc::BasicAsyncResolverFactory| if no factory is provided. |
| 274 | |
jonaso | c251cb1 | 2017-08-29 03:20:58 -0700 | [diff] [blame] | 275 | network_thread_->Invoke<void>( |
Benjamin Wright | 6f7e6d6 | 2018-05-02 13:46:31 -0700 | [diff] [blame] | 276 | RTC_FROM_HERE, |
| 277 | rtc::Bind(&cricket::PortAllocator::SetNetworkIgnoreMask, |
| 278 | dependencies.allocator.get(), options_.network_ignore_mask)); |
jonaso | c251cb1 | 2017-08-29 03:20:58 -0700 | [diff] [blame] | 279 | |
eladalon | 393a9f6 | 2017-09-05 04:30:30 -0700 | [diff] [blame] | 280 | std::unique_ptr<RtcEventLog> event_log = |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 281 | worker_thread_->Invoke<std::unique_ptr<RtcEventLog>>( |
| 282 | RTC_FROM_HERE, |
| 283 | rtc::Bind(&PeerConnectionFactory::CreateRtcEventLog_w, this)); |
maxmorin | e9ef907 | 2017-08-29 04:49:00 -0700 | [diff] [blame] | 284 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 285 | std::unique_ptr<Call> call = worker_thread_->Invoke<std::unique_ptr<Call>>( |
| 286 | RTC_FROM_HERE, |
| 287 | rtc::Bind(&PeerConnectionFactory::CreateCall_w, this, event_log.get())); |
| 288 | |
deadbeef | 41b0798 | 2015-12-01 15:01:24 -0800 | [diff] [blame] | 289 | rtc::scoped_refptr<PeerConnection> pc( |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 290 | new rtc::RefCountedObject<PeerConnection>(this, std::move(event_log), |
| 291 | std::move(call))); |
Harald Alvestrand | 1979384 | 2018-06-25 12:03:50 +0200 | [diff] [blame] | 292 | ActionsBeforeInitializeForTesting(pc); |
Benjamin Wright | cab5888 | 2018-05-02 15:12:47 -0700 | [diff] [blame] | 293 | if (!pc->Initialize(configuration, std::move(dependencies))) { |
deadbeef | 41b0798 | 2015-12-01 15:01:24 -0800 | [diff] [blame] | 294 | return nullptr; |
| 295 | } |
| 296 | return PeerConnectionProxy::Create(signaling_thread(), pc); |
| 297 | } |
| 298 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 299 | rtc::scoped_refptr<MediaStreamInterface> |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 300 | PeerConnectionFactory::CreateLocalMediaStream(const std::string& stream_id) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 301 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 302 | return MediaStreamProxy::Create(signaling_thread_, |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 303 | MediaStream::Create(stream_id)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 304 | } |
| 305 | |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 306 | rtc::scoped_refptr<VideoTrackInterface> PeerConnectionFactory::CreateVideoTrack( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 307 | const std::string& id, |
perkj | a3ede6c | 2016-03-08 01:27:48 +0100 | [diff] [blame] | 308 | VideoTrackSourceInterface* source) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 309 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 310 | rtc::scoped_refptr<VideoTrackInterface> track( |
perkj | 773be36 | 2017-07-31 23:22:01 -0700 | [diff] [blame] | 311 | VideoTrack::Create(id, source, worker_thread_)); |
nisse | 5b68ab5 | 2016-04-07 07:45:54 -0700 | [diff] [blame] | 312 | return VideoTrackProxy::Create(signaling_thread_, worker_thread_, track); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 315 | rtc::scoped_refptr<AudioTrackInterface> PeerConnectionFactory::CreateAudioTrack( |
| 316 | const std::string& id, |
| 317 | AudioSourceInterface* source) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 318 | RTC_DCHECK(signaling_thread_->IsCurrent()); |
tommi | 6eca7e3 | 2015-12-15 04:27:11 -0800 | [diff] [blame] | 319 | rtc::scoped_refptr<AudioTrackInterface> track(AudioTrack::Create(id, source)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 320 | return AudioTrackProxy::Create(signaling_thread_, track); |
| 321 | } |
| 322 | |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 323 | std::unique_ptr<cricket::SctpTransportInternalFactory> |
| 324 | PeerConnectionFactory::CreateSctpTransportInternalFactory() { |
| 325 | #ifdef HAVE_SCTP |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 326 | return absl::make_unique<cricket::SctpTransportFactory>(network_thread()); |
Steve Anton | da6c095 | 2017-10-23 11:41:54 -0700 | [diff] [blame] | 327 | #else |
| 328 | return nullptr; |
| 329 | #endif |
| 330 | } |
| 331 | |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 332 | cricket::ChannelManager* PeerConnectionFactory::channel_manager() { |
| 333 | return channel_manager_.get(); |
| 334 | } |
| 335 | |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 336 | std::unique_ptr<RtcEventLog> PeerConnectionFactory::CreateRtcEventLog_w() { |
eladalon | 591753b | 2017-09-06 12:33:43 -0700 | [diff] [blame] | 337 | RTC_DCHECK_RUN_ON(worker_thread_); |
Bjorn Terelius | b26cf2f | 2018-10-26 20:39:33 +0200 | [diff] [blame] | 338 | |
| 339 | auto encoding_type = RtcEventLog::EncodingType::Legacy; |
| 340 | if (field_trial::IsEnabled("WebRTC-RtcEventLogNewFormat")) |
| 341 | encoding_type = RtcEventLog::EncodingType::NewFormat; |
Elad Alon | 4a87e1c | 2017-10-03 16:11:34 +0200 | [diff] [blame] | 342 | return event_log_factory_ |
| 343 | ? event_log_factory_->CreateRtcEventLog(encoding_type) |
Danil Chapovalov | 83bbe91 | 2019-08-07 12:24:53 +0200 | [diff] [blame] | 344 | : absl::make_unique<RtcEventLogNull>(); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 345 | } |
| 346 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 347 | std::unique_ptr<Call> PeerConnectionFactory::CreateCall_w( |
| 348 | RtcEventLog* event_log) { |
eladalon | 591753b | 2017-09-06 12:33:43 -0700 | [diff] [blame] | 349 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 350 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 351 | webrtc::Call::Config call_config(event_log); |
| 352 | if (!channel_manager_->media_engine() || !call_factory_) { |
| 353 | return nullptr; |
| 354 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 355 | call_config.audio_state = |
| 356 | channel_manager_->media_engine()->voice().GetAudioState(); |
Elad Alon | 9cfdb20 | 2019-08-13 10:48:48 +0200 | [diff] [blame] | 357 | |
| 358 | FieldTrialParameter<DataRate> min_bandwidth("min", DataRate::kbps(30)); |
| 359 | FieldTrialParameter<DataRate> start_bandwidth("start", DataRate::kbps(300)); |
| 360 | FieldTrialParameter<DataRate> max_bandwidth("max", DataRate::kbps(2000)); |
| 361 | ParseFieldTrial({&min_bandwidth, &start_bandwidth, &max_bandwidth}, |
| 362 | field_trial::FindFullName("WebRTC-PcFactoryDefaultBitrates")); |
| 363 | |
| 364 | call_config.bitrate_config.min_bitrate_bps = |
| 365 | rtc::saturated_cast<int>(min_bandwidth->bps()); |
| 366 | call_config.bitrate_config.start_bitrate_bps = |
| 367 | rtc::saturated_cast<int>(start_bandwidth->bps()); |
| 368 | call_config.bitrate_config.max_bitrate_bps = |
| 369 | rtc::saturated_cast<int>(max_bandwidth->bps()); |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 370 | |
Ying Wang | 0dd1b0a | 2018-02-20 12:50:27 +0100 | [diff] [blame] | 371 | call_config.fec_controller_factory = fec_controller_factory_.get(); |
Danil Chapovalov | 9435c61 | 2019-04-01 10:33:16 +0200 | [diff] [blame] | 372 | call_config.task_queue_factory = task_queue_factory_.get(); |
Ying Wang | 0810a7c | 2019-04-10 13:48:24 +0200 | [diff] [blame] | 373 | call_config.network_state_predictor_factory = |
| 374 | network_state_predictor_factory_.get(); |
Ying Wang | 0dd1b0a | 2018-02-20 12:50:27 +0100 | [diff] [blame] | 375 | |
Sebastian Jansson | 7f57788 | 2018-12-20 09:46:29 +0100 | [diff] [blame] | 376 | if (field_trial::IsEnabled("WebRTC-Bwe-InjectedCongestionController")) { |
Sebastian Jansson | dfce03a | 2018-05-18 18:05:10 +0200 | [diff] [blame] | 377 | RTC_LOG(LS_INFO) << "Using injected network controller factory"; |
| 378 | call_config.network_controller_factory = |
| 379 | injected_network_controller_factory_.get(); |
| 380 | } else { |
| 381 | RTC_LOG(LS_INFO) << "Using default network controller factory"; |
| 382 | } |
| 383 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 384 | return std::unique_ptr<Call>(call_factory_->CreateCall(call_config)); |
| 385 | } |
| 386 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 387 | } // namespace webrtc |