blob: 6b19ba0fda27e8bbdc8420618d59e0492b91ff23 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
11#include <stdio.h>
12
13#include <algorithm>
14#include <list>
15#include <map>
kwibergd1fe2812016-04-27 06:47:29 -070016#include <memory>
kwiberg0eb15ed2015-12-17 03:04:15 -080017#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018#include <vector>
19
Henrik Kjellander15583c12016-02-10 10:53:12 +010020#include "webrtc/api/dtmfsender.h"
21#include "webrtc/api/fakemetricsobserver.h"
22#include "webrtc/api/localaudiosource.h"
23#include "webrtc/api/mediastreaminterface.h"
24#include "webrtc/api/peerconnection.h"
25#include "webrtc/api/peerconnectionfactory.h"
26#include "webrtc/api/peerconnectioninterface.h"
27#include "webrtc/api/test/fakeaudiocapturemodule.h"
28#include "webrtc/api/test/fakeconstraints.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010029#include "webrtc/api/test/fakeperiodicvideocapturer.h"
Henrik Boströmd79599d2016-06-01 13:58:50 +020030#include "webrtc/api/test/fakertccertificategenerator.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010031#include "webrtc/api/test/fakevideotrackrenderer.h"
32#include "webrtc/api/test/mockpeerconnectionobservers.h"
Taylor Brandstettere5835f52016-09-16 15:07:50 -070033#include "webrtc/base/fakenetwork.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/gunit.h"
Taylor Brandstetter9b5306c2016-08-18 11:40:37 -070035#include "webrtc/base/helpers.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000036#include "webrtc/base/physicalsocketserver.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000037#include "webrtc/base/ssladapter.h"
38#include "webrtc/base/sslstreamadapter.h"
39#include "webrtc/base/thread.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000040#include "webrtc/base/virtualsocketserver.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010041#include "webrtc/media/engine/fakewebrtcvideoengine.h"
kjellanderf4752772016-03-02 05:42:30 -080042#include "webrtc/p2p/base/p2pconstants.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000043#include "webrtc/p2p/base/sessiondescription.h"
Taylor Brandstettere5835f52016-09-16 15:07:50 -070044#include "webrtc/p2p/base/testturnserver.h"
45#include "webrtc/p2p/client/basicportallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010046#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047
48#define MAYBE_SKIP_TEST(feature) \
49 if (!(feature())) { \
50 LOG(LS_INFO) << "Feature disabled... skipping"; \
51 return; \
52 }
53
54using cricket::ContentInfo;
55using cricket::FakeWebRtcVideoDecoder;
56using cricket::FakeWebRtcVideoDecoderFactory;
57using cricket::FakeWebRtcVideoEncoder;
58using cricket::FakeWebRtcVideoEncoderFactory;
59using cricket::MediaContentDescription;
60using webrtc::DataBuffer;
61using webrtc::DataChannelInterface;
62using webrtc::DtmfSender;
63using webrtc::DtmfSenderInterface;
64using webrtc::DtmfSenderObserverInterface;
65using webrtc::FakeConstraints;
66using webrtc::MediaConstraintsInterface;
deadbeeffaac4972015-11-12 15:33:07 -080067using webrtc::MediaStreamInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068using webrtc::MediaStreamTrackInterface;
69using webrtc::MockCreateSessionDescriptionObserver;
70using webrtc::MockDataChannelObserver;
71using webrtc::MockSetSessionDescriptionObserver;
72using webrtc::MockStatsObserver;
deadbeeffaac4972015-11-12 15:33:07 -080073using webrtc::ObserverInterface;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000074using webrtc::PeerConnectionInterface;
Joachim Bauch04e5b492015-05-29 09:40:39 +020075using webrtc::PeerConnectionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076using webrtc::SessionDescriptionInterface;
77using webrtc::StreamCollectionInterface;
78
hta6b4f8392016-03-10 00:24:31 -080079namespace {
80
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000081static const int kMaxWaitMs = 10000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000082// Disable for TSan v2, see
83// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
84// This declaration is also #ifdef'd as it causes uninitialized-variable
85// warnings.
86#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087static const int kMaxWaitForStatsMs = 3000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000088#endif
deadbeeffac06552015-11-25 11:26:01 -080089static const int kMaxWaitForActivationMs = 5000;
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000090static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091static const int kEndAudioFrameCount = 3;
92static const int kEndVideoFrameCount = 3;
93
94static const char kStreamLabelBase[] = "stream_label";
95static const char kVideoTrackLabelBase[] = "video_track";
96static const char kAudioTrackLabelBase[] = "audio_track";
97static const char kDataChannelLabel[] = "data_channel";
98
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +000099// Disable for TSan v2, see
100// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
101// This declaration is also #ifdef'd as it causes unused-variable errors.
102#if !defined(THREAD_SANITIZER)
103// SRTP cipher name negotiated by the tests. This must be updated if the
104// default changes.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800105static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
jbauchcb560652016-08-04 05:20:32 -0700106static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000107#endif
108
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700109// Used to simulate signaling ICE/SDP between two PeerConnections.
110enum Message { MSG_SDP_MESSAGE, MSG_ICE_MESSAGE };
111
112struct SdpMessage {
113 std::string type;
114 std::string msg;
115};
116
117struct IceMessage {
118 std::string sdp_mid;
119 int sdp_mline_index;
120 std::string msg;
121};
122
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123static void RemoveLinesFromSdp(const std::string& line_start,
124 std::string* sdp) {
125 const char kSdpLineEnd[] = "\r\n";
126 size_t ssrc_pos = 0;
127 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
128 std::string::npos) {
129 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
130 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
131 }
132}
133
hta6b4f8392016-03-10 00:24:31 -0800134bool StreamsHaveAudioTrack(StreamCollectionInterface* streams) {
135 for (size_t idx = 0; idx < streams->count(); idx++) {
136 auto stream = streams->at(idx);
137 if (stream->GetAudioTracks().size() > 0) {
138 return true;
139 }
140 }
141 return false;
142}
143
144bool StreamsHaveVideoTrack(StreamCollectionInterface* streams) {
145 for (size_t idx = 0; idx < streams->count(); idx++) {
146 auto stream = streams->at(idx);
147 if (stream->GetVideoTracks().size() > 0) {
148 return true;
149 }
150 }
151 return false;
152}
153
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154class SignalingMessageReceiver {
155 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 virtual void ReceiveSdpMessage(const std::string& type,
157 std::string& msg) = 0;
158 virtual void ReceiveIceMessage(const std::string& sdp_mid,
159 int sdp_mline_index,
160 const std::string& msg) = 0;
161
162 protected:
deadbeefaf1b59c2015-10-15 12:08:41 -0700163 SignalingMessageReceiver() {}
164 virtual ~SignalingMessageReceiver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165};
166
zhihuang184a3fd2016-06-14 11:47:14 -0700167class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
168 public:
169 MockRtpReceiverObserver(cricket::MediaType media_type)
170 : expected_media_type_(media_type) {}
171
172 void OnFirstPacketReceived(cricket::MediaType media_type) override {
173 ASSERT_EQ(expected_media_type_, media_type);
174 first_packet_received_ = true;
175 }
176
177 bool first_packet_received() { return first_packet_received_; }
178
179 virtual ~MockRtpReceiverObserver() {}
180
181 private:
182 bool first_packet_received_ = false;
183 cricket::MediaType expected_media_type_;
184};
185
deadbeefaf1b59c2015-10-15 12:08:41 -0700186class PeerConnectionTestClient : public webrtc::PeerConnectionObserver,
deadbeeffaac4972015-11-12 15:33:07 -0800187 public SignalingMessageReceiver,
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700188 public ObserverInterface,
189 public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 public:
kjellander71a1b612016-11-07 01:18:08 -0800191 // We need these using declarations because there are two versions of each of
192 // the below methods and we only override one of them.
193 // TODO(deadbeef): Remove once there's only one version of the methods.
194 using PeerConnectionObserver::OnAddStream;
195 using PeerConnectionObserver::OnRemoveStream;
196 using PeerConnectionObserver::OnDataChannel;
197
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700198 // If |config| is not provided, uses a default constructed RTCConfiguration.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800199 static PeerConnectionTestClient* CreateClientWithDtlsIdentityStore(
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800200 const std::string& id,
201 const MediaConstraintsInterface* constraints,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800202 const PeerConnectionFactory::Options* options,
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700203 const PeerConnectionInterface::RTCConfiguration* config,
Henrik Boströmd79599d2016-06-01 13:58:50 +0200204 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
perkj8aba9972016-04-10 23:54:34 -0700205 bool prefer_constraint_apis,
danilchape9021a32016-05-17 01:52:02 -0700206 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700207 rtc::Thread* worker_thread) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800208 PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
zhihuang9763d562016-08-05 11:14:50 -0700209 if (!client->Init(constraints, options, config, std::move(cert_generator),
danilchape9021a32016-05-17 01:52:02 -0700210 prefer_constraint_apis, network_thread, worker_thread)) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800211 delete client;
212 return nullptr;
213 }
214 return client;
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800215 }
216
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800217 static PeerConnectionTestClient* CreateClient(
218 const std::string& id,
219 const MediaConstraintsInterface* constraints,
perkj8aba9972016-04-10 23:54:34 -0700220 const PeerConnectionFactory::Options* options,
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700221 const PeerConnectionInterface::RTCConfiguration* config,
danilchape9021a32016-05-17 01:52:02 -0700222 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700223 rtc::Thread* worker_thread) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200224 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
225 rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
226 new FakeRTCCertificateGenerator() : nullptr);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800227
zhihuang9763d562016-08-05 11:14:50 -0700228 return CreateClientWithDtlsIdentityStore(id, constraints, options, config,
229 std::move(cert_generator), true,
230 network_thread, worker_thread);
htaaac2dea2016-03-10 13:35:55 -0800231 }
232
233 static PeerConnectionTestClient* CreateClientPreferNoConstraints(
234 const std::string& id,
perkj8aba9972016-04-10 23:54:34 -0700235 const PeerConnectionFactory::Options* options,
danilchape9021a32016-05-17 01:52:02 -0700236 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700237 rtc::Thread* worker_thread) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200238 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
239 rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
240 new FakeRTCCertificateGenerator() : nullptr);
htaaac2dea2016-03-10 13:35:55 -0800241
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700242 return CreateClientWithDtlsIdentityStore(id, nullptr, options, nullptr,
zhihuang9763d562016-08-05 11:14:50 -0700243 std::move(cert_generator), false,
244 network_thread, worker_thread);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800245 }
246
deadbeefaf1b59c2015-10-15 12:08:41 -0700247 ~PeerConnectionTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248 }
249
deadbeefaf1b59c2015-10-15 12:08:41 -0700250 void Negotiate() { Negotiate(true, true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251
deadbeefaf1b59c2015-10-15 12:08:41 -0700252 void Negotiate(bool audio, bool video) {
kwibergd1fe2812016-04-27 06:47:29 -0700253 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700254 ASSERT_TRUE(DoCreateOffer(&offer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255
deadbeefaf1b59c2015-10-15 12:08:41 -0700256 if (offer->description()->GetContentByName("audio")) {
257 offer->description()->GetContentByName("audio")->rejected = !audio;
258 }
259 if (offer->description()->GetContentByName("video")) {
260 offer->description()->GetContentByName("video")->rejected = !video;
261 }
262
263 std::string sdp;
264 EXPECT_TRUE(offer->ToString(&sdp));
265 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700266 SendSdpMessage(webrtc::SessionDescriptionInterface::kOffer, sdp);
267 }
268
269 void SendSdpMessage(const std::string& type, std::string& msg) {
270 if (signaling_delay_ms_ == 0) {
271 if (signaling_message_receiver_) {
272 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
273 }
274 } else {
275 rtc::Thread::Current()->PostDelayed(
276 RTC_FROM_HERE, signaling_delay_ms_, this, MSG_SDP_MESSAGE,
277 new rtc::TypedMessageData<SdpMessage>({type, msg}));
278 }
279 }
280
281 void SendIceMessage(const std::string& sdp_mid,
282 int sdp_mline_index,
283 const std::string& msg) {
284 if (signaling_delay_ms_ == 0) {
285 if (signaling_message_receiver_) {
286 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
287 msg);
288 }
289 } else {
290 rtc::Thread::Current()->PostDelayed(RTC_FROM_HERE, signaling_delay_ms_,
291 this, MSG_ICE_MESSAGE,
292 new rtc::TypedMessageData<IceMessage>(
293 {sdp_mid, sdp_mline_index, msg}));
294 }
295 }
296
297 // MessageHandler callback.
298 void OnMessage(rtc::Message* msg) override {
299 switch (msg->message_id) {
300 case MSG_SDP_MESSAGE: {
301 auto sdp_message =
302 static_cast<rtc::TypedMessageData<SdpMessage>*>(msg->pdata);
303 if (signaling_message_receiver_) {
304 signaling_message_receiver_->ReceiveSdpMessage(
305 sdp_message->data().type, sdp_message->data().msg);
306 }
307 delete sdp_message;
308 break;
309 }
310 case MSG_ICE_MESSAGE: {
311 auto ice_message =
312 static_cast<rtc::TypedMessageData<IceMessage>*>(msg->pdata);
313 if (signaling_message_receiver_) {
314 signaling_message_receiver_->ReceiveIceMessage(
315 ice_message->data().sdp_mid, ice_message->data().sdp_mline_index,
316 ice_message->data().msg);
317 }
318 delete ice_message;
319 break;
320 }
321 default:
322 RTC_CHECK(false);
323 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700324 }
325
326 // SignalingMessageReceiver callback.
327 void ReceiveSdpMessage(const std::string& type, std::string& msg) override {
328 FilterIncomingSdpMessage(&msg);
329 if (type == webrtc::SessionDescriptionInterface::kOffer) {
330 HandleIncomingOffer(msg);
331 } else {
332 HandleIncomingAnswer(msg);
333 }
334 }
335
336 // SignalingMessageReceiver callback.
337 void ReceiveIceMessage(const std::string& sdp_mid,
338 int sdp_mline_index,
339 const std::string& msg) override {
340 LOG(INFO) << id_ << "ReceiveIceMessage";
kwibergd1fe2812016-04-27 06:47:29 -0700341 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
deadbeefaf1b59c2015-10-15 12:08:41 -0700342 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
343 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
344 }
345
346 // PeerConnectionObserver callbacks.
347 void OnSignalingChange(
348 webrtc::PeerConnectionInterface::SignalingState new_state) override {
349 EXPECT_EQ(pc()->signaling_state(), new_state);
350 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700351 void OnAddStream(
352 rtc::scoped_refptr<MediaStreamInterface> media_stream) override {
deadbeeffaac4972015-11-12 15:33:07 -0800353 media_stream->RegisterObserver(this);
deadbeefaf1b59c2015-10-15 12:08:41 -0700354 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
355 const std::string id = media_stream->GetVideoTracks()[i]->id();
356 ASSERT_TRUE(fake_video_renderers_.find(id) ==
357 fake_video_renderers_.end());
deadbeefc9be0072015-12-14 18:27:57 -0800358 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
359 media_stream->GetVideoTracks()[i]));
deadbeefaf1b59c2015-10-15 12:08:41 -0700360 }
361 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700362 void OnRemoveStream(
363 rtc::scoped_refptr<MediaStreamInterface> media_stream) override {}
deadbeefaf1b59c2015-10-15 12:08:41 -0700364 void OnRenegotiationNeeded() override {}
365 void OnIceConnectionChange(
366 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
367 EXPECT_EQ(pc()->ice_connection_state(), new_state);
368 }
369 void OnIceGatheringChange(
370 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
371 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
372 }
373 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
374 LOG(INFO) << id_ << "OnIceCandidate";
375
376 std::string ice_sdp;
377 EXPECT_TRUE(candidate->ToString(&ice_sdp));
378 if (signaling_message_receiver_ == nullptr) {
379 // Remote party may be deleted.
380 return;
381 }
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700382 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
deadbeefaf1b59c2015-10-15 12:08:41 -0700383 }
384
deadbeeffaac4972015-11-12 15:33:07 -0800385 // MediaStreamInterface callback
386 void OnChanged() override {
387 // Track added or removed from MediaStream, so update our renderers.
388 rtc::scoped_refptr<StreamCollectionInterface> remote_streams =
389 pc()->remote_streams();
390 // Remove renderers for tracks that were removed.
391 for (auto it = fake_video_renderers_.begin();
392 it != fake_video_renderers_.end();) {
393 if (remote_streams->FindVideoTrack(it->first) == nullptr) {
deadbeefc9be0072015-12-14 18:27:57 -0800394 auto to_remove = it++;
395 removed_fake_video_renderers_.push_back(std::move(to_remove->second));
396 fake_video_renderers_.erase(to_remove);
deadbeeffaac4972015-11-12 15:33:07 -0800397 } else {
398 ++it;
399 }
400 }
401 // Create renderers for new video tracks.
402 for (size_t stream_index = 0; stream_index < remote_streams->count();
403 ++stream_index) {
404 MediaStreamInterface* remote_stream = remote_streams->at(stream_index);
405 for (size_t track_index = 0;
406 track_index < remote_stream->GetVideoTracks().size();
407 ++track_index) {
408 const std::string id =
409 remote_stream->GetVideoTracks()[track_index]->id();
410 if (fake_video_renderers_.find(id) != fake_video_renderers_.end()) {
411 continue;
412 }
deadbeefc9be0072015-12-14 18:27:57 -0800413 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
414 remote_stream->GetVideoTracks()[track_index]));
deadbeeffaac4972015-11-12 15:33:07 -0800415 }
416 }
417 }
418
deadbeefaf1b59c2015-10-15 12:08:41 -0700419 void SetVideoConstraints(const webrtc::FakeConstraints& video_constraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000420 video_constraints_ = video_constraint;
421 }
422
423 void AddMediaStream(bool audio, bool video) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700424 std::string stream_label =
425 kStreamLabelBase +
426 rtc::ToString<int>(static_cast<int>(pc()->local_streams()->count()));
deadbeeffaac4972015-11-12 15:33:07 -0800427 rtc::scoped_refptr<MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000428 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000429
430 if (audio && can_receive_audio()) {
deadbeeffac06552015-11-25 11:26:01 -0800431 stream->AddTrack(CreateLocalAudioTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000432 }
433 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000434 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435 }
436
deadbeefaf1b59c2015-10-15 12:08:41 -0700437 EXPECT_TRUE(pc()->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000438 }
439
deadbeefaf1b59c2015-10-15 12:08:41 -0700440 size_t NumberOfLocalMediaStreams() { return pc()->local_streams()->count(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441
442 bool SessionActive() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700443 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444 }
445
deadbeeffaac4972015-11-12 15:33:07 -0800446 // Automatically add a stream when receiving an offer, if we don't have one.
447 // Defaults to true.
448 void set_auto_add_stream(bool auto_add_stream) {
449 auto_add_stream_ = auto_add_stream;
450 }
451
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452 void set_signaling_message_receiver(
deadbeefaf1b59c2015-10-15 12:08:41 -0700453 SignalingMessageReceiver* signaling_message_receiver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454 signaling_message_receiver_ = signaling_message_receiver;
455 }
456
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700457 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
458
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459 void EnableVideoDecoderFactory() {
460 video_decoder_factory_enabled_ = true;
461 fake_video_decoder_factory_->AddSupportedVideoCodecType(
462 webrtc::kVideoCodecVP8);
463 }
464
deadbeefaf1b59c2015-10-15 12:08:41 -0700465 void IceRestart() {
htaaac2dea2016-03-10 13:35:55 -0800466 offer_answer_constraints_.SetMandatoryIceRestart(true);
467 offer_answer_options_.ice_restart = true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700468 SetExpectIceRestart(true);
469 }
470
471 void SetExpectIceRestart(bool expect_restart) {
472 expect_ice_restart_ = expect_restart;
473 }
474
475 bool ExpectIceRestart() const { return expect_ice_restart_; }
476
Honghai Zhang4cedf2b2016-08-31 08:18:11 -0700477 void SetExpectIceRenomination(bool expect_renomination) {
478 expect_ice_renomination_ = expect_renomination;
479 }
480 void SetExpectRemoteIceRenomination(bool expect_renomination) {
481 expect_remote_ice_renomination_ = expect_renomination;
482 }
483 bool ExpectIceRenomination() { return expect_ice_renomination_; }
484 bool ExpectRemoteIceRenomination() { return expect_remote_ice_renomination_; }
485
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700486 // The below 3 methods assume streams will be offered.
487 // Thus they'll only set the "offer to receive" flag to true if it's
488 // currently false, not if it's just unset.
deadbeefaf1b59c2015-10-15 12:08:41 -0700489 void SetReceiveAudioVideo(bool audio, bool video) {
490 SetReceiveAudio(audio);
491 SetReceiveVideo(video);
492 ASSERT_EQ(audio, can_receive_audio());
493 ASSERT_EQ(video, can_receive_video());
494 }
495
496 void SetReceiveAudio(bool audio) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700497 if (audio && can_receive_audio()) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700498 return;
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700499 }
htaaac2dea2016-03-10 13:35:55 -0800500 offer_answer_constraints_.SetMandatoryReceiveAudio(audio);
501 offer_answer_options_.offer_to_receive_audio = audio ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700502 }
503
504 void SetReceiveVideo(bool video) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700505 if (video && can_receive_video()) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700506 return;
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700507 }
508 offer_answer_constraints_.SetMandatoryReceiveVideo(video);
509 offer_answer_options_.offer_to_receive_video = video ? 1 : 0;
510 }
511
512 void SetOfferToReceiveAudioVideo(bool audio, bool video) {
513 offer_answer_constraints_.SetMandatoryReceiveAudio(audio);
514 offer_answer_options_.offer_to_receive_audio = audio ? 1 : 0;
htaaac2dea2016-03-10 13:35:55 -0800515 offer_answer_constraints_.SetMandatoryReceiveVideo(video);
516 offer_answer_options_.offer_to_receive_video = video ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700517 }
518
519 void RemoveMsidFromReceivedSdp(bool remove) { remove_msid_ = remove; }
520
521 void RemoveSdesCryptoFromReceivedSdp(bool remove) { remove_sdes_ = remove; }
522
523 void RemoveBundleFromReceivedSdp(bool remove) { remove_bundle_ = remove; }
524
perkjcaafdba2016-03-20 07:34:29 -0700525 void RemoveCvoFromReceivedSdp(bool remove) { remove_cvo_ = remove; }
526
deadbeefaf1b59c2015-10-15 12:08:41 -0700527 bool can_receive_audio() {
528 bool value;
htaaac2dea2016-03-10 13:35:55 -0800529 if (prefer_constraint_apis_) {
530 if (webrtc::FindConstraint(
531 &offer_answer_constraints_,
532 MediaConstraintsInterface::kOfferToReceiveAudio, &value,
533 nullptr)) {
534 return value;
535 }
536 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700537 }
htaaac2dea2016-03-10 13:35:55 -0800538 return offer_answer_options_.offer_to_receive_audio > 0 ||
539 offer_answer_options_.offer_to_receive_audio ==
540 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700541 }
542
543 bool can_receive_video() {
544 bool value;
htaaac2dea2016-03-10 13:35:55 -0800545 if (prefer_constraint_apis_) {
546 if (webrtc::FindConstraint(
547 &offer_answer_constraints_,
548 MediaConstraintsInterface::kOfferToReceiveVideo, &value,
549 nullptr)) {
550 return value;
551 }
552 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700553 }
htaaac2dea2016-03-10 13:35:55 -0800554 return offer_answer_options_.offer_to_receive_video > 0 ||
555 offer_answer_options_.offer_to_receive_video ==
556 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700557 }
558
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700559 void OnDataChannel(
560 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
deadbeefaf1b59c2015-10-15 12:08:41 -0700561 LOG(INFO) << id_ << "OnDataChannel";
562 data_channel_ = data_channel;
563 data_observer_.reset(new MockDataChannelObserver(data_channel));
564 }
565
zhihuang9763d562016-08-05 11:14:50 -0700566 void CreateDataChannel() { CreateDataChannel(nullptr); }
567
568 void CreateDataChannel(const webrtc::DataChannelInit* init) {
569 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, init);
deadbeefaf1b59c2015-10-15 12:08:41 -0700570 ASSERT_TRUE(data_channel_.get() != nullptr);
571 data_observer_.reset(new MockDataChannelObserver(data_channel_));
572 }
573
deadbeeffac06552015-11-25 11:26:01 -0800574 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack(
575 const std::string& stream_label) {
576 FakeConstraints constraints;
577 // Disable highpass filter so that we can get all the test audio frames.
578 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
579 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
580 peer_connection_factory_->CreateAudioSource(&constraints);
581 // TODO(perkj): Test audio source when it is implemented. Currently audio
582 // always use the default input.
583 std::string label = stream_label + kAudioTrackLabelBase;
584 return peer_connection_factory_->CreateAudioTrack(label, source);
585 }
586
587 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack(
588 const std::string& stream_label) {
589 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
590 FakeConstraints source_constraints = video_constraints_;
591 source_constraints.SetMandatoryMaxFrameRate(10);
592
593 cricket::FakeVideoCapturer* fake_capturer =
594 new webrtc::FakePeriodicVideoCapturer();
perkjcaafdba2016-03-20 07:34:29 -0700595 fake_capturer->SetRotation(capture_rotation_);
deadbeeffac06552015-11-25 11:26:01 -0800596 video_capturers_.push_back(fake_capturer);
perkja3ede6c2016-03-08 01:27:48 +0100597 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
deadbeeffac06552015-11-25 11:26:01 -0800598 peer_connection_factory_->CreateVideoSource(fake_capturer,
599 &source_constraints);
600 std::string label = stream_label + kVideoTrackLabelBase;
perkjcaafdba2016-03-20 07:34:29 -0700601
602 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
603 peer_connection_factory_->CreateVideoTrack(label, source));
604 if (!local_video_renderer_) {
605 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
606 }
607 return track;
deadbeeffac06552015-11-25 11:26:01 -0800608 }
609
deadbeefaf1b59c2015-10-15 12:08:41 -0700610 DataChannelInterface* data_channel() { return data_channel_; }
611 const MockDataChannelObserver* data_observer() const {
612 return data_observer_.get();
613 }
614
hta6b4f8392016-03-10 00:24:31 -0800615 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
deadbeefaf1b59c2015-10-15 12:08:41 -0700616
617 void StopVideoCapturers() {
perkjcaafdba2016-03-20 07:34:29 -0700618 for (auto* capturer : video_capturers_) {
619 capturer->Stop();
deadbeefaf1b59c2015-10-15 12:08:41 -0700620 }
621 }
622
perkjcaafdba2016-03-20 07:34:29 -0700623 void SetCaptureRotation(webrtc::VideoRotation rotation) {
624 ASSERT_TRUE(video_capturers_.empty());
625 capture_rotation_ = rotation;
626 }
627
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628 bool AudioFramesReceivedCheck(int number_of_frames) const {
629 return number_of_frames <= fake_audio_capture_module_->frames_received();
630 }
631
deadbeefc9be0072015-12-14 18:27:57 -0800632 int audio_frames_received() const {
633 return fake_audio_capture_module_->frames_received();
634 }
635
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636 bool VideoFramesReceivedCheck(int number_of_frames) {
637 if (video_decoder_factory_enabled_) {
638 const std::vector<FakeWebRtcVideoDecoder*>& decoders
639 = fake_video_decoder_factory_->decoders();
640 if (decoders.empty()) {
641 return number_of_frames <= 0;
642 }
hta6b4f8392016-03-10 00:24:31 -0800643 // Note - this checks that EACH decoder has the requisite number
644 // of frames. The video_frames_received() function sums them.
deadbeefc9be0072015-12-14 18:27:57 -0800645 for (FakeWebRtcVideoDecoder* decoder : decoders) {
646 if (number_of_frames > decoder->GetNumFramesReceived()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 return false;
648 }
649 }
650 return true;
651 } else {
652 if (fake_video_renderers_.empty()) {
653 return number_of_frames <= 0;
654 }
655
deadbeefc9be0072015-12-14 18:27:57 -0800656 for (const auto& pair : fake_video_renderers_) {
657 if (number_of_frames > pair.second->num_rendered_frames()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000658 return false;
659 }
660 }
661 return true;
662 }
663 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700664
deadbeefc9be0072015-12-14 18:27:57 -0800665 int video_frames_received() const {
666 int total = 0;
667 if (video_decoder_factory_enabled_) {
668 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
669 fake_video_decoder_factory_->decoders();
670 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
671 total += decoder->GetNumFramesReceived();
672 }
673 } else {
674 for (const auto& pair : fake_video_renderers_) {
675 total += pair.second->num_rendered_frames();
676 }
677 for (const auto& renderer : removed_fake_video_renderers_) {
678 total += renderer->num_rendered_frames();
679 }
680 }
681 return total;
682 }
683
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684 // Verify the CreateDtmfSender interface
685 void VerifyDtmf() {
kwibergd1fe2812016-04-27 06:47:29 -0700686 std::unique_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000687 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000688
689 // We can't create a DTMF sender with an invalid audio track or a non local
690 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700691 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000692 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700693 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
694 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695
696 // We should be able to create a DTMF sender from a local track.
697 webrtc::AudioTrackInterface* localtrack =
698 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
699 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700700 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701 dtmf_sender->RegisterObserver(observer.get());
702
703 // Test the DtmfSender object just created.
704 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
705 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
706
707 // We don't need to verify that the DTMF tones are actually sent out because
708 // that is already covered by the tests of the lower level components.
709
710 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
711 std::vector<std::string> tones;
712 tones.push_back("1");
713 tones.push_back("a");
714 tones.push_back("");
715 observer->Verify(tones);
716
717 dtmf_sender->UnregisterObserver();
718 }
719
720 // Verifies that the SessionDescription have rejected the appropriate media
721 // content.
722 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700723 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
724 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 const cricket::SessionDescription* remote_desc =
726 peer_connection_->remote_description()->description();
727 const cricket::SessionDescription* local_desc =
728 peer_connection_->local_description()->description();
729
730 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
731 if (remote_audio_content) {
732 const ContentInfo* audio_content =
733 GetFirstAudioContent(local_desc);
734 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
735 }
736
737 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
738 if (remote_video_content) {
739 const ContentInfo* video_content =
740 GetFirstVideoContent(local_desc);
741 EXPECT_EQ(can_receive_video(), !video_content->rejected);
742 }
743 }
744
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700746 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 const cricket::SessionDescription* desc =
748 peer_connection_->local_description()->description();
749 const cricket::ContentInfos& contents = desc->contents();
750
751 for (size_t index = 0; index < contents.size(); ++index) {
752 if (contents[index].rejected)
753 continue;
754 const cricket::TransportDescription* transport_desc =
755 desc->GetTransportDescriptionByName(contents[index].name);
756
757 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000758 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 if (ufragpair_it == ice_ufrag_pwd_.end()) {
760 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000761 ice_ufrag_pwd_[static_cast<int>(index)] =
762 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 } else if (ExpectIceRestart()) {
764 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
765 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
766 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
767 } else {
768 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
769 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
770 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
771 }
772 }
773 }
774
Honghai Zhang4cedf2b2016-08-31 08:18:11 -0700775 void VerifyLocalIceRenomination() {
776 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
777 const cricket::SessionDescription* desc =
778 peer_connection_->local_description()->description();
779 const cricket::ContentInfos& contents = desc->contents();
780
781 for (auto content : contents) {
782 if (content.rejected)
783 continue;
784 const cricket::TransportDescription* transport_desc =
785 desc->GetTransportDescriptionByName(content.name);
786 const auto& options = transport_desc->transport_options;
787 auto iter = std::find(options.begin(), options.end(),
788 cricket::ICE_RENOMINATION_STR);
789 EXPECT_EQ(ExpectIceRenomination(), iter != options.end());
790 }
791 }
792
793 void VerifyRemoteIceRenomination() {
794 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
795 const cricket::SessionDescription* desc =
796 peer_connection_->remote_description()->description();
797 const cricket::ContentInfos& contents = desc->contents();
798
799 for (auto content : contents) {
800 if (content.rejected)
801 continue;
802 const cricket::TransportDescription* transport_desc =
803 desc->GetTransportDescriptionByName(content.name);
804 const auto& options = transport_desc->transport_options;
805 auto iter = std::find(options.begin(), options.end(),
806 cricket::ICE_RENOMINATION_STR);
807 EXPECT_EQ(ExpectRemoteIceRenomination(), iter != options.end());
808 }
809 }
810
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000812 rtc::scoped_refptr<MockStatsObserver>
813 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000814 EXPECT_TRUE(peer_connection_->GetStats(
815 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700817 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 return observer->AudioOutputLevel();
819 }
820
821 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000822 rtc::scoped_refptr<MockStatsObserver>
823 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000824 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700825 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700827 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 return observer->AudioInputLevel();
829 }
830
831 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000832 rtc::scoped_refptr<MockStatsObserver>
833 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000834 EXPECT_TRUE(peer_connection_->GetStats(
835 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700837 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 return observer->BytesReceived();
839 }
840
841 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000842 rtc::scoped_refptr<MockStatsObserver>
843 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000844 EXPECT_TRUE(peer_connection_->GetStats(
845 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700847 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 return observer->BytesSent();
849 }
850
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000851 int GetAvailableReceivedBandwidthStats() {
852 rtc::scoped_refptr<MockStatsObserver>
853 observer(new rtc::RefCountedObject<MockStatsObserver>());
854 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700855 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000856 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700857 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000858 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000859 return bw;
860 }
861
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000862 std::string GetDtlsCipherStats() {
863 rtc::scoped_refptr<MockStatsObserver>
864 observer(new rtc::RefCountedObject<MockStatsObserver>());
865 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700866 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000867 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700868 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000869 return observer->DtlsCipher();
870 }
871
872 std::string GetSrtpCipherStats() {
873 rtc::scoped_refptr<MockStatsObserver>
874 observer(new rtc::RefCountedObject<MockStatsObserver>());
875 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700876 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000877 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700878 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000879 return observer->SrtpCipher();
880 }
881
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 int rendered_width() {
883 EXPECT_FALSE(fake_video_renderers_.empty());
884 return fake_video_renderers_.empty() ? 1 :
885 fake_video_renderers_.begin()->second->width();
886 }
887
888 int rendered_height() {
889 EXPECT_FALSE(fake_video_renderers_.empty());
890 return fake_video_renderers_.empty() ? 1 :
891 fake_video_renderers_.begin()->second->height();
892 }
893
perkjcaafdba2016-03-20 07:34:29 -0700894 webrtc::VideoRotation rendered_rotation() {
895 EXPECT_FALSE(fake_video_renderers_.empty());
896 return fake_video_renderers_.empty()
897 ? webrtc::kVideoRotation_0
898 : fake_video_renderers_.begin()->second->rotation();
899 }
900
901 int local_rendered_width() {
902 return local_video_renderer_ ? local_video_renderer_->width() : 1;
903 }
904
905 int local_rendered_height() {
906 return local_video_renderer_ ? local_video_renderer_->height() : 1;
907 }
908
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 size_t number_of_remote_streams() {
910 if (!pc())
911 return 0;
912 return pc()->remote_streams()->count();
913 }
914
hta6b4f8392016-03-10 00:24:31 -0800915 StreamCollectionInterface* remote_streams() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916 if (!pc()) {
917 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700918 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 }
920 return pc()->remote_streams();
921 }
922
923 StreamCollectionInterface* local_streams() {
924 if (!pc()) {
925 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700926 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 }
928 return pc()->local_streams();
929 }
930
hta6b4f8392016-03-10 00:24:31 -0800931 bool HasLocalAudioTrack() { return StreamsHaveAudioTrack(local_streams()); }
932
933 bool HasLocalVideoTrack() { return StreamsHaveVideoTrack(local_streams()); }
934
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
936 return pc()->signaling_state();
937 }
938
939 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
940 return pc()->ice_connection_state();
941 }
942
943 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
944 return pc()->ice_gathering_state();
945 }
946
zhihuang184a3fd2016-06-14 11:47:14 -0700947 std::vector<std::unique_ptr<MockRtpReceiverObserver>> const&
948 rtp_receiver_observers() {
949 return rtp_receiver_observers_;
950 }
951
952 void SetRtpReceiverObservers() {
953 rtp_receiver_observers_.clear();
954 for (auto receiver : pc()->GetReceivers()) {
955 std::unique_ptr<MockRtpReceiverObserver> observer(
956 new MockRtpReceiverObserver(receiver->media_type()));
957 receiver->SetObserver(observer.get());
958 rtp_receiver_observers_.push_back(std::move(observer));
959 }
960 }
961
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962 private:
963 class DummyDtmfObserver : public DtmfSenderObserverInterface {
964 public:
965 DummyDtmfObserver() : completed_(false) {}
966
967 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700968 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969 tones_.push_back(tone);
970 if (tone.empty()) {
971 completed_ = true;
972 }
973 }
974
975 void Verify(const std::vector<std::string>& tones) const {
976 ASSERT_TRUE(tones_.size() == tones.size());
977 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
978 }
979
980 bool completed() const { return completed_; }
981
982 private:
983 bool completed_;
984 std::vector<std::string> tones_;
985 };
986
deadbeefaf1b59c2015-10-15 12:08:41 -0700987 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
988
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800989 bool Init(
990 const MediaConstraintsInterface* constraints,
991 const PeerConnectionFactory::Options* options,
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700992 const PeerConnectionInterface::RTCConfiguration* config,
Henrik Boströmd79599d2016-06-01 13:58:50 +0200993 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
perkj8aba9972016-04-10 23:54:34 -0700994 bool prefer_constraint_apis,
danilchape9021a32016-05-17 01:52:02 -0700995 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700996 rtc::Thread* worker_thread) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700997 EXPECT_TRUE(!peer_connection_);
998 EXPECT_TRUE(!peer_connection_factory_);
htaaac2dea2016-03-10 13:35:55 -0800999 if (!prefer_constraint_apis) {
1000 EXPECT_TRUE(!constraints);
1001 }
1002 prefer_constraint_apis_ = prefer_constraint_apis;
1003
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001004 fake_network_manager_.reset(new rtc::FakeNetworkManager());
1005 fake_network_manager_->AddInterface(rtc::SocketAddress("192.168.1.1", 0));
1006
kwibergd1fe2812016-04-27 06:47:29 -07001007 std::unique_ptr<cricket::PortAllocator> port_allocator(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001008 new cricket::BasicPortAllocator(fake_network_manager_.get()));
deadbeefaf1b59c2015-10-15 12:08:41 -07001009 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
1010
1011 if (fake_audio_capture_module_ == nullptr) {
1012 return false;
1013 }
1014 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
1015 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
danilchape9021a32016-05-17 01:52:02 -07001016 rtc::Thread* const signaling_thread = rtc::Thread::Current();
deadbeefaf1b59c2015-10-15 12:08:41 -07001017 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -07001018 network_thread, worker_thread, signaling_thread,
1019 fake_audio_capture_module_, fake_video_encoder_factory_,
1020 fake_video_decoder_factory_);
deadbeefaf1b59c2015-10-15 12:08:41 -07001021 if (!peer_connection_factory_) {
1022 return false;
1023 }
1024 if (options) {
1025 peer_connection_factory_->SetOptions(*options);
1026 }
zhihuang9763d562016-08-05 11:14:50 -07001027 peer_connection_ =
1028 CreatePeerConnection(std::move(port_allocator), constraints, config,
1029 std::move(cert_generator));
deadbeefaf1b59c2015-10-15 12:08:41 -07001030 return peer_connection_.get() != nullptr;
1031 }
1032
deadbeefaf1b59c2015-10-15 12:08:41 -07001033 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
kwibergd1fe2812016-04-27 06:47:29 -07001034 std::unique_ptr<cricket::PortAllocator> port_allocator,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001035 const MediaConstraintsInterface* constraints,
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001036 const PeerConnectionInterface::RTCConfiguration* config,
Henrik Boströmd79599d2016-06-01 13:58:50 +02001037 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001038 // CreatePeerConnection with RTCConfiguration.
1039 PeerConnectionInterface::RTCConfiguration default_config;
1040
1041 if (!config) {
1042 config = &default_config;
1043 }
1044
Henrik Boströmd79599d2016-06-01 13:58:50 +02001045 return peer_connection_factory_->CreatePeerConnection(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001046 *config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +02001047 std::move(cert_generator), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001048 }
1049
1050 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001051 LOG(INFO) << id_ << "HandleIncomingOffer ";
deadbeeffaac4972015-11-12 15:33:07 -08001052 if (NumberOfLocalMediaStreams() == 0 && auto_add_stream_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053 // If we are not sending any streams ourselves it is time to add some.
1054 AddMediaStream(true, true);
1055 }
kwibergd1fe2812016-04-27 06:47:29 -07001056 std::unique_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -07001057 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
zhihuang184a3fd2016-06-14 11:47:14 -07001059 // Set the RtpReceiverObserver after receivers are created.
1060 SetRtpReceiverObservers();
kwibergd1fe2812016-04-27 06:47:29 -07001061 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001062 EXPECT_TRUE(DoCreateAnswer(&answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001063 std::string sdp;
1064 EXPECT_TRUE(answer->ToString(&sdp));
1065 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001066 SendSdpMessage(webrtc::SessionDescriptionInterface::kAnswer, sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001067 }
1068
1069 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001070 LOG(INFO) << id_ << "HandleIncomingAnswer";
kwibergd1fe2812016-04-27 06:47:29 -07001071 std::unique_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -07001072 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
zhihuang184a3fd2016-06-14 11:47:14 -07001074 // Set the RtpReceiverObserver after receivers are created.
1075 SetRtpReceiverObservers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001076 }
1077
kwibergd1fe2812016-04-27 06:47:29 -07001078 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001079 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001080 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
1081 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001082 MockCreateSessionDescriptionObserver>());
htaaac2dea2016-03-10 13:35:55 -08001083 if (prefer_constraint_apis_) {
1084 if (offer) {
1085 pc()->CreateOffer(observer, &offer_answer_constraints_);
1086 } else {
1087 pc()->CreateAnswer(observer, &offer_answer_constraints_);
1088 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001089 } else {
htaaac2dea2016-03-10 13:35:55 -08001090 if (offer) {
1091 pc()->CreateOffer(observer, offer_answer_options_);
1092 } else {
1093 pc()->CreateAnswer(observer, offer_answer_options_);
1094 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095 }
1096 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
kwiberg2bbff992016-03-16 11:03:04 -07001097 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098 if (observer->result() && ExpectIceRestart()) {
1099 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
1100 }
1101 return observer->result();
1102 }
1103
kwibergd1fe2812016-04-27 06:47:29 -07001104 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001105 return DoCreateOfferAnswer(desc, true);
1106 }
1107
kwibergd1fe2812016-04-27 06:47:29 -07001108 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001109 return DoCreateOfferAnswer(desc, false);
1110 }
1111
1112 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001113 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
1114 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -07001116 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001117 pc()->SetLocalDescription(observer, desc);
1118 // Ignore the observer result. If we wait for the result with
1119 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
1120 // before the offer which is an error.
1121 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001122 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001123 // ProcessMessages waits at least 1ms but processes all messages before
1124 // returning. Since this test is synchronous and send messages to the remote
1125 // peer whenever a callback is invoked, this can lead to messages being
1126 // sent to the remote peer in the wrong order.
1127 // TODO(perkj): Find a way to check the result without risking that the
1128 // order of sent messages are changed. Ex- by posting all messages that are
1129 // sent to the remote peer.
1130 return true;
1131 }
1132
1133 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001134 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
1135 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001136 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -07001137 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 pc()->SetRemoteDescription(observer, desc);
1139 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
1140 return observer->result();
1141 }
1142
1143 // This modifies all received SDP messages before they are processed.
1144 void FilterIncomingSdpMessage(std::string* sdp) {
1145 if (remove_msid_) {
1146 const char kSdpSsrcAttribute[] = "a=ssrc:";
1147 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
1148 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
1149 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
1150 }
1151 if (remove_bundle_) {
1152 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
1153 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
1154 }
1155 if (remove_sdes_) {
1156 const char kSdpSdesCryptoAttribute[] = "a=crypto";
1157 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
1158 }
perkjcaafdba2016-03-20 07:34:29 -07001159 if (remove_cvo_) {
1160 const char kSdpCvoExtenstion[] = "urn:3gpp:video-orientation";
1161 RemoveLinesFromSdp(kSdpCvoExtenstion, sdp);
1162 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001163 }
1164
deadbeefaf1b59c2015-10-15 12:08:41 -07001165 std::string id_;
1166
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001167 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
1168
deadbeefaf1b59c2015-10-15 12:08:41 -07001169 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
1170 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
1171 peer_connection_factory_;
1172
htaaac2dea2016-03-10 13:35:55 -08001173 bool prefer_constraint_apis_ = true;
deadbeeffaac4972015-11-12 15:33:07 -08001174 bool auto_add_stream_ = true;
1175
deadbeefaf1b59c2015-10-15 12:08:41 -07001176 typedef std::pair<std::string, std::string> IceUfragPwdPair;
1177 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
1178 bool expect_ice_restart_ = false;
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07001179 bool expect_ice_renomination_ = false;
1180 bool expect_remote_ice_renomination_ = false;
deadbeefaf1b59c2015-10-15 12:08:41 -07001181
deadbeefc9be0072015-12-14 18:27:57 -08001182 // Needed to keep track of number of frames sent.
deadbeefaf1b59c2015-10-15 12:08:41 -07001183 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
1184 // Needed to keep track of number of frames received.
kwibergd1fe2812016-04-27 06:47:29 -07001185 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
deadbeefc9be0072015-12-14 18:27:57 -08001186 fake_video_renderers_;
1187 // Needed to ensure frames aren't received for removed tracks.
kwibergd1fe2812016-04-27 06:47:29 -07001188 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
deadbeefc9be0072015-12-14 18:27:57 -08001189 removed_fake_video_renderers_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001190 // Needed to keep track of number of frames received when external decoder
1191 // used.
1192 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
1193 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
1194 bool video_decoder_factory_enabled_ = false;
1195 webrtc::FakeConstraints video_constraints_;
1196
1197 // For remote peer communication.
1198 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001199 int signaling_delay_ms_ = 0;
deadbeefaf1b59c2015-10-15 12:08:41 -07001200
1201 // Store references to the video capturers we've created, so that we can stop
1202 // them, if required.
perkjcaafdba2016-03-20 07:34:29 -07001203 std::vector<cricket::FakeVideoCapturer*> video_capturers_;
1204 webrtc::VideoRotation capture_rotation_ = webrtc::kVideoRotation_0;
1205 // |local_video_renderer_| attached to the first created local video track.
kwibergd1fe2812016-04-27 06:47:29 -07001206 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001207
htaaac2dea2016-03-10 13:35:55 -08001208 webrtc::FakeConstraints offer_answer_constraints_;
1209 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001210 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
1211 bool remove_bundle_ =
1212 false; // True if bundle should be removed in received SDP.
1213 bool remove_sdes_ =
1214 false; // True if a=crypto should be removed in received SDP.
perkjcaafdba2016-03-20 07:34:29 -07001215 // |remove_cvo_| is true if extension urn:3gpp:video-orientation should be
1216 // removed in the received SDP.
1217 bool remove_cvo_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001218
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001219 rtc::scoped_refptr<DataChannelInterface> data_channel_;
kwibergd1fe2812016-04-27 06:47:29 -07001220 std::unique_ptr<MockDataChannelObserver> data_observer_;
zhihuang184a3fd2016-06-14 11:47:14 -07001221
1222 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223};
1224
deadbeef7c73bdb2015-12-10 15:10:44 -08001225class P2PTestConductor : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 public:
deadbeef7c73bdb2015-12-10 15:10:44 -08001227 P2PTestConductor()
deadbeefeff5b852016-05-27 14:18:01 -07001228 : pss_(new rtc::PhysicalSocketServer),
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001229 ss_(new rtc::VirtualSocketServer(pss_.get())),
deadbeefeff5b852016-05-27 14:18:01 -07001230 network_thread_(new rtc::Thread(ss_.get())),
1231 worker_thread_(rtc::Thread::Create()) {
danilchape9021a32016-05-17 01:52:02 -07001232 RTC_CHECK(network_thread_->Start());
1233 RTC_CHECK(worker_thread_->Start());
perkj8aba9972016-04-10 23:54:34 -07001234 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001235
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001236 bool SessionActive() {
1237 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001238 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001239 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001240
hta6b4f8392016-03-10 00:24:31 -08001241 // Return true if the number of frames provided have been received
1242 // on the video and audio tracks provided.
1243 bool FramesHaveArrived(int audio_frames_to_receive,
1244 int video_frames_to_receive) {
1245 bool all_good = true;
1246 if (initiating_client_->HasLocalAudioTrack() &&
1247 receiving_client_->can_receive_audio()) {
1248 all_good &=
1249 receiving_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1250 }
1251 if (initiating_client_->HasLocalVideoTrack() &&
1252 receiving_client_->can_receive_video()) {
1253 all_good &=
1254 receiving_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1255 }
1256 if (receiving_client_->HasLocalAudioTrack() &&
1257 initiating_client_->can_receive_audio()) {
1258 all_good &=
1259 initiating_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1260 }
1261 if (receiving_client_->HasLocalVideoTrack() &&
1262 initiating_client_->can_receive_video()) {
1263 all_good &=
1264 initiating_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1265 }
1266 return all_good;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001267 }
hta6b4f8392016-03-10 00:24:31 -08001268
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001269 void VerifyDtmf() {
1270 initiating_client_->VerifyDtmf();
1271 receiving_client_->VerifyDtmf();
1272 }
1273
1274 void TestUpdateOfferWithRejectedContent() {
deadbeefc9be0072015-12-14 18:27:57 -08001275 // Renegotiate, rejecting the video m-line.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001276 initiating_client_->Negotiate(true, false);
deadbeefc9be0072015-12-14 18:27:57 -08001277 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1278
1279 int pc1_audio_received = initiating_client_->audio_frames_received();
1280 int pc1_video_received = initiating_client_->video_frames_received();
1281 int pc2_audio_received = receiving_client_->audio_frames_received();
1282 int pc2_video_received = receiving_client_->video_frames_received();
1283
1284 // Wait for some additional audio frames to be received.
1285 EXPECT_TRUE_WAIT(initiating_client_->AudioFramesReceivedCheck(
1286 pc1_audio_received + kEndAudioFrameCount) &&
1287 receiving_client_->AudioFramesReceivedCheck(
1288 pc2_audio_received + kEndAudioFrameCount),
1289 kMaxWaitForFramesMs);
1290
1291 // During this time, we shouldn't have received any additional video frames
1292 // for the rejected video tracks.
1293 EXPECT_EQ(pc1_video_received, initiating_client_->video_frames_received());
1294 EXPECT_EQ(pc2_video_received, receiving_client_->video_frames_received());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001295 }
1296
deadbeefc23efae2016-12-02 15:45:31 -08001297 void VerifyRenderedAspectRatio(int width, int height) {
1298 VerifyRenderedAspectRatio(width, height, webrtc::kVideoRotation_0);
perkjcaafdba2016-03-20 07:34:29 -07001299 }
1300
deadbeefc23efae2016-12-02 15:45:31 -08001301 void VerifyRenderedAspectRatio(int width,
1302 int height,
1303 webrtc::VideoRotation rotation) {
deadbeefb4659802016-12-01 16:23:28 -08001304 double expected_aspect_ratio = static_cast<double>(width) / height;
1305 double receiving_client_rendered_aspect_ratio =
1306 static_cast<double>(receiving_client()->rendered_width()) /
1307 receiving_client()->rendered_height();
1308 double initializing_client_rendered_aspect_ratio =
1309 static_cast<double>(initializing_client()->rendered_width()) /
1310 initializing_client()->rendered_height();
deadbeefc23efae2016-12-02 15:45:31 -08001311 double initializing_client_local_rendered_aspect_ratio =
1312 static_cast<double>(initializing_client()->local_rendered_width()) /
1313 initializing_client()->local_rendered_height();
1314 // Verify end-to-end rendered aspect ratio.
deadbeefb4659802016-12-01 16:23:28 -08001315 EXPECT_EQ(expected_aspect_ratio, receiving_client_rendered_aspect_ratio);
1316 EXPECT_EQ(expected_aspect_ratio, initializing_client_rendered_aspect_ratio);
deadbeefc23efae2016-12-02 15:45:31 -08001317 // Verify aspect ratio of the local preview.
1318 EXPECT_EQ(expected_aspect_ratio,
1319 initializing_client_local_rendered_aspect_ratio);
deadbeefb4659802016-12-01 16:23:28 -08001320
deadbeefc23efae2016-12-02 15:45:31 -08001321 // Verify rotation.
perkjcaafdba2016-03-20 07:34:29 -07001322 EXPECT_EQ(rotation, receiving_client()->rendered_rotation());
perkjcaafdba2016-03-20 07:34:29 -07001323 EXPECT_EQ(rotation, initializing_client()->rendered_rotation());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001324 }
1325
1326 void VerifySessionDescriptions() {
1327 initiating_client_->VerifyRejectedMediaInSessionDescription();
1328 receiving_client_->VerifyRejectedMediaInSessionDescription();
1329 initiating_client_->VerifyLocalIceUfragAndPassword();
1330 receiving_client_->VerifyLocalIceUfragAndPassword();
1331 }
1332
deadbeef7c73bdb2015-12-10 15:10:44 -08001333 ~P2PTestConductor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001335 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001336 }
1337 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001338 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001339 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340 }
1341
deadbeefaf1b59c2015-10-15 12:08:41 -07001342 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343
1344 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1345 MediaConstraintsInterface* recv_constraints) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001346 return CreateTestClients(init_constraints, nullptr, nullptr,
1347 recv_constraints, nullptr, nullptr);
1348 }
1349
1350 bool CreateTestClients(
1351 const PeerConnectionInterface::RTCConfiguration& init_config,
1352 const PeerConnectionInterface::RTCConfiguration& recv_config) {
1353 return CreateTestClients(nullptr, nullptr, &init_config, nullptr, nullptr,
1354 &recv_config);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001355 }
1356
htaaac2dea2016-03-10 13:35:55 -08001357 bool CreateTestClientsThatPreferNoConstraints() {
1358 initiating_client_.reset(
perkj8aba9972016-04-10 23:54:34 -07001359 PeerConnectionTestClient::CreateClientPreferNoConstraints(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001360 "Caller: ", nullptr, network_thread_.get(), worker_thread_.get()));
htaaac2dea2016-03-10 13:35:55 -08001361 receiving_client_.reset(
perkj8aba9972016-04-10 23:54:34 -07001362 PeerConnectionTestClient::CreateClientPreferNoConstraints(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001363 "Callee: ", nullptr, network_thread_.get(), worker_thread_.get()));
htaaac2dea2016-03-10 13:35:55 -08001364 if (!initiating_client_ || !receiving_client_) {
1365 return false;
1366 }
1367 // Remember the choice for possible later resets of the clients.
1368 prefer_constraint_apis_ = false;
1369 SetSignalingReceivers();
1370 return true;
1371 }
1372
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001373 bool CreateTestClients(
1374 MediaConstraintsInterface* init_constraints,
1375 PeerConnectionFactory::Options* init_options,
1376 const PeerConnectionInterface::RTCConfiguration* init_config,
1377 MediaConstraintsInterface* recv_constraints,
1378 PeerConnectionFactory::Options* recv_options,
1379 const PeerConnectionInterface::RTCConfiguration* recv_config) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001380 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001381 "Caller: ", init_constraints, init_options, init_config,
zhihuang9763d562016-08-05 11:14:50 -07001382 network_thread_.get(), worker_thread_.get()));
deadbeefaf1b59c2015-10-15 12:08:41 -07001383 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001384 "Callee: ", recv_constraints, recv_options, recv_config,
zhihuang9763d562016-08-05 11:14:50 -07001385 network_thread_.get(), worker_thread_.get()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001386 if (!initiating_client_ || !receiving_client_) {
1387 return false;
1388 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001389 SetSignalingReceivers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001390 return true;
1391 }
1392
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001393 void SetSignalingReceivers() {
1394 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
1395 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1396 }
1397
1398 void SetSignalingDelayMs(int delay_ms) {
1399 initiating_client_->set_signaling_delay_ms(delay_ms);
1400 receiving_client_->set_signaling_delay_ms(delay_ms);
1401 }
1402
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001403 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1404 const webrtc::FakeConstraints& recv_constraints) {
1405 initiating_client_->SetVideoConstraints(init_constraints);
1406 receiving_client_->SetVideoConstraints(recv_constraints);
1407 }
1408
perkjcaafdba2016-03-20 07:34:29 -07001409 void SetCaptureRotation(webrtc::VideoRotation rotation) {
1410 initiating_client_->SetCaptureRotation(rotation);
1411 receiving_client_->SetCaptureRotation(rotation);
1412 }
1413
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001414 void EnableVideoDecoderFactory() {
1415 initiating_client_->EnableVideoDecoderFactory();
1416 receiving_client_->EnableVideoDecoderFactory();
1417 }
1418
1419 // This test sets up a call between two parties. Both parties send static
1420 // frames to each other. Once the test is finished the number of sent frames
1421 // is compared to the number of received frames.
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -07001422 void LocalP2PTest() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001423 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1424 initiating_client_->AddMediaStream(true, true);
1425 }
1426 initiating_client_->Negotiate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001427 // Assert true is used here since next tests are guaranteed to fail and
1428 // would eat up 5 seconds.
1429 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1430 VerifySessionDescriptions();
1431
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001432 int audio_frame_count = kEndAudioFrameCount;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001433 int video_frame_count = kEndVideoFrameCount;
hta6b4f8392016-03-10 00:24:31 -08001434 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1435
1436 if ((!initiating_client_->can_receive_audio() &&
1437 !initiating_client_->can_receive_video()) ||
1438 (!receiving_client_->can_receive_audio() &&
1439 !receiving_client_->can_receive_video())) {
1440 // Neither audio nor video will flow, so connections won't be
1441 // established. There's nothing more to check.
1442 // TODO(hta): Check connection if there's a data channel.
1443 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001444 }
1445
hta6b4f8392016-03-10 00:24:31 -08001446 // Audio or video is expected to flow, so both clients should reach the
1447 // Connected state, and the offerer (ICE controller) should proceed to
1448 // Completed.
1449 // Note: These tests have been observed to fail under heavy load at
1450 // shorter timeouts, so they may be flaky.
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -07001451 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1452 initiating_client_->ice_connection_state(),
1453 kMaxWaitForFramesMs);
hta6b4f8392016-03-10 00:24:31 -08001454 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1455 receiving_client_->ice_connection_state(),
1456 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001457
hta6b4f8392016-03-10 00:24:31 -08001458 // The ICE gathering state should end up in kIceGatheringComplete,
1459 // but there's a bug that prevents this at the moment, and the state
1460 // machine is being updated by the WEBRTC WG.
1461 // TODO(hta): Update this check when spec revisions finish.
1462 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1463 initiating_client_->ice_gathering_state());
1464 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1465 receiving_client_->ice_gathering_state(),
1466 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001467
hta6b4f8392016-03-10 00:24:31 -08001468 // Check that the expected number of frames have arrived.
1469 EXPECT_TRUE_WAIT(FramesHaveArrived(audio_frame_count, video_frame_count),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001470 kMaxWaitForFramesMs);
1471 }
1472
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001473 void SetupAndVerifyDtlsCall() {
1474 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1475 FakeConstraints setup_constraints;
1476 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1477 true);
kthelgason876222f2016-11-29 01:44:11 -08001478 // Disable resolution adaptation, we don't want it interfering with the
1479 // test results.
1480 webrtc::PeerConnectionInterface::RTCConfiguration rtc_config;
1481 rtc_config.set_cpu_adaptation(false);
1482
1483 ASSERT_TRUE(CreateTestClients(&setup_constraints, nullptr, &rtc_config,
1484 &setup_constraints, nullptr, &rtc_config));
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001485 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001486 VerifyRenderedAspectRatio(640, 480);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001487 }
1488
1489 PeerConnectionTestClient* CreateDtlsClientWithAlternateKey() {
1490 FakeConstraints setup_constraints;
1491 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1492 true);
kthelgason876222f2016-11-29 01:44:11 -08001493 // Disable resolution adaptation, we don't want it interfering with the
1494 // test results.
1495 webrtc::PeerConnectionInterface::RTCConfiguration rtc_config;
1496 rtc_config.set_cpu_adaptation(false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001497
Henrik Boströmd79599d2016-06-01 13:58:50 +02001498 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1499 rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
1500 new FakeRTCCertificateGenerator() : nullptr);
1501 cert_generator->use_alternate_key();
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001502
1503 // Make sure the new client is using a different certificate.
1504 return PeerConnectionTestClient::CreateClientWithDtlsIdentityStore(
kthelgason876222f2016-11-29 01:44:11 -08001505 "New Peer: ", &setup_constraints, nullptr, &rtc_config,
Henrik Boströmd79599d2016-06-01 13:58:50 +02001506 std::move(cert_generator), prefer_constraint_apis_,
danilchape9021a32016-05-17 01:52:02 -07001507 network_thread_.get(), worker_thread_.get());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001508 }
1509
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001510 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1511 // Messages may get lost on the unreliable DataChannel, so we send multiple
1512 // times to avoid test flakiness.
1513 static const size_t kSendAttempts = 5;
1514
1515 for (size_t i = 0; i < kSendAttempts; ++i) {
1516 dc->Send(DataBuffer(data));
1517 }
1518 }
1519
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001520 rtc::Thread* network_thread() { return network_thread_.get(); }
1521
Taylor Brandstetter9b5306c2016-08-18 11:40:37 -07001522 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1523
deadbeefaf1b59c2015-10-15 12:08:41 -07001524 PeerConnectionTestClient* initializing_client() {
1525 return initiating_client_.get();
1526 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001527
1528 // Set the |initiating_client_| to the |client| passed in and return the
1529 // original |initiating_client_|.
1530 PeerConnectionTestClient* set_initializing_client(
1531 PeerConnectionTestClient* client) {
1532 PeerConnectionTestClient* old = initiating_client_.release();
1533 initiating_client_.reset(client);
1534 return old;
1535 }
1536
deadbeefaf1b59c2015-10-15 12:08:41 -07001537 PeerConnectionTestClient* receiving_client() {
1538 return receiving_client_.get();
1539 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001541 // Set the |receiving_client_| to the |client| passed in and return the
1542 // original |receiving_client_|.
1543 PeerConnectionTestClient* set_receiving_client(
1544 PeerConnectionTestClient* client) {
1545 PeerConnectionTestClient* old = receiving_client_.release();
1546 receiving_client_.reset(client);
1547 return old;
1548 }
1549
zhihuang184a3fd2016-06-14 11:47:14 -07001550 bool AllObserversReceived(
1551 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>& observers) {
1552 for (auto& observer : observers) {
1553 if (!observer->first_packet_received()) {
1554 return false;
1555 }
1556 }
1557 return true;
1558 }
1559
jbauchcb560652016-08-04 05:20:32 -07001560 void TestGcmNegotiation(bool local_gcm_enabled, bool remote_gcm_enabled,
1561 int expected_cipher_suite) {
1562 PeerConnectionFactory::Options init_options;
1563 init_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled;
1564 PeerConnectionFactory::Options recv_options;
1565 recv_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001566 ASSERT_TRUE(CreateTestClients(nullptr, &init_options, nullptr, nullptr,
1567 &recv_options, nullptr));
jbauchcb560652016-08-04 05:20:32 -07001568 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1569 init_observer =
1570 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1571 initializing_client()->pc()->RegisterUMAObserver(init_observer);
1572 LocalP2PTest();
1573
1574 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
1575 initializing_client()->GetSrtpCipherStats(),
1576 kMaxWaitMs);
1577 EXPECT_EQ(1,
1578 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1579 expected_cipher_suite));
1580 }
1581
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 private:
deadbeefeff5b852016-05-27 14:18:01 -07001583 // |ss_| is used by |network_thread_| so it must be destroyed later.
kwibergd1fe2812016-04-27 06:47:29 -07001584 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
1585 std::unique_ptr<rtc::VirtualSocketServer> ss_;
deadbeefeff5b852016-05-27 14:18:01 -07001586 // |network_thread_| and |worker_thread_| are used by both
1587 // |initiating_client_| and |receiving_client_| so they must be destroyed
1588 // later.
1589 std::unique_ptr<rtc::Thread> network_thread_;
1590 std::unique_ptr<rtc::Thread> worker_thread_;
kwibergd1fe2812016-04-27 06:47:29 -07001591 std::unique_ptr<PeerConnectionTestClient> initiating_client_;
1592 std::unique_ptr<PeerConnectionTestClient> receiving_client_;
htaaac2dea2016-03-10 13:35:55 -08001593 bool prefer_constraint_apis_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001596// Disable for TSan v2, see
1597// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1598#if !defined(THREAD_SANITIZER)
1599
zhihuang184a3fd2016-06-14 11:47:14 -07001600TEST_F(P2PTestConductor, TestRtpReceiverObserverCallbackFunction) {
1601 ASSERT_TRUE(CreateTestClients());
1602 LocalP2PTest();
1603 EXPECT_TRUE_WAIT(
1604 AllObserversReceived(initializing_client()->rtp_receiver_observers()),
1605 kMaxWaitForFramesMs);
1606 EXPECT_TRUE_WAIT(
1607 AllObserversReceived(receiving_client()->rtp_receiver_observers()),
1608 kMaxWaitForFramesMs);
1609}
1610
1611// The observers are expected to fire the signal even if they are set after the
1612// first packet is received.
1613TEST_F(P2PTestConductor, TestSetRtpReceiverObserverAfterFirstPacketIsReceived) {
1614 ASSERT_TRUE(CreateTestClients());
1615 LocalP2PTest();
1616 // Reset the RtpReceiverObservers.
1617 initializing_client()->SetRtpReceiverObservers();
1618 receiving_client()->SetRtpReceiverObservers();
1619 EXPECT_TRUE_WAIT(
1620 AllObserversReceived(initializing_client()->rtp_receiver_observers()),
1621 kMaxWaitForFramesMs);
1622 EXPECT_TRUE_WAIT(
1623 AllObserversReceived(receiving_client()->rtp_receiver_observers()),
1624 kMaxWaitForFramesMs);
1625}
1626
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001627// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001628// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1629// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001630TEST_F(P2PTestConductor, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631 ASSERT_TRUE(CreateTestClients());
1632 LocalP2PTest();
1633 VerifyDtmf();
1634}
1635
1636// This test sets up a Jsep call between two parties and test that we can get a
1637// video aspect ratio of 16:9.
deadbeef7c73bdb2015-12-10 15:10:44 -08001638TEST_F(P2PTestConductor, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001639 ASSERT_TRUE(CreateTestClients());
1640 FakeConstraints constraint;
1641 double requested_ratio = 640.0/360;
1642 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1643 SetVideoConstraints(constraint, constraint);
1644 LocalP2PTest();
1645
1646 ASSERT_LE(0, initializing_client()->rendered_height());
1647 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001648 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649 initializing_client()->rendered_height();
1650 EXPECT_LE(requested_ratio, initiating_video_ratio);
1651
1652 ASSERT_LE(0, receiving_client()->rendered_height());
1653 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001654 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 receiving_client()->rendered_height();
1656 EXPECT_LE(requested_ratio, receiving_video_ratio);
1657}
1658
1659// This test sets up a Jsep call between two parties and test that the
1660// received video has a resolution of 1280*720.
1661// TODO(mallinath): Enable when
1662// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
deadbeef7c73bdb2015-12-10 15:10:44 -08001663TEST_F(P2PTestConductor, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664 ASSERT_TRUE(CreateTestClients());
1665 FakeConstraints constraint;
1666 constraint.SetMandatoryMinWidth(1280);
1667 constraint.SetMandatoryMinHeight(720);
1668 SetVideoConstraints(constraint, constraint);
1669 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001670 VerifyRenderedAspectRatio(1280, 720);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671}
1672
1673// This test sets up a call between two endpoints that are configured to use
1674// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001675TEST_F(P2PTestConductor, LocalP2PTestDtls) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001676 SetupAndVerifyDtlsCall();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001677}
1678
hta6b4f8392016-03-10 00:24:31 -08001679// This test sets up an one-way call, with media only from initiator to
1680// responder.
1681TEST_F(P2PTestConductor, OneWayMediaCall) {
1682 ASSERT_TRUE(CreateTestClients());
1683 receiving_client()->set_auto_add_stream(false);
1684 LocalP2PTest();
1685}
1686
htaaac2dea2016-03-10 13:35:55 -08001687TEST_F(P2PTestConductor, OneWayMediaCallWithoutConstraints) {
1688 ASSERT_TRUE(CreateTestClientsThatPreferNoConstraints());
1689 receiving_client()->set_auto_add_stream(false);
1690 LocalP2PTest();
1691}
1692
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001693// This test sets up a audio call initially and then upgrades to audio/video,
1694// using DTLS.
deadbeef7c73bdb2015-12-10 15:10:44 -08001695TEST_F(P2PTestConductor, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001696 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001697 FakeConstraints setup_constraints;
1698 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1699 true);
1700 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1701 receiving_client()->SetReceiveAudioVideo(true, false);
1702 LocalP2PTest();
1703 receiving_client()->SetReceiveAudioVideo(true, true);
1704 receiving_client()->Negotiate();
1705}
1706
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001707// This test sets up a call transfer to a new caller with a different DTLS
1708// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001709TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCallee) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001710 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1711 SetupAndVerifyDtlsCall();
1712
1713 // Keeping the original peer around which will still send packets to the
1714 // receiving client. These SRTP packets will be dropped.
kwibergd1fe2812016-04-27 06:47:29 -07001715 std::unique_ptr<PeerConnectionTestClient> original_peer(
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001716 set_initializing_client(CreateDtlsClientWithAlternateKey()));
1717 original_peer->pc()->Close();
1718
1719 SetSignalingReceivers();
1720 receiving_client()->SetExpectIceRestart(true);
1721 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001722 VerifyRenderedAspectRatio(640, 480);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001723}
1724
guoweis46383312015-12-17 16:45:59 -08001725// This test sets up a non-bundle call and apply bundle during ICE restart. When
1726// bundle is in effect in the restart, the channel can successfully reset its
1727// DTLS-SRTP context.
henrik.lundina28a1b92016-12-02 02:59:32 -08001728#if defined(MEMORY_SANITIZER)
1729// Fails under MemorySanitizer:
1730// See https://bugs.chromium.org/p/webrtc/issues/detail?id=6811
1731#define MAYBE_LocalP2PTestDtlsBundleInIceRestart \
1732 DISABLED_LocalP2PTestDtlsBundleInIceRestart
1733#else
1734#define MAYBE_LocalP2PTestDtlsBundleInIceRestart \
1735 LocalP2PTestDtlsBundleInIceRestart
1736#endif
1737TEST_F(P2PTestConductor, MAYBE_LocalP2PTestDtlsBundleInIceRestart) {
guoweis46383312015-12-17 16:45:59 -08001738 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1739 FakeConstraints setup_constraints;
1740 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1741 true);
1742 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1743 receiving_client()->RemoveBundleFromReceivedSdp(true);
1744 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001745 VerifyRenderedAspectRatio(640, 480);
guoweis46383312015-12-17 16:45:59 -08001746
1747 initializing_client()->IceRestart();
1748 receiving_client()->SetExpectIceRestart(true);
1749 receiving_client()->RemoveBundleFromReceivedSdp(false);
1750 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001751 VerifyRenderedAspectRatio(640, 480);
guoweis46383312015-12-17 16:45:59 -08001752}
1753
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001754// This test sets up a call transfer to a new callee with a different DTLS
1755// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001756TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCaller) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001757 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1758 SetupAndVerifyDtlsCall();
1759
1760 // Keeping the original peer around which will still send packets to the
1761 // receiving client. These SRTP packets will be dropped.
kwibergd1fe2812016-04-27 06:47:29 -07001762 std::unique_ptr<PeerConnectionTestClient> original_peer(
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001763 set_receiving_client(CreateDtlsClientWithAlternateKey()));
1764 original_peer->pc()->Close();
1765
1766 SetSignalingReceivers();
1767 initializing_client()->IceRestart();
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -07001768 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001769 VerifyRenderedAspectRatio(640, 480);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001770}
1771
perkjcaafdba2016-03-20 07:34:29 -07001772TEST_F(P2PTestConductor, LocalP2PTestCVO) {
1773 ASSERT_TRUE(CreateTestClients());
1774 SetCaptureRotation(webrtc::kVideoRotation_90);
1775 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001776 VerifyRenderedAspectRatio(640, 480, webrtc::kVideoRotation_90);
perkjcaafdba2016-03-20 07:34:29 -07001777}
1778
1779TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportCVO) {
1780 ASSERT_TRUE(CreateTestClients());
1781 SetCaptureRotation(webrtc::kVideoRotation_90);
1782 receiving_client()->RemoveCvoFromReceivedSdp(true);
1783 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001784 VerifyRenderedAspectRatio(480, 640, webrtc::kVideoRotation_0);
perkjcaafdba2016-03-20 07:34:29 -07001785}
1786
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001787// This test sets up a call between two endpoints that are configured to use
1788// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1789// negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001790TEST_F(P2PTestConductor, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001791 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001792 FakeConstraints setup_constraints;
1793 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1794 true);
1795 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1796 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1797 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001798 VerifyRenderedAspectRatio(640, 480);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001799}
1800
zhihuangaf388472016-11-02 16:49:48 -07001801// This test verifies that the negotiation will succeed with data channel only
1802// in max-bundle mode.
1803TEST_F(P2PTestConductor, LocalP2PTestOfferDataChannelOnly) {
1804 webrtc::PeerConnectionInterface::RTCConfiguration rtc_config;
1805 rtc_config.bundle_policy =
1806 webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle;
1807 ASSERT_TRUE(CreateTestClients(rtc_config, rtc_config));
1808 initializing_client()->CreateDataChannel();
1809 initializing_client()->Negotiate();
1810}
1811
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001812// This test sets up a Jsep call between two parties, and the callee only
1813// accept to receive video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001814TEST_F(P2PTestConductor, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001815 ASSERT_TRUE(CreateTestClients());
1816 receiving_client()->SetReceiveAudioVideo(false, true);
1817 LocalP2PTest();
1818}
1819
1820// This test sets up a Jsep call between two parties, and the callee only
1821// accept to receive audio.
deadbeef7c73bdb2015-12-10 15:10:44 -08001822TEST_F(P2PTestConductor, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823 ASSERT_TRUE(CreateTestClients());
1824 receiving_client()->SetReceiveAudioVideo(true, false);
1825 LocalP2PTest();
1826}
1827
1828// This test sets up a Jsep call between two parties, and the callee reject both
1829// audio and video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001830TEST_F(P2PTestConductor, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831 ASSERT_TRUE(CreateTestClients());
1832 receiving_client()->SetReceiveAudioVideo(false, false);
1833 LocalP2PTest();
1834}
1835
1836// This test sets up an audio and video call between two parties. After the call
1837// runs for a while (10 frames), the caller sends an update offer with video
1838// being rejected. Once the re-negotiation is done, the video flow should stop
1839// and the audio flow should continue.
deadbeefc9be0072015-12-14 18:27:57 -08001840TEST_F(P2PTestConductor, UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001841 ASSERT_TRUE(CreateTestClients());
1842 LocalP2PTest();
1843 TestUpdateOfferWithRejectedContent();
1844}
1845
1846// This test sets up a Jsep call between two parties. The MSID is removed from
1847// the SDP strings from the caller.
deadbeefc9be0072015-12-14 18:27:57 -08001848TEST_F(P2PTestConductor, LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001849 ASSERT_TRUE(CreateTestClients());
1850 receiving_client()->RemoveMsidFromReceivedSdp(true);
1851 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1852 // audio and video is muxed when MSID is disabled. Remove
1853 // SetRemoveBundleFromSdp once
1854 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1855 receiving_client()->RemoveBundleFromReceivedSdp(true);
1856 LocalP2PTest();
1857}
1858
kthelgasone2397792016-11-08 08:19:48 -08001859TEST_F(P2PTestConductor, LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001860 ASSERT_TRUE(CreateTestClients());
1861 // Set optional video constraint to max 320pixels to decrease CPU usage.
1862 FakeConstraints constraint;
1863 constraint.SetOptionalMaxWidth(320);
1864 SetVideoConstraints(constraint, constraint);
1865 initializing_client()->AddMediaStream(true, true);
1866 initializing_client()->AddMediaStream(false, true);
1867 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1868 LocalP2PTest();
1869 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1870}
1871
1872// Test that we can receive the audio output level from a remote audio track.
deadbeef7c73bdb2015-12-10 15:10:44 -08001873TEST_F(P2PTestConductor, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001874 ASSERT_TRUE(CreateTestClients());
1875 LocalP2PTest();
1876
1877 StreamCollectionInterface* remote_streams =
1878 initializing_client()->remote_streams();
1879 ASSERT_GT(remote_streams->count(), 0u);
1880 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1881 MediaStreamTrackInterface* remote_audio_track =
1882 remote_streams->at(0)->GetAudioTracks()[0];
1883
1884 // Get the audio output level stats. Note that the level is not available
1885 // until a RTCP packet has been received.
1886 EXPECT_TRUE_WAIT(
1887 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1888 kMaxWaitForStatsMs);
1889}
1890
1891// Test that an audio input level is reported.
deadbeef7c73bdb2015-12-10 15:10:44 -08001892TEST_F(P2PTestConductor, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001893 ASSERT_TRUE(CreateTestClients());
1894 LocalP2PTest();
1895
1896 // Get the audio input level stats. The level should be available very
1897 // soon after the test starts.
1898 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1899 kMaxWaitForStatsMs);
1900}
1901
1902// Test that we can get incoming byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001903TEST_F(P2PTestConductor, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001904 ASSERT_TRUE(CreateTestClients());
1905 LocalP2PTest();
1906
1907 StreamCollectionInterface* remote_streams =
1908 initializing_client()->remote_streams();
1909 ASSERT_GT(remote_streams->count(), 0u);
1910 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1911 MediaStreamTrackInterface* remote_audio_track =
1912 remote_streams->at(0)->GetAudioTracks()[0];
1913 EXPECT_TRUE_WAIT(
1914 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1915 kMaxWaitForStatsMs);
1916
1917 MediaStreamTrackInterface* remote_video_track =
1918 remote_streams->at(0)->GetVideoTracks()[0];
1919 EXPECT_TRUE_WAIT(
1920 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1921 kMaxWaitForStatsMs);
1922}
1923
1924// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001925TEST_F(P2PTestConductor, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001926 ASSERT_TRUE(CreateTestClients());
1927 LocalP2PTest();
1928
1929 StreamCollectionInterface* local_streams =
1930 initializing_client()->local_streams();
1931 ASSERT_GT(local_streams->count(), 0u);
1932 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1933 MediaStreamTrackInterface* local_audio_track =
1934 local_streams->at(0)->GetAudioTracks()[0];
1935 EXPECT_TRUE_WAIT(
1936 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1937 kMaxWaitForStatsMs);
1938
1939 MediaStreamTrackInterface* local_video_track =
1940 local_streams->at(0)->GetVideoTracks()[0];
1941 EXPECT_TRUE_WAIT(
1942 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1943 kMaxWaitForStatsMs);
1944}
1945
Joachim Bauch04e5b492015-05-29 09:40:39 +02001946// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001947TEST_F(P2PTestConductor, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001948 PeerConnectionFactory::Options init_options;
1949 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1950 PeerConnectionFactory::Options recv_options;
1951 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001952 ASSERT_TRUE(CreateTestClients(nullptr, &init_options, nullptr, nullptr,
1953 &recv_options, nullptr));
jbauchac8869e2015-07-03 01:36:14 -07001954 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1955 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1956 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001957 LocalP2PTest();
1958
torbjorng43166b82016-03-11 00:06:47 -08001959 EXPECT_TRUE_WAIT(
1960 rtc::SSLStreamAdapter::IsAcceptableCipher(
1961 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1962 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001963 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001964 initializing_client()->GetSrtpCipherStats(),
1965 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001966 EXPECT_EQ(1,
1967 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1968 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001969}
1970
1971// Test that DTLS 1.2 is used if both ends support it.
torbjorng79a5a832016-01-15 07:16:51 -08001972TEST_F(P2PTestConductor, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001973 PeerConnectionFactory::Options init_options;
1974 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1975 PeerConnectionFactory::Options recv_options;
1976 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001977 ASSERT_TRUE(CreateTestClients(nullptr, &init_options, nullptr, nullptr,
1978 &recv_options, nullptr));
jbauchac8869e2015-07-03 01:36:14 -07001979 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1980 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1981 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001982 LocalP2PTest();
1983
torbjorng43166b82016-03-11 00:06:47 -08001984 EXPECT_TRUE_WAIT(
1985 rtc::SSLStreamAdapter::IsAcceptableCipher(
1986 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1987 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001988 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001989 initializing_client()->GetSrtpCipherStats(),
1990 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001991 EXPECT_EQ(1,
1992 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1993 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001994}
1995
1996// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1997// received supports 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001998TEST_F(P2PTestConductor, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001999 PeerConnectionFactory::Options init_options;
2000 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2001 PeerConnectionFactory::Options recv_options;
2002 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002003 ASSERT_TRUE(CreateTestClients(nullptr, &init_options, nullptr, nullptr,
2004 &recv_options, nullptr));
jbauchac8869e2015-07-03 01:36:14 -07002005 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
2006 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2007 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02002008 LocalP2PTest();
2009
torbjorng43166b82016-03-11 00:06:47 -08002010 EXPECT_TRUE_WAIT(
2011 rtc::SSLStreamAdapter::IsAcceptableCipher(
2012 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
2013 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002014 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002015 initializing_client()->GetSrtpCipherStats(),
2016 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002017 EXPECT_EQ(1,
2018 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2019 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02002020}
2021
2022// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
2023// received supports 1.2.
torbjorng43166b82016-03-11 00:06:47 -08002024TEST_F(P2PTestConductor, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02002025 PeerConnectionFactory::Options init_options;
2026 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2027 PeerConnectionFactory::Options recv_options;
2028 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002029 ASSERT_TRUE(CreateTestClients(nullptr, &init_options, nullptr, nullptr,
2030 &recv_options, nullptr));
jbauchac8869e2015-07-03 01:36:14 -07002031 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
2032 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2033 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02002034 LocalP2PTest();
2035
torbjorng43166b82016-03-11 00:06:47 -08002036 EXPECT_TRUE_WAIT(
2037 rtc::SSLStreamAdapter::IsAcceptableCipher(
2038 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
2039 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002040 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002041 initializing_client()->GetSrtpCipherStats(),
2042 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002043 EXPECT_EQ(1,
2044 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2045 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00002046}
2047
jbauchcb560652016-08-04 05:20:32 -07002048// Test that a non-GCM cipher is used if both sides only support non-GCM.
2049TEST_F(P2PTestConductor, GetGcmNone) {
2050 TestGcmNegotiation(false, false, kDefaultSrtpCryptoSuite);
2051}
2052
2053// Test that a GCM cipher is used if both ends support it.
2054TEST_F(P2PTestConductor, GetGcmBoth) {
2055 TestGcmNegotiation(true, true, kDefaultSrtpCryptoSuiteGcm);
2056}
2057
2058// Test that GCM isn't used if only the initiator supports it.
2059TEST_F(P2PTestConductor, GetGcmInit) {
2060 TestGcmNegotiation(true, false, kDefaultSrtpCryptoSuite);
2061}
2062
2063// Test that GCM isn't used if only the receiver supports it.
2064TEST_F(P2PTestConductor, GetGcmRecv) {
2065 TestGcmNegotiation(false, true, kDefaultSrtpCryptoSuite);
2066}
2067
deadbeefb5cb19b2015-11-23 16:39:12 -08002068// This test sets up a call between two parties with audio, video and an RTP
2069// data channel.
deadbeef8f89bff2016-12-01 12:54:20 -08002070TEST_F(P2PTestConductor, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002071 FakeConstraints setup_constraints;
2072 setup_constraints.SetAllowRtpDataChannels();
2073 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
2074 initializing_client()->CreateDataChannel();
2075 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07002076 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
2077 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002078 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2079 kMaxWaitMs);
2080 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
2081 kMaxWaitMs);
2082
2083 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00002084
2085 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002086 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
2087 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00002088
2089 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002090 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
2091 kMaxWaitMs);
2092
2093 receiving_client()->data_channel()->Close();
2094 // Send new offer and answer.
2095 receiving_client()->Negotiate();
2096 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
2097 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
2098}
2099
deadbeefb5cb19b2015-11-23 16:39:12 -08002100// This test sets up a call between two parties with audio, video and an SCTP
2101// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08002102TEST_F(P2PTestConductor, LocalP2PTestSctpDataChannel) {
deadbeefb5cb19b2015-11-23 16:39:12 -08002103 ASSERT_TRUE(CreateTestClients());
2104 initializing_client()->CreateDataChannel();
2105 LocalP2PTest();
2106 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
2107 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
2108 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2109 kMaxWaitMs);
2110 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
2111
2112 std::string data = "hello world";
2113
2114 initializing_client()->data_channel()->Send(DataBuffer(data));
2115 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
2116 kMaxWaitMs);
2117
2118 receiving_client()->data_channel()->Send(DataBuffer(data));
2119 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
2120 kMaxWaitMs);
2121
2122 receiving_client()->data_channel()->Close();
deadbeef15887932015-12-14 19:32:34 -08002123 EXPECT_TRUE_WAIT(!initializing_client()->data_observer()->IsOpen(),
2124 kMaxWaitMs);
2125 EXPECT_TRUE_WAIT(!receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
deadbeefb5cb19b2015-11-23 16:39:12 -08002126}
2127
Taylor Brandstetter9b5306c2016-08-18 11:40:37 -07002128TEST_F(P2PTestConductor, UnorderedSctpDataChannel) {
2129 ASSERT_TRUE(CreateTestClients());
2130 webrtc::DataChannelInit init;
2131 init.ordered = false;
2132 initializing_client()->CreateDataChannel(&init);
2133
2134 // Introduce random network delays.
2135 // Otherwise it's not a true "unordered" test.
2136 virtual_socket_server()->set_delay_mean(20);
2137 virtual_socket_server()->set_delay_stddev(5);
2138 virtual_socket_server()->UpdateDelayDistribution();
2139
2140 initializing_client()->Negotiate();
2141 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
2142 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
2143 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2144 kMaxWaitMs);
2145 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
2146
2147 static constexpr int kNumMessages = 100;
2148 // Deliberately chosen to be larger than the MTU so messages get fragmented.
2149 static constexpr size_t kMaxMessageSize = 4096;
2150 // Create and send random messages.
2151 std::vector<std::string> sent_messages;
2152 for (int i = 0; i < kNumMessages; ++i) {
2153 size_t length = (rand() % kMaxMessageSize) + 1;
2154 std::string message;
2155 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
2156 initializing_client()->data_channel()->Send(DataBuffer(message));
2157 receiving_client()->data_channel()->Send(DataBuffer(message));
2158 sent_messages.push_back(message);
2159 }
2160
2161 EXPECT_EQ_WAIT(
2162 kNumMessages,
2163 initializing_client()->data_observer()->received_message_count(),
2164 kMaxWaitMs);
2165 EXPECT_EQ_WAIT(kNumMessages,
2166 receiving_client()->data_observer()->received_message_count(),
2167 kMaxWaitMs);
2168
2169 // Sort and compare to make sure none of the messages were corrupted.
2170 std::vector<std::string> initializing_client_received_messages =
2171 initializing_client()->data_observer()->messages();
2172 std::vector<std::string> receiving_client_received_messages =
2173 receiving_client()->data_observer()->messages();
2174 std::sort(sent_messages.begin(), sent_messages.end());
2175 std::sort(initializing_client_received_messages.begin(),
2176 initializing_client_received_messages.end());
2177 std::sort(receiving_client_received_messages.begin(),
2178 receiving_client_received_messages.end());
2179 EXPECT_EQ(sent_messages, initializing_client_received_messages);
2180 EXPECT_EQ(sent_messages, receiving_client_received_messages);
2181
2182 receiving_client()->data_channel()->Close();
2183 EXPECT_TRUE_WAIT(!initializing_client()->data_observer()->IsOpen(),
2184 kMaxWaitMs);
2185 EXPECT_TRUE_WAIT(!receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
2186}
2187
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188// This test sets up a call between two parties and creates a data channel.
2189// The test tests that received data is buffered unless an observer has been
2190// registered.
2191// Rtp data channels can receive data before the underlying
2192// transport has detected that a channel is writable and thus data can be
2193// received before the data channel state changes to open. That is hard to test
2194// but the same buffering is used in that case.
deadbeef7c73bdb2015-12-10 15:10:44 -08002195TEST_F(P2PTestConductor, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002196 FakeConstraints setup_constraints;
2197 setup_constraints.SetAllowRtpDataChannels();
2198 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
2199 initializing_client()->CreateDataChannel();
2200 initializing_client()->Negotiate();
2201
deadbeefaf1b59c2015-10-15 12:08:41 -07002202 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
2203 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002204 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2205 kMaxWaitMs);
2206 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
2207 receiving_client()->data_channel()->state(), kMaxWaitMs);
2208
2209 // Unregister the existing observer.
2210 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00002211
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002212 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00002213 SendRtpData(initializing_client()->data_channel(), data);
2214
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002215 // Wait a while to allow the sent data to arrive before an observer is
2216 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002217 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002218
2219 MockDataChannelObserver new_observer(receiving_client()->data_channel());
2220 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
2221}
2222
2223// This test sets up a call between two parties with audio, video and but only
2224// the initiating client support data.
deadbeef8f89bff2016-12-01 12:54:20 -08002225TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00002226 FakeConstraints setup_constraints_1;
2227 setup_constraints_1.SetAllowRtpDataChannels();
2228 // Must disable DTLS to make negotiation succeed.
2229 setup_constraints_1.SetMandatory(
2230 MediaConstraintsInterface::kEnableDtlsSrtp, false);
2231 FakeConstraints setup_constraints_2;
2232 setup_constraints_2.SetMandatory(
2233 MediaConstraintsInterface::kEnableDtlsSrtp, false);
2234 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002235 initializing_client()->CreateDataChannel();
2236 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07002237 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002238 EXPECT_FALSE(receiving_client()->data_channel());
2239 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
2240}
2241
2242// This test sets up a call between two parties with audio, video. When audio
2243// and video is setup and flowing and data channel is negotiated.
deadbeef8f89bff2016-12-01 12:54:20 -08002244TEST_F(P2PTestConductor, AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002245 FakeConstraints setup_constraints;
2246 setup_constraints.SetAllowRtpDataChannels();
2247 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
2248 LocalP2PTest();
2249 initializing_client()->CreateDataChannel();
2250 // Send new offer and answer.
2251 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07002252 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
2253 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002254 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2255 kMaxWaitMs);
2256 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
2257 kMaxWaitMs);
2258}
2259
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00002260// This test sets up a Jsep call with SCTP DataChannel and verifies the
2261// negotiation is completed without error.
2262#ifdef HAVE_SCTP
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002263TEST_F(P2PTestConductor, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002264 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00002265 FakeConstraints constraints;
2266 constraints.SetMandatory(
2267 MediaConstraintsInterface::kEnableDtlsSrtp, true);
2268 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
2269 initializing_client()->CreateDataChannel();
2270 initializing_client()->Negotiate(false, false);
2271}
2272#endif
2273
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002274// This test sets up a call between two parties with audio, and video.
2275// During the call, the initializing side restart ice and the test verifies that
2276// new ice candidates are generated and audio and video still can flow.
deadbeef7c73bdb2015-12-10 15:10:44 -08002277TEST_F(P2PTestConductor, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002278 ASSERT_TRUE(CreateTestClients());
2279
2280 // Negotiate and wait for ice completion and make sure audio and video plays.
2281 LocalP2PTest();
2282
2283 // Create a SDP string of the first audio candidate for both clients.
2284 const webrtc::IceCandidateCollection* audio_candidates_initiator =
2285 initializing_client()->pc()->local_description()->candidates(0);
2286 const webrtc::IceCandidateCollection* audio_candidates_receiver =
2287 receiving_client()->pc()->local_description()->candidates(0);
2288 ASSERT_GT(audio_candidates_initiator->count(), 0u);
2289 ASSERT_GT(audio_candidates_receiver->count(), 0u);
2290 std::string initiator_candidate;
2291 EXPECT_TRUE(
2292 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
2293 std::string receiver_candidate;
2294 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
2295
2296 // Restart ice on the initializing client.
2297 receiving_client()->SetExpectIceRestart(true);
2298 initializing_client()->IceRestart();
2299
2300 // Negotiate and wait for ice completion again and make sure audio and video
2301 // plays.
2302 LocalP2PTest();
2303
2304 // Create a SDP string of the first audio candidate for both clients again.
2305 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
2306 initializing_client()->pc()->local_description()->candidates(0);
2307 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
2308 receiving_client()->pc()->local_description()->candidates(0);
2309 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
2310 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
2311 std::string initiator_candidate_restart;
2312 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
2313 &initiator_candidate_restart));
2314 std::string receiver_candidate_restart;
2315 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
2316 &receiver_candidate_restart));
2317
2318 // Verify that the first candidates in the local session descriptions has
2319 // changed.
2320 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
2321 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
2322}
2323
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07002324TEST_F(P2PTestConductor, IceRenominationDisabled) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002325 PeerConnectionInterface::RTCConfiguration config;
2326 config.enable_ice_renomination = false;
2327 ASSERT_TRUE(CreateTestClients(config, config));
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07002328 LocalP2PTest();
2329
2330 initializing_client()->VerifyLocalIceRenomination();
2331 receiving_client()->VerifyLocalIceRenomination();
2332 initializing_client()->VerifyRemoteIceRenomination();
2333 receiving_client()->VerifyRemoteIceRenomination();
2334}
2335
2336TEST_F(P2PTestConductor, IceRenominationEnabled) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002337 PeerConnectionInterface::RTCConfiguration config;
2338 config.enable_ice_renomination = true;
2339 ASSERT_TRUE(CreateTestClients(config, config));
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07002340 initializing_client()->SetExpectIceRenomination(true);
2341 initializing_client()->SetExpectRemoteIceRenomination(true);
2342 receiving_client()->SetExpectIceRenomination(true);
2343 receiving_client()->SetExpectRemoteIceRenomination(true);
2344 LocalP2PTest();
2345
2346 initializing_client()->VerifyLocalIceRenomination();
2347 receiving_client()->VerifyLocalIceRenomination();
2348 initializing_client()->VerifyRemoteIceRenomination();
2349 receiving_client()->VerifyRemoteIceRenomination();
2350}
2351
deadbeeffaac4972015-11-12 15:33:07 -08002352// This test sets up a call between two parties with audio, and video.
2353// It then renegotiates setting the video m-line to "port 0", then later
2354// renegotiates again, enabling video.
deadbeef7c73bdb2015-12-10 15:10:44 -08002355TEST_F(P2PTestConductor, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08002356 ASSERT_TRUE(CreateTestClients());
2357
2358 // Do initial negotiation. Will result in video and audio sendonly m-lines.
2359 receiving_client()->set_auto_add_stream(false);
2360 initializing_client()->AddMediaStream(true, true);
2361 initializing_client()->Negotiate();
2362
2363 // Negotiate again, disabling the video m-line (receiving client will
2364 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
2365 receiving_client()->SetReceiveVideo(false);
2366 initializing_client()->Negotiate();
2367
2368 // Enable video and do negotiation again, making sure video is received
2369 // end-to-end.
2370 receiving_client()->SetReceiveVideo(true);
2371 receiving_client()->AddMediaStream(true, true);
2372 LocalP2PTest();
2373}
2374
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002375// This test sets up a Jsep call between two parties with external
2376// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00002377// TODO(holmer): Disabled due to sometimes crashing on buildbots.
2378// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08002379TEST_F(P2PTestConductor, DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002380 ASSERT_TRUE(CreateTestClients());
2381 EnableVideoDecoderFactory();
2382 LocalP2PTest();
2383}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00002384
deadbeeffac06552015-11-25 11:26:01 -08002385// This tests that if we negotiate after calling CreateSender but before we
2386// have a track, then set a track later, frames from the newly-set track are
2387// received end-to-end.
deadbeef7c73bdb2015-12-10 15:10:44 -08002388TEST_F(P2PTestConductor, EarlyWarmupTest) {
deadbeeffac06552015-11-25 11:26:01 -08002389 ASSERT_TRUE(CreateTestClients());
deadbeefbd7d8f72015-12-18 16:58:44 -08002390 auto audio_sender =
2391 initializing_client()->pc()->CreateSender("audio", "stream_id");
2392 auto video_sender =
2393 initializing_client()->pc()->CreateSender("video", "stream_id");
deadbeeffac06552015-11-25 11:26:01 -08002394 initializing_client()->Negotiate();
2395 // Wait for ICE connection to complete, without any tracks.
2396 // Note that the receiving client WILL (in HandleIncomingOffer) create
2397 // tracks, so it's only the initiator here that's doing early warmup.
2398 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
2399 VerifySessionDescriptions();
2400 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2401 initializing_client()->ice_connection_state(),
2402 kMaxWaitForFramesMs);
2403 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2404 receiving_client()->ice_connection_state(),
2405 kMaxWaitForFramesMs);
2406 // Now set the tracks, and expect frames to immediately start flowing.
2407 EXPECT_TRUE(
2408 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
2409 EXPECT_TRUE(
2410 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
hta6b4f8392016-03-10 00:24:31 -08002411 EXPECT_TRUE_WAIT(FramesHaveArrived(kEndAudioFrameCount, kEndVideoFrameCount),
deadbeeffac06552015-11-25 11:26:01 -08002412 kMaxWaitForFramesMs);
2413}
2414
zhihuang9763d562016-08-05 11:14:50 -07002415#ifdef HAVE_QUIC
2416// This test sets up a call between two parties using QUIC instead of DTLS for
2417// audio and video, and a QUIC data channel.
2418TEST_F(P2PTestConductor, LocalP2PTestQuicDataChannel) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002419 PeerConnectionInterface::RTCConfiguration quic_config;
2420 quic_config.enable_quic = true;
2421 ASSERT_TRUE(CreateTestClients(quic_config, quic_config));
zhihuang9763d562016-08-05 11:14:50 -07002422 webrtc::DataChannelInit init;
2423 init.ordered = false;
2424 init.reliable = true;
2425 init.id = 1;
2426 initializing_client()->CreateDataChannel(&init);
2427 receiving_client()->CreateDataChannel(&init);
2428 LocalP2PTest();
2429 ASSERT_NE(nullptr, initializing_client()->data_channel());
2430 ASSERT_NE(nullptr, receiving_client()->data_channel());
2431 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2432 kMaxWaitMs);
2433 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
2434
2435 std::string data = "hello world";
2436
2437 initializing_client()->data_channel()->Send(DataBuffer(data));
2438 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
2439 kMaxWaitMs);
2440
2441 receiving_client()->data_channel()->Send(DataBuffer(data));
2442 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
2443 kMaxWaitMs);
2444}
2445
2446// Tests that negotiation of QUIC data channels is completed without error.
2447TEST_F(P2PTestConductor, NegotiateQuicDataChannel) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002448 PeerConnectionInterface::RTCConfiguration quic_config;
2449 quic_config.enable_quic = true;
2450 ASSERT_TRUE(CreateTestClients(quic_config, quic_config));
zhihuang9763d562016-08-05 11:14:50 -07002451 FakeConstraints constraints;
2452 constraints.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp, true);
2453 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
2454 webrtc::DataChannelInit init;
2455 init.ordered = false;
2456 init.reliable = true;
2457 init.id = 1;
2458 initializing_client()->CreateDataChannel(&init);
2459 initializing_client()->Negotiate(false, false);
2460}
2461
2462// This test sets up a JSEP call using QUIC. The callee only receives video.
2463TEST_F(P2PTestConductor, LocalP2PTestVideoOnlyWithQuic) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002464 PeerConnectionInterface::RTCConfiguration quic_config;
2465 quic_config.enable_quic = true;
2466 ASSERT_TRUE(CreateTestClients(quic_config, quic_config));
zhihuang9763d562016-08-05 11:14:50 -07002467 receiving_client()->SetReceiveAudioVideo(false, true);
2468 LocalP2PTest();
2469}
2470
2471// This test sets up a JSEP call using QUIC. The callee only receives audio.
2472TEST_F(P2PTestConductor, LocalP2PTestAudioOnlyWithQuic) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002473 PeerConnectionInterface::RTCConfiguration quic_config;
2474 quic_config.enable_quic = true;
2475 ASSERT_TRUE(CreateTestClients(quic_config, quic_config));
zhihuang9763d562016-08-05 11:14:50 -07002476 receiving_client()->SetReceiveAudioVideo(true, false);
2477 LocalP2PTest();
2478}
2479
2480// This test sets up a JSEP call using QUIC. The callee rejects both audio and
2481// video.
2482TEST_F(P2PTestConductor, LocalP2PTestNoVideoAudioWithQuic) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002483 PeerConnectionInterface::RTCConfiguration quic_config;
2484 quic_config.enable_quic = true;
2485 ASSERT_TRUE(CreateTestClients(quic_config, quic_config));
zhihuang9763d562016-08-05 11:14:50 -07002486 receiving_client()->SetReceiveAudioVideo(false, false);
2487 LocalP2PTest();
2488}
2489
2490#endif // HAVE_QUIC
2491
nissed98cf1f2016-04-22 07:27:36 -07002492TEST_F(P2PTestConductor, ForwardVideoOnlyStream) {
2493 ASSERT_TRUE(CreateTestClients());
2494 // One-way stream
2495 receiving_client()->set_auto_add_stream(false);
2496 // Video only, audio forwarding not expected to work.
2497 initializing_client()->AddMediaStream(false, true);
2498 initializing_client()->Negotiate();
2499
2500 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
2501 VerifySessionDescriptions();
2502
2503 ASSERT_TRUE(initializing_client()->can_receive_video());
2504 ASSERT_TRUE(receiving_client()->can_receive_video());
2505
2506 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2507 initializing_client()->ice_connection_state(),
2508 kMaxWaitForFramesMs);
2509 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2510 receiving_client()->ice_connection_state(),
2511 kMaxWaitForFramesMs);
2512
2513 ASSERT_TRUE(receiving_client()->remote_streams()->count() == 1);
2514
2515 // Echo the stream back.
2516 receiving_client()->pc()->AddStream(
2517 receiving_client()->remote_streams()->at(0));
2518 receiving_client()->Negotiate();
2519
2520 EXPECT_TRUE_WAIT(
2521 initializing_client()->VideoFramesReceivedCheck(kEndVideoFrameCount),
2522 kMaxWaitForFramesMs);
2523}
2524
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002525// Test that we achieve the expected end-to-end connection time, using a
2526// fake clock and simulated latency on the media and signaling paths.
2527// We use a TURN<->TURN connection because this is usually the quickest to
2528// set up initially, especially when we're confident the connection will work
2529// and can start sending media before we get a STUN response.
2530//
2531// With various optimizations enabled, here are the network delays we expect to
2532// be on the critical path:
2533// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
2534// signaling answer (with DTLS fingerprint).
2535// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
2536// using TURN<->TURN pair, and DTLS exchange is 4 packets,
2537// the first of which should have arrived before the answer.
2538TEST_F(P2PTestConductor, EndToEndConnectionTimeWithTurnTurnPair) {
2539 rtc::ScopedFakeClock fake_clock;
2540 // Some things use a time of "0" as a special value, so we need to start out
2541 // the fake clock at a nonzero time.
2542 // TODO(deadbeef): Fix this.
2543 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
2544
2545 static constexpr int media_hop_delay_ms = 50;
2546 static constexpr int signaling_trip_delay_ms = 500;
2547 // For explanation of these values, see comment above.
2548 static constexpr int required_media_hops = 9;
2549 static constexpr int required_signaling_trips = 2;
2550 // For internal delays (such as posting an event asychronously).
2551 static constexpr int allowed_internal_delay_ms = 20;
2552 static constexpr int total_connection_time_ms =
2553 media_hop_delay_ms * required_media_hops +
2554 signaling_trip_delay_ms * required_signaling_trips +
2555 allowed_internal_delay_ms;
2556
2557 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
2558 3478};
2559 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
2560 0};
2561 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
2562 3478};
2563 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
2564 0};
2565 cricket::TestTurnServer turn_server_1(network_thread(),
2566 turn_server_1_internal_address,
2567 turn_server_1_external_address);
2568 cricket::TestTurnServer turn_server_2(network_thread(),
2569 turn_server_2_internal_address,
2570 turn_server_2_external_address);
2571 // Bypass permission check on received packets so media can be sent before
2572 // the candidate is signaled.
2573 turn_server_1.set_enable_permission_checks(false);
2574 turn_server_2.set_enable_permission_checks(false);
2575
2576 PeerConnectionInterface::RTCConfiguration client_1_config;
2577 webrtc::PeerConnectionInterface::IceServer ice_server_1;
2578 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
2579 ice_server_1.username = "test";
2580 ice_server_1.password = "test";
2581 client_1_config.servers.push_back(ice_server_1);
2582 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2583 client_1_config.presume_writable_when_fully_relayed = true;
2584
2585 PeerConnectionInterface::RTCConfiguration client_2_config;
2586 webrtc::PeerConnectionInterface::IceServer ice_server_2;
2587 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
2588 ice_server_2.username = "test";
2589 ice_server_2.password = "test";
2590 client_2_config.servers.push_back(ice_server_2);
2591 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2592 client_2_config.presume_writable_when_fully_relayed = true;
2593
2594 ASSERT_TRUE(CreateTestClients(client_1_config, client_2_config));
2595 // Set up the simulated delays.
2596 SetSignalingDelayMs(signaling_trip_delay_ms);
2597 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
2598 virtual_socket_server()->UpdateDelayDistribution();
2599
2600 initializing_client()->SetOfferToReceiveAudioVideo(true, true);
2601 initializing_client()->Negotiate();
2602 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
2603 // are connected. This is an important distinction. Once we have separate ICE
2604 // and DTLS state, this check needs to use the DTLS state.
2605 EXPECT_TRUE_SIMULATED_WAIT(
2606 (receiving_client()->ice_connection_state() ==
2607 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
2608 receiving_client()->ice_connection_state() ==
2609 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
2610 (initializing_client()->ice_connection_state() ==
2611 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
2612 initializing_client()->ice_connection_state() ==
2613 webrtc::PeerConnectionInterface::kIceConnectionCompleted),
2614 total_connection_time_ms, fake_clock);
2615 // Need to free the clients here since they're using things we created on
2616 // the stack.
2617 delete set_initializing_client(nullptr);
2618 delete set_receiving_client(nullptr);
2619}
2620
deadbeef0a6c4ca2015-10-06 11:38:28 -07002621class IceServerParsingTest : public testing::Test {
2622 public:
2623 // Convenience for parsing a single URL.
2624 bool ParseUrl(const std::string& url) {
2625 return ParseUrl(url, std::string(), std::string());
2626 }
2627
2628 bool ParseUrl(const std::string& url,
2629 const std::string& username,
2630 const std::string& password) {
2631 PeerConnectionInterface::IceServers servers;
2632 PeerConnectionInterface::IceServer server;
2633 server.urls.push_back(url);
2634 server.username = username;
2635 server.password = password;
2636 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002637 return webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002638 }
2639
2640 protected:
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002641 cricket::ServerAddresses stun_servers_;
2642 std::vector<cricket::RelayServerConfig> turn_servers_;
deadbeef0a6c4ca2015-10-06 11:38:28 -07002643};
2644
2645// Make sure all STUN/TURN prefixes are parsed correctly.
2646TEST_F(IceServerParsingTest, ParseStunPrefixes) {
2647 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002648 EXPECT_EQ(1U, stun_servers_.size());
2649 EXPECT_EQ(0U, turn_servers_.size());
2650 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002651
2652 EXPECT_TRUE(ParseUrl("stuns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002653 EXPECT_EQ(1U, stun_servers_.size());
2654 EXPECT_EQ(0U, turn_servers_.size());
2655 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002656
2657 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002658 EXPECT_EQ(0U, stun_servers_.size());
2659 EXPECT_EQ(1U, turn_servers_.size());
2660 EXPECT_FALSE(turn_servers_[0].ports[0].secure);
2661 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002662
2663 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002664 EXPECT_EQ(0U, stun_servers_.size());
2665 EXPECT_EQ(1U, turn_servers_.size());
2666 EXPECT_TRUE(turn_servers_[0].ports[0].secure);
2667 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002668
2669 // invalid prefixes
2670 EXPECT_FALSE(ParseUrl("stunn:hostname"));
2671 EXPECT_FALSE(ParseUrl(":hostname"));
2672 EXPECT_FALSE(ParseUrl(":"));
2673 EXPECT_FALSE(ParseUrl(""));
2674}
2675
2676TEST_F(IceServerParsingTest, VerifyDefaults) {
2677 // TURNS defaults
2678 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002679 EXPECT_EQ(1U, turn_servers_.size());
2680 EXPECT_EQ(5349, turn_servers_[0].ports[0].address.port());
2681 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
2682 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002683
2684 // TURN defaults
2685 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002686 EXPECT_EQ(1U, turn_servers_.size());
2687 EXPECT_EQ(3478, turn_servers_[0].ports[0].address.port());
2688 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2689 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002690
2691 // STUN defaults
2692 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002693 EXPECT_EQ(1U, stun_servers_.size());
2694 EXPECT_EQ(3478, stun_servers_.begin()->port());
2695 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002696}
2697
2698// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
2699// can be parsed correctly.
2700TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
2701 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002702 EXPECT_EQ(1U, stun_servers_.size());
2703 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2704 EXPECT_EQ(1234, stun_servers_.begin()->port());
2705 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002706
2707 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002708 EXPECT_EQ(1U, stun_servers_.size());
2709 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2710 EXPECT_EQ(4321, stun_servers_.begin()->port());
2711 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002712
2713 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002714 EXPECT_EQ(1U, stun_servers_.size());
2715 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2716 EXPECT_EQ(9999, stun_servers_.begin()->port());
2717 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002718
2719 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002720 EXPECT_EQ(1U, stun_servers_.size());
2721 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2722 EXPECT_EQ(3478, stun_servers_.begin()->port());
2723 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002724
2725 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002726 EXPECT_EQ(1U, stun_servers_.size());
2727 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2728 EXPECT_EQ(3478, stun_servers_.begin()->port());
2729 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002730
2731 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002732 EXPECT_EQ(1U, stun_servers_.size());
2733 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2734 EXPECT_EQ(3478, stun_servers_.begin()->port());
2735 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002736
2737 // Try some invalid hostname:port strings.
2738 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
2739 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002740 EXPECT_FALSE(ParseUrl("stun:hostname:port:more"));
2741 EXPECT_FALSE(ParseUrl("stun:hostname:port more"));
deadbeef0a6c4ca2015-10-06 11:38:28 -07002742 EXPECT_FALSE(ParseUrl("stun:hostname:"));
2743 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
2744 EXPECT_FALSE(ParseUrl("stun::5555"));
2745 EXPECT_FALSE(ParseUrl("stun:"));
2746}
2747
2748// Test parsing the "?transport=xxx" part of the URL.
2749TEST_F(IceServerParsingTest, ParseTransport) {
2750 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002751 EXPECT_EQ(1U, turn_servers_.size());
2752 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
2753 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002754
2755 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002756 EXPECT_EQ(1U, turn_servers_.size());
2757 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2758 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002759
2760 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
2761}
2762
2763// Test parsing ICE username contained in URL.
2764TEST_F(IceServerParsingTest, ParseUsername) {
2765 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002766 EXPECT_EQ(1U, turn_servers_.size());
2767 EXPECT_EQ("user", turn_servers_[0].credentials.username);
2768 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002769
2770 EXPECT_FALSE(ParseUrl("turn:@hostname"));
2771 EXPECT_FALSE(ParseUrl("turn:username@"));
2772 EXPECT_FALSE(ParseUrl("turn:@"));
2773 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
2774}
2775
2776// Test that username and password from IceServer is copied into the resulting
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002777// RelayServerConfig.
deadbeef0a6c4ca2015-10-06 11:38:28 -07002778TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
2779 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002780 EXPECT_EQ(1U, turn_servers_.size());
2781 EXPECT_EQ("username", turn_servers_[0].credentials.username);
2782 EXPECT_EQ("password", turn_servers_[0].credentials.password);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002783}
2784
2785// Ensure that if a server has multiple URLs, each one is parsed.
2786TEST_F(IceServerParsingTest, ParseMultipleUrls) {
2787 PeerConnectionInterface::IceServers servers;
2788 PeerConnectionInterface::IceServer server;
2789 server.urls.push_back("stun:hostname");
2790 server.urls.push_back("turn:hostname");
2791 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002792 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2793 EXPECT_EQ(1U, stun_servers_.size());
2794 EXPECT_EQ(1U, turn_servers_.size());
deadbeef0a6c4ca2015-10-06 11:38:28 -07002795}
2796
Taylor Brandstetter893505d2016-01-07 15:12:48 -08002797// Ensure that TURN servers are given unique priorities,
2798// so that their resulting candidates have unique priorities.
2799TEST_F(IceServerParsingTest, TurnServerPrioritiesUnique) {
2800 PeerConnectionInterface::IceServers servers;
2801 PeerConnectionInterface::IceServer server;
2802 server.urls.push_back("turn:hostname");
2803 server.urls.push_back("turn:hostname2");
2804 servers.push_back(server);
2805 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2806 EXPECT_EQ(2U, turn_servers_.size());
2807 EXPECT_NE(turn_servers_[0].priority, turn_servers_[1].priority);
2808}
2809
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00002810#endif // if !defined(THREAD_SANITIZER)
hta6b4f8392016-03-10 00:24:31 -08002811
2812} // namespace