blob: 81d864cba21019d63e5d76d04b0a7d64f605c523 [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
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000046using rtc::scoped_refptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047
48namespace {
49
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000050typedef rtc::TypedMessageData<bool> InitMessageData;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000052struct CreatePeerConnectionParams : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053 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
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000073struct CreateAudioSourceParams : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 explicit CreateAudioSourceParams(
75 const webrtc::MediaConstraintsInterface* constraints)
76 : constraints(constraints) {
77 }
78 const webrtc::MediaConstraintsInterface* constraints;
79 scoped_refptr<webrtc::AudioSourceInterface> source;
80};
81
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000082struct CreateVideoSourceParams : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 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
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000093struct StartAecDumpParams : public rtc::MessageData {
94 explicit StartAecDumpParams(rtc::PlatformFile aec_dump_file)
wu@webrtc.orga9890802013-12-13 00:21:03 +000095 : aec_dump_file(aec_dump_file) {
96 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000097 rtc::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.orgd4e598d2014-07-29 17:36:52 +0000114rtc::scoped_refptr<PeerConnectionFactoryInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115CreatePeerConnectionFactory() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000116 rtc::scoped_refptr<PeerConnectionFactory> pc_factory(
117 new rtc::RefCountedObject<PeerConnectionFactory>());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118
119 if (!pc_factory->Initialize()) {
120 return NULL;
121 }
122 return pc_factory;
123}
124
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000125rtc::scoped_refptr<PeerConnectionFactoryInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000127 rtc::Thread* worker_thread,
128 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 AudioDeviceModule* default_adm,
130 cricket::WebRtcVideoEncoderFactory* encoder_factory,
131 cricket::WebRtcVideoDecoderFactory* decoder_factory) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000132 rtc::scoped_refptr<PeerConnectionFactory> pc_factory(
133 new rtc::RefCountedObject<PeerConnectionFactory>(worker_thread,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000134 signaling_thread,
135 default_adm,
136 encoder_factory,
137 decoder_factory));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138 if (!pc_factory->Initialize()) {
139 return NULL;
140 }
141 return pc_factory;
142}
143
144PeerConnectionFactory::PeerConnectionFactory()
145 : owns_ptrs_(true),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000146 signaling_thread_(new rtc::Thread),
147 worker_thread_(new rtc::Thread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 bool result = signaling_thread_->Start();
149 ASSERT(result);
150 result = worker_thread_->Start();
151 ASSERT(result);
152}
153
154PeerConnectionFactory::PeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000155 rtc::Thread* worker_thread,
156 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 AudioDeviceModule* default_adm,
158 cricket::WebRtcVideoEncoderFactory* video_encoder_factory,
159 cricket::WebRtcVideoDecoderFactory* video_decoder_factory)
160 : owns_ptrs_(false),
161 signaling_thread_(signaling_thread),
162 worker_thread_(worker_thread),
163 default_adm_(default_adm),
164 video_encoder_factory_(video_encoder_factory),
165 video_decoder_factory_(video_decoder_factory) {
166 ASSERT(worker_thread != NULL);
167 ASSERT(signaling_thread != NULL);
168 // TODO: Currently there is no way creating an external adm in
169 // libjingle source tree. So we can 't currently assert if this is NULL.
170 // ASSERT(default_adm != NULL);
171}
172
173PeerConnectionFactory::~PeerConnectionFactory() {
174 signaling_thread_->Clear(this);
175 signaling_thread_->Send(this, MSG_TERMINATE_FACTORY);
176 if (owns_ptrs_) {
fischman@webrtc.org29540b12014-04-17 22:54:30 +0000177 delete signaling_thread_;
178 delete worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 }
180}
181
182bool PeerConnectionFactory::Initialize() {
183 InitMessageData result(false);
184 signaling_thread_->Send(this, MSG_INIT_FACTORY, &result);
185 return result.data();
186}
187
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000188void PeerConnectionFactory::OnMessage(rtc::Message* msg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 switch (msg->message_id) {
190 case MSG_INIT_FACTORY: {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000191 InitMessageData* pdata = static_cast<InitMessageData*>(msg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 pdata->data() = Initialize_s();
193 break;
194 }
195 case MSG_TERMINATE_FACTORY: {
196 Terminate_s();
197 break;
198 }
199 case MSG_CREATE_PEERCONNECTION: {
200 CreatePeerConnectionParams* pdata =
wu@webrtc.org91053e72013-08-10 07:18:04 +0000201 static_cast<CreatePeerConnectionParams*> (msg->pdata);
202 pdata->peerconnection = CreatePeerConnection_s(
203 pdata->configuration,
204 pdata->constraints,
205 pdata->allocator_factory,
206 pdata->dtls_identity_service,
207 pdata->observer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 break;
209 }
210 case MSG_CREATE_AUDIOSOURCE: {
211 CreateAudioSourceParams* pdata =
212 static_cast<CreateAudioSourceParams*>(msg->pdata);
213 pdata->source = CreateAudioSource_s(pdata->constraints);
214 break;
215 }
216 case MSG_CREATE_VIDEOSOURCE: {
217 CreateVideoSourceParams* pdata =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000218 static_cast<CreateVideoSourceParams*>(msg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219 pdata->source = CreateVideoSource_s(pdata->capturer, pdata->constraints);
220 break;
221 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000222 case MSG_START_AEC_DUMP: {
223 StartAecDumpParams* pdata =
224 static_cast<StartAecDumpParams*>(msg->pdata);
225 pdata->result = StartAecDump_s(pdata->aec_dump_file);
226 break;
227 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228 }
229}
230
231bool PeerConnectionFactory::Initialize_s() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000232 rtc::InitRandom(rtc::Time());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233
234 allocator_factory_ = PortAllocatorFactory::Create(worker_thread_);
235 if (!allocator_factory_)
236 return false;
237
238 cricket::DummyDeviceManager* device_manager(
239 new cricket::DummyDeviceManager());
240 // TODO: Need to make sure only one VoE is created inside
241 // WebRtcMediaEngine.
buildbot@webrtc.orgbb2d6582014-06-20 14:58:56 +0000242 cricket::WebRtcMediaEngine* webrtc_media_engine(
243 new cricket::WebRtcMediaEngine(default_adm_.get(),
244 NULL, // No secondary adm.
245 video_encoder_factory_.get(),
246 video_decoder_factory_.get()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247
248 channel_manager_.reset(new cricket::ChannelManager(
buildbot@webrtc.orgbb2d6582014-06-20 14:58:56 +0000249 webrtc_media_engine, device_manager, worker_thread_));
stefan@webrtc.org85d27942014-06-09 12:51:39 +0000250 channel_manager_->SetVideoRtxEnabled(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251 if (!channel_manager_->Init()) {
252 return false;
253 }
254 return true;
255}
256
257// Terminate what we created on the signaling thread.
258void PeerConnectionFactory::Terminate_s() {
259 channel_manager_.reset(NULL);
260 allocator_factory_ = NULL;
261}
262
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000263rtc::scoped_refptr<AudioSourceInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264PeerConnectionFactory::CreateAudioSource_s(
265 const MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000266 rtc::scoped_refptr<LocalAudioSource> source(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000267 LocalAudioSource::Create(options_, constraints));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268 return source;
269}
270
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000271rtc::scoped_refptr<VideoSourceInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272PeerConnectionFactory::CreateVideoSource_s(
273 cricket::VideoCapturer* capturer,
274 const MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000275 rtc::scoped_refptr<VideoSource> source(
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000276 VideoSource::Create(channel_manager_.get(), capturer, constraints));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 return VideoSourceProxy::Create(signaling_thread_, source);
278}
279
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000280bool PeerConnectionFactory::StartAecDump_s(rtc::PlatformFile file) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000281 return channel_manager_->StartAecDump(file);
282}
283
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000284rtc::scoped_refptr<PeerConnectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285PeerConnectionFactory::CreatePeerConnection(
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000286 const PeerConnectionInterface::RTCConfiguration& configuration,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287 const MediaConstraintsInterface* constraints,
288 PortAllocatorFactoryInterface* allocator_factory,
289 DTLSIdentityServiceInterface* dtls_identity_service,
290 PeerConnectionObserver* observer) {
291 CreatePeerConnectionParams params(configuration, constraints,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000292 allocator_factory, dtls_identity_service,
293 observer);
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000294 signaling_thread_->Send(
295 this, MSG_CREATE_PEERCONNECTION, &params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296 return params.peerconnection;
297}
298
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000299rtc::scoped_refptr<PeerConnectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300PeerConnectionFactory::CreatePeerConnection_s(
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000301 const PeerConnectionInterface::RTCConfiguration& configuration,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302 const MediaConstraintsInterface* constraints,
303 PortAllocatorFactoryInterface* allocator_factory,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000304 DTLSIdentityServiceInterface* dtls_identity_service,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305 PeerConnectionObserver* observer) {
306 ASSERT(allocator_factory || allocator_factory_);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000307 rtc::scoped_refptr<PeerConnection> pc(
308 new rtc::RefCountedObject<PeerConnection>(this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 if (!pc->Initialize(
310 configuration,
311 constraints,
312 allocator_factory ? allocator_factory : allocator_factory_.get(),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000313 dtls_identity_service,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314 observer)) {
315 return NULL;
316 }
317 return PeerConnectionProxy::Create(signaling_thread(), pc);
318}
319
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000320rtc::scoped_refptr<MediaStreamInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321PeerConnectionFactory::CreateLocalMediaStream(const std::string& label) {
322 return MediaStreamProxy::Create(signaling_thread_,
323 MediaStream::Create(label));
324}
325
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000326rtc::scoped_refptr<AudioSourceInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327PeerConnectionFactory::CreateAudioSource(
328 const MediaConstraintsInterface* constraints) {
329 CreateAudioSourceParams params(constraints);
330 signaling_thread_->Send(this, MSG_CREATE_AUDIOSOURCE, &params);
331 return params.source;
332}
333
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000334rtc::scoped_refptr<VideoSourceInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335PeerConnectionFactory::CreateVideoSource(
336 cricket::VideoCapturer* capturer,
337 const MediaConstraintsInterface* constraints) {
338
339 CreateVideoSourceParams params(capturer,
340 constraints);
341 signaling_thread_->Send(this, MSG_CREATE_VIDEOSOURCE, &params);
342 return params.source;
343}
344
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000345rtc::scoped_refptr<VideoTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346PeerConnectionFactory::CreateVideoTrack(
347 const std::string& id,
348 VideoSourceInterface* source) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000349 rtc::scoped_refptr<VideoTrackInterface> track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350 VideoTrack::Create(id, source));
351 return VideoTrackProxy::Create(signaling_thread_, track);
352}
353
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000354rtc::scoped_refptr<AudioTrackInterface>
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000355PeerConnectionFactory::CreateAudioTrack(const std::string& id,
356 AudioSourceInterface* source) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000357 rtc::scoped_refptr<AudioTrackInterface> track(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358 AudioTrack::Create(id, source));
359 return AudioTrackProxy::Create(signaling_thread_, track);
360}
361
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000362bool PeerConnectionFactory::StartAecDump(rtc::PlatformFile file) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000363 StartAecDumpParams params(file);
364 signaling_thread_->Send(this, MSG_START_AEC_DUMP, &params);
365 return params.result;
366}
367
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368cricket::ChannelManager* PeerConnectionFactory::channel_manager() {
369 return channel_manager_.get();
370}
371
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000372rtc::Thread* PeerConnectionFactory::signaling_thread() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000373 return signaling_thread_;
374}
375
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000376rtc::Thread* PeerConnectionFactory::worker_thread() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377 return worker_thread_;
378}
379
380} // namespace webrtc