OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2004 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/pc/peerconnectionfactory.h" | 11 #include "webrtc/pc/peerconnectionfactory.h" |
12 | 12 |
13 #include <utility> | 13 #include <utility> |
14 | 14 |
15 #include "webrtc/api/audio_codecs/builtin_audio_decoder_factory.h" | |
16 #include "webrtc/api/audio_codecs/builtin_audio_encoder_factory.h" | |
17 #include "webrtc/api/mediaconstraintsinterface.h" | 15 #include "webrtc/api/mediaconstraintsinterface.h" |
18 #include "webrtc/api/mediastreamproxy.h" | 16 #include "webrtc/api/mediastreamproxy.h" |
19 #include "webrtc/api/mediastreamtrackproxy.h" | 17 #include "webrtc/api/mediastreamtrackproxy.h" |
20 #include "webrtc/api/peerconnectionfactoryproxy.h" | 18 #include "webrtc/api/peerconnectionfactoryproxy.h" |
21 #include "webrtc/api/peerconnectionproxy.h" | 19 #include "webrtc/api/peerconnectionproxy.h" |
22 #include "webrtc/api/videosourceproxy.h" | 20 #include "webrtc/api/videosourceproxy.h" |
23 #include "webrtc/base/bind.h" | 21 #include "webrtc/base/bind.h" |
24 #include "webrtc/base/checks.h" | 22 #include "webrtc/base/checks.h" |
25 #include "webrtc/media/engine/webrtcmediaengine.h" | 23 #include "webrtc/logging/rtc_event_log/rtc_event_log.h" |
26 #include "webrtc/media/engine/webrtcvideodecoderfactory.h" | 24 // Adding 'nogncheck' to disable the gn include headers check to support modular |
27 #include "webrtc/media/engine/webrtcvideoencoderfactory.h" | 25 // WebRTC build targets. |
28 #include "webrtc/modules/audio_device/include/audio_device.h" | 26 // TODO(zhihuang): This wouldn't be necessary if the interface and |
| 27 // implementation of the media engine were in separate build targets. |
| 28 #include "webrtc/media/engine/webrtcmediaengine.h" // nogncheck |
| 29 #include "webrtc/media/engine/webrtcvideodecoderfactory.h" // nogncheck |
| 30 #include "webrtc/media/engine/webrtcvideoencoderfactory.h" // nogncheck |
| 31 #include "webrtc/modules/audio_device/include/audio_device.h" // nogncheck |
29 #include "webrtc/p2p/base/basicpacketsocketfactory.h" | 32 #include "webrtc/p2p/base/basicpacketsocketfactory.h" |
30 #include "webrtc/p2p/client/basicportallocator.h" | 33 #include "webrtc/p2p/client/basicportallocator.h" |
31 #include "webrtc/pc/audiotrack.h" | 34 #include "webrtc/pc/audiotrack.h" |
32 #include "webrtc/pc/localaudiosource.h" | 35 #include "webrtc/pc/localaudiosource.h" |
33 #include "webrtc/pc/mediastream.h" | 36 #include "webrtc/pc/mediastream.h" |
34 #include "webrtc/pc/peerconnection.h" | 37 #include "webrtc/pc/peerconnection.h" |
35 #include "webrtc/pc/videocapturertracksource.h" | 38 #include "webrtc/pc/videocapturertracksource.h" |
36 #include "webrtc/pc/videotrack.h" | 39 #include "webrtc/pc/videotrack.h" |
37 | 40 |
38 namespace webrtc { | 41 namespace webrtc { |
39 | 42 |
40 rtc::scoped_refptr<PeerConnectionFactoryInterface> CreatePeerConnectionFactory( | |
41 rtc::scoped_refptr<AudioEncoderFactory> audio_encoder_factory, | |
42 rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory) { | |
43 rtc::scoped_refptr<PeerConnectionFactory> pc_factory( | |
44 new rtc::RefCountedObject<PeerConnectionFactory>(audio_encoder_factory, | |
45 audio_decoder_factory)); | |
46 | |
47 RTC_CHECK(rtc::Thread::Current() == pc_factory->signaling_thread()); | |
48 // The signaling thread is the current thread so we can | |
49 // safely call Initialize directly. | |
50 if (!pc_factory->Initialize()) { | |
51 return nullptr; | |
52 } | |
53 return PeerConnectionFactoryProxy::Create(pc_factory->signaling_thread(), | |
54 pc_factory); | |
55 } | |
56 | |
57 rtc::scoped_refptr<PeerConnectionFactoryInterface> | 43 rtc::scoped_refptr<PeerConnectionFactoryInterface> |
58 CreatePeerConnectionFactory() { | 44 CreateModularPeerConnectionFactory( |
59 return CreatePeerConnectionFactory(CreateBuiltinAudioEncoderFactory(), | |
60 CreateBuiltinAudioDecoderFactory()); | |
61 } | |
62 | |
63 rtc::scoped_refptr<PeerConnectionFactoryInterface> CreatePeerConnectionFactory( | |
64 rtc::Thread* network_thread, | 45 rtc::Thread* network_thread, |
65 rtc::Thread* worker_thread, | 46 rtc::Thread* worker_thread, |
66 rtc::Thread* signaling_thread, | 47 rtc::Thread* signaling_thread, |
67 AudioDeviceModule* default_adm, | |
68 rtc::scoped_refptr<AudioEncoderFactory> audio_encoder_factory, | |
69 rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory, | |
70 cricket::WebRtcVideoEncoderFactory* video_encoder_factory, | |
71 cricket::WebRtcVideoDecoderFactory* video_decoder_factory) { | |
72 return CreatePeerConnectionFactoryWithAudioMixer( | |
73 network_thread, worker_thread, signaling_thread, default_adm, | |
74 audio_encoder_factory, audio_decoder_factory, video_encoder_factory, | |
75 video_decoder_factory, nullptr); | |
76 } | |
77 | |
78 rtc::scoped_refptr<PeerConnectionFactoryInterface> CreatePeerConnectionFactory( | |
79 rtc::Thread* network_thread, | |
80 rtc::Thread* worker_thread, | |
81 rtc::Thread* signaling_thread, | |
82 AudioDeviceModule* default_adm, | |
83 cricket::WebRtcVideoEncoderFactory* encoder_factory, | |
84 cricket::WebRtcVideoDecoderFactory* decoder_factory) { | |
85 return CreatePeerConnectionFactoryWithAudioMixer( | |
86 network_thread, worker_thread, signaling_thread, default_adm, | |
87 encoder_factory, decoder_factory, nullptr); | |
88 } | |
89 | |
90 rtc::scoped_refptr<PeerConnectionFactoryInterface> | |
91 CreatePeerConnectionFactoryWithAudioMixer( | |
92 rtc::Thread* network_thread, | |
93 rtc::Thread* worker_thread, | |
94 rtc::Thread* signaling_thread, | |
95 AudioDeviceModule* default_adm, | 48 AudioDeviceModule* default_adm, |
96 rtc::scoped_refptr<AudioEncoderFactory> audio_encoder_factory, | 49 rtc::scoped_refptr<AudioEncoderFactory> audio_encoder_factory, |
97 rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory, | 50 rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory, |
98 cricket::WebRtcVideoEncoderFactory* video_encoder_factory, | 51 cricket::WebRtcVideoEncoderFactory* video_encoder_factory, |
99 cricket::WebRtcVideoDecoderFactory* video_decoder_factory, | 52 cricket::WebRtcVideoDecoderFactory* video_decoder_factory, |
100 rtc::scoped_refptr<AudioMixer> audio_mixer) { | 53 rtc::scoped_refptr<AudioMixer> audio_mixer, |
| 54 std::unique_ptr<cricket::MediaEngineInterface> media_engine, |
| 55 std::unique_ptr<CallFactoryInterface> call_factory, |
| 56 std::unique_ptr<RtcEventLogFactoryInterface> event_log_factory) { |
101 rtc::scoped_refptr<PeerConnectionFactory> pc_factory( | 57 rtc::scoped_refptr<PeerConnectionFactory> pc_factory( |
102 new rtc::RefCountedObject<PeerConnectionFactory>( | 58 new rtc::RefCountedObject<PeerConnectionFactory>( |
103 network_thread, worker_thread, signaling_thread, default_adm, | 59 network_thread, worker_thread, signaling_thread, default_adm, |
104 audio_encoder_factory, audio_decoder_factory, video_encoder_factory, | 60 audio_encoder_factory, audio_decoder_factory, video_encoder_factory, |
105 video_decoder_factory, audio_mixer)); | 61 video_decoder_factory, audio_mixer, std::move(media_engine), |
| 62 std::move(call_factory), std::move(event_log_factory))); |
106 | 63 |
107 // Call Initialize synchronously but make sure it is executed on | 64 // Call Initialize synchronously but make sure it is executed on |
108 // |signaling_thread|. | 65 // |signaling_thread|. |
109 MethodCall0<PeerConnectionFactory, bool> call( | 66 MethodCall0<PeerConnectionFactory, bool> call( |
110 pc_factory.get(), &PeerConnectionFactory::Initialize); | 67 pc_factory.get(), &PeerConnectionFactory::Initialize); |
111 bool result = call.Marshal(RTC_FROM_HERE, signaling_thread); | 68 bool result = call.Marshal(RTC_FROM_HERE, pc_factory->signaling_thread()); |
112 | 69 |
113 if (!result) { | 70 if (!result) { |
114 return nullptr; | 71 return nullptr; |
115 } | 72 } |
116 return PeerConnectionFactoryProxy::Create(signaling_thread, pc_factory); | 73 return PeerConnectionFactoryProxy::Create(pc_factory->signaling_thread(), |
117 } | 74 pc_factory); |
118 | |
119 rtc::scoped_refptr<PeerConnectionFactoryInterface> | |
120 CreatePeerConnectionFactoryWithAudioMixer( | |
121 rtc::Thread* network_thread, | |
122 rtc::Thread* worker_thread, | |
123 rtc::Thread* signaling_thread, | |
124 AudioDeviceModule* default_adm, | |
125 cricket::WebRtcVideoEncoderFactory* encoder_factory, | |
126 cricket::WebRtcVideoDecoderFactory* decoder_factory, | |
127 rtc::scoped_refptr<AudioMixer> audio_mixer) { | |
128 return CreatePeerConnectionFactoryWithAudioMixer( | |
129 network_thread, worker_thread, signaling_thread, default_adm, | |
130 CreateBuiltinAudioEncoderFactory(), CreateBuiltinAudioDecoderFactory(), | |
131 encoder_factory, decoder_factory, audio_mixer); | |
132 } | 75 } |
133 | 76 |
134 PeerConnectionFactory::PeerConnectionFactory( | 77 PeerConnectionFactory::PeerConnectionFactory( |
135 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory, | |
136 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory) | |
137 : owns_ptrs_(true), | |
138 wraps_current_thread_(false), | |
139 network_thread_(rtc::Thread::CreateWithSocketServer().release()), | |
140 worker_thread_(rtc::Thread::Create().release()), | |
141 signaling_thread_(rtc::Thread::Current()), | |
142 audio_encoder_factory_(audio_encoder_factory), | |
143 audio_decoder_factory_(audio_decoder_factory) { | |
144 if (!signaling_thread_) { | |
145 signaling_thread_ = rtc::ThreadManager::Instance()->WrapCurrentThread(); | |
146 wraps_current_thread_ = true; | |
147 } | |
148 network_thread_->Start(); | |
149 worker_thread_->Start(); | |
150 } | |
151 | |
152 PeerConnectionFactory::PeerConnectionFactory( | |
153 rtc::Thread* network_thread, | 78 rtc::Thread* network_thread, |
154 rtc::Thread* worker_thread, | 79 rtc::Thread* worker_thread, |
155 rtc::Thread* signaling_thread, | 80 rtc::Thread* signaling_thread, |
156 AudioDeviceModule* default_adm, | 81 AudioDeviceModule* default_adm, |
157 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory, | 82 rtc::scoped_refptr<webrtc::AudioEncoderFactory> audio_encoder_factory, |
158 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory, | 83 rtc::scoped_refptr<webrtc::AudioDecoderFactory> audio_decoder_factory, |
159 cricket::WebRtcVideoEncoderFactory* video_encoder_factory, | 84 cricket::WebRtcVideoEncoderFactory* video_encoder_factory, |
160 cricket::WebRtcVideoDecoderFactory* video_decoder_factory, | 85 cricket::WebRtcVideoDecoderFactory* video_decoder_factory, |
161 rtc::scoped_refptr<AudioMixer> audio_mixer) | 86 rtc::scoped_refptr<AudioMixer> audio_mixer, |
162 : owns_ptrs_(false), | 87 std::unique_ptr<cricket::MediaEngineInterface> media_engine, |
163 wraps_current_thread_(false), | 88 std::unique_ptr<webrtc::CallFactoryInterface> call_factory, |
| 89 std::unique_ptr<RtcEventLogFactoryInterface> event_log_factory) |
| 90 : wraps_current_thread_(false), |
164 network_thread_(network_thread), | 91 network_thread_(network_thread), |
165 worker_thread_(worker_thread), | 92 worker_thread_(worker_thread), |
166 signaling_thread_(signaling_thread), | 93 signaling_thread_(signaling_thread), |
167 default_adm_(default_adm), | 94 default_adm_(default_adm), |
168 audio_encoder_factory_(audio_encoder_factory), | 95 audio_encoder_factory_(audio_encoder_factory), |
169 audio_decoder_factory_(audio_decoder_factory), | 96 audio_decoder_factory_(audio_decoder_factory), |
170 video_encoder_factory_(video_encoder_factory), | 97 video_encoder_factory_(video_encoder_factory), |
171 video_decoder_factory_(video_decoder_factory), | 98 video_decoder_factory_(video_decoder_factory), |
172 external_audio_mixer_(audio_mixer) { | 99 external_audio_mixer_(audio_mixer), |
173 RTC_DCHECK(network_thread); | 100 media_engine_(std::move(media_engine)), |
174 RTC_DCHECK(worker_thread); | 101 call_factory_(std::move(call_factory)), |
175 RTC_DCHECK(signaling_thread); | 102 event_log_factory_(std::move(event_log_factory)) { |
| 103 if (!network_thread_) { |
| 104 owned_network_thread_ = rtc::Thread::CreateWithSocketServer(); |
| 105 owned_network_thread_->Start(); |
| 106 network_thread_ = owned_network_thread_.get(); |
| 107 } |
| 108 |
| 109 if (!worker_thread_) { |
| 110 owned_worker_thread_ = rtc::Thread::Create(); |
| 111 owned_worker_thread_->Start(); |
| 112 worker_thread_ = owned_worker_thread_.get(); |
| 113 } |
| 114 |
| 115 if (!signaling_thread_) { |
| 116 signaling_thread_ = rtc::Thread::Current(); |
| 117 if (!signaling_thread_) { |
| 118 // If this thread isn't already wrapped by an rtc::Thread, create a |
| 119 // wrapper and own it in this class. |
| 120 signaling_thread_ = rtc::ThreadManager::Instance()->WrapCurrentThread(); |
| 121 wraps_current_thread_ = true; |
| 122 } |
| 123 } |
| 124 |
176 // TODO: Currently there is no way creating an external adm in | 125 // TODO: Currently there is no way creating an external adm in |
177 // libjingle source tree. So we can 't currently assert if this is NULL. | 126 // libjingle source tree. So we can 't currently assert if this is NULL. |
178 // RTC_DCHECK(default_adm != NULL); | 127 // RTC_DCHECK(default_adm != NULL); |
179 } | 128 } |
180 | 129 |
181 PeerConnectionFactory::~PeerConnectionFactory() { | 130 PeerConnectionFactory::~PeerConnectionFactory() { |
182 RTC_DCHECK(signaling_thread_->IsCurrent()); | 131 RTC_DCHECK(signaling_thread_->IsCurrent()); |
183 channel_manager_.reset(nullptr); | 132 channel_manager_.reset(nullptr); |
184 | 133 |
185 // Make sure |worker_thread_| and |signaling_thread_| outlive | 134 // Make sure |worker_thread_| and |signaling_thread_| outlive |
186 // |default_socket_factory_| and |default_network_manager_|. | 135 // |default_socket_factory_| and |default_network_manager_|. |
187 default_socket_factory_ = nullptr; | 136 default_socket_factory_ = nullptr; |
188 default_network_manager_ = nullptr; | 137 default_network_manager_ = nullptr; |
189 | 138 |
190 if (owns_ptrs_) { | 139 if (wraps_current_thread_) |
191 if (wraps_current_thread_) | 140 rtc::ThreadManager::Instance()->UnwrapCurrentThread(); |
192 rtc::ThreadManager::Instance()->UnwrapCurrentThread(); | |
193 delete worker_thread_; | |
194 delete network_thread_; | |
195 } | |
196 } | 141 } |
197 | 142 |
198 bool PeerConnectionFactory::Initialize() { | 143 bool PeerConnectionFactory::Initialize() { |
199 RTC_DCHECK(signaling_thread_->IsCurrent()); | 144 RTC_DCHECK(signaling_thread_->IsCurrent()); |
200 rtc::InitRandom(rtc::Time32()); | 145 rtc::InitRandom(rtc::Time32()); |
201 | 146 |
202 default_network_manager_.reset(new rtc::BasicNetworkManager()); | 147 default_network_manager_.reset(new rtc::BasicNetworkManager()); |
203 if (!default_network_manager_) { | 148 if (!default_network_manager_) { |
204 return false; | 149 return false; |
205 } | 150 } |
206 | 151 |
207 default_socket_factory_.reset( | 152 default_socket_factory_.reset( |
208 new rtc::BasicPacketSocketFactory(network_thread_)); | 153 new rtc::BasicPacketSocketFactory(network_thread_)); |
209 if (!default_socket_factory_) { | 154 if (!default_socket_factory_) { |
210 return false; | 155 return false; |
211 } | 156 } |
212 | 157 |
213 std::unique_ptr<cricket::MediaEngineInterface> media_engine( | |
214 cricket::WebRtcMediaEngineFactory::Create( | |
215 default_adm_.get(), audio_encoder_factory_, audio_decoder_factory_, | |
216 video_encoder_factory_.get(), video_decoder_factory_.get(), | |
217 external_audio_mixer_)); | |
218 | |
219 channel_manager_.reset(new cricket::ChannelManager( | 158 channel_manager_.reset(new cricket::ChannelManager( |
220 std::move(media_engine), worker_thread_, network_thread_)); | 159 std::move(media_engine_), worker_thread_, network_thread_)); |
221 | 160 |
222 channel_manager_->SetVideoRtxEnabled(true); | 161 channel_manager_->SetVideoRtxEnabled(true); |
223 if (!channel_manager_->Init()) { | 162 if (!channel_manager_->Init()) { |
224 return false; | 163 return false; |
225 } | 164 } |
226 | 165 |
227 return true; | 166 return true; |
228 } | 167 } |
229 | 168 |
230 void PeerConnectionFactory::SetOptions(const Options& options) { | 169 void PeerConnectionFactory::SetOptions(const Options& options) { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 } | 253 } |
315 | 254 |
316 if (!allocator) { | 255 if (!allocator) { |
317 allocator.reset(new cricket::BasicPortAllocator( | 256 allocator.reset(new cricket::BasicPortAllocator( |
318 default_network_manager_.get(), default_socket_factory_.get())); | 257 default_network_manager_.get(), default_socket_factory_.get())); |
319 } | 258 } |
320 network_thread_->Invoke<void>( | 259 network_thread_->Invoke<void>( |
321 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::SetNetworkIgnoreMask, | 260 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::SetNetworkIgnoreMask, |
322 allocator.get(), options_.network_ignore_mask)); | 261 allocator.get(), options_.network_ignore_mask)); |
323 | 262 |
| 263 std::unique_ptr<RtcEventLog> event_log(new RtcEventLogNullImpl()); |
| 264 if (event_log_factory_) { |
| 265 event_log = event_log_factory_->CreateRtcEventLog(); |
| 266 } |
| 267 |
| 268 std::unique_ptr<Call> call = worker_thread_->Invoke<std::unique_ptr<Call>>( |
| 269 RTC_FROM_HERE, |
| 270 rtc::Bind(&PeerConnectionFactory::CreateCall_w, this, event_log.get())); |
| 271 |
324 rtc::scoped_refptr<PeerConnection> pc( | 272 rtc::scoped_refptr<PeerConnection> pc( |
325 new rtc::RefCountedObject<PeerConnection>(this)); | 273 new rtc::RefCountedObject<PeerConnection>(this, std::move(event_log), |
| 274 std::move(call))); |
326 | 275 |
327 if (!pc->Initialize(configuration, std::move(allocator), | 276 if (!pc->Initialize(configuration, std::move(allocator), |
328 std::move(cert_generator), observer)) { | 277 std::move(cert_generator), observer)) { |
329 return nullptr; | 278 return nullptr; |
330 } | 279 } |
331 return PeerConnectionProxy::Create(signaling_thread(), pc); | 280 return PeerConnectionProxy::Create(signaling_thread(), pc); |
332 } | 281 } |
333 | 282 |
334 rtc::scoped_refptr<MediaStreamInterface> | 283 rtc::scoped_refptr<MediaStreamInterface> |
335 PeerConnectionFactory::CreateLocalMediaStream(const std::string& label) { | 284 PeerConnectionFactory::CreateLocalMediaStream(const std::string& label) { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 } | 324 } |
376 | 325 |
377 rtc::Thread* PeerConnectionFactory::worker_thread() { | 326 rtc::Thread* PeerConnectionFactory::worker_thread() { |
378 return worker_thread_; | 327 return worker_thread_; |
379 } | 328 } |
380 | 329 |
381 rtc::Thread* PeerConnectionFactory::network_thread() { | 330 rtc::Thread* PeerConnectionFactory::network_thread() { |
382 return network_thread_; | 331 return network_thread_; |
383 } | 332 } |
384 | 333 |
| 334 std::unique_ptr<Call> PeerConnectionFactory::CreateCall_w( |
| 335 RtcEventLog* event_log) { |
| 336 const int kMinBandwidthBps = 30000; |
| 337 const int kStartBandwidthBps = 300000; |
| 338 const int kMaxBandwidthBps = 2000000; |
| 339 |
| 340 webrtc::Call::Config call_config(event_log); |
| 341 if (!channel_manager_->media_engine() || !call_factory_) { |
| 342 return nullptr; |
| 343 } |
| 344 call_config.audio_state = channel_manager_->media_engine()->GetAudioState(); |
| 345 call_config.bitrate_config.min_bitrate_bps = kMinBandwidthBps; |
| 346 call_config.bitrate_config.start_bitrate_bps = kStartBandwidthBps; |
| 347 call_config.bitrate_config.max_bitrate_bps = kMaxBandwidthBps; |
| 348 |
| 349 return std::unique_ptr<Call>(call_factory_->CreateCall(call_config)); |
| 350 } |
| 351 |
385 } // namespace webrtc | 352 } // namespace webrtc |
OLD | NEW |