| OLD | NEW |
| (Empty) |
| 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 "webrtc/api/peerconnectionfactory.h" | |
| 12 | |
| 13 #include <utility> | |
| 14 | |
| 15 #include "webrtc/api/audiotrack.h" | |
| 16 #include "webrtc/api/localaudiosource.h" | |
| 17 #include "webrtc/api/mediaconstraintsinterface.h" | |
| 18 #include "webrtc/api/mediastream.h" | |
| 19 #include "webrtc/api/mediastreamproxy.h" | |
| 20 #include "webrtc/api/mediastreamtrackproxy.h" | |
| 21 #include "webrtc/api/peerconnection.h" | |
| 22 #include "webrtc/api/peerconnectionfactoryproxy.h" | |
| 23 #include "webrtc/api/peerconnectionproxy.h" | |
| 24 #include "webrtc/api/videocapturertracksource.h" | |
| 25 #include "webrtc/api/videosourceproxy.h" | |
| 26 #include "webrtc/api/videotrack.h" | |
| 27 #include "webrtc/base/bind.h" | |
| 28 #include "webrtc/media/engine/webrtcmediaengine.h" | |
| 29 #include "webrtc/media/engine/webrtcvideodecoderfactory.h" | |
| 30 #include "webrtc/media/engine/webrtcvideoencoderfactory.h" | |
| 31 #include "webrtc/modules/audio_coding/codecs/builtin_audio_decoder_factory.h" | |
| 32 #include "webrtc/modules/audio_device/include/audio_device.h" | |
| 33 #include "webrtc/p2p/base/basicpacketsocketfactory.h" | |
| 34 #include "webrtc/p2p/client/basicportallocator.h" | |
| 35 | |
| 36 namespace webrtc { | |
| 37 | |
| 38 rtc::scoped_refptr<PeerConnectionFactoryInterface> | |
| 39 CreatePeerConnectionFactory() { | |
| 40 rtc::scoped_refptr<PeerConnectionFactory> pc_factory( | |
| 41 new rtc::RefCountedObject<PeerConnectionFactory>()); | |
| 42 | |
| 43 RTC_CHECK(rtc::Thread::Current() == pc_factory->signaling_thread()); | |
| 44 // The signaling thread is the current thread so we can | |
| 45 // safely call Initialize directly. | |
| 46 if (!pc_factory->Initialize()) { | |
| 47 return nullptr; | |
| 48 } | |
| 49 return PeerConnectionFactoryProxy::Create(pc_factory->signaling_thread(), | |
| 50 pc_factory); | |
| 51 } | |
| 52 | |
| 53 rtc::scoped_refptr<PeerConnectionFactoryInterface> CreatePeerConnectionFactory( | |
| 54 rtc::Thread* network_thread, | |
| 55 rtc::Thread* worker_thread, | |
| 56 rtc::Thread* signaling_thread, | |
| 57 AudioDeviceModule* default_adm, | |
| 58 cricket::WebRtcVideoEncoderFactory* encoder_factory, | |
| 59 cricket::WebRtcVideoDecoderFactory* decoder_factory) { | |
| 60 rtc::scoped_refptr<PeerConnectionFactory> pc_factory( | |
| 61 new rtc::RefCountedObject<PeerConnectionFactory>( | |
| 62 network_thread, | |
| 63 worker_thread, | |
| 64 signaling_thread, | |
| 65 default_adm, | |
| 66 CreateBuiltinAudioDecoderFactory(), | |
| 67 encoder_factory, | |
| 68 decoder_factory)); | |
| 69 | |
| 70 // Call Initialize synchronously but make sure its executed on | |
| 71 // |signaling_thread|. | |
| 72 MethodCall0<PeerConnectionFactory, bool> call( | |
| 73 pc_factory.get(), | |
| 74 &PeerConnectionFactory::Initialize); | |
| 75 bool result = call.Marshal(RTC_FROM_HERE, signaling_thread); | |
| 76 | |
| 77 if (!result) { | |
| 78 return nullptr; | |
| 79 } | |
| 80 return PeerConnectionFactoryProxy::Create(signaling_thread, pc_factory); | |
| 81 } | |
| 82 | |
| 83 PeerConnectionFactory::PeerConnectionFactory() | |
| 84 : owns_ptrs_(true), | |
| 85 wraps_current_thread_(false), | |
| 86 network_thread_(rtc::Thread::CreateWithSocketServer().release()), | |
| 87 worker_thread_(rtc::Thread::Create().release()), | |
| 88 signaling_thread_(rtc::Thread::Current()), | |
| 89 audio_decoder_factory_(CreateBuiltinAudioDecoderFactory()) { | |
| 90 if (!signaling_thread_) { | |
| 91 signaling_thread_ = rtc::ThreadManager::Instance()->WrapCurrentThread(); | |
| 92 wraps_current_thread_ = true; | |
| 93 } | |
| 94 network_thread_->Start(); | |
| 95 worker_thread_->Start(); | |
| 96 } | |
| 97 | |
| 98 PeerConnectionFactory::PeerConnectionFactory( | |
| 99 rtc::Thread* network_thread, | |
| 100 rtc::Thread* worker_thread, | |
| 101 rtc::Thread* signaling_thread, | |
| 102 AudioDeviceModule* default_adm, | |
| 103 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& | |
| 104 audio_decoder_factory, | |
| 105 cricket::WebRtcVideoEncoderFactory* video_encoder_factory, | |
| 106 cricket::WebRtcVideoDecoderFactory* video_decoder_factory) | |
| 107 : owns_ptrs_(false), | |
| 108 wraps_current_thread_(false), | |
| 109 network_thread_(network_thread), | |
| 110 worker_thread_(worker_thread), | |
| 111 signaling_thread_(signaling_thread), | |
| 112 default_adm_(default_adm), | |
| 113 audio_decoder_factory_(audio_decoder_factory), | |
| 114 video_encoder_factory_(video_encoder_factory), | |
| 115 video_decoder_factory_(video_decoder_factory) { | |
| 116 RTC_DCHECK(network_thread); | |
| 117 RTC_DCHECK(worker_thread); | |
| 118 RTC_DCHECK(signaling_thread); | |
| 119 // TODO: Currently there is no way creating an external adm in | |
| 120 // libjingle source tree. So we can 't currently assert if this is NULL. | |
| 121 // ASSERT(default_adm != NULL); | |
| 122 } | |
| 123 | |
| 124 PeerConnectionFactory::~PeerConnectionFactory() { | |
| 125 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
| 126 channel_manager_.reset(nullptr); | |
| 127 | |
| 128 // Make sure |worker_thread_| and |signaling_thread_| outlive | |
| 129 // |default_socket_factory_| and |default_network_manager_|. | |
| 130 default_socket_factory_ = nullptr; | |
| 131 default_network_manager_ = nullptr; | |
| 132 | |
| 133 if (owns_ptrs_) { | |
| 134 if (wraps_current_thread_) | |
| 135 rtc::ThreadManager::Instance()->UnwrapCurrentThread(); | |
| 136 delete worker_thread_; | |
| 137 delete network_thread_; | |
| 138 } | |
| 139 } | |
| 140 | |
| 141 bool PeerConnectionFactory::Initialize() { | |
| 142 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
| 143 rtc::InitRandom(rtc::Time32()); | |
| 144 | |
| 145 default_network_manager_.reset(new rtc::BasicNetworkManager()); | |
| 146 if (!default_network_manager_) { | |
| 147 return false; | |
| 148 } | |
| 149 | |
| 150 default_socket_factory_.reset( | |
| 151 new rtc::BasicPacketSocketFactory(network_thread_)); | |
| 152 if (!default_socket_factory_) { | |
| 153 return false; | |
| 154 } | |
| 155 | |
| 156 // TODO: Need to make sure only one VoE is created inside | |
| 157 // WebRtcMediaEngine. | |
| 158 cricket::MediaEngineInterface* media_engine = | |
| 159 worker_thread_->Invoke<cricket::MediaEngineInterface*>( | |
| 160 RTC_FROM_HERE, | |
| 161 rtc::Bind(&PeerConnectionFactory::CreateMediaEngine_w, this)); | |
| 162 | |
| 163 channel_manager_.reset(new cricket::ChannelManager( | |
| 164 media_engine, worker_thread_, network_thread_)); | |
| 165 | |
| 166 channel_manager_->SetVideoRtxEnabled(true); | |
| 167 channel_manager_->SetCryptoOptions(options_.crypto_options); | |
| 168 if (!channel_manager_->Init()) { | |
| 169 return false; | |
| 170 } | |
| 171 | |
| 172 return true; | |
| 173 } | |
| 174 | |
| 175 void PeerConnectionFactory::SetOptions(const Options& options) { | |
| 176 options_ = options; | |
| 177 if (channel_manager_) { | |
| 178 channel_manager_->SetCryptoOptions(options.crypto_options); | |
| 179 } | |
| 180 } | |
| 181 | |
| 182 rtc::scoped_refptr<AudioSourceInterface> | |
| 183 PeerConnectionFactory::CreateAudioSource( | |
| 184 const MediaConstraintsInterface* constraints) { | |
| 185 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
| 186 rtc::scoped_refptr<LocalAudioSource> source( | |
| 187 LocalAudioSource::Create(options_, constraints)); | |
| 188 return source; | |
| 189 } | |
| 190 | |
| 191 rtc::scoped_refptr<AudioSourceInterface> | |
| 192 PeerConnectionFactory::CreateAudioSource(const cricket::AudioOptions& options) { | |
| 193 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
| 194 rtc::scoped_refptr<LocalAudioSource> source( | |
| 195 LocalAudioSource::Create(options_, &options)); | |
| 196 return source; | |
| 197 } | |
| 198 | |
| 199 rtc::scoped_refptr<VideoTrackSourceInterface> | |
| 200 PeerConnectionFactory::CreateVideoSource( | |
| 201 cricket::VideoCapturer* capturer, | |
| 202 const MediaConstraintsInterface* constraints) { | |
| 203 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
| 204 rtc::scoped_refptr<VideoTrackSourceInterface> source( | |
| 205 VideoCapturerTrackSource::Create(worker_thread_, capturer, constraints, | |
| 206 false)); | |
| 207 return VideoTrackSourceProxy::Create(signaling_thread_, worker_thread_, | |
| 208 source); | |
| 209 } | |
| 210 | |
| 211 rtc::scoped_refptr<VideoTrackSourceInterface> | |
| 212 PeerConnectionFactory::CreateVideoSource(cricket::VideoCapturer* capturer) { | |
| 213 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
| 214 rtc::scoped_refptr<VideoTrackSourceInterface> source( | |
| 215 VideoCapturerTrackSource::Create(worker_thread_, capturer, false)); | |
| 216 return VideoTrackSourceProxy::Create(signaling_thread_, worker_thread_, | |
| 217 source); | |
| 218 } | |
| 219 | |
| 220 bool PeerConnectionFactory::StartAecDump(rtc::PlatformFile file, | |
| 221 int64_t max_size_bytes) { | |
| 222 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
| 223 return channel_manager_->StartAecDump(file, max_size_bytes); | |
| 224 } | |
| 225 | |
| 226 void PeerConnectionFactory::StopAecDump() { | |
| 227 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
| 228 channel_manager_->StopAecDump(); | |
| 229 } | |
| 230 | |
| 231 rtc::scoped_refptr<PeerConnectionInterface> | |
| 232 PeerConnectionFactory::CreatePeerConnection( | |
| 233 const PeerConnectionInterface::RTCConfiguration& configuration_in, | |
| 234 const MediaConstraintsInterface* constraints, | |
| 235 std::unique_ptr<cricket::PortAllocator> allocator, | |
| 236 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, | |
| 237 PeerConnectionObserver* observer) { | |
| 238 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
| 239 | |
| 240 // We merge constraints and configuration into a single configuration. | |
| 241 PeerConnectionInterface::RTCConfiguration configuration = configuration_in; | |
| 242 CopyConstraintsIntoRtcConfiguration(constraints, &configuration); | |
| 243 | |
| 244 return CreatePeerConnection(configuration, std::move(allocator), | |
| 245 std::move(cert_generator), observer); | |
| 246 } | |
| 247 | |
| 248 rtc::scoped_refptr<PeerConnectionInterface> | |
| 249 PeerConnectionFactory::CreatePeerConnection( | |
| 250 const PeerConnectionInterface::RTCConfiguration& configuration, | |
| 251 std::unique_ptr<cricket::PortAllocator> allocator, | |
| 252 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, | |
| 253 PeerConnectionObserver* observer) { | |
| 254 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
| 255 | |
| 256 if (!cert_generator.get()) { | |
| 257 // No certificate generator specified, use the default one. | |
| 258 cert_generator.reset( | |
| 259 new rtc::RTCCertificateGenerator(signaling_thread_, network_thread_)); | |
| 260 } | |
| 261 | |
| 262 if (!allocator) { | |
| 263 allocator.reset(new cricket::BasicPortAllocator( | |
| 264 default_network_manager_.get(), default_socket_factory_.get())); | |
| 265 } | |
| 266 network_thread_->Invoke<void>( | |
| 267 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::SetNetworkIgnoreMask, | |
| 268 allocator.get(), options_.network_ignore_mask)); | |
| 269 | |
| 270 rtc::scoped_refptr<PeerConnection> pc( | |
| 271 new rtc::RefCountedObject<PeerConnection>(this)); | |
| 272 | |
| 273 if (!pc->Initialize(configuration, std::move(allocator), | |
| 274 std::move(cert_generator), observer)) { | |
| 275 return nullptr; | |
| 276 } | |
| 277 return PeerConnectionProxy::Create(signaling_thread(), pc); | |
| 278 } | |
| 279 | |
| 280 rtc::scoped_refptr<MediaStreamInterface> | |
| 281 PeerConnectionFactory::CreateLocalMediaStream(const std::string& label) { | |
| 282 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
| 283 return MediaStreamProxy::Create(signaling_thread_, | |
| 284 MediaStream::Create(label)); | |
| 285 } | |
| 286 | |
| 287 rtc::scoped_refptr<VideoTrackInterface> PeerConnectionFactory::CreateVideoTrack( | |
| 288 const std::string& id, | |
| 289 VideoTrackSourceInterface* source) { | |
| 290 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
| 291 rtc::scoped_refptr<VideoTrackInterface> track( | |
| 292 VideoTrack::Create(id, source)); | |
| 293 return VideoTrackProxy::Create(signaling_thread_, worker_thread_, track); | |
| 294 } | |
| 295 | |
| 296 rtc::scoped_refptr<AudioTrackInterface> | |
| 297 PeerConnectionFactory::CreateAudioTrack(const std::string& id, | |
| 298 AudioSourceInterface* source) { | |
| 299 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
| 300 rtc::scoped_refptr<AudioTrackInterface> track(AudioTrack::Create(id, source)); | |
| 301 return AudioTrackProxy::Create(signaling_thread_, track); | |
| 302 } | |
| 303 | |
| 304 webrtc::MediaControllerInterface* PeerConnectionFactory::CreateMediaController( | |
| 305 const cricket::MediaConfig& config, | |
| 306 webrtc::RtcEventLog* event_log) const { | |
| 307 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
| 308 return MediaControllerInterface::Create(config, worker_thread_, | |
| 309 channel_manager_.get(), event_log); | |
| 310 } | |
| 311 | |
| 312 cricket::TransportController* PeerConnectionFactory::CreateTransportController( | |
| 313 cricket::PortAllocator* port_allocator, | |
| 314 bool redetermine_role_on_ice_restart) { | |
| 315 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
| 316 return new cricket::TransportController(signaling_thread_, network_thread_, | |
| 317 port_allocator, | |
| 318 redetermine_role_on_ice_restart); | |
| 319 } | |
| 320 | |
| 321 rtc::Thread* PeerConnectionFactory::signaling_thread() { | |
| 322 // This method can be called on a different thread when the factory is | |
| 323 // created in CreatePeerConnectionFactory(). | |
| 324 return signaling_thread_; | |
| 325 } | |
| 326 | |
| 327 rtc::Thread* PeerConnectionFactory::worker_thread() { | |
| 328 RTC_DCHECK(signaling_thread_->IsCurrent()); | |
| 329 return worker_thread_; | |
| 330 } | |
| 331 | |
| 332 rtc::Thread* PeerConnectionFactory::network_thread() { | |
| 333 return network_thread_; | |
| 334 } | |
| 335 | |
| 336 cricket::MediaEngineInterface* PeerConnectionFactory::CreateMediaEngine_w() { | |
| 337 ASSERT(worker_thread_ == rtc::Thread::Current()); | |
| 338 return cricket::WebRtcMediaEngineFactory::Create( | |
| 339 default_adm_.get(), | |
| 340 audio_decoder_factory_, | |
| 341 video_encoder_factory_.get(), | |
| 342 video_decoder_factory_.get()); | |
| 343 } | |
| 344 | |
| 345 } // namespace webrtc | |
| OLD | NEW |