blob: a19f2cc8a4fc7dde0c1cc06ba145affc9ee45437 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
11#include <stdio.h>
12
13#include <algorithm>
14#include <list>
15#include <map>
kwiberg0eb15ed2015-12-17 03:04:15 -080016#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include <vector>
18
Henrik Kjellander15583c12016-02-10 10:53:12 +010019#include "webrtc/api/dtmfsender.h"
20#include "webrtc/api/fakemetricsobserver.h"
21#include "webrtc/api/localaudiosource.h"
22#include "webrtc/api/mediastreaminterface.h"
23#include "webrtc/api/peerconnection.h"
24#include "webrtc/api/peerconnectionfactory.h"
25#include "webrtc/api/peerconnectioninterface.h"
26#include "webrtc/api/test/fakeaudiocapturemodule.h"
27#include "webrtc/api/test/fakeconstraints.h"
28#include "webrtc/api/test/fakedtlsidentitystore.h"
29#include "webrtc/api/test/fakeperiodicvideocapturer.h"
30#include "webrtc/api/test/fakevideotrackrenderer.h"
31#include "webrtc/api/test/mockpeerconnectionobservers.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000032#include "webrtc/base/gunit.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000033#include "webrtc/base/physicalsocketserver.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/scoped_ptr.h"
35#include "webrtc/base/ssladapter.h"
36#include "webrtc/base/sslstreamadapter.h"
37#include "webrtc/base/thread.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000038#include "webrtc/base/virtualsocketserver.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010039#include "webrtc/media/engine/fakewebrtcvideoengine.h"
kjellanderf4752772016-03-02 05:42:30 -080040#include "webrtc/p2p/base/p2pconstants.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000041#include "webrtc/p2p/base/sessiondescription.h"
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080042#include "webrtc/p2p/client/fakeportallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010043#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45#define MAYBE_SKIP_TEST(feature) \
46 if (!(feature())) { \
47 LOG(LS_INFO) << "Feature disabled... skipping"; \
48 return; \
49 }
50
51using cricket::ContentInfo;
52using cricket::FakeWebRtcVideoDecoder;
53using cricket::FakeWebRtcVideoDecoderFactory;
54using cricket::FakeWebRtcVideoEncoder;
55using cricket::FakeWebRtcVideoEncoderFactory;
56using cricket::MediaContentDescription;
57using webrtc::DataBuffer;
58using webrtc::DataChannelInterface;
59using webrtc::DtmfSender;
60using webrtc::DtmfSenderInterface;
61using webrtc::DtmfSenderObserverInterface;
62using webrtc::FakeConstraints;
63using webrtc::MediaConstraintsInterface;
deadbeeffaac4972015-11-12 15:33:07 -080064using webrtc::MediaStreamInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065using webrtc::MediaStreamTrackInterface;
66using webrtc::MockCreateSessionDescriptionObserver;
67using webrtc::MockDataChannelObserver;
68using webrtc::MockSetSessionDescriptionObserver;
69using webrtc::MockStatsObserver;
deadbeeffaac4972015-11-12 15:33:07 -080070using webrtc::ObserverInterface;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000071using webrtc::PeerConnectionInterface;
Joachim Bauch04e5b492015-05-29 09:40:39 +020072using webrtc::PeerConnectionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073using webrtc::SessionDescriptionInterface;
74using webrtc::StreamCollectionInterface;
75
hta6b4f8392016-03-10 00:24:31 -080076namespace {
77
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000078static const int kMaxWaitMs = 10000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000079// Disable for TSan v2, see
80// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
81// This declaration is also #ifdef'd as it causes uninitialized-variable
82// warnings.
83#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084static const int kMaxWaitForStatsMs = 3000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000085#endif
deadbeeffac06552015-11-25 11:26:01 -080086static const int kMaxWaitForActivationMs = 5000;
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000087static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088static const int kEndAudioFrameCount = 3;
89static const int kEndVideoFrameCount = 3;
90
91static const char kStreamLabelBase[] = "stream_label";
92static const char kVideoTrackLabelBase[] = "video_track";
93static const char kAudioTrackLabelBase[] = "audio_track";
94static const char kDataChannelLabel[] = "data_channel";
95
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +000096// Disable for TSan v2, see
97// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
98// This declaration is also #ifdef'd as it causes unused-variable errors.
99#if !defined(THREAD_SANITIZER)
100// SRTP cipher name negotiated by the tests. This must be updated if the
101// default changes.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800102static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000103#endif
104
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105static void RemoveLinesFromSdp(const std::string& line_start,
106 std::string* sdp) {
107 const char kSdpLineEnd[] = "\r\n";
108 size_t ssrc_pos = 0;
109 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
110 std::string::npos) {
111 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
112 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
113 }
114}
115
hta6b4f8392016-03-10 00:24:31 -0800116bool StreamsHaveAudioTrack(StreamCollectionInterface* streams) {
117 for (size_t idx = 0; idx < streams->count(); idx++) {
118 auto stream = streams->at(idx);
119 if (stream->GetAudioTracks().size() > 0) {
120 return true;
121 }
122 }
123 return false;
124}
125
126bool StreamsHaveVideoTrack(StreamCollectionInterface* streams) {
127 for (size_t idx = 0; idx < streams->count(); idx++) {
128 auto stream = streams->at(idx);
129 if (stream->GetVideoTracks().size() > 0) {
130 return true;
131 }
132 }
133 return false;
134}
135
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136class SignalingMessageReceiver {
137 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138 virtual void ReceiveSdpMessage(const std::string& type,
139 std::string& msg) = 0;
140 virtual void ReceiveIceMessage(const std::string& sdp_mid,
141 int sdp_mline_index,
142 const std::string& msg) = 0;
143
144 protected:
deadbeefaf1b59c2015-10-15 12:08:41 -0700145 SignalingMessageReceiver() {}
146 virtual ~SignalingMessageReceiver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147};
148
deadbeefaf1b59c2015-10-15 12:08:41 -0700149class PeerConnectionTestClient : public webrtc::PeerConnectionObserver,
deadbeeffaac4972015-11-12 15:33:07 -0800150 public SignalingMessageReceiver,
151 public ObserverInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 public:
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800153 static PeerConnectionTestClient* CreateClientWithDtlsIdentityStore(
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800154 const std::string& id,
155 const MediaConstraintsInterface* constraints,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800156 const PeerConnectionFactory::Options* options,
htaaac2dea2016-03-10 13:35:55 -0800157 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store,
158 bool prefer_constraint_apis) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800159 PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
htaaac2dea2016-03-10 13:35:55 -0800160 if (!client->Init(constraints, options, std::move(dtls_identity_store),
161 prefer_constraint_apis)) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800162 delete client;
163 return nullptr;
164 }
165 return client;
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800166 }
167
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800168 static PeerConnectionTestClient* CreateClient(
169 const std::string& id,
170 const MediaConstraintsInterface* constraints,
171 const PeerConnectionFactory::Options* options) {
172 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
173 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
174 : nullptr);
175
htaaac2dea2016-03-10 13:35:55 -0800176 return CreateClientWithDtlsIdentityStore(
177 id, constraints, options, std::move(dtls_identity_store), true);
178 }
179
180 static PeerConnectionTestClient* CreateClientPreferNoConstraints(
181 const std::string& id,
182 const PeerConnectionFactory::Options* options) {
183 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
184 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
185 : nullptr);
186
187 return CreateClientWithDtlsIdentityStore(
188 id, nullptr, options, std::move(dtls_identity_store), false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800189 }
190
deadbeefaf1b59c2015-10-15 12:08:41 -0700191 ~PeerConnectionTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 }
193
deadbeefaf1b59c2015-10-15 12:08:41 -0700194 void Negotiate() { Negotiate(true, true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195
deadbeefaf1b59c2015-10-15 12:08:41 -0700196 void Negotiate(bool audio, bool video) {
197 rtc::scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700198 ASSERT_TRUE(DoCreateOffer(&offer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199
deadbeefaf1b59c2015-10-15 12:08:41 -0700200 if (offer->description()->GetContentByName("audio")) {
201 offer->description()->GetContentByName("audio")->rejected = !audio;
202 }
203 if (offer->description()->GetContentByName("video")) {
204 offer->description()->GetContentByName("video")->rejected = !video;
205 }
206
207 std::string sdp;
208 EXPECT_TRUE(offer->ToString(&sdp));
209 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
210 signaling_message_receiver_->ReceiveSdpMessage(
211 webrtc::SessionDescriptionInterface::kOffer, sdp);
212 }
213
214 // SignalingMessageReceiver callback.
215 void ReceiveSdpMessage(const std::string& type, std::string& msg) override {
216 FilterIncomingSdpMessage(&msg);
217 if (type == webrtc::SessionDescriptionInterface::kOffer) {
218 HandleIncomingOffer(msg);
219 } else {
220 HandleIncomingAnswer(msg);
221 }
222 }
223
224 // SignalingMessageReceiver callback.
225 void ReceiveIceMessage(const std::string& sdp_mid,
226 int sdp_mline_index,
227 const std::string& msg) override {
228 LOG(INFO) << id_ << "ReceiveIceMessage";
229 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
230 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
231 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
232 }
233
234 // PeerConnectionObserver callbacks.
235 void OnSignalingChange(
236 webrtc::PeerConnectionInterface::SignalingState new_state) override {
237 EXPECT_EQ(pc()->signaling_state(), new_state);
238 }
deadbeeffaac4972015-11-12 15:33:07 -0800239 void OnAddStream(MediaStreamInterface* media_stream) override {
240 media_stream->RegisterObserver(this);
deadbeefaf1b59c2015-10-15 12:08:41 -0700241 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
242 const std::string id = media_stream->GetVideoTracks()[i]->id();
243 ASSERT_TRUE(fake_video_renderers_.find(id) ==
244 fake_video_renderers_.end());
deadbeefc9be0072015-12-14 18:27:57 -0800245 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
246 media_stream->GetVideoTracks()[i]));
deadbeefaf1b59c2015-10-15 12:08:41 -0700247 }
248 }
deadbeeffaac4972015-11-12 15:33:07 -0800249 void OnRemoveStream(MediaStreamInterface* media_stream) override {}
deadbeefaf1b59c2015-10-15 12:08:41 -0700250 void OnRenegotiationNeeded() override {}
251 void OnIceConnectionChange(
252 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
253 EXPECT_EQ(pc()->ice_connection_state(), new_state);
254 }
255 void OnIceGatheringChange(
256 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
257 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
258 }
259 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
260 LOG(INFO) << id_ << "OnIceCandidate";
261
262 std::string ice_sdp;
263 EXPECT_TRUE(candidate->ToString(&ice_sdp));
264 if (signaling_message_receiver_ == nullptr) {
265 // Remote party may be deleted.
266 return;
267 }
268 signaling_message_receiver_->ReceiveIceMessage(
269 candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
270 }
271
deadbeeffaac4972015-11-12 15:33:07 -0800272 // MediaStreamInterface callback
273 void OnChanged() override {
274 // Track added or removed from MediaStream, so update our renderers.
275 rtc::scoped_refptr<StreamCollectionInterface> remote_streams =
276 pc()->remote_streams();
277 // Remove renderers for tracks that were removed.
278 for (auto it = fake_video_renderers_.begin();
279 it != fake_video_renderers_.end();) {
280 if (remote_streams->FindVideoTrack(it->first) == nullptr) {
deadbeefc9be0072015-12-14 18:27:57 -0800281 auto to_remove = it++;
282 removed_fake_video_renderers_.push_back(std::move(to_remove->second));
283 fake_video_renderers_.erase(to_remove);
deadbeeffaac4972015-11-12 15:33:07 -0800284 } else {
285 ++it;
286 }
287 }
288 // Create renderers for new video tracks.
289 for (size_t stream_index = 0; stream_index < remote_streams->count();
290 ++stream_index) {
291 MediaStreamInterface* remote_stream = remote_streams->at(stream_index);
292 for (size_t track_index = 0;
293 track_index < remote_stream->GetVideoTracks().size();
294 ++track_index) {
295 const std::string id =
296 remote_stream->GetVideoTracks()[track_index]->id();
297 if (fake_video_renderers_.find(id) != fake_video_renderers_.end()) {
298 continue;
299 }
deadbeefc9be0072015-12-14 18:27:57 -0800300 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
301 remote_stream->GetVideoTracks()[track_index]));
deadbeeffaac4972015-11-12 15:33:07 -0800302 }
303 }
304 }
305
deadbeefaf1b59c2015-10-15 12:08:41 -0700306 void SetVideoConstraints(const webrtc::FakeConstraints& video_constraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307 video_constraints_ = video_constraint;
308 }
309
310 void AddMediaStream(bool audio, bool video) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700311 std::string stream_label =
312 kStreamLabelBase +
313 rtc::ToString<int>(static_cast<int>(pc()->local_streams()->count()));
deadbeeffaac4972015-11-12 15:33:07 -0800314 rtc::scoped_refptr<MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000315 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316
317 if (audio && can_receive_audio()) {
deadbeeffac06552015-11-25 11:26:01 -0800318 stream->AddTrack(CreateLocalAudioTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319 }
320 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000321 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322 }
323
deadbeefaf1b59c2015-10-15 12:08:41 -0700324 EXPECT_TRUE(pc()->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325 }
326
deadbeefaf1b59c2015-10-15 12:08:41 -0700327 size_t NumberOfLocalMediaStreams() { return pc()->local_streams()->count(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328
329 bool SessionActive() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700330 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331 }
332
deadbeeffaac4972015-11-12 15:33:07 -0800333 // Automatically add a stream when receiving an offer, if we don't have one.
334 // Defaults to true.
335 void set_auto_add_stream(bool auto_add_stream) {
336 auto_add_stream_ = auto_add_stream;
337 }
338
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339 void set_signaling_message_receiver(
deadbeefaf1b59c2015-10-15 12:08:41 -0700340 SignalingMessageReceiver* signaling_message_receiver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341 signaling_message_receiver_ = signaling_message_receiver;
342 }
343
344 void EnableVideoDecoderFactory() {
345 video_decoder_factory_enabled_ = true;
346 fake_video_decoder_factory_->AddSupportedVideoCodecType(
347 webrtc::kVideoCodecVP8);
348 }
349
deadbeefaf1b59c2015-10-15 12:08:41 -0700350 void IceRestart() {
htaaac2dea2016-03-10 13:35:55 -0800351 offer_answer_constraints_.SetMandatoryIceRestart(true);
352 offer_answer_options_.ice_restart = true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700353 SetExpectIceRestart(true);
354 }
355
356 void SetExpectIceRestart(bool expect_restart) {
357 expect_ice_restart_ = expect_restart;
358 }
359
360 bool ExpectIceRestart() const { return expect_ice_restart_; }
361
362 void SetReceiveAudioVideo(bool audio, bool video) {
363 SetReceiveAudio(audio);
364 SetReceiveVideo(video);
365 ASSERT_EQ(audio, can_receive_audio());
366 ASSERT_EQ(video, can_receive_video());
367 }
368
369 void SetReceiveAudio(bool audio) {
370 if (audio && can_receive_audio())
371 return;
htaaac2dea2016-03-10 13:35:55 -0800372 offer_answer_constraints_.SetMandatoryReceiveAudio(audio);
373 offer_answer_options_.offer_to_receive_audio = audio ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700374 }
375
376 void SetReceiveVideo(bool video) {
377 if (video && can_receive_video())
378 return;
htaaac2dea2016-03-10 13:35:55 -0800379 offer_answer_constraints_.SetMandatoryReceiveVideo(video);
380 offer_answer_options_.offer_to_receive_video = video ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700381 }
382
383 void RemoveMsidFromReceivedSdp(bool remove) { remove_msid_ = remove; }
384
385 void RemoveSdesCryptoFromReceivedSdp(bool remove) { remove_sdes_ = remove; }
386
387 void RemoveBundleFromReceivedSdp(bool remove) { remove_bundle_ = remove; }
388
perkjcaafdba2016-03-20 07:34:29 -0700389 void RemoveCvoFromReceivedSdp(bool remove) { remove_cvo_ = remove; }
390
deadbeefaf1b59c2015-10-15 12:08:41 -0700391 bool can_receive_audio() {
392 bool value;
htaaac2dea2016-03-10 13:35:55 -0800393 if (prefer_constraint_apis_) {
394 if (webrtc::FindConstraint(
395 &offer_answer_constraints_,
396 MediaConstraintsInterface::kOfferToReceiveAudio, &value,
397 nullptr)) {
398 return value;
399 }
400 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700401 }
htaaac2dea2016-03-10 13:35:55 -0800402 return offer_answer_options_.offer_to_receive_audio > 0 ||
403 offer_answer_options_.offer_to_receive_audio ==
404 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700405 }
406
407 bool can_receive_video() {
408 bool value;
htaaac2dea2016-03-10 13:35:55 -0800409 if (prefer_constraint_apis_) {
410 if (webrtc::FindConstraint(
411 &offer_answer_constraints_,
412 MediaConstraintsInterface::kOfferToReceiveVideo, &value,
413 nullptr)) {
414 return value;
415 }
416 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700417 }
htaaac2dea2016-03-10 13:35:55 -0800418 return offer_answer_options_.offer_to_receive_video > 0 ||
419 offer_answer_options_.offer_to_receive_video ==
420 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700421 }
422
deadbeefaf1b59c2015-10-15 12:08:41 -0700423 void OnDataChannel(DataChannelInterface* data_channel) override {
424 LOG(INFO) << id_ << "OnDataChannel";
425 data_channel_ = data_channel;
426 data_observer_.reset(new MockDataChannelObserver(data_channel));
427 }
428
429 void CreateDataChannel() {
430 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, nullptr);
431 ASSERT_TRUE(data_channel_.get() != nullptr);
432 data_observer_.reset(new MockDataChannelObserver(data_channel_));
433 }
434
deadbeeffac06552015-11-25 11:26:01 -0800435 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack(
436 const std::string& stream_label) {
437 FakeConstraints constraints;
438 // Disable highpass filter so that we can get all the test audio frames.
439 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
440 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
441 peer_connection_factory_->CreateAudioSource(&constraints);
442 // TODO(perkj): Test audio source when it is implemented. Currently audio
443 // always use the default input.
444 std::string label = stream_label + kAudioTrackLabelBase;
445 return peer_connection_factory_->CreateAudioTrack(label, source);
446 }
447
448 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack(
449 const std::string& stream_label) {
450 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
451 FakeConstraints source_constraints = video_constraints_;
452 source_constraints.SetMandatoryMaxFrameRate(10);
453
454 cricket::FakeVideoCapturer* fake_capturer =
455 new webrtc::FakePeriodicVideoCapturer();
perkjcaafdba2016-03-20 07:34:29 -0700456 fake_capturer->SetRotation(capture_rotation_);
deadbeeffac06552015-11-25 11:26:01 -0800457 video_capturers_.push_back(fake_capturer);
perkja3ede6c2016-03-08 01:27:48 +0100458 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
deadbeeffac06552015-11-25 11:26:01 -0800459 peer_connection_factory_->CreateVideoSource(fake_capturer,
460 &source_constraints);
461 std::string label = stream_label + kVideoTrackLabelBase;
perkjcaafdba2016-03-20 07:34:29 -0700462
463 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
464 peer_connection_factory_->CreateVideoTrack(label, source));
465 if (!local_video_renderer_) {
466 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
467 }
468 return track;
deadbeeffac06552015-11-25 11:26:01 -0800469 }
470
deadbeefaf1b59c2015-10-15 12:08:41 -0700471 DataChannelInterface* data_channel() { return data_channel_; }
472 const MockDataChannelObserver* data_observer() const {
473 return data_observer_.get();
474 }
475
hta6b4f8392016-03-10 00:24:31 -0800476 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
deadbeefaf1b59c2015-10-15 12:08:41 -0700477
478 void StopVideoCapturers() {
perkjcaafdba2016-03-20 07:34:29 -0700479 for (auto* capturer : video_capturers_) {
480 capturer->Stop();
deadbeefaf1b59c2015-10-15 12:08:41 -0700481 }
482 }
483
perkjcaafdba2016-03-20 07:34:29 -0700484 void SetCaptureRotation(webrtc::VideoRotation rotation) {
485 ASSERT_TRUE(video_capturers_.empty());
486 capture_rotation_ = rotation;
487 }
488
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 bool AudioFramesReceivedCheck(int number_of_frames) const {
490 return number_of_frames <= fake_audio_capture_module_->frames_received();
491 }
492
deadbeefc9be0072015-12-14 18:27:57 -0800493 int audio_frames_received() const {
494 return fake_audio_capture_module_->frames_received();
495 }
496
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 bool VideoFramesReceivedCheck(int number_of_frames) {
498 if (video_decoder_factory_enabled_) {
499 const std::vector<FakeWebRtcVideoDecoder*>& decoders
500 = fake_video_decoder_factory_->decoders();
501 if (decoders.empty()) {
502 return number_of_frames <= 0;
503 }
hta6b4f8392016-03-10 00:24:31 -0800504 // Note - this checks that EACH decoder has the requisite number
505 // of frames. The video_frames_received() function sums them.
deadbeefc9be0072015-12-14 18:27:57 -0800506 for (FakeWebRtcVideoDecoder* decoder : decoders) {
507 if (number_of_frames > decoder->GetNumFramesReceived()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508 return false;
509 }
510 }
511 return true;
512 } else {
513 if (fake_video_renderers_.empty()) {
514 return number_of_frames <= 0;
515 }
516
deadbeefc9be0072015-12-14 18:27:57 -0800517 for (const auto& pair : fake_video_renderers_) {
518 if (number_of_frames > pair.second->num_rendered_frames()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519 return false;
520 }
521 }
522 return true;
523 }
524 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700525
deadbeefc9be0072015-12-14 18:27:57 -0800526 int video_frames_received() const {
527 int total = 0;
528 if (video_decoder_factory_enabled_) {
529 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
530 fake_video_decoder_factory_->decoders();
531 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
532 total += decoder->GetNumFramesReceived();
533 }
534 } else {
535 for (const auto& pair : fake_video_renderers_) {
536 total += pair.second->num_rendered_frames();
537 }
538 for (const auto& renderer : removed_fake_video_renderers_) {
539 total += renderer->num_rendered_frames();
540 }
541 }
542 return total;
543 }
544
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545 // Verify the CreateDtmfSender interface
546 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000547 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
548 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549
550 // We can't create a DTMF sender with an invalid audio track or a non local
551 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700552 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000553 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700554 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
555 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556
557 // We should be able to create a DTMF sender from a local track.
558 webrtc::AudioTrackInterface* localtrack =
559 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
560 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700561 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562 dtmf_sender->RegisterObserver(observer.get());
563
564 // Test the DtmfSender object just created.
565 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
566 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
567
568 // We don't need to verify that the DTMF tones are actually sent out because
569 // that is already covered by the tests of the lower level components.
570
571 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
572 std::vector<std::string> tones;
573 tones.push_back("1");
574 tones.push_back("a");
575 tones.push_back("");
576 observer->Verify(tones);
577
578 dtmf_sender->UnregisterObserver();
579 }
580
581 // Verifies that the SessionDescription have rejected the appropriate media
582 // content.
583 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700584 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
585 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586 const cricket::SessionDescription* remote_desc =
587 peer_connection_->remote_description()->description();
588 const cricket::SessionDescription* local_desc =
589 peer_connection_->local_description()->description();
590
591 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
592 if (remote_audio_content) {
593 const ContentInfo* audio_content =
594 GetFirstAudioContent(local_desc);
595 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
596 }
597
598 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
599 if (remote_video_content) {
600 const ContentInfo* video_content =
601 GetFirstVideoContent(local_desc);
602 EXPECT_EQ(can_receive_video(), !video_content->rejected);
603 }
604 }
605
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700607 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 const cricket::SessionDescription* desc =
609 peer_connection_->local_description()->description();
610 const cricket::ContentInfos& contents = desc->contents();
611
612 for (size_t index = 0; index < contents.size(); ++index) {
613 if (contents[index].rejected)
614 continue;
615 const cricket::TransportDescription* transport_desc =
616 desc->GetTransportDescriptionByName(contents[index].name);
617
618 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000619 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620 if (ufragpair_it == ice_ufrag_pwd_.end()) {
621 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000622 ice_ufrag_pwd_[static_cast<int>(index)] =
623 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624 } else if (ExpectIceRestart()) {
625 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
626 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
627 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
628 } else {
629 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
630 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
631 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
632 }
633 }
634 }
635
636 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000637 rtc::scoped_refptr<MockStatsObserver>
638 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000639 EXPECT_TRUE(peer_connection_->GetStats(
640 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700642 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 return observer->AudioOutputLevel();
644 }
645
646 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000647 rtc::scoped_refptr<MockStatsObserver>
648 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000649 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700650 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700652 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653 return observer->AudioInputLevel();
654 }
655
656 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000657 rtc::scoped_refptr<MockStatsObserver>
658 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000659 EXPECT_TRUE(peer_connection_->GetStats(
660 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700662 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000663 return observer->BytesReceived();
664 }
665
666 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000667 rtc::scoped_refptr<MockStatsObserver>
668 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000669 EXPECT_TRUE(peer_connection_->GetStats(
670 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700672 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000673 return observer->BytesSent();
674 }
675
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000676 int GetAvailableReceivedBandwidthStats() {
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));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000681 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700682 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000683 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000684 return bw;
685 }
686
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000687 std::string GetDtlsCipherStats() {
688 rtc::scoped_refptr<MockStatsObserver>
689 observer(new rtc::RefCountedObject<MockStatsObserver>());
690 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700691 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000692 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700693 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000694 return observer->DtlsCipher();
695 }
696
697 std::string GetSrtpCipherStats() {
698 rtc::scoped_refptr<MockStatsObserver>
699 observer(new rtc::RefCountedObject<MockStatsObserver>());
700 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700701 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000702 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700703 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000704 return observer->SrtpCipher();
705 }
706
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 int rendered_width() {
708 EXPECT_FALSE(fake_video_renderers_.empty());
709 return fake_video_renderers_.empty() ? 1 :
710 fake_video_renderers_.begin()->second->width();
711 }
712
713 int rendered_height() {
714 EXPECT_FALSE(fake_video_renderers_.empty());
715 return fake_video_renderers_.empty() ? 1 :
716 fake_video_renderers_.begin()->second->height();
717 }
718
perkjcaafdba2016-03-20 07:34:29 -0700719 webrtc::VideoRotation rendered_rotation() {
720 EXPECT_FALSE(fake_video_renderers_.empty());
721 return fake_video_renderers_.empty()
722 ? webrtc::kVideoRotation_0
723 : fake_video_renderers_.begin()->second->rotation();
724 }
725
726 int local_rendered_width() {
727 return local_video_renderer_ ? local_video_renderer_->width() : 1;
728 }
729
730 int local_rendered_height() {
731 return local_video_renderer_ ? local_video_renderer_->height() : 1;
732 }
733
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734 size_t number_of_remote_streams() {
735 if (!pc())
736 return 0;
737 return pc()->remote_streams()->count();
738 }
739
hta6b4f8392016-03-10 00:24:31 -0800740 StreamCollectionInterface* remote_streams() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 if (!pc()) {
742 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700743 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 }
745 return pc()->remote_streams();
746 }
747
748 StreamCollectionInterface* local_streams() {
749 if (!pc()) {
750 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700751 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752 }
753 return pc()->local_streams();
754 }
755
hta6b4f8392016-03-10 00:24:31 -0800756 bool HasLocalAudioTrack() { return StreamsHaveAudioTrack(local_streams()); }
757
758 bool HasLocalVideoTrack() { return StreamsHaveVideoTrack(local_streams()); }
759
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
761 return pc()->signaling_state();
762 }
763
764 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
765 return pc()->ice_connection_state();
766 }
767
768 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
769 return pc()->ice_gathering_state();
770 }
771
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772 private:
773 class DummyDtmfObserver : public DtmfSenderObserverInterface {
774 public:
775 DummyDtmfObserver() : completed_(false) {}
776
777 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700778 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779 tones_.push_back(tone);
780 if (tone.empty()) {
781 completed_ = true;
782 }
783 }
784
785 void Verify(const std::vector<std::string>& tones) const {
786 ASSERT_TRUE(tones_.size() == tones.size());
787 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
788 }
789
790 bool completed() const { return completed_; }
791
792 private:
793 bool completed_;
794 std::vector<std::string> tones_;
795 };
796
deadbeefaf1b59c2015-10-15 12:08:41 -0700797 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
798
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800799 bool Init(
800 const MediaConstraintsInterface* constraints,
801 const PeerConnectionFactory::Options* options,
htaaac2dea2016-03-10 13:35:55 -0800802 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store,
803 bool prefer_constraint_apis) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700804 EXPECT_TRUE(!peer_connection_);
805 EXPECT_TRUE(!peer_connection_factory_);
htaaac2dea2016-03-10 13:35:55 -0800806 if (!prefer_constraint_apis) {
807 EXPECT_TRUE(!constraints);
808 }
809 prefer_constraint_apis_ = prefer_constraint_apis;
810
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800811 rtc::scoped_ptr<cricket::PortAllocator> port_allocator(
812 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
deadbeefaf1b59c2015-10-15 12:08:41 -0700813 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
814
815 if (fake_audio_capture_module_ == nullptr) {
816 return false;
817 }
818 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
819 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
820 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
821 rtc::Thread::Current(), rtc::Thread::Current(),
822 fake_audio_capture_module_, fake_video_encoder_factory_,
823 fake_video_decoder_factory_);
824 if (!peer_connection_factory_) {
825 return false;
826 }
827 if (options) {
828 peer_connection_factory_->SetOptions(*options);
829 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800830 peer_connection_ = CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800831 std::move(port_allocator), constraints, std::move(dtls_identity_store));
deadbeefaf1b59c2015-10-15 12:08:41 -0700832 return peer_connection_.get() != nullptr;
833 }
834
deadbeefaf1b59c2015-10-15 12:08:41 -0700835 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800836 rtc::scoped_ptr<cricket::PortAllocator> port_allocator,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800837 const MediaConstraintsInterface* constraints,
838 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800839 // CreatePeerConnection with RTCConfiguration.
840 webrtc::PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 webrtc::PeerConnectionInterface::IceServer ice_server;
842 ice_server.uri = "stun:stun.l.google.com:19302";
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800843 config.servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000844
deadbeefaf1b59c2015-10-15 12:08:41 -0700845 return peer_connection_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800846 config, constraints, std::move(port_allocator),
847 std::move(dtls_identity_store), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 }
849
850 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700851 LOG(INFO) << id_ << "HandleIncomingOffer ";
deadbeeffaac4972015-11-12 15:33:07 -0800852 if (NumberOfLocalMediaStreams() == 0 && auto_add_stream_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 // If we are not sending any streams ourselves it is time to add some.
854 AddMediaStream(true, true);
855 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000856 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700857 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000859 rtc::scoped_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700860 EXPECT_TRUE(DoCreateAnswer(&answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 std::string sdp;
862 EXPECT_TRUE(answer->ToString(&sdp));
863 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
deadbeefaf1b59c2015-10-15 12:08:41 -0700864 if (signaling_message_receiver_) {
865 signaling_message_receiver_->ReceiveSdpMessage(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866 webrtc::SessionDescriptionInterface::kAnswer, sdp);
867 }
868 }
869
870 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700871 LOG(INFO) << id_ << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000872 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700873 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
875 }
876
kwiberg2bbff992016-03-16 11:03:04 -0700877 bool DoCreateOfferAnswer(rtc::scoped_ptr<SessionDescriptionInterface>* desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000879 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
880 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881 MockCreateSessionDescriptionObserver>());
htaaac2dea2016-03-10 13:35:55 -0800882 if (prefer_constraint_apis_) {
883 if (offer) {
884 pc()->CreateOffer(observer, &offer_answer_constraints_);
885 } else {
886 pc()->CreateAnswer(observer, &offer_answer_constraints_);
887 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 } else {
htaaac2dea2016-03-10 13:35:55 -0800889 if (offer) {
890 pc()->CreateOffer(observer, offer_answer_options_);
891 } else {
892 pc()->CreateAnswer(observer, offer_answer_options_);
893 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894 }
895 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
kwiberg2bbff992016-03-16 11:03:04 -0700896 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897 if (observer->result() && ExpectIceRestart()) {
898 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
899 }
900 return observer->result();
901 }
902
kwiberg2bbff992016-03-16 11:03:04 -0700903 bool DoCreateOffer(rtc::scoped_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 return DoCreateOfferAnswer(desc, true);
905 }
906
kwiberg2bbff992016-03-16 11:03:04 -0700907 bool DoCreateAnswer(rtc::scoped_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 return DoCreateOfferAnswer(desc, false);
909 }
910
911 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000912 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
913 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700915 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916 pc()->SetLocalDescription(observer, desc);
917 // Ignore the observer result. If we wait for the result with
918 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
919 // before the offer which is an error.
920 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000921 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 // ProcessMessages waits at least 1ms but processes all messages before
923 // returning. Since this test is synchronous and send messages to the remote
924 // peer whenever a callback is invoked, this can lead to messages being
925 // sent to the remote peer in the wrong order.
926 // TODO(perkj): Find a way to check the result without risking that the
927 // order of sent messages are changed. Ex- by posting all messages that are
928 // sent to the remote peer.
929 return true;
930 }
931
932 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000933 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
934 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700936 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937 pc()->SetRemoteDescription(observer, desc);
938 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
939 return observer->result();
940 }
941
942 // This modifies all received SDP messages before they are processed.
943 void FilterIncomingSdpMessage(std::string* sdp) {
944 if (remove_msid_) {
945 const char kSdpSsrcAttribute[] = "a=ssrc:";
946 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
947 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
948 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
949 }
950 if (remove_bundle_) {
951 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
952 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
953 }
954 if (remove_sdes_) {
955 const char kSdpSdesCryptoAttribute[] = "a=crypto";
956 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
957 }
perkjcaafdba2016-03-20 07:34:29 -0700958 if (remove_cvo_) {
959 const char kSdpCvoExtenstion[] = "urn:3gpp:video-orientation";
960 RemoveLinesFromSdp(kSdpCvoExtenstion, sdp);
961 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962 }
963
deadbeefaf1b59c2015-10-15 12:08:41 -0700964 std::string id_;
965
deadbeefaf1b59c2015-10-15 12:08:41 -0700966 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
967 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
968 peer_connection_factory_;
969
htaaac2dea2016-03-10 13:35:55 -0800970 bool prefer_constraint_apis_ = true;
deadbeeffaac4972015-11-12 15:33:07 -0800971 bool auto_add_stream_ = true;
972
deadbeefaf1b59c2015-10-15 12:08:41 -0700973 typedef std::pair<std::string, std::string> IceUfragPwdPair;
974 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
975 bool expect_ice_restart_ = false;
976
deadbeefc9be0072015-12-14 18:27:57 -0800977 // Needed to keep track of number of frames sent.
deadbeefaf1b59c2015-10-15 12:08:41 -0700978 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
979 // Needed to keep track of number of frames received.
deadbeefc9be0072015-12-14 18:27:57 -0800980 std::map<std::string, rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
981 fake_video_renderers_;
982 // Needed to ensure frames aren't received for removed tracks.
983 std::vector<rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
984 removed_fake_video_renderers_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700985 // Needed to keep track of number of frames received when external decoder
986 // used.
987 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
988 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
989 bool video_decoder_factory_enabled_ = false;
990 webrtc::FakeConstraints video_constraints_;
991
992 // For remote peer communication.
993 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
994
995 // Store references to the video capturers we've created, so that we can stop
996 // them, if required.
perkjcaafdba2016-03-20 07:34:29 -0700997 std::vector<cricket::FakeVideoCapturer*> video_capturers_;
998 webrtc::VideoRotation capture_rotation_ = webrtc::kVideoRotation_0;
999 // |local_video_renderer_| attached to the first created local video track.
1000 rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001001
htaaac2dea2016-03-10 13:35:55 -08001002 webrtc::FakeConstraints offer_answer_constraints_;
1003 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001004 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
1005 bool remove_bundle_ =
1006 false; // True if bundle should be removed in received SDP.
1007 bool remove_sdes_ =
1008 false; // True if a=crypto should be removed in received SDP.
perkjcaafdba2016-03-20 07:34:29 -07001009 // |remove_cvo_| is true if extension urn:3gpp:video-orientation should be
1010 // removed in the received SDP.
1011 bool remove_cvo_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001013 rtc::scoped_refptr<DataChannelInterface> data_channel_;
1014 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015};
1016
deadbeef7c73bdb2015-12-10 15:10:44 -08001017class P2PTestConductor : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018 public:
deadbeef7c73bdb2015-12-10 15:10:44 -08001019 P2PTestConductor()
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001020 : pss_(new rtc::PhysicalSocketServer),
1021 ss_(new rtc::VirtualSocketServer(pss_.get())),
1022 ss_scope_(ss_.get()) {}
1023
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001024 bool SessionActive() {
1025 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001026 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001028
hta6b4f8392016-03-10 00:24:31 -08001029 // Return true if the number of frames provided have been received
1030 // on the video and audio tracks provided.
1031 bool FramesHaveArrived(int audio_frames_to_receive,
1032 int video_frames_to_receive) {
1033 bool all_good = true;
1034 if (initiating_client_->HasLocalAudioTrack() &&
1035 receiving_client_->can_receive_audio()) {
1036 all_good &=
1037 receiving_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1038 }
1039 if (initiating_client_->HasLocalVideoTrack() &&
1040 receiving_client_->can_receive_video()) {
1041 all_good &=
1042 receiving_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1043 }
1044 if (receiving_client_->HasLocalAudioTrack() &&
1045 initiating_client_->can_receive_audio()) {
1046 all_good &=
1047 initiating_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1048 }
1049 if (receiving_client_->HasLocalVideoTrack() &&
1050 initiating_client_->can_receive_video()) {
1051 all_good &=
1052 initiating_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1053 }
1054 return all_good;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001055 }
hta6b4f8392016-03-10 00:24:31 -08001056
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001057 void VerifyDtmf() {
1058 initiating_client_->VerifyDtmf();
1059 receiving_client_->VerifyDtmf();
1060 }
1061
1062 void TestUpdateOfferWithRejectedContent() {
deadbeefc9be0072015-12-14 18:27:57 -08001063 // Renegotiate, rejecting the video m-line.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001064 initiating_client_->Negotiate(true, false);
deadbeefc9be0072015-12-14 18:27:57 -08001065 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1066
1067 int pc1_audio_received = initiating_client_->audio_frames_received();
1068 int pc1_video_received = initiating_client_->video_frames_received();
1069 int pc2_audio_received = receiving_client_->audio_frames_received();
1070 int pc2_video_received = receiving_client_->video_frames_received();
1071
1072 // Wait for some additional audio frames to be received.
1073 EXPECT_TRUE_WAIT(initiating_client_->AudioFramesReceivedCheck(
1074 pc1_audio_received + kEndAudioFrameCount) &&
1075 receiving_client_->AudioFramesReceivedCheck(
1076 pc2_audio_received + kEndAudioFrameCount),
1077 kMaxWaitForFramesMs);
1078
1079 // During this time, we shouldn't have received any additional video frames
1080 // for the rejected video tracks.
1081 EXPECT_EQ(pc1_video_received, initiating_client_->video_frames_received());
1082 EXPECT_EQ(pc2_video_received, receiving_client_->video_frames_received());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001083 }
1084
1085 void VerifyRenderedSize(int width, int height) {
perkjcaafdba2016-03-20 07:34:29 -07001086 VerifyRenderedSize(width, height, webrtc::kVideoRotation_0);
1087 }
1088
1089 void VerifyRenderedSize(int width,
1090 int height,
1091 webrtc::VideoRotation rotation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092 EXPECT_EQ(width, receiving_client()->rendered_width());
1093 EXPECT_EQ(height, receiving_client()->rendered_height());
perkjcaafdba2016-03-20 07:34:29 -07001094 EXPECT_EQ(rotation, receiving_client()->rendered_rotation());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095 EXPECT_EQ(width, initializing_client()->rendered_width());
1096 EXPECT_EQ(height, initializing_client()->rendered_height());
perkjcaafdba2016-03-20 07:34:29 -07001097 EXPECT_EQ(rotation, initializing_client()->rendered_rotation());
1098
1099 // Verify size of the local preview.
1100 EXPECT_EQ(width, initializing_client()->local_rendered_width());
1101 EXPECT_EQ(height, initializing_client()->local_rendered_height());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001102 }
1103
1104 void VerifySessionDescriptions() {
1105 initiating_client_->VerifyRejectedMediaInSessionDescription();
1106 receiving_client_->VerifyRejectedMediaInSessionDescription();
1107 initiating_client_->VerifyLocalIceUfragAndPassword();
1108 receiving_client_->VerifyLocalIceUfragAndPassword();
1109 }
1110
deadbeef7c73bdb2015-12-10 15:10:44 -08001111 ~P2PTestConductor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001112 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001113 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001114 }
1115 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001116 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001117 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118 }
1119
deadbeefaf1b59c2015-10-15 12:08:41 -07001120 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001121
1122 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1123 MediaConstraintsInterface* recv_constraints) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001124 return CreateTestClients(init_constraints, nullptr, recv_constraints,
1125 nullptr);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001126 }
1127
htaaac2dea2016-03-10 13:35:55 -08001128 bool CreateTestClientsThatPreferNoConstraints() {
1129 initiating_client_.reset(
1130 PeerConnectionTestClient::CreateClientPreferNoConstraints("Caller: ",
1131 nullptr));
1132 receiving_client_.reset(
1133 PeerConnectionTestClient::CreateClientPreferNoConstraints("Callee: ",
1134 nullptr));
1135 if (!initiating_client_ || !receiving_client_) {
1136 return false;
1137 }
1138 // Remember the choice for possible later resets of the clients.
1139 prefer_constraint_apis_ = false;
1140 SetSignalingReceivers();
1141 return true;
1142 }
1143
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001144 void SetSignalingReceivers() {
1145 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
1146 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1147 }
1148
Joachim Bauch04e5b492015-05-29 09:40:39 +02001149 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1150 PeerConnectionFactory::Options* init_options,
1151 MediaConstraintsInterface* recv_constraints,
1152 PeerConnectionFactory::Options* recv_options) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001153 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
1154 "Caller: ", init_constraints, init_options));
1155 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
1156 "Callee: ", recv_constraints, recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157 if (!initiating_client_ || !receiving_client_) {
1158 return false;
1159 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001160 SetSignalingReceivers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161 return true;
1162 }
1163
1164 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1165 const webrtc::FakeConstraints& recv_constraints) {
1166 initiating_client_->SetVideoConstraints(init_constraints);
1167 receiving_client_->SetVideoConstraints(recv_constraints);
1168 }
1169
perkjcaafdba2016-03-20 07:34:29 -07001170 void SetCaptureRotation(webrtc::VideoRotation rotation) {
1171 initiating_client_->SetCaptureRotation(rotation);
1172 receiving_client_->SetCaptureRotation(rotation);
1173 }
1174
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001175 void EnableVideoDecoderFactory() {
1176 initiating_client_->EnableVideoDecoderFactory();
1177 receiving_client_->EnableVideoDecoderFactory();
1178 }
1179
1180 // This test sets up a call between two parties. Both parties send static
1181 // frames to each other. Once the test is finished the number of sent frames
1182 // is compared to the number of received frames.
1183 void LocalP2PTest() {
1184 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1185 initiating_client_->AddMediaStream(true, true);
1186 }
1187 initiating_client_->Negotiate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001188 // Assert true is used here since next tests are guaranteed to fail and
1189 // would eat up 5 seconds.
1190 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1191 VerifySessionDescriptions();
1192
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001193 int audio_frame_count = kEndAudioFrameCount;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194 int video_frame_count = kEndVideoFrameCount;
hta6b4f8392016-03-10 00:24:31 -08001195 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1196
1197 if ((!initiating_client_->can_receive_audio() &&
1198 !initiating_client_->can_receive_video()) ||
1199 (!receiving_client_->can_receive_audio() &&
1200 !receiving_client_->can_receive_video())) {
1201 // Neither audio nor video will flow, so connections won't be
1202 // established. There's nothing more to check.
1203 // TODO(hta): Check connection if there's a data channel.
1204 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205 }
1206
hta6b4f8392016-03-10 00:24:31 -08001207 // Audio or video is expected to flow, so both clients should reach the
1208 // Connected state, and the offerer (ICE controller) should proceed to
1209 // Completed.
1210 // Note: These tests have been observed to fail under heavy load at
1211 // shorter timeouts, so they may be flaky.
1212 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1213 initiating_client_->ice_connection_state(),
1214 kMaxWaitForFramesMs);
1215 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1216 receiving_client_->ice_connection_state(),
1217 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001218
hta6b4f8392016-03-10 00:24:31 -08001219 // The ICE gathering state should end up in kIceGatheringComplete,
1220 // but there's a bug that prevents this at the moment, and the state
1221 // machine is being updated by the WEBRTC WG.
1222 // TODO(hta): Update this check when spec revisions finish.
1223 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1224 initiating_client_->ice_gathering_state());
1225 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1226 receiving_client_->ice_gathering_state(),
1227 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001228
hta6b4f8392016-03-10 00:24:31 -08001229 // Check that the expected number of frames have arrived.
1230 EXPECT_TRUE_WAIT(FramesHaveArrived(audio_frame_count, video_frame_count),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231 kMaxWaitForFramesMs);
1232 }
1233
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001234 void SetupAndVerifyDtlsCall() {
1235 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1236 FakeConstraints setup_constraints;
1237 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1238 true);
1239 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1240 LocalP2PTest();
1241 VerifyRenderedSize(640, 480);
1242 }
1243
1244 PeerConnectionTestClient* CreateDtlsClientWithAlternateKey() {
1245 FakeConstraints setup_constraints;
1246 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1247 true);
1248
1249 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
1250 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
1251 : nullptr);
1252 dtls_identity_store->use_alternate_key();
1253
1254 // Make sure the new client is using a different certificate.
1255 return PeerConnectionTestClient::CreateClientWithDtlsIdentityStore(
kwiberg0eb15ed2015-12-17 03:04:15 -08001256 "New Peer: ", &setup_constraints, nullptr,
htaaac2dea2016-03-10 13:35:55 -08001257 std::move(dtls_identity_store), prefer_constraint_apis_);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001258 }
1259
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001260 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1261 // Messages may get lost on the unreliable DataChannel, so we send multiple
1262 // times to avoid test flakiness.
1263 static const size_t kSendAttempts = 5;
1264
1265 for (size_t i = 0; i < kSendAttempts; ++i) {
1266 dc->Send(DataBuffer(data));
1267 }
1268 }
1269
deadbeefaf1b59c2015-10-15 12:08:41 -07001270 PeerConnectionTestClient* initializing_client() {
1271 return initiating_client_.get();
1272 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001273
1274 // Set the |initiating_client_| to the |client| passed in and return the
1275 // original |initiating_client_|.
1276 PeerConnectionTestClient* set_initializing_client(
1277 PeerConnectionTestClient* client) {
1278 PeerConnectionTestClient* old = initiating_client_.release();
1279 initiating_client_.reset(client);
1280 return old;
1281 }
1282
deadbeefaf1b59c2015-10-15 12:08:41 -07001283 PeerConnectionTestClient* receiving_client() {
1284 return receiving_client_.get();
1285 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001286
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001287 // Set the |receiving_client_| to the |client| passed in and return the
1288 // original |receiving_client_|.
1289 PeerConnectionTestClient* set_receiving_client(
1290 PeerConnectionTestClient* client) {
1291 PeerConnectionTestClient* old = receiving_client_.release();
1292 receiving_client_.reset(client);
1293 return old;
1294 }
1295
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001296 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001297 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1298 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1299 rtc::SocketServerScope ss_scope_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001300 rtc::scoped_ptr<PeerConnectionTestClient> initiating_client_;
1301 rtc::scoped_ptr<PeerConnectionTestClient> receiving_client_;
htaaac2dea2016-03-10 13:35:55 -08001302 bool prefer_constraint_apis_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001303};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001305// Disable for TSan v2, see
1306// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1307#if !defined(THREAD_SANITIZER)
1308
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001309// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001310// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1311// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001312TEST_F(P2PTestConductor, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313 ASSERT_TRUE(CreateTestClients());
1314 LocalP2PTest();
1315 VerifyDtmf();
1316}
1317
1318// This test sets up a Jsep call between two parties and test that we can get a
1319// video aspect ratio of 16:9.
deadbeef7c73bdb2015-12-10 15:10:44 -08001320TEST_F(P2PTestConductor, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321 ASSERT_TRUE(CreateTestClients());
1322 FakeConstraints constraint;
1323 double requested_ratio = 640.0/360;
1324 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1325 SetVideoConstraints(constraint, constraint);
1326 LocalP2PTest();
1327
1328 ASSERT_LE(0, initializing_client()->rendered_height());
1329 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001330 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001331 initializing_client()->rendered_height();
1332 EXPECT_LE(requested_ratio, initiating_video_ratio);
1333
1334 ASSERT_LE(0, receiving_client()->rendered_height());
1335 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001336 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001337 receiving_client()->rendered_height();
1338 EXPECT_LE(requested_ratio, receiving_video_ratio);
1339}
1340
1341// This test sets up a Jsep call between two parties and test that the
1342// received video has a resolution of 1280*720.
1343// TODO(mallinath): Enable when
1344// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
deadbeef7c73bdb2015-12-10 15:10:44 -08001345TEST_F(P2PTestConductor, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346 ASSERT_TRUE(CreateTestClients());
1347 FakeConstraints constraint;
1348 constraint.SetMandatoryMinWidth(1280);
1349 constraint.SetMandatoryMinHeight(720);
1350 SetVideoConstraints(constraint, constraint);
1351 LocalP2PTest();
1352 VerifyRenderedSize(1280, 720);
1353}
1354
1355// This test sets up a call between two endpoints that are configured to use
1356// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001357TEST_F(P2PTestConductor, LocalP2PTestDtls) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001358 SetupAndVerifyDtlsCall();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001359}
1360
hta6b4f8392016-03-10 00:24:31 -08001361// This test sets up an one-way call, with media only from initiator to
1362// responder.
1363TEST_F(P2PTestConductor, OneWayMediaCall) {
1364 ASSERT_TRUE(CreateTestClients());
1365 receiving_client()->set_auto_add_stream(false);
1366 LocalP2PTest();
1367}
1368
htaaac2dea2016-03-10 13:35:55 -08001369TEST_F(P2PTestConductor, OneWayMediaCallWithoutConstraints) {
1370 ASSERT_TRUE(CreateTestClientsThatPreferNoConstraints());
1371 receiving_client()->set_auto_add_stream(false);
1372 LocalP2PTest();
1373}
1374
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001375// This test sets up a audio call initially and then upgrades to audio/video,
1376// using DTLS.
deadbeef7c73bdb2015-12-10 15:10:44 -08001377TEST_F(P2PTestConductor, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001378 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001379 FakeConstraints setup_constraints;
1380 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1381 true);
1382 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1383 receiving_client()->SetReceiveAudioVideo(true, false);
1384 LocalP2PTest();
1385 receiving_client()->SetReceiveAudioVideo(true, true);
1386 receiving_client()->Negotiate();
1387}
1388
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001389// This test sets up a call transfer to a new caller with a different DTLS
1390// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001391TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCallee) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001392 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1393 SetupAndVerifyDtlsCall();
1394
1395 // Keeping the original peer around which will still send packets to the
1396 // receiving client. These SRTP packets will be dropped.
1397 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1398 set_initializing_client(CreateDtlsClientWithAlternateKey()));
1399 original_peer->pc()->Close();
1400
1401 SetSignalingReceivers();
1402 receiving_client()->SetExpectIceRestart(true);
1403 LocalP2PTest();
1404 VerifyRenderedSize(640, 480);
1405}
1406
guoweis46383312015-12-17 16:45:59 -08001407// This test sets up a non-bundle call and apply bundle during ICE restart. When
1408// bundle is in effect in the restart, the channel can successfully reset its
1409// DTLS-SRTP context.
1410TEST_F(P2PTestConductor, LocalP2PTestDtlsBundleInIceRestart) {
1411 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1412 FakeConstraints setup_constraints;
1413 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1414 true);
1415 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1416 receiving_client()->RemoveBundleFromReceivedSdp(true);
1417 LocalP2PTest();
1418 VerifyRenderedSize(640, 480);
1419
1420 initializing_client()->IceRestart();
1421 receiving_client()->SetExpectIceRestart(true);
1422 receiving_client()->RemoveBundleFromReceivedSdp(false);
1423 LocalP2PTest();
1424 VerifyRenderedSize(640, 480);
1425}
1426
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001427// This test sets up a call transfer to a new callee with a different DTLS
1428// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001429TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCaller) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001430 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1431 SetupAndVerifyDtlsCall();
1432
1433 // Keeping the original peer around which will still send packets to the
1434 // receiving client. These SRTP packets will be dropped.
1435 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1436 set_receiving_client(CreateDtlsClientWithAlternateKey()));
1437 original_peer->pc()->Close();
1438
1439 SetSignalingReceivers();
1440 initializing_client()->IceRestart();
1441 LocalP2PTest();
1442 VerifyRenderedSize(640, 480);
1443}
1444
perkjcaafdba2016-03-20 07:34:29 -07001445TEST_F(P2PTestConductor, LocalP2PTestCVO) {
1446 ASSERT_TRUE(CreateTestClients());
1447 SetCaptureRotation(webrtc::kVideoRotation_90);
1448 LocalP2PTest();
1449 VerifyRenderedSize(640, 480, webrtc::kVideoRotation_90);
1450}
1451
1452TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportCVO) {
1453 ASSERT_TRUE(CreateTestClients());
1454 SetCaptureRotation(webrtc::kVideoRotation_90);
1455 receiving_client()->RemoveCvoFromReceivedSdp(true);
1456 LocalP2PTest();
1457 VerifyRenderedSize(480, 640, webrtc::kVideoRotation_0);
1458}
1459
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001460// This test sets up a call between two endpoints that are configured to use
1461// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1462// negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001463TEST_F(P2PTestConductor, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001464 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001465 FakeConstraints setup_constraints;
1466 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1467 true);
1468 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1469 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1470 LocalP2PTest();
1471 VerifyRenderedSize(640, 480);
1472}
1473
1474// This test sets up a Jsep call between two parties, and the callee only
1475// accept to receive video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001476TEST_F(P2PTestConductor, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001477 ASSERT_TRUE(CreateTestClients());
1478 receiving_client()->SetReceiveAudioVideo(false, true);
1479 LocalP2PTest();
1480}
1481
1482// This test sets up a Jsep call between two parties, and the callee only
1483// accept to receive audio.
deadbeef7c73bdb2015-12-10 15:10:44 -08001484TEST_F(P2PTestConductor, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001485 ASSERT_TRUE(CreateTestClients());
1486 receiving_client()->SetReceiveAudioVideo(true, false);
1487 LocalP2PTest();
1488}
1489
1490// This test sets up a Jsep call between two parties, and the callee reject both
1491// audio and video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001492TEST_F(P2PTestConductor, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001493 ASSERT_TRUE(CreateTestClients());
1494 receiving_client()->SetReceiveAudioVideo(false, false);
1495 LocalP2PTest();
1496}
1497
1498// This test sets up an audio and video call between two parties. After the call
1499// runs for a while (10 frames), the caller sends an update offer with video
1500// being rejected. Once the re-negotiation is done, the video flow should stop
1501// and the audio flow should continue.
deadbeefc9be0072015-12-14 18:27:57 -08001502TEST_F(P2PTestConductor, UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001503 ASSERT_TRUE(CreateTestClients());
1504 LocalP2PTest();
1505 TestUpdateOfferWithRejectedContent();
1506}
1507
1508// This test sets up a Jsep call between two parties. The MSID is removed from
1509// the SDP strings from the caller.
deadbeefc9be0072015-12-14 18:27:57 -08001510TEST_F(P2PTestConductor, LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511 ASSERT_TRUE(CreateTestClients());
1512 receiving_client()->RemoveMsidFromReceivedSdp(true);
1513 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1514 // audio and video is muxed when MSID is disabled. Remove
1515 // SetRemoveBundleFromSdp once
1516 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1517 receiving_client()->RemoveBundleFromReceivedSdp(true);
1518 LocalP2PTest();
1519}
1520
1521// This test sets up a Jsep call between two parties and the initiating peer
1522// sends two steams.
1523// TODO(perkj): Disabled due to
1524// https://code.google.com/p/webrtc/issues/detail?id=1454
deadbeef7c73bdb2015-12-10 15:10:44 -08001525TEST_F(P2PTestConductor, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001526 ASSERT_TRUE(CreateTestClients());
1527 // Set optional video constraint to max 320pixels to decrease CPU usage.
1528 FakeConstraints constraint;
1529 constraint.SetOptionalMaxWidth(320);
1530 SetVideoConstraints(constraint, constraint);
1531 initializing_client()->AddMediaStream(true, true);
1532 initializing_client()->AddMediaStream(false, true);
1533 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1534 LocalP2PTest();
1535 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1536}
1537
1538// Test that we can receive the audio output level from a remote audio track.
deadbeef7c73bdb2015-12-10 15:10:44 -08001539TEST_F(P2PTestConductor, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540 ASSERT_TRUE(CreateTestClients());
1541 LocalP2PTest();
1542
1543 StreamCollectionInterface* remote_streams =
1544 initializing_client()->remote_streams();
1545 ASSERT_GT(remote_streams->count(), 0u);
1546 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1547 MediaStreamTrackInterface* remote_audio_track =
1548 remote_streams->at(0)->GetAudioTracks()[0];
1549
1550 // Get the audio output level stats. Note that the level is not available
1551 // until a RTCP packet has been received.
1552 EXPECT_TRUE_WAIT(
1553 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1554 kMaxWaitForStatsMs);
1555}
1556
1557// Test that an audio input level is reported.
deadbeef7c73bdb2015-12-10 15:10:44 -08001558TEST_F(P2PTestConductor, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001559 ASSERT_TRUE(CreateTestClients());
1560 LocalP2PTest();
1561
1562 // Get the audio input level stats. The level should be available very
1563 // soon after the test starts.
1564 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1565 kMaxWaitForStatsMs);
1566}
1567
1568// Test that we can get incoming byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001569TEST_F(P2PTestConductor, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570 ASSERT_TRUE(CreateTestClients());
1571 LocalP2PTest();
1572
1573 StreamCollectionInterface* remote_streams =
1574 initializing_client()->remote_streams();
1575 ASSERT_GT(remote_streams->count(), 0u);
1576 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1577 MediaStreamTrackInterface* remote_audio_track =
1578 remote_streams->at(0)->GetAudioTracks()[0];
1579 EXPECT_TRUE_WAIT(
1580 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1581 kMaxWaitForStatsMs);
1582
1583 MediaStreamTrackInterface* remote_video_track =
1584 remote_streams->at(0)->GetVideoTracks()[0];
1585 EXPECT_TRUE_WAIT(
1586 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1587 kMaxWaitForStatsMs);
1588}
1589
1590// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001591TEST_F(P2PTestConductor, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001592 ASSERT_TRUE(CreateTestClients());
1593 LocalP2PTest();
1594
1595 StreamCollectionInterface* local_streams =
1596 initializing_client()->local_streams();
1597 ASSERT_GT(local_streams->count(), 0u);
1598 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1599 MediaStreamTrackInterface* local_audio_track =
1600 local_streams->at(0)->GetAudioTracks()[0];
1601 EXPECT_TRUE_WAIT(
1602 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1603 kMaxWaitForStatsMs);
1604
1605 MediaStreamTrackInterface* local_video_track =
1606 local_streams->at(0)->GetVideoTracks()[0];
1607 EXPECT_TRUE_WAIT(
1608 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1609 kMaxWaitForStatsMs);
1610}
1611
Joachim Bauch04e5b492015-05-29 09:40:39 +02001612// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001613TEST_F(P2PTestConductor, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001614 PeerConnectionFactory::Options init_options;
1615 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1616 PeerConnectionFactory::Options recv_options;
1617 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001618 ASSERT_TRUE(
1619 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001620 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1621 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1622 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001623 LocalP2PTest();
1624
torbjorng43166b82016-03-11 00:06:47 -08001625 EXPECT_TRUE_WAIT(
1626 rtc::SSLStreamAdapter::IsAcceptableCipher(
1627 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1628 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001629 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001630 initializing_client()->GetSrtpCipherStats(),
1631 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001632 EXPECT_EQ(1,
1633 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1634 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001635}
1636
1637// Test that DTLS 1.2 is used if both ends support it.
torbjorng79a5a832016-01-15 07:16:51 -08001638TEST_F(P2PTestConductor, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001639 PeerConnectionFactory::Options init_options;
1640 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1641 PeerConnectionFactory::Options recv_options;
1642 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001643 ASSERT_TRUE(
1644 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001645 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1646 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1647 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001648 LocalP2PTest();
1649
torbjorng43166b82016-03-11 00:06:47 -08001650 EXPECT_TRUE_WAIT(
1651 rtc::SSLStreamAdapter::IsAcceptableCipher(
1652 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1653 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001654 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001655 initializing_client()->GetSrtpCipherStats(),
1656 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001657 EXPECT_EQ(1,
1658 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1659 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001660}
1661
1662// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1663// received supports 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001664TEST_F(P2PTestConductor, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001665 PeerConnectionFactory::Options init_options;
1666 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1667 PeerConnectionFactory::Options recv_options;
1668 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001669 ASSERT_TRUE(
1670 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001671 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1672 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1673 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001674 LocalP2PTest();
1675
torbjorng43166b82016-03-11 00:06:47 -08001676 EXPECT_TRUE_WAIT(
1677 rtc::SSLStreamAdapter::IsAcceptableCipher(
1678 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1679 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001680 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001681 initializing_client()->GetSrtpCipherStats(),
1682 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001683 EXPECT_EQ(1,
1684 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1685 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001686}
1687
1688// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1689// received supports 1.2.
torbjorng43166b82016-03-11 00:06:47 -08001690TEST_F(P2PTestConductor, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001691 PeerConnectionFactory::Options init_options;
1692 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1693 PeerConnectionFactory::Options recv_options;
1694 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001695 ASSERT_TRUE(
1696 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001697 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1698 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1699 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001700 LocalP2PTest();
1701
torbjorng43166b82016-03-11 00:06:47 -08001702 EXPECT_TRUE_WAIT(
1703 rtc::SSLStreamAdapter::IsAcceptableCipher(
1704 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1705 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001706 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001707 initializing_client()->GetSrtpCipherStats(),
1708 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001709 EXPECT_EQ(1,
1710 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1711 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001712}
1713
deadbeefb5cb19b2015-11-23 16:39:12 -08001714// This test sets up a call between two parties with audio, video and an RTP
1715// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001716TEST_F(P2PTestConductor, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717 FakeConstraints setup_constraints;
1718 setup_constraints.SetAllowRtpDataChannels();
1719 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1720 initializing_client()->CreateDataChannel();
1721 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001722 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1723 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1725 kMaxWaitMs);
1726 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1727 kMaxWaitMs);
1728
1729 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001730
1731 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1733 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001734
1735 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001736 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1737 kMaxWaitMs);
1738
1739 receiving_client()->data_channel()->Close();
1740 // Send new offer and answer.
1741 receiving_client()->Negotiate();
1742 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1743 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1744}
1745
deadbeefb5cb19b2015-11-23 16:39:12 -08001746// This test sets up a call between two parties with audio, video and an SCTP
1747// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001748TEST_F(P2PTestConductor, LocalP2PTestSctpDataChannel) {
deadbeefb5cb19b2015-11-23 16:39:12 -08001749 ASSERT_TRUE(CreateTestClients());
1750 initializing_client()->CreateDataChannel();
1751 LocalP2PTest();
1752 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1753 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
1754 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1755 kMaxWaitMs);
1756 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
1757
1758 std::string data = "hello world";
1759
1760 initializing_client()->data_channel()->Send(DataBuffer(data));
1761 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1762 kMaxWaitMs);
1763
1764 receiving_client()->data_channel()->Send(DataBuffer(data));
1765 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1766 kMaxWaitMs);
1767
1768 receiving_client()->data_channel()->Close();
deadbeef15887932015-12-14 19:32:34 -08001769 EXPECT_TRUE_WAIT(!initializing_client()->data_observer()->IsOpen(),
1770 kMaxWaitMs);
1771 EXPECT_TRUE_WAIT(!receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
deadbeefb5cb19b2015-11-23 16:39:12 -08001772}
1773
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774// This test sets up a call between two parties and creates a data channel.
1775// The test tests that received data is buffered unless an observer has been
1776// registered.
1777// Rtp data channels can receive data before the underlying
1778// transport has detected that a channel is writable and thus data can be
1779// received before the data channel state changes to open. That is hard to test
1780// but the same buffering is used in that case.
deadbeef7c73bdb2015-12-10 15:10:44 -08001781TEST_F(P2PTestConductor, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782 FakeConstraints setup_constraints;
1783 setup_constraints.SetAllowRtpDataChannels();
1784 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1785 initializing_client()->CreateDataChannel();
1786 initializing_client()->Negotiate();
1787
deadbeefaf1b59c2015-10-15 12:08:41 -07001788 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1789 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001790 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1791 kMaxWaitMs);
1792 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1793 receiving_client()->data_channel()->state(), kMaxWaitMs);
1794
1795 // Unregister the existing observer.
1796 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001797
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001799 SendRtpData(initializing_client()->data_channel(), data);
1800
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001801 // Wait a while to allow the sent data to arrive before an observer is
1802 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001803 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001804
1805 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1806 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1807}
1808
1809// This test sets up a call between two parties with audio, video and but only
1810// the initiating client support data.
deadbeef7c73bdb2015-12-10 15:10:44 -08001811TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001812 FakeConstraints setup_constraints_1;
1813 setup_constraints_1.SetAllowRtpDataChannels();
1814 // Must disable DTLS to make negotiation succeed.
1815 setup_constraints_1.SetMandatory(
1816 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1817 FakeConstraints setup_constraints_2;
1818 setup_constraints_2.SetMandatory(
1819 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1820 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001821 initializing_client()->CreateDataChannel();
1822 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001823 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824 EXPECT_FALSE(receiving_client()->data_channel());
1825 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1826}
1827
1828// This test sets up a call between two parties with audio, video. When audio
1829// and video is setup and flowing and data channel is negotiated.
deadbeef7c73bdb2015-12-10 15:10:44 -08001830TEST_F(P2PTestConductor, AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831 FakeConstraints setup_constraints;
1832 setup_constraints.SetAllowRtpDataChannels();
1833 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1834 LocalP2PTest();
1835 initializing_client()->CreateDataChannel();
1836 // Send new offer and answer.
1837 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07001838 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1839 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001840 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1841 kMaxWaitMs);
1842 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1843 kMaxWaitMs);
1844}
1845
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001846// This test sets up a Jsep call with SCTP DataChannel and verifies the
1847// negotiation is completed without error.
1848#ifdef HAVE_SCTP
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01001849// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01001850#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01001851#define MAYBE_CreateOfferWithSctpDataChannel \
1852 DISABLED_CreateOfferWithSctpDataChannel
1853#else
1854#define MAYBE_CreateOfferWithSctpDataChannel CreateOfferWithSctpDataChannel
1855#endif
1856TEST_F(P2PTestConductor, MAYBE_CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001857 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001858 FakeConstraints constraints;
1859 constraints.SetMandatory(
1860 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1861 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1862 initializing_client()->CreateDataChannel();
1863 initializing_client()->Negotiate(false, false);
1864}
1865#endif
1866
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867// This test sets up a call between two parties with audio, and video.
1868// During the call, the initializing side restart ice and the test verifies that
1869// new ice candidates are generated and audio and video still can flow.
deadbeef7c73bdb2015-12-10 15:10:44 -08001870TEST_F(P2PTestConductor, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001871 ASSERT_TRUE(CreateTestClients());
1872
1873 // Negotiate and wait for ice completion and make sure audio and video plays.
1874 LocalP2PTest();
1875
1876 // Create a SDP string of the first audio candidate for both clients.
1877 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1878 initializing_client()->pc()->local_description()->candidates(0);
1879 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1880 receiving_client()->pc()->local_description()->candidates(0);
1881 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1882 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1883 std::string initiator_candidate;
1884 EXPECT_TRUE(
1885 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1886 std::string receiver_candidate;
1887 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1888
1889 // Restart ice on the initializing client.
1890 receiving_client()->SetExpectIceRestart(true);
1891 initializing_client()->IceRestart();
1892
1893 // Negotiate and wait for ice completion again and make sure audio and video
1894 // plays.
1895 LocalP2PTest();
1896
1897 // Create a SDP string of the first audio candidate for both clients again.
1898 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1899 initializing_client()->pc()->local_description()->candidates(0);
1900 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1901 receiving_client()->pc()->local_description()->candidates(0);
1902 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1903 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1904 std::string initiator_candidate_restart;
1905 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1906 &initiator_candidate_restart));
1907 std::string receiver_candidate_restart;
1908 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1909 &receiver_candidate_restart));
1910
1911 // Verify that the first candidates in the local session descriptions has
1912 // changed.
1913 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1914 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1915}
1916
deadbeeffaac4972015-11-12 15:33:07 -08001917// This test sets up a call between two parties with audio, and video.
1918// It then renegotiates setting the video m-line to "port 0", then later
1919// renegotiates again, enabling video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001920TEST_F(P2PTestConductor, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08001921 ASSERT_TRUE(CreateTestClients());
1922
1923 // Do initial negotiation. Will result in video and audio sendonly m-lines.
1924 receiving_client()->set_auto_add_stream(false);
1925 initializing_client()->AddMediaStream(true, true);
1926 initializing_client()->Negotiate();
1927
1928 // Negotiate again, disabling the video m-line (receiving client will
1929 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
1930 receiving_client()->SetReceiveVideo(false);
1931 initializing_client()->Negotiate();
1932
1933 // Enable video and do negotiation again, making sure video is received
1934 // end-to-end.
1935 receiving_client()->SetReceiveVideo(true);
1936 receiving_client()->AddMediaStream(true, true);
1937 LocalP2PTest();
1938}
1939
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001940// This test sets up a Jsep call between two parties with external
1941// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001942// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1943// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001944TEST_F(P2PTestConductor, DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001945 ASSERT_TRUE(CreateTestClients());
1946 EnableVideoDecoderFactory();
1947 LocalP2PTest();
1948}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001949
deadbeeffac06552015-11-25 11:26:01 -08001950// This tests that if we negotiate after calling CreateSender but before we
1951// have a track, then set a track later, frames from the newly-set track are
1952// received end-to-end.
deadbeef7c73bdb2015-12-10 15:10:44 -08001953TEST_F(P2PTestConductor, EarlyWarmupTest) {
deadbeeffac06552015-11-25 11:26:01 -08001954 ASSERT_TRUE(CreateTestClients());
deadbeefbd7d8f72015-12-18 16:58:44 -08001955 auto audio_sender =
1956 initializing_client()->pc()->CreateSender("audio", "stream_id");
1957 auto video_sender =
1958 initializing_client()->pc()->CreateSender("video", "stream_id");
deadbeeffac06552015-11-25 11:26:01 -08001959 initializing_client()->Negotiate();
1960 // Wait for ICE connection to complete, without any tracks.
1961 // Note that the receiving client WILL (in HandleIncomingOffer) create
1962 // tracks, so it's only the initiator here that's doing early warmup.
1963 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1964 VerifySessionDescriptions();
1965 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1966 initializing_client()->ice_connection_state(),
1967 kMaxWaitForFramesMs);
1968 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1969 receiving_client()->ice_connection_state(),
1970 kMaxWaitForFramesMs);
1971 // Now set the tracks, and expect frames to immediately start flowing.
1972 EXPECT_TRUE(
1973 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
1974 EXPECT_TRUE(
1975 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
hta6b4f8392016-03-10 00:24:31 -08001976 EXPECT_TRUE_WAIT(FramesHaveArrived(kEndAudioFrameCount, kEndVideoFrameCount),
deadbeeffac06552015-11-25 11:26:01 -08001977 kMaxWaitForFramesMs);
1978}
1979
deadbeef0a6c4ca2015-10-06 11:38:28 -07001980class IceServerParsingTest : public testing::Test {
1981 public:
1982 // Convenience for parsing a single URL.
1983 bool ParseUrl(const std::string& url) {
1984 return ParseUrl(url, std::string(), std::string());
1985 }
1986
1987 bool ParseUrl(const std::string& url,
1988 const std::string& username,
1989 const std::string& password) {
1990 PeerConnectionInterface::IceServers servers;
1991 PeerConnectionInterface::IceServer server;
1992 server.urls.push_back(url);
1993 server.username = username;
1994 server.password = password;
1995 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001996 return webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_);
deadbeef0a6c4ca2015-10-06 11:38:28 -07001997 }
1998
1999 protected:
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002000 cricket::ServerAddresses stun_servers_;
2001 std::vector<cricket::RelayServerConfig> turn_servers_;
deadbeef0a6c4ca2015-10-06 11:38:28 -07002002};
2003
2004// Make sure all STUN/TURN prefixes are parsed correctly.
2005TEST_F(IceServerParsingTest, ParseStunPrefixes) {
2006 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002007 EXPECT_EQ(1U, stun_servers_.size());
2008 EXPECT_EQ(0U, turn_servers_.size());
2009 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002010
2011 EXPECT_TRUE(ParseUrl("stuns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002012 EXPECT_EQ(1U, stun_servers_.size());
2013 EXPECT_EQ(0U, turn_servers_.size());
2014 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002015
2016 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002017 EXPECT_EQ(0U, stun_servers_.size());
2018 EXPECT_EQ(1U, turn_servers_.size());
2019 EXPECT_FALSE(turn_servers_[0].ports[0].secure);
2020 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002021
2022 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002023 EXPECT_EQ(0U, stun_servers_.size());
2024 EXPECT_EQ(1U, turn_servers_.size());
2025 EXPECT_TRUE(turn_servers_[0].ports[0].secure);
2026 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002027
2028 // invalid prefixes
2029 EXPECT_FALSE(ParseUrl("stunn:hostname"));
2030 EXPECT_FALSE(ParseUrl(":hostname"));
2031 EXPECT_FALSE(ParseUrl(":"));
2032 EXPECT_FALSE(ParseUrl(""));
2033}
2034
2035TEST_F(IceServerParsingTest, VerifyDefaults) {
2036 // TURNS defaults
2037 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002038 EXPECT_EQ(1U, turn_servers_.size());
2039 EXPECT_EQ(5349, turn_servers_[0].ports[0].address.port());
2040 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
2041 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002042
2043 // TURN defaults
2044 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002045 EXPECT_EQ(1U, turn_servers_.size());
2046 EXPECT_EQ(3478, turn_servers_[0].ports[0].address.port());
2047 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2048 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002049
2050 // STUN defaults
2051 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002052 EXPECT_EQ(1U, stun_servers_.size());
2053 EXPECT_EQ(3478, stun_servers_.begin()->port());
2054 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002055}
2056
2057// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
2058// can be parsed correctly.
2059TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
2060 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002061 EXPECT_EQ(1U, stun_servers_.size());
2062 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2063 EXPECT_EQ(1234, stun_servers_.begin()->port());
2064 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002065
2066 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002067 EXPECT_EQ(1U, stun_servers_.size());
2068 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2069 EXPECT_EQ(4321, stun_servers_.begin()->port());
2070 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002071
2072 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002073 EXPECT_EQ(1U, stun_servers_.size());
2074 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2075 EXPECT_EQ(9999, stun_servers_.begin()->port());
2076 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002077
2078 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002079 EXPECT_EQ(1U, stun_servers_.size());
2080 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2081 EXPECT_EQ(3478, stun_servers_.begin()->port());
2082 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002083
2084 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002085 EXPECT_EQ(1U, stun_servers_.size());
2086 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2087 EXPECT_EQ(3478, stun_servers_.begin()->port());
2088 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002089
2090 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002091 EXPECT_EQ(1U, stun_servers_.size());
2092 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2093 EXPECT_EQ(3478, stun_servers_.begin()->port());
2094 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002095
2096 // Try some invalid hostname:port strings.
2097 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
2098 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002099 EXPECT_FALSE(ParseUrl("stun:hostname:port:more"));
2100 EXPECT_FALSE(ParseUrl("stun:hostname:port more"));
deadbeef0a6c4ca2015-10-06 11:38:28 -07002101 EXPECT_FALSE(ParseUrl("stun:hostname:"));
2102 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
2103 EXPECT_FALSE(ParseUrl("stun::5555"));
2104 EXPECT_FALSE(ParseUrl("stun:"));
2105}
2106
2107// Test parsing the "?transport=xxx" part of the URL.
2108TEST_F(IceServerParsingTest, ParseTransport) {
2109 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002110 EXPECT_EQ(1U, turn_servers_.size());
2111 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
2112 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002113
2114 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002115 EXPECT_EQ(1U, turn_servers_.size());
2116 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2117 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002118
2119 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
2120}
2121
2122// Test parsing ICE username contained in URL.
2123TEST_F(IceServerParsingTest, ParseUsername) {
2124 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002125 EXPECT_EQ(1U, turn_servers_.size());
2126 EXPECT_EQ("user", turn_servers_[0].credentials.username);
2127 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002128
2129 EXPECT_FALSE(ParseUrl("turn:@hostname"));
2130 EXPECT_FALSE(ParseUrl("turn:username@"));
2131 EXPECT_FALSE(ParseUrl("turn:@"));
2132 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
2133}
2134
2135// Test that username and password from IceServer is copied into the resulting
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002136// RelayServerConfig.
deadbeef0a6c4ca2015-10-06 11:38:28 -07002137TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
2138 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002139 EXPECT_EQ(1U, turn_servers_.size());
2140 EXPECT_EQ("username", turn_servers_[0].credentials.username);
2141 EXPECT_EQ("password", turn_servers_[0].credentials.password);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002142}
2143
2144// Ensure that if a server has multiple URLs, each one is parsed.
2145TEST_F(IceServerParsingTest, ParseMultipleUrls) {
2146 PeerConnectionInterface::IceServers servers;
2147 PeerConnectionInterface::IceServer server;
2148 server.urls.push_back("stun:hostname");
2149 server.urls.push_back("turn:hostname");
2150 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002151 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2152 EXPECT_EQ(1U, stun_servers_.size());
2153 EXPECT_EQ(1U, turn_servers_.size());
deadbeef0a6c4ca2015-10-06 11:38:28 -07002154}
2155
Taylor Brandstetter893505d2016-01-07 15:12:48 -08002156// Ensure that TURN servers are given unique priorities,
2157// so that their resulting candidates have unique priorities.
2158TEST_F(IceServerParsingTest, TurnServerPrioritiesUnique) {
2159 PeerConnectionInterface::IceServers servers;
2160 PeerConnectionInterface::IceServer server;
2161 server.urls.push_back("turn:hostname");
2162 server.urls.push_back("turn:hostname2");
2163 servers.push_back(server);
2164 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2165 EXPECT_EQ(2U, turn_servers_.size());
2166 EXPECT_NE(turn_servers_[0].priority, turn_servers_[1].priority);
2167}
2168
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00002169#endif // if !defined(THREAD_SANITIZER)
hta6b4f8392016-03-10 00:24:31 -08002170
2171} // namespace