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