blob: bf60673400b847493147d519b252c385a7dc4e9c [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 }
135 virtual void OnError() {}
136 virtual void OnSignalingChange(
137 PeerConnectionInterface::SignalingState new_state) {
138 EXPECT_EQ(pc_->signaling_state(), new_state);
139 state_ = new_state;
140 }
141 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
142 virtual void OnStateChange(StateType state_changed) {
143 if (pc_.get() == NULL)
144 return;
145 switch (state_changed) {
146 case kSignalingState:
147 // OnSignalingChange and OnStateChange(kSignalingState) should always
148 // be called approximately simultaneously. To ease testing, we require
149 // that they always be called in that order. This check verifies
150 // that OnSignalingChange has just been called.
151 EXPECT_EQ(pc_->signaling_state(), state_);
152 break;
153 case kIceState:
154 ADD_FAILURE();
155 break;
156 default:
157 ADD_FAILURE();
158 break;
159 }
160 }
161 virtual void OnAddStream(MediaStreamInterface* stream) {
162 last_added_stream_ = stream;
163 }
164 virtual void OnRemoveStream(MediaStreamInterface* stream) {
165 last_removed_stream_ = stream;
166 }
167 virtual void OnRenegotiationNeeded() {
168 renegotiation_needed_ = true;
169 }
170 virtual void OnDataChannel(DataChannelInterface* data_channel) {
171 last_datachannel_ = data_channel;
172 }
173
174 virtual void OnIceConnectionChange(
175 PeerConnectionInterface::IceConnectionState new_state) {
176 EXPECT_EQ(pc_->ice_connection_state(), new_state);
177 }
178 virtual void OnIceGatheringChange(
179 PeerConnectionInterface::IceGatheringState new_state) {
180 EXPECT_EQ(pc_->ice_gathering_state(), new_state);
181 }
182 virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
183 EXPECT_NE(PeerConnectionInterface::kIceGatheringNew,
184 pc_->ice_gathering_state());
185
186 std::string sdp;
187 EXPECT_TRUE(candidate->ToString(&sdp));
188 EXPECT_LT(0u, sdp.size());
189 last_candidate_.reset(webrtc::CreateIceCandidate(candidate->sdp_mid(),
190 candidate->sdp_mline_index(), sdp, NULL));
191 EXPECT_TRUE(last_candidate_.get() != NULL);
192 }
193 // TODO(bemasc): Remove this once callers transition to OnSignalingChange.
194 virtual void OnIceComplete() {
195 ice_complete_ = true;
196 // OnIceGatheringChange(IceGatheringCompleted) and OnIceComplete() should
197 // be called approximately simultaneously. For ease of testing, this
198 // check additionally requires that they be called in the above order.
199 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
200 pc_->ice_gathering_state());
201 }
202
203 // Returns the label of the last added stream.
204 // Empty string if no stream have been added.
205 std::string GetLastAddedStreamLabel() {
206 if (last_added_stream_.get())
207 return last_added_stream_->label();
208 return "";
209 }
210 std::string GetLastRemovedStreamLabel() {
211 if (last_removed_stream_.get())
212 return last_removed_stream_->label();
213 return "";
214 }
215
216 scoped_refptr<PeerConnectionInterface> pc_;
217 PeerConnectionInterface::SignalingState state_;
218 scoped_ptr<IceCandidateInterface> last_candidate_;
219 scoped_refptr<DataChannelInterface> last_datachannel_;
220 bool renegotiation_needed_;
221 bool ice_complete_;
222
223 private:
224 scoped_refptr<MediaStreamInterface> last_added_stream_;
225 scoped_refptr<MediaStreamInterface> last_removed_stream_;
226};
227
228} // namespace
229class PeerConnectionInterfaceTest : public testing::Test {
230 protected:
231 virtual void SetUp() {
232 pc_factory_ = webrtc::CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000233 rtc::Thread::Current(), rtc::Thread::Current(), NULL, NULL,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234 NULL);
235 ASSERT_TRUE(pc_factory_.get() != NULL);
236 }
237
238 void CreatePeerConnection() {
239 CreatePeerConnection("", "", NULL);
240 }
241
242 void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
243 CreatePeerConnection("", "", constraints);
244 }
245
246 void CreatePeerConnection(const std::string& uri,
247 const std::string& password,
248 webrtc::MediaConstraintsInterface* constraints) {
249 PeerConnectionInterface::IceServer server;
250 PeerConnectionInterface::IceServers servers;
251 server.uri = uri;
252 server.password = password;
253 servers.push_back(server);
254
255 port_allocator_factory_ = FakePortAllocatorFactory::Create();
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000256
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000257 // DTLS does not work in a loopback call, so is disabled for most of the
258 // tests in this file. We only create a FakeIdentityService if the test
259 // explicitly sets the constraint.
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000260 FakeIdentityService* dtls_service = NULL;
261 bool dtls;
262 if (FindConstraint(constraints,
263 webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
264 &dtls,
265 NULL) && dtls) {
266 dtls_service = new FakeIdentityService();
267 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268 pc_ = pc_factory_->CreatePeerConnection(servers, constraints,
269 port_allocator_factory_.get(),
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000270 dtls_service,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 &observer_);
272 ASSERT_TRUE(pc_.get() != NULL);
273 observer_.SetPeerConnectionInterface(pc_.get());
274 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
275 }
276
277 void CreatePeerConnectionWithDifferentConfigurations() {
278 CreatePeerConnection(kStunAddressOnly, "", NULL);
279 EXPECT_EQ(1u, port_allocator_factory_->stun_configs().size());
280 EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
281 EXPECT_EQ("address",
282 port_allocator_factory_->stun_configs()[0].server.hostname());
283 EXPECT_EQ(kDefaultStunPort,
284 port_allocator_factory_->stun_configs()[0].server.port());
285
286 CreatePeerConnection(kStunInvalidPort, "", NULL);
287 EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size());
288 EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
289
290 CreatePeerConnection(kStunAddressPortAndMore1, "", NULL);
291 EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size());
292 EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
293
294 CreatePeerConnection(kStunAddressPortAndMore2, "", NULL);
295 EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size());
296 EXPECT_EQ(0u, port_allocator_factory_->turn_configs().size());
297
298 CreatePeerConnection(kTurnIceServerUri, kTurnPassword, NULL);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000299 EXPECT_EQ(0u, port_allocator_factory_->stun_configs().size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300 EXPECT_EQ(1u, port_allocator_factory_->turn_configs().size());
301 EXPECT_EQ(kTurnUsername,
302 port_allocator_factory_->turn_configs()[0].username);
303 EXPECT_EQ(kTurnPassword,
304 port_allocator_factory_->turn_configs()[0].password);
305 EXPECT_EQ(kTurnHostname,
306 port_allocator_factory_->turn_configs()[0].server.hostname());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307 }
308
309 void ReleasePeerConnection() {
310 pc_ = NULL;
311 observer_.SetPeerConnectionInterface(NULL);
312 }
313
314 void AddStream(const std::string& label) {
315 // Create a local stream.
316 scoped_refptr<MediaStreamInterface> stream(
317 pc_factory_->CreateLocalMediaStream(label));
318 scoped_refptr<VideoSourceInterface> video_source(
319 pc_factory_->CreateVideoSource(new cricket::FakeVideoCapturer(), NULL));
320 scoped_refptr<VideoTrackInterface> video_track(
321 pc_factory_->CreateVideoTrack(label + "v0", video_source));
322 stream->AddTrack(video_track.get());
323 EXPECT_TRUE(pc_->AddStream(stream, NULL));
324 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
325 observer_.renegotiation_needed_ = false;
326 }
327
328 void AddVoiceStream(const std::string& label) {
329 // Create a local stream.
330 scoped_refptr<MediaStreamInterface> stream(
331 pc_factory_->CreateLocalMediaStream(label));
332 scoped_refptr<AudioTrackInterface> audio_track(
333 pc_factory_->CreateAudioTrack(label + "a0", NULL));
334 stream->AddTrack(audio_track.get());
335 EXPECT_TRUE(pc_->AddStream(stream, NULL));
336 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
337 observer_.renegotiation_needed_ = false;
338 }
339
340 void AddAudioVideoStream(const std::string& stream_label,
341 const std::string& audio_track_label,
342 const std::string& video_track_label) {
343 // Create a local stream.
344 scoped_refptr<MediaStreamInterface> stream(
345 pc_factory_->CreateLocalMediaStream(stream_label));
346 scoped_refptr<AudioTrackInterface> audio_track(
347 pc_factory_->CreateAudioTrack(
348 audio_track_label, static_cast<AudioSourceInterface*>(NULL)));
349 stream->AddTrack(audio_track.get());
350 scoped_refptr<VideoTrackInterface> video_track(
351 pc_factory_->CreateVideoTrack(video_track_label, NULL));
352 stream->AddTrack(video_track.get());
353 EXPECT_TRUE(pc_->AddStream(stream, NULL));
354 EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
355 observer_.renegotiation_needed_ = false;
356 }
357
358 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc, bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000359 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
360 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361 MockCreateSessionDescriptionObserver>());
362 if (offer) {
363 pc_->CreateOffer(observer, NULL);
364 } else {
365 pc_->CreateAnswer(observer, NULL);
366 }
367 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
368 *desc = observer->release_desc();
369 return observer->result();
370 }
371
372 bool DoCreateOffer(SessionDescriptionInterface** desc) {
373 return DoCreateOfferAnswer(desc, true);
374 }
375
376 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
377 return DoCreateOfferAnswer(desc, false);
378 }
379
380 bool DoSetSessionDescription(SessionDescriptionInterface* desc, bool local) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000381 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
382 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000383 MockSetSessionDescriptionObserver>());
384 if (local) {
385 pc_->SetLocalDescription(observer, desc);
386 } else {
387 pc_->SetRemoteDescription(observer, desc);
388 }
389 EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
390 return observer->result();
391 }
392
393 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
394 return DoSetSessionDescription(desc, true);
395 }
396
397 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
398 return DoSetSessionDescription(desc, false);
399 }
400
401 // Calls PeerConnection::GetStats and check the return value.
402 // It does not verify the values in the StatReports since a RTCP packet might
403 // be required.
404 bool DoGetStats(MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000405 rtc::scoped_refptr<MockStatsObserver> observer(
406 new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000407 if (!pc_->GetStats(
408 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000409 return false;
410 EXPECT_TRUE_WAIT(observer->called(), kTimeout);
411 return observer->called();
412 }
413
414 void InitiateCall() {
415 CreatePeerConnection();
416 // Create a local stream with audio&video tracks.
417 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
418 CreateOfferReceiveAnswer();
419 }
420
421 // Verify that RTP Header extensions has been negotiated for audio and video.
422 void VerifyRemoteRtpHeaderExtensions() {
423 const cricket::MediaContentDescription* desc =
424 cricket::GetFirstAudioContentDescription(
425 pc_->remote_description()->description());
426 ASSERT_TRUE(desc != NULL);
427 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
428
429 desc = cricket::GetFirstVideoContentDescription(
430 pc_->remote_description()->description());
431 ASSERT_TRUE(desc != NULL);
432 EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
433 }
434
435 void CreateOfferAsRemoteDescription() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000436 rtc::scoped_ptr<SessionDescriptionInterface> offer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437 EXPECT_TRUE(DoCreateOffer(offer.use()));
438 std::string sdp;
439 EXPECT_TRUE(offer->ToString(&sdp));
440 SessionDescriptionInterface* remote_offer =
441 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
442 sdp, NULL);
443 EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
444 EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
445 }
446
447 void CreateAnswerAsLocalDescription() {
448 scoped_ptr<SessionDescriptionInterface> answer;
449 EXPECT_TRUE(DoCreateAnswer(answer.use()));
450
451 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
452 // audio codec change, even if the parameter has nothing to do with
453 // receiving. Not all parameters are serialized to SDP.
454 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
455 // the SessionDescription, it is necessary to do that here to in order to
456 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
457 // https://code.google.com/p/webrtc/issues/detail?id=1356
458 std::string sdp;
459 EXPECT_TRUE(answer->ToString(&sdp));
460 SessionDescriptionInterface* new_answer =
461 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
462 sdp, NULL);
463 EXPECT_TRUE(DoSetLocalDescription(new_answer));
464 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
465 }
466
467 void CreatePrAnswerAsLocalDescription() {
468 scoped_ptr<SessionDescriptionInterface> answer;
469 EXPECT_TRUE(DoCreateAnswer(answer.use()));
470
471 std::string sdp;
472 EXPECT_TRUE(answer->ToString(&sdp));
473 SessionDescriptionInterface* pr_answer =
474 webrtc::CreateSessionDescription(SessionDescriptionInterface::kPrAnswer,
475 sdp, NULL);
476 EXPECT_TRUE(DoSetLocalDescription(pr_answer));
477 EXPECT_EQ(PeerConnectionInterface::kHaveLocalPrAnswer, observer_.state_);
478 }
479
480 void CreateOfferReceiveAnswer() {
481 CreateOfferAsLocalDescription();
482 std::string sdp;
483 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
484 CreateAnswerAsRemoteDescription(sdp);
485 }
486
487 void CreateOfferAsLocalDescription() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000488 rtc::scoped_ptr<SessionDescriptionInterface> offer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 ASSERT_TRUE(DoCreateOffer(offer.use()));
490 // TODO(perkj): Currently SetLocalDescription fails if any parameters in an
491 // audio codec change, even if the parameter has nothing to do with
492 // receiving. Not all parameters are serialized to SDP.
493 // Since CreatePrAnswerAsLocalDescription serialize/deserialize
494 // the SessionDescription, it is necessary to do that here to in order to
495 // get ReceiveOfferCreatePrAnswerAndAnswer and RenegotiateAudioOnly to pass.
496 // https://code.google.com/p/webrtc/issues/detail?id=1356
497 std::string sdp;
498 EXPECT_TRUE(offer->ToString(&sdp));
499 SessionDescriptionInterface* new_offer =
500 webrtc::CreateSessionDescription(
501 SessionDescriptionInterface::kOffer,
502 sdp, NULL);
503
504 EXPECT_TRUE(DoSetLocalDescription(new_offer));
505 EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
mallinath@webrtc.org68cbd012014-01-22 00:16:46 +0000506 // Wait for the ice_complete message, so that SDP will have candidates.
507 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508 }
509
510 void CreateAnswerAsRemoteDescription(const std::string& offer) {
511 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
512 SessionDescriptionInterface::kAnswer);
513 EXPECT_TRUE(answer->Initialize(offer, NULL));
514 EXPECT_TRUE(DoSetRemoteDescription(answer));
515 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
516 }
517
518 void CreatePrAnswerAndAnswerAsRemoteDescription(const std::string& offer) {
519 webrtc::JsepSessionDescription* pr_answer =
520 new webrtc::JsepSessionDescription(
521 SessionDescriptionInterface::kPrAnswer);
522 EXPECT_TRUE(pr_answer->Initialize(offer, NULL));
523 EXPECT_TRUE(DoSetRemoteDescription(pr_answer));
524 EXPECT_EQ(PeerConnectionInterface::kHaveRemotePrAnswer, observer_.state_);
525 webrtc::JsepSessionDescription* answer =
526 new webrtc::JsepSessionDescription(
527 SessionDescriptionInterface::kAnswer);
528 EXPECT_TRUE(answer->Initialize(offer, NULL));
529 EXPECT_TRUE(DoSetRemoteDescription(answer));
530 EXPECT_EQ(PeerConnectionInterface::kStable, observer_.state_);
531 }
532
533 // Help function used for waiting until a the last signaled remote stream has
534 // the same label as |stream_label|. In a few of the tests in this file we
535 // answer with the same session description as we offer and thus we can
536 // check if OnAddStream have been called with the same stream as we offer to
537 // send.
538 void WaitAndVerifyOnAddStream(const std::string& stream_label) {
539 EXPECT_EQ_WAIT(stream_label, observer_.GetLastAddedStreamLabel(), kTimeout);
540 }
541
542 // Creates an offer and applies it as a local session description.
543 // Creates an answer with the same SDP an the offer but removes all lines
544 // that start with a:ssrc"
545 void CreateOfferReceiveAnswerWithoutSsrc() {
546 CreateOfferAsLocalDescription();
547 std::string sdp;
548 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
549 SetSsrcToZero(&sdp);
550 CreateAnswerAsRemoteDescription(sdp);
551 }
552
553 scoped_refptr<FakePortAllocatorFactory> port_allocator_factory_;
554 scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory_;
555 scoped_refptr<PeerConnectionInterface> pc_;
556 MockPeerConnectionObserver observer_;
557};
558
559TEST_F(PeerConnectionInterfaceTest,
560 CreatePeerConnectionWithDifferentConfigurations) {
561 CreatePeerConnectionWithDifferentConfigurations();
562}
563
564TEST_F(PeerConnectionInterfaceTest, AddStreams) {
565 CreatePeerConnection();
566 AddStream(kStreamLabel1);
567 AddVoiceStream(kStreamLabel2);
568 ASSERT_EQ(2u, pc_->local_streams()->count());
569
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000570 // Test we can add multiple local streams to one peerconnection.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 scoped_refptr<MediaStreamInterface> stream(
572 pc_factory_->CreateLocalMediaStream(kStreamLabel3));
573 scoped_refptr<AudioTrackInterface> audio_track(
574 pc_factory_->CreateAudioTrack(
575 kStreamLabel3, static_cast<AudioSourceInterface*>(NULL)));
576 stream->AddTrack(audio_track.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577 EXPECT_TRUE(pc_->AddStream(stream, NULL));
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000578 EXPECT_EQ(3u, pc_->local_streams()->count());
579
580 // Remove the third stream.
581 pc_->RemoveStream(pc_->local_streams()->at(2));
582 EXPECT_EQ(2u, pc_->local_streams()->count());
583
584 // Remove the second stream.
585 pc_->RemoveStream(pc_->local_streams()->at(1));
586 EXPECT_EQ(1u, pc_->local_streams()->count());
587
588 // Remove the first stream.
589 pc_->RemoveStream(pc_->local_streams()->at(0));
590 EXPECT_EQ(0u, pc_->local_streams()->count());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591}
592
593TEST_F(PeerConnectionInterfaceTest, RemoveStream) {
594 CreatePeerConnection();
595 AddStream(kStreamLabel1);
596 ASSERT_EQ(1u, pc_->local_streams()->count());
597 pc_->RemoveStream(pc_->local_streams()->at(0));
598 EXPECT_EQ(0u, pc_->local_streams()->count());
599}
600
601TEST_F(PeerConnectionInterfaceTest, CreateOfferReceiveAnswer) {
602 InitiateCall();
603 WaitAndVerifyOnAddStream(kStreamLabel1);
604 VerifyRemoteRtpHeaderExtensions();
605}
606
607TEST_F(PeerConnectionInterfaceTest, CreateOfferReceivePrAnswerAndAnswer) {
608 CreatePeerConnection();
609 AddStream(kStreamLabel1);
610 CreateOfferAsLocalDescription();
611 std::string offer;
612 EXPECT_TRUE(pc_->local_description()->ToString(&offer));
613 CreatePrAnswerAndAnswerAsRemoteDescription(offer);
614 WaitAndVerifyOnAddStream(kStreamLabel1);
615}
616
617TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreateAnswer) {
618 CreatePeerConnection();
619 AddStream(kStreamLabel1);
620
621 CreateOfferAsRemoteDescription();
622 CreateAnswerAsLocalDescription();
623
624 WaitAndVerifyOnAddStream(kStreamLabel1);
625}
626
627TEST_F(PeerConnectionInterfaceTest, ReceiveOfferCreatePrAnswerAndAnswer) {
628 CreatePeerConnection();
629 AddStream(kStreamLabel1);
630
631 CreateOfferAsRemoteDescription();
632 CreatePrAnswerAsLocalDescription();
633 CreateAnswerAsLocalDescription();
634
635 WaitAndVerifyOnAddStream(kStreamLabel1);
636}
637
638TEST_F(PeerConnectionInterfaceTest, Renegotiate) {
639 InitiateCall();
640 ASSERT_EQ(1u, pc_->remote_streams()->count());
641 pc_->RemoveStream(pc_->local_streams()->at(0));
642 CreateOfferReceiveAnswer();
643 EXPECT_EQ(0u, pc_->remote_streams()->count());
644 AddStream(kStreamLabel1);
645 CreateOfferReceiveAnswer();
646}
647
648// Tests that after negotiating an audio only call, the respondent can perform a
649// renegotiation that removes the audio stream.
650TEST_F(PeerConnectionInterfaceTest, RenegotiateAudioOnly) {
651 CreatePeerConnection();
652 AddVoiceStream(kStreamLabel1);
653 CreateOfferAsRemoteDescription();
654 CreateAnswerAsLocalDescription();
655
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}
661
662// Test that candidates are generated and that we can parse our own candidates.
663TEST_F(PeerConnectionInterfaceTest, IceCandidates) {
664 CreatePeerConnection();
665
666 EXPECT_FALSE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
667 // SetRemoteDescription takes ownership of offer.
668 SessionDescriptionInterface* offer = NULL;
669 AddStream(kStreamLabel1);
670 EXPECT_TRUE(DoCreateOffer(&offer));
671 EXPECT_TRUE(DoSetRemoteDescription(offer));
672
673 // SetLocalDescription takes ownership of answer.
674 SessionDescriptionInterface* answer = NULL;
675 EXPECT_TRUE(DoCreateAnswer(&answer));
676 EXPECT_TRUE(DoSetLocalDescription(answer));
677
678 EXPECT_TRUE_WAIT(observer_.last_candidate_.get() != NULL, kTimeout);
679 EXPECT_TRUE_WAIT(observer_.ice_complete_, kTimeout);
680
681 EXPECT_TRUE(pc_->AddIceCandidate(observer_.last_candidate_.get()));
682}
683
684// Test that the CreateOffer and CreatAnswer will fail if the track labels are
685// not unique.
686TEST_F(PeerConnectionInterfaceTest, CreateOfferAnswerWithInvalidStream) {
687 CreatePeerConnection();
688 // Create a regular offer for the CreateAnswer test later.
689 SessionDescriptionInterface* offer = NULL;
690 EXPECT_TRUE(DoCreateOffer(&offer));
691 EXPECT_TRUE(offer != NULL);
692 delete offer;
693 offer = NULL;
694
695 // Create a local stream with audio&video tracks having same label.
696 AddAudioVideoStream(kStreamLabel1, "track_label", "track_label");
697
698 // Test CreateOffer
699 EXPECT_FALSE(DoCreateOffer(&offer));
700
701 // Test CreateAnswer
702 SessionDescriptionInterface* answer = NULL;
703 EXPECT_FALSE(DoCreateAnswer(&answer));
704}
705
706// Test that we will get different SSRCs for each tracks in the offer and answer
707// we created.
708TEST_F(PeerConnectionInterfaceTest, SsrcInOfferAnswer) {
709 CreatePeerConnection();
710 // Create a local stream with audio&video tracks having different labels.
711 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
712
713 // Test CreateOffer
714 scoped_ptr<SessionDescriptionInterface> offer;
715 EXPECT_TRUE(DoCreateOffer(offer.use()));
716 int audio_ssrc = 0;
717 int video_ssrc = 0;
718 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(offer->description()),
719 &audio_ssrc));
720 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(offer->description()),
721 &video_ssrc));
722 EXPECT_NE(audio_ssrc, video_ssrc);
723
724 // Test CreateAnswer
725 EXPECT_TRUE(DoSetRemoteDescription(offer.release()));
726 scoped_ptr<SessionDescriptionInterface> answer;
727 EXPECT_TRUE(DoCreateAnswer(answer.use()));
728 audio_ssrc = 0;
729 video_ssrc = 0;
730 EXPECT_TRUE(GetFirstSsrc(GetFirstAudioContent(answer->description()),
731 &audio_ssrc));
732 EXPECT_TRUE(GetFirstSsrc(GetFirstVideoContent(answer->description()),
733 &video_ssrc));
734 EXPECT_NE(audio_ssrc, video_ssrc);
735}
736
737// Test that we can specify a certain track that we want statistics about.
738TEST_F(PeerConnectionInterfaceTest, GetStatsForSpecificTrack) {
739 InitiateCall();
740 ASSERT_LT(0u, pc_->remote_streams()->count());
741 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetAudioTracks().size());
742 scoped_refptr<MediaStreamTrackInterface> remote_audio =
743 pc_->remote_streams()->at(0)->GetAudioTracks()[0];
744 EXPECT_TRUE(DoGetStats(remote_audio));
745
746 // Remove the stream. Since we are sending to our selves the local
747 // and the remote stream is the same.
748 pc_->RemoveStream(pc_->local_streams()->at(0));
749 // Do a re-negotiation.
750 CreateOfferReceiveAnswer();
751
752 ASSERT_EQ(0u, pc_->remote_streams()->count());
753
754 // Test that we still can get statistics for the old track. Even if it is not
755 // sent any longer.
756 EXPECT_TRUE(DoGetStats(remote_audio));
757}
758
759// Test that we can get stats on a video track.
760TEST_F(PeerConnectionInterfaceTest, GetStatsForVideoTrack) {
761 InitiateCall();
762 ASSERT_LT(0u, pc_->remote_streams()->count());
763 ASSERT_LT(0u, pc_->remote_streams()->at(0)->GetVideoTracks().size());
764 scoped_refptr<MediaStreamTrackInterface> remote_video =
765 pc_->remote_streams()->at(0)->GetVideoTracks()[0];
766 EXPECT_TRUE(DoGetStats(remote_video));
767}
768
769// Test that we don't get statistics for an invalid track.
tommi@webrtc.org908f57e2014-07-21 11:44:39 +0000770// TODO(tommi): Fix this test. DoGetStats will return true
771// for the unknown track (since GetStats is async), but no
772// data is returned for the track.
773TEST_F(PeerConnectionInterfaceTest, DISABLED_GetStatsForInvalidTrack) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774 InitiateCall();
775 scoped_refptr<AudioTrackInterface> unknown_audio_track(
776 pc_factory_->CreateAudioTrack("unknown track", NULL));
777 EXPECT_FALSE(DoGetStats(unknown_audio_track));
778}
779
780// This test setup two RTP data channels in loop back.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781TEST_F(PeerConnectionInterfaceTest, TestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 FakeConstraints constraints;
783 constraints.SetAllowRtpDataChannels();
784 CreatePeerConnection(&constraints);
785 scoped_refptr<DataChannelInterface> data1 =
786 pc_->CreateDataChannel("test1", NULL);
787 scoped_refptr<DataChannelInterface> data2 =
788 pc_->CreateDataChannel("test2", NULL);
789 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000790 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000792 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 new MockDataChannelObserver(data2));
794
795 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
796 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
797 std::string data_to_send1 = "testing testing";
798 std::string data_to_send2 = "testing something else";
799 EXPECT_FALSE(data1->Send(DataBuffer(data_to_send1)));
800
801 CreateOfferReceiveAnswer();
802 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
803 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
804
805 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
806 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
807 EXPECT_TRUE(data1->Send(DataBuffer(data_to_send1)));
808 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
809
810 EXPECT_EQ_WAIT(data_to_send1, observer1->last_message(), kTimeout);
811 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
812
813 data1->Close();
814 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
815 CreateOfferReceiveAnswer();
816 EXPECT_FALSE(observer1->IsOpen());
817 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
818 EXPECT_TRUE(observer2->IsOpen());
819
820 data_to_send2 = "testing something else again";
821 EXPECT_TRUE(data2->Send(DataBuffer(data_to_send2)));
822
823 EXPECT_EQ_WAIT(data_to_send2, observer2->last_message(), kTimeout);
824}
825
826// This test verifies that sendnig binary data over RTP data channels should
827// fail.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828TEST_F(PeerConnectionInterfaceTest, TestSendBinaryOnRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829 FakeConstraints constraints;
830 constraints.SetAllowRtpDataChannels();
831 CreatePeerConnection(&constraints);
832 scoped_refptr<DataChannelInterface> data1 =
833 pc_->CreateDataChannel("test1", NULL);
834 scoped_refptr<DataChannelInterface> data2 =
835 pc_->CreateDataChannel("test2", NULL);
836 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000837 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000839 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 new MockDataChannelObserver(data2));
841
842 EXPECT_EQ(DataChannelInterface::kConnecting, data1->state());
843 EXPECT_EQ(DataChannelInterface::kConnecting, data2->state());
844
845 CreateOfferReceiveAnswer();
846 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
847 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
848
849 EXPECT_EQ(DataChannelInterface::kOpen, data1->state());
850 EXPECT_EQ(DataChannelInterface::kOpen, data2->state());
851
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000852 rtc::Buffer buffer("test", 4);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 EXPECT_FALSE(data1->Send(DataBuffer(buffer, true)));
854}
855
856// This test setup a RTP data channels in loop back and test that a channel is
857// opened even if the remote end answer with a zero SSRC.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858TEST_F(PeerConnectionInterfaceTest, TestSendOnlyDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 FakeConstraints constraints;
860 constraints.SetAllowRtpDataChannels();
861 CreatePeerConnection(&constraints);
862 scoped_refptr<DataChannelInterface> data1 =
863 pc_->CreateDataChannel("test1", NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000864 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865 new MockDataChannelObserver(data1));
866
867 CreateOfferReceiveAnswerWithoutSsrc();
868
869 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
870
871 data1->Close();
872 EXPECT_EQ(DataChannelInterface::kClosing, data1->state());
873 CreateOfferReceiveAnswerWithoutSsrc();
874 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
875 EXPECT_FALSE(observer1->IsOpen());
876}
877
878// This test that if a data channel is added in an answer a receive only channel
879// channel is created.
880TEST_F(PeerConnectionInterfaceTest, TestReceiveOnlyDataChannel) {
881 FakeConstraints constraints;
882 constraints.SetAllowRtpDataChannels();
883 CreatePeerConnection(&constraints);
884
885 std::string offer_label = "offer_channel";
886 scoped_refptr<DataChannelInterface> offer_channel =
887 pc_->CreateDataChannel(offer_label, NULL);
888
889 CreateOfferAsLocalDescription();
890
891 // Replace the data channel label in the offer and apply it as an answer.
892 std::string receive_label = "answer_channel";
893 std::string sdp;
894 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000895 rtc::replace_substrs(offer_label.c_str(), offer_label.length(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000896 receive_label.c_str(), receive_label.length(),
897 &sdp);
898 CreateAnswerAsRemoteDescription(sdp);
899
900 // Verify that a new incoming data channel has been created and that
901 // it is open but can't we written to.
902 ASSERT_TRUE(observer_.last_datachannel_ != NULL);
903 DataChannelInterface* received_channel = observer_.last_datachannel_;
904 EXPECT_EQ(DataChannelInterface::kConnecting, received_channel->state());
905 EXPECT_EQ(receive_label, received_channel->label());
906 EXPECT_FALSE(received_channel->Send(DataBuffer("something")));
907
908 // Verify that the channel we initially offered has been rejected.
909 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
910
911 // Do another offer / answer exchange and verify that the data channel is
912 // opened.
913 CreateOfferReceiveAnswer();
914 EXPECT_EQ_WAIT(DataChannelInterface::kOpen, received_channel->state(),
915 kTimeout);
916}
917
918// This test that no data channel is returned if a reliable channel is
919// requested.
920// TODO(perkj): Remove this test once reliable channels are implemented.
921TEST_F(PeerConnectionInterfaceTest, CreateReliableRtpDataChannelShouldFail) {
922 FakeConstraints constraints;
923 constraints.SetAllowRtpDataChannels();
924 CreatePeerConnection(&constraints);
925
926 std::string label = "test";
927 webrtc::DataChannelInit config;
928 config.reliable = true;
929 scoped_refptr<DataChannelInterface> channel =
930 pc_->CreateDataChannel(label, &config);
931 EXPECT_TRUE(channel == NULL);
932}
933
934// This tests that a SCTP data channel is returned using different
935// DataChannelInit configurations.
936TEST_F(PeerConnectionInterfaceTest, CreateSctpDataChannel) {
937 FakeConstraints constraints;
938 constraints.SetAllowDtlsSctpDataChannels();
939 CreatePeerConnection(&constraints);
940
941 webrtc::DataChannelInit config;
942
943 scoped_refptr<DataChannelInterface> channel =
944 pc_->CreateDataChannel("1", &config);
945 EXPECT_TRUE(channel != NULL);
946 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000947 EXPECT_TRUE(observer_.renegotiation_needed_);
948 observer_.renegotiation_needed_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949
950 config.ordered = false;
951 channel = pc_->CreateDataChannel("2", &config);
952 EXPECT_TRUE(channel != NULL);
953 EXPECT_TRUE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000954 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955
956 config.ordered = true;
957 config.maxRetransmits = 0;
958 channel = pc_->CreateDataChannel("3", &config);
959 EXPECT_TRUE(channel != NULL);
960 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000961 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962
963 config.maxRetransmits = -1;
964 config.maxRetransmitTime = 0;
965 channel = pc_->CreateDataChannel("4", &config);
966 EXPECT_TRUE(channel != NULL);
967 EXPECT_FALSE(channel->reliable());
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +0000968 EXPECT_FALSE(observer_.renegotiation_needed_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969}
970
971// This tests that no data channel is returned if both maxRetransmits and
972// maxRetransmitTime are set for SCTP data channels.
973TEST_F(PeerConnectionInterfaceTest,
974 CreateSctpDataChannelShouldFailForInvalidConfig) {
975 FakeConstraints constraints;
976 constraints.SetAllowDtlsSctpDataChannels();
977 CreatePeerConnection(&constraints);
978
979 std::string label = "test";
980 webrtc::DataChannelInit config;
981 config.maxRetransmits = 0;
982 config.maxRetransmitTime = 0;
983
984 scoped_refptr<DataChannelInterface> channel =
985 pc_->CreateDataChannel(label, &config);
986 EXPECT_TRUE(channel == NULL);
987}
988
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989// The test verifies that creating a SCTP data channel with an id already in use
990// or out of range should fail.
991TEST_F(PeerConnectionInterfaceTest,
992 CreateSctpDataChannelWithInvalidIdShouldFail) {
993 FakeConstraints constraints;
994 constraints.SetAllowDtlsSctpDataChannels();
995 CreatePeerConnection(&constraints);
996
997 webrtc::DataChannelInit config;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000998 scoped_refptr<DataChannelInterface> channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001000 config.id = 1;
1001 channel = pc_->CreateDataChannel("1", &config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001002 EXPECT_TRUE(channel != NULL);
1003 EXPECT_EQ(1, channel->id());
1004
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001005 channel = pc_->CreateDataChannel("x", &config);
1006 EXPECT_TRUE(channel == NULL);
1007
1008 config.id = cricket::kMaxSctpSid;
1009 channel = pc_->CreateDataChannel("max", &config);
1010 EXPECT_TRUE(channel != NULL);
1011 EXPECT_EQ(config.id, channel->id());
1012
1013 config.id = cricket::kMaxSctpSid + 1;
1014 channel = pc_->CreateDataChannel("x", &config);
1015 EXPECT_TRUE(channel == NULL);
1016}
1017
jiayl@webrtc.org001fd2d2014-05-29 15:31:11 +00001018// This test verifies that OnRenegotiationNeeded is fired for every new RTP
1019// DataChannel.
1020TEST_F(PeerConnectionInterfaceTest, RenegotiationNeededForNewRtpDataChannel) {
1021 FakeConstraints constraints;
1022 constraints.SetAllowRtpDataChannels();
1023 CreatePeerConnection(&constraints);
1024
1025 scoped_refptr<DataChannelInterface> dc1 =
1026 pc_->CreateDataChannel("test1", NULL);
1027 EXPECT_TRUE(observer_.renegotiation_needed_);
1028 observer_.renegotiation_needed_ = false;
1029
1030 scoped_refptr<DataChannelInterface> dc2 =
1031 pc_->CreateDataChannel("test2", NULL);
1032 EXPECT_TRUE(observer_.renegotiation_needed_);
1033}
1034
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001035// This test that a data channel closes when a PeerConnection is deleted/closed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036TEST_F(PeerConnectionInterfaceTest, DataChannelCloseWhenPeerConnectionClose) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001037 FakeConstraints constraints;
1038 constraints.SetAllowRtpDataChannels();
1039 CreatePeerConnection(&constraints);
1040
1041 scoped_refptr<DataChannelInterface> data1 =
1042 pc_->CreateDataChannel("test1", NULL);
1043 scoped_refptr<DataChannelInterface> data2 =
1044 pc_->CreateDataChannel("test2", NULL);
1045 ASSERT_TRUE(data1 != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001046 rtc::scoped_ptr<MockDataChannelObserver> observer1(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 new MockDataChannelObserver(data1));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001048 rtc::scoped_ptr<MockDataChannelObserver> observer2(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049 new MockDataChannelObserver(data2));
1050
1051 CreateOfferReceiveAnswer();
1052 EXPECT_TRUE_WAIT(observer1->IsOpen(), kTimeout);
1053 EXPECT_TRUE_WAIT(observer2->IsOpen(), kTimeout);
1054
1055 ReleasePeerConnection();
1056 EXPECT_EQ(DataChannelInterface::kClosed, data1->state());
1057 EXPECT_EQ(DataChannelInterface::kClosed, data2->state());
1058}
1059
1060// This test that data channels can be rejected in an answer.
1061TEST_F(PeerConnectionInterfaceTest, TestRejectDataChannelInAnswer) {
1062 FakeConstraints constraints;
1063 constraints.SetAllowRtpDataChannels();
1064 CreatePeerConnection(&constraints);
1065
1066 scoped_refptr<DataChannelInterface> offer_channel(
1067 pc_->CreateDataChannel("offer_channel", NULL));
1068
1069 CreateOfferAsLocalDescription();
1070
1071 // Create an answer where the m-line for data channels are rejected.
1072 std::string sdp;
1073 EXPECT_TRUE(pc_->local_description()->ToString(&sdp));
1074 webrtc::JsepSessionDescription* answer = new webrtc::JsepSessionDescription(
1075 SessionDescriptionInterface::kAnswer);
1076 EXPECT_TRUE(answer->Initialize(sdp, NULL));
1077 cricket::ContentInfo* data_info =
1078 answer->description()->GetContentByName("data");
1079 data_info->rejected = true;
1080
1081 DoSetRemoteDescription(answer);
1082 EXPECT_EQ(DataChannelInterface::kClosed, offer_channel->state());
1083}
1084
1085// Test that we can create a session description from an SDP string from
1086// FireFox, use it as a remote session description, generate an answer and use
1087// the answer as a local description.
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001088TEST_F(PeerConnectionInterfaceTest, ReceiveFireFoxOffer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001089 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001090 FakeConstraints constraints;
1091 constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
1092 true);
1093 CreatePeerConnection(&constraints);
1094 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1095 SessionDescriptionInterface* desc =
1096 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1097 webrtc::kFireFoxSdpOffer);
1098 EXPECT_TRUE(DoSetSessionDescription(desc, false));
1099 CreateAnswerAsLocalDescription();
1100 ASSERT_TRUE(pc_->local_description() != NULL);
1101 ASSERT_TRUE(pc_->remote_description() != NULL);
1102
1103 const cricket::ContentInfo* content =
1104 cricket::GetFirstAudioContent(pc_->local_description()->description());
1105 ASSERT_TRUE(content != NULL);
1106 EXPECT_FALSE(content->rejected);
1107
1108 content =
1109 cricket::GetFirstVideoContent(pc_->local_description()->description());
1110 ASSERT_TRUE(content != NULL);
1111 EXPECT_FALSE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001112#ifdef HAVE_SCTP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001113 content =
1114 cricket::GetFirstDataContent(pc_->local_description()->description());
1115 ASSERT_TRUE(content != NULL);
1116 EXPECT_TRUE(content->rejected);
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00001117#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118}
1119
1120// Test that we can create an audio only offer and receive an answer with a
1121// limited set of audio codecs and receive an updated offer with more audio
1122// codecs, where the added codecs are not supported.
1123TEST_F(PeerConnectionInterfaceTest, ReceiveUpdatedAudioOfferWithBadCodecs) {
1124 CreatePeerConnection();
1125 AddVoiceStream("audio_label");
1126 CreateOfferAsLocalDescription();
1127
1128 SessionDescriptionInterface* answer =
1129 webrtc::CreateSessionDescription(SessionDescriptionInterface::kAnswer,
1130 webrtc::kAudioSdp);
1131 EXPECT_TRUE(DoSetSessionDescription(answer, false));
1132
1133 SessionDescriptionInterface* updated_offer =
1134 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1135 webrtc::kAudioSdpWithUnsupportedCodecs);
1136 EXPECT_TRUE(DoSetSessionDescription(updated_offer, false));
1137 CreateAnswerAsLocalDescription();
1138}
1139
1140// Test that PeerConnection::Close changes the states to closed and all remote
1141// tracks change state to ended.
1142TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
1143 // Initialize a PeerConnection and negotiate local and remote session
1144 // description.
1145 InitiateCall();
1146 ASSERT_EQ(1u, pc_->local_streams()->count());
1147 ASSERT_EQ(1u, pc_->remote_streams()->count());
1148
1149 pc_->Close();
1150
1151 EXPECT_EQ(PeerConnectionInterface::kClosed, pc_->signaling_state());
1152 EXPECT_EQ(PeerConnectionInterface::kIceConnectionClosed,
1153 pc_->ice_connection_state());
1154 EXPECT_EQ(PeerConnectionInterface::kIceGatheringComplete,
1155 pc_->ice_gathering_state());
1156
1157 EXPECT_EQ(1u, pc_->local_streams()->count());
1158 EXPECT_EQ(1u, pc_->remote_streams()->count());
1159
1160 scoped_refptr<MediaStreamInterface> remote_stream =
1161 pc_->remote_streams()->at(0);
1162 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1163 remote_stream->GetVideoTracks()[0]->state());
1164 EXPECT_EQ(MediaStreamTrackInterface::kEnded,
1165 remote_stream->GetAudioTracks()[0]->state());
1166}
1167
1168// Test that PeerConnection methods fails gracefully after
1169// PeerConnection::Close has been called.
1170TEST_F(PeerConnectionInterfaceTest, CloseAndTestMethods) {
1171 CreatePeerConnection();
1172 AddAudioVideoStream(kStreamLabel1, "audio_label", "video_label");
1173 CreateOfferAsRemoteDescription();
1174 CreateAnswerAsLocalDescription();
1175
1176 ASSERT_EQ(1u, pc_->local_streams()->count());
1177 scoped_refptr<MediaStreamInterface> local_stream =
1178 pc_->local_streams()->at(0);
1179
1180 pc_->Close();
1181
1182 pc_->RemoveStream(local_stream);
1183 EXPECT_FALSE(pc_->AddStream(local_stream, NULL));
1184
1185 ASSERT_FALSE(local_stream->GetAudioTracks().empty());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001186 rtc::scoped_refptr<webrtc::DtmfSenderInterface> dtmf_sender(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187 pc_->CreateDtmfSender(local_stream->GetAudioTracks()[0]));
wu@webrtc.org66037362013-08-13 00:09:35 +00001188 EXPECT_TRUE(NULL == dtmf_sender); // local stream has been removed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189
1190 EXPECT_TRUE(pc_->CreateDataChannel("test", NULL) == NULL);
1191
1192 EXPECT_TRUE(pc_->local_description() != NULL);
1193 EXPECT_TRUE(pc_->remote_description() != NULL);
1194
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001195 rtc::scoped_ptr<SessionDescriptionInterface> offer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001196 EXPECT_TRUE(DoCreateOffer(offer.use()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001197 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198 EXPECT_TRUE(DoCreateAnswer(answer.use()));
1199
1200 std::string sdp;
1201 ASSERT_TRUE(pc_->remote_description()->ToString(&sdp));
1202 SessionDescriptionInterface* remote_offer =
1203 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1204 sdp, NULL);
1205 EXPECT_FALSE(DoSetRemoteDescription(remote_offer));
1206
1207 ASSERT_TRUE(pc_->local_description()->ToString(&sdp));
1208 SessionDescriptionInterface* local_offer =
1209 webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
1210 sdp, NULL);
1211 EXPECT_FALSE(DoSetLocalDescription(local_offer));
1212}
1213
1214// Test that GetStats can still be called after PeerConnection::Close.
1215TEST_F(PeerConnectionInterfaceTest, CloseAndGetStats) {
1216 InitiateCall();
1217 pc_->Close();
1218 DoGetStats(NULL);
1219}