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