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