blob: abe5e1ceb631cdb34ccf1d073b20dd0225b4cb2e [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>
33#include <vector>
34
35#include "talk/app/webrtc/dtmfsender.h"
jbauchac8869e2015-07-03 01:36:14 -070036#include "talk/app/webrtc/fakemetricsobserver.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037#include "talk/app/webrtc/fakeportallocatorfactory.h"
38#include "talk/app/webrtc/localaudiosource.h"
39#include "talk/app/webrtc/mediastreaminterface.h"
deadbeef0a6c4ca2015-10-06 11:38:28 -070040#include "talk/app/webrtc/peerconnection.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041#include "talk/app/webrtc/peerconnectionfactory.h"
42#include "talk/app/webrtc/peerconnectioninterface.h"
43#include "talk/app/webrtc/test/fakeaudiocapturemodule.h"
44#include "talk/app/webrtc/test/fakeconstraints.h"
Henrik Boström5e56c592015-08-11 10:33:13 +020045#include "talk/app/webrtc/test/fakedtlsidentitystore.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046#include "talk/app/webrtc/test/fakeperiodicvideocapturer.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000047#include "talk/app/webrtc/test/fakevideotrackrenderer.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048#include "talk/app/webrtc/test/mockpeerconnectionobservers.h"
49#include "talk/app/webrtc/videosourceinterface.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000050#include "talk/media/webrtc/fakewebrtcvideoengine.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000051#include "talk/session/media/mediasession.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000052#include "webrtc/base/gunit.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000053#include "webrtc/base/physicalsocketserver.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000054#include "webrtc/base/scoped_ptr.h"
55#include "webrtc/base/ssladapter.h"
56#include "webrtc/base/sslstreamadapter.h"
57#include "webrtc/base/thread.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000058#include "webrtc/base/virtualsocketserver.h"
59#include "webrtc/p2p/base/constants.h"
60#include "webrtc/p2p/base/sessiondescription.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061
62#define MAYBE_SKIP_TEST(feature) \
63 if (!(feature())) { \
64 LOG(LS_INFO) << "Feature disabled... skipping"; \
65 return; \
66 }
67
68using cricket::ContentInfo;
69using cricket::FakeWebRtcVideoDecoder;
70using cricket::FakeWebRtcVideoDecoderFactory;
71using cricket::FakeWebRtcVideoEncoder;
72using cricket::FakeWebRtcVideoEncoderFactory;
73using cricket::MediaContentDescription;
74using webrtc::DataBuffer;
75using webrtc::DataChannelInterface;
76using webrtc::DtmfSender;
77using webrtc::DtmfSenderInterface;
78using webrtc::DtmfSenderObserverInterface;
79using webrtc::FakeConstraints;
80using webrtc::MediaConstraintsInterface;
81using webrtc::MediaStreamTrackInterface;
82using webrtc::MockCreateSessionDescriptionObserver;
83using webrtc::MockDataChannelObserver;
84using webrtc::MockSetSessionDescriptionObserver;
85using webrtc::MockStatsObserver;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000086using webrtc::PeerConnectionInterface;
Joachim Bauch04e5b492015-05-29 09:40:39 +020087using webrtc::PeerConnectionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088using webrtc::SessionDescriptionInterface;
89using webrtc::StreamCollectionInterface;
90
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000091static const int kMaxWaitMs = 10000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000092// Disable for TSan v2, see
93// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
94// This declaration is also #ifdef'd as it causes uninitialized-variable
95// warnings.
96#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097static const int kMaxWaitForStatsMs = 3000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000098#endif
deadbeefac9d92c2015-10-26 11:48:22 -070099static const int kMaxWaitForActivationMs = 5000;
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +0000100static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101static const int kEndAudioFrameCount = 3;
102static const int kEndVideoFrameCount = 3;
103
104static const char kStreamLabelBase[] = "stream_label";
105static const char kVideoTrackLabelBase[] = "video_track";
106static const char kAudioTrackLabelBase[] = "audio_track";
107static const char kDataChannelLabel[] = "data_channel";
108
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000109// Disable for TSan v2, see
110// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
111// This declaration is also #ifdef'd as it causes unused-variable errors.
112#if !defined(THREAD_SANITIZER)
113// SRTP cipher name negotiated by the tests. This must be updated if the
114// default changes.
115static const char kDefaultSrtpCipher[] = "AES_CM_128_HMAC_SHA1_32";
116#endif
117
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118static void RemoveLinesFromSdp(const std::string& line_start,
119 std::string* sdp) {
120 const char kSdpLineEnd[] = "\r\n";
121 size_t ssrc_pos = 0;
122 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
123 std::string::npos) {
124 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
125 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
126 }
127}
128
129class SignalingMessageReceiver {
130 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 virtual void ReceiveSdpMessage(const std::string& type,
132 std::string& msg) = 0;
133 virtual void ReceiveIceMessage(const std::string& sdp_mid,
134 int sdp_mline_index,
135 const std::string& msg) = 0;
136
137 protected:
deadbeefaf1b59c2015-10-15 12:08:41 -0700138 SignalingMessageReceiver() {}
139 virtual ~SignalingMessageReceiver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140};
141
deadbeefaf1b59c2015-10-15 12:08:41 -0700142class PeerConnectionTestClient : public webrtc::PeerConnectionObserver,
143 public SignalingMessageReceiver {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 public:
deadbeefaf1b59c2015-10-15 12:08:41 -0700145 static PeerConnectionTestClient* CreateClient(
146 const std::string& id,
147 const MediaConstraintsInterface* constraints,
148 const PeerConnectionFactory::Options* options) {
149 PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
150 if (!client->Init(constraints, options)) {
151 delete client;
152 return nullptr;
153 }
154 return client;
155 }
156
157 ~PeerConnectionTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 while (!fake_video_renderers_.empty()) {
159 RenderMap::iterator it = fake_video_renderers_.begin();
160 delete it->second;
161 fake_video_renderers_.erase(it);
162 }
163 }
164
deadbeefaf1b59c2015-10-15 12:08:41 -0700165 void Negotiate() { Negotiate(true, true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166
deadbeefaf1b59c2015-10-15 12:08:41 -0700167 void Negotiate(bool audio, bool video) {
168 rtc::scoped_ptr<SessionDescriptionInterface> offer;
169 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170
deadbeefaf1b59c2015-10-15 12:08:41 -0700171 if (offer->description()->GetContentByName("audio")) {
172 offer->description()->GetContentByName("audio")->rejected = !audio;
173 }
174 if (offer->description()->GetContentByName("video")) {
175 offer->description()->GetContentByName("video")->rejected = !video;
176 }
177
178 std::string sdp;
179 EXPECT_TRUE(offer->ToString(&sdp));
180 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
181 signaling_message_receiver_->ReceiveSdpMessage(
182 webrtc::SessionDescriptionInterface::kOffer, sdp);
183 }
184
185 // SignalingMessageReceiver callback.
186 void ReceiveSdpMessage(const std::string& type, std::string& msg) override {
187 FilterIncomingSdpMessage(&msg);
188 if (type == webrtc::SessionDescriptionInterface::kOffer) {
189 HandleIncomingOffer(msg);
190 } else {
191 HandleIncomingAnswer(msg);
192 }
193 }
194
195 // SignalingMessageReceiver callback.
196 void ReceiveIceMessage(const std::string& sdp_mid,
197 int sdp_mline_index,
198 const std::string& msg) override {
199 LOG(INFO) << id_ << "ReceiveIceMessage";
200 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
201 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
202 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
203 }
204
205 // PeerConnectionObserver callbacks.
206 void OnSignalingChange(
207 webrtc::PeerConnectionInterface::SignalingState new_state) override {
208 EXPECT_EQ(pc()->signaling_state(), new_state);
209 }
210 void OnAddStream(webrtc::MediaStreamInterface* media_stream) override {
211 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
212 const std::string id = media_stream->GetVideoTracks()[i]->id();
213 ASSERT_TRUE(fake_video_renderers_.find(id) ==
214 fake_video_renderers_.end());
215 fake_video_renderers_[id] =
216 new webrtc::FakeVideoTrackRenderer(media_stream->GetVideoTracks()[i]);
217 }
218 }
219 void OnRemoveStream(webrtc::MediaStreamInterface* media_stream) override {}
220 void OnRenegotiationNeeded() override {}
221 void OnIceConnectionChange(
222 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
223 EXPECT_EQ(pc()->ice_connection_state(), new_state);
224 }
225 void OnIceGatheringChange(
226 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
227 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
228 }
229 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
230 LOG(INFO) << id_ << "OnIceCandidate";
231
232 std::string ice_sdp;
233 EXPECT_TRUE(candidate->ToString(&ice_sdp));
234 if (signaling_message_receiver_ == nullptr) {
235 // Remote party may be deleted.
236 return;
237 }
238 signaling_message_receiver_->ReceiveIceMessage(
239 candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
240 }
241
242 void SetVideoConstraints(const webrtc::FakeConstraints& video_constraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243 video_constraints_ = video_constraint;
244 }
245
246 void AddMediaStream(bool audio, bool video) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700247 std::string stream_label =
248 kStreamLabelBase +
249 rtc::ToString<int>(static_cast<int>(pc()->local_streams()->count()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000250 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000251 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252
253 if (audio && can_receive_audio()) {
deadbeefac9d92c2015-10-26 11:48:22 -0700254 stream->AddTrack(CreateLocalAudioTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 }
256 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000257 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258 }
259
deadbeefaf1b59c2015-10-15 12:08:41 -0700260 EXPECT_TRUE(pc()->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261 }
262
deadbeefaf1b59c2015-10-15 12:08:41 -0700263 size_t NumberOfLocalMediaStreams() { return pc()->local_streams()->count(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264
265 bool SessionActive() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700266 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 }
268
269 void set_signaling_message_receiver(
deadbeefaf1b59c2015-10-15 12:08:41 -0700270 SignalingMessageReceiver* signaling_message_receiver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 signaling_message_receiver_ = signaling_message_receiver;
272 }
273
274 void EnableVideoDecoderFactory() {
275 video_decoder_factory_enabled_ = true;
276 fake_video_decoder_factory_->AddSupportedVideoCodecType(
277 webrtc::kVideoCodecVP8);
278 }
279
deadbeefaf1b59c2015-10-15 12:08:41 -0700280 void IceRestart() {
281 session_description_constraints_.SetMandatoryIceRestart(true);
282 SetExpectIceRestart(true);
283 }
284
285 void SetExpectIceRestart(bool expect_restart) {
286 expect_ice_restart_ = expect_restart;
287 }
288
289 bool ExpectIceRestart() const { return expect_ice_restart_; }
290
291 void SetReceiveAudioVideo(bool audio, bool video) {
292 SetReceiveAudio(audio);
293 SetReceiveVideo(video);
294 ASSERT_EQ(audio, can_receive_audio());
295 ASSERT_EQ(video, can_receive_video());
296 }
297
298 void SetReceiveAudio(bool audio) {
299 if (audio && can_receive_audio())
300 return;
301 session_description_constraints_.SetMandatoryReceiveAudio(audio);
302 }
303
304 void SetReceiveVideo(bool video) {
305 if (video && can_receive_video())
306 return;
307 session_description_constraints_.SetMandatoryReceiveVideo(video);
308 }
309
310 void RemoveMsidFromReceivedSdp(bool remove) { remove_msid_ = remove; }
311
312 void RemoveSdesCryptoFromReceivedSdp(bool remove) { remove_sdes_ = remove; }
313
314 void RemoveBundleFromReceivedSdp(bool remove) { remove_bundle_ = remove; }
315
316 bool can_receive_audio() {
317 bool value;
318 if (webrtc::FindConstraint(&session_description_constraints_,
319 MediaConstraintsInterface::kOfferToReceiveAudio,
320 &value, nullptr)) {
321 return value;
322 }
323 return true;
324 }
325
326 bool can_receive_video() {
327 bool value;
328 if (webrtc::FindConstraint(&session_description_constraints_,
329 MediaConstraintsInterface::kOfferToReceiveVideo,
330 &value, nullptr)) {
331 return value;
332 }
333 return true;
334 }
335
336 void OnIceComplete() override { LOG(INFO) << id_ << "OnIceComplete"; }
337
338 void OnDataChannel(DataChannelInterface* data_channel) override {
339 LOG(INFO) << id_ << "OnDataChannel";
340 data_channel_ = data_channel;
341 data_observer_.reset(new MockDataChannelObserver(data_channel));
342 }
343
344 void CreateDataChannel() {
345 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, nullptr);
346 ASSERT_TRUE(data_channel_.get() != nullptr);
347 data_observer_.reset(new MockDataChannelObserver(data_channel_));
348 }
349
deadbeefac9d92c2015-10-26 11:48:22 -0700350 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack(
351 const std::string& stream_label) {
352 FakeConstraints constraints;
353 // Disable highpass filter so that we can get all the test audio frames.
354 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
355 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
356 peer_connection_factory_->CreateAudioSource(&constraints);
357 // TODO(perkj): Test audio source when it is implemented. Currently audio
358 // always use the default input.
359 std::string label = stream_label + kAudioTrackLabelBase;
360 return peer_connection_factory_->CreateAudioTrack(label, source);
361 }
362
363 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack(
364 const std::string& stream_label) {
365 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
366 FakeConstraints source_constraints = video_constraints_;
367 source_constraints.SetMandatoryMaxFrameRate(10);
368
369 cricket::FakeVideoCapturer* fake_capturer =
370 new webrtc::FakePeriodicVideoCapturer();
371 video_capturers_.push_back(fake_capturer);
372 rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
373 peer_connection_factory_->CreateVideoSource(fake_capturer,
374 &source_constraints);
375 std::string label = stream_label + kVideoTrackLabelBase;
376 return peer_connection_factory_->CreateVideoTrack(label, source);
377 }
378
deadbeefaf1b59c2015-10-15 12:08:41 -0700379 DataChannelInterface* data_channel() { return data_channel_; }
380 const MockDataChannelObserver* data_observer() const {
381 return data_observer_.get();
382 }
383
384 webrtc::PeerConnectionInterface* pc() { return peer_connection_.get(); }
385
386 void StopVideoCapturers() {
387 for (std::vector<cricket::VideoCapturer*>::iterator it =
388 video_capturers_.begin();
389 it != video_capturers_.end(); ++it) {
390 (*it)->Stop();
391 }
392 }
393
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394 bool AudioFramesReceivedCheck(int number_of_frames) const {
395 return number_of_frames <= fake_audio_capture_module_->frames_received();
396 }
397
398 bool VideoFramesReceivedCheck(int number_of_frames) {
399 if (video_decoder_factory_enabled_) {
400 const std::vector<FakeWebRtcVideoDecoder*>& decoders
401 = fake_video_decoder_factory_->decoders();
402 if (decoders.empty()) {
403 return number_of_frames <= 0;
404 }
405
406 for (std::vector<FakeWebRtcVideoDecoder*>::const_iterator
407 it = decoders.begin(); it != decoders.end(); ++it) {
408 if (number_of_frames > (*it)->GetNumFramesReceived()) {
409 return false;
410 }
411 }
412 return true;
413 } else {
414 if (fake_video_renderers_.empty()) {
415 return number_of_frames <= 0;
416 }
417
418 for (RenderMap::const_iterator it = fake_video_renderers_.begin();
419 it != fake_video_renderers_.end(); ++it) {
420 if (number_of_frames > it->second->num_rendered_frames()) {
421 return false;
422 }
423 }
424 return true;
425 }
426 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700427
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428 // Verify the CreateDtmfSender interface
429 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000430 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
431 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000432
433 // We can't create a DTMF sender with an invalid audio track or a non local
434 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700435 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000436 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700437 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
438 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439
440 // We should be able to create a DTMF sender from a local track.
441 webrtc::AudioTrackInterface* localtrack =
442 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
443 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700444 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445 dtmf_sender->RegisterObserver(observer.get());
446
447 // Test the DtmfSender object just created.
448 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
449 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
450
451 // We don't need to verify that the DTMF tones are actually sent out because
452 // that is already covered by the tests of the lower level components.
453
454 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
455 std::vector<std::string> tones;
456 tones.push_back("1");
457 tones.push_back("a");
458 tones.push_back("");
459 observer->Verify(tones);
460
461 dtmf_sender->UnregisterObserver();
462 }
463
464 // Verifies that the SessionDescription have rejected the appropriate media
465 // content.
466 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700467 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
468 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469 const cricket::SessionDescription* remote_desc =
470 peer_connection_->remote_description()->description();
471 const cricket::SessionDescription* local_desc =
472 peer_connection_->local_description()->description();
473
474 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
475 if (remote_audio_content) {
476 const ContentInfo* audio_content =
477 GetFirstAudioContent(local_desc);
478 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
479 }
480
481 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
482 if (remote_video_content) {
483 const ContentInfo* video_content =
484 GetFirstVideoContent(local_desc);
485 EXPECT_EQ(can_receive_video(), !video_content->rejected);
486 }
487 }
488
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700490 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 const cricket::SessionDescription* desc =
492 peer_connection_->local_description()->description();
493 const cricket::ContentInfos& contents = desc->contents();
494
495 for (size_t index = 0; index < contents.size(); ++index) {
496 if (contents[index].rejected)
497 continue;
498 const cricket::TransportDescription* transport_desc =
499 desc->GetTransportDescriptionByName(contents[index].name);
500
501 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000502 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 if (ufragpair_it == ice_ufrag_pwd_.end()) {
504 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000505 ice_ufrag_pwd_[static_cast<int>(index)] =
506 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507 } else if (ExpectIceRestart()) {
508 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
509 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
510 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
511 } else {
512 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
513 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
514 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
515 }
516 }
517 }
518
519 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000520 rtc::scoped_refptr<MockStatsObserver>
521 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000522 EXPECT_TRUE(peer_connection_->GetStats(
523 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700525 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 return observer->AudioOutputLevel();
527 }
528
529 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000530 rtc::scoped_refptr<MockStatsObserver>
531 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000532 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700533 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700535 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536 return observer->AudioInputLevel();
537 }
538
539 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000540 rtc::scoped_refptr<MockStatsObserver>
541 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000542 EXPECT_TRUE(peer_connection_->GetStats(
543 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700545 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 return observer->BytesReceived();
547 }
548
549 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000550 rtc::scoped_refptr<MockStatsObserver>
551 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000552 EXPECT_TRUE(peer_connection_->GetStats(
553 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700555 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556 return observer->BytesSent();
557 }
558
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000559 int GetAvailableReceivedBandwidthStats() {
560 rtc::scoped_refptr<MockStatsObserver>
561 observer(new rtc::RefCountedObject<MockStatsObserver>());
562 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700563 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000564 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700565 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000566 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000567 return bw;
568 }
569
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000570 std::string GetDtlsCipherStats() {
571 rtc::scoped_refptr<MockStatsObserver>
572 observer(new rtc::RefCountedObject<MockStatsObserver>());
573 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700574 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000575 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700576 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000577 return observer->DtlsCipher();
578 }
579
580 std::string GetSrtpCipherStats() {
581 rtc::scoped_refptr<MockStatsObserver>
582 observer(new rtc::RefCountedObject<MockStatsObserver>());
583 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700584 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000585 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700586 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000587 return observer->SrtpCipher();
588 }
589
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590 int rendered_width() {
591 EXPECT_FALSE(fake_video_renderers_.empty());
592 return fake_video_renderers_.empty() ? 1 :
593 fake_video_renderers_.begin()->second->width();
594 }
595
596 int rendered_height() {
597 EXPECT_FALSE(fake_video_renderers_.empty());
598 return fake_video_renderers_.empty() ? 1 :
599 fake_video_renderers_.begin()->second->height();
600 }
601
602 size_t number_of_remote_streams() {
603 if (!pc())
604 return 0;
605 return pc()->remote_streams()->count();
606 }
607
608 StreamCollectionInterface* remote_streams() {
609 if (!pc()) {
610 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700611 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 }
613 return pc()->remote_streams();
614 }
615
616 StreamCollectionInterface* local_streams() {
617 if (!pc()) {
618 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700619 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620 }
621 return pc()->local_streams();
622 }
623
624 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
625 return pc()->signaling_state();
626 }
627
628 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
629 return pc()->ice_connection_state();
630 }
631
632 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
633 return pc()->ice_gathering_state();
634 }
635
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636 private:
637 class DummyDtmfObserver : public DtmfSenderObserverInterface {
638 public:
639 DummyDtmfObserver() : completed_(false) {}
640
641 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700642 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 tones_.push_back(tone);
644 if (tone.empty()) {
645 completed_ = true;
646 }
647 }
648
649 void Verify(const std::vector<std::string>& tones) const {
650 ASSERT_TRUE(tones_.size() == tones.size());
651 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
652 }
653
654 bool completed() const { return completed_; }
655
656 private:
657 bool completed_;
658 std::vector<std::string> tones_;
659 };
660
deadbeefaf1b59c2015-10-15 12:08:41 -0700661 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
662
663 bool Init(const MediaConstraintsInterface* constraints,
664 const PeerConnectionFactory::Options* options) {
665 EXPECT_TRUE(!peer_connection_);
666 EXPECT_TRUE(!peer_connection_factory_);
667 allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
668 if (!allocator_factory_) {
669 return false;
670 }
671 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
672
673 if (fake_audio_capture_module_ == nullptr) {
674 return false;
675 }
676 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
677 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
678 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
679 rtc::Thread::Current(), rtc::Thread::Current(),
680 fake_audio_capture_module_, fake_video_encoder_factory_,
681 fake_video_decoder_factory_);
682 if (!peer_connection_factory_) {
683 return false;
684 }
685 if (options) {
686 peer_connection_factory_->SetOptions(*options);
687 }
688 peer_connection_ = CreatePeerConnection(allocator_factory_.get(),
689 constraints);
690 return peer_connection_.get() != nullptr;
691 }
692
deadbeefaf1b59c2015-10-15 12:08:41 -0700693 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
694 webrtc::PortAllocatorFactoryInterface* factory,
695 const MediaConstraintsInterface* constraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 // CreatePeerConnection with IceServers.
697 webrtc::PeerConnectionInterface::IceServers ice_servers;
698 webrtc::PeerConnectionInterface::IceServer ice_server;
699 ice_server.uri = "stun:stun.l.google.com:19302";
700 ice_servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000701
Henrik Boström5e56c592015-08-11 10:33:13 +0200702 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store(
703 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
704 : nullptr);
deadbeefaf1b59c2015-10-15 12:08:41 -0700705 return peer_connection_factory_->CreatePeerConnection(
Henrik Boström5e56c592015-08-11 10:33:13 +0200706 ice_servers, constraints, factory, dtls_identity_store.Pass(), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 }
708
709 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700710 LOG(INFO) << id_ << "HandleIncomingOffer ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 if (NumberOfLocalMediaStreams() == 0) {
712 // If we are not sending any streams ourselves it is time to add some.
713 AddMediaStream(true, true);
714 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000715 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700716 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000718 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 EXPECT_TRUE(DoCreateAnswer(answer.use()));
720 std::string sdp;
721 EXPECT_TRUE(answer->ToString(&sdp));
722 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
deadbeefaf1b59c2015-10-15 12:08:41 -0700723 if (signaling_message_receiver_) {
724 signaling_message_receiver_->ReceiveSdpMessage(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 webrtc::SessionDescriptionInterface::kAnswer, sdp);
726 }
727 }
728
729 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700730 LOG(INFO) << id_ << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000731 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700732 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
734 }
735
736 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
737 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000738 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
739 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740 MockCreateSessionDescriptionObserver>());
741 if (offer) {
742 pc()->CreateOffer(observer, &session_description_constraints_);
743 } else {
744 pc()->CreateAnswer(observer, &session_description_constraints_);
745 }
746 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
747 *desc = observer->release_desc();
748 if (observer->result() && ExpectIceRestart()) {
749 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
750 }
751 return observer->result();
752 }
753
754 bool DoCreateOffer(SessionDescriptionInterface** desc) {
755 return DoCreateOfferAnswer(desc, true);
756 }
757
758 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
759 return DoCreateOfferAnswer(desc, false);
760 }
761
762 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000763 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
764 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700766 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 pc()->SetLocalDescription(observer, desc);
768 // Ignore the observer result. If we wait for the result with
769 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
770 // before the offer which is an error.
771 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000772 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 // ProcessMessages waits at least 1ms but processes all messages before
774 // returning. Since this test is synchronous and send messages to the remote
775 // peer whenever a callback is invoked, this can lead to messages being
776 // sent to the remote peer in the wrong order.
777 // TODO(perkj): Find a way to check the result without risking that the
778 // order of sent messages are changed. Ex- by posting all messages that are
779 // sent to the remote peer.
780 return true;
781 }
782
783 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000784 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
785 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700787 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 pc()->SetRemoteDescription(observer, desc);
789 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
790 return observer->result();
791 }
792
793 // This modifies all received SDP messages before they are processed.
794 void FilterIncomingSdpMessage(std::string* sdp) {
795 if (remove_msid_) {
796 const char kSdpSsrcAttribute[] = "a=ssrc:";
797 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
798 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
799 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
800 }
801 if (remove_bundle_) {
802 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
803 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
804 }
805 if (remove_sdes_) {
806 const char kSdpSdesCryptoAttribute[] = "a=crypto";
807 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
808 }
809 }
810
deadbeefaf1b59c2015-10-15 12:08:41 -0700811 std::string id_;
812
813 rtc::scoped_refptr<webrtc::PortAllocatorFactoryInterface> allocator_factory_;
814 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
815 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
816 peer_connection_factory_;
817
818 typedef std::pair<std::string, std::string> IceUfragPwdPair;
819 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
820 bool expect_ice_restart_ = false;
821
822 // Needed to keep track of number of frames send.
823 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
824 // Needed to keep track of number of frames received.
825 typedef std::map<std::string, webrtc::FakeVideoTrackRenderer*> RenderMap;
826 RenderMap fake_video_renderers_;
827 // Needed to keep track of number of frames received when external decoder
828 // used.
829 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
830 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
831 bool video_decoder_factory_enabled_ = false;
832 webrtc::FakeConstraints video_constraints_;
833
834 // For remote peer communication.
835 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
836
837 // Store references to the video capturers we've created, so that we can stop
838 // them, if required.
839 std::vector<cricket::VideoCapturer*> video_capturers_;
840
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 webrtc::FakeConstraints session_description_constraints_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700842 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
843 bool remove_bundle_ =
844 false; // True if bundle should be removed in received SDP.
845 bool remove_sdes_ =
846 false; // True if a=crypto should be removed in received SDP.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000848 rtc::scoped_refptr<DataChannelInterface> data_channel_;
849 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000850};
851
deadbeefcbc95072015-10-15 19:31:56 -0700852// TODO(deadbeef): Rename this to P2PTestConductor once the Linux memcheck and
853// Windows DrMemory Full bots' blacklists are updated.
854class JsepPeerConnectionP2PTestClient : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 public:
deadbeefcbc95072015-10-15 19:31:56 -0700856 JsepPeerConnectionP2PTestClient()
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000857 : pss_(new rtc::PhysicalSocketServer),
858 ss_(new rtc::VirtualSocketServer(pss_.get())),
859 ss_scope_(ss_.get()) {}
860
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 bool SessionActive() {
862 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000863 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000865
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866 // Return true if the number of frames provided have been received or it is
867 // known that that will never occur (e.g. no frames will be sent or
868 // captured).
869 bool FramesNotPending(int audio_frames_to_receive,
870 int video_frames_to_receive) {
871 return VideoFramesReceivedCheck(video_frames_to_receive) &&
872 AudioFramesReceivedCheck(audio_frames_to_receive);
873 }
874 bool AudioFramesReceivedCheck(int frames_received) {
875 return initiating_client_->AudioFramesReceivedCheck(frames_received) &&
876 receiving_client_->AudioFramesReceivedCheck(frames_received);
877 }
878 bool VideoFramesReceivedCheck(int frames_received) {
879 return initiating_client_->VideoFramesReceivedCheck(frames_received) &&
880 receiving_client_->VideoFramesReceivedCheck(frames_received);
881 }
882 void VerifyDtmf() {
883 initiating_client_->VerifyDtmf();
884 receiving_client_->VerifyDtmf();
885 }
886
887 void TestUpdateOfferWithRejectedContent() {
888 initiating_client_->Negotiate(true, false);
889 EXPECT_TRUE_WAIT(
890 FramesNotPending(kEndAudioFrameCount * 2, kEndVideoFrameCount),
891 kMaxWaitForFramesMs);
892 // There shouldn't be any more video frame after the new offer is
893 // negotiated.
894 EXPECT_FALSE(VideoFramesReceivedCheck(kEndVideoFrameCount + 1));
895 }
896
897 void VerifyRenderedSize(int width, int height) {
898 EXPECT_EQ(width, receiving_client()->rendered_width());
899 EXPECT_EQ(height, receiving_client()->rendered_height());
900 EXPECT_EQ(width, initializing_client()->rendered_width());
901 EXPECT_EQ(height, initializing_client()->rendered_height());
902 }
903
904 void VerifySessionDescriptions() {
905 initiating_client_->VerifyRejectedMediaInSessionDescription();
906 receiving_client_->VerifyRejectedMediaInSessionDescription();
907 initiating_client_->VerifyLocalIceUfragAndPassword();
908 receiving_client_->VerifyLocalIceUfragAndPassword();
909 }
910
deadbeefcbc95072015-10-15 19:31:56 -0700911 ~JsepPeerConnectionP2PTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700913 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 }
915 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700916 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918 }
919
deadbeefaf1b59c2015-10-15 12:08:41 -0700920 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921
922 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
923 MediaConstraintsInterface* recv_constraints) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700924 return CreateTestClients(init_constraints, nullptr, recv_constraints,
925 nullptr);
Joachim Bauch04e5b492015-05-29 09:40:39 +0200926 }
927
928 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
929 PeerConnectionFactory::Options* init_options,
930 MediaConstraintsInterface* recv_constraints,
931 PeerConnectionFactory::Options* recv_options) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700932 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
933 "Caller: ", init_constraints, init_options));
934 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
935 "Callee: ", recv_constraints, recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 if (!initiating_client_ || !receiving_client_) {
937 return false;
938 }
939 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
940 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
941 return true;
942 }
943
944 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
945 const webrtc::FakeConstraints& recv_constraints) {
946 initiating_client_->SetVideoConstraints(init_constraints);
947 receiving_client_->SetVideoConstraints(recv_constraints);
948 }
949
950 void EnableVideoDecoderFactory() {
951 initiating_client_->EnableVideoDecoderFactory();
952 receiving_client_->EnableVideoDecoderFactory();
953 }
954
955 // This test sets up a call between two parties. Both parties send static
956 // frames to each other. Once the test is finished the number of sent frames
957 // is compared to the number of received frames.
958 void LocalP2PTest() {
959 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
960 initiating_client_->AddMediaStream(true, true);
961 }
962 initiating_client_->Negotiate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 // Assert true is used here since next tests are guaranteed to fail and
964 // would eat up 5 seconds.
965 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
966 VerifySessionDescriptions();
967
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000968 int audio_frame_count = kEndAudioFrameCount;
969 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
970 if (!initiating_client_->can_receive_audio() ||
971 !receiving_client_->can_receive_audio()) {
972 audio_frame_count = -1;
973 }
974 int video_frame_count = kEndVideoFrameCount;
975 if (!initiating_client_->can_receive_video() ||
976 !receiving_client_->can_receive_video()) {
977 video_frame_count = -1;
978 }
979
980 if (audio_frame_count != -1 || video_frame_count != -1) {
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000981 // Audio or video is expected to flow, so both clients should reach the
982 // Connected state, and the offerer (ICE controller) should proceed to
983 // Completed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984 // Note: These tests have been observed to fail under heavy load at
985 // shorter timeouts, so they may be flaky.
986 EXPECT_EQ_WAIT(
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000987 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988 initiating_client_->ice_connection_state(),
989 kMaxWaitForFramesMs);
990 EXPECT_EQ_WAIT(
991 webrtc::PeerConnectionInterface::kIceConnectionConnected,
992 receiving_client_->ice_connection_state(),
993 kMaxWaitForFramesMs);
994 }
995
996 if (initiating_client_->can_receive_audio() ||
997 initiating_client_->can_receive_video()) {
998 // The initiating client can receive media, so it must produce candidates
999 // that will serve as destinations for that media.
1000 // TODO(bemasc): Understand why the state is not already Complete here, as
1001 // seems to be the case for the receiving client. This may indicate a bug
1002 // in the ICE gathering system.
1003 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1004 initiating_client_->ice_gathering_state());
1005 }
1006 if (receiving_client_->can_receive_audio() ||
1007 receiving_client_->can_receive_video()) {
1008 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1009 receiving_client_->ice_gathering_state(),
1010 kMaxWaitForFramesMs);
1011 }
1012
1013 EXPECT_TRUE_WAIT(FramesNotPending(audio_frame_count, video_frame_count),
1014 kMaxWaitForFramesMs);
1015 }
1016
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001017 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1018 // Messages may get lost on the unreliable DataChannel, so we send multiple
1019 // times to avoid test flakiness.
1020 static const size_t kSendAttempts = 5;
1021
1022 for (size_t i = 0; i < kSendAttempts; ++i) {
1023 dc->Send(DataBuffer(data));
1024 }
1025 }
1026
deadbeefaf1b59c2015-10-15 12:08:41 -07001027 PeerConnectionTestClient* initializing_client() {
1028 return initiating_client_.get();
1029 }
1030 PeerConnectionTestClient* receiving_client() {
1031 return receiving_client_.get();
1032 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001033
1034 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001035 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1036 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1037 rtc::SocketServerScope ss_scope_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001038 rtc::scoped_ptr<PeerConnectionTestClient> initiating_client_;
1039 rtc::scoped_ptr<PeerConnectionTestClient> receiving_client_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001040};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001042// Disable for TSan v2, see
1043// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1044#if !defined(THREAD_SANITIZER)
1045
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001047// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1048// See issue webrtc/2378.
deadbeefcbc95072015-10-15 19:31:56 -07001049TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001050 ASSERT_TRUE(CreateTestClients());
1051 LocalP2PTest();
1052 VerifyDtmf();
1053}
1054
1055// This test sets up a Jsep call between two parties and test that we can get a
1056// video aspect ratio of 16:9.
deadbeefcbc95072015-10-15 19:31:56 -07001057TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058 ASSERT_TRUE(CreateTestClients());
1059 FakeConstraints constraint;
1060 double requested_ratio = 640.0/360;
1061 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1062 SetVideoConstraints(constraint, constraint);
1063 LocalP2PTest();
1064
1065 ASSERT_LE(0, initializing_client()->rendered_height());
1066 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001067 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001068 initializing_client()->rendered_height();
1069 EXPECT_LE(requested_ratio, initiating_video_ratio);
1070
1071 ASSERT_LE(0, receiving_client()->rendered_height());
1072 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001073 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001074 receiving_client()->rendered_height();
1075 EXPECT_LE(requested_ratio, receiving_video_ratio);
1076}
1077
1078// This test sets up a Jsep call between two parties and test that the
1079// received video has a resolution of 1280*720.
1080// TODO(mallinath): Enable when
1081// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
deadbeefcbc95072015-10-15 19:31:56 -07001082TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001083 ASSERT_TRUE(CreateTestClients());
1084 FakeConstraints constraint;
1085 constraint.SetMandatoryMinWidth(1280);
1086 constraint.SetMandatoryMinHeight(720);
1087 SetVideoConstraints(constraint, constraint);
1088 LocalP2PTest();
1089 VerifyRenderedSize(1280, 720);
1090}
1091
1092// This test sets up a call between two endpoints that are configured to use
1093// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
deadbeefcbc95072015-10-15 19:31:56 -07001094TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtls) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001095 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001096 FakeConstraints setup_constraints;
1097 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1098 true);
1099 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1100 LocalP2PTest();
1101 VerifyRenderedSize(640, 480);
1102}
1103
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001104// This test sets up a audio call initially and then upgrades to audio/video,
1105// using DTLS.
deadbeefcbc95072015-10-15 19:31:56 -07001106TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001107 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001108 FakeConstraints setup_constraints;
1109 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1110 true);
1111 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1112 receiving_client()->SetReceiveAudioVideo(true, false);
1113 LocalP2PTest();
1114 receiving_client()->SetReceiveAudioVideo(true, true);
1115 receiving_client()->Negotiate();
1116}
1117
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118// This test sets up a call between two endpoints that are configured to use
1119// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1120// negotiated and used for transport.
deadbeefcbc95072015-10-15 19:31:56 -07001121TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001122 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001123 FakeConstraints setup_constraints;
1124 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1125 true);
1126 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1127 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1128 LocalP2PTest();
1129 VerifyRenderedSize(640, 480);
1130}
1131
1132// This test sets up a Jsep call between two parties, and the callee only
1133// accept to receive video.
deadbeefcbc95072015-10-15 19:31:56 -07001134TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001135 ASSERT_TRUE(CreateTestClients());
1136 receiving_client()->SetReceiveAudioVideo(false, true);
1137 LocalP2PTest();
1138}
1139
1140// This test sets up a Jsep call between two parties, and the callee only
1141// accept to receive audio.
deadbeefcbc95072015-10-15 19:31:56 -07001142TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001143 ASSERT_TRUE(CreateTestClients());
1144 receiving_client()->SetReceiveAudioVideo(true, false);
1145 LocalP2PTest();
1146}
1147
1148// This test sets up a Jsep call between two parties, and the callee reject both
1149// audio and video.
deadbeefcbc95072015-10-15 19:31:56 -07001150TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001151 ASSERT_TRUE(CreateTestClients());
1152 receiving_client()->SetReceiveAudioVideo(false, false);
1153 LocalP2PTest();
1154}
1155
1156// This test sets up an audio and video call between two parties. After the call
1157// runs for a while (10 frames), the caller sends an update offer with video
1158// being rejected. Once the re-negotiation is done, the video flow should stop
1159// and the audio flow should continue.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001160// Disabled due to b/14955157.
deadbeefcbc95072015-10-15 19:31:56 -07001161TEST_F(JsepPeerConnectionP2PTestClient,
1162 DISABLED_UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001163 ASSERT_TRUE(CreateTestClients());
1164 LocalP2PTest();
1165 TestUpdateOfferWithRejectedContent();
1166}
1167
1168// This test sets up a Jsep call between two parties. The MSID is removed from
1169// the SDP strings from the caller.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001170// Disabled due to b/14955157.
deadbeefcbc95072015-10-15 19:31:56 -07001171TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001172 ASSERT_TRUE(CreateTestClients());
1173 receiving_client()->RemoveMsidFromReceivedSdp(true);
1174 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1175 // audio and video is muxed when MSID is disabled. Remove
1176 // SetRemoveBundleFromSdp once
1177 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1178 receiving_client()->RemoveBundleFromReceivedSdp(true);
1179 LocalP2PTest();
1180}
1181
1182// This test sets up a Jsep call between two parties and the initiating peer
1183// sends two steams.
1184// TODO(perkj): Disabled due to
1185// https://code.google.com/p/webrtc/issues/detail?id=1454
deadbeefcbc95072015-10-15 19:31:56 -07001186TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187 ASSERT_TRUE(CreateTestClients());
1188 // Set optional video constraint to max 320pixels to decrease CPU usage.
1189 FakeConstraints constraint;
1190 constraint.SetOptionalMaxWidth(320);
1191 SetVideoConstraints(constraint, constraint);
1192 initializing_client()->AddMediaStream(true, true);
1193 initializing_client()->AddMediaStream(false, true);
1194 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1195 LocalP2PTest();
1196 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1197}
1198
1199// Test that we can receive the audio output level from a remote audio track.
deadbeefcbc95072015-10-15 19:31:56 -07001200TEST_F(JsepPeerConnectionP2PTestClient, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001201 ASSERT_TRUE(CreateTestClients());
1202 LocalP2PTest();
1203
1204 StreamCollectionInterface* remote_streams =
1205 initializing_client()->remote_streams();
1206 ASSERT_GT(remote_streams->count(), 0u);
1207 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1208 MediaStreamTrackInterface* remote_audio_track =
1209 remote_streams->at(0)->GetAudioTracks()[0];
1210
1211 // Get the audio output level stats. Note that the level is not available
1212 // until a RTCP packet has been received.
1213 EXPECT_TRUE_WAIT(
1214 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1215 kMaxWaitForStatsMs);
1216}
1217
1218// Test that an audio input level is reported.
deadbeefcbc95072015-10-15 19:31:56 -07001219TEST_F(JsepPeerConnectionP2PTestClient, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220 ASSERT_TRUE(CreateTestClients());
1221 LocalP2PTest();
1222
1223 // Get the audio input level stats. The level should be available very
1224 // soon after the test starts.
1225 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1226 kMaxWaitForStatsMs);
1227}
1228
1229// Test that we can get incoming byte counts from both audio and video tracks.
deadbeefcbc95072015-10-15 19:31:56 -07001230TEST_F(JsepPeerConnectionP2PTestClient, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231 ASSERT_TRUE(CreateTestClients());
1232 LocalP2PTest();
1233
1234 StreamCollectionInterface* remote_streams =
1235 initializing_client()->remote_streams();
1236 ASSERT_GT(remote_streams->count(), 0u);
1237 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1238 MediaStreamTrackInterface* remote_audio_track =
1239 remote_streams->at(0)->GetAudioTracks()[0];
1240 EXPECT_TRUE_WAIT(
1241 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1242 kMaxWaitForStatsMs);
1243
1244 MediaStreamTrackInterface* remote_video_track =
1245 remote_streams->at(0)->GetVideoTracks()[0];
1246 EXPECT_TRUE_WAIT(
1247 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1248 kMaxWaitForStatsMs);
1249}
1250
1251// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeefcbc95072015-10-15 19:31:56 -07001252TEST_F(JsepPeerConnectionP2PTestClient, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253 ASSERT_TRUE(CreateTestClients());
1254 LocalP2PTest();
1255
1256 StreamCollectionInterface* local_streams =
1257 initializing_client()->local_streams();
1258 ASSERT_GT(local_streams->count(), 0u);
1259 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1260 MediaStreamTrackInterface* local_audio_track =
1261 local_streams->at(0)->GetAudioTracks()[0];
1262 EXPECT_TRUE_WAIT(
1263 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1264 kMaxWaitForStatsMs);
1265
1266 MediaStreamTrackInterface* local_video_track =
1267 local_streams->at(0)->GetVideoTracks()[0];
1268 EXPECT_TRUE_WAIT(
1269 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1270 kMaxWaitForStatsMs);
1271}
1272
Joachim Bauch04e5b492015-05-29 09:40:39 +02001273// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
deadbeefcbc95072015-10-15 19:31:56 -07001274TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001275 PeerConnectionFactory::Options init_options;
1276 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1277 PeerConnectionFactory::Options recv_options;
1278 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001279 ASSERT_TRUE(
1280 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001281 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1282 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1283 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001284 LocalP2PTest();
1285
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001286 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::GetSslCipherSuiteName(
1287 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1288 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1289 initializing_client()->GetDtlsCipherStats(),
1290 kMaxWaitForStatsMs);
1291 EXPECT_EQ(1, init_observer->GetEnumCounter(
1292 webrtc::kEnumCounterAudioSslCipher,
1293 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1294 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001295
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001296 EXPECT_EQ_WAIT(kDefaultSrtpCipher,
1297 initializing_client()->GetSrtpCipherStats(),
1298 kMaxWaitForStatsMs);
1299 EXPECT_EQ(1, init_observer->GetEnumCounter(
1300 webrtc::kEnumCounterAudioSrtpCipher,
1301 rtc::GetSrtpCryptoSuiteFromName(kDefaultSrtpCipher)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001302}
1303
1304// Test that DTLS 1.2 is used if both ends support it.
deadbeefcbc95072015-10-15 19:31:56 -07001305TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001306 PeerConnectionFactory::Options init_options;
1307 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1308 PeerConnectionFactory::Options recv_options;
1309 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001310 ASSERT_TRUE(
1311 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001312 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1313 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1314 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001315 LocalP2PTest();
1316
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001317 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::GetSslCipherSuiteName(
1318 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1319 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)),
1320 initializing_client()->GetDtlsCipherStats(),
1321 kMaxWaitForStatsMs);
1322 EXPECT_EQ(1, init_observer->GetEnumCounter(
1323 webrtc::kEnumCounterAudioSslCipher,
1324 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1325 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001326
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001327 EXPECT_EQ_WAIT(kDefaultSrtpCipher,
1328 initializing_client()->GetSrtpCipherStats(),
1329 kMaxWaitForStatsMs);
1330 EXPECT_EQ(1, init_observer->GetEnumCounter(
1331 webrtc::kEnumCounterAudioSrtpCipher,
1332 rtc::GetSrtpCryptoSuiteFromName(kDefaultSrtpCipher)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001333}
1334
1335// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1336// received supports 1.0.
deadbeefcbc95072015-10-15 19:31:56 -07001337TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001338 PeerConnectionFactory::Options init_options;
1339 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1340 PeerConnectionFactory::Options recv_options;
1341 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001342 ASSERT_TRUE(
1343 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001344 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1345 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1346 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001347 LocalP2PTest();
1348
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001349 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::GetSslCipherSuiteName(
1350 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1351 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1352 initializing_client()->GetDtlsCipherStats(),
1353 kMaxWaitForStatsMs);
1354 EXPECT_EQ(1, init_observer->GetEnumCounter(
1355 webrtc::kEnumCounterAudioSslCipher,
1356 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1357 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001358
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001359 EXPECT_EQ_WAIT(kDefaultSrtpCipher,
1360 initializing_client()->GetSrtpCipherStats(),
1361 kMaxWaitForStatsMs);
1362 EXPECT_EQ(1, init_observer->GetEnumCounter(
1363 webrtc::kEnumCounterAudioSrtpCipher,
1364 rtc::GetSrtpCryptoSuiteFromName(kDefaultSrtpCipher)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001365}
1366
1367// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1368// received supports 1.2.
deadbeefcbc95072015-10-15 19:31:56 -07001369TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001370 PeerConnectionFactory::Options init_options;
1371 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1372 PeerConnectionFactory::Options recv_options;
1373 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001374 ASSERT_TRUE(
1375 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001376 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1377 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1378 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001379 LocalP2PTest();
1380
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001381 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::GetSslCipherSuiteName(
1382 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1383 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1384 initializing_client()->GetDtlsCipherStats(),
1385 kMaxWaitForStatsMs);
1386 EXPECT_EQ(1, init_observer->GetEnumCounter(
1387 webrtc::kEnumCounterAudioSslCipher,
1388 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1389 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001390
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001391 EXPECT_EQ_WAIT(kDefaultSrtpCipher,
1392 initializing_client()->GetSrtpCipherStats(),
1393 kMaxWaitForStatsMs);
1394 EXPECT_EQ(1, init_observer->GetEnumCounter(
1395 webrtc::kEnumCounterAudioSrtpCipher,
1396 rtc::GetSrtpCryptoSuiteFromName(kDefaultSrtpCipher)));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001397}
1398
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001399// This test sets up a call between two parties with audio, video and data.
deadbeefcbc95072015-10-15 19:31:56 -07001400TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001401 FakeConstraints setup_constraints;
1402 setup_constraints.SetAllowRtpDataChannels();
1403 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1404 initializing_client()->CreateDataChannel();
1405 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001406 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1407 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001408 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1409 kMaxWaitMs);
1410 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1411 kMaxWaitMs);
1412
1413 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001414
1415 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001416 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1417 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001418
1419 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001420 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1421 kMaxWaitMs);
1422
1423 receiving_client()->data_channel()->Close();
1424 // Send new offer and answer.
1425 receiving_client()->Negotiate();
1426 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1427 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1428}
1429
1430// This test sets up a call between two parties and creates a data channel.
1431// The test tests that received data is buffered unless an observer has been
1432// registered.
1433// Rtp data channels can receive data before the underlying
1434// transport has detected that a channel is writable and thus data can be
1435// received before the data channel state changes to open. That is hard to test
1436// but the same buffering is used in that case.
deadbeefcbc95072015-10-15 19:31:56 -07001437TEST_F(JsepPeerConnectionP2PTestClient, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001438 FakeConstraints setup_constraints;
1439 setup_constraints.SetAllowRtpDataChannels();
1440 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1441 initializing_client()->CreateDataChannel();
1442 initializing_client()->Negotiate();
1443
deadbeefaf1b59c2015-10-15 12:08:41 -07001444 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1445 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001446 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1447 kMaxWaitMs);
1448 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1449 receiving_client()->data_channel()->state(), kMaxWaitMs);
1450
1451 // Unregister the existing observer.
1452 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001453
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001454 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001455 SendRtpData(initializing_client()->data_channel(), data);
1456
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001457 // Wait a while to allow the sent data to arrive before an observer is
1458 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001459 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001460
1461 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1462 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1463}
1464
1465// This test sets up a call between two parties with audio, video and but only
1466// the initiating client support data.
deadbeefcbc95072015-10-15 19:31:56 -07001467TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001468 FakeConstraints setup_constraints_1;
1469 setup_constraints_1.SetAllowRtpDataChannels();
1470 // Must disable DTLS to make negotiation succeed.
1471 setup_constraints_1.SetMandatory(
1472 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1473 FakeConstraints setup_constraints_2;
1474 setup_constraints_2.SetMandatory(
1475 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1476 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001477 initializing_client()->CreateDataChannel();
1478 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001479 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001480 EXPECT_FALSE(receiving_client()->data_channel());
1481 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1482}
1483
1484// This test sets up a call between two parties with audio, video. When audio
1485// and video is setup and flowing and data channel is negotiated.
deadbeefcbc95072015-10-15 19:31:56 -07001486TEST_F(JsepPeerConnectionP2PTestClient, AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001487 FakeConstraints setup_constraints;
1488 setup_constraints.SetAllowRtpDataChannels();
1489 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1490 LocalP2PTest();
1491 initializing_client()->CreateDataChannel();
1492 // Send new offer and answer.
1493 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07001494 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1495 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001496 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1497 kMaxWaitMs);
1498 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1499 kMaxWaitMs);
1500}
1501
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001502// This test sets up a Jsep call with SCTP DataChannel and verifies the
1503// negotiation is completed without error.
1504#ifdef HAVE_SCTP
deadbeefcbc95072015-10-15 19:31:56 -07001505TEST_F(JsepPeerConnectionP2PTestClient, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001506 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001507 FakeConstraints constraints;
1508 constraints.SetMandatory(
1509 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1510 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1511 initializing_client()->CreateDataChannel();
1512 initializing_client()->Negotiate(false, false);
1513}
1514#endif
1515
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001516// This test sets up a call between two parties with audio, and video.
1517// During the call, the initializing side restart ice and the test verifies that
1518// new ice candidates are generated and audio and video still can flow.
deadbeefcbc95072015-10-15 19:31:56 -07001519TEST_F(JsepPeerConnectionP2PTestClient, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001520 ASSERT_TRUE(CreateTestClients());
1521
1522 // Negotiate and wait for ice completion and make sure audio and video plays.
1523 LocalP2PTest();
1524
1525 // Create a SDP string of the first audio candidate for both clients.
1526 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1527 initializing_client()->pc()->local_description()->candidates(0);
1528 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1529 receiving_client()->pc()->local_description()->candidates(0);
1530 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1531 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1532 std::string initiator_candidate;
1533 EXPECT_TRUE(
1534 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1535 std::string receiver_candidate;
1536 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1537
1538 // Restart ice on the initializing client.
1539 receiving_client()->SetExpectIceRestart(true);
1540 initializing_client()->IceRestart();
1541
1542 // Negotiate and wait for ice completion again and make sure audio and video
1543 // plays.
1544 LocalP2PTest();
1545
1546 // Create a SDP string of the first audio candidate for both clients again.
1547 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1548 initializing_client()->pc()->local_description()->candidates(0);
1549 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1550 receiving_client()->pc()->local_description()->candidates(0);
1551 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1552 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1553 std::string initiator_candidate_restart;
1554 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1555 &initiator_candidate_restart));
1556 std::string receiver_candidate_restart;
1557 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1558 &receiver_candidate_restart));
1559
1560 // Verify that the first candidates in the local session descriptions has
1561 // changed.
1562 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1563 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1564}
1565
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566// This test sets up a Jsep call between two parties with external
1567// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001568// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1569// See issue webrtc/2378.
deadbeefcbc95072015-10-15 19:31:56 -07001570TEST_F(JsepPeerConnectionP2PTestClient,
1571 DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001572 ASSERT_TRUE(CreateTestClients());
1573 EnableVideoDecoderFactory();
1574 LocalP2PTest();
1575}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001576
deadbeefac9d92c2015-10-26 11:48:22 -07001577// This tests that if we negotiate after calling CreateSender but before we
1578// have a track, then set a track later, frames from the newly-set track are
1579// received end-to-end.
1580TEST_F(JsepPeerConnectionP2PTestClient, EarlyWarmupTest) {
1581 ASSERT_TRUE(CreateTestClients());
1582 auto audio_sender = initializing_client()->pc()->CreateSender("audio");
1583 auto video_sender = initializing_client()->pc()->CreateSender("video");
1584 initializing_client()->Negotiate();
1585 // Wait for ICE connection to complete, without any tracks.
1586 // Note that the receiving client WILL (in HandleIncomingOffer) create
1587 // tracks, so it's only the initiator here that's doing early warmup.
1588 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1589 VerifySessionDescriptions();
1590 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1591 initializing_client()->ice_connection_state(),
1592 kMaxWaitForFramesMs);
1593 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1594 receiving_client()->ice_connection_state(),
1595 kMaxWaitForFramesMs);
1596 // Now set the tracks, and expect frames to immediately start flowing.
1597 EXPECT_TRUE(
1598 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
1599 EXPECT_TRUE(
1600 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
1601 EXPECT_TRUE_WAIT(FramesNotPending(kEndAudioFrameCount, kEndVideoFrameCount),
1602 kMaxWaitForFramesMs);
1603}
1604
deadbeef0a6c4ca2015-10-06 11:38:28 -07001605class IceServerParsingTest : public testing::Test {
1606 public:
1607 // Convenience for parsing a single URL.
1608 bool ParseUrl(const std::string& url) {
1609 return ParseUrl(url, std::string(), std::string());
1610 }
1611
1612 bool ParseUrl(const std::string& url,
1613 const std::string& username,
1614 const std::string& password) {
1615 PeerConnectionInterface::IceServers servers;
1616 PeerConnectionInterface::IceServer server;
1617 server.urls.push_back(url);
1618 server.username = username;
1619 server.password = password;
1620 servers.push_back(server);
1621 return webrtc::ParseIceServers(servers, &stun_configurations_,
1622 &turn_configurations_);
1623 }
1624
1625 protected:
1626 webrtc::StunConfigurations stun_configurations_;
1627 webrtc::TurnConfigurations turn_configurations_;
1628};
1629
1630// Make sure all STUN/TURN prefixes are parsed correctly.
1631TEST_F(IceServerParsingTest, ParseStunPrefixes) {
1632 EXPECT_TRUE(ParseUrl("stun:hostname"));
1633 EXPECT_EQ(1U, stun_configurations_.size());
1634 EXPECT_EQ(0U, turn_configurations_.size());
1635 stun_configurations_.clear();
1636
1637 EXPECT_TRUE(ParseUrl("stuns:hostname"));
1638 EXPECT_EQ(1U, stun_configurations_.size());
1639 EXPECT_EQ(0U, turn_configurations_.size());
1640 stun_configurations_.clear();
1641
1642 EXPECT_TRUE(ParseUrl("turn:hostname"));
1643 EXPECT_EQ(0U, stun_configurations_.size());
1644 EXPECT_EQ(1U, turn_configurations_.size());
1645 EXPECT_FALSE(turn_configurations_[0].secure);
1646 turn_configurations_.clear();
1647
1648 EXPECT_TRUE(ParseUrl("turns:hostname"));
1649 EXPECT_EQ(0U, stun_configurations_.size());
1650 EXPECT_EQ(1U, turn_configurations_.size());
1651 EXPECT_TRUE(turn_configurations_[0].secure);
1652 turn_configurations_.clear();
1653
1654 // invalid prefixes
1655 EXPECT_FALSE(ParseUrl("stunn:hostname"));
1656 EXPECT_FALSE(ParseUrl(":hostname"));
1657 EXPECT_FALSE(ParseUrl(":"));
1658 EXPECT_FALSE(ParseUrl(""));
1659}
1660
1661TEST_F(IceServerParsingTest, VerifyDefaults) {
1662 // TURNS defaults
1663 EXPECT_TRUE(ParseUrl("turns:hostname"));
1664 EXPECT_EQ(1U, turn_configurations_.size());
1665 EXPECT_EQ(5349, turn_configurations_[0].server.port());
1666 EXPECT_EQ("tcp", turn_configurations_[0].transport_type);
1667 turn_configurations_.clear();
1668
1669 // TURN defaults
1670 EXPECT_TRUE(ParseUrl("turn:hostname"));
1671 EXPECT_EQ(1U, turn_configurations_.size());
1672 EXPECT_EQ(3478, turn_configurations_[0].server.port());
1673 EXPECT_EQ("udp", turn_configurations_[0].transport_type);
1674 turn_configurations_.clear();
1675
1676 // STUN defaults
1677 EXPECT_TRUE(ParseUrl("stun:hostname"));
1678 EXPECT_EQ(1U, stun_configurations_.size());
1679 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1680 stun_configurations_.clear();
1681}
1682
1683// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
1684// can be parsed correctly.
1685TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
1686 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
1687 EXPECT_EQ(1U, stun_configurations_.size());
1688 EXPECT_EQ("1.2.3.4", stun_configurations_[0].server.hostname());
1689 EXPECT_EQ(1234, stun_configurations_[0].server.port());
1690 stun_configurations_.clear();
1691
1692 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
1693 EXPECT_EQ(1U, stun_configurations_.size());
1694 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_configurations_[0].server.hostname());
1695 EXPECT_EQ(4321, stun_configurations_[0].server.port());
1696 stun_configurations_.clear();
1697
1698 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
1699 EXPECT_EQ(1U, stun_configurations_.size());
1700 EXPECT_EQ("hostname", stun_configurations_[0].server.hostname());
1701 EXPECT_EQ(9999, stun_configurations_[0].server.port());
1702 stun_configurations_.clear();
1703
1704 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
1705 EXPECT_EQ(1U, stun_configurations_.size());
1706 EXPECT_EQ("1.2.3.4", stun_configurations_[0].server.hostname());
1707 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1708 stun_configurations_.clear();
1709
1710 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
1711 EXPECT_EQ(1U, stun_configurations_.size());
1712 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_configurations_[0].server.hostname());
1713 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1714 stun_configurations_.clear();
1715
1716 EXPECT_TRUE(ParseUrl("stun:hostname"));
1717 EXPECT_EQ(1U, stun_configurations_.size());
1718 EXPECT_EQ("hostname", stun_configurations_[0].server.hostname());
1719 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1720 stun_configurations_.clear();
1721
1722 // Try some invalid hostname:port strings.
1723 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
1724 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
1725 EXPECT_FALSE(ParseUrl("stun:hostname:"));
1726 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
1727 EXPECT_FALSE(ParseUrl("stun::5555"));
1728 EXPECT_FALSE(ParseUrl("stun:"));
1729}
1730
1731// Test parsing the "?transport=xxx" part of the URL.
1732TEST_F(IceServerParsingTest, ParseTransport) {
1733 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
1734 EXPECT_EQ(1U, turn_configurations_.size());
1735 EXPECT_EQ("tcp", turn_configurations_[0].transport_type);
1736 turn_configurations_.clear();
1737
1738 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
1739 EXPECT_EQ(1U, turn_configurations_.size());
1740 EXPECT_EQ("udp", turn_configurations_[0].transport_type);
1741 turn_configurations_.clear();
1742
1743 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
1744}
1745
1746// Test parsing ICE username contained in URL.
1747TEST_F(IceServerParsingTest, ParseUsername) {
1748 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
1749 EXPECT_EQ(1U, turn_configurations_.size());
1750 EXPECT_EQ("user", turn_configurations_[0].username);
1751 turn_configurations_.clear();
1752
1753 EXPECT_FALSE(ParseUrl("turn:@hostname"));
1754 EXPECT_FALSE(ParseUrl("turn:username@"));
1755 EXPECT_FALSE(ParseUrl("turn:@"));
1756 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
1757}
1758
1759// Test that username and password from IceServer is copied into the resulting
1760// TurnConfiguration.
1761TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
1762 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
1763 EXPECT_EQ(1U, turn_configurations_.size());
1764 EXPECT_EQ("username", turn_configurations_[0].username);
1765 EXPECT_EQ("password", turn_configurations_[0].password);
1766}
1767
1768// Ensure that if a server has multiple URLs, each one is parsed.
1769TEST_F(IceServerParsingTest, ParseMultipleUrls) {
1770 PeerConnectionInterface::IceServers servers;
1771 PeerConnectionInterface::IceServer server;
1772 server.urls.push_back("stun:hostname");
1773 server.urls.push_back("turn:hostname");
1774 servers.push_back(server);
1775 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_configurations_,
1776 &turn_configurations_));
1777 EXPECT_EQ(1U, stun_configurations_.size());
1778 EXPECT_EQ(1U, turn_configurations_.size());
1779}
1780
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001781#endif // if !defined(THREAD_SANITIZER)