blob: 99ee9be867168a99ca9f6fe898ce2517aede4a56 [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"
36#include "talk/app/webrtc/fakeportallocatorfactory.h"
37#include "talk/app/webrtc/localaudiosource.h"
38#include "talk/app/webrtc/mediastreaminterface.h"
39#include "talk/app/webrtc/peerconnectionfactory.h"
40#include "talk/app/webrtc/peerconnectioninterface.h"
41#include "talk/app/webrtc/test/fakeaudiocapturemodule.h"
42#include "talk/app/webrtc/test/fakeconstraints.h"
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +000043#include "talk/app/webrtc/test/fakedtlsidentityservice.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044#include "talk/app/webrtc/test/fakeperiodicvideocapturer.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000045#include "talk/app/webrtc/test/fakevideotrackrenderer.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046#include "talk/app/webrtc/test/mockpeerconnectionobservers.h"
47#include "talk/app/webrtc/videosourceinterface.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000048#include "talk/media/webrtc/fakewebrtcvideoengine.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000049#include "talk/session/media/mediasession.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000050#include "webrtc/base/gunit.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000051#include "webrtc/base/physicalsocketserver.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000052#include "webrtc/base/scoped_ptr.h"
53#include "webrtc/base/ssladapter.h"
54#include "webrtc/base/sslstreamadapter.h"
55#include "webrtc/base/thread.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000056#include "webrtc/base/virtualsocketserver.h"
57#include "webrtc/p2p/base/constants.h"
58#include "webrtc/p2p/base/sessiondescription.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059
60#define MAYBE_SKIP_TEST(feature) \
61 if (!(feature())) { \
62 LOG(LS_INFO) << "Feature disabled... skipping"; \
63 return; \
64 }
65
66using cricket::ContentInfo;
67using cricket::FakeWebRtcVideoDecoder;
68using cricket::FakeWebRtcVideoDecoderFactory;
69using cricket::FakeWebRtcVideoEncoder;
70using cricket::FakeWebRtcVideoEncoderFactory;
71using cricket::MediaContentDescription;
72using webrtc::DataBuffer;
73using webrtc::DataChannelInterface;
74using webrtc::DtmfSender;
75using webrtc::DtmfSenderInterface;
76using webrtc::DtmfSenderObserverInterface;
77using webrtc::FakeConstraints;
78using webrtc::MediaConstraintsInterface;
79using webrtc::MediaStreamTrackInterface;
80using webrtc::MockCreateSessionDescriptionObserver;
81using webrtc::MockDataChannelObserver;
82using webrtc::MockSetSessionDescriptionObserver;
83using webrtc::MockStatsObserver;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000084using webrtc::PeerConnectionInterface;
Joachim Bauch04e5b492015-05-29 09:40:39 +020085using webrtc::PeerConnectionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086using webrtc::SessionDescriptionInterface;
87using webrtc::StreamCollectionInterface;
88
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000089static const int kMaxWaitMs = 10000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000090// Disable for TSan v2, see
91// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
92// This declaration is also #ifdef'd as it causes uninitialized-variable
93// warnings.
94#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095static const int kMaxWaitForStatsMs = 3000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000096#endif
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000097static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098static const int kEndAudioFrameCount = 3;
99static const int kEndVideoFrameCount = 3;
100
101static const char kStreamLabelBase[] = "stream_label";
102static const char kVideoTrackLabelBase[] = "video_track";
103static const char kAudioTrackLabelBase[] = "audio_track";
104static const char kDataChannelLabel[] = "data_channel";
105
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000106// Disable for TSan v2, see
107// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
108// This declaration is also #ifdef'd as it causes unused-variable errors.
109#if !defined(THREAD_SANITIZER)
110// SRTP cipher name negotiated by the tests. This must be updated if the
111// default changes.
112static const char kDefaultSrtpCipher[] = "AES_CM_128_HMAC_SHA1_32";
113#endif
114
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115static void RemoveLinesFromSdp(const std::string& line_start,
116 std::string* sdp) {
117 const char kSdpLineEnd[] = "\r\n";
118 size_t ssrc_pos = 0;
119 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
120 std::string::npos) {
121 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
122 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
123 }
124}
125
126class SignalingMessageReceiver {
127 public:
128 protected:
129 SignalingMessageReceiver() {}
130 virtual ~SignalingMessageReceiver() {}
131};
132
133class JsepMessageReceiver : public SignalingMessageReceiver {
134 public:
135 virtual void ReceiveSdpMessage(const std::string& type,
136 std::string& msg) = 0;
137 virtual void ReceiveIceMessage(const std::string& sdp_mid,
138 int sdp_mline_index,
139 const std::string& msg) = 0;
140
141 protected:
142 JsepMessageReceiver() {}
143 virtual ~JsepMessageReceiver() {}
144};
145
146template <typename MessageReceiver>
147class PeerConnectionTestClientBase
148 : public webrtc::PeerConnectionObserver,
149 public MessageReceiver {
150 public:
151 ~PeerConnectionTestClientBase() {
152 while (!fake_video_renderers_.empty()) {
153 RenderMap::iterator it = fake_video_renderers_.begin();
154 delete it->second;
155 fake_video_renderers_.erase(it);
156 }
157 }
158
159 virtual void Negotiate() = 0;
160
161 virtual void Negotiate(bool audio, bool video) = 0;
162
163 virtual void SetVideoConstraints(
164 const webrtc::FakeConstraints& video_constraint) {
165 video_constraints_ = video_constraint;
166 }
167
168 void AddMediaStream(bool audio, bool video) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000169 std::string stream_label = kStreamLabelBase +
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000170 rtc::ToString<int>(
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000171 static_cast<int>(peer_connection_->local_streams()->count()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000172 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000173 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174
175 if (audio && can_receive_audio()) {
176 FakeConstraints constraints;
177 // Disable highpass filter so that we can get all the test audio frames.
178 constraints.AddMandatory(
179 MediaConstraintsInterface::kHighpassFilter, false);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000180 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
wu@webrtc.org97077a32013-10-25 21:18:33 +0000181 peer_connection_factory_->CreateAudioSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 // TODO(perkj): Test audio source when it is implemented. Currently audio
183 // always use the default input.
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000184 std::string label = stream_label + kAudioTrackLabelBase;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000185 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000186 peer_connection_factory_->CreateAudioTrack(label, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 stream->AddTrack(audio_track);
188 }
189 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000190 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191 }
192
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000193 EXPECT_TRUE(peer_connection_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194 }
195
196 size_t NumberOfLocalMediaStreams() {
197 return peer_connection_->local_streams()->count();
198 }
199
200 bool SessionActive() {
201 return peer_connection_->signaling_state() ==
202 webrtc::PeerConnectionInterface::kStable;
203 }
204
205 void set_signaling_message_receiver(
206 MessageReceiver* signaling_message_receiver) {
207 signaling_message_receiver_ = signaling_message_receiver;
208 }
209
210 void EnableVideoDecoderFactory() {
211 video_decoder_factory_enabled_ = true;
212 fake_video_decoder_factory_->AddSupportedVideoCodecType(
213 webrtc::kVideoCodecVP8);
214 }
215
216 bool AudioFramesReceivedCheck(int number_of_frames) const {
217 return number_of_frames <= fake_audio_capture_module_->frames_received();
218 }
219
220 bool VideoFramesReceivedCheck(int number_of_frames) {
221 if (video_decoder_factory_enabled_) {
222 const std::vector<FakeWebRtcVideoDecoder*>& decoders
223 = fake_video_decoder_factory_->decoders();
224 if (decoders.empty()) {
225 return number_of_frames <= 0;
226 }
227
228 for (std::vector<FakeWebRtcVideoDecoder*>::const_iterator
229 it = decoders.begin(); it != decoders.end(); ++it) {
230 if (number_of_frames > (*it)->GetNumFramesReceived()) {
231 return false;
232 }
233 }
234 return true;
235 } else {
236 if (fake_video_renderers_.empty()) {
237 return number_of_frames <= 0;
238 }
239
240 for (RenderMap::const_iterator it = fake_video_renderers_.begin();
241 it != fake_video_renderers_.end(); ++it) {
242 if (number_of_frames > it->second->num_rendered_frames()) {
243 return false;
244 }
245 }
246 return true;
247 }
248 }
249 // Verify the CreateDtmfSender interface
250 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000251 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
252 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253
254 // We can't create a DTMF sender with an invalid audio track or a non local
255 // track.
256 EXPECT_TRUE(peer_connection_->CreateDtmfSender(NULL) == NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000257 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258 peer_connection_factory_->CreateAudioTrack("dummy_track",
259 NULL));
260 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == NULL);
261
262 // We should be able to create a DTMF sender from a local track.
263 webrtc::AudioTrackInterface* localtrack =
264 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
265 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
266 EXPECT_TRUE(dtmf_sender.get() != NULL);
267 dtmf_sender->RegisterObserver(observer.get());
268
269 // Test the DtmfSender object just created.
270 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
271 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
272
273 // We don't need to verify that the DTMF tones are actually sent out because
274 // that is already covered by the tests of the lower level components.
275
276 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
277 std::vector<std::string> tones;
278 tones.push_back("1");
279 tones.push_back("a");
280 tones.push_back("");
281 observer->Verify(tones);
282
283 dtmf_sender->UnregisterObserver();
284 }
285
286 // Verifies that the SessionDescription have rejected the appropriate media
287 // content.
288 void VerifyRejectedMediaInSessionDescription() {
289 ASSERT_TRUE(peer_connection_->remote_description() != NULL);
290 ASSERT_TRUE(peer_connection_->local_description() != NULL);
291 const cricket::SessionDescription* remote_desc =
292 peer_connection_->remote_description()->description();
293 const cricket::SessionDescription* local_desc =
294 peer_connection_->local_description()->description();
295
296 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
297 if (remote_audio_content) {
298 const ContentInfo* audio_content =
299 GetFirstAudioContent(local_desc);
300 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
301 }
302
303 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
304 if (remote_video_content) {
305 const ContentInfo* video_content =
306 GetFirstVideoContent(local_desc);
307 EXPECT_EQ(can_receive_video(), !video_content->rejected);
308 }
309 }
310
311 void SetExpectIceRestart(bool expect_restart) {
312 expect_ice_restart_ = expect_restart;
313 }
314
315 bool ExpectIceRestart() const { return expect_ice_restart_; }
316
317 void VerifyLocalIceUfragAndPassword() {
318 ASSERT_TRUE(peer_connection_->local_description() != NULL);
319 const cricket::SessionDescription* desc =
320 peer_connection_->local_description()->description();
321 const cricket::ContentInfos& contents = desc->contents();
322
323 for (size_t index = 0; index < contents.size(); ++index) {
324 if (contents[index].rejected)
325 continue;
326 const cricket::TransportDescription* transport_desc =
327 desc->GetTransportDescriptionByName(contents[index].name);
328
329 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000330 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331 if (ufragpair_it == ice_ufrag_pwd_.end()) {
332 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000333 ice_ufrag_pwd_[static_cast<int>(index)] =
334 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335 } else if (ExpectIceRestart()) {
336 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
337 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
338 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
339 } else {
340 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
341 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
342 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
343 }
344 }
345 }
346
347 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000348 rtc::scoped_refptr<MockStatsObserver>
349 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000350 EXPECT_TRUE(peer_connection_->GetStats(
351 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
353 return observer->AudioOutputLevel();
354 }
355
356 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000357 rtc::scoped_refptr<MockStatsObserver>
358 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000359 EXPECT_TRUE(peer_connection_->GetStats(
360 observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
362 return observer->AudioInputLevel();
363 }
364
365 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000366 rtc::scoped_refptr<MockStatsObserver>
367 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000368 EXPECT_TRUE(peer_connection_->GetStats(
369 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
371 return observer->BytesReceived();
372 }
373
374 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000375 rtc::scoped_refptr<MockStatsObserver>
376 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000377 EXPECT_TRUE(peer_connection_->GetStats(
378 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
380 return observer->BytesSent();
381 }
382
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000383 int GetAvailableReceivedBandwidthStats() {
384 rtc::scoped_refptr<MockStatsObserver>
385 observer(new rtc::RefCountedObject<MockStatsObserver>());
386 EXPECT_TRUE(peer_connection_->GetStats(
387 observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
388 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
389 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000390 return bw;
391 }
392
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000393 std::string GetDtlsCipherStats() {
394 rtc::scoped_refptr<MockStatsObserver>
395 observer(new rtc::RefCountedObject<MockStatsObserver>());
396 EXPECT_TRUE(peer_connection_->GetStats(
397 observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
398 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
399 return observer->DtlsCipher();
400 }
401
402 std::string GetSrtpCipherStats() {
403 rtc::scoped_refptr<MockStatsObserver>
404 observer(new rtc::RefCountedObject<MockStatsObserver>());
405 EXPECT_TRUE(peer_connection_->GetStats(
406 observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
407 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
408 return observer->SrtpCipher();
409 }
410
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411 int rendered_width() {
412 EXPECT_FALSE(fake_video_renderers_.empty());
413 return fake_video_renderers_.empty() ? 1 :
414 fake_video_renderers_.begin()->second->width();
415 }
416
417 int rendered_height() {
418 EXPECT_FALSE(fake_video_renderers_.empty());
419 return fake_video_renderers_.empty() ? 1 :
420 fake_video_renderers_.begin()->second->height();
421 }
422
423 size_t number_of_remote_streams() {
424 if (!pc())
425 return 0;
426 return pc()->remote_streams()->count();
427 }
428
429 StreamCollectionInterface* remote_streams() {
430 if (!pc()) {
431 ADD_FAILURE();
432 return NULL;
433 }
434 return pc()->remote_streams();
435 }
436
437 StreamCollectionInterface* local_streams() {
438 if (!pc()) {
439 ADD_FAILURE();
440 return NULL;
441 }
442 return pc()->local_streams();
443 }
444
445 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
446 return pc()->signaling_state();
447 }
448
449 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
450 return pc()->ice_connection_state();
451 }
452
453 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
454 return pc()->ice_gathering_state();
455 }
456
457 // PeerConnectionObserver callbacks.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458 virtual void OnMessage(const std::string&) {}
459 virtual void OnSignalingMessage(const std::string& /*msg*/) {}
460 virtual void OnSignalingChange(
461 webrtc::PeerConnectionInterface::SignalingState new_state) {
462 EXPECT_EQ(peer_connection_->signaling_state(), new_state);
463 }
464 virtual void OnAddStream(webrtc::MediaStreamInterface* media_stream) {
465 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
466 const std::string id = media_stream->GetVideoTracks()[i]->id();
467 ASSERT_TRUE(fake_video_renderers_.find(id) ==
468 fake_video_renderers_.end());
469 fake_video_renderers_[id] = new webrtc::FakeVideoTrackRenderer(
470 media_stream->GetVideoTracks()[i]);
471 }
472 }
473 virtual void OnRemoveStream(webrtc::MediaStreamInterface* media_stream) {}
474 virtual void OnRenegotiationNeeded() {}
475 virtual void OnIceConnectionChange(
476 webrtc::PeerConnectionInterface::IceConnectionState new_state) {
477 EXPECT_EQ(peer_connection_->ice_connection_state(), new_state);
478 }
479 virtual void OnIceGatheringChange(
480 webrtc::PeerConnectionInterface::IceGatheringState new_state) {
481 EXPECT_EQ(peer_connection_->ice_gathering_state(), new_state);
482 }
483 virtual void OnIceCandidate(
484 const webrtc::IceCandidateInterface* /*candidate*/) {}
485
486 webrtc::PeerConnectionInterface* pc() {
487 return peer_connection_.get();
488 }
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000489 void StopVideoCapturers() {
490 for (std::vector<cricket::VideoCapturer*>::iterator it =
491 video_capturers_.begin(); it != video_capturers_.end(); ++it) {
492 (*it)->Stop();
493 }
494 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495
496 protected:
497 explicit PeerConnectionTestClientBase(const std::string& id)
498 : id_(id),
499 expect_ice_restart_(false),
500 fake_video_decoder_factory_(NULL),
501 fake_video_encoder_factory_(NULL),
502 video_decoder_factory_enabled_(false),
503 signaling_message_receiver_(NULL) {
504 }
Joachim Bauch04e5b492015-05-29 09:40:39 +0200505 bool Init(const MediaConstraintsInterface* constraints,
506 const PeerConnectionFactory::Options* options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507 EXPECT_TRUE(!peer_connection_);
508 EXPECT_TRUE(!peer_connection_factory_);
509 allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
510 if (!allocator_factory_) {
511 return false;
512 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 fake_audio_capture_module_ = FakeAudioCaptureModule::Create(
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000514 rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515
516 if (fake_audio_capture_module_ == NULL) {
517 return false;
518 }
519 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
520 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
521 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000522 rtc::Thread::Current(), rtc::Thread::Current(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 fake_audio_capture_module_, fake_video_encoder_factory_,
524 fake_video_decoder_factory_);
525 if (!peer_connection_factory_) {
526 return false;
527 }
Joachim Bauch04e5b492015-05-29 09:40:39 +0200528 if (options) {
529 peer_connection_factory_->SetOptions(*options);
530 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 peer_connection_ = CreatePeerConnection(allocator_factory_.get(),
532 constraints);
533 return peer_connection_.get() != NULL;
534 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000535 virtual rtc::scoped_refptr<webrtc::PeerConnectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536 CreatePeerConnection(webrtc::PortAllocatorFactoryInterface* factory,
537 const MediaConstraintsInterface* constraints) = 0;
538 MessageReceiver* signaling_message_receiver() {
539 return signaling_message_receiver_;
540 }
541 webrtc::PeerConnectionFactoryInterface* peer_connection_factory() {
542 return peer_connection_factory_.get();
543 }
544
545 virtual bool can_receive_audio() = 0;
546 virtual bool can_receive_video() = 0;
547 const std::string& id() const { return id_; }
548
549 private:
550 class DummyDtmfObserver : public DtmfSenderObserverInterface {
551 public:
552 DummyDtmfObserver() : completed_(false) {}
553
554 // Implements DtmfSenderObserverInterface.
555 void OnToneChange(const std::string& tone) {
556 tones_.push_back(tone);
557 if (tone.empty()) {
558 completed_ = true;
559 }
560 }
561
562 void Verify(const std::vector<std::string>& tones) const {
563 ASSERT_TRUE(tones_.size() == tones.size());
564 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
565 }
566
567 bool completed() const { return completed_; }
568
569 private:
570 bool completed_;
571 std::vector<std::string> tones_;
572 };
573
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000574 rtc::scoped_refptr<webrtc::VideoTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575 CreateLocalVideoTrack(const std::string stream_label) {
576 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
577 FakeConstraints source_constraints = video_constraints_;
578 source_constraints.SetMandatoryMaxFrameRate(10);
579
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000580 cricket::FakeVideoCapturer* fake_capturer =
581 new webrtc::FakePeriodicVideoCapturer();
582 video_capturers_.push_back(fake_capturer);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000583 rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000584 peer_connection_factory_->CreateVideoSource(
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000585 fake_capturer, &source_constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586 std::string label = stream_label + kVideoTrackLabelBase;
587 return peer_connection_factory_->CreateVideoTrack(label, source);
588 }
589
590 std::string id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000592 rtc::scoped_refptr<webrtc::PortAllocatorFactoryInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 allocator_factory_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000594 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
595 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596 peer_connection_factory_;
597
598 typedef std::pair<std::string, std::string> IceUfragPwdPair;
599 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
600 bool expect_ice_restart_;
601
602 // Needed to keep track of number of frames send.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000603 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 // Needed to keep track of number of frames received.
605 typedef std::map<std::string, webrtc::FakeVideoTrackRenderer*> RenderMap;
606 RenderMap fake_video_renderers_;
607 // Needed to keep track of number of frames received when external decoder
608 // used.
609 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_;
610 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_;
611 bool video_decoder_factory_enabled_;
612 webrtc::FakeConstraints video_constraints_;
613
614 // For remote peer communication.
615 MessageReceiver* signaling_message_receiver_;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000616
617 // Store references to the video capturers we've created, so that we can stop
618 // them, if required.
619 std::vector<cricket::VideoCapturer*> video_capturers_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620};
621
622class JsepTestClient
623 : public PeerConnectionTestClientBase<JsepMessageReceiver> {
624 public:
625 static JsepTestClient* CreateClient(
626 const std::string& id,
Joachim Bauch04e5b492015-05-29 09:40:39 +0200627 const MediaConstraintsInterface* constraints,
628 const PeerConnectionFactory::Options* options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 JsepTestClient* client(new JsepTestClient(id));
Joachim Bauch04e5b492015-05-29 09:40:39 +0200630 if (!client->Init(constraints, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 delete client;
632 return NULL;
633 }
634 return client;
635 }
636 ~JsepTestClient() {}
637
638 virtual void Negotiate() {
639 Negotiate(true, true);
640 }
641 virtual void Negotiate(bool audio, bool video) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000642 rtc::scoped_ptr<SessionDescriptionInterface> offer;
pbos@webrtc.orgceb956b2014-09-04 15:27:49 +0000643 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000644
645 if (offer->description()->GetContentByName("audio")) {
646 offer->description()->GetContentByName("audio")->rejected = !audio;
647 }
648 if (offer->description()->GetContentByName("video")) {
649 offer->description()->GetContentByName("video")->rejected = !video;
650 }
651
652 std::string sdp;
653 EXPECT_TRUE(offer->ToString(&sdp));
654 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
655 signaling_message_receiver()->ReceiveSdpMessage(
656 webrtc::SessionDescriptionInterface::kOffer, sdp);
657 }
658 // JsepMessageReceiver callback.
659 virtual void ReceiveSdpMessage(const std::string& type,
660 std::string& msg) {
661 FilterIncomingSdpMessage(&msg);
662 if (type == webrtc::SessionDescriptionInterface::kOffer) {
663 HandleIncomingOffer(msg);
664 } else {
665 HandleIncomingAnswer(msg);
666 }
667 }
668 // JsepMessageReceiver callback.
669 virtual void ReceiveIceMessage(const std::string& sdp_mid,
670 int sdp_mline_index,
671 const std::string& msg) {
672 LOG(INFO) << id() << "ReceiveIceMessage";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000673 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, NULL));
675 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
676 }
677 // Implements PeerConnectionObserver functions needed by Jsep.
678 virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
679 LOG(INFO) << id() << "OnIceCandidate";
680
681 std::string ice_sdp;
682 EXPECT_TRUE(candidate->ToString(&ice_sdp));
683 if (signaling_message_receiver() == NULL) {
684 // Remote party may be deleted.
685 return;
686 }
687 signaling_message_receiver()->ReceiveIceMessage(candidate->sdp_mid(),
688 candidate->sdp_mline_index(), ice_sdp);
689 }
690
691 void IceRestart() {
692 session_description_constraints_.SetMandatoryIceRestart(true);
693 SetExpectIceRestart(true);
694 }
695
696 void SetReceiveAudioVideo(bool audio, bool video) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000697 SetReceiveAudio(audio);
698 SetReceiveVideo(video);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699 ASSERT_EQ(audio, can_receive_audio());
700 ASSERT_EQ(video, can_receive_video());
701 }
702
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000703 void SetReceiveAudio(bool audio) {
704 if (audio && can_receive_audio())
705 return;
706 session_description_constraints_.SetMandatoryReceiveAudio(audio);
707 }
708
709 void SetReceiveVideo(bool video) {
710 if (video && can_receive_video())
711 return;
712 session_description_constraints_.SetMandatoryReceiveVideo(video);
713 }
714
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 void RemoveMsidFromReceivedSdp(bool remove) {
716 remove_msid_ = remove;
717 }
718
719 void RemoveSdesCryptoFromReceivedSdp(bool remove) {
720 remove_sdes_ = remove;
721 }
722
723 void RemoveBundleFromReceivedSdp(bool remove) {
724 remove_bundle_ = remove;
725 }
726
727 virtual bool can_receive_audio() {
728 bool value;
729 if (webrtc::FindConstraint(&session_description_constraints_,
730 MediaConstraintsInterface::kOfferToReceiveAudio, &value, NULL)) {
731 return value;
732 }
733 return true;
734 }
735
736 virtual bool can_receive_video() {
737 bool value;
738 if (webrtc::FindConstraint(&session_description_constraints_,
739 MediaConstraintsInterface::kOfferToReceiveVideo, &value, NULL)) {
740 return value;
741 }
742 return true;
743 }
744
745 virtual void OnIceComplete() {
746 LOG(INFO) << id() << "OnIceComplete";
747 }
748
749 virtual void OnDataChannel(DataChannelInterface* data_channel) {
750 LOG(INFO) << id() << "OnDataChannel";
751 data_channel_ = data_channel;
752 data_observer_.reset(new MockDataChannelObserver(data_channel));
753 }
754
755 void CreateDataChannel() {
756 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel,
757 NULL);
758 ASSERT_TRUE(data_channel_.get() != NULL);
759 data_observer_.reset(new MockDataChannelObserver(data_channel_));
760 }
761
762 DataChannelInterface* data_channel() { return data_channel_; }
763 const MockDataChannelObserver* data_observer() const {
764 return data_observer_.get();
765 }
766
767 protected:
768 explicit JsepTestClient(const std::string& id)
769 : PeerConnectionTestClientBase<JsepMessageReceiver>(id),
770 remove_msid_(false),
771 remove_bundle_(false),
772 remove_sdes_(false) {
773 }
774
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000775 virtual rtc::scoped_refptr<webrtc::PeerConnectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 CreatePeerConnection(webrtc::PortAllocatorFactoryInterface* factory,
777 const MediaConstraintsInterface* constraints) {
778 // CreatePeerConnection with IceServers.
779 webrtc::PeerConnectionInterface::IceServers ice_servers;
780 webrtc::PeerConnectionInterface::IceServer ice_server;
781 ice_server.uri = "stun:stun.l.google.com:19302";
782 ice_servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000783
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000784 FakeIdentityService* dtls_service =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000785 rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000786 new FakeIdentityService() : NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 return peer_connection_factory()->CreatePeerConnection(
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000788 ice_servers, constraints, factory, dtls_service, this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 }
790
791 void HandleIncomingOffer(const std::string& msg) {
792 LOG(INFO) << id() << "HandleIncomingOffer ";
793 if (NumberOfLocalMediaStreams() == 0) {
794 // If we are not sending any streams ourselves it is time to add some.
795 AddMediaStream(true, true);
796 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000797 rtc::scoped_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 webrtc::CreateSessionDescription("offer", msg, NULL));
799 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000800 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801 EXPECT_TRUE(DoCreateAnswer(answer.use()));
802 std::string sdp;
803 EXPECT_TRUE(answer->ToString(&sdp));
804 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
805 if (signaling_message_receiver()) {
806 signaling_message_receiver()->ReceiveSdpMessage(
807 webrtc::SessionDescriptionInterface::kAnswer, sdp);
808 }
809 }
810
811 void HandleIncomingAnswer(const std::string& msg) {
812 LOG(INFO) << id() << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000813 rtc::scoped_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 webrtc::CreateSessionDescription("answer", msg, NULL));
815 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
816 }
817
818 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
819 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000820 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
821 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 MockCreateSessionDescriptionObserver>());
823 if (offer) {
824 pc()->CreateOffer(observer, &session_description_constraints_);
825 } else {
826 pc()->CreateAnswer(observer, &session_description_constraints_);
827 }
828 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
829 *desc = observer->release_desc();
830 if (observer->result() && ExpectIceRestart()) {
831 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
832 }
833 return observer->result();
834 }
835
836 bool DoCreateOffer(SessionDescriptionInterface** desc) {
837 return DoCreateOfferAnswer(desc, true);
838 }
839
840 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
841 return DoCreateOfferAnswer(desc, false);
842 }
843
844 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000845 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
846 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 MockSetSessionDescriptionObserver>());
848 LOG(INFO) << id() << "SetLocalDescription ";
849 pc()->SetLocalDescription(observer, desc);
850 // Ignore the observer result. If we wait for the result with
851 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
852 // before the offer which is an error.
853 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000854 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 // ProcessMessages waits at least 1ms but processes all messages before
856 // returning. Since this test is synchronous and send messages to the remote
857 // peer whenever a callback is invoked, this can lead to messages being
858 // sent to the remote peer in the wrong order.
859 // TODO(perkj): Find a way to check the result without risking that the
860 // order of sent messages are changed. Ex- by posting all messages that are
861 // sent to the remote peer.
862 return true;
863 }
864
865 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000866 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
867 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 MockSetSessionDescriptionObserver>());
869 LOG(INFO) << id() << "SetRemoteDescription ";
870 pc()->SetRemoteDescription(observer, desc);
871 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
872 return observer->result();
873 }
874
875 // This modifies all received SDP messages before they are processed.
876 void FilterIncomingSdpMessage(std::string* sdp) {
877 if (remove_msid_) {
878 const char kSdpSsrcAttribute[] = "a=ssrc:";
879 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
880 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
881 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
882 }
883 if (remove_bundle_) {
884 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
885 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
886 }
887 if (remove_sdes_) {
888 const char kSdpSdesCryptoAttribute[] = "a=crypto";
889 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
890 }
891 }
892
893 private:
894 webrtc::FakeConstraints session_description_constraints_;
895 bool remove_msid_; // True if MSID should be removed in received SDP.
896 bool remove_bundle_; // True if bundle should be removed in received SDP.
897 bool remove_sdes_; // True if a=crypto should be removed in received SDP.
898
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000899 rtc::scoped_refptr<DataChannelInterface> data_channel_;
900 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901};
902
903template <typename SignalingClass>
904class P2PTestConductor : public testing::Test {
905 public:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000906 P2PTestConductor()
907 : pss_(new rtc::PhysicalSocketServer),
908 ss_(new rtc::VirtualSocketServer(pss_.get())),
909 ss_scope_(ss_.get()) {}
910
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 bool SessionActive() {
912 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000913 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000915
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916 // Return true if the number of frames provided have been received or it is
917 // known that that will never occur (e.g. no frames will be sent or
918 // captured).
919 bool FramesNotPending(int audio_frames_to_receive,
920 int video_frames_to_receive) {
921 return VideoFramesReceivedCheck(video_frames_to_receive) &&
922 AudioFramesReceivedCheck(audio_frames_to_receive);
923 }
924 bool AudioFramesReceivedCheck(int frames_received) {
925 return initiating_client_->AudioFramesReceivedCheck(frames_received) &&
926 receiving_client_->AudioFramesReceivedCheck(frames_received);
927 }
928 bool VideoFramesReceivedCheck(int frames_received) {
929 return initiating_client_->VideoFramesReceivedCheck(frames_received) &&
930 receiving_client_->VideoFramesReceivedCheck(frames_received);
931 }
932 void VerifyDtmf() {
933 initiating_client_->VerifyDtmf();
934 receiving_client_->VerifyDtmf();
935 }
936
937 void TestUpdateOfferWithRejectedContent() {
938 initiating_client_->Negotiate(true, false);
939 EXPECT_TRUE_WAIT(
940 FramesNotPending(kEndAudioFrameCount * 2, kEndVideoFrameCount),
941 kMaxWaitForFramesMs);
942 // There shouldn't be any more video frame after the new offer is
943 // negotiated.
944 EXPECT_FALSE(VideoFramesReceivedCheck(kEndVideoFrameCount + 1));
945 }
946
947 void VerifyRenderedSize(int width, int height) {
948 EXPECT_EQ(width, receiving_client()->rendered_width());
949 EXPECT_EQ(height, receiving_client()->rendered_height());
950 EXPECT_EQ(width, initializing_client()->rendered_width());
951 EXPECT_EQ(height, initializing_client()->rendered_height());
952 }
953
954 void VerifySessionDescriptions() {
955 initiating_client_->VerifyRejectedMediaInSessionDescription();
956 receiving_client_->VerifyRejectedMediaInSessionDescription();
957 initiating_client_->VerifyLocalIceUfragAndPassword();
958 receiving_client_->VerifyLocalIceUfragAndPassword();
959 }
960
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961 ~P2PTestConductor() {
962 if (initiating_client_) {
963 initiating_client_->set_signaling_message_receiver(NULL);
964 }
965 if (receiving_client_) {
966 receiving_client_->set_signaling_message_receiver(NULL);
967 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000968 }
969
970 bool CreateTestClients() {
971 return CreateTestClients(NULL, NULL);
972 }
973
974 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
975 MediaConstraintsInterface* recv_constraints) {
Joachim Bauch04e5b492015-05-29 09:40:39 +0200976 return CreateTestClients(init_constraints, NULL, recv_constraints, NULL);
977 }
978
979 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
980 PeerConnectionFactory::Options* init_options,
981 MediaConstraintsInterface* recv_constraints,
982 PeerConnectionFactory::Options* recv_options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983 initiating_client_.reset(SignalingClass::CreateClient("Caller: ",
Joachim Bauch04e5b492015-05-29 09:40:39 +0200984 init_constraints,
985 init_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000986 receiving_client_.reset(SignalingClass::CreateClient("Callee: ",
Joachim Bauch04e5b492015-05-29 09:40:39 +0200987 recv_constraints,
988 recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989 if (!initiating_client_ || !receiving_client_) {
990 return false;
991 }
992 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
993 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
994 return true;
995 }
996
997 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
998 const webrtc::FakeConstraints& recv_constraints) {
999 initiating_client_->SetVideoConstraints(init_constraints);
1000 receiving_client_->SetVideoConstraints(recv_constraints);
1001 }
1002
1003 void EnableVideoDecoderFactory() {
1004 initiating_client_->EnableVideoDecoderFactory();
1005 receiving_client_->EnableVideoDecoderFactory();
1006 }
1007
1008 // This test sets up a call between two parties. Both parties send static
1009 // frames to each other. Once the test is finished the number of sent frames
1010 // is compared to the number of received frames.
1011 void LocalP2PTest() {
1012 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1013 initiating_client_->AddMediaStream(true, true);
1014 }
1015 initiating_client_->Negotiate();
1016 const int kMaxWaitForActivationMs = 5000;
1017 // Assert true is used here since next tests are guaranteed to fail and
1018 // would eat up 5 seconds.
1019 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1020 VerifySessionDescriptions();
1021
1022
1023 int audio_frame_count = kEndAudioFrameCount;
1024 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1025 if (!initiating_client_->can_receive_audio() ||
1026 !receiving_client_->can_receive_audio()) {
1027 audio_frame_count = -1;
1028 }
1029 int video_frame_count = kEndVideoFrameCount;
1030 if (!initiating_client_->can_receive_video() ||
1031 !receiving_client_->can_receive_video()) {
1032 video_frame_count = -1;
1033 }
1034
1035 if (audio_frame_count != -1 || video_frame_count != -1) {
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001036 // Audio or video is expected to flow, so both clients should reach the
1037 // Connected state, and the offerer (ICE controller) should proceed to
1038 // Completed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001039 // Note: These tests have been observed to fail under heavy load at
1040 // shorter timeouts, so they may be flaky.
1041 EXPECT_EQ_WAIT(
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001042 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001043 initiating_client_->ice_connection_state(),
1044 kMaxWaitForFramesMs);
1045 EXPECT_EQ_WAIT(
1046 webrtc::PeerConnectionInterface::kIceConnectionConnected,
1047 receiving_client_->ice_connection_state(),
1048 kMaxWaitForFramesMs);
1049 }
1050
1051 if (initiating_client_->can_receive_audio() ||
1052 initiating_client_->can_receive_video()) {
1053 // The initiating client can receive media, so it must produce candidates
1054 // that will serve as destinations for that media.
1055 // TODO(bemasc): Understand why the state is not already Complete here, as
1056 // seems to be the case for the receiving client. This may indicate a bug
1057 // in the ICE gathering system.
1058 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1059 initiating_client_->ice_gathering_state());
1060 }
1061 if (receiving_client_->can_receive_audio() ||
1062 receiving_client_->can_receive_video()) {
1063 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1064 receiving_client_->ice_gathering_state(),
1065 kMaxWaitForFramesMs);
1066 }
1067
1068 EXPECT_TRUE_WAIT(FramesNotPending(audio_frame_count, video_frame_count),
1069 kMaxWaitForFramesMs);
1070 }
1071
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001072 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1073 // Messages may get lost on the unreliable DataChannel, so we send multiple
1074 // times to avoid test flakiness.
1075 static const size_t kSendAttempts = 5;
1076
1077 for (size_t i = 0; i < kSendAttempts; ++i) {
1078 dc->Send(DataBuffer(data));
1079 }
1080 }
1081
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001082 SignalingClass* initializing_client() { return initiating_client_.get(); }
1083 SignalingClass* receiving_client() { return receiving_client_.get(); }
1084
1085 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001086 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1087 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1088 rtc::SocketServerScope ss_scope_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001089 rtc::scoped_ptr<SignalingClass> initiating_client_;
1090 rtc::scoped_ptr<SignalingClass> receiving_client_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001091};
1092typedef P2PTestConductor<JsepTestClient> JsepPeerConnectionP2PTestClient;
1093
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001094// Disable for TSan v2, see
1095// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1096#if !defined(THREAD_SANITIZER)
1097
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001099// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1100// See issue webrtc/2378.
1101TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001102 ASSERT_TRUE(CreateTestClients());
1103 LocalP2PTest();
1104 VerifyDtmf();
1105}
1106
1107// This test sets up a Jsep call between two parties and test that we can get a
1108// video aspect ratio of 16:9.
1109TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTest16To9) {
1110 ASSERT_TRUE(CreateTestClients());
1111 FakeConstraints constraint;
1112 double requested_ratio = 640.0/360;
1113 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1114 SetVideoConstraints(constraint, constraint);
1115 LocalP2PTest();
1116
1117 ASSERT_LE(0, initializing_client()->rendered_height());
1118 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001119 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001120 initializing_client()->rendered_height();
1121 EXPECT_LE(requested_ratio, initiating_video_ratio);
1122
1123 ASSERT_LE(0, receiving_client()->rendered_height());
1124 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001125 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001126 receiving_client()->rendered_height();
1127 EXPECT_LE(requested_ratio, receiving_video_ratio);
1128}
1129
1130// This test sets up a Jsep call between two parties and test that the
1131// received video has a resolution of 1280*720.
1132// TODO(mallinath): Enable when
1133// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
1134TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTest1280By720) {
1135 ASSERT_TRUE(CreateTestClients());
1136 FakeConstraints constraint;
1137 constraint.SetMandatoryMinWidth(1280);
1138 constraint.SetMandatoryMinHeight(720);
1139 SetVideoConstraints(constraint, constraint);
1140 LocalP2PTest();
1141 VerifyRenderedSize(1280, 720);
1142}
1143
1144// This test sets up a call between two endpoints that are configured to use
1145// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
1146TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtls) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001147 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001148 FakeConstraints setup_constraints;
1149 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1150 true);
1151 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1152 LocalP2PTest();
1153 VerifyRenderedSize(640, 480);
1154}
1155
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001156// This test sets up a audio call initially and then upgrades to audio/video,
1157// using DTLS.
mallinath@webrtc.org50bc5532013-10-21 17:58:35 +00001158TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001159 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001160 FakeConstraints setup_constraints;
1161 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1162 true);
1163 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1164 receiving_client()->SetReceiveAudioVideo(true, false);
1165 LocalP2PTest();
1166 receiving_client()->SetReceiveAudioVideo(true, true);
1167 receiving_client()->Negotiate();
1168}
1169
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001170// This test sets up a call between two endpoints that are configured to use
1171// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1172// negotiated and used for transport.
1173TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001174 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001175 FakeConstraints setup_constraints;
1176 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1177 true);
1178 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1179 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1180 LocalP2PTest();
1181 VerifyRenderedSize(640, 480);
1182}
1183
1184// This test sets up a Jsep call between two parties, and the callee only
1185// accept to receive video.
solenberg@webrtc.org503c3362015-02-12 13:12:50 +00001186TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187 ASSERT_TRUE(CreateTestClients());
1188 receiving_client()->SetReceiveAudioVideo(false, true);
1189 LocalP2PTest();
1190}
1191
1192// This test sets up a Jsep call between two parties, and the callee only
1193// accept to receive audio.
solenberg@webrtc.org503c3362015-02-12 13:12:50 +00001194TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195 ASSERT_TRUE(CreateTestClients());
1196 receiving_client()->SetReceiveAudioVideo(true, false);
1197 LocalP2PTest();
1198}
1199
1200// This test sets up a Jsep call between two parties, and the callee reject both
1201// audio and video.
1202TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerNone) {
1203 ASSERT_TRUE(CreateTestClients());
1204 receiving_client()->SetReceiveAudioVideo(false, false);
1205 LocalP2PTest();
1206}
1207
1208// This test sets up an audio and video call between two parties. After the call
1209// runs for a while (10 frames), the caller sends an update offer with video
1210// being rejected. Once the re-negotiation is done, the video flow should stop
1211// and the audio flow should continue.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001212// Disabled due to b/14955157.
1213TEST_F(JsepPeerConnectionP2PTestClient,
1214 DISABLED_UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215 ASSERT_TRUE(CreateTestClients());
1216 LocalP2PTest();
1217 TestUpdateOfferWithRejectedContent();
1218}
1219
1220// This test sets up a Jsep call between two parties. The MSID is removed from
1221// the SDP strings from the caller.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001222// Disabled due to b/14955157.
1223TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001224 ASSERT_TRUE(CreateTestClients());
1225 receiving_client()->RemoveMsidFromReceivedSdp(true);
1226 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1227 // audio and video is muxed when MSID is disabled. Remove
1228 // SetRemoveBundleFromSdp once
1229 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1230 receiving_client()->RemoveBundleFromReceivedSdp(true);
1231 LocalP2PTest();
1232}
1233
1234// This test sets up a Jsep call between two parties and the initiating peer
1235// sends two steams.
1236// TODO(perkj): Disabled due to
1237// https://code.google.com/p/webrtc/issues/detail?id=1454
1238TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestTwoStreams) {
1239 ASSERT_TRUE(CreateTestClients());
1240 // Set optional video constraint to max 320pixels to decrease CPU usage.
1241 FakeConstraints constraint;
1242 constraint.SetOptionalMaxWidth(320);
1243 SetVideoConstraints(constraint, constraint);
1244 initializing_client()->AddMediaStream(true, true);
1245 initializing_client()->AddMediaStream(false, true);
1246 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1247 LocalP2PTest();
1248 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1249}
1250
1251// Test that we can receive the audio output level from a remote audio track.
1252TEST_F(JsepPeerConnectionP2PTestClient, GetAudioOutputLevelStats) {
1253 ASSERT_TRUE(CreateTestClients());
1254 LocalP2PTest();
1255
1256 StreamCollectionInterface* remote_streams =
1257 initializing_client()->remote_streams();
1258 ASSERT_GT(remote_streams->count(), 0u);
1259 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1260 MediaStreamTrackInterface* remote_audio_track =
1261 remote_streams->at(0)->GetAudioTracks()[0];
1262
1263 // Get the audio output level stats. Note that the level is not available
1264 // until a RTCP packet has been received.
1265 EXPECT_TRUE_WAIT(
1266 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1267 kMaxWaitForStatsMs);
1268}
1269
1270// Test that an audio input level is reported.
1271TEST_F(JsepPeerConnectionP2PTestClient, GetAudioInputLevelStats) {
1272 ASSERT_TRUE(CreateTestClients());
1273 LocalP2PTest();
1274
1275 // Get the audio input level stats. The level should be available very
1276 // soon after the test starts.
1277 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1278 kMaxWaitForStatsMs);
1279}
1280
1281// Test that we can get incoming byte counts from both audio and video tracks.
1282TEST_F(JsepPeerConnectionP2PTestClient, GetBytesReceivedStats) {
1283 ASSERT_TRUE(CreateTestClients());
1284 LocalP2PTest();
1285
1286 StreamCollectionInterface* remote_streams =
1287 initializing_client()->remote_streams();
1288 ASSERT_GT(remote_streams->count(), 0u);
1289 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1290 MediaStreamTrackInterface* remote_audio_track =
1291 remote_streams->at(0)->GetAudioTracks()[0];
1292 EXPECT_TRUE_WAIT(
1293 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1294 kMaxWaitForStatsMs);
1295
1296 MediaStreamTrackInterface* remote_video_track =
1297 remote_streams->at(0)->GetVideoTracks()[0];
1298 EXPECT_TRUE_WAIT(
1299 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1300 kMaxWaitForStatsMs);
1301}
1302
1303// Test that we can get outgoing byte counts from both audio and video tracks.
1304TEST_F(JsepPeerConnectionP2PTestClient, GetBytesSentStats) {
1305 ASSERT_TRUE(CreateTestClients());
1306 LocalP2PTest();
1307
1308 StreamCollectionInterface* local_streams =
1309 initializing_client()->local_streams();
1310 ASSERT_GT(local_streams->count(), 0u);
1311 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1312 MediaStreamTrackInterface* local_audio_track =
1313 local_streams->at(0)->GetAudioTracks()[0];
1314 EXPECT_TRUE_WAIT(
1315 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1316 kMaxWaitForStatsMs);
1317
1318 MediaStreamTrackInterface* local_video_track =
1319 local_streams->at(0)->GetVideoTracks()[0];
1320 EXPECT_TRUE_WAIT(
1321 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1322 kMaxWaitForStatsMs);
1323}
1324
Joachim Bauch04e5b492015-05-29 09:40:39 +02001325// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
1326TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12None) {
1327 PeerConnectionFactory::Options init_options;
1328 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1329 PeerConnectionFactory::Options recv_options;
1330 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1331 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001332 LocalP2PTest();
1333
Joachim Bauch04e5b492015-05-29 09:40:39 +02001334 EXPECT_EQ_WAIT(
1335 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_10),
1336 initializing_client()->GetDtlsCipherStats(),
1337 kMaxWaitForStatsMs);
1338
1339 EXPECT_EQ_WAIT(
1340 kDefaultSrtpCipher,
1341 initializing_client()->GetSrtpCipherStats(),
1342 kMaxWaitForStatsMs);
1343}
1344
1345// Test that DTLS 1.2 is used if both ends support it.
1346TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Both) {
1347 PeerConnectionFactory::Options init_options;
1348 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1349 PeerConnectionFactory::Options recv_options;
1350 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1351 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
1352 LocalP2PTest();
1353
1354 EXPECT_EQ_WAIT(
1355 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_12),
1356 initializing_client()->GetDtlsCipherStats(),
1357 kMaxWaitForStatsMs);
1358
1359 EXPECT_EQ_WAIT(
1360 kDefaultSrtpCipher,
1361 initializing_client()->GetSrtpCipherStats(),
1362 kMaxWaitForStatsMs);
1363}
1364
1365// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1366// received supports 1.0.
1367TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Init) {
1368 PeerConnectionFactory::Options init_options;
1369 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1370 PeerConnectionFactory::Options recv_options;
1371 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1372 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
1373 LocalP2PTest();
1374
1375 EXPECT_EQ_WAIT(
1376 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_10),
1377 initializing_client()->GetDtlsCipherStats(),
1378 kMaxWaitForStatsMs);
1379
1380 EXPECT_EQ_WAIT(
1381 kDefaultSrtpCipher,
1382 initializing_client()->GetSrtpCipherStats(),
1383 kMaxWaitForStatsMs);
1384}
1385
1386// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1387// received supports 1.2.
1388TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Recv) {
1389 PeerConnectionFactory::Options init_options;
1390 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1391 PeerConnectionFactory::Options recv_options;
1392 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1393 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
1394 LocalP2PTest();
1395
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001396 EXPECT_EQ_WAIT(
Joachim Bauch831c5582015-05-20 12:48:41 +02001397 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_10),
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001398 initializing_client()->GetDtlsCipherStats(),
1399 kMaxWaitForStatsMs);
1400
1401 EXPECT_EQ_WAIT(
1402 kDefaultSrtpCipher,
1403 initializing_client()->GetSrtpCipherStats(),
1404 kMaxWaitForStatsMs);
1405}
1406
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001407// This test sets up a call between two parties with audio, video and data.
1408TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDataChannel) {
1409 FakeConstraints setup_constraints;
1410 setup_constraints.SetAllowRtpDataChannels();
1411 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1412 initializing_client()->CreateDataChannel();
1413 LocalP2PTest();
1414 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1415 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1416 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1417 kMaxWaitMs);
1418 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1419 kMaxWaitMs);
1420
1421 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001422
1423 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001424 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1425 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001426
1427 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001428 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1429 kMaxWaitMs);
1430
1431 receiving_client()->data_channel()->Close();
1432 // Send new offer and answer.
1433 receiving_client()->Negotiate();
1434 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1435 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1436}
1437
1438// This test sets up a call between two parties and creates a data channel.
1439// The test tests that received data is buffered unless an observer has been
1440// registered.
1441// Rtp data channels can receive data before the underlying
1442// transport has detected that a channel is writable and thus data can be
1443// received before the data channel state changes to open. That is hard to test
1444// but the same buffering is used in that case.
1445TEST_F(JsepPeerConnectionP2PTestClient, RegisterDataChannelObserver) {
1446 FakeConstraints setup_constraints;
1447 setup_constraints.SetAllowRtpDataChannels();
1448 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1449 initializing_client()->CreateDataChannel();
1450 initializing_client()->Negotiate();
1451
1452 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1453 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1454 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1455 kMaxWaitMs);
1456 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1457 receiving_client()->data_channel()->state(), kMaxWaitMs);
1458
1459 // Unregister the existing observer.
1460 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001461
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001462 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001463 SendRtpData(initializing_client()->data_channel(), data);
1464
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001465 // Wait a while to allow the sent data to arrive before an observer is
1466 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001467 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001468
1469 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1470 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1471}
1472
1473// This test sets up a call between two parties with audio, video and but only
1474// the initiating client support data.
1475TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001476 FakeConstraints setup_constraints_1;
1477 setup_constraints_1.SetAllowRtpDataChannels();
1478 // Must disable DTLS to make negotiation succeed.
1479 setup_constraints_1.SetMandatory(
1480 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1481 FakeConstraints setup_constraints_2;
1482 setup_constraints_2.SetMandatory(
1483 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1484 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001485 initializing_client()->CreateDataChannel();
1486 LocalP2PTest();
1487 EXPECT_TRUE(initializing_client()->data_channel() != NULL);
1488 EXPECT_FALSE(receiving_client()->data_channel());
1489 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1490}
1491
1492// This test sets up a call between two parties with audio, video. When audio
1493// and video is setup and flowing and data channel is negotiated.
1494TEST_F(JsepPeerConnectionP2PTestClient, AddDataChannelAfterRenegotiation) {
1495 FakeConstraints setup_constraints;
1496 setup_constraints.SetAllowRtpDataChannels();
1497 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1498 LocalP2PTest();
1499 initializing_client()->CreateDataChannel();
1500 // Send new offer and answer.
1501 initializing_client()->Negotiate();
1502 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1503 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1504 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1505 kMaxWaitMs);
1506 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1507 kMaxWaitMs);
1508}
1509
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001510// This test sets up a Jsep call with SCTP DataChannel and verifies the
1511// negotiation is completed without error.
1512#ifdef HAVE_SCTP
1513TEST_F(JsepPeerConnectionP2PTestClient, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001514 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001515 FakeConstraints constraints;
1516 constraints.SetMandatory(
1517 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1518 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1519 initializing_client()->CreateDataChannel();
1520 initializing_client()->Negotiate(false, false);
1521}
1522#endif
1523
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001524// This test sets up a call between two parties with audio, and video.
1525// During the call, the initializing side restart ice and the test verifies that
1526// new ice candidates are generated and audio and video still can flow.
1527TEST_F(JsepPeerConnectionP2PTestClient, IceRestart) {
1528 ASSERT_TRUE(CreateTestClients());
1529
1530 // Negotiate and wait for ice completion and make sure audio and video plays.
1531 LocalP2PTest();
1532
1533 // Create a SDP string of the first audio candidate for both clients.
1534 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1535 initializing_client()->pc()->local_description()->candidates(0);
1536 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1537 receiving_client()->pc()->local_description()->candidates(0);
1538 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1539 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1540 std::string initiator_candidate;
1541 EXPECT_TRUE(
1542 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1543 std::string receiver_candidate;
1544 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1545
1546 // Restart ice on the initializing client.
1547 receiving_client()->SetExpectIceRestart(true);
1548 initializing_client()->IceRestart();
1549
1550 // Negotiate and wait for ice completion again and make sure audio and video
1551 // plays.
1552 LocalP2PTest();
1553
1554 // Create a SDP string of the first audio candidate for both clients again.
1555 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1556 initializing_client()->pc()->local_description()->candidates(0);
1557 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1558 receiving_client()->pc()->local_description()->candidates(0);
1559 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1560 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1561 std::string initiator_candidate_restart;
1562 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1563 &initiator_candidate_restart));
1564 std::string receiver_candidate_restart;
1565 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1566 &receiver_candidate_restart));
1567
1568 // Verify that the first candidates in the local session descriptions has
1569 // changed.
1570 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1571 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1572}
1573
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574// This test sets up a Jsep call between two parties with external
1575// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001576// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1577// See issue webrtc/2378.
1578TEST_F(JsepPeerConnectionP2PTestClient,
1579 DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580 ASSERT_TRUE(CreateTestClients());
1581 EnableVideoDecoderFactory();
1582 LocalP2PTest();
1583}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001584
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001585#endif // if !defined(THREAD_SANITIZER)