blob: 4ab9e35c891dcc9205c7d9a98e331c5398d778b0 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2012, 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
fischman@webrtc.org33584f92013-07-25 16:43:30 +000019 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021 * 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 <string>
29
30#include "talk/app/webrtc/fakeportallocatorfactory.h"
31#include "talk/app/webrtc/mediastreaminterface.h"
32#include "talk/app/webrtc/peerconnectionfactory.h"
33#include "talk/app/webrtc/videosourceinterface.h"
34#include "talk/app/webrtc/test/fakevideotrackrenderer.h"
35#include "talk/base/gunit.h"
36#include "talk/base/scoped_ptr.h"
37#include "talk/base/thread.h"
38#include "talk/media/base/fakevideocapturer.h"
39#include "talk/media/webrtc/webrtccommon.h"
40#include "talk/media/webrtc/webrtcvoe.h"
41
42using webrtc::FakeVideoTrackRenderer;
43using webrtc::MediaStreamInterface;
44using webrtc::PeerConnectionFactoryInterface;
45using webrtc::PeerConnectionInterface;
46using webrtc::PeerConnectionObserver;
47using webrtc::PortAllocatorFactoryInterface;
48using webrtc::VideoSourceInterface;
49using webrtc::VideoTrackInterface;
50
51namespace {
52
53typedef std::vector<PortAllocatorFactoryInterface::StunConfiguration>
54 StunConfigurations;
55typedef std::vector<PortAllocatorFactoryInterface::TurnConfiguration>
56 TurnConfigurations;
57
58static const char kStunIceServer[] = "stun:stun.l.google.com:19302";
59static const char kTurnIceServer[] = "turn:test%40hello.com@test.com:1234";
60static const char kTurnIceServerWithTransport[] =
61 "turn:test@hello.com?transport=tcp";
62static const char kSecureTurnIceServer[] =
63 "turns:test@hello.com?transport=tcp";
wu@webrtc.org78187522013-10-07 23:32:02 +000064static const char kSecureTurnIceServerWithoutTransportParam[] =
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +000065 "turns:test_no_transport@hello.com:443";
66static const char kSecureTurnIceServerWithoutTransportAndPortParam[] =
wu@webrtc.org78187522013-10-07 23:32:02 +000067 "turns:test_no_transport@hello.com";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068static const char kTurnIceServerWithNoUsernameInUri[] =
69 "turn:test.com:1234";
70static const char kTurnPassword[] = "turnpassword";
wu@webrtc.org91053e72013-08-10 07:18:04 +000071static const int kDefaultStunPort = 3478;
72static const int kDefaultStunTlsPort = 5349;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073static const char kTurnUsername[] = "test";
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +000074static const char kStunIceServerWithIPv4Address[] = "stun:1.2.3.4:1234";
75static const char kStunIceServerWithIPv4AddressWithoutPort[] = "stun:1.2.3.4";
76static const char kStunIceServerWithIPv6Address[] = "stun:[2401:fa00:4::]:1234";
77static const char kStunIceServerWithIPv6AddressWithoutPort[] =
78 "stun:[2401:fa00:4::]";
79static const char kStunIceServerWithInvalidIPv6Address[] =
80 "stun:[2401:fa00:4:::3478";
81static const char kTurnIceServerWithIPv6Address[] =
82 "turn:test@[2401:fa00:4::]:1234";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083
84class NullPeerConnectionObserver : public PeerConnectionObserver {
85 public:
86 virtual void OnError() {}
87 virtual void OnMessage(const std::string& msg) {}
88 virtual void OnSignalingMessage(const std::string& msg) {}
89 virtual void OnSignalingChange(
90 PeerConnectionInterface::SignalingState new_state) {}
91 virtual void OnAddStream(MediaStreamInterface* stream) {}
92 virtual void OnRemoveStream(MediaStreamInterface* stream) {}
93 virtual void OnRenegotiationNeeded() {}
94 virtual void OnIceConnectionChange(
95 PeerConnectionInterface::IceConnectionState new_state) {}
96 virtual void OnIceGatheringChange(
97 PeerConnectionInterface::IceGatheringState new_state) {}
98 virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {}
99};
100
101} // namespace
102
103class PeerConnectionFactoryTest : public testing::Test {
104 void SetUp() {
105 factory_ = webrtc::CreatePeerConnectionFactory(talk_base::Thread::Current(),
106 talk_base::Thread::Current(),
107 NULL,
108 NULL,
109 NULL);
110
111 ASSERT_TRUE(factory_.get() != NULL);
112 allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
113 }
114
115 protected:
116 void VerifyStunConfigurations(StunConfigurations stun_config) {
117 webrtc::FakePortAllocatorFactory* allocator =
118 static_cast<webrtc::FakePortAllocatorFactory*>(
119 allocator_factory_.get());
120 ASSERT_TRUE(allocator != NULL);
121 EXPECT_EQ(stun_config.size(), allocator->stun_configs().size());
122 for (size_t i = 0; i < stun_config.size(); ++i) {
123 EXPECT_EQ(stun_config[i].server.ToString(),
124 allocator->stun_configs()[i].server.ToString());
125 }
126 }
127
128 void VerifyTurnConfigurations(TurnConfigurations turn_config) {
129 webrtc::FakePortAllocatorFactory* allocator =
130 static_cast<webrtc::FakePortAllocatorFactory*>(
131 allocator_factory_.get());
132 ASSERT_TRUE(allocator != NULL);
133 EXPECT_EQ(turn_config.size(), allocator->turn_configs().size());
134 for (size_t i = 0; i < turn_config.size(); ++i) {
135 EXPECT_EQ(turn_config[i].server.ToString(),
136 allocator->turn_configs()[i].server.ToString());
137 EXPECT_EQ(turn_config[i].username, allocator->turn_configs()[i].username);
138 EXPECT_EQ(turn_config[i].password, allocator->turn_configs()[i].password);
139 EXPECT_EQ(turn_config[i].transport_type,
140 allocator->turn_configs()[i].transport_type);
141 }
142 }
143
144 talk_base::scoped_refptr<PeerConnectionFactoryInterface> factory_;
145 NullPeerConnectionObserver observer_;
146 talk_base::scoped_refptr<PortAllocatorFactoryInterface> allocator_factory_;
147};
148
149// Verify creation of PeerConnection using internal ADM, video factory and
150// internal libjingle threads.
151TEST(PeerConnectionFactoryTestInternal, CreatePCUsingInternalModules) {
152 talk_base::scoped_refptr<PeerConnectionFactoryInterface> factory(
153 webrtc::CreatePeerConnectionFactory());
154
155 NullPeerConnectionObserver observer;
156 webrtc::PeerConnectionInterface::IceServers servers;
157
158 talk_base::scoped_refptr<PeerConnectionInterface> pc(
159 factory->CreatePeerConnection(servers, NULL, NULL, &observer));
160
161 EXPECT_TRUE(pc.get() != NULL);
162}
163
164// This test verifies creation of PeerConnection with valid STUN and TURN
165// configuration. Also verifies the URL's parsed correctly as expected.
166TEST_F(PeerConnectionFactoryTest, CreatePCUsingIceServers) {
167 webrtc::PeerConnectionInterface::IceServers ice_servers;
168 webrtc::PeerConnectionInterface::IceServer ice_server;
169 ice_server.uri = kStunIceServer;
170 ice_servers.push_back(ice_server);
171 ice_server.uri = kTurnIceServer;
172 ice_server.password = kTurnPassword;
173 ice_servers.push_back(ice_server);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000174 ice_server.uri = kTurnIceServerWithTransport;
175 ice_server.password = kTurnPassword;
176 ice_servers.push_back(ice_server);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 talk_base::scoped_refptr<PeerConnectionInterface> pc(
178 factory_->CreatePeerConnection(ice_servers, NULL,
179 allocator_factory_.get(),
180 NULL,
181 &observer_));
182 EXPECT_TRUE(pc.get() != NULL);
183 StunConfigurations stun_configs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun1(
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000185 "stun.l.google.com", 19302);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 stun_configs.push_back(stun1);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000187 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun2(
188 "test.com", 1234);
189 stun_configs.push_back(stun2);
190 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun3(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000191 "hello.com", kDefaultStunPort);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000192 stun_configs.push_back(stun3);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 VerifyStunConfigurations(stun_configs);
194 TurnConfigurations turn_configs;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000195 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn1(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000196 "test.com", 1234, "test@hello.com", kTurnPassword, "udp", false);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000197 turn_configs.push_back(turn1);
198 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn2(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000199 "hello.com", kDefaultStunPort, "test", kTurnPassword, "tcp", false);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000200 turn_configs.push_back(turn2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 VerifyTurnConfigurations(turn_configs);
202}
203
204TEST_F(PeerConnectionFactoryTest, CreatePCUsingNoUsernameInUri) {
205 webrtc::PeerConnectionInterface::IceServers ice_servers;
206 webrtc::PeerConnectionInterface::IceServer ice_server;
207 ice_server.uri = kStunIceServer;
208 ice_servers.push_back(ice_server);
209 ice_server.uri = kTurnIceServerWithNoUsernameInUri;
210 ice_server.username = kTurnUsername;
211 ice_server.password = kTurnPassword;
212 ice_servers.push_back(ice_server);
213 talk_base::scoped_refptr<PeerConnectionInterface> pc(
214 factory_->CreatePeerConnection(ice_servers, NULL,
215 allocator_factory_.get(),
216 NULL,
217 &observer_));
218 EXPECT_TRUE(pc.get() != NULL);
219 TurnConfigurations turn_configs;
220 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000221 "test.com", 1234, kTurnUsername, kTurnPassword, "udp", false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000222 turn_configs.push_back(turn);
223 VerifyTurnConfigurations(turn_configs);
224}
225
226// This test verifies the PeerConnection created properly with TURN url which
227// has transport parameter in it.
228TEST_F(PeerConnectionFactoryTest, CreatePCUsingTurnUrlWithTransportParam) {
229 webrtc::PeerConnectionInterface::IceServers ice_servers;
230 webrtc::PeerConnectionInterface::IceServer ice_server;
231 ice_server.uri = kTurnIceServerWithTransport;
232 ice_server.password = kTurnPassword;
233 ice_servers.push_back(ice_server);
234 talk_base::scoped_refptr<PeerConnectionInterface> pc(
235 factory_->CreatePeerConnection(ice_servers, NULL,
236 allocator_factory_.get(),
237 NULL,
238 &observer_));
239 EXPECT_TRUE(pc.get() != NULL);
240 TurnConfigurations turn_configs;
241 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000242 "hello.com", kDefaultStunPort, "test", kTurnPassword, "tcp", false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243 turn_configs.push_back(turn);
244 VerifyTurnConfigurations(turn_configs);
245 StunConfigurations stun_configs;
246 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000247 "hello.com", kDefaultStunPort);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248 stun_configs.push_back(stun);
249 VerifyStunConfigurations(stun_configs);
250}
251
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252TEST_F(PeerConnectionFactoryTest, CreatePCUsingSecureTurnUrl) {
253 webrtc::PeerConnectionInterface::IceServers ice_servers;
254 webrtc::PeerConnectionInterface::IceServer ice_server;
255 ice_server.uri = kSecureTurnIceServer;
256 ice_server.password = kTurnPassword;
257 ice_servers.push_back(ice_server);
wu@webrtc.org78187522013-10-07 23:32:02 +0000258 ice_server.uri = kSecureTurnIceServerWithoutTransportParam;
259 ice_server.password = kTurnPassword;
260 ice_servers.push_back(ice_server);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000261 ice_server.uri = kSecureTurnIceServerWithoutTransportAndPortParam;
262 ice_server.password = kTurnPassword;
263 ice_servers.push_back(ice_server);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 talk_base::scoped_refptr<PeerConnectionInterface> pc(
265 factory_->CreatePeerConnection(ice_servers, NULL,
266 allocator_factory_.get(),
267 NULL,
268 &observer_));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000269 EXPECT_TRUE(pc.get() != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 TurnConfigurations turn_configs;
wu@webrtc.org78187522013-10-07 23:32:02 +0000271 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn1(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000272 "hello.com", kDefaultStunTlsPort, "test", kTurnPassword, "tcp", true);
wu@webrtc.org78187522013-10-07 23:32:02 +0000273 turn_configs.push_back(turn1);
274 // TURNS with transport param should be default to tcp.
275 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn2(
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000276 "hello.com", 443, "test_no_transport", kTurnPassword, "tcp", true);
277 turn_configs.push_back(turn2);
278 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn3(
wu@webrtc.org78187522013-10-07 23:32:02 +0000279 "hello.com", kDefaultStunTlsPort, "test_no_transport",
280 kTurnPassword, "tcp", true);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000281 turn_configs.push_back(turn3);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282 VerifyTurnConfigurations(turn_configs);
283}
284
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000285TEST_F(PeerConnectionFactoryTest, CreatePCUsingIPLiteralAddress) {
286 webrtc::PeerConnectionInterface::IceServers ice_servers;
287 webrtc::PeerConnectionInterface::IceServer ice_server;
288 ice_server.uri = kStunIceServerWithIPv4Address;
289 ice_servers.push_back(ice_server);
290 ice_server.uri = kStunIceServerWithIPv4AddressWithoutPort;
291 ice_servers.push_back(ice_server);
292 ice_server.uri = kStunIceServerWithIPv6Address;
293 ice_servers.push_back(ice_server);
294 ice_server.uri = kStunIceServerWithIPv6AddressWithoutPort;
295 ice_servers.push_back(ice_server);
296 ice_server.uri = kStunIceServerWithInvalidIPv6Address;
297 ice_servers.push_back(ice_server);
298 ice_server.uri = kTurnIceServerWithIPv6Address;
299 ice_server.password = kTurnPassword;
300 ice_servers.push_back(ice_server);
301 talk_base::scoped_refptr<PeerConnectionInterface> pc(
302 factory_->CreatePeerConnection(ice_servers, NULL,
303 allocator_factory_.get(),
304 NULL,
305 &observer_));
306 EXPECT_TRUE(pc.get() != NULL);
307 StunConfigurations stun_configs;
308 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun1(
309 "1.2.3.4", 1234);
310 stun_configs.push_back(stun1);
311 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun2(
312 "1.2.3.4", 3478);
313 stun_configs.push_back(stun2); // Default port
314 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun3(
315 "2401:fa00:4::", 1234);
316 stun_configs.push_back(stun3);
317 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun4(
318 "2401:fa00:4::", 3478);
319 stun_configs.push_back(stun4); // Default port
320 // Turn Address has the same host information as |stun3|.
321 stun_configs.push_back(stun3);
322 VerifyStunConfigurations(stun_configs);
323 TurnConfigurations turn_configs;
324 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn1(
325 "2401:fa00:4::", 1234, "test", kTurnPassword, "udp", false);
326 turn_configs.push_back(turn1);
327 VerifyTurnConfigurations(turn_configs);
328}
329
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330// This test verifies the captured stream is rendered locally using a
331// local video track.
332TEST_F(PeerConnectionFactoryTest, LocalRendering) {
333 cricket::FakeVideoCapturer* capturer = new cricket::FakeVideoCapturer();
334 // The source take ownership of |capturer|.
335 talk_base::scoped_refptr<VideoSourceInterface> source(
336 factory_->CreateVideoSource(capturer, NULL));
337 ASSERT_TRUE(source.get() != NULL);
338 talk_base::scoped_refptr<VideoTrackInterface> track(
339 factory_->CreateVideoTrack("testlabel", source));
340 ASSERT_TRUE(track.get() != NULL);
341 FakeVideoTrackRenderer local_renderer(track);
342
343 EXPECT_EQ(0, local_renderer.num_rendered_frames());
344 EXPECT_TRUE(capturer->CaptureFrame());
345 EXPECT_EQ(1, local_renderer.num_rendered_frames());
346
347 track->set_enabled(false);
348 EXPECT_TRUE(capturer->CaptureFrame());
349 EXPECT_EQ(1, local_renderer.num_rendered_frames());
350
351 track->set_enabled(true);
352 EXPECT_TRUE(capturer->CaptureFrame());
353 EXPECT_EQ(2, local_renderer.num_rendered_frames());
354}