blob: 90615918e53969c613feade7ff7ad704d891a8e2 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <stdio.h>
29
30#include <algorithm>
31#include <list>
32#include <map>
kwiberg0eb15ed2015-12-17 03:04:15 -080033#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034#include <vector>
35
36#include "talk/app/webrtc/dtmfsender.h"
jbauchac8869e2015-07-03 01:36:14 -070037#include "talk/app/webrtc/fakemetricsobserver.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038#include "talk/app/webrtc/localaudiosource.h"
39#include "talk/app/webrtc/mediastreaminterface.h"
deadbeef0a6c4ca2015-10-06 11:38:28 -070040#include "talk/app/webrtc/peerconnection.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041#include "talk/app/webrtc/peerconnectionfactory.h"
42#include "talk/app/webrtc/peerconnectioninterface.h"
43#include "talk/app/webrtc/test/fakeaudiocapturemodule.h"
44#include "talk/app/webrtc/test/fakeconstraints.h"
Henrik Boström5e56c592015-08-11 10:33:13 +020045#include "talk/app/webrtc/test/fakedtlsidentitystore.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046#include "talk/app/webrtc/test/fakeperiodicvideocapturer.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000047#include "talk/app/webrtc/test/fakevideotrackrenderer.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048#include "talk/app/webrtc/test/mockpeerconnectionobservers.h"
49#include "talk/app/webrtc/videosourceinterface.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000050#include "talk/session/media/mediasession.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000051#include "webrtc/base/gunit.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000052#include "webrtc/base/physicalsocketserver.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000053#include "webrtc/base/scoped_ptr.h"
54#include "webrtc/base/ssladapter.h"
55#include "webrtc/base/sslstreamadapter.h"
56#include "webrtc/base/thread.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000057#include "webrtc/base/virtualsocketserver.h"
kjellandera96e2d72016-02-04 23:52:28 -080058#include "webrtc/media/webrtc/fakewebrtcvideoengine.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000059#include "webrtc/p2p/base/constants.h"
60#include "webrtc/p2p/base/sessiondescription.h"
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -080061#include "webrtc/p2p/client/fakeportallocator.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062
63#define MAYBE_SKIP_TEST(feature) \
64 if (!(feature())) { \
65 LOG(LS_INFO) << "Feature disabled... skipping"; \
66 return; \
67 }
68
69using cricket::ContentInfo;
70using cricket::FakeWebRtcVideoDecoder;
71using cricket::FakeWebRtcVideoDecoderFactory;
72using cricket::FakeWebRtcVideoEncoder;
73using cricket::FakeWebRtcVideoEncoderFactory;
74using cricket::MediaContentDescription;
75using webrtc::DataBuffer;
76using webrtc::DataChannelInterface;
77using webrtc::DtmfSender;
78using webrtc::DtmfSenderInterface;
79using webrtc::DtmfSenderObserverInterface;
80using webrtc::FakeConstraints;
81using webrtc::MediaConstraintsInterface;
deadbeeffaac4972015-11-12 15:33:07 -080082using webrtc::MediaStreamInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083using webrtc::MediaStreamTrackInterface;
84using webrtc::MockCreateSessionDescriptionObserver;
85using webrtc::MockDataChannelObserver;
86using webrtc::MockSetSessionDescriptionObserver;
87using webrtc::MockStatsObserver;
deadbeeffaac4972015-11-12 15:33:07 -080088using webrtc::ObserverInterface;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000089using webrtc::PeerConnectionInterface;
Joachim Bauch04e5b492015-05-29 09:40:39 +020090using webrtc::PeerConnectionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091using webrtc::SessionDescriptionInterface;
92using webrtc::StreamCollectionInterface;
93
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000094static const int kMaxWaitMs = 10000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +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 uninitialized-variable
98// warnings.
99#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100static const int kMaxWaitForStatsMs = 3000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +0000101#endif
deadbeeffac06552015-11-25 11:26:01 -0800102static const int kMaxWaitForActivationMs = 5000;
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +0000103static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104static const int kEndAudioFrameCount = 3;
105static const int kEndVideoFrameCount = 3;
106
107static const char kStreamLabelBase[] = "stream_label";
108static const char kVideoTrackLabelBase[] = "video_track";
109static const char kAudioTrackLabelBase[] = "audio_track";
110static const char kDataChannelLabel[] = "data_channel";
111
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000112// Disable for TSan v2, see
113// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
114// This declaration is also #ifdef'd as it causes unused-variable errors.
115#if !defined(THREAD_SANITIZER)
116// SRTP cipher name negotiated by the tests. This must be updated if the
117// default changes.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800118static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000119#endif
120
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121static void RemoveLinesFromSdp(const std::string& line_start,
122 std::string* sdp) {
123 const char kSdpLineEnd[] = "\r\n";
124 size_t ssrc_pos = 0;
125 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
126 std::string::npos) {
127 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
128 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
129 }
130}
131
132class SignalingMessageReceiver {
133 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 virtual void ReceiveSdpMessage(const std::string& type,
135 std::string& msg) = 0;
136 virtual void ReceiveIceMessage(const std::string& sdp_mid,
137 int sdp_mline_index,
138 const std::string& msg) = 0;
139
140 protected:
deadbeefaf1b59c2015-10-15 12:08:41 -0700141 SignalingMessageReceiver() {}
142 virtual ~SignalingMessageReceiver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143};
144
deadbeefaf1b59c2015-10-15 12:08:41 -0700145class PeerConnectionTestClient : public webrtc::PeerConnectionObserver,
deadbeeffaac4972015-11-12 15:33:07 -0800146 public SignalingMessageReceiver,
147 public ObserverInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 public:
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800149 static PeerConnectionTestClient* CreateClientWithDtlsIdentityStore(
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800150 const std::string& id,
151 const MediaConstraintsInterface* constraints,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800152 const PeerConnectionFactory::Options* options,
153 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800154 PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
kwiberg0eb15ed2015-12-17 03:04:15 -0800155 if (!client->Init(constraints, options, std::move(dtls_identity_store))) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800156 delete client;
157 return nullptr;
158 }
159 return client;
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800160 }
161
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800162 static PeerConnectionTestClient* CreateClient(
163 const std::string& id,
164 const MediaConstraintsInterface* constraints,
165 const PeerConnectionFactory::Options* options) {
166 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
167 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
168 : nullptr);
169
170 return CreateClientWithDtlsIdentityStore(id, constraints, options,
kwiberg0eb15ed2015-12-17 03:04:15 -0800171 std::move(dtls_identity_store));
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800172 }
173
deadbeefaf1b59c2015-10-15 12:08:41 -0700174 ~PeerConnectionTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 }
176
deadbeefaf1b59c2015-10-15 12:08:41 -0700177 void Negotiate() { Negotiate(true, true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178
deadbeefaf1b59c2015-10-15 12:08:41 -0700179 void Negotiate(bool audio, bool video) {
180 rtc::scoped_ptr<SessionDescriptionInterface> offer;
181 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182
deadbeefaf1b59c2015-10-15 12:08:41 -0700183 if (offer->description()->GetContentByName("audio")) {
184 offer->description()->GetContentByName("audio")->rejected = !audio;
185 }
186 if (offer->description()->GetContentByName("video")) {
187 offer->description()->GetContentByName("video")->rejected = !video;
188 }
189
190 std::string sdp;
191 EXPECT_TRUE(offer->ToString(&sdp));
192 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
193 signaling_message_receiver_->ReceiveSdpMessage(
194 webrtc::SessionDescriptionInterface::kOffer, sdp);
195 }
196
197 // SignalingMessageReceiver callback.
198 void ReceiveSdpMessage(const std::string& type, std::string& msg) override {
199 FilterIncomingSdpMessage(&msg);
200 if (type == webrtc::SessionDescriptionInterface::kOffer) {
201 HandleIncomingOffer(msg);
202 } else {
203 HandleIncomingAnswer(msg);
204 }
205 }
206
207 // SignalingMessageReceiver callback.
208 void ReceiveIceMessage(const std::string& sdp_mid,
209 int sdp_mline_index,
210 const std::string& msg) override {
211 LOG(INFO) << id_ << "ReceiveIceMessage";
212 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
213 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
214 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
215 }
216
217 // PeerConnectionObserver callbacks.
218 void OnSignalingChange(
219 webrtc::PeerConnectionInterface::SignalingState new_state) override {
220 EXPECT_EQ(pc()->signaling_state(), new_state);
221 }
deadbeeffaac4972015-11-12 15:33:07 -0800222 void OnAddStream(MediaStreamInterface* media_stream) override {
223 media_stream->RegisterObserver(this);
deadbeefaf1b59c2015-10-15 12:08:41 -0700224 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
225 const std::string id = media_stream->GetVideoTracks()[i]->id();
226 ASSERT_TRUE(fake_video_renderers_.find(id) ==
227 fake_video_renderers_.end());
deadbeefc9be0072015-12-14 18:27:57 -0800228 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
229 media_stream->GetVideoTracks()[i]));
deadbeefaf1b59c2015-10-15 12:08:41 -0700230 }
231 }
deadbeeffaac4972015-11-12 15:33:07 -0800232 void OnRemoveStream(MediaStreamInterface* media_stream) override {}
deadbeefaf1b59c2015-10-15 12:08:41 -0700233 void OnRenegotiationNeeded() override {}
234 void OnIceConnectionChange(
235 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
236 EXPECT_EQ(pc()->ice_connection_state(), new_state);
237 }
238 void OnIceGatheringChange(
239 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
240 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
241 }
242 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
243 LOG(INFO) << id_ << "OnIceCandidate";
244
245 std::string ice_sdp;
246 EXPECT_TRUE(candidate->ToString(&ice_sdp));
247 if (signaling_message_receiver_ == nullptr) {
248 // Remote party may be deleted.
249 return;
250 }
251 signaling_message_receiver_->ReceiveIceMessage(
252 candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
253 }
254
deadbeeffaac4972015-11-12 15:33:07 -0800255 // MediaStreamInterface callback
256 void OnChanged() override {
257 // Track added or removed from MediaStream, so update our renderers.
258 rtc::scoped_refptr<StreamCollectionInterface> remote_streams =
259 pc()->remote_streams();
260 // Remove renderers for tracks that were removed.
261 for (auto it = fake_video_renderers_.begin();
262 it != fake_video_renderers_.end();) {
263 if (remote_streams->FindVideoTrack(it->first) == nullptr) {
deadbeefc9be0072015-12-14 18:27:57 -0800264 auto to_remove = it++;
265 removed_fake_video_renderers_.push_back(std::move(to_remove->second));
266 fake_video_renderers_.erase(to_remove);
deadbeeffaac4972015-11-12 15:33:07 -0800267 } else {
268 ++it;
269 }
270 }
271 // Create renderers for new video tracks.
272 for (size_t stream_index = 0; stream_index < remote_streams->count();
273 ++stream_index) {
274 MediaStreamInterface* remote_stream = remote_streams->at(stream_index);
275 for (size_t track_index = 0;
276 track_index < remote_stream->GetVideoTracks().size();
277 ++track_index) {
278 const std::string id =
279 remote_stream->GetVideoTracks()[track_index]->id();
280 if (fake_video_renderers_.find(id) != fake_video_renderers_.end()) {
281 continue;
282 }
deadbeefc9be0072015-12-14 18:27:57 -0800283 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
284 remote_stream->GetVideoTracks()[track_index]));
deadbeeffaac4972015-11-12 15:33:07 -0800285 }
286 }
287 }
288
deadbeefaf1b59c2015-10-15 12:08:41 -0700289 void SetVideoConstraints(const webrtc::FakeConstraints& video_constraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290 video_constraints_ = video_constraint;
291 }
292
293 void AddMediaStream(bool audio, bool video) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700294 std::string stream_label =
295 kStreamLabelBase +
296 rtc::ToString<int>(static_cast<int>(pc()->local_streams()->count()));
deadbeeffaac4972015-11-12 15:33:07 -0800297 rtc::scoped_refptr<MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000298 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299
300 if (audio && can_receive_audio()) {
deadbeeffac06552015-11-25 11:26:01 -0800301 stream->AddTrack(CreateLocalAudioTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302 }
303 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000304 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305 }
306
deadbeefaf1b59c2015-10-15 12:08:41 -0700307 EXPECT_TRUE(pc()->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308 }
309
deadbeefaf1b59c2015-10-15 12:08:41 -0700310 size_t NumberOfLocalMediaStreams() { return pc()->local_streams()->count(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311
312 bool SessionActive() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700313 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314 }
315
deadbeeffaac4972015-11-12 15:33:07 -0800316 // Automatically add a stream when receiving an offer, if we don't have one.
317 // Defaults to true.
318 void set_auto_add_stream(bool auto_add_stream) {
319 auto_add_stream_ = auto_add_stream;
320 }
321
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322 void set_signaling_message_receiver(
deadbeefaf1b59c2015-10-15 12:08:41 -0700323 SignalingMessageReceiver* signaling_message_receiver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324 signaling_message_receiver_ = signaling_message_receiver;
325 }
326
327 void EnableVideoDecoderFactory() {
328 video_decoder_factory_enabled_ = true;
329 fake_video_decoder_factory_->AddSupportedVideoCodecType(
330 webrtc::kVideoCodecVP8);
331 }
332
deadbeefaf1b59c2015-10-15 12:08:41 -0700333 void IceRestart() {
334 session_description_constraints_.SetMandatoryIceRestart(true);
335 SetExpectIceRestart(true);
336 }
337
338 void SetExpectIceRestart(bool expect_restart) {
339 expect_ice_restart_ = expect_restart;
340 }
341
342 bool ExpectIceRestart() const { return expect_ice_restart_; }
343
344 void SetReceiveAudioVideo(bool audio, bool video) {
345 SetReceiveAudio(audio);
346 SetReceiveVideo(video);
347 ASSERT_EQ(audio, can_receive_audio());
348 ASSERT_EQ(video, can_receive_video());
349 }
350
351 void SetReceiveAudio(bool audio) {
352 if (audio && can_receive_audio())
353 return;
354 session_description_constraints_.SetMandatoryReceiveAudio(audio);
355 }
356
357 void SetReceiveVideo(bool video) {
358 if (video && can_receive_video())
359 return;
360 session_description_constraints_.SetMandatoryReceiveVideo(video);
361 }
362
363 void RemoveMsidFromReceivedSdp(bool remove) { remove_msid_ = remove; }
364
365 void RemoveSdesCryptoFromReceivedSdp(bool remove) { remove_sdes_ = remove; }
366
367 void RemoveBundleFromReceivedSdp(bool remove) { remove_bundle_ = remove; }
368
369 bool can_receive_audio() {
370 bool value;
371 if (webrtc::FindConstraint(&session_description_constraints_,
372 MediaConstraintsInterface::kOfferToReceiveAudio,
373 &value, nullptr)) {
374 return value;
375 }
376 return true;
377 }
378
379 bool can_receive_video() {
380 bool value;
381 if (webrtc::FindConstraint(&session_description_constraints_,
382 MediaConstraintsInterface::kOfferToReceiveVideo,
383 &value, nullptr)) {
384 return value;
385 }
386 return true;
387 }
388
389 void OnIceComplete() override { LOG(INFO) << id_ << "OnIceComplete"; }
390
391 void OnDataChannel(DataChannelInterface* data_channel) override {
392 LOG(INFO) << id_ << "OnDataChannel";
393 data_channel_ = data_channel;
394 data_observer_.reset(new MockDataChannelObserver(data_channel));
395 }
396
397 void CreateDataChannel() {
398 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, nullptr);
399 ASSERT_TRUE(data_channel_.get() != nullptr);
400 data_observer_.reset(new MockDataChannelObserver(data_channel_));
401 }
402
deadbeeffac06552015-11-25 11:26:01 -0800403 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack(
404 const std::string& stream_label) {
405 FakeConstraints constraints;
406 // Disable highpass filter so that we can get all the test audio frames.
407 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
408 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
409 peer_connection_factory_->CreateAudioSource(&constraints);
410 // TODO(perkj): Test audio source when it is implemented. Currently audio
411 // always use the default input.
412 std::string label = stream_label + kAudioTrackLabelBase;
413 return peer_connection_factory_->CreateAudioTrack(label, source);
414 }
415
416 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack(
417 const std::string& stream_label) {
418 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
419 FakeConstraints source_constraints = video_constraints_;
420 source_constraints.SetMandatoryMaxFrameRate(10);
421
422 cricket::FakeVideoCapturer* fake_capturer =
423 new webrtc::FakePeriodicVideoCapturer();
424 video_capturers_.push_back(fake_capturer);
425 rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
426 peer_connection_factory_->CreateVideoSource(fake_capturer,
427 &source_constraints);
428 std::string label = stream_label + kVideoTrackLabelBase;
429 return peer_connection_factory_->CreateVideoTrack(label, source);
430 }
431
deadbeefaf1b59c2015-10-15 12:08:41 -0700432 DataChannelInterface* data_channel() { return data_channel_; }
433 const MockDataChannelObserver* data_observer() const {
434 return data_observer_.get();
435 }
436
437 webrtc::PeerConnectionInterface* pc() { return peer_connection_.get(); }
438
439 void StopVideoCapturers() {
440 for (std::vector<cricket::VideoCapturer*>::iterator it =
441 video_capturers_.begin();
442 it != video_capturers_.end(); ++it) {
443 (*it)->Stop();
444 }
445 }
446
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000447 bool AudioFramesReceivedCheck(int number_of_frames) const {
448 return number_of_frames <= fake_audio_capture_module_->frames_received();
449 }
450
deadbeefc9be0072015-12-14 18:27:57 -0800451 int audio_frames_received() const {
452 return fake_audio_capture_module_->frames_received();
453 }
454
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000455 bool VideoFramesReceivedCheck(int number_of_frames) {
456 if (video_decoder_factory_enabled_) {
457 const std::vector<FakeWebRtcVideoDecoder*>& decoders
458 = fake_video_decoder_factory_->decoders();
459 if (decoders.empty()) {
460 return number_of_frames <= 0;
461 }
462
deadbeefc9be0072015-12-14 18:27:57 -0800463 for (FakeWebRtcVideoDecoder* decoder : decoders) {
464 if (number_of_frames > decoder->GetNumFramesReceived()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465 return false;
466 }
467 }
468 return true;
469 } else {
470 if (fake_video_renderers_.empty()) {
471 return number_of_frames <= 0;
472 }
473
deadbeefc9be0072015-12-14 18:27:57 -0800474 for (const auto& pair : fake_video_renderers_) {
475 if (number_of_frames > pair.second->num_rendered_frames()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 return false;
477 }
478 }
479 return true;
480 }
481 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700482
deadbeefc9be0072015-12-14 18:27:57 -0800483 int video_frames_received() const {
484 int total = 0;
485 if (video_decoder_factory_enabled_) {
486 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
487 fake_video_decoder_factory_->decoders();
488 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
489 total += decoder->GetNumFramesReceived();
490 }
491 } else {
492 for (const auto& pair : fake_video_renderers_) {
493 total += pair.second->num_rendered_frames();
494 }
495 for (const auto& renderer : removed_fake_video_renderers_) {
496 total += renderer->num_rendered_frames();
497 }
498 }
499 return total;
500 }
501
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 // Verify the CreateDtmfSender interface
503 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000504 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
505 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506
507 // We can't create a DTMF sender with an invalid audio track or a non local
508 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700509 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000510 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700511 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
512 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513
514 // We should be able to create a DTMF sender from a local track.
515 webrtc::AudioTrackInterface* localtrack =
516 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
517 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700518 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519 dtmf_sender->RegisterObserver(observer.get());
520
521 // Test the DtmfSender object just created.
522 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
523 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
524
525 // We don't need to verify that the DTMF tones are actually sent out because
526 // that is already covered by the tests of the lower level components.
527
528 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
529 std::vector<std::string> tones;
530 tones.push_back("1");
531 tones.push_back("a");
532 tones.push_back("");
533 observer->Verify(tones);
534
535 dtmf_sender->UnregisterObserver();
536 }
537
538 // Verifies that the SessionDescription have rejected the appropriate media
539 // content.
540 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700541 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
542 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 const cricket::SessionDescription* remote_desc =
544 peer_connection_->remote_description()->description();
545 const cricket::SessionDescription* local_desc =
546 peer_connection_->local_description()->description();
547
548 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
549 if (remote_audio_content) {
550 const ContentInfo* audio_content =
551 GetFirstAudioContent(local_desc);
552 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
553 }
554
555 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
556 if (remote_video_content) {
557 const ContentInfo* video_content =
558 GetFirstVideoContent(local_desc);
559 EXPECT_EQ(can_receive_video(), !video_content->rejected);
560 }
561 }
562
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000563 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700564 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000565 const cricket::SessionDescription* desc =
566 peer_connection_->local_description()->description();
567 const cricket::ContentInfos& contents = desc->contents();
568
569 for (size_t index = 0; index < contents.size(); ++index) {
570 if (contents[index].rejected)
571 continue;
572 const cricket::TransportDescription* transport_desc =
573 desc->GetTransportDescriptionByName(contents[index].name);
574
575 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000576 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577 if (ufragpair_it == ice_ufrag_pwd_.end()) {
578 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000579 ice_ufrag_pwd_[static_cast<int>(index)] =
580 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 } else if (ExpectIceRestart()) {
582 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
583 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
584 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
585 } else {
586 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
587 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
588 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
589 }
590 }
591 }
592
593 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000594 rtc::scoped_refptr<MockStatsObserver>
595 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000596 EXPECT_TRUE(peer_connection_->GetStats(
597 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700599 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600 return observer->AudioOutputLevel();
601 }
602
603 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000604 rtc::scoped_refptr<MockStatsObserver>
605 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000606 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700607 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700609 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610 return observer->AudioInputLevel();
611 }
612
613 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000614 rtc::scoped_refptr<MockStatsObserver>
615 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000616 EXPECT_TRUE(peer_connection_->GetStats(
617 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700619 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620 return observer->BytesReceived();
621 }
622
623 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000624 rtc::scoped_refptr<MockStatsObserver>
625 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000626 EXPECT_TRUE(peer_connection_->GetStats(
627 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700629 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 return observer->BytesSent();
631 }
632
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000633 int GetAvailableReceivedBandwidthStats() {
634 rtc::scoped_refptr<MockStatsObserver>
635 observer(new rtc::RefCountedObject<MockStatsObserver>());
636 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700637 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000638 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700639 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000640 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000641 return bw;
642 }
643
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000644 std::string GetDtlsCipherStats() {
645 rtc::scoped_refptr<MockStatsObserver>
646 observer(new rtc::RefCountedObject<MockStatsObserver>());
647 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700648 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000649 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700650 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000651 return observer->DtlsCipher();
652 }
653
654 std::string GetSrtpCipherStats() {
655 rtc::scoped_refptr<MockStatsObserver>
656 observer(new rtc::RefCountedObject<MockStatsObserver>());
657 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700658 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000659 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700660 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000661 return observer->SrtpCipher();
662 }
663
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 int rendered_width() {
665 EXPECT_FALSE(fake_video_renderers_.empty());
666 return fake_video_renderers_.empty() ? 1 :
667 fake_video_renderers_.begin()->second->width();
668 }
669
670 int rendered_height() {
671 EXPECT_FALSE(fake_video_renderers_.empty());
672 return fake_video_renderers_.empty() ? 1 :
673 fake_video_renderers_.begin()->second->height();
674 }
675
676 size_t number_of_remote_streams() {
677 if (!pc())
678 return 0;
679 return pc()->remote_streams()->count();
680 }
681
682 StreamCollectionInterface* remote_streams() {
683 if (!pc()) {
684 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700685 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 }
687 return pc()->remote_streams();
688 }
689
690 StreamCollectionInterface* local_streams() {
691 if (!pc()) {
692 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700693 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 }
695 return pc()->local_streams();
696 }
697
698 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
699 return pc()->signaling_state();
700 }
701
702 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
703 return pc()->ice_connection_state();
704 }
705
706 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
707 return pc()->ice_gathering_state();
708 }
709
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 private:
711 class DummyDtmfObserver : public DtmfSenderObserverInterface {
712 public:
713 DummyDtmfObserver() : completed_(false) {}
714
715 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700716 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 tones_.push_back(tone);
718 if (tone.empty()) {
719 completed_ = true;
720 }
721 }
722
723 void Verify(const std::vector<std::string>& tones) const {
724 ASSERT_TRUE(tones_.size() == tones.size());
725 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
726 }
727
728 bool completed() const { return completed_; }
729
730 private:
731 bool completed_;
732 std::vector<std::string> tones_;
733 };
734
deadbeefaf1b59c2015-10-15 12:08:41 -0700735 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
736
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800737 bool Init(
738 const MediaConstraintsInterface* constraints,
739 const PeerConnectionFactory::Options* options,
740 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700741 EXPECT_TRUE(!peer_connection_);
742 EXPECT_TRUE(!peer_connection_factory_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800743 rtc::scoped_ptr<cricket::PortAllocator> port_allocator(
744 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
deadbeefaf1b59c2015-10-15 12:08:41 -0700745 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
746
747 if (fake_audio_capture_module_ == nullptr) {
748 return false;
749 }
750 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
751 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
752 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
753 rtc::Thread::Current(), rtc::Thread::Current(),
754 fake_audio_capture_module_, fake_video_encoder_factory_,
755 fake_video_decoder_factory_);
756 if (!peer_connection_factory_) {
757 return false;
758 }
759 if (options) {
760 peer_connection_factory_->SetOptions(*options);
761 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800762 peer_connection_ = CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800763 std::move(port_allocator), constraints, std::move(dtls_identity_store));
deadbeefaf1b59c2015-10-15 12:08:41 -0700764 return peer_connection_.get() != nullptr;
765 }
766
deadbeefaf1b59c2015-10-15 12:08:41 -0700767 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800768 rtc::scoped_ptr<cricket::PortAllocator> port_allocator,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800769 const MediaConstraintsInterface* constraints,
770 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800771 // CreatePeerConnection with RTCConfiguration.
772 webrtc::PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 webrtc::PeerConnectionInterface::IceServer ice_server;
774 ice_server.uri = "stun:stun.l.google.com:19302";
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800775 config.servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000776
deadbeefaf1b59c2015-10-15 12:08:41 -0700777 return peer_connection_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800778 config, constraints, std::move(port_allocator),
779 std::move(dtls_identity_store), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 }
781
782 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700783 LOG(INFO) << id_ << "HandleIncomingOffer ";
deadbeeffaac4972015-11-12 15:33:07 -0800784 if (NumberOfLocalMediaStreams() == 0 && auto_add_stream_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 // If we are not sending any streams ourselves it is time to add some.
786 AddMediaStream(true, true);
787 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000788 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700789 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000791 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000792 EXPECT_TRUE(DoCreateAnswer(answer.use()));
793 std::string sdp;
794 EXPECT_TRUE(answer->ToString(&sdp));
795 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
deadbeefaf1b59c2015-10-15 12:08:41 -0700796 if (signaling_message_receiver_) {
797 signaling_message_receiver_->ReceiveSdpMessage(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 webrtc::SessionDescriptionInterface::kAnswer, sdp);
799 }
800 }
801
802 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700803 LOG(INFO) << id_ << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000804 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700805 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
807 }
808
809 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
810 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000811 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
812 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 MockCreateSessionDescriptionObserver>());
814 if (offer) {
815 pc()->CreateOffer(observer, &session_description_constraints_);
816 } else {
817 pc()->CreateAnswer(observer, &session_description_constraints_);
818 }
819 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
820 *desc = observer->release_desc();
821 if (observer->result() && ExpectIceRestart()) {
822 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
823 }
824 return observer->result();
825 }
826
827 bool DoCreateOffer(SessionDescriptionInterface** desc) {
828 return DoCreateOfferAnswer(desc, true);
829 }
830
831 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
832 return DoCreateOfferAnswer(desc, false);
833 }
834
835 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000836 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
837 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700839 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 pc()->SetLocalDescription(observer, desc);
841 // Ignore the observer result. If we wait for the result with
842 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
843 // before the offer which is an error.
844 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000845 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 // ProcessMessages waits at least 1ms but processes all messages before
847 // returning. Since this test is synchronous and send messages to the remote
848 // peer whenever a callback is invoked, this can lead to messages being
849 // sent to the remote peer in the wrong order.
850 // TODO(perkj): Find a way to check the result without risking that the
851 // order of sent messages are changed. Ex- by posting all messages that are
852 // sent to the remote peer.
853 return true;
854 }
855
856 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000857 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
858 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700860 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 pc()->SetRemoteDescription(observer, desc);
862 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
863 return observer->result();
864 }
865
866 // This modifies all received SDP messages before they are processed.
867 void FilterIncomingSdpMessage(std::string* sdp) {
868 if (remove_msid_) {
869 const char kSdpSsrcAttribute[] = "a=ssrc:";
870 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
871 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
872 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
873 }
874 if (remove_bundle_) {
875 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
876 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
877 }
878 if (remove_sdes_) {
879 const char kSdpSdesCryptoAttribute[] = "a=crypto";
880 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
881 }
882 }
883
deadbeefaf1b59c2015-10-15 12:08:41 -0700884 std::string id_;
885
deadbeefaf1b59c2015-10-15 12:08:41 -0700886 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
887 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
888 peer_connection_factory_;
889
deadbeeffaac4972015-11-12 15:33:07 -0800890 bool auto_add_stream_ = true;
891
deadbeefaf1b59c2015-10-15 12:08:41 -0700892 typedef std::pair<std::string, std::string> IceUfragPwdPair;
893 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
894 bool expect_ice_restart_ = false;
895
deadbeefc9be0072015-12-14 18:27:57 -0800896 // Needed to keep track of number of frames sent.
deadbeefaf1b59c2015-10-15 12:08:41 -0700897 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
898 // Needed to keep track of number of frames received.
deadbeefc9be0072015-12-14 18:27:57 -0800899 std::map<std::string, rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
900 fake_video_renderers_;
901 // Needed to ensure frames aren't received for removed tracks.
902 std::vector<rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
903 removed_fake_video_renderers_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700904 // Needed to keep track of number of frames received when external decoder
905 // used.
906 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
907 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
908 bool video_decoder_factory_enabled_ = false;
909 webrtc::FakeConstraints video_constraints_;
910
911 // For remote peer communication.
912 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
913
914 // Store references to the video capturers we've created, so that we can stop
915 // them, if required.
916 std::vector<cricket::VideoCapturer*> video_capturers_;
917
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918 webrtc::FakeConstraints session_description_constraints_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700919 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
920 bool remove_bundle_ =
921 false; // True if bundle should be removed in received SDP.
922 bool remove_sdes_ =
923 false; // True if a=crypto should be removed in received SDP.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000925 rtc::scoped_refptr<DataChannelInterface> data_channel_;
926 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927};
928
deadbeef7c73bdb2015-12-10 15:10:44 -0800929class P2PTestConductor : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930 public:
deadbeef7c73bdb2015-12-10 15:10:44 -0800931 P2PTestConductor()
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000932 : pss_(new rtc::PhysicalSocketServer),
933 ss_(new rtc::VirtualSocketServer(pss_.get())),
934 ss_scope_(ss_.get()) {}
935
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 bool SessionActive() {
937 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000938 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000940
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941 // Return true if the number of frames provided have been received or it is
942 // known that that will never occur (e.g. no frames will be sent or
943 // captured).
944 bool FramesNotPending(int audio_frames_to_receive,
945 int video_frames_to_receive) {
946 return VideoFramesReceivedCheck(video_frames_to_receive) &&
947 AudioFramesReceivedCheck(audio_frames_to_receive);
948 }
949 bool AudioFramesReceivedCheck(int frames_received) {
950 return initiating_client_->AudioFramesReceivedCheck(frames_received) &&
951 receiving_client_->AudioFramesReceivedCheck(frames_received);
952 }
953 bool VideoFramesReceivedCheck(int frames_received) {
954 return initiating_client_->VideoFramesReceivedCheck(frames_received) &&
955 receiving_client_->VideoFramesReceivedCheck(frames_received);
956 }
957 void VerifyDtmf() {
958 initiating_client_->VerifyDtmf();
959 receiving_client_->VerifyDtmf();
960 }
961
962 void TestUpdateOfferWithRejectedContent() {
deadbeefc9be0072015-12-14 18:27:57 -0800963 // Renegotiate, rejecting the video m-line.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 initiating_client_->Negotiate(true, false);
deadbeefc9be0072015-12-14 18:27:57 -0800965 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
966
967 int pc1_audio_received = initiating_client_->audio_frames_received();
968 int pc1_video_received = initiating_client_->video_frames_received();
969 int pc2_audio_received = receiving_client_->audio_frames_received();
970 int pc2_video_received = receiving_client_->video_frames_received();
971
972 // Wait for some additional audio frames to be received.
973 EXPECT_TRUE_WAIT(initiating_client_->AudioFramesReceivedCheck(
974 pc1_audio_received + kEndAudioFrameCount) &&
975 receiving_client_->AudioFramesReceivedCheck(
976 pc2_audio_received + kEndAudioFrameCount),
977 kMaxWaitForFramesMs);
978
979 // During this time, we shouldn't have received any additional video frames
980 // for the rejected video tracks.
981 EXPECT_EQ(pc1_video_received, initiating_client_->video_frames_received());
982 EXPECT_EQ(pc2_video_received, receiving_client_->video_frames_received());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983 }
984
985 void VerifyRenderedSize(int width, int height) {
986 EXPECT_EQ(width, receiving_client()->rendered_width());
987 EXPECT_EQ(height, receiving_client()->rendered_height());
988 EXPECT_EQ(width, initializing_client()->rendered_width());
989 EXPECT_EQ(height, initializing_client()->rendered_height());
990 }
991
992 void VerifySessionDescriptions() {
993 initiating_client_->VerifyRejectedMediaInSessionDescription();
994 receiving_client_->VerifyRejectedMediaInSessionDescription();
995 initiating_client_->VerifyLocalIceUfragAndPassword();
996 receiving_client_->VerifyLocalIceUfragAndPassword();
997 }
998
deadbeef7c73bdb2015-12-10 15:10:44 -0800999 ~P2PTestConductor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001000 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001001 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001002 }
1003 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001004 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001005 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006 }
1007
deadbeefaf1b59c2015-10-15 12:08:41 -07001008 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009
1010 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1011 MediaConstraintsInterface* recv_constraints) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001012 return CreateTestClients(init_constraints, nullptr, recv_constraints,
1013 nullptr);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001014 }
1015
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001016 void SetSignalingReceivers() {
1017 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
1018 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1019 }
1020
Joachim Bauch04e5b492015-05-29 09:40:39 +02001021 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1022 PeerConnectionFactory::Options* init_options,
1023 MediaConstraintsInterface* recv_constraints,
1024 PeerConnectionFactory::Options* recv_options) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001025 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
1026 "Caller: ", init_constraints, init_options));
1027 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
1028 "Callee: ", recv_constraints, recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029 if (!initiating_client_ || !receiving_client_) {
1030 return false;
1031 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001032 SetSignalingReceivers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001033 return true;
1034 }
1035
1036 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1037 const webrtc::FakeConstraints& recv_constraints) {
1038 initiating_client_->SetVideoConstraints(init_constraints);
1039 receiving_client_->SetVideoConstraints(recv_constraints);
1040 }
1041
1042 void EnableVideoDecoderFactory() {
1043 initiating_client_->EnableVideoDecoderFactory();
1044 receiving_client_->EnableVideoDecoderFactory();
1045 }
1046
1047 // This test sets up a call between two parties. Both parties send static
1048 // frames to each other. Once the test is finished the number of sent frames
1049 // is compared to the number of received frames.
1050 void LocalP2PTest() {
1051 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1052 initiating_client_->AddMediaStream(true, true);
1053 }
1054 initiating_client_->Negotiate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001055 // Assert true is used here since next tests are guaranteed to fail and
1056 // would eat up 5 seconds.
1057 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1058 VerifySessionDescriptions();
1059
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001060 int audio_frame_count = kEndAudioFrameCount;
1061 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1062 if (!initiating_client_->can_receive_audio() ||
1063 !receiving_client_->can_receive_audio()) {
1064 audio_frame_count = -1;
1065 }
1066 int video_frame_count = kEndVideoFrameCount;
1067 if (!initiating_client_->can_receive_video() ||
1068 !receiving_client_->can_receive_video()) {
1069 video_frame_count = -1;
1070 }
1071
1072 if (audio_frame_count != -1 || video_frame_count != -1) {
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001073 // Audio or video is expected to flow, so both clients should reach the
1074 // Connected state, and the offerer (ICE controller) should proceed to
1075 // Completed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001076 // Note: These tests have been observed to fail under heavy load at
1077 // shorter timeouts, so they may be flaky.
1078 EXPECT_EQ_WAIT(
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001079 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001080 initiating_client_->ice_connection_state(),
1081 kMaxWaitForFramesMs);
1082 EXPECT_EQ_WAIT(
1083 webrtc::PeerConnectionInterface::kIceConnectionConnected,
1084 receiving_client_->ice_connection_state(),
1085 kMaxWaitForFramesMs);
1086 }
1087
1088 if (initiating_client_->can_receive_audio() ||
1089 initiating_client_->can_receive_video()) {
1090 // The initiating client can receive media, so it must produce candidates
1091 // that will serve as destinations for that media.
1092 // TODO(bemasc): Understand why the state is not already Complete here, as
1093 // seems to be the case for the receiving client. This may indicate a bug
1094 // in the ICE gathering system.
1095 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1096 initiating_client_->ice_gathering_state());
1097 }
1098 if (receiving_client_->can_receive_audio() ||
1099 receiving_client_->can_receive_video()) {
1100 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1101 receiving_client_->ice_gathering_state(),
1102 kMaxWaitForFramesMs);
1103 }
1104
1105 EXPECT_TRUE_WAIT(FramesNotPending(audio_frame_count, video_frame_count),
1106 kMaxWaitForFramesMs);
1107 }
1108
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001109 void SetupAndVerifyDtlsCall() {
1110 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1111 FakeConstraints setup_constraints;
1112 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1113 true);
1114 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1115 LocalP2PTest();
1116 VerifyRenderedSize(640, 480);
1117 }
1118
1119 PeerConnectionTestClient* CreateDtlsClientWithAlternateKey() {
1120 FakeConstraints setup_constraints;
1121 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1122 true);
1123
1124 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
1125 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
1126 : nullptr);
1127 dtls_identity_store->use_alternate_key();
1128
1129 // Make sure the new client is using a different certificate.
1130 return PeerConnectionTestClient::CreateClientWithDtlsIdentityStore(
kwiberg0eb15ed2015-12-17 03:04:15 -08001131 "New Peer: ", &setup_constraints, nullptr,
1132 std::move(dtls_identity_store));
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001133 }
1134
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001135 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1136 // Messages may get lost on the unreliable DataChannel, so we send multiple
1137 // times to avoid test flakiness.
1138 static const size_t kSendAttempts = 5;
1139
1140 for (size_t i = 0; i < kSendAttempts; ++i) {
1141 dc->Send(DataBuffer(data));
1142 }
1143 }
1144
deadbeefaf1b59c2015-10-15 12:08:41 -07001145 PeerConnectionTestClient* initializing_client() {
1146 return initiating_client_.get();
1147 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001148
1149 // Set the |initiating_client_| to the |client| passed in and return the
1150 // original |initiating_client_|.
1151 PeerConnectionTestClient* set_initializing_client(
1152 PeerConnectionTestClient* client) {
1153 PeerConnectionTestClient* old = initiating_client_.release();
1154 initiating_client_.reset(client);
1155 return old;
1156 }
1157
deadbeefaf1b59c2015-10-15 12:08:41 -07001158 PeerConnectionTestClient* receiving_client() {
1159 return receiving_client_.get();
1160 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001162 // Set the |receiving_client_| to the |client| passed in and return the
1163 // original |receiving_client_|.
1164 PeerConnectionTestClient* set_receiving_client(
1165 PeerConnectionTestClient* client) {
1166 PeerConnectionTestClient* old = receiving_client_.release();
1167 receiving_client_.reset(client);
1168 return old;
1169 }
1170
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001171 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001172 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1173 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1174 rtc::SocketServerScope ss_scope_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001175 rtc::scoped_ptr<PeerConnectionTestClient> initiating_client_;
1176 rtc::scoped_ptr<PeerConnectionTestClient> receiving_client_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001178
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001179// Disable for TSan v2, see
1180// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1181#if !defined(THREAD_SANITIZER)
1182
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001184// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1185// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001186TEST_F(P2PTestConductor, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187 ASSERT_TRUE(CreateTestClients());
1188 LocalP2PTest();
1189 VerifyDtmf();
1190}
1191
1192// This test sets up a Jsep call between two parties and test that we can get a
1193// video aspect ratio of 16:9.
deadbeef7c73bdb2015-12-10 15:10:44 -08001194TEST_F(P2PTestConductor, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195 ASSERT_TRUE(CreateTestClients());
1196 FakeConstraints constraint;
1197 double requested_ratio = 640.0/360;
1198 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1199 SetVideoConstraints(constraint, constraint);
1200 LocalP2PTest();
1201
1202 ASSERT_LE(0, initializing_client()->rendered_height());
1203 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001204 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205 initializing_client()->rendered_height();
1206 EXPECT_LE(requested_ratio, initiating_video_ratio);
1207
1208 ASSERT_LE(0, receiving_client()->rendered_height());
1209 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001210 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211 receiving_client()->rendered_height();
1212 EXPECT_LE(requested_ratio, receiving_video_ratio);
1213}
1214
1215// This test sets up a Jsep call between two parties and test that the
1216// received video has a resolution of 1280*720.
1217// TODO(mallinath): Enable when
1218// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
deadbeef7c73bdb2015-12-10 15:10:44 -08001219TEST_F(P2PTestConductor, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220 ASSERT_TRUE(CreateTestClients());
1221 FakeConstraints constraint;
1222 constraint.SetMandatoryMinWidth(1280);
1223 constraint.SetMandatoryMinHeight(720);
1224 SetVideoConstraints(constraint, constraint);
1225 LocalP2PTest();
1226 VerifyRenderedSize(1280, 720);
1227}
1228
1229// This test sets up a call between two endpoints that are configured to use
1230// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001231TEST_F(P2PTestConductor, LocalP2PTestDtls) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001232 SetupAndVerifyDtlsCall();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001233}
1234
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001235// This test sets up a audio call initially and then upgrades to audio/video,
1236// using DTLS.
deadbeef7c73bdb2015-12-10 15:10:44 -08001237TEST_F(P2PTestConductor, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001238 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001239 FakeConstraints setup_constraints;
1240 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1241 true);
1242 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1243 receiving_client()->SetReceiveAudioVideo(true, false);
1244 LocalP2PTest();
1245 receiving_client()->SetReceiveAudioVideo(true, true);
1246 receiving_client()->Negotiate();
1247}
1248
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001249// This test sets up a call transfer to a new caller with a different DTLS
1250// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001251TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCallee) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001252 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1253 SetupAndVerifyDtlsCall();
1254
1255 // Keeping the original peer around which will still send packets to the
1256 // receiving client. These SRTP packets will be dropped.
1257 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1258 set_initializing_client(CreateDtlsClientWithAlternateKey()));
1259 original_peer->pc()->Close();
1260
1261 SetSignalingReceivers();
1262 receiving_client()->SetExpectIceRestart(true);
1263 LocalP2PTest();
1264 VerifyRenderedSize(640, 480);
1265}
1266
guoweis46383312015-12-17 16:45:59 -08001267// This test sets up a non-bundle call and apply bundle during ICE restart. When
1268// bundle is in effect in the restart, the channel can successfully reset its
1269// DTLS-SRTP context.
1270TEST_F(P2PTestConductor, LocalP2PTestDtlsBundleInIceRestart) {
1271 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1272 FakeConstraints setup_constraints;
1273 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1274 true);
1275 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1276 receiving_client()->RemoveBundleFromReceivedSdp(true);
1277 LocalP2PTest();
1278 VerifyRenderedSize(640, 480);
1279
1280 initializing_client()->IceRestart();
1281 receiving_client()->SetExpectIceRestart(true);
1282 receiving_client()->RemoveBundleFromReceivedSdp(false);
1283 LocalP2PTest();
1284 VerifyRenderedSize(640, 480);
1285}
1286
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001287// This test sets up a call transfer to a new callee with a different DTLS
1288// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001289TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCaller) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001290 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1291 SetupAndVerifyDtlsCall();
1292
1293 // Keeping the original peer around which will still send packets to the
1294 // receiving client. These SRTP packets will be dropped.
1295 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1296 set_receiving_client(CreateDtlsClientWithAlternateKey()));
1297 original_peer->pc()->Close();
1298
1299 SetSignalingReceivers();
1300 initializing_client()->IceRestart();
1301 LocalP2PTest();
1302 VerifyRenderedSize(640, 480);
1303}
1304
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001305// This test sets up a call between two endpoints that are configured to use
1306// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1307// negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001308TEST_F(P2PTestConductor, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001309 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310 FakeConstraints setup_constraints;
1311 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1312 true);
1313 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1314 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1315 LocalP2PTest();
1316 VerifyRenderedSize(640, 480);
1317}
1318
1319// This test sets up a Jsep call between two parties, and the callee only
1320// accept to receive video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001321TEST_F(P2PTestConductor, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322 ASSERT_TRUE(CreateTestClients());
1323 receiving_client()->SetReceiveAudioVideo(false, true);
1324 LocalP2PTest();
1325}
1326
1327// This test sets up a Jsep call between two parties, and the callee only
1328// accept to receive audio.
deadbeef7c73bdb2015-12-10 15:10:44 -08001329TEST_F(P2PTestConductor, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330 ASSERT_TRUE(CreateTestClients());
1331 receiving_client()->SetReceiveAudioVideo(true, false);
1332 LocalP2PTest();
1333}
1334
1335// This test sets up a Jsep call between two parties, and the callee reject both
1336// audio and video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001337TEST_F(P2PTestConductor, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001338 ASSERT_TRUE(CreateTestClients());
1339 receiving_client()->SetReceiveAudioVideo(false, false);
1340 LocalP2PTest();
1341}
1342
1343// This test sets up an audio and video call between two parties. After the call
1344// runs for a while (10 frames), the caller sends an update offer with video
1345// being rejected. Once the re-negotiation is done, the video flow should stop
1346// and the audio flow should continue.
deadbeefc9be0072015-12-14 18:27:57 -08001347TEST_F(P2PTestConductor, UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348 ASSERT_TRUE(CreateTestClients());
1349 LocalP2PTest();
1350 TestUpdateOfferWithRejectedContent();
1351}
1352
1353// This test sets up a Jsep call between two parties. The MSID is removed from
1354// the SDP strings from the caller.
deadbeefc9be0072015-12-14 18:27:57 -08001355TEST_F(P2PTestConductor, LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356 ASSERT_TRUE(CreateTestClients());
1357 receiving_client()->RemoveMsidFromReceivedSdp(true);
1358 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1359 // audio and video is muxed when MSID is disabled. Remove
1360 // SetRemoveBundleFromSdp once
1361 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1362 receiving_client()->RemoveBundleFromReceivedSdp(true);
1363 LocalP2PTest();
1364}
1365
1366// This test sets up a Jsep call between two parties and the initiating peer
1367// sends two steams.
1368// TODO(perkj): Disabled due to
1369// https://code.google.com/p/webrtc/issues/detail?id=1454
deadbeef7c73bdb2015-12-10 15:10:44 -08001370TEST_F(P2PTestConductor, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001371 ASSERT_TRUE(CreateTestClients());
1372 // Set optional video constraint to max 320pixels to decrease CPU usage.
1373 FakeConstraints constraint;
1374 constraint.SetOptionalMaxWidth(320);
1375 SetVideoConstraints(constraint, constraint);
1376 initializing_client()->AddMediaStream(true, true);
1377 initializing_client()->AddMediaStream(false, true);
1378 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1379 LocalP2PTest();
1380 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1381}
1382
1383// Test that we can receive the audio output level from a remote audio track.
deadbeef7c73bdb2015-12-10 15:10:44 -08001384TEST_F(P2PTestConductor, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001385 ASSERT_TRUE(CreateTestClients());
1386 LocalP2PTest();
1387
1388 StreamCollectionInterface* remote_streams =
1389 initializing_client()->remote_streams();
1390 ASSERT_GT(remote_streams->count(), 0u);
1391 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1392 MediaStreamTrackInterface* remote_audio_track =
1393 remote_streams->at(0)->GetAudioTracks()[0];
1394
1395 // Get the audio output level stats. Note that the level is not available
1396 // until a RTCP packet has been received.
1397 EXPECT_TRUE_WAIT(
1398 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1399 kMaxWaitForStatsMs);
1400}
1401
1402// Test that an audio input level is reported.
deadbeef7c73bdb2015-12-10 15:10:44 -08001403TEST_F(P2PTestConductor, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001404 ASSERT_TRUE(CreateTestClients());
1405 LocalP2PTest();
1406
1407 // Get the audio input level stats. The level should be available very
1408 // soon after the test starts.
1409 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1410 kMaxWaitForStatsMs);
1411}
1412
1413// Test that we can get incoming byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001414TEST_F(P2PTestConductor, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001415 ASSERT_TRUE(CreateTestClients());
1416 LocalP2PTest();
1417
1418 StreamCollectionInterface* remote_streams =
1419 initializing_client()->remote_streams();
1420 ASSERT_GT(remote_streams->count(), 0u);
1421 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1422 MediaStreamTrackInterface* remote_audio_track =
1423 remote_streams->at(0)->GetAudioTracks()[0];
1424 EXPECT_TRUE_WAIT(
1425 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1426 kMaxWaitForStatsMs);
1427
1428 MediaStreamTrackInterface* remote_video_track =
1429 remote_streams->at(0)->GetVideoTracks()[0];
1430 EXPECT_TRUE_WAIT(
1431 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1432 kMaxWaitForStatsMs);
1433}
1434
1435// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001436TEST_F(P2PTestConductor, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001437 ASSERT_TRUE(CreateTestClients());
1438 LocalP2PTest();
1439
1440 StreamCollectionInterface* local_streams =
1441 initializing_client()->local_streams();
1442 ASSERT_GT(local_streams->count(), 0u);
1443 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1444 MediaStreamTrackInterface* local_audio_track =
1445 local_streams->at(0)->GetAudioTracks()[0];
1446 EXPECT_TRUE_WAIT(
1447 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1448 kMaxWaitForStatsMs);
1449
1450 MediaStreamTrackInterface* local_video_track =
1451 local_streams->at(0)->GetVideoTracks()[0];
1452 EXPECT_TRUE_WAIT(
1453 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1454 kMaxWaitForStatsMs);
1455}
1456
Joachim Bauch04e5b492015-05-29 09:40:39 +02001457// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
deadbeef7c73bdb2015-12-10 15:10:44 -08001458TEST_F(P2PTestConductor, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001459 PeerConnectionFactory::Options init_options;
1460 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1461 PeerConnectionFactory::Options recv_options;
1462 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001463 ASSERT_TRUE(
1464 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001465 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1466 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1467 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001468 LocalP2PTest();
1469
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001470 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001471 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1472 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1473 initializing_client()->GetDtlsCipherStats(),
1474 kMaxWaitForStatsMs);
1475 EXPECT_EQ(1, init_observer->GetEnumCounter(
1476 webrtc::kEnumCounterAudioSslCipher,
1477 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1478 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001479
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001480 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001481 initializing_client()->GetSrtpCipherStats(),
1482 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001483 EXPECT_EQ(1,
1484 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1485 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001486}
1487
1488// Test that DTLS 1.2 is used if both ends support it.
torbjorng79a5a832016-01-15 07:16:51 -08001489TEST_F(P2PTestConductor, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001490 PeerConnectionFactory::Options init_options;
1491 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1492 PeerConnectionFactory::Options recv_options;
1493 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001494 ASSERT_TRUE(
1495 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001496 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1497 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1498 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001499 LocalP2PTest();
1500
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001501 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001502 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1503 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)),
1504 initializing_client()->GetDtlsCipherStats(),
1505 kMaxWaitForStatsMs);
1506 EXPECT_EQ(1, init_observer->GetEnumCounter(
1507 webrtc::kEnumCounterAudioSslCipher,
1508 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1509 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001510
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001511 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001512 initializing_client()->GetSrtpCipherStats(),
1513 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001514 EXPECT_EQ(1,
1515 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1516 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001517}
1518
1519// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1520// received supports 1.0.
deadbeef7c73bdb2015-12-10 15:10:44 -08001521TEST_F(P2PTestConductor, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001522 PeerConnectionFactory::Options init_options;
1523 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1524 PeerConnectionFactory::Options recv_options;
1525 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001526 ASSERT_TRUE(
1527 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001528 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1529 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1530 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001531 LocalP2PTest();
1532
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001533 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001534 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1535 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1536 initializing_client()->GetDtlsCipherStats(),
1537 kMaxWaitForStatsMs);
1538 EXPECT_EQ(1, init_observer->GetEnumCounter(
1539 webrtc::kEnumCounterAudioSslCipher,
1540 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1541 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001542
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001543 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001544 initializing_client()->GetSrtpCipherStats(),
1545 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001546 EXPECT_EQ(1,
1547 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1548 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001549}
1550
1551// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1552// received supports 1.2.
deadbeef7c73bdb2015-12-10 15:10:44 -08001553TEST_F(P2PTestConductor, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001554 PeerConnectionFactory::Options init_options;
1555 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1556 PeerConnectionFactory::Options recv_options;
1557 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001558 ASSERT_TRUE(
1559 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001560 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1561 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1562 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001563 LocalP2PTest();
1564
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001565 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001566 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1567 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1568 initializing_client()->GetDtlsCipherStats(),
1569 kMaxWaitForStatsMs);
1570 EXPECT_EQ(1, init_observer->GetEnumCounter(
1571 webrtc::kEnumCounterAudioSslCipher,
1572 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1573 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001574
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001575 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001576 initializing_client()->GetSrtpCipherStats(),
1577 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001578 EXPECT_EQ(1,
1579 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1580 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001581}
1582
deadbeefb5cb19b2015-11-23 16:39:12 -08001583// This test sets up a call between two parties with audio, video and an RTP
1584// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001585TEST_F(P2PTestConductor, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 FakeConstraints setup_constraints;
1587 setup_constraints.SetAllowRtpDataChannels();
1588 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1589 initializing_client()->CreateDataChannel();
1590 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001591 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1592 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1594 kMaxWaitMs);
1595 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1596 kMaxWaitMs);
1597
1598 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001599
1600 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001601 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1602 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001603
1604 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1606 kMaxWaitMs);
1607
1608 receiving_client()->data_channel()->Close();
1609 // Send new offer and answer.
1610 receiving_client()->Negotiate();
1611 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1612 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1613}
1614
deadbeefb5cb19b2015-11-23 16:39:12 -08001615// This test sets up a call between two parties with audio, video and an SCTP
1616// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001617TEST_F(P2PTestConductor, LocalP2PTestSctpDataChannel) {
deadbeefb5cb19b2015-11-23 16:39:12 -08001618 ASSERT_TRUE(CreateTestClients());
1619 initializing_client()->CreateDataChannel();
1620 LocalP2PTest();
1621 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1622 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
1623 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1624 kMaxWaitMs);
1625 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
1626
1627 std::string data = "hello world";
1628
1629 initializing_client()->data_channel()->Send(DataBuffer(data));
1630 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1631 kMaxWaitMs);
1632
1633 receiving_client()->data_channel()->Send(DataBuffer(data));
1634 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1635 kMaxWaitMs);
1636
1637 receiving_client()->data_channel()->Close();
deadbeef15887932015-12-14 19:32:34 -08001638 EXPECT_TRUE_WAIT(!initializing_client()->data_observer()->IsOpen(),
1639 kMaxWaitMs);
1640 EXPECT_TRUE_WAIT(!receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
deadbeefb5cb19b2015-11-23 16:39:12 -08001641}
1642
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001643// This test sets up a call between two parties and creates a data channel.
1644// The test tests that received data is buffered unless an observer has been
1645// registered.
1646// Rtp data channels can receive data before the underlying
1647// transport has detected that a channel is writable and thus data can be
1648// received before the data channel state changes to open. That is hard to test
1649// but the same buffering is used in that case.
deadbeef7c73bdb2015-12-10 15:10:44 -08001650TEST_F(P2PTestConductor, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001651 FakeConstraints setup_constraints;
1652 setup_constraints.SetAllowRtpDataChannels();
1653 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1654 initializing_client()->CreateDataChannel();
1655 initializing_client()->Negotiate();
1656
deadbeefaf1b59c2015-10-15 12:08:41 -07001657 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1658 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1660 kMaxWaitMs);
1661 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1662 receiving_client()->data_channel()->state(), kMaxWaitMs);
1663
1664 // Unregister the existing observer.
1665 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001666
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001668 SendRtpData(initializing_client()->data_channel(), data);
1669
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001670 // Wait a while to allow the sent data to arrive before an observer is
1671 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001672 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001673
1674 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1675 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1676}
1677
1678// This test sets up a call between two parties with audio, video and but only
1679// the initiating client support data.
deadbeef7c73bdb2015-12-10 15:10:44 -08001680TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001681 FakeConstraints setup_constraints_1;
1682 setup_constraints_1.SetAllowRtpDataChannels();
1683 // Must disable DTLS to make negotiation succeed.
1684 setup_constraints_1.SetMandatory(
1685 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1686 FakeConstraints setup_constraints_2;
1687 setup_constraints_2.SetMandatory(
1688 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1689 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001690 initializing_client()->CreateDataChannel();
1691 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001692 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001693 EXPECT_FALSE(receiving_client()->data_channel());
1694 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1695}
1696
1697// This test sets up a call between two parties with audio, video. When audio
1698// and video is setup and flowing and data channel is negotiated.
deadbeef7c73bdb2015-12-10 15:10:44 -08001699TEST_F(P2PTestConductor, AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700 FakeConstraints setup_constraints;
1701 setup_constraints.SetAllowRtpDataChannels();
1702 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1703 LocalP2PTest();
1704 initializing_client()->CreateDataChannel();
1705 // Send new offer and answer.
1706 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07001707 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1708 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1710 kMaxWaitMs);
1711 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1712 kMaxWaitMs);
1713}
1714
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001715// This test sets up a Jsep call with SCTP DataChannel and verifies the
1716// negotiation is completed without error.
1717#ifdef HAVE_SCTP
deadbeef7c73bdb2015-12-10 15:10:44 -08001718TEST_F(P2PTestConductor, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001719 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001720 FakeConstraints constraints;
1721 constraints.SetMandatory(
1722 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1723 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1724 initializing_client()->CreateDataChannel();
1725 initializing_client()->Negotiate(false, false);
1726}
1727#endif
1728
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729// This test sets up a call between two parties with audio, and video.
1730// During the call, the initializing side restart ice and the test verifies that
1731// new ice candidates are generated and audio and video still can flow.
deadbeef7c73bdb2015-12-10 15:10:44 -08001732TEST_F(P2PTestConductor, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733 ASSERT_TRUE(CreateTestClients());
1734
1735 // Negotiate and wait for ice completion and make sure audio and video plays.
1736 LocalP2PTest();
1737
1738 // Create a SDP string of the first audio candidate for both clients.
1739 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1740 initializing_client()->pc()->local_description()->candidates(0);
1741 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1742 receiving_client()->pc()->local_description()->candidates(0);
1743 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1744 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1745 std::string initiator_candidate;
1746 EXPECT_TRUE(
1747 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1748 std::string receiver_candidate;
1749 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1750
1751 // Restart ice on the initializing client.
1752 receiving_client()->SetExpectIceRestart(true);
1753 initializing_client()->IceRestart();
1754
1755 // Negotiate and wait for ice completion again and make sure audio and video
1756 // plays.
1757 LocalP2PTest();
1758
1759 // Create a SDP string of the first audio candidate for both clients again.
1760 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1761 initializing_client()->pc()->local_description()->candidates(0);
1762 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1763 receiving_client()->pc()->local_description()->candidates(0);
1764 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1765 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1766 std::string initiator_candidate_restart;
1767 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1768 &initiator_candidate_restart));
1769 std::string receiver_candidate_restart;
1770 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1771 &receiver_candidate_restart));
1772
1773 // Verify that the first candidates in the local session descriptions has
1774 // changed.
1775 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1776 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1777}
1778
deadbeeffaac4972015-11-12 15:33:07 -08001779// This test sets up a call between two parties with audio, and video.
1780// It then renegotiates setting the video m-line to "port 0", then later
1781// renegotiates again, enabling video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001782TEST_F(P2PTestConductor, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08001783 ASSERT_TRUE(CreateTestClients());
1784
1785 // Do initial negotiation. Will result in video and audio sendonly m-lines.
1786 receiving_client()->set_auto_add_stream(false);
1787 initializing_client()->AddMediaStream(true, true);
1788 initializing_client()->Negotiate();
1789
1790 // Negotiate again, disabling the video m-line (receiving client will
1791 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
1792 receiving_client()->SetReceiveVideo(false);
1793 initializing_client()->Negotiate();
1794
1795 // Enable video and do negotiation again, making sure video is received
1796 // end-to-end.
1797 receiving_client()->SetReceiveVideo(true);
1798 receiving_client()->AddMediaStream(true, true);
1799 LocalP2PTest();
1800}
1801
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001802// This test sets up a Jsep call between two parties with external
1803// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001804// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1805// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001806TEST_F(P2PTestConductor, DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807 ASSERT_TRUE(CreateTestClients());
1808 EnableVideoDecoderFactory();
1809 LocalP2PTest();
1810}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001811
deadbeeffac06552015-11-25 11:26:01 -08001812// This tests that if we negotiate after calling CreateSender but before we
1813// have a track, then set a track later, frames from the newly-set track are
1814// received end-to-end.
deadbeef7c73bdb2015-12-10 15:10:44 -08001815TEST_F(P2PTestConductor, EarlyWarmupTest) {
deadbeeffac06552015-11-25 11:26:01 -08001816 ASSERT_TRUE(CreateTestClients());
deadbeefbd7d8f72015-12-18 16:58:44 -08001817 auto audio_sender =
1818 initializing_client()->pc()->CreateSender("audio", "stream_id");
1819 auto video_sender =
1820 initializing_client()->pc()->CreateSender("video", "stream_id");
deadbeeffac06552015-11-25 11:26:01 -08001821 initializing_client()->Negotiate();
1822 // Wait for ICE connection to complete, without any tracks.
1823 // Note that the receiving client WILL (in HandleIncomingOffer) create
1824 // tracks, so it's only the initiator here that's doing early warmup.
1825 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1826 VerifySessionDescriptions();
1827 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1828 initializing_client()->ice_connection_state(),
1829 kMaxWaitForFramesMs);
1830 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1831 receiving_client()->ice_connection_state(),
1832 kMaxWaitForFramesMs);
1833 // Now set the tracks, and expect frames to immediately start flowing.
1834 EXPECT_TRUE(
1835 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
1836 EXPECT_TRUE(
1837 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
1838 EXPECT_TRUE_WAIT(FramesNotPending(kEndAudioFrameCount, kEndVideoFrameCount),
1839 kMaxWaitForFramesMs);
1840}
1841
deadbeef0a6c4ca2015-10-06 11:38:28 -07001842class IceServerParsingTest : public testing::Test {
1843 public:
1844 // Convenience for parsing a single URL.
1845 bool ParseUrl(const std::string& url) {
1846 return ParseUrl(url, std::string(), std::string());
1847 }
1848
1849 bool ParseUrl(const std::string& url,
1850 const std::string& username,
1851 const std::string& password) {
1852 PeerConnectionInterface::IceServers servers;
1853 PeerConnectionInterface::IceServer server;
1854 server.urls.push_back(url);
1855 server.username = username;
1856 server.password = password;
1857 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001858 return webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_);
deadbeef0a6c4ca2015-10-06 11:38:28 -07001859 }
1860
1861 protected:
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001862 cricket::ServerAddresses stun_servers_;
1863 std::vector<cricket::RelayServerConfig> turn_servers_;
deadbeef0a6c4ca2015-10-06 11:38:28 -07001864};
1865
1866// Make sure all STUN/TURN prefixes are parsed correctly.
1867TEST_F(IceServerParsingTest, ParseStunPrefixes) {
1868 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001869 EXPECT_EQ(1U, stun_servers_.size());
1870 EXPECT_EQ(0U, turn_servers_.size());
1871 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001872
1873 EXPECT_TRUE(ParseUrl("stuns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001874 EXPECT_EQ(1U, stun_servers_.size());
1875 EXPECT_EQ(0U, turn_servers_.size());
1876 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001877
1878 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001879 EXPECT_EQ(0U, stun_servers_.size());
1880 EXPECT_EQ(1U, turn_servers_.size());
1881 EXPECT_FALSE(turn_servers_[0].ports[0].secure);
1882 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001883
1884 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001885 EXPECT_EQ(0U, stun_servers_.size());
1886 EXPECT_EQ(1U, turn_servers_.size());
1887 EXPECT_TRUE(turn_servers_[0].ports[0].secure);
1888 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001889
1890 // invalid prefixes
1891 EXPECT_FALSE(ParseUrl("stunn:hostname"));
1892 EXPECT_FALSE(ParseUrl(":hostname"));
1893 EXPECT_FALSE(ParseUrl(":"));
1894 EXPECT_FALSE(ParseUrl(""));
1895}
1896
1897TEST_F(IceServerParsingTest, VerifyDefaults) {
1898 // TURNS defaults
1899 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001900 EXPECT_EQ(1U, turn_servers_.size());
1901 EXPECT_EQ(5349, turn_servers_[0].ports[0].address.port());
1902 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
1903 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001904
1905 // TURN defaults
1906 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001907 EXPECT_EQ(1U, turn_servers_.size());
1908 EXPECT_EQ(3478, turn_servers_[0].ports[0].address.port());
1909 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
1910 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001911
1912 // STUN defaults
1913 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001914 EXPECT_EQ(1U, stun_servers_.size());
1915 EXPECT_EQ(3478, stun_servers_.begin()->port());
1916 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001917}
1918
1919// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
1920// can be parsed correctly.
1921TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
1922 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001923 EXPECT_EQ(1U, stun_servers_.size());
1924 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
1925 EXPECT_EQ(1234, stun_servers_.begin()->port());
1926 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001927
1928 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001929 EXPECT_EQ(1U, stun_servers_.size());
1930 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
1931 EXPECT_EQ(4321, stun_servers_.begin()->port());
1932 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001933
1934 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001935 EXPECT_EQ(1U, stun_servers_.size());
1936 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
1937 EXPECT_EQ(9999, stun_servers_.begin()->port());
1938 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001939
1940 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001941 EXPECT_EQ(1U, stun_servers_.size());
1942 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
1943 EXPECT_EQ(3478, stun_servers_.begin()->port());
1944 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001945
1946 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001947 EXPECT_EQ(1U, stun_servers_.size());
1948 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
1949 EXPECT_EQ(3478, stun_servers_.begin()->port());
1950 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001951
1952 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001953 EXPECT_EQ(1U, stun_servers_.size());
1954 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
1955 EXPECT_EQ(3478, stun_servers_.begin()->port());
1956 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001957
1958 // Try some invalid hostname:port strings.
1959 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
1960 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001961 EXPECT_FALSE(ParseUrl("stun:hostname:port:more"));
1962 EXPECT_FALSE(ParseUrl("stun:hostname:port more"));
deadbeef0a6c4ca2015-10-06 11:38:28 -07001963 EXPECT_FALSE(ParseUrl("stun:hostname:"));
1964 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
1965 EXPECT_FALSE(ParseUrl("stun::5555"));
1966 EXPECT_FALSE(ParseUrl("stun:"));
1967}
1968
1969// Test parsing the "?transport=xxx" part of the URL.
1970TEST_F(IceServerParsingTest, ParseTransport) {
1971 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001972 EXPECT_EQ(1U, turn_servers_.size());
1973 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
1974 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001975
1976 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001977 EXPECT_EQ(1U, turn_servers_.size());
1978 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
1979 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001980
1981 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
1982}
1983
1984// Test parsing ICE username contained in URL.
1985TEST_F(IceServerParsingTest, ParseUsername) {
1986 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001987 EXPECT_EQ(1U, turn_servers_.size());
1988 EXPECT_EQ("user", turn_servers_[0].credentials.username);
1989 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001990
1991 EXPECT_FALSE(ParseUrl("turn:@hostname"));
1992 EXPECT_FALSE(ParseUrl("turn:username@"));
1993 EXPECT_FALSE(ParseUrl("turn:@"));
1994 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
1995}
1996
1997// Test that username and password from IceServer is copied into the resulting
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001998// RelayServerConfig.
deadbeef0a6c4ca2015-10-06 11:38:28 -07001999TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
2000 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002001 EXPECT_EQ(1U, turn_servers_.size());
2002 EXPECT_EQ("username", turn_servers_[0].credentials.username);
2003 EXPECT_EQ("password", turn_servers_[0].credentials.password);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002004}
2005
2006// Ensure that if a server has multiple URLs, each one is parsed.
2007TEST_F(IceServerParsingTest, ParseMultipleUrls) {
2008 PeerConnectionInterface::IceServers servers;
2009 PeerConnectionInterface::IceServer server;
2010 server.urls.push_back("stun:hostname");
2011 server.urls.push_back("turn:hostname");
2012 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002013 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2014 EXPECT_EQ(1U, stun_servers_.size());
2015 EXPECT_EQ(1U, turn_servers_.size());
deadbeef0a6c4ca2015-10-06 11:38:28 -07002016}
2017
Taylor Brandstetter893505d2016-01-07 15:12:48 -08002018// Ensure that TURN servers are given unique priorities,
2019// so that their resulting candidates have unique priorities.
2020TEST_F(IceServerParsingTest, TurnServerPrioritiesUnique) {
2021 PeerConnectionInterface::IceServers servers;
2022 PeerConnectionInterface::IceServer server;
2023 server.urls.push_back("turn:hostname");
2024 server.urls.push_back("turn:hostname2");
2025 servers.push_back(server);
2026 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2027 EXPECT_EQ(2U, turn_servers_.size());
2028 EXPECT_NE(turn_servers_[0].priority, turn_servers_[1].priority);
2029}
2030
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00002031#endif // if !defined(THREAD_SANITIZER)