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