blob: 00100ac56773f90861d5d01005d31c64cc54d855 [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:
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800148 static PeerConnectionTestClient* CreateClientWithDtlsIdentityStore(
deadbeefaf1b59c2015-10-15 12:08:41 -0700149 const std::string& id,
150 const MediaConstraintsInterface* constraints,
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800151 const PeerConnectionFactory::Options* options,
152 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700153 PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800154 if (!client->Init(constraints, options, dtls_identity_store.Pass())) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700155 delete client;
156 return nullptr;
157 }
158 return client;
159 }
160
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800161 static PeerConnectionTestClient* CreateClient(
162 const std::string& id,
163 const MediaConstraintsInterface* constraints,
164 const PeerConnectionFactory::Options* options) {
165 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
166 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
167 : nullptr);
168
169 return CreateClientWithDtlsIdentityStore(id, constraints, options,
170 dtls_identity_store.Pass());
171 }
172
deadbeefaf1b59c2015-10-15 12:08:41 -0700173 ~PeerConnectionTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 while (!fake_video_renderers_.empty()) {
175 RenderMap::iterator it = fake_video_renderers_.begin();
176 delete it->second;
177 fake_video_renderers_.erase(it);
178 }
179 }
180
deadbeefaf1b59c2015-10-15 12:08:41 -0700181 void Negotiate() { Negotiate(true, true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182
deadbeefaf1b59c2015-10-15 12:08:41 -0700183 void Negotiate(bool audio, bool video) {
184 rtc::scoped_ptr<SessionDescriptionInterface> offer;
185 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186
deadbeefaf1b59c2015-10-15 12:08:41 -0700187 if (offer->description()->GetContentByName("audio")) {
188 offer->description()->GetContentByName("audio")->rejected = !audio;
189 }
190 if (offer->description()->GetContentByName("video")) {
191 offer->description()->GetContentByName("video")->rejected = !video;
192 }
193
194 std::string sdp;
195 EXPECT_TRUE(offer->ToString(&sdp));
196 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
197 signaling_message_receiver_->ReceiveSdpMessage(
198 webrtc::SessionDescriptionInterface::kOffer, sdp);
199 }
200
201 // SignalingMessageReceiver callback.
202 void ReceiveSdpMessage(const std::string& type, std::string& msg) override {
203 FilterIncomingSdpMessage(&msg);
204 if (type == webrtc::SessionDescriptionInterface::kOffer) {
205 HandleIncomingOffer(msg);
206 } else {
207 HandleIncomingAnswer(msg);
208 }
209 }
210
211 // SignalingMessageReceiver callback.
212 void ReceiveIceMessage(const std::string& sdp_mid,
213 int sdp_mline_index,
214 const std::string& msg) override {
215 LOG(INFO) << id_ << "ReceiveIceMessage";
216 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
217 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
218 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
219 }
220
221 // PeerConnectionObserver callbacks.
222 void OnSignalingChange(
223 webrtc::PeerConnectionInterface::SignalingState new_state) override {
224 EXPECT_EQ(pc()->signaling_state(), new_state);
225 }
deadbeeffaac4972015-11-12 15:33:07 -0800226 void OnAddStream(MediaStreamInterface* media_stream) override {
227 media_stream->RegisterObserver(this);
deadbeefaf1b59c2015-10-15 12:08:41 -0700228 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
229 const std::string id = media_stream->GetVideoTracks()[i]->id();
230 ASSERT_TRUE(fake_video_renderers_.find(id) ==
231 fake_video_renderers_.end());
232 fake_video_renderers_[id] =
233 new webrtc::FakeVideoTrackRenderer(media_stream->GetVideoTracks()[i]);
234 }
235 }
deadbeeffaac4972015-11-12 15:33:07 -0800236 void OnRemoveStream(MediaStreamInterface* media_stream) override {}
deadbeefaf1b59c2015-10-15 12:08:41 -0700237 void OnRenegotiationNeeded() override {}
238 void OnIceConnectionChange(
239 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
240 EXPECT_EQ(pc()->ice_connection_state(), new_state);
241 }
242 void OnIceGatheringChange(
243 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
244 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
245 }
246 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
247 LOG(INFO) << id_ << "OnIceCandidate";
248
249 std::string ice_sdp;
250 EXPECT_TRUE(candidate->ToString(&ice_sdp));
251 if (signaling_message_receiver_ == nullptr) {
252 // Remote party may be deleted.
253 return;
254 }
255 signaling_message_receiver_->ReceiveIceMessage(
256 candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
257 }
258
deadbeeffaac4972015-11-12 15:33:07 -0800259 // MediaStreamInterface callback
260 void OnChanged() override {
261 // Track added or removed from MediaStream, so update our renderers.
262 rtc::scoped_refptr<StreamCollectionInterface> remote_streams =
263 pc()->remote_streams();
264 // Remove renderers for tracks that were removed.
265 for (auto it = fake_video_renderers_.begin();
266 it != fake_video_renderers_.end();) {
267 if (remote_streams->FindVideoTrack(it->first) == nullptr) {
268 auto to_delete = it++;
269 delete to_delete->second;
270 fake_video_renderers_.erase(to_delete);
271 } else {
272 ++it;
273 }
274 }
275 // Create renderers for new video tracks.
276 for (size_t stream_index = 0; stream_index < remote_streams->count();
277 ++stream_index) {
278 MediaStreamInterface* remote_stream = remote_streams->at(stream_index);
279 for (size_t track_index = 0;
280 track_index < remote_stream->GetVideoTracks().size();
281 ++track_index) {
282 const std::string id =
283 remote_stream->GetVideoTracks()[track_index]->id();
284 if (fake_video_renderers_.find(id) != fake_video_renderers_.end()) {
285 continue;
286 }
287 fake_video_renderers_[id] = new webrtc::FakeVideoTrackRenderer(
288 remote_stream->GetVideoTracks()[track_index]);
289 }
290 }
291 }
292
deadbeefaf1b59c2015-10-15 12:08:41 -0700293 void SetVideoConstraints(const webrtc::FakeConstraints& video_constraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294 video_constraints_ = video_constraint;
295 }
296
297 void AddMediaStream(bool audio, bool video) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700298 std::string stream_label =
299 kStreamLabelBase +
300 rtc::ToString<int>(static_cast<int>(pc()->local_streams()->count()));
deadbeeffaac4972015-11-12 15:33:07 -0800301 rtc::scoped_refptr<MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000302 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303
304 if (audio && can_receive_audio()) {
deadbeeffac06552015-11-25 11:26:01 -0800305 stream->AddTrack(CreateLocalAudioTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306 }
307 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000308 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 }
310
deadbeefaf1b59c2015-10-15 12:08:41 -0700311 EXPECT_TRUE(pc()->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312 }
313
deadbeefaf1b59c2015-10-15 12:08:41 -0700314 size_t NumberOfLocalMediaStreams() { return pc()->local_streams()->count(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315
316 bool SessionActive() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700317 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318 }
319
deadbeeffaac4972015-11-12 15:33:07 -0800320 // Automatically add a stream when receiving an offer, if we don't have one.
321 // Defaults to true.
322 void set_auto_add_stream(bool auto_add_stream) {
323 auto_add_stream_ = auto_add_stream;
324 }
325
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326 void set_signaling_message_receiver(
deadbeefaf1b59c2015-10-15 12:08:41 -0700327 SignalingMessageReceiver* signaling_message_receiver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328 signaling_message_receiver_ = signaling_message_receiver;
329 }
330
331 void EnableVideoDecoderFactory() {
332 video_decoder_factory_enabled_ = true;
333 fake_video_decoder_factory_->AddSupportedVideoCodecType(
334 webrtc::kVideoCodecVP8);
335 }
336
deadbeefaf1b59c2015-10-15 12:08:41 -0700337 void IceRestart() {
338 session_description_constraints_.SetMandatoryIceRestart(true);
339 SetExpectIceRestart(true);
340 }
341
342 void SetExpectIceRestart(bool expect_restart) {
343 expect_ice_restart_ = expect_restart;
344 }
345
346 bool ExpectIceRestart() const { return expect_ice_restart_; }
347
348 void SetReceiveAudioVideo(bool audio, bool video) {
349 SetReceiveAudio(audio);
350 SetReceiveVideo(video);
351 ASSERT_EQ(audio, can_receive_audio());
352 ASSERT_EQ(video, can_receive_video());
353 }
354
355 void SetReceiveAudio(bool audio) {
356 if (audio && can_receive_audio())
357 return;
358 session_description_constraints_.SetMandatoryReceiveAudio(audio);
359 }
360
361 void SetReceiveVideo(bool video) {
362 if (video && can_receive_video())
363 return;
364 session_description_constraints_.SetMandatoryReceiveVideo(video);
365 }
366
367 void RemoveMsidFromReceivedSdp(bool remove) { remove_msid_ = remove; }
368
369 void RemoveSdesCryptoFromReceivedSdp(bool remove) { remove_sdes_ = remove; }
370
371 void RemoveBundleFromReceivedSdp(bool remove) { remove_bundle_ = remove; }
372
373 bool can_receive_audio() {
374 bool value;
375 if (webrtc::FindConstraint(&session_description_constraints_,
376 MediaConstraintsInterface::kOfferToReceiveAudio,
377 &value, nullptr)) {
378 return value;
379 }
380 return true;
381 }
382
383 bool can_receive_video() {
384 bool value;
385 if (webrtc::FindConstraint(&session_description_constraints_,
386 MediaConstraintsInterface::kOfferToReceiveVideo,
387 &value, nullptr)) {
388 return value;
389 }
390 return true;
391 }
392
393 void OnIceComplete() override { LOG(INFO) << id_ << "OnIceComplete"; }
394
395 void OnDataChannel(DataChannelInterface* data_channel) override {
396 LOG(INFO) << id_ << "OnDataChannel";
397 data_channel_ = data_channel;
398 data_observer_.reset(new MockDataChannelObserver(data_channel));
399 }
400
401 void CreateDataChannel() {
402 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, nullptr);
403 ASSERT_TRUE(data_channel_.get() != nullptr);
404 data_observer_.reset(new MockDataChannelObserver(data_channel_));
405 }
406
deadbeeffac06552015-11-25 11:26:01 -0800407 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack(
408 const std::string& stream_label) {
409 FakeConstraints constraints;
410 // Disable highpass filter so that we can get all the test audio frames.
411 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
412 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
413 peer_connection_factory_->CreateAudioSource(&constraints);
414 // TODO(perkj): Test audio source when it is implemented. Currently audio
415 // always use the default input.
416 std::string label = stream_label + kAudioTrackLabelBase;
417 return peer_connection_factory_->CreateAudioTrack(label, source);
418 }
419
420 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack(
421 const std::string& stream_label) {
422 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
423 FakeConstraints source_constraints = video_constraints_;
424 source_constraints.SetMandatoryMaxFrameRate(10);
425
426 cricket::FakeVideoCapturer* fake_capturer =
427 new webrtc::FakePeriodicVideoCapturer();
428 video_capturers_.push_back(fake_capturer);
429 rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
430 peer_connection_factory_->CreateVideoSource(fake_capturer,
431 &source_constraints);
432 std::string label = stream_label + kVideoTrackLabelBase;
433 return peer_connection_factory_->CreateVideoTrack(label, source);
434 }
435
deadbeefaf1b59c2015-10-15 12:08:41 -0700436 DataChannelInterface* data_channel() { return data_channel_; }
437 const MockDataChannelObserver* data_observer() const {
438 return data_observer_.get();
439 }
440
441 webrtc::PeerConnectionInterface* pc() { return peer_connection_.get(); }
442
443 void StopVideoCapturers() {
444 for (std::vector<cricket::VideoCapturer*>::iterator it =
445 video_capturers_.begin();
446 it != video_capturers_.end(); ++it) {
447 (*it)->Stop();
448 }
449 }
450
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451 bool AudioFramesReceivedCheck(int number_of_frames) const {
452 return number_of_frames <= fake_audio_capture_module_->frames_received();
453 }
454
455 bool VideoFramesReceivedCheck(int number_of_frames) {
456 if (video_decoder_factory_enabled_) {
457 const std::vector<FakeWebRtcVideoDecoder*>& decoders
458 = fake_video_decoder_factory_->decoders();
459 if (decoders.empty()) {
460 return number_of_frames <= 0;
461 }
462
463 for (std::vector<FakeWebRtcVideoDecoder*>::const_iterator
464 it = decoders.begin(); it != decoders.end(); ++it) {
465 if (number_of_frames > (*it)->GetNumFramesReceived()) {
466 return false;
467 }
468 }
469 return true;
470 } else {
471 if (fake_video_renderers_.empty()) {
472 return number_of_frames <= 0;
473 }
474
475 for (RenderMap::const_iterator it = fake_video_renderers_.begin();
476 it != fake_video_renderers_.end(); ++it) {
477 if (number_of_frames > it->second->num_rendered_frames()) {
478 return false;
479 }
480 }
481 return true;
482 }
483 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700484
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 // Verify the CreateDtmfSender interface
486 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000487 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
488 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489
490 // We can't create a DTMF sender with an invalid audio track or a non local
491 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700492 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000493 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700494 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
495 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496
497 // We should be able to create a DTMF sender from a local track.
498 webrtc::AudioTrackInterface* localtrack =
499 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
500 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700501 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 dtmf_sender->RegisterObserver(observer.get());
503
504 // Test the DtmfSender object just created.
505 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
506 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
507
508 // We don't need to verify that the DTMF tones are actually sent out because
509 // that is already covered by the tests of the lower level components.
510
511 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
512 std::vector<std::string> tones;
513 tones.push_back("1");
514 tones.push_back("a");
515 tones.push_back("");
516 observer->Verify(tones);
517
518 dtmf_sender->UnregisterObserver();
519 }
520
521 // Verifies that the SessionDescription have rejected the appropriate media
522 // content.
523 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700524 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
525 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 const cricket::SessionDescription* remote_desc =
527 peer_connection_->remote_description()->description();
528 const cricket::SessionDescription* local_desc =
529 peer_connection_->local_description()->description();
530
531 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
532 if (remote_audio_content) {
533 const ContentInfo* audio_content =
534 GetFirstAudioContent(local_desc);
535 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
536 }
537
538 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
539 if (remote_video_content) {
540 const ContentInfo* video_content =
541 GetFirstVideoContent(local_desc);
542 EXPECT_EQ(can_receive_video(), !video_content->rejected);
543 }
544 }
545
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700547 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548 const cricket::SessionDescription* desc =
549 peer_connection_->local_description()->description();
550 const cricket::ContentInfos& contents = desc->contents();
551
552 for (size_t index = 0; index < contents.size(); ++index) {
553 if (contents[index].rejected)
554 continue;
555 const cricket::TransportDescription* transport_desc =
556 desc->GetTransportDescriptionByName(contents[index].name);
557
558 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000559 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560 if (ufragpair_it == ice_ufrag_pwd_.end()) {
561 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000562 ice_ufrag_pwd_[static_cast<int>(index)] =
563 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564 } else if (ExpectIceRestart()) {
565 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
566 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
567 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
568 } else {
569 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
570 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
571 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
572 }
573 }
574 }
575
576 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000577 rtc::scoped_refptr<MockStatsObserver>
578 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000579 EXPECT_TRUE(peer_connection_->GetStats(
580 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700582 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583 return observer->AudioOutputLevel();
584 }
585
586 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000587 rtc::scoped_refptr<MockStatsObserver>
588 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000589 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700590 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700592 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 return observer->AudioInputLevel();
594 }
595
596 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000597 rtc::scoped_refptr<MockStatsObserver>
598 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000599 EXPECT_TRUE(peer_connection_->GetStats(
600 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700602 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 return observer->BytesReceived();
604 }
605
606 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000607 rtc::scoped_refptr<MockStatsObserver>
608 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000609 EXPECT_TRUE(peer_connection_->GetStats(
610 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700612 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613 return observer->BytesSent();
614 }
615
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000616 int GetAvailableReceivedBandwidthStats() {
617 rtc::scoped_refptr<MockStatsObserver>
618 observer(new rtc::RefCountedObject<MockStatsObserver>());
619 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700620 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000621 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700622 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000623 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000624 return bw;
625 }
626
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000627 std::string GetDtlsCipherStats() {
628 rtc::scoped_refptr<MockStatsObserver>
629 observer(new rtc::RefCountedObject<MockStatsObserver>());
630 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700631 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000632 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700633 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000634 return observer->DtlsCipher();
635 }
636
637 std::string GetSrtpCipherStats() {
638 rtc::scoped_refptr<MockStatsObserver>
639 observer(new rtc::RefCountedObject<MockStatsObserver>());
640 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700641 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000642 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700643 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000644 return observer->SrtpCipher();
645 }
646
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 int rendered_width() {
648 EXPECT_FALSE(fake_video_renderers_.empty());
649 return fake_video_renderers_.empty() ? 1 :
650 fake_video_renderers_.begin()->second->width();
651 }
652
653 int rendered_height() {
654 EXPECT_FALSE(fake_video_renderers_.empty());
655 return fake_video_renderers_.empty() ? 1 :
656 fake_video_renderers_.begin()->second->height();
657 }
658
659 size_t number_of_remote_streams() {
660 if (!pc())
661 return 0;
662 return pc()->remote_streams()->count();
663 }
664
665 StreamCollectionInterface* remote_streams() {
666 if (!pc()) {
667 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700668 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669 }
670 return pc()->remote_streams();
671 }
672
673 StreamCollectionInterface* local_streams() {
674 if (!pc()) {
675 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700676 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677 }
678 return pc()->local_streams();
679 }
680
681 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
682 return pc()->signaling_state();
683 }
684
685 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
686 return pc()->ice_connection_state();
687 }
688
689 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
690 return pc()->ice_gathering_state();
691 }
692
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693 private:
694 class DummyDtmfObserver : public DtmfSenderObserverInterface {
695 public:
696 DummyDtmfObserver() : completed_(false) {}
697
698 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700699 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 tones_.push_back(tone);
701 if (tone.empty()) {
702 completed_ = true;
703 }
704 }
705
706 void Verify(const std::vector<std::string>& tones) const {
707 ASSERT_TRUE(tones_.size() == tones.size());
708 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
709 }
710
711 bool completed() const { return completed_; }
712
713 private:
714 bool completed_;
715 std::vector<std::string> tones_;
716 };
717
deadbeefaf1b59c2015-10-15 12:08:41 -0700718 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
719
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800720 bool Init(
721 const MediaConstraintsInterface* constraints,
722 const PeerConnectionFactory::Options* options,
723 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700724 EXPECT_TRUE(!peer_connection_);
725 EXPECT_TRUE(!peer_connection_factory_);
726 allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
727 if (!allocator_factory_) {
728 return false;
729 }
730 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
731
732 if (fake_audio_capture_module_ == nullptr) {
733 return false;
734 }
735 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
736 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
737 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
738 rtc::Thread::Current(), rtc::Thread::Current(),
739 fake_audio_capture_module_, fake_video_encoder_factory_,
740 fake_video_decoder_factory_);
741 if (!peer_connection_factory_) {
742 return false;
743 }
744 if (options) {
745 peer_connection_factory_->SetOptions(*options);
746 }
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800747 peer_connection_ = CreatePeerConnection(
748 allocator_factory_.get(), constraints, dtls_identity_store.Pass());
deadbeefaf1b59c2015-10-15 12:08:41 -0700749 return peer_connection_.get() != nullptr;
750 }
751
deadbeefaf1b59c2015-10-15 12:08:41 -0700752 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
753 webrtc::PortAllocatorFactoryInterface* factory,
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800754 const MediaConstraintsInterface* constraints,
755 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 // CreatePeerConnection with IceServers.
757 webrtc::PeerConnectionInterface::IceServers ice_servers;
758 webrtc::PeerConnectionInterface::IceServer ice_server;
759 ice_server.uri = "stun:stun.l.google.com:19302";
760 ice_servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000761
deadbeefaf1b59c2015-10-15 12:08:41 -0700762 return peer_connection_factory_->CreatePeerConnection(
Henrik Boström5e56c592015-08-11 10:33:13 +0200763 ice_servers, constraints, factory, dtls_identity_store.Pass(), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 }
765
766 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700767 LOG(INFO) << id_ << "HandleIncomingOffer ";
deadbeeffaac4972015-11-12 15:33:07 -0800768 if (NumberOfLocalMediaStreams() == 0 && auto_add_stream_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 // If we are not sending any streams ourselves it is time to add some.
770 AddMediaStream(true, true);
771 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000772 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700773 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000775 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 EXPECT_TRUE(DoCreateAnswer(answer.use()));
777 std::string sdp;
778 EXPECT_TRUE(answer->ToString(&sdp));
779 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
deadbeefaf1b59c2015-10-15 12:08:41 -0700780 if (signaling_message_receiver_) {
781 signaling_message_receiver_->ReceiveSdpMessage(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 webrtc::SessionDescriptionInterface::kAnswer, sdp);
783 }
784 }
785
786 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700787 LOG(INFO) << id_ << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000788 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700789 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
791 }
792
793 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
794 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000795 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
796 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 MockCreateSessionDescriptionObserver>());
798 if (offer) {
799 pc()->CreateOffer(observer, &session_description_constraints_);
800 } else {
801 pc()->CreateAnswer(observer, &session_description_constraints_);
802 }
803 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
804 *desc = observer->release_desc();
805 if (observer->result() && ExpectIceRestart()) {
806 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
807 }
808 return observer->result();
809 }
810
811 bool DoCreateOffer(SessionDescriptionInterface** desc) {
812 return DoCreateOfferAnswer(desc, true);
813 }
814
815 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
816 return DoCreateOfferAnswer(desc, false);
817 }
818
819 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000820 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
821 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700823 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 pc()->SetLocalDescription(observer, desc);
825 // Ignore the observer result. If we wait for the result with
826 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
827 // before the offer which is an error.
828 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000829 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 // ProcessMessages waits at least 1ms but processes all messages before
831 // returning. Since this test is synchronous and send messages to the remote
832 // peer whenever a callback is invoked, this can lead to messages being
833 // sent to the remote peer in the wrong order.
834 // TODO(perkj): Find a way to check the result without risking that the
835 // order of sent messages are changed. Ex- by posting all messages that are
836 // sent to the remote peer.
837 return true;
838 }
839
840 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000841 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
842 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700844 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000845 pc()->SetRemoteDescription(observer, desc);
846 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
847 return observer->result();
848 }
849
850 // This modifies all received SDP messages before they are processed.
851 void FilterIncomingSdpMessage(std::string* sdp) {
852 if (remove_msid_) {
853 const char kSdpSsrcAttribute[] = "a=ssrc:";
854 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
855 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
856 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
857 }
858 if (remove_bundle_) {
859 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
860 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
861 }
862 if (remove_sdes_) {
863 const char kSdpSdesCryptoAttribute[] = "a=crypto";
864 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
865 }
866 }
867
deadbeefaf1b59c2015-10-15 12:08:41 -0700868 std::string id_;
869
870 rtc::scoped_refptr<webrtc::PortAllocatorFactoryInterface> allocator_factory_;
871 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
872 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
873 peer_connection_factory_;
874
deadbeeffaac4972015-11-12 15:33:07 -0800875 bool auto_add_stream_ = true;
876
deadbeefaf1b59c2015-10-15 12:08:41 -0700877 typedef std::pair<std::string, std::string> IceUfragPwdPair;
878 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
879 bool expect_ice_restart_ = false;
880
881 // Needed to keep track of number of frames send.
882 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
883 // Needed to keep track of number of frames received.
884 typedef std::map<std::string, webrtc::FakeVideoTrackRenderer*> RenderMap;
885 RenderMap fake_video_renderers_;
886 // Needed to keep track of number of frames received when external decoder
887 // used.
888 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
889 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
890 bool video_decoder_factory_enabled_ = false;
891 webrtc::FakeConstraints video_constraints_;
892
893 // For remote peer communication.
894 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
895
896 // Store references to the video capturers we've created, so that we can stop
897 // them, if required.
898 std::vector<cricket::VideoCapturer*> video_capturers_;
899
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 webrtc::FakeConstraints session_description_constraints_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700901 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
902 bool remove_bundle_ =
903 false; // True if bundle should be removed in received SDP.
904 bool remove_sdes_ =
905 false; // True if a=crypto should be removed in received SDP.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000907 rtc::scoped_refptr<DataChannelInterface> data_channel_;
908 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909};
910
ivoc191c1f92015-11-19 11:12:06 -0800911// Flaky on Mac Debug bots. See webrtc:5231
912#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
913#define MAYBE_JsepPeerConnectionP2PTestClient \
914 DISABLED_JsepPeerConnectionP2PTestClient
915#else
916#define MAYBE_JsepPeerConnectionP2PTestClient JsepPeerConnectionP2PTestClient
917#endif
918
deadbeefcbc95072015-10-15 19:31:56 -0700919// TODO(deadbeef): Rename this to P2PTestConductor once the Linux memcheck and
920// Windows DrMemory Full bots' blacklists are updated.
ivoc191c1f92015-11-19 11:12:06 -0800921class MAYBE_JsepPeerConnectionP2PTestClient : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 public:
ivoc191c1f92015-11-19 11:12:06 -0800923 MAYBE_JsepPeerConnectionP2PTestClient()
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000924 : pss_(new rtc::PhysicalSocketServer),
925 ss_(new rtc::VirtualSocketServer(pss_.get())),
926 ss_scope_(ss_.get()) {}
927
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 bool SessionActive() {
929 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000930 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000931 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000932
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000933 // Return true if the number of frames provided have been received or it is
934 // known that that will never occur (e.g. no frames will be sent or
935 // captured).
936 bool FramesNotPending(int audio_frames_to_receive,
937 int video_frames_to_receive) {
938 return VideoFramesReceivedCheck(video_frames_to_receive) &&
939 AudioFramesReceivedCheck(audio_frames_to_receive);
940 }
941 bool AudioFramesReceivedCheck(int frames_received) {
942 return initiating_client_->AudioFramesReceivedCheck(frames_received) &&
943 receiving_client_->AudioFramesReceivedCheck(frames_received);
944 }
945 bool VideoFramesReceivedCheck(int frames_received) {
946 return initiating_client_->VideoFramesReceivedCheck(frames_received) &&
947 receiving_client_->VideoFramesReceivedCheck(frames_received);
948 }
949 void VerifyDtmf() {
950 initiating_client_->VerifyDtmf();
951 receiving_client_->VerifyDtmf();
952 }
953
954 void TestUpdateOfferWithRejectedContent() {
955 initiating_client_->Negotiate(true, false);
956 EXPECT_TRUE_WAIT(
957 FramesNotPending(kEndAudioFrameCount * 2, kEndVideoFrameCount),
958 kMaxWaitForFramesMs);
959 // There shouldn't be any more video frame after the new offer is
960 // negotiated.
961 EXPECT_FALSE(VideoFramesReceivedCheck(kEndVideoFrameCount + 1));
962 }
963
964 void VerifyRenderedSize(int width, int height) {
965 EXPECT_EQ(width, receiving_client()->rendered_width());
966 EXPECT_EQ(height, receiving_client()->rendered_height());
967 EXPECT_EQ(width, initializing_client()->rendered_width());
968 EXPECT_EQ(height, initializing_client()->rendered_height());
969 }
970
971 void VerifySessionDescriptions() {
972 initiating_client_->VerifyRejectedMediaInSessionDescription();
973 receiving_client_->VerifyRejectedMediaInSessionDescription();
974 initiating_client_->VerifyLocalIceUfragAndPassword();
975 receiving_client_->VerifyLocalIceUfragAndPassword();
976 }
977
ivoc191c1f92015-11-19 11:12:06 -0800978 ~MAYBE_JsepPeerConnectionP2PTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700980 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981 }
982 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700983 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985 }
986
deadbeefaf1b59c2015-10-15 12:08:41 -0700987 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988
989 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
990 MediaConstraintsInterface* recv_constraints) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700991 return CreateTestClients(init_constraints, nullptr, recv_constraints,
992 nullptr);
Joachim Bauch04e5b492015-05-29 09:40:39 +0200993 }
994
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800995 void SetSignalingReceivers() {
996 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
997 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
998 }
999
Joachim Bauch04e5b492015-05-29 09:40:39 +02001000 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1001 PeerConnectionFactory::Options* init_options,
1002 MediaConstraintsInterface* recv_constraints,
1003 PeerConnectionFactory::Options* recv_options) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001004 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
1005 "Caller: ", init_constraints, init_options));
1006 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
1007 "Callee: ", recv_constraints, recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001008 if (!initiating_client_ || !receiving_client_) {
1009 return false;
1010 }
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -08001011 SetSignalingReceivers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012 return true;
1013 }
1014
1015 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1016 const webrtc::FakeConstraints& recv_constraints) {
1017 initiating_client_->SetVideoConstraints(init_constraints);
1018 receiving_client_->SetVideoConstraints(recv_constraints);
1019 }
1020
1021 void EnableVideoDecoderFactory() {
1022 initiating_client_->EnableVideoDecoderFactory();
1023 receiving_client_->EnableVideoDecoderFactory();
1024 }
1025
1026 // This test sets up a call between two parties. Both parties send static
1027 // frames to each other. Once the test is finished the number of sent frames
1028 // is compared to the number of received frames.
1029 void LocalP2PTest() {
1030 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1031 initiating_client_->AddMediaStream(true, true);
1032 }
1033 initiating_client_->Negotiate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034 // Assert true is used here since next tests are guaranteed to fail and
1035 // would eat up 5 seconds.
1036 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1037 VerifySessionDescriptions();
1038
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001039 int audio_frame_count = kEndAudioFrameCount;
1040 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1041 if (!initiating_client_->can_receive_audio() ||
1042 !receiving_client_->can_receive_audio()) {
1043 audio_frame_count = -1;
1044 }
1045 int video_frame_count = kEndVideoFrameCount;
1046 if (!initiating_client_->can_receive_video() ||
1047 !receiving_client_->can_receive_video()) {
1048 video_frame_count = -1;
1049 }
1050
1051 if (audio_frame_count != -1 || video_frame_count != -1) {
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001052 // Audio or video is expected to flow, so both clients should reach the
1053 // Connected state, and the offerer (ICE controller) should proceed to
1054 // Completed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001055 // Note: These tests have been observed to fail under heavy load at
1056 // shorter timeouts, so they may be flaky.
1057 EXPECT_EQ_WAIT(
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001058 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001059 initiating_client_->ice_connection_state(),
1060 kMaxWaitForFramesMs);
1061 EXPECT_EQ_WAIT(
1062 webrtc::PeerConnectionInterface::kIceConnectionConnected,
1063 receiving_client_->ice_connection_state(),
1064 kMaxWaitForFramesMs);
1065 }
1066
1067 if (initiating_client_->can_receive_audio() ||
1068 initiating_client_->can_receive_video()) {
1069 // The initiating client can receive media, so it must produce candidates
1070 // that will serve as destinations for that media.
1071 // TODO(bemasc): Understand why the state is not already Complete here, as
1072 // seems to be the case for the receiving client. This may indicate a bug
1073 // in the ICE gathering system.
1074 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1075 initiating_client_->ice_gathering_state());
1076 }
1077 if (receiving_client_->can_receive_audio() ||
1078 receiving_client_->can_receive_video()) {
1079 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1080 receiving_client_->ice_gathering_state(),
1081 kMaxWaitForFramesMs);
1082 }
1083
1084 EXPECT_TRUE_WAIT(FramesNotPending(audio_frame_count, video_frame_count),
1085 kMaxWaitForFramesMs);
1086 }
1087
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -08001088 void SetupAndVerifyDtlsCall() {
1089 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1090 FakeConstraints setup_constraints;
1091 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1092 true);
1093 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1094 LocalP2PTest();
1095 VerifyRenderedSize(640, 480);
1096 }
1097
1098 PeerConnectionTestClient* CreateDtlsClientWithAlternateKey() {
1099 FakeConstraints setup_constraints;
1100 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1101 true);
1102
1103 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
1104 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
1105 : nullptr);
1106 dtls_identity_store->use_alternate_key();
1107
1108 // Make sure the new client is using a different certificate.
1109 return PeerConnectionTestClient::CreateClientWithDtlsIdentityStore(
1110 "New Peer: ", &setup_constraints, nullptr, dtls_identity_store.Pass());
1111 }
1112
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001113 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1114 // Messages may get lost on the unreliable DataChannel, so we send multiple
1115 // times to avoid test flakiness.
1116 static const size_t kSendAttempts = 5;
1117
1118 for (size_t i = 0; i < kSendAttempts; ++i) {
1119 dc->Send(DataBuffer(data));
1120 }
1121 }
1122
deadbeefaf1b59c2015-10-15 12:08:41 -07001123 PeerConnectionTestClient* initializing_client() {
1124 return initiating_client_.get();
1125 }
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -08001126
1127 // Set the |initiating_client_| to the |client| passed in and return the
1128 // original |initiating_client_|.
1129 PeerConnectionTestClient* set_initializing_client(
1130 PeerConnectionTestClient* client) {
1131 PeerConnectionTestClient* old = initiating_client_.release();
1132 initiating_client_.reset(client);
1133 return old;
1134 }
1135
deadbeefaf1b59c2015-10-15 12:08:41 -07001136 PeerConnectionTestClient* receiving_client() {
1137 return receiving_client_.get();
1138 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001139
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -08001140 // Set the |receiving_client_| to the |client| passed in and return the
1141 // original |receiving_client_|.
1142 PeerConnectionTestClient* set_receiving_client(
1143 PeerConnectionTestClient* client) {
1144 PeerConnectionTestClient* old = receiving_client_.release();
1145 receiving_client_.reset(client);
1146 return old;
1147 }
1148
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001149 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001150 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1151 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1152 rtc::SocketServerScope ss_scope_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001153 rtc::scoped_ptr<PeerConnectionTestClient> initiating_client_;
1154 rtc::scoped_ptr<PeerConnectionTestClient> receiving_client_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001155};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001156
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001157// Disable for TSan v2, see
1158// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1159#if !defined(THREAD_SANITIZER)
1160
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001162// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1163// See issue webrtc/2378.
ivoc191c1f92015-11-19 11:12:06 -08001164TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001165 ASSERT_TRUE(CreateTestClients());
1166 LocalP2PTest();
1167 VerifyDtmf();
1168}
1169
1170// This test sets up a Jsep call between two parties and test that we can get a
1171// video aspect ratio of 16:9.
ivoc191c1f92015-11-19 11:12:06 -08001172TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001173 ASSERT_TRUE(CreateTestClients());
1174 FakeConstraints constraint;
1175 double requested_ratio = 640.0/360;
1176 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1177 SetVideoConstraints(constraint, constraint);
1178 LocalP2PTest();
1179
1180 ASSERT_LE(0, initializing_client()->rendered_height());
1181 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001182 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183 initializing_client()->rendered_height();
1184 EXPECT_LE(requested_ratio, initiating_video_ratio);
1185
1186 ASSERT_LE(0, receiving_client()->rendered_height());
1187 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001188 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189 receiving_client()->rendered_height();
1190 EXPECT_LE(requested_ratio, receiving_video_ratio);
1191}
1192
1193// This test sets up a Jsep call between two parties and test that the
1194// received video has a resolution of 1280*720.
1195// TODO(mallinath): Enable when
1196// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
ivoc191c1f92015-11-19 11:12:06 -08001197TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198 ASSERT_TRUE(CreateTestClients());
1199 FakeConstraints constraint;
1200 constraint.SetMandatoryMinWidth(1280);
1201 constraint.SetMandatoryMinHeight(720);
1202 SetVideoConstraints(constraint, constraint);
1203 LocalP2PTest();
1204 VerifyRenderedSize(1280, 720);
1205}
1206
1207// This test sets up a call between two endpoints that are configured to use
1208// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
ivoc191c1f92015-11-19 11:12:06 -08001209TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestDtls) {
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -08001210 SetupAndVerifyDtlsCall();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211}
1212
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001213// This test sets up a audio call initially and then upgrades to audio/video,
1214// using DTLS.
ivoc191c1f92015-11-19 11:12:06 -08001215TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001216 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001217 FakeConstraints setup_constraints;
1218 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1219 true);
1220 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1221 receiving_client()->SetReceiveAudioVideo(true, false);
1222 LocalP2PTest();
1223 receiving_client()->SetReceiveAudioVideo(true, true);
1224 receiving_client()->Negotiate();
1225}
1226
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -08001227// This test sets up a call transfer to a new caller with a different DTLS
1228// fingerprint.
1229TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestDtlsTransferCallee) {
1230 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1231 SetupAndVerifyDtlsCall();
1232
1233 // Keeping the original peer around which will still send packets to the
1234 // receiving client. These SRTP packets will be dropped.
1235 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1236 set_initializing_client(CreateDtlsClientWithAlternateKey()));
1237
1238 SetSignalingReceivers();
1239 receiving_client()->SetExpectIceRestart(true);
1240 LocalP2PTest();
1241 VerifyRenderedSize(640, 480);
1242}
1243
1244// This test sets up a call transfer to a new callee with a different DTLS
1245// fingerprint.
1246TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestDtlsTransferCaller) {
1247 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1248 SetupAndVerifyDtlsCall();
1249
1250 // Keeping the original peer around which will still send packets to the
1251 // receiving client. These SRTP packets will be dropped.
1252 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1253 set_receiving_client(CreateDtlsClientWithAlternateKey()));
1254
1255 SetSignalingReceivers();
1256 initializing_client()->IceRestart();
1257 LocalP2PTest();
1258 VerifyRenderedSize(640, 480);
1259}
1260
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001261// This test sets up a call between two endpoints that are configured to use
1262// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1263// negotiated and used for transport.
Ivo Creusen9cf0c3d2015-12-04 10:36:57 +01001264TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001265 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001266 FakeConstraints setup_constraints;
1267 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1268 true);
1269 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1270 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1271 LocalP2PTest();
1272 VerifyRenderedSize(640, 480);
1273}
1274
1275// This test sets up a Jsep call between two parties, and the callee only
1276// accept to receive video.
ivoc191c1f92015-11-19 11:12:06 -08001277TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001278 ASSERT_TRUE(CreateTestClients());
1279 receiving_client()->SetReceiveAudioVideo(false, true);
1280 LocalP2PTest();
1281}
1282
1283// This test sets up a Jsep call between two parties, and the callee only
1284// accept to receive audio.
ivoc191c1f92015-11-19 11:12:06 -08001285TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001286 ASSERT_TRUE(CreateTestClients());
1287 receiving_client()->SetReceiveAudioVideo(true, false);
1288 LocalP2PTest();
1289}
1290
1291// This test sets up a Jsep call between two parties, and the callee reject both
1292// audio and video.
ivoc191c1f92015-11-19 11:12:06 -08001293TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294 ASSERT_TRUE(CreateTestClients());
1295 receiving_client()->SetReceiveAudioVideo(false, false);
1296 LocalP2PTest();
1297}
1298
1299// This test sets up an audio and video call between two parties. After the call
1300// runs for a while (10 frames), the caller sends an update offer with video
1301// being rejected. Once the re-negotiation is done, the video flow should stop
1302// and the audio flow should continue.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001303// Disabled due to b/14955157.
ivoc191c1f92015-11-19 11:12:06 -08001304TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
deadbeefcbc95072015-10-15 19:31:56 -07001305 DISABLED_UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001306 ASSERT_TRUE(CreateTestClients());
1307 LocalP2PTest();
1308 TestUpdateOfferWithRejectedContent();
1309}
1310
1311// This test sets up a Jsep call between two parties. The MSID is removed from
1312// the SDP strings from the caller.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001313// Disabled due to b/14955157.
ivoc191c1f92015-11-19 11:12:06 -08001314TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
1315 DISABLED_LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001316 ASSERT_TRUE(CreateTestClients());
1317 receiving_client()->RemoveMsidFromReceivedSdp(true);
1318 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1319 // audio and video is muxed when MSID is disabled. Remove
1320 // SetRemoveBundleFromSdp once
1321 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1322 receiving_client()->RemoveBundleFromReceivedSdp(true);
1323 LocalP2PTest();
1324}
1325
1326// This test sets up a Jsep call between two parties and the initiating peer
1327// sends two steams.
1328// TODO(perkj): Disabled due to
1329// https://code.google.com/p/webrtc/issues/detail?id=1454
ivoc191c1f92015-11-19 11:12:06 -08001330TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001331 ASSERT_TRUE(CreateTestClients());
1332 // Set optional video constraint to max 320pixels to decrease CPU usage.
1333 FakeConstraints constraint;
1334 constraint.SetOptionalMaxWidth(320);
1335 SetVideoConstraints(constraint, constraint);
1336 initializing_client()->AddMediaStream(true, true);
1337 initializing_client()->AddMediaStream(false, true);
1338 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1339 LocalP2PTest();
1340 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1341}
1342
1343// Test that we can receive the audio output level from a remote audio track.
Ivo Creusen9cf0c3d2015-12-04 10:36:57 +01001344TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345 ASSERT_TRUE(CreateTestClients());
1346 LocalP2PTest();
1347
1348 StreamCollectionInterface* remote_streams =
1349 initializing_client()->remote_streams();
1350 ASSERT_GT(remote_streams->count(), 0u);
1351 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1352 MediaStreamTrackInterface* remote_audio_track =
1353 remote_streams->at(0)->GetAudioTracks()[0];
1354
1355 // Get the audio output level stats. Note that the level is not available
1356 // until a RTCP packet has been received.
1357 EXPECT_TRUE_WAIT(
1358 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1359 kMaxWaitForStatsMs);
1360}
1361
1362// Test that an audio input level is reported.
Ivo Creusen9cf0c3d2015-12-04 10:36:57 +01001363TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364 ASSERT_TRUE(CreateTestClients());
1365 LocalP2PTest();
1366
1367 // Get the audio input level stats. The level should be available very
1368 // soon after the test starts.
1369 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1370 kMaxWaitForStatsMs);
1371}
1372
1373// Test that we can get incoming byte counts from both audio and video tracks.
Ivo Creusen9cf0c3d2015-12-04 10:36:57 +01001374TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001375 ASSERT_TRUE(CreateTestClients());
1376 LocalP2PTest();
1377
1378 StreamCollectionInterface* remote_streams =
1379 initializing_client()->remote_streams();
1380 ASSERT_GT(remote_streams->count(), 0u);
1381 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1382 MediaStreamTrackInterface* remote_audio_track =
1383 remote_streams->at(0)->GetAudioTracks()[0];
1384 EXPECT_TRUE_WAIT(
1385 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1386 kMaxWaitForStatsMs);
1387
1388 MediaStreamTrackInterface* remote_video_track =
1389 remote_streams->at(0)->GetVideoTracks()[0];
1390 EXPECT_TRUE_WAIT(
1391 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1392 kMaxWaitForStatsMs);
1393}
1394
1395// Test that we can get outgoing byte counts from both audio and video tracks.
Ivo Creusen9cf0c3d2015-12-04 10:36:57 +01001396TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001397 ASSERT_TRUE(CreateTestClients());
1398 LocalP2PTest();
1399
1400 StreamCollectionInterface* local_streams =
1401 initializing_client()->local_streams();
1402 ASSERT_GT(local_streams->count(), 0u);
1403 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1404 MediaStreamTrackInterface* local_audio_track =
1405 local_streams->at(0)->GetAudioTracks()[0];
1406 EXPECT_TRUE_WAIT(
1407 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1408 kMaxWaitForStatsMs);
1409
1410 MediaStreamTrackInterface* local_video_track =
1411 local_streams->at(0)->GetVideoTracks()[0];
1412 EXPECT_TRUE_WAIT(
1413 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1414 kMaxWaitForStatsMs);
1415}
1416
Joachim Bauch04e5b492015-05-29 09:40:39 +02001417// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
ivoc191c1f92015-11-19 11:12:06 -08001418TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001419 PeerConnectionFactory::Options init_options;
1420 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1421 PeerConnectionFactory::Options recv_options;
1422 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001423 ASSERT_TRUE(
1424 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001425 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1426 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1427 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001428 LocalP2PTest();
1429
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001430 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001431 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1432 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1433 initializing_client()->GetDtlsCipherStats(),
1434 kMaxWaitForStatsMs);
1435 EXPECT_EQ(1, init_observer->GetEnumCounter(
1436 webrtc::kEnumCounterAudioSslCipher,
1437 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1438 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001439
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001440 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001441 initializing_client()->GetSrtpCipherStats(),
1442 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001443 EXPECT_EQ(1,
1444 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1445 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001446}
1447
1448// Test that DTLS 1.2 is used if both ends support it.
Ivo Creusen9cf0c3d2015-12-04 10:36:57 +01001449TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001450 PeerConnectionFactory::Options init_options;
1451 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1452 PeerConnectionFactory::Options recv_options;
1453 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001454 ASSERT_TRUE(
1455 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001456 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1457 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1458 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001459 LocalP2PTest();
1460
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001461 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001462 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1463 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)),
1464 initializing_client()->GetDtlsCipherStats(),
1465 kMaxWaitForStatsMs);
1466 EXPECT_EQ(1, init_observer->GetEnumCounter(
1467 webrtc::kEnumCounterAudioSslCipher,
1468 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1469 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001470
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001471 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001472 initializing_client()->GetSrtpCipherStats(),
1473 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001474 EXPECT_EQ(1,
1475 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1476 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001477}
1478
1479// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1480// received supports 1.0.
ivoc191c1f92015-11-19 11:12:06 -08001481TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001482 PeerConnectionFactory::Options init_options;
1483 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1484 PeerConnectionFactory::Options recv_options;
1485 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001486 ASSERT_TRUE(
1487 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001488 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1489 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1490 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001491 LocalP2PTest();
1492
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001493 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001494 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1495 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1496 initializing_client()->GetDtlsCipherStats(),
1497 kMaxWaitForStatsMs);
1498 EXPECT_EQ(1, init_observer->GetEnumCounter(
1499 webrtc::kEnumCounterAudioSslCipher,
1500 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1501 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001502
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001503 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001504 initializing_client()->GetSrtpCipherStats(),
1505 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001506 EXPECT_EQ(1,
1507 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1508 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001509}
1510
1511// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1512// received supports 1.2.
ivoc191c1f92015-11-19 11:12:06 -08001513TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001514 PeerConnectionFactory::Options init_options;
1515 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1516 PeerConnectionFactory::Options recv_options;
1517 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001518 ASSERT_TRUE(
1519 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001520 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1521 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1522 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001523 LocalP2PTest();
1524
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001525 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001526 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1527 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1528 initializing_client()->GetDtlsCipherStats(),
1529 kMaxWaitForStatsMs);
1530 EXPECT_EQ(1, init_observer->GetEnumCounter(
1531 webrtc::kEnumCounterAudioSslCipher,
1532 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1533 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001534
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001535 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001536 initializing_client()->GetSrtpCipherStats(),
1537 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001538 EXPECT_EQ(1,
1539 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1540 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001541}
1542
deadbeefb5cb19b2015-11-23 16:39:12 -08001543// This test sets up a call between two parties with audio, video and an RTP
1544// data channel.
1545TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001546 FakeConstraints setup_constraints;
1547 setup_constraints.SetAllowRtpDataChannels();
1548 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1549 initializing_client()->CreateDataChannel();
1550 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001551 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1552 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001553 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1554 kMaxWaitMs);
1555 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1556 kMaxWaitMs);
1557
1558 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001559
1560 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001561 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1562 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001563
1564 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001565 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1566 kMaxWaitMs);
1567
1568 receiving_client()->data_channel()->Close();
1569 // Send new offer and answer.
1570 receiving_client()->Negotiate();
1571 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1572 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1573}
1574
deadbeefb5cb19b2015-11-23 16:39:12 -08001575// This test sets up a call between two parties with audio, video and an SCTP
1576// data channel.
1577TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestSctpDataChannel) {
1578 ASSERT_TRUE(CreateTestClients());
1579 initializing_client()->CreateDataChannel();
1580 LocalP2PTest();
1581 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1582 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
1583 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1584 kMaxWaitMs);
1585 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
1586
1587 std::string data = "hello world";
1588
1589 initializing_client()->data_channel()->Send(DataBuffer(data));
1590 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1591 kMaxWaitMs);
1592
1593 receiving_client()->data_channel()->Send(DataBuffer(data));
1594 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1595 kMaxWaitMs);
1596
1597 receiving_client()->data_channel()->Close();
1598 // Send new offer and answer.
1599 receiving_client()->Negotiate();
1600 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1601 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1602}
1603
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001604// This test sets up a call between two parties and creates a data channel.
1605// The test tests that received data is buffered unless an observer has been
1606// registered.
1607// Rtp data channels can receive data before the underlying
1608// transport has detected that a channel is writable and thus data can be
1609// received before the data channel state changes to open. That is hard to test
1610// but the same buffering is used in that case.
ivoc191c1f92015-11-19 11:12:06 -08001611TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001612 FakeConstraints setup_constraints;
1613 setup_constraints.SetAllowRtpDataChannels();
1614 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1615 initializing_client()->CreateDataChannel();
1616 initializing_client()->Negotiate();
1617
deadbeefaf1b59c2015-10-15 12:08:41 -07001618 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1619 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001620 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1621 kMaxWaitMs);
1622 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1623 receiving_client()->data_channel()->state(), kMaxWaitMs);
1624
1625 // Unregister the existing observer.
1626 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001627
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001629 SendRtpData(initializing_client()->data_channel(), data);
1630
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631 // Wait a while to allow the sent data to arrive before an observer is
1632 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001633 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634
1635 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1636 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1637}
1638
1639// This test sets up a call between two parties with audio, video and but only
1640// the initiating client support data.
ivoc191c1f92015-11-19 11:12:06 -08001641TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
1642 LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001643 FakeConstraints setup_constraints_1;
1644 setup_constraints_1.SetAllowRtpDataChannels();
1645 // Must disable DTLS to make negotiation succeed.
1646 setup_constraints_1.SetMandatory(
1647 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1648 FakeConstraints setup_constraints_2;
1649 setup_constraints_2.SetMandatory(
1650 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1651 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001652 initializing_client()->CreateDataChannel();
1653 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001654 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 EXPECT_FALSE(receiving_client()->data_channel());
1656 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1657}
1658
1659// This test sets up a call between two parties with audio, video. When audio
1660// and video is setup and flowing and data channel is negotiated.
ivoc191c1f92015-11-19 11:12:06 -08001661TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
1662 AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001663 FakeConstraints setup_constraints;
1664 setup_constraints.SetAllowRtpDataChannels();
1665 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1666 LocalP2PTest();
1667 initializing_client()->CreateDataChannel();
1668 // Send new offer and answer.
1669 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07001670 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1671 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1673 kMaxWaitMs);
1674 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1675 kMaxWaitMs);
1676}
1677
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001678// This test sets up a Jsep call with SCTP DataChannel and verifies the
1679// negotiation is completed without error.
1680#ifdef HAVE_SCTP
ivoc191c1f92015-11-19 11:12:06 -08001681TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001682 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001683 FakeConstraints constraints;
1684 constraints.SetMandatory(
1685 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1686 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1687 initializing_client()->CreateDataChannel();
1688 initializing_client()->Negotiate(false, false);
1689}
1690#endif
1691
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001692// This test sets up a call between two parties with audio, and video.
1693// During the call, the initializing side restart ice and the test verifies that
1694// new ice candidates are generated and audio and video still can flow.
Ivo Creusen9cf0c3d2015-12-04 10:36:57 +01001695TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001696 ASSERT_TRUE(CreateTestClients());
1697
1698 // Negotiate and wait for ice completion and make sure audio and video plays.
1699 LocalP2PTest();
1700
1701 // Create a SDP string of the first audio candidate for both clients.
1702 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1703 initializing_client()->pc()->local_description()->candidates(0);
1704 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1705 receiving_client()->pc()->local_description()->candidates(0);
1706 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1707 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1708 std::string initiator_candidate;
1709 EXPECT_TRUE(
1710 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1711 std::string receiver_candidate;
1712 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1713
1714 // Restart ice on the initializing client.
1715 receiving_client()->SetExpectIceRestart(true);
1716 initializing_client()->IceRestart();
1717
1718 // Negotiate and wait for ice completion again and make sure audio and video
1719 // plays.
1720 LocalP2PTest();
1721
1722 // Create a SDP string of the first audio candidate for both clients again.
1723 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1724 initializing_client()->pc()->local_description()->candidates(0);
1725 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1726 receiving_client()->pc()->local_description()->candidates(0);
1727 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1728 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1729 std::string initiator_candidate_restart;
1730 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1731 &initiator_candidate_restart));
1732 std::string receiver_candidate_restart;
1733 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1734 &receiver_candidate_restart));
1735
1736 // Verify that the first candidates in the local session descriptions has
1737 // changed.
1738 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1739 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1740}
1741
deadbeeffaac4972015-11-12 15:33:07 -08001742// This test sets up a call between two parties with audio, and video.
1743// It then renegotiates setting the video m-line to "port 0", then later
1744// renegotiates again, enabling video.
ivoc191c1f92015-11-19 11:12:06 -08001745TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08001746 ASSERT_TRUE(CreateTestClients());
1747
1748 // Do initial negotiation. Will result in video and audio sendonly m-lines.
1749 receiving_client()->set_auto_add_stream(false);
1750 initializing_client()->AddMediaStream(true, true);
1751 initializing_client()->Negotiate();
1752
1753 // Negotiate again, disabling the video m-line (receiving client will
1754 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
1755 receiving_client()->SetReceiveVideo(false);
1756 initializing_client()->Negotiate();
1757
1758 // Enable video and do negotiation again, making sure video is received
1759 // end-to-end.
1760 receiving_client()->SetReceiveVideo(true);
1761 receiving_client()->AddMediaStream(true, true);
1762 LocalP2PTest();
1763}
1764
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765// This test sets up a Jsep call between two parties with external
1766// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001767// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1768// See issue webrtc/2378.
ivoc191c1f92015-11-19 11:12:06 -08001769TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
deadbeefcbc95072015-10-15 19:31:56 -07001770 DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771 ASSERT_TRUE(CreateTestClients());
1772 EnableVideoDecoderFactory();
1773 LocalP2PTest();
1774}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001775
deadbeeffac06552015-11-25 11:26:01 -08001776// This tests that if we negotiate after calling CreateSender but before we
1777// have a track, then set a track later, frames from the newly-set track are
1778// received end-to-end.
1779TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, EarlyWarmupTest) {
1780 ASSERT_TRUE(CreateTestClients());
1781 auto audio_sender = initializing_client()->pc()->CreateSender("audio");
1782 auto video_sender = initializing_client()->pc()->CreateSender("video");
1783 initializing_client()->Negotiate();
1784 // Wait for ICE connection to complete, without any tracks.
1785 // Note that the receiving client WILL (in HandleIncomingOffer) create
1786 // tracks, so it's only the initiator here that's doing early warmup.
1787 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1788 VerifySessionDescriptions();
1789 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1790 initializing_client()->ice_connection_state(),
1791 kMaxWaitForFramesMs);
1792 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1793 receiving_client()->ice_connection_state(),
1794 kMaxWaitForFramesMs);
1795 // Now set the tracks, and expect frames to immediately start flowing.
1796 EXPECT_TRUE(
1797 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
1798 EXPECT_TRUE(
1799 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
1800 EXPECT_TRUE_WAIT(FramesNotPending(kEndAudioFrameCount, kEndVideoFrameCount),
1801 kMaxWaitForFramesMs);
1802}
1803
deadbeef0a6c4ca2015-10-06 11:38:28 -07001804class IceServerParsingTest : public testing::Test {
1805 public:
1806 // Convenience for parsing a single URL.
1807 bool ParseUrl(const std::string& url) {
1808 return ParseUrl(url, std::string(), std::string());
1809 }
1810
1811 bool ParseUrl(const std::string& url,
1812 const std::string& username,
1813 const std::string& password) {
1814 PeerConnectionInterface::IceServers servers;
1815 PeerConnectionInterface::IceServer server;
1816 server.urls.push_back(url);
1817 server.username = username;
1818 server.password = password;
1819 servers.push_back(server);
1820 return webrtc::ParseIceServers(servers, &stun_configurations_,
1821 &turn_configurations_);
1822 }
1823
1824 protected:
1825 webrtc::StunConfigurations stun_configurations_;
1826 webrtc::TurnConfigurations turn_configurations_;
1827};
1828
1829// Make sure all STUN/TURN prefixes are parsed correctly.
1830TEST_F(IceServerParsingTest, ParseStunPrefixes) {
1831 EXPECT_TRUE(ParseUrl("stun:hostname"));
1832 EXPECT_EQ(1U, stun_configurations_.size());
1833 EXPECT_EQ(0U, turn_configurations_.size());
1834 stun_configurations_.clear();
1835
1836 EXPECT_TRUE(ParseUrl("stuns:hostname"));
1837 EXPECT_EQ(1U, stun_configurations_.size());
1838 EXPECT_EQ(0U, turn_configurations_.size());
1839 stun_configurations_.clear();
1840
1841 EXPECT_TRUE(ParseUrl("turn:hostname"));
1842 EXPECT_EQ(0U, stun_configurations_.size());
1843 EXPECT_EQ(1U, turn_configurations_.size());
1844 EXPECT_FALSE(turn_configurations_[0].secure);
1845 turn_configurations_.clear();
1846
1847 EXPECT_TRUE(ParseUrl("turns:hostname"));
1848 EXPECT_EQ(0U, stun_configurations_.size());
1849 EXPECT_EQ(1U, turn_configurations_.size());
1850 EXPECT_TRUE(turn_configurations_[0].secure);
1851 turn_configurations_.clear();
1852
1853 // invalid prefixes
1854 EXPECT_FALSE(ParseUrl("stunn:hostname"));
1855 EXPECT_FALSE(ParseUrl(":hostname"));
1856 EXPECT_FALSE(ParseUrl(":"));
1857 EXPECT_FALSE(ParseUrl(""));
1858}
1859
1860TEST_F(IceServerParsingTest, VerifyDefaults) {
1861 // TURNS defaults
1862 EXPECT_TRUE(ParseUrl("turns:hostname"));
1863 EXPECT_EQ(1U, turn_configurations_.size());
1864 EXPECT_EQ(5349, turn_configurations_[0].server.port());
1865 EXPECT_EQ("tcp", turn_configurations_[0].transport_type);
1866 turn_configurations_.clear();
1867
1868 // TURN defaults
1869 EXPECT_TRUE(ParseUrl("turn:hostname"));
1870 EXPECT_EQ(1U, turn_configurations_.size());
1871 EXPECT_EQ(3478, turn_configurations_[0].server.port());
1872 EXPECT_EQ("udp", turn_configurations_[0].transport_type);
1873 turn_configurations_.clear();
1874
1875 // STUN defaults
1876 EXPECT_TRUE(ParseUrl("stun:hostname"));
1877 EXPECT_EQ(1U, stun_configurations_.size());
1878 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1879 stun_configurations_.clear();
1880}
1881
1882// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
1883// can be parsed correctly.
1884TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
1885 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
1886 EXPECT_EQ(1U, stun_configurations_.size());
1887 EXPECT_EQ("1.2.3.4", stun_configurations_[0].server.hostname());
1888 EXPECT_EQ(1234, stun_configurations_[0].server.port());
1889 stun_configurations_.clear();
1890
1891 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
1892 EXPECT_EQ(1U, stun_configurations_.size());
1893 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_configurations_[0].server.hostname());
1894 EXPECT_EQ(4321, stun_configurations_[0].server.port());
1895 stun_configurations_.clear();
1896
1897 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
1898 EXPECT_EQ(1U, stun_configurations_.size());
1899 EXPECT_EQ("hostname", stun_configurations_[0].server.hostname());
1900 EXPECT_EQ(9999, stun_configurations_[0].server.port());
1901 stun_configurations_.clear();
1902
1903 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
1904 EXPECT_EQ(1U, stun_configurations_.size());
1905 EXPECT_EQ("1.2.3.4", stun_configurations_[0].server.hostname());
1906 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1907 stun_configurations_.clear();
1908
1909 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
1910 EXPECT_EQ(1U, stun_configurations_.size());
1911 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_configurations_[0].server.hostname());
1912 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1913 stun_configurations_.clear();
1914
1915 EXPECT_TRUE(ParseUrl("stun:hostname"));
1916 EXPECT_EQ(1U, stun_configurations_.size());
1917 EXPECT_EQ("hostname", stun_configurations_[0].server.hostname());
1918 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1919 stun_configurations_.clear();
1920
1921 // Try some invalid hostname:port strings.
1922 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
1923 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
1924 EXPECT_FALSE(ParseUrl("stun:hostname:"));
1925 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
1926 EXPECT_FALSE(ParseUrl("stun::5555"));
1927 EXPECT_FALSE(ParseUrl("stun:"));
1928}
1929
1930// Test parsing the "?transport=xxx" part of the URL.
1931TEST_F(IceServerParsingTest, ParseTransport) {
1932 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
1933 EXPECT_EQ(1U, turn_configurations_.size());
1934 EXPECT_EQ("tcp", turn_configurations_[0].transport_type);
1935 turn_configurations_.clear();
1936
1937 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
1938 EXPECT_EQ(1U, turn_configurations_.size());
1939 EXPECT_EQ("udp", turn_configurations_[0].transport_type);
1940 turn_configurations_.clear();
1941
1942 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
1943}
1944
1945// Test parsing ICE username contained in URL.
1946TEST_F(IceServerParsingTest, ParseUsername) {
1947 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
1948 EXPECT_EQ(1U, turn_configurations_.size());
1949 EXPECT_EQ("user", turn_configurations_[0].username);
1950 turn_configurations_.clear();
1951
1952 EXPECT_FALSE(ParseUrl("turn:@hostname"));
1953 EXPECT_FALSE(ParseUrl("turn:username@"));
1954 EXPECT_FALSE(ParseUrl("turn:@"));
1955 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
1956}
1957
1958// Test that username and password from IceServer is copied into the resulting
1959// TurnConfiguration.
1960TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
1961 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
1962 EXPECT_EQ(1U, turn_configurations_.size());
1963 EXPECT_EQ("username", turn_configurations_[0].username);
1964 EXPECT_EQ("password", turn_configurations_[0].password);
1965}
1966
1967// Ensure that if a server has multiple URLs, each one is parsed.
1968TEST_F(IceServerParsingTest, ParseMultipleUrls) {
1969 PeerConnectionInterface::IceServers servers;
1970 PeerConnectionInterface::IceServer server;
1971 server.urls.push_back("stun:hostname");
1972 server.urls.push_back("turn:hostname");
1973 servers.push_back(server);
1974 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_configurations_,
1975 &turn_configurations_));
1976 EXPECT_EQ(1U, stun_configurations_.size());
1977 EXPECT_EQ(1U, turn_configurations_.size());
1978}
1979
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001980#endif // if !defined(THREAD_SANITIZER)