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