blob: b9d80196e6b1d635bc3a9a895d7468b0e64b324e [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);
jbauchbe24c942015-06-22 15:06:43 -0700353 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354 return observer->AudioOutputLevel();
355 }
356
357 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000358 rtc::scoped_refptr<MockStatsObserver>
359 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000360 EXPECT_TRUE(peer_connection_->GetStats(
361 observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700363 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364 return observer->AudioInputLevel();
365 }
366
367 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000368 rtc::scoped_refptr<MockStatsObserver>
369 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000370 EXPECT_TRUE(peer_connection_->GetStats(
371 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000372 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700373 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374 return observer->BytesReceived();
375 }
376
377 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000378 rtc::scoped_refptr<MockStatsObserver>
379 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000380 EXPECT_TRUE(peer_connection_->GetStats(
381 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700383 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000384 return observer->BytesSent();
385 }
386
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000387 int GetAvailableReceivedBandwidthStats() {
388 rtc::scoped_refptr<MockStatsObserver>
389 observer(new rtc::RefCountedObject<MockStatsObserver>());
390 EXPECT_TRUE(peer_connection_->GetStats(
391 observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
392 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700393 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000394 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000395 return bw;
396 }
397
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000398 std::string GetDtlsCipherStats() {
399 rtc::scoped_refptr<MockStatsObserver>
400 observer(new rtc::RefCountedObject<MockStatsObserver>());
401 EXPECT_TRUE(peer_connection_->GetStats(
402 observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
403 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700404 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000405 return observer->DtlsCipher();
406 }
407
408 std::string GetSrtpCipherStats() {
409 rtc::scoped_refptr<MockStatsObserver>
410 observer(new rtc::RefCountedObject<MockStatsObserver>());
411 EXPECT_TRUE(peer_connection_->GetStats(
412 observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
413 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700414 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000415 return observer->SrtpCipher();
416 }
417
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418 int rendered_width() {
419 EXPECT_FALSE(fake_video_renderers_.empty());
420 return fake_video_renderers_.empty() ? 1 :
421 fake_video_renderers_.begin()->second->width();
422 }
423
424 int rendered_height() {
425 EXPECT_FALSE(fake_video_renderers_.empty());
426 return fake_video_renderers_.empty() ? 1 :
427 fake_video_renderers_.begin()->second->height();
428 }
429
430 size_t number_of_remote_streams() {
431 if (!pc())
432 return 0;
433 return pc()->remote_streams()->count();
434 }
435
436 StreamCollectionInterface* remote_streams() {
437 if (!pc()) {
438 ADD_FAILURE();
439 return NULL;
440 }
441 return pc()->remote_streams();
442 }
443
444 StreamCollectionInterface* local_streams() {
445 if (!pc()) {
446 ADD_FAILURE();
447 return NULL;
448 }
449 return pc()->local_streams();
450 }
451
452 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
453 return pc()->signaling_state();
454 }
455
456 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
457 return pc()->ice_connection_state();
458 }
459
460 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
461 return pc()->ice_gathering_state();
462 }
463
464 // PeerConnectionObserver callbacks.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465 virtual void OnMessage(const std::string&) {}
466 virtual void OnSignalingMessage(const std::string& /*msg*/) {}
467 virtual void OnSignalingChange(
468 webrtc::PeerConnectionInterface::SignalingState new_state) {
469 EXPECT_EQ(peer_connection_->signaling_state(), new_state);
470 }
471 virtual void OnAddStream(webrtc::MediaStreamInterface* media_stream) {
472 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
473 const std::string id = media_stream->GetVideoTracks()[i]->id();
474 ASSERT_TRUE(fake_video_renderers_.find(id) ==
475 fake_video_renderers_.end());
476 fake_video_renderers_[id] = new webrtc::FakeVideoTrackRenderer(
477 media_stream->GetVideoTracks()[i]);
478 }
479 }
480 virtual void OnRemoveStream(webrtc::MediaStreamInterface* media_stream) {}
481 virtual void OnRenegotiationNeeded() {}
482 virtual void OnIceConnectionChange(
483 webrtc::PeerConnectionInterface::IceConnectionState new_state) {
484 EXPECT_EQ(peer_connection_->ice_connection_state(), new_state);
485 }
486 virtual void OnIceGatheringChange(
487 webrtc::PeerConnectionInterface::IceGatheringState new_state) {
488 EXPECT_EQ(peer_connection_->ice_gathering_state(), new_state);
489 }
490 virtual void OnIceCandidate(
491 const webrtc::IceCandidateInterface* /*candidate*/) {}
492
493 webrtc::PeerConnectionInterface* pc() {
494 return peer_connection_.get();
495 }
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000496 void StopVideoCapturers() {
497 for (std::vector<cricket::VideoCapturer*>::iterator it =
498 video_capturers_.begin(); it != video_capturers_.end(); ++it) {
499 (*it)->Stop();
500 }
501 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502
503 protected:
504 explicit PeerConnectionTestClientBase(const std::string& id)
505 : id_(id),
506 expect_ice_restart_(false),
507 fake_video_decoder_factory_(NULL),
508 fake_video_encoder_factory_(NULL),
509 video_decoder_factory_enabled_(false),
510 signaling_message_receiver_(NULL) {
511 }
Joachim Bauch04e5b492015-05-29 09:40:39 +0200512 bool Init(const MediaConstraintsInterface* constraints,
513 const PeerConnectionFactory::Options* options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514 EXPECT_TRUE(!peer_connection_);
515 EXPECT_TRUE(!peer_connection_factory_);
516 allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
517 if (!allocator_factory_) {
518 return false;
519 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 fake_audio_capture_module_ = FakeAudioCaptureModule::Create(
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000521 rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522
523 if (fake_audio_capture_module_ == NULL) {
524 return false;
525 }
526 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
527 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
528 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000529 rtc::Thread::Current(), rtc::Thread::Current(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530 fake_audio_capture_module_, fake_video_encoder_factory_,
531 fake_video_decoder_factory_);
532 if (!peer_connection_factory_) {
533 return false;
534 }
Joachim Bauch04e5b492015-05-29 09:40:39 +0200535 if (options) {
536 peer_connection_factory_->SetOptions(*options);
537 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538 peer_connection_ = CreatePeerConnection(allocator_factory_.get(),
539 constraints);
540 return peer_connection_.get() != NULL;
541 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000542 virtual rtc::scoped_refptr<webrtc::PeerConnectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 CreatePeerConnection(webrtc::PortAllocatorFactoryInterface* factory,
544 const MediaConstraintsInterface* constraints) = 0;
545 MessageReceiver* signaling_message_receiver() {
546 return signaling_message_receiver_;
547 }
548 webrtc::PeerConnectionFactoryInterface* peer_connection_factory() {
549 return peer_connection_factory_.get();
550 }
551
552 virtual bool can_receive_audio() = 0;
553 virtual bool can_receive_video() = 0;
554 const std::string& id() const { return id_; }
555
556 private:
557 class DummyDtmfObserver : public DtmfSenderObserverInterface {
558 public:
559 DummyDtmfObserver() : completed_(false) {}
560
561 // Implements DtmfSenderObserverInterface.
562 void OnToneChange(const std::string& tone) {
563 tones_.push_back(tone);
564 if (tone.empty()) {
565 completed_ = true;
566 }
567 }
568
569 void Verify(const std::vector<std::string>& tones) const {
570 ASSERT_TRUE(tones_.size() == tones.size());
571 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
572 }
573
574 bool completed() const { return completed_; }
575
576 private:
577 bool completed_;
578 std::vector<std::string> tones_;
579 };
580
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000581 rtc::scoped_refptr<webrtc::VideoTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 CreateLocalVideoTrack(const std::string stream_label) {
583 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
584 FakeConstraints source_constraints = video_constraints_;
585 source_constraints.SetMandatoryMaxFrameRate(10);
586
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000587 cricket::FakeVideoCapturer* fake_capturer =
588 new webrtc::FakePeriodicVideoCapturer();
589 video_capturers_.push_back(fake_capturer);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000590 rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 peer_connection_factory_->CreateVideoSource(
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000592 fake_capturer, &source_constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593 std::string label = stream_label + kVideoTrackLabelBase;
594 return peer_connection_factory_->CreateVideoTrack(label, source);
595 }
596
597 std::string id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000599 rtc::scoped_refptr<webrtc::PortAllocatorFactoryInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600 allocator_factory_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000601 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
602 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 peer_connection_factory_;
604
605 typedef std::pair<std::string, std::string> IceUfragPwdPair;
606 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
607 bool expect_ice_restart_;
608
609 // Needed to keep track of number of frames send.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000610 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611 // Needed to keep track of number of frames received.
612 typedef std::map<std::string, webrtc::FakeVideoTrackRenderer*> RenderMap;
613 RenderMap fake_video_renderers_;
614 // Needed to keep track of number of frames received when external decoder
615 // used.
616 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_;
617 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_;
618 bool video_decoder_factory_enabled_;
619 webrtc::FakeConstraints video_constraints_;
620
621 // For remote peer communication.
622 MessageReceiver* signaling_message_receiver_;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000623
624 // Store references to the video capturers we've created, so that we can stop
625 // them, if required.
626 std::vector<cricket::VideoCapturer*> video_capturers_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627};
628
629class JsepTestClient
630 : public PeerConnectionTestClientBase<JsepMessageReceiver> {
631 public:
632 static JsepTestClient* CreateClient(
633 const std::string& id,
Joachim Bauch04e5b492015-05-29 09:40:39 +0200634 const MediaConstraintsInterface* constraints,
635 const PeerConnectionFactory::Options* options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636 JsepTestClient* client(new JsepTestClient(id));
Joachim Bauch04e5b492015-05-29 09:40:39 +0200637 if (!client->Init(constraints, options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638 delete client;
639 return NULL;
640 }
641 return client;
642 }
643 ~JsepTestClient() {}
644
645 virtual void Negotiate() {
646 Negotiate(true, true);
647 }
648 virtual void Negotiate(bool audio, bool video) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000649 rtc::scoped_ptr<SessionDescriptionInterface> offer;
pbos@webrtc.orgceb956b2014-09-04 15:27:49 +0000650 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651
652 if (offer->description()->GetContentByName("audio")) {
653 offer->description()->GetContentByName("audio")->rejected = !audio;
654 }
655 if (offer->description()->GetContentByName("video")) {
656 offer->description()->GetContentByName("video")->rejected = !video;
657 }
658
659 std::string sdp;
660 EXPECT_TRUE(offer->ToString(&sdp));
661 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
662 signaling_message_receiver()->ReceiveSdpMessage(
663 webrtc::SessionDescriptionInterface::kOffer, sdp);
664 }
665 // JsepMessageReceiver callback.
666 virtual void ReceiveSdpMessage(const std::string& type,
667 std::string& msg) {
668 FilterIncomingSdpMessage(&msg);
669 if (type == webrtc::SessionDescriptionInterface::kOffer) {
670 HandleIncomingOffer(msg);
671 } else {
672 HandleIncomingAnswer(msg);
673 }
674 }
675 // JsepMessageReceiver callback.
676 virtual void ReceiveIceMessage(const std::string& sdp_mid,
677 int sdp_mline_index,
678 const std::string& msg) {
679 LOG(INFO) << id() << "ReceiveIceMessage";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000680 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, NULL));
682 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
683 }
684 // Implements PeerConnectionObserver functions needed by Jsep.
685 virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
686 LOG(INFO) << id() << "OnIceCandidate";
687
688 std::string ice_sdp;
689 EXPECT_TRUE(candidate->ToString(&ice_sdp));
690 if (signaling_message_receiver() == NULL) {
691 // Remote party may be deleted.
692 return;
693 }
694 signaling_message_receiver()->ReceiveIceMessage(candidate->sdp_mid(),
695 candidate->sdp_mline_index(), ice_sdp);
696 }
697
698 void IceRestart() {
699 session_description_constraints_.SetMandatoryIceRestart(true);
700 SetExpectIceRestart(true);
701 }
702
703 void SetReceiveAudioVideo(bool audio, bool video) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000704 SetReceiveAudio(audio);
705 SetReceiveVideo(video);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 ASSERT_EQ(audio, can_receive_audio());
707 ASSERT_EQ(video, can_receive_video());
708 }
709
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000710 void SetReceiveAudio(bool audio) {
711 if (audio && can_receive_audio())
712 return;
713 session_description_constraints_.SetMandatoryReceiveAudio(audio);
714 }
715
716 void SetReceiveVideo(bool video) {
717 if (video && can_receive_video())
718 return;
719 session_description_constraints_.SetMandatoryReceiveVideo(video);
720 }
721
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 void RemoveMsidFromReceivedSdp(bool remove) {
723 remove_msid_ = remove;
724 }
725
726 void RemoveSdesCryptoFromReceivedSdp(bool remove) {
727 remove_sdes_ = remove;
728 }
729
730 void RemoveBundleFromReceivedSdp(bool remove) {
731 remove_bundle_ = remove;
732 }
733
734 virtual bool can_receive_audio() {
735 bool value;
736 if (webrtc::FindConstraint(&session_description_constraints_,
737 MediaConstraintsInterface::kOfferToReceiveAudio, &value, NULL)) {
738 return value;
739 }
740 return true;
741 }
742
743 virtual bool can_receive_video() {
744 bool value;
745 if (webrtc::FindConstraint(&session_description_constraints_,
746 MediaConstraintsInterface::kOfferToReceiveVideo, &value, NULL)) {
747 return value;
748 }
749 return true;
750 }
751
752 virtual void OnIceComplete() {
753 LOG(INFO) << id() << "OnIceComplete";
754 }
755
756 virtual void OnDataChannel(DataChannelInterface* data_channel) {
757 LOG(INFO) << id() << "OnDataChannel";
758 data_channel_ = data_channel;
759 data_observer_.reset(new MockDataChannelObserver(data_channel));
760 }
761
762 void CreateDataChannel() {
763 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel,
764 NULL);
765 ASSERT_TRUE(data_channel_.get() != NULL);
766 data_observer_.reset(new MockDataChannelObserver(data_channel_));
767 }
768
769 DataChannelInterface* data_channel() { return data_channel_; }
770 const MockDataChannelObserver* data_observer() const {
771 return data_observer_.get();
772 }
773
774 protected:
775 explicit JsepTestClient(const std::string& id)
776 : PeerConnectionTestClientBase<JsepMessageReceiver>(id),
777 remove_msid_(false),
778 remove_bundle_(false),
779 remove_sdes_(false) {
780 }
781
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000782 virtual rtc::scoped_refptr<webrtc::PeerConnectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 CreatePeerConnection(webrtc::PortAllocatorFactoryInterface* factory,
784 const MediaConstraintsInterface* constraints) {
785 // CreatePeerConnection with IceServers.
786 webrtc::PeerConnectionInterface::IceServers ice_servers;
787 webrtc::PeerConnectionInterface::IceServer ice_server;
788 ice_server.uri = "stun:stun.l.google.com:19302";
789 ice_servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000790
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000791 FakeIdentityService* dtls_service =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000792 rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000793 new FakeIdentityService() : NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 return peer_connection_factory()->CreatePeerConnection(
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000795 ice_servers, constraints, factory, dtls_service, this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 }
797
798 void HandleIncomingOffer(const std::string& msg) {
799 LOG(INFO) << id() << "HandleIncomingOffer ";
800 if (NumberOfLocalMediaStreams() == 0) {
801 // If we are not sending any streams ourselves it is time to add some.
802 AddMediaStream(true, true);
803 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000804 rtc::scoped_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 webrtc::CreateSessionDescription("offer", msg, NULL));
806 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000807 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 EXPECT_TRUE(DoCreateAnswer(answer.use()));
809 std::string sdp;
810 EXPECT_TRUE(answer->ToString(&sdp));
811 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
812 if (signaling_message_receiver()) {
813 signaling_message_receiver()->ReceiveSdpMessage(
814 webrtc::SessionDescriptionInterface::kAnswer, sdp);
815 }
816 }
817
818 void HandleIncomingAnswer(const std::string& msg) {
819 LOG(INFO) << id() << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000820 rtc::scoped_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 webrtc::CreateSessionDescription("answer", msg, NULL));
822 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
823 }
824
825 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
826 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000827 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
828 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829 MockCreateSessionDescriptionObserver>());
830 if (offer) {
831 pc()->CreateOffer(observer, &session_description_constraints_);
832 } else {
833 pc()->CreateAnswer(observer, &session_description_constraints_);
834 }
835 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
836 *desc = observer->release_desc();
837 if (observer->result() && ExpectIceRestart()) {
838 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
839 }
840 return observer->result();
841 }
842
843 bool DoCreateOffer(SessionDescriptionInterface** desc) {
844 return DoCreateOfferAnswer(desc, true);
845 }
846
847 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
848 return DoCreateOfferAnswer(desc, false);
849 }
850
851 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000852 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
853 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 MockSetSessionDescriptionObserver>());
855 LOG(INFO) << id() << "SetLocalDescription ";
856 pc()->SetLocalDescription(observer, desc);
857 // Ignore the observer result. If we wait for the result with
858 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
859 // before the offer which is an error.
860 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000861 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 // ProcessMessages waits at least 1ms but processes all messages before
863 // returning. Since this test is synchronous and send messages to the remote
864 // peer whenever a callback is invoked, this can lead to messages being
865 // sent to the remote peer in the wrong order.
866 // TODO(perkj): Find a way to check the result without risking that the
867 // order of sent messages are changed. Ex- by posting all messages that are
868 // sent to the remote peer.
869 return true;
870 }
871
872 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000873 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
874 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 MockSetSessionDescriptionObserver>());
876 LOG(INFO) << id() << "SetRemoteDescription ";
877 pc()->SetRemoteDescription(observer, desc);
878 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
879 return observer->result();
880 }
881
882 // This modifies all received SDP messages before they are processed.
883 void FilterIncomingSdpMessage(std::string* sdp) {
884 if (remove_msid_) {
885 const char kSdpSsrcAttribute[] = "a=ssrc:";
886 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
887 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
888 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
889 }
890 if (remove_bundle_) {
891 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
892 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
893 }
894 if (remove_sdes_) {
895 const char kSdpSdesCryptoAttribute[] = "a=crypto";
896 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
897 }
898 }
899
900 private:
901 webrtc::FakeConstraints session_description_constraints_;
902 bool remove_msid_; // True if MSID should be removed in received SDP.
903 bool remove_bundle_; // True if bundle should be removed in received SDP.
904 bool remove_sdes_; // True if a=crypto should be removed in received SDP.
905
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000906 rtc::scoped_refptr<DataChannelInterface> data_channel_;
907 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908};
909
910template <typename SignalingClass>
911class P2PTestConductor : public testing::Test {
912 public:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000913 P2PTestConductor()
914 : pss_(new rtc::PhysicalSocketServer),
915 ss_(new rtc::VirtualSocketServer(pss_.get())),
916 ss_scope_(ss_.get()) {}
917
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918 bool SessionActive() {
919 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000920 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000922
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 // Return true if the number of frames provided have been received or it is
924 // known that that will never occur (e.g. no frames will be sent or
925 // captured).
926 bool FramesNotPending(int audio_frames_to_receive,
927 int video_frames_to_receive) {
928 return VideoFramesReceivedCheck(video_frames_to_receive) &&
929 AudioFramesReceivedCheck(audio_frames_to_receive);
930 }
931 bool AudioFramesReceivedCheck(int frames_received) {
932 return initiating_client_->AudioFramesReceivedCheck(frames_received) &&
933 receiving_client_->AudioFramesReceivedCheck(frames_received);
934 }
935 bool VideoFramesReceivedCheck(int frames_received) {
936 return initiating_client_->VideoFramesReceivedCheck(frames_received) &&
937 receiving_client_->VideoFramesReceivedCheck(frames_received);
938 }
939 void VerifyDtmf() {
940 initiating_client_->VerifyDtmf();
941 receiving_client_->VerifyDtmf();
942 }
943
944 void TestUpdateOfferWithRejectedContent() {
945 initiating_client_->Negotiate(true, false);
946 EXPECT_TRUE_WAIT(
947 FramesNotPending(kEndAudioFrameCount * 2, kEndVideoFrameCount),
948 kMaxWaitForFramesMs);
949 // There shouldn't be any more video frame after the new offer is
950 // negotiated.
951 EXPECT_FALSE(VideoFramesReceivedCheck(kEndVideoFrameCount + 1));
952 }
953
954 void VerifyRenderedSize(int width, int height) {
955 EXPECT_EQ(width, receiving_client()->rendered_width());
956 EXPECT_EQ(height, receiving_client()->rendered_height());
957 EXPECT_EQ(width, initializing_client()->rendered_width());
958 EXPECT_EQ(height, initializing_client()->rendered_height());
959 }
960
961 void VerifySessionDescriptions() {
962 initiating_client_->VerifyRejectedMediaInSessionDescription();
963 receiving_client_->VerifyRejectedMediaInSessionDescription();
964 initiating_client_->VerifyLocalIceUfragAndPassword();
965 receiving_client_->VerifyLocalIceUfragAndPassword();
966 }
967
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000968 ~P2PTestConductor() {
969 if (initiating_client_) {
970 initiating_client_->set_signaling_message_receiver(NULL);
971 }
972 if (receiving_client_) {
973 receiving_client_->set_signaling_message_receiver(NULL);
974 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975 }
976
977 bool CreateTestClients() {
978 return CreateTestClients(NULL, NULL);
979 }
980
981 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
982 MediaConstraintsInterface* recv_constraints) {
Joachim Bauch04e5b492015-05-29 09:40:39 +0200983 return CreateTestClients(init_constraints, NULL, recv_constraints, NULL);
984 }
985
986 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
987 PeerConnectionFactory::Options* init_options,
988 MediaConstraintsInterface* recv_constraints,
989 PeerConnectionFactory::Options* recv_options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000990 initiating_client_.reset(SignalingClass::CreateClient("Caller: ",
Joachim Bauch04e5b492015-05-29 09:40:39 +0200991 init_constraints,
992 init_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000993 receiving_client_.reset(SignalingClass::CreateClient("Callee: ",
Joachim Bauch04e5b492015-05-29 09:40:39 +0200994 recv_constraints,
995 recv_options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996 if (!initiating_client_ || !receiving_client_) {
997 return false;
998 }
999 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
1000 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1001 return true;
1002 }
1003
1004 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1005 const webrtc::FakeConstraints& recv_constraints) {
1006 initiating_client_->SetVideoConstraints(init_constraints);
1007 receiving_client_->SetVideoConstraints(recv_constraints);
1008 }
1009
1010 void EnableVideoDecoderFactory() {
1011 initiating_client_->EnableVideoDecoderFactory();
1012 receiving_client_->EnableVideoDecoderFactory();
1013 }
1014
1015 // This test sets up a call between two parties. Both parties send static
1016 // frames to each other. Once the test is finished the number of sent frames
1017 // is compared to the number of received frames.
1018 void LocalP2PTest() {
1019 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1020 initiating_client_->AddMediaStream(true, true);
1021 }
1022 initiating_client_->Negotiate();
1023 const int kMaxWaitForActivationMs = 5000;
1024 // Assert true is used here since next tests are guaranteed to fail and
1025 // would eat up 5 seconds.
1026 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1027 VerifySessionDescriptions();
1028
1029
1030 int audio_frame_count = kEndAudioFrameCount;
1031 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1032 if (!initiating_client_->can_receive_audio() ||
1033 !receiving_client_->can_receive_audio()) {
1034 audio_frame_count = -1;
1035 }
1036 int video_frame_count = kEndVideoFrameCount;
1037 if (!initiating_client_->can_receive_video() ||
1038 !receiving_client_->can_receive_video()) {
1039 video_frame_count = -1;
1040 }
1041
1042 if (audio_frame_count != -1 || video_frame_count != -1) {
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001043 // Audio or video is expected to flow, so both clients should reach the
1044 // Connected state, and the offerer (ICE controller) should proceed to
1045 // Completed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 // Note: These tests have been observed to fail under heavy load at
1047 // shorter timeouts, so they may be flaky.
1048 EXPECT_EQ_WAIT(
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001049 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001050 initiating_client_->ice_connection_state(),
1051 kMaxWaitForFramesMs);
1052 EXPECT_EQ_WAIT(
1053 webrtc::PeerConnectionInterface::kIceConnectionConnected,
1054 receiving_client_->ice_connection_state(),
1055 kMaxWaitForFramesMs);
1056 }
1057
1058 if (initiating_client_->can_receive_audio() ||
1059 initiating_client_->can_receive_video()) {
1060 // The initiating client can receive media, so it must produce candidates
1061 // that will serve as destinations for that media.
1062 // TODO(bemasc): Understand why the state is not already Complete here, as
1063 // seems to be the case for the receiving client. This may indicate a bug
1064 // in the ICE gathering system.
1065 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1066 initiating_client_->ice_gathering_state());
1067 }
1068 if (receiving_client_->can_receive_audio() ||
1069 receiving_client_->can_receive_video()) {
1070 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1071 receiving_client_->ice_gathering_state(),
1072 kMaxWaitForFramesMs);
1073 }
1074
1075 EXPECT_TRUE_WAIT(FramesNotPending(audio_frame_count, video_frame_count),
1076 kMaxWaitForFramesMs);
1077 }
1078
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001079 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1080 // Messages may get lost on the unreliable DataChannel, so we send multiple
1081 // times to avoid test flakiness.
1082 static const size_t kSendAttempts = 5;
1083
1084 for (size_t i = 0; i < kSendAttempts; ++i) {
1085 dc->Send(DataBuffer(data));
1086 }
1087 }
1088
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001089 SignalingClass* initializing_client() { return initiating_client_.get(); }
1090 SignalingClass* receiving_client() { return receiving_client_.get(); }
1091
1092 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001093 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1094 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1095 rtc::SocketServerScope ss_scope_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001096 rtc::scoped_ptr<SignalingClass> initiating_client_;
1097 rtc::scoped_ptr<SignalingClass> receiving_client_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098};
1099typedef P2PTestConductor<JsepTestClient> JsepPeerConnectionP2PTestClient;
1100
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001101// Disable for TSan v2, see
1102// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1103#if !defined(THREAD_SANITIZER)
1104
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001105// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001106// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1107// See issue webrtc/2378.
1108TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001109 ASSERT_TRUE(CreateTestClients());
1110 LocalP2PTest();
1111 VerifyDtmf();
1112}
1113
1114// This test sets up a Jsep call between two parties and test that we can get a
1115// video aspect ratio of 16:9.
1116TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTest16To9) {
1117 ASSERT_TRUE(CreateTestClients());
1118 FakeConstraints constraint;
1119 double requested_ratio = 640.0/360;
1120 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1121 SetVideoConstraints(constraint, constraint);
1122 LocalP2PTest();
1123
1124 ASSERT_LE(0, initializing_client()->rendered_height());
1125 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001126 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001127 initializing_client()->rendered_height();
1128 EXPECT_LE(requested_ratio, initiating_video_ratio);
1129
1130 ASSERT_LE(0, receiving_client()->rendered_height());
1131 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001132 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001133 receiving_client()->rendered_height();
1134 EXPECT_LE(requested_ratio, receiving_video_ratio);
1135}
1136
1137// This test sets up a Jsep call between two parties and test that the
1138// received video has a resolution of 1280*720.
1139// TODO(mallinath): Enable when
1140// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
1141TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTest1280By720) {
1142 ASSERT_TRUE(CreateTestClients());
1143 FakeConstraints constraint;
1144 constraint.SetMandatoryMinWidth(1280);
1145 constraint.SetMandatoryMinHeight(720);
1146 SetVideoConstraints(constraint, constraint);
1147 LocalP2PTest();
1148 VerifyRenderedSize(1280, 720);
1149}
1150
1151// This test sets up a call between two endpoints that are configured to use
1152// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
1153TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtls) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001154 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001155 FakeConstraints setup_constraints;
1156 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1157 true);
1158 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1159 LocalP2PTest();
1160 VerifyRenderedSize(640, 480);
1161}
1162
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001163// This test sets up a audio call initially and then upgrades to audio/video,
1164// using DTLS.
mallinath@webrtc.org50bc5532013-10-21 17:58:35 +00001165TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001166 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001167 FakeConstraints setup_constraints;
1168 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1169 true);
1170 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1171 receiving_client()->SetReceiveAudioVideo(true, false);
1172 LocalP2PTest();
1173 receiving_client()->SetReceiveAudioVideo(true, true);
1174 receiving_client()->Negotiate();
1175}
1176
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177// This test sets up a call between two endpoints that are configured to use
1178// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1179// negotiated and used for transport.
1180TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001181 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182 FakeConstraints setup_constraints;
1183 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1184 true);
1185 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1186 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1187 LocalP2PTest();
1188 VerifyRenderedSize(640, 480);
1189}
1190
1191// This test sets up a Jsep call between two parties, and the callee only
1192// accept to receive video.
solenberg@webrtc.org503c3362015-02-12 13:12:50 +00001193TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194 ASSERT_TRUE(CreateTestClients());
1195 receiving_client()->SetReceiveAudioVideo(false, true);
1196 LocalP2PTest();
1197}
1198
1199// This test sets up a Jsep call between two parties, and the callee only
1200// accept to receive audio.
solenberg@webrtc.org503c3362015-02-12 13:12:50 +00001201TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001202 ASSERT_TRUE(CreateTestClients());
1203 receiving_client()->SetReceiveAudioVideo(true, false);
1204 LocalP2PTest();
1205}
1206
1207// This test sets up a Jsep call between two parties, and the callee reject both
1208// audio and video.
1209TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerNone) {
1210 ASSERT_TRUE(CreateTestClients());
1211 receiving_client()->SetReceiveAudioVideo(false, false);
1212 LocalP2PTest();
1213}
1214
1215// This test sets up an audio and video call between two parties. After the call
1216// runs for a while (10 frames), the caller sends an update offer with video
1217// being rejected. Once the re-negotiation is done, the video flow should stop
1218// and the audio flow should continue.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001219// Disabled due to b/14955157.
1220TEST_F(JsepPeerConnectionP2PTestClient,
1221 DISABLED_UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001222 ASSERT_TRUE(CreateTestClients());
1223 LocalP2PTest();
1224 TestUpdateOfferWithRejectedContent();
1225}
1226
1227// This test sets up a Jsep call between two parties. The MSID is removed from
1228// the SDP strings from the caller.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001229// Disabled due to b/14955157.
1230TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231 ASSERT_TRUE(CreateTestClients());
1232 receiving_client()->RemoveMsidFromReceivedSdp(true);
1233 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1234 // audio and video is muxed when MSID is disabled. Remove
1235 // SetRemoveBundleFromSdp once
1236 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1237 receiving_client()->RemoveBundleFromReceivedSdp(true);
1238 LocalP2PTest();
1239}
1240
1241// This test sets up a Jsep call between two parties and the initiating peer
1242// sends two steams.
1243// TODO(perkj): Disabled due to
1244// https://code.google.com/p/webrtc/issues/detail?id=1454
1245TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestTwoStreams) {
1246 ASSERT_TRUE(CreateTestClients());
1247 // Set optional video constraint to max 320pixels to decrease CPU usage.
1248 FakeConstraints constraint;
1249 constraint.SetOptionalMaxWidth(320);
1250 SetVideoConstraints(constraint, constraint);
1251 initializing_client()->AddMediaStream(true, true);
1252 initializing_client()->AddMediaStream(false, true);
1253 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1254 LocalP2PTest();
1255 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1256}
1257
1258// Test that we can receive the audio output level from a remote audio track.
1259TEST_F(JsepPeerConnectionP2PTestClient, GetAudioOutputLevelStats) {
1260 ASSERT_TRUE(CreateTestClients());
1261 LocalP2PTest();
1262
1263 StreamCollectionInterface* remote_streams =
1264 initializing_client()->remote_streams();
1265 ASSERT_GT(remote_streams->count(), 0u);
1266 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1267 MediaStreamTrackInterface* remote_audio_track =
1268 remote_streams->at(0)->GetAudioTracks()[0];
1269
1270 // Get the audio output level stats. Note that the level is not available
1271 // until a RTCP packet has been received.
1272 EXPECT_TRUE_WAIT(
1273 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1274 kMaxWaitForStatsMs);
1275}
1276
1277// Test that an audio input level is reported.
1278TEST_F(JsepPeerConnectionP2PTestClient, GetAudioInputLevelStats) {
1279 ASSERT_TRUE(CreateTestClients());
1280 LocalP2PTest();
1281
1282 // Get the audio input level stats. The level should be available very
1283 // soon after the test starts.
1284 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1285 kMaxWaitForStatsMs);
1286}
1287
1288// Test that we can get incoming byte counts from both audio and video tracks.
1289TEST_F(JsepPeerConnectionP2PTestClient, GetBytesReceivedStats) {
1290 ASSERT_TRUE(CreateTestClients());
1291 LocalP2PTest();
1292
1293 StreamCollectionInterface* remote_streams =
1294 initializing_client()->remote_streams();
1295 ASSERT_GT(remote_streams->count(), 0u);
1296 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1297 MediaStreamTrackInterface* remote_audio_track =
1298 remote_streams->at(0)->GetAudioTracks()[0];
1299 EXPECT_TRUE_WAIT(
1300 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1301 kMaxWaitForStatsMs);
1302
1303 MediaStreamTrackInterface* remote_video_track =
1304 remote_streams->at(0)->GetVideoTracks()[0];
1305 EXPECT_TRUE_WAIT(
1306 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1307 kMaxWaitForStatsMs);
1308}
1309
1310// Test that we can get outgoing byte counts from both audio and video tracks.
1311TEST_F(JsepPeerConnectionP2PTestClient, GetBytesSentStats) {
1312 ASSERT_TRUE(CreateTestClients());
1313 LocalP2PTest();
1314
1315 StreamCollectionInterface* local_streams =
1316 initializing_client()->local_streams();
1317 ASSERT_GT(local_streams->count(), 0u);
1318 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1319 MediaStreamTrackInterface* local_audio_track =
1320 local_streams->at(0)->GetAudioTracks()[0];
1321 EXPECT_TRUE_WAIT(
1322 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1323 kMaxWaitForStatsMs);
1324
1325 MediaStreamTrackInterface* local_video_track =
1326 local_streams->at(0)->GetVideoTracks()[0];
1327 EXPECT_TRUE_WAIT(
1328 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1329 kMaxWaitForStatsMs);
1330}
1331
Joachim Bauch04e5b492015-05-29 09:40:39 +02001332// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
1333TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12None) {
1334 PeerConnectionFactory::Options init_options;
1335 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1336 PeerConnectionFactory::Options recv_options;
1337 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1338 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001339 LocalP2PTest();
1340
Joachim Bauch04e5b492015-05-29 09:40:39 +02001341 EXPECT_EQ_WAIT(
1342 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_10),
1343 initializing_client()->GetDtlsCipherStats(),
1344 kMaxWaitForStatsMs);
1345
1346 EXPECT_EQ_WAIT(
1347 kDefaultSrtpCipher,
1348 initializing_client()->GetSrtpCipherStats(),
1349 kMaxWaitForStatsMs);
1350}
1351
1352// Test that DTLS 1.2 is used if both ends support it.
1353TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Both) {
1354 PeerConnectionFactory::Options init_options;
1355 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1356 PeerConnectionFactory::Options recv_options;
1357 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1358 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
1359 LocalP2PTest();
1360
1361 EXPECT_EQ_WAIT(
1362 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_12),
1363 initializing_client()->GetDtlsCipherStats(),
1364 kMaxWaitForStatsMs);
1365
1366 EXPECT_EQ_WAIT(
1367 kDefaultSrtpCipher,
1368 initializing_client()->GetSrtpCipherStats(),
1369 kMaxWaitForStatsMs);
1370}
1371
1372// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1373// received supports 1.0.
1374TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Init) {
1375 PeerConnectionFactory::Options init_options;
1376 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1377 PeerConnectionFactory::Options recv_options;
1378 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1379 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
1380 LocalP2PTest();
1381
1382 EXPECT_EQ_WAIT(
1383 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_10),
1384 initializing_client()->GetDtlsCipherStats(),
1385 kMaxWaitForStatsMs);
1386
1387 EXPECT_EQ_WAIT(
1388 kDefaultSrtpCipher,
1389 initializing_client()->GetSrtpCipherStats(),
1390 kMaxWaitForStatsMs);
1391}
1392
1393// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1394// received supports 1.2.
1395TEST_F(JsepPeerConnectionP2PTestClient, GetDtls12Recv) {
1396 PeerConnectionFactory::Options init_options;
1397 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1398 PeerConnectionFactory::Options recv_options;
1399 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1400 ASSERT_TRUE(CreateTestClients(NULL, &init_options, NULL, &recv_options));
1401 LocalP2PTest();
1402
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001403 EXPECT_EQ_WAIT(
Joachim Bauch831c5582015-05-20 12:48:41 +02001404 rtc::SSLStreamAdapter::GetDefaultSslCipher(rtc::SSL_PROTOCOL_DTLS_10),
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001405 initializing_client()->GetDtlsCipherStats(),
1406 kMaxWaitForStatsMs);
1407
1408 EXPECT_EQ_WAIT(
1409 kDefaultSrtpCipher,
1410 initializing_client()->GetSrtpCipherStats(),
1411 kMaxWaitForStatsMs);
1412}
1413
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001414// This test sets up a call between two parties with audio, video and data.
1415TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDataChannel) {
1416 FakeConstraints setup_constraints;
1417 setup_constraints.SetAllowRtpDataChannels();
1418 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1419 initializing_client()->CreateDataChannel();
1420 LocalP2PTest();
1421 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1422 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1423 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1424 kMaxWaitMs);
1425 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1426 kMaxWaitMs);
1427
1428 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001429
1430 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001431 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1432 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001433
1434 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001435 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1436 kMaxWaitMs);
1437
1438 receiving_client()->data_channel()->Close();
1439 // Send new offer and answer.
1440 receiving_client()->Negotiate();
1441 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1442 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1443}
1444
1445// This test sets up a call between two parties and creates a data channel.
1446// The test tests that received data is buffered unless an observer has been
1447// registered.
1448// Rtp data channels can receive data before the underlying
1449// transport has detected that a channel is writable and thus data can be
1450// received before the data channel state changes to open. That is hard to test
1451// but the same buffering is used in that case.
1452TEST_F(JsepPeerConnectionP2PTestClient, RegisterDataChannelObserver) {
1453 FakeConstraints setup_constraints;
1454 setup_constraints.SetAllowRtpDataChannels();
1455 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1456 initializing_client()->CreateDataChannel();
1457 initializing_client()->Negotiate();
1458
1459 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1460 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1461 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1462 kMaxWaitMs);
1463 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1464 receiving_client()->data_channel()->state(), kMaxWaitMs);
1465
1466 // Unregister the existing observer.
1467 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001468
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001469 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001470 SendRtpData(initializing_client()->data_channel(), data);
1471
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001472 // Wait a while to allow the sent data to arrive before an observer is
1473 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001474 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475
1476 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1477 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1478}
1479
1480// This test sets up a call between two parties with audio, video and but only
1481// the initiating client support data.
1482TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001483 FakeConstraints setup_constraints_1;
1484 setup_constraints_1.SetAllowRtpDataChannels();
1485 // Must disable DTLS to make negotiation succeed.
1486 setup_constraints_1.SetMandatory(
1487 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1488 FakeConstraints setup_constraints_2;
1489 setup_constraints_2.SetMandatory(
1490 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1491 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001492 initializing_client()->CreateDataChannel();
1493 LocalP2PTest();
1494 EXPECT_TRUE(initializing_client()->data_channel() != NULL);
1495 EXPECT_FALSE(receiving_client()->data_channel());
1496 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1497}
1498
1499// This test sets up a call between two parties with audio, video. When audio
1500// and video is setup and flowing and data channel is negotiated.
1501TEST_F(JsepPeerConnectionP2PTestClient, AddDataChannelAfterRenegotiation) {
1502 FakeConstraints setup_constraints;
1503 setup_constraints.SetAllowRtpDataChannels();
1504 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1505 LocalP2PTest();
1506 initializing_client()->CreateDataChannel();
1507 // Send new offer and answer.
1508 initializing_client()->Negotiate();
1509 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1510 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1511 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1512 kMaxWaitMs);
1513 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1514 kMaxWaitMs);
1515}
1516
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001517// This test sets up a Jsep call with SCTP DataChannel and verifies the
1518// negotiation is completed without error.
1519#ifdef HAVE_SCTP
1520TEST_F(JsepPeerConnectionP2PTestClient, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001521 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001522 FakeConstraints constraints;
1523 constraints.SetMandatory(
1524 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1525 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1526 initializing_client()->CreateDataChannel();
1527 initializing_client()->Negotiate(false, false);
1528}
1529#endif
1530
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001531// This test sets up a call between two parties with audio, and video.
1532// During the call, the initializing side restart ice and the test verifies that
1533// new ice candidates are generated and audio and video still can flow.
1534TEST_F(JsepPeerConnectionP2PTestClient, IceRestart) {
1535 ASSERT_TRUE(CreateTestClients());
1536
1537 // Negotiate and wait for ice completion and make sure audio and video plays.
1538 LocalP2PTest();
1539
1540 // Create a SDP string of the first audio candidate for both clients.
1541 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1542 initializing_client()->pc()->local_description()->candidates(0);
1543 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1544 receiving_client()->pc()->local_description()->candidates(0);
1545 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1546 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1547 std::string initiator_candidate;
1548 EXPECT_TRUE(
1549 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1550 std::string receiver_candidate;
1551 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1552
1553 // Restart ice on the initializing client.
1554 receiving_client()->SetExpectIceRestart(true);
1555 initializing_client()->IceRestart();
1556
1557 // Negotiate and wait for ice completion again and make sure audio and video
1558 // plays.
1559 LocalP2PTest();
1560
1561 // Create a SDP string of the first audio candidate for both clients again.
1562 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1563 initializing_client()->pc()->local_description()->candidates(0);
1564 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1565 receiving_client()->pc()->local_description()->candidates(0);
1566 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1567 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1568 std::string initiator_candidate_restart;
1569 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1570 &initiator_candidate_restart));
1571 std::string receiver_candidate_restart;
1572 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1573 &receiver_candidate_restart));
1574
1575 // Verify that the first candidates in the local session descriptions has
1576 // changed.
1577 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1578 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1579}
1580
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581// This test sets up a Jsep call between two parties with external
1582// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001583// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1584// See issue webrtc/2378.
1585TEST_F(JsepPeerConnectionP2PTestClient,
1586 DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587 ASSERT_TRUE(CreateTestClients());
1588 EnableVideoDecoderFactory();
1589 LocalP2PTest();
1590}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001591
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001592#endif // if !defined(THREAD_SANITIZER)