blob: 99be22f4591ad70e9c232463a5ec0b46d26bfd42 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
11#include <stdio.h>
12
13#include <algorithm>
14#include <list>
15#include <map>
kwiberg0eb15ed2015-12-17 03:04:15 -080016#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include <vector>
18
Henrik Kjellander15583c12016-02-10 10:53:12 +010019#include "webrtc/api/dtmfsender.h"
20#include "webrtc/api/fakemetricsobserver.h"
21#include "webrtc/api/localaudiosource.h"
22#include "webrtc/api/mediastreaminterface.h"
23#include "webrtc/api/peerconnection.h"
24#include "webrtc/api/peerconnectionfactory.h"
25#include "webrtc/api/peerconnectioninterface.h"
26#include "webrtc/api/test/fakeaudiocapturemodule.h"
27#include "webrtc/api/test/fakeconstraints.h"
28#include "webrtc/api/test/fakedtlsidentitystore.h"
29#include "webrtc/api/test/fakeperiodicvideocapturer.h"
30#include "webrtc/api/test/fakevideotrackrenderer.h"
31#include "webrtc/api/test/mockpeerconnectionobservers.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000032#include "webrtc/base/gunit.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000033#include "webrtc/base/physicalsocketserver.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/scoped_ptr.h"
35#include "webrtc/base/ssladapter.h"
36#include "webrtc/base/sslstreamadapter.h"
37#include "webrtc/base/thread.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000038#include "webrtc/base/virtualsocketserver.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010039#include "webrtc/media/engine/fakewebrtcvideoengine.h"
kjellanderf4752772016-03-02 05:42:30 -080040#include "webrtc/p2p/base/p2pconstants.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000041#include "webrtc/p2p/base/sessiondescription.h"
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080042#include "webrtc/p2p/client/fakeportallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010043#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45#define MAYBE_SKIP_TEST(feature) \
46 if (!(feature())) { \
47 LOG(LS_INFO) << "Feature disabled... skipping"; \
48 return; \
49 }
50
51using cricket::ContentInfo;
52using cricket::FakeWebRtcVideoDecoder;
53using cricket::FakeWebRtcVideoDecoderFactory;
54using cricket::FakeWebRtcVideoEncoder;
55using cricket::FakeWebRtcVideoEncoderFactory;
56using cricket::MediaContentDescription;
57using webrtc::DataBuffer;
58using webrtc::DataChannelInterface;
59using webrtc::DtmfSender;
60using webrtc::DtmfSenderInterface;
61using webrtc::DtmfSenderObserverInterface;
62using webrtc::FakeConstraints;
63using webrtc::MediaConstraintsInterface;
deadbeeffaac4972015-11-12 15:33:07 -080064using webrtc::MediaStreamInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065using webrtc::MediaStreamTrackInterface;
66using webrtc::MockCreateSessionDescriptionObserver;
67using webrtc::MockDataChannelObserver;
68using webrtc::MockSetSessionDescriptionObserver;
69using webrtc::MockStatsObserver;
deadbeeffaac4972015-11-12 15:33:07 -080070using webrtc::ObserverInterface;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000071using webrtc::PeerConnectionInterface;
Joachim Bauch04e5b492015-05-29 09:40:39 +020072using webrtc::PeerConnectionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073using webrtc::SessionDescriptionInterface;
74using webrtc::StreamCollectionInterface;
75
hta6b4f8392016-03-10 00:24:31 -080076namespace {
77
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000078static const int kMaxWaitMs = 10000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000079// Disable for TSan v2, see
80// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
81// This declaration is also #ifdef'd as it causes uninitialized-variable
82// warnings.
83#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084static const int kMaxWaitForStatsMs = 3000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000085#endif
deadbeeffac06552015-11-25 11:26:01 -080086static const int kMaxWaitForActivationMs = 5000;
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000087static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088static const int kEndAudioFrameCount = 3;
89static const int kEndVideoFrameCount = 3;
90
91static const char kStreamLabelBase[] = "stream_label";
92static const char kVideoTrackLabelBase[] = "video_track";
93static const char kAudioTrackLabelBase[] = "audio_track";
94static const char kDataChannelLabel[] = "data_channel";
95
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +000096// Disable for TSan v2, see
97// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
98// This declaration is also #ifdef'd as it causes unused-variable errors.
99#if !defined(THREAD_SANITIZER)
100// SRTP cipher name negotiated by the tests. This must be updated if the
101// default changes.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800102static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000103#endif
104
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105static void RemoveLinesFromSdp(const std::string& line_start,
106 std::string* sdp) {
107 const char kSdpLineEnd[] = "\r\n";
108 size_t ssrc_pos = 0;
109 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
110 std::string::npos) {
111 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
112 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
113 }
114}
115
hta6b4f8392016-03-10 00:24:31 -0800116bool StreamsHaveAudioTrack(StreamCollectionInterface* streams) {
117 for (size_t idx = 0; idx < streams->count(); idx++) {
118 auto stream = streams->at(idx);
119 if (stream->GetAudioTracks().size() > 0) {
120 return true;
121 }
122 }
123 return false;
124}
125
126bool StreamsHaveVideoTrack(StreamCollectionInterface* streams) {
127 for (size_t idx = 0; idx < streams->count(); idx++) {
128 auto stream = streams->at(idx);
129 if (stream->GetVideoTracks().size() > 0) {
130 return true;
131 }
132 }
133 return false;
134}
135
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136class SignalingMessageReceiver {
137 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138 virtual void ReceiveSdpMessage(const std::string& type,
139 std::string& msg) = 0;
140 virtual void ReceiveIceMessage(const std::string& sdp_mid,
141 int sdp_mline_index,
142 const std::string& msg) = 0;
143
144 protected:
deadbeefaf1b59c2015-10-15 12:08:41 -0700145 SignalingMessageReceiver() {}
146 virtual ~SignalingMessageReceiver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147};
148
deadbeefaf1b59c2015-10-15 12:08:41 -0700149class PeerConnectionTestClient : public webrtc::PeerConnectionObserver,
deadbeeffaac4972015-11-12 15:33:07 -0800150 public SignalingMessageReceiver,
151 public ObserverInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 public:
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800153 static PeerConnectionTestClient* CreateClientWithDtlsIdentityStore(
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800154 const std::string& id,
155 const MediaConstraintsInterface* constraints,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800156 const PeerConnectionFactory::Options* options,
htaaac2dea2016-03-10 13:35:55 -0800157 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store,
perkj61724012016-04-06 00:02:59 -0700158 bool prefer_constraint_apis,
159 rtc::Thread* worker_thread) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800160 PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
htaaac2dea2016-03-10 13:35:55 -0800161 if (!client->Init(constraints, options, std::move(dtls_identity_store),
perkj61724012016-04-06 00:02:59 -0700162 prefer_constraint_apis, worker_thread)) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800163 delete client;
164 return nullptr;
165 }
166 return client;
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800167 }
168
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800169 static PeerConnectionTestClient* CreateClient(
170 const std::string& id,
171 const MediaConstraintsInterface* constraints,
perkj61724012016-04-06 00:02:59 -0700172 const PeerConnectionFactory::Options* options,
173 rtc::Thread* worker_thread) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800174 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
175 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
176 : nullptr);
177
perkj61724012016-04-06 00:02:59 -0700178 return CreateClientWithDtlsIdentityStore(id, constraints, options,
179 std::move(dtls_identity_store),
180 true, worker_thread);
htaaac2dea2016-03-10 13:35:55 -0800181 }
182
183 static PeerConnectionTestClient* CreateClientPreferNoConstraints(
184 const std::string& id,
perkj61724012016-04-06 00:02:59 -0700185 const PeerConnectionFactory::Options* options,
186 rtc::Thread* worker_thread) {
htaaac2dea2016-03-10 13:35:55 -0800187 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
188 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
189 : nullptr);
190
perkj61724012016-04-06 00:02:59 -0700191 return CreateClientWithDtlsIdentityStore(id, nullptr, options,
192 std::move(dtls_identity_store),
193 false, worker_thread);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800194 }
195
deadbeefaf1b59c2015-10-15 12:08:41 -0700196 ~PeerConnectionTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 }
198
deadbeefaf1b59c2015-10-15 12:08:41 -0700199 void Negotiate() { Negotiate(true, true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200
deadbeefaf1b59c2015-10-15 12:08:41 -0700201 void Negotiate(bool audio, bool video) {
202 rtc::scoped_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700203 ASSERT_TRUE(DoCreateOffer(&offer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204
deadbeefaf1b59c2015-10-15 12:08:41 -0700205 if (offer->description()->GetContentByName("audio")) {
206 offer->description()->GetContentByName("audio")->rejected = !audio;
207 }
208 if (offer->description()->GetContentByName("video")) {
209 offer->description()->GetContentByName("video")->rejected = !video;
210 }
211
212 std::string sdp;
213 EXPECT_TRUE(offer->ToString(&sdp));
214 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
215 signaling_message_receiver_->ReceiveSdpMessage(
216 webrtc::SessionDescriptionInterface::kOffer, sdp);
217 }
218
219 // SignalingMessageReceiver callback.
220 void ReceiveSdpMessage(const std::string& type, std::string& msg) override {
221 FilterIncomingSdpMessage(&msg);
222 if (type == webrtc::SessionDescriptionInterface::kOffer) {
223 HandleIncomingOffer(msg);
224 } else {
225 HandleIncomingAnswer(msg);
226 }
227 }
228
229 // SignalingMessageReceiver callback.
230 void ReceiveIceMessage(const std::string& sdp_mid,
231 int sdp_mline_index,
232 const std::string& msg) override {
233 LOG(INFO) << id_ << "ReceiveIceMessage";
234 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
235 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
236 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
237 }
238
239 // PeerConnectionObserver callbacks.
240 void OnSignalingChange(
241 webrtc::PeerConnectionInterface::SignalingState new_state) override {
242 EXPECT_EQ(pc()->signaling_state(), new_state);
243 }
deadbeeffaac4972015-11-12 15:33:07 -0800244 void OnAddStream(MediaStreamInterface* media_stream) override {
245 media_stream->RegisterObserver(this);
deadbeefaf1b59c2015-10-15 12:08:41 -0700246 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
247 const std::string id = media_stream->GetVideoTracks()[i]->id();
248 ASSERT_TRUE(fake_video_renderers_.find(id) ==
249 fake_video_renderers_.end());
deadbeefc9be0072015-12-14 18:27:57 -0800250 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
251 media_stream->GetVideoTracks()[i]));
deadbeefaf1b59c2015-10-15 12:08:41 -0700252 }
253 }
deadbeeffaac4972015-11-12 15:33:07 -0800254 void OnRemoveStream(MediaStreamInterface* media_stream) override {}
deadbeefaf1b59c2015-10-15 12:08:41 -0700255 void OnRenegotiationNeeded() override {}
256 void OnIceConnectionChange(
257 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
258 EXPECT_EQ(pc()->ice_connection_state(), new_state);
259 }
260 void OnIceGatheringChange(
261 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
262 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
263 }
264 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
265 LOG(INFO) << id_ << "OnIceCandidate";
266
267 std::string ice_sdp;
268 EXPECT_TRUE(candidate->ToString(&ice_sdp));
269 if (signaling_message_receiver_ == nullptr) {
270 // Remote party may be deleted.
271 return;
272 }
273 signaling_message_receiver_->ReceiveIceMessage(
274 candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
275 }
276
deadbeeffaac4972015-11-12 15:33:07 -0800277 // MediaStreamInterface callback
278 void OnChanged() override {
279 // Track added or removed from MediaStream, so update our renderers.
280 rtc::scoped_refptr<StreamCollectionInterface> remote_streams =
281 pc()->remote_streams();
282 // Remove renderers for tracks that were removed.
283 for (auto it = fake_video_renderers_.begin();
284 it != fake_video_renderers_.end();) {
285 if (remote_streams->FindVideoTrack(it->first) == nullptr) {
deadbeefc9be0072015-12-14 18:27:57 -0800286 auto to_remove = it++;
287 removed_fake_video_renderers_.push_back(std::move(to_remove->second));
288 fake_video_renderers_.erase(to_remove);
deadbeeffaac4972015-11-12 15:33:07 -0800289 } else {
290 ++it;
291 }
292 }
293 // Create renderers for new video tracks.
294 for (size_t stream_index = 0; stream_index < remote_streams->count();
295 ++stream_index) {
296 MediaStreamInterface* remote_stream = remote_streams->at(stream_index);
297 for (size_t track_index = 0;
298 track_index < remote_stream->GetVideoTracks().size();
299 ++track_index) {
300 const std::string id =
301 remote_stream->GetVideoTracks()[track_index]->id();
302 if (fake_video_renderers_.find(id) != fake_video_renderers_.end()) {
303 continue;
304 }
deadbeefc9be0072015-12-14 18:27:57 -0800305 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
306 remote_stream->GetVideoTracks()[track_index]));
deadbeeffaac4972015-11-12 15:33:07 -0800307 }
308 }
309 }
310
deadbeefaf1b59c2015-10-15 12:08:41 -0700311 void SetVideoConstraints(const webrtc::FakeConstraints& video_constraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312 video_constraints_ = video_constraint;
313 }
314
315 void AddMediaStream(bool audio, bool video) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700316 std::string stream_label =
317 kStreamLabelBase +
318 rtc::ToString<int>(static_cast<int>(pc()->local_streams()->count()));
deadbeeffaac4972015-11-12 15:33:07 -0800319 rtc::scoped_refptr<MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000320 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321
322 if (audio && can_receive_audio()) {
deadbeeffac06552015-11-25 11:26:01 -0800323 stream->AddTrack(CreateLocalAudioTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324 }
325 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000326 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 }
328
deadbeefaf1b59c2015-10-15 12:08:41 -0700329 EXPECT_TRUE(pc()->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330 }
331
deadbeefaf1b59c2015-10-15 12:08:41 -0700332 size_t NumberOfLocalMediaStreams() { return pc()->local_streams()->count(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333
334 bool SessionActive() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700335 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 }
337
deadbeeffaac4972015-11-12 15:33:07 -0800338 // Automatically add a stream when receiving an offer, if we don't have one.
339 // Defaults to true.
340 void set_auto_add_stream(bool auto_add_stream) {
341 auto_add_stream_ = auto_add_stream;
342 }
343
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344 void set_signaling_message_receiver(
deadbeefaf1b59c2015-10-15 12:08:41 -0700345 SignalingMessageReceiver* signaling_message_receiver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346 signaling_message_receiver_ = signaling_message_receiver;
347 }
348
349 void EnableVideoDecoderFactory() {
350 video_decoder_factory_enabled_ = true;
351 fake_video_decoder_factory_->AddSupportedVideoCodecType(
352 webrtc::kVideoCodecVP8);
353 }
354
deadbeefaf1b59c2015-10-15 12:08:41 -0700355 void IceRestart() {
htaaac2dea2016-03-10 13:35:55 -0800356 offer_answer_constraints_.SetMandatoryIceRestart(true);
357 offer_answer_options_.ice_restart = true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700358 SetExpectIceRestart(true);
359 }
360
361 void SetExpectIceRestart(bool expect_restart) {
362 expect_ice_restart_ = expect_restart;
363 }
364
365 bool ExpectIceRestart() const { return expect_ice_restart_; }
366
367 void SetReceiveAudioVideo(bool audio, bool video) {
368 SetReceiveAudio(audio);
369 SetReceiveVideo(video);
370 ASSERT_EQ(audio, can_receive_audio());
371 ASSERT_EQ(video, can_receive_video());
372 }
373
374 void SetReceiveAudio(bool audio) {
375 if (audio && can_receive_audio())
376 return;
htaaac2dea2016-03-10 13:35:55 -0800377 offer_answer_constraints_.SetMandatoryReceiveAudio(audio);
378 offer_answer_options_.offer_to_receive_audio = audio ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700379 }
380
381 void SetReceiveVideo(bool video) {
382 if (video && can_receive_video())
383 return;
htaaac2dea2016-03-10 13:35:55 -0800384 offer_answer_constraints_.SetMandatoryReceiveVideo(video);
385 offer_answer_options_.offer_to_receive_video = video ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700386 }
387
388 void RemoveMsidFromReceivedSdp(bool remove) { remove_msid_ = remove; }
389
390 void RemoveSdesCryptoFromReceivedSdp(bool remove) { remove_sdes_ = remove; }
391
392 void RemoveBundleFromReceivedSdp(bool remove) { remove_bundle_ = remove; }
393
perkjcaafdba2016-03-20 07:34:29 -0700394 void RemoveCvoFromReceivedSdp(bool remove) { remove_cvo_ = remove; }
395
deadbeefaf1b59c2015-10-15 12:08:41 -0700396 bool can_receive_audio() {
397 bool value;
htaaac2dea2016-03-10 13:35:55 -0800398 if (prefer_constraint_apis_) {
399 if (webrtc::FindConstraint(
400 &offer_answer_constraints_,
401 MediaConstraintsInterface::kOfferToReceiveAudio, &value,
402 nullptr)) {
403 return value;
404 }
405 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700406 }
htaaac2dea2016-03-10 13:35:55 -0800407 return offer_answer_options_.offer_to_receive_audio > 0 ||
408 offer_answer_options_.offer_to_receive_audio ==
409 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700410 }
411
412 bool can_receive_video() {
413 bool value;
htaaac2dea2016-03-10 13:35:55 -0800414 if (prefer_constraint_apis_) {
415 if (webrtc::FindConstraint(
416 &offer_answer_constraints_,
417 MediaConstraintsInterface::kOfferToReceiveVideo, &value,
418 nullptr)) {
419 return value;
420 }
421 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700422 }
htaaac2dea2016-03-10 13:35:55 -0800423 return offer_answer_options_.offer_to_receive_video > 0 ||
424 offer_answer_options_.offer_to_receive_video ==
425 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700426 }
427
deadbeefaf1b59c2015-10-15 12:08:41 -0700428 void OnDataChannel(DataChannelInterface* data_channel) override {
429 LOG(INFO) << id_ << "OnDataChannel";
430 data_channel_ = data_channel;
431 data_observer_.reset(new MockDataChannelObserver(data_channel));
432 }
433
434 void CreateDataChannel() {
435 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, nullptr);
436 ASSERT_TRUE(data_channel_.get() != nullptr);
437 data_observer_.reset(new MockDataChannelObserver(data_channel_));
438 }
439
deadbeeffac06552015-11-25 11:26:01 -0800440 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack(
441 const std::string& stream_label) {
442 FakeConstraints constraints;
443 // Disable highpass filter so that we can get all the test audio frames.
444 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
445 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
446 peer_connection_factory_->CreateAudioSource(&constraints);
447 // TODO(perkj): Test audio source when it is implemented. Currently audio
448 // always use the default input.
449 std::string label = stream_label + kAudioTrackLabelBase;
450 return peer_connection_factory_->CreateAudioTrack(label, source);
451 }
452
453 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack(
454 const std::string& stream_label) {
455 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
456 FakeConstraints source_constraints = video_constraints_;
457 source_constraints.SetMandatoryMaxFrameRate(10);
458
459 cricket::FakeVideoCapturer* fake_capturer =
460 new webrtc::FakePeriodicVideoCapturer();
perkjcaafdba2016-03-20 07:34:29 -0700461 fake_capturer->SetRotation(capture_rotation_);
deadbeeffac06552015-11-25 11:26:01 -0800462 video_capturers_.push_back(fake_capturer);
perkja3ede6c2016-03-08 01:27:48 +0100463 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
deadbeeffac06552015-11-25 11:26:01 -0800464 peer_connection_factory_->CreateVideoSource(fake_capturer,
465 &source_constraints);
466 std::string label = stream_label + kVideoTrackLabelBase;
perkjcaafdba2016-03-20 07:34:29 -0700467
468 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
469 peer_connection_factory_->CreateVideoTrack(label, source));
470 if (!local_video_renderer_) {
471 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
472 }
473 return track;
deadbeeffac06552015-11-25 11:26:01 -0800474 }
475
deadbeefaf1b59c2015-10-15 12:08:41 -0700476 DataChannelInterface* data_channel() { return data_channel_; }
477 const MockDataChannelObserver* data_observer() const {
478 return data_observer_.get();
479 }
480
hta6b4f8392016-03-10 00:24:31 -0800481 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
deadbeefaf1b59c2015-10-15 12:08:41 -0700482
483 void StopVideoCapturers() {
perkjcaafdba2016-03-20 07:34:29 -0700484 for (auto* capturer : video_capturers_) {
485 capturer->Stop();
deadbeefaf1b59c2015-10-15 12:08:41 -0700486 }
487 }
488
perkjcaafdba2016-03-20 07:34:29 -0700489 void SetCaptureRotation(webrtc::VideoRotation rotation) {
490 ASSERT_TRUE(video_capturers_.empty());
491 capture_rotation_ = rotation;
492 }
493
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 bool AudioFramesReceivedCheck(int number_of_frames) const {
495 return number_of_frames <= fake_audio_capture_module_->frames_received();
496 }
497
deadbeefc9be0072015-12-14 18:27:57 -0800498 int audio_frames_received() const {
499 return fake_audio_capture_module_->frames_received();
500 }
501
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 bool VideoFramesReceivedCheck(int number_of_frames) {
503 if (video_decoder_factory_enabled_) {
504 const std::vector<FakeWebRtcVideoDecoder*>& decoders
505 = fake_video_decoder_factory_->decoders();
506 if (decoders.empty()) {
507 return number_of_frames <= 0;
508 }
hta6b4f8392016-03-10 00:24:31 -0800509 // Note - this checks that EACH decoder has the requisite number
510 // of frames. The video_frames_received() function sums them.
deadbeefc9be0072015-12-14 18:27:57 -0800511 for (FakeWebRtcVideoDecoder* decoder : decoders) {
512 if (number_of_frames > decoder->GetNumFramesReceived()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 return false;
514 }
515 }
516 return true;
517 } else {
518 if (fake_video_renderers_.empty()) {
519 return number_of_frames <= 0;
520 }
521
deadbeefc9be0072015-12-14 18:27:57 -0800522 for (const auto& pair : fake_video_renderers_) {
523 if (number_of_frames > pair.second->num_rendered_frames()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 return false;
525 }
526 }
527 return true;
528 }
529 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700530
deadbeefc9be0072015-12-14 18:27:57 -0800531 int video_frames_received() const {
532 int total = 0;
533 if (video_decoder_factory_enabled_) {
534 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
535 fake_video_decoder_factory_->decoders();
536 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
537 total += decoder->GetNumFramesReceived();
538 }
539 } else {
540 for (const auto& pair : fake_video_renderers_) {
541 total += pair.second->num_rendered_frames();
542 }
543 for (const auto& renderer : removed_fake_video_renderers_) {
544 total += renderer->num_rendered_frames();
545 }
546 }
547 return total;
548 }
549
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 // Verify the CreateDtmfSender interface
551 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000552 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
553 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554
555 // We can't create a DTMF sender with an invalid audio track or a non local
556 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700557 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000558 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700559 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
560 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561
562 // We should be able to create a DTMF sender from a local track.
563 webrtc::AudioTrackInterface* localtrack =
564 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
565 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700566 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 dtmf_sender->RegisterObserver(observer.get());
568
569 // Test the DtmfSender object just created.
570 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
571 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
572
573 // We don't need to verify that the DTMF tones are actually sent out because
574 // that is already covered by the tests of the lower level components.
575
576 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
577 std::vector<std::string> tones;
578 tones.push_back("1");
579 tones.push_back("a");
580 tones.push_back("");
581 observer->Verify(tones);
582
583 dtmf_sender->UnregisterObserver();
584 }
585
586 // Verifies that the SessionDescription have rejected the appropriate media
587 // content.
588 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700589 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
590 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 const cricket::SessionDescription* remote_desc =
592 peer_connection_->remote_description()->description();
593 const cricket::SessionDescription* local_desc =
594 peer_connection_->local_description()->description();
595
596 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
597 if (remote_audio_content) {
598 const ContentInfo* audio_content =
599 GetFirstAudioContent(local_desc);
600 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
601 }
602
603 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
604 if (remote_video_content) {
605 const ContentInfo* video_content =
606 GetFirstVideoContent(local_desc);
607 EXPECT_EQ(can_receive_video(), !video_content->rejected);
608 }
609 }
610
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700612 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613 const cricket::SessionDescription* desc =
614 peer_connection_->local_description()->description();
615 const cricket::ContentInfos& contents = desc->contents();
616
617 for (size_t index = 0; index < contents.size(); ++index) {
618 if (contents[index].rejected)
619 continue;
620 const cricket::TransportDescription* transport_desc =
621 desc->GetTransportDescriptionByName(contents[index].name);
622
623 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000624 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000625 if (ufragpair_it == ice_ufrag_pwd_.end()) {
626 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000627 ice_ufrag_pwd_[static_cast<int>(index)] =
628 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 } else if (ExpectIceRestart()) {
630 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
631 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
632 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
633 } else {
634 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
635 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
636 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
637 }
638 }
639 }
640
641 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000642 rtc::scoped_refptr<MockStatsObserver>
643 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000644 EXPECT_TRUE(peer_connection_->GetStats(
645 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700647 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648 return observer->AudioOutputLevel();
649 }
650
651 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000652 rtc::scoped_refptr<MockStatsObserver>
653 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000654 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700655 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000656 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700657 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000658 return observer->AudioInputLevel();
659 }
660
661 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000662 rtc::scoped_refptr<MockStatsObserver>
663 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000664 EXPECT_TRUE(peer_connection_->GetStats(
665 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000666 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700667 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 return observer->BytesReceived();
669 }
670
671 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000672 rtc::scoped_refptr<MockStatsObserver>
673 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000674 EXPECT_TRUE(peer_connection_->GetStats(
675 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700677 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678 return observer->BytesSent();
679 }
680
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000681 int GetAvailableReceivedBandwidthStats() {
682 rtc::scoped_refptr<MockStatsObserver>
683 observer(new rtc::RefCountedObject<MockStatsObserver>());
684 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700685 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000686 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700687 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000688 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000689 return bw;
690 }
691
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000692 std::string GetDtlsCipherStats() {
693 rtc::scoped_refptr<MockStatsObserver>
694 observer(new rtc::RefCountedObject<MockStatsObserver>());
695 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700696 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000697 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700698 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000699 return observer->DtlsCipher();
700 }
701
702 std::string GetSrtpCipherStats() {
703 rtc::scoped_refptr<MockStatsObserver>
704 observer(new rtc::RefCountedObject<MockStatsObserver>());
705 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700706 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000707 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700708 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000709 return observer->SrtpCipher();
710 }
711
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 int rendered_width() {
713 EXPECT_FALSE(fake_video_renderers_.empty());
714 return fake_video_renderers_.empty() ? 1 :
715 fake_video_renderers_.begin()->second->width();
716 }
717
718 int rendered_height() {
719 EXPECT_FALSE(fake_video_renderers_.empty());
720 return fake_video_renderers_.empty() ? 1 :
721 fake_video_renderers_.begin()->second->height();
722 }
723
perkjcaafdba2016-03-20 07:34:29 -0700724 webrtc::VideoRotation rendered_rotation() {
725 EXPECT_FALSE(fake_video_renderers_.empty());
726 return fake_video_renderers_.empty()
727 ? webrtc::kVideoRotation_0
728 : fake_video_renderers_.begin()->second->rotation();
729 }
730
731 int local_rendered_width() {
732 return local_video_renderer_ ? local_video_renderer_->width() : 1;
733 }
734
735 int local_rendered_height() {
736 return local_video_renderer_ ? local_video_renderer_->height() : 1;
737 }
738
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000739 size_t number_of_remote_streams() {
740 if (!pc())
741 return 0;
742 return pc()->remote_streams()->count();
743 }
744
hta6b4f8392016-03-10 00:24:31 -0800745 StreamCollectionInterface* remote_streams() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 if (!pc()) {
747 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700748 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 }
750 return pc()->remote_streams();
751 }
752
753 StreamCollectionInterface* local_streams() {
754 if (!pc()) {
755 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700756 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 }
758 return pc()->local_streams();
759 }
760
hta6b4f8392016-03-10 00:24:31 -0800761 bool HasLocalAudioTrack() { return StreamsHaveAudioTrack(local_streams()); }
762
763 bool HasLocalVideoTrack() { return StreamsHaveVideoTrack(local_streams()); }
764
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
766 return pc()->signaling_state();
767 }
768
769 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
770 return pc()->ice_connection_state();
771 }
772
773 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
774 return pc()->ice_gathering_state();
775 }
776
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 private:
778 class DummyDtmfObserver : public DtmfSenderObserverInterface {
779 public:
780 DummyDtmfObserver() : completed_(false) {}
781
782 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700783 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 tones_.push_back(tone);
785 if (tone.empty()) {
786 completed_ = true;
787 }
788 }
789
790 void Verify(const std::vector<std::string>& tones) const {
791 ASSERT_TRUE(tones_.size() == tones.size());
792 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
793 }
794
795 bool completed() const { return completed_; }
796
797 private:
798 bool completed_;
799 std::vector<std::string> tones_;
800 };
801
deadbeefaf1b59c2015-10-15 12:08:41 -0700802 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
803
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800804 bool Init(
805 const MediaConstraintsInterface* constraints,
806 const PeerConnectionFactory::Options* options,
htaaac2dea2016-03-10 13:35:55 -0800807 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store,
perkj61724012016-04-06 00:02:59 -0700808 bool prefer_constraint_apis,
809 rtc::Thread* worker_thread) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700810 EXPECT_TRUE(!peer_connection_);
811 EXPECT_TRUE(!peer_connection_factory_);
htaaac2dea2016-03-10 13:35:55 -0800812 if (!prefer_constraint_apis) {
813 EXPECT_TRUE(!constraints);
814 }
815 prefer_constraint_apis_ = prefer_constraint_apis;
816
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800817 rtc::scoped_ptr<cricket::PortAllocator> port_allocator(
perkj61724012016-04-06 00:02:59 -0700818 new cricket::FakePortAllocator(worker_thread, nullptr));
deadbeefaf1b59c2015-10-15 12:08:41 -0700819 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
820
821 if (fake_audio_capture_module_ == nullptr) {
822 return false;
823 }
824 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
825 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
826 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
perkj61724012016-04-06 00:02:59 -0700827 worker_thread, rtc::Thread::Current(), fake_audio_capture_module_,
828 fake_video_encoder_factory_, fake_video_decoder_factory_);
deadbeefaf1b59c2015-10-15 12:08:41 -0700829 if (!peer_connection_factory_) {
830 return false;
831 }
832 if (options) {
833 peer_connection_factory_->SetOptions(*options);
834 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800835 peer_connection_ = CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800836 std::move(port_allocator), constraints, std::move(dtls_identity_store));
deadbeefaf1b59c2015-10-15 12:08:41 -0700837 return peer_connection_.get() != nullptr;
838 }
839
deadbeefaf1b59c2015-10-15 12:08:41 -0700840 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800841 rtc::scoped_ptr<cricket::PortAllocator> port_allocator,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800842 const MediaConstraintsInterface* constraints,
843 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800844 // CreatePeerConnection with RTCConfiguration.
845 webrtc::PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 webrtc::PeerConnectionInterface::IceServer ice_server;
847 ice_server.uri = "stun:stun.l.google.com:19302";
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800848 config.servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000849
deadbeefaf1b59c2015-10-15 12:08:41 -0700850 return peer_connection_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800851 config, constraints, std::move(port_allocator),
852 std::move(dtls_identity_store), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 }
854
855 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700856 LOG(INFO) << id_ << "HandleIncomingOffer ";
deadbeeffaac4972015-11-12 15:33:07 -0800857 if (NumberOfLocalMediaStreams() == 0 && auto_add_stream_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 // If we are not sending any streams ourselves it is time to add some.
859 AddMediaStream(true, true);
860 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000861 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700862 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000864 rtc::scoped_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700865 EXPECT_TRUE(DoCreateAnswer(&answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866 std::string sdp;
867 EXPECT_TRUE(answer->ToString(&sdp));
868 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
deadbeefaf1b59c2015-10-15 12:08:41 -0700869 if (signaling_message_receiver_) {
870 signaling_message_receiver_->ReceiveSdpMessage(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871 webrtc::SessionDescriptionInterface::kAnswer, sdp);
872 }
873 }
874
875 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700876 LOG(INFO) << id_ << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000877 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700878 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
880 }
881
kwiberg2bbff992016-03-16 11:03:04 -0700882 bool DoCreateOfferAnswer(rtc::scoped_ptr<SessionDescriptionInterface>* desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000884 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
885 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886 MockCreateSessionDescriptionObserver>());
htaaac2dea2016-03-10 13:35:55 -0800887 if (prefer_constraint_apis_) {
888 if (offer) {
889 pc()->CreateOffer(observer, &offer_answer_constraints_);
890 } else {
891 pc()->CreateAnswer(observer, &offer_answer_constraints_);
892 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 } else {
htaaac2dea2016-03-10 13:35:55 -0800894 if (offer) {
895 pc()->CreateOffer(observer, offer_answer_options_);
896 } else {
897 pc()->CreateAnswer(observer, offer_answer_options_);
898 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899 }
900 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
kwiberg2bbff992016-03-16 11:03:04 -0700901 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 if (observer->result() && ExpectIceRestart()) {
903 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
904 }
905 return observer->result();
906 }
907
kwiberg2bbff992016-03-16 11:03:04 -0700908 bool DoCreateOffer(rtc::scoped_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 return DoCreateOfferAnswer(desc, true);
910 }
911
kwiberg2bbff992016-03-16 11:03:04 -0700912 bool DoCreateAnswer(rtc::scoped_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 return DoCreateOfferAnswer(desc, false);
914 }
915
916 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000917 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
918 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700920 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 pc()->SetLocalDescription(observer, desc);
922 // Ignore the observer result. If we wait for the result with
923 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
924 // before the offer which is an error.
925 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000926 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 // ProcessMessages waits at least 1ms but processes all messages before
928 // returning. Since this test is synchronous and send messages to the remote
929 // peer whenever a callback is invoked, this can lead to messages being
930 // sent to the remote peer in the wrong order.
931 // TODO(perkj): Find a way to check the result without risking that the
932 // order of sent messages are changed. Ex- by posting all messages that are
933 // sent to the remote peer.
934 return true;
935 }
936
937 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000938 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
939 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700941 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942 pc()->SetRemoteDescription(observer, desc);
943 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
944 return observer->result();
945 }
946
947 // This modifies all received SDP messages before they are processed.
948 void FilterIncomingSdpMessage(std::string* sdp) {
949 if (remove_msid_) {
950 const char kSdpSsrcAttribute[] = "a=ssrc:";
951 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
952 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
953 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
954 }
955 if (remove_bundle_) {
956 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
957 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
958 }
959 if (remove_sdes_) {
960 const char kSdpSdesCryptoAttribute[] = "a=crypto";
961 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
962 }
perkjcaafdba2016-03-20 07:34:29 -0700963 if (remove_cvo_) {
964 const char kSdpCvoExtenstion[] = "urn:3gpp:video-orientation";
965 RemoveLinesFromSdp(kSdpCvoExtenstion, sdp);
966 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000967 }
968
deadbeefaf1b59c2015-10-15 12:08:41 -0700969 std::string id_;
970
deadbeefaf1b59c2015-10-15 12:08:41 -0700971 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
972 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
973 peer_connection_factory_;
974
htaaac2dea2016-03-10 13:35:55 -0800975 bool prefer_constraint_apis_ = true;
deadbeeffaac4972015-11-12 15:33:07 -0800976 bool auto_add_stream_ = true;
977
deadbeefaf1b59c2015-10-15 12:08:41 -0700978 typedef std::pair<std::string, std::string> IceUfragPwdPair;
979 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
980 bool expect_ice_restart_ = false;
981
deadbeefc9be0072015-12-14 18:27:57 -0800982 // Needed to keep track of number of frames sent.
deadbeefaf1b59c2015-10-15 12:08:41 -0700983 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
984 // Needed to keep track of number of frames received.
deadbeefc9be0072015-12-14 18:27:57 -0800985 std::map<std::string, rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
986 fake_video_renderers_;
987 // Needed to ensure frames aren't received for removed tracks.
988 std::vector<rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
989 removed_fake_video_renderers_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700990 // Needed to keep track of number of frames received when external decoder
991 // used.
992 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
993 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
994 bool video_decoder_factory_enabled_ = false;
995 webrtc::FakeConstraints video_constraints_;
996
997 // For remote peer communication.
998 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
999
1000 // Store references to the video capturers we've created, so that we can stop
1001 // them, if required.
perkjcaafdba2016-03-20 07:34:29 -07001002 std::vector<cricket::FakeVideoCapturer*> video_capturers_;
1003 webrtc::VideoRotation capture_rotation_ = webrtc::kVideoRotation_0;
1004 // |local_video_renderer_| attached to the first created local video track.
1005 rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001006
htaaac2dea2016-03-10 13:35:55 -08001007 webrtc::FakeConstraints offer_answer_constraints_;
1008 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001009 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
1010 bool remove_bundle_ =
1011 false; // True if bundle should be removed in received SDP.
1012 bool remove_sdes_ =
1013 false; // True if a=crypto should be removed in received SDP.
perkjcaafdba2016-03-20 07:34:29 -07001014 // |remove_cvo_| is true if extension urn:3gpp:video-orientation should be
1015 // removed in the received SDP.
1016 bool remove_cvo_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001017
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001018 rtc::scoped_refptr<DataChannelInterface> data_channel_;
1019 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020};
1021
deadbeef7c73bdb2015-12-10 15:10:44 -08001022class P2PTestConductor : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023 public:
deadbeef7c73bdb2015-12-10 15:10:44 -08001024 P2PTestConductor()
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001025 : pss_(new rtc::PhysicalSocketServer),
1026 ss_(new rtc::VirtualSocketServer(pss_.get())),
perkj61724012016-04-06 00:02:59 -07001027 ss_scope_(ss_.get()) {
1028 RTC_CHECK(worker_thread_.Start());
1029 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001030
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031 bool SessionActive() {
1032 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001033 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001035
hta6b4f8392016-03-10 00:24:31 -08001036 // Return true if the number of frames provided have been received
1037 // on the video and audio tracks provided.
1038 bool FramesHaveArrived(int audio_frames_to_receive,
1039 int video_frames_to_receive) {
1040 bool all_good = true;
1041 if (initiating_client_->HasLocalAudioTrack() &&
1042 receiving_client_->can_receive_audio()) {
1043 all_good &=
1044 receiving_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1045 }
1046 if (initiating_client_->HasLocalVideoTrack() &&
1047 receiving_client_->can_receive_video()) {
1048 all_good &=
1049 receiving_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1050 }
1051 if (receiving_client_->HasLocalAudioTrack() &&
1052 initiating_client_->can_receive_audio()) {
1053 all_good &=
1054 initiating_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1055 }
1056 if (receiving_client_->HasLocalVideoTrack() &&
1057 initiating_client_->can_receive_video()) {
1058 all_good &=
1059 initiating_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1060 }
1061 return all_good;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001062 }
hta6b4f8392016-03-10 00:24:31 -08001063
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001064 void VerifyDtmf() {
1065 initiating_client_->VerifyDtmf();
1066 receiving_client_->VerifyDtmf();
1067 }
1068
1069 void TestUpdateOfferWithRejectedContent() {
deadbeefc9be0072015-12-14 18:27:57 -08001070 // Renegotiate, rejecting the video m-line.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071 initiating_client_->Negotiate(true, false);
deadbeefc9be0072015-12-14 18:27:57 -08001072 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1073
1074 int pc1_audio_received = initiating_client_->audio_frames_received();
1075 int pc1_video_received = initiating_client_->video_frames_received();
1076 int pc2_audio_received = receiving_client_->audio_frames_received();
1077 int pc2_video_received = receiving_client_->video_frames_received();
1078
1079 // Wait for some additional audio frames to be received.
1080 EXPECT_TRUE_WAIT(initiating_client_->AudioFramesReceivedCheck(
1081 pc1_audio_received + kEndAudioFrameCount) &&
1082 receiving_client_->AudioFramesReceivedCheck(
1083 pc2_audio_received + kEndAudioFrameCount),
1084 kMaxWaitForFramesMs);
1085
1086 // During this time, we shouldn't have received any additional video frames
1087 // for the rejected video tracks.
1088 EXPECT_EQ(pc1_video_received, initiating_client_->video_frames_received());
1089 EXPECT_EQ(pc2_video_received, receiving_client_->video_frames_received());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001090 }
1091
1092 void VerifyRenderedSize(int width, int height) {
perkjcaafdba2016-03-20 07:34:29 -07001093 VerifyRenderedSize(width, height, webrtc::kVideoRotation_0);
1094 }
1095
1096 void VerifyRenderedSize(int width,
1097 int height,
1098 webrtc::VideoRotation rotation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099 EXPECT_EQ(width, receiving_client()->rendered_width());
1100 EXPECT_EQ(height, receiving_client()->rendered_height());
perkjcaafdba2016-03-20 07:34:29 -07001101 EXPECT_EQ(rotation, receiving_client()->rendered_rotation());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001102 EXPECT_EQ(width, initializing_client()->rendered_width());
1103 EXPECT_EQ(height, initializing_client()->rendered_height());
perkjcaafdba2016-03-20 07:34:29 -07001104 EXPECT_EQ(rotation, initializing_client()->rendered_rotation());
1105
1106 // Verify size of the local preview.
1107 EXPECT_EQ(width, initializing_client()->local_rendered_width());
1108 EXPECT_EQ(height, initializing_client()->local_rendered_height());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001109 }
1110
1111 void VerifySessionDescriptions() {
1112 initiating_client_->VerifyRejectedMediaInSessionDescription();
1113 receiving_client_->VerifyRejectedMediaInSessionDescription();
1114 initiating_client_->VerifyLocalIceUfragAndPassword();
1115 receiving_client_->VerifyLocalIceUfragAndPassword();
1116 }
1117
deadbeef7c73bdb2015-12-10 15:10:44 -08001118 ~P2PTestConductor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001119 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001120 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001121 }
1122 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001123 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001124 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001125 }
1126
deadbeefaf1b59c2015-10-15 12:08:41 -07001127 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128
1129 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1130 MediaConstraintsInterface* recv_constraints) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001131 return CreateTestClients(init_constraints, nullptr, recv_constraints,
1132 nullptr);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001133 }
1134
htaaac2dea2016-03-10 13:35:55 -08001135 bool CreateTestClientsThatPreferNoConstraints() {
1136 initiating_client_.reset(
perkj61724012016-04-06 00:02:59 -07001137 PeerConnectionTestClient::CreateClientPreferNoConstraints(
1138 "Caller: ", nullptr, &worker_thread_));
htaaac2dea2016-03-10 13:35:55 -08001139 receiving_client_.reset(
perkj61724012016-04-06 00:02:59 -07001140 PeerConnectionTestClient::CreateClientPreferNoConstraints(
1141 "Callee: ", nullptr, &worker_thread_));
htaaac2dea2016-03-10 13:35:55 -08001142 if (!initiating_client_ || !receiving_client_) {
1143 return false;
1144 }
1145 // Remember the choice for possible later resets of the clients.
1146 prefer_constraint_apis_ = false;
1147 SetSignalingReceivers();
1148 return true;
1149 }
1150
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001151 void SetSignalingReceivers() {
1152 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
1153 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1154 }
1155
Joachim Bauch04e5b492015-05-29 09:40:39 +02001156 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1157 PeerConnectionFactory::Options* init_options,
1158 MediaConstraintsInterface* recv_constraints,
1159 PeerConnectionFactory::Options* recv_options) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001160 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
perkj61724012016-04-06 00:02:59 -07001161 "Caller: ", init_constraints, init_options, &worker_thread_));
deadbeefaf1b59c2015-10-15 12:08:41 -07001162 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
perkj61724012016-04-06 00:02:59 -07001163 "Callee: ", recv_constraints, recv_options, &worker_thread_));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001164 if (!initiating_client_ || !receiving_client_) {
1165 return false;
1166 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001167 SetSignalingReceivers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001168 return true;
1169 }
1170
1171 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1172 const webrtc::FakeConstraints& recv_constraints) {
1173 initiating_client_->SetVideoConstraints(init_constraints);
1174 receiving_client_->SetVideoConstraints(recv_constraints);
1175 }
1176
perkjcaafdba2016-03-20 07:34:29 -07001177 void SetCaptureRotation(webrtc::VideoRotation rotation) {
1178 initiating_client_->SetCaptureRotation(rotation);
1179 receiving_client_->SetCaptureRotation(rotation);
1180 }
1181
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182 void EnableVideoDecoderFactory() {
1183 initiating_client_->EnableVideoDecoderFactory();
1184 receiving_client_->EnableVideoDecoderFactory();
1185 }
1186
1187 // This test sets up a call between two parties. Both parties send static
1188 // frames to each other. Once the test is finished the number of sent frames
1189 // is compared to the number of received frames.
1190 void LocalP2PTest() {
1191 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1192 initiating_client_->AddMediaStream(true, true);
1193 }
1194 initiating_client_->Negotiate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195 // Assert true is used here since next tests are guaranteed to fail and
1196 // would eat up 5 seconds.
1197 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1198 VerifySessionDescriptions();
1199
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001200 int audio_frame_count = kEndAudioFrameCount;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001201 int video_frame_count = kEndVideoFrameCount;
hta6b4f8392016-03-10 00:24:31 -08001202 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1203
1204 if ((!initiating_client_->can_receive_audio() &&
1205 !initiating_client_->can_receive_video()) ||
1206 (!receiving_client_->can_receive_audio() &&
1207 !receiving_client_->can_receive_video())) {
1208 // Neither audio nor video will flow, so connections won't be
1209 // established. There's nothing more to check.
1210 // TODO(hta): Check connection if there's a data channel.
1211 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001212 }
1213
hta6b4f8392016-03-10 00:24:31 -08001214 // Audio or video is expected to flow, so both clients should reach the
1215 // Connected state, and the offerer (ICE controller) should proceed to
1216 // Completed.
1217 // Note: These tests have been observed to fail under heavy load at
1218 // shorter timeouts, so they may be flaky.
1219 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1220 initiating_client_->ice_connection_state(),
1221 kMaxWaitForFramesMs);
1222 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1223 receiving_client_->ice_connection_state(),
1224 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001225
hta6b4f8392016-03-10 00:24:31 -08001226 // The ICE gathering state should end up in kIceGatheringComplete,
1227 // but there's a bug that prevents this at the moment, and the state
1228 // machine is being updated by the WEBRTC WG.
1229 // TODO(hta): Update this check when spec revisions finish.
1230 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1231 initiating_client_->ice_gathering_state());
1232 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1233 receiving_client_->ice_gathering_state(),
1234 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001235
hta6b4f8392016-03-10 00:24:31 -08001236 // Check that the expected number of frames have arrived.
1237 EXPECT_TRUE_WAIT(FramesHaveArrived(audio_frame_count, video_frame_count),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001238 kMaxWaitForFramesMs);
1239 }
1240
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001241 void SetupAndVerifyDtlsCall() {
1242 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1243 FakeConstraints setup_constraints;
1244 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1245 true);
1246 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1247 LocalP2PTest();
1248 VerifyRenderedSize(640, 480);
1249 }
1250
1251 PeerConnectionTestClient* CreateDtlsClientWithAlternateKey() {
1252 FakeConstraints setup_constraints;
1253 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1254 true);
1255
1256 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
1257 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
1258 : nullptr);
1259 dtls_identity_store->use_alternate_key();
1260
1261 // Make sure the new client is using a different certificate.
1262 return PeerConnectionTestClient::CreateClientWithDtlsIdentityStore(
kwiberg0eb15ed2015-12-17 03:04:15 -08001263 "New Peer: ", &setup_constraints, nullptr,
perkj61724012016-04-06 00:02:59 -07001264 std::move(dtls_identity_store), prefer_constraint_apis_,
1265 &worker_thread_);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001266 }
1267
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001268 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1269 // Messages may get lost on the unreliable DataChannel, so we send multiple
1270 // times to avoid test flakiness.
1271 static const size_t kSendAttempts = 5;
1272
1273 for (size_t i = 0; i < kSendAttempts; ++i) {
1274 dc->Send(DataBuffer(data));
1275 }
1276 }
1277
deadbeefaf1b59c2015-10-15 12:08:41 -07001278 PeerConnectionTestClient* initializing_client() {
1279 return initiating_client_.get();
1280 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001281
1282 // Set the |initiating_client_| to the |client| passed in and return the
1283 // original |initiating_client_|.
1284 PeerConnectionTestClient* set_initializing_client(
1285 PeerConnectionTestClient* client) {
1286 PeerConnectionTestClient* old = initiating_client_.release();
1287 initiating_client_.reset(client);
1288 return old;
1289 }
1290
deadbeefaf1b59c2015-10-15 12:08:41 -07001291 PeerConnectionTestClient* receiving_client() {
1292 return receiving_client_.get();
1293 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001295 // Set the |receiving_client_| to the |client| passed in and return the
1296 // original |receiving_client_|.
1297 PeerConnectionTestClient* set_receiving_client(
1298 PeerConnectionTestClient* client) {
1299 PeerConnectionTestClient* old = receiving_client_.release();
1300 receiving_client_.reset(client);
1301 return old;
1302 }
1303
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304 private:
perkj61724012016-04-06 00:02:59 -07001305 // |worker_thread_| is used by both |initiating_client_| and
1306 // |receiving_client_|. Must be destroyed last.
1307 rtc::Thread worker_thread_;
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001308 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1309 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1310 rtc::SocketServerScope ss_scope_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001311 rtc::scoped_ptr<PeerConnectionTestClient> initiating_client_;
1312 rtc::scoped_ptr<PeerConnectionTestClient> receiving_client_;
htaaac2dea2016-03-10 13:35:55 -08001313 bool prefer_constraint_apis_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001314};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001316// Disable for TSan v2, see
1317// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1318#if !defined(THREAD_SANITIZER)
1319
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001320// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001321// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1322// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001323TEST_F(P2PTestConductor, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001324 ASSERT_TRUE(CreateTestClients());
1325 LocalP2PTest();
1326 VerifyDtmf();
1327}
1328
1329// This test sets up a Jsep call between two parties and test that we can get a
1330// video aspect ratio of 16:9.
deadbeef7c73bdb2015-12-10 15:10:44 -08001331TEST_F(P2PTestConductor, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332 ASSERT_TRUE(CreateTestClients());
1333 FakeConstraints constraint;
1334 double requested_ratio = 640.0/360;
1335 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1336 SetVideoConstraints(constraint, constraint);
1337 LocalP2PTest();
1338
1339 ASSERT_LE(0, initializing_client()->rendered_height());
1340 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001341 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001342 initializing_client()->rendered_height();
1343 EXPECT_LE(requested_ratio, initiating_video_ratio);
1344
1345 ASSERT_LE(0, receiving_client()->rendered_height());
1346 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001347 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348 receiving_client()->rendered_height();
1349 EXPECT_LE(requested_ratio, receiving_video_ratio);
1350}
1351
1352// This test sets up a Jsep call between two parties and test that the
1353// received video has a resolution of 1280*720.
1354// TODO(mallinath): Enable when
1355// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
deadbeef7c73bdb2015-12-10 15:10:44 -08001356TEST_F(P2PTestConductor, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001357 ASSERT_TRUE(CreateTestClients());
1358 FakeConstraints constraint;
1359 constraint.SetMandatoryMinWidth(1280);
1360 constraint.SetMandatoryMinHeight(720);
1361 SetVideoConstraints(constraint, constraint);
1362 LocalP2PTest();
1363 VerifyRenderedSize(1280, 720);
1364}
1365
1366// This test sets up a call between two endpoints that are configured to use
1367// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001368TEST_F(P2PTestConductor, LocalP2PTestDtls) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001369 SetupAndVerifyDtlsCall();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001370}
1371
hta6b4f8392016-03-10 00:24:31 -08001372// This test sets up an one-way call, with media only from initiator to
1373// responder.
1374TEST_F(P2PTestConductor, OneWayMediaCall) {
1375 ASSERT_TRUE(CreateTestClients());
1376 receiving_client()->set_auto_add_stream(false);
1377 LocalP2PTest();
1378}
1379
htaaac2dea2016-03-10 13:35:55 -08001380TEST_F(P2PTestConductor, OneWayMediaCallWithoutConstraints) {
1381 ASSERT_TRUE(CreateTestClientsThatPreferNoConstraints());
1382 receiving_client()->set_auto_add_stream(false);
1383 LocalP2PTest();
1384}
1385
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001386// This test sets up a audio call initially and then upgrades to audio/video,
1387// using DTLS.
deadbeef7c73bdb2015-12-10 15:10:44 -08001388TEST_F(P2PTestConductor, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001389 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001390 FakeConstraints setup_constraints;
1391 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1392 true);
1393 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1394 receiving_client()->SetReceiveAudioVideo(true, false);
1395 LocalP2PTest();
1396 receiving_client()->SetReceiveAudioVideo(true, true);
1397 receiving_client()->Negotiate();
1398}
1399
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001400// This test sets up a call transfer to a new caller with a different DTLS
1401// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001402TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCallee) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001403 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1404 SetupAndVerifyDtlsCall();
1405
1406 // Keeping the original peer around which will still send packets to the
1407 // receiving client. These SRTP packets will be dropped.
1408 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1409 set_initializing_client(CreateDtlsClientWithAlternateKey()));
1410 original_peer->pc()->Close();
1411
1412 SetSignalingReceivers();
1413 receiving_client()->SetExpectIceRestart(true);
1414 LocalP2PTest();
1415 VerifyRenderedSize(640, 480);
1416}
1417
guoweis46383312015-12-17 16:45:59 -08001418// This test sets up a non-bundle call and apply bundle during ICE restart. When
1419// bundle is in effect in the restart, the channel can successfully reset its
1420// DTLS-SRTP context.
1421TEST_F(P2PTestConductor, LocalP2PTestDtlsBundleInIceRestart) {
1422 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1423 FakeConstraints setup_constraints;
1424 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1425 true);
1426 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1427 receiving_client()->RemoveBundleFromReceivedSdp(true);
1428 LocalP2PTest();
1429 VerifyRenderedSize(640, 480);
1430
1431 initializing_client()->IceRestart();
1432 receiving_client()->SetExpectIceRestart(true);
1433 receiving_client()->RemoveBundleFromReceivedSdp(false);
1434 LocalP2PTest();
1435 VerifyRenderedSize(640, 480);
1436}
1437
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001438// This test sets up a call transfer to a new callee with a different DTLS
1439// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001440TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCaller) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001441 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1442 SetupAndVerifyDtlsCall();
1443
1444 // Keeping the original peer around which will still send packets to the
1445 // receiving client. These SRTP packets will be dropped.
1446 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1447 set_receiving_client(CreateDtlsClientWithAlternateKey()));
1448 original_peer->pc()->Close();
1449
1450 SetSignalingReceivers();
1451 initializing_client()->IceRestart();
1452 LocalP2PTest();
1453 VerifyRenderedSize(640, 480);
1454}
1455
perkjcaafdba2016-03-20 07:34:29 -07001456TEST_F(P2PTestConductor, LocalP2PTestCVO) {
1457 ASSERT_TRUE(CreateTestClients());
1458 SetCaptureRotation(webrtc::kVideoRotation_90);
1459 LocalP2PTest();
1460 VerifyRenderedSize(640, 480, webrtc::kVideoRotation_90);
1461}
1462
1463TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportCVO) {
1464 ASSERT_TRUE(CreateTestClients());
1465 SetCaptureRotation(webrtc::kVideoRotation_90);
1466 receiving_client()->RemoveCvoFromReceivedSdp(true);
1467 LocalP2PTest();
1468 VerifyRenderedSize(480, 640, webrtc::kVideoRotation_0);
1469}
1470
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001471// This test sets up a call between two endpoints that are configured to use
1472// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1473// negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001474TEST_F(P2PTestConductor, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001475 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001476 FakeConstraints setup_constraints;
1477 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1478 true);
1479 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1480 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1481 LocalP2PTest();
1482 VerifyRenderedSize(640, 480);
1483}
1484
1485// This test sets up a Jsep call between two parties, and the callee only
1486// accept to receive video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001487TEST_F(P2PTestConductor, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001488 ASSERT_TRUE(CreateTestClients());
1489 receiving_client()->SetReceiveAudioVideo(false, true);
1490 LocalP2PTest();
1491}
1492
1493// This test sets up a Jsep call between two parties, and the callee only
1494// accept to receive audio.
deadbeef7c73bdb2015-12-10 15:10:44 -08001495TEST_F(P2PTestConductor, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001496 ASSERT_TRUE(CreateTestClients());
1497 receiving_client()->SetReceiveAudioVideo(true, false);
1498 LocalP2PTest();
1499}
1500
1501// This test sets up a Jsep call between two parties, and the callee reject both
1502// audio and video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001503TEST_F(P2PTestConductor, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001504 ASSERT_TRUE(CreateTestClients());
1505 receiving_client()->SetReceiveAudioVideo(false, false);
1506 LocalP2PTest();
1507}
1508
1509// This test sets up an audio and video call between two parties. After the call
1510// runs for a while (10 frames), the caller sends an update offer with video
1511// being rejected. Once the re-negotiation is done, the video flow should stop
1512// and the audio flow should continue.
deadbeefc9be0072015-12-14 18:27:57 -08001513TEST_F(P2PTestConductor, UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001514 ASSERT_TRUE(CreateTestClients());
1515 LocalP2PTest();
1516 TestUpdateOfferWithRejectedContent();
1517}
1518
1519// This test sets up a Jsep call between two parties. The MSID is removed from
1520// the SDP strings from the caller.
deadbeefc9be0072015-12-14 18:27:57 -08001521TEST_F(P2PTestConductor, LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522 ASSERT_TRUE(CreateTestClients());
1523 receiving_client()->RemoveMsidFromReceivedSdp(true);
1524 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1525 // audio and video is muxed when MSID is disabled. Remove
1526 // SetRemoveBundleFromSdp once
1527 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1528 receiving_client()->RemoveBundleFromReceivedSdp(true);
1529 LocalP2PTest();
1530}
1531
1532// This test sets up a Jsep call between two parties and the initiating peer
1533// sends two steams.
1534// TODO(perkj): Disabled due to
1535// https://code.google.com/p/webrtc/issues/detail?id=1454
deadbeef7c73bdb2015-12-10 15:10:44 -08001536TEST_F(P2PTestConductor, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001537 ASSERT_TRUE(CreateTestClients());
1538 // Set optional video constraint to max 320pixels to decrease CPU usage.
1539 FakeConstraints constraint;
1540 constraint.SetOptionalMaxWidth(320);
1541 SetVideoConstraints(constraint, constraint);
1542 initializing_client()->AddMediaStream(true, true);
1543 initializing_client()->AddMediaStream(false, true);
1544 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1545 LocalP2PTest();
1546 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1547}
1548
1549// Test that we can receive the audio output level from a remote audio track.
deadbeef7c73bdb2015-12-10 15:10:44 -08001550TEST_F(P2PTestConductor, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551 ASSERT_TRUE(CreateTestClients());
1552 LocalP2PTest();
1553
1554 StreamCollectionInterface* remote_streams =
1555 initializing_client()->remote_streams();
1556 ASSERT_GT(remote_streams->count(), 0u);
1557 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1558 MediaStreamTrackInterface* remote_audio_track =
1559 remote_streams->at(0)->GetAudioTracks()[0];
1560
1561 // Get the audio output level stats. Note that the level is not available
1562 // until a RTCP packet has been received.
1563 EXPECT_TRUE_WAIT(
1564 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1565 kMaxWaitForStatsMs);
1566}
1567
1568// Test that an audio input level is reported.
deadbeef7c73bdb2015-12-10 15:10:44 -08001569TEST_F(P2PTestConductor, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570 ASSERT_TRUE(CreateTestClients());
1571 LocalP2PTest();
1572
1573 // Get the audio input level stats. The level should be available very
1574 // soon after the test starts.
1575 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1576 kMaxWaitForStatsMs);
1577}
1578
1579// Test that we can get incoming byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001580TEST_F(P2PTestConductor, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581 ASSERT_TRUE(CreateTestClients());
1582 LocalP2PTest();
1583
1584 StreamCollectionInterface* remote_streams =
1585 initializing_client()->remote_streams();
1586 ASSERT_GT(remote_streams->count(), 0u);
1587 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1588 MediaStreamTrackInterface* remote_audio_track =
1589 remote_streams->at(0)->GetAudioTracks()[0];
1590 EXPECT_TRUE_WAIT(
1591 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1592 kMaxWaitForStatsMs);
1593
1594 MediaStreamTrackInterface* remote_video_track =
1595 remote_streams->at(0)->GetVideoTracks()[0];
1596 EXPECT_TRUE_WAIT(
1597 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1598 kMaxWaitForStatsMs);
1599}
1600
1601// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001602TEST_F(P2PTestConductor, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001603 ASSERT_TRUE(CreateTestClients());
1604 LocalP2PTest();
1605
1606 StreamCollectionInterface* local_streams =
1607 initializing_client()->local_streams();
1608 ASSERT_GT(local_streams->count(), 0u);
1609 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1610 MediaStreamTrackInterface* local_audio_track =
1611 local_streams->at(0)->GetAudioTracks()[0];
1612 EXPECT_TRUE_WAIT(
1613 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1614 kMaxWaitForStatsMs);
1615
1616 MediaStreamTrackInterface* local_video_track =
1617 local_streams->at(0)->GetVideoTracks()[0];
1618 EXPECT_TRUE_WAIT(
1619 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1620 kMaxWaitForStatsMs);
1621}
1622
Joachim Bauch04e5b492015-05-29 09:40:39 +02001623// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001624TEST_F(P2PTestConductor, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001625 PeerConnectionFactory::Options init_options;
1626 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1627 PeerConnectionFactory::Options recv_options;
1628 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001629 ASSERT_TRUE(
1630 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001631 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1632 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1633 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001634 LocalP2PTest();
1635
torbjorng43166b82016-03-11 00:06:47 -08001636 EXPECT_TRUE_WAIT(
1637 rtc::SSLStreamAdapter::IsAcceptableCipher(
1638 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1639 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001640 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001641 initializing_client()->GetSrtpCipherStats(),
1642 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001643 EXPECT_EQ(1,
1644 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1645 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001646}
1647
1648// Test that DTLS 1.2 is used if both ends support it.
torbjorng79a5a832016-01-15 07:16:51 -08001649TEST_F(P2PTestConductor, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001650 PeerConnectionFactory::Options init_options;
1651 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1652 PeerConnectionFactory::Options recv_options;
1653 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001654 ASSERT_TRUE(
1655 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001656 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1657 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1658 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001659 LocalP2PTest();
1660
torbjorng43166b82016-03-11 00:06:47 -08001661 EXPECT_TRUE_WAIT(
1662 rtc::SSLStreamAdapter::IsAcceptableCipher(
1663 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1664 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001665 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001666 initializing_client()->GetSrtpCipherStats(),
1667 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001668 EXPECT_EQ(1,
1669 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1670 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001671}
1672
1673// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1674// received supports 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001675TEST_F(P2PTestConductor, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001676 PeerConnectionFactory::Options init_options;
1677 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1678 PeerConnectionFactory::Options recv_options;
1679 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001680 ASSERT_TRUE(
1681 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001682 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1683 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1684 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001685 LocalP2PTest();
1686
torbjorng43166b82016-03-11 00:06:47 -08001687 EXPECT_TRUE_WAIT(
1688 rtc::SSLStreamAdapter::IsAcceptableCipher(
1689 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1690 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001691 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001692 initializing_client()->GetSrtpCipherStats(),
1693 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001694 EXPECT_EQ(1,
1695 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1696 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001697}
1698
1699// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1700// received supports 1.2.
torbjorng43166b82016-03-11 00:06:47 -08001701TEST_F(P2PTestConductor, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001702 PeerConnectionFactory::Options init_options;
1703 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1704 PeerConnectionFactory::Options recv_options;
1705 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001706 ASSERT_TRUE(
1707 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001708 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1709 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1710 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001711 LocalP2PTest();
1712
torbjorng43166b82016-03-11 00:06:47 -08001713 EXPECT_TRUE_WAIT(
1714 rtc::SSLStreamAdapter::IsAcceptableCipher(
1715 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1716 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001717 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001718 initializing_client()->GetSrtpCipherStats(),
1719 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001720 EXPECT_EQ(1,
1721 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1722 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001723}
1724
deadbeefb5cb19b2015-11-23 16:39:12 -08001725// This test sets up a call between two parties with audio, video and an RTP
1726// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001727TEST_F(P2PTestConductor, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001728 FakeConstraints setup_constraints;
1729 setup_constraints.SetAllowRtpDataChannels();
1730 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1731 initializing_client()->CreateDataChannel();
1732 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001733 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1734 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1736 kMaxWaitMs);
1737 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1738 kMaxWaitMs);
1739
1740 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001741
1742 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1744 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001745
1746 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1748 kMaxWaitMs);
1749
1750 receiving_client()->data_channel()->Close();
1751 // Send new offer and answer.
1752 receiving_client()->Negotiate();
1753 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1754 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1755}
1756
deadbeefb5cb19b2015-11-23 16:39:12 -08001757// This test sets up a call between two parties with audio, video and an SCTP
1758// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001759TEST_F(P2PTestConductor, LocalP2PTestSctpDataChannel) {
deadbeefb5cb19b2015-11-23 16:39:12 -08001760 ASSERT_TRUE(CreateTestClients());
1761 initializing_client()->CreateDataChannel();
1762 LocalP2PTest();
1763 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1764 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
1765 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1766 kMaxWaitMs);
1767 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
1768
1769 std::string data = "hello world";
1770
1771 initializing_client()->data_channel()->Send(DataBuffer(data));
1772 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1773 kMaxWaitMs);
1774
1775 receiving_client()->data_channel()->Send(DataBuffer(data));
1776 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1777 kMaxWaitMs);
1778
1779 receiving_client()->data_channel()->Close();
deadbeef15887932015-12-14 19:32:34 -08001780 EXPECT_TRUE_WAIT(!initializing_client()->data_observer()->IsOpen(),
1781 kMaxWaitMs);
1782 EXPECT_TRUE_WAIT(!receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
deadbeefb5cb19b2015-11-23 16:39:12 -08001783}
1784
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785// This test sets up a call between two parties and creates a data channel.
1786// The test tests that received data is buffered unless an observer has been
1787// registered.
1788// Rtp data channels can receive data before the underlying
1789// transport has detected that a channel is writable and thus data can be
1790// received before the data channel state changes to open. That is hard to test
1791// but the same buffering is used in that case.
deadbeef7c73bdb2015-12-10 15:10:44 -08001792TEST_F(P2PTestConductor, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793 FakeConstraints setup_constraints;
1794 setup_constraints.SetAllowRtpDataChannels();
1795 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1796 initializing_client()->CreateDataChannel();
1797 initializing_client()->Negotiate();
1798
deadbeefaf1b59c2015-10-15 12:08:41 -07001799 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1800 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001801 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1802 kMaxWaitMs);
1803 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1804 receiving_client()->data_channel()->state(), kMaxWaitMs);
1805
1806 // Unregister the existing observer.
1807 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001808
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001810 SendRtpData(initializing_client()->data_channel(), data);
1811
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001812 // Wait a while to allow the sent data to arrive before an observer is
1813 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001814 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001815
1816 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1817 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1818}
1819
1820// This test sets up a call between two parties with audio, video and but only
1821// the initiating client support data.
deadbeef7c73bdb2015-12-10 15:10:44 -08001822TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001823 FakeConstraints setup_constraints_1;
1824 setup_constraints_1.SetAllowRtpDataChannels();
1825 // Must disable DTLS to make negotiation succeed.
1826 setup_constraints_1.SetMandatory(
1827 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1828 FakeConstraints setup_constraints_2;
1829 setup_constraints_2.SetMandatory(
1830 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1831 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001832 initializing_client()->CreateDataChannel();
1833 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001834 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001835 EXPECT_FALSE(receiving_client()->data_channel());
1836 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1837}
1838
1839// This test sets up a call between two parties with audio, video. When audio
1840// and video is setup and flowing and data channel is negotiated.
deadbeef7c73bdb2015-12-10 15:10:44 -08001841TEST_F(P2PTestConductor, AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842 FakeConstraints setup_constraints;
1843 setup_constraints.SetAllowRtpDataChannels();
1844 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1845 LocalP2PTest();
1846 initializing_client()->CreateDataChannel();
1847 // Send new offer and answer.
1848 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07001849 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1850 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1852 kMaxWaitMs);
1853 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1854 kMaxWaitMs);
1855}
1856
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001857// This test sets up a Jsep call with SCTP DataChannel and verifies the
1858// negotiation is completed without error.
1859#ifdef HAVE_SCTP
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07001860TEST_F(P2PTestConductor, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001861 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001862 FakeConstraints constraints;
1863 constraints.SetMandatory(
1864 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1865 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1866 initializing_client()->CreateDataChannel();
1867 initializing_client()->Negotiate(false, false);
1868}
1869#endif
1870
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001871// This test sets up a call between two parties with audio, and video.
1872// During the call, the initializing side restart ice and the test verifies that
1873// new ice candidates are generated and audio and video still can flow.
deadbeef7c73bdb2015-12-10 15:10:44 -08001874TEST_F(P2PTestConductor, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875 ASSERT_TRUE(CreateTestClients());
1876
1877 // Negotiate and wait for ice completion and make sure audio and video plays.
1878 LocalP2PTest();
1879
1880 // Create a SDP string of the first audio candidate for both clients.
1881 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1882 initializing_client()->pc()->local_description()->candidates(0);
1883 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1884 receiving_client()->pc()->local_description()->candidates(0);
1885 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1886 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1887 std::string initiator_candidate;
1888 EXPECT_TRUE(
1889 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1890 std::string receiver_candidate;
1891 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1892
1893 // Restart ice on the initializing client.
1894 receiving_client()->SetExpectIceRestart(true);
1895 initializing_client()->IceRestart();
1896
1897 // Negotiate and wait for ice completion again and make sure audio and video
1898 // plays.
1899 LocalP2PTest();
1900
1901 // Create a SDP string of the first audio candidate for both clients again.
1902 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1903 initializing_client()->pc()->local_description()->candidates(0);
1904 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1905 receiving_client()->pc()->local_description()->candidates(0);
1906 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1907 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1908 std::string initiator_candidate_restart;
1909 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1910 &initiator_candidate_restart));
1911 std::string receiver_candidate_restart;
1912 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1913 &receiver_candidate_restart));
1914
1915 // Verify that the first candidates in the local session descriptions has
1916 // changed.
1917 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1918 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1919}
1920
deadbeeffaac4972015-11-12 15:33:07 -08001921// This test sets up a call between two parties with audio, and video.
1922// It then renegotiates setting the video m-line to "port 0", then later
1923// renegotiates again, enabling video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001924TEST_F(P2PTestConductor, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08001925 ASSERT_TRUE(CreateTestClients());
1926
1927 // Do initial negotiation. Will result in video and audio sendonly m-lines.
1928 receiving_client()->set_auto_add_stream(false);
1929 initializing_client()->AddMediaStream(true, true);
1930 initializing_client()->Negotiate();
1931
1932 // Negotiate again, disabling the video m-line (receiving client will
1933 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
1934 receiving_client()->SetReceiveVideo(false);
1935 initializing_client()->Negotiate();
1936
1937 // Enable video and do negotiation again, making sure video is received
1938 // end-to-end.
1939 receiving_client()->SetReceiveVideo(true);
1940 receiving_client()->AddMediaStream(true, true);
1941 LocalP2PTest();
1942}
1943
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944// This test sets up a Jsep call between two parties with external
1945// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001946// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1947// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001948TEST_F(P2PTestConductor, DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001949 ASSERT_TRUE(CreateTestClients());
1950 EnableVideoDecoderFactory();
1951 LocalP2PTest();
1952}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001953
deadbeeffac06552015-11-25 11:26:01 -08001954// This tests that if we negotiate after calling CreateSender but before we
1955// have a track, then set a track later, frames from the newly-set track are
1956// received end-to-end.
deadbeef7c73bdb2015-12-10 15:10:44 -08001957TEST_F(P2PTestConductor, EarlyWarmupTest) {
deadbeeffac06552015-11-25 11:26:01 -08001958 ASSERT_TRUE(CreateTestClients());
deadbeefbd7d8f72015-12-18 16:58:44 -08001959 auto audio_sender =
1960 initializing_client()->pc()->CreateSender("audio", "stream_id");
1961 auto video_sender =
1962 initializing_client()->pc()->CreateSender("video", "stream_id");
deadbeeffac06552015-11-25 11:26:01 -08001963 initializing_client()->Negotiate();
1964 // Wait for ICE connection to complete, without any tracks.
1965 // Note that the receiving client WILL (in HandleIncomingOffer) create
1966 // tracks, so it's only the initiator here that's doing early warmup.
1967 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1968 VerifySessionDescriptions();
1969 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1970 initializing_client()->ice_connection_state(),
1971 kMaxWaitForFramesMs);
1972 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1973 receiving_client()->ice_connection_state(),
1974 kMaxWaitForFramesMs);
1975 // Now set the tracks, and expect frames to immediately start flowing.
1976 EXPECT_TRUE(
1977 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
1978 EXPECT_TRUE(
1979 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
hta6b4f8392016-03-10 00:24:31 -08001980 EXPECT_TRUE_WAIT(FramesHaveArrived(kEndAudioFrameCount, kEndVideoFrameCount),
deadbeeffac06552015-11-25 11:26:01 -08001981 kMaxWaitForFramesMs);
1982}
1983
deadbeef0a6c4ca2015-10-06 11:38:28 -07001984class IceServerParsingTest : public testing::Test {
1985 public:
1986 // Convenience for parsing a single URL.
1987 bool ParseUrl(const std::string& url) {
1988 return ParseUrl(url, std::string(), std::string());
1989 }
1990
1991 bool ParseUrl(const std::string& url,
1992 const std::string& username,
1993 const std::string& password) {
1994 PeerConnectionInterface::IceServers servers;
1995 PeerConnectionInterface::IceServer server;
1996 server.urls.push_back(url);
1997 server.username = username;
1998 server.password = password;
1999 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002000 return webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002001 }
2002
2003 protected:
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002004 cricket::ServerAddresses stun_servers_;
2005 std::vector<cricket::RelayServerConfig> turn_servers_;
deadbeef0a6c4ca2015-10-06 11:38:28 -07002006};
2007
2008// Make sure all STUN/TURN prefixes are parsed correctly.
2009TEST_F(IceServerParsingTest, ParseStunPrefixes) {
2010 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002011 EXPECT_EQ(1U, stun_servers_.size());
2012 EXPECT_EQ(0U, turn_servers_.size());
2013 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002014
2015 EXPECT_TRUE(ParseUrl("stuns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002016 EXPECT_EQ(1U, stun_servers_.size());
2017 EXPECT_EQ(0U, turn_servers_.size());
2018 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002019
2020 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002021 EXPECT_EQ(0U, stun_servers_.size());
2022 EXPECT_EQ(1U, turn_servers_.size());
2023 EXPECT_FALSE(turn_servers_[0].ports[0].secure);
2024 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002025
2026 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002027 EXPECT_EQ(0U, stun_servers_.size());
2028 EXPECT_EQ(1U, turn_servers_.size());
2029 EXPECT_TRUE(turn_servers_[0].ports[0].secure);
2030 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002031
2032 // invalid prefixes
2033 EXPECT_FALSE(ParseUrl("stunn:hostname"));
2034 EXPECT_FALSE(ParseUrl(":hostname"));
2035 EXPECT_FALSE(ParseUrl(":"));
2036 EXPECT_FALSE(ParseUrl(""));
2037}
2038
2039TEST_F(IceServerParsingTest, VerifyDefaults) {
2040 // TURNS defaults
2041 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002042 EXPECT_EQ(1U, turn_servers_.size());
2043 EXPECT_EQ(5349, turn_servers_[0].ports[0].address.port());
2044 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
2045 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002046
2047 // TURN defaults
2048 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002049 EXPECT_EQ(1U, turn_servers_.size());
2050 EXPECT_EQ(3478, turn_servers_[0].ports[0].address.port());
2051 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2052 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002053
2054 // STUN defaults
2055 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002056 EXPECT_EQ(1U, stun_servers_.size());
2057 EXPECT_EQ(3478, stun_servers_.begin()->port());
2058 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002059}
2060
2061// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
2062// can be parsed correctly.
2063TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
2064 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002065 EXPECT_EQ(1U, stun_servers_.size());
2066 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2067 EXPECT_EQ(1234, stun_servers_.begin()->port());
2068 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002069
2070 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002071 EXPECT_EQ(1U, stun_servers_.size());
2072 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2073 EXPECT_EQ(4321, stun_servers_.begin()->port());
2074 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002075
2076 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002077 EXPECT_EQ(1U, stun_servers_.size());
2078 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2079 EXPECT_EQ(9999, stun_servers_.begin()->port());
2080 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002081
2082 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002083 EXPECT_EQ(1U, stun_servers_.size());
2084 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2085 EXPECT_EQ(3478, stun_servers_.begin()->port());
2086 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002087
2088 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002089 EXPECT_EQ(1U, stun_servers_.size());
2090 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2091 EXPECT_EQ(3478, stun_servers_.begin()->port());
2092 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002093
2094 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002095 EXPECT_EQ(1U, stun_servers_.size());
2096 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2097 EXPECT_EQ(3478, stun_servers_.begin()->port());
2098 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002099
2100 // Try some invalid hostname:port strings.
2101 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
2102 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002103 EXPECT_FALSE(ParseUrl("stun:hostname:port:more"));
2104 EXPECT_FALSE(ParseUrl("stun:hostname:port more"));
deadbeef0a6c4ca2015-10-06 11:38:28 -07002105 EXPECT_FALSE(ParseUrl("stun:hostname:"));
2106 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
2107 EXPECT_FALSE(ParseUrl("stun::5555"));
2108 EXPECT_FALSE(ParseUrl("stun:"));
2109}
2110
2111// Test parsing the "?transport=xxx" part of the URL.
2112TEST_F(IceServerParsingTest, ParseTransport) {
2113 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002114 EXPECT_EQ(1U, turn_servers_.size());
2115 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
2116 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002117
2118 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002119 EXPECT_EQ(1U, turn_servers_.size());
2120 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2121 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002122
2123 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
2124}
2125
2126// Test parsing ICE username contained in URL.
2127TEST_F(IceServerParsingTest, ParseUsername) {
2128 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002129 EXPECT_EQ(1U, turn_servers_.size());
2130 EXPECT_EQ("user", turn_servers_[0].credentials.username);
2131 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002132
2133 EXPECT_FALSE(ParseUrl("turn:@hostname"));
2134 EXPECT_FALSE(ParseUrl("turn:username@"));
2135 EXPECT_FALSE(ParseUrl("turn:@"));
2136 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
2137}
2138
2139// Test that username and password from IceServer is copied into the resulting
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002140// RelayServerConfig.
deadbeef0a6c4ca2015-10-06 11:38:28 -07002141TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
2142 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002143 EXPECT_EQ(1U, turn_servers_.size());
2144 EXPECT_EQ("username", turn_servers_[0].credentials.username);
2145 EXPECT_EQ("password", turn_servers_[0].credentials.password);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002146}
2147
2148// Ensure that if a server has multiple URLs, each one is parsed.
2149TEST_F(IceServerParsingTest, ParseMultipleUrls) {
2150 PeerConnectionInterface::IceServers servers;
2151 PeerConnectionInterface::IceServer server;
2152 server.urls.push_back("stun:hostname");
2153 server.urls.push_back("turn:hostname");
2154 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002155 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2156 EXPECT_EQ(1U, stun_servers_.size());
2157 EXPECT_EQ(1U, turn_servers_.size());
deadbeef0a6c4ca2015-10-06 11:38:28 -07002158}
2159
Taylor Brandstetter893505d2016-01-07 15:12:48 -08002160// Ensure that TURN servers are given unique priorities,
2161// so that their resulting candidates have unique priorities.
2162TEST_F(IceServerParsingTest, TurnServerPrioritiesUnique) {
2163 PeerConnectionInterface::IceServers servers;
2164 PeerConnectionInterface::IceServer server;
2165 server.urls.push_back("turn:hostname");
2166 server.urls.push_back("turn:hostname2");
2167 servers.push_back(server);
2168 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2169 EXPECT_EQ(2U, turn_servers_.size());
2170 EXPECT_NE(turn_servers_[0].priority, turn_servers_[1].priority);
2171}
2172
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00002173#endif // if !defined(THREAD_SANITIZER)
hta6b4f8392016-03-10 00:24:31 -08002174
2175} // namespace