blob: ce70955bd54e2c531fa53513c1421d354eb3e94f [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <stdio.h>
29
30#include <algorithm>
31#include <list>
32#include <map>
33#include <vector>
34
35#include "talk/app/webrtc/dtmfsender.h"
36#include "talk/app/webrtc/fakeportallocatorfactory.h"
37#include "talk/app/webrtc/localaudiosource.h"
38#include "talk/app/webrtc/mediastreaminterface.h"
39#include "talk/app/webrtc/peerconnectionfactory.h"
40#include "talk/app/webrtc/peerconnectioninterface.h"
41#include "talk/app/webrtc/test/fakeaudiocapturemodule.h"
42#include "talk/app/webrtc/test/fakeconstraints.h"
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +000043#include "talk/app/webrtc/test/fakedtlsidentityservice.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044#include "talk/app/webrtc/test/fakeperiodicvideocapturer.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000045#include "talk/app/webrtc/test/fakevideotrackrenderer.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046#include "talk/app/webrtc/test/mockpeerconnectionobservers.h"
47#include "talk/app/webrtc/videosourceinterface.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000048#include "talk/media/webrtc/fakewebrtcvideoengine.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000049#include "talk/session/media/mediasession.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000050#include "webrtc/base/gunit.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000051#include "webrtc/base/physicalsocketserver.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000052#include "webrtc/base/scoped_ptr.h"
53#include "webrtc/base/ssladapter.h"
54#include "webrtc/base/sslstreamadapter.h"
55#include "webrtc/base/thread.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000056#include "webrtc/base/virtualsocketserver.h"
57#include "webrtc/p2p/base/constants.h"
58#include "webrtc/p2p/base/sessiondescription.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059
60#define MAYBE_SKIP_TEST(feature) \
61 if (!(feature())) { \
62 LOG(LS_INFO) << "Feature disabled... skipping"; \
63 return; \
64 }
65
66using cricket::ContentInfo;
67using cricket::FakeWebRtcVideoDecoder;
68using cricket::FakeWebRtcVideoDecoderFactory;
69using cricket::FakeWebRtcVideoEncoder;
70using cricket::FakeWebRtcVideoEncoderFactory;
71using cricket::MediaContentDescription;
72using webrtc::DataBuffer;
73using webrtc::DataChannelInterface;
74using webrtc::DtmfSender;
75using webrtc::DtmfSenderInterface;
76using webrtc::DtmfSenderObserverInterface;
77using webrtc::FakeConstraints;
78using webrtc::MediaConstraintsInterface;
79using webrtc::MediaStreamTrackInterface;
80using webrtc::MockCreateSessionDescriptionObserver;
81using webrtc::MockDataChannelObserver;
82using webrtc::MockSetSessionDescriptionObserver;
83using webrtc::MockStatsObserver;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000084using webrtc::PeerConnectionInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085using webrtc::SessionDescriptionInterface;
86using webrtc::StreamCollectionInterface;
87
jiayl@webrtc.org8f88f202014-04-16 17:14:21 +000088static const int kMaxWaitMs = 2000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000089// Disable for TSan v2, see
90// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
91// This declaration is also #ifdef'd as it causes uninitialized-variable
92// warnings.
93#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094static const int kMaxWaitForStatsMs = 3000;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +000095static const int kMaxWaitForRembMs = 5000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000096#endif
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000097static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098static const int kEndAudioFrameCount = 3;
99static const int kEndVideoFrameCount = 3;
100
101static const char kStreamLabelBase[] = "stream_label";
102static const char kVideoTrackLabelBase[] = "video_track";
103static const char kAudioTrackLabelBase[] = "audio_track";
104static const char kDataChannelLabel[] = "data_channel";
105
106static void RemoveLinesFromSdp(const std::string& line_start,
107 std::string* sdp) {
108 const char kSdpLineEnd[] = "\r\n";
109 size_t ssrc_pos = 0;
110 while ((ssrc_pos = sdp->find(line_start, ssrc_pos)) !=
111 std::string::npos) {
112 size_t end_ssrc = sdp->find(kSdpLineEnd, ssrc_pos);
113 sdp->erase(ssrc_pos, end_ssrc - ssrc_pos + strlen(kSdpLineEnd));
114 }
115}
116
117class SignalingMessageReceiver {
118 public:
119 protected:
120 SignalingMessageReceiver() {}
121 virtual ~SignalingMessageReceiver() {}
122};
123
124class JsepMessageReceiver : public SignalingMessageReceiver {
125 public:
126 virtual void ReceiveSdpMessage(const std::string& type,
127 std::string& msg) = 0;
128 virtual void ReceiveIceMessage(const std::string& sdp_mid,
129 int sdp_mline_index,
130 const std::string& msg) = 0;
131
132 protected:
133 JsepMessageReceiver() {}
134 virtual ~JsepMessageReceiver() {}
135};
136
137template <typename MessageReceiver>
138class PeerConnectionTestClientBase
139 : public webrtc::PeerConnectionObserver,
140 public MessageReceiver {
141 public:
142 ~PeerConnectionTestClientBase() {
143 while (!fake_video_renderers_.empty()) {
144 RenderMap::iterator it = fake_video_renderers_.begin();
145 delete it->second;
146 fake_video_renderers_.erase(it);
147 }
148 }
149
150 virtual void Negotiate() = 0;
151
152 virtual void Negotiate(bool audio, bool video) = 0;
153
154 virtual void SetVideoConstraints(
155 const webrtc::FakeConstraints& video_constraint) {
156 video_constraints_ = video_constraint;
157 }
158
159 void AddMediaStream(bool audio, bool video) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000160 std::string stream_label = kStreamLabelBase +
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000161 rtc::ToString<int>(
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000162 static_cast<int>(peer_connection_->local_streams()->count()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000163 rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000164 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165
166 if (audio && can_receive_audio()) {
167 FakeConstraints constraints;
168 // Disable highpass filter so that we can get all the test audio frames.
169 constraints.AddMandatory(
170 MediaConstraintsInterface::kHighpassFilter, false);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000171 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
wu@webrtc.org97077a32013-10-25 21:18:33 +0000172 peer_connection_factory_->CreateAudioSource(&constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173 // TODO(perkj): Test audio source when it is implemented. Currently audio
174 // always use the default input.
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000175 std::string label = stream_label + kAudioTrackLabelBase;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000176 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000177 peer_connection_factory_->CreateAudioTrack(label, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 stream->AddTrack(audio_track);
179 }
180 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000181 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 }
183
perkj@webrtc.orgc2dd5ee2014-11-04 11:31:29 +0000184 EXPECT_TRUE(peer_connection_->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 }
186
187 size_t NumberOfLocalMediaStreams() {
188 return peer_connection_->local_streams()->count();
189 }
190
191 bool SessionActive() {
192 return peer_connection_->signaling_state() ==
193 webrtc::PeerConnectionInterface::kStable;
194 }
195
196 void set_signaling_message_receiver(
197 MessageReceiver* signaling_message_receiver) {
198 signaling_message_receiver_ = signaling_message_receiver;
199 }
200
201 void EnableVideoDecoderFactory() {
202 video_decoder_factory_enabled_ = true;
203 fake_video_decoder_factory_->AddSupportedVideoCodecType(
204 webrtc::kVideoCodecVP8);
205 }
206
207 bool AudioFramesReceivedCheck(int number_of_frames) const {
208 return number_of_frames <= fake_audio_capture_module_->frames_received();
209 }
210
211 bool VideoFramesReceivedCheck(int number_of_frames) {
212 if (video_decoder_factory_enabled_) {
213 const std::vector<FakeWebRtcVideoDecoder*>& decoders
214 = fake_video_decoder_factory_->decoders();
215 if (decoders.empty()) {
216 return number_of_frames <= 0;
217 }
218
219 for (std::vector<FakeWebRtcVideoDecoder*>::const_iterator
220 it = decoders.begin(); it != decoders.end(); ++it) {
221 if (number_of_frames > (*it)->GetNumFramesReceived()) {
222 return false;
223 }
224 }
225 return true;
226 } else {
227 if (fake_video_renderers_.empty()) {
228 return number_of_frames <= 0;
229 }
230
231 for (RenderMap::const_iterator it = fake_video_renderers_.begin();
232 it != fake_video_renderers_.end(); ++it) {
233 if (number_of_frames > it->second->num_rendered_frames()) {
234 return false;
235 }
236 }
237 return true;
238 }
239 }
240 // Verify the CreateDtmfSender interface
241 void VerifyDtmf() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000242 rtc::scoped_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
243 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244
245 // We can't create a DTMF sender with an invalid audio track or a non local
246 // track.
247 EXPECT_TRUE(peer_connection_->CreateDtmfSender(NULL) == NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000248 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249 peer_connection_factory_->CreateAudioTrack("dummy_track",
250 NULL));
251 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == NULL);
252
253 // We should be able to create a DTMF sender from a local track.
254 webrtc::AudioTrackInterface* localtrack =
255 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
256 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
257 EXPECT_TRUE(dtmf_sender.get() != NULL);
258 dtmf_sender->RegisterObserver(observer.get());
259
260 // Test the DtmfSender object just created.
261 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
262 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
263
264 // We don't need to verify that the DTMF tones are actually sent out because
265 // that is already covered by the tests of the lower level components.
266
267 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
268 std::vector<std::string> tones;
269 tones.push_back("1");
270 tones.push_back("a");
271 tones.push_back("");
272 observer->Verify(tones);
273
274 dtmf_sender->UnregisterObserver();
275 }
276
277 // Verifies that the SessionDescription have rejected the appropriate media
278 // content.
279 void VerifyRejectedMediaInSessionDescription() {
280 ASSERT_TRUE(peer_connection_->remote_description() != NULL);
281 ASSERT_TRUE(peer_connection_->local_description() != NULL);
282 const cricket::SessionDescription* remote_desc =
283 peer_connection_->remote_description()->description();
284 const cricket::SessionDescription* local_desc =
285 peer_connection_->local_description()->description();
286
287 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
288 if (remote_audio_content) {
289 const ContentInfo* audio_content =
290 GetFirstAudioContent(local_desc);
291 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
292 }
293
294 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
295 if (remote_video_content) {
296 const ContentInfo* video_content =
297 GetFirstVideoContent(local_desc);
298 EXPECT_EQ(can_receive_video(), !video_content->rejected);
299 }
300 }
301
302 void SetExpectIceRestart(bool expect_restart) {
303 expect_ice_restart_ = expect_restart;
304 }
305
306 bool ExpectIceRestart() const { return expect_ice_restart_; }
307
308 void VerifyLocalIceUfragAndPassword() {
309 ASSERT_TRUE(peer_connection_->local_description() != NULL);
310 const cricket::SessionDescription* desc =
311 peer_connection_->local_description()->description();
312 const cricket::ContentInfos& contents = desc->contents();
313
314 for (size_t index = 0; index < contents.size(); ++index) {
315 if (contents[index].rejected)
316 continue;
317 const cricket::TransportDescription* transport_desc =
318 desc->GetTransportDescriptionByName(contents[index].name);
319
320 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000321 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322 if (ufragpair_it == ice_ufrag_pwd_.end()) {
323 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000324 ice_ufrag_pwd_[static_cast<int>(index)] =
325 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326 } else if (ExpectIceRestart()) {
327 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
328 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
329 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
330 } else {
331 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
332 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
333 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
334 }
335 }
336 }
337
338 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000339 rtc::scoped_refptr<MockStatsObserver>
340 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000341 EXPECT_TRUE(peer_connection_->GetStats(
342 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
344 return observer->AudioOutputLevel();
345 }
346
347 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000348 rtc::scoped_refptr<MockStatsObserver>
349 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000350 EXPECT_TRUE(peer_connection_->GetStats(
351 observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
353 return observer->AudioInputLevel();
354 }
355
356 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000357 rtc::scoped_refptr<MockStatsObserver>
358 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000359 EXPECT_TRUE(peer_connection_->GetStats(
360 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
362 return observer->BytesReceived();
363 }
364
365 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000366 rtc::scoped_refptr<MockStatsObserver>
367 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000368 EXPECT_TRUE(peer_connection_->GetStats(
369 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
371 return observer->BytesSent();
372 }
373
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000374 int GetAvailableReceivedBandwidthStats() {
375 rtc::scoped_refptr<MockStatsObserver>
376 observer(new rtc::RefCountedObject<MockStatsObserver>());
377 EXPECT_TRUE(peer_connection_->GetStats(
378 observer, NULL, PeerConnectionInterface::kStatsOutputLevelStandard));
379 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
380 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000381 return bw;
382 }
383
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000384 int rendered_width() {
385 EXPECT_FALSE(fake_video_renderers_.empty());
386 return fake_video_renderers_.empty() ? 1 :
387 fake_video_renderers_.begin()->second->width();
388 }
389
390 int rendered_height() {
391 EXPECT_FALSE(fake_video_renderers_.empty());
392 return fake_video_renderers_.empty() ? 1 :
393 fake_video_renderers_.begin()->second->height();
394 }
395
396 size_t number_of_remote_streams() {
397 if (!pc())
398 return 0;
399 return pc()->remote_streams()->count();
400 }
401
402 StreamCollectionInterface* remote_streams() {
403 if (!pc()) {
404 ADD_FAILURE();
405 return NULL;
406 }
407 return pc()->remote_streams();
408 }
409
410 StreamCollectionInterface* local_streams() {
411 if (!pc()) {
412 ADD_FAILURE();
413 return NULL;
414 }
415 return pc()->local_streams();
416 }
417
418 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
419 return pc()->signaling_state();
420 }
421
422 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
423 return pc()->ice_connection_state();
424 }
425
426 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
427 return pc()->ice_gathering_state();
428 }
429
430 // PeerConnectionObserver callbacks.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000431 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 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 fake_audio_capture_module_ = FakeAudioCaptureModule::Create(
jiayl@webrtc.org3987b6d2014-09-24 17:14:05 +0000486 rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487
488 if (fake_audio_capture_module_ == NULL) {
489 return false;
490 }
491 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
492 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
493 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000494 rtc::Thread::Current(), rtc::Thread::Current(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495 fake_audio_capture_module_, fake_video_encoder_factory_,
496 fake_video_decoder_factory_);
497 if (!peer_connection_factory_) {
498 return false;
499 }
500 peer_connection_ = CreatePeerConnection(allocator_factory_.get(),
501 constraints);
502 return peer_connection_.get() != NULL;
503 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000504 virtual rtc::scoped_refptr<webrtc::PeerConnectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 CreatePeerConnection(webrtc::PortAllocatorFactoryInterface* factory,
506 const MediaConstraintsInterface* constraints) = 0;
507 MessageReceiver* signaling_message_receiver() {
508 return signaling_message_receiver_;
509 }
510 webrtc::PeerConnectionFactoryInterface* peer_connection_factory() {
511 return peer_connection_factory_.get();
512 }
513
514 virtual bool can_receive_audio() = 0;
515 virtual bool can_receive_video() = 0;
516 const std::string& id() const { return id_; }
517
518 private:
519 class DummyDtmfObserver : public DtmfSenderObserverInterface {
520 public:
521 DummyDtmfObserver() : completed_(false) {}
522
523 // Implements DtmfSenderObserverInterface.
524 void OnToneChange(const std::string& tone) {
525 tones_.push_back(tone);
526 if (tone.empty()) {
527 completed_ = true;
528 }
529 }
530
531 void Verify(const std::vector<std::string>& tones) const {
532 ASSERT_TRUE(tones_.size() == tones.size());
533 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
534 }
535
536 bool completed() const { return completed_; }
537
538 private:
539 bool completed_;
540 std::vector<std::string> tones_;
541 };
542
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000543 rtc::scoped_refptr<webrtc::VideoTrackInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544 CreateLocalVideoTrack(const std::string stream_label) {
545 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
546 FakeConstraints source_constraints = video_constraints_;
547 source_constraints.SetMandatoryMaxFrameRate(10);
548
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000549 cricket::FakeVideoCapturer* fake_capturer =
550 new webrtc::FakePeriodicVideoCapturer();
551 video_capturers_.push_back(fake_capturer);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000552 rtc::scoped_refptr<webrtc::VideoSourceInterface> source =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 peer_connection_factory_->CreateVideoSource(
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000554 fake_capturer, &source_constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 std::string label = stream_label + kVideoTrackLabelBase;
556 return peer_connection_factory_->CreateVideoTrack(label, source);
557 }
558
559 std::string id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000561 rtc::scoped_refptr<webrtc::PortAllocatorFactoryInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562 allocator_factory_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000563 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
564 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000565 peer_connection_factory_;
566
567 typedef std::pair<std::string, std::string> IceUfragPwdPair;
568 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
569 bool expect_ice_restart_;
570
571 // Needed to keep track of number of frames send.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000572 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573 // Needed to keep track of number of frames received.
574 typedef std::map<std::string, webrtc::FakeVideoTrackRenderer*> RenderMap;
575 RenderMap fake_video_renderers_;
576 // Needed to keep track of number of frames received when external decoder
577 // used.
578 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_;
579 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_;
580 bool video_decoder_factory_enabled_;
581 webrtc::FakeConstraints video_constraints_;
582
583 // For remote peer communication.
584 MessageReceiver* signaling_message_receiver_;
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000585
586 // Store references to the video capturers we've created, so that we can stop
587 // them, if required.
588 std::vector<cricket::VideoCapturer*> video_capturers_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589};
590
591class JsepTestClient
592 : public PeerConnectionTestClientBase<JsepMessageReceiver> {
593 public:
594 static JsepTestClient* CreateClient(
595 const std::string& id,
596 const MediaConstraintsInterface* constraints) {
597 JsepTestClient* client(new JsepTestClient(id));
598 if (!client->Init(constraints)) {
599 delete client;
600 return NULL;
601 }
602 return client;
603 }
604 ~JsepTestClient() {}
605
606 virtual void Negotiate() {
607 Negotiate(true, true);
608 }
609 virtual void Negotiate(bool audio, bool video) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000610 rtc::scoped_ptr<SessionDescriptionInterface> offer;
pbos@webrtc.orgceb956b2014-09-04 15:27:49 +0000611 ASSERT_TRUE(DoCreateOffer(offer.use()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612
613 if (offer->description()->GetContentByName("audio")) {
614 offer->description()->GetContentByName("audio")->rejected = !audio;
615 }
616 if (offer->description()->GetContentByName("video")) {
617 offer->description()->GetContentByName("video")->rejected = !video;
618 }
619
620 std::string sdp;
621 EXPECT_TRUE(offer->ToString(&sdp));
622 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
623 signaling_message_receiver()->ReceiveSdpMessage(
624 webrtc::SessionDescriptionInterface::kOffer, sdp);
625 }
626 // JsepMessageReceiver callback.
627 virtual void ReceiveSdpMessage(const std::string& type,
628 std::string& msg) {
629 FilterIncomingSdpMessage(&msg);
630 if (type == webrtc::SessionDescriptionInterface::kOffer) {
631 HandleIncomingOffer(msg);
632 } else {
633 HandleIncomingAnswer(msg);
634 }
635 }
636 // JsepMessageReceiver callback.
637 virtual void ReceiveIceMessage(const std::string& sdp_mid,
638 int sdp_mline_index,
639 const std::string& msg) {
640 LOG(INFO) << id() << "ReceiveIceMessage";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000641 rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, NULL));
643 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
644 }
645 // Implements PeerConnectionObserver functions needed by Jsep.
646 virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
647 LOG(INFO) << id() << "OnIceCandidate";
648
649 std::string ice_sdp;
650 EXPECT_TRUE(candidate->ToString(&ice_sdp));
651 if (signaling_message_receiver() == NULL) {
652 // Remote party may be deleted.
653 return;
654 }
655 signaling_message_receiver()->ReceiveIceMessage(candidate->sdp_mid(),
656 candidate->sdp_mline_index(), ice_sdp);
657 }
658
659 void IceRestart() {
660 session_description_constraints_.SetMandatoryIceRestart(true);
661 SetExpectIceRestart(true);
662 }
663
664 void SetReceiveAudioVideo(bool audio, bool video) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000665 SetReceiveAudio(audio);
666 SetReceiveVideo(video);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 ASSERT_EQ(audio, can_receive_audio());
668 ASSERT_EQ(video, can_receive_video());
669 }
670
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000671 void SetReceiveAudio(bool audio) {
672 if (audio && can_receive_audio())
673 return;
674 session_description_constraints_.SetMandatoryReceiveAudio(audio);
675 }
676
677 void SetReceiveVideo(bool video) {
678 if (video && can_receive_video())
679 return;
680 session_description_constraints_.SetMandatoryReceiveVideo(video);
681 }
682
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683 void RemoveMsidFromReceivedSdp(bool remove) {
684 remove_msid_ = remove;
685 }
686
687 void RemoveSdesCryptoFromReceivedSdp(bool remove) {
688 remove_sdes_ = remove;
689 }
690
691 void RemoveBundleFromReceivedSdp(bool remove) {
692 remove_bundle_ = remove;
693 }
694
695 virtual bool can_receive_audio() {
696 bool value;
697 if (webrtc::FindConstraint(&session_description_constraints_,
698 MediaConstraintsInterface::kOfferToReceiveAudio, &value, NULL)) {
699 return value;
700 }
701 return true;
702 }
703
704 virtual bool can_receive_video() {
705 bool value;
706 if (webrtc::FindConstraint(&session_description_constraints_,
707 MediaConstraintsInterface::kOfferToReceiveVideo, &value, NULL)) {
708 return value;
709 }
710 return true;
711 }
712
713 virtual void OnIceComplete() {
714 LOG(INFO) << id() << "OnIceComplete";
715 }
716
717 virtual void OnDataChannel(DataChannelInterface* data_channel) {
718 LOG(INFO) << id() << "OnDataChannel";
719 data_channel_ = data_channel;
720 data_observer_.reset(new MockDataChannelObserver(data_channel));
721 }
722
723 void CreateDataChannel() {
724 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel,
725 NULL);
726 ASSERT_TRUE(data_channel_.get() != NULL);
727 data_observer_.reset(new MockDataChannelObserver(data_channel_));
728 }
729
730 DataChannelInterface* data_channel() { return data_channel_; }
731 const MockDataChannelObserver* data_observer() const {
732 return data_observer_.get();
733 }
734
735 protected:
736 explicit JsepTestClient(const std::string& id)
737 : PeerConnectionTestClientBase<JsepMessageReceiver>(id),
738 remove_msid_(false),
739 remove_bundle_(false),
740 remove_sdes_(false) {
741 }
742
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000743 virtual rtc::scoped_refptr<webrtc::PeerConnectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 CreatePeerConnection(webrtc::PortAllocatorFactoryInterface* factory,
745 const MediaConstraintsInterface* constraints) {
746 // CreatePeerConnection with IceServers.
747 webrtc::PeerConnectionInterface::IceServers ice_servers;
748 webrtc::PeerConnectionInterface::IceServer ice_server;
749 ice_server.uri = "stun:stun.l.google.com:19302";
750 ice_servers.push_back(ice_server);
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000751
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000752 FakeIdentityService* dtls_service =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000753 rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +0000754 new FakeIdentityService() : NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 return peer_connection_factory()->CreatePeerConnection(
jiayl@webrtc.orga576faf2014-01-29 17:45:53 +0000756 ice_servers, constraints, factory, dtls_service, this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 }
758
759 void HandleIncomingOffer(const std::string& msg) {
760 LOG(INFO) << id() << "HandleIncomingOffer ";
761 if (NumberOfLocalMediaStreams() == 0) {
762 // If we are not sending any streams ourselves it is time to add some.
763 AddMediaStream(true, true);
764 }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000765 rtc::scoped_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 webrtc::CreateSessionDescription("offer", msg, NULL));
767 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000768 rtc::scoped_ptr<SessionDescriptionInterface> answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 EXPECT_TRUE(DoCreateAnswer(answer.use()));
770 std::string sdp;
771 EXPECT_TRUE(answer->ToString(&sdp));
772 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
773 if (signaling_message_receiver()) {
774 signaling_message_receiver()->ReceiveSdpMessage(
775 webrtc::SessionDescriptionInterface::kAnswer, sdp);
776 }
777 }
778
779 void HandleIncomingAnswer(const std::string& msg) {
780 LOG(INFO) << id() << "HandleIncomingAnswer";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000781 rtc::scoped_ptr<SessionDescriptionInterface> desc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 webrtc::CreateSessionDescription("answer", msg, NULL));
783 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
784 }
785
786 bool DoCreateOfferAnswer(SessionDescriptionInterface** desc,
787 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000788 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
789 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 MockCreateSessionDescriptionObserver>());
791 if (offer) {
792 pc()->CreateOffer(observer, &session_description_constraints_);
793 } else {
794 pc()->CreateAnswer(observer, &session_description_constraints_);
795 }
796 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
797 *desc = observer->release_desc();
798 if (observer->result() && ExpectIceRestart()) {
799 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
800 }
801 return observer->result();
802 }
803
804 bool DoCreateOffer(SessionDescriptionInterface** desc) {
805 return DoCreateOfferAnswer(desc, true);
806 }
807
808 bool DoCreateAnswer(SessionDescriptionInterface** desc) {
809 return DoCreateOfferAnswer(desc, false);
810 }
811
812 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000813 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
814 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 MockSetSessionDescriptionObserver>());
816 LOG(INFO) << id() << "SetLocalDescription ";
817 pc()->SetLocalDescription(observer, desc);
818 // Ignore the observer result. If we wait for the result with
819 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
820 // before the offer which is an error.
821 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000822 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 // ProcessMessages waits at least 1ms but processes all messages before
824 // returning. Since this test is synchronous and send messages to the remote
825 // peer whenever a callback is invoked, this can lead to messages being
826 // sent to the remote peer in the wrong order.
827 // TODO(perkj): Find a way to check the result without risking that the
828 // order of sent messages are changed. Ex- by posting all messages that are
829 // sent to the remote peer.
830 return true;
831 }
832
833 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000834 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
835 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 MockSetSessionDescriptionObserver>());
837 LOG(INFO) << id() << "SetRemoteDescription ";
838 pc()->SetRemoteDescription(observer, desc);
839 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
840 return observer->result();
841 }
842
843 // This modifies all received SDP messages before they are processed.
844 void FilterIncomingSdpMessage(std::string* sdp) {
845 if (remove_msid_) {
846 const char kSdpSsrcAttribute[] = "a=ssrc:";
847 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
848 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
849 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
850 }
851 if (remove_bundle_) {
852 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
853 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
854 }
855 if (remove_sdes_) {
856 const char kSdpSdesCryptoAttribute[] = "a=crypto";
857 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
858 }
859 }
860
861 private:
862 webrtc::FakeConstraints session_description_constraints_;
863 bool remove_msid_; // True if MSID should be removed in received SDP.
864 bool remove_bundle_; // True if bundle should be removed in received SDP.
865 bool remove_sdes_; // True if a=crypto should be removed in received SDP.
866
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000867 rtc::scoped_refptr<DataChannelInterface> data_channel_;
868 rtc::scoped_ptr<MockDataChannelObserver> data_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869};
870
871template <typename SignalingClass>
872class P2PTestConductor : public testing::Test {
873 public:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000874 P2PTestConductor()
875 : pss_(new rtc::PhysicalSocketServer),
876 ss_(new rtc::VirtualSocketServer(pss_.get())),
877 ss_scope_(ss_.get()) {}
878
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 bool SessionActive() {
880 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000881 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +0000883
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 // Return true if the number of frames provided have been received or it is
885 // known that that will never occur (e.g. no frames will be sent or
886 // captured).
887 bool FramesNotPending(int audio_frames_to_receive,
888 int video_frames_to_receive) {
889 return VideoFramesReceivedCheck(video_frames_to_receive) &&
890 AudioFramesReceivedCheck(audio_frames_to_receive);
891 }
892 bool AudioFramesReceivedCheck(int frames_received) {
893 return initiating_client_->AudioFramesReceivedCheck(frames_received) &&
894 receiving_client_->AudioFramesReceivedCheck(frames_received);
895 }
896 bool VideoFramesReceivedCheck(int frames_received) {
897 return initiating_client_->VideoFramesReceivedCheck(frames_received) &&
898 receiving_client_->VideoFramesReceivedCheck(frames_received);
899 }
900 void VerifyDtmf() {
901 initiating_client_->VerifyDtmf();
902 receiving_client_->VerifyDtmf();
903 }
904
905 void TestUpdateOfferWithRejectedContent() {
906 initiating_client_->Negotiate(true, false);
907 EXPECT_TRUE_WAIT(
908 FramesNotPending(kEndAudioFrameCount * 2, kEndVideoFrameCount),
909 kMaxWaitForFramesMs);
910 // There shouldn't be any more video frame after the new offer is
911 // negotiated.
912 EXPECT_FALSE(VideoFramesReceivedCheck(kEndVideoFrameCount + 1));
913 }
914
915 void VerifyRenderedSize(int width, int height) {
916 EXPECT_EQ(width, receiving_client()->rendered_width());
917 EXPECT_EQ(height, receiving_client()->rendered_height());
918 EXPECT_EQ(width, initializing_client()->rendered_width());
919 EXPECT_EQ(height, initializing_client()->rendered_height());
920 }
921
922 void VerifySessionDescriptions() {
923 initiating_client_->VerifyRejectedMediaInSessionDescription();
924 receiving_client_->VerifyRejectedMediaInSessionDescription();
925 initiating_client_->VerifyLocalIceUfragAndPassword();
926 receiving_client_->VerifyLocalIceUfragAndPassword();
927 }
928
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929 ~P2PTestConductor() {
930 if (initiating_client_) {
931 initiating_client_->set_signaling_message_receiver(NULL);
932 }
933 if (receiving_client_) {
934 receiving_client_->set_signaling_message_receiver(NULL);
935 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 }
937
938 bool CreateTestClients() {
939 return CreateTestClients(NULL, NULL);
940 }
941
942 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
943 MediaConstraintsInterface* recv_constraints) {
944 initiating_client_.reset(SignalingClass::CreateClient("Caller: ",
945 init_constraints));
946 receiving_client_.reset(SignalingClass::CreateClient("Callee: ",
947 recv_constraints));
948 if (!initiating_client_ || !receiving_client_) {
949 return false;
950 }
951 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
952 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
953 return true;
954 }
955
956 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
957 const webrtc::FakeConstraints& recv_constraints) {
958 initiating_client_->SetVideoConstraints(init_constraints);
959 receiving_client_->SetVideoConstraints(recv_constraints);
960 }
961
962 void EnableVideoDecoderFactory() {
963 initiating_client_->EnableVideoDecoderFactory();
964 receiving_client_->EnableVideoDecoderFactory();
965 }
966
967 // This test sets up a call between two parties. Both parties send static
968 // frames to each other. Once the test is finished the number of sent frames
969 // is compared to the number of received frames.
970 void LocalP2PTest() {
971 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
972 initiating_client_->AddMediaStream(true, true);
973 }
974 initiating_client_->Negotiate();
975 const int kMaxWaitForActivationMs = 5000;
976 // Assert true is used here since next tests are guaranteed to fail and
977 // would eat up 5 seconds.
978 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
979 VerifySessionDescriptions();
980
981
982 int audio_frame_count = kEndAudioFrameCount;
983 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
984 if (!initiating_client_->can_receive_audio() ||
985 !receiving_client_->can_receive_audio()) {
986 audio_frame_count = -1;
987 }
988 int video_frame_count = kEndVideoFrameCount;
989 if (!initiating_client_->can_receive_video() ||
990 !receiving_client_->can_receive_video()) {
991 video_frame_count = -1;
992 }
993
994 if (audio_frame_count != -1 || video_frame_count != -1) {
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000995 // Audio or video is expected to flow, so both clients should reach the
996 // Connected state, and the offerer (ICE controller) should proceed to
997 // Completed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998 // Note: These tests have been observed to fail under heavy load at
999 // shorter timeouts, so they may be flaky.
1000 EXPECT_EQ_WAIT(
mallinath@webrtc.org385857d2014-02-14 00:56:12 +00001001 webrtc::PeerConnectionInterface::kIceConnectionCompleted,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001002 initiating_client_->ice_connection_state(),
1003 kMaxWaitForFramesMs);
1004 EXPECT_EQ_WAIT(
1005 webrtc::PeerConnectionInterface::kIceConnectionConnected,
1006 receiving_client_->ice_connection_state(),
1007 kMaxWaitForFramesMs);
1008 }
1009
1010 if (initiating_client_->can_receive_audio() ||
1011 initiating_client_->can_receive_video()) {
1012 // The initiating client can receive media, so it must produce candidates
1013 // that will serve as destinations for that media.
1014 // TODO(bemasc): Understand why the state is not already Complete here, as
1015 // seems to be the case for the receiving client. This may indicate a bug
1016 // in the ICE gathering system.
1017 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1018 initiating_client_->ice_gathering_state());
1019 }
1020 if (receiving_client_->can_receive_audio() ||
1021 receiving_client_->can_receive_video()) {
1022 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1023 receiving_client_->ice_gathering_state(),
1024 kMaxWaitForFramesMs);
1025 }
1026
1027 EXPECT_TRUE_WAIT(FramesNotPending(audio_frame_count, video_frame_count),
1028 kMaxWaitForFramesMs);
1029 }
1030
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001031 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1032 // Messages may get lost on the unreliable DataChannel, so we send multiple
1033 // times to avoid test flakiness.
1034 static const size_t kSendAttempts = 5;
1035
1036 for (size_t i = 0; i < kSendAttempts; ++i) {
1037 dc->Send(DataBuffer(data));
1038 }
1039 }
1040
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001041 // Wait until 'size' bytes of audio has been seen by the receiver, on the
1042 // first audio stream.
1043 void WaitForAudioData(int size) {
solenberg@webrtc.org00f11f52014-08-27 08:52:17 +00001044 const int kMaxWaitForAudioDataMs = 10000;
kjellander@webrtc.orge9bfed02014-08-25 19:46:26 +00001045
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001046 StreamCollectionInterface* local_streams =
1047 initializing_client()->local_streams();
1048 ASSERT_GT(local_streams->count(), 0u);
1049 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1050 MediaStreamTrackInterface* local_audio_track =
1051 local_streams->at(0)->GetAudioTracks()[0];
1052
1053 // Wait until *any* audio has been received.
1054 EXPECT_TRUE_WAIT(
1055 receiving_client()->GetBytesReceivedStats(local_audio_track) > 0,
1056 kMaxWaitForAudioDataMs);
1057
1058 // Wait until 'size' number of bytes have been received.
1059 size += receiving_client()->GetBytesReceivedStats(local_audio_track);
1060 EXPECT_TRUE_WAIT(
1061 receiving_client()->GetBytesReceivedStats(local_audio_track) > size,
1062 kMaxWaitForAudioDataMs);
1063 }
1064
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001065 SignalingClass* initializing_client() { return initiating_client_.get(); }
1066 SignalingClass* receiving_client() { return receiving_client_.get(); }
1067
1068 private:
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001069 rtc::scoped_ptr<rtc::PhysicalSocketServer> pss_;
1070 rtc::scoped_ptr<rtc::VirtualSocketServer> ss_;
1071 rtc::SocketServerScope ss_scope_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001072 rtc::scoped_ptr<SignalingClass> initiating_client_;
1073 rtc::scoped_ptr<SignalingClass> receiving_client_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001074};
1075typedef P2PTestConductor<JsepTestClient> JsepPeerConnectionP2PTestClient;
1076
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001077// Disable for TSan v2, see
1078// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1079#if !defined(THREAD_SANITIZER)
1080
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001081// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001082// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1083// See issue webrtc/2378.
1084TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001085 ASSERT_TRUE(CreateTestClients());
1086 LocalP2PTest();
1087 VerifyDtmf();
1088}
1089
1090// This test sets up a Jsep call between two parties and test that we can get a
1091// video aspect ratio of 16:9.
1092TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTest16To9) {
1093 ASSERT_TRUE(CreateTestClients());
1094 FakeConstraints constraint;
1095 double requested_ratio = 640.0/360;
1096 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1097 SetVideoConstraints(constraint, constraint);
1098 LocalP2PTest();
1099
1100 ASSERT_LE(0, initializing_client()->rendered_height());
1101 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001102 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001103 initializing_client()->rendered_height();
1104 EXPECT_LE(requested_ratio, initiating_video_ratio);
1105
1106 ASSERT_LE(0, receiving_client()->rendered_height());
1107 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001108 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001109 receiving_client()->rendered_height();
1110 EXPECT_LE(requested_ratio, receiving_video_ratio);
1111}
1112
1113// This test sets up a Jsep call between two parties and test that the
1114// received video has a resolution of 1280*720.
1115// TODO(mallinath): Enable when
1116// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
1117TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTest1280By720) {
1118 ASSERT_TRUE(CreateTestClients());
1119 FakeConstraints constraint;
1120 constraint.SetMandatoryMinWidth(1280);
1121 constraint.SetMandatoryMinHeight(720);
1122 SetVideoConstraints(constraint, constraint);
1123 LocalP2PTest();
1124 VerifyRenderedSize(1280, 720);
1125}
1126
1127// This test sets up a call between two endpoints that are configured to use
1128// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
1129TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtls) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001130 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001131 FakeConstraints setup_constraints;
1132 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1133 true);
1134 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1135 LocalP2PTest();
1136 VerifyRenderedSize(640, 480);
1137}
1138
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001139// This test sets up a audio call initially and then upgrades to audio/video,
1140// using DTLS.
mallinath@webrtc.org50bc5532013-10-21 17:58:35 +00001141TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001142 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001143 FakeConstraints setup_constraints;
1144 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1145 true);
1146 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1147 receiving_client()->SetReceiveAudioVideo(true, false);
1148 LocalP2PTest();
1149 receiving_client()->SetReceiveAudioVideo(true, true);
1150 receiving_client()->Negotiate();
1151}
1152
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001153// This test sets up a call between two endpoints that are configured to use
1154// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1155// negotiated and used for transport.
1156TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001157 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001158 FakeConstraints setup_constraints;
1159 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1160 true);
1161 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1162 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1163 LocalP2PTest();
1164 VerifyRenderedSize(640, 480);
1165}
1166
1167// This test sets up a Jsep call between two parties, and the callee only
1168// accept to receive video.
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00001169// BUG=https://code.google.com/p/webrtc/issues/detail?id=2288
1170TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001171 ASSERT_TRUE(CreateTestClients());
1172 receiving_client()->SetReceiveAudioVideo(false, true);
1173 LocalP2PTest();
1174}
1175
1176// This test sets up a Jsep call between two parties, and the callee only
1177// accept to receive audio.
henrike@webrtc.orgc0b1a282013-08-23 14:32:21 +00001178TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001179 ASSERT_TRUE(CreateTestClients());
1180 receiving_client()->SetReceiveAudioVideo(true, false);
1181 LocalP2PTest();
1182}
1183
1184// This test sets up a Jsep call between two parties, and the callee reject both
1185// audio and video.
1186TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestAnswerNone) {
1187 ASSERT_TRUE(CreateTestClients());
1188 receiving_client()->SetReceiveAudioVideo(false, false);
1189 LocalP2PTest();
1190}
1191
1192// This test sets up an audio and video call between two parties. After the call
1193// runs for a while (10 frames), the caller sends an update offer with video
1194// being rejected. Once the re-negotiation is done, the video flow should stop
1195// and the audio flow should continue.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001196// Disabled due to b/14955157.
1197TEST_F(JsepPeerConnectionP2PTestClient,
1198 DISABLED_UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001199 ASSERT_TRUE(CreateTestClients());
1200 LocalP2PTest();
1201 TestUpdateOfferWithRejectedContent();
1202}
1203
1204// This test sets up a Jsep call between two parties. The MSID is removed from
1205// the SDP strings from the caller.
buildbot@webrtc.org688ed692014-05-14 18:26:09 +00001206// Disabled due to b/14955157.
1207TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001208 ASSERT_TRUE(CreateTestClients());
1209 receiving_client()->RemoveMsidFromReceivedSdp(true);
1210 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1211 // audio and video is muxed when MSID is disabled. Remove
1212 // SetRemoveBundleFromSdp once
1213 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1214 receiving_client()->RemoveBundleFromReceivedSdp(true);
1215 LocalP2PTest();
1216}
1217
1218// This test sets up a Jsep call between two parties and the initiating peer
1219// sends two steams.
1220// TODO(perkj): Disabled due to
1221// https://code.google.com/p/webrtc/issues/detail?id=1454
1222TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_LocalP2PTestTwoStreams) {
1223 ASSERT_TRUE(CreateTestClients());
1224 // Set optional video constraint to max 320pixels to decrease CPU usage.
1225 FakeConstraints constraint;
1226 constraint.SetOptionalMaxWidth(320);
1227 SetVideoConstraints(constraint, constraint);
1228 initializing_client()->AddMediaStream(true, true);
1229 initializing_client()->AddMediaStream(false, true);
1230 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1231 LocalP2PTest();
1232 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1233}
1234
1235// Test that we can receive the audio output level from a remote audio track.
1236TEST_F(JsepPeerConnectionP2PTestClient, GetAudioOutputLevelStats) {
1237 ASSERT_TRUE(CreateTestClients());
1238 LocalP2PTest();
1239
1240 StreamCollectionInterface* remote_streams =
1241 initializing_client()->remote_streams();
1242 ASSERT_GT(remote_streams->count(), 0u);
1243 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1244 MediaStreamTrackInterface* remote_audio_track =
1245 remote_streams->at(0)->GetAudioTracks()[0];
1246
1247 // Get the audio output level stats. Note that the level is not available
1248 // until a RTCP packet has been received.
1249 EXPECT_TRUE_WAIT(
1250 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1251 kMaxWaitForStatsMs);
1252}
1253
1254// Test that an audio input level is reported.
1255TEST_F(JsepPeerConnectionP2PTestClient, GetAudioInputLevelStats) {
1256 ASSERT_TRUE(CreateTestClients());
1257 LocalP2PTest();
1258
1259 // Get the audio input level stats. The level should be available very
1260 // soon after the test starts.
1261 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1262 kMaxWaitForStatsMs);
1263}
1264
1265// Test that we can get incoming byte counts from both audio and video tracks.
1266TEST_F(JsepPeerConnectionP2PTestClient, GetBytesReceivedStats) {
1267 ASSERT_TRUE(CreateTestClients());
1268 LocalP2PTest();
1269
1270 StreamCollectionInterface* remote_streams =
1271 initializing_client()->remote_streams();
1272 ASSERT_GT(remote_streams->count(), 0u);
1273 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1274 MediaStreamTrackInterface* remote_audio_track =
1275 remote_streams->at(0)->GetAudioTracks()[0];
1276 EXPECT_TRUE_WAIT(
1277 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1278 kMaxWaitForStatsMs);
1279
1280 MediaStreamTrackInterface* remote_video_track =
1281 remote_streams->at(0)->GetVideoTracks()[0];
1282 EXPECT_TRUE_WAIT(
1283 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1284 kMaxWaitForStatsMs);
1285}
1286
1287// Test that we can get outgoing byte counts from both audio and video tracks.
1288TEST_F(JsepPeerConnectionP2PTestClient, GetBytesSentStats) {
1289 ASSERT_TRUE(CreateTestClients());
1290 LocalP2PTest();
1291
1292 StreamCollectionInterface* local_streams =
1293 initializing_client()->local_streams();
1294 ASSERT_GT(local_streams->count(), 0u);
1295 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1296 MediaStreamTrackInterface* local_audio_track =
1297 local_streams->at(0)->GetAudioTracks()[0];
1298 EXPECT_TRUE_WAIT(
1299 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1300 kMaxWaitForStatsMs);
1301
1302 MediaStreamTrackInterface* local_video_track =
1303 local_streams->at(0)->GetVideoTracks()[0];
1304 EXPECT_TRUE_WAIT(
1305 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1306 kMaxWaitForStatsMs);
1307}
1308
1309// This test sets up a call between two parties with audio, video and data.
1310TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestDataChannel) {
1311 FakeConstraints setup_constraints;
1312 setup_constraints.SetAllowRtpDataChannels();
1313 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1314 initializing_client()->CreateDataChannel();
1315 LocalP2PTest();
1316 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1317 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1318 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1319 kMaxWaitMs);
1320 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1321 kMaxWaitMs);
1322
1323 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001324
1325 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001326 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1327 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001328
1329 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1331 kMaxWaitMs);
1332
1333 receiving_client()->data_channel()->Close();
1334 // Send new offer and answer.
1335 receiving_client()->Negotiate();
1336 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1337 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1338}
1339
1340// This test sets up a call between two parties and creates a data channel.
1341// The test tests that received data is buffered unless an observer has been
1342// registered.
1343// Rtp data channels can receive data before the underlying
1344// transport has detected that a channel is writable and thus data can be
1345// received before the data channel state changes to open. That is hard to test
1346// but the same buffering is used in that case.
1347TEST_F(JsepPeerConnectionP2PTestClient, RegisterDataChannelObserver) {
1348 FakeConstraints setup_constraints;
1349 setup_constraints.SetAllowRtpDataChannels();
1350 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1351 initializing_client()->CreateDataChannel();
1352 initializing_client()->Negotiate();
1353
1354 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1355 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1356 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1357 kMaxWaitMs);
1358 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1359 receiving_client()->data_channel()->state(), kMaxWaitMs);
1360
1361 // Unregister the existing observer.
1362 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001363
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001365 SendRtpData(initializing_client()->data_channel(), data);
1366
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001367 // Wait a while to allow the sent data to arrive before an observer is
1368 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001369 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001370
1371 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1372 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1373}
1374
1375// This test sets up a call between two parties with audio, video and but only
1376// the initiating client support data.
1377TEST_F(JsepPeerConnectionP2PTestClient, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001378 FakeConstraints setup_constraints_1;
1379 setup_constraints_1.SetAllowRtpDataChannels();
1380 // Must disable DTLS to make negotiation succeed.
1381 setup_constraints_1.SetMandatory(
1382 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1383 FakeConstraints setup_constraints_2;
1384 setup_constraints_2.SetMandatory(
1385 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1386 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001387 initializing_client()->CreateDataChannel();
1388 LocalP2PTest();
1389 EXPECT_TRUE(initializing_client()->data_channel() != NULL);
1390 EXPECT_FALSE(receiving_client()->data_channel());
1391 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1392}
1393
1394// This test sets up a call between two parties with audio, video. When audio
1395// and video is setup and flowing and data channel is negotiated.
1396TEST_F(JsepPeerConnectionP2PTestClient, AddDataChannelAfterRenegotiation) {
1397 FakeConstraints setup_constraints;
1398 setup_constraints.SetAllowRtpDataChannels();
1399 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1400 LocalP2PTest();
1401 initializing_client()->CreateDataChannel();
1402 // Send new offer and answer.
1403 initializing_client()->Negotiate();
1404 ASSERT_TRUE(initializing_client()->data_channel() != NULL);
1405 ASSERT_TRUE(receiving_client()->data_channel() != NULL);
1406 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1407 kMaxWaitMs);
1408 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1409 kMaxWaitMs);
1410}
1411
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001412// This test sets up a Jsep call with SCTP DataChannel and verifies the
1413// negotiation is completed without error.
1414#ifdef HAVE_SCTP
1415TEST_F(JsepPeerConnectionP2PTestClient, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001416 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00001417 FakeConstraints constraints;
1418 constraints.SetMandatory(
1419 MediaConstraintsInterface::kEnableDtlsSrtp, true);
1420 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
1421 initializing_client()->CreateDataChannel();
1422 initializing_client()->Negotiate(false, false);
1423}
1424#endif
1425
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001426// This test sets up a call between two parties with audio, and video.
1427// During the call, the initializing side restart ice and the test verifies that
1428// new ice candidates are generated and audio and video still can flow.
1429TEST_F(JsepPeerConnectionP2PTestClient, IceRestart) {
1430 ASSERT_TRUE(CreateTestClients());
1431
1432 // Negotiate and wait for ice completion and make sure audio and video plays.
1433 LocalP2PTest();
1434
1435 // Create a SDP string of the first audio candidate for both clients.
1436 const webrtc::IceCandidateCollection* audio_candidates_initiator =
1437 initializing_client()->pc()->local_description()->candidates(0);
1438 const webrtc::IceCandidateCollection* audio_candidates_receiver =
1439 receiving_client()->pc()->local_description()->candidates(0);
1440 ASSERT_GT(audio_candidates_initiator->count(), 0u);
1441 ASSERT_GT(audio_candidates_receiver->count(), 0u);
1442 std::string initiator_candidate;
1443 EXPECT_TRUE(
1444 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
1445 std::string receiver_candidate;
1446 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
1447
1448 // Restart ice on the initializing client.
1449 receiving_client()->SetExpectIceRestart(true);
1450 initializing_client()->IceRestart();
1451
1452 // Negotiate and wait for ice completion again and make sure audio and video
1453 // plays.
1454 LocalP2PTest();
1455
1456 // Create a SDP string of the first audio candidate for both clients again.
1457 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
1458 initializing_client()->pc()->local_description()->candidates(0);
1459 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
1460 receiving_client()->pc()->local_description()->candidates(0);
1461 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
1462 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
1463 std::string initiator_candidate_restart;
1464 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
1465 &initiator_candidate_restart));
1466 std::string receiver_candidate_restart;
1467 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
1468 &receiver_candidate_restart));
1469
1470 // Verify that the first candidates in the local session descriptions has
1471 // changed.
1472 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
1473 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
1474}
1475
1476
1477// This test sets up a Jsep call between two parties with external
1478// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001479// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1480// See issue webrtc/2378.
1481TEST_F(JsepPeerConnectionP2PTestClient,
1482 DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483 ASSERT_TRUE(CreateTestClients());
1484 EnableVideoDecoderFactory();
1485 LocalP2PTest();
1486}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001487
1488// Test receive bandwidth stats with only audio enabled at receiver.
1489TEST_F(JsepPeerConnectionP2PTestClient, ReceivedBweStatsAudio) {
1490 ASSERT_TRUE(CreateTestClients());
1491 receiving_client()->SetReceiveAudioVideo(true, false);
1492 LocalP2PTest();
1493
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001494 // Wait until we have received some audio data. Following REMB shoud be zero.
1495 WaitForAudioData(10000);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001496 EXPECT_EQ_WAIT(
1497 receiving_client()->GetAvailableReceivedBandwidthStats(), 0,
1498 kMaxWaitForRembMs);
1499}
1500
1501// Test receive bandwidth stats with combined BWE.
asapersson@webrtc.org62662402014-09-29 14:30:07 +00001502// Disabled due to https://code.google.com/p/webrtc/issues/detail?id=3871.
1503TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_ReceivedBweStatsCombined) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001504 FakeConstraints setup_constraints;
1505 setup_constraints.AddOptional(
1506 MediaConstraintsInterface::kCombinedAudioVideoBwe, true);
1507 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1508 initializing_client()->AddMediaStream(true, true);
1509 initializing_client()->AddMediaStream(false, true);
1510 initializing_client()->AddMediaStream(false, true);
1511 initializing_client()->AddMediaStream(false, true);
1512 LocalP2PTest();
1513
1514 // Run until a non-zero bw is reported.
pbos@webrtc.org000d8672014-09-15 14:38:07 +00001515 EXPECT_TRUE_WAIT(receiving_client()->GetAvailableReceivedBandwidthStats() > 0,
1516 kMaxWaitForRembMs);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001517
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001518 // Halt video capturers, then run until we have gotten some audio. Following
1519 // REMB should be non-zero.
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001520 initializing_client()->StopVideoCapturers();
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001521 WaitForAudioData(10000);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001522 EXPECT_TRUE_WAIT(
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001523 receiving_client()->GetAvailableReceivedBandwidthStats() > 0,
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001524 kMaxWaitForRembMs);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001525}
1526
1527// Test receive bandwidth stats with 1 video, 3 audio streams but no combined
1528// BWE.
asapersson@webrtc.org62662402014-09-29 14:30:07 +00001529// Disabled due to https://code.google.com/p/webrtc/issues/detail?id=3871.
1530TEST_F(JsepPeerConnectionP2PTestClient, DISABLED_ReceivedBweStatsNotCombined) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001531 FakeConstraints setup_constraints;
1532 setup_constraints.AddOptional(
1533 MediaConstraintsInterface::kCombinedAudioVideoBwe, false);
1534 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1535 initializing_client()->AddMediaStream(true, true);
1536 initializing_client()->AddMediaStream(false, true);
1537 initializing_client()->AddMediaStream(false, true);
1538 initializing_client()->AddMediaStream(false, true);
1539 LocalP2PTest();
1540
1541 // Run until a non-zero bw is reported.
pbos@webrtc.org000d8672014-09-15 14:38:07 +00001542 EXPECT_TRUE_WAIT(receiving_client()->GetAvailableReceivedBandwidthStats() > 0,
1543 kMaxWaitForRembMs);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001544
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001545 // Halt video capturers, then run until we have gotten some audio. Following
1546 // REMB should be zero.
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001547 initializing_client()->StopVideoCapturers();
solenberg@webrtc.org6556a592014-08-25 14:35:40 +00001548 WaitForAudioData(10000);
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001549 EXPECT_EQ_WAIT(
1550 receiving_client()->GetAvailableReceivedBandwidthStats(), 0,
1551 kMaxWaitForRembMs);
1552}
1553
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001554#endif // if !defined(THREAD_SANITIZER)