blob: 4f0b7297ea294b354500ec4593f2879206e4f434 [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[] =
65 "turns:test_no_transport@hello.com";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066static const char kTurnIceServerWithNoUsernameInUri[] =
67 "turn:test.com:1234";
68static const char kTurnPassword[] = "turnpassword";
wu@webrtc.org91053e72013-08-10 07:18:04 +000069static const int kDefaultStunPort = 3478;
70static const int kDefaultStunTlsPort = 5349;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071static const char kTurnUsername[] = "test";
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +000072static const char kStunIceServerWithIPv4Address[] = "stun:1.2.3.4:1234";
73static const char kStunIceServerWithIPv4AddressWithoutPort[] = "stun:1.2.3.4";
74static const char kStunIceServerWithIPv6Address[] = "stun:[2401:fa00:4::]:1234";
75static const char kStunIceServerWithIPv6AddressWithoutPort[] =
76 "stun:[2401:fa00:4::]";
77static const char kStunIceServerWithInvalidIPv6Address[] =
78 "stun:[2401:fa00:4:::3478";
79static const char kTurnIceServerWithIPv6Address[] =
80 "turn:test@[2401:fa00:4::]:1234";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081
82class NullPeerConnectionObserver : public PeerConnectionObserver {
83 public:
84 virtual void OnError() {}
85 virtual void OnMessage(const std::string& msg) {}
86 virtual void OnSignalingMessage(const std::string& msg) {}
87 virtual void OnSignalingChange(
88 PeerConnectionInterface::SignalingState new_state) {}
89 virtual void OnAddStream(MediaStreamInterface* stream) {}
90 virtual void OnRemoveStream(MediaStreamInterface* stream) {}
91 virtual void OnRenegotiationNeeded() {}
92 virtual void OnIceConnectionChange(
93 PeerConnectionInterface::IceConnectionState new_state) {}
94 virtual void OnIceGatheringChange(
95 PeerConnectionInterface::IceGatheringState new_state) {}
96 virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {}
97};
98
99} // namespace
100
101class PeerConnectionFactoryTest : public testing::Test {
102 void SetUp() {
103 factory_ = webrtc::CreatePeerConnectionFactory(talk_base::Thread::Current(),
104 talk_base::Thread::Current(),
105 NULL,
106 NULL,
107 NULL);
108
109 ASSERT_TRUE(factory_.get() != NULL);
110 allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
111 }
112
113 protected:
114 void VerifyStunConfigurations(StunConfigurations stun_config) {
115 webrtc::FakePortAllocatorFactory* allocator =
116 static_cast<webrtc::FakePortAllocatorFactory*>(
117 allocator_factory_.get());
118 ASSERT_TRUE(allocator != NULL);
119 EXPECT_EQ(stun_config.size(), allocator->stun_configs().size());
120 for (size_t i = 0; i < stun_config.size(); ++i) {
121 EXPECT_EQ(stun_config[i].server.ToString(),
122 allocator->stun_configs()[i].server.ToString());
123 }
124 }
125
126 void VerifyTurnConfigurations(TurnConfigurations turn_config) {
127 webrtc::FakePortAllocatorFactory* allocator =
128 static_cast<webrtc::FakePortAllocatorFactory*>(
129 allocator_factory_.get());
130 ASSERT_TRUE(allocator != NULL);
131 EXPECT_EQ(turn_config.size(), allocator->turn_configs().size());
132 for (size_t i = 0; i < turn_config.size(); ++i) {
133 EXPECT_EQ(turn_config[i].server.ToString(),
134 allocator->turn_configs()[i].server.ToString());
135 EXPECT_EQ(turn_config[i].username, allocator->turn_configs()[i].username);
136 EXPECT_EQ(turn_config[i].password, allocator->turn_configs()[i].password);
137 EXPECT_EQ(turn_config[i].transport_type,
138 allocator->turn_configs()[i].transport_type);
139 }
140 }
141
142 talk_base::scoped_refptr<PeerConnectionFactoryInterface> factory_;
143 NullPeerConnectionObserver observer_;
144 talk_base::scoped_refptr<PortAllocatorFactoryInterface> allocator_factory_;
145};
146
147// Verify creation of PeerConnection using internal ADM, video factory and
148// internal libjingle threads.
149TEST(PeerConnectionFactoryTestInternal, CreatePCUsingInternalModules) {
150 talk_base::scoped_refptr<PeerConnectionFactoryInterface> factory(
151 webrtc::CreatePeerConnectionFactory());
152
153 NullPeerConnectionObserver observer;
154 webrtc::PeerConnectionInterface::IceServers servers;
155
156 talk_base::scoped_refptr<PeerConnectionInterface> pc(
157 factory->CreatePeerConnection(servers, NULL, NULL, &observer));
158
159 EXPECT_TRUE(pc.get() != NULL);
160}
161
162// This test verifies creation of PeerConnection with valid STUN and TURN
163// configuration. Also verifies the URL's parsed correctly as expected.
164TEST_F(PeerConnectionFactoryTest, CreatePCUsingIceServers) {
165 webrtc::PeerConnectionInterface::IceServers ice_servers;
166 webrtc::PeerConnectionInterface::IceServer ice_server;
167 ice_server.uri = kStunIceServer;
168 ice_servers.push_back(ice_server);
169 ice_server.uri = kTurnIceServer;
170 ice_server.password = kTurnPassword;
171 ice_servers.push_back(ice_server);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000172 ice_server.uri = kTurnIceServerWithTransport;
173 ice_server.password = kTurnPassword;
174 ice_servers.push_back(ice_server);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 talk_base::scoped_refptr<PeerConnectionInterface> pc(
176 factory_->CreatePeerConnection(ice_servers, NULL,
177 allocator_factory_.get(),
178 NULL,
179 &observer_));
180 EXPECT_TRUE(pc.get() != NULL);
181 StunConfigurations stun_configs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun1(
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000183 "stun.l.google.com", 19302);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184 stun_configs.push_back(stun1);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000185 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun2(
186 "test.com", 1234);
187 stun_configs.push_back(stun2);
188 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun3(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000189 "hello.com", kDefaultStunPort);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000190 stun_configs.push_back(stun3);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191 VerifyStunConfigurations(stun_configs);
192 TurnConfigurations turn_configs;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000193 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn1(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000194 "test.com", 1234, "test@hello.com", kTurnPassword, "udp", false);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000195 turn_configs.push_back(turn1);
196 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn2(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000197 "hello.com", kDefaultStunPort, "test", kTurnPassword, "tcp", false);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000198 turn_configs.push_back(turn2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 VerifyTurnConfigurations(turn_configs);
200}
201
202TEST_F(PeerConnectionFactoryTest, CreatePCUsingNoUsernameInUri) {
203 webrtc::PeerConnectionInterface::IceServers ice_servers;
204 webrtc::PeerConnectionInterface::IceServer ice_server;
205 ice_server.uri = kStunIceServer;
206 ice_servers.push_back(ice_server);
207 ice_server.uri = kTurnIceServerWithNoUsernameInUri;
208 ice_server.username = kTurnUsername;
209 ice_server.password = kTurnPassword;
210 ice_servers.push_back(ice_server);
211 talk_base::scoped_refptr<PeerConnectionInterface> pc(
212 factory_->CreatePeerConnection(ice_servers, NULL,
213 allocator_factory_.get(),
214 NULL,
215 &observer_));
216 EXPECT_TRUE(pc.get() != NULL);
217 TurnConfigurations turn_configs;
218 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000219 "test.com", 1234, kTurnUsername, kTurnPassword, "udp", false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220 turn_configs.push_back(turn);
221 VerifyTurnConfigurations(turn_configs);
222}
223
224// This test verifies the PeerConnection created properly with TURN url which
225// has transport parameter in it.
226TEST_F(PeerConnectionFactoryTest, CreatePCUsingTurnUrlWithTransportParam) {
227 webrtc::PeerConnectionInterface::IceServers ice_servers;
228 webrtc::PeerConnectionInterface::IceServer ice_server;
229 ice_server.uri = kTurnIceServerWithTransport;
230 ice_server.password = kTurnPassword;
231 ice_servers.push_back(ice_server);
232 talk_base::scoped_refptr<PeerConnectionInterface> pc(
233 factory_->CreatePeerConnection(ice_servers, NULL,
234 allocator_factory_.get(),
235 NULL,
236 &observer_));
237 EXPECT_TRUE(pc.get() != NULL);
238 TurnConfigurations turn_configs;
239 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000240 "hello.com", kDefaultStunPort, "test", kTurnPassword, "tcp", false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241 turn_configs.push_back(turn);
242 VerifyTurnConfigurations(turn_configs);
243 StunConfigurations stun_configs;
244 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000245 "hello.com", kDefaultStunPort);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246 stun_configs.push_back(stun);
247 VerifyStunConfigurations(stun_configs);
248}
249
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250TEST_F(PeerConnectionFactoryTest, CreatePCUsingSecureTurnUrl) {
251 webrtc::PeerConnectionInterface::IceServers ice_servers;
252 webrtc::PeerConnectionInterface::IceServer ice_server;
253 ice_server.uri = kSecureTurnIceServer;
254 ice_server.password = kTurnPassword;
255 ice_servers.push_back(ice_server);
wu@webrtc.org78187522013-10-07 23:32:02 +0000256 ice_server.uri = kSecureTurnIceServerWithoutTransportParam;
257 ice_server.password = kTurnPassword;
258 ice_servers.push_back(ice_server);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 talk_base::scoped_refptr<PeerConnectionInterface> pc(
260 factory_->CreatePeerConnection(ice_servers, NULL,
261 allocator_factory_.get(),
262 NULL,
263 &observer_));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000264 EXPECT_TRUE(pc.get() != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 TurnConfigurations turn_configs;
wu@webrtc.org78187522013-10-07 23:32:02 +0000266 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn1(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000267 "hello.com", kDefaultStunTlsPort, "test", kTurnPassword, "tcp", true);
wu@webrtc.org78187522013-10-07 23:32:02 +0000268 turn_configs.push_back(turn1);
269 // TURNS with transport param should be default to tcp.
270 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn2(
271 "hello.com", kDefaultStunTlsPort, "test_no_transport",
272 kTurnPassword, "tcp", true);
273 turn_configs.push_back(turn2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 VerifyTurnConfigurations(turn_configs);
275}
276
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000277TEST_F(PeerConnectionFactoryTest, CreatePCUsingIPLiteralAddress) {
278 webrtc::PeerConnectionInterface::IceServers ice_servers;
279 webrtc::PeerConnectionInterface::IceServer ice_server;
280 ice_server.uri = kStunIceServerWithIPv4Address;
281 ice_servers.push_back(ice_server);
282 ice_server.uri = kStunIceServerWithIPv4AddressWithoutPort;
283 ice_servers.push_back(ice_server);
284 ice_server.uri = kStunIceServerWithIPv6Address;
285 ice_servers.push_back(ice_server);
286 ice_server.uri = kStunIceServerWithIPv6AddressWithoutPort;
287 ice_servers.push_back(ice_server);
288 ice_server.uri = kStunIceServerWithInvalidIPv6Address;
289 ice_servers.push_back(ice_server);
290 ice_server.uri = kTurnIceServerWithIPv6Address;
291 ice_server.password = kTurnPassword;
292 ice_servers.push_back(ice_server);
293 talk_base::scoped_refptr<PeerConnectionInterface> pc(
294 factory_->CreatePeerConnection(ice_servers, NULL,
295 allocator_factory_.get(),
296 NULL,
297 &observer_));
298 EXPECT_TRUE(pc.get() != NULL);
299 StunConfigurations stun_configs;
300 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun1(
301 "1.2.3.4", 1234);
302 stun_configs.push_back(stun1);
303 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun2(
304 "1.2.3.4", 3478);
305 stun_configs.push_back(stun2); // Default port
306 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun3(
307 "2401:fa00:4::", 1234);
308 stun_configs.push_back(stun3);
309 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun4(
310 "2401:fa00:4::", 3478);
311 stun_configs.push_back(stun4); // Default port
312 // Turn Address has the same host information as |stun3|.
313 stun_configs.push_back(stun3);
314 VerifyStunConfigurations(stun_configs);
315 TurnConfigurations turn_configs;
316 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn1(
317 "2401:fa00:4::", 1234, "test", kTurnPassword, "udp", false);
318 turn_configs.push_back(turn1);
319 VerifyTurnConfigurations(turn_configs);
320}
321
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322// This test verifies the captured stream is rendered locally using a
323// local video track.
324TEST_F(PeerConnectionFactoryTest, LocalRendering) {
325 cricket::FakeVideoCapturer* capturer = new cricket::FakeVideoCapturer();
326 // The source take ownership of |capturer|.
327 talk_base::scoped_refptr<VideoSourceInterface> source(
328 factory_->CreateVideoSource(capturer, NULL));
329 ASSERT_TRUE(source.get() != NULL);
330 talk_base::scoped_refptr<VideoTrackInterface> track(
331 factory_->CreateVideoTrack("testlabel", source));
332 ASSERT_TRUE(track.get() != NULL);
333 FakeVideoTrackRenderer local_renderer(track);
334
335 EXPECT_EQ(0, local_renderer.num_rendered_frames());
336 EXPECT_TRUE(capturer->CaptureFrame());
337 EXPECT_EQ(1, local_renderer.num_rendered_frames());
338
339 track->set_enabled(false);
340 EXPECT_TRUE(capturer->CaptureFrame());
341 EXPECT_EQ(1, local_renderer.num_rendered_frames());
342
343 track->set_enabled(true);
344 EXPECT_TRUE(capturer->CaptureFrame());
345 EXPECT_EQ(2, local_renderer.num_rendered_frames());
346}