blob: dcffd1330f11cd483b1fc19d33f9775dc18d0a96 [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"
deadbeef246b5272016-03-17 15:03:39 -070032#include "webrtc/api/videosourceinterface.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000033#include "webrtc/base/gunit.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000034#include "webrtc/base/physicalsocketserver.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000035#include "webrtc/base/scoped_ptr.h"
36#include "webrtc/base/ssladapter.h"
37#include "webrtc/base/sslstreamadapter.h"
38#include "webrtc/base/thread.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000039#include "webrtc/base/virtualsocketserver.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010040#include "webrtc/media/engine/fakewebrtcvideoengine.h"
kjellanderf4752772016-03-02 05:42:30 -080041#include "webrtc/p2p/base/p2pconstants.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000042#include "webrtc/p2p/base/sessiondescription.h"
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080043#include "webrtc/p2p/client/fakeportallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010044#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045
46#define MAYBE_SKIP_TEST(feature) \
47 if (!(feature())) { \
48 LOG(LS_INFO) << "Feature disabled... skipping"; \
49 return; \
50 }
51
52using cricket::ContentInfo;
53using cricket::FakeWebRtcVideoDecoder;
54using cricket::FakeWebRtcVideoDecoderFactory;
55using cricket::FakeWebRtcVideoEncoder;
56using cricket::FakeWebRtcVideoEncoderFactory;
57using cricket::MediaContentDescription;
58using webrtc::DataBuffer;
59using webrtc::DataChannelInterface;
60using webrtc::DtmfSender;
61using webrtc::DtmfSenderInterface;
62using webrtc::DtmfSenderObserverInterface;
63using webrtc::FakeConstraints;
64using webrtc::MediaConstraintsInterface;
deadbeeffaac4972015-11-12 15:33:07 -080065using webrtc::MediaStreamInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066using webrtc::MediaStreamTrackInterface;
67using webrtc::MockCreateSessionDescriptionObserver;
68using webrtc::MockDataChannelObserver;
69using webrtc::MockSetSessionDescriptionObserver;
70using webrtc::MockStatsObserver;
deadbeeffaac4972015-11-12 15:33:07 -080071using webrtc::ObserverInterface;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000072using webrtc::PeerConnectionInterface;
Joachim Bauch04e5b492015-05-29 09:40:39 +020073using webrtc::PeerConnectionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074using webrtc::SessionDescriptionInterface;
75using webrtc::StreamCollectionInterface;
76
hta6b4f8392016-03-10 00:24:31 -080077namespace {
78
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000079static const int kMaxWaitMs = 10000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000080// Disable for TSan v2, see
81// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
82// This declaration is also #ifdef'd as it causes uninitialized-variable
83// warnings.
84#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085static const int kMaxWaitForStatsMs = 3000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000086#endif
deadbeeffac06552015-11-25 11:26:01 -080087static const int kMaxWaitForActivationMs = 5000;
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000088static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089static const int kEndAudioFrameCount = 3;
90static const int kEndVideoFrameCount = 3;
91
92static const char kStreamLabelBase[] = "stream_label";
93static const char kVideoTrackLabelBase[] = "video_track";
94static const char kAudioTrackLabelBase[] = "audio_track";
95static const char kDataChannelLabel[] = "data_channel";
96
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +000097// Disable for TSan v2, see
98// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
99// This declaration is also #ifdef'd as it causes unused-variable errors.
100#if !defined(THREAD_SANITIZER)
101// SRTP cipher name negotiated by the tests. This must be updated if the
102// default changes.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800103static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000104#endif
105
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106static void RemoveLinesFromSdp(const std::string& line_start,
107 std::string* sdp) {
108 const char kSdpLineEnd[] = "\r\n";
109 size_t ssrc_pos = 0;
110 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
111 std::string::npos) {
112 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
113 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
114 }
115}
116
hta6b4f8392016-03-10 00:24:31 -0800117bool StreamsHaveAudioTrack(StreamCollectionInterface* streams) {
118 for (size_t idx = 0; idx < streams->count(); idx++) {
119 auto stream = streams->at(idx);
120 if (stream->GetAudioTracks().size() > 0) {
121 return true;
122 }
123 }
124 return false;
125}
126
127bool StreamsHaveVideoTrack(StreamCollectionInterface* streams) {
128 for (size_t idx = 0; idx < streams->count(); idx++) {
129 auto stream = streams->at(idx);
130 if (stream->GetVideoTracks().size() > 0) {
131 return true;
132 }
133 }
134 return false;
135}
136
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137class SignalingMessageReceiver {
138 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 virtual void ReceiveSdpMessage(const std::string& type,
140 std::string& msg) = 0;
141 virtual void ReceiveIceMessage(const std::string& sdp_mid,
142 int sdp_mline_index,
143 const std::string& msg) = 0;
144
145 protected:
deadbeefaf1b59c2015-10-15 12:08:41 -0700146 SignalingMessageReceiver() {}
147 virtual ~SignalingMessageReceiver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148};
149
deadbeefaf1b59c2015-10-15 12:08:41 -0700150class PeerConnectionTestClient : public webrtc::PeerConnectionObserver,
deadbeeffaac4972015-11-12 15:33:07 -0800151 public SignalingMessageReceiver,
152 public ObserverInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 public:
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800154 static PeerConnectionTestClient* CreateClientWithDtlsIdentityStore(
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800155 const std::string& id,
156 const MediaConstraintsInterface* constraints,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800157 const PeerConnectionFactory::Options* options,
htaaac2dea2016-03-10 13:35:55 -0800158 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store,
159 bool prefer_constraint_apis) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800160 PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
htaaac2dea2016-03-10 13:35:55 -0800161 if (!client->Init(constraints, options, std::move(dtls_identity_store),
162 prefer_constraint_apis)) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800163 delete client;
164 return nullptr;
165 }
166 return client;
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800167 }
168
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800169 static PeerConnectionTestClient* CreateClient(
170 const std::string& id,
171 const MediaConstraintsInterface* constraints,
172 const PeerConnectionFactory::Options* options) {
173 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
174 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
175 : nullptr);
176
htaaac2dea2016-03-10 13:35:55 -0800177 return CreateClientWithDtlsIdentityStore(
178 id, constraints, options, std::move(dtls_identity_store), true);
179 }
180
181 static PeerConnectionTestClient* CreateClientPreferNoConstraints(
182 const std::string& id,
183 const PeerConnectionFactory::Options* options) {
184 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
185 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
186 : nullptr);
187
188 return CreateClientWithDtlsIdentityStore(
189 id, nullptr, options, std::move(dtls_identity_store), false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800190 }
191
deadbeefaf1b59c2015-10-15 12:08:41 -0700192 ~PeerConnectionTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 }
194
deadbeefaf1b59c2015-10-15 12:08:41 -0700195 void Negotiate() { Negotiate(true, true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196
deadbeefaf1b59c2015-10-15 12:08:41 -0700197 void Negotiate(bool audio, bool video) {
198 rtc::scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700199 ASSERT_TRUE(DoCreateOffer(&offer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200
deadbeefaf1b59c2015-10-15 12:08:41 -0700201 if (offer->description()->GetContentByName("audio")) {
202 offer->description()->GetContentByName("audio")->rejected = !audio;
203 }
204 if (offer->description()->GetContentByName("video")) {
205 offer->description()->GetContentByName("video")->rejected = !video;
206 }
207
208 std::string sdp;
209 EXPECT_TRUE(offer->ToString(&sdp));
210 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
211 signaling_message_receiver_->ReceiveSdpMessage(
212 webrtc::SessionDescriptionInterface::kOffer, sdp);
213 }
214
215 // SignalingMessageReceiver callback.
216 void ReceiveSdpMessage(const std::string& type, std::string& msg) override {
217 FilterIncomingSdpMessage(&msg);
218 if (type == webrtc::SessionDescriptionInterface::kOffer) {
219 HandleIncomingOffer(msg);
220 } else {
221 HandleIncomingAnswer(msg);
222 }
223 }
224
225 // SignalingMessageReceiver callback.
226 void ReceiveIceMessage(const std::string& sdp_mid,
227 int sdp_mline_index,
228 const std::string& msg) override {
229 LOG(INFO) << id_ << "ReceiveIceMessage";
230 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
231 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
232 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
233 }
234
235 // PeerConnectionObserver callbacks.
236 void OnSignalingChange(
237 webrtc::PeerConnectionInterface::SignalingState new_state) override {
238 EXPECT_EQ(pc()->signaling_state(), new_state);
239 }
deadbeeffaac4972015-11-12 15:33:07 -0800240 void OnAddStream(MediaStreamInterface* media_stream) override {
241 media_stream->RegisterObserver(this);
deadbeefaf1b59c2015-10-15 12:08:41 -0700242 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
243 const std::string id = media_stream->GetVideoTracks()[i]->id();
244 ASSERT_TRUE(fake_video_renderers_.find(id) ==
245 fake_video_renderers_.end());
deadbeefc9be0072015-12-14 18:27:57 -0800246 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
247 media_stream->GetVideoTracks()[i]));
deadbeefaf1b59c2015-10-15 12:08:41 -0700248 }
249 }
deadbeeffaac4972015-11-12 15:33:07 -0800250 void OnRemoveStream(MediaStreamInterface* media_stream) override {}
deadbeefaf1b59c2015-10-15 12:08:41 -0700251 void OnRenegotiationNeeded() override {}
252 void OnIceConnectionChange(
253 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
254 EXPECT_EQ(pc()->ice_connection_state(), new_state);
255 }
256 void OnIceGatheringChange(
257 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
258 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
259 }
260 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
261 LOG(INFO) << id_ << "OnIceCandidate";
262
263 std::string ice_sdp;
264 EXPECT_TRUE(candidate->ToString(&ice_sdp));
265 if (signaling_message_receiver_ == nullptr) {
266 // Remote party may be deleted.
267 return;
268 }
269 signaling_message_receiver_->ReceiveIceMessage(
270 candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
271 }
272
deadbeeffaac4972015-11-12 15:33:07 -0800273 // MediaStreamInterface callback
274 void OnChanged() override {
275 // Track added or removed from MediaStream, so update our renderers.
276 rtc::scoped_refptr<StreamCollectionInterface> remote_streams =
277 pc()->remote_streams();
278 // Remove renderers for tracks that were removed.
279 for (auto it = fake_video_renderers_.begin();
280 it != fake_video_renderers_.end();) {
281 if (remote_streams->FindVideoTrack(it->first) == nullptr) {
deadbeefc9be0072015-12-14 18:27:57 -0800282 auto to_remove = it++;
283 removed_fake_video_renderers_.push_back(std::move(to_remove->second));
284 fake_video_renderers_.erase(to_remove);
deadbeeffaac4972015-11-12 15:33:07 -0800285 } else {
286 ++it;
287 }
288 }
289 // Create renderers for new video tracks.
290 for (size_t stream_index = 0; stream_index < remote_streams->count();
291 ++stream_index) {
292 MediaStreamInterface* remote_stream = remote_streams->at(stream_index);
293 for (size_t track_index = 0;
294 track_index < remote_stream->GetVideoTracks().size();
295 ++track_index) {
296 const std::string id =
297 remote_stream->GetVideoTracks()[track_index]->id();
298 if (fake_video_renderers_.find(id) != fake_video_renderers_.end()) {
299 continue;
300 }
deadbeefc9be0072015-12-14 18:27:57 -0800301 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
302 remote_stream->GetVideoTracks()[track_index]));
deadbeeffaac4972015-11-12 15:33:07 -0800303 }
304 }
305 }
306
deadbeefaf1b59c2015-10-15 12:08:41 -0700307 void SetVideoConstraints(const webrtc::FakeConstraints& video_constraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308 video_constraints_ = video_constraint;
309 }
310
311 void AddMediaStream(bool audio, bool video) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700312 std::string stream_label =
313 kStreamLabelBase +
314 rtc::ToString<int>(static_cast<int>(pc()->local_streams()->count()));
deadbeeffaac4972015-11-12 15:33:07 -0800315 rtc::scoped_refptr<MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000316 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317
318 if (audio && can_receive_audio()) {
deadbeeffac06552015-11-25 11:26:01 -0800319 stream->AddTrack(CreateLocalAudioTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 }
321 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000322 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323 }
324
deadbeefaf1b59c2015-10-15 12:08:41 -0700325 EXPECT_TRUE(pc()->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326 }
327
deadbeefaf1b59c2015-10-15 12:08:41 -0700328 size_t NumberOfLocalMediaStreams() { return pc()->local_streams()->count(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329
330 bool SessionActive() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700331 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332 }
333
deadbeeffaac4972015-11-12 15:33:07 -0800334 // Automatically add a stream when receiving an offer, if we don't have one.
335 // Defaults to true.
336 void set_auto_add_stream(bool auto_add_stream) {
337 auto_add_stream_ = auto_add_stream;
338 }
339
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340 void set_signaling_message_receiver(
deadbeefaf1b59c2015-10-15 12:08:41 -0700341 SignalingMessageReceiver* signaling_message_receiver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 signaling_message_receiver_ = signaling_message_receiver;
343 }
344
345 void EnableVideoDecoderFactory() {
346 video_decoder_factory_enabled_ = true;
347 fake_video_decoder_factory_->AddSupportedVideoCodecType(
348 webrtc::kVideoCodecVP8);
349 }
350
deadbeefaf1b59c2015-10-15 12:08:41 -0700351 void IceRestart() {
htaaac2dea2016-03-10 13:35:55 -0800352 offer_answer_constraints_.SetMandatoryIceRestart(true);
353 offer_answer_options_.ice_restart = true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700354 SetExpectIceRestart(true);
355 }
356
357 void SetExpectIceRestart(bool expect_restart) {
358 expect_ice_restart_ = expect_restart;
359 }
360
361 bool ExpectIceRestart() const { return expect_ice_restart_; }
362
363 void SetReceiveAudioVideo(bool audio, bool video) {
364 SetReceiveAudio(audio);
365 SetReceiveVideo(video);
366 ASSERT_EQ(audio, can_receive_audio());
367 ASSERT_EQ(video, can_receive_video());
368 }
369
370 void SetReceiveAudio(bool audio) {
371 if (audio && can_receive_audio())
372 return;
htaaac2dea2016-03-10 13:35:55 -0800373 offer_answer_constraints_.SetMandatoryReceiveAudio(audio);
374 offer_answer_options_.offer_to_receive_audio = audio ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700375 }
376
377 void SetReceiveVideo(bool video) {
378 if (video && can_receive_video())
379 return;
htaaac2dea2016-03-10 13:35:55 -0800380 offer_answer_constraints_.SetMandatoryReceiveVideo(video);
381 offer_answer_options_.offer_to_receive_video = video ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700382 }
383
384 void RemoveMsidFromReceivedSdp(bool remove) { remove_msid_ = remove; }
385
386 void RemoveSdesCryptoFromReceivedSdp(bool remove) { remove_sdes_ = remove; }
387
388 void RemoveBundleFromReceivedSdp(bool remove) { remove_bundle_ = remove; }
389
390 bool can_receive_audio() {
391 bool value;
htaaac2dea2016-03-10 13:35:55 -0800392 if (prefer_constraint_apis_) {
393 if (webrtc::FindConstraint(
394 &offer_answer_constraints_,
395 MediaConstraintsInterface::kOfferToReceiveAudio, &value,
396 nullptr)) {
397 return value;
398 }
399 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700400 }
htaaac2dea2016-03-10 13:35:55 -0800401 return offer_answer_options_.offer_to_receive_audio > 0 ||
402 offer_answer_options_.offer_to_receive_audio ==
403 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700404 }
405
406 bool can_receive_video() {
407 bool value;
htaaac2dea2016-03-10 13:35:55 -0800408 if (prefer_constraint_apis_) {
409 if (webrtc::FindConstraint(
410 &offer_answer_constraints_,
411 MediaConstraintsInterface::kOfferToReceiveVideo, &value,
412 nullptr)) {
413 return value;
414 }
415 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700416 }
htaaac2dea2016-03-10 13:35:55 -0800417 return offer_answer_options_.offer_to_receive_video > 0 ||
418 offer_answer_options_.offer_to_receive_video ==
419 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700420 }
421
deadbeefaf1b59c2015-10-15 12:08:41 -0700422 void OnDataChannel(DataChannelInterface* data_channel) override {
423 LOG(INFO) << id_ << "OnDataChannel";
424 data_channel_ = data_channel;
425 data_observer_.reset(new MockDataChannelObserver(data_channel));
426 }
427
428 void CreateDataChannel() {
429 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, nullptr);
430 ASSERT_TRUE(data_channel_.get() != nullptr);
431 data_observer_.reset(new MockDataChannelObserver(data_channel_));
432 }
433
deadbeeffac06552015-11-25 11:26:01 -0800434 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack(
435 const std::string& stream_label) {
436 FakeConstraints constraints;
437 // Disable highpass filter so that we can get all the test audio frames.
438 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
439 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
440 peer_connection_factory_->CreateAudioSource(&constraints);
441 // TODO(perkj): Test audio source when it is implemented. Currently audio
442 // always use the default input.
443 std::string label = stream_label + kAudioTrackLabelBase;
444 return peer_connection_factory_->CreateAudioTrack(label, source);
445 }
446
447 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack(
448 const std::string& stream_label) {
449 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
450 FakeConstraints source_constraints = video_constraints_;
451 source_constraints.SetMandatoryMaxFrameRate(10);
452
453 cricket::FakeVideoCapturer* fake_capturer =
454 new webrtc::FakePeriodicVideoCapturer();
455 video_capturers_.push_back(fake_capturer);
perkja3ede6c2016-03-08 01:27:48 +0100456 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
deadbeeffac06552015-11-25 11:26:01 -0800457 peer_connection_factory_->CreateVideoSource(fake_capturer,
458 &source_constraints);
459 std::string label = stream_label + kVideoTrackLabelBase;
460 return peer_connection_factory_->CreateVideoTrack(label, source);
461 }
462
deadbeefaf1b59c2015-10-15 12:08:41 -0700463 DataChannelInterface* data_channel() { return data_channel_; }
464 const MockDataChannelObserver* data_observer() const {
465 return data_observer_.get();
466 }
467
hta6b4f8392016-03-10 00:24:31 -0800468 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
deadbeefaf1b59c2015-10-15 12:08:41 -0700469
470 void StopVideoCapturers() {
471 for (std::vector<cricket::VideoCapturer*>::iterator it =
472 video_capturers_.begin();
473 it != video_capturers_.end(); ++it) {
474 (*it)->Stop();
475 }
476 }
477
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478 bool AudioFramesReceivedCheck(int number_of_frames) const {
479 return number_of_frames <= fake_audio_capture_module_->frames_received();
480 }
481
deadbeefc9be0072015-12-14 18:27:57 -0800482 int audio_frames_received() const {
483 return fake_audio_capture_module_->frames_received();
484 }
485
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 bool VideoFramesReceivedCheck(int number_of_frames) {
487 if (video_decoder_factory_enabled_) {
488 const std::vector<FakeWebRtcVideoDecoder*>& decoders
489 = fake_video_decoder_factory_->decoders();
490 if (decoders.empty()) {
491 return number_of_frames <= 0;
492 }
hta6b4f8392016-03-10 00:24:31 -0800493 // Note - this checks that EACH decoder has the requisite number
494 // of frames. The video_frames_received() function sums them.
deadbeefc9be0072015-12-14 18:27:57 -0800495 for (FakeWebRtcVideoDecoder* decoder : decoders) {
496 if (number_of_frames > decoder->GetNumFramesReceived()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 return false;
498 }
499 }
500 return true;
501 } else {
502 if (fake_video_renderers_.empty()) {
503 return number_of_frames <= 0;
504 }
505
deadbeefc9be0072015-12-14 18:27:57 -0800506 for (const auto& pair : fake_video_renderers_) {
507 if (number_of_frames > pair.second->num_rendered_frames()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508 return false;
509 }
510 }
511 return true;
512 }
513 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700514
deadbeefc9be0072015-12-14 18:27:57 -0800515 int video_frames_received() const {
516 int total = 0;
517 if (video_decoder_factory_enabled_) {
518 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
519 fake_video_decoder_factory_->decoders();
520 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
521 total += decoder->GetNumFramesReceived();
522 }
523 } else {
524 for (const auto& pair : fake_video_renderers_) {
525 total += pair.second->num_rendered_frames();
526 }
527 for (const auto& renderer : removed_fake_video_renderers_) {
528 total += renderer->num_rendered_frames();
529 }
530 }
531 return total;
532 }
533
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 // Verify the CreateDtmfSender interface
535 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000536 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
537 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538
539 // We can't create a DTMF sender with an invalid audio track or a non local
540 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700541 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000542 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700543 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
544 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545
546 // We should be able to create a DTMF sender from a local track.
547 webrtc::AudioTrackInterface* localtrack =
548 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
549 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700550 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551 dtmf_sender->RegisterObserver(observer.get());
552
553 // Test the DtmfSender object just created.
554 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
555 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
556
557 // We don't need to verify that the DTMF tones are actually sent out because
558 // that is already covered by the tests of the lower level components.
559
560 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
561 std::vector<std::string> tones;
562 tones.push_back("1");
563 tones.push_back("a");
564 tones.push_back("");
565 observer->Verify(tones);
566
567 dtmf_sender->UnregisterObserver();
568 }
569
570 // Verifies that the SessionDescription have rejected the appropriate media
571 // content.
572 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700573 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
574 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575 const cricket::SessionDescription* remote_desc =
576 peer_connection_->remote_description()->description();
577 const cricket::SessionDescription* local_desc =
578 peer_connection_->local_description()->description();
579
580 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
581 if (remote_audio_content) {
582 const ContentInfo* audio_content =
583 GetFirstAudioContent(local_desc);
584 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
585 }
586
587 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
588 if (remote_video_content) {
589 const ContentInfo* video_content =
590 GetFirstVideoContent(local_desc);
591 EXPECT_EQ(can_receive_video(), !video_content->rejected);
592 }
593 }
594
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000595 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700596 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597 const cricket::SessionDescription* desc =
598 peer_connection_->local_description()->description();
599 const cricket::ContentInfos& contents = desc->contents();
600
601 for (size_t index = 0; index < contents.size(); ++index) {
602 if (contents[index].rejected)
603 continue;
604 const cricket::TransportDescription* transport_desc =
605 desc->GetTransportDescriptionByName(contents[index].name);
606
607 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000608 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 if (ufragpair_it == ice_ufrag_pwd_.end()) {
610 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000611 ice_ufrag_pwd_[static_cast<int>(index)] =
612 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613 } else if (ExpectIceRestart()) {
614 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
615 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
616 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
617 } else {
618 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
619 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
620 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
621 }
622 }
623 }
624
625 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000626 rtc::scoped_refptr<MockStatsObserver>
627 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000628 EXPECT_TRUE(peer_connection_->GetStats(
629 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700631 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632 return observer->AudioOutputLevel();
633 }
634
635 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000636 rtc::scoped_refptr<MockStatsObserver>
637 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000638 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700639 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700641 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 return observer->AudioInputLevel();
643 }
644
645 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000646 rtc::scoped_refptr<MockStatsObserver>
647 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000648 EXPECT_TRUE(peer_connection_->GetStats(
649 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000650 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700651 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652 return observer->BytesReceived();
653 }
654
655 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000656 rtc::scoped_refptr<MockStatsObserver>
657 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000658 EXPECT_TRUE(peer_connection_->GetStats(
659 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700661 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662 return observer->BytesSent();
663 }
664
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000665 int GetAvailableReceivedBandwidthStats() {
666 rtc::scoped_refptr<MockStatsObserver>
667 observer(new rtc::RefCountedObject<MockStatsObserver>());
668 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700669 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000670 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700671 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000672 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000673 return bw;
674 }
675
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000676 std::string GetDtlsCipherStats() {
677 rtc::scoped_refptr<MockStatsObserver>
678 observer(new rtc::RefCountedObject<MockStatsObserver>());
679 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700680 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000681 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700682 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000683 return observer->DtlsCipher();
684 }
685
686 std::string GetSrtpCipherStats() {
687 rtc::scoped_refptr<MockStatsObserver>
688 observer(new rtc::RefCountedObject<MockStatsObserver>());
689 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700690 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000691 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700692 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000693 return observer->SrtpCipher();
694 }
695
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 int rendered_width() {
697 EXPECT_FALSE(fake_video_renderers_.empty());
698 return fake_video_renderers_.empty() ? 1 :
699 fake_video_renderers_.begin()->second->width();
700 }
701
702 int rendered_height() {
703 EXPECT_FALSE(fake_video_renderers_.empty());
704 return fake_video_renderers_.empty() ? 1 :
705 fake_video_renderers_.begin()->second->height();
706 }
707
708 size_t number_of_remote_streams() {
709 if (!pc())
710 return 0;
711 return pc()->remote_streams()->count();
712 }
713
hta6b4f8392016-03-10 00:24:31 -0800714 StreamCollectionInterface* remote_streams() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 if (!pc()) {
716 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700717 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 }
719 return pc()->remote_streams();
720 }
721
722 StreamCollectionInterface* local_streams() {
723 if (!pc()) {
724 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700725 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726 }
727 return pc()->local_streams();
728 }
729
hta6b4f8392016-03-10 00:24:31 -0800730 bool HasLocalAudioTrack() { return StreamsHaveAudioTrack(local_streams()); }
731
732 bool HasLocalVideoTrack() { return StreamsHaveVideoTrack(local_streams()); }
733
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
735 return pc()->signaling_state();
736 }
737
738 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
739 return pc()->ice_connection_state();
740 }
741
742 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
743 return pc()->ice_gathering_state();
744 }
745
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 private:
747 class DummyDtmfObserver : public DtmfSenderObserverInterface {
748 public:
749 DummyDtmfObserver() : completed_(false) {}
750
751 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700752 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 tones_.push_back(tone);
754 if (tone.empty()) {
755 completed_ = true;
756 }
757 }
758
759 void Verify(const std::vector<std::string>& tones) const {
760 ASSERT_TRUE(tones_.size() == tones.size());
761 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
762 }
763
764 bool completed() const { return completed_; }
765
766 private:
767 bool completed_;
768 std::vector<std::string> tones_;
769 };
770
deadbeefaf1b59c2015-10-15 12:08:41 -0700771 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
772
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800773 bool Init(
774 const MediaConstraintsInterface* constraints,
775 const PeerConnectionFactory::Options* options,
htaaac2dea2016-03-10 13:35:55 -0800776 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store,
777 bool prefer_constraint_apis) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700778 EXPECT_TRUE(!peer_connection_);
779 EXPECT_TRUE(!peer_connection_factory_);
htaaac2dea2016-03-10 13:35:55 -0800780 if (!prefer_constraint_apis) {
781 EXPECT_TRUE(!constraints);
782 }
783 prefer_constraint_apis_ = prefer_constraint_apis;
784
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800785 rtc::scoped_ptr<cricket::PortAllocator> port_allocator(
786 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
deadbeefaf1b59c2015-10-15 12:08:41 -0700787 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
788
789 if (fake_audio_capture_module_ == nullptr) {
790 return false;
791 }
792 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
793 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
794 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
795 rtc::Thread::Current(), rtc::Thread::Current(),
796 fake_audio_capture_module_, fake_video_encoder_factory_,
797 fake_video_decoder_factory_);
798 if (!peer_connection_factory_) {
799 return false;
800 }
801 if (options) {
802 peer_connection_factory_->SetOptions(*options);
803 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800804 peer_connection_ = CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800805 std::move(port_allocator), constraints, std::move(dtls_identity_store));
deadbeefaf1b59c2015-10-15 12:08:41 -0700806 return peer_connection_.get() != nullptr;
807 }
808
deadbeefaf1b59c2015-10-15 12:08:41 -0700809 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800810 rtc::scoped_ptr<cricket::PortAllocator> port_allocator,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800811 const MediaConstraintsInterface* constraints,
812 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800813 // CreatePeerConnection with RTCConfiguration.
814 webrtc::PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 webrtc::PeerConnectionInterface::IceServer ice_server;
816 ice_server.uri = "stun:stun.l.google.com:19302";
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800817 config.servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000818
deadbeefaf1b59c2015-10-15 12:08:41 -0700819 return peer_connection_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800820 config, constraints, std::move(port_allocator),
821 std::move(dtls_identity_store), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 }
823
824 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700825 LOG(INFO) << id_ << "HandleIncomingOffer ";
deadbeeffaac4972015-11-12 15:33:07 -0800826 if (NumberOfLocalMediaStreams() == 0 && auto_add_stream_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 // If we are not sending any streams ourselves it is time to add some.
828 AddMediaStream(true, true);
829 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000830 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700831 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000833 rtc::scoped_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700834 EXPECT_TRUE(DoCreateAnswer(&answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 std::string sdp;
836 EXPECT_TRUE(answer->ToString(&sdp));
837 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
deadbeefaf1b59c2015-10-15 12:08:41 -0700838 if (signaling_message_receiver_) {
839 signaling_message_receiver_->ReceiveSdpMessage(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 webrtc::SessionDescriptionInterface::kAnswer, sdp);
841 }
842 }
843
844 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700845 LOG(INFO) << id_ << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000846 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700847 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
849 }
850
kwiberg2bbff992016-03-16 11:03:04 -0700851 bool DoCreateOfferAnswer(rtc::scoped_ptr<SessionDescriptionInterface>* desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000853 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
854 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 MockCreateSessionDescriptionObserver>());
htaaac2dea2016-03-10 13:35:55 -0800856 if (prefer_constraint_apis_) {
857 if (offer) {
858 pc()->CreateOffer(observer, &offer_answer_constraints_);
859 } else {
860 pc()->CreateAnswer(observer, &offer_answer_constraints_);
861 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 } else {
htaaac2dea2016-03-10 13:35:55 -0800863 if (offer) {
864 pc()->CreateOffer(observer, offer_answer_options_);
865 } else {
866 pc()->CreateAnswer(observer, offer_answer_options_);
867 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 }
869 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
kwiberg2bbff992016-03-16 11:03:04 -0700870 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871 if (observer->result() && ExpectIceRestart()) {
872 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
873 }
874 return observer->result();
875 }
876
kwiberg2bbff992016-03-16 11:03:04 -0700877 bool DoCreateOffer(rtc::scoped_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 return DoCreateOfferAnswer(desc, true);
879 }
880
kwiberg2bbff992016-03-16 11:03:04 -0700881 bool DoCreateAnswer(rtc::scoped_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 return DoCreateOfferAnswer(desc, false);
883 }
884
885 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000886 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
887 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700889 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890 pc()->SetLocalDescription(observer, desc);
891 // Ignore the observer result. If we wait for the result with
892 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
893 // before the offer which is an error.
894 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000895 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000896 // ProcessMessages waits at least 1ms but processes all messages before
897 // returning. Since this test is synchronous and send messages to the remote
898 // peer whenever a callback is invoked, this can lead to messages being
899 // sent to the remote peer in the wrong order.
900 // TODO(perkj): Find a way to check the result without risking that the
901 // order of sent messages are changed. Ex- by posting all messages that are
902 // sent to the remote peer.
903 return true;
904 }
905
906 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000907 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
908 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700910 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 pc()->SetRemoteDescription(observer, desc);
912 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
913 return observer->result();
914 }
915
916 // This modifies all received SDP messages before they are processed.
917 void FilterIncomingSdpMessage(std::string* sdp) {
918 if (remove_msid_) {
919 const char kSdpSsrcAttribute[] = "a=ssrc:";
920 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
921 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
922 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
923 }
924 if (remove_bundle_) {
925 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
926 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
927 }
928 if (remove_sdes_) {
929 const char kSdpSdesCryptoAttribute[] = "a=crypto";
930 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
931 }
932 }
933
deadbeefaf1b59c2015-10-15 12:08:41 -0700934 std::string id_;
935
deadbeefaf1b59c2015-10-15 12:08:41 -0700936 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
937 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
938 peer_connection_factory_;
939
htaaac2dea2016-03-10 13:35:55 -0800940 bool prefer_constraint_apis_ = true;
deadbeeffaac4972015-11-12 15:33:07 -0800941 bool auto_add_stream_ = true;
942
deadbeefaf1b59c2015-10-15 12:08:41 -0700943 typedef std::pair<std::string, std::string> IceUfragPwdPair;
944 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
945 bool expect_ice_restart_ = false;
946
deadbeefc9be0072015-12-14 18:27:57 -0800947 // Needed to keep track of number of frames sent.
deadbeefaf1b59c2015-10-15 12:08:41 -0700948 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
949 // Needed to keep track of number of frames received.
deadbeefc9be0072015-12-14 18:27:57 -0800950 std::map<std::string, rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
951 fake_video_renderers_;
952 // Needed to ensure frames aren't received for removed tracks.
953 std::vector<rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
954 removed_fake_video_renderers_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700955 // Needed to keep track of number of frames received when external decoder
956 // used.
957 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
958 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
959 bool video_decoder_factory_enabled_ = false;
960 webrtc::FakeConstraints video_constraints_;
961
962 // For remote peer communication.
963 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
964
965 // Store references to the video capturers we've created, so that we can stop
966 // them, if required.
967 std::vector<cricket::VideoCapturer*> video_capturers_;
968
htaaac2dea2016-03-10 13:35:55 -0800969 webrtc::FakeConstraints offer_answer_constraints_;
970 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700971 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
972 bool remove_bundle_ =
973 false; // True if bundle should be removed in received SDP.
974 bool remove_sdes_ =
975 false; // True if a=crypto should be removed in received SDP.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000977 rtc::scoped_refptr<DataChannelInterface> data_channel_;
978 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979};
980
deadbeef7c73bdb2015-12-10 15:10:44 -0800981class P2PTestConductor : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982 public:
deadbeef7c73bdb2015-12-10 15:10:44 -0800983 P2PTestConductor()
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000984 : pss_(new rtc::PhysicalSocketServer),
985 ss_(new rtc::VirtualSocketServer(pss_.get())),
986 ss_scope_(ss_.get()) {}
987
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988 bool SessionActive() {
989 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000990 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000992
hta6b4f8392016-03-10 00:24:31 -0800993 // Return true if the number of frames provided have been received
994 // on the video and audio tracks provided.
995 bool FramesHaveArrived(int audio_frames_to_receive,
996 int video_frames_to_receive) {
997 bool all_good = true;
998 if (initiating_client_->HasLocalAudioTrack() &&
999 receiving_client_->can_receive_audio()) {
1000 all_good &=
1001 receiving_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1002 }
1003 if (initiating_client_->HasLocalVideoTrack() &&
1004 receiving_client_->can_receive_video()) {
1005 all_good &=
1006 receiving_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1007 }
1008 if (receiving_client_->HasLocalAudioTrack() &&
1009 initiating_client_->can_receive_audio()) {
1010 all_good &=
1011 initiating_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1012 }
1013 if (receiving_client_->HasLocalVideoTrack() &&
1014 initiating_client_->can_receive_video()) {
1015 all_good &=
1016 initiating_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1017 }
1018 return all_good;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019 }
hta6b4f8392016-03-10 00:24:31 -08001020
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021 void VerifyDtmf() {
1022 initiating_client_->VerifyDtmf();
1023 receiving_client_->VerifyDtmf();
1024 }
1025
1026 void TestUpdateOfferWithRejectedContent() {
deadbeefc9be0072015-12-14 18:27:57 -08001027 // Renegotiate, rejecting the video m-line.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001028 initiating_client_->Negotiate(true, false);
deadbeefc9be0072015-12-14 18:27:57 -08001029 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1030
1031 int pc1_audio_received = initiating_client_->audio_frames_received();
1032 int pc1_video_received = initiating_client_->video_frames_received();
1033 int pc2_audio_received = receiving_client_->audio_frames_received();
1034 int pc2_video_received = receiving_client_->video_frames_received();
1035
1036 // Wait for some additional audio frames to be received.
1037 EXPECT_TRUE_WAIT(initiating_client_->AudioFramesReceivedCheck(
1038 pc1_audio_received + kEndAudioFrameCount) &&
1039 receiving_client_->AudioFramesReceivedCheck(
1040 pc2_audio_received + kEndAudioFrameCount),
1041 kMaxWaitForFramesMs);
1042
1043 // During this time, we shouldn't have received any additional video frames
1044 // for the rejected video tracks.
1045 EXPECT_EQ(pc1_video_received, initiating_client_->video_frames_received());
1046 EXPECT_EQ(pc2_video_received, receiving_client_->video_frames_received());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 }
1048
1049 void VerifyRenderedSize(int width, int height) {
1050 EXPECT_EQ(width, receiving_client()->rendered_width());
1051 EXPECT_EQ(height, receiving_client()->rendered_height());
1052 EXPECT_EQ(width, initializing_client()->rendered_width());
1053 EXPECT_EQ(height, initializing_client()->rendered_height());
1054 }
1055
1056 void VerifySessionDescriptions() {
1057 initiating_client_->VerifyRejectedMediaInSessionDescription();
1058 receiving_client_->VerifyRejectedMediaInSessionDescription();
1059 initiating_client_->VerifyLocalIceUfragAndPassword();
1060 receiving_client_->VerifyLocalIceUfragAndPassword();
1061 }
1062
deadbeef7c73bdb2015-12-10 15:10:44 -08001063 ~P2PTestConductor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001064 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001065 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001066 }
1067 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001068 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001069 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001070 }
1071
deadbeefaf1b59c2015-10-15 12:08:41 -07001072 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073
1074 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1075 MediaConstraintsInterface* recv_constraints) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001076 return CreateTestClients(init_constraints, nullptr, recv_constraints,
1077 nullptr);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001078 }
1079
htaaac2dea2016-03-10 13:35:55 -08001080 bool CreateTestClientsThatPreferNoConstraints() {
1081 initiating_client_.reset(
1082 PeerConnectionTestClient::CreateClientPreferNoConstraints("Caller: ",
1083 nullptr));
1084 receiving_client_.reset(
1085 PeerConnectionTestClient::CreateClientPreferNoConstraints("Callee: ",
1086 nullptr));
1087 if (!initiating_client_ || !receiving_client_) {
1088 return false;
1089 }
1090 // Remember the choice for possible later resets of the clients.
1091 prefer_constraint_apis_ = false;
1092 SetSignalingReceivers();
1093 return true;
1094 }
1095
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001096 void SetSignalingReceivers() {
1097 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
1098 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1099 }
1100
Joachim Bauch04e5b492015-05-29 09:40:39 +02001101 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1102 PeerConnectionFactory::Options* init_options,
1103 MediaConstraintsInterface* recv_constraints,
1104 PeerConnectionFactory::Options* recv_options) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001105 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
1106 "Caller: ", init_constraints, init_options));
1107 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
1108 "Callee: ", recv_constraints, recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001109 if (!initiating_client_ || !receiving_client_) {
1110 return false;
1111 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001112 SetSignalingReceivers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001113 return true;
1114 }
1115
1116 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1117 const webrtc::FakeConstraints& recv_constraints) {
1118 initiating_client_->SetVideoConstraints(init_constraints);
1119 receiving_client_->SetVideoConstraints(recv_constraints);
1120 }
1121
1122 void EnableVideoDecoderFactory() {
1123 initiating_client_->EnableVideoDecoderFactory();
1124 receiving_client_->EnableVideoDecoderFactory();
1125 }
1126
1127 // This test sets up a call between two parties. Both parties send static
1128 // frames to each other. Once the test is finished the number of sent frames
1129 // is compared to the number of received frames.
1130 void LocalP2PTest() {
1131 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1132 initiating_client_->AddMediaStream(true, true);
1133 }
1134 initiating_client_->Negotiate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001135 // Assert true is used here since next tests are guaranteed to fail and
1136 // would eat up 5 seconds.
1137 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1138 VerifySessionDescriptions();
1139
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001140 int audio_frame_count = kEndAudioFrameCount;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001141 int video_frame_count = kEndVideoFrameCount;
hta6b4f8392016-03-10 00:24:31 -08001142 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1143
1144 if ((!initiating_client_->can_receive_audio() &&
1145 !initiating_client_->can_receive_video()) ||
1146 (!receiving_client_->can_receive_audio() &&
1147 !receiving_client_->can_receive_video())) {
1148 // Neither audio nor video will flow, so connections won't be
1149 // established. There's nothing more to check.
1150 // TODO(hta): Check connection if there's a data channel.
1151 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001152 }
1153
hta6b4f8392016-03-10 00:24:31 -08001154 // Audio or video is expected to flow, so both clients should reach the
1155 // Connected state, and the offerer (ICE controller) should proceed to
1156 // Completed.
1157 // Note: These tests have been observed to fail under heavy load at
1158 // shorter timeouts, so they may be flaky.
1159 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1160 initiating_client_->ice_connection_state(),
1161 kMaxWaitForFramesMs);
1162 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1163 receiving_client_->ice_connection_state(),
1164 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001165
hta6b4f8392016-03-10 00:24:31 -08001166 // The ICE gathering state should end up in kIceGatheringComplete,
1167 // but there's a bug that prevents this at the moment, and the state
1168 // machine is being updated by the WEBRTC WG.
1169 // TODO(hta): Update this check when spec revisions finish.
1170 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1171 initiating_client_->ice_gathering_state());
1172 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1173 receiving_client_->ice_gathering_state(),
1174 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001175
hta6b4f8392016-03-10 00:24:31 -08001176 // Check that the expected number of frames have arrived.
1177 EXPECT_TRUE_WAIT(FramesHaveArrived(audio_frame_count, video_frame_count),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001178 kMaxWaitForFramesMs);
1179 }
1180
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001181 void SetupAndVerifyDtlsCall() {
1182 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1183 FakeConstraints setup_constraints;
1184 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1185 true);
1186 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1187 LocalP2PTest();
1188 VerifyRenderedSize(640, 480);
1189 }
1190
1191 PeerConnectionTestClient* CreateDtlsClientWithAlternateKey() {
1192 FakeConstraints setup_constraints;
1193 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1194 true);
1195
1196 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
1197 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
1198 : nullptr);
1199 dtls_identity_store->use_alternate_key();
1200
1201 // Make sure the new client is using a different certificate.
1202 return PeerConnectionTestClient::CreateClientWithDtlsIdentityStore(
kwiberg0eb15ed2015-12-17 03:04:15 -08001203 "New Peer: ", &setup_constraints, nullptr,
htaaac2dea2016-03-10 13:35:55 -08001204 std::move(dtls_identity_store), prefer_constraint_apis_);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001205 }
1206
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001207 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1208 // Messages may get lost on the unreliable DataChannel, so we send multiple
1209 // times to avoid test flakiness.
1210 static const size_t kSendAttempts = 5;
1211
1212 for (size_t i = 0; i < kSendAttempts; ++i) {
1213 dc->Send(DataBuffer(data));
1214 }
1215 }
1216
deadbeefaf1b59c2015-10-15 12:08:41 -07001217 PeerConnectionTestClient* initializing_client() {
1218 return initiating_client_.get();
1219 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001220
1221 // Set the |initiating_client_| to the |client| passed in and return the
1222 // original |initiating_client_|.
1223 PeerConnectionTestClient* set_initializing_client(
1224 PeerConnectionTestClient* client) {
1225 PeerConnectionTestClient* old = initiating_client_.release();
1226 initiating_client_.reset(client);
1227 return old;
1228 }
1229
deadbeefaf1b59c2015-10-15 12:08:41 -07001230 PeerConnectionTestClient* receiving_client() {
1231 return receiving_client_.get();
1232 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001233
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001234 // Set the |receiving_client_| to the |client| passed in and return the
1235 // original |receiving_client_|.
1236 PeerConnectionTestClient* set_receiving_client(
1237 PeerConnectionTestClient* client) {
1238 PeerConnectionTestClient* old = receiving_client_.release();
1239 receiving_client_.reset(client);
1240 return old;
1241 }
1242
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001244 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1245 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1246 rtc::SocketServerScope ss_scope_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001247 rtc::scoped_ptr<PeerConnectionTestClient> initiating_client_;
1248 rtc::scoped_ptr<PeerConnectionTestClient> receiving_client_;
htaaac2dea2016-03-10 13:35:55 -08001249 bool prefer_constraint_apis_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001250};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001251
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001252// Disable for TSan v2, see
1253// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1254#if !defined(THREAD_SANITIZER)
1255
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001256// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001257// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1258// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001259TEST_F(P2PTestConductor, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001260 ASSERT_TRUE(CreateTestClients());
1261 LocalP2PTest();
1262 VerifyDtmf();
1263}
1264
1265// This test sets up a Jsep call between two parties and test that we can get a
1266// video aspect ratio of 16:9.
deadbeef7c73bdb2015-12-10 15:10:44 -08001267TEST_F(P2PTestConductor, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001268 ASSERT_TRUE(CreateTestClients());
1269 FakeConstraints constraint;
1270 double requested_ratio = 640.0/360;
1271 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1272 SetVideoConstraints(constraint, constraint);
1273 LocalP2PTest();
1274
1275 ASSERT_LE(0, initializing_client()->rendered_height());
1276 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001277 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001278 initializing_client()->rendered_height();
1279 EXPECT_LE(requested_ratio, initiating_video_ratio);
1280
1281 ASSERT_LE(0, receiving_client()->rendered_height());
1282 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001283 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001284 receiving_client()->rendered_height();
1285 EXPECT_LE(requested_ratio, receiving_video_ratio);
1286}
1287
1288// This test sets up a Jsep call between two parties and test that the
1289// received video has a resolution of 1280*720.
1290// TODO(mallinath): Enable when
1291// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
deadbeef7c73bdb2015-12-10 15:10:44 -08001292TEST_F(P2PTestConductor, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001293 ASSERT_TRUE(CreateTestClients());
1294 FakeConstraints constraint;
1295 constraint.SetMandatoryMinWidth(1280);
1296 constraint.SetMandatoryMinHeight(720);
1297 SetVideoConstraints(constraint, constraint);
1298 LocalP2PTest();
1299 VerifyRenderedSize(1280, 720);
1300}
1301
1302// This test sets up a call between two endpoints that are configured to use
1303// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001304TEST_F(P2PTestConductor, LocalP2PTestDtls) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001305 SetupAndVerifyDtlsCall();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001306}
1307
hta6b4f8392016-03-10 00:24:31 -08001308// This test sets up an one-way call, with media only from initiator to
1309// responder.
1310TEST_F(P2PTestConductor, OneWayMediaCall) {
1311 ASSERT_TRUE(CreateTestClients());
1312 receiving_client()->set_auto_add_stream(false);
1313 LocalP2PTest();
1314}
1315
htaaac2dea2016-03-10 13:35:55 -08001316TEST_F(P2PTestConductor, OneWayMediaCallWithoutConstraints) {
1317 ASSERT_TRUE(CreateTestClientsThatPreferNoConstraints());
1318 receiving_client()->set_auto_add_stream(false);
1319 LocalP2PTest();
1320}
1321
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001322// This test sets up a audio call initially and then upgrades to audio/video,
1323// using DTLS.
deadbeef7c73bdb2015-12-10 15:10:44 -08001324TEST_F(P2PTestConductor, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001325 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001326 FakeConstraints setup_constraints;
1327 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1328 true);
1329 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1330 receiving_client()->SetReceiveAudioVideo(true, false);
1331 LocalP2PTest();
1332 receiving_client()->SetReceiveAudioVideo(true, true);
1333 receiving_client()->Negotiate();
1334}
1335
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001336// This test sets up a call transfer to a new caller with a different DTLS
1337// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001338TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCallee) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001339 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1340 SetupAndVerifyDtlsCall();
1341
1342 // Keeping the original peer around which will still send packets to the
1343 // receiving client. These SRTP packets will be dropped.
1344 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1345 set_initializing_client(CreateDtlsClientWithAlternateKey()));
1346 original_peer->pc()->Close();
1347
1348 SetSignalingReceivers();
1349 receiving_client()->SetExpectIceRestart(true);
1350 LocalP2PTest();
1351 VerifyRenderedSize(640, 480);
1352}
1353
guoweis46383312015-12-17 16:45:59 -08001354// This test sets up a non-bundle call and apply bundle during ICE restart. When
1355// bundle is in effect in the restart, the channel can successfully reset its
1356// DTLS-SRTP context.
1357TEST_F(P2PTestConductor, LocalP2PTestDtlsBundleInIceRestart) {
1358 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1359 FakeConstraints setup_constraints;
1360 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1361 true);
1362 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1363 receiving_client()->RemoveBundleFromReceivedSdp(true);
1364 LocalP2PTest();
1365 VerifyRenderedSize(640, 480);
1366
1367 initializing_client()->IceRestart();
1368 receiving_client()->SetExpectIceRestart(true);
1369 receiving_client()->RemoveBundleFromReceivedSdp(false);
1370 LocalP2PTest();
1371 VerifyRenderedSize(640, 480);
1372}
1373
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001374// This test sets up a call transfer to a new callee with a different DTLS
1375// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001376TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCaller) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001377 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1378 SetupAndVerifyDtlsCall();
1379
1380 // Keeping the original peer around which will still send packets to the
1381 // receiving client. These SRTP packets will be dropped.
1382 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1383 set_receiving_client(CreateDtlsClientWithAlternateKey()));
1384 original_peer->pc()->Close();
1385
1386 SetSignalingReceivers();
1387 initializing_client()->IceRestart();
1388 LocalP2PTest();
1389 VerifyRenderedSize(640, 480);
1390}
1391
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001392// This test sets up a call between two endpoints that are configured to use
1393// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1394// negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001395TEST_F(P2PTestConductor, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001396 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001397 FakeConstraints setup_constraints;
1398 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1399 true);
1400 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1401 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1402 LocalP2PTest();
1403 VerifyRenderedSize(640, 480);
1404}
1405
1406// This test sets up a Jsep call between two parties, and the callee only
1407// accept to receive video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001408TEST_F(P2PTestConductor, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001409 ASSERT_TRUE(CreateTestClients());
1410 receiving_client()->SetReceiveAudioVideo(false, true);
1411 LocalP2PTest();
1412}
1413
1414// This test sets up a Jsep call between two parties, and the callee only
1415// accept to receive audio.
deadbeef7c73bdb2015-12-10 15:10:44 -08001416TEST_F(P2PTestConductor, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001417 ASSERT_TRUE(CreateTestClients());
1418 receiving_client()->SetReceiveAudioVideo(true, false);
1419 LocalP2PTest();
1420}
1421
1422// This test sets up a Jsep call between two parties, and the callee reject both
1423// audio and video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001424TEST_F(P2PTestConductor, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001425 ASSERT_TRUE(CreateTestClients());
1426 receiving_client()->SetReceiveAudioVideo(false, false);
1427 LocalP2PTest();
1428}
1429
1430// This test sets up an audio and video call between two parties. After the call
1431// runs for a while (10 frames), the caller sends an update offer with video
1432// being rejected. Once the re-negotiation is done, the video flow should stop
1433// and the audio flow should continue.
deadbeefc9be0072015-12-14 18:27:57 -08001434TEST_F(P2PTestConductor, UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001435 ASSERT_TRUE(CreateTestClients());
1436 LocalP2PTest();
1437 TestUpdateOfferWithRejectedContent();
1438}
1439
1440// This test sets up a Jsep call between two parties. The MSID is removed from
1441// the SDP strings from the caller.
deadbeefc9be0072015-12-14 18:27:57 -08001442TEST_F(P2PTestConductor, LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001443 ASSERT_TRUE(CreateTestClients());
1444 receiving_client()->RemoveMsidFromReceivedSdp(true);
1445 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1446 // audio and video is muxed when MSID is disabled. Remove
1447 // SetRemoveBundleFromSdp once
1448 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1449 receiving_client()->RemoveBundleFromReceivedSdp(true);
1450 LocalP2PTest();
1451}
1452
1453// This test sets up a Jsep call between two parties and the initiating peer
1454// sends two steams.
1455// TODO(perkj): Disabled due to
1456// https://code.google.com/p/webrtc/issues/detail?id=1454
deadbeef7c73bdb2015-12-10 15:10:44 -08001457TEST_F(P2PTestConductor, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001458 ASSERT_TRUE(CreateTestClients());
1459 // Set optional video constraint to max 320pixels to decrease CPU usage.
1460 FakeConstraints constraint;
1461 constraint.SetOptionalMaxWidth(320);
1462 SetVideoConstraints(constraint, constraint);
1463 initializing_client()->AddMediaStream(true, true);
1464 initializing_client()->AddMediaStream(false, true);
1465 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1466 LocalP2PTest();
1467 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1468}
1469
1470// Test that we can receive the audio output level from a remote audio track.
deadbeef7c73bdb2015-12-10 15:10:44 -08001471TEST_F(P2PTestConductor, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001472 ASSERT_TRUE(CreateTestClients());
1473 LocalP2PTest();
1474
1475 StreamCollectionInterface* remote_streams =
1476 initializing_client()->remote_streams();
1477 ASSERT_GT(remote_streams->count(), 0u);
1478 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1479 MediaStreamTrackInterface* remote_audio_track =
1480 remote_streams->at(0)->GetAudioTracks()[0];
1481
1482 // Get the audio output level stats. Note that the level is not available
1483 // until a RTCP packet has been received.
1484 EXPECT_TRUE_WAIT(
1485 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1486 kMaxWaitForStatsMs);
1487}
1488
1489// Test that an audio input level is reported.
deadbeef7c73bdb2015-12-10 15:10:44 -08001490TEST_F(P2PTestConductor, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001491 ASSERT_TRUE(CreateTestClients());
1492 LocalP2PTest();
1493
1494 // Get the audio input level stats. The level should be available very
1495 // soon after the test starts.
1496 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1497 kMaxWaitForStatsMs);
1498}
1499
1500// Test that we can get incoming byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001501TEST_F(P2PTestConductor, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001502 ASSERT_TRUE(CreateTestClients());
1503 LocalP2PTest();
1504
1505 StreamCollectionInterface* remote_streams =
1506 initializing_client()->remote_streams();
1507 ASSERT_GT(remote_streams->count(), 0u);
1508 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1509 MediaStreamTrackInterface* remote_audio_track =
1510 remote_streams->at(0)->GetAudioTracks()[0];
1511 EXPECT_TRUE_WAIT(
1512 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1513 kMaxWaitForStatsMs);
1514
1515 MediaStreamTrackInterface* remote_video_track =
1516 remote_streams->at(0)->GetVideoTracks()[0];
1517 EXPECT_TRUE_WAIT(
1518 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1519 kMaxWaitForStatsMs);
1520}
1521
1522// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001523TEST_F(P2PTestConductor, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001524 ASSERT_TRUE(CreateTestClients());
1525 LocalP2PTest();
1526
1527 StreamCollectionInterface* local_streams =
1528 initializing_client()->local_streams();
1529 ASSERT_GT(local_streams->count(), 0u);
1530 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1531 MediaStreamTrackInterface* local_audio_track =
1532 local_streams->at(0)->GetAudioTracks()[0];
1533 EXPECT_TRUE_WAIT(
1534 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1535 kMaxWaitForStatsMs);
1536
1537 MediaStreamTrackInterface* local_video_track =
1538 local_streams->at(0)->GetVideoTracks()[0];
1539 EXPECT_TRUE_WAIT(
1540 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1541 kMaxWaitForStatsMs);
1542}
1543
Joachim Bauch04e5b492015-05-29 09:40:39 +02001544// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001545TEST_F(P2PTestConductor, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001546 PeerConnectionFactory::Options init_options;
1547 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1548 PeerConnectionFactory::Options recv_options;
1549 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001550 ASSERT_TRUE(
1551 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001552 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1553 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1554 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001555 LocalP2PTest();
1556
torbjorng43166b82016-03-11 00:06:47 -08001557 EXPECT_TRUE_WAIT(
1558 rtc::SSLStreamAdapter::IsAcceptableCipher(
1559 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1560 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001561 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001562 initializing_client()->GetSrtpCipherStats(),
1563 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001564 EXPECT_EQ(1,
1565 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1566 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001567}
1568
1569// Test that DTLS 1.2 is used if both ends support it.
torbjorng79a5a832016-01-15 07:16:51 -08001570TEST_F(P2PTestConductor, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001571 PeerConnectionFactory::Options init_options;
1572 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1573 PeerConnectionFactory::Options recv_options;
1574 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001575 ASSERT_TRUE(
1576 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001577 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1578 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1579 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001580 LocalP2PTest();
1581
torbjorng43166b82016-03-11 00:06:47 -08001582 EXPECT_TRUE_WAIT(
1583 rtc::SSLStreamAdapter::IsAcceptableCipher(
1584 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1585 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001586 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001587 initializing_client()->GetSrtpCipherStats(),
1588 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001589 EXPECT_EQ(1,
1590 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1591 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001592}
1593
1594// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1595// received supports 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001596TEST_F(P2PTestConductor, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001597 PeerConnectionFactory::Options init_options;
1598 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1599 PeerConnectionFactory::Options recv_options;
1600 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001601 ASSERT_TRUE(
1602 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001603 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1604 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1605 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001606 LocalP2PTest();
1607
torbjorng43166b82016-03-11 00:06:47 -08001608 EXPECT_TRUE_WAIT(
1609 rtc::SSLStreamAdapter::IsAcceptableCipher(
1610 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1611 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001612 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001613 initializing_client()->GetSrtpCipherStats(),
1614 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001615 EXPECT_EQ(1,
1616 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1617 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001618}
1619
1620// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1621// received supports 1.2.
torbjorng43166b82016-03-11 00:06:47 -08001622TEST_F(P2PTestConductor, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001623 PeerConnectionFactory::Options init_options;
1624 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1625 PeerConnectionFactory::Options recv_options;
1626 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001627 ASSERT_TRUE(
1628 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001629 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1630 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1631 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001632 LocalP2PTest();
1633
torbjorng43166b82016-03-11 00:06:47 -08001634 EXPECT_TRUE_WAIT(
1635 rtc::SSLStreamAdapter::IsAcceptableCipher(
1636 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1637 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001638 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001639 initializing_client()->GetSrtpCipherStats(),
1640 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001641 EXPECT_EQ(1,
1642 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1643 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001644}
1645
deadbeefb5cb19b2015-11-23 16:39:12 -08001646// This test sets up a call between two parties with audio, video and an RTP
1647// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001648TEST_F(P2PTestConductor, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649 FakeConstraints setup_constraints;
1650 setup_constraints.SetAllowRtpDataChannels();
1651 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1652 initializing_client()->CreateDataChannel();
1653 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001654 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1655 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001656 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1657 kMaxWaitMs);
1658 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1659 kMaxWaitMs);
1660
1661 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001662
1663 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1665 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001666
1667 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1669 kMaxWaitMs);
1670
1671 receiving_client()->data_channel()->Close();
1672 // Send new offer and answer.
1673 receiving_client()->Negotiate();
1674 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1675 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1676}
1677
deadbeefb5cb19b2015-11-23 16:39:12 -08001678// This test sets up a call between two parties with audio, video and an SCTP
1679// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001680TEST_F(P2PTestConductor, LocalP2PTestSctpDataChannel) {
deadbeefb5cb19b2015-11-23 16:39:12 -08001681 ASSERT_TRUE(CreateTestClients());
1682 initializing_client()->CreateDataChannel();
1683 LocalP2PTest();
1684 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1685 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
1686 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1687 kMaxWaitMs);
1688 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
1689
1690 std::string data = "hello world";
1691
1692 initializing_client()->data_channel()->Send(DataBuffer(data));
1693 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1694 kMaxWaitMs);
1695
1696 receiving_client()->data_channel()->Send(DataBuffer(data));
1697 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1698 kMaxWaitMs);
1699
1700 receiving_client()->data_channel()->Close();
deadbeef15887932015-12-14 19:32:34 -08001701 EXPECT_TRUE_WAIT(!initializing_client()->data_observer()->IsOpen(),
1702 kMaxWaitMs);
1703 EXPECT_TRUE_WAIT(!receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
deadbeefb5cb19b2015-11-23 16:39:12 -08001704}
1705
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001706// This test sets up a call between two parties and creates a data channel.
1707// The test tests that received data is buffered unless an observer has been
1708// registered.
1709// Rtp data channels can receive data before the underlying
1710// transport has detected that a channel is writable and thus data can be
1711// received before the data channel state changes to open. That is hard to test
1712// but the same buffering is used in that case.
deadbeef7c73bdb2015-12-10 15:10:44 -08001713TEST_F(P2PTestConductor, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714 FakeConstraints setup_constraints;
1715 setup_constraints.SetAllowRtpDataChannels();
1716 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1717 initializing_client()->CreateDataChannel();
1718 initializing_client()->Negotiate();
1719
deadbeefaf1b59c2015-10-15 12:08:41 -07001720 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1721 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1723 kMaxWaitMs);
1724 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1725 receiving_client()->data_channel()->state(), kMaxWaitMs);
1726
1727 // Unregister the existing observer.
1728 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001729
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001731 SendRtpData(initializing_client()->data_channel(), data);
1732
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733 // Wait a while to allow the sent data to arrive before an observer is
1734 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001735 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001736
1737 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1738 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1739}
1740
1741// This test sets up a call between two parties with audio, video and but only
1742// the initiating client support data.
deadbeef7c73bdb2015-12-10 15:10:44 -08001743TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001744 FakeConstraints setup_constraints_1;
1745 setup_constraints_1.SetAllowRtpDataChannels();
1746 // Must disable DTLS to make negotiation succeed.
1747 setup_constraints_1.SetMandatory(
1748 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1749 FakeConstraints setup_constraints_2;
1750 setup_constraints_2.SetMandatory(
1751 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1752 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753 initializing_client()->CreateDataChannel();
1754 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001755 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756 EXPECT_FALSE(receiving_client()->data_channel());
1757 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1758}
1759
1760// This test sets up a call between two parties with audio, video. When audio
1761// and video is setup and flowing and data channel is negotiated.
deadbeef7c73bdb2015-12-10 15:10:44 -08001762TEST_F(P2PTestConductor, AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763 FakeConstraints setup_constraints;
1764 setup_constraints.SetAllowRtpDataChannels();
1765 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1766 LocalP2PTest();
1767 initializing_client()->CreateDataChannel();
1768 // Send new offer and answer.
1769 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07001770 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1771 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1773 kMaxWaitMs);
1774 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1775 kMaxWaitMs);
1776}
1777
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001778// This test sets up a Jsep call with SCTP DataChannel and verifies the
1779// negotiation is completed without error.
1780#ifdef HAVE_SCTP
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01001781// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
1782#if defined(WIN) && defined(_DEBUG)
1783#define MAYBE_CreateOfferWithSctpDataChannel \
1784 DISABLED_CreateOfferWithSctpDataChannel
1785#else
1786#define MAYBE_CreateOfferWithSctpDataChannel CreateOfferWithSctpDataChannel
1787#endif
1788TEST_F(P2PTestConductor, MAYBE_CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001789 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001790 FakeConstraints constraints;
1791 constraints.SetMandatory(
1792 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1793 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1794 initializing_client()->CreateDataChannel();
1795 initializing_client()->Negotiate(false, false);
1796}
1797#endif
1798
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001799// This test sets up a call between two parties with audio, and video.
1800// During the call, the initializing side restart ice and the test verifies that
1801// new ice candidates are generated and audio and video still can flow.
deadbeef7c73bdb2015-12-10 15:10:44 -08001802TEST_F(P2PTestConductor, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001803 ASSERT_TRUE(CreateTestClients());
1804
1805 // Negotiate and wait for ice completion and make sure audio and video plays.
1806 LocalP2PTest();
1807
1808 // Create a SDP string of the first audio candidate for both clients.
1809 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1810 initializing_client()->pc()->local_description()->candidates(0);
1811 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1812 receiving_client()->pc()->local_description()->candidates(0);
1813 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1814 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1815 std::string initiator_candidate;
1816 EXPECT_TRUE(
1817 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1818 std::string receiver_candidate;
1819 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1820
1821 // Restart ice on the initializing client.
1822 receiving_client()->SetExpectIceRestart(true);
1823 initializing_client()->IceRestart();
1824
1825 // Negotiate and wait for ice completion again and make sure audio and video
1826 // plays.
1827 LocalP2PTest();
1828
1829 // Create a SDP string of the first audio candidate for both clients again.
1830 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1831 initializing_client()->pc()->local_description()->candidates(0);
1832 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1833 receiving_client()->pc()->local_description()->candidates(0);
1834 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1835 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1836 std::string initiator_candidate_restart;
1837 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1838 &initiator_candidate_restart));
1839 std::string receiver_candidate_restart;
1840 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1841 &receiver_candidate_restart));
1842
1843 // Verify that the first candidates in the local session descriptions has
1844 // changed.
1845 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1846 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1847}
1848
deadbeeffaac4972015-11-12 15:33:07 -08001849// This test sets up a call between two parties with audio, and video.
1850// It then renegotiates setting the video m-line to "port 0", then later
1851// renegotiates again, enabling video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001852TEST_F(P2PTestConductor, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08001853 ASSERT_TRUE(CreateTestClients());
1854
1855 // Do initial negotiation. Will result in video and audio sendonly m-lines.
1856 receiving_client()->set_auto_add_stream(false);
1857 initializing_client()->AddMediaStream(true, true);
1858 initializing_client()->Negotiate();
1859
1860 // Negotiate again, disabling the video m-line (receiving client will
1861 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
1862 receiving_client()->SetReceiveVideo(false);
1863 initializing_client()->Negotiate();
1864
1865 // Enable video and do negotiation again, making sure video is received
1866 // end-to-end.
1867 receiving_client()->SetReceiveVideo(true);
1868 receiving_client()->AddMediaStream(true, true);
1869 LocalP2PTest();
1870}
1871
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001872// This test sets up a Jsep call between two parties with external
1873// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001874// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1875// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001876TEST_F(P2PTestConductor, DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 ASSERT_TRUE(CreateTestClients());
1878 EnableVideoDecoderFactory();
1879 LocalP2PTest();
1880}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001881
deadbeeffac06552015-11-25 11:26:01 -08001882// This tests that if we negotiate after calling CreateSender but before we
1883// have a track, then set a track later, frames from the newly-set track are
1884// received end-to-end.
deadbeef7c73bdb2015-12-10 15:10:44 -08001885TEST_F(P2PTestConductor, EarlyWarmupTest) {
deadbeeffac06552015-11-25 11:26:01 -08001886 ASSERT_TRUE(CreateTestClients());
deadbeefbd7d8f72015-12-18 16:58:44 -08001887 auto audio_sender =
1888 initializing_client()->pc()->CreateSender("audio", "stream_id");
1889 auto video_sender =
1890 initializing_client()->pc()->CreateSender("video", "stream_id");
deadbeeffac06552015-11-25 11:26:01 -08001891 initializing_client()->Negotiate();
1892 // Wait for ICE connection to complete, without any tracks.
1893 // Note that the receiving client WILL (in HandleIncomingOffer) create
1894 // tracks, so it's only the initiator here that's doing early warmup.
1895 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1896 VerifySessionDescriptions();
1897 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1898 initializing_client()->ice_connection_state(),
1899 kMaxWaitForFramesMs);
1900 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1901 receiving_client()->ice_connection_state(),
1902 kMaxWaitForFramesMs);
1903 // Now set the tracks, and expect frames to immediately start flowing.
1904 EXPECT_TRUE(
1905 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
1906 EXPECT_TRUE(
1907 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
hta6b4f8392016-03-10 00:24:31 -08001908 EXPECT_TRUE_WAIT(FramesHaveArrived(kEndAudioFrameCount, kEndVideoFrameCount),
deadbeeffac06552015-11-25 11:26:01 -08001909 kMaxWaitForFramesMs);
1910}
1911
deadbeef0a6c4ca2015-10-06 11:38:28 -07001912class IceServerParsingTest : public testing::Test {
1913 public:
1914 // Convenience for parsing a single URL.
1915 bool ParseUrl(const std::string& url) {
1916 return ParseUrl(url, std::string(), std::string());
1917 }
1918
1919 bool ParseUrl(const std::string& url,
1920 const std::string& username,
1921 const std::string& password) {
1922 PeerConnectionInterface::IceServers servers;
1923 PeerConnectionInterface::IceServer server;
1924 server.urls.push_back(url);
1925 server.username = username;
1926 server.password = password;
1927 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001928 return webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_);
deadbeef0a6c4ca2015-10-06 11:38:28 -07001929 }
1930
1931 protected:
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001932 cricket::ServerAddresses stun_servers_;
1933 std::vector<cricket::RelayServerConfig> turn_servers_;
deadbeef0a6c4ca2015-10-06 11:38:28 -07001934};
1935
1936// Make sure all STUN/TURN prefixes are parsed correctly.
1937TEST_F(IceServerParsingTest, ParseStunPrefixes) {
1938 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001939 EXPECT_EQ(1U, stun_servers_.size());
1940 EXPECT_EQ(0U, turn_servers_.size());
1941 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001942
1943 EXPECT_TRUE(ParseUrl("stuns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001944 EXPECT_EQ(1U, stun_servers_.size());
1945 EXPECT_EQ(0U, turn_servers_.size());
1946 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001947
1948 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001949 EXPECT_EQ(0U, stun_servers_.size());
1950 EXPECT_EQ(1U, turn_servers_.size());
1951 EXPECT_FALSE(turn_servers_[0].ports[0].secure);
1952 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001953
1954 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001955 EXPECT_EQ(0U, stun_servers_.size());
1956 EXPECT_EQ(1U, turn_servers_.size());
1957 EXPECT_TRUE(turn_servers_[0].ports[0].secure);
1958 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001959
1960 // invalid prefixes
1961 EXPECT_FALSE(ParseUrl("stunn:hostname"));
1962 EXPECT_FALSE(ParseUrl(":hostname"));
1963 EXPECT_FALSE(ParseUrl(":"));
1964 EXPECT_FALSE(ParseUrl(""));
1965}
1966
1967TEST_F(IceServerParsingTest, VerifyDefaults) {
1968 // TURNS defaults
1969 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001970 EXPECT_EQ(1U, turn_servers_.size());
1971 EXPECT_EQ(5349, turn_servers_[0].ports[0].address.port());
1972 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
1973 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001974
1975 // TURN defaults
1976 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001977 EXPECT_EQ(1U, turn_servers_.size());
1978 EXPECT_EQ(3478, turn_servers_[0].ports[0].address.port());
1979 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
1980 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001981
1982 // STUN defaults
1983 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001984 EXPECT_EQ(1U, stun_servers_.size());
1985 EXPECT_EQ(3478, stun_servers_.begin()->port());
1986 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001987}
1988
1989// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
1990// can be parsed correctly.
1991TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
1992 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001993 EXPECT_EQ(1U, stun_servers_.size());
1994 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
1995 EXPECT_EQ(1234, stun_servers_.begin()->port());
1996 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001997
1998 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001999 EXPECT_EQ(1U, stun_servers_.size());
2000 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2001 EXPECT_EQ(4321, stun_servers_.begin()->port());
2002 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002003
2004 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002005 EXPECT_EQ(1U, stun_servers_.size());
2006 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2007 EXPECT_EQ(9999, stun_servers_.begin()->port());
2008 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002009
2010 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002011 EXPECT_EQ(1U, stun_servers_.size());
2012 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2013 EXPECT_EQ(3478, stun_servers_.begin()->port());
2014 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002015
2016 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002017 EXPECT_EQ(1U, stun_servers_.size());
2018 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2019 EXPECT_EQ(3478, stun_servers_.begin()->port());
2020 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002021
2022 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002023 EXPECT_EQ(1U, stun_servers_.size());
2024 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2025 EXPECT_EQ(3478, stun_servers_.begin()->port());
2026 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002027
2028 // Try some invalid hostname:port strings.
2029 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
2030 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002031 EXPECT_FALSE(ParseUrl("stun:hostname:port:more"));
2032 EXPECT_FALSE(ParseUrl("stun:hostname:port more"));
deadbeef0a6c4ca2015-10-06 11:38:28 -07002033 EXPECT_FALSE(ParseUrl("stun:hostname:"));
2034 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
2035 EXPECT_FALSE(ParseUrl("stun::5555"));
2036 EXPECT_FALSE(ParseUrl("stun:"));
2037}
2038
2039// Test parsing the "?transport=xxx" part of the URL.
2040TEST_F(IceServerParsingTest, ParseTransport) {
2041 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002042 EXPECT_EQ(1U, turn_servers_.size());
2043 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
2044 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002045
2046 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002047 EXPECT_EQ(1U, turn_servers_.size());
2048 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2049 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002050
2051 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
2052}
2053
2054// Test parsing ICE username contained in URL.
2055TEST_F(IceServerParsingTest, ParseUsername) {
2056 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002057 EXPECT_EQ(1U, turn_servers_.size());
2058 EXPECT_EQ("user", turn_servers_[0].credentials.username);
2059 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002060
2061 EXPECT_FALSE(ParseUrl("turn:@hostname"));
2062 EXPECT_FALSE(ParseUrl("turn:username@"));
2063 EXPECT_FALSE(ParseUrl("turn:@"));
2064 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
2065}
2066
2067// Test that username and password from IceServer is copied into the resulting
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002068// RelayServerConfig.
deadbeef0a6c4ca2015-10-06 11:38:28 -07002069TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
2070 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002071 EXPECT_EQ(1U, turn_servers_.size());
2072 EXPECT_EQ("username", turn_servers_[0].credentials.username);
2073 EXPECT_EQ("password", turn_servers_[0].credentials.password);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002074}
2075
2076// Ensure that if a server has multiple URLs, each one is parsed.
2077TEST_F(IceServerParsingTest, ParseMultipleUrls) {
2078 PeerConnectionInterface::IceServers servers;
2079 PeerConnectionInterface::IceServer server;
2080 server.urls.push_back("stun:hostname");
2081 server.urls.push_back("turn:hostname");
2082 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002083 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2084 EXPECT_EQ(1U, stun_servers_.size());
2085 EXPECT_EQ(1U, turn_servers_.size());
deadbeef0a6c4ca2015-10-06 11:38:28 -07002086}
2087
Taylor Brandstetter893505d2016-01-07 15:12:48 -08002088// Ensure that TURN servers are given unique priorities,
2089// so that their resulting candidates have unique priorities.
2090TEST_F(IceServerParsingTest, TurnServerPrioritiesUnique) {
2091 PeerConnectionInterface::IceServers servers;
2092 PeerConnectionInterface::IceServer server;
2093 server.urls.push_back("turn:hostname");
2094 server.urls.push_back("turn:hostname2");
2095 servers.push_back(server);
2096 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2097 EXPECT_EQ(2U, turn_servers_.size());
2098 EXPECT_NE(turn_servers_[0].priority, turn_servers_[1].priority);
2099}
2100
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00002101#endif // if !defined(THREAD_SANITIZER)
hta6b4f8392016-03-10 00:24:31 -08002102
2103} // namespace