blob: 4876b40bc844ff925c00de81cfb302b45894512c [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"
hnslb0f04fd2016-12-19 04:10:30 -080043#include "webrtc/p2p/base/portinterface.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000044#include "webrtc/p2p/base/sessiondescription.h"
Taylor Brandstettere5835f52016-09-16 15:07:50 -070045#include "webrtc/p2p/base/testturnserver.h"
46#include "webrtc/p2p/client/basicportallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010047#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
49#define MAYBE_SKIP_TEST(feature) \
50 if (!(feature())) { \
51 LOG(LS_INFO) << "Feature disabled... skipping"; \
52 return; \
53 }
54
55using cricket::ContentInfo;
56using cricket::FakeWebRtcVideoDecoder;
57using cricket::FakeWebRtcVideoDecoderFactory;
58using cricket::FakeWebRtcVideoEncoder;
59using cricket::FakeWebRtcVideoEncoderFactory;
60using cricket::MediaContentDescription;
61using webrtc::DataBuffer;
62using webrtc::DataChannelInterface;
63using webrtc::DtmfSender;
64using webrtc::DtmfSenderInterface;
65using webrtc::DtmfSenderObserverInterface;
66using webrtc::FakeConstraints;
67using webrtc::MediaConstraintsInterface;
deadbeeffaac4972015-11-12 15:33:07 -080068using webrtc::MediaStreamInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069using webrtc::MediaStreamTrackInterface;
70using webrtc::MockCreateSessionDescriptionObserver;
71using webrtc::MockDataChannelObserver;
72using webrtc::MockSetSessionDescriptionObserver;
73using webrtc::MockStatsObserver;
deadbeeffaac4972015-11-12 15:33:07 -080074using webrtc::ObserverInterface;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000075using webrtc::PeerConnectionInterface;
Joachim Bauch04e5b492015-05-29 09:40:39 +020076using webrtc::PeerConnectionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077using webrtc::SessionDescriptionInterface;
78using webrtc::StreamCollectionInterface;
79
hta6b4f8392016-03-10 00:24:31 -080080namespace {
81
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000082static const int kMaxWaitMs = 10000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000083// Disable for TSan v2, see
84// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
85// This declaration is also #ifdef'd as it causes uninitialized-variable
86// warnings.
87#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088static const int kMaxWaitForStatsMs = 3000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000089#endif
deadbeeffac06552015-11-25 11:26:01 -080090static const int kMaxWaitForActivationMs = 5000;
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000091static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092static const int kEndAudioFrameCount = 3;
93static const int kEndVideoFrameCount = 3;
94
95static const char kStreamLabelBase[] = "stream_label";
96static const char kVideoTrackLabelBase[] = "video_track";
97static const char kAudioTrackLabelBase[] = "audio_track";
98static const char kDataChannelLabel[] = "data_channel";
99
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000100// Disable for TSan v2, see
101// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
102// This declaration is also #ifdef'd as it causes unused-variable errors.
103#if !defined(THREAD_SANITIZER)
104// SRTP cipher name negotiated by the tests. This must be updated if the
105// default changes.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800106static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
jbauchcb560652016-08-04 05:20:32 -0700107static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000108#endif
109
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700110// Used to simulate signaling ICE/SDP between two PeerConnections.
111enum Message { MSG_SDP_MESSAGE, MSG_ICE_MESSAGE };
112
113struct SdpMessage {
114 std::string type;
115 std::string msg;
116};
117
118struct IceMessage {
119 std::string sdp_mid;
120 int sdp_mline_index;
121 std::string msg;
122};
123
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124static void RemoveLinesFromSdp(const std::string& line_start,
125 std::string* sdp) {
126 const char kSdpLineEnd[] = "\r\n";
127 size_t ssrc_pos = 0;
128 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
129 std::string::npos) {
130 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
131 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
132 }
133}
134
hta6b4f8392016-03-10 00:24:31 -0800135bool StreamsHaveAudioTrack(StreamCollectionInterface* streams) {
136 for (size_t idx = 0; idx < streams->count(); idx++) {
137 auto stream = streams->at(idx);
138 if (stream->GetAudioTracks().size() > 0) {
139 return true;
140 }
141 }
142 return false;
143}
144
145bool StreamsHaveVideoTrack(StreamCollectionInterface* streams) {
146 for (size_t idx = 0; idx < streams->count(); idx++) {
147 auto stream = streams->at(idx);
148 if (stream->GetVideoTracks().size() > 0) {
149 return true;
150 }
151 }
152 return false;
153}
154
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155class SignalingMessageReceiver {
156 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 virtual void ReceiveSdpMessage(const std::string& type,
158 std::string& msg) = 0;
159 virtual void ReceiveIceMessage(const std::string& sdp_mid,
160 int sdp_mline_index,
161 const std::string& msg) = 0;
162
163 protected:
deadbeefaf1b59c2015-10-15 12:08:41 -0700164 SignalingMessageReceiver() {}
165 virtual ~SignalingMessageReceiver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166};
167
zhihuang184a3fd2016-06-14 11:47:14 -0700168class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
169 public:
170 MockRtpReceiverObserver(cricket::MediaType media_type)
171 : expected_media_type_(media_type) {}
172
173 void OnFirstPacketReceived(cricket::MediaType media_type) override {
174 ASSERT_EQ(expected_media_type_, media_type);
175 first_packet_received_ = true;
176 }
177
178 bool first_packet_received() { return first_packet_received_; }
179
180 virtual ~MockRtpReceiverObserver() {}
181
182 private:
183 bool first_packet_received_ = false;
184 cricket::MediaType expected_media_type_;
185};
186
deadbeefaf1b59c2015-10-15 12:08:41 -0700187class PeerConnectionTestClient : public webrtc::PeerConnectionObserver,
deadbeeffaac4972015-11-12 15:33:07 -0800188 public SignalingMessageReceiver,
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700189 public ObserverInterface,
190 public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191 public:
kjellander71a1b612016-11-07 01:18:08 -0800192 // We need these using declarations because there are two versions of each of
193 // the below methods and we only override one of them.
194 // TODO(deadbeef): Remove once there's only one version of the methods.
195 using PeerConnectionObserver::OnAddStream;
196 using PeerConnectionObserver::OnRemoveStream;
197 using PeerConnectionObserver::OnDataChannel;
198
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700199 // If |config| is not provided, uses a default constructed RTCConfiguration.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800200 static PeerConnectionTestClient* CreateClientWithDtlsIdentityStore(
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800201 const std::string& id,
202 const MediaConstraintsInterface* constraints,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800203 const PeerConnectionFactory::Options* options,
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700204 const PeerConnectionInterface::RTCConfiguration* config,
Henrik Boströmd79599d2016-06-01 13:58:50 +0200205 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
perkj8aba9972016-04-10 23:54:34 -0700206 bool prefer_constraint_apis,
danilchape9021a32016-05-17 01:52:02 -0700207 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700208 rtc::Thread* worker_thread) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800209 PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
zhihuang9763d562016-08-05 11:14:50 -0700210 if (!client->Init(constraints, options, config, std::move(cert_generator),
danilchape9021a32016-05-17 01:52:02 -0700211 prefer_constraint_apis, network_thread, worker_thread)) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800212 delete client;
213 return nullptr;
214 }
215 return client;
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800216 }
217
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800218 static PeerConnectionTestClient* CreateClient(
219 const std::string& id,
220 const MediaConstraintsInterface* constraints,
perkj8aba9972016-04-10 23:54:34 -0700221 const PeerConnectionFactory::Options* options,
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700222 const PeerConnectionInterface::RTCConfiguration* config,
danilchape9021a32016-05-17 01:52:02 -0700223 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700224 rtc::Thread* worker_thread) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200225 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
226 rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
227 new FakeRTCCertificateGenerator() : nullptr);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800228
zhihuang9763d562016-08-05 11:14:50 -0700229 return CreateClientWithDtlsIdentityStore(id, constraints, options, config,
230 std::move(cert_generator), true,
231 network_thread, worker_thread);
htaaac2dea2016-03-10 13:35:55 -0800232 }
233
234 static PeerConnectionTestClient* CreateClientPreferNoConstraints(
235 const std::string& id,
perkj8aba9972016-04-10 23:54:34 -0700236 const PeerConnectionFactory::Options* options,
danilchape9021a32016-05-17 01:52:02 -0700237 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700238 rtc::Thread* worker_thread) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200239 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
240 rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
241 new FakeRTCCertificateGenerator() : nullptr);
htaaac2dea2016-03-10 13:35:55 -0800242
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700243 return CreateClientWithDtlsIdentityStore(id, nullptr, options, nullptr,
zhihuang9763d562016-08-05 11:14:50 -0700244 std::move(cert_generator), false,
245 network_thread, worker_thread);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800246 }
247
deadbeefaf1b59c2015-10-15 12:08:41 -0700248 ~PeerConnectionTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249 }
250
deadbeefaf1b59c2015-10-15 12:08:41 -0700251 void Negotiate() { Negotiate(true, true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252
deadbeefaf1b59c2015-10-15 12:08:41 -0700253 void Negotiate(bool audio, bool video) {
kwibergd1fe2812016-04-27 06:47:29 -0700254 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700255 ASSERT_TRUE(DoCreateOffer(&offer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256
deadbeefaf1b59c2015-10-15 12:08:41 -0700257 if (offer->description()->GetContentByName("audio")) {
258 offer->description()->GetContentByName("audio")->rejected = !audio;
259 }
260 if (offer->description()->GetContentByName("video")) {
261 offer->description()->GetContentByName("video")->rejected = !video;
262 }
263
264 std::string sdp;
265 EXPECT_TRUE(offer->ToString(&sdp));
266 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700267 SendSdpMessage(webrtc::SessionDescriptionInterface::kOffer, sdp);
268 }
269
270 void SendSdpMessage(const std::string& type, std::string& msg) {
271 if (signaling_delay_ms_ == 0) {
272 if (signaling_message_receiver_) {
273 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
274 }
275 } else {
276 rtc::Thread::Current()->PostDelayed(
277 RTC_FROM_HERE, signaling_delay_ms_, this, MSG_SDP_MESSAGE,
278 new rtc::TypedMessageData<SdpMessage>({type, msg}));
279 }
280 }
281
282 void SendIceMessage(const std::string& sdp_mid,
283 int sdp_mline_index,
284 const std::string& msg) {
285 if (signaling_delay_ms_ == 0) {
286 if (signaling_message_receiver_) {
287 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
288 msg);
289 }
290 } else {
291 rtc::Thread::Current()->PostDelayed(RTC_FROM_HERE, signaling_delay_ms_,
292 this, MSG_ICE_MESSAGE,
293 new rtc::TypedMessageData<IceMessage>(
294 {sdp_mid, sdp_mline_index, msg}));
295 }
296 }
297
298 // MessageHandler callback.
299 void OnMessage(rtc::Message* msg) override {
300 switch (msg->message_id) {
301 case MSG_SDP_MESSAGE: {
302 auto sdp_message =
303 static_cast<rtc::TypedMessageData<SdpMessage>*>(msg->pdata);
304 if (signaling_message_receiver_) {
305 signaling_message_receiver_->ReceiveSdpMessage(
306 sdp_message->data().type, sdp_message->data().msg);
307 }
308 delete sdp_message;
309 break;
310 }
311 case MSG_ICE_MESSAGE: {
312 auto ice_message =
313 static_cast<rtc::TypedMessageData<IceMessage>*>(msg->pdata);
314 if (signaling_message_receiver_) {
315 signaling_message_receiver_->ReceiveIceMessage(
316 ice_message->data().sdp_mid, ice_message->data().sdp_mline_index,
317 ice_message->data().msg);
318 }
319 delete ice_message;
320 break;
321 }
322 default:
323 RTC_CHECK(false);
324 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700325 }
326
327 // SignalingMessageReceiver callback.
328 void ReceiveSdpMessage(const std::string& type, std::string& msg) override {
329 FilterIncomingSdpMessage(&msg);
330 if (type == webrtc::SessionDescriptionInterface::kOffer) {
331 HandleIncomingOffer(msg);
332 } else {
333 HandleIncomingAnswer(msg);
334 }
335 }
336
337 // SignalingMessageReceiver callback.
338 void ReceiveIceMessage(const std::string& sdp_mid,
339 int sdp_mline_index,
340 const std::string& msg) override {
341 LOG(INFO) << id_ << "ReceiveIceMessage";
kwibergd1fe2812016-04-27 06:47:29 -0700342 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
deadbeefaf1b59c2015-10-15 12:08:41 -0700343 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
344 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
345 }
346
347 // PeerConnectionObserver callbacks.
348 void OnSignalingChange(
349 webrtc::PeerConnectionInterface::SignalingState new_state) override {
350 EXPECT_EQ(pc()->signaling_state(), new_state);
351 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700352 void OnAddStream(
353 rtc::scoped_refptr<MediaStreamInterface> media_stream) override {
deadbeeffaac4972015-11-12 15:33:07 -0800354 media_stream->RegisterObserver(this);
deadbeefaf1b59c2015-10-15 12:08:41 -0700355 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
356 const std::string id = media_stream->GetVideoTracks()[i]->id();
357 ASSERT_TRUE(fake_video_renderers_.find(id) ==
358 fake_video_renderers_.end());
deadbeefc9be0072015-12-14 18:27:57 -0800359 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
360 media_stream->GetVideoTracks()[i]));
deadbeefaf1b59c2015-10-15 12:08:41 -0700361 }
362 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700363 void OnRemoveStream(
364 rtc::scoped_refptr<MediaStreamInterface> media_stream) override {}
deadbeefaf1b59c2015-10-15 12:08:41 -0700365 void OnRenegotiationNeeded() override {}
366 void OnIceConnectionChange(
367 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
368 EXPECT_EQ(pc()->ice_connection_state(), new_state);
369 }
370 void OnIceGatheringChange(
371 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
372 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
373 }
374 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
375 LOG(INFO) << id_ << "OnIceCandidate";
376
377 std::string ice_sdp;
378 EXPECT_TRUE(candidate->ToString(&ice_sdp));
379 if (signaling_message_receiver_ == nullptr) {
380 // Remote party may be deleted.
381 return;
382 }
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700383 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
deadbeefaf1b59c2015-10-15 12:08:41 -0700384 }
385
deadbeeffaac4972015-11-12 15:33:07 -0800386 // MediaStreamInterface callback
387 void OnChanged() override {
388 // Track added or removed from MediaStream, so update our renderers.
389 rtc::scoped_refptr<StreamCollectionInterface> remote_streams =
390 pc()->remote_streams();
391 // Remove renderers for tracks that were removed.
392 for (auto it = fake_video_renderers_.begin();
393 it != fake_video_renderers_.end();) {
394 if (remote_streams->FindVideoTrack(it->first) == nullptr) {
deadbeefc9be0072015-12-14 18:27:57 -0800395 auto to_remove = it++;
396 removed_fake_video_renderers_.push_back(std::move(to_remove->second));
397 fake_video_renderers_.erase(to_remove);
deadbeeffaac4972015-11-12 15:33:07 -0800398 } else {
399 ++it;
400 }
401 }
402 // Create renderers for new video tracks.
403 for (size_t stream_index = 0; stream_index < remote_streams->count();
404 ++stream_index) {
405 MediaStreamInterface* remote_stream = remote_streams->at(stream_index);
406 for (size_t track_index = 0;
407 track_index < remote_stream->GetVideoTracks().size();
408 ++track_index) {
409 const std::string id =
410 remote_stream->GetVideoTracks()[track_index]->id();
411 if (fake_video_renderers_.find(id) != fake_video_renderers_.end()) {
412 continue;
413 }
deadbeefc9be0072015-12-14 18:27:57 -0800414 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
415 remote_stream->GetVideoTracks()[track_index]));
deadbeeffaac4972015-11-12 15:33:07 -0800416 }
417 }
418 }
419
deadbeefaf1b59c2015-10-15 12:08:41 -0700420 void SetVideoConstraints(const webrtc::FakeConstraints& video_constraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000421 video_constraints_ = video_constraint;
422 }
423
424 void AddMediaStream(bool audio, bool video) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700425 std::string stream_label =
426 kStreamLabelBase +
427 rtc::ToString<int>(static_cast<int>(pc()->local_streams()->count()));
deadbeeffaac4972015-11-12 15:33:07 -0800428 rtc::scoped_refptr<MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000429 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430
431 if (audio && can_receive_audio()) {
deadbeeffac06552015-11-25 11:26:01 -0800432 stream->AddTrack(CreateLocalAudioTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433 }
434 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000435 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 }
437
deadbeefaf1b59c2015-10-15 12:08:41 -0700438 EXPECT_TRUE(pc()->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439 }
440
deadbeefaf1b59c2015-10-15 12:08:41 -0700441 size_t NumberOfLocalMediaStreams() { return pc()->local_streams()->count(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000442
443 bool SessionActive() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700444 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445 }
446
deadbeeffaac4972015-11-12 15:33:07 -0800447 // Automatically add a stream when receiving an offer, if we don't have one.
448 // Defaults to true.
449 void set_auto_add_stream(bool auto_add_stream) {
450 auto_add_stream_ = auto_add_stream;
451 }
452
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000453 void set_signaling_message_receiver(
deadbeefaf1b59c2015-10-15 12:08:41 -0700454 SignalingMessageReceiver* signaling_message_receiver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000455 signaling_message_receiver_ = signaling_message_receiver;
456 }
457
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700458 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
459
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000460 void EnableVideoDecoderFactory() {
461 video_decoder_factory_enabled_ = true;
462 fake_video_decoder_factory_->AddSupportedVideoCodecType(
463 webrtc::kVideoCodecVP8);
464 }
465
deadbeefaf1b59c2015-10-15 12:08:41 -0700466 void IceRestart() {
htaaac2dea2016-03-10 13:35:55 -0800467 offer_answer_constraints_.SetMandatoryIceRestart(true);
468 offer_answer_options_.ice_restart = true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700469 SetExpectIceRestart(true);
470 }
471
472 void SetExpectIceRestart(bool expect_restart) {
473 expect_ice_restart_ = expect_restart;
474 }
475
476 bool ExpectIceRestart() const { return expect_ice_restart_; }
477
Honghai Zhang4cedf2b2016-08-31 08:18:11 -0700478 void SetExpectIceRenomination(bool expect_renomination) {
479 expect_ice_renomination_ = expect_renomination;
480 }
481 void SetExpectRemoteIceRenomination(bool expect_renomination) {
482 expect_remote_ice_renomination_ = expect_renomination;
483 }
484 bool ExpectIceRenomination() { return expect_ice_renomination_; }
485 bool ExpectRemoteIceRenomination() { return expect_remote_ice_renomination_; }
486
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700487 // The below 3 methods assume streams will be offered.
488 // Thus they'll only set the "offer to receive" flag to true if it's
489 // currently false, not if it's just unset.
deadbeefaf1b59c2015-10-15 12:08:41 -0700490 void SetReceiveAudioVideo(bool audio, bool video) {
491 SetReceiveAudio(audio);
492 SetReceiveVideo(video);
493 ASSERT_EQ(audio, can_receive_audio());
494 ASSERT_EQ(video, can_receive_video());
495 }
496
497 void SetReceiveAudio(bool audio) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700498 if (audio && can_receive_audio()) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700499 return;
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700500 }
htaaac2dea2016-03-10 13:35:55 -0800501 offer_answer_constraints_.SetMandatoryReceiveAudio(audio);
502 offer_answer_options_.offer_to_receive_audio = audio ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700503 }
504
505 void SetReceiveVideo(bool video) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700506 if (video && can_receive_video()) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700507 return;
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700508 }
509 offer_answer_constraints_.SetMandatoryReceiveVideo(video);
510 offer_answer_options_.offer_to_receive_video = video ? 1 : 0;
511 }
512
513 void SetOfferToReceiveAudioVideo(bool audio, bool video) {
514 offer_answer_constraints_.SetMandatoryReceiveAudio(audio);
515 offer_answer_options_.offer_to_receive_audio = audio ? 1 : 0;
htaaac2dea2016-03-10 13:35:55 -0800516 offer_answer_constraints_.SetMandatoryReceiveVideo(video);
517 offer_answer_options_.offer_to_receive_video = video ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700518 }
519
520 void RemoveMsidFromReceivedSdp(bool remove) { remove_msid_ = remove; }
521
522 void RemoveSdesCryptoFromReceivedSdp(bool remove) { remove_sdes_ = remove; }
523
524 void RemoveBundleFromReceivedSdp(bool remove) { remove_bundle_ = remove; }
525
perkjcaafdba2016-03-20 07:34:29 -0700526 void RemoveCvoFromReceivedSdp(bool remove) { remove_cvo_ = remove; }
527
deadbeefaf1b59c2015-10-15 12:08:41 -0700528 bool can_receive_audio() {
529 bool value;
htaaac2dea2016-03-10 13:35:55 -0800530 if (prefer_constraint_apis_) {
531 if (webrtc::FindConstraint(
532 &offer_answer_constraints_,
533 MediaConstraintsInterface::kOfferToReceiveAudio, &value,
534 nullptr)) {
535 return value;
536 }
537 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700538 }
htaaac2dea2016-03-10 13:35:55 -0800539 return offer_answer_options_.offer_to_receive_audio > 0 ||
540 offer_answer_options_.offer_to_receive_audio ==
541 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700542 }
543
544 bool can_receive_video() {
545 bool value;
htaaac2dea2016-03-10 13:35:55 -0800546 if (prefer_constraint_apis_) {
547 if (webrtc::FindConstraint(
548 &offer_answer_constraints_,
549 MediaConstraintsInterface::kOfferToReceiveVideo, &value,
550 nullptr)) {
551 return value;
552 }
553 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700554 }
htaaac2dea2016-03-10 13:35:55 -0800555 return offer_answer_options_.offer_to_receive_video > 0 ||
556 offer_answer_options_.offer_to_receive_video ==
557 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700558 }
559
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700560 void OnDataChannel(
561 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
deadbeefaf1b59c2015-10-15 12:08:41 -0700562 LOG(INFO) << id_ << "OnDataChannel";
563 data_channel_ = data_channel;
564 data_observer_.reset(new MockDataChannelObserver(data_channel));
565 }
566
zhihuang9763d562016-08-05 11:14:50 -0700567 void CreateDataChannel() { CreateDataChannel(nullptr); }
568
569 void CreateDataChannel(const webrtc::DataChannelInit* init) {
570 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, init);
deadbeefaf1b59c2015-10-15 12:08:41 -0700571 ASSERT_TRUE(data_channel_.get() != nullptr);
572 data_observer_.reset(new MockDataChannelObserver(data_channel_));
573 }
574
deadbeeffac06552015-11-25 11:26:01 -0800575 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack(
576 const std::string& stream_label) {
577 FakeConstraints constraints;
578 // Disable highpass filter so that we can get all the test audio frames.
579 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
580 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
581 peer_connection_factory_->CreateAudioSource(&constraints);
582 // TODO(perkj): Test audio source when it is implemented. Currently audio
583 // always use the default input.
584 std::string label = stream_label + kAudioTrackLabelBase;
585 return peer_connection_factory_->CreateAudioTrack(label, source);
586 }
587
588 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack(
589 const std::string& stream_label) {
590 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
591 FakeConstraints source_constraints = video_constraints_;
592 source_constraints.SetMandatoryMaxFrameRate(10);
593
594 cricket::FakeVideoCapturer* fake_capturer =
595 new webrtc::FakePeriodicVideoCapturer();
perkjcaafdba2016-03-20 07:34:29 -0700596 fake_capturer->SetRotation(capture_rotation_);
deadbeeffac06552015-11-25 11:26:01 -0800597 video_capturers_.push_back(fake_capturer);
perkja3ede6c2016-03-08 01:27:48 +0100598 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
deadbeeffac06552015-11-25 11:26:01 -0800599 peer_connection_factory_->CreateVideoSource(fake_capturer,
600 &source_constraints);
601 std::string label = stream_label + kVideoTrackLabelBase;
perkjcaafdba2016-03-20 07:34:29 -0700602
603 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
604 peer_connection_factory_->CreateVideoTrack(label, source));
605 if (!local_video_renderer_) {
606 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
607 }
608 return track;
deadbeeffac06552015-11-25 11:26:01 -0800609 }
610
deadbeefaf1b59c2015-10-15 12:08:41 -0700611 DataChannelInterface* data_channel() { return data_channel_; }
612 const MockDataChannelObserver* data_observer() const {
613 return data_observer_.get();
614 }
615
hta6b4f8392016-03-10 00:24:31 -0800616 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
deadbeefaf1b59c2015-10-15 12:08:41 -0700617
618 void StopVideoCapturers() {
perkjcaafdba2016-03-20 07:34:29 -0700619 for (auto* capturer : video_capturers_) {
620 capturer->Stop();
deadbeefaf1b59c2015-10-15 12:08:41 -0700621 }
622 }
623
perkjcaafdba2016-03-20 07:34:29 -0700624 void SetCaptureRotation(webrtc::VideoRotation rotation) {
625 ASSERT_TRUE(video_capturers_.empty());
626 capture_rotation_ = rotation;
627 }
628
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 bool AudioFramesReceivedCheck(int number_of_frames) const {
630 return number_of_frames <= fake_audio_capture_module_->frames_received();
631 }
632
deadbeefc9be0072015-12-14 18:27:57 -0800633 int audio_frames_received() const {
634 return fake_audio_capture_module_->frames_received();
635 }
636
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 bool VideoFramesReceivedCheck(int number_of_frames) {
638 if (video_decoder_factory_enabled_) {
639 const std::vector<FakeWebRtcVideoDecoder*>& decoders
640 = fake_video_decoder_factory_->decoders();
641 if (decoders.empty()) {
642 return number_of_frames <= 0;
643 }
hta6b4f8392016-03-10 00:24:31 -0800644 // Note - this checks that EACH decoder has the requisite number
645 // of frames. The video_frames_received() function sums them.
deadbeefc9be0072015-12-14 18:27:57 -0800646 for (FakeWebRtcVideoDecoder* decoder : decoders) {
647 if (number_of_frames > decoder->GetNumFramesReceived()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648 return false;
649 }
650 }
651 return true;
652 } else {
653 if (fake_video_renderers_.empty()) {
654 return number_of_frames <= 0;
655 }
656
deadbeefc9be0072015-12-14 18:27:57 -0800657 for (const auto& pair : fake_video_renderers_) {
658 if (number_of_frames > pair.second->num_rendered_frames()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 return false;
660 }
661 }
662 return true;
663 }
664 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700665
deadbeefc9be0072015-12-14 18:27:57 -0800666 int video_frames_received() const {
667 int total = 0;
668 if (video_decoder_factory_enabled_) {
669 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
670 fake_video_decoder_factory_->decoders();
671 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
672 total += decoder->GetNumFramesReceived();
673 }
674 } else {
675 for (const auto& pair : fake_video_renderers_) {
676 total += pair.second->num_rendered_frames();
677 }
678 for (const auto& renderer : removed_fake_video_renderers_) {
679 total += renderer->num_rendered_frames();
680 }
681 }
682 return total;
683 }
684
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 // Verify the CreateDtmfSender interface
686 void VerifyDtmf() {
kwibergd1fe2812016-04-27 06:47:29 -0700687 std::unique_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000688 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689
690 // We can't create a DTMF sender with an invalid audio track or a non local
691 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700692 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000693 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700694 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
695 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696
697 // We should be able to create a DTMF sender from a local track.
698 webrtc::AudioTrackInterface* localtrack =
699 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
700 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700701 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 dtmf_sender->RegisterObserver(observer.get());
703
704 // Test the DtmfSender object just created.
705 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
706 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
707
708 // We don't need to verify that the DTMF tones are actually sent out because
709 // that is already covered by the tests of the lower level components.
710
711 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
712 std::vector<std::string> tones;
713 tones.push_back("1");
714 tones.push_back("a");
715 tones.push_back("");
716 observer->Verify(tones);
717
718 dtmf_sender->UnregisterObserver();
719 }
720
721 // Verifies that the SessionDescription have rejected the appropriate media
722 // content.
723 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700724 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
725 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726 const cricket::SessionDescription* remote_desc =
727 peer_connection_->remote_description()->description();
728 const cricket::SessionDescription* local_desc =
729 peer_connection_->local_description()->description();
730
731 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
732 if (remote_audio_content) {
733 const ContentInfo* audio_content =
734 GetFirstAudioContent(local_desc);
735 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
736 }
737
738 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
739 if (remote_video_content) {
740 const ContentInfo* video_content =
741 GetFirstVideoContent(local_desc);
742 EXPECT_EQ(can_receive_video(), !video_content->rejected);
743 }
744 }
745
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700747 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748 const cricket::SessionDescription* desc =
749 peer_connection_->local_description()->description();
750 const cricket::ContentInfos& contents = desc->contents();
751
752 for (size_t index = 0; index < contents.size(); ++index) {
753 if (contents[index].rejected)
754 continue;
755 const cricket::TransportDescription* transport_desc =
756 desc->GetTransportDescriptionByName(contents[index].name);
757
758 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000759 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 if (ufragpair_it == ice_ufrag_pwd_.end()) {
761 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000762 ice_ufrag_pwd_[static_cast<int>(index)] =
763 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 } else if (ExpectIceRestart()) {
765 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
766 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
767 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
768 } else {
769 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
770 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
771 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
772 }
773 }
774 }
775
Honghai Zhang4cedf2b2016-08-31 08:18:11 -0700776 void VerifyLocalIceRenomination() {
777 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
778 const cricket::SessionDescription* desc =
779 peer_connection_->local_description()->description();
780 const cricket::ContentInfos& contents = desc->contents();
781
782 for (auto content : contents) {
783 if (content.rejected)
784 continue;
785 const cricket::TransportDescription* transport_desc =
786 desc->GetTransportDescriptionByName(content.name);
787 const auto& options = transport_desc->transport_options;
788 auto iter = std::find(options.begin(), options.end(),
789 cricket::ICE_RENOMINATION_STR);
790 EXPECT_EQ(ExpectIceRenomination(), iter != options.end());
791 }
792 }
793
794 void VerifyRemoteIceRenomination() {
795 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
796 const cricket::SessionDescription* desc =
797 peer_connection_->remote_description()->description();
798 const cricket::ContentInfos& contents = desc->contents();
799
800 for (auto content : contents) {
801 if (content.rejected)
802 continue;
803 const cricket::TransportDescription* transport_desc =
804 desc->GetTransportDescriptionByName(content.name);
805 const auto& options = transport_desc->transport_options;
806 auto iter = std::find(options.begin(), options.end(),
807 cricket::ICE_RENOMINATION_STR);
808 EXPECT_EQ(ExpectRemoteIceRenomination(), iter != options.end());
809 }
810 }
811
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000812 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000813 rtc::scoped_refptr<MockStatsObserver>
814 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000815 EXPECT_TRUE(peer_connection_->GetStats(
816 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700818 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 return observer->AudioOutputLevel();
820 }
821
822 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000823 rtc::scoped_refptr<MockStatsObserver>
824 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000825 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700826 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700828 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829 return observer->AudioInputLevel();
830 }
831
832 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000833 rtc::scoped_refptr<MockStatsObserver>
834 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000835 EXPECT_TRUE(peer_connection_->GetStats(
836 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700838 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 return observer->BytesReceived();
840 }
841
842 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000843 rtc::scoped_refptr<MockStatsObserver>
844 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000845 EXPECT_TRUE(peer_connection_->GetStats(
846 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700848 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 return observer->BytesSent();
850 }
851
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000852 int GetAvailableReceivedBandwidthStats() {
853 rtc::scoped_refptr<MockStatsObserver>
854 observer(new rtc::RefCountedObject<MockStatsObserver>());
855 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700856 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000857 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700858 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000859 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000860 return bw;
861 }
862
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000863 std::string GetDtlsCipherStats() {
864 rtc::scoped_refptr<MockStatsObserver>
865 observer(new rtc::RefCountedObject<MockStatsObserver>());
866 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700867 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000868 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700869 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000870 return observer->DtlsCipher();
871 }
872
873 std::string GetSrtpCipherStats() {
874 rtc::scoped_refptr<MockStatsObserver>
875 observer(new rtc::RefCountedObject<MockStatsObserver>());
876 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700877 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000878 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700879 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000880 return observer->SrtpCipher();
881 }
882
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 int rendered_width() {
884 EXPECT_FALSE(fake_video_renderers_.empty());
885 return fake_video_renderers_.empty() ? 1 :
886 fake_video_renderers_.begin()->second->width();
887 }
888
889 int rendered_height() {
890 EXPECT_FALSE(fake_video_renderers_.empty());
891 return fake_video_renderers_.empty() ? 1 :
892 fake_video_renderers_.begin()->second->height();
893 }
894
perkjcaafdba2016-03-20 07:34:29 -0700895 webrtc::VideoRotation rendered_rotation() {
896 EXPECT_FALSE(fake_video_renderers_.empty());
897 return fake_video_renderers_.empty()
898 ? webrtc::kVideoRotation_0
899 : fake_video_renderers_.begin()->second->rotation();
900 }
901
902 int local_rendered_width() {
903 return local_video_renderer_ ? local_video_renderer_->width() : 1;
904 }
905
906 int local_rendered_height() {
907 return local_video_renderer_ ? local_video_renderer_->height() : 1;
908 }
909
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910 size_t number_of_remote_streams() {
911 if (!pc())
912 return 0;
913 return pc()->remote_streams()->count();
914 }
915
hta6b4f8392016-03-10 00:24:31 -0800916 StreamCollectionInterface* remote_streams() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 if (!pc()) {
918 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700919 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 }
921 return pc()->remote_streams();
922 }
923
924 StreamCollectionInterface* local_streams() {
925 if (!pc()) {
926 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700927 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 }
929 return pc()->local_streams();
930 }
931
hta6b4f8392016-03-10 00:24:31 -0800932 bool HasLocalAudioTrack() { return StreamsHaveAudioTrack(local_streams()); }
933
934 bool HasLocalVideoTrack() { return StreamsHaveVideoTrack(local_streams()); }
935
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
937 return pc()->signaling_state();
938 }
939
940 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
941 return pc()->ice_connection_state();
942 }
943
944 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
945 return pc()->ice_gathering_state();
946 }
947
zhihuang184a3fd2016-06-14 11:47:14 -0700948 std::vector<std::unique_ptr<MockRtpReceiverObserver>> const&
949 rtp_receiver_observers() {
950 return rtp_receiver_observers_;
951 }
952
953 void SetRtpReceiverObservers() {
954 rtp_receiver_observers_.clear();
955 for (auto receiver : pc()->GetReceivers()) {
956 std::unique_ptr<MockRtpReceiverObserver> observer(
957 new MockRtpReceiverObserver(receiver->media_type()));
958 receiver->SetObserver(observer.get());
959 rtp_receiver_observers_.push_back(std::move(observer));
960 }
961 }
962
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 private:
964 class DummyDtmfObserver : public DtmfSenderObserverInterface {
965 public:
966 DummyDtmfObserver() : completed_(false) {}
967
968 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700969 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970 tones_.push_back(tone);
971 if (tone.empty()) {
972 completed_ = true;
973 }
974 }
975
976 void Verify(const std::vector<std::string>& tones) const {
977 ASSERT_TRUE(tones_.size() == tones.size());
978 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
979 }
980
981 bool completed() const { return completed_; }
982
983 private:
984 bool completed_;
985 std::vector<std::string> tones_;
986 };
987
deadbeefaf1b59c2015-10-15 12:08:41 -0700988 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
989
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800990 bool Init(
991 const MediaConstraintsInterface* constraints,
992 const PeerConnectionFactory::Options* options,
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700993 const PeerConnectionInterface::RTCConfiguration* config,
Henrik Boströmd79599d2016-06-01 13:58:50 +0200994 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
perkj8aba9972016-04-10 23:54:34 -0700995 bool prefer_constraint_apis,
danilchape9021a32016-05-17 01:52:02 -0700996 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700997 rtc::Thread* worker_thread) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700998 EXPECT_TRUE(!peer_connection_);
999 EXPECT_TRUE(!peer_connection_factory_);
htaaac2dea2016-03-10 13:35:55 -08001000 if (!prefer_constraint_apis) {
1001 EXPECT_TRUE(!constraints);
1002 }
1003 prefer_constraint_apis_ = prefer_constraint_apis;
1004
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001005 fake_network_manager_.reset(new rtc::FakeNetworkManager());
1006 fake_network_manager_->AddInterface(rtc::SocketAddress("192.168.1.1", 0));
1007
kwibergd1fe2812016-04-27 06:47:29 -07001008 std::unique_ptr<cricket::PortAllocator> port_allocator(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001009 new cricket::BasicPortAllocator(fake_network_manager_.get()));
deadbeefaf1b59c2015-10-15 12:08:41 -07001010 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
1011
1012 if (fake_audio_capture_module_ == nullptr) {
1013 return false;
1014 }
1015 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
1016 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
danilchape9021a32016-05-17 01:52:02 -07001017 rtc::Thread* const signaling_thread = rtc::Thread::Current();
deadbeefaf1b59c2015-10-15 12:08:41 -07001018 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -07001019 network_thread, worker_thread, signaling_thread,
1020 fake_audio_capture_module_, fake_video_encoder_factory_,
1021 fake_video_decoder_factory_);
deadbeefaf1b59c2015-10-15 12:08:41 -07001022 if (!peer_connection_factory_) {
1023 return false;
1024 }
1025 if (options) {
1026 peer_connection_factory_->SetOptions(*options);
1027 }
zhihuang9763d562016-08-05 11:14:50 -07001028 peer_connection_ =
1029 CreatePeerConnection(std::move(port_allocator), constraints, config,
1030 std::move(cert_generator));
deadbeefaf1b59c2015-10-15 12:08:41 -07001031 return peer_connection_.get() != nullptr;
1032 }
1033
deadbeefaf1b59c2015-10-15 12:08:41 -07001034 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
kwibergd1fe2812016-04-27 06:47:29 -07001035 std::unique_ptr<cricket::PortAllocator> port_allocator,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001036 const MediaConstraintsInterface* constraints,
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001037 const PeerConnectionInterface::RTCConfiguration* config,
Henrik Boströmd79599d2016-06-01 13:58:50 +02001038 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001039 // CreatePeerConnection with RTCConfiguration.
1040 PeerConnectionInterface::RTCConfiguration default_config;
1041
1042 if (!config) {
1043 config = &default_config;
1044 }
1045
Henrik Boströmd79599d2016-06-01 13:58:50 +02001046 return peer_connection_factory_->CreatePeerConnection(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001047 *config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +02001048 std::move(cert_generator), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049 }
1050
1051 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001052 LOG(INFO) << id_ << "HandleIncomingOffer ";
deadbeeffaac4972015-11-12 15:33:07 -08001053 if (NumberOfLocalMediaStreams() == 0 && auto_add_stream_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001054 // If we are not sending any streams ourselves it is time to add some.
1055 AddMediaStream(true, true);
1056 }
kwibergd1fe2812016-04-27 06:47:29 -07001057 std::unique_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -07001058 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001059 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
zhihuang184a3fd2016-06-14 11:47:14 -07001060 // Set the RtpReceiverObserver after receivers are created.
1061 SetRtpReceiverObservers();
kwibergd1fe2812016-04-27 06:47:29 -07001062 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001063 EXPECT_TRUE(DoCreateAnswer(&answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001064 std::string sdp;
1065 EXPECT_TRUE(answer->ToString(&sdp));
1066 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001067 SendSdpMessage(webrtc::SessionDescriptionInterface::kAnswer, sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001068 }
1069
1070 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001071 LOG(INFO) << id_ << "HandleIncomingAnswer";
kwibergd1fe2812016-04-27 06:47:29 -07001072 std::unique_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -07001073 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001074 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
zhihuang184a3fd2016-06-14 11:47:14 -07001075 // Set the RtpReceiverObserver after receivers are created.
1076 SetRtpReceiverObservers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001077 }
1078
kwibergd1fe2812016-04-27 06:47:29 -07001079 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001080 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001081 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
1082 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001083 MockCreateSessionDescriptionObserver>());
htaaac2dea2016-03-10 13:35:55 -08001084 if (prefer_constraint_apis_) {
1085 if (offer) {
1086 pc()->CreateOffer(observer, &offer_answer_constraints_);
1087 } else {
1088 pc()->CreateAnswer(observer, &offer_answer_constraints_);
1089 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001090 } else {
htaaac2dea2016-03-10 13:35:55 -08001091 if (offer) {
1092 pc()->CreateOffer(observer, offer_answer_options_);
1093 } else {
1094 pc()->CreateAnswer(observer, offer_answer_options_);
1095 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001096 }
1097 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
kwiberg2bbff992016-03-16 11:03:04 -07001098 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099 if (observer->result() && ExpectIceRestart()) {
1100 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
1101 }
1102 return observer->result();
1103 }
1104
kwibergd1fe2812016-04-27 06:47:29 -07001105 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001106 return DoCreateOfferAnswer(desc, true);
1107 }
1108
kwibergd1fe2812016-04-27 06:47:29 -07001109 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001110 return DoCreateOfferAnswer(desc, false);
1111 }
1112
1113 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001114 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
1115 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001116 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -07001117 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118 pc()->SetLocalDescription(observer, desc);
1119 // Ignore the observer result. If we wait for the result with
1120 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
1121 // before the offer which is an error.
1122 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001123 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001124 // ProcessMessages waits at least 1ms but processes all messages before
1125 // returning. Since this test is synchronous and send messages to the remote
1126 // peer whenever a callback is invoked, this can lead to messages being
1127 // sent to the remote peer in the wrong order.
1128 // TODO(perkj): Find a way to check the result without risking that the
1129 // order of sent messages are changed. Ex- by posting all messages that are
1130 // sent to the remote peer.
1131 return true;
1132 }
1133
1134 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001135 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
1136 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001137 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -07001138 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001139 pc()->SetRemoteDescription(observer, desc);
1140 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
1141 return observer->result();
1142 }
1143
1144 // This modifies all received SDP messages before they are processed.
1145 void FilterIncomingSdpMessage(std::string* sdp) {
1146 if (remove_msid_) {
1147 const char kSdpSsrcAttribute[] = "a=ssrc:";
1148 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
1149 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
1150 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
1151 }
1152 if (remove_bundle_) {
1153 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
1154 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
1155 }
1156 if (remove_sdes_) {
1157 const char kSdpSdesCryptoAttribute[] = "a=crypto";
1158 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
1159 }
perkjcaafdba2016-03-20 07:34:29 -07001160 if (remove_cvo_) {
1161 const char kSdpCvoExtenstion[] = "urn:3gpp:video-orientation";
1162 RemoveLinesFromSdp(kSdpCvoExtenstion, sdp);
1163 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001164 }
1165
deadbeefaf1b59c2015-10-15 12:08:41 -07001166 std::string id_;
1167
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001168 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
1169
deadbeefaf1b59c2015-10-15 12:08:41 -07001170 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
1171 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
1172 peer_connection_factory_;
1173
htaaac2dea2016-03-10 13:35:55 -08001174 bool prefer_constraint_apis_ = true;
deadbeeffaac4972015-11-12 15:33:07 -08001175 bool auto_add_stream_ = true;
1176
deadbeefaf1b59c2015-10-15 12:08:41 -07001177 typedef std::pair<std::string, std::string> IceUfragPwdPair;
1178 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
1179 bool expect_ice_restart_ = false;
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07001180 bool expect_ice_renomination_ = false;
1181 bool expect_remote_ice_renomination_ = false;
deadbeefaf1b59c2015-10-15 12:08:41 -07001182
deadbeefc9be0072015-12-14 18:27:57 -08001183 // Needed to keep track of number of frames sent.
deadbeefaf1b59c2015-10-15 12:08:41 -07001184 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
1185 // Needed to keep track of number of frames received.
kwibergd1fe2812016-04-27 06:47:29 -07001186 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
deadbeefc9be0072015-12-14 18:27:57 -08001187 fake_video_renderers_;
1188 // Needed to ensure frames aren't received for removed tracks.
kwibergd1fe2812016-04-27 06:47:29 -07001189 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
deadbeefc9be0072015-12-14 18:27:57 -08001190 removed_fake_video_renderers_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001191 // Needed to keep track of number of frames received when external decoder
1192 // used.
1193 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
1194 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
1195 bool video_decoder_factory_enabled_ = false;
1196 webrtc::FakeConstraints video_constraints_;
1197
1198 // For remote peer communication.
1199 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001200 int signaling_delay_ms_ = 0;
deadbeefaf1b59c2015-10-15 12:08:41 -07001201
1202 // Store references to the video capturers we've created, so that we can stop
1203 // them, if required.
perkjcaafdba2016-03-20 07:34:29 -07001204 std::vector<cricket::FakeVideoCapturer*> video_capturers_;
1205 webrtc::VideoRotation capture_rotation_ = webrtc::kVideoRotation_0;
1206 // |local_video_renderer_| attached to the first created local video track.
kwibergd1fe2812016-04-27 06:47:29 -07001207 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001208
htaaac2dea2016-03-10 13:35:55 -08001209 webrtc::FakeConstraints offer_answer_constraints_;
1210 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001211 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
1212 bool remove_bundle_ =
1213 false; // True if bundle should be removed in received SDP.
1214 bool remove_sdes_ =
1215 false; // True if a=crypto should be removed in received SDP.
perkjcaafdba2016-03-20 07:34:29 -07001216 // |remove_cvo_| is true if extension urn:3gpp:video-orientation should be
1217 // removed in the received SDP.
1218 bool remove_cvo_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001219
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001220 rtc::scoped_refptr<DataChannelInterface> data_channel_;
kwibergd1fe2812016-04-27 06:47:29 -07001221 std::unique_ptr<MockDataChannelObserver> data_observer_;
zhihuang184a3fd2016-06-14 11:47:14 -07001222
1223 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001224};
1225
deadbeef7c73bdb2015-12-10 15:10:44 -08001226class P2PTestConductor : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001227 public:
deadbeef7c73bdb2015-12-10 15:10:44 -08001228 P2PTestConductor()
deadbeefeff5b852016-05-27 14:18:01 -07001229 : pss_(new rtc::PhysicalSocketServer),
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001230 ss_(new rtc::VirtualSocketServer(pss_.get())),
deadbeefeff5b852016-05-27 14:18:01 -07001231 network_thread_(new rtc::Thread(ss_.get())),
1232 worker_thread_(rtc::Thread::Create()) {
danilchape9021a32016-05-17 01:52:02 -07001233 RTC_CHECK(network_thread_->Start());
1234 RTC_CHECK(worker_thread_->Start());
perkj8aba9972016-04-10 23:54:34 -07001235 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001236
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237 bool SessionActive() {
1238 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001239 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001241
hta6b4f8392016-03-10 00:24:31 -08001242 // Return true if the number of frames provided have been received
1243 // on the video and audio tracks provided.
1244 bool FramesHaveArrived(int audio_frames_to_receive,
1245 int video_frames_to_receive) {
1246 bool all_good = true;
1247 if (initiating_client_->HasLocalAudioTrack() &&
1248 receiving_client_->can_receive_audio()) {
1249 all_good &=
1250 receiving_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1251 }
1252 if (initiating_client_->HasLocalVideoTrack() &&
1253 receiving_client_->can_receive_video()) {
1254 all_good &=
1255 receiving_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1256 }
1257 if (receiving_client_->HasLocalAudioTrack() &&
1258 initiating_client_->can_receive_audio()) {
1259 all_good &=
1260 initiating_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1261 }
1262 if (receiving_client_->HasLocalVideoTrack() &&
1263 initiating_client_->can_receive_video()) {
1264 all_good &=
1265 initiating_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1266 }
1267 return all_good;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001268 }
hta6b4f8392016-03-10 00:24:31 -08001269
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001270 void VerifyDtmf() {
1271 initiating_client_->VerifyDtmf();
1272 receiving_client_->VerifyDtmf();
1273 }
1274
1275 void TestUpdateOfferWithRejectedContent() {
deadbeefc9be0072015-12-14 18:27:57 -08001276 // Renegotiate, rejecting the video m-line.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001277 initiating_client_->Negotiate(true, false);
deadbeefc9be0072015-12-14 18:27:57 -08001278 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1279
1280 int pc1_audio_received = initiating_client_->audio_frames_received();
1281 int pc1_video_received = initiating_client_->video_frames_received();
1282 int pc2_audio_received = receiving_client_->audio_frames_received();
1283 int pc2_video_received = receiving_client_->video_frames_received();
1284
1285 // Wait for some additional audio frames to be received.
1286 EXPECT_TRUE_WAIT(initiating_client_->AudioFramesReceivedCheck(
1287 pc1_audio_received + kEndAudioFrameCount) &&
1288 receiving_client_->AudioFramesReceivedCheck(
1289 pc2_audio_received + kEndAudioFrameCount),
1290 kMaxWaitForFramesMs);
1291
1292 // During this time, we shouldn't have received any additional video frames
1293 // for the rejected video tracks.
1294 EXPECT_EQ(pc1_video_received, initiating_client_->video_frames_received());
1295 EXPECT_EQ(pc2_video_received, receiving_client_->video_frames_received());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001296 }
1297
deadbeefc23efae2016-12-02 15:45:31 -08001298 void VerifyRenderedAspectRatio(int width, int height) {
1299 VerifyRenderedAspectRatio(width, height, webrtc::kVideoRotation_0);
perkjcaafdba2016-03-20 07:34:29 -07001300 }
1301
deadbeefc23efae2016-12-02 15:45:31 -08001302 void VerifyRenderedAspectRatio(int width,
1303 int height,
1304 webrtc::VideoRotation rotation) {
deadbeefb4659802016-12-01 16:23:28 -08001305 double expected_aspect_ratio = static_cast<double>(width) / height;
1306 double receiving_client_rendered_aspect_ratio =
1307 static_cast<double>(receiving_client()->rendered_width()) /
1308 receiving_client()->rendered_height();
1309 double initializing_client_rendered_aspect_ratio =
1310 static_cast<double>(initializing_client()->rendered_width()) /
1311 initializing_client()->rendered_height();
deadbeefc23efae2016-12-02 15:45:31 -08001312 double initializing_client_local_rendered_aspect_ratio =
1313 static_cast<double>(initializing_client()->local_rendered_width()) /
1314 initializing_client()->local_rendered_height();
1315 // Verify end-to-end rendered aspect ratio.
deadbeefb4659802016-12-01 16:23:28 -08001316 EXPECT_EQ(expected_aspect_ratio, receiving_client_rendered_aspect_ratio);
1317 EXPECT_EQ(expected_aspect_ratio, initializing_client_rendered_aspect_ratio);
deadbeefc23efae2016-12-02 15:45:31 -08001318 // Verify aspect ratio of the local preview.
1319 EXPECT_EQ(expected_aspect_ratio,
1320 initializing_client_local_rendered_aspect_ratio);
deadbeefb4659802016-12-01 16:23:28 -08001321
deadbeefc23efae2016-12-02 15:45:31 -08001322 // Verify rotation.
perkjcaafdba2016-03-20 07:34:29 -07001323 EXPECT_EQ(rotation, receiving_client()->rendered_rotation());
perkjcaafdba2016-03-20 07:34:29 -07001324 EXPECT_EQ(rotation, initializing_client()->rendered_rotation());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001325 }
1326
1327 void VerifySessionDescriptions() {
1328 initiating_client_->VerifyRejectedMediaInSessionDescription();
1329 receiving_client_->VerifyRejectedMediaInSessionDescription();
1330 initiating_client_->VerifyLocalIceUfragAndPassword();
1331 receiving_client_->VerifyLocalIceUfragAndPassword();
1332 }
1333
deadbeef7c73bdb2015-12-10 15:10:44 -08001334 ~P2PTestConductor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001335 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001336 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001337 }
1338 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001339 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001341 }
1342
deadbeefaf1b59c2015-10-15 12:08:41 -07001343 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001344
1345 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1346 MediaConstraintsInterface* recv_constraints) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001347 return CreateTestClients(init_constraints, nullptr, nullptr,
1348 recv_constraints, nullptr, nullptr);
1349 }
1350
1351 bool CreateTestClients(
1352 const PeerConnectionInterface::RTCConfiguration& init_config,
1353 const PeerConnectionInterface::RTCConfiguration& recv_config) {
1354 return CreateTestClients(nullptr, nullptr, &init_config, nullptr, nullptr,
1355 &recv_config);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001356 }
1357
htaaac2dea2016-03-10 13:35:55 -08001358 bool CreateTestClientsThatPreferNoConstraints() {
1359 initiating_client_.reset(
perkj8aba9972016-04-10 23:54:34 -07001360 PeerConnectionTestClient::CreateClientPreferNoConstraints(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001361 "Caller: ", nullptr, network_thread_.get(), worker_thread_.get()));
htaaac2dea2016-03-10 13:35:55 -08001362 receiving_client_.reset(
perkj8aba9972016-04-10 23:54:34 -07001363 PeerConnectionTestClient::CreateClientPreferNoConstraints(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001364 "Callee: ", nullptr, network_thread_.get(), worker_thread_.get()));
htaaac2dea2016-03-10 13:35:55 -08001365 if (!initiating_client_ || !receiving_client_) {
1366 return false;
1367 }
1368 // Remember the choice for possible later resets of the clients.
1369 prefer_constraint_apis_ = false;
1370 SetSignalingReceivers();
1371 return true;
1372 }
1373
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001374 bool CreateTestClients(
1375 MediaConstraintsInterface* init_constraints,
1376 PeerConnectionFactory::Options* init_options,
1377 const PeerConnectionInterface::RTCConfiguration* init_config,
1378 MediaConstraintsInterface* recv_constraints,
1379 PeerConnectionFactory::Options* recv_options,
1380 const PeerConnectionInterface::RTCConfiguration* recv_config) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001381 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001382 "Caller: ", init_constraints, init_options, init_config,
zhihuang9763d562016-08-05 11:14:50 -07001383 network_thread_.get(), worker_thread_.get()));
deadbeefaf1b59c2015-10-15 12:08:41 -07001384 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001385 "Callee: ", recv_constraints, recv_options, recv_config,
zhihuang9763d562016-08-05 11:14:50 -07001386 network_thread_.get(), worker_thread_.get()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001387 if (!initiating_client_ || !receiving_client_) {
1388 return false;
1389 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001390 SetSignalingReceivers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001391 return true;
1392 }
1393
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001394 void SetSignalingReceivers() {
1395 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
1396 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1397 }
1398
1399 void SetSignalingDelayMs(int delay_ms) {
1400 initiating_client_->set_signaling_delay_ms(delay_ms);
1401 receiving_client_->set_signaling_delay_ms(delay_ms);
1402 }
1403
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001404 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1405 const webrtc::FakeConstraints& recv_constraints) {
1406 initiating_client_->SetVideoConstraints(init_constraints);
1407 receiving_client_->SetVideoConstraints(recv_constraints);
1408 }
1409
perkjcaafdba2016-03-20 07:34:29 -07001410 void SetCaptureRotation(webrtc::VideoRotation rotation) {
1411 initiating_client_->SetCaptureRotation(rotation);
1412 receiving_client_->SetCaptureRotation(rotation);
1413 }
1414
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001415 void EnableVideoDecoderFactory() {
1416 initiating_client_->EnableVideoDecoderFactory();
1417 receiving_client_->EnableVideoDecoderFactory();
1418 }
1419
1420 // This test sets up a call between two parties. Both parties send static
1421 // frames to each other. Once the test is finished the number of sent frames
1422 // is compared to the number of received frames.
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -07001423 void LocalP2PTest() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001424 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1425 initiating_client_->AddMediaStream(true, true);
1426 }
1427 initiating_client_->Negotiate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001428 // Assert true is used here since next tests are guaranteed to fail and
1429 // would eat up 5 seconds.
1430 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1431 VerifySessionDescriptions();
1432
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001433 int audio_frame_count = kEndAudioFrameCount;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001434 int video_frame_count = kEndVideoFrameCount;
hta6b4f8392016-03-10 00:24:31 -08001435 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1436
1437 if ((!initiating_client_->can_receive_audio() &&
1438 !initiating_client_->can_receive_video()) ||
1439 (!receiving_client_->can_receive_audio() &&
1440 !receiving_client_->can_receive_video())) {
1441 // Neither audio nor video will flow, so connections won't be
1442 // established. There's nothing more to check.
1443 // TODO(hta): Check connection if there's a data channel.
1444 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001445 }
1446
hta6b4f8392016-03-10 00:24:31 -08001447 // Audio or video is expected to flow, so both clients should reach the
1448 // Connected state, and the offerer (ICE controller) should proceed to
1449 // Completed.
1450 // Note: These tests have been observed to fail under heavy load at
1451 // shorter timeouts, so they may be flaky.
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -07001452 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1453 initiating_client_->ice_connection_state(),
1454 kMaxWaitForFramesMs);
hta6b4f8392016-03-10 00:24:31 -08001455 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1456 receiving_client_->ice_connection_state(),
1457 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001458
hta6b4f8392016-03-10 00:24:31 -08001459 // The ICE gathering state should end up in kIceGatheringComplete,
1460 // but there's a bug that prevents this at the moment, and the state
1461 // machine is being updated by the WEBRTC WG.
1462 // TODO(hta): Update this check when spec revisions finish.
1463 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1464 initiating_client_->ice_gathering_state());
1465 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1466 receiving_client_->ice_gathering_state(),
1467 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001468
hta6b4f8392016-03-10 00:24:31 -08001469 // Check that the expected number of frames have arrived.
1470 EXPECT_TRUE_WAIT(FramesHaveArrived(audio_frame_count, video_frame_count),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001471 kMaxWaitForFramesMs);
1472 }
1473
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001474 void SetupAndVerifyDtlsCall() {
1475 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1476 FakeConstraints setup_constraints;
1477 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1478 true);
kthelgason876222f2016-11-29 01:44:11 -08001479 // Disable resolution adaptation, we don't want it interfering with the
1480 // test results.
1481 webrtc::PeerConnectionInterface::RTCConfiguration rtc_config;
1482 rtc_config.set_cpu_adaptation(false);
1483
1484 ASSERT_TRUE(CreateTestClients(&setup_constraints, nullptr, &rtc_config,
1485 &setup_constraints, nullptr, &rtc_config));
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001486 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001487 VerifyRenderedAspectRatio(640, 480);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001488 }
1489
1490 PeerConnectionTestClient* CreateDtlsClientWithAlternateKey() {
1491 FakeConstraints setup_constraints;
1492 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1493 true);
kthelgason876222f2016-11-29 01:44:11 -08001494 // Disable resolution adaptation, we don't want it interfering with the
1495 // test results.
1496 webrtc::PeerConnectionInterface::RTCConfiguration rtc_config;
1497 rtc_config.set_cpu_adaptation(false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001498
Henrik Boströmd79599d2016-06-01 13:58:50 +02001499 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1500 rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
1501 new FakeRTCCertificateGenerator() : nullptr);
1502 cert_generator->use_alternate_key();
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001503
1504 // Make sure the new client is using a different certificate.
1505 return PeerConnectionTestClient::CreateClientWithDtlsIdentityStore(
kthelgason876222f2016-11-29 01:44:11 -08001506 "New Peer: ", &setup_constraints, nullptr, &rtc_config,
Henrik Boströmd79599d2016-06-01 13:58:50 +02001507 std::move(cert_generator), prefer_constraint_apis_,
danilchape9021a32016-05-17 01:52:02 -07001508 network_thread_.get(), worker_thread_.get());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001509 }
1510
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001511 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1512 // Messages may get lost on the unreliable DataChannel, so we send multiple
1513 // times to avoid test flakiness.
1514 static const size_t kSendAttempts = 5;
1515
1516 for (size_t i = 0; i < kSendAttempts; ++i) {
1517 dc->Send(DataBuffer(data));
1518 }
1519 }
1520
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001521 rtc::Thread* network_thread() { return network_thread_.get(); }
1522
Taylor Brandstetter9b5306c2016-08-18 11:40:37 -07001523 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1524
deadbeefaf1b59c2015-10-15 12:08:41 -07001525 PeerConnectionTestClient* initializing_client() {
1526 return initiating_client_.get();
1527 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001528
1529 // Set the |initiating_client_| to the |client| passed in and return the
1530 // original |initiating_client_|.
1531 PeerConnectionTestClient* set_initializing_client(
1532 PeerConnectionTestClient* client) {
1533 PeerConnectionTestClient* old = initiating_client_.release();
1534 initiating_client_.reset(client);
1535 return old;
1536 }
1537
deadbeefaf1b59c2015-10-15 12:08:41 -07001538 PeerConnectionTestClient* receiving_client() {
1539 return receiving_client_.get();
1540 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001542 // Set the |receiving_client_| to the |client| passed in and return the
1543 // original |receiving_client_|.
1544 PeerConnectionTestClient* set_receiving_client(
1545 PeerConnectionTestClient* client) {
1546 PeerConnectionTestClient* old = receiving_client_.release();
1547 receiving_client_.reset(client);
1548 return old;
1549 }
1550
zhihuang184a3fd2016-06-14 11:47:14 -07001551 bool AllObserversReceived(
1552 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>& observers) {
1553 for (auto& observer : observers) {
1554 if (!observer->first_packet_received()) {
1555 return false;
1556 }
1557 }
1558 return true;
1559 }
1560
jbauchcb560652016-08-04 05:20:32 -07001561 void TestGcmNegotiation(bool local_gcm_enabled, bool remote_gcm_enabled,
1562 int expected_cipher_suite) {
1563 PeerConnectionFactory::Options init_options;
1564 init_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled;
1565 PeerConnectionFactory::Options recv_options;
1566 recv_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001567 ASSERT_TRUE(CreateTestClients(nullptr, &init_options, nullptr, nullptr,
1568 &recv_options, nullptr));
jbauchcb560652016-08-04 05:20:32 -07001569 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1570 init_observer =
1571 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1572 initializing_client()->pc()->RegisterUMAObserver(init_observer);
1573 LocalP2PTest();
1574
1575 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
1576 initializing_client()->GetSrtpCipherStats(),
1577 kMaxWaitMs);
1578 EXPECT_EQ(1,
1579 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1580 expected_cipher_suite));
1581 }
1582
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583 private:
deadbeefeff5b852016-05-27 14:18:01 -07001584 // |ss_| is used by |network_thread_| so it must be destroyed later.
kwibergd1fe2812016-04-27 06:47:29 -07001585 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
1586 std::unique_ptr<rtc::VirtualSocketServer> ss_;
deadbeefeff5b852016-05-27 14:18:01 -07001587 // |network_thread_| and |worker_thread_| are used by both
1588 // |initiating_client_| and |receiving_client_| so they must be destroyed
1589 // later.
1590 std::unique_ptr<rtc::Thread> network_thread_;
1591 std::unique_ptr<rtc::Thread> worker_thread_;
kwibergd1fe2812016-04-27 06:47:29 -07001592 std::unique_ptr<PeerConnectionTestClient> initiating_client_;
1593 std::unique_ptr<PeerConnectionTestClient> receiving_client_;
htaaac2dea2016-03-10 13:35:55 -08001594 bool prefer_constraint_apis_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001597// Disable for TSan v2, see
1598// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1599#if !defined(THREAD_SANITIZER)
1600
zhihuang184a3fd2016-06-14 11:47:14 -07001601TEST_F(P2PTestConductor, TestRtpReceiverObserverCallbackFunction) {
1602 ASSERT_TRUE(CreateTestClients());
1603 LocalP2PTest();
1604 EXPECT_TRUE_WAIT(
1605 AllObserversReceived(initializing_client()->rtp_receiver_observers()),
1606 kMaxWaitForFramesMs);
1607 EXPECT_TRUE_WAIT(
1608 AllObserversReceived(receiving_client()->rtp_receiver_observers()),
1609 kMaxWaitForFramesMs);
1610}
1611
1612// The observers are expected to fire the signal even if they are set after the
1613// first packet is received.
1614TEST_F(P2PTestConductor, TestSetRtpReceiverObserverAfterFirstPacketIsReceived) {
1615 ASSERT_TRUE(CreateTestClients());
1616 LocalP2PTest();
1617 // Reset the RtpReceiverObservers.
1618 initializing_client()->SetRtpReceiverObservers();
1619 receiving_client()->SetRtpReceiverObservers();
1620 EXPECT_TRUE_WAIT(
1621 AllObserversReceived(initializing_client()->rtp_receiver_observers()),
1622 kMaxWaitForFramesMs);
1623 EXPECT_TRUE_WAIT(
1624 AllObserversReceived(receiving_client()->rtp_receiver_observers()),
1625 kMaxWaitForFramesMs);
1626}
1627
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001629// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1630// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001631TEST_F(P2PTestConductor, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632 ASSERT_TRUE(CreateTestClients());
1633 LocalP2PTest();
1634 VerifyDtmf();
1635}
1636
1637// This test sets up a Jsep call between two parties and test that we can get a
1638// video aspect ratio of 16:9.
deadbeef7c73bdb2015-12-10 15:10:44 -08001639TEST_F(P2PTestConductor, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640 ASSERT_TRUE(CreateTestClients());
1641 FakeConstraints constraint;
1642 double requested_ratio = 640.0/360;
1643 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1644 SetVideoConstraints(constraint, constraint);
1645 LocalP2PTest();
1646
1647 ASSERT_LE(0, initializing_client()->rendered_height());
1648 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001649 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650 initializing_client()->rendered_height();
1651 EXPECT_LE(requested_ratio, initiating_video_ratio);
1652
1653 ASSERT_LE(0, receiving_client()->rendered_height());
1654 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001655 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001656 receiving_client()->rendered_height();
1657 EXPECT_LE(requested_ratio, receiving_video_ratio);
1658}
1659
1660// This test sets up a Jsep call between two parties and test that the
1661// received video has a resolution of 1280*720.
1662// TODO(mallinath): Enable when
1663// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
deadbeef7c73bdb2015-12-10 15:10:44 -08001664TEST_F(P2PTestConductor, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665 ASSERT_TRUE(CreateTestClients());
1666 FakeConstraints constraint;
1667 constraint.SetMandatoryMinWidth(1280);
1668 constraint.SetMandatoryMinHeight(720);
1669 SetVideoConstraints(constraint, constraint);
1670 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001671 VerifyRenderedAspectRatio(1280, 720);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672}
1673
1674// This test sets up a call between two endpoints that are configured to use
1675// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001676TEST_F(P2PTestConductor, LocalP2PTestDtls) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001677 SetupAndVerifyDtlsCall();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678}
1679
hta6b4f8392016-03-10 00:24:31 -08001680// This test sets up an one-way call, with media only from initiator to
1681// responder.
1682TEST_F(P2PTestConductor, OneWayMediaCall) {
1683 ASSERT_TRUE(CreateTestClients());
1684 receiving_client()->set_auto_add_stream(false);
1685 LocalP2PTest();
1686}
1687
htaaac2dea2016-03-10 13:35:55 -08001688TEST_F(P2PTestConductor, OneWayMediaCallWithoutConstraints) {
1689 ASSERT_TRUE(CreateTestClientsThatPreferNoConstraints());
1690 receiving_client()->set_auto_add_stream(false);
1691 LocalP2PTest();
1692}
1693
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001694// This test sets up a audio call initially and then upgrades to audio/video,
1695// using DTLS.
deadbeef7c73bdb2015-12-10 15:10:44 -08001696TEST_F(P2PTestConductor, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001697 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001698 FakeConstraints setup_constraints;
1699 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1700 true);
1701 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1702 receiving_client()->SetReceiveAudioVideo(true, false);
1703 LocalP2PTest();
1704 receiving_client()->SetReceiveAudioVideo(true, true);
1705 receiving_client()->Negotiate();
1706}
1707
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001708// This test sets up a call transfer to a new caller with a different DTLS
1709// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001710TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCallee) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001711 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1712 SetupAndVerifyDtlsCall();
1713
1714 // Keeping the original peer around which will still send packets to the
1715 // receiving client. These SRTP packets will be dropped.
kwibergd1fe2812016-04-27 06:47:29 -07001716 std::unique_ptr<PeerConnectionTestClient> original_peer(
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001717 set_initializing_client(CreateDtlsClientWithAlternateKey()));
1718 original_peer->pc()->Close();
1719
1720 SetSignalingReceivers();
1721 receiving_client()->SetExpectIceRestart(true);
1722 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001723 VerifyRenderedAspectRatio(640, 480);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001724}
1725
guoweis46383312015-12-17 16:45:59 -08001726// This test sets up a non-bundle call and apply bundle during ICE restart. When
1727// bundle is in effect in the restart, the channel can successfully reset its
1728// DTLS-SRTP context.
deadbeef4c6696c2016-12-02 18:08:57 -08001729TEST_F(P2PTestConductor, LocalP2PTestDtlsBundleInIceRestart) {
guoweis46383312015-12-17 16:45:59 -08001730 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1731 FakeConstraints setup_constraints;
1732 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1733 true);
1734 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1735 receiving_client()->RemoveBundleFromReceivedSdp(true);
1736 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001737 VerifyRenderedAspectRatio(640, 480);
guoweis46383312015-12-17 16:45:59 -08001738
1739 initializing_client()->IceRestart();
1740 receiving_client()->SetExpectIceRestart(true);
1741 receiving_client()->RemoveBundleFromReceivedSdp(false);
1742 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001743 VerifyRenderedAspectRatio(640, 480);
guoweis46383312015-12-17 16:45:59 -08001744}
1745
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001746// This test sets up a call transfer to a new callee with a different DTLS
1747// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001748TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCaller) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001749 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1750 SetupAndVerifyDtlsCall();
1751
1752 // Keeping the original peer around which will still send packets to the
1753 // receiving client. These SRTP packets will be dropped.
kwibergd1fe2812016-04-27 06:47:29 -07001754 std::unique_ptr<PeerConnectionTestClient> original_peer(
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001755 set_receiving_client(CreateDtlsClientWithAlternateKey()));
1756 original_peer->pc()->Close();
1757
1758 SetSignalingReceivers();
1759 initializing_client()->IceRestart();
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -07001760 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001761 VerifyRenderedAspectRatio(640, 480);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001762}
1763
perkjcaafdba2016-03-20 07:34:29 -07001764TEST_F(P2PTestConductor, LocalP2PTestCVO) {
1765 ASSERT_TRUE(CreateTestClients());
1766 SetCaptureRotation(webrtc::kVideoRotation_90);
1767 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001768 VerifyRenderedAspectRatio(640, 480, webrtc::kVideoRotation_90);
perkjcaafdba2016-03-20 07:34:29 -07001769}
1770
1771TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportCVO) {
1772 ASSERT_TRUE(CreateTestClients());
1773 SetCaptureRotation(webrtc::kVideoRotation_90);
1774 receiving_client()->RemoveCvoFromReceivedSdp(true);
1775 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001776 VerifyRenderedAspectRatio(480, 640, webrtc::kVideoRotation_0);
perkjcaafdba2016-03-20 07:34:29 -07001777}
1778
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779// This test sets up a call between two endpoints that are configured to use
1780// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1781// negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001782TEST_F(P2PTestConductor, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001783 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784 FakeConstraints setup_constraints;
1785 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1786 true);
1787 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1788 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1789 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001790 VerifyRenderedAspectRatio(640, 480);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791}
1792
zhihuangaf388472016-11-02 16:49:48 -07001793// This test verifies that the negotiation will succeed with data channel only
1794// in max-bundle mode.
1795TEST_F(P2PTestConductor, LocalP2PTestOfferDataChannelOnly) {
1796 webrtc::PeerConnectionInterface::RTCConfiguration rtc_config;
1797 rtc_config.bundle_policy =
1798 webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle;
1799 ASSERT_TRUE(CreateTestClients(rtc_config, rtc_config));
1800 initializing_client()->CreateDataChannel();
1801 initializing_client()->Negotiate();
1802}
1803
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001804// This test sets up a Jsep call between two parties, and the callee only
1805// accept to receive video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001806TEST_F(P2PTestConductor, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807 ASSERT_TRUE(CreateTestClients());
1808 receiving_client()->SetReceiveAudioVideo(false, true);
1809 LocalP2PTest();
1810}
1811
1812// This test sets up a Jsep call between two parties, and the callee only
1813// accept to receive audio.
deadbeef7c73bdb2015-12-10 15:10:44 -08001814TEST_F(P2PTestConductor, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001815 ASSERT_TRUE(CreateTestClients());
1816 receiving_client()->SetReceiveAudioVideo(true, false);
1817 LocalP2PTest();
1818}
1819
1820// This test sets up a Jsep call between two parties, and the callee reject both
1821// audio and video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001822TEST_F(P2PTestConductor, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823 ASSERT_TRUE(CreateTestClients());
1824 receiving_client()->SetReceiveAudioVideo(false, false);
1825 LocalP2PTest();
1826}
1827
1828// This test sets up an audio and video call between two parties. After the call
1829// runs for a while (10 frames), the caller sends an update offer with video
1830// being rejected. Once the re-negotiation is done, the video flow should stop
1831// and the audio flow should continue.
deadbeefc9be0072015-12-14 18:27:57 -08001832TEST_F(P2PTestConductor, UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001833 ASSERT_TRUE(CreateTestClients());
1834 LocalP2PTest();
1835 TestUpdateOfferWithRejectedContent();
1836}
1837
1838// This test sets up a Jsep call between two parties. The MSID is removed from
1839// the SDP strings from the caller.
deadbeefc9be0072015-12-14 18:27:57 -08001840TEST_F(P2PTestConductor, LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001841 ASSERT_TRUE(CreateTestClients());
1842 receiving_client()->RemoveMsidFromReceivedSdp(true);
1843 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1844 // audio and video is muxed when MSID is disabled. Remove
1845 // SetRemoveBundleFromSdp once
1846 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1847 receiving_client()->RemoveBundleFromReceivedSdp(true);
1848 LocalP2PTest();
1849}
1850
kthelgasone2397792016-11-08 08:19:48 -08001851TEST_F(P2PTestConductor, LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852 ASSERT_TRUE(CreateTestClients());
1853 // Set optional video constraint to max 320pixels to decrease CPU usage.
1854 FakeConstraints constraint;
1855 constraint.SetOptionalMaxWidth(320);
1856 SetVideoConstraints(constraint, constraint);
1857 initializing_client()->AddMediaStream(true, true);
1858 initializing_client()->AddMediaStream(false, true);
1859 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1860 LocalP2PTest();
1861 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1862}
1863
1864// Test that we can receive the audio output level from a remote audio track.
deadbeef7c73bdb2015-12-10 15:10:44 -08001865TEST_F(P2PTestConductor, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001866 ASSERT_TRUE(CreateTestClients());
1867 LocalP2PTest();
1868
1869 StreamCollectionInterface* remote_streams =
1870 initializing_client()->remote_streams();
1871 ASSERT_GT(remote_streams->count(), 0u);
1872 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1873 MediaStreamTrackInterface* remote_audio_track =
1874 remote_streams->at(0)->GetAudioTracks()[0];
1875
1876 // Get the audio output level stats. Note that the level is not available
1877 // until a RTCP packet has been received.
1878 EXPECT_TRUE_WAIT(
1879 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1880 kMaxWaitForStatsMs);
1881}
1882
1883// Test that an audio input level is reported.
deadbeef7c73bdb2015-12-10 15:10:44 -08001884TEST_F(P2PTestConductor, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001885 ASSERT_TRUE(CreateTestClients());
1886 LocalP2PTest();
1887
1888 // Get the audio input level stats. The level should be available very
1889 // soon after the test starts.
1890 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1891 kMaxWaitForStatsMs);
1892}
1893
1894// Test that we can get incoming byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001895TEST_F(P2PTestConductor, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001896 ASSERT_TRUE(CreateTestClients());
1897 LocalP2PTest();
1898
1899 StreamCollectionInterface* remote_streams =
1900 initializing_client()->remote_streams();
1901 ASSERT_GT(remote_streams->count(), 0u);
1902 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1903 MediaStreamTrackInterface* remote_audio_track =
1904 remote_streams->at(0)->GetAudioTracks()[0];
1905 EXPECT_TRUE_WAIT(
1906 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1907 kMaxWaitForStatsMs);
1908
1909 MediaStreamTrackInterface* remote_video_track =
1910 remote_streams->at(0)->GetVideoTracks()[0];
1911 EXPECT_TRUE_WAIT(
1912 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1913 kMaxWaitForStatsMs);
1914}
1915
1916// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001917TEST_F(P2PTestConductor, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918 ASSERT_TRUE(CreateTestClients());
1919 LocalP2PTest();
1920
1921 StreamCollectionInterface* local_streams =
1922 initializing_client()->local_streams();
1923 ASSERT_GT(local_streams->count(), 0u);
1924 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1925 MediaStreamTrackInterface* local_audio_track =
1926 local_streams->at(0)->GetAudioTracks()[0];
1927 EXPECT_TRUE_WAIT(
1928 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1929 kMaxWaitForStatsMs);
1930
1931 MediaStreamTrackInterface* local_video_track =
1932 local_streams->at(0)->GetVideoTracks()[0];
1933 EXPECT_TRUE_WAIT(
1934 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1935 kMaxWaitForStatsMs);
1936}
1937
Joachim Bauch04e5b492015-05-29 09:40:39 +02001938// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001939TEST_F(P2PTestConductor, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001940 PeerConnectionFactory::Options init_options;
1941 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1942 PeerConnectionFactory::Options recv_options;
1943 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001944 ASSERT_TRUE(CreateTestClients(nullptr, &init_options, nullptr, nullptr,
1945 &recv_options, nullptr));
jbauchac8869e2015-07-03 01:36:14 -07001946 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1947 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1948 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001949 LocalP2PTest();
1950
torbjorng43166b82016-03-11 00:06:47 -08001951 EXPECT_TRUE_WAIT(
1952 rtc::SSLStreamAdapter::IsAcceptableCipher(
1953 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1954 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001955 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001956 initializing_client()->GetSrtpCipherStats(),
1957 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001958 EXPECT_EQ(1,
1959 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1960 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001961}
1962
1963// Test that DTLS 1.2 is used if both ends support it.
torbjorng79a5a832016-01-15 07:16:51 -08001964TEST_F(P2PTestConductor, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001965 PeerConnectionFactory::Options init_options;
1966 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1967 PeerConnectionFactory::Options recv_options;
1968 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001969 ASSERT_TRUE(CreateTestClients(nullptr, &init_options, nullptr, nullptr,
1970 &recv_options, nullptr));
jbauchac8869e2015-07-03 01:36:14 -07001971 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1972 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1973 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001974 LocalP2PTest();
1975
torbjorng43166b82016-03-11 00:06:47 -08001976 EXPECT_TRUE_WAIT(
1977 rtc::SSLStreamAdapter::IsAcceptableCipher(
1978 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1979 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001980 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001981 initializing_client()->GetSrtpCipherStats(),
1982 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001983 EXPECT_EQ(1,
1984 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1985 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001986}
1987
1988// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1989// received supports 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001990TEST_F(P2PTestConductor, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001991 PeerConnectionFactory::Options init_options;
1992 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1993 PeerConnectionFactory::Options recv_options;
1994 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001995 ASSERT_TRUE(CreateTestClients(nullptr, &init_options, nullptr, nullptr,
1996 &recv_options, nullptr));
jbauchac8869e2015-07-03 01:36:14 -07001997 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1998 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1999 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02002000 LocalP2PTest();
2001
torbjorng43166b82016-03-11 00:06:47 -08002002 EXPECT_TRUE_WAIT(
2003 rtc::SSLStreamAdapter::IsAcceptableCipher(
2004 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
2005 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002006 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002007 initializing_client()->GetSrtpCipherStats(),
2008 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002009 EXPECT_EQ(1,
2010 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2011 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02002012}
2013
2014// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
2015// received supports 1.2.
torbjorng43166b82016-03-11 00:06:47 -08002016TEST_F(P2PTestConductor, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02002017 PeerConnectionFactory::Options init_options;
2018 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2019 PeerConnectionFactory::Options recv_options;
2020 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002021 ASSERT_TRUE(CreateTestClients(nullptr, &init_options, nullptr, nullptr,
2022 &recv_options, nullptr));
jbauchac8869e2015-07-03 01:36:14 -07002023 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
2024 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2025 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02002026 LocalP2PTest();
2027
torbjorng43166b82016-03-11 00:06:47 -08002028 EXPECT_TRUE_WAIT(
2029 rtc::SSLStreamAdapter::IsAcceptableCipher(
2030 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
2031 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002032 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002033 initializing_client()->GetSrtpCipherStats(),
2034 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002035 EXPECT_EQ(1,
2036 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2037 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00002038}
2039
jbauchcb560652016-08-04 05:20:32 -07002040// Test that a non-GCM cipher is used if both sides only support non-GCM.
2041TEST_F(P2PTestConductor, GetGcmNone) {
2042 TestGcmNegotiation(false, false, kDefaultSrtpCryptoSuite);
2043}
2044
2045// Test that a GCM cipher is used if both ends support it.
2046TEST_F(P2PTestConductor, GetGcmBoth) {
2047 TestGcmNegotiation(true, true, kDefaultSrtpCryptoSuiteGcm);
2048}
2049
2050// Test that GCM isn't used if only the initiator supports it.
2051TEST_F(P2PTestConductor, GetGcmInit) {
2052 TestGcmNegotiation(true, false, kDefaultSrtpCryptoSuite);
2053}
2054
2055// Test that GCM isn't used if only the receiver supports it.
2056TEST_F(P2PTestConductor, GetGcmRecv) {
2057 TestGcmNegotiation(false, true, kDefaultSrtpCryptoSuite);
2058}
2059
deadbeefb5cb19b2015-11-23 16:39:12 -08002060// This test sets up a call between two parties with audio, video and an RTP
2061// data channel.
deadbeef8f89bff2016-12-01 12:54:20 -08002062TEST_F(P2PTestConductor, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002063 FakeConstraints setup_constraints;
2064 setup_constraints.SetAllowRtpDataChannels();
2065 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
2066 initializing_client()->CreateDataChannel();
2067 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07002068 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
2069 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002070 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2071 kMaxWaitMs);
2072 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
2073 kMaxWaitMs);
2074
2075 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00002076
2077 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002078 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
2079 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00002080
2081 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002082 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
2083 kMaxWaitMs);
2084
2085 receiving_client()->data_channel()->Close();
2086 // Send new offer and answer.
2087 receiving_client()->Negotiate();
2088 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
2089 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
2090}
2091
deadbeefb5cb19b2015-11-23 16:39:12 -08002092// This test sets up a call between two parties with audio, video and an SCTP
2093// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08002094TEST_F(P2PTestConductor, LocalP2PTestSctpDataChannel) {
deadbeefb5cb19b2015-11-23 16:39:12 -08002095 ASSERT_TRUE(CreateTestClients());
2096 initializing_client()->CreateDataChannel();
2097 LocalP2PTest();
2098 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
2099 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
2100 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2101 kMaxWaitMs);
2102 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
2103
2104 std::string data = "hello world";
2105
2106 initializing_client()->data_channel()->Send(DataBuffer(data));
2107 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
2108 kMaxWaitMs);
2109
2110 receiving_client()->data_channel()->Send(DataBuffer(data));
2111 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
2112 kMaxWaitMs);
2113
2114 receiving_client()->data_channel()->Close();
deadbeef15887932015-12-14 19:32:34 -08002115 EXPECT_TRUE_WAIT(!initializing_client()->data_observer()->IsOpen(),
2116 kMaxWaitMs);
2117 EXPECT_TRUE_WAIT(!receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
deadbeefb5cb19b2015-11-23 16:39:12 -08002118}
2119
Taylor Brandstetter9b5306c2016-08-18 11:40:37 -07002120TEST_F(P2PTestConductor, UnorderedSctpDataChannel) {
2121 ASSERT_TRUE(CreateTestClients());
2122 webrtc::DataChannelInit init;
2123 init.ordered = false;
2124 initializing_client()->CreateDataChannel(&init);
2125
2126 // Introduce random network delays.
2127 // Otherwise it's not a true "unordered" test.
2128 virtual_socket_server()->set_delay_mean(20);
2129 virtual_socket_server()->set_delay_stddev(5);
2130 virtual_socket_server()->UpdateDelayDistribution();
2131
2132 initializing_client()->Negotiate();
2133 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
2134 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
2135 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2136 kMaxWaitMs);
2137 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
2138
2139 static constexpr int kNumMessages = 100;
2140 // Deliberately chosen to be larger than the MTU so messages get fragmented.
2141 static constexpr size_t kMaxMessageSize = 4096;
2142 // Create and send random messages.
2143 std::vector<std::string> sent_messages;
2144 for (int i = 0; i < kNumMessages; ++i) {
2145 size_t length = (rand() % kMaxMessageSize) + 1;
2146 std::string message;
2147 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
2148 initializing_client()->data_channel()->Send(DataBuffer(message));
2149 receiving_client()->data_channel()->Send(DataBuffer(message));
2150 sent_messages.push_back(message);
2151 }
2152
2153 EXPECT_EQ_WAIT(
2154 kNumMessages,
2155 initializing_client()->data_observer()->received_message_count(),
2156 kMaxWaitMs);
2157 EXPECT_EQ_WAIT(kNumMessages,
2158 receiving_client()->data_observer()->received_message_count(),
2159 kMaxWaitMs);
2160
2161 // Sort and compare to make sure none of the messages were corrupted.
2162 std::vector<std::string> initializing_client_received_messages =
2163 initializing_client()->data_observer()->messages();
2164 std::vector<std::string> receiving_client_received_messages =
2165 receiving_client()->data_observer()->messages();
2166 std::sort(sent_messages.begin(), sent_messages.end());
2167 std::sort(initializing_client_received_messages.begin(),
2168 initializing_client_received_messages.end());
2169 std::sort(receiving_client_received_messages.begin(),
2170 receiving_client_received_messages.end());
2171 EXPECT_EQ(sent_messages, initializing_client_received_messages);
2172 EXPECT_EQ(sent_messages, receiving_client_received_messages);
2173
2174 receiving_client()->data_channel()->Close();
2175 EXPECT_TRUE_WAIT(!initializing_client()->data_observer()->IsOpen(),
2176 kMaxWaitMs);
2177 EXPECT_TRUE_WAIT(!receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
2178}
2179
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002180// This test sets up a call between two parties and creates a data channel.
2181// The test tests that received data is buffered unless an observer has been
2182// registered.
2183// Rtp data channels can receive data before the underlying
2184// transport has detected that a channel is writable and thus data can be
2185// received before the data channel state changes to open. That is hard to test
2186// but the same buffering is used in that case.
deadbeef7c73bdb2015-12-10 15:10:44 -08002187TEST_F(P2PTestConductor, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188 FakeConstraints setup_constraints;
2189 setup_constraints.SetAllowRtpDataChannels();
2190 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
2191 initializing_client()->CreateDataChannel();
2192 initializing_client()->Negotiate();
2193
deadbeefaf1b59c2015-10-15 12:08:41 -07002194 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
2195 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002196 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2197 kMaxWaitMs);
2198 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
2199 receiving_client()->data_channel()->state(), kMaxWaitMs);
2200
2201 // Unregister the existing observer.
2202 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00002203
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002204 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00002205 SendRtpData(initializing_client()->data_channel(), data);
2206
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002207 // Wait a while to allow the sent data to arrive before an observer is
2208 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002209 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002210
2211 MockDataChannelObserver new_observer(receiving_client()->data_channel());
2212 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
2213}
2214
2215// This test sets up a call between two parties with audio, video and but only
2216// the initiating client support data.
deadbeef8f89bff2016-12-01 12:54:20 -08002217TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00002218 FakeConstraints setup_constraints_1;
2219 setup_constraints_1.SetAllowRtpDataChannels();
2220 // Must disable DTLS to make negotiation succeed.
2221 setup_constraints_1.SetMandatory(
2222 MediaConstraintsInterface::kEnableDtlsSrtp, false);
2223 FakeConstraints setup_constraints_2;
2224 setup_constraints_2.SetMandatory(
2225 MediaConstraintsInterface::kEnableDtlsSrtp, false);
2226 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002227 initializing_client()->CreateDataChannel();
2228 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07002229 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002230 EXPECT_FALSE(receiving_client()->data_channel());
2231 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
2232}
2233
2234// This test sets up a call between two parties with audio, video. When audio
2235// and video is setup and flowing and data channel is negotiated.
deadbeef8f89bff2016-12-01 12:54:20 -08002236TEST_F(P2PTestConductor, AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002237 FakeConstraints setup_constraints;
2238 setup_constraints.SetAllowRtpDataChannels();
2239 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
2240 LocalP2PTest();
2241 initializing_client()->CreateDataChannel();
2242 // Send new offer and answer.
2243 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07002244 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
2245 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002246 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2247 kMaxWaitMs);
2248 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
2249 kMaxWaitMs);
2250}
2251
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00002252// This test sets up a Jsep call with SCTP DataChannel and verifies the
2253// negotiation is completed without error.
2254#ifdef HAVE_SCTP
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002255TEST_F(P2PTestConductor, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002256 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00002257 FakeConstraints constraints;
2258 constraints.SetMandatory(
2259 MediaConstraintsInterface::kEnableDtlsSrtp, true);
2260 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
2261 initializing_client()->CreateDataChannel();
2262 initializing_client()->Negotiate(false, false);
2263}
2264#endif
2265
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002266// This test sets up a call between two parties with audio, and video.
2267// During the call, the initializing side restart ice and the test verifies that
2268// new ice candidates are generated and audio and video still can flow.
deadbeef7c73bdb2015-12-10 15:10:44 -08002269TEST_F(P2PTestConductor, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002270 ASSERT_TRUE(CreateTestClients());
2271
2272 // Negotiate and wait for ice completion and make sure audio and video plays.
2273 LocalP2PTest();
2274
2275 // Create a SDP string of the first audio candidate for both clients.
2276 const webrtc::IceCandidateCollection* audio_candidates_initiator =
2277 initializing_client()->pc()->local_description()->candidates(0);
2278 const webrtc::IceCandidateCollection* audio_candidates_receiver =
2279 receiving_client()->pc()->local_description()->candidates(0);
2280 ASSERT_GT(audio_candidates_initiator->count(), 0u);
2281 ASSERT_GT(audio_candidates_receiver->count(), 0u);
2282 std::string initiator_candidate;
2283 EXPECT_TRUE(
2284 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
2285 std::string receiver_candidate;
2286 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
2287
2288 // Restart ice on the initializing client.
2289 receiving_client()->SetExpectIceRestart(true);
2290 initializing_client()->IceRestart();
2291
2292 // Negotiate and wait for ice completion again and make sure audio and video
2293 // plays.
2294 LocalP2PTest();
2295
2296 // Create a SDP string of the first audio candidate for both clients again.
2297 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
2298 initializing_client()->pc()->local_description()->candidates(0);
2299 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
2300 receiving_client()->pc()->local_description()->candidates(0);
2301 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
2302 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
2303 std::string initiator_candidate_restart;
2304 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
2305 &initiator_candidate_restart));
2306 std::string receiver_candidate_restart;
2307 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
2308 &receiver_candidate_restart));
2309
2310 // Verify that the first candidates in the local session descriptions has
2311 // changed.
2312 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
2313 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
2314}
2315
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07002316TEST_F(P2PTestConductor, IceRenominationDisabled) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002317 PeerConnectionInterface::RTCConfiguration config;
2318 config.enable_ice_renomination = false;
2319 ASSERT_TRUE(CreateTestClients(config, config));
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07002320 LocalP2PTest();
2321
2322 initializing_client()->VerifyLocalIceRenomination();
2323 receiving_client()->VerifyLocalIceRenomination();
2324 initializing_client()->VerifyRemoteIceRenomination();
2325 receiving_client()->VerifyRemoteIceRenomination();
2326}
2327
2328TEST_F(P2PTestConductor, IceRenominationEnabled) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002329 PeerConnectionInterface::RTCConfiguration config;
2330 config.enable_ice_renomination = true;
2331 ASSERT_TRUE(CreateTestClients(config, config));
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07002332 initializing_client()->SetExpectIceRenomination(true);
2333 initializing_client()->SetExpectRemoteIceRenomination(true);
2334 receiving_client()->SetExpectIceRenomination(true);
2335 receiving_client()->SetExpectRemoteIceRenomination(true);
2336 LocalP2PTest();
2337
2338 initializing_client()->VerifyLocalIceRenomination();
2339 receiving_client()->VerifyLocalIceRenomination();
2340 initializing_client()->VerifyRemoteIceRenomination();
2341 receiving_client()->VerifyRemoteIceRenomination();
2342}
2343
deadbeeffaac4972015-11-12 15:33:07 -08002344// This test sets up a call between two parties with audio, and video.
2345// It then renegotiates setting the video m-line to "port 0", then later
2346// renegotiates again, enabling video.
deadbeef7c73bdb2015-12-10 15:10:44 -08002347TEST_F(P2PTestConductor, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08002348 ASSERT_TRUE(CreateTestClients());
2349
2350 // Do initial negotiation. Will result in video and audio sendonly m-lines.
2351 receiving_client()->set_auto_add_stream(false);
2352 initializing_client()->AddMediaStream(true, true);
2353 initializing_client()->Negotiate();
2354
2355 // Negotiate again, disabling the video m-line (receiving client will
2356 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
2357 receiving_client()->SetReceiveVideo(false);
2358 initializing_client()->Negotiate();
2359
2360 // Enable video and do negotiation again, making sure video is received
2361 // end-to-end.
2362 receiving_client()->SetReceiveVideo(true);
2363 receiving_client()->AddMediaStream(true, true);
2364 LocalP2PTest();
2365}
2366
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002367// This test sets up a Jsep call between two parties with external
2368// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00002369// TODO(holmer): Disabled due to sometimes crashing on buildbots.
2370// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08002371TEST_F(P2PTestConductor, DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002372 ASSERT_TRUE(CreateTestClients());
2373 EnableVideoDecoderFactory();
2374 LocalP2PTest();
2375}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00002376
deadbeeffac06552015-11-25 11:26:01 -08002377// This tests that if we negotiate after calling CreateSender but before we
2378// have a track, then set a track later, frames from the newly-set track are
2379// received end-to-end.
deadbeef7c73bdb2015-12-10 15:10:44 -08002380TEST_F(P2PTestConductor, EarlyWarmupTest) {
deadbeeffac06552015-11-25 11:26:01 -08002381 ASSERT_TRUE(CreateTestClients());
deadbeefbd7d8f72015-12-18 16:58:44 -08002382 auto audio_sender =
2383 initializing_client()->pc()->CreateSender("audio", "stream_id");
2384 auto video_sender =
2385 initializing_client()->pc()->CreateSender("video", "stream_id");
deadbeeffac06552015-11-25 11:26:01 -08002386 initializing_client()->Negotiate();
2387 // Wait for ICE connection to complete, without any tracks.
2388 // Note that the receiving client WILL (in HandleIncomingOffer) create
2389 // tracks, so it's only the initiator here that's doing early warmup.
2390 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
2391 VerifySessionDescriptions();
2392 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2393 initializing_client()->ice_connection_state(),
2394 kMaxWaitForFramesMs);
2395 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2396 receiving_client()->ice_connection_state(),
2397 kMaxWaitForFramesMs);
2398 // Now set the tracks, and expect frames to immediately start flowing.
2399 EXPECT_TRUE(
2400 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
2401 EXPECT_TRUE(
2402 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
hta6b4f8392016-03-10 00:24:31 -08002403 EXPECT_TRUE_WAIT(FramesHaveArrived(kEndAudioFrameCount, kEndVideoFrameCount),
deadbeeffac06552015-11-25 11:26:01 -08002404 kMaxWaitForFramesMs);
2405}
2406
zhihuang9763d562016-08-05 11:14:50 -07002407#ifdef HAVE_QUIC
2408// This test sets up a call between two parties using QUIC instead of DTLS for
2409// audio and video, and a QUIC data channel.
2410TEST_F(P2PTestConductor, LocalP2PTestQuicDataChannel) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002411 PeerConnectionInterface::RTCConfiguration quic_config;
2412 quic_config.enable_quic = true;
2413 ASSERT_TRUE(CreateTestClients(quic_config, quic_config));
zhihuang9763d562016-08-05 11:14:50 -07002414 webrtc::DataChannelInit init;
2415 init.ordered = false;
2416 init.reliable = true;
2417 init.id = 1;
2418 initializing_client()->CreateDataChannel(&init);
2419 receiving_client()->CreateDataChannel(&init);
2420 LocalP2PTest();
2421 ASSERT_NE(nullptr, initializing_client()->data_channel());
2422 ASSERT_NE(nullptr, receiving_client()->data_channel());
2423 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2424 kMaxWaitMs);
2425 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
2426
2427 std::string data = "hello world";
2428
2429 initializing_client()->data_channel()->Send(DataBuffer(data));
2430 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
2431 kMaxWaitMs);
2432
2433 receiving_client()->data_channel()->Send(DataBuffer(data));
2434 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
2435 kMaxWaitMs);
2436}
2437
2438// Tests that negotiation of QUIC data channels is completed without error.
2439TEST_F(P2PTestConductor, NegotiateQuicDataChannel) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002440 PeerConnectionInterface::RTCConfiguration quic_config;
2441 quic_config.enable_quic = true;
2442 ASSERT_TRUE(CreateTestClients(quic_config, quic_config));
zhihuang9763d562016-08-05 11:14:50 -07002443 FakeConstraints constraints;
2444 constraints.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp, true);
2445 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
2446 webrtc::DataChannelInit init;
2447 init.ordered = false;
2448 init.reliable = true;
2449 init.id = 1;
2450 initializing_client()->CreateDataChannel(&init);
2451 initializing_client()->Negotiate(false, false);
2452}
2453
2454// This test sets up a JSEP call using QUIC. The callee only receives video.
2455TEST_F(P2PTestConductor, LocalP2PTestVideoOnlyWithQuic) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002456 PeerConnectionInterface::RTCConfiguration quic_config;
2457 quic_config.enable_quic = true;
2458 ASSERT_TRUE(CreateTestClients(quic_config, quic_config));
zhihuang9763d562016-08-05 11:14:50 -07002459 receiving_client()->SetReceiveAudioVideo(false, true);
2460 LocalP2PTest();
2461}
2462
2463// This test sets up a JSEP call using QUIC. The callee only receives audio.
2464TEST_F(P2PTestConductor, LocalP2PTestAudioOnlyWithQuic) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002465 PeerConnectionInterface::RTCConfiguration quic_config;
2466 quic_config.enable_quic = true;
2467 ASSERT_TRUE(CreateTestClients(quic_config, quic_config));
zhihuang9763d562016-08-05 11:14:50 -07002468 receiving_client()->SetReceiveAudioVideo(true, false);
2469 LocalP2PTest();
2470}
2471
2472// This test sets up a JSEP call using QUIC. The callee rejects both audio and
2473// video.
2474TEST_F(P2PTestConductor, LocalP2PTestNoVideoAudioWithQuic) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002475 PeerConnectionInterface::RTCConfiguration quic_config;
2476 quic_config.enable_quic = true;
2477 ASSERT_TRUE(CreateTestClients(quic_config, quic_config));
zhihuang9763d562016-08-05 11:14:50 -07002478 receiving_client()->SetReceiveAudioVideo(false, false);
2479 LocalP2PTest();
2480}
2481
2482#endif // HAVE_QUIC
2483
nissed98cf1f2016-04-22 07:27:36 -07002484TEST_F(P2PTestConductor, ForwardVideoOnlyStream) {
2485 ASSERT_TRUE(CreateTestClients());
2486 // One-way stream
2487 receiving_client()->set_auto_add_stream(false);
2488 // Video only, audio forwarding not expected to work.
2489 initializing_client()->AddMediaStream(false, true);
2490 initializing_client()->Negotiate();
2491
2492 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
2493 VerifySessionDescriptions();
2494
2495 ASSERT_TRUE(initializing_client()->can_receive_video());
2496 ASSERT_TRUE(receiving_client()->can_receive_video());
2497
2498 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2499 initializing_client()->ice_connection_state(),
2500 kMaxWaitForFramesMs);
2501 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2502 receiving_client()->ice_connection_state(),
2503 kMaxWaitForFramesMs);
2504
2505 ASSERT_TRUE(receiving_client()->remote_streams()->count() == 1);
2506
2507 // Echo the stream back.
2508 receiving_client()->pc()->AddStream(
2509 receiving_client()->remote_streams()->at(0));
2510 receiving_client()->Negotiate();
2511
2512 EXPECT_TRUE_WAIT(
2513 initializing_client()->VideoFramesReceivedCheck(kEndVideoFrameCount),
2514 kMaxWaitForFramesMs);
2515}
2516
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002517// Test that we achieve the expected end-to-end connection time, using a
2518// fake clock and simulated latency on the media and signaling paths.
2519// We use a TURN<->TURN connection because this is usually the quickest to
2520// set up initially, especially when we're confident the connection will work
2521// and can start sending media before we get a STUN response.
2522//
2523// With various optimizations enabled, here are the network delays we expect to
2524// be on the critical path:
2525// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
2526// signaling answer (with DTLS fingerprint).
2527// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
2528// using TURN<->TURN pair, and DTLS exchange is 4 packets,
2529// the first of which should have arrived before the answer.
2530TEST_F(P2PTestConductor, EndToEndConnectionTimeWithTurnTurnPair) {
2531 rtc::ScopedFakeClock fake_clock;
2532 // Some things use a time of "0" as a special value, so we need to start out
2533 // the fake clock at a nonzero time.
2534 // TODO(deadbeef): Fix this.
2535 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
2536
2537 static constexpr int media_hop_delay_ms = 50;
2538 static constexpr int signaling_trip_delay_ms = 500;
2539 // For explanation of these values, see comment above.
2540 static constexpr int required_media_hops = 9;
2541 static constexpr int required_signaling_trips = 2;
2542 // For internal delays (such as posting an event asychronously).
2543 static constexpr int allowed_internal_delay_ms = 20;
2544 static constexpr int total_connection_time_ms =
2545 media_hop_delay_ms * required_media_hops +
2546 signaling_trip_delay_ms * required_signaling_trips +
2547 allowed_internal_delay_ms;
2548
2549 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
2550 3478};
2551 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
2552 0};
2553 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
2554 3478};
2555 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
2556 0};
2557 cricket::TestTurnServer turn_server_1(network_thread(),
2558 turn_server_1_internal_address,
2559 turn_server_1_external_address);
2560 cricket::TestTurnServer turn_server_2(network_thread(),
2561 turn_server_2_internal_address,
2562 turn_server_2_external_address);
2563 // Bypass permission check on received packets so media can be sent before
2564 // the candidate is signaled.
2565 turn_server_1.set_enable_permission_checks(false);
2566 turn_server_2.set_enable_permission_checks(false);
2567
2568 PeerConnectionInterface::RTCConfiguration client_1_config;
2569 webrtc::PeerConnectionInterface::IceServer ice_server_1;
2570 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
2571 ice_server_1.username = "test";
2572 ice_server_1.password = "test";
2573 client_1_config.servers.push_back(ice_server_1);
2574 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2575 client_1_config.presume_writable_when_fully_relayed = true;
2576
2577 PeerConnectionInterface::RTCConfiguration client_2_config;
2578 webrtc::PeerConnectionInterface::IceServer ice_server_2;
2579 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
2580 ice_server_2.username = "test";
2581 ice_server_2.password = "test";
2582 client_2_config.servers.push_back(ice_server_2);
2583 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2584 client_2_config.presume_writable_when_fully_relayed = true;
2585
2586 ASSERT_TRUE(CreateTestClients(client_1_config, client_2_config));
2587 // Set up the simulated delays.
2588 SetSignalingDelayMs(signaling_trip_delay_ms);
2589 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
2590 virtual_socket_server()->UpdateDelayDistribution();
2591
2592 initializing_client()->SetOfferToReceiveAudioVideo(true, true);
2593 initializing_client()->Negotiate();
2594 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
2595 // are connected. This is an important distinction. Once we have separate ICE
2596 // and DTLS state, this check needs to use the DTLS state.
2597 EXPECT_TRUE_SIMULATED_WAIT(
2598 (receiving_client()->ice_connection_state() ==
2599 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
2600 receiving_client()->ice_connection_state() ==
2601 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
2602 (initializing_client()->ice_connection_state() ==
2603 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
2604 initializing_client()->ice_connection_state() ==
2605 webrtc::PeerConnectionInterface::kIceConnectionCompleted),
2606 total_connection_time_ms, fake_clock);
2607 // Need to free the clients here since they're using things we created on
2608 // the stack.
2609 delete set_initializing_client(nullptr);
2610 delete set_receiving_client(nullptr);
2611}
2612
deadbeef0a6c4ca2015-10-06 11:38:28 -07002613class IceServerParsingTest : public testing::Test {
2614 public:
2615 // Convenience for parsing a single URL.
2616 bool ParseUrl(const std::string& url) {
2617 return ParseUrl(url, std::string(), std::string());
2618 }
2619
2620 bool ParseUrl(const std::string& url,
2621 const std::string& username,
2622 const std::string& password) {
hnslb0f04fd2016-12-19 04:10:30 -08002623 return ParseUrl(
2624 url, username, password,
2625 PeerConnectionInterface::TlsCertPolicy::kTlsCertPolicySecure);
2626 }
2627
2628 bool ParseUrl(const std::string& url,
2629 const std::string& username,
2630 const std::string& password,
2631 PeerConnectionInterface::TlsCertPolicy tls_certificate_policy) {
deadbeef0a6c4ca2015-10-06 11:38:28 -07002632 PeerConnectionInterface::IceServers servers;
2633 PeerConnectionInterface::IceServer server;
2634 server.urls.push_back(url);
2635 server.username = username;
2636 server.password = password;
hnslb0f04fd2016-12-19 04:10:30 -08002637 server.tls_cert_policy = tls_certificate_policy;
deadbeef0a6c4ca2015-10-06 11:38:28 -07002638 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002639 return webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002640 }
2641
2642 protected:
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002643 cricket::ServerAddresses stun_servers_;
2644 std::vector<cricket::RelayServerConfig> turn_servers_;
deadbeef0a6c4ca2015-10-06 11:38:28 -07002645};
2646
2647// Make sure all STUN/TURN prefixes are parsed correctly.
2648TEST_F(IceServerParsingTest, ParseStunPrefixes) {
2649 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002650 EXPECT_EQ(1U, stun_servers_.size());
2651 EXPECT_EQ(0U, turn_servers_.size());
2652 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002653
2654 EXPECT_TRUE(ParseUrl("stuns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002655 EXPECT_EQ(1U, stun_servers_.size());
2656 EXPECT_EQ(0U, turn_servers_.size());
2657 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002658
2659 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002660 EXPECT_EQ(0U, stun_servers_.size());
2661 EXPECT_EQ(1U, turn_servers_.size());
hnsl277b2502016-12-13 05:17:23 -08002662 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002663 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002664
2665 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002666 EXPECT_EQ(0U, stun_servers_.size());
2667 EXPECT_EQ(1U, turn_servers_.size());
hnsl277b2502016-12-13 05:17:23 -08002668 EXPECT_EQ(cricket::PROTO_TLS, turn_servers_[0].ports[0].proto);
hnslb0f04fd2016-12-19 04:10:30 -08002669 EXPECT_TRUE(turn_servers_[0].tls_cert_policy ==
2670 cricket::TlsCertPolicy::TLS_CERT_POLICY_SECURE);
2671 turn_servers_.clear();
2672
2673 EXPECT_TRUE(ParseUrl(
2674 "turns:hostname", "", "",
2675 PeerConnectionInterface::TlsCertPolicy::kTlsCertPolicyInsecureNoCheck));
2676 EXPECT_EQ(0U, stun_servers_.size());
2677 EXPECT_EQ(1U, turn_servers_.size());
2678 EXPECT_TRUE(turn_servers_[0].tls_cert_policy ==
2679 cricket::TlsCertPolicy::TLS_CERT_POLICY_INSECURE_NO_CHECK);
2680 EXPECT_EQ(cricket::PROTO_TLS, turn_servers_[0].ports[0].proto);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002681 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002682
2683 // invalid prefixes
2684 EXPECT_FALSE(ParseUrl("stunn:hostname"));
2685 EXPECT_FALSE(ParseUrl(":hostname"));
2686 EXPECT_FALSE(ParseUrl(":"));
2687 EXPECT_FALSE(ParseUrl(""));
2688}
2689
2690TEST_F(IceServerParsingTest, VerifyDefaults) {
2691 // TURNS defaults
2692 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002693 EXPECT_EQ(1U, turn_servers_.size());
2694 EXPECT_EQ(5349, turn_servers_[0].ports[0].address.port());
hnsl277b2502016-12-13 05:17:23 -08002695 EXPECT_EQ(cricket::PROTO_TLS, turn_servers_[0].ports[0].proto);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002696 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002697
2698 // TURN defaults
2699 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002700 EXPECT_EQ(1U, turn_servers_.size());
2701 EXPECT_EQ(3478, turn_servers_[0].ports[0].address.port());
2702 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2703 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002704
2705 // STUN defaults
2706 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002707 EXPECT_EQ(1U, stun_servers_.size());
2708 EXPECT_EQ(3478, stun_servers_.begin()->port());
2709 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002710}
2711
2712// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
2713// can be parsed correctly.
2714TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
2715 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002716 EXPECT_EQ(1U, stun_servers_.size());
2717 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2718 EXPECT_EQ(1234, stun_servers_.begin()->port());
2719 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002720
2721 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002722 EXPECT_EQ(1U, stun_servers_.size());
2723 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2724 EXPECT_EQ(4321, stun_servers_.begin()->port());
2725 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002726
2727 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002728 EXPECT_EQ(1U, stun_servers_.size());
2729 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2730 EXPECT_EQ(9999, stun_servers_.begin()->port());
2731 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002732
2733 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002734 EXPECT_EQ(1U, stun_servers_.size());
2735 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2736 EXPECT_EQ(3478, stun_servers_.begin()->port());
2737 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002738
2739 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002740 EXPECT_EQ(1U, stun_servers_.size());
2741 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2742 EXPECT_EQ(3478, stun_servers_.begin()->port());
2743 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002744
2745 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002746 EXPECT_EQ(1U, stun_servers_.size());
2747 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2748 EXPECT_EQ(3478, stun_servers_.begin()->port());
2749 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002750
2751 // Try some invalid hostname:port strings.
2752 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
2753 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002754 EXPECT_FALSE(ParseUrl("stun:hostname:port:more"));
2755 EXPECT_FALSE(ParseUrl("stun:hostname:port more"));
deadbeef0a6c4ca2015-10-06 11:38:28 -07002756 EXPECT_FALSE(ParseUrl("stun:hostname:"));
2757 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
2758 EXPECT_FALSE(ParseUrl("stun::5555"));
2759 EXPECT_FALSE(ParseUrl("stun:"));
2760}
2761
2762// Test parsing the "?transport=xxx" part of the URL.
2763TEST_F(IceServerParsingTest, ParseTransport) {
2764 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002765 EXPECT_EQ(1U, turn_servers_.size());
2766 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
2767 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002768
2769 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002770 EXPECT_EQ(1U, turn_servers_.size());
2771 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2772 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002773
2774 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
hnslbd44bb02016-12-12 03:14:30 -08002775 EXPECT_FALSE(ParseUrl("turn:hostname?transport="));
2776 EXPECT_FALSE(ParseUrl("turn:hostname?="));
2777 EXPECT_FALSE(ParseUrl("?"));
deadbeef0a6c4ca2015-10-06 11:38:28 -07002778}
2779
2780// Test parsing ICE username contained in URL.
2781TEST_F(IceServerParsingTest, ParseUsername) {
2782 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002783 EXPECT_EQ(1U, turn_servers_.size());
2784 EXPECT_EQ("user", turn_servers_[0].credentials.username);
2785 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002786
2787 EXPECT_FALSE(ParseUrl("turn:@hostname"));
2788 EXPECT_FALSE(ParseUrl("turn:username@"));
2789 EXPECT_FALSE(ParseUrl("turn:@"));
2790 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
2791}
2792
2793// Test that username and password from IceServer is copied into the resulting
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002794// RelayServerConfig.
deadbeef0a6c4ca2015-10-06 11:38:28 -07002795TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
2796 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002797 EXPECT_EQ(1U, turn_servers_.size());
2798 EXPECT_EQ("username", turn_servers_[0].credentials.username);
2799 EXPECT_EQ("password", turn_servers_[0].credentials.password);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002800}
2801
2802// Ensure that if a server has multiple URLs, each one is parsed.
2803TEST_F(IceServerParsingTest, ParseMultipleUrls) {
2804 PeerConnectionInterface::IceServers servers;
2805 PeerConnectionInterface::IceServer server;
2806 server.urls.push_back("stun:hostname");
2807 server.urls.push_back("turn:hostname");
2808 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002809 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2810 EXPECT_EQ(1U, stun_servers_.size());
2811 EXPECT_EQ(1U, turn_servers_.size());
deadbeef0a6c4ca2015-10-06 11:38:28 -07002812}
2813
Taylor Brandstetter893505d2016-01-07 15:12:48 -08002814// Ensure that TURN servers are given unique priorities,
2815// so that their resulting candidates have unique priorities.
2816TEST_F(IceServerParsingTest, TurnServerPrioritiesUnique) {
2817 PeerConnectionInterface::IceServers servers;
2818 PeerConnectionInterface::IceServer server;
2819 server.urls.push_back("turn:hostname");
2820 server.urls.push_back("turn:hostname2");
2821 servers.push_back(server);
2822 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2823 EXPECT_EQ(2U, turn_servers_.size());
2824 EXPECT_NE(turn_servers_[0].priority, turn_servers_[1].priority);
2825}
2826
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00002827#endif // if !defined(THREAD_SANITIZER)
hta6b4f8392016-03-10 00:24:31 -08002828
2829} // namespace