blob: c8a1eb6e2a1c48a6189637f7a7dea889ae717742 [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"
hnsl04833622017-01-09 08:35:45 -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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049using cricket::ContentInfo;
50using cricket::FakeWebRtcVideoDecoder;
51using cricket::FakeWebRtcVideoDecoderFactory;
52using cricket::FakeWebRtcVideoEncoder;
53using cricket::FakeWebRtcVideoEncoderFactory;
54using cricket::MediaContentDescription;
55using webrtc::DataBuffer;
56using webrtc::DataChannelInterface;
57using webrtc::DtmfSender;
58using webrtc::DtmfSenderInterface;
59using webrtc::DtmfSenderObserverInterface;
60using webrtc::FakeConstraints;
61using webrtc::MediaConstraintsInterface;
deadbeeffaac4972015-11-12 15:33:07 -080062using webrtc::MediaStreamInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063using webrtc::MediaStreamTrackInterface;
64using webrtc::MockCreateSessionDescriptionObserver;
65using webrtc::MockDataChannelObserver;
66using webrtc::MockSetSessionDescriptionObserver;
67using webrtc::MockStatsObserver;
deadbeeffaac4972015-11-12 15:33:07 -080068using webrtc::ObserverInterface;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000069using webrtc::PeerConnectionInterface;
Joachim Bauch04e5b492015-05-29 09:40:39 +020070using webrtc::PeerConnectionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071using webrtc::SessionDescriptionInterface;
72using webrtc::StreamCollectionInterface;
73
hta6b4f8392016-03-10 00:24:31 -080074namespace {
75
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000076static const int kMaxWaitMs = 10000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000077// Disable for TSan v2, see
78// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
79// This declaration is also #ifdef'd as it causes uninitialized-variable
80// warnings.
81#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082static const int kMaxWaitForStatsMs = 3000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000083#endif
deadbeeffac06552015-11-25 11:26:01 -080084static const int kMaxWaitForActivationMs = 5000;
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000085static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086static const int kEndAudioFrameCount = 3;
87static const int kEndVideoFrameCount = 3;
88
89static const char kStreamLabelBase[] = "stream_label";
90static const char kVideoTrackLabelBase[] = "video_track";
91static const char kAudioTrackLabelBase[] = "audio_track";
92static const char kDataChannelLabel[] = "data_channel";
93
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +000094// Disable for TSan v2, see
95// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
96// This declaration is also #ifdef'd as it causes unused-variable errors.
97#if !defined(THREAD_SANITIZER)
98// SRTP cipher name negotiated by the tests. This must be updated if the
99// default changes.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800100static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
jbauchcb560652016-08-04 05:20:32 -0700101static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000102#endif
103
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700104// Used to simulate signaling ICE/SDP between two PeerConnections.
105enum Message { MSG_SDP_MESSAGE, MSG_ICE_MESSAGE };
106
107struct SdpMessage {
108 std::string type;
109 std::string msg;
110};
111
112struct IceMessage {
113 std::string sdp_mid;
114 int sdp_mline_index;
115 std::string msg;
116};
117
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118static void RemoveLinesFromSdp(const std::string& line_start,
119 std::string* sdp) {
120 const char kSdpLineEnd[] = "\r\n";
121 size_t ssrc_pos = 0;
122 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
123 std::string::npos) {
124 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
125 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
126 }
127}
128
hta6b4f8392016-03-10 00:24:31 -0800129bool StreamsHaveAudioTrack(StreamCollectionInterface* streams) {
130 for (size_t idx = 0; idx < streams->count(); idx++) {
131 auto stream = streams->at(idx);
132 if (stream->GetAudioTracks().size() > 0) {
133 return true;
134 }
135 }
136 return false;
137}
138
139bool StreamsHaveVideoTrack(StreamCollectionInterface* streams) {
140 for (size_t idx = 0; idx < streams->count(); idx++) {
141 auto stream = streams->at(idx);
142 if (stream->GetVideoTracks().size() > 0) {
143 return true;
144 }
145 }
146 return false;
147}
148
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149class SignalingMessageReceiver {
150 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 virtual void ReceiveSdpMessage(const std::string& type,
152 std::string& msg) = 0;
153 virtual void ReceiveIceMessage(const std::string& sdp_mid,
154 int sdp_mline_index,
155 const std::string& msg) = 0;
156
157 protected:
deadbeefaf1b59c2015-10-15 12:08:41 -0700158 SignalingMessageReceiver() {}
159 virtual ~SignalingMessageReceiver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160};
161
zhihuang184a3fd2016-06-14 11:47:14 -0700162class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
163 public:
164 MockRtpReceiverObserver(cricket::MediaType media_type)
165 : expected_media_type_(media_type) {}
166
167 void OnFirstPacketReceived(cricket::MediaType media_type) override {
168 ASSERT_EQ(expected_media_type_, media_type);
169 first_packet_received_ = true;
170 }
171
172 bool first_packet_received() { return first_packet_received_; }
173
174 virtual ~MockRtpReceiverObserver() {}
175
176 private:
177 bool first_packet_received_ = false;
178 cricket::MediaType expected_media_type_;
179};
180
deadbeefaf1b59c2015-10-15 12:08:41 -0700181class PeerConnectionTestClient : public webrtc::PeerConnectionObserver,
deadbeeffaac4972015-11-12 15:33:07 -0800182 public SignalingMessageReceiver,
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700183 public ObserverInterface,
184 public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 public:
kjellander71a1b612016-11-07 01:18:08 -0800186 // We need these using declarations because there are two versions of each of
187 // the below methods and we only override one of them.
188 // TODO(deadbeef): Remove once there's only one version of the methods.
189 using PeerConnectionObserver::OnAddStream;
190 using PeerConnectionObserver::OnRemoveStream;
191 using PeerConnectionObserver::OnDataChannel;
192
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700193 // If |config| is not provided, uses a default constructed RTCConfiguration.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800194 static PeerConnectionTestClient* CreateClientWithDtlsIdentityStore(
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800195 const std::string& id,
196 const MediaConstraintsInterface* constraints,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800197 const PeerConnectionFactory::Options* options,
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700198 const PeerConnectionInterface::RTCConfiguration* config,
Henrik Boströmd79599d2016-06-01 13:58:50 +0200199 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
perkj8aba9972016-04-10 23:54:34 -0700200 bool prefer_constraint_apis,
danilchape9021a32016-05-17 01:52:02 -0700201 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700202 rtc::Thread* worker_thread) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800203 PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
zhihuang9763d562016-08-05 11:14:50 -0700204 if (!client->Init(constraints, options, config, std::move(cert_generator),
danilchape9021a32016-05-17 01:52:02 -0700205 prefer_constraint_apis, network_thread, worker_thread)) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800206 delete client;
207 return nullptr;
208 }
209 return client;
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800210 }
211
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800212 static PeerConnectionTestClient* CreateClient(
213 const std::string& id,
214 const MediaConstraintsInterface* constraints,
perkj8aba9972016-04-10 23:54:34 -0700215 const PeerConnectionFactory::Options* options,
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700216 const PeerConnectionInterface::RTCConfiguration* config,
danilchape9021a32016-05-17 01:52:02 -0700217 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700218 rtc::Thread* worker_thread) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200219 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
deadbeefeaa826c2017-01-20 15:15:58 -0800220 new FakeRTCCertificateGenerator());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800221
zhihuang9763d562016-08-05 11:14:50 -0700222 return CreateClientWithDtlsIdentityStore(id, constraints, options, config,
223 std::move(cert_generator), true,
224 network_thread, worker_thread);
htaaac2dea2016-03-10 13:35:55 -0800225 }
226
227 static PeerConnectionTestClient* CreateClientPreferNoConstraints(
228 const std::string& id,
perkj8aba9972016-04-10 23:54:34 -0700229 const PeerConnectionFactory::Options* options,
danilchape9021a32016-05-17 01:52:02 -0700230 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700231 rtc::Thread* worker_thread) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200232 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
deadbeefeaa826c2017-01-20 15:15:58 -0800233 new FakeRTCCertificateGenerator());
htaaac2dea2016-03-10 13:35:55 -0800234
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700235 return CreateClientWithDtlsIdentityStore(id, nullptr, options, nullptr,
zhihuang9763d562016-08-05 11:14:50 -0700236 std::move(cert_generator), false,
237 network_thread, worker_thread);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800238 }
239
deadbeefaf1b59c2015-10-15 12:08:41 -0700240 ~PeerConnectionTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241 }
242
deadbeefaf1b59c2015-10-15 12:08:41 -0700243 void Negotiate() { Negotiate(true, true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244
deadbeefaf1b59c2015-10-15 12:08:41 -0700245 void Negotiate(bool audio, bool video) {
kwibergd1fe2812016-04-27 06:47:29 -0700246 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700247 ASSERT_TRUE(DoCreateOffer(&offer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248
deadbeefaf1b59c2015-10-15 12:08:41 -0700249 if (offer->description()->GetContentByName("audio")) {
250 offer->description()->GetContentByName("audio")->rejected = !audio;
251 }
252 if (offer->description()->GetContentByName("video")) {
253 offer->description()->GetContentByName("video")->rejected = !video;
254 }
255
256 std::string sdp;
257 EXPECT_TRUE(offer->ToString(&sdp));
258 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700259 SendSdpMessage(webrtc::SessionDescriptionInterface::kOffer, sdp);
260 }
261
262 void SendSdpMessage(const std::string& type, std::string& msg) {
263 if (signaling_delay_ms_ == 0) {
264 if (signaling_message_receiver_) {
265 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
266 }
267 } else {
268 rtc::Thread::Current()->PostDelayed(
269 RTC_FROM_HERE, signaling_delay_ms_, this, MSG_SDP_MESSAGE,
270 new rtc::TypedMessageData<SdpMessage>({type, msg}));
271 }
272 }
273
274 void SendIceMessage(const std::string& sdp_mid,
275 int sdp_mline_index,
276 const std::string& msg) {
277 if (signaling_delay_ms_ == 0) {
278 if (signaling_message_receiver_) {
279 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
280 msg);
281 }
282 } else {
283 rtc::Thread::Current()->PostDelayed(RTC_FROM_HERE, signaling_delay_ms_,
284 this, MSG_ICE_MESSAGE,
285 new rtc::TypedMessageData<IceMessage>(
286 {sdp_mid, sdp_mline_index, msg}));
287 }
288 }
289
290 // MessageHandler callback.
291 void OnMessage(rtc::Message* msg) override {
292 switch (msg->message_id) {
293 case MSG_SDP_MESSAGE: {
294 auto sdp_message =
295 static_cast<rtc::TypedMessageData<SdpMessage>*>(msg->pdata);
296 if (signaling_message_receiver_) {
297 signaling_message_receiver_->ReceiveSdpMessage(
298 sdp_message->data().type, sdp_message->data().msg);
299 }
300 delete sdp_message;
301 break;
302 }
303 case MSG_ICE_MESSAGE: {
304 auto ice_message =
305 static_cast<rtc::TypedMessageData<IceMessage>*>(msg->pdata);
306 if (signaling_message_receiver_) {
307 signaling_message_receiver_->ReceiveIceMessage(
308 ice_message->data().sdp_mid, ice_message->data().sdp_mline_index,
309 ice_message->data().msg);
310 }
311 delete ice_message;
312 break;
313 }
314 default:
315 RTC_CHECK(false);
316 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700317 }
318
319 // SignalingMessageReceiver callback.
320 void ReceiveSdpMessage(const std::string& type, std::string& msg) override {
321 FilterIncomingSdpMessage(&msg);
322 if (type == webrtc::SessionDescriptionInterface::kOffer) {
323 HandleIncomingOffer(msg);
324 } else {
325 HandleIncomingAnswer(msg);
326 }
327 }
328
329 // SignalingMessageReceiver callback.
330 void ReceiveIceMessage(const std::string& sdp_mid,
331 int sdp_mline_index,
332 const std::string& msg) override {
333 LOG(INFO) << id_ << "ReceiveIceMessage";
kwibergd1fe2812016-04-27 06:47:29 -0700334 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
deadbeefaf1b59c2015-10-15 12:08:41 -0700335 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
336 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
337 }
338
339 // PeerConnectionObserver callbacks.
340 void OnSignalingChange(
341 webrtc::PeerConnectionInterface::SignalingState new_state) override {
342 EXPECT_EQ(pc()->signaling_state(), new_state);
343 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700344 void OnAddStream(
345 rtc::scoped_refptr<MediaStreamInterface> media_stream) override {
deadbeeffaac4972015-11-12 15:33:07 -0800346 media_stream->RegisterObserver(this);
deadbeefaf1b59c2015-10-15 12:08:41 -0700347 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
348 const std::string id = media_stream->GetVideoTracks()[i]->id();
349 ASSERT_TRUE(fake_video_renderers_.find(id) ==
350 fake_video_renderers_.end());
deadbeefc9be0072015-12-14 18:27:57 -0800351 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
352 media_stream->GetVideoTracks()[i]));
deadbeefaf1b59c2015-10-15 12:08:41 -0700353 }
354 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700355 void OnRemoveStream(
356 rtc::scoped_refptr<MediaStreamInterface> media_stream) override {}
deadbeefaf1b59c2015-10-15 12:08:41 -0700357 void OnRenegotiationNeeded() override {}
358 void OnIceConnectionChange(
359 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
360 EXPECT_EQ(pc()->ice_connection_state(), new_state);
361 }
362 void OnIceGatheringChange(
363 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
364 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
365 }
366 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
367 LOG(INFO) << id_ << "OnIceCandidate";
368
369 std::string ice_sdp;
370 EXPECT_TRUE(candidate->ToString(&ice_sdp));
371 if (signaling_message_receiver_ == nullptr) {
372 // Remote party may be deleted.
373 return;
374 }
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700375 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
deadbeefaf1b59c2015-10-15 12:08:41 -0700376 }
377
deadbeeffaac4972015-11-12 15:33:07 -0800378 // MediaStreamInterface callback
379 void OnChanged() override {
380 // Track added or removed from MediaStream, so update our renderers.
381 rtc::scoped_refptr<StreamCollectionInterface> remote_streams =
382 pc()->remote_streams();
383 // Remove renderers for tracks that were removed.
384 for (auto it = fake_video_renderers_.begin();
385 it != fake_video_renderers_.end();) {
386 if (remote_streams->FindVideoTrack(it->first) == nullptr) {
deadbeefc9be0072015-12-14 18:27:57 -0800387 auto to_remove = it++;
388 removed_fake_video_renderers_.push_back(std::move(to_remove->second));
389 fake_video_renderers_.erase(to_remove);
deadbeeffaac4972015-11-12 15:33:07 -0800390 } else {
391 ++it;
392 }
393 }
394 // Create renderers for new video tracks.
395 for (size_t stream_index = 0; stream_index < remote_streams->count();
396 ++stream_index) {
397 MediaStreamInterface* remote_stream = remote_streams->at(stream_index);
398 for (size_t track_index = 0;
399 track_index < remote_stream->GetVideoTracks().size();
400 ++track_index) {
401 const std::string id =
402 remote_stream->GetVideoTracks()[track_index]->id();
403 if (fake_video_renderers_.find(id) != fake_video_renderers_.end()) {
404 continue;
405 }
deadbeefc9be0072015-12-14 18:27:57 -0800406 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
407 remote_stream->GetVideoTracks()[track_index]));
deadbeeffaac4972015-11-12 15:33:07 -0800408 }
409 }
410 }
411
deadbeefaf1b59c2015-10-15 12:08:41 -0700412 void SetVideoConstraints(const webrtc::FakeConstraints& video_constraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413 video_constraints_ = video_constraint;
414 }
415
416 void AddMediaStream(bool audio, bool video) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700417 std::string stream_label =
418 kStreamLabelBase +
419 rtc::ToString<int>(static_cast<int>(pc()->local_streams()->count()));
deadbeeffaac4972015-11-12 15:33:07 -0800420 rtc::scoped_refptr<MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000421 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422
423 if (audio && can_receive_audio()) {
deadbeeffac06552015-11-25 11:26:01 -0800424 stream->AddTrack(CreateLocalAudioTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425 }
426 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000427 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428 }
429
deadbeefaf1b59c2015-10-15 12:08:41 -0700430 EXPECT_TRUE(pc()->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000431 }
432
deadbeefaf1b59c2015-10-15 12:08:41 -0700433 size_t NumberOfLocalMediaStreams() { return pc()->local_streams()->count(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434
435 bool SessionActive() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700436 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437 }
438
deadbeeffaac4972015-11-12 15:33:07 -0800439 // Automatically add a stream when receiving an offer, if we don't have one.
440 // Defaults to true.
441 void set_auto_add_stream(bool auto_add_stream) {
442 auto_add_stream_ = auto_add_stream;
443 }
444
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445 void set_signaling_message_receiver(
deadbeefaf1b59c2015-10-15 12:08:41 -0700446 SignalingMessageReceiver* signaling_message_receiver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000447 signaling_message_receiver_ = signaling_message_receiver;
448 }
449
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700450 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
451
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452 void EnableVideoDecoderFactory() {
453 video_decoder_factory_enabled_ = true;
454 fake_video_decoder_factory_->AddSupportedVideoCodecType(
455 webrtc::kVideoCodecVP8);
456 }
457
deadbeefaf1b59c2015-10-15 12:08:41 -0700458 void IceRestart() {
htaaac2dea2016-03-10 13:35:55 -0800459 offer_answer_constraints_.SetMandatoryIceRestart(true);
460 offer_answer_options_.ice_restart = true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700461 SetExpectIceRestart(true);
462 }
463
464 void SetExpectIceRestart(bool expect_restart) {
465 expect_ice_restart_ = expect_restart;
466 }
467
468 bool ExpectIceRestart() const { return expect_ice_restart_; }
469
Honghai Zhang4cedf2b2016-08-31 08:18:11 -0700470 void SetExpectIceRenomination(bool expect_renomination) {
471 expect_ice_renomination_ = expect_renomination;
472 }
473 void SetExpectRemoteIceRenomination(bool expect_renomination) {
474 expect_remote_ice_renomination_ = expect_renomination;
475 }
476 bool ExpectIceRenomination() { return expect_ice_renomination_; }
477 bool ExpectRemoteIceRenomination() { return expect_remote_ice_renomination_; }
478
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700479 // The below 3 methods assume streams will be offered.
480 // Thus they'll only set the "offer to receive" flag to true if it's
481 // currently false, not if it's just unset.
deadbeefaf1b59c2015-10-15 12:08:41 -0700482 void SetReceiveAudioVideo(bool audio, bool video) {
483 SetReceiveAudio(audio);
484 SetReceiveVideo(video);
485 ASSERT_EQ(audio, can_receive_audio());
486 ASSERT_EQ(video, can_receive_video());
487 }
488
489 void SetReceiveAudio(bool audio) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700490 if (audio && can_receive_audio()) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700491 return;
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700492 }
htaaac2dea2016-03-10 13:35:55 -0800493 offer_answer_constraints_.SetMandatoryReceiveAudio(audio);
494 offer_answer_options_.offer_to_receive_audio = audio ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700495 }
496
497 void SetReceiveVideo(bool video) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700498 if (video && can_receive_video()) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700499 return;
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700500 }
501 offer_answer_constraints_.SetMandatoryReceiveVideo(video);
502 offer_answer_options_.offer_to_receive_video = video ? 1 : 0;
503 }
504
505 void SetOfferToReceiveAudioVideo(bool audio, bool video) {
506 offer_answer_constraints_.SetMandatoryReceiveAudio(audio);
507 offer_answer_options_.offer_to_receive_audio = audio ? 1 : 0;
htaaac2dea2016-03-10 13:35:55 -0800508 offer_answer_constraints_.SetMandatoryReceiveVideo(video);
509 offer_answer_options_.offer_to_receive_video = video ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700510 }
511
512 void RemoveMsidFromReceivedSdp(bool remove) { remove_msid_ = remove; }
513
514 void RemoveSdesCryptoFromReceivedSdp(bool remove) { remove_sdes_ = remove; }
515
516 void RemoveBundleFromReceivedSdp(bool remove) { remove_bundle_ = remove; }
517
perkjcaafdba2016-03-20 07:34:29 -0700518 void RemoveCvoFromReceivedSdp(bool remove) { remove_cvo_ = remove; }
519
deadbeefaf1b59c2015-10-15 12:08:41 -0700520 bool can_receive_audio() {
521 bool value;
htaaac2dea2016-03-10 13:35:55 -0800522 if (prefer_constraint_apis_) {
523 if (webrtc::FindConstraint(
524 &offer_answer_constraints_,
525 MediaConstraintsInterface::kOfferToReceiveAudio, &value,
526 nullptr)) {
527 return value;
528 }
529 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700530 }
htaaac2dea2016-03-10 13:35:55 -0800531 return offer_answer_options_.offer_to_receive_audio > 0 ||
532 offer_answer_options_.offer_to_receive_audio ==
533 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700534 }
535
536 bool can_receive_video() {
537 bool value;
htaaac2dea2016-03-10 13:35:55 -0800538 if (prefer_constraint_apis_) {
539 if (webrtc::FindConstraint(
540 &offer_answer_constraints_,
541 MediaConstraintsInterface::kOfferToReceiveVideo, &value,
542 nullptr)) {
543 return value;
544 }
545 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700546 }
htaaac2dea2016-03-10 13:35:55 -0800547 return offer_answer_options_.offer_to_receive_video > 0 ||
548 offer_answer_options_.offer_to_receive_video ==
549 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700550 }
551
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700552 void OnDataChannel(
553 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
deadbeefaf1b59c2015-10-15 12:08:41 -0700554 LOG(INFO) << id_ << "OnDataChannel";
555 data_channel_ = data_channel;
556 data_observer_.reset(new MockDataChannelObserver(data_channel));
557 }
558
zhihuang9763d562016-08-05 11:14:50 -0700559 void CreateDataChannel() { CreateDataChannel(nullptr); }
560
561 void CreateDataChannel(const webrtc::DataChannelInit* init) {
562 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, init);
deadbeefaf1b59c2015-10-15 12:08:41 -0700563 ASSERT_TRUE(data_channel_.get() != nullptr);
564 data_observer_.reset(new MockDataChannelObserver(data_channel_));
565 }
566
deadbeeffac06552015-11-25 11:26:01 -0800567 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack(
568 const std::string& stream_label) {
569 FakeConstraints constraints;
570 // Disable highpass filter so that we can get all the test audio frames.
571 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
572 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
573 peer_connection_factory_->CreateAudioSource(&constraints);
574 // TODO(perkj): Test audio source when it is implemented. Currently audio
575 // always use the default input.
576 std::string label = stream_label + kAudioTrackLabelBase;
577 return peer_connection_factory_->CreateAudioTrack(label, source);
578 }
579
580 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack(
581 const std::string& stream_label) {
582 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
583 FakeConstraints source_constraints = video_constraints_;
584 source_constraints.SetMandatoryMaxFrameRate(10);
585
586 cricket::FakeVideoCapturer* fake_capturer =
587 new webrtc::FakePeriodicVideoCapturer();
perkjcaafdba2016-03-20 07:34:29 -0700588 fake_capturer->SetRotation(capture_rotation_);
deadbeeffac06552015-11-25 11:26:01 -0800589 video_capturers_.push_back(fake_capturer);
perkja3ede6c2016-03-08 01:27:48 +0100590 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
deadbeeffac06552015-11-25 11:26:01 -0800591 peer_connection_factory_->CreateVideoSource(fake_capturer,
592 &source_constraints);
593 std::string label = stream_label + kVideoTrackLabelBase;
perkjcaafdba2016-03-20 07:34:29 -0700594
595 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
596 peer_connection_factory_->CreateVideoTrack(label, source));
597 if (!local_video_renderer_) {
598 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
599 }
600 return track;
deadbeeffac06552015-11-25 11:26:01 -0800601 }
602
deadbeefaf1b59c2015-10-15 12:08:41 -0700603 DataChannelInterface* data_channel() { return data_channel_; }
604 const MockDataChannelObserver* data_observer() const {
605 return data_observer_.get();
606 }
607
hta6b4f8392016-03-10 00:24:31 -0800608 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
deadbeefaf1b59c2015-10-15 12:08:41 -0700609
610 void StopVideoCapturers() {
perkjcaafdba2016-03-20 07:34:29 -0700611 for (auto* capturer : video_capturers_) {
612 capturer->Stop();
deadbeefaf1b59c2015-10-15 12:08:41 -0700613 }
614 }
615
perkjcaafdba2016-03-20 07:34:29 -0700616 void SetCaptureRotation(webrtc::VideoRotation rotation) {
617 ASSERT_TRUE(video_capturers_.empty());
618 capture_rotation_ = rotation;
619 }
620
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621 bool AudioFramesReceivedCheck(int number_of_frames) const {
622 return number_of_frames <= fake_audio_capture_module_->frames_received();
623 }
624
deadbeefc9be0072015-12-14 18:27:57 -0800625 int audio_frames_received() const {
626 return fake_audio_capture_module_->frames_received();
627 }
628
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 bool VideoFramesReceivedCheck(int number_of_frames) {
630 if (video_decoder_factory_enabled_) {
631 const std::vector<FakeWebRtcVideoDecoder*>& decoders
632 = fake_video_decoder_factory_->decoders();
633 if (decoders.empty()) {
634 return number_of_frames <= 0;
635 }
hta6b4f8392016-03-10 00:24:31 -0800636 // Note - this checks that EACH decoder has the requisite number
637 // of frames. The video_frames_received() function sums them.
deadbeefc9be0072015-12-14 18:27:57 -0800638 for (FakeWebRtcVideoDecoder* decoder : decoders) {
639 if (number_of_frames > decoder->GetNumFramesReceived()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640 return false;
641 }
642 }
643 return true;
644 } else {
645 if (fake_video_renderers_.empty()) {
646 return number_of_frames <= 0;
647 }
648
deadbeefc9be0072015-12-14 18:27:57 -0800649 for (const auto& pair : fake_video_renderers_) {
650 if (number_of_frames > pair.second->num_rendered_frames()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651 return false;
652 }
653 }
654 return true;
655 }
656 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700657
deadbeefc9be0072015-12-14 18:27:57 -0800658 int video_frames_received() const {
659 int total = 0;
660 if (video_decoder_factory_enabled_) {
661 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
662 fake_video_decoder_factory_->decoders();
663 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
664 total += decoder->GetNumFramesReceived();
665 }
666 } else {
667 for (const auto& pair : fake_video_renderers_) {
668 total += pair.second->num_rendered_frames();
669 }
670 for (const auto& renderer : removed_fake_video_renderers_) {
671 total += renderer->num_rendered_frames();
672 }
673 }
674 return total;
675 }
676
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677 // Verify the CreateDtmfSender interface
678 void VerifyDtmf() {
kwibergd1fe2812016-04-27 06:47:29 -0700679 std::unique_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000680 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681
682 // We can't create a DTMF sender with an invalid audio track or a non local
683 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700684 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000685 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700686 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
687 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000688
689 // We should be able to create a DTMF sender from a local track.
690 webrtc::AudioTrackInterface* localtrack =
691 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
692 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700693 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 dtmf_sender->RegisterObserver(observer.get());
695
696 // Test the DtmfSender object just created.
697 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
698 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
699
700 // We don't need to verify that the DTMF tones are actually sent out because
701 // that is already covered by the tests of the lower level components.
702
703 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
704 std::vector<std::string> tones;
705 tones.push_back("1");
706 tones.push_back("a");
707 tones.push_back("");
708 observer->Verify(tones);
709
710 dtmf_sender->UnregisterObserver();
711 }
712
713 // Verifies that the SessionDescription have rejected the appropriate media
714 // content.
715 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700716 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
717 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 const cricket::SessionDescription* remote_desc =
719 peer_connection_->remote_description()->description();
720 const cricket::SessionDescription* local_desc =
721 peer_connection_->local_description()->description();
722
723 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
724 if (remote_audio_content) {
725 const ContentInfo* audio_content =
726 GetFirstAudioContent(local_desc);
727 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
728 }
729
730 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
731 if (remote_video_content) {
732 const ContentInfo* video_content =
733 GetFirstVideoContent(local_desc);
734 EXPECT_EQ(can_receive_video(), !video_content->rejected);
735 }
736 }
737
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000738 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700739 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740 const cricket::SessionDescription* desc =
741 peer_connection_->local_description()->description();
742 const cricket::ContentInfos& contents = desc->contents();
743
744 for (size_t index = 0; index < contents.size(); ++index) {
745 if (contents[index].rejected)
746 continue;
747 const cricket::TransportDescription* transport_desc =
748 desc->GetTransportDescriptionByName(contents[index].name);
749
750 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000751 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752 if (ufragpair_it == ice_ufrag_pwd_.end()) {
753 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000754 ice_ufrag_pwd_[static_cast<int>(index)] =
755 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 } else if (ExpectIceRestart()) {
757 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
758 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
759 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
760 } else {
761 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
762 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
763 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
764 }
765 }
766 }
767
Honghai Zhang4cedf2b2016-08-31 08:18:11 -0700768 void VerifyLocalIceRenomination() {
769 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
770 const cricket::SessionDescription* desc =
771 peer_connection_->local_description()->description();
772 const cricket::ContentInfos& contents = desc->contents();
773
774 for (auto content : contents) {
775 if (content.rejected)
776 continue;
777 const cricket::TransportDescription* transport_desc =
778 desc->GetTransportDescriptionByName(content.name);
779 const auto& options = transport_desc->transport_options;
780 auto iter = std::find(options.begin(), options.end(),
781 cricket::ICE_RENOMINATION_STR);
782 EXPECT_EQ(ExpectIceRenomination(), iter != options.end());
783 }
784 }
785
786 void VerifyRemoteIceRenomination() {
787 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
788 const cricket::SessionDescription* desc =
789 peer_connection_->remote_description()->description();
790 const cricket::ContentInfos& contents = desc->contents();
791
792 for (auto content : contents) {
793 if (content.rejected)
794 continue;
795 const cricket::TransportDescription* transport_desc =
796 desc->GetTransportDescriptionByName(content.name);
797 const auto& options = transport_desc->transport_options;
798 auto iter = std::find(options.begin(), options.end(),
799 cricket::ICE_RENOMINATION_STR);
800 EXPECT_EQ(ExpectRemoteIceRenomination(), iter != options.end());
801 }
802 }
803
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000805 rtc::scoped_refptr<MockStatsObserver>
806 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000807 EXPECT_TRUE(peer_connection_->GetStats(
808 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700810 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 return observer->AudioOutputLevel();
812 }
813
814 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000815 rtc::scoped_refptr<MockStatsObserver>
816 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000817 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700818 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700820 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 return observer->AudioInputLevel();
822 }
823
824 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000825 rtc::scoped_refptr<MockStatsObserver>
826 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000827 EXPECT_TRUE(peer_connection_->GetStats(
828 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700830 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000831 return observer->BytesReceived();
832 }
833
834 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000835 rtc::scoped_refptr<MockStatsObserver>
836 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000837 EXPECT_TRUE(peer_connection_->GetStats(
838 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700840 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 return observer->BytesSent();
842 }
843
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000844 int GetAvailableReceivedBandwidthStats() {
845 rtc::scoped_refptr<MockStatsObserver>
846 observer(new rtc::RefCountedObject<MockStatsObserver>());
847 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700848 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000849 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700850 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000851 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000852 return bw;
853 }
854
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000855 std::string GetDtlsCipherStats() {
856 rtc::scoped_refptr<MockStatsObserver>
857 observer(new rtc::RefCountedObject<MockStatsObserver>());
858 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700859 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000860 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700861 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000862 return observer->DtlsCipher();
863 }
864
865 std::string GetSrtpCipherStats() {
866 rtc::scoped_refptr<MockStatsObserver>
867 observer(new rtc::RefCountedObject<MockStatsObserver>());
868 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700869 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000870 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700871 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000872 return observer->SrtpCipher();
873 }
874
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 int rendered_width() {
876 EXPECT_FALSE(fake_video_renderers_.empty());
877 return fake_video_renderers_.empty() ? 1 :
878 fake_video_renderers_.begin()->second->width();
879 }
880
881 int rendered_height() {
882 EXPECT_FALSE(fake_video_renderers_.empty());
883 return fake_video_renderers_.empty() ? 1 :
884 fake_video_renderers_.begin()->second->height();
885 }
886
perkjcaafdba2016-03-20 07:34:29 -0700887 webrtc::VideoRotation rendered_rotation() {
888 EXPECT_FALSE(fake_video_renderers_.empty());
889 return fake_video_renderers_.empty()
890 ? webrtc::kVideoRotation_0
891 : fake_video_renderers_.begin()->second->rotation();
892 }
893
894 int local_rendered_width() {
895 return local_video_renderer_ ? local_video_renderer_->width() : 1;
896 }
897
898 int local_rendered_height() {
899 return local_video_renderer_ ? local_video_renderer_->height() : 1;
900 }
901
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 size_t number_of_remote_streams() {
903 if (!pc())
904 return 0;
905 return pc()->remote_streams()->count();
906 }
907
hta6b4f8392016-03-10 00:24:31 -0800908 StreamCollectionInterface* remote_streams() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 if (!pc()) {
910 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700911 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 }
913 return pc()->remote_streams();
914 }
915
916 StreamCollectionInterface* local_streams() {
917 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()->local_streams();
922 }
923
hta6b4f8392016-03-10 00:24:31 -0800924 bool HasLocalAudioTrack() { return StreamsHaveAudioTrack(local_streams()); }
925
926 bool HasLocalVideoTrack() { return StreamsHaveVideoTrack(local_streams()); }
927
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
929 return pc()->signaling_state();
930 }
931
932 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
933 return pc()->ice_connection_state();
934 }
935
936 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
937 return pc()->ice_gathering_state();
938 }
939
zhihuang184a3fd2016-06-14 11:47:14 -0700940 std::vector<std::unique_ptr<MockRtpReceiverObserver>> const&
941 rtp_receiver_observers() {
942 return rtp_receiver_observers_;
943 }
944
945 void SetRtpReceiverObservers() {
946 rtp_receiver_observers_.clear();
947 for (auto receiver : pc()->GetReceivers()) {
948 std::unique_ptr<MockRtpReceiverObserver> observer(
949 new MockRtpReceiverObserver(receiver->media_type()));
950 receiver->SetObserver(observer.get());
951 rtp_receiver_observers_.push_back(std::move(observer));
952 }
953 }
954
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955 private:
956 class DummyDtmfObserver : public DtmfSenderObserverInterface {
957 public:
958 DummyDtmfObserver() : completed_(false) {}
959
960 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700961 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962 tones_.push_back(tone);
963 if (tone.empty()) {
964 completed_ = true;
965 }
966 }
967
968 void Verify(const std::vector<std::string>& tones) const {
969 ASSERT_TRUE(tones_.size() == tones.size());
970 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
971 }
972
973 bool completed() const { return completed_; }
974
975 private:
976 bool completed_;
977 std::vector<std::string> tones_;
978 };
979
deadbeefaf1b59c2015-10-15 12:08:41 -0700980 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
981
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800982 bool Init(
983 const MediaConstraintsInterface* constraints,
984 const PeerConnectionFactory::Options* options,
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700985 const PeerConnectionInterface::RTCConfiguration* config,
Henrik Boströmd79599d2016-06-01 13:58:50 +0200986 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
perkj8aba9972016-04-10 23:54:34 -0700987 bool prefer_constraint_apis,
danilchape9021a32016-05-17 01:52:02 -0700988 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700989 rtc::Thread* worker_thread) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700990 EXPECT_TRUE(!peer_connection_);
991 EXPECT_TRUE(!peer_connection_factory_);
htaaac2dea2016-03-10 13:35:55 -0800992 if (!prefer_constraint_apis) {
993 EXPECT_TRUE(!constraints);
994 }
995 prefer_constraint_apis_ = prefer_constraint_apis;
996
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700997 fake_network_manager_.reset(new rtc::FakeNetworkManager());
998 fake_network_manager_->AddInterface(rtc::SocketAddress("192.168.1.1", 0));
999
kwibergd1fe2812016-04-27 06:47:29 -07001000 std::unique_ptr<cricket::PortAllocator> port_allocator(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001001 new cricket::BasicPortAllocator(fake_network_manager_.get()));
deadbeefaf1b59c2015-10-15 12:08:41 -07001002 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
1003
1004 if (fake_audio_capture_module_ == nullptr) {
1005 return false;
1006 }
1007 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
1008 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
danilchape9021a32016-05-17 01:52:02 -07001009 rtc::Thread* const signaling_thread = rtc::Thread::Current();
deadbeefaf1b59c2015-10-15 12:08:41 -07001010 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -07001011 network_thread, worker_thread, signaling_thread,
1012 fake_audio_capture_module_, fake_video_encoder_factory_,
1013 fake_video_decoder_factory_);
deadbeefaf1b59c2015-10-15 12:08:41 -07001014 if (!peer_connection_factory_) {
1015 return false;
1016 }
1017 if (options) {
1018 peer_connection_factory_->SetOptions(*options);
1019 }
zhihuang9763d562016-08-05 11:14:50 -07001020 peer_connection_ =
1021 CreatePeerConnection(std::move(port_allocator), constraints, config,
1022 std::move(cert_generator));
deadbeefaf1b59c2015-10-15 12:08:41 -07001023 return peer_connection_.get() != nullptr;
1024 }
1025
deadbeefaf1b59c2015-10-15 12:08:41 -07001026 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
kwibergd1fe2812016-04-27 06:47:29 -07001027 std::unique_ptr<cricket::PortAllocator> port_allocator,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001028 const MediaConstraintsInterface* constraints,
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001029 const PeerConnectionInterface::RTCConfiguration* config,
Henrik Boströmd79599d2016-06-01 13:58:50 +02001030 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001031 // CreatePeerConnection with RTCConfiguration.
1032 PeerConnectionInterface::RTCConfiguration default_config;
1033
1034 if (!config) {
1035 config = &default_config;
1036 }
1037
Henrik Boströmd79599d2016-06-01 13:58:50 +02001038 return peer_connection_factory_->CreatePeerConnection(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001039 *config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +02001040 std::move(cert_generator), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041 }
1042
1043 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001044 LOG(INFO) << id_ << "HandleIncomingOffer ";
deadbeeffaac4972015-11-12 15:33:07 -08001045 if (NumberOfLocalMediaStreams() == 0 && auto_add_stream_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 // If we are not sending any streams ourselves it is time to add some.
1047 AddMediaStream(true, true);
1048 }
kwibergd1fe2812016-04-27 06:47:29 -07001049 std::unique_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -07001050 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
zhihuang184a3fd2016-06-14 11:47:14 -07001052 // Set the RtpReceiverObserver after receivers are created.
1053 SetRtpReceiverObservers();
kwibergd1fe2812016-04-27 06:47:29 -07001054 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -07001055 EXPECT_TRUE(DoCreateAnswer(&answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 std::string sdp;
1057 EXPECT_TRUE(answer->ToString(&sdp));
1058 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001059 SendSdpMessage(webrtc::SessionDescriptionInterface::kAnswer, sdp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001060 }
1061
1062 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001063 LOG(INFO) << id_ << "HandleIncomingAnswer";
kwibergd1fe2812016-04-27 06:47:29 -07001064 std::unique_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -07001065 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001066 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
zhihuang184a3fd2016-06-14 11:47:14 -07001067 // Set the RtpReceiverObserver after receivers are created.
1068 SetRtpReceiverObservers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001069 }
1070
kwibergd1fe2812016-04-27 06:47:29 -07001071 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001072 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001073 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
1074 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001075 MockCreateSessionDescriptionObserver>());
htaaac2dea2016-03-10 13:35:55 -08001076 if (prefer_constraint_apis_) {
1077 if (offer) {
1078 pc()->CreateOffer(observer, &offer_answer_constraints_);
1079 } else {
1080 pc()->CreateAnswer(observer, &offer_answer_constraints_);
1081 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001082 } else {
htaaac2dea2016-03-10 13:35:55 -08001083 if (offer) {
1084 pc()->CreateOffer(observer, offer_answer_options_);
1085 } else {
1086 pc()->CreateAnswer(observer, offer_answer_options_);
1087 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001088 }
1089 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
kwiberg2bbff992016-03-16 11:03:04 -07001090 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001091 if (observer->result() && ExpectIceRestart()) {
1092 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
1093 }
1094 return observer->result();
1095 }
1096
kwibergd1fe2812016-04-27 06:47:29 -07001097 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098 return DoCreateOfferAnswer(desc, true);
1099 }
1100
kwibergd1fe2812016-04-27 06:47:29 -07001101 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001102 return DoCreateOfferAnswer(desc, false);
1103 }
1104
1105 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001106 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
1107 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001108 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -07001109 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001110 pc()->SetLocalDescription(observer, desc);
1111 // Ignore the observer result. If we wait for the result with
1112 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
1113 // before the offer which is an error.
1114 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001115 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001116 // ProcessMessages waits at least 1ms but processes all messages before
1117 // returning. Since this test is synchronous and send messages to the remote
1118 // peer whenever a callback is invoked, this can lead to messages being
1119 // sent to the remote peer in the wrong order.
1120 // TODO(perkj): Find a way to check the result without risking that the
1121 // order of sent messages are changed. Ex- by posting all messages that are
1122 // sent to the remote peer.
1123 return true;
1124 }
1125
1126 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001127 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
1128 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -07001130 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001131 pc()->SetRemoteDescription(observer, desc);
1132 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
1133 return observer->result();
1134 }
1135
1136 // This modifies all received SDP messages before they are processed.
1137 void FilterIncomingSdpMessage(std::string* sdp) {
1138 if (remove_msid_) {
1139 const char kSdpSsrcAttribute[] = "a=ssrc:";
1140 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
1141 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
1142 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
1143 }
1144 if (remove_bundle_) {
1145 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
1146 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
1147 }
1148 if (remove_sdes_) {
1149 const char kSdpSdesCryptoAttribute[] = "a=crypto";
1150 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
1151 }
perkjcaafdba2016-03-20 07:34:29 -07001152 if (remove_cvo_) {
1153 const char kSdpCvoExtenstion[] = "urn:3gpp:video-orientation";
1154 RemoveLinesFromSdp(kSdpCvoExtenstion, sdp);
1155 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001156 }
1157
deadbeefaf1b59c2015-10-15 12:08:41 -07001158 std::string id_;
1159
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001160 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
1161
deadbeefaf1b59c2015-10-15 12:08:41 -07001162 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
1163 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
1164 peer_connection_factory_;
1165
htaaac2dea2016-03-10 13:35:55 -08001166 bool prefer_constraint_apis_ = true;
deadbeeffaac4972015-11-12 15:33:07 -08001167 bool auto_add_stream_ = true;
1168
deadbeefaf1b59c2015-10-15 12:08:41 -07001169 typedef std::pair<std::string, std::string> IceUfragPwdPair;
1170 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
1171 bool expect_ice_restart_ = false;
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07001172 bool expect_ice_renomination_ = false;
1173 bool expect_remote_ice_renomination_ = false;
deadbeefaf1b59c2015-10-15 12:08:41 -07001174
deadbeefc9be0072015-12-14 18:27:57 -08001175 // Needed to keep track of number of frames sent.
deadbeefaf1b59c2015-10-15 12:08:41 -07001176 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
1177 // Needed to keep track of number of frames received.
kwibergd1fe2812016-04-27 06:47:29 -07001178 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
deadbeefc9be0072015-12-14 18:27:57 -08001179 fake_video_renderers_;
1180 // Needed to ensure frames aren't received for removed tracks.
kwibergd1fe2812016-04-27 06:47:29 -07001181 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
deadbeefc9be0072015-12-14 18:27:57 -08001182 removed_fake_video_renderers_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001183 // Needed to keep track of number of frames received when external decoder
1184 // used.
1185 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
1186 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
1187 bool video_decoder_factory_enabled_ = false;
1188 webrtc::FakeConstraints video_constraints_;
1189
1190 // For remote peer communication.
1191 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001192 int signaling_delay_ms_ = 0;
deadbeefaf1b59c2015-10-15 12:08:41 -07001193
1194 // Store references to the video capturers we've created, so that we can stop
1195 // them, if required.
perkjcaafdba2016-03-20 07:34:29 -07001196 std::vector<cricket::FakeVideoCapturer*> video_capturers_;
1197 webrtc::VideoRotation capture_rotation_ = webrtc::kVideoRotation_0;
1198 // |local_video_renderer_| attached to the first created local video track.
kwibergd1fe2812016-04-27 06:47:29 -07001199 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001200
htaaac2dea2016-03-10 13:35:55 -08001201 webrtc::FakeConstraints offer_answer_constraints_;
1202 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001203 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
1204 bool remove_bundle_ =
1205 false; // True if bundle should be removed in received SDP.
1206 bool remove_sdes_ =
1207 false; // True if a=crypto should be removed in received SDP.
perkjcaafdba2016-03-20 07:34:29 -07001208 // |remove_cvo_| is true if extension urn:3gpp:video-orientation should be
1209 // removed in the received SDP.
1210 bool remove_cvo_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001212 rtc::scoped_refptr<DataChannelInterface> data_channel_;
kwibergd1fe2812016-04-27 06:47:29 -07001213 std::unique_ptr<MockDataChannelObserver> data_observer_;
zhihuang184a3fd2016-06-14 11:47:14 -07001214
1215 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001216};
1217
deadbeef7c73bdb2015-12-10 15:10:44 -08001218class P2PTestConductor : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001219 public:
deadbeef7c73bdb2015-12-10 15:10:44 -08001220 P2PTestConductor()
deadbeefeff5b852016-05-27 14:18:01 -07001221 : pss_(new rtc::PhysicalSocketServer),
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001222 ss_(new rtc::VirtualSocketServer(pss_.get())),
deadbeefeff5b852016-05-27 14:18:01 -07001223 network_thread_(new rtc::Thread(ss_.get())),
1224 worker_thread_(rtc::Thread::Create()) {
danilchape9021a32016-05-17 01:52:02 -07001225 RTC_CHECK(network_thread_->Start());
1226 RTC_CHECK(worker_thread_->Start());
perkj8aba9972016-04-10 23:54:34 -07001227 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001228
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001229 bool SessionActive() {
1230 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001231 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001232 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001233
hta6b4f8392016-03-10 00:24:31 -08001234 // Return true if the number of frames provided have been received
1235 // on the video and audio tracks provided.
1236 bool FramesHaveArrived(int audio_frames_to_receive,
1237 int video_frames_to_receive) {
1238 bool all_good = true;
1239 if (initiating_client_->HasLocalAudioTrack() &&
1240 receiving_client_->can_receive_audio()) {
1241 all_good &=
1242 receiving_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1243 }
1244 if (initiating_client_->HasLocalVideoTrack() &&
1245 receiving_client_->can_receive_video()) {
1246 all_good &=
1247 receiving_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1248 }
1249 if (receiving_client_->HasLocalAudioTrack() &&
1250 initiating_client_->can_receive_audio()) {
1251 all_good &=
1252 initiating_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1253 }
1254 if (receiving_client_->HasLocalVideoTrack() &&
1255 initiating_client_->can_receive_video()) {
1256 all_good &=
1257 initiating_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1258 }
1259 return all_good;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001260 }
hta6b4f8392016-03-10 00:24:31 -08001261
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262 void VerifyDtmf() {
1263 initiating_client_->VerifyDtmf();
1264 receiving_client_->VerifyDtmf();
1265 }
1266
1267 void TestUpdateOfferWithRejectedContent() {
deadbeefc9be0072015-12-14 18:27:57 -08001268 // Renegotiate, rejecting the video m-line.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001269 initiating_client_->Negotiate(true, false);
deadbeefc9be0072015-12-14 18:27:57 -08001270 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1271
1272 int pc1_audio_received = initiating_client_->audio_frames_received();
1273 int pc1_video_received = initiating_client_->video_frames_received();
1274 int pc2_audio_received = receiving_client_->audio_frames_received();
1275 int pc2_video_received = receiving_client_->video_frames_received();
1276
1277 // Wait for some additional audio frames to be received.
1278 EXPECT_TRUE_WAIT(initiating_client_->AudioFramesReceivedCheck(
1279 pc1_audio_received + kEndAudioFrameCount) &&
1280 receiving_client_->AudioFramesReceivedCheck(
1281 pc2_audio_received + kEndAudioFrameCount),
1282 kMaxWaitForFramesMs);
1283
1284 // During this time, we shouldn't have received any additional video frames
1285 // for the rejected video tracks.
1286 EXPECT_EQ(pc1_video_received, initiating_client_->video_frames_received());
1287 EXPECT_EQ(pc2_video_received, receiving_client_->video_frames_received());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288 }
1289
deadbeefc23efae2016-12-02 15:45:31 -08001290 void VerifyRenderedAspectRatio(int width, int height) {
1291 VerifyRenderedAspectRatio(width, height, webrtc::kVideoRotation_0);
perkjcaafdba2016-03-20 07:34:29 -07001292 }
1293
deadbeefc23efae2016-12-02 15:45:31 -08001294 void VerifyRenderedAspectRatio(int width,
1295 int height,
1296 webrtc::VideoRotation rotation) {
deadbeefb4659802016-12-01 16:23:28 -08001297 double expected_aspect_ratio = static_cast<double>(width) / height;
1298 double receiving_client_rendered_aspect_ratio =
1299 static_cast<double>(receiving_client()->rendered_width()) /
1300 receiving_client()->rendered_height();
1301 double initializing_client_rendered_aspect_ratio =
1302 static_cast<double>(initializing_client()->rendered_width()) /
1303 initializing_client()->rendered_height();
deadbeefc23efae2016-12-02 15:45:31 -08001304 double initializing_client_local_rendered_aspect_ratio =
1305 static_cast<double>(initializing_client()->local_rendered_width()) /
1306 initializing_client()->local_rendered_height();
1307 // Verify end-to-end rendered aspect ratio.
deadbeefb4659802016-12-01 16:23:28 -08001308 EXPECT_EQ(expected_aspect_ratio, receiving_client_rendered_aspect_ratio);
1309 EXPECT_EQ(expected_aspect_ratio, initializing_client_rendered_aspect_ratio);
deadbeefc23efae2016-12-02 15:45:31 -08001310 // Verify aspect ratio of the local preview.
1311 EXPECT_EQ(expected_aspect_ratio,
1312 initializing_client_local_rendered_aspect_ratio);
deadbeefb4659802016-12-01 16:23:28 -08001313
deadbeefc23efae2016-12-02 15:45:31 -08001314 // Verify rotation.
perkjcaafdba2016-03-20 07:34:29 -07001315 EXPECT_EQ(rotation, receiving_client()->rendered_rotation());
perkjcaafdba2016-03-20 07:34:29 -07001316 EXPECT_EQ(rotation, initializing_client()->rendered_rotation());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317 }
1318
1319 void VerifySessionDescriptions() {
1320 initiating_client_->VerifyRejectedMediaInSessionDescription();
1321 receiving_client_->VerifyRejectedMediaInSessionDescription();
1322 initiating_client_->VerifyLocalIceUfragAndPassword();
1323 receiving_client_->VerifyLocalIceUfragAndPassword();
1324 }
1325
deadbeef7c73bdb2015-12-10 15:10:44 -08001326 ~P2PTestConductor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001327 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001328 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001329 }
1330 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001331 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001333 }
1334
deadbeefaf1b59c2015-10-15 12:08:41 -07001335 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001336
1337 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1338 MediaConstraintsInterface* recv_constraints) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001339 return CreateTestClients(init_constraints, nullptr, nullptr,
1340 recv_constraints, nullptr, nullptr);
1341 }
1342
1343 bool CreateTestClients(
1344 const PeerConnectionInterface::RTCConfiguration& init_config,
1345 const PeerConnectionInterface::RTCConfiguration& recv_config) {
1346 return CreateTestClients(nullptr, nullptr, &init_config, nullptr, nullptr,
1347 &recv_config);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001348 }
1349
htaaac2dea2016-03-10 13:35:55 -08001350 bool CreateTestClientsThatPreferNoConstraints() {
1351 initiating_client_.reset(
perkj8aba9972016-04-10 23:54:34 -07001352 PeerConnectionTestClient::CreateClientPreferNoConstraints(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001353 "Caller: ", nullptr, network_thread_.get(), worker_thread_.get()));
htaaac2dea2016-03-10 13:35:55 -08001354 receiving_client_.reset(
perkj8aba9972016-04-10 23:54:34 -07001355 PeerConnectionTestClient::CreateClientPreferNoConstraints(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001356 "Callee: ", nullptr, network_thread_.get(), worker_thread_.get()));
htaaac2dea2016-03-10 13:35:55 -08001357 if (!initiating_client_ || !receiving_client_) {
1358 return false;
1359 }
1360 // Remember the choice for possible later resets of the clients.
1361 prefer_constraint_apis_ = false;
1362 SetSignalingReceivers();
1363 return true;
1364 }
1365
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001366 bool CreateTestClients(
1367 MediaConstraintsInterface* init_constraints,
1368 PeerConnectionFactory::Options* init_options,
1369 const PeerConnectionInterface::RTCConfiguration* init_config,
1370 MediaConstraintsInterface* recv_constraints,
1371 PeerConnectionFactory::Options* recv_options,
1372 const PeerConnectionInterface::RTCConfiguration* recv_config) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001373 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001374 "Caller: ", init_constraints, init_options, init_config,
zhihuang9763d562016-08-05 11:14:50 -07001375 network_thread_.get(), worker_thread_.get()));
deadbeefaf1b59c2015-10-15 12:08:41 -07001376 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001377 "Callee: ", recv_constraints, recv_options, recv_config,
zhihuang9763d562016-08-05 11:14:50 -07001378 network_thread_.get(), worker_thread_.get()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001379 if (!initiating_client_ || !receiving_client_) {
1380 return false;
1381 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001382 SetSignalingReceivers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001383 return true;
1384 }
1385
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001386 void SetSignalingReceivers() {
1387 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
1388 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1389 }
1390
1391 void SetSignalingDelayMs(int delay_ms) {
1392 initiating_client_->set_signaling_delay_ms(delay_ms);
1393 receiving_client_->set_signaling_delay_ms(delay_ms);
1394 }
1395
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001396 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1397 const webrtc::FakeConstraints& recv_constraints) {
1398 initiating_client_->SetVideoConstraints(init_constraints);
1399 receiving_client_->SetVideoConstraints(recv_constraints);
1400 }
1401
perkjcaafdba2016-03-20 07:34:29 -07001402 void SetCaptureRotation(webrtc::VideoRotation rotation) {
1403 initiating_client_->SetCaptureRotation(rotation);
1404 receiving_client_->SetCaptureRotation(rotation);
1405 }
1406
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001407 void EnableVideoDecoderFactory() {
1408 initiating_client_->EnableVideoDecoderFactory();
1409 receiving_client_->EnableVideoDecoderFactory();
1410 }
1411
1412 // This test sets up a call between two parties. Both parties send static
1413 // frames to each other. Once the test is finished the number of sent frames
1414 // is compared to the number of received frames.
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -07001415 void LocalP2PTest() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001416 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1417 initiating_client_->AddMediaStream(true, true);
1418 }
1419 initiating_client_->Negotiate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001420 // Assert true is used here since next tests are guaranteed to fail and
1421 // would eat up 5 seconds.
1422 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1423 VerifySessionDescriptions();
1424
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001425 int audio_frame_count = kEndAudioFrameCount;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001426 int video_frame_count = kEndVideoFrameCount;
hta6b4f8392016-03-10 00:24:31 -08001427 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1428
1429 if ((!initiating_client_->can_receive_audio() &&
1430 !initiating_client_->can_receive_video()) ||
1431 (!receiving_client_->can_receive_audio() &&
1432 !receiving_client_->can_receive_video())) {
1433 // Neither audio nor video will flow, so connections won't be
1434 // established. There's nothing more to check.
1435 // TODO(hta): Check connection if there's a data channel.
1436 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001437 }
1438
hta6b4f8392016-03-10 00:24:31 -08001439 // Audio or video is expected to flow, so both clients should reach the
1440 // Connected state, and the offerer (ICE controller) should proceed to
1441 // Completed.
1442 // Note: These tests have been observed to fail under heavy load at
1443 // shorter timeouts, so they may be flaky.
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -07001444 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1445 initiating_client_->ice_connection_state(),
1446 kMaxWaitForFramesMs);
hta6b4f8392016-03-10 00:24:31 -08001447 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1448 receiving_client_->ice_connection_state(),
1449 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001450
hta6b4f8392016-03-10 00:24:31 -08001451 // The ICE gathering state should end up in kIceGatheringComplete,
1452 // but there's a bug that prevents this at the moment, and the state
1453 // machine is being updated by the WEBRTC WG.
1454 // TODO(hta): Update this check when spec revisions finish.
1455 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1456 initiating_client_->ice_gathering_state());
1457 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1458 receiving_client_->ice_gathering_state(),
1459 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001460
hta6b4f8392016-03-10 00:24:31 -08001461 // Check that the expected number of frames have arrived.
1462 EXPECT_TRUE_WAIT(FramesHaveArrived(audio_frame_count, video_frame_count),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001463 kMaxWaitForFramesMs);
1464 }
1465
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001466 void SetupAndVerifyDtlsCall() {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001467 FakeConstraints setup_constraints;
1468 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1469 true);
kthelgason876222f2016-11-29 01:44:11 -08001470 // Disable resolution adaptation, we don't want it interfering with the
1471 // test results.
1472 webrtc::PeerConnectionInterface::RTCConfiguration rtc_config;
1473 rtc_config.set_cpu_adaptation(false);
1474
1475 ASSERT_TRUE(CreateTestClients(&setup_constraints, nullptr, &rtc_config,
1476 &setup_constraints, nullptr, &rtc_config));
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001477 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001478 VerifyRenderedAspectRatio(640, 480);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001479 }
1480
1481 PeerConnectionTestClient* CreateDtlsClientWithAlternateKey() {
1482 FakeConstraints setup_constraints;
1483 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1484 true);
kthelgason876222f2016-11-29 01:44:11 -08001485 // Disable resolution adaptation, we don't want it interfering with the
1486 // test results.
1487 webrtc::PeerConnectionInterface::RTCConfiguration rtc_config;
1488 rtc_config.set_cpu_adaptation(false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001489
Henrik Boströmd79599d2016-06-01 13:58:50 +02001490 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
deadbeefeaa826c2017-01-20 15:15:58 -08001491 new FakeRTCCertificateGenerator());
Henrik Boströmd79599d2016-06-01 13:58:50 +02001492 cert_generator->use_alternate_key();
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001493
1494 // Make sure the new client is using a different certificate.
1495 return PeerConnectionTestClient::CreateClientWithDtlsIdentityStore(
kthelgason876222f2016-11-29 01:44:11 -08001496 "New Peer: ", &setup_constraints, nullptr, &rtc_config,
Henrik Boströmd79599d2016-06-01 13:58:50 +02001497 std::move(cert_generator), prefer_constraint_apis_,
danilchape9021a32016-05-17 01:52:02 -07001498 network_thread_.get(), worker_thread_.get());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001499 }
1500
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001501 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1502 // Messages may get lost on the unreliable DataChannel, so we send multiple
1503 // times to avoid test flakiness.
1504 static const size_t kSendAttempts = 5;
1505
1506 for (size_t i = 0; i < kSendAttempts; ++i) {
1507 dc->Send(DataBuffer(data));
1508 }
1509 }
1510
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001511 rtc::Thread* network_thread() { return network_thread_.get(); }
1512
Taylor Brandstetter9b5306c2016-08-18 11:40:37 -07001513 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1514
deadbeefaf1b59c2015-10-15 12:08:41 -07001515 PeerConnectionTestClient* initializing_client() {
1516 return initiating_client_.get();
1517 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001518
1519 // Set the |initiating_client_| to the |client| passed in and return the
1520 // original |initiating_client_|.
1521 PeerConnectionTestClient* set_initializing_client(
1522 PeerConnectionTestClient* client) {
1523 PeerConnectionTestClient* old = initiating_client_.release();
1524 initiating_client_.reset(client);
1525 return old;
1526 }
1527
deadbeefaf1b59c2015-10-15 12:08:41 -07001528 PeerConnectionTestClient* receiving_client() {
1529 return receiving_client_.get();
1530 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001531
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001532 // Set the |receiving_client_| to the |client| passed in and return the
1533 // original |receiving_client_|.
1534 PeerConnectionTestClient* set_receiving_client(
1535 PeerConnectionTestClient* client) {
1536 PeerConnectionTestClient* old = receiving_client_.release();
1537 receiving_client_.reset(client);
1538 return old;
1539 }
1540
zhihuang184a3fd2016-06-14 11:47:14 -07001541 bool AllObserversReceived(
1542 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>& observers) {
1543 for (auto& observer : observers) {
1544 if (!observer->first_packet_received()) {
1545 return false;
1546 }
1547 }
1548 return true;
1549 }
1550
jbauchcb560652016-08-04 05:20:32 -07001551 void TestGcmNegotiation(bool local_gcm_enabled, bool remote_gcm_enabled,
1552 int expected_cipher_suite) {
1553 PeerConnectionFactory::Options init_options;
1554 init_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled;
1555 PeerConnectionFactory::Options recv_options;
1556 recv_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001557 ASSERT_TRUE(CreateTestClients(nullptr, &init_options, nullptr, nullptr,
1558 &recv_options, nullptr));
jbauchcb560652016-08-04 05:20:32 -07001559 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1560 init_observer =
1561 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1562 initializing_client()->pc()->RegisterUMAObserver(init_observer);
1563 LocalP2PTest();
1564
1565 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
1566 initializing_client()->GetSrtpCipherStats(),
1567 kMaxWaitMs);
1568 EXPECT_EQ(1,
1569 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1570 expected_cipher_suite));
1571 }
1572
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001573 private:
deadbeefeff5b852016-05-27 14:18:01 -07001574 // |ss_| is used by |network_thread_| so it must be destroyed later.
kwibergd1fe2812016-04-27 06:47:29 -07001575 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
1576 std::unique_ptr<rtc::VirtualSocketServer> ss_;
deadbeefeff5b852016-05-27 14:18:01 -07001577 // |network_thread_| and |worker_thread_| are used by both
1578 // |initiating_client_| and |receiving_client_| so they must be destroyed
1579 // later.
1580 std::unique_ptr<rtc::Thread> network_thread_;
1581 std::unique_ptr<rtc::Thread> worker_thread_;
kwibergd1fe2812016-04-27 06:47:29 -07001582 std::unique_ptr<PeerConnectionTestClient> initiating_client_;
1583 std::unique_ptr<PeerConnectionTestClient> receiving_client_;
htaaac2dea2016-03-10 13:35:55 -08001584 bool prefer_constraint_apis_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001585};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001587// Disable for TSan v2, see
1588// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1589#if !defined(THREAD_SANITIZER)
1590
zhihuang184a3fd2016-06-14 11:47:14 -07001591TEST_F(P2PTestConductor, TestRtpReceiverObserverCallbackFunction) {
1592 ASSERT_TRUE(CreateTestClients());
1593 LocalP2PTest();
1594 EXPECT_TRUE_WAIT(
1595 AllObserversReceived(initializing_client()->rtp_receiver_observers()),
1596 kMaxWaitForFramesMs);
1597 EXPECT_TRUE_WAIT(
1598 AllObserversReceived(receiving_client()->rtp_receiver_observers()),
1599 kMaxWaitForFramesMs);
1600}
1601
1602// The observers are expected to fire the signal even if they are set after the
1603// first packet is received.
1604TEST_F(P2PTestConductor, TestSetRtpReceiverObserverAfterFirstPacketIsReceived) {
1605 ASSERT_TRUE(CreateTestClients());
1606 LocalP2PTest();
1607 // Reset the RtpReceiverObservers.
1608 initializing_client()->SetRtpReceiverObservers();
1609 receiving_client()->SetRtpReceiverObservers();
1610 EXPECT_TRUE_WAIT(
1611 AllObserversReceived(initializing_client()->rtp_receiver_observers()),
1612 kMaxWaitForFramesMs);
1613 EXPECT_TRUE_WAIT(
1614 AllObserversReceived(receiving_client()->rtp_receiver_observers()),
1615 kMaxWaitForFramesMs);
1616}
1617
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001618// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001619// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1620// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001621TEST_F(P2PTestConductor, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001622 ASSERT_TRUE(CreateTestClients());
1623 LocalP2PTest();
1624 VerifyDtmf();
1625}
1626
1627// This test sets up a Jsep call between two parties and test that we can get a
1628// video aspect ratio of 16:9.
deadbeef7c73bdb2015-12-10 15:10:44 -08001629TEST_F(P2PTestConductor, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630 ASSERT_TRUE(CreateTestClients());
1631 FakeConstraints constraint;
1632 double requested_ratio = 640.0/360;
1633 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1634 SetVideoConstraints(constraint, constraint);
1635 LocalP2PTest();
1636
1637 ASSERT_LE(0, initializing_client()->rendered_height());
1638 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001639 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640 initializing_client()->rendered_height();
1641 EXPECT_LE(requested_ratio, initiating_video_ratio);
1642
1643 ASSERT_LE(0, receiving_client()->rendered_height());
1644 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001645 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646 receiving_client()->rendered_height();
1647 EXPECT_LE(requested_ratio, receiving_video_ratio);
1648}
1649
1650// This test sets up a Jsep call between two parties and test that the
1651// received video has a resolution of 1280*720.
1652// TODO(mallinath): Enable when
1653// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
deadbeef7c73bdb2015-12-10 15:10:44 -08001654TEST_F(P2PTestConductor, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 ASSERT_TRUE(CreateTestClients());
1656 FakeConstraints constraint;
1657 constraint.SetMandatoryMinWidth(1280);
1658 constraint.SetMandatoryMinHeight(720);
1659 SetVideoConstraints(constraint, constraint);
1660 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001661 VerifyRenderedAspectRatio(1280, 720);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001662}
1663
1664// This test sets up a call between two endpoints that are configured to use
1665// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001666TEST_F(P2PTestConductor, LocalP2PTestDtls) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001667 SetupAndVerifyDtlsCall();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668}
1669
hta6b4f8392016-03-10 00:24:31 -08001670// This test sets up an one-way call, with media only from initiator to
1671// responder.
1672TEST_F(P2PTestConductor, OneWayMediaCall) {
1673 ASSERT_TRUE(CreateTestClients());
1674 receiving_client()->set_auto_add_stream(false);
1675 LocalP2PTest();
1676}
1677
htaaac2dea2016-03-10 13:35:55 -08001678TEST_F(P2PTestConductor, OneWayMediaCallWithoutConstraints) {
1679 ASSERT_TRUE(CreateTestClientsThatPreferNoConstraints());
1680 receiving_client()->set_auto_add_stream(false);
1681 LocalP2PTest();
1682}
1683
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001684// This test sets up a audio call initially and then upgrades to audio/video,
1685// using DTLS.
deadbeef7c73bdb2015-12-10 15:10:44 -08001686TEST_F(P2PTestConductor, LocalP2PTestDtlsRenegotiate) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001687 FakeConstraints setup_constraints;
1688 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1689 true);
1690 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1691 receiving_client()->SetReceiveAudioVideo(true, false);
1692 LocalP2PTest();
1693 receiving_client()->SetReceiveAudioVideo(true, true);
1694 receiving_client()->Negotiate();
1695}
1696
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001697// This test sets up a call transfer to a new caller with a different DTLS
1698// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001699TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCallee) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001700 SetupAndVerifyDtlsCall();
1701
1702 // Keeping the original peer around which will still send packets to the
1703 // receiving client. These SRTP packets will be dropped.
kwibergd1fe2812016-04-27 06:47:29 -07001704 std::unique_ptr<PeerConnectionTestClient> original_peer(
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001705 set_initializing_client(CreateDtlsClientWithAlternateKey()));
1706 original_peer->pc()->Close();
1707
1708 SetSignalingReceivers();
1709 receiving_client()->SetExpectIceRestart(true);
1710 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001711 VerifyRenderedAspectRatio(640, 480);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001712}
1713
guoweis46383312015-12-17 16:45:59 -08001714// This test sets up a non-bundle call and apply bundle during ICE restart. When
1715// bundle is in effect in the restart, the channel can successfully reset its
1716// DTLS-SRTP context.
deadbeef4c6696c2016-12-02 18:08:57 -08001717TEST_F(P2PTestConductor, LocalP2PTestDtlsBundleInIceRestart) {
guoweis46383312015-12-17 16:45:59 -08001718 FakeConstraints setup_constraints;
1719 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1720 true);
1721 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1722 receiving_client()->RemoveBundleFromReceivedSdp(true);
1723 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001724 VerifyRenderedAspectRatio(640, 480);
guoweis46383312015-12-17 16:45:59 -08001725
1726 initializing_client()->IceRestart();
1727 receiving_client()->SetExpectIceRestart(true);
1728 receiving_client()->RemoveBundleFromReceivedSdp(false);
1729 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001730 VerifyRenderedAspectRatio(640, 480);
guoweis46383312015-12-17 16:45:59 -08001731}
1732
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001733// This test sets up a call transfer to a new callee with a different DTLS
1734// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001735TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCaller) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001736 SetupAndVerifyDtlsCall();
1737
1738 // Keeping the original peer around which will still send packets to the
1739 // receiving client. These SRTP packets will be dropped.
kwibergd1fe2812016-04-27 06:47:29 -07001740 std::unique_ptr<PeerConnectionTestClient> original_peer(
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001741 set_receiving_client(CreateDtlsClientWithAlternateKey()));
1742 original_peer->pc()->Close();
1743
1744 SetSignalingReceivers();
1745 initializing_client()->IceRestart();
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -07001746 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001747 VerifyRenderedAspectRatio(640, 480);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001748}
1749
perkjcaafdba2016-03-20 07:34:29 -07001750TEST_F(P2PTestConductor, LocalP2PTestCVO) {
1751 ASSERT_TRUE(CreateTestClients());
1752 SetCaptureRotation(webrtc::kVideoRotation_90);
1753 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001754 VerifyRenderedAspectRatio(640, 480, webrtc::kVideoRotation_90);
perkjcaafdba2016-03-20 07:34:29 -07001755}
1756
1757TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportCVO) {
1758 ASSERT_TRUE(CreateTestClients());
1759 SetCaptureRotation(webrtc::kVideoRotation_90);
1760 receiving_client()->RemoveCvoFromReceivedSdp(true);
1761 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001762 VerifyRenderedAspectRatio(480, 640, webrtc::kVideoRotation_0);
perkjcaafdba2016-03-20 07:34:29 -07001763}
1764
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765// This test sets up a call between two endpoints that are configured to use
1766// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1767// negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001768TEST_F(P2PTestConductor, LocalP2PTestOfferDtlsButNotSdes) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769 FakeConstraints setup_constraints;
1770 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1771 true);
1772 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1773 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1774 LocalP2PTest();
deadbeefc23efae2016-12-02 15:45:31 -08001775 VerifyRenderedAspectRatio(640, 480);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776}
1777
deadbeef40610e22016-12-22 10:53:38 -08001778#ifdef HAVE_SCTP
zhihuangaf388472016-11-02 16:49:48 -07001779// This test verifies that the negotiation will succeed with data channel only
1780// in max-bundle mode.
1781TEST_F(P2PTestConductor, LocalP2PTestOfferDataChannelOnly) {
1782 webrtc::PeerConnectionInterface::RTCConfiguration rtc_config;
1783 rtc_config.bundle_policy =
1784 webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle;
1785 ASSERT_TRUE(CreateTestClients(rtc_config, rtc_config));
1786 initializing_client()->CreateDataChannel();
1787 initializing_client()->Negotiate();
1788}
deadbeef40610e22016-12-22 10:53:38 -08001789#endif
zhihuangaf388472016-11-02 16:49:48 -07001790
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791// This test sets up a Jsep call between two parties, and the callee only
1792// accept to receive video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001793TEST_F(P2PTestConductor, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794 ASSERT_TRUE(CreateTestClients());
1795 receiving_client()->SetReceiveAudioVideo(false, true);
1796 LocalP2PTest();
1797}
1798
1799// This test sets up a Jsep call between two parties, and the callee only
1800// accept to receive audio.
deadbeef7c73bdb2015-12-10 15:10:44 -08001801TEST_F(P2PTestConductor, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001802 ASSERT_TRUE(CreateTestClients());
1803 receiving_client()->SetReceiveAudioVideo(true, false);
1804 LocalP2PTest();
1805}
1806
1807// This test sets up a Jsep call between two parties, and the callee reject both
1808// audio and video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001809TEST_F(P2PTestConductor, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001810 ASSERT_TRUE(CreateTestClients());
1811 receiving_client()->SetReceiveAudioVideo(false, false);
1812 LocalP2PTest();
1813}
1814
1815// This test sets up an audio and video call between two parties. After the call
1816// runs for a while (10 frames), the caller sends an update offer with video
1817// being rejected. Once the re-negotiation is done, the video flow should stop
1818// and the audio flow should continue.
deadbeefc9be0072015-12-14 18:27:57 -08001819TEST_F(P2PTestConductor, UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001820 ASSERT_TRUE(CreateTestClients());
1821 LocalP2PTest();
1822 TestUpdateOfferWithRejectedContent();
1823}
1824
1825// This test sets up a Jsep call between two parties. The MSID is removed from
1826// the SDP strings from the caller.
deadbeefc9be0072015-12-14 18:27:57 -08001827TEST_F(P2PTestConductor, LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001828 ASSERT_TRUE(CreateTestClients());
1829 receiving_client()->RemoveMsidFromReceivedSdp(true);
1830 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1831 // audio and video is muxed when MSID is disabled. Remove
1832 // SetRemoveBundleFromSdp once
1833 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1834 receiving_client()->RemoveBundleFromReceivedSdp(true);
1835 LocalP2PTest();
1836}
1837
kthelgasone2397792016-11-08 08:19:48 -08001838TEST_F(P2PTestConductor, LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839 ASSERT_TRUE(CreateTestClients());
1840 // Set optional video constraint to max 320pixels to decrease CPU usage.
1841 FakeConstraints constraint;
1842 constraint.SetOptionalMaxWidth(320);
1843 SetVideoConstraints(constraint, constraint);
1844 initializing_client()->AddMediaStream(true, true);
1845 initializing_client()->AddMediaStream(false, true);
1846 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1847 LocalP2PTest();
1848 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1849}
1850
1851// Test that we can receive the audio output level from a remote audio track.
deadbeef7c73bdb2015-12-10 15:10:44 -08001852TEST_F(P2PTestConductor, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 ASSERT_TRUE(CreateTestClients());
1854 LocalP2PTest();
1855
1856 StreamCollectionInterface* remote_streams =
1857 initializing_client()->remote_streams();
1858 ASSERT_GT(remote_streams->count(), 0u);
1859 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1860 MediaStreamTrackInterface* remote_audio_track =
1861 remote_streams->at(0)->GetAudioTracks()[0];
1862
1863 // Get the audio output level stats. Note that the level is not available
1864 // until a RTCP packet has been received.
1865 EXPECT_TRUE_WAIT(
1866 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1867 kMaxWaitForStatsMs);
1868}
1869
1870// Test that an audio input level is reported.
deadbeef7c73bdb2015-12-10 15:10:44 -08001871TEST_F(P2PTestConductor, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001872 ASSERT_TRUE(CreateTestClients());
1873 LocalP2PTest();
1874
1875 // Get the audio input level stats. The level should be available very
1876 // soon after the test starts.
1877 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1878 kMaxWaitForStatsMs);
1879}
1880
1881// Test that we can get incoming byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001882TEST_F(P2PTestConductor, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001883 ASSERT_TRUE(CreateTestClients());
1884 LocalP2PTest();
1885
1886 StreamCollectionInterface* remote_streams =
1887 initializing_client()->remote_streams();
1888 ASSERT_GT(remote_streams->count(), 0u);
1889 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1890 MediaStreamTrackInterface* remote_audio_track =
1891 remote_streams->at(0)->GetAudioTracks()[0];
1892 EXPECT_TRUE_WAIT(
1893 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1894 kMaxWaitForStatsMs);
1895
1896 MediaStreamTrackInterface* remote_video_track =
1897 remote_streams->at(0)->GetVideoTracks()[0];
1898 EXPECT_TRUE_WAIT(
1899 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1900 kMaxWaitForStatsMs);
1901}
1902
1903// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001904TEST_F(P2PTestConductor, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001905 ASSERT_TRUE(CreateTestClients());
1906 LocalP2PTest();
1907
1908 StreamCollectionInterface* local_streams =
1909 initializing_client()->local_streams();
1910 ASSERT_GT(local_streams->count(), 0u);
1911 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1912 MediaStreamTrackInterface* local_audio_track =
1913 local_streams->at(0)->GetAudioTracks()[0];
1914 EXPECT_TRUE_WAIT(
1915 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1916 kMaxWaitForStatsMs);
1917
1918 MediaStreamTrackInterface* local_video_track =
1919 local_streams->at(0)->GetVideoTracks()[0];
1920 EXPECT_TRUE_WAIT(
1921 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1922 kMaxWaitForStatsMs);
1923}
1924
Joachim Bauch04e5b492015-05-29 09:40:39 +02001925// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001926TEST_F(P2PTestConductor, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001927 PeerConnectionFactory::Options init_options;
1928 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1929 PeerConnectionFactory::Options recv_options;
1930 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001931 ASSERT_TRUE(CreateTestClients(nullptr, &init_options, nullptr, nullptr,
1932 &recv_options, nullptr));
jbauchac8869e2015-07-03 01:36:14 -07001933 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1934 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1935 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001936 LocalP2PTest();
1937
torbjorng43166b82016-03-11 00:06:47 -08001938 EXPECT_TRUE_WAIT(
1939 rtc::SSLStreamAdapter::IsAcceptableCipher(
1940 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1941 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001942 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001943 initializing_client()->GetSrtpCipherStats(),
1944 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001945 EXPECT_EQ(1,
1946 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1947 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001948}
1949
1950// Test that DTLS 1.2 is used if both ends support it.
torbjorng79a5a832016-01-15 07:16:51 -08001951TEST_F(P2PTestConductor, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001952 PeerConnectionFactory::Options init_options;
1953 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1954 PeerConnectionFactory::Options recv_options;
1955 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001956 ASSERT_TRUE(CreateTestClients(nullptr, &init_options, nullptr, nullptr,
1957 &recv_options, nullptr));
jbauchac8869e2015-07-03 01:36:14 -07001958 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1959 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1960 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001961 LocalP2PTest();
1962
torbjorng43166b82016-03-11 00:06:47 -08001963 EXPECT_TRUE_WAIT(
1964 rtc::SSLStreamAdapter::IsAcceptableCipher(
1965 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1966 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001967 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001968 initializing_client()->GetSrtpCipherStats(),
1969 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001970 EXPECT_EQ(1,
1971 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1972 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001973}
1974
1975// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1976// received supports 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001977TEST_F(P2PTestConductor, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001978 PeerConnectionFactory::Options init_options;
1979 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1980 PeerConnectionFactory::Options recv_options;
1981 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001982 ASSERT_TRUE(CreateTestClients(nullptr, &init_options, nullptr, nullptr,
1983 &recv_options, nullptr));
jbauchac8869e2015-07-03 01:36:14 -07001984 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1985 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1986 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001987 LocalP2PTest();
1988
torbjorng43166b82016-03-11 00:06:47 -08001989 EXPECT_TRUE_WAIT(
1990 rtc::SSLStreamAdapter::IsAcceptableCipher(
1991 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1992 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001993 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001994 initializing_client()->GetSrtpCipherStats(),
1995 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001996 EXPECT_EQ(1,
1997 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1998 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001999}
2000
2001// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
2002// received supports 1.2.
torbjorng43166b82016-03-11 00:06:47 -08002003TEST_F(P2PTestConductor, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02002004 PeerConnectionFactory::Options init_options;
2005 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2006 PeerConnectionFactory::Options recv_options;
2007 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002008 ASSERT_TRUE(CreateTestClients(nullptr, &init_options, nullptr, nullptr,
2009 &recv_options, nullptr));
jbauchac8869e2015-07-03 01:36:14 -07002010 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
2011 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2012 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02002013 LocalP2PTest();
2014
torbjorng43166b82016-03-11 00:06:47 -08002015 EXPECT_TRUE_WAIT(
2016 rtc::SSLStreamAdapter::IsAcceptableCipher(
2017 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
2018 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002019 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002020 initializing_client()->GetSrtpCipherStats(),
2021 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002022 EXPECT_EQ(1,
2023 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2024 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00002025}
2026
jbauchcb560652016-08-04 05:20:32 -07002027// Test that a non-GCM cipher is used if both sides only support non-GCM.
2028TEST_F(P2PTestConductor, GetGcmNone) {
2029 TestGcmNegotiation(false, false, kDefaultSrtpCryptoSuite);
2030}
2031
2032// Test that a GCM cipher is used if both ends support it.
2033TEST_F(P2PTestConductor, GetGcmBoth) {
2034 TestGcmNegotiation(true, true, kDefaultSrtpCryptoSuiteGcm);
2035}
2036
2037// Test that GCM isn't used if only the initiator supports it.
2038TEST_F(P2PTestConductor, GetGcmInit) {
2039 TestGcmNegotiation(true, false, kDefaultSrtpCryptoSuite);
2040}
2041
2042// Test that GCM isn't used if only the receiver supports it.
2043TEST_F(P2PTestConductor, GetGcmRecv) {
2044 TestGcmNegotiation(false, true, kDefaultSrtpCryptoSuite);
2045}
2046
deadbeefb5cb19b2015-11-23 16:39:12 -08002047// This test sets up a call between two parties with audio, video and an RTP
2048// data channel.
deadbeef8f89bff2016-12-01 12:54:20 -08002049TEST_F(P2PTestConductor, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002050 FakeConstraints setup_constraints;
2051 setup_constraints.SetAllowRtpDataChannels();
2052 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
2053 initializing_client()->CreateDataChannel();
2054 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07002055 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
2056 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002057 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2058 kMaxWaitMs);
2059 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
2060 kMaxWaitMs);
2061
2062 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00002063
2064 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002065 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
2066 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00002067
2068 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002069 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
2070 kMaxWaitMs);
2071
2072 receiving_client()->data_channel()->Close();
2073 // Send new offer and answer.
2074 receiving_client()->Negotiate();
2075 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
2076 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
2077}
2078
deadbeef40610e22016-12-22 10:53:38 -08002079#ifdef HAVE_SCTP
deadbeefb5cb19b2015-11-23 16:39:12 -08002080// This test sets up a call between two parties with audio, video and an SCTP
2081// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08002082TEST_F(P2PTestConductor, LocalP2PTestSctpDataChannel) {
deadbeefb5cb19b2015-11-23 16:39:12 -08002083 ASSERT_TRUE(CreateTestClients());
2084 initializing_client()->CreateDataChannel();
2085 LocalP2PTest();
2086 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
2087 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
2088 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2089 kMaxWaitMs);
2090 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
2091
2092 std::string data = "hello world";
2093
2094 initializing_client()->data_channel()->Send(DataBuffer(data));
2095 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
2096 kMaxWaitMs);
2097
2098 receiving_client()->data_channel()->Send(DataBuffer(data));
2099 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
2100 kMaxWaitMs);
2101
2102 receiving_client()->data_channel()->Close();
deadbeef15887932015-12-14 19:32:34 -08002103 EXPECT_TRUE_WAIT(!initializing_client()->data_observer()->IsOpen(),
2104 kMaxWaitMs);
2105 EXPECT_TRUE_WAIT(!receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
deadbeefb5cb19b2015-11-23 16:39:12 -08002106}
2107
Taylor Brandstetter9b5306c2016-08-18 11:40:37 -07002108TEST_F(P2PTestConductor, UnorderedSctpDataChannel) {
2109 ASSERT_TRUE(CreateTestClients());
2110 webrtc::DataChannelInit init;
2111 init.ordered = false;
2112 initializing_client()->CreateDataChannel(&init);
2113
2114 // Introduce random network delays.
2115 // Otherwise it's not a true "unordered" test.
2116 virtual_socket_server()->set_delay_mean(20);
2117 virtual_socket_server()->set_delay_stddev(5);
2118 virtual_socket_server()->UpdateDelayDistribution();
2119
2120 initializing_client()->Negotiate();
2121 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
2122 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
2123 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2124 kMaxWaitMs);
2125 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
2126
2127 static constexpr int kNumMessages = 100;
2128 // Deliberately chosen to be larger than the MTU so messages get fragmented.
2129 static constexpr size_t kMaxMessageSize = 4096;
2130 // Create and send random messages.
2131 std::vector<std::string> sent_messages;
2132 for (int i = 0; i < kNumMessages; ++i) {
2133 size_t length = (rand() % kMaxMessageSize) + 1;
2134 std::string message;
2135 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
2136 initializing_client()->data_channel()->Send(DataBuffer(message));
2137 receiving_client()->data_channel()->Send(DataBuffer(message));
2138 sent_messages.push_back(message);
2139 }
2140
2141 EXPECT_EQ_WAIT(
2142 kNumMessages,
2143 initializing_client()->data_observer()->received_message_count(),
2144 kMaxWaitMs);
2145 EXPECT_EQ_WAIT(kNumMessages,
2146 receiving_client()->data_observer()->received_message_count(),
2147 kMaxWaitMs);
2148
2149 // Sort and compare to make sure none of the messages were corrupted.
2150 std::vector<std::string> initializing_client_received_messages =
2151 initializing_client()->data_observer()->messages();
2152 std::vector<std::string> receiving_client_received_messages =
2153 receiving_client()->data_observer()->messages();
2154 std::sort(sent_messages.begin(), sent_messages.end());
2155 std::sort(initializing_client_received_messages.begin(),
2156 initializing_client_received_messages.end());
2157 std::sort(receiving_client_received_messages.begin(),
2158 receiving_client_received_messages.end());
2159 EXPECT_EQ(sent_messages, initializing_client_received_messages);
2160 EXPECT_EQ(sent_messages, receiving_client_received_messages);
2161
2162 receiving_client()->data_channel()->Close();
2163 EXPECT_TRUE_WAIT(!initializing_client()->data_observer()->IsOpen(),
2164 kMaxWaitMs);
2165 EXPECT_TRUE_WAIT(!receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
2166}
deadbeef40610e22016-12-22 10:53:38 -08002167#endif // HAVE_SCTP
Taylor Brandstetter9b5306c2016-08-18 11:40:37 -07002168
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002169// This test sets up a call between two parties and creates a data channel.
2170// The test tests that received data is buffered unless an observer has been
2171// registered.
2172// Rtp data channels can receive data before the underlying
2173// transport has detected that a channel is writable and thus data can be
2174// received before the data channel state changes to open. That is hard to test
2175// but the same buffering is used in that case.
deadbeef7c73bdb2015-12-10 15:10:44 -08002176TEST_F(P2PTestConductor, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002177 FakeConstraints setup_constraints;
2178 setup_constraints.SetAllowRtpDataChannels();
2179 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
2180 initializing_client()->CreateDataChannel();
2181 initializing_client()->Negotiate();
2182
deadbeefaf1b59c2015-10-15 12:08:41 -07002183 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
2184 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002185 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2186 kMaxWaitMs);
2187 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
2188 receiving_client()->data_channel()->state(), kMaxWaitMs);
2189
2190 // Unregister the existing observer.
2191 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00002192
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002193 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00002194 SendRtpData(initializing_client()->data_channel(), data);
2195
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002196 // Wait a while to allow the sent data to arrive before an observer is
2197 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002198 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002199
2200 MockDataChannelObserver new_observer(receiving_client()->data_channel());
2201 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
2202}
2203
2204// This test sets up a call between two parties with audio, video and but only
2205// the initiating client support data.
deadbeef8f89bff2016-12-01 12:54:20 -08002206TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00002207 FakeConstraints setup_constraints_1;
2208 setup_constraints_1.SetAllowRtpDataChannels();
2209 // Must disable DTLS to make negotiation succeed.
2210 setup_constraints_1.SetMandatory(
2211 MediaConstraintsInterface::kEnableDtlsSrtp, false);
2212 FakeConstraints setup_constraints_2;
2213 setup_constraints_2.SetMandatory(
2214 MediaConstraintsInterface::kEnableDtlsSrtp, false);
2215 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002216 initializing_client()->CreateDataChannel();
2217 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07002218 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002219 EXPECT_FALSE(receiving_client()->data_channel());
2220 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
2221}
2222
2223// This test sets up a call between two parties with audio, video. When audio
2224// and video is setup and flowing and data channel is negotiated.
deadbeef8f89bff2016-12-01 12:54:20 -08002225TEST_F(P2PTestConductor, AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002226 FakeConstraints setup_constraints;
2227 setup_constraints.SetAllowRtpDataChannels();
2228 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
2229 LocalP2PTest();
2230 initializing_client()->CreateDataChannel();
2231 // Send new offer and answer.
2232 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07002233 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
2234 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002235 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2236 kMaxWaitMs);
2237 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
2238 kMaxWaitMs);
2239}
2240
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00002241// This test sets up a Jsep call with SCTP DataChannel and verifies the
2242// negotiation is completed without error.
2243#ifdef HAVE_SCTP
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002244TEST_F(P2PTestConductor, CreateOfferWithSctpDataChannel) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00002245 FakeConstraints constraints;
2246 constraints.SetMandatory(
2247 MediaConstraintsInterface::kEnableDtlsSrtp, true);
2248 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
2249 initializing_client()->CreateDataChannel();
2250 initializing_client()->Negotiate(false, false);
2251}
2252#endif
2253
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002254// This test sets up a call between two parties with audio, and video.
2255// During the call, the initializing side restart ice and the test verifies that
2256// new ice candidates are generated and audio and video still can flow.
deadbeef7c73bdb2015-12-10 15:10:44 -08002257TEST_F(P2PTestConductor, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002258 ASSERT_TRUE(CreateTestClients());
2259
2260 // Negotiate and wait for ice completion and make sure audio and video plays.
2261 LocalP2PTest();
2262
2263 // Create a SDP string of the first audio candidate for both clients.
2264 const webrtc::IceCandidateCollection* audio_candidates_initiator =
2265 initializing_client()->pc()->local_description()->candidates(0);
2266 const webrtc::IceCandidateCollection* audio_candidates_receiver =
2267 receiving_client()->pc()->local_description()->candidates(0);
2268 ASSERT_GT(audio_candidates_initiator->count(), 0u);
2269 ASSERT_GT(audio_candidates_receiver->count(), 0u);
2270 std::string initiator_candidate;
2271 EXPECT_TRUE(
2272 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
2273 std::string receiver_candidate;
2274 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
2275
2276 // Restart ice on the initializing client.
2277 receiving_client()->SetExpectIceRestart(true);
2278 initializing_client()->IceRestart();
2279
2280 // Negotiate and wait for ice completion again and make sure audio and video
2281 // plays.
2282 LocalP2PTest();
2283
2284 // Create a SDP string of the first audio candidate for both clients again.
2285 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
2286 initializing_client()->pc()->local_description()->candidates(0);
2287 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
2288 receiving_client()->pc()->local_description()->candidates(0);
2289 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
2290 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
2291 std::string initiator_candidate_restart;
2292 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
2293 &initiator_candidate_restart));
2294 std::string receiver_candidate_restart;
2295 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
2296 &receiver_candidate_restart));
2297
2298 // Verify that the first candidates in the local session descriptions has
2299 // changed.
2300 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
2301 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
2302}
2303
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07002304TEST_F(P2PTestConductor, IceRenominationDisabled) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002305 PeerConnectionInterface::RTCConfiguration config;
2306 config.enable_ice_renomination = false;
2307 ASSERT_TRUE(CreateTestClients(config, config));
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07002308 LocalP2PTest();
2309
2310 initializing_client()->VerifyLocalIceRenomination();
2311 receiving_client()->VerifyLocalIceRenomination();
2312 initializing_client()->VerifyRemoteIceRenomination();
2313 receiving_client()->VerifyRemoteIceRenomination();
2314}
2315
2316TEST_F(P2PTestConductor, IceRenominationEnabled) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002317 PeerConnectionInterface::RTCConfiguration config;
2318 config.enable_ice_renomination = true;
2319 ASSERT_TRUE(CreateTestClients(config, config));
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07002320 initializing_client()->SetExpectIceRenomination(true);
2321 initializing_client()->SetExpectRemoteIceRenomination(true);
2322 receiving_client()->SetExpectIceRenomination(true);
2323 receiving_client()->SetExpectRemoteIceRenomination(true);
2324 LocalP2PTest();
2325
2326 initializing_client()->VerifyLocalIceRenomination();
2327 receiving_client()->VerifyLocalIceRenomination();
2328 initializing_client()->VerifyRemoteIceRenomination();
2329 receiving_client()->VerifyRemoteIceRenomination();
2330}
2331
deadbeeffaac4972015-11-12 15:33:07 -08002332// This test sets up a call between two parties with audio, and video.
2333// It then renegotiates setting the video m-line to "port 0", then later
2334// renegotiates again, enabling video.
deadbeef7c73bdb2015-12-10 15:10:44 -08002335TEST_F(P2PTestConductor, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08002336 ASSERT_TRUE(CreateTestClients());
2337
2338 // Do initial negotiation. Will result in video and audio sendonly m-lines.
2339 receiving_client()->set_auto_add_stream(false);
2340 initializing_client()->AddMediaStream(true, true);
2341 initializing_client()->Negotiate();
2342
2343 // Negotiate again, disabling the video m-line (receiving client will
2344 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
2345 receiving_client()->SetReceiveVideo(false);
2346 initializing_client()->Negotiate();
2347
2348 // Enable video and do negotiation again, making sure video is received
2349 // end-to-end.
2350 receiving_client()->SetReceiveVideo(true);
2351 receiving_client()->AddMediaStream(true, true);
2352 LocalP2PTest();
2353}
2354
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002355// This test sets up a Jsep call between two parties with external
2356// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00002357// TODO(holmer): Disabled due to sometimes crashing on buildbots.
2358// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08002359TEST_F(P2PTestConductor, DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002360 ASSERT_TRUE(CreateTestClients());
2361 EnableVideoDecoderFactory();
2362 LocalP2PTest();
2363}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00002364
deadbeeffac06552015-11-25 11:26:01 -08002365// This tests that if we negotiate after calling CreateSender but before we
2366// have a track, then set a track later, frames from the newly-set track are
2367// received end-to-end.
deadbeef7c73bdb2015-12-10 15:10:44 -08002368TEST_F(P2PTestConductor, EarlyWarmupTest) {
deadbeeffac06552015-11-25 11:26:01 -08002369 ASSERT_TRUE(CreateTestClients());
deadbeefbd7d8f72015-12-18 16:58:44 -08002370 auto audio_sender =
2371 initializing_client()->pc()->CreateSender("audio", "stream_id");
2372 auto video_sender =
2373 initializing_client()->pc()->CreateSender("video", "stream_id");
deadbeeffac06552015-11-25 11:26:01 -08002374 initializing_client()->Negotiate();
2375 // Wait for ICE connection to complete, without any tracks.
2376 // Note that the receiving client WILL (in HandleIncomingOffer) create
2377 // tracks, so it's only the initiator here that's doing early warmup.
2378 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
2379 VerifySessionDescriptions();
2380 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2381 initializing_client()->ice_connection_state(),
2382 kMaxWaitForFramesMs);
2383 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2384 receiving_client()->ice_connection_state(),
2385 kMaxWaitForFramesMs);
2386 // Now set the tracks, and expect frames to immediately start flowing.
2387 EXPECT_TRUE(
2388 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
2389 EXPECT_TRUE(
2390 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
hta6b4f8392016-03-10 00:24:31 -08002391 EXPECT_TRUE_WAIT(FramesHaveArrived(kEndAudioFrameCount, kEndVideoFrameCount),
deadbeeffac06552015-11-25 11:26:01 -08002392 kMaxWaitForFramesMs);
2393}
2394
zhihuang9763d562016-08-05 11:14:50 -07002395#ifdef HAVE_QUIC
2396// This test sets up a call between two parties using QUIC instead of DTLS for
2397// audio and video, and a QUIC data channel.
2398TEST_F(P2PTestConductor, LocalP2PTestQuicDataChannel) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002399 PeerConnectionInterface::RTCConfiguration quic_config;
2400 quic_config.enable_quic = true;
2401 ASSERT_TRUE(CreateTestClients(quic_config, quic_config));
zhihuang9763d562016-08-05 11:14:50 -07002402 webrtc::DataChannelInit init;
2403 init.ordered = false;
2404 init.reliable = true;
2405 init.id = 1;
2406 initializing_client()->CreateDataChannel(&init);
2407 receiving_client()->CreateDataChannel(&init);
2408 LocalP2PTest();
2409 ASSERT_NE(nullptr, initializing_client()->data_channel());
2410 ASSERT_NE(nullptr, receiving_client()->data_channel());
2411 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2412 kMaxWaitMs);
2413 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
2414
2415 std::string data = "hello world";
2416
2417 initializing_client()->data_channel()->Send(DataBuffer(data));
2418 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
2419 kMaxWaitMs);
2420
2421 receiving_client()->data_channel()->Send(DataBuffer(data));
2422 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
2423 kMaxWaitMs);
2424}
2425
2426// Tests that negotiation of QUIC data channels is completed without error.
2427TEST_F(P2PTestConductor, NegotiateQuicDataChannel) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002428 PeerConnectionInterface::RTCConfiguration quic_config;
2429 quic_config.enable_quic = true;
2430 ASSERT_TRUE(CreateTestClients(quic_config, quic_config));
zhihuang9763d562016-08-05 11:14:50 -07002431 FakeConstraints constraints;
2432 constraints.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp, true);
2433 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
2434 webrtc::DataChannelInit init;
2435 init.ordered = false;
2436 init.reliable = true;
2437 init.id = 1;
2438 initializing_client()->CreateDataChannel(&init);
2439 initializing_client()->Negotiate(false, false);
2440}
2441
2442// This test sets up a JSEP call using QUIC. The callee only receives video.
2443TEST_F(P2PTestConductor, LocalP2PTestVideoOnlyWithQuic) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002444 PeerConnectionInterface::RTCConfiguration quic_config;
2445 quic_config.enable_quic = true;
2446 ASSERT_TRUE(CreateTestClients(quic_config, quic_config));
zhihuang9763d562016-08-05 11:14:50 -07002447 receiving_client()->SetReceiveAudioVideo(false, true);
2448 LocalP2PTest();
2449}
2450
2451// This test sets up a JSEP call using QUIC. The callee only receives audio.
2452TEST_F(P2PTestConductor, LocalP2PTestAudioOnlyWithQuic) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002453 PeerConnectionInterface::RTCConfiguration quic_config;
2454 quic_config.enable_quic = true;
2455 ASSERT_TRUE(CreateTestClients(quic_config, quic_config));
zhihuang9763d562016-08-05 11:14:50 -07002456 receiving_client()->SetReceiveAudioVideo(true, false);
2457 LocalP2PTest();
2458}
2459
2460// This test sets up a JSEP call using QUIC. The callee rejects both audio and
2461// video.
2462TEST_F(P2PTestConductor, LocalP2PTestNoVideoAudioWithQuic) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002463 PeerConnectionInterface::RTCConfiguration quic_config;
2464 quic_config.enable_quic = true;
2465 ASSERT_TRUE(CreateTestClients(quic_config, quic_config));
zhihuang9763d562016-08-05 11:14:50 -07002466 receiving_client()->SetReceiveAudioVideo(false, false);
2467 LocalP2PTest();
2468}
2469
2470#endif // HAVE_QUIC
2471
nissed98cf1f2016-04-22 07:27:36 -07002472TEST_F(P2PTestConductor, ForwardVideoOnlyStream) {
2473 ASSERT_TRUE(CreateTestClients());
2474 // One-way stream
2475 receiving_client()->set_auto_add_stream(false);
2476 // Video only, audio forwarding not expected to work.
2477 initializing_client()->AddMediaStream(false, true);
2478 initializing_client()->Negotiate();
2479
2480 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
2481 VerifySessionDescriptions();
2482
2483 ASSERT_TRUE(initializing_client()->can_receive_video());
2484 ASSERT_TRUE(receiving_client()->can_receive_video());
2485
2486 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2487 initializing_client()->ice_connection_state(),
2488 kMaxWaitForFramesMs);
2489 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2490 receiving_client()->ice_connection_state(),
2491 kMaxWaitForFramesMs);
2492
2493 ASSERT_TRUE(receiving_client()->remote_streams()->count() == 1);
2494
2495 // Echo the stream back.
2496 receiving_client()->pc()->AddStream(
2497 receiving_client()->remote_streams()->at(0));
2498 receiving_client()->Negotiate();
2499
2500 EXPECT_TRUE_WAIT(
2501 initializing_client()->VideoFramesReceivedCheck(kEndVideoFrameCount),
2502 kMaxWaitForFramesMs);
2503}
2504
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002505// Test that we achieve the expected end-to-end connection time, using a
2506// fake clock and simulated latency on the media and signaling paths.
2507// We use a TURN<->TURN connection because this is usually the quickest to
2508// set up initially, especially when we're confident the connection will work
2509// and can start sending media before we get a STUN response.
2510//
2511// With various optimizations enabled, here are the network delays we expect to
2512// be on the critical path:
2513// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
2514// signaling answer (with DTLS fingerprint).
2515// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
2516// using TURN<->TURN pair, and DTLS exchange is 4 packets,
2517// the first of which should have arrived before the answer.
2518TEST_F(P2PTestConductor, EndToEndConnectionTimeWithTurnTurnPair) {
2519 rtc::ScopedFakeClock fake_clock;
2520 // Some things use a time of "0" as a special value, so we need to start out
2521 // the fake clock at a nonzero time.
2522 // TODO(deadbeef): Fix this.
2523 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
2524
2525 static constexpr int media_hop_delay_ms = 50;
2526 static constexpr int signaling_trip_delay_ms = 500;
2527 // For explanation of these values, see comment above.
2528 static constexpr int required_media_hops = 9;
2529 static constexpr int required_signaling_trips = 2;
2530 // For internal delays (such as posting an event asychronously).
2531 static constexpr int allowed_internal_delay_ms = 20;
2532 static constexpr int total_connection_time_ms =
2533 media_hop_delay_ms * required_media_hops +
2534 signaling_trip_delay_ms * required_signaling_trips +
2535 allowed_internal_delay_ms;
2536
2537 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
2538 3478};
2539 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
2540 0};
2541 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
2542 3478};
2543 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
2544 0};
2545 cricket::TestTurnServer turn_server_1(network_thread(),
2546 turn_server_1_internal_address,
2547 turn_server_1_external_address);
2548 cricket::TestTurnServer turn_server_2(network_thread(),
2549 turn_server_2_internal_address,
2550 turn_server_2_external_address);
2551 // Bypass permission check on received packets so media can be sent before
2552 // the candidate is signaled.
2553 turn_server_1.set_enable_permission_checks(false);
2554 turn_server_2.set_enable_permission_checks(false);
2555
2556 PeerConnectionInterface::RTCConfiguration client_1_config;
2557 webrtc::PeerConnectionInterface::IceServer ice_server_1;
2558 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
2559 ice_server_1.username = "test";
2560 ice_server_1.password = "test";
2561 client_1_config.servers.push_back(ice_server_1);
2562 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2563 client_1_config.presume_writable_when_fully_relayed = true;
2564
2565 PeerConnectionInterface::RTCConfiguration client_2_config;
2566 webrtc::PeerConnectionInterface::IceServer ice_server_2;
2567 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
2568 ice_server_2.username = "test";
2569 ice_server_2.password = "test";
2570 client_2_config.servers.push_back(ice_server_2);
2571 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2572 client_2_config.presume_writable_when_fully_relayed = true;
2573
2574 ASSERT_TRUE(CreateTestClients(client_1_config, client_2_config));
2575 // Set up the simulated delays.
2576 SetSignalingDelayMs(signaling_trip_delay_ms);
2577 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
2578 virtual_socket_server()->UpdateDelayDistribution();
2579
2580 initializing_client()->SetOfferToReceiveAudioVideo(true, true);
2581 initializing_client()->Negotiate();
2582 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
2583 // are connected. This is an important distinction. Once we have separate ICE
2584 // and DTLS state, this check needs to use the DTLS state.
2585 EXPECT_TRUE_SIMULATED_WAIT(
2586 (receiving_client()->ice_connection_state() ==
2587 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
2588 receiving_client()->ice_connection_state() ==
2589 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
2590 (initializing_client()->ice_connection_state() ==
2591 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
2592 initializing_client()->ice_connection_state() ==
2593 webrtc::PeerConnectionInterface::kIceConnectionCompleted),
2594 total_connection_time_ms, fake_clock);
2595 // Need to free the clients here since they're using things we created on
2596 // the stack.
2597 delete set_initializing_client(nullptr);
2598 delete set_receiving_client(nullptr);
2599}
2600
deadbeef0a6c4ca2015-10-06 11:38:28 -07002601class IceServerParsingTest : public testing::Test {
2602 public:
2603 // Convenience for parsing a single URL.
2604 bool ParseUrl(const std::string& url) {
2605 return ParseUrl(url, std::string(), std::string());
2606 }
2607
deadbeef293e9262017-01-11 12:28:30 -08002608 bool ParseTurnUrl(const std::string& url) {
2609 return ParseUrl(url, "username", "password");
2610 }
2611
deadbeef0a6c4ca2015-10-06 11:38:28 -07002612 bool ParseUrl(const std::string& url,
2613 const std::string& username,
2614 const std::string& password) {
hnsl04833622017-01-09 08:35:45 -08002615 return ParseUrl(
2616 url, username, password,
2617 PeerConnectionInterface::TlsCertPolicy::kTlsCertPolicySecure);
2618 }
2619
2620 bool ParseUrl(const std::string& url,
2621 const std::string& username,
2622 const std::string& password,
2623 PeerConnectionInterface::TlsCertPolicy tls_certificate_policy) {
deadbeef0a6c4ca2015-10-06 11:38:28 -07002624 PeerConnectionInterface::IceServers servers;
2625 PeerConnectionInterface::IceServer server;
2626 server.urls.push_back(url);
2627 server.username = username;
2628 server.password = password;
hnsl04833622017-01-09 08:35:45 -08002629 server.tls_cert_policy = tls_certificate_policy;
deadbeef0a6c4ca2015-10-06 11:38:28 -07002630 servers.push_back(server);
deadbeef293e9262017-01-11 12:28:30 -08002631 return webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_) ==
2632 webrtc::RTCErrorType::NONE;
deadbeef0a6c4ca2015-10-06 11:38:28 -07002633 }
2634
2635 protected:
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002636 cricket::ServerAddresses stun_servers_;
2637 std::vector<cricket::RelayServerConfig> turn_servers_;
deadbeef0a6c4ca2015-10-06 11:38:28 -07002638};
2639
2640// Make sure all STUN/TURN prefixes are parsed correctly.
2641TEST_F(IceServerParsingTest, ParseStunPrefixes) {
2642 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002643 EXPECT_EQ(1U, stun_servers_.size());
2644 EXPECT_EQ(0U, turn_servers_.size());
2645 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002646
2647 EXPECT_TRUE(ParseUrl("stuns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002648 EXPECT_EQ(1U, stun_servers_.size());
2649 EXPECT_EQ(0U, turn_servers_.size());
2650 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002651
deadbeef293e9262017-01-11 12:28:30 -08002652 EXPECT_TRUE(ParseTurnUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002653 EXPECT_EQ(0U, stun_servers_.size());
2654 EXPECT_EQ(1U, turn_servers_.size());
hnsl277b2502016-12-13 05:17:23 -08002655 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002656 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002657
deadbeef293e9262017-01-11 12:28:30 -08002658 EXPECT_TRUE(ParseTurnUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002659 EXPECT_EQ(0U, stun_servers_.size());
2660 EXPECT_EQ(1U, turn_servers_.size());
hnsl277b2502016-12-13 05:17:23 -08002661 EXPECT_EQ(cricket::PROTO_TLS, turn_servers_[0].ports[0].proto);
hnsl04833622017-01-09 08:35:45 -08002662 EXPECT_TRUE(turn_servers_[0].tls_cert_policy ==
2663 cricket::TlsCertPolicy::TLS_CERT_POLICY_SECURE);
2664 turn_servers_.clear();
2665
2666 EXPECT_TRUE(ParseUrl(
deadbeef293e9262017-01-11 12:28:30 -08002667 "turns:hostname", "username", "password",
hnsl04833622017-01-09 08:35:45 -08002668 PeerConnectionInterface::TlsCertPolicy::kTlsCertPolicyInsecureNoCheck));
2669 EXPECT_EQ(0U, stun_servers_.size());
2670 EXPECT_EQ(1U, turn_servers_.size());
2671 EXPECT_TRUE(turn_servers_[0].tls_cert_policy ==
2672 cricket::TlsCertPolicy::TLS_CERT_POLICY_INSECURE_NO_CHECK);
2673 EXPECT_EQ(cricket::PROTO_TLS, turn_servers_[0].ports[0].proto);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002674 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002675
2676 // invalid prefixes
2677 EXPECT_FALSE(ParseUrl("stunn:hostname"));
2678 EXPECT_FALSE(ParseUrl(":hostname"));
2679 EXPECT_FALSE(ParseUrl(":"));
2680 EXPECT_FALSE(ParseUrl(""));
2681}
2682
2683TEST_F(IceServerParsingTest, VerifyDefaults) {
2684 // TURNS defaults
deadbeef293e9262017-01-11 12:28:30 -08002685 EXPECT_TRUE(ParseTurnUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002686 EXPECT_EQ(1U, turn_servers_.size());
2687 EXPECT_EQ(5349, turn_servers_[0].ports[0].address.port());
hnsl277b2502016-12-13 05:17:23 -08002688 EXPECT_EQ(cricket::PROTO_TLS, turn_servers_[0].ports[0].proto);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002689 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002690
2691 // TURN defaults
deadbeef293e9262017-01-11 12:28:30 -08002692 EXPECT_TRUE(ParseTurnUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002693 EXPECT_EQ(1U, turn_servers_.size());
2694 EXPECT_EQ(3478, turn_servers_[0].ports[0].address.port());
2695 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2696 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002697
2698 // STUN defaults
2699 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002700 EXPECT_EQ(1U, stun_servers_.size());
2701 EXPECT_EQ(3478, stun_servers_.begin()->port());
2702 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002703}
2704
2705// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
2706// can be parsed correctly.
2707TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
2708 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002709 EXPECT_EQ(1U, stun_servers_.size());
2710 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2711 EXPECT_EQ(1234, stun_servers_.begin()->port());
2712 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002713
2714 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002715 EXPECT_EQ(1U, stun_servers_.size());
2716 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2717 EXPECT_EQ(4321, stun_servers_.begin()->port());
2718 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002719
2720 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002721 EXPECT_EQ(1U, stun_servers_.size());
2722 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2723 EXPECT_EQ(9999, stun_servers_.begin()->port());
2724 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002725
2726 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002727 EXPECT_EQ(1U, stun_servers_.size());
2728 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2729 EXPECT_EQ(3478, stun_servers_.begin()->port());
2730 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002731
2732 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002733 EXPECT_EQ(1U, stun_servers_.size());
2734 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2735 EXPECT_EQ(3478, stun_servers_.begin()->port());
2736 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002737
2738 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002739 EXPECT_EQ(1U, stun_servers_.size());
2740 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2741 EXPECT_EQ(3478, stun_servers_.begin()->port());
2742 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002743
2744 // Try some invalid hostname:port strings.
2745 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
2746 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002747 EXPECT_FALSE(ParseUrl("stun:hostname:port:more"));
2748 EXPECT_FALSE(ParseUrl("stun:hostname:port more"));
deadbeef0a6c4ca2015-10-06 11:38:28 -07002749 EXPECT_FALSE(ParseUrl("stun:hostname:"));
2750 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
2751 EXPECT_FALSE(ParseUrl("stun::5555"));
2752 EXPECT_FALSE(ParseUrl("stun:"));
2753}
2754
2755// Test parsing the "?transport=xxx" part of the URL.
2756TEST_F(IceServerParsingTest, ParseTransport) {
deadbeef293e9262017-01-11 12:28:30 -08002757 EXPECT_TRUE(ParseTurnUrl("turn:hostname:1234?transport=tcp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002758 EXPECT_EQ(1U, turn_servers_.size());
2759 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
2760 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002761
deadbeef293e9262017-01-11 12:28:30 -08002762 EXPECT_TRUE(ParseTurnUrl("turn:hostname?transport=udp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002763 EXPECT_EQ(1U, turn_servers_.size());
2764 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2765 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002766
deadbeef293e9262017-01-11 12:28:30 -08002767 EXPECT_FALSE(ParseTurnUrl("turn:hostname?transport=invalid"));
2768 EXPECT_FALSE(ParseTurnUrl("turn:hostname?transport="));
2769 EXPECT_FALSE(ParseTurnUrl("turn:hostname?="));
2770 EXPECT_FALSE(ParseTurnUrl("?"));
deadbeef0a6c4ca2015-10-06 11:38:28 -07002771}
2772
2773// Test parsing ICE username contained in URL.
2774TEST_F(IceServerParsingTest, ParseUsername) {
deadbeef293e9262017-01-11 12:28:30 -08002775 EXPECT_TRUE(ParseTurnUrl("turn:user@hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002776 EXPECT_EQ(1U, turn_servers_.size());
2777 EXPECT_EQ("user", turn_servers_[0].credentials.username);
2778 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002779
deadbeef293e9262017-01-11 12:28:30 -08002780 EXPECT_FALSE(ParseTurnUrl("turn:@hostname"));
2781 EXPECT_FALSE(ParseTurnUrl("turn:username@"));
2782 EXPECT_FALSE(ParseTurnUrl("turn:@"));
2783 EXPECT_FALSE(ParseTurnUrl("turn:user@name@hostname"));
deadbeef0a6c4ca2015-10-06 11:38:28 -07002784}
2785
2786// Test that username and password from IceServer is copied into the resulting
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002787// RelayServerConfig.
deadbeef0a6c4ca2015-10-06 11:38:28 -07002788TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
2789 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002790 EXPECT_EQ(1U, turn_servers_.size());
2791 EXPECT_EQ("username", turn_servers_[0].credentials.username);
2792 EXPECT_EQ("password", turn_servers_[0].credentials.password);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002793}
2794
2795// Ensure that if a server has multiple URLs, each one is parsed.
2796TEST_F(IceServerParsingTest, ParseMultipleUrls) {
2797 PeerConnectionInterface::IceServers servers;
2798 PeerConnectionInterface::IceServer server;
2799 server.urls.push_back("stun:hostname");
2800 server.urls.push_back("turn:hostname");
deadbeef293e9262017-01-11 12:28:30 -08002801 server.username = "foo";
2802 server.password = "bar";
deadbeef0a6c4ca2015-10-06 11:38:28 -07002803 servers.push_back(server);
deadbeef293e9262017-01-11 12:28:30 -08002804 EXPECT_EQ(webrtc::RTCErrorType::NONE,
2805 webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002806 EXPECT_EQ(1U, stun_servers_.size());
2807 EXPECT_EQ(1U, turn_servers_.size());
deadbeef0a6c4ca2015-10-06 11:38:28 -07002808}
2809
Taylor Brandstetter893505d2016-01-07 15:12:48 -08002810// Ensure that TURN servers are given unique priorities,
2811// so that their resulting candidates have unique priorities.
2812TEST_F(IceServerParsingTest, TurnServerPrioritiesUnique) {
2813 PeerConnectionInterface::IceServers servers;
2814 PeerConnectionInterface::IceServer server;
2815 server.urls.push_back("turn:hostname");
2816 server.urls.push_back("turn:hostname2");
deadbeef293e9262017-01-11 12:28:30 -08002817 server.username = "foo";
2818 server.password = "bar";
Taylor Brandstetter893505d2016-01-07 15:12:48 -08002819 servers.push_back(server);
deadbeef293e9262017-01-11 12:28:30 -08002820 EXPECT_EQ(webrtc::RTCErrorType::NONE,
2821 webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
Taylor Brandstetter893505d2016-01-07 15:12:48 -08002822 EXPECT_EQ(2U, turn_servers_.size());
2823 EXPECT_NE(turn_servers_[0].priority, turn_servers_[1].priority);
2824}
2825
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00002826#endif // if !defined(THREAD_SANITIZER)
hta6b4f8392016-03-10 00:24:31 -08002827
2828} // namespace