blob: d593f888ff8add8910fa5c6829d720978733da52 [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"
40#include "talk/app/webrtc/peerconnectionfactory.h"
41#include "talk/app/webrtc/peerconnectioninterface.h"
42#include "talk/app/webrtc/test/fakeaudiocapturemodule.h"
43#include "talk/app/webrtc/test/fakeconstraints.h"
Henrik Boström5e56c592015-08-11 10:33:13 +020044#include "talk/app/webrtc/test/fakedtlsidentitystore.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045#include "talk/app/webrtc/test/fakeperiodicvideocapturer.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000046#include "talk/app/webrtc/test/fakevideotrackrenderer.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047#include "talk/app/webrtc/test/mockpeerconnectionobservers.h"
48#include "talk/app/webrtc/videosourceinterface.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000049#include "talk/media/webrtc/fakewebrtcvideoengine.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000050#include "talk/session/media/mediasession.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000051#include "webrtc/base/gunit.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000052#include "webrtc/base/physicalsocketserver.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000053#include "webrtc/base/scoped_ptr.h"
54#include "webrtc/base/ssladapter.h"
55#include "webrtc/base/sslstreamadapter.h"
56#include "webrtc/base/thread.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000057#include "webrtc/base/virtualsocketserver.h"
58#include "webrtc/p2p/base/constants.h"
59#include "webrtc/p2p/base/sessiondescription.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060
61#define MAYBE_SKIP_TEST(feature) \
62 if (!(feature())) { \
63 LOG(LS_INFO) << "Feature disabled... skipping"; \
64 return; \
65 }
66
67using cricket::ContentInfo;
68using cricket::FakeWebRtcVideoDecoder;
69using cricket::FakeWebRtcVideoDecoderFactory;
70using cricket::FakeWebRtcVideoEncoder;
71using cricket::FakeWebRtcVideoEncoderFactory;
72using cricket::MediaContentDescription;
73using webrtc::DataBuffer;
74using webrtc::DataChannelInterface;
75using webrtc::DtmfSender;
76using webrtc::DtmfSenderInterface;
77using webrtc::DtmfSenderObserverInterface;
78using webrtc::FakeConstraints;
79using webrtc::MediaConstraintsInterface;
80using webrtc::MediaStreamTrackInterface;
81using webrtc::MockCreateSessionDescriptionObserver;
82using webrtc::MockDataChannelObserver;
83using webrtc::MockSetSessionDescriptionObserver;
84using webrtc::MockStatsObserver;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000085using webrtc::PeerConnectionInterface;
Joachim Bauch04e5b492015-05-29 09:40:39 +020086using webrtc::PeerConnectionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087using webrtc::SessionDescriptionInterface;
88using webrtc::StreamCollectionInterface;
89
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000090static const int kMaxWaitMs = 10000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000091// Disable for TSan v2, see
92// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
93// This declaration is also #ifdef'd as it causes uninitialized-variable
94// warnings.
95#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096static const int kMaxWaitForStatsMs = 3000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000097#endif
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000098static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099static const int kEndAudioFrameCount = 3;
100static const int kEndVideoFrameCount = 3;
101
102static const char kStreamLabelBase[] = "stream_label";
103static const char kVideoTrackLabelBase[] = "video_track";
104static const char kAudioTrackLabelBase[] = "audio_track";
105static const char kDataChannelLabel[] = "data_channel";
106
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000107// Disable for TSan v2, see
108// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
109// This declaration is also #ifdef'd as it causes unused-variable errors.
110#if !defined(THREAD_SANITIZER)
111// SRTP cipher name negotiated by the tests. This must be updated if the
112// default changes.
113static const char kDefaultSrtpCipher[] = "AES_CM_128_HMAC_SHA1_32";
114#endif
115
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116static void RemoveLinesFromSdp(const std::string& line_start,
117 std::string* sdp) {
118 const char kSdpLineEnd[] = "\r\n";
119 size_t ssrc_pos = 0;
120 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
121 std::string::npos) {
122 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
123 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
124 }
125}
126
127class SignalingMessageReceiver {
128 public:
129 protected:
130 SignalingMessageReceiver() {}
131 virtual ~SignalingMessageReceiver() {}
132};
133
134class JsepMessageReceiver : public SignalingMessageReceiver {
135 public:
136 virtual void ReceiveSdpMessage(const std::string& type,
137 std::string& msg) = 0;
138 virtual void ReceiveIceMessage(const std::string& sdp_mid,
139 int sdp_mline_index,
140 const std::string& msg) = 0;
141
142 protected:
143 JsepMessageReceiver() {}
144 virtual ~JsepMessageReceiver() {}
145};
146
147template <typename MessageReceiver>
148class PeerConnectionTestClientBase
149 : public webrtc::PeerConnectionObserver,
150 public MessageReceiver {
151 public:
152 ~PeerConnectionTestClientBase() {
153 while (!fake_video_renderers_.empty()) {
154 RenderMap::iterator it = fake_video_renderers_.begin();
155 delete it->second;
156 fake_video_renderers_.erase(it);
157 }
158 }
159
160 virtual void Negotiate() = 0;
161
162 virtual void Negotiate(bool audio, bool video) = 0;
163
164 virtual void SetVideoConstraints(
165 const webrtc::FakeConstraints& video_constraint) {
166 video_constraints_ = video_constraint;
167 }
168
169 void AddMediaStream(bool audio, bool video) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000170 std::string stream_label = kStreamLabelBase +
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000171 rtc::ToString<int>(
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000172 static_cast<int>(peer_connection_->local_streams()->count()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000173 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000174 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175
176 if (audio && can_receive_audio()) {
177 FakeConstraints constraints;
178 // Disable highpass filter so that we can get all the test audio frames.
179 constraints.AddMandatory(
180 MediaConstraintsInterface::kHighpassFilter, false);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000181 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
wu@webrtc.org97077a32013-10-25 21:18:33 +0000182 peer_connection_factory_->CreateAudioSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 // TODO(perkj): Test audio source when it is implemented. Currently audio
184 // always use the default input.
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000185 std::string label = stream_label + kAudioTrackLabelBase;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000186 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000187 peer_connection_factory_->CreateAudioTrack(label, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 stream->AddTrack(audio_track);
189 }
190 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000191 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 }
193
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000194 EXPECT_TRUE(peer_connection_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 }
196
197 size_t NumberOfLocalMediaStreams() {
198 return peer_connection_->local_streams()->count();
199 }
200
201 bool SessionActive() {
202 return peer_connection_->signaling_state() ==
203 webrtc::PeerConnectionInterface::kStable;
204 }
205
206 void set_signaling_message_receiver(
207 MessageReceiver* signaling_message_receiver) {
208 signaling_message_receiver_ = signaling_message_receiver;
209 }
210
211 void EnableVideoDecoderFactory() {
212 video_decoder_factory_enabled_ = true;
213 fake_video_decoder_factory_->AddSupportedVideoCodecType(
214 webrtc::kVideoCodecVP8);
215 }
216
217 bool AudioFramesReceivedCheck(int number_of_frames) const {
218 return number_of_frames <= fake_audio_capture_module_->frames_received();
219 }
220
221 bool VideoFramesReceivedCheck(int number_of_frames) {
222 if (video_decoder_factory_enabled_) {
223 const std::vector<FakeWebRtcVideoDecoder*>& decoders
224 = fake_video_decoder_factory_->decoders();
225 if (decoders.empty()) {
226 return number_of_frames <= 0;
227 }
228
229 for (std::vector<FakeWebRtcVideoDecoder*>::const_iterator
230 it = decoders.begin(); it != decoders.end(); ++it) {
231 if (number_of_frames > (*it)->GetNumFramesReceived()) {
232 return false;
233 }
234 }
235 return true;
236 } else {
237 if (fake_video_renderers_.empty()) {
238 return number_of_frames <= 0;
239 }
240
241 for (RenderMap::const_iterator it = fake_video_renderers_.begin();
242 it != fake_video_renderers_.end(); ++it) {
243 if (number_of_frames > it->second->num_rendered_frames()) {
244 return false;
245 }
246 }
247 return true;
248 }
249 }
250 // Verify the CreateDtmfSender interface
251 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000252 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
253 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254
255 // We can't create a DTMF sender with an invalid audio track or a non local
256 // track.
257 EXPECT_TRUE(peer_connection_->CreateDtmfSender(NULL) == NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000258 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 peer_connection_factory_->CreateAudioTrack("dummy_track",
260 NULL));
261 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == NULL);
262
263 // We should be able to create a DTMF sender from a local track.
264 webrtc::AudioTrackInterface* localtrack =
265 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
266 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
267 EXPECT_TRUE(dtmf_sender.get() != NULL);
268 dtmf_sender->RegisterObserver(observer.get());
269
270 // Test the DtmfSender object just created.
271 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
272 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
273
274 // We don't need to verify that the DTMF tones are actually sent out because
275 // that is already covered by the tests of the lower level components.
276
277 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
278 std::vector<std::string> tones;
279 tones.push_back("1");
280 tones.push_back("a");
281 tones.push_back("");
282 observer->Verify(tones);
283
284 dtmf_sender->UnregisterObserver();
285 }
286
287 // Verifies that the SessionDescription have rejected the appropriate media
288 // content.
289 void VerifyRejectedMediaInSessionDescription() {
290 ASSERT_TRUE(peer_connection_->remote_description() != NULL);
291 ASSERT_TRUE(peer_connection_->local_description() != NULL);
292 const cricket::SessionDescription* remote_desc =
293 peer_connection_->remote_description()->description();
294 const cricket::SessionDescription* local_desc =
295 peer_connection_->local_description()->description();
296
297 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
298 if (remote_audio_content) {
299 const ContentInfo* audio_content =
300 GetFirstAudioContent(local_desc);
301 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
302 }
303
304 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
305 if (remote_video_content) {
306 const ContentInfo* video_content =
307 GetFirstVideoContent(local_desc);
308 EXPECT_EQ(can_receive_video(), !video_content->rejected);
309 }
310 }
311
312 void SetExpectIceRestart(bool expect_restart) {
313 expect_ice_restart_ = expect_restart;
314 }
315
316 bool ExpectIceRestart() const { return expect_ice_restart_; }
317
318 void VerifyLocalIceUfragAndPassword() {
319 ASSERT_TRUE(peer_connection_->local_description() != NULL);
320 const cricket::SessionDescription* desc =
321 peer_connection_->local_description()->description();
322 const cricket::ContentInfos& contents = desc->contents();
323
324 for (size_t index = 0; index < contents.size(); ++index) {
325 if (contents[index].rejected)
326 continue;
327 const cricket::TransportDescription* transport_desc =
328 desc->GetTransportDescriptionByName(contents[index].name);
329
330 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000331 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332 if (ufragpair_it == ice_ufrag_pwd_.end()) {
333 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000334 ice_ufrag_pwd_[static_cast<int>(index)] =
335 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 } else if (ExpectIceRestart()) {
337 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
338 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
339 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
340 } else {
341 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
342 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
343 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
344 }
345 }
346 }
347
348 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000349 rtc::scoped_refptr<MockStatsObserver>
350 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000351 EXPECT_TRUE(peer_connection_->GetStats(
352 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700354 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355 return observer->AudioOutputLevel();
356 }
357
358 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000359 rtc::scoped_refptr<MockStatsObserver>
360 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000361 EXPECT_TRUE(peer_connection_->GetStats(
362 observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000363 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700364 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365 return observer->AudioInputLevel();
366 }
367
368 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000369 rtc::scoped_refptr<MockStatsObserver>
370 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000371 EXPECT_TRUE(peer_connection_->GetStats(
372 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000373 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700374 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375 return observer->BytesReceived();
376 }
377
378 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000379 rtc::scoped_refptr<MockStatsObserver>
380 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000381 EXPECT_TRUE(peer_connection_->GetStats(
382 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000383 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700384 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385 return observer->BytesSent();
386 }
387
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000388 int GetAvailableReceivedBandwidthStats() {
389 rtc::scoped_refptr<MockStatsObserver>
390 observer(new rtc::RefCountedObject<MockStatsObserver>());
391 EXPECT_TRUE(peer_connection_->GetStats(
392 observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
393 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700394 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000395 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000396 return bw;
397 }
398
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000399 std::string GetDtlsCipherStats() {
400 rtc::scoped_refptr<MockStatsObserver>
401 observer(new rtc::RefCountedObject<MockStatsObserver>());
402 EXPECT_TRUE(peer_connection_->GetStats(
403 observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
404 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700405 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000406 return observer->DtlsCipher();
407 }
408
409 std::string GetSrtpCipherStats() {
410 rtc::scoped_refptr<MockStatsObserver>
411 observer(new rtc::RefCountedObject<MockStatsObserver>());
412 EXPECT_TRUE(peer_connection_->GetStats(
413 observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
414 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700415 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000416 return observer->SrtpCipher();
417 }
418
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 int rendered_width() {
420 EXPECT_FALSE(fake_video_renderers_.empty());
421 return fake_video_renderers_.empty() ? 1 :
422 fake_video_renderers_.begin()->second->width();
423 }
424
425 int rendered_height() {
426 EXPECT_FALSE(fake_video_renderers_.empty());
427 return fake_video_renderers_.empty() ? 1 :
428 fake_video_renderers_.begin()->second->height();
429 }
430
431 size_t number_of_remote_streams() {
432 if (!pc())
433 return 0;
434 return pc()->remote_streams()->count();
435 }
436
437 StreamCollectionInterface* remote_streams() {
438 if (!pc()) {
439 ADD_FAILURE();
440 return NULL;
441 }
442 return pc()->remote_streams();
443 }
444
445 StreamCollectionInterface* local_streams() {
446 if (!pc()) {
447 ADD_FAILURE();
448 return NULL;
449 }
450 return pc()->local_streams();
451 }
452
453 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
454 return pc()->signaling_state();
455 }
456
457 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
458 return pc()->ice_connection_state();
459 }
460
461 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
462 return pc()->ice_gathering_state();
463 }
464
465 // PeerConnectionObserver callbacks.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466 virtual void OnMessage(const std::string&) {}
467 virtual void OnSignalingMessage(const std::string& /*msg*/) {}
468 virtual void OnSignalingChange(
469 webrtc::PeerConnectionInterface::SignalingState new_state) {
470 EXPECT_EQ(peer_connection_->signaling_state(), new_state);
471 }
472 virtual void OnAddStream(webrtc::MediaStreamInterface* media_stream) {
473 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
474 const std::string id = media_stream->GetVideoTracks()[i]->id();
475 ASSERT_TRUE(fake_video_renderers_.find(id) ==
476 fake_video_renderers_.end());
477 fake_video_renderers_[id] = new webrtc::FakeVideoTrackRenderer(
478 media_stream->GetVideoTracks()[i]);
479 }
480 }
481 virtual void OnRemoveStream(webrtc::MediaStreamInterface* media_stream) {}
482 virtual void OnRenegotiationNeeded() {}
483 virtual void OnIceConnectionChange(
484 webrtc::PeerConnectionInterface::IceConnectionState new_state) {
485 EXPECT_EQ(peer_connection_->ice_connection_state(), new_state);
486 }
487 virtual void OnIceGatheringChange(
488 webrtc::PeerConnectionInterface::IceGatheringState new_state) {
489 EXPECT_EQ(peer_connection_->ice_gathering_state(), new_state);
490 }
491 virtual void OnIceCandidate(
492 const webrtc::IceCandidateInterface* /*candidate*/) {}
493
494 webrtc::PeerConnectionInterface* pc() {
495 return peer_connection_.get();
496 }
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000497 void StopVideoCapturers() {
498 for (std::vector<cricket::VideoCapturer*>::iterator it =
499 video_capturers_.begin(); it != video_capturers_.end(); ++it) {
500 (*it)->Stop();
501 }
502 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503
504 protected:
505 explicit PeerConnectionTestClientBase(const std::string& id)
506 : id_(id),
507 expect_ice_restart_(false),
508 fake_video_decoder_factory_(NULL),
509 fake_video_encoder_factory_(NULL),
510 video_decoder_factory_enabled_(false),
511 signaling_message_receiver_(NULL) {
512 }
Joachim Bauch04e5b492015-05-29 09:40:39 +0200513 bool Init(const MediaConstraintsInterface* constraints,
514 const PeerConnectionFactory::Options* options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 EXPECT_TRUE(!peer_connection_);
516 EXPECT_TRUE(!peer_connection_factory_);
517 allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
518 if (!allocator_factory_) {
519 return false;
520 }
deadbeefee8c6d32015-08-13 14:27:18 -0700521 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522
523 if (fake_audio_capture_module_ == NULL) {
524 return false;
525 }
526 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
527 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
528 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000529 rtc::Thread::Current(), rtc::Thread::Current(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530 fake_audio_capture_module_, fake_video_encoder_factory_,
531 fake_video_decoder_factory_);
532 if (!peer_connection_factory_) {
533 return false;
534 }
Joachim Bauch04e5b492015-05-29 09:40:39 +0200535 if (options) {
536 peer_connection_factory_->SetOptions(*options);
537 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538 peer_connection_ = CreatePeerConnection(allocator_factory_.get(),
539 constraints);
540 return peer_connection_.get() != NULL;
541 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000542 virtual rtc::scoped_refptr<webrtc::PeerConnectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 CreatePeerConnection(webrtc::PortAllocatorFactoryInterface* factory,
544 const MediaConstraintsInterface* constraints) = 0;
545 MessageReceiver* signaling_message_receiver() {
546 return signaling_message_receiver_;
547 }
548 webrtc::PeerConnectionFactoryInterface* peer_connection_factory() {
549 return peer_connection_factory_.get();
550 }
551
552 virtual bool can_receive_audio() = 0;
553 virtual bool can_receive_video() = 0;
554 const std::string& id() const { return id_; }
555
556 private:
557 class DummyDtmfObserver : public DtmfSenderObserverInterface {
558 public:
559 DummyDtmfObserver() : completed_(false) {}
560
561 // Implements DtmfSenderObserverInterface.
562 void OnToneChange(const std::string& tone) {
563 tones_.push_back(tone);
564 if (tone.empty()) {
565 completed_ = true;
566 }
567 }
568
569 void Verify(const std::vector<std::string>& tones) const {
570 ASSERT_TRUE(tones_.size() == tones.size());
571 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
572 }
573
574 bool completed() const { return completed_; }
575
576 private:
577 bool completed_;
578 std::vector<std::string> tones_;
579 };
580
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000581 rtc::scoped_refptr<webrtc::VideoTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 CreateLocalVideoTrack(const std::string stream_label) {
583 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
584 FakeConstraints source_constraints = video_constraints_;
585 source_constraints.SetMandatoryMaxFrameRate(10);
586
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000587 cricket::FakeVideoCapturer* fake_capturer =
588 new webrtc::FakePeriodicVideoCapturer();
589 video_capturers_.push_back(fake_capturer);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000590 rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 peer_connection_factory_->CreateVideoSource(
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000592 fake_capturer, &source_constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 std::string label = stream_label + kVideoTrackLabelBase;
594 return peer_connection_factory_->CreateVideoTrack(label, source);
595 }
596
597 std::string id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000599 rtc::scoped_refptr<webrtc::PortAllocatorFactoryInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600 allocator_factory_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000601 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
602 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 peer_connection_factory_;
604
605 typedef std::pair<std::string, std::string> IceUfragPwdPair;
606 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
607 bool expect_ice_restart_;
608
609 // Needed to keep track of number of frames send.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000610 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611 // Needed to keep track of number of frames received.
612 typedef std::map<std::string, webrtc::FakeVideoTrackRenderer*> RenderMap;
613 RenderMap fake_video_renderers_;
614 // Needed to keep track of number of frames received when external decoder
615 // used.
616 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_;
617 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_;
618 bool video_decoder_factory_enabled_;
619 webrtc::FakeConstraints video_constraints_;
620
621 // For remote peer communication.
622 MessageReceiver* signaling_message_receiver_;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000623
624 // Store references to the video capturers we've created, so that we can stop
625 // them, if required.
626 std::vector<cricket::VideoCapturer*> video_capturers_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627};
628
629class JsepTestClient
630 : public PeerConnectionTestClientBase<JsepMessageReceiver> {
631 public:
632 static JsepTestClient* CreateClient(
633 const std::string& id,
Joachim Bauch04e5b492015-05-29 09:40:39 +0200634 const MediaConstraintsInterface* constraints,
635 const PeerConnectionFactory::Options* options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636 JsepTestClient* client(new JsepTestClient(id));
Joachim Bauch04e5b492015-05-29 09:40:39 +0200637 if (!client->Init(constraints, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638 delete client;
639 return NULL;
640 }
641 return client;
642 }
643 ~JsepTestClient() {}
644
645 virtual void Negotiate() {
646 Negotiate(true, true);
647 }
648 virtual void Negotiate(bool audio, bool video) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000649 rtc::scoped_ptr<SessionDescriptionInterface> offer;
pbos@webrtc.orgceb956b2014-09-04 15:27:49 +0000650 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651
652 if (offer->description()->GetContentByName("audio")) {
653 offer->description()->GetContentByName("audio")->rejected = !audio;
654 }
655 if (offer->description()->GetContentByName("video")) {
656 offer->description()->GetContentByName("video")->rejected = !video;
657 }
658
659 std::string sdp;
660 EXPECT_TRUE(offer->ToString(&sdp));
661 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
662 signaling_message_receiver()->ReceiveSdpMessage(
663 webrtc::SessionDescriptionInterface::kOffer, sdp);
664 }
665 // JsepMessageReceiver callback.
666 virtual void ReceiveSdpMessage(const std::string& type,
667 std::string& msg) {
668 FilterIncomingSdpMessage(&msg);
669 if (type == webrtc::SessionDescriptionInterface::kOffer) {
670 HandleIncomingOffer(msg);
671 } else {
672 HandleIncomingAnswer(msg);
673 }
674 }
675 // JsepMessageReceiver callback.
676 virtual void ReceiveIceMessage(const std::string& sdp_mid,
677 int sdp_mline_index,
678 const std::string& msg) {
679 LOG(INFO) << id() << "ReceiveIceMessage";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000680 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, NULL));
682 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
683 }
684 // Implements PeerConnectionObserver functions needed by Jsep.
685 virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
686 LOG(INFO) << id() << "OnIceCandidate";
687
688 std::string ice_sdp;
689 EXPECT_TRUE(candidate->ToString(&ice_sdp));
690 if (signaling_message_receiver() == NULL) {
691 // Remote party may be deleted.
692 return;
693 }
694 signaling_message_receiver()->ReceiveIceMessage(candidate->sdp_mid(),
695 candidate->sdp_mline_index(), ice_sdp);
696 }
697
698 void IceRestart() {
699 session_description_constraints_.SetMandatoryIceRestart(true);
700 SetExpectIceRestart(true);
701 }
702
703 void SetReceiveAudioVideo(bool audio, bool video) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000704 SetReceiveAudio(audio);
705 SetReceiveVideo(video);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 ASSERT_EQ(audio, can_receive_audio());
707 ASSERT_EQ(video, can_receive_video());
708 }
709
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000710 void SetReceiveAudio(bool audio) {
711 if (audio && can_receive_audio())
712 return;
713 session_description_constraints_.SetMandatoryReceiveAudio(audio);
714 }
715
716 void SetReceiveVideo(bool video) {
717 if (video && can_receive_video())
718 return;
719 session_description_constraints_.SetMandatoryReceiveVideo(video);
720 }
721
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 void RemoveMsidFromReceivedSdp(bool remove) {
723 remove_msid_ = remove;
724 }
725
726 void RemoveSdesCryptoFromReceivedSdp(bool remove) {
727 remove_sdes_ = remove;
728 }
729
730 void RemoveBundleFromReceivedSdp(bool remove) {
731 remove_bundle_ = remove;
732 }
733
734 virtual bool can_receive_audio() {
735 bool value;
736 if (webrtc::FindConstraint(&session_description_constraints_,
737 MediaConstraintsInterface::kOfferToReceiveAudio, &value, NULL)) {
738 return value;
739 }
740 return true;
741 }
742
743 virtual bool can_receive_video() {
744 bool value;
745 if (webrtc::FindConstraint(&session_description_constraints_,
746 MediaConstraintsInterface::kOfferToReceiveVideo, &value, NULL)) {
747 return value;
748 }
749 return true;
750 }
751
752 virtual void OnIceComplete() {
753 LOG(INFO) << id() << "OnIceComplete";
754 }
755
756 virtual void OnDataChannel(DataChannelInterface* data_channel) {
757 LOG(INFO) << id() << "OnDataChannel";
758 data_channel_ = data_channel;
759 data_observer_.reset(new MockDataChannelObserver(data_channel));
760 }
761
762 void CreateDataChannel() {
763 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel,
764 NULL);
765 ASSERT_TRUE(data_channel_.get() != NULL);
766 data_observer_.reset(new MockDataChannelObserver(data_channel_));
767 }
768
769 DataChannelInterface* data_channel() { return data_channel_; }
770 const MockDataChannelObserver* data_observer() const {
771 return data_observer_.get();
772 }
773
774 protected:
775 explicit JsepTestClient(const std::string& id)
776 : PeerConnectionTestClientBase<JsepMessageReceiver>(id),
777 remove_msid_(false),
778 remove_bundle_(false),
779 remove_sdes_(false) {
780 }
781
Henrik Boström5e56c592015-08-11 10:33:13 +0200782 rtc::scoped_refptr<webrtc::PeerConnectionInterface>
783 CreatePeerConnection(
784 webrtc::PortAllocatorFactoryInterface* factory,
785 const MediaConstraintsInterface* constraints) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 // CreatePeerConnection with IceServers.
787 webrtc::PeerConnectionInterface::IceServers ice_servers;
788 webrtc::PeerConnectionInterface::IceServer ice_server;
789 ice_server.uri = "stun:stun.l.google.com:19302";
790 ice_servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000791
Henrik Boström5e56c592015-08-11 10:33:13 +0200792 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store(
793 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
794 : nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795 return peer_connection_factory()->CreatePeerConnection(
Henrik Boström5e56c592015-08-11 10:33:13 +0200796 ice_servers, constraints, factory, dtls_identity_store.Pass(), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 }
798
799 void HandleIncomingOffer(const std::string& msg) {
800 LOG(INFO) << id() << "HandleIncomingOffer ";
801 if (NumberOfLocalMediaStreams() == 0) {
802 // If we are not sending any streams ourselves it is time to add some.
803 AddMediaStream(true, true);
804 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000805 rtc::scoped_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 webrtc::CreateSessionDescription("offer", msg, NULL));
807 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000808 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 EXPECT_TRUE(DoCreateAnswer(answer.use()));
810 std::string sdp;
811 EXPECT_TRUE(answer->ToString(&sdp));
812 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
813 if (signaling_message_receiver()) {
814 signaling_message_receiver()->ReceiveSdpMessage(
815 webrtc::SessionDescriptionInterface::kAnswer, sdp);
816 }
817 }
818
819 void HandleIncomingAnswer(const std::string& msg) {
820 LOG(INFO) << id() << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000821 rtc::scoped_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 webrtc::CreateSessionDescription("answer", msg, NULL));
823 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
824 }
825
826 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
827 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000828 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
829 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 MockCreateSessionDescriptionObserver>());
831 if (offer) {
832 pc()->CreateOffer(observer, &session_description_constraints_);
833 } else {
834 pc()->CreateAnswer(observer, &session_description_constraints_);
835 }
836 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
837 *desc = observer->release_desc();
838 if (observer->result() && ExpectIceRestart()) {
839 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
840 }
841 return observer->result();
842 }
843
844 bool DoCreateOffer(SessionDescriptionInterface** desc) {
845 return DoCreateOfferAnswer(desc, true);
846 }
847
848 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
849 return DoCreateOfferAnswer(desc, false);
850 }
851
852 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000853 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
854 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 MockSetSessionDescriptionObserver>());
856 LOG(INFO) << id() << "SetLocalDescription ";
857 pc()->SetLocalDescription(observer, desc);
858 // Ignore the observer result. If we wait for the result with
859 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
860 // before the offer which is an error.
861 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000862 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 // ProcessMessages waits at least 1ms but processes all messages before
864 // returning. Since this test is synchronous and send messages to the remote
865 // peer whenever a callback is invoked, this can lead to messages being
866 // sent to the remote peer in the wrong order.
867 // TODO(perkj): Find a way to check the result without risking that the
868 // order of sent messages are changed. Ex- by posting all messages that are
869 // sent to the remote peer.
870 return true;
871 }
872
873 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000874 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
875 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876 MockSetSessionDescriptionObserver>());
877 LOG(INFO) << id() << "SetRemoteDescription ";
878 pc()->SetRemoteDescription(observer, desc);
879 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
880 return observer->result();
881 }
882
883 // This modifies all received SDP messages before they are processed.
884 void FilterIncomingSdpMessage(std::string* sdp) {
885 if (remove_msid_) {
886 const char kSdpSsrcAttribute[] = "a=ssrc:";
887 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
888 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
889 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
890 }
891 if (remove_bundle_) {
892 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
893 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
894 }
895 if (remove_sdes_) {
896 const char kSdpSdesCryptoAttribute[] = "a=crypto";
897 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
898 }
899 }
900
901 private:
902 webrtc::FakeConstraints session_description_constraints_;
903 bool remove_msid_; // True if MSID should be removed in received SDP.
904 bool remove_bundle_; // True if bundle should be removed in received SDP.
905 bool remove_sdes_; // True if a=crypto should be removed in received SDP.
906
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
911template <typename SignalingClass>
912class P2PTestConductor : public testing::Test {
913 public:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000914 P2PTestConductor()
915 : pss_(new rtc::PhysicalSocketServer),
916 ss_(new rtc::VirtualSocketServer(pss_.get())),
917 ss_scope_(ss_.get()) {}
918
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 bool SessionActive() {
920 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000921 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000923
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924 // Return true if the number of frames provided have been received or it is
925 // known that that will never occur (e.g. no frames will be sent or
926 // captured).
927 bool FramesNotPending(int audio_frames_to_receive,
928 int video_frames_to_receive) {
929 return VideoFramesReceivedCheck(video_frames_to_receive) &&
930 AudioFramesReceivedCheck(audio_frames_to_receive);
931 }
932 bool AudioFramesReceivedCheck(int frames_received) {
933 return initiating_client_->AudioFramesReceivedCheck(frames_received) &&
934 receiving_client_->AudioFramesReceivedCheck(frames_received);
935 }
936 bool VideoFramesReceivedCheck(int frames_received) {
937 return initiating_client_->VideoFramesReceivedCheck(frames_received) &&
938 receiving_client_->VideoFramesReceivedCheck(frames_received);
939 }
940 void VerifyDtmf() {
941 initiating_client_->VerifyDtmf();
942 receiving_client_->VerifyDtmf();
943 }
944
945 void TestUpdateOfferWithRejectedContent() {
946 initiating_client_->Negotiate(true, false);
947 EXPECT_TRUE_WAIT(
948 FramesNotPending(kEndAudioFrameCount * 2, kEndVideoFrameCount),
949 kMaxWaitForFramesMs);
950 // There shouldn't be any more video frame after the new offer is
951 // negotiated.
952 EXPECT_FALSE(VideoFramesReceivedCheck(kEndVideoFrameCount + 1));
953 }
954
955 void VerifyRenderedSize(int width, int height) {
956 EXPECT_EQ(width, receiving_client()->rendered_width());
957 EXPECT_EQ(height, receiving_client()->rendered_height());
958 EXPECT_EQ(width, initializing_client()->rendered_width());
959 EXPECT_EQ(height, initializing_client()->rendered_height());
960 }
961
962 void VerifySessionDescriptions() {
963 initiating_client_->VerifyRejectedMediaInSessionDescription();
964 receiving_client_->VerifyRejectedMediaInSessionDescription();
965 initiating_client_->VerifyLocalIceUfragAndPassword();
966 receiving_client_->VerifyLocalIceUfragAndPassword();
967 }
968
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969 ~P2PTestConductor() {
970 if (initiating_client_) {
971 initiating_client_->set_signaling_message_receiver(NULL);
972 }
973 if (receiving_client_) {
974 receiving_client_->set_signaling_message_receiver(NULL);
975 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 }
977
978 bool CreateTestClients() {
979 return CreateTestClients(NULL, NULL);
980 }
981
982 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
983 MediaConstraintsInterface* recv_constraints) {
Joachim Bauch04e5b492015-05-29 09:40:39 +0200984 return CreateTestClients(init_constraints, NULL, recv_constraints, NULL);
985 }
986
987 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
988 PeerConnectionFactory::Options* init_options,
989 MediaConstraintsInterface* recv_constraints,
990 PeerConnectionFactory::Options* recv_options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 initiating_client_.reset(SignalingClass::CreateClient("Caller: ",
Joachim Bauch04e5b492015-05-29 09:40:39 +0200992 init_constraints,
993 init_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 receiving_client_.reset(SignalingClass::CreateClient("Callee: ",
Joachim Bauch04e5b492015-05-29 09:40:39 +0200995 recv_constraints,
996 recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000997 if (!initiating_client_ || !receiving_client_) {
998 return false;
999 }
1000 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
1001 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1002 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();
1024 const int kMaxWaitForActivationMs = 5000;
1025 // Assert true is used here since next tests are guaranteed to fail and
1026 // would eat up 5 seconds.
1027 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1028 VerifySessionDescriptions();
1029
1030
1031 int audio_frame_count = kEndAudioFrameCount;
1032 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1033 if (!initiating_client_->can_receive_audio() ||
1034 !receiving_client_->can_receive_audio()) {
1035 audio_frame_count = -1;
1036 }
1037 int video_frame_count = kEndVideoFrameCount;
1038 if (!initiating_client_->can_receive_video() ||
1039 !receiving_client_->can_receive_video()) {
1040 video_frame_count = -1;
1041 }
1042
1043 if (audio_frame_count != -1 || video_frame_count != -1) {
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001044 // Audio or video is expected to flow, so both clients should reach the
1045 // Connected state, and the offerer (ICE controller) should proceed to
1046 // Completed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 // Note: These tests have been observed to fail under heavy load at
1048 // shorter timeouts, so they may be flaky.
1049 EXPECT_EQ_WAIT(
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001050 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051 initiating_client_->ice_connection_state(),
1052 kMaxWaitForFramesMs);
1053 EXPECT_EQ_WAIT(
1054 webrtc::PeerConnectionInterface::kIceConnectionConnected,
1055 receiving_client_->ice_connection_state(),
1056 kMaxWaitForFramesMs);
1057 }
1058
1059 if (initiating_client_->can_receive_audio() ||
1060 initiating_client_->can_receive_video()) {
1061 // The initiating client can receive media, so it must produce candidates
1062 // that will serve as destinations for that media.
1063 // TODO(bemasc): Understand why the state is not already Complete here, as
1064 // seems to be the case for the receiving client. This may indicate a bug
1065 // in the ICE gathering system.
1066 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1067 initiating_client_->ice_gathering_state());
1068 }
1069 if (receiving_client_->can_receive_audio() ||
1070 receiving_client_->can_receive_video()) {
1071 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1072 receiving_client_->ice_gathering_state(),
1073 kMaxWaitForFramesMs);
1074 }
1075
1076 EXPECT_TRUE_WAIT(FramesNotPending(audio_frame_count, video_frame_count),
1077 kMaxWaitForFramesMs);
1078 }
1079
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001080 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1081 // Messages may get lost on the unreliable DataChannel, so we send multiple
1082 // times to avoid test flakiness.
1083 static const size_t kSendAttempts = 5;
1084
1085 for (size_t i = 0; i < kSendAttempts; ++i) {
1086 dc->Send(DataBuffer(data));
1087 }
1088 }
1089
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001090 SignalingClass* initializing_client() { return initiating_client_.get(); }
1091 SignalingClass* receiving_client() { return receiving_client_.get(); }
1092
1093 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001094 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1095 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1096 rtc::SocketServerScope ss_scope_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001097 rtc::scoped_ptr<SignalingClass> initiating_client_;
1098 rtc::scoped_ptr<SignalingClass> receiving_client_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099};
1100typedef P2PTestConductor<JsepTestClient> JsepPeerConnectionP2PTestClient;
1101
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001102// Disable for TSan v2, see
1103// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1104#if !defined(THREAD_SANITIZER)
1105
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001106// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001107// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1108// See issue webrtc/2378.
1109TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001110 ASSERT_TRUE(CreateTestClients());
1111 LocalP2PTest();
1112 VerifyDtmf();
1113}
1114
1115// This test sets up a Jsep call between two parties and test that we can get a
1116// video aspect ratio of 16:9.
1117TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTest16To9) {
1118 ASSERT_TRUE(CreateTestClients());
1119 FakeConstraints constraint;
1120 double requested_ratio = 640.0/360;
1121 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1122 SetVideoConstraints(constraint, constraint);
1123 LocalP2PTest();
1124
1125 ASSERT_LE(0, initializing_client()->rendered_height());
1126 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001127 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128 initializing_client()->rendered_height();
1129 EXPECT_LE(requested_ratio, initiating_video_ratio);
1130
1131 ASSERT_LE(0, receiving_client()->rendered_height());
1132 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001133 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001134 receiving_client()->rendered_height();
1135 EXPECT_LE(requested_ratio, receiving_video_ratio);
1136}
1137
1138// This test sets up a Jsep call between two parties and test that the
1139// received video has a resolution of 1280*720.
1140// TODO(mallinath): Enable when
1141// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
1142TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTest1280By720) {
1143 ASSERT_TRUE(CreateTestClients());
1144 FakeConstraints constraint;
1145 constraint.SetMandatoryMinWidth(1280);
1146 constraint.SetMandatoryMinHeight(720);
1147 SetVideoConstraints(constraint, constraint);
1148 LocalP2PTest();
1149 VerifyRenderedSize(1280, 720);
1150}
1151
1152// This test sets up a call between two endpoints that are configured to use
1153// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
1154TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtls) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001155 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001156 FakeConstraints setup_constraints;
1157 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1158 true);
1159 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1160 LocalP2PTest();
1161 VerifyRenderedSize(640, 480);
1162}
1163
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001164// This test sets up a audio call initially and then upgrades to audio/video,
1165// using DTLS.
mallinath@webrtc.org50bc5532013-10-21 17:58:35 +00001166TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001167 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001168 FakeConstraints setup_constraints;
1169 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1170 true);
1171 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1172 receiving_client()->SetReceiveAudioVideo(true, false);
1173 LocalP2PTest();
1174 receiving_client()->SetReceiveAudioVideo(true, true);
1175 receiving_client()->Negotiate();
1176}
1177
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001178// This test sets up a call between two endpoints that are configured to use
1179// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1180// negotiated and used for transport.
1181TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001182 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183 FakeConstraints setup_constraints;
1184 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1185 true);
1186 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1187 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1188 LocalP2PTest();
1189 VerifyRenderedSize(640, 480);
1190}
1191
1192// This test sets up a Jsep call between two parties, and the callee only
1193// accept to receive video.
solenberg@webrtc.org503c3362015-02-12 13:12:50 +00001194TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195 ASSERT_TRUE(CreateTestClients());
1196 receiving_client()->SetReceiveAudioVideo(false, true);
1197 LocalP2PTest();
1198}
1199
1200// This test sets up a Jsep call between two parties, and the callee only
1201// accept to receive audio.
solenberg@webrtc.org503c3362015-02-12 13:12:50 +00001202TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203 ASSERT_TRUE(CreateTestClients());
1204 receiving_client()->SetReceiveAudioVideo(true, false);
1205 LocalP2PTest();
1206}
1207
1208// This test sets up a Jsep call between two parties, and the callee reject both
1209// audio and video.
1210TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerNone) {
1211 ASSERT_TRUE(CreateTestClients());
1212 receiving_client()->SetReceiveAudioVideo(false, false);
1213 LocalP2PTest();
1214}
1215
1216// This test sets up an audio and video call between two parties. After the call
1217// runs for a while (10 frames), the caller sends an update offer with video
1218// being rejected. Once the re-negotiation is done, the video flow should stop
1219// and the audio flow should continue.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001220// Disabled due to b/14955157.
1221TEST_F(JsepPeerConnectionP2PTestClient,
1222 DISABLED_UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223 ASSERT_TRUE(CreateTestClients());
1224 LocalP2PTest();
1225 TestUpdateOfferWithRejectedContent();
1226}
1227
1228// This test sets up a Jsep call between two parties. The MSID is removed from
1229// the SDP strings from the caller.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001230// Disabled due to b/14955157.
1231TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001232 ASSERT_TRUE(CreateTestClients());
1233 receiving_client()->RemoveMsidFromReceivedSdp(true);
1234 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1235 // audio and video is muxed when MSID is disabled. Remove
1236 // SetRemoveBundleFromSdp once
1237 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1238 receiving_client()->RemoveBundleFromReceivedSdp(true);
1239 LocalP2PTest();
1240}
1241
1242// This test sets up a Jsep call between two parties and the initiating peer
1243// sends two steams.
1244// TODO(perkj): Disabled due to
1245// https://code.google.com/p/webrtc/issues/detail?id=1454
1246TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestTwoStreams) {
1247 ASSERT_TRUE(CreateTestClients());
1248 // Set optional video constraint to max 320pixels to decrease CPU usage.
1249 FakeConstraints constraint;
1250 constraint.SetOptionalMaxWidth(320);
1251 SetVideoConstraints(constraint, constraint);
1252 initializing_client()->AddMediaStream(true, true);
1253 initializing_client()->AddMediaStream(false, true);
1254 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1255 LocalP2PTest();
1256 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1257}
1258
1259// Test that we can receive the audio output level from a remote audio track.
1260TEST_F(JsepPeerConnectionP2PTestClient, GetAudioOutputLevelStats) {
1261 ASSERT_TRUE(CreateTestClients());
1262 LocalP2PTest();
1263
1264 StreamCollectionInterface* remote_streams =
1265 initializing_client()->remote_streams();
1266 ASSERT_GT(remote_streams->count(), 0u);
1267 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1268 MediaStreamTrackInterface* remote_audio_track =
1269 remote_streams->at(0)->GetAudioTracks()[0];
1270
1271 // Get the audio output level stats. Note that the level is not available
1272 // until a RTCP packet has been received.
1273 EXPECT_TRUE_WAIT(
1274 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1275 kMaxWaitForStatsMs);
1276}
1277
1278// Test that an audio input level is reported.
1279TEST_F(JsepPeerConnectionP2PTestClient, GetAudioInputLevelStats) {
1280 ASSERT_TRUE(CreateTestClients());
1281 LocalP2PTest();
1282
1283 // Get the audio input level stats. The level should be available very
1284 // soon after the test starts.
1285 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1286 kMaxWaitForStatsMs);
1287}
1288
1289// Test that we can get incoming byte counts from both audio and video tracks.
1290TEST_F(JsepPeerConnectionP2PTestClient, GetBytesReceivedStats) {
1291 ASSERT_TRUE(CreateTestClients());
1292 LocalP2PTest();
1293
1294 StreamCollectionInterface* remote_streams =
1295 initializing_client()->remote_streams();
1296 ASSERT_GT(remote_streams->count(), 0u);
1297 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1298 MediaStreamTrackInterface* remote_audio_track =
1299 remote_streams->at(0)->GetAudioTracks()[0];
1300 EXPECT_TRUE_WAIT(
1301 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1302 kMaxWaitForStatsMs);
1303
1304 MediaStreamTrackInterface* remote_video_track =
1305 remote_streams->at(0)->GetVideoTracks()[0];
1306 EXPECT_TRUE_WAIT(
1307 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1308 kMaxWaitForStatsMs);
1309}
1310
1311// Test that we can get outgoing byte counts from both audio and video tracks.
1312TEST_F(JsepPeerConnectionP2PTestClient, GetBytesSentStats) {
1313 ASSERT_TRUE(CreateTestClients());
1314 LocalP2PTest();
1315
1316 StreamCollectionInterface* local_streams =
1317 initializing_client()->local_streams();
1318 ASSERT_GT(local_streams->count(), 0u);
1319 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1320 MediaStreamTrackInterface* local_audio_track =
1321 local_streams->at(0)->GetAudioTracks()[0];
1322 EXPECT_TRUE_WAIT(
1323 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1324 kMaxWaitForStatsMs);
1325
1326 MediaStreamTrackInterface* local_video_track =
1327 local_streams->at(0)->GetVideoTracks()[0];
1328 EXPECT_TRUE_WAIT(
1329 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1330 kMaxWaitForStatsMs);
1331}
1332
Joachim Bauch04e5b492015-05-29 09:40:39 +02001333// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
1334TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12None) {
1335 PeerConnectionFactory::Options init_options;
1336 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1337 PeerConnectionFactory::Options recv_options;
1338 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1339 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001340 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1341 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1342 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001343 LocalP2PTest();
1344
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001345 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::GetSslCipherSuiteName(
1346 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1347 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1348 initializing_client()->GetDtlsCipherStats(),
1349 kMaxWaitForStatsMs);
1350 EXPECT_EQ(1, init_observer->GetEnumCounter(
1351 webrtc::kEnumCounterAudioSslCipher,
1352 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1353 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001354
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001355 EXPECT_EQ_WAIT(kDefaultSrtpCipher,
1356 initializing_client()->GetSrtpCipherStats(),
1357 kMaxWaitForStatsMs);
1358 EXPECT_EQ(1, init_observer->GetEnumCounter(
1359 webrtc::kEnumCounterAudioSrtpCipher,
1360 rtc::GetSrtpCryptoSuiteFromName(kDefaultSrtpCipher)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001361}
1362
1363// Test that DTLS 1.2 is used if both ends support it.
1364TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Both) {
1365 PeerConnectionFactory::Options init_options;
1366 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1367 PeerConnectionFactory::Options recv_options;
1368 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1369 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001370 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1371 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1372 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001373 LocalP2PTest();
1374
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001375 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::GetSslCipherSuiteName(
1376 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1377 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)),
1378 initializing_client()->GetDtlsCipherStats(),
1379 kMaxWaitForStatsMs);
1380 EXPECT_EQ(1, init_observer->GetEnumCounter(
1381 webrtc::kEnumCounterAudioSslCipher,
1382 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1383 rtc::SSL_PROTOCOL_DTLS_12, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001384
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001385 EXPECT_EQ_WAIT(kDefaultSrtpCipher,
1386 initializing_client()->GetSrtpCipherStats(),
1387 kMaxWaitForStatsMs);
1388 EXPECT_EQ(1, init_observer->GetEnumCounter(
1389 webrtc::kEnumCounterAudioSrtpCipher,
1390 rtc::GetSrtpCryptoSuiteFromName(kDefaultSrtpCipher)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001391}
1392
1393// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1394// received supports 1.0.
1395TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Init) {
1396 PeerConnectionFactory::Options init_options;
1397 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1398 PeerConnectionFactory::Options recv_options;
1399 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1400 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001401 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1402 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1403 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001404 LocalP2PTest();
1405
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001406 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::GetSslCipherSuiteName(
1407 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1408 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1409 initializing_client()->GetDtlsCipherStats(),
1410 kMaxWaitForStatsMs);
1411 EXPECT_EQ(1, init_observer->GetEnumCounter(
1412 webrtc::kEnumCounterAudioSslCipher,
1413 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1414 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001415
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001416 EXPECT_EQ_WAIT(kDefaultSrtpCipher,
1417 initializing_client()->GetSrtpCipherStats(),
1418 kMaxWaitForStatsMs);
1419 EXPECT_EQ(1, init_observer->GetEnumCounter(
1420 webrtc::kEnumCounterAudioSrtpCipher,
1421 rtc::GetSrtpCryptoSuiteFromName(kDefaultSrtpCipher)));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001422}
1423
1424// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1425// received supports 1.2.
1426TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Recv) {
1427 PeerConnectionFactory::Options init_options;
1428 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1429 PeerConnectionFactory::Options recv_options;
1430 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1431 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001432 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1433 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1434 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001435 LocalP2PTest();
1436
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001437 EXPECT_EQ_WAIT(rtc::SSLStreamAdapter::GetSslCipherSuiteName(
1438 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1439 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)),
1440 initializing_client()->GetDtlsCipherStats(),
1441 kMaxWaitForStatsMs);
1442 EXPECT_EQ(1, init_observer->GetEnumCounter(
1443 webrtc::kEnumCounterAudioSslCipher,
1444 rtc::SSLStreamAdapter::GetDefaultSslCipherForTest(
1445 rtc::SSL_PROTOCOL_DTLS_10, rtc::KT_DEFAULT)));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001446
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001447 EXPECT_EQ_WAIT(kDefaultSrtpCipher,
1448 initializing_client()->GetSrtpCipherStats(),
1449 kMaxWaitForStatsMs);
1450 EXPECT_EQ(1, init_observer->GetEnumCounter(
1451 webrtc::kEnumCounterAudioSrtpCipher,
1452 rtc::GetSrtpCryptoSuiteFromName(kDefaultSrtpCipher)));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001453}
1454
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001455// This test sets up a call between two parties with audio, video and data.
1456TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDataChannel) {
1457 FakeConstraints setup_constraints;
1458 setup_constraints.SetAllowRtpDataChannels();
1459 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1460 initializing_client()->CreateDataChannel();
1461 LocalP2PTest();
1462 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1463 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1464 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1465 kMaxWaitMs);
1466 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1467 kMaxWaitMs);
1468
1469 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001470
1471 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001472 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1473 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001474
1475 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001476 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1477 kMaxWaitMs);
1478
1479 receiving_client()->data_channel()->Close();
1480 // Send new offer and answer.
1481 receiving_client()->Negotiate();
1482 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1483 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1484}
1485
1486// This test sets up a call between two parties and creates a data channel.
1487// The test tests that received data is buffered unless an observer has been
1488// registered.
1489// Rtp data channels can receive data before the underlying
1490// transport has detected that a channel is writable and thus data can be
1491// received before the data channel state changes to open. That is hard to test
1492// but the same buffering is used in that case.
1493TEST_F(JsepPeerConnectionP2PTestClient, RegisterDataChannelObserver) {
1494 FakeConstraints setup_constraints;
1495 setup_constraints.SetAllowRtpDataChannels();
1496 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1497 initializing_client()->CreateDataChannel();
1498 initializing_client()->Negotiate();
1499
1500 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1501 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1502 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1503 kMaxWaitMs);
1504 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1505 receiving_client()->data_channel()->state(), kMaxWaitMs);
1506
1507 // Unregister the existing observer.
1508 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001509
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001510 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001511 SendRtpData(initializing_client()->data_channel(), data);
1512
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513 // Wait a while to allow the sent data to arrive before an observer is
1514 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001515 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001516
1517 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1518 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1519}
1520
1521// This test sets up a call between two parties with audio, video and but only
1522// the initiating client support data.
1523TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001524 FakeConstraints setup_constraints_1;
1525 setup_constraints_1.SetAllowRtpDataChannels();
1526 // Must disable DTLS to make negotiation succeed.
1527 setup_constraints_1.SetMandatory(
1528 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1529 FakeConstraints setup_constraints_2;
1530 setup_constraints_2.SetMandatory(
1531 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1532 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001533 initializing_client()->CreateDataChannel();
1534 LocalP2PTest();
1535 EXPECT_TRUE(initializing_client()->data_channel() != NULL);
1536 EXPECT_FALSE(receiving_client()->data_channel());
1537 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1538}
1539
1540// This test sets up a call between two parties with audio, video. When audio
1541// and video is setup and flowing and data channel is negotiated.
1542TEST_F(JsepPeerConnectionP2PTestClient, AddDataChannelAfterRenegotiation) {
1543 FakeConstraints setup_constraints;
1544 setup_constraints.SetAllowRtpDataChannels();
1545 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1546 LocalP2PTest();
1547 initializing_client()->CreateDataChannel();
1548 // Send new offer and answer.
1549 initializing_client()->Negotiate();
1550 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1551 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1552 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1553 kMaxWaitMs);
1554 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1555 kMaxWaitMs);
1556}
1557
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001558// This test sets up a Jsep call with SCTP DataChannel and verifies the
1559// negotiation is completed without error.
1560#ifdef HAVE_SCTP
1561TEST_F(JsepPeerConnectionP2PTestClient, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001562 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001563 FakeConstraints constraints;
1564 constraints.SetMandatory(
1565 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1566 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1567 initializing_client()->CreateDataChannel();
1568 initializing_client()->Negotiate(false, false);
1569}
1570#endif
1571
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001572// This test sets up a call between two parties with audio, and video.
1573// During the call, the initializing side restart ice and the test verifies that
1574// new ice candidates are generated and audio and video still can flow.
1575TEST_F(JsepPeerConnectionP2PTestClient, IceRestart) {
1576 ASSERT_TRUE(CreateTestClients());
1577
1578 // Negotiate and wait for ice completion and make sure audio and video plays.
1579 LocalP2PTest();
1580
1581 // Create a SDP string of the first audio candidate for both clients.
1582 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1583 initializing_client()->pc()->local_description()->candidates(0);
1584 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1585 receiving_client()->pc()->local_description()->candidates(0);
1586 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1587 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1588 std::string initiator_candidate;
1589 EXPECT_TRUE(
1590 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1591 std::string receiver_candidate;
1592 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1593
1594 // Restart ice on the initializing client.
1595 receiving_client()->SetExpectIceRestart(true);
1596 initializing_client()->IceRestart();
1597
1598 // Negotiate and wait for ice completion again and make sure audio and video
1599 // plays.
1600 LocalP2PTest();
1601
1602 // Create a SDP string of the first audio candidate for both clients again.
1603 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1604 initializing_client()->pc()->local_description()->candidates(0);
1605 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1606 receiving_client()->pc()->local_description()->candidates(0);
1607 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1608 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1609 std::string initiator_candidate_restart;
1610 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1611 &initiator_candidate_restart));
1612 std::string receiver_candidate_restart;
1613 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1614 &receiver_candidate_restart));
1615
1616 // Verify that the first candidates in the local session descriptions has
1617 // changed.
1618 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1619 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1620}
1621
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001622// This test sets up a Jsep call between two parties with external
1623// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001624// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1625// See issue webrtc/2378.
1626TEST_F(JsepPeerConnectionP2PTestClient,
1627 DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628 ASSERT_TRUE(CreateTestClients());
1629 EnableVideoDecoderFactory();
1630 LocalP2PTest();
1631}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001632
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001633#endif // if !defined(THREAD_SANITIZER)