blob: 03ceaf2e101418aa4c4029f7f76fea47a3a31bd9 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
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"
Henrik Boström5e56c592015-08-11 10:33:13 +020035#include "talk/app/webrtc/test/fakedtlsidentitystore.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"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000039#include "talk/media/base/fakevideocapturer.h"
40#include "talk/media/sctp/sctpdataengine.h"
41#include "talk/session/media/mediasession.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000042#include "webrtc/base/gunit.h"
43#include "webrtc/base/scoped_ptr.h"
44#include "webrtc/base/ssladapter.h"
45#include "webrtc/base/sslstreamadapter.h"
46#include "webrtc/base/stringutils.h"
47#include "webrtc/base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
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";
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000061static const uint32 kTimeout = 10000U;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062
63#define MAYBE_SKIP_TEST(feature) \
64 if (!(feature())) { \
65 LOG(LS_INFO) << "Feature disabled... skipping"; \
66 return; \
67 }
68
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000069using rtc::scoped_ptr;
70using rtc::scoped_refptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071using 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 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 virtual void OnSignalingChange(
136 PeerConnectionInterface::SignalingState new_state) {
137 EXPECT_EQ(pc_->signaling_state(), new_state);
138 state_ = new_state;
139 }
140 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
141 virtual void OnStateChange(StateType state_changed) {
142 if (pc_.get() == NULL)
143 return;
144 switch (state_changed) {
145 case kSignalingState:
146 // OnSignalingChange and OnStateChange(kSignalingState) should always
147 // be called approximately simultaneously. To ease testing, we require
148 // that they always be called in that order. This check verifies
149 // that OnSignalingChange has just been called.
150 EXPECT_EQ(pc_->signaling_state(), state_);
151 break;
152 case kIceState:
153 ADD_FAILURE();
154 break;
155 default:
156 ADD_FAILURE();
157 break;
158 }
159 }
160 virtual void OnAddStream(MediaStreamInterface* stream) {
161 last_added_stream_ = stream;
162 }
163 virtual void OnRemoveStream(MediaStreamInterface* stream) {
164 last_removed_stream_ = stream;
165 }
166 virtual void OnRenegotiationNeeded() {
167 renegotiation_needed_ = true;
168 }
169 virtual void OnDataChannel(DataChannelInterface* data_channel) {
170 last_datachannel_ = data_channel;
171 }
172
173 virtual void OnIceConnectionChange(
174 PeerConnectionInterface::IceConnectionState new_state) {
175 EXPECT_EQ(pc_->ice_connection_state(), new_state);
176 }
177 virtual void OnIceGatheringChange(
178 PeerConnectionInterface::IceGatheringState new_state) {
179 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
180 }
181 virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
182 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
183 pc_->ice_gathering_state());
184
185 std::string sdp;
186 EXPECT_TRUE(candidate->ToString(&sdp));
187 EXPECT_LT(0u, sdp.size());
188 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
189 candidate->sdp_mline_index(), sdp, NULL));
190 EXPECT_TRUE(last_candidate_.get() != NULL);
191 }
192 // TODO(bemasc): Remove this once callers transition to OnSignalingChange.
193 virtual void OnIceComplete() {
194 ice_complete_ = true;
195 // OnIceGatheringChange(IceGatheringCompleted) and OnIceComplete() should
196 // be called approximately simultaneously. For ease of testing, this
197 // check additionally requires that they be called in the above order.
198 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
199 pc_->ice_gathering_state());
200 }
201
202 // Returns the label of the last added stream.
203 // Empty string if no stream have been added.
204 std::string GetLastAddedStreamLabel() {
205 if (last_added_stream_.get())
206 return last_added_stream_->label();
207 return "";
208 }
209 std::string GetLastRemovedStreamLabel() {
210 if (last_removed_stream_.get())
211 return last_removed_stream_->label();
212 return "";
213 }
214
215 scoped_refptr<PeerConnectionInterface> pc_;
216 PeerConnectionInterface::SignalingState state_;
217 scoped_ptr<IceCandidateInterface> last_candidate_;
218 scoped_refptr<DataChannelInterface> last_datachannel_;
219 bool renegotiation_needed_;
220 bool ice_complete_;
221
222 private:
223 scoped_refptr<MediaStreamInterface> last_added_stream_;
224 scoped_refptr<MediaStreamInterface> last_removed_stream_;
225};
226
227} // namespace
228class PeerConnectionInterfaceTest : public testing::Test {
229 protected:
230 virtual void SetUp() {
231 pc_factory_ = webrtc::CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000232 rtc::Thread::Current(), rtc::Thread::Current(), NULL, NULL,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233 NULL);
234 ASSERT_TRUE(pc_factory_.get() != NULL);
235 }
236
237 void CreatePeerConnection() {
238 CreatePeerConnection("", "", NULL);
239 }
240
241 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
242 CreatePeerConnection("", "", constraints);
243 }
244
245 void CreatePeerConnection(const std::string& uri,
246 const std::string& password,
247 webrtc::MediaConstraintsInterface* constraints) {
248 PeerConnectionInterface::IceServer server;
249 PeerConnectionInterface::IceServers servers;
250 server.uri = uri;
251 server.password = password;
252 servers.push_back(server);
253
254 port_allocator_factory_ = FakePortAllocatorFactory::Create();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000255
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000256 // DTLS does not work in a loopback call, so is disabled for most of the
257 // tests in this file. We only create a FakeIdentityService if the test
258 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000259 FakeConstraints default_constraints;
260 if (!constraints) {
261 constraints = &default_constraints;
262
263 default_constraints.AddMandatory(
264 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
265 }
266
Henrik Boström5e56c592015-08-11 10:33:13 +0200267 scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000268 bool dtls;
269 if (FindConstraint(constraints,
270 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
271 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200272 nullptr) && dtls) {
273 dtls_identity_store.reset(new FakeDtlsIdentityStore());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000274 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 pc_ = pc_factory_->CreatePeerConnection(servers, constraints,
276 port_allocator_factory_.get(),
Henrik Boström5e56c592015-08-11 10:33:13 +0200277 dtls_identity_store.Pass(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278 &observer_);
279 ASSERT_TRUE(pc_.get() != NULL);
280 observer_.SetPeerConnectionInterface(pc_.get());
281 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
282 }
283
284 void CreatePeerConnectionWithDifferentConfigurations() {
285 CreatePeerConnection(kStunAddressOnly, "", NULL);
286 EXPECT_EQ(1u, port_allocator_factory_->stun_configs().size());
287 EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
288 EXPECT_EQ("address",
289 port_allocator_factory_->stun_configs()[0].server.hostname());
290 EXPECT_EQ(kDefaultStunPort,
291 port_allocator_factory_->stun_configs()[0].server.port());
292
293 CreatePeerConnection(kStunInvalidPort, "", NULL);
294 EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size());
295 EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
296
297 CreatePeerConnection(kStunAddressPortAndMore1, "", NULL);
298 EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size());
299 EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
300
301 CreatePeerConnection(kStunAddressPortAndMore2, "", NULL);
302 EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size());
303 EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
304
305 CreatePeerConnection(kTurnIceServerUri, kTurnPassword, NULL);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000306 EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307 EXPECT_EQ(1u, port_allocator_factory_->turn_configs().size());
308 EXPECT_EQ(kTurnUsername,
309 port_allocator_factory_->turn_configs()[0].username);
310 EXPECT_EQ(kTurnPassword,
311 port_allocator_factory_->turn_configs()[0].password);
312 EXPECT_EQ(kTurnHostname,
313 port_allocator_factory_->turn_configs()[0].server.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314 }
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());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000330 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331 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());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000342 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343 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());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000360 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
362 observer_.renegotiation_needed_ = false;
363 }
364
365 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc, bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000366 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
367 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368 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) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000388 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
389 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000390 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) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000412 rtc::scoped_refptr<MockStatsObserver> observer(
413 new rtc::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() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000443 rtc::scoped_ptr<SessionDescriptionInterface> offer;
pkasting@chromium.org005b6ff2015-01-30 19:41:42 +0000444 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445 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;
pkasting@chromium.org005b6ff2015-01-30 19:41:42 +0000456 ASSERT_TRUE(DoCreateAnswer(answer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457
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;
pkasting@chromium.org005b6ff2015-01-30 19:41:42 +0000476 ASSERT_TRUE(DoCreateAnswer(answer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477
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() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000495 rtc::scoped_ptr<SessionDescriptionInterface> offer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496 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());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000584 EXPECT_TRUE(pc_->AddStream(stream));
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;
pkasting@chromium.org005b6ff2015-01-30 19:41:42 +0000722 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 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;
pkasting@chromium.org005b6ff2015-01-30 19:41:42 +0000734 ASSERT_TRUE(DoCreateAnswer(answer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 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.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +0000777// TODO(tommi): Fix this test. DoGetStats will return true
778// for the unknown track (since GetStats is async), but no
779// data is returned for the track.
780TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 InitiateCall();
782 scoped_refptr<AudioTrackInterface> unknown_audio_track(
783 pc_factory_->CreateAudioTrack("unknown track", NULL));
784 EXPECT_FALSE(DoGetStats(unknown_audio_track));
785}
786
787// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 FakeConstraints constraints;
790 constraints.SetAllowRtpDataChannels();
791 CreatePeerConnection(&constraints);
792 scoped_refptr<DataChannelInterface> data1 =
793 pc_->CreateDataChannel("test1", NULL);
794 scoped_refptr<DataChannelInterface> data2 =
795 pc_->CreateDataChannel("test2", NULL);
796 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000797 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000799 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 new MockDataChannelObserver(data2));
801
802 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
803 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
804 std::string data_to_send1 = "testing testing";
805 std::string data_to_send2 = "testing something else";
806 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
807
808 CreateOfferReceiveAnswer();
809 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
810 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
811
812 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
813 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
814 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
815 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
816
817 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
818 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
819
820 data1->Close();
821 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
822 CreateOfferReceiveAnswer();
823 EXPECT_FALSE(observer1->IsOpen());
824 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
825 EXPECT_TRUE(observer2->IsOpen());
826
827 data_to_send2 = "testing something else again";
828 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
829
830 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
831}
832
833// This test verifies that sendnig binary data over RTP data channels should
834// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 FakeConstraints constraints;
837 constraints.SetAllowRtpDataChannels();
838 CreatePeerConnection(&constraints);
839 scoped_refptr<DataChannelInterface> data1 =
840 pc_->CreateDataChannel("test1", NULL);
841 scoped_refptr<DataChannelInterface> data2 =
842 pc_->CreateDataChannel("test2", NULL);
843 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000844 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000845 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000846 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 new MockDataChannelObserver(data2));
848
849 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
850 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
851
852 CreateOfferReceiveAnswer();
853 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
854 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
855
856 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
857 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
858
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000859 rtc::Buffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
861}
862
863// This test setup a RTP data channels in loop back and test that a channel is
864// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866 FakeConstraints constraints;
867 constraints.SetAllowRtpDataChannels();
868 CreatePeerConnection(&constraints);
869 scoped_refptr<DataChannelInterface> data1 =
870 pc_->CreateDataChannel("test1", NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000871 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 new MockDataChannelObserver(data1));
873
874 CreateOfferReceiveAnswerWithoutSsrc();
875
876 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
877
878 data1->Close();
879 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
880 CreateOfferReceiveAnswerWithoutSsrc();
881 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
882 EXPECT_FALSE(observer1->IsOpen());
883}
884
885// This test that if a data channel is added in an answer a receive only channel
886// channel is created.
887TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
888 FakeConstraints constraints;
889 constraints.SetAllowRtpDataChannels();
890 CreatePeerConnection(&constraints);
891
892 std::string offer_label = "offer_channel";
893 scoped_refptr<DataChannelInterface> offer_channel =
894 pc_->CreateDataChannel(offer_label, NULL);
895
896 CreateOfferAsLocalDescription();
897
898 // Replace the data channel label in the offer and apply it as an answer.
899 std::string receive_label = "answer_channel";
900 std::string sdp;
901 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000902 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903 receive_label.c_str(), receive_label.length(),
904 &sdp);
905 CreateAnswerAsRemoteDescription(sdp);
906
907 // Verify that a new incoming data channel has been created and that
908 // it is open but can't we written to.
909 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
910 DataChannelInterface* received_channel = observer_.last_datachannel_;
911 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
912 EXPECT_EQ(receive_label, received_channel->label());
913 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
914
915 // Verify that the channel we initially offered has been rejected.
916 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
917
918 // Do another offer / answer exchange and verify that the data channel is
919 // opened.
920 CreateOfferReceiveAnswer();
921 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
922 kTimeout);
923}
924
925// This test that no data channel is returned if a reliable channel is
926// requested.
927// TODO(perkj): Remove this test once reliable channels are implemented.
928TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
929 FakeConstraints constraints;
930 constraints.SetAllowRtpDataChannels();
931 CreatePeerConnection(&constraints);
932
933 std::string label = "test";
934 webrtc::DataChannelInit config;
935 config.reliable = true;
936 scoped_refptr<DataChannelInterface> channel =
937 pc_->CreateDataChannel(label, &config);
938 EXPECT_TRUE(channel == NULL);
939}
940
941// This tests that a SCTP data channel is returned using different
942// DataChannelInit configurations.
943TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
944 FakeConstraints constraints;
945 constraints.SetAllowDtlsSctpDataChannels();
946 CreatePeerConnection(&constraints);
947
948 webrtc::DataChannelInit config;
949
950 scoped_refptr<DataChannelInterface> channel =
951 pc_->CreateDataChannel("1", &config);
952 EXPECT_TRUE(channel != NULL);
953 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000954 EXPECT_TRUE(observer_.renegotiation_needed_);
955 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000956
957 config.ordered = false;
958 channel = pc_->CreateDataChannel("2", &config);
959 EXPECT_TRUE(channel != NULL);
960 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000961 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962
963 config.ordered = true;
964 config.maxRetransmits = 0;
965 channel = pc_->CreateDataChannel("3", &config);
966 EXPECT_TRUE(channel != NULL);
967 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000968 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969
970 config.maxRetransmits = -1;
971 config.maxRetransmitTime = 0;
972 channel = pc_->CreateDataChannel("4", &config);
973 EXPECT_TRUE(channel != NULL);
974 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000975 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976}
977
978// This tests that no data channel is returned if both maxRetransmits and
979// maxRetransmitTime are set for SCTP data channels.
980TEST_F(PeerConnectionInterfaceTest,
981 CreateSctpDataChannelShouldFailForInvalidConfig) {
982 FakeConstraints constraints;
983 constraints.SetAllowDtlsSctpDataChannels();
984 CreatePeerConnection(&constraints);
985
986 std::string label = "test";
987 webrtc::DataChannelInit config;
988 config.maxRetransmits = 0;
989 config.maxRetransmitTime = 0;
990
991 scoped_refptr<DataChannelInterface> channel =
992 pc_->CreateDataChannel(label, &config);
993 EXPECT_TRUE(channel == NULL);
994}
995
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996// The test verifies that creating a SCTP data channel with an id already in use
997// or out of range should fail.
998TEST_F(PeerConnectionInterfaceTest,
999 CreateSctpDataChannelWithInvalidIdShouldFail) {
1000 FakeConstraints constraints;
1001 constraints.SetAllowDtlsSctpDataChannels();
1002 CreatePeerConnection(&constraints);
1003
1004 webrtc::DataChannelInit config;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001005 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001007 config.id = 1;
1008 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009 EXPECT_TRUE(channel != NULL);
1010 EXPECT_EQ(1, channel->id());
1011
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012 channel = pc_->CreateDataChannel("x", &config);
1013 EXPECT_TRUE(channel == NULL);
1014
1015 config.id = cricket::kMaxSctpSid;
1016 channel = pc_->CreateDataChannel("max", &config);
1017 EXPECT_TRUE(channel != NULL);
1018 EXPECT_EQ(config.id, channel->id());
1019
1020 config.id = cricket::kMaxSctpSid + 1;
1021 channel = pc_->CreateDataChannel("x", &config);
1022 EXPECT_TRUE(channel == NULL);
1023}
1024
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001025// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1026// DataChannel.
1027TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1028 FakeConstraints constraints;
1029 constraints.SetAllowRtpDataChannels();
1030 CreatePeerConnection(&constraints);
1031
1032 scoped_refptr<DataChannelInterface> dc1 =
1033 pc_->CreateDataChannel("test1", NULL);
1034 EXPECT_TRUE(observer_.renegotiation_needed_);
1035 observer_.renegotiation_needed_ = false;
1036
1037 scoped_refptr<DataChannelInterface> dc2 =
1038 pc_->CreateDataChannel("test2", NULL);
1039 EXPECT_TRUE(observer_.renegotiation_needed_);
1040}
1041
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001042// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001043TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001044 FakeConstraints constraints;
1045 constraints.SetAllowRtpDataChannels();
1046 CreatePeerConnection(&constraints);
1047
1048 scoped_refptr<DataChannelInterface> data1 =
1049 pc_->CreateDataChannel("test1", NULL);
1050 scoped_refptr<DataChannelInterface> data2 =
1051 pc_->CreateDataChannel("test2", NULL);
1052 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001053 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001054 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001055 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 new MockDataChannelObserver(data2));
1057
1058 CreateOfferReceiveAnswer();
1059 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1060 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1061
1062 ReleasePeerConnection();
1063 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1064 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1065}
1066
1067// This test that data channels can be rejected in an answer.
1068TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1069 FakeConstraints constraints;
1070 constraints.SetAllowRtpDataChannels();
1071 CreatePeerConnection(&constraints);
1072
1073 scoped_refptr<DataChannelInterface> offer_channel(
1074 pc_->CreateDataChannel("offer_channel", NULL));
1075
1076 CreateOfferAsLocalDescription();
1077
1078 // Create an answer where the m-line for data channels are rejected.
1079 std::string sdp;
1080 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1081 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1082 SessionDescriptionInterface::kAnswer);
1083 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1084 cricket::ContentInfo* data_info =
1085 answer->description()->GetContentByName("data");
1086 data_info->rejected = true;
1087
1088 DoSetRemoteDescription(answer);
1089 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1090}
1091
1092// Test that we can create a session description from an SDP string from
1093// FireFox, use it as a remote session description, generate an answer and use
1094// the answer as a local description.
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001095TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001096 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001097 FakeConstraints constraints;
1098 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1099 true);
1100 CreatePeerConnection(&constraints);
1101 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1102 SessionDescriptionInterface* desc =
1103 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001104 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001105 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1106 CreateAnswerAsLocalDescription();
1107 ASSERT_TRUE(pc_->local_description() != NULL);
1108 ASSERT_TRUE(pc_->remote_description() != NULL);
1109
1110 const cricket::ContentInfo* content =
1111 cricket::GetFirstAudioContent(pc_->local_description()->description());
1112 ASSERT_TRUE(content != NULL);
1113 EXPECT_FALSE(content->rejected);
1114
1115 content =
1116 cricket::GetFirstVideoContent(pc_->local_description()->description());
1117 ASSERT_TRUE(content != NULL);
1118 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001119#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001120 content =
1121 cricket::GetFirstDataContent(pc_->local_description()->description());
1122 ASSERT_TRUE(content != NULL);
1123 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001124#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001125}
1126
1127// Test that we can create an audio only offer and receive an answer with a
1128// limited set of audio codecs and receive an updated offer with more audio
1129// codecs, where the added codecs are not supported.
1130TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1131 CreatePeerConnection();
1132 AddVoiceStream("audio_label");
1133 CreateOfferAsLocalDescription();
1134
1135 SessionDescriptionInterface* answer =
1136 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001137 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1139
1140 SessionDescriptionInterface* updated_offer =
1141 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001142 webrtc::kAudioSdpWithUnsupportedCodecs,
1143 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001144 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1145 CreateAnswerAsLocalDescription();
1146}
1147
1148// Test that PeerConnection::Close changes the states to closed and all remote
1149// tracks change state to ended.
1150TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
1151 // Initialize a PeerConnection and negotiate local and remote session
1152 // description.
1153 InitiateCall();
1154 ASSERT_EQ(1u, pc_->local_streams()->count());
1155 ASSERT_EQ(1u, pc_->remote_streams()->count());
1156
1157 pc_->Close();
1158
1159 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
1160 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
1161 pc_->ice_connection_state());
1162 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
1163 pc_->ice_gathering_state());
1164
1165 EXPECT_EQ(1u, pc_->local_streams()->count());
1166 EXPECT_EQ(1u, pc_->remote_streams()->count());
1167
1168 scoped_refptr<MediaStreamInterface> remote_stream =
1169 pc_->remote_streams()->at(0);
1170 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1171 remote_stream->GetVideoTracks()[0]->state());
1172 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1173 remote_stream->GetAudioTracks()[0]->state());
1174}
1175
1176// Test that PeerConnection methods fails gracefully after
1177// PeerConnection::Close has been called.
1178TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
1179 CreatePeerConnection();
1180 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1181 CreateOfferAsRemoteDescription();
1182 CreateAnswerAsLocalDescription();
1183
1184 ASSERT_EQ(1u, pc_->local_streams()->count());
1185 scoped_refptr<MediaStreamInterface> local_stream =
1186 pc_->local_streams()->at(0);
1187
1188 pc_->Close();
1189
1190 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001191 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192
1193 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001194 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00001196 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001197
1198 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
1199
1200 EXPECT_TRUE(pc_->local_description() != NULL);
1201 EXPECT_TRUE(pc_->remote_description() != NULL);
1202
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001203 rtc::scoped_ptr<SessionDescriptionInterface> offer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204 EXPECT_TRUE(DoCreateOffer(offer.use()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001205 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001206 EXPECT_TRUE(DoCreateAnswer(answer.use()));
1207
1208 std::string sdp;
1209 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
1210 SessionDescriptionInterface* remote_offer =
1211 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1212 sdp, NULL);
1213 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
1214
1215 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
1216 SessionDescriptionInterface* local_offer =
1217 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1218 sdp, NULL);
1219 EXPECT_FALSE(DoSetLocalDescription(local_offer));
1220}
1221
1222// Test that GetStats can still be called after PeerConnection::Close.
1223TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
1224 InitiateCall();
1225 pc_->Close();
1226 DoGetStats(NULL);
1227}