blob: 9f9d7881a8a6f75a10b5920e733688ca78b4cbb5 [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);
187 VerifyStunConfigurations(stun_configs);
188 TurnConfigurations turn_configs;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000189 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn1(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000190 "test.com", 1234, "test@hello.com", kTurnPassword, "udp", false);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000191 turn_configs.push_back(turn1);
192 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn2(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000193 "hello.com", kDefaultStunPort, "test", kTurnPassword, "tcp", false);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000194 turn_configs.push_back(turn2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 VerifyTurnConfigurations(turn_configs);
196}
197
198TEST_F(PeerConnectionFactoryTest, CreatePCUsingNoUsernameInUri) {
199 webrtc::PeerConnectionInterface::IceServers ice_servers;
200 webrtc::PeerConnectionInterface::IceServer ice_server;
201 ice_server.uri = kStunIceServer;
202 ice_servers.push_back(ice_server);
203 ice_server.uri = kTurnIceServerWithNoUsernameInUri;
204 ice_server.username = kTurnUsername;
205 ice_server.password = kTurnPassword;
206 ice_servers.push_back(ice_server);
207 talk_base::scoped_refptr<PeerConnectionInterface> pc(
208 factory_->CreatePeerConnection(ice_servers, NULL,
209 allocator_factory_.get(),
210 NULL,
211 &observer_));
212 EXPECT_TRUE(pc.get() != NULL);
213 TurnConfigurations turn_configs;
214 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000215 "test.com", 1234, kTurnUsername, kTurnPassword, "udp", false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216 turn_configs.push_back(turn);
217 VerifyTurnConfigurations(turn_configs);
218}
219
220// This test verifies the PeerConnection created properly with TURN url which
221// has transport parameter in it.
222TEST_F(PeerConnectionFactoryTest, CreatePCUsingTurnUrlWithTransportParam) {
223 webrtc::PeerConnectionInterface::IceServers ice_servers;
224 webrtc::PeerConnectionInterface::IceServer ice_server;
225 ice_server.uri = kTurnIceServerWithTransport;
226 ice_server.password = kTurnPassword;
227 ice_servers.push_back(ice_server);
228 talk_base::scoped_refptr<PeerConnectionInterface> pc(
229 factory_->CreatePeerConnection(ice_servers, NULL,
230 allocator_factory_.get(),
231 NULL,
232 &observer_));
233 EXPECT_TRUE(pc.get() != NULL);
234 TurnConfigurations turn_configs;
235 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000236 "hello.com", kDefaultStunPort, "test", kTurnPassword, "tcp", false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237 turn_configs.push_back(turn);
238 VerifyTurnConfigurations(turn_configs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239}
240
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241TEST_F(PeerConnectionFactoryTest, CreatePCUsingSecureTurnUrl) {
242 webrtc::PeerConnectionInterface::IceServers ice_servers;
243 webrtc::PeerConnectionInterface::IceServer ice_server;
244 ice_server.uri = kSecureTurnIceServer;
245 ice_server.password = kTurnPassword;
246 ice_servers.push_back(ice_server);
wu@webrtc.org78187522013-10-07 23:32:02 +0000247 ice_server.uri = kSecureTurnIceServerWithoutTransportParam;
248 ice_server.password = kTurnPassword;
249 ice_servers.push_back(ice_server);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000250 ice_server.uri = kSecureTurnIceServerWithoutTransportAndPortParam;
251 ice_server.password = kTurnPassword;
252 ice_servers.push_back(ice_server);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253 talk_base::scoped_refptr<PeerConnectionInterface> pc(
254 factory_->CreatePeerConnection(ice_servers, NULL,
255 allocator_factory_.get(),
256 NULL,
257 &observer_));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000258 EXPECT_TRUE(pc.get() != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 TurnConfigurations turn_configs;
wu@webrtc.org78187522013-10-07 23:32:02 +0000260 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn1(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000261 "hello.com", kDefaultStunTlsPort, "test", kTurnPassword, "tcp", true);
wu@webrtc.org78187522013-10-07 23:32:02 +0000262 turn_configs.push_back(turn1);
263 // TURNS with transport param should be default to tcp.
264 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn2(
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000265 "hello.com", 443, "test_no_transport", kTurnPassword, "tcp", true);
266 turn_configs.push_back(turn2);
267 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn3(
wu@webrtc.org78187522013-10-07 23:32:02 +0000268 "hello.com", kDefaultStunTlsPort, "test_no_transport",
269 kTurnPassword, "tcp", true);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000270 turn_configs.push_back(turn3);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 VerifyTurnConfigurations(turn_configs);
272}
273
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000274TEST_F(PeerConnectionFactoryTest, CreatePCUsingIPLiteralAddress) {
275 webrtc::PeerConnectionInterface::IceServers ice_servers;
276 webrtc::PeerConnectionInterface::IceServer ice_server;
277 ice_server.uri = kStunIceServerWithIPv4Address;
278 ice_servers.push_back(ice_server);
279 ice_server.uri = kStunIceServerWithIPv4AddressWithoutPort;
280 ice_servers.push_back(ice_server);
281 ice_server.uri = kStunIceServerWithIPv6Address;
282 ice_servers.push_back(ice_server);
283 ice_server.uri = kStunIceServerWithIPv6AddressWithoutPort;
284 ice_servers.push_back(ice_server);
285 ice_server.uri = kStunIceServerWithInvalidIPv6Address;
286 ice_servers.push_back(ice_server);
287 ice_server.uri = kTurnIceServerWithIPv6Address;
288 ice_server.password = kTurnPassword;
289 ice_servers.push_back(ice_server);
290 talk_base::scoped_refptr<PeerConnectionInterface> pc(
291 factory_->CreatePeerConnection(ice_servers, NULL,
292 allocator_factory_.get(),
293 NULL,
294 &observer_));
295 EXPECT_TRUE(pc.get() != NULL);
296 StunConfigurations stun_configs;
297 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun1(
298 "1.2.3.4", 1234);
299 stun_configs.push_back(stun1);
300 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun2(
301 "1.2.3.4", 3478);
302 stun_configs.push_back(stun2); // Default port
303 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun3(
304 "2401:fa00:4::", 1234);
305 stun_configs.push_back(stun3);
306 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun4(
307 "2401:fa00:4::", 3478);
308 stun_configs.push_back(stun4); // Default port
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000309 VerifyStunConfigurations(stun_configs);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000310
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000311 TurnConfigurations turn_configs;
312 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn1(
313 "2401:fa00:4::", 1234, "test", kTurnPassword, "udp", false);
314 turn_configs.push_back(turn1);
315 VerifyTurnConfigurations(turn_configs);
316}
317
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318// This test verifies the captured stream is rendered locally using a
319// local video track.
320TEST_F(PeerConnectionFactoryTest, LocalRendering) {
321 cricket::FakeVideoCapturer* capturer = new cricket::FakeVideoCapturer();
322 // The source take ownership of |capturer|.
323 talk_base::scoped_refptr<VideoSourceInterface> source(
324 factory_->CreateVideoSource(capturer, NULL));
325 ASSERT_TRUE(source.get() != NULL);
326 talk_base::scoped_refptr<VideoTrackInterface> track(
327 factory_->CreateVideoTrack("testlabel", source));
328 ASSERT_TRUE(track.get() != NULL);
329 FakeVideoTrackRenderer local_renderer(track);
330
331 EXPECT_EQ(0, local_renderer.num_rendered_frames());
332 EXPECT_TRUE(capturer->CaptureFrame());
333 EXPECT_EQ(1, local_renderer.num_rendered_frames());
334
335 track->set_enabled(false);
336 EXPECT_TRUE(capturer->CaptureFrame());
337 EXPECT_EQ(1, local_renderer.num_rendered_frames());
338
339 track->set_enabled(true);
340 EXPECT_TRUE(capturer->CaptureFrame());
341 EXPECT_EQ(2, local_renderer.num_rendered_frames());
342}