1 /*
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "pc/peer_connection_factory.h"
12
13 #include <utility>
14 #include <vector>
15
16 #include "api/audio/audio_mixer.h"
17 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
18 #include "api/audio_codecs/builtin_audio_encoder_factory.h"
19 #include "api/create_peerconnection_factory.h"
20 #include "api/data_channel_interface.h"
21 #include "api/jsep.h"
22 #include "api/media_stream_interface.h"
23 #include "api/test/mock_packet_socket_factory.h"
24 #include "api/video_codecs/builtin_video_decoder_factory.h"
25 #include "api/video_codecs/builtin_video_encoder_factory.h"
26 #include "media/base/fake_frame_source.h"
27 #include "modules/audio_device/include/audio_device.h"
28 #include "modules/audio_processing/include/audio_processing.h"
29 #include "p2p/base/fake_port_allocator.h"
30 #include "p2p/base/port.h"
31 #include "p2p/base/port_allocator.h"
32 #include "p2p/base/port_interface.h"
33 #include "pc/test/fake_audio_capture_module.h"
34 #include "pc/test/fake_video_track_source.h"
35 #include "pc/test/mock_peer_connection_observers.h"
36 #include "rtc_base/gunit.h"
37 #include "rtc_base/internal/default_socket_server.h"
38 #include "rtc_base/rtc_certificate_generator.h"
39 #include "rtc_base/socket_address.h"
40 #include "rtc_base/time_utils.h"
41 #include "test/gmock.h"
42 #include "test/gtest.h"
43
44 #ifdef WEBRTC_ANDROID
45 #include "pc/test/android_test_initializer.h"
46 #endif
47 #include "pc/test/fake_rtc_certificate_generator.h"
48 #include "pc/test/fake_video_track_renderer.h"
49
50 using webrtc::DataChannelInterface;
51 using webrtc::FakeVideoTrackRenderer;
52 using webrtc::MediaStreamInterface;
53 using webrtc::PeerConnectionFactoryInterface;
54 using webrtc::PeerConnectionInterface;
55 using webrtc::PeerConnectionObserver;
56 using webrtc::VideoTrackInterface;
57 using webrtc::VideoTrackSourceInterface;
58
59 using ::testing::_;
60 using ::testing::AtLeast;
61 using ::testing::InvokeWithoutArgs;
62 using ::testing::NiceMock;
63 using ::testing::Return;
64 using ::testing::UnorderedElementsAre;
65
66 namespace {
67
68 static const char kStunIceServer[] = "stun:stun.l.google.com:19302";
69 static const char kTurnIceServer[] = "turn:test.com:1234";
70 static const char kTurnIceServerWithTransport[] =
71 "turn:hello.com?transport=tcp";
72 static const char kSecureTurnIceServer[] = "turns:hello.com?transport=tcp";
73 static const char kSecureTurnIceServerWithoutTransportParam[] =
74 "turns:hello.com:443";
75 static const char kSecureTurnIceServerWithoutTransportAndPortParam[] =
76 "turns:hello.com";
77 static const char kTurnIceServerWithNoUsernameInUri[] = "turn:test.com:1234";
78 static const char kTurnPassword[] = "turnpassword";
79 static const int kDefaultStunPort = 3478;
80 static const int kDefaultStunTlsPort = 5349;
81 static const char kTurnUsername[] = "test";
82 static const char kStunIceServerWithIPv4Address[] = "stun:1.2.3.4:1234";
83 static const char kStunIceServerWithIPv4AddressWithoutPort[] = "stun:1.2.3.4";
84 static const char kStunIceServerWithIPv6Address[] = "stun:[2401:fa00:4::]:1234";
85 static const char kStunIceServerWithIPv6AddressWithoutPort[] =
86 "stun:[2401:fa00:4::]";
87 static const char kTurnIceServerWithIPv6Address[] = "turn:[2401:fa00:4::]:1234";
88
89 class NullPeerConnectionObserver : public PeerConnectionObserver {
90 public:
91 virtual ~NullPeerConnectionObserver() = default;
OnSignalingChange(PeerConnectionInterface::SignalingState new_state)92 void OnSignalingChange(
93 PeerConnectionInterface::SignalingState new_state) override {}
OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream)94 void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {}
OnRemoveStream(rtc::scoped_refptr<MediaStreamInterface> stream)95 void OnRemoveStream(
96 rtc::scoped_refptr<MediaStreamInterface> stream) override {}
OnDataChannel(rtc::scoped_refptr<DataChannelInterface> data_channel)97 void OnDataChannel(
98 rtc::scoped_refptr<DataChannelInterface> data_channel) override {}
OnRenegotiationNeeded()99 void OnRenegotiationNeeded() override {}
OnIceConnectionChange(PeerConnectionInterface::IceConnectionState new_state)100 void OnIceConnectionChange(
101 PeerConnectionInterface::IceConnectionState new_state) override {}
OnIceGatheringChange(PeerConnectionInterface::IceGatheringState new_state)102 void OnIceGatheringChange(
103 PeerConnectionInterface::IceGatheringState new_state) override {}
OnIceCandidate(const webrtc::IceCandidateInterface * candidate)104 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
105 }
106 };
107
108 class MockNetworkManager : public rtc::NetworkManager {
109 public:
110 MOCK_METHOD(void, StartUpdating, (), (override));
111 MOCK_METHOD(void, StopUpdating, (), (override));
112 MOCK_METHOD(std::vector<const rtc::Network*>,
113 GetNetworks,
114 (),
115 (const override));
116 MOCK_METHOD(std::vector<const rtc::Network*>,
117 GetAnyAddressNetworks,
118 (),
119 (override));
120 };
121
122 } // namespace
123
124 class PeerConnectionFactoryTest : public ::testing::Test {
125 public:
PeerConnectionFactoryTest()126 PeerConnectionFactoryTest()
127 : socket_server_(rtc::CreateDefaultSocketServer()),
128 main_thread_(socket_server_.get()) {}
129
130 private:
SetUp()131 void SetUp() {
132 #ifdef WEBRTC_ANDROID
133 webrtc::InitializeAndroidObjects();
134 #endif
135 // Use fake audio device module since we're only testing the interface
136 // level, and using a real one could make tests flaky e.g. when run in
137 // parallel.
138 factory_ = webrtc::CreatePeerConnectionFactory(
139 rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(),
140 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
141 FakeAudioCaptureModule::Create()),
142 webrtc::CreateBuiltinAudioEncoderFactory(),
143 webrtc::CreateBuiltinAudioDecoderFactory(),
144 webrtc::CreateBuiltinVideoEncoderFactory(),
145 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
146 nullptr /* audio_processing */);
147
148 ASSERT_TRUE(factory_.get() != NULL);
149 packet_socket_factory_.reset(
150 new rtc::BasicPacketSocketFactory(socket_server_.get()));
151 port_allocator_.reset(new cricket::FakePortAllocator(
152 rtc::Thread::Current(), packet_socket_factory_.get()));
153 raw_port_allocator_ = port_allocator_.get();
154 }
155
156 protected:
VerifyStunServers(cricket::ServerAddresses stun_servers)157 void VerifyStunServers(cricket::ServerAddresses stun_servers) {
158 EXPECT_EQ(stun_servers, raw_port_allocator_->stun_servers());
159 }
160
VerifyTurnServers(std::vector<cricket::RelayServerConfig> turn_servers)161 void VerifyTurnServers(std::vector<cricket::RelayServerConfig> turn_servers) {
162 EXPECT_EQ(turn_servers.size(), raw_port_allocator_->turn_servers().size());
163 for (size_t i = 0; i < turn_servers.size(); ++i) {
164 ASSERT_EQ(1u, turn_servers[i].ports.size());
165 EXPECT_EQ(1u, raw_port_allocator_->turn_servers()[i].ports.size());
166 EXPECT_EQ(
167 turn_servers[i].ports[0].address.ToString(),
168 raw_port_allocator_->turn_servers()[i].ports[0].address.ToString());
169 EXPECT_EQ(turn_servers[i].ports[0].proto,
170 raw_port_allocator_->turn_servers()[i].ports[0].proto);
171 EXPECT_EQ(turn_servers[i].credentials.username,
172 raw_port_allocator_->turn_servers()[i].credentials.username);
173 EXPECT_EQ(turn_servers[i].credentials.password,
174 raw_port_allocator_->turn_servers()[i].credentials.password);
175 }
176 }
177
VerifyAudioCodecCapability(const webrtc::RtpCodecCapability & codec)178 void VerifyAudioCodecCapability(const webrtc::RtpCodecCapability& codec) {
179 EXPECT_EQ(codec.kind, cricket::MEDIA_TYPE_AUDIO);
180 EXPECT_FALSE(codec.name.empty());
181 EXPECT_GT(codec.clock_rate, 0);
182 EXPECT_GT(codec.num_channels, 0);
183 }
184
VerifyVideoCodecCapability(const webrtc::RtpCodecCapability & codec,bool sender)185 void VerifyVideoCodecCapability(const webrtc::RtpCodecCapability& codec,
186 bool sender) {
187 EXPECT_EQ(codec.kind, cricket::MEDIA_TYPE_VIDEO);
188 EXPECT_FALSE(codec.name.empty());
189 EXPECT_GT(codec.clock_rate, 0);
190 if (sender) {
191 if (codec.name == "VP8" || codec.name == "H264") {
192 EXPECT_THAT(codec.scalability_modes,
193 UnorderedElementsAre(webrtc::ScalabilityMode::kL1T1,
194 webrtc::ScalabilityMode::kL1T2,
195 webrtc::ScalabilityMode::kL1T3))
196 << "Codec: " << codec.name;
197 } else if (codec.name == "VP9" || codec.name == "AV1") {
198 EXPECT_THAT(
199 codec.scalability_modes,
200 UnorderedElementsAre(
201 // clang-format off
202 webrtc::ScalabilityMode::kL1T1,
203 webrtc::ScalabilityMode::kL1T2,
204 webrtc::ScalabilityMode::kL1T3,
205 webrtc::ScalabilityMode::kL2T1,
206 webrtc::ScalabilityMode::kL2T1h,
207 webrtc::ScalabilityMode::kL2T1_KEY,
208 webrtc::ScalabilityMode::kL2T2,
209 webrtc::ScalabilityMode::kL2T2h,
210 webrtc::ScalabilityMode::kL2T2_KEY,
211 webrtc::ScalabilityMode::kL2T2_KEY_SHIFT,
212 webrtc::ScalabilityMode::kL2T3,
213 webrtc::ScalabilityMode::kL2T3h,
214 webrtc::ScalabilityMode::kL2T3_KEY,
215 webrtc::ScalabilityMode::kL3T1,
216 webrtc::ScalabilityMode::kL3T1h,
217 webrtc::ScalabilityMode::kL3T1_KEY,
218 webrtc::ScalabilityMode::kL3T2,
219 webrtc::ScalabilityMode::kL3T2h,
220 webrtc::ScalabilityMode::kL3T2_KEY,
221 webrtc::ScalabilityMode::kL3T3,
222 webrtc::ScalabilityMode::kL3T3h,
223 webrtc::ScalabilityMode::kL3T3_KEY,
224 webrtc::ScalabilityMode::kS2T1,
225 webrtc::ScalabilityMode::kS2T1h,
226 webrtc::ScalabilityMode::kS2T2,
227 webrtc::ScalabilityMode::kS2T2h,
228 webrtc::ScalabilityMode::kS2T3,
229 webrtc::ScalabilityMode::kS2T3h,
230 webrtc::ScalabilityMode::kS3T1,
231 webrtc::ScalabilityMode::kS3T1h,
232 webrtc::ScalabilityMode::kS3T2,
233 webrtc::ScalabilityMode::kS3T2h,
234 webrtc::ScalabilityMode::kS3T3,
235 webrtc::ScalabilityMode::kS3T3h)
236 // clang-format on
237 )
238 << "Codec: " << codec.name;
239 } else {
240 EXPECT_TRUE(codec.scalability_modes.empty());
241 }
242 } else {
243 EXPECT_TRUE(codec.scalability_modes.empty());
244 }
245 }
246
247 std::unique_ptr<rtc::SocketServer> socket_server_;
248 rtc::AutoSocketServerThread main_thread_;
249 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory_;
250 NullPeerConnectionObserver observer_;
251 std::unique_ptr<rtc::PacketSocketFactory> packet_socket_factory_;
252 std::unique_ptr<cricket::FakePortAllocator> port_allocator_;
253 // Since the PC owns the port allocator after it's been initialized,
254 // this should only be used when known to be safe.
255 cricket::FakePortAllocator* raw_port_allocator_;
256 };
257
258 // Verify creation of PeerConnection using internal ADM, video factory and
259 // internal libjingle threads.
260 // TODO(henrika): disabling this test since relying on real audio can result in
261 // flaky tests and focus on details that are out of scope for you might expect
262 // for a PeerConnectionFactory unit test.
263 // See https://bugs.chromium.org/p/webrtc/issues/detail?id=7806 for details.
TEST(PeerConnectionFactoryTestInternal,DISABLED_CreatePCUsingInternalModules)264 TEST(PeerConnectionFactoryTestInternal, DISABLED_CreatePCUsingInternalModules) {
265 #ifdef WEBRTC_ANDROID
266 webrtc::InitializeAndroidObjects();
267 #endif
268
269 rtc::scoped_refptr<PeerConnectionFactoryInterface> factory(
270 webrtc::CreatePeerConnectionFactory(
271 nullptr /* network_thread */, nullptr /* worker_thread */,
272 nullptr /* signaling_thread */, nullptr /* default_adm */,
273 webrtc::CreateBuiltinAudioEncoderFactory(),
274 webrtc::CreateBuiltinAudioDecoderFactory(),
275 webrtc::CreateBuiltinVideoEncoderFactory(),
276 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */,
277 nullptr /* audio_processing */));
278
279 NullPeerConnectionObserver observer;
280 webrtc::PeerConnectionInterface::RTCConfiguration config;
281 config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
282
283 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
284 new FakeRTCCertificateGenerator());
285 webrtc::PeerConnectionDependencies pc_dependencies(&observer);
286 pc_dependencies.cert_generator = std::move(cert_generator);
287 auto result =
288 factory->CreatePeerConnectionOrError(config, std::move(pc_dependencies));
289
290 EXPECT_TRUE(result.ok());
291 }
292
TEST_F(PeerConnectionFactoryTest,CheckRtpSenderAudioCapabilities)293 TEST_F(PeerConnectionFactoryTest, CheckRtpSenderAudioCapabilities) {
294 webrtc::RtpCapabilities audio_capabilities =
295 factory_->GetRtpSenderCapabilities(cricket::MEDIA_TYPE_AUDIO);
296 EXPECT_FALSE(audio_capabilities.codecs.empty());
297 for (const auto& codec : audio_capabilities.codecs) {
298 VerifyAudioCodecCapability(codec);
299 }
300 EXPECT_FALSE(audio_capabilities.header_extensions.empty());
301 for (const auto& header_extension : audio_capabilities.header_extensions) {
302 EXPECT_FALSE(header_extension.uri.empty());
303 }
304 }
305
TEST_F(PeerConnectionFactoryTest,CheckRtpSenderVideoCapabilities)306 TEST_F(PeerConnectionFactoryTest, CheckRtpSenderVideoCapabilities) {
307 webrtc::RtpCapabilities video_capabilities =
308 factory_->GetRtpSenderCapabilities(cricket::MEDIA_TYPE_VIDEO);
309 EXPECT_FALSE(video_capabilities.codecs.empty());
310 for (const auto& codec : video_capabilities.codecs) {
311 VerifyVideoCodecCapability(codec, true);
312 }
313 EXPECT_FALSE(video_capabilities.header_extensions.empty());
314 for (const auto& header_extension : video_capabilities.header_extensions) {
315 EXPECT_FALSE(header_extension.uri.empty());
316 }
317 }
318
TEST_F(PeerConnectionFactoryTest,CheckRtpSenderDataCapabilities)319 TEST_F(PeerConnectionFactoryTest, CheckRtpSenderDataCapabilities) {
320 webrtc::RtpCapabilities data_capabilities =
321 factory_->GetRtpSenderCapabilities(cricket::MEDIA_TYPE_DATA);
322 EXPECT_TRUE(data_capabilities.codecs.empty());
323 EXPECT_TRUE(data_capabilities.header_extensions.empty());
324 }
325
TEST_F(PeerConnectionFactoryTest,CheckRtpReceiverAudioCapabilities)326 TEST_F(PeerConnectionFactoryTest, CheckRtpReceiverAudioCapabilities) {
327 webrtc::RtpCapabilities audio_capabilities =
328 factory_->GetRtpReceiverCapabilities(cricket::MEDIA_TYPE_AUDIO);
329 EXPECT_FALSE(audio_capabilities.codecs.empty());
330 for (const auto& codec : audio_capabilities.codecs) {
331 VerifyAudioCodecCapability(codec);
332 }
333 EXPECT_FALSE(audio_capabilities.header_extensions.empty());
334 for (const auto& header_extension : audio_capabilities.header_extensions) {
335 EXPECT_FALSE(header_extension.uri.empty());
336 }
337 }
338
TEST_F(PeerConnectionFactoryTest,CheckRtpReceiverVideoCapabilities)339 TEST_F(PeerConnectionFactoryTest, CheckRtpReceiverVideoCapabilities) {
340 webrtc::RtpCapabilities video_capabilities =
341 factory_->GetRtpReceiverCapabilities(cricket::MEDIA_TYPE_VIDEO);
342 EXPECT_FALSE(video_capabilities.codecs.empty());
343 for (const auto& codec : video_capabilities.codecs) {
344 VerifyVideoCodecCapability(codec, false);
345 }
346 EXPECT_FALSE(video_capabilities.header_extensions.empty());
347 for (const auto& header_extension : video_capabilities.header_extensions) {
348 EXPECT_FALSE(header_extension.uri.empty());
349 }
350 }
351
TEST_F(PeerConnectionFactoryTest,CheckRtpReceiverDataCapabilities)352 TEST_F(PeerConnectionFactoryTest, CheckRtpReceiverDataCapabilities) {
353 webrtc::RtpCapabilities data_capabilities =
354 factory_->GetRtpReceiverCapabilities(cricket::MEDIA_TYPE_DATA);
355 EXPECT_TRUE(data_capabilities.codecs.empty());
356 EXPECT_TRUE(data_capabilities.header_extensions.empty());
357 }
358
359 // This test verifies creation of PeerConnection with valid STUN and TURN
360 // configuration. Also verifies the URL's parsed correctly as expected.
TEST_F(PeerConnectionFactoryTest,CreatePCUsingIceServers)361 TEST_F(PeerConnectionFactoryTest, CreatePCUsingIceServers) {
362 PeerConnectionInterface::RTCConfiguration config;
363 config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
364 webrtc::PeerConnectionInterface::IceServer ice_server;
365 ice_server.uri = kStunIceServer;
366 config.servers.push_back(ice_server);
367 ice_server.uri = kTurnIceServer;
368 ice_server.username = kTurnUsername;
369 ice_server.password = kTurnPassword;
370 config.servers.push_back(ice_server);
371 ice_server.uri = kTurnIceServerWithTransport;
372 ice_server.username = kTurnUsername;
373 ice_server.password = kTurnPassword;
374 config.servers.push_back(ice_server);
375 webrtc::PeerConnectionDependencies pc_dependencies(&observer_);
376 pc_dependencies.cert_generator =
377 std::make_unique<FakeRTCCertificateGenerator>();
378 pc_dependencies.allocator = std::move(port_allocator_);
379 auto result =
380 factory_->CreatePeerConnectionOrError(config, std::move(pc_dependencies));
381 ASSERT_TRUE(result.ok());
382 cricket::ServerAddresses stun_servers;
383 rtc::SocketAddress stun1("stun.l.google.com", 19302);
384 stun_servers.insert(stun1);
385 VerifyStunServers(stun_servers);
386 std::vector<cricket::RelayServerConfig> turn_servers;
387 cricket::RelayServerConfig turn1("test.com", 1234, kTurnUsername,
388 kTurnPassword, cricket::PROTO_UDP);
389 turn_servers.push_back(turn1);
390 cricket::RelayServerConfig turn2("hello.com", kDefaultStunPort, kTurnUsername,
391 kTurnPassword, cricket::PROTO_TCP);
392 turn_servers.push_back(turn2);
393 VerifyTurnServers(turn_servers);
394 }
395
396 // This test verifies creation of PeerConnection with valid STUN and TURN
397 // configuration. Also verifies the list of URL's parsed correctly as expected.
TEST_F(PeerConnectionFactoryTest,CreatePCUsingIceServersUrls)398 TEST_F(PeerConnectionFactoryTest, CreatePCUsingIceServersUrls) {
399 PeerConnectionInterface::RTCConfiguration config;
400 config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
401 webrtc::PeerConnectionInterface::IceServer ice_server;
402 ice_server.urls.push_back(kStunIceServer);
403 ice_server.urls.push_back(kTurnIceServer);
404 ice_server.urls.push_back(kTurnIceServerWithTransport);
405 ice_server.username = kTurnUsername;
406 ice_server.password = kTurnPassword;
407 config.servers.push_back(ice_server);
408 webrtc::PeerConnectionDependencies pc_dependencies(&observer_);
409 pc_dependencies.cert_generator =
410 std::make_unique<FakeRTCCertificateGenerator>();
411 pc_dependencies.allocator = std::move(port_allocator_);
412 auto result =
413 factory_->CreatePeerConnectionOrError(config, std::move(pc_dependencies));
414 ASSERT_TRUE(result.ok());
415 cricket::ServerAddresses stun_servers;
416 rtc::SocketAddress stun1("stun.l.google.com", 19302);
417 stun_servers.insert(stun1);
418 VerifyStunServers(stun_servers);
419 std::vector<cricket::RelayServerConfig> turn_servers;
420 cricket::RelayServerConfig turn1("test.com", 1234, kTurnUsername,
421 kTurnPassword, cricket::PROTO_UDP);
422 turn_servers.push_back(turn1);
423 cricket::RelayServerConfig turn2("hello.com", kDefaultStunPort, kTurnUsername,
424 kTurnPassword, cricket::PROTO_TCP);
425 turn_servers.push_back(turn2);
426 VerifyTurnServers(turn_servers);
427 }
428
TEST_F(PeerConnectionFactoryTest,CreatePCUsingNoUsernameInUri)429 TEST_F(PeerConnectionFactoryTest, CreatePCUsingNoUsernameInUri) {
430 PeerConnectionInterface::RTCConfiguration config;
431 config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
432 webrtc::PeerConnectionInterface::IceServer ice_server;
433 ice_server.uri = kStunIceServer;
434 config.servers.push_back(ice_server);
435 ice_server.uri = kTurnIceServerWithNoUsernameInUri;
436 ice_server.username = kTurnUsername;
437 ice_server.password = kTurnPassword;
438 config.servers.push_back(ice_server);
439 webrtc::PeerConnectionDependencies pc_dependencies(&observer_);
440 pc_dependencies.cert_generator =
441 std::make_unique<FakeRTCCertificateGenerator>();
442 pc_dependencies.allocator = std::move(port_allocator_);
443 auto result =
444 factory_->CreatePeerConnectionOrError(config, std::move(pc_dependencies));
445 ASSERT_TRUE(result.ok());
446 std::vector<cricket::RelayServerConfig> turn_servers;
447 cricket::RelayServerConfig turn("test.com", 1234, kTurnUsername,
448 kTurnPassword, cricket::PROTO_UDP);
449 turn_servers.push_back(turn);
450 VerifyTurnServers(turn_servers);
451 }
452
453 // This test verifies the PeerConnection created properly with TURN url which
454 // has transport parameter in it.
TEST_F(PeerConnectionFactoryTest,CreatePCUsingTurnUrlWithTransportParam)455 TEST_F(PeerConnectionFactoryTest, CreatePCUsingTurnUrlWithTransportParam) {
456 PeerConnectionInterface::RTCConfiguration config;
457 config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
458 webrtc::PeerConnectionInterface::IceServer ice_server;
459 ice_server.uri = kTurnIceServerWithTransport;
460 ice_server.username = kTurnUsername;
461 ice_server.password = kTurnPassword;
462 config.servers.push_back(ice_server);
463 webrtc::PeerConnectionDependencies pc_dependencies(&observer_);
464 pc_dependencies.cert_generator =
465 std::make_unique<FakeRTCCertificateGenerator>();
466 pc_dependencies.allocator = std::move(port_allocator_);
467 auto result =
468 factory_->CreatePeerConnectionOrError(config, std::move(pc_dependencies));
469 ASSERT_TRUE(result.ok());
470 std::vector<cricket::RelayServerConfig> turn_servers;
471 cricket::RelayServerConfig turn("hello.com", kDefaultStunPort, kTurnUsername,
472 kTurnPassword, cricket::PROTO_TCP);
473 turn_servers.push_back(turn);
474 VerifyTurnServers(turn_servers);
475 }
476
TEST_F(PeerConnectionFactoryTest,CreatePCUsingSecureTurnUrl)477 TEST_F(PeerConnectionFactoryTest, CreatePCUsingSecureTurnUrl) {
478 PeerConnectionInterface::RTCConfiguration config;
479 config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
480 webrtc::PeerConnectionInterface::IceServer ice_server;
481 ice_server.uri = kSecureTurnIceServer;
482 ice_server.username = kTurnUsername;
483 ice_server.password = kTurnPassword;
484 config.servers.push_back(ice_server);
485 ice_server.uri = kSecureTurnIceServerWithoutTransportParam;
486 ice_server.username = kTurnUsername;
487 ice_server.password = kTurnPassword;
488 config.servers.push_back(ice_server);
489 ice_server.uri = kSecureTurnIceServerWithoutTransportAndPortParam;
490 ice_server.username = kTurnUsername;
491 ice_server.password = kTurnPassword;
492 config.servers.push_back(ice_server);
493 webrtc::PeerConnectionDependencies pc_dependencies(&observer_);
494 pc_dependencies.cert_generator =
495 std::make_unique<FakeRTCCertificateGenerator>();
496 pc_dependencies.allocator = std::move(port_allocator_);
497 auto result =
498 factory_->CreatePeerConnectionOrError(config, std::move(pc_dependencies));
499 ASSERT_TRUE(result.ok());
500 std::vector<cricket::RelayServerConfig> turn_servers;
501 cricket::RelayServerConfig turn1("hello.com", kDefaultStunTlsPort,
502 kTurnUsername, kTurnPassword,
503 cricket::PROTO_TLS);
504 turn_servers.push_back(turn1);
505 // TURNS with transport param should be default to tcp.
506 cricket::RelayServerConfig turn2("hello.com", 443, kTurnUsername,
507 kTurnPassword, cricket::PROTO_TLS);
508 turn_servers.push_back(turn2);
509 cricket::RelayServerConfig turn3("hello.com", kDefaultStunTlsPort,
510 kTurnUsername, kTurnPassword,
511 cricket::PROTO_TLS);
512 turn_servers.push_back(turn3);
513 VerifyTurnServers(turn_servers);
514 }
515
TEST_F(PeerConnectionFactoryTest,CreatePCUsingIPLiteralAddress)516 TEST_F(PeerConnectionFactoryTest, CreatePCUsingIPLiteralAddress) {
517 PeerConnectionInterface::RTCConfiguration config;
518 config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan;
519 webrtc::PeerConnectionInterface::IceServer ice_server;
520 ice_server.uri = kStunIceServerWithIPv4Address;
521 config.servers.push_back(ice_server);
522 ice_server.uri = kStunIceServerWithIPv4AddressWithoutPort;
523 config.servers.push_back(ice_server);
524 ice_server.uri = kStunIceServerWithIPv6Address;
525 config.servers.push_back(ice_server);
526 ice_server.uri = kStunIceServerWithIPv6AddressWithoutPort;
527 config.servers.push_back(ice_server);
528 ice_server.uri = kTurnIceServerWithIPv6Address;
529 ice_server.username = kTurnUsername;
530 ice_server.password = kTurnPassword;
531 config.servers.push_back(ice_server);
532 webrtc::PeerConnectionDependencies pc_dependencies(&observer_);
533 pc_dependencies.cert_generator =
534 std::make_unique<FakeRTCCertificateGenerator>();
535 pc_dependencies.allocator = std::move(port_allocator_);
536 auto result =
537 factory_->CreatePeerConnectionOrError(config, std::move(pc_dependencies));
538 ASSERT_TRUE(result.ok());
539 cricket::ServerAddresses stun_servers;
540 rtc::SocketAddress stun1("1.2.3.4", 1234);
541 stun_servers.insert(stun1);
542 rtc::SocketAddress stun2("1.2.3.4", 3478);
543 stun_servers.insert(stun2); // Default port
544 rtc::SocketAddress stun3("2401:fa00:4::", 1234);
545 stun_servers.insert(stun3);
546 rtc::SocketAddress stun4("2401:fa00:4::", 3478);
547 stun_servers.insert(stun4); // Default port
548 VerifyStunServers(stun_servers);
549
550 std::vector<cricket::RelayServerConfig> turn_servers;
551 cricket::RelayServerConfig turn1("2401:fa00:4::", 1234, kTurnUsername,
552 kTurnPassword, cricket::PROTO_UDP);
553 turn_servers.push_back(turn1);
554 VerifyTurnServers(turn_servers);
555 }
556
557 // This test verifies the captured stream is rendered locally using a
558 // local video track.
TEST_F(PeerConnectionFactoryTest,LocalRendering)559 TEST_F(PeerConnectionFactoryTest, LocalRendering) {
560 rtc::scoped_refptr<webrtc::FakeVideoTrackSource> source =
561 webrtc::FakeVideoTrackSource::Create(/*is_screencast=*/false);
562
563 cricket::FakeFrameSource frame_source(1280, 720,
564 rtc::kNumMicrosecsPerSec / 30);
565
566 ASSERT_TRUE(source.get() != NULL);
567 rtc::scoped_refptr<VideoTrackInterface> track(
568 factory_->CreateVideoTrack("testlabel", source.get()));
569 ASSERT_TRUE(track.get() != NULL);
570 FakeVideoTrackRenderer local_renderer(track.get());
571
572 EXPECT_EQ(0, local_renderer.num_rendered_frames());
573 source->InjectFrame(frame_source.GetFrame());
574 EXPECT_EQ(1, local_renderer.num_rendered_frames());
575 EXPECT_FALSE(local_renderer.black_frame());
576
577 track->set_enabled(false);
578 source->InjectFrame(frame_source.GetFrame());
579 EXPECT_EQ(2, local_renderer.num_rendered_frames());
580 EXPECT_TRUE(local_renderer.black_frame());
581
582 track->set_enabled(true);
583 source->InjectFrame(frame_source.GetFrame());
584 EXPECT_EQ(3, local_renderer.num_rendered_frames());
585 EXPECT_FALSE(local_renderer.black_frame());
586 }
587
TEST(PeerConnectionFactoryDependenciesTest,UsesNetworkManager)588 TEST(PeerConnectionFactoryDependenciesTest, UsesNetworkManager) {
589 constexpr webrtc::TimeDelta kWaitTimeout = webrtc::TimeDelta::Seconds(10);
590 auto mock_network_manager = std::make_unique<NiceMock<MockNetworkManager>>();
591
592 rtc::Event called;
593 EXPECT_CALL(*mock_network_manager, StartUpdating())
594 .Times(AtLeast(1))
595 .WillRepeatedly(InvokeWithoutArgs([&] { called.Set(); }));
596
597 webrtc::PeerConnectionFactoryDependencies pcf_dependencies;
598 pcf_dependencies.network_manager = std::move(mock_network_manager);
599
600 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pcf =
601 CreateModularPeerConnectionFactory(std::move(pcf_dependencies));
602
603 PeerConnectionInterface::RTCConfiguration config;
604 config.ice_candidate_pool_size = 2;
605 NullPeerConnectionObserver observer;
606 auto pc = pcf->CreatePeerConnectionOrError(
607 config, webrtc::PeerConnectionDependencies(&observer));
608 ASSERT_TRUE(pc.ok());
609
610 called.Wait(kWaitTimeout);
611 }
612
TEST(PeerConnectionFactoryDependenciesTest,UsesPacketSocketFactory)613 TEST(PeerConnectionFactoryDependenciesTest, UsesPacketSocketFactory) {
614 constexpr webrtc::TimeDelta kWaitTimeout = webrtc::TimeDelta::Seconds(10);
615 auto mock_socket_factory =
616 std::make_unique<NiceMock<rtc::MockPacketSocketFactory>>();
617
618 rtc::Event called;
619 EXPECT_CALL(*mock_socket_factory, CreateUdpSocket(_, _, _))
620 .WillOnce(InvokeWithoutArgs([&] {
621 called.Set();
622 return nullptr;
623 }))
624 .WillRepeatedly(Return(nullptr));
625
626 webrtc::PeerConnectionFactoryDependencies pcf_dependencies;
627 pcf_dependencies.packet_socket_factory = std::move(mock_socket_factory);
628
629 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pcf =
630 CreateModularPeerConnectionFactory(std::move(pcf_dependencies));
631
632 // By default, localhost addresses are ignored, which makes tests fail if test
633 // machine is offline.
634 PeerConnectionFactoryInterface::Options options;
635 options.network_ignore_mask = 0;
636 pcf->SetOptions(options);
637
638 PeerConnectionInterface::RTCConfiguration config;
639 config.ice_candidate_pool_size = 2;
640 NullPeerConnectionObserver observer;
641 auto pc = pcf->CreatePeerConnectionOrError(
642 config, webrtc::PeerConnectionDependencies(&observer));
643 ASSERT_TRUE(pc.ok());
644
645 called.Wait(kWaitTimeout);
646 }
647