blob: c6e51a6758eee1202fab0912c32d046acf8c5fa2 [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"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000032#include "webrtc/base/gunit.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000033#include "webrtc/base/physicalsocketserver.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/scoped_ptr.h"
35#include "webrtc/base/ssladapter.h"
36#include "webrtc/base/sslstreamadapter.h"
37#include "webrtc/base/thread.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000038#include "webrtc/base/virtualsocketserver.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010039#include "webrtc/media/engine/fakewebrtcvideoengine.h"
kjellanderf4752772016-03-02 05:42:30 -080040#include "webrtc/p2p/base/p2pconstants.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000041#include "webrtc/p2p/base/sessiondescription.h"
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080042#include "webrtc/p2p/client/fakeportallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010043#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45#define MAYBE_SKIP_TEST(feature) \
46 if (!(feature())) { \
47 LOG(LS_INFO) << "Feature disabled... skipping"; \
48 return; \
49 }
50
51using cricket::ContentInfo;
52using cricket::FakeWebRtcVideoDecoder;
53using cricket::FakeWebRtcVideoDecoderFactory;
54using cricket::FakeWebRtcVideoEncoder;
55using cricket::FakeWebRtcVideoEncoderFactory;
56using cricket::MediaContentDescription;
57using webrtc::DataBuffer;
58using webrtc::DataChannelInterface;
59using webrtc::DtmfSender;
60using webrtc::DtmfSenderInterface;
61using webrtc::DtmfSenderObserverInterface;
62using webrtc::FakeConstraints;
63using webrtc::MediaConstraintsInterface;
deadbeeffaac4972015-11-12 15:33:07 -080064using webrtc::MediaStreamInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065using webrtc::MediaStreamTrackInterface;
66using webrtc::MockCreateSessionDescriptionObserver;
67using webrtc::MockDataChannelObserver;
68using webrtc::MockSetSessionDescriptionObserver;
69using webrtc::MockStatsObserver;
deadbeeffaac4972015-11-12 15:33:07 -080070using webrtc::ObserverInterface;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000071using webrtc::PeerConnectionInterface;
Joachim Bauch04e5b492015-05-29 09:40:39 +020072using webrtc::PeerConnectionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073using webrtc::SessionDescriptionInterface;
74using webrtc::StreamCollectionInterface;
75
hta6b4f8392016-03-10 00:24:31 -080076namespace {
77
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000078static const int kMaxWaitMs = 10000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000079// Disable for TSan v2, see
80// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
81// This declaration is also #ifdef'd as it causes uninitialized-variable
82// warnings.
83#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084static const int kMaxWaitForStatsMs = 3000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000085#endif
deadbeeffac06552015-11-25 11:26:01 -080086static const int kMaxWaitForActivationMs = 5000;
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000087static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088static const int kEndAudioFrameCount = 3;
89static const int kEndVideoFrameCount = 3;
90
91static const char kStreamLabelBase[] = "stream_label";
92static const char kVideoTrackLabelBase[] = "video_track";
93static const char kAudioTrackLabelBase[] = "audio_track";
94static const char kDataChannelLabel[] = "data_channel";
95
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +000096// Disable for TSan v2, see
97// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
98// This declaration is also #ifdef'd as it causes unused-variable errors.
99#if !defined(THREAD_SANITIZER)
100// SRTP cipher name negotiated by the tests. This must be updated if the
101// default changes.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800102static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000103#endif
104
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105static void RemoveLinesFromSdp(const std::string& line_start,
106 std::string* sdp) {
107 const char kSdpLineEnd[] = "\r\n";
108 size_t ssrc_pos = 0;
109 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
110 std::string::npos) {
111 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
112 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
113 }
114}
115
hta6b4f8392016-03-10 00:24:31 -0800116bool StreamsHaveAudioTrack(StreamCollectionInterface* streams) {
117 for (size_t idx = 0; idx < streams->count(); idx++) {
118 auto stream = streams->at(idx);
119 if (stream->GetAudioTracks().size() > 0) {
120 return true;
121 }
122 }
123 return false;
124}
125
126bool StreamsHaveVideoTrack(StreamCollectionInterface* streams) {
127 for (size_t idx = 0; idx < streams->count(); idx++) {
128 auto stream = streams->at(idx);
129 if (stream->GetVideoTracks().size() > 0) {
130 return true;
131 }
132 }
133 return false;
134}
135
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136class SignalingMessageReceiver {
137 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138 virtual void ReceiveSdpMessage(const std::string& type,
139 std::string& msg) = 0;
140 virtual void ReceiveIceMessage(const std::string& sdp_mid,
141 int sdp_mline_index,
142 const std::string& msg) = 0;
143
144 protected:
deadbeefaf1b59c2015-10-15 12:08:41 -0700145 SignalingMessageReceiver() {}
146 virtual ~SignalingMessageReceiver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147};
148
deadbeefaf1b59c2015-10-15 12:08:41 -0700149class PeerConnectionTestClient : public webrtc::PeerConnectionObserver,
deadbeeffaac4972015-11-12 15:33:07 -0800150 public SignalingMessageReceiver,
151 public ObserverInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 public:
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800153 static PeerConnectionTestClient* CreateClientWithDtlsIdentityStore(
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800154 const std::string& id,
155 const MediaConstraintsInterface* constraints,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800156 const PeerConnectionFactory::Options* options,
htaaac2dea2016-03-10 13:35:55 -0800157 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store,
158 bool prefer_constraint_apis) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800159 PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
htaaac2dea2016-03-10 13:35:55 -0800160 if (!client->Init(constraints, options, std::move(dtls_identity_store),
161 prefer_constraint_apis)) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800162 delete client;
163 return nullptr;
164 }
165 return client;
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800166 }
167
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800168 static PeerConnectionTestClient* CreateClient(
169 const std::string& id,
170 const MediaConstraintsInterface* constraints,
171 const PeerConnectionFactory::Options* options) {
172 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
173 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
174 : nullptr);
175
htaaac2dea2016-03-10 13:35:55 -0800176 return CreateClientWithDtlsIdentityStore(
177 id, constraints, options, std::move(dtls_identity_store), true);
178 }
179
180 static PeerConnectionTestClient* CreateClientPreferNoConstraints(
181 const std::string& id,
182 const PeerConnectionFactory::Options* options) {
183 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
184 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
185 : nullptr);
186
187 return CreateClientWithDtlsIdentityStore(
188 id, nullptr, options, std::move(dtls_identity_store), false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800189 }
190
deadbeefaf1b59c2015-10-15 12:08:41 -0700191 ~PeerConnectionTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 }
193
deadbeefaf1b59c2015-10-15 12:08:41 -0700194 void Negotiate() { Negotiate(true, true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195
deadbeefaf1b59c2015-10-15 12:08:41 -0700196 void Negotiate(bool audio, bool video) {
197 rtc::scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700198 ASSERT_TRUE(DoCreateOffer(&offer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199
deadbeefaf1b59c2015-10-15 12:08:41 -0700200 if (offer->description()->GetContentByName("audio")) {
201 offer->description()->GetContentByName("audio")->rejected = !audio;
202 }
203 if (offer->description()->GetContentByName("video")) {
204 offer->description()->GetContentByName("video")->rejected = !video;
205 }
206
207 std::string sdp;
208 EXPECT_TRUE(offer->ToString(&sdp));
209 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
210 signaling_message_receiver_->ReceiveSdpMessage(
211 webrtc::SessionDescriptionInterface::kOffer, sdp);
212 }
213
214 // SignalingMessageReceiver callback.
215 void ReceiveSdpMessage(const std::string& type, std::string& msg) override {
216 FilterIncomingSdpMessage(&msg);
217 if (type == webrtc::SessionDescriptionInterface::kOffer) {
218 HandleIncomingOffer(msg);
219 } else {
220 HandleIncomingAnswer(msg);
221 }
222 }
223
224 // SignalingMessageReceiver callback.
225 void ReceiveIceMessage(const std::string& sdp_mid,
226 int sdp_mline_index,
227 const std::string& msg) override {
228 LOG(INFO) << id_ << "ReceiveIceMessage";
229 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
230 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
231 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
232 }
233
234 // PeerConnectionObserver callbacks.
235 void OnSignalingChange(
236 webrtc::PeerConnectionInterface::SignalingState new_state) override {
237 EXPECT_EQ(pc()->signaling_state(), new_state);
238 }
deadbeeffaac4972015-11-12 15:33:07 -0800239 void OnAddStream(MediaStreamInterface* media_stream) override {
240 media_stream->RegisterObserver(this);
deadbeefaf1b59c2015-10-15 12:08:41 -0700241 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
242 const std::string id = media_stream->GetVideoTracks()[i]->id();
243 ASSERT_TRUE(fake_video_renderers_.find(id) ==
244 fake_video_renderers_.end());
deadbeefc9be0072015-12-14 18:27:57 -0800245 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
246 media_stream->GetVideoTracks()[i]));
deadbeefaf1b59c2015-10-15 12:08:41 -0700247 }
248 }
deadbeeffaac4972015-11-12 15:33:07 -0800249 void OnRemoveStream(MediaStreamInterface* media_stream) override {}
deadbeefaf1b59c2015-10-15 12:08:41 -0700250 void OnRenegotiationNeeded() override {}
251 void OnIceConnectionChange(
252 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
253 EXPECT_EQ(pc()->ice_connection_state(), new_state);
254 }
255 void OnIceGatheringChange(
256 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
257 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
258 }
259 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
260 LOG(INFO) << id_ << "OnIceCandidate";
261
262 std::string ice_sdp;
263 EXPECT_TRUE(candidate->ToString(&ice_sdp));
264 if (signaling_message_receiver_ == nullptr) {
265 // Remote party may be deleted.
266 return;
267 }
268 signaling_message_receiver_->ReceiveIceMessage(
269 candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
270 }
271
deadbeeffaac4972015-11-12 15:33:07 -0800272 // MediaStreamInterface callback
273 void OnChanged() override {
274 // Track added or removed from MediaStream, so update our renderers.
275 rtc::scoped_refptr<StreamCollectionInterface> remote_streams =
276 pc()->remote_streams();
277 // Remove renderers for tracks that were removed.
278 for (auto it = fake_video_renderers_.begin();
279 it != fake_video_renderers_.end();) {
280 if (remote_streams->FindVideoTrack(it->first) == nullptr) {
deadbeefc9be0072015-12-14 18:27:57 -0800281 auto to_remove = it++;
282 removed_fake_video_renderers_.push_back(std::move(to_remove->second));
283 fake_video_renderers_.erase(to_remove);
deadbeeffaac4972015-11-12 15:33:07 -0800284 } else {
285 ++it;
286 }
287 }
288 // Create renderers for new video tracks.
289 for (size_t stream_index = 0; stream_index < remote_streams->count();
290 ++stream_index) {
291 MediaStreamInterface* remote_stream = remote_streams->at(stream_index);
292 for (size_t track_index = 0;
293 track_index < remote_stream->GetVideoTracks().size();
294 ++track_index) {
295 const std::string id =
296 remote_stream->GetVideoTracks()[track_index]->id();
297 if (fake_video_renderers_.find(id) != fake_video_renderers_.end()) {
298 continue;
299 }
deadbeefc9be0072015-12-14 18:27:57 -0800300 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
301 remote_stream->GetVideoTracks()[track_index]));
deadbeeffaac4972015-11-12 15:33:07 -0800302 }
303 }
304 }
305
deadbeefaf1b59c2015-10-15 12:08:41 -0700306 void SetVideoConstraints(const webrtc::FakeConstraints& video_constraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307 video_constraints_ = video_constraint;
308 }
309
310 void AddMediaStream(bool audio, bool video) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700311 std::string stream_label =
312 kStreamLabelBase +
313 rtc::ToString<int>(static_cast<int>(pc()->local_streams()->count()));
deadbeeffaac4972015-11-12 15:33:07 -0800314 rtc::scoped_refptr<MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000315 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316
317 if (audio && can_receive_audio()) {
deadbeeffac06552015-11-25 11:26:01 -0800318 stream->AddTrack(CreateLocalAudioTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319 }
320 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000321 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322 }
323
deadbeefaf1b59c2015-10-15 12:08:41 -0700324 EXPECT_TRUE(pc()->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325 }
326
deadbeefaf1b59c2015-10-15 12:08:41 -0700327 size_t NumberOfLocalMediaStreams() { return pc()->local_streams()->count(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328
329 bool SessionActive() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700330 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331 }
332
deadbeeffaac4972015-11-12 15:33:07 -0800333 // Automatically add a stream when receiving an offer, if we don't have one.
334 // Defaults to true.
335 void set_auto_add_stream(bool auto_add_stream) {
336 auto_add_stream_ = auto_add_stream;
337 }
338
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339 void set_signaling_message_receiver(
deadbeefaf1b59c2015-10-15 12:08:41 -0700340 SignalingMessageReceiver* signaling_message_receiver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341 signaling_message_receiver_ = signaling_message_receiver;
342 }
343
344 void EnableVideoDecoderFactory() {
345 video_decoder_factory_enabled_ = true;
346 fake_video_decoder_factory_->AddSupportedVideoCodecType(
347 webrtc::kVideoCodecVP8);
348 }
349
deadbeefaf1b59c2015-10-15 12:08:41 -0700350 void IceRestart() {
htaaac2dea2016-03-10 13:35:55 -0800351 offer_answer_constraints_.SetMandatoryIceRestart(true);
352 offer_answer_options_.ice_restart = true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700353 SetExpectIceRestart(true);
354 }
355
356 void SetExpectIceRestart(bool expect_restart) {
357 expect_ice_restart_ = expect_restart;
358 }
359
360 bool ExpectIceRestart() const { return expect_ice_restart_; }
361
362 void SetReceiveAudioVideo(bool audio, bool video) {
363 SetReceiveAudio(audio);
364 SetReceiveVideo(video);
365 ASSERT_EQ(audio, can_receive_audio());
366 ASSERT_EQ(video, can_receive_video());
367 }
368
369 void SetReceiveAudio(bool audio) {
370 if (audio && can_receive_audio())
371 return;
htaaac2dea2016-03-10 13:35:55 -0800372 offer_answer_constraints_.SetMandatoryReceiveAudio(audio);
373 offer_answer_options_.offer_to_receive_audio = audio ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700374 }
375
376 void SetReceiveVideo(bool video) {
377 if (video && can_receive_video())
378 return;
htaaac2dea2016-03-10 13:35:55 -0800379 offer_answer_constraints_.SetMandatoryReceiveVideo(video);
380 offer_answer_options_.offer_to_receive_video = video ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700381 }
382
383 void RemoveMsidFromReceivedSdp(bool remove) { remove_msid_ = remove; }
384
385 void RemoveSdesCryptoFromReceivedSdp(bool remove) { remove_sdes_ = remove; }
386
387 void RemoveBundleFromReceivedSdp(bool remove) { remove_bundle_ = remove; }
388
389 bool can_receive_audio() {
390 bool value;
htaaac2dea2016-03-10 13:35:55 -0800391 if (prefer_constraint_apis_) {
392 if (webrtc::FindConstraint(
393 &offer_answer_constraints_,
394 MediaConstraintsInterface::kOfferToReceiveAudio, &value,
395 nullptr)) {
396 return value;
397 }
398 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700399 }
htaaac2dea2016-03-10 13:35:55 -0800400 return offer_answer_options_.offer_to_receive_audio > 0 ||
401 offer_answer_options_.offer_to_receive_audio ==
402 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700403 }
404
405 bool can_receive_video() {
406 bool value;
htaaac2dea2016-03-10 13:35:55 -0800407 if (prefer_constraint_apis_) {
408 if (webrtc::FindConstraint(
409 &offer_answer_constraints_,
410 MediaConstraintsInterface::kOfferToReceiveVideo, &value,
411 nullptr)) {
412 return value;
413 }
414 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700415 }
htaaac2dea2016-03-10 13:35:55 -0800416 return offer_answer_options_.offer_to_receive_video > 0 ||
417 offer_answer_options_.offer_to_receive_video ==
418 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700419 }
420
deadbeefaf1b59c2015-10-15 12:08:41 -0700421 void OnDataChannel(DataChannelInterface* data_channel) override {
422 LOG(INFO) << id_ << "OnDataChannel";
423 data_channel_ = data_channel;
424 data_observer_.reset(new MockDataChannelObserver(data_channel));
425 }
426
427 void CreateDataChannel() {
428 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, nullptr);
429 ASSERT_TRUE(data_channel_.get() != nullptr);
430 data_observer_.reset(new MockDataChannelObserver(data_channel_));
431 }
432
deadbeeffac06552015-11-25 11:26:01 -0800433 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack(
434 const std::string& stream_label) {
435 FakeConstraints constraints;
436 // Disable highpass filter so that we can get all the test audio frames.
437 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
438 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
439 peer_connection_factory_->CreateAudioSource(&constraints);
440 // TODO(perkj): Test audio source when it is implemented. Currently audio
441 // always use the default input.
442 std::string label = stream_label + kAudioTrackLabelBase;
443 return peer_connection_factory_->CreateAudioTrack(label, source);
444 }
445
446 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack(
447 const std::string& stream_label) {
448 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
449 FakeConstraints source_constraints = video_constraints_;
450 source_constraints.SetMandatoryMaxFrameRate(10);
451
452 cricket::FakeVideoCapturer* fake_capturer =
453 new webrtc::FakePeriodicVideoCapturer();
454 video_capturers_.push_back(fake_capturer);
perkja3ede6c2016-03-08 01:27:48 +0100455 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
deadbeeffac06552015-11-25 11:26:01 -0800456 peer_connection_factory_->CreateVideoSource(fake_capturer,
457 &source_constraints);
458 std::string label = stream_label + kVideoTrackLabelBase;
459 return peer_connection_factory_->CreateVideoTrack(label, source);
460 }
461
deadbeefaf1b59c2015-10-15 12:08:41 -0700462 DataChannelInterface* data_channel() { return data_channel_; }
463 const MockDataChannelObserver* data_observer() const {
464 return data_observer_.get();
465 }
466
hta6b4f8392016-03-10 00:24:31 -0800467 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
deadbeefaf1b59c2015-10-15 12:08:41 -0700468
469 void StopVideoCapturers() {
470 for (std::vector<cricket::VideoCapturer*>::iterator it =
471 video_capturers_.begin();
472 it != video_capturers_.end(); ++it) {
473 (*it)->Stop();
474 }
475 }
476
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 bool AudioFramesReceivedCheck(int number_of_frames) const {
478 return number_of_frames <= fake_audio_capture_module_->frames_received();
479 }
480
deadbeefc9be0072015-12-14 18:27:57 -0800481 int audio_frames_received() const {
482 return fake_audio_capture_module_->frames_received();
483 }
484
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 bool VideoFramesReceivedCheck(int number_of_frames) {
486 if (video_decoder_factory_enabled_) {
487 const std::vector<FakeWebRtcVideoDecoder*>& decoders
488 = fake_video_decoder_factory_->decoders();
489 if (decoders.empty()) {
490 return number_of_frames <= 0;
491 }
hta6b4f8392016-03-10 00:24:31 -0800492 // Note - this checks that EACH decoder has the requisite number
493 // of frames. The video_frames_received() function sums them.
deadbeefc9be0072015-12-14 18:27:57 -0800494 for (FakeWebRtcVideoDecoder* decoder : decoders) {
495 if (number_of_frames > decoder->GetNumFramesReceived()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496 return false;
497 }
498 }
499 return true;
500 } else {
501 if (fake_video_renderers_.empty()) {
502 return number_of_frames <= 0;
503 }
504
deadbeefc9be0072015-12-14 18:27:57 -0800505 for (const auto& pair : fake_video_renderers_) {
506 if (number_of_frames > pair.second->num_rendered_frames()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507 return false;
508 }
509 }
510 return true;
511 }
512 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700513
deadbeefc9be0072015-12-14 18:27:57 -0800514 int video_frames_received() const {
515 int total = 0;
516 if (video_decoder_factory_enabled_) {
517 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
518 fake_video_decoder_factory_->decoders();
519 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
520 total += decoder->GetNumFramesReceived();
521 }
522 } else {
523 for (const auto& pair : fake_video_renderers_) {
524 total += pair.second->num_rendered_frames();
525 }
526 for (const auto& renderer : removed_fake_video_renderers_) {
527 total += renderer->num_rendered_frames();
528 }
529 }
530 return total;
531 }
532
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533 // Verify the CreateDtmfSender interface
534 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000535 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
536 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537
538 // We can't create a DTMF sender with an invalid audio track or a non local
539 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700540 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000541 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700542 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
543 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544
545 // We should be able to create a DTMF sender from a local track.
546 webrtc::AudioTrackInterface* localtrack =
547 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
548 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700549 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 dtmf_sender->RegisterObserver(observer.get());
551
552 // Test the DtmfSender object just created.
553 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
554 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
555
556 // We don't need to verify that the DTMF tones are actually sent out because
557 // that is already covered by the tests of the lower level components.
558
559 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
560 std::vector<std::string> tones;
561 tones.push_back("1");
562 tones.push_back("a");
563 tones.push_back("");
564 observer->Verify(tones);
565
566 dtmf_sender->UnregisterObserver();
567 }
568
569 // Verifies that the SessionDescription have rejected the appropriate media
570 // content.
571 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700572 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
573 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 const cricket::SessionDescription* remote_desc =
575 peer_connection_->remote_description()->description();
576 const cricket::SessionDescription* local_desc =
577 peer_connection_->local_description()->description();
578
579 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
580 if (remote_audio_content) {
581 const ContentInfo* audio_content =
582 GetFirstAudioContent(local_desc);
583 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
584 }
585
586 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
587 if (remote_video_content) {
588 const ContentInfo* video_content =
589 GetFirstVideoContent(local_desc);
590 EXPECT_EQ(can_receive_video(), !video_content->rejected);
591 }
592 }
593
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700595 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596 const cricket::SessionDescription* desc =
597 peer_connection_->local_description()->description();
598 const cricket::ContentInfos& contents = desc->contents();
599
600 for (size_t index = 0; index < contents.size(); ++index) {
601 if (contents[index].rejected)
602 continue;
603 const cricket::TransportDescription* transport_desc =
604 desc->GetTransportDescriptionByName(contents[index].name);
605
606 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000607 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 if (ufragpair_it == ice_ufrag_pwd_.end()) {
609 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000610 ice_ufrag_pwd_[static_cast<int>(index)] =
611 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 } else if (ExpectIceRestart()) {
613 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
614 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
615 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
616 } else {
617 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
618 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
619 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
620 }
621 }
622 }
623
624 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000625 rtc::scoped_refptr<MockStatsObserver>
626 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000627 EXPECT_TRUE(peer_connection_->GetStats(
628 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700630 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 return observer->AudioOutputLevel();
632 }
633
634 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000635 rtc::scoped_refptr<MockStatsObserver>
636 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000637 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700638 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700640 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 return observer->AudioInputLevel();
642 }
643
644 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000645 rtc::scoped_refptr<MockStatsObserver>
646 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000647 EXPECT_TRUE(peer_connection_->GetStats(
648 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700650 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651 return observer->BytesReceived();
652 }
653
654 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000655 rtc::scoped_refptr<MockStatsObserver>
656 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000657 EXPECT_TRUE(peer_connection_->GetStats(
658 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700660 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661 return observer->BytesSent();
662 }
663
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000664 int GetAvailableReceivedBandwidthStats() {
665 rtc::scoped_refptr<MockStatsObserver>
666 observer(new rtc::RefCountedObject<MockStatsObserver>());
667 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700668 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000669 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700670 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000671 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000672 return bw;
673 }
674
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000675 std::string GetDtlsCipherStats() {
676 rtc::scoped_refptr<MockStatsObserver>
677 observer(new rtc::RefCountedObject<MockStatsObserver>());
678 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700679 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000680 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700681 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000682 return observer->DtlsCipher();
683 }
684
685 std::string GetSrtpCipherStats() {
686 rtc::scoped_refptr<MockStatsObserver>
687 observer(new rtc::RefCountedObject<MockStatsObserver>());
688 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700689 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000690 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700691 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000692 return observer->SrtpCipher();
693 }
694
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 int rendered_width() {
696 EXPECT_FALSE(fake_video_renderers_.empty());
697 return fake_video_renderers_.empty() ? 1 :
698 fake_video_renderers_.begin()->second->width();
699 }
700
701 int rendered_height() {
702 EXPECT_FALSE(fake_video_renderers_.empty());
703 return fake_video_renderers_.empty() ? 1 :
704 fake_video_renderers_.begin()->second->height();
705 }
706
707 size_t number_of_remote_streams() {
708 if (!pc())
709 return 0;
710 return pc()->remote_streams()->count();
711 }
712
hta6b4f8392016-03-10 00:24:31 -0800713 StreamCollectionInterface* remote_streams() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 if (!pc()) {
715 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700716 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 }
718 return pc()->remote_streams();
719 }
720
721 StreamCollectionInterface* local_streams() {
722 if (!pc()) {
723 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700724 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 }
726 return pc()->local_streams();
727 }
728
hta6b4f8392016-03-10 00:24:31 -0800729 bool HasLocalAudioTrack() { return StreamsHaveAudioTrack(local_streams()); }
730
731 bool HasLocalVideoTrack() { return StreamsHaveVideoTrack(local_streams()); }
732
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
734 return pc()->signaling_state();
735 }
736
737 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
738 return pc()->ice_connection_state();
739 }
740
741 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
742 return pc()->ice_gathering_state();
743 }
744
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 private:
746 class DummyDtmfObserver : public DtmfSenderObserverInterface {
747 public:
748 DummyDtmfObserver() : completed_(false) {}
749
750 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700751 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752 tones_.push_back(tone);
753 if (tone.empty()) {
754 completed_ = true;
755 }
756 }
757
758 void Verify(const std::vector<std::string>& tones) const {
759 ASSERT_TRUE(tones_.size() == tones.size());
760 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
761 }
762
763 bool completed() const { return completed_; }
764
765 private:
766 bool completed_;
767 std::vector<std::string> tones_;
768 };
769
deadbeefaf1b59c2015-10-15 12:08:41 -0700770 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
771
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800772 bool Init(
773 const MediaConstraintsInterface* constraints,
774 const PeerConnectionFactory::Options* options,
htaaac2dea2016-03-10 13:35:55 -0800775 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store,
776 bool prefer_constraint_apis) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700777 EXPECT_TRUE(!peer_connection_);
778 EXPECT_TRUE(!peer_connection_factory_);
htaaac2dea2016-03-10 13:35:55 -0800779 if (!prefer_constraint_apis) {
780 EXPECT_TRUE(!constraints);
781 }
782 prefer_constraint_apis_ = prefer_constraint_apis;
783
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800784 rtc::scoped_ptr<cricket::PortAllocator> port_allocator(
785 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
deadbeefaf1b59c2015-10-15 12:08:41 -0700786 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
787
788 if (fake_audio_capture_module_ == nullptr) {
789 return false;
790 }
791 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
792 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
793 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
794 rtc::Thread::Current(), rtc::Thread::Current(),
795 fake_audio_capture_module_, fake_video_encoder_factory_,
796 fake_video_decoder_factory_);
797 if (!peer_connection_factory_) {
798 return false;
799 }
800 if (options) {
801 peer_connection_factory_->SetOptions(*options);
802 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800803 peer_connection_ = CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800804 std::move(port_allocator), constraints, std::move(dtls_identity_store));
deadbeefaf1b59c2015-10-15 12:08:41 -0700805 return peer_connection_.get() != nullptr;
806 }
807
deadbeefaf1b59c2015-10-15 12:08:41 -0700808 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800809 rtc::scoped_ptr<cricket::PortAllocator> port_allocator,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800810 const MediaConstraintsInterface* constraints,
811 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800812 // CreatePeerConnection with RTCConfiguration.
813 webrtc::PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 webrtc::PeerConnectionInterface::IceServer ice_server;
815 ice_server.uri = "stun:stun.l.google.com:19302";
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800816 config.servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000817
deadbeefaf1b59c2015-10-15 12:08:41 -0700818 return peer_connection_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800819 config, constraints, std::move(port_allocator),
820 std::move(dtls_identity_store), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 }
822
823 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700824 LOG(INFO) << id_ << "HandleIncomingOffer ";
deadbeeffaac4972015-11-12 15:33:07 -0800825 if (NumberOfLocalMediaStreams() == 0 && auto_add_stream_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826 // If we are not sending any streams ourselves it is time to add some.
827 AddMediaStream(true, true);
828 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000829 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700830 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000831 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000832 rtc::scoped_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700833 EXPECT_TRUE(DoCreateAnswer(&answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 std::string sdp;
835 EXPECT_TRUE(answer->ToString(&sdp));
836 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
deadbeefaf1b59c2015-10-15 12:08:41 -0700837 if (signaling_message_receiver_) {
838 signaling_message_receiver_->ReceiveSdpMessage(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 webrtc::SessionDescriptionInterface::kAnswer, sdp);
840 }
841 }
842
843 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700844 LOG(INFO) << id_ << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000845 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700846 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
848 }
849
kwiberg2bbff992016-03-16 11:03:04 -0700850 bool DoCreateOfferAnswer(rtc::scoped_ptr<SessionDescriptionInterface>* desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000852 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
853 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 MockCreateSessionDescriptionObserver>());
htaaac2dea2016-03-10 13:35:55 -0800855 if (prefer_constraint_apis_) {
856 if (offer) {
857 pc()->CreateOffer(observer, &offer_answer_constraints_);
858 } else {
859 pc()->CreateAnswer(observer, &offer_answer_constraints_);
860 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 } else {
htaaac2dea2016-03-10 13:35:55 -0800862 if (offer) {
863 pc()->CreateOffer(observer, offer_answer_options_);
864 } else {
865 pc()->CreateAnswer(observer, offer_answer_options_);
866 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 }
868 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
kwiberg2bbff992016-03-16 11:03:04 -0700869 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 if (observer->result() && ExpectIceRestart()) {
871 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
872 }
873 return observer->result();
874 }
875
kwiberg2bbff992016-03-16 11:03:04 -0700876 bool DoCreateOffer(rtc::scoped_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 return DoCreateOfferAnswer(desc, true);
878 }
879
kwiberg2bbff992016-03-16 11:03:04 -0700880 bool DoCreateAnswer(rtc::scoped_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881 return DoCreateOfferAnswer(desc, false);
882 }
883
884 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000885 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
886 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700888 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 pc()->SetLocalDescription(observer, desc);
890 // Ignore the observer result. If we wait for the result with
891 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
892 // before the offer which is an error.
893 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000894 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 // ProcessMessages waits at least 1ms but processes all messages before
896 // returning. Since this test is synchronous and send messages to the remote
897 // peer whenever a callback is invoked, this can lead to messages being
898 // sent to the remote peer in the wrong order.
899 // TODO(perkj): Find a way to check the result without risking that the
900 // order of sent messages are changed. Ex- by posting all messages that are
901 // sent to the remote peer.
902 return true;
903 }
904
905 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000906 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
907 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700909 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910 pc()->SetRemoteDescription(observer, desc);
911 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
912 return observer->result();
913 }
914
915 // This modifies all received SDP messages before they are processed.
916 void FilterIncomingSdpMessage(std::string* sdp) {
917 if (remove_msid_) {
918 const char kSdpSsrcAttribute[] = "a=ssrc:";
919 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
920 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
921 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
922 }
923 if (remove_bundle_) {
924 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
925 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
926 }
927 if (remove_sdes_) {
928 const char kSdpSdesCryptoAttribute[] = "a=crypto";
929 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
930 }
931 }
932
deadbeefaf1b59c2015-10-15 12:08:41 -0700933 std::string id_;
934
deadbeefaf1b59c2015-10-15 12:08:41 -0700935 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
936 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
937 peer_connection_factory_;
938
htaaac2dea2016-03-10 13:35:55 -0800939 bool prefer_constraint_apis_ = true;
deadbeeffaac4972015-11-12 15:33:07 -0800940 bool auto_add_stream_ = true;
941
deadbeefaf1b59c2015-10-15 12:08:41 -0700942 typedef std::pair<std::string, std::string> IceUfragPwdPair;
943 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
944 bool expect_ice_restart_ = false;
945
deadbeefc9be0072015-12-14 18:27:57 -0800946 // Needed to keep track of number of frames sent.
deadbeefaf1b59c2015-10-15 12:08:41 -0700947 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
948 // Needed to keep track of number of frames received.
deadbeefc9be0072015-12-14 18:27:57 -0800949 std::map<std::string, rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
950 fake_video_renderers_;
951 // Needed to ensure frames aren't received for removed tracks.
952 std::vector<rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
953 removed_fake_video_renderers_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700954 // Needed to keep track of number of frames received when external decoder
955 // used.
956 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
957 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
958 bool video_decoder_factory_enabled_ = false;
959 webrtc::FakeConstraints video_constraints_;
960
961 // For remote peer communication.
962 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
963
964 // Store references to the video capturers we've created, so that we can stop
965 // them, if required.
966 std::vector<cricket::VideoCapturer*> video_capturers_;
967
htaaac2dea2016-03-10 13:35:55 -0800968 webrtc::FakeConstraints offer_answer_constraints_;
969 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700970 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
971 bool remove_bundle_ =
972 false; // True if bundle should be removed in received SDP.
973 bool remove_sdes_ =
974 false; // True if a=crypto should be removed in received SDP.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000976 rtc::scoped_refptr<DataChannelInterface> data_channel_;
977 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000978};
979
deadbeef7c73bdb2015-12-10 15:10:44 -0800980class P2PTestConductor : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981 public:
deadbeef7c73bdb2015-12-10 15:10:44 -0800982 P2PTestConductor()
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000983 : pss_(new rtc::PhysicalSocketServer),
984 ss_(new rtc::VirtualSocketServer(pss_.get())),
985 ss_scope_(ss_.get()) {}
986
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987 bool SessionActive() {
988 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000989 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000990 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000991
hta6b4f8392016-03-10 00:24:31 -0800992 // Return true if the number of frames provided have been received
993 // on the video and audio tracks provided.
994 bool FramesHaveArrived(int audio_frames_to_receive,
995 int video_frames_to_receive) {
996 bool all_good = true;
997 if (initiating_client_->HasLocalAudioTrack() &&
998 receiving_client_->can_receive_audio()) {
999 all_good &=
1000 receiving_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1001 }
1002 if (initiating_client_->HasLocalVideoTrack() &&
1003 receiving_client_->can_receive_video()) {
1004 all_good &=
1005 receiving_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1006 }
1007 if (receiving_client_->HasLocalAudioTrack() &&
1008 initiating_client_->can_receive_audio()) {
1009 all_good &=
1010 initiating_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1011 }
1012 if (receiving_client_->HasLocalVideoTrack() &&
1013 initiating_client_->can_receive_video()) {
1014 all_good &=
1015 initiating_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1016 }
1017 return all_good;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018 }
hta6b4f8392016-03-10 00:24:31 -08001019
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020 void VerifyDtmf() {
1021 initiating_client_->VerifyDtmf();
1022 receiving_client_->VerifyDtmf();
1023 }
1024
1025 void TestUpdateOfferWithRejectedContent() {
deadbeefc9be0072015-12-14 18:27:57 -08001026 // Renegotiate, rejecting the video m-line.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027 initiating_client_->Negotiate(true, false);
deadbeefc9be0072015-12-14 18:27:57 -08001028 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1029
1030 int pc1_audio_received = initiating_client_->audio_frames_received();
1031 int pc1_video_received = initiating_client_->video_frames_received();
1032 int pc2_audio_received = receiving_client_->audio_frames_received();
1033 int pc2_video_received = receiving_client_->video_frames_received();
1034
1035 // Wait for some additional audio frames to be received.
1036 EXPECT_TRUE_WAIT(initiating_client_->AudioFramesReceivedCheck(
1037 pc1_audio_received + kEndAudioFrameCount) &&
1038 receiving_client_->AudioFramesReceivedCheck(
1039 pc2_audio_received + kEndAudioFrameCount),
1040 kMaxWaitForFramesMs);
1041
1042 // During this time, we shouldn't have received any additional video frames
1043 // for the rejected video tracks.
1044 EXPECT_EQ(pc1_video_received, initiating_client_->video_frames_received());
1045 EXPECT_EQ(pc2_video_received, receiving_client_->video_frames_received());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 }
1047
1048 void VerifyRenderedSize(int width, int height) {
1049 EXPECT_EQ(width, receiving_client()->rendered_width());
1050 EXPECT_EQ(height, receiving_client()->rendered_height());
1051 EXPECT_EQ(width, initializing_client()->rendered_width());
1052 EXPECT_EQ(height, initializing_client()->rendered_height());
1053 }
1054
1055 void VerifySessionDescriptions() {
1056 initiating_client_->VerifyRejectedMediaInSessionDescription();
1057 receiving_client_->VerifyRejectedMediaInSessionDescription();
1058 initiating_client_->VerifyLocalIceUfragAndPassword();
1059 receiving_client_->VerifyLocalIceUfragAndPassword();
1060 }
1061
deadbeef7c73bdb2015-12-10 15:10:44 -08001062 ~P2PTestConductor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001063 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001064 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001065 }
1066 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001067 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001068 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001069 }
1070
deadbeefaf1b59c2015-10-15 12:08:41 -07001071 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001072
1073 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1074 MediaConstraintsInterface* recv_constraints) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001075 return CreateTestClients(init_constraints, nullptr, recv_constraints,
1076 nullptr);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001077 }
1078
htaaac2dea2016-03-10 13:35:55 -08001079 bool CreateTestClientsThatPreferNoConstraints() {
1080 initiating_client_.reset(
1081 PeerConnectionTestClient::CreateClientPreferNoConstraints("Caller: ",
1082 nullptr));
1083 receiving_client_.reset(
1084 PeerConnectionTestClient::CreateClientPreferNoConstraints("Callee: ",
1085 nullptr));
1086 if (!initiating_client_ || !receiving_client_) {
1087 return false;
1088 }
1089 // Remember the choice for possible later resets of the clients.
1090 prefer_constraint_apis_ = false;
1091 SetSignalingReceivers();
1092 return true;
1093 }
1094
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001095 void SetSignalingReceivers() {
1096 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
1097 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1098 }
1099
Joachim Bauch04e5b492015-05-29 09:40:39 +02001100 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1101 PeerConnectionFactory::Options* init_options,
1102 MediaConstraintsInterface* recv_constraints,
1103 PeerConnectionFactory::Options* recv_options) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001104 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
1105 "Caller: ", init_constraints, init_options));
1106 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
1107 "Callee: ", recv_constraints, recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001108 if (!initiating_client_ || !receiving_client_) {
1109 return false;
1110 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001111 SetSignalingReceivers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001112 return true;
1113 }
1114
1115 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1116 const webrtc::FakeConstraints& recv_constraints) {
1117 initiating_client_->SetVideoConstraints(init_constraints);
1118 receiving_client_->SetVideoConstraints(recv_constraints);
1119 }
1120
1121 void EnableVideoDecoderFactory() {
1122 initiating_client_->EnableVideoDecoderFactory();
1123 receiving_client_->EnableVideoDecoderFactory();
1124 }
1125
1126 // This test sets up a call between two parties. Both parties send static
1127 // frames to each other. Once the test is finished the number of sent frames
1128 // is compared to the number of received frames.
1129 void LocalP2PTest() {
1130 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1131 initiating_client_->AddMediaStream(true, true);
1132 }
1133 initiating_client_->Negotiate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001134 // Assert true is used here since next tests are guaranteed to fail and
1135 // would eat up 5 seconds.
1136 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1137 VerifySessionDescriptions();
1138
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001139 int audio_frame_count = kEndAudioFrameCount;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001140 int video_frame_count = kEndVideoFrameCount;
hta6b4f8392016-03-10 00:24:31 -08001141 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1142
1143 if ((!initiating_client_->can_receive_audio() &&
1144 !initiating_client_->can_receive_video()) ||
1145 (!receiving_client_->can_receive_audio() &&
1146 !receiving_client_->can_receive_video())) {
1147 // Neither audio nor video will flow, so connections won't be
1148 // established. There's nothing more to check.
1149 // TODO(hta): Check connection if there's a data channel.
1150 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001151 }
1152
hta6b4f8392016-03-10 00:24:31 -08001153 // Audio or video is expected to flow, so both clients should reach the
1154 // Connected state, and the offerer (ICE controller) should proceed to
1155 // Completed.
1156 // Note: These tests have been observed to fail under heavy load at
1157 // shorter timeouts, so they may be flaky.
1158 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1159 initiating_client_->ice_connection_state(),
1160 kMaxWaitForFramesMs);
1161 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1162 receiving_client_->ice_connection_state(),
1163 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001164
hta6b4f8392016-03-10 00:24:31 -08001165 // The ICE gathering state should end up in kIceGatheringComplete,
1166 // but there's a bug that prevents this at the moment, and the state
1167 // machine is being updated by the WEBRTC WG.
1168 // TODO(hta): Update this check when spec revisions finish.
1169 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1170 initiating_client_->ice_gathering_state());
1171 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1172 receiving_client_->ice_gathering_state(),
1173 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001174
hta6b4f8392016-03-10 00:24:31 -08001175 // Check that the expected number of frames have arrived.
1176 EXPECT_TRUE_WAIT(FramesHaveArrived(audio_frame_count, video_frame_count),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177 kMaxWaitForFramesMs);
1178 }
1179
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001180 void SetupAndVerifyDtlsCall() {
1181 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1182 FakeConstraints setup_constraints;
1183 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1184 true);
1185 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1186 LocalP2PTest();
1187 VerifyRenderedSize(640, 480);
1188 }
1189
1190 PeerConnectionTestClient* CreateDtlsClientWithAlternateKey() {
1191 FakeConstraints setup_constraints;
1192 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1193 true);
1194
1195 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
1196 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
1197 : nullptr);
1198 dtls_identity_store->use_alternate_key();
1199
1200 // Make sure the new client is using a different certificate.
1201 return PeerConnectionTestClient::CreateClientWithDtlsIdentityStore(
kwiberg0eb15ed2015-12-17 03:04:15 -08001202 "New Peer: ", &setup_constraints, nullptr,
htaaac2dea2016-03-10 13:35:55 -08001203 std::move(dtls_identity_store), prefer_constraint_apis_);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001204 }
1205
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001206 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1207 // Messages may get lost on the unreliable DataChannel, so we send multiple
1208 // times to avoid test flakiness.
1209 static const size_t kSendAttempts = 5;
1210
1211 for (size_t i = 0; i < kSendAttempts; ++i) {
1212 dc->Send(DataBuffer(data));
1213 }
1214 }
1215
deadbeefaf1b59c2015-10-15 12:08:41 -07001216 PeerConnectionTestClient* initializing_client() {
1217 return initiating_client_.get();
1218 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001219
1220 // Set the |initiating_client_| to the |client| passed in and return the
1221 // original |initiating_client_|.
1222 PeerConnectionTestClient* set_initializing_client(
1223 PeerConnectionTestClient* client) {
1224 PeerConnectionTestClient* old = initiating_client_.release();
1225 initiating_client_.reset(client);
1226 return old;
1227 }
1228
deadbeefaf1b59c2015-10-15 12:08:41 -07001229 PeerConnectionTestClient* receiving_client() {
1230 return receiving_client_.get();
1231 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001232
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001233 // Set the |receiving_client_| to the |client| passed in and return the
1234 // original |receiving_client_|.
1235 PeerConnectionTestClient* set_receiving_client(
1236 PeerConnectionTestClient* client) {
1237 PeerConnectionTestClient* old = receiving_client_.release();
1238 receiving_client_.reset(client);
1239 return old;
1240 }
1241
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001243 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1244 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1245 rtc::SocketServerScope ss_scope_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001246 rtc::scoped_ptr<PeerConnectionTestClient> initiating_client_;
1247 rtc::scoped_ptr<PeerConnectionTestClient> receiving_client_;
htaaac2dea2016-03-10 13:35:55 -08001248 bool prefer_constraint_apis_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001249};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001250
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001251// Disable for TSan v2, see
1252// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1253#if !defined(THREAD_SANITIZER)
1254
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001256// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1257// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001258TEST_F(P2PTestConductor, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001259 ASSERT_TRUE(CreateTestClients());
1260 LocalP2PTest();
1261 VerifyDtmf();
1262}
1263
1264// This test sets up a Jsep call between two parties and test that we can get a
1265// video aspect ratio of 16:9.
deadbeef7c73bdb2015-12-10 15:10:44 -08001266TEST_F(P2PTestConductor, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001267 ASSERT_TRUE(CreateTestClients());
1268 FakeConstraints constraint;
1269 double requested_ratio = 640.0/360;
1270 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1271 SetVideoConstraints(constraint, constraint);
1272 LocalP2PTest();
1273
1274 ASSERT_LE(0, initializing_client()->rendered_height());
1275 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001276 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001277 initializing_client()->rendered_height();
1278 EXPECT_LE(requested_ratio, initiating_video_ratio);
1279
1280 ASSERT_LE(0, receiving_client()->rendered_height());
1281 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001282 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283 receiving_client()->rendered_height();
1284 EXPECT_LE(requested_ratio, receiving_video_ratio);
1285}
1286
1287// This test sets up a Jsep call between two parties and test that the
1288// received video has a resolution of 1280*720.
1289// TODO(mallinath): Enable when
1290// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
deadbeef7c73bdb2015-12-10 15:10:44 -08001291TEST_F(P2PTestConductor, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001292 ASSERT_TRUE(CreateTestClients());
1293 FakeConstraints constraint;
1294 constraint.SetMandatoryMinWidth(1280);
1295 constraint.SetMandatoryMinHeight(720);
1296 SetVideoConstraints(constraint, constraint);
1297 LocalP2PTest();
1298 VerifyRenderedSize(1280, 720);
1299}
1300
1301// This test sets up a call between two endpoints that are configured to use
1302// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001303TEST_F(P2PTestConductor, LocalP2PTestDtls) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001304 SetupAndVerifyDtlsCall();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001305}
1306
hta6b4f8392016-03-10 00:24:31 -08001307// This test sets up an one-way call, with media only from initiator to
1308// responder.
1309TEST_F(P2PTestConductor, OneWayMediaCall) {
1310 ASSERT_TRUE(CreateTestClients());
1311 receiving_client()->set_auto_add_stream(false);
1312 LocalP2PTest();
1313}
1314
htaaac2dea2016-03-10 13:35:55 -08001315TEST_F(P2PTestConductor, OneWayMediaCallWithoutConstraints) {
1316 ASSERT_TRUE(CreateTestClientsThatPreferNoConstraints());
1317 receiving_client()->set_auto_add_stream(false);
1318 LocalP2PTest();
1319}
1320
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001321// This test sets up a audio call initially and then upgrades to audio/video,
1322// using DTLS.
deadbeef7c73bdb2015-12-10 15:10:44 -08001323TEST_F(P2PTestConductor, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001324 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001325 FakeConstraints setup_constraints;
1326 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1327 true);
1328 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1329 receiving_client()->SetReceiveAudioVideo(true, false);
1330 LocalP2PTest();
1331 receiving_client()->SetReceiveAudioVideo(true, true);
1332 receiving_client()->Negotiate();
1333}
1334
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001335// This test sets up a call transfer to a new caller with a different DTLS
1336// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001337TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCallee) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001338 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1339 SetupAndVerifyDtlsCall();
1340
1341 // Keeping the original peer around which will still send packets to the
1342 // receiving client. These SRTP packets will be dropped.
1343 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1344 set_initializing_client(CreateDtlsClientWithAlternateKey()));
1345 original_peer->pc()->Close();
1346
1347 SetSignalingReceivers();
1348 receiving_client()->SetExpectIceRestart(true);
1349 LocalP2PTest();
1350 VerifyRenderedSize(640, 480);
1351}
1352
guoweis46383312015-12-17 16:45:59 -08001353// This test sets up a non-bundle call and apply bundle during ICE restart. When
1354// bundle is in effect in the restart, the channel can successfully reset its
1355// DTLS-SRTP context.
1356TEST_F(P2PTestConductor, LocalP2PTestDtlsBundleInIceRestart) {
1357 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1358 FakeConstraints setup_constraints;
1359 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1360 true);
1361 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1362 receiving_client()->RemoveBundleFromReceivedSdp(true);
1363 LocalP2PTest();
1364 VerifyRenderedSize(640, 480);
1365
1366 initializing_client()->IceRestart();
1367 receiving_client()->SetExpectIceRestart(true);
1368 receiving_client()->RemoveBundleFromReceivedSdp(false);
1369 LocalP2PTest();
1370 VerifyRenderedSize(640, 480);
1371}
1372
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001373// This test sets up a call transfer to a new callee with a different DTLS
1374// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001375TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCaller) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001376 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1377 SetupAndVerifyDtlsCall();
1378
1379 // Keeping the original peer around which will still send packets to the
1380 // receiving client. These SRTP packets will be dropped.
1381 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1382 set_receiving_client(CreateDtlsClientWithAlternateKey()));
1383 original_peer->pc()->Close();
1384
1385 SetSignalingReceivers();
1386 initializing_client()->IceRestart();
1387 LocalP2PTest();
1388 VerifyRenderedSize(640, 480);
1389}
1390
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001391// This test sets up a call between two endpoints that are configured to use
1392// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1393// negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001394TEST_F(P2PTestConductor, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001395 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001396 FakeConstraints setup_constraints;
1397 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1398 true);
1399 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1400 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1401 LocalP2PTest();
1402 VerifyRenderedSize(640, 480);
1403}
1404
1405// This test sets up a Jsep call between two parties, and the callee only
1406// accept to receive video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001407TEST_F(P2PTestConductor, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001408 ASSERT_TRUE(CreateTestClients());
1409 receiving_client()->SetReceiveAudioVideo(false, true);
1410 LocalP2PTest();
1411}
1412
1413// This test sets up a Jsep call between two parties, and the callee only
1414// accept to receive audio.
deadbeef7c73bdb2015-12-10 15:10:44 -08001415TEST_F(P2PTestConductor, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001416 ASSERT_TRUE(CreateTestClients());
1417 receiving_client()->SetReceiveAudioVideo(true, false);
1418 LocalP2PTest();
1419}
1420
1421// This test sets up a Jsep call between two parties, and the callee reject both
1422// audio and video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001423TEST_F(P2PTestConductor, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001424 ASSERT_TRUE(CreateTestClients());
1425 receiving_client()->SetReceiveAudioVideo(false, false);
1426 LocalP2PTest();
1427}
1428
1429// This test sets up an audio and video call between two parties. After the call
1430// runs for a while (10 frames), the caller sends an update offer with video
1431// being rejected. Once the re-negotiation is done, the video flow should stop
1432// and the audio flow should continue.
deadbeefc9be0072015-12-14 18:27:57 -08001433TEST_F(P2PTestConductor, UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001434 ASSERT_TRUE(CreateTestClients());
1435 LocalP2PTest();
1436 TestUpdateOfferWithRejectedContent();
1437}
1438
1439// This test sets up a Jsep call between two parties. The MSID is removed from
1440// the SDP strings from the caller.
deadbeefc9be0072015-12-14 18:27:57 -08001441TEST_F(P2PTestConductor, LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001442 ASSERT_TRUE(CreateTestClients());
1443 receiving_client()->RemoveMsidFromReceivedSdp(true);
1444 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1445 // audio and video is muxed when MSID is disabled. Remove
1446 // SetRemoveBundleFromSdp once
1447 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1448 receiving_client()->RemoveBundleFromReceivedSdp(true);
1449 LocalP2PTest();
1450}
1451
1452// This test sets up a Jsep call between two parties and the initiating peer
1453// sends two steams.
1454// TODO(perkj): Disabled due to
1455// https://code.google.com/p/webrtc/issues/detail?id=1454
deadbeef7c73bdb2015-12-10 15:10:44 -08001456TEST_F(P2PTestConductor, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001457 ASSERT_TRUE(CreateTestClients());
1458 // Set optional video constraint to max 320pixels to decrease CPU usage.
1459 FakeConstraints constraint;
1460 constraint.SetOptionalMaxWidth(320);
1461 SetVideoConstraints(constraint, constraint);
1462 initializing_client()->AddMediaStream(true, true);
1463 initializing_client()->AddMediaStream(false, true);
1464 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1465 LocalP2PTest();
1466 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1467}
1468
1469// Test that we can receive the audio output level from a remote audio track.
deadbeef7c73bdb2015-12-10 15:10:44 -08001470TEST_F(P2PTestConductor, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001471 ASSERT_TRUE(CreateTestClients());
1472 LocalP2PTest();
1473
1474 StreamCollectionInterface* remote_streams =
1475 initializing_client()->remote_streams();
1476 ASSERT_GT(remote_streams->count(), 0u);
1477 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1478 MediaStreamTrackInterface* remote_audio_track =
1479 remote_streams->at(0)->GetAudioTracks()[0];
1480
1481 // Get the audio output level stats. Note that the level is not available
1482 // until a RTCP packet has been received.
1483 EXPECT_TRUE_WAIT(
1484 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1485 kMaxWaitForStatsMs);
1486}
1487
1488// Test that an audio input level is reported.
deadbeef7c73bdb2015-12-10 15:10:44 -08001489TEST_F(P2PTestConductor, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001490 ASSERT_TRUE(CreateTestClients());
1491 LocalP2PTest();
1492
1493 // Get the audio input level stats. The level should be available very
1494 // soon after the test starts.
1495 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1496 kMaxWaitForStatsMs);
1497}
1498
1499// Test that we can get incoming byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001500TEST_F(P2PTestConductor, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001501 ASSERT_TRUE(CreateTestClients());
1502 LocalP2PTest();
1503
1504 StreamCollectionInterface* remote_streams =
1505 initializing_client()->remote_streams();
1506 ASSERT_GT(remote_streams->count(), 0u);
1507 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1508 MediaStreamTrackInterface* remote_audio_track =
1509 remote_streams->at(0)->GetAudioTracks()[0];
1510 EXPECT_TRUE_WAIT(
1511 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1512 kMaxWaitForStatsMs);
1513
1514 MediaStreamTrackInterface* remote_video_track =
1515 remote_streams->at(0)->GetVideoTracks()[0];
1516 EXPECT_TRUE_WAIT(
1517 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1518 kMaxWaitForStatsMs);
1519}
1520
1521// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001522TEST_F(P2PTestConductor, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523 ASSERT_TRUE(CreateTestClients());
1524 LocalP2PTest();
1525
1526 StreamCollectionInterface* local_streams =
1527 initializing_client()->local_streams();
1528 ASSERT_GT(local_streams->count(), 0u);
1529 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1530 MediaStreamTrackInterface* local_audio_track =
1531 local_streams->at(0)->GetAudioTracks()[0];
1532 EXPECT_TRUE_WAIT(
1533 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1534 kMaxWaitForStatsMs);
1535
1536 MediaStreamTrackInterface* local_video_track =
1537 local_streams->at(0)->GetVideoTracks()[0];
1538 EXPECT_TRUE_WAIT(
1539 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1540 kMaxWaitForStatsMs);
1541}
1542
Joachim Bauch04e5b492015-05-29 09:40:39 +02001543// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001544TEST_F(P2PTestConductor, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001545 PeerConnectionFactory::Options init_options;
1546 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1547 PeerConnectionFactory::Options recv_options;
1548 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001549 ASSERT_TRUE(
1550 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001551 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1552 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1553 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001554 LocalP2PTest();
1555
torbjorng43166b82016-03-11 00:06:47 -08001556 EXPECT_TRUE_WAIT(
1557 rtc::SSLStreamAdapter::IsAcceptableCipher(
1558 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1559 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001560 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001561 initializing_client()->GetSrtpCipherStats(),
1562 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001563 EXPECT_EQ(1,
1564 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1565 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001566}
1567
1568// Test that DTLS 1.2 is used if both ends support it.
torbjorng79a5a832016-01-15 07:16:51 -08001569TEST_F(P2PTestConductor, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001570 PeerConnectionFactory::Options init_options;
1571 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1572 PeerConnectionFactory::Options recv_options;
1573 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001574 ASSERT_TRUE(
1575 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001576 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1577 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1578 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001579 LocalP2PTest();
1580
torbjorng43166b82016-03-11 00:06:47 -08001581 EXPECT_TRUE_WAIT(
1582 rtc::SSLStreamAdapter::IsAcceptableCipher(
1583 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1584 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001585 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001586 initializing_client()->GetSrtpCipherStats(),
1587 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001588 EXPECT_EQ(1,
1589 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1590 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001591}
1592
1593// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1594// received supports 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001595TEST_F(P2PTestConductor, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001596 PeerConnectionFactory::Options init_options;
1597 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1598 PeerConnectionFactory::Options recv_options;
1599 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001600 ASSERT_TRUE(
1601 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001602 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1603 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1604 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001605 LocalP2PTest();
1606
torbjorng43166b82016-03-11 00:06:47 -08001607 EXPECT_TRUE_WAIT(
1608 rtc::SSLStreamAdapter::IsAcceptableCipher(
1609 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1610 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001611 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001612 initializing_client()->GetSrtpCipherStats(),
1613 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001614 EXPECT_EQ(1,
1615 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1616 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001617}
1618
1619// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1620// received supports 1.2.
torbjorng43166b82016-03-11 00:06:47 -08001621TEST_F(P2PTestConductor, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001622 PeerConnectionFactory::Options init_options;
1623 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1624 PeerConnectionFactory::Options recv_options;
1625 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001626 ASSERT_TRUE(
1627 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001628 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1629 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1630 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001631 LocalP2PTest();
1632
torbjorng43166b82016-03-11 00:06:47 -08001633 EXPECT_TRUE_WAIT(
1634 rtc::SSLStreamAdapter::IsAcceptableCipher(
1635 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1636 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001637 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001638 initializing_client()->GetSrtpCipherStats(),
1639 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001640 EXPECT_EQ(1,
1641 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1642 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001643}
1644
deadbeefb5cb19b2015-11-23 16:39:12 -08001645// This test sets up a call between two parties with audio, video and an RTP
1646// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001647TEST_F(P2PTestConductor, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001648 FakeConstraints setup_constraints;
1649 setup_constraints.SetAllowRtpDataChannels();
1650 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1651 initializing_client()->CreateDataChannel();
1652 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001653 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1654 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1656 kMaxWaitMs);
1657 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1658 kMaxWaitMs);
1659
1660 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001661
1662 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001663 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1664 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001665
1666 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1668 kMaxWaitMs);
1669
1670 receiving_client()->data_channel()->Close();
1671 // Send new offer and answer.
1672 receiving_client()->Negotiate();
1673 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1674 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1675}
1676
deadbeefb5cb19b2015-11-23 16:39:12 -08001677// This test sets up a call between two parties with audio, video and an SCTP
1678// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001679TEST_F(P2PTestConductor, LocalP2PTestSctpDataChannel) {
deadbeefb5cb19b2015-11-23 16:39:12 -08001680 ASSERT_TRUE(CreateTestClients());
1681 initializing_client()->CreateDataChannel();
1682 LocalP2PTest();
1683 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1684 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
1685 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1686 kMaxWaitMs);
1687 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
1688
1689 std::string data = "hello world";
1690
1691 initializing_client()->data_channel()->Send(DataBuffer(data));
1692 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1693 kMaxWaitMs);
1694
1695 receiving_client()->data_channel()->Send(DataBuffer(data));
1696 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1697 kMaxWaitMs);
1698
1699 receiving_client()->data_channel()->Close();
deadbeef15887932015-12-14 19:32:34 -08001700 EXPECT_TRUE_WAIT(!initializing_client()->data_observer()->IsOpen(),
1701 kMaxWaitMs);
1702 EXPECT_TRUE_WAIT(!receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
deadbeefb5cb19b2015-11-23 16:39:12 -08001703}
1704
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001705// This test sets up a call between two parties and creates a data channel.
1706// The test tests that received data is buffered unless an observer has been
1707// registered.
1708// Rtp data channels can receive data before the underlying
1709// transport has detected that a channel is writable and thus data can be
1710// received before the data channel state changes to open. That is hard to test
1711// but the same buffering is used in that case.
deadbeef7c73bdb2015-12-10 15:10:44 -08001712TEST_F(P2PTestConductor, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713 FakeConstraints setup_constraints;
1714 setup_constraints.SetAllowRtpDataChannels();
1715 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1716 initializing_client()->CreateDataChannel();
1717 initializing_client()->Negotiate();
1718
deadbeefaf1b59c2015-10-15 12:08:41 -07001719 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1720 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1722 kMaxWaitMs);
1723 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1724 receiving_client()->data_channel()->state(), kMaxWaitMs);
1725
1726 // Unregister the existing observer.
1727 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001728
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001730 SendRtpData(initializing_client()->data_channel(), data);
1731
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732 // Wait a while to allow the sent data to arrive before an observer is
1733 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001734 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735
1736 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1737 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1738}
1739
1740// This test sets up a call between two parties with audio, video and but only
1741// the initiating client support data.
deadbeef7c73bdb2015-12-10 15:10:44 -08001742TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001743 FakeConstraints setup_constraints_1;
1744 setup_constraints_1.SetAllowRtpDataChannels();
1745 // Must disable DTLS to make negotiation succeed.
1746 setup_constraints_1.SetMandatory(
1747 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1748 FakeConstraints setup_constraints_2;
1749 setup_constraints_2.SetMandatory(
1750 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1751 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752 initializing_client()->CreateDataChannel();
1753 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001754 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 EXPECT_FALSE(receiving_client()->data_channel());
1756 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1757}
1758
1759// This test sets up a call between two parties with audio, video. When audio
1760// and video is setup and flowing and data channel is negotiated.
deadbeef7c73bdb2015-12-10 15:10:44 -08001761TEST_F(P2PTestConductor, AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001762 FakeConstraints setup_constraints;
1763 setup_constraints.SetAllowRtpDataChannels();
1764 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1765 LocalP2PTest();
1766 initializing_client()->CreateDataChannel();
1767 // Send new offer and answer.
1768 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07001769 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1770 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1772 kMaxWaitMs);
1773 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1774 kMaxWaitMs);
1775}
1776
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001777// This test sets up a Jsep call with SCTP DataChannel and verifies the
1778// negotiation is completed without error.
1779#ifdef HAVE_SCTP
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01001780// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
1781#if defined(WIN) && defined(_DEBUG)
1782#define MAYBE_CreateOfferWithSctpDataChannel \
1783 DISABLED_CreateOfferWithSctpDataChannel
1784#else
1785#define MAYBE_CreateOfferWithSctpDataChannel CreateOfferWithSctpDataChannel
1786#endif
1787TEST_F(P2PTestConductor, MAYBE_CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001788 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001789 FakeConstraints constraints;
1790 constraints.SetMandatory(
1791 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1792 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1793 initializing_client()->CreateDataChannel();
1794 initializing_client()->Negotiate(false, false);
1795}
1796#endif
1797
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798// This test sets up a call between two parties with audio, and video.
1799// During the call, the initializing side restart ice and the test verifies that
1800// new ice candidates are generated and audio and video still can flow.
deadbeef7c73bdb2015-12-10 15:10:44 -08001801TEST_F(P2PTestConductor, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001802 ASSERT_TRUE(CreateTestClients());
1803
1804 // Negotiate and wait for ice completion and make sure audio and video plays.
1805 LocalP2PTest();
1806
1807 // Create a SDP string of the first audio candidate for both clients.
1808 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1809 initializing_client()->pc()->local_description()->candidates(0);
1810 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1811 receiving_client()->pc()->local_description()->candidates(0);
1812 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1813 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1814 std::string initiator_candidate;
1815 EXPECT_TRUE(
1816 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1817 std::string receiver_candidate;
1818 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1819
1820 // Restart ice on the initializing client.
1821 receiving_client()->SetExpectIceRestart(true);
1822 initializing_client()->IceRestart();
1823
1824 // Negotiate and wait for ice completion again and make sure audio and video
1825 // plays.
1826 LocalP2PTest();
1827
1828 // Create a SDP string of the first audio candidate for both clients again.
1829 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1830 initializing_client()->pc()->local_description()->candidates(0);
1831 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1832 receiving_client()->pc()->local_description()->candidates(0);
1833 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1834 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1835 std::string initiator_candidate_restart;
1836 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1837 &initiator_candidate_restart));
1838 std::string receiver_candidate_restart;
1839 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1840 &receiver_candidate_restart));
1841
1842 // Verify that the first candidates in the local session descriptions has
1843 // changed.
1844 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1845 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1846}
1847
deadbeeffaac4972015-11-12 15:33:07 -08001848// This test sets up a call between two parties with audio, and video.
1849// It then renegotiates setting the video m-line to "port 0", then later
1850// renegotiates again, enabling video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001851TEST_F(P2PTestConductor, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08001852 ASSERT_TRUE(CreateTestClients());
1853
1854 // Do initial negotiation. Will result in video and audio sendonly m-lines.
1855 receiving_client()->set_auto_add_stream(false);
1856 initializing_client()->AddMediaStream(true, true);
1857 initializing_client()->Negotiate();
1858
1859 // Negotiate again, disabling the video m-line (receiving client will
1860 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
1861 receiving_client()->SetReceiveVideo(false);
1862 initializing_client()->Negotiate();
1863
1864 // Enable video and do negotiation again, making sure video is received
1865 // end-to-end.
1866 receiving_client()->SetReceiveVideo(true);
1867 receiving_client()->AddMediaStream(true, true);
1868 LocalP2PTest();
1869}
1870
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001871// This test sets up a Jsep call between two parties with external
1872// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001873// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1874// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001875TEST_F(P2PTestConductor, DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876 ASSERT_TRUE(CreateTestClients());
1877 EnableVideoDecoderFactory();
1878 LocalP2PTest();
1879}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001880
deadbeeffac06552015-11-25 11:26:01 -08001881// This tests that if we negotiate after calling CreateSender but before we
1882// have a track, then set a track later, frames from the newly-set track are
1883// received end-to-end.
deadbeef7c73bdb2015-12-10 15:10:44 -08001884TEST_F(P2PTestConductor, EarlyWarmupTest) {
deadbeeffac06552015-11-25 11:26:01 -08001885 ASSERT_TRUE(CreateTestClients());
deadbeefbd7d8f72015-12-18 16:58:44 -08001886 auto audio_sender =
1887 initializing_client()->pc()->CreateSender("audio", "stream_id");
1888 auto video_sender =
1889 initializing_client()->pc()->CreateSender("video", "stream_id");
deadbeeffac06552015-11-25 11:26:01 -08001890 initializing_client()->Negotiate();
1891 // Wait for ICE connection to complete, without any tracks.
1892 // Note that the receiving client WILL (in HandleIncomingOffer) create
1893 // tracks, so it's only the initiator here that's doing early warmup.
1894 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1895 VerifySessionDescriptions();
1896 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1897 initializing_client()->ice_connection_state(),
1898 kMaxWaitForFramesMs);
1899 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1900 receiving_client()->ice_connection_state(),
1901 kMaxWaitForFramesMs);
1902 // Now set the tracks, and expect frames to immediately start flowing.
1903 EXPECT_TRUE(
1904 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
1905 EXPECT_TRUE(
1906 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
hta6b4f8392016-03-10 00:24:31 -08001907 EXPECT_TRUE_WAIT(FramesHaveArrived(kEndAudioFrameCount, kEndVideoFrameCount),
deadbeeffac06552015-11-25 11:26:01 -08001908 kMaxWaitForFramesMs);
1909}
1910
deadbeef0a6c4ca2015-10-06 11:38:28 -07001911class IceServerParsingTest : public testing::Test {
1912 public:
1913 // Convenience for parsing a single URL.
1914 bool ParseUrl(const std::string& url) {
1915 return ParseUrl(url, std::string(), std::string());
1916 }
1917
1918 bool ParseUrl(const std::string& url,
1919 const std::string& username,
1920 const std::string& password) {
1921 PeerConnectionInterface::IceServers servers;
1922 PeerConnectionInterface::IceServer server;
1923 server.urls.push_back(url);
1924 server.username = username;
1925 server.password = password;
1926 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001927 return webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_);
deadbeef0a6c4ca2015-10-06 11:38:28 -07001928 }
1929
1930 protected:
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001931 cricket::ServerAddresses stun_servers_;
1932 std::vector<cricket::RelayServerConfig> turn_servers_;
deadbeef0a6c4ca2015-10-06 11:38:28 -07001933};
1934
1935// Make sure all STUN/TURN prefixes are parsed correctly.
1936TEST_F(IceServerParsingTest, ParseStunPrefixes) {
1937 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001938 EXPECT_EQ(1U, stun_servers_.size());
1939 EXPECT_EQ(0U, turn_servers_.size());
1940 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001941
1942 EXPECT_TRUE(ParseUrl("stuns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001943 EXPECT_EQ(1U, stun_servers_.size());
1944 EXPECT_EQ(0U, turn_servers_.size());
1945 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001946
1947 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001948 EXPECT_EQ(0U, stun_servers_.size());
1949 EXPECT_EQ(1U, turn_servers_.size());
1950 EXPECT_FALSE(turn_servers_[0].ports[0].secure);
1951 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001952
1953 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001954 EXPECT_EQ(0U, stun_servers_.size());
1955 EXPECT_EQ(1U, turn_servers_.size());
1956 EXPECT_TRUE(turn_servers_[0].ports[0].secure);
1957 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001958
1959 // invalid prefixes
1960 EXPECT_FALSE(ParseUrl("stunn:hostname"));
1961 EXPECT_FALSE(ParseUrl(":hostname"));
1962 EXPECT_FALSE(ParseUrl(":"));
1963 EXPECT_FALSE(ParseUrl(""));
1964}
1965
1966TEST_F(IceServerParsingTest, VerifyDefaults) {
1967 // TURNS defaults
1968 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001969 EXPECT_EQ(1U, turn_servers_.size());
1970 EXPECT_EQ(5349, turn_servers_[0].ports[0].address.port());
1971 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
1972 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001973
1974 // TURN defaults
1975 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001976 EXPECT_EQ(1U, turn_servers_.size());
1977 EXPECT_EQ(3478, turn_servers_[0].ports[0].address.port());
1978 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
1979 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001980
1981 // STUN defaults
1982 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001983 EXPECT_EQ(1U, stun_servers_.size());
1984 EXPECT_EQ(3478, stun_servers_.begin()->port());
1985 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001986}
1987
1988// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
1989// can be parsed correctly.
1990TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
1991 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001992 EXPECT_EQ(1U, stun_servers_.size());
1993 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
1994 EXPECT_EQ(1234, stun_servers_.begin()->port());
1995 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001996
1997 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001998 EXPECT_EQ(1U, stun_servers_.size());
1999 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2000 EXPECT_EQ(4321, stun_servers_.begin()->port());
2001 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002002
2003 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002004 EXPECT_EQ(1U, stun_servers_.size());
2005 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2006 EXPECT_EQ(9999, stun_servers_.begin()->port());
2007 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002008
2009 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002010 EXPECT_EQ(1U, stun_servers_.size());
2011 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2012 EXPECT_EQ(3478, stun_servers_.begin()->port());
2013 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002014
2015 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002016 EXPECT_EQ(1U, stun_servers_.size());
2017 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2018 EXPECT_EQ(3478, stun_servers_.begin()->port());
2019 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002020
2021 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002022 EXPECT_EQ(1U, stun_servers_.size());
2023 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2024 EXPECT_EQ(3478, stun_servers_.begin()->port());
2025 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002026
2027 // Try some invalid hostname:port strings.
2028 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
2029 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002030 EXPECT_FALSE(ParseUrl("stun:hostname:port:more"));
2031 EXPECT_FALSE(ParseUrl("stun:hostname:port more"));
deadbeef0a6c4ca2015-10-06 11:38:28 -07002032 EXPECT_FALSE(ParseUrl("stun:hostname:"));
2033 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
2034 EXPECT_FALSE(ParseUrl("stun::5555"));
2035 EXPECT_FALSE(ParseUrl("stun:"));
2036}
2037
2038// Test parsing the "?transport=xxx" part of the URL.
2039TEST_F(IceServerParsingTest, ParseTransport) {
2040 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002041 EXPECT_EQ(1U, turn_servers_.size());
2042 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
2043 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002044
2045 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002046 EXPECT_EQ(1U, turn_servers_.size());
2047 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2048 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002049
2050 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
2051}
2052
2053// Test parsing ICE username contained in URL.
2054TEST_F(IceServerParsingTest, ParseUsername) {
2055 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002056 EXPECT_EQ(1U, turn_servers_.size());
2057 EXPECT_EQ("user", turn_servers_[0].credentials.username);
2058 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002059
2060 EXPECT_FALSE(ParseUrl("turn:@hostname"));
2061 EXPECT_FALSE(ParseUrl("turn:username@"));
2062 EXPECT_FALSE(ParseUrl("turn:@"));
2063 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
2064}
2065
2066// Test that username and password from IceServer is copied into the resulting
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002067// RelayServerConfig.
deadbeef0a6c4ca2015-10-06 11:38:28 -07002068TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
2069 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002070 EXPECT_EQ(1U, turn_servers_.size());
2071 EXPECT_EQ("username", turn_servers_[0].credentials.username);
2072 EXPECT_EQ("password", turn_servers_[0].credentials.password);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002073}
2074
2075// Ensure that if a server has multiple URLs, each one is parsed.
2076TEST_F(IceServerParsingTest, ParseMultipleUrls) {
2077 PeerConnectionInterface::IceServers servers;
2078 PeerConnectionInterface::IceServer server;
2079 server.urls.push_back("stun:hostname");
2080 server.urls.push_back("turn:hostname");
2081 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002082 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2083 EXPECT_EQ(1U, stun_servers_.size());
2084 EXPECT_EQ(1U, turn_servers_.size());
deadbeef0a6c4ca2015-10-06 11:38:28 -07002085}
2086
Taylor Brandstetter893505d2016-01-07 15:12:48 -08002087// Ensure that TURN servers are given unique priorities,
2088// so that their resulting candidates have unique priorities.
2089TEST_F(IceServerParsingTest, TurnServerPrioritiesUnique) {
2090 PeerConnectionInterface::IceServers servers;
2091 PeerConnectionInterface::IceServer server;
2092 server.urls.push_back("turn:hostname");
2093 server.urls.push_back("turn:hostname2");
2094 servers.push_back(server);
2095 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2096 EXPECT_EQ(2U, turn_servers_.size());
2097 EXPECT_NE(turn_servers_[0].priority, turn_servers_[1].priority);
2098}
2099
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00002100#endif // if !defined(THREAD_SANITIZER)
hta6b4f8392016-03-10 00:24:31 -08002101
2102} // namespace