blob: 4a544c48256cca9cae27ae1c852eed54f56adf7c [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
11#include <stdio.h>
12
13#include <algorithm>
14#include <list>
15#include <map>
kwiberg0eb15ed2015-12-17 03:04:15 -080016#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include <vector>
18
Henrik Kjellander15583c12016-02-10 10:53:12 +010019#include "webrtc/api/dtmfsender.h"
20#include "webrtc/api/fakemetricsobserver.h"
21#include "webrtc/api/localaudiosource.h"
22#include "webrtc/api/mediastreaminterface.h"
23#include "webrtc/api/peerconnection.h"
24#include "webrtc/api/peerconnectionfactory.h"
25#include "webrtc/api/peerconnectioninterface.h"
26#include "webrtc/api/test/fakeaudiocapturemodule.h"
27#include "webrtc/api/test/fakeconstraints.h"
28#include "webrtc/api/test/fakedtlsidentitystore.h"
29#include "webrtc/api/test/fakeperiodicvideocapturer.h"
30#include "webrtc/api/test/fakevideotrackrenderer.h"
31#include "webrtc/api/test/mockpeerconnectionobservers.h"
32#include "webrtc/api/videosourceinterface.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000033#include "webrtc/base/gunit.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000034#include "webrtc/base/physicalsocketserver.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000035#include "webrtc/base/scoped_ptr.h"
36#include "webrtc/base/ssladapter.h"
37#include "webrtc/base/sslstreamadapter.h"
38#include "webrtc/base/thread.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000039#include "webrtc/base/virtualsocketserver.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010040#include "webrtc/media/engine/fakewebrtcvideoengine.h"
kjellanderf4752772016-03-02 05:42:30 -080041#include "webrtc/p2p/base/p2pconstants.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000042#include "webrtc/p2p/base/sessiondescription.h"
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080043#include "webrtc/p2p/client/fakeportallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010044#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045
46#define MAYBE_SKIP_TEST(feature) \
47 if (!(feature())) { \
48 LOG(LS_INFO) << "Feature disabled... skipping"; \
49 return; \
50 }
51
52using cricket::ContentInfo;
53using cricket::FakeWebRtcVideoDecoder;
54using cricket::FakeWebRtcVideoDecoderFactory;
55using cricket::FakeWebRtcVideoEncoder;
56using cricket::FakeWebRtcVideoEncoderFactory;
57using cricket::MediaContentDescription;
58using webrtc::DataBuffer;
59using webrtc::DataChannelInterface;
60using webrtc::DtmfSender;
61using webrtc::DtmfSenderInterface;
62using webrtc::DtmfSenderObserverInterface;
63using webrtc::FakeConstraints;
64using webrtc::MediaConstraintsInterface;
deadbeeffaac4972015-11-12 15:33:07 -080065using webrtc::MediaStreamInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066using webrtc::MediaStreamTrackInterface;
67using webrtc::MockCreateSessionDescriptionObserver;
68using webrtc::MockDataChannelObserver;
69using webrtc::MockSetSessionDescriptionObserver;
70using webrtc::MockStatsObserver;
deadbeeffaac4972015-11-12 15:33:07 -080071using webrtc::ObserverInterface;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000072using webrtc::PeerConnectionInterface;
Joachim Bauch04e5b492015-05-29 09:40:39 +020073using webrtc::PeerConnectionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074using webrtc::SessionDescriptionInterface;
75using webrtc::StreamCollectionInterface;
76
hta6b4f8392016-03-10 00:24:31 -080077namespace {
78
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000079static const int kMaxWaitMs = 10000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000080// Disable for TSan v2, see
81// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
82// This declaration is also #ifdef'd as it causes uninitialized-variable
83// warnings.
84#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085static const int kMaxWaitForStatsMs = 3000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000086#endif
deadbeeffac06552015-11-25 11:26:01 -080087static const int kMaxWaitForActivationMs = 5000;
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000088static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089static const int kEndAudioFrameCount = 3;
90static const int kEndVideoFrameCount = 3;
91
92static const char kStreamLabelBase[] = "stream_label";
93static const char kVideoTrackLabelBase[] = "video_track";
94static const char kAudioTrackLabelBase[] = "audio_track";
95static const char kDataChannelLabel[] = "data_channel";
96
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +000097// Disable for TSan v2, see
98// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
99// This declaration is also #ifdef'd as it causes unused-variable errors.
100#if !defined(THREAD_SANITIZER)
101// SRTP cipher name negotiated by the tests. This must be updated if the
102// default changes.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800103static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000104#endif
105
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106static void RemoveLinesFromSdp(const std::string& line_start,
107 std::string* sdp) {
108 const char kSdpLineEnd[] = "\r\n";
109 size_t ssrc_pos = 0;
110 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
111 std::string::npos) {
112 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
113 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
114 }
115}
116
hta6b4f8392016-03-10 00:24:31 -0800117bool StreamsHaveAudioTrack(StreamCollectionInterface* streams) {
118 for (size_t idx = 0; idx < streams->count(); idx++) {
119 auto stream = streams->at(idx);
120 if (stream->GetAudioTracks().size() > 0) {
121 return true;
122 }
123 }
124 return false;
125}
126
127bool StreamsHaveVideoTrack(StreamCollectionInterface* streams) {
128 for (size_t idx = 0; idx < streams->count(); idx++) {
129 auto stream = streams->at(idx);
130 if (stream->GetVideoTracks().size() > 0) {
131 return true;
132 }
133 }
134 return false;
135}
136
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137class SignalingMessageReceiver {
138 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 virtual void ReceiveSdpMessage(const std::string& type,
140 std::string& msg) = 0;
141 virtual void ReceiveIceMessage(const std::string& sdp_mid,
142 int sdp_mline_index,
143 const std::string& msg) = 0;
144
145 protected:
deadbeefaf1b59c2015-10-15 12:08:41 -0700146 SignalingMessageReceiver() {}
147 virtual ~SignalingMessageReceiver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148};
149
deadbeefaf1b59c2015-10-15 12:08:41 -0700150class PeerConnectionTestClient : public webrtc::PeerConnectionObserver,
deadbeeffaac4972015-11-12 15:33:07 -0800151 public SignalingMessageReceiver,
152 public ObserverInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 public:
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800154 static PeerConnectionTestClient* CreateClientWithDtlsIdentityStore(
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800155 const std::string& id,
156 const MediaConstraintsInterface* constraints,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800157 const PeerConnectionFactory::Options* options,
158 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800159 PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
kwiberg0eb15ed2015-12-17 03:04:15 -0800160 if (!client->Init(constraints, options, std::move(dtls_identity_store))) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800161 delete client;
162 return nullptr;
163 }
164 return client;
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800165 }
166
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800167 static PeerConnectionTestClient* CreateClient(
168 const std::string& id,
169 const MediaConstraintsInterface* constraints,
170 const PeerConnectionFactory::Options* options) {
171 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
172 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
173 : nullptr);
174
175 return CreateClientWithDtlsIdentityStore(id, constraints, options,
kwiberg0eb15ed2015-12-17 03:04:15 -0800176 std::move(dtls_identity_store));
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800177 }
178
deadbeefaf1b59c2015-10-15 12:08:41 -0700179 ~PeerConnectionTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 }
181
deadbeefaf1b59c2015-10-15 12:08:41 -0700182 void Negotiate() { Negotiate(true, true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183
deadbeefaf1b59c2015-10-15 12:08:41 -0700184 void Negotiate(bool audio, bool video) {
185 rtc::scoped_ptr<SessionDescriptionInterface> offer;
186 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187
deadbeefaf1b59c2015-10-15 12:08:41 -0700188 if (offer->description()->GetContentByName("audio")) {
189 offer->description()->GetContentByName("audio")->rejected = !audio;
190 }
191 if (offer->description()->GetContentByName("video")) {
192 offer->description()->GetContentByName("video")->rejected = !video;
193 }
194
195 std::string sdp;
196 EXPECT_TRUE(offer->ToString(&sdp));
197 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
198 signaling_message_receiver_->ReceiveSdpMessage(
199 webrtc::SessionDescriptionInterface::kOffer, sdp);
200 }
201
202 // SignalingMessageReceiver callback.
203 void ReceiveSdpMessage(const std::string& type, std::string& msg) override {
204 FilterIncomingSdpMessage(&msg);
205 if (type == webrtc::SessionDescriptionInterface::kOffer) {
206 HandleIncomingOffer(msg);
207 } else {
208 HandleIncomingAnswer(msg);
209 }
210 }
211
212 // SignalingMessageReceiver callback.
213 void ReceiveIceMessage(const std::string& sdp_mid,
214 int sdp_mline_index,
215 const std::string& msg) override {
216 LOG(INFO) << id_ << "ReceiveIceMessage";
217 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
218 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
219 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
220 }
221
222 // PeerConnectionObserver callbacks.
223 void OnSignalingChange(
224 webrtc::PeerConnectionInterface::SignalingState new_state) override {
225 EXPECT_EQ(pc()->signaling_state(), new_state);
226 }
deadbeeffaac4972015-11-12 15:33:07 -0800227 void OnAddStream(MediaStreamInterface* media_stream) override {
228 media_stream->RegisterObserver(this);
deadbeefaf1b59c2015-10-15 12:08:41 -0700229 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
230 const std::string id = media_stream->GetVideoTracks()[i]->id();
231 ASSERT_TRUE(fake_video_renderers_.find(id) ==
232 fake_video_renderers_.end());
deadbeefc9be0072015-12-14 18:27:57 -0800233 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
234 media_stream->GetVideoTracks()[i]));
deadbeefaf1b59c2015-10-15 12:08:41 -0700235 }
236 }
deadbeeffaac4972015-11-12 15:33:07 -0800237 void OnRemoveStream(MediaStreamInterface* media_stream) override {}
deadbeefaf1b59c2015-10-15 12:08:41 -0700238 void OnRenegotiationNeeded() override {}
239 void OnIceConnectionChange(
240 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
241 EXPECT_EQ(pc()->ice_connection_state(), new_state);
242 }
243 void OnIceGatheringChange(
244 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
245 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
246 }
247 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
248 LOG(INFO) << id_ << "OnIceCandidate";
249
250 std::string ice_sdp;
251 EXPECT_TRUE(candidate->ToString(&ice_sdp));
252 if (signaling_message_receiver_ == nullptr) {
253 // Remote party may be deleted.
254 return;
255 }
256 signaling_message_receiver_->ReceiveIceMessage(
257 candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
258 }
259
deadbeeffaac4972015-11-12 15:33:07 -0800260 // MediaStreamInterface callback
261 void OnChanged() override {
262 // Track added or removed from MediaStream, so update our renderers.
263 rtc::scoped_refptr<StreamCollectionInterface> remote_streams =
264 pc()->remote_streams();
265 // Remove renderers for tracks that were removed.
266 for (auto it = fake_video_renderers_.begin();
267 it != fake_video_renderers_.end();) {
268 if (remote_streams->FindVideoTrack(it->first) == nullptr) {
deadbeefc9be0072015-12-14 18:27:57 -0800269 auto to_remove = it++;
270 removed_fake_video_renderers_.push_back(std::move(to_remove->second));
271 fake_video_renderers_.erase(to_remove);
deadbeeffaac4972015-11-12 15:33:07 -0800272 } else {
273 ++it;
274 }
275 }
276 // Create renderers for new video tracks.
277 for (size_t stream_index = 0; stream_index < remote_streams->count();
278 ++stream_index) {
279 MediaStreamInterface* remote_stream = remote_streams->at(stream_index);
280 for (size_t track_index = 0;
281 track_index < remote_stream->GetVideoTracks().size();
282 ++track_index) {
283 const std::string id =
284 remote_stream->GetVideoTracks()[track_index]->id();
285 if (fake_video_renderers_.find(id) != fake_video_renderers_.end()) {
286 continue;
287 }
deadbeefc9be0072015-12-14 18:27:57 -0800288 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
289 remote_stream->GetVideoTracks()[track_index]));
deadbeeffaac4972015-11-12 15:33:07 -0800290 }
291 }
292 }
293
deadbeefaf1b59c2015-10-15 12:08:41 -0700294 void SetVideoConstraints(const webrtc::FakeConstraints& video_constraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 video_constraints_ = video_constraint;
296 }
297
298 void AddMediaStream(bool audio, bool video) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700299 std::string stream_label =
300 kStreamLabelBase +
301 rtc::ToString<int>(static_cast<int>(pc()->local_streams()->count()));
deadbeeffaac4972015-11-12 15:33:07 -0800302 rtc::scoped_refptr<MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000303 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000304
305 if (audio && can_receive_audio()) {
deadbeeffac06552015-11-25 11:26:01 -0800306 stream->AddTrack(CreateLocalAudioTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307 }
308 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000309 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310 }
311
deadbeefaf1b59c2015-10-15 12:08:41 -0700312 EXPECT_TRUE(pc()->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313 }
314
deadbeefaf1b59c2015-10-15 12:08:41 -0700315 size_t NumberOfLocalMediaStreams() { return pc()->local_streams()->count(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316
317 bool SessionActive() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700318 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319 }
320
deadbeeffaac4972015-11-12 15:33:07 -0800321 // Automatically add a stream when receiving an offer, if we don't have one.
322 // Defaults to true.
323 void set_auto_add_stream(bool auto_add_stream) {
324 auto_add_stream_ = auto_add_stream;
325 }
326
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 void set_signaling_message_receiver(
deadbeefaf1b59c2015-10-15 12:08:41 -0700328 SignalingMessageReceiver* signaling_message_receiver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329 signaling_message_receiver_ = signaling_message_receiver;
330 }
331
332 void EnableVideoDecoderFactory() {
333 video_decoder_factory_enabled_ = true;
334 fake_video_decoder_factory_->AddSupportedVideoCodecType(
335 webrtc::kVideoCodecVP8);
336 }
337
deadbeefaf1b59c2015-10-15 12:08:41 -0700338 void IceRestart() {
339 session_description_constraints_.SetMandatoryIceRestart(true);
340 SetExpectIceRestart(true);
341 }
342
343 void SetExpectIceRestart(bool expect_restart) {
344 expect_ice_restart_ = expect_restart;
345 }
346
347 bool ExpectIceRestart() const { return expect_ice_restart_; }
348
349 void SetReceiveAudioVideo(bool audio, bool video) {
350 SetReceiveAudio(audio);
351 SetReceiveVideo(video);
352 ASSERT_EQ(audio, can_receive_audio());
353 ASSERT_EQ(video, can_receive_video());
354 }
355
356 void SetReceiveAudio(bool audio) {
357 if (audio && can_receive_audio())
358 return;
359 session_description_constraints_.SetMandatoryReceiveAudio(audio);
360 }
361
362 void SetReceiveVideo(bool video) {
363 if (video && can_receive_video())
364 return;
365 session_description_constraints_.SetMandatoryReceiveVideo(video);
366 }
367
368 void RemoveMsidFromReceivedSdp(bool remove) { remove_msid_ = remove; }
369
370 void RemoveSdesCryptoFromReceivedSdp(bool remove) { remove_sdes_ = remove; }
371
372 void RemoveBundleFromReceivedSdp(bool remove) { remove_bundle_ = remove; }
373
374 bool can_receive_audio() {
375 bool value;
376 if (webrtc::FindConstraint(&session_description_constraints_,
377 MediaConstraintsInterface::kOfferToReceiveAudio,
378 &value, nullptr)) {
379 return value;
380 }
381 return true;
382 }
383
384 bool can_receive_video() {
385 bool value;
386 if (webrtc::FindConstraint(&session_description_constraints_,
387 MediaConstraintsInterface::kOfferToReceiveVideo,
388 &value, nullptr)) {
389 return value;
390 }
391 return true;
392 }
393
deadbeefaf1b59c2015-10-15 12:08:41 -0700394 void OnDataChannel(DataChannelInterface* data_channel) override {
395 LOG(INFO) << id_ << "OnDataChannel";
396 data_channel_ = data_channel;
397 data_observer_.reset(new MockDataChannelObserver(data_channel));
398 }
399
400 void CreateDataChannel() {
401 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, nullptr);
402 ASSERT_TRUE(data_channel_.get() != nullptr);
403 data_observer_.reset(new MockDataChannelObserver(data_channel_));
404 }
405
deadbeeffac06552015-11-25 11:26:01 -0800406 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack(
407 const std::string& stream_label) {
408 FakeConstraints constraints;
409 // Disable highpass filter so that we can get all the test audio frames.
410 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
411 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
412 peer_connection_factory_->CreateAudioSource(&constraints);
413 // TODO(perkj): Test audio source when it is implemented. Currently audio
414 // always use the default input.
415 std::string label = stream_label + kAudioTrackLabelBase;
416 return peer_connection_factory_->CreateAudioTrack(label, source);
417 }
418
419 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack(
420 const std::string& stream_label) {
421 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
422 FakeConstraints source_constraints = video_constraints_;
423 source_constraints.SetMandatoryMaxFrameRate(10);
424
425 cricket::FakeVideoCapturer* fake_capturer =
426 new webrtc::FakePeriodicVideoCapturer();
427 video_capturers_.push_back(fake_capturer);
perkja3ede6c2016-03-08 01:27:48 +0100428 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
deadbeeffac06552015-11-25 11:26:01 -0800429 peer_connection_factory_->CreateVideoSource(fake_capturer,
430 &source_constraints);
431 std::string label = stream_label + kVideoTrackLabelBase;
432 return peer_connection_factory_->CreateVideoTrack(label, source);
433 }
434
deadbeefaf1b59c2015-10-15 12:08:41 -0700435 DataChannelInterface* data_channel() { return data_channel_; }
436 const MockDataChannelObserver* data_observer() const {
437 return data_observer_.get();
438 }
439
hta6b4f8392016-03-10 00:24:31 -0800440 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
deadbeefaf1b59c2015-10-15 12:08:41 -0700441
442 void StopVideoCapturers() {
443 for (std::vector<cricket::VideoCapturer*>::iterator it =
444 video_capturers_.begin();
445 it != video_capturers_.end(); ++it) {
446 (*it)->Stop();
447 }
448 }
449
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450 bool AudioFramesReceivedCheck(int number_of_frames) const {
451 return number_of_frames <= fake_audio_capture_module_->frames_received();
452 }
453
deadbeefc9be0072015-12-14 18:27:57 -0800454 int audio_frames_received() const {
455 return fake_audio_capture_module_->frames_received();
456 }
457
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458 bool VideoFramesReceivedCheck(int number_of_frames) {
459 if (video_decoder_factory_enabled_) {
460 const std::vector<FakeWebRtcVideoDecoder*>& decoders
461 = fake_video_decoder_factory_->decoders();
462 if (decoders.empty()) {
463 return number_of_frames <= 0;
464 }
hta6b4f8392016-03-10 00:24:31 -0800465 // Note - this checks that EACH decoder has the requisite number
466 // of frames. The video_frames_received() function sums them.
deadbeefc9be0072015-12-14 18:27:57 -0800467 for (FakeWebRtcVideoDecoder* decoder : decoders) {
468 if (number_of_frames > decoder->GetNumFramesReceived()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469 return false;
470 }
471 }
472 return true;
473 } else {
474 if (fake_video_renderers_.empty()) {
475 return number_of_frames <= 0;
476 }
477
deadbeefc9be0072015-12-14 18:27:57 -0800478 for (const auto& pair : fake_video_renderers_) {
479 if (number_of_frames > pair.second->num_rendered_frames()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 return false;
481 }
482 }
483 return true;
484 }
485 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700486
deadbeefc9be0072015-12-14 18:27:57 -0800487 int video_frames_received() const {
488 int total = 0;
489 if (video_decoder_factory_enabled_) {
490 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
491 fake_video_decoder_factory_->decoders();
492 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
493 total += decoder->GetNumFramesReceived();
494 }
495 } else {
496 for (const auto& pair : fake_video_renderers_) {
497 total += pair.second->num_rendered_frames();
498 }
499 for (const auto& renderer : removed_fake_video_renderers_) {
500 total += renderer->num_rendered_frames();
501 }
502 }
503 return total;
504 }
505
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506 // Verify the CreateDtmfSender interface
507 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000508 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
509 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510
511 // We can't create a DTMF sender with an invalid audio track or a non local
512 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700513 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000514 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700515 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
516 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517
518 // We should be able to create a DTMF sender from a local track.
519 webrtc::AudioTrackInterface* localtrack =
520 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
521 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700522 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 dtmf_sender->RegisterObserver(observer.get());
524
525 // Test the DtmfSender object just created.
526 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
527 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
528
529 // We don't need to verify that the DTMF tones are actually sent out because
530 // that is already covered by the tests of the lower level components.
531
532 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
533 std::vector<std::string> tones;
534 tones.push_back("1");
535 tones.push_back("a");
536 tones.push_back("");
537 observer->Verify(tones);
538
539 dtmf_sender->UnregisterObserver();
540 }
541
542 // Verifies that the SessionDescription have rejected the appropriate media
543 // content.
544 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700545 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
546 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 const cricket::SessionDescription* remote_desc =
548 peer_connection_->remote_description()->description();
549 const cricket::SessionDescription* local_desc =
550 peer_connection_->local_description()->description();
551
552 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
553 if (remote_audio_content) {
554 const ContentInfo* audio_content =
555 GetFirstAudioContent(local_desc);
556 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
557 }
558
559 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
560 if (remote_video_content) {
561 const ContentInfo* video_content =
562 GetFirstVideoContent(local_desc);
563 EXPECT_EQ(can_receive_video(), !video_content->rejected);
564 }
565 }
566
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700568 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 const cricket::SessionDescription* desc =
570 peer_connection_->local_description()->description();
571 const cricket::ContentInfos& contents = desc->contents();
572
573 for (size_t index = 0; index < contents.size(); ++index) {
574 if (contents[index].rejected)
575 continue;
576 const cricket::TransportDescription* transport_desc =
577 desc->GetTransportDescriptionByName(contents[index].name);
578
579 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000580 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 if (ufragpair_it == ice_ufrag_pwd_.end()) {
582 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000583 ice_ufrag_pwd_[static_cast<int>(index)] =
584 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585 } else if (ExpectIceRestart()) {
586 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
587 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
588 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
589 } else {
590 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
591 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
592 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
593 }
594 }
595 }
596
597 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000598 rtc::scoped_refptr<MockStatsObserver>
599 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000600 EXPECT_TRUE(peer_connection_->GetStats(
601 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700603 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 return observer->AudioOutputLevel();
605 }
606
607 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000608 rtc::scoped_refptr<MockStatsObserver>
609 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000610 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700611 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700613 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 return observer->AudioInputLevel();
615 }
616
617 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000618 rtc::scoped_refptr<MockStatsObserver>
619 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000620 EXPECT_TRUE(peer_connection_->GetStats(
621 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700623 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624 return observer->BytesReceived();
625 }
626
627 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000628 rtc::scoped_refptr<MockStatsObserver>
629 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000630 EXPECT_TRUE(peer_connection_->GetStats(
631 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700633 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634 return observer->BytesSent();
635 }
636
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000637 int GetAvailableReceivedBandwidthStats() {
638 rtc::scoped_refptr<MockStatsObserver>
639 observer(new rtc::RefCountedObject<MockStatsObserver>());
640 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700641 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000642 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700643 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000644 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000645 return bw;
646 }
647
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000648 std::string GetDtlsCipherStats() {
649 rtc::scoped_refptr<MockStatsObserver>
650 observer(new rtc::RefCountedObject<MockStatsObserver>());
651 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700652 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000653 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700654 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000655 return observer->DtlsCipher();
656 }
657
658 std::string GetSrtpCipherStats() {
659 rtc::scoped_refptr<MockStatsObserver>
660 observer(new rtc::RefCountedObject<MockStatsObserver>());
661 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700662 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000663 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700664 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000665 return observer->SrtpCipher();
666 }
667
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 int rendered_width() {
669 EXPECT_FALSE(fake_video_renderers_.empty());
670 return fake_video_renderers_.empty() ? 1 :
671 fake_video_renderers_.begin()->second->width();
672 }
673
674 int rendered_height() {
675 EXPECT_FALSE(fake_video_renderers_.empty());
676 return fake_video_renderers_.empty() ? 1 :
677 fake_video_renderers_.begin()->second->height();
678 }
679
680 size_t number_of_remote_streams() {
681 if (!pc())
682 return 0;
683 return pc()->remote_streams()->count();
684 }
685
hta6b4f8392016-03-10 00:24:31 -0800686 StreamCollectionInterface* remote_streams() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 if (!pc()) {
688 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700689 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690 }
691 return pc()->remote_streams();
692 }
693
694 StreamCollectionInterface* local_streams() {
695 if (!pc()) {
696 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700697 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 }
699 return pc()->local_streams();
700 }
701
hta6b4f8392016-03-10 00:24:31 -0800702 bool HasLocalAudioTrack() { return StreamsHaveAudioTrack(local_streams()); }
703
704 bool HasLocalVideoTrack() { return StreamsHaveVideoTrack(local_streams()); }
705
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
707 return pc()->signaling_state();
708 }
709
710 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
711 return pc()->ice_connection_state();
712 }
713
714 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
715 return pc()->ice_gathering_state();
716 }
717
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 private:
719 class DummyDtmfObserver : public DtmfSenderObserverInterface {
720 public:
721 DummyDtmfObserver() : completed_(false) {}
722
723 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700724 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 tones_.push_back(tone);
726 if (tone.empty()) {
727 completed_ = true;
728 }
729 }
730
731 void Verify(const std::vector<std::string>& tones) const {
732 ASSERT_TRUE(tones_.size() == tones.size());
733 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
734 }
735
736 bool completed() const { return completed_; }
737
738 private:
739 bool completed_;
740 std::vector<std::string> tones_;
741 };
742
deadbeefaf1b59c2015-10-15 12:08:41 -0700743 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
744
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800745 bool Init(
746 const MediaConstraintsInterface* constraints,
747 const PeerConnectionFactory::Options* options,
748 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700749 EXPECT_TRUE(!peer_connection_);
750 EXPECT_TRUE(!peer_connection_factory_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800751 rtc::scoped_ptr<cricket::PortAllocator> port_allocator(
752 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
deadbeefaf1b59c2015-10-15 12:08:41 -0700753 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
754
755 if (fake_audio_capture_module_ == nullptr) {
756 return false;
757 }
758 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
759 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
760 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
761 rtc::Thread::Current(), rtc::Thread::Current(),
762 fake_audio_capture_module_, fake_video_encoder_factory_,
763 fake_video_decoder_factory_);
764 if (!peer_connection_factory_) {
765 return false;
766 }
767 if (options) {
768 peer_connection_factory_->SetOptions(*options);
769 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800770 peer_connection_ = CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800771 std::move(port_allocator), constraints, std::move(dtls_identity_store));
deadbeefaf1b59c2015-10-15 12:08:41 -0700772 return peer_connection_.get() != nullptr;
773 }
774
deadbeefaf1b59c2015-10-15 12:08:41 -0700775 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800776 rtc::scoped_ptr<cricket::PortAllocator> port_allocator,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800777 const MediaConstraintsInterface* constraints,
778 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800779 // CreatePeerConnection with RTCConfiguration.
780 webrtc::PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 webrtc::PeerConnectionInterface::IceServer ice_server;
782 ice_server.uri = "stun:stun.l.google.com:19302";
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800783 config.servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000784
deadbeefaf1b59c2015-10-15 12:08:41 -0700785 return peer_connection_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800786 config, constraints, std::move(port_allocator),
787 std::move(dtls_identity_store), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 }
789
790 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700791 LOG(INFO) << id_ << "HandleIncomingOffer ";
deadbeeffaac4972015-11-12 15:33:07 -0800792 if (NumberOfLocalMediaStreams() == 0 && auto_add_stream_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 // If we are not sending any streams ourselves it is time to add some.
794 AddMediaStream(true, true);
795 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000796 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700797 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000799 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 EXPECT_TRUE(DoCreateAnswer(answer.use()));
801 std::string sdp;
802 EXPECT_TRUE(answer->ToString(&sdp));
803 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
deadbeefaf1b59c2015-10-15 12:08:41 -0700804 if (signaling_message_receiver_) {
805 signaling_message_receiver_->ReceiveSdpMessage(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 webrtc::SessionDescriptionInterface::kAnswer, sdp);
807 }
808 }
809
810 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700811 LOG(INFO) << id_ << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000812 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700813 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
815 }
816
817 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
818 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000819 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
820 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 MockCreateSessionDescriptionObserver>());
822 if (offer) {
823 pc()->CreateOffer(observer, &session_description_constraints_);
824 } else {
825 pc()->CreateAnswer(observer, &session_description_constraints_);
826 }
827 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
828 *desc = observer->release_desc();
829 if (observer->result() && ExpectIceRestart()) {
830 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
831 }
832 return observer->result();
833 }
834
835 bool DoCreateOffer(SessionDescriptionInterface** desc) {
836 return DoCreateOfferAnswer(desc, true);
837 }
838
839 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
840 return DoCreateOfferAnswer(desc, false);
841 }
842
843 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000844 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
845 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700847 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 pc()->SetLocalDescription(observer, desc);
849 // Ignore the observer result. If we wait for the result with
850 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
851 // before the offer which is an error.
852 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000853 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 // ProcessMessages waits at least 1ms but processes all messages before
855 // returning. Since this test is synchronous and send messages to the remote
856 // peer whenever a callback is invoked, this can lead to messages being
857 // sent to the remote peer in the wrong order.
858 // TODO(perkj): Find a way to check the result without risking that the
859 // order of sent messages are changed. Ex- by posting all messages that are
860 // sent to the remote peer.
861 return true;
862 }
863
864 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000865 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
866 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700868 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 pc()->SetRemoteDescription(observer, desc);
870 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
871 return observer->result();
872 }
873
874 // This modifies all received SDP messages before they are processed.
875 void FilterIncomingSdpMessage(std::string* sdp) {
876 if (remove_msid_) {
877 const char kSdpSsrcAttribute[] = "a=ssrc:";
878 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
879 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
880 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
881 }
882 if (remove_bundle_) {
883 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
884 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
885 }
886 if (remove_sdes_) {
887 const char kSdpSdesCryptoAttribute[] = "a=crypto";
888 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
889 }
890 }
891
deadbeefaf1b59c2015-10-15 12:08:41 -0700892 std::string id_;
893
deadbeefaf1b59c2015-10-15 12:08:41 -0700894 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
895 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
896 peer_connection_factory_;
897
deadbeeffaac4972015-11-12 15:33:07 -0800898 bool auto_add_stream_ = true;
899
deadbeefaf1b59c2015-10-15 12:08:41 -0700900 typedef std::pair<std::string, std::string> IceUfragPwdPair;
901 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
902 bool expect_ice_restart_ = false;
903
deadbeefc9be0072015-12-14 18:27:57 -0800904 // Needed to keep track of number of frames sent.
deadbeefaf1b59c2015-10-15 12:08:41 -0700905 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
906 // Needed to keep track of number of frames received.
deadbeefc9be0072015-12-14 18:27:57 -0800907 std::map<std::string, rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
908 fake_video_renderers_;
909 // Needed to ensure frames aren't received for removed tracks.
910 std::vector<rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
911 removed_fake_video_renderers_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700912 // Needed to keep track of number of frames received when external decoder
913 // used.
914 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
915 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
916 bool video_decoder_factory_enabled_ = false;
917 webrtc::FakeConstraints video_constraints_;
918
919 // For remote peer communication.
920 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
921
922 // Store references to the video capturers we've created, so that we can stop
923 // them, if required.
924 std::vector<cricket::VideoCapturer*> video_capturers_;
925
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926 webrtc::FakeConstraints session_description_constraints_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700927 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
928 bool remove_bundle_ =
929 false; // True if bundle should be removed in received SDP.
930 bool remove_sdes_ =
931 false; // True if a=crypto should be removed in received SDP.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000933 rtc::scoped_refptr<DataChannelInterface> data_channel_;
934 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935};
936
deadbeef7c73bdb2015-12-10 15:10:44 -0800937class P2PTestConductor : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938 public:
deadbeef7c73bdb2015-12-10 15:10:44 -0800939 P2PTestConductor()
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000940 : pss_(new rtc::PhysicalSocketServer),
941 ss_(new rtc::VirtualSocketServer(pss_.get())),
942 ss_scope_(ss_.get()) {}
943
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944 bool SessionActive() {
945 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000946 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000948
hta6b4f8392016-03-10 00:24:31 -0800949 // Return true if the number of frames provided have been received
950 // on the video and audio tracks provided.
951 bool FramesHaveArrived(int audio_frames_to_receive,
952 int video_frames_to_receive) {
953 bool all_good = true;
954 if (initiating_client_->HasLocalAudioTrack() &&
955 receiving_client_->can_receive_audio()) {
956 all_good &=
957 receiving_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
958 }
959 if (initiating_client_->HasLocalVideoTrack() &&
960 receiving_client_->can_receive_video()) {
961 all_good &=
962 receiving_client_->VideoFramesReceivedCheck(video_frames_to_receive);
963 }
964 if (receiving_client_->HasLocalAudioTrack() &&
965 initiating_client_->can_receive_audio()) {
966 all_good &=
967 initiating_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
968 }
969 if (receiving_client_->HasLocalVideoTrack() &&
970 initiating_client_->can_receive_video()) {
971 all_good &=
972 initiating_client_->VideoFramesReceivedCheck(video_frames_to_receive);
973 }
974 return all_good;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975 }
hta6b4f8392016-03-10 00:24:31 -0800976
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000977 void VerifyDtmf() {
978 initiating_client_->VerifyDtmf();
979 receiving_client_->VerifyDtmf();
980 }
981
982 void TestUpdateOfferWithRejectedContent() {
deadbeefc9be0072015-12-14 18:27:57 -0800983 // Renegotiate, rejecting the video m-line.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984 initiating_client_->Negotiate(true, false);
deadbeefc9be0072015-12-14 18:27:57 -0800985 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
986
987 int pc1_audio_received = initiating_client_->audio_frames_received();
988 int pc1_video_received = initiating_client_->video_frames_received();
989 int pc2_audio_received = receiving_client_->audio_frames_received();
990 int pc2_video_received = receiving_client_->video_frames_received();
991
992 // Wait for some additional audio frames to be received.
993 EXPECT_TRUE_WAIT(initiating_client_->AudioFramesReceivedCheck(
994 pc1_audio_received + kEndAudioFrameCount) &&
995 receiving_client_->AudioFramesReceivedCheck(
996 pc2_audio_received + kEndAudioFrameCount),
997 kMaxWaitForFramesMs);
998
999 // During this time, we shouldn't have received any additional video frames
1000 // for the rejected video tracks.
1001 EXPECT_EQ(pc1_video_received, initiating_client_->video_frames_received());
1002 EXPECT_EQ(pc2_video_received, receiving_client_->video_frames_received());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003 }
1004
1005 void VerifyRenderedSize(int width, int height) {
1006 EXPECT_EQ(width, receiving_client()->rendered_width());
1007 EXPECT_EQ(height, receiving_client()->rendered_height());
1008 EXPECT_EQ(width, initializing_client()->rendered_width());
1009 EXPECT_EQ(height, initializing_client()->rendered_height());
1010 }
1011
1012 void VerifySessionDescriptions() {
1013 initiating_client_->VerifyRejectedMediaInSessionDescription();
1014 receiving_client_->VerifyRejectedMediaInSessionDescription();
1015 initiating_client_->VerifyLocalIceUfragAndPassword();
1016 receiving_client_->VerifyLocalIceUfragAndPassword();
1017 }
1018
deadbeef7c73bdb2015-12-10 15:10:44 -08001019 ~P2PTestConductor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001021 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001022 }
1023 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001024 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001025 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001026 }
1027
deadbeefaf1b59c2015-10-15 12:08:41 -07001028 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029
1030 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1031 MediaConstraintsInterface* recv_constraints) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001032 return CreateTestClients(init_constraints, nullptr, recv_constraints,
1033 nullptr);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001034 }
1035
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001036 void SetSignalingReceivers() {
1037 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
1038 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1039 }
1040
Joachim Bauch04e5b492015-05-29 09:40:39 +02001041 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1042 PeerConnectionFactory::Options* init_options,
1043 MediaConstraintsInterface* recv_constraints,
1044 PeerConnectionFactory::Options* recv_options) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001045 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
1046 "Caller: ", init_constraints, init_options));
1047 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
1048 "Callee: ", recv_constraints, recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049 if (!initiating_client_ || !receiving_client_) {
1050 return false;
1051 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001052 SetSignalingReceivers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053 return true;
1054 }
1055
1056 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1057 const webrtc::FakeConstraints& recv_constraints) {
1058 initiating_client_->SetVideoConstraints(init_constraints);
1059 receiving_client_->SetVideoConstraints(recv_constraints);
1060 }
1061
1062 void EnableVideoDecoderFactory() {
1063 initiating_client_->EnableVideoDecoderFactory();
1064 receiving_client_->EnableVideoDecoderFactory();
1065 }
1066
1067 // This test sets up a call between two parties. Both parties send static
1068 // frames to each other. Once the test is finished the number of sent frames
1069 // is compared to the number of received frames.
1070 void LocalP2PTest() {
1071 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1072 initiating_client_->AddMediaStream(true, true);
1073 }
1074 initiating_client_->Negotiate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001075 // Assert true is used here since next tests are guaranteed to fail and
1076 // would eat up 5 seconds.
1077 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1078 VerifySessionDescriptions();
1079
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001080 int audio_frame_count = kEndAudioFrameCount;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001081 int video_frame_count = kEndVideoFrameCount;
hta6b4f8392016-03-10 00:24:31 -08001082 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1083
1084 if ((!initiating_client_->can_receive_audio() &&
1085 !initiating_client_->can_receive_video()) ||
1086 (!receiving_client_->can_receive_audio() &&
1087 !receiving_client_->can_receive_video())) {
1088 // Neither audio nor video will flow, so connections won't be
1089 // established. There's nothing more to check.
1090 // TODO(hta): Check connection if there's a data channel.
1091 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092 }
1093
hta6b4f8392016-03-10 00:24:31 -08001094 // Audio or video is expected to flow, so both clients should reach the
1095 // Connected state, and the offerer (ICE controller) should proceed to
1096 // Completed.
1097 // Note: These tests have been observed to fail under heavy load at
1098 // shorter timeouts, so they may be flaky.
1099 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1100 initiating_client_->ice_connection_state(),
1101 kMaxWaitForFramesMs);
1102 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1103 receiving_client_->ice_connection_state(),
1104 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001105
hta6b4f8392016-03-10 00:24:31 -08001106 // The ICE gathering state should end up in kIceGatheringComplete,
1107 // but there's a bug that prevents this at the moment, and the state
1108 // machine is being updated by the WEBRTC WG.
1109 // TODO(hta): Update this check when spec revisions finish.
1110 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1111 initiating_client_->ice_gathering_state());
1112 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1113 receiving_client_->ice_gathering_state(),
1114 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115
hta6b4f8392016-03-10 00:24:31 -08001116 // Check that the expected number of frames have arrived.
1117 EXPECT_TRUE_WAIT(FramesHaveArrived(audio_frame_count, video_frame_count),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118 kMaxWaitForFramesMs);
1119 }
1120
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001121 void SetupAndVerifyDtlsCall() {
1122 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1123 FakeConstraints setup_constraints;
1124 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1125 true);
1126 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1127 LocalP2PTest();
1128 VerifyRenderedSize(640, 480);
1129 }
1130
1131 PeerConnectionTestClient* CreateDtlsClientWithAlternateKey() {
1132 FakeConstraints setup_constraints;
1133 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1134 true);
1135
1136 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
1137 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
1138 : nullptr);
1139 dtls_identity_store->use_alternate_key();
1140
1141 // Make sure the new client is using a different certificate.
1142 return PeerConnectionTestClient::CreateClientWithDtlsIdentityStore(
kwiberg0eb15ed2015-12-17 03:04:15 -08001143 "New Peer: ", &setup_constraints, nullptr,
1144 std::move(dtls_identity_store));
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001145 }
1146
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001147 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1148 // Messages may get lost on the unreliable DataChannel, so we send multiple
1149 // times to avoid test flakiness.
1150 static const size_t kSendAttempts = 5;
1151
1152 for (size_t i = 0; i < kSendAttempts; ++i) {
1153 dc->Send(DataBuffer(data));
1154 }
1155 }
1156
deadbeefaf1b59c2015-10-15 12:08:41 -07001157 PeerConnectionTestClient* initializing_client() {
1158 return initiating_client_.get();
1159 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001160
1161 // Set the |initiating_client_| to the |client| passed in and return the
1162 // original |initiating_client_|.
1163 PeerConnectionTestClient* set_initializing_client(
1164 PeerConnectionTestClient* client) {
1165 PeerConnectionTestClient* old = initiating_client_.release();
1166 initiating_client_.reset(client);
1167 return old;
1168 }
1169
deadbeefaf1b59c2015-10-15 12:08:41 -07001170 PeerConnectionTestClient* receiving_client() {
1171 return receiving_client_.get();
1172 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001173
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001174 // Set the |receiving_client_| to the |client| passed in and return the
1175 // original |receiving_client_|.
1176 PeerConnectionTestClient* set_receiving_client(
1177 PeerConnectionTestClient* client) {
1178 PeerConnectionTestClient* old = receiving_client_.release();
1179 receiving_client_.reset(client);
1180 return old;
1181 }
1182
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001184 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1185 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1186 rtc::SocketServerScope ss_scope_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001187 rtc::scoped_ptr<PeerConnectionTestClient> initiating_client_;
1188 rtc::scoped_ptr<PeerConnectionTestClient> receiving_client_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001190
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001191// Disable for TSan v2, see
1192// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1193#if !defined(THREAD_SANITIZER)
1194
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001196// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1197// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001198TEST_F(P2PTestConductor, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001199 ASSERT_TRUE(CreateTestClients());
1200 LocalP2PTest();
1201 VerifyDtmf();
1202}
1203
1204// This test sets up a Jsep call between two parties and test that we can get a
1205// video aspect ratio of 16:9.
deadbeef7c73bdb2015-12-10 15:10:44 -08001206TEST_F(P2PTestConductor, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001207 ASSERT_TRUE(CreateTestClients());
1208 FakeConstraints constraint;
1209 double requested_ratio = 640.0/360;
1210 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1211 SetVideoConstraints(constraint, constraint);
1212 LocalP2PTest();
1213
1214 ASSERT_LE(0, initializing_client()->rendered_height());
1215 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001216 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001217 initializing_client()->rendered_height();
1218 EXPECT_LE(requested_ratio, initiating_video_ratio);
1219
1220 ASSERT_LE(0, receiving_client()->rendered_height());
1221 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001222 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223 receiving_client()->rendered_height();
1224 EXPECT_LE(requested_ratio, receiving_video_ratio);
1225}
1226
1227// This test sets up a Jsep call between two parties and test that the
1228// received video has a resolution of 1280*720.
1229// TODO(mallinath): Enable when
1230// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
deadbeef7c73bdb2015-12-10 15:10:44 -08001231TEST_F(P2PTestConductor, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001232 ASSERT_TRUE(CreateTestClients());
1233 FakeConstraints constraint;
1234 constraint.SetMandatoryMinWidth(1280);
1235 constraint.SetMandatoryMinHeight(720);
1236 SetVideoConstraints(constraint, constraint);
1237 LocalP2PTest();
1238 VerifyRenderedSize(1280, 720);
1239}
1240
1241// This test sets up a call between two endpoints that are configured to use
1242// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001243TEST_F(P2PTestConductor, LocalP2PTestDtls) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001244 SetupAndVerifyDtlsCall();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001245}
1246
hta6b4f8392016-03-10 00:24:31 -08001247// This test sets up an one-way call, with media only from initiator to
1248// responder.
1249TEST_F(P2PTestConductor, OneWayMediaCall) {
1250 ASSERT_TRUE(CreateTestClients());
1251 receiving_client()->set_auto_add_stream(false);
1252 LocalP2PTest();
1253}
1254
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001255// This test sets up a audio call initially and then upgrades to audio/video,
1256// using DTLS.
deadbeef7c73bdb2015-12-10 15:10:44 -08001257TEST_F(P2PTestConductor, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001258 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001259 FakeConstraints setup_constraints;
1260 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1261 true);
1262 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1263 receiving_client()->SetReceiveAudioVideo(true, false);
1264 LocalP2PTest();
1265 receiving_client()->SetReceiveAudioVideo(true, true);
1266 receiving_client()->Negotiate();
1267}
1268
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001269// This test sets up a call transfer to a new caller with a different DTLS
1270// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001271TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCallee) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001272 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1273 SetupAndVerifyDtlsCall();
1274
1275 // Keeping the original peer around which will still send packets to the
1276 // receiving client. These SRTP packets will be dropped.
1277 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1278 set_initializing_client(CreateDtlsClientWithAlternateKey()));
1279 original_peer->pc()->Close();
1280
1281 SetSignalingReceivers();
1282 receiving_client()->SetExpectIceRestart(true);
1283 LocalP2PTest();
1284 VerifyRenderedSize(640, 480);
1285}
1286
guoweis46383312015-12-17 16:45:59 -08001287// This test sets up a non-bundle call and apply bundle during ICE restart. When
1288// bundle is in effect in the restart, the channel can successfully reset its
1289// DTLS-SRTP context.
1290TEST_F(P2PTestConductor, LocalP2PTestDtlsBundleInIceRestart) {
1291 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1292 FakeConstraints setup_constraints;
1293 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1294 true);
1295 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1296 receiving_client()->RemoveBundleFromReceivedSdp(true);
1297 LocalP2PTest();
1298 VerifyRenderedSize(640, 480);
1299
1300 initializing_client()->IceRestart();
1301 receiving_client()->SetExpectIceRestart(true);
1302 receiving_client()->RemoveBundleFromReceivedSdp(false);
1303 LocalP2PTest();
1304 VerifyRenderedSize(640, 480);
1305}
1306
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001307// This test sets up a call transfer to a new callee with a different DTLS
1308// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001309TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCaller) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001310 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1311 SetupAndVerifyDtlsCall();
1312
1313 // Keeping the original peer around which will still send packets to the
1314 // receiving client. These SRTP packets will be dropped.
1315 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1316 set_receiving_client(CreateDtlsClientWithAlternateKey()));
1317 original_peer->pc()->Close();
1318
1319 SetSignalingReceivers();
1320 initializing_client()->IceRestart();
1321 LocalP2PTest();
1322 VerifyRenderedSize(640, 480);
1323}
1324
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001325// This test sets up a call between two endpoints that are configured to use
1326// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1327// negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001328TEST_F(P2PTestConductor, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001329 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330 FakeConstraints setup_constraints;
1331 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1332 true);
1333 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1334 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1335 LocalP2PTest();
1336 VerifyRenderedSize(640, 480);
1337}
1338
1339// This test sets up a Jsep call between two parties, and the callee only
1340// accept to receive video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001341TEST_F(P2PTestConductor, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001342 ASSERT_TRUE(CreateTestClients());
1343 receiving_client()->SetReceiveAudioVideo(false, true);
1344 LocalP2PTest();
1345}
1346
1347// This test sets up a Jsep call between two parties, and the callee only
1348// accept to receive audio.
deadbeef7c73bdb2015-12-10 15:10:44 -08001349TEST_F(P2PTestConductor, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001350 ASSERT_TRUE(CreateTestClients());
1351 receiving_client()->SetReceiveAudioVideo(true, false);
1352 LocalP2PTest();
1353}
1354
1355// This test sets up a Jsep call between two parties, and the callee reject both
1356// audio and video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001357TEST_F(P2PTestConductor, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001358 ASSERT_TRUE(CreateTestClients());
1359 receiving_client()->SetReceiveAudioVideo(false, false);
1360 LocalP2PTest();
1361}
1362
1363// This test sets up an audio and video call between two parties. After the call
1364// runs for a while (10 frames), the caller sends an update offer with video
1365// being rejected. Once the re-negotiation is done, the video flow should stop
1366// and the audio flow should continue.
deadbeefc9be0072015-12-14 18:27:57 -08001367TEST_F(P2PTestConductor, UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368 ASSERT_TRUE(CreateTestClients());
1369 LocalP2PTest();
1370 TestUpdateOfferWithRejectedContent();
1371}
1372
1373// This test sets up a Jsep call between two parties. The MSID is removed from
1374// the SDP strings from the caller.
deadbeefc9be0072015-12-14 18:27:57 -08001375TEST_F(P2PTestConductor, LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376 ASSERT_TRUE(CreateTestClients());
1377 receiving_client()->RemoveMsidFromReceivedSdp(true);
1378 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1379 // audio and video is muxed when MSID is disabled. Remove
1380 // SetRemoveBundleFromSdp once
1381 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1382 receiving_client()->RemoveBundleFromReceivedSdp(true);
1383 LocalP2PTest();
1384}
1385
1386// This test sets up a Jsep call between two parties and the initiating peer
1387// sends two steams.
1388// TODO(perkj): Disabled due to
1389// https://code.google.com/p/webrtc/issues/detail?id=1454
deadbeef7c73bdb2015-12-10 15:10:44 -08001390TEST_F(P2PTestConductor, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001391 ASSERT_TRUE(CreateTestClients());
1392 // Set optional video constraint to max 320pixels to decrease CPU usage.
1393 FakeConstraints constraint;
1394 constraint.SetOptionalMaxWidth(320);
1395 SetVideoConstraints(constraint, constraint);
1396 initializing_client()->AddMediaStream(true, true);
1397 initializing_client()->AddMediaStream(false, true);
1398 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1399 LocalP2PTest();
1400 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1401}
1402
1403// Test that we can receive the audio output level from a remote audio track.
deadbeef7c73bdb2015-12-10 15:10:44 -08001404TEST_F(P2PTestConductor, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001405 ASSERT_TRUE(CreateTestClients());
1406 LocalP2PTest();
1407
1408 StreamCollectionInterface* remote_streams =
1409 initializing_client()->remote_streams();
1410 ASSERT_GT(remote_streams->count(), 0u);
1411 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1412 MediaStreamTrackInterface* remote_audio_track =
1413 remote_streams->at(0)->GetAudioTracks()[0];
1414
1415 // Get the audio output level stats. Note that the level is not available
1416 // until a RTCP packet has been received.
1417 EXPECT_TRUE_WAIT(
1418 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1419 kMaxWaitForStatsMs);
1420}
1421
1422// Test that an audio input level is reported.
deadbeef7c73bdb2015-12-10 15:10:44 -08001423TEST_F(P2PTestConductor, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001424 ASSERT_TRUE(CreateTestClients());
1425 LocalP2PTest();
1426
1427 // Get the audio input level stats. The level should be available very
1428 // soon after the test starts.
1429 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1430 kMaxWaitForStatsMs);
1431}
1432
1433// Test that we can get incoming byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001434TEST_F(P2PTestConductor, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001435 ASSERT_TRUE(CreateTestClients());
1436 LocalP2PTest();
1437
1438 StreamCollectionInterface* remote_streams =
1439 initializing_client()->remote_streams();
1440 ASSERT_GT(remote_streams->count(), 0u);
1441 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1442 MediaStreamTrackInterface* remote_audio_track =
1443 remote_streams->at(0)->GetAudioTracks()[0];
1444 EXPECT_TRUE_WAIT(
1445 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1446 kMaxWaitForStatsMs);
1447
1448 MediaStreamTrackInterface* remote_video_track =
1449 remote_streams->at(0)->GetVideoTracks()[0];
1450 EXPECT_TRUE_WAIT(
1451 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1452 kMaxWaitForStatsMs);
1453}
1454
1455// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001456TEST_F(P2PTestConductor, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001457 ASSERT_TRUE(CreateTestClients());
1458 LocalP2PTest();
1459
1460 StreamCollectionInterface* local_streams =
1461 initializing_client()->local_streams();
1462 ASSERT_GT(local_streams->count(), 0u);
1463 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1464 MediaStreamTrackInterface* local_audio_track =
1465 local_streams->at(0)->GetAudioTracks()[0];
1466 EXPECT_TRUE_WAIT(
1467 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1468 kMaxWaitForStatsMs);
1469
1470 MediaStreamTrackInterface* local_video_track =
1471 local_streams->at(0)->GetVideoTracks()[0];
1472 EXPECT_TRUE_WAIT(
1473 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1474 kMaxWaitForStatsMs);
1475}
1476
Joachim Bauch04e5b492015-05-29 09:40:39 +02001477// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
kjellander43942d12016-03-07 13:59:10 -08001478// Disabled due to new BoringSSLL version, see webrtc:5634
1479TEST_F(P2PTestConductor, DISABLED_GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001480 PeerConnectionFactory::Options init_options;
1481 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1482 PeerConnectionFactory::Options recv_options;
1483 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001484 ASSERT_TRUE(
1485 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001486 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1487 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1488 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001489 LocalP2PTest();
1490
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001491 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001492 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1493 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1494 initializing_client()->GetDtlsCipherStats(),
1495 kMaxWaitForStatsMs);
1496 EXPECT_EQ(1, init_observer->GetEnumCounter(
1497 webrtc::kEnumCounterAudioSslCipher,
1498 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1499 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001500
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001501 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001502 initializing_client()->GetSrtpCipherStats(),
1503 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001504 EXPECT_EQ(1,
1505 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1506 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001507}
1508
1509// Test that DTLS 1.2 is used if both ends support it.
torbjorng79a5a832016-01-15 07:16:51 -08001510TEST_F(P2PTestConductor, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001511 PeerConnectionFactory::Options init_options;
1512 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1513 PeerConnectionFactory::Options recv_options;
1514 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001515 ASSERT_TRUE(
1516 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001517 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1518 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1519 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001520 LocalP2PTest();
1521
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001522 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001523 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1524 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)),
1525 initializing_client()->GetDtlsCipherStats(),
1526 kMaxWaitForStatsMs);
1527 EXPECT_EQ(1, init_observer->GetEnumCounter(
1528 webrtc::kEnumCounterAudioSslCipher,
1529 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1530 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001531
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001532 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001533 initializing_client()->GetSrtpCipherStats(),
1534 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001535 EXPECT_EQ(1,
1536 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1537 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001538}
1539
1540// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1541// received supports 1.0.
kjellander43942d12016-03-07 13:59:10 -08001542// Disabled due to new BoringSSLL version, see webrtc:5634
1543TEST_F(P2PTestConductor, DISABLED_GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001544 PeerConnectionFactory::Options init_options;
1545 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1546 PeerConnectionFactory::Options recv_options;
1547 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001548 ASSERT_TRUE(
1549 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001550 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1551 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1552 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001553 LocalP2PTest();
1554
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001555 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001556 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1557 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1558 initializing_client()->GetDtlsCipherStats(),
1559 kMaxWaitForStatsMs);
1560 EXPECT_EQ(1, init_observer->GetEnumCounter(
1561 webrtc::kEnumCounterAudioSslCipher,
1562 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1563 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001564
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001565 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001566 initializing_client()->GetSrtpCipherStats(),
1567 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001568 EXPECT_EQ(1,
1569 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1570 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001571}
1572
1573// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1574// received supports 1.2.
kjellander43942d12016-03-07 13:59:10 -08001575// Disabled due to new BoringSSLL version, see webrtc:5634
1576TEST_F(P2PTestConductor, DISABLED_GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001577 PeerConnectionFactory::Options init_options;
1578 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1579 PeerConnectionFactory::Options recv_options;
1580 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001581 ASSERT_TRUE(
1582 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001583 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1584 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1585 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001586 LocalP2PTest();
1587
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001588 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001589 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1590 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1591 initializing_client()->GetDtlsCipherStats(),
1592 kMaxWaitForStatsMs);
1593 EXPECT_EQ(1, init_observer->GetEnumCounter(
1594 webrtc::kEnumCounterAudioSslCipher,
1595 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1596 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001597
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001598 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001599 initializing_client()->GetSrtpCipherStats(),
1600 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001601 EXPECT_EQ(1,
1602 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1603 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001604}
1605
deadbeefb5cb19b2015-11-23 16:39:12 -08001606// This test sets up a call between two parties with audio, video and an RTP
1607// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001608TEST_F(P2PTestConductor, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001609 FakeConstraints setup_constraints;
1610 setup_constraints.SetAllowRtpDataChannels();
1611 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1612 initializing_client()->CreateDataChannel();
1613 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001614 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1615 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1617 kMaxWaitMs);
1618 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1619 kMaxWaitMs);
1620
1621 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001622
1623 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1625 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001626
1627 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1629 kMaxWaitMs);
1630
1631 receiving_client()->data_channel()->Close();
1632 // Send new offer and answer.
1633 receiving_client()->Negotiate();
1634 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1635 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1636}
1637
deadbeefb5cb19b2015-11-23 16:39:12 -08001638// This test sets up a call between two parties with audio, video and an SCTP
1639// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001640TEST_F(P2PTestConductor, LocalP2PTestSctpDataChannel) {
deadbeefb5cb19b2015-11-23 16:39:12 -08001641 ASSERT_TRUE(CreateTestClients());
1642 initializing_client()->CreateDataChannel();
1643 LocalP2PTest();
1644 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1645 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
1646 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1647 kMaxWaitMs);
1648 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
1649
1650 std::string data = "hello world";
1651
1652 initializing_client()->data_channel()->Send(DataBuffer(data));
1653 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1654 kMaxWaitMs);
1655
1656 receiving_client()->data_channel()->Send(DataBuffer(data));
1657 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1658 kMaxWaitMs);
1659
1660 receiving_client()->data_channel()->Close();
deadbeef15887932015-12-14 19:32:34 -08001661 EXPECT_TRUE_WAIT(!initializing_client()->data_observer()->IsOpen(),
1662 kMaxWaitMs);
1663 EXPECT_TRUE_WAIT(!receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
deadbeefb5cb19b2015-11-23 16:39:12 -08001664}
1665
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001666// This test sets up a call between two parties and creates a data channel.
1667// The test tests that received data is buffered unless an observer has been
1668// registered.
1669// Rtp data channels can receive data before the underlying
1670// transport has detected that a channel is writable and thus data can be
1671// received before the data channel state changes to open. That is hard to test
1672// but the same buffering is used in that case.
deadbeef7c73bdb2015-12-10 15:10:44 -08001673TEST_F(P2PTestConductor, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674 FakeConstraints setup_constraints;
1675 setup_constraints.SetAllowRtpDataChannels();
1676 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1677 initializing_client()->CreateDataChannel();
1678 initializing_client()->Negotiate();
1679
deadbeefaf1b59c2015-10-15 12:08:41 -07001680 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1681 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1683 kMaxWaitMs);
1684 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1685 receiving_client()->data_channel()->state(), kMaxWaitMs);
1686
1687 // Unregister the existing observer.
1688 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001689
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001690 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001691 SendRtpData(initializing_client()->data_channel(), data);
1692
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001693 // Wait a while to allow the sent data to arrive before an observer is
1694 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001695 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001696
1697 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1698 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1699}
1700
1701// This test sets up a call between two parties with audio, video and but only
1702// the initiating client support data.
deadbeef7c73bdb2015-12-10 15:10:44 -08001703TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001704 FakeConstraints setup_constraints_1;
1705 setup_constraints_1.SetAllowRtpDataChannels();
1706 // Must disable DTLS to make negotiation succeed.
1707 setup_constraints_1.SetMandatory(
1708 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1709 FakeConstraints setup_constraints_2;
1710 setup_constraints_2.SetMandatory(
1711 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1712 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713 initializing_client()->CreateDataChannel();
1714 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001715 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716 EXPECT_FALSE(receiving_client()->data_channel());
1717 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1718}
1719
1720// This test sets up a call between two parties with audio, video. When audio
1721// and video is setup and flowing and data channel is negotiated.
deadbeef7c73bdb2015-12-10 15:10:44 -08001722TEST_F(P2PTestConductor, AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723 FakeConstraints setup_constraints;
1724 setup_constraints.SetAllowRtpDataChannels();
1725 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1726 LocalP2PTest();
1727 initializing_client()->CreateDataChannel();
1728 // Send new offer and answer.
1729 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07001730 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1731 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1733 kMaxWaitMs);
1734 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1735 kMaxWaitMs);
1736}
1737
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001738// This test sets up a Jsep call with SCTP DataChannel and verifies the
1739// negotiation is completed without error.
1740#ifdef HAVE_SCTP
deadbeef7c73bdb2015-12-10 15:10:44 -08001741TEST_F(P2PTestConductor, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001742 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001743 FakeConstraints constraints;
1744 constraints.SetMandatory(
1745 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1746 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1747 initializing_client()->CreateDataChannel();
1748 initializing_client()->Negotiate(false, false);
1749}
1750#endif
1751
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752// This test sets up a call between two parties with audio, and video.
1753// During the call, the initializing side restart ice and the test verifies that
1754// new ice candidates are generated and audio and video still can flow.
deadbeef7c73bdb2015-12-10 15:10:44 -08001755TEST_F(P2PTestConductor, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756 ASSERT_TRUE(CreateTestClients());
1757
1758 // Negotiate and wait for ice completion and make sure audio and video plays.
1759 LocalP2PTest();
1760
1761 // Create a SDP string of the first audio candidate for both clients.
1762 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1763 initializing_client()->pc()->local_description()->candidates(0);
1764 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1765 receiving_client()->pc()->local_description()->candidates(0);
1766 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1767 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1768 std::string initiator_candidate;
1769 EXPECT_TRUE(
1770 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1771 std::string receiver_candidate;
1772 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1773
1774 // Restart ice on the initializing client.
1775 receiving_client()->SetExpectIceRestart(true);
1776 initializing_client()->IceRestart();
1777
1778 // Negotiate and wait for ice completion again and make sure audio and video
1779 // plays.
1780 LocalP2PTest();
1781
1782 // Create a SDP string of the first audio candidate for both clients again.
1783 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1784 initializing_client()->pc()->local_description()->candidates(0);
1785 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1786 receiving_client()->pc()->local_description()->candidates(0);
1787 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1788 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1789 std::string initiator_candidate_restart;
1790 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1791 &initiator_candidate_restart));
1792 std::string receiver_candidate_restart;
1793 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1794 &receiver_candidate_restart));
1795
1796 // Verify that the first candidates in the local session descriptions has
1797 // changed.
1798 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1799 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1800}
1801
deadbeeffaac4972015-11-12 15:33:07 -08001802// This test sets up a call between two parties with audio, and video.
1803// It then renegotiates setting the video m-line to "port 0", then later
1804// renegotiates again, enabling video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001805TEST_F(P2PTestConductor, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08001806 ASSERT_TRUE(CreateTestClients());
1807
1808 // Do initial negotiation. Will result in video and audio sendonly m-lines.
1809 receiving_client()->set_auto_add_stream(false);
1810 initializing_client()->AddMediaStream(true, true);
1811 initializing_client()->Negotiate();
1812
1813 // Negotiate again, disabling the video m-line (receiving client will
1814 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
1815 receiving_client()->SetReceiveVideo(false);
1816 initializing_client()->Negotiate();
1817
1818 // Enable video and do negotiation again, making sure video is received
1819 // end-to-end.
1820 receiving_client()->SetReceiveVideo(true);
1821 receiving_client()->AddMediaStream(true, true);
1822 LocalP2PTest();
1823}
1824
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825// This test sets up a Jsep call between two parties with external
1826// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001827// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1828// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001829TEST_F(P2PTestConductor, DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001830 ASSERT_TRUE(CreateTestClients());
1831 EnableVideoDecoderFactory();
1832 LocalP2PTest();
1833}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001834
deadbeeffac06552015-11-25 11:26:01 -08001835// This tests that if we negotiate after calling CreateSender but before we
1836// have a track, then set a track later, frames from the newly-set track are
1837// received end-to-end.
deadbeef7c73bdb2015-12-10 15:10:44 -08001838TEST_F(P2PTestConductor, EarlyWarmupTest) {
deadbeeffac06552015-11-25 11:26:01 -08001839 ASSERT_TRUE(CreateTestClients());
deadbeefbd7d8f72015-12-18 16:58:44 -08001840 auto audio_sender =
1841 initializing_client()->pc()->CreateSender("audio", "stream_id");
1842 auto video_sender =
1843 initializing_client()->pc()->CreateSender("video", "stream_id");
deadbeeffac06552015-11-25 11:26:01 -08001844 initializing_client()->Negotiate();
1845 // Wait for ICE connection to complete, without any tracks.
1846 // Note that the receiving client WILL (in HandleIncomingOffer) create
1847 // tracks, so it's only the initiator here that's doing early warmup.
1848 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1849 VerifySessionDescriptions();
1850 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1851 initializing_client()->ice_connection_state(),
1852 kMaxWaitForFramesMs);
1853 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1854 receiving_client()->ice_connection_state(),
1855 kMaxWaitForFramesMs);
1856 // Now set the tracks, and expect frames to immediately start flowing.
1857 EXPECT_TRUE(
1858 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
1859 EXPECT_TRUE(
1860 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
hta6b4f8392016-03-10 00:24:31 -08001861 EXPECT_TRUE_WAIT(FramesHaveArrived(kEndAudioFrameCount, kEndVideoFrameCount),
deadbeeffac06552015-11-25 11:26:01 -08001862 kMaxWaitForFramesMs);
1863}
1864
deadbeef0a6c4ca2015-10-06 11:38:28 -07001865class IceServerParsingTest : public testing::Test {
1866 public:
1867 // Convenience for parsing a single URL.
1868 bool ParseUrl(const std::string& url) {
1869 return ParseUrl(url, std::string(), std::string());
1870 }
1871
1872 bool ParseUrl(const std::string& url,
1873 const std::string& username,
1874 const std::string& password) {
1875 PeerConnectionInterface::IceServers servers;
1876 PeerConnectionInterface::IceServer server;
1877 server.urls.push_back(url);
1878 server.username = username;
1879 server.password = password;
1880 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001881 return webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_);
deadbeef0a6c4ca2015-10-06 11:38:28 -07001882 }
1883
1884 protected:
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001885 cricket::ServerAddresses stun_servers_;
1886 std::vector<cricket::RelayServerConfig> turn_servers_;
deadbeef0a6c4ca2015-10-06 11:38:28 -07001887};
1888
1889// Make sure all STUN/TURN prefixes are parsed correctly.
1890TEST_F(IceServerParsingTest, ParseStunPrefixes) {
1891 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001892 EXPECT_EQ(1U, stun_servers_.size());
1893 EXPECT_EQ(0U, turn_servers_.size());
1894 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001895
1896 EXPECT_TRUE(ParseUrl("stuns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001897 EXPECT_EQ(1U, stun_servers_.size());
1898 EXPECT_EQ(0U, turn_servers_.size());
1899 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001900
1901 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001902 EXPECT_EQ(0U, stun_servers_.size());
1903 EXPECT_EQ(1U, turn_servers_.size());
1904 EXPECT_FALSE(turn_servers_[0].ports[0].secure);
1905 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001906
1907 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001908 EXPECT_EQ(0U, stun_servers_.size());
1909 EXPECT_EQ(1U, turn_servers_.size());
1910 EXPECT_TRUE(turn_servers_[0].ports[0].secure);
1911 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001912
1913 // invalid prefixes
1914 EXPECT_FALSE(ParseUrl("stunn:hostname"));
1915 EXPECT_FALSE(ParseUrl(":hostname"));
1916 EXPECT_FALSE(ParseUrl(":"));
1917 EXPECT_FALSE(ParseUrl(""));
1918}
1919
1920TEST_F(IceServerParsingTest, VerifyDefaults) {
1921 // TURNS defaults
1922 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001923 EXPECT_EQ(1U, turn_servers_.size());
1924 EXPECT_EQ(5349, turn_servers_[0].ports[0].address.port());
1925 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
1926 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001927
1928 // TURN defaults
1929 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001930 EXPECT_EQ(1U, turn_servers_.size());
1931 EXPECT_EQ(3478, turn_servers_[0].ports[0].address.port());
1932 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
1933 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001934
1935 // STUN defaults
1936 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001937 EXPECT_EQ(1U, stun_servers_.size());
1938 EXPECT_EQ(3478, stun_servers_.begin()->port());
1939 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001940}
1941
1942// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
1943// can be parsed correctly.
1944TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
1945 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001946 EXPECT_EQ(1U, stun_servers_.size());
1947 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
1948 EXPECT_EQ(1234, stun_servers_.begin()->port());
1949 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001950
1951 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001952 EXPECT_EQ(1U, stun_servers_.size());
1953 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
1954 EXPECT_EQ(4321, stun_servers_.begin()->port());
1955 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001956
1957 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001958 EXPECT_EQ(1U, stun_servers_.size());
1959 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
1960 EXPECT_EQ(9999, stun_servers_.begin()->port());
1961 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001962
1963 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001964 EXPECT_EQ(1U, stun_servers_.size());
1965 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
1966 EXPECT_EQ(3478, stun_servers_.begin()->port());
1967 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001968
1969 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001970 EXPECT_EQ(1U, stun_servers_.size());
1971 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
1972 EXPECT_EQ(3478, stun_servers_.begin()->port());
1973 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001974
1975 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001976 EXPECT_EQ(1U, stun_servers_.size());
1977 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
1978 EXPECT_EQ(3478, stun_servers_.begin()->port());
1979 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001980
1981 // Try some invalid hostname:port strings.
1982 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
1983 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001984 EXPECT_FALSE(ParseUrl("stun:hostname:port:more"));
1985 EXPECT_FALSE(ParseUrl("stun:hostname:port more"));
deadbeef0a6c4ca2015-10-06 11:38:28 -07001986 EXPECT_FALSE(ParseUrl("stun:hostname:"));
1987 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
1988 EXPECT_FALSE(ParseUrl("stun::5555"));
1989 EXPECT_FALSE(ParseUrl("stun:"));
1990}
1991
1992// Test parsing the "?transport=xxx" part of the URL.
1993TEST_F(IceServerParsingTest, ParseTransport) {
1994 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001995 EXPECT_EQ(1U, turn_servers_.size());
1996 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
1997 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001998
1999 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002000 EXPECT_EQ(1U, turn_servers_.size());
2001 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2002 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002003
2004 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
2005}
2006
2007// Test parsing ICE username contained in URL.
2008TEST_F(IceServerParsingTest, ParseUsername) {
2009 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002010 EXPECT_EQ(1U, turn_servers_.size());
2011 EXPECT_EQ("user", turn_servers_[0].credentials.username);
2012 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002013
2014 EXPECT_FALSE(ParseUrl("turn:@hostname"));
2015 EXPECT_FALSE(ParseUrl("turn:username@"));
2016 EXPECT_FALSE(ParseUrl("turn:@"));
2017 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
2018}
2019
2020// Test that username and password from IceServer is copied into the resulting
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002021// RelayServerConfig.
deadbeef0a6c4ca2015-10-06 11:38:28 -07002022TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
2023 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002024 EXPECT_EQ(1U, turn_servers_.size());
2025 EXPECT_EQ("username", turn_servers_[0].credentials.username);
2026 EXPECT_EQ("password", turn_servers_[0].credentials.password);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002027}
2028
2029// Ensure that if a server has multiple URLs, each one is parsed.
2030TEST_F(IceServerParsingTest, ParseMultipleUrls) {
2031 PeerConnectionInterface::IceServers servers;
2032 PeerConnectionInterface::IceServer server;
2033 server.urls.push_back("stun:hostname");
2034 server.urls.push_back("turn:hostname");
2035 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002036 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2037 EXPECT_EQ(1U, stun_servers_.size());
2038 EXPECT_EQ(1U, turn_servers_.size());
deadbeef0a6c4ca2015-10-06 11:38:28 -07002039}
2040
Taylor Brandstetter893505d2016-01-07 15:12:48 -08002041// Ensure that TURN servers are given unique priorities,
2042// so that their resulting candidates have unique priorities.
2043TEST_F(IceServerParsingTest, TurnServerPrioritiesUnique) {
2044 PeerConnectionInterface::IceServers servers;
2045 PeerConnectionInterface::IceServer server;
2046 server.urls.push_back("turn:hostname");
2047 server.urls.push_back("turn:hostname2");
2048 servers.push_back(server);
2049 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2050 EXPECT_EQ(2U, turn_servers_.size());
2051 EXPECT_NE(turn_servers_[0].priority, turn_servers_[1].priority);
2052}
2053
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00002054#endif // if !defined(THREAD_SANITIZER)
hta6b4f8392016-03-10 00:24:31 -08002055
2056} // namespace