blob: 5e135df647376bddb4632f8271a0b5f540e4444a [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;
deadbeef0a6c4ca2015-10-06 11:38:28 -0700250 if (!uri.empty()) {
251 server.uri = uri;
252 server.password = password;
253 servers.push_back(server);
254 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255
256 port_allocator_factory_ = FakePortAllocatorFactory::Create();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000257
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000258 // DTLS does not work in a loopback call, so is disabled for most of the
259 // tests in this file. We only create a FakeIdentityService if the test
260 // explicitly sets the constraint.
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000261 FakeConstraints default_constraints;
262 if (!constraints) {
263 constraints = &default_constraints;
264
265 default_constraints.AddMandatory(
266 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, false);
267 }
268
Henrik Boström5e56c592015-08-11 10:33:13 +0200269 scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store;
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000270 bool dtls;
271 if (FindConstraint(constraints,
272 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
273 &dtls,
Henrik Boström5e56c592015-08-11 10:33:13 +0200274 nullptr) && dtls) {
275 dtls_identity_store.reset(new FakeDtlsIdentityStore());
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000276 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 pc_ = pc_factory_->CreatePeerConnection(servers, constraints,
278 port_allocator_factory_.get(),
Henrik Boström5e56c592015-08-11 10:33:13 +0200279 dtls_identity_store.Pass(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 &observer_);
281 ASSERT_TRUE(pc_.get() != NULL);
282 observer_.SetPeerConnectionInterface(pc_.get());
283 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
284 }
285
deadbeef0a6c4ca2015-10-06 11:38:28 -0700286 void CreatePeerConnectionExpectFail(const std::string& uri) {
287 PeerConnectionInterface::IceServer server;
288 PeerConnectionInterface::IceServers servers;
289 server.uri = uri;
290 servers.push_back(server);
291
292 scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store;
293 port_allocator_factory_ = FakePortAllocatorFactory::Create();
294 scoped_refptr<PeerConnectionInterface> pc;
295 pc = pc_factory_->CreatePeerConnection(
296 servers, nullptr, port_allocator_factory_.get(),
297 dtls_identity_store.Pass(), &observer_);
298 ASSERT_EQ(nullptr, pc);
299 }
300
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000301 void CreatePeerConnectionWithDifferentConfigurations() {
302 CreatePeerConnection(kStunAddressOnly, "", NULL);
303 EXPECT_EQ(1u, port_allocator_factory_->stun_configs().size());
304 EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
305 EXPECT_EQ("address",
306 port_allocator_factory_->stun_configs()[0].server.hostname());
307 EXPECT_EQ(kDefaultStunPort,
308 port_allocator_factory_->stun_configs()[0].server.port());
309
deadbeef0a6c4ca2015-10-06 11:38:28 -0700310 CreatePeerConnectionExpectFail(kStunInvalidPort);
311 CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
312 CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313
314 CreatePeerConnection(kTurnIceServerUri, kTurnPassword, NULL);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000315 EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316 EXPECT_EQ(1u, port_allocator_factory_->turn_configs().size());
317 EXPECT_EQ(kTurnUsername,
318 port_allocator_factory_->turn_configs()[0].username);
319 EXPECT_EQ(kTurnPassword,
320 port_allocator_factory_->turn_configs()[0].password);
321 EXPECT_EQ(kTurnHostname,
322 port_allocator_factory_->turn_configs()[0].server.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323 }
324
325 void ReleasePeerConnection() {
326 pc_ = NULL;
327 observer_.SetPeerConnectionInterface(NULL);
328 }
329
330 void AddStream(const std::string& label) {
331 // Create a local stream.
332 scoped_refptr<MediaStreamInterface> stream(
333 pc_factory_->CreateLocalMediaStream(label));
334 scoped_refptr<VideoSourceInterface> video_source(
335 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
336 scoped_refptr<VideoTrackInterface> video_track(
337 pc_factory_->CreateVideoTrack(label + "v0", video_source));
338 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000339 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
341 observer_.renegotiation_needed_ = false;
342 }
343
344 void AddVoiceStream(const std::string& label) {
345 // Create a local stream.
346 scoped_refptr<MediaStreamInterface> stream(
347 pc_factory_->CreateLocalMediaStream(label));
348 scoped_refptr<AudioTrackInterface> audio_track(
349 pc_factory_->CreateAudioTrack(label + "a0", NULL));
350 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000351 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
353 observer_.renegotiation_needed_ = false;
354 }
355
356 void AddAudioVideoStream(const std::string& stream_label,
357 const std::string& audio_track_label,
358 const std::string& video_track_label) {
359 // Create a local stream.
360 scoped_refptr<MediaStreamInterface> stream(
361 pc_factory_->CreateLocalMediaStream(stream_label));
362 scoped_refptr<AudioTrackInterface> audio_track(
363 pc_factory_->CreateAudioTrack(
364 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
365 stream->AddTrack(audio_track.get());
366 scoped_refptr<VideoTrackInterface> video_track(
367 pc_factory_->CreateVideoTrack(video_track_label, NULL));
368 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000369 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
371 observer_.renegotiation_needed_ = false;
372 }
373
374 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc, bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000375 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
376 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377 MockCreateSessionDescriptionObserver>());
378 if (offer) {
379 pc_->CreateOffer(observer, NULL);
380 } else {
381 pc_->CreateAnswer(observer, NULL);
382 }
383 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
384 *desc = observer->release_desc();
385 return observer->result();
386 }
387
388 bool DoCreateOffer(SessionDescriptionInterface** desc) {
389 return DoCreateOfferAnswer(desc, true);
390 }
391
392 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
393 return DoCreateOfferAnswer(desc, false);
394 }
395
396 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000397 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
398 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399 MockSetSessionDescriptionObserver>());
400 if (local) {
401 pc_->SetLocalDescription(observer, desc);
402 } else {
403 pc_->SetRemoteDescription(observer, desc);
404 }
405 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
406 return observer->result();
407 }
408
409 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
410 return DoSetSessionDescription(desc, true);
411 }
412
413 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
414 return DoSetSessionDescription(desc, false);
415 }
416
417 // Calls PeerConnection::GetStats and check the return value.
418 // It does not verify the values in the StatReports since a RTCP packet might
419 // be required.
420 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000421 rtc::scoped_refptr<MockStatsObserver> observer(
422 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000423 if (!pc_->GetStats(
424 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425 return false;
426 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
427 return observer->called();
428 }
429
430 void InitiateCall() {
431 CreatePeerConnection();
432 // Create a local stream with audio&video tracks.
433 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
434 CreateOfferReceiveAnswer();
435 }
436
437 // Verify that RTP Header extensions has been negotiated for audio and video.
438 void VerifyRemoteRtpHeaderExtensions() {
439 const cricket::MediaContentDescription* desc =
440 cricket::GetFirstAudioContentDescription(
441 pc_->remote_description()->description());
442 ASSERT_TRUE(desc != NULL);
443 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
444
445 desc = cricket::GetFirstVideoContentDescription(
446 pc_->remote_description()->description());
447 ASSERT_TRUE(desc != NULL);
448 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
449 }
450
451 void CreateOfferAsRemoteDescription() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000452 rtc::scoped_ptr<SessionDescriptionInterface> offer;
pkasting@chromium.org005b6ff2015-01-30 19:41:42 +0000453 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454 std::string sdp;
455 EXPECT_TRUE(offer->ToString(&sdp));
456 SessionDescriptionInterface* remote_offer =
457 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
458 sdp, NULL);
459 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
460 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
461 }
462
463 void CreateAnswerAsLocalDescription() {
464 scoped_ptr<SessionDescriptionInterface> answer;
pkasting@chromium.org005b6ff2015-01-30 19:41:42 +0000465 ASSERT_TRUE(DoCreateAnswer(answer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466
467 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
468 // audio codec change, even if the parameter has nothing to do with
469 // receiving. Not all parameters are serialized to SDP.
470 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
471 // the SessionDescription, it is necessary to do that here to in order to
472 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
473 // https://code.google.com/p/webrtc/issues/detail?id=1356
474 std::string sdp;
475 EXPECT_TRUE(answer->ToString(&sdp));
476 SessionDescriptionInterface* new_answer =
477 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
478 sdp, NULL);
479 EXPECT_TRUE(DoSetLocalDescription(new_answer));
480 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
481 }
482
483 void CreatePrAnswerAsLocalDescription() {
484 scoped_ptr<SessionDescriptionInterface> answer;
pkasting@chromium.org005b6ff2015-01-30 19:41:42 +0000485 ASSERT_TRUE(DoCreateAnswer(answer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486
487 std::string sdp;
488 EXPECT_TRUE(answer->ToString(&sdp));
489 SessionDescriptionInterface* pr_answer =
490 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
491 sdp, NULL);
492 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
493 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
494 }
495
496 void CreateOfferReceiveAnswer() {
497 CreateOfferAsLocalDescription();
498 std::string sdp;
499 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
500 CreateAnswerAsRemoteDescription(sdp);
501 }
502
503 void CreateOfferAsLocalDescription() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000504 rtc::scoped_ptr<SessionDescriptionInterface> offer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 ASSERT_TRUE(DoCreateOffer(offer.use()));
506 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
507 // audio codec change, even if the parameter has nothing to do with
508 // receiving. Not all parameters are serialized to SDP.
509 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
510 // the SessionDescription, it is necessary to do that here to in order to
511 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
512 // https://code.google.com/p/webrtc/issues/detail?id=1356
513 std::string sdp;
514 EXPECT_TRUE(offer->ToString(&sdp));
515 SessionDescriptionInterface* new_offer =
516 webrtc::CreateSessionDescription(
517 SessionDescriptionInterface::kOffer,
518 sdp, NULL);
519
520 EXPECT_TRUE(DoSetLocalDescription(new_offer));
521 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000522 // Wait for the ice_complete message, so that SDP will have candidates.
523 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 }
525
526 void CreateAnswerAsRemoteDescription(const std::string& offer) {
527 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
528 SessionDescriptionInterface::kAnswer);
529 EXPECT_TRUE(answer->Initialize(offer, NULL));
530 EXPECT_TRUE(DoSetRemoteDescription(answer));
531 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
532 }
533
534 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& offer) {
535 webrtc::JsepSessionDescription* pr_answer =
536 new webrtc::JsepSessionDescription(
537 SessionDescriptionInterface::kPrAnswer);
538 EXPECT_TRUE(pr_answer->Initialize(offer, NULL));
539 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
540 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
541 webrtc::JsepSessionDescription* answer =
542 new webrtc::JsepSessionDescription(
543 SessionDescriptionInterface::kAnswer);
544 EXPECT_TRUE(answer->Initialize(offer, NULL));
545 EXPECT_TRUE(DoSetRemoteDescription(answer));
546 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
547 }
548
549 // Help function used for waiting until a the last signaled remote stream has
550 // the same label as |stream_label|. In a few of the tests in this file we
551 // answer with the same session description as we offer and thus we can
552 // check if OnAddStream have been called with the same stream as we offer to
553 // send.
554 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
555 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
556 }
557
558 // Creates an offer and applies it as a local session description.
559 // Creates an answer with the same SDP an the offer but removes all lines
560 // that start with a:ssrc"
561 void CreateOfferReceiveAnswerWithoutSsrc() {
562 CreateOfferAsLocalDescription();
563 std::string sdp;
564 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
565 SetSsrcToZero(&sdp);
566 CreateAnswerAsRemoteDescription(sdp);
567 }
568
569 scoped_refptr<FakePortAllocatorFactory> port_allocator_factory_;
570 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
571 scoped_refptr<PeerConnectionInterface> pc_;
572 MockPeerConnectionObserver observer_;
573};
574
575TEST_F(PeerConnectionInterfaceTest,
576 CreatePeerConnectionWithDifferentConfigurations) {
577 CreatePeerConnectionWithDifferentConfigurations();
578}
579
580TEST_F(PeerConnectionInterfaceTest, AddStreams) {
581 CreatePeerConnection();
582 AddStream(kStreamLabel1);
583 AddVoiceStream(kStreamLabel2);
584 ASSERT_EQ(2u, pc_->local_streams()->count());
585
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000586 // Test we can add multiple local streams to one peerconnection.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 scoped_refptr<MediaStreamInterface> stream(
588 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
589 scoped_refptr<AudioTrackInterface> audio_track(
590 pc_factory_->CreateAudioTrack(
591 kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
592 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000593 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000594 EXPECT_EQ(3u, pc_->local_streams()->count());
595
596 // Remove the third stream.
597 pc_->RemoveStream(pc_->local_streams()->at(2));
598 EXPECT_EQ(2u, pc_->local_streams()->count());
599
600 // Remove the second stream.
601 pc_->RemoveStream(pc_->local_streams()->at(1));
602 EXPECT_EQ(1u, pc_->local_streams()->count());
603
604 // Remove the first stream.
605 pc_->RemoveStream(pc_->local_streams()->at(0));
606 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607}
608
609TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
610 CreatePeerConnection();
611 AddStream(kStreamLabel1);
612 ASSERT_EQ(1u, pc_->local_streams()->count());
613 pc_->RemoveStream(pc_->local_streams()->at(0));
614 EXPECT_EQ(0u, pc_->local_streams()->count());
615}
616
617TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
618 InitiateCall();
619 WaitAndVerifyOnAddStream(kStreamLabel1);
620 VerifyRemoteRtpHeaderExtensions();
621}
622
623TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
624 CreatePeerConnection();
625 AddStream(kStreamLabel1);
626 CreateOfferAsLocalDescription();
627 std::string offer;
628 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
629 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
630 WaitAndVerifyOnAddStream(kStreamLabel1);
631}
632
633TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
634 CreatePeerConnection();
635 AddStream(kStreamLabel1);
636
637 CreateOfferAsRemoteDescription();
638 CreateAnswerAsLocalDescription();
639
640 WaitAndVerifyOnAddStream(kStreamLabel1);
641}
642
643TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
644 CreatePeerConnection();
645 AddStream(kStreamLabel1);
646
647 CreateOfferAsRemoteDescription();
648 CreatePrAnswerAsLocalDescription();
649 CreateAnswerAsLocalDescription();
650
651 WaitAndVerifyOnAddStream(kStreamLabel1);
652}
653
654TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
655 InitiateCall();
656 ASSERT_EQ(1u, pc_->remote_streams()->count());
657 pc_->RemoveStream(pc_->local_streams()->at(0));
658 CreateOfferReceiveAnswer();
659 EXPECT_EQ(0u, pc_->remote_streams()->count());
660 AddStream(kStreamLabel1);
661 CreateOfferReceiveAnswer();
662}
663
664// Tests that after negotiating an audio only call, the respondent can perform a
665// renegotiation that removes the audio stream.
666TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
667 CreatePeerConnection();
668 AddVoiceStream(kStreamLabel1);
669 CreateOfferAsRemoteDescription();
670 CreateAnswerAsLocalDescription();
671
672 ASSERT_EQ(1u, pc_->remote_streams()->count());
673 pc_->RemoveStream(pc_->local_streams()->at(0));
674 CreateOfferReceiveAnswer();
675 EXPECT_EQ(0u, pc_->remote_streams()->count());
676}
677
678// Test that candidates are generated and that we can parse our own candidates.
679TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
680 CreatePeerConnection();
681
682 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
683 // SetRemoteDescription takes ownership of offer.
684 SessionDescriptionInterface* offer = NULL;
685 AddStream(kStreamLabel1);
686 EXPECT_TRUE(DoCreateOffer(&offer));
687 EXPECT_TRUE(DoSetRemoteDescription(offer));
688
689 // SetLocalDescription takes ownership of answer.
690 SessionDescriptionInterface* answer = NULL;
691 EXPECT_TRUE(DoCreateAnswer(&answer));
692 EXPECT_TRUE(DoSetLocalDescription(answer));
693
694 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
695 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
696
697 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
698}
699
700// Test that the CreateOffer and CreatAnswer will fail if the track labels are
701// not unique.
702TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
703 CreatePeerConnection();
704 // Create a regular offer for the CreateAnswer test later.
705 SessionDescriptionInterface* offer = NULL;
706 EXPECT_TRUE(DoCreateOffer(&offer));
707 EXPECT_TRUE(offer != NULL);
708 delete offer;
709 offer = NULL;
710
711 // Create a local stream with audio&video tracks having same label.
712 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
713
714 // Test CreateOffer
715 EXPECT_FALSE(DoCreateOffer(&offer));
716
717 // Test CreateAnswer
718 SessionDescriptionInterface* answer = NULL;
719 EXPECT_FALSE(DoCreateAnswer(&answer));
720}
721
722// Test that we will get different SSRCs for each tracks in the offer and answer
723// we created.
724TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
725 CreatePeerConnection();
726 // Create a local stream with audio&video tracks having different labels.
727 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
728
729 // Test CreateOffer
730 scoped_ptr<SessionDescriptionInterface> offer;
pkasting@chromium.org005b6ff2015-01-30 19:41:42 +0000731 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732 int audio_ssrc = 0;
733 int video_ssrc = 0;
734 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
735 &audio_ssrc));
736 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
737 &video_ssrc));
738 EXPECT_NE(audio_ssrc, video_ssrc);
739
740 // Test CreateAnswer
741 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
742 scoped_ptr<SessionDescriptionInterface> answer;
pkasting@chromium.org005b6ff2015-01-30 19:41:42 +0000743 ASSERT_TRUE(DoCreateAnswer(answer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 audio_ssrc = 0;
745 video_ssrc = 0;
746 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
747 &audio_ssrc));
748 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
749 &video_ssrc));
750 EXPECT_NE(audio_ssrc, video_ssrc);
751}
752
753// Test that we can specify a certain track that we want statistics about.
754TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
755 InitiateCall();
756 ASSERT_LT(0u, pc_->remote_streams()->count());
757 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
758 scoped_refptr<MediaStreamTrackInterface> remote_audio =
759 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
760 EXPECT_TRUE(DoGetStats(remote_audio));
761
762 // Remove the stream. Since we are sending to our selves the local
763 // and the remote stream is the same.
764 pc_->RemoveStream(pc_->local_streams()->at(0));
765 // Do a re-negotiation.
766 CreateOfferReceiveAnswer();
767
768 ASSERT_EQ(0u, pc_->remote_streams()->count());
769
770 // Test that we still can get statistics for the old track. Even if it is not
771 // sent any longer.
772 EXPECT_TRUE(DoGetStats(remote_audio));
773}
774
775// Test that we can get stats on a video track.
776TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
777 InitiateCall();
778 ASSERT_LT(0u, pc_->remote_streams()->count());
779 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
780 scoped_refptr<MediaStreamTrackInterface> remote_video =
781 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
782 EXPECT_TRUE(DoGetStats(remote_video));
783}
784
785// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +0000786// TODO(tommi): Fix this test. DoGetStats will return true
787// for the unknown track (since GetStats is async), but no
788// data is returned for the track.
789TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 InitiateCall();
791 scoped_refptr<AudioTrackInterface> unknown_audio_track(
792 pc_factory_->CreateAudioTrack("unknown track", NULL));
793 EXPECT_FALSE(DoGetStats(unknown_audio_track));
794}
795
796// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 FakeConstraints constraints;
799 constraints.SetAllowRtpDataChannels();
800 CreatePeerConnection(&constraints);
801 scoped_refptr<DataChannelInterface> data1 =
802 pc_->CreateDataChannel("test1", NULL);
803 scoped_refptr<DataChannelInterface> data2 =
804 pc_->CreateDataChannel("test2", NULL);
805 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000806 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000808 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 new MockDataChannelObserver(data2));
810
811 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
812 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
813 std::string data_to_send1 = "testing testing";
814 std::string data_to_send2 = "testing something else";
815 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
816
817 CreateOfferReceiveAnswer();
818 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
819 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
820
821 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
822 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
823 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
824 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
825
826 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
827 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
828
829 data1->Close();
830 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
831 CreateOfferReceiveAnswer();
832 EXPECT_FALSE(observer1->IsOpen());
833 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
834 EXPECT_TRUE(observer2->IsOpen());
835
836 data_to_send2 = "testing something else again";
837 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
838
839 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
840}
841
842// This test verifies that sendnig binary data over RTP data channels should
843// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000845 FakeConstraints constraints;
846 constraints.SetAllowRtpDataChannels();
847 CreatePeerConnection(&constraints);
848 scoped_refptr<DataChannelInterface> data1 =
849 pc_->CreateDataChannel("test1", NULL);
850 scoped_refptr<DataChannelInterface> data2 =
851 pc_->CreateDataChannel("test2", NULL);
852 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000853 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000855 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856 new MockDataChannelObserver(data2));
857
858 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
859 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
860
861 CreateOfferReceiveAnswer();
862 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
863 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
864
865 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
866 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
867
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000868 rtc::Buffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
870}
871
872// This test setup a RTP data channels in loop back and test that a channel is
873// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 FakeConstraints constraints;
876 constraints.SetAllowRtpDataChannels();
877 CreatePeerConnection(&constraints);
878 scoped_refptr<DataChannelInterface> data1 =
879 pc_->CreateDataChannel("test1", NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000880 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881 new MockDataChannelObserver(data1));
882
883 CreateOfferReceiveAnswerWithoutSsrc();
884
885 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
886
887 data1->Close();
888 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
889 CreateOfferReceiveAnswerWithoutSsrc();
890 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
891 EXPECT_FALSE(observer1->IsOpen());
892}
893
894// This test that if a data channel is added in an answer a receive only channel
895// channel is created.
896TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
897 FakeConstraints constraints;
898 constraints.SetAllowRtpDataChannels();
899 CreatePeerConnection(&constraints);
900
901 std::string offer_label = "offer_channel";
902 scoped_refptr<DataChannelInterface> offer_channel =
903 pc_->CreateDataChannel(offer_label, NULL);
904
905 CreateOfferAsLocalDescription();
906
907 // Replace the data channel label in the offer and apply it as an answer.
908 std::string receive_label = "answer_channel";
909 std::string sdp;
910 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000911 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 receive_label.c_str(), receive_label.length(),
913 &sdp);
914 CreateAnswerAsRemoteDescription(sdp);
915
916 // Verify that a new incoming data channel has been created and that
917 // it is open but can't we written to.
918 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
919 DataChannelInterface* received_channel = observer_.last_datachannel_;
920 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
921 EXPECT_EQ(receive_label, received_channel->label());
922 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
923
924 // Verify that the channel we initially offered has been rejected.
925 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
926
927 // Do another offer / answer exchange and verify that the data channel is
928 // opened.
929 CreateOfferReceiveAnswer();
930 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
931 kTimeout);
932}
933
934// This test that no data channel is returned if a reliable channel is
935// requested.
936// TODO(perkj): Remove this test once reliable channels are implemented.
937TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
938 FakeConstraints constraints;
939 constraints.SetAllowRtpDataChannels();
940 CreatePeerConnection(&constraints);
941
942 std::string label = "test";
943 webrtc::DataChannelInit config;
944 config.reliable = true;
945 scoped_refptr<DataChannelInterface> channel =
946 pc_->CreateDataChannel(label, &config);
947 EXPECT_TRUE(channel == NULL);
948}
949
950// This tests that a SCTP data channel is returned using different
951// DataChannelInit configurations.
952TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
953 FakeConstraints constraints;
954 constraints.SetAllowDtlsSctpDataChannels();
955 CreatePeerConnection(&constraints);
956
957 webrtc::DataChannelInit config;
958
959 scoped_refptr<DataChannelInterface> channel =
960 pc_->CreateDataChannel("1", &config);
961 EXPECT_TRUE(channel != NULL);
962 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000963 EXPECT_TRUE(observer_.renegotiation_needed_);
964 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965
966 config.ordered = false;
967 channel = pc_->CreateDataChannel("2", &config);
968 EXPECT_TRUE(channel != NULL);
969 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000970 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971
972 config.ordered = true;
973 config.maxRetransmits = 0;
974 channel = pc_->CreateDataChannel("3", &config);
975 EXPECT_TRUE(channel != NULL);
976 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000977 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000978
979 config.maxRetransmits = -1;
980 config.maxRetransmitTime = 0;
981 channel = pc_->CreateDataChannel("4", &config);
982 EXPECT_TRUE(channel != NULL);
983 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000984 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985}
986
987// This tests that no data channel is returned if both maxRetransmits and
988// maxRetransmitTime are set for SCTP data channels.
989TEST_F(PeerConnectionInterfaceTest,
990 CreateSctpDataChannelShouldFailForInvalidConfig) {
991 FakeConstraints constraints;
992 constraints.SetAllowDtlsSctpDataChannels();
993 CreatePeerConnection(&constraints);
994
995 std::string label = "test";
996 webrtc::DataChannelInit config;
997 config.maxRetransmits = 0;
998 config.maxRetransmitTime = 0;
999
1000 scoped_refptr<DataChannelInterface> channel =
1001 pc_->CreateDataChannel(label, &config);
1002 EXPECT_TRUE(channel == NULL);
1003}
1004
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001005// The test verifies that creating a SCTP data channel with an id already in use
1006// or out of range should fail.
1007TEST_F(PeerConnectionInterfaceTest,
1008 CreateSctpDataChannelWithInvalidIdShouldFail) {
1009 FakeConstraints constraints;
1010 constraints.SetAllowDtlsSctpDataChannels();
1011 CreatePeerConnection(&constraints);
1012
1013 webrtc::DataChannelInit config;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001014 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001016 config.id = 1;
1017 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018 EXPECT_TRUE(channel != NULL);
1019 EXPECT_EQ(1, channel->id());
1020
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021 channel = pc_->CreateDataChannel("x", &config);
1022 EXPECT_TRUE(channel == NULL);
1023
1024 config.id = cricket::kMaxSctpSid;
1025 channel = pc_->CreateDataChannel("max", &config);
1026 EXPECT_TRUE(channel != NULL);
1027 EXPECT_EQ(config.id, channel->id());
1028
1029 config.id = cricket::kMaxSctpSid + 1;
1030 channel = pc_->CreateDataChannel("x", &config);
1031 EXPECT_TRUE(channel == NULL);
1032}
1033
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001034// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1035// DataChannel.
1036TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1037 FakeConstraints constraints;
1038 constraints.SetAllowRtpDataChannels();
1039 CreatePeerConnection(&constraints);
1040
1041 scoped_refptr<DataChannelInterface> dc1 =
1042 pc_->CreateDataChannel("test1", NULL);
1043 EXPECT_TRUE(observer_.renegotiation_needed_);
1044 observer_.renegotiation_needed_ = false;
1045
1046 scoped_refptr<DataChannelInterface> dc2 =
1047 pc_->CreateDataChannel("test2", NULL);
1048 EXPECT_TRUE(observer_.renegotiation_needed_);
1049}
1050
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053 FakeConstraints constraints;
1054 constraints.SetAllowRtpDataChannels();
1055 CreatePeerConnection(&constraints);
1056
1057 scoped_refptr<DataChannelInterface> data1 =
1058 pc_->CreateDataChannel("test1", NULL);
1059 scoped_refptr<DataChannelInterface> data2 =
1060 pc_->CreateDataChannel("test2", NULL);
1061 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001062 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001063 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001064 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001065 new MockDataChannelObserver(data2));
1066
1067 CreateOfferReceiveAnswer();
1068 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1069 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1070
1071 ReleasePeerConnection();
1072 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1073 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1074}
1075
1076// This test that data channels can be rejected in an answer.
1077TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1078 FakeConstraints constraints;
1079 constraints.SetAllowRtpDataChannels();
1080 CreatePeerConnection(&constraints);
1081
1082 scoped_refptr<DataChannelInterface> offer_channel(
1083 pc_->CreateDataChannel("offer_channel", NULL));
1084
1085 CreateOfferAsLocalDescription();
1086
1087 // Create an answer where the m-line for data channels are rejected.
1088 std::string sdp;
1089 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1090 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1091 SessionDescriptionInterface::kAnswer);
1092 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1093 cricket::ContentInfo* data_info =
1094 answer->description()->GetContentByName("data");
1095 data_info->rejected = true;
1096
1097 DoSetRemoteDescription(answer);
1098 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1099}
1100
1101// Test that we can create a session description from an SDP string from
1102// FireFox, use it as a remote session description, generate an answer and use
1103// the answer as a local description.
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001104TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001105 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001106 FakeConstraints constraints;
1107 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1108 true);
1109 CreatePeerConnection(&constraints);
1110 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1111 SessionDescriptionInterface* desc =
1112 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001113 webrtc::kFireFoxSdpOffer, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001114 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1115 CreateAnswerAsLocalDescription();
1116 ASSERT_TRUE(pc_->local_description() != NULL);
1117 ASSERT_TRUE(pc_->remote_description() != NULL);
1118
1119 const cricket::ContentInfo* content =
1120 cricket::GetFirstAudioContent(pc_->local_description()->description());
1121 ASSERT_TRUE(content != NULL);
1122 EXPECT_FALSE(content->rejected);
1123
1124 content =
1125 cricket::GetFirstVideoContent(pc_->local_description()->description());
1126 ASSERT_TRUE(content != NULL);
1127 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001128#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129 content =
1130 cricket::GetFirstDataContent(pc_->local_description()->description());
1131 ASSERT_TRUE(content != NULL);
1132 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001133#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001134}
1135
1136// Test that we can create an audio only offer and receive an answer with a
1137// limited set of audio codecs and receive an updated offer with more audio
1138// codecs, where the added codecs are not supported.
1139TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1140 CreatePeerConnection();
1141 AddVoiceStream("audio_label");
1142 CreateOfferAsLocalDescription();
1143
1144 SessionDescriptionInterface* answer =
1145 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
jbauchfabe2c92015-07-16 13:43:14 -07001146 webrtc::kAudioSdp, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001147 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1148
1149 SessionDescriptionInterface* updated_offer =
1150 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
jbauchfabe2c92015-07-16 13:43:14 -07001151 webrtc::kAudioSdpWithUnsupportedCodecs,
1152 nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001153 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1154 CreateAnswerAsLocalDescription();
1155}
1156
1157// Test that PeerConnection::Close changes the states to closed and all remote
1158// tracks change state to ended.
1159TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
1160 // Initialize a PeerConnection and negotiate local and remote session
1161 // description.
1162 InitiateCall();
1163 ASSERT_EQ(1u, pc_->local_streams()->count());
1164 ASSERT_EQ(1u, pc_->remote_streams()->count());
1165
1166 pc_->Close();
1167
1168 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
1169 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
1170 pc_->ice_connection_state());
1171 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
1172 pc_->ice_gathering_state());
1173
1174 EXPECT_EQ(1u, pc_->local_streams()->count());
1175 EXPECT_EQ(1u, pc_->remote_streams()->count());
1176
1177 scoped_refptr<MediaStreamInterface> remote_stream =
1178 pc_->remote_streams()->at(0);
1179 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1180 remote_stream->GetVideoTracks()[0]->state());
1181 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1182 remote_stream->GetAudioTracks()[0]->state());
1183}
1184
1185// Test that PeerConnection methods fails gracefully after
1186// PeerConnection::Close has been called.
1187TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
1188 CreatePeerConnection();
1189 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1190 CreateOfferAsRemoteDescription();
1191 CreateAnswerAsLocalDescription();
1192
1193 ASSERT_EQ(1u, pc_->local_streams()->count());
1194 scoped_refptr<MediaStreamInterface> local_stream =
1195 pc_->local_streams()->at(0);
1196
1197 pc_->Close();
1198
1199 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001200 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001201
1202 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001203 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00001205 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001206
1207 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
1208
1209 EXPECT_TRUE(pc_->local_description() != NULL);
1210 EXPECT_TRUE(pc_->remote_description() != NULL);
1211
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001212 rtc::scoped_ptr<SessionDescriptionInterface> offer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001213 EXPECT_TRUE(DoCreateOffer(offer.use()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001214 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215 EXPECT_TRUE(DoCreateAnswer(answer.use()));
1216
1217 std::string sdp;
1218 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
1219 SessionDescriptionInterface* remote_offer =
1220 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1221 sdp, NULL);
1222 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
1223
1224 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
1225 SessionDescriptionInterface* local_offer =
1226 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1227 sdp, NULL);
1228 EXPECT_FALSE(DoSetLocalDescription(local_offer));
1229}
1230
1231// Test that GetStats can still be called after PeerConnection::Close.
1232TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
1233 InitiateCall();
1234 pc_->Close();
1235 DoGetStats(NULL);
1236}