blob: 9ce6c5e7585645f56e6d2a2cef52988370ce39d9 [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;
deadbeeffaac4972015-11-12 15:33:07 -080081using webrtc::MediaStreamInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082using webrtc::MediaStreamTrackInterface;
83using webrtc::MockCreateSessionDescriptionObserver;
84using webrtc::MockDataChannelObserver;
85using webrtc::MockSetSessionDescriptionObserver;
86using webrtc::MockStatsObserver;
deadbeeffaac4972015-11-12 15:33:07 -080087using webrtc::ObserverInterface;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000088using webrtc::PeerConnectionInterface;
Joachim Bauch04e5b492015-05-29 09:40:39 +020089using webrtc::PeerConnectionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090using webrtc::SessionDescriptionInterface;
91using webrtc::StreamCollectionInterface;
92
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000093static const int kMaxWaitMs = 10000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000094// Disable for TSan v2, see
95// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
96// This declaration is also #ifdef'd as it causes uninitialized-variable
97// warnings.
98#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099static const int kMaxWaitForStatsMs = 3000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +0000100#endif
deadbeeffac06552015-11-25 11:26:01 -0800101static const int kMaxWaitForActivationMs = 5000;
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +0000102static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103static const int kEndAudioFrameCount = 3;
104static const int kEndVideoFrameCount = 3;
105
106static const char kStreamLabelBase[] = "stream_label";
107static const char kVideoTrackLabelBase[] = "video_track";
108static const char kAudioTrackLabelBase[] = "audio_track";
109static const char kDataChannelLabel[] = "data_channel";
110
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000111// Disable for TSan v2, see
112// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
113// This declaration is also #ifdef'd as it causes unused-variable errors.
114#if !defined(THREAD_SANITIZER)
115// SRTP cipher name negotiated by the tests. This must be updated if the
116// default changes.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800117static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000118#endif
119
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120static void RemoveLinesFromSdp(const std::string& line_start,
121 std::string* sdp) {
122 const char kSdpLineEnd[] = "\r\n";
123 size_t ssrc_pos = 0;
124 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
125 std::string::npos) {
126 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
127 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
128 }
129}
130
131class SignalingMessageReceiver {
132 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 virtual void ReceiveSdpMessage(const std::string& type,
134 std::string& msg) = 0;
135 virtual void ReceiveIceMessage(const std::string& sdp_mid,
136 int sdp_mline_index,
137 const std::string& msg) = 0;
138
139 protected:
deadbeefaf1b59c2015-10-15 12:08:41 -0700140 SignalingMessageReceiver() {}
141 virtual ~SignalingMessageReceiver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142};
143
deadbeefaf1b59c2015-10-15 12:08:41 -0700144class PeerConnectionTestClient : public webrtc::PeerConnectionObserver,
deadbeeffaac4972015-11-12 15:33:07 -0800145 public SignalingMessageReceiver,
146 public ObserverInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147 public:
deadbeefaf1b59c2015-10-15 12:08:41 -0700148 static PeerConnectionTestClient* CreateClient(
149 const std::string& id,
150 const MediaConstraintsInterface* constraints,
151 const PeerConnectionFactory::Options* options) {
152 PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
153 if (!client->Init(constraints, options)) {
154 delete client;
155 return nullptr;
156 }
157 return client;
158 }
159
160 ~PeerConnectionTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 while (!fake_video_renderers_.empty()) {
162 RenderMap::iterator it = fake_video_renderers_.begin();
163 delete it->second;
164 fake_video_renderers_.erase(it);
165 }
166 }
167
deadbeefaf1b59c2015-10-15 12:08:41 -0700168 void Negotiate() { Negotiate(true, true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169
deadbeefaf1b59c2015-10-15 12:08:41 -0700170 void Negotiate(bool audio, bool video) {
171 rtc::scoped_ptr<SessionDescriptionInterface> offer;
172 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173
deadbeefaf1b59c2015-10-15 12:08:41 -0700174 if (offer->description()->GetContentByName("audio")) {
175 offer->description()->GetContentByName("audio")->rejected = !audio;
176 }
177 if (offer->description()->GetContentByName("video")) {
178 offer->description()->GetContentByName("video")->rejected = !video;
179 }
180
181 std::string sdp;
182 EXPECT_TRUE(offer->ToString(&sdp));
183 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
184 signaling_message_receiver_->ReceiveSdpMessage(
185 webrtc::SessionDescriptionInterface::kOffer, sdp);
186 }
187
188 // SignalingMessageReceiver callback.
189 void ReceiveSdpMessage(const std::string& type, std::string& msg) override {
190 FilterIncomingSdpMessage(&msg);
191 if (type == webrtc::SessionDescriptionInterface::kOffer) {
192 HandleIncomingOffer(msg);
193 } else {
194 HandleIncomingAnswer(msg);
195 }
196 }
197
198 // SignalingMessageReceiver callback.
199 void ReceiveIceMessage(const std::string& sdp_mid,
200 int sdp_mline_index,
201 const std::string& msg) override {
202 LOG(INFO) << id_ << "ReceiveIceMessage";
203 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
204 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
205 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
206 }
207
208 // PeerConnectionObserver callbacks.
209 void OnSignalingChange(
210 webrtc::PeerConnectionInterface::SignalingState new_state) override {
211 EXPECT_EQ(pc()->signaling_state(), new_state);
212 }
deadbeeffaac4972015-11-12 15:33:07 -0800213 void OnAddStream(MediaStreamInterface* media_stream) override {
214 media_stream->RegisterObserver(this);
deadbeefaf1b59c2015-10-15 12:08:41 -0700215 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
216 const std::string id = media_stream->GetVideoTracks()[i]->id();
217 ASSERT_TRUE(fake_video_renderers_.find(id) ==
218 fake_video_renderers_.end());
219 fake_video_renderers_[id] =
220 new webrtc::FakeVideoTrackRenderer(media_stream->GetVideoTracks()[i]);
221 }
222 }
deadbeeffaac4972015-11-12 15:33:07 -0800223 void OnRemoveStream(MediaStreamInterface* media_stream) override {}
deadbeefaf1b59c2015-10-15 12:08:41 -0700224 void OnRenegotiationNeeded() override {}
225 void OnIceConnectionChange(
226 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
227 EXPECT_EQ(pc()->ice_connection_state(), new_state);
228 }
229 void OnIceGatheringChange(
230 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
231 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
232 }
233 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
234 LOG(INFO) << id_ << "OnIceCandidate";
235
236 std::string ice_sdp;
237 EXPECT_TRUE(candidate->ToString(&ice_sdp));
238 if (signaling_message_receiver_ == nullptr) {
239 // Remote party may be deleted.
240 return;
241 }
242 signaling_message_receiver_->ReceiveIceMessage(
243 candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
244 }
245
deadbeeffaac4972015-11-12 15:33:07 -0800246 // MediaStreamInterface callback
247 void OnChanged() override {
248 // Track added or removed from MediaStream, so update our renderers.
249 rtc::scoped_refptr<StreamCollectionInterface> remote_streams =
250 pc()->remote_streams();
251 // Remove renderers for tracks that were removed.
252 for (auto it = fake_video_renderers_.begin();
253 it != fake_video_renderers_.end();) {
254 if (remote_streams->FindVideoTrack(it->first) == nullptr) {
255 auto to_delete = it++;
256 delete to_delete->second;
257 fake_video_renderers_.erase(to_delete);
258 } else {
259 ++it;
260 }
261 }
262 // Create renderers for new video tracks.
263 for (size_t stream_index = 0; stream_index < remote_streams->count();
264 ++stream_index) {
265 MediaStreamInterface* remote_stream = remote_streams->at(stream_index);
266 for (size_t track_index = 0;
267 track_index < remote_stream->GetVideoTracks().size();
268 ++track_index) {
269 const std::string id =
270 remote_stream->GetVideoTracks()[track_index]->id();
271 if (fake_video_renderers_.find(id) != fake_video_renderers_.end()) {
272 continue;
273 }
274 fake_video_renderers_[id] = new webrtc::FakeVideoTrackRenderer(
275 remote_stream->GetVideoTracks()[track_index]);
276 }
277 }
278 }
279
deadbeefaf1b59c2015-10-15 12:08:41 -0700280 void SetVideoConstraints(const webrtc::FakeConstraints& video_constraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 video_constraints_ = video_constraint;
282 }
283
284 void AddMediaStream(bool audio, bool video) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700285 std::string stream_label =
286 kStreamLabelBase +
287 rtc::ToString<int>(static_cast<int>(pc()->local_streams()->count()));
deadbeeffaac4972015-11-12 15:33:07 -0800288 rtc::scoped_refptr<MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000289 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290
291 if (audio && can_receive_audio()) {
deadbeeffac06552015-11-25 11:26:01 -0800292 stream->AddTrack(CreateLocalAudioTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 }
294 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000295 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296 }
297
deadbeefaf1b59c2015-10-15 12:08:41 -0700298 EXPECT_TRUE(pc()->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 }
300
deadbeefaf1b59c2015-10-15 12:08:41 -0700301 size_t NumberOfLocalMediaStreams() { return pc()->local_streams()->count(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302
303 bool SessionActive() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700304 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000305 }
306
deadbeeffaac4972015-11-12 15:33:07 -0800307 // Automatically add a stream when receiving an offer, if we don't have one.
308 // Defaults to true.
309 void set_auto_add_stream(bool auto_add_stream) {
310 auto_add_stream_ = auto_add_stream;
311 }
312
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313 void set_signaling_message_receiver(
deadbeefaf1b59c2015-10-15 12:08:41 -0700314 SignalingMessageReceiver* signaling_message_receiver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315 signaling_message_receiver_ = signaling_message_receiver;
316 }
317
318 void EnableVideoDecoderFactory() {
319 video_decoder_factory_enabled_ = true;
320 fake_video_decoder_factory_->AddSupportedVideoCodecType(
321 webrtc::kVideoCodecVP8);
322 }
323
deadbeefaf1b59c2015-10-15 12:08:41 -0700324 void IceRestart() {
325 session_description_constraints_.SetMandatoryIceRestart(true);
326 SetExpectIceRestart(true);
327 }
328
329 void SetExpectIceRestart(bool expect_restart) {
330 expect_ice_restart_ = expect_restart;
331 }
332
333 bool ExpectIceRestart() const { return expect_ice_restart_; }
334
335 void SetReceiveAudioVideo(bool audio, bool video) {
336 SetReceiveAudio(audio);
337 SetReceiveVideo(video);
338 ASSERT_EQ(audio, can_receive_audio());
339 ASSERT_EQ(video, can_receive_video());
340 }
341
342 void SetReceiveAudio(bool audio) {
343 if (audio && can_receive_audio())
344 return;
345 session_description_constraints_.SetMandatoryReceiveAudio(audio);
346 }
347
348 void SetReceiveVideo(bool video) {
349 if (video && can_receive_video())
350 return;
351 session_description_constraints_.SetMandatoryReceiveVideo(video);
352 }
353
354 void RemoveMsidFromReceivedSdp(bool remove) { remove_msid_ = remove; }
355
356 void RemoveSdesCryptoFromReceivedSdp(bool remove) { remove_sdes_ = remove; }
357
358 void RemoveBundleFromReceivedSdp(bool remove) { remove_bundle_ = remove; }
359
360 bool can_receive_audio() {
361 bool value;
362 if (webrtc::FindConstraint(&session_description_constraints_,
363 MediaConstraintsInterface::kOfferToReceiveAudio,
364 &value, nullptr)) {
365 return value;
366 }
367 return true;
368 }
369
370 bool can_receive_video() {
371 bool value;
372 if (webrtc::FindConstraint(&session_description_constraints_,
373 MediaConstraintsInterface::kOfferToReceiveVideo,
374 &value, nullptr)) {
375 return value;
376 }
377 return true;
378 }
379
380 void OnIceComplete() override { LOG(INFO) << id_ << "OnIceComplete"; }
381
382 void OnDataChannel(DataChannelInterface* data_channel) override {
383 LOG(INFO) << id_ << "OnDataChannel";
384 data_channel_ = data_channel;
385 data_observer_.reset(new MockDataChannelObserver(data_channel));
386 }
387
388 void CreateDataChannel() {
389 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, nullptr);
390 ASSERT_TRUE(data_channel_.get() != nullptr);
391 data_observer_.reset(new MockDataChannelObserver(data_channel_));
392 }
393
deadbeeffac06552015-11-25 11:26:01 -0800394 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack(
395 const std::string& stream_label) {
396 FakeConstraints constraints;
397 // Disable highpass filter so that we can get all the test audio frames.
398 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
399 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
400 peer_connection_factory_->CreateAudioSource(&constraints);
401 // TODO(perkj): Test audio source when it is implemented. Currently audio
402 // always use the default input.
403 std::string label = stream_label + kAudioTrackLabelBase;
404 return peer_connection_factory_->CreateAudioTrack(label, source);
405 }
406
407 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack(
408 const std::string& stream_label) {
409 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
410 FakeConstraints source_constraints = video_constraints_;
411 source_constraints.SetMandatoryMaxFrameRate(10);
412
413 cricket::FakeVideoCapturer* fake_capturer =
414 new webrtc::FakePeriodicVideoCapturer();
415 video_capturers_.push_back(fake_capturer);
416 rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
417 peer_connection_factory_->CreateVideoSource(fake_capturer,
418 &source_constraints);
419 std::string label = stream_label + kVideoTrackLabelBase;
420 return peer_connection_factory_->CreateVideoTrack(label, source);
421 }
422
deadbeefaf1b59c2015-10-15 12:08:41 -0700423 DataChannelInterface* data_channel() { return data_channel_; }
424 const MockDataChannelObserver* data_observer() const {
425 return data_observer_.get();
426 }
427
428 webrtc::PeerConnectionInterface* pc() { return peer_connection_.get(); }
429
430 void StopVideoCapturers() {
431 for (std::vector<cricket::VideoCapturer*>::iterator it =
432 video_capturers_.begin();
433 it != video_capturers_.end(); ++it) {
434 (*it)->Stop();
435 }
436 }
437
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000438 bool AudioFramesReceivedCheck(int number_of_frames) const {
439 return number_of_frames <= fake_audio_capture_module_->frames_received();
440 }
441
442 bool VideoFramesReceivedCheck(int number_of_frames) {
443 if (video_decoder_factory_enabled_) {
444 const std::vector<FakeWebRtcVideoDecoder*>& decoders
445 = fake_video_decoder_factory_->decoders();
446 if (decoders.empty()) {
447 return number_of_frames <= 0;
448 }
449
450 for (std::vector<FakeWebRtcVideoDecoder*>::const_iterator
451 it = decoders.begin(); it != decoders.end(); ++it) {
452 if (number_of_frames > (*it)->GetNumFramesReceived()) {
453 return false;
454 }
455 }
456 return true;
457 } else {
458 if (fake_video_renderers_.empty()) {
459 return number_of_frames <= 0;
460 }
461
462 for (RenderMap::const_iterator it = fake_video_renderers_.begin();
463 it != fake_video_renderers_.end(); ++it) {
464 if (number_of_frames > it->second->num_rendered_frames()) {
465 return false;
466 }
467 }
468 return true;
469 }
470 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700471
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472 // Verify the CreateDtmfSender interface
473 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000474 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
475 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476
477 // We can't create a DTMF sender with an invalid audio track or a non local
478 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700479 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000480 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700481 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
482 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483
484 // We should be able to create a DTMF sender from a local track.
485 webrtc::AudioTrackInterface* localtrack =
486 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
487 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700488 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 dtmf_sender->RegisterObserver(observer.get());
490
491 // Test the DtmfSender object just created.
492 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
493 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
494
495 // We don't need to verify that the DTMF tones are actually sent out because
496 // that is already covered by the tests of the lower level components.
497
498 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
499 std::vector<std::string> tones;
500 tones.push_back("1");
501 tones.push_back("a");
502 tones.push_back("");
503 observer->Verify(tones);
504
505 dtmf_sender->UnregisterObserver();
506 }
507
508 // Verifies that the SessionDescription have rejected the appropriate media
509 // content.
510 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700511 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
512 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 const cricket::SessionDescription* remote_desc =
514 peer_connection_->remote_description()->description();
515 const cricket::SessionDescription* local_desc =
516 peer_connection_->local_description()->description();
517
518 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
519 if (remote_audio_content) {
520 const ContentInfo* audio_content =
521 GetFirstAudioContent(local_desc);
522 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
523 }
524
525 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
526 if (remote_video_content) {
527 const ContentInfo* video_content =
528 GetFirstVideoContent(local_desc);
529 EXPECT_EQ(can_receive_video(), !video_content->rejected);
530 }
531 }
532
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700534 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 const cricket::SessionDescription* desc =
536 peer_connection_->local_description()->description();
537 const cricket::ContentInfos& contents = desc->contents();
538
539 for (size_t index = 0; index < contents.size(); ++index) {
540 if (contents[index].rejected)
541 continue;
542 const cricket::TransportDescription* transport_desc =
543 desc->GetTransportDescriptionByName(contents[index].name);
544
545 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000546 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 if (ufragpair_it == ice_ufrag_pwd_.end()) {
548 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000549 ice_ufrag_pwd_[static_cast<int>(index)] =
550 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551 } else if (ExpectIceRestart()) {
552 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
553 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
554 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
555 } else {
556 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
557 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
558 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
559 }
560 }
561 }
562
563 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000564 rtc::scoped_refptr<MockStatsObserver>
565 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000566 EXPECT_TRUE(peer_connection_->GetStats(
567 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700569 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 return observer->AudioOutputLevel();
571 }
572
573 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000574 rtc::scoped_refptr<MockStatsObserver>
575 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000576 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700577 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700579 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580 return observer->AudioInputLevel();
581 }
582
583 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000584 rtc::scoped_refptr<MockStatsObserver>
585 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000586 EXPECT_TRUE(peer_connection_->GetStats(
587 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000588 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700589 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590 return observer->BytesReceived();
591 }
592
593 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000594 rtc::scoped_refptr<MockStatsObserver>
595 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000596 EXPECT_TRUE(peer_connection_->GetStats(
597 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700599 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600 return observer->BytesSent();
601 }
602
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000603 int GetAvailableReceivedBandwidthStats() {
604 rtc::scoped_refptr<MockStatsObserver>
605 observer(new rtc::RefCountedObject<MockStatsObserver>());
606 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700607 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000608 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700609 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000610 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000611 return bw;
612 }
613
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000614 std::string GetDtlsCipherStats() {
615 rtc::scoped_refptr<MockStatsObserver>
616 observer(new rtc::RefCountedObject<MockStatsObserver>());
617 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700618 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000619 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700620 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000621 return observer->DtlsCipher();
622 }
623
624 std::string GetSrtpCipherStats() {
625 rtc::scoped_refptr<MockStatsObserver>
626 observer(new rtc::RefCountedObject<MockStatsObserver>());
627 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700628 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000629 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700630 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000631 return observer->SrtpCipher();
632 }
633
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634 int rendered_width() {
635 EXPECT_FALSE(fake_video_renderers_.empty());
636 return fake_video_renderers_.empty() ? 1 :
637 fake_video_renderers_.begin()->second->width();
638 }
639
640 int rendered_height() {
641 EXPECT_FALSE(fake_video_renderers_.empty());
642 return fake_video_renderers_.empty() ? 1 :
643 fake_video_renderers_.begin()->second->height();
644 }
645
646 size_t number_of_remote_streams() {
647 if (!pc())
648 return 0;
649 return pc()->remote_streams()->count();
650 }
651
652 StreamCollectionInterface* remote_streams() {
653 if (!pc()) {
654 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700655 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000656 }
657 return pc()->remote_streams();
658 }
659
660 StreamCollectionInterface* local_streams() {
661 if (!pc()) {
662 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700663 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 }
665 return pc()->local_streams();
666 }
667
668 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
669 return pc()->signaling_state();
670 }
671
672 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
673 return pc()->ice_connection_state();
674 }
675
676 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
677 return pc()->ice_gathering_state();
678 }
679
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680 private:
681 class DummyDtmfObserver : public DtmfSenderObserverInterface {
682 public:
683 DummyDtmfObserver() : completed_(false) {}
684
685 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700686 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 tones_.push_back(tone);
688 if (tone.empty()) {
689 completed_ = true;
690 }
691 }
692
693 void Verify(const std::vector<std::string>& tones) const {
694 ASSERT_TRUE(tones_.size() == tones.size());
695 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
696 }
697
698 bool completed() const { return completed_; }
699
700 private:
701 bool completed_;
702 std::vector<std::string> tones_;
703 };
704
deadbeefaf1b59c2015-10-15 12:08:41 -0700705 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
706
707 bool Init(const MediaConstraintsInterface* constraints,
708 const PeerConnectionFactory::Options* options) {
709 EXPECT_TRUE(!peer_connection_);
710 EXPECT_TRUE(!peer_connection_factory_);
711 allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
712 if (!allocator_factory_) {
713 return false;
714 }
715 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
716
717 if (fake_audio_capture_module_ == nullptr) {
718 return false;
719 }
720 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
721 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
722 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
723 rtc::Thread::Current(), rtc::Thread::Current(),
724 fake_audio_capture_module_, fake_video_encoder_factory_,
725 fake_video_decoder_factory_);
726 if (!peer_connection_factory_) {
727 return false;
728 }
729 if (options) {
730 peer_connection_factory_->SetOptions(*options);
731 }
732 peer_connection_ = CreatePeerConnection(allocator_factory_.get(),
733 constraints);
734 return peer_connection_.get() != nullptr;
735 }
736
deadbeefaf1b59c2015-10-15 12:08:41 -0700737 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
738 webrtc::PortAllocatorFactoryInterface* factory,
739 const MediaConstraintsInterface* constraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740 // CreatePeerConnection with IceServers.
741 webrtc::PeerConnectionInterface::IceServers ice_servers;
742 webrtc::PeerConnectionInterface::IceServer ice_server;
743 ice_server.uri = "stun:stun.l.google.com:19302";
744 ice_servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000745
Henrik Boström5e56c592015-08-11 10:33:13 +0200746 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store(
747 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
748 : nullptr);
deadbeefaf1b59c2015-10-15 12:08:41 -0700749 return peer_connection_factory_->CreatePeerConnection(
Henrik Boström5e56c592015-08-11 10:33:13 +0200750 ice_servers, constraints, factory, dtls_identity_store.Pass(), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751 }
752
753 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700754 LOG(INFO) << id_ << "HandleIncomingOffer ";
deadbeeffaac4972015-11-12 15:33:07 -0800755 if (NumberOfLocalMediaStreams() == 0 && auto_add_stream_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 // If we are not sending any streams ourselves it is time to add some.
757 AddMediaStream(true, true);
758 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000759 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700760 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000762 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 EXPECT_TRUE(DoCreateAnswer(answer.use()));
764 std::string sdp;
765 EXPECT_TRUE(answer->ToString(&sdp));
766 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
deadbeefaf1b59c2015-10-15 12:08:41 -0700767 if (signaling_message_receiver_) {
768 signaling_message_receiver_->ReceiveSdpMessage(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 webrtc::SessionDescriptionInterface::kAnswer, sdp);
770 }
771 }
772
773 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700774 LOG(INFO) << id_ << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000775 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700776 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
778 }
779
780 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
781 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000782 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
783 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 MockCreateSessionDescriptionObserver>());
785 if (offer) {
786 pc()->CreateOffer(observer, &session_description_constraints_);
787 } else {
788 pc()->CreateAnswer(observer, &session_description_constraints_);
789 }
790 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
791 *desc = observer->release_desc();
792 if (observer->result() && ExpectIceRestart()) {
793 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
794 }
795 return observer->result();
796 }
797
798 bool DoCreateOffer(SessionDescriptionInterface** desc) {
799 return DoCreateOfferAnswer(desc, true);
800 }
801
802 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
803 return DoCreateOfferAnswer(desc, false);
804 }
805
806 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000807 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
808 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700810 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 pc()->SetLocalDescription(observer, desc);
812 // Ignore the observer result. If we wait for the result with
813 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
814 // before the offer which is an error.
815 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000816 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 // ProcessMessages waits at least 1ms but processes all messages before
818 // returning. Since this test is synchronous and send messages to the remote
819 // peer whenever a callback is invoked, this can lead to messages being
820 // sent to the remote peer in the wrong order.
821 // TODO(perkj): Find a way to check the result without risking that the
822 // order of sent messages are changed. Ex- by posting all messages that are
823 // sent to the remote peer.
824 return true;
825 }
826
827 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000828 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
829 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700831 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832 pc()->SetRemoteDescription(observer, desc);
833 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
834 return observer->result();
835 }
836
837 // This modifies all received SDP messages before they are processed.
838 void FilterIncomingSdpMessage(std::string* sdp) {
839 if (remove_msid_) {
840 const char kSdpSsrcAttribute[] = "a=ssrc:";
841 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
842 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
843 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
844 }
845 if (remove_bundle_) {
846 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
847 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
848 }
849 if (remove_sdes_) {
850 const char kSdpSdesCryptoAttribute[] = "a=crypto";
851 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
852 }
853 }
854
deadbeefaf1b59c2015-10-15 12:08:41 -0700855 std::string id_;
856
857 rtc::scoped_refptr<webrtc::PortAllocatorFactoryInterface> allocator_factory_;
858 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
859 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
860 peer_connection_factory_;
861
deadbeeffaac4972015-11-12 15:33:07 -0800862 bool auto_add_stream_ = true;
863
deadbeefaf1b59c2015-10-15 12:08:41 -0700864 typedef std::pair<std::string, std::string> IceUfragPwdPair;
865 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
866 bool expect_ice_restart_ = false;
867
868 // Needed to keep track of number of frames send.
869 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
870 // Needed to keep track of number of frames received.
871 typedef std::map<std::string, webrtc::FakeVideoTrackRenderer*> RenderMap;
872 RenderMap fake_video_renderers_;
873 // Needed to keep track of number of frames received when external decoder
874 // used.
875 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
876 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
877 bool video_decoder_factory_enabled_ = false;
878 webrtc::FakeConstraints video_constraints_;
879
880 // For remote peer communication.
881 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
882
883 // Store references to the video capturers we've created, so that we can stop
884 // them, if required.
885 std::vector<cricket::VideoCapturer*> video_capturers_;
886
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 webrtc::FakeConstraints session_description_constraints_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700888 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
889 bool remove_bundle_ =
890 false; // True if bundle should be removed in received SDP.
891 bool remove_sdes_ =
892 false; // True if a=crypto should be removed in received SDP.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000894 rtc::scoped_refptr<DataChannelInterface> data_channel_;
895 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000896};
897
ivoc191c1f92015-11-19 11:12:06 -0800898// Flaky on Mac Debug bots. See webrtc:5231
899#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
900#define MAYBE_JsepPeerConnectionP2PTestClient \
901 DISABLED_JsepPeerConnectionP2PTestClient
902#else
903#define MAYBE_JsepPeerConnectionP2PTestClient JsepPeerConnectionP2PTestClient
904#endif
905
deadbeefcbc95072015-10-15 19:31:56 -0700906// TODO(deadbeef): Rename this to P2PTestConductor once the Linux memcheck and
907// Windows DrMemory Full bots' blacklists are updated.
ivoc191c1f92015-11-19 11:12:06 -0800908class MAYBE_JsepPeerConnectionP2PTestClient : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 public:
ivoc191c1f92015-11-19 11:12:06 -0800910 MAYBE_JsepPeerConnectionP2PTestClient()
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000911 : pss_(new rtc::PhysicalSocketServer),
912 ss_(new rtc::VirtualSocketServer(pss_.get())),
913 ss_scope_(ss_.get()) {}
914
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 bool SessionActive() {
916 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000917 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000919
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 // Return true if the number of frames provided have been received or it is
921 // known that that will never occur (e.g. no frames will be sent or
922 // captured).
923 bool FramesNotPending(int audio_frames_to_receive,
924 int video_frames_to_receive) {
925 return VideoFramesReceivedCheck(video_frames_to_receive) &&
926 AudioFramesReceivedCheck(audio_frames_to_receive);
927 }
928 bool AudioFramesReceivedCheck(int frames_received) {
929 return initiating_client_->AudioFramesReceivedCheck(frames_received) &&
930 receiving_client_->AudioFramesReceivedCheck(frames_received);
931 }
932 bool VideoFramesReceivedCheck(int frames_received) {
933 return initiating_client_->VideoFramesReceivedCheck(frames_received) &&
934 receiving_client_->VideoFramesReceivedCheck(frames_received);
935 }
936 void VerifyDtmf() {
937 initiating_client_->VerifyDtmf();
938 receiving_client_->VerifyDtmf();
939 }
940
941 void TestUpdateOfferWithRejectedContent() {
942 initiating_client_->Negotiate(true, false);
943 EXPECT_TRUE_WAIT(
944 FramesNotPending(kEndAudioFrameCount * 2, kEndVideoFrameCount),
945 kMaxWaitForFramesMs);
946 // There shouldn't be any more video frame after the new offer is
947 // negotiated.
948 EXPECT_FALSE(VideoFramesReceivedCheck(kEndVideoFrameCount + 1));
949 }
950
951 void VerifyRenderedSize(int width, int height) {
952 EXPECT_EQ(width, receiving_client()->rendered_width());
953 EXPECT_EQ(height, receiving_client()->rendered_height());
954 EXPECT_EQ(width, initializing_client()->rendered_width());
955 EXPECT_EQ(height, initializing_client()->rendered_height());
956 }
957
958 void VerifySessionDescriptions() {
959 initiating_client_->VerifyRejectedMediaInSessionDescription();
960 receiving_client_->VerifyRejectedMediaInSessionDescription();
961 initiating_client_->VerifyLocalIceUfragAndPassword();
962 receiving_client_->VerifyLocalIceUfragAndPassword();
963 }
964
ivoc191c1f92015-11-19 11:12:06 -0800965 ~MAYBE_JsepPeerConnectionP2PTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700967 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000968 }
969 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700970 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000972 }
973
deadbeefaf1b59c2015-10-15 12:08:41 -0700974 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975
976 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
977 MediaConstraintsInterface* recv_constraints) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700978 return CreateTestClients(init_constraints, nullptr, recv_constraints,
979 nullptr);
Joachim Bauch04e5b492015-05-29 09:40:39 +0200980 }
981
982 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
983 PeerConnectionFactory::Options* init_options,
984 MediaConstraintsInterface* recv_constraints,
985 PeerConnectionFactory::Options* recv_options) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700986 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
987 "Caller: ", init_constraints, init_options));
988 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
989 "Callee: ", recv_constraints, recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000990 if (!initiating_client_ || !receiving_client_) {
991 return false;
992 }
993 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
994 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
995 return true;
996 }
997
998 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
999 const webrtc::FakeConstraints& recv_constraints) {
1000 initiating_client_->SetVideoConstraints(init_constraints);
1001 receiving_client_->SetVideoConstraints(recv_constraints);
1002 }
1003
1004 void EnableVideoDecoderFactory() {
1005 initiating_client_->EnableVideoDecoderFactory();
1006 receiving_client_->EnableVideoDecoderFactory();
1007 }
1008
1009 // This test sets up a call between two parties. Both parties send static
1010 // frames to each other. Once the test is finished the number of sent frames
1011 // is compared to the number of received frames.
1012 void LocalP2PTest() {
1013 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1014 initiating_client_->AddMediaStream(true, true);
1015 }
1016 initiating_client_->Negotiate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001017 // Assert true is used here since next tests are guaranteed to fail and
1018 // would eat up 5 seconds.
1019 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1020 VerifySessionDescriptions();
1021
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001022 int audio_frame_count = kEndAudioFrameCount;
1023 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1024 if (!initiating_client_->can_receive_audio() ||
1025 !receiving_client_->can_receive_audio()) {
1026 audio_frame_count = -1;
1027 }
1028 int video_frame_count = kEndVideoFrameCount;
1029 if (!initiating_client_->can_receive_video() ||
1030 !receiving_client_->can_receive_video()) {
1031 video_frame_count = -1;
1032 }
1033
1034 if (audio_frame_count != -1 || video_frame_count != -1) {
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001035 // Audio or video is expected to flow, so both clients should reach the
1036 // Connected state, and the offerer (ICE controller) should proceed to
1037 // Completed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001038 // Note: These tests have been observed to fail under heavy load at
1039 // shorter timeouts, so they may be flaky.
1040 EXPECT_EQ_WAIT(
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001041 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001042 initiating_client_->ice_connection_state(),
1043 kMaxWaitForFramesMs);
1044 EXPECT_EQ_WAIT(
1045 webrtc::PeerConnectionInterface::kIceConnectionConnected,
1046 receiving_client_->ice_connection_state(),
1047 kMaxWaitForFramesMs);
1048 }
1049
1050 if (initiating_client_->can_receive_audio() ||
1051 initiating_client_->can_receive_video()) {
1052 // The initiating client can receive media, so it must produce candidates
1053 // that will serve as destinations for that media.
1054 // TODO(bemasc): Understand why the state is not already Complete here, as
1055 // seems to be the case for the receiving client. This may indicate a bug
1056 // in the ICE gathering system.
1057 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1058 initiating_client_->ice_gathering_state());
1059 }
1060 if (receiving_client_->can_receive_audio() ||
1061 receiving_client_->can_receive_video()) {
1062 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1063 receiving_client_->ice_gathering_state(),
1064 kMaxWaitForFramesMs);
1065 }
1066
1067 EXPECT_TRUE_WAIT(FramesNotPending(audio_frame_count, video_frame_count),
1068 kMaxWaitForFramesMs);
1069 }
1070
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001071 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1072 // Messages may get lost on the unreliable DataChannel, so we send multiple
1073 // times to avoid test flakiness.
1074 static const size_t kSendAttempts = 5;
1075
1076 for (size_t i = 0; i < kSendAttempts; ++i) {
1077 dc->Send(DataBuffer(data));
1078 }
1079 }
1080
deadbeefaf1b59c2015-10-15 12:08:41 -07001081 PeerConnectionTestClient* initializing_client() {
1082 return initiating_client_.get();
1083 }
1084 PeerConnectionTestClient* receiving_client() {
1085 return receiving_client_.get();
1086 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001087
1088 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001089 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1090 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1091 rtc::SocketServerScope ss_scope_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001092 rtc::scoped_ptr<PeerConnectionTestClient> initiating_client_;
1093 rtc::scoped_ptr<PeerConnectionTestClient> receiving_client_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001094};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001096// Disable for TSan v2, see
1097// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1098#if !defined(THREAD_SANITIZER)
1099
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001100// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001101// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1102// See issue webrtc/2378.
ivoc191c1f92015-11-19 11:12:06 -08001103TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 ASSERT_TRUE(CreateTestClients());
1105 LocalP2PTest();
1106 VerifyDtmf();
1107}
1108
1109// This test sets up a Jsep call between two parties and test that we can get a
1110// video aspect ratio of 16:9.
ivoc191c1f92015-11-19 11:12:06 -08001111TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001112 ASSERT_TRUE(CreateTestClients());
1113 FakeConstraints constraint;
1114 double requested_ratio = 640.0/360;
1115 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1116 SetVideoConstraints(constraint, constraint);
1117 LocalP2PTest();
1118
1119 ASSERT_LE(0, initializing_client()->rendered_height());
1120 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001121 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001122 initializing_client()->rendered_height();
1123 EXPECT_LE(requested_ratio, initiating_video_ratio);
1124
1125 ASSERT_LE(0, receiving_client()->rendered_height());
1126 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001127 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128 receiving_client()->rendered_height();
1129 EXPECT_LE(requested_ratio, receiving_video_ratio);
1130}
1131
1132// This test sets up a Jsep call between two parties and test that the
1133// received video has a resolution of 1280*720.
1134// TODO(mallinath): Enable when
1135// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
ivoc191c1f92015-11-19 11:12:06 -08001136TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001137 ASSERT_TRUE(CreateTestClients());
1138 FakeConstraints constraint;
1139 constraint.SetMandatoryMinWidth(1280);
1140 constraint.SetMandatoryMinHeight(720);
1141 SetVideoConstraints(constraint, constraint);
1142 LocalP2PTest();
1143 VerifyRenderedSize(1280, 720);
1144}
1145
1146// This test sets up a call between two endpoints that are configured to use
1147// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
ivoc191c1f92015-11-19 11:12:06 -08001148TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestDtls) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001149 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150 FakeConstraints setup_constraints;
1151 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1152 true);
1153 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1154 LocalP2PTest();
1155 VerifyRenderedSize(640, 480);
1156}
1157
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001158// This test sets up a audio call initially and then upgrades to audio/video,
1159// using DTLS.
ivoc191c1f92015-11-19 11:12:06 -08001160TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001161 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001162 FakeConstraints setup_constraints;
1163 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1164 true);
1165 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1166 receiving_client()->SetReceiveAudioVideo(true, false);
1167 LocalP2PTest();
1168 receiving_client()->SetReceiveAudioVideo(true, true);
1169 receiving_client()->Negotiate();
1170}
1171
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001172// This test sets up a call between two endpoints that are configured to use
1173// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1174// negotiated and used for transport.
ivoc191c1f92015-11-19 11:12:06 -08001175TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
1176 MAYBE_LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001177 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001178 FakeConstraints setup_constraints;
1179 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1180 true);
1181 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1182 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1183 LocalP2PTest();
1184 VerifyRenderedSize(640, 480);
1185}
1186
1187// This test sets up a Jsep call between two parties, and the callee only
1188// accept to receive video.
ivoc191c1f92015-11-19 11:12:06 -08001189TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001190 ASSERT_TRUE(CreateTestClients());
1191 receiving_client()->SetReceiveAudioVideo(false, true);
1192 LocalP2PTest();
1193}
1194
1195// This test sets up a Jsep call between two parties, and the callee only
1196// accept to receive audio.
ivoc191c1f92015-11-19 11:12:06 -08001197TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198 ASSERT_TRUE(CreateTestClients());
1199 receiving_client()->SetReceiveAudioVideo(true, false);
1200 LocalP2PTest();
1201}
1202
1203// This test sets up a Jsep call between two parties, and the callee reject both
1204// audio and video.
ivoc191c1f92015-11-19 11:12:06 -08001205TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001206 ASSERT_TRUE(CreateTestClients());
1207 receiving_client()->SetReceiveAudioVideo(false, false);
1208 LocalP2PTest();
1209}
1210
1211// This test sets up an audio and video call between two parties. After the call
1212// runs for a while (10 frames), the caller sends an update offer with video
1213// being rejected. Once the re-negotiation is done, the video flow should stop
1214// and the audio flow should continue.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001215// Disabled due to b/14955157.
ivoc191c1f92015-11-19 11:12:06 -08001216TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
deadbeefcbc95072015-10-15 19:31:56 -07001217 DISABLED_UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001218 ASSERT_TRUE(CreateTestClients());
1219 LocalP2PTest();
1220 TestUpdateOfferWithRejectedContent();
1221}
1222
1223// This test sets up a Jsep call between two parties. The MSID is removed from
1224// the SDP strings from the caller.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001225// Disabled due to b/14955157.
ivoc191c1f92015-11-19 11:12:06 -08001226TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
1227 DISABLED_LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001228 ASSERT_TRUE(CreateTestClients());
1229 receiving_client()->RemoveMsidFromReceivedSdp(true);
1230 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1231 // audio and video is muxed when MSID is disabled. Remove
1232 // SetRemoveBundleFromSdp once
1233 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1234 receiving_client()->RemoveBundleFromReceivedSdp(true);
1235 LocalP2PTest();
1236}
1237
1238// This test sets up a Jsep call between two parties and the initiating peer
1239// sends two steams.
1240// TODO(perkj): Disabled due to
1241// https://code.google.com/p/webrtc/issues/detail?id=1454
ivoc191c1f92015-11-19 11:12:06 -08001242TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243 ASSERT_TRUE(CreateTestClients());
1244 // Set optional video constraint to max 320pixels to decrease CPU usage.
1245 FakeConstraints constraint;
1246 constraint.SetOptionalMaxWidth(320);
1247 SetVideoConstraints(constraint, constraint);
1248 initializing_client()->AddMediaStream(true, true);
1249 initializing_client()->AddMediaStream(false, true);
1250 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1251 LocalP2PTest();
1252 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1253}
1254
1255// Test that we can receive the audio output level from a remote audio track.
ivoc191c1f92015-11-19 11:12:06 -08001256TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, MAYBE_GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257 ASSERT_TRUE(CreateTestClients());
1258 LocalP2PTest();
1259
1260 StreamCollectionInterface* remote_streams =
1261 initializing_client()->remote_streams();
1262 ASSERT_GT(remote_streams->count(), 0u);
1263 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1264 MediaStreamTrackInterface* remote_audio_track =
1265 remote_streams->at(0)->GetAudioTracks()[0];
1266
1267 // Get the audio output level stats. Note that the level is not available
1268 // until a RTCP packet has been received.
1269 EXPECT_TRUE_WAIT(
1270 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1271 kMaxWaitForStatsMs);
1272}
1273
1274// Test that an audio input level is reported.
ivoc191c1f92015-11-19 11:12:06 -08001275TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, MAYBE_GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001276 ASSERT_TRUE(CreateTestClients());
1277 LocalP2PTest();
1278
1279 // Get the audio input level stats. The level should be available very
1280 // soon after the test starts.
1281 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1282 kMaxWaitForStatsMs);
1283}
1284
1285// Test that we can get incoming byte counts from both audio and video tracks.
ivoc191c1f92015-11-19 11:12:06 -08001286TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, MAYBE_GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287 ASSERT_TRUE(CreateTestClients());
1288 LocalP2PTest();
1289
1290 StreamCollectionInterface* remote_streams =
1291 initializing_client()->remote_streams();
1292 ASSERT_GT(remote_streams->count(), 0u);
1293 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1294 MediaStreamTrackInterface* remote_audio_track =
1295 remote_streams->at(0)->GetAudioTracks()[0];
1296 EXPECT_TRUE_WAIT(
1297 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1298 kMaxWaitForStatsMs);
1299
1300 MediaStreamTrackInterface* remote_video_track =
1301 remote_streams->at(0)->GetVideoTracks()[0];
1302 EXPECT_TRUE_WAIT(
1303 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1304 kMaxWaitForStatsMs);
1305}
1306
1307// Test that we can get outgoing byte counts from both audio and video tracks.
ivoc191c1f92015-11-19 11:12:06 -08001308TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, MAYBE_GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001309 ASSERT_TRUE(CreateTestClients());
1310 LocalP2PTest();
1311
1312 StreamCollectionInterface* local_streams =
1313 initializing_client()->local_streams();
1314 ASSERT_GT(local_streams->count(), 0u);
1315 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1316 MediaStreamTrackInterface* local_audio_track =
1317 local_streams->at(0)->GetAudioTracks()[0];
1318 EXPECT_TRUE_WAIT(
1319 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1320 kMaxWaitForStatsMs);
1321
1322 MediaStreamTrackInterface* local_video_track =
1323 local_streams->at(0)->GetVideoTracks()[0];
1324 EXPECT_TRUE_WAIT(
1325 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1326 kMaxWaitForStatsMs);
1327}
1328
Joachim Bauch04e5b492015-05-29 09:40:39 +02001329// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
ivoc191c1f92015-11-19 11:12:06 -08001330TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001331 PeerConnectionFactory::Options init_options;
1332 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1333 PeerConnectionFactory::Options recv_options;
1334 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001335 ASSERT_TRUE(
1336 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001337 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1338 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1339 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001340 LocalP2PTest();
1341
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001342 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001343 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1344 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1345 initializing_client()->GetDtlsCipherStats(),
1346 kMaxWaitForStatsMs);
1347 EXPECT_EQ(1, init_observer->GetEnumCounter(
1348 webrtc::kEnumCounterAudioSslCipher,
1349 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1350 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001351
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001352 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001353 initializing_client()->GetSrtpCipherStats(),
1354 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001355 EXPECT_EQ(1,
1356 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1357 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001358}
1359
1360// Test that DTLS 1.2 is used if both ends support it.
ivoc191c1f92015-11-19 11:12:06 -08001361TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, MAYBE_GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001362 PeerConnectionFactory::Options init_options;
1363 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1364 PeerConnectionFactory::Options recv_options;
1365 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001366 ASSERT_TRUE(
1367 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001368 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1369 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1370 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001371 LocalP2PTest();
1372
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001373 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001374 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1375 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)),
1376 initializing_client()->GetDtlsCipherStats(),
1377 kMaxWaitForStatsMs);
1378 EXPECT_EQ(1, init_observer->GetEnumCounter(
1379 webrtc::kEnumCounterAudioSslCipher,
1380 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1381 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001382
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001383 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001384 initializing_client()->GetSrtpCipherStats(),
1385 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001386 EXPECT_EQ(1,
1387 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1388 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001389}
1390
1391// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1392// received supports 1.0.
ivoc191c1f92015-11-19 11:12:06 -08001393TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001394 PeerConnectionFactory::Options init_options;
1395 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1396 PeerConnectionFactory::Options recv_options;
1397 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001398 ASSERT_TRUE(
1399 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001400 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1401 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1402 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001403 LocalP2PTest();
1404
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001405 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001406 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1407 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1408 initializing_client()->GetDtlsCipherStats(),
1409 kMaxWaitForStatsMs);
1410 EXPECT_EQ(1, init_observer->GetEnumCounter(
1411 webrtc::kEnumCounterAudioSslCipher,
1412 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1413 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001414
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001415 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001416 initializing_client()->GetSrtpCipherStats(),
1417 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001418 EXPECT_EQ(1,
1419 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1420 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001421}
1422
1423// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1424// received supports 1.2.
ivoc191c1f92015-11-19 11:12:06 -08001425TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001426 PeerConnectionFactory::Options init_options;
1427 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1428 PeerConnectionFactory::Options recv_options;
1429 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001430 ASSERT_TRUE(
1431 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001432 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1433 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1434 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001435 LocalP2PTest();
1436
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001437 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001438 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1439 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1440 initializing_client()->GetDtlsCipherStats(),
1441 kMaxWaitForStatsMs);
1442 EXPECT_EQ(1, init_observer->GetEnumCounter(
1443 webrtc::kEnumCounterAudioSslCipher,
1444 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1445 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001446
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001447 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001448 initializing_client()->GetSrtpCipherStats(),
1449 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001450 EXPECT_EQ(1,
1451 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1452 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001453}
1454
deadbeefb5cb19b2015-11-23 16:39:12 -08001455// This test sets up a call between two parties with audio, video and an RTP
1456// data channel.
1457TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001458 FakeConstraints setup_constraints;
1459 setup_constraints.SetAllowRtpDataChannels();
1460 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1461 initializing_client()->CreateDataChannel();
1462 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001463 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1464 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001465 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1466 kMaxWaitMs);
1467 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1468 kMaxWaitMs);
1469
1470 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001471
1472 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1474 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001475
1476 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001477 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1478 kMaxWaitMs);
1479
1480 receiving_client()->data_channel()->Close();
1481 // Send new offer and answer.
1482 receiving_client()->Negotiate();
1483 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1484 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1485}
1486
deadbeefb5cb19b2015-11-23 16:39:12 -08001487// This test sets up a call between two parties with audio, video and an SCTP
1488// data channel.
1489TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestSctpDataChannel) {
1490 ASSERT_TRUE(CreateTestClients());
1491 initializing_client()->CreateDataChannel();
1492 LocalP2PTest();
1493 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1494 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
1495 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1496 kMaxWaitMs);
1497 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
1498
1499 std::string data = "hello world";
1500
1501 initializing_client()->data_channel()->Send(DataBuffer(data));
1502 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1503 kMaxWaitMs);
1504
1505 receiving_client()->data_channel()->Send(DataBuffer(data));
1506 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1507 kMaxWaitMs);
1508
1509 receiving_client()->data_channel()->Close();
1510 // Send new offer and answer.
1511 receiving_client()->Negotiate();
1512 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1513 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1514}
1515
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001516// This test sets up a call between two parties and creates a data channel.
1517// The test tests that received data is buffered unless an observer has been
1518// registered.
1519// Rtp data channels can receive data before the underlying
1520// transport has detected that a channel is writable and thus data can be
1521// received before the data channel state changes to open. That is hard to test
1522// but the same buffering is used in that case.
ivoc191c1f92015-11-19 11:12:06 -08001523TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001524 FakeConstraints setup_constraints;
1525 setup_constraints.SetAllowRtpDataChannels();
1526 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1527 initializing_client()->CreateDataChannel();
1528 initializing_client()->Negotiate();
1529
deadbeefaf1b59c2015-10-15 12:08:41 -07001530 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1531 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1533 kMaxWaitMs);
1534 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1535 receiving_client()->data_channel()->state(), kMaxWaitMs);
1536
1537 // Unregister the existing observer.
1538 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001539
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001541 SendRtpData(initializing_client()->data_channel(), data);
1542
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543 // Wait a while to allow the sent data to arrive before an observer is
1544 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001545 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001546
1547 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1548 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1549}
1550
1551// This test sets up a call between two parties with audio, video and but only
1552// the initiating client support data.
ivoc191c1f92015-11-19 11:12:06 -08001553TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
1554 LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001555 FakeConstraints setup_constraints_1;
1556 setup_constraints_1.SetAllowRtpDataChannels();
1557 // Must disable DTLS to make negotiation succeed.
1558 setup_constraints_1.SetMandatory(
1559 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1560 FakeConstraints setup_constraints_2;
1561 setup_constraints_2.SetMandatory(
1562 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1563 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001564 initializing_client()->CreateDataChannel();
1565 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001566 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567 EXPECT_FALSE(receiving_client()->data_channel());
1568 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1569}
1570
1571// This test sets up a call between two parties with audio, video. When audio
1572// and video is setup and flowing and data channel is negotiated.
ivoc191c1f92015-11-19 11:12:06 -08001573TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
1574 AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001575 FakeConstraints setup_constraints;
1576 setup_constraints.SetAllowRtpDataChannels();
1577 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1578 LocalP2PTest();
1579 initializing_client()->CreateDataChannel();
1580 // Send new offer and answer.
1581 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07001582 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1583 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1585 kMaxWaitMs);
1586 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1587 kMaxWaitMs);
1588}
1589
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001590// This test sets up a Jsep call with SCTP DataChannel and verifies the
1591// negotiation is completed without error.
1592#ifdef HAVE_SCTP
ivoc191c1f92015-11-19 11:12:06 -08001593TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001594 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001595 FakeConstraints constraints;
1596 constraints.SetMandatory(
1597 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1598 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1599 initializing_client()->CreateDataChannel();
1600 initializing_client()->Negotiate(false, false);
1601}
1602#endif
1603
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001604// This test sets up a call between two parties with audio, and video.
1605// During the call, the initializing side restart ice and the test verifies that
1606// new ice candidates are generated and audio and video still can flow.
ivoc191c1f92015-11-19 11:12:06 -08001607TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, MAYBE_IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001608 ASSERT_TRUE(CreateTestClients());
1609
1610 // Negotiate and wait for ice completion and make sure audio and video plays.
1611 LocalP2PTest();
1612
1613 // Create a SDP string of the first audio candidate for both clients.
1614 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1615 initializing_client()->pc()->local_description()->candidates(0);
1616 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1617 receiving_client()->pc()->local_description()->candidates(0);
1618 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1619 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1620 std::string initiator_candidate;
1621 EXPECT_TRUE(
1622 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1623 std::string receiver_candidate;
1624 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1625
1626 // Restart ice on the initializing client.
1627 receiving_client()->SetExpectIceRestart(true);
1628 initializing_client()->IceRestart();
1629
1630 // Negotiate and wait for ice completion again and make sure audio and video
1631 // plays.
1632 LocalP2PTest();
1633
1634 // Create a SDP string of the first audio candidate for both clients again.
1635 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1636 initializing_client()->pc()->local_description()->candidates(0);
1637 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1638 receiving_client()->pc()->local_description()->candidates(0);
1639 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1640 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1641 std::string initiator_candidate_restart;
1642 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1643 &initiator_candidate_restart));
1644 std::string receiver_candidate_restart;
1645 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1646 &receiver_candidate_restart));
1647
1648 // Verify that the first candidates in the local session descriptions has
1649 // changed.
1650 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1651 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1652}
1653
deadbeeffaac4972015-11-12 15:33:07 -08001654// This test sets up a call between two parties with audio, and video.
1655// It then renegotiates setting the video m-line to "port 0", then later
1656// renegotiates again, enabling video.
ivoc191c1f92015-11-19 11:12:06 -08001657TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08001658 ASSERT_TRUE(CreateTestClients());
1659
1660 // Do initial negotiation. Will result in video and audio sendonly m-lines.
1661 receiving_client()->set_auto_add_stream(false);
1662 initializing_client()->AddMediaStream(true, true);
1663 initializing_client()->Negotiate();
1664
1665 // Negotiate again, disabling the video m-line (receiving client will
1666 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
1667 receiving_client()->SetReceiveVideo(false);
1668 initializing_client()->Negotiate();
1669
1670 // Enable video and do negotiation again, making sure video is received
1671 // end-to-end.
1672 receiving_client()->SetReceiveVideo(true);
1673 receiving_client()->AddMediaStream(true, true);
1674 LocalP2PTest();
1675}
1676
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001677// This test sets up a Jsep call between two parties with external
1678// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001679// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1680// See issue webrtc/2378.
ivoc191c1f92015-11-19 11:12:06 -08001681TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
deadbeefcbc95072015-10-15 19:31:56 -07001682 DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001683 ASSERT_TRUE(CreateTestClients());
1684 EnableVideoDecoderFactory();
1685 LocalP2PTest();
1686}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001687
deadbeeffac06552015-11-25 11:26:01 -08001688// This tests that if we negotiate after calling CreateSender but before we
1689// have a track, then set a track later, frames from the newly-set track are
1690// received end-to-end.
1691TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, EarlyWarmupTest) {
1692 ASSERT_TRUE(CreateTestClients());
1693 auto audio_sender = initializing_client()->pc()->CreateSender("audio");
1694 auto video_sender = initializing_client()->pc()->CreateSender("video");
1695 initializing_client()->Negotiate();
1696 // Wait for ICE connection to complete, without any tracks.
1697 // Note that the receiving client WILL (in HandleIncomingOffer) create
1698 // tracks, so it's only the initiator here that's doing early warmup.
1699 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1700 VerifySessionDescriptions();
1701 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1702 initializing_client()->ice_connection_state(),
1703 kMaxWaitForFramesMs);
1704 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1705 receiving_client()->ice_connection_state(),
1706 kMaxWaitForFramesMs);
1707 // Now set the tracks, and expect frames to immediately start flowing.
1708 EXPECT_TRUE(
1709 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
1710 EXPECT_TRUE(
1711 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
1712 EXPECT_TRUE_WAIT(FramesNotPending(kEndAudioFrameCount, kEndVideoFrameCount),
1713 kMaxWaitForFramesMs);
1714}
1715
deadbeef0a6c4ca2015-10-06 11:38:28 -07001716class IceServerParsingTest : public testing::Test {
1717 public:
1718 // Convenience for parsing a single URL.
1719 bool ParseUrl(const std::string& url) {
1720 return ParseUrl(url, std::string(), std::string());
1721 }
1722
1723 bool ParseUrl(const std::string& url,
1724 const std::string& username,
1725 const std::string& password) {
1726 PeerConnectionInterface::IceServers servers;
1727 PeerConnectionInterface::IceServer server;
1728 server.urls.push_back(url);
1729 server.username = username;
1730 server.password = password;
1731 servers.push_back(server);
1732 return webrtc::ParseIceServers(servers, &stun_configurations_,
1733 &turn_configurations_);
1734 }
1735
1736 protected:
1737 webrtc::StunConfigurations stun_configurations_;
1738 webrtc::TurnConfigurations turn_configurations_;
1739};
1740
1741// Make sure all STUN/TURN prefixes are parsed correctly.
1742TEST_F(IceServerParsingTest, ParseStunPrefixes) {
1743 EXPECT_TRUE(ParseUrl("stun:hostname"));
1744 EXPECT_EQ(1U, stun_configurations_.size());
1745 EXPECT_EQ(0U, turn_configurations_.size());
1746 stun_configurations_.clear();
1747
1748 EXPECT_TRUE(ParseUrl("stuns:hostname"));
1749 EXPECT_EQ(1U, stun_configurations_.size());
1750 EXPECT_EQ(0U, turn_configurations_.size());
1751 stun_configurations_.clear();
1752
1753 EXPECT_TRUE(ParseUrl("turn:hostname"));
1754 EXPECT_EQ(0U, stun_configurations_.size());
1755 EXPECT_EQ(1U, turn_configurations_.size());
1756 EXPECT_FALSE(turn_configurations_[0].secure);
1757 turn_configurations_.clear();
1758
1759 EXPECT_TRUE(ParseUrl("turns:hostname"));
1760 EXPECT_EQ(0U, stun_configurations_.size());
1761 EXPECT_EQ(1U, turn_configurations_.size());
1762 EXPECT_TRUE(turn_configurations_[0].secure);
1763 turn_configurations_.clear();
1764
1765 // invalid prefixes
1766 EXPECT_FALSE(ParseUrl("stunn:hostname"));
1767 EXPECT_FALSE(ParseUrl(":hostname"));
1768 EXPECT_FALSE(ParseUrl(":"));
1769 EXPECT_FALSE(ParseUrl(""));
1770}
1771
1772TEST_F(IceServerParsingTest, VerifyDefaults) {
1773 // TURNS defaults
1774 EXPECT_TRUE(ParseUrl("turns:hostname"));
1775 EXPECT_EQ(1U, turn_configurations_.size());
1776 EXPECT_EQ(5349, turn_configurations_[0].server.port());
1777 EXPECT_EQ("tcp", turn_configurations_[0].transport_type);
1778 turn_configurations_.clear();
1779
1780 // TURN defaults
1781 EXPECT_TRUE(ParseUrl("turn:hostname"));
1782 EXPECT_EQ(1U, turn_configurations_.size());
1783 EXPECT_EQ(3478, turn_configurations_[0].server.port());
1784 EXPECT_EQ("udp", turn_configurations_[0].transport_type);
1785 turn_configurations_.clear();
1786
1787 // STUN defaults
1788 EXPECT_TRUE(ParseUrl("stun:hostname"));
1789 EXPECT_EQ(1U, stun_configurations_.size());
1790 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1791 stun_configurations_.clear();
1792}
1793
1794// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
1795// can be parsed correctly.
1796TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
1797 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
1798 EXPECT_EQ(1U, stun_configurations_.size());
1799 EXPECT_EQ("1.2.3.4", stun_configurations_[0].server.hostname());
1800 EXPECT_EQ(1234, stun_configurations_[0].server.port());
1801 stun_configurations_.clear();
1802
1803 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
1804 EXPECT_EQ(1U, stun_configurations_.size());
1805 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_configurations_[0].server.hostname());
1806 EXPECT_EQ(4321, stun_configurations_[0].server.port());
1807 stun_configurations_.clear();
1808
1809 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
1810 EXPECT_EQ(1U, stun_configurations_.size());
1811 EXPECT_EQ("hostname", stun_configurations_[0].server.hostname());
1812 EXPECT_EQ(9999, stun_configurations_[0].server.port());
1813 stun_configurations_.clear();
1814
1815 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
1816 EXPECT_EQ(1U, stun_configurations_.size());
1817 EXPECT_EQ("1.2.3.4", stun_configurations_[0].server.hostname());
1818 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1819 stun_configurations_.clear();
1820
1821 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
1822 EXPECT_EQ(1U, stun_configurations_.size());
1823 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_configurations_[0].server.hostname());
1824 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1825 stun_configurations_.clear();
1826
1827 EXPECT_TRUE(ParseUrl("stun:hostname"));
1828 EXPECT_EQ(1U, stun_configurations_.size());
1829 EXPECT_EQ("hostname", stun_configurations_[0].server.hostname());
1830 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1831 stun_configurations_.clear();
1832
1833 // Try some invalid hostname:port strings.
1834 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
1835 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
1836 EXPECT_FALSE(ParseUrl("stun:hostname:"));
1837 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
1838 EXPECT_FALSE(ParseUrl("stun::5555"));
1839 EXPECT_FALSE(ParseUrl("stun:"));
1840}
1841
1842// Test parsing the "?transport=xxx" part of the URL.
1843TEST_F(IceServerParsingTest, ParseTransport) {
1844 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
1845 EXPECT_EQ(1U, turn_configurations_.size());
1846 EXPECT_EQ("tcp", turn_configurations_[0].transport_type);
1847 turn_configurations_.clear();
1848
1849 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
1850 EXPECT_EQ(1U, turn_configurations_.size());
1851 EXPECT_EQ("udp", turn_configurations_[0].transport_type);
1852 turn_configurations_.clear();
1853
1854 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
1855}
1856
1857// Test parsing ICE username contained in URL.
1858TEST_F(IceServerParsingTest, ParseUsername) {
1859 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
1860 EXPECT_EQ(1U, turn_configurations_.size());
1861 EXPECT_EQ("user", turn_configurations_[0].username);
1862 turn_configurations_.clear();
1863
1864 EXPECT_FALSE(ParseUrl("turn:@hostname"));
1865 EXPECT_FALSE(ParseUrl("turn:username@"));
1866 EXPECT_FALSE(ParseUrl("turn:@"));
1867 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
1868}
1869
1870// Test that username and password from IceServer is copied into the resulting
1871// TurnConfiguration.
1872TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
1873 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
1874 EXPECT_EQ(1U, turn_configurations_.size());
1875 EXPECT_EQ("username", turn_configurations_[0].username);
1876 EXPECT_EQ("password", turn_configurations_[0].password);
1877}
1878
1879// Ensure that if a server has multiple URLs, each one is parsed.
1880TEST_F(IceServerParsingTest, ParseMultipleUrls) {
1881 PeerConnectionInterface::IceServers servers;
1882 PeerConnectionInterface::IceServer server;
1883 server.urls.push_back("stun:hostname");
1884 server.urls.push_back("turn:hostname");
1885 servers.push_back(server);
1886 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_configurations_,
1887 &turn_configurations_));
1888 EXPECT_EQ(1U, stun_configurations_.size());
1889 EXPECT_EQ(1U, turn_configurations_.size());
1890}
1891
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001892#endif // if !defined(THREAD_SANITIZER)