blob: e3def6c241b7c302ee9296a01885d022638d4d05 [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";
72
73class NullPeerConnectionObserver : public PeerConnectionObserver {
74 public:
75 virtual void OnError() {}
76 virtual void OnMessage(const std::string& msg) {}
77 virtual void OnSignalingMessage(const std::string& msg) {}
78 virtual void OnSignalingChange(
79 PeerConnectionInterface::SignalingState new_state) {}
80 virtual void OnAddStream(MediaStreamInterface* stream) {}
81 virtual void OnRemoveStream(MediaStreamInterface* stream) {}
82 virtual void OnRenegotiationNeeded() {}
83 virtual void OnIceConnectionChange(
84 PeerConnectionInterface::IceConnectionState new_state) {}
85 virtual void OnIceGatheringChange(
86 PeerConnectionInterface::IceGatheringState new_state) {}
87 virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {}
88};
89
90} // namespace
91
92class PeerConnectionFactoryTest : public testing::Test {
93 void SetUp() {
94 factory_ = webrtc::CreatePeerConnectionFactory(talk_base::Thread::Current(),
95 talk_base::Thread::Current(),
96 NULL,
97 NULL,
98 NULL);
99
100 ASSERT_TRUE(factory_.get() != NULL);
101 allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
102 }
103
104 protected:
105 void VerifyStunConfigurations(StunConfigurations stun_config) {
106 webrtc::FakePortAllocatorFactory* allocator =
107 static_cast<webrtc::FakePortAllocatorFactory*>(
108 allocator_factory_.get());
109 ASSERT_TRUE(allocator != NULL);
110 EXPECT_EQ(stun_config.size(), allocator->stun_configs().size());
111 for (size_t i = 0; i < stun_config.size(); ++i) {
112 EXPECT_EQ(stun_config[i].server.ToString(),
113 allocator->stun_configs()[i].server.ToString());
114 }
115 }
116
117 void VerifyTurnConfigurations(TurnConfigurations turn_config) {
118 webrtc::FakePortAllocatorFactory* allocator =
119 static_cast<webrtc::FakePortAllocatorFactory*>(
120 allocator_factory_.get());
121 ASSERT_TRUE(allocator != NULL);
122 EXPECT_EQ(turn_config.size(), allocator->turn_configs().size());
123 for (size_t i = 0; i < turn_config.size(); ++i) {
124 EXPECT_EQ(turn_config[i].server.ToString(),
125 allocator->turn_configs()[i].server.ToString());
126 EXPECT_EQ(turn_config[i].username, allocator->turn_configs()[i].username);
127 EXPECT_EQ(turn_config[i].password, allocator->turn_configs()[i].password);
128 EXPECT_EQ(turn_config[i].transport_type,
129 allocator->turn_configs()[i].transport_type);
130 }
131 }
132
133 talk_base::scoped_refptr<PeerConnectionFactoryInterface> factory_;
134 NullPeerConnectionObserver observer_;
135 talk_base::scoped_refptr<PortAllocatorFactoryInterface> allocator_factory_;
136};
137
138// Verify creation of PeerConnection using internal ADM, video factory and
139// internal libjingle threads.
140TEST(PeerConnectionFactoryTestInternal, CreatePCUsingInternalModules) {
141 talk_base::scoped_refptr<PeerConnectionFactoryInterface> factory(
142 webrtc::CreatePeerConnectionFactory());
143
144 NullPeerConnectionObserver observer;
145 webrtc::PeerConnectionInterface::IceServers servers;
146
147 talk_base::scoped_refptr<PeerConnectionInterface> pc(
148 factory->CreatePeerConnection(servers, NULL, NULL, &observer));
149
150 EXPECT_TRUE(pc.get() != NULL);
151}
152
153// This test verifies creation of PeerConnection with valid STUN and TURN
154// configuration. Also verifies the URL's parsed correctly as expected.
155TEST_F(PeerConnectionFactoryTest, CreatePCUsingIceServers) {
156 webrtc::PeerConnectionInterface::IceServers ice_servers;
157 webrtc::PeerConnectionInterface::IceServer ice_server;
158 ice_server.uri = kStunIceServer;
159 ice_servers.push_back(ice_server);
160 ice_server.uri = kTurnIceServer;
161 ice_server.password = kTurnPassword;
162 ice_servers.push_back(ice_server);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000163 ice_server.uri = kTurnIceServerWithTransport;
164 ice_server.password = kTurnPassword;
165 ice_servers.push_back(ice_server);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166 talk_base::scoped_refptr<PeerConnectionInterface> pc(
167 factory_->CreatePeerConnection(ice_servers, NULL,
168 allocator_factory_.get(),
169 NULL,
170 &observer_));
171 EXPECT_TRUE(pc.get() != NULL);
172 StunConfigurations stun_configs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun1(
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000174 "stun.l.google.com", 19302);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 stun_configs.push_back(stun1);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000176 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun2(
177 "test.com", 1234);
178 stun_configs.push_back(stun2);
179 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun3(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000180 "hello.com", kDefaultStunPort);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000181 stun_configs.push_back(stun3);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 VerifyStunConfigurations(stun_configs);
183 TurnConfigurations turn_configs;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000184 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn1(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000185 "test.com", 1234, "test@hello.com", kTurnPassword, "udp", false);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000186 turn_configs.push_back(turn1);
187 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn2(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000188 "hello.com", kDefaultStunPort, "test", kTurnPassword, "tcp", false);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000189 turn_configs.push_back(turn2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 VerifyTurnConfigurations(turn_configs);
191}
192
193TEST_F(PeerConnectionFactoryTest, CreatePCUsingNoUsernameInUri) {
194 webrtc::PeerConnectionInterface::IceServers ice_servers;
195 webrtc::PeerConnectionInterface::IceServer ice_server;
196 ice_server.uri = kStunIceServer;
197 ice_servers.push_back(ice_server);
198 ice_server.uri = kTurnIceServerWithNoUsernameInUri;
199 ice_server.username = kTurnUsername;
200 ice_server.password = kTurnPassword;
201 ice_servers.push_back(ice_server);
202 talk_base::scoped_refptr<PeerConnectionInterface> pc(
203 factory_->CreatePeerConnection(ice_servers, NULL,
204 allocator_factory_.get(),
205 NULL,
206 &observer_));
207 EXPECT_TRUE(pc.get() != NULL);
208 TurnConfigurations turn_configs;
209 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000210 "test.com", 1234, kTurnUsername, kTurnPassword, "udp", false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 turn_configs.push_back(turn);
212 VerifyTurnConfigurations(turn_configs);
213}
214
215// This test verifies the PeerConnection created properly with TURN url which
216// has transport parameter in it.
217TEST_F(PeerConnectionFactoryTest, CreatePCUsingTurnUrlWithTransportParam) {
218 webrtc::PeerConnectionInterface::IceServers ice_servers;
219 webrtc::PeerConnectionInterface::IceServer ice_server;
220 ice_server.uri = kTurnIceServerWithTransport;
221 ice_server.password = kTurnPassword;
222 ice_servers.push_back(ice_server);
223 talk_base::scoped_refptr<PeerConnectionInterface> pc(
224 factory_->CreatePeerConnection(ice_servers, NULL,
225 allocator_factory_.get(),
226 NULL,
227 &observer_));
228 EXPECT_TRUE(pc.get() != NULL);
229 TurnConfigurations turn_configs;
230 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000231 "hello.com", kDefaultStunPort, "test", kTurnPassword, "tcp", false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 turn_configs.push_back(turn);
233 VerifyTurnConfigurations(turn_configs);
234 StunConfigurations stun_configs;
235 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000236 "hello.com", kDefaultStunPort);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237 stun_configs.push_back(stun);
238 VerifyStunConfigurations(stun_configs);
239}
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);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250 talk_base::scoped_refptr<PeerConnectionInterface> pc(
251 factory_->CreatePeerConnection(ice_servers, NULL,
252 allocator_factory_.get(),
253 NULL,
254 &observer_));
wu@webrtc.org91053e72013-08-10 07:18:04 +0000255 EXPECT_TRUE(pc.get() != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256 TurnConfigurations turn_configs;
wu@webrtc.org78187522013-10-07 23:32:02 +0000257 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn1(
wu@webrtc.org91053e72013-08-10 07:18:04 +0000258 "hello.com", kDefaultStunTlsPort, "test", kTurnPassword, "tcp", true);
wu@webrtc.org78187522013-10-07 23:32:02 +0000259 turn_configs.push_back(turn1);
260 // TURNS with transport param should be default to tcp.
261 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn2(
262 "hello.com", kDefaultStunTlsPort, "test_no_transport",
263 kTurnPassword, "tcp", true);
264 turn_configs.push_back(turn2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 VerifyTurnConfigurations(turn_configs);
266}
267
268// This test verifies the captured stream is rendered locally using a
269// local video track.
270TEST_F(PeerConnectionFactoryTest, LocalRendering) {
271 cricket::FakeVideoCapturer* capturer = new cricket::FakeVideoCapturer();
272 // The source take ownership of |capturer|.
273 talk_base::scoped_refptr<VideoSourceInterface> source(
274 factory_->CreateVideoSource(capturer, NULL));
275 ASSERT_TRUE(source.get() != NULL);
276 talk_base::scoped_refptr<VideoTrackInterface> track(
277 factory_->CreateVideoTrack("testlabel", source));
278 ASSERT_TRUE(track.get() != NULL);
279 FakeVideoTrackRenderer local_renderer(track);
280
281 EXPECT_EQ(0, local_renderer.num_rendered_frames());
282 EXPECT_TRUE(capturer->CaptureFrame());
283 EXPECT_EQ(1, local_renderer.num_rendered_frames());
284
285 track->set_enabled(false);
286 EXPECT_TRUE(capturer->CaptureFrame());
287 EXPECT_EQ(1, local_renderer.num_rendered_frames());
288
289 track->set_enabled(true);
290 EXPECT_TRUE(capturer->CaptureFrame());
291 EXPECT_EQ(2, local_renderer.num_rendered_frames());
292}