henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 32 | #include "talk/app/webrtc/mediastreamproxy.h" |
| 33 | #include "talk/app/webrtc/mediastreamtrackproxy.h" |
| 34 | #include "talk/app/webrtc/peerconnection.h" |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 35 | #include "talk/app/webrtc/peerconnectionfactoryproxy.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 36 | #include "talk/app/webrtc/peerconnectionproxy.h" |
| 37 | #include "talk/app/webrtc/portallocatorfactory.h" |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 38 | #include "talk/app/webrtc/videosource.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 39 | #include "talk/app/webrtc/videosourceproxy.h" |
| 40 | #include "talk/app/webrtc/videotrack.h" |
| 41 | #include "talk/media/devices/dummydevicemanager.h" |
| 42 | #include "talk/media/webrtc/webrtcmediaengine.h" |
| 43 | #include "talk/media/webrtc/webrtcvideodecoderfactory.h" |
| 44 | #include "talk/media/webrtc/webrtcvideoencoderfactory.h" |
jiayl@webrtc.org | 3987b6d | 2014-09-24 17:14:05 +0000 | [diff] [blame] | 45 | #include "webrtc/base/bind.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 46 | #include "webrtc/modules/audio_device/include/audio_device.h" |
| 47 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 48 | namespace webrtc { |
| 49 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 50 | rtc::scoped_refptr<PeerConnectionFactoryInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 51 | CreatePeerConnectionFactory() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 52 | rtc::scoped_refptr<PeerConnectionFactory> pc_factory( |
| 53 | new rtc::RefCountedObject<PeerConnectionFactory>()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 55 | |
| 56 | // Call Initialize synchronously but make sure its executed on |
| 57 | // |signaling_thread|. |
| 58 | MethodCall0<PeerConnectionFactory, bool> call( |
| 59 | pc_factory.get(), |
| 60 | &PeerConnectionFactory::Initialize); |
| 61 | bool result = call.Marshal(pc_factory->signaling_thread()); |
| 62 | |
| 63 | if (!result) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 64 | return NULL; |
| 65 | } |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 66 | return PeerConnectionFactoryProxy::Create(pc_factory->signaling_thread(), |
| 67 | pc_factory); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 68 | } |
| 69 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 70 | rtc::scoped_refptr<PeerConnectionFactoryInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 71 | CreatePeerConnectionFactory( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 72 | rtc::Thread* worker_thread, |
| 73 | rtc::Thread* signaling_thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 74 | AudioDeviceModule* default_adm, |
| 75 | cricket::WebRtcVideoEncoderFactory* encoder_factory, |
| 76 | cricket::WebRtcVideoDecoderFactory* decoder_factory) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 77 | rtc::scoped_refptr<PeerConnectionFactory> pc_factory( |
| 78 | new rtc::RefCountedObject<PeerConnectionFactory>(worker_thread, |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 79 | signaling_thread, |
| 80 | default_adm, |
| 81 | encoder_factory, |
| 82 | decoder_factory)); |
| 83 | |
| 84 | // Call Initialize synchronously but make sure its executed on |
| 85 | // |signaling_thread|. |
| 86 | MethodCall0<PeerConnectionFactory, bool> call( |
| 87 | pc_factory.get(), |
| 88 | &PeerConnectionFactory::Initialize); |
| 89 | bool result = call.Marshal(signaling_thread); |
| 90 | |
| 91 | if (!result) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 92 | return NULL; |
| 93 | } |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 94 | return PeerConnectionFactoryProxy::Create(signaling_thread, pc_factory); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | PeerConnectionFactory::PeerConnectionFactory() |
| 98 | : owns_ptrs_(true), |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 99 | wraps_current_thread_(false), |
| 100 | signaling_thread_(rtc::ThreadManager::Instance()->CurrentThread()), |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 101 | worker_thread_(new rtc::Thread) { |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 102 | if (!signaling_thread_) { |
| 103 | signaling_thread_ = rtc::ThreadManager::Instance()->WrapCurrentThread(); |
| 104 | wraps_current_thread_ = true; |
| 105 | } |
| 106 | worker_thread_->Start(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | PeerConnectionFactory::PeerConnectionFactory( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 110 | rtc::Thread* worker_thread, |
| 111 | rtc::Thread* signaling_thread, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 112 | AudioDeviceModule* default_adm, |
| 113 | cricket::WebRtcVideoEncoderFactory* video_encoder_factory, |
| 114 | cricket::WebRtcVideoDecoderFactory* video_decoder_factory) |
| 115 | : owns_ptrs_(false), |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 116 | wraps_current_thread_(false), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 117 | signaling_thread_(signaling_thread), |
| 118 | worker_thread_(worker_thread), |
| 119 | default_adm_(default_adm), |
| 120 | video_encoder_factory_(video_encoder_factory), |
| 121 | video_decoder_factory_(video_decoder_factory) { |
| 122 | ASSERT(worker_thread != NULL); |
| 123 | ASSERT(signaling_thread != NULL); |
| 124 | // TODO: Currently there is no way creating an external adm in |
| 125 | // libjingle source tree. So we can 't currently assert if this is NULL. |
| 126 | // ASSERT(default_adm != NULL); |
| 127 | } |
| 128 | |
| 129 | PeerConnectionFactory::~PeerConnectionFactory() { |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 130 | DCHECK(signaling_thread_->IsCurrent()); |
| 131 | channel_manager_.reset(NULL); |
| 132 | allocator_factory_ = NULL; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 133 | if (owns_ptrs_) { |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 134 | if (wraps_current_thread_) |
| 135 | rtc::ThreadManager::Instance()->UnwrapCurrentThread(); |
fischman@webrtc.org | 29540b1 | 2014-04-17 22:54:30 +0000 | [diff] [blame] | 136 | delete worker_thread_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | bool PeerConnectionFactory::Initialize() { |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 141 | DCHECK(signaling_thread_->IsCurrent()); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 142 | rtc::InitRandom(rtc::Time()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 143 | |
| 144 | allocator_factory_ = PortAllocatorFactory::Create(worker_thread_); |
| 145 | if (!allocator_factory_) |
| 146 | return false; |
| 147 | |
| 148 | cricket::DummyDeviceManager* device_manager( |
| 149 | new cricket::DummyDeviceManager()); |
| 150 | // TODO: Need to make sure only one VoE is created inside |
| 151 | // WebRtcMediaEngine. |
henrike@webrtc.org | 0481f15 | 2014-08-19 14:56:59 +0000 | [diff] [blame] | 152 | cricket::MediaEngineInterface* media_engine( |
| 153 | cricket::WebRtcMediaEngineFactory::Create(default_adm_.get(), |
| 154 | NULL, // No secondary adm. |
| 155 | video_encoder_factory_.get(), |
| 156 | video_decoder_factory_.get())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 157 | |
| 158 | channel_manager_.reset(new cricket::ChannelManager( |
henrike@webrtc.org | 0481f15 | 2014-08-19 14:56:59 +0000 | [diff] [blame] | 159 | media_engine, device_manager, worker_thread_)); |
stefan@webrtc.org | 85d2794 | 2014-06-09 12:51:39 +0000 | [diff] [blame] | 160 | channel_manager_->SetVideoRtxEnabled(true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 161 | if (!channel_manager_->Init()) { |
| 162 | return false; |
| 163 | } |
| 164 | return true; |
| 165 | } |
| 166 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 167 | rtc::scoped_refptr<AudioSourceInterface> |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 168 | PeerConnectionFactory::CreateAudioSource( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 169 | const MediaConstraintsInterface* constraints) { |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 170 | DCHECK(signaling_thread_->IsCurrent()); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 171 | rtc::scoped_refptr<LocalAudioSource> source( |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 172 | LocalAudioSource::Create(options_, constraints)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 173 | return source; |
| 174 | } |
| 175 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 176 | rtc::scoped_refptr<VideoSourceInterface> |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 177 | PeerConnectionFactory::CreateVideoSource( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 178 | cricket::VideoCapturer* capturer, |
| 179 | const MediaConstraintsInterface* constraints) { |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 180 | DCHECK(signaling_thread_->IsCurrent()); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 181 | rtc::scoped_refptr<VideoSource> source( |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 182 | VideoSource::Create(channel_manager_.get(), capturer, constraints)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 183 | return VideoSourceProxy::Create(signaling_thread_, source); |
| 184 | } |
| 185 | |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 186 | bool PeerConnectionFactory::StartAecDump(rtc::PlatformFile file) { |
| 187 | DCHECK(signaling_thread_->IsCurrent()); |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 188 | return channel_manager_->StartAecDump(file); |
| 189 | } |
| 190 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 191 | rtc::scoped_refptr<PeerConnectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 192 | PeerConnectionFactory::CreatePeerConnection( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 193 | const PeerConnectionInterface::RTCConfiguration& configuration, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 194 | const MediaConstraintsInterface* constraints, |
| 195 | PortAllocatorFactoryInterface* allocator_factory, |
| 196 | DTLSIdentityServiceInterface* dtls_identity_service, |
| 197 | PeerConnectionObserver* observer) { |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 198 | DCHECK(signaling_thread_->IsCurrent()); |
| 199 | DCHECK(allocator_factory || allocator_factory_); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 200 | rtc::scoped_refptr<PeerConnection> pc( |
| 201 | new rtc::RefCountedObject<PeerConnection>(this)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 202 | if (!pc->Initialize( |
| 203 | configuration, |
| 204 | constraints, |
| 205 | allocator_factory ? allocator_factory : allocator_factory_.get(), |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 206 | dtls_identity_service, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 207 | observer)) { |
| 208 | return NULL; |
| 209 | } |
| 210 | return PeerConnectionProxy::Create(signaling_thread(), pc); |
| 211 | } |
| 212 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 213 | rtc::scoped_refptr<MediaStreamInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 214 | PeerConnectionFactory::CreateLocalMediaStream(const std::string& label) { |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 215 | DCHECK(signaling_thread_->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 216 | return MediaStreamProxy::Create(signaling_thread_, |
| 217 | MediaStream::Create(label)); |
| 218 | } |
| 219 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 220 | rtc::scoped_refptr<VideoTrackInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 221 | PeerConnectionFactory::CreateVideoTrack( |
| 222 | const std::string& id, |
| 223 | VideoSourceInterface* source) { |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 224 | DCHECK(signaling_thread_->IsCurrent()); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 225 | rtc::scoped_refptr<VideoTrackInterface> track( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 226 | VideoTrack::Create(id, source)); |
| 227 | return VideoTrackProxy::Create(signaling_thread_, track); |
| 228 | } |
| 229 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 230 | rtc::scoped_refptr<AudioTrackInterface> |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 231 | PeerConnectionFactory::CreateAudioTrack(const std::string& id, |
| 232 | AudioSourceInterface* source) { |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 233 | DCHECK(signaling_thread_->IsCurrent()); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 234 | rtc::scoped_refptr<AudioTrackInterface> track( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 235 | AudioTrack::Create(id, source)); |
| 236 | return AudioTrackProxy::Create(signaling_thread_, track); |
| 237 | } |
| 238 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 239 | cricket::ChannelManager* PeerConnectionFactory::channel_manager() { |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 240 | DCHECK(signaling_thread_->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 241 | return channel_manager_.get(); |
| 242 | } |
| 243 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 244 | rtc::Thread* PeerConnectionFactory::signaling_thread() { |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 245 | // This method can be called on a different thread when the factory is |
| 246 | // created in CreatePeerConnectionFactory(). |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 247 | return signaling_thread_; |
| 248 | } |
| 249 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 250 | rtc::Thread* PeerConnectionFactory::worker_thread() { |
perkj@webrtc.org | 81134d0 | 2015-01-12 08:30:16 +0000 | [diff] [blame^] | 251 | DCHECK(signaling_thread_->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 252 | return worker_thread_; |
| 253 | } |
| 254 | |
| 255 | } // namespace webrtc |