blob: fd58ecdff6f46238c3b5aa8cbd0e9b6b6c6fad65 [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"
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +000044#include "talk/app/webrtc/test/fakedtlsidentityservice.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
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000783 virtual rtc::scoped_refptr<webrtc::PeerConnectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 CreatePeerConnection(webrtc::PortAllocatorFactoryInterface* factory,
785 const MediaConstraintsInterface* constraints) {
786 // CreatePeerConnection with IceServers.
787 webrtc::PeerConnectionInterface::IceServers ice_servers;
788 webrtc::PeerConnectionInterface::IceServer ice_server;
789 ice_server.uri = "stun:stun.l.google.com:19302";
790 ice_servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000791
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000792 FakeIdentityService* dtls_service =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000793 rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000794 new FakeIdentityService() : NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795 return peer_connection_factory()->CreatePeerConnection(
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000796 ice_servers, constraints, factory, dtls_service, this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 }
798
799 void HandleIncomingOffer(const std::string& msg) {
800 LOG(INFO) << id() << "HandleIncomingOffer ";
801 if (NumberOfLocalMediaStreams() == 0) {
802 // If we are not sending any streams ourselves it is time to add some.
803 AddMediaStream(true, true);
804 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000805 rtc::scoped_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 webrtc::CreateSessionDescription("offer", msg, NULL));
807 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000808 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 EXPECT_TRUE(DoCreateAnswer(answer.use()));
810 std::string sdp;
811 EXPECT_TRUE(answer->ToString(&sdp));
812 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
813 if (signaling_message_receiver()) {
814 signaling_message_receiver()->ReceiveSdpMessage(
815 webrtc::SessionDescriptionInterface::kAnswer, sdp);
816 }
817 }
818
819 void HandleIncomingAnswer(const std::string& msg) {
820 LOG(INFO) << id() << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000821 rtc::scoped_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 webrtc::CreateSessionDescription("answer", msg, NULL));
823 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
824 }
825
826 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
827 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000828 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
829 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 MockCreateSessionDescriptionObserver>());
831 if (offer) {
832 pc()->CreateOffer(observer, &session_description_constraints_);
833 } else {
834 pc()->CreateAnswer(observer, &session_description_constraints_);
835 }
836 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
837 *desc = observer->release_desc();
838 if (observer->result() && ExpectIceRestart()) {
839 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
840 }
841 return observer->result();
842 }
843
844 bool DoCreateOffer(SessionDescriptionInterface** desc) {
845 return DoCreateOfferAnswer(desc, true);
846 }
847
848 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
849 return DoCreateOfferAnswer(desc, false);
850 }
851
852 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000853 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
854 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 MockSetSessionDescriptionObserver>());
856 LOG(INFO) << id() << "SetLocalDescription ";
857 pc()->SetLocalDescription(observer, desc);
858 // Ignore the observer result. If we wait for the result with
859 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
860 // before the offer which is an error.
861 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000862 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 // ProcessMessages waits at least 1ms but processes all messages before
864 // returning. Since this test is synchronous and send messages to the remote
865 // peer whenever a callback is invoked, this can lead to messages being
866 // sent to the remote peer in the wrong order.
867 // TODO(perkj): Find a way to check the result without risking that the
868 // order of sent messages are changed. Ex- by posting all messages that are
869 // sent to the remote peer.
870 return true;
871 }
872
873 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000874 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
875 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876 MockSetSessionDescriptionObserver>());
877 LOG(INFO) << id() << "SetRemoteDescription ";
878 pc()->SetRemoteDescription(observer, desc);
879 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
880 return observer->result();
881 }
882
883 // This modifies all received SDP messages before they are processed.
884 void FilterIncomingSdpMessage(std::string* sdp) {
885 if (remove_msid_) {
886 const char kSdpSsrcAttribute[] = "a=ssrc:";
887 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
888 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
889 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
890 }
891 if (remove_bundle_) {
892 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
893 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
894 }
895 if (remove_sdes_) {
896 const char kSdpSdesCryptoAttribute[] = "a=crypto";
897 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
898 }
899 }
900
901 private:
902 webrtc::FakeConstraints session_description_constraints_;
903 bool remove_msid_; // True if MSID should be removed in received SDP.
904 bool remove_bundle_; // True if bundle should be removed in received SDP.
905 bool remove_sdes_; // True if a=crypto should be removed in received SDP.
906
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000907 rtc::scoped_refptr<DataChannelInterface> data_channel_;
908 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909};
910
911template <typename SignalingClass>
912class P2PTestConductor : public testing::Test {
913 public:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000914 P2PTestConductor()
915 : pss_(new rtc::PhysicalSocketServer),
916 ss_(new rtc::VirtualSocketServer(pss_.get())),
917 ss_scope_(ss_.get()) {}
918
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 bool SessionActive() {
920 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000921 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000923
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924 // Return true if the number of frames provided have been received or it is
925 // known that that will never occur (e.g. no frames will be sent or
926 // captured).
927 bool FramesNotPending(int audio_frames_to_receive,
928 int video_frames_to_receive) {
929 return VideoFramesReceivedCheck(video_frames_to_receive) &&
930 AudioFramesReceivedCheck(audio_frames_to_receive);
931 }
932 bool AudioFramesReceivedCheck(int frames_received) {
933 return initiating_client_->AudioFramesReceivedCheck(frames_received) &&
934 receiving_client_->AudioFramesReceivedCheck(frames_received);
935 }
936 bool VideoFramesReceivedCheck(int frames_received) {
937 return initiating_client_->VideoFramesReceivedCheck(frames_received) &&
938 receiving_client_->VideoFramesReceivedCheck(frames_received);
939 }
940 void VerifyDtmf() {
941 initiating_client_->VerifyDtmf();
942 receiving_client_->VerifyDtmf();
943 }
944
945 void TestUpdateOfferWithRejectedContent() {
946 initiating_client_->Negotiate(true, false);
947 EXPECT_TRUE_WAIT(
948 FramesNotPending(kEndAudioFrameCount * 2, kEndVideoFrameCount),
949 kMaxWaitForFramesMs);
950 // There shouldn't be any more video frame after the new offer is
951 // negotiated.
952 EXPECT_FALSE(VideoFramesReceivedCheck(kEndVideoFrameCount + 1));
953 }
954
955 void VerifyRenderedSize(int width, int height) {
956 EXPECT_EQ(width, receiving_client()->rendered_width());
957 EXPECT_EQ(height, receiving_client()->rendered_height());
958 EXPECT_EQ(width, initializing_client()->rendered_width());
959 EXPECT_EQ(height, initializing_client()->rendered_height());
960 }
961
962 void VerifySessionDescriptions() {
963 initiating_client_->VerifyRejectedMediaInSessionDescription();
964 receiving_client_->VerifyRejectedMediaInSessionDescription();
965 initiating_client_->VerifyLocalIceUfragAndPassword();
966 receiving_client_->VerifyLocalIceUfragAndPassword();
967 }
968
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969 ~P2PTestConductor() {
970 if (initiating_client_) {
971 initiating_client_->set_signaling_message_receiver(NULL);
972 }
973 if (receiving_client_) {
974 receiving_client_->set_signaling_message_receiver(NULL);
975 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 }
977
978 bool CreateTestClients() {
979 return CreateTestClients(NULL, NULL);
980 }
981
982 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
983 MediaConstraintsInterface* recv_constraints) {
Joachim Bauch04e5b492015-05-29 09:40:39 +0200984 return CreateTestClients(init_constraints, NULL, recv_constraints, NULL);
985 }
986
987 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
988 PeerConnectionFactory::Options* init_options,
989 MediaConstraintsInterface* recv_constraints,
990 PeerConnectionFactory::Options* recv_options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 initiating_client_.reset(SignalingClass::CreateClient("Caller: ",
Joachim Bauch04e5b492015-05-29 09:40:39 +0200992 init_constraints,
993 init_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 receiving_client_.reset(SignalingClass::CreateClient("Callee: ",
Joachim Bauch04e5b492015-05-29 09:40:39 +0200995 recv_constraints,
996 recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000997 if (!initiating_client_ || !receiving_client_) {
998 return false;
999 }
1000 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
1001 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1002 return true;
1003 }
1004
1005 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1006 const webrtc::FakeConstraints& recv_constraints) {
1007 initiating_client_->SetVideoConstraints(init_constraints);
1008 receiving_client_->SetVideoConstraints(recv_constraints);
1009 }
1010
1011 void EnableVideoDecoderFactory() {
1012 initiating_client_->EnableVideoDecoderFactory();
1013 receiving_client_->EnableVideoDecoderFactory();
1014 }
1015
1016 // This test sets up a call between two parties. Both parties send static
1017 // frames to each other. Once the test is finished the number of sent frames
1018 // is compared to the number of received frames.
1019 void LocalP2PTest() {
1020 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1021 initiating_client_->AddMediaStream(true, true);
1022 }
1023 initiating_client_->Negotiate();
1024 const int kMaxWaitForActivationMs = 5000;
1025 // Assert true is used here since next tests are guaranteed to fail and
1026 // would eat up 5 seconds.
1027 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1028 VerifySessionDescriptions();
1029
1030
1031 int audio_frame_count = kEndAudioFrameCount;
1032 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1033 if (!initiating_client_->can_receive_audio() ||
1034 !receiving_client_->can_receive_audio()) {
1035 audio_frame_count = -1;
1036 }
1037 int video_frame_count = kEndVideoFrameCount;
1038 if (!initiating_client_->can_receive_video() ||
1039 !receiving_client_->can_receive_video()) {
1040 video_frame_count = -1;
1041 }
1042
1043 if (audio_frame_count != -1 || video_frame_count != -1) {
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001044 // Audio or video is expected to flow, so both clients should reach the
1045 // Connected state, and the offerer (ICE controller) should proceed to
1046 // Completed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 // Note: These tests have been observed to fail under heavy load at
1048 // shorter timeouts, so they may be flaky.
1049 EXPECT_EQ_WAIT(
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001050 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051 initiating_client_->ice_connection_state(),
1052 kMaxWaitForFramesMs);
1053 EXPECT_EQ_WAIT(
1054 webrtc::PeerConnectionInterface::kIceConnectionConnected,
1055 receiving_client_->ice_connection_state(),
1056 kMaxWaitForFramesMs);
1057 }
1058
1059 if (initiating_client_->can_receive_audio() ||
1060 initiating_client_->can_receive_video()) {
1061 // The initiating client can receive media, so it must produce candidates
1062 // that will serve as destinations for that media.
1063 // TODO(bemasc): Understand why the state is not already Complete here, as
1064 // seems to be the case for the receiving client. This may indicate a bug
1065 // in the ICE gathering system.
1066 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1067 initiating_client_->ice_gathering_state());
1068 }
1069 if (receiving_client_->can_receive_audio() ||
1070 receiving_client_->can_receive_video()) {
1071 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1072 receiving_client_->ice_gathering_state(),
1073 kMaxWaitForFramesMs);
1074 }
1075
1076 EXPECT_TRUE_WAIT(FramesNotPending(audio_frame_count, video_frame_count),
1077 kMaxWaitForFramesMs);
1078 }
1079
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001080 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1081 // Messages may get lost on the unreliable DataChannel, so we send multiple
1082 // times to avoid test flakiness.
1083 static const size_t kSendAttempts = 5;
1084
1085 for (size_t i = 0; i < kSendAttempts; ++i) {
1086 dc->Send(DataBuffer(data));
1087 }
1088 }
1089
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001090 SignalingClass* initializing_client() { return initiating_client_.get(); }
1091 SignalingClass* receiving_client() { return receiving_client_.get(); }
1092
1093 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001094 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1095 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1096 rtc::SocketServerScope ss_scope_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001097 rtc::scoped_ptr<SignalingClass> initiating_client_;
1098 rtc::scoped_ptr<SignalingClass> receiving_client_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099};
1100typedef P2PTestConductor<JsepTestClient> JsepPeerConnectionP2PTestClient;
1101
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001102// Disable for TSan v2, see
1103// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1104#if !defined(THREAD_SANITIZER)
1105
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001106// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001107// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1108// See issue webrtc/2378.
1109TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001110 ASSERT_TRUE(CreateTestClients());
1111 LocalP2PTest();
1112 VerifyDtmf();
1113}
1114
1115// This test sets up a Jsep call between two parties and test that we can get a
1116// video aspect ratio of 16:9.
1117TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTest16To9) {
1118 ASSERT_TRUE(CreateTestClients());
1119 FakeConstraints constraint;
1120 double requested_ratio = 640.0/360;
1121 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1122 SetVideoConstraints(constraint, constraint);
1123 LocalP2PTest();
1124
1125 ASSERT_LE(0, initializing_client()->rendered_height());
1126 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001127 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128 initializing_client()->rendered_height();
1129 EXPECT_LE(requested_ratio, initiating_video_ratio);
1130
1131 ASSERT_LE(0, receiving_client()->rendered_height());
1132 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001133 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001134 receiving_client()->rendered_height();
1135 EXPECT_LE(requested_ratio, receiving_video_ratio);
1136}
1137
1138// This test sets up a Jsep call between two parties and test that the
1139// received video has a resolution of 1280*720.
1140// TODO(mallinath): Enable when
1141// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
1142TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTest1280By720) {
1143 ASSERT_TRUE(CreateTestClients());
1144 FakeConstraints constraint;
1145 constraint.SetMandatoryMinWidth(1280);
1146 constraint.SetMandatoryMinHeight(720);
1147 SetVideoConstraints(constraint, constraint);
1148 LocalP2PTest();
1149 VerifyRenderedSize(1280, 720);
1150}
1151
1152// This test sets up a call between two endpoints that are configured to use
1153// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
1154TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtls) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001155 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001156 FakeConstraints setup_constraints;
1157 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1158 true);
1159 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1160 LocalP2PTest();
1161 VerifyRenderedSize(640, 480);
1162}
1163
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001164// This test sets up a audio call initially and then upgrades to audio/video,
1165// using DTLS.
mallinath@webrtc.org50bc5532013-10-21 17:58:35 +00001166TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001167 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001168 FakeConstraints setup_constraints;
1169 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1170 true);
1171 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1172 receiving_client()->SetReceiveAudioVideo(true, false);
1173 LocalP2PTest();
1174 receiving_client()->SetReceiveAudioVideo(true, true);
1175 receiving_client()->Negotiate();
1176}
1177
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001178// This test sets up a call between two endpoints that are configured to use
1179// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1180// negotiated and used for transport.
1181TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001182 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183 FakeConstraints setup_constraints;
1184 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1185 true);
1186 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1187 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1188 LocalP2PTest();
1189 VerifyRenderedSize(640, 480);
1190}
1191
1192// This test sets up a Jsep call between two parties, and the callee only
1193// accept to receive video.
solenberg@webrtc.org503c3362015-02-12 13:12:50 +00001194TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195 ASSERT_TRUE(CreateTestClients());
1196 receiving_client()->SetReceiveAudioVideo(false, true);
1197 LocalP2PTest();
1198}
1199
1200// This test sets up a Jsep call between two parties, and the callee only
1201// accept to receive audio.
solenberg@webrtc.org503c3362015-02-12 13:12:50 +00001202TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203 ASSERT_TRUE(CreateTestClients());
1204 receiving_client()->SetReceiveAudioVideo(true, false);
1205 LocalP2PTest();
1206}
1207
1208// This test sets up a Jsep call between two parties, and the callee reject both
1209// audio and video.
1210TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerNone) {
1211 ASSERT_TRUE(CreateTestClients());
1212 receiving_client()->SetReceiveAudioVideo(false, false);
1213 LocalP2PTest();
1214}
1215
1216// This test sets up an audio and video call between two parties. After the call
1217// runs for a while (10 frames), the caller sends an update offer with video
1218// being rejected. Once the re-negotiation is done, the video flow should stop
1219// and the audio flow should continue.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001220// Disabled due to b/14955157.
1221TEST_F(JsepPeerConnectionP2PTestClient,
1222 DISABLED_UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223 ASSERT_TRUE(CreateTestClients());
1224 LocalP2PTest();
1225 TestUpdateOfferWithRejectedContent();
1226}
1227
1228// This test sets up a Jsep call between two parties. The MSID is removed from
1229// the SDP strings from the caller.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001230// Disabled due to b/14955157.
1231TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001232 ASSERT_TRUE(CreateTestClients());
1233 receiving_client()->RemoveMsidFromReceivedSdp(true);
1234 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1235 // audio and video is muxed when MSID is disabled. Remove
1236 // SetRemoveBundleFromSdp once
1237 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1238 receiving_client()->RemoveBundleFromReceivedSdp(true);
1239 LocalP2PTest();
1240}
1241
1242// This test sets up a Jsep call between two parties and the initiating peer
1243// sends two steams.
1244// TODO(perkj): Disabled due to
1245// https://code.google.com/p/webrtc/issues/detail?id=1454
1246TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestTwoStreams) {
1247 ASSERT_TRUE(CreateTestClients());
1248 // Set optional video constraint to max 320pixels to decrease CPU usage.
1249 FakeConstraints constraint;
1250 constraint.SetOptionalMaxWidth(320);
1251 SetVideoConstraints(constraint, constraint);
1252 initializing_client()->AddMediaStream(true, true);
1253 initializing_client()->AddMediaStream(false, true);
1254 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1255 LocalP2PTest();
1256 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1257}
1258
1259// Test that we can receive the audio output level from a remote audio track.
1260TEST_F(JsepPeerConnectionP2PTestClient, GetAudioOutputLevelStats) {
1261 ASSERT_TRUE(CreateTestClients());
1262 LocalP2PTest();
1263
1264 StreamCollectionInterface* remote_streams =
1265 initializing_client()->remote_streams();
1266 ASSERT_GT(remote_streams->count(), 0u);
1267 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1268 MediaStreamTrackInterface* remote_audio_track =
1269 remote_streams->at(0)->GetAudioTracks()[0];
1270
1271 // Get the audio output level stats. Note that the level is not available
1272 // until a RTCP packet has been received.
1273 EXPECT_TRUE_WAIT(
1274 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1275 kMaxWaitForStatsMs);
1276}
1277
1278// Test that an audio input level is reported.
1279TEST_F(JsepPeerConnectionP2PTestClient, GetAudioInputLevelStats) {
1280 ASSERT_TRUE(CreateTestClients());
1281 LocalP2PTest();
1282
1283 // Get the audio input level stats. The level should be available very
1284 // soon after the test starts.
1285 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1286 kMaxWaitForStatsMs);
1287}
1288
1289// Test that we can get incoming byte counts from both audio and video tracks.
1290TEST_F(JsepPeerConnectionP2PTestClient, GetBytesReceivedStats) {
1291 ASSERT_TRUE(CreateTestClients());
1292 LocalP2PTest();
1293
1294 StreamCollectionInterface* remote_streams =
1295 initializing_client()->remote_streams();
1296 ASSERT_GT(remote_streams->count(), 0u);
1297 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1298 MediaStreamTrackInterface* remote_audio_track =
1299 remote_streams->at(0)->GetAudioTracks()[0];
1300 EXPECT_TRUE_WAIT(
1301 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1302 kMaxWaitForStatsMs);
1303
1304 MediaStreamTrackInterface* remote_video_track =
1305 remote_streams->at(0)->GetVideoTracks()[0];
1306 EXPECT_TRUE_WAIT(
1307 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1308 kMaxWaitForStatsMs);
1309}
1310
1311// Test that we can get outgoing byte counts from both audio and video tracks.
1312TEST_F(JsepPeerConnectionP2PTestClient, GetBytesSentStats) {
1313 ASSERT_TRUE(CreateTestClients());
1314 LocalP2PTest();
1315
1316 StreamCollectionInterface* local_streams =
1317 initializing_client()->local_streams();
1318 ASSERT_GT(local_streams->count(), 0u);
1319 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1320 MediaStreamTrackInterface* local_audio_track =
1321 local_streams->at(0)->GetAudioTracks()[0];
1322 EXPECT_TRUE_WAIT(
1323 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1324 kMaxWaitForStatsMs);
1325
1326 MediaStreamTrackInterface* local_video_track =
1327 local_streams->at(0)->GetVideoTracks()[0];
1328 EXPECT_TRUE_WAIT(
1329 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1330 kMaxWaitForStatsMs);
1331}
1332
Joachim Bauch04e5b492015-05-29 09:40:39 +02001333// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
1334TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12None) {
1335 PeerConnectionFactory::Options init_options;
1336 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1337 PeerConnectionFactory::Options recv_options;
1338 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1339 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001340 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1341 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1342 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001343 LocalP2PTest();
1344
Joachim Bauch04e5b492015-05-29 09:40:39 +02001345 EXPECT_EQ_WAIT(
1346 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_10),
1347 initializing_client()->GetDtlsCipherStats(),
1348 kMaxWaitForStatsMs);
jbauchac8869e2015-07-03 01:36:14 -07001349 EXPECT_EQ(
1350 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_10),
1351 init_observer->GetStringHistogramSample(webrtc::kAudioSslCipher));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001352
1353 EXPECT_EQ_WAIT(
1354 kDefaultSrtpCipher,
1355 initializing_client()->GetSrtpCipherStats(),
1356 kMaxWaitForStatsMs);
jbauchac8869e2015-07-03 01:36:14 -07001357 EXPECT_EQ(
1358 kDefaultSrtpCipher,
1359 init_observer->GetStringHistogramSample(webrtc::kAudioSrtpCipher));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001360}
1361
1362// Test that DTLS 1.2 is used if both ends support it.
1363TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Both) {
1364 PeerConnectionFactory::Options init_options;
1365 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1366 PeerConnectionFactory::Options recv_options;
1367 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1368 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001369 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1370 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1371 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001372 LocalP2PTest();
1373
1374 EXPECT_EQ_WAIT(
1375 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_12),
1376 initializing_client()->GetDtlsCipherStats(),
1377 kMaxWaitForStatsMs);
jbauchac8869e2015-07-03 01:36:14 -07001378 EXPECT_EQ(
1379 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_12),
1380 init_observer->GetStringHistogramSample(webrtc::kAudioSslCipher));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001381
1382 EXPECT_EQ_WAIT(
1383 kDefaultSrtpCipher,
1384 initializing_client()->GetSrtpCipherStats(),
1385 kMaxWaitForStatsMs);
jbauchac8869e2015-07-03 01:36:14 -07001386 EXPECT_EQ(
1387 kDefaultSrtpCipher,
1388 init_observer->GetStringHistogramSample(webrtc::kAudioSrtpCipher));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001389}
1390
1391// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1392// received supports 1.0.
1393TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Init) {
1394 PeerConnectionFactory::Options init_options;
1395 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1396 PeerConnectionFactory::Options recv_options;
1397 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1398 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001399 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1400 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1401 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001402 LocalP2PTest();
1403
1404 EXPECT_EQ_WAIT(
1405 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_10),
1406 initializing_client()->GetDtlsCipherStats(),
1407 kMaxWaitForStatsMs);
jbauchac8869e2015-07-03 01:36:14 -07001408 EXPECT_EQ(
1409 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_10),
1410 init_observer->GetStringHistogramSample(webrtc::kAudioSslCipher));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001411
1412 EXPECT_EQ_WAIT(
1413 kDefaultSrtpCipher,
1414 initializing_client()->GetSrtpCipherStats(),
1415 kMaxWaitForStatsMs);
jbauchac8869e2015-07-03 01:36:14 -07001416 EXPECT_EQ(
1417 kDefaultSrtpCipher,
1418 init_observer->GetStringHistogramSample(webrtc::kAudioSrtpCipher));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001419}
1420
1421// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1422// received supports 1.2.
1423TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Recv) {
1424 PeerConnectionFactory::Options init_options;
1425 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1426 PeerConnectionFactory::Options recv_options;
1427 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1428 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001429 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1430 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1431 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001432 LocalP2PTest();
1433
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001434 EXPECT_EQ_WAIT(
Joachim Bauch831c5582015-05-20 12:48:41 +02001435 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_10),
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001436 initializing_client()->GetDtlsCipherStats(),
1437 kMaxWaitForStatsMs);
jbauchac8869e2015-07-03 01:36:14 -07001438 EXPECT_EQ(
1439 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_10),
1440 init_observer->GetStringHistogramSample(webrtc::kAudioSslCipher));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001441
1442 EXPECT_EQ_WAIT(
1443 kDefaultSrtpCipher,
1444 initializing_client()->GetSrtpCipherStats(),
1445 kMaxWaitForStatsMs);
jbauchac8869e2015-07-03 01:36:14 -07001446 EXPECT_EQ(
1447 kDefaultSrtpCipher,
1448 init_observer->GetStringHistogramSample(webrtc::kAudioSrtpCipher));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001449}
1450
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001451// This test sets up a call between two parties with audio, video and data.
1452TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDataChannel) {
1453 FakeConstraints setup_constraints;
1454 setup_constraints.SetAllowRtpDataChannels();
1455 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1456 initializing_client()->CreateDataChannel();
1457 LocalP2PTest();
1458 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1459 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1460 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1461 kMaxWaitMs);
1462 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1463 kMaxWaitMs);
1464
1465 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001466
1467 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001468 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1469 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001470
1471 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001472 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1473 kMaxWaitMs);
1474
1475 receiving_client()->data_channel()->Close();
1476 // Send new offer and answer.
1477 receiving_client()->Negotiate();
1478 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1479 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1480}
1481
1482// This test sets up a call between two parties and creates a data channel.
1483// The test tests that received data is buffered unless an observer has been
1484// registered.
1485// Rtp data channels can receive data before the underlying
1486// transport has detected that a channel is writable and thus data can be
1487// received before the data channel state changes to open. That is hard to test
1488// but the same buffering is used in that case.
1489TEST_F(JsepPeerConnectionP2PTestClient, RegisterDataChannelObserver) {
1490 FakeConstraints setup_constraints;
1491 setup_constraints.SetAllowRtpDataChannels();
1492 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1493 initializing_client()->CreateDataChannel();
1494 initializing_client()->Negotiate();
1495
1496 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1497 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1498 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1499 kMaxWaitMs);
1500 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1501 receiving_client()->data_channel()->state(), kMaxWaitMs);
1502
1503 // Unregister the existing observer.
1504 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001505
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001507 SendRtpData(initializing_client()->data_channel(), data);
1508
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001509 // Wait a while to allow the sent data to arrive before an observer is
1510 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001511 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512
1513 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1514 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1515}
1516
1517// This test sets up a call between two parties with audio, video and but only
1518// the initiating client support data.
1519TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001520 FakeConstraints setup_constraints_1;
1521 setup_constraints_1.SetAllowRtpDataChannels();
1522 // Must disable DTLS to make negotiation succeed.
1523 setup_constraints_1.SetMandatory(
1524 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1525 FakeConstraints setup_constraints_2;
1526 setup_constraints_2.SetMandatory(
1527 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1528 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001529 initializing_client()->CreateDataChannel();
1530 LocalP2PTest();
1531 EXPECT_TRUE(initializing_client()->data_channel() != NULL);
1532 EXPECT_FALSE(receiving_client()->data_channel());
1533 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1534}
1535
1536// This test sets up a call between two parties with audio, video. When audio
1537// and video is setup and flowing and data channel is negotiated.
1538TEST_F(JsepPeerConnectionP2PTestClient, AddDataChannelAfterRenegotiation) {
1539 FakeConstraints setup_constraints;
1540 setup_constraints.SetAllowRtpDataChannels();
1541 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1542 LocalP2PTest();
1543 initializing_client()->CreateDataChannel();
1544 // Send new offer and answer.
1545 initializing_client()->Negotiate();
1546 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1547 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1548 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1549 kMaxWaitMs);
1550 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1551 kMaxWaitMs);
1552}
1553
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001554// This test sets up a Jsep call with SCTP DataChannel and verifies the
1555// negotiation is completed without error.
1556#ifdef HAVE_SCTP
1557TEST_F(JsepPeerConnectionP2PTestClient, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001558 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001559 FakeConstraints constraints;
1560 constraints.SetMandatory(
1561 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1562 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1563 initializing_client()->CreateDataChannel();
1564 initializing_client()->Negotiate(false, false);
1565}
1566#endif
1567
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568// This test sets up a call between two parties with audio, and video.
1569// During the call, the initializing side restart ice and the test verifies that
1570// new ice candidates are generated and audio and video still can flow.
1571TEST_F(JsepPeerConnectionP2PTestClient, IceRestart) {
1572 ASSERT_TRUE(CreateTestClients());
1573
1574 // Negotiate and wait for ice completion and make sure audio and video plays.
1575 LocalP2PTest();
1576
1577 // Create a SDP string of the first audio candidate for both clients.
1578 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1579 initializing_client()->pc()->local_description()->candidates(0);
1580 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1581 receiving_client()->pc()->local_description()->candidates(0);
1582 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1583 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1584 std::string initiator_candidate;
1585 EXPECT_TRUE(
1586 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1587 std::string receiver_candidate;
1588 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1589
1590 // Restart ice on the initializing client.
1591 receiving_client()->SetExpectIceRestart(true);
1592 initializing_client()->IceRestart();
1593
1594 // Negotiate and wait for ice completion again and make sure audio and video
1595 // plays.
1596 LocalP2PTest();
1597
1598 // Create a SDP string of the first audio candidate for both clients again.
1599 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1600 initializing_client()->pc()->local_description()->candidates(0);
1601 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1602 receiving_client()->pc()->local_description()->candidates(0);
1603 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1604 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1605 std::string initiator_candidate_restart;
1606 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1607 &initiator_candidate_restart));
1608 std::string receiver_candidate_restart;
1609 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1610 &receiver_candidate_restart));
1611
1612 // Verify that the first candidates in the local session descriptions has
1613 // changed.
1614 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1615 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1616}
1617
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001618// This test sets up a Jsep call between two parties with external
1619// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001620// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1621// See issue webrtc/2378.
1622TEST_F(JsepPeerConnectionP2PTestClient,
1623 DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624 ASSERT_TRUE(CreateTestClients());
1625 EnableVideoDecoderFactory();
1626 LocalP2PTest();
1627}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001628
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001629#endif // if !defined(THREAD_SANITIZER)