blob: d38433c18e083532c535af353b60b7faa214af58 [file] [log] [blame]
deadbeef1dcb1642017-03-29 21:08:16 -07001/*
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * 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.
9 */
10
11// Disable for TSan v2, see
12// https://code.google.com/p/webrtc/issues/detail?id=1205 for details.
13#if !defined(THREAD_SANITIZER)
14
15#include <stdio.h>
16
17#include <algorithm>
18#include <functional>
19#include <list>
20#include <map>
21#include <memory>
22#include <utility>
23#include <vector>
24
Karl Wiberg1b0eae32017-10-17 14:48:54 +020025#include "api/audio_codecs/builtin_audio_decoder_factory.h"
26#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "api/fakemetricsobserver.h"
28#include "api/mediastreaminterface.h"
29#include "api/peerconnectioninterface.h"
Steve Anton8c0f7a72017-10-03 10:03:10 -070030#include "api/peerconnectionproxy.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "api/test/fakeconstraints.h"
32#include "media/engine/fakewebrtcvideoengine.h"
33#include "p2p/base/p2pconstants.h"
34#include "p2p/base/portinterface.h"
35#include "p2p/base/sessiondescription.h"
Steve Antonede9ca52017-10-16 13:04:27 -070036#include "p2p/base/teststunserver.h"
Jonas Orelandbdcee282017-10-10 14:01:40 +020037#include "p2p/base/testturncustomizer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "p2p/base/testturnserver.h"
39#include "p2p/client/basicportallocator.h"
40#include "pc/dtmfsender.h"
41#include "pc/localaudiosource.h"
42#include "pc/mediasession.h"
43#include "pc/peerconnection.h"
44#include "pc/peerconnectionfactory.h"
45#include "pc/test/fakeaudiocapturemodule.h"
46#include "pc/test/fakeperiodicvideocapturer.h"
47#include "pc/test/fakertccertificategenerator.h"
48#include "pc/test/fakevideotrackrenderer.h"
49#include "pc/test/mockpeerconnectionobservers.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020050#include "rtc_base/fakenetwork.h"
Steve Antonede9ca52017-10-16 13:04:27 -070051#include "rtc_base/firewallsocketserver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020052#include "rtc_base/gunit.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020053#include "rtc_base/virtualsocketserver.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020054#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070055
56using cricket::ContentInfo;
57using cricket::FakeWebRtcVideoDecoder;
58using cricket::FakeWebRtcVideoDecoderFactory;
59using cricket::FakeWebRtcVideoEncoder;
60using cricket::FakeWebRtcVideoEncoderFactory;
61using cricket::MediaContentDescription;
Steve Antonede9ca52017-10-16 13:04:27 -070062using rtc::SocketAddress;
63using ::testing::ElementsAre;
64using ::testing::Values;
deadbeef1dcb1642017-03-29 21:08:16 -070065using webrtc::DataBuffer;
66using webrtc::DataChannelInterface;
67using webrtc::DtmfSender;
68using webrtc::DtmfSenderInterface;
69using webrtc::DtmfSenderObserverInterface;
70using webrtc::FakeConstraints;
71using webrtc::MediaConstraintsInterface;
72using webrtc::MediaStreamInterface;
73using webrtc::MediaStreamTrackInterface;
74using webrtc::MockCreateSessionDescriptionObserver;
75using webrtc::MockDataChannelObserver;
76using webrtc::MockSetSessionDescriptionObserver;
77using webrtc::MockStatsObserver;
78using webrtc::ObserverInterface;
Steve Anton8c0f7a72017-10-03 10:03:10 -070079using webrtc::PeerConnection;
deadbeef1dcb1642017-03-29 21:08:16 -070080using webrtc::PeerConnectionInterface;
81using webrtc::PeerConnectionFactory;
Steve Anton8c0f7a72017-10-03 10:03:10 -070082using webrtc::PeerConnectionProxy;
deadbeef1dcb1642017-03-29 21:08:16 -070083using webrtc::SessionDescriptionInterface;
84using webrtc::StreamCollectionInterface;
85
86namespace {
87
88static const int kDefaultTimeout = 10000;
89static const int kMaxWaitForStatsMs = 3000;
90static const int kMaxWaitForActivationMs = 5000;
91static const int kMaxWaitForFramesMs = 10000;
92// Default number of audio/video frames to wait for before considering a test
93// successful.
94static const int kDefaultExpectedAudioFrameCount = 3;
95static const int kDefaultExpectedVideoFrameCount = 3;
96
deadbeef1dcb1642017-03-29 21:08:16 -070097static const char kDataChannelLabel[] = "data_channel";
98
99// SRTP cipher name negotiated by the tests. This must be updated if the
100// default changes.
101static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
102static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
103
Steve Antonede9ca52017-10-16 13:04:27 -0700104static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
105
deadbeef1dcb1642017-03-29 21:08:16 -0700106// Helper function for constructing offer/answer options to initiate an ICE
107// restart.
108PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
109 PeerConnectionInterface::RTCOfferAnswerOptions options;
110 options.ice_restart = true;
111 return options;
112}
113
deadbeefd8ad7882017-04-18 16:01:17 -0700114// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
115// attribute from received SDP, simulating a legacy endpoint.
116void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
117 for (ContentInfo& content : desc->contents()) {
118 MediaContentDescription* media_desc =
119 static_cast<MediaContentDescription*>(content.description);
120 media_desc->mutable_streams().clear();
121 }
122 desc->set_msid_supported(false);
123}
124
zhihuangf8164932017-05-19 13:09:47 -0700125int FindFirstMediaStatsIndexByKind(
126 const std::string& kind,
127 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
128 media_stats_vec) {
129 for (size_t i = 0; i < media_stats_vec.size(); i++) {
130 if (media_stats_vec[i]->kind.ValueToString() == kind) {
131 return i;
132 }
133 }
134 return -1;
135}
136
deadbeef1dcb1642017-03-29 21:08:16 -0700137class SignalingMessageReceiver {
138 public:
139 virtual void ReceiveSdpMessage(const std::string& type,
140 const 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:
146 SignalingMessageReceiver() {}
147 virtual ~SignalingMessageReceiver() {}
148};
149
150class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
151 public:
152 explicit 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() const { 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
169// Helper class that wraps a peer connection, observes it, and can accept
170// signaling messages from another wrapper.
171//
172// Uses a fake network, fake A/V capture, and optionally fake
173// encoders/decoders, though they aren't used by default since they don't
174// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700175// TODO(steveanton): See how this could become a subclass of
176// PeerConnectionWrapper defined in peerconnectionwrapper.h .
deadbeef1dcb1642017-03-29 21:08:16 -0700177class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
178 public SignalingMessageReceiver,
179 public ObserverInterface {
180 public:
181 // Different factory methods for convenience.
182 // TODO(deadbeef): Could use the pattern of:
183 //
184 // PeerConnectionWrapper =
185 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
186 //
187 // To reduce some code duplication.
188 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
189 const std::string& debug_name,
190 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
191 rtc::Thread* network_thread,
192 rtc::Thread* worker_thread) {
193 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
194 if (!client->Init(nullptr, nullptr, nullptr, std::move(cert_generator),
195 network_thread, worker_thread)) {
196 delete client;
197 return nullptr;
198 }
199 return client;
200 }
201
202 static PeerConnectionWrapper* CreateWithConfig(
203 const std::string& debug_name,
204 const PeerConnectionInterface::RTCConfiguration& config,
205 rtc::Thread* network_thread,
206 rtc::Thread* worker_thread) {
207 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
208 new FakeRTCCertificateGenerator());
209 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
210 if (!client->Init(nullptr, nullptr, &config, std::move(cert_generator),
211 network_thread, worker_thread)) {
212 delete client;
213 return nullptr;
214 }
215 return client;
216 }
217
218 static PeerConnectionWrapper* CreateWithOptions(
219 const std::string& debug_name,
220 const PeerConnectionFactory::Options& options,
221 rtc::Thread* network_thread,
222 rtc::Thread* worker_thread) {
223 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
224 new FakeRTCCertificateGenerator());
225 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
226 if (!client->Init(nullptr, &options, nullptr, std::move(cert_generator),
227 network_thread, worker_thread)) {
228 delete client;
229 return nullptr;
230 }
231 return client;
232 }
233
234 static PeerConnectionWrapper* CreateWithConstraints(
235 const std::string& debug_name,
236 const MediaConstraintsInterface* constraints,
237 rtc::Thread* network_thread,
238 rtc::Thread* worker_thread) {
239 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
240 new FakeRTCCertificateGenerator());
241 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
242 if (!client->Init(constraints, nullptr, nullptr, std::move(cert_generator),
243 network_thread, worker_thread)) {
244 delete client;
245 return nullptr;
246 }
247 return client;
248 }
249
deadbeef2f425aa2017-04-14 10:41:32 -0700250 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
251 return peer_connection_factory_.get();
252 }
253
deadbeef1dcb1642017-03-29 21:08:16 -0700254 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
255
256 // If a signaling message receiver is set (via ConnectFakeSignaling), this
257 // will set the whole offer/answer exchange in motion. Just need to wait for
258 // the signaling state to reach "stable".
259 void CreateAndSetAndSignalOffer() {
260 auto offer = CreateOffer();
261 ASSERT_NE(nullptr, offer);
262 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
263 }
264
265 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
266 // when a remote offer is received (via fake signaling) and an answer is
267 // generated. By default, uses default options.
268 void SetOfferAnswerOptions(
269 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
270 offer_answer_options_ = options;
271 }
272
273 // Set a callback to be invoked when SDP is received via the fake signaling
274 // channel, which provides an opportunity to munge (modify) the SDP. This is
275 // used to test SDP being applied that a PeerConnection would normally not
276 // generate, but a non-JSEP endpoint might.
277 void SetReceivedSdpMunger(
278 std::function<void(cricket::SessionDescription*)> munger) {
279 received_sdp_munger_ = munger;
280 }
281
deadbeefc964d0b2017-04-03 10:03:35 -0700282 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700283 // generated.
284 void SetGeneratedSdpMunger(
285 std::function<void(cricket::SessionDescription*)> munger) {
286 generated_sdp_munger_ = munger;
287 }
288
Steve Antonede9ca52017-10-16 13:04:27 -0700289 // Every ICE connection state in order that has been seen by the observer.
290 std::vector<PeerConnectionInterface::IceConnectionState>
291 ice_connection_state_history() const {
292 return ice_connection_state_history_;
293 }
Steve Anton6f25b092017-10-23 09:39:20 -0700294 void clear_ice_connection_state_history() {
295 ice_connection_state_history_.clear();
296 }
Steve Antonede9ca52017-10-16 13:04:27 -0700297
298 // Every ICE gathering state in order that has been seen by the observer.
299 std::vector<PeerConnectionInterface::IceGatheringState>
300 ice_gathering_state_history() const {
301 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700302 }
303
304 // TODO(deadbeef): Switch the majority of these tests to use AddTrack instead
305 // of AddStream since AddStream is deprecated.
306 void AddAudioVideoMediaStream() {
307 AddMediaStreamFromTracks(CreateLocalAudioTrack(), CreateLocalVideoTrack());
308 }
309
310 void AddAudioOnlyMediaStream() {
311 AddMediaStreamFromTracks(CreateLocalAudioTrack(), nullptr);
312 }
313
314 void AddVideoOnlyMediaStream() {
315 AddMediaStreamFromTracks(nullptr, CreateLocalVideoTrack());
316 }
317
318 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
319 FakeConstraints constraints;
320 // Disable highpass filter so that we can get all the test audio frames.
321 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
322 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
323 peer_connection_factory_->CreateAudioSource(&constraints);
324 // TODO(perkj): Test audio source when it is implemented. Currently audio
325 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700326 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700327 source);
328 }
329
330 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
deadbeefb1a15d72017-09-07 14:12:05 -0700331 return CreateLocalVideoTrackInternal(FakeConstraints(),
332 webrtc::kVideoRotation_0);
deadbeef1dcb1642017-03-29 21:08:16 -0700333 }
334
335 rtc::scoped_refptr<webrtc::VideoTrackInterface>
336 CreateLocalVideoTrackWithConstraints(const FakeConstraints& constraints) {
deadbeefb1a15d72017-09-07 14:12:05 -0700337 return CreateLocalVideoTrackInternal(constraints, webrtc::kVideoRotation_0);
deadbeef1dcb1642017-03-29 21:08:16 -0700338 }
339
340 rtc::scoped_refptr<webrtc::VideoTrackInterface>
341 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
deadbeefb1a15d72017-09-07 14:12:05 -0700342 return CreateLocalVideoTrackInternal(FakeConstraints(), rotation);
deadbeef1dcb1642017-03-29 21:08:16 -0700343 }
344
345 void AddMediaStreamFromTracks(
346 rtc::scoped_refptr<webrtc::AudioTrackInterface> audio,
347 rtc::scoped_refptr<webrtc::VideoTrackInterface> video) {
deadbeef1dcb1642017-03-29 21:08:16 -0700348 rtc::scoped_refptr<MediaStreamInterface> stream =
deadbeefb1a15d72017-09-07 14:12:05 -0700349 peer_connection_factory_->CreateLocalMediaStream(
350 rtc::CreateRandomUuid());
deadbeef1dcb1642017-03-29 21:08:16 -0700351 if (audio) {
352 stream->AddTrack(audio);
353 }
354 if (video) {
355 stream->AddTrack(video);
356 }
357 EXPECT_TRUE(pc()->AddStream(stream));
358 }
359
360 bool SignalingStateStable() {
361 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
362 }
363
364 void CreateDataChannel() { CreateDataChannel(nullptr); }
365
366 void CreateDataChannel(const webrtc::DataChannelInit* init) {
367 data_channel_ = pc()->CreateDataChannel(kDataChannelLabel, init);
368 ASSERT_TRUE(data_channel_.get() != nullptr);
369 data_observer_.reset(new MockDataChannelObserver(data_channel_));
370 }
371
372 DataChannelInterface* data_channel() { return data_channel_; }
373 const MockDataChannelObserver* data_observer() const {
374 return data_observer_.get();
375 }
376
377 int audio_frames_received() const {
378 return fake_audio_capture_module_->frames_received();
379 }
380
381 // Takes minimum of video frames received for each track.
382 //
383 // Can be used like:
384 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
385 //
386 // To ensure that all video tracks received at least a certain number of
387 // frames.
388 int min_video_frames_received_per_track() const {
389 int min_frames = INT_MAX;
390 if (video_decoder_factory_enabled_) {
391 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
392 fake_video_decoder_factory_->decoders();
393 if (decoders.empty()) {
394 return 0;
395 }
396 for (FakeWebRtcVideoDecoder* decoder : decoders) {
397 min_frames = std::min(min_frames, decoder->GetNumFramesReceived());
398 }
399 return min_frames;
400 } else {
401 if (fake_video_renderers_.empty()) {
402 return 0;
403 }
404
405 for (const auto& pair : fake_video_renderers_) {
406 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
407 }
408 return min_frames;
409 }
410 }
411
412 // In contrast to the above, sums the video frames received for all tracks.
413 // Can be used to verify that no video frames were received, or that the
414 // counts didn't increase.
415 int total_video_frames_received() const {
416 int total = 0;
417 if (video_decoder_factory_enabled_) {
418 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
419 fake_video_decoder_factory_->decoders();
420 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
421 total += decoder->GetNumFramesReceived();
422 }
423 } else {
424 for (const auto& pair : fake_video_renderers_) {
425 total += pair.second->num_rendered_frames();
426 }
427 for (const auto& renderer : removed_fake_video_renderers_) {
428 total += renderer->num_rendered_frames();
429 }
430 }
431 return total;
432 }
433
434 // Returns a MockStatsObserver in a state after stats gathering finished,
435 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700436 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700437 webrtc::MediaStreamTrackInterface* track) {
438 rtc::scoped_refptr<MockStatsObserver> observer(
439 new rtc::RefCountedObject<MockStatsObserver>());
440 EXPECT_TRUE(peer_connection_->GetStats(
441 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
442 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
443 return observer;
444 }
445
446 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700447 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
448 return OldGetStatsForTrack(nullptr);
449 }
450
451 // Synchronously gets stats and returns them. If it times out, fails the test
452 // and returns null.
453 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
454 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
455 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
456 peer_connection_->GetStats(callback);
457 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
458 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700459 }
460
461 int rendered_width() {
462 EXPECT_FALSE(fake_video_renderers_.empty());
463 return fake_video_renderers_.empty()
464 ? 0
465 : fake_video_renderers_.begin()->second->width();
466 }
467
468 int rendered_height() {
469 EXPECT_FALSE(fake_video_renderers_.empty());
470 return fake_video_renderers_.empty()
471 ? 0
472 : fake_video_renderers_.begin()->second->height();
473 }
474
475 double rendered_aspect_ratio() {
476 if (rendered_height() == 0) {
477 return 0.0;
478 }
479 return static_cast<double>(rendered_width()) / rendered_height();
480 }
481
482 webrtc::VideoRotation rendered_rotation() {
483 EXPECT_FALSE(fake_video_renderers_.empty());
484 return fake_video_renderers_.empty()
485 ? webrtc::kVideoRotation_0
486 : fake_video_renderers_.begin()->second->rotation();
487 }
488
489 int local_rendered_width() {
490 return local_video_renderer_ ? local_video_renderer_->width() : 0;
491 }
492
493 int local_rendered_height() {
494 return local_video_renderer_ ? local_video_renderer_->height() : 0;
495 }
496
497 double local_rendered_aspect_ratio() {
498 if (local_rendered_height() == 0) {
499 return 0.0;
500 }
501 return static_cast<double>(local_rendered_width()) /
502 local_rendered_height();
503 }
504
505 size_t number_of_remote_streams() {
506 if (!pc()) {
507 return 0;
508 }
509 return pc()->remote_streams()->count();
510 }
511
512 StreamCollectionInterface* remote_streams() const {
513 if (!pc()) {
514 ADD_FAILURE();
515 return nullptr;
516 }
517 return pc()->remote_streams();
518 }
519
520 StreamCollectionInterface* local_streams() {
521 if (!pc()) {
522 ADD_FAILURE();
523 return nullptr;
524 }
525 return pc()->local_streams();
526 }
527
528 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
529 return pc()->signaling_state();
530 }
531
532 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
533 return pc()->ice_connection_state();
534 }
535
536 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
537 return pc()->ice_gathering_state();
538 }
539
540 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
541 // GetReceivers. They're updated automatically when a remote offer/answer
542 // from the fake signaling channel is applied, or when
543 // ResetRtpReceiverObservers below is called.
544 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
545 rtp_receiver_observers() {
546 return rtp_receiver_observers_;
547 }
548
549 void ResetRtpReceiverObservers() {
550 rtp_receiver_observers_.clear();
551 for (auto receiver : pc()->GetReceivers()) {
552 std::unique_ptr<MockRtpReceiverObserver> observer(
553 new MockRtpReceiverObserver(receiver->media_type()));
554 receiver->SetObserver(observer.get());
555 rtp_receiver_observers_.push_back(std::move(observer));
556 }
557 }
558
Steve Antonede9ca52017-10-16 13:04:27 -0700559 rtc::FakeNetworkManager* network() const {
560 return fake_network_manager_.get();
561 }
562 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
563
deadbeef1dcb1642017-03-29 21:08:16 -0700564 private:
565 explicit PeerConnectionWrapper(const std::string& debug_name)
566 : debug_name_(debug_name) {}
567
568 bool Init(
569 const MediaConstraintsInterface* constraints,
570 const PeerConnectionFactory::Options* options,
571 const PeerConnectionInterface::RTCConfiguration* config,
572 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
573 rtc::Thread* network_thread,
574 rtc::Thread* worker_thread) {
575 // There's an error in this test code if Init ends up being called twice.
576 RTC_DCHECK(!peer_connection_);
577 RTC_DCHECK(!peer_connection_factory_);
578
579 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700580 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700581
582 std::unique_ptr<cricket::PortAllocator> port_allocator(
583 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700584 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700585 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
586 if (!fake_audio_capture_module_) {
587 return false;
588 }
589 // Note that these factories don't end up getting used unless supported
590 // codecs are added to them.
591 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
592 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
593 rtc::Thread* const signaling_thread = rtc::Thread::Current();
594 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
595 network_thread, worker_thread, signaling_thread,
Karl Wiberg1b0eae32017-10-17 14:48:54 +0200596 fake_audio_capture_module_, webrtc::CreateBuiltinAudioEncoderFactory(),
597 webrtc::CreateBuiltinAudioDecoderFactory(), fake_video_encoder_factory_,
deadbeef1dcb1642017-03-29 21:08:16 -0700598 fake_video_decoder_factory_);
599 if (!peer_connection_factory_) {
600 return false;
601 }
602 if (options) {
603 peer_connection_factory_->SetOptions(*options);
604 }
605 peer_connection_ =
606 CreatePeerConnection(std::move(port_allocator), constraints, config,
607 std::move(cert_generator));
608 return peer_connection_.get() != nullptr;
609 }
610
611 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
612 std::unique_ptr<cricket::PortAllocator> port_allocator,
613 const MediaConstraintsInterface* constraints,
614 const PeerConnectionInterface::RTCConfiguration* config,
615 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) {
616 PeerConnectionInterface::RTCConfiguration modified_config;
617 // If |config| is null, this will result in a default configuration being
618 // used.
619 if (config) {
620 modified_config = *config;
621 }
622 // Disable resolution adaptation; we don't want it interfering with the
623 // test results.
624 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
625 // ratios and not specific resolutions, is this even necessary?
626 modified_config.set_cpu_adaptation(false);
627
628 return peer_connection_factory_->CreatePeerConnection(
629 modified_config, constraints, std::move(port_allocator),
630 std::move(cert_generator), this);
631 }
632
633 void set_signaling_message_receiver(
634 SignalingMessageReceiver* signaling_message_receiver) {
635 signaling_message_receiver_ = signaling_message_receiver;
636 }
637
638 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
639
Steve Antonede9ca52017-10-16 13:04:27 -0700640 void set_signal_ice_candidates(bool signal) {
641 signal_ice_candidates_ = signal;
642 }
643
deadbeef1dcb1642017-03-29 21:08:16 -0700644 void EnableVideoDecoderFactory() {
645 video_decoder_factory_enabled_ = true;
646 fake_video_decoder_factory_->AddSupportedVideoCodecType(
647 webrtc::kVideoCodecVP8);
648 }
649
650 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
deadbeef1dcb1642017-03-29 21:08:16 -0700651 const FakeConstraints& constraints,
652 webrtc::VideoRotation rotation) {
653 // Set max frame rate to 10fps to reduce the risk of test flakiness.
654 // TODO(deadbeef): Do something more robust.
655 FakeConstraints source_constraints = constraints;
656 source_constraints.SetMandatoryMaxFrameRate(10);
657
658 cricket::FakeVideoCapturer* fake_capturer =
659 new webrtc::FakePeriodicVideoCapturer();
660 fake_capturer->SetRotation(rotation);
661 video_capturers_.push_back(fake_capturer);
662 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
663 peer_connection_factory_->CreateVideoSource(fake_capturer,
664 &source_constraints);
665 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
deadbeefb1a15d72017-09-07 14:12:05 -0700666 peer_connection_factory_->CreateVideoTrack(rtc::CreateRandomUuid(),
667 source));
deadbeef1dcb1642017-03-29 21:08:16 -0700668 if (!local_video_renderer_) {
669 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
670 }
671 return track;
672 }
673
674 void HandleIncomingOffer(const std::string& msg) {
675 LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
676 std::unique_ptr<SessionDescriptionInterface> desc(
677 webrtc::CreateSessionDescription("offer", msg, nullptr));
678 if (received_sdp_munger_) {
679 received_sdp_munger_(desc->description());
680 }
681
682 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
683 // Setting a remote description may have changed the number of receivers,
684 // so reset the receiver observers.
685 ResetRtpReceiverObservers();
686 auto answer = CreateAnswer();
687 ASSERT_NE(nullptr, answer);
688 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
689 }
690
691 void HandleIncomingAnswer(const std::string& msg) {
692 LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
693 std::unique_ptr<SessionDescriptionInterface> desc(
694 webrtc::CreateSessionDescription("answer", msg, nullptr));
695 if (received_sdp_munger_) {
696 received_sdp_munger_(desc->description());
697 }
698
699 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
700 // Set the RtpReceiverObserver after receivers are created.
701 ResetRtpReceiverObservers();
702 }
703
704 // Returns null on failure.
705 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
706 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
707 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
708 pc()->CreateOffer(observer, offer_answer_options_);
709 return WaitForDescriptionFromObserver(observer);
710 }
711
712 // Returns null on failure.
713 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
714 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
715 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
716 pc()->CreateAnswer(observer, offer_answer_options_);
717 return WaitForDescriptionFromObserver(observer);
718 }
719
720 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
721 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer) {
722 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
723 if (!observer->result()) {
724 return nullptr;
725 }
726 auto description = observer->MoveDescription();
727 if (generated_sdp_munger_) {
728 generated_sdp_munger_(description->description());
729 }
730 return description;
731 }
732
733 // Setting the local description and sending the SDP message over the fake
734 // signaling channel are combined into the same method because the SDP
735 // message needs to be sent as soon as SetLocalDescription finishes, without
736 // waiting for the observer to be called. This ensures that ICE candidates
737 // don't outrace the description.
738 bool SetLocalDescriptionAndSendSdpMessage(
739 std::unique_ptr<SessionDescriptionInterface> desc) {
740 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
741 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
742 LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
743 std::string type = desc->type();
744 std::string sdp;
745 EXPECT_TRUE(desc->ToString(&sdp));
746 pc()->SetLocalDescription(observer, desc.release());
747 // As mentioned above, we need to send the message immediately after
748 // SetLocalDescription.
749 SendSdpMessage(type, sdp);
750 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
751 return true;
752 }
753
754 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
755 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
756 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
757 LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
758 pc()->SetRemoteDescription(observer, desc.release());
759 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
760 return observer->result();
761 }
762
763 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
764 // default).
765 void SendSdpMessage(const std::string& type, const std::string& msg) {
766 if (signaling_delay_ms_ == 0) {
767 RelaySdpMessageIfReceiverExists(type, msg);
768 } else {
769 invoker_.AsyncInvokeDelayed<void>(
770 RTC_FROM_HERE, rtc::Thread::Current(),
771 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
772 this, type, msg),
773 signaling_delay_ms_);
774 }
775 }
776
777 void RelaySdpMessageIfReceiverExists(const std::string& type,
778 const std::string& msg) {
779 if (signaling_message_receiver_) {
780 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
781 }
782 }
783
784 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
785 // default).
786 void SendIceMessage(const std::string& sdp_mid,
787 int sdp_mline_index,
788 const std::string& msg) {
789 if (signaling_delay_ms_ == 0) {
790 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
791 } else {
792 invoker_.AsyncInvokeDelayed<void>(
793 RTC_FROM_HERE, rtc::Thread::Current(),
794 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
795 this, sdp_mid, sdp_mline_index, msg),
796 signaling_delay_ms_);
797 }
798 }
799
800 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
801 int sdp_mline_index,
802 const std::string& msg) {
803 if (signaling_message_receiver_) {
804 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
805 msg);
806 }
807 }
808
809 // SignalingMessageReceiver callbacks.
810 void ReceiveSdpMessage(const std::string& type,
811 const std::string& msg) override {
812 if (type == webrtc::SessionDescriptionInterface::kOffer) {
813 HandleIncomingOffer(msg);
814 } else {
815 HandleIncomingAnswer(msg);
816 }
817 }
818
819 void ReceiveIceMessage(const std::string& sdp_mid,
820 int sdp_mline_index,
821 const std::string& msg) override {
822 LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
823 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
824 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
825 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
826 }
827
828 // PeerConnectionObserver callbacks.
829 void OnSignalingChange(
830 webrtc::PeerConnectionInterface::SignalingState new_state) override {
831 EXPECT_EQ(pc()->signaling_state(), new_state);
832 }
833 void OnAddStream(
834 rtc::scoped_refptr<MediaStreamInterface> media_stream) override {
835 media_stream->RegisterObserver(this);
836 for (size_t i = 0; i < media_stream->GetVideoTracks().size(); ++i) {
837 const std::string id = media_stream->GetVideoTracks()[i]->id();
838 ASSERT_TRUE(fake_video_renderers_.find(id) ==
839 fake_video_renderers_.end());
840 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
841 media_stream->GetVideoTracks()[i]));
842 }
843 }
844 void OnRemoveStream(
845 rtc::scoped_refptr<MediaStreamInterface> media_stream) override {}
846 void OnRenegotiationNeeded() override {}
847 void OnIceConnectionChange(
848 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
849 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700850 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700851 }
852 void OnIceGatheringChange(
853 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700854 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700855 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700856 }
857 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
858 LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
859
860 std::string ice_sdp;
861 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700862 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700863 // Remote party may be deleted.
864 return;
865 }
866 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
867 }
868 void OnDataChannel(
869 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
870 LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
871 data_channel_ = data_channel;
872 data_observer_.reset(new MockDataChannelObserver(data_channel));
873 }
874
875 // MediaStreamInterface callback
876 void OnChanged() override {
877 // Track added or removed from MediaStream, so update our renderers.
878 rtc::scoped_refptr<StreamCollectionInterface> remote_streams =
879 pc()->remote_streams();
880 // Remove renderers for tracks that were removed.
881 for (auto it = fake_video_renderers_.begin();
882 it != fake_video_renderers_.end();) {
883 if (remote_streams->FindVideoTrack(it->first) == nullptr) {
884 auto to_remove = it++;
885 removed_fake_video_renderers_.push_back(std::move(to_remove->second));
886 fake_video_renderers_.erase(to_remove);
887 } else {
888 ++it;
889 }
890 }
891 // Create renderers for new video tracks.
892 for (size_t stream_index = 0; stream_index < remote_streams->count();
893 ++stream_index) {
894 MediaStreamInterface* remote_stream = remote_streams->at(stream_index);
895 for (size_t track_index = 0;
896 track_index < remote_stream->GetVideoTracks().size();
897 ++track_index) {
898 const std::string id =
899 remote_stream->GetVideoTracks()[track_index]->id();
900 if (fake_video_renderers_.find(id) != fake_video_renderers_.end()) {
901 continue;
902 }
903 fake_video_renderers_[id].reset(new webrtc::FakeVideoTrackRenderer(
904 remote_stream->GetVideoTracks()[track_index]));
905 }
906 }
907 }
908
909 std::string debug_name_;
910
911 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
912
913 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
914 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
915 peer_connection_factory_;
916
Steve Antonede9ca52017-10-16 13:04:27 -0700917 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700918 // Needed to keep track of number of frames sent.
919 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
920 // Needed to keep track of number of frames received.
921 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
922 fake_video_renderers_;
923 // Needed to ensure frames aren't received for removed tracks.
924 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
925 removed_fake_video_renderers_;
926 // Needed to keep track of number of frames received when external decoder
927 // used.
928 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
929 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
930 bool video_decoder_factory_enabled_ = false;
931
932 // For remote peer communication.
933 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
934 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700935 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -0700936
937 // Store references to the video capturers we've created, so that we can stop
938 // them, if required.
939 std::vector<cricket::FakeVideoCapturer*> video_capturers_;
940 // |local_video_renderer_| attached to the first created local video track.
941 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
942
943 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
944 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
945 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
946
947 rtc::scoped_refptr<DataChannelInterface> data_channel_;
948 std::unique_ptr<MockDataChannelObserver> data_observer_;
949
950 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
951
Steve Antonede9ca52017-10-16 13:04:27 -0700952 std::vector<PeerConnectionInterface::IceConnectionState>
953 ice_connection_state_history_;
954 std::vector<PeerConnectionInterface::IceGatheringState>
955 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700956
957 rtc::AsyncInvoker invoker_;
958
959 friend class PeerConnectionIntegrationTest;
960};
961
Elad Alon99c3fe52017-10-13 16:29:40 +0200962class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
963 public:
964 virtual ~MockRtcEventLogOutput() = default;
965 MOCK_CONST_METHOD0(IsActive, bool());
966 MOCK_METHOD1(Write, bool(const std::string&));
967};
968
deadbeef1dcb1642017-03-29 21:08:16 -0700969// Tests two PeerConnections connecting to each other end-to-end, using a
970// virtual network, fake A/V capture and fake encoder/decoders. The
971// PeerConnections share the threads/socket servers, but use separate versions
972// of everything else (including "PeerConnectionFactory"s).
973class PeerConnectionIntegrationTest : public testing::Test {
974 public:
975 PeerConnectionIntegrationTest()
deadbeef98e186c2017-05-16 18:00:06 -0700976 : ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -0700977 fss_(new rtc::FirewallSocketServer(ss_.get())),
978 network_thread_(new rtc::Thread(fss_.get())),
deadbeef1dcb1642017-03-29 21:08:16 -0700979 worker_thread_(rtc::Thread::Create()) {
980 RTC_CHECK(network_thread_->Start());
981 RTC_CHECK(worker_thread_->Start());
982 }
983
984 ~PeerConnectionIntegrationTest() {
985 if (caller_) {
986 caller_->set_signaling_message_receiver(nullptr);
987 }
988 if (callee_) {
989 callee_->set_signaling_message_receiver(nullptr);
990 }
991 }
992
993 bool SignalingStateStable() {
994 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
995 }
996
deadbeef71452802017-05-07 17:21:01 -0700997 bool DtlsConnected() {
998 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
999 // are connected. This is an important distinction. Once we have separate
1000 // ICE and DTLS state, this check needs to use the DTLS state.
1001 return (callee()->ice_connection_state() ==
1002 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1003 callee()->ice_connection_state() ==
1004 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1005 (caller()->ice_connection_state() ==
1006 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1007 caller()->ice_connection_state() ==
1008 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
1009 }
1010
deadbeef1dcb1642017-03-29 21:08:16 -07001011 bool CreatePeerConnectionWrappers() {
1012 return CreatePeerConnectionWrappersWithConfig(
1013 PeerConnectionInterface::RTCConfiguration(),
1014 PeerConnectionInterface::RTCConfiguration());
1015 }
1016
1017 bool CreatePeerConnectionWrappersWithConstraints(
1018 MediaConstraintsInterface* caller_constraints,
1019 MediaConstraintsInterface* callee_constraints) {
1020 caller_.reset(PeerConnectionWrapper::CreateWithConstraints(
1021 "Caller", caller_constraints, network_thread_.get(),
1022 worker_thread_.get()));
1023 callee_.reset(PeerConnectionWrapper::CreateWithConstraints(
1024 "Callee", callee_constraints, network_thread_.get(),
1025 worker_thread_.get()));
1026 return caller_ && callee_;
1027 }
1028
1029 bool CreatePeerConnectionWrappersWithConfig(
1030 const PeerConnectionInterface::RTCConfiguration& caller_config,
1031 const PeerConnectionInterface::RTCConfiguration& callee_config) {
1032 caller_.reset(PeerConnectionWrapper::CreateWithConfig(
1033 "Caller", caller_config, network_thread_.get(), worker_thread_.get()));
1034 callee_.reset(PeerConnectionWrapper::CreateWithConfig(
1035 "Callee", callee_config, network_thread_.get(), worker_thread_.get()));
1036 return caller_ && callee_;
1037 }
1038
1039 bool CreatePeerConnectionWrappersWithOptions(
1040 const PeerConnectionFactory::Options& caller_options,
1041 const PeerConnectionFactory::Options& callee_options) {
1042 caller_.reset(PeerConnectionWrapper::CreateWithOptions(
1043 "Caller", caller_options, network_thread_.get(), worker_thread_.get()));
1044 callee_.reset(PeerConnectionWrapper::CreateWithOptions(
1045 "Callee", callee_options, network_thread_.get(), worker_thread_.get()));
1046 return caller_ && callee_;
1047 }
1048
1049 PeerConnectionWrapper* CreatePeerConnectionWrapperWithAlternateKey() {
1050 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1051 new FakeRTCCertificateGenerator());
1052 cert_generator->use_alternate_key();
1053
1054 // Make sure the new client is using a different certificate.
1055 return PeerConnectionWrapper::CreateWithDtlsIdentityStore(
1056 "New Peer", std::move(cert_generator), network_thread_.get(),
1057 worker_thread_.get());
1058 }
1059
1060 // Once called, SDP blobs and ICE candidates will be automatically signaled
1061 // between PeerConnections.
1062 void ConnectFakeSignaling() {
1063 caller_->set_signaling_message_receiver(callee_.get());
1064 callee_->set_signaling_message_receiver(caller_.get());
1065 }
1066
Steve Antonede9ca52017-10-16 13:04:27 -07001067 // Once called, SDP blobs will be automatically signaled between
1068 // PeerConnections. Note that ICE candidates will not be signaled unless they
1069 // are in the exchanged SDP blobs.
1070 void ConnectFakeSignalingForSdpOnly() {
1071 ConnectFakeSignaling();
1072 SetSignalIceCandidates(false);
1073 }
1074
deadbeef1dcb1642017-03-29 21:08:16 -07001075 void SetSignalingDelayMs(int delay_ms) {
1076 caller_->set_signaling_delay_ms(delay_ms);
1077 callee_->set_signaling_delay_ms(delay_ms);
1078 }
1079
Steve Antonede9ca52017-10-16 13:04:27 -07001080 void SetSignalIceCandidates(bool signal) {
1081 caller_->set_signal_ice_candidates(signal);
1082 callee_->set_signal_ice_candidates(signal);
1083 }
1084
deadbeef1dcb1642017-03-29 21:08:16 -07001085 void EnableVideoDecoderFactory() {
1086 caller_->EnableVideoDecoderFactory();
1087 callee_->EnableVideoDecoderFactory();
1088 }
1089
1090 // Messages may get lost on the unreliable DataChannel, so we send multiple
1091 // times to avoid test flakiness.
1092 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1093 const std::string& data,
1094 int retries) {
1095 for (int i = 0; i < retries; ++i) {
1096 dc->Send(DataBuffer(data));
1097 }
1098 }
1099
1100 rtc::Thread* network_thread() { return network_thread_.get(); }
1101
1102 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1103
1104 PeerConnectionWrapper* caller() { return caller_.get(); }
1105
1106 // Set the |caller_| to the |wrapper| passed in and return the
1107 // original |caller_|.
1108 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1109 PeerConnectionWrapper* wrapper) {
1110 PeerConnectionWrapper* old = caller_.release();
1111 caller_.reset(wrapper);
1112 return old;
1113 }
1114
1115 PeerConnectionWrapper* callee() { return callee_.get(); }
1116
1117 // Set the |callee_| to the |wrapper| passed in and return the
1118 // original |callee_|.
1119 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1120 PeerConnectionWrapper* wrapper) {
1121 PeerConnectionWrapper* old = callee_.release();
1122 callee_.reset(wrapper);
1123 return old;
1124 }
1125
Steve Antonede9ca52017-10-16 13:04:27 -07001126 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1127
deadbeef1dcb1642017-03-29 21:08:16 -07001128 // Expects the provided number of new frames to be received within |wait_ms|.
1129 // "New frames" meaning that it waits for the current frame counts to
1130 // *increase* by the provided values. For video, uses
1131 // RecievedVideoFramesForEachTrack for the case of multiple video tracks
1132 // being received.
1133 void ExpectNewFramesReceivedWithWait(
1134 int expected_caller_received_audio_frames,
1135 int expected_caller_received_video_frames,
1136 int expected_callee_received_audio_frames,
1137 int expected_callee_received_video_frames,
1138 int wait_ms) {
1139 // Add current frame counts to the provided values, in order to wait for
1140 // the frame count to increase.
1141 expected_caller_received_audio_frames += caller()->audio_frames_received();
1142 expected_caller_received_video_frames +=
1143 caller()->min_video_frames_received_per_track();
1144 expected_callee_received_audio_frames += callee()->audio_frames_received();
1145 expected_callee_received_video_frames +=
1146 callee()->min_video_frames_received_per_track();
1147
1148 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
1149 expected_caller_received_audio_frames &&
1150 caller()->min_video_frames_received_per_track() >=
1151 expected_caller_received_video_frames &&
1152 callee()->audio_frames_received() >=
1153 expected_callee_received_audio_frames &&
1154 callee()->min_video_frames_received_per_track() >=
1155 expected_callee_received_video_frames,
1156 wait_ms);
1157
1158 // After the combined wait, do an "expect" for each individual count, to
1159 // print out a more detailed message upon failure.
1160 EXPECT_GE(caller()->audio_frames_received(),
1161 expected_caller_received_audio_frames);
1162 EXPECT_GE(caller()->min_video_frames_received_per_track(),
1163 expected_caller_received_video_frames);
1164 EXPECT_GE(callee()->audio_frames_received(),
1165 expected_callee_received_audio_frames);
1166 EXPECT_GE(callee()->min_video_frames_received_per_track(),
1167 expected_callee_received_video_frames);
1168 }
1169
1170 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1171 bool remote_gcm_enabled,
1172 int expected_cipher_suite) {
1173 PeerConnectionFactory::Options caller_options;
1174 caller_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled;
1175 PeerConnectionFactory::Options callee_options;
1176 callee_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled;
1177 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1178 callee_options));
1179 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1180 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1181 caller()->pc()->RegisterUMAObserver(caller_observer);
1182 ConnectFakeSignaling();
1183 caller()->AddAudioVideoMediaStream();
1184 callee()->AddAudioVideoMediaStream();
1185 caller()->CreateAndSetAndSignalOffer();
1186 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1187 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001188 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001189 EXPECT_EQ(
1190 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1191 expected_cipher_suite));
1192 caller()->pc()->RegisterUMAObserver(nullptr);
1193 }
1194
1195 private:
1196 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001197 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001198 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001199 // |network_thread_| and |worker_thread_| are used by both
1200 // |caller_| and |callee_| so they must be destroyed
1201 // later.
1202 std::unique_ptr<rtc::Thread> network_thread_;
1203 std::unique_ptr<rtc::Thread> worker_thread_;
1204 std::unique_ptr<PeerConnectionWrapper> caller_;
1205 std::unique_ptr<PeerConnectionWrapper> callee_;
1206};
1207
1208// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1209// includes testing that the callback is invoked if an observer is connected
1210// after the first packet has already been received.
1211TEST_F(PeerConnectionIntegrationTest,
1212 RtpReceiverObserverOnFirstPacketReceived) {
1213 ASSERT_TRUE(CreatePeerConnectionWrappers());
1214 ConnectFakeSignaling();
1215 caller()->AddAudioVideoMediaStream();
1216 callee()->AddAudioVideoMediaStream();
1217 // Start offer/answer exchange and wait for it to complete.
1218 caller()->CreateAndSetAndSignalOffer();
1219 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1220 // Should be one receiver each for audio/video.
1221 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1222 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1223 // Wait for all "first packet received" callbacks to be fired.
1224 EXPECT_TRUE_WAIT(
1225 std::all_of(caller()->rtp_receiver_observers().begin(),
1226 caller()->rtp_receiver_observers().end(),
1227 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1228 return o->first_packet_received();
1229 }),
1230 kMaxWaitForFramesMs);
1231 EXPECT_TRUE_WAIT(
1232 std::all_of(callee()->rtp_receiver_observers().begin(),
1233 callee()->rtp_receiver_observers().end(),
1234 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1235 return o->first_packet_received();
1236 }),
1237 kMaxWaitForFramesMs);
1238 // If new observers are set after the first packet was already received, the
1239 // callback should still be invoked.
1240 caller()->ResetRtpReceiverObservers();
1241 callee()->ResetRtpReceiverObservers();
1242 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1243 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1244 EXPECT_TRUE(
1245 std::all_of(caller()->rtp_receiver_observers().begin(),
1246 caller()->rtp_receiver_observers().end(),
1247 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1248 return o->first_packet_received();
1249 }));
1250 EXPECT_TRUE(
1251 std::all_of(callee()->rtp_receiver_observers().begin(),
1252 callee()->rtp_receiver_observers().end(),
1253 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1254 return o->first_packet_received();
1255 }));
1256}
1257
1258class DummyDtmfObserver : public DtmfSenderObserverInterface {
1259 public:
1260 DummyDtmfObserver() : completed_(false) {}
1261
1262 // Implements DtmfSenderObserverInterface.
1263 void OnToneChange(const std::string& tone) override {
1264 tones_.push_back(tone);
1265 if (tone.empty()) {
1266 completed_ = true;
1267 }
1268 }
1269
1270 const std::vector<std::string>& tones() const { return tones_; }
1271 bool completed() const { return completed_; }
1272
1273 private:
1274 bool completed_;
1275 std::vector<std::string> tones_;
1276};
1277
1278// Assumes |sender| already has an audio track added and the offer/answer
1279// exchange is done.
1280void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1281 PeerConnectionWrapper* receiver) {
1282 DummyDtmfObserver observer;
1283 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender;
1284
1285 // We should be able to create a DTMF sender from a local track.
1286 webrtc::AudioTrackInterface* localtrack =
1287 sender->local_streams()->at(0)->GetAudioTracks()[0];
1288 dtmf_sender = sender->pc()->CreateDtmfSender(localtrack);
1289 ASSERT_NE(nullptr, dtmf_sender.get());
1290 dtmf_sender->RegisterObserver(&observer);
1291
1292 // Test the DtmfSender object just created.
1293 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1294 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1295
1296 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1297 std::vector<std::string> tones = {"1", "a", ""};
1298 EXPECT_EQ(tones, observer.tones());
1299 dtmf_sender->UnregisterObserver();
1300 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1301}
1302
1303// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1304// direction).
1305TEST_F(PeerConnectionIntegrationTest, DtmfSenderObserver) {
1306 ASSERT_TRUE(CreatePeerConnectionWrappers());
1307 ConnectFakeSignaling();
1308 // Only need audio for DTMF.
1309 caller()->AddAudioOnlyMediaStream();
1310 callee()->AddAudioOnlyMediaStream();
1311 caller()->CreateAndSetAndSignalOffer();
1312 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001313 // DTLS must finish before the DTMF sender can be used reliably.
1314 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001315 TestDtmfFromSenderToReceiver(caller(), callee());
1316 TestDtmfFromSenderToReceiver(callee(), caller());
1317}
1318
1319// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1320// between two connections, using DTLS-SRTP.
1321TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
1322 ASSERT_TRUE(CreatePeerConnectionWrappers());
1323 ConnectFakeSignaling();
1324 // Do normal offer/answer and wait for some frames to be received in each
1325 // direction.
1326 caller()->AddAudioVideoMediaStream();
1327 callee()->AddAudioVideoMediaStream();
1328 caller()->CreateAndSetAndSignalOffer();
1329 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1330 ExpectNewFramesReceivedWithWait(
1331 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1332 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1333 kMaxWaitForFramesMs);
1334}
1335
1336// Uses SDES instead of DTLS for key agreement.
1337TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
1338 PeerConnectionInterface::RTCConfiguration sdes_config;
1339 sdes_config.enable_dtls_srtp.emplace(false);
1340 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1341 ConnectFakeSignaling();
1342
1343 // Do normal offer/answer and wait for some frames to be received in each
1344 // direction.
1345 caller()->AddAudioVideoMediaStream();
1346 callee()->AddAudioVideoMediaStream();
1347 caller()->CreateAndSetAndSignalOffer();
1348 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1349 ExpectNewFramesReceivedWithWait(
1350 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1351 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1352 kMaxWaitForFramesMs);
1353}
1354
Steve Anton8c0f7a72017-10-03 10:03:10 -07001355// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1356// certificate once the DTLS handshake has finished.
1357TEST_F(PeerConnectionIntegrationTest,
1358 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1359 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1360 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1361 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1362 return pc->GetRemoteAudioSSLCertificate();
1363 };
1364
1365 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1366 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1367
1368 // Configure each side with a known certificate so they can be compared later.
1369 PeerConnectionInterface::RTCConfiguration caller_config;
1370 caller_config.enable_dtls_srtp.emplace(true);
1371 caller_config.certificates.push_back(caller_cert);
1372 PeerConnectionInterface::RTCConfiguration callee_config;
1373 callee_config.enable_dtls_srtp.emplace(true);
1374 callee_config.certificates.push_back(callee_cert);
1375 ASSERT_TRUE(
1376 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1377 ConnectFakeSignaling();
1378
1379 // When first initialized, there should not be a remote SSL certificate (and
1380 // calling this method should not crash).
1381 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1382 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
1383
1384 caller()->AddAudioOnlyMediaStream();
1385 callee()->AddAudioOnlyMediaStream();
1386 caller()->CreateAndSetAndSignalOffer();
1387 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1388 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1389
1390 // Once DTLS has been connected, each side should return the other's SSL
1391 // certificate when calling GetRemoteAudioSSLCertificate.
1392
1393 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1394 ASSERT_TRUE(caller_remote_cert);
1395 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1396 caller_remote_cert->ToPEMString());
1397
1398 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1399 ASSERT_TRUE(callee_remote_cert);
1400 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1401 callee_remote_cert->ToPEMString());
1402}
1403
deadbeef1dcb1642017-03-29 21:08:16 -07001404// This test sets up a call between two parties (using DTLS) and tests that we
1405// can get a video aspect ratio of 16:9.
1406TEST_F(PeerConnectionIntegrationTest, SendAndReceive16To9AspectRatio) {
1407 ASSERT_TRUE(CreatePeerConnectionWrappers());
1408 ConnectFakeSignaling();
1409
1410 // Add video tracks with 16:9 constraint.
1411 FakeConstraints constraints;
1412 double requested_ratio = 16.0 / 9;
1413 constraints.SetMandatoryMinAspectRatio(requested_ratio);
1414 caller()->AddMediaStreamFromTracks(
1415 nullptr, caller()->CreateLocalVideoTrackWithConstraints(constraints));
1416 callee()->AddMediaStreamFromTracks(
1417 nullptr, callee()->CreateLocalVideoTrackWithConstraints(constraints));
1418
1419 // Do normal offer/answer and wait for at least one frame to be received in
1420 // each direction.
1421 caller()->CreateAndSetAndSignalOffer();
1422 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1423 callee()->min_video_frames_received_per_track() > 0,
1424 kMaxWaitForFramesMs);
1425
1426 // Check rendered aspect ratio.
1427 EXPECT_EQ(requested_ratio, caller()->local_rendered_aspect_ratio());
1428 EXPECT_EQ(requested_ratio, caller()->rendered_aspect_ratio());
1429 EXPECT_EQ(requested_ratio, callee()->local_rendered_aspect_ratio());
1430 EXPECT_EQ(requested_ratio, callee()->rendered_aspect_ratio());
1431}
1432
1433// This test sets up a call between two parties with a source resolution of
1434// 1280x720 and verifies that a 16:9 aspect ratio is received.
1435TEST_F(PeerConnectionIntegrationTest,
1436 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1437 ASSERT_TRUE(CreatePeerConnectionWrappers());
1438 ConnectFakeSignaling();
1439
1440 // Similar to above test, but uses MandatoryMin[Width/Height] constraint
1441 // instead of aspect ratio constraint.
1442 FakeConstraints constraints;
1443 constraints.SetMandatoryMinWidth(1280);
1444 constraints.SetMandatoryMinHeight(720);
1445 caller()->AddMediaStreamFromTracks(
1446 nullptr, caller()->CreateLocalVideoTrackWithConstraints(constraints));
1447 callee()->AddMediaStreamFromTracks(
1448 nullptr, callee()->CreateLocalVideoTrackWithConstraints(constraints));
1449
1450 // Do normal offer/answer and wait for at least one frame to be received in
1451 // each direction.
1452 caller()->CreateAndSetAndSignalOffer();
1453 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1454 callee()->min_video_frames_received_per_track() > 0,
1455 kMaxWaitForFramesMs);
1456
1457 // Check rendered aspect ratio.
1458 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1459 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1460 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1461 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1462}
1463
1464// This test sets up an one-way call, with media only from caller to
1465// callee.
1466TEST_F(PeerConnectionIntegrationTest, OneWayMediaCall) {
1467 ASSERT_TRUE(CreatePeerConnectionWrappers());
1468 ConnectFakeSignaling();
1469 caller()->AddAudioVideoMediaStream();
1470 caller()->CreateAndSetAndSignalOffer();
1471 int caller_received_frames = 0;
1472 ExpectNewFramesReceivedWithWait(
1473 caller_received_frames, caller_received_frames,
1474 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1475 kMaxWaitForFramesMs);
1476}
1477
1478// This test sets up a audio call initially, with the callee rejecting video
1479// initially. Then later the callee decides to upgrade to audio/video, and
1480// initiates a new offer/answer exchange.
1481TEST_F(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
1482 ASSERT_TRUE(CreatePeerConnectionWrappers());
1483 ConnectFakeSignaling();
1484 // Initially, offer an audio/video stream from the caller, but refuse to
1485 // send/receive video on the callee side.
1486 caller()->AddAudioVideoMediaStream();
deadbeef4389b4d2017-09-07 09:07:36 -07001487 callee()->AddAudioOnlyMediaStream();
deadbeef1dcb1642017-03-29 21:08:16 -07001488 PeerConnectionInterface::RTCOfferAnswerOptions options;
1489 options.offer_to_receive_video = 0;
1490 callee()->SetOfferAnswerOptions(options);
1491 // Do offer/answer and make sure audio is still received end-to-end.
1492 caller()->CreateAndSetAndSignalOffer();
1493 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1494 ExpectNewFramesReceivedWithWait(kDefaultExpectedAudioFrameCount, 0,
1495 kDefaultExpectedAudioFrameCount, 0,
1496 kMaxWaitForFramesMs);
1497 // Sanity check that the callee's description has a rejected video section.
1498 ASSERT_NE(nullptr, callee()->pc()->local_description());
1499 const ContentInfo* callee_video_content =
1500 GetFirstVideoContent(callee()->pc()->local_description()->description());
1501 ASSERT_NE(nullptr, callee_video_content);
1502 EXPECT_TRUE(callee_video_content->rejected);
1503 // Now negotiate with video and ensure negotiation succeeds, with video
1504 // frames and additional audio frames being received.
deadbeefb1a15d72017-09-07 14:12:05 -07001505 callee()->AddVideoOnlyMediaStream();
deadbeef1dcb1642017-03-29 21:08:16 -07001506 options.offer_to_receive_video = 1;
1507 callee()->SetOfferAnswerOptions(options);
1508 callee()->CreateAndSetAndSignalOffer();
1509 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1510 // Expect additional audio frames to be received after the upgrade.
1511 ExpectNewFramesReceivedWithWait(
1512 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1513 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1514 kMaxWaitForFramesMs);
1515}
1516
deadbeef4389b4d2017-09-07 09:07:36 -07001517// Simpler than the above test; just add an audio track to an established
1518// video-only connection.
1519TEST_F(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
1520 ASSERT_TRUE(CreatePeerConnectionWrappers());
1521 ConnectFakeSignaling();
1522 // Do initial offer/answer with just a video track.
1523 caller()->AddVideoOnlyMediaStream();
1524 callee()->AddVideoOnlyMediaStream();
1525 caller()->CreateAndSetAndSignalOffer();
1526 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1527 // Now add an audio track and do another offer/answer.
deadbeefb1a15d72017-09-07 14:12:05 -07001528 caller()->AddAudioOnlyMediaStream();
1529 callee()->AddAudioOnlyMediaStream();
deadbeef4389b4d2017-09-07 09:07:36 -07001530 caller()->CreateAndSetAndSignalOffer();
1531 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1532 // Ensure both audio and video frames are received end-to-end.
1533 ExpectNewFramesReceivedWithWait(
1534 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1535 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1536 kMaxWaitForFramesMs);
1537}
1538
deadbeef1dcb1642017-03-29 21:08:16 -07001539// This test sets up a call that's transferred to a new caller with a different
1540// DTLS fingerprint.
1541TEST_F(PeerConnectionIntegrationTest, CallTransferredForCallee) {
1542 ASSERT_TRUE(CreatePeerConnectionWrappers());
1543 ConnectFakeSignaling();
1544 caller()->AddAudioVideoMediaStream();
1545 callee()->AddAudioVideoMediaStream();
1546 caller()->CreateAndSetAndSignalOffer();
1547 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1548
1549 // Keep the original peer around which will still send packets to the
1550 // receiving client. These SRTP packets will be dropped.
1551 std::unique_ptr<PeerConnectionWrapper> original_peer(
1552 SetCallerPcWrapperAndReturnCurrent(
1553 CreatePeerConnectionWrapperWithAlternateKey()));
1554 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1555 // directly above.
1556 original_peer->pc()->Close();
1557
1558 ConnectFakeSignaling();
1559 caller()->AddAudioVideoMediaStream();
1560 caller()->CreateAndSetAndSignalOffer();
1561 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1562 // Wait for some additional frames to be transmitted end-to-end.
1563 ExpectNewFramesReceivedWithWait(
1564 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1565 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1566 kMaxWaitForFramesMs);
1567}
1568
1569// This test sets up a call that's transferred to a new callee with a different
1570// DTLS fingerprint.
1571TEST_F(PeerConnectionIntegrationTest, CallTransferredForCaller) {
1572 ASSERT_TRUE(CreatePeerConnectionWrappers());
1573 ConnectFakeSignaling();
1574 caller()->AddAudioVideoMediaStream();
1575 callee()->AddAudioVideoMediaStream();
1576 caller()->CreateAndSetAndSignalOffer();
1577 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1578
1579 // Keep the original peer around which will still send packets to the
1580 // receiving client. These SRTP packets will be dropped.
1581 std::unique_ptr<PeerConnectionWrapper> original_peer(
1582 SetCalleePcWrapperAndReturnCurrent(
1583 CreatePeerConnectionWrapperWithAlternateKey()));
1584 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1585 // directly above.
1586 original_peer->pc()->Close();
1587
1588 ConnectFakeSignaling();
1589 callee()->AddAudioVideoMediaStream();
1590 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1591 caller()->CreateAndSetAndSignalOffer();
1592 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1593 // Wait for some additional frames to be transmitted end-to-end.
1594 ExpectNewFramesReceivedWithWait(
1595 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1596 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1597 kMaxWaitForFramesMs);
1598}
1599
1600// This test sets up a non-bundled call and negotiates bundling at the same
1601// time as starting an ICE restart. When bundling is in effect in the restart,
1602// the DTLS-SRTP context should be successfully reset.
1603TEST_F(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
1604 ASSERT_TRUE(CreatePeerConnectionWrappers());
1605 ConnectFakeSignaling();
1606
1607 caller()->AddAudioVideoMediaStream();
1608 callee()->AddAudioVideoMediaStream();
1609 // Remove the bundle group from the SDP received by the callee.
1610 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1611 desc->RemoveGroupByName("BUNDLE");
1612 });
1613 caller()->CreateAndSetAndSignalOffer();
1614 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1615 ExpectNewFramesReceivedWithWait(
1616 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1617 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1618 kMaxWaitForFramesMs);
1619
1620 // Now stop removing the BUNDLE group, and trigger an ICE restart.
1621 callee()->SetReceivedSdpMunger(nullptr);
1622 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1623 caller()->CreateAndSetAndSignalOffer();
1624 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1625
1626 // Expect additional frames to be received after the ICE restart.
1627 ExpectNewFramesReceivedWithWait(
1628 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1629 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1630 kMaxWaitForFramesMs);
1631}
1632
1633// Test CVO (Coordination of Video Orientation). If a video source is rotated
1634// and both peers support the CVO RTP header extension, the actual video frames
1635// don't need to be encoded in different resolutions, since the rotation is
1636// communicated through the RTP header extension.
1637TEST_F(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
1638 ASSERT_TRUE(CreatePeerConnectionWrappers());
1639 ConnectFakeSignaling();
1640 // Add rotated video tracks.
1641 caller()->AddMediaStreamFromTracks(
1642 nullptr,
1643 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
1644 callee()->AddMediaStreamFromTracks(
1645 nullptr,
1646 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1647
1648 // Wait for video frames to be received by both sides.
1649 caller()->CreateAndSetAndSignalOffer();
1650 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1651 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1652 callee()->min_video_frames_received_per_track() > 0,
1653 kMaxWaitForFramesMs);
1654
1655 // Ensure that the aspect ratio is unmodified.
1656 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1657 // not just assumed.
1658 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
1659 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
1660 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
1661 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
1662 // Ensure that the CVO bits were surfaced to the renderer.
1663 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
1664 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
1665}
1666
1667// Test that when the CVO extension isn't supported, video is rotated the
1668// old-fashioned way, by encoding rotated frames.
1669TEST_F(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
1670 ASSERT_TRUE(CreatePeerConnectionWrappers());
1671 ConnectFakeSignaling();
1672 // Add rotated video tracks.
1673 caller()->AddMediaStreamFromTracks(
1674 nullptr,
1675 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
1676 callee()->AddMediaStreamFromTracks(
1677 nullptr,
1678 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1679
1680 // Remove the CVO extension from the offered SDP.
1681 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1682 cricket::VideoContentDescription* video =
1683 GetFirstVideoContentDescription(desc);
1684 video->ClearRtpHeaderExtensions();
1685 });
1686 // Wait for video frames to be received by both sides.
1687 caller()->CreateAndSetAndSignalOffer();
1688 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1689 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1690 callee()->min_video_frames_received_per_track() > 0,
1691 kMaxWaitForFramesMs);
1692
1693 // Expect that the aspect ratio is inversed to account for the 90/270 degree
1694 // rotation.
1695 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1696 // not just assumed.
1697 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
1698 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
1699 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
1700 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
1701 // Expect that each endpoint is unaware of the rotation of the other endpoint.
1702 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
1703 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
1704}
1705
1706// TODO(deadbeef): The tests below rely on RTCOfferAnswerOptions to reject an
1707// m= section. When we implement Unified Plan SDP, the right way to do this
1708// would be by stopping an RtpTransceiver.
1709
1710// Test that if the answerer rejects the audio m= section, no audio is sent or
1711// received, but video still can be.
1712TEST_F(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
1713 ASSERT_TRUE(CreatePeerConnectionWrappers());
1714 ConnectFakeSignaling();
1715 caller()->AddAudioVideoMediaStream();
1716 // Only add video track for callee, and set offer_to_receive_audio to 0, so
1717 // it will reject the audio m= section completely.
1718 PeerConnectionInterface::RTCOfferAnswerOptions options;
1719 options.offer_to_receive_audio = 0;
1720 callee()->SetOfferAnswerOptions(options);
1721 callee()->AddMediaStreamFromTracks(nullptr,
1722 callee()->CreateLocalVideoTrack());
1723 // Do offer/answer and wait for successful end-to-end video frames.
1724 caller()->CreateAndSetAndSignalOffer();
1725 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1726 ExpectNewFramesReceivedWithWait(0, kDefaultExpectedVideoFrameCount, 0,
1727 kDefaultExpectedVideoFrameCount,
1728 kMaxWaitForFramesMs);
1729 // Shouldn't have received audio frames at any point.
1730 EXPECT_EQ(0, caller()->audio_frames_received());
1731 EXPECT_EQ(0, callee()->audio_frames_received());
1732 // Sanity check that the callee's description has a rejected audio section.
1733 ASSERT_NE(nullptr, callee()->pc()->local_description());
1734 const ContentInfo* callee_audio_content =
1735 GetFirstAudioContent(callee()->pc()->local_description()->description());
1736 ASSERT_NE(nullptr, callee_audio_content);
1737 EXPECT_TRUE(callee_audio_content->rejected);
1738}
1739
1740// Test that if the answerer rejects the video m= section, no video is sent or
1741// received, but audio still can be.
1742TEST_F(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
1743 ASSERT_TRUE(CreatePeerConnectionWrappers());
1744 ConnectFakeSignaling();
1745 caller()->AddAudioVideoMediaStream();
1746 // Only add audio track for callee, and set offer_to_receive_video to 0, so
1747 // it will reject the video m= section completely.
1748 PeerConnectionInterface::RTCOfferAnswerOptions options;
1749 options.offer_to_receive_video = 0;
1750 callee()->SetOfferAnswerOptions(options);
1751 callee()->AddMediaStreamFromTracks(callee()->CreateLocalAudioTrack(),
1752 nullptr);
1753 // Do offer/answer and wait for successful end-to-end audio frames.
1754 caller()->CreateAndSetAndSignalOffer();
1755 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1756 ExpectNewFramesReceivedWithWait(kDefaultExpectedAudioFrameCount, 0,
1757 kDefaultExpectedAudioFrameCount, 0,
1758 kMaxWaitForFramesMs);
1759 // Shouldn't have received video frames at any point.
1760 EXPECT_EQ(0, caller()->total_video_frames_received());
1761 EXPECT_EQ(0, callee()->total_video_frames_received());
1762 // Sanity check that the callee's description has a rejected video section.
1763 ASSERT_NE(nullptr, callee()->pc()->local_description());
1764 const ContentInfo* callee_video_content =
1765 GetFirstVideoContent(callee()->pc()->local_description()->description());
1766 ASSERT_NE(nullptr, callee_video_content);
1767 EXPECT_TRUE(callee_video_content->rejected);
1768}
1769
1770// Test that if the answerer rejects both audio and video m= sections, nothing
1771// bad happens.
1772// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
1773// test anything but the fact that negotiation succeeds, which doesn't mean
1774// much.
1775TEST_F(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
1776 ASSERT_TRUE(CreatePeerConnectionWrappers());
1777 ConnectFakeSignaling();
1778 caller()->AddAudioVideoMediaStream();
1779 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
1780 // will reject both audio and video m= sections.
1781 PeerConnectionInterface::RTCOfferAnswerOptions options;
1782 options.offer_to_receive_audio = 0;
1783 options.offer_to_receive_video = 0;
1784 callee()->SetOfferAnswerOptions(options);
1785 // Do offer/answer and wait for stable signaling state.
1786 caller()->CreateAndSetAndSignalOffer();
1787 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1788 // Sanity check that the callee's description has rejected m= sections.
1789 ASSERT_NE(nullptr, callee()->pc()->local_description());
1790 const ContentInfo* callee_audio_content =
1791 GetFirstAudioContent(callee()->pc()->local_description()->description());
1792 ASSERT_NE(nullptr, callee_audio_content);
1793 EXPECT_TRUE(callee_audio_content->rejected);
1794 const ContentInfo* callee_video_content =
1795 GetFirstVideoContent(callee()->pc()->local_description()->description());
1796 ASSERT_NE(nullptr, callee_video_content);
1797 EXPECT_TRUE(callee_video_content->rejected);
1798}
1799
1800// This test sets up an audio and video call between two parties. After the
1801// call runs for a while, the caller sends an updated offer with video being
1802// rejected. Once the re-negotiation is done, the video flow should stop and
1803// the audio flow should continue.
1804TEST_F(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
1805 ASSERT_TRUE(CreatePeerConnectionWrappers());
1806 ConnectFakeSignaling();
1807 caller()->AddAudioVideoMediaStream();
1808 callee()->AddAudioVideoMediaStream();
1809 caller()->CreateAndSetAndSignalOffer();
1810 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1811 ExpectNewFramesReceivedWithWait(
1812 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1813 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1814 kMaxWaitForFramesMs);
1815
1816 // Renegotiate, rejecting the video m= section.
1817 // TODO(deadbeef): When an RtpTransceiver API is available, use that to
1818 // reject the video m= section.
1819 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
1820 for (cricket::ContentInfo& content : description->contents()) {
1821 if (cricket::IsVideoContent(&content)) {
1822 content.rejected = true;
1823 }
1824 }
1825 });
1826 caller()->CreateAndSetAndSignalOffer();
1827 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
1828
1829 // Sanity check that the caller's description has a rejected video section.
1830 ASSERT_NE(nullptr, caller()->pc()->local_description());
1831 const ContentInfo* caller_video_content =
1832 GetFirstVideoContent(caller()->pc()->local_description()->description());
1833 ASSERT_NE(nullptr, caller_video_content);
1834 EXPECT_TRUE(caller_video_content->rejected);
1835
1836 int caller_video_received = caller()->total_video_frames_received();
1837 int callee_video_received = callee()->total_video_frames_received();
1838
1839 // Wait for some additional audio frames to be received.
1840 ExpectNewFramesReceivedWithWait(kDefaultExpectedAudioFrameCount, 0,
1841 kDefaultExpectedAudioFrameCount, 0,
1842 kMaxWaitForFramesMs);
1843
1844 // During this time, we shouldn't have received any additional video frames
1845 // for the rejected video tracks.
1846 EXPECT_EQ(caller_video_received, caller()->total_video_frames_received());
1847 EXPECT_EQ(callee_video_received, callee()->total_video_frames_received());
1848}
1849
1850// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
1851// is needed to support legacy endpoints.
1852// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
1853// add a test for an end-to-end test without MID signaling either (basically,
1854// the minimum acceptable SDP).
1855TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
1856 ASSERT_TRUE(CreatePeerConnectionWrappers());
1857 ConnectFakeSignaling();
1858 // Add audio and video, testing that packets can be demuxed on payload type.
1859 caller()->AddAudioVideoMediaStream();
1860 callee()->AddAudioVideoMediaStream();
deadbeefd8ad7882017-04-18 16:01:17 -07001861 // Remove SSRCs and MSIDs from the received offer SDP.
1862 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07001863 caller()->CreateAndSetAndSignalOffer();
1864 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1865 ExpectNewFramesReceivedWithWait(
1866 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1867 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1868 kMaxWaitForFramesMs);
1869}
1870
1871// Test that if two video tracks are sent (from caller to callee, in this test),
1872// they're transmitted correctly end-to-end.
1873TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
1874 ASSERT_TRUE(CreatePeerConnectionWrappers());
1875 ConnectFakeSignaling();
1876 // Add one audio/video stream, and one video-only stream.
1877 caller()->AddAudioVideoMediaStream();
deadbeefb1a15d72017-09-07 14:12:05 -07001878 caller()->AddVideoOnlyMediaStream();
deadbeef1dcb1642017-03-29 21:08:16 -07001879 caller()->CreateAndSetAndSignalOffer();
1880 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1881 ASSERT_EQ(2u, callee()->number_of_remote_streams());
1882 int expected_callee_received_frames = kDefaultExpectedVideoFrameCount;
1883 ExpectNewFramesReceivedWithWait(0, 0, 0, expected_callee_received_frames,
1884 kMaxWaitForFramesMs);
1885}
1886
1887static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
1888 bool first = true;
1889 for (cricket::ContentInfo& content : desc->contents()) {
1890 if (first) {
1891 first = false;
1892 continue;
1893 }
1894 content.bundle_only = true;
1895 }
1896 first = true;
1897 for (cricket::TransportInfo& transport : desc->transport_infos()) {
1898 if (first) {
1899 first = false;
1900 continue;
1901 }
1902 transport.description.ice_ufrag.clear();
1903 transport.description.ice_pwd.clear();
1904 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
1905 transport.description.identity_fingerprint.reset(nullptr);
1906 }
1907}
1908
1909// Test that if applying a true "max bundle" offer, which uses ports of 0,
1910// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
1911// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
1912// successfully and media flows.
1913// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
1914// TODO(deadbeef): Won't need this test once we start generating actual
1915// standards-compliant SDP.
1916TEST_F(PeerConnectionIntegrationTest,
1917 EndToEndCallWithSpecCompliantMaxBundleOffer) {
1918 ASSERT_TRUE(CreatePeerConnectionWrappers());
1919 ConnectFakeSignaling();
1920 caller()->AddAudioVideoMediaStream();
1921 callee()->AddAudioVideoMediaStream();
1922 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
1923 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
1924 // but the first m= section.
1925 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
1926 caller()->CreateAndSetAndSignalOffer();
1927 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1928 ExpectNewFramesReceivedWithWait(
1929 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1930 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1931 kMaxWaitForFramesMs);
1932}
1933
1934// Test that we can receive the audio output level from a remote audio track.
1935// TODO(deadbeef): Use a fake audio source and verify that the output level is
1936// exactly what the source on the other side was configured with.
deadbeefd8ad7882017-04-18 16:01:17 -07001937TEST_F(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07001938 ASSERT_TRUE(CreatePeerConnectionWrappers());
1939 ConnectFakeSignaling();
1940 // Just add an audio track.
1941 caller()->AddMediaStreamFromTracks(caller()->CreateLocalAudioTrack(),
1942 nullptr);
1943 caller()->CreateAndSetAndSignalOffer();
1944 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1945
1946 // Get the audio output level stats. Note that the level is not available
1947 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07001948 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07001949 kMaxWaitForFramesMs);
1950}
1951
1952// Test that an audio input level is reported.
1953// TODO(deadbeef): Use a fake audio source and verify that the input level is
1954// exactly what the source was configured with.
deadbeefd8ad7882017-04-18 16:01:17 -07001955TEST_F(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07001956 ASSERT_TRUE(CreatePeerConnectionWrappers());
1957 ConnectFakeSignaling();
1958 // Just add an audio track.
1959 caller()->AddMediaStreamFromTracks(caller()->CreateLocalAudioTrack(),
1960 nullptr);
1961 caller()->CreateAndSetAndSignalOffer();
1962 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1963
1964 // Get the audio input level stats. The level should be available very
1965 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07001966 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07001967 kMaxWaitForStatsMs);
1968}
1969
1970// Test that we can get incoming byte counts from both audio and video tracks.
deadbeefd8ad7882017-04-18 16:01:17 -07001971TEST_F(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07001972 ASSERT_TRUE(CreatePeerConnectionWrappers());
1973 ConnectFakeSignaling();
1974 caller()->AddAudioVideoMediaStream();
1975 // Do offer/answer, wait for the callee to receive some frames.
1976 caller()->CreateAndSetAndSignalOffer();
1977 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1978 int expected_caller_received_frames = 0;
1979 ExpectNewFramesReceivedWithWait(
1980 expected_caller_received_frames, expected_caller_received_frames,
1981 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1982 kMaxWaitForFramesMs);
1983
1984 // Get a handle to the remote tracks created, so they can be used as GetStats
1985 // filters.
1986 StreamCollectionInterface* remote_streams = callee()->remote_streams();
1987 ASSERT_EQ(1u, remote_streams->count());
1988 ASSERT_EQ(1u, remote_streams->at(0)->GetAudioTracks().size());
1989 ASSERT_EQ(1u, remote_streams->at(0)->GetVideoTracks().size());
1990 MediaStreamTrackInterface* remote_audio_track =
1991 remote_streams->at(0)->GetAudioTracks()[0];
1992 MediaStreamTrackInterface* remote_video_track =
1993 remote_streams->at(0)->GetVideoTracks()[0];
1994
1995 // We received frames, so we definitely should have nonzero "received bytes"
1996 // stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07001997 EXPECT_GT(callee()->OldGetStatsForTrack(remote_audio_track)->BytesReceived(),
1998 0);
1999 EXPECT_GT(callee()->OldGetStatsForTrack(remote_video_track)->BytesReceived(),
2000 0);
deadbeef1dcb1642017-03-29 21:08:16 -07002001}
2002
2003// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeefd8ad7882017-04-18 16:01:17 -07002004TEST_F(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002005 ASSERT_TRUE(CreatePeerConnectionWrappers());
2006 ConnectFakeSignaling();
2007 auto audio_track = caller()->CreateLocalAudioTrack();
2008 auto video_track = caller()->CreateLocalVideoTrack();
2009 caller()->AddMediaStreamFromTracks(audio_track, video_track);
2010 // Do offer/answer, wait for the callee to receive some frames.
2011 caller()->CreateAndSetAndSignalOffer();
2012 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2013 int expected_caller_received_frames = 0;
2014 ExpectNewFramesReceivedWithWait(
2015 expected_caller_received_frames, expected_caller_received_frames,
2016 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2017 kMaxWaitForFramesMs);
2018
2019 // The callee received frames, so we definitely should have nonzero "sent
2020 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002021 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2022 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2023}
2024
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002025// Test that we can get capture start ntp time.
2026TEST_F(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
2027 ASSERT_TRUE(CreatePeerConnectionWrappers());
2028 ConnectFakeSignaling();
2029 caller()->AddAudioOnlyMediaStream();
2030
2031 auto audio_track = callee()->CreateLocalAudioTrack();
2032 callee()->AddMediaStreamFromTracks(audio_track, nullptr);
2033
2034 // Do offer/answer, wait for the callee to receive some frames.
2035 caller()->CreateAndSetAndSignalOffer();
2036 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2037
2038 // Get the remote audio track created on the receiver, so they can be used as
2039 // GetStats filters.
2040 StreamCollectionInterface* remote_streams = callee()->remote_streams();
2041 ASSERT_EQ(1u, remote_streams->count());
2042 ASSERT_EQ(1u, remote_streams->at(0)->GetAudioTracks().size());
2043 MediaStreamTrackInterface* remote_audio_track =
2044 remote_streams->at(0)->GetAudioTracks()[0];
2045
2046 // Get the audio output level stats. Note that the level is not available
2047 // until an RTCP packet has been received.
2048 EXPECT_TRUE_WAIT(callee()->OldGetStatsForTrack(remote_audio_track)->
2049 CaptureStartNtpTime() > 0, 2 * kMaxWaitForFramesMs);
2050}
2051
deadbeefd8ad7882017-04-18 16:01:17 -07002052// Test that we can get stats (using the new stats implemnetation) for
2053// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2054// SDP.
2055TEST_F(PeerConnectionIntegrationTest,
2056 GetStatsForUnsignaledStreamWithNewStatsApi) {
2057 ASSERT_TRUE(CreatePeerConnectionWrappers());
2058 ConnectFakeSignaling();
2059 caller()->AddAudioOnlyMediaStream();
2060 // Remove SSRCs and MSIDs from the received offer SDP.
2061 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2062 caller()->CreateAndSetAndSignalOffer();
2063 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2064 // Wait for one audio frame to be received by the callee.
2065 ExpectNewFramesReceivedWithWait(0, 0, 1, 0, kMaxWaitForFramesMs);
2066
2067 // We received a frame, so we should have nonzero "bytes received" stats for
2068 // the unsignaled stream, if stats are working for it.
2069 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2070 callee()->NewGetStats();
2071 ASSERT_NE(nullptr, report);
2072 auto inbound_stream_stats =
2073 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2074 ASSERT_EQ(1U, inbound_stream_stats.size());
2075 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2076 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002077 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2078}
2079
2080// Test that we can successfully get the media related stats (audio level
2081// etc.) for the unsignaled stream.
2082TEST_F(PeerConnectionIntegrationTest,
2083 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2084 ASSERT_TRUE(CreatePeerConnectionWrappers());
2085 ConnectFakeSignaling();
2086 caller()->AddAudioVideoMediaStream();
2087 // Remove SSRCs and MSIDs from the received offer SDP.
2088 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2089 caller()->CreateAndSetAndSignalOffer();
2090 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2091 // Wait for one audio frame to be received by the callee.
2092 ExpectNewFramesReceivedWithWait(0, 0, 1, 1, kMaxWaitForFramesMs);
2093
2094 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2095 callee()->NewGetStats();
2096 ASSERT_NE(nullptr, report);
2097
2098 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2099 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2100 ASSERT_GE(audio_index, 0);
2101 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002102}
2103
deadbeef4e2deab2017-09-20 13:56:21 -07002104// Helper for test below.
2105void ModifySsrcs(cricket::SessionDescription* desc) {
2106 for (ContentInfo& content : desc->contents()) {
2107 MediaContentDescription* media_desc =
2108 static_cast<MediaContentDescription*>(content.description);
2109 for (cricket::StreamParams& stream : media_desc->mutable_streams()) {
2110 for (uint32_t& ssrc : stream.ssrcs) {
2111 ssrc = rtc::CreateRandomId();
2112 }
2113 }
2114 }
2115}
2116
2117// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2118// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2119// This should result in two "RTCInboundRTPStreamStats", but only one
2120// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2121// being reset to 0 once the SSRC change occurs.
2122//
2123// Regression test for this bug:
2124// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2125//
2126// The bug causes the track stats to only represent one of the two streams:
2127// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2128// that the track stat counters would reset to 0 when the new stream is
2129// received, and a 50% chance that they'll stop updating (while
2130// "concealed_samples" continues increasing, due to silence being generated for
2131// the inactive stream).
2132TEST_F(PeerConnectionIntegrationTest,
2133 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
2134 ASSERT_TRUE(CreatePeerConnectionWrappers());
2135 ConnectFakeSignaling();
2136 caller()->AddAudioOnlyMediaStream();
2137 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2138 // that doesn't signal SSRCs (from the callee's perspective).
2139 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2140 caller()->CreateAndSetAndSignalOffer();
2141 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2142 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
2143 ExpectNewFramesReceivedWithWait(0, 0, 25, 0, kMaxWaitForFramesMs);
2144
2145 // Some audio frames were received, so we should have nonzero "samples
2146 // received" for the track.
2147 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2148 callee()->NewGetStats();
2149 ASSERT_NE(nullptr, report);
2150 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2151 ASSERT_EQ(1U, track_stats.size());
2152 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2153 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2154 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2155
2156 // Create a new offer and munge it to cause the caller to use a new SSRC.
2157 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2158 caller()->CreateAndSetAndSignalOffer();
2159 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2160 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2161 // SSRC.
2162 ExpectNewFramesReceivedWithWait(0, 0, 25, 0, kMaxWaitForFramesMs);
2163
2164 report = callee()->NewGetStats();
2165 ASSERT_NE(nullptr, report);
2166 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2167 ASSERT_EQ(1U, track_stats.size());
2168 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2169 // The "total samples received" stat should only be greater than it was
2170 // before.
2171 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2172 // Right now, the new SSRC will cause the counters to reset to 0.
2173 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2174
2175 // Additionally, the percentage of concealed samples (samples generated to
2176 // conceal packet loss) should be less than 25%%. If it's greater, that's a
2177 // good sign that we're seeing stats from the old stream that's no longer
2178 // receiving packets, and is generating concealed samples of silence.
2179 constexpr double kAcceptableConcealedSamplesPercentage = 0.25;
2180 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2181 EXPECT_LT(*track_stats[0]->concealed_samples,
2182 *track_stats[0]->total_samples_received *
2183 kAcceptableConcealedSamplesPercentage);
2184
2185 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2186 // sanity check that the SSRC really changed.
2187 // TODO(deadbeef): This isn't working right now, because we're not returning
2188 // *any* stats for the inactive stream. Uncomment when the bug is completely
2189 // fixed.
2190 // auto inbound_stream_stats =
2191 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2192 // ASSERT_EQ(2U, inbound_stream_stats.size());
2193}
2194
deadbeef1dcb1642017-03-29 21:08:16 -07002195// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
2196TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
2197 PeerConnectionFactory::Options dtls_10_options;
2198 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2199 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2200 dtls_10_options));
2201 ConnectFakeSignaling();
2202 // Do normal offer/answer and wait for some frames to be received in each
2203 // direction.
2204 caller()->AddAudioVideoMediaStream();
2205 callee()->AddAudioVideoMediaStream();
2206 caller()->CreateAndSetAndSignalOffer();
2207 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2208 ExpectNewFramesReceivedWithWait(
2209 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2210 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2211 kMaxWaitForFramesMs);
2212}
2213
2214// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
2215TEST_F(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
2216 PeerConnectionFactory::Options dtls_10_options;
2217 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2218 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2219 dtls_10_options));
2220 ConnectFakeSignaling();
2221 // Register UMA observer before signaling begins.
2222 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2223 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2224 caller()->pc()->RegisterUMAObserver(caller_observer);
2225 caller()->AddAudioVideoMediaStream();
2226 callee()->AddAudioVideoMediaStream();
2227 caller()->CreateAndSetAndSignalOffer();
2228 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2229 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002230 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002231 kDefaultTimeout);
2232 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002233 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002234 EXPECT_EQ(1,
2235 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2236 kDefaultSrtpCryptoSuite));
2237}
2238
2239// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
2240TEST_F(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
2241 PeerConnectionFactory::Options dtls_12_options;
2242 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2243 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2244 dtls_12_options));
2245 ConnectFakeSignaling();
2246 // Register UMA observer before signaling begins.
2247 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2248 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2249 caller()->pc()->RegisterUMAObserver(caller_observer);
2250 caller()->AddAudioVideoMediaStream();
2251 callee()->AddAudioVideoMediaStream();
2252 caller()->CreateAndSetAndSignalOffer();
2253 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2254 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002255 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002256 kDefaultTimeout);
2257 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002258 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002259 EXPECT_EQ(1,
2260 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2261 kDefaultSrtpCryptoSuite));
2262}
2263
2264// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2265// callee only supports 1.0.
2266TEST_F(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
2267 PeerConnectionFactory::Options caller_options;
2268 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2269 PeerConnectionFactory::Options callee_options;
2270 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2271 ASSERT_TRUE(
2272 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2273 ConnectFakeSignaling();
2274 // Do normal offer/answer and wait for some frames to be received in each
2275 // direction.
2276 caller()->AddAudioVideoMediaStream();
2277 callee()->AddAudioVideoMediaStream();
2278 caller()->CreateAndSetAndSignalOffer();
2279 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2280 ExpectNewFramesReceivedWithWait(
2281 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2282 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2283 kMaxWaitForFramesMs);
2284}
2285
2286// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2287// callee supports 1.2.
2288TEST_F(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
2289 PeerConnectionFactory::Options caller_options;
2290 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2291 PeerConnectionFactory::Options callee_options;
2292 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2293 ASSERT_TRUE(
2294 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2295 ConnectFakeSignaling();
2296 // Do normal offer/answer and wait for some frames to be received in each
2297 // direction.
2298 caller()->AddAudioVideoMediaStream();
2299 callee()->AddAudioVideoMediaStream();
2300 caller()->CreateAndSetAndSignalOffer();
2301 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2302 ExpectNewFramesReceivedWithWait(
2303 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2304 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2305 kMaxWaitForFramesMs);
2306}
2307
2308// Test that a non-GCM cipher is used if both sides only support non-GCM.
2309TEST_F(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
2310 bool local_gcm_enabled = false;
2311 bool remote_gcm_enabled = false;
2312 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2313 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2314 expected_cipher_suite);
2315}
2316
2317// Test that a GCM cipher is used if both ends support it.
2318TEST_F(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
2319 bool local_gcm_enabled = true;
2320 bool remote_gcm_enabled = true;
2321 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2322 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2323 expected_cipher_suite);
2324}
2325
2326// Test that GCM isn't used if only the offerer supports it.
2327TEST_F(PeerConnectionIntegrationTest,
2328 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2329 bool local_gcm_enabled = true;
2330 bool remote_gcm_enabled = false;
2331 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2332 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2333 expected_cipher_suite);
2334}
2335
2336// Test that GCM isn't used if only the answerer supports it.
2337TEST_F(PeerConnectionIntegrationTest,
2338 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2339 bool local_gcm_enabled = false;
2340 bool remote_gcm_enabled = true;
2341 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2342 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2343 expected_cipher_suite);
2344}
2345
deadbeef7914b8c2017-04-21 03:23:33 -07002346// Verify that media can be transmitted end-to-end when GCM crypto suites are
2347// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2348// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2349// works with it.
2350TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
2351 PeerConnectionFactory::Options gcm_options;
2352 gcm_options.crypto_options.enable_gcm_crypto_suites = true;
2353 ASSERT_TRUE(
2354 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2355 ConnectFakeSignaling();
2356 // Do normal offer/answer and wait for some frames to be received in each
2357 // direction.
2358 caller()->AddAudioVideoMediaStream();
2359 callee()->AddAudioVideoMediaStream();
2360 caller()->CreateAndSetAndSignalOffer();
2361 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2362 ExpectNewFramesReceivedWithWait(
2363 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2364 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2365 kMaxWaitForFramesMs);
2366}
2367
deadbeef1dcb1642017-03-29 21:08:16 -07002368// This test sets up a call between two parties with audio, video and an RTP
2369// data channel.
2370TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
2371 FakeConstraints setup_constraints;
2372 setup_constraints.SetAllowRtpDataChannels();
2373 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2374 &setup_constraints));
2375 ConnectFakeSignaling();
2376 // Expect that data channel created on caller side will show up for callee as
2377 // well.
2378 caller()->CreateDataChannel();
2379 caller()->AddAudioVideoMediaStream();
2380 callee()->AddAudioVideoMediaStream();
2381 caller()->CreateAndSetAndSignalOffer();
2382 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2383 // Ensure the existence of the RTP data channel didn't impede audio/video.
2384 ExpectNewFramesReceivedWithWait(
2385 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2386 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2387 kMaxWaitForFramesMs);
2388 ASSERT_NE(nullptr, caller()->data_channel());
2389 ASSERT_NE(nullptr, callee()->data_channel());
2390 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2391 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2392
2393 // Ensure data can be sent in both directions.
2394 std::string data = "hello world";
2395 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2396 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2397 kDefaultTimeout);
2398 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2399 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2400 kDefaultTimeout);
2401}
2402
2403// Ensure that an RTP data channel is signaled as closed for the caller when
2404// the callee rejects it in a subsequent offer.
2405TEST_F(PeerConnectionIntegrationTest,
2406 RtpDataChannelSignaledClosedInCalleeOffer) {
2407 // Same procedure as above test.
2408 FakeConstraints setup_constraints;
2409 setup_constraints.SetAllowRtpDataChannels();
2410 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2411 &setup_constraints));
2412 ConnectFakeSignaling();
2413 caller()->CreateDataChannel();
2414 caller()->AddAudioVideoMediaStream();
2415 callee()->AddAudioVideoMediaStream();
2416 caller()->CreateAndSetAndSignalOffer();
2417 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2418 ASSERT_NE(nullptr, caller()->data_channel());
2419 ASSERT_NE(nullptr, callee()->data_channel());
2420 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2421 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2422
2423 // Close the data channel on the callee, and do an updated offer/answer.
2424 callee()->data_channel()->Close();
2425 callee()->CreateAndSetAndSignalOffer();
2426 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2427 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2428 EXPECT_FALSE(callee()->data_observer()->IsOpen());
2429}
2430
2431// Tests that data is buffered in an RTP data channel until an observer is
2432// registered for it.
2433//
2434// NOTE: RTP data channels can receive data before the underlying
2435// transport has detected that a channel is writable and thus data can be
2436// received before the data channel state changes to open. That is hard to test
2437// but the same buffering is expected to be used in that case.
2438TEST_F(PeerConnectionIntegrationTest,
2439 DataBufferedUntilRtpDataChannelObserverRegistered) {
2440 // Use fake clock and simulated network delay so that we predictably can wait
2441 // until an SCTP message has been delivered without "sleep()"ing.
2442 rtc::ScopedFakeClock fake_clock;
2443 // Some things use a time of "0" as a special value, so we need to start out
2444 // the fake clock at a nonzero time.
2445 // TODO(deadbeef): Fix this.
2446 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
2447 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
2448 virtual_socket_server()->UpdateDelayDistribution();
2449
2450 FakeConstraints constraints;
2451 constraints.SetAllowRtpDataChannels();
2452 ASSERT_TRUE(
2453 CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints));
2454 ConnectFakeSignaling();
2455 caller()->CreateDataChannel();
2456 caller()->CreateAndSetAndSignalOffer();
2457 ASSERT_TRUE(caller()->data_channel() != nullptr);
2458 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
2459 kDefaultTimeout, fake_clock);
2460 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
2461 kDefaultTimeout, fake_clock);
2462 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
2463 callee()->data_channel()->state(), kDefaultTimeout,
2464 fake_clock);
2465
2466 // Unregister the observer which is normally automatically registered.
2467 callee()->data_channel()->UnregisterObserver();
2468 // Send data and advance fake clock until it should have been received.
2469 std::string data = "hello world";
2470 caller()->data_channel()->Send(DataBuffer(data));
2471 SIMULATED_WAIT(false, 50, fake_clock);
2472
2473 // Attach data channel and expect data to be received immediately. Note that
2474 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
2475 // further, but data can be received even if the callback is asynchronous.
2476 MockDataChannelObserver new_observer(callee()->data_channel());
2477 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
2478 fake_clock);
2479}
2480
2481// This test sets up a call between two parties with audio, video and but only
2482// the caller client supports RTP data channels.
2483TEST_F(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
2484 FakeConstraints setup_constraints_1;
2485 setup_constraints_1.SetAllowRtpDataChannels();
2486 // Must disable DTLS to make negotiation succeed.
2487 setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2488 false);
2489 FakeConstraints setup_constraints_2;
2490 setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2491 false);
2492 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(
2493 &setup_constraints_1, &setup_constraints_2));
2494 ConnectFakeSignaling();
2495 caller()->CreateDataChannel();
2496 caller()->AddAudioVideoMediaStream();
2497 callee()->AddAudioVideoMediaStream();
2498 caller()->CreateAndSetAndSignalOffer();
2499 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2500 // The caller should still have a data channel, but it should be closed, and
2501 // one should ever have been created for the callee.
2502 EXPECT_TRUE(caller()->data_channel() != nullptr);
2503 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2504 EXPECT_EQ(nullptr, callee()->data_channel());
2505}
2506
2507// This test sets up a call between two parties with audio, and video. When
2508// audio and video is setup and flowing, an RTP data channel is negotiated.
2509TEST_F(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
2510 FakeConstraints setup_constraints;
2511 setup_constraints.SetAllowRtpDataChannels();
2512 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2513 &setup_constraints));
2514 ConnectFakeSignaling();
2515 // Do initial offer/answer with audio/video.
2516 caller()->AddAudioVideoMediaStream();
2517 callee()->AddAudioVideoMediaStream();
2518 caller()->CreateAndSetAndSignalOffer();
2519 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2520 // Create data channel and do new offer and answer.
2521 caller()->CreateDataChannel();
2522 caller()->CreateAndSetAndSignalOffer();
2523 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2524 ASSERT_NE(nullptr, caller()->data_channel());
2525 ASSERT_NE(nullptr, callee()->data_channel());
2526 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2527 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2528 // Ensure data can be sent in both directions.
2529 std::string data = "hello world";
2530 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2531 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2532 kDefaultTimeout);
2533 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2534 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2535 kDefaultTimeout);
2536}
2537
2538#ifdef HAVE_SCTP
2539
2540// This test sets up a call between two parties with audio, video and an SCTP
2541// data channel.
2542TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
2543 ASSERT_TRUE(CreatePeerConnectionWrappers());
2544 ConnectFakeSignaling();
2545 // Expect that data channel created on caller side will show up for callee as
2546 // well.
2547 caller()->CreateDataChannel();
2548 caller()->AddAudioVideoMediaStream();
2549 callee()->AddAudioVideoMediaStream();
2550 caller()->CreateAndSetAndSignalOffer();
2551 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2552 // Ensure the existence of the SCTP data channel didn't impede audio/video.
2553 ExpectNewFramesReceivedWithWait(
2554 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2555 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2556 kMaxWaitForFramesMs);
2557 // Caller data channel should already exist (it created one). Callee data
2558 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2559 ASSERT_NE(nullptr, caller()->data_channel());
2560 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2561 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2562 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2563
2564 // Ensure data can be sent in both directions.
2565 std::string data = "hello world";
2566 caller()->data_channel()->Send(DataBuffer(data));
2567 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2568 kDefaultTimeout);
2569 callee()->data_channel()->Send(DataBuffer(data));
2570 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2571 kDefaultTimeout);
2572}
2573
2574// Ensure that when the callee closes an SCTP data channel, the closing
2575// procedure results in the data channel being closed for the caller as well.
2576TEST_F(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
2577 // Same procedure as above test.
2578 ASSERT_TRUE(CreatePeerConnectionWrappers());
2579 ConnectFakeSignaling();
2580 caller()->CreateDataChannel();
2581 caller()->AddAudioVideoMediaStream();
2582 callee()->AddAudioVideoMediaStream();
2583 caller()->CreateAndSetAndSignalOffer();
2584 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2585 ASSERT_NE(nullptr, caller()->data_channel());
2586 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2587 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2588 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2589
2590 // Close the data channel on the callee side, and wait for it to reach the
2591 // "closed" state on both sides.
2592 callee()->data_channel()->Close();
2593 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
2594 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
2595}
2596
2597// Test usrsctp's ability to process unordered data stream, where data actually
2598// arrives out of order using simulated delays. Previously there have been some
2599// bugs in this area.
2600TEST_F(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
2601 // Introduce random network delays.
2602 // Otherwise it's not a true "unordered" test.
2603 virtual_socket_server()->set_delay_mean(20);
2604 virtual_socket_server()->set_delay_stddev(5);
2605 virtual_socket_server()->UpdateDelayDistribution();
2606 // Normal procedure, but with unordered data channel config.
2607 ASSERT_TRUE(CreatePeerConnectionWrappers());
2608 ConnectFakeSignaling();
2609 webrtc::DataChannelInit init;
2610 init.ordered = false;
2611 caller()->CreateDataChannel(&init);
2612 caller()->CreateAndSetAndSignalOffer();
2613 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2614 ASSERT_NE(nullptr, caller()->data_channel());
2615 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2616 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2617 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2618
2619 static constexpr int kNumMessages = 100;
2620 // Deliberately chosen to be larger than the MTU so messages get fragmented.
2621 static constexpr size_t kMaxMessageSize = 4096;
2622 // Create and send random messages.
2623 std::vector<std::string> sent_messages;
2624 for (int i = 0; i < kNumMessages; ++i) {
2625 size_t length =
2626 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
2627 std::string message;
2628 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
2629 caller()->data_channel()->Send(DataBuffer(message));
2630 callee()->data_channel()->Send(DataBuffer(message));
2631 sent_messages.push_back(message);
2632 }
2633
2634 // Wait for all messages to be received.
2635 EXPECT_EQ_WAIT(kNumMessages,
2636 caller()->data_observer()->received_message_count(),
2637 kDefaultTimeout);
2638 EXPECT_EQ_WAIT(kNumMessages,
2639 callee()->data_observer()->received_message_count(),
2640 kDefaultTimeout);
2641
2642 // Sort and compare to make sure none of the messages were corrupted.
2643 std::vector<std::string> caller_received_messages =
2644 caller()->data_observer()->messages();
2645 std::vector<std::string> callee_received_messages =
2646 callee()->data_observer()->messages();
2647 std::sort(sent_messages.begin(), sent_messages.end());
2648 std::sort(caller_received_messages.begin(), caller_received_messages.end());
2649 std::sort(callee_received_messages.begin(), callee_received_messages.end());
2650 EXPECT_EQ(sent_messages, caller_received_messages);
2651 EXPECT_EQ(sent_messages, callee_received_messages);
2652}
2653
2654// This test sets up a call between two parties with audio, and video. When
2655// audio and video are setup and flowing, an SCTP data channel is negotiated.
2656TEST_F(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
2657 ASSERT_TRUE(CreatePeerConnectionWrappers());
2658 ConnectFakeSignaling();
2659 // Do initial offer/answer with audio/video.
2660 caller()->AddAudioVideoMediaStream();
2661 callee()->AddAudioVideoMediaStream();
2662 caller()->CreateAndSetAndSignalOffer();
2663 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2664 // Create data channel and do new offer and answer.
2665 caller()->CreateDataChannel();
2666 caller()->CreateAndSetAndSignalOffer();
2667 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2668 // Caller data channel should already exist (it created one). Callee data
2669 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2670 ASSERT_NE(nullptr, caller()->data_channel());
2671 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2672 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2673 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2674 // Ensure data can be sent in both directions.
2675 std::string data = "hello world";
2676 caller()->data_channel()->Send(DataBuffer(data));
2677 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2678 kDefaultTimeout);
2679 callee()->data_channel()->Send(DataBuffer(data));
2680 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2681 kDefaultTimeout);
2682}
2683
deadbeef7914b8c2017-04-21 03:23:33 -07002684// Set up a connection initially just using SCTP data channels, later upgrading
2685// to audio/video, ensuring frames are received end-to-end. Effectively the
2686// inverse of the test above.
2687// This was broken in M57; see https://crbug.com/711243
2688TEST_F(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
2689 ASSERT_TRUE(CreatePeerConnectionWrappers());
2690 ConnectFakeSignaling();
2691 // Do initial offer/answer with just data channel.
2692 caller()->CreateDataChannel();
2693 caller()->CreateAndSetAndSignalOffer();
2694 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2695 // Wait until data can be sent over the data channel.
2696 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2697 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2698 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2699
2700 // Do subsequent offer/answer with two-way audio and video. Audio and video
2701 // should end up bundled on the DTLS/ICE transport already used for data.
2702 caller()->AddAudioVideoMediaStream();
2703 callee()->AddAudioVideoMediaStream();
2704 caller()->CreateAndSetAndSignalOffer();
2705 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2706 ExpectNewFramesReceivedWithWait(
2707 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2708 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2709 kMaxWaitForFramesMs);
2710}
2711
deadbeef8b7e9ad2017-05-25 09:38:55 -07002712static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
2713 const ContentInfo* dc_offer = GetFirstDataContent(desc);
2714 ASSERT_NE(nullptr, dc_offer);
2715 cricket::DataContentDescription* dcd_offer =
2716 static_cast<cricket::DataContentDescription*>(dc_offer->description);
2717 dcd_offer->set_use_sctpmap(false);
2718 dcd_offer->set_protocol("UDP/DTLS/SCTP");
2719}
2720
2721// Test that the data channel works when a spec-compliant SCTP m= section is
2722// offered (using "a=sctp-port" instead of "a=sctpmap", and using
2723// "UDP/DTLS/SCTP" as the protocol).
2724TEST_F(PeerConnectionIntegrationTest,
2725 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
2726 ASSERT_TRUE(CreatePeerConnectionWrappers());
2727 ConnectFakeSignaling();
2728 caller()->CreateDataChannel();
2729 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
2730 caller()->CreateAndSetAndSignalOffer();
2731 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2732 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2733 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2734 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2735
2736 // Ensure data can be sent in both directions.
2737 std::string data = "hello world";
2738 caller()->data_channel()->Send(DataBuffer(data));
2739 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2740 kDefaultTimeout);
2741 callee()->data_channel()->Send(DataBuffer(data));
2742 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2743 kDefaultTimeout);
2744}
2745
deadbeef1dcb1642017-03-29 21:08:16 -07002746#endif // HAVE_SCTP
2747
2748// Test that the ICE connection and gathering states eventually reach
2749// "complete".
2750TEST_F(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
2751 ASSERT_TRUE(CreatePeerConnectionWrappers());
2752 ConnectFakeSignaling();
2753 // Do normal offer/answer.
2754 caller()->AddAudioVideoMediaStream();
2755 callee()->AddAudioVideoMediaStream();
2756 caller()->CreateAndSetAndSignalOffer();
2757 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2758 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
2759 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
2760 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
2761 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
2762 // After the best candidate pair is selected and all candidates are signaled,
2763 // the ICE connection state should reach "complete".
2764 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
2765 // answerer/"callee" by default) only reaches "connected". When this is
2766 // fixed, this test should be updated.
2767 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2768 caller()->ice_connection_state(), kDefaultTimeout);
2769 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2770 callee()->ice_connection_state(), kDefaultTimeout);
2771}
2772
Steve Antonede9ca52017-10-16 13:04:27 -07002773// Test that firewalling the ICE connection causes the clients to identify the
2774// disconnected state and then removing the firewall causes them to reconnect.
2775class PeerConnectionIntegrationIceStatesTest
2776 : public PeerConnectionIntegrationTest,
2777 public ::testing::WithParamInterface<std::tuple<std::string, uint32_t>> {
2778 protected:
2779 PeerConnectionIntegrationIceStatesTest() {
2780 port_allocator_flags_ = std::get<1>(GetParam());
2781 }
2782
2783 void StartStunServer(const SocketAddress& server_address) {
2784 stun_server_.reset(
2785 cricket::TestStunServer::Create(network_thread(), server_address));
2786 }
2787
2788 bool TestIPv6() {
2789 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
2790 }
2791
2792 void SetPortAllocatorFlags() {
2793 caller()->port_allocator()->set_flags(port_allocator_flags_);
2794 callee()->port_allocator()->set_flags(port_allocator_flags_);
2795 }
2796
2797 std::vector<SocketAddress> CallerAddresses() {
2798 std::vector<SocketAddress> addresses;
2799 addresses.push_back(SocketAddress("1.1.1.1", 0));
2800 if (TestIPv6()) {
2801 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
2802 }
2803 return addresses;
2804 }
2805
2806 std::vector<SocketAddress> CalleeAddresses() {
2807 std::vector<SocketAddress> addresses;
2808 addresses.push_back(SocketAddress("2.2.2.2", 0));
2809 if (TestIPv6()) {
2810 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
2811 }
2812 return addresses;
2813 }
2814
2815 void SetUpNetworkInterfaces() {
2816 // Remove the default interfaces added by the test infrastructure.
2817 caller()->network()->RemoveInterface(kDefaultLocalAddress);
2818 callee()->network()->RemoveInterface(kDefaultLocalAddress);
2819
2820 // Add network addresses for test.
2821 for (const auto& caller_address : CallerAddresses()) {
2822 caller()->network()->AddInterface(caller_address);
2823 }
2824 for (const auto& callee_address : CalleeAddresses()) {
2825 callee()->network()->AddInterface(callee_address);
2826 }
2827 }
2828
2829 private:
2830 uint32_t port_allocator_flags_;
2831 std::unique_ptr<cricket::TestStunServer> stun_server_;
2832};
2833
2834// Tests that the PeerConnection goes through all the ICE gathering/connection
2835// states over the duration of the call. This includes Disconnected and Failed
2836// states, induced by putting a firewall between the peers and waiting for them
2837// to time out.
2838TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
2839 rtc::ScopedFakeClock fake_clock;
2840 // Some things use a time of "0" as a special value, so we need to start out
2841 // the fake clock at a nonzero time.
2842 // TODO(deadbeef): Fix this.
2843 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
2844
2845 const SocketAddress kStunServerAddress =
2846 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
2847 StartStunServer(kStunServerAddress);
2848
2849 PeerConnectionInterface::RTCConfiguration config;
2850 PeerConnectionInterface::IceServer ice_stun_server;
2851 ice_stun_server.urls.push_back(
2852 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
2853 kStunServerAddress.PortAsString());
2854 config.servers.push_back(ice_stun_server);
2855
2856 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
2857 ConnectFakeSignaling();
2858 SetPortAllocatorFlags();
2859 SetUpNetworkInterfaces();
2860 caller()->AddAudioVideoMediaStream();
2861 callee()->AddAudioVideoMediaStream();
2862
2863 // Initial state before anything happens.
2864 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
2865 caller()->ice_gathering_state());
2866 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
2867 caller()->ice_connection_state());
2868
2869 // Start the call by creating the offer, setting it as the local description,
2870 // then sending it to the peer who will respond with an answer. This happens
2871 // asynchronously so that we can watch the states as it runs in the
2872 // background.
2873 caller()->CreateAndSetAndSignalOffer();
2874
2875 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
2876 caller()->ice_connection_state(), kDefaultTimeout,
2877 fake_clock);
2878
2879 // Verify that the observer was notified of the intermediate transitions.
2880 EXPECT_THAT(caller()->ice_connection_state_history(),
2881 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
2882 PeerConnectionInterface::kIceConnectionConnected,
2883 PeerConnectionInterface::kIceConnectionCompleted));
2884 EXPECT_THAT(caller()->ice_gathering_state_history(),
2885 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
2886 PeerConnectionInterface::kIceGatheringComplete));
2887
2888 // Block connections to/from the caller and wait for ICE to become
2889 // disconnected.
2890 for (const auto& caller_address : CallerAddresses()) {
2891 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
2892 }
2893 LOG(LS_INFO) << "Firewall rules applied";
2894 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
2895 caller()->ice_connection_state(), kDefaultTimeout,
2896 fake_clock);
2897
2898 // Let ICE re-establish by removing the firewall rules.
2899 firewall()->ClearRules();
2900 LOG(LS_INFO) << "Firewall rules cleared";
2901 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
2902 caller()->ice_connection_state(), kDefaultTimeout,
2903 fake_clock);
2904
2905 // According to RFC7675, if there is no response within 30 seconds then the
2906 // peer should consider the other side to have rejected the connection. This
2907 // is signalled by the state transitioning to "failed".
2908 constexpr int kConsentTimeout = 30000;
2909 for (const auto& caller_address : CallerAddresses()) {
2910 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
2911 }
2912 LOG(LS_INFO) << "Firewall rules applied again";
2913 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
2914 caller()->ice_connection_state(), kConsentTimeout,
2915 fake_clock);
2916}
2917
2918// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
2919// and that the statistics in the metric observers are updated correctly.
2920TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
2921 ASSERT_TRUE(CreatePeerConnectionWrappers());
2922 ConnectFakeSignaling();
2923 SetPortAllocatorFlags();
2924 SetUpNetworkInterfaces();
2925 caller()->AddAudioVideoMediaStream();
2926 callee()->AddAudioVideoMediaStream();
2927
2928 rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer(
2929 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>());
2930 caller()->pc()->RegisterUMAObserver(metrics_observer.get());
2931
2932 caller()->CreateAndSetAndSignalOffer();
2933
2934 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2935
2936 const int num_best_ipv4 = metrics_observer->GetEnumCounter(
2937 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv4);
2938 const int num_best_ipv6 = metrics_observer->GetEnumCounter(
2939 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv6);
2940 if (TestIPv6()) {
2941 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
2942 // connection.
2943 EXPECT_EQ(0u, num_best_ipv4);
2944 EXPECT_EQ(1u, num_best_ipv6);
2945 } else {
2946 EXPECT_EQ(1u, num_best_ipv4);
2947 EXPECT_EQ(0u, num_best_ipv6);
2948 }
2949
2950 EXPECT_EQ(0u, metrics_observer->GetEnumCounter(
2951 webrtc::kEnumCounterIceCandidatePairTypeUdp,
2952 webrtc::kIceCandidatePairHostHost));
2953 EXPECT_EQ(1u, metrics_observer->GetEnumCounter(
2954 webrtc::kEnumCounterIceCandidatePairTypeUdp,
2955 webrtc::kIceCandidatePairHostPublicHostPublic));
2956}
2957
2958constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
2959 cricket::PORTALLOCATOR_DISABLE_STUN |
2960 cricket::PORTALLOCATOR_DISABLE_RELAY;
2961constexpr uint32_t kFlagsIPv6NoStun =
2962 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
2963 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
2964constexpr uint32_t kFlagsIPv4Stun =
2965 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
2966
2967INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
2968 PeerConnectionIntegrationIceStatesTest,
2969 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
2970 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
2971 std::make_pair("IPv4 with STUN",
2972 kFlagsIPv4Stun)));
2973
deadbeef1dcb1642017-03-29 21:08:16 -07002974// This test sets up a call between two parties with audio and video.
2975// During the call, the caller restarts ICE and the test verifies that
2976// new ICE candidates are generated and audio and video still can flow, and the
2977// ICE state reaches completed again.
2978TEST_F(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
2979 ASSERT_TRUE(CreatePeerConnectionWrappers());
2980 ConnectFakeSignaling();
2981 // Do normal offer/answer and wait for ICE to complete.
2982 caller()->AddAudioVideoMediaStream();
2983 callee()->AddAudioVideoMediaStream();
2984 caller()->CreateAndSetAndSignalOffer();
2985 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2986 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2987 caller()->ice_connection_state(), kMaxWaitForFramesMs);
2988 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2989 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2990
2991 // To verify that the ICE restart actually occurs, get
2992 // ufrag/password/candidates before and after restart.
2993 // Create an SDP string of the first audio candidate for both clients.
2994 const webrtc::IceCandidateCollection* audio_candidates_caller =
2995 caller()->pc()->local_description()->candidates(0);
2996 const webrtc::IceCandidateCollection* audio_candidates_callee =
2997 callee()->pc()->local_description()->candidates(0);
2998 ASSERT_GT(audio_candidates_caller->count(), 0u);
2999 ASSERT_GT(audio_candidates_callee->count(), 0u);
3000 std::string caller_candidate_pre_restart;
3001 ASSERT_TRUE(
3002 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3003 std::string callee_candidate_pre_restart;
3004 ASSERT_TRUE(
3005 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3006 const cricket::SessionDescription* desc =
3007 caller()->pc()->local_description()->description();
3008 std::string caller_ufrag_pre_restart =
3009 desc->transport_infos()[0].description.ice_ufrag;
3010 desc = callee()->pc()->local_description()->description();
3011 std::string callee_ufrag_pre_restart =
3012 desc->transport_infos()[0].description.ice_ufrag;
3013
3014 // Have the caller initiate an ICE restart.
3015 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3016 caller()->CreateAndSetAndSignalOffer();
3017 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3018 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3019 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3020 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3021 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3022
3023 // Grab the ufrags/candidates again.
3024 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3025 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3026 ASSERT_GT(audio_candidates_caller->count(), 0u);
3027 ASSERT_GT(audio_candidates_callee->count(), 0u);
3028 std::string caller_candidate_post_restart;
3029 ASSERT_TRUE(
3030 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3031 std::string callee_candidate_post_restart;
3032 ASSERT_TRUE(
3033 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3034 desc = caller()->pc()->local_description()->description();
3035 std::string caller_ufrag_post_restart =
3036 desc->transport_infos()[0].description.ice_ufrag;
3037 desc = callee()->pc()->local_description()->description();
3038 std::string callee_ufrag_post_restart =
3039 desc->transport_infos()[0].description.ice_ufrag;
3040 // Sanity check that an ICE restart was actually negotiated in SDP.
3041 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3042 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3043 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3044 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3045
3046 // Ensure that additional frames are received after the ICE restart.
3047 ExpectNewFramesReceivedWithWait(
3048 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3049 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3050 kMaxWaitForFramesMs);
3051}
3052
3053// Verify that audio/video can be received end-to-end when ICE renomination is
3054// enabled.
3055TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
3056 PeerConnectionInterface::RTCConfiguration config;
3057 config.enable_ice_renomination = true;
3058 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3059 ConnectFakeSignaling();
3060 // Do normal offer/answer and wait for some frames to be received in each
3061 // direction.
3062 caller()->AddAudioVideoMediaStream();
3063 callee()->AddAudioVideoMediaStream();
3064 caller()->CreateAndSetAndSignalOffer();
3065 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3066 // Sanity check that ICE renomination was actually negotiated.
3067 const cricket::SessionDescription* desc =
3068 caller()->pc()->local_description()->description();
3069 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003070 ASSERT_NE(
3071 info.description.transport_options.end(),
3072 std::find(info.description.transport_options.begin(),
3073 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003074 }
3075 desc = callee()->pc()->local_description()->description();
3076 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003077 ASSERT_NE(
3078 info.description.transport_options.end(),
3079 std::find(info.description.transport_options.begin(),
3080 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003081 }
3082 ExpectNewFramesReceivedWithWait(
3083 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3084 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3085 kMaxWaitForFramesMs);
3086}
3087
Steve Anton6f25b092017-10-23 09:39:20 -07003088// With a max bundle policy and RTCP muxing, adding a new media description to
3089// the connection should not affect ICE at all because the new media will use
3090// the existing connection.
3091TEST_F(PeerConnectionIntegrationTest,
3092 AddMediaToConnectedBundleDoesNotRestartIce) {
3093 PeerConnectionInterface::RTCConfiguration config;
3094 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3095 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3096 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3097 config, PeerConnectionInterface::RTCConfiguration()));
3098 ConnectFakeSignaling();
3099
3100 caller()->AddAudioOnlyMediaStream();
3101 caller()->CreateAndSetAndSignalOffer();
3102 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3103
3104 caller()->clear_ice_connection_state_history();
3105
3106 caller()->AddVideoOnlyMediaStream();
3107 caller()->CreateAndSetAndSignalOffer();
3108 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3109
3110 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
3111}
3112
deadbeef1dcb1642017-03-29 21:08:16 -07003113// This test sets up a call between two parties with audio and video. It then
3114// renegotiates setting the video m-line to "port 0", then later renegotiates
3115// again, enabling video.
3116TEST_F(PeerConnectionIntegrationTest,
3117 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
3118 ASSERT_TRUE(CreatePeerConnectionWrappers());
3119 ConnectFakeSignaling();
3120
3121 // Do initial negotiation, only sending media from the caller. Will result in
3122 // video and audio recvonly "m=" sections.
3123 caller()->AddAudioVideoMediaStream();
3124 caller()->CreateAndSetAndSignalOffer();
3125 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3126
3127 // Negotiate again, disabling the video "m=" section (the callee will set the
3128 // port to 0 due to offer_to_receive_video = 0).
3129 PeerConnectionInterface::RTCOfferAnswerOptions options;
3130 options.offer_to_receive_video = 0;
3131 callee()->SetOfferAnswerOptions(options);
3132 caller()->CreateAndSetAndSignalOffer();
3133 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3134 // Sanity check that video "m=" section was actually rejected.
3135 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
3136 callee()->pc()->local_description()->description());
3137 ASSERT_NE(nullptr, answer_video_content);
3138 ASSERT_TRUE(answer_video_content->rejected);
3139
3140 // Enable video and do negotiation again, making sure video is received
3141 // end-to-end, also adding media stream to callee.
3142 options.offer_to_receive_video = 1;
3143 callee()->SetOfferAnswerOptions(options);
3144 callee()->AddAudioVideoMediaStream();
3145 caller()->CreateAndSetAndSignalOffer();
3146 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3147 // Verify the caller receives frames from the newly added stream, and the
3148 // callee receives additional frames from the re-enabled video m= section.
3149 ExpectNewFramesReceivedWithWait(
3150 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3151 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3152 kMaxWaitForFramesMs);
3153}
3154
3155// This test sets up a Jsep call between two parties with external
3156// VideoDecoderFactory.
3157// TODO(holmer): Disabled due to sometimes crashing on buildbots.
3158// See issue webrtc/2378.
3159TEST_F(PeerConnectionIntegrationTest,
3160 DISABLED_EndToEndCallWithVideoDecoderFactory) {
3161 ASSERT_TRUE(CreatePeerConnectionWrappers());
3162 EnableVideoDecoderFactory();
3163 ConnectFakeSignaling();
3164 caller()->AddAudioVideoMediaStream();
3165 callee()->AddAudioVideoMediaStream();
3166 caller()->CreateAndSetAndSignalOffer();
3167 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3168 ExpectNewFramesReceivedWithWait(
3169 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3170 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3171 kMaxWaitForFramesMs);
3172}
3173
3174// This tests that if we negotiate after calling CreateSender but before we
3175// have a track, then set a track later, frames from the newly-set track are
3176// received end-to-end.
3177// TODO(deadbeef): Change this test to use AddTransceiver, once that's
3178// implemented.
3179TEST_F(PeerConnectionIntegrationTest,
3180 MediaFlowsAfterEarlyWarmupWithCreateSender) {
3181 ASSERT_TRUE(CreatePeerConnectionWrappers());
3182 ConnectFakeSignaling();
3183 auto caller_audio_sender =
3184 caller()->pc()->CreateSender("audio", "caller_stream");
3185 auto caller_video_sender =
3186 caller()->pc()->CreateSender("video", "caller_stream");
3187 auto callee_audio_sender =
3188 callee()->pc()->CreateSender("audio", "callee_stream");
3189 auto callee_video_sender =
3190 callee()->pc()->CreateSender("video", "callee_stream");
3191 caller()->CreateAndSetAndSignalOffer();
3192 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3193 // Wait for ICE to complete, without any tracks being set.
3194 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3195 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3196 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3197 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3198 // Now set the tracks, and expect frames to immediately start flowing.
3199 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3200 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3201 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3202 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
3203 ExpectNewFramesReceivedWithWait(
3204 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3205 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3206 kMaxWaitForFramesMs);
3207}
3208
3209// This test verifies that a remote video track can be added via AddStream,
3210// and sent end-to-end. For this particular test, it's simply echoed back
3211// from the caller to the callee, rather than being forwarded to a third
3212// PeerConnection.
3213TEST_F(PeerConnectionIntegrationTest, CanSendRemoteVideoTrack) {
3214 ASSERT_TRUE(CreatePeerConnectionWrappers());
3215 ConnectFakeSignaling();
3216 // Just send a video track from the caller.
3217 caller()->AddMediaStreamFromTracks(nullptr,
3218 caller()->CreateLocalVideoTrack());
3219 caller()->CreateAndSetAndSignalOffer();
3220 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3221 ASSERT_EQ(1, callee()->remote_streams()->count());
3222
3223 // Echo the stream back, and do a new offer/anwer (initiated by callee this
3224 // time).
3225 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
3226 callee()->CreateAndSetAndSignalOffer();
3227 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3228
3229 int expected_caller_received_video_frames = kDefaultExpectedVideoFrameCount;
3230 ExpectNewFramesReceivedWithWait(0, expected_caller_received_video_frames, 0,
3231 0, kMaxWaitForFramesMs);
3232}
3233
3234// Test that we achieve the expected end-to-end connection time, using a
3235// fake clock and simulated latency on the media and signaling paths.
3236// We use a TURN<->TURN connection because this is usually the quickest to
3237// set up initially, especially when we're confident the connection will work
3238// and can start sending media before we get a STUN response.
3239//
3240// With various optimizations enabled, here are the network delays we expect to
3241// be on the critical path:
3242// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
3243// signaling answer (with DTLS fingerprint).
3244// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
3245// using TURN<->TURN pair, and DTLS exchange is 4 packets,
3246// the first of which should have arrived before the answer.
3247TEST_F(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
3248 rtc::ScopedFakeClock fake_clock;
3249 // Some things use a time of "0" as a special value, so we need to start out
3250 // the fake clock at a nonzero time.
3251 // TODO(deadbeef): Fix this.
3252 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
3253
3254 static constexpr int media_hop_delay_ms = 50;
3255 static constexpr int signaling_trip_delay_ms = 500;
3256 // For explanation of these values, see comment above.
3257 static constexpr int required_media_hops = 9;
3258 static constexpr int required_signaling_trips = 2;
3259 // For internal delays (such as posting an event asychronously).
3260 static constexpr int allowed_internal_delay_ms = 20;
3261 static constexpr int total_connection_time_ms =
3262 media_hop_delay_ms * required_media_hops +
3263 signaling_trip_delay_ms * required_signaling_trips +
3264 allowed_internal_delay_ms;
3265
3266 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3267 3478};
3268 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3269 0};
3270 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3271 3478};
3272 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3273 0};
3274 cricket::TestTurnServer turn_server_1(network_thread(),
3275 turn_server_1_internal_address,
3276 turn_server_1_external_address);
3277 cricket::TestTurnServer turn_server_2(network_thread(),
3278 turn_server_2_internal_address,
3279 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003280
deadbeef1dcb1642017-03-29 21:08:16 -07003281 // Bypass permission check on received packets so media can be sent before
3282 // the candidate is signaled.
3283 turn_server_1.set_enable_permission_checks(false);
3284 turn_server_2.set_enable_permission_checks(false);
3285
3286 PeerConnectionInterface::RTCConfiguration client_1_config;
3287 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3288 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3289 ice_server_1.username = "test";
3290 ice_server_1.password = "test";
3291 client_1_config.servers.push_back(ice_server_1);
3292 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3293 client_1_config.presume_writable_when_fully_relayed = true;
3294
3295 PeerConnectionInterface::RTCConfiguration client_2_config;
3296 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3297 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3298 ice_server_2.username = "test";
3299 ice_server_2.password = "test";
3300 client_2_config.servers.push_back(ice_server_2);
3301 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3302 client_2_config.presume_writable_when_fully_relayed = true;
3303
3304 ASSERT_TRUE(
3305 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3306 // Set up the simulated delays.
3307 SetSignalingDelayMs(signaling_trip_delay_ms);
3308 ConnectFakeSignaling();
3309 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
3310 virtual_socket_server()->UpdateDelayDistribution();
3311
3312 // Set "offer to receive audio/video" without adding any tracks, so we just
3313 // set up ICE/DTLS with no media.
3314 PeerConnectionInterface::RTCOfferAnswerOptions options;
3315 options.offer_to_receive_audio = 1;
3316 options.offer_to_receive_video = 1;
3317 caller()->SetOfferAnswerOptions(options);
3318 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07003319 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
3320 fake_clock);
deadbeef1dcb1642017-03-29 21:08:16 -07003321 // Need to free the clients here since they're using things we created on
3322 // the stack.
3323 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3324 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3325}
3326
Jonas Orelandbdcee282017-10-10 14:01:40 +02003327// Verify that a TurnCustomizer passed in through RTCConfiguration
3328// is actually used by the underlying TURN candidate pair.
3329// Note that turnport_unittest.cc contains more detailed, lower-level tests.
3330TEST_F(PeerConnectionIntegrationTest, \
3331 TurnCustomizerUsedForTurnConnections) {
3332 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3333 3478};
3334 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3335 0};
3336 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3337 3478};
3338 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3339 0};
3340 cricket::TestTurnServer turn_server_1(network_thread(),
3341 turn_server_1_internal_address,
3342 turn_server_1_external_address);
3343 cricket::TestTurnServer turn_server_2(network_thread(),
3344 turn_server_2_internal_address,
3345 turn_server_2_external_address);
3346
3347 PeerConnectionInterface::RTCConfiguration client_1_config;
3348 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3349 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3350 ice_server_1.username = "test";
3351 ice_server_1.password = "test";
3352 client_1_config.servers.push_back(ice_server_1);
3353 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3354 auto customizer1 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3355 client_1_config.turn_customizer = customizer1.get();
3356
3357 PeerConnectionInterface::RTCConfiguration client_2_config;
3358 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3359 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3360 ice_server_2.username = "test";
3361 ice_server_2.password = "test";
3362 client_2_config.servers.push_back(ice_server_2);
3363 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3364 auto customizer2 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3365 client_2_config.turn_customizer = customizer2.get();
3366
3367 ASSERT_TRUE(
3368 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3369 ConnectFakeSignaling();
3370
3371 // Set "offer to receive audio/video" without adding any tracks, so we just
3372 // set up ICE/DTLS with no media.
3373 PeerConnectionInterface::RTCOfferAnswerOptions options;
3374 options.offer_to_receive_audio = 1;
3375 options.offer_to_receive_video = 1;
3376 caller()->SetOfferAnswerOptions(options);
3377 caller()->CreateAndSetAndSignalOffer();
3378 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3379
3380 EXPECT_GT(customizer1->allow_channel_data_cnt_, 0u);
3381 EXPECT_GT(customizer1->modify_cnt_, 0u);
3382
3383 EXPECT_GT(customizer2->allow_channel_data_cnt_, 0u);
3384 EXPECT_GT(customizer2->modify_cnt_, 0u);
3385
3386 // Need to free the clients here since they're using things we created on
3387 // the stack.
3388 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3389 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3390}
3391
deadbeefc964d0b2017-04-03 10:03:35 -07003392// Test that audio and video flow end-to-end when codec names don't use the
3393// expected casing, given that they're supposed to be case insensitive. To test
3394// this, all but one codec is removed from each media description, and its
3395// casing is changed.
3396//
3397// In the past, this has regressed and caused crashes/black video, due to the
3398// fact that code at some layers was doing case-insensitive comparisons and
3399// code at other layers was not.
3400TEST_F(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
3401 ASSERT_TRUE(CreatePeerConnectionWrappers());
3402 ConnectFakeSignaling();
3403 caller()->AddAudioVideoMediaStream();
3404 callee()->AddAudioVideoMediaStream();
3405
3406 // Remove all but one audio/video codec (opus and VP8), and change the
3407 // casing of the caller's generated offer.
3408 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
3409 cricket::AudioContentDescription* audio =
3410 GetFirstAudioContentDescription(description);
3411 ASSERT_NE(nullptr, audio);
3412 auto audio_codecs = audio->codecs();
3413 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
3414 [](const cricket::AudioCodec& codec) {
3415 return codec.name != "opus";
3416 }),
3417 audio_codecs.end());
3418 ASSERT_EQ(1u, audio_codecs.size());
3419 audio_codecs[0].name = "OpUs";
3420 audio->set_codecs(audio_codecs);
3421
3422 cricket::VideoContentDescription* video =
3423 GetFirstVideoContentDescription(description);
3424 ASSERT_NE(nullptr, video);
3425 auto video_codecs = video->codecs();
3426 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
3427 [](const cricket::VideoCodec& codec) {
3428 return codec.name != "VP8";
3429 }),
3430 video_codecs.end());
3431 ASSERT_EQ(1u, video_codecs.size());
3432 video_codecs[0].name = "vP8";
3433 video->set_codecs(video_codecs);
3434 });
3435
3436 caller()->CreateAndSetAndSignalOffer();
3437 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3438
3439 // Verify frames are still received end-to-end.
3440 ExpectNewFramesReceivedWithWait(
3441 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3442 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3443 kMaxWaitForFramesMs);
3444}
3445
hbos8d609f62017-04-10 07:39:05 -07003446TEST_F(PeerConnectionIntegrationTest, GetSources) {
3447 ASSERT_TRUE(CreatePeerConnectionWrappers());
3448 ConnectFakeSignaling();
3449 caller()->AddAudioOnlyMediaStream();
3450 caller()->CreateAndSetAndSignalOffer();
3451 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07003452 // Wait for one audio frame to be received by the callee.
hbos8d609f62017-04-10 07:39:05 -07003453 ExpectNewFramesReceivedWithWait(0, 0, 1, 0, kMaxWaitForFramesMs);
3454 ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u);
3455 auto receiver = callee()->pc()->GetReceivers()[0];
3456 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
3457
3458 auto contributing_sources = receiver->GetSources();
3459 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
3460 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
3461 contributing_sources[0].source_id());
3462}
3463
deadbeef2f425aa2017-04-14 10:41:32 -07003464// Test that if a track is removed and added again with a different stream ID,
3465// the new stream ID is successfully communicated in SDP and media continues to
3466// flow end-to-end.
3467TEST_F(PeerConnectionIntegrationTest, RemoveAndAddTrackWithNewStreamId) {
3468 ASSERT_TRUE(CreatePeerConnectionWrappers());
3469 ConnectFakeSignaling();
3470
3471 rtc::scoped_refptr<MediaStreamInterface> stream_1 =
3472 caller()->pc_factory()->CreateLocalMediaStream("stream_1");
3473 rtc::scoped_refptr<MediaStreamInterface> stream_2 =
3474 caller()->pc_factory()->CreateLocalMediaStream("stream_2");
3475
3476 // Add track using stream 1, do offer/answer.
3477 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
3478 caller()->CreateLocalAudioTrack();
3479 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
3480 caller()->pc()->AddTrack(track, {stream_1.get()});
3481 caller()->CreateAndSetAndSignalOffer();
3482 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3483 // Wait for one audio frame to be received by the callee.
3484 ExpectNewFramesReceivedWithWait(0, 0, 1, 0, kMaxWaitForFramesMs);
3485
3486 // Remove the sender, and create a new one with the new stream.
3487 caller()->pc()->RemoveTrack(sender);
3488 sender = caller()->pc()->AddTrack(track, {stream_2.get()});
3489 caller()->CreateAndSetAndSignalOffer();
3490 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3491 // Wait for additional audio frames to be received by the callee.
3492 ExpectNewFramesReceivedWithWait(0, 0, kDefaultExpectedAudioFrameCount, 0,
3493 kMaxWaitForFramesMs);
3494}
3495
Elad Alon99c3fe52017-10-13 16:29:40 +02003496TEST_F(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
3497 ASSERT_TRUE(CreatePeerConnectionWrappers());
3498 ConnectFakeSignaling();
3499
3500 auto output = rtc::MakeUnique<testing::NiceMock<MockRtcEventLogOutput>>();
3501 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
3502 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
3503 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
3504 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(std::move(output)));
3505
3506 caller()->AddAudioVideoMediaStream();
3507 caller()->CreateAndSetAndSignalOffer();
3508 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3509}
3510
Steve Antonede9ca52017-10-16 13:04:27 -07003511// Test that if candidates are only signaled by applying full session
3512// descriptions (instead of using AddIceCandidate), the peers can connect to
3513// each other and exchange media.
3514TEST_F(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
3515 ASSERT_TRUE(CreatePeerConnectionWrappers());
3516 // Each side will signal the session descriptions but not candidates.
3517 ConnectFakeSignalingForSdpOnly();
3518
3519 // Add audio video track and exchange the initial offer/answer with media
3520 // information only. This will start ICE gathering on each side.
3521 caller()->AddAudioVideoMediaStream();
3522 callee()->AddAudioVideoMediaStream();
3523 caller()->CreateAndSetAndSignalOffer();
3524
3525 // Wait for all candidates to be gathered on both the caller and callee.
3526 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3527 caller()->ice_gathering_state(), kDefaultTimeout);
3528 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3529 callee()->ice_gathering_state(), kDefaultTimeout);
3530
3531 // The candidates will now be included in the session description, so
3532 // signaling them will start the ICE connection.
3533 caller()->CreateAndSetAndSignalOffer();
3534 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3535
3536 // Ensure that media flows in both directions.
3537 ExpectNewFramesReceivedWithWait(
3538 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3539 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3540 kMaxWaitForFramesMs);
3541}
3542
deadbeef1dcb1642017-03-29 21:08:16 -07003543} // namespace
3544
3545#endif // if !defined(THREAD_SANITIZER)