blob: dc14bfb5675f6596f33464472ee95b890cb9d248 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004--2011, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/app/webrtc/peerconnectionfactory.h"
29
30#include "talk/app/webrtc/audiotrack.h"
31#include "talk/app/webrtc/localaudiosource.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032#include "talk/app/webrtc/mediastreamproxy.h"
33#include "talk/app/webrtc/mediastreamtrackproxy.h"
34#include "talk/app/webrtc/peerconnection.h"
35#include "talk/app/webrtc/peerconnectionproxy.h"
36#include "talk/app/webrtc/portallocatorfactory.h"
wu@webrtc.org967bfff2013-09-19 05:49:50 +000037#include "talk/app/webrtc/videosource.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038#include "talk/app/webrtc/videosourceproxy.h"
39#include "talk/app/webrtc/videotrack.h"
40#include "talk/media/devices/dummydevicemanager.h"
41#include "talk/media/webrtc/webrtcmediaengine.h"
42#include "talk/media/webrtc/webrtcvideodecoderfactory.h"
43#include "talk/media/webrtc/webrtcvideoencoderfactory.h"
44#include "webrtc/modules/audio_device/include/audio_device.h"
45
46using talk_base::scoped_refptr;
47
48namespace {
49
50typedef talk_base::TypedMessageData<bool> InitMessageData;
51
52struct CreatePeerConnectionParams : public talk_base::MessageData {
53 CreatePeerConnectionParams(
54 const webrtc::PeerConnectionInterface::IceServers& configuration,
55 const webrtc::MediaConstraintsInterface* constraints,
56 webrtc::PortAllocatorFactoryInterface* allocator_factory,
wu@webrtc.org91053e72013-08-10 07:18:04 +000057 webrtc::DTLSIdentityServiceInterface* dtls_identity_service,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 webrtc::PeerConnectionObserver* observer)
59 : configuration(configuration),
60 constraints(constraints),
61 allocator_factory(allocator_factory),
wu@webrtc.org91053e72013-08-10 07:18:04 +000062 dtls_identity_service(dtls_identity_service),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063 observer(observer) {
64 }
65 scoped_refptr<webrtc::PeerConnectionInterface> peerconnection;
66 const webrtc::PeerConnectionInterface::IceServers& configuration;
67 const webrtc::MediaConstraintsInterface* constraints;
68 scoped_refptr<webrtc::PortAllocatorFactoryInterface> allocator_factory;
wu@webrtc.org91053e72013-08-10 07:18:04 +000069 webrtc::DTLSIdentityServiceInterface* dtls_identity_service;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070 webrtc::PeerConnectionObserver* observer;
71};
72
73struct CreatePeerConnectionParamsDeprecated : public talk_base::MessageData {
74 CreatePeerConnectionParamsDeprecated(
75 const std::string& configuration,
76 webrtc::PortAllocatorFactoryInterface* allocator_factory,
77 webrtc::PeerConnectionObserver* observer)
78 : configuration(configuration),
79 allocator_factory(allocator_factory),
80 observer(observer) {
81 }
82 scoped_refptr<webrtc::PeerConnectionInterface> peerconnection;
83 const std::string& configuration;
84 scoped_refptr<webrtc::PortAllocatorFactoryInterface> allocator_factory;
85 webrtc::PeerConnectionObserver* observer;
86};
87
88struct CreateAudioSourceParams : public talk_base::MessageData {
89 explicit CreateAudioSourceParams(
90 const webrtc::MediaConstraintsInterface* constraints)
91 : constraints(constraints) {
92 }
93 const webrtc::MediaConstraintsInterface* constraints;
94 scoped_refptr<webrtc::AudioSourceInterface> source;
95};
96
97struct CreateVideoSourceParams : public talk_base::MessageData {
98 CreateVideoSourceParams(cricket::VideoCapturer* capturer,
99 const webrtc::MediaConstraintsInterface* constraints)
100 : capturer(capturer),
101 constraints(constraints) {
102 }
103 cricket::VideoCapturer* capturer;
104 const webrtc::MediaConstraintsInterface* constraints;
105 scoped_refptr<webrtc::VideoSourceInterface> source;
106};
107
wu@webrtc.orga9890802013-12-13 00:21:03 +0000108struct StartAecDumpParams : public talk_base::MessageData {
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000109 explicit StartAecDumpParams(talk_base::PlatformFile aec_dump_file)
wu@webrtc.orga9890802013-12-13 00:21:03 +0000110 : aec_dump_file(aec_dump_file) {
111 }
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000112 talk_base::PlatformFile aec_dump_file;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000113 bool result;
114};
115
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116enum {
117 MSG_INIT_FACTORY = 1,
118 MSG_TERMINATE_FACTORY,
119 MSG_CREATE_PEERCONNECTION,
120 MSG_CREATE_AUDIOSOURCE,
121 MSG_CREATE_VIDEOSOURCE,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000122 MSG_START_AEC_DUMP,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123};
124
125} // namespace
126
127namespace webrtc {
128
129scoped_refptr<PeerConnectionFactoryInterface>
130CreatePeerConnectionFactory() {
131 scoped_refptr<PeerConnectionFactory> pc_factory(
132 new talk_base::RefCountedObject<PeerConnectionFactory>());
133
134 if (!pc_factory->Initialize()) {
135 return NULL;
136 }
137 return pc_factory;
138}
139
140scoped_refptr<PeerConnectionFactoryInterface>
141CreatePeerConnectionFactory(
142 talk_base::Thread* worker_thread,
143 talk_base::Thread* signaling_thread,
144 AudioDeviceModule* default_adm,
145 cricket::WebRtcVideoEncoderFactory* encoder_factory,
146 cricket::WebRtcVideoDecoderFactory* decoder_factory) {
147 scoped_refptr<PeerConnectionFactory> pc_factory(
148 new talk_base::RefCountedObject<PeerConnectionFactory>(
149 worker_thread, signaling_thread, default_adm,
150 encoder_factory, decoder_factory));
151 if (!pc_factory->Initialize()) {
152 return NULL;
153 }
154 return pc_factory;
155}
156
157PeerConnectionFactory::PeerConnectionFactory()
158 : owns_ptrs_(true),
159 signaling_thread_(new talk_base::Thread),
160 worker_thread_(new talk_base::Thread) {
161 bool result = signaling_thread_->Start();
162 ASSERT(result);
163 result = worker_thread_->Start();
164 ASSERT(result);
165}
166
167PeerConnectionFactory::PeerConnectionFactory(
168 talk_base::Thread* worker_thread,
169 talk_base::Thread* signaling_thread,
170 AudioDeviceModule* default_adm,
171 cricket::WebRtcVideoEncoderFactory* video_encoder_factory,
172 cricket::WebRtcVideoDecoderFactory* video_decoder_factory)
173 : owns_ptrs_(false),
174 signaling_thread_(signaling_thread),
175 worker_thread_(worker_thread),
176 default_adm_(default_adm),
177 video_encoder_factory_(video_encoder_factory),
178 video_decoder_factory_(video_decoder_factory) {
179 ASSERT(worker_thread != NULL);
180 ASSERT(signaling_thread != NULL);
181 // TODO: Currently there is no way creating an external adm in
182 // libjingle source tree. So we can 't currently assert if this is NULL.
183 // ASSERT(default_adm != NULL);
184}
185
186PeerConnectionFactory::~PeerConnectionFactory() {
187 signaling_thread_->Clear(this);
188 signaling_thread_->Send(this, MSG_TERMINATE_FACTORY);
189 if (owns_ptrs_) {
190 delete signaling_thread_;
191 delete worker_thread_;
192 }
193}
194
195bool PeerConnectionFactory::Initialize() {
196 InitMessageData result(false);
197 signaling_thread_->Send(this, MSG_INIT_FACTORY, &result);
198 return result.data();
199}
200
201void PeerConnectionFactory::OnMessage(talk_base::Message* msg) {
202 switch (msg->message_id) {
203 case MSG_INIT_FACTORY: {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000204 InitMessageData* pdata = static_cast<InitMessageData*>(msg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205 pdata->data() = Initialize_s();
206 break;
207 }
208 case MSG_TERMINATE_FACTORY: {
209 Terminate_s();
210 break;
211 }
212 case MSG_CREATE_PEERCONNECTION: {
213 CreatePeerConnectionParams* pdata =
wu@webrtc.org91053e72013-08-10 07:18:04 +0000214 static_cast<CreatePeerConnectionParams*> (msg->pdata);
215 pdata->peerconnection = CreatePeerConnection_s(
216 pdata->configuration,
217 pdata->constraints,
218 pdata->allocator_factory,
219 pdata->dtls_identity_service,
220 pdata->observer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221 break;
222 }
223 case MSG_CREATE_AUDIOSOURCE: {
224 CreateAudioSourceParams* pdata =
225 static_cast<CreateAudioSourceParams*>(msg->pdata);
226 pdata->source = CreateAudioSource_s(pdata->constraints);
227 break;
228 }
229 case MSG_CREATE_VIDEOSOURCE: {
230 CreateVideoSourceParams* pdata =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000231 static_cast<CreateVideoSourceParams*>(msg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 pdata->source = CreateVideoSource_s(pdata->capturer, pdata->constraints);
233 break;
234 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000235 case MSG_START_AEC_DUMP: {
236 StartAecDumpParams* pdata =
237 static_cast<StartAecDumpParams*>(msg->pdata);
238 pdata->result = StartAecDump_s(pdata->aec_dump_file);
239 break;
240 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241 }
242}
243
244bool PeerConnectionFactory::Initialize_s() {
245 talk_base::InitRandom(talk_base::Time());
246
247 allocator_factory_ = PortAllocatorFactory::Create(worker_thread_);
248 if (!allocator_factory_)
249 return false;
250
251 cricket::DummyDeviceManager* device_manager(
252 new cricket::DummyDeviceManager());
253 // TODO: Need to make sure only one VoE is created inside
254 // WebRtcMediaEngine.
255 cricket::WebRtcMediaEngine* webrtc_media_engine(
256 new cricket::WebRtcMediaEngine(default_adm_.get(),
257 NULL, // No secondary adm.
258 video_encoder_factory_.get(),
259 video_decoder_factory_.get()));
260
261 channel_manager_.reset(new cricket::ChannelManager(
262 webrtc_media_engine, device_manager, worker_thread_));
263 if (!channel_manager_->Init()) {
264 return false;
265 }
266 return true;
267}
268
269// Terminate what we created on the signaling thread.
270void PeerConnectionFactory::Terminate_s() {
271 channel_manager_.reset(NULL);
272 allocator_factory_ = NULL;
273}
274
275talk_base::scoped_refptr<AudioSourceInterface>
276PeerConnectionFactory::CreateAudioSource_s(
277 const MediaConstraintsInterface* constraints) {
278 talk_base::scoped_refptr<LocalAudioSource> source(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000279 LocalAudioSource::Create(options_, constraints));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 return source;
281}
282
283talk_base::scoped_refptr<VideoSourceInterface>
284PeerConnectionFactory::CreateVideoSource_s(
285 cricket::VideoCapturer* capturer,
286 const MediaConstraintsInterface* constraints) {
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000287 talk_base::scoped_refptr<VideoSource> source(
288 VideoSource::Create(channel_manager_.get(), capturer, constraints));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289 return VideoSourceProxy::Create(signaling_thread_, source);
290}
291
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000292bool PeerConnectionFactory::StartAecDump_s(talk_base::PlatformFile file) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000293 return channel_manager_->StartAecDump(file);
294}
295
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296scoped_refptr<PeerConnectionInterface>
297PeerConnectionFactory::CreatePeerConnection(
298 const PeerConnectionInterface::IceServers& configuration,
299 const MediaConstraintsInterface* constraints,
300 PortAllocatorFactoryInterface* allocator_factory,
301 DTLSIdentityServiceInterface* dtls_identity_service,
302 PeerConnectionObserver* observer) {
303 CreatePeerConnectionParams params(configuration, constraints,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000304 allocator_factory, dtls_identity_service,
305 observer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306 signaling_thread_->Send(this, MSG_CREATE_PEERCONNECTION, &params);
307 return params.peerconnection;
308}
309
310scoped_refptr<PeerConnectionInterface>
311PeerConnectionFactory::CreatePeerConnection(
312 const PeerConnectionInterface::IceServers& configuration,
313 const MediaConstraintsInterface* constraints,
314 DTLSIdentityServiceInterface* dtls_identity_service,
315 PeerConnectionObserver* observer) {
316 return CreatePeerConnection(
317 configuration, constraints, NULL, dtls_identity_service, observer);
318}
319
320talk_base::scoped_refptr<PeerConnectionInterface>
321PeerConnectionFactory::CreatePeerConnection_s(
322 const PeerConnectionInterface::IceServers& configuration,
323 const MediaConstraintsInterface* constraints,
324 PortAllocatorFactoryInterface* allocator_factory,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000325 DTLSIdentityServiceInterface* dtls_identity_service,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326 PeerConnectionObserver* observer) {
327 ASSERT(allocator_factory || allocator_factory_);
328 talk_base::scoped_refptr<PeerConnection> pc(
329 new talk_base::RefCountedObject<PeerConnection>(this));
330 if (!pc->Initialize(
331 configuration,
332 constraints,
333 allocator_factory ? allocator_factory : allocator_factory_.get(),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000334 dtls_identity_service,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335 observer)) {
336 return NULL;
337 }
338 return PeerConnectionProxy::Create(signaling_thread(), pc);
339}
340
341scoped_refptr<MediaStreamInterface>
342PeerConnectionFactory::CreateLocalMediaStream(const std::string& label) {
343 return MediaStreamProxy::Create(signaling_thread_,
344 MediaStream::Create(label));
345}
346
347talk_base::scoped_refptr<AudioSourceInterface>
348PeerConnectionFactory::CreateAudioSource(
349 const MediaConstraintsInterface* constraints) {
350 CreateAudioSourceParams params(constraints);
351 signaling_thread_->Send(this, MSG_CREATE_AUDIOSOURCE, &params);
352 return params.source;
353}
354
355talk_base::scoped_refptr<VideoSourceInterface>
356PeerConnectionFactory::CreateVideoSource(
357 cricket::VideoCapturer* capturer,
358 const MediaConstraintsInterface* constraints) {
359
360 CreateVideoSourceParams params(capturer,
361 constraints);
362 signaling_thread_->Send(this, MSG_CREATE_VIDEOSOURCE, &params);
363 return params.source;
364}
365
366talk_base::scoped_refptr<VideoTrackInterface>
367PeerConnectionFactory::CreateVideoTrack(
368 const std::string& id,
369 VideoSourceInterface* source) {
370 talk_base::scoped_refptr<VideoTrackInterface> track(
371 VideoTrack::Create(id, source));
372 return VideoTrackProxy::Create(signaling_thread_, track);
373}
374
375scoped_refptr<AudioTrackInterface> PeerConnectionFactory::CreateAudioTrack(
376 const std::string& id,
377 AudioSourceInterface* source) {
378 talk_base::scoped_refptr<AudioTrackInterface> track(
379 AudioTrack::Create(id, source));
380 return AudioTrackProxy::Create(signaling_thread_, track);
381}
382
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000383bool PeerConnectionFactory::StartAecDump(talk_base::PlatformFile file) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000384 StartAecDumpParams params(file);
385 signaling_thread_->Send(this, MSG_START_AEC_DUMP, &params);
386 return params.result;
387}
388
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389cricket::ChannelManager* PeerConnectionFactory::channel_manager() {
390 return channel_manager_.get();
391}
392
393talk_base::Thread* PeerConnectionFactory::signaling_thread() {
394 return signaling_thread_;
395}
396
397talk_base::Thread* PeerConnectionFactory::worker_thread() {
398 return worker_thread_;
399}
400
401} // namespace webrtc