blob: c1e7e3dfd940c6f9fbc8d4b37066ee776c8f3aa7 [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
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000036#include "talk/session/media/mediasession.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010037#include "webrtc/api/dtmfsender.h"
38#include "webrtc/api/fakemetricsobserver.h"
39#include "webrtc/api/localaudiosource.h"
40#include "webrtc/api/mediastreaminterface.h"
41#include "webrtc/api/peerconnection.h"
42#include "webrtc/api/peerconnectionfactory.h"
43#include "webrtc/api/peerconnectioninterface.h"
44#include "webrtc/api/test/fakeaudiocapturemodule.h"
45#include "webrtc/api/test/fakeconstraints.h"
46#include "webrtc/api/test/fakedtlsidentitystore.h"
47#include "webrtc/api/test/fakeperiodicvideocapturer.h"
48#include "webrtc/api/test/fakevideotrackrenderer.h"
49#include "webrtc/api/test/mockpeerconnectionobservers.h"
50#include "webrtc/api/videosourceinterface.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
deadbeefaf1b59c2015-10-15 12:08:41 -0700389 void OnDataChannel(DataChannelInterface* data_channel) override {
390 LOG(INFO) << id_ << "OnDataChannel";
391 data_channel_ = data_channel;
392 data_observer_.reset(new MockDataChannelObserver(data_channel));
393 }
394
395 void CreateDataChannel() {
396 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, nullptr);
397 ASSERT_TRUE(data_channel_.get() != nullptr);
398 data_observer_.reset(new MockDataChannelObserver(data_channel_));
399 }
400
deadbeeffac06552015-11-25 11:26:01 -0800401 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack(
402 const std::string& stream_label) {
403 FakeConstraints constraints;
404 // Disable highpass filter so that we can get all the test audio frames.
405 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
406 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
407 peer_connection_factory_->CreateAudioSource(&constraints);
408 // TODO(perkj): Test audio source when it is implemented. Currently audio
409 // always use the default input.
410 std::string label = stream_label + kAudioTrackLabelBase;
411 return peer_connection_factory_->CreateAudioTrack(label, source);
412 }
413
414 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack(
415 const std::string& stream_label) {
416 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
417 FakeConstraints source_constraints = video_constraints_;
418 source_constraints.SetMandatoryMaxFrameRate(10);
419
420 cricket::FakeVideoCapturer* fake_capturer =
421 new webrtc::FakePeriodicVideoCapturer();
422 video_capturers_.push_back(fake_capturer);
423 rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
424 peer_connection_factory_->CreateVideoSource(fake_capturer,
425 &source_constraints);
426 std::string label = stream_label + kVideoTrackLabelBase;
427 return peer_connection_factory_->CreateVideoTrack(label, source);
428 }
429
deadbeefaf1b59c2015-10-15 12:08:41 -0700430 DataChannelInterface* data_channel() { return data_channel_; }
431 const MockDataChannelObserver* data_observer() const {
432 return data_observer_.get();
433 }
434
435 webrtc::PeerConnectionInterface* pc() { return peer_connection_.get(); }
436
437 void StopVideoCapturers() {
438 for (std::vector<cricket::VideoCapturer*>::iterator it =
439 video_capturers_.begin();
440 it != video_capturers_.end(); ++it) {
441 (*it)->Stop();
442 }
443 }
444
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445 bool AudioFramesReceivedCheck(int number_of_frames) const {
446 return number_of_frames <= fake_audio_capture_module_->frames_received();
447 }
448
deadbeefc9be0072015-12-14 18:27:57 -0800449 int audio_frames_received() const {
450 return fake_audio_capture_module_->frames_received();
451 }
452
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000453 bool VideoFramesReceivedCheck(int number_of_frames) {
454 if (video_decoder_factory_enabled_) {
455 const std::vector<FakeWebRtcVideoDecoder*>& decoders
456 = fake_video_decoder_factory_->decoders();
457 if (decoders.empty()) {
458 return number_of_frames <= 0;
459 }
460
deadbeefc9be0072015-12-14 18:27:57 -0800461 for (FakeWebRtcVideoDecoder* decoder : decoders) {
462 if (number_of_frames > decoder->GetNumFramesReceived()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463 return false;
464 }
465 }
466 return true;
467 } else {
468 if (fake_video_renderers_.empty()) {
469 return number_of_frames <= 0;
470 }
471
deadbeefc9be0072015-12-14 18:27:57 -0800472 for (const auto& pair : fake_video_renderers_) {
473 if (number_of_frames > pair.second->num_rendered_frames()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474 return false;
475 }
476 }
477 return true;
478 }
479 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700480
deadbeefc9be0072015-12-14 18:27:57 -0800481 int video_frames_received() const {
482 int total = 0;
483 if (video_decoder_factory_enabled_) {
484 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
485 fake_video_decoder_factory_->decoders();
486 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
487 total += decoder->GetNumFramesReceived();
488 }
489 } else {
490 for (const auto& pair : fake_video_renderers_) {
491 total += pair.second->num_rendered_frames();
492 }
493 for (const auto& renderer : removed_fake_video_renderers_) {
494 total += renderer->num_rendered_frames();
495 }
496 }
497 return total;
498 }
499
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500 // Verify the CreateDtmfSender interface
501 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000502 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
503 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504
505 // We can't create a DTMF sender with an invalid audio track or a non local
506 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700507 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000508 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700509 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
510 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511
512 // We should be able to create a DTMF sender from a local track.
513 webrtc::AudioTrackInterface* localtrack =
514 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
515 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700516 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 dtmf_sender->RegisterObserver(observer.get());
518
519 // Test the DtmfSender object just created.
520 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
521 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
522
523 // We don't need to verify that the DTMF tones are actually sent out because
524 // that is already covered by the tests of the lower level components.
525
526 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
527 std::vector<std::string> tones;
528 tones.push_back("1");
529 tones.push_back("a");
530 tones.push_back("");
531 observer->Verify(tones);
532
533 dtmf_sender->UnregisterObserver();
534 }
535
536 // Verifies that the SessionDescription have rejected the appropriate media
537 // content.
538 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700539 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
540 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 const cricket::SessionDescription* remote_desc =
542 peer_connection_->remote_description()->description();
543 const cricket::SessionDescription* local_desc =
544 peer_connection_->local_description()->description();
545
546 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
547 if (remote_audio_content) {
548 const ContentInfo* audio_content =
549 GetFirstAudioContent(local_desc);
550 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
551 }
552
553 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
554 if (remote_video_content) {
555 const ContentInfo* video_content =
556 GetFirstVideoContent(local_desc);
557 EXPECT_EQ(can_receive_video(), !video_content->rejected);
558 }
559 }
560
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700562 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000563 const cricket::SessionDescription* desc =
564 peer_connection_->local_description()->description();
565 const cricket::ContentInfos& contents = desc->contents();
566
567 for (size_t index = 0; index < contents.size(); ++index) {
568 if (contents[index].rejected)
569 continue;
570 const cricket::TransportDescription* transport_desc =
571 desc->GetTransportDescriptionByName(contents[index].name);
572
573 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000574 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575 if (ufragpair_it == ice_ufrag_pwd_.end()) {
576 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000577 ice_ufrag_pwd_[static_cast<int>(index)] =
578 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579 } else if (ExpectIceRestart()) {
580 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
581 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
582 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
583 } else {
584 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
585 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
586 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
587 }
588 }
589 }
590
591 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000592 rtc::scoped_refptr<MockStatsObserver>
593 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000594 EXPECT_TRUE(peer_connection_->GetStats(
595 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700597 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598 return observer->AudioOutputLevel();
599 }
600
601 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000602 rtc::scoped_refptr<MockStatsObserver>
603 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000604 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700605 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700607 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 return observer->AudioInputLevel();
609 }
610
611 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000612 rtc::scoped_refptr<MockStatsObserver>
613 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000614 EXPECT_TRUE(peer_connection_->GetStats(
615 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700617 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 return observer->BytesReceived();
619 }
620
621 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000622 rtc::scoped_refptr<MockStatsObserver>
623 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000624 EXPECT_TRUE(peer_connection_->GetStats(
625 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700627 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628 return observer->BytesSent();
629 }
630
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000631 int GetAvailableReceivedBandwidthStats() {
632 rtc::scoped_refptr<MockStatsObserver>
633 observer(new rtc::RefCountedObject<MockStatsObserver>());
634 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700635 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000636 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700637 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000638 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000639 return bw;
640 }
641
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000642 std::string GetDtlsCipherStats() {
643 rtc::scoped_refptr<MockStatsObserver>
644 observer(new rtc::RefCountedObject<MockStatsObserver>());
645 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700646 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000647 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700648 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000649 return observer->DtlsCipher();
650 }
651
652 std::string GetSrtpCipherStats() {
653 rtc::scoped_refptr<MockStatsObserver>
654 observer(new rtc::RefCountedObject<MockStatsObserver>());
655 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700656 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000657 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700658 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000659 return observer->SrtpCipher();
660 }
661
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662 int rendered_width() {
663 EXPECT_FALSE(fake_video_renderers_.empty());
664 return fake_video_renderers_.empty() ? 1 :
665 fake_video_renderers_.begin()->second->width();
666 }
667
668 int rendered_height() {
669 EXPECT_FALSE(fake_video_renderers_.empty());
670 return fake_video_renderers_.empty() ? 1 :
671 fake_video_renderers_.begin()->second->height();
672 }
673
674 size_t number_of_remote_streams() {
675 if (!pc())
676 return 0;
677 return pc()->remote_streams()->count();
678 }
679
680 StreamCollectionInterface* remote_streams() {
681 if (!pc()) {
682 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700683 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684 }
685 return pc()->remote_streams();
686 }
687
688 StreamCollectionInterface* local_streams() {
689 if (!pc()) {
690 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700691 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 }
693 return pc()->local_streams();
694 }
695
696 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
697 return pc()->signaling_state();
698 }
699
700 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
701 return pc()->ice_connection_state();
702 }
703
704 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
705 return pc()->ice_gathering_state();
706 }
707
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 private:
709 class DummyDtmfObserver : public DtmfSenderObserverInterface {
710 public:
711 DummyDtmfObserver() : completed_(false) {}
712
713 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700714 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 tones_.push_back(tone);
716 if (tone.empty()) {
717 completed_ = true;
718 }
719 }
720
721 void Verify(const std::vector<std::string>& tones) const {
722 ASSERT_TRUE(tones_.size() == tones.size());
723 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
724 }
725
726 bool completed() const { return completed_; }
727
728 private:
729 bool completed_;
730 std::vector<std::string> tones_;
731 };
732
deadbeefaf1b59c2015-10-15 12:08:41 -0700733 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
734
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800735 bool Init(
736 const MediaConstraintsInterface* constraints,
737 const PeerConnectionFactory::Options* options,
738 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700739 EXPECT_TRUE(!peer_connection_);
740 EXPECT_TRUE(!peer_connection_factory_);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800741 rtc::scoped_ptr<cricket::PortAllocator> port_allocator(
742 new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
deadbeefaf1b59c2015-10-15 12:08:41 -0700743 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
744
745 if (fake_audio_capture_module_ == nullptr) {
746 return false;
747 }
748 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
749 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
750 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
751 rtc::Thread::Current(), rtc::Thread::Current(),
752 fake_audio_capture_module_, fake_video_encoder_factory_,
753 fake_video_decoder_factory_);
754 if (!peer_connection_factory_) {
755 return false;
756 }
757 if (options) {
758 peer_connection_factory_->SetOptions(*options);
759 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800760 peer_connection_ = CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800761 std::move(port_allocator), constraints, std::move(dtls_identity_store));
deadbeefaf1b59c2015-10-15 12:08:41 -0700762 return peer_connection_.get() != nullptr;
763 }
764
deadbeefaf1b59c2015-10-15 12:08:41 -0700765 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800766 rtc::scoped_ptr<cricket::PortAllocator> port_allocator,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800767 const MediaConstraintsInterface* constraints,
768 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800769 // CreatePeerConnection with RTCConfiguration.
770 webrtc::PeerConnectionInterface::RTCConfiguration config;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000771 webrtc::PeerConnectionInterface::IceServer ice_server;
772 ice_server.uri = "stun:stun.l.google.com:19302";
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800773 config.servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000774
deadbeefaf1b59c2015-10-15 12:08:41 -0700775 return peer_connection_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800776 config, constraints, std::move(port_allocator),
777 std::move(dtls_identity_store), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778 }
779
780 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700781 LOG(INFO) << id_ << "HandleIncomingOffer ";
deadbeeffaac4972015-11-12 15:33:07 -0800782 if (NumberOfLocalMediaStreams() == 0 && auto_add_stream_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 // If we are not sending any streams ourselves it is time to add some.
784 AddMediaStream(true, true);
785 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000786 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700787 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000789 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 EXPECT_TRUE(DoCreateAnswer(answer.use()));
791 std::string sdp;
792 EXPECT_TRUE(answer->ToString(&sdp));
793 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
deadbeefaf1b59c2015-10-15 12:08:41 -0700794 if (signaling_message_receiver_) {
795 signaling_message_receiver_->ReceiveSdpMessage(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 webrtc::SessionDescriptionInterface::kAnswer, sdp);
797 }
798 }
799
800 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700801 LOG(INFO) << id_ << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000802 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700803 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
805 }
806
807 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
808 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000809 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
810 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 MockCreateSessionDescriptionObserver>());
812 if (offer) {
813 pc()->CreateOffer(observer, &session_description_constraints_);
814 } else {
815 pc()->CreateAnswer(observer, &session_description_constraints_);
816 }
817 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
818 *desc = observer->release_desc();
819 if (observer->result() && ExpectIceRestart()) {
820 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
821 }
822 return observer->result();
823 }
824
825 bool DoCreateOffer(SessionDescriptionInterface** desc) {
826 return DoCreateOfferAnswer(desc, true);
827 }
828
829 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
830 return DoCreateOfferAnswer(desc, false);
831 }
832
833 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000834 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
835 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700837 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 pc()->SetLocalDescription(observer, desc);
839 // Ignore the observer result. If we wait for the result with
840 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
841 // before the offer which is an error.
842 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000843 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 // ProcessMessages waits at least 1ms but processes all messages before
845 // returning. Since this test is synchronous and send messages to the remote
846 // peer whenever a callback is invoked, this can lead to messages being
847 // sent to the remote peer in the wrong order.
848 // TODO(perkj): Find a way to check the result without risking that the
849 // order of sent messages are changed. Ex- by posting all messages that are
850 // sent to the remote peer.
851 return true;
852 }
853
854 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000855 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
856 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000857 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700858 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 pc()->SetRemoteDescription(observer, desc);
860 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
861 return observer->result();
862 }
863
864 // This modifies all received SDP messages before they are processed.
865 void FilterIncomingSdpMessage(std::string* sdp) {
866 if (remove_msid_) {
867 const char kSdpSsrcAttribute[] = "a=ssrc:";
868 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
869 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
870 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
871 }
872 if (remove_bundle_) {
873 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
874 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
875 }
876 if (remove_sdes_) {
877 const char kSdpSdesCryptoAttribute[] = "a=crypto";
878 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
879 }
880 }
881
deadbeefaf1b59c2015-10-15 12:08:41 -0700882 std::string id_;
883
deadbeefaf1b59c2015-10-15 12:08:41 -0700884 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
885 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
886 peer_connection_factory_;
887
deadbeeffaac4972015-11-12 15:33:07 -0800888 bool auto_add_stream_ = true;
889
deadbeefaf1b59c2015-10-15 12:08:41 -0700890 typedef std::pair<std::string, std::string> IceUfragPwdPair;
891 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
892 bool expect_ice_restart_ = false;
893
deadbeefc9be0072015-12-14 18:27:57 -0800894 // Needed to keep track of number of frames sent.
deadbeefaf1b59c2015-10-15 12:08:41 -0700895 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
896 // Needed to keep track of number of frames received.
deadbeefc9be0072015-12-14 18:27:57 -0800897 std::map<std::string, rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
898 fake_video_renderers_;
899 // Needed to ensure frames aren't received for removed tracks.
900 std::vector<rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
901 removed_fake_video_renderers_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700902 // Needed to keep track of number of frames received when external decoder
903 // used.
904 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
905 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
906 bool video_decoder_factory_enabled_ = false;
907 webrtc::FakeConstraints video_constraints_;
908
909 // For remote peer communication.
910 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
911
912 // Store references to the video capturers we've created, so that we can stop
913 // them, if required.
914 std::vector<cricket::VideoCapturer*> video_capturers_;
915
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916 webrtc::FakeConstraints session_description_constraints_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700917 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
918 bool remove_bundle_ =
919 false; // True if bundle should be removed in received SDP.
920 bool remove_sdes_ =
921 false; // True if a=crypto should be removed in received SDP.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000923 rtc::scoped_refptr<DataChannelInterface> data_channel_;
924 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925};
926
deadbeef7c73bdb2015-12-10 15:10:44 -0800927class P2PTestConductor : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 public:
deadbeef7c73bdb2015-12-10 15:10:44 -0800929 P2PTestConductor()
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000930 : pss_(new rtc::PhysicalSocketServer),
931 ss_(new rtc::VirtualSocketServer(pss_.get())),
932 ss_scope_(ss_.get()) {}
933
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934 bool SessionActive() {
935 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000936 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000938
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939 // Return true if the number of frames provided have been received or it is
940 // known that that will never occur (e.g. no frames will be sent or
941 // captured).
942 bool FramesNotPending(int audio_frames_to_receive,
943 int video_frames_to_receive) {
944 return VideoFramesReceivedCheck(video_frames_to_receive) &&
945 AudioFramesReceivedCheck(audio_frames_to_receive);
946 }
947 bool AudioFramesReceivedCheck(int frames_received) {
948 return initiating_client_->AudioFramesReceivedCheck(frames_received) &&
949 receiving_client_->AudioFramesReceivedCheck(frames_received);
950 }
951 bool VideoFramesReceivedCheck(int frames_received) {
952 return initiating_client_->VideoFramesReceivedCheck(frames_received) &&
953 receiving_client_->VideoFramesReceivedCheck(frames_received);
954 }
955 void VerifyDtmf() {
956 initiating_client_->VerifyDtmf();
957 receiving_client_->VerifyDtmf();
958 }
959
960 void TestUpdateOfferWithRejectedContent() {
deadbeefc9be0072015-12-14 18:27:57 -0800961 // Renegotiate, rejecting the video m-line.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962 initiating_client_->Negotiate(true, false);
deadbeefc9be0072015-12-14 18:27:57 -0800963 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
964
965 int pc1_audio_received = initiating_client_->audio_frames_received();
966 int pc1_video_received = initiating_client_->video_frames_received();
967 int pc2_audio_received = receiving_client_->audio_frames_received();
968 int pc2_video_received = receiving_client_->video_frames_received();
969
970 // Wait for some additional audio frames to be received.
971 EXPECT_TRUE_WAIT(initiating_client_->AudioFramesReceivedCheck(
972 pc1_audio_received + kEndAudioFrameCount) &&
973 receiving_client_->AudioFramesReceivedCheck(
974 pc2_audio_received + kEndAudioFrameCount),
975 kMaxWaitForFramesMs);
976
977 // During this time, we shouldn't have received any additional video frames
978 // for the rejected video tracks.
979 EXPECT_EQ(pc1_video_received, initiating_client_->video_frames_received());
980 EXPECT_EQ(pc2_video_received, receiving_client_->video_frames_received());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981 }
982
983 void VerifyRenderedSize(int width, int height) {
984 EXPECT_EQ(width, receiving_client()->rendered_width());
985 EXPECT_EQ(height, receiving_client()->rendered_height());
986 EXPECT_EQ(width, initializing_client()->rendered_width());
987 EXPECT_EQ(height, initializing_client()->rendered_height());
988 }
989
990 void VerifySessionDescriptions() {
991 initiating_client_->VerifyRejectedMediaInSessionDescription();
992 receiving_client_->VerifyRejectedMediaInSessionDescription();
993 initiating_client_->VerifyLocalIceUfragAndPassword();
994 receiving_client_->VerifyLocalIceUfragAndPassword();
995 }
996
deadbeef7c73bdb2015-12-10 15:10:44 -0800997 ~P2PTestConductor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700999 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001000 }
1001 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001002 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 }
1005
deadbeefaf1b59c2015-10-15 12:08:41 -07001006 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001007
1008 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1009 MediaConstraintsInterface* recv_constraints) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001010 return CreateTestClients(init_constraints, nullptr, recv_constraints,
1011 nullptr);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001012 }
1013
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001014 void SetSignalingReceivers() {
1015 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
1016 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1017 }
1018
Joachim Bauch04e5b492015-05-29 09:40:39 +02001019 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1020 PeerConnectionFactory::Options* init_options,
1021 MediaConstraintsInterface* recv_constraints,
1022 PeerConnectionFactory::Options* recv_options) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001023 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
1024 "Caller: ", init_constraints, init_options));
1025 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
1026 "Callee: ", recv_constraints, recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027 if (!initiating_client_ || !receiving_client_) {
1028 return false;
1029 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001030 SetSignalingReceivers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031 return true;
1032 }
1033
1034 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1035 const webrtc::FakeConstraints& recv_constraints) {
1036 initiating_client_->SetVideoConstraints(init_constraints);
1037 receiving_client_->SetVideoConstraints(recv_constraints);
1038 }
1039
1040 void EnableVideoDecoderFactory() {
1041 initiating_client_->EnableVideoDecoderFactory();
1042 receiving_client_->EnableVideoDecoderFactory();
1043 }
1044
1045 // This test sets up a call between two parties. Both parties send static
1046 // frames to each other. Once the test is finished the number of sent frames
1047 // is compared to the number of received frames.
1048 void LocalP2PTest() {
1049 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1050 initiating_client_->AddMediaStream(true, true);
1051 }
1052 initiating_client_->Negotiate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053 // Assert true is used here since next tests are guaranteed to fail and
1054 // would eat up 5 seconds.
1055 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1056 VerifySessionDescriptions();
1057
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058 int audio_frame_count = kEndAudioFrameCount;
1059 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1060 if (!initiating_client_->can_receive_audio() ||
1061 !receiving_client_->can_receive_audio()) {
1062 audio_frame_count = -1;
1063 }
1064 int video_frame_count = kEndVideoFrameCount;
1065 if (!initiating_client_->can_receive_video() ||
1066 !receiving_client_->can_receive_video()) {
1067 video_frame_count = -1;
1068 }
1069
1070 if (audio_frame_count != -1 || video_frame_count != -1) {
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001071 // Audio or video is expected to flow, so both clients should reach the
1072 // Connected state, and the offerer (ICE controller) should proceed to
1073 // Completed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001074 // Note: These tests have been observed to fail under heavy load at
1075 // shorter timeouts, so they may be flaky.
1076 EXPECT_EQ_WAIT(
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001077 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001078 initiating_client_->ice_connection_state(),
1079 kMaxWaitForFramesMs);
1080 EXPECT_EQ_WAIT(
1081 webrtc::PeerConnectionInterface::kIceConnectionConnected,
1082 receiving_client_->ice_connection_state(),
1083 kMaxWaitForFramesMs);
1084 }
1085
1086 if (initiating_client_->can_receive_audio() ||
1087 initiating_client_->can_receive_video()) {
1088 // The initiating client can receive media, so it must produce candidates
1089 // that will serve as destinations for that media.
1090 // TODO(bemasc): Understand why the state is not already Complete here, as
1091 // seems to be the case for the receiving client. This may indicate a bug
1092 // in the ICE gathering system.
1093 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1094 initiating_client_->ice_gathering_state());
1095 }
1096 if (receiving_client_->can_receive_audio() ||
1097 receiving_client_->can_receive_video()) {
1098 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1099 receiving_client_->ice_gathering_state(),
1100 kMaxWaitForFramesMs);
1101 }
1102
1103 EXPECT_TRUE_WAIT(FramesNotPending(audio_frame_count, video_frame_count),
1104 kMaxWaitForFramesMs);
1105 }
1106
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001107 void SetupAndVerifyDtlsCall() {
1108 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1109 FakeConstraints setup_constraints;
1110 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1111 true);
1112 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1113 LocalP2PTest();
1114 VerifyRenderedSize(640, 480);
1115 }
1116
1117 PeerConnectionTestClient* CreateDtlsClientWithAlternateKey() {
1118 FakeConstraints setup_constraints;
1119 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1120 true);
1121
1122 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
1123 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
1124 : nullptr);
1125 dtls_identity_store->use_alternate_key();
1126
1127 // Make sure the new client is using a different certificate.
1128 return PeerConnectionTestClient::CreateClientWithDtlsIdentityStore(
kwiberg0eb15ed2015-12-17 03:04:15 -08001129 "New Peer: ", &setup_constraints, nullptr,
1130 std::move(dtls_identity_store));
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001131 }
1132
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001133 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1134 // Messages may get lost on the unreliable DataChannel, so we send multiple
1135 // times to avoid test flakiness.
1136 static const size_t kSendAttempts = 5;
1137
1138 for (size_t i = 0; i < kSendAttempts; ++i) {
1139 dc->Send(DataBuffer(data));
1140 }
1141 }
1142
deadbeefaf1b59c2015-10-15 12:08:41 -07001143 PeerConnectionTestClient* initializing_client() {
1144 return initiating_client_.get();
1145 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001146
1147 // Set the |initiating_client_| to the |client| passed in and return the
1148 // original |initiating_client_|.
1149 PeerConnectionTestClient* set_initializing_client(
1150 PeerConnectionTestClient* client) {
1151 PeerConnectionTestClient* old = initiating_client_.release();
1152 initiating_client_.reset(client);
1153 return old;
1154 }
1155
deadbeefaf1b59c2015-10-15 12:08:41 -07001156 PeerConnectionTestClient* receiving_client() {
1157 return receiving_client_.get();
1158 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001159
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001160 // Set the |receiving_client_| to the |client| passed in and return the
1161 // original |receiving_client_|.
1162 PeerConnectionTestClient* set_receiving_client(
1163 PeerConnectionTestClient* client) {
1164 PeerConnectionTestClient* old = receiving_client_.release();
1165 receiving_client_.reset(client);
1166 return old;
1167 }
1168
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001169 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001170 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1171 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1172 rtc::SocketServerScope ss_scope_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001173 rtc::scoped_ptr<PeerConnectionTestClient> initiating_client_;
1174 rtc::scoped_ptr<PeerConnectionTestClient> receiving_client_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001175};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001176
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001177// Disable for TSan v2, see
1178// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1179#if !defined(THREAD_SANITIZER)
1180
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001182// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1183// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001184TEST_F(P2PTestConductor, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001185 ASSERT_TRUE(CreateTestClients());
1186 LocalP2PTest();
1187 VerifyDtmf();
1188}
1189
1190// This test sets up a Jsep call between two parties and test that we can get a
1191// video aspect ratio of 16:9.
deadbeef7c73bdb2015-12-10 15:10:44 -08001192TEST_F(P2PTestConductor, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001193 ASSERT_TRUE(CreateTestClients());
1194 FakeConstraints constraint;
1195 double requested_ratio = 640.0/360;
1196 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1197 SetVideoConstraints(constraint, constraint);
1198 LocalP2PTest();
1199
1200 ASSERT_LE(0, initializing_client()->rendered_height());
1201 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001202 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203 initializing_client()->rendered_height();
1204 EXPECT_LE(requested_ratio, initiating_video_ratio);
1205
1206 ASSERT_LE(0, receiving_client()->rendered_height());
1207 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001208 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001209 receiving_client()->rendered_height();
1210 EXPECT_LE(requested_ratio, receiving_video_ratio);
1211}
1212
1213// This test sets up a Jsep call between two parties and test that the
1214// received video has a resolution of 1280*720.
1215// TODO(mallinath): Enable when
1216// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
deadbeef7c73bdb2015-12-10 15:10:44 -08001217TEST_F(P2PTestConductor, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001218 ASSERT_TRUE(CreateTestClients());
1219 FakeConstraints constraint;
1220 constraint.SetMandatoryMinWidth(1280);
1221 constraint.SetMandatoryMinHeight(720);
1222 SetVideoConstraints(constraint, constraint);
1223 LocalP2PTest();
1224 VerifyRenderedSize(1280, 720);
1225}
1226
1227// This test sets up a call between two endpoints that are configured to use
1228// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001229TEST_F(P2PTestConductor, LocalP2PTestDtls) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001230 SetupAndVerifyDtlsCall();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231}
1232
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001233// This test sets up a audio call initially and then upgrades to audio/video,
1234// using DTLS.
deadbeef7c73bdb2015-12-10 15:10:44 -08001235TEST_F(P2PTestConductor, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001236 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001237 FakeConstraints setup_constraints;
1238 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1239 true);
1240 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1241 receiving_client()->SetReceiveAudioVideo(true, false);
1242 LocalP2PTest();
1243 receiving_client()->SetReceiveAudioVideo(true, true);
1244 receiving_client()->Negotiate();
1245}
1246
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001247// This test sets up a call transfer to a new caller with a different DTLS
1248// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001249TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCallee) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001250 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1251 SetupAndVerifyDtlsCall();
1252
1253 // Keeping the original peer around which will still send packets to the
1254 // receiving client. These SRTP packets will be dropped.
1255 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1256 set_initializing_client(CreateDtlsClientWithAlternateKey()));
1257 original_peer->pc()->Close();
1258
1259 SetSignalingReceivers();
1260 receiving_client()->SetExpectIceRestart(true);
1261 LocalP2PTest();
1262 VerifyRenderedSize(640, 480);
1263}
1264
guoweis46383312015-12-17 16:45:59 -08001265// This test sets up a non-bundle call and apply bundle during ICE restart. When
1266// bundle is in effect in the restart, the channel can successfully reset its
1267// DTLS-SRTP context.
1268TEST_F(P2PTestConductor, LocalP2PTestDtlsBundleInIceRestart) {
1269 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1270 FakeConstraints setup_constraints;
1271 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1272 true);
1273 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1274 receiving_client()->RemoveBundleFromReceivedSdp(true);
1275 LocalP2PTest();
1276 VerifyRenderedSize(640, 480);
1277
1278 initializing_client()->IceRestart();
1279 receiving_client()->SetExpectIceRestart(true);
1280 receiving_client()->RemoveBundleFromReceivedSdp(false);
1281 LocalP2PTest();
1282 VerifyRenderedSize(640, 480);
1283}
1284
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001285// This test sets up a call transfer to a new callee with a different DTLS
1286// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001287TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCaller) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001288 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1289 SetupAndVerifyDtlsCall();
1290
1291 // Keeping the original peer around which will still send packets to the
1292 // receiving client. These SRTP packets will be dropped.
1293 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1294 set_receiving_client(CreateDtlsClientWithAlternateKey()));
1295 original_peer->pc()->Close();
1296
1297 SetSignalingReceivers();
1298 initializing_client()->IceRestart();
1299 LocalP2PTest();
1300 VerifyRenderedSize(640, 480);
1301}
1302
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001303// This test sets up a call between two endpoints that are configured to use
1304// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1305// negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001306TEST_F(P2PTestConductor, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001307 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001308 FakeConstraints setup_constraints;
1309 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1310 true);
1311 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1312 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1313 LocalP2PTest();
1314 VerifyRenderedSize(640, 480);
1315}
1316
1317// This test sets up a Jsep call between two parties, and the callee only
1318// accept to receive video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001319TEST_F(P2PTestConductor, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001320 ASSERT_TRUE(CreateTestClients());
1321 receiving_client()->SetReceiveAudioVideo(false, true);
1322 LocalP2PTest();
1323}
1324
1325// This test sets up a Jsep call between two parties, and the callee only
1326// accept to receive audio.
deadbeef7c73bdb2015-12-10 15:10:44 -08001327TEST_F(P2PTestConductor, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001328 ASSERT_TRUE(CreateTestClients());
1329 receiving_client()->SetReceiveAudioVideo(true, false);
1330 LocalP2PTest();
1331}
1332
1333// This test sets up a Jsep call between two parties, and the callee reject both
1334// audio and video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001335TEST_F(P2PTestConductor, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001336 ASSERT_TRUE(CreateTestClients());
1337 receiving_client()->SetReceiveAudioVideo(false, false);
1338 LocalP2PTest();
1339}
1340
1341// This test sets up an audio and video call between two parties. After the call
1342// runs for a while (10 frames), the caller sends an update offer with video
1343// being rejected. Once the re-negotiation is done, the video flow should stop
1344// and the audio flow should continue.
deadbeefc9be0072015-12-14 18:27:57 -08001345TEST_F(P2PTestConductor, UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346 ASSERT_TRUE(CreateTestClients());
1347 LocalP2PTest();
1348 TestUpdateOfferWithRejectedContent();
1349}
1350
1351// This test sets up a Jsep call between two parties. The MSID is removed from
1352// the SDP strings from the caller.
deadbeefc9be0072015-12-14 18:27:57 -08001353TEST_F(P2PTestConductor, LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001354 ASSERT_TRUE(CreateTestClients());
1355 receiving_client()->RemoveMsidFromReceivedSdp(true);
1356 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1357 // audio and video is muxed when MSID is disabled. Remove
1358 // SetRemoveBundleFromSdp once
1359 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1360 receiving_client()->RemoveBundleFromReceivedSdp(true);
1361 LocalP2PTest();
1362}
1363
1364// This test sets up a Jsep call between two parties and the initiating peer
1365// sends two steams.
1366// TODO(perkj): Disabled due to
1367// https://code.google.com/p/webrtc/issues/detail?id=1454
deadbeef7c73bdb2015-12-10 15:10:44 -08001368TEST_F(P2PTestConductor, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369 ASSERT_TRUE(CreateTestClients());
1370 // Set optional video constraint to max 320pixels to decrease CPU usage.
1371 FakeConstraints constraint;
1372 constraint.SetOptionalMaxWidth(320);
1373 SetVideoConstraints(constraint, constraint);
1374 initializing_client()->AddMediaStream(true, true);
1375 initializing_client()->AddMediaStream(false, true);
1376 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1377 LocalP2PTest();
1378 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1379}
1380
1381// Test that we can receive the audio output level from a remote audio track.
deadbeef7c73bdb2015-12-10 15:10:44 -08001382TEST_F(P2PTestConductor, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001383 ASSERT_TRUE(CreateTestClients());
1384 LocalP2PTest();
1385
1386 StreamCollectionInterface* remote_streams =
1387 initializing_client()->remote_streams();
1388 ASSERT_GT(remote_streams->count(), 0u);
1389 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1390 MediaStreamTrackInterface* remote_audio_track =
1391 remote_streams->at(0)->GetAudioTracks()[0];
1392
1393 // Get the audio output level stats. Note that the level is not available
1394 // until a RTCP packet has been received.
1395 EXPECT_TRUE_WAIT(
1396 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1397 kMaxWaitForStatsMs);
1398}
1399
1400// Test that an audio input level is reported.
deadbeef7c73bdb2015-12-10 15:10:44 -08001401TEST_F(P2PTestConductor, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001402 ASSERT_TRUE(CreateTestClients());
1403 LocalP2PTest();
1404
1405 // Get the audio input level stats. The level should be available very
1406 // soon after the test starts.
1407 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1408 kMaxWaitForStatsMs);
1409}
1410
1411// Test that we can get incoming byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001412TEST_F(P2PTestConductor, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001413 ASSERT_TRUE(CreateTestClients());
1414 LocalP2PTest();
1415
1416 StreamCollectionInterface* remote_streams =
1417 initializing_client()->remote_streams();
1418 ASSERT_GT(remote_streams->count(), 0u);
1419 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1420 MediaStreamTrackInterface* remote_audio_track =
1421 remote_streams->at(0)->GetAudioTracks()[0];
1422 EXPECT_TRUE_WAIT(
1423 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1424 kMaxWaitForStatsMs);
1425
1426 MediaStreamTrackInterface* remote_video_track =
1427 remote_streams->at(0)->GetVideoTracks()[0];
1428 EXPECT_TRUE_WAIT(
1429 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1430 kMaxWaitForStatsMs);
1431}
1432
1433// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001434TEST_F(P2PTestConductor, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001435 ASSERT_TRUE(CreateTestClients());
1436 LocalP2PTest();
1437
1438 StreamCollectionInterface* local_streams =
1439 initializing_client()->local_streams();
1440 ASSERT_GT(local_streams->count(), 0u);
1441 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1442 MediaStreamTrackInterface* local_audio_track =
1443 local_streams->at(0)->GetAudioTracks()[0];
1444 EXPECT_TRUE_WAIT(
1445 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1446 kMaxWaitForStatsMs);
1447
1448 MediaStreamTrackInterface* local_video_track =
1449 local_streams->at(0)->GetVideoTracks()[0];
1450 EXPECT_TRUE_WAIT(
1451 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1452 kMaxWaitForStatsMs);
1453}
1454
Joachim Bauch04e5b492015-05-29 09:40:39 +02001455// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
deadbeef7c73bdb2015-12-10 15:10:44 -08001456TEST_F(P2PTestConductor, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001457 PeerConnectionFactory::Options init_options;
1458 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1459 PeerConnectionFactory::Options recv_options;
1460 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001461 ASSERT_TRUE(
1462 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001463 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1464 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1465 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001466 LocalP2PTest();
1467
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001468 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001469 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1470 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1471 initializing_client()->GetDtlsCipherStats(),
1472 kMaxWaitForStatsMs);
1473 EXPECT_EQ(1, init_observer->GetEnumCounter(
1474 webrtc::kEnumCounterAudioSslCipher,
1475 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1476 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001477
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001478 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001479 initializing_client()->GetSrtpCipherStats(),
1480 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001481 EXPECT_EQ(1,
1482 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1483 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001484}
1485
1486// Test that DTLS 1.2 is used if both ends support it.
torbjorng79a5a832016-01-15 07:16:51 -08001487TEST_F(P2PTestConductor, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001488 PeerConnectionFactory::Options init_options;
1489 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1490 PeerConnectionFactory::Options recv_options;
1491 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001492 ASSERT_TRUE(
1493 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001494 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1495 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1496 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001497 LocalP2PTest();
1498
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001499 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001500 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1501 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)),
1502 initializing_client()->GetDtlsCipherStats(),
1503 kMaxWaitForStatsMs);
1504 EXPECT_EQ(1, init_observer->GetEnumCounter(
1505 webrtc::kEnumCounterAudioSslCipher,
1506 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1507 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001508
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001509 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001510 initializing_client()->GetSrtpCipherStats(),
1511 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001512 EXPECT_EQ(1,
1513 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1514 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001515}
1516
1517// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1518// received supports 1.0.
deadbeef7c73bdb2015-12-10 15:10:44 -08001519TEST_F(P2PTestConductor, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001520 PeerConnectionFactory::Options init_options;
1521 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1522 PeerConnectionFactory::Options recv_options;
1523 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001524 ASSERT_TRUE(
1525 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001526 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1527 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1528 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001529 LocalP2PTest();
1530
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001531 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001532 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1533 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1534 initializing_client()->GetDtlsCipherStats(),
1535 kMaxWaitForStatsMs);
1536 EXPECT_EQ(1, init_observer->GetEnumCounter(
1537 webrtc::kEnumCounterAudioSslCipher,
1538 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1539 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001540
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001541 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001542 initializing_client()->GetSrtpCipherStats(),
1543 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001544 EXPECT_EQ(1,
1545 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1546 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001547}
1548
1549// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1550// received supports 1.2.
deadbeef7c73bdb2015-12-10 15:10:44 -08001551TEST_F(P2PTestConductor, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001552 PeerConnectionFactory::Options init_options;
1553 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1554 PeerConnectionFactory::Options recv_options;
1555 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001556 ASSERT_TRUE(
1557 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001558 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1559 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1560 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001561 LocalP2PTest();
1562
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001563 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001564 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1565 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1566 initializing_client()->GetDtlsCipherStats(),
1567 kMaxWaitForStatsMs);
1568 EXPECT_EQ(1, init_observer->GetEnumCounter(
1569 webrtc::kEnumCounterAudioSslCipher,
1570 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1571 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001572
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001573 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001574 initializing_client()->GetSrtpCipherStats(),
1575 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001576 EXPECT_EQ(1,
1577 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1578 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001579}
1580
deadbeefb5cb19b2015-11-23 16:39:12 -08001581// This test sets up a call between two parties with audio, video and an RTP
1582// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001583TEST_F(P2PTestConductor, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584 FakeConstraints setup_constraints;
1585 setup_constraints.SetAllowRtpDataChannels();
1586 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1587 initializing_client()->CreateDataChannel();
1588 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001589 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1590 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1592 kMaxWaitMs);
1593 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1594 kMaxWaitMs);
1595
1596 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001597
1598 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001599 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1600 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001601
1602 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001603 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1604 kMaxWaitMs);
1605
1606 receiving_client()->data_channel()->Close();
1607 // Send new offer and answer.
1608 receiving_client()->Negotiate();
1609 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1610 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1611}
1612
deadbeefb5cb19b2015-11-23 16:39:12 -08001613// This test sets up a call between two parties with audio, video and an SCTP
1614// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001615TEST_F(P2PTestConductor, LocalP2PTestSctpDataChannel) {
deadbeefb5cb19b2015-11-23 16:39:12 -08001616 ASSERT_TRUE(CreateTestClients());
1617 initializing_client()->CreateDataChannel();
1618 LocalP2PTest();
1619 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1620 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
1621 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1622 kMaxWaitMs);
1623 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
1624
1625 std::string data = "hello world";
1626
1627 initializing_client()->data_channel()->Send(DataBuffer(data));
1628 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1629 kMaxWaitMs);
1630
1631 receiving_client()->data_channel()->Send(DataBuffer(data));
1632 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1633 kMaxWaitMs);
1634
1635 receiving_client()->data_channel()->Close();
deadbeef15887932015-12-14 19:32:34 -08001636 EXPECT_TRUE_WAIT(!initializing_client()->data_observer()->IsOpen(),
1637 kMaxWaitMs);
1638 EXPECT_TRUE_WAIT(!receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
deadbeefb5cb19b2015-11-23 16:39:12 -08001639}
1640
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001641// This test sets up a call between two parties and creates a data channel.
1642// The test tests that received data is buffered unless an observer has been
1643// registered.
1644// Rtp data channels can receive data before the underlying
1645// transport has detected that a channel is writable and thus data can be
1646// received before the data channel state changes to open. That is hard to test
1647// but the same buffering is used in that case.
deadbeef7c73bdb2015-12-10 15:10:44 -08001648TEST_F(P2PTestConductor, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649 FakeConstraints setup_constraints;
1650 setup_constraints.SetAllowRtpDataChannels();
1651 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1652 initializing_client()->CreateDataChannel();
1653 initializing_client()->Negotiate();
1654
deadbeefaf1b59c2015-10-15 12:08:41 -07001655 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1656 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001657 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1658 kMaxWaitMs);
1659 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1660 receiving_client()->data_channel()->state(), kMaxWaitMs);
1661
1662 // Unregister the existing observer.
1663 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001664
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001666 SendRtpData(initializing_client()->data_channel(), data);
1667
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668 // Wait a while to allow the sent data to arrive before an observer is
1669 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001670 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671
1672 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1673 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1674}
1675
1676// This test sets up a call between two parties with audio, video and but only
1677// the initiating client support data.
deadbeef7c73bdb2015-12-10 15:10:44 -08001678TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001679 FakeConstraints setup_constraints_1;
1680 setup_constraints_1.SetAllowRtpDataChannels();
1681 // Must disable DTLS to make negotiation succeed.
1682 setup_constraints_1.SetMandatory(
1683 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1684 FakeConstraints setup_constraints_2;
1685 setup_constraints_2.SetMandatory(
1686 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1687 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688 initializing_client()->CreateDataChannel();
1689 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001690 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001691 EXPECT_FALSE(receiving_client()->data_channel());
1692 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1693}
1694
1695// This test sets up a call between two parties with audio, video. When audio
1696// and video is setup and flowing and data channel is negotiated.
deadbeef7c73bdb2015-12-10 15:10:44 -08001697TEST_F(P2PTestConductor, AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698 FakeConstraints setup_constraints;
1699 setup_constraints.SetAllowRtpDataChannels();
1700 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1701 LocalP2PTest();
1702 initializing_client()->CreateDataChannel();
1703 // Send new offer and answer.
1704 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07001705 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1706 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001707 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1708 kMaxWaitMs);
1709 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1710 kMaxWaitMs);
1711}
1712
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001713// This test sets up a Jsep call with SCTP DataChannel and verifies the
1714// negotiation is completed without error.
1715#ifdef HAVE_SCTP
deadbeef7c73bdb2015-12-10 15:10:44 -08001716TEST_F(P2PTestConductor, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001717 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001718 FakeConstraints constraints;
1719 constraints.SetMandatory(
1720 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1721 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1722 initializing_client()->CreateDataChannel();
1723 initializing_client()->Negotiate(false, false);
1724}
1725#endif
1726
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727// This test sets up a call between two parties with audio, and video.
1728// During the call, the initializing side restart ice and the test verifies that
1729// new ice candidates are generated and audio and video still can flow.
deadbeef7c73bdb2015-12-10 15:10:44 -08001730TEST_F(P2PTestConductor, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001731 ASSERT_TRUE(CreateTestClients());
1732
1733 // Negotiate and wait for ice completion and make sure audio and video plays.
1734 LocalP2PTest();
1735
1736 // Create a SDP string of the first audio candidate for both clients.
1737 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1738 initializing_client()->pc()->local_description()->candidates(0);
1739 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1740 receiving_client()->pc()->local_description()->candidates(0);
1741 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1742 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1743 std::string initiator_candidate;
1744 EXPECT_TRUE(
1745 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1746 std::string receiver_candidate;
1747 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1748
1749 // Restart ice on the initializing client.
1750 receiving_client()->SetExpectIceRestart(true);
1751 initializing_client()->IceRestart();
1752
1753 // Negotiate and wait for ice completion again and make sure audio and video
1754 // plays.
1755 LocalP2PTest();
1756
1757 // Create a SDP string of the first audio candidate for both clients again.
1758 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1759 initializing_client()->pc()->local_description()->candidates(0);
1760 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1761 receiving_client()->pc()->local_description()->candidates(0);
1762 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1763 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1764 std::string initiator_candidate_restart;
1765 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1766 &initiator_candidate_restart));
1767 std::string receiver_candidate_restart;
1768 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1769 &receiver_candidate_restart));
1770
1771 // Verify that the first candidates in the local session descriptions has
1772 // changed.
1773 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1774 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1775}
1776
deadbeeffaac4972015-11-12 15:33:07 -08001777// This test sets up a call between two parties with audio, and video.
1778// It then renegotiates setting the video m-line to "port 0", then later
1779// renegotiates again, enabling video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001780TEST_F(P2PTestConductor, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08001781 ASSERT_TRUE(CreateTestClients());
1782
1783 // Do initial negotiation. Will result in video and audio sendonly m-lines.
1784 receiving_client()->set_auto_add_stream(false);
1785 initializing_client()->AddMediaStream(true, true);
1786 initializing_client()->Negotiate();
1787
1788 // Negotiate again, disabling the video m-line (receiving client will
1789 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
1790 receiving_client()->SetReceiveVideo(false);
1791 initializing_client()->Negotiate();
1792
1793 // Enable video and do negotiation again, making sure video is received
1794 // end-to-end.
1795 receiving_client()->SetReceiveVideo(true);
1796 receiving_client()->AddMediaStream(true, true);
1797 LocalP2PTest();
1798}
1799
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001800// This test sets up a Jsep call between two parties with external
1801// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001802// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1803// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001804TEST_F(P2PTestConductor, DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805 ASSERT_TRUE(CreateTestClients());
1806 EnableVideoDecoderFactory();
1807 LocalP2PTest();
1808}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001809
deadbeeffac06552015-11-25 11:26:01 -08001810// This tests that if we negotiate after calling CreateSender but before we
1811// have a track, then set a track later, frames from the newly-set track are
1812// received end-to-end.
deadbeef7c73bdb2015-12-10 15:10:44 -08001813TEST_F(P2PTestConductor, EarlyWarmupTest) {
deadbeeffac06552015-11-25 11:26:01 -08001814 ASSERT_TRUE(CreateTestClients());
deadbeefbd7d8f72015-12-18 16:58:44 -08001815 auto audio_sender =
1816 initializing_client()->pc()->CreateSender("audio", "stream_id");
1817 auto video_sender =
1818 initializing_client()->pc()->CreateSender("video", "stream_id");
deadbeeffac06552015-11-25 11:26:01 -08001819 initializing_client()->Negotiate();
1820 // Wait for ICE connection to complete, without any tracks.
1821 // Note that the receiving client WILL (in HandleIncomingOffer) create
1822 // tracks, so it's only the initiator here that's doing early warmup.
1823 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1824 VerifySessionDescriptions();
1825 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1826 initializing_client()->ice_connection_state(),
1827 kMaxWaitForFramesMs);
1828 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1829 receiving_client()->ice_connection_state(),
1830 kMaxWaitForFramesMs);
1831 // Now set the tracks, and expect frames to immediately start flowing.
1832 EXPECT_TRUE(
1833 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
1834 EXPECT_TRUE(
1835 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
1836 EXPECT_TRUE_WAIT(FramesNotPending(kEndAudioFrameCount, kEndVideoFrameCount),
1837 kMaxWaitForFramesMs);
1838}
1839
deadbeef0a6c4ca2015-10-06 11:38:28 -07001840class IceServerParsingTest : public testing::Test {
1841 public:
1842 // Convenience for parsing a single URL.
1843 bool ParseUrl(const std::string& url) {
1844 return ParseUrl(url, std::string(), std::string());
1845 }
1846
1847 bool ParseUrl(const std::string& url,
1848 const std::string& username,
1849 const std::string& password) {
1850 PeerConnectionInterface::IceServers servers;
1851 PeerConnectionInterface::IceServer server;
1852 server.urls.push_back(url);
1853 server.username = username;
1854 server.password = password;
1855 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001856 return webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_);
deadbeef0a6c4ca2015-10-06 11:38:28 -07001857 }
1858
1859 protected:
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001860 cricket::ServerAddresses stun_servers_;
1861 std::vector<cricket::RelayServerConfig> turn_servers_;
deadbeef0a6c4ca2015-10-06 11:38:28 -07001862};
1863
1864// Make sure all STUN/TURN prefixes are parsed correctly.
1865TEST_F(IceServerParsingTest, ParseStunPrefixes) {
1866 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001867 EXPECT_EQ(1U, stun_servers_.size());
1868 EXPECT_EQ(0U, turn_servers_.size());
1869 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001870
1871 EXPECT_TRUE(ParseUrl("stuns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001872 EXPECT_EQ(1U, stun_servers_.size());
1873 EXPECT_EQ(0U, turn_servers_.size());
1874 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001875
1876 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001877 EXPECT_EQ(0U, stun_servers_.size());
1878 EXPECT_EQ(1U, turn_servers_.size());
1879 EXPECT_FALSE(turn_servers_[0].ports[0].secure);
1880 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001881
1882 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001883 EXPECT_EQ(0U, stun_servers_.size());
1884 EXPECT_EQ(1U, turn_servers_.size());
1885 EXPECT_TRUE(turn_servers_[0].ports[0].secure);
1886 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001887
1888 // invalid prefixes
1889 EXPECT_FALSE(ParseUrl("stunn:hostname"));
1890 EXPECT_FALSE(ParseUrl(":hostname"));
1891 EXPECT_FALSE(ParseUrl(":"));
1892 EXPECT_FALSE(ParseUrl(""));
1893}
1894
1895TEST_F(IceServerParsingTest, VerifyDefaults) {
1896 // TURNS defaults
1897 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001898 EXPECT_EQ(1U, turn_servers_.size());
1899 EXPECT_EQ(5349, turn_servers_[0].ports[0].address.port());
1900 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
1901 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001902
1903 // TURN defaults
1904 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001905 EXPECT_EQ(1U, turn_servers_.size());
1906 EXPECT_EQ(3478, turn_servers_[0].ports[0].address.port());
1907 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
1908 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001909
1910 // STUN defaults
1911 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001912 EXPECT_EQ(1U, stun_servers_.size());
1913 EXPECT_EQ(3478, stun_servers_.begin()->port());
1914 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001915}
1916
1917// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
1918// can be parsed correctly.
1919TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
1920 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001921 EXPECT_EQ(1U, stun_servers_.size());
1922 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
1923 EXPECT_EQ(1234, stun_servers_.begin()->port());
1924 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001925
1926 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001927 EXPECT_EQ(1U, stun_servers_.size());
1928 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
1929 EXPECT_EQ(4321, stun_servers_.begin()->port());
1930 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001931
1932 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001933 EXPECT_EQ(1U, stun_servers_.size());
1934 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
1935 EXPECT_EQ(9999, stun_servers_.begin()->port());
1936 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001937
1938 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001939 EXPECT_EQ(1U, stun_servers_.size());
1940 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
1941 EXPECT_EQ(3478, stun_servers_.begin()->port());
1942 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001943
1944 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001945 EXPECT_EQ(1U, stun_servers_.size());
1946 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
1947 EXPECT_EQ(3478, stun_servers_.begin()->port());
1948 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001949
1950 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001951 EXPECT_EQ(1U, stun_servers_.size());
1952 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
1953 EXPECT_EQ(3478, stun_servers_.begin()->port());
1954 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001955
1956 // Try some invalid hostname:port strings.
1957 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
1958 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001959 EXPECT_FALSE(ParseUrl("stun:hostname:port:more"));
1960 EXPECT_FALSE(ParseUrl("stun:hostname:port more"));
deadbeef0a6c4ca2015-10-06 11:38:28 -07001961 EXPECT_FALSE(ParseUrl("stun:hostname:"));
1962 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
1963 EXPECT_FALSE(ParseUrl("stun::5555"));
1964 EXPECT_FALSE(ParseUrl("stun:"));
1965}
1966
1967// Test parsing the "?transport=xxx" part of the URL.
1968TEST_F(IceServerParsingTest, ParseTransport) {
1969 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001970 EXPECT_EQ(1U, turn_servers_.size());
1971 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
1972 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001973
1974 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001975 EXPECT_EQ(1U, turn_servers_.size());
1976 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
1977 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001978
1979 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
1980}
1981
1982// Test parsing ICE username contained in URL.
1983TEST_F(IceServerParsingTest, ParseUsername) {
1984 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001985 EXPECT_EQ(1U, turn_servers_.size());
1986 EXPECT_EQ("user", turn_servers_[0].credentials.username);
1987 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07001988
1989 EXPECT_FALSE(ParseUrl("turn:@hostname"));
1990 EXPECT_FALSE(ParseUrl("turn:username@"));
1991 EXPECT_FALSE(ParseUrl("turn:@"));
1992 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
1993}
1994
1995// Test that username and password from IceServer is copied into the resulting
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001996// RelayServerConfig.
deadbeef0a6c4ca2015-10-06 11:38:28 -07001997TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
1998 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08001999 EXPECT_EQ(1U, turn_servers_.size());
2000 EXPECT_EQ("username", turn_servers_[0].credentials.username);
2001 EXPECT_EQ("password", turn_servers_[0].credentials.password);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002002}
2003
2004// Ensure that if a server has multiple URLs, each one is parsed.
2005TEST_F(IceServerParsingTest, ParseMultipleUrls) {
2006 PeerConnectionInterface::IceServers servers;
2007 PeerConnectionInterface::IceServer server;
2008 server.urls.push_back("stun:hostname");
2009 server.urls.push_back("turn:hostname");
2010 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002011 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2012 EXPECT_EQ(1U, stun_servers_.size());
2013 EXPECT_EQ(1U, turn_servers_.size());
deadbeef0a6c4ca2015-10-06 11:38:28 -07002014}
2015
Taylor Brandstetter893505d2016-01-07 15:12:48 -08002016// Ensure that TURN servers are given unique priorities,
2017// so that their resulting candidates have unique priorities.
2018TEST_F(IceServerParsingTest, TurnServerPrioritiesUnique) {
2019 PeerConnectionInterface::IceServers servers;
2020 PeerConnectionInterface::IceServer server;
2021 server.urls.push_back("turn:hostname");
2022 server.urls.push_back("turn:hostname2");
2023 servers.push_back(server);
2024 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2025 EXPECT_EQ(2U, turn_servers_.size());
2026 EXPECT_NE(turn_servers_[0].priority, turn_servers_[1].priority);
2027}
2028
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00002029#endif // if !defined(THREAD_SANITIZER)