blob: af18e1178615ede568ef2787f26e45491dbad24a [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 }
175
deadbeefaf1b59c2015-10-15 12:08:41 -0700176 void Negotiate() { Negotiate(true, true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177
deadbeefaf1b59c2015-10-15 12:08:41 -0700178 void Negotiate(bool audio, bool video) {
179 rtc::scoped_ptr<SessionDescriptionInterface> offer;
180 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181
deadbeefaf1b59c2015-10-15 12:08:41 -0700182 if (offer->description()->GetContentByName("audio")) {
183 offer->description()->GetContentByName("audio")->rejected = !audio;
184 }
185 if (offer->description()->GetContentByName("video")) {
186 offer->description()->GetContentByName("video")->rejected = !video;
187 }
188
189 std::string sdp;
190 EXPECT_TRUE(offer->ToString(&sdp));
191 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
192 signaling_message_receiver_->ReceiveSdpMessage(
193 webrtc::SessionDescriptionInterface::kOffer, sdp);
194 }
195
196 // SignalingMessageReceiver callback.
197 void ReceiveSdpMessage(const std::string& type, std::string& msg) override {
198 FilterIncomingSdpMessage(&msg);
199 if (type == webrtc::SessionDescriptionInterface::kOffer) {
200 HandleIncomingOffer(msg);
201 } else {
202 HandleIncomingAnswer(msg);
203 }
204 }
205
206 // SignalingMessageReceiver callback.
207 void ReceiveIceMessage(const std::string& sdp_mid,
208 int sdp_mline_index,
209 const std::string& msg) override {
210 LOG(INFO) << id_ << "ReceiveIceMessage";
211 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
212 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
213 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
214 }
215
216 // PeerConnectionObserver callbacks.
217 void OnSignalingChange(
218 webrtc::PeerConnectionInterface::SignalingState new_state) override {
219 EXPECT_EQ(pc()->signaling_state(), new_state);
220 }
deadbeeffaac4972015-11-12 15:33:07 -0800221 void OnAddStream(MediaStreamInterface* media_stream) override {
222 media_stream->RegisterObserver(this);
deadbeefaf1b59c2015-10-15 12:08:41 -0700223 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
224 const std::string id = media_stream->GetVideoTracks()[i]->id();
225 ASSERT_TRUE(fake_video_renderers_.find(id) ==
226 fake_video_renderers_.end());
deadbeefc9be0072015-12-14 18:27:57 -0800227 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
228 media_stream->GetVideoTracks()[i]));
deadbeefaf1b59c2015-10-15 12:08:41 -0700229 }
230 }
deadbeeffaac4972015-11-12 15:33:07 -0800231 void OnRemoveStream(MediaStreamInterface* media_stream) override {}
deadbeefaf1b59c2015-10-15 12:08:41 -0700232 void OnRenegotiationNeeded() override {}
233 void OnIceConnectionChange(
234 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
235 EXPECT_EQ(pc()->ice_connection_state(), new_state);
236 }
237 void OnIceGatheringChange(
238 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
239 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
240 }
241 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
242 LOG(INFO) << id_ << "OnIceCandidate";
243
244 std::string ice_sdp;
245 EXPECT_TRUE(candidate->ToString(&ice_sdp));
246 if (signaling_message_receiver_ == nullptr) {
247 // Remote party may be deleted.
248 return;
249 }
250 signaling_message_receiver_->ReceiveIceMessage(
251 candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
252 }
253
deadbeeffaac4972015-11-12 15:33:07 -0800254 // MediaStreamInterface callback
255 void OnChanged() override {
256 // Track added or removed from MediaStream, so update our renderers.
257 rtc::scoped_refptr<StreamCollectionInterface> remote_streams =
258 pc()->remote_streams();
259 // Remove renderers for tracks that were removed.
260 for (auto it = fake_video_renderers_.begin();
261 it != fake_video_renderers_.end();) {
262 if (remote_streams->FindVideoTrack(it->first) == nullptr) {
deadbeefc9be0072015-12-14 18:27:57 -0800263 auto to_remove = it++;
264 removed_fake_video_renderers_.push_back(std::move(to_remove->second));
265 fake_video_renderers_.erase(to_remove);
deadbeeffaac4972015-11-12 15:33:07 -0800266 } else {
267 ++it;
268 }
269 }
270 // Create renderers for new video tracks.
271 for (size_t stream_index = 0; stream_index < remote_streams->count();
272 ++stream_index) {
273 MediaStreamInterface* remote_stream = remote_streams->at(stream_index);
274 for (size_t track_index = 0;
275 track_index < remote_stream->GetVideoTracks().size();
276 ++track_index) {
277 const std::string id =
278 remote_stream->GetVideoTracks()[track_index]->id();
279 if (fake_video_renderers_.find(id) != fake_video_renderers_.end()) {
280 continue;
281 }
deadbeefc9be0072015-12-14 18:27:57 -0800282 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
283 remote_stream->GetVideoTracks()[track_index]));
deadbeeffaac4972015-11-12 15:33:07 -0800284 }
285 }
286 }
287
deadbeefaf1b59c2015-10-15 12:08:41 -0700288 void SetVideoConstraints(const webrtc::FakeConstraints& video_constraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289 video_constraints_ = video_constraint;
290 }
291
292 void AddMediaStream(bool audio, bool video) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700293 std::string stream_label =
294 kStreamLabelBase +
295 rtc::ToString<int>(static_cast<int>(pc()->local_streams()->count()));
deadbeeffaac4972015-11-12 15:33:07 -0800296 rtc::scoped_refptr<MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000297 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298
299 if (audio && can_receive_audio()) {
deadbeeffac06552015-11-25 11:26:01 -0800300 stream->AddTrack(CreateLocalAudioTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000301 }
302 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000303 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000304 }
305
deadbeefaf1b59c2015-10-15 12:08:41 -0700306 EXPECT_TRUE(pc()->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307 }
308
deadbeefaf1b59c2015-10-15 12:08:41 -0700309 size_t NumberOfLocalMediaStreams() { return pc()->local_streams()->count(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310
311 bool SessionActive() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700312 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313 }
314
deadbeeffaac4972015-11-12 15:33:07 -0800315 // Automatically add a stream when receiving an offer, if we don't have one.
316 // Defaults to true.
317 void set_auto_add_stream(bool auto_add_stream) {
318 auto_add_stream_ = auto_add_stream;
319 }
320
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321 void set_signaling_message_receiver(
deadbeefaf1b59c2015-10-15 12:08:41 -0700322 SignalingMessageReceiver* signaling_message_receiver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323 signaling_message_receiver_ = signaling_message_receiver;
324 }
325
326 void EnableVideoDecoderFactory() {
327 video_decoder_factory_enabled_ = true;
328 fake_video_decoder_factory_->AddSupportedVideoCodecType(
329 webrtc::kVideoCodecVP8);
330 }
331
deadbeefaf1b59c2015-10-15 12:08:41 -0700332 void IceRestart() {
333 session_description_constraints_.SetMandatoryIceRestart(true);
334 SetExpectIceRestart(true);
335 }
336
337 void SetExpectIceRestart(bool expect_restart) {
338 expect_ice_restart_ = expect_restart;
339 }
340
341 bool ExpectIceRestart() const { return expect_ice_restart_; }
342
343 void SetReceiveAudioVideo(bool audio, bool video) {
344 SetReceiveAudio(audio);
345 SetReceiveVideo(video);
346 ASSERT_EQ(audio, can_receive_audio());
347 ASSERT_EQ(video, can_receive_video());
348 }
349
350 void SetReceiveAudio(bool audio) {
351 if (audio && can_receive_audio())
352 return;
353 session_description_constraints_.SetMandatoryReceiveAudio(audio);
354 }
355
356 void SetReceiveVideo(bool video) {
357 if (video && can_receive_video())
358 return;
359 session_description_constraints_.SetMandatoryReceiveVideo(video);
360 }
361
362 void RemoveMsidFromReceivedSdp(bool remove) { remove_msid_ = remove; }
363
364 void RemoveSdesCryptoFromReceivedSdp(bool remove) { remove_sdes_ = remove; }
365
366 void RemoveBundleFromReceivedSdp(bool remove) { remove_bundle_ = remove; }
367
368 bool can_receive_audio() {
369 bool value;
370 if (webrtc::FindConstraint(&session_description_constraints_,
371 MediaConstraintsInterface::kOfferToReceiveAudio,
372 &value, nullptr)) {
373 return value;
374 }
375 return true;
376 }
377
378 bool can_receive_video() {
379 bool value;
380 if (webrtc::FindConstraint(&session_description_constraints_,
381 MediaConstraintsInterface::kOfferToReceiveVideo,
382 &value, nullptr)) {
383 return value;
384 }
385 return true;
386 }
387
388 void OnIceComplete() override { LOG(INFO) << id_ << "OnIceComplete"; }
389
390 void OnDataChannel(DataChannelInterface* data_channel) override {
391 LOG(INFO) << id_ << "OnDataChannel";
392 data_channel_ = data_channel;
393 data_observer_.reset(new MockDataChannelObserver(data_channel));
394 }
395
396 void CreateDataChannel() {
397 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, nullptr);
398 ASSERT_TRUE(data_channel_.get() != nullptr);
399 data_observer_.reset(new MockDataChannelObserver(data_channel_));
400 }
401
deadbeeffac06552015-11-25 11:26:01 -0800402 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack(
403 const std::string& stream_label) {
404 FakeConstraints constraints;
405 // Disable highpass filter so that we can get all the test audio frames.
406 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
407 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
408 peer_connection_factory_->CreateAudioSource(&constraints);
409 // TODO(perkj): Test audio source when it is implemented. Currently audio
410 // always use the default input.
411 std::string label = stream_label + kAudioTrackLabelBase;
412 return peer_connection_factory_->CreateAudioTrack(label, source);
413 }
414
415 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack(
416 const std::string& stream_label) {
417 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
418 FakeConstraints source_constraints = video_constraints_;
419 source_constraints.SetMandatoryMaxFrameRate(10);
420
421 cricket::FakeVideoCapturer* fake_capturer =
422 new webrtc::FakePeriodicVideoCapturer();
423 video_capturers_.push_back(fake_capturer);
424 rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
425 peer_connection_factory_->CreateVideoSource(fake_capturer,
426 &source_constraints);
427 std::string label = stream_label + kVideoTrackLabelBase;
428 return peer_connection_factory_->CreateVideoTrack(label, source);
429 }
430
deadbeefaf1b59c2015-10-15 12:08:41 -0700431 DataChannelInterface* data_channel() { return data_channel_; }
432 const MockDataChannelObserver* data_observer() const {
433 return data_observer_.get();
434 }
435
436 webrtc::PeerConnectionInterface* pc() { return peer_connection_.get(); }
437
438 void StopVideoCapturers() {
439 for (std::vector<cricket::VideoCapturer*>::iterator it =
440 video_capturers_.begin();
441 it != video_capturers_.end(); ++it) {
442 (*it)->Stop();
443 }
444 }
445
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446 bool AudioFramesReceivedCheck(int number_of_frames) const {
447 return number_of_frames <= fake_audio_capture_module_->frames_received();
448 }
449
deadbeefc9be0072015-12-14 18:27:57 -0800450 int audio_frames_received() const {
451 return fake_audio_capture_module_->frames_received();
452 }
453
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454 bool VideoFramesReceivedCheck(int number_of_frames) {
455 if (video_decoder_factory_enabled_) {
456 const std::vector<FakeWebRtcVideoDecoder*>& decoders
457 = fake_video_decoder_factory_->decoders();
458 if (decoders.empty()) {
459 return number_of_frames <= 0;
460 }
461
deadbeefc9be0072015-12-14 18:27:57 -0800462 for (FakeWebRtcVideoDecoder* decoder : decoders) {
463 if (number_of_frames > decoder->GetNumFramesReceived()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464 return false;
465 }
466 }
467 return true;
468 } else {
469 if (fake_video_renderers_.empty()) {
470 return number_of_frames <= 0;
471 }
472
deadbeefc9be0072015-12-14 18:27:57 -0800473 for (const auto& pair : fake_video_renderers_) {
474 if (number_of_frames > pair.second->num_rendered_frames()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000475 return false;
476 }
477 }
478 return true;
479 }
480 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700481
deadbeefc9be0072015-12-14 18:27:57 -0800482 int video_frames_received() const {
483 int total = 0;
484 if (video_decoder_factory_enabled_) {
485 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
486 fake_video_decoder_factory_->decoders();
487 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
488 total += decoder->GetNumFramesReceived();
489 }
490 } else {
491 for (const auto& pair : fake_video_renderers_) {
492 total += pair.second->num_rendered_frames();
493 }
494 for (const auto& renderer : removed_fake_video_renderers_) {
495 total += renderer->num_rendered_frames();
496 }
497 }
498 return total;
499 }
500
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 // Verify the CreateDtmfSender interface
502 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000503 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
504 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505
506 // We can't create a DTMF sender with an invalid audio track or a non local
507 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700508 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000509 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700510 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
511 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512
513 // We should be able to create a DTMF sender from a local track.
514 webrtc::AudioTrackInterface* localtrack =
515 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
516 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700517 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518 dtmf_sender->RegisterObserver(observer.get());
519
520 // Test the DtmfSender object just created.
521 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
522 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
523
524 // We don't need to verify that the DTMF tones are actually sent out because
525 // that is already covered by the tests of the lower level components.
526
527 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
528 std::vector<std::string> tones;
529 tones.push_back("1");
530 tones.push_back("a");
531 tones.push_back("");
532 observer->Verify(tones);
533
534 dtmf_sender->UnregisterObserver();
535 }
536
537 // Verifies that the SessionDescription have rejected the appropriate media
538 // content.
539 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700540 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
541 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542 const cricket::SessionDescription* remote_desc =
543 peer_connection_->remote_description()->description();
544 const cricket::SessionDescription* local_desc =
545 peer_connection_->local_description()->description();
546
547 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
548 if (remote_audio_content) {
549 const ContentInfo* audio_content =
550 GetFirstAudioContent(local_desc);
551 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
552 }
553
554 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
555 if (remote_video_content) {
556 const ContentInfo* video_content =
557 GetFirstVideoContent(local_desc);
558 EXPECT_EQ(can_receive_video(), !video_content->rejected);
559 }
560 }
561
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700563 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564 const cricket::SessionDescription* desc =
565 peer_connection_->local_description()->description();
566 const cricket::ContentInfos& contents = desc->contents();
567
568 for (size_t index = 0; index < contents.size(); ++index) {
569 if (contents[index].rejected)
570 continue;
571 const cricket::TransportDescription* transport_desc =
572 desc->GetTransportDescriptionByName(contents[index].name);
573
574 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000575 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576 if (ufragpair_it == ice_ufrag_pwd_.end()) {
577 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000578 ice_ufrag_pwd_[static_cast<int>(index)] =
579 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580 } else if (ExpectIceRestart()) {
581 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
582 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
583 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
584 } else {
585 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
586 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
587 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
588 }
589 }
590 }
591
592 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000593 rtc::scoped_refptr<MockStatsObserver>
594 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000595 EXPECT_TRUE(peer_connection_->GetStats(
596 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700598 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599 return observer->AudioOutputLevel();
600 }
601
602 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000603 rtc::scoped_refptr<MockStatsObserver>
604 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000605 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700606 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700608 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 return observer->AudioInputLevel();
610 }
611
612 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000613 rtc::scoped_refptr<MockStatsObserver>
614 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000615 EXPECT_TRUE(peer_connection_->GetStats(
616 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000617 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700618 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619 return observer->BytesReceived();
620 }
621
622 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000623 rtc::scoped_refptr<MockStatsObserver>
624 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000625 EXPECT_TRUE(peer_connection_->GetStats(
626 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700628 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 return observer->BytesSent();
630 }
631
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000632 int GetAvailableReceivedBandwidthStats() {
633 rtc::scoped_refptr<MockStatsObserver>
634 observer(new rtc::RefCountedObject<MockStatsObserver>());
635 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700636 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000637 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700638 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000639 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000640 return bw;
641 }
642
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000643 std::string GetDtlsCipherStats() {
644 rtc::scoped_refptr<MockStatsObserver>
645 observer(new rtc::RefCountedObject<MockStatsObserver>());
646 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700647 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000648 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700649 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000650 return observer->DtlsCipher();
651 }
652
653 std::string GetSrtpCipherStats() {
654 rtc::scoped_refptr<MockStatsObserver>
655 observer(new rtc::RefCountedObject<MockStatsObserver>());
656 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700657 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000658 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700659 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000660 return observer->SrtpCipher();
661 }
662
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000663 int rendered_width() {
664 EXPECT_FALSE(fake_video_renderers_.empty());
665 return fake_video_renderers_.empty() ? 1 :
666 fake_video_renderers_.begin()->second->width();
667 }
668
669 int rendered_height() {
670 EXPECT_FALSE(fake_video_renderers_.empty());
671 return fake_video_renderers_.empty() ? 1 :
672 fake_video_renderers_.begin()->second->height();
673 }
674
675 size_t number_of_remote_streams() {
676 if (!pc())
677 return 0;
678 return pc()->remote_streams()->count();
679 }
680
681 StreamCollectionInterface* remote_streams() {
682 if (!pc()) {
683 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700684 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 }
686 return pc()->remote_streams();
687 }
688
689 StreamCollectionInterface* local_streams() {
690 if (!pc()) {
691 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700692 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693 }
694 return pc()->local_streams();
695 }
696
697 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
698 return pc()->signaling_state();
699 }
700
701 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
702 return pc()->ice_connection_state();
703 }
704
705 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
706 return pc()->ice_gathering_state();
707 }
708
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 private:
710 class DummyDtmfObserver : public DtmfSenderObserverInterface {
711 public:
712 DummyDtmfObserver() : completed_(false) {}
713
714 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700715 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000716 tones_.push_back(tone);
717 if (tone.empty()) {
718 completed_ = true;
719 }
720 }
721
722 void Verify(const std::vector<std::string>& tones) const {
723 ASSERT_TRUE(tones_.size() == tones.size());
724 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
725 }
726
727 bool completed() const { return completed_; }
728
729 private:
730 bool completed_;
731 std::vector<std::string> tones_;
732 };
733
deadbeefaf1b59c2015-10-15 12:08:41 -0700734 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
735
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800736 bool Init(
737 const MediaConstraintsInterface* constraints,
738 const PeerConnectionFactory::Options* options,
739 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700740 EXPECT_TRUE(!peer_connection_);
741 EXPECT_TRUE(!peer_connection_factory_);
742 allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
743 if (!allocator_factory_) {
744 return false;
745 }
746 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
747
748 if (fake_audio_capture_module_ == nullptr) {
749 return false;
750 }
751 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
752 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
753 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
754 rtc::Thread::Current(), rtc::Thread::Current(),
755 fake_audio_capture_module_, fake_video_encoder_factory_,
756 fake_video_decoder_factory_);
757 if (!peer_connection_factory_) {
758 return false;
759 }
760 if (options) {
761 peer_connection_factory_->SetOptions(*options);
762 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800763 peer_connection_ = CreatePeerConnection(
764 allocator_factory_.get(), constraints, dtls_identity_store.Pass());
deadbeefaf1b59c2015-10-15 12:08:41 -0700765 return peer_connection_.get() != nullptr;
766 }
767
deadbeefaf1b59c2015-10-15 12:08:41 -0700768 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
769 webrtc::PortAllocatorFactoryInterface* factory,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800770 const MediaConstraintsInterface* constraints,
771 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772 // CreatePeerConnection with IceServers.
773 webrtc::PeerConnectionInterface::IceServers ice_servers;
774 webrtc::PeerConnectionInterface::IceServer ice_server;
775 ice_server.uri = "stun:stun.l.google.com:19302";
776 ice_servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000777
deadbeefaf1b59c2015-10-15 12:08:41 -0700778 return peer_connection_factory_->CreatePeerConnection(
Henrik Boström5e56c592015-08-11 10:33:13 +0200779 ice_servers, constraints, factory, dtls_identity_store.Pass(), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 }
781
782 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700783 LOG(INFO) << id_ << "HandleIncomingOffer ";
deadbeeffaac4972015-11-12 15:33:07 -0800784 if (NumberOfLocalMediaStreams() == 0 && auto_add_stream_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 // If we are not sending any streams ourselves it is time to add some.
786 AddMediaStream(true, true);
787 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000788 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700789 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000791 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000792 EXPECT_TRUE(DoCreateAnswer(answer.use()));
793 std::string sdp;
794 EXPECT_TRUE(answer->ToString(&sdp));
795 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
deadbeefaf1b59c2015-10-15 12:08:41 -0700796 if (signaling_message_receiver_) {
797 signaling_message_receiver_->ReceiveSdpMessage(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 webrtc::SessionDescriptionInterface::kAnswer, sdp);
799 }
800 }
801
802 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700803 LOG(INFO) << id_ << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000804 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700805 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
807 }
808
809 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
810 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000811 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
812 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 MockCreateSessionDescriptionObserver>());
814 if (offer) {
815 pc()->CreateOffer(observer, &session_description_constraints_);
816 } else {
817 pc()->CreateAnswer(observer, &session_description_constraints_);
818 }
819 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
820 *desc = observer->release_desc();
821 if (observer->result() && ExpectIceRestart()) {
822 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
823 }
824 return observer->result();
825 }
826
827 bool DoCreateOffer(SessionDescriptionInterface** desc) {
828 return DoCreateOfferAnswer(desc, true);
829 }
830
831 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
832 return DoCreateOfferAnswer(desc, false);
833 }
834
835 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000836 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
837 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700839 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 pc()->SetLocalDescription(observer, desc);
841 // Ignore the observer result. If we wait for the result with
842 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
843 // before the offer which is an error.
844 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000845 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 // ProcessMessages waits at least 1ms but processes all messages before
847 // returning. Since this test is synchronous and send messages to the remote
848 // peer whenever a callback is invoked, this can lead to messages being
849 // sent to the remote peer in the wrong order.
850 // TODO(perkj): Find a way to check the result without risking that the
851 // order of sent messages are changed. Ex- by posting all messages that are
852 // sent to the remote peer.
853 return true;
854 }
855
856 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000857 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
858 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700860 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 pc()->SetRemoteDescription(observer, desc);
862 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
863 return observer->result();
864 }
865
866 // This modifies all received SDP messages before they are processed.
867 void FilterIncomingSdpMessage(std::string* sdp) {
868 if (remove_msid_) {
869 const char kSdpSsrcAttribute[] = "a=ssrc:";
870 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
871 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
872 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
873 }
874 if (remove_bundle_) {
875 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
876 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
877 }
878 if (remove_sdes_) {
879 const char kSdpSdesCryptoAttribute[] = "a=crypto";
880 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
881 }
882 }
883
deadbeefaf1b59c2015-10-15 12:08:41 -0700884 std::string id_;
885
886 rtc::scoped_refptr<webrtc::PortAllocatorFactoryInterface> allocator_factory_;
887 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
888 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
889 peer_connection_factory_;
890
deadbeeffaac4972015-11-12 15:33:07 -0800891 bool auto_add_stream_ = true;
892
deadbeefaf1b59c2015-10-15 12:08:41 -0700893 typedef std::pair<std::string, std::string> IceUfragPwdPair;
894 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
895 bool expect_ice_restart_ = false;
896
deadbeefc9be0072015-12-14 18:27:57 -0800897 // Needed to keep track of number of frames sent.
deadbeefaf1b59c2015-10-15 12:08:41 -0700898 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
899 // Needed to keep track of number of frames received.
deadbeefc9be0072015-12-14 18:27:57 -0800900 std::map<std::string, rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
901 fake_video_renderers_;
902 // Needed to ensure frames aren't received for removed tracks.
903 std::vector<rtc::scoped_ptr<webrtc::FakeVideoTrackRenderer>>
904 removed_fake_video_renderers_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700905 // Needed to keep track of number of frames received when external decoder
906 // used.
907 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
908 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
909 bool video_decoder_factory_enabled_ = false;
910 webrtc::FakeConstraints video_constraints_;
911
912 // For remote peer communication.
913 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
914
915 // Store references to the video capturers we've created, so that we can stop
916 // them, if required.
917 std::vector<cricket::VideoCapturer*> video_capturers_;
918
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 webrtc::FakeConstraints session_description_constraints_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700920 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
921 bool remove_bundle_ =
922 false; // True if bundle should be removed in received SDP.
923 bool remove_sdes_ =
924 false; // True if a=crypto should be removed in received SDP.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000926 rtc::scoped_refptr<DataChannelInterface> data_channel_;
927 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928};
929
deadbeef7c73bdb2015-12-10 15:10:44 -0800930class P2PTestConductor : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000931 public:
deadbeef7c73bdb2015-12-10 15:10:44 -0800932 P2PTestConductor()
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000933 : pss_(new rtc::PhysicalSocketServer),
934 ss_(new rtc::VirtualSocketServer(pss_.get())),
935 ss_scope_(ss_.get()) {}
936
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937 bool SessionActive() {
938 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000939 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000941
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942 // Return true if the number of frames provided have been received or it is
943 // known that that will never occur (e.g. no frames will be sent or
944 // captured).
945 bool FramesNotPending(int audio_frames_to_receive,
946 int video_frames_to_receive) {
947 return VideoFramesReceivedCheck(video_frames_to_receive) &&
948 AudioFramesReceivedCheck(audio_frames_to_receive);
949 }
950 bool AudioFramesReceivedCheck(int frames_received) {
951 return initiating_client_->AudioFramesReceivedCheck(frames_received) &&
952 receiving_client_->AudioFramesReceivedCheck(frames_received);
953 }
954 bool VideoFramesReceivedCheck(int frames_received) {
955 return initiating_client_->VideoFramesReceivedCheck(frames_received) &&
956 receiving_client_->VideoFramesReceivedCheck(frames_received);
957 }
958 void VerifyDtmf() {
959 initiating_client_->VerifyDtmf();
960 receiving_client_->VerifyDtmf();
961 }
962
963 void TestUpdateOfferWithRejectedContent() {
deadbeefc9be0072015-12-14 18:27:57 -0800964 // Renegotiate, rejecting the video m-line.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965 initiating_client_->Negotiate(true, false);
deadbeefc9be0072015-12-14 18:27:57 -0800966 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
967
968 int pc1_audio_received = initiating_client_->audio_frames_received();
969 int pc1_video_received = initiating_client_->video_frames_received();
970 int pc2_audio_received = receiving_client_->audio_frames_received();
971 int pc2_video_received = receiving_client_->video_frames_received();
972
973 // Wait for some additional audio frames to be received.
974 EXPECT_TRUE_WAIT(initiating_client_->AudioFramesReceivedCheck(
975 pc1_audio_received + kEndAudioFrameCount) &&
976 receiving_client_->AudioFramesReceivedCheck(
977 pc2_audio_received + kEndAudioFrameCount),
978 kMaxWaitForFramesMs);
979
980 // During this time, we shouldn't have received any additional video frames
981 // for the rejected video tracks.
982 EXPECT_EQ(pc1_video_received, initiating_client_->video_frames_received());
983 EXPECT_EQ(pc2_video_received, receiving_client_->video_frames_received());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984 }
985
986 void VerifyRenderedSize(int width, int height) {
987 EXPECT_EQ(width, receiving_client()->rendered_width());
988 EXPECT_EQ(height, receiving_client()->rendered_height());
989 EXPECT_EQ(width, initializing_client()->rendered_width());
990 EXPECT_EQ(height, initializing_client()->rendered_height());
991 }
992
993 void VerifySessionDescriptions() {
994 initiating_client_->VerifyRejectedMediaInSessionDescription();
995 receiving_client_->VerifyRejectedMediaInSessionDescription();
996 initiating_client_->VerifyLocalIceUfragAndPassword();
997 receiving_client_->VerifyLocalIceUfragAndPassword();
998 }
999
deadbeef7c73bdb2015-12-10 15:10:44 -08001000 ~P2PTestConductor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001002 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003 }
1004 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001005 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001007 }
1008
deadbeefaf1b59c2015-10-15 12:08:41 -07001009 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001010
1011 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1012 MediaConstraintsInterface* recv_constraints) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001013 return CreateTestClients(init_constraints, nullptr, recv_constraints,
1014 nullptr);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001015 }
1016
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001017 void SetSignalingReceivers() {
1018 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
1019 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1020 }
1021
Joachim Bauch04e5b492015-05-29 09:40:39 +02001022 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1023 PeerConnectionFactory::Options* init_options,
1024 MediaConstraintsInterface* recv_constraints,
1025 PeerConnectionFactory::Options* recv_options) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001026 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
1027 "Caller: ", init_constraints, init_options));
1028 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
1029 "Callee: ", recv_constraints, recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001030 if (!initiating_client_ || !receiving_client_) {
1031 return false;
1032 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001033 SetSignalingReceivers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034 return true;
1035 }
1036
1037 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1038 const webrtc::FakeConstraints& recv_constraints) {
1039 initiating_client_->SetVideoConstraints(init_constraints);
1040 receiving_client_->SetVideoConstraints(recv_constraints);
1041 }
1042
1043 void EnableVideoDecoderFactory() {
1044 initiating_client_->EnableVideoDecoderFactory();
1045 receiving_client_->EnableVideoDecoderFactory();
1046 }
1047
1048 // This test sets up a call between two parties. Both parties send static
1049 // frames to each other. Once the test is finished the number of sent frames
1050 // is compared to the number of received frames.
1051 void LocalP2PTest() {
1052 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1053 initiating_client_->AddMediaStream(true, true);
1054 }
1055 initiating_client_->Negotiate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 // Assert true is used here since next tests are guaranteed to fail and
1057 // would eat up 5 seconds.
1058 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1059 VerifySessionDescriptions();
1060
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001061 int audio_frame_count = kEndAudioFrameCount;
1062 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1063 if (!initiating_client_->can_receive_audio() ||
1064 !receiving_client_->can_receive_audio()) {
1065 audio_frame_count = -1;
1066 }
1067 int video_frame_count = kEndVideoFrameCount;
1068 if (!initiating_client_->can_receive_video() ||
1069 !receiving_client_->can_receive_video()) {
1070 video_frame_count = -1;
1071 }
1072
1073 if (audio_frame_count != -1 || video_frame_count != -1) {
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001074 // Audio or video is expected to flow, so both clients should reach the
1075 // Connected state, and the offerer (ICE controller) should proceed to
1076 // Completed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001077 // Note: These tests have been observed to fail under heavy load at
1078 // shorter timeouts, so they may be flaky.
1079 EXPECT_EQ_WAIT(
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001080 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001081 initiating_client_->ice_connection_state(),
1082 kMaxWaitForFramesMs);
1083 EXPECT_EQ_WAIT(
1084 webrtc::PeerConnectionInterface::kIceConnectionConnected,
1085 receiving_client_->ice_connection_state(),
1086 kMaxWaitForFramesMs);
1087 }
1088
1089 if (initiating_client_->can_receive_audio() ||
1090 initiating_client_->can_receive_video()) {
1091 // The initiating client can receive media, so it must produce candidates
1092 // that will serve as destinations for that media.
1093 // TODO(bemasc): Understand why the state is not already Complete here, as
1094 // seems to be the case for the receiving client. This may indicate a bug
1095 // in the ICE gathering system.
1096 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1097 initiating_client_->ice_gathering_state());
1098 }
1099 if (receiving_client_->can_receive_audio() ||
1100 receiving_client_->can_receive_video()) {
1101 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1102 receiving_client_->ice_gathering_state(),
1103 kMaxWaitForFramesMs);
1104 }
1105
1106 EXPECT_TRUE_WAIT(FramesNotPending(audio_frame_count, video_frame_count),
1107 kMaxWaitForFramesMs);
1108 }
1109
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001110 void SetupAndVerifyDtlsCall() {
1111 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1112 FakeConstraints setup_constraints;
1113 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1114 true);
1115 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1116 LocalP2PTest();
1117 VerifyRenderedSize(640, 480);
1118 }
1119
1120 PeerConnectionTestClient* CreateDtlsClientWithAlternateKey() {
1121 FakeConstraints setup_constraints;
1122 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1123 true);
1124
1125 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
1126 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
1127 : nullptr);
1128 dtls_identity_store->use_alternate_key();
1129
1130 // Make sure the new client is using a different certificate.
1131 return PeerConnectionTestClient::CreateClientWithDtlsIdentityStore(
1132 "New Peer: ", &setup_constraints, nullptr, dtls_identity_store.Pass());
1133 }
1134
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001135 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1136 // Messages may get lost on the unreliable DataChannel, so we send multiple
1137 // times to avoid test flakiness.
1138 static const size_t kSendAttempts = 5;
1139
1140 for (size_t i = 0; i < kSendAttempts; ++i) {
1141 dc->Send(DataBuffer(data));
1142 }
1143 }
1144
deadbeefaf1b59c2015-10-15 12:08:41 -07001145 PeerConnectionTestClient* initializing_client() {
1146 return initiating_client_.get();
1147 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001148
1149 // Set the |initiating_client_| to the |client| passed in and return the
1150 // original |initiating_client_|.
1151 PeerConnectionTestClient* set_initializing_client(
1152 PeerConnectionTestClient* client) {
1153 PeerConnectionTestClient* old = initiating_client_.release();
1154 initiating_client_.reset(client);
1155 return old;
1156 }
1157
deadbeefaf1b59c2015-10-15 12:08:41 -07001158 PeerConnectionTestClient* receiving_client() {
1159 return receiving_client_.get();
1160 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001162 // Set the |receiving_client_| to the |client| passed in and return the
1163 // original |receiving_client_|.
1164 PeerConnectionTestClient* set_receiving_client(
1165 PeerConnectionTestClient* client) {
1166 PeerConnectionTestClient* old = receiving_client_.release();
1167 receiving_client_.reset(client);
1168 return old;
1169 }
1170
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001171 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001172 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1173 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1174 rtc::SocketServerScope ss_scope_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001175 rtc::scoped_ptr<PeerConnectionTestClient> initiating_client_;
1176 rtc::scoped_ptr<PeerConnectionTestClient> receiving_client_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001178
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001179// Disable for TSan v2, see
1180// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1181#if !defined(THREAD_SANITIZER)
1182
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001184// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1185// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001186TEST_F(P2PTestConductor, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187 ASSERT_TRUE(CreateTestClients());
1188 LocalP2PTest();
1189 VerifyDtmf();
1190}
1191
1192// This test sets up a Jsep call between two parties and test that we can get a
1193// video aspect ratio of 16:9.
deadbeef7c73bdb2015-12-10 15:10:44 -08001194TEST_F(P2PTestConductor, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195 ASSERT_TRUE(CreateTestClients());
1196 FakeConstraints constraint;
1197 double requested_ratio = 640.0/360;
1198 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1199 SetVideoConstraints(constraint, constraint);
1200 LocalP2PTest();
1201
1202 ASSERT_LE(0, initializing_client()->rendered_height());
1203 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001204 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205 initializing_client()->rendered_height();
1206 EXPECT_LE(requested_ratio, initiating_video_ratio);
1207
1208 ASSERT_LE(0, receiving_client()->rendered_height());
1209 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001210 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211 receiving_client()->rendered_height();
1212 EXPECT_LE(requested_ratio, receiving_video_ratio);
1213}
1214
1215// This test sets up a Jsep call between two parties and test that the
1216// received video has a resolution of 1280*720.
1217// TODO(mallinath): Enable when
1218// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
deadbeef7c73bdb2015-12-10 15:10:44 -08001219TEST_F(P2PTestConductor, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220 ASSERT_TRUE(CreateTestClients());
1221 FakeConstraints constraint;
1222 constraint.SetMandatoryMinWidth(1280);
1223 constraint.SetMandatoryMinHeight(720);
1224 SetVideoConstraints(constraint, constraint);
1225 LocalP2PTest();
1226 VerifyRenderedSize(1280, 720);
1227}
1228
1229// This test sets up a call between two endpoints that are configured to use
1230// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001231TEST_F(P2PTestConductor, LocalP2PTestDtls) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001232 SetupAndVerifyDtlsCall();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001233}
1234
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001235// This test sets up a audio call initially and then upgrades to audio/video,
1236// using DTLS.
deadbeef7c73bdb2015-12-10 15:10:44 -08001237TEST_F(P2PTestConductor, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001238 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001239 FakeConstraints setup_constraints;
1240 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1241 true);
1242 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1243 receiving_client()->SetReceiveAudioVideo(true, false);
1244 LocalP2PTest();
1245 receiving_client()->SetReceiveAudioVideo(true, true);
1246 receiving_client()->Negotiate();
1247}
1248
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001249// This test sets up a call transfer to a new caller with a different DTLS
1250// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001251TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCallee) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001252 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1253 SetupAndVerifyDtlsCall();
1254
1255 // Keeping the original peer around which will still send packets to the
1256 // receiving client. These SRTP packets will be dropped.
1257 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1258 set_initializing_client(CreateDtlsClientWithAlternateKey()));
1259 original_peer->pc()->Close();
1260
1261 SetSignalingReceivers();
1262 receiving_client()->SetExpectIceRestart(true);
1263 LocalP2PTest();
1264 VerifyRenderedSize(640, 480);
1265}
1266
1267// This test sets up a call transfer to a new callee with a different DTLS
1268// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001269TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCaller) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001270 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1271 SetupAndVerifyDtlsCall();
1272
1273 // Keeping the original peer around which will still send packets to the
1274 // receiving client. These SRTP packets will be dropped.
1275 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1276 set_receiving_client(CreateDtlsClientWithAlternateKey()));
1277 original_peer->pc()->Close();
1278
1279 SetSignalingReceivers();
1280 initializing_client()->IceRestart();
1281 LocalP2PTest();
1282 VerifyRenderedSize(640, 480);
1283}
1284
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001285// This test sets up a call between two endpoints that are configured to use
1286// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1287// negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001288TEST_F(P2PTestConductor, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001289 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001290 FakeConstraints setup_constraints;
1291 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1292 true);
1293 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1294 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1295 LocalP2PTest();
1296 VerifyRenderedSize(640, 480);
1297}
1298
1299// This test sets up a Jsep call between two parties, and the callee only
1300// accept to receive video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001301TEST_F(P2PTestConductor, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001302 ASSERT_TRUE(CreateTestClients());
1303 receiving_client()->SetReceiveAudioVideo(false, true);
1304 LocalP2PTest();
1305}
1306
1307// This test sets up a Jsep call between two parties, and the callee only
1308// accept to receive audio.
deadbeef7c73bdb2015-12-10 15:10:44 -08001309TEST_F(P2PTestConductor, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310 ASSERT_TRUE(CreateTestClients());
1311 receiving_client()->SetReceiveAudioVideo(true, false);
1312 LocalP2PTest();
1313}
1314
1315// This test sets up a Jsep call between two parties, and the callee reject both
1316// audio and video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001317TEST_F(P2PTestConductor, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001318 ASSERT_TRUE(CreateTestClients());
1319 receiving_client()->SetReceiveAudioVideo(false, false);
1320 LocalP2PTest();
1321}
1322
1323// This test sets up an audio and video call between two parties. After the call
1324// runs for a while (10 frames), the caller sends an update offer with video
1325// being rejected. Once the re-negotiation is done, the video flow should stop
1326// and the audio flow should continue.
deadbeefc9be0072015-12-14 18:27:57 -08001327TEST_F(P2PTestConductor, UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001328 ASSERT_TRUE(CreateTestClients());
1329 LocalP2PTest();
1330 TestUpdateOfferWithRejectedContent();
1331}
1332
1333// This test sets up a Jsep call between two parties. The MSID is removed from
1334// the SDP strings from the caller.
deadbeefc9be0072015-12-14 18:27:57 -08001335TEST_F(P2PTestConductor, LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001336 ASSERT_TRUE(CreateTestClients());
1337 receiving_client()->RemoveMsidFromReceivedSdp(true);
1338 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1339 // audio and video is muxed when MSID is disabled. Remove
1340 // SetRemoveBundleFromSdp once
1341 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1342 receiving_client()->RemoveBundleFromReceivedSdp(true);
1343 LocalP2PTest();
1344}
1345
1346// This test sets up a Jsep call between two parties and the initiating peer
1347// sends two steams.
1348// TODO(perkj): Disabled due to
1349// https://code.google.com/p/webrtc/issues/detail?id=1454
deadbeef7c73bdb2015-12-10 15:10:44 -08001350TEST_F(P2PTestConductor, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001351 ASSERT_TRUE(CreateTestClients());
1352 // Set optional video constraint to max 320pixels to decrease CPU usage.
1353 FakeConstraints constraint;
1354 constraint.SetOptionalMaxWidth(320);
1355 SetVideoConstraints(constraint, constraint);
1356 initializing_client()->AddMediaStream(true, true);
1357 initializing_client()->AddMediaStream(false, true);
1358 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1359 LocalP2PTest();
1360 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1361}
1362
1363// Test that we can receive the audio output level from a remote audio track.
deadbeef7c73bdb2015-12-10 15:10:44 -08001364TEST_F(P2PTestConductor, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001365 ASSERT_TRUE(CreateTestClients());
1366 LocalP2PTest();
1367
1368 StreamCollectionInterface* remote_streams =
1369 initializing_client()->remote_streams();
1370 ASSERT_GT(remote_streams->count(), 0u);
1371 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1372 MediaStreamTrackInterface* remote_audio_track =
1373 remote_streams->at(0)->GetAudioTracks()[0];
1374
1375 // Get the audio output level stats. Note that the level is not available
1376 // until a RTCP packet has been received.
1377 EXPECT_TRUE_WAIT(
1378 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1379 kMaxWaitForStatsMs);
1380}
1381
1382// Test that an audio input level is reported.
deadbeef7c73bdb2015-12-10 15:10:44 -08001383TEST_F(P2PTestConductor, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001384 ASSERT_TRUE(CreateTestClients());
1385 LocalP2PTest();
1386
1387 // Get the audio input level stats. The level should be available very
1388 // soon after the test starts.
1389 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1390 kMaxWaitForStatsMs);
1391}
1392
1393// Test that we can get incoming byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001394TEST_F(P2PTestConductor, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001395 ASSERT_TRUE(CreateTestClients());
1396 LocalP2PTest();
1397
1398 StreamCollectionInterface* remote_streams =
1399 initializing_client()->remote_streams();
1400 ASSERT_GT(remote_streams->count(), 0u);
1401 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1402 MediaStreamTrackInterface* remote_audio_track =
1403 remote_streams->at(0)->GetAudioTracks()[0];
1404 EXPECT_TRUE_WAIT(
1405 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1406 kMaxWaitForStatsMs);
1407
1408 MediaStreamTrackInterface* remote_video_track =
1409 remote_streams->at(0)->GetVideoTracks()[0];
1410 EXPECT_TRUE_WAIT(
1411 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1412 kMaxWaitForStatsMs);
1413}
1414
1415// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001416TEST_F(P2PTestConductor, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001417 ASSERT_TRUE(CreateTestClients());
1418 LocalP2PTest();
1419
1420 StreamCollectionInterface* local_streams =
1421 initializing_client()->local_streams();
1422 ASSERT_GT(local_streams->count(), 0u);
1423 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1424 MediaStreamTrackInterface* local_audio_track =
1425 local_streams->at(0)->GetAudioTracks()[0];
1426 EXPECT_TRUE_WAIT(
1427 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1428 kMaxWaitForStatsMs);
1429
1430 MediaStreamTrackInterface* local_video_track =
1431 local_streams->at(0)->GetVideoTracks()[0];
1432 EXPECT_TRUE_WAIT(
1433 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1434 kMaxWaitForStatsMs);
1435}
1436
Joachim Bauch04e5b492015-05-29 09:40:39 +02001437// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
deadbeef7c73bdb2015-12-10 15:10:44 -08001438TEST_F(P2PTestConductor, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001439 PeerConnectionFactory::Options init_options;
1440 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1441 PeerConnectionFactory::Options recv_options;
1442 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001443 ASSERT_TRUE(
1444 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001445 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1446 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1447 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001448 LocalP2PTest();
1449
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001450 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001451 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1452 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1453 initializing_client()->GetDtlsCipherStats(),
1454 kMaxWaitForStatsMs);
1455 EXPECT_EQ(1, init_observer->GetEnumCounter(
1456 webrtc::kEnumCounterAudioSslCipher,
1457 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1458 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001459
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001460 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001461 initializing_client()->GetSrtpCipherStats(),
1462 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001463 EXPECT_EQ(1,
1464 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1465 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001466}
1467
1468// Test that DTLS 1.2 is used if both ends support it.
deadbeef7c73bdb2015-12-10 15:10:44 -08001469TEST_F(P2PTestConductor, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001470 PeerConnectionFactory::Options init_options;
1471 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1472 PeerConnectionFactory::Options recv_options;
1473 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001474 ASSERT_TRUE(
1475 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001476 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1477 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1478 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001479 LocalP2PTest();
1480
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001481 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001482 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1483 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)),
1484 initializing_client()->GetDtlsCipherStats(),
1485 kMaxWaitForStatsMs);
1486 EXPECT_EQ(1, init_observer->GetEnumCounter(
1487 webrtc::kEnumCounterAudioSslCipher,
1488 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1489 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001490
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001491 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001492 initializing_client()->GetSrtpCipherStats(),
1493 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001494 EXPECT_EQ(1,
1495 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1496 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001497}
1498
1499// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1500// received supports 1.0.
deadbeef7c73bdb2015-12-10 15:10:44 -08001501TEST_F(P2PTestConductor, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001502 PeerConnectionFactory::Options init_options;
1503 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1504 PeerConnectionFactory::Options recv_options;
1505 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001506 ASSERT_TRUE(
1507 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001508 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1509 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1510 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001511 LocalP2PTest();
1512
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001513 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001514 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1515 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1516 initializing_client()->GetDtlsCipherStats(),
1517 kMaxWaitForStatsMs);
1518 EXPECT_EQ(1, init_observer->GetEnumCounter(
1519 webrtc::kEnumCounterAudioSslCipher,
1520 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1521 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001522
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001523 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001524 initializing_client()->GetSrtpCipherStats(),
1525 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001526 EXPECT_EQ(1,
1527 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1528 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001529}
1530
1531// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1532// received supports 1.2.
deadbeef7c73bdb2015-12-10 15:10:44 -08001533TEST_F(P2PTestConductor, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001534 PeerConnectionFactory::Options init_options;
1535 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1536 PeerConnectionFactory::Options recv_options;
1537 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001538 ASSERT_TRUE(
1539 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001540 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1541 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1542 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001543 LocalP2PTest();
1544
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001545 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001546 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1547 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1548 initializing_client()->GetDtlsCipherStats(),
1549 kMaxWaitForStatsMs);
1550 EXPECT_EQ(1, init_observer->GetEnumCounter(
1551 webrtc::kEnumCounterAudioSslCipher,
1552 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1553 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001554
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001555 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001556 initializing_client()->GetSrtpCipherStats(),
1557 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001558 EXPECT_EQ(1,
1559 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1560 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001561}
1562
deadbeefb5cb19b2015-11-23 16:39:12 -08001563// This test sets up a call between two parties with audio, video and an RTP
1564// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001565TEST_F(P2PTestConductor, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566 FakeConstraints setup_constraints;
1567 setup_constraints.SetAllowRtpDataChannels();
1568 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1569 initializing_client()->CreateDataChannel();
1570 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001571 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1572 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001573 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1574 kMaxWaitMs);
1575 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1576 kMaxWaitMs);
1577
1578 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001579
1580 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1582 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001583
1584 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001585 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1586 kMaxWaitMs);
1587
1588 receiving_client()->data_channel()->Close();
1589 // Send new offer and answer.
1590 receiving_client()->Negotiate();
1591 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1592 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1593}
1594
deadbeefb5cb19b2015-11-23 16:39:12 -08001595// This test sets up a call between two parties with audio, video and an SCTP
1596// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001597TEST_F(P2PTestConductor, LocalP2PTestSctpDataChannel) {
deadbeefb5cb19b2015-11-23 16:39:12 -08001598 ASSERT_TRUE(CreateTestClients());
1599 initializing_client()->CreateDataChannel();
1600 LocalP2PTest();
1601 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1602 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
1603 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1604 kMaxWaitMs);
1605 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
1606
1607 std::string data = "hello world";
1608
1609 initializing_client()->data_channel()->Send(DataBuffer(data));
1610 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1611 kMaxWaitMs);
1612
1613 receiving_client()->data_channel()->Send(DataBuffer(data));
1614 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1615 kMaxWaitMs);
1616
1617 receiving_client()->data_channel()->Close();
1618 // Send new offer and answer.
1619 receiving_client()->Negotiate();
1620 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1621 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1622}
1623
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624// This test sets up a call between two parties and creates a data channel.
1625// The test tests that received data is buffered unless an observer has been
1626// registered.
1627// Rtp data channels can receive data before the underlying
1628// transport has detected that a channel is writable and thus data can be
1629// received before the data channel state changes to open. That is hard to test
1630// but the same buffering is used in that case.
deadbeef7c73bdb2015-12-10 15:10:44 -08001631TEST_F(P2PTestConductor, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632 FakeConstraints setup_constraints;
1633 setup_constraints.SetAllowRtpDataChannels();
1634 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1635 initializing_client()->CreateDataChannel();
1636 initializing_client()->Negotiate();
1637
deadbeefaf1b59c2015-10-15 12:08:41 -07001638 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1639 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1641 kMaxWaitMs);
1642 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1643 receiving_client()->data_channel()->state(), kMaxWaitMs);
1644
1645 // Unregister the existing observer.
1646 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001647
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001648 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001649 SendRtpData(initializing_client()->data_channel(), data);
1650
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001651 // Wait a while to allow the sent data to arrive before an observer is
1652 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001653 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654
1655 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1656 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1657}
1658
1659// This test sets up a call between two parties with audio, video and but only
1660// the initiating client support data.
deadbeef7c73bdb2015-12-10 15:10:44 -08001661TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001662 FakeConstraints setup_constraints_1;
1663 setup_constraints_1.SetAllowRtpDataChannels();
1664 // Must disable DTLS to make negotiation succeed.
1665 setup_constraints_1.SetMandatory(
1666 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1667 FakeConstraints setup_constraints_2;
1668 setup_constraints_2.SetMandatory(
1669 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1670 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671 initializing_client()->CreateDataChannel();
1672 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001673 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674 EXPECT_FALSE(receiving_client()->data_channel());
1675 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1676}
1677
1678// This test sets up a call between two parties with audio, video. When audio
1679// and video is setup and flowing and data channel is negotiated.
deadbeef7c73bdb2015-12-10 15:10:44 -08001680TEST_F(P2PTestConductor, AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001681 FakeConstraints setup_constraints;
1682 setup_constraints.SetAllowRtpDataChannels();
1683 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1684 LocalP2PTest();
1685 initializing_client()->CreateDataChannel();
1686 // Send new offer and answer.
1687 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07001688 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1689 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001690 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1691 kMaxWaitMs);
1692 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1693 kMaxWaitMs);
1694}
1695
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001696// This test sets up a Jsep call with SCTP DataChannel and verifies the
1697// negotiation is completed without error.
1698#ifdef HAVE_SCTP
deadbeef7c73bdb2015-12-10 15:10:44 -08001699TEST_F(P2PTestConductor, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001700 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001701 FakeConstraints constraints;
1702 constraints.SetMandatory(
1703 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1704 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1705 initializing_client()->CreateDataChannel();
1706 initializing_client()->Negotiate(false, false);
1707}
1708#endif
1709
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710// This test sets up a call between two parties with audio, and video.
1711// During the call, the initializing side restart ice and the test verifies that
1712// new ice candidates are generated and audio and video still can flow.
deadbeef7c73bdb2015-12-10 15:10:44 -08001713TEST_F(P2PTestConductor, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714 ASSERT_TRUE(CreateTestClients());
1715
1716 // Negotiate and wait for ice completion and make sure audio and video plays.
1717 LocalP2PTest();
1718
1719 // Create a SDP string of the first audio candidate for both clients.
1720 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1721 initializing_client()->pc()->local_description()->candidates(0);
1722 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1723 receiving_client()->pc()->local_description()->candidates(0);
1724 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1725 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1726 std::string initiator_candidate;
1727 EXPECT_TRUE(
1728 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1729 std::string receiver_candidate;
1730 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1731
1732 // Restart ice on the initializing client.
1733 receiving_client()->SetExpectIceRestart(true);
1734 initializing_client()->IceRestart();
1735
1736 // Negotiate and wait for ice completion again and make sure audio and video
1737 // plays.
1738 LocalP2PTest();
1739
1740 // Create a SDP string of the first audio candidate for both clients again.
1741 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1742 initializing_client()->pc()->local_description()->candidates(0);
1743 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1744 receiving_client()->pc()->local_description()->candidates(0);
1745 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1746 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1747 std::string initiator_candidate_restart;
1748 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1749 &initiator_candidate_restart));
1750 std::string receiver_candidate_restart;
1751 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1752 &receiver_candidate_restart));
1753
1754 // Verify that the first candidates in the local session descriptions has
1755 // changed.
1756 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1757 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1758}
1759
deadbeeffaac4972015-11-12 15:33:07 -08001760// This test sets up a call between two parties with audio, and video.
1761// It then renegotiates setting the video m-line to "port 0", then later
1762// renegotiates again, enabling video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001763TEST_F(P2PTestConductor, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08001764 ASSERT_TRUE(CreateTestClients());
1765
1766 // Do initial negotiation. Will result in video and audio sendonly m-lines.
1767 receiving_client()->set_auto_add_stream(false);
1768 initializing_client()->AddMediaStream(true, true);
1769 initializing_client()->Negotiate();
1770
1771 // Negotiate again, disabling the video m-line (receiving client will
1772 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
1773 receiving_client()->SetReceiveVideo(false);
1774 initializing_client()->Negotiate();
1775
1776 // Enable video and do negotiation again, making sure video is received
1777 // end-to-end.
1778 receiving_client()->SetReceiveVideo(true);
1779 receiving_client()->AddMediaStream(true, true);
1780 LocalP2PTest();
1781}
1782
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783// This test sets up a Jsep call between two parties with external
1784// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001785// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1786// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001787TEST_F(P2PTestConductor, DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788 ASSERT_TRUE(CreateTestClients());
1789 EnableVideoDecoderFactory();
1790 LocalP2PTest();
1791}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001792
deadbeeffac06552015-11-25 11:26:01 -08001793// This tests that if we negotiate after calling CreateSender but before we
1794// have a track, then set a track later, frames from the newly-set track are
1795// received end-to-end.
deadbeef7c73bdb2015-12-10 15:10:44 -08001796TEST_F(P2PTestConductor, EarlyWarmupTest) {
deadbeeffac06552015-11-25 11:26:01 -08001797 ASSERT_TRUE(CreateTestClients());
1798 auto audio_sender = initializing_client()->pc()->CreateSender("audio");
1799 auto video_sender = initializing_client()->pc()->CreateSender("video");
1800 initializing_client()->Negotiate();
1801 // Wait for ICE connection to complete, without any tracks.
1802 // Note that the receiving client WILL (in HandleIncomingOffer) create
1803 // tracks, so it's only the initiator here that's doing early warmup.
1804 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1805 VerifySessionDescriptions();
1806 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1807 initializing_client()->ice_connection_state(),
1808 kMaxWaitForFramesMs);
1809 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1810 receiving_client()->ice_connection_state(),
1811 kMaxWaitForFramesMs);
1812 // Now set the tracks, and expect frames to immediately start flowing.
1813 EXPECT_TRUE(
1814 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
1815 EXPECT_TRUE(
1816 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
1817 EXPECT_TRUE_WAIT(FramesNotPending(kEndAudioFrameCount, kEndVideoFrameCount),
1818 kMaxWaitForFramesMs);
1819}
1820
deadbeef0a6c4ca2015-10-06 11:38:28 -07001821class IceServerParsingTest : public testing::Test {
1822 public:
1823 // Convenience for parsing a single URL.
1824 bool ParseUrl(const std::string& url) {
1825 return ParseUrl(url, std::string(), std::string());
1826 }
1827
1828 bool ParseUrl(const std::string& url,
1829 const std::string& username,
1830 const std::string& password) {
1831 PeerConnectionInterface::IceServers servers;
1832 PeerConnectionInterface::IceServer server;
1833 server.urls.push_back(url);
1834 server.username = username;
1835 server.password = password;
1836 servers.push_back(server);
1837 return webrtc::ParseIceServers(servers, &stun_configurations_,
1838 &turn_configurations_);
1839 }
1840
1841 protected:
1842 webrtc::StunConfigurations stun_configurations_;
1843 webrtc::TurnConfigurations turn_configurations_;
1844};
1845
1846// Make sure all STUN/TURN prefixes are parsed correctly.
1847TEST_F(IceServerParsingTest, ParseStunPrefixes) {
1848 EXPECT_TRUE(ParseUrl("stun:hostname"));
1849 EXPECT_EQ(1U, stun_configurations_.size());
1850 EXPECT_EQ(0U, turn_configurations_.size());
1851 stun_configurations_.clear();
1852
1853 EXPECT_TRUE(ParseUrl("stuns:hostname"));
1854 EXPECT_EQ(1U, stun_configurations_.size());
1855 EXPECT_EQ(0U, turn_configurations_.size());
1856 stun_configurations_.clear();
1857
1858 EXPECT_TRUE(ParseUrl("turn:hostname"));
1859 EXPECT_EQ(0U, stun_configurations_.size());
1860 EXPECT_EQ(1U, turn_configurations_.size());
1861 EXPECT_FALSE(turn_configurations_[0].secure);
1862 turn_configurations_.clear();
1863
1864 EXPECT_TRUE(ParseUrl("turns:hostname"));
1865 EXPECT_EQ(0U, stun_configurations_.size());
1866 EXPECT_EQ(1U, turn_configurations_.size());
1867 EXPECT_TRUE(turn_configurations_[0].secure);
1868 turn_configurations_.clear();
1869
1870 // invalid prefixes
1871 EXPECT_FALSE(ParseUrl("stunn:hostname"));
1872 EXPECT_FALSE(ParseUrl(":hostname"));
1873 EXPECT_FALSE(ParseUrl(":"));
1874 EXPECT_FALSE(ParseUrl(""));
1875}
1876
1877TEST_F(IceServerParsingTest, VerifyDefaults) {
1878 // TURNS defaults
1879 EXPECT_TRUE(ParseUrl("turns:hostname"));
1880 EXPECT_EQ(1U, turn_configurations_.size());
1881 EXPECT_EQ(5349, turn_configurations_[0].server.port());
1882 EXPECT_EQ("tcp", turn_configurations_[0].transport_type);
1883 turn_configurations_.clear();
1884
1885 // TURN defaults
1886 EXPECT_TRUE(ParseUrl("turn:hostname"));
1887 EXPECT_EQ(1U, turn_configurations_.size());
1888 EXPECT_EQ(3478, turn_configurations_[0].server.port());
1889 EXPECT_EQ("udp", turn_configurations_[0].transport_type);
1890 turn_configurations_.clear();
1891
1892 // STUN defaults
1893 EXPECT_TRUE(ParseUrl("stun:hostname"));
1894 EXPECT_EQ(1U, stun_configurations_.size());
1895 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1896 stun_configurations_.clear();
1897}
1898
1899// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
1900// can be parsed correctly.
1901TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
1902 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
1903 EXPECT_EQ(1U, stun_configurations_.size());
1904 EXPECT_EQ("1.2.3.4", stun_configurations_[0].server.hostname());
1905 EXPECT_EQ(1234, stun_configurations_[0].server.port());
1906 stun_configurations_.clear();
1907
1908 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
1909 EXPECT_EQ(1U, stun_configurations_.size());
1910 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_configurations_[0].server.hostname());
1911 EXPECT_EQ(4321, stun_configurations_[0].server.port());
1912 stun_configurations_.clear();
1913
1914 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
1915 EXPECT_EQ(1U, stun_configurations_.size());
1916 EXPECT_EQ("hostname", stun_configurations_[0].server.hostname());
1917 EXPECT_EQ(9999, stun_configurations_[0].server.port());
1918 stun_configurations_.clear();
1919
1920 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
1921 EXPECT_EQ(1U, stun_configurations_.size());
1922 EXPECT_EQ("1.2.3.4", stun_configurations_[0].server.hostname());
1923 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1924 stun_configurations_.clear();
1925
1926 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
1927 EXPECT_EQ(1U, stun_configurations_.size());
1928 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_configurations_[0].server.hostname());
1929 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1930 stun_configurations_.clear();
1931
1932 EXPECT_TRUE(ParseUrl("stun:hostname"));
1933 EXPECT_EQ(1U, stun_configurations_.size());
1934 EXPECT_EQ("hostname", stun_configurations_[0].server.hostname());
1935 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1936 stun_configurations_.clear();
1937
1938 // Try some invalid hostname:port strings.
1939 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
1940 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
1941 EXPECT_FALSE(ParseUrl("stun:hostname:"));
1942 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
1943 EXPECT_FALSE(ParseUrl("stun::5555"));
1944 EXPECT_FALSE(ParseUrl("stun:"));
1945}
1946
1947// Test parsing the "?transport=xxx" part of the URL.
1948TEST_F(IceServerParsingTest, ParseTransport) {
1949 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
1950 EXPECT_EQ(1U, turn_configurations_.size());
1951 EXPECT_EQ("tcp", turn_configurations_[0].transport_type);
1952 turn_configurations_.clear();
1953
1954 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
1955 EXPECT_EQ(1U, turn_configurations_.size());
1956 EXPECT_EQ("udp", turn_configurations_[0].transport_type);
1957 turn_configurations_.clear();
1958
1959 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
1960}
1961
1962// Test parsing ICE username contained in URL.
1963TEST_F(IceServerParsingTest, ParseUsername) {
1964 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
1965 EXPECT_EQ(1U, turn_configurations_.size());
1966 EXPECT_EQ("user", turn_configurations_[0].username);
1967 turn_configurations_.clear();
1968
1969 EXPECT_FALSE(ParseUrl("turn:@hostname"));
1970 EXPECT_FALSE(ParseUrl("turn:username@"));
1971 EXPECT_FALSE(ParseUrl("turn:@"));
1972 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
1973}
1974
1975// Test that username and password from IceServer is copied into the resulting
1976// TurnConfiguration.
1977TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
1978 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
1979 EXPECT_EQ(1U, turn_configurations_.size());
1980 EXPECT_EQ("username", turn_configurations_[0].username);
1981 EXPECT_EQ("password", turn_configurations_[0].password);
1982}
1983
1984// Ensure that if a server has multiple URLs, each one is parsed.
1985TEST_F(IceServerParsingTest, ParseMultipleUrls) {
1986 PeerConnectionInterface::IceServers servers;
1987 PeerConnectionInterface::IceServer server;
1988 server.urls.push_back("stun:hostname");
1989 server.urls.push_back("turn:hostname");
1990 servers.push_back(server);
1991 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_configurations_,
1992 &turn_configurations_));
1993 EXPECT_EQ(1U, stun_configurations_.size());
1994 EXPECT_EQ(1U, turn_configurations_.size());
1995}
1996
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001997#endif // if !defined(THREAD_SANITIZER)