blob: a77fed773509fdc9f9d51de684ad03f8698b59e7 [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";
64static const char kTurnIceServerWithNoUsernameInUri[] =
65 "turn:test.com:1234";
66static const char kTurnPassword[] = "turnpassword";
67static const int kDefaultPort = 3478;
68static const char kTurnUsername[] = "test";
69
70class NullPeerConnectionObserver : public PeerConnectionObserver {
71 public:
72 virtual void OnError() {}
73 virtual void OnMessage(const std::string& msg) {}
74 virtual void OnSignalingMessage(const std::string& msg) {}
75 virtual void OnSignalingChange(
76 PeerConnectionInterface::SignalingState new_state) {}
77 virtual void OnAddStream(MediaStreamInterface* stream) {}
78 virtual void OnRemoveStream(MediaStreamInterface* stream) {}
79 virtual void OnRenegotiationNeeded() {}
80 virtual void OnIceConnectionChange(
81 PeerConnectionInterface::IceConnectionState new_state) {}
82 virtual void OnIceGatheringChange(
83 PeerConnectionInterface::IceGatheringState new_state) {}
84 virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {}
85};
86
87} // namespace
88
89class PeerConnectionFactoryTest : public testing::Test {
90 void SetUp() {
91 factory_ = webrtc::CreatePeerConnectionFactory(talk_base::Thread::Current(),
92 talk_base::Thread::Current(),
93 NULL,
94 NULL,
95 NULL);
96
97 ASSERT_TRUE(factory_.get() != NULL);
98 allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
99 }
100
101 protected:
102 void VerifyStunConfigurations(StunConfigurations stun_config) {
103 webrtc::FakePortAllocatorFactory* allocator =
104 static_cast<webrtc::FakePortAllocatorFactory*>(
105 allocator_factory_.get());
106 ASSERT_TRUE(allocator != NULL);
107 EXPECT_EQ(stun_config.size(), allocator->stun_configs().size());
108 for (size_t i = 0; i < stun_config.size(); ++i) {
109 EXPECT_EQ(stun_config[i].server.ToString(),
110 allocator->stun_configs()[i].server.ToString());
111 }
112 }
113
114 void VerifyTurnConfigurations(TurnConfigurations turn_config) {
115 webrtc::FakePortAllocatorFactory* allocator =
116 static_cast<webrtc::FakePortAllocatorFactory*>(
117 allocator_factory_.get());
118 ASSERT_TRUE(allocator != NULL);
119 EXPECT_EQ(turn_config.size(), allocator->turn_configs().size());
120 for (size_t i = 0; i < turn_config.size(); ++i) {
121 EXPECT_EQ(turn_config[i].server.ToString(),
122 allocator->turn_configs()[i].server.ToString());
123 EXPECT_EQ(turn_config[i].username, allocator->turn_configs()[i].username);
124 EXPECT_EQ(turn_config[i].password, allocator->turn_configs()[i].password);
125 EXPECT_EQ(turn_config[i].transport_type,
126 allocator->turn_configs()[i].transport_type);
127 }
128 }
129
130 talk_base::scoped_refptr<PeerConnectionFactoryInterface> factory_;
131 NullPeerConnectionObserver observer_;
132 talk_base::scoped_refptr<PortAllocatorFactoryInterface> allocator_factory_;
133};
134
135// Verify creation of PeerConnection using internal ADM, video factory and
136// internal libjingle threads.
137TEST(PeerConnectionFactoryTestInternal, CreatePCUsingInternalModules) {
138 talk_base::scoped_refptr<PeerConnectionFactoryInterface> factory(
139 webrtc::CreatePeerConnectionFactory());
140
141 NullPeerConnectionObserver observer;
142 webrtc::PeerConnectionInterface::IceServers servers;
143
144 talk_base::scoped_refptr<PeerConnectionInterface> pc(
145 factory->CreatePeerConnection(servers, NULL, NULL, &observer));
146
147 EXPECT_TRUE(pc.get() != NULL);
148}
149
150// This test verifies creation of PeerConnection with valid STUN and TURN
151// configuration. Also verifies the URL's parsed correctly as expected.
152TEST_F(PeerConnectionFactoryTest, CreatePCUsingIceServers) {
153 webrtc::PeerConnectionInterface::IceServers ice_servers;
154 webrtc::PeerConnectionInterface::IceServer ice_server;
155 ice_server.uri = kStunIceServer;
156 ice_servers.push_back(ice_server);
157 ice_server.uri = kTurnIceServer;
158 ice_server.password = kTurnPassword;
159 ice_servers.push_back(ice_server);
160 talk_base::scoped_refptr<PeerConnectionInterface> pc(
161 factory_->CreatePeerConnection(ice_servers, NULL,
162 allocator_factory_.get(),
163 NULL,
164 &observer_));
165 EXPECT_TRUE(pc.get() != NULL);
166 StunConfigurations stun_configs;
167 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun(
168 "stun.l.google.com", 19302);
169 stun_configs.push_back(stun);
170 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun1(
171 "test.com", 1234);
172 stun_configs.push_back(stun1);
173 VerifyStunConfigurations(stun_configs);
174 TurnConfigurations turn_configs;
175 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn(
176 "test.com", 1234, "test@hello.com", kTurnPassword, "udp");
177 turn_configs.push_back(turn);
178 VerifyTurnConfigurations(turn_configs);
179}
180
181TEST_F(PeerConnectionFactoryTest, CreatePCUsingNoUsernameInUri) {
182 webrtc::PeerConnectionInterface::IceServers ice_servers;
183 webrtc::PeerConnectionInterface::IceServer ice_server;
184 ice_server.uri = kStunIceServer;
185 ice_servers.push_back(ice_server);
186 ice_server.uri = kTurnIceServerWithNoUsernameInUri;
187 ice_server.username = kTurnUsername;
188 ice_server.password = kTurnPassword;
189 ice_servers.push_back(ice_server);
190 talk_base::scoped_refptr<PeerConnectionInterface> pc(
191 factory_->CreatePeerConnection(ice_servers, NULL,
192 allocator_factory_.get(),
193 NULL,
194 &observer_));
195 EXPECT_TRUE(pc.get() != NULL);
196 TurnConfigurations turn_configs;
197 webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn(
198 "test.com", 1234, kTurnUsername, kTurnPassword, "udp");
199 turn_configs.push_back(turn);
200 VerifyTurnConfigurations(turn_configs);
201}
202
203// This test verifies the PeerConnection created properly with TURN url which
204// has transport parameter in it.
205TEST_F(PeerConnectionFactoryTest, CreatePCUsingTurnUrlWithTransportParam) {
206 webrtc::PeerConnectionInterface::IceServers ice_servers;
207 webrtc::PeerConnectionInterface::IceServer ice_server;
208 ice_server.uri = kTurnIceServerWithTransport;
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(
219 "hello.com", kDefaultPort, "test", kTurnPassword, "tcp");
220 turn_configs.push_back(turn);
221 VerifyTurnConfigurations(turn_configs);
222 StunConfigurations stun_configs;
223 webrtc::PortAllocatorFactoryInterface::StunConfiguration stun(
224 "hello.com", kDefaultPort);
225 stun_configs.push_back(stun);
226 VerifyStunConfigurations(stun_configs);
227}
228
229// This test verifies factory failed to create a peerconneciton object when
230// a valid secure TURN url passed. Connecting to a secure TURN server is not
231// supported currently.
232TEST_F(PeerConnectionFactoryTest, CreatePCUsingSecureTurnUrl) {
233 webrtc::PeerConnectionInterface::IceServers ice_servers;
234 webrtc::PeerConnectionInterface::IceServer ice_server;
235 ice_server.uri = kSecureTurnIceServer;
236 ice_server.password = kTurnPassword;
237 ice_servers.push_back(ice_server);
238 talk_base::scoped_refptr<PeerConnectionInterface> pc(
239 factory_->CreatePeerConnection(ice_servers, NULL,
240 allocator_factory_.get(),
241 NULL,
242 &observer_));
243 EXPECT_TRUE(pc.get() == NULL);
244 TurnConfigurations turn_configs;
245 VerifyTurnConfigurations(turn_configs);
246}
247
248// This test verifies the captured stream is rendered locally using a
249// local video track.
250TEST_F(PeerConnectionFactoryTest, LocalRendering) {
251 cricket::FakeVideoCapturer* capturer = new cricket::FakeVideoCapturer();
252 // The source take ownership of |capturer|.
253 talk_base::scoped_refptr<VideoSourceInterface> source(
254 factory_->CreateVideoSource(capturer, NULL));
255 ASSERT_TRUE(source.get() != NULL);
256 talk_base::scoped_refptr<VideoTrackInterface> track(
257 factory_->CreateVideoTrack("testlabel", source));
258 ASSERT_TRUE(track.get() != NULL);
259 FakeVideoTrackRenderer local_renderer(track);
260
261 EXPECT_EQ(0, local_renderer.num_rendered_frames());
262 EXPECT_TRUE(capturer->CaptureFrame());
263 EXPECT_EQ(1, local_renderer.num_rendered_frames());
264
265 track->set_enabled(false);
266 EXPECT_TRUE(capturer->CaptureFrame());
267 EXPECT_EQ(1, local_renderer.num_rendered_frames());
268
269 track->set_enabled(true);
270 EXPECT_TRUE(capturer->CaptureFrame());
271 EXPECT_EQ(2, local_renderer.num_rendered_frames());
272}