blob: 2b4e3e3e3b1b06eff3f98cdc8e7e5d9f7280699a [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
11#include <stdio.h>
12
13#include <algorithm>
14#include <list>
15#include <map>
kwiberg0eb15ed2015-12-17 03:04:15 -080016#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include <vector>
18
Henrik Kjellander15583c12016-02-10 10:53:12 +010019#include "webrtc/api/dtmfsender.h"
20#include "webrtc/api/fakemetricsobserver.h"
21#include "webrtc/api/localaudiosource.h"
22#include "webrtc/api/mediastreaminterface.h"
23#include "webrtc/api/peerconnection.h"
24#include "webrtc/api/peerconnectionfactory.h"
25#include "webrtc/api/peerconnectioninterface.h"
26#include "webrtc/api/test/fakeaudiocapturemodule.h"
27#include "webrtc/api/test/fakeconstraints.h"
28#include "webrtc/api/test/fakedtlsidentitystore.h"
29#include "webrtc/api/test/fakeperiodicvideocapturer.h"
30#include "webrtc/api/test/fakevideotrackrenderer.h"
31#include "webrtc/api/test/mockpeerconnectionobservers.h"
32#include "webrtc/api/videosourceinterface.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000033#include "webrtc/base/gunit.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000034#include "webrtc/base/physicalsocketserver.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000035#include "webrtc/base/scoped_ptr.h"
36#include "webrtc/base/ssladapter.h"
37#include "webrtc/base/sslstreamadapter.h"
38#include "webrtc/base/thread.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000039#include "webrtc/base/virtualsocketserver.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010040#include "webrtc/media/engine/fakewebrtcvideoengine.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000041#include "webrtc/p2p/base/constants.h"
42#include "webrtc/p2p/base/sessiondescription.h"
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080043#include "webrtc/p2p/client/fakeportallocator.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010044#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045
46#define MAYBE_SKIP_TEST(feature) \
47 if (!(feature())) { \
48 LOG(LS_INFO) << "Feature disabled... skipping"; \
49 return; \
50 }
51
52using cricket::ContentInfo;
53using cricket::FakeWebRtcVideoDecoder;
54using cricket::FakeWebRtcVideoDecoderFactory;
55using cricket::FakeWebRtcVideoEncoder;
56using cricket::FakeWebRtcVideoEncoderFactory;
57using cricket::MediaContentDescription;
58using webrtc::DataBuffer;
59using webrtc::DataChannelInterface;
60using webrtc::DtmfSender;
61using webrtc::DtmfSenderInterface;
62using webrtc::DtmfSenderObserverInterface;
63using webrtc::FakeConstraints;
64using webrtc::MediaConstraintsInterface;
deadbeeffaac4972015-11-12 15:33:07 -080065using webrtc::MediaStreamInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066using webrtc::MediaStreamTrackInterface;
67using webrtc::MockCreateSessionDescriptionObserver;
68using webrtc::MockDataChannelObserver;
69using webrtc::MockSetSessionDescriptionObserver;
70using webrtc::MockStatsObserver;
deadbeeffaac4972015-11-12 15:33:07 -080071using webrtc::ObserverInterface;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000072using webrtc::PeerConnectionInterface;
Joachim Bauch04e5b492015-05-29 09:40:39 +020073using webrtc::PeerConnectionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074using webrtc::SessionDescriptionInterface;
75using webrtc::StreamCollectionInterface;
76
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000077static const int kMaxWaitMs = 10000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000078// Disable for TSan v2, see
79// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
80// This declaration is also #ifdef'd as it causes uninitialized-variable
81// warnings.
82#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083static const int kMaxWaitForStatsMs = 3000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000084#endif
deadbeeffac06552015-11-25 11:26:01 -080085static const int kMaxWaitForActivationMs = 5000;
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000086static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087static const int kEndAudioFrameCount = 3;
88static const int kEndVideoFrameCount = 3;
89
90static const char kStreamLabelBase[] = "stream_label";
91static const char kVideoTrackLabelBase[] = "video_track";
92static const char kAudioTrackLabelBase[] = "audio_track";
93static const char kDataChannelLabel[] = "data_channel";
94
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +000095// Disable for TSan v2, see
96// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
97// This declaration is also #ifdef'd as it causes unused-variable errors.
98#if !defined(THREAD_SANITIZER)
99// SRTP cipher name negotiated by the tests. This must be updated if the
100// default changes.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800101static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000102#endif
103
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104static void RemoveLinesFromSdp(const std::string& line_start,
105 std::string* sdp) {
106 const char kSdpLineEnd[] = "\r\n";
107 size_t ssrc_pos = 0;
108 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
109 std::string::npos) {
110 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
111 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
112 }
113}
114
115class SignalingMessageReceiver {
116 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 virtual void ReceiveSdpMessage(const std::string& type,
118 std::string& msg) = 0;
119 virtual void ReceiveIceMessage(const std::string& sdp_mid,
120 int sdp_mline_index,
121 const std::string& msg) = 0;
122
123 protected:
deadbeefaf1b59c2015-10-15 12:08:41 -0700124 SignalingMessageReceiver() {}
125 virtual ~SignalingMessageReceiver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126};
127
deadbeefaf1b59c2015-10-15 12:08:41 -0700128class PeerConnectionTestClient : public webrtc::PeerConnectionObserver,
deadbeeffaac4972015-11-12 15:33:07 -0800129 public SignalingMessageReceiver,
130 public ObserverInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 public:
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800132 static PeerConnectionTestClient* CreateClientWithDtlsIdentityStore(
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800133 const std::string& id,
134 const MediaConstraintsInterface* constraints,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800135 const PeerConnectionFactory::Options* options,
136 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800137 PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
kwiberg0eb15ed2015-12-17 03:04:15 -0800138 if (!client->Init(constraints, options, std::move(dtls_identity_store))) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800139 delete client;
140 return nullptr;
141 }
142 return client;
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800143 }
144
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800145 static PeerConnectionTestClient* CreateClient(
146 const std::string& id,
147 const MediaConstraintsInterface* constraints,
148 const PeerConnectionFactory::Options* options) {
149 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
150 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
151 : nullptr);
152
153 return CreateClientWithDtlsIdentityStore(id, constraints, options,
kwiberg0eb15ed2015-12-17 03:04:15 -0800154 std::move(dtls_identity_store));
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800155 }
156
deadbeefaf1b59c2015-10-15 12:08:41 -0700157 ~PeerConnectionTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 }
159
deadbeefaf1b59c2015-10-15 12:08:41 -0700160 void Negotiate() { Negotiate(true, true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161
deadbeefaf1b59c2015-10-15 12:08:41 -0700162 void Negotiate(bool audio, bool video) {
163 rtc::scoped_ptr<SessionDescriptionInterface> offer;
164 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165
deadbeefaf1b59c2015-10-15 12:08:41 -0700166 if (offer->description()->GetContentByName("audio")) {
167 offer->description()->GetContentByName("audio")->rejected = !audio;
168 }
169 if (offer->description()->GetContentByName("video")) {
170 offer->description()->GetContentByName("video")->rejected = !video;
171 }
172
173 std::string sdp;
174 EXPECT_TRUE(offer->ToString(&sdp));
175 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
176 signaling_message_receiver_->ReceiveSdpMessage(
177 webrtc::SessionDescriptionInterface::kOffer, sdp);
178 }
179
180 // SignalingMessageReceiver callback.
181 void ReceiveSdpMessage(const std::string& type, std::string& msg) override {
182 FilterIncomingSdpMessage(&msg);
183 if (type == webrtc::SessionDescriptionInterface::kOffer) {
184 HandleIncomingOffer(msg);
185 } else {
186 HandleIncomingAnswer(msg);
187 }
188 }
189
190 // SignalingMessageReceiver callback.
191 void ReceiveIceMessage(const std::string& sdp_mid,
192 int sdp_mline_index,
193 const std::string& msg) override {
194 LOG(INFO) << id_ << "ReceiveIceMessage";
195 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
196 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
197 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
198 }
199
200 // PeerConnectionObserver callbacks.
201 void OnSignalingChange(
202 webrtc::PeerConnectionInterface::SignalingState new_state) override {
203 EXPECT_EQ(pc()->signaling_state(), new_state);
204 }
deadbeeffaac4972015-11-12 15:33:07 -0800205 void OnAddStream(MediaStreamInterface* media_stream) override {
206 media_stream->RegisterObserver(this);
deadbeefaf1b59c2015-10-15 12:08:41 -0700207 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
208 const std::string id = media_stream->GetVideoTracks()[i]->id();
209 ASSERT_TRUE(fake_video_renderers_.find(id) ==
210 fake_video_renderers_.end());
deadbeefc9be0072015-12-14 18:27:57 -0800211 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
212 media_stream->GetVideoTracks()[i]));
deadbeefaf1b59c2015-10-15 12:08:41 -0700213 }
214 }
deadbeeffaac4972015-11-12 15:33:07 -0800215 void OnRemoveStream(MediaStreamInterface* media_stream) override {}
deadbeefaf1b59c2015-10-15 12:08:41 -0700216 void OnRenegotiationNeeded() override {}
217 void OnIceConnectionChange(
218 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
219 EXPECT_EQ(pc()->ice_connection_state(), new_state);
220 }
221 void OnIceGatheringChange(
222 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
223 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
224 }
225 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
226 LOG(INFO) << id_ << "OnIceCandidate";
227
228 std::string ice_sdp;
229 EXPECT_TRUE(candidate->ToString(&ice_sdp));
230 if (signaling_message_receiver_ == nullptr) {
231 // Remote party may be deleted.
232 return;
233 }
234 signaling_message_receiver_->ReceiveIceMessage(
235 candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
236 }
237
deadbeeffaac4972015-11-12 15:33:07 -0800238 // MediaStreamInterface callback
239 void OnChanged() override {
240 // Track added or removed from MediaStream, so update our renderers.
241 rtc::scoped_refptr<StreamCollectionInterface> remote_streams =
242 pc()->remote_streams();
243 // Remove renderers for tracks that were removed.
244 for (auto it = fake_video_renderers_.begin();
245 it != fake_video_renderers_.end();) {
246 if (remote_streams->FindVideoTrack(it->first) == nullptr) {
deadbeefc9be0072015-12-14 18:27:57 -0800247 auto to_remove = it++;
248 removed_fake_video_renderers_.push_back(std::move(to_remove->second));
249 fake_video_renderers_.erase(to_remove);
deadbeeffaac4972015-11-12 15:33:07 -0800250 } else {
251 ++it;
252 }
253 }
254 // Create renderers for new video tracks.
255 for (size_t stream_index = 0; stream_index < remote_streams->count();
256 ++stream_index) {
257 MediaStreamInterface* remote_stream = remote_streams->at(stream_index);
258 for (size_t track_index = 0;
259 track_index < remote_stream->GetVideoTracks().size();
260 ++track_index) {
261 const std::string id =
262 remote_stream->GetVideoTracks()[track_index]->id();
263 if (fake_video_renderers_.find(id) != fake_video_renderers_.end()) {
264 continue;
265 }
deadbeefc9be0072015-12-14 18:27:57 -0800266 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
267 remote_stream->GetVideoTracks()[track_index]));
deadbeeffaac4972015-11-12 15:33:07 -0800268 }
269 }
270 }
271
deadbeefaf1b59c2015-10-15 12:08:41 -0700272 void SetVideoConstraints(const webrtc::FakeConstraints& video_constraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 video_constraints_ = video_constraint;
274 }
275
276 void AddMediaStream(bool audio, bool video) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700277 std::string stream_label =
278 kStreamLabelBase +
279 rtc::ToString<int>(static_cast<int>(pc()->local_streams()->count()));
deadbeeffaac4972015-11-12 15:33:07 -0800280 rtc::scoped_refptr<MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000281 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282
283 if (audio && can_receive_audio()) {
deadbeeffac06552015-11-25 11:26:01 -0800284 stream->AddTrack(CreateLocalAudioTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285 }
286 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000287 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288 }
289
deadbeefaf1b59c2015-10-15 12:08:41 -0700290 EXPECT_TRUE(pc()->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 }
292
deadbeefaf1b59c2015-10-15 12:08:41 -0700293 size_t NumberOfLocalMediaStreams() { return pc()->local_streams()->count(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294
295 bool SessionActive() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700296 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 }
298
deadbeeffaac4972015-11-12 15:33:07 -0800299 // Automatically add a stream when receiving an offer, if we don't have one.
300 // Defaults to true.
301 void set_auto_add_stream(bool auto_add_stream) {
302 auto_add_stream_ = auto_add_stream;
303 }
304
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305 void set_signaling_message_receiver(
deadbeefaf1b59c2015-10-15 12:08:41 -0700306 SignalingMessageReceiver* signaling_message_receiver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307 signaling_message_receiver_ = signaling_message_receiver;
308 }
309
310 void EnableVideoDecoderFactory() {
311 video_decoder_factory_enabled_ = true;
312 fake_video_decoder_factory_->AddSupportedVideoCodecType(
313 webrtc::kVideoCodecVP8);
314 }
315
deadbeefaf1b59c2015-10-15 12:08:41 -0700316 void IceRestart() {
317 session_description_constraints_.SetMandatoryIceRestart(true);
318 SetExpectIceRestart(true);
319 }
320
321 void SetExpectIceRestart(bool expect_restart) {
322 expect_ice_restart_ = expect_restart;
323 }
324
325 bool ExpectIceRestart() const { return expect_ice_restart_; }
326
327 void SetReceiveAudioVideo(bool audio, bool video) {
328 SetReceiveAudio(audio);
329 SetReceiveVideo(video);
330 ASSERT_EQ(audio, can_receive_audio());
331 ASSERT_EQ(video, can_receive_video());
332 }
333
334 void SetReceiveAudio(bool audio) {
335 if (audio && can_receive_audio())
336 return;
337 session_description_constraints_.SetMandatoryReceiveAudio(audio);
338 }
339
340 void SetReceiveVideo(bool video) {
341 if (video && can_receive_video())
342 return;
343 session_description_constraints_.SetMandatoryReceiveVideo(video);
344 }
345
346 void RemoveMsidFromReceivedSdp(bool remove) { remove_msid_ = remove; }
347
348 void RemoveSdesCryptoFromReceivedSdp(bool remove) { remove_sdes_ = remove; }
349
350 void RemoveBundleFromReceivedSdp(bool remove) { remove_bundle_ = remove; }
351
352 bool can_receive_audio() {
353 bool value;
354 if (webrtc::FindConstraint(&session_description_constraints_,
355 MediaConstraintsInterface::kOfferToReceiveAudio,
356 &value, nullptr)) {
357 return value;
358 }
359 return true;
360 }
361
362 bool can_receive_video() {
363 bool value;
364 if (webrtc::FindConstraint(&session_description_constraints_,
365 MediaConstraintsInterface::kOfferToReceiveVideo,
366 &value, nullptr)) {
367 return value;
368 }
369 return true;
370 }
371
deadbeefaf1b59c2015-10-15 12:08:41 -0700372 void OnDataChannel(DataChannelInterface* data_channel) override {
373 LOG(INFO) << id_ << "OnDataChannel";
374 data_channel_ = data_channel;
375 data_observer_.reset(new MockDataChannelObserver(data_channel));
376 }
377
378 void CreateDataChannel() {
379 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, nullptr);
380 ASSERT_TRUE(data_channel_.get() != nullptr);
381 data_observer_.reset(new MockDataChannelObserver(data_channel_));
382 }
383
deadbeeffac06552015-11-25 11:26:01 -0800384 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack(
385 const std::string& stream_label) {
386 FakeConstraints constraints;
387 // Disable highpass filter so that we can get all the test audio frames.
388 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
389 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
390 peer_connection_factory_->CreateAudioSource(&constraints);
391 // TODO(perkj): Test audio source when it is implemented. Currently audio
392 // always use the default input.
393 std::string label = stream_label + kAudioTrackLabelBase;
394 return peer_connection_factory_->CreateAudioTrack(label, source);
395 }
396
397 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack(
398 const std::string& stream_label) {
399 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
400 FakeConstraints source_constraints = video_constraints_;
401 source_constraints.SetMandatoryMaxFrameRate(10);
402
403 cricket::FakeVideoCapturer* fake_capturer =
404 new webrtc::FakePeriodicVideoCapturer();
405 video_capturers_.push_back(fake_capturer);
406 rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
407 peer_connection_factory_->CreateVideoSource(fake_capturer,
408 &source_constraints);
409 std::string label = stream_label + kVideoTrackLabelBase;
410 return peer_connection_factory_->CreateVideoTrack(label, source);
411 }
412
deadbeefaf1b59c2015-10-15 12:08:41 -0700413 DataChannelInterface* data_channel() { return data_channel_; }
414 const MockDataChannelObserver* data_observer() const {
415 return data_observer_.get();
416 }
417
418 webrtc::PeerConnectionInterface* pc() { return peer_connection_.get(); }
419
420 void StopVideoCapturers() {
421 for (std::vector<cricket::VideoCapturer*>::iterator it =
422 video_capturers_.begin();
423 it != video_capturers_.end(); ++it) {
424 (*it)->Stop();
425 }
426 }
427
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428 bool AudioFramesReceivedCheck(int number_of_frames) const {
429 return number_of_frames <= fake_audio_capture_module_->frames_received();
430 }
431
deadbeefc9be0072015-12-14 18:27:57 -0800432 int audio_frames_received() const {
433 return fake_audio_capture_module_->frames_received();
434 }
435
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 bool VideoFramesReceivedCheck(int number_of_frames) {
437 if (video_decoder_factory_enabled_) {
438 const std::vector<FakeWebRtcVideoDecoder*>& decoders
439 = fake_video_decoder_factory_->decoders();
440 if (decoders.empty()) {
441 return number_of_frames <= 0;
442 }
443
deadbeefc9be0072015-12-14 18:27:57 -0800444 for (FakeWebRtcVideoDecoder* decoder : decoders) {
445 if (number_of_frames > decoder->GetNumFramesReceived()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446 return false;
447 }
448 }
449 return true;
450 } else {
451 if (fake_video_renderers_.empty()) {
452 return number_of_frames <= 0;
453 }
454
deadbeefc9be0072015-12-14 18:27:57 -0800455 for (const auto& pair : fake_video_renderers_) {
456 if (number_of_frames > pair.second->num_rendered_frames()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457 return false;
458 }
459 }
460 return true;
461 }
462 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700463
deadbeefc9be0072015-12-14 18:27:57 -0800464 int video_frames_received() const {
465 int total = 0;
466 if (video_decoder_factory_enabled_) {
467 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
468 fake_video_decoder_factory_->decoders();
469 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
470 total += decoder->GetNumFramesReceived();
471 }
472 } else {
473 for (const auto& pair : fake_video_renderers_) {
474 total += pair.second->num_rendered_frames();
475 }
476 for (const auto& renderer : removed_fake_video_renderers_) {
477 total += renderer->num_rendered_frames();
478 }
479 }
480 return total;
481 }
482
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 // Verify the CreateDtmfSender interface
484 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000485 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
486 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487
488 // We can't create a DTMF sender with an invalid audio track or a non local
489 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700490 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000491 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700492 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
493 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494
495 // We should be able to create a DTMF sender from a local track.
496 webrtc::AudioTrackInterface* localtrack =
497 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
498 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700499 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500 dtmf_sender->RegisterObserver(observer.get());
501
502 // Test the DtmfSender object just created.
503 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
504 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
505
506 // We don't need to verify that the DTMF tones are actually sent out because
507 // that is already covered by the tests of the lower level components.
508
509 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
510 std::vector<std::string> tones;
511 tones.push_back("1");
512 tones.push_back("a");
513 tones.push_back("");
514 observer->Verify(tones);
515
516 dtmf_sender->UnregisterObserver();
517 }
518
519 // Verifies that the SessionDescription have rejected the appropriate media
520 // content.
521 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700522 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
523 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 const cricket::SessionDescription* remote_desc =
525 peer_connection_->remote_description()->description();
526 const cricket::SessionDescription* local_desc =
527 peer_connection_->local_description()->description();
528
529 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
530 if (remote_audio_content) {
531 const ContentInfo* audio_content =
532 GetFirstAudioContent(local_desc);
533 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
534 }
535
536 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
537 if (remote_video_content) {
538 const ContentInfo* video_content =
539 GetFirstVideoContent(local_desc);
540 EXPECT_EQ(can_receive_video(), !video_content->rejected);
541 }
542 }
543
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700545 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 const cricket::SessionDescription* desc =
547 peer_connection_->local_description()->description();
548 const cricket::ContentInfos& contents = desc->contents();
549
550 for (size_t index = 0; index < contents.size(); ++index) {
551 if (contents[index].rejected)
552 continue;
553 const cricket::TransportDescription* transport_desc =
554 desc->GetTransportDescriptionByName(contents[index].name);
555
556 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000557 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558 if (ufragpair_it == ice_ufrag_pwd_.end()) {
559 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000560 ice_ufrag_pwd_[static_cast<int>(index)] =
561 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562 } else if (ExpectIceRestart()) {
563 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
564 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
565 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
566 } else {
567 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
568 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
569 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
570 }
571 }
572 }
573
574 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000575 rtc::scoped_refptr<MockStatsObserver>
576 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000577 EXPECT_TRUE(peer_connection_->GetStats(
578 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700580 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 return observer->AudioOutputLevel();
582 }
583
584 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000585 rtc::scoped_refptr<MockStatsObserver>
586 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000587 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700588 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700590 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 return observer->AudioInputLevel();
592 }
593
594 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000595 rtc::scoped_refptr<MockStatsObserver>
596 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000597 EXPECT_TRUE(peer_connection_->GetStats(
598 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700600 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601 return observer->BytesReceived();
602 }
603
604 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000605 rtc::scoped_refptr<MockStatsObserver>
606 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000607 EXPECT_TRUE(peer_connection_->GetStats(
608 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700610 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611 return observer->BytesSent();
612 }
613
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000614 int GetAvailableReceivedBandwidthStats() {
615 rtc::scoped_refptr<MockStatsObserver>
616 observer(new rtc::RefCountedObject<MockStatsObserver>());
617 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700618 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000619 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700620 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000621 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000622 return bw;
623 }
624
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000625 std::string GetDtlsCipherStats() {
626 rtc::scoped_refptr<MockStatsObserver>
627 observer(new rtc::RefCountedObject<MockStatsObserver>());
628 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700629 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000630 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700631 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000632 return observer->DtlsCipher();
633 }
634
635 std::string GetSrtpCipherStats() {
636 rtc::scoped_refptr<MockStatsObserver>
637 observer(new rtc::RefCountedObject<MockStatsObserver>());
638 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700639 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000640 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700641 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000642 return observer->SrtpCipher();
643 }
644
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 int rendered_width() {
646 EXPECT_FALSE(fake_video_renderers_.empty());
647 return fake_video_renderers_.empty() ? 1 :
648 fake_video_renderers_.begin()->second->width();
649 }
650
651 int rendered_height() {
652 EXPECT_FALSE(fake_video_renderers_.empty());
653 return fake_video_renderers_.empty() ? 1 :
654 fake_video_renderers_.begin()->second->height();
655 }
656
657 size_t number_of_remote_streams() {
658 if (!pc())
659 return 0;
660 return pc()->remote_streams()->count();
661 }
662
663 StreamCollectionInterface* remote_streams() {
664 if (!pc()) {
665 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700666 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 }
668 return pc()->remote_streams();
669 }
670
671 StreamCollectionInterface* local_streams() {
672 if (!pc()) {
673 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700674 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000675 }
676 return pc()->local_streams();
677 }
678
679 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
680 return pc()->signaling_state();
681 }
682
683 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
684 return pc()->ice_connection_state();
685 }
686
687 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
688 return pc()->ice_gathering_state();
689 }
690
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 private:
692 class DummyDtmfObserver : public DtmfSenderObserverInterface {
693 public:
694 DummyDtmfObserver() : completed_(false) {}
695
696 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700697 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 tones_.push_back(tone);
699 if (tone.empty()) {
700 completed_ = true;
701 }
702 }
703
704 void Verify(const std::vector<std::string>& tones) const {
705 ASSERT_TRUE(tones_.size() == tones.size());
706 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
707 }
708
709 bool completed() const { return completed_; }
710
711 private:
712 bool completed_;
713 std::vector<std::string> tones_;
714 };
715
deadbeefaf1b59c2015-10-15 12:08:41 -0700716 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
717
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800718 bool Init(
719 const MediaConstraintsInterface* constraints,
720 const PeerConnectionFactory::Options* options,
721 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700722 EXPECT_TRUE(!peer_connection_);
723 EXPECT_TRUE(!peer_connection_factory_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800724 rtc::scoped_ptr<cricket::PortAllocator> port_allocator(
725 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
deadbeefaf1b59c2015-10-15 12:08:41 -0700726 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
727
728 if (fake_audio_capture_module_ == nullptr) {
729 return false;
730 }
731 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
732 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
733 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
734 rtc::Thread::Current(), rtc::Thread::Current(),
735 fake_audio_capture_module_, fake_video_encoder_factory_,
736 fake_video_decoder_factory_);
737 if (!peer_connection_factory_) {
738 return false;
739 }
740 if (options) {
741 peer_connection_factory_->SetOptions(*options);
742 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800743 peer_connection_ = CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800744 std::move(port_allocator), constraints, std::move(dtls_identity_store));
deadbeefaf1b59c2015-10-15 12:08:41 -0700745 return peer_connection_.get() != nullptr;
746 }
747
deadbeefaf1b59c2015-10-15 12:08:41 -0700748 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800749 rtc::scoped_ptr<cricket::PortAllocator> port_allocator,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800750 const MediaConstraintsInterface* constraints,
751 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800752 // CreatePeerConnection with RTCConfiguration.
753 webrtc::PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754 webrtc::PeerConnectionInterface::IceServer ice_server;
755 ice_server.uri = "stun:stun.l.google.com:19302";
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800756 config.servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000757
deadbeefaf1b59c2015-10-15 12:08:41 -0700758 return peer_connection_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800759 config, constraints, std::move(port_allocator),
760 std::move(dtls_identity_store), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761 }
762
763 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700764 LOG(INFO) << id_ << "HandleIncomingOffer ";
deadbeeffaac4972015-11-12 15:33:07 -0800765 if (NumberOfLocalMediaStreams() == 0 && auto_add_stream_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 // If we are not sending any streams ourselves it is time to add some.
767 AddMediaStream(true, true);
768 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000769 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700770 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000772 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 EXPECT_TRUE(DoCreateAnswer(answer.use()));
774 std::string sdp;
775 EXPECT_TRUE(answer->ToString(&sdp));
776 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
deadbeefaf1b59c2015-10-15 12:08:41 -0700777 if (signaling_message_receiver_) {
778 signaling_message_receiver_->ReceiveSdpMessage(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779 webrtc::SessionDescriptionInterface::kAnswer, sdp);
780 }
781 }
782
783 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700784 LOG(INFO) << id_ << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000785 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700786 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
788 }
789
790 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
791 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000792 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
793 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 MockCreateSessionDescriptionObserver>());
795 if (offer) {
796 pc()->CreateOffer(observer, &session_description_constraints_);
797 } else {
798 pc()->CreateAnswer(observer, &session_description_constraints_);
799 }
800 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
801 *desc = observer->release_desc();
802 if (observer->result() && ExpectIceRestart()) {
803 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
804 }
805 return observer->result();
806 }
807
808 bool DoCreateOffer(SessionDescriptionInterface** desc) {
809 return DoCreateOfferAnswer(desc, true);
810 }
811
812 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
813 return DoCreateOfferAnswer(desc, false);
814 }
815
816 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000817 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
818 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700820 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 pc()->SetLocalDescription(observer, desc);
822 // Ignore the observer result. If we wait for the result with
823 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
824 // before the offer which is an error.
825 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000826 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 // ProcessMessages waits at least 1ms but processes all messages before
828 // returning. Since this test is synchronous and send messages to the remote
829 // peer whenever a callback is invoked, this can lead to messages being
830 // sent to the remote peer in the wrong order.
831 // TODO(perkj): Find a way to check the result without risking that the
832 // order of sent messages are changed. Ex- by posting all messages that are
833 // sent to the remote peer.
834 return true;
835 }
836
837 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000838 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
839 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700841 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 pc()->SetRemoteDescription(observer, desc);
843 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
844 return observer->result();
845 }
846
847 // This modifies all received SDP messages before they are processed.
848 void FilterIncomingSdpMessage(std::string* sdp) {
849 if (remove_msid_) {
850 const char kSdpSsrcAttribute[] = "a=ssrc:";
851 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
852 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
853 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
854 }
855 if (remove_bundle_) {
856 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
857 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
858 }
859 if (remove_sdes_) {
860 const char kSdpSdesCryptoAttribute[] = "a=crypto";
861 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
862 }
863 }
864
deadbeefaf1b59c2015-10-15 12:08:41 -0700865 std::string id_;
866
deadbeefaf1b59c2015-10-15 12:08:41 -0700867 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
868 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
869 peer_connection_factory_;
870
deadbeeffaac4972015-11-12 15:33:07 -0800871 bool auto_add_stream_ = true;
872
deadbeefaf1b59c2015-10-15 12:08:41 -0700873 typedef std::pair<std::string, std::string> IceUfragPwdPair;
874 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
875 bool expect_ice_restart_ = false;
876
deadbeefc9be0072015-12-14 18:27:57 -0800877 // Needed to keep track of number of frames sent.
deadbeefaf1b59c2015-10-15 12:08:41 -0700878 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
879 // Needed to keep track of number of frames received.
deadbeefc9be0072015-12-14 18:27:57 -0800880 std::map<std::string, rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
881 fake_video_renderers_;
882 // Needed to ensure frames aren't received for removed tracks.
883 std::vector<rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
884 removed_fake_video_renderers_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700885 // Needed to keep track of number of frames received when external decoder
886 // used.
887 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
888 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
889 bool video_decoder_factory_enabled_ = false;
890 webrtc::FakeConstraints video_constraints_;
891
892 // For remote peer communication.
893 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
894
895 // Store references to the video capturers we've created, so that we can stop
896 // them, if required.
897 std::vector<cricket::VideoCapturer*> video_capturers_;
898
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899 webrtc::FakeConstraints session_description_constraints_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700900 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
901 bool remove_bundle_ =
902 false; // True if bundle should be removed in received SDP.
903 bool remove_sdes_ =
904 false; // True if a=crypto should be removed in received SDP.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000906 rtc::scoped_refptr<DataChannelInterface> data_channel_;
907 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908};
909
deadbeef7c73bdb2015-12-10 15:10:44 -0800910class P2PTestConductor : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 public:
deadbeef7c73bdb2015-12-10 15:10:44 -0800912 P2PTestConductor()
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000913 : pss_(new rtc::PhysicalSocketServer),
914 ss_(new rtc::VirtualSocketServer(pss_.get())),
915 ss_scope_(ss_.get()) {}
916
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 bool SessionActive() {
918 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000919 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000921
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 // Return true if the number of frames provided have been received or it is
923 // known that that will never occur (e.g. no frames will be sent or
924 // captured).
925 bool FramesNotPending(int audio_frames_to_receive,
926 int video_frames_to_receive) {
927 return VideoFramesReceivedCheck(video_frames_to_receive) &&
928 AudioFramesReceivedCheck(audio_frames_to_receive);
929 }
930 bool AudioFramesReceivedCheck(int frames_received) {
931 return initiating_client_->AudioFramesReceivedCheck(frames_received) &&
932 receiving_client_->AudioFramesReceivedCheck(frames_received);
933 }
934 bool VideoFramesReceivedCheck(int frames_received) {
935 return initiating_client_->VideoFramesReceivedCheck(frames_received) &&
936 receiving_client_->VideoFramesReceivedCheck(frames_received);
937 }
938 void VerifyDtmf() {
939 initiating_client_->VerifyDtmf();
940 receiving_client_->VerifyDtmf();
941 }
942
943 void TestUpdateOfferWithRejectedContent() {
deadbeefc9be0072015-12-14 18:27:57 -0800944 // Renegotiate, rejecting the video m-line.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945 initiating_client_->Negotiate(true, false);
deadbeefc9be0072015-12-14 18:27:57 -0800946 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
947
948 int pc1_audio_received = initiating_client_->audio_frames_received();
949 int pc1_video_received = initiating_client_->video_frames_received();
950 int pc2_audio_received = receiving_client_->audio_frames_received();
951 int pc2_video_received = receiving_client_->video_frames_received();
952
953 // Wait for some additional audio frames to be received.
954 EXPECT_TRUE_WAIT(initiating_client_->AudioFramesReceivedCheck(
955 pc1_audio_received + kEndAudioFrameCount) &&
956 receiving_client_->AudioFramesReceivedCheck(
957 pc2_audio_received + kEndAudioFrameCount),
958 kMaxWaitForFramesMs);
959
960 // During this time, we shouldn't have received any additional video frames
961 // for the rejected video tracks.
962 EXPECT_EQ(pc1_video_received, initiating_client_->video_frames_received());
963 EXPECT_EQ(pc2_video_received, receiving_client_->video_frames_received());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 }
965
966 void VerifyRenderedSize(int width, int height) {
967 EXPECT_EQ(width, receiving_client()->rendered_width());
968 EXPECT_EQ(height, receiving_client()->rendered_height());
969 EXPECT_EQ(width, initializing_client()->rendered_width());
970 EXPECT_EQ(height, initializing_client()->rendered_height());
971 }
972
973 void VerifySessionDescriptions() {
974 initiating_client_->VerifyRejectedMediaInSessionDescription();
975 receiving_client_->VerifyRejectedMediaInSessionDescription();
976 initiating_client_->VerifyLocalIceUfragAndPassword();
977 receiving_client_->VerifyLocalIceUfragAndPassword();
978 }
979
deadbeef7c73bdb2015-12-10 15:10:44 -0800980 ~P2PTestConductor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700982 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983 }
984 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700985 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000986 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987 }
988
deadbeefaf1b59c2015-10-15 12:08:41 -0700989 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000990
991 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
992 MediaConstraintsInterface* recv_constraints) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700993 return CreateTestClients(init_constraints, nullptr, recv_constraints,
994 nullptr);
Joachim Bauch04e5b492015-05-29 09:40:39 +0200995 }
996
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800997 void SetSignalingReceivers() {
998 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
999 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1000 }
1001
Joachim Bauch04e5b492015-05-29 09:40:39 +02001002 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1003 PeerConnectionFactory::Options* init_options,
1004 MediaConstraintsInterface* recv_constraints,
1005 PeerConnectionFactory::Options* recv_options) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001006 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
1007 "Caller: ", init_constraints, init_options));
1008 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
1009 "Callee: ", recv_constraints, recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001010 if (!initiating_client_ || !receiving_client_) {
1011 return false;
1012 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001013 SetSignalingReceivers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014 return true;
1015 }
1016
1017 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1018 const webrtc::FakeConstraints& recv_constraints) {
1019 initiating_client_->SetVideoConstraints(init_constraints);
1020 receiving_client_->SetVideoConstraints(recv_constraints);
1021 }
1022
1023 void EnableVideoDecoderFactory() {
1024 initiating_client_->EnableVideoDecoderFactory();
1025 receiving_client_->EnableVideoDecoderFactory();
1026 }
1027
1028 // This test sets up a call between two parties. Both parties send static
1029 // frames to each other. Once the test is finished the number of sent frames
1030 // is compared to the number of received frames.
1031 void LocalP2PTest() {
1032 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1033 initiating_client_->AddMediaStream(true, true);
1034 }
1035 initiating_client_->Negotiate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036 // Assert true is used here since next tests are guaranteed to fail and
1037 // would eat up 5 seconds.
1038 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1039 VerifySessionDescriptions();
1040
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041 int audio_frame_count = kEndAudioFrameCount;
1042 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1043 if (!initiating_client_->can_receive_audio() ||
1044 !receiving_client_->can_receive_audio()) {
1045 audio_frame_count = -1;
1046 }
1047 int video_frame_count = kEndVideoFrameCount;
1048 if (!initiating_client_->can_receive_video() ||
1049 !receiving_client_->can_receive_video()) {
1050 video_frame_count = -1;
1051 }
1052
1053 if (audio_frame_count != -1 || video_frame_count != -1) {
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001054 // Audio or video is expected to flow, so both clients should reach the
1055 // Connected state, and the offerer (ICE controller) should proceed to
1056 // Completed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001057 // Note: These tests have been observed to fail under heavy load at
1058 // shorter timeouts, so they may be flaky.
1059 EXPECT_EQ_WAIT(
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001060 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001061 initiating_client_->ice_connection_state(),
1062 kMaxWaitForFramesMs);
1063 EXPECT_EQ_WAIT(
1064 webrtc::PeerConnectionInterface::kIceConnectionConnected,
1065 receiving_client_->ice_connection_state(),
1066 kMaxWaitForFramesMs);
1067 }
1068
1069 if (initiating_client_->can_receive_audio() ||
1070 initiating_client_->can_receive_video()) {
1071 // The initiating client can receive media, so it must produce candidates
1072 // that will serve as destinations for that media.
1073 // TODO(bemasc): Understand why the state is not already Complete here, as
1074 // seems to be the case for the receiving client. This may indicate a bug
1075 // in the ICE gathering system.
1076 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1077 initiating_client_->ice_gathering_state());
1078 }
1079 if (receiving_client_->can_receive_audio() ||
1080 receiving_client_->can_receive_video()) {
1081 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1082 receiving_client_->ice_gathering_state(),
1083 kMaxWaitForFramesMs);
1084 }
1085
1086 EXPECT_TRUE_WAIT(FramesNotPending(audio_frame_count, video_frame_count),
1087 kMaxWaitForFramesMs);
1088 }
1089
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001090 void SetupAndVerifyDtlsCall() {
1091 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1092 FakeConstraints setup_constraints;
1093 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1094 true);
1095 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1096 LocalP2PTest();
1097 VerifyRenderedSize(640, 480);
1098 }
1099
1100 PeerConnectionTestClient* CreateDtlsClientWithAlternateKey() {
1101 FakeConstraints setup_constraints;
1102 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1103 true);
1104
1105 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
1106 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
1107 : nullptr);
1108 dtls_identity_store->use_alternate_key();
1109
1110 // Make sure the new client is using a different certificate.
1111 return PeerConnectionTestClient::CreateClientWithDtlsIdentityStore(
kwiberg0eb15ed2015-12-17 03:04:15 -08001112 "New Peer: ", &setup_constraints, nullptr,
1113 std::move(dtls_identity_store));
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001114 }
1115
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001116 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1117 // Messages may get lost on the unreliable DataChannel, so we send multiple
1118 // times to avoid test flakiness.
1119 static const size_t kSendAttempts = 5;
1120
1121 for (size_t i = 0; i < kSendAttempts; ++i) {
1122 dc->Send(DataBuffer(data));
1123 }
1124 }
1125
deadbeefaf1b59c2015-10-15 12:08:41 -07001126 PeerConnectionTestClient* initializing_client() {
1127 return initiating_client_.get();
1128 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001129
1130 // Set the |initiating_client_| to the |client| passed in and return the
1131 // original |initiating_client_|.
1132 PeerConnectionTestClient* set_initializing_client(
1133 PeerConnectionTestClient* client) {
1134 PeerConnectionTestClient* old = initiating_client_.release();
1135 initiating_client_.reset(client);
1136 return old;
1137 }
1138
deadbeefaf1b59c2015-10-15 12:08:41 -07001139 PeerConnectionTestClient* receiving_client() {
1140 return receiving_client_.get();
1141 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001142
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001143 // Set the |receiving_client_| to the |client| passed in and return the
1144 // original |receiving_client_|.
1145 PeerConnectionTestClient* set_receiving_client(
1146 PeerConnectionTestClient* client) {
1147 PeerConnectionTestClient* old = receiving_client_.release();
1148 receiving_client_.reset(client);
1149 return old;
1150 }
1151
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001152 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001153 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1154 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1155 rtc::SocketServerScope ss_scope_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001156 rtc::scoped_ptr<PeerConnectionTestClient> initiating_client_;
1157 rtc::scoped_ptr<PeerConnectionTestClient> receiving_client_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001158};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001159
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001160// Disable for TSan v2, see
1161// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1162#if !defined(THREAD_SANITIZER)
1163
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001164// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001165// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1166// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001167TEST_F(P2PTestConductor, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001168 ASSERT_TRUE(CreateTestClients());
1169 LocalP2PTest();
1170 VerifyDtmf();
1171}
1172
1173// This test sets up a Jsep call between two parties and test that we can get a
1174// video aspect ratio of 16:9.
deadbeef7c73bdb2015-12-10 15:10:44 -08001175TEST_F(P2PTestConductor, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001176 ASSERT_TRUE(CreateTestClients());
1177 FakeConstraints constraint;
1178 double requested_ratio = 640.0/360;
1179 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1180 SetVideoConstraints(constraint, constraint);
1181 LocalP2PTest();
1182
1183 ASSERT_LE(0, initializing_client()->rendered_height());
1184 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001185 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001186 initializing_client()->rendered_height();
1187 EXPECT_LE(requested_ratio, initiating_video_ratio);
1188
1189 ASSERT_LE(0, receiving_client()->rendered_height());
1190 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001191 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192 receiving_client()->rendered_height();
1193 EXPECT_LE(requested_ratio, receiving_video_ratio);
1194}
1195
1196// This test sets up a Jsep call between two parties and test that the
1197// received video has a resolution of 1280*720.
1198// TODO(mallinath): Enable when
1199// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
deadbeef7c73bdb2015-12-10 15:10:44 -08001200TEST_F(P2PTestConductor, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001201 ASSERT_TRUE(CreateTestClients());
1202 FakeConstraints constraint;
1203 constraint.SetMandatoryMinWidth(1280);
1204 constraint.SetMandatoryMinHeight(720);
1205 SetVideoConstraints(constraint, constraint);
1206 LocalP2PTest();
1207 VerifyRenderedSize(1280, 720);
1208}
1209
1210// This test sets up a call between two endpoints that are configured to use
1211// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001212TEST_F(P2PTestConductor, LocalP2PTestDtls) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001213 SetupAndVerifyDtlsCall();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001214}
1215
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001216// This test sets up a audio call initially and then upgrades to audio/video,
1217// using DTLS.
deadbeef7c73bdb2015-12-10 15:10:44 -08001218TEST_F(P2PTestConductor, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001219 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001220 FakeConstraints setup_constraints;
1221 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1222 true);
1223 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1224 receiving_client()->SetReceiveAudioVideo(true, false);
1225 LocalP2PTest();
1226 receiving_client()->SetReceiveAudioVideo(true, true);
1227 receiving_client()->Negotiate();
1228}
1229
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001230// This test sets up a call transfer to a new caller with a different DTLS
1231// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001232TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCallee) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001233 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1234 SetupAndVerifyDtlsCall();
1235
1236 // Keeping the original peer around which will still send packets to the
1237 // receiving client. These SRTP packets will be dropped.
1238 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1239 set_initializing_client(CreateDtlsClientWithAlternateKey()));
1240 original_peer->pc()->Close();
1241
1242 SetSignalingReceivers();
1243 receiving_client()->SetExpectIceRestart(true);
1244 LocalP2PTest();
1245 VerifyRenderedSize(640, 480);
1246}
1247
guoweis46383312015-12-17 16:45:59 -08001248// This test sets up a non-bundle call and apply bundle during ICE restart. When
1249// bundle is in effect in the restart, the channel can successfully reset its
1250// DTLS-SRTP context.
1251TEST_F(P2PTestConductor, LocalP2PTestDtlsBundleInIceRestart) {
1252 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1253 FakeConstraints setup_constraints;
1254 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1255 true);
1256 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1257 receiving_client()->RemoveBundleFromReceivedSdp(true);
1258 LocalP2PTest();
1259 VerifyRenderedSize(640, 480);
1260
1261 initializing_client()->IceRestart();
1262 receiving_client()->SetExpectIceRestart(true);
1263 receiving_client()->RemoveBundleFromReceivedSdp(false);
1264 LocalP2PTest();
1265 VerifyRenderedSize(640, 480);
1266}
1267
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001268// This test sets up a call transfer to a new callee with a different DTLS
1269// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001270TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCaller) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001271 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1272 SetupAndVerifyDtlsCall();
1273
1274 // Keeping the original peer around which will still send packets to the
1275 // receiving client. These SRTP packets will be dropped.
1276 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1277 set_receiving_client(CreateDtlsClientWithAlternateKey()));
1278 original_peer->pc()->Close();
1279
1280 SetSignalingReceivers();
1281 initializing_client()->IceRestart();
1282 LocalP2PTest();
1283 VerifyRenderedSize(640, 480);
1284}
1285
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001286// This test sets up a call between two endpoints that are configured to use
1287// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1288// negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001289TEST_F(P2PTestConductor, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001290 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 FakeConstraints setup_constraints;
1292 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1293 true);
1294 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1295 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1296 LocalP2PTest();
1297 VerifyRenderedSize(640, 480);
1298}
1299
1300// This test sets up a Jsep call between two parties, and the callee only
1301// accept to receive video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001302TEST_F(P2PTestConductor, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001303 ASSERT_TRUE(CreateTestClients());
1304 receiving_client()->SetReceiveAudioVideo(false, true);
1305 LocalP2PTest();
1306}
1307
1308// This test sets up a Jsep call between two parties, and the callee only
1309// accept to receive audio.
deadbeef7c73bdb2015-12-10 15:10:44 -08001310TEST_F(P2PTestConductor, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001311 ASSERT_TRUE(CreateTestClients());
1312 receiving_client()->SetReceiveAudioVideo(true, false);
1313 LocalP2PTest();
1314}
1315
1316// This test sets up a Jsep call between two parties, and the callee reject both
1317// audio and video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001318TEST_F(P2PTestConductor, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001319 ASSERT_TRUE(CreateTestClients());
1320 receiving_client()->SetReceiveAudioVideo(false, false);
1321 LocalP2PTest();
1322}
1323
1324// This test sets up an audio and video call between two parties. After the call
1325// runs for a while (10 frames), the caller sends an update offer with video
1326// being rejected. Once the re-negotiation is done, the video flow should stop
1327// and the audio flow should continue.
deadbeefc9be0072015-12-14 18:27:57 -08001328TEST_F(P2PTestConductor, UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001329 ASSERT_TRUE(CreateTestClients());
1330 LocalP2PTest();
1331 TestUpdateOfferWithRejectedContent();
1332}
1333
1334// This test sets up a Jsep call between two parties. The MSID is removed from
1335// the SDP strings from the caller.
deadbeefc9be0072015-12-14 18:27:57 -08001336TEST_F(P2PTestConductor, LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001337 ASSERT_TRUE(CreateTestClients());
1338 receiving_client()->RemoveMsidFromReceivedSdp(true);
1339 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1340 // audio and video is muxed when MSID is disabled. Remove
1341 // SetRemoveBundleFromSdp once
1342 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1343 receiving_client()->RemoveBundleFromReceivedSdp(true);
1344 LocalP2PTest();
1345}
1346
1347// This test sets up a Jsep call between two parties and the initiating peer
1348// sends two steams.
1349// TODO(perkj): Disabled due to
1350// https://code.google.com/p/webrtc/issues/detail?id=1454
deadbeef7c73bdb2015-12-10 15:10:44 -08001351TEST_F(P2PTestConductor, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352 ASSERT_TRUE(CreateTestClients());
1353 // Set optional video constraint to max 320pixels to decrease CPU usage.
1354 FakeConstraints constraint;
1355 constraint.SetOptionalMaxWidth(320);
1356 SetVideoConstraints(constraint, constraint);
1357 initializing_client()->AddMediaStream(true, true);
1358 initializing_client()->AddMediaStream(false, true);
1359 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1360 LocalP2PTest();
1361 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1362}
1363
1364// Test that we can receive the audio output level from a remote audio track.
deadbeef7c73bdb2015-12-10 15:10:44 -08001365TEST_F(P2PTestConductor, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366 ASSERT_TRUE(CreateTestClients());
1367 LocalP2PTest();
1368
1369 StreamCollectionInterface* remote_streams =
1370 initializing_client()->remote_streams();
1371 ASSERT_GT(remote_streams->count(), 0u);
1372 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1373 MediaStreamTrackInterface* remote_audio_track =
1374 remote_streams->at(0)->GetAudioTracks()[0];
1375
1376 // Get the audio output level stats. Note that the level is not available
1377 // until a RTCP packet has been received.
1378 EXPECT_TRUE_WAIT(
1379 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1380 kMaxWaitForStatsMs);
1381}
1382
1383// Test that an audio input level is reported.
deadbeef7c73bdb2015-12-10 15:10:44 -08001384TEST_F(P2PTestConductor, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001385 ASSERT_TRUE(CreateTestClients());
1386 LocalP2PTest();
1387
1388 // Get the audio input level stats. The level should be available very
1389 // soon after the test starts.
1390 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1391 kMaxWaitForStatsMs);
1392}
1393
1394// Test that we can get incoming byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001395TEST_F(P2PTestConductor, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001396 ASSERT_TRUE(CreateTestClients());
1397 LocalP2PTest();
1398
1399 StreamCollectionInterface* remote_streams =
1400 initializing_client()->remote_streams();
1401 ASSERT_GT(remote_streams->count(), 0u);
1402 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1403 MediaStreamTrackInterface* remote_audio_track =
1404 remote_streams->at(0)->GetAudioTracks()[0];
1405 EXPECT_TRUE_WAIT(
1406 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1407 kMaxWaitForStatsMs);
1408
1409 MediaStreamTrackInterface* remote_video_track =
1410 remote_streams->at(0)->GetVideoTracks()[0];
1411 EXPECT_TRUE_WAIT(
1412 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1413 kMaxWaitForStatsMs);
1414}
1415
1416// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001417TEST_F(P2PTestConductor, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001418 ASSERT_TRUE(CreateTestClients());
1419 LocalP2PTest();
1420
1421 StreamCollectionInterface* local_streams =
1422 initializing_client()->local_streams();
1423 ASSERT_GT(local_streams->count(), 0u);
1424 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1425 MediaStreamTrackInterface* local_audio_track =
1426 local_streams->at(0)->GetAudioTracks()[0];
1427 EXPECT_TRUE_WAIT(
1428 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1429 kMaxWaitForStatsMs);
1430
1431 MediaStreamTrackInterface* local_video_track =
1432 local_streams->at(0)->GetVideoTracks()[0];
1433 EXPECT_TRUE_WAIT(
1434 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1435 kMaxWaitForStatsMs);
1436}
1437
Joachim Bauch04e5b492015-05-29 09:40:39 +02001438// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
deadbeef7c73bdb2015-12-10 15:10:44 -08001439TEST_F(P2PTestConductor, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001440 PeerConnectionFactory::Options init_options;
1441 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1442 PeerConnectionFactory::Options recv_options;
1443 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001444 ASSERT_TRUE(
1445 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001446 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1447 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1448 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001449 LocalP2PTest();
1450
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001451 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001452 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1453 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1454 initializing_client()->GetDtlsCipherStats(),
1455 kMaxWaitForStatsMs);
1456 EXPECT_EQ(1, init_observer->GetEnumCounter(
1457 webrtc::kEnumCounterAudioSslCipher,
1458 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1459 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001460
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001461 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001462 initializing_client()->GetSrtpCipherStats(),
1463 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001464 EXPECT_EQ(1,
1465 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1466 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001467}
1468
1469// Test that DTLS 1.2 is used if both ends support it.
torbjorng79a5a832016-01-15 07:16:51 -08001470TEST_F(P2PTestConductor, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001471 PeerConnectionFactory::Options init_options;
1472 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1473 PeerConnectionFactory::Options recv_options;
1474 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001475 ASSERT_TRUE(
1476 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001477 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1478 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1479 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001480 LocalP2PTest();
1481
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001482 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001483 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1484 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)),
1485 initializing_client()->GetDtlsCipherStats(),
1486 kMaxWaitForStatsMs);
1487 EXPECT_EQ(1, init_observer->GetEnumCounter(
1488 webrtc::kEnumCounterAudioSslCipher,
1489 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1490 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001491
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001492 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001493 initializing_client()->GetSrtpCipherStats(),
1494 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001495 EXPECT_EQ(1,
1496 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1497 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001498}
1499
1500// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1501// received supports 1.0.
deadbeef7c73bdb2015-12-10 15:10:44 -08001502TEST_F(P2PTestConductor, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001503 PeerConnectionFactory::Options init_options;
1504 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1505 PeerConnectionFactory::Options recv_options;
1506 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001507 ASSERT_TRUE(
1508 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001509 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1510 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1511 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001512 LocalP2PTest();
1513
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001514 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001515 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1516 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1517 initializing_client()->GetDtlsCipherStats(),
1518 kMaxWaitForStatsMs);
1519 EXPECT_EQ(1, init_observer->GetEnumCounter(
1520 webrtc::kEnumCounterAudioSslCipher,
1521 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1522 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001523
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001524 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001525 initializing_client()->GetSrtpCipherStats(),
1526 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001527 EXPECT_EQ(1,
1528 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1529 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001530}
1531
1532// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1533// received supports 1.2.
deadbeef7c73bdb2015-12-10 15:10:44 -08001534TEST_F(P2PTestConductor, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001535 PeerConnectionFactory::Options init_options;
1536 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1537 PeerConnectionFactory::Options recv_options;
1538 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001539 ASSERT_TRUE(
1540 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001541 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1542 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1543 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001544 LocalP2PTest();
1545
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001546 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001547 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1548 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1549 initializing_client()->GetDtlsCipherStats(),
1550 kMaxWaitForStatsMs);
1551 EXPECT_EQ(1, init_observer->GetEnumCounter(
1552 webrtc::kEnumCounterAudioSslCipher,
1553 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1554 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001555
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001556 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001557 initializing_client()->GetSrtpCipherStats(),
1558 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001559 EXPECT_EQ(1,
1560 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1561 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001562}
1563
deadbeefb5cb19b2015-11-23 16:39:12 -08001564// This test sets up a call between two parties with audio, video and an RTP
1565// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001566TEST_F(P2PTestConductor, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567 FakeConstraints setup_constraints;
1568 setup_constraints.SetAllowRtpDataChannels();
1569 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1570 initializing_client()->CreateDataChannel();
1571 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001572 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1573 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1575 kMaxWaitMs);
1576 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1577 kMaxWaitMs);
1578
1579 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001580
1581 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1583 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001584
1585 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1587 kMaxWaitMs);
1588
1589 receiving_client()->data_channel()->Close();
1590 // Send new offer and answer.
1591 receiving_client()->Negotiate();
1592 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1593 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1594}
1595
deadbeefb5cb19b2015-11-23 16:39:12 -08001596// This test sets up a call between two parties with audio, video and an SCTP
1597// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001598TEST_F(P2PTestConductor, LocalP2PTestSctpDataChannel) {
deadbeefb5cb19b2015-11-23 16:39:12 -08001599 ASSERT_TRUE(CreateTestClients());
1600 initializing_client()->CreateDataChannel();
1601 LocalP2PTest();
1602 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1603 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
1604 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1605 kMaxWaitMs);
1606 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
1607
1608 std::string data = "hello world";
1609
1610 initializing_client()->data_channel()->Send(DataBuffer(data));
1611 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1612 kMaxWaitMs);
1613
1614 receiving_client()->data_channel()->Send(DataBuffer(data));
1615 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1616 kMaxWaitMs);
1617
1618 receiving_client()->data_channel()->Close();
deadbeef15887932015-12-14 19:32:34 -08001619 EXPECT_TRUE_WAIT(!initializing_client()->data_observer()->IsOpen(),
1620 kMaxWaitMs);
1621 EXPECT_TRUE_WAIT(!receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
deadbeefb5cb19b2015-11-23 16:39:12 -08001622}
1623
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624// This test sets up a call between two parties and creates a data channel.
1625// The test tests that received data is buffered unless an observer has been
1626// registered.
1627// Rtp data channels can receive data before the underlying
1628// transport has detected that a channel is writable and thus data can be
1629// received before the data channel state changes to open. That is hard to test
1630// but the same buffering is used in that case.
deadbeef7c73bdb2015-12-10 15:10:44 -08001631TEST_F(P2PTestConductor, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632 FakeConstraints setup_constraints;
1633 setup_constraints.SetAllowRtpDataChannels();
1634 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1635 initializing_client()->CreateDataChannel();
1636 initializing_client()->Negotiate();
1637
deadbeefaf1b59c2015-10-15 12:08:41 -07001638 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1639 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1641 kMaxWaitMs);
1642 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1643 receiving_client()->data_channel()->state(), kMaxWaitMs);
1644
1645 // Unregister the existing observer.
1646 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001647
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001648 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001649 SendRtpData(initializing_client()->data_channel(), data);
1650
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001651 // Wait a while to allow the sent data to arrive before an observer is
1652 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001653 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654
1655 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1656 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1657}
1658
1659// This test sets up a call between two parties with audio, video and but only
1660// the initiating client support data.
deadbeef7c73bdb2015-12-10 15:10:44 -08001661TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001662 FakeConstraints setup_constraints_1;
1663 setup_constraints_1.SetAllowRtpDataChannels();
1664 // Must disable DTLS to make negotiation succeed.
1665 setup_constraints_1.SetMandatory(
1666 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1667 FakeConstraints setup_constraints_2;
1668 setup_constraints_2.SetMandatory(
1669 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1670 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671 initializing_client()->CreateDataChannel();
1672 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001673 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674 EXPECT_FALSE(receiving_client()->data_channel());
1675 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1676}
1677
1678// This test sets up a call between two parties with audio, video. When audio
1679// and video is setup and flowing and data channel is negotiated.
deadbeef7c73bdb2015-12-10 15:10:44 -08001680TEST_F(P2PTestConductor, AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001681 FakeConstraints setup_constraints;
1682 setup_constraints.SetAllowRtpDataChannels();
1683 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1684 LocalP2PTest();
1685 initializing_client()->CreateDataChannel();
1686 // Send new offer and answer.
1687 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07001688 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1689 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001690 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1691 kMaxWaitMs);
1692 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1693 kMaxWaitMs);
1694}
1695
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001696// This test sets up a Jsep call with SCTP DataChannel and verifies the
1697// negotiation is completed without error.
1698#ifdef HAVE_SCTP
deadbeef7c73bdb2015-12-10 15:10:44 -08001699TEST_F(P2PTestConductor, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001700 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001701 FakeConstraints constraints;
1702 constraints.SetMandatory(
1703 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1704 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1705 initializing_client()->CreateDataChannel();
1706 initializing_client()->Negotiate(false, false);
1707}
1708#endif
1709
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710// This test sets up a call between two parties with audio, and video.
1711// During the call, the initializing side restart ice and the test verifies that
1712// new ice candidates are generated and audio and video still can flow.
deadbeef7c73bdb2015-12-10 15:10:44 -08001713TEST_F(P2PTestConductor, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714 ASSERT_TRUE(CreateTestClients());
1715
1716 // Negotiate and wait for ice completion and make sure audio and video plays.
1717 LocalP2PTest();
1718
1719 // Create a SDP string of the first audio candidate for both clients.
1720 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1721 initializing_client()->pc()->local_description()->candidates(0);
1722 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1723 receiving_client()->pc()->local_description()->candidates(0);
1724 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1725 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1726 std::string initiator_candidate;
1727 EXPECT_TRUE(
1728 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1729 std::string receiver_candidate;
1730 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1731
1732 // Restart ice on the initializing client.
1733 receiving_client()->SetExpectIceRestart(true);
1734 initializing_client()->IceRestart();
1735
1736 // Negotiate and wait for ice completion again and make sure audio and video
1737 // plays.
1738 LocalP2PTest();
1739
1740 // Create a SDP string of the first audio candidate for both clients again.
1741 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1742 initializing_client()->pc()->local_description()->candidates(0);
1743 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1744 receiving_client()->pc()->local_description()->candidates(0);
1745 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1746 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1747 std::string initiator_candidate_restart;
1748 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1749 &initiator_candidate_restart));
1750 std::string receiver_candidate_restart;
1751 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1752 &receiver_candidate_restart));
1753
1754 // Verify that the first candidates in the local session descriptions has
1755 // changed.
1756 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1757 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1758}
1759
deadbeeffaac4972015-11-12 15:33:07 -08001760// This test sets up a call between two parties with audio, and video.
1761// It then renegotiates setting the video m-line to "port 0", then later
1762// renegotiates again, enabling video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001763TEST_F(P2PTestConductor, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08001764 ASSERT_TRUE(CreateTestClients());
1765
1766 // Do initial negotiation. Will result in video and audio sendonly m-lines.
1767 receiving_client()->set_auto_add_stream(false);
1768 initializing_client()->AddMediaStream(true, true);
1769 initializing_client()->Negotiate();
1770
1771 // Negotiate again, disabling the video m-line (receiving client will
1772 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
1773 receiving_client()->SetReceiveVideo(false);
1774 initializing_client()->Negotiate();
1775
1776 // Enable video and do negotiation again, making sure video is received
1777 // end-to-end.
1778 receiving_client()->SetReceiveVideo(true);
1779 receiving_client()->AddMediaStream(true, true);
1780 LocalP2PTest();
1781}
1782
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783// This test sets up a Jsep call between two parties with external
1784// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001785// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1786// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001787TEST_F(P2PTestConductor, DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788 ASSERT_TRUE(CreateTestClients());
1789 EnableVideoDecoderFactory();
1790 LocalP2PTest();
1791}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001792
deadbeeffac06552015-11-25 11:26:01 -08001793// This tests that if we negotiate after calling CreateSender but before we
1794// have a track, then set a track later, frames from the newly-set track are
1795// received end-to-end.
deadbeef7c73bdb2015-12-10 15:10:44 -08001796TEST_F(P2PTestConductor, EarlyWarmupTest) {
deadbeeffac06552015-11-25 11:26:01 -08001797 ASSERT_TRUE(CreateTestClients());
deadbeefbd7d8f72015-12-18 16:58:44 -08001798 auto audio_sender =
1799 initializing_client()->pc()->CreateSender("audio", "stream_id");
1800 auto video_sender =
1801 initializing_client()->pc()->CreateSender("video", "stream_id");
deadbeeffac06552015-11-25 11:26:01 -08001802 initializing_client()->Negotiate();
1803 // Wait for ICE connection to complete, without any tracks.
1804 // Note that the receiving client WILL (in HandleIncomingOffer) create
1805 // tracks, so it's only the initiator here that's doing early warmup.
1806 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1807 VerifySessionDescriptions();
1808 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1809 initializing_client()->ice_connection_state(),
1810 kMaxWaitForFramesMs);
1811 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1812 receiving_client()->ice_connection_state(),
1813 kMaxWaitForFramesMs);
1814 // Now set the tracks, and expect frames to immediately start flowing.
1815 EXPECT_TRUE(
1816 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
1817 EXPECT_TRUE(
1818 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
1819 EXPECT_TRUE_WAIT(FramesNotPending(kEndAudioFrameCount, kEndVideoFrameCount),
1820 kMaxWaitForFramesMs);
1821}
1822
deadbeef0a6c4ca2015-10-06 11:38:28 -07001823class IceServerParsingTest : public testing::Test {
1824 public:
1825 // Convenience for parsing a single URL.
1826 bool ParseUrl(const std::string& url) {
1827 return ParseUrl(url, std::string(), std::string());
1828 }
1829
1830 bool ParseUrl(const std::string& url,
1831 const std::string& username,
1832 const std::string& password) {
1833 PeerConnectionInterface::IceServers servers;
1834 PeerConnectionInterface::IceServer server;
1835 server.urls.push_back(url);
1836 server.username = username;
1837 server.password = password;
1838 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001839 return webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_);
deadbeef0a6c4ca2015-10-06 11:38:28 -07001840 }
1841
1842 protected:
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001843 cricket::ServerAddresses stun_servers_;
1844 std::vector<cricket::RelayServerConfig> turn_servers_;
deadbeef0a6c4ca2015-10-06 11:38:28 -07001845};
1846
1847// Make sure all STUN/TURN prefixes are parsed correctly.
1848TEST_F(IceServerParsingTest, ParseStunPrefixes) {
1849 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001850 EXPECT_EQ(1U, stun_servers_.size());
1851 EXPECT_EQ(0U, turn_servers_.size());
1852 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001853
1854 EXPECT_TRUE(ParseUrl("stuns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001855 EXPECT_EQ(1U, stun_servers_.size());
1856 EXPECT_EQ(0U, turn_servers_.size());
1857 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001858
1859 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001860 EXPECT_EQ(0U, stun_servers_.size());
1861 EXPECT_EQ(1U, turn_servers_.size());
1862 EXPECT_FALSE(turn_servers_[0].ports[0].secure);
1863 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001864
1865 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001866 EXPECT_EQ(0U, stun_servers_.size());
1867 EXPECT_EQ(1U, turn_servers_.size());
1868 EXPECT_TRUE(turn_servers_[0].ports[0].secure);
1869 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001870
1871 // invalid prefixes
1872 EXPECT_FALSE(ParseUrl("stunn:hostname"));
1873 EXPECT_FALSE(ParseUrl(":hostname"));
1874 EXPECT_FALSE(ParseUrl(":"));
1875 EXPECT_FALSE(ParseUrl(""));
1876}
1877
1878TEST_F(IceServerParsingTest, VerifyDefaults) {
1879 // TURNS defaults
1880 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001881 EXPECT_EQ(1U, turn_servers_.size());
1882 EXPECT_EQ(5349, turn_servers_[0].ports[0].address.port());
1883 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
1884 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001885
1886 // TURN defaults
1887 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001888 EXPECT_EQ(1U, turn_servers_.size());
1889 EXPECT_EQ(3478, turn_servers_[0].ports[0].address.port());
1890 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
1891 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001892
1893 // STUN defaults
1894 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001895 EXPECT_EQ(1U, stun_servers_.size());
1896 EXPECT_EQ(3478, stun_servers_.begin()->port());
1897 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001898}
1899
1900// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
1901// can be parsed correctly.
1902TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
1903 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001904 EXPECT_EQ(1U, stun_servers_.size());
1905 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
1906 EXPECT_EQ(1234, stun_servers_.begin()->port());
1907 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001908
1909 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001910 EXPECT_EQ(1U, stun_servers_.size());
1911 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
1912 EXPECT_EQ(4321, stun_servers_.begin()->port());
1913 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001914
1915 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001916 EXPECT_EQ(1U, stun_servers_.size());
1917 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
1918 EXPECT_EQ(9999, stun_servers_.begin()->port());
1919 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001920
1921 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001922 EXPECT_EQ(1U, stun_servers_.size());
1923 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
1924 EXPECT_EQ(3478, stun_servers_.begin()->port());
1925 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001926
1927 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001928 EXPECT_EQ(1U, stun_servers_.size());
1929 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
1930 EXPECT_EQ(3478, stun_servers_.begin()->port());
1931 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001932
1933 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001934 EXPECT_EQ(1U, stun_servers_.size());
1935 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
1936 EXPECT_EQ(3478, stun_servers_.begin()->port());
1937 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001938
1939 // Try some invalid hostname:port strings.
1940 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
1941 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001942 EXPECT_FALSE(ParseUrl("stun:hostname:port:more"));
1943 EXPECT_FALSE(ParseUrl("stun:hostname:port more"));
deadbeef0a6c4ca2015-10-06 11:38:28 -07001944 EXPECT_FALSE(ParseUrl("stun:hostname:"));
1945 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
1946 EXPECT_FALSE(ParseUrl("stun::5555"));
1947 EXPECT_FALSE(ParseUrl("stun:"));
1948}
1949
1950// Test parsing the "?transport=xxx" part of the URL.
1951TEST_F(IceServerParsingTest, ParseTransport) {
1952 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001953 EXPECT_EQ(1U, turn_servers_.size());
1954 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
1955 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001956
1957 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001958 EXPECT_EQ(1U, turn_servers_.size());
1959 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
1960 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001961
1962 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
1963}
1964
1965// Test parsing ICE username contained in URL.
1966TEST_F(IceServerParsingTest, ParseUsername) {
1967 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001968 EXPECT_EQ(1U, turn_servers_.size());
1969 EXPECT_EQ("user", turn_servers_[0].credentials.username);
1970 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001971
1972 EXPECT_FALSE(ParseUrl("turn:@hostname"));
1973 EXPECT_FALSE(ParseUrl("turn:username@"));
1974 EXPECT_FALSE(ParseUrl("turn:@"));
1975 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
1976}
1977
1978// Test that username and password from IceServer is copied into the resulting
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001979// RelayServerConfig.
deadbeef0a6c4ca2015-10-06 11:38:28 -07001980TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
1981 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001982 EXPECT_EQ(1U, turn_servers_.size());
1983 EXPECT_EQ("username", turn_servers_[0].credentials.username);
1984 EXPECT_EQ("password", turn_servers_[0].credentials.password);
deadbeef0a6c4ca2015-10-06 11:38:28 -07001985}
1986
1987// Ensure that if a server has multiple URLs, each one is parsed.
1988TEST_F(IceServerParsingTest, ParseMultipleUrls) {
1989 PeerConnectionInterface::IceServers servers;
1990 PeerConnectionInterface::IceServer server;
1991 server.urls.push_back("stun:hostname");
1992 server.urls.push_back("turn:hostname");
1993 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001994 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
1995 EXPECT_EQ(1U, stun_servers_.size());
1996 EXPECT_EQ(1U, turn_servers_.size());
deadbeef0a6c4ca2015-10-06 11:38:28 -07001997}
1998
Taylor Brandstetter893505d2016-01-07 15:12:48 -08001999// Ensure that TURN servers are given unique priorities,
2000// so that their resulting candidates have unique priorities.
2001TEST_F(IceServerParsingTest, TurnServerPrioritiesUnique) {
2002 PeerConnectionInterface::IceServers servers;
2003 PeerConnectionInterface::IceServer server;
2004 server.urls.push_back("turn:hostname");
2005 server.urls.push_back("turn:hostname2");
2006 servers.push_back(server);
2007 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2008 EXPECT_EQ(2U, turn_servers_.size());
2009 EXPECT_NE(turn_servers_[0].priority, turn_servers_[1].priority);
2010}
2011
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00002012#endif // if !defined(THREAD_SANITIZER)