blob: 9ecf08014c7cd3458c4cb9f35cb1550c4969eed2 [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 Shieh1218d7a2015-12-05 09:59:56 -0800148 static PeerConnectionTestClient* CreateClientWithDtlsIdentityStore(
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800149 const std::string& id,
150 const MediaConstraintsInterface* constraints,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800151 const PeerConnectionFactory::Options* options,
152 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800153 PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800154 if (!client->Init(constraints, options, dtls_identity_store.Pass())) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800155 delete client;
156 return nullptr;
157 }
158 return client;
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800159 }
160
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -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 Shieh1218d7a2015-12-05 09:59:56 -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 Shieh1218d7a2015-12-05 09:59:56 -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 Shieh1218d7a2015-12-05 09:59:56 -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 Shieh1218d7a2015-12-05 09:59:56 -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 Shieh1218d7a2015-12-05 09:59:56 -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 Shieh1218d7a2015-12-05 09:59:56 -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 Shieh1218d7a2015-12-05 09:59:56 -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 Shieh1218d7a2015-12-05 09:59:56 -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 Shieh1218d7a2015-12-05 09:59:56 -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 Shieh1218d7a2015-12-05 09:59:56 -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 original_peer->pc()->Close();
1238
1239 SetSignalingReceivers();
1240 receiving_client()->SetExpectIceRestart(true);
1241 LocalP2PTest();
1242 VerifyRenderedSize(640, 480);
1243}
1244
1245// This test sets up a call transfer to a new callee with a different DTLS
1246// fingerprint.
1247TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestDtlsTransferCaller) {
1248 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1249 SetupAndVerifyDtlsCall();
1250
1251 // Keeping the original peer around which will still send packets to the
1252 // receiving client. These SRTP packets will be dropped.
1253 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1254 set_receiving_client(CreateDtlsClientWithAlternateKey()));
1255 original_peer->pc()->Close();
1256
1257 SetSignalingReceivers();
1258 initializing_client()->IceRestart();
1259 LocalP2PTest();
1260 VerifyRenderedSize(640, 480);
1261}
1262
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001263// This test sets up a call between two endpoints that are configured to use
1264// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1265// negotiated and used for transport.
Ivo Creusen9cf0c3d2015-12-04 10:36:57 +01001266TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001267 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001268 FakeConstraints setup_constraints;
1269 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1270 true);
1271 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1272 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1273 LocalP2PTest();
1274 VerifyRenderedSize(640, 480);
1275}
1276
1277// This test sets up a Jsep call between two parties, and the callee only
1278// accept to receive video.
ivoc191c1f92015-11-19 11:12:06 -08001279TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280 ASSERT_TRUE(CreateTestClients());
1281 receiving_client()->SetReceiveAudioVideo(false, true);
1282 LocalP2PTest();
1283}
1284
1285// This test sets up a Jsep call between two parties, and the callee only
1286// accept to receive audio.
ivoc191c1f92015-11-19 11:12:06 -08001287TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288 ASSERT_TRUE(CreateTestClients());
1289 receiving_client()->SetReceiveAudioVideo(true, false);
1290 LocalP2PTest();
1291}
1292
1293// This test sets up a Jsep call between two parties, and the callee reject both
1294// audio and video.
ivoc191c1f92015-11-19 11:12:06 -08001295TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001296 ASSERT_TRUE(CreateTestClients());
1297 receiving_client()->SetReceiveAudioVideo(false, false);
1298 LocalP2PTest();
1299}
1300
1301// This test sets up an audio and video call between two parties. After the call
1302// runs for a while (10 frames), the caller sends an update offer with video
1303// being rejected. Once the re-negotiation is done, the video flow should stop
1304// and the audio flow should continue.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001305// Disabled due to b/14955157.
ivoc191c1f92015-11-19 11:12:06 -08001306TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
deadbeefcbc95072015-10-15 19:31:56 -07001307 DISABLED_UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001308 ASSERT_TRUE(CreateTestClients());
1309 LocalP2PTest();
1310 TestUpdateOfferWithRejectedContent();
1311}
1312
1313// This test sets up a Jsep call between two parties. The MSID is removed from
1314// the SDP strings from the caller.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001315// Disabled due to b/14955157.
ivoc191c1f92015-11-19 11:12:06 -08001316TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
1317 DISABLED_LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001318 ASSERT_TRUE(CreateTestClients());
1319 receiving_client()->RemoveMsidFromReceivedSdp(true);
1320 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1321 // audio and video is muxed when MSID is disabled. Remove
1322 // SetRemoveBundleFromSdp once
1323 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1324 receiving_client()->RemoveBundleFromReceivedSdp(true);
1325 LocalP2PTest();
1326}
1327
1328// This test sets up a Jsep call between two parties and the initiating peer
1329// sends two steams.
1330// TODO(perkj): Disabled due to
1331// https://code.google.com/p/webrtc/issues/detail?id=1454
ivoc191c1f92015-11-19 11:12:06 -08001332TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001333 ASSERT_TRUE(CreateTestClients());
1334 // Set optional video constraint to max 320pixels to decrease CPU usage.
1335 FakeConstraints constraint;
1336 constraint.SetOptionalMaxWidth(320);
1337 SetVideoConstraints(constraint, constraint);
1338 initializing_client()->AddMediaStream(true, true);
1339 initializing_client()->AddMediaStream(false, true);
1340 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1341 LocalP2PTest();
1342 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1343}
1344
1345// Test that we can receive the audio output level from a remote audio track.
Ivo Creusen9cf0c3d2015-12-04 10:36:57 +01001346TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001347 ASSERT_TRUE(CreateTestClients());
1348 LocalP2PTest();
1349
1350 StreamCollectionInterface* remote_streams =
1351 initializing_client()->remote_streams();
1352 ASSERT_GT(remote_streams->count(), 0u);
1353 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1354 MediaStreamTrackInterface* remote_audio_track =
1355 remote_streams->at(0)->GetAudioTracks()[0];
1356
1357 // Get the audio output level stats. Note that the level is not available
1358 // until a RTCP packet has been received.
1359 EXPECT_TRUE_WAIT(
1360 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1361 kMaxWaitForStatsMs);
1362}
1363
1364// Test that an audio input level is reported.
Ivo Creusen9cf0c3d2015-12-04 10:36:57 +01001365TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366 ASSERT_TRUE(CreateTestClients());
1367 LocalP2PTest();
1368
1369 // Get the audio input level stats. The level should be available very
1370 // soon after the test starts.
1371 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1372 kMaxWaitForStatsMs);
1373}
1374
1375// Test that we can get incoming byte counts from both audio and video tracks.
Ivo Creusen9cf0c3d2015-12-04 10:36:57 +01001376TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001377 ASSERT_TRUE(CreateTestClients());
1378 LocalP2PTest();
1379
1380 StreamCollectionInterface* remote_streams =
1381 initializing_client()->remote_streams();
1382 ASSERT_GT(remote_streams->count(), 0u);
1383 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1384 MediaStreamTrackInterface* remote_audio_track =
1385 remote_streams->at(0)->GetAudioTracks()[0];
1386 EXPECT_TRUE_WAIT(
1387 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1388 kMaxWaitForStatsMs);
1389
1390 MediaStreamTrackInterface* remote_video_track =
1391 remote_streams->at(0)->GetVideoTracks()[0];
1392 EXPECT_TRUE_WAIT(
1393 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1394 kMaxWaitForStatsMs);
1395}
1396
1397// Test that we can get outgoing byte counts from both audio and video tracks.
Ivo Creusen9cf0c3d2015-12-04 10:36:57 +01001398TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001399 ASSERT_TRUE(CreateTestClients());
1400 LocalP2PTest();
1401
1402 StreamCollectionInterface* local_streams =
1403 initializing_client()->local_streams();
1404 ASSERT_GT(local_streams->count(), 0u);
1405 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1406 MediaStreamTrackInterface* local_audio_track =
1407 local_streams->at(0)->GetAudioTracks()[0];
1408 EXPECT_TRUE_WAIT(
1409 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1410 kMaxWaitForStatsMs);
1411
1412 MediaStreamTrackInterface* local_video_track =
1413 local_streams->at(0)->GetVideoTracks()[0];
1414 EXPECT_TRUE_WAIT(
1415 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1416 kMaxWaitForStatsMs);
1417}
1418
Joachim Bauch04e5b492015-05-29 09:40:39 +02001419// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
ivoc191c1f92015-11-19 11:12:06 -08001420TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001421 PeerConnectionFactory::Options init_options;
1422 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1423 PeerConnectionFactory::Options recv_options;
1424 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001425 ASSERT_TRUE(
1426 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001427 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1428 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1429 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001430 LocalP2PTest();
1431
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001432 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001433 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1434 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1435 initializing_client()->GetDtlsCipherStats(),
1436 kMaxWaitForStatsMs);
1437 EXPECT_EQ(1, init_observer->GetEnumCounter(
1438 webrtc::kEnumCounterAudioSslCipher,
1439 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1440 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001441
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001442 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001443 initializing_client()->GetSrtpCipherStats(),
1444 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001445 EXPECT_EQ(1,
1446 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1447 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001448}
1449
1450// Test that DTLS 1.2 is used if both ends support it.
Ivo Creusen9cf0c3d2015-12-04 10:36:57 +01001451TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001452 PeerConnectionFactory::Options init_options;
1453 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1454 PeerConnectionFactory::Options recv_options;
1455 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001456 ASSERT_TRUE(
1457 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001458 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1459 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1460 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001461 LocalP2PTest();
1462
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001463 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001464 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1465 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)),
1466 initializing_client()->GetDtlsCipherStats(),
1467 kMaxWaitForStatsMs);
1468 EXPECT_EQ(1, init_observer->GetEnumCounter(
1469 webrtc::kEnumCounterAudioSslCipher,
1470 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1471 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001472
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001473 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001474 initializing_client()->GetSrtpCipherStats(),
1475 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001476 EXPECT_EQ(1,
1477 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1478 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001479}
1480
1481// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1482// received supports 1.0.
ivoc191c1f92015-11-19 11:12:06 -08001483TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001484 PeerConnectionFactory::Options init_options;
1485 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1486 PeerConnectionFactory::Options recv_options;
1487 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001488 ASSERT_TRUE(
1489 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001490 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1491 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1492 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001493 LocalP2PTest();
1494
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001495 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001496 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1497 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1498 initializing_client()->GetDtlsCipherStats(),
1499 kMaxWaitForStatsMs);
1500 EXPECT_EQ(1, init_observer->GetEnumCounter(
1501 webrtc::kEnumCounterAudioSslCipher,
1502 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1503 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001504
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001505 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001506 initializing_client()->GetSrtpCipherStats(),
1507 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001508 EXPECT_EQ(1,
1509 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1510 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001511}
1512
1513// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1514// received supports 1.2.
ivoc191c1f92015-11-19 11:12:06 -08001515TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001516 PeerConnectionFactory::Options init_options;
1517 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1518 PeerConnectionFactory::Options recv_options;
1519 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001520 ASSERT_TRUE(
1521 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001522 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1523 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1524 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001525 LocalP2PTest();
1526
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001527 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001528 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1529 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1530 initializing_client()->GetDtlsCipherStats(),
1531 kMaxWaitForStatsMs);
1532 EXPECT_EQ(1, init_observer->GetEnumCounter(
1533 webrtc::kEnumCounterAudioSslCipher,
1534 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1535 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001536
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001537 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001538 initializing_client()->GetSrtpCipherStats(),
1539 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001540 EXPECT_EQ(1,
1541 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1542 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001543}
1544
deadbeefb5cb19b2015-11-23 16:39:12 -08001545// This test sets up a call between two parties with audio, video and an RTP
1546// data channel.
1547TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001548 FakeConstraints setup_constraints;
1549 setup_constraints.SetAllowRtpDataChannels();
1550 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1551 initializing_client()->CreateDataChannel();
1552 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001553 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1554 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1556 kMaxWaitMs);
1557 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1558 kMaxWaitMs);
1559
1560 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001561
1562 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001563 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1564 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001565
1566 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1568 kMaxWaitMs);
1569
1570 receiving_client()->data_channel()->Close();
1571 // Send new offer and answer.
1572 receiving_client()->Negotiate();
1573 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1574 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1575}
1576
deadbeefb5cb19b2015-11-23 16:39:12 -08001577// This test sets up a call between two parties with audio, video and an SCTP
1578// data channel.
1579TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestSctpDataChannel) {
1580 ASSERT_TRUE(CreateTestClients());
1581 initializing_client()->CreateDataChannel();
1582 LocalP2PTest();
1583 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1584 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
1585 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1586 kMaxWaitMs);
1587 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
1588
1589 std::string data = "hello world";
1590
1591 initializing_client()->data_channel()->Send(DataBuffer(data));
1592 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1593 kMaxWaitMs);
1594
1595 receiving_client()->data_channel()->Send(DataBuffer(data));
1596 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1597 kMaxWaitMs);
1598
1599 receiving_client()->data_channel()->Close();
1600 // Send new offer and answer.
1601 receiving_client()->Negotiate();
1602 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1603 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1604}
1605
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606// This test sets up a call between two parties and creates a data channel.
1607// The test tests that received data is buffered unless an observer has been
1608// registered.
1609// Rtp data channels can receive data before the underlying
1610// transport has detected that a channel is writable and thus data can be
1611// received before the data channel state changes to open. That is hard to test
1612// but the same buffering is used in that case.
ivoc191c1f92015-11-19 11:12:06 -08001613TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614 FakeConstraints setup_constraints;
1615 setup_constraints.SetAllowRtpDataChannels();
1616 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1617 initializing_client()->CreateDataChannel();
1618 initializing_client()->Negotiate();
1619
deadbeefaf1b59c2015-10-15 12:08:41 -07001620 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1621 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001622 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1623 kMaxWaitMs);
1624 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1625 receiving_client()->data_channel()->state(), kMaxWaitMs);
1626
1627 // Unregister the existing observer.
1628 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001629
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001631 SendRtpData(initializing_client()->data_channel(), data);
1632
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001633 // Wait a while to allow the sent data to arrive before an observer is
1634 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001635 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636
1637 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1638 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1639}
1640
1641// This test sets up a call between two parties with audio, video and but only
1642// the initiating client support data.
ivoc191c1f92015-11-19 11:12:06 -08001643TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
1644 LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001645 FakeConstraints setup_constraints_1;
1646 setup_constraints_1.SetAllowRtpDataChannels();
1647 // Must disable DTLS to make negotiation succeed.
1648 setup_constraints_1.SetMandatory(
1649 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1650 FakeConstraints setup_constraints_2;
1651 setup_constraints_2.SetMandatory(
1652 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1653 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654 initializing_client()->CreateDataChannel();
1655 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001656 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001657 EXPECT_FALSE(receiving_client()->data_channel());
1658 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1659}
1660
1661// This test sets up a call between two parties with audio, video. When audio
1662// and video is setup and flowing and data channel is negotiated.
ivoc191c1f92015-11-19 11:12:06 -08001663TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
1664 AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665 FakeConstraints setup_constraints;
1666 setup_constraints.SetAllowRtpDataChannels();
1667 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1668 LocalP2PTest();
1669 initializing_client()->CreateDataChannel();
1670 // Send new offer and answer.
1671 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07001672 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1673 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1675 kMaxWaitMs);
1676 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1677 kMaxWaitMs);
1678}
1679
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001680// This test sets up a Jsep call with SCTP DataChannel and verifies the
1681// negotiation is completed without error.
1682#ifdef HAVE_SCTP
ivoc191c1f92015-11-19 11:12:06 -08001683TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001684 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001685 FakeConstraints constraints;
1686 constraints.SetMandatory(
1687 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1688 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1689 initializing_client()->CreateDataChannel();
1690 initializing_client()->Negotiate(false, false);
1691}
1692#endif
1693
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001694// This test sets up a call between two parties with audio, and video.
1695// During the call, the initializing side restart ice and the test verifies that
1696// new ice candidates are generated and audio and video still can flow.
Ivo Creusen9cf0c3d2015-12-04 10:36:57 +01001697TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698 ASSERT_TRUE(CreateTestClients());
1699
1700 // Negotiate and wait for ice completion and make sure audio and video plays.
1701 LocalP2PTest();
1702
1703 // Create a SDP string of the first audio candidate for both clients.
1704 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1705 initializing_client()->pc()->local_description()->candidates(0);
1706 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1707 receiving_client()->pc()->local_description()->candidates(0);
1708 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1709 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1710 std::string initiator_candidate;
1711 EXPECT_TRUE(
1712 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1713 std::string receiver_candidate;
1714 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1715
1716 // Restart ice on the initializing client.
1717 receiving_client()->SetExpectIceRestart(true);
1718 initializing_client()->IceRestart();
1719
1720 // Negotiate and wait for ice completion again and make sure audio and video
1721 // plays.
1722 LocalP2PTest();
1723
1724 // Create a SDP string of the first audio candidate for both clients again.
1725 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1726 initializing_client()->pc()->local_description()->candidates(0);
1727 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1728 receiving_client()->pc()->local_description()->candidates(0);
1729 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1730 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1731 std::string initiator_candidate_restart;
1732 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1733 &initiator_candidate_restart));
1734 std::string receiver_candidate_restart;
1735 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1736 &receiver_candidate_restart));
1737
1738 // Verify that the first candidates in the local session descriptions has
1739 // changed.
1740 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1741 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1742}
1743
deadbeeffaac4972015-11-12 15:33:07 -08001744// This test sets up a call between two parties with audio, and video.
1745// It then renegotiates setting the video m-line to "port 0", then later
1746// renegotiates again, enabling video.
ivoc191c1f92015-11-19 11:12:06 -08001747TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08001748 ASSERT_TRUE(CreateTestClients());
1749
1750 // Do initial negotiation. Will result in video and audio sendonly m-lines.
1751 receiving_client()->set_auto_add_stream(false);
1752 initializing_client()->AddMediaStream(true, true);
1753 initializing_client()->Negotiate();
1754
1755 // Negotiate again, disabling the video m-line (receiving client will
1756 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
1757 receiving_client()->SetReceiveVideo(false);
1758 initializing_client()->Negotiate();
1759
1760 // Enable video and do negotiation again, making sure video is received
1761 // end-to-end.
1762 receiving_client()->SetReceiveVideo(true);
1763 receiving_client()->AddMediaStream(true, true);
1764 LocalP2PTest();
1765}
1766
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767// This test sets up a Jsep call between two parties with external
1768// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001769// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1770// See issue webrtc/2378.
ivoc191c1f92015-11-19 11:12:06 -08001771TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
deadbeefcbc95072015-10-15 19:31:56 -07001772 DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773 ASSERT_TRUE(CreateTestClients());
1774 EnableVideoDecoderFactory();
1775 LocalP2PTest();
1776}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001777
deadbeeffac06552015-11-25 11:26:01 -08001778// This tests that if we negotiate after calling CreateSender but before we
1779// have a track, then set a track later, frames from the newly-set track are
1780// received end-to-end.
1781TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, EarlyWarmupTest) {
1782 ASSERT_TRUE(CreateTestClients());
1783 auto audio_sender = initializing_client()->pc()->CreateSender("audio");
1784 auto video_sender = initializing_client()->pc()->CreateSender("video");
1785 initializing_client()->Negotiate();
1786 // Wait for ICE connection to complete, without any tracks.
1787 // Note that the receiving client WILL (in HandleIncomingOffer) create
1788 // tracks, so it's only the initiator here that's doing early warmup.
1789 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1790 VerifySessionDescriptions();
1791 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1792 initializing_client()->ice_connection_state(),
1793 kMaxWaitForFramesMs);
1794 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1795 receiving_client()->ice_connection_state(),
1796 kMaxWaitForFramesMs);
1797 // Now set the tracks, and expect frames to immediately start flowing.
1798 EXPECT_TRUE(
1799 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
1800 EXPECT_TRUE(
1801 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
1802 EXPECT_TRUE_WAIT(FramesNotPending(kEndAudioFrameCount, kEndVideoFrameCount),
1803 kMaxWaitForFramesMs);
1804}
1805
deadbeef0a6c4ca2015-10-06 11:38:28 -07001806class IceServerParsingTest : public testing::Test {
1807 public:
1808 // Convenience for parsing a single URL.
1809 bool ParseUrl(const std::string& url) {
1810 return ParseUrl(url, std::string(), std::string());
1811 }
1812
1813 bool ParseUrl(const std::string& url,
1814 const std::string& username,
1815 const std::string& password) {
1816 PeerConnectionInterface::IceServers servers;
1817 PeerConnectionInterface::IceServer server;
1818 server.urls.push_back(url);
1819 server.username = username;
1820 server.password = password;
1821 servers.push_back(server);
1822 return webrtc::ParseIceServers(servers, &stun_configurations_,
1823 &turn_configurations_);
1824 }
1825
1826 protected:
1827 webrtc::StunConfigurations stun_configurations_;
1828 webrtc::TurnConfigurations turn_configurations_;
1829};
1830
1831// Make sure all STUN/TURN prefixes are parsed correctly.
1832TEST_F(IceServerParsingTest, ParseStunPrefixes) {
1833 EXPECT_TRUE(ParseUrl("stun:hostname"));
1834 EXPECT_EQ(1U, stun_configurations_.size());
1835 EXPECT_EQ(0U, turn_configurations_.size());
1836 stun_configurations_.clear();
1837
1838 EXPECT_TRUE(ParseUrl("stuns:hostname"));
1839 EXPECT_EQ(1U, stun_configurations_.size());
1840 EXPECT_EQ(0U, turn_configurations_.size());
1841 stun_configurations_.clear();
1842
1843 EXPECT_TRUE(ParseUrl("turn:hostname"));
1844 EXPECT_EQ(0U, stun_configurations_.size());
1845 EXPECT_EQ(1U, turn_configurations_.size());
1846 EXPECT_FALSE(turn_configurations_[0].secure);
1847 turn_configurations_.clear();
1848
1849 EXPECT_TRUE(ParseUrl("turns:hostname"));
1850 EXPECT_EQ(0U, stun_configurations_.size());
1851 EXPECT_EQ(1U, turn_configurations_.size());
1852 EXPECT_TRUE(turn_configurations_[0].secure);
1853 turn_configurations_.clear();
1854
1855 // invalid prefixes
1856 EXPECT_FALSE(ParseUrl("stunn:hostname"));
1857 EXPECT_FALSE(ParseUrl(":hostname"));
1858 EXPECT_FALSE(ParseUrl(":"));
1859 EXPECT_FALSE(ParseUrl(""));
1860}
1861
1862TEST_F(IceServerParsingTest, VerifyDefaults) {
1863 // TURNS defaults
1864 EXPECT_TRUE(ParseUrl("turns:hostname"));
1865 EXPECT_EQ(1U, turn_configurations_.size());
1866 EXPECT_EQ(5349, turn_configurations_[0].server.port());
1867 EXPECT_EQ("tcp", turn_configurations_[0].transport_type);
1868 turn_configurations_.clear();
1869
1870 // TURN defaults
1871 EXPECT_TRUE(ParseUrl("turn:hostname"));
1872 EXPECT_EQ(1U, turn_configurations_.size());
1873 EXPECT_EQ(3478, turn_configurations_[0].server.port());
1874 EXPECT_EQ("udp", turn_configurations_[0].transport_type);
1875 turn_configurations_.clear();
1876
1877 // STUN defaults
1878 EXPECT_TRUE(ParseUrl("stun:hostname"));
1879 EXPECT_EQ(1U, stun_configurations_.size());
1880 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1881 stun_configurations_.clear();
1882}
1883
1884// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
1885// can be parsed correctly.
1886TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
1887 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
1888 EXPECT_EQ(1U, stun_configurations_.size());
1889 EXPECT_EQ("1.2.3.4", stun_configurations_[0].server.hostname());
1890 EXPECT_EQ(1234, stun_configurations_[0].server.port());
1891 stun_configurations_.clear();
1892
1893 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
1894 EXPECT_EQ(1U, stun_configurations_.size());
1895 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_configurations_[0].server.hostname());
1896 EXPECT_EQ(4321, stun_configurations_[0].server.port());
1897 stun_configurations_.clear();
1898
1899 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
1900 EXPECT_EQ(1U, stun_configurations_.size());
1901 EXPECT_EQ("hostname", stun_configurations_[0].server.hostname());
1902 EXPECT_EQ(9999, stun_configurations_[0].server.port());
1903 stun_configurations_.clear();
1904
1905 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
1906 EXPECT_EQ(1U, stun_configurations_.size());
1907 EXPECT_EQ("1.2.3.4", stun_configurations_[0].server.hostname());
1908 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1909 stun_configurations_.clear();
1910
1911 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
1912 EXPECT_EQ(1U, stun_configurations_.size());
1913 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_configurations_[0].server.hostname());
1914 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1915 stun_configurations_.clear();
1916
1917 EXPECT_TRUE(ParseUrl("stun:hostname"));
1918 EXPECT_EQ(1U, stun_configurations_.size());
1919 EXPECT_EQ("hostname", stun_configurations_[0].server.hostname());
1920 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1921 stun_configurations_.clear();
1922
1923 // Try some invalid hostname:port strings.
1924 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
1925 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
1926 EXPECT_FALSE(ParseUrl("stun:hostname:"));
1927 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
1928 EXPECT_FALSE(ParseUrl("stun::5555"));
1929 EXPECT_FALSE(ParseUrl("stun:"));
1930}
1931
1932// Test parsing the "?transport=xxx" part of the URL.
1933TEST_F(IceServerParsingTest, ParseTransport) {
1934 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
1935 EXPECT_EQ(1U, turn_configurations_.size());
1936 EXPECT_EQ("tcp", turn_configurations_[0].transport_type);
1937 turn_configurations_.clear();
1938
1939 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
1940 EXPECT_EQ(1U, turn_configurations_.size());
1941 EXPECT_EQ("udp", turn_configurations_[0].transport_type);
1942 turn_configurations_.clear();
1943
1944 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
1945}
1946
1947// Test parsing ICE username contained in URL.
1948TEST_F(IceServerParsingTest, ParseUsername) {
1949 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
1950 EXPECT_EQ(1U, turn_configurations_.size());
1951 EXPECT_EQ("user", turn_configurations_[0].username);
1952 turn_configurations_.clear();
1953
1954 EXPECT_FALSE(ParseUrl("turn:@hostname"));
1955 EXPECT_FALSE(ParseUrl("turn:username@"));
1956 EXPECT_FALSE(ParseUrl("turn:@"));
1957 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
1958}
1959
1960// Test that username and password from IceServer is copied into the resulting
1961// TurnConfiguration.
1962TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
1963 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
1964 EXPECT_EQ(1U, turn_configurations_.size());
1965 EXPECT_EQ("username", turn_configurations_[0].username);
1966 EXPECT_EQ("password", turn_configurations_[0].password);
1967}
1968
1969// Ensure that if a server has multiple URLs, each one is parsed.
1970TEST_F(IceServerParsingTest, ParseMultipleUrls) {
1971 PeerConnectionInterface::IceServers servers;
1972 PeerConnectionInterface::IceServer server;
1973 server.urls.push_back("stun:hostname");
1974 server.urls.push_back("turn:hostname");
1975 servers.push_back(server);
1976 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_configurations_,
1977 &turn_configurations_));
1978 EXPECT_EQ(1U, stun_configurations_.size());
1979 EXPECT_EQ(1U, turn_configurations_.size());
1980}
1981
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001982#endif // if !defined(THREAD_SANITIZER)