blob: a328bd0e92bbce93009962f6178f05606759f92a [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(
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000054 const webrtc::PeerConnectionInterface::RTCConfiguration& configuration,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055 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;
buildbot@webrtc.org41451d42014-05-03 05:39:45 +000066 const webrtc::PeerConnectionInterface::RTCConfiguration& configuration;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067 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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073struct CreateAudioSourceParams : public talk_base::MessageData {
74 explicit CreateAudioSourceParams(
75 const webrtc::MediaConstraintsInterface* constraints)
76 : constraints(constraints) {
77 }
78 const webrtc::MediaConstraintsInterface* constraints;
79 scoped_refptr<webrtc::AudioSourceInterface> source;
80};
81
82struct CreateVideoSourceParams : public talk_base::MessageData {
83 CreateVideoSourceParams(cricket::VideoCapturer* capturer,
84 const webrtc::MediaConstraintsInterface* constraints)
85 : capturer(capturer),
86 constraints(constraints) {
87 }
88 cricket::VideoCapturer* capturer;
89 const webrtc::MediaConstraintsInterface* constraints;
90 scoped_refptr<webrtc::VideoSourceInterface> source;
91};
92
wu@webrtc.orga9890802013-12-13 00:21:03 +000093struct StartAecDumpParams : public talk_base::MessageData {
wu@webrtc.orga8910d22014-01-23 22:12:45 +000094 explicit StartAecDumpParams(talk_base::PlatformFile aec_dump_file)
wu@webrtc.orga9890802013-12-13 00:21:03 +000095 : aec_dump_file(aec_dump_file) {
96 }
wu@webrtc.orga8910d22014-01-23 22:12:45 +000097 talk_base::PlatformFile aec_dump_file;
wu@webrtc.orga9890802013-12-13 00:21:03 +000098 bool result;
99};
100
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101enum {
102 MSG_INIT_FACTORY = 1,
103 MSG_TERMINATE_FACTORY,
104 MSG_CREATE_PEERCONNECTION,
105 MSG_CREATE_AUDIOSOURCE,
106 MSG_CREATE_VIDEOSOURCE,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000107 MSG_START_AEC_DUMP,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108};
109
110} // namespace
111
112namespace webrtc {
113
buildbot@webrtc.org17911dc2014-05-12 18:42:49 +0000114scoped_refptr<PeerConnectionFactoryInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115CreatePeerConnectionFactory() {
buildbot@webrtc.org17911dc2014-05-12 18:42:49 +0000116 scoped_refptr<PeerConnectionFactory> pc_factory(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 new talk_base::RefCountedObject<PeerConnectionFactory>());
118
119 if (!pc_factory->Initialize()) {
120 return NULL;
121 }
122 return pc_factory;
123}
124
buildbot@webrtc.org17911dc2014-05-12 18:42:49 +0000125scoped_refptr<PeerConnectionFactoryInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126CreatePeerConnectionFactory(
127 talk_base::Thread* worker_thread,
128 talk_base::Thread* signaling_thread,
129 AudioDeviceModule* default_adm,
130 cricket::WebRtcVideoEncoderFactory* encoder_factory,
131 cricket::WebRtcVideoDecoderFactory* decoder_factory) {
buildbot@webrtc.org17911dc2014-05-12 18:42:49 +0000132 scoped_refptr<PeerConnectionFactory> pc_factory(
133 new talk_base::RefCountedObject<PeerConnectionFactory>(
134 worker_thread, signaling_thread, default_adm,
135 encoder_factory, decoder_factory));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 if (!pc_factory->Initialize()) {
137 return NULL;
138 }
139 return pc_factory;
140}
141
142PeerConnectionFactory::PeerConnectionFactory()
143 : owns_ptrs_(true),
144 signaling_thread_(new talk_base::Thread),
145 worker_thread_(new talk_base::Thread) {
146 bool result = signaling_thread_->Start();
147 ASSERT(result);
148 result = worker_thread_->Start();
149 ASSERT(result);
150}
151
152PeerConnectionFactory::PeerConnectionFactory(
153 talk_base::Thread* worker_thread,
154 talk_base::Thread* signaling_thread,
155 AudioDeviceModule* default_adm,
156 cricket::WebRtcVideoEncoderFactory* video_encoder_factory,
157 cricket::WebRtcVideoDecoderFactory* video_decoder_factory)
158 : owns_ptrs_(false),
159 signaling_thread_(signaling_thread),
160 worker_thread_(worker_thread),
161 default_adm_(default_adm),
162 video_encoder_factory_(video_encoder_factory),
163 video_decoder_factory_(video_decoder_factory) {
164 ASSERT(worker_thread != NULL);
165 ASSERT(signaling_thread != NULL);
166 // TODO: Currently there is no way creating an external adm in
167 // libjingle source tree. So we can 't currently assert if this is NULL.
168 // ASSERT(default_adm != NULL);
169}
170
171PeerConnectionFactory::~PeerConnectionFactory() {
172 signaling_thread_->Clear(this);
173 signaling_thread_->Send(this, MSG_TERMINATE_FACTORY);
174 if (owns_ptrs_) {
fischman@webrtc.org29540b12014-04-17 22:54:30 +0000175 delete signaling_thread_;
176 delete worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 }
178}
179
180bool PeerConnectionFactory::Initialize() {
181 InitMessageData result(false);
182 signaling_thread_->Send(this, MSG_INIT_FACTORY, &result);
183 return result.data();
184}
185
186void PeerConnectionFactory::OnMessage(talk_base::Message* msg) {
187 switch (msg->message_id) {
188 case MSG_INIT_FACTORY: {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000189 InitMessageData* pdata = static_cast<InitMessageData*>(msg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 pdata->data() = Initialize_s();
191 break;
192 }
193 case MSG_TERMINATE_FACTORY: {
194 Terminate_s();
195 break;
196 }
197 case MSG_CREATE_PEERCONNECTION: {
198 CreatePeerConnectionParams* pdata =
wu@webrtc.org91053e72013-08-10 07:18:04 +0000199 static_cast<CreatePeerConnectionParams*> (msg->pdata);
200 pdata->peerconnection = CreatePeerConnection_s(
201 pdata->configuration,
202 pdata->constraints,
203 pdata->allocator_factory,
204 pdata->dtls_identity_service,
205 pdata->observer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 break;
207 }
208 case MSG_CREATE_AUDIOSOURCE: {
209 CreateAudioSourceParams* pdata =
210 static_cast<CreateAudioSourceParams*>(msg->pdata);
211 pdata->source = CreateAudioSource_s(pdata->constraints);
212 break;
213 }
214 case MSG_CREATE_VIDEOSOURCE: {
215 CreateVideoSourceParams* pdata =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000216 static_cast<CreateVideoSourceParams*>(msg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 pdata->source = CreateVideoSource_s(pdata->capturer, pdata->constraints);
218 break;
219 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000220 case MSG_START_AEC_DUMP: {
221 StartAecDumpParams* pdata =
222 static_cast<StartAecDumpParams*>(msg->pdata);
223 pdata->result = StartAecDump_s(pdata->aec_dump_file);
224 break;
225 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 }
227}
228
229bool PeerConnectionFactory::Initialize_s() {
230 talk_base::InitRandom(talk_base::Time());
231
232 allocator_factory_ = PortAllocatorFactory::Create(worker_thread_);
233 if (!allocator_factory_)
234 return false;
235
236 cricket::DummyDeviceManager* device_manager(
237 new cricket::DummyDeviceManager());
238 // TODO: Need to make sure only one VoE is created inside
239 // WebRtcMediaEngine.
240 cricket::WebRtcMediaEngine* webrtc_media_engine(
241 new cricket::WebRtcMediaEngine(default_adm_.get(),
242 NULL, // No secondary adm.
243 video_encoder_factory_.get(),
244 video_decoder_factory_.get()));
245
246 channel_manager_.reset(new cricket::ChannelManager(
247 webrtc_media_engine, device_manager, worker_thread_));
248 if (!channel_manager_->Init()) {
249 return false;
250 }
251 return true;
252}
253
254// Terminate what we created on the signaling thread.
255void PeerConnectionFactory::Terminate_s() {
256 channel_manager_.reset(NULL);
257 allocator_factory_ = NULL;
258}
259
260talk_base::scoped_refptr<AudioSourceInterface>
261PeerConnectionFactory::CreateAudioSource_s(
262 const MediaConstraintsInterface* constraints) {
263 talk_base::scoped_refptr<LocalAudioSource> source(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000264 LocalAudioSource::Create(options_, constraints));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 return source;
266}
267
268talk_base::scoped_refptr<VideoSourceInterface>
269PeerConnectionFactory::CreateVideoSource_s(
270 cricket::VideoCapturer* capturer,
271 const MediaConstraintsInterface* constraints) {
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000272 talk_base::scoped_refptr<VideoSource> source(
273 VideoSource::Create(channel_manager_.get(), capturer, constraints));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 return VideoSourceProxy::Create(signaling_thread_, source);
275}
276
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000277bool PeerConnectionFactory::StartAecDump_s(talk_base::PlatformFile file) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000278 return channel_manager_->StartAecDump(file);
279}
280
buildbot@webrtc.org17911dc2014-05-12 18:42:49 +0000281scoped_refptr<PeerConnectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282PeerConnectionFactory::CreatePeerConnection(
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000283 const PeerConnectionInterface::RTCConfiguration& configuration,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 const MediaConstraintsInterface* constraints,
285 PortAllocatorFactoryInterface* allocator_factory,
286 DTLSIdentityServiceInterface* dtls_identity_service,
287 PeerConnectionObserver* observer) {
288 CreatePeerConnectionParams params(configuration, constraints,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000289 allocator_factory, dtls_identity_service,
290 observer);
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000291 signaling_thread_->Send(
292 this, MSG_CREATE_PEERCONNECTION, &params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 return params.peerconnection;
294}
295
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296talk_base::scoped_refptr<PeerConnectionInterface>
297PeerConnectionFactory::CreatePeerConnection_s(
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000298 const PeerConnectionInterface::RTCConfiguration& configuration,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 const MediaConstraintsInterface* constraints,
300 PortAllocatorFactoryInterface* allocator_factory,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000301 DTLSIdentityServiceInterface* dtls_identity_service,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302 PeerConnectionObserver* observer) {
303 ASSERT(allocator_factory || allocator_factory_);
304 talk_base::scoped_refptr<PeerConnection> pc(
305 new talk_base::RefCountedObject<PeerConnection>(this));
306 if (!pc->Initialize(
307 configuration,
308 constraints,
309 allocator_factory ? allocator_factory : allocator_factory_.get(),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000310 dtls_identity_service,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311 observer)) {
312 return NULL;
313 }
314 return PeerConnectionProxy::Create(signaling_thread(), pc);
315}
316
buildbot@webrtc.org17911dc2014-05-12 18:42:49 +0000317scoped_refptr<MediaStreamInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318PeerConnectionFactory::CreateLocalMediaStream(const std::string& label) {
319 return MediaStreamProxy::Create(signaling_thread_,
320 MediaStream::Create(label));
321}
322
323talk_base::scoped_refptr<AudioSourceInterface>
324PeerConnectionFactory::CreateAudioSource(
325 const MediaConstraintsInterface* constraints) {
326 CreateAudioSourceParams params(constraints);
327 signaling_thread_->Send(this, MSG_CREATE_AUDIOSOURCE, &params);
328 return params.source;
329}
330
331talk_base::scoped_refptr<VideoSourceInterface>
332PeerConnectionFactory::CreateVideoSource(
333 cricket::VideoCapturer* capturer,
334 const MediaConstraintsInterface* constraints) {
335
336 CreateVideoSourceParams params(capturer,
337 constraints);
338 signaling_thread_->Send(this, MSG_CREATE_VIDEOSOURCE, &params);
339 return params.source;
340}
341
342talk_base::scoped_refptr<VideoTrackInterface>
343PeerConnectionFactory::CreateVideoTrack(
344 const std::string& id,
345 VideoSourceInterface* source) {
346 talk_base::scoped_refptr<VideoTrackInterface> track(
347 VideoTrack::Create(id, source));
348 return VideoTrackProxy::Create(signaling_thread_, track);
349}
350
buildbot@webrtc.org17911dc2014-05-12 18:42:49 +0000351scoped_refptr<AudioTrackInterface> PeerConnectionFactory::CreateAudioTrack(
352 const std::string& id,
353 AudioSourceInterface* source) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354 talk_base::scoped_refptr<AudioTrackInterface> track(
355 AudioTrack::Create(id, source));
356 return AudioTrackProxy::Create(signaling_thread_, track);
357}
358
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000359bool PeerConnectionFactory::StartAecDump(talk_base::PlatformFile file) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000360 StartAecDumpParams params(file);
361 signaling_thread_->Send(this, MSG_START_AEC_DUMP, &params);
362 return params.result;
363}
364
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365cricket::ChannelManager* PeerConnectionFactory::channel_manager() {
366 return channel_manager_.get();
367}
368
369talk_base::Thread* PeerConnectionFactory::signaling_thread() {
370 return signaling_thread_;
371}
372
373talk_base::Thread* PeerConnectionFactory::worker_thread() {
374 return worker_thread_;
375}
376
377} // namespace webrtc