blob: 3be628055ea478938d487be82f51aeb566a2c58c [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2012, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <string>
29
30#include "talk/app/webrtc/fakeportallocatorfactory.h"
31#include "talk/app/webrtc/jsepsessiondescription.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032#include "talk/app/webrtc/mediastreaminterface.h"
33#include "talk/app/webrtc/peerconnectioninterface.h"
34#include "talk/app/webrtc/test/fakeconstraints.h"
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +000035#include "talk/app/webrtc/test/fakedtlsidentityservice.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036#include "talk/app/webrtc/test/mockpeerconnectionobservers.h"
37#include "talk/app/webrtc/test/testsdpstrings.h"
wu@webrtc.org967bfff2013-09-19 05:49:50 +000038#include "talk/app/webrtc/videosource.h"
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";
61static const uint32 kTimeout = 5000U;
62
63#define MAYBE_SKIP_TEST(feature) \
64 if (!(feature())) { \
65 LOG(LS_INFO) << "Feature disabled... skipping"; \
66 return; \
67 }
68
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.orga576faf2014-01-29 17:45:53 +0000259 FakeIdentityService* dtls_service = NULL;
260 bool dtls;
261 if (FindConstraint(constraints,
262 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
263 &dtls,
264 NULL) && dtls) {
265 dtls_service = new FakeIdentityService();
266 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 pc_ = pc_factory_->CreatePeerConnection(servers, constraints,
268 port_allocator_factory_.get(),
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000269 dtls_service,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 &observer_);
271 ASSERT_TRUE(pc_.get() != NULL);
272 observer_.SetPeerConnectionInterface(pc_.get());
273 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
274 }
275
276 void CreatePeerConnectionWithDifferentConfigurations() {
277 CreatePeerConnection(kStunAddressOnly, "", NULL);
278 EXPECT_EQ(1u, port_allocator_factory_->stun_configs().size());
279 EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
280 EXPECT_EQ("address",
281 port_allocator_factory_->stun_configs()[0].server.hostname());
282 EXPECT_EQ(kDefaultStunPort,
283 port_allocator_factory_->stun_configs()[0].server.port());
284
285 CreatePeerConnection(kStunInvalidPort, "", NULL);
286 EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size());
287 EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
288
289 CreatePeerConnection(kStunAddressPortAndMore1, "", NULL);
290 EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size());
291 EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
292
293 CreatePeerConnection(kStunAddressPortAndMore2, "", NULL);
294 EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size());
295 EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
296
297 CreatePeerConnection(kTurnIceServerUri, kTurnPassword, NULL);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000298 EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 EXPECT_EQ(1u, port_allocator_factory_->turn_configs().size());
300 EXPECT_EQ(kTurnUsername,
301 port_allocator_factory_->turn_configs()[0].username);
302 EXPECT_EQ(kTurnPassword,
303 port_allocator_factory_->turn_configs()[0].password);
304 EXPECT_EQ(kTurnHostname,
305 port_allocator_factory_->turn_configs()[0].server.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306 }
307
308 void ReleasePeerConnection() {
309 pc_ = NULL;
310 observer_.SetPeerConnectionInterface(NULL);
311 }
312
313 void AddStream(const std::string& label) {
314 // Create a local stream.
315 scoped_refptr<MediaStreamInterface> stream(
316 pc_factory_->CreateLocalMediaStream(label));
317 scoped_refptr<VideoSourceInterface> video_source(
318 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
319 scoped_refptr<VideoTrackInterface> video_track(
320 pc_factory_->CreateVideoTrack(label + "v0", video_source));
321 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000322 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
324 observer_.renegotiation_needed_ = false;
325 }
326
327 void AddVoiceStream(const std::string& label) {
328 // Create a local stream.
329 scoped_refptr<MediaStreamInterface> stream(
330 pc_factory_->CreateLocalMediaStream(label));
331 scoped_refptr<AudioTrackInterface> audio_track(
332 pc_factory_->CreateAudioTrack(label + "a0", NULL));
333 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000334 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
336 observer_.renegotiation_needed_ = false;
337 }
338
339 void AddAudioVideoStream(const std::string& stream_label,
340 const std::string& audio_track_label,
341 const std::string& video_track_label) {
342 // Create a local stream.
343 scoped_refptr<MediaStreamInterface> stream(
344 pc_factory_->CreateLocalMediaStream(stream_label));
345 scoped_refptr<AudioTrackInterface> audio_track(
346 pc_factory_->CreateAudioTrack(
347 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
348 stream->AddTrack(audio_track.get());
349 scoped_refptr<VideoTrackInterface> video_track(
350 pc_factory_->CreateVideoTrack(video_track_label, NULL));
351 stream->AddTrack(video_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000352 EXPECT_TRUE(pc_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
354 observer_.renegotiation_needed_ = false;
355 }
356
357 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc, bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000358 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
359 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360 MockCreateSessionDescriptionObserver>());
361 if (offer) {
362 pc_->CreateOffer(observer, NULL);
363 } else {
364 pc_->CreateAnswer(observer, NULL);
365 }
366 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
367 *desc = observer->release_desc();
368 return observer->result();
369 }
370
371 bool DoCreateOffer(SessionDescriptionInterface** desc) {
372 return DoCreateOfferAnswer(desc, true);
373 }
374
375 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
376 return DoCreateOfferAnswer(desc, false);
377 }
378
379 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000380 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
381 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382 MockSetSessionDescriptionObserver>());
383 if (local) {
384 pc_->SetLocalDescription(observer, desc);
385 } else {
386 pc_->SetRemoteDescription(observer, desc);
387 }
388 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
389 return observer->result();
390 }
391
392 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
393 return DoSetSessionDescription(desc, true);
394 }
395
396 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
397 return DoSetSessionDescription(desc, false);
398 }
399
400 // Calls PeerConnection::GetStats and check the return value.
401 // It does not verify the values in the StatReports since a RTCP packet might
402 // be required.
403 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000404 rtc::scoped_refptr<MockStatsObserver> observer(
405 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000406 if (!pc_->GetStats(
407 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408 return false;
409 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
410 return observer->called();
411 }
412
413 void InitiateCall() {
414 CreatePeerConnection();
415 // Create a local stream with audio&video tracks.
416 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
417 CreateOfferReceiveAnswer();
418 }
419
420 // Verify that RTP Header extensions has been negotiated for audio and video.
421 void VerifyRemoteRtpHeaderExtensions() {
422 const cricket::MediaContentDescription* desc =
423 cricket::GetFirstAudioContentDescription(
424 pc_->remote_description()->description());
425 ASSERT_TRUE(desc != NULL);
426 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
427
428 desc = cricket::GetFirstVideoContentDescription(
429 pc_->remote_description()->description());
430 ASSERT_TRUE(desc != NULL);
431 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
432 }
433
434 void CreateOfferAsRemoteDescription() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000435 rtc::scoped_ptr<SessionDescriptionInterface> offer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 EXPECT_TRUE(DoCreateOffer(offer.use()));
437 std::string sdp;
438 EXPECT_TRUE(offer->ToString(&sdp));
439 SessionDescriptionInterface* remote_offer =
440 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
441 sdp, NULL);
442 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
443 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
444 }
445
446 void CreateAnswerAsLocalDescription() {
447 scoped_ptr<SessionDescriptionInterface> answer;
448 EXPECT_TRUE(DoCreateAnswer(answer.use()));
449
450 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
451 // audio codec change, even if the parameter has nothing to do with
452 // receiving. Not all parameters are serialized to SDP.
453 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
454 // the SessionDescription, it is necessary to do that here to in order to
455 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
456 // https://code.google.com/p/webrtc/issues/detail?id=1356
457 std::string sdp;
458 EXPECT_TRUE(answer->ToString(&sdp));
459 SessionDescriptionInterface* new_answer =
460 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
461 sdp, NULL);
462 EXPECT_TRUE(DoSetLocalDescription(new_answer));
463 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
464 }
465
466 void CreatePrAnswerAsLocalDescription() {
467 scoped_ptr<SessionDescriptionInterface> answer;
468 EXPECT_TRUE(DoCreateAnswer(answer.use()));
469
470 std::string sdp;
471 EXPECT_TRUE(answer->ToString(&sdp));
472 SessionDescriptionInterface* pr_answer =
473 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
474 sdp, NULL);
475 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
476 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
477 }
478
479 void CreateOfferReceiveAnswer() {
480 CreateOfferAsLocalDescription();
481 std::string sdp;
482 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
483 CreateAnswerAsRemoteDescription(sdp);
484 }
485
486 void CreateOfferAsLocalDescription() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000487 rtc::scoped_ptr<SessionDescriptionInterface> offer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488 ASSERT_TRUE(DoCreateOffer(offer.use()));
489 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
490 // audio codec change, even if the parameter has nothing to do with
491 // receiving. Not all parameters are serialized to SDP.
492 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
493 // the SessionDescription, it is necessary to do that here to in order to
494 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
495 // https://code.google.com/p/webrtc/issues/detail?id=1356
496 std::string sdp;
497 EXPECT_TRUE(offer->ToString(&sdp));
498 SessionDescriptionInterface* new_offer =
499 webrtc::CreateSessionDescription(
500 SessionDescriptionInterface::kOffer,
501 sdp, NULL);
502
503 EXPECT_TRUE(DoSetLocalDescription(new_offer));
504 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000505 // Wait for the ice_complete message, so that SDP will have candidates.
506 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507 }
508
509 void CreateAnswerAsRemoteDescription(const std::string& offer) {
510 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
511 SessionDescriptionInterface::kAnswer);
512 EXPECT_TRUE(answer->Initialize(offer, NULL));
513 EXPECT_TRUE(DoSetRemoteDescription(answer));
514 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
515 }
516
517 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& offer) {
518 webrtc::JsepSessionDescription* pr_answer =
519 new webrtc::JsepSessionDescription(
520 SessionDescriptionInterface::kPrAnswer);
521 EXPECT_TRUE(pr_answer->Initialize(offer, NULL));
522 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
523 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
524 webrtc::JsepSessionDescription* answer =
525 new webrtc::JsepSessionDescription(
526 SessionDescriptionInterface::kAnswer);
527 EXPECT_TRUE(answer->Initialize(offer, NULL));
528 EXPECT_TRUE(DoSetRemoteDescription(answer));
529 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
530 }
531
532 // Help function used for waiting until a the last signaled remote stream has
533 // the same label as |stream_label|. In a few of the tests in this file we
534 // answer with the same session description as we offer and thus we can
535 // check if OnAddStream have been called with the same stream as we offer to
536 // send.
537 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
538 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
539 }
540
541 // Creates an offer and applies it as a local session description.
542 // Creates an answer with the same SDP an the offer but removes all lines
543 // that start with a:ssrc"
544 void CreateOfferReceiveAnswerWithoutSsrc() {
545 CreateOfferAsLocalDescription();
546 std::string sdp;
547 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
548 SetSsrcToZero(&sdp);
549 CreateAnswerAsRemoteDescription(sdp);
550 }
551
552 scoped_refptr<FakePortAllocatorFactory> port_allocator_factory_;
553 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
554 scoped_refptr<PeerConnectionInterface> pc_;
555 MockPeerConnectionObserver observer_;
556};
557
558TEST_F(PeerConnectionInterfaceTest,
559 CreatePeerConnectionWithDifferentConfigurations) {
560 CreatePeerConnectionWithDifferentConfigurations();
561}
562
563TEST_F(PeerConnectionInterfaceTest, AddStreams) {
564 CreatePeerConnection();
565 AddStream(kStreamLabel1);
566 AddVoiceStream(kStreamLabel2);
567 ASSERT_EQ(2u, pc_->local_streams()->count());
568
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000569 // Test we can add multiple local streams to one peerconnection.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 scoped_refptr<MediaStreamInterface> stream(
571 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
572 scoped_refptr<AudioTrackInterface> audio_track(
573 pc_factory_->CreateAudioTrack(
574 kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
575 stream->AddTrack(audio_track.get());
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000576 EXPECT_TRUE(pc_->AddStream(stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000577 EXPECT_EQ(3u, pc_->local_streams()->count());
578
579 // Remove the third stream.
580 pc_->RemoveStream(pc_->local_streams()->at(2));
581 EXPECT_EQ(2u, pc_->local_streams()->count());
582
583 // Remove the second stream.
584 pc_->RemoveStream(pc_->local_streams()->at(1));
585 EXPECT_EQ(1u, pc_->local_streams()->count());
586
587 // Remove the first stream.
588 pc_->RemoveStream(pc_->local_streams()->at(0));
589 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590}
591
592TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
593 CreatePeerConnection();
594 AddStream(kStreamLabel1);
595 ASSERT_EQ(1u, pc_->local_streams()->count());
596 pc_->RemoveStream(pc_->local_streams()->at(0));
597 EXPECT_EQ(0u, pc_->local_streams()->count());
598}
599
600TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
601 InitiateCall();
602 WaitAndVerifyOnAddStream(kStreamLabel1);
603 VerifyRemoteRtpHeaderExtensions();
604}
605
606TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
607 CreatePeerConnection();
608 AddStream(kStreamLabel1);
609 CreateOfferAsLocalDescription();
610 std::string offer;
611 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
612 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
613 WaitAndVerifyOnAddStream(kStreamLabel1);
614}
615
616TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
617 CreatePeerConnection();
618 AddStream(kStreamLabel1);
619
620 CreateOfferAsRemoteDescription();
621 CreateAnswerAsLocalDescription();
622
623 WaitAndVerifyOnAddStream(kStreamLabel1);
624}
625
626TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
627 CreatePeerConnection();
628 AddStream(kStreamLabel1);
629
630 CreateOfferAsRemoteDescription();
631 CreatePrAnswerAsLocalDescription();
632 CreateAnswerAsLocalDescription();
633
634 WaitAndVerifyOnAddStream(kStreamLabel1);
635}
636
637TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
638 InitiateCall();
639 ASSERT_EQ(1u, pc_->remote_streams()->count());
640 pc_->RemoveStream(pc_->local_streams()->at(0));
641 CreateOfferReceiveAnswer();
642 EXPECT_EQ(0u, pc_->remote_streams()->count());
643 AddStream(kStreamLabel1);
644 CreateOfferReceiveAnswer();
645}
646
647// Tests that after negotiating an audio only call, the respondent can perform a
648// renegotiation that removes the audio stream.
649TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
650 CreatePeerConnection();
651 AddVoiceStream(kStreamLabel1);
652 CreateOfferAsRemoteDescription();
653 CreateAnswerAsLocalDescription();
654
655 ASSERT_EQ(1u, pc_->remote_streams()->count());
656 pc_->RemoveStream(pc_->local_streams()->at(0));
657 CreateOfferReceiveAnswer();
658 EXPECT_EQ(0u, pc_->remote_streams()->count());
659}
660
661// Test that candidates are generated and that we can parse our own candidates.
662TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
663 CreatePeerConnection();
664
665 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
666 // SetRemoteDescription takes ownership of offer.
667 SessionDescriptionInterface* offer = NULL;
668 AddStream(kStreamLabel1);
669 EXPECT_TRUE(DoCreateOffer(&offer));
670 EXPECT_TRUE(DoSetRemoteDescription(offer));
671
672 // SetLocalDescription takes ownership of answer.
673 SessionDescriptionInterface* answer = NULL;
674 EXPECT_TRUE(DoCreateAnswer(&answer));
675 EXPECT_TRUE(DoSetLocalDescription(answer));
676
677 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
678 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
679
680 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
681}
682
683// Test that the CreateOffer and CreatAnswer will fail if the track labels are
684// not unique.
685TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
686 CreatePeerConnection();
687 // Create a regular offer for the CreateAnswer test later.
688 SessionDescriptionInterface* offer = NULL;
689 EXPECT_TRUE(DoCreateOffer(&offer));
690 EXPECT_TRUE(offer != NULL);
691 delete offer;
692 offer = NULL;
693
694 // Create a local stream with audio&video tracks having same label.
695 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
696
697 // Test CreateOffer
698 EXPECT_FALSE(DoCreateOffer(&offer));
699
700 // Test CreateAnswer
701 SessionDescriptionInterface* answer = NULL;
702 EXPECT_FALSE(DoCreateAnswer(&answer));
703}
704
705// Test that we will get different SSRCs for each tracks in the offer and answer
706// we created.
707TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
708 CreatePeerConnection();
709 // Create a local stream with audio&video tracks having different labels.
710 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
711
712 // Test CreateOffer
713 scoped_ptr<SessionDescriptionInterface> offer;
714 EXPECT_TRUE(DoCreateOffer(offer.use()));
715 int audio_ssrc = 0;
716 int video_ssrc = 0;
717 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
718 &audio_ssrc));
719 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
720 &video_ssrc));
721 EXPECT_NE(audio_ssrc, video_ssrc);
722
723 // Test CreateAnswer
724 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
725 scoped_ptr<SessionDescriptionInterface> answer;
726 EXPECT_TRUE(DoCreateAnswer(answer.use()));
727 audio_ssrc = 0;
728 video_ssrc = 0;
729 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
730 &audio_ssrc));
731 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
732 &video_ssrc));
733 EXPECT_NE(audio_ssrc, video_ssrc);
734}
735
736// Test that we can specify a certain track that we want statistics about.
737TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
738 InitiateCall();
739 ASSERT_LT(0u, pc_->remote_streams()->count());
740 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
741 scoped_refptr<MediaStreamTrackInterface> remote_audio =
742 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
743 EXPECT_TRUE(DoGetStats(remote_audio));
744
745 // Remove the stream. Since we are sending to our selves the local
746 // and the remote stream is the same.
747 pc_->RemoveStream(pc_->local_streams()->at(0));
748 // Do a re-negotiation.
749 CreateOfferReceiveAnswer();
750
751 ASSERT_EQ(0u, pc_->remote_streams()->count());
752
753 // Test that we still can get statistics for the old track. Even if it is not
754 // sent any longer.
755 EXPECT_TRUE(DoGetStats(remote_audio));
756}
757
758// Test that we can get stats on a video track.
759TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
760 InitiateCall();
761 ASSERT_LT(0u, pc_->remote_streams()->count());
762 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
763 scoped_refptr<MediaStreamTrackInterface> remote_video =
764 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
765 EXPECT_TRUE(DoGetStats(remote_video));
766}
767
768// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +0000769// TODO(tommi): Fix this test. DoGetStats will return true
770// for the unknown track (since GetStats is async), but no
771// data is returned for the track.
772TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 InitiateCall();
774 scoped_refptr<AudioTrackInterface> unknown_audio_track(
775 pc_factory_->CreateAudioTrack("unknown track", NULL));
776 EXPECT_FALSE(DoGetStats(unknown_audio_track));
777}
778
779// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 FakeConstraints constraints;
782 constraints.SetAllowRtpDataChannels();
783 CreatePeerConnection(&constraints);
784 scoped_refptr<DataChannelInterface> data1 =
785 pc_->CreateDataChannel("test1", NULL);
786 scoped_refptr<DataChannelInterface> data2 =
787 pc_->CreateDataChannel("test2", NULL);
788 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000789 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000791 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000792 new MockDataChannelObserver(data2));
793
794 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
795 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
796 std::string data_to_send1 = "testing testing";
797 std::string data_to_send2 = "testing something else";
798 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
799
800 CreateOfferReceiveAnswer();
801 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
802 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
803
804 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
805 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
806 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
807 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
808
809 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
810 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
811
812 data1->Close();
813 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
814 CreateOfferReceiveAnswer();
815 EXPECT_FALSE(observer1->IsOpen());
816 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
817 EXPECT_TRUE(observer2->IsOpen());
818
819 data_to_send2 = "testing something else again";
820 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
821
822 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
823}
824
825// This test verifies that sendnig binary data over RTP data channels should
826// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 FakeConstraints constraints;
829 constraints.SetAllowRtpDataChannels();
830 CreatePeerConnection(&constraints);
831 scoped_refptr<DataChannelInterface> data1 =
832 pc_->CreateDataChannel("test1", NULL);
833 scoped_refptr<DataChannelInterface> data2 =
834 pc_->CreateDataChannel("test2", NULL);
835 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000836 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000838 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 new MockDataChannelObserver(data2));
840
841 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
842 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
843
844 CreateOfferReceiveAnswer();
845 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
846 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
847
848 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
849 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
850
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000851 rtc::Buffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
853}
854
855// This test setup a RTP data channels in loop back and test that a channel is
856// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000857TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 FakeConstraints constraints;
859 constraints.SetAllowRtpDataChannels();
860 CreatePeerConnection(&constraints);
861 scoped_refptr<DataChannelInterface> data1 =
862 pc_->CreateDataChannel("test1", NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000863 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 new MockDataChannelObserver(data1));
865
866 CreateOfferReceiveAnswerWithoutSsrc();
867
868 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
869
870 data1->Close();
871 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
872 CreateOfferReceiveAnswerWithoutSsrc();
873 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
874 EXPECT_FALSE(observer1->IsOpen());
875}
876
877// This test that if a data channel is added in an answer a receive only channel
878// channel is created.
879TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
880 FakeConstraints constraints;
881 constraints.SetAllowRtpDataChannels();
882 CreatePeerConnection(&constraints);
883
884 std::string offer_label = "offer_channel";
885 scoped_refptr<DataChannelInterface> offer_channel =
886 pc_->CreateDataChannel(offer_label, NULL);
887
888 CreateOfferAsLocalDescription();
889
890 // Replace the data channel label in the offer and apply it as an answer.
891 std::string receive_label = "answer_channel";
892 std::string sdp;
893 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000894 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 receive_label.c_str(), receive_label.length(),
896 &sdp);
897 CreateAnswerAsRemoteDescription(sdp);
898
899 // Verify that a new incoming data channel has been created and that
900 // it is open but can't we written to.
901 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
902 DataChannelInterface* received_channel = observer_.last_datachannel_;
903 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
904 EXPECT_EQ(receive_label, received_channel->label());
905 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
906
907 // Verify that the channel we initially offered has been rejected.
908 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
909
910 // Do another offer / answer exchange and verify that the data channel is
911 // opened.
912 CreateOfferReceiveAnswer();
913 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
914 kTimeout);
915}
916
917// This test that no data channel is returned if a reliable channel is
918// requested.
919// TODO(perkj): Remove this test once reliable channels are implemented.
920TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
921 FakeConstraints constraints;
922 constraints.SetAllowRtpDataChannels();
923 CreatePeerConnection(&constraints);
924
925 std::string label = "test";
926 webrtc::DataChannelInit config;
927 config.reliable = true;
928 scoped_refptr<DataChannelInterface> channel =
929 pc_->CreateDataChannel(label, &config);
930 EXPECT_TRUE(channel == NULL);
931}
932
933// This tests that a SCTP data channel is returned using different
934// DataChannelInit configurations.
935TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
936 FakeConstraints constraints;
937 constraints.SetAllowDtlsSctpDataChannels();
938 CreatePeerConnection(&constraints);
939
940 webrtc::DataChannelInit config;
941
942 scoped_refptr<DataChannelInterface> channel =
943 pc_->CreateDataChannel("1", &config);
944 EXPECT_TRUE(channel != NULL);
945 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000946 EXPECT_TRUE(observer_.renegotiation_needed_);
947 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948
949 config.ordered = false;
950 channel = pc_->CreateDataChannel("2", &config);
951 EXPECT_TRUE(channel != NULL);
952 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000953 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954
955 config.ordered = true;
956 config.maxRetransmits = 0;
957 channel = pc_->CreateDataChannel("3", &config);
958 EXPECT_TRUE(channel != NULL);
959 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000960 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961
962 config.maxRetransmits = -1;
963 config.maxRetransmitTime = 0;
964 channel = pc_->CreateDataChannel("4", &config);
965 EXPECT_TRUE(channel != NULL);
966 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000967 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000968}
969
970// This tests that no data channel is returned if both maxRetransmits and
971// maxRetransmitTime are set for SCTP data channels.
972TEST_F(PeerConnectionInterfaceTest,
973 CreateSctpDataChannelShouldFailForInvalidConfig) {
974 FakeConstraints constraints;
975 constraints.SetAllowDtlsSctpDataChannels();
976 CreatePeerConnection(&constraints);
977
978 std::string label = "test";
979 webrtc::DataChannelInit config;
980 config.maxRetransmits = 0;
981 config.maxRetransmitTime = 0;
982
983 scoped_refptr<DataChannelInterface> channel =
984 pc_->CreateDataChannel(label, &config);
985 EXPECT_TRUE(channel == NULL);
986}
987
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988// The test verifies that creating a SCTP data channel with an id already in use
989// or out of range should fail.
990TEST_F(PeerConnectionInterfaceTest,
991 CreateSctpDataChannelWithInvalidIdShouldFail) {
992 FakeConstraints constraints;
993 constraints.SetAllowDtlsSctpDataChannels();
994 CreatePeerConnection(&constraints);
995
996 webrtc::DataChannelInit config;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000997 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000999 config.id = 1;
1000 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001 EXPECT_TRUE(channel != NULL);
1002 EXPECT_EQ(1, channel->id());
1003
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 channel = pc_->CreateDataChannel("x", &config);
1005 EXPECT_TRUE(channel == NULL);
1006
1007 config.id = cricket::kMaxSctpSid;
1008 channel = pc_->CreateDataChannel("max", &config);
1009 EXPECT_TRUE(channel != NULL);
1010 EXPECT_EQ(config.id, channel->id());
1011
1012 config.id = cricket::kMaxSctpSid + 1;
1013 channel = pc_->CreateDataChannel("x", &config);
1014 EXPECT_TRUE(channel == NULL);
1015}
1016
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001017// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1018// DataChannel.
1019TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1020 FakeConstraints constraints;
1021 constraints.SetAllowRtpDataChannels();
1022 CreatePeerConnection(&constraints);
1023
1024 scoped_refptr<DataChannelInterface> dc1 =
1025 pc_->CreateDataChannel("test1", NULL);
1026 EXPECT_TRUE(observer_.renegotiation_needed_);
1027 observer_.renegotiation_needed_ = false;
1028
1029 scoped_refptr<DataChannelInterface> dc2 =
1030 pc_->CreateDataChannel("test2", NULL);
1031 EXPECT_TRUE(observer_.renegotiation_needed_);
1032}
1033
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001035TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036 FakeConstraints constraints;
1037 constraints.SetAllowRtpDataChannels();
1038 CreatePeerConnection(&constraints);
1039
1040 scoped_refptr<DataChannelInterface> data1 =
1041 pc_->CreateDataChannel("test1", NULL);
1042 scoped_refptr<DataChannelInterface> data2 =
1043 pc_->CreateDataChannel("test2", NULL);
1044 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001045 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001047 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001048 new MockDataChannelObserver(data2));
1049
1050 CreateOfferReceiveAnswer();
1051 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1052 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1053
1054 ReleasePeerConnection();
1055 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1056 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1057}
1058
1059// This test that data channels can be rejected in an answer.
1060TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1061 FakeConstraints constraints;
1062 constraints.SetAllowRtpDataChannels();
1063 CreatePeerConnection(&constraints);
1064
1065 scoped_refptr<DataChannelInterface> offer_channel(
1066 pc_->CreateDataChannel("offer_channel", NULL));
1067
1068 CreateOfferAsLocalDescription();
1069
1070 // Create an answer where the m-line for data channels are rejected.
1071 std::string sdp;
1072 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1073 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1074 SessionDescriptionInterface::kAnswer);
1075 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1076 cricket::ContentInfo* data_info =
1077 answer->description()->GetContentByName("data");
1078 data_info->rejected = true;
1079
1080 DoSetRemoteDescription(answer);
1081 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1082}
1083
1084// Test that we can create a session description from an SDP string from
1085// FireFox, use it as a remote session description, generate an answer and use
1086// the answer as a local description.
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001087TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001088 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001089 FakeConstraints constraints;
1090 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1091 true);
1092 CreatePeerConnection(&constraints);
1093 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1094 SessionDescriptionInterface* desc =
1095 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1096 webrtc::kFireFoxSdpOffer);
1097 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1098 CreateAnswerAsLocalDescription();
1099 ASSERT_TRUE(pc_->local_description() != NULL);
1100 ASSERT_TRUE(pc_->remote_description() != NULL);
1101
1102 const cricket::ContentInfo* content =
1103 cricket::GetFirstAudioContent(pc_->local_description()->description());
1104 ASSERT_TRUE(content != NULL);
1105 EXPECT_FALSE(content->rejected);
1106
1107 content =
1108 cricket::GetFirstVideoContent(pc_->local_description()->description());
1109 ASSERT_TRUE(content != NULL);
1110 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001111#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001112 content =
1113 cricket::GetFirstDataContent(pc_->local_description()->description());
1114 ASSERT_TRUE(content != NULL);
1115 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001116#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001117}
1118
1119// Test that we can create an audio only offer and receive an answer with a
1120// limited set of audio codecs and receive an updated offer with more audio
1121// codecs, where the added codecs are not supported.
1122TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1123 CreatePeerConnection();
1124 AddVoiceStream("audio_label");
1125 CreateOfferAsLocalDescription();
1126
1127 SessionDescriptionInterface* answer =
1128 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
1129 webrtc::kAudioSdp);
1130 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1131
1132 SessionDescriptionInterface* updated_offer =
1133 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1134 webrtc::kAudioSdpWithUnsupportedCodecs);
1135 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1136 CreateAnswerAsLocalDescription();
1137}
1138
1139// Test that PeerConnection::Close changes the states to closed and all remote
1140// tracks change state to ended.
1141TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
1142 // Initialize a PeerConnection and negotiate local and remote session
1143 // description.
1144 InitiateCall();
1145 ASSERT_EQ(1u, pc_->local_streams()->count());
1146 ASSERT_EQ(1u, pc_->remote_streams()->count());
1147
1148 pc_->Close();
1149
1150 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
1151 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
1152 pc_->ice_connection_state());
1153 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
1154 pc_->ice_gathering_state());
1155
1156 EXPECT_EQ(1u, pc_->local_streams()->count());
1157 EXPECT_EQ(1u, pc_->remote_streams()->count());
1158
1159 scoped_refptr<MediaStreamInterface> remote_stream =
1160 pc_->remote_streams()->at(0);
1161 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1162 remote_stream->GetVideoTracks()[0]->state());
1163 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1164 remote_stream->GetAudioTracks()[0]->state());
1165}
1166
1167// Test that PeerConnection methods fails gracefully after
1168// PeerConnection::Close has been called.
1169TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
1170 CreatePeerConnection();
1171 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1172 CreateOfferAsRemoteDescription();
1173 CreateAnswerAsLocalDescription();
1174
1175 ASSERT_EQ(1u, pc_->local_streams()->count());
1176 scoped_refptr<MediaStreamInterface> local_stream =
1177 pc_->local_streams()->at(0);
1178
1179 pc_->Close();
1180
1181 pc_->RemoveStream(local_stream);
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +00001182 EXPECT_FALSE(pc_->AddStream(local_stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183
1184 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001185 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001186 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00001187 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001188
1189 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
1190
1191 EXPECT_TRUE(pc_->local_description() != NULL);
1192 EXPECT_TRUE(pc_->remote_description() != NULL);
1193
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001194 rtc::scoped_ptr<SessionDescriptionInterface> offer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195 EXPECT_TRUE(DoCreateOffer(offer.use()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001196 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001197 EXPECT_TRUE(DoCreateAnswer(answer.use()));
1198
1199 std::string sdp;
1200 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
1201 SessionDescriptionInterface* remote_offer =
1202 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1203 sdp, NULL);
1204 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
1205
1206 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
1207 SessionDescriptionInterface* local_offer =
1208 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1209 sdp, NULL);
1210 EXPECT_FALSE(DoSetLocalDescription(local_offer));
1211}
1212
1213// Test that GetStats can still be called after PeerConnection::Close.
1214TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
1215 InitiateCall();
1216 pc_->Close();
1217 DoGetStats(NULL);
1218}