blob: 94e77310f590c7693dbf885c3acba8d7334bc46b [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();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000379 return bw;
380 }
381
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382 int rendered_width() {
383 EXPECT_FALSE(fake_video_renderers_.empty());
384 return fake_video_renderers_.empty() ? 1 :
385 fake_video_renderers_.begin()->second->width();
386 }
387
388 int rendered_height() {
389 EXPECT_FALSE(fake_video_renderers_.empty());
390 return fake_video_renderers_.empty() ? 1 :
391 fake_video_renderers_.begin()->second->height();
392 }
393
394 size_t number_of_remote_streams() {
395 if (!pc())
396 return 0;
397 return pc()->remote_streams()->count();
398 }
399
400 StreamCollectionInterface* remote_streams() {
401 if (!pc()) {
402 ADD_FAILURE();
403 return NULL;
404 }
405 return pc()->remote_streams();
406 }
407
408 StreamCollectionInterface* local_streams() {
409 if (!pc()) {
410 ADD_FAILURE();
411 return NULL;
412 }
413 return pc()->local_streams();
414 }
415
416 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
417 return pc()->signaling_state();
418 }
419
420 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
421 return pc()->ice_connection_state();
422 }
423
424 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
425 return pc()->ice_gathering_state();
426 }
427
428 // PeerConnectionObserver callbacks.
429 virtual void OnError() {}
430 virtual void OnMessage(const std::string&) {}
431 virtual void OnSignalingMessage(const std::string& /*msg*/) {}
432 virtual void OnSignalingChange(
433 webrtc::PeerConnectionInterface::SignalingState new_state) {
434 EXPECT_EQ(peer_connection_->signaling_state(), new_state);
435 }
436 virtual void OnAddStream(webrtc::MediaStreamInterface* media_stream) {
437 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
438 const std::string id = media_stream->GetVideoTracks()[i]->id();
439 ASSERT_TRUE(fake_video_renderers_.find(id) ==
440 fake_video_renderers_.end());
441 fake_video_renderers_[id] = new webrtc::FakeVideoTrackRenderer(
442 media_stream->GetVideoTracks()[i]);
443 }
444 }
445 virtual void OnRemoveStream(webrtc::MediaStreamInterface* media_stream) {}
446 virtual void OnRenegotiationNeeded() {}
447 virtual void OnIceConnectionChange(
448 webrtc::PeerConnectionInterface::IceConnectionState new_state) {
449 EXPECT_EQ(peer_connection_->ice_connection_state(), new_state);
450 }
451 virtual void OnIceGatheringChange(
452 webrtc::PeerConnectionInterface::IceGatheringState new_state) {
453 EXPECT_EQ(peer_connection_->ice_gathering_state(), new_state);
454 }
455 virtual void OnIceCandidate(
456 const webrtc::IceCandidateInterface* /*candidate*/) {}
457
458 webrtc::PeerConnectionInterface* pc() {
459 return peer_connection_.get();
460 }
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000461 void StopVideoCapturers() {
462 for (std::vector<cricket::VideoCapturer*>::iterator it =
463 video_capturers_.begin(); it != video_capturers_.end(); ++it) {
464 (*it)->Stop();
465 }
466 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467
468 protected:
469 explicit PeerConnectionTestClientBase(const std::string& id)
470 : id_(id),
471 expect_ice_restart_(false),
472 fake_video_decoder_factory_(NULL),
473 fake_video_encoder_factory_(NULL),
474 video_decoder_factory_enabled_(false),
475 signaling_message_receiver_(NULL) {
476 }
477 bool Init(const MediaConstraintsInterface* constraints) {
478 EXPECT_TRUE(!peer_connection_);
479 EXPECT_TRUE(!peer_connection_factory_);
480 allocator_factory_ = webrtc::FakePortAllocatorFactory::Create();
481 if (!allocator_factory_) {
482 return false;
483 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484 fake_audio_capture_module_ = FakeAudioCaptureModule::Create(
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000485 rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486
487 if (fake_audio_capture_module_ == NULL) {
488 return false;
489 }
490 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
491 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
492 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000493 rtc::Thread::Current(), rtc::Thread::Current(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 fake_audio_capture_module_, fake_video_encoder_factory_,
495 fake_video_decoder_factory_);
496 if (!peer_connection_factory_) {
497 return false;
498 }
499 peer_connection_ = CreatePeerConnection(allocator_factory_.get(),
500 constraints);
501 return peer_connection_.get() != NULL;
502 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000503 virtual rtc::scoped_refptr<webrtc::PeerConnectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 CreatePeerConnection(webrtc::PortAllocatorFactoryInterface* factory,
505 const MediaConstraintsInterface* constraints) = 0;
506 MessageReceiver* signaling_message_receiver() {
507 return signaling_message_receiver_;
508 }
509 webrtc::PeerConnectionFactoryInterface* peer_connection_factory() {
510 return peer_connection_factory_.get();
511 }
512
513 virtual bool can_receive_audio() = 0;
514 virtual bool can_receive_video() = 0;
515 const std::string& id() const { return id_; }
516
517 private:
518 class DummyDtmfObserver : public DtmfSenderObserverInterface {
519 public:
520 DummyDtmfObserver() : completed_(false) {}
521
522 // Implements DtmfSenderObserverInterface.
523 void OnToneChange(const std::string& tone) {
524 tones_.push_back(tone);
525 if (tone.empty()) {
526 completed_ = true;
527 }
528 }
529
530 void Verify(const std::vector<std::string>& tones) const {
531 ASSERT_TRUE(tones_.size() == tones.size());
532 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
533 }
534
535 bool completed() const { return completed_; }
536
537 private:
538 bool completed_;
539 std::vector<std::string> tones_;
540 };
541
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000542 rtc::scoped_refptr<webrtc::VideoTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 CreateLocalVideoTrack(const std::string stream_label) {
544 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
545 FakeConstraints source_constraints = video_constraints_;
546 source_constraints.SetMandatoryMaxFrameRate(10);
547
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000548 cricket::FakeVideoCapturer* fake_capturer =
549 new webrtc::FakePeriodicVideoCapturer();
550 video_capturers_.push_back(fake_capturer);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000551 rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552 peer_connection_factory_->CreateVideoSource(
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000553 fake_capturer, &source_constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 std::string label = stream_label + kVideoTrackLabelBase;
555 return peer_connection_factory_->CreateVideoTrack(label, source);
556 }
557
558 std::string id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000560 rtc::scoped_refptr<webrtc::PortAllocatorFactoryInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 allocator_factory_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000562 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
563 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564 peer_connection_factory_;
565
566 typedef std::pair<std::string, std::string> IceUfragPwdPair;
567 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
568 bool expect_ice_restart_;
569
570 // Needed to keep track of number of frames send.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000571 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 // Needed to keep track of number of frames received.
573 typedef std::map<std::string, webrtc::FakeVideoTrackRenderer*> RenderMap;
574 RenderMap fake_video_renderers_;
575 // Needed to keep track of number of frames received when external decoder
576 // used.
577 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_;
578 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_;
579 bool video_decoder_factory_enabled_;
580 webrtc::FakeConstraints video_constraints_;
581
582 // For remote peer communication.
583 MessageReceiver* signaling_message_receiver_;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000584
585 // Store references to the video capturers we've created, so that we can stop
586 // them, if required.
587 std::vector<cricket::VideoCapturer*> video_capturers_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000588};
589
590class JsepTestClient
591 : public PeerConnectionTestClientBase<JsepMessageReceiver> {
592 public:
593 static JsepTestClient* CreateClient(
594 const std::string& id,
595 const MediaConstraintsInterface* constraints) {
596 JsepTestClient* client(new JsepTestClient(id));
597 if (!client->Init(constraints)) {
598 delete client;
599 return NULL;
600 }
601 return client;
602 }
603 ~JsepTestClient() {}
604
605 virtual void Negotiate() {
606 Negotiate(true, true);
607 }
608 virtual void Negotiate(bool audio, bool video) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000609 rtc::scoped_ptr<SessionDescriptionInterface> offer;
pbos@webrtc.orgceb956b2014-09-04 15:27:49 +0000610 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611
612 if (offer->description()->GetContentByName("audio")) {
613 offer->description()->GetContentByName("audio")->rejected = !audio;
614 }
615 if (offer->description()->GetContentByName("video")) {
616 offer->description()->GetContentByName("video")->rejected = !video;
617 }
618
619 std::string sdp;
620 EXPECT_TRUE(offer->ToString(&sdp));
621 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
622 signaling_message_receiver()->ReceiveSdpMessage(
623 webrtc::SessionDescriptionInterface::kOffer, sdp);
624 }
625 // JsepMessageReceiver callback.
626 virtual void ReceiveSdpMessage(const std::string& type,
627 std::string& msg) {
628 FilterIncomingSdpMessage(&msg);
629 if (type == webrtc::SessionDescriptionInterface::kOffer) {
630 HandleIncomingOffer(msg);
631 } else {
632 HandleIncomingAnswer(msg);
633 }
634 }
635 // JsepMessageReceiver callback.
636 virtual void ReceiveIceMessage(const std::string& sdp_mid,
637 int sdp_mline_index,
638 const std::string& msg) {
639 LOG(INFO) << id() << "ReceiveIceMessage";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000640 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, NULL));
642 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
643 }
644 // Implements PeerConnectionObserver functions needed by Jsep.
645 virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
646 LOG(INFO) << id() << "OnIceCandidate";
647
648 std::string ice_sdp;
649 EXPECT_TRUE(candidate->ToString(&ice_sdp));
650 if (signaling_message_receiver() == NULL) {
651 // Remote party may be deleted.
652 return;
653 }
654 signaling_message_receiver()->ReceiveIceMessage(candidate->sdp_mid(),
655 candidate->sdp_mline_index(), ice_sdp);
656 }
657
658 void IceRestart() {
659 session_description_constraints_.SetMandatoryIceRestart(true);
660 SetExpectIceRestart(true);
661 }
662
663 void SetReceiveAudioVideo(bool audio, bool video) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000664 SetReceiveAudio(audio);
665 SetReceiveVideo(video);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000666 ASSERT_EQ(audio, can_receive_audio());
667 ASSERT_EQ(video, can_receive_video());
668 }
669
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000670 void SetReceiveAudio(bool audio) {
671 if (audio && can_receive_audio())
672 return;
673 session_description_constraints_.SetMandatoryReceiveAudio(audio);
674 }
675
676 void SetReceiveVideo(bool video) {
677 if (video && can_receive_video())
678 return;
679 session_description_constraints_.SetMandatoryReceiveVideo(video);
680 }
681
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682 void RemoveMsidFromReceivedSdp(bool remove) {
683 remove_msid_ = remove;
684 }
685
686 void RemoveSdesCryptoFromReceivedSdp(bool remove) {
687 remove_sdes_ = remove;
688 }
689
690 void RemoveBundleFromReceivedSdp(bool remove) {
691 remove_bundle_ = remove;
692 }
693
694 virtual bool can_receive_audio() {
695 bool value;
696 if (webrtc::FindConstraint(&session_description_constraints_,
697 MediaConstraintsInterface::kOfferToReceiveAudio, &value, NULL)) {
698 return value;
699 }
700 return true;
701 }
702
703 virtual bool can_receive_video() {
704 bool value;
705 if (webrtc::FindConstraint(&session_description_constraints_,
706 MediaConstraintsInterface::kOfferToReceiveVideo, &value, NULL)) {
707 return value;
708 }
709 return true;
710 }
711
712 virtual void OnIceComplete() {
713 LOG(INFO) << id() << "OnIceComplete";
714 }
715
716 virtual void OnDataChannel(DataChannelInterface* data_channel) {
717 LOG(INFO) << id() << "OnDataChannel";
718 data_channel_ = data_channel;
719 data_observer_.reset(new MockDataChannelObserver(data_channel));
720 }
721
722 void CreateDataChannel() {
723 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel,
724 NULL);
725 ASSERT_TRUE(data_channel_.get() != NULL);
726 data_observer_.reset(new MockDataChannelObserver(data_channel_));
727 }
728
729 DataChannelInterface* data_channel() { return data_channel_; }
730 const MockDataChannelObserver* data_observer() const {
731 return data_observer_.get();
732 }
733
734 protected:
735 explicit JsepTestClient(const std::string& id)
736 : PeerConnectionTestClientBase<JsepMessageReceiver>(id),
737 remove_msid_(false),
738 remove_bundle_(false),
739 remove_sdes_(false) {
740 }
741
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000742 virtual rtc::scoped_refptr<webrtc::PeerConnectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 CreatePeerConnection(webrtc::PortAllocatorFactoryInterface* factory,
744 const MediaConstraintsInterface* constraints) {
745 // CreatePeerConnection with IceServers.
746 webrtc::PeerConnectionInterface::IceServers ice_servers;
747 webrtc::PeerConnectionInterface::IceServer ice_server;
748 ice_server.uri = "stun:stun.l.google.com:19302";
749 ice_servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000750
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000751 FakeIdentityService* dtls_service =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000752 rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000753 new FakeIdentityService() : NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754 return peer_connection_factory()->CreatePeerConnection(
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000755 ice_servers, constraints, factory, dtls_service, this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 }
757
758 void HandleIncomingOffer(const std::string& msg) {
759 LOG(INFO) << id() << "HandleIncomingOffer ";
760 if (NumberOfLocalMediaStreams() == 0) {
761 // If we are not sending any streams ourselves it is time to add some.
762 AddMediaStream(true, true);
763 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000764 rtc::scoped_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 webrtc::CreateSessionDescription("offer", msg, NULL));
766 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000767 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768 EXPECT_TRUE(DoCreateAnswer(answer.use()));
769 std::string sdp;
770 EXPECT_TRUE(answer->ToString(&sdp));
771 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
772 if (signaling_message_receiver()) {
773 signaling_message_receiver()->ReceiveSdpMessage(
774 webrtc::SessionDescriptionInterface::kAnswer, sdp);
775 }
776 }
777
778 void HandleIncomingAnswer(const std::string& msg) {
779 LOG(INFO) << id() << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000780 rtc::scoped_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 webrtc::CreateSessionDescription("answer", msg, NULL));
782 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
783 }
784
785 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
786 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000787 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
788 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 MockCreateSessionDescriptionObserver>());
790 if (offer) {
791 pc()->CreateOffer(observer, &session_description_constraints_);
792 } else {
793 pc()->CreateAnswer(observer, &session_description_constraints_);
794 }
795 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
796 *desc = observer->release_desc();
797 if (observer->result() && ExpectIceRestart()) {
798 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
799 }
800 return observer->result();
801 }
802
803 bool DoCreateOffer(SessionDescriptionInterface** desc) {
804 return DoCreateOfferAnswer(desc, true);
805 }
806
807 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
808 return DoCreateOfferAnswer(desc, false);
809 }
810
811 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000812 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
813 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 MockSetSessionDescriptionObserver>());
815 LOG(INFO) << id() << "SetLocalDescription ";
816 pc()->SetLocalDescription(observer, desc);
817 // Ignore the observer result. If we wait for the result with
818 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
819 // before the offer which is an error.
820 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000821 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 // ProcessMessages waits at least 1ms but processes all messages before
823 // returning. Since this test is synchronous and send messages to the remote
824 // peer whenever a callback is invoked, this can lead to messages being
825 // sent to the remote peer in the wrong order.
826 // TODO(perkj): Find a way to check the result without risking that the
827 // order of sent messages are changed. Ex- by posting all messages that are
828 // sent to the remote peer.
829 return true;
830 }
831
832 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000833 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
834 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 MockSetSessionDescriptionObserver>());
836 LOG(INFO) << id() << "SetRemoteDescription ";
837 pc()->SetRemoteDescription(observer, desc);
838 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
839 return observer->result();
840 }
841
842 // This modifies all received SDP messages before they are processed.
843 void FilterIncomingSdpMessage(std::string* sdp) {
844 if (remove_msid_) {
845 const char kSdpSsrcAttribute[] = "a=ssrc:";
846 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
847 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
848 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
849 }
850 if (remove_bundle_) {
851 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
852 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
853 }
854 if (remove_sdes_) {
855 const char kSdpSdesCryptoAttribute[] = "a=crypto";
856 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
857 }
858 }
859
860 private:
861 webrtc::FakeConstraints session_description_constraints_;
862 bool remove_msid_; // True if MSID should be removed in received SDP.
863 bool remove_bundle_; // True if bundle should be removed in received SDP.
864 bool remove_sdes_; // True if a=crypto should be removed in received SDP.
865
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000866 rtc::scoped_refptr<DataChannelInterface> data_channel_;
867 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868};
869
870template <typename SignalingClass>
871class P2PTestConductor : public testing::Test {
872 public:
873 bool SessionActive() {
874 return initiating_client_->SessionActive() &&
875 receiving_client_->SessionActive();
876 }
877 // Return true if the number of frames provided have been received or it is
878 // known that that will never occur (e.g. no frames will be sent or
879 // captured).
880 bool FramesNotPending(int audio_frames_to_receive,
881 int video_frames_to_receive) {
882 return VideoFramesReceivedCheck(video_frames_to_receive) &&
883 AudioFramesReceivedCheck(audio_frames_to_receive);
884 }
885 bool AudioFramesReceivedCheck(int frames_received) {
886 return initiating_client_->AudioFramesReceivedCheck(frames_received) &&
887 receiving_client_->AudioFramesReceivedCheck(frames_received);
888 }
889 bool VideoFramesReceivedCheck(int frames_received) {
890 return initiating_client_->VideoFramesReceivedCheck(frames_received) &&
891 receiving_client_->VideoFramesReceivedCheck(frames_received);
892 }
893 void VerifyDtmf() {
894 initiating_client_->VerifyDtmf();
895 receiving_client_->VerifyDtmf();
896 }
897
898 void TestUpdateOfferWithRejectedContent() {
899 initiating_client_->Negotiate(true, false);
900 EXPECT_TRUE_WAIT(
901 FramesNotPending(kEndAudioFrameCount * 2, kEndVideoFrameCount),
902 kMaxWaitForFramesMs);
903 // There shouldn't be any more video frame after the new offer is
904 // negotiated.
905 EXPECT_FALSE(VideoFramesReceivedCheck(kEndVideoFrameCount + 1));
906 }
907
908 void VerifyRenderedSize(int width, int height) {
909 EXPECT_EQ(width, receiving_client()->rendered_width());
910 EXPECT_EQ(height, receiving_client()->rendered_height());
911 EXPECT_EQ(width, initializing_client()->rendered_width());
912 EXPECT_EQ(height, initializing_client()->rendered_height());
913 }
914
915 void VerifySessionDescriptions() {
916 initiating_client_->VerifyRejectedMediaInSessionDescription();
917 receiving_client_->VerifyRejectedMediaInSessionDescription();
918 initiating_client_->VerifyLocalIceUfragAndPassword();
919 receiving_client_->VerifyLocalIceUfragAndPassword();
920 }
921
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 ~P2PTestConductor() {
923 if (initiating_client_) {
924 initiating_client_->set_signaling_message_receiver(NULL);
925 }
926 if (receiving_client_) {
927 receiving_client_->set_signaling_message_receiver(NULL);
928 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929 }
930
931 bool CreateTestClients() {
932 return CreateTestClients(NULL, NULL);
933 }
934
935 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
936 MediaConstraintsInterface* recv_constraints) {
937 initiating_client_.reset(SignalingClass::CreateClient("Caller: ",
938 init_constraints));
939 receiving_client_.reset(SignalingClass::CreateClient("Callee: ",
940 recv_constraints));
941 if (!initiating_client_ || !receiving_client_) {
942 return false;
943 }
944 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
945 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
946 return true;
947 }
948
949 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
950 const webrtc::FakeConstraints& recv_constraints) {
951 initiating_client_->SetVideoConstraints(init_constraints);
952 receiving_client_->SetVideoConstraints(recv_constraints);
953 }
954
955 void EnableVideoDecoderFactory() {
956 initiating_client_->EnableVideoDecoderFactory();
957 receiving_client_->EnableVideoDecoderFactory();
958 }
959
960 // This test sets up a call between two parties. Both parties send static
961 // frames to each other. Once the test is finished the number of sent frames
962 // is compared to the number of received frames.
963 void LocalP2PTest() {
964 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
965 initiating_client_->AddMediaStream(true, true);
966 }
967 initiating_client_->Negotiate();
968 const int kMaxWaitForActivationMs = 5000;
969 // Assert true is used here since next tests are guaranteed to fail and
970 // would eat up 5 seconds.
971 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
972 VerifySessionDescriptions();
973
974
975 int audio_frame_count = kEndAudioFrameCount;
976 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
977 if (!initiating_client_->can_receive_audio() ||
978 !receiving_client_->can_receive_audio()) {
979 audio_frame_count = -1;
980 }
981 int video_frame_count = kEndVideoFrameCount;
982 if (!initiating_client_->can_receive_video() ||
983 !receiving_client_->can_receive_video()) {
984 video_frame_count = -1;
985 }
986
987 if (audio_frame_count != -1 || video_frame_count != -1) {
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000988 // Audio or video is expected to flow, so both clients should reach the
989 // Connected state, and the offerer (ICE controller) should proceed to
990 // Completed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 // Note: These tests have been observed to fail under heavy load at
992 // shorter timeouts, so they may be flaky.
993 EXPECT_EQ_WAIT(
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000994 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 initiating_client_->ice_connection_state(),
996 kMaxWaitForFramesMs);
997 EXPECT_EQ_WAIT(
998 webrtc::PeerConnectionInterface::kIceConnectionConnected,
999 receiving_client_->ice_connection_state(),
1000 kMaxWaitForFramesMs);
1001 }
1002
1003 if (initiating_client_->can_receive_audio() ||
1004 initiating_client_->can_receive_video()) {
1005 // The initiating client can receive media, so it must produce candidates
1006 // that will serve as destinations for that media.
1007 // TODO(bemasc): Understand why the state is not already Complete here, as
1008 // seems to be the case for the receiving client. This may indicate a bug
1009 // in the ICE gathering system.
1010 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1011 initiating_client_->ice_gathering_state());
1012 }
1013 if (receiving_client_->can_receive_audio() ||
1014 receiving_client_->can_receive_video()) {
1015 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1016 receiving_client_->ice_gathering_state(),
1017 kMaxWaitForFramesMs);
1018 }
1019
1020 EXPECT_TRUE_WAIT(FramesNotPending(audio_frame_count, video_frame_count),
1021 kMaxWaitForFramesMs);
1022 }
1023
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001024 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1025 // Messages may get lost on the unreliable DataChannel, so we send multiple
1026 // times to avoid test flakiness.
1027 static const size_t kSendAttempts = 5;
1028
1029 for (size_t i = 0; i < kSendAttempts; ++i) {
1030 dc->Send(DataBuffer(data));
1031 }
1032 }
1033
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001034 // Wait until 'size' bytes of audio has been seen by the receiver, on the
1035 // first audio stream.
1036 void WaitForAudioData(int size) {
solenberg@webrtc.org00f11f52014-08-27 08:52:17 +00001037 const int kMaxWaitForAudioDataMs = 10000;
kjellander@webrtc.orge9bfed02014-08-25 19:46:26 +00001038
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001039 StreamCollectionInterface* local_streams =
1040 initializing_client()->local_streams();
1041 ASSERT_GT(local_streams->count(), 0u);
1042 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1043 MediaStreamTrackInterface* local_audio_track =
1044 local_streams->at(0)->GetAudioTracks()[0];
1045
1046 // Wait until *any* audio has been received.
1047 EXPECT_TRUE_WAIT(
1048 receiving_client()->GetBytesReceivedStats(local_audio_track) > 0,
1049 kMaxWaitForAudioDataMs);
1050
1051 // Wait until 'size' number of bytes have been received.
1052 size += receiving_client()->GetBytesReceivedStats(local_audio_track);
1053 EXPECT_TRUE_WAIT(
1054 receiving_client()->GetBytesReceivedStats(local_audio_track) > size,
1055 kMaxWaitForAudioDataMs);
1056 }
1057
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058 SignalingClass* initializing_client() { return initiating_client_.get(); }
1059 SignalingClass* receiving_client() { return receiving_client_.get(); }
1060
1061 private:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001062 rtc::scoped_ptr<SignalingClass> initiating_client_;
1063 rtc::scoped_ptr<SignalingClass> receiving_client_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001064};
1065typedef P2PTestConductor<JsepTestClient> JsepPeerConnectionP2PTestClient;
1066
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001067// Disable for TSan v2, see
1068// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1069#if !defined(THREAD_SANITIZER)
1070
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001072// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1073// See issue webrtc/2378.
1074TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001075 ASSERT_TRUE(CreateTestClients());
1076 LocalP2PTest();
1077 VerifyDtmf();
1078}
1079
1080// This test sets up a Jsep call between two parties and test that we can get a
1081// video aspect ratio of 16:9.
1082TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTest16To9) {
1083 ASSERT_TRUE(CreateTestClients());
1084 FakeConstraints constraint;
1085 double requested_ratio = 640.0/360;
1086 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1087 SetVideoConstraints(constraint, constraint);
1088 LocalP2PTest();
1089
1090 ASSERT_LE(0, initializing_client()->rendered_height());
1091 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001092 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001093 initializing_client()->rendered_height();
1094 EXPECT_LE(requested_ratio, initiating_video_ratio);
1095
1096 ASSERT_LE(0, receiving_client()->rendered_height());
1097 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001098 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099 receiving_client()->rendered_height();
1100 EXPECT_LE(requested_ratio, receiving_video_ratio);
1101}
1102
1103// This test sets up a Jsep call between two parties and test that the
1104// received video has a resolution of 1280*720.
1105// TODO(mallinath): Enable when
1106// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
1107TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTest1280By720) {
1108 ASSERT_TRUE(CreateTestClients());
1109 FakeConstraints constraint;
1110 constraint.SetMandatoryMinWidth(1280);
1111 constraint.SetMandatoryMinHeight(720);
1112 SetVideoConstraints(constraint, constraint);
1113 LocalP2PTest();
1114 VerifyRenderedSize(1280, 720);
1115}
1116
1117// This test sets up a call between two endpoints that are configured to use
1118// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
1119TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtls) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001120 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001121 FakeConstraints setup_constraints;
1122 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1123 true);
1124 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1125 LocalP2PTest();
1126 VerifyRenderedSize(640, 480);
1127}
1128
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001129// This test sets up a audio call initially and then upgrades to audio/video,
1130// using DTLS.
mallinath@webrtc.org50bc5532013-10-21 17:58:35 +00001131TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001132 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001133 FakeConstraints setup_constraints;
1134 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1135 true);
1136 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1137 receiving_client()->SetReceiveAudioVideo(true, false);
1138 LocalP2PTest();
1139 receiving_client()->SetReceiveAudioVideo(true, true);
1140 receiving_client()->Negotiate();
1141}
1142
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001143// This test sets up a call between two endpoints that are configured to use
1144// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1145// negotiated and used for transport.
1146TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001147 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001148 FakeConstraints setup_constraints;
1149 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1150 true);
1151 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1152 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1153 LocalP2PTest();
1154 VerifyRenderedSize(640, 480);
1155}
1156
1157// This test sets up a Jsep call between two parties, and the callee only
1158// accept to receive video.
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00001159// BUG=https://code.google.com/p/webrtc/issues/detail?id=2288
1160TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161 ASSERT_TRUE(CreateTestClients());
1162 receiving_client()->SetReceiveAudioVideo(false, true);
1163 LocalP2PTest();
1164}
1165
1166// This test sets up a Jsep call between two parties, and the callee only
1167// accept to receive audio.
henrike@webrtc.orgc0b1a282013-08-23 14:32:21 +00001168TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001169 ASSERT_TRUE(CreateTestClients());
1170 receiving_client()->SetReceiveAudioVideo(true, false);
1171 LocalP2PTest();
1172}
1173
1174// This test sets up a Jsep call between two parties, and the callee reject both
1175// audio and video.
1176TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerNone) {
1177 ASSERT_TRUE(CreateTestClients());
1178 receiving_client()->SetReceiveAudioVideo(false, false);
1179 LocalP2PTest();
1180}
1181
1182// This test sets up an audio and video call between two parties. After the call
1183// runs for a while (10 frames), the caller sends an update offer with video
1184// being rejected. Once the re-negotiation is done, the video flow should stop
1185// and the audio flow should continue.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001186// Disabled due to b/14955157.
1187TEST_F(JsepPeerConnectionP2PTestClient,
1188 DISABLED_UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189 ASSERT_TRUE(CreateTestClients());
1190 LocalP2PTest();
1191 TestUpdateOfferWithRejectedContent();
1192}
1193
1194// This test sets up a Jsep call between two parties. The MSID is removed from
1195// the SDP strings from the caller.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001196// Disabled due to b/14955157.
1197TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198 ASSERT_TRUE(CreateTestClients());
1199 receiving_client()->RemoveMsidFromReceivedSdp(true);
1200 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1201 // audio and video is muxed when MSID is disabled. Remove
1202 // SetRemoveBundleFromSdp once
1203 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1204 receiving_client()->RemoveBundleFromReceivedSdp(true);
1205 LocalP2PTest();
1206}
1207
1208// This test sets up a Jsep call between two parties and the initiating peer
1209// sends two steams.
1210// TODO(perkj): Disabled due to
1211// https://code.google.com/p/webrtc/issues/detail?id=1454
1212TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestTwoStreams) {
1213 ASSERT_TRUE(CreateTestClients());
1214 // Set optional video constraint to max 320pixels to decrease CPU usage.
1215 FakeConstraints constraint;
1216 constraint.SetOptionalMaxWidth(320);
1217 SetVideoConstraints(constraint, constraint);
1218 initializing_client()->AddMediaStream(true, true);
1219 initializing_client()->AddMediaStream(false, true);
1220 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1221 LocalP2PTest();
1222 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1223}
1224
1225// Test that we can receive the audio output level from a remote audio track.
1226TEST_F(JsepPeerConnectionP2PTestClient, GetAudioOutputLevelStats) {
1227 ASSERT_TRUE(CreateTestClients());
1228 LocalP2PTest();
1229
1230 StreamCollectionInterface* remote_streams =
1231 initializing_client()->remote_streams();
1232 ASSERT_GT(remote_streams->count(), 0u);
1233 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1234 MediaStreamTrackInterface* remote_audio_track =
1235 remote_streams->at(0)->GetAudioTracks()[0];
1236
1237 // Get the audio output level stats. Note that the level is not available
1238 // until a RTCP packet has been received.
1239 EXPECT_TRUE_WAIT(
1240 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1241 kMaxWaitForStatsMs);
1242}
1243
1244// Test that an audio input level is reported.
1245TEST_F(JsepPeerConnectionP2PTestClient, GetAudioInputLevelStats) {
1246 ASSERT_TRUE(CreateTestClients());
1247 LocalP2PTest();
1248
1249 // Get the audio input level stats. The level should be available very
1250 // soon after the test starts.
1251 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1252 kMaxWaitForStatsMs);
1253}
1254
1255// Test that we can get incoming byte counts from both audio and video tracks.
1256TEST_F(JsepPeerConnectionP2PTestClient, GetBytesReceivedStats) {
1257 ASSERT_TRUE(CreateTestClients());
1258 LocalP2PTest();
1259
1260 StreamCollectionInterface* remote_streams =
1261 initializing_client()->remote_streams();
1262 ASSERT_GT(remote_streams->count(), 0u);
1263 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1264 MediaStreamTrackInterface* remote_audio_track =
1265 remote_streams->at(0)->GetAudioTracks()[0];
1266 EXPECT_TRUE_WAIT(
1267 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1268 kMaxWaitForStatsMs);
1269
1270 MediaStreamTrackInterface* remote_video_track =
1271 remote_streams->at(0)->GetVideoTracks()[0];
1272 EXPECT_TRUE_WAIT(
1273 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1274 kMaxWaitForStatsMs);
1275}
1276
1277// Test that we can get outgoing byte counts from both audio and video tracks.
1278TEST_F(JsepPeerConnectionP2PTestClient, GetBytesSentStats) {
1279 ASSERT_TRUE(CreateTestClients());
1280 LocalP2PTest();
1281
1282 StreamCollectionInterface* local_streams =
1283 initializing_client()->local_streams();
1284 ASSERT_GT(local_streams->count(), 0u);
1285 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1286 MediaStreamTrackInterface* local_audio_track =
1287 local_streams->at(0)->GetAudioTracks()[0];
1288 EXPECT_TRUE_WAIT(
1289 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1290 kMaxWaitForStatsMs);
1291
1292 MediaStreamTrackInterface* local_video_track =
1293 local_streams->at(0)->GetVideoTracks()[0];
1294 EXPECT_TRUE_WAIT(
1295 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1296 kMaxWaitForStatsMs);
1297}
1298
1299// This test sets up a call between two parties with audio, video and data.
1300TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDataChannel) {
1301 FakeConstraints setup_constraints;
1302 setup_constraints.SetAllowRtpDataChannels();
1303 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1304 initializing_client()->CreateDataChannel();
1305 LocalP2PTest();
1306 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1307 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1308 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1309 kMaxWaitMs);
1310 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1311 kMaxWaitMs);
1312
1313 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001314
1315 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001316 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1317 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001318
1319 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001320 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1321 kMaxWaitMs);
1322
1323 receiving_client()->data_channel()->Close();
1324 // Send new offer and answer.
1325 receiving_client()->Negotiate();
1326 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1327 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1328}
1329
1330// This test sets up a call between two parties and creates a data channel.
1331// The test tests that received data is buffered unless an observer has been
1332// registered.
1333// Rtp data channels can receive data before the underlying
1334// transport has detected that a channel is writable and thus data can be
1335// received before the data channel state changes to open. That is hard to test
1336// but the same buffering is used in that case.
1337TEST_F(JsepPeerConnectionP2PTestClient, RegisterDataChannelObserver) {
1338 FakeConstraints setup_constraints;
1339 setup_constraints.SetAllowRtpDataChannels();
1340 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1341 initializing_client()->CreateDataChannel();
1342 initializing_client()->Negotiate();
1343
1344 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1345 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1346 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1347 kMaxWaitMs);
1348 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1349 receiving_client()->data_channel()->state(), kMaxWaitMs);
1350
1351 // Unregister the existing observer.
1352 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001353
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001354 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001355 SendRtpData(initializing_client()->data_channel(), data);
1356
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001357 // Wait a while to allow the sent data to arrive before an observer is
1358 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001359 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001360
1361 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1362 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1363}
1364
1365// This test sets up a call between two parties with audio, video and but only
1366// the initiating client support data.
1367TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001368 FakeConstraints setup_constraints_1;
1369 setup_constraints_1.SetAllowRtpDataChannels();
1370 // Must disable DTLS to make negotiation succeed.
1371 setup_constraints_1.SetMandatory(
1372 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1373 FakeConstraints setup_constraints_2;
1374 setup_constraints_2.SetMandatory(
1375 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1376 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001377 initializing_client()->CreateDataChannel();
1378 LocalP2PTest();
1379 EXPECT_TRUE(initializing_client()->data_channel() != NULL);
1380 EXPECT_FALSE(receiving_client()->data_channel());
1381 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1382}
1383
1384// This test sets up a call between two parties with audio, video. When audio
1385// and video is setup and flowing and data channel is negotiated.
1386TEST_F(JsepPeerConnectionP2PTestClient, AddDataChannelAfterRenegotiation) {
1387 FakeConstraints setup_constraints;
1388 setup_constraints.SetAllowRtpDataChannels();
1389 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1390 LocalP2PTest();
1391 initializing_client()->CreateDataChannel();
1392 // Send new offer and answer.
1393 initializing_client()->Negotiate();
1394 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1395 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1396 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1397 kMaxWaitMs);
1398 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1399 kMaxWaitMs);
1400}
1401
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001402// This test sets up a Jsep call with SCTP DataChannel and verifies the
1403// negotiation is completed without error.
1404#ifdef HAVE_SCTP
1405TEST_F(JsepPeerConnectionP2PTestClient, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001406 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001407 FakeConstraints constraints;
1408 constraints.SetMandatory(
1409 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1410 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1411 initializing_client()->CreateDataChannel();
1412 initializing_client()->Negotiate(false, false);
1413}
1414#endif
1415
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001416// This test sets up a call between two parties with audio, and video.
1417// During the call, the initializing side restart ice and the test verifies that
1418// new ice candidates are generated and audio and video still can flow.
1419TEST_F(JsepPeerConnectionP2PTestClient, IceRestart) {
1420 ASSERT_TRUE(CreateTestClients());
1421
1422 // Negotiate and wait for ice completion and make sure audio and video plays.
1423 LocalP2PTest();
1424
1425 // Create a SDP string of the first audio candidate for both clients.
1426 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1427 initializing_client()->pc()->local_description()->candidates(0);
1428 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1429 receiving_client()->pc()->local_description()->candidates(0);
1430 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1431 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1432 std::string initiator_candidate;
1433 EXPECT_TRUE(
1434 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1435 std::string receiver_candidate;
1436 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1437
1438 // Restart ice on the initializing client.
1439 receiving_client()->SetExpectIceRestart(true);
1440 initializing_client()->IceRestart();
1441
1442 // Negotiate and wait for ice completion again and make sure audio and video
1443 // plays.
1444 LocalP2PTest();
1445
1446 // Create a SDP string of the first audio candidate for both clients again.
1447 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1448 initializing_client()->pc()->local_description()->candidates(0);
1449 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1450 receiving_client()->pc()->local_description()->candidates(0);
1451 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1452 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1453 std::string initiator_candidate_restart;
1454 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1455 &initiator_candidate_restart));
1456 std::string receiver_candidate_restart;
1457 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1458 &receiver_candidate_restart));
1459
1460 // Verify that the first candidates in the local session descriptions has
1461 // changed.
1462 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1463 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1464}
1465
1466
1467// This test sets up a Jsep call between two parties with external
1468// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001469// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1470// See issue webrtc/2378.
1471TEST_F(JsepPeerConnectionP2PTestClient,
1472 DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473 ASSERT_TRUE(CreateTestClients());
1474 EnableVideoDecoderFactory();
1475 LocalP2PTest();
1476}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001477
1478// Test receive bandwidth stats with only audio enabled at receiver.
1479TEST_F(JsepPeerConnectionP2PTestClient, ReceivedBweStatsAudio) {
1480 ASSERT_TRUE(CreateTestClients());
1481 receiving_client()->SetReceiveAudioVideo(true, false);
1482 LocalP2PTest();
1483
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001484 // Wait until we have received some audio data. Following REMB shoud be zero.
1485 WaitForAudioData(10000);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001486 EXPECT_EQ_WAIT(
1487 receiving_client()->GetAvailableReceivedBandwidthStats(), 0,
1488 kMaxWaitForRembMs);
1489}
1490
1491// Test receive bandwidth stats with combined BWE.
1492TEST_F(JsepPeerConnectionP2PTestClient, ReceivedBweStatsCombined) {
1493 FakeConstraints setup_constraints;
1494 setup_constraints.AddOptional(
1495 MediaConstraintsInterface::kCombinedAudioVideoBwe, true);
1496 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1497 initializing_client()->AddMediaStream(true, true);
1498 initializing_client()->AddMediaStream(false, true);
1499 initializing_client()->AddMediaStream(false, true);
1500 initializing_client()->AddMediaStream(false, true);
1501 LocalP2PTest();
1502
1503 // Run until a non-zero bw is reported.
pbos@webrtc.org000d8672014-09-15 14:38:07 +00001504 EXPECT_TRUE_WAIT(receiving_client()->GetAvailableReceivedBandwidthStats() > 0,
1505 kMaxWaitForRembMs);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001506
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001507 // Halt video capturers, then run until we have gotten some audio. Following
1508 // REMB should be non-zero.
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001509 initializing_client()->StopVideoCapturers();
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001510 WaitForAudioData(10000);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001511 EXPECT_TRUE_WAIT(
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001512 receiving_client()->GetAvailableReceivedBandwidthStats() > 0,
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001513 kMaxWaitForRembMs);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001514}
1515
1516// Test receive bandwidth stats with 1 video, 3 audio streams but no combined
1517// BWE.
1518TEST_F(JsepPeerConnectionP2PTestClient, ReceivedBweStatsNotCombined) {
1519 FakeConstraints setup_constraints;
1520 setup_constraints.AddOptional(
1521 MediaConstraintsInterface::kCombinedAudioVideoBwe, false);
1522 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1523 initializing_client()->AddMediaStream(true, true);
1524 initializing_client()->AddMediaStream(false, true);
1525 initializing_client()->AddMediaStream(false, true);
1526 initializing_client()->AddMediaStream(false, true);
1527 LocalP2PTest();
1528
1529 // Run until a non-zero bw is reported.
pbos@webrtc.org000d8672014-09-15 14:38:07 +00001530 EXPECT_TRUE_WAIT(receiving_client()->GetAvailableReceivedBandwidthStats() > 0,
1531 kMaxWaitForRembMs);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001532
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001533 // Halt video capturers, then run until we have gotten some audio. Following
1534 // REMB should be zero.
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001535 initializing_client()->StopVideoCapturers();
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001536 WaitForAudioData(10000);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001537 EXPECT_EQ_WAIT(
1538 receiving_client()->GetAvailableReceivedBandwidthStats(), 0,
1539 kMaxWaitForRembMs);
1540}
1541
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001542#endif // if !defined(THREAD_SANITIZER)