blob: 08e6cef1c6731939ac530ad018aae75c4e3e3843 [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 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521 fake_audio_capture_module_ = FakeAudioCaptureModule::Create(
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000522 rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523
524 if (fake_audio_capture_module_ == NULL) {
525 return false;
526 }
527 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
528 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
529 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000530 rtc::Thread::Current(), rtc::Thread::Current(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 fake_audio_capture_module_, fake_video_encoder_factory_,
532 fake_video_decoder_factory_);
533 if (!peer_connection_factory_) {
534 return false;
535 }
Joachim Bauch04e5b492015-05-29 09:40:39 +0200536 if (options) {
537 peer_connection_factory_->SetOptions(*options);
538 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 peer_connection_ = CreatePeerConnection(allocator_factory_.get(),
540 constraints);
541 return peer_connection_.get() != NULL;
542 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000543 virtual rtc::scoped_refptr<webrtc::PeerConnectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544 CreatePeerConnection(webrtc::PortAllocatorFactoryInterface* factory,
545 const MediaConstraintsInterface* constraints) = 0;
546 MessageReceiver* signaling_message_receiver() {
547 return signaling_message_receiver_;
548 }
549 webrtc::PeerConnectionFactoryInterface* peer_connection_factory() {
550 return peer_connection_factory_.get();
551 }
552
553 virtual bool can_receive_audio() = 0;
554 virtual bool can_receive_video() = 0;
555 const std::string& id() const { return id_; }
556
557 private:
558 class DummyDtmfObserver : public DtmfSenderObserverInterface {
559 public:
560 DummyDtmfObserver() : completed_(false) {}
561
562 // Implements DtmfSenderObserverInterface.
563 void OnToneChange(const std::string& tone) {
564 tones_.push_back(tone);
565 if (tone.empty()) {
566 completed_ = true;
567 }
568 }
569
570 void Verify(const std::vector<std::string>& tones) const {
571 ASSERT_TRUE(tones_.size() == tones.size());
572 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
573 }
574
575 bool completed() const { return completed_; }
576
577 private:
578 bool completed_;
579 std::vector<std::string> tones_;
580 };
581
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000582 rtc::scoped_refptr<webrtc::VideoTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583 CreateLocalVideoTrack(const std::string stream_label) {
584 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
585 FakeConstraints source_constraints = video_constraints_;
586 source_constraints.SetMandatoryMaxFrameRate(10);
587
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000588 cricket::FakeVideoCapturer* fake_capturer =
589 new webrtc::FakePeriodicVideoCapturer();
590 video_capturers_.push_back(fake_capturer);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000591 rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000592 peer_connection_factory_->CreateVideoSource(
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000593 fake_capturer, &source_constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594 std::string label = stream_label + kVideoTrackLabelBase;
595 return peer_connection_factory_->CreateVideoTrack(label, source);
596 }
597
598 std::string id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000600 rtc::scoped_refptr<webrtc::PortAllocatorFactoryInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601 allocator_factory_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000602 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
603 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 peer_connection_factory_;
605
606 typedef std::pair<std::string, std::string> IceUfragPwdPair;
607 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
608 bool expect_ice_restart_;
609
610 // Needed to keep track of number of frames send.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000611 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 // Needed to keep track of number of frames received.
613 typedef std::map<std::string, webrtc::FakeVideoTrackRenderer*> RenderMap;
614 RenderMap fake_video_renderers_;
615 // Needed to keep track of number of frames received when external decoder
616 // used.
617 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_;
618 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_;
619 bool video_decoder_factory_enabled_;
620 webrtc::FakeConstraints video_constraints_;
621
622 // For remote peer communication.
623 MessageReceiver* signaling_message_receiver_;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000624
625 // Store references to the video capturers we've created, so that we can stop
626 // them, if required.
627 std::vector<cricket::VideoCapturer*> video_capturers_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628};
629
630class JsepTestClient
631 : public PeerConnectionTestClientBase<JsepMessageReceiver> {
632 public:
633 static JsepTestClient* CreateClient(
634 const std::string& id,
Joachim Bauch04e5b492015-05-29 09:40:39 +0200635 const MediaConstraintsInterface* constraints,
636 const PeerConnectionFactory::Options* options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 JsepTestClient* client(new JsepTestClient(id));
Joachim Bauch04e5b492015-05-29 09:40:39 +0200638 if (!client->Init(constraints, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 delete client;
640 return NULL;
641 }
642 return client;
643 }
644 ~JsepTestClient() {}
645
646 virtual void Negotiate() {
647 Negotiate(true, true);
648 }
649 virtual void Negotiate(bool audio, bool video) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000650 rtc::scoped_ptr<SessionDescriptionInterface> offer;
pbos@webrtc.orgceb956b2014-09-04 15:27:49 +0000651 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652
653 if (offer->description()->GetContentByName("audio")) {
654 offer->description()->GetContentByName("audio")->rejected = !audio;
655 }
656 if (offer->description()->GetContentByName("video")) {
657 offer->description()->GetContentByName("video")->rejected = !video;
658 }
659
660 std::string sdp;
661 EXPECT_TRUE(offer->ToString(&sdp));
662 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
663 signaling_message_receiver()->ReceiveSdpMessage(
664 webrtc::SessionDescriptionInterface::kOffer, sdp);
665 }
666 // JsepMessageReceiver callback.
667 virtual void ReceiveSdpMessage(const std::string& type,
668 std::string& msg) {
669 FilterIncomingSdpMessage(&msg);
670 if (type == webrtc::SessionDescriptionInterface::kOffer) {
671 HandleIncomingOffer(msg);
672 } else {
673 HandleIncomingAnswer(msg);
674 }
675 }
676 // JsepMessageReceiver callback.
677 virtual void ReceiveIceMessage(const std::string& sdp_mid,
678 int sdp_mline_index,
679 const std::string& msg) {
680 LOG(INFO) << id() << "ReceiveIceMessage";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000681 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, NULL));
683 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
684 }
685 // Implements PeerConnectionObserver functions needed by Jsep.
686 virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
687 LOG(INFO) << id() << "OnIceCandidate";
688
689 std::string ice_sdp;
690 EXPECT_TRUE(candidate->ToString(&ice_sdp));
691 if (signaling_message_receiver() == NULL) {
692 // Remote party may be deleted.
693 return;
694 }
695 signaling_message_receiver()->ReceiveIceMessage(candidate->sdp_mid(),
696 candidate->sdp_mline_index(), ice_sdp);
697 }
698
699 void IceRestart() {
700 session_description_constraints_.SetMandatoryIceRestart(true);
701 SetExpectIceRestart(true);
702 }
703
704 void SetReceiveAudioVideo(bool audio, bool video) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000705 SetReceiveAudio(audio);
706 SetReceiveVideo(video);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 ASSERT_EQ(audio, can_receive_audio());
708 ASSERT_EQ(video, can_receive_video());
709 }
710
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000711 void SetReceiveAudio(bool audio) {
712 if (audio && can_receive_audio())
713 return;
714 session_description_constraints_.SetMandatoryReceiveAudio(audio);
715 }
716
717 void SetReceiveVideo(bool video) {
718 if (video && can_receive_video())
719 return;
720 session_description_constraints_.SetMandatoryReceiveVideo(video);
721 }
722
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 void RemoveMsidFromReceivedSdp(bool remove) {
724 remove_msid_ = remove;
725 }
726
727 void RemoveSdesCryptoFromReceivedSdp(bool remove) {
728 remove_sdes_ = remove;
729 }
730
731 void RemoveBundleFromReceivedSdp(bool remove) {
732 remove_bundle_ = remove;
733 }
734
735 virtual bool can_receive_audio() {
736 bool value;
737 if (webrtc::FindConstraint(&session_description_constraints_,
738 MediaConstraintsInterface::kOfferToReceiveAudio, &value, NULL)) {
739 return value;
740 }
741 return true;
742 }
743
744 virtual bool can_receive_video() {
745 bool value;
746 if (webrtc::FindConstraint(&session_description_constraints_,
747 MediaConstraintsInterface::kOfferToReceiveVideo, &value, NULL)) {
748 return value;
749 }
750 return true;
751 }
752
753 virtual void OnIceComplete() {
754 LOG(INFO) << id() << "OnIceComplete";
755 }
756
757 virtual void OnDataChannel(DataChannelInterface* data_channel) {
758 LOG(INFO) << id() << "OnDataChannel";
759 data_channel_ = data_channel;
760 data_observer_.reset(new MockDataChannelObserver(data_channel));
761 }
762
763 void CreateDataChannel() {
764 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel,
765 NULL);
766 ASSERT_TRUE(data_channel_.get() != NULL);
767 data_observer_.reset(new MockDataChannelObserver(data_channel_));
768 }
769
770 DataChannelInterface* data_channel() { return data_channel_; }
771 const MockDataChannelObserver* data_observer() const {
772 return data_observer_.get();
773 }
774
775 protected:
776 explicit JsepTestClient(const std::string& id)
777 : PeerConnectionTestClientBase<JsepMessageReceiver>(id),
778 remove_msid_(false),
779 remove_bundle_(false),
780 remove_sdes_(false) {
781 }
782
Henrik Boström5e56c592015-08-11 10:33:13 +0200783 rtc::scoped_refptr<webrtc::PeerConnectionInterface>
784 CreatePeerConnection(
785 webrtc::PortAllocatorFactoryInterface* factory,
786 const MediaConstraintsInterface* constraints) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 // CreatePeerConnection with IceServers.
788 webrtc::PeerConnectionInterface::IceServers ice_servers;
789 webrtc::PeerConnectionInterface::IceServer ice_server;
790 ice_server.uri = "stun:stun.l.google.com:19302";
791 ice_servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000792
Henrik Boström5e56c592015-08-11 10:33:13 +0200793 rtc::scoped_ptr<webrtc::DtlsIdentityStoreInterface> dtls_identity_store(
794 rtc::SSLStreamAdapter::HaveDtlsSrtp() ? new FakeDtlsIdentityStore()
795 : nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 return peer_connection_factory()->CreatePeerConnection(
Henrik Boström5e56c592015-08-11 10:33:13 +0200797 ice_servers, constraints, factory, dtls_identity_store.Pass(), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 }
799
800 void HandleIncomingOffer(const std::string& msg) {
801 LOG(INFO) << id() << "HandleIncomingOffer ";
802 if (NumberOfLocalMediaStreams() == 0) {
803 // If we are not sending any streams ourselves it is time to add some.
804 AddMediaStream(true, true);
805 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000806 rtc::scoped_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 webrtc::CreateSessionDescription("offer", msg, NULL));
808 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000809 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 EXPECT_TRUE(DoCreateAnswer(answer.use()));
811 std::string sdp;
812 EXPECT_TRUE(answer->ToString(&sdp));
813 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
814 if (signaling_message_receiver()) {
815 signaling_message_receiver()->ReceiveSdpMessage(
816 webrtc::SessionDescriptionInterface::kAnswer, sdp);
817 }
818 }
819
820 void HandleIncomingAnswer(const std::string& msg) {
821 LOG(INFO) << id() << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000822 rtc::scoped_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 webrtc::CreateSessionDescription("answer", msg, NULL));
824 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
825 }
826
827 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
828 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000829 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
830 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000831 MockCreateSessionDescriptionObserver>());
832 if (offer) {
833 pc()->CreateOffer(observer, &session_description_constraints_);
834 } else {
835 pc()->CreateAnswer(observer, &session_description_constraints_);
836 }
837 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
838 *desc = observer->release_desc();
839 if (observer->result() && ExpectIceRestart()) {
840 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
841 }
842 return observer->result();
843 }
844
845 bool DoCreateOffer(SessionDescriptionInterface** desc) {
846 return DoCreateOfferAnswer(desc, true);
847 }
848
849 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
850 return DoCreateOfferAnswer(desc, false);
851 }
852
853 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000854 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
855 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856 MockSetSessionDescriptionObserver>());
857 LOG(INFO) << id() << "SetLocalDescription ";
858 pc()->SetLocalDescription(observer, desc);
859 // Ignore the observer result. If we wait for the result with
860 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
861 // before the offer which is an error.
862 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000863 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 // ProcessMessages waits at least 1ms but processes all messages before
865 // returning. Since this test is synchronous and send messages to the remote
866 // peer whenever a callback is invoked, this can lead to messages being
867 // sent to the remote peer in the wrong order.
868 // TODO(perkj): Find a way to check the result without risking that the
869 // order of sent messages are changed. Ex- by posting all messages that are
870 // sent to the remote peer.
871 return true;
872 }
873
874 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000875 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
876 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 MockSetSessionDescriptionObserver>());
878 LOG(INFO) << id() << "SetRemoteDescription ";
879 pc()->SetRemoteDescription(observer, desc);
880 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
881 return observer->result();
882 }
883
884 // This modifies all received SDP messages before they are processed.
885 void FilterIncomingSdpMessage(std::string* sdp) {
886 if (remove_msid_) {
887 const char kSdpSsrcAttribute[] = "a=ssrc:";
888 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
889 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
890 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
891 }
892 if (remove_bundle_) {
893 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
894 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
895 }
896 if (remove_sdes_) {
897 const char kSdpSdesCryptoAttribute[] = "a=crypto";
898 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
899 }
900 }
901
902 private:
903 webrtc::FakeConstraints session_description_constraints_;
904 bool remove_msid_; // True if MSID should be removed in received SDP.
905 bool remove_bundle_; // True if bundle should be removed in received SDP.
906 bool remove_sdes_; // True if a=crypto should be removed in received SDP.
907
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000908 rtc::scoped_refptr<DataChannelInterface> data_channel_;
909 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910};
911
912template <typename SignalingClass>
913class P2PTestConductor : public testing::Test {
914 public:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000915 P2PTestConductor()
916 : pss_(new rtc::PhysicalSocketServer),
917 ss_(new rtc::VirtualSocketServer(pss_.get())),
918 ss_scope_(ss_.get()) {}
919
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 bool SessionActive() {
921 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000922 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000924
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 // Return true if the number of frames provided have been received or it is
926 // known that that will never occur (e.g. no frames will be sent or
927 // captured).
928 bool FramesNotPending(int audio_frames_to_receive,
929 int video_frames_to_receive) {
930 return VideoFramesReceivedCheck(video_frames_to_receive) &&
931 AudioFramesReceivedCheck(audio_frames_to_receive);
932 }
933 bool AudioFramesReceivedCheck(int frames_received) {
934 return initiating_client_->AudioFramesReceivedCheck(frames_received) &&
935 receiving_client_->AudioFramesReceivedCheck(frames_received);
936 }
937 bool VideoFramesReceivedCheck(int frames_received) {
938 return initiating_client_->VideoFramesReceivedCheck(frames_received) &&
939 receiving_client_->VideoFramesReceivedCheck(frames_received);
940 }
941 void VerifyDtmf() {
942 initiating_client_->VerifyDtmf();
943 receiving_client_->VerifyDtmf();
944 }
945
946 void TestUpdateOfferWithRejectedContent() {
947 initiating_client_->Negotiate(true, false);
948 EXPECT_TRUE_WAIT(
949 FramesNotPending(kEndAudioFrameCount * 2, kEndVideoFrameCount),
950 kMaxWaitForFramesMs);
951 // There shouldn't be any more video frame after the new offer is
952 // negotiated.
953 EXPECT_FALSE(VideoFramesReceivedCheck(kEndVideoFrameCount + 1));
954 }
955
956 void VerifyRenderedSize(int width, int height) {
957 EXPECT_EQ(width, receiving_client()->rendered_width());
958 EXPECT_EQ(height, receiving_client()->rendered_height());
959 EXPECT_EQ(width, initializing_client()->rendered_width());
960 EXPECT_EQ(height, initializing_client()->rendered_height());
961 }
962
963 void VerifySessionDescriptions() {
964 initiating_client_->VerifyRejectedMediaInSessionDescription();
965 receiving_client_->VerifyRejectedMediaInSessionDescription();
966 initiating_client_->VerifyLocalIceUfragAndPassword();
967 receiving_client_->VerifyLocalIceUfragAndPassword();
968 }
969
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970 ~P2PTestConductor() {
971 if (initiating_client_) {
972 initiating_client_->set_signaling_message_receiver(NULL);
973 }
974 if (receiving_client_) {
975 receiving_client_->set_signaling_message_receiver(NULL);
976 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000977 }
978
979 bool CreateTestClients() {
980 return CreateTestClients(NULL, NULL);
981 }
982
983 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
984 MediaConstraintsInterface* recv_constraints) {
Joachim Bauch04e5b492015-05-29 09:40:39 +0200985 return CreateTestClients(init_constraints, NULL, recv_constraints, NULL);
986 }
987
988 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
989 PeerConnectionFactory::Options* init_options,
990 MediaConstraintsInterface* recv_constraints,
991 PeerConnectionFactory::Options* recv_options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992 initiating_client_.reset(SignalingClass::CreateClient("Caller: ",
Joachim Bauch04e5b492015-05-29 09:40:39 +0200993 init_constraints,
994 init_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 receiving_client_.reset(SignalingClass::CreateClient("Callee: ",
Joachim Bauch04e5b492015-05-29 09:40:39 +0200996 recv_constraints,
997 recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998 if (!initiating_client_ || !receiving_client_) {
999 return false;
1000 }
1001 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
1002 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1003 return true;
1004 }
1005
1006 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1007 const webrtc::FakeConstraints& recv_constraints) {
1008 initiating_client_->SetVideoConstraints(init_constraints);
1009 receiving_client_->SetVideoConstraints(recv_constraints);
1010 }
1011
1012 void EnableVideoDecoderFactory() {
1013 initiating_client_->EnableVideoDecoderFactory();
1014 receiving_client_->EnableVideoDecoderFactory();
1015 }
1016
1017 // This test sets up a call between two parties. Both parties send static
1018 // frames to each other. Once the test is finished the number of sent frames
1019 // is compared to the number of received frames.
1020 void LocalP2PTest() {
1021 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1022 initiating_client_->AddMediaStream(true, true);
1023 }
1024 initiating_client_->Negotiate();
1025 const int kMaxWaitForActivationMs = 5000;
1026 // Assert true is used here since next tests are guaranteed to fail and
1027 // would eat up 5 seconds.
1028 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1029 VerifySessionDescriptions();
1030
1031
1032 int audio_frame_count = kEndAudioFrameCount;
1033 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1034 if (!initiating_client_->can_receive_audio() ||
1035 !receiving_client_->can_receive_audio()) {
1036 audio_frame_count = -1;
1037 }
1038 int video_frame_count = kEndVideoFrameCount;
1039 if (!initiating_client_->can_receive_video() ||
1040 !receiving_client_->can_receive_video()) {
1041 video_frame_count = -1;
1042 }
1043
1044 if (audio_frame_count != -1 || video_frame_count != -1) {
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001045 // Audio or video is expected to flow, so both clients should reach the
1046 // Connected state, and the offerer (ICE controller) should proceed to
1047 // Completed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001048 // Note: These tests have been observed to fail under heavy load at
1049 // shorter timeouts, so they may be flaky.
1050 EXPECT_EQ_WAIT(
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001051 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052 initiating_client_->ice_connection_state(),
1053 kMaxWaitForFramesMs);
1054 EXPECT_EQ_WAIT(
1055 webrtc::PeerConnectionInterface::kIceConnectionConnected,
1056 receiving_client_->ice_connection_state(),
1057 kMaxWaitForFramesMs);
1058 }
1059
1060 if (initiating_client_->can_receive_audio() ||
1061 initiating_client_->can_receive_video()) {
1062 // The initiating client can receive media, so it must produce candidates
1063 // that will serve as destinations for that media.
1064 // TODO(bemasc): Understand why the state is not already Complete here, as
1065 // seems to be the case for the receiving client. This may indicate a bug
1066 // in the ICE gathering system.
1067 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1068 initiating_client_->ice_gathering_state());
1069 }
1070 if (receiving_client_->can_receive_audio() ||
1071 receiving_client_->can_receive_video()) {
1072 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1073 receiving_client_->ice_gathering_state(),
1074 kMaxWaitForFramesMs);
1075 }
1076
1077 EXPECT_TRUE_WAIT(FramesNotPending(audio_frame_count, video_frame_count),
1078 kMaxWaitForFramesMs);
1079 }
1080
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001081 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1082 // Messages may get lost on the unreliable DataChannel, so we send multiple
1083 // times to avoid test flakiness.
1084 static const size_t kSendAttempts = 5;
1085
1086 for (size_t i = 0; i < kSendAttempts; ++i) {
1087 dc->Send(DataBuffer(data));
1088 }
1089 }
1090
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001091 SignalingClass* initializing_client() { return initiating_client_.get(); }
1092 SignalingClass* receiving_client() { return receiving_client_.get(); }
1093
1094 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001095 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1096 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1097 rtc::SocketServerScope ss_scope_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001098 rtc::scoped_ptr<SignalingClass> initiating_client_;
1099 rtc::scoped_ptr<SignalingClass> receiving_client_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001100};
1101typedef P2PTestConductor<JsepTestClient> JsepPeerConnectionP2PTestClient;
1102
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001103// Disable for TSan v2, see
1104// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1105#if !defined(THREAD_SANITIZER)
1106
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001107// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001108// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1109// See issue webrtc/2378.
1110TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001111 ASSERT_TRUE(CreateTestClients());
1112 LocalP2PTest();
1113 VerifyDtmf();
1114}
1115
1116// This test sets up a Jsep call between two parties and test that we can get a
1117// video aspect ratio of 16:9.
1118TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTest16To9) {
1119 ASSERT_TRUE(CreateTestClients());
1120 FakeConstraints constraint;
1121 double requested_ratio = 640.0/360;
1122 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1123 SetVideoConstraints(constraint, constraint);
1124 LocalP2PTest();
1125
1126 ASSERT_LE(0, initializing_client()->rendered_height());
1127 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001128 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129 initializing_client()->rendered_height();
1130 EXPECT_LE(requested_ratio, initiating_video_ratio);
1131
1132 ASSERT_LE(0, receiving_client()->rendered_height());
1133 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001134 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001135 receiving_client()->rendered_height();
1136 EXPECT_LE(requested_ratio, receiving_video_ratio);
1137}
1138
1139// This test sets up a Jsep call between two parties and test that the
1140// received video has a resolution of 1280*720.
1141// TODO(mallinath): Enable when
1142// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
1143TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTest1280By720) {
1144 ASSERT_TRUE(CreateTestClients());
1145 FakeConstraints constraint;
1146 constraint.SetMandatoryMinWidth(1280);
1147 constraint.SetMandatoryMinHeight(720);
1148 SetVideoConstraints(constraint, constraint);
1149 LocalP2PTest();
1150 VerifyRenderedSize(1280, 720);
1151}
1152
1153// This test sets up a call between two endpoints that are configured to use
1154// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
1155TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtls) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001156 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157 FakeConstraints setup_constraints;
1158 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1159 true);
1160 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1161 LocalP2PTest();
1162 VerifyRenderedSize(640, 480);
1163}
1164
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001165// This test sets up a audio call initially and then upgrades to audio/video,
1166// using DTLS.
mallinath@webrtc.org50bc5532013-10-21 17:58:35 +00001167TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001168 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001169 FakeConstraints setup_constraints;
1170 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1171 true);
1172 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1173 receiving_client()->SetReceiveAudioVideo(true, false);
1174 LocalP2PTest();
1175 receiving_client()->SetReceiveAudioVideo(true, true);
1176 receiving_client()->Negotiate();
1177}
1178
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001179// This test sets up a call between two endpoints that are configured to use
1180// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1181// negotiated and used for transport.
1182TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001183 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184 FakeConstraints setup_constraints;
1185 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1186 true);
1187 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1188 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1189 LocalP2PTest();
1190 VerifyRenderedSize(640, 480);
1191}
1192
1193// This test sets up a Jsep call between two parties, and the callee only
1194// accept to receive video.
solenberg@webrtc.org503c3362015-02-12 13:12:50 +00001195TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001196 ASSERT_TRUE(CreateTestClients());
1197 receiving_client()->SetReceiveAudioVideo(false, true);
1198 LocalP2PTest();
1199}
1200
1201// This test sets up a Jsep call between two parties, and the callee only
1202// accept to receive audio.
solenberg@webrtc.org503c3362015-02-12 13:12:50 +00001203TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204 ASSERT_TRUE(CreateTestClients());
1205 receiving_client()->SetReceiveAudioVideo(true, false);
1206 LocalP2PTest();
1207}
1208
1209// This test sets up a Jsep call between two parties, and the callee reject both
1210// audio and video.
1211TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerNone) {
1212 ASSERT_TRUE(CreateTestClients());
1213 receiving_client()->SetReceiveAudioVideo(false, false);
1214 LocalP2PTest();
1215}
1216
1217// This test sets up an audio and video call between two parties. After the call
1218// runs for a while (10 frames), the caller sends an update offer with video
1219// being rejected. Once the re-negotiation is done, the video flow should stop
1220// and the audio flow should continue.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001221// Disabled due to b/14955157.
1222TEST_F(JsepPeerConnectionP2PTestClient,
1223 DISABLED_UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001224 ASSERT_TRUE(CreateTestClients());
1225 LocalP2PTest();
1226 TestUpdateOfferWithRejectedContent();
1227}
1228
1229// This test sets up a Jsep call between two parties. The MSID is removed from
1230// the SDP strings from the caller.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001231// Disabled due to b/14955157.
1232TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001233 ASSERT_TRUE(CreateTestClients());
1234 receiving_client()->RemoveMsidFromReceivedSdp(true);
1235 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1236 // audio and video is muxed when MSID is disabled. Remove
1237 // SetRemoveBundleFromSdp once
1238 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1239 receiving_client()->RemoveBundleFromReceivedSdp(true);
1240 LocalP2PTest();
1241}
1242
1243// This test sets up a Jsep call between two parties and the initiating peer
1244// sends two steams.
1245// TODO(perkj): Disabled due to
1246// https://code.google.com/p/webrtc/issues/detail?id=1454
1247TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestTwoStreams) {
1248 ASSERT_TRUE(CreateTestClients());
1249 // Set optional video constraint to max 320pixels to decrease CPU usage.
1250 FakeConstraints constraint;
1251 constraint.SetOptionalMaxWidth(320);
1252 SetVideoConstraints(constraint, constraint);
1253 initializing_client()->AddMediaStream(true, true);
1254 initializing_client()->AddMediaStream(false, true);
1255 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1256 LocalP2PTest();
1257 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1258}
1259
1260// Test that we can receive the audio output level from a remote audio track.
1261TEST_F(JsepPeerConnectionP2PTestClient, GetAudioOutputLevelStats) {
1262 ASSERT_TRUE(CreateTestClients());
1263 LocalP2PTest();
1264
1265 StreamCollectionInterface* remote_streams =
1266 initializing_client()->remote_streams();
1267 ASSERT_GT(remote_streams->count(), 0u);
1268 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1269 MediaStreamTrackInterface* remote_audio_track =
1270 remote_streams->at(0)->GetAudioTracks()[0];
1271
1272 // Get the audio output level stats. Note that the level is not available
1273 // until a RTCP packet has been received.
1274 EXPECT_TRUE_WAIT(
1275 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1276 kMaxWaitForStatsMs);
1277}
1278
1279// Test that an audio input level is reported.
1280TEST_F(JsepPeerConnectionP2PTestClient, GetAudioInputLevelStats) {
1281 ASSERT_TRUE(CreateTestClients());
1282 LocalP2PTest();
1283
1284 // Get the audio input level stats. The level should be available very
1285 // soon after the test starts.
1286 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1287 kMaxWaitForStatsMs);
1288}
1289
1290// Test that we can get incoming byte counts from both audio and video tracks.
1291TEST_F(JsepPeerConnectionP2PTestClient, GetBytesReceivedStats) {
1292 ASSERT_TRUE(CreateTestClients());
1293 LocalP2PTest();
1294
1295 StreamCollectionInterface* remote_streams =
1296 initializing_client()->remote_streams();
1297 ASSERT_GT(remote_streams->count(), 0u);
1298 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1299 MediaStreamTrackInterface* remote_audio_track =
1300 remote_streams->at(0)->GetAudioTracks()[0];
1301 EXPECT_TRUE_WAIT(
1302 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1303 kMaxWaitForStatsMs);
1304
1305 MediaStreamTrackInterface* remote_video_track =
1306 remote_streams->at(0)->GetVideoTracks()[0];
1307 EXPECT_TRUE_WAIT(
1308 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1309 kMaxWaitForStatsMs);
1310}
1311
1312// Test that we can get outgoing byte counts from both audio and video tracks.
1313TEST_F(JsepPeerConnectionP2PTestClient, GetBytesSentStats) {
1314 ASSERT_TRUE(CreateTestClients());
1315 LocalP2PTest();
1316
1317 StreamCollectionInterface* local_streams =
1318 initializing_client()->local_streams();
1319 ASSERT_GT(local_streams->count(), 0u);
1320 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1321 MediaStreamTrackInterface* local_audio_track =
1322 local_streams->at(0)->GetAudioTracks()[0];
1323 EXPECT_TRUE_WAIT(
1324 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1325 kMaxWaitForStatsMs);
1326
1327 MediaStreamTrackInterface* local_video_track =
1328 local_streams->at(0)->GetVideoTracks()[0];
1329 EXPECT_TRUE_WAIT(
1330 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1331 kMaxWaitForStatsMs);
1332}
1333
Joachim Bauch04e5b492015-05-29 09:40:39 +02001334// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
1335TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12None) {
1336 PeerConnectionFactory::Options init_options;
1337 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1338 PeerConnectionFactory::Options recv_options;
1339 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1340 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001341 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1342 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1343 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001344 LocalP2PTest();
1345
Joachim Bauch04e5b492015-05-29 09:40:39 +02001346 EXPECT_EQ_WAIT(
1347 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_10),
1348 initializing_client()->GetDtlsCipherStats(),
1349 kMaxWaitForStatsMs);
jbauchac8869e2015-07-03 01:36:14 -07001350 EXPECT_EQ(
1351 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_10),
1352 init_observer->GetStringHistogramSample(webrtc::kAudioSslCipher));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001353
1354 EXPECT_EQ_WAIT(
1355 kDefaultSrtpCipher,
1356 initializing_client()->GetSrtpCipherStats(),
1357 kMaxWaitForStatsMs);
jbauchac8869e2015-07-03 01:36:14 -07001358 EXPECT_EQ(
1359 kDefaultSrtpCipher,
1360 init_observer->GetStringHistogramSample(webrtc::kAudioSrtpCipher));
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
1375 EXPECT_EQ_WAIT(
1376 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_12),
1377 initializing_client()->GetDtlsCipherStats(),
1378 kMaxWaitForStatsMs);
jbauchac8869e2015-07-03 01:36:14 -07001379 EXPECT_EQ(
1380 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_12),
1381 init_observer->GetStringHistogramSample(webrtc::kAudioSslCipher));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001382
1383 EXPECT_EQ_WAIT(
1384 kDefaultSrtpCipher,
1385 initializing_client()->GetSrtpCipherStats(),
1386 kMaxWaitForStatsMs);
jbauchac8869e2015-07-03 01:36:14 -07001387 EXPECT_EQ(
1388 kDefaultSrtpCipher,
1389 init_observer->GetStringHistogramSample(webrtc::kAudioSrtpCipher));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001390}
1391
1392// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1393// received supports 1.0.
1394TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Init) {
1395 PeerConnectionFactory::Options init_options;
1396 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1397 PeerConnectionFactory::Options recv_options;
1398 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1399 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001400 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1401 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1402 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001403 LocalP2PTest();
1404
1405 EXPECT_EQ_WAIT(
1406 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_10),
1407 initializing_client()->GetDtlsCipherStats(),
1408 kMaxWaitForStatsMs);
jbauchac8869e2015-07-03 01:36:14 -07001409 EXPECT_EQ(
1410 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_10),
1411 init_observer->GetStringHistogramSample(webrtc::kAudioSslCipher));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001412
1413 EXPECT_EQ_WAIT(
1414 kDefaultSrtpCipher,
1415 initializing_client()->GetSrtpCipherStats(),
1416 kMaxWaitForStatsMs);
jbauchac8869e2015-07-03 01:36:14 -07001417 EXPECT_EQ(
1418 kDefaultSrtpCipher,
1419 init_observer->GetStringHistogramSample(webrtc::kAudioSrtpCipher));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001420}
1421
1422// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1423// received supports 1.2.
1424TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Recv) {
1425 PeerConnectionFactory::Options init_options;
1426 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1427 PeerConnectionFactory::Options recv_options;
1428 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1429 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &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
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001435 EXPECT_EQ_WAIT(
Joachim Bauch831c5582015-05-20 12:48:41 +02001436 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_10),
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001437 initializing_client()->GetDtlsCipherStats(),
1438 kMaxWaitForStatsMs);
jbauchac8869e2015-07-03 01:36:14 -07001439 EXPECT_EQ(
1440 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_10),
1441 init_observer->GetStringHistogramSample(webrtc::kAudioSslCipher));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001442
1443 EXPECT_EQ_WAIT(
1444 kDefaultSrtpCipher,
1445 initializing_client()->GetSrtpCipherStats(),
1446 kMaxWaitForStatsMs);
jbauchac8869e2015-07-03 01:36:14 -07001447 EXPECT_EQ(
1448 kDefaultSrtpCipher,
1449 init_observer->GetStringHistogramSample(webrtc::kAudioSrtpCipher));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001450}
1451
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001452// This test sets up a call between two parties with audio, video and data.
1453TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDataChannel) {
1454 FakeConstraints setup_constraints;
1455 setup_constraints.SetAllowRtpDataChannels();
1456 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1457 initializing_client()->CreateDataChannel();
1458 LocalP2PTest();
1459 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1460 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1461 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1462 kMaxWaitMs);
1463 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1464 kMaxWaitMs);
1465
1466 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001467
1468 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001469 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1470 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001471
1472 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1474 kMaxWaitMs);
1475
1476 receiving_client()->data_channel()->Close();
1477 // Send new offer and answer.
1478 receiving_client()->Negotiate();
1479 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1480 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1481}
1482
1483// This test sets up a call between two parties and creates a data channel.
1484// The test tests that received data is buffered unless an observer has been
1485// registered.
1486// Rtp data channels can receive data before the underlying
1487// transport has detected that a channel is writable and thus data can be
1488// received before the data channel state changes to open. That is hard to test
1489// but the same buffering is used in that case.
1490TEST_F(JsepPeerConnectionP2PTestClient, RegisterDataChannelObserver) {
1491 FakeConstraints setup_constraints;
1492 setup_constraints.SetAllowRtpDataChannels();
1493 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1494 initializing_client()->CreateDataChannel();
1495 initializing_client()->Negotiate();
1496
1497 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1498 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1499 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1500 kMaxWaitMs);
1501 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1502 receiving_client()->data_channel()->state(), kMaxWaitMs);
1503
1504 // Unregister the existing observer.
1505 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001506
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001507 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001508 SendRtpData(initializing_client()->data_channel(), data);
1509
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001510 // Wait a while to allow the sent data to arrive before an observer is
1511 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001512 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513
1514 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1515 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1516}
1517
1518// This test sets up a call between two parties with audio, video and but only
1519// the initiating client support data.
1520TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001521 FakeConstraints setup_constraints_1;
1522 setup_constraints_1.SetAllowRtpDataChannels();
1523 // Must disable DTLS to make negotiation succeed.
1524 setup_constraints_1.SetMandatory(
1525 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1526 FakeConstraints setup_constraints_2;
1527 setup_constraints_2.SetMandatory(
1528 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1529 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530 initializing_client()->CreateDataChannel();
1531 LocalP2PTest();
1532 EXPECT_TRUE(initializing_client()->data_channel() != NULL);
1533 EXPECT_FALSE(receiving_client()->data_channel());
1534 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1535}
1536
1537// This test sets up a call between two parties with audio, video. When audio
1538// and video is setup and flowing and data channel is negotiated.
1539TEST_F(JsepPeerConnectionP2PTestClient, AddDataChannelAfterRenegotiation) {
1540 FakeConstraints setup_constraints;
1541 setup_constraints.SetAllowRtpDataChannels();
1542 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1543 LocalP2PTest();
1544 initializing_client()->CreateDataChannel();
1545 // Send new offer and answer.
1546 initializing_client()->Negotiate();
1547 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1548 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1549 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1550 kMaxWaitMs);
1551 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1552 kMaxWaitMs);
1553}
1554
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001555// This test sets up a Jsep call with SCTP DataChannel and verifies the
1556// negotiation is completed without error.
1557#ifdef HAVE_SCTP
1558TEST_F(JsepPeerConnectionP2PTestClient, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001559 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001560 FakeConstraints constraints;
1561 constraints.SetMandatory(
1562 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1563 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1564 initializing_client()->CreateDataChannel();
1565 initializing_client()->Negotiate(false, false);
1566}
1567#endif
1568
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001569// This test sets up a call between two parties with audio, and video.
1570// During the call, the initializing side restart ice and the test verifies that
1571// new ice candidates are generated and audio and video still can flow.
1572TEST_F(JsepPeerConnectionP2PTestClient, IceRestart) {
1573 ASSERT_TRUE(CreateTestClients());
1574
1575 // Negotiate and wait for ice completion and make sure audio and video plays.
1576 LocalP2PTest();
1577
1578 // Create a SDP string of the first audio candidate for both clients.
1579 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1580 initializing_client()->pc()->local_description()->candidates(0);
1581 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1582 receiving_client()->pc()->local_description()->candidates(0);
1583 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1584 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1585 std::string initiator_candidate;
1586 EXPECT_TRUE(
1587 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1588 std::string receiver_candidate;
1589 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1590
1591 // Restart ice on the initializing client.
1592 receiving_client()->SetExpectIceRestart(true);
1593 initializing_client()->IceRestart();
1594
1595 // Negotiate and wait for ice completion again and make sure audio and video
1596 // plays.
1597 LocalP2PTest();
1598
1599 // Create a SDP string of the first audio candidate for both clients again.
1600 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1601 initializing_client()->pc()->local_description()->candidates(0);
1602 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1603 receiving_client()->pc()->local_description()->candidates(0);
1604 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1605 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1606 std::string initiator_candidate_restart;
1607 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1608 &initiator_candidate_restart));
1609 std::string receiver_candidate_restart;
1610 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1611 &receiver_candidate_restart));
1612
1613 // Verify that the first candidates in the local session descriptions has
1614 // changed.
1615 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1616 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1617}
1618
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001619// This test sets up a Jsep call between two parties with external
1620// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001621// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1622// See issue webrtc/2378.
1623TEST_F(JsepPeerConnectionP2PTestClient,
1624 DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625 ASSERT_TRUE(CreateTestClients());
1626 EnableVideoDecoderFactory();
1627 LocalP2PTest();
1628}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001629
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001630#endif // if !defined(THREAD_SANITIZER)