blob: 422d37c2551d070df14072b234840a56d6a5ee29 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
11#include <stdio.h>
12
13#include <algorithm>
14#include <list>
15#include <map>
kwibergd1fe2812016-04-27 06:47:29 -070016#include <memory>
kwiberg0eb15ed2015-12-17 03:04:15 -080017#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018#include <vector>
19
Henrik Kjellander15583c12016-02-10 10:53:12 +010020#include "webrtc/api/dtmfsender.h"
21#include "webrtc/api/fakemetricsobserver.h"
22#include "webrtc/api/localaudiosource.h"
23#include "webrtc/api/mediastreaminterface.h"
24#include "webrtc/api/peerconnection.h"
25#include "webrtc/api/peerconnectionfactory.h"
26#include "webrtc/api/peerconnectioninterface.h"
27#include "webrtc/api/test/fakeaudiocapturemodule.h"
28#include "webrtc/api/test/fakeconstraints.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010029#include "webrtc/api/test/fakeperiodicvideocapturer.h"
Henrik Boströmd79599d2016-06-01 13:58:50 +020030#include "webrtc/api/test/fakertccertificategenerator.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010031#include "webrtc/api/test/fakevideotrackrenderer.h"
32#include "webrtc/api/test/mockpeerconnectionobservers.h"
Taylor Brandstettere5835f52016-09-16 15:07:50 -070033#include "webrtc/base/fakenetwork.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/gunit.h"
Taylor Brandstetter9b5306c2016-08-18 11:40:37 -070035#include "webrtc/base/helpers.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000036#include "webrtc/base/physicalsocketserver.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000037#include "webrtc/base/ssladapter.h"
38#include "webrtc/base/sslstreamadapter.h"
39#include "webrtc/base/thread.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000040#include "webrtc/base/virtualsocketserver.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010041#include "webrtc/media/engine/fakewebrtcvideoengine.h"
kjellanderf4752772016-03-02 05:42:30 -080042#include "webrtc/p2p/base/p2pconstants.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000043#include "webrtc/p2p/base/sessiondescription.h"
Taylor Brandstettere5835f52016-09-16 15:07:50 -070044#include "webrtc/p2p/base/testturnserver.h"
45#include "webrtc/p2p/client/basicportallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010046#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047
48#define MAYBE_SKIP_TEST(feature) \
49 if (!(feature())) { \
50 LOG(LS_INFO) << "Feature disabled... skipping"; \
51 return; \
52 }
53
54using cricket::ContentInfo;
55using cricket::FakeWebRtcVideoDecoder;
56using cricket::FakeWebRtcVideoDecoderFactory;
57using cricket::FakeWebRtcVideoEncoder;
58using cricket::FakeWebRtcVideoEncoderFactory;
59using cricket::MediaContentDescription;
60using webrtc::DataBuffer;
61using webrtc::DataChannelInterface;
62using webrtc::DtmfSender;
63using webrtc::DtmfSenderInterface;
64using webrtc::DtmfSenderObserverInterface;
65using webrtc::FakeConstraints;
66using webrtc::MediaConstraintsInterface;
deadbeeffaac4972015-11-12 15:33:07 -080067using webrtc::MediaStreamInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068using webrtc::MediaStreamTrackInterface;
69using webrtc::MockCreateSessionDescriptionObserver;
70using webrtc::MockDataChannelObserver;
71using webrtc::MockSetSessionDescriptionObserver;
72using webrtc::MockStatsObserver;
deadbeeffaac4972015-11-12 15:33:07 -080073using webrtc::ObserverInterface;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000074using webrtc::PeerConnectionInterface;
Joachim Bauch04e5b492015-05-29 09:40:39 +020075using webrtc::PeerConnectionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076using webrtc::SessionDescriptionInterface;
77using webrtc::StreamCollectionInterface;
78
hta6b4f8392016-03-10 00:24:31 -080079namespace {
80
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000081static const int kMaxWaitMs = 10000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000082// Disable for TSan v2, see
83// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
84// This declaration is also #ifdef'd as it causes uninitialized-variable
85// warnings.
86#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087static const int kMaxWaitForStatsMs = 3000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000088#endif
deadbeeffac06552015-11-25 11:26:01 -080089static const int kMaxWaitForActivationMs = 5000;
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000090static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091static const int kEndAudioFrameCount = 3;
92static const int kEndVideoFrameCount = 3;
93
94static const char kStreamLabelBase[] = "stream_label";
95static const char kVideoTrackLabelBase[] = "video_track";
96static const char kAudioTrackLabelBase[] = "audio_track";
97static const char kDataChannelLabel[] = "data_channel";
98
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +000099// Disable for TSan v2, see
100// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
101// This declaration is also #ifdef'd as it causes unused-variable errors.
102#if !defined(THREAD_SANITIZER)
103// SRTP cipher name negotiated by the tests. This must be updated if the
104// default changes.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800105static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
jbauchcb560652016-08-04 05:20:32 -0700106static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000107#endif
108
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700109// Used to simulate signaling ICE/SDP between two PeerConnections.
110enum Message { MSG_SDP_MESSAGE, MSG_ICE_MESSAGE };
111
112struct SdpMessage {
113 std::string type;
114 std::string msg;
115};
116
117struct IceMessage {
118 std::string sdp_mid;
119 int sdp_mline_index;
120 std::string msg;
121};
122
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123static void RemoveLinesFromSdp(const std::string& line_start,
124 std::string* sdp) {
125 const char kSdpLineEnd[] = "\r\n";
126 size_t ssrc_pos = 0;
127 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
128 std::string::npos) {
129 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
130 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
131 }
132}
133
hta6b4f8392016-03-10 00:24:31 -0800134bool StreamsHaveAudioTrack(StreamCollectionInterface* streams) {
135 for (size_t idx = 0; idx < streams->count(); idx++) {
136 auto stream = streams->at(idx);
137 if (stream->GetAudioTracks().size() > 0) {
138 return true;
139 }
140 }
141 return false;
142}
143
144bool StreamsHaveVideoTrack(StreamCollectionInterface* streams) {
145 for (size_t idx = 0; idx < streams->count(); idx++) {
146 auto stream = streams->at(idx);
147 if (stream->GetVideoTracks().size() > 0) {
148 return true;
149 }
150 }
151 return false;
152}
153
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154class SignalingMessageReceiver {
155 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 virtual void ReceiveSdpMessage(const std::string& type,
157 std::string& msg) = 0;
158 virtual void ReceiveIceMessage(const std::string& sdp_mid,
159 int sdp_mline_index,
160 const std::string& msg) = 0;
161
162 protected:
deadbeefaf1b59c2015-10-15 12:08:41 -0700163 SignalingMessageReceiver() {}
164 virtual ~SignalingMessageReceiver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165};
166
zhihuang184a3fd2016-06-14 11:47:14 -0700167class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
168 public:
169 MockRtpReceiverObserver(cricket::MediaType media_type)
170 : expected_media_type_(media_type) {}
171
172 void OnFirstPacketReceived(cricket::MediaType media_type) override {
173 ASSERT_EQ(expected_media_type_, media_type);
174 first_packet_received_ = true;
175 }
176
177 bool first_packet_received() { return first_packet_received_; }
178
179 virtual ~MockRtpReceiverObserver() {}
180
181 private:
182 bool first_packet_received_ = false;
183 cricket::MediaType expected_media_type_;
184};
185
deadbeefaf1b59c2015-10-15 12:08:41 -0700186class PeerConnectionTestClient : public webrtc::PeerConnectionObserver,
deadbeeffaac4972015-11-12 15:33:07 -0800187 public SignalingMessageReceiver,
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700188 public ObserverInterface,
189 public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 public:
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700191 // If |config| is not provided, uses a default constructed RTCConfiguration.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800192 static PeerConnectionTestClient* CreateClientWithDtlsIdentityStore(
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800193 const std::string& id,
194 const MediaConstraintsInterface* constraints,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800195 const PeerConnectionFactory::Options* options,
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700196 const PeerConnectionInterface::RTCConfiguration* config,
Henrik Boströmd79599d2016-06-01 13:58:50 +0200197 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
perkj8aba9972016-04-10 23:54:34 -0700198 bool prefer_constraint_apis,
danilchape9021a32016-05-17 01:52:02 -0700199 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700200 rtc::Thread* worker_thread) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800201 PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
zhihuang9763d562016-08-05 11:14:50 -0700202 if (!client->Init(constraints, options, config, std::move(cert_generator),
danilchape9021a32016-05-17 01:52:02 -0700203 prefer_constraint_apis, network_thread, worker_thread)) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800204 delete client;
205 return nullptr;
206 }
207 return client;
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800208 }
209
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800210 static PeerConnectionTestClient* CreateClient(
211 const std::string& id,
212 const MediaConstraintsInterface* constraints,
perkj8aba9972016-04-10 23:54:34 -0700213 const PeerConnectionFactory::Options* options,
Taylor Brandstettere5835f52016-09-16 15:07:50 -0700214 const PeerConnectionInterface::RTCConfiguration* config,
danilchape9021a32016-05-17 01:52:02 -0700215 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700216 rtc::Thread* worker_thread) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200217 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
218 rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
219 new FakeRTCCertificateGenerator() : nullptr);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800220
zhihuang9763d562016-08-05 11:14:50 -0700221 return CreateClientWithDtlsIdentityStore(id, constraints, options, config,
222 std::move(cert_generator), true,
223 network_thread, worker_thread);
htaaac2dea2016-03-10 13:35:55 -0800224 }
225
226 static PeerConnectionTestClient* CreateClientPreferNoConstraints(
227 const std::string& id,
perkj8aba9972016-04-10 23:54:34 -0700228 const PeerConnectionFactory::Options* options,
danilchape9021a32016-05-17 01:52:02 -0700229 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700230 rtc::Thread* worker_thread) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200231 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
232 rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
233 new FakeRTCCertificateGenerator() : nullptr);
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
1290 void VerifyRenderedSize(int width, int height) {
perkjcaafdba2016-03-20 07:34:29 -07001291 VerifyRenderedSize(width, height, webrtc::kVideoRotation_0);
1292 }
1293
1294 void VerifyRenderedSize(int width,
1295 int height,
1296 webrtc::VideoRotation rotation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 EXPECT_EQ(width, receiving_client()->rendered_width());
1298 EXPECT_EQ(height, receiving_client()->rendered_height());
perkjcaafdba2016-03-20 07:34:29 -07001299 EXPECT_EQ(rotation, receiving_client()->rendered_rotation());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300 EXPECT_EQ(width, initializing_client()->rendered_width());
1301 EXPECT_EQ(height, initializing_client()->rendered_height());
perkjcaafdba2016-03-20 07:34:29 -07001302 EXPECT_EQ(rotation, initializing_client()->rendered_rotation());
1303
1304 // Verify size of the local preview.
1305 EXPECT_EQ(width, initializing_client()->local_rendered_width());
1306 EXPECT_EQ(height, initializing_client()->local_rendered_height());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001307 }
1308
1309 void VerifySessionDescriptions() {
1310 initiating_client_->VerifyRejectedMediaInSessionDescription();
1311 receiving_client_->VerifyRejectedMediaInSessionDescription();
1312 initiating_client_->VerifyLocalIceUfragAndPassword();
1313 receiving_client_->VerifyLocalIceUfragAndPassword();
1314 }
1315
deadbeef7c73bdb2015-12-10 15:10:44 -08001316 ~P2PTestConductor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001318 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001319 }
1320 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001321 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323 }
1324
deadbeefaf1b59c2015-10-15 12:08:41 -07001325 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001326
1327 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1328 MediaConstraintsInterface* recv_constraints) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001329 return CreateTestClients(init_constraints, nullptr, nullptr,
1330 recv_constraints, nullptr, nullptr);
1331 }
1332
1333 bool CreateTestClients(
1334 const PeerConnectionInterface::RTCConfiguration& init_config,
1335 const PeerConnectionInterface::RTCConfiguration& recv_config) {
1336 return CreateTestClients(nullptr, nullptr, &init_config, nullptr, nullptr,
1337 &recv_config);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001338 }
1339
htaaac2dea2016-03-10 13:35:55 -08001340 bool CreateTestClientsThatPreferNoConstraints() {
1341 initiating_client_.reset(
perkj8aba9972016-04-10 23:54:34 -07001342 PeerConnectionTestClient::CreateClientPreferNoConstraints(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001343 "Caller: ", nullptr, network_thread_.get(), worker_thread_.get()));
htaaac2dea2016-03-10 13:35:55 -08001344 receiving_client_.reset(
perkj8aba9972016-04-10 23:54:34 -07001345 PeerConnectionTestClient::CreateClientPreferNoConstraints(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001346 "Callee: ", nullptr, network_thread_.get(), worker_thread_.get()));
htaaac2dea2016-03-10 13:35:55 -08001347 if (!initiating_client_ || !receiving_client_) {
1348 return false;
1349 }
1350 // Remember the choice for possible later resets of the clients.
1351 prefer_constraint_apis_ = false;
1352 SetSignalingReceivers();
1353 return true;
1354 }
1355
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001356 bool CreateTestClients(
1357 MediaConstraintsInterface* init_constraints,
1358 PeerConnectionFactory::Options* init_options,
1359 const PeerConnectionInterface::RTCConfiguration* init_config,
1360 MediaConstraintsInterface* recv_constraints,
1361 PeerConnectionFactory::Options* recv_options,
1362 const PeerConnectionInterface::RTCConfiguration* recv_config) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001363 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001364 "Caller: ", init_constraints, init_options, init_config,
zhihuang9763d562016-08-05 11:14:50 -07001365 network_thread_.get(), worker_thread_.get()));
deadbeefaf1b59c2015-10-15 12:08:41 -07001366 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001367 "Callee: ", recv_constraints, recv_options, recv_config,
zhihuang9763d562016-08-05 11:14:50 -07001368 network_thread_.get(), worker_thread_.get()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369 if (!initiating_client_ || !receiving_client_) {
1370 return false;
1371 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001372 SetSignalingReceivers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001373 return true;
1374 }
1375
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001376 void SetSignalingReceivers() {
1377 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
1378 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1379 }
1380
1381 void SetSignalingDelayMs(int delay_ms) {
1382 initiating_client_->set_signaling_delay_ms(delay_ms);
1383 receiving_client_->set_signaling_delay_ms(delay_ms);
1384 }
1385
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001386 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1387 const webrtc::FakeConstraints& recv_constraints) {
1388 initiating_client_->SetVideoConstraints(init_constraints);
1389 receiving_client_->SetVideoConstraints(recv_constraints);
1390 }
1391
perkjcaafdba2016-03-20 07:34:29 -07001392 void SetCaptureRotation(webrtc::VideoRotation rotation) {
1393 initiating_client_->SetCaptureRotation(rotation);
1394 receiving_client_->SetCaptureRotation(rotation);
1395 }
1396
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001397 void EnableVideoDecoderFactory() {
1398 initiating_client_->EnableVideoDecoderFactory();
1399 receiving_client_->EnableVideoDecoderFactory();
1400 }
1401
1402 // This test sets up a call between two parties. Both parties send static
1403 // frames to each other. Once the test is finished the number of sent frames
1404 // is compared to the number of received frames.
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -07001405 void LocalP2PTest() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001406 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1407 initiating_client_->AddMediaStream(true, true);
1408 }
1409 initiating_client_->Negotiate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001410 // Assert true is used here since next tests are guaranteed to fail and
1411 // would eat up 5 seconds.
1412 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1413 VerifySessionDescriptions();
1414
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001415 int audio_frame_count = kEndAudioFrameCount;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001416 int video_frame_count = kEndVideoFrameCount;
hta6b4f8392016-03-10 00:24:31 -08001417 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1418
1419 if ((!initiating_client_->can_receive_audio() &&
1420 !initiating_client_->can_receive_video()) ||
1421 (!receiving_client_->can_receive_audio() &&
1422 !receiving_client_->can_receive_video())) {
1423 // Neither audio nor video will flow, so connections won't be
1424 // established. There's nothing more to check.
1425 // TODO(hta): Check connection if there's a data channel.
1426 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001427 }
1428
hta6b4f8392016-03-10 00:24:31 -08001429 // Audio or video is expected to flow, so both clients should reach the
1430 // Connected state, and the offerer (ICE controller) should proceed to
1431 // Completed.
1432 // Note: These tests have been observed to fail under heavy load at
1433 // shorter timeouts, so they may be flaky.
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -07001434 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1435 initiating_client_->ice_connection_state(),
1436 kMaxWaitForFramesMs);
hta6b4f8392016-03-10 00:24:31 -08001437 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1438 receiving_client_->ice_connection_state(),
1439 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001440
hta6b4f8392016-03-10 00:24:31 -08001441 // The ICE gathering state should end up in kIceGatheringComplete,
1442 // but there's a bug that prevents this at the moment, and the state
1443 // machine is being updated by the WEBRTC WG.
1444 // TODO(hta): Update this check when spec revisions finish.
1445 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1446 initiating_client_->ice_gathering_state());
1447 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1448 receiving_client_->ice_gathering_state(),
1449 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001450
hta6b4f8392016-03-10 00:24:31 -08001451 // Check that the expected number of frames have arrived.
1452 EXPECT_TRUE_WAIT(FramesHaveArrived(audio_frame_count, video_frame_count),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001453 kMaxWaitForFramesMs);
1454 }
1455
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001456 void SetupAndVerifyDtlsCall() {
1457 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1458 FakeConstraints setup_constraints;
1459 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1460 true);
1461 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1462 LocalP2PTest();
1463 VerifyRenderedSize(640, 480);
1464 }
1465
1466 PeerConnectionTestClient* CreateDtlsClientWithAlternateKey() {
1467 FakeConstraints setup_constraints;
1468 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1469 true);
1470
Henrik Boströmd79599d2016-06-01 13:58:50 +02001471 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1472 rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
1473 new FakeRTCCertificateGenerator() : nullptr);
1474 cert_generator->use_alternate_key();
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001475
1476 // Make sure the new client is using a different certificate.
1477 return PeerConnectionTestClient::CreateClientWithDtlsIdentityStore(
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001478 "New Peer: ", &setup_constraints, nullptr, nullptr,
Henrik Boströmd79599d2016-06-01 13:58:50 +02001479 std::move(cert_generator), prefer_constraint_apis_,
danilchape9021a32016-05-17 01:52:02 -07001480 network_thread_.get(), worker_thread_.get());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001481 }
1482
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001483 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1484 // Messages may get lost on the unreliable DataChannel, so we send multiple
1485 // times to avoid test flakiness.
1486 static const size_t kSendAttempts = 5;
1487
1488 for (size_t i = 0; i < kSendAttempts; ++i) {
1489 dc->Send(DataBuffer(data));
1490 }
1491 }
1492
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001493 rtc::Thread* network_thread() { return network_thread_.get(); }
1494
Taylor Brandstetter9b5306c2016-08-18 11:40:37 -07001495 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1496
deadbeefaf1b59c2015-10-15 12:08:41 -07001497 PeerConnectionTestClient* initializing_client() {
1498 return initiating_client_.get();
1499 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001500
1501 // Set the |initiating_client_| to the |client| passed in and return the
1502 // original |initiating_client_|.
1503 PeerConnectionTestClient* set_initializing_client(
1504 PeerConnectionTestClient* client) {
1505 PeerConnectionTestClient* old = initiating_client_.release();
1506 initiating_client_.reset(client);
1507 return old;
1508 }
1509
deadbeefaf1b59c2015-10-15 12:08:41 -07001510 PeerConnectionTestClient* receiving_client() {
1511 return receiving_client_.get();
1512 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001514 // Set the |receiving_client_| to the |client| passed in and return the
1515 // original |receiving_client_|.
1516 PeerConnectionTestClient* set_receiving_client(
1517 PeerConnectionTestClient* client) {
1518 PeerConnectionTestClient* old = receiving_client_.release();
1519 receiving_client_.reset(client);
1520 return old;
1521 }
1522
zhihuang184a3fd2016-06-14 11:47:14 -07001523 bool AllObserversReceived(
1524 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>& observers) {
1525 for (auto& observer : observers) {
1526 if (!observer->first_packet_received()) {
1527 return false;
1528 }
1529 }
1530 return true;
1531 }
1532
jbauchcb560652016-08-04 05:20:32 -07001533 void TestGcmNegotiation(bool local_gcm_enabled, bool remote_gcm_enabled,
1534 int expected_cipher_suite) {
1535 PeerConnectionFactory::Options init_options;
1536 init_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled;
1537 PeerConnectionFactory::Options recv_options;
1538 recv_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001539 ASSERT_TRUE(CreateTestClients(nullptr, &init_options, nullptr, nullptr,
1540 &recv_options, nullptr));
jbauchcb560652016-08-04 05:20:32 -07001541 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1542 init_observer =
1543 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1544 initializing_client()->pc()->RegisterUMAObserver(init_observer);
1545 LocalP2PTest();
1546
1547 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
1548 initializing_client()->GetSrtpCipherStats(),
1549 kMaxWaitMs);
1550 EXPECT_EQ(1,
1551 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1552 expected_cipher_suite));
1553 }
1554
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555 private:
deadbeefeff5b852016-05-27 14:18:01 -07001556 // |ss_| is used by |network_thread_| so it must be destroyed later.
kwibergd1fe2812016-04-27 06:47:29 -07001557 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
1558 std::unique_ptr<rtc::VirtualSocketServer> ss_;
deadbeefeff5b852016-05-27 14:18:01 -07001559 // |network_thread_| and |worker_thread_| are used by both
1560 // |initiating_client_| and |receiving_client_| so they must be destroyed
1561 // later.
1562 std::unique_ptr<rtc::Thread> network_thread_;
1563 std::unique_ptr<rtc::Thread> worker_thread_;
kwibergd1fe2812016-04-27 06:47:29 -07001564 std::unique_ptr<PeerConnectionTestClient> initiating_client_;
1565 std::unique_ptr<PeerConnectionTestClient> receiving_client_;
htaaac2dea2016-03-10 13:35:55 -08001566 bool prefer_constraint_apis_ = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001569// Disable for TSan v2, see
1570// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1571#if !defined(THREAD_SANITIZER)
1572
zhihuang184a3fd2016-06-14 11:47:14 -07001573TEST_F(P2PTestConductor, TestRtpReceiverObserverCallbackFunction) {
1574 ASSERT_TRUE(CreateTestClients());
1575 LocalP2PTest();
1576 EXPECT_TRUE_WAIT(
1577 AllObserversReceived(initializing_client()->rtp_receiver_observers()),
1578 kMaxWaitForFramesMs);
1579 EXPECT_TRUE_WAIT(
1580 AllObserversReceived(receiving_client()->rtp_receiver_observers()),
1581 kMaxWaitForFramesMs);
1582}
1583
1584// The observers are expected to fire the signal even if they are set after the
1585// first packet is received.
1586TEST_F(P2PTestConductor, TestSetRtpReceiverObserverAfterFirstPacketIsReceived) {
1587 ASSERT_TRUE(CreateTestClients());
1588 LocalP2PTest();
1589 // Reset the RtpReceiverObservers.
1590 initializing_client()->SetRtpReceiverObservers();
1591 receiving_client()->SetRtpReceiverObservers();
1592 EXPECT_TRUE_WAIT(
1593 AllObserversReceived(initializing_client()->rtp_receiver_observers()),
1594 kMaxWaitForFramesMs);
1595 EXPECT_TRUE_WAIT(
1596 AllObserversReceived(receiving_client()->rtp_receiver_observers()),
1597 kMaxWaitForFramesMs);
1598}
1599
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001601// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1602// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001603TEST_F(P2PTestConductor, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001604 ASSERT_TRUE(CreateTestClients());
1605 LocalP2PTest();
1606 VerifyDtmf();
1607}
1608
1609// This test sets up a Jsep call between two parties and test that we can get a
1610// video aspect ratio of 16:9.
deadbeef7c73bdb2015-12-10 15:10:44 -08001611TEST_F(P2PTestConductor, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001612 ASSERT_TRUE(CreateTestClients());
1613 FakeConstraints constraint;
1614 double requested_ratio = 640.0/360;
1615 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1616 SetVideoConstraints(constraint, constraint);
1617 LocalP2PTest();
1618
1619 ASSERT_LE(0, initializing_client()->rendered_height());
1620 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001621 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001622 initializing_client()->rendered_height();
1623 EXPECT_LE(requested_ratio, initiating_video_ratio);
1624
1625 ASSERT_LE(0, receiving_client()->rendered_height());
1626 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001627 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628 receiving_client()->rendered_height();
1629 EXPECT_LE(requested_ratio, receiving_video_ratio);
1630}
1631
1632// This test sets up a Jsep call between two parties and test that the
1633// received video has a resolution of 1280*720.
1634// TODO(mallinath): Enable when
1635// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
deadbeef7c73bdb2015-12-10 15:10:44 -08001636TEST_F(P2PTestConductor, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637 ASSERT_TRUE(CreateTestClients());
1638 FakeConstraints constraint;
1639 constraint.SetMandatoryMinWidth(1280);
1640 constraint.SetMandatoryMinHeight(720);
1641 SetVideoConstraints(constraint, constraint);
1642 LocalP2PTest();
1643 VerifyRenderedSize(1280, 720);
1644}
1645
1646// This test sets up a call between two endpoints that are configured to use
1647// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001648TEST_F(P2PTestConductor, LocalP2PTestDtls) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001649 SetupAndVerifyDtlsCall();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650}
1651
hta6b4f8392016-03-10 00:24:31 -08001652// This test sets up an one-way call, with media only from initiator to
1653// responder.
1654TEST_F(P2PTestConductor, OneWayMediaCall) {
1655 ASSERT_TRUE(CreateTestClients());
1656 receiving_client()->set_auto_add_stream(false);
1657 LocalP2PTest();
1658}
1659
htaaac2dea2016-03-10 13:35:55 -08001660TEST_F(P2PTestConductor, OneWayMediaCallWithoutConstraints) {
1661 ASSERT_TRUE(CreateTestClientsThatPreferNoConstraints());
1662 receiving_client()->set_auto_add_stream(false);
1663 LocalP2PTest();
1664}
1665
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001666// This test sets up a audio call initially and then upgrades to audio/video,
1667// using DTLS.
deadbeef7c73bdb2015-12-10 15:10:44 -08001668TEST_F(P2PTestConductor, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001669 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001670 FakeConstraints setup_constraints;
1671 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1672 true);
1673 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1674 receiving_client()->SetReceiveAudioVideo(true, false);
1675 LocalP2PTest();
1676 receiving_client()->SetReceiveAudioVideo(true, true);
1677 receiving_client()->Negotiate();
1678}
1679
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001680// This test sets up a call transfer to a new caller with a different DTLS
1681// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001682TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCallee) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001683 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1684 SetupAndVerifyDtlsCall();
1685
1686 // Keeping the original peer around which will still send packets to the
1687 // receiving client. These SRTP packets will be dropped.
kwibergd1fe2812016-04-27 06:47:29 -07001688 std::unique_ptr<PeerConnectionTestClient> original_peer(
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001689 set_initializing_client(CreateDtlsClientWithAlternateKey()));
1690 original_peer->pc()->Close();
1691
1692 SetSignalingReceivers();
1693 receiving_client()->SetExpectIceRestart(true);
1694 LocalP2PTest();
1695 VerifyRenderedSize(640, 480);
1696}
1697
guoweis46383312015-12-17 16:45:59 -08001698// This test sets up a non-bundle call and apply bundle during ICE restart. When
1699// bundle is in effect in the restart, the channel can successfully reset its
1700// DTLS-SRTP context.
1701TEST_F(P2PTestConductor, LocalP2PTestDtlsBundleInIceRestart) {
1702 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1703 FakeConstraints setup_constraints;
1704 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1705 true);
1706 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1707 receiving_client()->RemoveBundleFromReceivedSdp(true);
1708 LocalP2PTest();
1709 VerifyRenderedSize(640, 480);
1710
1711 initializing_client()->IceRestart();
1712 receiving_client()->SetExpectIceRestart(true);
1713 receiving_client()->RemoveBundleFromReceivedSdp(false);
1714 LocalP2PTest();
1715 VerifyRenderedSize(640, 480);
1716}
1717
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001718// This test sets up a call transfer to a new callee with a different DTLS
1719// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001720TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCaller) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001721 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1722 SetupAndVerifyDtlsCall();
1723
1724 // Keeping the original peer around which will still send packets to the
1725 // receiving client. These SRTP packets will be dropped.
kwibergd1fe2812016-04-27 06:47:29 -07001726 std::unique_ptr<PeerConnectionTestClient> original_peer(
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001727 set_receiving_client(CreateDtlsClientWithAlternateKey()));
1728 original_peer->pc()->Close();
1729
1730 SetSignalingReceivers();
1731 initializing_client()->IceRestart();
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -07001732 LocalP2PTest();
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001733 VerifyRenderedSize(640, 480);
1734}
1735
perkjcaafdba2016-03-20 07:34:29 -07001736TEST_F(P2PTestConductor, LocalP2PTestCVO) {
1737 ASSERT_TRUE(CreateTestClients());
1738 SetCaptureRotation(webrtc::kVideoRotation_90);
1739 LocalP2PTest();
1740 VerifyRenderedSize(640, 480, webrtc::kVideoRotation_90);
1741}
1742
1743TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportCVO) {
1744 ASSERT_TRUE(CreateTestClients());
1745 SetCaptureRotation(webrtc::kVideoRotation_90);
1746 receiving_client()->RemoveCvoFromReceivedSdp(true);
1747 LocalP2PTest();
1748 VerifyRenderedSize(480, 640, webrtc::kVideoRotation_0);
1749}
1750
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001751// This test sets up a call between two endpoints that are configured to use
1752// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1753// negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001754TEST_F(P2PTestConductor, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001755 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756 FakeConstraints setup_constraints;
1757 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1758 true);
1759 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1760 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1761 LocalP2PTest();
1762 VerifyRenderedSize(640, 480);
1763}
1764
zhihuangaf388472016-11-02 16:49:48 -07001765// This test verifies that the negotiation will succeed with data channel only
1766// in max-bundle mode.
1767TEST_F(P2PTestConductor, LocalP2PTestOfferDataChannelOnly) {
1768 webrtc::PeerConnectionInterface::RTCConfiguration rtc_config;
1769 rtc_config.bundle_policy =
1770 webrtc::PeerConnectionInterface::kBundlePolicyMaxBundle;
1771 ASSERT_TRUE(CreateTestClients(rtc_config, rtc_config));
1772 initializing_client()->CreateDataChannel();
1773 initializing_client()->Negotiate();
1774}
1775
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776// This test sets up a Jsep call between two parties, and the callee only
1777// accept to receive video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001778TEST_F(P2PTestConductor, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779 ASSERT_TRUE(CreateTestClients());
1780 receiving_client()->SetReceiveAudioVideo(false, true);
1781 LocalP2PTest();
1782}
1783
1784// This test sets up a Jsep call between two parties, and the callee only
1785// accept to receive audio.
deadbeef7c73bdb2015-12-10 15:10:44 -08001786TEST_F(P2PTestConductor, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001787 ASSERT_TRUE(CreateTestClients());
1788 receiving_client()->SetReceiveAudioVideo(true, false);
1789 LocalP2PTest();
1790}
1791
1792// This test sets up a Jsep call between two parties, and the callee reject both
1793// audio and video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001794TEST_F(P2PTestConductor, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795 ASSERT_TRUE(CreateTestClients());
1796 receiving_client()->SetReceiveAudioVideo(false, false);
1797 LocalP2PTest();
1798}
1799
1800// This test sets up an audio and video call between two parties. After the call
1801// runs for a while (10 frames), the caller sends an update offer with video
1802// being rejected. Once the re-negotiation is done, the video flow should stop
1803// and the audio flow should continue.
deadbeefc9be0072015-12-14 18:27:57 -08001804TEST_F(P2PTestConductor, UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805 ASSERT_TRUE(CreateTestClients());
1806 LocalP2PTest();
1807 TestUpdateOfferWithRejectedContent();
1808}
1809
1810// This test sets up a Jsep call between two parties. The MSID is removed from
1811// the SDP strings from the caller.
deadbeefc9be0072015-12-14 18:27:57 -08001812TEST_F(P2PTestConductor, LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813 ASSERT_TRUE(CreateTestClients());
1814 receiving_client()->RemoveMsidFromReceivedSdp(true);
1815 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1816 // audio and video is muxed when MSID is disabled. Remove
1817 // SetRemoveBundleFromSdp once
1818 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1819 receiving_client()->RemoveBundleFromReceivedSdp(true);
1820 LocalP2PTest();
1821}
1822
1823// This test sets up a Jsep call between two parties and the initiating peer
1824// sends two steams.
1825// TODO(perkj): Disabled due to
1826// https://code.google.com/p/webrtc/issues/detail?id=1454
deadbeef7c73bdb2015-12-10 15:10:44 -08001827TEST_F(P2PTestConductor, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001828 ASSERT_TRUE(CreateTestClients());
1829 // Set optional video constraint to max 320pixels to decrease CPU usage.
1830 FakeConstraints constraint;
1831 constraint.SetOptionalMaxWidth(320);
1832 SetVideoConstraints(constraint, constraint);
1833 initializing_client()->AddMediaStream(true, true);
1834 initializing_client()->AddMediaStream(false, true);
1835 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1836 LocalP2PTest();
1837 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1838}
1839
1840// Test that we can receive the audio output level from a remote audio track.
deadbeef7c73bdb2015-12-10 15:10:44 -08001841TEST_F(P2PTestConductor, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842 ASSERT_TRUE(CreateTestClients());
1843 LocalP2PTest();
1844
1845 StreamCollectionInterface* remote_streams =
1846 initializing_client()->remote_streams();
1847 ASSERT_GT(remote_streams->count(), 0u);
1848 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1849 MediaStreamTrackInterface* remote_audio_track =
1850 remote_streams->at(0)->GetAudioTracks()[0];
1851
1852 // Get the audio output level stats. Note that the level is not available
1853 // until a RTCP packet has been received.
1854 EXPECT_TRUE_WAIT(
1855 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1856 kMaxWaitForStatsMs);
1857}
1858
1859// Test that an audio input level is reported.
deadbeef7c73bdb2015-12-10 15:10:44 -08001860TEST_F(P2PTestConductor, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861 ASSERT_TRUE(CreateTestClients());
1862 LocalP2PTest();
1863
1864 // Get the audio input level stats. The level should be available very
1865 // soon after the test starts.
1866 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1867 kMaxWaitForStatsMs);
1868}
1869
1870// Test that we can get incoming byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001871TEST_F(P2PTestConductor, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001872 ASSERT_TRUE(CreateTestClients());
1873 LocalP2PTest();
1874
1875 StreamCollectionInterface* remote_streams =
1876 initializing_client()->remote_streams();
1877 ASSERT_GT(remote_streams->count(), 0u);
1878 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1879 MediaStreamTrackInterface* remote_audio_track =
1880 remote_streams->at(0)->GetAudioTracks()[0];
1881 EXPECT_TRUE_WAIT(
1882 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1883 kMaxWaitForStatsMs);
1884
1885 MediaStreamTrackInterface* remote_video_track =
1886 remote_streams->at(0)->GetVideoTracks()[0];
1887 EXPECT_TRUE_WAIT(
1888 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1889 kMaxWaitForStatsMs);
1890}
1891
1892// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001893TEST_F(P2PTestConductor, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001894 ASSERT_TRUE(CreateTestClients());
1895 LocalP2PTest();
1896
1897 StreamCollectionInterface* local_streams =
1898 initializing_client()->local_streams();
1899 ASSERT_GT(local_streams->count(), 0u);
1900 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1901 MediaStreamTrackInterface* local_audio_track =
1902 local_streams->at(0)->GetAudioTracks()[0];
1903 EXPECT_TRUE_WAIT(
1904 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1905 kMaxWaitForStatsMs);
1906
1907 MediaStreamTrackInterface* local_video_track =
1908 local_streams->at(0)->GetVideoTracks()[0];
1909 EXPECT_TRUE_WAIT(
1910 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1911 kMaxWaitForStatsMs);
1912}
1913
Joachim Bauch04e5b492015-05-29 09:40:39 +02001914// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001915TEST_F(P2PTestConductor, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001916 PeerConnectionFactory::Options init_options;
1917 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1918 PeerConnectionFactory::Options recv_options;
1919 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001920 ASSERT_TRUE(CreateTestClients(nullptr, &init_options, nullptr, nullptr,
1921 &recv_options, nullptr));
jbauchac8869e2015-07-03 01:36:14 -07001922 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1923 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1924 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001925 LocalP2PTest();
1926
torbjorng43166b82016-03-11 00:06:47 -08001927 EXPECT_TRUE_WAIT(
1928 rtc::SSLStreamAdapter::IsAcceptableCipher(
1929 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1930 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001931 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001932 initializing_client()->GetSrtpCipherStats(),
1933 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001934 EXPECT_EQ(1,
1935 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1936 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001937}
1938
1939// Test that DTLS 1.2 is used if both ends support it.
torbjorng79a5a832016-01-15 07:16:51 -08001940TEST_F(P2PTestConductor, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001941 PeerConnectionFactory::Options init_options;
1942 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1943 PeerConnectionFactory::Options recv_options;
1944 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001945 ASSERT_TRUE(CreateTestClients(nullptr, &init_options, nullptr, nullptr,
1946 &recv_options, nullptr));
jbauchac8869e2015-07-03 01:36:14 -07001947 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1948 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1949 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001950 LocalP2PTest();
1951
torbjorng43166b82016-03-11 00:06:47 -08001952 EXPECT_TRUE_WAIT(
1953 rtc::SSLStreamAdapter::IsAcceptableCipher(
1954 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1955 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001956 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001957 initializing_client()->GetSrtpCipherStats(),
1958 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001959 EXPECT_EQ(1,
1960 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1961 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001962}
1963
1964// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1965// received supports 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001966TEST_F(P2PTestConductor, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001967 PeerConnectionFactory::Options init_options;
1968 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1969 PeerConnectionFactory::Options recv_options;
1970 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001971 ASSERT_TRUE(CreateTestClients(nullptr, &init_options, nullptr, nullptr,
1972 &recv_options, nullptr));
jbauchac8869e2015-07-03 01:36:14 -07001973 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1974 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1975 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001976 LocalP2PTest();
1977
torbjorng43166b82016-03-11 00:06:47 -08001978 EXPECT_TRUE_WAIT(
1979 rtc::SSLStreamAdapter::IsAcceptableCipher(
1980 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1981 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001982 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001983 initializing_client()->GetSrtpCipherStats(),
1984 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001985 EXPECT_EQ(1,
1986 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1987 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001988}
1989
1990// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1991// received supports 1.2.
torbjorng43166b82016-03-11 00:06:47 -08001992TEST_F(P2PTestConductor, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001993 PeerConnectionFactory::Options init_options;
1994 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1995 PeerConnectionFactory::Options recv_options;
1996 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
Taylor Brandstettere5835f52016-09-16 15:07:50 -07001997 ASSERT_TRUE(CreateTestClients(nullptr, &init_options, nullptr, nullptr,
1998 &recv_options, nullptr));
jbauchac8869e2015-07-03 01:36:14 -07001999 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
2000 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2001 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02002002 LocalP2PTest();
2003
torbjorng43166b82016-03-11 00:06:47 -08002004 EXPECT_TRUE_WAIT(
2005 rtc::SSLStreamAdapter::IsAcceptableCipher(
2006 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
2007 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002008 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002009 initializing_client()->GetSrtpCipherStats(),
2010 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002011 EXPECT_EQ(1,
2012 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2013 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00002014}
2015
jbauchcb560652016-08-04 05:20:32 -07002016// Test that a non-GCM cipher is used if both sides only support non-GCM.
2017TEST_F(P2PTestConductor, GetGcmNone) {
2018 TestGcmNegotiation(false, false, kDefaultSrtpCryptoSuite);
2019}
2020
2021// Test that a GCM cipher is used if both ends support it.
2022TEST_F(P2PTestConductor, GetGcmBoth) {
2023 TestGcmNegotiation(true, true, kDefaultSrtpCryptoSuiteGcm);
2024}
2025
2026// Test that GCM isn't used if only the initiator supports it.
2027TEST_F(P2PTestConductor, GetGcmInit) {
2028 TestGcmNegotiation(true, false, kDefaultSrtpCryptoSuite);
2029}
2030
2031// Test that GCM isn't used if only the receiver supports it.
2032TEST_F(P2PTestConductor, GetGcmRecv) {
2033 TestGcmNegotiation(false, true, kDefaultSrtpCryptoSuite);
2034}
2035
deadbeefb5cb19b2015-11-23 16:39:12 -08002036// This test sets up a call between two parties with audio, video and an RTP
2037// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08002038TEST_F(P2PTestConductor, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002039 FakeConstraints setup_constraints;
2040 setup_constraints.SetAllowRtpDataChannels();
2041 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
2042 initializing_client()->CreateDataChannel();
2043 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07002044 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
2045 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002046 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2047 kMaxWaitMs);
2048 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
2049 kMaxWaitMs);
2050
2051 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00002052
2053 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002054 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
2055 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00002056
2057 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002058 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
2059 kMaxWaitMs);
2060
2061 receiving_client()->data_channel()->Close();
2062 // Send new offer and answer.
2063 receiving_client()->Negotiate();
2064 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
2065 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
2066}
2067
deadbeefb5cb19b2015-11-23 16:39:12 -08002068// This test sets up a call between two parties with audio, video and an SCTP
2069// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08002070TEST_F(P2PTestConductor, LocalP2PTestSctpDataChannel) {
deadbeefb5cb19b2015-11-23 16:39:12 -08002071 ASSERT_TRUE(CreateTestClients());
2072 initializing_client()->CreateDataChannel();
2073 LocalP2PTest();
2074 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
2075 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
2076 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2077 kMaxWaitMs);
2078 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
2079
2080 std::string data = "hello world";
2081
2082 initializing_client()->data_channel()->Send(DataBuffer(data));
2083 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
2084 kMaxWaitMs);
2085
2086 receiving_client()->data_channel()->Send(DataBuffer(data));
2087 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
2088 kMaxWaitMs);
2089
2090 receiving_client()->data_channel()->Close();
deadbeef15887932015-12-14 19:32:34 -08002091 EXPECT_TRUE_WAIT(!initializing_client()->data_observer()->IsOpen(),
2092 kMaxWaitMs);
2093 EXPECT_TRUE_WAIT(!receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
deadbeefb5cb19b2015-11-23 16:39:12 -08002094}
2095
Taylor Brandstetter9b5306c2016-08-18 11:40:37 -07002096TEST_F(P2PTestConductor, UnorderedSctpDataChannel) {
2097 ASSERT_TRUE(CreateTestClients());
2098 webrtc::DataChannelInit init;
2099 init.ordered = false;
2100 initializing_client()->CreateDataChannel(&init);
2101
2102 // Introduce random network delays.
2103 // Otherwise it's not a true "unordered" test.
2104 virtual_socket_server()->set_delay_mean(20);
2105 virtual_socket_server()->set_delay_stddev(5);
2106 virtual_socket_server()->UpdateDelayDistribution();
2107
2108 initializing_client()->Negotiate();
2109 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
2110 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
2111 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2112 kMaxWaitMs);
2113 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
2114
2115 static constexpr int kNumMessages = 100;
2116 // Deliberately chosen to be larger than the MTU so messages get fragmented.
2117 static constexpr size_t kMaxMessageSize = 4096;
2118 // Create and send random messages.
2119 std::vector<std::string> sent_messages;
2120 for (int i = 0; i < kNumMessages; ++i) {
2121 size_t length = (rand() % kMaxMessageSize) + 1;
2122 std::string message;
2123 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
2124 initializing_client()->data_channel()->Send(DataBuffer(message));
2125 receiving_client()->data_channel()->Send(DataBuffer(message));
2126 sent_messages.push_back(message);
2127 }
2128
2129 EXPECT_EQ_WAIT(
2130 kNumMessages,
2131 initializing_client()->data_observer()->received_message_count(),
2132 kMaxWaitMs);
2133 EXPECT_EQ_WAIT(kNumMessages,
2134 receiving_client()->data_observer()->received_message_count(),
2135 kMaxWaitMs);
2136
2137 // Sort and compare to make sure none of the messages were corrupted.
2138 std::vector<std::string> initializing_client_received_messages =
2139 initializing_client()->data_observer()->messages();
2140 std::vector<std::string> receiving_client_received_messages =
2141 receiving_client()->data_observer()->messages();
2142 std::sort(sent_messages.begin(), sent_messages.end());
2143 std::sort(initializing_client_received_messages.begin(),
2144 initializing_client_received_messages.end());
2145 std::sort(receiving_client_received_messages.begin(),
2146 receiving_client_received_messages.end());
2147 EXPECT_EQ(sent_messages, initializing_client_received_messages);
2148 EXPECT_EQ(sent_messages, receiving_client_received_messages);
2149
2150 receiving_client()->data_channel()->Close();
2151 EXPECT_TRUE_WAIT(!initializing_client()->data_observer()->IsOpen(),
2152 kMaxWaitMs);
2153 EXPECT_TRUE_WAIT(!receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
2154}
2155
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002156// This test sets up a call between two parties and creates a data channel.
2157// The test tests that received data is buffered unless an observer has been
2158// registered.
2159// Rtp data channels can receive data before the underlying
2160// transport has detected that a channel is writable and thus data can be
2161// received before the data channel state changes to open. That is hard to test
2162// but the same buffering is used in that case.
deadbeef7c73bdb2015-12-10 15:10:44 -08002163TEST_F(P2PTestConductor, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002164 FakeConstraints setup_constraints;
2165 setup_constraints.SetAllowRtpDataChannels();
2166 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
2167 initializing_client()->CreateDataChannel();
2168 initializing_client()->Negotiate();
2169
deadbeefaf1b59c2015-10-15 12:08:41 -07002170 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
2171 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002172 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2173 kMaxWaitMs);
2174 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
2175 receiving_client()->data_channel()->state(), kMaxWaitMs);
2176
2177 // Unregister the existing observer.
2178 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00002179
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002180 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00002181 SendRtpData(initializing_client()->data_channel(), data);
2182
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002183 // Wait a while to allow the sent data to arrive before an observer is
2184 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002185 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002186
2187 MockDataChannelObserver new_observer(receiving_client()->data_channel());
2188 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
2189}
2190
2191// This test sets up a call between two parties with audio, video and but only
2192// the initiating client support data.
deadbeef7c73bdb2015-12-10 15:10:44 -08002193TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00002194 FakeConstraints setup_constraints_1;
2195 setup_constraints_1.SetAllowRtpDataChannels();
2196 // Must disable DTLS to make negotiation succeed.
2197 setup_constraints_1.SetMandatory(
2198 MediaConstraintsInterface::kEnableDtlsSrtp, false);
2199 FakeConstraints setup_constraints_2;
2200 setup_constraints_2.SetMandatory(
2201 MediaConstraintsInterface::kEnableDtlsSrtp, false);
2202 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002203 initializing_client()->CreateDataChannel();
2204 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07002205 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002206 EXPECT_FALSE(receiving_client()->data_channel());
2207 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
2208}
2209
2210// This test sets up a call between two parties with audio, video. When audio
2211// and video is setup and flowing and data channel is negotiated.
deadbeef7c73bdb2015-12-10 15:10:44 -08002212TEST_F(P2PTestConductor, AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213 FakeConstraints setup_constraints;
2214 setup_constraints.SetAllowRtpDataChannels();
2215 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
2216 LocalP2PTest();
2217 initializing_client()->CreateDataChannel();
2218 // Send new offer and answer.
2219 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07002220 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
2221 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002222 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2223 kMaxWaitMs);
2224 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
2225 kMaxWaitMs);
2226}
2227
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00002228// This test sets up a Jsep call with SCTP DataChannel and verifies the
2229// negotiation is completed without error.
2230#ifdef HAVE_SCTP
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002231TEST_F(P2PTestConductor, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002232 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00002233 FakeConstraints constraints;
2234 constraints.SetMandatory(
2235 MediaConstraintsInterface::kEnableDtlsSrtp, true);
2236 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
2237 initializing_client()->CreateDataChannel();
2238 initializing_client()->Negotiate(false, false);
2239}
2240#endif
2241
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002242// This test sets up a call between two parties with audio, and video.
2243// During the call, the initializing side restart ice and the test verifies that
2244// new ice candidates are generated and audio and video still can flow.
deadbeef7c73bdb2015-12-10 15:10:44 -08002245TEST_F(P2PTestConductor, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002246 ASSERT_TRUE(CreateTestClients());
2247
2248 // Negotiate and wait for ice completion and make sure audio and video plays.
2249 LocalP2PTest();
2250
2251 // Create a SDP string of the first audio candidate for both clients.
2252 const webrtc::IceCandidateCollection* audio_candidates_initiator =
2253 initializing_client()->pc()->local_description()->candidates(0);
2254 const webrtc::IceCandidateCollection* audio_candidates_receiver =
2255 receiving_client()->pc()->local_description()->candidates(0);
2256 ASSERT_GT(audio_candidates_initiator->count(), 0u);
2257 ASSERT_GT(audio_candidates_receiver->count(), 0u);
2258 std::string initiator_candidate;
2259 EXPECT_TRUE(
2260 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
2261 std::string receiver_candidate;
2262 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
2263
2264 // Restart ice on the initializing client.
2265 receiving_client()->SetExpectIceRestart(true);
2266 initializing_client()->IceRestart();
2267
2268 // Negotiate and wait for ice completion again and make sure audio and video
2269 // plays.
2270 LocalP2PTest();
2271
2272 // Create a SDP string of the first audio candidate for both clients again.
2273 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
2274 initializing_client()->pc()->local_description()->candidates(0);
2275 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
2276 receiving_client()->pc()->local_description()->candidates(0);
2277 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
2278 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
2279 std::string initiator_candidate_restart;
2280 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
2281 &initiator_candidate_restart));
2282 std::string receiver_candidate_restart;
2283 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
2284 &receiver_candidate_restart));
2285
2286 // Verify that the first candidates in the local session descriptions has
2287 // changed.
2288 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
2289 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
2290}
2291
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07002292TEST_F(P2PTestConductor, IceRenominationDisabled) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002293 PeerConnectionInterface::RTCConfiguration config;
2294 config.enable_ice_renomination = false;
2295 ASSERT_TRUE(CreateTestClients(config, config));
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07002296 LocalP2PTest();
2297
2298 initializing_client()->VerifyLocalIceRenomination();
2299 receiving_client()->VerifyLocalIceRenomination();
2300 initializing_client()->VerifyRemoteIceRenomination();
2301 receiving_client()->VerifyRemoteIceRenomination();
2302}
2303
2304TEST_F(P2PTestConductor, IceRenominationEnabled) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002305 PeerConnectionInterface::RTCConfiguration config;
2306 config.enable_ice_renomination = true;
2307 ASSERT_TRUE(CreateTestClients(config, config));
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07002308 initializing_client()->SetExpectIceRenomination(true);
2309 initializing_client()->SetExpectRemoteIceRenomination(true);
2310 receiving_client()->SetExpectIceRenomination(true);
2311 receiving_client()->SetExpectRemoteIceRenomination(true);
2312 LocalP2PTest();
2313
2314 initializing_client()->VerifyLocalIceRenomination();
2315 receiving_client()->VerifyLocalIceRenomination();
2316 initializing_client()->VerifyRemoteIceRenomination();
2317 receiving_client()->VerifyRemoteIceRenomination();
2318}
2319
deadbeeffaac4972015-11-12 15:33:07 -08002320// This test sets up a call between two parties with audio, and video.
2321// It then renegotiates setting the video m-line to "port 0", then later
2322// renegotiates again, enabling video.
deadbeef7c73bdb2015-12-10 15:10:44 -08002323TEST_F(P2PTestConductor, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08002324 ASSERT_TRUE(CreateTestClients());
2325
2326 // Do initial negotiation. Will result in video and audio sendonly m-lines.
2327 receiving_client()->set_auto_add_stream(false);
2328 initializing_client()->AddMediaStream(true, true);
2329 initializing_client()->Negotiate();
2330
2331 // Negotiate again, disabling the video m-line (receiving client will
2332 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
2333 receiving_client()->SetReceiveVideo(false);
2334 initializing_client()->Negotiate();
2335
2336 // Enable video and do negotiation again, making sure video is received
2337 // end-to-end.
2338 receiving_client()->SetReceiveVideo(true);
2339 receiving_client()->AddMediaStream(true, true);
2340 LocalP2PTest();
2341}
2342
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002343// This test sets up a Jsep call between two parties with external
2344// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00002345// TODO(holmer): Disabled due to sometimes crashing on buildbots.
2346// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08002347TEST_F(P2PTestConductor, DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002348 ASSERT_TRUE(CreateTestClients());
2349 EnableVideoDecoderFactory();
2350 LocalP2PTest();
2351}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00002352
deadbeeffac06552015-11-25 11:26:01 -08002353// This tests that if we negotiate after calling CreateSender but before we
2354// have a track, then set a track later, frames from the newly-set track are
2355// received end-to-end.
deadbeef7c73bdb2015-12-10 15:10:44 -08002356TEST_F(P2PTestConductor, EarlyWarmupTest) {
deadbeeffac06552015-11-25 11:26:01 -08002357 ASSERT_TRUE(CreateTestClients());
deadbeefbd7d8f72015-12-18 16:58:44 -08002358 auto audio_sender =
2359 initializing_client()->pc()->CreateSender("audio", "stream_id");
2360 auto video_sender =
2361 initializing_client()->pc()->CreateSender("video", "stream_id");
deadbeeffac06552015-11-25 11:26:01 -08002362 initializing_client()->Negotiate();
2363 // Wait for ICE connection to complete, without any tracks.
2364 // Note that the receiving client WILL (in HandleIncomingOffer) create
2365 // tracks, so it's only the initiator here that's doing early warmup.
2366 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
2367 VerifySessionDescriptions();
2368 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2369 initializing_client()->ice_connection_state(),
2370 kMaxWaitForFramesMs);
2371 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2372 receiving_client()->ice_connection_state(),
2373 kMaxWaitForFramesMs);
2374 // Now set the tracks, and expect frames to immediately start flowing.
2375 EXPECT_TRUE(
2376 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
2377 EXPECT_TRUE(
2378 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
hta6b4f8392016-03-10 00:24:31 -08002379 EXPECT_TRUE_WAIT(FramesHaveArrived(kEndAudioFrameCount, kEndVideoFrameCount),
deadbeeffac06552015-11-25 11:26:01 -08002380 kMaxWaitForFramesMs);
2381}
2382
zhihuang9763d562016-08-05 11:14:50 -07002383#ifdef HAVE_QUIC
2384// This test sets up a call between two parties using QUIC instead of DTLS for
2385// audio and video, and a QUIC data channel.
2386TEST_F(P2PTestConductor, LocalP2PTestQuicDataChannel) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002387 PeerConnectionInterface::RTCConfiguration quic_config;
2388 quic_config.enable_quic = true;
2389 ASSERT_TRUE(CreateTestClients(quic_config, quic_config));
zhihuang9763d562016-08-05 11:14:50 -07002390 webrtc::DataChannelInit init;
2391 init.ordered = false;
2392 init.reliable = true;
2393 init.id = 1;
2394 initializing_client()->CreateDataChannel(&init);
2395 receiving_client()->CreateDataChannel(&init);
2396 LocalP2PTest();
2397 ASSERT_NE(nullptr, initializing_client()->data_channel());
2398 ASSERT_NE(nullptr, receiving_client()->data_channel());
2399 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2400 kMaxWaitMs);
2401 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
2402
2403 std::string data = "hello world";
2404
2405 initializing_client()->data_channel()->Send(DataBuffer(data));
2406 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
2407 kMaxWaitMs);
2408
2409 receiving_client()->data_channel()->Send(DataBuffer(data));
2410 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
2411 kMaxWaitMs);
2412}
2413
2414// Tests that negotiation of QUIC data channels is completed without error.
2415TEST_F(P2PTestConductor, NegotiateQuicDataChannel) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002416 PeerConnectionInterface::RTCConfiguration quic_config;
2417 quic_config.enable_quic = true;
2418 ASSERT_TRUE(CreateTestClients(quic_config, quic_config));
zhihuang9763d562016-08-05 11:14:50 -07002419 FakeConstraints constraints;
2420 constraints.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp, true);
2421 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
2422 webrtc::DataChannelInit init;
2423 init.ordered = false;
2424 init.reliable = true;
2425 init.id = 1;
2426 initializing_client()->CreateDataChannel(&init);
2427 initializing_client()->Negotiate(false, false);
2428}
2429
2430// This test sets up a JSEP call using QUIC. The callee only receives video.
2431TEST_F(P2PTestConductor, LocalP2PTestVideoOnlyWithQuic) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002432 PeerConnectionInterface::RTCConfiguration quic_config;
2433 quic_config.enable_quic = true;
2434 ASSERT_TRUE(CreateTestClients(quic_config, quic_config));
zhihuang9763d562016-08-05 11:14:50 -07002435 receiving_client()->SetReceiveAudioVideo(false, true);
2436 LocalP2PTest();
2437}
2438
2439// This test sets up a JSEP call using QUIC. The callee only receives audio.
2440TEST_F(P2PTestConductor, LocalP2PTestAudioOnlyWithQuic) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002441 PeerConnectionInterface::RTCConfiguration quic_config;
2442 quic_config.enable_quic = true;
2443 ASSERT_TRUE(CreateTestClients(quic_config, quic_config));
zhihuang9763d562016-08-05 11:14:50 -07002444 receiving_client()->SetReceiveAudioVideo(true, false);
2445 LocalP2PTest();
2446}
2447
2448// This test sets up a JSEP call using QUIC. The callee rejects both audio and
2449// video.
2450TEST_F(P2PTestConductor, LocalP2PTestNoVideoAudioWithQuic) {
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002451 PeerConnectionInterface::RTCConfiguration quic_config;
2452 quic_config.enable_quic = true;
2453 ASSERT_TRUE(CreateTestClients(quic_config, quic_config));
zhihuang9763d562016-08-05 11:14:50 -07002454 receiving_client()->SetReceiveAudioVideo(false, false);
2455 LocalP2PTest();
2456}
2457
2458#endif // HAVE_QUIC
2459
nissed98cf1f2016-04-22 07:27:36 -07002460TEST_F(P2PTestConductor, ForwardVideoOnlyStream) {
2461 ASSERT_TRUE(CreateTestClients());
2462 // One-way stream
2463 receiving_client()->set_auto_add_stream(false);
2464 // Video only, audio forwarding not expected to work.
2465 initializing_client()->AddMediaStream(false, true);
2466 initializing_client()->Negotiate();
2467
2468 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
2469 VerifySessionDescriptions();
2470
2471 ASSERT_TRUE(initializing_client()->can_receive_video());
2472 ASSERT_TRUE(receiving_client()->can_receive_video());
2473
2474 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2475 initializing_client()->ice_connection_state(),
2476 kMaxWaitForFramesMs);
2477 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2478 receiving_client()->ice_connection_state(),
2479 kMaxWaitForFramesMs);
2480
2481 ASSERT_TRUE(receiving_client()->remote_streams()->count() == 1);
2482
2483 // Echo the stream back.
2484 receiving_client()->pc()->AddStream(
2485 receiving_client()->remote_streams()->at(0));
2486 receiving_client()->Negotiate();
2487
2488 EXPECT_TRUE_WAIT(
2489 initializing_client()->VideoFramesReceivedCheck(kEndVideoFrameCount),
2490 kMaxWaitForFramesMs);
2491}
2492
Taylor Brandstettere5835f52016-09-16 15:07:50 -07002493// Test that we achieve the expected end-to-end connection time, using a
2494// fake clock and simulated latency on the media and signaling paths.
2495// We use a TURN<->TURN connection because this is usually the quickest to
2496// set up initially, especially when we're confident the connection will work
2497// and can start sending media before we get a STUN response.
2498//
2499// With various optimizations enabled, here are the network delays we expect to
2500// be on the critical path:
2501// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
2502// signaling answer (with DTLS fingerprint).
2503// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
2504// using TURN<->TURN pair, and DTLS exchange is 4 packets,
2505// the first of which should have arrived before the answer.
2506TEST_F(P2PTestConductor, EndToEndConnectionTimeWithTurnTurnPair) {
2507 rtc::ScopedFakeClock fake_clock;
2508 // Some things use a time of "0" as a special value, so we need to start out
2509 // the fake clock at a nonzero time.
2510 // TODO(deadbeef): Fix this.
2511 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
2512
2513 static constexpr int media_hop_delay_ms = 50;
2514 static constexpr int signaling_trip_delay_ms = 500;
2515 // For explanation of these values, see comment above.
2516 static constexpr int required_media_hops = 9;
2517 static constexpr int required_signaling_trips = 2;
2518 // For internal delays (such as posting an event asychronously).
2519 static constexpr int allowed_internal_delay_ms = 20;
2520 static constexpr int total_connection_time_ms =
2521 media_hop_delay_ms * required_media_hops +
2522 signaling_trip_delay_ms * required_signaling_trips +
2523 allowed_internal_delay_ms;
2524
2525 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
2526 3478};
2527 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
2528 0};
2529 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
2530 3478};
2531 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
2532 0};
2533 cricket::TestTurnServer turn_server_1(network_thread(),
2534 turn_server_1_internal_address,
2535 turn_server_1_external_address);
2536 cricket::TestTurnServer turn_server_2(network_thread(),
2537 turn_server_2_internal_address,
2538 turn_server_2_external_address);
2539 // Bypass permission check on received packets so media can be sent before
2540 // the candidate is signaled.
2541 turn_server_1.set_enable_permission_checks(false);
2542 turn_server_2.set_enable_permission_checks(false);
2543
2544 PeerConnectionInterface::RTCConfiguration client_1_config;
2545 webrtc::PeerConnectionInterface::IceServer ice_server_1;
2546 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
2547 ice_server_1.username = "test";
2548 ice_server_1.password = "test";
2549 client_1_config.servers.push_back(ice_server_1);
2550 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
2551 client_1_config.presume_writable_when_fully_relayed = true;
2552
2553 PeerConnectionInterface::RTCConfiguration client_2_config;
2554 webrtc::PeerConnectionInterface::IceServer ice_server_2;
2555 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
2556 ice_server_2.username = "test";
2557 ice_server_2.password = "test";
2558 client_2_config.servers.push_back(ice_server_2);
2559 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
2560 client_2_config.presume_writable_when_fully_relayed = true;
2561
2562 ASSERT_TRUE(CreateTestClients(client_1_config, client_2_config));
2563 // Set up the simulated delays.
2564 SetSignalingDelayMs(signaling_trip_delay_ms);
2565 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
2566 virtual_socket_server()->UpdateDelayDistribution();
2567
2568 initializing_client()->SetOfferToReceiveAudioVideo(true, true);
2569 initializing_client()->Negotiate();
2570 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
2571 // are connected. This is an important distinction. Once we have separate ICE
2572 // and DTLS state, this check needs to use the DTLS state.
2573 EXPECT_TRUE_SIMULATED_WAIT(
2574 (receiving_client()->ice_connection_state() ==
2575 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
2576 receiving_client()->ice_connection_state() ==
2577 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
2578 (initializing_client()->ice_connection_state() ==
2579 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
2580 initializing_client()->ice_connection_state() ==
2581 webrtc::PeerConnectionInterface::kIceConnectionCompleted),
2582 total_connection_time_ms, fake_clock);
2583 // Need to free the clients here since they're using things we created on
2584 // the stack.
2585 delete set_initializing_client(nullptr);
2586 delete set_receiving_client(nullptr);
2587}
2588
deadbeef0a6c4ca2015-10-06 11:38:28 -07002589class IceServerParsingTest : public testing::Test {
2590 public:
2591 // Convenience for parsing a single URL.
2592 bool ParseUrl(const std::string& url) {
2593 return ParseUrl(url, std::string(), std::string());
2594 }
2595
2596 bool ParseUrl(const std::string& url,
2597 const std::string& username,
2598 const std::string& password) {
2599 PeerConnectionInterface::IceServers servers;
2600 PeerConnectionInterface::IceServer server;
2601 server.urls.push_back(url);
2602 server.username = username;
2603 server.password = password;
2604 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002605 return webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002606 }
2607
2608 protected:
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002609 cricket::ServerAddresses stun_servers_;
2610 std::vector<cricket::RelayServerConfig> turn_servers_;
deadbeef0a6c4ca2015-10-06 11:38:28 -07002611};
2612
2613// Make sure all STUN/TURN prefixes are parsed correctly.
2614TEST_F(IceServerParsingTest, ParseStunPrefixes) {
2615 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002616 EXPECT_EQ(1U, stun_servers_.size());
2617 EXPECT_EQ(0U, turn_servers_.size());
2618 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002619
2620 EXPECT_TRUE(ParseUrl("stuns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002621 EXPECT_EQ(1U, stun_servers_.size());
2622 EXPECT_EQ(0U, turn_servers_.size());
2623 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002624
2625 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002626 EXPECT_EQ(0U, stun_servers_.size());
2627 EXPECT_EQ(1U, turn_servers_.size());
2628 EXPECT_FALSE(turn_servers_[0].ports[0].secure);
2629 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002630
2631 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002632 EXPECT_EQ(0U, stun_servers_.size());
2633 EXPECT_EQ(1U, turn_servers_.size());
2634 EXPECT_TRUE(turn_servers_[0].ports[0].secure);
2635 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002636
2637 // invalid prefixes
2638 EXPECT_FALSE(ParseUrl("stunn:hostname"));
2639 EXPECT_FALSE(ParseUrl(":hostname"));
2640 EXPECT_FALSE(ParseUrl(":"));
2641 EXPECT_FALSE(ParseUrl(""));
2642}
2643
2644TEST_F(IceServerParsingTest, VerifyDefaults) {
2645 // TURNS defaults
2646 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002647 EXPECT_EQ(1U, turn_servers_.size());
2648 EXPECT_EQ(5349, turn_servers_[0].ports[0].address.port());
2649 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
2650 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002651
2652 // TURN defaults
2653 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002654 EXPECT_EQ(1U, turn_servers_.size());
2655 EXPECT_EQ(3478, turn_servers_[0].ports[0].address.port());
2656 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2657 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002658
2659 // STUN defaults
2660 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002661 EXPECT_EQ(1U, stun_servers_.size());
2662 EXPECT_EQ(3478, stun_servers_.begin()->port());
2663 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002664}
2665
2666// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
2667// can be parsed correctly.
2668TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
2669 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002670 EXPECT_EQ(1U, stun_servers_.size());
2671 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2672 EXPECT_EQ(1234, stun_servers_.begin()->port());
2673 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002674
2675 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002676 EXPECT_EQ(1U, stun_servers_.size());
2677 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2678 EXPECT_EQ(4321, stun_servers_.begin()->port());
2679 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002680
2681 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002682 EXPECT_EQ(1U, stun_servers_.size());
2683 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2684 EXPECT_EQ(9999, stun_servers_.begin()->port());
2685 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002686
2687 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002688 EXPECT_EQ(1U, stun_servers_.size());
2689 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2690 EXPECT_EQ(3478, stun_servers_.begin()->port());
2691 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002692
2693 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002694 EXPECT_EQ(1U, stun_servers_.size());
2695 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2696 EXPECT_EQ(3478, stun_servers_.begin()->port());
2697 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002698
2699 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002700 EXPECT_EQ(1U, stun_servers_.size());
2701 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2702 EXPECT_EQ(3478, stun_servers_.begin()->port());
2703 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002704
2705 // Try some invalid hostname:port strings.
2706 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
2707 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002708 EXPECT_FALSE(ParseUrl("stun:hostname:port:more"));
2709 EXPECT_FALSE(ParseUrl("stun:hostname:port more"));
deadbeef0a6c4ca2015-10-06 11:38:28 -07002710 EXPECT_FALSE(ParseUrl("stun:hostname:"));
2711 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
2712 EXPECT_FALSE(ParseUrl("stun::5555"));
2713 EXPECT_FALSE(ParseUrl("stun:"));
2714}
2715
2716// Test parsing the "?transport=xxx" part of the URL.
2717TEST_F(IceServerParsingTest, ParseTransport) {
2718 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002719 EXPECT_EQ(1U, turn_servers_.size());
2720 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
2721 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002722
2723 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002724 EXPECT_EQ(1U, turn_servers_.size());
2725 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2726 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002727
2728 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
2729}
2730
2731// Test parsing ICE username contained in URL.
2732TEST_F(IceServerParsingTest, ParseUsername) {
2733 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002734 EXPECT_EQ(1U, turn_servers_.size());
2735 EXPECT_EQ("user", turn_servers_[0].credentials.username);
2736 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002737
2738 EXPECT_FALSE(ParseUrl("turn:@hostname"));
2739 EXPECT_FALSE(ParseUrl("turn:username@"));
2740 EXPECT_FALSE(ParseUrl("turn:@"));
2741 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
2742}
2743
2744// Test that username and password from IceServer is copied into the resulting
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002745// RelayServerConfig.
deadbeef0a6c4ca2015-10-06 11:38:28 -07002746TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
2747 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002748 EXPECT_EQ(1U, turn_servers_.size());
2749 EXPECT_EQ("username", turn_servers_[0].credentials.username);
2750 EXPECT_EQ("password", turn_servers_[0].credentials.password);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002751}
2752
2753// Ensure that if a server has multiple URLs, each one is parsed.
2754TEST_F(IceServerParsingTest, ParseMultipleUrls) {
2755 PeerConnectionInterface::IceServers servers;
2756 PeerConnectionInterface::IceServer server;
2757 server.urls.push_back("stun:hostname");
2758 server.urls.push_back("turn:hostname");
2759 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002760 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2761 EXPECT_EQ(1U, stun_servers_.size());
2762 EXPECT_EQ(1U, turn_servers_.size());
deadbeef0a6c4ca2015-10-06 11:38:28 -07002763}
2764
Taylor Brandstetter893505d2016-01-07 15:12:48 -08002765// Ensure that TURN servers are given unique priorities,
2766// so that their resulting candidates have unique priorities.
2767TEST_F(IceServerParsingTest, TurnServerPrioritiesUnique) {
2768 PeerConnectionInterface::IceServers servers;
2769 PeerConnectionInterface::IceServer server;
2770 server.urls.push_back("turn:hostname");
2771 server.urls.push_back("turn:hostname2");
2772 servers.push_back(server);
2773 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2774 EXPECT_EQ(2U, turn_servers_.size());
2775 EXPECT_NE(turn_servers_[0].priority, turn_servers_[1].priority);
2776}
2777
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00002778#endif // if !defined(THREAD_SANITIZER)
hta6b4f8392016-03-10 00:24:31 -08002779
2780} // namespace