blob: 6dc83a575132d889462568b9f5abd6b8b6e4fab8 [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
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +0000101static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102static const int kEndAudioFrameCount = 3;
103static const int kEndVideoFrameCount = 3;
104
105static const char kStreamLabelBase[] = "stream_label";
106static const char kVideoTrackLabelBase[] = "video_track";
107static const char kAudioTrackLabelBase[] = "audio_track";
108static const char kDataChannelLabel[] = "data_channel";
109
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000110// Disable for TSan v2, see
111// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
112// This declaration is also #ifdef'd as it causes unused-variable errors.
113#if !defined(THREAD_SANITIZER)
114// SRTP cipher name negotiated by the tests. This must be updated if the
115// default changes.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800116static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000117#endif
118
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119static void RemoveLinesFromSdp(const std::string& line_start,
120 std::string* sdp) {
121 const char kSdpLineEnd[] = "\r\n";
122 size_t ssrc_pos = 0;
123 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
124 std::string::npos) {
125 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
126 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
127 }
128}
129
130class SignalingMessageReceiver {
131 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 virtual void ReceiveSdpMessage(const std::string& type,
133 std::string& msg) = 0;
134 virtual void ReceiveIceMessage(const std::string& sdp_mid,
135 int sdp_mline_index,
136 const std::string& msg) = 0;
137
138 protected:
deadbeefaf1b59c2015-10-15 12:08:41 -0700139 SignalingMessageReceiver() {}
140 virtual ~SignalingMessageReceiver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141};
142
deadbeefaf1b59c2015-10-15 12:08:41 -0700143class PeerConnectionTestClient : public webrtc::PeerConnectionObserver,
deadbeeffaac4972015-11-12 15:33:07 -0800144 public SignalingMessageReceiver,
145 public ObserverInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 public:
deadbeefaf1b59c2015-10-15 12:08:41 -0700147 static PeerConnectionTestClient* CreateClient(
148 const std::string& id,
149 const MediaConstraintsInterface* constraints,
150 const PeerConnectionFactory::Options* options) {
151 PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
152 if (!client->Init(constraints, options)) {
153 delete client;
154 return nullptr;
155 }
156 return client;
157 }
158
159 ~PeerConnectionTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160 while (!fake_video_renderers_.empty()) {
161 RenderMap::iterator it = fake_video_renderers_.begin();
162 delete it->second;
163 fake_video_renderers_.erase(it);
164 }
165 }
166
deadbeefaf1b59c2015-10-15 12:08:41 -0700167 void Negotiate() { Negotiate(true, true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168
deadbeefaf1b59c2015-10-15 12:08:41 -0700169 void Negotiate(bool audio, bool video) {
170 rtc::scoped_ptr<SessionDescriptionInterface> offer;
171 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172
deadbeefaf1b59c2015-10-15 12:08:41 -0700173 if (offer->description()->GetContentByName("audio")) {
174 offer->description()->GetContentByName("audio")->rejected = !audio;
175 }
176 if (offer->description()->GetContentByName("video")) {
177 offer->description()->GetContentByName("video")->rejected = !video;
178 }
179
180 std::string sdp;
181 EXPECT_TRUE(offer->ToString(&sdp));
182 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
183 signaling_message_receiver_->ReceiveSdpMessage(
184 webrtc::SessionDescriptionInterface::kOffer, sdp);
185 }
186
187 // SignalingMessageReceiver callback.
188 void ReceiveSdpMessage(const std::string& type, std::string& msg) override {
189 FilterIncomingSdpMessage(&msg);
190 if (type == webrtc::SessionDescriptionInterface::kOffer) {
191 HandleIncomingOffer(msg);
192 } else {
193 HandleIncomingAnswer(msg);
194 }
195 }
196
197 // SignalingMessageReceiver callback.
198 void ReceiveIceMessage(const std::string& sdp_mid,
199 int sdp_mline_index,
200 const std::string& msg) override {
201 LOG(INFO) << id_ << "ReceiveIceMessage";
202 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
203 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
204 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
205 }
206
207 // PeerConnectionObserver callbacks.
208 void OnSignalingChange(
209 webrtc::PeerConnectionInterface::SignalingState new_state) override {
210 EXPECT_EQ(pc()->signaling_state(), new_state);
211 }
deadbeeffaac4972015-11-12 15:33:07 -0800212 void OnAddStream(MediaStreamInterface* media_stream) override {
213 media_stream->RegisterObserver(this);
deadbeefaf1b59c2015-10-15 12:08:41 -0700214 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
215 const std::string id = media_stream->GetVideoTracks()[i]->id();
216 ASSERT_TRUE(fake_video_renderers_.find(id) ==
217 fake_video_renderers_.end());
218 fake_video_renderers_[id] =
219 new webrtc::FakeVideoTrackRenderer(media_stream->GetVideoTracks()[i]);
220 }
221 }
deadbeeffaac4972015-11-12 15:33:07 -0800222 void OnRemoveStream(MediaStreamInterface* media_stream) override {}
deadbeefaf1b59c2015-10-15 12:08:41 -0700223 void OnRenegotiationNeeded() override {}
224 void OnIceConnectionChange(
225 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
226 EXPECT_EQ(pc()->ice_connection_state(), new_state);
227 }
228 void OnIceGatheringChange(
229 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
230 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
231 }
232 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
233 LOG(INFO) << id_ << "OnIceCandidate";
234
235 std::string ice_sdp;
236 EXPECT_TRUE(candidate->ToString(&ice_sdp));
237 if (signaling_message_receiver_ == nullptr) {
238 // Remote party may be deleted.
239 return;
240 }
241 signaling_message_receiver_->ReceiveIceMessage(
242 candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
243 }
244
deadbeeffaac4972015-11-12 15:33:07 -0800245 // MediaStreamInterface callback
246 void OnChanged() override {
247 // Track added or removed from MediaStream, so update our renderers.
248 rtc::scoped_refptr<StreamCollectionInterface> remote_streams =
249 pc()->remote_streams();
250 // Remove renderers for tracks that were removed.
251 for (auto it = fake_video_renderers_.begin();
252 it != fake_video_renderers_.end();) {
253 if (remote_streams->FindVideoTrack(it->first) == nullptr) {
254 auto to_delete = it++;
255 delete to_delete->second;
256 fake_video_renderers_.erase(to_delete);
257 } else {
258 ++it;
259 }
260 }
261 // Create renderers for new video tracks.
262 for (size_t stream_index = 0; stream_index < remote_streams->count();
263 ++stream_index) {
264 MediaStreamInterface* remote_stream = remote_streams->at(stream_index);
265 for (size_t track_index = 0;
266 track_index < remote_stream->GetVideoTracks().size();
267 ++track_index) {
268 const std::string id =
269 remote_stream->GetVideoTracks()[track_index]->id();
270 if (fake_video_renderers_.find(id) != fake_video_renderers_.end()) {
271 continue;
272 }
273 fake_video_renderers_[id] = new webrtc::FakeVideoTrackRenderer(
274 remote_stream->GetVideoTracks()[track_index]);
275 }
276 }
277 }
278
deadbeefaf1b59c2015-10-15 12:08:41 -0700279 void SetVideoConstraints(const webrtc::FakeConstraints& video_constraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 video_constraints_ = video_constraint;
281 }
282
283 void AddMediaStream(bool audio, bool video) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700284 std::string stream_label =
285 kStreamLabelBase +
286 rtc::ToString<int>(static_cast<int>(pc()->local_streams()->count()));
deadbeeffaac4972015-11-12 15:33:07 -0800287 rtc::scoped_refptr<MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000288 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289
290 if (audio && can_receive_audio()) {
deadbeef5def7b92015-11-20 11:43:22 -0800291 FakeConstraints constraints;
292 // Disable highpass filter so that we can get all the test audio frames.
293 constraints.AddMandatory(
294 MediaConstraintsInterface::kHighpassFilter, false);
295 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
296 peer_connection_factory_->CreateAudioSource(&constraints);
297 // TODO(perkj): Test audio source when it is implemented. Currently audio
298 // always use the default input.
299 std::string label = stream_label + kAudioTrackLabelBase;
300 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
301 peer_connection_factory_->CreateAudioTrack(label, source));
302 stream->AddTrack(audio_track);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303 }
304 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000305 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306 }
307
deadbeefaf1b59c2015-10-15 12:08:41 -0700308 EXPECT_TRUE(pc()->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 }
310
deadbeefaf1b59c2015-10-15 12:08:41 -0700311 size_t NumberOfLocalMediaStreams() { return pc()->local_streams()->count(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312
313 bool SessionActive() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700314 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315 }
316
deadbeeffaac4972015-11-12 15:33:07 -0800317 // Automatically add a stream when receiving an offer, if we don't have one.
318 // Defaults to true.
319 void set_auto_add_stream(bool auto_add_stream) {
320 auto_add_stream_ = auto_add_stream;
321 }
322
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323 void set_signaling_message_receiver(
deadbeefaf1b59c2015-10-15 12:08:41 -0700324 SignalingMessageReceiver* signaling_message_receiver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325 signaling_message_receiver_ = signaling_message_receiver;
326 }
327
328 void EnableVideoDecoderFactory() {
329 video_decoder_factory_enabled_ = true;
330 fake_video_decoder_factory_->AddSupportedVideoCodecType(
331 webrtc::kVideoCodecVP8);
332 }
333
deadbeefaf1b59c2015-10-15 12:08:41 -0700334 void IceRestart() {
335 session_description_constraints_.SetMandatoryIceRestart(true);
336 SetExpectIceRestart(true);
337 }
338
339 void SetExpectIceRestart(bool expect_restart) {
340 expect_ice_restart_ = expect_restart;
341 }
342
343 bool ExpectIceRestart() const { return expect_ice_restart_; }
344
345 void SetReceiveAudioVideo(bool audio, bool video) {
346 SetReceiveAudio(audio);
347 SetReceiveVideo(video);
348 ASSERT_EQ(audio, can_receive_audio());
349 ASSERT_EQ(video, can_receive_video());
350 }
351
352 void SetReceiveAudio(bool audio) {
353 if (audio && can_receive_audio())
354 return;
355 session_description_constraints_.SetMandatoryReceiveAudio(audio);
356 }
357
358 void SetReceiveVideo(bool video) {
359 if (video && can_receive_video())
360 return;
361 session_description_constraints_.SetMandatoryReceiveVideo(video);
362 }
363
364 void RemoveMsidFromReceivedSdp(bool remove) { remove_msid_ = remove; }
365
366 void RemoveSdesCryptoFromReceivedSdp(bool remove) { remove_sdes_ = remove; }
367
368 void RemoveBundleFromReceivedSdp(bool remove) { remove_bundle_ = remove; }
369
370 bool can_receive_audio() {
371 bool value;
372 if (webrtc::FindConstraint(&session_description_constraints_,
373 MediaConstraintsInterface::kOfferToReceiveAudio,
374 &value, nullptr)) {
375 return value;
376 }
377 return true;
378 }
379
380 bool can_receive_video() {
381 bool value;
382 if (webrtc::FindConstraint(&session_description_constraints_,
383 MediaConstraintsInterface::kOfferToReceiveVideo,
384 &value, nullptr)) {
385 return value;
386 }
387 return true;
388 }
389
390 void OnIceComplete() override { LOG(INFO) << id_ << "OnIceComplete"; }
391
392 void OnDataChannel(DataChannelInterface* data_channel) override {
393 LOG(INFO) << id_ << "OnDataChannel";
394 data_channel_ = data_channel;
395 data_observer_.reset(new MockDataChannelObserver(data_channel));
396 }
397
398 void CreateDataChannel() {
399 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, nullptr);
400 ASSERT_TRUE(data_channel_.get() != nullptr);
401 data_observer_.reset(new MockDataChannelObserver(data_channel_));
402 }
403
404 DataChannelInterface* data_channel() { return data_channel_; }
405 const MockDataChannelObserver* data_observer() const {
406 return data_observer_.get();
407 }
408
409 webrtc::PeerConnectionInterface* pc() { return peer_connection_.get(); }
410
411 void StopVideoCapturers() {
412 for (std::vector<cricket::VideoCapturer*>::iterator it =
413 video_capturers_.begin();
414 it != video_capturers_.end(); ++it) {
415 (*it)->Stop();
416 }
417 }
418
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 bool AudioFramesReceivedCheck(int number_of_frames) const {
420 return number_of_frames <= fake_audio_capture_module_->frames_received();
421 }
422
423 bool VideoFramesReceivedCheck(int number_of_frames) {
424 if (video_decoder_factory_enabled_) {
425 const std::vector<FakeWebRtcVideoDecoder*>& decoders
426 = fake_video_decoder_factory_->decoders();
427 if (decoders.empty()) {
428 return number_of_frames <= 0;
429 }
430
431 for (std::vector<FakeWebRtcVideoDecoder*>::const_iterator
432 it = decoders.begin(); it != decoders.end(); ++it) {
433 if (number_of_frames > (*it)->GetNumFramesReceived()) {
434 return false;
435 }
436 }
437 return true;
438 } else {
439 if (fake_video_renderers_.empty()) {
440 return number_of_frames <= 0;
441 }
442
443 for (RenderMap::const_iterator it = fake_video_renderers_.begin();
444 it != fake_video_renderers_.end(); ++it) {
445 if (number_of_frames > it->second->num_rendered_frames()) {
446 return false;
447 }
448 }
449 return true;
450 }
451 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700452
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000453 // Verify the CreateDtmfSender interface
454 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000455 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
456 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457
458 // We can't create a DTMF sender with an invalid audio track or a non local
459 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700460 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000461 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700462 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
463 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464
465 // We should be able to create a DTMF sender from a local track.
466 webrtc::AudioTrackInterface* localtrack =
467 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
468 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700469 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470 dtmf_sender->RegisterObserver(observer.get());
471
472 // Test the DtmfSender object just created.
473 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
474 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
475
476 // We don't need to verify that the DTMF tones are actually sent out because
477 // that is already covered by the tests of the lower level components.
478
479 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
480 std::vector<std::string> tones;
481 tones.push_back("1");
482 tones.push_back("a");
483 tones.push_back("");
484 observer->Verify(tones);
485
486 dtmf_sender->UnregisterObserver();
487 }
488
489 // Verifies that the SessionDescription have rejected the appropriate media
490 // content.
491 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700492 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
493 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 const cricket::SessionDescription* remote_desc =
495 peer_connection_->remote_description()->description();
496 const cricket::SessionDescription* local_desc =
497 peer_connection_->local_description()->description();
498
499 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
500 if (remote_audio_content) {
501 const ContentInfo* audio_content =
502 GetFirstAudioContent(local_desc);
503 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
504 }
505
506 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
507 if (remote_video_content) {
508 const ContentInfo* video_content =
509 GetFirstVideoContent(local_desc);
510 EXPECT_EQ(can_receive_video(), !video_content->rejected);
511 }
512 }
513
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700515 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000516 const cricket::SessionDescription* desc =
517 peer_connection_->local_description()->description();
518 const cricket::ContentInfos& contents = desc->contents();
519
520 for (size_t index = 0; index < contents.size(); ++index) {
521 if (contents[index].rejected)
522 continue;
523 const cricket::TransportDescription* transport_desc =
524 desc->GetTransportDescriptionByName(contents[index].name);
525
526 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000527 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 if (ufragpair_it == ice_ufrag_pwd_.end()) {
529 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000530 ice_ufrag_pwd_[static_cast<int>(index)] =
531 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532 } else if (ExpectIceRestart()) {
533 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
534 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
535 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
536 } else {
537 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
538 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
539 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
540 }
541 }
542 }
543
544 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000545 rtc::scoped_refptr<MockStatsObserver>
546 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000547 EXPECT_TRUE(peer_connection_->GetStats(
548 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700550 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551 return observer->AudioOutputLevel();
552 }
553
554 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000555 rtc::scoped_refptr<MockStatsObserver>
556 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000557 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700558 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700560 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 return observer->AudioInputLevel();
562 }
563
564 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000565 rtc::scoped_refptr<MockStatsObserver>
566 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000567 EXPECT_TRUE(peer_connection_->GetStats(
568 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700570 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 return observer->BytesReceived();
572 }
573
574 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000575 rtc::scoped_refptr<MockStatsObserver>
576 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000577 EXPECT_TRUE(peer_connection_->GetStats(
578 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700580 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 return observer->BytesSent();
582 }
583
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000584 int GetAvailableReceivedBandwidthStats() {
585 rtc::scoped_refptr<MockStatsObserver>
586 observer(new rtc::RefCountedObject<MockStatsObserver>());
587 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700588 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000589 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700590 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000591 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000592 return bw;
593 }
594
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000595 std::string GetDtlsCipherStats() {
596 rtc::scoped_refptr<MockStatsObserver>
597 observer(new rtc::RefCountedObject<MockStatsObserver>());
598 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700599 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000600 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700601 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000602 return observer->DtlsCipher();
603 }
604
605 std::string GetSrtpCipherStats() {
606 rtc::scoped_refptr<MockStatsObserver>
607 observer(new rtc::RefCountedObject<MockStatsObserver>());
608 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700609 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000610 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700611 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000612 return observer->SrtpCipher();
613 }
614
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000615 int rendered_width() {
616 EXPECT_FALSE(fake_video_renderers_.empty());
617 return fake_video_renderers_.empty() ? 1 :
618 fake_video_renderers_.begin()->second->width();
619 }
620
621 int rendered_height() {
622 EXPECT_FALSE(fake_video_renderers_.empty());
623 return fake_video_renderers_.empty() ? 1 :
624 fake_video_renderers_.begin()->second->height();
625 }
626
627 size_t number_of_remote_streams() {
628 if (!pc())
629 return 0;
630 return pc()->remote_streams()->count();
631 }
632
633 StreamCollectionInterface* remote_streams() {
634 if (!pc()) {
635 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700636 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 }
638 return pc()->remote_streams();
639 }
640
641 StreamCollectionInterface* local_streams() {
642 if (!pc()) {
643 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700644 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 }
646 return pc()->local_streams();
647 }
648
649 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
650 return pc()->signaling_state();
651 }
652
653 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
654 return pc()->ice_connection_state();
655 }
656
657 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
658 return pc()->ice_gathering_state();
659 }
660
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661 private:
662 class DummyDtmfObserver : public DtmfSenderObserverInterface {
663 public:
664 DummyDtmfObserver() : completed_(false) {}
665
666 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700667 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 tones_.push_back(tone);
669 if (tone.empty()) {
670 completed_ = true;
671 }
672 }
673
674 void Verify(const std::vector<std::string>& tones) const {
675 ASSERT_TRUE(tones_.size() == tones.size());
676 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
677 }
678
679 bool completed() const { return completed_; }
680
681 private:
682 bool completed_;
683 std::vector<std::string> tones_;
684 };
685
deadbeefaf1b59c2015-10-15 12:08:41 -0700686 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
687
688 bool Init(const MediaConstraintsInterface* constraints,
689 const PeerConnectionFactory::Options* options) {
690 EXPECT_TRUE(!peer_connection_);
691 EXPECT_TRUE(!peer_connection_factory_);
692 allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
693 if (!allocator_factory_) {
694 return false;
695 }
696 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
697
698 if (fake_audio_capture_module_ == nullptr) {
699 return false;
700 }
701 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
702 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
703 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
704 rtc::Thread::Current(), rtc::Thread::Current(),
705 fake_audio_capture_module_, fake_video_encoder_factory_,
706 fake_video_decoder_factory_);
707 if (!peer_connection_factory_) {
708 return false;
709 }
710 if (options) {
711 peer_connection_factory_->SetOptions(*options);
712 }
713 peer_connection_ = CreatePeerConnection(allocator_factory_.get(),
714 constraints);
715 return peer_connection_.get() != nullptr;
716 }
717
deadbeef5def7b92015-11-20 11:43:22 -0800718 rtc::scoped_refptr<webrtc::VideoTrackInterface>
719 CreateLocalVideoTrack(const std::string stream_label) {
720 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
721 FakeConstraints source_constraints = video_constraints_;
722 source_constraints.SetMandatoryMaxFrameRate(10);
723
724 cricket::FakeVideoCapturer* fake_capturer =
725 new webrtc::FakePeriodicVideoCapturer();
726 video_capturers_.push_back(fake_capturer);
727 rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
728 peer_connection_factory_->CreateVideoSource(
729 fake_capturer, &source_constraints);
730 std::string label = stream_label + kVideoTrackLabelBase;
731 return peer_connection_factory_->CreateVideoTrack(label, source);
732 }
733
deadbeefaf1b59c2015-10-15 12:08:41 -0700734 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
735 webrtc::PortAllocatorFactoryInterface* factory,
736 const MediaConstraintsInterface* constraints) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737 // CreatePeerConnection with IceServers.
738 webrtc::PeerConnectionInterface::IceServers ice_servers;
739 webrtc::PeerConnectionInterface::IceServer ice_server;
740 ice_server.uri = "stun:stun.l.google.com:19302";
741 ice_servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000742
Henrik Boström5e56c592015-08-11 10:33:13 +0200743 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store(
744 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
745 : nullptr);
deadbeefaf1b59c2015-10-15 12:08:41 -0700746 return peer_connection_factory_->CreatePeerConnection(
Henrik Boström5e56c592015-08-11 10:33:13 +0200747 ice_servers, constraints, factory, dtls_identity_store.Pass(), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748 }
749
750 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700751 LOG(INFO) << id_ << "HandleIncomingOffer ";
deadbeeffaac4972015-11-12 15:33:07 -0800752 if (NumberOfLocalMediaStreams() == 0 && auto_add_stream_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 // If we are not sending any streams ourselves it is time to add some.
754 AddMediaStream(true, true);
755 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000756 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700757 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000759 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 EXPECT_TRUE(DoCreateAnswer(answer.use()));
761 std::string sdp;
762 EXPECT_TRUE(answer->ToString(&sdp));
763 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
deadbeefaf1b59c2015-10-15 12:08:41 -0700764 if (signaling_message_receiver_) {
765 signaling_message_receiver_->ReceiveSdpMessage(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 webrtc::SessionDescriptionInterface::kAnswer, sdp);
767 }
768 }
769
770 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700771 LOG(INFO) << id_ << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000772 rtc::scoped_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700773 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
775 }
776
777 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
778 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000779 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
780 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 MockCreateSessionDescriptionObserver>());
782 if (offer) {
783 pc()->CreateOffer(observer, &session_description_constraints_);
784 } else {
785 pc()->CreateAnswer(observer, &session_description_constraints_);
786 }
787 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
788 *desc = observer->release_desc();
789 if (observer->result() && ExpectIceRestart()) {
790 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
791 }
792 return observer->result();
793 }
794
795 bool DoCreateOffer(SessionDescriptionInterface** desc) {
796 return DoCreateOfferAnswer(desc, true);
797 }
798
799 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
800 return DoCreateOfferAnswer(desc, false);
801 }
802
803 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000804 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
805 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700807 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 pc()->SetLocalDescription(observer, desc);
809 // Ignore the observer result. If we wait for the result with
810 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
811 // before the offer which is an error.
812 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000813 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 // ProcessMessages waits at least 1ms but processes all messages before
815 // returning. Since this test is synchronous and send messages to the remote
816 // peer whenever a callback is invoked, this can lead to messages being
817 // sent to the remote peer in the wrong order.
818 // TODO(perkj): Find a way to check the result without risking that the
819 // order of sent messages are changed. Ex- by posting all messages that are
820 // sent to the remote peer.
821 return true;
822 }
823
824 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000825 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
826 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700828 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829 pc()->SetRemoteDescription(observer, desc);
830 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
831 return observer->result();
832 }
833
834 // This modifies all received SDP messages before they are processed.
835 void FilterIncomingSdpMessage(std::string* sdp) {
836 if (remove_msid_) {
837 const char kSdpSsrcAttribute[] = "a=ssrc:";
838 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
839 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
840 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
841 }
842 if (remove_bundle_) {
843 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
844 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
845 }
846 if (remove_sdes_) {
847 const char kSdpSdesCryptoAttribute[] = "a=crypto";
848 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
849 }
850 }
851
deadbeefaf1b59c2015-10-15 12:08:41 -0700852 std::string id_;
853
854 rtc::scoped_refptr<webrtc::PortAllocatorFactoryInterface> allocator_factory_;
855 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
856 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
857 peer_connection_factory_;
858
deadbeeffaac4972015-11-12 15:33:07 -0800859 bool auto_add_stream_ = true;
860
deadbeefaf1b59c2015-10-15 12:08:41 -0700861 typedef std::pair<std::string, std::string> IceUfragPwdPair;
862 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
863 bool expect_ice_restart_ = false;
864
865 // Needed to keep track of number of frames send.
866 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
867 // Needed to keep track of number of frames received.
868 typedef std::map<std::string, webrtc::FakeVideoTrackRenderer*> RenderMap;
869 RenderMap fake_video_renderers_;
870 // Needed to keep track of number of frames received when external decoder
871 // used.
872 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
873 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
874 bool video_decoder_factory_enabled_ = false;
875 webrtc::FakeConstraints video_constraints_;
876
877 // For remote peer communication.
878 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
879
880 // Store references to the video capturers we've created, so that we can stop
881 // them, if required.
882 std::vector<cricket::VideoCapturer*> video_capturers_;
883
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 webrtc::FakeConstraints session_description_constraints_;
deadbeefaf1b59c2015-10-15 12:08:41 -0700885 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
886 bool remove_bundle_ =
887 false; // True if bundle should be removed in received SDP.
888 bool remove_sdes_ =
889 false; // True if a=crypto should be removed in received SDP.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000891 rtc::scoped_refptr<DataChannelInterface> data_channel_;
892 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893};
894
ivoc191c1f92015-11-19 11:12:06 -0800895// Flaky on Mac Debug bots. See webrtc:5231
896#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
897#define MAYBE_JsepPeerConnectionP2PTestClient \
898 DISABLED_JsepPeerConnectionP2PTestClient
899#else
900#define MAYBE_JsepPeerConnectionP2PTestClient JsepPeerConnectionP2PTestClient
901#endif
902
deadbeefcbc95072015-10-15 19:31:56 -0700903// TODO(deadbeef): Rename this to P2PTestConductor once the Linux memcheck and
904// Windows DrMemory Full bots' blacklists are updated.
ivoc191c1f92015-11-19 11:12:06 -0800905class MAYBE_JsepPeerConnectionP2PTestClient : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 public:
ivoc191c1f92015-11-19 11:12:06 -0800907 MAYBE_JsepPeerConnectionP2PTestClient()
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000908 : pss_(new rtc::PhysicalSocketServer),
909 ss_(new rtc::VirtualSocketServer(pss_.get())),
910 ss_scope_(ss_.get()) {}
911
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 bool SessionActive() {
913 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000914 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000916
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 // Return true if the number of frames provided have been received or it is
918 // known that that will never occur (e.g. no frames will be sent or
919 // captured).
920 bool FramesNotPending(int audio_frames_to_receive,
921 int video_frames_to_receive) {
922 return VideoFramesReceivedCheck(video_frames_to_receive) &&
923 AudioFramesReceivedCheck(audio_frames_to_receive);
924 }
925 bool AudioFramesReceivedCheck(int frames_received) {
926 return initiating_client_->AudioFramesReceivedCheck(frames_received) &&
927 receiving_client_->AudioFramesReceivedCheck(frames_received);
928 }
929 bool VideoFramesReceivedCheck(int frames_received) {
930 return initiating_client_->VideoFramesReceivedCheck(frames_received) &&
931 receiving_client_->VideoFramesReceivedCheck(frames_received);
932 }
933 void VerifyDtmf() {
934 initiating_client_->VerifyDtmf();
935 receiving_client_->VerifyDtmf();
936 }
937
938 void TestUpdateOfferWithRejectedContent() {
939 initiating_client_->Negotiate(true, false);
940 EXPECT_TRUE_WAIT(
941 FramesNotPending(kEndAudioFrameCount * 2, kEndVideoFrameCount),
942 kMaxWaitForFramesMs);
943 // There shouldn't be any more video frame after the new offer is
944 // negotiated.
945 EXPECT_FALSE(VideoFramesReceivedCheck(kEndVideoFrameCount + 1));
946 }
947
948 void VerifyRenderedSize(int width, int height) {
949 EXPECT_EQ(width, receiving_client()->rendered_width());
950 EXPECT_EQ(height, receiving_client()->rendered_height());
951 EXPECT_EQ(width, initializing_client()->rendered_width());
952 EXPECT_EQ(height, initializing_client()->rendered_height());
953 }
954
955 void VerifySessionDescriptions() {
956 initiating_client_->VerifyRejectedMediaInSessionDescription();
957 receiving_client_->VerifyRejectedMediaInSessionDescription();
958 initiating_client_->VerifyLocalIceUfragAndPassword();
959 receiving_client_->VerifyLocalIceUfragAndPassword();
960 }
961
ivoc191c1f92015-11-19 11:12:06 -0800962 ~MAYBE_JsepPeerConnectionP2PTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700964 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965 }
966 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700967 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000968 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969 }
970
deadbeefaf1b59c2015-10-15 12:08:41 -0700971 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000972
973 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
974 MediaConstraintsInterface* recv_constraints) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700975 return CreateTestClients(init_constraints, nullptr, recv_constraints,
976 nullptr);
Joachim Bauch04e5b492015-05-29 09:40:39 +0200977 }
978
979 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
980 PeerConnectionFactory::Options* init_options,
981 MediaConstraintsInterface* recv_constraints,
982 PeerConnectionFactory::Options* recv_options) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700983 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
984 "Caller: ", init_constraints, init_options));
985 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
986 "Callee: ", recv_constraints, recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987 if (!initiating_client_ || !receiving_client_) {
988 return false;
989 }
990 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
991 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
992 return true;
993 }
994
995 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
996 const webrtc::FakeConstraints& recv_constraints) {
997 initiating_client_->SetVideoConstraints(init_constraints);
998 receiving_client_->SetVideoConstraints(recv_constraints);
999 }
1000
1001 void EnableVideoDecoderFactory() {
1002 initiating_client_->EnableVideoDecoderFactory();
1003 receiving_client_->EnableVideoDecoderFactory();
1004 }
1005
1006 // This test sets up a call between two parties. Both parties send static
1007 // frames to each other. Once the test is finished the number of sent frames
1008 // is compared to the number of received frames.
1009 void LocalP2PTest() {
1010 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1011 initiating_client_->AddMediaStream(true, true);
1012 }
1013 initiating_client_->Negotiate();
deadbeef5def7b92015-11-20 11:43:22 -08001014 const int kMaxWaitForActivationMs = 5000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015 // Assert true is used here since next tests are guaranteed to fail and
1016 // would eat up 5 seconds.
1017 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1018 VerifySessionDescriptions();
1019
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020 int audio_frame_count = kEndAudioFrameCount;
1021 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1022 if (!initiating_client_->can_receive_audio() ||
1023 !receiving_client_->can_receive_audio()) {
1024 audio_frame_count = -1;
1025 }
1026 int video_frame_count = kEndVideoFrameCount;
1027 if (!initiating_client_->can_receive_video() ||
1028 !receiving_client_->can_receive_video()) {
1029 video_frame_count = -1;
1030 }
1031
1032 if (audio_frame_count != -1 || video_frame_count != -1) {
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001033 // Audio or video is expected to flow, so both clients should reach the
1034 // Connected state, and the offerer (ICE controller) should proceed to
1035 // Completed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036 // Note: These tests have been observed to fail under heavy load at
1037 // shorter timeouts, so they may be flaky.
1038 EXPECT_EQ_WAIT(
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001039 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001040 initiating_client_->ice_connection_state(),
1041 kMaxWaitForFramesMs);
1042 EXPECT_EQ_WAIT(
1043 webrtc::PeerConnectionInterface::kIceConnectionConnected,
1044 receiving_client_->ice_connection_state(),
1045 kMaxWaitForFramesMs);
1046 }
1047
1048 if (initiating_client_->can_receive_audio() ||
1049 initiating_client_->can_receive_video()) {
1050 // The initiating client can receive media, so it must produce candidates
1051 // that will serve as destinations for that media.
1052 // TODO(bemasc): Understand why the state is not already Complete here, as
1053 // seems to be the case for the receiving client. This may indicate a bug
1054 // in the ICE gathering system.
1055 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1056 initiating_client_->ice_gathering_state());
1057 }
1058 if (receiving_client_->can_receive_audio() ||
1059 receiving_client_->can_receive_video()) {
1060 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1061 receiving_client_->ice_gathering_state(),
1062 kMaxWaitForFramesMs);
1063 }
1064
1065 EXPECT_TRUE_WAIT(FramesNotPending(audio_frame_count, video_frame_count),
1066 kMaxWaitForFramesMs);
1067 }
1068
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001069 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1070 // Messages may get lost on the unreliable DataChannel, so we send multiple
1071 // times to avoid test flakiness.
1072 static const size_t kSendAttempts = 5;
1073
1074 for (size_t i = 0; i < kSendAttempts; ++i) {
1075 dc->Send(DataBuffer(data));
1076 }
1077 }
1078
deadbeefaf1b59c2015-10-15 12:08:41 -07001079 PeerConnectionTestClient* initializing_client() {
1080 return initiating_client_.get();
1081 }
1082 PeerConnectionTestClient* receiving_client() {
1083 return receiving_client_.get();
1084 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001085
1086 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001087 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1088 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1089 rtc::SocketServerScope ss_scope_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001090 rtc::scoped_ptr<PeerConnectionTestClient> initiating_client_;
1091 rtc::scoped_ptr<PeerConnectionTestClient> receiving_client_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001093
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001094// Disable for TSan v2, see
1095// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1096#if !defined(THREAD_SANITIZER)
1097
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001099// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1100// See issue webrtc/2378.
ivoc191c1f92015-11-19 11:12:06 -08001101TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001102 ASSERT_TRUE(CreateTestClients());
1103 LocalP2PTest();
1104 VerifyDtmf();
1105}
1106
1107// This test sets up a Jsep call between two parties and test that we can get a
1108// video aspect ratio of 16:9.
ivoc191c1f92015-11-19 11:12:06 -08001109TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001110 ASSERT_TRUE(CreateTestClients());
1111 FakeConstraints constraint;
1112 double requested_ratio = 640.0/360;
1113 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1114 SetVideoConstraints(constraint, constraint);
1115 LocalP2PTest();
1116
1117 ASSERT_LE(0, initializing_client()->rendered_height());
1118 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001119 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001120 initializing_client()->rendered_height();
1121 EXPECT_LE(requested_ratio, initiating_video_ratio);
1122
1123 ASSERT_LE(0, receiving_client()->rendered_height());
1124 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001125 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001126 receiving_client()->rendered_height();
1127 EXPECT_LE(requested_ratio, receiving_video_ratio);
1128}
1129
1130// This test sets up a Jsep call between two parties and test that the
1131// received video has a resolution of 1280*720.
1132// TODO(mallinath): Enable when
1133// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
ivoc191c1f92015-11-19 11:12:06 -08001134TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001135 ASSERT_TRUE(CreateTestClients());
1136 FakeConstraints constraint;
1137 constraint.SetMandatoryMinWidth(1280);
1138 constraint.SetMandatoryMinHeight(720);
1139 SetVideoConstraints(constraint, constraint);
1140 LocalP2PTest();
1141 VerifyRenderedSize(1280, 720);
1142}
1143
1144// This test sets up a call between two endpoints that are configured to use
1145// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
ivoc191c1f92015-11-19 11:12:06 -08001146TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestDtls) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001147 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001148 FakeConstraints setup_constraints;
1149 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1150 true);
1151 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1152 LocalP2PTest();
1153 VerifyRenderedSize(640, 480);
1154}
1155
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001156// This test sets up a audio call initially and then upgrades to audio/video,
1157// using DTLS.
ivoc191c1f92015-11-19 11:12:06 -08001158TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001159 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001160 FakeConstraints setup_constraints;
1161 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1162 true);
1163 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1164 receiving_client()->SetReceiveAudioVideo(true, false);
1165 LocalP2PTest();
1166 receiving_client()->SetReceiveAudioVideo(true, true);
1167 receiving_client()->Negotiate();
1168}
1169
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001170// This test sets up a call between two endpoints that are configured to use
1171// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1172// negotiated and used for transport.
ivoc191c1f92015-11-19 11:12:06 -08001173TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
1174 MAYBE_LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001175 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001176 FakeConstraints setup_constraints;
1177 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1178 true);
1179 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1180 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1181 LocalP2PTest();
1182 VerifyRenderedSize(640, 480);
1183}
1184
1185// This test sets up a Jsep call between two parties, and the callee only
1186// accept to receive video.
ivoc191c1f92015-11-19 11:12:06 -08001187TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001188 ASSERT_TRUE(CreateTestClients());
1189 receiving_client()->SetReceiveAudioVideo(false, true);
1190 LocalP2PTest();
1191}
1192
1193// This test sets up a Jsep call between two parties, and the callee only
1194// accept to receive audio.
ivoc191c1f92015-11-19 11:12:06 -08001195TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001196 ASSERT_TRUE(CreateTestClients());
1197 receiving_client()->SetReceiveAudioVideo(true, false);
1198 LocalP2PTest();
1199}
1200
1201// This test sets up a Jsep call between two parties, and the callee reject both
1202// audio and video.
ivoc191c1f92015-11-19 11:12:06 -08001203TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204 ASSERT_TRUE(CreateTestClients());
1205 receiving_client()->SetReceiveAudioVideo(false, false);
1206 LocalP2PTest();
1207}
1208
1209// This test sets up an audio and video call between two parties. After the call
1210// runs for a while (10 frames), the caller sends an update offer with video
1211// being rejected. Once the re-negotiation is done, the video flow should stop
1212// and the audio flow should continue.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001213// Disabled due to b/14955157.
ivoc191c1f92015-11-19 11:12:06 -08001214TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
deadbeefcbc95072015-10-15 19:31:56 -07001215 DISABLED_UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001216 ASSERT_TRUE(CreateTestClients());
1217 LocalP2PTest();
1218 TestUpdateOfferWithRejectedContent();
1219}
1220
1221// This test sets up a Jsep call between two parties. The MSID is removed from
1222// the SDP strings from the caller.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001223// Disabled due to b/14955157.
ivoc191c1f92015-11-19 11:12:06 -08001224TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
1225 DISABLED_LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 ASSERT_TRUE(CreateTestClients());
1227 receiving_client()->RemoveMsidFromReceivedSdp(true);
1228 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1229 // audio and video is muxed when MSID is disabled. Remove
1230 // SetRemoveBundleFromSdp once
1231 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1232 receiving_client()->RemoveBundleFromReceivedSdp(true);
1233 LocalP2PTest();
1234}
1235
1236// This test sets up a Jsep call between two parties and the initiating peer
1237// sends two steams.
1238// TODO(perkj): Disabled due to
1239// https://code.google.com/p/webrtc/issues/detail?id=1454
ivoc191c1f92015-11-19 11:12:06 -08001240TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001241 ASSERT_TRUE(CreateTestClients());
1242 // Set optional video constraint to max 320pixels to decrease CPU usage.
1243 FakeConstraints constraint;
1244 constraint.SetOptionalMaxWidth(320);
1245 SetVideoConstraints(constraint, constraint);
1246 initializing_client()->AddMediaStream(true, true);
1247 initializing_client()->AddMediaStream(false, true);
1248 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1249 LocalP2PTest();
1250 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1251}
1252
1253// Test that we can receive the audio output level from a remote audio track.
ivoc191c1f92015-11-19 11:12:06 -08001254TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, MAYBE_GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255 ASSERT_TRUE(CreateTestClients());
1256 LocalP2PTest();
1257
1258 StreamCollectionInterface* remote_streams =
1259 initializing_client()->remote_streams();
1260 ASSERT_GT(remote_streams->count(), 0u);
1261 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1262 MediaStreamTrackInterface* remote_audio_track =
1263 remote_streams->at(0)->GetAudioTracks()[0];
1264
1265 // Get the audio output level stats. Note that the level is not available
1266 // until a RTCP packet has been received.
1267 EXPECT_TRUE_WAIT(
1268 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1269 kMaxWaitForStatsMs);
1270}
1271
1272// Test that an audio input level is reported.
ivoc191c1f92015-11-19 11:12:06 -08001273TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, MAYBE_GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001274 ASSERT_TRUE(CreateTestClients());
1275 LocalP2PTest();
1276
1277 // Get the audio input level stats. The level should be available very
1278 // soon after the test starts.
1279 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1280 kMaxWaitForStatsMs);
1281}
1282
1283// Test that we can get incoming byte counts from both audio and video tracks.
ivoc191c1f92015-11-19 11:12:06 -08001284TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, MAYBE_GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001285 ASSERT_TRUE(CreateTestClients());
1286 LocalP2PTest();
1287
1288 StreamCollectionInterface* remote_streams =
1289 initializing_client()->remote_streams();
1290 ASSERT_GT(remote_streams->count(), 0u);
1291 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1292 MediaStreamTrackInterface* remote_audio_track =
1293 remote_streams->at(0)->GetAudioTracks()[0];
1294 EXPECT_TRUE_WAIT(
1295 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1296 kMaxWaitForStatsMs);
1297
1298 MediaStreamTrackInterface* remote_video_track =
1299 remote_streams->at(0)->GetVideoTracks()[0];
1300 EXPECT_TRUE_WAIT(
1301 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1302 kMaxWaitForStatsMs);
1303}
1304
1305// Test that we can get outgoing byte counts from both audio and video tracks.
ivoc191c1f92015-11-19 11:12:06 -08001306TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, MAYBE_GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001307 ASSERT_TRUE(CreateTestClients());
1308 LocalP2PTest();
1309
1310 StreamCollectionInterface* local_streams =
1311 initializing_client()->local_streams();
1312 ASSERT_GT(local_streams->count(), 0u);
1313 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1314 MediaStreamTrackInterface* local_audio_track =
1315 local_streams->at(0)->GetAudioTracks()[0];
1316 EXPECT_TRUE_WAIT(
1317 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1318 kMaxWaitForStatsMs);
1319
1320 MediaStreamTrackInterface* local_video_track =
1321 local_streams->at(0)->GetVideoTracks()[0];
1322 EXPECT_TRUE_WAIT(
1323 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1324 kMaxWaitForStatsMs);
1325}
1326
Joachim Bauch04e5b492015-05-29 09:40:39 +02001327// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
ivoc191c1f92015-11-19 11:12:06 -08001328TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001329 PeerConnectionFactory::Options init_options;
1330 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1331 PeerConnectionFactory::Options recv_options;
1332 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001333 ASSERT_TRUE(
1334 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001335 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1336 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1337 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001338 LocalP2PTest();
1339
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001340 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001341 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1342 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1343 initializing_client()->GetDtlsCipherStats(),
1344 kMaxWaitForStatsMs);
1345 EXPECT_EQ(1, init_observer->GetEnumCounter(
1346 webrtc::kEnumCounterAudioSslCipher,
1347 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1348 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001349
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001350 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001351 initializing_client()->GetSrtpCipherStats(),
1352 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001353 EXPECT_EQ(1,
1354 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1355 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001356}
1357
1358// Test that DTLS 1.2 is used if both ends support it.
ivoc191c1f92015-11-19 11:12:06 -08001359TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, MAYBE_GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001360 PeerConnectionFactory::Options init_options;
1361 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1362 PeerConnectionFactory::Options recv_options;
1363 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001364 ASSERT_TRUE(
1365 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001366 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1367 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1368 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001369 LocalP2PTest();
1370
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001371 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001372 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1373 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)),
1374 initializing_client()->GetDtlsCipherStats(),
1375 kMaxWaitForStatsMs);
1376 EXPECT_EQ(1, init_observer->GetEnumCounter(
1377 webrtc::kEnumCounterAudioSslCipher,
1378 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1379 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001380
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001381 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001382 initializing_client()->GetSrtpCipherStats(),
1383 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001384 EXPECT_EQ(1,
1385 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1386 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001387}
1388
1389// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1390// received supports 1.0.
ivoc191c1f92015-11-19 11:12:06 -08001391TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001392 PeerConnectionFactory::Options init_options;
1393 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1394 PeerConnectionFactory::Options recv_options;
1395 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001396 ASSERT_TRUE(
1397 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001398 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1399 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1400 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001401 LocalP2PTest();
1402
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001403 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001404 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1405 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1406 initializing_client()->GetDtlsCipherStats(),
1407 kMaxWaitForStatsMs);
1408 EXPECT_EQ(1, init_observer->GetEnumCounter(
1409 webrtc::kEnumCounterAudioSslCipher,
1410 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1411 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001412
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001413 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001414 initializing_client()->GetSrtpCipherStats(),
1415 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001416 EXPECT_EQ(1,
1417 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1418 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001419}
1420
1421// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1422// received supports 1.2.
ivoc191c1f92015-11-19 11:12:06 -08001423TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001424 PeerConnectionFactory::Options init_options;
1425 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1426 PeerConnectionFactory::Options recv_options;
1427 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001428 ASSERT_TRUE(
1429 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001430 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1431 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1432 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001433 LocalP2PTest();
1434
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001435 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::SslCipherSuiteToName(
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001436 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1437 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1438 initializing_client()->GetDtlsCipherStats(),
1439 kMaxWaitForStatsMs);
1440 EXPECT_EQ(1, init_observer->GetEnumCounter(
1441 webrtc::kEnumCounterAudioSslCipher,
1442 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1443 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001444
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001445 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001446 initializing_client()->GetSrtpCipherStats(),
1447 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001448 EXPECT_EQ(1,
1449 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1450 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001451}
1452
deadbeefb5cb19b2015-11-23 16:39:12 -08001453// This test sets up a call between two parties with audio, video and an RTP
1454// data channel.
1455TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001456 FakeConstraints setup_constraints;
1457 setup_constraints.SetAllowRtpDataChannels();
1458 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1459 initializing_client()->CreateDataChannel();
1460 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001461 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1462 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001463 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1464 kMaxWaitMs);
1465 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1466 kMaxWaitMs);
1467
1468 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001469
1470 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001471 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1472 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001473
1474 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1476 kMaxWaitMs);
1477
1478 receiving_client()->data_channel()->Close();
1479 // Send new offer and answer.
1480 receiving_client()->Negotiate();
1481 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1482 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1483}
1484
deadbeefb5cb19b2015-11-23 16:39:12 -08001485// This test sets up a call between two parties with audio, video and an SCTP
1486// data channel.
1487TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestSctpDataChannel) {
1488 ASSERT_TRUE(CreateTestClients());
1489 initializing_client()->CreateDataChannel();
1490 LocalP2PTest();
1491 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1492 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
1493 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1494 kMaxWaitMs);
1495 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
1496
1497 std::string data = "hello world";
1498
1499 initializing_client()->data_channel()->Send(DataBuffer(data));
1500 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1501 kMaxWaitMs);
1502
1503 receiving_client()->data_channel()->Send(DataBuffer(data));
1504 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1505 kMaxWaitMs);
1506
1507 receiving_client()->data_channel()->Close();
1508 // Send new offer and answer.
1509 receiving_client()->Negotiate();
1510 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1511 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1512}
1513
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001514// This test sets up a call between two parties and creates a data channel.
1515// The test tests that received data is buffered unless an observer has been
1516// registered.
1517// Rtp data channels can receive data before the underlying
1518// transport has detected that a channel is writable and thus data can be
1519// received before the data channel state changes to open. That is hard to test
1520// but the same buffering is used in that case.
ivoc191c1f92015-11-19 11:12:06 -08001521TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522 FakeConstraints setup_constraints;
1523 setup_constraints.SetAllowRtpDataChannels();
1524 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1525 initializing_client()->CreateDataChannel();
1526 initializing_client()->Negotiate();
1527
deadbeefaf1b59c2015-10-15 12:08:41 -07001528 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1529 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1531 kMaxWaitMs);
1532 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1533 receiving_client()->data_channel()->state(), kMaxWaitMs);
1534
1535 // Unregister the existing observer.
1536 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001537
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001538 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001539 SendRtpData(initializing_client()->data_channel(), data);
1540
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541 // Wait a while to allow the sent data to arrive before an observer is
1542 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001543 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001544
1545 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1546 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1547}
1548
1549// This test sets up a call between two parties with audio, video and but only
1550// the initiating client support data.
ivoc191c1f92015-11-19 11:12:06 -08001551TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
1552 LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001553 FakeConstraints setup_constraints_1;
1554 setup_constraints_1.SetAllowRtpDataChannels();
1555 // Must disable DTLS to make negotiation succeed.
1556 setup_constraints_1.SetMandatory(
1557 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1558 FakeConstraints setup_constraints_2;
1559 setup_constraints_2.SetMandatory(
1560 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1561 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562 initializing_client()->CreateDataChannel();
1563 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001564 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001565 EXPECT_FALSE(receiving_client()->data_channel());
1566 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1567}
1568
1569// This test sets up a call between two parties with audio, video. When audio
1570// and video is setup and flowing and data channel is negotiated.
ivoc191c1f92015-11-19 11:12:06 -08001571TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
1572 AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001573 FakeConstraints setup_constraints;
1574 setup_constraints.SetAllowRtpDataChannels();
1575 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1576 LocalP2PTest();
1577 initializing_client()->CreateDataChannel();
1578 // Send new offer and answer.
1579 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07001580 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1581 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1583 kMaxWaitMs);
1584 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1585 kMaxWaitMs);
1586}
1587
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001588// This test sets up a Jsep call with SCTP DataChannel and verifies the
1589// negotiation is completed without error.
1590#ifdef HAVE_SCTP
ivoc191c1f92015-11-19 11:12:06 -08001591TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001592 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001593 FakeConstraints constraints;
1594 constraints.SetMandatory(
1595 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1596 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1597 initializing_client()->CreateDataChannel();
1598 initializing_client()->Negotiate(false, false);
1599}
1600#endif
1601
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602// This test sets up a call between two parties with audio, and video.
1603// During the call, the initializing side restart ice and the test verifies that
1604// new ice candidates are generated and audio and video still can flow.
ivoc191c1f92015-11-19 11:12:06 -08001605TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, MAYBE_IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606 ASSERT_TRUE(CreateTestClients());
1607
1608 // Negotiate and wait for ice completion and make sure audio and video plays.
1609 LocalP2PTest();
1610
1611 // Create a SDP string of the first audio candidate for both clients.
1612 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1613 initializing_client()->pc()->local_description()->candidates(0);
1614 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1615 receiving_client()->pc()->local_description()->candidates(0);
1616 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1617 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1618 std::string initiator_candidate;
1619 EXPECT_TRUE(
1620 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1621 std::string receiver_candidate;
1622 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1623
1624 // Restart ice on the initializing client.
1625 receiving_client()->SetExpectIceRestart(true);
1626 initializing_client()->IceRestart();
1627
1628 // Negotiate and wait for ice completion again and make sure audio and video
1629 // plays.
1630 LocalP2PTest();
1631
1632 // Create a SDP string of the first audio candidate for both clients again.
1633 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1634 initializing_client()->pc()->local_description()->candidates(0);
1635 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1636 receiving_client()->pc()->local_description()->candidates(0);
1637 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1638 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1639 std::string initiator_candidate_restart;
1640 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1641 &initiator_candidate_restart));
1642 std::string receiver_candidate_restart;
1643 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1644 &receiver_candidate_restart));
1645
1646 // Verify that the first candidates in the local session descriptions has
1647 // changed.
1648 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1649 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1650}
1651
deadbeeffaac4972015-11-12 15:33:07 -08001652// This test sets up a call between two parties with audio, and video.
1653// It then renegotiates setting the video m-line to "port 0", then later
1654// renegotiates again, enabling video.
ivoc191c1f92015-11-19 11:12:06 -08001655TEST_F(MAYBE_JsepPeerConnectionP2PTestClient, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08001656 ASSERT_TRUE(CreateTestClients());
1657
1658 // Do initial negotiation. Will result in video and audio sendonly m-lines.
1659 receiving_client()->set_auto_add_stream(false);
1660 initializing_client()->AddMediaStream(true, true);
1661 initializing_client()->Negotiate();
1662
1663 // Negotiate again, disabling the video m-line (receiving client will
1664 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
1665 receiving_client()->SetReceiveVideo(false);
1666 initializing_client()->Negotiate();
1667
1668 // Enable video and do negotiation again, making sure video is received
1669 // end-to-end.
1670 receiving_client()->SetReceiveVideo(true);
1671 receiving_client()->AddMediaStream(true, true);
1672 LocalP2PTest();
1673}
1674
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001675// This test sets up a Jsep call between two parties with external
1676// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001677// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1678// See issue webrtc/2378.
ivoc191c1f92015-11-19 11:12:06 -08001679TEST_F(MAYBE_JsepPeerConnectionP2PTestClient,
deadbeefcbc95072015-10-15 19:31:56 -07001680 DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001681 ASSERT_TRUE(CreateTestClients());
1682 EnableVideoDecoderFactory();
1683 LocalP2PTest();
1684}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001685
deadbeef0a6c4ca2015-10-06 11:38:28 -07001686class IceServerParsingTest : public testing::Test {
1687 public:
1688 // Convenience for parsing a single URL.
1689 bool ParseUrl(const std::string& url) {
1690 return ParseUrl(url, std::string(), std::string());
1691 }
1692
1693 bool ParseUrl(const std::string& url,
1694 const std::string& username,
1695 const std::string& password) {
1696 PeerConnectionInterface::IceServers servers;
1697 PeerConnectionInterface::IceServer server;
1698 server.urls.push_back(url);
1699 server.username = username;
1700 server.password = password;
1701 servers.push_back(server);
1702 return webrtc::ParseIceServers(servers, &stun_configurations_,
1703 &turn_configurations_);
1704 }
1705
1706 protected:
1707 webrtc::StunConfigurations stun_configurations_;
1708 webrtc::TurnConfigurations turn_configurations_;
1709};
1710
1711// Make sure all STUN/TURN prefixes are parsed correctly.
1712TEST_F(IceServerParsingTest, ParseStunPrefixes) {
1713 EXPECT_TRUE(ParseUrl("stun:hostname"));
1714 EXPECT_EQ(1U, stun_configurations_.size());
1715 EXPECT_EQ(0U, turn_configurations_.size());
1716 stun_configurations_.clear();
1717
1718 EXPECT_TRUE(ParseUrl("stuns:hostname"));
1719 EXPECT_EQ(1U, stun_configurations_.size());
1720 EXPECT_EQ(0U, turn_configurations_.size());
1721 stun_configurations_.clear();
1722
1723 EXPECT_TRUE(ParseUrl("turn:hostname"));
1724 EXPECT_EQ(0U, stun_configurations_.size());
1725 EXPECT_EQ(1U, turn_configurations_.size());
1726 EXPECT_FALSE(turn_configurations_[0].secure);
1727 turn_configurations_.clear();
1728
1729 EXPECT_TRUE(ParseUrl("turns:hostname"));
1730 EXPECT_EQ(0U, stun_configurations_.size());
1731 EXPECT_EQ(1U, turn_configurations_.size());
1732 EXPECT_TRUE(turn_configurations_[0].secure);
1733 turn_configurations_.clear();
1734
1735 // invalid prefixes
1736 EXPECT_FALSE(ParseUrl("stunn:hostname"));
1737 EXPECT_FALSE(ParseUrl(":hostname"));
1738 EXPECT_FALSE(ParseUrl(":"));
1739 EXPECT_FALSE(ParseUrl(""));
1740}
1741
1742TEST_F(IceServerParsingTest, VerifyDefaults) {
1743 // TURNS defaults
1744 EXPECT_TRUE(ParseUrl("turns:hostname"));
1745 EXPECT_EQ(1U, turn_configurations_.size());
1746 EXPECT_EQ(5349, turn_configurations_[0].server.port());
1747 EXPECT_EQ("tcp", turn_configurations_[0].transport_type);
1748 turn_configurations_.clear();
1749
1750 // TURN defaults
1751 EXPECT_TRUE(ParseUrl("turn:hostname"));
1752 EXPECT_EQ(1U, turn_configurations_.size());
1753 EXPECT_EQ(3478, turn_configurations_[0].server.port());
1754 EXPECT_EQ("udp", turn_configurations_[0].transport_type);
1755 turn_configurations_.clear();
1756
1757 // STUN defaults
1758 EXPECT_TRUE(ParseUrl("stun:hostname"));
1759 EXPECT_EQ(1U, stun_configurations_.size());
1760 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1761 stun_configurations_.clear();
1762}
1763
1764// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
1765// can be parsed correctly.
1766TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
1767 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
1768 EXPECT_EQ(1U, stun_configurations_.size());
1769 EXPECT_EQ("1.2.3.4", stun_configurations_[0].server.hostname());
1770 EXPECT_EQ(1234, stun_configurations_[0].server.port());
1771 stun_configurations_.clear();
1772
1773 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
1774 EXPECT_EQ(1U, stun_configurations_.size());
1775 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_configurations_[0].server.hostname());
1776 EXPECT_EQ(4321, stun_configurations_[0].server.port());
1777 stun_configurations_.clear();
1778
1779 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
1780 EXPECT_EQ(1U, stun_configurations_.size());
1781 EXPECT_EQ("hostname", stun_configurations_[0].server.hostname());
1782 EXPECT_EQ(9999, stun_configurations_[0].server.port());
1783 stun_configurations_.clear();
1784
1785 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
1786 EXPECT_EQ(1U, stun_configurations_.size());
1787 EXPECT_EQ("1.2.3.4", stun_configurations_[0].server.hostname());
1788 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1789 stun_configurations_.clear();
1790
1791 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
1792 EXPECT_EQ(1U, stun_configurations_.size());
1793 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_configurations_[0].server.hostname());
1794 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1795 stun_configurations_.clear();
1796
1797 EXPECT_TRUE(ParseUrl("stun:hostname"));
1798 EXPECT_EQ(1U, stun_configurations_.size());
1799 EXPECT_EQ("hostname", stun_configurations_[0].server.hostname());
1800 EXPECT_EQ(3478, stun_configurations_[0].server.port());
1801 stun_configurations_.clear();
1802
1803 // Try some invalid hostname:port strings.
1804 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
1805 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
1806 EXPECT_FALSE(ParseUrl("stun:hostname:"));
1807 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
1808 EXPECT_FALSE(ParseUrl("stun::5555"));
1809 EXPECT_FALSE(ParseUrl("stun:"));
1810}
1811
1812// Test parsing the "?transport=xxx" part of the URL.
1813TEST_F(IceServerParsingTest, ParseTransport) {
1814 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
1815 EXPECT_EQ(1U, turn_configurations_.size());
1816 EXPECT_EQ("tcp", turn_configurations_[0].transport_type);
1817 turn_configurations_.clear();
1818
1819 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
1820 EXPECT_EQ(1U, turn_configurations_.size());
1821 EXPECT_EQ("udp", turn_configurations_[0].transport_type);
1822 turn_configurations_.clear();
1823
1824 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
1825}
1826
1827// Test parsing ICE username contained in URL.
1828TEST_F(IceServerParsingTest, ParseUsername) {
1829 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
1830 EXPECT_EQ(1U, turn_configurations_.size());
1831 EXPECT_EQ("user", turn_configurations_[0].username);
1832 turn_configurations_.clear();
1833
1834 EXPECT_FALSE(ParseUrl("turn:@hostname"));
1835 EXPECT_FALSE(ParseUrl("turn:username@"));
1836 EXPECT_FALSE(ParseUrl("turn:@"));
1837 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
1838}
1839
1840// Test that username and password from IceServer is copied into the resulting
1841// TurnConfiguration.
1842TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
1843 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
1844 EXPECT_EQ(1U, turn_configurations_.size());
1845 EXPECT_EQ("username", turn_configurations_[0].username);
1846 EXPECT_EQ("password", turn_configurations_[0].password);
1847}
1848
1849// Ensure that if a server has multiple URLs, each one is parsed.
1850TEST_F(IceServerParsingTest, ParseMultipleUrls) {
1851 PeerConnectionInterface::IceServers servers;
1852 PeerConnectionInterface::IceServer server;
1853 server.urls.push_back("stun:hostname");
1854 server.urls.push_back("turn:hostname");
1855 servers.push_back(server);
1856 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_configurations_,
1857 &turn_configurations_));
1858 EXPECT_EQ(1U, stun_configurations_.size());
1859 EXPECT_EQ(1U, turn_configurations_.size());
1860}
1861
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001862#endif // if !defined(THREAD_SANITIZER)