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 |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 20 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 21 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 22 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 23 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include <string> |
| 27 | |
| 28 | #include "talk/app/webrtc/fakeportallocatorfactory.h" |
| 29 | #include "talk/app/webrtc/mediastreaminterface.h" |
| 30 | #include "talk/app/webrtc/peerconnectionfactory.h" |
| 31 | #include "talk/app/webrtc/videosourceinterface.h" |
| 32 | #include "talk/app/webrtc/test/fakevideotrackrenderer.h" |
| 33 | #include "talk/base/gunit.h" |
| 34 | #include "talk/base/scoped_ptr.h" |
| 35 | #include "talk/base/thread.h" |
| 36 | #include "talk/media/base/fakevideocapturer.h" |
| 37 | #include "talk/media/webrtc/webrtccommon.h" |
| 38 | #include "talk/media/webrtc/webrtcvoe.h" |
| 39 | |
| 40 | using webrtc::FakeVideoTrackRenderer; |
| 41 | using webrtc::MediaStreamInterface; |
| 42 | using webrtc::PeerConnectionFactoryInterface; |
| 43 | using webrtc::PeerConnectionInterface; |
| 44 | using webrtc::PeerConnectionObserver; |
| 45 | using webrtc::PortAllocatorFactoryInterface; |
| 46 | using webrtc::VideoSourceInterface; |
| 47 | using webrtc::VideoTrackInterface; |
| 48 | |
| 49 | namespace { |
| 50 | |
| 51 | typedef std::vector<PortAllocatorFactoryInterface::StunConfiguration> |
| 52 | StunConfigurations; |
| 53 | typedef std::vector<PortAllocatorFactoryInterface::TurnConfiguration> |
| 54 | TurnConfigurations; |
| 55 | |
| 56 | static const char kStunIceServer[] = "stun:stun.l.google.com:19302"; |
| 57 | static const char kTurnIceServer[] = "turn:test%40hello.com@test.com:1234"; |
| 58 | static const char kTurnIceServerWithTransport[] = |
| 59 | "turn:test@hello.com?transport=tcp"; |
| 60 | static const char kSecureTurnIceServer[] = |
| 61 | "turns:test@hello.com?transport=tcp"; |
| 62 | static const char kTurnIceServerWithNoUsernameInUri[] = |
| 63 | "turn:test.com:1234"; |
| 64 | static const char kTurnPassword[] = "turnpassword"; |
| 65 | static const int kDefaultPort = 3478; |
| 66 | static const char kTurnUsername[] = "test"; |
| 67 | |
| 68 | class NullPeerConnectionObserver : public PeerConnectionObserver { |
| 69 | public: |
| 70 | virtual void OnError() {} |
| 71 | virtual void OnMessage(const std::string& msg) {} |
| 72 | virtual void OnSignalingMessage(const std::string& msg) {} |
| 73 | virtual void OnSignalingChange( |
| 74 | PeerConnectionInterface::SignalingState new_state) {} |
| 75 | virtual void OnAddStream(MediaStreamInterface* stream) {} |
| 76 | virtual void OnRemoveStream(MediaStreamInterface* stream) {} |
| 77 | virtual void OnRenegotiationNeeded() {} |
| 78 | virtual void OnIceConnectionChange( |
| 79 | PeerConnectionInterface::IceConnectionState new_state) {} |
| 80 | virtual void OnIceGatheringChange( |
| 81 | PeerConnectionInterface::IceGatheringState new_state) {} |
| 82 | virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {} |
| 83 | }; |
| 84 | |
| 85 | } // namespace |
| 86 | |
| 87 | class PeerConnectionFactoryTest : public testing::Test { |
| 88 | void SetUp() { |
| 89 | factory_ = webrtc::CreatePeerConnectionFactory(talk_base::Thread::Current(), |
| 90 | talk_base::Thread::Current(), |
| 91 | NULL, |
| 92 | NULL, |
| 93 | NULL); |
| 94 | |
| 95 | ASSERT_TRUE(factory_.get() != NULL); |
| 96 | allocator_factory_ = webrtc::FakePortAllocatorFactory::Create(); |
| 97 | } |
| 98 | |
| 99 | protected: |
| 100 | void VerifyStunConfigurations(StunConfigurations stun_config) { |
| 101 | webrtc::FakePortAllocatorFactory* allocator = |
| 102 | static_cast<webrtc::FakePortAllocatorFactory*>( |
| 103 | allocator_factory_.get()); |
| 104 | ASSERT_TRUE(allocator != NULL); |
| 105 | EXPECT_EQ(stun_config.size(), allocator->stun_configs().size()); |
| 106 | for (size_t i = 0; i < stun_config.size(); ++i) { |
| 107 | EXPECT_EQ(stun_config[i].server.ToString(), |
| 108 | allocator->stun_configs()[i].server.ToString()); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void VerifyTurnConfigurations(TurnConfigurations turn_config) { |
| 113 | webrtc::FakePortAllocatorFactory* allocator = |
| 114 | static_cast<webrtc::FakePortAllocatorFactory*>( |
| 115 | allocator_factory_.get()); |
| 116 | ASSERT_TRUE(allocator != NULL); |
| 117 | EXPECT_EQ(turn_config.size(), allocator->turn_configs().size()); |
| 118 | for (size_t i = 0; i < turn_config.size(); ++i) { |
| 119 | EXPECT_EQ(turn_config[i].server.ToString(), |
| 120 | allocator->turn_configs()[i].server.ToString()); |
| 121 | EXPECT_EQ(turn_config[i].username, allocator->turn_configs()[i].username); |
| 122 | EXPECT_EQ(turn_config[i].password, allocator->turn_configs()[i].password); |
| 123 | EXPECT_EQ(turn_config[i].transport_type, |
| 124 | allocator->turn_configs()[i].transport_type); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | talk_base::scoped_refptr<PeerConnectionFactoryInterface> factory_; |
| 129 | NullPeerConnectionObserver observer_; |
| 130 | talk_base::scoped_refptr<PortAllocatorFactoryInterface> allocator_factory_; |
| 131 | }; |
| 132 | |
| 133 | // Verify creation of PeerConnection using internal ADM, video factory and |
| 134 | // internal libjingle threads. |
| 135 | TEST(PeerConnectionFactoryTestInternal, CreatePCUsingInternalModules) { |
| 136 | talk_base::scoped_refptr<PeerConnectionFactoryInterface> factory( |
| 137 | webrtc::CreatePeerConnectionFactory()); |
| 138 | |
| 139 | NullPeerConnectionObserver observer; |
| 140 | webrtc::PeerConnectionInterface::IceServers servers; |
| 141 | |
| 142 | talk_base::scoped_refptr<PeerConnectionInterface> pc( |
| 143 | factory->CreatePeerConnection(servers, NULL, NULL, &observer)); |
| 144 | |
| 145 | EXPECT_TRUE(pc.get() != NULL); |
| 146 | } |
| 147 | |
| 148 | // This test verifies creation of PeerConnection with valid STUN and TURN |
| 149 | // configuration. Also verifies the URL's parsed correctly as expected. |
| 150 | TEST_F(PeerConnectionFactoryTest, CreatePCUsingIceServers) { |
| 151 | webrtc::PeerConnectionInterface::IceServers ice_servers; |
| 152 | webrtc::PeerConnectionInterface::IceServer ice_server; |
| 153 | ice_server.uri = kStunIceServer; |
| 154 | ice_servers.push_back(ice_server); |
| 155 | ice_server.uri = kTurnIceServer; |
| 156 | ice_server.password = kTurnPassword; |
| 157 | ice_servers.push_back(ice_server); |
| 158 | talk_base::scoped_refptr<PeerConnectionInterface> pc( |
| 159 | factory_->CreatePeerConnection(ice_servers, NULL, |
| 160 | allocator_factory_.get(), |
| 161 | NULL, |
| 162 | &observer_)); |
| 163 | EXPECT_TRUE(pc.get() != NULL); |
| 164 | StunConfigurations stun_configs; |
| 165 | webrtc::PortAllocatorFactoryInterface::StunConfiguration stun( |
| 166 | "stun.l.google.com", 19302); |
| 167 | stun_configs.push_back(stun); |
| 168 | webrtc::PortAllocatorFactoryInterface::StunConfiguration stun1( |
| 169 | "test.com", 1234); |
| 170 | stun_configs.push_back(stun1); |
| 171 | VerifyStunConfigurations(stun_configs); |
| 172 | TurnConfigurations turn_configs; |
| 173 | webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn( |
| 174 | "test.com", 1234, "test@hello.com", kTurnPassword, "udp"); |
| 175 | turn_configs.push_back(turn); |
| 176 | VerifyTurnConfigurations(turn_configs); |
| 177 | } |
| 178 | |
| 179 | TEST_F(PeerConnectionFactoryTest, CreatePCUsingNoUsernameInUri) { |
| 180 | webrtc::PeerConnectionInterface::IceServers ice_servers; |
| 181 | webrtc::PeerConnectionInterface::IceServer ice_server; |
| 182 | ice_server.uri = kStunIceServer; |
| 183 | ice_servers.push_back(ice_server); |
| 184 | ice_server.uri = kTurnIceServerWithNoUsernameInUri; |
| 185 | ice_server.username = kTurnUsername; |
| 186 | ice_server.password = kTurnPassword; |
| 187 | ice_servers.push_back(ice_server); |
| 188 | talk_base::scoped_refptr<PeerConnectionInterface> pc( |
| 189 | factory_->CreatePeerConnection(ice_servers, NULL, |
| 190 | allocator_factory_.get(), |
| 191 | NULL, |
| 192 | &observer_)); |
| 193 | EXPECT_TRUE(pc.get() != NULL); |
| 194 | TurnConfigurations turn_configs; |
| 195 | webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn( |
| 196 | "test.com", 1234, kTurnUsername, kTurnPassword, "udp"); |
| 197 | turn_configs.push_back(turn); |
| 198 | VerifyTurnConfigurations(turn_configs); |
| 199 | } |
| 200 | |
| 201 | // This test verifies the PeerConnection created properly with TURN url which |
| 202 | // has transport parameter in it. |
| 203 | TEST_F(PeerConnectionFactoryTest, CreatePCUsingTurnUrlWithTransportParam) { |
| 204 | webrtc::PeerConnectionInterface::IceServers ice_servers; |
| 205 | webrtc::PeerConnectionInterface::IceServer ice_server; |
| 206 | ice_server.uri = kTurnIceServerWithTransport; |
| 207 | ice_server.password = kTurnPassword; |
| 208 | ice_servers.push_back(ice_server); |
| 209 | talk_base::scoped_refptr<PeerConnectionInterface> pc( |
| 210 | factory_->CreatePeerConnection(ice_servers, NULL, |
| 211 | allocator_factory_.get(), |
| 212 | NULL, |
| 213 | &observer_)); |
| 214 | EXPECT_TRUE(pc.get() != NULL); |
| 215 | TurnConfigurations turn_configs; |
| 216 | webrtc::PortAllocatorFactoryInterface::TurnConfiguration turn( |
| 217 | "hello.com", kDefaultPort, "test", kTurnPassword, "tcp"); |
| 218 | turn_configs.push_back(turn); |
| 219 | VerifyTurnConfigurations(turn_configs); |
| 220 | StunConfigurations stun_configs; |
| 221 | webrtc::PortAllocatorFactoryInterface::StunConfiguration stun( |
| 222 | "hello.com", kDefaultPort); |
| 223 | stun_configs.push_back(stun); |
| 224 | VerifyStunConfigurations(stun_configs); |
| 225 | } |
| 226 | |
| 227 | // This test verifies factory failed to create a peerconneciton object when |
| 228 | // a valid secure TURN url passed. Connecting to a secure TURN server is not |
| 229 | // supported currently. |
| 230 | TEST_F(PeerConnectionFactoryTest, CreatePCUsingSecureTurnUrl) { |
| 231 | webrtc::PeerConnectionInterface::IceServers ice_servers; |
| 232 | webrtc::PeerConnectionInterface::IceServer ice_server; |
| 233 | ice_server.uri = kSecureTurnIceServer; |
| 234 | ice_server.password = kTurnPassword; |
| 235 | ice_servers.push_back(ice_server); |
| 236 | talk_base::scoped_refptr<PeerConnectionInterface> pc( |
| 237 | factory_->CreatePeerConnection(ice_servers, NULL, |
| 238 | allocator_factory_.get(), |
| 239 | NULL, |
| 240 | &observer_)); |
| 241 | EXPECT_TRUE(pc.get() == NULL); |
| 242 | TurnConfigurations turn_configs; |
| 243 | VerifyTurnConfigurations(turn_configs); |
| 244 | } |
| 245 | |
| 246 | // This test verifies the captured stream is rendered locally using a |
| 247 | // local video track. |
| 248 | TEST_F(PeerConnectionFactoryTest, LocalRendering) { |
| 249 | cricket::FakeVideoCapturer* capturer = new cricket::FakeVideoCapturer(); |
| 250 | // The source take ownership of |capturer|. |
| 251 | talk_base::scoped_refptr<VideoSourceInterface> source( |
| 252 | factory_->CreateVideoSource(capturer, NULL)); |
| 253 | ASSERT_TRUE(source.get() != NULL); |
| 254 | talk_base::scoped_refptr<VideoTrackInterface> track( |
| 255 | factory_->CreateVideoTrack("testlabel", source)); |
| 256 | ASSERT_TRUE(track.get() != NULL); |
| 257 | FakeVideoTrackRenderer local_renderer(track); |
| 258 | |
| 259 | EXPECT_EQ(0, local_renderer.num_rendered_frames()); |
| 260 | EXPECT_TRUE(capturer->CaptureFrame()); |
| 261 | EXPECT_EQ(1, local_renderer.num_rendered_frames()); |
| 262 | |
| 263 | track->set_enabled(false); |
| 264 | EXPECT_TRUE(capturer->CaptureFrame()); |
| 265 | EXPECT_EQ(1, local_renderer.num_rendered_frames()); |
| 266 | |
| 267 | track->set_enabled(true); |
| 268 | EXPECT_TRUE(capturer->CaptureFrame()); |
| 269 | EXPECT_EQ(2, local_renderer.num_rendered_frames()); |
| 270 | } |