blob: ae3706942804073e3fab62668c89cdc40d8b90a8 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
11#include <stdio.h>
12
13#include <algorithm>
14#include <list>
15#include <map>
kwiberg0eb15ed2015-12-17 03:04:15 -080016#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include <vector>
18
Henrik Kjellander15583c12016-02-10 10:53:12 +010019#include "webrtc/api/dtmfsender.h"
20#include "webrtc/api/fakemetricsobserver.h"
21#include "webrtc/api/localaudiosource.h"
22#include "webrtc/api/mediastreaminterface.h"
23#include "webrtc/api/peerconnection.h"
24#include "webrtc/api/peerconnectionfactory.h"
25#include "webrtc/api/peerconnectioninterface.h"
26#include "webrtc/api/test/fakeaudiocapturemodule.h"
27#include "webrtc/api/test/fakeconstraints.h"
28#include "webrtc/api/test/fakedtlsidentitystore.h"
29#include "webrtc/api/test/fakeperiodicvideocapturer.h"
30#include "webrtc/api/test/fakevideotrackrenderer.h"
31#include "webrtc/api/test/mockpeerconnectionobservers.h"
deadbeef246b5272016-03-17 15:03:39 -070032#include "webrtc/api/videosourceinterface.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000033#include "webrtc/base/gunit.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000034#include "webrtc/base/physicalsocketserver.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000035#include "webrtc/base/scoped_ptr.h"
36#include "webrtc/base/ssladapter.h"
37#include "webrtc/base/sslstreamadapter.h"
38#include "webrtc/base/thread.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000039#include "webrtc/base/virtualsocketserver.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010040#include "webrtc/media/engine/fakewebrtcvideoengine.h"
kjellanderf4752772016-03-02 05:42:30 -080041#include "webrtc/p2p/base/p2pconstants.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000042#include "webrtc/p2p/base/sessiondescription.h"
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080043#include "webrtc/p2p/client/fakeportallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010044#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045
46#define MAYBE_SKIP_TEST(feature) \
47 if (!(feature())) { \
48 LOG(LS_INFO) << "Feature disabled... skipping"; \
49 return; \
50 }
51
52using cricket::ContentInfo;
53using cricket::FakeWebRtcVideoDecoder;
54using cricket::FakeWebRtcVideoDecoderFactory;
55using cricket::FakeWebRtcVideoEncoder;
56using cricket::FakeWebRtcVideoEncoderFactory;
57using cricket::MediaContentDescription;
58using webrtc::DataBuffer;
59using webrtc::DataChannelInterface;
60using webrtc::DtmfSender;
61using webrtc::DtmfSenderInterface;
62using webrtc::DtmfSenderObserverInterface;
63using webrtc::FakeConstraints;
64using webrtc::MediaConstraintsInterface;
deadbeeffaac4972015-11-12 15:33:07 -080065using webrtc::MediaStreamInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066using webrtc::MediaStreamTrackInterface;
67using webrtc::MockCreateSessionDescriptionObserver;
68using webrtc::MockDataChannelObserver;
69using webrtc::MockSetSessionDescriptionObserver;
70using webrtc::MockStatsObserver;
deadbeeffaac4972015-11-12 15:33:07 -080071using webrtc::ObserverInterface;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000072using webrtc::PeerConnectionInterface;
Joachim Bauch04e5b492015-05-29 09:40:39 +020073using webrtc::PeerConnectionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074using webrtc::SessionDescriptionInterface;
75using webrtc::StreamCollectionInterface;
76
hta6b4f8392016-03-10 00:24:31 -080077namespace {
78
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000079static const int kMaxWaitMs = 10000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000080// Disable for TSan v2, see
81// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
82// This declaration is also #ifdef'd as it causes uninitialized-variable
83// warnings.
84#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085static const int kMaxWaitForStatsMs = 3000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000086#endif
deadbeeffac06552015-11-25 11:26:01 -080087static const int kMaxWaitForActivationMs = 5000;
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000088static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089static const int kEndAudioFrameCount = 3;
90static const int kEndVideoFrameCount = 3;
91
92static const char kStreamLabelBase[] = "stream_label";
93static const char kVideoTrackLabelBase[] = "video_track";
94static const char kAudioTrackLabelBase[] = "audio_track";
95static const char kDataChannelLabel[] = "data_channel";
96
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +000097// Disable for TSan v2, see
98// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
99// This declaration is also #ifdef'd as it causes unused-variable errors.
100#if !defined(THREAD_SANITIZER)
101// SRTP cipher name negotiated by the tests. This must be updated if the
102// default changes.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800103static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000104#endif
105
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106static void RemoveLinesFromSdp(const std::string& line_start,
107 std::string* sdp) {
108 const char kSdpLineEnd[] = "\r\n";
109 size_t ssrc_pos = 0;
110 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
111 std::string::npos) {
112 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
113 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
114 }
115}
116
hta6b4f8392016-03-10 00:24:31 -0800117bool StreamsHaveAudioTrack(StreamCollectionInterface* streams) {
118 for (size_t idx = 0; idx < streams->count(); idx++) {
119 auto stream = streams->at(idx);
120 if (stream->GetAudioTracks().size() > 0) {
121 return true;
122 }
123 }
124 return false;
125}
126
127bool StreamsHaveVideoTrack(StreamCollectionInterface* streams) {
128 for (size_t idx = 0; idx < streams->count(); idx++) {
129 auto stream = streams->at(idx);
130 if (stream->GetVideoTracks().size() > 0) {
131 return true;
132 }
133 }
134 return false;
135}
136
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137class SignalingMessageReceiver {
138 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 virtual void ReceiveSdpMessage(const std::string& type,
140 std::string& msg) = 0;
141 virtual void ReceiveIceMessage(const std::string& sdp_mid,
142 int sdp_mline_index,
143 const std::string& msg) = 0;
144
145 protected:
deadbeefaf1b59c2015-10-15 12:08:41 -0700146 SignalingMessageReceiver() {}
147 virtual ~SignalingMessageReceiver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148};
149
deadbeefaf1b59c2015-10-15 12:08:41 -0700150class PeerConnectionTestClient : public webrtc::PeerConnectionObserver,
deadbeeffaac4972015-11-12 15:33:07 -0800151 public SignalingMessageReceiver,
152 public ObserverInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 public:
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800154 static PeerConnectionTestClient* CreateClientWithDtlsIdentityStore(
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800155 const std::string& id,
156 const MediaConstraintsInterface* constraints,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800157 const PeerConnectionFactory::Options* options,
htaaac2dea2016-03-10 13:35:55 -0800158 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store,
159 bool prefer_constraint_apis) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800160 PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
htaaac2dea2016-03-10 13:35:55 -0800161 if (!client->Init(constraints, options, std::move(dtls_identity_store),
162 prefer_constraint_apis)) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800163 delete client;
164 return nullptr;
165 }
166 return client;
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800167 }
168
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800169 static PeerConnectionTestClient* CreateClient(
170 const std::string& id,
171 const MediaConstraintsInterface* constraints,
172 const PeerConnectionFactory::Options* options) {
173 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
174 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
175 : nullptr);
176
htaaac2dea2016-03-10 13:35:55 -0800177 return CreateClientWithDtlsIdentityStore(
178 id, constraints, options, std::move(dtls_identity_store), true);
179 }
180
181 static PeerConnectionTestClient* CreateClientPreferNoConstraints(
182 const std::string& id,
183 const PeerConnectionFactory::Options* options) {
184 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
185 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
186 : nullptr);
187
188 return CreateClientWithDtlsIdentityStore(
189 id, nullptr, options, std::move(dtls_identity_store), false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800190 }
191
deadbeefaf1b59c2015-10-15 12:08:41 -0700192 ~PeerConnectionTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 }
194
deadbeefaf1b59c2015-10-15 12:08:41 -0700195 void Negotiate() { Negotiate(true, true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196
deadbeefaf1b59c2015-10-15 12:08:41 -0700197 void Negotiate(bool audio, bool video) {
198 rtc::scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700199 ASSERT_TRUE(DoCreateOffer(&offer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200
deadbeefaf1b59c2015-10-15 12:08:41 -0700201 if (offer->description()->GetContentByName("audio")) {
202 offer->description()->GetContentByName("audio")->rejected = !audio;
203 }
204 if (offer->description()->GetContentByName("video")) {
205 offer->description()->GetContentByName("video")->rejected = !video;
206 }
207
208 std::string sdp;
209 EXPECT_TRUE(offer->ToString(&sdp));
210 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
211 signaling_message_receiver_->ReceiveSdpMessage(
212 webrtc::SessionDescriptionInterface::kOffer, sdp);
213 }
214
215 // SignalingMessageReceiver callback.
216 void ReceiveSdpMessage(const std::string& type, std::string& msg) override {
217 FilterIncomingSdpMessage(&msg);
218 if (type == webrtc::SessionDescriptionInterface::kOffer) {
219 HandleIncomingOffer(msg);
220 } else {
221 HandleIncomingAnswer(msg);
222 }
223 }
224
225 // SignalingMessageReceiver callback.
226 void ReceiveIceMessage(const std::string& sdp_mid,
227 int sdp_mline_index,
228 const std::string& msg) override {
229 LOG(INFO) << id_ << "ReceiveIceMessage";
230 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
231 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
232 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
233 }
234
235 // PeerConnectionObserver callbacks.
236 void OnSignalingChange(
237 webrtc::PeerConnectionInterface::SignalingState new_state) override {
238 EXPECT_EQ(pc()->signaling_state(), new_state);
239 }
deadbeeffaac4972015-11-12 15:33:07 -0800240 void OnAddStream(MediaStreamInterface* media_stream) override {
241 media_stream->RegisterObserver(this);
deadbeefaf1b59c2015-10-15 12:08:41 -0700242 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
243 const std::string id = media_stream->GetVideoTracks()[i]->id();
244 ASSERT_TRUE(fake_video_renderers_.find(id) ==
245 fake_video_renderers_.end());
deadbeefc9be0072015-12-14 18:27:57 -0800246 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
247 media_stream->GetVideoTracks()[i]));
deadbeefaf1b59c2015-10-15 12:08:41 -0700248 }
249 }
deadbeeffaac4972015-11-12 15:33:07 -0800250 void OnRemoveStream(MediaStreamInterface* media_stream) override {}
deadbeefaf1b59c2015-10-15 12:08:41 -0700251 void OnRenegotiationNeeded() override {}
252 void OnIceConnectionChange(
253 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
254 EXPECT_EQ(pc()->ice_connection_state(), new_state);
255 }
256 void OnIceGatheringChange(
257 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
258 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
259 }
260 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
261 LOG(INFO) << id_ << "OnIceCandidate";
262
263 std::string ice_sdp;
264 EXPECT_TRUE(candidate->ToString(&ice_sdp));
265 if (signaling_message_receiver_ == nullptr) {
266 // Remote party may be deleted.
267 return;
268 }
269 signaling_message_receiver_->ReceiveIceMessage(
270 candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
271 }
272
deadbeeffaac4972015-11-12 15:33:07 -0800273 // MediaStreamInterface callback
274 void OnChanged() override {
275 // Track added or removed from MediaStream, so update our renderers.
276 rtc::scoped_refptr<StreamCollectionInterface> remote_streams =
277 pc()->remote_streams();
278 // Remove renderers for tracks that were removed.
279 for (auto it = fake_video_renderers_.begin();
280 it != fake_video_renderers_.end();) {
281 if (remote_streams->FindVideoTrack(it->first) == nullptr) {
deadbeefc9be0072015-12-14 18:27:57 -0800282 auto to_remove = it++;
283 removed_fake_video_renderers_.push_back(std::move(to_remove->second));
284 fake_video_renderers_.erase(to_remove);
deadbeeffaac4972015-11-12 15:33:07 -0800285 } else {
286 ++it;
287 }
288 }
289 // Create renderers for new video tracks.
290 for (size_t stream_index = 0; stream_index < remote_streams->count();
291 ++stream_index) {
292 MediaStreamInterface* remote_stream = remote_streams->at(stream_index);
293 for (size_t track_index = 0;
294 track_index < remote_stream->GetVideoTracks().size();
295 ++track_index) {
296 const std::string id =
297 remote_stream->GetVideoTracks()[track_index]->id();
298 if (fake_video_renderers_.find(id) != fake_video_renderers_.end()) {
299 continue;
300 }
deadbeefc9be0072015-12-14 18:27:57 -0800301 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
302 remote_stream->GetVideoTracks()[track_index]));
deadbeeffaac4972015-11-12 15:33:07 -0800303 }
304 }
305 }
306
deadbeefaf1b59c2015-10-15 12:08:41 -0700307 void SetVideoConstraints(const webrtc::FakeConstraints& video_constraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308 video_constraints_ = video_constraint;
309 }
310
311 void AddMediaStream(bool audio, bool video) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700312 std::string stream_label =
313 kStreamLabelBase +
314 rtc::ToString<int>(static_cast<int>(pc()->local_streams()->count()));
deadbeeffaac4972015-11-12 15:33:07 -0800315 rtc::scoped_refptr<MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000316 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317
318 if (audio && can_receive_audio()) {
deadbeeffac06552015-11-25 11:26:01 -0800319 stream->AddTrack(CreateLocalAudioTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 }
321 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000322 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323 }
324
deadbeefaf1b59c2015-10-15 12:08:41 -0700325 EXPECT_TRUE(pc()->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326 }
327
deadbeefaf1b59c2015-10-15 12:08:41 -0700328 size_t NumberOfLocalMediaStreams() { return pc()->local_streams()->count(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329
330 bool SessionActive() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700331 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332 }
333
deadbeeffaac4972015-11-12 15:33:07 -0800334 // Automatically add a stream when receiving an offer, if we don't have one.
335 // Defaults to true.
336 void set_auto_add_stream(bool auto_add_stream) {
337 auto_add_stream_ = auto_add_stream;
338 }
339
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340 void set_signaling_message_receiver(
deadbeefaf1b59c2015-10-15 12:08:41 -0700341 SignalingMessageReceiver* signaling_message_receiver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 signaling_message_receiver_ = signaling_message_receiver;
343 }
344
345 void EnableVideoDecoderFactory() {
346 video_decoder_factory_enabled_ = true;
347 fake_video_decoder_factory_->AddSupportedVideoCodecType(
348 webrtc::kVideoCodecVP8);
349 }
350
deadbeefaf1b59c2015-10-15 12:08:41 -0700351 void IceRestart() {
htaaac2dea2016-03-10 13:35:55 -0800352 offer_answer_constraints_.SetMandatoryIceRestart(true);
353 offer_answer_options_.ice_restart = true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700354 SetExpectIceRestart(true);
355 }
356
357 void SetExpectIceRestart(bool expect_restart) {
358 expect_ice_restart_ = expect_restart;
359 }
360
361 bool ExpectIceRestart() const { return expect_ice_restart_; }
362
363 void SetReceiveAudioVideo(bool audio, bool video) {
364 SetReceiveAudio(audio);
365 SetReceiveVideo(video);
366 ASSERT_EQ(audio, can_receive_audio());
367 ASSERT_EQ(video, can_receive_video());
368 }
369
370 void SetReceiveAudio(bool audio) {
371 if (audio && can_receive_audio())
372 return;
htaaac2dea2016-03-10 13:35:55 -0800373 offer_answer_constraints_.SetMandatoryReceiveAudio(audio);
374 offer_answer_options_.offer_to_receive_audio = audio ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700375 }
376
377 void SetReceiveVideo(bool video) {
378 if (video && can_receive_video())
379 return;
htaaac2dea2016-03-10 13:35:55 -0800380 offer_answer_constraints_.SetMandatoryReceiveVideo(video);
381 offer_answer_options_.offer_to_receive_video = video ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700382 }
383
384 void RemoveMsidFromReceivedSdp(bool remove) { remove_msid_ = remove; }
385
386 void RemoveSdesCryptoFromReceivedSdp(bool remove) { remove_sdes_ = remove; }
387
388 void RemoveBundleFromReceivedSdp(bool remove) { remove_bundle_ = remove; }
389
perkjcaafdba2016-03-20 07:34:29 -0700390 void RemoveCvoFromReceivedSdp(bool remove) { remove_cvo_ = remove; }
391
deadbeefaf1b59c2015-10-15 12:08:41 -0700392 bool can_receive_audio() {
393 bool value;
htaaac2dea2016-03-10 13:35:55 -0800394 if (prefer_constraint_apis_) {
395 if (webrtc::FindConstraint(
396 &offer_answer_constraints_,
397 MediaConstraintsInterface::kOfferToReceiveAudio, &value,
398 nullptr)) {
399 return value;
400 }
401 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700402 }
htaaac2dea2016-03-10 13:35:55 -0800403 return offer_answer_options_.offer_to_receive_audio > 0 ||
404 offer_answer_options_.offer_to_receive_audio ==
405 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700406 }
407
408 bool can_receive_video() {
409 bool value;
htaaac2dea2016-03-10 13:35:55 -0800410 if (prefer_constraint_apis_) {
411 if (webrtc::FindConstraint(
412 &offer_answer_constraints_,
413 MediaConstraintsInterface::kOfferToReceiveVideo, &value,
414 nullptr)) {
415 return value;
416 }
417 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700418 }
htaaac2dea2016-03-10 13:35:55 -0800419 return offer_answer_options_.offer_to_receive_video > 0 ||
420 offer_answer_options_.offer_to_receive_video ==
421 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700422 }
423
deadbeefaf1b59c2015-10-15 12:08:41 -0700424 void OnDataChannel(DataChannelInterface* data_channel) override {
425 LOG(INFO) << id_ << "OnDataChannel";
426 data_channel_ = data_channel;
427 data_observer_.reset(new MockDataChannelObserver(data_channel));
428 }
429
430 void CreateDataChannel() {
431 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, nullptr);
432 ASSERT_TRUE(data_channel_.get() != nullptr);
433 data_observer_.reset(new MockDataChannelObserver(data_channel_));
434 }
435
deadbeeffac06552015-11-25 11:26:01 -0800436 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack(
437 const std::string& stream_label) {
438 FakeConstraints constraints;
439 // Disable highpass filter so that we can get all the test audio frames.
440 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
441 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
442 peer_connection_factory_->CreateAudioSource(&constraints);
443 // TODO(perkj): Test audio source when it is implemented. Currently audio
444 // always use the default input.
445 std::string label = stream_label + kAudioTrackLabelBase;
446 return peer_connection_factory_->CreateAudioTrack(label, source);
447 }
448
449 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack(
450 const std::string& stream_label) {
451 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
452 FakeConstraints source_constraints = video_constraints_;
453 source_constraints.SetMandatoryMaxFrameRate(10);
454
455 cricket::FakeVideoCapturer* fake_capturer =
456 new webrtc::FakePeriodicVideoCapturer();
perkjcaafdba2016-03-20 07:34:29 -0700457 fake_capturer->SetRotation(capture_rotation_);
deadbeeffac06552015-11-25 11:26:01 -0800458 video_capturers_.push_back(fake_capturer);
perkja3ede6c2016-03-08 01:27:48 +0100459 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
deadbeeffac06552015-11-25 11:26:01 -0800460 peer_connection_factory_->CreateVideoSource(fake_capturer,
461 &source_constraints);
462 std::string label = stream_label + kVideoTrackLabelBase;
perkjcaafdba2016-03-20 07:34:29 -0700463
464 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
465 peer_connection_factory_->CreateVideoTrack(label, source));
466 if (!local_video_renderer_) {
467 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
468 }
469 return track;
deadbeeffac06552015-11-25 11:26:01 -0800470 }
471
deadbeefaf1b59c2015-10-15 12:08:41 -0700472 DataChannelInterface* data_channel() { return data_channel_; }
473 const MockDataChannelObserver* data_observer() const {
474 return data_observer_.get();
475 }
476
hta6b4f8392016-03-10 00:24:31 -0800477 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
deadbeefaf1b59c2015-10-15 12:08:41 -0700478
479 void StopVideoCapturers() {
perkjcaafdba2016-03-20 07:34:29 -0700480 for (auto* capturer : video_capturers_) {
481 capturer->Stop();
deadbeefaf1b59c2015-10-15 12:08:41 -0700482 }
483 }
484
perkjcaafdba2016-03-20 07:34:29 -0700485 void SetCaptureRotation(webrtc::VideoRotation rotation) {
486 ASSERT_TRUE(video_capturers_.empty());
487 capture_rotation_ = rotation;
488 }
489
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 bool AudioFramesReceivedCheck(int number_of_frames) const {
491 return number_of_frames <= fake_audio_capture_module_->frames_received();
492 }
493
deadbeefc9be0072015-12-14 18:27:57 -0800494 int audio_frames_received() const {
495 return fake_audio_capture_module_->frames_received();
496 }
497
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 bool VideoFramesReceivedCheck(int number_of_frames) {
499 if (video_decoder_factory_enabled_) {
500 const std::vector<FakeWebRtcVideoDecoder*>& decoders
501 = fake_video_decoder_factory_->decoders();
502 if (decoders.empty()) {
503 return number_of_frames <= 0;
504 }
hta6b4f8392016-03-10 00:24:31 -0800505 // Note - this checks that EACH decoder has the requisite number
506 // of frames. The video_frames_received() function sums them.
deadbeefc9be0072015-12-14 18:27:57 -0800507 for (FakeWebRtcVideoDecoder* decoder : decoders) {
508 if (number_of_frames > decoder->GetNumFramesReceived()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509 return false;
510 }
511 }
512 return true;
513 } else {
514 if (fake_video_renderers_.empty()) {
515 return number_of_frames <= 0;
516 }
517
deadbeefc9be0072015-12-14 18:27:57 -0800518 for (const auto& pair : fake_video_renderers_) {
519 if (number_of_frames > pair.second->num_rendered_frames()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 return false;
521 }
522 }
523 return true;
524 }
525 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700526
deadbeefc9be0072015-12-14 18:27:57 -0800527 int video_frames_received() const {
528 int total = 0;
529 if (video_decoder_factory_enabled_) {
530 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
531 fake_video_decoder_factory_->decoders();
532 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
533 total += decoder->GetNumFramesReceived();
534 }
535 } else {
536 for (const auto& pair : fake_video_renderers_) {
537 total += pair.second->num_rendered_frames();
538 }
539 for (const auto& renderer : removed_fake_video_renderers_) {
540 total += renderer->num_rendered_frames();
541 }
542 }
543 return total;
544 }
545
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 // Verify the CreateDtmfSender interface
547 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000548 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
549 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550
551 // We can't create a DTMF sender with an invalid audio track or a non local
552 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700553 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000554 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700555 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
556 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557
558 // We should be able to create a DTMF sender from a local track.
559 webrtc::AudioTrackInterface* localtrack =
560 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
561 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700562 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000563 dtmf_sender->RegisterObserver(observer.get());
564
565 // Test the DtmfSender object just created.
566 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
567 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
568
569 // We don't need to verify that the DTMF tones are actually sent out because
570 // that is already covered by the tests of the lower level components.
571
572 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
573 std::vector<std::string> tones;
574 tones.push_back("1");
575 tones.push_back("a");
576 tones.push_back("");
577 observer->Verify(tones);
578
579 dtmf_sender->UnregisterObserver();
580 }
581
582 // Verifies that the SessionDescription have rejected the appropriate media
583 // content.
584 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700585 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
586 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 const cricket::SessionDescription* remote_desc =
588 peer_connection_->remote_description()->description();
589 const cricket::SessionDescription* local_desc =
590 peer_connection_->local_description()->description();
591
592 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
593 if (remote_audio_content) {
594 const ContentInfo* audio_content =
595 GetFirstAudioContent(local_desc);
596 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
597 }
598
599 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
600 if (remote_video_content) {
601 const ContentInfo* video_content =
602 GetFirstVideoContent(local_desc);
603 EXPECT_EQ(can_receive_video(), !video_content->rejected);
604 }
605 }
606
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700608 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 const cricket::SessionDescription* desc =
610 peer_connection_->local_description()->description();
611 const cricket::ContentInfos& contents = desc->contents();
612
613 for (size_t index = 0; index < contents.size(); ++index) {
614 if (contents[index].rejected)
615 continue;
616 const cricket::TransportDescription* transport_desc =
617 desc->GetTransportDescriptionByName(contents[index].name);
618
619 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000620 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621 if (ufragpair_it == ice_ufrag_pwd_.end()) {
622 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000623 ice_ufrag_pwd_[static_cast<int>(index)] =
624 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000625 } else if (ExpectIceRestart()) {
626 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
627 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
628 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
629 } else {
630 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
631 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
632 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
633 }
634 }
635 }
636
637 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000638 rtc::scoped_refptr<MockStatsObserver>
639 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000640 EXPECT_TRUE(peer_connection_->GetStats(
641 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700643 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000644 return observer->AudioOutputLevel();
645 }
646
647 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000648 rtc::scoped_refptr<MockStatsObserver>
649 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000650 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700651 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700653 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654 return observer->AudioInputLevel();
655 }
656
657 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000658 rtc::scoped_refptr<MockStatsObserver>
659 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000660 EXPECT_TRUE(peer_connection_->GetStats(
661 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700663 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 return observer->BytesReceived();
665 }
666
667 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000668 rtc::scoped_refptr<MockStatsObserver>
669 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000670 EXPECT_TRUE(peer_connection_->GetStats(
671 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000672 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700673 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 return observer->BytesSent();
675 }
676
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000677 int GetAvailableReceivedBandwidthStats() {
678 rtc::scoped_refptr<MockStatsObserver>
679 observer(new rtc::RefCountedObject<MockStatsObserver>());
680 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700681 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000682 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700683 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000684 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000685 return bw;
686 }
687
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000688 std::string GetDtlsCipherStats() {
689 rtc::scoped_refptr<MockStatsObserver>
690 observer(new rtc::RefCountedObject<MockStatsObserver>());
691 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700692 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000693 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700694 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000695 return observer->DtlsCipher();
696 }
697
698 std::string GetSrtpCipherStats() {
699 rtc::scoped_refptr<MockStatsObserver>
700 observer(new rtc::RefCountedObject<MockStatsObserver>());
701 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700702 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000703 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700704 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000705 return observer->SrtpCipher();
706 }
707
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 int rendered_width() {
709 EXPECT_FALSE(fake_video_renderers_.empty());
710 return fake_video_renderers_.empty() ? 1 :
711 fake_video_renderers_.begin()->second->width();
712 }
713
714 int rendered_height() {
715 EXPECT_FALSE(fake_video_renderers_.empty());
716 return fake_video_renderers_.empty() ? 1 :
717 fake_video_renderers_.begin()->second->height();
718 }
719
perkjcaafdba2016-03-20 07:34:29 -0700720 webrtc::VideoRotation rendered_rotation() {
721 EXPECT_FALSE(fake_video_renderers_.empty());
722 return fake_video_renderers_.empty()
723 ? webrtc::kVideoRotation_0
724 : fake_video_renderers_.begin()->second->rotation();
725 }
726
727 int local_rendered_width() {
728 return local_video_renderer_ ? local_video_renderer_->width() : 1;
729 }
730
731 int local_rendered_height() {
732 return local_video_renderer_ ? local_video_renderer_->height() : 1;
733 }
734
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 size_t number_of_remote_streams() {
736 if (!pc())
737 return 0;
738 return pc()->remote_streams()->count();
739 }
740
hta6b4f8392016-03-10 00:24:31 -0800741 StreamCollectionInterface* remote_streams() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742 if (!pc()) {
743 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700744 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 }
746 return pc()->remote_streams();
747 }
748
749 StreamCollectionInterface* local_streams() {
750 if (!pc()) {
751 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700752 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 }
754 return pc()->local_streams();
755 }
756
hta6b4f8392016-03-10 00:24:31 -0800757 bool HasLocalAudioTrack() { return StreamsHaveAudioTrack(local_streams()); }
758
759 bool HasLocalVideoTrack() { return StreamsHaveVideoTrack(local_streams()); }
760
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
762 return pc()->signaling_state();
763 }
764
765 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
766 return pc()->ice_connection_state();
767 }
768
769 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
770 return pc()->ice_gathering_state();
771 }
772
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 private:
774 class DummyDtmfObserver : public DtmfSenderObserverInterface {
775 public:
776 DummyDtmfObserver() : completed_(false) {}
777
778 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700779 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 tones_.push_back(tone);
781 if (tone.empty()) {
782 completed_ = true;
783 }
784 }
785
786 void Verify(const std::vector<std::string>& tones) const {
787 ASSERT_TRUE(tones_.size() == tones.size());
788 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
789 }
790
791 bool completed() const { return completed_; }
792
793 private:
794 bool completed_;
795 std::vector<std::string> tones_;
796 };
797
deadbeefaf1b59c2015-10-15 12:08:41 -0700798 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
799
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800800 bool Init(
801 const MediaConstraintsInterface* constraints,
802 const PeerConnectionFactory::Options* options,
htaaac2dea2016-03-10 13:35:55 -0800803 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store,
804 bool prefer_constraint_apis) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700805 EXPECT_TRUE(!peer_connection_);
806 EXPECT_TRUE(!peer_connection_factory_);
htaaac2dea2016-03-10 13:35:55 -0800807 if (!prefer_constraint_apis) {
808 EXPECT_TRUE(!constraints);
809 }
810 prefer_constraint_apis_ = prefer_constraint_apis;
811
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800812 rtc::scoped_ptr<cricket::PortAllocator> port_allocator(
813 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
deadbeefaf1b59c2015-10-15 12:08:41 -0700814 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
815
816 if (fake_audio_capture_module_ == nullptr) {
817 return false;
818 }
819 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
820 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
821 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
822 rtc::Thread::Current(), rtc::Thread::Current(),
823 fake_audio_capture_module_, fake_video_encoder_factory_,
824 fake_video_decoder_factory_);
825 if (!peer_connection_factory_) {
826 return false;
827 }
828 if (options) {
829 peer_connection_factory_->SetOptions(*options);
830 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800831 peer_connection_ = CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800832 std::move(port_allocator), constraints, std::move(dtls_identity_store));
deadbeefaf1b59c2015-10-15 12:08:41 -0700833 return peer_connection_.get() != nullptr;
834 }
835
deadbeefaf1b59c2015-10-15 12:08:41 -0700836 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800837 rtc::scoped_ptr<cricket::PortAllocator> port_allocator,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800838 const MediaConstraintsInterface* constraints,
839 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800840 // CreatePeerConnection with RTCConfiguration.
841 webrtc::PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 webrtc::PeerConnectionInterface::IceServer ice_server;
843 ice_server.uri = "stun:stun.l.google.com:19302";
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800844 config.servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000845
deadbeefaf1b59c2015-10-15 12:08:41 -0700846 return peer_connection_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800847 config, constraints, std::move(port_allocator),
848 std::move(dtls_identity_store), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 }
850
851 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700852 LOG(INFO) << id_ << "HandleIncomingOffer ";
deadbeeffaac4972015-11-12 15:33:07 -0800853 if (NumberOfLocalMediaStreams() == 0 && auto_add_stream_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 // If we are not sending any streams ourselves it is time to add some.
855 AddMediaStream(true, true);
856 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000857 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700858 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000860 rtc::scoped_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700861 EXPECT_TRUE(DoCreateAnswer(&answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 std::string sdp;
863 EXPECT_TRUE(answer->ToString(&sdp));
864 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
deadbeefaf1b59c2015-10-15 12:08:41 -0700865 if (signaling_message_receiver_) {
866 signaling_message_receiver_->ReceiveSdpMessage(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 webrtc::SessionDescriptionInterface::kAnswer, sdp);
868 }
869 }
870
871 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700872 LOG(INFO) << id_ << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000873 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700874 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
876 }
877
kwiberg2bbff992016-03-16 11:03:04 -0700878 bool DoCreateOfferAnswer(rtc::scoped_ptr<SessionDescriptionInterface>* desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000880 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
881 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 MockCreateSessionDescriptionObserver>());
htaaac2dea2016-03-10 13:35:55 -0800883 if (prefer_constraint_apis_) {
884 if (offer) {
885 pc()->CreateOffer(observer, &offer_answer_constraints_);
886 } else {
887 pc()->CreateAnswer(observer, &offer_answer_constraints_);
888 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 } else {
htaaac2dea2016-03-10 13:35:55 -0800890 if (offer) {
891 pc()->CreateOffer(observer, offer_answer_options_);
892 } else {
893 pc()->CreateAnswer(observer, offer_answer_options_);
894 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 }
896 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
kwiberg2bbff992016-03-16 11:03:04 -0700897 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000898 if (observer->result() && ExpectIceRestart()) {
899 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
900 }
901 return observer->result();
902 }
903
kwiberg2bbff992016-03-16 11:03:04 -0700904 bool DoCreateOffer(rtc::scoped_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 return DoCreateOfferAnswer(desc, true);
906 }
907
kwiberg2bbff992016-03-16 11:03:04 -0700908 bool DoCreateAnswer(rtc::scoped_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 return DoCreateOfferAnswer(desc, false);
910 }
911
912 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000913 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
914 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700916 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 pc()->SetLocalDescription(observer, desc);
918 // Ignore the observer result. If we wait for the result with
919 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
920 // before the offer which is an error.
921 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000922 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 // ProcessMessages waits at least 1ms but processes all messages before
924 // returning. Since this test is synchronous and send messages to the remote
925 // peer whenever a callback is invoked, this can lead to messages being
926 // sent to the remote peer in the wrong order.
927 // TODO(perkj): Find a way to check the result without risking that the
928 // order of sent messages are changed. Ex- by posting all messages that are
929 // sent to the remote peer.
930 return true;
931 }
932
933 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000934 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
935 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700937 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938 pc()->SetRemoteDescription(observer, desc);
939 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
940 return observer->result();
941 }
942
943 // This modifies all received SDP messages before they are processed.
944 void FilterIncomingSdpMessage(std::string* sdp) {
945 if (remove_msid_) {
946 const char kSdpSsrcAttribute[] = "a=ssrc:";
947 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
948 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
949 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
950 }
951 if (remove_bundle_) {
952 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
953 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
954 }
955 if (remove_sdes_) {
956 const char kSdpSdesCryptoAttribute[] = "a=crypto";
957 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
958 }
perkjcaafdba2016-03-20 07:34:29 -0700959 if (remove_cvo_) {
960 const char kSdpCvoExtenstion[] = "urn:3gpp:video-orientation";
961 RemoveLinesFromSdp(kSdpCvoExtenstion, sdp);
962 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 }
964
deadbeefaf1b59c2015-10-15 12:08:41 -0700965 std::string id_;
966
deadbeefaf1b59c2015-10-15 12:08:41 -0700967 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
968 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
969 peer_connection_factory_;
970
htaaac2dea2016-03-10 13:35:55 -0800971 bool prefer_constraint_apis_ = true;
deadbeeffaac4972015-11-12 15:33:07 -0800972 bool auto_add_stream_ = true;
973
deadbeefaf1b59c2015-10-15 12:08:41 -0700974 typedef std::pair<std::string, std::string> IceUfragPwdPair;
975 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
976 bool expect_ice_restart_ = false;
977
deadbeefc9be0072015-12-14 18:27:57 -0800978 // Needed to keep track of number of frames sent.
deadbeefaf1b59c2015-10-15 12:08:41 -0700979 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
980 // Needed to keep track of number of frames received.
deadbeefc9be0072015-12-14 18:27:57 -0800981 std::map<std::string, rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
982 fake_video_renderers_;
983 // Needed to ensure frames aren't received for removed tracks.
984 std::vector<rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
985 removed_fake_video_renderers_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700986 // Needed to keep track of number of frames received when external decoder
987 // used.
988 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
989 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
990 bool video_decoder_factory_enabled_ = false;
991 webrtc::FakeConstraints video_constraints_;
992
993 // For remote peer communication.
994 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
995
996 // Store references to the video capturers we've created, so that we can stop
997 // them, if required.
perkjcaafdba2016-03-20 07:34:29 -0700998 std::vector<cricket::FakeVideoCapturer*> video_capturers_;
999 webrtc::VideoRotation capture_rotation_ = webrtc::kVideoRotation_0;
1000 // |local_video_renderer_| attached to the first created local video track.
1001 rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001002
htaaac2dea2016-03-10 13:35:55 -08001003 webrtc::FakeConstraints offer_answer_constraints_;
1004 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001005 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
1006 bool remove_bundle_ =
1007 false; // True if bundle should be removed in received SDP.
1008 bool remove_sdes_ =
1009 false; // True if a=crypto should be removed in received SDP.
perkjcaafdba2016-03-20 07:34:29 -07001010 // |remove_cvo_| is true if extension urn:3gpp:video-orientation should be
1011 // removed in the received SDP.
1012 bool remove_cvo_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001013
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001014 rtc::scoped_refptr<DataChannelInterface> data_channel_;
1015 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001016};
1017
deadbeef7c73bdb2015-12-10 15:10:44 -08001018class P2PTestConductor : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019 public:
deadbeef7c73bdb2015-12-10 15:10:44 -08001020 P2PTestConductor()
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001021 : pss_(new rtc::PhysicalSocketServer),
1022 ss_(new rtc::VirtualSocketServer(pss_.get())),
1023 ss_scope_(ss_.get()) {}
1024
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001025 bool SessionActive() {
1026 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001027 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001028 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001029
hta6b4f8392016-03-10 00:24:31 -08001030 // Return true if the number of frames provided have been received
1031 // on the video and audio tracks provided.
1032 bool FramesHaveArrived(int audio_frames_to_receive,
1033 int video_frames_to_receive) {
1034 bool all_good = true;
1035 if (initiating_client_->HasLocalAudioTrack() &&
1036 receiving_client_->can_receive_audio()) {
1037 all_good &=
1038 receiving_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1039 }
1040 if (initiating_client_->HasLocalVideoTrack() &&
1041 receiving_client_->can_receive_video()) {
1042 all_good &=
1043 receiving_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1044 }
1045 if (receiving_client_->HasLocalAudioTrack() &&
1046 initiating_client_->can_receive_audio()) {
1047 all_good &=
1048 initiating_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1049 }
1050 if (receiving_client_->HasLocalVideoTrack() &&
1051 initiating_client_->can_receive_video()) {
1052 all_good &=
1053 initiating_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1054 }
1055 return all_good;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 }
hta6b4f8392016-03-10 00:24:31 -08001057
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058 void VerifyDtmf() {
1059 initiating_client_->VerifyDtmf();
1060 receiving_client_->VerifyDtmf();
1061 }
1062
1063 void TestUpdateOfferWithRejectedContent() {
deadbeefc9be0072015-12-14 18:27:57 -08001064 // Renegotiate, rejecting the video m-line.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001065 initiating_client_->Negotiate(true, false);
deadbeefc9be0072015-12-14 18:27:57 -08001066 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1067
1068 int pc1_audio_received = initiating_client_->audio_frames_received();
1069 int pc1_video_received = initiating_client_->video_frames_received();
1070 int pc2_audio_received = receiving_client_->audio_frames_received();
1071 int pc2_video_received = receiving_client_->video_frames_received();
1072
1073 // Wait for some additional audio frames to be received.
1074 EXPECT_TRUE_WAIT(initiating_client_->AudioFramesReceivedCheck(
1075 pc1_audio_received + kEndAudioFrameCount) &&
1076 receiving_client_->AudioFramesReceivedCheck(
1077 pc2_audio_received + kEndAudioFrameCount),
1078 kMaxWaitForFramesMs);
1079
1080 // During this time, we shouldn't have received any additional video frames
1081 // for the rejected video tracks.
1082 EXPECT_EQ(pc1_video_received, initiating_client_->video_frames_received());
1083 EXPECT_EQ(pc2_video_received, receiving_client_->video_frames_received());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001084 }
1085
1086 void VerifyRenderedSize(int width, int height) {
perkjcaafdba2016-03-20 07:34:29 -07001087 VerifyRenderedSize(width, height, webrtc::kVideoRotation_0);
1088 }
1089
1090 void VerifyRenderedSize(int width,
1091 int height,
1092 webrtc::VideoRotation rotation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001093 EXPECT_EQ(width, receiving_client()->rendered_width());
1094 EXPECT_EQ(height, receiving_client()->rendered_height());
perkjcaafdba2016-03-20 07:34:29 -07001095 EXPECT_EQ(rotation, receiving_client()->rendered_rotation());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001096 EXPECT_EQ(width, initializing_client()->rendered_width());
1097 EXPECT_EQ(height, initializing_client()->rendered_height());
perkjcaafdba2016-03-20 07:34:29 -07001098 EXPECT_EQ(rotation, initializing_client()->rendered_rotation());
1099
1100 // Verify size of the local preview.
1101 EXPECT_EQ(width, initializing_client()->local_rendered_width());
1102 EXPECT_EQ(height, initializing_client()->local_rendered_height());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001103 }
1104
1105 void VerifySessionDescriptions() {
1106 initiating_client_->VerifyRejectedMediaInSessionDescription();
1107 receiving_client_->VerifyRejectedMediaInSessionDescription();
1108 initiating_client_->VerifyLocalIceUfragAndPassword();
1109 receiving_client_->VerifyLocalIceUfragAndPassword();
1110 }
1111
deadbeef7c73bdb2015-12-10 15:10:44 -08001112 ~P2PTestConductor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001113 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001114 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115 }
1116 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001117 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001119 }
1120
deadbeefaf1b59c2015-10-15 12:08:41 -07001121 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001122
1123 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1124 MediaConstraintsInterface* recv_constraints) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001125 return CreateTestClients(init_constraints, nullptr, recv_constraints,
1126 nullptr);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001127 }
1128
htaaac2dea2016-03-10 13:35:55 -08001129 bool CreateTestClientsThatPreferNoConstraints() {
1130 initiating_client_.reset(
1131 PeerConnectionTestClient::CreateClientPreferNoConstraints("Caller: ",
1132 nullptr));
1133 receiving_client_.reset(
1134 PeerConnectionTestClient::CreateClientPreferNoConstraints("Callee: ",
1135 nullptr));
1136 if (!initiating_client_ || !receiving_client_) {
1137 return false;
1138 }
1139 // Remember the choice for possible later resets of the clients.
1140 prefer_constraint_apis_ = false;
1141 SetSignalingReceivers();
1142 return true;
1143 }
1144
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001145 void SetSignalingReceivers() {
1146 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
1147 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1148 }
1149
Joachim Bauch04e5b492015-05-29 09:40:39 +02001150 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1151 PeerConnectionFactory::Options* init_options,
1152 MediaConstraintsInterface* recv_constraints,
1153 PeerConnectionFactory::Options* recv_options) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001154 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
1155 "Caller: ", init_constraints, init_options));
1156 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
1157 "Callee: ", recv_constraints, recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001158 if (!initiating_client_ || !receiving_client_) {
1159 return false;
1160 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001161 SetSignalingReceivers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001162 return true;
1163 }
1164
1165 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1166 const webrtc::FakeConstraints& recv_constraints) {
1167 initiating_client_->SetVideoConstraints(init_constraints);
1168 receiving_client_->SetVideoConstraints(recv_constraints);
1169 }
1170
perkjcaafdba2016-03-20 07:34:29 -07001171 void SetCaptureRotation(webrtc::VideoRotation rotation) {
1172 initiating_client_->SetCaptureRotation(rotation);
1173 receiving_client_->SetCaptureRotation(rotation);
1174 }
1175
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001176 void EnableVideoDecoderFactory() {
1177 initiating_client_->EnableVideoDecoderFactory();
1178 receiving_client_->EnableVideoDecoderFactory();
1179 }
1180
1181 // This test sets up a call between two parties. Both parties send static
1182 // frames to each other. Once the test is finished the number of sent frames
1183 // is compared to the number of received frames.
1184 void LocalP2PTest() {
1185 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1186 initiating_client_->AddMediaStream(true, true);
1187 }
1188 initiating_client_->Negotiate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189 // Assert true is used here since next tests are guaranteed to fail and
1190 // would eat up 5 seconds.
1191 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1192 VerifySessionDescriptions();
1193
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194 int audio_frame_count = kEndAudioFrameCount;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195 int video_frame_count = kEndVideoFrameCount;
hta6b4f8392016-03-10 00:24:31 -08001196 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1197
1198 if ((!initiating_client_->can_receive_audio() &&
1199 !initiating_client_->can_receive_video()) ||
1200 (!receiving_client_->can_receive_audio() &&
1201 !receiving_client_->can_receive_video())) {
1202 // Neither audio nor video will flow, so connections won't be
1203 // established. There's nothing more to check.
1204 // TODO(hta): Check connection if there's a data channel.
1205 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001206 }
1207
hta6b4f8392016-03-10 00:24:31 -08001208 // Audio or video is expected to flow, so both clients should reach the
1209 // Connected state, and the offerer (ICE controller) should proceed to
1210 // Completed.
1211 // Note: These tests have been observed to fail under heavy load at
1212 // shorter timeouts, so they may be flaky.
1213 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1214 initiating_client_->ice_connection_state(),
1215 kMaxWaitForFramesMs);
1216 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1217 receiving_client_->ice_connection_state(),
1218 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001219
hta6b4f8392016-03-10 00:24:31 -08001220 // The ICE gathering state should end up in kIceGatheringComplete,
1221 // but there's a bug that prevents this at the moment, and the state
1222 // machine is being updated by the WEBRTC WG.
1223 // TODO(hta): Update this check when spec revisions finish.
1224 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1225 initiating_client_->ice_gathering_state());
1226 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1227 receiving_client_->ice_gathering_state(),
1228 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001229
hta6b4f8392016-03-10 00:24:31 -08001230 // Check that the expected number of frames have arrived.
1231 EXPECT_TRUE_WAIT(FramesHaveArrived(audio_frame_count, video_frame_count),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001232 kMaxWaitForFramesMs);
1233 }
1234
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001235 void SetupAndVerifyDtlsCall() {
1236 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1237 FakeConstraints setup_constraints;
1238 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1239 true);
1240 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1241 LocalP2PTest();
1242 VerifyRenderedSize(640, 480);
1243 }
1244
1245 PeerConnectionTestClient* CreateDtlsClientWithAlternateKey() {
1246 FakeConstraints setup_constraints;
1247 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1248 true);
1249
1250 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
1251 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
1252 : nullptr);
1253 dtls_identity_store->use_alternate_key();
1254
1255 // Make sure the new client is using a different certificate.
1256 return PeerConnectionTestClient::CreateClientWithDtlsIdentityStore(
kwiberg0eb15ed2015-12-17 03:04:15 -08001257 "New Peer: ", &setup_constraints, nullptr,
htaaac2dea2016-03-10 13:35:55 -08001258 std::move(dtls_identity_store), prefer_constraint_apis_);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001259 }
1260
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001261 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1262 // Messages may get lost on the unreliable DataChannel, so we send multiple
1263 // times to avoid test flakiness.
1264 static const size_t kSendAttempts = 5;
1265
1266 for (size_t i = 0; i < kSendAttempts; ++i) {
1267 dc->Send(DataBuffer(data));
1268 }
1269 }
1270
deadbeefaf1b59c2015-10-15 12:08:41 -07001271 PeerConnectionTestClient* initializing_client() {
1272 return initiating_client_.get();
1273 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001274
1275 // Set the |initiating_client_| to the |client| passed in and return the
1276 // original |initiating_client_|.
1277 PeerConnectionTestClient* set_initializing_client(
1278 PeerConnectionTestClient* client) {
1279 PeerConnectionTestClient* old = initiating_client_.release();
1280 initiating_client_.reset(client);
1281 return old;
1282 }
1283
deadbeefaf1b59c2015-10-15 12:08:41 -07001284 PeerConnectionTestClient* receiving_client() {
1285 return receiving_client_.get();
1286 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001288 // Set the |receiving_client_| to the |client| passed in and return the
1289 // original |receiving_client_|.
1290 PeerConnectionTestClient* set_receiving_client(
1291 PeerConnectionTestClient* client) {
1292 PeerConnectionTestClient* old = receiving_client_.release();
1293 receiving_client_.reset(client);
1294 return old;
1295 }
1296
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001298 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1299 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1300 rtc::SocketServerScope ss_scope_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001301 rtc::scoped_ptr<PeerConnectionTestClient> initiating_client_;
1302 rtc::scoped_ptr<PeerConnectionTestClient> receiving_client_;
htaaac2dea2016-03-10 13:35:55 -08001303 bool prefer_constraint_apis_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001305
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001306// Disable for TSan v2, see
1307// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1308#if !defined(THREAD_SANITIZER)
1309
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001311// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1312// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001313TEST_F(P2PTestConductor, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001314 ASSERT_TRUE(CreateTestClients());
1315 LocalP2PTest();
1316 VerifyDtmf();
1317}
1318
1319// This test sets up a Jsep call between two parties and test that we can get a
1320// video aspect ratio of 16:9.
deadbeef7c73bdb2015-12-10 15:10:44 -08001321TEST_F(P2PTestConductor, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322 ASSERT_TRUE(CreateTestClients());
1323 FakeConstraints constraint;
1324 double requested_ratio = 640.0/360;
1325 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1326 SetVideoConstraints(constraint, constraint);
1327 LocalP2PTest();
1328
1329 ASSERT_LE(0, initializing_client()->rendered_height());
1330 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001331 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332 initializing_client()->rendered_height();
1333 EXPECT_LE(requested_ratio, initiating_video_ratio);
1334
1335 ASSERT_LE(0, receiving_client()->rendered_height());
1336 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001337 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001338 receiving_client()->rendered_height();
1339 EXPECT_LE(requested_ratio, receiving_video_ratio);
1340}
1341
1342// This test sets up a Jsep call between two parties and test that the
1343// received video has a resolution of 1280*720.
1344// TODO(mallinath): Enable when
1345// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
deadbeef7c73bdb2015-12-10 15:10:44 -08001346TEST_F(P2PTestConductor, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001347 ASSERT_TRUE(CreateTestClients());
1348 FakeConstraints constraint;
1349 constraint.SetMandatoryMinWidth(1280);
1350 constraint.SetMandatoryMinHeight(720);
1351 SetVideoConstraints(constraint, constraint);
1352 LocalP2PTest();
1353 VerifyRenderedSize(1280, 720);
1354}
1355
1356// This test sets up a call between two endpoints that are configured to use
1357// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001358TEST_F(P2PTestConductor, LocalP2PTestDtls) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001359 SetupAndVerifyDtlsCall();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001360}
1361
hta6b4f8392016-03-10 00:24:31 -08001362// This test sets up an one-way call, with media only from initiator to
1363// responder.
1364TEST_F(P2PTestConductor, OneWayMediaCall) {
1365 ASSERT_TRUE(CreateTestClients());
1366 receiving_client()->set_auto_add_stream(false);
1367 LocalP2PTest();
1368}
1369
htaaac2dea2016-03-10 13:35:55 -08001370TEST_F(P2PTestConductor, OneWayMediaCallWithoutConstraints) {
1371 ASSERT_TRUE(CreateTestClientsThatPreferNoConstraints());
1372 receiving_client()->set_auto_add_stream(false);
1373 LocalP2PTest();
1374}
1375
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001376// This test sets up a audio call initially and then upgrades to audio/video,
1377// using DTLS.
deadbeef7c73bdb2015-12-10 15:10:44 -08001378TEST_F(P2PTestConductor, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001379 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001380 FakeConstraints setup_constraints;
1381 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1382 true);
1383 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1384 receiving_client()->SetReceiveAudioVideo(true, false);
1385 LocalP2PTest();
1386 receiving_client()->SetReceiveAudioVideo(true, true);
1387 receiving_client()->Negotiate();
1388}
1389
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001390// This test sets up a call transfer to a new caller with a different DTLS
1391// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001392TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCallee) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001393 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1394 SetupAndVerifyDtlsCall();
1395
1396 // Keeping the original peer around which will still send packets to the
1397 // receiving client. These SRTP packets will be dropped.
1398 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1399 set_initializing_client(CreateDtlsClientWithAlternateKey()));
1400 original_peer->pc()->Close();
1401
1402 SetSignalingReceivers();
1403 receiving_client()->SetExpectIceRestart(true);
1404 LocalP2PTest();
1405 VerifyRenderedSize(640, 480);
1406}
1407
guoweis46383312015-12-17 16:45:59 -08001408// This test sets up a non-bundle call and apply bundle during ICE restart. When
1409// bundle is in effect in the restart, the channel can successfully reset its
1410// DTLS-SRTP context.
1411TEST_F(P2PTestConductor, LocalP2PTestDtlsBundleInIceRestart) {
1412 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1413 FakeConstraints setup_constraints;
1414 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1415 true);
1416 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1417 receiving_client()->RemoveBundleFromReceivedSdp(true);
1418 LocalP2PTest();
1419 VerifyRenderedSize(640, 480);
1420
1421 initializing_client()->IceRestart();
1422 receiving_client()->SetExpectIceRestart(true);
1423 receiving_client()->RemoveBundleFromReceivedSdp(false);
1424 LocalP2PTest();
1425 VerifyRenderedSize(640, 480);
1426}
1427
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001428// This test sets up a call transfer to a new callee with a different DTLS
1429// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001430TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCaller) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001431 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1432 SetupAndVerifyDtlsCall();
1433
1434 // Keeping the original peer around which will still send packets to the
1435 // receiving client. These SRTP packets will be dropped.
1436 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1437 set_receiving_client(CreateDtlsClientWithAlternateKey()));
1438 original_peer->pc()->Close();
1439
1440 SetSignalingReceivers();
1441 initializing_client()->IceRestart();
1442 LocalP2PTest();
1443 VerifyRenderedSize(640, 480);
1444}
1445
perkjcaafdba2016-03-20 07:34:29 -07001446TEST_F(P2PTestConductor, LocalP2PTestCVO) {
1447 ASSERT_TRUE(CreateTestClients());
1448 SetCaptureRotation(webrtc::kVideoRotation_90);
1449 LocalP2PTest();
1450 VerifyRenderedSize(640, 480, webrtc::kVideoRotation_90);
1451}
1452
1453TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportCVO) {
1454 ASSERT_TRUE(CreateTestClients());
1455 SetCaptureRotation(webrtc::kVideoRotation_90);
1456 receiving_client()->RemoveCvoFromReceivedSdp(true);
1457 LocalP2PTest();
1458 VerifyRenderedSize(480, 640, webrtc::kVideoRotation_0);
1459}
1460
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001461// This test sets up a call between two endpoints that are configured to use
1462// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1463// negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001464TEST_F(P2PTestConductor, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001465 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001466 FakeConstraints setup_constraints;
1467 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1468 true);
1469 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1470 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1471 LocalP2PTest();
1472 VerifyRenderedSize(640, 480);
1473}
1474
1475// This test sets up a Jsep call between two parties, and the callee only
1476// accept to receive video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001477TEST_F(P2PTestConductor, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478 ASSERT_TRUE(CreateTestClients());
1479 receiving_client()->SetReceiveAudioVideo(false, true);
1480 LocalP2PTest();
1481}
1482
1483// This test sets up a Jsep call between two parties, and the callee only
1484// accept to receive audio.
deadbeef7c73bdb2015-12-10 15:10:44 -08001485TEST_F(P2PTestConductor, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001486 ASSERT_TRUE(CreateTestClients());
1487 receiving_client()->SetReceiveAudioVideo(true, false);
1488 LocalP2PTest();
1489}
1490
1491// This test sets up a Jsep call between two parties, and the callee reject both
1492// audio and video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001493TEST_F(P2PTestConductor, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001494 ASSERT_TRUE(CreateTestClients());
1495 receiving_client()->SetReceiveAudioVideo(false, false);
1496 LocalP2PTest();
1497}
1498
1499// This test sets up an audio and video call between two parties. After the call
1500// runs for a while (10 frames), the caller sends an update offer with video
1501// being rejected. Once the re-negotiation is done, the video flow should stop
1502// and the audio flow should continue.
deadbeefc9be0072015-12-14 18:27:57 -08001503TEST_F(P2PTestConductor, UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001504 ASSERT_TRUE(CreateTestClients());
1505 LocalP2PTest();
1506 TestUpdateOfferWithRejectedContent();
1507}
1508
1509// This test sets up a Jsep call between two parties. The MSID is removed from
1510// the SDP strings from the caller.
deadbeefc9be0072015-12-14 18:27:57 -08001511TEST_F(P2PTestConductor, LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512 ASSERT_TRUE(CreateTestClients());
1513 receiving_client()->RemoveMsidFromReceivedSdp(true);
1514 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1515 // audio and video is muxed when MSID is disabled. Remove
1516 // SetRemoveBundleFromSdp once
1517 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1518 receiving_client()->RemoveBundleFromReceivedSdp(true);
1519 LocalP2PTest();
1520}
1521
1522// This test sets up a Jsep call between two parties and the initiating peer
1523// sends two steams.
1524// TODO(perkj): Disabled due to
1525// https://code.google.com/p/webrtc/issues/detail?id=1454
deadbeef7c73bdb2015-12-10 15:10:44 -08001526TEST_F(P2PTestConductor, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001527 ASSERT_TRUE(CreateTestClients());
1528 // Set optional video constraint to max 320pixels to decrease CPU usage.
1529 FakeConstraints constraint;
1530 constraint.SetOptionalMaxWidth(320);
1531 SetVideoConstraints(constraint, constraint);
1532 initializing_client()->AddMediaStream(true, true);
1533 initializing_client()->AddMediaStream(false, true);
1534 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1535 LocalP2PTest();
1536 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1537}
1538
1539// Test that we can receive the audio output level from a remote audio track.
deadbeef7c73bdb2015-12-10 15:10:44 -08001540TEST_F(P2PTestConductor, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541 ASSERT_TRUE(CreateTestClients());
1542 LocalP2PTest();
1543
1544 StreamCollectionInterface* remote_streams =
1545 initializing_client()->remote_streams();
1546 ASSERT_GT(remote_streams->count(), 0u);
1547 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1548 MediaStreamTrackInterface* remote_audio_track =
1549 remote_streams->at(0)->GetAudioTracks()[0];
1550
1551 // Get the audio output level stats. Note that the level is not available
1552 // until a RTCP packet has been received.
1553 EXPECT_TRUE_WAIT(
1554 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1555 kMaxWaitForStatsMs);
1556}
1557
1558// Test that an audio input level is reported.
deadbeef7c73bdb2015-12-10 15:10:44 -08001559TEST_F(P2PTestConductor, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001560 ASSERT_TRUE(CreateTestClients());
1561 LocalP2PTest();
1562
1563 // Get the audio input level stats. The level should be available very
1564 // soon after the test starts.
1565 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1566 kMaxWaitForStatsMs);
1567}
1568
1569// Test that we can get incoming byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001570TEST_F(P2PTestConductor, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001571 ASSERT_TRUE(CreateTestClients());
1572 LocalP2PTest();
1573
1574 StreamCollectionInterface* remote_streams =
1575 initializing_client()->remote_streams();
1576 ASSERT_GT(remote_streams->count(), 0u);
1577 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1578 MediaStreamTrackInterface* remote_audio_track =
1579 remote_streams->at(0)->GetAudioTracks()[0];
1580 EXPECT_TRUE_WAIT(
1581 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1582 kMaxWaitForStatsMs);
1583
1584 MediaStreamTrackInterface* remote_video_track =
1585 remote_streams->at(0)->GetVideoTracks()[0];
1586 EXPECT_TRUE_WAIT(
1587 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1588 kMaxWaitForStatsMs);
1589}
1590
1591// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001592TEST_F(P2PTestConductor, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593 ASSERT_TRUE(CreateTestClients());
1594 LocalP2PTest();
1595
1596 StreamCollectionInterface* local_streams =
1597 initializing_client()->local_streams();
1598 ASSERT_GT(local_streams->count(), 0u);
1599 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1600 MediaStreamTrackInterface* local_audio_track =
1601 local_streams->at(0)->GetAudioTracks()[0];
1602 EXPECT_TRUE_WAIT(
1603 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1604 kMaxWaitForStatsMs);
1605
1606 MediaStreamTrackInterface* local_video_track =
1607 local_streams->at(0)->GetVideoTracks()[0];
1608 EXPECT_TRUE_WAIT(
1609 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1610 kMaxWaitForStatsMs);
1611}
1612
Joachim Bauch04e5b492015-05-29 09:40:39 +02001613// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001614TEST_F(P2PTestConductor, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001615 PeerConnectionFactory::Options init_options;
1616 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1617 PeerConnectionFactory::Options recv_options;
1618 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001619 ASSERT_TRUE(
1620 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001621 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1622 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1623 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001624 LocalP2PTest();
1625
torbjorng43166b82016-03-11 00:06:47 -08001626 EXPECT_TRUE_WAIT(
1627 rtc::SSLStreamAdapter::IsAcceptableCipher(
1628 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1629 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001630 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001631 initializing_client()->GetSrtpCipherStats(),
1632 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001633 EXPECT_EQ(1,
1634 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1635 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001636}
1637
1638// Test that DTLS 1.2 is used if both ends support it.
torbjorng79a5a832016-01-15 07:16:51 -08001639TEST_F(P2PTestConductor, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001640 PeerConnectionFactory::Options init_options;
1641 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1642 PeerConnectionFactory::Options recv_options;
1643 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001644 ASSERT_TRUE(
1645 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001646 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1647 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1648 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001649 LocalP2PTest();
1650
torbjorng43166b82016-03-11 00:06:47 -08001651 EXPECT_TRUE_WAIT(
1652 rtc::SSLStreamAdapter::IsAcceptableCipher(
1653 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1654 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001655 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001656 initializing_client()->GetSrtpCipherStats(),
1657 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001658 EXPECT_EQ(1,
1659 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1660 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001661}
1662
1663// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1664// received supports 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001665TEST_F(P2PTestConductor, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001666 PeerConnectionFactory::Options init_options;
1667 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1668 PeerConnectionFactory::Options recv_options;
1669 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001670 ASSERT_TRUE(
1671 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001672 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1673 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1674 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001675 LocalP2PTest();
1676
torbjorng43166b82016-03-11 00:06:47 -08001677 EXPECT_TRUE_WAIT(
1678 rtc::SSLStreamAdapter::IsAcceptableCipher(
1679 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1680 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001681 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001682 initializing_client()->GetSrtpCipherStats(),
1683 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001684 EXPECT_EQ(1,
1685 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1686 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001687}
1688
1689// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1690// received supports 1.2.
torbjorng43166b82016-03-11 00:06:47 -08001691TEST_F(P2PTestConductor, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001692 PeerConnectionFactory::Options init_options;
1693 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1694 PeerConnectionFactory::Options recv_options;
1695 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001696 ASSERT_TRUE(
1697 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001698 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1699 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1700 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001701 LocalP2PTest();
1702
torbjorng43166b82016-03-11 00:06:47 -08001703 EXPECT_TRUE_WAIT(
1704 rtc::SSLStreamAdapter::IsAcceptableCipher(
1705 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1706 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001707 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001708 initializing_client()->GetSrtpCipherStats(),
1709 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001710 EXPECT_EQ(1,
1711 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1712 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001713}
1714
deadbeefb5cb19b2015-11-23 16:39:12 -08001715// This test sets up a call between two parties with audio, video and an RTP
1716// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001717TEST_F(P2PTestConductor, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 FakeConstraints setup_constraints;
1719 setup_constraints.SetAllowRtpDataChannels();
1720 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1721 initializing_client()->CreateDataChannel();
1722 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001723 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1724 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1726 kMaxWaitMs);
1727 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1728 kMaxWaitMs);
1729
1730 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001731
1732 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1734 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001735
1736 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1738 kMaxWaitMs);
1739
1740 receiving_client()->data_channel()->Close();
1741 // Send new offer and answer.
1742 receiving_client()->Negotiate();
1743 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1744 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1745}
1746
deadbeefb5cb19b2015-11-23 16:39:12 -08001747// This test sets up a call between two parties with audio, video and an SCTP
1748// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001749TEST_F(P2PTestConductor, LocalP2PTestSctpDataChannel) {
deadbeefb5cb19b2015-11-23 16:39:12 -08001750 ASSERT_TRUE(CreateTestClients());
1751 initializing_client()->CreateDataChannel();
1752 LocalP2PTest();
1753 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1754 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
1755 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1756 kMaxWaitMs);
1757 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
1758
1759 std::string data = "hello world";
1760
1761 initializing_client()->data_channel()->Send(DataBuffer(data));
1762 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1763 kMaxWaitMs);
1764
1765 receiving_client()->data_channel()->Send(DataBuffer(data));
1766 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1767 kMaxWaitMs);
1768
1769 receiving_client()->data_channel()->Close();
deadbeef15887932015-12-14 19:32:34 -08001770 EXPECT_TRUE_WAIT(!initializing_client()->data_observer()->IsOpen(),
1771 kMaxWaitMs);
1772 EXPECT_TRUE_WAIT(!receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
deadbeefb5cb19b2015-11-23 16:39:12 -08001773}
1774
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775// This test sets up a call between two parties and creates a data channel.
1776// The test tests that received data is buffered unless an observer has been
1777// registered.
1778// Rtp data channels can receive data before the underlying
1779// transport has detected that a channel is writable and thus data can be
1780// received before the data channel state changes to open. That is hard to test
1781// but the same buffering is used in that case.
deadbeef7c73bdb2015-12-10 15:10:44 -08001782TEST_F(P2PTestConductor, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783 FakeConstraints setup_constraints;
1784 setup_constraints.SetAllowRtpDataChannels();
1785 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1786 initializing_client()->CreateDataChannel();
1787 initializing_client()->Negotiate();
1788
deadbeefaf1b59c2015-10-15 12:08:41 -07001789 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1790 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1792 kMaxWaitMs);
1793 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1794 receiving_client()->data_channel()->state(), kMaxWaitMs);
1795
1796 // Unregister the existing observer.
1797 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001798
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001799 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001800 SendRtpData(initializing_client()->data_channel(), data);
1801
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001802 // Wait a while to allow the sent data to arrive before an observer is
1803 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001804 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805
1806 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1807 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1808}
1809
1810// This test sets up a call between two parties with audio, video and but only
1811// the initiating client support data.
deadbeef7c73bdb2015-12-10 15:10:44 -08001812TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001813 FakeConstraints setup_constraints_1;
1814 setup_constraints_1.SetAllowRtpDataChannels();
1815 // Must disable DTLS to make negotiation succeed.
1816 setup_constraints_1.SetMandatory(
1817 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1818 FakeConstraints setup_constraints_2;
1819 setup_constraints_2.SetMandatory(
1820 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1821 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822 initializing_client()->CreateDataChannel();
1823 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001824 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825 EXPECT_FALSE(receiving_client()->data_channel());
1826 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1827}
1828
1829// This test sets up a call between two parties with audio, video. When audio
1830// and video is setup and flowing and data channel is negotiated.
deadbeef7c73bdb2015-12-10 15:10:44 -08001831TEST_F(P2PTestConductor, AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001832 FakeConstraints setup_constraints;
1833 setup_constraints.SetAllowRtpDataChannels();
1834 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1835 LocalP2PTest();
1836 initializing_client()->CreateDataChannel();
1837 // Send new offer and answer.
1838 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07001839 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1840 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001841 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1842 kMaxWaitMs);
1843 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1844 kMaxWaitMs);
1845}
1846
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001847// This test sets up a Jsep call with SCTP DataChannel and verifies the
1848// negotiation is completed without error.
1849#ifdef HAVE_SCTP
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01001850// Disabled on Win dbg: https://bugs.chromium.org/p/webrtc/issues/detail?id=5659
Stefan Holmer102362b2016-03-18 09:39:07 +01001851#if defined(WEBRTC_WIN) && defined(_DEBUG)
Stefan Holmer55d6e7c2016-03-17 16:26:40 +01001852#define MAYBE_CreateOfferWithSctpDataChannel \
1853 DISABLED_CreateOfferWithSctpDataChannel
1854#else
1855#define MAYBE_CreateOfferWithSctpDataChannel CreateOfferWithSctpDataChannel
1856#endif
1857TEST_F(P2PTestConductor, MAYBE_CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001858 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001859 FakeConstraints constraints;
1860 constraints.SetMandatory(
1861 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1862 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1863 initializing_client()->CreateDataChannel();
1864 initializing_client()->Negotiate(false, false);
1865}
1866#endif
1867
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001868// This test sets up a call between two parties with audio, and video.
1869// During the call, the initializing side restart ice and the test verifies that
1870// new ice candidates are generated and audio and video still can flow.
deadbeef7c73bdb2015-12-10 15:10:44 -08001871TEST_F(P2PTestConductor, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001872 ASSERT_TRUE(CreateTestClients());
1873
1874 // Negotiate and wait for ice completion and make sure audio and video plays.
1875 LocalP2PTest();
1876
1877 // Create a SDP string of the first audio candidate for both clients.
1878 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1879 initializing_client()->pc()->local_description()->candidates(0);
1880 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1881 receiving_client()->pc()->local_description()->candidates(0);
1882 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1883 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1884 std::string initiator_candidate;
1885 EXPECT_TRUE(
1886 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1887 std::string receiver_candidate;
1888 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1889
1890 // Restart ice on the initializing client.
1891 receiving_client()->SetExpectIceRestart(true);
1892 initializing_client()->IceRestart();
1893
1894 // Negotiate and wait for ice completion again and make sure audio and video
1895 // plays.
1896 LocalP2PTest();
1897
1898 // Create a SDP string of the first audio candidate for both clients again.
1899 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1900 initializing_client()->pc()->local_description()->candidates(0);
1901 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1902 receiving_client()->pc()->local_description()->candidates(0);
1903 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1904 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1905 std::string initiator_candidate_restart;
1906 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1907 &initiator_candidate_restart));
1908 std::string receiver_candidate_restart;
1909 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1910 &receiver_candidate_restart));
1911
1912 // Verify that the first candidates in the local session descriptions has
1913 // changed.
1914 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1915 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1916}
1917
deadbeeffaac4972015-11-12 15:33:07 -08001918// This test sets up a call between two parties with audio, and video.
1919// It then renegotiates setting the video m-line to "port 0", then later
1920// renegotiates again, enabling video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001921TEST_F(P2PTestConductor, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08001922 ASSERT_TRUE(CreateTestClients());
1923
1924 // Do initial negotiation. Will result in video and audio sendonly m-lines.
1925 receiving_client()->set_auto_add_stream(false);
1926 initializing_client()->AddMediaStream(true, true);
1927 initializing_client()->Negotiate();
1928
1929 // Negotiate again, disabling the video m-line (receiving client will
1930 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
1931 receiving_client()->SetReceiveVideo(false);
1932 initializing_client()->Negotiate();
1933
1934 // Enable video and do negotiation again, making sure video is received
1935 // end-to-end.
1936 receiving_client()->SetReceiveVideo(true);
1937 receiving_client()->AddMediaStream(true, true);
1938 LocalP2PTest();
1939}
1940
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941// This test sets up a Jsep call between two parties with external
1942// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001943// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1944// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001945TEST_F(P2PTestConductor, DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001946 ASSERT_TRUE(CreateTestClients());
1947 EnableVideoDecoderFactory();
1948 LocalP2PTest();
1949}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001950
deadbeeffac06552015-11-25 11:26:01 -08001951// This tests that if we negotiate after calling CreateSender but before we
1952// have a track, then set a track later, frames from the newly-set track are
1953// received end-to-end.
deadbeef7c73bdb2015-12-10 15:10:44 -08001954TEST_F(P2PTestConductor, EarlyWarmupTest) {
deadbeeffac06552015-11-25 11:26:01 -08001955 ASSERT_TRUE(CreateTestClients());
deadbeefbd7d8f72015-12-18 16:58:44 -08001956 auto audio_sender =
1957 initializing_client()->pc()->CreateSender("audio", "stream_id");
1958 auto video_sender =
1959 initializing_client()->pc()->CreateSender("video", "stream_id");
deadbeeffac06552015-11-25 11:26:01 -08001960 initializing_client()->Negotiate();
1961 // Wait for ICE connection to complete, without any tracks.
1962 // Note that the receiving client WILL (in HandleIncomingOffer) create
1963 // tracks, so it's only the initiator here that's doing early warmup.
1964 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1965 VerifySessionDescriptions();
1966 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1967 initializing_client()->ice_connection_state(),
1968 kMaxWaitForFramesMs);
1969 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1970 receiving_client()->ice_connection_state(),
1971 kMaxWaitForFramesMs);
1972 // Now set the tracks, and expect frames to immediately start flowing.
1973 EXPECT_TRUE(
1974 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
1975 EXPECT_TRUE(
1976 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
hta6b4f8392016-03-10 00:24:31 -08001977 EXPECT_TRUE_WAIT(FramesHaveArrived(kEndAudioFrameCount, kEndVideoFrameCount),
deadbeeffac06552015-11-25 11:26:01 -08001978 kMaxWaitForFramesMs);
1979}
1980
deadbeef0a6c4ca2015-10-06 11:38:28 -07001981class IceServerParsingTest : public testing::Test {
1982 public:
1983 // Convenience for parsing a single URL.
1984 bool ParseUrl(const std::string& url) {
1985 return ParseUrl(url, std::string(), std::string());
1986 }
1987
1988 bool ParseUrl(const std::string& url,
1989 const std::string& username,
1990 const std::string& password) {
1991 PeerConnectionInterface::IceServers servers;
1992 PeerConnectionInterface::IceServer server;
1993 server.urls.push_back(url);
1994 server.username = username;
1995 server.password = password;
1996 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001997 return webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_);
deadbeef0a6c4ca2015-10-06 11:38:28 -07001998 }
1999
2000 protected:
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002001 cricket::ServerAddresses stun_servers_;
2002 std::vector<cricket::RelayServerConfig> turn_servers_;
deadbeef0a6c4ca2015-10-06 11:38:28 -07002003};
2004
2005// Make sure all STUN/TURN prefixes are parsed correctly.
2006TEST_F(IceServerParsingTest, ParseStunPrefixes) {
2007 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002008 EXPECT_EQ(1U, stun_servers_.size());
2009 EXPECT_EQ(0U, turn_servers_.size());
2010 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002011
2012 EXPECT_TRUE(ParseUrl("stuns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002013 EXPECT_EQ(1U, stun_servers_.size());
2014 EXPECT_EQ(0U, turn_servers_.size());
2015 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002016
2017 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002018 EXPECT_EQ(0U, stun_servers_.size());
2019 EXPECT_EQ(1U, turn_servers_.size());
2020 EXPECT_FALSE(turn_servers_[0].ports[0].secure);
2021 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002022
2023 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002024 EXPECT_EQ(0U, stun_servers_.size());
2025 EXPECT_EQ(1U, turn_servers_.size());
2026 EXPECT_TRUE(turn_servers_[0].ports[0].secure);
2027 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002028
2029 // invalid prefixes
2030 EXPECT_FALSE(ParseUrl("stunn:hostname"));
2031 EXPECT_FALSE(ParseUrl(":hostname"));
2032 EXPECT_FALSE(ParseUrl(":"));
2033 EXPECT_FALSE(ParseUrl(""));
2034}
2035
2036TEST_F(IceServerParsingTest, VerifyDefaults) {
2037 // TURNS defaults
2038 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002039 EXPECT_EQ(1U, turn_servers_.size());
2040 EXPECT_EQ(5349, turn_servers_[0].ports[0].address.port());
2041 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
2042 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002043
2044 // TURN defaults
2045 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002046 EXPECT_EQ(1U, turn_servers_.size());
2047 EXPECT_EQ(3478, turn_servers_[0].ports[0].address.port());
2048 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2049 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002050
2051 // STUN defaults
2052 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002053 EXPECT_EQ(1U, stun_servers_.size());
2054 EXPECT_EQ(3478, stun_servers_.begin()->port());
2055 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002056}
2057
2058// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
2059// can be parsed correctly.
2060TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
2061 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002062 EXPECT_EQ(1U, stun_servers_.size());
2063 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2064 EXPECT_EQ(1234, stun_servers_.begin()->port());
2065 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002066
2067 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002068 EXPECT_EQ(1U, stun_servers_.size());
2069 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2070 EXPECT_EQ(4321, stun_servers_.begin()->port());
2071 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002072
2073 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002074 EXPECT_EQ(1U, stun_servers_.size());
2075 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2076 EXPECT_EQ(9999, stun_servers_.begin()->port());
2077 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002078
2079 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002080 EXPECT_EQ(1U, stun_servers_.size());
2081 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2082 EXPECT_EQ(3478, stun_servers_.begin()->port());
2083 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002084
2085 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002086 EXPECT_EQ(1U, stun_servers_.size());
2087 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2088 EXPECT_EQ(3478, stun_servers_.begin()->port());
2089 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002090
2091 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002092 EXPECT_EQ(1U, stun_servers_.size());
2093 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2094 EXPECT_EQ(3478, stun_servers_.begin()->port());
2095 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002096
2097 // Try some invalid hostname:port strings.
2098 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
2099 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002100 EXPECT_FALSE(ParseUrl("stun:hostname:port:more"));
2101 EXPECT_FALSE(ParseUrl("stun:hostname:port more"));
deadbeef0a6c4ca2015-10-06 11:38:28 -07002102 EXPECT_FALSE(ParseUrl("stun:hostname:"));
2103 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
2104 EXPECT_FALSE(ParseUrl("stun::5555"));
2105 EXPECT_FALSE(ParseUrl("stun:"));
2106}
2107
2108// Test parsing the "?transport=xxx" part of the URL.
2109TEST_F(IceServerParsingTest, ParseTransport) {
2110 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002111 EXPECT_EQ(1U, turn_servers_.size());
2112 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
2113 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002114
2115 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002116 EXPECT_EQ(1U, turn_servers_.size());
2117 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2118 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002119
2120 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
2121}
2122
2123// Test parsing ICE username contained in URL.
2124TEST_F(IceServerParsingTest, ParseUsername) {
2125 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002126 EXPECT_EQ(1U, turn_servers_.size());
2127 EXPECT_EQ("user", turn_servers_[0].credentials.username);
2128 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002129
2130 EXPECT_FALSE(ParseUrl("turn:@hostname"));
2131 EXPECT_FALSE(ParseUrl("turn:username@"));
2132 EXPECT_FALSE(ParseUrl("turn:@"));
2133 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
2134}
2135
2136// Test that username and password from IceServer is copied into the resulting
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002137// RelayServerConfig.
deadbeef0a6c4ca2015-10-06 11:38:28 -07002138TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
2139 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002140 EXPECT_EQ(1U, turn_servers_.size());
2141 EXPECT_EQ("username", turn_servers_[0].credentials.username);
2142 EXPECT_EQ("password", turn_servers_[0].credentials.password);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002143}
2144
2145// Ensure that if a server has multiple URLs, each one is parsed.
2146TEST_F(IceServerParsingTest, ParseMultipleUrls) {
2147 PeerConnectionInterface::IceServers servers;
2148 PeerConnectionInterface::IceServer server;
2149 server.urls.push_back("stun:hostname");
2150 server.urls.push_back("turn:hostname");
2151 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002152 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2153 EXPECT_EQ(1U, stun_servers_.size());
2154 EXPECT_EQ(1U, turn_servers_.size());
deadbeef0a6c4ca2015-10-06 11:38:28 -07002155}
2156
Taylor Brandstetter893505d2016-01-07 15:12:48 -08002157// Ensure that TURN servers are given unique priorities,
2158// so that their resulting candidates have unique priorities.
2159TEST_F(IceServerParsingTest, TurnServerPrioritiesUnique) {
2160 PeerConnectionInterface::IceServers servers;
2161 PeerConnectionInterface::IceServer server;
2162 server.urls.push_back("turn:hostname");
2163 server.urls.push_back("turn:hostname2");
2164 servers.push_back(server);
2165 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2166 EXPECT_EQ(2U, turn_servers_.size());
2167 EXPECT_NE(turn_servers_[0].priority, turn_servers_[1].priority);
2168}
2169
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00002170#endif // if !defined(THREAD_SANITIZER)
hta6b4f8392016-03-10 00:24:31 -08002171
2172} // namespace