henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 33584f9 | 2013-07-25 16:43:30 +0000 | [diff] [blame] | 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 21 | * 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 | |
| 42 | using webrtc::FakeVideoTrackRenderer; |
| 43 | using webrtc::MediaStreamInterface; |
| 44 | using webrtc::PeerConnectionFactoryInterface; |
| 45 | using webrtc::PeerConnectionInterface; |
| 46 | using webrtc::PeerConnectionObserver; |
| 47 | using webrtc::PortAllocatorFactoryInterface; |
| 48 | using webrtc::VideoSourceInterface; |
| 49 | using webrtc::VideoTrackInterface; |
| 50 | |
| 51 | namespace { |
| 52 | |
| 53 | typedef std::vector<PortAllocatorFactoryInterface::StunConfiguration> |
| 54 | StunConfigurations; |
| 55 | typedef std::vector<PortAllocatorFactoryInterface::TurnConfiguration> |
| 56 | TurnConfigurations; |
| 57 | |
| 58 | static const char kStunIceServer[] = "stun:stun.l.google.com:19302"; |
| 59 | static const char kTurnIceServer[] = "turn:test%40hello.com@test.com:1234"; |
| 60 | static const char kTurnIceServerWithTransport[] = |
| 61 | "turn:test@hello.com?transport=tcp"; |
| 62 | static const char kSecureTurnIceServer[] = |
| 63 | "turns:test@hello.com?transport=tcp"; |
| 64 | static const char kTurnIceServerWithNoUsernameInUri[] = |
| 65 | "turn:test.com:1234"; |
| 66 | static const char kTurnPassword[] = "turnpassword"; |
| 67 | static const int kDefaultPort = 3478; |
| 68 | static const char kTurnUsername[] = "test"; |
| 69 | |
| 70 | class 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 | |
| 89 | class 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. |
| 137 | TEST(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. |
| 152 | TEST_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); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 160 | ice_server.uri = kTurnIceServerWithTransport; |
| 161 | ice_server.password = kTurnPassword; |
| 162 | ice_servers.push_back(ice_server); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 163 | talk_base::scoped_refptr<PeerConnectionInterface> pc( |
| 164 | factory_->CreatePeerConnection(ice_servers, NULL, |
| 165 | allocator_factory_.get(), |
| 166 | NULL, |
| 167 | &observer_)); |
| 168 | EXPECT_TRUE(pc.get() != NULL); |
| 169 | StunConfigurations stun_configs; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 170 | webrtc::PortAllocatorFactoryInterface::StunConfiguration stun1( |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 171 | "stun.l.google.com", 19302); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 172 | stun_configs.push_back(stun1); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 173 | webrtc::PortAllocatorFactoryInterface::StunConfiguration stun2( |
| 174 | "test.com", 1234); |
| 175 | stun_configs.push_back(stun2); |
| 176 | webrtc::PortAllocatorFactoryInterface::StunConfiguration stun3( |
| 177 | "hello.com", kDefaultPort); |
| 178 | stun_configs.push_back(stun3); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 179 | VerifyStunConfigurations(stun_configs); |
| 180 | TurnConfigurations turn_configs; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 181 | webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn1( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 182 | "test.com", 1234, "test@hello.com", kTurnPassword, "udp"); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 183 | turn_configs.push_back(turn1); |
| 184 | webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn2( |
| 185 | "hello.com", kDefaultPort, "test", kTurnPassword, "tcp"); |
| 186 | turn_configs.push_back(turn2); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 187 | VerifyTurnConfigurations(turn_configs); |
| 188 | } |
| 189 | |
| 190 | TEST_F(PeerConnectionFactoryTest, CreatePCUsingNoUsernameInUri) { |
| 191 | webrtc::PeerConnectionInterface::IceServers ice_servers; |
| 192 | webrtc::PeerConnectionInterface::IceServer ice_server; |
| 193 | ice_server.uri = kStunIceServer; |
| 194 | ice_servers.push_back(ice_server); |
| 195 | ice_server.uri = kTurnIceServerWithNoUsernameInUri; |
| 196 | ice_server.username = kTurnUsername; |
| 197 | ice_server.password = kTurnPassword; |
| 198 | ice_servers.push_back(ice_server); |
| 199 | talk_base::scoped_refptr<PeerConnectionInterface> pc( |
| 200 | factory_->CreatePeerConnection(ice_servers, NULL, |
| 201 | allocator_factory_.get(), |
| 202 | NULL, |
| 203 | &observer_)); |
| 204 | EXPECT_TRUE(pc.get() != NULL); |
| 205 | TurnConfigurations turn_configs; |
| 206 | webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn( |
| 207 | "test.com", 1234, kTurnUsername, kTurnPassword, "udp"); |
| 208 | turn_configs.push_back(turn); |
| 209 | VerifyTurnConfigurations(turn_configs); |
| 210 | } |
| 211 | |
| 212 | // This test verifies the PeerConnection created properly with TURN url which |
| 213 | // has transport parameter in it. |
| 214 | TEST_F(PeerConnectionFactoryTest, CreatePCUsingTurnUrlWithTransportParam) { |
| 215 | webrtc::PeerConnectionInterface::IceServers ice_servers; |
| 216 | webrtc::PeerConnectionInterface::IceServer ice_server; |
| 217 | ice_server.uri = kTurnIceServerWithTransport; |
| 218 | ice_server.password = kTurnPassword; |
| 219 | ice_servers.push_back(ice_server); |
| 220 | talk_base::scoped_refptr<PeerConnectionInterface> pc( |
| 221 | factory_->CreatePeerConnection(ice_servers, NULL, |
| 222 | allocator_factory_.get(), |
| 223 | NULL, |
| 224 | &observer_)); |
| 225 | EXPECT_TRUE(pc.get() != NULL); |
| 226 | TurnConfigurations turn_configs; |
| 227 | webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn( |
| 228 | "hello.com", kDefaultPort, "test", kTurnPassword, "tcp"); |
| 229 | turn_configs.push_back(turn); |
| 230 | VerifyTurnConfigurations(turn_configs); |
| 231 | StunConfigurations stun_configs; |
| 232 | webrtc::PortAllocatorFactoryInterface::StunConfiguration stun( |
| 233 | "hello.com", kDefaultPort); |
| 234 | stun_configs.push_back(stun); |
| 235 | VerifyStunConfigurations(stun_configs); |
| 236 | } |
| 237 | |
| 238 | // This test verifies factory failed to create a peerconneciton object when |
| 239 | // a valid secure TURN url passed. Connecting to a secure TURN server is not |
| 240 | // supported currently. |
| 241 | TEST_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); |
| 247 | talk_base::scoped_refptr<PeerConnectionInterface> pc( |
| 248 | factory_->CreatePeerConnection(ice_servers, NULL, |
| 249 | allocator_factory_.get(), |
| 250 | NULL, |
| 251 | &observer_)); |
| 252 | EXPECT_TRUE(pc.get() == NULL); |
| 253 | TurnConfigurations turn_configs; |
| 254 | VerifyTurnConfigurations(turn_configs); |
| 255 | } |
| 256 | |
| 257 | // This test verifies the captured stream is rendered locally using a |
| 258 | // local video track. |
| 259 | TEST_F(PeerConnectionFactoryTest, LocalRendering) { |
| 260 | cricket::FakeVideoCapturer* capturer = new cricket::FakeVideoCapturer(); |
| 261 | // The source take ownership of |capturer|. |
| 262 | talk_base::scoped_refptr<VideoSourceInterface> source( |
| 263 | factory_->CreateVideoSource(capturer, NULL)); |
| 264 | ASSERT_TRUE(source.get() != NULL); |
| 265 | talk_base::scoped_refptr<VideoTrackInterface> track( |
| 266 | factory_->CreateVideoTrack("testlabel", source)); |
| 267 | ASSERT_TRUE(track.get() != NULL); |
| 268 | FakeVideoTrackRenderer local_renderer(track); |
| 269 | |
| 270 | EXPECT_EQ(0, local_renderer.num_rendered_frames()); |
| 271 | EXPECT_TRUE(capturer->CaptureFrame()); |
| 272 | EXPECT_EQ(1, local_renderer.num_rendered_frames()); |
| 273 | |
| 274 | track->set_enabled(false); |
| 275 | EXPECT_TRUE(capturer->CaptureFrame()); |
| 276 | EXPECT_EQ(1, local_renderer.num_rendered_frames()); |
| 277 | |
| 278 | track->set_enabled(true); |
| 279 | EXPECT_TRUE(capturer->CaptureFrame()); |
| 280 | EXPECT_EQ(2, local_renderer.num_rendered_frames()); |
| 281 | } |