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