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 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 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/jsepsessiondescription.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 32 | #include "talk/app/webrtc/mediastreaminterface.h" |
| 33 | #include "talk/app/webrtc/peerconnectioninterface.h" |
| 34 | #include "talk/app/webrtc/test/fakeconstraints.h" |
| 35 | #include "talk/app/webrtc/test/mockpeerconnectionobservers.h" |
| 36 | #include "talk/app/webrtc/test/testsdpstrings.h" |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 37 | #include "talk/app/webrtc/videosource.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 38 | #include "talk/base/gunit.h" |
| 39 | #include "talk/base/scoped_ptr.h" |
| 40 | #include "talk/base/sslstreamadapter.h" |
| 41 | #include "talk/base/stringutils.h" |
| 42 | #include "talk/base/thread.h" |
| 43 | #include "talk/media/base/fakevideocapturer.h" |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 44 | #include "talk/media/sctp/sctpdataengine.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 45 | #include "talk/session/media/mediasession.h" |
| 46 | |
| 47 | static const char kStreamLabel1[] = "local_stream_1"; |
| 48 | static const char kStreamLabel2[] = "local_stream_2"; |
| 49 | static const char kStreamLabel3[] = "local_stream_3"; |
| 50 | static const int kDefaultStunPort = 3478; |
| 51 | static const char kStunAddressOnly[] = "stun:address"; |
| 52 | static const char kStunInvalidPort[] = "stun:address:-1"; |
| 53 | static const char kStunAddressPortAndMore1[] = "stun:address:port:more"; |
| 54 | static const char kStunAddressPortAndMore2[] = "stun:address:port more"; |
| 55 | static const char kTurnIceServerUri[] = "turn:user@turn.example.org"; |
| 56 | static const char kTurnUsername[] = "user"; |
| 57 | static const char kTurnPassword[] = "password"; |
| 58 | static const char kTurnHostname[] = "turn.example.org"; |
| 59 | static const uint32 kTimeout = 5000U; |
| 60 | |
| 61 | #define MAYBE_SKIP_TEST(feature) \ |
| 62 | if (!(feature())) { \ |
| 63 | LOG(LS_INFO) << "Feature disabled... skipping"; \ |
| 64 | return; \ |
| 65 | } |
| 66 | |
| 67 | using talk_base::scoped_ptr; |
| 68 | using talk_base::scoped_refptr; |
| 69 | using webrtc::AudioSourceInterface; |
| 70 | using webrtc::AudioTrackInterface; |
| 71 | using webrtc::DataBuffer; |
| 72 | using webrtc::DataChannelInterface; |
| 73 | using webrtc::FakeConstraints; |
| 74 | using webrtc::FakePortAllocatorFactory; |
| 75 | using webrtc::IceCandidateInterface; |
| 76 | using webrtc::MediaStreamInterface; |
| 77 | using webrtc::MediaStreamTrackInterface; |
| 78 | using webrtc::MockCreateSessionDescriptionObserver; |
| 79 | using webrtc::MockDataChannelObserver; |
| 80 | using webrtc::MockSetSessionDescriptionObserver; |
| 81 | using webrtc::MockStatsObserver; |
| 82 | using webrtc::PeerConnectionInterface; |
| 83 | using webrtc::PeerConnectionObserver; |
| 84 | using webrtc::PortAllocatorFactoryInterface; |
| 85 | using webrtc::SdpParseError; |
| 86 | using webrtc::SessionDescriptionInterface; |
| 87 | using webrtc::VideoSourceInterface; |
| 88 | using webrtc::VideoTrackInterface; |
| 89 | |
| 90 | namespace { |
| 91 | |
| 92 | // Gets the first ssrc of given content type from the ContentInfo. |
| 93 | bool GetFirstSsrc(const cricket::ContentInfo* content_info, int* ssrc) { |
| 94 | if (!content_info || !ssrc) { |
| 95 | return false; |
| 96 | } |
| 97 | const cricket::MediaContentDescription* media_desc = |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 98 | static_cast<const cricket::MediaContentDescription*>( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 99 | content_info->description); |
| 100 | if (!media_desc || media_desc->streams().empty()) { |
| 101 | return false; |
| 102 | } |
| 103 | *ssrc = media_desc->streams().begin()->first_ssrc(); |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | void SetSsrcToZero(std::string* sdp) { |
| 108 | const char kSdpSsrcAtribute[] = "a=ssrc:"; |
| 109 | const char kSdpSsrcAtributeZero[] = "a=ssrc:0"; |
| 110 | size_t ssrc_pos = 0; |
| 111 | while ((ssrc_pos = sdp->find(kSdpSsrcAtribute, ssrc_pos)) != |
| 112 | std::string::npos) { |
| 113 | size_t end_ssrc = sdp->find(" ", ssrc_pos); |
| 114 | sdp->replace(ssrc_pos, end_ssrc - ssrc_pos, kSdpSsrcAtributeZero); |
| 115 | ssrc_pos = end_ssrc; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | class MockPeerConnectionObserver : public PeerConnectionObserver { |
| 120 | public: |
| 121 | MockPeerConnectionObserver() |
| 122 | : renegotiation_needed_(false), |
| 123 | ice_complete_(false) { |
| 124 | } |
| 125 | ~MockPeerConnectionObserver() { |
| 126 | } |
| 127 | void SetPeerConnectionInterface(PeerConnectionInterface* pc) { |
| 128 | pc_ = pc; |
| 129 | if (pc) { |
| 130 | state_ = pc_->signaling_state(); |
| 131 | } |
| 132 | } |
| 133 | virtual void OnError() {} |
| 134 | virtual void OnSignalingChange( |
| 135 | PeerConnectionInterface::SignalingState new_state) { |
| 136 | EXPECT_EQ(pc_->signaling_state(), new_state); |
| 137 | state_ = new_state; |
| 138 | } |
| 139 | // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange. |
| 140 | virtual void OnStateChange(StateType state_changed) { |
| 141 | if (pc_.get() == NULL) |
| 142 | return; |
| 143 | switch (state_changed) { |
| 144 | case kSignalingState: |
| 145 | // OnSignalingChange and OnStateChange(kSignalingState) should always |
| 146 | // be called approximately simultaneously. To ease testing, we require |
| 147 | // that they always be called in that order. This check verifies |
| 148 | // that OnSignalingChange has just been called. |
| 149 | EXPECT_EQ(pc_->signaling_state(), state_); |
| 150 | break; |
| 151 | case kIceState: |
| 152 | ADD_FAILURE(); |
| 153 | break; |
| 154 | default: |
| 155 | ADD_FAILURE(); |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | virtual void OnAddStream(MediaStreamInterface* stream) { |
| 160 | last_added_stream_ = stream; |
| 161 | } |
| 162 | virtual void OnRemoveStream(MediaStreamInterface* stream) { |
| 163 | last_removed_stream_ = stream; |
| 164 | } |
| 165 | virtual void OnRenegotiationNeeded() { |
| 166 | renegotiation_needed_ = true; |
| 167 | } |
| 168 | virtual void OnDataChannel(DataChannelInterface* data_channel) { |
| 169 | last_datachannel_ = data_channel; |
| 170 | } |
| 171 | |
| 172 | virtual void OnIceConnectionChange( |
| 173 | PeerConnectionInterface::IceConnectionState new_state) { |
| 174 | EXPECT_EQ(pc_->ice_connection_state(), new_state); |
| 175 | } |
| 176 | virtual void OnIceGatheringChange( |
| 177 | PeerConnectionInterface::IceGatheringState new_state) { |
| 178 | EXPECT_EQ(pc_->ice_gathering_state(), new_state); |
| 179 | } |
| 180 | virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) { |
| 181 | EXPECT_NE(PeerConnectionInterface::kIceGatheringNew, |
| 182 | pc_->ice_gathering_state()); |
| 183 | |
| 184 | std::string sdp; |
| 185 | EXPECT_TRUE(candidate->ToString(&sdp)); |
| 186 | EXPECT_LT(0u, sdp.size()); |
| 187 | last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(), |
| 188 | candidate->sdp_mline_index(), sdp, NULL)); |
| 189 | EXPECT_TRUE(last_candidate_.get() != NULL); |
| 190 | } |
| 191 | // TODO(bemasc): Remove this once callers transition to OnSignalingChange. |
| 192 | virtual void OnIceComplete() { |
| 193 | ice_complete_ = true; |
| 194 | // OnIceGatheringChange(IceGatheringCompleted) and OnIceComplete() should |
| 195 | // be called approximately simultaneously. For ease of testing, this |
| 196 | // check additionally requires that they be called in the above order. |
| 197 | EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete, |
| 198 | pc_->ice_gathering_state()); |
| 199 | } |
| 200 | |
| 201 | // Returns the label of the last added stream. |
| 202 | // Empty string if no stream have been added. |
| 203 | std::string GetLastAddedStreamLabel() { |
| 204 | if (last_added_stream_.get()) |
| 205 | return last_added_stream_->label(); |
| 206 | return ""; |
| 207 | } |
| 208 | std::string GetLastRemovedStreamLabel() { |
| 209 | if (last_removed_stream_.get()) |
| 210 | return last_removed_stream_->label(); |
| 211 | return ""; |
| 212 | } |
| 213 | |
| 214 | scoped_refptr<PeerConnectionInterface> pc_; |
| 215 | PeerConnectionInterface::SignalingState state_; |
| 216 | scoped_ptr<IceCandidateInterface> last_candidate_; |
| 217 | scoped_refptr<DataChannelInterface> last_datachannel_; |
| 218 | bool renegotiation_needed_; |
| 219 | bool ice_complete_; |
| 220 | |
| 221 | private: |
| 222 | scoped_refptr<MediaStreamInterface> last_added_stream_; |
| 223 | scoped_refptr<MediaStreamInterface> last_removed_stream_; |
| 224 | }; |
| 225 | |
| 226 | } // namespace |
| 227 | class PeerConnectionInterfaceTest : public testing::Test { |
| 228 | protected: |
| 229 | virtual void SetUp() { |
| 230 | pc_factory_ = webrtc::CreatePeerConnectionFactory( |
| 231 | talk_base::Thread::Current(), talk_base::Thread::Current(), NULL, NULL, |
| 232 | NULL); |
| 233 | ASSERT_TRUE(pc_factory_.get() != NULL); |
| 234 | } |
| 235 | |
| 236 | void CreatePeerConnection() { |
| 237 | CreatePeerConnection("", "", NULL); |
| 238 | } |
| 239 | |
| 240 | void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) { |
| 241 | CreatePeerConnection("", "", constraints); |
| 242 | } |
| 243 | |
| 244 | void CreatePeerConnection(const std::string& uri, |
| 245 | const std::string& password, |
| 246 | webrtc::MediaConstraintsInterface* constraints) { |
| 247 | PeerConnectionInterface::IceServer server; |
| 248 | PeerConnectionInterface::IceServers servers; |
| 249 | server.uri = uri; |
| 250 | server.password = password; |
| 251 | servers.push_back(server); |
| 252 | |
| 253 | port_allocator_factory_ = FakePortAllocatorFactory::Create(); |
| 254 | pc_ = pc_factory_->CreatePeerConnection(servers, constraints, |
| 255 | port_allocator_factory_.get(), |
| 256 | NULL, |
| 257 | &observer_); |
| 258 | ASSERT_TRUE(pc_.get() != NULL); |
| 259 | observer_.SetPeerConnectionInterface(pc_.get()); |
| 260 | EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_); |
| 261 | } |
| 262 | |
| 263 | void CreatePeerConnectionWithDifferentConfigurations() { |
| 264 | CreatePeerConnection(kStunAddressOnly, "", NULL); |
| 265 | EXPECT_EQ(1u, port_allocator_factory_->stun_configs().size()); |
| 266 | EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size()); |
| 267 | EXPECT_EQ("address", |
| 268 | port_allocator_factory_->stun_configs()[0].server.hostname()); |
| 269 | EXPECT_EQ(kDefaultStunPort, |
| 270 | port_allocator_factory_->stun_configs()[0].server.port()); |
| 271 | |
| 272 | CreatePeerConnection(kStunInvalidPort, "", NULL); |
| 273 | EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size()); |
| 274 | EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size()); |
| 275 | |
| 276 | CreatePeerConnection(kStunAddressPortAndMore1, "", NULL); |
| 277 | EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size()); |
| 278 | EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size()); |
| 279 | |
| 280 | CreatePeerConnection(kStunAddressPortAndMore2, "", NULL); |
| 281 | EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size()); |
| 282 | EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size()); |
| 283 | |
| 284 | CreatePeerConnection(kTurnIceServerUri, kTurnPassword, NULL); |
| 285 | EXPECT_EQ(1u, port_allocator_factory_->stun_configs().size()); |
| 286 | EXPECT_EQ(1u, port_allocator_factory_->turn_configs().size()); |
| 287 | EXPECT_EQ(kTurnUsername, |
| 288 | port_allocator_factory_->turn_configs()[0].username); |
| 289 | EXPECT_EQ(kTurnPassword, |
| 290 | port_allocator_factory_->turn_configs()[0].password); |
| 291 | EXPECT_EQ(kTurnHostname, |
| 292 | port_allocator_factory_->turn_configs()[0].server.hostname()); |
| 293 | EXPECT_EQ(kTurnHostname, |
| 294 | port_allocator_factory_->stun_configs()[0].server.hostname()); |
| 295 | } |
| 296 | |
| 297 | void ReleasePeerConnection() { |
| 298 | pc_ = NULL; |
| 299 | observer_.SetPeerConnectionInterface(NULL); |
| 300 | } |
| 301 | |
| 302 | void AddStream(const std::string& label) { |
| 303 | // Create a local stream. |
| 304 | scoped_refptr<MediaStreamInterface> stream( |
| 305 | pc_factory_->CreateLocalMediaStream(label)); |
| 306 | scoped_refptr<VideoSourceInterface> video_source( |
| 307 | pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL)); |
| 308 | scoped_refptr<VideoTrackInterface> video_track( |
| 309 | pc_factory_->CreateVideoTrack(label + "v0", video_source)); |
| 310 | stream->AddTrack(video_track.get()); |
| 311 | EXPECT_TRUE(pc_->AddStream(stream, NULL)); |
| 312 | EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout); |
| 313 | observer_.renegotiation_needed_ = false; |
| 314 | } |
| 315 | |
| 316 | void AddVoiceStream(const std::string& label) { |
| 317 | // Create a local stream. |
| 318 | scoped_refptr<MediaStreamInterface> stream( |
| 319 | pc_factory_->CreateLocalMediaStream(label)); |
| 320 | scoped_refptr<AudioTrackInterface> audio_track( |
| 321 | pc_factory_->CreateAudioTrack(label + "a0", NULL)); |
| 322 | stream->AddTrack(audio_track.get()); |
| 323 | EXPECT_TRUE(pc_->AddStream(stream, NULL)); |
| 324 | EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout); |
| 325 | observer_.renegotiation_needed_ = false; |
| 326 | } |
| 327 | |
| 328 | void AddAudioVideoStream(const std::string& stream_label, |
| 329 | const std::string& audio_track_label, |
| 330 | const std::string& video_track_label) { |
| 331 | // Create a local stream. |
| 332 | scoped_refptr<MediaStreamInterface> stream( |
| 333 | pc_factory_->CreateLocalMediaStream(stream_label)); |
| 334 | scoped_refptr<AudioTrackInterface> audio_track( |
| 335 | pc_factory_->CreateAudioTrack( |
| 336 | audio_track_label, static_cast<AudioSourceInterface*>(NULL))); |
| 337 | stream->AddTrack(audio_track.get()); |
| 338 | scoped_refptr<VideoTrackInterface> video_track( |
| 339 | pc_factory_->CreateVideoTrack(video_track_label, NULL)); |
| 340 | stream->AddTrack(video_track.get()); |
| 341 | EXPECT_TRUE(pc_->AddStream(stream, NULL)); |
| 342 | EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout); |
| 343 | observer_.renegotiation_needed_ = false; |
| 344 | } |
| 345 | |
| 346 | bool DoCreateOfferAnswer(SessionDescriptionInterface** desc, bool offer) { |
| 347 | talk_base::scoped_refptr<MockCreateSessionDescriptionObserver> |
| 348 | observer(new talk_base::RefCountedObject< |
| 349 | MockCreateSessionDescriptionObserver>()); |
| 350 | if (offer) { |
| 351 | pc_->CreateOffer(observer, NULL); |
| 352 | } else { |
| 353 | pc_->CreateAnswer(observer, NULL); |
| 354 | } |
| 355 | EXPECT_EQ_WAIT(true, observer->called(), kTimeout); |
| 356 | *desc = observer->release_desc(); |
| 357 | return observer->result(); |
| 358 | } |
| 359 | |
| 360 | bool DoCreateOffer(SessionDescriptionInterface** desc) { |
| 361 | return DoCreateOfferAnswer(desc, true); |
| 362 | } |
| 363 | |
| 364 | bool DoCreateAnswer(SessionDescriptionInterface** desc) { |
| 365 | return DoCreateOfferAnswer(desc, false); |
| 366 | } |
| 367 | |
| 368 | bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) { |
| 369 | talk_base::scoped_refptr<MockSetSessionDescriptionObserver> |
| 370 | observer(new talk_base::RefCountedObject< |
| 371 | MockSetSessionDescriptionObserver>()); |
| 372 | if (local) { |
| 373 | pc_->SetLocalDescription(observer, desc); |
| 374 | } else { |
| 375 | pc_->SetRemoteDescription(observer, desc); |
| 376 | } |
| 377 | EXPECT_EQ_WAIT(true, observer->called(), kTimeout); |
| 378 | return observer->result(); |
| 379 | } |
| 380 | |
| 381 | bool DoSetLocalDescription(SessionDescriptionInterface* desc) { |
| 382 | return DoSetSessionDescription(desc, true); |
| 383 | } |
| 384 | |
| 385 | bool DoSetRemoteDescription(SessionDescriptionInterface* desc) { |
| 386 | return DoSetSessionDescription(desc, false); |
| 387 | } |
| 388 | |
| 389 | // Calls PeerConnection::GetStats and check the return value. |
| 390 | // It does not verify the values in the StatReports since a RTCP packet might |
| 391 | // be required. |
| 392 | bool DoGetStats(MediaStreamTrackInterface* track) { |
| 393 | talk_base::scoped_refptr<MockStatsObserver> observer( |
| 394 | new talk_base::RefCountedObject<MockStatsObserver>()); |
| 395 | if (!pc_->GetStats(observer, track)) |
| 396 | return false; |
| 397 | EXPECT_TRUE_WAIT(observer->called(), kTimeout); |
| 398 | return observer->called(); |
| 399 | } |
| 400 | |
| 401 | void InitiateCall() { |
| 402 | CreatePeerConnection(); |
| 403 | // Create a local stream with audio&video tracks. |
| 404 | AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label"); |
| 405 | CreateOfferReceiveAnswer(); |
| 406 | } |
| 407 | |
| 408 | // Verify that RTP Header extensions has been negotiated for audio and video. |
| 409 | void VerifyRemoteRtpHeaderExtensions() { |
| 410 | const cricket::MediaContentDescription* desc = |
| 411 | cricket::GetFirstAudioContentDescription( |
| 412 | pc_->remote_description()->description()); |
| 413 | ASSERT_TRUE(desc != NULL); |
| 414 | EXPECT_GT(desc->rtp_header_extensions().size(), 0u); |
| 415 | |
| 416 | desc = cricket::GetFirstVideoContentDescription( |
| 417 | pc_->remote_description()->description()); |
| 418 | ASSERT_TRUE(desc != NULL); |
| 419 | EXPECT_GT(desc->rtp_header_extensions().size(), 0u); |
| 420 | } |
| 421 | |
| 422 | void CreateOfferAsRemoteDescription() { |
| 423 | talk_base::scoped_ptr<SessionDescriptionInterface> offer; |
| 424 | EXPECT_TRUE(DoCreateOffer(offer.use())); |
| 425 | std::string sdp; |
| 426 | EXPECT_TRUE(offer->ToString(&sdp)); |
| 427 | SessionDescriptionInterface* remote_offer = |
| 428 | webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, |
| 429 | sdp, NULL); |
| 430 | EXPECT_TRUE(DoSetRemoteDescription(remote_offer)); |
| 431 | EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_); |
| 432 | } |
| 433 | |
| 434 | void CreateAnswerAsLocalDescription() { |
| 435 | scoped_ptr<SessionDescriptionInterface> answer; |
| 436 | EXPECT_TRUE(DoCreateAnswer(answer.use())); |
| 437 | |
| 438 | // TODO(perkj): Currently SetLocalDescription fails if any parameters in an |
| 439 | // audio codec change, even if the parameter has nothing to do with |
| 440 | // receiving. Not all parameters are serialized to SDP. |
| 441 | // Since CreatePrAnswerAsLocalDescription serialize/deserialize |
| 442 | // the SessionDescription, it is necessary to do that here to in order to |
| 443 | // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass. |
| 444 | // https://code.google.com/p/webrtc/issues/detail?id=1356 |
| 445 | std::string sdp; |
| 446 | EXPECT_TRUE(answer->ToString(&sdp)); |
| 447 | SessionDescriptionInterface* new_answer = |
| 448 | webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer, |
| 449 | sdp, NULL); |
| 450 | EXPECT_TRUE(DoSetLocalDescription(new_answer)); |
| 451 | EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_); |
| 452 | } |
| 453 | |
| 454 | void CreatePrAnswerAsLocalDescription() { |
| 455 | scoped_ptr<SessionDescriptionInterface> answer; |
| 456 | EXPECT_TRUE(DoCreateAnswer(answer.use())); |
| 457 | |
| 458 | std::string sdp; |
| 459 | EXPECT_TRUE(answer->ToString(&sdp)); |
| 460 | SessionDescriptionInterface* pr_answer = |
| 461 | webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer, |
| 462 | sdp, NULL); |
| 463 | EXPECT_TRUE(DoSetLocalDescription(pr_answer)); |
| 464 | EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_); |
| 465 | } |
| 466 | |
| 467 | void CreateOfferReceiveAnswer() { |
| 468 | CreateOfferAsLocalDescription(); |
| 469 | std::string sdp; |
| 470 | EXPECT_TRUE(pc_->local_description()->ToString(&sdp)); |
| 471 | CreateAnswerAsRemoteDescription(sdp); |
| 472 | } |
| 473 | |
| 474 | void CreateOfferAsLocalDescription() { |
| 475 | talk_base::scoped_ptr<SessionDescriptionInterface> offer; |
| 476 | ASSERT_TRUE(DoCreateOffer(offer.use())); |
| 477 | // TODO(perkj): Currently SetLocalDescription fails if any parameters in an |
| 478 | // audio codec change, even if the parameter has nothing to do with |
| 479 | // receiving. Not all parameters are serialized to SDP. |
| 480 | // Since CreatePrAnswerAsLocalDescription serialize/deserialize |
| 481 | // the SessionDescription, it is necessary to do that here to in order to |
| 482 | // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass. |
| 483 | // https://code.google.com/p/webrtc/issues/detail?id=1356 |
| 484 | std::string sdp; |
| 485 | EXPECT_TRUE(offer->ToString(&sdp)); |
| 486 | SessionDescriptionInterface* new_offer = |
| 487 | webrtc::CreateSessionDescription( |
| 488 | SessionDescriptionInterface::kOffer, |
| 489 | sdp, NULL); |
| 490 | |
| 491 | EXPECT_TRUE(DoSetLocalDescription(new_offer)); |
| 492 | EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_); |
| 493 | } |
| 494 | |
| 495 | void CreateAnswerAsRemoteDescription(const std::string& offer) { |
| 496 | webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription( |
| 497 | SessionDescriptionInterface::kAnswer); |
| 498 | EXPECT_TRUE(answer->Initialize(offer, NULL)); |
| 499 | EXPECT_TRUE(DoSetRemoteDescription(answer)); |
| 500 | EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_); |
| 501 | } |
| 502 | |
| 503 | void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& offer) { |
| 504 | webrtc::JsepSessionDescription* pr_answer = |
| 505 | new webrtc::JsepSessionDescription( |
| 506 | SessionDescriptionInterface::kPrAnswer); |
| 507 | EXPECT_TRUE(pr_answer->Initialize(offer, NULL)); |
| 508 | EXPECT_TRUE(DoSetRemoteDescription(pr_answer)); |
| 509 | EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_); |
| 510 | webrtc::JsepSessionDescription* answer = |
| 511 | new webrtc::JsepSessionDescription( |
| 512 | SessionDescriptionInterface::kAnswer); |
| 513 | EXPECT_TRUE(answer->Initialize(offer, NULL)); |
| 514 | EXPECT_TRUE(DoSetRemoteDescription(answer)); |
| 515 | EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_); |
| 516 | } |
| 517 | |
| 518 | // Help function used for waiting until a the last signaled remote stream has |
| 519 | // the same label as |stream_label|. In a few of the tests in this file we |
| 520 | // answer with the same session description as we offer and thus we can |
| 521 | // check if OnAddStream have been called with the same stream as we offer to |
| 522 | // send. |
| 523 | void WaitAndVerifyOnAddStream(const std::string& stream_label) { |
| 524 | EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout); |
| 525 | } |
| 526 | |
| 527 | // Creates an offer and applies it as a local session description. |
| 528 | // Creates an answer with the same SDP an the offer but removes all lines |
| 529 | // that start with a:ssrc" |
| 530 | void CreateOfferReceiveAnswerWithoutSsrc() { |
| 531 | CreateOfferAsLocalDescription(); |
| 532 | std::string sdp; |
| 533 | EXPECT_TRUE(pc_->local_description()->ToString(&sdp)); |
| 534 | SetSsrcToZero(&sdp); |
| 535 | CreateAnswerAsRemoteDescription(sdp); |
| 536 | } |
| 537 | |
| 538 | scoped_refptr<FakePortAllocatorFactory> port_allocator_factory_; |
| 539 | scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_; |
| 540 | scoped_refptr<PeerConnectionInterface> pc_; |
| 541 | MockPeerConnectionObserver observer_; |
| 542 | }; |
| 543 | |
| 544 | TEST_F(PeerConnectionInterfaceTest, |
| 545 | CreatePeerConnectionWithDifferentConfigurations) { |
| 546 | CreatePeerConnectionWithDifferentConfigurations(); |
| 547 | } |
| 548 | |
| 549 | TEST_F(PeerConnectionInterfaceTest, AddStreams) { |
| 550 | CreatePeerConnection(); |
| 551 | AddStream(kStreamLabel1); |
| 552 | AddVoiceStream(kStreamLabel2); |
| 553 | ASSERT_EQ(2u, pc_->local_streams()->count()); |
| 554 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 555 | // Test we can add multiple local streams to one peerconnection. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 556 | scoped_refptr<MediaStreamInterface> stream( |
| 557 | pc_factory_->CreateLocalMediaStream(kStreamLabel3)); |
| 558 | scoped_refptr<AudioTrackInterface> audio_track( |
| 559 | pc_factory_->CreateAudioTrack( |
| 560 | kStreamLabel3, static_cast<AudioSourceInterface*>(NULL))); |
| 561 | stream->AddTrack(audio_track.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 562 | EXPECT_TRUE(pc_->AddStream(stream, NULL)); |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 563 | EXPECT_EQ(3u, pc_->local_streams()->count()); |
| 564 | |
| 565 | // Remove the third stream. |
| 566 | pc_->RemoveStream(pc_->local_streams()->at(2)); |
| 567 | EXPECT_EQ(2u, pc_->local_streams()->count()); |
| 568 | |
| 569 | // Remove the second stream. |
| 570 | pc_->RemoveStream(pc_->local_streams()->at(1)); |
| 571 | EXPECT_EQ(1u, pc_->local_streams()->count()); |
| 572 | |
| 573 | // Remove the first stream. |
| 574 | pc_->RemoveStream(pc_->local_streams()->at(0)); |
| 575 | EXPECT_EQ(0u, pc_->local_streams()->count()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | TEST_F(PeerConnectionInterfaceTest, RemoveStream) { |
| 579 | CreatePeerConnection(); |
| 580 | AddStream(kStreamLabel1); |
| 581 | ASSERT_EQ(1u, pc_->local_streams()->count()); |
| 582 | pc_->RemoveStream(pc_->local_streams()->at(0)); |
| 583 | EXPECT_EQ(0u, pc_->local_streams()->count()); |
| 584 | } |
| 585 | |
| 586 | TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) { |
| 587 | InitiateCall(); |
| 588 | WaitAndVerifyOnAddStream(kStreamLabel1); |
| 589 | VerifyRemoteRtpHeaderExtensions(); |
| 590 | } |
| 591 | |
| 592 | TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) { |
| 593 | CreatePeerConnection(); |
| 594 | AddStream(kStreamLabel1); |
| 595 | CreateOfferAsLocalDescription(); |
| 596 | std::string offer; |
| 597 | EXPECT_TRUE(pc_->local_description()->ToString(&offer)); |
| 598 | CreatePrAnswerAndAnswerAsRemoteDescription(offer); |
| 599 | WaitAndVerifyOnAddStream(kStreamLabel1); |
| 600 | } |
| 601 | |
| 602 | TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) { |
| 603 | CreatePeerConnection(); |
| 604 | AddStream(kStreamLabel1); |
| 605 | |
| 606 | CreateOfferAsRemoteDescription(); |
| 607 | CreateAnswerAsLocalDescription(); |
| 608 | |
| 609 | WaitAndVerifyOnAddStream(kStreamLabel1); |
| 610 | } |
| 611 | |
| 612 | TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) { |
| 613 | CreatePeerConnection(); |
| 614 | AddStream(kStreamLabel1); |
| 615 | |
| 616 | CreateOfferAsRemoteDescription(); |
| 617 | CreatePrAnswerAsLocalDescription(); |
| 618 | CreateAnswerAsLocalDescription(); |
| 619 | |
| 620 | WaitAndVerifyOnAddStream(kStreamLabel1); |
| 621 | } |
| 622 | |
| 623 | TEST_F(PeerConnectionInterfaceTest, Renegotiate) { |
| 624 | InitiateCall(); |
| 625 | ASSERT_EQ(1u, pc_->remote_streams()->count()); |
| 626 | pc_->RemoveStream(pc_->local_streams()->at(0)); |
| 627 | CreateOfferReceiveAnswer(); |
| 628 | EXPECT_EQ(0u, pc_->remote_streams()->count()); |
| 629 | AddStream(kStreamLabel1); |
| 630 | CreateOfferReceiveAnswer(); |
| 631 | } |
| 632 | |
| 633 | // Tests that after negotiating an audio only call, the respondent can perform a |
| 634 | // renegotiation that removes the audio stream. |
| 635 | TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) { |
| 636 | CreatePeerConnection(); |
| 637 | AddVoiceStream(kStreamLabel1); |
| 638 | CreateOfferAsRemoteDescription(); |
| 639 | CreateAnswerAsLocalDescription(); |
| 640 | |
| 641 | ASSERT_EQ(1u, pc_->remote_streams()->count()); |
| 642 | pc_->RemoveStream(pc_->local_streams()->at(0)); |
| 643 | CreateOfferReceiveAnswer(); |
| 644 | EXPECT_EQ(0u, pc_->remote_streams()->count()); |
| 645 | } |
| 646 | |
| 647 | // Test that candidates are generated and that we can parse our own candidates. |
| 648 | TEST_F(PeerConnectionInterfaceTest, IceCandidates) { |
| 649 | CreatePeerConnection(); |
| 650 | |
| 651 | EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get())); |
| 652 | // SetRemoteDescription takes ownership of offer. |
| 653 | SessionDescriptionInterface* offer = NULL; |
| 654 | AddStream(kStreamLabel1); |
| 655 | EXPECT_TRUE(DoCreateOffer(&offer)); |
| 656 | EXPECT_TRUE(DoSetRemoteDescription(offer)); |
| 657 | |
| 658 | // SetLocalDescription takes ownership of answer. |
| 659 | SessionDescriptionInterface* answer = NULL; |
| 660 | EXPECT_TRUE(DoCreateAnswer(&answer)); |
| 661 | EXPECT_TRUE(DoSetLocalDescription(answer)); |
| 662 | |
| 663 | EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout); |
| 664 | EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout); |
| 665 | |
| 666 | EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get())); |
| 667 | } |
| 668 | |
| 669 | // Test that the CreateOffer and CreatAnswer will fail if the track labels are |
| 670 | // not unique. |
| 671 | TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) { |
| 672 | CreatePeerConnection(); |
| 673 | // Create a regular offer for the CreateAnswer test later. |
| 674 | SessionDescriptionInterface* offer = NULL; |
| 675 | EXPECT_TRUE(DoCreateOffer(&offer)); |
| 676 | EXPECT_TRUE(offer != NULL); |
| 677 | delete offer; |
| 678 | offer = NULL; |
| 679 | |
| 680 | // Create a local stream with audio&video tracks having same label. |
| 681 | AddAudioVideoStream(kStreamLabel1, "track_label", "track_label"); |
| 682 | |
| 683 | // Test CreateOffer |
| 684 | EXPECT_FALSE(DoCreateOffer(&offer)); |
| 685 | |
| 686 | // Test CreateAnswer |
| 687 | SessionDescriptionInterface* answer = NULL; |
| 688 | EXPECT_FALSE(DoCreateAnswer(&answer)); |
| 689 | } |
| 690 | |
| 691 | // Test that we will get different SSRCs for each tracks in the offer and answer |
| 692 | // we created. |
| 693 | TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) { |
| 694 | CreatePeerConnection(); |
| 695 | // Create a local stream with audio&video tracks having different labels. |
| 696 | AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label"); |
| 697 | |
| 698 | // Test CreateOffer |
| 699 | scoped_ptr<SessionDescriptionInterface> offer; |
| 700 | EXPECT_TRUE(DoCreateOffer(offer.use())); |
| 701 | int audio_ssrc = 0; |
| 702 | int video_ssrc = 0; |
| 703 | EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()), |
| 704 | &audio_ssrc)); |
| 705 | EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()), |
| 706 | &video_ssrc)); |
| 707 | EXPECT_NE(audio_ssrc, video_ssrc); |
| 708 | |
| 709 | // Test CreateAnswer |
| 710 | EXPECT_TRUE(DoSetRemoteDescription(offer.release())); |
| 711 | scoped_ptr<SessionDescriptionInterface> answer; |
| 712 | EXPECT_TRUE(DoCreateAnswer(answer.use())); |
| 713 | audio_ssrc = 0; |
| 714 | video_ssrc = 0; |
| 715 | EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()), |
| 716 | &audio_ssrc)); |
| 717 | EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()), |
| 718 | &video_ssrc)); |
| 719 | EXPECT_NE(audio_ssrc, video_ssrc); |
| 720 | } |
| 721 | |
| 722 | // Test that we can specify a certain track that we want statistics about. |
| 723 | TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) { |
| 724 | InitiateCall(); |
| 725 | ASSERT_LT(0u, pc_->remote_streams()->count()); |
| 726 | ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size()); |
| 727 | scoped_refptr<MediaStreamTrackInterface> remote_audio = |
| 728 | pc_->remote_streams()->at(0)->GetAudioTracks()[0]; |
| 729 | EXPECT_TRUE(DoGetStats(remote_audio)); |
| 730 | |
| 731 | // Remove the stream. Since we are sending to our selves the local |
| 732 | // and the remote stream is the same. |
| 733 | pc_->RemoveStream(pc_->local_streams()->at(0)); |
| 734 | // Do a re-negotiation. |
| 735 | CreateOfferReceiveAnswer(); |
| 736 | |
| 737 | ASSERT_EQ(0u, pc_->remote_streams()->count()); |
| 738 | |
| 739 | // Test that we still can get statistics for the old track. Even if it is not |
| 740 | // sent any longer. |
| 741 | EXPECT_TRUE(DoGetStats(remote_audio)); |
| 742 | } |
| 743 | |
| 744 | // Test that we can get stats on a video track. |
| 745 | TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) { |
| 746 | InitiateCall(); |
| 747 | ASSERT_LT(0u, pc_->remote_streams()->count()); |
| 748 | ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size()); |
| 749 | scoped_refptr<MediaStreamTrackInterface> remote_video = |
| 750 | pc_->remote_streams()->at(0)->GetVideoTracks()[0]; |
| 751 | EXPECT_TRUE(DoGetStats(remote_video)); |
| 752 | } |
| 753 | |
| 754 | // Test that we don't get statistics for an invalid track. |
| 755 | TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) { |
| 756 | InitiateCall(); |
| 757 | scoped_refptr<AudioTrackInterface> unknown_audio_track( |
| 758 | pc_factory_->CreateAudioTrack("unknown track", NULL)); |
| 759 | EXPECT_FALSE(DoGetStats(unknown_audio_track)); |
| 760 | } |
| 761 | |
| 762 | // This test setup two RTP data channels in loop back. |
| 763 | #ifdef WIN32 |
| 764 | // TODO(perkj): Investigate why the transport channel sometimes don't become |
| 765 | // writable on Windows when we try to connect in loop back. |
| 766 | TEST_F(PeerConnectionInterfaceTest, DISABLED_TestDataChannel) { |
| 767 | #else |
| 768 | TEST_F(PeerConnectionInterfaceTest, TestDataChannel) { |
| 769 | #endif |
| 770 | FakeConstraints constraints; |
| 771 | constraints.SetAllowRtpDataChannels(); |
| 772 | CreatePeerConnection(&constraints); |
| 773 | scoped_refptr<DataChannelInterface> data1 = |
| 774 | pc_->CreateDataChannel("test1", NULL); |
| 775 | scoped_refptr<DataChannelInterface> data2 = |
| 776 | pc_->CreateDataChannel("test2", NULL); |
| 777 | ASSERT_TRUE(data1 != NULL); |
| 778 | talk_base::scoped_ptr<MockDataChannelObserver> observer1( |
| 779 | new MockDataChannelObserver(data1)); |
| 780 | talk_base::scoped_ptr<MockDataChannelObserver> observer2( |
| 781 | new MockDataChannelObserver(data2)); |
| 782 | |
| 783 | EXPECT_EQ(DataChannelInterface::kConnecting, data1->state()); |
| 784 | EXPECT_EQ(DataChannelInterface::kConnecting, data2->state()); |
| 785 | std::string data_to_send1 = "testing testing"; |
| 786 | std::string data_to_send2 = "testing something else"; |
| 787 | EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1))); |
| 788 | |
| 789 | CreateOfferReceiveAnswer(); |
| 790 | EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout); |
| 791 | EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout); |
| 792 | |
| 793 | EXPECT_EQ(DataChannelInterface::kOpen, data1->state()); |
| 794 | EXPECT_EQ(DataChannelInterface::kOpen, data2->state()); |
| 795 | EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1))); |
| 796 | EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2))); |
| 797 | |
| 798 | EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout); |
| 799 | EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout); |
| 800 | |
| 801 | data1->Close(); |
| 802 | EXPECT_EQ(DataChannelInterface::kClosing, data1->state()); |
| 803 | CreateOfferReceiveAnswer(); |
| 804 | EXPECT_FALSE(observer1->IsOpen()); |
| 805 | EXPECT_EQ(DataChannelInterface::kClosed, data1->state()); |
| 806 | EXPECT_TRUE(observer2->IsOpen()); |
| 807 | |
| 808 | data_to_send2 = "testing something else again"; |
| 809 | EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2))); |
| 810 | |
| 811 | EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout); |
| 812 | } |
| 813 | |
| 814 | // This test verifies that sendnig binary data over RTP data channels should |
| 815 | // fail. |
| 816 | #ifdef WIN32 |
| 817 | // TODO(perkj): Investigate why the transport channel sometimes don't become |
| 818 | // writable on Windows when we try to connect in loop back. |
| 819 | TEST_F(PeerConnectionInterfaceTest, DISABLED_TestSendBinaryOnRtpDataChannel) { |
| 820 | #else |
| 821 | TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) { |
| 822 | #endif |
| 823 | FakeConstraints constraints; |
| 824 | constraints.SetAllowRtpDataChannels(); |
| 825 | CreatePeerConnection(&constraints); |
| 826 | scoped_refptr<DataChannelInterface> data1 = |
| 827 | pc_->CreateDataChannel("test1", NULL); |
| 828 | scoped_refptr<DataChannelInterface> data2 = |
| 829 | pc_->CreateDataChannel("test2", NULL); |
| 830 | ASSERT_TRUE(data1 != NULL); |
| 831 | talk_base::scoped_ptr<MockDataChannelObserver> observer1( |
| 832 | new MockDataChannelObserver(data1)); |
| 833 | talk_base::scoped_ptr<MockDataChannelObserver> observer2( |
| 834 | new MockDataChannelObserver(data2)); |
| 835 | |
| 836 | EXPECT_EQ(DataChannelInterface::kConnecting, data1->state()); |
| 837 | EXPECT_EQ(DataChannelInterface::kConnecting, data2->state()); |
| 838 | |
| 839 | CreateOfferReceiveAnswer(); |
| 840 | EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout); |
| 841 | EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout); |
| 842 | |
| 843 | EXPECT_EQ(DataChannelInterface::kOpen, data1->state()); |
| 844 | EXPECT_EQ(DataChannelInterface::kOpen, data2->state()); |
| 845 | |
| 846 | talk_base::Buffer buffer("test", 4); |
| 847 | EXPECT_FALSE(data1->Send(DataBuffer(buffer, true))); |
| 848 | } |
| 849 | |
| 850 | // This test setup a RTP data channels in loop back and test that a channel is |
| 851 | // opened even if the remote end answer with a zero SSRC. |
| 852 | #ifdef WIN32 |
| 853 | // TODO(perkj): Investigate why the transport channel sometimes don't become |
| 854 | // writable on Windows when we try to connect in loop back. |
| 855 | TEST_F(PeerConnectionInterfaceTest, DISABLED_TestSendOnlyDataChannel) { |
| 856 | #else |
| 857 | TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) { |
| 858 | #endif |
| 859 | FakeConstraints constraints; |
| 860 | constraints.SetAllowRtpDataChannels(); |
| 861 | CreatePeerConnection(&constraints); |
| 862 | scoped_refptr<DataChannelInterface> data1 = |
| 863 | pc_->CreateDataChannel("test1", NULL); |
| 864 | talk_base::scoped_ptr<MockDataChannelObserver> observer1( |
| 865 | new MockDataChannelObserver(data1)); |
| 866 | |
| 867 | CreateOfferReceiveAnswerWithoutSsrc(); |
| 868 | |
| 869 | EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout); |
| 870 | |
| 871 | data1->Close(); |
| 872 | EXPECT_EQ(DataChannelInterface::kClosing, data1->state()); |
| 873 | CreateOfferReceiveAnswerWithoutSsrc(); |
| 874 | EXPECT_EQ(DataChannelInterface::kClosed, data1->state()); |
| 875 | EXPECT_FALSE(observer1->IsOpen()); |
| 876 | } |
| 877 | |
| 878 | // This test that if a data channel is added in an answer a receive only channel |
| 879 | // channel is created. |
| 880 | TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) { |
| 881 | FakeConstraints constraints; |
| 882 | constraints.SetAllowRtpDataChannels(); |
| 883 | CreatePeerConnection(&constraints); |
| 884 | |
| 885 | std::string offer_label = "offer_channel"; |
| 886 | scoped_refptr<DataChannelInterface> offer_channel = |
| 887 | pc_->CreateDataChannel(offer_label, NULL); |
| 888 | |
| 889 | CreateOfferAsLocalDescription(); |
| 890 | |
| 891 | // Replace the data channel label in the offer and apply it as an answer. |
| 892 | std::string receive_label = "answer_channel"; |
| 893 | std::string sdp; |
| 894 | EXPECT_TRUE(pc_->local_description()->ToString(&sdp)); |
| 895 | talk_base::replace_substrs(offer_label.c_str(), offer_label.length(), |
| 896 | receive_label.c_str(), receive_label.length(), |
| 897 | &sdp); |
| 898 | CreateAnswerAsRemoteDescription(sdp); |
| 899 | |
| 900 | // Verify that a new incoming data channel has been created and that |
| 901 | // it is open but can't we written to. |
| 902 | ASSERT_TRUE(observer_.last_datachannel_ != NULL); |
| 903 | DataChannelInterface* received_channel = observer_.last_datachannel_; |
| 904 | EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state()); |
| 905 | EXPECT_EQ(receive_label, received_channel->label()); |
| 906 | EXPECT_FALSE(received_channel->Send(DataBuffer("something"))); |
| 907 | |
| 908 | // Verify that the channel we initially offered has been rejected. |
| 909 | EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state()); |
| 910 | |
| 911 | // Do another offer / answer exchange and verify that the data channel is |
| 912 | // opened. |
| 913 | CreateOfferReceiveAnswer(); |
| 914 | EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(), |
| 915 | kTimeout); |
| 916 | } |
| 917 | |
| 918 | // This test that no data channel is returned if a reliable channel is |
| 919 | // requested. |
| 920 | // TODO(perkj): Remove this test once reliable channels are implemented. |
| 921 | TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) { |
| 922 | FakeConstraints constraints; |
| 923 | constraints.SetAllowRtpDataChannels(); |
| 924 | CreatePeerConnection(&constraints); |
| 925 | |
| 926 | std::string label = "test"; |
| 927 | webrtc::DataChannelInit config; |
| 928 | config.reliable = true; |
| 929 | scoped_refptr<DataChannelInterface> channel = |
| 930 | pc_->CreateDataChannel(label, &config); |
| 931 | EXPECT_TRUE(channel == NULL); |
| 932 | } |
| 933 | |
| 934 | // This tests that a SCTP data channel is returned using different |
| 935 | // DataChannelInit configurations. |
| 936 | TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) { |
| 937 | FakeConstraints constraints; |
| 938 | constraints.SetAllowDtlsSctpDataChannels(); |
| 939 | CreatePeerConnection(&constraints); |
| 940 | |
| 941 | webrtc::DataChannelInit config; |
| 942 | |
| 943 | scoped_refptr<DataChannelInterface> channel = |
| 944 | pc_->CreateDataChannel("1", &config); |
| 945 | EXPECT_TRUE(channel != NULL); |
| 946 | EXPECT_TRUE(channel->reliable()); |
| 947 | |
| 948 | config.ordered = false; |
| 949 | channel = pc_->CreateDataChannel("2", &config); |
| 950 | EXPECT_TRUE(channel != NULL); |
| 951 | EXPECT_TRUE(channel->reliable()); |
| 952 | |
| 953 | config.ordered = true; |
| 954 | config.maxRetransmits = 0; |
| 955 | channel = pc_->CreateDataChannel("3", &config); |
| 956 | EXPECT_TRUE(channel != NULL); |
| 957 | EXPECT_FALSE(channel->reliable()); |
| 958 | |
| 959 | config.maxRetransmits = -1; |
| 960 | config.maxRetransmitTime = 0; |
| 961 | channel = pc_->CreateDataChannel("4", &config); |
| 962 | EXPECT_TRUE(channel != NULL); |
| 963 | EXPECT_FALSE(channel->reliable()); |
| 964 | } |
| 965 | |
| 966 | // This tests that no data channel is returned if both maxRetransmits and |
| 967 | // maxRetransmitTime are set for SCTP data channels. |
| 968 | TEST_F(PeerConnectionInterfaceTest, |
| 969 | CreateSctpDataChannelShouldFailForInvalidConfig) { |
| 970 | FakeConstraints constraints; |
| 971 | constraints.SetAllowDtlsSctpDataChannels(); |
| 972 | CreatePeerConnection(&constraints); |
| 973 | |
| 974 | std::string label = "test"; |
| 975 | webrtc::DataChannelInit config; |
| 976 | config.maxRetransmits = 0; |
| 977 | config.maxRetransmitTime = 0; |
| 978 | |
| 979 | scoped_refptr<DataChannelInterface> channel = |
| 980 | pc_->CreateDataChannel(label, &config); |
| 981 | EXPECT_TRUE(channel == NULL); |
| 982 | } |
| 983 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 984 | // The test verifies that creating a SCTP data channel with an id already in use |
| 985 | // or out of range should fail. |
| 986 | TEST_F(PeerConnectionInterfaceTest, |
| 987 | CreateSctpDataChannelWithInvalidIdShouldFail) { |
| 988 | FakeConstraints constraints; |
| 989 | constraints.SetAllowDtlsSctpDataChannels(); |
| 990 | CreatePeerConnection(&constraints); |
| 991 | |
| 992 | webrtc::DataChannelInit config; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame^] | 993 | scoped_refptr<DataChannelInterface> channel; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 994 | |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame^] | 995 | config.id = 1; |
| 996 | channel = pc_->CreateDataChannel("1", &config); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 997 | EXPECT_TRUE(channel != NULL); |
| 998 | EXPECT_EQ(1, channel->id()); |
| 999 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1000 | channel = pc_->CreateDataChannel("x", &config); |
| 1001 | EXPECT_TRUE(channel == NULL); |
| 1002 | |
| 1003 | config.id = cricket::kMaxSctpSid; |
| 1004 | channel = pc_->CreateDataChannel("max", &config); |
| 1005 | EXPECT_TRUE(channel != NULL); |
| 1006 | EXPECT_EQ(config.id, channel->id()); |
| 1007 | |
| 1008 | config.id = cricket::kMaxSctpSid + 1; |
| 1009 | channel = pc_->CreateDataChannel("x", &config); |
| 1010 | EXPECT_TRUE(channel == NULL); |
| 1011 | } |
| 1012 | |
| 1013 | // This test that a data channel closes when a PeerConnection is deleted/closed. |
| 1014 | #ifdef WIN32 |
| 1015 | // TODO(perkj): Investigate why the transport channel sometimes don't become |
| 1016 | // writable on Windows when we try to connect in loop back. |
| 1017 | TEST_F(PeerConnectionInterfaceTest, |
| 1018 | DISABLED_DataChannelCloseWhenPeerConnectionClose) { |
| 1019 | #else |
| 1020 | TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) { |
| 1021 | #endif |
| 1022 | FakeConstraints constraints; |
| 1023 | constraints.SetAllowRtpDataChannels(); |
| 1024 | CreatePeerConnection(&constraints); |
| 1025 | |
| 1026 | scoped_refptr<DataChannelInterface> data1 = |
| 1027 | pc_->CreateDataChannel("test1", NULL); |
| 1028 | scoped_refptr<DataChannelInterface> data2 = |
| 1029 | pc_->CreateDataChannel("test2", NULL); |
| 1030 | ASSERT_TRUE(data1 != NULL); |
| 1031 | talk_base::scoped_ptr<MockDataChannelObserver> observer1( |
| 1032 | new MockDataChannelObserver(data1)); |
| 1033 | talk_base::scoped_ptr<MockDataChannelObserver> observer2( |
| 1034 | new MockDataChannelObserver(data2)); |
| 1035 | |
| 1036 | CreateOfferReceiveAnswer(); |
| 1037 | EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout); |
| 1038 | EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout); |
| 1039 | |
| 1040 | ReleasePeerConnection(); |
| 1041 | EXPECT_EQ(DataChannelInterface::kClosed, data1->state()); |
| 1042 | EXPECT_EQ(DataChannelInterface::kClosed, data2->state()); |
| 1043 | } |
| 1044 | |
| 1045 | // This test that data channels can be rejected in an answer. |
| 1046 | TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) { |
| 1047 | FakeConstraints constraints; |
| 1048 | constraints.SetAllowRtpDataChannels(); |
| 1049 | CreatePeerConnection(&constraints); |
| 1050 | |
| 1051 | scoped_refptr<DataChannelInterface> offer_channel( |
| 1052 | pc_->CreateDataChannel("offer_channel", NULL)); |
| 1053 | |
| 1054 | CreateOfferAsLocalDescription(); |
| 1055 | |
| 1056 | // Create an answer where the m-line for data channels are rejected. |
| 1057 | std::string sdp; |
| 1058 | EXPECT_TRUE(pc_->local_description()->ToString(&sdp)); |
| 1059 | webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription( |
| 1060 | SessionDescriptionInterface::kAnswer); |
| 1061 | EXPECT_TRUE(answer->Initialize(sdp, NULL)); |
| 1062 | cricket::ContentInfo* data_info = |
| 1063 | answer->description()->GetContentByName("data"); |
| 1064 | data_info->rejected = true; |
| 1065 | |
| 1066 | DoSetRemoteDescription(answer); |
| 1067 | EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state()); |
| 1068 | } |
| 1069 | |
| 1070 | // Test that we can create a session description from an SDP string from |
| 1071 | // FireFox, use it as a remote session description, generate an answer and use |
| 1072 | // the answer as a local description. |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame^] | 1073 | // TODO(mallinath): re-enable per |
| 1074 | // https://code.google.com/p/webrtc/issues/detail?id=2574 |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1075 | TEST_F(PeerConnectionInterfaceTest, DISABLED_ReceiveFireFoxOffer) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1076 | MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp); |
| 1077 | FakeConstraints constraints; |
| 1078 | constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, |
| 1079 | true); |
| 1080 | CreatePeerConnection(&constraints); |
| 1081 | AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label"); |
| 1082 | SessionDescriptionInterface* desc = |
| 1083 | webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, |
| 1084 | webrtc::kFireFoxSdpOffer); |
| 1085 | EXPECT_TRUE(DoSetSessionDescription(desc, false)); |
| 1086 | CreateAnswerAsLocalDescription(); |
| 1087 | ASSERT_TRUE(pc_->local_description() != NULL); |
| 1088 | ASSERT_TRUE(pc_->remote_description() != NULL); |
| 1089 | |
| 1090 | const cricket::ContentInfo* content = |
| 1091 | cricket::GetFirstAudioContent(pc_->local_description()->description()); |
| 1092 | ASSERT_TRUE(content != NULL); |
| 1093 | EXPECT_FALSE(content->rejected); |
| 1094 | |
| 1095 | content = |
| 1096 | cricket::GetFirstVideoContent(pc_->local_description()->description()); |
| 1097 | ASSERT_TRUE(content != NULL); |
| 1098 | EXPECT_FALSE(content->rejected); |
| 1099 | |
| 1100 | content = |
| 1101 | cricket::GetFirstDataContent(pc_->local_description()->description()); |
| 1102 | ASSERT_TRUE(content != NULL); |
| 1103 | EXPECT_TRUE(content->rejected); |
| 1104 | } |
| 1105 | |
| 1106 | // Test that we can create an audio only offer and receive an answer with a |
| 1107 | // limited set of audio codecs and receive an updated offer with more audio |
| 1108 | // codecs, where the added codecs are not supported. |
| 1109 | TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) { |
| 1110 | CreatePeerConnection(); |
| 1111 | AddVoiceStream("audio_label"); |
| 1112 | CreateOfferAsLocalDescription(); |
| 1113 | |
| 1114 | SessionDescriptionInterface* answer = |
| 1115 | webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer, |
| 1116 | webrtc::kAudioSdp); |
| 1117 | EXPECT_TRUE(DoSetSessionDescription(answer, false)); |
| 1118 | |
| 1119 | SessionDescriptionInterface* updated_offer = |
| 1120 | webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, |
| 1121 | webrtc::kAudioSdpWithUnsupportedCodecs); |
| 1122 | EXPECT_TRUE(DoSetSessionDescription(updated_offer, false)); |
| 1123 | CreateAnswerAsLocalDescription(); |
| 1124 | } |
| 1125 | |
| 1126 | // Test that PeerConnection::Close changes the states to closed and all remote |
| 1127 | // tracks change state to ended. |
| 1128 | TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) { |
| 1129 | // Initialize a PeerConnection and negotiate local and remote session |
| 1130 | // description. |
| 1131 | InitiateCall(); |
| 1132 | ASSERT_EQ(1u, pc_->local_streams()->count()); |
| 1133 | ASSERT_EQ(1u, pc_->remote_streams()->count()); |
| 1134 | |
| 1135 | pc_->Close(); |
| 1136 | |
| 1137 | EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state()); |
| 1138 | EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed, |
| 1139 | pc_->ice_connection_state()); |
| 1140 | EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete, |
| 1141 | pc_->ice_gathering_state()); |
| 1142 | |
| 1143 | EXPECT_EQ(1u, pc_->local_streams()->count()); |
| 1144 | EXPECT_EQ(1u, pc_->remote_streams()->count()); |
| 1145 | |
| 1146 | scoped_refptr<MediaStreamInterface> remote_stream = |
| 1147 | pc_->remote_streams()->at(0); |
| 1148 | EXPECT_EQ(MediaStreamTrackInterface::kEnded, |
| 1149 | remote_stream->GetVideoTracks()[0]->state()); |
| 1150 | EXPECT_EQ(MediaStreamTrackInterface::kEnded, |
| 1151 | remote_stream->GetAudioTracks()[0]->state()); |
| 1152 | } |
| 1153 | |
| 1154 | // Test that PeerConnection methods fails gracefully after |
| 1155 | // PeerConnection::Close has been called. |
| 1156 | TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) { |
| 1157 | CreatePeerConnection(); |
| 1158 | AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label"); |
| 1159 | CreateOfferAsRemoteDescription(); |
| 1160 | CreateAnswerAsLocalDescription(); |
| 1161 | |
| 1162 | ASSERT_EQ(1u, pc_->local_streams()->count()); |
| 1163 | scoped_refptr<MediaStreamInterface> local_stream = |
| 1164 | pc_->local_streams()->at(0); |
| 1165 | |
| 1166 | pc_->Close(); |
| 1167 | |
| 1168 | pc_->RemoveStream(local_stream); |
| 1169 | EXPECT_FALSE(pc_->AddStream(local_stream, NULL)); |
| 1170 | |
| 1171 | ASSERT_FALSE(local_stream->GetAudioTracks().empty()); |
| 1172 | talk_base::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender( |
| 1173 | pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0])); |
wu@webrtc.org | 6603736 | 2013-08-13 00:09:35 +0000 | [diff] [blame] | 1174 | EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1175 | |
| 1176 | EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL); |
| 1177 | |
| 1178 | EXPECT_TRUE(pc_->local_description() != NULL); |
| 1179 | EXPECT_TRUE(pc_->remote_description() != NULL); |
| 1180 | |
| 1181 | talk_base::scoped_ptr<SessionDescriptionInterface> offer; |
| 1182 | EXPECT_TRUE(DoCreateOffer(offer.use())); |
| 1183 | talk_base::scoped_ptr<SessionDescriptionInterface> answer; |
| 1184 | EXPECT_TRUE(DoCreateAnswer(answer.use())); |
| 1185 | |
| 1186 | std::string sdp; |
| 1187 | ASSERT_TRUE(pc_->remote_description()->ToString(&sdp)); |
| 1188 | SessionDescriptionInterface* remote_offer = |
| 1189 | webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, |
| 1190 | sdp, NULL); |
| 1191 | EXPECT_FALSE(DoSetRemoteDescription(remote_offer)); |
| 1192 | |
| 1193 | ASSERT_TRUE(pc_->local_description()->ToString(&sdp)); |
| 1194 | SessionDescriptionInterface* local_offer = |
| 1195 | webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer, |
| 1196 | sdp, NULL); |
| 1197 | EXPECT_FALSE(DoSetLocalDescription(local_offer)); |
| 1198 | } |
| 1199 | |
| 1200 | // Test that GetStats can still be called after PeerConnection::Close. |
| 1201 | TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) { |
| 1202 | InitiateCall(); |
| 1203 | pc_->Close(); |
| 1204 | DoGetStats(NULL); |
| 1205 | } |