blob: 4817477035e3166ecbdb0ab6444691cc547239c6 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
11#include <stdio.h>
12
13#include <algorithm>
14#include <list>
15#include <map>
kwiberg0eb15ed2015-12-17 03:04:15 -080016#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include <vector>
18
Henrik Kjellander15583c12016-02-10 10:53:12 +010019#include "webrtc/api/dtmfsender.h"
20#include "webrtc/api/fakemetricsobserver.h"
21#include "webrtc/api/localaudiosource.h"
22#include "webrtc/api/mediastreaminterface.h"
23#include "webrtc/api/peerconnection.h"
24#include "webrtc/api/peerconnectionfactory.h"
25#include "webrtc/api/peerconnectioninterface.h"
26#include "webrtc/api/test/fakeaudiocapturemodule.h"
27#include "webrtc/api/test/fakeconstraints.h"
28#include "webrtc/api/test/fakedtlsidentitystore.h"
29#include "webrtc/api/test/fakeperiodicvideocapturer.h"
30#include "webrtc/api/test/fakevideotrackrenderer.h"
31#include "webrtc/api/test/mockpeerconnectionobservers.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000032#include "webrtc/base/gunit.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000033#include "webrtc/base/physicalsocketserver.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/scoped_ptr.h"
35#include "webrtc/base/ssladapter.h"
36#include "webrtc/base/sslstreamadapter.h"
37#include "webrtc/base/thread.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000038#include "webrtc/base/virtualsocketserver.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010039#include "webrtc/media/engine/fakewebrtcvideoengine.h"
kjellanderf4752772016-03-02 05:42:30 -080040#include "webrtc/p2p/base/p2pconstants.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000041#include "webrtc/p2p/base/sessiondescription.h"
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080042#include "webrtc/p2p/client/fakeportallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010043#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45#define MAYBE_SKIP_TEST(feature) \
46 if (!(feature())) { \
47 LOG(LS_INFO) << "Feature disabled... skipping"; \
48 return; \
49 }
50
51using cricket::ContentInfo;
52using cricket::FakeWebRtcVideoDecoder;
53using cricket::FakeWebRtcVideoDecoderFactory;
54using cricket::FakeWebRtcVideoEncoder;
55using cricket::FakeWebRtcVideoEncoderFactory;
56using cricket::MediaContentDescription;
57using webrtc::DataBuffer;
58using webrtc::DataChannelInterface;
59using webrtc::DtmfSender;
60using webrtc::DtmfSenderInterface;
61using webrtc::DtmfSenderObserverInterface;
62using webrtc::FakeConstraints;
63using webrtc::MediaConstraintsInterface;
deadbeeffaac4972015-11-12 15:33:07 -080064using webrtc::MediaStreamInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065using webrtc::MediaStreamTrackInterface;
66using webrtc::MockCreateSessionDescriptionObserver;
67using webrtc::MockDataChannelObserver;
68using webrtc::MockSetSessionDescriptionObserver;
69using webrtc::MockStatsObserver;
deadbeeffaac4972015-11-12 15:33:07 -080070using webrtc::ObserverInterface;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000071using webrtc::PeerConnectionInterface;
Joachim Bauch04e5b492015-05-29 09:40:39 +020072using webrtc::PeerConnectionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073using webrtc::SessionDescriptionInterface;
74using webrtc::StreamCollectionInterface;
75
hta6b4f8392016-03-10 00:24:31 -080076namespace {
77
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000078static const int kMaxWaitMs = 10000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000079// Disable for TSan v2, see
80// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
81// This declaration is also #ifdef'd as it causes uninitialized-variable
82// warnings.
83#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084static const int kMaxWaitForStatsMs = 3000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000085#endif
deadbeeffac06552015-11-25 11:26:01 -080086static const int kMaxWaitForActivationMs = 5000;
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000087static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088static const int kEndAudioFrameCount = 3;
89static const int kEndVideoFrameCount = 3;
90
91static const char kStreamLabelBase[] = "stream_label";
92static const char kVideoTrackLabelBase[] = "video_track";
93static const char kAudioTrackLabelBase[] = "audio_track";
94static const char kDataChannelLabel[] = "data_channel";
95
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +000096// Disable for TSan v2, see
97// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
98// This declaration is also #ifdef'd as it causes unused-variable errors.
99#if !defined(THREAD_SANITIZER)
100// SRTP cipher name negotiated by the tests. This must be updated if the
101// default changes.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800102static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000103#endif
104
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105static void RemoveLinesFromSdp(const std::string& line_start,
106 std::string* sdp) {
107 const char kSdpLineEnd[] = "\r\n";
108 size_t ssrc_pos = 0;
109 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
110 std::string::npos) {
111 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
112 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
113 }
114}
115
hta6b4f8392016-03-10 00:24:31 -0800116bool StreamsHaveAudioTrack(StreamCollectionInterface* streams) {
117 for (size_t idx = 0; idx < streams->count(); idx++) {
118 auto stream = streams->at(idx);
119 if (stream->GetAudioTracks().size() > 0) {
120 return true;
121 }
122 }
123 return false;
124}
125
126bool StreamsHaveVideoTrack(StreamCollectionInterface* streams) {
127 for (size_t idx = 0; idx < streams->count(); idx++) {
128 auto stream = streams->at(idx);
129 if (stream->GetVideoTracks().size() > 0) {
130 return true;
131 }
132 }
133 return false;
134}
135
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136class SignalingMessageReceiver {
137 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138 virtual void ReceiveSdpMessage(const std::string& type,
139 std::string& msg) = 0;
140 virtual void ReceiveIceMessage(const std::string& sdp_mid,
141 int sdp_mline_index,
142 const std::string& msg) = 0;
143
144 protected:
deadbeefaf1b59c2015-10-15 12:08:41 -0700145 SignalingMessageReceiver() {}
146 virtual ~SignalingMessageReceiver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147};
148
deadbeefaf1b59c2015-10-15 12:08:41 -0700149class PeerConnectionTestClient : public webrtc::PeerConnectionObserver,
deadbeeffaac4972015-11-12 15:33:07 -0800150 public SignalingMessageReceiver,
151 public ObserverInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 public:
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800153 static PeerConnectionTestClient* CreateClientWithDtlsIdentityStore(
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800154 const std::string& id,
155 const MediaConstraintsInterface* constraints,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800156 const PeerConnectionFactory::Options* options,
htaaac2dea2016-03-10 13:35:55 -0800157 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store,
perkj8aba9972016-04-10 23:54:34 -0700158 bool prefer_constraint_apis,
159 rtc::Thread* worker_thread) {
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),
perkj8aba9972016-04-10 23:54:34 -0700162 prefer_constraint_apis, worker_thread)) {
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,
perkj8aba9972016-04-10 23:54:34 -0700172 const PeerConnectionFactory::Options* options,
173 rtc::Thread* worker_thread) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800174 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
175 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
176 : nullptr);
177
perkj8aba9972016-04-10 23:54:34 -0700178 return CreateClientWithDtlsIdentityStore(id, constraints, options,
179 std::move(dtls_identity_store),
180 true, worker_thread);
htaaac2dea2016-03-10 13:35:55 -0800181 }
182
183 static PeerConnectionTestClient* CreateClientPreferNoConstraints(
184 const std::string& id,
perkj8aba9972016-04-10 23:54:34 -0700185 const PeerConnectionFactory::Options* options,
186 rtc::Thread* worker_thread) {
htaaac2dea2016-03-10 13:35:55 -0800187 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
188 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
189 : nullptr);
190
perkj8aba9972016-04-10 23:54:34 -0700191 return CreateClientWithDtlsIdentityStore(id, nullptr, options,
192 std::move(dtls_identity_store),
193 false, worker_thread);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800194 }
195
deadbeefaf1b59c2015-10-15 12:08:41 -0700196 ~PeerConnectionTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 }
198
deadbeefaf1b59c2015-10-15 12:08:41 -0700199 void Negotiate() { Negotiate(true, true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200
deadbeefaf1b59c2015-10-15 12:08:41 -0700201 void Negotiate(bool audio, bool video) {
202 rtc::scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700203 ASSERT_TRUE(DoCreateOffer(&offer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204
deadbeefaf1b59c2015-10-15 12:08:41 -0700205 if (offer->description()->GetContentByName("audio")) {
206 offer->description()->GetContentByName("audio")->rejected = !audio;
207 }
208 if (offer->description()->GetContentByName("video")) {
209 offer->description()->GetContentByName("video")->rejected = !video;
210 }
211
212 std::string sdp;
213 EXPECT_TRUE(offer->ToString(&sdp));
214 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
215 signaling_message_receiver_->ReceiveSdpMessage(
216 webrtc::SessionDescriptionInterface::kOffer, sdp);
217 }
218
219 // SignalingMessageReceiver callback.
220 void ReceiveSdpMessage(const std::string& type, std::string& msg) override {
221 FilterIncomingSdpMessage(&msg);
222 if (type == webrtc::SessionDescriptionInterface::kOffer) {
223 HandleIncomingOffer(msg);
224 } else {
225 HandleIncomingAnswer(msg);
226 }
227 }
228
229 // SignalingMessageReceiver callback.
230 void ReceiveIceMessage(const std::string& sdp_mid,
231 int sdp_mline_index,
232 const std::string& msg) override {
233 LOG(INFO) << id_ << "ReceiveIceMessage";
234 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
235 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
236 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
237 }
238
239 // PeerConnectionObserver callbacks.
240 void OnSignalingChange(
241 webrtc::PeerConnectionInterface::SignalingState new_state) override {
242 EXPECT_EQ(pc()->signaling_state(), new_state);
243 }
deadbeeffaac4972015-11-12 15:33:07 -0800244 void OnAddStream(MediaStreamInterface* media_stream) override {
245 media_stream->RegisterObserver(this);
deadbeefaf1b59c2015-10-15 12:08:41 -0700246 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
247 const std::string id = media_stream->GetVideoTracks()[i]->id();
248 ASSERT_TRUE(fake_video_renderers_.find(id) ==
249 fake_video_renderers_.end());
deadbeefc9be0072015-12-14 18:27:57 -0800250 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
251 media_stream->GetVideoTracks()[i]));
deadbeefaf1b59c2015-10-15 12:08:41 -0700252 }
253 }
deadbeeffaac4972015-11-12 15:33:07 -0800254 void OnRemoveStream(MediaStreamInterface* media_stream) override {}
deadbeefaf1b59c2015-10-15 12:08:41 -0700255 void OnRenegotiationNeeded() override {}
256 void OnIceConnectionChange(
257 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
258 EXPECT_EQ(pc()->ice_connection_state(), new_state);
259 }
260 void OnIceGatheringChange(
261 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
262 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
263 }
264 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
265 LOG(INFO) << id_ << "OnIceCandidate";
266
267 std::string ice_sdp;
268 EXPECT_TRUE(candidate->ToString(&ice_sdp));
269 if (signaling_message_receiver_ == nullptr) {
270 // Remote party may be deleted.
271 return;
272 }
273 signaling_message_receiver_->ReceiveIceMessage(
274 candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
275 }
276
deadbeeffaac4972015-11-12 15:33:07 -0800277 // MediaStreamInterface callback
278 void OnChanged() override {
279 // Track added or removed from MediaStream, so update our renderers.
280 rtc::scoped_refptr<StreamCollectionInterface> remote_streams =
281 pc()->remote_streams();
282 // Remove renderers for tracks that were removed.
283 for (auto it = fake_video_renderers_.begin();
284 it != fake_video_renderers_.end();) {
285 if (remote_streams->FindVideoTrack(it->first) == nullptr) {
deadbeefc9be0072015-12-14 18:27:57 -0800286 auto to_remove = it++;
287 removed_fake_video_renderers_.push_back(std::move(to_remove->second));
288 fake_video_renderers_.erase(to_remove);
deadbeeffaac4972015-11-12 15:33:07 -0800289 } else {
290 ++it;
291 }
292 }
293 // Create renderers for new video tracks.
294 for (size_t stream_index = 0; stream_index < remote_streams->count();
295 ++stream_index) {
296 MediaStreamInterface* remote_stream = remote_streams->at(stream_index);
297 for (size_t track_index = 0;
298 track_index < remote_stream->GetVideoTracks().size();
299 ++track_index) {
300 const std::string id =
301 remote_stream->GetVideoTracks()[track_index]->id();
302 if (fake_video_renderers_.find(id) != fake_video_renderers_.end()) {
303 continue;
304 }
deadbeefc9be0072015-12-14 18:27:57 -0800305 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
306 remote_stream->GetVideoTracks()[track_index]));
deadbeeffaac4972015-11-12 15:33:07 -0800307 }
308 }
309 }
310
deadbeefaf1b59c2015-10-15 12:08:41 -0700311 void SetVideoConstraints(const webrtc::FakeConstraints& video_constraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312 video_constraints_ = video_constraint;
313 }
314
315 void AddMediaStream(bool audio, bool video) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700316 std::string stream_label =
317 kStreamLabelBase +
318 rtc::ToString<int>(static_cast<int>(pc()->local_streams()->count()));
deadbeeffaac4972015-11-12 15:33:07 -0800319 rtc::scoped_refptr<MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000320 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321
322 if (audio && can_receive_audio()) {
deadbeeffac06552015-11-25 11:26:01 -0800323 stream->AddTrack(CreateLocalAudioTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324 }
325 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000326 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 }
328
deadbeefaf1b59c2015-10-15 12:08:41 -0700329 EXPECT_TRUE(pc()->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330 }
331
deadbeefaf1b59c2015-10-15 12:08:41 -0700332 size_t NumberOfLocalMediaStreams() { return pc()->local_streams()->count(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333
334 bool SessionActive() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700335 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 }
337
deadbeeffaac4972015-11-12 15:33:07 -0800338 // Automatically add a stream when receiving an offer, if we don't have one.
339 // Defaults to true.
340 void set_auto_add_stream(bool auto_add_stream) {
341 auto_add_stream_ = auto_add_stream;
342 }
343
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344 void set_signaling_message_receiver(
deadbeefaf1b59c2015-10-15 12:08:41 -0700345 SignalingMessageReceiver* signaling_message_receiver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346 signaling_message_receiver_ = signaling_message_receiver;
347 }
348
349 void EnableVideoDecoderFactory() {
350 video_decoder_factory_enabled_ = true;
351 fake_video_decoder_factory_->AddSupportedVideoCodecType(
352 webrtc::kVideoCodecVP8);
353 }
354
deadbeefaf1b59c2015-10-15 12:08:41 -0700355 void IceRestart() {
htaaac2dea2016-03-10 13:35:55 -0800356 offer_answer_constraints_.SetMandatoryIceRestart(true);
357 offer_answer_options_.ice_restart = true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700358 SetExpectIceRestart(true);
359 }
360
361 void SetExpectIceRestart(bool expect_restart) {
362 expect_ice_restart_ = expect_restart;
363 }
364
365 bool ExpectIceRestart() const { return expect_ice_restart_; }
366
367 void SetReceiveAudioVideo(bool audio, bool video) {
368 SetReceiveAudio(audio);
369 SetReceiveVideo(video);
370 ASSERT_EQ(audio, can_receive_audio());
371 ASSERT_EQ(video, can_receive_video());
372 }
373
374 void SetReceiveAudio(bool audio) {
375 if (audio && can_receive_audio())
376 return;
htaaac2dea2016-03-10 13:35:55 -0800377 offer_answer_constraints_.SetMandatoryReceiveAudio(audio);
378 offer_answer_options_.offer_to_receive_audio = audio ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700379 }
380
381 void SetReceiveVideo(bool video) {
382 if (video && can_receive_video())
383 return;
htaaac2dea2016-03-10 13:35:55 -0800384 offer_answer_constraints_.SetMandatoryReceiveVideo(video);
385 offer_answer_options_.offer_to_receive_video = video ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700386 }
387
388 void RemoveMsidFromReceivedSdp(bool remove) { remove_msid_ = remove; }
389
390 void RemoveSdesCryptoFromReceivedSdp(bool remove) { remove_sdes_ = remove; }
391
392 void RemoveBundleFromReceivedSdp(bool remove) { remove_bundle_ = remove; }
393
perkjcaafdba2016-03-20 07:34:29 -0700394 void RemoveCvoFromReceivedSdp(bool remove) { remove_cvo_ = remove; }
395
deadbeefaf1b59c2015-10-15 12:08:41 -0700396 bool can_receive_audio() {
397 bool value;
htaaac2dea2016-03-10 13:35:55 -0800398 if (prefer_constraint_apis_) {
399 if (webrtc::FindConstraint(
400 &offer_answer_constraints_,
401 MediaConstraintsInterface::kOfferToReceiveAudio, &value,
402 nullptr)) {
403 return value;
404 }
405 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700406 }
htaaac2dea2016-03-10 13:35:55 -0800407 return offer_answer_options_.offer_to_receive_audio > 0 ||
408 offer_answer_options_.offer_to_receive_audio ==
409 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700410 }
411
412 bool can_receive_video() {
413 bool value;
htaaac2dea2016-03-10 13:35:55 -0800414 if (prefer_constraint_apis_) {
415 if (webrtc::FindConstraint(
416 &offer_answer_constraints_,
417 MediaConstraintsInterface::kOfferToReceiveVideo, &value,
418 nullptr)) {
419 return value;
420 }
421 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700422 }
htaaac2dea2016-03-10 13:35:55 -0800423 return offer_answer_options_.offer_to_receive_video > 0 ||
424 offer_answer_options_.offer_to_receive_video ==
425 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700426 }
427
deadbeefaf1b59c2015-10-15 12:08:41 -0700428 void OnDataChannel(DataChannelInterface* data_channel) override {
429 LOG(INFO) << id_ << "OnDataChannel";
430 data_channel_ = data_channel;
431 data_observer_.reset(new MockDataChannelObserver(data_channel));
432 }
433
434 void CreateDataChannel() {
435 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, nullptr);
436 ASSERT_TRUE(data_channel_.get() != nullptr);
437 data_observer_.reset(new MockDataChannelObserver(data_channel_));
438 }
439
deadbeeffac06552015-11-25 11:26:01 -0800440 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack(
441 const std::string& stream_label) {
442 FakeConstraints constraints;
443 // Disable highpass filter so that we can get all the test audio frames.
444 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
445 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
446 peer_connection_factory_->CreateAudioSource(&constraints);
447 // TODO(perkj): Test audio source when it is implemented. Currently audio
448 // always use the default input.
449 std::string label = stream_label + kAudioTrackLabelBase;
450 return peer_connection_factory_->CreateAudioTrack(label, source);
451 }
452
453 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack(
454 const std::string& stream_label) {
455 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
456 FakeConstraints source_constraints = video_constraints_;
457 source_constraints.SetMandatoryMaxFrameRate(10);
458
459 cricket::FakeVideoCapturer* fake_capturer =
460 new webrtc::FakePeriodicVideoCapturer();
perkjcaafdba2016-03-20 07:34:29 -0700461 fake_capturer->SetRotation(capture_rotation_);
deadbeeffac06552015-11-25 11:26:01 -0800462 video_capturers_.push_back(fake_capturer);
perkja3ede6c2016-03-08 01:27:48 +0100463 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
deadbeeffac06552015-11-25 11:26:01 -0800464 peer_connection_factory_->CreateVideoSource(fake_capturer,
465 &source_constraints);
466 std::string label = stream_label + kVideoTrackLabelBase;
perkjcaafdba2016-03-20 07:34:29 -0700467
468 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
469 peer_connection_factory_->CreateVideoTrack(label, source));
470 if (!local_video_renderer_) {
471 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
472 }
473 return track;
deadbeeffac06552015-11-25 11:26:01 -0800474 }
475
deadbeefaf1b59c2015-10-15 12:08:41 -0700476 DataChannelInterface* data_channel() { return data_channel_; }
477 const MockDataChannelObserver* data_observer() const {
478 return data_observer_.get();
479 }
480
hta6b4f8392016-03-10 00:24:31 -0800481 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
deadbeefaf1b59c2015-10-15 12:08:41 -0700482
483 void StopVideoCapturers() {
perkjcaafdba2016-03-20 07:34:29 -0700484 for (auto* capturer : video_capturers_) {
485 capturer->Stop();
deadbeefaf1b59c2015-10-15 12:08:41 -0700486 }
487 }
488
perkjcaafdba2016-03-20 07:34:29 -0700489 void SetCaptureRotation(webrtc::VideoRotation rotation) {
490 ASSERT_TRUE(video_capturers_.empty());
491 capture_rotation_ = rotation;
492 }
493
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 bool AudioFramesReceivedCheck(int number_of_frames) const {
495 return number_of_frames <= fake_audio_capture_module_->frames_received();
496 }
497
deadbeefc9be0072015-12-14 18:27:57 -0800498 int audio_frames_received() const {
499 return fake_audio_capture_module_->frames_received();
500 }
501
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 bool VideoFramesReceivedCheck(int number_of_frames) {
503 if (video_decoder_factory_enabled_) {
504 const std::vector<FakeWebRtcVideoDecoder*>& decoders
505 = fake_video_decoder_factory_->decoders();
506 if (decoders.empty()) {
507 return number_of_frames <= 0;
508 }
hta6b4f8392016-03-10 00:24:31 -0800509 // Note - this checks that EACH decoder has the requisite number
510 // of frames. The video_frames_received() function sums them.
deadbeefc9be0072015-12-14 18:27:57 -0800511 for (FakeWebRtcVideoDecoder* decoder : decoders) {
512 if (number_of_frames > decoder->GetNumFramesReceived()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 return false;
514 }
515 }
516 return true;
517 } else {
518 if (fake_video_renderers_.empty()) {
519 return number_of_frames <= 0;
520 }
521
deadbeefc9be0072015-12-14 18:27:57 -0800522 for (const auto& pair : fake_video_renderers_) {
523 if (number_of_frames > pair.second->num_rendered_frames()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 return false;
525 }
526 }
527 return true;
528 }
529 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700530
deadbeefc9be0072015-12-14 18:27:57 -0800531 int video_frames_received() const {
532 int total = 0;
533 if (video_decoder_factory_enabled_) {
534 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
535 fake_video_decoder_factory_->decoders();
536 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
537 total += decoder->GetNumFramesReceived();
538 }
539 } else {
540 for (const auto& pair : fake_video_renderers_) {
541 total += pair.second->num_rendered_frames();
542 }
543 for (const auto& renderer : removed_fake_video_renderers_) {
544 total += renderer->num_rendered_frames();
545 }
546 }
547 return total;
548 }
549
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 // Verify the CreateDtmfSender interface
551 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000552 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
553 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554
555 // We can't create a DTMF sender with an invalid audio track or a non local
556 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700557 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000558 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700559 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
560 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561
562 // We should be able to create a DTMF sender from a local track.
563 webrtc::AudioTrackInterface* localtrack =
564 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
565 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700566 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 dtmf_sender->RegisterObserver(observer.get());
568
569 // Test the DtmfSender object just created.
570 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
571 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
572
573 // We don't need to verify that the DTMF tones are actually sent out because
574 // that is already covered by the tests of the lower level components.
575
576 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
577 std::vector<std::string> tones;
578 tones.push_back("1");
579 tones.push_back("a");
580 tones.push_back("");
581 observer->Verify(tones);
582
583 dtmf_sender->UnregisterObserver();
584 }
585
586 // Verifies that the SessionDescription have rejected the appropriate media
587 // content.
588 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700589 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
590 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 const cricket::SessionDescription* remote_desc =
592 peer_connection_->remote_description()->description();
593 const cricket::SessionDescription* local_desc =
594 peer_connection_->local_description()->description();
595
596 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
597 if (remote_audio_content) {
598 const ContentInfo* audio_content =
599 GetFirstAudioContent(local_desc);
600 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
601 }
602
603 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
604 if (remote_video_content) {
605 const ContentInfo* video_content =
606 GetFirstVideoContent(local_desc);
607 EXPECT_EQ(can_receive_video(), !video_content->rejected);
608 }
609 }
610
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700612 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613 const cricket::SessionDescription* desc =
614 peer_connection_->local_description()->description();
615 const cricket::ContentInfos& contents = desc->contents();
616
617 for (size_t index = 0; index < contents.size(); ++index) {
618 if (contents[index].rejected)
619 continue;
620 const cricket::TransportDescription* transport_desc =
621 desc->GetTransportDescriptionByName(contents[index].name);
622
623 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000624 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000625 if (ufragpair_it == ice_ufrag_pwd_.end()) {
626 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000627 ice_ufrag_pwd_[static_cast<int>(index)] =
628 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 } else if (ExpectIceRestart()) {
630 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
631 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
632 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
633 } else {
634 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
635 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
636 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
637 }
638 }
639 }
640
641 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000642 rtc::scoped_refptr<MockStatsObserver>
643 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000644 EXPECT_TRUE(peer_connection_->GetStats(
645 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700647 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648 return observer->AudioOutputLevel();
649 }
650
651 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000652 rtc::scoped_refptr<MockStatsObserver>
653 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000654 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700655 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000656 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700657 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000658 return observer->AudioInputLevel();
659 }
660
661 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000662 rtc::scoped_refptr<MockStatsObserver>
663 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000664 EXPECT_TRUE(peer_connection_->GetStats(
665 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000666 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700667 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 return observer->BytesReceived();
669 }
670
671 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000672 rtc::scoped_refptr<MockStatsObserver>
673 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000674 EXPECT_TRUE(peer_connection_->GetStats(
675 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700677 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678 return observer->BytesSent();
679 }
680
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000681 int GetAvailableReceivedBandwidthStats() {
682 rtc::scoped_refptr<MockStatsObserver>
683 observer(new rtc::RefCountedObject<MockStatsObserver>());
684 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700685 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000686 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700687 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000688 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000689 return bw;
690 }
691
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000692 std::string GetDtlsCipherStats() {
693 rtc::scoped_refptr<MockStatsObserver>
694 observer(new rtc::RefCountedObject<MockStatsObserver>());
695 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700696 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000697 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700698 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000699 return observer->DtlsCipher();
700 }
701
702 std::string GetSrtpCipherStats() {
703 rtc::scoped_refptr<MockStatsObserver>
704 observer(new rtc::RefCountedObject<MockStatsObserver>());
705 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700706 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000707 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700708 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000709 return observer->SrtpCipher();
710 }
711
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 int rendered_width() {
713 EXPECT_FALSE(fake_video_renderers_.empty());
714 return fake_video_renderers_.empty() ? 1 :
715 fake_video_renderers_.begin()->second->width();
716 }
717
718 int rendered_height() {
719 EXPECT_FALSE(fake_video_renderers_.empty());
720 return fake_video_renderers_.empty() ? 1 :
721 fake_video_renderers_.begin()->second->height();
722 }
723
perkjcaafdba2016-03-20 07:34:29 -0700724 webrtc::VideoRotation rendered_rotation() {
725 EXPECT_FALSE(fake_video_renderers_.empty());
726 return fake_video_renderers_.empty()
727 ? webrtc::kVideoRotation_0
728 : fake_video_renderers_.begin()->second->rotation();
729 }
730
731 int local_rendered_width() {
732 return local_video_renderer_ ? local_video_renderer_->width() : 1;
733 }
734
735 int local_rendered_height() {
736 return local_video_renderer_ ? local_video_renderer_->height() : 1;
737 }
738
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000739 size_t number_of_remote_streams() {
740 if (!pc())
741 return 0;
742 return pc()->remote_streams()->count();
743 }
744
hta6b4f8392016-03-10 00:24:31 -0800745 StreamCollectionInterface* remote_streams() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 if (!pc()) {
747 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700748 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 }
750 return pc()->remote_streams();
751 }
752
753 StreamCollectionInterface* local_streams() {
754 if (!pc()) {
755 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700756 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 }
758 return pc()->local_streams();
759 }
760
hta6b4f8392016-03-10 00:24:31 -0800761 bool HasLocalAudioTrack() { return StreamsHaveAudioTrack(local_streams()); }
762
763 bool HasLocalVideoTrack() { return StreamsHaveVideoTrack(local_streams()); }
764
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
766 return pc()->signaling_state();
767 }
768
769 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
770 return pc()->ice_connection_state();
771 }
772
773 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
774 return pc()->ice_gathering_state();
775 }
776
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 private:
778 class DummyDtmfObserver : public DtmfSenderObserverInterface {
779 public:
780 DummyDtmfObserver() : completed_(false) {}
781
782 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700783 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 tones_.push_back(tone);
785 if (tone.empty()) {
786 completed_ = true;
787 }
788 }
789
790 void Verify(const std::vector<std::string>& tones) const {
791 ASSERT_TRUE(tones_.size() == tones.size());
792 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
793 }
794
795 bool completed() const { return completed_; }
796
797 private:
798 bool completed_;
799 std::vector<std::string> tones_;
800 };
801
deadbeefaf1b59c2015-10-15 12:08:41 -0700802 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
803
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800804 bool Init(
805 const MediaConstraintsInterface* constraints,
806 const PeerConnectionFactory::Options* options,
htaaac2dea2016-03-10 13:35:55 -0800807 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store,
perkj8aba9972016-04-10 23:54:34 -0700808 bool prefer_constraint_apis,
809 rtc::Thread* worker_thread) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700810 EXPECT_TRUE(!peer_connection_);
811 EXPECT_TRUE(!peer_connection_factory_);
htaaac2dea2016-03-10 13:35:55 -0800812 if (!prefer_constraint_apis) {
813 EXPECT_TRUE(!constraints);
814 }
815 prefer_constraint_apis_ = prefer_constraint_apis;
816
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800817 rtc::scoped_ptr<cricket::PortAllocator> port_allocator(
perkj8aba9972016-04-10 23:54:34 -0700818 new cricket::FakePortAllocator(worker_thread, nullptr));
deadbeefaf1b59c2015-10-15 12:08:41 -0700819 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
820
821 if (fake_audio_capture_module_ == nullptr) {
822 return false;
823 }
824 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
825 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
826 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
perkj8aba9972016-04-10 23:54:34 -0700827 worker_thread, rtc::Thread::Current(), fake_audio_capture_module_,
828 fake_video_encoder_factory_, fake_video_decoder_factory_);
deadbeefaf1b59c2015-10-15 12:08:41 -0700829 if (!peer_connection_factory_) {
830 return false;
831 }
832 if (options) {
833 peer_connection_factory_->SetOptions(*options);
834 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800835 peer_connection_ = CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800836 std::move(port_allocator), constraints, std::move(dtls_identity_store));
deadbeefaf1b59c2015-10-15 12:08:41 -0700837 return peer_connection_.get() != nullptr;
838 }
839
deadbeefaf1b59c2015-10-15 12:08:41 -0700840 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800841 rtc::scoped_ptr<cricket::PortAllocator> port_allocator,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800842 const MediaConstraintsInterface* constraints,
843 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800844 // CreatePeerConnection with RTCConfiguration.
845 webrtc::PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 webrtc::PeerConnectionInterface::IceServer ice_server;
847 ice_server.uri = "stun:stun.l.google.com:19302";
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800848 config.servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000849
deadbeefaf1b59c2015-10-15 12:08:41 -0700850 return peer_connection_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800851 config, constraints, std::move(port_allocator),
852 std::move(dtls_identity_store), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 }
854
855 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700856 LOG(INFO) << id_ << "HandleIncomingOffer ";
deadbeeffaac4972015-11-12 15:33:07 -0800857 if (NumberOfLocalMediaStreams() == 0 && auto_add_stream_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 // If we are not sending any streams ourselves it is time to add some.
859 AddMediaStream(true, true);
860 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000861 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700862 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000864 rtc::scoped_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700865 EXPECT_TRUE(DoCreateAnswer(&answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866 std::string sdp;
867 EXPECT_TRUE(answer->ToString(&sdp));
868 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
deadbeefaf1b59c2015-10-15 12:08:41 -0700869 if (signaling_message_receiver_) {
870 signaling_message_receiver_->ReceiveSdpMessage(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871 webrtc::SessionDescriptionInterface::kAnswer, sdp);
872 }
873 }
874
875 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700876 LOG(INFO) << id_ << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000877 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700878 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
880 }
881
kwiberg2bbff992016-03-16 11:03:04 -0700882 bool DoCreateOfferAnswer(rtc::scoped_ptr<SessionDescriptionInterface>* desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000884 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
885 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886 MockCreateSessionDescriptionObserver>());
htaaac2dea2016-03-10 13:35:55 -0800887 if (prefer_constraint_apis_) {
888 if (offer) {
889 pc()->CreateOffer(observer, &offer_answer_constraints_);
890 } else {
891 pc()->CreateAnswer(observer, &offer_answer_constraints_);
892 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 } else {
htaaac2dea2016-03-10 13:35:55 -0800894 if (offer) {
895 pc()->CreateOffer(observer, offer_answer_options_);
896 } else {
897 pc()->CreateAnswer(observer, offer_answer_options_);
898 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899 }
900 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
kwiberg2bbff992016-03-16 11:03:04 -0700901 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 if (observer->result() && ExpectIceRestart()) {
903 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
904 }
905 return observer->result();
906 }
907
kwiberg2bbff992016-03-16 11:03:04 -0700908 bool DoCreateOffer(rtc::scoped_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 return DoCreateOfferAnswer(desc, true);
910 }
911
kwiberg2bbff992016-03-16 11:03:04 -0700912 bool DoCreateAnswer(rtc::scoped_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 return DoCreateOfferAnswer(desc, false);
914 }
915
916 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000917 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
918 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700920 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 pc()->SetLocalDescription(observer, desc);
922 // Ignore the observer result. If we wait for the result with
923 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
924 // before the offer which is an error.
925 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000926 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 // ProcessMessages waits at least 1ms but processes all messages before
928 // returning. Since this test is synchronous and send messages to the remote
929 // peer whenever a callback is invoked, this can lead to messages being
930 // sent to the remote peer in the wrong order.
931 // TODO(perkj): Find a way to check the result without risking that the
932 // order of sent messages are changed. Ex- by posting all messages that are
933 // sent to the remote peer.
934 return true;
935 }
936
937 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000938 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
939 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700941 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942 pc()->SetRemoteDescription(observer, desc);
943 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
944 return observer->result();
945 }
946
947 // This modifies all received SDP messages before they are processed.
948 void FilterIncomingSdpMessage(std::string* sdp) {
949 if (remove_msid_) {
950 const char kSdpSsrcAttribute[] = "a=ssrc:";
951 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
952 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
953 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
954 }
955 if (remove_bundle_) {
956 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
957 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
958 }
959 if (remove_sdes_) {
960 const char kSdpSdesCryptoAttribute[] = "a=crypto";
961 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
962 }
perkjcaafdba2016-03-20 07:34:29 -0700963 if (remove_cvo_) {
964 const char kSdpCvoExtenstion[] = "urn:3gpp:video-orientation";
965 RemoveLinesFromSdp(kSdpCvoExtenstion, sdp);
966 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000967 }
968
deadbeefaf1b59c2015-10-15 12:08:41 -0700969 std::string id_;
970
deadbeefaf1b59c2015-10-15 12:08:41 -0700971 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
972 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
973 peer_connection_factory_;
974
htaaac2dea2016-03-10 13:35:55 -0800975 bool prefer_constraint_apis_ = true;
deadbeeffaac4972015-11-12 15:33:07 -0800976 bool auto_add_stream_ = true;
977
deadbeefaf1b59c2015-10-15 12:08:41 -0700978 typedef std::pair<std::string, std::string> IceUfragPwdPair;
979 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
980 bool expect_ice_restart_ = false;
981
deadbeefc9be0072015-12-14 18:27:57 -0800982 // Needed to keep track of number of frames sent.
deadbeefaf1b59c2015-10-15 12:08:41 -0700983 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
984 // Needed to keep track of number of frames received.
deadbeefc9be0072015-12-14 18:27:57 -0800985 std::map<std::string, rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
986 fake_video_renderers_;
987 // Needed to ensure frames aren't received for removed tracks.
988 std::vector<rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
989 removed_fake_video_renderers_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700990 // Needed to keep track of number of frames received when external decoder
991 // used.
992 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
993 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
994 bool video_decoder_factory_enabled_ = false;
995 webrtc::FakeConstraints video_constraints_;
996
997 // For remote peer communication.
998 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
999
1000 // Store references to the video capturers we've created, so that we can stop
1001 // them, if required.
perkjcaafdba2016-03-20 07:34:29 -07001002 std::vector<cricket::FakeVideoCapturer*> video_capturers_;
1003 webrtc::VideoRotation capture_rotation_ = webrtc::kVideoRotation_0;
1004 // |local_video_renderer_| attached to the first created local video track.
1005 rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001006
htaaac2dea2016-03-10 13:35:55 -08001007 webrtc::FakeConstraints offer_answer_constraints_;
1008 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001009 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
1010 bool remove_bundle_ =
1011 false; // True if bundle should be removed in received SDP.
1012 bool remove_sdes_ =
1013 false; // True if a=crypto should be removed in received SDP.
perkjcaafdba2016-03-20 07:34:29 -07001014 // |remove_cvo_| is true if extension urn:3gpp:video-orientation should be
1015 // removed in the received SDP.
1016 bool remove_cvo_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001017
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001018 rtc::scoped_refptr<DataChannelInterface> data_channel_;
1019 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020};
1021
deadbeef7c73bdb2015-12-10 15:10:44 -08001022class P2PTestConductor : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023 public:
deadbeef7c73bdb2015-12-10 15:10:44 -08001024 P2PTestConductor()
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001025 : pss_(new rtc::PhysicalSocketServer),
1026 ss_(new rtc::VirtualSocketServer(pss_.get())),
perkj8aba9972016-04-10 23:54:34 -07001027 ss_scope_(ss_.get()) {
1028 RTC_CHECK(worker_thread_.Start());
1029 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001030
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031 bool SessionActive() {
1032 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001033 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001035
hta6b4f8392016-03-10 00:24:31 -08001036 // Return true if the number of frames provided have been received
1037 // on the video and audio tracks provided.
1038 bool FramesHaveArrived(int audio_frames_to_receive,
1039 int video_frames_to_receive) {
1040 bool all_good = true;
1041 if (initiating_client_->HasLocalAudioTrack() &&
1042 receiving_client_->can_receive_audio()) {
1043 all_good &=
1044 receiving_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1045 }
1046 if (initiating_client_->HasLocalVideoTrack() &&
1047 receiving_client_->can_receive_video()) {
1048 all_good &=
1049 receiving_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1050 }
1051 if (receiving_client_->HasLocalAudioTrack() &&
1052 initiating_client_->can_receive_audio()) {
1053 all_good &=
1054 initiating_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1055 }
1056 if (receiving_client_->HasLocalVideoTrack() &&
1057 initiating_client_->can_receive_video()) {
1058 all_good &=
1059 initiating_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1060 }
1061 return all_good;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001062 }
hta6b4f8392016-03-10 00:24:31 -08001063
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001064 void VerifyDtmf() {
1065 initiating_client_->VerifyDtmf();
1066 receiving_client_->VerifyDtmf();
1067 }
1068
1069 void TestUpdateOfferWithRejectedContent() {
deadbeefc9be0072015-12-14 18:27:57 -08001070 // Renegotiate, rejecting the video m-line.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071 initiating_client_->Negotiate(true, false);
deadbeefc9be0072015-12-14 18:27:57 -08001072 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1073
1074 int pc1_audio_received = initiating_client_->audio_frames_received();
1075 int pc1_video_received = initiating_client_->video_frames_received();
1076 int pc2_audio_received = receiving_client_->audio_frames_received();
1077 int pc2_video_received = receiving_client_->video_frames_received();
1078
1079 // Wait for some additional audio frames to be received.
1080 EXPECT_TRUE_WAIT(initiating_client_->AudioFramesReceivedCheck(
1081 pc1_audio_received + kEndAudioFrameCount) &&
1082 receiving_client_->AudioFramesReceivedCheck(
1083 pc2_audio_received + kEndAudioFrameCount),
1084 kMaxWaitForFramesMs);
1085
1086 // During this time, we shouldn't have received any additional video frames
1087 // for the rejected video tracks.
1088 EXPECT_EQ(pc1_video_received, initiating_client_->video_frames_received());
1089 EXPECT_EQ(pc2_video_received, receiving_client_->video_frames_received());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001090 }
1091
1092 void VerifyRenderedSize(int width, int height) {
perkjcaafdba2016-03-20 07:34:29 -07001093 VerifyRenderedSize(width, height, webrtc::kVideoRotation_0);
1094 }
1095
1096 void VerifyRenderedSize(int width,
1097 int height,
1098 webrtc::VideoRotation rotation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099 EXPECT_EQ(width, receiving_client()->rendered_width());
1100 EXPECT_EQ(height, receiving_client()->rendered_height());
perkjcaafdba2016-03-20 07:34:29 -07001101 EXPECT_EQ(rotation, receiving_client()->rendered_rotation());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001102 EXPECT_EQ(width, initializing_client()->rendered_width());
1103 EXPECT_EQ(height, initializing_client()->rendered_height());
perkjcaafdba2016-03-20 07:34:29 -07001104 EXPECT_EQ(rotation, initializing_client()->rendered_rotation());
1105
1106 // Verify size of the local preview.
1107 EXPECT_EQ(width, initializing_client()->local_rendered_width());
1108 EXPECT_EQ(height, initializing_client()->local_rendered_height());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001109 }
1110
1111 void VerifySessionDescriptions() {
1112 initiating_client_->VerifyRejectedMediaInSessionDescription();
1113 receiving_client_->VerifyRejectedMediaInSessionDescription();
1114 initiating_client_->VerifyLocalIceUfragAndPassword();
1115 receiving_client_->VerifyLocalIceUfragAndPassword();
1116 }
1117
deadbeef7c73bdb2015-12-10 15:10:44 -08001118 ~P2PTestConductor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001119 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001120 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001121 }
1122 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001123 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001124 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001125 }
1126
deadbeefaf1b59c2015-10-15 12:08:41 -07001127 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128
1129 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1130 MediaConstraintsInterface* recv_constraints) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001131 return CreateTestClients(init_constraints, nullptr, recv_constraints,
1132 nullptr);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001133 }
1134
htaaac2dea2016-03-10 13:35:55 -08001135 bool CreateTestClientsThatPreferNoConstraints() {
1136 initiating_client_.reset(
perkj8aba9972016-04-10 23:54:34 -07001137 PeerConnectionTestClient::CreateClientPreferNoConstraints(
1138 "Caller: ", nullptr, &worker_thread_));
htaaac2dea2016-03-10 13:35:55 -08001139 receiving_client_.reset(
perkj8aba9972016-04-10 23:54:34 -07001140 PeerConnectionTestClient::CreateClientPreferNoConstraints(
1141 "Callee: ", nullptr, &worker_thread_));
htaaac2dea2016-03-10 13:35:55 -08001142 if (!initiating_client_ || !receiving_client_) {
1143 return false;
1144 }
1145 // Remember the choice for possible later resets of the clients.
1146 prefer_constraint_apis_ = false;
1147 SetSignalingReceivers();
1148 return true;
1149 }
1150
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001151 void SetSignalingReceivers() {
1152 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
1153 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1154 }
1155
Joachim Bauch04e5b492015-05-29 09:40:39 +02001156 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1157 PeerConnectionFactory::Options* init_options,
1158 MediaConstraintsInterface* recv_constraints,
1159 PeerConnectionFactory::Options* recv_options) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001160 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
perkj8aba9972016-04-10 23:54:34 -07001161 "Caller: ", init_constraints, init_options, &worker_thread_));
deadbeefaf1b59c2015-10-15 12:08:41 -07001162 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
perkj8aba9972016-04-10 23:54:34 -07001163 "Callee: ", recv_constraints, recv_options, &worker_thread_));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001164 if (!initiating_client_ || !receiving_client_) {
1165 return false;
1166 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001167 SetSignalingReceivers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001168 return true;
1169 }
1170
1171 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1172 const webrtc::FakeConstraints& recv_constraints) {
1173 initiating_client_->SetVideoConstraints(init_constraints);
1174 receiving_client_->SetVideoConstraints(recv_constraints);
1175 }
1176
perkjcaafdba2016-03-20 07:34:29 -07001177 void SetCaptureRotation(webrtc::VideoRotation rotation) {
1178 initiating_client_->SetCaptureRotation(rotation);
1179 receiving_client_->SetCaptureRotation(rotation);
1180 }
1181
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182 void EnableVideoDecoderFactory() {
1183 initiating_client_->EnableVideoDecoderFactory();
1184 receiving_client_->EnableVideoDecoderFactory();
1185 }
1186
1187 // This test sets up a call between two parties. Both parties send static
1188 // frames to each other. Once the test is finished the number of sent frames
1189 // is compared to the number of received frames.
perkj8aba9972016-04-10 23:54:34 -07001190 void LocalP2PTest() { LocalP2PTest(false); }
1191 // TODO(perkj); Remove the flag bug5752 when
1192 // https://bugs.chromium.org/p/webrtc/issues/detail?id=5752 is fixed.
1193 void LocalP2PTest(bool bug5752) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1195 initiating_client_->AddMediaStream(true, true);
1196 }
1197 initiating_client_->Negotiate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198 // Assert true is used here since next tests are guaranteed to fail and
1199 // would eat up 5 seconds.
1200 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1201 VerifySessionDescriptions();
1202
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203 int audio_frame_count = kEndAudioFrameCount;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204 int video_frame_count = kEndVideoFrameCount;
hta6b4f8392016-03-10 00:24:31 -08001205 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1206
1207 if ((!initiating_client_->can_receive_audio() &&
1208 !initiating_client_->can_receive_video()) ||
1209 (!receiving_client_->can_receive_audio() &&
1210 !receiving_client_->can_receive_video())) {
1211 // Neither audio nor video will flow, so connections won't be
1212 // established. There's nothing more to check.
1213 // TODO(hta): Check connection if there's a data channel.
1214 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215 }
1216
hta6b4f8392016-03-10 00:24:31 -08001217 // Audio or video is expected to flow, so both clients should reach the
1218 // Connected state, and the offerer (ICE controller) should proceed to
1219 // Completed.
1220 // Note: These tests have been observed to fail under heavy load at
1221 // shorter timeouts, so they may be flaky.
perkj8aba9972016-04-10 23:54:34 -07001222 if (bug5752) {
1223 EXPECT_TRUE_WAIT(
1224 initiating_client_->ice_connection_state() ==
1225 webrtc::PeerConnectionInterface::kIceConnectionCompleted ||
1226 initiating_client_->ice_connection_state() ==
1227 webrtc::PeerConnectionInterface::kIceConnectionConnected,
1228 kMaxWaitForFramesMs);
1229 } else {
1230 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1231 initiating_client_->ice_connection_state(),
1232 kMaxWaitForFramesMs);
1233 }
hta6b4f8392016-03-10 00:24:31 -08001234 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1235 receiving_client_->ice_connection_state(),
1236 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237
hta6b4f8392016-03-10 00:24:31 -08001238 // The ICE gathering state should end up in kIceGatheringComplete,
1239 // but there's a bug that prevents this at the moment, and the state
1240 // machine is being updated by the WEBRTC WG.
1241 // TODO(hta): Update this check when spec revisions finish.
1242 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1243 initiating_client_->ice_gathering_state());
1244 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1245 receiving_client_->ice_gathering_state(),
1246 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001247
hta6b4f8392016-03-10 00:24:31 -08001248 // Check that the expected number of frames have arrived.
1249 EXPECT_TRUE_WAIT(FramesHaveArrived(audio_frame_count, video_frame_count),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001250 kMaxWaitForFramesMs);
1251 }
1252
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001253 void SetupAndVerifyDtlsCall() {
1254 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1255 FakeConstraints setup_constraints;
1256 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1257 true);
1258 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1259 LocalP2PTest();
1260 VerifyRenderedSize(640, 480);
1261 }
1262
1263 PeerConnectionTestClient* CreateDtlsClientWithAlternateKey() {
1264 FakeConstraints setup_constraints;
1265 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1266 true);
1267
1268 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
1269 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
1270 : nullptr);
1271 dtls_identity_store->use_alternate_key();
1272
1273 // Make sure the new client is using a different certificate.
1274 return PeerConnectionTestClient::CreateClientWithDtlsIdentityStore(
kwiberg0eb15ed2015-12-17 03:04:15 -08001275 "New Peer: ", &setup_constraints, nullptr,
perkj8aba9972016-04-10 23:54:34 -07001276 std::move(dtls_identity_store), prefer_constraint_apis_,
1277 &worker_thread_);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001278 }
1279
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001280 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1281 // Messages may get lost on the unreliable DataChannel, so we send multiple
1282 // times to avoid test flakiness.
1283 static const size_t kSendAttempts = 5;
1284
1285 for (size_t i = 0; i < kSendAttempts; ++i) {
1286 dc->Send(DataBuffer(data));
1287 }
1288 }
1289
deadbeefaf1b59c2015-10-15 12:08:41 -07001290 PeerConnectionTestClient* initializing_client() {
1291 return initiating_client_.get();
1292 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001293
1294 // Set the |initiating_client_| to the |client| passed in and return the
1295 // original |initiating_client_|.
1296 PeerConnectionTestClient* set_initializing_client(
1297 PeerConnectionTestClient* client) {
1298 PeerConnectionTestClient* old = initiating_client_.release();
1299 initiating_client_.reset(client);
1300 return old;
1301 }
1302
deadbeefaf1b59c2015-10-15 12:08:41 -07001303 PeerConnectionTestClient* receiving_client() {
1304 return receiving_client_.get();
1305 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001306
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001307 // Set the |receiving_client_| to the |client| passed in and return the
1308 // original |receiving_client_|.
1309 PeerConnectionTestClient* set_receiving_client(
1310 PeerConnectionTestClient* client) {
1311 PeerConnectionTestClient* old = receiving_client_.release();
1312 receiving_client_.reset(client);
1313 return old;
1314 }
1315
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001316 private:
perkj8aba9972016-04-10 23:54:34 -07001317 // |worker_thread_| is used by both |initiating_client_| and
1318 // |receiving_client_|. Must be destroyed last.
1319 rtc::Thread worker_thread_;
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001320 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1321 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1322 rtc::SocketServerScope ss_scope_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001323 rtc::scoped_ptr<PeerConnectionTestClient> initiating_client_;
1324 rtc::scoped_ptr<PeerConnectionTestClient> receiving_client_;
htaaac2dea2016-03-10 13:35:55 -08001325 bool prefer_constraint_apis_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001326};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001327
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001328// Disable for TSan v2, see
1329// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1330#if !defined(THREAD_SANITIZER)
1331
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001333// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1334// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001335TEST_F(P2PTestConductor, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001336 ASSERT_TRUE(CreateTestClients());
1337 LocalP2PTest();
1338 VerifyDtmf();
1339}
1340
1341// This test sets up a Jsep call between two parties and test that we can get a
1342// video aspect ratio of 16:9.
deadbeef7c73bdb2015-12-10 15:10:44 -08001343TEST_F(P2PTestConductor, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001344 ASSERT_TRUE(CreateTestClients());
1345 FakeConstraints constraint;
1346 double requested_ratio = 640.0/360;
1347 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1348 SetVideoConstraints(constraint, constraint);
1349 LocalP2PTest();
1350
1351 ASSERT_LE(0, initializing_client()->rendered_height());
1352 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001353 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001354 initializing_client()->rendered_height();
1355 EXPECT_LE(requested_ratio, initiating_video_ratio);
1356
1357 ASSERT_LE(0, receiving_client()->rendered_height());
1358 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001359 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001360 receiving_client()->rendered_height();
1361 EXPECT_LE(requested_ratio, receiving_video_ratio);
1362}
1363
1364// This test sets up a Jsep call between two parties and test that the
1365// received video has a resolution of 1280*720.
1366// TODO(mallinath): Enable when
1367// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
deadbeef7c73bdb2015-12-10 15:10:44 -08001368TEST_F(P2PTestConductor, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369 ASSERT_TRUE(CreateTestClients());
1370 FakeConstraints constraint;
1371 constraint.SetMandatoryMinWidth(1280);
1372 constraint.SetMandatoryMinHeight(720);
1373 SetVideoConstraints(constraint, constraint);
1374 LocalP2PTest();
1375 VerifyRenderedSize(1280, 720);
1376}
1377
1378// This test sets up a call between two endpoints that are configured to use
1379// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001380TEST_F(P2PTestConductor, LocalP2PTestDtls) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001381 SetupAndVerifyDtlsCall();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001382}
1383
hta6b4f8392016-03-10 00:24:31 -08001384// This test sets up an one-way call, with media only from initiator to
1385// responder.
1386TEST_F(P2PTestConductor, OneWayMediaCall) {
1387 ASSERT_TRUE(CreateTestClients());
1388 receiving_client()->set_auto_add_stream(false);
1389 LocalP2PTest();
1390}
1391
htaaac2dea2016-03-10 13:35:55 -08001392TEST_F(P2PTestConductor, OneWayMediaCallWithoutConstraints) {
1393 ASSERT_TRUE(CreateTestClientsThatPreferNoConstraints());
1394 receiving_client()->set_auto_add_stream(false);
1395 LocalP2PTest();
1396}
1397
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001398// This test sets up a audio call initially and then upgrades to audio/video,
1399// using DTLS.
deadbeef7c73bdb2015-12-10 15:10:44 -08001400TEST_F(P2PTestConductor, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001401 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001402 FakeConstraints setup_constraints;
1403 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1404 true);
1405 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1406 receiving_client()->SetReceiveAudioVideo(true, false);
1407 LocalP2PTest();
1408 receiving_client()->SetReceiveAudioVideo(true, true);
1409 receiving_client()->Negotiate();
1410}
1411
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001412// This test sets up a call transfer to a new caller with a different DTLS
1413// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001414TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCallee) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001415 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1416 SetupAndVerifyDtlsCall();
1417
1418 // Keeping the original peer around which will still send packets to the
1419 // receiving client. These SRTP packets will be dropped.
1420 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1421 set_initializing_client(CreateDtlsClientWithAlternateKey()));
1422 original_peer->pc()->Close();
1423
1424 SetSignalingReceivers();
1425 receiving_client()->SetExpectIceRestart(true);
1426 LocalP2PTest();
1427 VerifyRenderedSize(640, 480);
1428}
1429
guoweis46383312015-12-17 16:45:59 -08001430// This test sets up a non-bundle call and apply bundle during ICE restart. When
1431// bundle is in effect in the restart, the channel can successfully reset its
1432// DTLS-SRTP context.
1433TEST_F(P2PTestConductor, LocalP2PTestDtlsBundleInIceRestart) {
1434 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1435 FakeConstraints setup_constraints;
1436 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1437 true);
1438 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1439 receiving_client()->RemoveBundleFromReceivedSdp(true);
1440 LocalP2PTest();
1441 VerifyRenderedSize(640, 480);
1442
1443 initializing_client()->IceRestart();
1444 receiving_client()->SetExpectIceRestart(true);
1445 receiving_client()->RemoveBundleFromReceivedSdp(false);
1446 LocalP2PTest();
1447 VerifyRenderedSize(640, 480);
1448}
1449
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001450// This test sets up a call transfer to a new callee with a different DTLS
1451// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001452TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCaller) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001453 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1454 SetupAndVerifyDtlsCall();
1455
1456 // Keeping the original peer around which will still send packets to the
1457 // receiving client. These SRTP packets will be dropped.
1458 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1459 set_receiving_client(CreateDtlsClientWithAlternateKey()));
1460 original_peer->pc()->Close();
1461
1462 SetSignalingReceivers();
1463 initializing_client()->IceRestart();
perkj8aba9972016-04-10 23:54:34 -07001464 // TODO(perkj): Remove the flag bug5752 when
1465 // https://bugs.chromium.org/p/webrtc/issues/detail?id=5752 is fixed.
1466 LocalP2PTest(true /* bug5752 */);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001467 VerifyRenderedSize(640, 480);
1468}
1469
perkjcaafdba2016-03-20 07:34:29 -07001470TEST_F(P2PTestConductor, LocalP2PTestCVO) {
1471 ASSERT_TRUE(CreateTestClients());
1472 SetCaptureRotation(webrtc::kVideoRotation_90);
1473 LocalP2PTest();
1474 VerifyRenderedSize(640, 480, webrtc::kVideoRotation_90);
1475}
1476
1477TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportCVO) {
1478 ASSERT_TRUE(CreateTestClients());
1479 SetCaptureRotation(webrtc::kVideoRotation_90);
1480 receiving_client()->RemoveCvoFromReceivedSdp(true);
1481 LocalP2PTest();
1482 VerifyRenderedSize(480, 640, webrtc::kVideoRotation_0);
1483}
1484
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001485// This test sets up a call between two endpoints that are configured to use
1486// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1487// negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001488TEST_F(P2PTestConductor, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001489 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001490 FakeConstraints setup_constraints;
1491 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1492 true);
1493 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1494 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1495 LocalP2PTest();
1496 VerifyRenderedSize(640, 480);
1497}
1498
1499// This test sets up a Jsep call between two parties, and the callee only
1500// accept to receive video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001501TEST_F(P2PTestConductor, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001502 ASSERT_TRUE(CreateTestClients());
1503 receiving_client()->SetReceiveAudioVideo(false, true);
1504 LocalP2PTest();
1505}
1506
1507// This test sets up a Jsep call between two parties, and the callee only
1508// accept to receive audio.
deadbeef7c73bdb2015-12-10 15:10:44 -08001509TEST_F(P2PTestConductor, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001510 ASSERT_TRUE(CreateTestClients());
1511 receiving_client()->SetReceiveAudioVideo(true, false);
1512 LocalP2PTest();
1513}
1514
1515// This test sets up a Jsep call between two parties, and the callee reject both
1516// audio and video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001517TEST_F(P2PTestConductor, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001518 ASSERT_TRUE(CreateTestClients());
1519 receiving_client()->SetReceiveAudioVideo(false, false);
1520 LocalP2PTest();
1521}
1522
1523// This test sets up an audio and video call between two parties. After the call
1524// runs for a while (10 frames), the caller sends an update offer with video
1525// being rejected. Once the re-negotiation is done, the video flow should stop
1526// and the audio flow should continue.
deadbeefc9be0072015-12-14 18:27:57 -08001527TEST_F(P2PTestConductor, UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001528 ASSERT_TRUE(CreateTestClients());
1529 LocalP2PTest();
1530 TestUpdateOfferWithRejectedContent();
1531}
1532
1533// This test sets up a Jsep call between two parties. The MSID is removed from
1534// the SDP strings from the caller.
deadbeefc9be0072015-12-14 18:27:57 -08001535TEST_F(P2PTestConductor, LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001536 ASSERT_TRUE(CreateTestClients());
1537 receiving_client()->RemoveMsidFromReceivedSdp(true);
1538 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1539 // audio and video is muxed when MSID is disabled. Remove
1540 // SetRemoveBundleFromSdp once
1541 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1542 receiving_client()->RemoveBundleFromReceivedSdp(true);
1543 LocalP2PTest();
1544}
1545
1546// This test sets up a Jsep call between two parties and the initiating peer
1547// sends two steams.
1548// TODO(perkj): Disabled due to
1549// https://code.google.com/p/webrtc/issues/detail?id=1454
deadbeef7c73bdb2015-12-10 15:10:44 -08001550TEST_F(P2PTestConductor, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551 ASSERT_TRUE(CreateTestClients());
1552 // Set optional video constraint to max 320pixels to decrease CPU usage.
1553 FakeConstraints constraint;
1554 constraint.SetOptionalMaxWidth(320);
1555 SetVideoConstraints(constraint, constraint);
1556 initializing_client()->AddMediaStream(true, true);
1557 initializing_client()->AddMediaStream(false, true);
1558 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1559 LocalP2PTest();
1560 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1561}
1562
1563// Test that we can receive the audio output level from a remote audio track.
deadbeef7c73bdb2015-12-10 15:10:44 -08001564TEST_F(P2PTestConductor, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001565 ASSERT_TRUE(CreateTestClients());
1566 LocalP2PTest();
1567
1568 StreamCollectionInterface* remote_streams =
1569 initializing_client()->remote_streams();
1570 ASSERT_GT(remote_streams->count(), 0u);
1571 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1572 MediaStreamTrackInterface* remote_audio_track =
1573 remote_streams->at(0)->GetAudioTracks()[0];
1574
1575 // Get the audio output level stats. Note that the level is not available
1576 // until a RTCP packet has been received.
1577 EXPECT_TRUE_WAIT(
1578 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1579 kMaxWaitForStatsMs);
1580}
1581
1582// Test that an audio input level is reported.
deadbeef7c73bdb2015-12-10 15:10:44 -08001583TEST_F(P2PTestConductor, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584 ASSERT_TRUE(CreateTestClients());
1585 LocalP2PTest();
1586
1587 // Get the audio input level stats. The level should be available very
1588 // soon after the test starts.
1589 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1590 kMaxWaitForStatsMs);
1591}
1592
1593// Test that we can get incoming byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001594TEST_F(P2PTestConductor, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595 ASSERT_TRUE(CreateTestClients());
1596 LocalP2PTest();
1597
1598 StreamCollectionInterface* remote_streams =
1599 initializing_client()->remote_streams();
1600 ASSERT_GT(remote_streams->count(), 0u);
1601 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1602 MediaStreamTrackInterface* remote_audio_track =
1603 remote_streams->at(0)->GetAudioTracks()[0];
1604 EXPECT_TRUE_WAIT(
1605 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1606 kMaxWaitForStatsMs);
1607
1608 MediaStreamTrackInterface* remote_video_track =
1609 remote_streams->at(0)->GetVideoTracks()[0];
1610 EXPECT_TRUE_WAIT(
1611 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1612 kMaxWaitForStatsMs);
1613}
1614
1615// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001616TEST_F(P2PTestConductor, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001617 ASSERT_TRUE(CreateTestClients());
1618 LocalP2PTest();
1619
1620 StreamCollectionInterface* local_streams =
1621 initializing_client()->local_streams();
1622 ASSERT_GT(local_streams->count(), 0u);
1623 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1624 MediaStreamTrackInterface* local_audio_track =
1625 local_streams->at(0)->GetAudioTracks()[0];
1626 EXPECT_TRUE_WAIT(
1627 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1628 kMaxWaitForStatsMs);
1629
1630 MediaStreamTrackInterface* local_video_track =
1631 local_streams->at(0)->GetVideoTracks()[0];
1632 EXPECT_TRUE_WAIT(
1633 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1634 kMaxWaitForStatsMs);
1635}
1636
Joachim Bauch04e5b492015-05-29 09:40:39 +02001637// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001638TEST_F(P2PTestConductor, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001639 PeerConnectionFactory::Options init_options;
1640 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1641 PeerConnectionFactory::Options recv_options;
1642 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001643 ASSERT_TRUE(
1644 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001645 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1646 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1647 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001648 LocalP2PTest();
1649
torbjorng43166b82016-03-11 00:06:47 -08001650 EXPECT_TRUE_WAIT(
1651 rtc::SSLStreamAdapter::IsAcceptableCipher(
1652 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1653 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001654 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001655 initializing_client()->GetSrtpCipherStats(),
1656 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001657 EXPECT_EQ(1,
1658 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1659 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001660}
1661
1662// Test that DTLS 1.2 is used if both ends support it.
torbjorng79a5a832016-01-15 07:16:51 -08001663TEST_F(P2PTestConductor, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001664 PeerConnectionFactory::Options init_options;
1665 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1666 PeerConnectionFactory::Options recv_options;
1667 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001668 ASSERT_TRUE(
1669 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001670 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1671 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1672 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001673 LocalP2PTest();
1674
torbjorng43166b82016-03-11 00:06:47 -08001675 EXPECT_TRUE_WAIT(
1676 rtc::SSLStreamAdapter::IsAcceptableCipher(
1677 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1678 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001679 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001680 initializing_client()->GetSrtpCipherStats(),
1681 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001682 EXPECT_EQ(1,
1683 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1684 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001685}
1686
1687// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1688// received supports 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001689TEST_F(P2PTestConductor, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001690 PeerConnectionFactory::Options init_options;
1691 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1692 PeerConnectionFactory::Options recv_options;
1693 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001694 ASSERT_TRUE(
1695 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001696 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1697 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1698 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001699 LocalP2PTest();
1700
torbjorng43166b82016-03-11 00:06:47 -08001701 EXPECT_TRUE_WAIT(
1702 rtc::SSLStreamAdapter::IsAcceptableCipher(
1703 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1704 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001705 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001706 initializing_client()->GetSrtpCipherStats(),
1707 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001708 EXPECT_EQ(1,
1709 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1710 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001711}
1712
1713// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1714// received supports 1.2.
torbjorng43166b82016-03-11 00:06:47 -08001715TEST_F(P2PTestConductor, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001716 PeerConnectionFactory::Options init_options;
1717 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1718 PeerConnectionFactory::Options recv_options;
1719 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001720 ASSERT_TRUE(
1721 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001722 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1723 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1724 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001725 LocalP2PTest();
1726
torbjorng43166b82016-03-11 00:06:47 -08001727 EXPECT_TRUE_WAIT(
1728 rtc::SSLStreamAdapter::IsAcceptableCipher(
1729 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1730 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001731 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001732 initializing_client()->GetSrtpCipherStats(),
1733 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001734 EXPECT_EQ(1,
1735 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1736 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001737}
1738
deadbeefb5cb19b2015-11-23 16:39:12 -08001739// This test sets up a call between two parties with audio, video and an RTP
1740// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001741TEST_F(P2PTestConductor, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001742 FakeConstraints setup_constraints;
1743 setup_constraints.SetAllowRtpDataChannels();
1744 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1745 initializing_client()->CreateDataChannel();
1746 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001747 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1748 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1750 kMaxWaitMs);
1751 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1752 kMaxWaitMs);
1753
1754 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001755
1756 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001757 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1758 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001759
1760 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1762 kMaxWaitMs);
1763
1764 receiving_client()->data_channel()->Close();
1765 // Send new offer and answer.
1766 receiving_client()->Negotiate();
1767 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1768 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1769}
1770
deadbeefb5cb19b2015-11-23 16:39:12 -08001771// This test sets up a call between two parties with audio, video and an SCTP
1772// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001773TEST_F(P2PTestConductor, LocalP2PTestSctpDataChannel) {
deadbeefb5cb19b2015-11-23 16:39:12 -08001774 ASSERT_TRUE(CreateTestClients());
1775 initializing_client()->CreateDataChannel();
1776 LocalP2PTest();
1777 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1778 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
1779 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1780 kMaxWaitMs);
1781 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
1782
1783 std::string data = "hello world";
1784
1785 initializing_client()->data_channel()->Send(DataBuffer(data));
1786 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1787 kMaxWaitMs);
1788
1789 receiving_client()->data_channel()->Send(DataBuffer(data));
1790 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1791 kMaxWaitMs);
1792
1793 receiving_client()->data_channel()->Close();
deadbeef15887932015-12-14 19:32:34 -08001794 EXPECT_TRUE_WAIT(!initializing_client()->data_observer()->IsOpen(),
1795 kMaxWaitMs);
1796 EXPECT_TRUE_WAIT(!receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
deadbeefb5cb19b2015-11-23 16:39:12 -08001797}
1798
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001799// This test sets up a call between two parties and creates a data channel.
1800// The test tests that received data is buffered unless an observer has been
1801// registered.
1802// Rtp data channels can receive data before the underlying
1803// transport has detected that a channel is writable and thus data can be
1804// received before the data channel state changes to open. That is hard to test
1805// but the same buffering is used in that case.
deadbeef7c73bdb2015-12-10 15:10:44 -08001806TEST_F(P2PTestConductor, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807 FakeConstraints setup_constraints;
1808 setup_constraints.SetAllowRtpDataChannels();
1809 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1810 initializing_client()->CreateDataChannel();
1811 initializing_client()->Negotiate();
1812
deadbeefaf1b59c2015-10-15 12:08:41 -07001813 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1814 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001815 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1816 kMaxWaitMs);
1817 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1818 receiving_client()->data_channel()->state(), kMaxWaitMs);
1819
1820 // Unregister the existing observer.
1821 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001822
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001824 SendRtpData(initializing_client()->data_channel(), data);
1825
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001826 // Wait a while to allow the sent data to arrive before an observer is
1827 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001828 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001829
1830 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1831 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1832}
1833
1834// This test sets up a call between two parties with audio, video and but only
1835// the initiating client support data.
deadbeef7c73bdb2015-12-10 15:10:44 -08001836TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001837 FakeConstraints setup_constraints_1;
1838 setup_constraints_1.SetAllowRtpDataChannels();
1839 // Must disable DTLS to make negotiation succeed.
1840 setup_constraints_1.SetMandatory(
1841 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1842 FakeConstraints setup_constraints_2;
1843 setup_constraints_2.SetMandatory(
1844 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1845 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001846 initializing_client()->CreateDataChannel();
1847 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001848 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001849 EXPECT_FALSE(receiving_client()->data_channel());
1850 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1851}
1852
1853// This test sets up a call between two parties with audio, video. When audio
1854// and video is setup and flowing and data channel is negotiated.
deadbeef7c73bdb2015-12-10 15:10:44 -08001855TEST_F(P2PTestConductor, AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856 FakeConstraints setup_constraints;
1857 setup_constraints.SetAllowRtpDataChannels();
1858 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1859 LocalP2PTest();
1860 initializing_client()->CreateDataChannel();
1861 // Send new offer and answer.
1862 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07001863 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1864 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1866 kMaxWaitMs);
1867 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1868 kMaxWaitMs);
1869}
1870
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001871// This test sets up a Jsep call with SCTP DataChannel and verifies the
1872// negotiation is completed without error.
1873#ifdef HAVE_SCTP
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001874TEST_F(P2PTestConductor, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001875 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001876 FakeConstraints constraints;
1877 constraints.SetMandatory(
1878 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1879 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1880 initializing_client()->CreateDataChannel();
1881 initializing_client()->Negotiate(false, false);
1882}
1883#endif
1884
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001885// This test sets up a call between two parties with audio, and video.
1886// During the call, the initializing side restart ice and the test verifies that
1887// new ice candidates are generated and audio and video still can flow.
deadbeef7c73bdb2015-12-10 15:10:44 -08001888TEST_F(P2PTestConductor, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001889 ASSERT_TRUE(CreateTestClients());
1890
1891 // Negotiate and wait for ice completion and make sure audio and video plays.
1892 LocalP2PTest();
1893
1894 // Create a SDP string of the first audio candidate for both clients.
1895 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1896 initializing_client()->pc()->local_description()->candidates(0);
1897 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1898 receiving_client()->pc()->local_description()->candidates(0);
1899 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1900 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1901 std::string initiator_candidate;
1902 EXPECT_TRUE(
1903 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1904 std::string receiver_candidate;
1905 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1906
1907 // Restart ice on the initializing client.
1908 receiving_client()->SetExpectIceRestart(true);
1909 initializing_client()->IceRestart();
1910
1911 // Negotiate and wait for ice completion again and make sure audio and video
1912 // plays.
1913 LocalP2PTest();
1914
1915 // Create a SDP string of the first audio candidate for both clients again.
1916 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1917 initializing_client()->pc()->local_description()->candidates(0);
1918 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1919 receiving_client()->pc()->local_description()->candidates(0);
1920 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1921 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1922 std::string initiator_candidate_restart;
1923 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1924 &initiator_candidate_restart));
1925 std::string receiver_candidate_restart;
1926 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1927 &receiver_candidate_restart));
1928
1929 // Verify that the first candidates in the local session descriptions has
1930 // changed.
1931 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1932 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1933}
1934
deadbeeffaac4972015-11-12 15:33:07 -08001935// This test sets up a call between two parties with audio, and video.
1936// It then renegotiates setting the video m-line to "port 0", then later
1937// renegotiates again, enabling video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001938TEST_F(P2PTestConductor, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08001939 ASSERT_TRUE(CreateTestClients());
1940
1941 // Do initial negotiation. Will result in video and audio sendonly m-lines.
1942 receiving_client()->set_auto_add_stream(false);
1943 initializing_client()->AddMediaStream(true, true);
1944 initializing_client()->Negotiate();
1945
1946 // Negotiate again, disabling the video m-line (receiving client will
1947 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
1948 receiving_client()->SetReceiveVideo(false);
1949 initializing_client()->Negotiate();
1950
1951 // Enable video and do negotiation again, making sure video is received
1952 // end-to-end.
1953 receiving_client()->SetReceiveVideo(true);
1954 receiving_client()->AddMediaStream(true, true);
1955 LocalP2PTest();
1956}
1957
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958// This test sets up a Jsep call between two parties with external
1959// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001960// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1961// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001962TEST_F(P2PTestConductor, DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001963 ASSERT_TRUE(CreateTestClients());
1964 EnableVideoDecoderFactory();
1965 LocalP2PTest();
1966}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001967
deadbeeffac06552015-11-25 11:26:01 -08001968// This tests that if we negotiate after calling CreateSender but before we
1969// have a track, then set a track later, frames from the newly-set track are
1970// received end-to-end.
deadbeef7c73bdb2015-12-10 15:10:44 -08001971TEST_F(P2PTestConductor, EarlyWarmupTest) {
deadbeeffac06552015-11-25 11:26:01 -08001972 ASSERT_TRUE(CreateTestClients());
deadbeefbd7d8f72015-12-18 16:58:44 -08001973 auto audio_sender =
1974 initializing_client()->pc()->CreateSender("audio", "stream_id");
1975 auto video_sender =
1976 initializing_client()->pc()->CreateSender("video", "stream_id");
deadbeeffac06552015-11-25 11:26:01 -08001977 initializing_client()->Negotiate();
1978 // Wait for ICE connection to complete, without any tracks.
1979 // Note that the receiving client WILL (in HandleIncomingOffer) create
1980 // tracks, so it's only the initiator here that's doing early warmup.
1981 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1982 VerifySessionDescriptions();
1983 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1984 initializing_client()->ice_connection_state(),
1985 kMaxWaitForFramesMs);
1986 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1987 receiving_client()->ice_connection_state(),
1988 kMaxWaitForFramesMs);
1989 // Now set the tracks, and expect frames to immediately start flowing.
1990 EXPECT_TRUE(
1991 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
1992 EXPECT_TRUE(
1993 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
hta6b4f8392016-03-10 00:24:31 -08001994 EXPECT_TRUE_WAIT(FramesHaveArrived(kEndAudioFrameCount, kEndVideoFrameCount),
deadbeeffac06552015-11-25 11:26:01 -08001995 kMaxWaitForFramesMs);
1996}
1997
deadbeef0a6c4ca2015-10-06 11:38:28 -07001998class IceServerParsingTest : public testing::Test {
1999 public:
2000 // Convenience for parsing a single URL.
2001 bool ParseUrl(const std::string& url) {
2002 return ParseUrl(url, std::string(), std::string());
2003 }
2004
2005 bool ParseUrl(const std::string& url,
2006 const std::string& username,
2007 const std::string& password) {
2008 PeerConnectionInterface::IceServers servers;
2009 PeerConnectionInterface::IceServer server;
2010 server.urls.push_back(url);
2011 server.username = username;
2012 server.password = password;
2013 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002014 return webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002015 }
2016
2017 protected:
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002018 cricket::ServerAddresses stun_servers_;
2019 std::vector<cricket::RelayServerConfig> turn_servers_;
deadbeef0a6c4ca2015-10-06 11:38:28 -07002020};
2021
2022// Make sure all STUN/TURN prefixes are parsed correctly.
2023TEST_F(IceServerParsingTest, ParseStunPrefixes) {
2024 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002025 EXPECT_EQ(1U, stun_servers_.size());
2026 EXPECT_EQ(0U, turn_servers_.size());
2027 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002028
2029 EXPECT_TRUE(ParseUrl("stuns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002030 EXPECT_EQ(1U, stun_servers_.size());
2031 EXPECT_EQ(0U, turn_servers_.size());
2032 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002033
2034 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002035 EXPECT_EQ(0U, stun_servers_.size());
2036 EXPECT_EQ(1U, turn_servers_.size());
2037 EXPECT_FALSE(turn_servers_[0].ports[0].secure);
2038 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002039
2040 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002041 EXPECT_EQ(0U, stun_servers_.size());
2042 EXPECT_EQ(1U, turn_servers_.size());
2043 EXPECT_TRUE(turn_servers_[0].ports[0].secure);
2044 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002045
2046 // invalid prefixes
2047 EXPECT_FALSE(ParseUrl("stunn:hostname"));
2048 EXPECT_FALSE(ParseUrl(":hostname"));
2049 EXPECT_FALSE(ParseUrl(":"));
2050 EXPECT_FALSE(ParseUrl(""));
2051}
2052
2053TEST_F(IceServerParsingTest, VerifyDefaults) {
2054 // TURNS defaults
2055 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002056 EXPECT_EQ(1U, turn_servers_.size());
2057 EXPECT_EQ(5349, turn_servers_[0].ports[0].address.port());
2058 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
2059 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002060
2061 // TURN defaults
2062 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002063 EXPECT_EQ(1U, turn_servers_.size());
2064 EXPECT_EQ(3478, turn_servers_[0].ports[0].address.port());
2065 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2066 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002067
2068 // STUN defaults
2069 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002070 EXPECT_EQ(1U, stun_servers_.size());
2071 EXPECT_EQ(3478, stun_servers_.begin()->port());
2072 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002073}
2074
2075// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
2076// can be parsed correctly.
2077TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
2078 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002079 EXPECT_EQ(1U, stun_servers_.size());
2080 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2081 EXPECT_EQ(1234, stun_servers_.begin()->port());
2082 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002083
2084 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002085 EXPECT_EQ(1U, stun_servers_.size());
2086 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2087 EXPECT_EQ(4321, stun_servers_.begin()->port());
2088 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002089
2090 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002091 EXPECT_EQ(1U, stun_servers_.size());
2092 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2093 EXPECT_EQ(9999, stun_servers_.begin()->port());
2094 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002095
2096 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002097 EXPECT_EQ(1U, stun_servers_.size());
2098 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2099 EXPECT_EQ(3478, stun_servers_.begin()->port());
2100 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002101
2102 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002103 EXPECT_EQ(1U, stun_servers_.size());
2104 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2105 EXPECT_EQ(3478, stun_servers_.begin()->port());
2106 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002107
2108 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002109 EXPECT_EQ(1U, stun_servers_.size());
2110 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2111 EXPECT_EQ(3478, stun_servers_.begin()->port());
2112 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002113
2114 // Try some invalid hostname:port strings.
2115 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
2116 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002117 EXPECT_FALSE(ParseUrl("stun:hostname:port:more"));
2118 EXPECT_FALSE(ParseUrl("stun:hostname:port more"));
deadbeef0a6c4ca2015-10-06 11:38:28 -07002119 EXPECT_FALSE(ParseUrl("stun:hostname:"));
2120 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
2121 EXPECT_FALSE(ParseUrl("stun::5555"));
2122 EXPECT_FALSE(ParseUrl("stun:"));
2123}
2124
2125// Test parsing the "?transport=xxx" part of the URL.
2126TEST_F(IceServerParsingTest, ParseTransport) {
2127 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002128 EXPECT_EQ(1U, turn_servers_.size());
2129 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
2130 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002131
2132 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002133 EXPECT_EQ(1U, turn_servers_.size());
2134 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2135 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002136
2137 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
2138}
2139
2140// Test parsing ICE username contained in URL.
2141TEST_F(IceServerParsingTest, ParseUsername) {
2142 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002143 EXPECT_EQ(1U, turn_servers_.size());
2144 EXPECT_EQ("user", turn_servers_[0].credentials.username);
2145 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002146
2147 EXPECT_FALSE(ParseUrl("turn:@hostname"));
2148 EXPECT_FALSE(ParseUrl("turn:username@"));
2149 EXPECT_FALSE(ParseUrl("turn:@"));
2150 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
2151}
2152
2153// Test that username and password from IceServer is copied into the resulting
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002154// RelayServerConfig.
deadbeef0a6c4ca2015-10-06 11:38:28 -07002155TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
2156 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002157 EXPECT_EQ(1U, turn_servers_.size());
2158 EXPECT_EQ("username", turn_servers_[0].credentials.username);
2159 EXPECT_EQ("password", turn_servers_[0].credentials.password);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002160}
2161
2162// Ensure that if a server has multiple URLs, each one is parsed.
2163TEST_F(IceServerParsingTest, ParseMultipleUrls) {
2164 PeerConnectionInterface::IceServers servers;
2165 PeerConnectionInterface::IceServer server;
2166 server.urls.push_back("stun:hostname");
2167 server.urls.push_back("turn:hostname");
2168 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002169 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2170 EXPECT_EQ(1U, stun_servers_.size());
2171 EXPECT_EQ(1U, turn_servers_.size());
deadbeef0a6c4ca2015-10-06 11:38:28 -07002172}
2173
Taylor Brandstetter893505d2016-01-07 15:12:48 -08002174// Ensure that TURN servers are given unique priorities,
2175// so that their resulting candidates have unique priorities.
2176TEST_F(IceServerParsingTest, TurnServerPrioritiesUnique) {
2177 PeerConnectionInterface::IceServers servers;
2178 PeerConnectionInterface::IceServer server;
2179 server.urls.push_back("turn:hostname");
2180 server.urls.push_back("turn:hostname2");
2181 servers.push_back(server);
2182 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2183 EXPECT_EQ(2U, turn_servers_.size());
2184 EXPECT_NE(turn_servers_[0].priority, turn_servers_[1].priority);
2185}
2186
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00002187#endif // if !defined(THREAD_SANITIZER)
hta6b4f8392016-03-10 00:24:31 -08002188
2189} // namespace