blob: 24411ab729060c09c9658aab987b49e0b9db85f5 [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>
kwibergd1fe2812016-04-27 06:47:29 -070016#include <memory>
kwiberg0eb15ed2015-12-17 03:04:15 -080017#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018#include <vector>
19
Henrik Kjellander15583c12016-02-10 10:53:12 +010020#include "webrtc/api/dtmfsender.h"
21#include "webrtc/api/fakemetricsobserver.h"
22#include "webrtc/api/localaudiosource.h"
23#include "webrtc/api/mediastreaminterface.h"
24#include "webrtc/api/peerconnection.h"
25#include "webrtc/api/peerconnectionfactory.h"
26#include "webrtc/api/peerconnectioninterface.h"
27#include "webrtc/api/test/fakeaudiocapturemodule.h"
28#include "webrtc/api/test/fakeconstraints.h"
29#include "webrtc/api/test/fakedtlsidentitystore.h"
30#include "webrtc/api/test/fakeperiodicvideocapturer.h"
31#include "webrtc/api/test/fakevideotrackrenderer.h"
32#include "webrtc/api/test/mockpeerconnectionobservers.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000033#include "webrtc/base/gunit.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000034#include "webrtc/base/physicalsocketserver.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000035#include "webrtc/base/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"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070040#include "webrtc/p2p/base/fakeportallocator.h"
kjellanderf4752772016-03-02 05:42:30 -080041#include "webrtc/p2p/base/p2pconstants.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000042#include "webrtc/p2p/base/sessiondescription.h"
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,
kwibergd1fe2812016-04-27 06:47:29 -0700157 std::unique_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store,
perkj8aba9972016-04-10 23:54:34 -0700158 bool prefer_constraint_apis,
danilchape9021a32016-05-17 01:52:02 -0700159 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700160 rtc::Thread* worker_thread) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800161 PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
htaaac2dea2016-03-10 13:35:55 -0800162 if (!client->Init(constraints, options, std::move(dtls_identity_store),
danilchape9021a32016-05-17 01:52:02 -0700163 prefer_constraint_apis, network_thread, worker_thread)) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800164 delete client;
165 return nullptr;
166 }
167 return client;
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800168 }
169
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800170 static PeerConnectionTestClient* CreateClient(
171 const std::string& id,
172 const MediaConstraintsInterface* constraints,
perkj8aba9972016-04-10 23:54:34 -0700173 const PeerConnectionFactory::Options* options,
danilchape9021a32016-05-17 01:52:02 -0700174 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700175 rtc::Thread* worker_thread) {
kwibergd1fe2812016-04-27 06:47:29 -0700176 std::unique_ptr<FakeDtlsIdentityStore> dtls_identity_store(
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800177 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
178 : nullptr);
179
danilchape9021a32016-05-17 01:52:02 -0700180 return CreateClientWithDtlsIdentityStore(
181 id, constraints, options, std::move(dtls_identity_store), true,
182 network_thread, worker_thread);
htaaac2dea2016-03-10 13:35:55 -0800183 }
184
185 static PeerConnectionTestClient* CreateClientPreferNoConstraints(
186 const std::string& id,
perkj8aba9972016-04-10 23:54:34 -0700187 const PeerConnectionFactory::Options* options,
danilchape9021a32016-05-17 01:52:02 -0700188 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700189 rtc::Thread* worker_thread) {
kwibergd1fe2812016-04-27 06:47:29 -0700190 std::unique_ptr<FakeDtlsIdentityStore> dtls_identity_store(
htaaac2dea2016-03-10 13:35:55 -0800191 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
192 : nullptr);
193
danilchape9021a32016-05-17 01:52:02 -0700194 return CreateClientWithDtlsIdentityStore(
195 id, nullptr, options, std::move(dtls_identity_store), false,
196 network_thread, worker_thread);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800197 }
198
deadbeefaf1b59c2015-10-15 12:08:41 -0700199 ~PeerConnectionTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200 }
201
deadbeefaf1b59c2015-10-15 12:08:41 -0700202 void Negotiate() { Negotiate(true, true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203
deadbeefaf1b59c2015-10-15 12:08:41 -0700204 void Negotiate(bool audio, bool video) {
kwibergd1fe2812016-04-27 06:47:29 -0700205 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700206 ASSERT_TRUE(DoCreateOffer(&offer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207
deadbeefaf1b59c2015-10-15 12:08:41 -0700208 if (offer->description()->GetContentByName("audio")) {
209 offer->description()->GetContentByName("audio")->rejected = !audio;
210 }
211 if (offer->description()->GetContentByName("video")) {
212 offer->description()->GetContentByName("video")->rejected = !video;
213 }
214
215 std::string sdp;
216 EXPECT_TRUE(offer->ToString(&sdp));
217 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
218 signaling_message_receiver_->ReceiveSdpMessage(
219 webrtc::SessionDescriptionInterface::kOffer, sdp);
220 }
221
222 // SignalingMessageReceiver callback.
223 void ReceiveSdpMessage(const std::string& type, std::string& msg) override {
224 FilterIncomingSdpMessage(&msg);
225 if (type == webrtc::SessionDescriptionInterface::kOffer) {
226 HandleIncomingOffer(msg);
227 } else {
228 HandleIncomingAnswer(msg);
229 }
230 }
231
232 // SignalingMessageReceiver callback.
233 void ReceiveIceMessage(const std::string& sdp_mid,
234 int sdp_mline_index,
235 const std::string& msg) override {
236 LOG(INFO) << id_ << "ReceiveIceMessage";
kwibergd1fe2812016-04-27 06:47:29 -0700237 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
deadbeefaf1b59c2015-10-15 12:08:41 -0700238 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
239 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
240 }
241
242 // PeerConnectionObserver callbacks.
243 void OnSignalingChange(
244 webrtc::PeerConnectionInterface::SignalingState new_state) override {
245 EXPECT_EQ(pc()->signaling_state(), new_state);
246 }
deadbeeffaac4972015-11-12 15:33:07 -0800247 void OnAddStream(MediaStreamInterface* media_stream) override {
248 media_stream->RegisterObserver(this);
deadbeefaf1b59c2015-10-15 12:08:41 -0700249 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
250 const std::string id = media_stream->GetVideoTracks()[i]->id();
251 ASSERT_TRUE(fake_video_renderers_.find(id) ==
252 fake_video_renderers_.end());
deadbeefc9be0072015-12-14 18:27:57 -0800253 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
254 media_stream->GetVideoTracks()[i]));
deadbeefaf1b59c2015-10-15 12:08:41 -0700255 }
256 }
deadbeeffaac4972015-11-12 15:33:07 -0800257 void OnRemoveStream(MediaStreamInterface* media_stream) override {}
deadbeefaf1b59c2015-10-15 12:08:41 -0700258 void OnRenegotiationNeeded() override {}
259 void OnIceConnectionChange(
260 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
261 EXPECT_EQ(pc()->ice_connection_state(), new_state);
262 }
263 void OnIceGatheringChange(
264 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
265 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
266 }
267 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
268 LOG(INFO) << id_ << "OnIceCandidate";
269
270 std::string ice_sdp;
271 EXPECT_TRUE(candidate->ToString(&ice_sdp));
272 if (signaling_message_receiver_ == nullptr) {
273 // Remote party may be deleted.
274 return;
275 }
276 signaling_message_receiver_->ReceiveIceMessage(
277 candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
278 }
279
deadbeeffaac4972015-11-12 15:33:07 -0800280 // MediaStreamInterface callback
281 void OnChanged() override {
282 // Track added or removed from MediaStream, so update our renderers.
283 rtc::scoped_refptr<StreamCollectionInterface> remote_streams =
284 pc()->remote_streams();
285 // Remove renderers for tracks that were removed.
286 for (auto it = fake_video_renderers_.begin();
287 it != fake_video_renderers_.end();) {
288 if (remote_streams->FindVideoTrack(it->first) == nullptr) {
deadbeefc9be0072015-12-14 18:27:57 -0800289 auto to_remove = it++;
290 removed_fake_video_renderers_.push_back(std::move(to_remove->second));
291 fake_video_renderers_.erase(to_remove);
deadbeeffaac4972015-11-12 15:33:07 -0800292 } else {
293 ++it;
294 }
295 }
296 // Create renderers for new video tracks.
297 for (size_t stream_index = 0; stream_index < remote_streams->count();
298 ++stream_index) {
299 MediaStreamInterface* remote_stream = remote_streams->at(stream_index);
300 for (size_t track_index = 0;
301 track_index < remote_stream->GetVideoTracks().size();
302 ++track_index) {
303 const std::string id =
304 remote_stream->GetVideoTracks()[track_index]->id();
305 if (fake_video_renderers_.find(id) != fake_video_renderers_.end()) {
306 continue;
307 }
deadbeefc9be0072015-12-14 18:27:57 -0800308 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
309 remote_stream->GetVideoTracks()[track_index]));
deadbeeffaac4972015-11-12 15:33:07 -0800310 }
311 }
312 }
313
deadbeefaf1b59c2015-10-15 12:08:41 -0700314 void SetVideoConstraints(const webrtc::FakeConstraints& video_constraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315 video_constraints_ = video_constraint;
316 }
317
318 void AddMediaStream(bool audio, bool video) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700319 std::string stream_label =
320 kStreamLabelBase +
321 rtc::ToString<int>(static_cast<int>(pc()->local_streams()->count()));
deadbeeffaac4972015-11-12 15:33:07 -0800322 rtc::scoped_refptr<MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000323 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324
325 if (audio && can_receive_audio()) {
deadbeeffac06552015-11-25 11:26:01 -0800326 stream->AddTrack(CreateLocalAudioTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 }
328 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000329 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330 }
331
deadbeefaf1b59c2015-10-15 12:08:41 -0700332 EXPECT_TRUE(pc()->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333 }
334
deadbeefaf1b59c2015-10-15 12:08:41 -0700335 size_t NumberOfLocalMediaStreams() { return pc()->local_streams()->count(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336
337 bool SessionActive() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700338 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339 }
340
deadbeeffaac4972015-11-12 15:33:07 -0800341 // Automatically add a stream when receiving an offer, if we don't have one.
342 // Defaults to true.
343 void set_auto_add_stream(bool auto_add_stream) {
344 auto_add_stream_ = auto_add_stream;
345 }
346
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347 void set_signaling_message_receiver(
deadbeefaf1b59c2015-10-15 12:08:41 -0700348 SignalingMessageReceiver* signaling_message_receiver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349 signaling_message_receiver_ = signaling_message_receiver;
350 }
351
352 void EnableVideoDecoderFactory() {
353 video_decoder_factory_enabled_ = true;
354 fake_video_decoder_factory_->AddSupportedVideoCodecType(
355 webrtc::kVideoCodecVP8);
356 }
357
deadbeefaf1b59c2015-10-15 12:08:41 -0700358 void IceRestart() {
htaaac2dea2016-03-10 13:35:55 -0800359 offer_answer_constraints_.SetMandatoryIceRestart(true);
360 offer_answer_options_.ice_restart = true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700361 SetExpectIceRestart(true);
362 }
363
364 void SetExpectIceRestart(bool expect_restart) {
365 expect_ice_restart_ = expect_restart;
366 }
367
368 bool ExpectIceRestart() const { return expect_ice_restart_; }
369
370 void SetReceiveAudioVideo(bool audio, bool video) {
371 SetReceiveAudio(audio);
372 SetReceiveVideo(video);
373 ASSERT_EQ(audio, can_receive_audio());
374 ASSERT_EQ(video, can_receive_video());
375 }
376
377 void SetReceiveAudio(bool audio) {
378 if (audio && can_receive_audio())
379 return;
htaaac2dea2016-03-10 13:35:55 -0800380 offer_answer_constraints_.SetMandatoryReceiveAudio(audio);
381 offer_answer_options_.offer_to_receive_audio = audio ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700382 }
383
384 void SetReceiveVideo(bool video) {
385 if (video && can_receive_video())
386 return;
htaaac2dea2016-03-10 13:35:55 -0800387 offer_answer_constraints_.SetMandatoryReceiveVideo(video);
388 offer_answer_options_.offer_to_receive_video = video ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700389 }
390
391 void RemoveMsidFromReceivedSdp(bool remove) { remove_msid_ = remove; }
392
393 void RemoveSdesCryptoFromReceivedSdp(bool remove) { remove_sdes_ = remove; }
394
395 void RemoveBundleFromReceivedSdp(bool remove) { remove_bundle_ = remove; }
396
perkjcaafdba2016-03-20 07:34:29 -0700397 void RemoveCvoFromReceivedSdp(bool remove) { remove_cvo_ = remove; }
398
deadbeefaf1b59c2015-10-15 12:08:41 -0700399 bool can_receive_audio() {
400 bool value;
htaaac2dea2016-03-10 13:35:55 -0800401 if (prefer_constraint_apis_) {
402 if (webrtc::FindConstraint(
403 &offer_answer_constraints_,
404 MediaConstraintsInterface::kOfferToReceiveAudio, &value,
405 nullptr)) {
406 return value;
407 }
408 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700409 }
htaaac2dea2016-03-10 13:35:55 -0800410 return offer_answer_options_.offer_to_receive_audio > 0 ||
411 offer_answer_options_.offer_to_receive_audio ==
412 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700413 }
414
415 bool can_receive_video() {
416 bool value;
htaaac2dea2016-03-10 13:35:55 -0800417 if (prefer_constraint_apis_) {
418 if (webrtc::FindConstraint(
419 &offer_answer_constraints_,
420 MediaConstraintsInterface::kOfferToReceiveVideo, &value,
421 nullptr)) {
422 return value;
423 }
424 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700425 }
htaaac2dea2016-03-10 13:35:55 -0800426 return offer_answer_options_.offer_to_receive_video > 0 ||
427 offer_answer_options_.offer_to_receive_video ==
428 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700429 }
430
deadbeefaf1b59c2015-10-15 12:08:41 -0700431 void OnDataChannel(DataChannelInterface* data_channel) override {
432 LOG(INFO) << id_ << "OnDataChannel";
433 data_channel_ = data_channel;
434 data_observer_.reset(new MockDataChannelObserver(data_channel));
435 }
436
437 void CreateDataChannel() {
438 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, nullptr);
439 ASSERT_TRUE(data_channel_.get() != nullptr);
440 data_observer_.reset(new MockDataChannelObserver(data_channel_));
441 }
442
deadbeeffac06552015-11-25 11:26:01 -0800443 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack(
444 const std::string& stream_label) {
445 FakeConstraints constraints;
446 // Disable highpass filter so that we can get all the test audio frames.
447 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
448 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
449 peer_connection_factory_->CreateAudioSource(&constraints);
450 // TODO(perkj): Test audio source when it is implemented. Currently audio
451 // always use the default input.
452 std::string label = stream_label + kAudioTrackLabelBase;
453 return peer_connection_factory_->CreateAudioTrack(label, source);
454 }
455
456 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack(
457 const std::string& stream_label) {
458 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
459 FakeConstraints source_constraints = video_constraints_;
460 source_constraints.SetMandatoryMaxFrameRate(10);
461
462 cricket::FakeVideoCapturer* fake_capturer =
463 new webrtc::FakePeriodicVideoCapturer();
perkjcaafdba2016-03-20 07:34:29 -0700464 fake_capturer->SetRotation(capture_rotation_);
deadbeeffac06552015-11-25 11:26:01 -0800465 video_capturers_.push_back(fake_capturer);
perkja3ede6c2016-03-08 01:27:48 +0100466 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
deadbeeffac06552015-11-25 11:26:01 -0800467 peer_connection_factory_->CreateVideoSource(fake_capturer,
468 &source_constraints);
469 std::string label = stream_label + kVideoTrackLabelBase;
perkjcaafdba2016-03-20 07:34:29 -0700470
471 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
472 peer_connection_factory_->CreateVideoTrack(label, source));
473 if (!local_video_renderer_) {
474 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
475 }
476 return track;
deadbeeffac06552015-11-25 11:26:01 -0800477 }
478
deadbeefaf1b59c2015-10-15 12:08:41 -0700479 DataChannelInterface* data_channel() { return data_channel_; }
480 const MockDataChannelObserver* data_observer() const {
481 return data_observer_.get();
482 }
483
hta6b4f8392016-03-10 00:24:31 -0800484 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
deadbeefaf1b59c2015-10-15 12:08:41 -0700485
486 void StopVideoCapturers() {
perkjcaafdba2016-03-20 07:34:29 -0700487 for (auto* capturer : video_capturers_) {
488 capturer->Stop();
deadbeefaf1b59c2015-10-15 12:08:41 -0700489 }
490 }
491
perkjcaafdba2016-03-20 07:34:29 -0700492 void SetCaptureRotation(webrtc::VideoRotation rotation) {
493 ASSERT_TRUE(video_capturers_.empty());
494 capture_rotation_ = rotation;
495 }
496
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 bool AudioFramesReceivedCheck(int number_of_frames) const {
498 return number_of_frames <= fake_audio_capture_module_->frames_received();
499 }
500
deadbeefc9be0072015-12-14 18:27:57 -0800501 int audio_frames_received() const {
502 return fake_audio_capture_module_->frames_received();
503 }
504
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 bool VideoFramesReceivedCheck(int number_of_frames) {
506 if (video_decoder_factory_enabled_) {
507 const std::vector<FakeWebRtcVideoDecoder*>& decoders
508 = fake_video_decoder_factory_->decoders();
509 if (decoders.empty()) {
510 return number_of_frames <= 0;
511 }
hta6b4f8392016-03-10 00:24:31 -0800512 // Note - this checks that EACH decoder has the requisite number
513 // of frames. The video_frames_received() function sums them.
deadbeefc9be0072015-12-14 18:27:57 -0800514 for (FakeWebRtcVideoDecoder* decoder : decoders) {
515 if (number_of_frames > decoder->GetNumFramesReceived()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000516 return false;
517 }
518 }
519 return true;
520 } else {
521 if (fake_video_renderers_.empty()) {
522 return number_of_frames <= 0;
523 }
524
deadbeefc9be0072015-12-14 18:27:57 -0800525 for (const auto& pair : fake_video_renderers_) {
526 if (number_of_frames > pair.second->num_rendered_frames()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 return false;
528 }
529 }
530 return true;
531 }
532 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700533
deadbeefc9be0072015-12-14 18:27:57 -0800534 int video_frames_received() const {
535 int total = 0;
536 if (video_decoder_factory_enabled_) {
537 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
538 fake_video_decoder_factory_->decoders();
539 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
540 total += decoder->GetNumFramesReceived();
541 }
542 } else {
543 for (const auto& pair : fake_video_renderers_) {
544 total += pair.second->num_rendered_frames();
545 }
546 for (const auto& renderer : removed_fake_video_renderers_) {
547 total += renderer->num_rendered_frames();
548 }
549 }
550 return total;
551 }
552
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 // Verify the CreateDtmfSender interface
554 void VerifyDtmf() {
kwibergd1fe2812016-04-27 06:47:29 -0700555 std::unique_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000556 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557
558 // We can't create a DTMF sender with an invalid audio track or a non local
559 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700560 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000561 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700562 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
563 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564
565 // We should be able to create a DTMF sender from a local track.
566 webrtc::AudioTrackInterface* localtrack =
567 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
568 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700569 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 dtmf_sender->RegisterObserver(observer.get());
571
572 // Test the DtmfSender object just created.
573 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
574 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
575
576 // We don't need to verify that the DTMF tones are actually sent out because
577 // that is already covered by the tests of the lower level components.
578
579 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
580 std::vector<std::string> tones;
581 tones.push_back("1");
582 tones.push_back("a");
583 tones.push_back("");
584 observer->Verify(tones);
585
586 dtmf_sender->UnregisterObserver();
587 }
588
589 // Verifies that the SessionDescription have rejected the appropriate media
590 // content.
591 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700592 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
593 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594 const cricket::SessionDescription* remote_desc =
595 peer_connection_->remote_description()->description();
596 const cricket::SessionDescription* local_desc =
597 peer_connection_->local_description()->description();
598
599 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
600 if (remote_audio_content) {
601 const ContentInfo* audio_content =
602 GetFirstAudioContent(local_desc);
603 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
604 }
605
606 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
607 if (remote_video_content) {
608 const ContentInfo* video_content =
609 GetFirstVideoContent(local_desc);
610 EXPECT_EQ(can_receive_video(), !video_content->rejected);
611 }
612 }
613
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700615 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616 const cricket::SessionDescription* desc =
617 peer_connection_->local_description()->description();
618 const cricket::ContentInfos& contents = desc->contents();
619
620 for (size_t index = 0; index < contents.size(); ++index) {
621 if (contents[index].rejected)
622 continue;
623 const cricket::TransportDescription* transport_desc =
624 desc->GetTransportDescriptionByName(contents[index].name);
625
626 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000627 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628 if (ufragpair_it == ice_ufrag_pwd_.end()) {
629 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000630 ice_ufrag_pwd_[static_cast<int>(index)] =
631 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632 } else if (ExpectIceRestart()) {
633 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
634 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
635 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
636 } else {
637 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
638 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
639 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
640 }
641 }
642 }
643
644 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000645 rtc::scoped_refptr<MockStatsObserver>
646 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000647 EXPECT_TRUE(peer_connection_->GetStats(
648 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700650 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651 return observer->AudioOutputLevel();
652 }
653
654 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000655 rtc::scoped_refptr<MockStatsObserver>
656 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000657 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700658 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700660 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661 return observer->AudioInputLevel();
662 }
663
664 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000665 rtc::scoped_refptr<MockStatsObserver>
666 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000667 EXPECT_TRUE(peer_connection_->GetStats(
668 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700670 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671 return observer->BytesReceived();
672 }
673
674 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000675 rtc::scoped_refptr<MockStatsObserver>
676 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000677 EXPECT_TRUE(peer_connection_->GetStats(
678 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700680 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 return observer->BytesSent();
682 }
683
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000684 int GetAvailableReceivedBandwidthStats() {
685 rtc::scoped_refptr<MockStatsObserver>
686 observer(new rtc::RefCountedObject<MockStatsObserver>());
687 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700688 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000689 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700690 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000691 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000692 return bw;
693 }
694
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000695 std::string GetDtlsCipherStats() {
696 rtc::scoped_refptr<MockStatsObserver>
697 observer(new rtc::RefCountedObject<MockStatsObserver>());
698 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700699 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000700 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700701 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000702 return observer->DtlsCipher();
703 }
704
705 std::string GetSrtpCipherStats() {
706 rtc::scoped_refptr<MockStatsObserver>
707 observer(new rtc::RefCountedObject<MockStatsObserver>());
708 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700709 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000710 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700711 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000712 return observer->SrtpCipher();
713 }
714
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 int rendered_width() {
716 EXPECT_FALSE(fake_video_renderers_.empty());
717 return fake_video_renderers_.empty() ? 1 :
718 fake_video_renderers_.begin()->second->width();
719 }
720
721 int rendered_height() {
722 EXPECT_FALSE(fake_video_renderers_.empty());
723 return fake_video_renderers_.empty() ? 1 :
724 fake_video_renderers_.begin()->second->height();
725 }
726
perkjcaafdba2016-03-20 07:34:29 -0700727 webrtc::VideoRotation rendered_rotation() {
728 EXPECT_FALSE(fake_video_renderers_.empty());
729 return fake_video_renderers_.empty()
730 ? webrtc::kVideoRotation_0
731 : fake_video_renderers_.begin()->second->rotation();
732 }
733
734 int local_rendered_width() {
735 return local_video_renderer_ ? local_video_renderer_->width() : 1;
736 }
737
738 int local_rendered_height() {
739 return local_video_renderer_ ? local_video_renderer_->height() : 1;
740 }
741
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742 size_t number_of_remote_streams() {
743 if (!pc())
744 return 0;
745 return pc()->remote_streams()->count();
746 }
747
hta6b4f8392016-03-10 00:24:31 -0800748 StreamCollectionInterface* remote_streams() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 if (!pc()) {
750 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700751 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752 }
753 return pc()->remote_streams();
754 }
755
756 StreamCollectionInterface* local_streams() {
757 if (!pc()) {
758 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700759 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 }
761 return pc()->local_streams();
762 }
763
hta6b4f8392016-03-10 00:24:31 -0800764 bool HasLocalAudioTrack() { return StreamsHaveAudioTrack(local_streams()); }
765
766 bool HasLocalVideoTrack() { return StreamsHaveVideoTrack(local_streams()); }
767
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
769 return pc()->signaling_state();
770 }
771
772 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
773 return pc()->ice_connection_state();
774 }
775
776 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
777 return pc()->ice_gathering_state();
778 }
779
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 private:
781 class DummyDtmfObserver : public DtmfSenderObserverInterface {
782 public:
783 DummyDtmfObserver() : completed_(false) {}
784
785 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700786 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 tones_.push_back(tone);
788 if (tone.empty()) {
789 completed_ = true;
790 }
791 }
792
793 void Verify(const std::vector<std::string>& tones) const {
794 ASSERT_TRUE(tones_.size() == tones.size());
795 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
796 }
797
798 bool completed() const { return completed_; }
799
800 private:
801 bool completed_;
802 std::vector<std::string> tones_;
803 };
804
deadbeefaf1b59c2015-10-15 12:08:41 -0700805 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
806
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800807 bool Init(
808 const MediaConstraintsInterface* constraints,
809 const PeerConnectionFactory::Options* options,
kwibergd1fe2812016-04-27 06:47:29 -0700810 std::unique_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store,
perkj8aba9972016-04-10 23:54:34 -0700811 bool prefer_constraint_apis,
danilchape9021a32016-05-17 01:52:02 -0700812 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700813 rtc::Thread* worker_thread) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700814 EXPECT_TRUE(!peer_connection_);
815 EXPECT_TRUE(!peer_connection_factory_);
htaaac2dea2016-03-10 13:35:55 -0800816 if (!prefer_constraint_apis) {
817 EXPECT_TRUE(!constraints);
818 }
819 prefer_constraint_apis_ = prefer_constraint_apis;
820
kwibergd1fe2812016-04-27 06:47:29 -0700821 std::unique_ptr<cricket::PortAllocator> port_allocator(
danilchape9021a32016-05-17 01:52:02 -0700822 new cricket::FakePortAllocator(network_thread, nullptr));
deadbeefaf1b59c2015-10-15 12:08:41 -0700823 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
824
825 if (fake_audio_capture_module_ == nullptr) {
826 return false;
827 }
828 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
829 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
danilchape9021a32016-05-17 01:52:02 -0700830 rtc::Thread* const signaling_thread = rtc::Thread::Current();
deadbeefaf1b59c2015-10-15 12:08:41 -0700831 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700832 network_thread, worker_thread, signaling_thread,
833 fake_audio_capture_module_, fake_video_encoder_factory_,
834 fake_video_decoder_factory_);
deadbeefaf1b59c2015-10-15 12:08:41 -0700835 if (!peer_connection_factory_) {
836 return false;
837 }
838 if (options) {
839 peer_connection_factory_->SetOptions(*options);
840 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800841 peer_connection_ = CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800842 std::move(port_allocator), constraints, std::move(dtls_identity_store));
deadbeefaf1b59c2015-10-15 12:08:41 -0700843 return peer_connection_.get() != nullptr;
844 }
845
deadbeefaf1b59c2015-10-15 12:08:41 -0700846 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
kwibergd1fe2812016-04-27 06:47:29 -0700847 std::unique_ptr<cricket::PortAllocator> port_allocator,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800848 const MediaConstraintsInterface* constraints,
kwibergd1fe2812016-04-27 06:47:29 -0700849 std::unique_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800850 // CreatePeerConnection with RTCConfiguration.
851 webrtc::PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 webrtc::PeerConnectionInterface::IceServer ice_server;
853 ice_server.uri = "stun:stun.l.google.com:19302";
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800854 config.servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000855
deadbeefaf1b59c2015-10-15 12:08:41 -0700856 return peer_connection_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800857 config, constraints, std::move(port_allocator),
858 std::move(dtls_identity_store), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 }
860
861 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700862 LOG(INFO) << id_ << "HandleIncomingOffer ";
deadbeeffaac4972015-11-12 15:33:07 -0800863 if (NumberOfLocalMediaStreams() == 0 && auto_add_stream_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 // If we are not sending any streams ourselves it is time to add some.
865 AddMediaStream(true, true);
866 }
kwibergd1fe2812016-04-27 06:47:29 -0700867 std::unique_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700868 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
kwibergd1fe2812016-04-27 06:47:29 -0700870 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700871 EXPECT_TRUE(DoCreateAnswer(&answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 std::string sdp;
873 EXPECT_TRUE(answer->ToString(&sdp));
874 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
deadbeefaf1b59c2015-10-15 12:08:41 -0700875 if (signaling_message_receiver_) {
876 signaling_message_receiver_->ReceiveSdpMessage(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 webrtc::SessionDescriptionInterface::kAnswer, sdp);
878 }
879 }
880
881 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700882 LOG(INFO) << id_ << "HandleIncomingAnswer";
kwibergd1fe2812016-04-27 06:47:29 -0700883 std::unique_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700884 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
886 }
887
kwibergd1fe2812016-04-27 06:47:29 -0700888 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000890 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
891 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892 MockCreateSessionDescriptionObserver>());
htaaac2dea2016-03-10 13:35:55 -0800893 if (prefer_constraint_apis_) {
894 if (offer) {
895 pc()->CreateOffer(observer, &offer_answer_constraints_);
896 } else {
897 pc()->CreateAnswer(observer, &offer_answer_constraints_);
898 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899 } else {
htaaac2dea2016-03-10 13:35:55 -0800900 if (offer) {
901 pc()->CreateOffer(observer, offer_answer_options_);
902 } else {
903 pc()->CreateAnswer(observer, offer_answer_options_);
904 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 }
906 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
kwiberg2bbff992016-03-16 11:03:04 -0700907 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 if (observer->result() && ExpectIceRestart()) {
909 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
910 }
911 return observer->result();
912 }
913
kwibergd1fe2812016-04-27 06:47:29 -0700914 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 return DoCreateOfferAnswer(desc, true);
916 }
917
kwibergd1fe2812016-04-27 06:47:29 -0700918 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 return DoCreateOfferAnswer(desc, false);
920 }
921
922 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000923 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
924 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700926 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 pc()->SetLocalDescription(observer, desc);
928 // Ignore the observer result. If we wait for the result with
929 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
930 // before the offer which is an error.
931 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000932 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000933 // ProcessMessages waits at least 1ms but processes all messages before
934 // returning. Since this test is synchronous and send messages to the remote
935 // peer whenever a callback is invoked, this can lead to messages being
936 // sent to the remote peer in the wrong order.
937 // TODO(perkj): Find a way to check the result without risking that the
938 // order of sent messages are changed. Ex- by posting all messages that are
939 // sent to the remote peer.
940 return true;
941 }
942
943 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000944 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
945 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700947 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948 pc()->SetRemoteDescription(observer, desc);
949 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
950 return observer->result();
951 }
952
953 // This modifies all received SDP messages before they are processed.
954 void FilterIncomingSdpMessage(std::string* sdp) {
955 if (remove_msid_) {
956 const char kSdpSsrcAttribute[] = "a=ssrc:";
957 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
958 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
959 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
960 }
961 if (remove_bundle_) {
962 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
963 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
964 }
965 if (remove_sdes_) {
966 const char kSdpSdesCryptoAttribute[] = "a=crypto";
967 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
968 }
perkjcaafdba2016-03-20 07:34:29 -0700969 if (remove_cvo_) {
970 const char kSdpCvoExtenstion[] = "urn:3gpp:video-orientation";
971 RemoveLinesFromSdp(kSdpCvoExtenstion, sdp);
972 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000973 }
974
deadbeefaf1b59c2015-10-15 12:08:41 -0700975 std::string id_;
976
deadbeefaf1b59c2015-10-15 12:08:41 -0700977 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
978 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
979 peer_connection_factory_;
980
htaaac2dea2016-03-10 13:35:55 -0800981 bool prefer_constraint_apis_ = true;
deadbeeffaac4972015-11-12 15:33:07 -0800982 bool auto_add_stream_ = true;
983
deadbeefaf1b59c2015-10-15 12:08:41 -0700984 typedef std::pair<std::string, std::string> IceUfragPwdPair;
985 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
986 bool expect_ice_restart_ = false;
987
deadbeefc9be0072015-12-14 18:27:57 -0800988 // Needed to keep track of number of frames sent.
deadbeefaf1b59c2015-10-15 12:08:41 -0700989 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
990 // Needed to keep track of number of frames received.
kwibergd1fe2812016-04-27 06:47:29 -0700991 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
deadbeefc9be0072015-12-14 18:27:57 -0800992 fake_video_renderers_;
993 // Needed to ensure frames aren't received for removed tracks.
kwibergd1fe2812016-04-27 06:47:29 -0700994 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
deadbeefc9be0072015-12-14 18:27:57 -0800995 removed_fake_video_renderers_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700996 // Needed to keep track of number of frames received when external decoder
997 // used.
998 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
999 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
1000 bool video_decoder_factory_enabled_ = false;
1001 webrtc::FakeConstraints video_constraints_;
1002
1003 // For remote peer communication.
1004 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
1005
1006 // Store references to the video capturers we've created, so that we can stop
1007 // them, if required.
perkjcaafdba2016-03-20 07:34:29 -07001008 std::vector<cricket::FakeVideoCapturer*> video_capturers_;
1009 webrtc::VideoRotation capture_rotation_ = webrtc::kVideoRotation_0;
1010 // |local_video_renderer_| attached to the first created local video track.
kwibergd1fe2812016-04-27 06:47:29 -07001011 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001012
htaaac2dea2016-03-10 13:35:55 -08001013 webrtc::FakeConstraints offer_answer_constraints_;
1014 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001015 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
1016 bool remove_bundle_ =
1017 false; // True if bundle should be removed in received SDP.
1018 bool remove_sdes_ =
1019 false; // True if a=crypto should be removed in received SDP.
perkjcaafdba2016-03-20 07:34:29 -07001020 // |remove_cvo_| is true if extension urn:3gpp:video-orientation should be
1021 // removed in the received SDP.
1022 bool remove_cvo_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001024 rtc::scoped_refptr<DataChannelInterface> data_channel_;
kwibergd1fe2812016-04-27 06:47:29 -07001025 std::unique_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001026};
1027
deadbeef7c73bdb2015-12-10 15:10:44 -08001028class P2PTestConductor : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029 public:
deadbeef7c73bdb2015-12-10 15:10:44 -08001030 P2PTestConductor()
danilchape9021a32016-05-17 01:52:02 -07001031 : network_thread_(rtc::Thread::CreateWithSocketServer()),
1032 worker_thread_(rtc::Thread::Create()),
1033 pss_(new rtc::PhysicalSocketServer),
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001034 ss_(new rtc::VirtualSocketServer(pss_.get())),
perkj8aba9972016-04-10 23:54:34 -07001035 ss_scope_(ss_.get()) {
danilchape9021a32016-05-17 01:52:02 -07001036 RTC_CHECK(network_thread_->Start());
1037 RTC_CHECK(worker_thread_->Start());
perkj8aba9972016-04-10 23:54:34 -07001038 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001039
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001040 bool SessionActive() {
1041 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001042 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001043 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001044
hta6b4f8392016-03-10 00:24:31 -08001045 // Return true if the number of frames provided have been received
1046 // on the video and audio tracks provided.
1047 bool FramesHaveArrived(int audio_frames_to_receive,
1048 int video_frames_to_receive) {
1049 bool all_good = true;
1050 if (initiating_client_->HasLocalAudioTrack() &&
1051 receiving_client_->can_receive_audio()) {
1052 all_good &=
1053 receiving_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1054 }
1055 if (initiating_client_->HasLocalVideoTrack() &&
1056 receiving_client_->can_receive_video()) {
1057 all_good &=
1058 receiving_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1059 }
1060 if (receiving_client_->HasLocalAudioTrack() &&
1061 initiating_client_->can_receive_audio()) {
1062 all_good &=
1063 initiating_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1064 }
1065 if (receiving_client_->HasLocalVideoTrack() &&
1066 initiating_client_->can_receive_video()) {
1067 all_good &=
1068 initiating_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1069 }
1070 return all_good;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071 }
hta6b4f8392016-03-10 00:24:31 -08001072
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073 void VerifyDtmf() {
1074 initiating_client_->VerifyDtmf();
1075 receiving_client_->VerifyDtmf();
1076 }
1077
1078 void TestUpdateOfferWithRejectedContent() {
deadbeefc9be0072015-12-14 18:27:57 -08001079 // Renegotiate, rejecting the video m-line.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001080 initiating_client_->Negotiate(true, false);
deadbeefc9be0072015-12-14 18:27:57 -08001081 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1082
1083 int pc1_audio_received = initiating_client_->audio_frames_received();
1084 int pc1_video_received = initiating_client_->video_frames_received();
1085 int pc2_audio_received = receiving_client_->audio_frames_received();
1086 int pc2_video_received = receiving_client_->video_frames_received();
1087
1088 // Wait for some additional audio frames to be received.
1089 EXPECT_TRUE_WAIT(initiating_client_->AudioFramesReceivedCheck(
1090 pc1_audio_received + kEndAudioFrameCount) &&
1091 receiving_client_->AudioFramesReceivedCheck(
1092 pc2_audio_received + kEndAudioFrameCount),
1093 kMaxWaitForFramesMs);
1094
1095 // During this time, we shouldn't have received any additional video frames
1096 // for the rejected video tracks.
1097 EXPECT_EQ(pc1_video_received, initiating_client_->video_frames_received());
1098 EXPECT_EQ(pc2_video_received, receiving_client_->video_frames_received());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099 }
1100
1101 void VerifyRenderedSize(int width, int height) {
perkjcaafdba2016-03-20 07:34:29 -07001102 VerifyRenderedSize(width, height, webrtc::kVideoRotation_0);
1103 }
1104
1105 void VerifyRenderedSize(int width,
1106 int height,
1107 webrtc::VideoRotation rotation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001108 EXPECT_EQ(width, receiving_client()->rendered_width());
1109 EXPECT_EQ(height, receiving_client()->rendered_height());
perkjcaafdba2016-03-20 07:34:29 -07001110 EXPECT_EQ(rotation, receiving_client()->rendered_rotation());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001111 EXPECT_EQ(width, initializing_client()->rendered_width());
1112 EXPECT_EQ(height, initializing_client()->rendered_height());
perkjcaafdba2016-03-20 07:34:29 -07001113 EXPECT_EQ(rotation, initializing_client()->rendered_rotation());
1114
1115 // Verify size of the local preview.
1116 EXPECT_EQ(width, initializing_client()->local_rendered_width());
1117 EXPECT_EQ(height, initializing_client()->local_rendered_height());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118 }
1119
1120 void VerifySessionDescriptions() {
1121 initiating_client_->VerifyRejectedMediaInSessionDescription();
1122 receiving_client_->VerifyRejectedMediaInSessionDescription();
1123 initiating_client_->VerifyLocalIceUfragAndPassword();
1124 receiving_client_->VerifyLocalIceUfragAndPassword();
1125 }
1126
deadbeef7c73bdb2015-12-10 15:10:44 -08001127 ~P2PTestConductor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001129 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001130 }
1131 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001132 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001133 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001134 }
1135
deadbeefaf1b59c2015-10-15 12:08:41 -07001136 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001137
1138 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1139 MediaConstraintsInterface* recv_constraints) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001140 return CreateTestClients(init_constraints, nullptr, recv_constraints,
1141 nullptr);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001142 }
1143
htaaac2dea2016-03-10 13:35:55 -08001144 bool CreateTestClientsThatPreferNoConstraints() {
1145 initiating_client_.reset(
perkj8aba9972016-04-10 23:54:34 -07001146 PeerConnectionTestClient::CreateClientPreferNoConstraints(
danilchape9021a32016-05-17 01:52:02 -07001147 "Caller: ", nullptr, network_thread_.get(), worker_thread_.get()));
htaaac2dea2016-03-10 13:35:55 -08001148 receiving_client_.reset(
perkj8aba9972016-04-10 23:54:34 -07001149 PeerConnectionTestClient::CreateClientPreferNoConstraints(
danilchape9021a32016-05-17 01:52:02 -07001150 "Callee: ", nullptr, network_thread_.get(), worker_thread_.get()));
htaaac2dea2016-03-10 13:35:55 -08001151 if (!initiating_client_ || !receiving_client_) {
1152 return false;
1153 }
1154 // Remember the choice for possible later resets of the clients.
1155 prefer_constraint_apis_ = false;
1156 SetSignalingReceivers();
1157 return true;
1158 }
1159
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001160 void SetSignalingReceivers() {
1161 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
1162 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1163 }
1164
Joachim Bauch04e5b492015-05-29 09:40:39 +02001165 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1166 PeerConnectionFactory::Options* init_options,
1167 MediaConstraintsInterface* recv_constraints,
1168 PeerConnectionFactory::Options* recv_options) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001169 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
danilchape9021a32016-05-17 01:52:02 -07001170 "Caller: ", init_constraints, init_options, network_thread_.get(),
1171 worker_thread_.get()));
deadbeefaf1b59c2015-10-15 12:08:41 -07001172 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
danilchape9021a32016-05-17 01:52:02 -07001173 "Callee: ", recv_constraints, recv_options, network_thread_.get(),
1174 worker_thread_.get()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001175 if (!initiating_client_ || !receiving_client_) {
1176 return false;
1177 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001178 SetSignalingReceivers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001179 return true;
1180 }
1181
1182 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1183 const webrtc::FakeConstraints& recv_constraints) {
1184 initiating_client_->SetVideoConstraints(init_constraints);
1185 receiving_client_->SetVideoConstraints(recv_constraints);
1186 }
1187
perkjcaafdba2016-03-20 07:34:29 -07001188 void SetCaptureRotation(webrtc::VideoRotation rotation) {
1189 initiating_client_->SetCaptureRotation(rotation);
1190 receiving_client_->SetCaptureRotation(rotation);
1191 }
1192
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001193 void EnableVideoDecoderFactory() {
1194 initiating_client_->EnableVideoDecoderFactory();
1195 receiving_client_->EnableVideoDecoderFactory();
1196 }
1197
1198 // This test sets up a call between two parties. Both parties send static
1199 // frames to each other. Once the test is finished the number of sent frames
1200 // is compared to the number of received frames.
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -07001201 void LocalP2PTest() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001202 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1203 initiating_client_->AddMediaStream(true, true);
1204 }
1205 initiating_client_->Negotiate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001206 // Assert true is used here since next tests are guaranteed to fail and
1207 // would eat up 5 seconds.
1208 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1209 VerifySessionDescriptions();
1210
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211 int audio_frame_count = kEndAudioFrameCount;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001212 int video_frame_count = kEndVideoFrameCount;
hta6b4f8392016-03-10 00:24:31 -08001213 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1214
1215 if ((!initiating_client_->can_receive_audio() &&
1216 !initiating_client_->can_receive_video()) ||
1217 (!receiving_client_->can_receive_audio() &&
1218 !receiving_client_->can_receive_video())) {
1219 // Neither audio nor video will flow, so connections won't be
1220 // established. There's nothing more to check.
1221 // TODO(hta): Check connection if there's a data channel.
1222 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223 }
1224
hta6b4f8392016-03-10 00:24:31 -08001225 // Audio or video is expected to flow, so both clients should reach the
1226 // Connected state, and the offerer (ICE controller) should proceed to
1227 // Completed.
1228 // Note: These tests have been observed to fail under heavy load at
1229 // shorter timeouts, so they may be flaky.
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -07001230 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1231 initiating_client_->ice_connection_state(),
1232 kMaxWaitForFramesMs);
hta6b4f8392016-03-10 00:24:31 -08001233 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1234 receiving_client_->ice_connection_state(),
1235 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001236
hta6b4f8392016-03-10 00:24:31 -08001237 // The ICE gathering state should end up in kIceGatheringComplete,
1238 // but there's a bug that prevents this at the moment, and the state
1239 // machine is being updated by the WEBRTC WG.
1240 // TODO(hta): Update this check when spec revisions finish.
1241 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1242 initiating_client_->ice_gathering_state());
1243 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1244 receiving_client_->ice_gathering_state(),
1245 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001246
hta6b4f8392016-03-10 00:24:31 -08001247 // Check that the expected number of frames have arrived.
1248 EXPECT_TRUE_WAIT(FramesHaveArrived(audio_frame_count, video_frame_count),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001249 kMaxWaitForFramesMs);
1250 }
1251
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001252 void SetupAndVerifyDtlsCall() {
1253 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1254 FakeConstraints setup_constraints;
1255 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1256 true);
1257 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1258 LocalP2PTest();
1259 VerifyRenderedSize(640, 480);
1260 }
1261
1262 PeerConnectionTestClient* CreateDtlsClientWithAlternateKey() {
1263 FakeConstraints setup_constraints;
1264 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1265 true);
1266
kwibergd1fe2812016-04-27 06:47:29 -07001267 std::unique_ptr<FakeDtlsIdentityStore> dtls_identity_store(
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001268 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
1269 : nullptr);
1270 dtls_identity_store->use_alternate_key();
1271
1272 // Make sure the new client is using a different certificate.
1273 return PeerConnectionTestClient::CreateClientWithDtlsIdentityStore(
kwiberg0eb15ed2015-12-17 03:04:15 -08001274 "New Peer: ", &setup_constraints, nullptr,
perkj8aba9972016-04-10 23:54:34 -07001275 std::move(dtls_identity_store), prefer_constraint_apis_,
danilchape9021a32016-05-17 01:52:02 -07001276 network_thread_.get(), worker_thread_.get());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001277 }
1278
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001279 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1280 // Messages may get lost on the unreliable DataChannel, so we send multiple
1281 // times to avoid test flakiness.
1282 static const size_t kSendAttempts = 5;
1283
1284 for (size_t i = 0; i < kSendAttempts; ++i) {
1285 dc->Send(DataBuffer(data));
1286 }
1287 }
1288
deadbeefaf1b59c2015-10-15 12:08:41 -07001289 PeerConnectionTestClient* initializing_client() {
1290 return initiating_client_.get();
1291 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001292
1293 // Set the |initiating_client_| to the |client| passed in and return the
1294 // original |initiating_client_|.
1295 PeerConnectionTestClient* set_initializing_client(
1296 PeerConnectionTestClient* client) {
1297 PeerConnectionTestClient* old = initiating_client_.release();
1298 initiating_client_.reset(client);
1299 return old;
1300 }
1301
deadbeefaf1b59c2015-10-15 12:08:41 -07001302 PeerConnectionTestClient* receiving_client() {
1303 return receiving_client_.get();
1304 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001305
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001306 // Set the |receiving_client_| to the |client| passed in and return the
1307 // original |receiving_client_|.
1308 PeerConnectionTestClient* set_receiving_client(
1309 PeerConnectionTestClient* client) {
1310 PeerConnectionTestClient* old = receiving_client_.release();
1311 receiving_client_.reset(client);
1312 return old;
1313 }
1314
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315 private:
perkj8aba9972016-04-10 23:54:34 -07001316 // |worker_thread_| is used by both |initiating_client_| and
1317 // |receiving_client_|. Must be destroyed last.
danilchape9021a32016-05-17 01:52:02 -07001318 std::unique_ptr<rtc::Thread> network_thread_;
1319 std::unique_ptr<rtc::Thread> worker_thread_;
kwibergd1fe2812016-04-27 06:47:29 -07001320 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
1321 std::unique_ptr<rtc::VirtualSocketServer> ss_;
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001322 rtc::SocketServerScope ss_scope_;
kwibergd1fe2812016-04-27 06:47:29 -07001323 std::unique_ptr<PeerConnectionTestClient> initiating_client_;
1324 std::unique_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.
kwibergd1fe2812016-04-27 06:47:29 -07001420 std::unique_ptr<PeerConnectionTestClient> original_peer(
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001421 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.
kwibergd1fe2812016-04-27 06:47:29 -07001458 std::unique_ptr<PeerConnectionTestClient> original_peer(
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001459 set_receiving_client(CreateDtlsClientWithAlternateKey()));
1460 original_peer->pc()->Close();
1461
1462 SetSignalingReceivers();
1463 initializing_client()->IceRestart();
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -07001464 LocalP2PTest();
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001465 VerifyRenderedSize(640, 480);
1466}
1467
perkjcaafdba2016-03-20 07:34:29 -07001468TEST_F(P2PTestConductor, LocalP2PTestCVO) {
1469 ASSERT_TRUE(CreateTestClients());
1470 SetCaptureRotation(webrtc::kVideoRotation_90);
1471 LocalP2PTest();
1472 VerifyRenderedSize(640, 480, webrtc::kVideoRotation_90);
1473}
1474
1475TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportCVO) {
1476 ASSERT_TRUE(CreateTestClients());
1477 SetCaptureRotation(webrtc::kVideoRotation_90);
1478 receiving_client()->RemoveCvoFromReceivedSdp(true);
1479 LocalP2PTest();
1480 VerifyRenderedSize(480, 640, webrtc::kVideoRotation_0);
1481}
1482
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483// This test sets up a call between two endpoints that are configured to use
1484// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1485// negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001486TEST_F(P2PTestConductor, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001487 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001488 FakeConstraints setup_constraints;
1489 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1490 true);
1491 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1492 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1493 LocalP2PTest();
1494 VerifyRenderedSize(640, 480);
1495}
1496
1497// This test sets up a Jsep call between two parties, and the callee only
1498// accept to receive video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001499TEST_F(P2PTestConductor, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001500 ASSERT_TRUE(CreateTestClients());
1501 receiving_client()->SetReceiveAudioVideo(false, true);
1502 LocalP2PTest();
1503}
1504
1505// This test sets up a Jsep call between two parties, and the callee only
1506// accept to receive audio.
deadbeef7c73bdb2015-12-10 15:10:44 -08001507TEST_F(P2PTestConductor, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001508 ASSERT_TRUE(CreateTestClients());
1509 receiving_client()->SetReceiveAudioVideo(true, false);
1510 LocalP2PTest();
1511}
1512
1513// This test sets up a Jsep call between two parties, and the callee reject both
1514// audio and video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001515TEST_F(P2PTestConductor, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001516 ASSERT_TRUE(CreateTestClients());
1517 receiving_client()->SetReceiveAudioVideo(false, false);
1518 LocalP2PTest();
1519}
1520
1521// This test sets up an audio and video call between two parties. After the call
1522// runs for a while (10 frames), the caller sends an update offer with video
1523// being rejected. Once the re-negotiation is done, the video flow should stop
1524// and the audio flow should continue.
deadbeefc9be0072015-12-14 18:27:57 -08001525TEST_F(P2PTestConductor, UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001526 ASSERT_TRUE(CreateTestClients());
1527 LocalP2PTest();
1528 TestUpdateOfferWithRejectedContent();
1529}
1530
1531// This test sets up a Jsep call between two parties. The MSID is removed from
1532// the SDP strings from the caller.
deadbeefc9be0072015-12-14 18:27:57 -08001533TEST_F(P2PTestConductor, LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001534 ASSERT_TRUE(CreateTestClients());
1535 receiving_client()->RemoveMsidFromReceivedSdp(true);
1536 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1537 // audio and video is muxed when MSID is disabled. Remove
1538 // SetRemoveBundleFromSdp once
1539 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1540 receiving_client()->RemoveBundleFromReceivedSdp(true);
1541 LocalP2PTest();
1542}
1543
1544// This test sets up a Jsep call between two parties and the initiating peer
1545// sends two steams.
1546// TODO(perkj): Disabled due to
1547// https://code.google.com/p/webrtc/issues/detail?id=1454
deadbeef7c73bdb2015-12-10 15:10:44 -08001548TEST_F(P2PTestConductor, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549 ASSERT_TRUE(CreateTestClients());
1550 // Set optional video constraint to max 320pixels to decrease CPU usage.
1551 FakeConstraints constraint;
1552 constraint.SetOptionalMaxWidth(320);
1553 SetVideoConstraints(constraint, constraint);
1554 initializing_client()->AddMediaStream(true, true);
1555 initializing_client()->AddMediaStream(false, true);
1556 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1557 LocalP2PTest();
1558 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1559}
1560
1561// Test that we can receive the audio output level from a remote audio track.
deadbeef7c73bdb2015-12-10 15:10:44 -08001562TEST_F(P2PTestConductor, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001563 ASSERT_TRUE(CreateTestClients());
1564 LocalP2PTest();
1565
1566 StreamCollectionInterface* remote_streams =
1567 initializing_client()->remote_streams();
1568 ASSERT_GT(remote_streams->count(), 0u);
1569 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1570 MediaStreamTrackInterface* remote_audio_track =
1571 remote_streams->at(0)->GetAudioTracks()[0];
1572
1573 // Get the audio output level stats. Note that the level is not available
1574 // until a RTCP packet has been received.
1575 EXPECT_TRUE_WAIT(
1576 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1577 kMaxWaitForStatsMs);
1578}
1579
1580// Test that an audio input level is reported.
deadbeef7c73bdb2015-12-10 15:10:44 -08001581TEST_F(P2PTestConductor, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 ASSERT_TRUE(CreateTestClients());
1583 LocalP2PTest();
1584
1585 // Get the audio input level stats. The level should be available very
1586 // soon after the test starts.
1587 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1588 kMaxWaitForStatsMs);
1589}
1590
1591// Test that we can get incoming byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001592TEST_F(P2PTestConductor, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593 ASSERT_TRUE(CreateTestClients());
1594 LocalP2PTest();
1595
1596 StreamCollectionInterface* remote_streams =
1597 initializing_client()->remote_streams();
1598 ASSERT_GT(remote_streams->count(), 0u);
1599 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1600 MediaStreamTrackInterface* remote_audio_track =
1601 remote_streams->at(0)->GetAudioTracks()[0];
1602 EXPECT_TRUE_WAIT(
1603 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1604 kMaxWaitForStatsMs);
1605
1606 MediaStreamTrackInterface* remote_video_track =
1607 remote_streams->at(0)->GetVideoTracks()[0];
1608 EXPECT_TRUE_WAIT(
1609 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1610 kMaxWaitForStatsMs);
1611}
1612
1613// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001614TEST_F(P2PTestConductor, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001615 ASSERT_TRUE(CreateTestClients());
1616 LocalP2PTest();
1617
1618 StreamCollectionInterface* local_streams =
1619 initializing_client()->local_streams();
1620 ASSERT_GT(local_streams->count(), 0u);
1621 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1622 MediaStreamTrackInterface* local_audio_track =
1623 local_streams->at(0)->GetAudioTracks()[0];
1624 EXPECT_TRUE_WAIT(
1625 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1626 kMaxWaitForStatsMs);
1627
1628 MediaStreamTrackInterface* local_video_track =
1629 local_streams->at(0)->GetVideoTracks()[0];
1630 EXPECT_TRUE_WAIT(
1631 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1632 kMaxWaitForStatsMs);
1633}
1634
Joachim Bauch04e5b492015-05-29 09:40:39 +02001635// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001636TEST_F(P2PTestConductor, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001637 PeerConnectionFactory::Options init_options;
1638 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1639 PeerConnectionFactory::Options recv_options;
1640 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001641 ASSERT_TRUE(
1642 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001643 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1644 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1645 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001646 LocalP2PTest();
1647
torbjorng43166b82016-03-11 00:06:47 -08001648 EXPECT_TRUE_WAIT(
1649 rtc::SSLStreamAdapter::IsAcceptableCipher(
1650 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1651 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001652 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001653 initializing_client()->GetSrtpCipherStats(),
1654 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001655 EXPECT_EQ(1,
1656 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1657 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001658}
1659
1660// Test that DTLS 1.2 is used if both ends support it.
torbjorng79a5a832016-01-15 07:16:51 -08001661TEST_F(P2PTestConductor, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001662 PeerConnectionFactory::Options init_options;
1663 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1664 PeerConnectionFactory::Options recv_options;
1665 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001666 ASSERT_TRUE(
1667 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001668 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1669 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1670 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001671 LocalP2PTest();
1672
torbjorng43166b82016-03-11 00:06:47 -08001673 EXPECT_TRUE_WAIT(
1674 rtc::SSLStreamAdapter::IsAcceptableCipher(
1675 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1676 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001677 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001678 initializing_client()->GetSrtpCipherStats(),
1679 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001680 EXPECT_EQ(1,
1681 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1682 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001683}
1684
1685// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1686// received supports 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001687TEST_F(P2PTestConductor, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001688 PeerConnectionFactory::Options init_options;
1689 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1690 PeerConnectionFactory::Options recv_options;
1691 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001692 ASSERT_TRUE(
1693 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001694 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1695 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1696 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001697 LocalP2PTest();
1698
torbjorng43166b82016-03-11 00:06:47 -08001699 EXPECT_TRUE_WAIT(
1700 rtc::SSLStreamAdapter::IsAcceptableCipher(
1701 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1702 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001703 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001704 initializing_client()->GetSrtpCipherStats(),
1705 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001706 EXPECT_EQ(1,
1707 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1708 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001709}
1710
1711// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1712// received supports 1.2.
torbjorng43166b82016-03-11 00:06:47 -08001713TEST_F(P2PTestConductor, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001714 PeerConnectionFactory::Options init_options;
1715 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1716 PeerConnectionFactory::Options recv_options;
1717 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001718 ASSERT_TRUE(
1719 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001720 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1721 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1722 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001723 LocalP2PTest();
1724
torbjorng43166b82016-03-11 00:06:47 -08001725 EXPECT_TRUE_WAIT(
1726 rtc::SSLStreamAdapter::IsAcceptableCipher(
1727 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1728 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001729 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001730 initializing_client()->GetSrtpCipherStats(),
1731 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001732 EXPECT_EQ(1,
1733 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1734 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001735}
1736
deadbeefb5cb19b2015-11-23 16:39:12 -08001737// This test sets up a call between two parties with audio, video and an RTP
1738// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001739TEST_F(P2PTestConductor, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001740 FakeConstraints setup_constraints;
1741 setup_constraints.SetAllowRtpDataChannels();
1742 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1743 initializing_client()->CreateDataChannel();
1744 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001745 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1746 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1748 kMaxWaitMs);
1749 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1750 kMaxWaitMs);
1751
1752 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001753
1754 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1756 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001757
1758 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1760 kMaxWaitMs);
1761
1762 receiving_client()->data_channel()->Close();
1763 // Send new offer and answer.
1764 receiving_client()->Negotiate();
1765 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1766 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1767}
1768
deadbeefb5cb19b2015-11-23 16:39:12 -08001769// This test sets up a call between two parties with audio, video and an SCTP
1770// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001771TEST_F(P2PTestConductor, LocalP2PTestSctpDataChannel) {
deadbeefb5cb19b2015-11-23 16:39:12 -08001772 ASSERT_TRUE(CreateTestClients());
1773 initializing_client()->CreateDataChannel();
1774 LocalP2PTest();
1775 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1776 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
1777 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1778 kMaxWaitMs);
1779 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
1780
1781 std::string data = "hello world";
1782
1783 initializing_client()->data_channel()->Send(DataBuffer(data));
1784 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1785 kMaxWaitMs);
1786
1787 receiving_client()->data_channel()->Send(DataBuffer(data));
1788 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1789 kMaxWaitMs);
1790
1791 receiving_client()->data_channel()->Close();
deadbeef15887932015-12-14 19:32:34 -08001792 EXPECT_TRUE_WAIT(!initializing_client()->data_observer()->IsOpen(),
1793 kMaxWaitMs);
1794 EXPECT_TRUE_WAIT(!receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
deadbeefb5cb19b2015-11-23 16:39:12 -08001795}
1796
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797// This test sets up a call between two parties and creates a data channel.
1798// The test tests that received data is buffered unless an observer has been
1799// registered.
1800// Rtp data channels can receive data before the underlying
1801// transport has detected that a channel is writable and thus data can be
1802// received before the data channel state changes to open. That is hard to test
1803// but the same buffering is used in that case.
deadbeef7c73bdb2015-12-10 15:10:44 -08001804TEST_F(P2PTestConductor, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805 FakeConstraints setup_constraints;
1806 setup_constraints.SetAllowRtpDataChannels();
1807 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1808 initializing_client()->CreateDataChannel();
1809 initializing_client()->Negotiate();
1810
deadbeefaf1b59c2015-10-15 12:08:41 -07001811 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1812 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1814 kMaxWaitMs);
1815 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1816 receiving_client()->data_channel()->state(), kMaxWaitMs);
1817
1818 // Unregister the existing observer.
1819 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001820
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001821 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001822 SendRtpData(initializing_client()->data_channel(), data);
1823
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824 // Wait a while to allow the sent data to arrive before an observer is
1825 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001826 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001827
1828 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1829 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1830}
1831
1832// This test sets up a call between two parties with audio, video and but only
1833// the initiating client support data.
deadbeef7c73bdb2015-12-10 15:10:44 -08001834TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001835 FakeConstraints setup_constraints_1;
1836 setup_constraints_1.SetAllowRtpDataChannels();
1837 // Must disable DTLS to make negotiation succeed.
1838 setup_constraints_1.SetMandatory(
1839 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1840 FakeConstraints setup_constraints_2;
1841 setup_constraints_2.SetMandatory(
1842 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1843 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844 initializing_client()->CreateDataChannel();
1845 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001846 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847 EXPECT_FALSE(receiving_client()->data_channel());
1848 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1849}
1850
1851// This test sets up a call between two parties with audio, video. When audio
1852// and video is setup and flowing and data channel is negotiated.
deadbeef7c73bdb2015-12-10 15:10:44 -08001853TEST_F(P2PTestConductor, AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854 FakeConstraints setup_constraints;
1855 setup_constraints.SetAllowRtpDataChannels();
1856 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1857 LocalP2PTest();
1858 initializing_client()->CreateDataChannel();
1859 // Send new offer and answer.
1860 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07001861 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1862 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001863 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1864 kMaxWaitMs);
1865 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1866 kMaxWaitMs);
1867}
1868
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001869// This test sets up a Jsep call with SCTP DataChannel and verifies the
1870// negotiation is completed without error.
1871#ifdef HAVE_SCTP
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001872TEST_F(P2PTestConductor, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001873 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001874 FakeConstraints constraints;
1875 constraints.SetMandatory(
1876 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1877 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1878 initializing_client()->CreateDataChannel();
1879 initializing_client()->Negotiate(false, false);
1880}
1881#endif
1882
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001883// This test sets up a call between two parties with audio, and video.
1884// During the call, the initializing side restart ice and the test verifies that
1885// new ice candidates are generated and audio and video still can flow.
deadbeef7c73bdb2015-12-10 15:10:44 -08001886TEST_F(P2PTestConductor, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887 ASSERT_TRUE(CreateTestClients());
1888
1889 // Negotiate and wait for ice completion and make sure audio and video plays.
1890 LocalP2PTest();
1891
1892 // Create a SDP string of the first audio candidate for both clients.
1893 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1894 initializing_client()->pc()->local_description()->candidates(0);
1895 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1896 receiving_client()->pc()->local_description()->candidates(0);
1897 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1898 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1899 std::string initiator_candidate;
1900 EXPECT_TRUE(
1901 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1902 std::string receiver_candidate;
1903 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1904
1905 // Restart ice on the initializing client.
1906 receiving_client()->SetExpectIceRestart(true);
1907 initializing_client()->IceRestart();
1908
1909 // Negotiate and wait for ice completion again and make sure audio and video
1910 // plays.
1911 LocalP2PTest();
1912
1913 // Create a SDP string of the first audio candidate for both clients again.
1914 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1915 initializing_client()->pc()->local_description()->candidates(0);
1916 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1917 receiving_client()->pc()->local_description()->candidates(0);
1918 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1919 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1920 std::string initiator_candidate_restart;
1921 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1922 &initiator_candidate_restart));
1923 std::string receiver_candidate_restart;
1924 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1925 &receiver_candidate_restart));
1926
1927 // Verify that the first candidates in the local session descriptions has
1928 // changed.
1929 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1930 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1931}
1932
deadbeeffaac4972015-11-12 15:33:07 -08001933// This test sets up a call between two parties with audio, and video.
1934// It then renegotiates setting the video m-line to "port 0", then later
1935// renegotiates again, enabling video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001936TEST_F(P2PTestConductor, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08001937 ASSERT_TRUE(CreateTestClients());
1938
1939 // Do initial negotiation. Will result in video and audio sendonly m-lines.
1940 receiving_client()->set_auto_add_stream(false);
1941 initializing_client()->AddMediaStream(true, true);
1942 initializing_client()->Negotiate();
1943
1944 // Negotiate again, disabling the video m-line (receiving client will
1945 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
1946 receiving_client()->SetReceiveVideo(false);
1947 initializing_client()->Negotiate();
1948
1949 // Enable video and do negotiation again, making sure video is received
1950 // end-to-end.
1951 receiving_client()->SetReceiveVideo(true);
1952 receiving_client()->AddMediaStream(true, true);
1953 LocalP2PTest();
1954}
1955
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001956// This test sets up a Jsep call between two parties with external
1957// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001958// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1959// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001960TEST_F(P2PTestConductor, DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961 ASSERT_TRUE(CreateTestClients());
1962 EnableVideoDecoderFactory();
1963 LocalP2PTest();
1964}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001965
deadbeeffac06552015-11-25 11:26:01 -08001966// This tests that if we negotiate after calling CreateSender but before we
1967// have a track, then set a track later, frames from the newly-set track are
1968// received end-to-end.
deadbeef7c73bdb2015-12-10 15:10:44 -08001969TEST_F(P2PTestConductor, EarlyWarmupTest) {
deadbeeffac06552015-11-25 11:26:01 -08001970 ASSERT_TRUE(CreateTestClients());
deadbeefbd7d8f72015-12-18 16:58:44 -08001971 auto audio_sender =
1972 initializing_client()->pc()->CreateSender("audio", "stream_id");
1973 auto video_sender =
1974 initializing_client()->pc()->CreateSender("video", "stream_id");
deadbeeffac06552015-11-25 11:26:01 -08001975 initializing_client()->Negotiate();
1976 // Wait for ICE connection to complete, without any tracks.
1977 // Note that the receiving client WILL (in HandleIncomingOffer) create
1978 // tracks, so it's only the initiator here that's doing early warmup.
1979 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1980 VerifySessionDescriptions();
1981 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1982 initializing_client()->ice_connection_state(),
1983 kMaxWaitForFramesMs);
1984 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1985 receiving_client()->ice_connection_state(),
1986 kMaxWaitForFramesMs);
1987 // Now set the tracks, and expect frames to immediately start flowing.
1988 EXPECT_TRUE(
1989 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
1990 EXPECT_TRUE(
1991 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
hta6b4f8392016-03-10 00:24:31 -08001992 EXPECT_TRUE_WAIT(FramesHaveArrived(kEndAudioFrameCount, kEndVideoFrameCount),
deadbeeffac06552015-11-25 11:26:01 -08001993 kMaxWaitForFramesMs);
1994}
1995
nissed98cf1f2016-04-22 07:27:36 -07001996TEST_F(P2PTestConductor, ForwardVideoOnlyStream) {
1997 ASSERT_TRUE(CreateTestClients());
1998 // One-way stream
1999 receiving_client()->set_auto_add_stream(false);
2000 // Video only, audio forwarding not expected to work.
2001 initializing_client()->AddMediaStream(false, true);
2002 initializing_client()->Negotiate();
2003
2004 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
2005 VerifySessionDescriptions();
2006
2007 ASSERT_TRUE(initializing_client()->can_receive_video());
2008 ASSERT_TRUE(receiving_client()->can_receive_video());
2009
2010 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2011 initializing_client()->ice_connection_state(),
2012 kMaxWaitForFramesMs);
2013 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2014 receiving_client()->ice_connection_state(),
2015 kMaxWaitForFramesMs);
2016
2017 ASSERT_TRUE(receiving_client()->remote_streams()->count() == 1);
2018
2019 // Echo the stream back.
2020 receiving_client()->pc()->AddStream(
2021 receiving_client()->remote_streams()->at(0));
2022 receiving_client()->Negotiate();
2023
2024 EXPECT_TRUE_WAIT(
2025 initializing_client()->VideoFramesReceivedCheck(kEndVideoFrameCount),
2026 kMaxWaitForFramesMs);
2027}
2028
deadbeef0a6c4ca2015-10-06 11:38:28 -07002029class IceServerParsingTest : public testing::Test {
2030 public:
2031 // Convenience for parsing a single URL.
2032 bool ParseUrl(const std::string& url) {
2033 return ParseUrl(url, std::string(), std::string());
2034 }
2035
2036 bool ParseUrl(const std::string& url,
2037 const std::string& username,
2038 const std::string& password) {
2039 PeerConnectionInterface::IceServers servers;
2040 PeerConnectionInterface::IceServer server;
2041 server.urls.push_back(url);
2042 server.username = username;
2043 server.password = password;
2044 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002045 return webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002046 }
2047
2048 protected:
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002049 cricket::ServerAddresses stun_servers_;
2050 std::vector<cricket::RelayServerConfig> turn_servers_;
deadbeef0a6c4ca2015-10-06 11:38:28 -07002051};
2052
2053// Make sure all STUN/TURN prefixes are parsed correctly.
2054TEST_F(IceServerParsingTest, ParseStunPrefixes) {
2055 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002056 EXPECT_EQ(1U, stun_servers_.size());
2057 EXPECT_EQ(0U, turn_servers_.size());
2058 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002059
2060 EXPECT_TRUE(ParseUrl("stuns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002061 EXPECT_EQ(1U, stun_servers_.size());
2062 EXPECT_EQ(0U, turn_servers_.size());
2063 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002064
2065 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002066 EXPECT_EQ(0U, stun_servers_.size());
2067 EXPECT_EQ(1U, turn_servers_.size());
2068 EXPECT_FALSE(turn_servers_[0].ports[0].secure);
2069 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002070
2071 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002072 EXPECT_EQ(0U, stun_servers_.size());
2073 EXPECT_EQ(1U, turn_servers_.size());
2074 EXPECT_TRUE(turn_servers_[0].ports[0].secure);
2075 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002076
2077 // invalid prefixes
2078 EXPECT_FALSE(ParseUrl("stunn:hostname"));
2079 EXPECT_FALSE(ParseUrl(":hostname"));
2080 EXPECT_FALSE(ParseUrl(":"));
2081 EXPECT_FALSE(ParseUrl(""));
2082}
2083
2084TEST_F(IceServerParsingTest, VerifyDefaults) {
2085 // TURNS defaults
2086 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002087 EXPECT_EQ(1U, turn_servers_.size());
2088 EXPECT_EQ(5349, turn_servers_[0].ports[0].address.port());
2089 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
2090 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002091
2092 // TURN defaults
2093 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002094 EXPECT_EQ(1U, turn_servers_.size());
2095 EXPECT_EQ(3478, turn_servers_[0].ports[0].address.port());
2096 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2097 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002098
2099 // STUN defaults
2100 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002101 EXPECT_EQ(1U, stun_servers_.size());
2102 EXPECT_EQ(3478, stun_servers_.begin()->port());
2103 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002104}
2105
2106// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
2107// can be parsed correctly.
2108TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
2109 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002110 EXPECT_EQ(1U, stun_servers_.size());
2111 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2112 EXPECT_EQ(1234, stun_servers_.begin()->port());
2113 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002114
2115 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002116 EXPECT_EQ(1U, stun_servers_.size());
2117 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2118 EXPECT_EQ(4321, stun_servers_.begin()->port());
2119 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002120
2121 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002122 EXPECT_EQ(1U, stun_servers_.size());
2123 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2124 EXPECT_EQ(9999, stun_servers_.begin()->port());
2125 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002126
2127 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002128 EXPECT_EQ(1U, stun_servers_.size());
2129 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2130 EXPECT_EQ(3478, stun_servers_.begin()->port());
2131 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002132
2133 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002134 EXPECT_EQ(1U, stun_servers_.size());
2135 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2136 EXPECT_EQ(3478, stun_servers_.begin()->port());
2137 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002138
2139 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002140 EXPECT_EQ(1U, stun_servers_.size());
2141 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2142 EXPECT_EQ(3478, stun_servers_.begin()->port());
2143 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002144
2145 // Try some invalid hostname:port strings.
2146 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
2147 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002148 EXPECT_FALSE(ParseUrl("stun:hostname:port:more"));
2149 EXPECT_FALSE(ParseUrl("stun:hostname:port more"));
deadbeef0a6c4ca2015-10-06 11:38:28 -07002150 EXPECT_FALSE(ParseUrl("stun:hostname:"));
2151 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
2152 EXPECT_FALSE(ParseUrl("stun::5555"));
2153 EXPECT_FALSE(ParseUrl("stun:"));
2154}
2155
2156// Test parsing the "?transport=xxx" part of the URL.
2157TEST_F(IceServerParsingTest, ParseTransport) {
2158 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002159 EXPECT_EQ(1U, turn_servers_.size());
2160 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
2161 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002162
2163 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002164 EXPECT_EQ(1U, turn_servers_.size());
2165 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2166 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002167
2168 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
2169}
2170
2171// Test parsing ICE username contained in URL.
2172TEST_F(IceServerParsingTest, ParseUsername) {
2173 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002174 EXPECT_EQ(1U, turn_servers_.size());
2175 EXPECT_EQ("user", turn_servers_[0].credentials.username);
2176 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002177
2178 EXPECT_FALSE(ParseUrl("turn:@hostname"));
2179 EXPECT_FALSE(ParseUrl("turn:username@"));
2180 EXPECT_FALSE(ParseUrl("turn:@"));
2181 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
2182}
2183
2184// Test that username and password from IceServer is copied into the resulting
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002185// RelayServerConfig.
deadbeef0a6c4ca2015-10-06 11:38:28 -07002186TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
2187 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002188 EXPECT_EQ(1U, turn_servers_.size());
2189 EXPECT_EQ("username", turn_servers_[0].credentials.username);
2190 EXPECT_EQ("password", turn_servers_[0].credentials.password);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002191}
2192
2193// Ensure that if a server has multiple URLs, each one is parsed.
2194TEST_F(IceServerParsingTest, ParseMultipleUrls) {
2195 PeerConnectionInterface::IceServers servers;
2196 PeerConnectionInterface::IceServer server;
2197 server.urls.push_back("stun:hostname");
2198 server.urls.push_back("turn:hostname");
2199 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002200 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2201 EXPECT_EQ(1U, stun_servers_.size());
2202 EXPECT_EQ(1U, turn_servers_.size());
deadbeef0a6c4ca2015-10-06 11:38:28 -07002203}
2204
Taylor Brandstetter893505d2016-01-07 15:12:48 -08002205// Ensure that TURN servers are given unique priorities,
2206// so that their resulting candidates have unique priorities.
2207TEST_F(IceServerParsingTest, TurnServerPrioritiesUnique) {
2208 PeerConnectionInterface::IceServers servers;
2209 PeerConnectionInterface::IceServer server;
2210 server.urls.push_back("turn:hostname");
2211 server.urls.push_back("turn:hostname2");
2212 servers.push_back(server);
2213 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2214 EXPECT_EQ(2U, turn_servers_.size());
2215 EXPECT_NE(turn_servers_[0].priority, turn_servers_[1].priority);
2216}
2217
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00002218#endif // if !defined(THREAD_SANITIZER)
hta6b4f8392016-03-10 00:24:31 -08002219
2220} // namespace