blob: 406fab0d89eb6cc13d397a1ac668a87fca043b6e [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
11#include <stdio.h>
12
13#include <algorithm>
14#include <list>
15#include <map>
kwibergd1fe2812016-04-27 06:47:29 -070016#include <memory>
kwiberg0eb15ed2015-12-17 03:04:15 -080017#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018#include <vector>
19
Henrik Kjellander15583c12016-02-10 10:53:12 +010020#include "webrtc/api/dtmfsender.h"
21#include "webrtc/api/fakemetricsobserver.h"
22#include "webrtc/api/localaudiosource.h"
23#include "webrtc/api/mediastreaminterface.h"
24#include "webrtc/api/peerconnection.h"
25#include "webrtc/api/peerconnectionfactory.h"
26#include "webrtc/api/peerconnectioninterface.h"
27#include "webrtc/api/test/fakeaudiocapturemodule.h"
28#include "webrtc/api/test/fakeconstraints.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010029#include "webrtc/api/test/fakeperiodicvideocapturer.h"
Henrik Boströmd79599d2016-06-01 13:58:50 +020030#include "webrtc/api/test/fakertccertificategenerator.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010031#include "webrtc/api/test/fakevideotrackrenderer.h"
32#include "webrtc/api/test/mockpeerconnectionobservers.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000033#include "webrtc/base/gunit.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000034#include "webrtc/base/physicalsocketserver.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000035#include "webrtc/base/ssladapter.h"
36#include "webrtc/base/sslstreamadapter.h"
37#include "webrtc/base/thread.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000038#include "webrtc/base/virtualsocketserver.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010039#include "webrtc/media/engine/fakewebrtcvideoengine.h"
Taylor Brandstettera1c30352016-05-13 08:15:11 -070040#include "webrtc/p2p/base/fakeportallocator.h"
kjellanderf4752772016-03-02 05:42:30 -080041#include "webrtc/p2p/base/p2pconstants.h"
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +000042#include "webrtc/p2p/base/sessiondescription.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010043#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45#define MAYBE_SKIP_TEST(feature) \
46 if (!(feature())) { \
47 LOG(LS_INFO) << "Feature disabled... skipping"; \
48 return; \
49 }
50
51using cricket::ContentInfo;
52using cricket::FakeWebRtcVideoDecoder;
53using cricket::FakeWebRtcVideoDecoderFactory;
54using cricket::FakeWebRtcVideoEncoder;
55using cricket::FakeWebRtcVideoEncoderFactory;
56using cricket::MediaContentDescription;
57using webrtc::DataBuffer;
58using webrtc::DataChannelInterface;
59using webrtc::DtmfSender;
60using webrtc::DtmfSenderInterface;
61using webrtc::DtmfSenderObserverInterface;
62using webrtc::FakeConstraints;
63using webrtc::MediaConstraintsInterface;
deadbeeffaac4972015-11-12 15:33:07 -080064using webrtc::MediaStreamInterface;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065using webrtc::MediaStreamTrackInterface;
66using webrtc::MockCreateSessionDescriptionObserver;
67using webrtc::MockDataChannelObserver;
68using webrtc::MockSetSessionDescriptionObserver;
69using webrtc::MockStatsObserver;
deadbeeffaac4972015-11-12 15:33:07 -080070using webrtc::ObserverInterface;
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +000071using webrtc::PeerConnectionInterface;
Joachim Bauch04e5b492015-05-29 09:40:39 +020072using webrtc::PeerConnectionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073using webrtc::SessionDescriptionInterface;
74using webrtc::StreamCollectionInterface;
75
hta6b4f8392016-03-10 00:24:31 -080076namespace {
77
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000078static const int kMaxWaitMs = 10000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000079// Disable for TSan v2, see
80// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
81// This declaration is also #ifdef'd as it causes uninitialized-variable
82// warnings.
83#if !defined(THREAD_SANITIZER)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084static const int kMaxWaitForStatsMs = 3000;
pbos@webrtc.org044bdac2014-06-03 09:40:01 +000085#endif
deadbeeffac06552015-11-25 11:26:01 -080086static const int kMaxWaitForActivationMs = 5000;
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000087static const int kMaxWaitForFramesMs = 10000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088static const int kEndAudioFrameCount = 3;
89static const int kEndVideoFrameCount = 3;
90
91static const char kStreamLabelBase[] = "stream_label";
92static const char kVideoTrackLabelBase[] = "video_track";
93static const char kAudioTrackLabelBase[] = "audio_track";
94static const char kDataChannelLabel[] = "data_channel";
95
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +000096// Disable for TSan v2, see
97// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
98// This declaration is also #ifdef'd as it causes unused-variable errors.
99#if !defined(THREAD_SANITIZER)
100// SRTP cipher name negotiated by the tests. This must be updated if the
101// default changes.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800102static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
jbauchcb560652016-08-04 05:20:32 -0700103static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000104#endif
105
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106static 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
hta6b4f8392016-03-10 00:24:31 -0800117bool StreamsHaveAudioTrack(StreamCollectionInterface* streams) {
118 for (size_t idx = 0; idx < streams->count(); idx++) {
119 auto stream = streams->at(idx);
120 if (stream->GetAudioTracks().size() > 0) {
121 return true;
122 }
123 }
124 return false;
125}
126
127bool StreamsHaveVideoTrack(StreamCollectionInterface* streams) {
128 for (size_t idx = 0; idx < streams->count(); idx++) {
129 auto stream = streams->at(idx);
130 if (stream->GetVideoTracks().size() > 0) {
131 return true;
132 }
133 }
134 return false;
135}
136
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137class SignalingMessageReceiver {
138 public:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 virtual void ReceiveSdpMessage(const std::string& type,
140 std::string& msg) = 0;
141 virtual void ReceiveIceMessage(const std::string& sdp_mid,
142 int sdp_mline_index,
143 const std::string& msg) = 0;
144
145 protected:
deadbeefaf1b59c2015-10-15 12:08:41 -0700146 SignalingMessageReceiver() {}
147 virtual ~SignalingMessageReceiver() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148};
149
zhihuang184a3fd2016-06-14 11:47:14 -0700150class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
151 public:
152 MockRtpReceiverObserver(cricket::MediaType media_type)
153 : expected_media_type_(media_type) {}
154
155 void OnFirstPacketReceived(cricket::MediaType media_type) override {
156 ASSERT_EQ(expected_media_type_, media_type);
157 first_packet_received_ = true;
158 }
159
160 bool first_packet_received() { return first_packet_received_; }
161
162 virtual ~MockRtpReceiverObserver() {}
163
164 private:
165 bool first_packet_received_ = false;
166 cricket::MediaType expected_media_type_;
167};
168
deadbeefaf1b59c2015-10-15 12:08:41 -0700169class PeerConnectionTestClient : public webrtc::PeerConnectionObserver,
deadbeeffaac4972015-11-12 15:33:07 -0800170 public SignalingMessageReceiver,
171 public ObserverInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 public:
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800173 static PeerConnectionTestClient* CreateClientWithDtlsIdentityStore(
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800174 const std::string& id,
175 const MediaConstraintsInterface* constraints,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800176 const PeerConnectionFactory::Options* options,
zhihuang34b54c32016-08-04 11:06:50 -0700177 const PeerConnectionInterface::RTCConfiguration& config,
Henrik Boströmd79599d2016-06-01 13:58:50 +0200178 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
perkj8aba9972016-04-10 23:54:34 -0700179 bool prefer_constraint_apis,
danilchape9021a32016-05-17 01:52:02 -0700180 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700181 rtc::Thread* worker_thread) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800182 PeerConnectionTestClient* client(new PeerConnectionTestClient(id));
zhihuang34b54c32016-08-04 11:06:50 -0700183 if (!client->Init(constraints, options, config, std::move(cert_generator),
danilchape9021a32016-05-17 01:52:02 -0700184 prefer_constraint_apis, network_thread, worker_thread)) {
Guo-wei Shieh86aaa4b2015-12-05 09:55:44 -0800185 delete client;
186 return nullptr;
187 }
188 return client;
Guo-wei Shieh9c38c2d2015-12-05 09:46:07 -0800189 }
190
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800191 static PeerConnectionTestClient* CreateClient(
192 const std::string& id,
193 const MediaConstraintsInterface* constraints,
perkj8aba9972016-04-10 23:54:34 -0700194 const PeerConnectionFactory::Options* options,
zhihuang34b54c32016-08-04 11:06:50 -0700195 const PeerConnectionInterface::RTCConfiguration& config,
danilchape9021a32016-05-17 01:52:02 -0700196 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700197 rtc::Thread* worker_thread) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200198 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
199 rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
200 new FakeRTCCertificateGenerator() : nullptr);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800201
zhihuang34b54c32016-08-04 11:06:50 -0700202 return CreateClientWithDtlsIdentityStore(id, constraints, options, config,
203 std::move(cert_generator), true,
204 network_thread, worker_thread);
htaaac2dea2016-03-10 13:35:55 -0800205 }
206
207 static PeerConnectionTestClient* CreateClientPreferNoConstraints(
208 const std::string& id,
perkj8aba9972016-04-10 23:54:34 -0700209 const PeerConnectionFactory::Options* options,
zhihuang34b54c32016-08-04 11:06:50 -0700210 const PeerConnectionInterface::RTCConfiguration& config,
danilchape9021a32016-05-17 01:52:02 -0700211 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700212 rtc::Thread* worker_thread) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200213 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
214 rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
215 new FakeRTCCertificateGenerator() : nullptr);
htaaac2dea2016-03-10 13:35:55 -0800216
zhihuang34b54c32016-08-04 11:06:50 -0700217 return CreateClientWithDtlsIdentityStore(id, nullptr, options, config,
218 std::move(cert_generator), false,
219 network_thread, worker_thread);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800220 }
221
deadbeefaf1b59c2015-10-15 12:08:41 -0700222 ~PeerConnectionTestClient() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223 }
224
deadbeefaf1b59c2015-10-15 12:08:41 -0700225 void Negotiate() { Negotiate(true, true); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226
deadbeefaf1b59c2015-10-15 12:08:41 -0700227 void Negotiate(bool audio, bool video) {
kwibergd1fe2812016-04-27 06:47:29 -0700228 std::unique_ptr<SessionDescriptionInterface> offer;
kwiberg2bbff992016-03-16 11:03:04 -0700229 ASSERT_TRUE(DoCreateOffer(&offer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230
deadbeefaf1b59c2015-10-15 12:08:41 -0700231 if (offer->description()->GetContentByName("audio")) {
232 offer->description()->GetContentByName("audio")->rejected = !audio;
233 }
234 if (offer->description()->GetContentByName("video")) {
235 offer->description()->GetContentByName("video")->rejected = !video;
236 }
237
238 std::string sdp;
239 EXPECT_TRUE(offer->ToString(&sdp));
240 EXPECT_TRUE(DoSetLocalDescription(offer.release()));
241 signaling_message_receiver_->ReceiveSdpMessage(
242 webrtc::SessionDescriptionInterface::kOffer, sdp);
243 }
244
245 // SignalingMessageReceiver callback.
246 void ReceiveSdpMessage(const std::string& type, std::string& msg) override {
247 FilterIncomingSdpMessage(&msg);
248 if (type == webrtc::SessionDescriptionInterface::kOffer) {
249 HandleIncomingOffer(msg);
250 } else {
251 HandleIncomingAnswer(msg);
252 }
253 }
254
255 // SignalingMessageReceiver callback.
256 void ReceiveIceMessage(const std::string& sdp_mid,
257 int sdp_mline_index,
258 const std::string& msg) override {
259 LOG(INFO) << id_ << "ReceiveIceMessage";
kwibergd1fe2812016-04-27 06:47:29 -0700260 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
deadbeefaf1b59c2015-10-15 12:08:41 -0700261 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
262 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
263 }
264
265 // PeerConnectionObserver callbacks.
266 void OnSignalingChange(
267 webrtc::PeerConnectionInterface::SignalingState new_state) override {
268 EXPECT_EQ(pc()->signaling_state(), new_state);
269 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700270 void OnAddStream(
271 rtc::scoped_refptr<MediaStreamInterface> media_stream) override {
deadbeeffaac4972015-11-12 15:33:07 -0800272 media_stream->RegisterObserver(this);
deadbeefaf1b59c2015-10-15 12:08:41 -0700273 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
274 const std::string id = media_stream->GetVideoTracks()[i]->id();
275 ASSERT_TRUE(fake_video_renderers_.find(id) ==
276 fake_video_renderers_.end());
deadbeefc9be0072015-12-14 18:27:57 -0800277 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
278 media_stream->GetVideoTracks()[i]));
deadbeefaf1b59c2015-10-15 12:08:41 -0700279 }
280 }
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700281 void OnRemoveStream(
282 rtc::scoped_refptr<MediaStreamInterface> media_stream) override {}
deadbeefaf1b59c2015-10-15 12:08:41 -0700283 void OnRenegotiationNeeded() override {}
284 void OnIceConnectionChange(
285 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
286 EXPECT_EQ(pc()->ice_connection_state(), new_state);
287 }
288 void OnIceGatheringChange(
289 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
290 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
291 }
292 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
293 LOG(INFO) << id_ << "OnIceCandidate";
294
295 std::string ice_sdp;
296 EXPECT_TRUE(candidate->ToString(&ice_sdp));
297 if (signaling_message_receiver_ == nullptr) {
298 // Remote party may be deleted.
299 return;
300 }
301 signaling_message_receiver_->ReceiveIceMessage(
302 candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
303 }
304
deadbeeffaac4972015-11-12 15:33:07 -0800305 // MediaStreamInterface callback
306 void OnChanged() override {
307 // Track added or removed from MediaStream, so update our renderers.
308 rtc::scoped_refptr<StreamCollectionInterface> remote_streams =
309 pc()->remote_streams();
310 // Remove renderers for tracks that were removed.
311 for (auto it = fake_video_renderers_.begin();
312 it != fake_video_renderers_.end();) {
313 if (remote_streams->FindVideoTrack(it->first) == nullptr) {
deadbeefc9be0072015-12-14 18:27:57 -0800314 auto to_remove = it++;
315 removed_fake_video_renderers_.push_back(std::move(to_remove->second));
316 fake_video_renderers_.erase(to_remove);
deadbeeffaac4972015-11-12 15:33:07 -0800317 } else {
318 ++it;
319 }
320 }
321 // Create renderers for new video tracks.
322 for (size_t stream_index = 0; stream_index < remote_streams->count();
323 ++stream_index) {
324 MediaStreamInterface* remote_stream = remote_streams->at(stream_index);
325 for (size_t track_index = 0;
326 track_index < remote_stream->GetVideoTracks().size();
327 ++track_index) {
328 const std::string id =
329 remote_stream->GetVideoTracks()[track_index]->id();
330 if (fake_video_renderers_.find(id) != fake_video_renderers_.end()) {
331 continue;
332 }
deadbeefc9be0072015-12-14 18:27:57 -0800333 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
334 remote_stream->GetVideoTracks()[track_index]));
deadbeeffaac4972015-11-12 15:33:07 -0800335 }
336 }
337 }
338
deadbeefaf1b59c2015-10-15 12:08:41 -0700339 void SetVideoConstraints(const webrtc::FakeConstraints& video_constraint) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340 video_constraints_ = video_constraint;
341 }
342
343 void AddMediaStream(bool audio, bool video) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700344 std::string stream_label =
345 kStreamLabelBase +
346 rtc::ToString<int>(static_cast<int>(pc()->local_streams()->count()));
deadbeeffaac4972015-11-12 15:33:07 -0800347 rtc::scoped_refptr<MediaStreamInterface> stream =
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000348 peer_connection_factory_->CreateLocalMediaStream(stream_label);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349
350 if (audio && can_receive_audio()) {
deadbeeffac06552015-11-25 11:26:01 -0800351 stream->AddTrack(CreateLocalAudioTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352 }
353 if (video && can_receive_video()) {
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000354 stream->AddTrack(CreateLocalVideoTrack(stream_label));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355 }
356
deadbeefaf1b59c2015-10-15 12:08:41 -0700357 EXPECT_TRUE(pc()->AddStream(stream));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358 }
359
deadbeefaf1b59c2015-10-15 12:08:41 -0700360 size_t NumberOfLocalMediaStreams() { return pc()->local_streams()->count(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361
362 bool SessionActive() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700363 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364 }
365
deadbeeffaac4972015-11-12 15:33:07 -0800366 // Automatically add a stream when receiving an offer, if we don't have one.
367 // Defaults to true.
368 void set_auto_add_stream(bool auto_add_stream) {
369 auto_add_stream_ = auto_add_stream;
370 }
371
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000372 void set_signaling_message_receiver(
deadbeefaf1b59c2015-10-15 12:08:41 -0700373 SignalingMessageReceiver* signaling_message_receiver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374 signaling_message_receiver_ = signaling_message_receiver;
375 }
376
377 void EnableVideoDecoderFactory() {
378 video_decoder_factory_enabled_ = true;
379 fake_video_decoder_factory_->AddSupportedVideoCodecType(
380 webrtc::kVideoCodecVP8);
381 }
382
deadbeefaf1b59c2015-10-15 12:08:41 -0700383 void IceRestart() {
htaaac2dea2016-03-10 13:35:55 -0800384 offer_answer_constraints_.SetMandatoryIceRestart(true);
385 offer_answer_options_.ice_restart = true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700386 SetExpectIceRestart(true);
387 }
388
389 void SetExpectIceRestart(bool expect_restart) {
390 expect_ice_restart_ = expect_restart;
391 }
392
393 bool ExpectIceRestart() const { return expect_ice_restart_; }
394
395 void SetReceiveAudioVideo(bool audio, bool video) {
396 SetReceiveAudio(audio);
397 SetReceiveVideo(video);
398 ASSERT_EQ(audio, can_receive_audio());
399 ASSERT_EQ(video, can_receive_video());
400 }
401
402 void SetReceiveAudio(bool audio) {
403 if (audio && can_receive_audio())
404 return;
htaaac2dea2016-03-10 13:35:55 -0800405 offer_answer_constraints_.SetMandatoryReceiveAudio(audio);
406 offer_answer_options_.offer_to_receive_audio = audio ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700407 }
408
409 void SetReceiveVideo(bool video) {
410 if (video && can_receive_video())
411 return;
htaaac2dea2016-03-10 13:35:55 -0800412 offer_answer_constraints_.SetMandatoryReceiveVideo(video);
413 offer_answer_options_.offer_to_receive_video = video ? 1 : 0;
deadbeefaf1b59c2015-10-15 12:08:41 -0700414 }
415
416 void RemoveMsidFromReceivedSdp(bool remove) { remove_msid_ = remove; }
417
418 void RemoveSdesCryptoFromReceivedSdp(bool remove) { remove_sdes_ = remove; }
419
420 void RemoveBundleFromReceivedSdp(bool remove) { remove_bundle_ = remove; }
421
perkjcaafdba2016-03-20 07:34:29 -0700422 void RemoveCvoFromReceivedSdp(bool remove) { remove_cvo_ = remove; }
423
deadbeefaf1b59c2015-10-15 12:08:41 -0700424 bool can_receive_audio() {
425 bool value;
htaaac2dea2016-03-10 13:35:55 -0800426 if (prefer_constraint_apis_) {
427 if (webrtc::FindConstraint(
428 &offer_answer_constraints_,
429 MediaConstraintsInterface::kOfferToReceiveAudio, &value,
430 nullptr)) {
431 return value;
432 }
433 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700434 }
htaaac2dea2016-03-10 13:35:55 -0800435 return offer_answer_options_.offer_to_receive_audio > 0 ||
436 offer_answer_options_.offer_to_receive_audio ==
437 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700438 }
439
440 bool can_receive_video() {
441 bool value;
htaaac2dea2016-03-10 13:35:55 -0800442 if (prefer_constraint_apis_) {
443 if (webrtc::FindConstraint(
444 &offer_answer_constraints_,
445 MediaConstraintsInterface::kOfferToReceiveVideo, &value,
446 nullptr)) {
447 return value;
448 }
449 return true;
deadbeefaf1b59c2015-10-15 12:08:41 -0700450 }
htaaac2dea2016-03-10 13:35:55 -0800451 return offer_answer_options_.offer_to_receive_video > 0 ||
452 offer_answer_options_.offer_to_receive_video ==
453 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined;
deadbeefaf1b59c2015-10-15 12:08:41 -0700454 }
455
Taylor Brandstetter98cde262016-05-31 13:02:21 -0700456 void OnDataChannel(
457 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
deadbeefaf1b59c2015-10-15 12:08:41 -0700458 LOG(INFO) << id_ << "OnDataChannel";
459 data_channel_ = data_channel;
460 data_observer_.reset(new MockDataChannelObserver(data_channel));
461 }
462
zhihuang34b54c32016-08-04 11:06:50 -0700463 void CreateDataChannel() { CreateDataChannel(nullptr); }
464
465 void CreateDataChannel(const webrtc::DataChannelInit* init) {
466 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, init);
deadbeefaf1b59c2015-10-15 12:08:41 -0700467 ASSERT_TRUE(data_channel_.get() != nullptr);
468 data_observer_.reset(new MockDataChannelObserver(data_channel_));
469 }
470
deadbeeffac06552015-11-25 11:26:01 -0800471 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack(
472 const std::string& stream_label) {
473 FakeConstraints constraints;
474 // Disable highpass filter so that we can get all the test audio frames.
475 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
476 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
477 peer_connection_factory_->CreateAudioSource(&constraints);
478 // TODO(perkj): Test audio source when it is implemented. Currently audio
479 // always use the default input.
480 std::string label = stream_label + kAudioTrackLabelBase;
481 return peer_connection_factory_->CreateAudioTrack(label, source);
482 }
483
484 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack(
485 const std::string& stream_label) {
486 // Set max frame rate to 10fps to reduce the risk of the tests to be flaky.
487 FakeConstraints source_constraints = video_constraints_;
488 source_constraints.SetMandatoryMaxFrameRate(10);
489
490 cricket::FakeVideoCapturer* fake_capturer =
491 new webrtc::FakePeriodicVideoCapturer();
perkjcaafdba2016-03-20 07:34:29 -0700492 fake_capturer->SetRotation(capture_rotation_);
deadbeeffac06552015-11-25 11:26:01 -0800493 video_capturers_.push_back(fake_capturer);
perkja3ede6c2016-03-08 01:27:48 +0100494 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
deadbeeffac06552015-11-25 11:26:01 -0800495 peer_connection_factory_->CreateVideoSource(fake_capturer,
496 &source_constraints);
497 std::string label = stream_label + kVideoTrackLabelBase;
perkjcaafdba2016-03-20 07:34:29 -0700498
499 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
500 peer_connection_factory_->CreateVideoTrack(label, source));
501 if (!local_video_renderer_) {
502 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
503 }
504 return track;
deadbeeffac06552015-11-25 11:26:01 -0800505 }
506
deadbeefaf1b59c2015-10-15 12:08:41 -0700507 DataChannelInterface* data_channel() { return data_channel_; }
508 const MockDataChannelObserver* data_observer() const {
509 return data_observer_.get();
510 }
511
hta6b4f8392016-03-10 00:24:31 -0800512 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
deadbeefaf1b59c2015-10-15 12:08:41 -0700513
514 void StopVideoCapturers() {
perkjcaafdba2016-03-20 07:34:29 -0700515 for (auto* capturer : video_capturers_) {
516 capturer->Stop();
deadbeefaf1b59c2015-10-15 12:08:41 -0700517 }
518 }
519
perkjcaafdba2016-03-20 07:34:29 -0700520 void SetCaptureRotation(webrtc::VideoRotation rotation) {
521 ASSERT_TRUE(video_capturers_.empty());
522 capture_rotation_ = rotation;
523 }
524
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525 bool AudioFramesReceivedCheck(int number_of_frames) const {
526 return number_of_frames <= fake_audio_capture_module_->frames_received();
527 }
528
deadbeefc9be0072015-12-14 18:27:57 -0800529 int audio_frames_received() const {
530 return fake_audio_capture_module_->frames_received();
531 }
532
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533 bool VideoFramesReceivedCheck(int number_of_frames) {
534 if (video_decoder_factory_enabled_) {
535 const std::vector<FakeWebRtcVideoDecoder*>& decoders
536 = fake_video_decoder_factory_->decoders();
537 if (decoders.empty()) {
538 return number_of_frames <= 0;
539 }
hta6b4f8392016-03-10 00:24:31 -0800540 // Note - this checks that EACH decoder has the requisite number
541 // of frames. The video_frames_received() function sums them.
deadbeefc9be0072015-12-14 18:27:57 -0800542 for (FakeWebRtcVideoDecoder* decoder : decoders) {
543 if (number_of_frames > decoder->GetNumFramesReceived()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544 return false;
545 }
546 }
547 return true;
548 } else {
549 if (fake_video_renderers_.empty()) {
550 return number_of_frames <= 0;
551 }
552
deadbeefc9be0072015-12-14 18:27:57 -0800553 for (const auto& pair : fake_video_renderers_) {
554 if (number_of_frames > pair.second->num_rendered_frames()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 return false;
556 }
557 }
558 return true;
559 }
560 }
deadbeefaf1b59c2015-10-15 12:08:41 -0700561
deadbeefc9be0072015-12-14 18:27:57 -0800562 int video_frames_received() const {
563 int total = 0;
564 if (video_decoder_factory_enabled_) {
565 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
566 fake_video_decoder_factory_->decoders();
567 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
568 total += decoder->GetNumFramesReceived();
569 }
570 } else {
571 for (const auto& pair : fake_video_renderers_) {
572 total += pair.second->num_rendered_frames();
573 }
574 for (const auto& renderer : removed_fake_video_renderers_) {
575 total += renderer->num_rendered_frames();
576 }
577 }
578 return total;
579 }
580
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 // Verify the CreateDtmfSender interface
582 void VerifyDtmf() {
kwibergd1fe2812016-04-27 06:47:29 -0700583 std::unique_ptr<DummyDtmfObserver> observer(new DummyDtmfObserver());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000584 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585
586 // We can't create a DTMF sender with an invalid audio track or a non local
587 // track.
deadbeefaf1b59c2015-10-15 12:08:41 -0700588 EXPECT_TRUE(peer_connection_->CreateDtmfSender(nullptr) == nullptr);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000589 rtc::scoped_refptr<webrtc::AudioTrackInterface> non_localtrack(
deadbeefaf1b59c2015-10-15 12:08:41 -0700590 peer_connection_factory_->CreateAudioTrack("dummy_track", nullptr));
591 EXPECT_TRUE(peer_connection_->CreateDtmfSender(non_localtrack) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000592
593 // We should be able to create a DTMF sender from a local track.
594 webrtc::AudioTrackInterface* localtrack =
595 peer_connection_->local_streams()->at(0)->GetAudioTracks()[0];
596 dtmf_sender = peer_connection_->CreateDtmfSender(localtrack);
deadbeefaf1b59c2015-10-15 12:08:41 -0700597 EXPECT_TRUE(dtmf_sender.get() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598 dtmf_sender->RegisterObserver(observer.get());
599
600 // Test the DtmfSender object just created.
601 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
602 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
603
604 // We don't need to verify that the DTMF tones are actually sent out because
605 // that is already covered by the tests of the lower level components.
606
607 EXPECT_TRUE_WAIT(observer->completed(), kMaxWaitMs);
608 std::vector<std::string> tones;
609 tones.push_back("1");
610 tones.push_back("a");
611 tones.push_back("");
612 observer->Verify(tones);
613
614 dtmf_sender->UnregisterObserver();
615 }
616
617 // Verifies that the SessionDescription have rejected the appropriate media
618 // content.
619 void VerifyRejectedMediaInSessionDescription() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700620 ASSERT_TRUE(peer_connection_->remote_description() != nullptr);
621 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 const cricket::SessionDescription* remote_desc =
623 peer_connection_->remote_description()->description();
624 const cricket::SessionDescription* local_desc =
625 peer_connection_->local_description()->description();
626
627 const ContentInfo* remote_audio_content = GetFirstAudioContent(remote_desc);
628 if (remote_audio_content) {
629 const ContentInfo* audio_content =
630 GetFirstAudioContent(local_desc);
631 EXPECT_EQ(can_receive_audio(), !audio_content->rejected);
632 }
633
634 const ContentInfo* remote_video_content = GetFirstVideoContent(remote_desc);
635 if (remote_video_content) {
636 const ContentInfo* video_content =
637 GetFirstVideoContent(local_desc);
638 EXPECT_EQ(can_receive_video(), !video_content->rejected);
639 }
640 }
641
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 void VerifyLocalIceUfragAndPassword() {
deadbeefaf1b59c2015-10-15 12:08:41 -0700643 ASSERT_TRUE(peer_connection_->local_description() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000644 const cricket::SessionDescription* desc =
645 peer_connection_->local_description()->description();
646 const cricket::ContentInfos& contents = desc->contents();
647
648 for (size_t index = 0; index < contents.size(); ++index) {
649 if (contents[index].rejected)
650 continue;
651 const cricket::TransportDescription* transport_desc =
652 desc->GetTransportDescriptionByName(contents[index].name);
653
654 std::map<int, IceUfragPwdPair>::const_iterator ufragpair_it =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000655 ice_ufrag_pwd_.find(static_cast<int>(index));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000656 if (ufragpair_it == ice_ufrag_pwd_.end()) {
657 ASSERT_FALSE(ExpectIceRestart());
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000658 ice_ufrag_pwd_[static_cast<int>(index)] =
659 IceUfragPwdPair(transport_desc->ice_ufrag, transport_desc->ice_pwd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660 } else if (ExpectIceRestart()) {
661 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
662 EXPECT_NE(ufrag_pwd.first, transport_desc->ice_ufrag);
663 EXPECT_NE(ufrag_pwd.second, transport_desc->ice_pwd);
664 } else {
665 const IceUfragPwdPair& ufrag_pwd = ufragpair_it->second;
666 EXPECT_EQ(ufrag_pwd.first, transport_desc->ice_ufrag);
667 EXPECT_EQ(ufrag_pwd.second, transport_desc->ice_pwd);
668 }
669 }
670 }
671
672 int GetAudioOutputLevelStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000673 rtc::scoped_refptr<MockStatsObserver>
674 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000675 EXPECT_TRUE(peer_connection_->GetStats(
676 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700678 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679 return observer->AudioOutputLevel();
680 }
681
682 int GetAudioInputLevelStats() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000683 rtc::scoped_refptr<MockStatsObserver>
684 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000685 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700686 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700688 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 return observer->AudioInputLevel();
690 }
691
692 int GetBytesReceivedStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000693 rtc::scoped_refptr<MockStatsObserver>
694 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000695 EXPECT_TRUE(peer_connection_->GetStats(
696 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700698 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699 return observer->BytesReceived();
700 }
701
702 int GetBytesSentStats(webrtc::MediaStreamTrackInterface* track) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000703 rtc::scoped_refptr<MockStatsObserver>
704 observer(new rtc::RefCountedObject<MockStatsObserver>());
jiayl@webrtc.orgdb41b4d2014-03-03 21:30:06 +0000705 EXPECT_TRUE(peer_connection_->GetStats(
706 observer, track, PeerConnectionInterface::kStatsOutputLevelStandard));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700708 EXPECT_NE(0, observer->timestamp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 return observer->BytesSent();
710 }
711
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000712 int GetAvailableReceivedBandwidthStats() {
713 rtc::scoped_refptr<MockStatsObserver>
714 observer(new rtc::RefCountedObject<MockStatsObserver>());
715 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700716 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000717 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700718 EXPECT_NE(0, observer->timestamp());
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000719 int bw = observer->AvailableReceiveBandwidth();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +0000720 return bw;
721 }
722
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000723 std::string GetDtlsCipherStats() {
724 rtc::scoped_refptr<MockStatsObserver>
725 observer(new rtc::RefCountedObject<MockStatsObserver>());
726 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700727 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000728 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700729 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000730 return observer->DtlsCipher();
731 }
732
733 std::string GetSrtpCipherStats() {
734 rtc::scoped_refptr<MockStatsObserver>
735 observer(new rtc::RefCountedObject<MockStatsObserver>());
736 EXPECT_TRUE(peer_connection_->GetStats(
deadbeefaf1b59c2015-10-15 12:08:41 -0700737 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000738 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
jbauchbe24c942015-06-22 15:06:43 -0700739 EXPECT_NE(0, observer->timestamp());
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +0000740 return observer->SrtpCipher();
741 }
742
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 int rendered_width() {
744 EXPECT_FALSE(fake_video_renderers_.empty());
745 return fake_video_renderers_.empty() ? 1 :
746 fake_video_renderers_.begin()->second->width();
747 }
748
749 int rendered_height() {
750 EXPECT_FALSE(fake_video_renderers_.empty());
751 return fake_video_renderers_.empty() ? 1 :
752 fake_video_renderers_.begin()->second->height();
753 }
754
perkjcaafdba2016-03-20 07:34:29 -0700755 webrtc::VideoRotation rendered_rotation() {
756 EXPECT_FALSE(fake_video_renderers_.empty());
757 return fake_video_renderers_.empty()
758 ? webrtc::kVideoRotation_0
759 : fake_video_renderers_.begin()->second->rotation();
760 }
761
762 int local_rendered_width() {
763 return local_video_renderer_ ? local_video_renderer_->width() : 1;
764 }
765
766 int local_rendered_height() {
767 return local_video_renderer_ ? local_video_renderer_->height() : 1;
768 }
769
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 size_t number_of_remote_streams() {
771 if (!pc())
772 return 0;
773 return pc()->remote_streams()->count();
774 }
775
hta6b4f8392016-03-10 00:24:31 -0800776 StreamCollectionInterface* remote_streams() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 if (!pc()) {
778 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700779 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 }
781 return pc()->remote_streams();
782 }
783
784 StreamCollectionInterface* local_streams() {
785 if (!pc()) {
786 ADD_FAILURE();
deadbeefaf1b59c2015-10-15 12:08:41 -0700787 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 }
789 return pc()->local_streams();
790 }
791
hta6b4f8392016-03-10 00:24:31 -0800792 bool HasLocalAudioTrack() { return StreamsHaveAudioTrack(local_streams()); }
793
794 bool HasLocalVideoTrack() { return StreamsHaveVideoTrack(local_streams()); }
795
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
797 return pc()->signaling_state();
798 }
799
800 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
801 return pc()->ice_connection_state();
802 }
803
804 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
805 return pc()->ice_gathering_state();
806 }
807
zhihuang184a3fd2016-06-14 11:47:14 -0700808 std::vector<std::unique_ptr<MockRtpReceiverObserver>> const&
809 rtp_receiver_observers() {
810 return rtp_receiver_observers_;
811 }
812
813 void SetRtpReceiverObservers() {
814 rtp_receiver_observers_.clear();
815 for (auto receiver : pc()->GetReceivers()) {
816 std::unique_ptr<MockRtpReceiverObserver> observer(
817 new MockRtpReceiverObserver(receiver->media_type()));
818 receiver->SetObserver(observer.get());
819 rtp_receiver_observers_.push_back(std::move(observer));
820 }
821 }
822
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 private:
824 class DummyDtmfObserver : public DtmfSenderObserverInterface {
825 public:
826 DummyDtmfObserver() : completed_(false) {}
827
828 // Implements DtmfSenderObserverInterface.
deadbeefaf1b59c2015-10-15 12:08:41 -0700829 void OnToneChange(const std::string& tone) override {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 tones_.push_back(tone);
831 if (tone.empty()) {
832 completed_ = true;
833 }
834 }
835
836 void Verify(const std::vector<std::string>& tones) const {
837 ASSERT_TRUE(tones_.size() == tones.size());
838 EXPECT_TRUE(std::equal(tones.begin(), tones.end(), tones_.begin()));
839 }
840
841 bool completed() const { return completed_; }
842
843 private:
844 bool completed_;
845 std::vector<std::string> tones_;
846 };
847
deadbeefaf1b59c2015-10-15 12:08:41 -0700848 explicit PeerConnectionTestClient(const std::string& id) : id_(id) {}
849
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800850 bool Init(
851 const MediaConstraintsInterface* constraints,
852 const PeerConnectionFactory::Options* options,
zhihuang34b54c32016-08-04 11:06:50 -0700853 const PeerConnectionInterface::RTCConfiguration& config,
Henrik Boströmd79599d2016-06-01 13:58:50 +0200854 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
perkj8aba9972016-04-10 23:54:34 -0700855 bool prefer_constraint_apis,
danilchape9021a32016-05-17 01:52:02 -0700856 rtc::Thread* network_thread,
perkj8aba9972016-04-10 23:54:34 -0700857 rtc::Thread* worker_thread) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700858 EXPECT_TRUE(!peer_connection_);
859 EXPECT_TRUE(!peer_connection_factory_);
htaaac2dea2016-03-10 13:35:55 -0800860 if (!prefer_constraint_apis) {
861 EXPECT_TRUE(!constraints);
862 }
863 prefer_constraint_apis_ = prefer_constraint_apis;
864
kwibergd1fe2812016-04-27 06:47:29 -0700865 std::unique_ptr<cricket::PortAllocator> port_allocator(
danilchape9021a32016-05-17 01:52:02 -0700866 new cricket::FakePortAllocator(network_thread, nullptr));
deadbeefaf1b59c2015-10-15 12:08:41 -0700867 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
868
869 if (fake_audio_capture_module_ == nullptr) {
870 return false;
871 }
872 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
873 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
danilchape9021a32016-05-17 01:52:02 -0700874 rtc::Thread* const signaling_thread = rtc::Thread::Current();
deadbeefaf1b59c2015-10-15 12:08:41 -0700875 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
danilchape9021a32016-05-17 01:52:02 -0700876 network_thread, worker_thread, signaling_thread,
877 fake_audio_capture_module_, fake_video_encoder_factory_,
878 fake_video_decoder_factory_);
deadbeefaf1b59c2015-10-15 12:08:41 -0700879 if (!peer_connection_factory_) {
880 return false;
881 }
882 if (options) {
883 peer_connection_factory_->SetOptions(*options);
884 }
zhihuang34b54c32016-08-04 11:06:50 -0700885 peer_connection_ =
886 CreatePeerConnection(std::move(port_allocator), constraints, config,
887 std::move(cert_generator));
888
deadbeefaf1b59c2015-10-15 12:08:41 -0700889 return peer_connection_.get() != nullptr;
890 }
891
deadbeefaf1b59c2015-10-15 12:08:41 -0700892 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
kwibergd1fe2812016-04-27 06:47:29 -0700893 std::unique_ptr<cricket::PortAllocator> port_allocator,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800894 const MediaConstraintsInterface* constraints,
zhihuang34b54c32016-08-04 11:06:50 -0700895 const PeerConnectionInterface::RTCConfiguration& config,
Henrik Boströmd79599d2016-06-01 13:58:50 +0200896 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) {
Henrik Boströmd79599d2016-06-01 13:58:50 +0200897 return peer_connection_factory_->CreatePeerConnection(
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -0800898 config, constraints, std::move(port_allocator),
Henrik Boströmd79599d2016-06-01 13:58:50 +0200899 std::move(cert_generator), this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 }
901
902 void HandleIncomingOffer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700903 LOG(INFO) << id_ << "HandleIncomingOffer ";
deadbeeffaac4972015-11-12 15:33:07 -0800904 if (NumberOfLocalMediaStreams() == 0 && auto_add_stream_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 // If we are not sending any streams ourselves it is time to add some.
906 AddMediaStream(true, true);
907 }
kwibergd1fe2812016-04-27 06:47:29 -0700908 std::unique_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700909 webrtc::CreateSessionDescription("offer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
zhihuang184a3fd2016-06-14 11:47:14 -0700911 // Set the RtpReceiverObserver after receivers are created.
912 SetRtpReceiverObservers();
kwibergd1fe2812016-04-27 06:47:29 -0700913 std::unique_ptr<SessionDescriptionInterface> answer;
kwiberg2bbff992016-03-16 11:03:04 -0700914 EXPECT_TRUE(DoCreateAnswer(&answer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 std::string sdp;
916 EXPECT_TRUE(answer->ToString(&sdp));
917 EXPECT_TRUE(DoSetLocalDescription(answer.release()));
deadbeefaf1b59c2015-10-15 12:08:41 -0700918 if (signaling_message_receiver_) {
919 signaling_message_receiver_->ReceiveSdpMessage(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 webrtc::SessionDescriptionInterface::kAnswer, sdp);
921 }
922 }
923
924 void HandleIncomingAnswer(const std::string& msg) {
deadbeefaf1b59c2015-10-15 12:08:41 -0700925 LOG(INFO) << id_ << "HandleIncomingAnswer";
kwibergd1fe2812016-04-27 06:47:29 -0700926 std::unique_ptr<SessionDescriptionInterface> desc(
deadbeefaf1b59c2015-10-15 12:08:41 -0700927 webrtc::CreateSessionDescription("answer", msg, nullptr));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 EXPECT_TRUE(DoSetRemoteDescription(desc.release()));
zhihuang184a3fd2016-06-14 11:47:14 -0700929 // Set the RtpReceiverObserver after receivers are created.
930 SetRtpReceiverObservers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000931 }
932
kwibergd1fe2812016-04-27 06:47:29 -0700933 bool DoCreateOfferAnswer(std::unique_ptr<SessionDescriptionInterface>* desc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934 bool offer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000935 rtc::scoped_refptr<MockCreateSessionDescriptionObserver>
936 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937 MockCreateSessionDescriptionObserver>());
htaaac2dea2016-03-10 13:35:55 -0800938 if (prefer_constraint_apis_) {
939 if (offer) {
940 pc()->CreateOffer(observer, &offer_answer_constraints_);
941 } else {
942 pc()->CreateAnswer(observer, &offer_answer_constraints_);
943 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944 } else {
htaaac2dea2016-03-10 13:35:55 -0800945 if (offer) {
946 pc()->CreateOffer(observer, offer_answer_options_);
947 } else {
948 pc()->CreateAnswer(observer, offer_answer_options_);
949 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950 }
951 EXPECT_EQ_WAIT(true, observer->called(), kMaxWaitMs);
kwiberg2bbff992016-03-16 11:03:04 -0700952 desc->reset(observer->release_desc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953 if (observer->result() && ExpectIceRestart()) {
954 EXPECT_EQ(0u, (*desc)->candidates(0)->count());
955 }
956 return observer->result();
957 }
958
kwibergd1fe2812016-04-27 06:47:29 -0700959 bool DoCreateOffer(std::unique_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960 return DoCreateOfferAnswer(desc, true);
961 }
962
kwibergd1fe2812016-04-27 06:47:29 -0700963 bool DoCreateAnswer(std::unique_ptr<SessionDescriptionInterface>* desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 return DoCreateOfferAnswer(desc, false);
965 }
966
967 bool DoSetLocalDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000968 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
969 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700971 LOG(INFO) << id_ << "SetLocalDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000972 pc()->SetLocalDescription(observer, desc);
973 // Ignore the observer result. If we wait for the result with
974 // EXPECT_TRUE_WAIT, local ice candidates might be sent to the remote peer
975 // before the offer which is an error.
976 // The reason is that EXPECT_TRUE_WAIT uses
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000977 // rtc::Thread::Current()->ProcessMessages(1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000978 // ProcessMessages waits at least 1ms but processes all messages before
979 // returning. Since this test is synchronous and send messages to the remote
980 // peer whenever a callback is invoked, this can lead to messages being
981 // sent to the remote peer in the wrong order.
982 // TODO(perkj): Find a way to check the result without risking that the
983 // order of sent messages are changed. Ex- by posting all messages that are
984 // sent to the remote peer.
985 return true;
986 }
987
988 bool DoSetRemoteDescription(SessionDescriptionInterface* desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000989 rtc::scoped_refptr<MockSetSessionDescriptionObserver>
990 observer(new rtc::RefCountedObject<
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 MockSetSessionDescriptionObserver>());
deadbeefaf1b59c2015-10-15 12:08:41 -0700992 LOG(INFO) << id_ << "SetRemoteDescription ";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000993 pc()->SetRemoteDescription(observer, desc);
994 EXPECT_TRUE_WAIT(observer->called(), kMaxWaitMs);
995 return observer->result();
996 }
997
998 // This modifies all received SDP messages before they are processed.
999 void FilterIncomingSdpMessage(std::string* sdp) {
1000 if (remove_msid_) {
1001 const char kSdpSsrcAttribute[] = "a=ssrc:";
1002 RemoveLinesFromSdp(kSdpSsrcAttribute, sdp);
1003 const char kSdpMsidSupportedAttribute[] = "a=msid-semantic:";
1004 RemoveLinesFromSdp(kSdpMsidSupportedAttribute, sdp);
1005 }
1006 if (remove_bundle_) {
1007 const char kSdpBundleAttribute[] = "a=group:BUNDLE";
1008 RemoveLinesFromSdp(kSdpBundleAttribute, sdp);
1009 }
1010 if (remove_sdes_) {
1011 const char kSdpSdesCryptoAttribute[] = "a=crypto";
1012 RemoveLinesFromSdp(kSdpSdesCryptoAttribute, sdp);
1013 }
perkjcaafdba2016-03-20 07:34:29 -07001014 if (remove_cvo_) {
1015 const char kSdpCvoExtenstion[] = "urn:3gpp:video-orientation";
1016 RemoveLinesFromSdp(kSdpCvoExtenstion, sdp);
1017 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018 }
1019
deadbeefaf1b59c2015-10-15 12:08:41 -07001020 std::string id_;
1021
deadbeefaf1b59c2015-10-15 12:08:41 -07001022 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
1023 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
1024 peer_connection_factory_;
1025
htaaac2dea2016-03-10 13:35:55 -08001026 bool prefer_constraint_apis_ = true;
deadbeeffaac4972015-11-12 15:33:07 -08001027 bool auto_add_stream_ = true;
1028
deadbeefaf1b59c2015-10-15 12:08:41 -07001029 typedef std::pair<std::string, std::string> IceUfragPwdPair;
1030 std::map<int, IceUfragPwdPair> ice_ufrag_pwd_;
1031 bool expect_ice_restart_ = false;
1032
deadbeefc9be0072015-12-14 18:27:57 -08001033 // Needed to keep track of number of frames sent.
deadbeefaf1b59c2015-10-15 12:08:41 -07001034 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
1035 // Needed to keep track of number of frames received.
kwibergd1fe2812016-04-27 06:47:29 -07001036 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
deadbeefc9be0072015-12-14 18:27:57 -08001037 fake_video_renderers_;
1038 // Needed to ensure frames aren't received for removed tracks.
kwibergd1fe2812016-04-27 06:47:29 -07001039 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
deadbeefc9be0072015-12-14 18:27:57 -08001040 removed_fake_video_renderers_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001041 // Needed to keep track of number of frames received when external decoder
1042 // used.
1043 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
1044 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
1045 bool video_decoder_factory_enabled_ = false;
1046 webrtc::FakeConstraints video_constraints_;
1047
1048 // For remote peer communication.
1049 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
1050
1051 // Store references to the video capturers we've created, so that we can stop
1052 // them, if required.
perkjcaafdba2016-03-20 07:34:29 -07001053 std::vector<cricket::FakeVideoCapturer*> video_capturers_;
1054 webrtc::VideoRotation capture_rotation_ = webrtc::kVideoRotation_0;
1055 // |local_video_renderer_| attached to the first created local video track.
kwibergd1fe2812016-04-27 06:47:29 -07001056 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001057
htaaac2dea2016-03-10 13:35:55 -08001058 webrtc::FakeConstraints offer_answer_constraints_;
1059 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
deadbeefaf1b59c2015-10-15 12:08:41 -07001060 bool remove_msid_ = false; // True if MSID should be removed in received SDP.
1061 bool remove_bundle_ =
1062 false; // True if bundle should be removed in received SDP.
1063 bool remove_sdes_ =
1064 false; // True if a=crypto should be removed in received SDP.
perkjcaafdba2016-03-20 07:34:29 -07001065 // |remove_cvo_| is true if extension urn:3gpp:video-orientation should be
1066 // removed in the received SDP.
1067 bool remove_cvo_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001068
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001069 rtc::scoped_refptr<DataChannelInterface> data_channel_;
kwibergd1fe2812016-04-27 06:47:29 -07001070 std::unique_ptr<MockDataChannelObserver> data_observer_;
zhihuang184a3fd2016-06-14 11:47:14 -07001071
1072 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073};
1074
deadbeef7c73bdb2015-12-10 15:10:44 -08001075class P2PTestConductor : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001076 public:
deadbeef7c73bdb2015-12-10 15:10:44 -08001077 P2PTestConductor()
deadbeefeff5b852016-05-27 14:18:01 -07001078 : pss_(new rtc::PhysicalSocketServer),
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001079 ss_(new rtc::VirtualSocketServer(pss_.get())),
deadbeefeff5b852016-05-27 14:18:01 -07001080 network_thread_(new rtc::Thread(ss_.get())),
1081 worker_thread_(rtc::Thread::Create()) {
danilchape9021a32016-05-17 01:52:02 -07001082 RTC_CHECK(network_thread_->Start());
1083 RTC_CHECK(worker_thread_->Start());
zhihuang34b54c32016-08-04 11:06:50 -07001084 webrtc::PeerConnectionInterface::IceServer ice_server;
1085 ice_server.uri = "stun:stun.l.google.com:19302";
1086 config_.servers.push_back(ice_server);
perkj8aba9972016-04-10 23:54:34 -07001087 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001088
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001089 bool SessionActive() {
1090 return initiating_client_->SessionActive() &&
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001091 receiving_client_->SessionActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092 }
pbos@webrtc.org9eacb8c2015-01-02 09:03:19 +00001093
hta6b4f8392016-03-10 00:24:31 -08001094 // Return true if the number of frames provided have been received
1095 // on the video and audio tracks provided.
1096 bool FramesHaveArrived(int audio_frames_to_receive,
1097 int video_frames_to_receive) {
1098 bool all_good = true;
1099 if (initiating_client_->HasLocalAudioTrack() &&
1100 receiving_client_->can_receive_audio()) {
1101 all_good &=
1102 receiving_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1103 }
1104 if (initiating_client_->HasLocalVideoTrack() &&
1105 receiving_client_->can_receive_video()) {
1106 all_good &=
1107 receiving_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1108 }
1109 if (receiving_client_->HasLocalAudioTrack() &&
1110 initiating_client_->can_receive_audio()) {
1111 all_good &=
1112 initiating_client_->AudioFramesReceivedCheck(audio_frames_to_receive);
1113 }
1114 if (receiving_client_->HasLocalVideoTrack() &&
1115 initiating_client_->can_receive_video()) {
1116 all_good &=
1117 initiating_client_->VideoFramesReceivedCheck(video_frames_to_receive);
1118 }
1119 return all_good;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001120 }
hta6b4f8392016-03-10 00:24:31 -08001121
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001122 void VerifyDtmf() {
1123 initiating_client_->VerifyDtmf();
1124 receiving_client_->VerifyDtmf();
1125 }
1126
1127 void TestUpdateOfferWithRejectedContent() {
deadbeefc9be0072015-12-14 18:27:57 -08001128 // Renegotiate, rejecting the video m-line.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129 initiating_client_->Negotiate(true, false);
deadbeefc9be0072015-12-14 18:27:57 -08001130 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1131
1132 int pc1_audio_received = initiating_client_->audio_frames_received();
1133 int pc1_video_received = initiating_client_->video_frames_received();
1134 int pc2_audio_received = receiving_client_->audio_frames_received();
1135 int pc2_video_received = receiving_client_->video_frames_received();
1136
1137 // Wait for some additional audio frames to be received.
1138 EXPECT_TRUE_WAIT(initiating_client_->AudioFramesReceivedCheck(
1139 pc1_audio_received + kEndAudioFrameCount) &&
1140 receiving_client_->AudioFramesReceivedCheck(
1141 pc2_audio_received + kEndAudioFrameCount),
1142 kMaxWaitForFramesMs);
1143
1144 // During this time, we shouldn't have received any additional video frames
1145 // for the rejected video tracks.
1146 EXPECT_EQ(pc1_video_received, initiating_client_->video_frames_received());
1147 EXPECT_EQ(pc2_video_received, receiving_client_->video_frames_received());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001148 }
1149
1150 void VerifyRenderedSize(int width, int height) {
perkjcaafdba2016-03-20 07:34:29 -07001151 VerifyRenderedSize(width, height, webrtc::kVideoRotation_0);
1152 }
1153
1154 void VerifyRenderedSize(int width,
1155 int height,
1156 webrtc::VideoRotation rotation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157 EXPECT_EQ(width, receiving_client()->rendered_width());
1158 EXPECT_EQ(height, receiving_client()->rendered_height());
perkjcaafdba2016-03-20 07:34:29 -07001159 EXPECT_EQ(rotation, receiving_client()->rendered_rotation());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001160 EXPECT_EQ(width, initializing_client()->rendered_width());
1161 EXPECT_EQ(height, initializing_client()->rendered_height());
perkjcaafdba2016-03-20 07:34:29 -07001162 EXPECT_EQ(rotation, initializing_client()->rendered_rotation());
1163
1164 // Verify size of the local preview.
1165 EXPECT_EQ(width, initializing_client()->local_rendered_width());
1166 EXPECT_EQ(height, initializing_client()->local_rendered_height());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001167 }
1168
1169 void VerifySessionDescriptions() {
1170 initiating_client_->VerifyRejectedMediaInSessionDescription();
1171 receiving_client_->VerifyRejectedMediaInSessionDescription();
1172 initiating_client_->VerifyLocalIceUfragAndPassword();
1173 receiving_client_->VerifyLocalIceUfragAndPassword();
1174 }
1175
deadbeef7c73bdb2015-12-10 15:10:44 -08001176 ~P2PTestConductor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177 if (initiating_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001178 initiating_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001179 }
1180 if (receiving_client_) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001181 receiving_client_->set_signaling_message_receiver(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183 }
1184
deadbeefaf1b59c2015-10-15 12:08:41 -07001185 bool CreateTestClients() { return CreateTestClients(nullptr, nullptr); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001186
1187 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1188 MediaConstraintsInterface* recv_constraints) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001189 return CreateTestClients(init_constraints, nullptr, recv_constraints,
1190 nullptr);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001191 }
1192
htaaac2dea2016-03-10 13:35:55 -08001193 bool CreateTestClientsThatPreferNoConstraints() {
1194 initiating_client_.reset(
perkj8aba9972016-04-10 23:54:34 -07001195 PeerConnectionTestClient::CreateClientPreferNoConstraints(
zhihuang34b54c32016-08-04 11:06:50 -07001196 "Caller: ", nullptr, config_, network_thread_.get(),
1197 worker_thread_.get()));
htaaac2dea2016-03-10 13:35:55 -08001198 receiving_client_.reset(
perkj8aba9972016-04-10 23:54:34 -07001199 PeerConnectionTestClient::CreateClientPreferNoConstraints(
zhihuang34b54c32016-08-04 11:06:50 -07001200 "Callee: ", nullptr, config_, network_thread_.get(),
1201 worker_thread_.get()));
htaaac2dea2016-03-10 13:35:55 -08001202 if (!initiating_client_ || !receiving_client_) {
1203 return false;
1204 }
1205 // Remember the choice for possible later resets of the clients.
1206 prefer_constraint_apis_ = false;
1207 SetSignalingReceivers();
1208 return true;
1209 }
1210
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001211 void SetSignalingReceivers() {
1212 initiating_client_->set_signaling_message_receiver(receiving_client_.get());
1213 receiving_client_->set_signaling_message_receiver(initiating_client_.get());
1214 }
1215
Joachim Bauch04e5b492015-05-29 09:40:39 +02001216 bool CreateTestClients(MediaConstraintsInterface* init_constraints,
1217 PeerConnectionFactory::Options* init_options,
1218 MediaConstraintsInterface* recv_constraints,
1219 PeerConnectionFactory::Options* recv_options) {
deadbeefaf1b59c2015-10-15 12:08:41 -07001220 initiating_client_.reset(PeerConnectionTestClient::CreateClient(
zhihuang34b54c32016-08-04 11:06:50 -07001221 "Caller: ", init_constraints, init_options, config_,
1222 network_thread_.get(), worker_thread_.get()));
deadbeefaf1b59c2015-10-15 12:08:41 -07001223 receiving_client_.reset(PeerConnectionTestClient::CreateClient(
zhihuang34b54c32016-08-04 11:06:50 -07001224 "Callee: ", recv_constraints, recv_options, config_,
1225 network_thread_.get(), worker_thread_.get()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 if (!initiating_client_ || !receiving_client_) {
1227 return false;
1228 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001229 SetSignalingReceivers();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001230 return true;
1231 }
1232
1233 void SetVideoConstraints(const webrtc::FakeConstraints& init_constraints,
1234 const webrtc::FakeConstraints& recv_constraints) {
1235 initiating_client_->SetVideoConstraints(init_constraints);
1236 receiving_client_->SetVideoConstraints(recv_constraints);
1237 }
1238
perkjcaafdba2016-03-20 07:34:29 -07001239 void SetCaptureRotation(webrtc::VideoRotation rotation) {
1240 initiating_client_->SetCaptureRotation(rotation);
1241 receiving_client_->SetCaptureRotation(rotation);
1242 }
1243
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001244 void EnableVideoDecoderFactory() {
1245 initiating_client_->EnableVideoDecoderFactory();
1246 receiving_client_->EnableVideoDecoderFactory();
1247 }
1248
1249 // This test sets up a call between two parties. Both parties send static
1250 // frames to each other. Once the test is finished the number of sent frames
1251 // is compared to the number of received frames.
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -07001252 void LocalP2PTest() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253 if (initiating_client_->NumberOfLocalMediaStreams() == 0) {
1254 initiating_client_->AddMediaStream(true, true);
1255 }
1256 initiating_client_->Negotiate();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257 // Assert true is used here since next tests are guaranteed to fail and
1258 // would eat up 5 seconds.
1259 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
1260 VerifySessionDescriptions();
1261
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262 int audio_frame_count = kEndAudioFrameCount;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001263 int video_frame_count = kEndVideoFrameCount;
hta6b4f8392016-03-10 00:24:31 -08001264 // TODO(ronghuawu): Add test to cover the case of sendonly and recvonly.
1265
1266 if ((!initiating_client_->can_receive_audio() &&
1267 !initiating_client_->can_receive_video()) ||
1268 (!receiving_client_->can_receive_audio() &&
1269 !receiving_client_->can_receive_video())) {
1270 // Neither audio nor video will flow, so connections won't be
1271 // established. There's nothing more to check.
1272 // TODO(hta): Check connection if there's a data channel.
1273 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001274 }
1275
hta6b4f8392016-03-10 00:24:31 -08001276 // Audio or video is expected to flow, so both clients should reach the
1277 // Connected state, and the offerer (ICE controller) should proceed to
1278 // Completed.
1279 // Note: These tests have been observed to fail under heavy load at
1280 // shorter timeouts, so they may be flaky.
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -07001281 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
1282 initiating_client_->ice_connection_state(),
1283 kMaxWaitForFramesMs);
hta6b4f8392016-03-10 00:24:31 -08001284 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
1285 receiving_client_->ice_connection_state(),
1286 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287
hta6b4f8392016-03-10 00:24:31 -08001288 // The ICE gathering state should end up in kIceGatheringComplete,
1289 // but there's a bug that prevents this at the moment, and the state
1290 // machine is being updated by the WEBRTC WG.
1291 // TODO(hta): Update this check when spec revisions finish.
1292 EXPECT_NE(webrtc::PeerConnectionInterface::kIceGatheringNew,
1293 initiating_client_->ice_gathering_state());
1294 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
1295 receiving_client_->ice_gathering_state(),
1296 kMaxWaitForFramesMs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297
hta6b4f8392016-03-10 00:24:31 -08001298 // Check that the expected number of frames have arrived.
1299 EXPECT_TRUE_WAIT(FramesHaveArrived(audio_frame_count, video_frame_count),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300 kMaxWaitForFramesMs);
1301 }
1302
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001303 void SetupAndVerifyDtlsCall() {
1304 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1305 FakeConstraints setup_constraints;
1306 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1307 true);
1308 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1309 LocalP2PTest();
1310 VerifyRenderedSize(640, 480);
1311 }
1312
1313 PeerConnectionTestClient* CreateDtlsClientWithAlternateKey() {
1314 FakeConstraints setup_constraints;
1315 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1316 true);
1317
Henrik Boströmd79599d2016-06-01 13:58:50 +02001318 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1319 rtc::SSLStreamAdapter::HaveDtlsSrtp() ?
1320 new FakeRTCCertificateGenerator() : nullptr);
1321 cert_generator->use_alternate_key();
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001322
1323 // Make sure the new client is using a different certificate.
1324 return PeerConnectionTestClient::CreateClientWithDtlsIdentityStore(
zhihuang34b54c32016-08-04 11:06:50 -07001325 "New Peer: ", &setup_constraints, nullptr, config_,
Henrik Boströmd79599d2016-06-01 13:58:50 +02001326 std::move(cert_generator), prefer_constraint_apis_,
danilchape9021a32016-05-17 01:52:02 -07001327 network_thread_.get(), worker_thread_.get());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001328 }
1329
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001330 void SendRtpData(webrtc::DataChannelInterface* dc, const std::string& data) {
1331 // Messages may get lost on the unreliable DataChannel, so we send multiple
1332 // times to avoid test flakiness.
1333 static const size_t kSendAttempts = 5;
1334
1335 for (size_t i = 0; i < kSendAttempts; ++i) {
1336 dc->Send(DataBuffer(data));
1337 }
1338 }
1339
deadbeefaf1b59c2015-10-15 12:08:41 -07001340 PeerConnectionTestClient* initializing_client() {
1341 return initiating_client_.get();
1342 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001343
1344 // Set the |initiating_client_| to the |client| passed in and return the
1345 // original |initiating_client_|.
1346 PeerConnectionTestClient* set_initializing_client(
1347 PeerConnectionTestClient* client) {
1348 PeerConnectionTestClient* old = initiating_client_.release();
1349 initiating_client_.reset(client);
1350 return old;
1351 }
1352
deadbeefaf1b59c2015-10-15 12:08:41 -07001353 PeerConnectionTestClient* receiving_client() {
1354 return receiving_client_.get();
1355 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001357 // Set the |receiving_client_| to the |client| passed in and return the
1358 // original |receiving_client_|.
1359 PeerConnectionTestClient* set_receiving_client(
1360 PeerConnectionTestClient* client) {
1361 PeerConnectionTestClient* old = receiving_client_.release();
1362 receiving_client_.reset(client);
1363 return old;
1364 }
zhihuang34b54c32016-08-04 11:06:50 -07001365 webrtc::PeerConnectionInterface::RTCConfiguration* config() {
1366 return &config_;
1367 }
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001368
zhihuang184a3fd2016-06-14 11:47:14 -07001369 bool AllObserversReceived(
1370 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>& observers) {
1371 for (auto& observer : observers) {
1372 if (!observer->first_packet_received()) {
1373 return false;
1374 }
1375 }
1376 return true;
1377 }
1378
jbauchcb560652016-08-04 05:20:32 -07001379 void TestGcmNegotiation(bool local_gcm_enabled, bool remote_gcm_enabled,
1380 int expected_cipher_suite) {
1381 PeerConnectionFactory::Options init_options;
1382 init_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled;
1383 PeerConnectionFactory::Options recv_options;
1384 recv_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled;
1385 ASSERT_TRUE(
1386 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
1387 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1388 init_observer =
1389 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1390 initializing_client()->pc()->RegisterUMAObserver(init_observer);
1391 LocalP2PTest();
1392
1393 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
1394 initializing_client()->GetSrtpCipherStats(),
1395 kMaxWaitMs);
1396 EXPECT_EQ(1,
1397 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1398 expected_cipher_suite));
1399 }
1400
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001401 private:
deadbeefeff5b852016-05-27 14:18:01 -07001402 // |ss_| is used by |network_thread_| so it must be destroyed later.
kwibergd1fe2812016-04-27 06:47:29 -07001403 std::unique_ptr<rtc::PhysicalSocketServer> pss_;
1404 std::unique_ptr<rtc::VirtualSocketServer> ss_;
deadbeefeff5b852016-05-27 14:18:01 -07001405 // |network_thread_| and |worker_thread_| are used by both
1406 // |initiating_client_| and |receiving_client_| so they must be destroyed
1407 // later.
1408 std::unique_ptr<rtc::Thread> network_thread_;
1409 std::unique_ptr<rtc::Thread> worker_thread_;
kwibergd1fe2812016-04-27 06:47:29 -07001410 std::unique_ptr<PeerConnectionTestClient> initiating_client_;
1411 std::unique_ptr<PeerConnectionTestClient> receiving_client_;
htaaac2dea2016-03-10 13:35:55 -08001412 bool prefer_constraint_apis_ = true;
zhihuang34b54c32016-08-04 11:06:50 -07001413 webrtc::PeerConnectionInterface::RTCConfiguration config_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001414};
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001415
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00001416// Disable for TSan v2, see
1417// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
1418#if !defined(THREAD_SANITIZER)
1419
zhihuang184a3fd2016-06-14 11:47:14 -07001420TEST_F(P2PTestConductor, TestRtpReceiverObserverCallbackFunction) {
1421 ASSERT_TRUE(CreateTestClients());
1422 LocalP2PTest();
1423 EXPECT_TRUE_WAIT(
1424 AllObserversReceived(initializing_client()->rtp_receiver_observers()),
1425 kMaxWaitForFramesMs);
1426 EXPECT_TRUE_WAIT(
1427 AllObserversReceived(receiving_client()->rtp_receiver_observers()),
1428 kMaxWaitForFramesMs);
1429}
1430
1431// The observers are expected to fire the signal even if they are set after the
1432// first packet is received.
1433TEST_F(P2PTestConductor, TestSetRtpReceiverObserverAfterFirstPacketIsReceived) {
1434 ASSERT_TRUE(CreateTestClients());
1435 LocalP2PTest();
1436 // Reset the RtpReceiverObservers.
1437 initializing_client()->SetRtpReceiverObservers();
1438 receiving_client()->SetRtpReceiverObservers();
1439 EXPECT_TRUE_WAIT(
1440 AllObserversReceived(initializing_client()->rtp_receiver_observers()),
1441 kMaxWaitForFramesMs);
1442 EXPECT_TRUE_WAIT(
1443 AllObserversReceived(receiving_client()->rtp_receiver_observers()),
1444 kMaxWaitForFramesMs);
1445}
1446
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001447// This test sets up a Jsep call between two parties and test Dtmf.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00001448// TODO(holmer): Disabled due to sometimes crashing on buildbots.
1449// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08001450TEST_F(P2PTestConductor, DISABLED_LocalP2PTestDtmf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001451 ASSERT_TRUE(CreateTestClients());
1452 LocalP2PTest();
1453 VerifyDtmf();
1454}
1455
1456// This test sets up a Jsep call between two parties and test that we can get a
1457// video aspect ratio of 16:9.
deadbeef7c73bdb2015-12-10 15:10:44 -08001458TEST_F(P2PTestConductor, LocalP2PTest16To9) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001459 ASSERT_TRUE(CreateTestClients());
1460 FakeConstraints constraint;
1461 double requested_ratio = 640.0/360;
1462 constraint.SetMandatoryMinAspectRatio(requested_ratio);
1463 SetVideoConstraints(constraint, constraint);
1464 LocalP2PTest();
1465
1466 ASSERT_LE(0, initializing_client()->rendered_height());
1467 double initiating_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001468 static_cast<double>(initializing_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001469 initializing_client()->rendered_height();
1470 EXPECT_LE(requested_ratio, initiating_video_ratio);
1471
1472 ASSERT_LE(0, receiving_client()->rendered_height());
1473 double receiving_video_ratio =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001474 static_cast<double>(receiving_client()->rendered_width()) /
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475 receiving_client()->rendered_height();
1476 EXPECT_LE(requested_ratio, receiving_video_ratio);
1477}
1478
1479// This test sets up a Jsep call between two parties and test that the
1480// received video has a resolution of 1280*720.
1481// TODO(mallinath): Enable when
1482// http://code.google.com/p/webrtc/issues/detail?id=981 is fixed.
deadbeef7c73bdb2015-12-10 15:10:44 -08001483TEST_F(P2PTestConductor, DISABLED_LocalP2PTest1280By720) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001484 ASSERT_TRUE(CreateTestClients());
1485 FakeConstraints constraint;
1486 constraint.SetMandatoryMinWidth(1280);
1487 constraint.SetMandatoryMinHeight(720);
1488 SetVideoConstraints(constraint, constraint);
1489 LocalP2PTest();
1490 VerifyRenderedSize(1280, 720);
1491}
1492
1493// This test sets up a call between two endpoints that are configured to use
1494// DTLS key agreement. As a result, DTLS is negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001495TEST_F(P2PTestConductor, LocalP2PTestDtls) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001496 SetupAndVerifyDtlsCall();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497}
1498
hta6b4f8392016-03-10 00:24:31 -08001499// This test sets up an one-way call, with media only from initiator to
1500// responder.
1501TEST_F(P2PTestConductor, OneWayMediaCall) {
1502 ASSERT_TRUE(CreateTestClients());
1503 receiving_client()->set_auto_add_stream(false);
1504 LocalP2PTest();
1505}
1506
htaaac2dea2016-03-10 13:35:55 -08001507TEST_F(P2PTestConductor, OneWayMediaCallWithoutConstraints) {
1508 ASSERT_TRUE(CreateTestClientsThatPreferNoConstraints());
1509 receiving_client()->set_auto_add_stream(false);
1510 LocalP2PTest();
1511}
1512
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001513// This test sets up a audio call initially and then upgrades to audio/video,
1514// using DTLS.
deadbeef7c73bdb2015-12-10 15:10:44 -08001515TEST_F(P2PTestConductor, LocalP2PTestDtlsRenegotiate) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001516 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001517 FakeConstraints setup_constraints;
1518 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1519 true);
1520 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1521 receiving_client()->SetReceiveAudioVideo(true, false);
1522 LocalP2PTest();
1523 receiving_client()->SetReceiveAudioVideo(true, true);
1524 receiving_client()->Negotiate();
1525}
1526
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001527// This test sets up a call transfer to a new caller with a different DTLS
1528// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001529TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCallee) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001530 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1531 SetupAndVerifyDtlsCall();
1532
1533 // Keeping the original peer around which will still send packets to the
1534 // receiving client. These SRTP packets will be dropped.
kwibergd1fe2812016-04-27 06:47:29 -07001535 std::unique_ptr<PeerConnectionTestClient> original_peer(
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001536 set_initializing_client(CreateDtlsClientWithAlternateKey()));
1537 original_peer->pc()->Close();
1538
1539 SetSignalingReceivers();
1540 receiving_client()->SetExpectIceRestart(true);
1541 LocalP2PTest();
1542 VerifyRenderedSize(640, 480);
1543}
1544
guoweis46383312015-12-17 16:45:59 -08001545// This test sets up a non-bundle call and apply bundle during ICE restart. When
1546// bundle is in effect in the restart, the channel can successfully reset its
1547// DTLS-SRTP context.
1548TEST_F(P2PTestConductor, LocalP2PTestDtlsBundleInIceRestart) {
1549 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1550 FakeConstraints setup_constraints;
1551 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1552 true);
1553 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1554 receiving_client()->RemoveBundleFromReceivedSdp(true);
1555 LocalP2PTest();
1556 VerifyRenderedSize(640, 480);
1557
1558 initializing_client()->IceRestart();
1559 receiving_client()->SetExpectIceRestart(true);
1560 receiving_client()->RemoveBundleFromReceivedSdp(false);
1561 LocalP2PTest();
1562 VerifyRenderedSize(640, 480);
1563}
1564
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001565// This test sets up a call transfer to a new callee with a different DTLS
1566// fingerprint.
deadbeef7c73bdb2015-12-10 15:10:44 -08001567TEST_F(P2PTestConductor, LocalP2PTestDtlsTransferCaller) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001568 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
1569 SetupAndVerifyDtlsCall();
1570
1571 // Keeping the original peer around which will still send packets to the
1572 // receiving client. These SRTP packets will be dropped.
kwibergd1fe2812016-04-27 06:47:29 -07001573 std::unique_ptr<PeerConnectionTestClient> original_peer(
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001574 set_receiving_client(CreateDtlsClientWithAlternateKey()));
1575 original_peer->pc()->Close();
1576
1577 SetSignalingReceivers();
1578 initializing_client()->IceRestart();
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -07001579 LocalP2PTest();
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001580 VerifyRenderedSize(640, 480);
1581}
1582
perkjcaafdba2016-03-20 07:34:29 -07001583TEST_F(P2PTestConductor, LocalP2PTestCVO) {
1584 ASSERT_TRUE(CreateTestClients());
1585 SetCaptureRotation(webrtc::kVideoRotation_90);
1586 LocalP2PTest();
1587 VerifyRenderedSize(640, 480, webrtc::kVideoRotation_90);
1588}
1589
1590TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportCVO) {
1591 ASSERT_TRUE(CreateTestClients());
1592 SetCaptureRotation(webrtc::kVideoRotation_90);
1593 receiving_client()->RemoveCvoFromReceivedSdp(true);
1594 LocalP2PTest();
1595 VerifyRenderedSize(480, 640, webrtc::kVideoRotation_0);
1596}
1597
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598// This test sets up a call between two endpoints that are configured to use
1599// DTLS key agreement. The offerer don't support SDES. As a result, DTLS is
1600// negotiated and used for transport.
deadbeef7c73bdb2015-12-10 15:10:44 -08001601TEST_F(P2PTestConductor, LocalP2PTestOfferDtlsButNotSdes) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001602 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001603 FakeConstraints setup_constraints;
1604 setup_constraints.AddMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
1605 true);
1606 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1607 receiving_client()->RemoveSdesCryptoFromReceivedSdp(true);
1608 LocalP2PTest();
1609 VerifyRenderedSize(640, 480);
1610}
1611
1612// This test sets up a Jsep call between two parties, and the callee only
1613// accept to receive video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001614TEST_F(P2PTestConductor, LocalP2PTestAnswerVideo) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001615 ASSERT_TRUE(CreateTestClients());
1616 receiving_client()->SetReceiveAudioVideo(false, true);
1617 LocalP2PTest();
1618}
1619
1620// This test sets up a Jsep call between two parties, and the callee only
1621// accept to receive audio.
deadbeef7c73bdb2015-12-10 15:10:44 -08001622TEST_F(P2PTestConductor, LocalP2PTestAnswerAudio) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623 ASSERT_TRUE(CreateTestClients());
1624 receiving_client()->SetReceiveAudioVideo(true, false);
1625 LocalP2PTest();
1626}
1627
1628// This test sets up a Jsep call between two parties, and the callee reject both
1629// audio and video.
deadbeef7c73bdb2015-12-10 15:10:44 -08001630TEST_F(P2PTestConductor, LocalP2PTestAnswerNone) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631 ASSERT_TRUE(CreateTestClients());
1632 receiving_client()->SetReceiveAudioVideo(false, false);
1633 LocalP2PTest();
1634}
1635
1636// This test sets up an audio and video call between two parties. After the call
1637// runs for a while (10 frames), the caller sends an update offer with video
1638// being rejected. Once the re-negotiation is done, the video flow should stop
1639// and the audio flow should continue.
deadbeefc9be0072015-12-14 18:27:57 -08001640TEST_F(P2PTestConductor, UpdateOfferWithRejectedContent) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001641 ASSERT_TRUE(CreateTestClients());
1642 LocalP2PTest();
1643 TestUpdateOfferWithRejectedContent();
1644}
1645
1646// This test sets up a Jsep call between two parties. The MSID is removed from
1647// the SDP strings from the caller.
deadbeefc9be0072015-12-14 18:27:57 -08001648TEST_F(P2PTestConductor, LocalP2PTestWithoutMsid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649 ASSERT_TRUE(CreateTestClients());
1650 receiving_client()->RemoveMsidFromReceivedSdp(true);
1651 // TODO(perkj): Currently there is a bug that cause audio to stop playing if
1652 // audio and video is muxed when MSID is disabled. Remove
1653 // SetRemoveBundleFromSdp once
1654 // https://code.google.com/p/webrtc/issues/detail?id=1193 is fixed.
1655 receiving_client()->RemoveBundleFromReceivedSdp(true);
1656 LocalP2PTest();
1657}
1658
1659// This test sets up a Jsep call between two parties and the initiating peer
1660// sends two steams.
1661// TODO(perkj): Disabled due to
1662// https://code.google.com/p/webrtc/issues/detail?id=1454
deadbeef7c73bdb2015-12-10 15:10:44 -08001663TEST_F(P2PTestConductor, DISABLED_LocalP2PTestTwoStreams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664 ASSERT_TRUE(CreateTestClients());
1665 // Set optional video constraint to max 320pixels to decrease CPU usage.
1666 FakeConstraints constraint;
1667 constraint.SetOptionalMaxWidth(320);
1668 SetVideoConstraints(constraint, constraint);
1669 initializing_client()->AddMediaStream(true, true);
1670 initializing_client()->AddMediaStream(false, true);
1671 ASSERT_EQ(2u, initializing_client()->NumberOfLocalMediaStreams());
1672 LocalP2PTest();
1673 EXPECT_EQ(2u, receiving_client()->number_of_remote_streams());
1674}
1675
1676// Test that we can receive the audio output level from a remote audio track.
deadbeef7c73bdb2015-12-10 15:10:44 -08001677TEST_F(P2PTestConductor, GetAudioOutputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678 ASSERT_TRUE(CreateTestClients());
1679 LocalP2PTest();
1680
1681 StreamCollectionInterface* remote_streams =
1682 initializing_client()->remote_streams();
1683 ASSERT_GT(remote_streams->count(), 0u);
1684 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1685 MediaStreamTrackInterface* remote_audio_track =
1686 remote_streams->at(0)->GetAudioTracks()[0];
1687
1688 // Get the audio output level stats. Note that the level is not available
1689 // until a RTCP packet has been received.
1690 EXPECT_TRUE_WAIT(
1691 initializing_client()->GetAudioOutputLevelStats(remote_audio_track) > 0,
1692 kMaxWaitForStatsMs);
1693}
1694
1695// Test that an audio input level is reported.
deadbeef7c73bdb2015-12-10 15:10:44 -08001696TEST_F(P2PTestConductor, GetAudioInputLevelStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001697 ASSERT_TRUE(CreateTestClients());
1698 LocalP2PTest();
1699
1700 // Get the audio input level stats. The level should be available very
1701 // soon after the test starts.
1702 EXPECT_TRUE_WAIT(initializing_client()->GetAudioInputLevelStats() > 0,
1703 kMaxWaitForStatsMs);
1704}
1705
1706// Test that we can get incoming byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001707TEST_F(P2PTestConductor, GetBytesReceivedStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001708 ASSERT_TRUE(CreateTestClients());
1709 LocalP2PTest();
1710
1711 StreamCollectionInterface* remote_streams =
1712 initializing_client()->remote_streams();
1713 ASSERT_GT(remote_streams->count(), 0u);
1714 ASSERT_GT(remote_streams->at(0)->GetAudioTracks().size(), 0u);
1715 MediaStreamTrackInterface* remote_audio_track =
1716 remote_streams->at(0)->GetAudioTracks()[0];
1717 EXPECT_TRUE_WAIT(
1718 initializing_client()->GetBytesReceivedStats(remote_audio_track) > 0,
1719 kMaxWaitForStatsMs);
1720
1721 MediaStreamTrackInterface* remote_video_track =
1722 remote_streams->at(0)->GetVideoTracks()[0];
1723 EXPECT_TRUE_WAIT(
1724 initializing_client()->GetBytesReceivedStats(remote_video_track) > 0,
1725 kMaxWaitForStatsMs);
1726}
1727
1728// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeef7c73bdb2015-12-10 15:10:44 -08001729TEST_F(P2PTestConductor, GetBytesSentStats) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730 ASSERT_TRUE(CreateTestClients());
1731 LocalP2PTest();
1732
1733 StreamCollectionInterface* local_streams =
1734 initializing_client()->local_streams();
1735 ASSERT_GT(local_streams->count(), 0u);
1736 ASSERT_GT(local_streams->at(0)->GetAudioTracks().size(), 0u);
1737 MediaStreamTrackInterface* local_audio_track =
1738 local_streams->at(0)->GetAudioTracks()[0];
1739 EXPECT_TRUE_WAIT(
1740 initializing_client()->GetBytesSentStats(local_audio_track) > 0,
1741 kMaxWaitForStatsMs);
1742
1743 MediaStreamTrackInterface* local_video_track =
1744 local_streams->at(0)->GetVideoTracks()[0];
1745 EXPECT_TRUE_WAIT(
1746 initializing_client()->GetBytesSentStats(local_video_track) > 0,
1747 kMaxWaitForStatsMs);
1748}
1749
Joachim Bauch04e5b492015-05-29 09:40:39 +02001750// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001751TEST_F(P2PTestConductor, GetDtls12None) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001752 PeerConnectionFactory::Options init_options;
1753 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1754 PeerConnectionFactory::Options recv_options;
1755 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001756 ASSERT_TRUE(
1757 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001758 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1759 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1760 initializing_client()->pc()->RegisterUMAObserver(init_observer);
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001761 LocalP2PTest();
1762
torbjorng43166b82016-03-11 00:06:47 -08001763 EXPECT_TRUE_WAIT(
1764 rtc::SSLStreamAdapter::IsAcceptableCipher(
1765 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1766 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001767 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001768 initializing_client()->GetSrtpCipherStats(),
1769 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001770 EXPECT_EQ(1,
1771 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1772 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001773}
1774
1775// Test that DTLS 1.2 is used if both ends support it.
torbjorng79a5a832016-01-15 07:16:51 -08001776TEST_F(P2PTestConductor, GetDtls12Both) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001777 PeerConnectionFactory::Options init_options;
1778 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1779 PeerConnectionFactory::Options recv_options;
1780 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001781 ASSERT_TRUE(
1782 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001783 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1784 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1785 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001786 LocalP2PTest();
1787
torbjorng43166b82016-03-11 00:06:47 -08001788 EXPECT_TRUE_WAIT(
1789 rtc::SSLStreamAdapter::IsAcceptableCipher(
1790 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1791 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001792 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001793 initializing_client()->GetSrtpCipherStats(),
1794 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001795 EXPECT_EQ(1,
1796 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1797 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001798}
1799
1800// Test that DTLS 1.0 is used if the initator supports DTLS 1.2 and the
1801// received supports 1.0.
torbjorng43166b82016-03-11 00:06:47 -08001802TEST_F(P2PTestConductor, GetDtls12Init) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001803 PeerConnectionFactory::Options init_options;
1804 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
1805 PeerConnectionFactory::Options recv_options;
1806 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
deadbeefaf1b59c2015-10-15 12:08:41 -07001807 ASSERT_TRUE(
1808 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001809 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1810 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1811 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001812 LocalP2PTest();
1813
torbjorng43166b82016-03-11 00:06:47 -08001814 EXPECT_TRUE_WAIT(
1815 rtc::SSLStreamAdapter::IsAcceptableCipher(
1816 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1817 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001818 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001819 initializing_client()->GetSrtpCipherStats(),
1820 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001821 EXPECT_EQ(1,
1822 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1823 kDefaultSrtpCryptoSuite));
Joachim Bauch04e5b492015-05-29 09:40:39 +02001824}
1825
1826// Test that DTLS 1.0 is used if the initator supports DTLS 1.0 and the
1827// received supports 1.2.
torbjorng43166b82016-03-11 00:06:47 -08001828TEST_F(P2PTestConductor, GetDtls12Recv) {
Joachim Bauch04e5b492015-05-29 09:40:39 +02001829 PeerConnectionFactory::Options init_options;
1830 init_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
1831 PeerConnectionFactory::Options recv_options;
1832 recv_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
deadbeefaf1b59c2015-10-15 12:08:41 -07001833 ASSERT_TRUE(
1834 CreateTestClients(nullptr, &init_options, nullptr, &recv_options));
jbauchac8869e2015-07-03 01:36:14 -07001835 rtc::scoped_refptr<webrtc::FakeMetricsObserver>
1836 init_observer = new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1837 initializing_client()->pc()->RegisterUMAObserver(init_observer);
Joachim Bauch04e5b492015-05-29 09:40:39 +02001838 LocalP2PTest();
1839
torbjorng43166b82016-03-11 00:06:47 -08001840 EXPECT_TRUE_WAIT(
1841 rtc::SSLStreamAdapter::IsAcceptableCipher(
1842 initializing_client()->GetDtlsCipherStats(), rtc::KT_DEFAULT),
1843 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001844 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001845 initializing_client()->GetSrtpCipherStats(),
1846 kMaxWaitForStatsMs);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001847 EXPECT_EQ(1,
1848 init_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1849 kDefaultSrtpCryptoSuite));
pthatcher@webrtc.org7bea1ff2015-03-04 01:38:30 +00001850}
1851
jbauchcb560652016-08-04 05:20:32 -07001852// Test that a non-GCM cipher is used if both sides only support non-GCM.
1853TEST_F(P2PTestConductor, GetGcmNone) {
1854 TestGcmNegotiation(false, false, kDefaultSrtpCryptoSuite);
1855}
1856
1857// Test that a GCM cipher is used if both ends support it.
1858TEST_F(P2PTestConductor, GetGcmBoth) {
1859 TestGcmNegotiation(true, true, kDefaultSrtpCryptoSuiteGcm);
1860}
1861
1862// Test that GCM isn't used if only the initiator supports it.
1863TEST_F(P2PTestConductor, GetGcmInit) {
1864 TestGcmNegotiation(true, false, kDefaultSrtpCryptoSuite);
1865}
1866
1867// Test that GCM isn't used if only the receiver supports it.
1868TEST_F(P2PTestConductor, GetGcmRecv) {
1869 TestGcmNegotiation(false, true, kDefaultSrtpCryptoSuite);
1870}
1871
deadbeefb5cb19b2015-11-23 16:39:12 -08001872// This test sets up a call between two parties with audio, video and an RTP
1873// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001874TEST_F(P2PTestConductor, LocalP2PTestRtpDataChannel) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875 FakeConstraints setup_constraints;
1876 setup_constraints.SetAllowRtpDataChannels();
1877 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1878 initializing_client()->CreateDataChannel();
1879 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001880 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1881 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1883 kMaxWaitMs);
1884 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
1885 kMaxWaitMs);
1886
1887 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001888
1889 SendRtpData(initializing_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1891 kMaxWaitMs);
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001892
1893 SendRtpData(receiving_client()->data_channel(), data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001894 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1895 kMaxWaitMs);
1896
1897 receiving_client()->data_channel()->Close();
1898 // Send new offer and answer.
1899 receiving_client()->Negotiate();
1900 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1901 EXPECT_FALSE(receiving_client()->data_observer()->IsOpen());
1902}
1903
deadbeefb5cb19b2015-11-23 16:39:12 -08001904// This test sets up a call between two parties with audio, video and an SCTP
1905// data channel.
deadbeef7c73bdb2015-12-10 15:10:44 -08001906TEST_F(P2PTestConductor, LocalP2PTestSctpDataChannel) {
deadbeefb5cb19b2015-11-23 16:39:12 -08001907 ASSERT_TRUE(CreateTestClients());
1908 initializing_client()->CreateDataChannel();
1909 LocalP2PTest();
1910 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1911 EXPECT_TRUE_WAIT(receiving_client()->data_channel() != nullptr, kMaxWaitMs);
1912 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1913 kMaxWaitMs);
1914 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
1915
1916 std::string data = "hello world";
1917
1918 initializing_client()->data_channel()->Send(DataBuffer(data));
1919 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
1920 kMaxWaitMs);
1921
1922 receiving_client()->data_channel()->Send(DataBuffer(data));
1923 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
1924 kMaxWaitMs);
1925
1926 receiving_client()->data_channel()->Close();
deadbeef15887932015-12-14 19:32:34 -08001927 EXPECT_TRUE_WAIT(!initializing_client()->data_observer()->IsOpen(),
1928 kMaxWaitMs);
1929 EXPECT_TRUE_WAIT(!receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
deadbeefb5cb19b2015-11-23 16:39:12 -08001930}
1931
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001932// This test sets up a call between two parties and creates a data channel.
1933// The test tests that received data is buffered unless an observer has been
1934// registered.
1935// Rtp data channels can receive data before the underlying
1936// transport has detected that a channel is writable and thus data can be
1937// received before the data channel state changes to open. That is hard to test
1938// but the same buffering is used in that case.
deadbeef7c73bdb2015-12-10 15:10:44 -08001939TEST_F(P2PTestConductor, RegisterDataChannelObserver) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001940 FakeConstraints setup_constraints;
1941 setup_constraints.SetAllowRtpDataChannels();
1942 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1943 initializing_client()->CreateDataChannel();
1944 initializing_client()->Negotiate();
1945
deadbeefaf1b59c2015-10-15 12:08:41 -07001946 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1947 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001948 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1949 kMaxWaitMs);
1950 EXPECT_EQ_WAIT(DataChannelInterface::kOpen,
1951 receiving_client()->data_channel()->state(), kMaxWaitMs);
1952
1953 // Unregister the existing observer.
1954 receiving_client()->data_channel()->UnregisterObserver();
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00001955
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001956 std::string data = "hello world";
jiayl@webrtc.org6c6f33b2014-06-12 21:05:19 +00001957 SendRtpData(initializing_client()->data_channel(), data);
1958
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959 // Wait a while to allow the sent data to arrive before an observer is
1960 // registered..
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001961 rtc::Thread::Current()->ProcessMessages(100);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001962
1963 MockDataChannelObserver new_observer(receiving_client()->data_channel());
1964 EXPECT_EQ_WAIT(data, new_observer.last_message(), kMaxWaitMs);
1965}
1966
1967// This test sets up a call between two parties with audio, video and but only
1968// the initiating client support data.
deadbeef7c73bdb2015-12-10 15:10:44 -08001969TEST_F(P2PTestConductor, LocalP2PTestReceiverDoesntSupportData) {
buildbot@webrtc.org61c1b8e2014-04-09 06:06:38 +00001970 FakeConstraints setup_constraints_1;
1971 setup_constraints_1.SetAllowRtpDataChannels();
1972 // Must disable DTLS to make negotiation succeed.
1973 setup_constraints_1.SetMandatory(
1974 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1975 FakeConstraints setup_constraints_2;
1976 setup_constraints_2.SetMandatory(
1977 MediaConstraintsInterface::kEnableDtlsSrtp, false);
1978 ASSERT_TRUE(CreateTestClients(&setup_constraints_1, &setup_constraints_2));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979 initializing_client()->CreateDataChannel();
1980 LocalP2PTest();
deadbeefaf1b59c2015-10-15 12:08:41 -07001981 EXPECT_TRUE(initializing_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001982 EXPECT_FALSE(receiving_client()->data_channel());
1983 EXPECT_FALSE(initializing_client()->data_observer()->IsOpen());
1984}
1985
1986// This test sets up a call between two parties with audio, video. When audio
1987// and video is setup and flowing and data channel is negotiated.
deadbeef7c73bdb2015-12-10 15:10:44 -08001988TEST_F(P2PTestConductor, AddDataChannelAfterRenegotiation) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001989 FakeConstraints setup_constraints;
1990 setup_constraints.SetAllowRtpDataChannels();
1991 ASSERT_TRUE(CreateTestClients(&setup_constraints, &setup_constraints));
1992 LocalP2PTest();
1993 initializing_client()->CreateDataChannel();
1994 // Send new offer and answer.
1995 initializing_client()->Negotiate();
deadbeefaf1b59c2015-10-15 12:08:41 -07001996 ASSERT_TRUE(initializing_client()->data_channel() != nullptr);
1997 ASSERT_TRUE(receiving_client()->data_channel() != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001998 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
1999 kMaxWaitMs);
2000 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(),
2001 kMaxWaitMs);
2002}
2003
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00002004// This test sets up a Jsep call with SCTP DataChannel and verifies the
2005// negotiation is completed without error.
2006#ifdef HAVE_SCTP
Taylor Brandstetter7ff17372016-04-01 11:50:39 -07002007TEST_F(P2PTestConductor, CreateOfferWithSctpDataChannel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002008 MAYBE_SKIP_TEST(rtc::SSLStreamAdapter::HaveDtlsSrtp);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +00002009 FakeConstraints constraints;
2010 constraints.SetMandatory(
2011 MediaConstraintsInterface::kEnableDtlsSrtp, true);
2012 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
2013 initializing_client()->CreateDataChannel();
2014 initializing_client()->Negotiate(false, false);
2015}
2016#endif
2017
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002018// This test sets up a call between two parties with audio, and video.
2019// During the call, the initializing side restart ice and the test verifies that
2020// new ice candidates are generated and audio and video still can flow.
deadbeef7c73bdb2015-12-10 15:10:44 -08002021TEST_F(P2PTestConductor, IceRestart) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002022 ASSERT_TRUE(CreateTestClients());
2023
2024 // Negotiate and wait for ice completion and make sure audio and video plays.
2025 LocalP2PTest();
2026
2027 // Create a SDP string of the first audio candidate for both clients.
2028 const webrtc::IceCandidateCollection* audio_candidates_initiator =
2029 initializing_client()->pc()->local_description()->candidates(0);
2030 const webrtc::IceCandidateCollection* audio_candidates_receiver =
2031 receiving_client()->pc()->local_description()->candidates(0);
2032 ASSERT_GT(audio_candidates_initiator->count(), 0u);
2033 ASSERT_GT(audio_candidates_receiver->count(), 0u);
2034 std::string initiator_candidate;
2035 EXPECT_TRUE(
2036 audio_candidates_initiator->at(0)->ToString(&initiator_candidate));
2037 std::string receiver_candidate;
2038 EXPECT_TRUE(audio_candidates_receiver->at(0)->ToString(&receiver_candidate));
2039
2040 // Restart ice on the initializing client.
2041 receiving_client()->SetExpectIceRestart(true);
2042 initializing_client()->IceRestart();
2043
2044 // Negotiate and wait for ice completion again and make sure audio and video
2045 // plays.
2046 LocalP2PTest();
2047
2048 // Create a SDP string of the first audio candidate for both clients again.
2049 const webrtc::IceCandidateCollection* audio_candidates_initiator_restart =
2050 initializing_client()->pc()->local_description()->candidates(0);
2051 const webrtc::IceCandidateCollection* audio_candidates_reciever_restart =
2052 receiving_client()->pc()->local_description()->candidates(0);
2053 ASSERT_GT(audio_candidates_initiator_restart->count(), 0u);
2054 ASSERT_GT(audio_candidates_reciever_restart->count(), 0u);
2055 std::string initiator_candidate_restart;
2056 EXPECT_TRUE(audio_candidates_initiator_restart->at(0)->ToString(
2057 &initiator_candidate_restart));
2058 std::string receiver_candidate_restart;
2059 EXPECT_TRUE(audio_candidates_reciever_restart->at(0)->ToString(
2060 &receiver_candidate_restart));
2061
2062 // Verify that the first candidates in the local session descriptions has
2063 // changed.
2064 EXPECT_NE(initiator_candidate, initiator_candidate_restart);
2065 EXPECT_NE(receiver_candidate, receiver_candidate_restart);
2066}
2067
deadbeeffaac4972015-11-12 15:33:07 -08002068// This test sets up a call between two parties with audio, and video.
2069// It then renegotiates setting the video m-line to "port 0", then later
2070// renegotiates again, enabling video.
deadbeef7c73bdb2015-12-10 15:10:44 -08002071TEST_F(P2PTestConductor, LocalP2PTestVideoDisableEnable) {
deadbeeffaac4972015-11-12 15:33:07 -08002072 ASSERT_TRUE(CreateTestClients());
2073
2074 // Do initial negotiation. Will result in video and audio sendonly m-lines.
2075 receiving_client()->set_auto_add_stream(false);
2076 initializing_client()->AddMediaStream(true, true);
2077 initializing_client()->Negotiate();
2078
2079 // Negotiate again, disabling the video m-line (receiving client will
2080 // set port to 0 due to mandatory "OfferToReceiveVideo: false" constraint).
2081 receiving_client()->SetReceiveVideo(false);
2082 initializing_client()->Negotiate();
2083
2084 // Enable video and do negotiation again, making sure video is received
2085 // end-to-end.
2086 receiving_client()->SetReceiveVideo(true);
2087 receiving_client()->AddMediaStream(true, true);
2088 LocalP2PTest();
2089}
2090
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002091// This test sets up a Jsep call between two parties with external
2092// VideoDecoderFactory.
stefan@webrtc.orgda790082013-09-17 13:11:38 +00002093// TODO(holmer): Disabled due to sometimes crashing on buildbots.
2094// See issue webrtc/2378.
deadbeef7c73bdb2015-12-10 15:10:44 -08002095TEST_F(P2PTestConductor, DISABLED_LocalP2PTestWithVideoDecoderFactory) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002096 ASSERT_TRUE(CreateTestClients());
2097 EnableVideoDecoderFactory();
2098 LocalP2PTest();
2099}
buildbot@webrtc.orgb4c7b092014-08-25 12:11:58 +00002100
deadbeeffac06552015-11-25 11:26:01 -08002101// This tests that if we negotiate after calling CreateSender but before we
2102// have a track, then set a track later, frames from the newly-set track are
2103// received end-to-end.
deadbeef7c73bdb2015-12-10 15:10:44 -08002104TEST_F(P2PTestConductor, EarlyWarmupTest) {
deadbeeffac06552015-11-25 11:26:01 -08002105 ASSERT_TRUE(CreateTestClients());
deadbeefbd7d8f72015-12-18 16:58:44 -08002106 auto audio_sender =
2107 initializing_client()->pc()->CreateSender("audio", "stream_id");
2108 auto video_sender =
2109 initializing_client()->pc()->CreateSender("video", "stream_id");
deadbeeffac06552015-11-25 11:26:01 -08002110 initializing_client()->Negotiate();
2111 // Wait for ICE connection to complete, without any tracks.
2112 // Note that the receiving client WILL (in HandleIncomingOffer) create
2113 // tracks, so it's only the initiator here that's doing early warmup.
2114 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
2115 VerifySessionDescriptions();
2116 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2117 initializing_client()->ice_connection_state(),
2118 kMaxWaitForFramesMs);
2119 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2120 receiving_client()->ice_connection_state(),
2121 kMaxWaitForFramesMs);
2122 // Now set the tracks, and expect frames to immediately start flowing.
2123 EXPECT_TRUE(
2124 audio_sender->SetTrack(initializing_client()->CreateLocalAudioTrack("")));
2125 EXPECT_TRUE(
2126 video_sender->SetTrack(initializing_client()->CreateLocalVideoTrack("")));
hta6b4f8392016-03-10 00:24:31 -08002127 EXPECT_TRUE_WAIT(FramesHaveArrived(kEndAudioFrameCount, kEndVideoFrameCount),
deadbeeffac06552015-11-25 11:26:01 -08002128 kMaxWaitForFramesMs);
2129}
2130
zhihuang34b54c32016-08-04 11:06:50 -07002131#ifdef HAVE_QUIC
2132// This test sets up a call between two parties using QUIC instead of DTLS for
2133// audio and video, and a QUIC data channel.
2134TEST_F(P2PTestConductor, LocalP2PTestQuicDataChannel) {
2135 config()->enable_quic = true;
2136 ASSERT_TRUE(CreateTestClients());
2137 webrtc::DataChannelInit init;
2138 init.ordered = false;
2139 init.reliable = true;
2140 init.id = 1;
2141 initializing_client()->CreateDataChannel(&init);
2142 receiving_client()->CreateDataChannel(&init);
2143 LocalP2PTest();
2144 ASSERT_NE(nullptr, initializing_client()->data_channel());
2145 ASSERT_NE(nullptr, receiving_client()->data_channel());
2146 EXPECT_TRUE_WAIT(initializing_client()->data_observer()->IsOpen(),
2147 kMaxWaitMs);
2148 EXPECT_TRUE_WAIT(receiving_client()->data_observer()->IsOpen(), kMaxWaitMs);
2149
2150 std::string data = "hello world";
2151
2152 initializing_client()->data_channel()->Send(DataBuffer(data));
2153 EXPECT_EQ_WAIT(data, receiving_client()->data_observer()->last_message(),
2154 kMaxWaitMs);
2155
2156 receiving_client()->data_channel()->Send(DataBuffer(data));
2157 EXPECT_EQ_WAIT(data, initializing_client()->data_observer()->last_message(),
2158 kMaxWaitMs);
2159}
2160
2161// Tests that negotiation of QUIC data channels is completed without error.
2162TEST_F(P2PTestConductor, NegotiateQuicDataChannel) {
2163 config()->enable_quic = true;
2164 FakeConstraints constraints;
2165 constraints.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp, true);
2166 ASSERT_TRUE(CreateTestClients(&constraints, &constraints));
2167 webrtc::DataChannelInit init;
2168 init.ordered = false;
2169 init.reliable = true;
2170 init.id = 1;
2171 initializing_client()->CreateDataChannel(&init);
2172 initializing_client()->Negotiate(false, false);
2173}
2174
2175// This test sets up a JSEP call using QUIC. The callee only receives video.
2176TEST_F(P2PTestConductor, LocalP2PTestVideoOnlyWithQuic) {
2177 config()->enable_quic = true;
2178 ASSERT_TRUE(CreateTestClients());
2179 receiving_client()->SetReceiveAudioVideo(false, true);
2180 LocalP2PTest();
2181}
2182
2183// This test sets up a JSEP call using QUIC. The callee only receives audio.
2184TEST_F(P2PTestConductor, LocalP2PTestAudioOnlyWithQuic) {
2185 config()->enable_quic = true;
2186 ASSERT_TRUE(CreateTestClients());
2187 receiving_client()->SetReceiveAudioVideo(true, false);
2188 LocalP2PTest();
2189}
2190
2191// This test sets up a JSEP call using QUIC. The callee rejects both audio and
2192// video.
2193TEST_F(P2PTestConductor, LocalP2PTestNoVideoAudioWithQuic) {
2194 config()->enable_quic = true;
2195 ASSERT_TRUE(CreateTestClients());
2196 receiving_client()->SetReceiveAudioVideo(false, false);
2197 LocalP2PTest();
2198}
2199
2200#endif // HAVE_QUIC
2201
nissed98cf1f2016-04-22 07:27:36 -07002202TEST_F(P2PTestConductor, ForwardVideoOnlyStream) {
2203 ASSERT_TRUE(CreateTestClients());
2204 // One-way stream
2205 receiving_client()->set_auto_add_stream(false);
2206 // Video only, audio forwarding not expected to work.
2207 initializing_client()->AddMediaStream(false, true);
2208 initializing_client()->Negotiate();
2209
2210 ASSERT_TRUE_WAIT(SessionActive(), kMaxWaitForActivationMs);
2211 VerifySessionDescriptions();
2212
2213 ASSERT_TRUE(initializing_client()->can_receive_video());
2214 ASSERT_TRUE(receiving_client()->can_receive_video());
2215
2216 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2217 initializing_client()->ice_connection_state(),
2218 kMaxWaitForFramesMs);
2219 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2220 receiving_client()->ice_connection_state(),
2221 kMaxWaitForFramesMs);
2222
2223 ASSERT_TRUE(receiving_client()->remote_streams()->count() == 1);
2224
2225 // Echo the stream back.
2226 receiving_client()->pc()->AddStream(
2227 receiving_client()->remote_streams()->at(0));
2228 receiving_client()->Negotiate();
2229
2230 EXPECT_TRUE_WAIT(
2231 initializing_client()->VideoFramesReceivedCheck(kEndVideoFrameCount),
2232 kMaxWaitForFramesMs);
2233}
2234
deadbeef0a6c4ca2015-10-06 11:38:28 -07002235class IceServerParsingTest : public testing::Test {
2236 public:
2237 // Convenience for parsing a single URL.
2238 bool ParseUrl(const std::string& url) {
2239 return ParseUrl(url, std::string(), std::string());
2240 }
2241
2242 bool ParseUrl(const std::string& url,
2243 const std::string& username,
2244 const std::string& password) {
2245 PeerConnectionInterface::IceServers servers;
2246 PeerConnectionInterface::IceServer server;
2247 server.urls.push_back(url);
2248 server.username = username;
2249 server.password = password;
2250 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002251 return webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002252 }
2253
2254 protected:
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002255 cricket::ServerAddresses stun_servers_;
2256 std::vector<cricket::RelayServerConfig> turn_servers_;
deadbeef0a6c4ca2015-10-06 11:38:28 -07002257};
2258
2259// Make sure all STUN/TURN prefixes are parsed correctly.
2260TEST_F(IceServerParsingTest, ParseStunPrefixes) {
2261 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002262 EXPECT_EQ(1U, stun_servers_.size());
2263 EXPECT_EQ(0U, turn_servers_.size());
2264 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002265
2266 EXPECT_TRUE(ParseUrl("stuns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002267 EXPECT_EQ(1U, stun_servers_.size());
2268 EXPECT_EQ(0U, turn_servers_.size());
2269 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002270
2271 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002272 EXPECT_EQ(0U, stun_servers_.size());
2273 EXPECT_EQ(1U, turn_servers_.size());
2274 EXPECT_FALSE(turn_servers_[0].ports[0].secure);
2275 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002276
2277 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002278 EXPECT_EQ(0U, stun_servers_.size());
2279 EXPECT_EQ(1U, turn_servers_.size());
2280 EXPECT_TRUE(turn_servers_[0].ports[0].secure);
2281 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002282
2283 // invalid prefixes
2284 EXPECT_FALSE(ParseUrl("stunn:hostname"));
2285 EXPECT_FALSE(ParseUrl(":hostname"));
2286 EXPECT_FALSE(ParseUrl(":"));
2287 EXPECT_FALSE(ParseUrl(""));
2288}
2289
2290TEST_F(IceServerParsingTest, VerifyDefaults) {
2291 // TURNS defaults
2292 EXPECT_TRUE(ParseUrl("turns:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002293 EXPECT_EQ(1U, turn_servers_.size());
2294 EXPECT_EQ(5349, turn_servers_[0].ports[0].address.port());
2295 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
2296 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002297
2298 // TURN defaults
2299 EXPECT_TRUE(ParseUrl("turn:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002300 EXPECT_EQ(1U, turn_servers_.size());
2301 EXPECT_EQ(3478, turn_servers_[0].ports[0].address.port());
2302 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2303 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002304
2305 // STUN defaults
2306 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002307 EXPECT_EQ(1U, stun_servers_.size());
2308 EXPECT_EQ(3478, stun_servers_.begin()->port());
2309 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002310}
2311
2312// Check that the 6 combinations of IPv4/IPv6/hostname and with/without port
2313// can be parsed correctly.
2314TEST_F(IceServerParsingTest, ParseHostnameAndPort) {
2315 EXPECT_TRUE(ParseUrl("stun:1.2.3.4:1234"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002316 EXPECT_EQ(1U, stun_servers_.size());
2317 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2318 EXPECT_EQ(1234, stun_servers_.begin()->port());
2319 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002320
2321 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]:4321"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002322 EXPECT_EQ(1U, stun_servers_.size());
2323 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2324 EXPECT_EQ(4321, stun_servers_.begin()->port());
2325 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002326
2327 EXPECT_TRUE(ParseUrl("stun:hostname:9999"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002328 EXPECT_EQ(1U, stun_servers_.size());
2329 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2330 EXPECT_EQ(9999, stun_servers_.begin()->port());
2331 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002332
2333 EXPECT_TRUE(ParseUrl("stun:1.2.3.4"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002334 EXPECT_EQ(1U, stun_servers_.size());
2335 EXPECT_EQ("1.2.3.4", stun_servers_.begin()->hostname());
2336 EXPECT_EQ(3478, stun_servers_.begin()->port());
2337 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002338
2339 EXPECT_TRUE(ParseUrl("stun:[1:2:3:4:5:6:7:8]"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002340 EXPECT_EQ(1U, stun_servers_.size());
2341 EXPECT_EQ("1:2:3:4:5:6:7:8", stun_servers_.begin()->hostname());
2342 EXPECT_EQ(3478, stun_servers_.begin()->port());
2343 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002344
2345 EXPECT_TRUE(ParseUrl("stun:hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002346 EXPECT_EQ(1U, stun_servers_.size());
2347 EXPECT_EQ("hostname", stun_servers_.begin()->hostname());
2348 EXPECT_EQ(3478, stun_servers_.begin()->port());
2349 stun_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002350
2351 // Try some invalid hostname:port strings.
2352 EXPECT_FALSE(ParseUrl("stun:hostname:99a99"));
2353 EXPECT_FALSE(ParseUrl("stun:hostname:-1"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002354 EXPECT_FALSE(ParseUrl("stun:hostname:port:more"));
2355 EXPECT_FALSE(ParseUrl("stun:hostname:port more"));
deadbeef0a6c4ca2015-10-06 11:38:28 -07002356 EXPECT_FALSE(ParseUrl("stun:hostname:"));
2357 EXPECT_FALSE(ParseUrl("stun:[1:2:3:4:5:6:7:8]junk:1000"));
2358 EXPECT_FALSE(ParseUrl("stun::5555"));
2359 EXPECT_FALSE(ParseUrl("stun:"));
2360}
2361
2362// Test parsing the "?transport=xxx" part of the URL.
2363TEST_F(IceServerParsingTest, ParseTransport) {
2364 EXPECT_TRUE(ParseUrl("turn:hostname:1234?transport=tcp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002365 EXPECT_EQ(1U, turn_servers_.size());
2366 EXPECT_EQ(cricket::PROTO_TCP, turn_servers_[0].ports[0].proto);
2367 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002368
2369 EXPECT_TRUE(ParseUrl("turn:hostname?transport=udp"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002370 EXPECT_EQ(1U, turn_servers_.size());
2371 EXPECT_EQ(cricket::PROTO_UDP, turn_servers_[0].ports[0].proto);
2372 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002373
2374 EXPECT_FALSE(ParseUrl("turn:hostname?transport=invalid"));
2375}
2376
2377// Test parsing ICE username contained in URL.
2378TEST_F(IceServerParsingTest, ParseUsername) {
2379 EXPECT_TRUE(ParseUrl("turn:user@hostname"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002380 EXPECT_EQ(1U, turn_servers_.size());
2381 EXPECT_EQ("user", turn_servers_[0].credentials.username);
2382 turn_servers_.clear();
deadbeef0a6c4ca2015-10-06 11:38:28 -07002383
2384 EXPECT_FALSE(ParseUrl("turn:@hostname"));
2385 EXPECT_FALSE(ParseUrl("turn:username@"));
2386 EXPECT_FALSE(ParseUrl("turn:@"));
2387 EXPECT_FALSE(ParseUrl("turn:user@name@hostname"));
2388}
2389
2390// Test that username and password from IceServer is copied into the resulting
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002391// RelayServerConfig.
deadbeef0a6c4ca2015-10-06 11:38:28 -07002392TEST_F(IceServerParsingTest, CopyUsernameAndPasswordFromIceServer) {
2393 EXPECT_TRUE(ParseUrl("turn:hostname", "username", "password"));
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002394 EXPECT_EQ(1U, turn_servers_.size());
2395 EXPECT_EQ("username", turn_servers_[0].credentials.username);
2396 EXPECT_EQ("password", turn_servers_[0].credentials.password);
deadbeef0a6c4ca2015-10-06 11:38:28 -07002397}
2398
2399// Ensure that if a server has multiple URLs, each one is parsed.
2400TEST_F(IceServerParsingTest, ParseMultipleUrls) {
2401 PeerConnectionInterface::IceServers servers;
2402 PeerConnectionInterface::IceServer server;
2403 server.urls.push_back("stun:hostname");
2404 server.urls.push_back("turn:hostname");
2405 servers.push_back(server);
Taylor Brandstetter0c7e9f52015-12-29 14:14:52 -08002406 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2407 EXPECT_EQ(1U, stun_servers_.size());
2408 EXPECT_EQ(1U, turn_servers_.size());
deadbeef0a6c4ca2015-10-06 11:38:28 -07002409}
2410
Taylor Brandstetter893505d2016-01-07 15:12:48 -08002411// Ensure that TURN servers are given unique priorities,
2412// so that their resulting candidates have unique priorities.
2413TEST_F(IceServerParsingTest, TurnServerPrioritiesUnique) {
2414 PeerConnectionInterface::IceServers servers;
2415 PeerConnectionInterface::IceServer server;
2416 server.urls.push_back("turn:hostname");
2417 server.urls.push_back("turn:hostname2");
2418 servers.push_back(server);
2419 EXPECT_TRUE(webrtc::ParseIceServers(servers, &stun_servers_, &turn_servers_));
2420 EXPECT_EQ(2U, turn_servers_.size());
2421 EXPECT_NE(turn_servers_[0].priority, turn_servers_[1].priority);
2422}
2423
kjellander@webrtc.orgd1cfa712013-10-16 16:51:52 +00002424#endif // if !defined(THREAD_SANITIZER)
hta6b4f8392016-03-10 00:24:31 -08002425
2426} // namespace