blob: 605e1a5e1f91f83701f17a8cd3e6a63c9a5bd1a7 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <stdio.h>
29
30#include <algorithm>
31#include <list>
32#include <map>
33#include <vector>
34
35#include "talk/app/webrtc/dtmfsender.h"
jbauchac8869e2015-07-03 01:36:14 -070036#include "talk/app/webrtc/fakemetricsobserver.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037#include "talk/app/webrtc/fakeportallocatorfactory.h"
38#include "talk/app/webrtc/localaudiosource.h"
39#include "talk/app/webrtc/mediastreaminterface.h"
deadbeef0a6c4ca2015-10-06 11:38:28 -070040#include "talk/app/webrtc/peerconnection.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041#include "talk/app/webrtc/peerconnectionfactory.h"
42#include "talk/app/webrtc/peerconnectioninterface.h"
43#include "talk/app/webrtc/test/fakeaudiocapturemodule.h"
44#include "talk/app/webrtc/test/fakeconstraints.h"
Henrik Boström5e56c592015-08-11 10:33:13 +020045#include "talk/app/webrtc/test/fakedtlsidentitystore.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046#include "talk/app/webrtc/test/fakeperiodicvideocapturer.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000047#include "talk/app/webrtc/test/fakevideotrackrenderer.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048#include "talk/app/webrtc/test/mockpeerconnectionobservers.h"
49#include "talk/app/webrtc/videosourceinterface.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000050#include "talk/media/webrtc/fakewebrtcvideoengine.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000051#include "talk/session/media/mediasession.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000052#include "webrtc/base/gunit.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000053#include "webrtc/base/physicalsocketserver.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000054#include "webrtc/base/scoped_ptr.h"
55#include "webrtc/base/ssladapter.h"
56#include "webrtc/base/sslstreamadapter.h"
57#include "webrtc/base/thread.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000058#include "webrtc/base/virtualsocketserver.h"
59#include "webrtc/p2p/base/constants.h"
60#include "webrtc/p2p/base/sessiondescription.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061
62#define MAYBE_SKIP_TEST(feature) \
63 if (!(feature())) { \
64 LOG(LS_INFO) << "Feature disabled... skipping"; \
65 return; \
66 }
67
68using cricket::ContentInfo;
69using cricket::FakeWebRtcVideoDecoder;
70using cricket::FakeWebRtcVideoDecoderFactory;
71using cricket::FakeWebRtcVideoEncoder;
72using cricket::FakeWebRtcVideoEncoderFactory;
73using cricket::MediaContentDescription;
74using webrtc::DataBuffer;
75using webrtc::DataChannelInterface;
76using webrtc::DtmfSender;
77using webrtc::DtmfSenderInterface;
78using webrtc::DtmfSenderObserverInterface;
79using webrtc::FakeConstraints;
80using webrtc::MediaConstraintsInterface;
deadbeeffaac4972015-11-12 15:33:07 -080081using webrtc::MediaStreamInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082using webrtc::MediaStreamTrackInterface;
83using webrtc::MockCreateSessionDescriptionObserver;
84using webrtc::MockDataChannelObserver;
85using webrtc::MockSetSessionDescriptionObserver;
86using webrtc::MockStatsObserver;
deadbeeffaac4972015-11-12 15:33:07 -080087using webrtc::ObserverInterface;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000088using webrtc::PeerConnectionInterface;
Joachim Bauch04e5b492015-05-29 09:40:39 +020089using webrtc::PeerConnectionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090using webrtc::SessionDescriptionInterface;
91using webrtc::StreamCollectionInterface;
92
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000093static const int kMaxWaitMs = 10000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000094// Disable for TSan v2, see
95// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
96// This declaration is also #ifdef'd as it causes uninitialized-variable
97// warnings.
98#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099static const int kMaxWaitForStatsMs = 3000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +0000100#endif
deadbeeffac06552015-11-25 11:26:01 -0800101static const int kMaxWaitForActivationMs = 5000;
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +0000102static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103static const int kEndAudioFrameCount = 3;
104static const int kEndVideoFrameCount = 3;
105
106static const char kStreamLabelBase[] = "stream_label";
107static const char kVideoTrackLabelBase[] = "video_track";
108static const char kAudioTrackLabelBase[] = "audio_track";
109static const char kDataChannelLabel[] = "data_channel";
110
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000111// Disable for TSan v2, see
112// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
113// This declaration is also #ifdef'd as it causes unused-variable errors.
114#if !defined(THREAD_SANITIZER)
115// SRTP cipher name negotiated by the tests. This must be updated if the
116// default changes.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800117static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000118#endif
119
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120static void RemoveLinesFromSdp(const std::string& line_start,
121 std::string* sdp) {
122 const char kSdpLineEnd[] = "\r\n";
123 size_t ssrc_pos = 0;
124 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
125 std::string::npos) {
126 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
127 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
128 }
129}
130
131class SignalingMessageReceiver {
132 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 virtual void ReceiveSdpMessage(const std::string& type,
134 std::string& msg) = 0;
135 virtual void ReceiveIceMessage(const std::string& sdp_mid,
136 int sdp_mline_index,
137 const std::string& msg) = 0;
138
139 protected:
deadbeefaf1b59c2015-10-15 12:08:41 -0700140 SignalingMessageReceiver() {}
141 virtual ~SignalingMessageReceiver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142};
143
deadbeefaf1b59c2015-10-15 12:08:41 -0700144class PeerConnectionTestClient : public webrtc::PeerConnectionObserver,
deadbeeffaac4972015-11-12 15:33:07 -0800145 public SignalingMessageReceiver,
146 public ObserverInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147 public:
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800148 static PeerConnectionTestClient* CreateClientWithDtlsIdentityStore(
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800149 const std::string& id,
150 const MediaConstraintsInterface* constraints,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800151 const PeerConnectionFactory::Options* options,
152 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800153 PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800154 if (!client->Init(constraints, options, dtls_identity_store.Pass())) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800155 delete client;
156 return nullptr;
157 }
158 return client;
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800159 }
160
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800161 static PeerConnectionTestClient* CreateClient(
162 const std::string& id,
163 const MediaConstraintsInterface* constraints,
164 const PeerConnectionFactory::Options* options) {
165 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
166 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
167 : nullptr);
168
169 return CreateClientWithDtlsIdentityStore(id, constraints, options,
170 dtls_identity_store.Pass());
171 }
172
deadbeefaf1b59c2015-10-15 12:08:41 -0700173 ~PeerConnectionTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 while (!fake_video_renderers_.empty()) {
175 RenderMap::iterator it = fake_video_renderers_.begin();
176 delete it->second;
177 fake_video_renderers_.erase(it);
178 }
179 }
180
deadbeefaf1b59c2015-10-15 12:08:41 -0700181 void Negotiate() { Negotiate(true, true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182
deadbeefaf1b59c2015-10-15 12:08:41 -0700183 void Negotiate(bool audio, bool video) {
184 rtc::scoped_ptr<SessionDescriptionInterface> offer;
185 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186
deadbeefaf1b59c2015-10-15 12:08:41 -0700187 if (offer->description()->GetContentByName("audio")) {
188 offer->description()->GetContentByName("audio")->rejected = !audio;
189 }
190 if (offer->description()->GetContentByName("video")) {
191 offer->description()->GetContentByName("video")->rejected = !video;
192 }
193
194 std::string sdp;
195 EXPECT_TRUE(offer->ToString(&sdp));
196 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
197 signaling_message_receiver_->ReceiveSdpMessage(
198 webrtc::SessionDescriptionInterface::kOffer, sdp);
199 }
200
201 // SignalingMessageReceiver callback.
202 void ReceiveSdpMessage(const std::string& type, std::string& msg) override {
203 FilterIncomingSdpMessage(&msg);
204 if (type == webrtc::SessionDescriptionInterface::kOffer) {
205 HandleIncomingOffer(msg);
206 } else {
207 HandleIncomingAnswer(msg);
208 }
209 }
210
211 // SignalingMessageReceiver callback.
212 void ReceiveIceMessage(const std::string& sdp_mid,
213 int sdp_mline_index,
214 const std::string& msg) override {
215 LOG(INFO) << id_ << "ReceiveIceMessage";
216 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
217 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
218 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
219 }
220
221 // PeerConnectionObserver callbacks.
222 void OnSignalingChange(
223 webrtc::PeerConnectionInterface::SignalingState new_state) override {
224 EXPECT_EQ(pc()->signaling_state(), new_state);
225 }
deadbeeffaac4972015-11-12 15:33:07 -0800226 void OnAddStream(MediaStreamInterface* media_stream) override {
227 media_stream->RegisterObserver(this);
deadbeefaf1b59c2015-10-15 12:08:41 -0700228 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
229 const std::string id = media_stream->GetVideoTracks()[i]->id();
230 ASSERT_TRUE(fake_video_renderers_.find(id) ==
231 fake_video_renderers_.end());
232 fake_video_renderers_[id] =
233 new webrtc::FakeVideoTrackRenderer(media_stream->GetVideoTracks()[i]);
234 }
235 }
deadbeeffaac4972015-11-12 15:33:07 -0800236 void OnRemoveStream(MediaStreamInterface* media_stream) override {}
deadbeefaf1b59c2015-10-15 12:08:41 -0700237 void OnRenegotiationNeeded() override {}
238 void OnIceConnectionChange(
239 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
240 EXPECT_EQ(pc()->ice_connection_state(), new_state);
241 }
242 void OnIceGatheringChange(
243 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
244 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
245 }
246 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
247 LOG(INFO) << id_ << "OnIceCandidate";
248
249 std::string ice_sdp;
250 EXPECT_TRUE(candidate->ToString(&ice_sdp));
251 if (signaling_message_receiver_ == nullptr) {
252 // Remote party may be deleted.
253 return;
254 }
255 signaling_message_receiver_->ReceiveIceMessage(
256 candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
257 }
258
deadbeeffaac4972015-11-12 15:33:07 -0800259 // MediaStreamInterface callback
260 void OnChanged() override {
261 // Track added or removed from MediaStream, so update our renderers.
262 rtc::scoped_refptr<StreamCollectionInterface> remote_streams =
263 pc()->remote_streams();
264 // Remove renderers for tracks that were removed.
265 for (auto it = fake_video_renderers_.begin();
266 it != fake_video_renderers_.end();) {
267 if (remote_streams->FindVideoTrack(it->first) == nullptr) {
268 auto to_delete = it++;
269 delete to_delete->second;
270 fake_video_renderers_.erase(to_delete);
271 } else {
272 ++it;
273 }
274 }
275 // Create renderers for new video tracks.
276 for (size_t stream_index = 0; stream_index < remote_streams->count();
277 ++stream_index) {
278 MediaStreamInterface* remote_stream = remote_streams->at(stream_index);
279 for (size_t track_index = 0;
280 track_index < remote_stream->GetVideoTracks().size();
281 ++track_index) {
282 const std::string id =
283 remote_stream->GetVideoTracks()[track_index]->id();
284 if (fake_video_renderers_.find(id) != fake_video_renderers_.end()) {
285 continue;
286 }
287 fake_video_renderers_[id] = new webrtc::FakeVideoTrackRenderer(
288 remote_stream->GetVideoTracks()[track_index]);
289 }
290 }
291 }
292
deadbeefaf1b59c2015-10-15 12:08:41 -0700293 void SetVideoConstraints(const webrtc::FakeConstraints& video_constraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294 video_constraints_ = video_constraint;
295 }
296
297 void AddMediaStream(bool audio, bool video) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700298 std::string stream_label =
299 kStreamLabelBase +
300 rtc::ToString<int>(static_cast<int>(pc()->local_streams()->count()));
deadbeeffaac4972015-11-12 15:33:07 -0800301 rtc::scoped_refptr<MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000302 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303
304 if (audio && can_receive_audio()) {
deadbeeffac06552015-11-25 11:26:01 -0800305 stream->AddTrack(CreateLocalAudioTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306 }
307 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000308 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 }
310
deadbeefaf1b59c2015-10-15 12:08:41 -0700311 EXPECT_TRUE(pc()->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312 }
313
deadbeefaf1b59c2015-10-15 12:08:41 -0700314 size_t NumberOfLocalMediaStreams() { return pc()->local_streams()->count(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315
316 bool SessionActive() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700317 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318 }
319
deadbeeffaac4972015-11-12 15:33:07 -0800320 // Automatically add a stream when receiving an offer, if we don't have one.
321 // Defaults to true.
322 void set_auto_add_stream(bool auto_add_stream) {
323 auto_add_stream_ = auto_add_stream;
324 }
325
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326 void set_signaling_message_receiver(
deadbeefaf1b59c2015-10-15 12:08:41 -0700327 SignalingMessageReceiver* signaling_message_receiver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328 signaling_message_receiver_ = signaling_message_receiver;
329 }
330
331 void EnableVideoDecoderFactory() {
332 video_decoder_factory_enabled_ = true;
333 fake_video_decoder_factory_->AddSupportedVideoCodecType(
334 webrtc::kVideoCodecVP8);
335 }
336
deadbeefaf1b59c2015-10-15 12:08:41 -0700337 void IceRestart() {
338 session_description_constraints_.SetMandatoryIceRestart(true);
339 SetExpectIceRestart(true);
340 }
341
342 void SetExpectIceRestart(bool expect_restart) {
343 expect_ice_restart_ = expect_restart;
344 }
345
346 bool ExpectIceRestart() const { return expect_ice_restart_; }
347
348 void SetReceiveAudioVideo(bool audio, bool video) {
349 SetReceiveAudio(audio);
350 SetReceiveVideo(video);
351 ASSERT_EQ(audio, can_receive_audio());
352 ASSERT_EQ(video, can_receive_video());
353 }
354
355 void SetReceiveAudio(bool audio) {
356 if (audio && can_receive_audio())
357 return;
358 session_description_constraints_.SetMandatoryReceiveAudio(audio);
359 }
360
361 void SetReceiveVideo(bool video) {
362 if (video && can_receive_video())
363 return;
364 session_description_constraints_.SetMandatoryReceiveVideo(video);
365 }
366
367 void RemoveMsidFromReceivedSdp(bool remove) { remove_msid_ = remove; }
368
369 void RemoveSdesCryptoFromReceivedSdp(bool remove) { remove_sdes_ = remove; }
370
371 void RemoveBundleFromReceivedSdp(bool remove) { remove_bundle_ = remove; }
372
373 bool can_receive_audio() {
374 bool value;
375 if (webrtc::FindConstraint(&session_description_constraints_,
376 MediaConstraintsInterface::kOfferToReceiveAudio,
377 &value, nullptr)) {
378 return value;
379 }
380 return true;
381 }
382
383 bool can_receive_video() {
384 bool value;
385 if (webrtc::FindConstraint(&session_description_constraints_,
386 MediaConstraintsInterface::kOfferToReceiveVideo,
387 &value, nullptr)) {
388 return value;
389 }
390 return true;
391 }
392
393 void OnIceComplete() override { LOG(INFO) << id_ << "OnIceComplete"; }
394
395 void OnDataChannel(DataChannelInterface* data_channel) override {
396 LOG(INFO) << id_ << "OnDataChannel";
397 data_channel_ = data_channel;
398 data_observer_.reset(new MockDataChannelObserver(data_channel));
399 }
400
401 void CreateDataChannel() {
402 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, nullptr);
403 ASSERT_TRUE(data_channel_.get() != nullptr);
404 data_observer_.reset(new MockDataChannelObserver(data_channel_));
405 }
406
deadbeeffac06552015-11-25 11:26:01 -0800407 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack(
408 const std::string& stream_label) {
409 FakeConstraints constraints;
410 // Disable highpass filter so that we can get all the test audio frames.
411 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
412 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
413 peer_connection_factory_->CreateAudioSource(&constraints);
414 // TODO(perkj): Test audio source when it is implemented. Currently audio
415 // always use the default input.
416 std::string label = stream_label + kAudioTrackLabelBase;
417 return peer_connection_factory_->CreateAudioTrack(label, source);
418 }
419
420 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack(
421 const std::string& stream_label) {
422 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
423 FakeConstraints source_constraints = video_constraints_;
424 source_constraints.SetMandatoryMaxFrameRate(10);
425
426 cricket::FakeVideoCapturer* fake_capturer =
427 new webrtc::FakePeriodicVideoCapturer();
428 video_capturers_.push_back(fake_capturer);
429 rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
430 peer_connection_factory_->CreateVideoSource(fake_capturer,
431 &source_constraints);
432 std::string label = stream_label + kVideoTrackLabelBase;
433 return peer_connection_factory_->CreateVideoTrack(label, source);
434 }
435
deadbeefaf1b59c2015-10-15 12:08:41 -0700436 DataChannelInterface* data_channel() { return data_channel_; }
437 const MockDataChannelObserver* data_observer() const {
438 return data_observer_.get();
439 }
440
441 webrtc::PeerConnectionInterface* pc() { return peer_connection_.get(); }
442
443 void StopVideoCapturers() {
444 for (std::vector<cricket::VideoCapturer*>::iterator it =
445 video_capturers_.begin();
446 it != video_capturers_.end(); ++it) {
447 (*it)->Stop();
448 }
449 }
450
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451 bool AudioFramesReceivedCheck(int number_of_frames) const {
452 return number_of_frames <= fake_audio_capture_module_->frames_received();
453 }
454
455 bool VideoFramesReceivedCheck(int number_of_frames) {
456 if (video_decoder_factory_enabled_) {
457 const std::vector<FakeWebRtcVideoDecoder*>& decoders
458 = fake_video_decoder_factory_->decoders();
459 if (decoders.empty()) {
460 return number_of_frames <= 0;
461 }
462
463 for (std::vector<FakeWebRtcVideoDecoder*>::const_iterator
464 it = decoders.begin(); it != decoders.end(); ++it) {
465 if (number_of_frames > (*it)->GetNumFramesReceived()) {
466 return false;
467 }
468 }
469 return true;
470 } else {
471 if (fake_video_renderers_.empty()) {
472 return number_of_frames <= 0;
473 }
474
475 for (RenderMap::const_iterator it = fake_video_renderers_.begin();
476 it != fake_video_renderers_.end(); ++it) {
477 if (number_of_frames > it->second->num_rendered_frames()) {
478 return false;
479 }
480 }
481 return true;
482 }
483 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700484
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 // Verify the CreateDtmfSender interface
486 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000487 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
488 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489
490 // We can't create a DTMF sender with an invalid audio track or a non local
491 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700492 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000493 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700494 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
495 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496
497 // We should be able to create a DTMF sender from a local track.
498 webrtc::AudioTrackInterface* localtrack =
499 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
500 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700501 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 dtmf_sender->RegisterObserver(observer.get());
503
504 // Test the DtmfSender object just created.
505 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
506 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
507
508 // We don't need to verify that the DTMF tones are actually sent out because
509 // that is already covered by the tests of the lower level components.
510
511 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
512 std::vector<std::string> tones;
513 tones.push_back("1");
514 tones.push_back("a");
515 tones.push_back("");
516 observer->Verify(tones);
517
518 dtmf_sender->UnregisterObserver();
519 }
520
521 // Verifies that the SessionDescription have rejected the appropriate media
522 // content.
523 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700524 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
525 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 const cricket::SessionDescription* remote_desc =
527 peer_connection_->remote_description()->description();
528 const cricket::SessionDescription* local_desc =
529 peer_connection_->local_description()->description();
530
531 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
532 if (remote_audio_content) {
533 const ContentInfo* audio_content =
534 GetFirstAudioContent(local_desc);
535 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
536 }
537
538 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
539 if (remote_video_content) {
540 const ContentInfo* video_content =
541 GetFirstVideoContent(local_desc);
542 EXPECT_EQ(can_receive_video(), !video_content->rejected);
543 }
544 }
545
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700547 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548 const cricket::SessionDescription* desc =
549 peer_connection_->local_description()->description();
550 const cricket::ContentInfos& contents = desc->contents();
551
552 for (size_t index = 0; index < contents.size(); ++index) {
553 if (contents[index].rejected)
554 continue;
555 const cricket::TransportDescription* transport_desc =
556 desc->GetTransportDescriptionByName(contents[index].name);
557
558 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000559 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560 if (ufragpair_it == ice_ufrag_pwd_.end()) {
561 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000562 ice_ufrag_pwd_[static_cast<int>(index)] =
563 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564 } else if (ExpectIceRestart()) {
565 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
566 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
567 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
568 } else {
569 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
570 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
571 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
572 }
573 }
574 }
575
576 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000577 rtc::scoped_refptr<MockStatsObserver>
578 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000579 EXPECT_TRUE(peer_connection_->GetStats(
580 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700582 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583 return observer->AudioOutputLevel();
584 }
585
586 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000587 rtc::scoped_refptr<MockStatsObserver>
588 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000589 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700590 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700592 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 return observer->AudioInputLevel();
594 }
595
596 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000597 rtc::scoped_refptr<MockStatsObserver>
598 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000599 EXPECT_TRUE(peer_connection_->GetStats(
600 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700602 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 return observer->BytesReceived();
604 }
605
606 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000607 rtc::scoped_refptr<MockStatsObserver>
608 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000609 EXPECT_TRUE(peer_connection_->GetStats(
610 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700612 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613 return observer->BytesSent();
614 }
615
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000616 int GetAvailableReceivedBandwidthStats() {
617 rtc::scoped_refptr<MockStatsObserver>
618 observer(new rtc::RefCountedObject<MockStatsObserver>());
619 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700620 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000621 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700622 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000623 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000624 return bw;
625 }
626
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000627 std::string GetDtlsCipherStats() {
628 rtc::scoped_refptr<MockStatsObserver>
629 observer(new rtc::RefCountedObject<MockStatsObserver>());
630 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700631 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000632 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700633 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000634 return observer->DtlsCipher();
635 }
636
637 std::string GetSrtpCipherStats() {
638 rtc::scoped_refptr<MockStatsObserver>
639 observer(new rtc::RefCountedObject<MockStatsObserver>());
640 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700641 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000642 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700643 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000644 return observer->SrtpCipher();
645 }
646
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 int rendered_width() {
648 EXPECT_FALSE(fake_video_renderers_.empty());
649 return fake_video_renderers_.empty() ? 1 :
650 fake_video_renderers_.begin()->second->width();
651 }
652
653 int rendered_height() {
654 EXPECT_FALSE(fake_video_renderers_.empty());
655 return fake_video_renderers_.empty() ? 1 :
656 fake_video_renderers_.begin()->second->height();
657 }
658
659 size_t number_of_remote_streams() {
660 if (!pc())
661 return 0;
662 return pc()->remote_streams()->count();
663 }
664
665 StreamCollectionInterface* remote_streams() {
666 if (!pc()) {
667 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700668 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669 }
670 return pc()->remote_streams();
671 }
672
673 StreamCollectionInterface* local_streams() {
674 if (!pc()) {
675 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700676 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677 }
678 return pc()->local_streams();
679 }
680
681 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
682 return pc()->signaling_state();
683 }
684
685 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
686 return pc()->ice_connection_state();
687 }
688
689 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
690 return pc()->ice_gathering_state();
691 }
692
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693 private:
694 class DummyDtmfObserver : public DtmfSenderObserverInterface {
695 public:
696 DummyDtmfObserver() : completed_(false) {}
697
698 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700699 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 tones_.push_back(tone);
701 if (tone.empty()) {
702 completed_ = true;
703 }
704 }
705
706 void Verify(const std::vector<std::string>& tones) const {
707 ASSERT_TRUE(tones_.size() == tones.size());
708 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
709 }
710
711 bool completed() const { return completed_; }
712
713 private:
714 bool completed_;
715 std::vector<std::string> tones_;
716 };
717
deadbeefaf1b59c2015-10-15 12:08:41 -0700718 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
719
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800720 bool Init(
721 const MediaConstraintsInterface* constraints,
722 const PeerConnectionFactory::Options* options,
723 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700724 EXPECT_TRUE(!peer_connection_);
725 EXPECT_TRUE(!peer_connection_factory_);
726 allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
727 if (!allocator_factory_) {
728 return false;
729 }
730 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
731
732 if (fake_audio_capture_module_ == nullptr) {
733 return false;
734 }
735 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
736 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
737 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
738 rtc::Thread::Current(), rtc::Thread::Current(),
739 fake_audio_capture_module_, fake_video_encoder_factory_,
740 fake_video_decoder_factory_);
741 if (!peer_connection_factory_) {
742 return false;
743 }
744 if (options) {
745 peer_connection_factory_->SetOptions(*options);
746 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800747 peer_connection_ = CreatePeerConnection(
748 allocator_factory_.get(), constraints, dtls_identity_store.Pass());
deadbeefaf1b59c2015-10-15 12:08:41 -0700749 return peer_connection_.get() != nullptr;
750 }
751
deadbeefaf1b59c2015-10-15 12:08:41 -0700752 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
753 webrtc::PortAllocatorFactoryInterface* factory,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800754 const MediaConstraintsInterface* constraints,
755 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 // CreatePeerConnection with IceServers.
757 webrtc::PeerConnectionInterface::IceServers ice_servers;
758 webrtc::PeerConnectionInterface::IceServer ice_server;
759 ice_server.uri = "stun:stun.l.google.com:19302";
760 ice_servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000761
deadbeefaf1b59c2015-10-15 12:08:41 -0700762 return peer_connection_factory_->CreatePeerConnection(
Henrik Boström5e56c592015-08-11 10:33:13 +0200763 ice_servers, constraints, factory, dtls_identity_store.Pass(), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 }
765
766 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700767 LOG(INFO) << id_ << "HandleIncomingOffer ";
deadbeeffaac4972015-11-12 15:33:07 -0800768 if (NumberOfLocalMediaStreams() == 0 && auto_add_stream_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 // If we are not sending any streams ourselves it is time to add some.
770 AddMediaStream(true, true);
771 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000772 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700773 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000775 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 EXPECT_TRUE(DoCreateAnswer(answer.use()));
777 std::string sdp;
778 EXPECT_TRUE(answer->ToString(&sdp));
779 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
deadbeefaf1b59c2015-10-15 12:08:41 -0700780 if (signaling_message_receiver_) {
781 signaling_message_receiver_->ReceiveSdpMessage(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 webrtc::SessionDescriptionInterface::kAnswer, sdp);
783 }
784 }
785
786 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700787 LOG(INFO) << id_ << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000788 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700789 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
791 }
792
793 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
794 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000795 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
796 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 MockCreateSessionDescriptionObserver>());
798 if (offer) {
799 pc()->CreateOffer(observer, &session_description_constraints_);
800 } else {
801 pc()->CreateAnswer(observer, &session_description_constraints_);
802 }
803 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
804 *desc = observer->release_desc();
805 if (observer->result() && ExpectIceRestart()) {
806 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
807 }
808 return observer->result();
809 }
810
811 bool DoCreateOffer(SessionDescriptionInterface** desc) {
812 return DoCreateOfferAnswer(desc, true);
813 }
814
815 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
816 return DoCreateOfferAnswer(desc, false);
817 }
818
819 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000820 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
821 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700823 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 pc()->SetLocalDescription(observer, desc);
825 // Ignore the observer result. If we wait for the result with
826 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
827 // before the offer which is an error.
828 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000829 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 // ProcessMessages waits at least 1ms but processes all messages before
831 // returning. Since this test is synchronous and send messages to the remote
832 // peer whenever a callback is invoked, this can lead to messages being
833 // sent to the remote peer in the wrong order.
834 // TODO(perkj): Find a way to check the result without risking that the
835 // order of sent messages are changed. Ex- by posting all messages that are
836 // sent to the remote peer.
837 return true;
838 }
839
840 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000841 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
842 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700844 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000845 pc()->SetRemoteDescription(observer, desc);
846 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
847 return observer->result();
848 }
849
850 // This modifies all received SDP messages before they are processed.
851 void FilterIncomingSdpMessage(std::string* sdp) {
852 if (remove_msid_) {
853 const char kSdpSsrcAttribute[] = "a=ssrc:";
854 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
855 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
856 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
857 }
858 if (remove_bundle_) {
859 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
860 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
861 }
862 if (remove_sdes_) {
863 const char kSdpSdesCryptoAttribute[] = "a=crypto";
864 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
865 }
866 }
867
deadbeefaf1b59c2015-10-15 12:08:41 -0700868 std::string id_;
869
870 rtc::scoped_refptr<webrtc::PortAllocatorFactoryInterface> allocator_factory_;
871 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
872 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
873 peer_connection_factory_;
874
deadbeeffaac4972015-11-12 15:33:07 -0800875 bool auto_add_stream_ = true;
876
deadbeefaf1b59c2015-10-15 12:08:41 -0700877 typedef std::pair<std::string, std::string> IceUfragPwdPair;
878 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
879 bool expect_ice_restart_ = false;
880
881 // Needed to keep track of number of frames send.
882 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
883 // Needed to keep track of number of frames received.
884 typedef std::map<std::string, webrtc::FakeVideoTrackRenderer*> RenderMap;
885 RenderMap fake_video_renderers_;
886 // Needed to keep track of number of frames received when external decoder
887 // used.
888 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
889 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
890 bool video_decoder_factory_enabled_ = false;
891 webrtc::FakeConstraints video_constraints_;
892
893 // For remote peer communication.
894 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
895
896 // Store references to the video capturers we've created, so that we can stop
897 // them, if required.
898 std::vector<cricket::VideoCapturer*> video_capturers_;
899
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 webrtc::FakeConstraints session_description_constraints_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700901 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
902 bool remove_bundle_ =
903 false; // True if bundle should be removed in received SDP.
904 bool remove_sdes_ =
905 false; // True if a=crypto should be removed in received SDP.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000907 rtc::scoped_refptr<DataChannelInterface> data_channel_;
908 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909};
910
deadbeef7c73bdb2015-12-10 15:10:44 -0800911class P2PTestConductor : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 public:
deadbeef7c73bdb2015-12-10 15:10:44 -0800913 P2PTestConductor()
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000914 : pss_(new rtc::PhysicalSocketServer),
915 ss_(new rtc::VirtualSocketServer(pss_.get())),
916 ss_scope_(ss_.get()) {}
917
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918 bool SessionActive() {
919 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000920 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000922
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 // Return true if the number of frames provided have been received or it is
924 // known that that will never occur (e.g. no frames will be sent or
925 // captured).
926 bool FramesNotPending(int audio_frames_to_receive,
927 int video_frames_to_receive) {
928 return VideoFramesReceivedCheck(video_frames_to_receive) &&
929 AudioFramesReceivedCheck(audio_frames_to_receive);
930 }
931 bool AudioFramesReceivedCheck(int frames_received) {
932 return initiating_client_->AudioFramesReceivedCheck(frames_received) &&
933 receiving_client_->AudioFramesReceivedCheck(frames_received);
934 }
935 bool VideoFramesReceivedCheck(int frames_received) {
936 return initiating_client_->VideoFramesReceivedCheck(frames_received) &&
937 receiving_client_->VideoFramesReceivedCheck(frames_received);
938 }
939 void VerifyDtmf() {
940 initiating_client_->VerifyDtmf();
941 receiving_client_->VerifyDtmf();
942 }
943
944 void TestUpdateOfferWithRejectedContent() {
945 initiating_client_->Negotiate(true, false);
946 EXPECT_TRUE_WAIT(
947 FramesNotPending(kEndAudioFrameCount * 2, kEndVideoFrameCount),
948 kMaxWaitForFramesMs);
949 // There shouldn't be any more video frame after the new offer is
950 // negotiated.
951 EXPECT_FALSE(VideoFramesReceivedCheck(kEndVideoFrameCount + 1));
952 }
953
954 void VerifyRenderedSize(int width, int height) {
955 EXPECT_EQ(width, receiving_client()->rendered_width());
956 EXPECT_EQ(height, receiving_client()->rendered_height());
957 EXPECT_EQ(width, initializing_client()->rendered_width());
958 EXPECT_EQ(height, initializing_client()->rendered_height());
959 }
960
961 void VerifySessionDescriptions() {
962 initiating_client_->VerifyRejectedMediaInSessionDescription();
963 receiving_client_->VerifyRejectedMediaInSessionDescription();
964 initiating_client_->VerifyLocalIceUfragAndPassword();
965 receiving_client_->VerifyLocalIceUfragAndPassword();
966 }
967
deadbeef7c73bdb2015-12-10 15:10:44 -0800968 ~P2PTestConductor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700970 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 }
972 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700973 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975 }
976
deadbeefaf1b59c2015-10-15 12:08:41 -0700977 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000978
979 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
980 MediaConstraintsInterface* recv_constraints) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700981 return CreateTestClients(init_constraints, nullptr, recv_constraints,
982 nullptr);
Joachim Bauch04e5b492015-05-29 09:40:39 +0200983 }
984
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800985 void SetSignalingReceivers() {
986 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
987 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
988 }
989
Joachim Bauch04e5b492015-05-29 09:40:39 +0200990 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
991 PeerConnectionFactory::Options* init_options,
992 MediaConstraintsInterface* recv_constraints,
993 PeerConnectionFactory::Options* recv_options) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700994 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
995 "Caller: ", init_constraints, init_options));
996 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
997 "Callee: ", recv_constraints, recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998 if (!initiating_client_ || !receiving_client_) {
999 return false;
1000 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001001 SetSignalingReceivers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001002 return true;
1003 }
1004
1005 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1006 const webrtc::FakeConstraints& recv_constraints) {
1007 initiating_client_->SetVideoConstraints(init_constraints);
1008 receiving_client_->SetVideoConstraints(recv_constraints);
1009 }
1010
1011 void EnableVideoDecoderFactory() {
1012 initiating_client_->EnableVideoDecoderFactory();
1013 receiving_client_->EnableVideoDecoderFactory();
1014 }
1015
1016 // This test sets up a call between two parties. Both parties send static
1017 // frames to each other. Once the test is finished the number of sent frames
1018 // is compared to the number of received frames.
1019 void LocalP2PTest() {
1020 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1021 initiating_client_->AddMediaStream(true, true);
1022 }
1023 initiating_client_->Negotiate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001024 // Assert true is used here since next tests are guaranteed to fail and
1025 // would eat up 5 seconds.
1026 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1027 VerifySessionDescriptions();
1028
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029 int audio_frame_count = kEndAudioFrameCount;
1030 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1031 if (!initiating_client_->can_receive_audio() ||
1032 !receiving_client_->can_receive_audio()) {
1033 audio_frame_count = -1;
1034 }
1035 int video_frame_count = kEndVideoFrameCount;
1036 if (!initiating_client_->can_receive_video() ||
1037 !receiving_client_->can_receive_video()) {
1038 video_frame_count = -1;
1039 }
1040
1041 if (audio_frame_count != -1 || video_frame_count != -1) {
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001042 // Audio or video is expected to flow, so both clients should reach the
1043 // Connected state, and the offerer (ICE controller) should proceed to
1044 // Completed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045 // Note: These tests have been observed to fail under heavy load at
1046 // shorter timeouts, so they may be flaky.
1047 EXPECT_EQ_WAIT(
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001048 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049 initiating_client_->ice_connection_state(),
1050 kMaxWaitForFramesMs);
1051 EXPECT_EQ_WAIT(
1052 webrtc::PeerConnectionInterface::kIceConnectionConnected,
1053 receiving_client_->ice_connection_state(),
1054 kMaxWaitForFramesMs);
1055 }
1056
1057 if (initiating_client_->can_receive_audio() ||
1058 initiating_client_->can_receive_video()) {
1059 // The initiating client can receive media, so it must produce candidates
1060 // that will serve as destinations for that media.
1061 // TODO(bemasc): Understand why the state is not already Complete here, as
1062 // seems to be the case for the receiving client. This may indicate a bug
1063 // in the ICE gathering system.
1064 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1065 initiating_client_->ice_gathering_state());
1066 }
1067 if (receiving_client_->can_receive_audio() ||
1068 receiving_client_->can_receive_video()) {
1069 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1070 receiving_client_->ice_gathering_state(),
1071 kMaxWaitForFramesMs);
1072 }
1073
1074 EXPECT_TRUE_WAIT(FramesNotPending(audio_frame_count, video_frame_count),
1075 kMaxWaitForFramesMs);
1076 }
1077
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001078 void SetupAndVerifyDtlsCall() {
1079 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1080 FakeConstraints setup_constraints;
1081 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1082 true);
1083 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1084 LocalP2PTest();
1085 VerifyRenderedSize(640, 480);
1086 }
1087
1088 PeerConnectionTestClient* CreateDtlsClientWithAlternateKey() {
1089 FakeConstraints setup_constraints;
1090 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1091 true);
1092
1093 rtc::scoped_ptr<FakeDtlsIdentityStore> dtls_identity_store(
1094 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
1095 : nullptr);
1096 dtls_identity_store->use_alternate_key();
1097
1098 // Make sure the new client is using a different certificate.
1099 return PeerConnectionTestClient::CreateClientWithDtlsIdentityStore(
1100 "New Peer: ", &setup_constraints, nullptr, dtls_identity_store.Pass());
1101 }
1102
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001103 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1104 // Messages may get lost on the unreliable DataChannel, so we send multiple
1105 // times to avoid test flakiness.
1106 static const size_t kSendAttempts = 5;
1107
1108 for (size_t i = 0; i < kSendAttempts; ++i) {
1109 dc->Send(DataBuffer(data));
1110 }
1111 }
1112
deadbeefaf1b59c2015-10-15 12:08:41 -07001113 PeerConnectionTestClient* initializing_client() {
1114 return initiating_client_.get();
1115 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001116
1117 // Set the |initiating_client_| to the |client| passed in and return the
1118 // original |initiating_client_|.
1119 PeerConnectionTestClient* set_initializing_client(
1120 PeerConnectionTestClient* client) {
1121 PeerConnectionTestClient* old = initiating_client_.release();
1122 initiating_client_.reset(client);
1123 return old;
1124 }
1125
deadbeefaf1b59c2015-10-15 12:08:41 -07001126 PeerConnectionTestClient* receiving_client() {
1127 return receiving_client_.get();
1128 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001130 // Set the |receiving_client_| to the |client| passed in and return the
1131 // original |receiving_client_|.
1132 PeerConnectionTestClient* set_receiving_client(
1133 PeerConnectionTestClient* client) {
1134 PeerConnectionTestClient* old = receiving_client_.release();
1135 receiving_client_.reset(client);
1136 return old;
1137 }
1138
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001139 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001140 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1141 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1142 rtc::SocketServerScope ss_scope_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001143 rtc::scoped_ptr<PeerConnectionTestClient> initiating_client_;
1144 rtc::scoped_ptr<PeerConnectionTestClient> receiving_client_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001145};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001146
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001147// Disable for TSan v2, see
1148// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1149#if !defined(THREAD_SANITIZER)
1150
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001151// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001152// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1153// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001154TEST_F(P2PTestConductor, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001155 ASSERT_TRUE(CreateTestClients());
1156 LocalP2PTest();
1157 VerifyDtmf();
1158}
1159
1160// This test sets up a Jsep call between two parties and test that we can get a
1161// video aspect ratio of 16:9.
deadbeef7c73bdb2015-12-10 15:10:44 -08001162TEST_F(P2PTestConductor, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001163 ASSERT_TRUE(CreateTestClients());
1164 FakeConstraints constraint;
1165 double requested_ratio = 640.0/360;
1166 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1167 SetVideoConstraints(constraint, constraint);
1168 LocalP2PTest();
1169
1170 ASSERT_LE(0, initializing_client()->rendered_height());
1171 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001172 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001173 initializing_client()->rendered_height();
1174 EXPECT_LE(requested_ratio, initiating_video_ratio);
1175
1176 ASSERT_LE(0, receiving_client()->rendered_height());
1177 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001178 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001179 receiving_client()->rendered_height();
1180 EXPECT_LE(requested_ratio, receiving_video_ratio);
1181}
1182
1183// This test sets up a Jsep call between two parties and test that the
1184// received video has a resolution of 1280*720.
1185// TODO(mallinath): Enable when
1186// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
deadbeef7c73bdb2015-12-10 15:10:44 -08001187TEST_F(P2PTestConductor, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001188 ASSERT_TRUE(CreateTestClients());
1189 FakeConstraints constraint;
1190 constraint.SetMandatoryMinWidth(1280);
1191 constraint.SetMandatoryMinHeight(720);
1192 SetVideoConstraints(constraint, constraint);
1193 LocalP2PTest();
1194 VerifyRenderedSize(1280, 720);
1195}
1196
1197// This test sets up a call between two endpoints that are configured to use
1198// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001199TEST_F(P2PTestConductor, LocalP2PTestDtls) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001200 SetupAndVerifyDtlsCall();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001201}
1202
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001203// This test sets up a audio call initially and then upgrades to audio/video,
1204// using DTLS.
deadbeef7c73bdb2015-12-10 15:10:44 -08001205TEST_F(P2PTestConductor, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001206 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001207 FakeConstraints setup_constraints;
1208 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1209 true);
1210 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1211 receiving_client()->SetReceiveAudioVideo(true, false);
1212 LocalP2PTest();
1213 receiving_client()->SetReceiveAudioVideo(true, true);
1214 receiving_client()->Negotiate();
1215}
1216
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001217// This test sets up a call transfer to a new caller with a different DTLS
1218// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001219TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCallee) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001220 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1221 SetupAndVerifyDtlsCall();
1222
1223 // Keeping the original peer around which will still send packets to the
1224 // receiving client. These SRTP packets will be dropped.
1225 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1226 set_initializing_client(CreateDtlsClientWithAlternateKey()));
1227 original_peer->pc()->Close();
1228
1229 SetSignalingReceivers();
1230 receiving_client()->SetExpectIceRestart(true);
1231 LocalP2PTest();
1232 VerifyRenderedSize(640, 480);
1233}
1234
1235// This test sets up a call transfer to a new callee with a different DTLS
1236// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001237TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCaller) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001238 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1239 SetupAndVerifyDtlsCall();
1240
1241 // Keeping the original peer around which will still send packets to the
1242 // receiving client. These SRTP packets will be dropped.
1243 rtc::scoped_ptr<PeerConnectionTestClient> original_peer(
1244 set_receiving_client(CreateDtlsClientWithAlternateKey()));
1245 original_peer->pc()->Close();
1246
1247 SetSignalingReceivers();
1248 initializing_client()->IceRestart();
1249 LocalP2PTest();
1250 VerifyRenderedSize(640, 480);
1251}
1252
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253// This test sets up a call between two endpoints that are configured to use
1254// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1255// negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001256TEST_F(P2PTestConductor, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001257 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001258 FakeConstraints setup_constraints;
1259 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1260 true);
1261 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1262 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1263 LocalP2PTest();
1264 VerifyRenderedSize(640, 480);
1265}
1266
1267// This test sets up a Jsep call between two parties, and the callee only
1268// accept to receive video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001269TEST_F(P2PTestConductor, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001270 ASSERT_TRUE(CreateTestClients());
1271 receiving_client()->SetReceiveAudioVideo(false, true);
1272 LocalP2PTest();
1273}
1274
1275// This test sets up a Jsep call between two parties, and the callee only
1276// accept to receive audio.
deadbeef7c73bdb2015-12-10 15:10:44 -08001277TEST_F(P2PTestConductor, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001278 ASSERT_TRUE(CreateTestClients());
1279 receiving_client()->SetReceiveAudioVideo(true, false);
1280 LocalP2PTest();
1281}
1282
1283// This test sets up a Jsep call between two parties, and the callee reject both
1284// audio and video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001285TEST_F(P2PTestConductor, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001286 ASSERT_TRUE(CreateTestClients());
1287 receiving_client()->SetReceiveAudioVideo(false, false);
1288 LocalP2PTest();
1289}
1290
1291// This test sets up an audio and video call between two parties. After the call
1292// runs for a while (10 frames), the caller sends an update offer with video
1293// being rejected. Once the re-negotiation is done, the video flow should stop
1294// and the audio flow should continue.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001295// Disabled due to b/14955157.
deadbeef7c73bdb2015-12-10 15:10:44 -08001296TEST_F(P2PTestConductor, DISABLED_UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 ASSERT_TRUE(CreateTestClients());
1298 LocalP2PTest();
1299 TestUpdateOfferWithRejectedContent();
1300}
1301
1302// This test sets up a Jsep call between two parties. The MSID is removed from
1303// the SDP strings from the caller.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001304// Disabled due to b/14955157.
deadbeef7c73bdb2015-12-10 15:10:44 -08001305TEST_F(P2PTestConductor, DISABLED_LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001306 ASSERT_TRUE(CreateTestClients());
1307 receiving_client()->RemoveMsidFromReceivedSdp(true);
1308 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1309 // audio and video is muxed when MSID is disabled. Remove
1310 // SetRemoveBundleFromSdp once
1311 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1312 receiving_client()->RemoveBundleFromReceivedSdp(true);
1313 LocalP2PTest();
1314}
1315
1316// This test sets up a Jsep call between two parties and the initiating peer
1317// sends two steams.
1318// TODO(perkj): Disabled due to
1319// https://code.google.com/p/webrtc/issues/detail?id=1454
deadbeef7c73bdb2015-12-10 15:10:44 -08001320TEST_F(P2PTestConductor, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321 ASSERT_TRUE(CreateTestClients());
1322 // Set optional video constraint to max 320pixels to decrease CPU usage.
1323 FakeConstraints constraint;
1324 constraint.SetOptionalMaxWidth(320);
1325 SetVideoConstraints(constraint, constraint);
1326 initializing_client()->AddMediaStream(true, true);
1327 initializing_client()->AddMediaStream(false, true);
1328 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1329 LocalP2PTest();
1330 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1331}
1332
1333// Test that we can receive the audio output level from a remote audio track.
deadbeef7c73bdb2015-12-10 15:10:44 -08001334TEST_F(P2PTestConductor, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001335 ASSERT_TRUE(CreateTestClients());
1336 LocalP2PTest();
1337
1338 StreamCollectionInterface* remote_streams =
1339 initializing_client()->remote_streams();
1340 ASSERT_GT(remote_streams->count(), 0u);
1341 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1342 MediaStreamTrackInterface* remote_audio_track =
1343 remote_streams->at(0)->GetAudioTracks()[0];
1344
1345 // Get the audio output level stats. Note that the level is not available
1346 // until a RTCP packet has been received.
1347 EXPECT_TRUE_WAIT(
1348 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1349 kMaxWaitForStatsMs);
1350}
1351
1352// Test that an audio input level is reported.
deadbeef7c73bdb2015-12-10 15:10:44 -08001353TEST_F(P2PTestConductor, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001354 ASSERT_TRUE(CreateTestClients());
1355 LocalP2PTest();
1356
1357 // Get the audio input level stats. The level should be available very
1358 // soon after the test starts.
1359 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1360 kMaxWaitForStatsMs);
1361}
1362
1363// Test that we can get incoming byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001364TEST_F(P2PTestConductor, GetBytesReceivedStats) {
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 EXPECT_TRUE_WAIT(
1375 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1376 kMaxWaitForStatsMs);
1377
1378 MediaStreamTrackInterface* remote_video_track =
1379 remote_streams->at(0)->GetVideoTracks()[0];
1380 EXPECT_TRUE_WAIT(
1381 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1382 kMaxWaitForStatsMs);
1383}
1384
1385// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001386TEST_F(P2PTestConductor, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001387 ASSERT_TRUE(CreateTestClients());
1388 LocalP2PTest();
1389
1390 StreamCollectionInterface* local_streams =
1391 initializing_client()->local_streams();
1392 ASSERT_GT(local_streams->count(), 0u);
1393 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1394 MediaStreamTrackInterface* local_audio_track =
1395 local_streams->at(0)->GetAudioTracks()[0];
1396 EXPECT_TRUE_WAIT(
1397 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1398 kMaxWaitForStatsMs);
1399
1400 MediaStreamTrackInterface* local_video_track =
1401 local_streams->at(0)->GetVideoTracks()[0];
1402 EXPECT_TRUE_WAIT(
1403 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1404 kMaxWaitForStatsMs);
1405}
1406
Joachim Bauch04e5b492015-05-29 09:40:39 +02001407// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
deadbeef7c73bdb2015-12-10 15:10:44 -08001408TEST_F(P2PTestConductor, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001409 PeerConnectionFactory::Options init_options;
1410 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1411 PeerConnectionFactory::Options recv_options;
1412 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001413 ASSERT_TRUE(
1414 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001415 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1416 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1417 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001418 LocalP2PTest();
1419
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001420 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001421 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1422 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1423 initializing_client()->GetDtlsCipherStats(),
1424 kMaxWaitForStatsMs);
1425 EXPECT_EQ(1, init_observer->GetEnumCounter(
1426 webrtc::kEnumCounterAudioSslCipher,
1427 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1428 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001429
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001430 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001431 initializing_client()->GetSrtpCipherStats(),
1432 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001433 EXPECT_EQ(1,
1434 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1435 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001436}
1437
1438// Test that DTLS 1.2 is used if both ends support it.
deadbeef7c73bdb2015-12-10 15:10:44 -08001439TEST_F(P2PTestConductor, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001440 PeerConnectionFactory::Options init_options;
1441 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1442 PeerConnectionFactory::Options recv_options;
1443 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001444 ASSERT_TRUE(
1445 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001446 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1447 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1448 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001449 LocalP2PTest();
1450
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001451 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001452 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1453 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)),
1454 initializing_client()->GetDtlsCipherStats(),
1455 kMaxWaitForStatsMs);
1456 EXPECT_EQ(1, init_observer->GetEnumCounter(
1457 webrtc::kEnumCounterAudioSslCipher,
1458 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1459 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001460
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001461 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001462 initializing_client()->GetSrtpCipherStats(),
1463 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001464 EXPECT_EQ(1,
1465 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1466 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001467}
1468
1469// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1470// received supports 1.0.
deadbeef7c73bdb2015-12-10 15:10:44 -08001471TEST_F(P2PTestConductor, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001472 PeerConnectionFactory::Options init_options;
1473 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1474 PeerConnectionFactory::Options recv_options;
1475 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001476 ASSERT_TRUE(
1477 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001478 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1479 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1480 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001481 LocalP2PTest();
1482
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001483 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001484 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1485 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1486 initializing_client()->GetDtlsCipherStats(),
1487 kMaxWaitForStatsMs);
1488 EXPECT_EQ(1, init_observer->GetEnumCounter(
1489 webrtc::kEnumCounterAudioSslCipher,
1490 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1491 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001492
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001493 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001494 initializing_client()->GetSrtpCipherStats(),
1495 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001496 EXPECT_EQ(1,
1497 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1498 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001499}
1500
1501// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1502// received supports 1.2.
deadbeef7c73bdb2015-12-10 15:10:44 -08001503TEST_F(P2PTestConductor, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001504 PeerConnectionFactory::Options init_options;
1505 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1506 PeerConnectionFactory::Options recv_options;
1507 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001508 ASSERT_TRUE(
1509 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001510 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1511 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1512 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001513 LocalP2PTest();
1514
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001515 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001516 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1517 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1518 initializing_client()->GetDtlsCipherStats(),
1519 kMaxWaitForStatsMs);
1520 EXPECT_EQ(1, init_observer->GetEnumCounter(
1521 webrtc::kEnumCounterAudioSslCipher,
1522 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1523 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001524
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001525 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001526 initializing_client()->GetSrtpCipherStats(),
1527 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001528 EXPECT_EQ(1,
1529 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1530 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001531}
1532
deadbeefb5cb19b2015-11-23 16:39:12 -08001533// This test sets up a call between two parties with audio, video and an RTP
1534// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001535TEST_F(P2PTestConductor, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001536 FakeConstraints setup_constraints;
1537 setup_constraints.SetAllowRtpDataChannels();
1538 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1539 initializing_client()->CreateDataChannel();
1540 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001541 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1542 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1544 kMaxWaitMs);
1545 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1546 kMaxWaitMs);
1547
1548 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001549
1550 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1552 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001553
1554 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1556 kMaxWaitMs);
1557
1558 receiving_client()->data_channel()->Close();
1559 // Send new offer and answer.
1560 receiving_client()->Negotiate();
1561 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1562 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1563}
1564
deadbeefb5cb19b2015-11-23 16:39:12 -08001565// This test sets up a call between two parties with audio, video and an SCTP
1566// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001567TEST_F(P2PTestConductor, LocalP2PTestSctpDataChannel) {
deadbeefb5cb19b2015-11-23 16:39:12 -08001568 ASSERT_TRUE(CreateTestClients());
1569 initializing_client()->CreateDataChannel();
1570 LocalP2PTest();
1571 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1572 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
1573 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1574 kMaxWaitMs);
1575 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
1576
1577 std::string data = "hello world";
1578
1579 initializing_client()->data_channel()->Send(DataBuffer(data));
1580 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1581 kMaxWaitMs);
1582
1583 receiving_client()->data_channel()->Send(DataBuffer(data));
1584 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1585 kMaxWaitMs);
1586
1587 receiving_client()->data_channel()->Close();
1588 // Send new offer and answer.
1589 receiving_client()->Negotiate();
1590 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1591 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1592}
1593
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594// This test sets up a call between two parties and creates a data channel.
1595// The test tests that received data is buffered unless an observer has been
1596// registered.
1597// Rtp data channels can receive data before the underlying
1598// transport has detected that a channel is writable and thus data can be
1599// received before the data channel state changes to open. That is hard to test
1600// but the same buffering is used in that case.
deadbeef7c73bdb2015-12-10 15:10:44 -08001601TEST_F(P2PTestConductor, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602 FakeConstraints setup_constraints;
1603 setup_constraints.SetAllowRtpDataChannels();
1604 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1605 initializing_client()->CreateDataChannel();
1606 initializing_client()->Negotiate();
1607
deadbeefaf1b59c2015-10-15 12:08:41 -07001608 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1609 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001610 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1611 kMaxWaitMs);
1612 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1613 receiving_client()->data_channel()->state(), kMaxWaitMs);
1614
1615 // Unregister the existing observer.
1616 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001617
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001618 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001619 SendRtpData(initializing_client()->data_channel(), data);
1620
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621 // Wait a while to allow the sent data to arrive before an observer is
1622 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001623 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624
1625 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1626 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1627}
1628
1629// This test sets up a call between two parties with audio, video and but only
1630// the initiating client support data.
deadbeef7c73bdb2015-12-10 15:10:44 -08001631TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001632 FakeConstraints setup_constraints_1;
1633 setup_constraints_1.SetAllowRtpDataChannels();
1634 // Must disable DTLS to make negotiation succeed.
1635 setup_constraints_1.SetMandatory(
1636 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1637 FakeConstraints setup_constraints_2;
1638 setup_constraints_2.SetMandatory(
1639 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1640 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001641 initializing_client()->CreateDataChannel();
1642 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001643 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644 EXPECT_FALSE(receiving_client()->data_channel());
1645 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1646}
1647
1648// This test sets up a call between two parties with audio, video. When audio
1649// and video is setup and flowing and data channel is negotiated.
deadbeef7c73bdb2015-12-10 15:10:44 -08001650TEST_F(P2PTestConductor, AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001651 FakeConstraints setup_constraints;
1652 setup_constraints.SetAllowRtpDataChannels();
1653 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1654 LocalP2PTest();
1655 initializing_client()->CreateDataChannel();
1656 // Send new offer and answer.
1657 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07001658 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1659 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1661 kMaxWaitMs);
1662 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1663 kMaxWaitMs);
1664}
1665
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001666// This test sets up a Jsep call with SCTP DataChannel and verifies the
1667// negotiation is completed without error.
1668#ifdef HAVE_SCTP
deadbeef7c73bdb2015-12-10 15:10:44 -08001669TEST_F(P2PTestConductor, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001670 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001671 FakeConstraints constraints;
1672 constraints.SetMandatory(
1673 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1674 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1675 initializing_client()->CreateDataChannel();
1676 initializing_client()->Negotiate(false, false);
1677}
1678#endif
1679
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001680// This test sets up a call between two parties with audio, and video.
1681// During the call, the initializing side restart ice and the test verifies that
1682// new ice candidates are generated and audio and video still can flow.
deadbeef7c73bdb2015-12-10 15:10:44 -08001683TEST_F(P2PTestConductor, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001684 ASSERT_TRUE(CreateTestClients());
1685
1686 // Negotiate and wait for ice completion and make sure audio and video plays.
1687 LocalP2PTest();
1688
1689 // Create a SDP string of the first audio candidate for both clients.
1690 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1691 initializing_client()->pc()->local_description()->candidates(0);
1692 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1693 receiving_client()->pc()->local_description()->candidates(0);
1694 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1695 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1696 std::string initiator_candidate;
1697 EXPECT_TRUE(
1698 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1699 std::string receiver_candidate;
1700 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1701
1702 // Restart ice on the initializing client.
1703 receiving_client()->SetExpectIceRestart(true);
1704 initializing_client()->IceRestart();
1705
1706 // Negotiate and wait for ice completion again and make sure audio and video
1707 // plays.
1708 LocalP2PTest();
1709
1710 // Create a SDP string of the first audio candidate for both clients again.
1711 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1712 initializing_client()->pc()->local_description()->candidates(0);
1713 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1714 receiving_client()->pc()->local_description()->candidates(0);
1715 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1716 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1717 std::string initiator_candidate_restart;
1718 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1719 &initiator_candidate_restart));
1720 std::string receiver_candidate_restart;
1721 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1722 &receiver_candidate_restart));
1723
1724 // Verify that the first candidates in the local session descriptions has
1725 // changed.
1726 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1727 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1728}
1729
deadbeeffaac4972015-11-12 15:33:07 -08001730// This test sets up a call between two parties with audio, and video.
1731// It then renegotiates setting the video m-line to "port 0", then later
1732// renegotiates again, enabling video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001733TEST_F(P2PTestConductor, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08001734 ASSERT_TRUE(CreateTestClients());
1735
1736 // Do initial negotiation. Will result in video and audio sendonly m-lines.
1737 receiving_client()->set_auto_add_stream(false);
1738 initializing_client()->AddMediaStream(true, true);
1739 initializing_client()->Negotiate();
1740
1741 // Negotiate again, disabling the video m-line (receiving client will
1742 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
1743 receiving_client()->SetReceiveVideo(false);
1744 initializing_client()->Negotiate();
1745
1746 // Enable video and do negotiation again, making sure video is received
1747 // end-to-end.
1748 receiving_client()->SetReceiveVideo(true);
1749 receiving_client()->AddMediaStream(true, true);
1750 LocalP2PTest();
1751}
1752
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753// This test sets up a Jsep call between two parties with external
1754// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001755// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1756// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001757TEST_F(P2PTestConductor, DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001758 ASSERT_TRUE(CreateTestClients());
1759 EnableVideoDecoderFactory();
1760 LocalP2PTest();
1761}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001762
deadbeeffac06552015-11-25 11:26:01 -08001763// This tests that if we negotiate after calling CreateSender but before we
1764// have a track, then set a track later, frames from the newly-set track are
1765// received end-to-end.
deadbeef7c73bdb2015-12-10 15:10:44 -08001766TEST_F(P2PTestConductor, EarlyWarmupTest) {
deadbeeffac06552015-11-25 11:26:01 -08001767 ASSERT_TRUE(CreateTestClients());
1768 auto audio_sender = initializing_client()->pc()->CreateSender("audio");
1769 auto video_sender = initializing_client()->pc()->CreateSender("video");
1770 initializing_client()->Negotiate();
1771 // Wait for ICE connection to complete, without any tracks.
1772 // Note that the receiving client WILL (in HandleIncomingOffer) create
1773 // tracks, so it's only the initiator here that's doing early warmup.
1774 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1775 VerifySessionDescriptions();
1776 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1777 initializing_client()->ice_connection_state(),
1778 kMaxWaitForFramesMs);
1779 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1780 receiving_client()->ice_connection_state(),
1781 kMaxWaitForFramesMs);
1782 // Now set the tracks, and expect frames to immediately start flowing.
1783 EXPECT_TRUE(
1784 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
1785 EXPECT_TRUE(
1786 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
1787 EXPECT_TRUE_WAIT(FramesNotPending(kEndAudioFrameCount, kEndVideoFrameCount),
1788 kMaxWaitForFramesMs);
1789}
1790
deadbeef0a6c4ca2015-10-06 11:38:28 -07001791class IceServerParsingTest : public testing::Test {
1792 public:
1793 // Convenience for parsing a single URL.
1794 bool ParseUrl(const std::string& url) {
1795 return ParseUrl(url, std::string(), std::string());
1796 }
1797
1798 bool ParseUrl(const std::string& url,
1799 const std::string& username,
1800 const std::string& password) {
1801 PeerConnectionInterface::IceServers servers;
1802 PeerConnectionInterface::IceServer server;
1803 server.urls.push_back(url);
1804 server.username = username;
1805 server.password = password;
1806 servers.push_back(server);
1807 return webrtc::ParseIceServers(servers, &stun_configurations_,
1808 &turn_configurations_);
1809 }
1810
1811 protected:
1812 webrtc::StunConfigurations stun_configurations_;
1813 webrtc::TurnConfigurations turn_configurations_;
1814};
1815
1816// Make sure all STUN/TURN prefixes are parsed correctly.
1817TEST_F(IceServerParsingTest, ParseStunPrefixes) {
1818 EXPECT_TRUE(ParseUrl("stun:hostname"));
1819 EXPECT_EQ(1U, stun_configurations_.size());
1820 EXPECT_EQ(0U, turn_configurations_.size());
1821 stun_configurations_.clear();
1822
1823 EXPECT_TRUE(ParseUrl("stuns:hostname"));
1824 EXPECT_EQ(1U, stun_configurations_.size());
1825 EXPECT_EQ(0U, turn_configurations_.size());
1826 stun_configurations_.clear();
1827
1828 EXPECT_TRUE(ParseUrl("turn:hostname"));
1829 EXPECT_EQ(0U, stun_configurations_.size());
1830 EXPECT_EQ(1U, turn_configurations_.size());
1831 EXPECT_FALSE(turn_configurations_[0].secure);
1832 turn_configurations_.clear();
1833
1834 EXPECT_TRUE(ParseUrl("turns:hostname"));
1835 EXPECT_EQ(0U, stun_configurations_.size());
1836 EXPECT_EQ(1U, turn_configurations_.size());
1837 EXPECT_TRUE(turn_configurations_[0].secure);
1838 turn_configurations_.clear();
1839
1840 // invalid prefixes
1841 EXPECT_FALSE(ParseUrl("stunn:hostname"));
1842 EXPECT_FALSE(ParseUrl(":hostname"));
1843 EXPECT_FALSE(ParseUrl(":"));
1844 EXPECT_FALSE(ParseUrl(""));
1845}
1846
1847TEST_F(IceServerParsingTest, VerifyDefaults) {
1848 // TURNS defaults
1849 EXPECT_TRUE(ParseUrl("turns:hostname"));
1850 EXPECT_EQ(1U, turn_configurations_.size());
1851 EXPECT_EQ(5349, turn_configurations_[0].server.port());
1852 EXPECT_EQ("tcp", turn_configurations_[0].transport_type);
1853 turn_configurations_.clear();
1854
1855 // TURN defaults
1856 EXPECT_TRUE(ParseUrl("turn:hostname"));
1857 EXPECT_EQ(1U, turn_configurations_.size());
1858 EXPECT_EQ(3478, turn_configurations_[0].server.port());
1859 EXPECT_EQ("udp", turn_configurations_[0].transport_type);
1860 turn_configurations_.clear();
1861
1862 // STUN defaults
1863 EXPECT_TRUE(ParseUrl("stun:hostname"));
1864 EXPECT_EQ(1U, stun_configurations_.size());
1865 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1866 stun_configurations_.clear();
1867}
1868
1869// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
1870// can be parsed correctly.
1871TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
1872 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
1873 EXPECT_EQ(1U, stun_configurations_.size());
1874 EXPECT_EQ("1.2.3.4", stun_configurations_[0].server.hostname());
1875 EXPECT_EQ(1234, stun_configurations_[0].server.port());
1876 stun_configurations_.clear();
1877
1878 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
1879 EXPECT_EQ(1U, stun_configurations_.size());
1880 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_configurations_[0].server.hostname());
1881 EXPECT_EQ(4321, stun_configurations_[0].server.port());
1882 stun_configurations_.clear();
1883
1884 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
1885 EXPECT_EQ(1U, stun_configurations_.size());
1886 EXPECT_EQ("hostname", stun_configurations_[0].server.hostname());
1887 EXPECT_EQ(9999, stun_configurations_[0].server.port());
1888 stun_configurations_.clear();
1889
1890 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
1891 EXPECT_EQ(1U, stun_configurations_.size());
1892 EXPECT_EQ("1.2.3.4", stun_configurations_[0].server.hostname());
1893 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1894 stun_configurations_.clear();
1895
1896 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
1897 EXPECT_EQ(1U, stun_configurations_.size());
1898 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_configurations_[0].server.hostname());
1899 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1900 stun_configurations_.clear();
1901
1902 EXPECT_TRUE(ParseUrl("stun:hostname"));
1903 EXPECT_EQ(1U, stun_configurations_.size());
1904 EXPECT_EQ("hostname", stun_configurations_[0].server.hostname());
1905 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1906 stun_configurations_.clear();
1907
1908 // Try some invalid hostname:port strings.
1909 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
1910 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
1911 EXPECT_FALSE(ParseUrl("stun:hostname:"));
1912 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
1913 EXPECT_FALSE(ParseUrl("stun::5555"));
1914 EXPECT_FALSE(ParseUrl("stun:"));
1915}
1916
1917// Test parsing the "?transport=xxx" part of the URL.
1918TEST_F(IceServerParsingTest, ParseTransport) {
1919 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
1920 EXPECT_EQ(1U, turn_configurations_.size());
1921 EXPECT_EQ("tcp", turn_configurations_[0].transport_type);
1922 turn_configurations_.clear();
1923
1924 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
1925 EXPECT_EQ(1U, turn_configurations_.size());
1926 EXPECT_EQ("udp", turn_configurations_[0].transport_type);
1927 turn_configurations_.clear();
1928
1929 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
1930}
1931
1932// Test parsing ICE username contained in URL.
1933TEST_F(IceServerParsingTest, ParseUsername) {
1934 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
1935 EXPECT_EQ(1U, turn_configurations_.size());
1936 EXPECT_EQ("user", turn_configurations_[0].username);
1937 turn_configurations_.clear();
1938
1939 EXPECT_FALSE(ParseUrl("turn:@hostname"));
1940 EXPECT_FALSE(ParseUrl("turn:username@"));
1941 EXPECT_FALSE(ParseUrl("turn:@"));
1942 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
1943}
1944
1945// Test that username and password from IceServer is copied into the resulting
1946// TurnConfiguration.
1947TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
1948 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
1949 EXPECT_EQ(1U, turn_configurations_.size());
1950 EXPECT_EQ("username", turn_configurations_[0].username);
1951 EXPECT_EQ("password", turn_configurations_[0].password);
1952}
1953
1954// Ensure that if a server has multiple URLs, each one is parsed.
1955TEST_F(IceServerParsingTest, ParseMultipleUrls) {
1956 PeerConnectionInterface::IceServers servers;
1957 PeerConnectionInterface::IceServer server;
1958 server.urls.push_back("stun:hostname");
1959 server.urls.push_back("turn:hostname");
1960 servers.push_back(server);
1961 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_configurations_,
1962 &turn_configurations_));
1963 EXPECT_EQ(1U, stun_configurations_.size());
1964 EXPECT_EQ(1U, turn_configurations_.size());
1965}
1966
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001967#endif // if !defined(THREAD_SANITIZER)