blob: 8a098b423ca43027485c749c20f961971d453e1a [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2012, Google Inc.
4 *
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"
49#include "talk/p2p/base/constants.h"
50#include "talk/p2p/base/sessiondescription.h"
51#include "talk/session/media/mediasession.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000052#include "webrtc/base/gunit.h"
53#include "webrtc/base/scoped_ptr.h"
54#include "webrtc/base/ssladapter.h"
55#include "webrtc/base/sslstreamadapter.h"
56#include "webrtc/base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057
58#define MAYBE_SKIP_TEST(feature) \
59 if (!(feature())) { \
60 LOG(LS_INFO) << "Feature disabled... skipping"; \
61 return; \
62 }
63
64using cricket::ContentInfo;
65using cricket::FakeWebRtcVideoDecoder;
66using cricket::FakeWebRtcVideoDecoderFactory;
67using cricket::FakeWebRtcVideoEncoder;
68using cricket::FakeWebRtcVideoEncoderFactory;
69using cricket::MediaContentDescription;
70using webrtc::DataBuffer;
71using webrtc::DataChannelInterface;
72using webrtc::DtmfSender;
73using webrtc::DtmfSenderInterface;
74using webrtc::DtmfSenderObserverInterface;
75using webrtc::FakeConstraints;
76using webrtc::MediaConstraintsInterface;
77using webrtc::MediaStreamTrackInterface;
78using webrtc::MockCreateSessionDescriptionObserver;
79using webrtc::MockDataChannelObserver;
80using webrtc::MockSetSessionDescriptionObserver;
81using webrtc::MockStatsObserver;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000082using webrtc::PeerConnectionInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083using webrtc::SessionDescriptionInterface;
84using webrtc::StreamCollectionInterface;
85
jiayl@webrtc.org8f88f202014-04-16 17:14:21 +000086static const int kMaxWaitMs = 2000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000087// Disable for TSan v2, see
88// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
89// This declaration is also #ifdef'd as it causes uninitialized-variable
90// warnings.
91#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092static const int kMaxWaitForStatsMs = 3000;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +000093static const int kMaxWaitForRembMs = 5000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000094#endif
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000095static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096static const int kEndAudioFrameCount = 3;
97static const int kEndVideoFrameCount = 3;
98
99static const char kStreamLabelBase[] = "stream_label";
100static const char kVideoTrackLabelBase[] = "video_track";
101static const char kAudioTrackLabelBase[] = "audio_track";
102static const char kDataChannelLabel[] = "data_channel";
103
104static void RemoveLinesFromSdp(const std::string& line_start,
105 std::string* sdp) {
106 const char kSdpLineEnd[] = "\r\n";
107 size_t ssrc_pos = 0;
108 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
109 std::string::npos) {
110 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
111 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
112 }
113}
114
115class SignalingMessageReceiver {
116 public:
117 protected:
118 SignalingMessageReceiver() {}
119 virtual ~SignalingMessageReceiver() {}
120};
121
122class JsepMessageReceiver : public SignalingMessageReceiver {
123 public:
124 virtual void ReceiveSdpMessage(const std::string& type,
125 std::string& msg) = 0;
126 virtual void ReceiveIceMessage(const std::string& sdp_mid,
127 int sdp_mline_index,
128 const std::string& msg) = 0;
129
130 protected:
131 JsepMessageReceiver() {}
132 virtual ~JsepMessageReceiver() {}
133};
134
135template <typename MessageReceiver>
136class PeerConnectionTestClientBase
137 : public webrtc::PeerConnectionObserver,
138 public MessageReceiver {
139 public:
140 ~PeerConnectionTestClientBase() {
141 while (!fake_video_renderers_.empty()) {
142 RenderMap::iterator it = fake_video_renderers_.begin();
143 delete it->second;
144 fake_video_renderers_.erase(it);
145 }
146 }
147
148 virtual void Negotiate() = 0;
149
150 virtual void Negotiate(bool audio, bool video) = 0;
151
152 virtual void SetVideoConstraints(
153 const webrtc::FakeConstraints& video_constraint) {
154 video_constraints_ = video_constraint;
155 }
156
157 void AddMediaStream(bool audio, bool video) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000158 std::string stream_label = kStreamLabelBase +
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000159 rtc::ToString<int>(
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000160 static_cast<int>(peer_connection_->local_streams()->count()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000161 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000162 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163
164 if (audio && can_receive_audio()) {
165 FakeConstraints constraints;
166 // Disable highpass filter so that we can get all the test audio frames.
167 constraints.AddMandatory(
168 MediaConstraintsInterface::kHighpassFilter, false);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000169 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
wu@webrtc.org97077a32013-10-25 21:18:33 +0000170 peer_connection_factory_->CreateAudioSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 // TODO(perkj): Test audio source when it is implemented. Currently audio
172 // always use the default input.
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000173 std::string label = stream_label + kAudioTrackLabelBase;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000174 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000175 peer_connection_factory_->CreateAudioTrack(label, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 stream->AddTrack(audio_track);
177 }
178 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000179 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 }
181
182 EXPECT_TRUE(peer_connection_->AddStream(stream, NULL));
183 }
184
185 size_t NumberOfLocalMediaStreams() {
186 return peer_connection_->local_streams()->count();
187 }
188
189 bool SessionActive() {
190 return peer_connection_->signaling_state() ==
191 webrtc::PeerConnectionInterface::kStable;
192 }
193
194 void set_signaling_message_receiver(
195 MessageReceiver* signaling_message_receiver) {
196 signaling_message_receiver_ = signaling_message_receiver;
197 }
198
199 void EnableVideoDecoderFactory() {
200 video_decoder_factory_enabled_ = true;
201 fake_video_decoder_factory_->AddSupportedVideoCodecType(
202 webrtc::kVideoCodecVP8);
203 }
204
205 bool AudioFramesReceivedCheck(int number_of_frames) const {
206 return number_of_frames <= fake_audio_capture_module_->frames_received();
207 }
208
209 bool VideoFramesReceivedCheck(int number_of_frames) {
210 if (video_decoder_factory_enabled_) {
211 const std::vector<FakeWebRtcVideoDecoder*>& decoders
212 = fake_video_decoder_factory_->decoders();
213 if (decoders.empty()) {
214 return number_of_frames <= 0;
215 }
216
217 for (std::vector<FakeWebRtcVideoDecoder*>::const_iterator
218 it = decoders.begin(); it != decoders.end(); ++it) {
219 if (number_of_frames > (*it)->GetNumFramesReceived()) {
220 return false;
221 }
222 }
223 return true;
224 } else {
225 if (fake_video_renderers_.empty()) {
226 return number_of_frames <= 0;
227 }
228
229 for (RenderMap::const_iterator it = fake_video_renderers_.begin();
230 it != fake_video_renderers_.end(); ++it) {
231 if (number_of_frames > it->second->num_rendered_frames()) {
232 return false;
233 }
234 }
235 return true;
236 }
237 }
238 // Verify the CreateDtmfSender interface
239 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000240 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
241 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242
243 // We can't create a DTMF sender with an invalid audio track or a non local
244 // track.
245 EXPECT_TRUE(peer_connection_->CreateDtmfSender(NULL) == NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000246 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247 peer_connection_factory_->CreateAudioTrack("dummy_track",
248 NULL));
249 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == NULL);
250
251 // We should be able to create a DTMF sender from a local track.
252 webrtc::AudioTrackInterface* localtrack =
253 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
254 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
255 EXPECT_TRUE(dtmf_sender.get() != NULL);
256 dtmf_sender->RegisterObserver(observer.get());
257
258 // Test the DtmfSender object just created.
259 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
260 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
261
262 // We don't need to verify that the DTMF tones are actually sent out because
263 // that is already covered by the tests of the lower level components.
264
265 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
266 std::vector<std::string> tones;
267 tones.push_back("1");
268 tones.push_back("a");
269 tones.push_back("");
270 observer->Verify(tones);
271
272 dtmf_sender->UnregisterObserver();
273 }
274
275 // Verifies that the SessionDescription have rejected the appropriate media
276 // content.
277 void VerifyRejectedMediaInSessionDescription() {
278 ASSERT_TRUE(peer_connection_->remote_description() != NULL);
279 ASSERT_TRUE(peer_connection_->local_description() != NULL);
280 const cricket::SessionDescription* remote_desc =
281 peer_connection_->remote_description()->description();
282 const cricket::SessionDescription* local_desc =
283 peer_connection_->local_description()->description();
284
285 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
286 if (remote_audio_content) {
287 const ContentInfo* audio_content =
288 GetFirstAudioContent(local_desc);
289 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
290 }
291
292 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
293 if (remote_video_content) {
294 const ContentInfo* video_content =
295 GetFirstVideoContent(local_desc);
296 EXPECT_EQ(can_receive_video(), !video_content->rejected);
297 }
298 }
299
300 void SetExpectIceRestart(bool expect_restart) {
301 expect_ice_restart_ = expect_restart;
302 }
303
304 bool ExpectIceRestart() const { return expect_ice_restart_; }
305
306 void VerifyLocalIceUfragAndPassword() {
307 ASSERT_TRUE(peer_connection_->local_description() != NULL);
308 const cricket::SessionDescription* desc =
309 peer_connection_->local_description()->description();
310 const cricket::ContentInfos& contents = desc->contents();
311
312 for (size_t index = 0; index < contents.size(); ++index) {
313 if (contents[index].rejected)
314 continue;
315 const cricket::TransportDescription* transport_desc =
316 desc->GetTransportDescriptionByName(contents[index].name);
317
318 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000319 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 if (ufragpair_it == ice_ufrag_pwd_.end()) {
321 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000322 ice_ufrag_pwd_[static_cast<int>(index)] =
323 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324 } else if (ExpectIceRestart()) {
325 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
326 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
327 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
328 } else {
329 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
330 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
331 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
332 }
333 }
334 }
335
336 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000337 rtc::scoped_refptr<MockStatsObserver>
338 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000339 EXPECT_TRUE(peer_connection_->GetStats(
340 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
342 return observer->AudioOutputLevel();
343 }
344
345 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000346 rtc::scoped_refptr<MockStatsObserver>
347 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000348 EXPECT_TRUE(peer_connection_->GetStats(
349 observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
351 return observer->AudioInputLevel();
352 }
353
354 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000355 rtc::scoped_refptr<MockStatsObserver>
356 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000357 EXPECT_TRUE(peer_connection_->GetStats(
358 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
360 return observer->BytesReceived();
361 }
362
363 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000364 rtc::scoped_refptr<MockStatsObserver>
365 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000366 EXPECT_TRUE(peer_connection_->GetStats(
367 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
369 return observer->BytesSent();
370 }
371
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000372 int GetAvailableReceivedBandwidthStats() {
373 rtc::scoped_refptr<MockStatsObserver>
374 observer(new rtc::RefCountedObject<MockStatsObserver>());
375 EXPECT_TRUE(peer_connection_->GetStats(
376 observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
377 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
378 int bw = observer->AvailableReceiveBandwidth();
379 LOG(INFO) << "Available Receive Bandwidth: " << bw;
380 return bw;
381 }
382
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000383 int rendered_width() {
384 EXPECT_FALSE(fake_video_renderers_.empty());
385 return fake_video_renderers_.empty() ? 1 :
386 fake_video_renderers_.begin()->second->width();
387 }
388
389 int rendered_height() {
390 EXPECT_FALSE(fake_video_renderers_.empty());
391 return fake_video_renderers_.empty() ? 1 :
392 fake_video_renderers_.begin()->second->height();
393 }
394
395 size_t number_of_remote_streams() {
396 if (!pc())
397 return 0;
398 return pc()->remote_streams()->count();
399 }
400
401 StreamCollectionInterface* remote_streams() {
402 if (!pc()) {
403 ADD_FAILURE();
404 return NULL;
405 }
406 return pc()->remote_streams();
407 }
408
409 StreamCollectionInterface* local_streams() {
410 if (!pc()) {
411 ADD_FAILURE();
412 return NULL;
413 }
414 return pc()->local_streams();
415 }
416
417 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
418 return pc()->signaling_state();
419 }
420
421 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
422 return pc()->ice_connection_state();
423 }
424
425 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
426 return pc()->ice_gathering_state();
427 }
428
429 // PeerConnectionObserver callbacks.
430 virtual void OnError() {}
431 virtual void OnMessage(const std::string&) {}
432 virtual void OnSignalingMessage(const std::string& /*msg*/) {}
433 virtual void OnSignalingChange(
434 webrtc::PeerConnectionInterface::SignalingState new_state) {
435 EXPECT_EQ(peer_connection_->signaling_state(), new_state);
436 }
437 virtual void OnAddStream(webrtc::MediaStreamInterface* media_stream) {
438 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
439 const std::string id = media_stream->GetVideoTracks()[i]->id();
440 ASSERT_TRUE(fake_video_renderers_.find(id) ==
441 fake_video_renderers_.end());
442 fake_video_renderers_[id] = new webrtc::FakeVideoTrackRenderer(
443 media_stream->GetVideoTracks()[i]);
444 }
445 }
446 virtual void OnRemoveStream(webrtc::MediaStreamInterface* media_stream) {}
447 virtual void OnRenegotiationNeeded() {}
448 virtual void OnIceConnectionChange(
449 webrtc::PeerConnectionInterface::IceConnectionState new_state) {
450 EXPECT_EQ(peer_connection_->ice_connection_state(), new_state);
451 }
452 virtual void OnIceGatheringChange(
453 webrtc::PeerConnectionInterface::IceGatheringState new_state) {
454 EXPECT_EQ(peer_connection_->ice_gathering_state(), new_state);
455 }
456 virtual void OnIceCandidate(
457 const webrtc::IceCandidateInterface* /*candidate*/) {}
458
459 webrtc::PeerConnectionInterface* pc() {
460 return peer_connection_.get();
461 }
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000462 void StopVideoCapturers() {
463 for (std::vector<cricket::VideoCapturer*>::iterator it =
464 video_capturers_.begin(); it != video_capturers_.end(); ++it) {
465 (*it)->Stop();
466 }
467 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000468
469 protected:
470 explicit PeerConnectionTestClientBase(const std::string& id)
471 : id_(id),
472 expect_ice_restart_(false),
473 fake_video_decoder_factory_(NULL),
474 fake_video_encoder_factory_(NULL),
475 video_decoder_factory_enabled_(false),
476 signaling_message_receiver_(NULL) {
477 }
478 bool Init(const MediaConstraintsInterface* constraints) {
479 EXPECT_TRUE(!peer_connection_);
480 EXPECT_TRUE(!peer_connection_factory_);
481 allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
482 if (!allocator_factory_) {
483 return false;
484 }
485 audio_thread_.Start();
486 fake_audio_capture_module_ = FakeAudioCaptureModule::Create(
487 &audio_thread_);
488
489 if (fake_audio_capture_module_ == NULL) {
490 return false;
491 }
492 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
493 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
494 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000495 rtc::Thread::Current(), rtc::Thread::Current(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496 fake_audio_capture_module_, fake_video_encoder_factory_,
497 fake_video_decoder_factory_);
498 if (!peer_connection_factory_) {
499 return false;
500 }
501 peer_connection_ = CreatePeerConnection(allocator_factory_.get(),
502 constraints);
503 return peer_connection_.get() != NULL;
504 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000505 virtual rtc::scoped_refptr<webrtc::PeerConnectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506 CreatePeerConnection(webrtc::PortAllocatorFactoryInterface* factory,
507 const MediaConstraintsInterface* constraints) = 0;
508 MessageReceiver* signaling_message_receiver() {
509 return signaling_message_receiver_;
510 }
511 webrtc::PeerConnectionFactoryInterface* peer_connection_factory() {
512 return peer_connection_factory_.get();
513 }
514
515 virtual bool can_receive_audio() = 0;
516 virtual bool can_receive_video() = 0;
517 const std::string& id() const { return id_; }
518
519 private:
520 class DummyDtmfObserver : public DtmfSenderObserverInterface {
521 public:
522 DummyDtmfObserver() : completed_(false) {}
523
524 // Implements DtmfSenderObserverInterface.
525 void OnToneChange(const std::string& tone) {
526 tones_.push_back(tone);
527 if (tone.empty()) {
528 completed_ = true;
529 }
530 }
531
532 void Verify(const std::vector<std::string>& tones) const {
533 ASSERT_TRUE(tones_.size() == tones.size());
534 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
535 }
536
537 bool completed() const { return completed_; }
538
539 private:
540 bool completed_;
541 std::vector<std::string> tones_;
542 };
543
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000544 rtc::scoped_refptr<webrtc::VideoTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545 CreateLocalVideoTrack(const std::string stream_label) {
546 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
547 FakeConstraints source_constraints = video_constraints_;
548 source_constraints.SetMandatoryMaxFrameRate(10);
549
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000550 cricket::FakeVideoCapturer* fake_capturer =
551 new webrtc::FakePeriodicVideoCapturer();
552 video_capturers_.push_back(fake_capturer);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000553 rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 peer_connection_factory_->CreateVideoSource(
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000555 fake_capturer, &source_constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556 std::string label = stream_label + kVideoTrackLabelBase;
557 return peer_connection_factory_->CreateVideoTrack(label, source);
558 }
559
560 std::string id_;
561 // Separate thread for executing |fake_audio_capture_module_| tasks. Audio
562 // processing must not be performed on the same thread as signaling due to
563 // signaling time constraints and relative complexity of the audio pipeline.
564 // This is consistent with the video pipeline that us a a separate thread for
565 // encoding and decoding.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000566 rtc::Thread audio_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000568 rtc::scoped_refptr<webrtc::PortAllocatorFactoryInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 allocator_factory_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000570 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
571 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 peer_connection_factory_;
573
574 typedef std::pair<std::string, std::string> IceUfragPwdPair;
575 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
576 bool expect_ice_restart_;
577
578 // Needed to keep track of number of frames send.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000579 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580 // Needed to keep track of number of frames received.
581 typedef std::map<std::string, webrtc::FakeVideoTrackRenderer*> RenderMap;
582 RenderMap fake_video_renderers_;
583 // Needed to keep track of number of frames received when external decoder
584 // used.
585 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_;
586 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_;
587 bool video_decoder_factory_enabled_;
588 webrtc::FakeConstraints video_constraints_;
589
590 // For remote peer communication.
591 MessageReceiver* signaling_message_receiver_;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000592
593 // Store references to the video capturers we've created, so that we can stop
594 // them, if required.
595 std::vector<cricket::VideoCapturer*> video_capturers_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596};
597
598class JsepTestClient
599 : public PeerConnectionTestClientBase<JsepMessageReceiver> {
600 public:
601 static JsepTestClient* CreateClient(
602 const std::string& id,
603 const MediaConstraintsInterface* constraints) {
604 JsepTestClient* client(new JsepTestClient(id));
605 if (!client->Init(constraints)) {
606 delete client;
607 return NULL;
608 }
609 return client;
610 }
611 ~JsepTestClient() {}
612
613 virtual void Negotiate() {
614 Negotiate(true, true);
615 }
616 virtual void Negotiate(bool audio, bool video) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000617 rtc::scoped_ptr<SessionDescriptionInterface> offer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 EXPECT_TRUE(DoCreateOffer(offer.use()));
619
620 if (offer->description()->GetContentByName("audio")) {
621 offer->description()->GetContentByName("audio")->rejected = !audio;
622 }
623 if (offer->description()->GetContentByName("video")) {
624 offer->description()->GetContentByName("video")->rejected = !video;
625 }
626
627 std::string sdp;
628 EXPECT_TRUE(offer->ToString(&sdp));
629 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
630 signaling_message_receiver()->ReceiveSdpMessage(
631 webrtc::SessionDescriptionInterface::kOffer, sdp);
632 }
633 // JsepMessageReceiver callback.
634 virtual void ReceiveSdpMessage(const std::string& type,
635 std::string& msg) {
636 FilterIncomingSdpMessage(&msg);
637 if (type == webrtc::SessionDescriptionInterface::kOffer) {
638 HandleIncomingOffer(msg);
639 } else {
640 HandleIncomingAnswer(msg);
641 }
642 }
643 // JsepMessageReceiver callback.
644 virtual void ReceiveIceMessage(const std::string& sdp_mid,
645 int sdp_mline_index,
646 const std::string& msg) {
647 LOG(INFO) << id() << "ReceiveIceMessage";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000648 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, NULL));
650 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
651 }
652 // Implements PeerConnectionObserver functions needed by Jsep.
653 virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
654 LOG(INFO) << id() << "OnIceCandidate";
655
656 std::string ice_sdp;
657 EXPECT_TRUE(candidate->ToString(&ice_sdp));
658 if (signaling_message_receiver() == NULL) {
659 // Remote party may be deleted.
660 return;
661 }
662 signaling_message_receiver()->ReceiveIceMessage(candidate->sdp_mid(),
663 candidate->sdp_mline_index(), ice_sdp);
664 }
665
666 void IceRestart() {
667 session_description_constraints_.SetMandatoryIceRestart(true);
668 SetExpectIceRestart(true);
669 }
670
671 void SetReceiveAudioVideo(bool audio, bool video) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000672 SetReceiveAudio(audio);
673 SetReceiveVideo(video);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 ASSERT_EQ(audio, can_receive_audio());
675 ASSERT_EQ(video, can_receive_video());
676 }
677
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000678 void SetReceiveAudio(bool audio) {
679 if (audio && can_receive_audio())
680 return;
681 session_description_constraints_.SetMandatoryReceiveAudio(audio);
682 }
683
684 void SetReceiveVideo(bool video) {
685 if (video && can_receive_video())
686 return;
687 session_description_constraints_.SetMandatoryReceiveVideo(video);
688 }
689
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690 void RemoveMsidFromReceivedSdp(bool remove) {
691 remove_msid_ = remove;
692 }
693
694 void RemoveSdesCryptoFromReceivedSdp(bool remove) {
695 remove_sdes_ = remove;
696 }
697
698 void RemoveBundleFromReceivedSdp(bool remove) {
699 remove_bundle_ = remove;
700 }
701
702 virtual bool can_receive_audio() {
703 bool value;
704 if (webrtc::FindConstraint(&session_description_constraints_,
705 MediaConstraintsInterface::kOfferToReceiveAudio, &value, NULL)) {
706 return value;
707 }
708 return true;
709 }
710
711 virtual bool can_receive_video() {
712 bool value;
713 if (webrtc::FindConstraint(&session_description_constraints_,
714 MediaConstraintsInterface::kOfferToReceiveVideo, &value, NULL)) {
715 return value;
716 }
717 return true;
718 }
719
720 virtual void OnIceComplete() {
721 LOG(INFO) << id() << "OnIceComplete";
722 }
723
724 virtual void OnDataChannel(DataChannelInterface* data_channel) {
725 LOG(INFO) << id() << "OnDataChannel";
726 data_channel_ = data_channel;
727 data_observer_.reset(new MockDataChannelObserver(data_channel));
728 }
729
730 void CreateDataChannel() {
731 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel,
732 NULL);
733 ASSERT_TRUE(data_channel_.get() != NULL);
734 data_observer_.reset(new MockDataChannelObserver(data_channel_));
735 }
736
737 DataChannelInterface* data_channel() { return data_channel_; }
738 const MockDataChannelObserver* data_observer() const {
739 return data_observer_.get();
740 }
741
742 protected:
743 explicit JsepTestClient(const std::string& id)
744 : PeerConnectionTestClientBase<JsepMessageReceiver>(id),
745 remove_msid_(false),
746 remove_bundle_(false),
747 remove_sdes_(false) {
748 }
749
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000750 virtual rtc::scoped_refptr<webrtc::PeerConnectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751 CreatePeerConnection(webrtc::PortAllocatorFactoryInterface* factory,
752 const MediaConstraintsInterface* constraints) {
753 // CreatePeerConnection with IceServers.
754 webrtc::PeerConnectionInterface::IceServers ice_servers;
755 webrtc::PeerConnectionInterface::IceServer ice_server;
756 ice_server.uri = "stun:stun.l.google.com:19302";
757 ice_servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000758
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000759 FakeIdentityService* dtls_service =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000760 rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000761 new FakeIdentityService() : NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762 return peer_connection_factory()->CreatePeerConnection(
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000763 ice_servers, constraints, factory, dtls_service, this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 }
765
766 void HandleIncomingOffer(const std::string& msg) {
767 LOG(INFO) << id() << "HandleIncomingOffer ";
768 if (NumberOfLocalMediaStreams() == 0) {
769 // If we are not sending any streams ourselves it is time to add some.
770 AddMediaStream(true, true);
771 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000772 rtc::scoped_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 webrtc::CreateSessionDescription("offer", msg, NULL));
774 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000775 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 EXPECT_TRUE(DoCreateAnswer(answer.use()));
777 std::string sdp;
778 EXPECT_TRUE(answer->ToString(&sdp));
779 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
780 if (signaling_message_receiver()) {
781 signaling_message_receiver()->ReceiveSdpMessage(
782 webrtc::SessionDescriptionInterface::kAnswer, sdp);
783 }
784 }
785
786 void HandleIncomingAnswer(const std::string& msg) {
787 LOG(INFO) << id() << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000788 rtc::scoped_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 webrtc::CreateSessionDescription("answer", msg, NULL));
790 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
791 }
792
793 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
794 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000795 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
796 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 MockCreateSessionDescriptionObserver>());
798 if (offer) {
799 pc()->CreateOffer(observer, &session_description_constraints_);
800 } else {
801 pc()->CreateAnswer(observer, &session_description_constraints_);
802 }
803 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
804 *desc = observer->release_desc();
805 if (observer->result() && ExpectIceRestart()) {
806 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
807 }
808 return observer->result();
809 }
810
811 bool DoCreateOffer(SessionDescriptionInterface** desc) {
812 return DoCreateOfferAnswer(desc, true);
813 }
814
815 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
816 return DoCreateOfferAnswer(desc, false);
817 }
818
819 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000820 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
821 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 MockSetSessionDescriptionObserver>());
823 LOG(INFO) << id() << "SetLocalDescription ";
824 pc()->SetLocalDescription(observer, desc);
825 // Ignore the observer result. If we wait for the result with
826 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
827 // before the offer which is an error.
828 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000829 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 // ProcessMessages waits at least 1ms but processes all messages before
831 // returning. Since this test is synchronous and send messages to the remote
832 // peer whenever a callback is invoked, this can lead to messages being
833 // sent to the remote peer in the wrong order.
834 // TODO(perkj): Find a way to check the result without risking that the
835 // order of sent messages are changed. Ex- by posting all messages that are
836 // sent to the remote peer.
837 return true;
838 }
839
840 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000841 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
842 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 MockSetSessionDescriptionObserver>());
844 LOG(INFO) << id() << "SetRemoteDescription ";
845 pc()->SetRemoteDescription(observer, desc);
846 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
847 return observer->result();
848 }
849
850 // This modifies all received SDP messages before they are processed.
851 void FilterIncomingSdpMessage(std::string* sdp) {
852 if (remove_msid_) {
853 const char kSdpSsrcAttribute[] = "a=ssrc:";
854 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
855 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
856 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
857 }
858 if (remove_bundle_) {
859 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
860 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
861 }
862 if (remove_sdes_) {
863 const char kSdpSdesCryptoAttribute[] = "a=crypto";
864 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
865 }
866 }
867
868 private:
869 webrtc::FakeConstraints session_description_constraints_;
870 bool remove_msid_; // True if MSID should be removed in received SDP.
871 bool remove_bundle_; // True if bundle should be removed in received SDP.
872 bool remove_sdes_; // True if a=crypto should be removed in received SDP.
873
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000874 rtc::scoped_refptr<DataChannelInterface> data_channel_;
875 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876};
877
878template <typename SignalingClass>
879class P2PTestConductor : public testing::Test {
880 public:
881 bool SessionActive() {
882 return initiating_client_->SessionActive() &&
883 receiving_client_->SessionActive();
884 }
885 // Return true if the number of frames provided have been received or it is
886 // known that that will never occur (e.g. no frames will be sent or
887 // captured).
888 bool FramesNotPending(int audio_frames_to_receive,
889 int video_frames_to_receive) {
890 return VideoFramesReceivedCheck(video_frames_to_receive) &&
891 AudioFramesReceivedCheck(audio_frames_to_receive);
892 }
893 bool AudioFramesReceivedCheck(int frames_received) {
894 return initiating_client_->AudioFramesReceivedCheck(frames_received) &&
895 receiving_client_->AudioFramesReceivedCheck(frames_received);
896 }
897 bool VideoFramesReceivedCheck(int frames_received) {
898 return initiating_client_->VideoFramesReceivedCheck(frames_received) &&
899 receiving_client_->VideoFramesReceivedCheck(frames_received);
900 }
901 void VerifyDtmf() {
902 initiating_client_->VerifyDtmf();
903 receiving_client_->VerifyDtmf();
904 }
905
906 void TestUpdateOfferWithRejectedContent() {
907 initiating_client_->Negotiate(true, false);
908 EXPECT_TRUE_WAIT(
909 FramesNotPending(kEndAudioFrameCount * 2, kEndVideoFrameCount),
910 kMaxWaitForFramesMs);
911 // There shouldn't be any more video frame after the new offer is
912 // negotiated.
913 EXPECT_FALSE(VideoFramesReceivedCheck(kEndVideoFrameCount + 1));
914 }
915
916 void VerifyRenderedSize(int width, int height) {
917 EXPECT_EQ(width, receiving_client()->rendered_width());
918 EXPECT_EQ(height, receiving_client()->rendered_height());
919 EXPECT_EQ(width, initializing_client()->rendered_width());
920 EXPECT_EQ(height, initializing_client()->rendered_height());
921 }
922
923 void VerifySessionDescriptions() {
924 initiating_client_->VerifyRejectedMediaInSessionDescription();
925 receiving_client_->VerifyRejectedMediaInSessionDescription();
926 initiating_client_->VerifyLocalIceUfragAndPassword();
927 receiving_client_->VerifyLocalIceUfragAndPassword();
928 }
929
930 P2PTestConductor() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000931 rtc::InitializeSSL(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932 }
933 ~P2PTestConductor() {
934 if (initiating_client_) {
935 initiating_client_->set_signaling_message_receiver(NULL);
936 }
937 if (receiving_client_) {
938 receiving_client_->set_signaling_message_receiver(NULL);
939 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000940 rtc::CleanupSSL();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941 }
942
943 bool CreateTestClients() {
944 return CreateTestClients(NULL, NULL);
945 }
946
947 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
948 MediaConstraintsInterface* recv_constraints) {
949 initiating_client_.reset(SignalingClass::CreateClient("Caller: ",
950 init_constraints));
951 receiving_client_.reset(SignalingClass::CreateClient("Callee: ",
952 recv_constraints));
953 if (!initiating_client_ || !receiving_client_) {
954 return false;
955 }
956 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
957 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
958 return true;
959 }
960
961 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
962 const webrtc::FakeConstraints& recv_constraints) {
963 initiating_client_->SetVideoConstraints(init_constraints);
964 receiving_client_->SetVideoConstraints(recv_constraints);
965 }
966
967 void EnableVideoDecoderFactory() {
968 initiating_client_->EnableVideoDecoderFactory();
969 receiving_client_->EnableVideoDecoderFactory();
970 }
971
972 // This test sets up a call between two parties. Both parties send static
973 // frames to each other. Once the test is finished the number of sent frames
974 // is compared to the number of received frames.
975 void LocalP2PTest() {
976 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
977 initiating_client_->AddMediaStream(true, true);
978 }
979 initiating_client_->Negotiate();
980 const int kMaxWaitForActivationMs = 5000;
981 // Assert true is used here since next tests are guaranteed to fail and
982 // would eat up 5 seconds.
983 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
984 VerifySessionDescriptions();
985
986
987 int audio_frame_count = kEndAudioFrameCount;
988 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
989 if (!initiating_client_->can_receive_audio() ||
990 !receiving_client_->can_receive_audio()) {
991 audio_frame_count = -1;
992 }
993 int video_frame_count = kEndVideoFrameCount;
994 if (!initiating_client_->can_receive_video() ||
995 !receiving_client_->can_receive_video()) {
996 video_frame_count = -1;
997 }
998
999 if (audio_frame_count != -1 || video_frame_count != -1) {
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001000 // Audio or video is expected to flow, so both clients should reach the
1001 // Connected state, and the offerer (ICE controller) should proceed to
1002 // Completed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003 // Note: These tests have been observed to fail under heavy load at
1004 // shorter timeouts, so they may be flaky.
1005 EXPECT_EQ_WAIT(
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001006 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001007 initiating_client_->ice_connection_state(),
1008 kMaxWaitForFramesMs);
1009 EXPECT_EQ_WAIT(
1010 webrtc::PeerConnectionInterface::kIceConnectionConnected,
1011 receiving_client_->ice_connection_state(),
1012 kMaxWaitForFramesMs);
1013 }
1014
1015 if (initiating_client_->can_receive_audio() ||
1016 initiating_client_->can_receive_video()) {
1017 // The initiating client can receive media, so it must produce candidates
1018 // that will serve as destinations for that media.
1019 // TODO(bemasc): Understand why the state is not already Complete here, as
1020 // seems to be the case for the receiving client. This may indicate a bug
1021 // in the ICE gathering system.
1022 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1023 initiating_client_->ice_gathering_state());
1024 }
1025 if (receiving_client_->can_receive_audio() ||
1026 receiving_client_->can_receive_video()) {
1027 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1028 receiving_client_->ice_gathering_state(),
1029 kMaxWaitForFramesMs);
1030 }
1031
1032 EXPECT_TRUE_WAIT(FramesNotPending(audio_frame_count, video_frame_count),
1033 kMaxWaitForFramesMs);
1034 }
1035
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001036 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1037 // Messages may get lost on the unreliable DataChannel, so we send multiple
1038 // times to avoid test flakiness.
1039 static const size_t kSendAttempts = 5;
1040
1041 for (size_t i = 0; i < kSendAttempts; ++i) {
1042 dc->Send(DataBuffer(data));
1043 }
1044 }
1045
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001046 // Wait until 'size' bytes of audio has been seen by the receiver, on the
1047 // first audio stream.
1048 void WaitForAudioData(int size) {
kjellander@webrtc.orge9bfed02014-08-25 19:46:26 +00001049 static const int kMaxWaitForAudioDataMs = 10000;
1050
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001051 StreamCollectionInterface* local_streams =
1052 initializing_client()->local_streams();
1053 ASSERT_GT(local_streams->count(), 0u);
1054 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1055 MediaStreamTrackInterface* local_audio_track =
1056 local_streams->at(0)->GetAudioTracks()[0];
1057
1058 // Wait until *any* audio has been received.
1059 EXPECT_TRUE_WAIT(
1060 receiving_client()->GetBytesReceivedStats(local_audio_track) > 0,
1061 kMaxWaitForAudioDataMs);
1062
1063 // Wait until 'size' number of bytes have been received.
1064 size += receiving_client()->GetBytesReceivedStats(local_audio_track);
1065 EXPECT_TRUE_WAIT(
1066 receiving_client()->GetBytesReceivedStats(local_audio_track) > size,
1067 kMaxWaitForAudioDataMs);
1068 }
1069
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001070 SignalingClass* initializing_client() { return initiating_client_.get(); }
1071 SignalingClass* receiving_client() { return receiving_client_.get(); }
1072
1073 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001074 rtc::scoped_ptr<SignalingClass> initiating_client_;
1075 rtc::scoped_ptr<SignalingClass> receiving_client_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001076};
1077typedef P2PTestConductor<JsepTestClient> JsepPeerConnectionP2PTestClient;
1078
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001079// Disable for TSan v2, see
1080// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1081#if !defined(THREAD_SANITIZER)
1082
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001083// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001084// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1085// See issue webrtc/2378.
1086TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001087 ASSERT_TRUE(CreateTestClients());
1088 LocalP2PTest();
1089 VerifyDtmf();
1090}
1091
1092// This test sets up a Jsep call between two parties and test that we can get a
1093// video aspect ratio of 16:9.
1094TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTest16To9) {
1095 ASSERT_TRUE(CreateTestClients());
1096 FakeConstraints constraint;
1097 double requested_ratio = 640.0/360;
1098 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1099 SetVideoConstraints(constraint, constraint);
1100 LocalP2PTest();
1101
1102 ASSERT_LE(0, initializing_client()->rendered_height());
1103 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001104 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001105 initializing_client()->rendered_height();
1106 EXPECT_LE(requested_ratio, initiating_video_ratio);
1107
1108 ASSERT_LE(0, receiving_client()->rendered_height());
1109 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001110 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001111 receiving_client()->rendered_height();
1112 EXPECT_LE(requested_ratio, receiving_video_ratio);
1113}
1114
1115// This test sets up a Jsep call between two parties and test that the
1116// received video has a resolution of 1280*720.
1117// TODO(mallinath): Enable when
1118// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
1119TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTest1280By720) {
1120 ASSERT_TRUE(CreateTestClients());
1121 FakeConstraints constraint;
1122 constraint.SetMandatoryMinWidth(1280);
1123 constraint.SetMandatoryMinHeight(720);
1124 SetVideoConstraints(constraint, constraint);
1125 LocalP2PTest();
1126 VerifyRenderedSize(1280, 720);
1127}
1128
1129// This test sets up a call between two endpoints that are configured to use
1130// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
1131TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtls) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001132 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001133 FakeConstraints setup_constraints;
1134 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1135 true);
1136 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1137 LocalP2PTest();
1138 VerifyRenderedSize(640, 480);
1139}
1140
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001141// This test sets up a audio call initially and then upgrades to audio/video,
1142// using DTLS.
mallinath@webrtc.org50bc5532013-10-21 17:58:35 +00001143TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001144 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001145 FakeConstraints setup_constraints;
1146 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1147 true);
1148 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1149 receiving_client()->SetReceiveAudioVideo(true, false);
1150 LocalP2PTest();
1151 receiving_client()->SetReceiveAudioVideo(true, true);
1152 receiving_client()->Negotiate();
1153}
1154
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001155// This test sets up a call between two endpoints that are configured to use
1156// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1157// negotiated and used for transport.
1158TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001159 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001160 FakeConstraints setup_constraints;
1161 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1162 true);
1163 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1164 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1165 LocalP2PTest();
1166 VerifyRenderedSize(640, 480);
1167}
1168
1169// This test sets up a Jsep call between two parties, and the callee only
1170// accept to receive video.
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00001171// BUG=https://code.google.com/p/webrtc/issues/detail?id=2288
1172TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001173 ASSERT_TRUE(CreateTestClients());
1174 receiving_client()->SetReceiveAudioVideo(false, true);
1175 LocalP2PTest();
1176}
1177
1178// This test sets up a Jsep call between two parties, and the callee only
1179// accept to receive audio.
henrike@webrtc.orgc0b1a282013-08-23 14:32:21 +00001180TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 ASSERT_TRUE(CreateTestClients());
1182 receiving_client()->SetReceiveAudioVideo(true, false);
1183 LocalP2PTest();
1184}
1185
1186// This test sets up a Jsep call between two parties, and the callee reject both
1187// audio and video.
1188TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerNone) {
1189 ASSERT_TRUE(CreateTestClients());
1190 receiving_client()->SetReceiveAudioVideo(false, false);
1191 LocalP2PTest();
1192}
1193
1194// This test sets up an audio and video call between two parties. After the call
1195// runs for a while (10 frames), the caller sends an update offer with video
1196// being rejected. Once the re-negotiation is done, the video flow should stop
1197// and the audio flow should continue.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001198// Disabled due to b/14955157.
1199TEST_F(JsepPeerConnectionP2PTestClient,
1200 DISABLED_UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001201 ASSERT_TRUE(CreateTestClients());
1202 LocalP2PTest();
1203 TestUpdateOfferWithRejectedContent();
1204}
1205
1206// This test sets up a Jsep call between two parties. The MSID is removed from
1207// the SDP strings from the caller.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001208// Disabled due to b/14955157.
1209TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001210 ASSERT_TRUE(CreateTestClients());
1211 receiving_client()->RemoveMsidFromReceivedSdp(true);
1212 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1213 // audio and video is muxed when MSID is disabled. Remove
1214 // SetRemoveBundleFromSdp once
1215 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1216 receiving_client()->RemoveBundleFromReceivedSdp(true);
1217 LocalP2PTest();
1218}
1219
1220// This test sets up a Jsep call between two parties and the initiating peer
1221// sends two steams.
1222// TODO(perkj): Disabled due to
1223// https://code.google.com/p/webrtc/issues/detail?id=1454
1224TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestTwoStreams) {
1225 ASSERT_TRUE(CreateTestClients());
1226 // Set optional video constraint to max 320pixels to decrease CPU usage.
1227 FakeConstraints constraint;
1228 constraint.SetOptionalMaxWidth(320);
1229 SetVideoConstraints(constraint, constraint);
1230 initializing_client()->AddMediaStream(true, true);
1231 initializing_client()->AddMediaStream(false, true);
1232 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1233 LocalP2PTest();
1234 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1235}
1236
1237// Test that we can receive the audio output level from a remote audio track.
1238TEST_F(JsepPeerConnectionP2PTestClient, GetAudioOutputLevelStats) {
1239 ASSERT_TRUE(CreateTestClients());
1240 LocalP2PTest();
1241
1242 StreamCollectionInterface* remote_streams =
1243 initializing_client()->remote_streams();
1244 ASSERT_GT(remote_streams->count(), 0u);
1245 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1246 MediaStreamTrackInterface* remote_audio_track =
1247 remote_streams->at(0)->GetAudioTracks()[0];
1248
1249 // Get the audio output level stats. Note that the level is not available
1250 // until a RTCP packet has been received.
1251 EXPECT_TRUE_WAIT(
1252 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1253 kMaxWaitForStatsMs);
1254}
1255
1256// Test that an audio input level is reported.
1257TEST_F(JsepPeerConnectionP2PTestClient, GetAudioInputLevelStats) {
1258 ASSERT_TRUE(CreateTestClients());
1259 LocalP2PTest();
1260
1261 // Get the audio input level stats. The level should be available very
1262 // soon after the test starts.
1263 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1264 kMaxWaitForStatsMs);
1265}
1266
1267// Test that we can get incoming byte counts from both audio and video tracks.
1268TEST_F(JsepPeerConnectionP2PTestClient, GetBytesReceivedStats) {
1269 ASSERT_TRUE(CreateTestClients());
1270 LocalP2PTest();
1271
1272 StreamCollectionInterface* remote_streams =
1273 initializing_client()->remote_streams();
1274 ASSERT_GT(remote_streams->count(), 0u);
1275 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1276 MediaStreamTrackInterface* remote_audio_track =
1277 remote_streams->at(0)->GetAudioTracks()[0];
1278 EXPECT_TRUE_WAIT(
1279 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1280 kMaxWaitForStatsMs);
1281
1282 MediaStreamTrackInterface* remote_video_track =
1283 remote_streams->at(0)->GetVideoTracks()[0];
1284 EXPECT_TRUE_WAIT(
1285 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1286 kMaxWaitForStatsMs);
1287}
1288
1289// Test that we can get outgoing byte counts from both audio and video tracks.
1290TEST_F(JsepPeerConnectionP2PTestClient, GetBytesSentStats) {
1291 ASSERT_TRUE(CreateTestClients());
1292 LocalP2PTest();
1293
1294 StreamCollectionInterface* local_streams =
1295 initializing_client()->local_streams();
1296 ASSERT_GT(local_streams->count(), 0u);
1297 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1298 MediaStreamTrackInterface* local_audio_track =
1299 local_streams->at(0)->GetAudioTracks()[0];
1300 EXPECT_TRUE_WAIT(
1301 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1302 kMaxWaitForStatsMs);
1303
1304 MediaStreamTrackInterface* local_video_track =
1305 local_streams->at(0)->GetVideoTracks()[0];
1306 EXPECT_TRUE_WAIT(
1307 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1308 kMaxWaitForStatsMs);
1309}
1310
1311// This test sets up a call between two parties with audio, video and data.
1312TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDataChannel) {
1313 FakeConstraints setup_constraints;
1314 setup_constraints.SetAllowRtpDataChannels();
1315 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1316 initializing_client()->CreateDataChannel();
1317 LocalP2PTest();
1318 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1319 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1320 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1321 kMaxWaitMs);
1322 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1323 kMaxWaitMs);
1324
1325 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001326
1327 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001328 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1329 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001330
1331 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1333 kMaxWaitMs);
1334
1335 receiving_client()->data_channel()->Close();
1336 // Send new offer and answer.
1337 receiving_client()->Negotiate();
1338 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1339 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1340}
1341
1342// This test sets up a call between two parties and creates a data channel.
1343// The test tests that received data is buffered unless an observer has been
1344// registered.
1345// Rtp data channels can receive data before the underlying
1346// transport has detected that a channel is writable and thus data can be
1347// received before the data channel state changes to open. That is hard to test
1348// but the same buffering is used in that case.
1349TEST_F(JsepPeerConnectionP2PTestClient, RegisterDataChannelObserver) {
1350 FakeConstraints setup_constraints;
1351 setup_constraints.SetAllowRtpDataChannels();
1352 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1353 initializing_client()->CreateDataChannel();
1354 initializing_client()->Negotiate();
1355
1356 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1357 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1358 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1359 kMaxWaitMs);
1360 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1361 receiving_client()->data_channel()->state(), kMaxWaitMs);
1362
1363 // Unregister the existing observer.
1364 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001365
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001367 SendRtpData(initializing_client()->data_channel(), data);
1368
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369 // Wait a while to allow the sent data to arrive before an observer is
1370 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001371 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001372
1373 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1374 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1375}
1376
1377// This test sets up a call between two parties with audio, video and but only
1378// the initiating client support data.
1379TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001380 FakeConstraints setup_constraints_1;
1381 setup_constraints_1.SetAllowRtpDataChannels();
1382 // Must disable DTLS to make negotiation succeed.
1383 setup_constraints_1.SetMandatory(
1384 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1385 FakeConstraints setup_constraints_2;
1386 setup_constraints_2.SetMandatory(
1387 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1388 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001389 initializing_client()->CreateDataChannel();
1390 LocalP2PTest();
1391 EXPECT_TRUE(initializing_client()->data_channel() != NULL);
1392 EXPECT_FALSE(receiving_client()->data_channel());
1393 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1394}
1395
1396// This test sets up a call between two parties with audio, video. When audio
1397// and video is setup and flowing and data channel is negotiated.
1398TEST_F(JsepPeerConnectionP2PTestClient, AddDataChannelAfterRenegotiation) {
1399 FakeConstraints setup_constraints;
1400 setup_constraints.SetAllowRtpDataChannels();
1401 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1402 LocalP2PTest();
1403 initializing_client()->CreateDataChannel();
1404 // Send new offer and answer.
1405 initializing_client()->Negotiate();
1406 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1407 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1408 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1409 kMaxWaitMs);
1410 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1411 kMaxWaitMs);
1412}
1413
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001414// This test sets up a Jsep call with SCTP DataChannel and verifies the
1415// negotiation is completed without error.
1416#ifdef HAVE_SCTP
1417TEST_F(JsepPeerConnectionP2PTestClient, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001418 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001419 FakeConstraints constraints;
1420 constraints.SetMandatory(
1421 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1422 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1423 initializing_client()->CreateDataChannel();
1424 initializing_client()->Negotiate(false, false);
1425}
1426#endif
1427
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001428// This test sets up a call between two parties with audio, and video.
1429// During the call, the initializing side restart ice and the test verifies that
1430// new ice candidates are generated and audio and video still can flow.
1431TEST_F(JsepPeerConnectionP2PTestClient, IceRestart) {
1432 ASSERT_TRUE(CreateTestClients());
1433
1434 // Negotiate and wait for ice completion and make sure audio and video plays.
1435 LocalP2PTest();
1436
1437 // Create a SDP string of the first audio candidate for both clients.
1438 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1439 initializing_client()->pc()->local_description()->candidates(0);
1440 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1441 receiving_client()->pc()->local_description()->candidates(0);
1442 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1443 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1444 std::string initiator_candidate;
1445 EXPECT_TRUE(
1446 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1447 std::string receiver_candidate;
1448 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1449
1450 // Restart ice on the initializing client.
1451 receiving_client()->SetExpectIceRestart(true);
1452 initializing_client()->IceRestart();
1453
1454 // Negotiate and wait for ice completion again and make sure audio and video
1455 // plays.
1456 LocalP2PTest();
1457
1458 // Create a SDP string of the first audio candidate for both clients again.
1459 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1460 initializing_client()->pc()->local_description()->candidates(0);
1461 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1462 receiving_client()->pc()->local_description()->candidates(0);
1463 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1464 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1465 std::string initiator_candidate_restart;
1466 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1467 &initiator_candidate_restart));
1468 std::string receiver_candidate_restart;
1469 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1470 &receiver_candidate_restart));
1471
1472 // Verify that the first candidates in the local session descriptions has
1473 // changed.
1474 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1475 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1476}
1477
1478
1479// This test sets up a Jsep call between two parties with external
1480// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001481// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1482// See issue webrtc/2378.
1483TEST_F(JsepPeerConnectionP2PTestClient,
1484 DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001485 ASSERT_TRUE(CreateTestClients());
1486 EnableVideoDecoderFactory();
1487 LocalP2PTest();
1488}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001489
1490// Test receive bandwidth stats with only audio enabled at receiver.
1491TEST_F(JsepPeerConnectionP2PTestClient, ReceivedBweStatsAudio) {
1492 ASSERT_TRUE(CreateTestClients());
1493 receiving_client()->SetReceiveAudioVideo(true, false);
1494 LocalP2PTest();
1495
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001496 // Wait until we have received some audio data. Following REMB shoud be zero.
1497 WaitForAudioData(10000);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001498 EXPECT_EQ_WAIT(
1499 receiving_client()->GetAvailableReceivedBandwidthStats(), 0,
1500 kMaxWaitForRembMs);
1501}
1502
1503// Test receive bandwidth stats with combined BWE.
1504TEST_F(JsepPeerConnectionP2PTestClient, ReceivedBweStatsCombined) {
1505 FakeConstraints setup_constraints;
1506 setup_constraints.AddOptional(
1507 MediaConstraintsInterface::kCombinedAudioVideoBwe, true);
1508 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1509 initializing_client()->AddMediaStream(true, true);
1510 initializing_client()->AddMediaStream(false, true);
1511 initializing_client()->AddMediaStream(false, true);
1512 initializing_client()->AddMediaStream(false, true);
1513 LocalP2PTest();
1514
1515 // Run until a non-zero bw is reported.
1516 EXPECT_TRUE_WAIT(
1517 receiving_client()->GetAvailableReceivedBandwidthStats() > 40000,
1518 kMaxWaitForRembMs);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001519
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001520 // Halt video capturers, then run until we have gotten some audio. Following
1521 // REMB should be non-zero.
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001522 initializing_client()->StopVideoCapturers();
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001523 WaitForAudioData(10000);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001524 EXPECT_TRUE_WAIT(
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001525 receiving_client()->GetAvailableReceivedBandwidthStats() > 0,
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001526 kMaxWaitForRembMs);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001527}
1528
1529// Test receive bandwidth stats with 1 video, 3 audio streams but no combined
1530// BWE.
1531TEST_F(JsepPeerConnectionP2PTestClient, ReceivedBweStatsNotCombined) {
1532 FakeConstraints setup_constraints;
1533 setup_constraints.AddOptional(
1534 MediaConstraintsInterface::kCombinedAudioVideoBwe, false);
1535 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1536 initializing_client()->AddMediaStream(true, true);
1537 initializing_client()->AddMediaStream(false, true);
1538 initializing_client()->AddMediaStream(false, true);
1539 initializing_client()->AddMediaStream(false, true);
1540 LocalP2PTest();
1541
1542 // Run until a non-zero bw is reported.
1543 EXPECT_TRUE_WAIT(
1544 receiving_client()->GetAvailableReceivedBandwidthStats() > 40000,
1545 kMaxWaitForRembMs);
1546
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001547 // Halt video capturers, then run until we have gotten some audio. Following
1548 // REMB should be zero.
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001549 initializing_client()->StopVideoCapturers();
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001550 WaitForAudioData(10000);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001551 EXPECT_EQ_WAIT(
1552 receiving_client()->GetAvailableReceivedBandwidthStats(), 0,
1553 kMaxWaitForRembMs);
1554}
1555
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001556#endif // if !defined(THREAD_SANITIZER)