blob: a69bfa0da0da91647e68e1d7c93ead368a2dcec5 [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
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000262 // DTLS does not work in a loopback call, so is disabled for most of the
263 // tests in this file. We only create a FakeIdentityService if the test
264 // explicitly sets the constraint.
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000265 FakeIdentityService* dtls_service = NULL;
266 bool dtls;
267 if (FindConstraint(constraints,
268 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
269 &dtls,
270 NULL) && dtls) {
271 dtls_service = new FakeIdentityService();
272 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 pc_ = pc_factory_->CreatePeerConnection(servers, constraints,
274 port_allocator_factory_.get(),
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000275 dtls_service,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 &observer_);
277 ASSERT_TRUE(pc_.get() != NULL);
278 observer_.SetPeerConnectionInterface(pc_.get());
279 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
280 }
281
282 void CreatePeerConnectionWithDifferentConfigurations() {
283 CreatePeerConnection(kStunAddressOnly, "", NULL);
284 EXPECT_EQ(1u, port_allocator_factory_->stun_configs().size());
285 EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
286 EXPECT_EQ("address",
287 port_allocator_factory_->stun_configs()[0].server.hostname());
288 EXPECT_EQ(kDefaultStunPort,
289 port_allocator_factory_->stun_configs()[0].server.port());
290
291 CreatePeerConnection(kStunInvalidPort, "", NULL);
292 EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size());
293 EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
294
295 CreatePeerConnection(kStunAddressPortAndMore1, "", NULL);
296 EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size());
297 EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
298
299 CreatePeerConnection(kStunAddressPortAndMore2, "", NULL);
300 EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size());
301 EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
302
303 CreatePeerConnection(kTurnIceServerUri, kTurnPassword, NULL);
304 EXPECT_EQ(1u, port_allocator_factory_->stun_configs().size());
305 EXPECT_EQ(1u, port_allocator_factory_->turn_configs().size());
306 EXPECT_EQ(kTurnUsername,
307 port_allocator_factory_->turn_configs()[0].username);
308 EXPECT_EQ(kTurnPassword,
309 port_allocator_factory_->turn_configs()[0].password);
310 EXPECT_EQ(kTurnHostname,
311 port_allocator_factory_->turn_configs()[0].server.hostname());
312 EXPECT_EQ(kTurnHostname,
313 port_allocator_factory_->stun_configs()[0].server.hostname());
314 }
315
316 void ReleasePeerConnection() {
317 pc_ = NULL;
318 observer_.SetPeerConnectionInterface(NULL);
319 }
320
321 void AddStream(const std::string& label) {
322 // Create a local stream.
323 scoped_refptr<MediaStreamInterface> stream(
324 pc_factory_->CreateLocalMediaStream(label));
325 scoped_refptr<VideoSourceInterface> video_source(
326 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
327 scoped_refptr<VideoTrackInterface> video_track(
328 pc_factory_->CreateVideoTrack(label + "v0", video_source));
329 stream->AddTrack(video_track.get());
330 EXPECT_TRUE(pc_->AddStream(stream, NULL));
331 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
332 observer_.renegotiation_needed_ = false;
333 }
334
335 void AddVoiceStream(const std::string& label) {
336 // Create a local stream.
337 scoped_refptr<MediaStreamInterface> stream(
338 pc_factory_->CreateLocalMediaStream(label));
339 scoped_refptr<AudioTrackInterface> audio_track(
340 pc_factory_->CreateAudioTrack(label + "a0", NULL));
341 stream->AddTrack(audio_track.get());
342 EXPECT_TRUE(pc_->AddStream(stream, NULL));
343 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
344 observer_.renegotiation_needed_ = false;
345 }
346
347 void AddAudioVideoStream(const std::string& stream_label,
348 const std::string& audio_track_label,
349 const std::string& video_track_label) {
350 // Create a local stream.
351 scoped_refptr<MediaStreamInterface> stream(
352 pc_factory_->CreateLocalMediaStream(stream_label));
353 scoped_refptr<AudioTrackInterface> audio_track(
354 pc_factory_->CreateAudioTrack(
355 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
356 stream->AddTrack(audio_track.get());
357 scoped_refptr<VideoTrackInterface> video_track(
358 pc_factory_->CreateVideoTrack(video_track_label, NULL));
359 stream->AddTrack(video_track.get());
360 EXPECT_TRUE(pc_->AddStream(stream, NULL));
361 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
362 observer_.renegotiation_needed_ = false;
363 }
364
365 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc, bool offer) {
366 talk_base::scoped_refptr<MockCreateSessionDescriptionObserver>
367 observer(new talk_base::RefCountedObject<
368 MockCreateSessionDescriptionObserver>());
369 if (offer) {
370 pc_->CreateOffer(observer, NULL);
371 } else {
372 pc_->CreateAnswer(observer, NULL);
373 }
374 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
375 *desc = observer->release_desc();
376 return observer->result();
377 }
378
379 bool DoCreateOffer(SessionDescriptionInterface** desc) {
380 return DoCreateOfferAnswer(desc, true);
381 }
382
383 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
384 return DoCreateOfferAnswer(desc, false);
385 }
386
387 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
388 talk_base::scoped_refptr<MockSetSessionDescriptionObserver>
389 observer(new talk_base::RefCountedObject<
390 MockSetSessionDescriptionObserver>());
391 if (local) {
392 pc_->SetLocalDescription(observer, desc);
393 } else {
394 pc_->SetRemoteDescription(observer, desc);
395 }
396 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
397 return observer->result();
398 }
399
400 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
401 return DoSetSessionDescription(desc, true);
402 }
403
404 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
405 return DoSetSessionDescription(desc, false);
406 }
407
408 // Calls PeerConnection::GetStats and check the return value.
409 // It does not verify the values in the StatReports since a RTCP packet might
410 // be required.
411 bool DoGetStats(MediaStreamTrackInterface* track) {
412 talk_base::scoped_refptr<MockStatsObserver> observer(
413 new talk_base::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000414 if (!pc_->GetStats(
415 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416 return false;
417 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
418 return observer->called();
419 }
420
421 void InitiateCall() {
422 CreatePeerConnection();
423 // Create a local stream with audio&video tracks.
424 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
425 CreateOfferReceiveAnswer();
426 }
427
428 // Verify that RTP Header extensions has been negotiated for audio and video.
429 void VerifyRemoteRtpHeaderExtensions() {
430 const cricket::MediaContentDescription* desc =
431 cricket::GetFirstAudioContentDescription(
432 pc_->remote_description()->description());
433 ASSERT_TRUE(desc != NULL);
434 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
435
436 desc = cricket::GetFirstVideoContentDescription(
437 pc_->remote_description()->description());
438 ASSERT_TRUE(desc != NULL);
439 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
440 }
441
442 void CreateOfferAsRemoteDescription() {
443 talk_base::scoped_ptr<SessionDescriptionInterface> offer;
444 EXPECT_TRUE(DoCreateOffer(offer.use()));
445 std::string sdp;
446 EXPECT_TRUE(offer->ToString(&sdp));
447 SessionDescriptionInterface* remote_offer =
448 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
449 sdp, NULL);
450 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
451 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
452 }
453
454 void CreateAnswerAsLocalDescription() {
455 scoped_ptr<SessionDescriptionInterface> answer;
456 EXPECT_TRUE(DoCreateAnswer(answer.use()));
457
458 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
459 // audio codec change, even if the parameter has nothing to do with
460 // receiving. Not all parameters are serialized to SDP.
461 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
462 // the SessionDescription, it is necessary to do that here to in order to
463 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
464 // https://code.google.com/p/webrtc/issues/detail?id=1356
465 std::string sdp;
466 EXPECT_TRUE(answer->ToString(&sdp));
467 SessionDescriptionInterface* new_answer =
468 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
469 sdp, NULL);
470 EXPECT_TRUE(DoSetLocalDescription(new_answer));
471 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
472 }
473
474 void CreatePrAnswerAsLocalDescription() {
475 scoped_ptr<SessionDescriptionInterface> answer;
476 EXPECT_TRUE(DoCreateAnswer(answer.use()));
477
478 std::string sdp;
479 EXPECT_TRUE(answer->ToString(&sdp));
480 SessionDescriptionInterface* pr_answer =
481 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
482 sdp, NULL);
483 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
484 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
485 }
486
487 void CreateOfferReceiveAnswer() {
488 CreateOfferAsLocalDescription();
489 std::string sdp;
490 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
491 CreateAnswerAsRemoteDescription(sdp);
492 }
493
494 void CreateOfferAsLocalDescription() {
495 talk_base::scoped_ptr<SessionDescriptionInterface> offer;
496 ASSERT_TRUE(DoCreateOffer(offer.use()));
497 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
498 // audio codec change, even if the parameter has nothing to do with
499 // receiving. Not all parameters are serialized to SDP.
500 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
501 // the SessionDescription, it is necessary to do that here to in order to
502 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
503 // https://code.google.com/p/webrtc/issues/detail?id=1356
504 std::string sdp;
505 EXPECT_TRUE(offer->ToString(&sdp));
506 SessionDescriptionInterface* new_offer =
507 webrtc::CreateSessionDescription(
508 SessionDescriptionInterface::kOffer,
509 sdp, NULL);
510
511 EXPECT_TRUE(DoSetLocalDescription(new_offer));
512 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000513 // Wait for the ice_complete message, so that SDP will have candidates.
514 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 }
516
517 void CreateAnswerAsRemoteDescription(const std::string& offer) {
518 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
519 SessionDescriptionInterface::kAnswer);
520 EXPECT_TRUE(answer->Initialize(offer, NULL));
521 EXPECT_TRUE(DoSetRemoteDescription(answer));
522 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
523 }
524
525 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& offer) {
526 webrtc::JsepSessionDescription* pr_answer =
527 new webrtc::JsepSessionDescription(
528 SessionDescriptionInterface::kPrAnswer);
529 EXPECT_TRUE(pr_answer->Initialize(offer, NULL));
530 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
531 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
532 webrtc::JsepSessionDescription* answer =
533 new webrtc::JsepSessionDescription(
534 SessionDescriptionInterface::kAnswer);
535 EXPECT_TRUE(answer->Initialize(offer, NULL));
536 EXPECT_TRUE(DoSetRemoteDescription(answer));
537 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
538 }
539
540 // Help function used for waiting until a the last signaled remote stream has
541 // the same label as |stream_label|. In a few of the tests in this file we
542 // answer with the same session description as we offer and thus we can
543 // check if OnAddStream have been called with the same stream as we offer to
544 // send.
545 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
546 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
547 }
548
549 // Creates an offer and applies it as a local session description.
550 // Creates an answer with the same SDP an the offer but removes all lines
551 // that start with a:ssrc"
552 void CreateOfferReceiveAnswerWithoutSsrc() {
553 CreateOfferAsLocalDescription();
554 std::string sdp;
555 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
556 SetSsrcToZero(&sdp);
557 CreateAnswerAsRemoteDescription(sdp);
558 }
559
560 scoped_refptr<FakePortAllocatorFactory> port_allocator_factory_;
561 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
562 scoped_refptr<PeerConnectionInterface> pc_;
563 MockPeerConnectionObserver observer_;
564};
565
566TEST_F(PeerConnectionInterfaceTest,
567 CreatePeerConnectionWithDifferentConfigurations) {
568 CreatePeerConnectionWithDifferentConfigurations();
569}
570
571TEST_F(PeerConnectionInterfaceTest, AddStreams) {
572 CreatePeerConnection();
573 AddStream(kStreamLabel1);
574 AddVoiceStream(kStreamLabel2);
575 ASSERT_EQ(2u, pc_->local_streams()->count());
576
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000577 // Test we can add multiple local streams to one peerconnection.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578 scoped_refptr<MediaStreamInterface> stream(
579 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
580 scoped_refptr<AudioTrackInterface> audio_track(
581 pc_factory_->CreateAudioTrack(
582 kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
583 stream->AddTrack(audio_track.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000584 EXPECT_TRUE(pc_->AddStream(stream, NULL));
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000585 EXPECT_EQ(3u, pc_->local_streams()->count());
586
587 // Remove the third stream.
588 pc_->RemoveStream(pc_->local_streams()->at(2));
589 EXPECT_EQ(2u, pc_->local_streams()->count());
590
591 // Remove the second stream.
592 pc_->RemoveStream(pc_->local_streams()->at(1));
593 EXPECT_EQ(1u, pc_->local_streams()->count());
594
595 // Remove the first stream.
596 pc_->RemoveStream(pc_->local_streams()->at(0));
597 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598}
599
600TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
601 CreatePeerConnection();
602 AddStream(kStreamLabel1);
603 ASSERT_EQ(1u, pc_->local_streams()->count());
604 pc_->RemoveStream(pc_->local_streams()->at(0));
605 EXPECT_EQ(0u, pc_->local_streams()->count());
606}
607
608TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
609 InitiateCall();
610 WaitAndVerifyOnAddStream(kStreamLabel1);
611 VerifyRemoteRtpHeaderExtensions();
612}
613
614TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
615 CreatePeerConnection();
616 AddStream(kStreamLabel1);
617 CreateOfferAsLocalDescription();
618 std::string offer;
619 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
620 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
621 WaitAndVerifyOnAddStream(kStreamLabel1);
622}
623
624TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
625 CreatePeerConnection();
626 AddStream(kStreamLabel1);
627
628 CreateOfferAsRemoteDescription();
629 CreateAnswerAsLocalDescription();
630
631 WaitAndVerifyOnAddStream(kStreamLabel1);
632}
633
634TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
635 CreatePeerConnection();
636 AddStream(kStreamLabel1);
637
638 CreateOfferAsRemoteDescription();
639 CreatePrAnswerAsLocalDescription();
640 CreateAnswerAsLocalDescription();
641
642 WaitAndVerifyOnAddStream(kStreamLabel1);
643}
644
645TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
646 InitiateCall();
647 ASSERT_EQ(1u, pc_->remote_streams()->count());
648 pc_->RemoveStream(pc_->local_streams()->at(0));
649 CreateOfferReceiveAnswer();
650 EXPECT_EQ(0u, pc_->remote_streams()->count());
651 AddStream(kStreamLabel1);
652 CreateOfferReceiveAnswer();
653}
654
655// Tests that after negotiating an audio only call, the respondent can perform a
656// renegotiation that removes the audio stream.
657TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
658 CreatePeerConnection();
659 AddVoiceStream(kStreamLabel1);
660 CreateOfferAsRemoteDescription();
661 CreateAnswerAsLocalDescription();
662
663 ASSERT_EQ(1u, pc_->remote_streams()->count());
664 pc_->RemoveStream(pc_->local_streams()->at(0));
665 CreateOfferReceiveAnswer();
666 EXPECT_EQ(0u, pc_->remote_streams()->count());
667}
668
669// Test that candidates are generated and that we can parse our own candidates.
670TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
671 CreatePeerConnection();
672
673 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
674 // SetRemoteDescription takes ownership of offer.
675 SessionDescriptionInterface* offer = NULL;
676 AddStream(kStreamLabel1);
677 EXPECT_TRUE(DoCreateOffer(&offer));
678 EXPECT_TRUE(DoSetRemoteDescription(offer));
679
680 // SetLocalDescription takes ownership of answer.
681 SessionDescriptionInterface* answer = NULL;
682 EXPECT_TRUE(DoCreateAnswer(&answer));
683 EXPECT_TRUE(DoSetLocalDescription(answer));
684
685 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
686 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
687
688 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
689}
690
691// Test that the CreateOffer and CreatAnswer will fail if the track labels are
692// not unique.
693TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
694 CreatePeerConnection();
695 // Create a regular offer for the CreateAnswer test later.
696 SessionDescriptionInterface* offer = NULL;
697 EXPECT_TRUE(DoCreateOffer(&offer));
698 EXPECT_TRUE(offer != NULL);
699 delete offer;
700 offer = NULL;
701
702 // Create a local stream with audio&video tracks having same label.
703 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
704
705 // Test CreateOffer
706 EXPECT_FALSE(DoCreateOffer(&offer));
707
708 // Test CreateAnswer
709 SessionDescriptionInterface* answer = NULL;
710 EXPECT_FALSE(DoCreateAnswer(&answer));
711}
712
713// Test that we will get different SSRCs for each tracks in the offer and answer
714// we created.
715TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
716 CreatePeerConnection();
717 // Create a local stream with audio&video tracks having different labels.
718 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
719
720 // Test CreateOffer
721 scoped_ptr<SessionDescriptionInterface> offer;
722 EXPECT_TRUE(DoCreateOffer(offer.use()));
723 int audio_ssrc = 0;
724 int video_ssrc = 0;
725 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
726 &audio_ssrc));
727 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
728 &video_ssrc));
729 EXPECT_NE(audio_ssrc, video_ssrc);
730
731 // Test CreateAnswer
732 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
733 scoped_ptr<SessionDescriptionInterface> answer;
734 EXPECT_TRUE(DoCreateAnswer(answer.use()));
735 audio_ssrc = 0;
736 video_ssrc = 0;
737 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
738 &audio_ssrc));
739 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
740 &video_ssrc));
741 EXPECT_NE(audio_ssrc, video_ssrc);
742}
743
744// Test that we can specify a certain track that we want statistics about.
745TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
746 InitiateCall();
747 ASSERT_LT(0u, pc_->remote_streams()->count());
748 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
749 scoped_refptr<MediaStreamTrackInterface> remote_audio =
750 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
751 EXPECT_TRUE(DoGetStats(remote_audio));
752
753 // Remove the stream. Since we are sending to our selves the local
754 // and the remote stream is the same.
755 pc_->RemoveStream(pc_->local_streams()->at(0));
756 // Do a re-negotiation.
757 CreateOfferReceiveAnswer();
758
759 ASSERT_EQ(0u, pc_->remote_streams()->count());
760
761 // Test that we still can get statistics for the old track. Even if it is not
762 // sent any longer.
763 EXPECT_TRUE(DoGetStats(remote_audio));
764}
765
766// Test that we can get stats on a video track.
767TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
768 InitiateCall();
769 ASSERT_LT(0u, pc_->remote_streams()->count());
770 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
771 scoped_refptr<MediaStreamTrackInterface> remote_video =
772 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
773 EXPECT_TRUE(DoGetStats(remote_video));
774}
775
776// Test that we don't get statistics for an invalid track.
777TEST_F(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
778 InitiateCall();
779 scoped_refptr<AudioTrackInterface> unknown_audio_track(
780 pc_factory_->CreateAudioTrack("unknown track", NULL));
781 EXPECT_FALSE(DoGetStats(unknown_audio_track));
782}
783
784// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 FakeConstraints constraints;
787 constraints.SetAllowRtpDataChannels();
788 CreatePeerConnection(&constraints);
789 scoped_refptr<DataChannelInterface> data1 =
790 pc_->CreateDataChannel("test1", NULL);
791 scoped_refptr<DataChannelInterface> data2 =
792 pc_->CreateDataChannel("test2", NULL);
793 ASSERT_TRUE(data1 != NULL);
794 talk_base::scoped_ptr<MockDataChannelObserver> observer1(
795 new MockDataChannelObserver(data1));
796 talk_base::scoped_ptr<MockDataChannelObserver> observer2(
797 new MockDataChannelObserver(data2));
798
799 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
800 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
801 std::string data_to_send1 = "testing testing";
802 std::string data_to_send2 = "testing something else";
803 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
804
805 CreateOfferReceiveAnswer();
806 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
807 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
808
809 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
810 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
811 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
812 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
813
814 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
815 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
816
817 data1->Close();
818 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
819 CreateOfferReceiveAnswer();
820 EXPECT_FALSE(observer1->IsOpen());
821 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
822 EXPECT_TRUE(observer2->IsOpen());
823
824 data_to_send2 = "testing something else again";
825 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
826
827 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
828}
829
830// This test verifies that sendnig binary data over RTP data channels should
831// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833 FakeConstraints constraints;
834 constraints.SetAllowRtpDataChannels();
835 CreatePeerConnection(&constraints);
836 scoped_refptr<DataChannelInterface> data1 =
837 pc_->CreateDataChannel("test1", NULL);
838 scoped_refptr<DataChannelInterface> data2 =
839 pc_->CreateDataChannel("test2", NULL);
840 ASSERT_TRUE(data1 != NULL);
841 talk_base::scoped_ptr<MockDataChannelObserver> observer1(
842 new MockDataChannelObserver(data1));
843 talk_base::scoped_ptr<MockDataChannelObserver> observer2(
844 new MockDataChannelObserver(data2));
845
846 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
847 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
848
849 CreateOfferReceiveAnswer();
850 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
851 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
852
853 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
854 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
855
856 talk_base::Buffer buffer("test", 4);
857 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
858}
859
860// This test setup a RTP data channels in loop back and test that a channel is
861// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 FakeConstraints constraints;
864 constraints.SetAllowRtpDataChannels();
865 CreatePeerConnection(&constraints);
866 scoped_refptr<DataChannelInterface> data1 =
867 pc_->CreateDataChannel("test1", NULL);
868 talk_base::scoped_ptr<MockDataChannelObserver> observer1(
869 new MockDataChannelObserver(data1));
870
871 CreateOfferReceiveAnswerWithoutSsrc();
872
873 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
874
875 data1->Close();
876 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
877 CreateOfferReceiveAnswerWithoutSsrc();
878 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
879 EXPECT_FALSE(observer1->IsOpen());
880}
881
882// This test that if a data channel is added in an answer a receive only channel
883// channel is created.
884TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
885 FakeConstraints constraints;
886 constraints.SetAllowRtpDataChannels();
887 CreatePeerConnection(&constraints);
888
889 std::string offer_label = "offer_channel";
890 scoped_refptr<DataChannelInterface> offer_channel =
891 pc_->CreateDataChannel(offer_label, NULL);
892
893 CreateOfferAsLocalDescription();
894
895 // Replace the data channel label in the offer and apply it as an answer.
896 std::string receive_label = "answer_channel";
897 std::string sdp;
898 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
899 talk_base::replace_substrs(offer_label.c_str(), offer_label.length(),
900 receive_label.c_str(), receive_label.length(),
901 &sdp);
902 CreateAnswerAsRemoteDescription(sdp);
903
904 // Verify that a new incoming data channel has been created and that
905 // it is open but can't we written to.
906 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
907 DataChannelInterface* received_channel = observer_.last_datachannel_;
908 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
909 EXPECT_EQ(receive_label, received_channel->label());
910 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
911
912 // Verify that the channel we initially offered has been rejected.
913 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
914
915 // Do another offer / answer exchange and verify that the data channel is
916 // opened.
917 CreateOfferReceiveAnswer();
918 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
919 kTimeout);
920}
921
922// This test that no data channel is returned if a reliable channel is
923// requested.
924// TODO(perkj): Remove this test once reliable channels are implemented.
925TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
926 FakeConstraints constraints;
927 constraints.SetAllowRtpDataChannels();
928 CreatePeerConnection(&constraints);
929
930 std::string label = "test";
931 webrtc::DataChannelInit config;
932 config.reliable = true;
933 scoped_refptr<DataChannelInterface> channel =
934 pc_->CreateDataChannel(label, &config);
935 EXPECT_TRUE(channel == NULL);
936}
937
938// This tests that a SCTP data channel is returned using different
939// DataChannelInit configurations.
940TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
941 FakeConstraints constraints;
942 constraints.SetAllowDtlsSctpDataChannels();
943 CreatePeerConnection(&constraints);
944
945 webrtc::DataChannelInit config;
946
947 scoped_refptr<DataChannelInterface> channel =
948 pc_->CreateDataChannel("1", &config);
949 EXPECT_TRUE(channel != NULL);
950 EXPECT_TRUE(channel->reliable());
951
952 config.ordered = false;
953 channel = pc_->CreateDataChannel("2", &config);
954 EXPECT_TRUE(channel != NULL);
955 EXPECT_TRUE(channel->reliable());
956
957 config.ordered = true;
958 config.maxRetransmits = 0;
959 channel = pc_->CreateDataChannel("3", &config);
960 EXPECT_TRUE(channel != NULL);
961 EXPECT_FALSE(channel->reliable());
962
963 config.maxRetransmits = -1;
964 config.maxRetransmitTime = 0;
965 channel = pc_->CreateDataChannel("4", &config);
966 EXPECT_TRUE(channel != NULL);
967 EXPECT_FALSE(channel->reliable());
968}
969
970// This tests that no data channel is returned if both maxRetransmits and
971// maxRetransmitTime are set for SCTP data channels.
972TEST_F(PeerConnectionInterfaceTest,
973 CreateSctpDataChannelShouldFailForInvalidConfig) {
974 FakeConstraints constraints;
975 constraints.SetAllowDtlsSctpDataChannels();
976 CreatePeerConnection(&constraints);
977
978 std::string label = "test";
979 webrtc::DataChannelInit config;
980 config.maxRetransmits = 0;
981 config.maxRetransmitTime = 0;
982
983 scoped_refptr<DataChannelInterface> channel =
984 pc_->CreateDataChannel(label, &config);
985 EXPECT_TRUE(channel == NULL);
986}
987
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988// The test verifies that creating a SCTP data channel with an id already in use
989// or out of range should fail.
990TEST_F(PeerConnectionInterfaceTest,
991 CreateSctpDataChannelWithInvalidIdShouldFail) {
992 FakeConstraints constraints;
993 constraints.SetAllowDtlsSctpDataChannels();
994 CreatePeerConnection(&constraints);
995
996 webrtc::DataChannelInit config;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000997 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000999 config.id = 1;
1000 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001 EXPECT_TRUE(channel != NULL);
1002 EXPECT_EQ(1, channel->id());
1003
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 channel = pc_->CreateDataChannel("x", &config);
1005 EXPECT_TRUE(channel == NULL);
1006
1007 config.id = cricket::kMaxSctpSid;
1008 channel = pc_->CreateDataChannel("max", &config);
1009 EXPECT_TRUE(channel != NULL);
1010 EXPECT_EQ(config.id, channel->id());
1011
1012 config.id = cricket::kMaxSctpSid + 1;
1013 channel = pc_->CreateDataChannel("x", &config);
1014 EXPECT_TRUE(channel == NULL);
1015}
1016
1017// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019 FakeConstraints constraints;
1020 constraints.SetAllowRtpDataChannels();
1021 CreatePeerConnection(&constraints);
1022
1023 scoped_refptr<DataChannelInterface> data1 =
1024 pc_->CreateDataChannel("test1", NULL);
1025 scoped_refptr<DataChannelInterface> data2 =
1026 pc_->CreateDataChannel("test2", NULL);
1027 ASSERT_TRUE(data1 != NULL);
1028 talk_base::scoped_ptr<MockDataChannelObserver> observer1(
1029 new MockDataChannelObserver(data1));
1030 talk_base::scoped_ptr<MockDataChannelObserver> observer2(
1031 new MockDataChannelObserver(data2));
1032
1033 CreateOfferReceiveAnswer();
1034 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1035 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1036
1037 ReleasePeerConnection();
1038 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1039 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1040}
1041
1042// This test that data channels can be rejected in an answer.
1043TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1044 FakeConstraints constraints;
1045 constraints.SetAllowRtpDataChannels();
1046 CreatePeerConnection(&constraints);
1047
1048 scoped_refptr<DataChannelInterface> offer_channel(
1049 pc_->CreateDataChannel("offer_channel", NULL));
1050
1051 CreateOfferAsLocalDescription();
1052
1053 // Create an answer where the m-line for data channels are rejected.
1054 std::string sdp;
1055 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1056 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1057 SessionDescriptionInterface::kAnswer);
1058 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1059 cricket::ContentInfo* data_info =
1060 answer->description()->GetContentByName("data");
1061 data_info->rejected = true;
1062
1063 DoSetRemoteDescription(answer);
1064 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1065}
1066
1067// Test that we can create a session description from an SDP string from
1068// FireFox, use it as a remote session description, generate an answer and use
1069// the answer as a local description.
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001070TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071 MAYBE_SKIP_TEST(talk_base::SSLStreamAdapter::HaveDtlsSrtp);
1072 FakeConstraints constraints;
1073 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1074 true);
1075 CreatePeerConnection(&constraints);
1076 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1077 SessionDescriptionInterface* desc =
1078 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1079 webrtc::kFireFoxSdpOffer);
1080 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1081 CreateAnswerAsLocalDescription();
1082 ASSERT_TRUE(pc_->local_description() != NULL);
1083 ASSERT_TRUE(pc_->remote_description() != NULL);
1084
1085 const cricket::ContentInfo* content =
1086 cricket::GetFirstAudioContent(pc_->local_description()->description());
1087 ASSERT_TRUE(content != NULL);
1088 EXPECT_FALSE(content->rejected);
1089
1090 content =
1091 cricket::GetFirstVideoContent(pc_->local_description()->description());
1092 ASSERT_TRUE(content != NULL);
1093 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001094#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095 content =
1096 cricket::GetFirstDataContent(pc_->local_description()->description());
1097 ASSERT_TRUE(content != NULL);
1098 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001099#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001100}
1101
1102// Test that we can create an audio only offer and receive an answer with a
1103// limited set of audio codecs and receive an updated offer with more audio
1104// codecs, where the added codecs are not supported.
1105TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1106 CreatePeerConnection();
1107 AddVoiceStream("audio_label");
1108 CreateOfferAsLocalDescription();
1109
1110 SessionDescriptionInterface* answer =
1111 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
1112 webrtc::kAudioSdp);
1113 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1114
1115 SessionDescriptionInterface* updated_offer =
1116 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1117 webrtc::kAudioSdpWithUnsupportedCodecs);
1118 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1119 CreateAnswerAsLocalDescription();
1120}
1121
1122// Test that PeerConnection::Close changes the states to closed and all remote
1123// tracks change state to ended.
1124TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
1125 // Initialize a PeerConnection and negotiate local and remote session
1126 // description.
1127 InitiateCall();
1128 ASSERT_EQ(1u, pc_->local_streams()->count());
1129 ASSERT_EQ(1u, pc_->remote_streams()->count());
1130
1131 pc_->Close();
1132
1133 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
1134 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
1135 pc_->ice_connection_state());
1136 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
1137 pc_->ice_gathering_state());
1138
1139 EXPECT_EQ(1u, pc_->local_streams()->count());
1140 EXPECT_EQ(1u, pc_->remote_streams()->count());
1141
1142 scoped_refptr<MediaStreamInterface> remote_stream =
1143 pc_->remote_streams()->at(0);
1144 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1145 remote_stream->GetVideoTracks()[0]->state());
1146 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1147 remote_stream->GetAudioTracks()[0]->state());
1148}
1149
1150// Test that PeerConnection methods fails gracefully after
1151// PeerConnection::Close has been called.
1152TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
1153 CreatePeerConnection();
1154 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1155 CreateOfferAsRemoteDescription();
1156 CreateAnswerAsLocalDescription();
1157
1158 ASSERT_EQ(1u, pc_->local_streams()->count());
1159 scoped_refptr<MediaStreamInterface> local_stream =
1160 pc_->local_streams()->at(0);
1161
1162 pc_->Close();
1163
1164 pc_->RemoveStream(local_stream);
1165 EXPECT_FALSE(pc_->AddStream(local_stream, NULL));
1166
1167 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
1168 talk_base::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
1169 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00001170 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001171
1172 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
1173
1174 EXPECT_TRUE(pc_->local_description() != NULL);
1175 EXPECT_TRUE(pc_->remote_description() != NULL);
1176
1177 talk_base::scoped_ptr<SessionDescriptionInterface> offer;
1178 EXPECT_TRUE(DoCreateOffer(offer.use()));
1179 talk_base::scoped_ptr<SessionDescriptionInterface> answer;
1180 EXPECT_TRUE(DoCreateAnswer(answer.use()));
1181
1182 std::string sdp;
1183 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
1184 SessionDescriptionInterface* remote_offer =
1185 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1186 sdp, NULL);
1187 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
1188
1189 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
1190 SessionDescriptionInterface* local_offer =
1191 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1192 sdp, NULL);
1193 EXPECT_FALSE(DoSetLocalDescription(local_offer));
1194}
1195
1196// Test that GetStats can still be called after PeerConnection::Close.
1197TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
1198 InitiateCall();
1199 pc_->Close();
1200 DoGetStats(NULL);
1201}