blob: aee5ca189446571812c8827ccfc4946a158ace43 [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 Bonadeic61ce0d2017-11-21 17:04:20 +010031#include "api/rtpreceiverinterface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "api/test/fakeconstraints.h"
33#include "media/engine/fakewebrtcvideoengine.h"
34#include "p2p/base/p2pconstants.h"
35#include "p2p/base/portinterface.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"
Steve Anton4ab68ee2017-12-19 14:26:11 -080045#include "pc/sessiondescription.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "pc/test/fakeaudiocapturemodule.h"
47#include "pc/test/fakeperiodicvideocapturer.h"
48#include "pc/test/fakertccertificategenerator.h"
49#include "pc/test/fakevideotrackrenderer.h"
50#include "pc/test/mockpeerconnectionobservers.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020051#include "rtc_base/fakenetwork.h"
Steve Antonede9ca52017-10-16 13:04:27 -070052#include "rtc_base/firewallsocketserver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020053#include "rtc_base/gunit.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020054#include "rtc_base/virtualsocketserver.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020055#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070056
57using cricket::ContentInfo;
58using cricket::FakeWebRtcVideoDecoder;
59using cricket::FakeWebRtcVideoDecoderFactory;
60using cricket::FakeWebRtcVideoEncoder;
61using cricket::FakeWebRtcVideoEncoderFactory;
62using cricket::MediaContentDescription;
Steve Antonede9ca52017-10-16 13:04:27 -070063using rtc::SocketAddress;
64using ::testing::ElementsAre;
65using ::testing::Values;
deadbeef1dcb1642017-03-29 21:08:16 -070066using webrtc::DataBuffer;
67using webrtc::DataChannelInterface;
68using webrtc::DtmfSender;
69using webrtc::DtmfSenderInterface;
70using webrtc::DtmfSenderObserverInterface;
71using webrtc::FakeConstraints;
Steve Anton15324772018-01-16 10:26:49 -080072using webrtc::FakeVideoTrackRenderer;
deadbeef1dcb1642017-03-29 21:08:16 -070073using webrtc::MediaConstraintsInterface;
74using webrtc::MediaStreamInterface;
75using webrtc::MediaStreamTrackInterface;
76using webrtc::MockCreateSessionDescriptionObserver;
77using webrtc::MockDataChannelObserver;
78using webrtc::MockSetSessionDescriptionObserver;
79using webrtc::MockStatsObserver;
80using webrtc::ObserverInterface;
Steve Anton8c0f7a72017-10-03 10:03:10 -070081using webrtc::PeerConnection;
deadbeef1dcb1642017-03-29 21:08:16 -070082using webrtc::PeerConnectionInterface;
83using webrtc::PeerConnectionFactory;
Steve Anton8c0f7a72017-10-03 10:03:10 -070084using webrtc::PeerConnectionProxy;
Steve Anton15324772018-01-16 10:26:49 -080085using webrtc::RTCErrorType;
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +010086using webrtc::RtpReceiverInterface;
Steve Antond3679212018-01-17 17:41:02 -080087using webrtc::SdpSemantics;
Steve Antona3a92c22017-12-07 10:27:41 -080088using webrtc::SdpType;
deadbeef1dcb1642017-03-29 21:08:16 -070089using webrtc::SessionDescriptionInterface;
90using webrtc::StreamCollectionInterface;
Steve Anton15324772018-01-16 10:26:49 -080091using webrtc::VideoTrackInterface;
deadbeef1dcb1642017-03-29 21:08:16 -070092
93namespace {
94
95static const int kDefaultTimeout = 10000;
96static const int kMaxWaitForStatsMs = 3000;
97static const int kMaxWaitForActivationMs = 5000;
98static const int kMaxWaitForFramesMs = 10000;
99// Default number of audio/video frames to wait for before considering a test
100// successful.
101static const int kDefaultExpectedAudioFrameCount = 3;
102static const int kDefaultExpectedVideoFrameCount = 3;
103
deadbeef1dcb1642017-03-29 21:08:16 -0700104static const char kDataChannelLabel[] = "data_channel";
105
106// SRTP cipher name negotiated by the tests. This must be updated if the
107// default changes.
108static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_32;
109static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
110
Steve Antonede9ca52017-10-16 13:04:27 -0700111static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
112
deadbeef1dcb1642017-03-29 21:08:16 -0700113// Helper function for constructing offer/answer options to initiate an ICE
114// restart.
115PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
116 PeerConnectionInterface::RTCOfferAnswerOptions options;
117 options.ice_restart = true;
118 return options;
119}
120
deadbeefd8ad7882017-04-18 16:01:17 -0700121// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
122// attribute from received SDP, simulating a legacy endpoint.
123void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
124 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800125 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700126 }
127 desc->set_msid_supported(false);
128}
129
zhihuangf8164932017-05-19 13:09:47 -0700130int FindFirstMediaStatsIndexByKind(
131 const std::string& kind,
132 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
133 media_stats_vec) {
134 for (size_t i = 0; i < media_stats_vec.size(); i++) {
135 if (media_stats_vec[i]->kind.ValueToString() == kind) {
136 return i;
137 }
138 }
139 return -1;
140}
141
deadbeef1dcb1642017-03-29 21:08:16 -0700142class SignalingMessageReceiver {
143 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800144 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700145 virtual void ReceiveIceMessage(const std::string& sdp_mid,
146 int sdp_mline_index,
147 const std::string& msg) = 0;
148
149 protected:
150 SignalingMessageReceiver() {}
151 virtual ~SignalingMessageReceiver() {}
152};
153
154class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
155 public:
156 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
157 : expected_media_type_(media_type) {}
158
159 void OnFirstPacketReceived(cricket::MediaType media_type) override {
160 ASSERT_EQ(expected_media_type_, media_type);
161 first_packet_received_ = true;
162 }
163
164 bool first_packet_received() const { return first_packet_received_; }
165
166 virtual ~MockRtpReceiverObserver() {}
167
168 private:
169 bool first_packet_received_ = false;
170 cricket::MediaType expected_media_type_;
171};
172
173// Helper class that wraps a peer connection, observes it, and can accept
174// signaling messages from another wrapper.
175//
176// Uses a fake network, fake A/V capture, and optionally fake
177// encoders/decoders, though they aren't used by default since they don't
178// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700179// TODO(steveanton): See how this could become a subclass of
180// PeerConnectionWrapper defined in peerconnectionwrapper.h .
deadbeef1dcb1642017-03-29 21:08:16 -0700181class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800182 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700183 public:
184 // Different factory methods for convenience.
185 // TODO(deadbeef): Could use the pattern of:
186 //
187 // PeerConnectionWrapper =
188 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
189 //
190 // To reduce some code duplication.
191 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
192 const std::string& debug_name,
193 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
194 rtc::Thread* network_thread,
195 rtc::Thread* worker_thread) {
196 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
197 if (!client->Init(nullptr, nullptr, nullptr, std::move(cert_generator),
198 network_thread, worker_thread)) {
199 delete client;
200 return nullptr;
201 }
202 return client;
203 }
204
205 static PeerConnectionWrapper* CreateWithConfig(
206 const std::string& debug_name,
207 const PeerConnectionInterface::RTCConfiguration& config,
208 rtc::Thread* network_thread,
209 rtc::Thread* worker_thread) {
210 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
211 new FakeRTCCertificateGenerator());
212 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
213 if (!client->Init(nullptr, nullptr, &config, std::move(cert_generator),
214 network_thread, worker_thread)) {
215 delete client;
216 return nullptr;
217 }
218 return client;
219 }
220
221 static PeerConnectionWrapper* CreateWithOptions(
222 const std::string& debug_name,
223 const PeerConnectionFactory::Options& options,
224 rtc::Thread* network_thread,
225 rtc::Thread* worker_thread) {
226 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
227 new FakeRTCCertificateGenerator());
228 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
229 if (!client->Init(nullptr, &options, nullptr, std::move(cert_generator),
230 network_thread, worker_thread)) {
231 delete client;
232 return nullptr;
233 }
234 return client;
235 }
236
237 static PeerConnectionWrapper* CreateWithConstraints(
238 const std::string& debug_name,
239 const MediaConstraintsInterface* constraints,
240 rtc::Thread* network_thread,
241 rtc::Thread* worker_thread) {
242 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
243 new FakeRTCCertificateGenerator());
244 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
245 if (!client->Init(constraints, nullptr, nullptr, std::move(cert_generator),
246 network_thread, worker_thread)) {
247 delete client;
248 return nullptr;
249 }
250 return client;
251 }
252
deadbeef2f425aa2017-04-14 10:41:32 -0700253 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
254 return peer_connection_factory_.get();
255 }
256
deadbeef1dcb1642017-03-29 21:08:16 -0700257 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
258
259 // If a signaling message receiver is set (via ConnectFakeSignaling), this
260 // will set the whole offer/answer exchange in motion. Just need to wait for
261 // the signaling state to reach "stable".
262 void CreateAndSetAndSignalOffer() {
263 auto offer = CreateOffer();
264 ASSERT_NE(nullptr, offer);
265 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
266 }
267
268 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
269 // when a remote offer is received (via fake signaling) and an answer is
270 // generated. By default, uses default options.
271 void SetOfferAnswerOptions(
272 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
273 offer_answer_options_ = options;
274 }
275
276 // Set a callback to be invoked when SDP is received via the fake signaling
277 // channel, which provides an opportunity to munge (modify) the SDP. This is
278 // used to test SDP being applied that a PeerConnection would normally not
279 // generate, but a non-JSEP endpoint might.
280 void SetReceivedSdpMunger(
281 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100282 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700283 }
284
deadbeefc964d0b2017-04-03 10:03:35 -0700285 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700286 // generated.
287 void SetGeneratedSdpMunger(
288 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100289 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700290 }
291
Steve Antonede9ca52017-10-16 13:04:27 -0700292 // Every ICE connection state in order that has been seen by the observer.
293 std::vector<PeerConnectionInterface::IceConnectionState>
294 ice_connection_state_history() const {
295 return ice_connection_state_history_;
296 }
Steve Anton6f25b092017-10-23 09:39:20 -0700297 void clear_ice_connection_state_history() {
298 ice_connection_state_history_.clear();
299 }
Steve Antonede9ca52017-10-16 13:04:27 -0700300
301 // Every ICE gathering state in order that has been seen by the observer.
302 std::vector<PeerConnectionInterface::IceGatheringState>
303 ice_gathering_state_history() const {
304 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700305 }
306
Steve Anton15324772018-01-16 10:26:49 -0800307 void AddAudioVideoTracks() {
308 AddAudioTrack();
309 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700310 }
311
Steve Anton15324772018-01-16 10:26:49 -0800312 void AddAudioTrack() { AddTrack(CreateLocalAudioTrack()); }
deadbeef1dcb1642017-03-29 21:08:16 -0700313
Steve Anton15324772018-01-16 10:26:49 -0800314 void AddVideoTrack() { AddTrack(CreateLocalVideoTrack()); }
deadbeef1dcb1642017-03-29 21:08:16 -0700315
316 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
317 FakeConstraints constraints;
318 // Disable highpass filter so that we can get all the test audio frames.
319 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
320 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
321 peer_connection_factory_->CreateAudioSource(&constraints);
322 // TODO(perkj): Test audio source when it is implemented. Currently audio
323 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700324 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700325 source);
326 }
327
328 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
deadbeefb1a15d72017-09-07 14:12:05 -0700329 return CreateLocalVideoTrackInternal(FakeConstraints(),
330 webrtc::kVideoRotation_0);
deadbeef1dcb1642017-03-29 21:08:16 -0700331 }
332
333 rtc::scoped_refptr<webrtc::VideoTrackInterface>
334 CreateLocalVideoTrackWithConstraints(const FakeConstraints& constraints) {
deadbeefb1a15d72017-09-07 14:12:05 -0700335 return CreateLocalVideoTrackInternal(constraints, webrtc::kVideoRotation_0);
deadbeef1dcb1642017-03-29 21:08:16 -0700336 }
337
338 rtc::scoped_refptr<webrtc::VideoTrackInterface>
339 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
deadbeefb1a15d72017-09-07 14:12:05 -0700340 return CreateLocalVideoTrackInternal(FakeConstraints(), rotation);
deadbeef1dcb1642017-03-29 21:08:16 -0700341 }
342
Steve Anton15324772018-01-16 10:26:49 -0800343 void AddTrack(rtc::scoped_refptr<MediaStreamTrackInterface> track,
344 const std::vector<std::string>& stream_labels = {}) {
345 auto result = pc()->AddTrack(track, stream_labels);
346 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
deadbeef1dcb1642017-03-29 21:08:16 -0700347 }
348
349 bool SignalingStateStable() {
350 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
351 }
352
353 void CreateDataChannel() { CreateDataChannel(nullptr); }
354
355 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700356 CreateDataChannel(kDataChannelLabel, init);
357 }
358
359 void CreateDataChannel(const std::string& label,
360 const webrtc::DataChannelInit* init) {
361 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700362 ASSERT_TRUE(data_channel_.get() != nullptr);
363 data_observer_.reset(new MockDataChannelObserver(data_channel_));
364 }
365
366 DataChannelInterface* data_channel() { return data_channel_; }
367 const MockDataChannelObserver* data_observer() const {
368 return data_observer_.get();
369 }
370
371 int audio_frames_received() const {
372 return fake_audio_capture_module_->frames_received();
373 }
374
375 // Takes minimum of video frames received for each track.
376 //
377 // Can be used like:
378 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
379 //
380 // To ensure that all video tracks received at least a certain number of
381 // frames.
382 int min_video_frames_received_per_track() const {
383 int min_frames = INT_MAX;
384 if (video_decoder_factory_enabled_) {
385 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
386 fake_video_decoder_factory_->decoders();
387 if (decoders.empty()) {
388 return 0;
389 }
390 for (FakeWebRtcVideoDecoder* decoder : decoders) {
391 min_frames = std::min(min_frames, decoder->GetNumFramesReceived());
392 }
393 return min_frames;
394 } else {
395 if (fake_video_renderers_.empty()) {
396 return 0;
397 }
398
399 for (const auto& pair : fake_video_renderers_) {
400 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
401 }
402 return min_frames;
403 }
404 }
405
406 // In contrast to the above, sums the video frames received for all tracks.
407 // Can be used to verify that no video frames were received, or that the
408 // counts didn't increase.
409 int total_video_frames_received() const {
410 int total = 0;
411 if (video_decoder_factory_enabled_) {
412 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
413 fake_video_decoder_factory_->decoders();
414 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
415 total += decoder->GetNumFramesReceived();
416 }
417 } else {
418 for (const auto& pair : fake_video_renderers_) {
419 total += pair.second->num_rendered_frames();
420 }
421 for (const auto& renderer : removed_fake_video_renderers_) {
422 total += renderer->num_rendered_frames();
423 }
424 }
425 return total;
426 }
427
428 // Returns a MockStatsObserver in a state after stats gathering finished,
429 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700430 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700431 webrtc::MediaStreamTrackInterface* track) {
432 rtc::scoped_refptr<MockStatsObserver> observer(
433 new rtc::RefCountedObject<MockStatsObserver>());
434 EXPECT_TRUE(peer_connection_->GetStats(
435 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
436 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
437 return observer;
438 }
439
440 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700441 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
442 return OldGetStatsForTrack(nullptr);
443 }
444
445 // Synchronously gets stats and returns them. If it times out, fails the test
446 // and returns null.
447 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
448 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
449 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
450 peer_connection_->GetStats(callback);
451 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
452 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700453 }
454
455 int rendered_width() {
456 EXPECT_FALSE(fake_video_renderers_.empty());
457 return fake_video_renderers_.empty()
458 ? 0
459 : fake_video_renderers_.begin()->second->width();
460 }
461
462 int rendered_height() {
463 EXPECT_FALSE(fake_video_renderers_.empty());
464 return fake_video_renderers_.empty()
465 ? 0
466 : fake_video_renderers_.begin()->second->height();
467 }
468
469 double rendered_aspect_ratio() {
470 if (rendered_height() == 0) {
471 return 0.0;
472 }
473 return static_cast<double>(rendered_width()) / rendered_height();
474 }
475
476 webrtc::VideoRotation rendered_rotation() {
477 EXPECT_FALSE(fake_video_renderers_.empty());
478 return fake_video_renderers_.empty()
479 ? webrtc::kVideoRotation_0
480 : fake_video_renderers_.begin()->second->rotation();
481 }
482
483 int local_rendered_width() {
484 return local_video_renderer_ ? local_video_renderer_->width() : 0;
485 }
486
487 int local_rendered_height() {
488 return local_video_renderer_ ? local_video_renderer_->height() : 0;
489 }
490
491 double local_rendered_aspect_ratio() {
492 if (local_rendered_height() == 0) {
493 return 0.0;
494 }
495 return static_cast<double>(local_rendered_width()) /
496 local_rendered_height();
497 }
498
499 size_t number_of_remote_streams() {
500 if (!pc()) {
501 return 0;
502 }
503 return pc()->remote_streams()->count();
504 }
505
506 StreamCollectionInterface* remote_streams() const {
507 if (!pc()) {
508 ADD_FAILURE();
509 return nullptr;
510 }
511 return pc()->remote_streams();
512 }
513
514 StreamCollectionInterface* local_streams() {
515 if (!pc()) {
516 ADD_FAILURE();
517 return nullptr;
518 }
519 return pc()->local_streams();
520 }
521
522 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
523 return pc()->signaling_state();
524 }
525
526 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
527 return pc()->ice_connection_state();
528 }
529
530 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
531 return pc()->ice_gathering_state();
532 }
533
534 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
535 // GetReceivers. They're updated automatically when a remote offer/answer
536 // from the fake signaling channel is applied, or when
537 // ResetRtpReceiverObservers below is called.
538 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
539 rtp_receiver_observers() {
540 return rtp_receiver_observers_;
541 }
542
543 void ResetRtpReceiverObservers() {
544 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100545 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
546 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700547 std::unique_ptr<MockRtpReceiverObserver> observer(
548 new MockRtpReceiverObserver(receiver->media_type()));
549 receiver->SetObserver(observer.get());
550 rtp_receiver_observers_.push_back(std::move(observer));
551 }
552 }
553
Steve Antonede9ca52017-10-16 13:04:27 -0700554 rtc::FakeNetworkManager* network() const {
555 return fake_network_manager_.get();
556 }
557 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
558
deadbeef1dcb1642017-03-29 21:08:16 -0700559 private:
560 explicit PeerConnectionWrapper(const std::string& debug_name)
561 : debug_name_(debug_name) {}
562
563 bool Init(
564 const MediaConstraintsInterface* constraints,
565 const PeerConnectionFactory::Options* options,
566 const PeerConnectionInterface::RTCConfiguration* config,
567 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
568 rtc::Thread* network_thread,
569 rtc::Thread* worker_thread) {
570 // There's an error in this test code if Init ends up being called twice.
571 RTC_DCHECK(!peer_connection_);
572 RTC_DCHECK(!peer_connection_factory_);
573
574 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700575 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700576
577 std::unique_ptr<cricket::PortAllocator> port_allocator(
578 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700579 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700580 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
581 if (!fake_audio_capture_module_) {
582 return false;
583 }
584 // Note that these factories don't end up getting used unless supported
585 // codecs are added to them.
586 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
587 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
588 rtc::Thread* const signaling_thread = rtc::Thread::Current();
589 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
590 network_thread, worker_thread, signaling_thread,
Karl Wiberg1b0eae32017-10-17 14:48:54 +0200591 fake_audio_capture_module_, webrtc::CreateBuiltinAudioEncoderFactory(),
592 webrtc::CreateBuiltinAudioDecoderFactory(), fake_video_encoder_factory_,
deadbeef1dcb1642017-03-29 21:08:16 -0700593 fake_video_decoder_factory_);
594 if (!peer_connection_factory_) {
595 return false;
596 }
597 if (options) {
598 peer_connection_factory_->SetOptions(*options);
599 }
600 peer_connection_ =
601 CreatePeerConnection(std::move(port_allocator), constraints, config,
602 std::move(cert_generator));
603 return peer_connection_.get() != nullptr;
604 }
605
606 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
607 std::unique_ptr<cricket::PortAllocator> port_allocator,
608 const MediaConstraintsInterface* constraints,
609 const PeerConnectionInterface::RTCConfiguration* config,
610 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator) {
611 PeerConnectionInterface::RTCConfiguration modified_config;
612 // If |config| is null, this will result in a default configuration being
613 // used.
614 if (config) {
615 modified_config = *config;
616 }
617 // Disable resolution adaptation; we don't want it interfering with the
618 // test results.
619 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
620 // ratios and not specific resolutions, is this even necessary?
621 modified_config.set_cpu_adaptation(false);
622
623 return peer_connection_factory_->CreatePeerConnection(
624 modified_config, constraints, std::move(port_allocator),
625 std::move(cert_generator), this);
626 }
627
628 void set_signaling_message_receiver(
629 SignalingMessageReceiver* signaling_message_receiver) {
630 signaling_message_receiver_ = signaling_message_receiver;
631 }
632
633 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
634
Steve Antonede9ca52017-10-16 13:04:27 -0700635 void set_signal_ice_candidates(bool signal) {
636 signal_ice_candidates_ = signal;
637 }
638
deadbeef1dcb1642017-03-29 21:08:16 -0700639 void EnableVideoDecoderFactory() {
640 video_decoder_factory_enabled_ = true;
641 fake_video_decoder_factory_->AddSupportedVideoCodecType(
642 webrtc::kVideoCodecVP8);
643 }
644
645 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
deadbeef1dcb1642017-03-29 21:08:16 -0700646 const FakeConstraints& constraints,
647 webrtc::VideoRotation rotation) {
648 // Set max frame rate to 10fps to reduce the risk of test flakiness.
649 // TODO(deadbeef): Do something more robust.
650 FakeConstraints source_constraints = constraints;
651 source_constraints.SetMandatoryMaxFrameRate(10);
652
653 cricket::FakeVideoCapturer* fake_capturer =
654 new webrtc::FakePeriodicVideoCapturer();
655 fake_capturer->SetRotation(rotation);
656 video_capturers_.push_back(fake_capturer);
657 rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source =
658 peer_connection_factory_->CreateVideoSource(fake_capturer,
659 &source_constraints);
660 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
deadbeefb1a15d72017-09-07 14:12:05 -0700661 peer_connection_factory_->CreateVideoTrack(rtc::CreateRandomUuid(),
662 source));
deadbeef1dcb1642017-03-29 21:08:16 -0700663 if (!local_video_renderer_) {
664 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
665 }
666 return track;
667 }
668
669 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100670 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800671 std::unique_ptr<SessionDescriptionInterface> desc =
672 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700673 if (received_sdp_munger_) {
674 received_sdp_munger_(desc->description());
675 }
676
677 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
678 // Setting a remote description may have changed the number of receivers,
679 // so reset the receiver observers.
680 ResetRtpReceiverObservers();
681 auto answer = CreateAnswer();
682 ASSERT_NE(nullptr, answer);
683 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
684 }
685
686 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100687 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800688 std::unique_ptr<SessionDescriptionInterface> desc =
689 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700690 if (received_sdp_munger_) {
691 received_sdp_munger_(desc->description());
692 }
693
694 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
695 // Set the RtpReceiverObserver after receivers are created.
696 ResetRtpReceiverObservers();
697 }
698
699 // Returns null on failure.
700 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
701 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
702 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
703 pc()->CreateOffer(observer, offer_answer_options_);
704 return WaitForDescriptionFromObserver(observer);
705 }
706
707 // Returns null on failure.
708 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
709 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
710 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
711 pc()->CreateAnswer(observer, offer_answer_options_);
712 return WaitForDescriptionFromObserver(observer);
713 }
714
715 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100716 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700717 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
718 if (!observer->result()) {
719 return nullptr;
720 }
721 auto description = observer->MoveDescription();
722 if (generated_sdp_munger_) {
723 generated_sdp_munger_(description->description());
724 }
725 return description;
726 }
727
728 // Setting the local description and sending the SDP message over the fake
729 // signaling channel are combined into the same method because the SDP
730 // message needs to be sent as soon as SetLocalDescription finishes, without
731 // waiting for the observer to be called. This ensures that ICE candidates
732 // don't outrace the description.
733 bool SetLocalDescriptionAndSendSdpMessage(
734 std::unique_ptr<SessionDescriptionInterface> desc) {
735 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
736 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100737 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800738 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700739 std::string sdp;
740 EXPECT_TRUE(desc->ToString(&sdp));
741 pc()->SetLocalDescription(observer, desc.release());
742 // As mentioned above, we need to send the message immediately after
743 // SetLocalDescription.
744 SendSdpMessage(type, sdp);
745 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
746 return true;
747 }
748
749 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
750 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
751 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100752 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700753 pc()->SetRemoteDescription(observer, desc.release());
754 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
755 return observer->result();
756 }
757
758 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
759 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800760 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700761 if (signaling_delay_ms_ == 0) {
762 RelaySdpMessageIfReceiverExists(type, msg);
763 } else {
764 invoker_.AsyncInvokeDelayed<void>(
765 RTC_FROM_HERE, rtc::Thread::Current(),
766 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
767 this, type, msg),
768 signaling_delay_ms_);
769 }
770 }
771
Steve Antona3a92c22017-12-07 10:27:41 -0800772 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700773 if (signaling_message_receiver_) {
774 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
775 }
776 }
777
778 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
779 // default).
780 void SendIceMessage(const std::string& sdp_mid,
781 int sdp_mline_index,
782 const std::string& msg) {
783 if (signaling_delay_ms_ == 0) {
784 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
785 } else {
786 invoker_.AsyncInvokeDelayed<void>(
787 RTC_FROM_HERE, rtc::Thread::Current(),
788 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
789 this, sdp_mid, sdp_mline_index, msg),
790 signaling_delay_ms_);
791 }
792 }
793
794 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
795 int sdp_mline_index,
796 const std::string& msg) {
797 if (signaling_message_receiver_) {
798 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
799 msg);
800 }
801 }
802
803 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800804 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
805 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700806 HandleIncomingOffer(msg);
807 } else {
808 HandleIncomingAnswer(msg);
809 }
810 }
811
812 void ReceiveIceMessage(const std::string& sdp_mid,
813 int sdp_mline_index,
814 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100815 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700816 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
817 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
818 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
819 }
820
821 // PeerConnectionObserver callbacks.
822 void OnSignalingChange(
823 webrtc::PeerConnectionInterface::SignalingState new_state) override {
824 EXPECT_EQ(pc()->signaling_state(), new_state);
825 }
Steve Anton15324772018-01-16 10:26:49 -0800826 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
827 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
828 streams) override {
829 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
830 rtc::scoped_refptr<VideoTrackInterface> video_track(
831 static_cast<VideoTrackInterface*>(receiver->track().get()));
832 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700833 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800834 fake_video_renderers_[video_track->id()] =
835 rtc::MakeUnique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700836 }
837 }
Steve Anton15324772018-01-16 10:26:49 -0800838 void OnRemoveTrack(
839 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
840 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
841 auto it = fake_video_renderers_.find(receiver->track()->id());
842 RTC_DCHECK(it != fake_video_renderers_.end());
843 fake_video_renderers_.erase(it);
844 }
845 }
deadbeef1dcb1642017-03-29 21:08:16 -0700846 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 {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100858 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700859
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 {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100870 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700871 data_channel_ = data_channel;
872 data_observer_.reset(new MockDataChannelObserver(data_channel));
873 }
874
deadbeef1dcb1642017-03-29 21:08:16 -0700875 std::string debug_name_;
876
877 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
878
879 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
880 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
881 peer_connection_factory_;
882
Steve Antonede9ca52017-10-16 13:04:27 -0700883 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700884 // Needed to keep track of number of frames sent.
885 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
886 // Needed to keep track of number of frames received.
887 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
888 fake_video_renderers_;
889 // Needed to ensure frames aren't received for removed tracks.
890 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
891 removed_fake_video_renderers_;
892 // Needed to keep track of number of frames received when external decoder
893 // used.
894 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
895 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
896 bool video_decoder_factory_enabled_ = false;
897
898 // For remote peer communication.
899 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
900 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700901 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -0700902
903 // Store references to the video capturers we've created, so that we can stop
904 // them, if required.
905 std::vector<cricket::FakeVideoCapturer*> video_capturers_;
906 // |local_video_renderer_| attached to the first created local video track.
907 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
908
909 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
910 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
911 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
912
913 rtc::scoped_refptr<DataChannelInterface> data_channel_;
914 std::unique_ptr<MockDataChannelObserver> data_observer_;
915
916 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
917
Steve Antonede9ca52017-10-16 13:04:27 -0700918 std::vector<PeerConnectionInterface::IceConnectionState>
919 ice_connection_state_history_;
920 std::vector<PeerConnectionInterface::IceGatheringState>
921 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700922
923 rtc::AsyncInvoker invoker_;
924
925 friend class PeerConnectionIntegrationTest;
926};
927
Elad Alon99c3fe52017-10-13 16:29:40 +0200928class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
929 public:
930 virtual ~MockRtcEventLogOutput() = default;
931 MOCK_CONST_METHOD0(IsActive, bool());
932 MOCK_METHOD1(Write, bool(const std::string&));
933};
934
deadbeef1dcb1642017-03-29 21:08:16 -0700935// Tests two PeerConnections connecting to each other end-to-end, using a
936// virtual network, fake A/V capture and fake encoder/decoders. The
937// PeerConnections share the threads/socket servers, but use separate versions
938// of everything else (including "PeerConnectionFactory"s).
939class PeerConnectionIntegrationTest : public testing::Test {
940 public:
941 PeerConnectionIntegrationTest()
deadbeef98e186c2017-05-16 18:00:06 -0700942 : ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -0700943 fss_(new rtc::FirewallSocketServer(ss_.get())),
944 network_thread_(new rtc::Thread(fss_.get())),
deadbeef1dcb1642017-03-29 21:08:16 -0700945 worker_thread_(rtc::Thread::Create()) {
946 RTC_CHECK(network_thread_->Start());
947 RTC_CHECK(worker_thread_->Start());
948 }
949
950 ~PeerConnectionIntegrationTest() {
951 if (caller_) {
952 caller_->set_signaling_message_receiver(nullptr);
953 }
954 if (callee_) {
955 callee_->set_signaling_message_receiver(nullptr);
956 }
957 }
958
959 bool SignalingStateStable() {
960 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
961 }
962
deadbeef71452802017-05-07 17:21:01 -0700963 bool DtlsConnected() {
964 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
965 // are connected. This is an important distinction. Once we have separate
966 // ICE and DTLS state, this check needs to use the DTLS state.
967 return (callee()->ice_connection_state() ==
968 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
969 callee()->ice_connection_state() ==
970 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
971 (caller()->ice_connection_state() ==
972 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
973 caller()->ice_connection_state() ==
974 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
975 }
976
deadbeef1dcb1642017-03-29 21:08:16 -0700977 bool CreatePeerConnectionWrappers() {
978 return CreatePeerConnectionWrappersWithConfig(
979 PeerConnectionInterface::RTCConfiguration(),
980 PeerConnectionInterface::RTCConfiguration());
981 }
982
983 bool CreatePeerConnectionWrappersWithConstraints(
984 MediaConstraintsInterface* caller_constraints,
985 MediaConstraintsInterface* callee_constraints) {
986 caller_.reset(PeerConnectionWrapper::CreateWithConstraints(
987 "Caller", caller_constraints, network_thread_.get(),
988 worker_thread_.get()));
989 callee_.reset(PeerConnectionWrapper::CreateWithConstraints(
990 "Callee", callee_constraints, network_thread_.get(),
991 worker_thread_.get()));
992 return caller_ && callee_;
993 }
994
995 bool CreatePeerConnectionWrappersWithConfig(
996 const PeerConnectionInterface::RTCConfiguration& caller_config,
997 const PeerConnectionInterface::RTCConfiguration& callee_config) {
998 caller_.reset(PeerConnectionWrapper::CreateWithConfig(
999 "Caller", caller_config, network_thread_.get(), worker_thread_.get()));
1000 callee_.reset(PeerConnectionWrapper::CreateWithConfig(
1001 "Callee", callee_config, network_thread_.get(), worker_thread_.get()));
1002 return caller_ && callee_;
1003 }
1004
1005 bool CreatePeerConnectionWrappersWithOptions(
1006 const PeerConnectionFactory::Options& caller_options,
1007 const PeerConnectionFactory::Options& callee_options) {
1008 caller_.reset(PeerConnectionWrapper::CreateWithOptions(
1009 "Caller", caller_options, network_thread_.get(), worker_thread_.get()));
1010 callee_.reset(PeerConnectionWrapper::CreateWithOptions(
1011 "Callee", callee_options, network_thread_.get(), worker_thread_.get()));
1012 return caller_ && callee_;
1013 }
1014
1015 PeerConnectionWrapper* CreatePeerConnectionWrapperWithAlternateKey() {
1016 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1017 new FakeRTCCertificateGenerator());
1018 cert_generator->use_alternate_key();
1019
1020 // Make sure the new client is using a different certificate.
1021 return PeerConnectionWrapper::CreateWithDtlsIdentityStore(
1022 "New Peer", std::move(cert_generator), network_thread_.get(),
1023 worker_thread_.get());
1024 }
1025
1026 // Once called, SDP blobs and ICE candidates will be automatically signaled
1027 // between PeerConnections.
1028 void ConnectFakeSignaling() {
1029 caller_->set_signaling_message_receiver(callee_.get());
1030 callee_->set_signaling_message_receiver(caller_.get());
1031 }
1032
Steve Antonede9ca52017-10-16 13:04:27 -07001033 // Once called, SDP blobs will be automatically signaled between
1034 // PeerConnections. Note that ICE candidates will not be signaled unless they
1035 // are in the exchanged SDP blobs.
1036 void ConnectFakeSignalingForSdpOnly() {
1037 ConnectFakeSignaling();
1038 SetSignalIceCandidates(false);
1039 }
1040
deadbeef1dcb1642017-03-29 21:08:16 -07001041 void SetSignalingDelayMs(int delay_ms) {
1042 caller_->set_signaling_delay_ms(delay_ms);
1043 callee_->set_signaling_delay_ms(delay_ms);
1044 }
1045
Steve Antonede9ca52017-10-16 13:04:27 -07001046 void SetSignalIceCandidates(bool signal) {
1047 caller_->set_signal_ice_candidates(signal);
1048 callee_->set_signal_ice_candidates(signal);
1049 }
1050
deadbeef1dcb1642017-03-29 21:08:16 -07001051 void EnableVideoDecoderFactory() {
1052 caller_->EnableVideoDecoderFactory();
1053 callee_->EnableVideoDecoderFactory();
1054 }
1055
1056 // Messages may get lost on the unreliable DataChannel, so we send multiple
1057 // times to avoid test flakiness.
1058 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1059 const std::string& data,
1060 int retries) {
1061 for (int i = 0; i < retries; ++i) {
1062 dc->Send(DataBuffer(data));
1063 }
1064 }
1065
1066 rtc::Thread* network_thread() { return network_thread_.get(); }
1067
1068 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1069
1070 PeerConnectionWrapper* caller() { return caller_.get(); }
1071
1072 // Set the |caller_| to the |wrapper| passed in and return the
1073 // original |caller_|.
1074 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1075 PeerConnectionWrapper* wrapper) {
1076 PeerConnectionWrapper* old = caller_.release();
1077 caller_.reset(wrapper);
1078 return old;
1079 }
1080
1081 PeerConnectionWrapper* callee() { return callee_.get(); }
1082
1083 // Set the |callee_| to the |wrapper| passed in and return the
1084 // original |callee_|.
1085 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1086 PeerConnectionWrapper* wrapper) {
1087 PeerConnectionWrapper* old = callee_.release();
1088 callee_.reset(wrapper);
1089 return old;
1090 }
1091
Steve Antonede9ca52017-10-16 13:04:27 -07001092 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1093
deadbeef1dcb1642017-03-29 21:08:16 -07001094 // Expects the provided number of new frames to be received within |wait_ms|.
1095 // "New frames" meaning that it waits for the current frame counts to
1096 // *increase* by the provided values. For video, uses
1097 // RecievedVideoFramesForEachTrack for the case of multiple video tracks
1098 // being received.
1099 void ExpectNewFramesReceivedWithWait(
1100 int expected_caller_received_audio_frames,
1101 int expected_caller_received_video_frames,
1102 int expected_callee_received_audio_frames,
1103 int expected_callee_received_video_frames,
1104 int wait_ms) {
1105 // Add current frame counts to the provided values, in order to wait for
1106 // the frame count to increase.
1107 expected_caller_received_audio_frames += caller()->audio_frames_received();
1108 expected_caller_received_video_frames +=
1109 caller()->min_video_frames_received_per_track();
1110 expected_callee_received_audio_frames += callee()->audio_frames_received();
1111 expected_callee_received_video_frames +=
1112 callee()->min_video_frames_received_per_track();
1113
1114 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
1115 expected_caller_received_audio_frames &&
1116 caller()->min_video_frames_received_per_track() >=
1117 expected_caller_received_video_frames &&
1118 callee()->audio_frames_received() >=
1119 expected_callee_received_audio_frames &&
1120 callee()->min_video_frames_received_per_track() >=
1121 expected_callee_received_video_frames,
1122 wait_ms);
1123
1124 // After the combined wait, do an "expect" for each individual count, to
1125 // print out a more detailed message upon failure.
1126 EXPECT_GE(caller()->audio_frames_received(),
1127 expected_caller_received_audio_frames);
1128 EXPECT_GE(caller()->min_video_frames_received_per_track(),
1129 expected_caller_received_video_frames);
1130 EXPECT_GE(callee()->audio_frames_received(),
1131 expected_callee_received_audio_frames);
1132 EXPECT_GE(callee()->min_video_frames_received_per_track(),
1133 expected_callee_received_video_frames);
1134 }
1135
1136 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1137 bool remote_gcm_enabled,
1138 int expected_cipher_suite) {
1139 PeerConnectionFactory::Options caller_options;
1140 caller_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled;
1141 PeerConnectionFactory::Options callee_options;
1142 callee_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled;
1143 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1144 callee_options));
1145 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1146 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1147 caller()->pc()->RegisterUMAObserver(caller_observer);
1148 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001149 caller()->AddAudioVideoTracks();
1150 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001151 caller()->CreateAndSetAndSignalOffer();
1152 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1153 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001154 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001155 EXPECT_EQ(
1156 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1157 expected_cipher_suite));
1158 caller()->pc()->RegisterUMAObserver(nullptr);
1159 }
1160
1161 private:
1162 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001163 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001164 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001165 // |network_thread_| and |worker_thread_| are used by both
1166 // |caller_| and |callee_| so they must be destroyed
1167 // later.
1168 std::unique_ptr<rtc::Thread> network_thread_;
1169 std::unique_ptr<rtc::Thread> worker_thread_;
1170 std::unique_ptr<PeerConnectionWrapper> caller_;
1171 std::unique_ptr<PeerConnectionWrapper> callee_;
1172};
1173
1174// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1175// includes testing that the callback is invoked if an observer is connected
1176// after the first packet has already been received.
1177TEST_F(PeerConnectionIntegrationTest,
1178 RtpReceiverObserverOnFirstPacketReceived) {
1179 ASSERT_TRUE(CreatePeerConnectionWrappers());
1180 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001181 caller()->AddAudioVideoTracks();
1182 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001183 // Start offer/answer exchange and wait for it to complete.
1184 caller()->CreateAndSetAndSignalOffer();
1185 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1186 // Should be one receiver each for audio/video.
1187 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1188 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1189 // Wait for all "first packet received" callbacks to be fired.
1190 EXPECT_TRUE_WAIT(
1191 std::all_of(caller()->rtp_receiver_observers().begin(),
1192 caller()->rtp_receiver_observers().end(),
1193 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1194 return o->first_packet_received();
1195 }),
1196 kMaxWaitForFramesMs);
1197 EXPECT_TRUE_WAIT(
1198 std::all_of(callee()->rtp_receiver_observers().begin(),
1199 callee()->rtp_receiver_observers().end(),
1200 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1201 return o->first_packet_received();
1202 }),
1203 kMaxWaitForFramesMs);
1204 // If new observers are set after the first packet was already received, the
1205 // callback should still be invoked.
1206 caller()->ResetRtpReceiverObservers();
1207 callee()->ResetRtpReceiverObservers();
1208 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1209 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1210 EXPECT_TRUE(
1211 std::all_of(caller()->rtp_receiver_observers().begin(),
1212 caller()->rtp_receiver_observers().end(),
1213 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1214 return o->first_packet_received();
1215 }));
1216 EXPECT_TRUE(
1217 std::all_of(callee()->rtp_receiver_observers().begin(),
1218 callee()->rtp_receiver_observers().end(),
1219 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1220 return o->first_packet_received();
1221 }));
1222}
1223
1224class DummyDtmfObserver : public DtmfSenderObserverInterface {
1225 public:
1226 DummyDtmfObserver() : completed_(false) {}
1227
1228 // Implements DtmfSenderObserverInterface.
1229 void OnToneChange(const std::string& tone) override {
1230 tones_.push_back(tone);
1231 if (tone.empty()) {
1232 completed_ = true;
1233 }
1234 }
1235
1236 const std::vector<std::string>& tones() const { return tones_; }
1237 bool completed() const { return completed_; }
1238
1239 private:
1240 bool completed_;
1241 std::vector<std::string> tones_;
1242};
1243
1244// Assumes |sender| already has an audio track added and the offer/answer
1245// exchange is done.
1246void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1247 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001248 // We should be able to get a DTMF sender from the local sender.
1249 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1250 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1251 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001252 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001253 dtmf_sender->RegisterObserver(&observer);
1254
1255 // Test the DtmfSender object just created.
1256 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1257 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1258
1259 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1260 std::vector<std::string> tones = {"1", "a", ""};
1261 EXPECT_EQ(tones, observer.tones());
1262 dtmf_sender->UnregisterObserver();
1263 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1264}
1265
1266// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1267// direction).
1268TEST_F(PeerConnectionIntegrationTest, DtmfSenderObserver) {
1269 ASSERT_TRUE(CreatePeerConnectionWrappers());
1270 ConnectFakeSignaling();
1271 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001272 caller()->AddAudioTrack();
1273 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001274 caller()->CreateAndSetAndSignalOffer();
1275 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001276 // DTLS must finish before the DTMF sender can be used reliably.
1277 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001278 TestDtmfFromSenderToReceiver(caller(), callee());
1279 TestDtmfFromSenderToReceiver(callee(), caller());
1280}
1281
1282// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1283// between two connections, using DTLS-SRTP.
1284TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
1285 ASSERT_TRUE(CreatePeerConnectionWrappers());
1286 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001287 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1288 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1289 caller()->pc()->RegisterUMAObserver(caller_observer);
1290
deadbeef1dcb1642017-03-29 21:08:16 -07001291 // Do normal offer/answer and wait for some frames to be received in each
1292 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001293 caller()->AddAudioVideoTracks();
1294 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001295 caller()->CreateAndSetAndSignalOffer();
1296 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1297 ExpectNewFramesReceivedWithWait(
1298 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1299 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1300 kMaxWaitForFramesMs);
Harald Alvestrand194939b2018-01-24 16:04:13 +01001301 EXPECT_LE(
1302 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1303 webrtc::kEnumCounterKeyProtocolDtls));
1304 EXPECT_EQ(
1305 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1306 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001307}
1308
1309// Uses SDES instead of DTLS for key agreement.
1310TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
1311 PeerConnectionInterface::RTCConfiguration sdes_config;
1312 sdes_config.enable_dtls_srtp.emplace(false);
1313 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1314 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001315 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1316 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1317 caller()->pc()->RegisterUMAObserver(caller_observer);
deadbeef1dcb1642017-03-29 21:08:16 -07001318
1319 // Do normal offer/answer and wait for some frames to be received in each
1320 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001321 caller()->AddAudioVideoTracks();
1322 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001323 caller()->CreateAndSetAndSignalOffer();
1324 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1325 ExpectNewFramesReceivedWithWait(
1326 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1327 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1328 kMaxWaitForFramesMs);
Harald Alvestrand194939b2018-01-24 16:04:13 +01001329 EXPECT_LE(
1330 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1331 webrtc::kEnumCounterKeyProtocolSdes));
1332 EXPECT_EQ(
1333 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1334 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001335}
1336
Steve Anton8c0f7a72017-10-03 10:03:10 -07001337// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1338// certificate once the DTLS handshake has finished.
1339TEST_F(PeerConnectionIntegrationTest,
1340 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1341 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1342 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1343 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1344 return pc->GetRemoteAudioSSLCertificate();
1345 };
1346
1347 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1348 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1349
1350 // Configure each side with a known certificate so they can be compared later.
1351 PeerConnectionInterface::RTCConfiguration caller_config;
1352 caller_config.enable_dtls_srtp.emplace(true);
1353 caller_config.certificates.push_back(caller_cert);
1354 PeerConnectionInterface::RTCConfiguration callee_config;
1355 callee_config.enable_dtls_srtp.emplace(true);
1356 callee_config.certificates.push_back(callee_cert);
1357 ASSERT_TRUE(
1358 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1359 ConnectFakeSignaling();
1360
1361 // When first initialized, there should not be a remote SSL certificate (and
1362 // calling this method should not crash).
1363 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1364 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
1365
Steve Anton15324772018-01-16 10:26:49 -08001366 caller()->AddAudioTrack();
1367 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001368 caller()->CreateAndSetAndSignalOffer();
1369 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1370 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1371
1372 // Once DTLS has been connected, each side should return the other's SSL
1373 // certificate when calling GetRemoteAudioSSLCertificate.
1374
1375 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1376 ASSERT_TRUE(caller_remote_cert);
1377 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1378 caller_remote_cert->ToPEMString());
1379
1380 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1381 ASSERT_TRUE(callee_remote_cert);
1382 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1383 callee_remote_cert->ToPEMString());
1384}
1385
deadbeef1dcb1642017-03-29 21:08:16 -07001386// This test sets up a call between two parties (using DTLS) and tests that we
1387// can get a video aspect ratio of 16:9.
1388TEST_F(PeerConnectionIntegrationTest, SendAndReceive16To9AspectRatio) {
1389 ASSERT_TRUE(CreatePeerConnectionWrappers());
1390 ConnectFakeSignaling();
1391
1392 // Add video tracks with 16:9 constraint.
1393 FakeConstraints constraints;
1394 double requested_ratio = 16.0 / 9;
1395 constraints.SetMandatoryMinAspectRatio(requested_ratio);
Steve Anton15324772018-01-16 10:26:49 -08001396 caller()->AddTrack(
1397 caller()->CreateLocalVideoTrackWithConstraints(constraints));
1398 callee()->AddTrack(
1399 callee()->CreateLocalVideoTrackWithConstraints(constraints));
deadbeef1dcb1642017-03-29 21:08:16 -07001400
1401 // Do normal offer/answer and wait for at least one frame to be received in
1402 // each direction.
1403 caller()->CreateAndSetAndSignalOffer();
1404 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1405 callee()->min_video_frames_received_per_track() > 0,
1406 kMaxWaitForFramesMs);
1407
1408 // Check rendered aspect ratio.
1409 EXPECT_EQ(requested_ratio, caller()->local_rendered_aspect_ratio());
1410 EXPECT_EQ(requested_ratio, caller()->rendered_aspect_ratio());
1411 EXPECT_EQ(requested_ratio, callee()->local_rendered_aspect_ratio());
1412 EXPECT_EQ(requested_ratio, callee()->rendered_aspect_ratio());
1413}
1414
1415// This test sets up a call between two parties with a source resolution of
1416// 1280x720 and verifies that a 16:9 aspect ratio is received.
1417TEST_F(PeerConnectionIntegrationTest,
1418 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1419 ASSERT_TRUE(CreatePeerConnectionWrappers());
1420 ConnectFakeSignaling();
1421
1422 // Similar to above test, but uses MandatoryMin[Width/Height] constraint
1423 // instead of aspect ratio constraint.
1424 FakeConstraints constraints;
1425 constraints.SetMandatoryMinWidth(1280);
1426 constraints.SetMandatoryMinHeight(720);
Steve Anton15324772018-01-16 10:26:49 -08001427 caller()->AddTrack(
1428 caller()->CreateLocalVideoTrackWithConstraints(constraints));
1429 callee()->AddTrack(
1430 callee()->CreateLocalVideoTrackWithConstraints(constraints));
deadbeef1dcb1642017-03-29 21:08:16 -07001431
1432 // Do normal offer/answer and wait for at least one frame to be received in
1433 // each direction.
1434 caller()->CreateAndSetAndSignalOffer();
1435 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1436 callee()->min_video_frames_received_per_track() > 0,
1437 kMaxWaitForFramesMs);
1438
1439 // Check rendered aspect ratio.
1440 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1441 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1442 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1443 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1444}
1445
1446// This test sets up an one-way call, with media only from caller to
1447// callee.
1448TEST_F(PeerConnectionIntegrationTest, OneWayMediaCall) {
1449 ASSERT_TRUE(CreatePeerConnectionWrappers());
1450 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001451 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001452 caller()->CreateAndSetAndSignalOffer();
1453 int caller_received_frames = 0;
1454 ExpectNewFramesReceivedWithWait(
1455 caller_received_frames, caller_received_frames,
1456 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1457 kMaxWaitForFramesMs);
1458}
1459
1460// This test sets up a audio call initially, with the callee rejecting video
1461// initially. Then later the callee decides to upgrade to audio/video, and
1462// initiates a new offer/answer exchange.
1463TEST_F(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
1464 ASSERT_TRUE(CreatePeerConnectionWrappers());
1465 ConnectFakeSignaling();
1466 // Initially, offer an audio/video stream from the caller, but refuse to
1467 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001468 caller()->AddAudioVideoTracks();
1469 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001470 PeerConnectionInterface::RTCOfferAnswerOptions options;
1471 options.offer_to_receive_video = 0;
1472 callee()->SetOfferAnswerOptions(options);
1473 // Do offer/answer and make sure audio is still received end-to-end.
1474 caller()->CreateAndSetAndSignalOffer();
1475 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1476 ExpectNewFramesReceivedWithWait(kDefaultExpectedAudioFrameCount, 0,
1477 kDefaultExpectedAudioFrameCount, 0,
1478 kMaxWaitForFramesMs);
1479 // Sanity check that the callee's description has a rejected video section.
1480 ASSERT_NE(nullptr, callee()->pc()->local_description());
1481 const ContentInfo* callee_video_content =
1482 GetFirstVideoContent(callee()->pc()->local_description()->description());
1483 ASSERT_NE(nullptr, callee_video_content);
1484 EXPECT_TRUE(callee_video_content->rejected);
1485 // Now negotiate with video and ensure negotiation succeeds, with video
1486 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001487 callee()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001488 options.offer_to_receive_video = 1;
1489 callee()->SetOfferAnswerOptions(options);
1490 callee()->CreateAndSetAndSignalOffer();
1491 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1492 // Expect additional audio frames to be received after the upgrade.
1493 ExpectNewFramesReceivedWithWait(
1494 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1495 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1496 kMaxWaitForFramesMs);
1497}
1498
deadbeef4389b4d2017-09-07 09:07:36 -07001499// Simpler than the above test; just add an audio track to an established
1500// video-only connection.
1501TEST_F(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
1502 ASSERT_TRUE(CreatePeerConnectionWrappers());
1503 ConnectFakeSignaling();
1504 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001505 caller()->AddVideoTrack();
1506 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001507 caller()->CreateAndSetAndSignalOffer();
1508 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1509 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001510 caller()->AddAudioTrack();
1511 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001512 caller()->CreateAndSetAndSignalOffer();
1513 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1514 // Ensure both audio and video frames are received end-to-end.
1515 ExpectNewFramesReceivedWithWait(
1516 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1517 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1518 kMaxWaitForFramesMs);
1519}
1520
deadbeef1dcb1642017-03-29 21:08:16 -07001521// This test sets up a call that's transferred to a new caller with a different
1522// DTLS fingerprint.
1523TEST_F(PeerConnectionIntegrationTest, CallTransferredForCallee) {
1524 ASSERT_TRUE(CreatePeerConnectionWrappers());
1525 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001526 caller()->AddAudioVideoTracks();
1527 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001528 caller()->CreateAndSetAndSignalOffer();
1529 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1530
1531 // Keep the original peer around which will still send packets to the
1532 // receiving client. These SRTP packets will be dropped.
1533 std::unique_ptr<PeerConnectionWrapper> original_peer(
1534 SetCallerPcWrapperAndReturnCurrent(
1535 CreatePeerConnectionWrapperWithAlternateKey()));
1536 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1537 // directly above.
1538 original_peer->pc()->Close();
1539
1540 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001541 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001542 caller()->CreateAndSetAndSignalOffer();
1543 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1544 // Wait for some additional frames to be transmitted end-to-end.
1545 ExpectNewFramesReceivedWithWait(
1546 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1547 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1548 kMaxWaitForFramesMs);
1549}
1550
1551// This test sets up a call that's transferred to a new callee with a different
1552// DTLS fingerprint.
1553TEST_F(PeerConnectionIntegrationTest, CallTransferredForCaller) {
1554 ASSERT_TRUE(CreatePeerConnectionWrappers());
1555 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001556 caller()->AddAudioVideoTracks();
1557 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001558 caller()->CreateAndSetAndSignalOffer();
1559 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1560
1561 // Keep the original peer around which will still send packets to the
1562 // receiving client. These SRTP packets will be dropped.
1563 std::unique_ptr<PeerConnectionWrapper> original_peer(
1564 SetCalleePcWrapperAndReturnCurrent(
1565 CreatePeerConnectionWrapperWithAlternateKey()));
1566 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1567 // directly above.
1568 original_peer->pc()->Close();
1569
1570 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001571 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001572 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1573 caller()->CreateAndSetAndSignalOffer();
1574 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1575 // Wait for some additional frames to be transmitted end-to-end.
1576 ExpectNewFramesReceivedWithWait(
1577 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1578 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1579 kMaxWaitForFramesMs);
1580}
1581
1582// This test sets up a non-bundled call and negotiates bundling at the same
1583// time as starting an ICE restart. When bundling is in effect in the restart,
1584// the DTLS-SRTP context should be successfully reset.
1585TEST_F(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
1586 ASSERT_TRUE(CreatePeerConnectionWrappers());
1587 ConnectFakeSignaling();
1588
Steve Anton15324772018-01-16 10:26:49 -08001589 caller()->AddAudioVideoTracks();
1590 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001591 // Remove the bundle group from the SDP received by the callee.
1592 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1593 desc->RemoveGroupByName("BUNDLE");
1594 });
1595 caller()->CreateAndSetAndSignalOffer();
1596 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1597 ExpectNewFramesReceivedWithWait(
1598 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1599 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1600 kMaxWaitForFramesMs);
1601
1602 // Now stop removing the BUNDLE group, and trigger an ICE restart.
1603 callee()->SetReceivedSdpMunger(nullptr);
1604 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1605 caller()->CreateAndSetAndSignalOffer();
1606 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1607
1608 // Expect additional frames to be received after the ICE restart.
1609 ExpectNewFramesReceivedWithWait(
1610 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1611 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1612 kMaxWaitForFramesMs);
1613}
1614
1615// Test CVO (Coordination of Video Orientation). If a video source is rotated
1616// and both peers support the CVO RTP header extension, the actual video frames
1617// don't need to be encoded in different resolutions, since the rotation is
1618// communicated through the RTP header extension.
1619TEST_F(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
1620 ASSERT_TRUE(CreatePeerConnectionWrappers());
1621 ConnectFakeSignaling();
1622 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001623 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001624 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001625 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001626 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1627
1628 // Wait for video frames to be received by both sides.
1629 caller()->CreateAndSetAndSignalOffer();
1630 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1631 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1632 callee()->min_video_frames_received_per_track() > 0,
1633 kMaxWaitForFramesMs);
1634
1635 // Ensure that the aspect ratio is unmodified.
1636 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1637 // not just assumed.
1638 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
1639 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
1640 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
1641 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
1642 // Ensure that the CVO bits were surfaced to the renderer.
1643 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
1644 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
1645}
1646
1647// Test that when the CVO extension isn't supported, video is rotated the
1648// old-fashioned way, by encoding rotated frames.
1649TEST_F(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
1650 ASSERT_TRUE(CreatePeerConnectionWrappers());
1651 ConnectFakeSignaling();
1652 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001653 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001654 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001655 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001656 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1657
1658 // Remove the CVO extension from the offered SDP.
1659 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1660 cricket::VideoContentDescription* video =
1661 GetFirstVideoContentDescription(desc);
1662 video->ClearRtpHeaderExtensions();
1663 });
1664 // Wait for video frames to be received by both sides.
1665 caller()->CreateAndSetAndSignalOffer();
1666 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1667 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1668 callee()->min_video_frames_received_per_track() > 0,
1669 kMaxWaitForFramesMs);
1670
1671 // Expect that the aspect ratio is inversed to account for the 90/270 degree
1672 // rotation.
1673 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1674 // not just assumed.
1675 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
1676 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
1677 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
1678 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
1679 // Expect that each endpoint is unaware of the rotation of the other endpoint.
1680 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
1681 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
1682}
1683
1684// TODO(deadbeef): The tests below rely on RTCOfferAnswerOptions to reject an
1685// m= section. When we implement Unified Plan SDP, the right way to do this
1686// would be by stopping an RtpTransceiver.
1687
1688// Test that if the answerer rejects the audio m= section, no audio is sent or
1689// received, but video still can be.
1690TEST_F(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
1691 ASSERT_TRUE(CreatePeerConnectionWrappers());
1692 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001693 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001694 // Only add video track for callee, and set offer_to_receive_audio to 0, so
1695 // it will reject the audio m= section completely.
1696 PeerConnectionInterface::RTCOfferAnswerOptions options;
1697 options.offer_to_receive_audio = 0;
1698 callee()->SetOfferAnswerOptions(options);
Steve Anton15324772018-01-16 10:26:49 -08001699 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07001700 // Do offer/answer and wait for successful end-to-end video frames.
1701 caller()->CreateAndSetAndSignalOffer();
1702 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1703 ExpectNewFramesReceivedWithWait(0, kDefaultExpectedVideoFrameCount, 0,
1704 kDefaultExpectedVideoFrameCount,
1705 kMaxWaitForFramesMs);
1706 // Shouldn't have received audio frames at any point.
1707 EXPECT_EQ(0, caller()->audio_frames_received());
1708 EXPECT_EQ(0, callee()->audio_frames_received());
1709 // Sanity check that the callee's description has a rejected audio section.
1710 ASSERT_NE(nullptr, callee()->pc()->local_description());
1711 const ContentInfo* callee_audio_content =
1712 GetFirstAudioContent(callee()->pc()->local_description()->description());
1713 ASSERT_NE(nullptr, callee_audio_content);
1714 EXPECT_TRUE(callee_audio_content->rejected);
1715}
1716
1717// Test that if the answerer rejects the video m= section, no video is sent or
1718// received, but audio still can be.
1719TEST_F(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
1720 ASSERT_TRUE(CreatePeerConnectionWrappers());
1721 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001722 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001723 // Only add audio track for callee, and set offer_to_receive_video to 0, so
1724 // it will reject the video m= section completely.
1725 PeerConnectionInterface::RTCOfferAnswerOptions options;
1726 options.offer_to_receive_video = 0;
1727 callee()->SetOfferAnswerOptions(options);
Steve Anton15324772018-01-16 10:26:49 -08001728 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07001729 // Do offer/answer and wait for successful end-to-end audio frames.
1730 caller()->CreateAndSetAndSignalOffer();
1731 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1732 ExpectNewFramesReceivedWithWait(kDefaultExpectedAudioFrameCount, 0,
1733 kDefaultExpectedAudioFrameCount, 0,
1734 kMaxWaitForFramesMs);
1735 // Shouldn't have received video frames at any point.
1736 EXPECT_EQ(0, caller()->total_video_frames_received());
1737 EXPECT_EQ(0, callee()->total_video_frames_received());
1738 // Sanity check that the callee's description has a rejected video section.
1739 ASSERT_NE(nullptr, callee()->pc()->local_description());
1740 const ContentInfo* callee_video_content =
1741 GetFirstVideoContent(callee()->pc()->local_description()->description());
1742 ASSERT_NE(nullptr, callee_video_content);
1743 EXPECT_TRUE(callee_video_content->rejected);
1744}
1745
1746// Test that if the answerer rejects both audio and video m= sections, nothing
1747// bad happens.
1748// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
1749// test anything but the fact that negotiation succeeds, which doesn't mean
1750// much.
1751TEST_F(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
1752 ASSERT_TRUE(CreatePeerConnectionWrappers());
1753 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001754 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001755 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
1756 // will reject both audio and video m= sections.
1757 PeerConnectionInterface::RTCOfferAnswerOptions options;
1758 options.offer_to_receive_audio = 0;
1759 options.offer_to_receive_video = 0;
1760 callee()->SetOfferAnswerOptions(options);
1761 // Do offer/answer and wait for stable signaling state.
1762 caller()->CreateAndSetAndSignalOffer();
1763 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1764 // Sanity check that the callee's description has rejected m= sections.
1765 ASSERT_NE(nullptr, callee()->pc()->local_description());
1766 const ContentInfo* callee_audio_content =
1767 GetFirstAudioContent(callee()->pc()->local_description()->description());
1768 ASSERT_NE(nullptr, callee_audio_content);
1769 EXPECT_TRUE(callee_audio_content->rejected);
1770 const ContentInfo* callee_video_content =
1771 GetFirstVideoContent(callee()->pc()->local_description()->description());
1772 ASSERT_NE(nullptr, callee_video_content);
1773 EXPECT_TRUE(callee_video_content->rejected);
1774}
1775
1776// This test sets up an audio and video call between two parties. After the
1777// call runs for a while, the caller sends an updated offer with video being
1778// rejected. Once the re-negotiation is done, the video flow should stop and
1779// the audio flow should continue.
1780TEST_F(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
1781 ASSERT_TRUE(CreatePeerConnectionWrappers());
1782 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001783 caller()->AddAudioVideoTracks();
1784 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001785 caller()->CreateAndSetAndSignalOffer();
1786 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1787 ExpectNewFramesReceivedWithWait(
1788 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1789 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1790 kMaxWaitForFramesMs);
1791
1792 // Renegotiate, rejecting the video m= section.
1793 // TODO(deadbeef): When an RtpTransceiver API is available, use that to
1794 // reject the video m= section.
1795 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
1796 for (cricket::ContentInfo& content : description->contents()) {
1797 if (cricket::IsVideoContent(&content)) {
1798 content.rejected = true;
1799 }
1800 }
1801 });
1802 caller()->CreateAndSetAndSignalOffer();
1803 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
1804
1805 // Sanity check that the caller's description has a rejected video section.
1806 ASSERT_NE(nullptr, caller()->pc()->local_description());
1807 const ContentInfo* caller_video_content =
1808 GetFirstVideoContent(caller()->pc()->local_description()->description());
1809 ASSERT_NE(nullptr, caller_video_content);
1810 EXPECT_TRUE(caller_video_content->rejected);
1811
1812 int caller_video_received = caller()->total_video_frames_received();
1813 int callee_video_received = callee()->total_video_frames_received();
1814
1815 // Wait for some additional audio frames to be received.
1816 ExpectNewFramesReceivedWithWait(kDefaultExpectedAudioFrameCount, 0,
1817 kDefaultExpectedAudioFrameCount, 0,
1818 kMaxWaitForFramesMs);
1819
1820 // During this time, we shouldn't have received any additional video frames
1821 // for the rejected video tracks.
1822 EXPECT_EQ(caller_video_received, caller()->total_video_frames_received());
1823 EXPECT_EQ(callee_video_received, callee()->total_video_frames_received());
1824}
1825
1826// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
1827// is needed to support legacy endpoints.
1828// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
1829// add a test for an end-to-end test without MID signaling either (basically,
1830// the minimum acceptable SDP).
1831TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
1832 ASSERT_TRUE(CreatePeerConnectionWrappers());
1833 ConnectFakeSignaling();
1834 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08001835 caller()->AddAudioVideoTracks();
1836 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07001837 // Remove SSRCs and MSIDs from the received offer SDP.
1838 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07001839 caller()->CreateAndSetAndSignalOffer();
1840 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1841 ExpectNewFramesReceivedWithWait(
1842 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1843 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1844 kMaxWaitForFramesMs);
1845}
1846
1847// Test that if two video tracks are sent (from caller to callee, in this test),
1848// they're transmitted correctly end-to-end.
1849TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
1850 ASSERT_TRUE(CreatePeerConnectionWrappers());
1851 ConnectFakeSignaling();
1852 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08001853 caller()->AddAudioVideoTracks();
1854 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001855 caller()->CreateAndSetAndSignalOffer();
1856 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08001857 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001858 int expected_callee_received_frames = kDefaultExpectedVideoFrameCount;
1859 ExpectNewFramesReceivedWithWait(0, 0, 0, expected_callee_received_frames,
1860 kMaxWaitForFramesMs);
1861}
1862
1863static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
1864 bool first = true;
1865 for (cricket::ContentInfo& content : desc->contents()) {
1866 if (first) {
1867 first = false;
1868 continue;
1869 }
1870 content.bundle_only = true;
1871 }
1872 first = true;
1873 for (cricket::TransportInfo& transport : desc->transport_infos()) {
1874 if (first) {
1875 first = false;
1876 continue;
1877 }
1878 transport.description.ice_ufrag.clear();
1879 transport.description.ice_pwd.clear();
1880 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
1881 transport.description.identity_fingerprint.reset(nullptr);
1882 }
1883}
1884
1885// Test that if applying a true "max bundle" offer, which uses ports of 0,
1886// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
1887// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
1888// successfully and media flows.
1889// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
1890// TODO(deadbeef): Won't need this test once we start generating actual
1891// standards-compliant SDP.
1892TEST_F(PeerConnectionIntegrationTest,
1893 EndToEndCallWithSpecCompliantMaxBundleOffer) {
1894 ASSERT_TRUE(CreatePeerConnectionWrappers());
1895 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001896 caller()->AddAudioVideoTracks();
1897 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001898 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
1899 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
1900 // but the first m= section.
1901 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
1902 caller()->CreateAndSetAndSignalOffer();
1903 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1904 ExpectNewFramesReceivedWithWait(
1905 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1906 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1907 kMaxWaitForFramesMs);
1908}
1909
1910// Test that we can receive the audio output level from a remote audio track.
1911// TODO(deadbeef): Use a fake audio source and verify that the output level is
1912// exactly what the source on the other side was configured with.
deadbeefd8ad7882017-04-18 16:01:17 -07001913TEST_F(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07001914 ASSERT_TRUE(CreatePeerConnectionWrappers());
1915 ConnectFakeSignaling();
1916 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08001917 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001918 caller()->CreateAndSetAndSignalOffer();
1919 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1920
1921 // Get the audio output level stats. Note that the level is not available
1922 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07001923 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07001924 kMaxWaitForFramesMs);
1925}
1926
1927// Test that an audio input level is reported.
1928// TODO(deadbeef): Use a fake audio source and verify that the input level is
1929// exactly what the source was configured with.
deadbeefd8ad7882017-04-18 16:01:17 -07001930TEST_F(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07001931 ASSERT_TRUE(CreatePeerConnectionWrappers());
1932 ConnectFakeSignaling();
1933 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08001934 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001935 caller()->CreateAndSetAndSignalOffer();
1936 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1937
1938 // Get the audio input level stats. The level should be available very
1939 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07001940 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07001941 kMaxWaitForStatsMs);
1942}
1943
1944// Test that we can get incoming byte counts from both audio and video tracks.
deadbeefd8ad7882017-04-18 16:01:17 -07001945TEST_F(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07001946 ASSERT_TRUE(CreatePeerConnectionWrappers());
1947 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001948 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001949 // Do offer/answer, wait for the callee to receive some frames.
1950 caller()->CreateAndSetAndSignalOffer();
1951 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1952 int expected_caller_received_frames = 0;
1953 ExpectNewFramesReceivedWithWait(
1954 expected_caller_received_frames, expected_caller_received_frames,
1955 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1956 kMaxWaitForFramesMs);
1957
1958 // Get a handle to the remote tracks created, so they can be used as GetStats
1959 // filters.
Steve Anton15324772018-01-16 10:26:49 -08001960 for (auto receiver : callee()->pc()->GetReceivers()) {
1961 // We received frames, so we definitely should have nonzero "received bytes"
1962 // stats at this point.
1963 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
1964 0);
1965 }
deadbeef1dcb1642017-03-29 21:08:16 -07001966}
1967
1968// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeefd8ad7882017-04-18 16:01:17 -07001969TEST_F(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07001970 ASSERT_TRUE(CreatePeerConnectionWrappers());
1971 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001972 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001973 auto audio_track = caller()->CreateLocalAudioTrack();
1974 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08001975 caller()->AddTrack(audio_track);
1976 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07001977 // Do offer/answer, wait for the callee to receive some frames.
1978 caller()->CreateAndSetAndSignalOffer();
1979 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1980 int expected_caller_received_frames = 0;
1981 ExpectNewFramesReceivedWithWait(
1982 expected_caller_received_frames, expected_caller_received_frames,
1983 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1984 kMaxWaitForFramesMs);
1985
1986 // The callee received frames, so we definitely should have nonzero "sent
1987 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07001988 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
1989 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
1990}
1991
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02001992// Test that we can get capture start ntp time.
1993TEST_F(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
1994 ASSERT_TRUE(CreatePeerConnectionWrappers());
1995 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001996 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02001997
Steve Anton15324772018-01-16 10:26:49 -08001998 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02001999
2000 // Do offer/answer, wait for the callee to receive some frames.
2001 caller()->CreateAndSetAndSignalOffer();
2002 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2003
2004 // Get the remote audio track created on the receiver, so they can be used as
2005 // GetStats filters.
2006 StreamCollectionInterface* remote_streams = callee()->remote_streams();
2007 ASSERT_EQ(1u, remote_streams->count());
2008 ASSERT_EQ(1u, remote_streams->at(0)->GetAudioTracks().size());
2009 MediaStreamTrackInterface* remote_audio_track =
2010 remote_streams->at(0)->GetAudioTracks()[0];
2011
2012 // Get the audio output level stats. Note that the level is not available
2013 // until an RTCP packet has been received.
2014 EXPECT_TRUE_WAIT(callee()->OldGetStatsForTrack(remote_audio_track)->
2015 CaptureStartNtpTime() > 0, 2 * kMaxWaitForFramesMs);
2016}
2017
deadbeefd8ad7882017-04-18 16:01:17 -07002018// Test that we can get stats (using the new stats implemnetation) for
2019// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2020// SDP.
2021TEST_F(PeerConnectionIntegrationTest,
2022 GetStatsForUnsignaledStreamWithNewStatsApi) {
2023 ASSERT_TRUE(CreatePeerConnectionWrappers());
2024 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002025 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002026 // Remove SSRCs and MSIDs from the received offer SDP.
2027 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2028 caller()->CreateAndSetAndSignalOffer();
2029 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2030 // Wait for one audio frame to be received by the callee.
2031 ExpectNewFramesReceivedWithWait(0, 0, 1, 0, kMaxWaitForFramesMs);
2032
2033 // We received a frame, so we should have nonzero "bytes received" stats for
2034 // the unsignaled stream, if stats are working for it.
2035 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2036 callee()->NewGetStats();
2037 ASSERT_NE(nullptr, report);
2038 auto inbound_stream_stats =
2039 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2040 ASSERT_EQ(1U, inbound_stream_stats.size());
2041 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2042 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002043 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2044}
2045
2046// Test that we can successfully get the media related stats (audio level
2047// etc.) for the unsignaled stream.
2048TEST_F(PeerConnectionIntegrationTest,
2049 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2050 ASSERT_TRUE(CreatePeerConnectionWrappers());
2051 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002052 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002053 // Remove SSRCs and MSIDs from the received offer SDP.
2054 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2055 caller()->CreateAndSetAndSignalOffer();
2056 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2057 // Wait for one audio frame to be received by the callee.
2058 ExpectNewFramesReceivedWithWait(0, 0, 1, 1, kMaxWaitForFramesMs);
2059
2060 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2061 callee()->NewGetStats();
2062 ASSERT_NE(nullptr, report);
2063
2064 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2065 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2066 ASSERT_GE(audio_index, 0);
2067 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002068}
2069
deadbeef4e2deab2017-09-20 13:56:21 -07002070// Helper for test below.
2071void ModifySsrcs(cricket::SessionDescription* desc) {
2072 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002073 for (cricket::StreamParams& stream :
2074 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002075 for (uint32_t& ssrc : stream.ssrcs) {
2076 ssrc = rtc::CreateRandomId();
2077 }
2078 }
2079 }
2080}
2081
2082// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2083// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2084// This should result in two "RTCInboundRTPStreamStats", but only one
2085// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2086// being reset to 0 once the SSRC change occurs.
2087//
2088// Regression test for this bug:
2089// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2090//
2091// The bug causes the track stats to only represent one of the two streams:
2092// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2093// that the track stat counters would reset to 0 when the new stream is
2094// received, and a 50% chance that they'll stop updating (while
2095// "concealed_samples" continues increasing, due to silence being generated for
2096// the inactive stream).
2097TEST_F(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002098 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002099 ASSERT_TRUE(CreatePeerConnectionWrappers());
2100 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002101 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002102 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2103 // that doesn't signal SSRCs (from the callee's perspective).
2104 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2105 caller()->CreateAndSetAndSignalOffer();
2106 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2107 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
2108 ExpectNewFramesReceivedWithWait(0, 0, 25, 0, kMaxWaitForFramesMs);
2109
2110 // Some audio frames were received, so we should have nonzero "samples
2111 // received" for the track.
2112 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2113 callee()->NewGetStats();
2114 ASSERT_NE(nullptr, report);
2115 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2116 ASSERT_EQ(1U, track_stats.size());
2117 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2118 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2119 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2120
2121 // Create a new offer and munge it to cause the caller to use a new SSRC.
2122 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2123 caller()->CreateAndSetAndSignalOffer();
2124 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2125 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2126 // SSRC.
2127 ExpectNewFramesReceivedWithWait(0, 0, 25, 0, kMaxWaitForFramesMs);
2128
2129 report = callee()->NewGetStats();
2130 ASSERT_NE(nullptr, report);
2131 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2132 ASSERT_EQ(1U, track_stats.size());
2133 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2134 // The "total samples received" stat should only be greater than it was
2135 // before.
2136 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2137 // Right now, the new SSRC will cause the counters to reset to 0.
2138 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2139
2140 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002141 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002142 // good sign that we're seeing stats from the old stream that's no longer
2143 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002144 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002145 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2146 EXPECT_LT(*track_stats[0]->concealed_samples,
2147 *track_stats[0]->total_samples_received *
2148 kAcceptableConcealedSamplesPercentage);
2149
2150 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2151 // sanity check that the SSRC really changed.
2152 // TODO(deadbeef): This isn't working right now, because we're not returning
2153 // *any* stats for the inactive stream. Uncomment when the bug is completely
2154 // fixed.
2155 // auto inbound_stream_stats =
2156 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2157 // ASSERT_EQ(2U, inbound_stream_stats.size());
2158}
2159
deadbeef1dcb1642017-03-29 21:08:16 -07002160// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
2161TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
2162 PeerConnectionFactory::Options dtls_10_options;
2163 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2164 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2165 dtls_10_options));
2166 ConnectFakeSignaling();
2167 // Do normal offer/answer and wait for some frames to be received in each
2168 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002169 caller()->AddAudioVideoTracks();
2170 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002171 caller()->CreateAndSetAndSignalOffer();
2172 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2173 ExpectNewFramesReceivedWithWait(
2174 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2175 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2176 kMaxWaitForFramesMs);
2177}
2178
2179// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
2180TEST_F(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
2181 PeerConnectionFactory::Options dtls_10_options;
2182 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2183 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2184 dtls_10_options));
2185 ConnectFakeSignaling();
2186 // Register UMA observer before signaling begins.
2187 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2188 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2189 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002190 caller()->AddAudioVideoTracks();
2191 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002192 caller()->CreateAndSetAndSignalOffer();
2193 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2194 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002195 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002196 kDefaultTimeout);
2197 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002198 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002199 EXPECT_EQ(1,
2200 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2201 kDefaultSrtpCryptoSuite));
2202}
2203
2204// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
2205TEST_F(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
2206 PeerConnectionFactory::Options dtls_12_options;
2207 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2208 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2209 dtls_12_options));
2210 ConnectFakeSignaling();
2211 // Register UMA observer before signaling begins.
2212 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2213 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2214 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002215 caller()->AddAudioVideoTracks();
2216 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002217 caller()->CreateAndSetAndSignalOffer();
2218 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2219 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002220 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002221 kDefaultTimeout);
2222 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002223 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002224 EXPECT_EQ(1,
2225 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2226 kDefaultSrtpCryptoSuite));
2227}
2228
2229// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2230// callee only supports 1.0.
2231TEST_F(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
2232 PeerConnectionFactory::Options caller_options;
2233 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2234 PeerConnectionFactory::Options callee_options;
2235 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2236 ASSERT_TRUE(
2237 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2238 ConnectFakeSignaling();
2239 // Do normal offer/answer and wait for some frames to be received in each
2240 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002241 caller()->AddAudioVideoTracks();
2242 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002243 caller()->CreateAndSetAndSignalOffer();
2244 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2245 ExpectNewFramesReceivedWithWait(
2246 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2247 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2248 kMaxWaitForFramesMs);
2249}
2250
2251// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2252// callee supports 1.2.
2253TEST_F(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
2254 PeerConnectionFactory::Options caller_options;
2255 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2256 PeerConnectionFactory::Options callee_options;
2257 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2258 ASSERT_TRUE(
2259 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2260 ConnectFakeSignaling();
2261 // Do normal offer/answer and wait for some frames to be received in each
2262 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002263 caller()->AddAudioVideoTracks();
2264 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002265 caller()->CreateAndSetAndSignalOffer();
2266 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2267 ExpectNewFramesReceivedWithWait(
2268 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2269 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2270 kMaxWaitForFramesMs);
2271}
2272
2273// Test that a non-GCM cipher is used if both sides only support non-GCM.
2274TEST_F(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
2275 bool local_gcm_enabled = false;
2276 bool remote_gcm_enabled = false;
2277 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2278 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2279 expected_cipher_suite);
2280}
2281
2282// Test that a GCM cipher is used if both ends support it.
2283TEST_F(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
2284 bool local_gcm_enabled = true;
2285 bool remote_gcm_enabled = true;
2286 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2287 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2288 expected_cipher_suite);
2289}
2290
2291// Test that GCM isn't used if only the offerer supports it.
2292TEST_F(PeerConnectionIntegrationTest,
2293 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2294 bool local_gcm_enabled = true;
2295 bool remote_gcm_enabled = false;
2296 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2297 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2298 expected_cipher_suite);
2299}
2300
2301// Test that GCM isn't used if only the answerer supports it.
2302TEST_F(PeerConnectionIntegrationTest,
2303 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2304 bool local_gcm_enabled = false;
2305 bool remote_gcm_enabled = true;
2306 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2307 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2308 expected_cipher_suite);
2309}
2310
deadbeef7914b8c2017-04-21 03:23:33 -07002311// Verify that media can be transmitted end-to-end when GCM crypto suites are
2312// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2313// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2314// works with it.
2315TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
2316 PeerConnectionFactory::Options gcm_options;
2317 gcm_options.crypto_options.enable_gcm_crypto_suites = true;
2318 ASSERT_TRUE(
2319 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2320 ConnectFakeSignaling();
2321 // Do normal offer/answer and wait for some frames to be received in each
2322 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002323 caller()->AddAudioVideoTracks();
2324 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002325 caller()->CreateAndSetAndSignalOffer();
2326 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2327 ExpectNewFramesReceivedWithWait(
2328 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2329 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2330 kMaxWaitForFramesMs);
2331}
2332
deadbeef1dcb1642017-03-29 21:08:16 -07002333// This test sets up a call between two parties with audio, video and an RTP
2334// data channel.
2335TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
2336 FakeConstraints setup_constraints;
2337 setup_constraints.SetAllowRtpDataChannels();
2338 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2339 &setup_constraints));
2340 ConnectFakeSignaling();
2341 // Expect that data channel created on caller side will show up for callee as
2342 // well.
2343 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002344 caller()->AddAudioVideoTracks();
2345 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002346 caller()->CreateAndSetAndSignalOffer();
2347 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2348 // Ensure the existence of the RTP data channel didn't impede audio/video.
2349 ExpectNewFramesReceivedWithWait(
2350 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2351 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2352 kMaxWaitForFramesMs);
2353 ASSERT_NE(nullptr, caller()->data_channel());
2354 ASSERT_NE(nullptr, callee()->data_channel());
2355 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2356 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2357
2358 // Ensure data can be sent in both directions.
2359 std::string data = "hello world";
2360 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2361 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2362 kDefaultTimeout);
2363 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2364 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2365 kDefaultTimeout);
2366}
2367
2368// Ensure that an RTP data channel is signaled as closed for the caller when
2369// the callee rejects it in a subsequent offer.
2370TEST_F(PeerConnectionIntegrationTest,
2371 RtpDataChannelSignaledClosedInCalleeOffer) {
2372 // Same procedure as above test.
2373 FakeConstraints setup_constraints;
2374 setup_constraints.SetAllowRtpDataChannels();
2375 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2376 &setup_constraints));
2377 ConnectFakeSignaling();
2378 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002379 caller()->AddAudioVideoTracks();
2380 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002381 caller()->CreateAndSetAndSignalOffer();
2382 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2383 ASSERT_NE(nullptr, caller()->data_channel());
2384 ASSERT_NE(nullptr, callee()->data_channel());
2385 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2386 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2387
2388 // Close the data channel on the callee, and do an updated offer/answer.
2389 callee()->data_channel()->Close();
2390 callee()->CreateAndSetAndSignalOffer();
2391 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2392 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2393 EXPECT_FALSE(callee()->data_observer()->IsOpen());
2394}
2395
2396// Tests that data is buffered in an RTP data channel until an observer is
2397// registered for it.
2398//
2399// NOTE: RTP data channels can receive data before the underlying
2400// transport has detected that a channel is writable and thus data can be
2401// received before the data channel state changes to open. That is hard to test
2402// but the same buffering is expected to be used in that case.
2403TEST_F(PeerConnectionIntegrationTest,
2404 DataBufferedUntilRtpDataChannelObserverRegistered) {
2405 // Use fake clock and simulated network delay so that we predictably can wait
2406 // until an SCTP message has been delivered without "sleep()"ing.
2407 rtc::ScopedFakeClock fake_clock;
2408 // Some things use a time of "0" as a special value, so we need to start out
2409 // the fake clock at a nonzero time.
2410 // TODO(deadbeef): Fix this.
2411 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
2412 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
2413 virtual_socket_server()->UpdateDelayDistribution();
2414
2415 FakeConstraints constraints;
2416 constraints.SetAllowRtpDataChannels();
2417 ASSERT_TRUE(
2418 CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints));
2419 ConnectFakeSignaling();
2420 caller()->CreateDataChannel();
2421 caller()->CreateAndSetAndSignalOffer();
2422 ASSERT_TRUE(caller()->data_channel() != nullptr);
2423 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
2424 kDefaultTimeout, fake_clock);
2425 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
2426 kDefaultTimeout, fake_clock);
2427 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
2428 callee()->data_channel()->state(), kDefaultTimeout,
2429 fake_clock);
2430
2431 // Unregister the observer which is normally automatically registered.
2432 callee()->data_channel()->UnregisterObserver();
2433 // Send data and advance fake clock until it should have been received.
2434 std::string data = "hello world";
2435 caller()->data_channel()->Send(DataBuffer(data));
2436 SIMULATED_WAIT(false, 50, fake_clock);
2437
2438 // Attach data channel and expect data to be received immediately. Note that
2439 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
2440 // further, but data can be received even if the callback is asynchronous.
2441 MockDataChannelObserver new_observer(callee()->data_channel());
2442 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
2443 fake_clock);
2444}
2445
2446// This test sets up a call between two parties with audio, video and but only
2447// the caller client supports RTP data channels.
2448TEST_F(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
2449 FakeConstraints setup_constraints_1;
2450 setup_constraints_1.SetAllowRtpDataChannels();
2451 // Must disable DTLS to make negotiation succeed.
2452 setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2453 false);
2454 FakeConstraints setup_constraints_2;
2455 setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2456 false);
2457 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(
2458 &setup_constraints_1, &setup_constraints_2));
2459 ConnectFakeSignaling();
2460 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002461 caller()->AddAudioVideoTracks();
2462 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002463 caller()->CreateAndSetAndSignalOffer();
2464 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2465 // The caller should still have a data channel, but it should be closed, and
2466 // one should ever have been created for the callee.
2467 EXPECT_TRUE(caller()->data_channel() != nullptr);
2468 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2469 EXPECT_EQ(nullptr, callee()->data_channel());
2470}
2471
2472// This test sets up a call between two parties with audio, and video. When
2473// audio and video is setup and flowing, an RTP data channel is negotiated.
2474TEST_F(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
2475 FakeConstraints setup_constraints;
2476 setup_constraints.SetAllowRtpDataChannels();
2477 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2478 &setup_constraints));
2479 ConnectFakeSignaling();
2480 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08002481 caller()->AddAudioVideoTracks();
2482 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002483 caller()->CreateAndSetAndSignalOffer();
2484 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2485 // Create data channel and do new offer and answer.
2486 caller()->CreateDataChannel();
2487 caller()->CreateAndSetAndSignalOffer();
2488 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2489 ASSERT_NE(nullptr, caller()->data_channel());
2490 ASSERT_NE(nullptr, callee()->data_channel());
2491 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2492 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2493 // Ensure data can be sent in both directions.
2494 std::string data = "hello world";
2495 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2496 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2497 kDefaultTimeout);
2498 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2499 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2500 kDefaultTimeout);
2501}
2502
2503#ifdef HAVE_SCTP
2504
2505// This test sets up a call between two parties with audio, video and an SCTP
2506// data channel.
2507TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
2508 ASSERT_TRUE(CreatePeerConnectionWrappers());
2509 ConnectFakeSignaling();
2510 // Expect that data channel created on caller side will show up for callee as
2511 // well.
2512 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002513 caller()->AddAudioVideoTracks();
2514 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002515 caller()->CreateAndSetAndSignalOffer();
2516 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2517 // Ensure the existence of the SCTP data channel didn't impede audio/video.
2518 ExpectNewFramesReceivedWithWait(
2519 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2520 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2521 kMaxWaitForFramesMs);
2522 // Caller data channel should already exist (it created one). Callee data
2523 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2524 ASSERT_NE(nullptr, caller()->data_channel());
2525 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2526 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2527 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2528
2529 // Ensure data can be sent in both directions.
2530 std::string data = "hello world";
2531 caller()->data_channel()->Send(DataBuffer(data));
2532 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2533 kDefaultTimeout);
2534 callee()->data_channel()->Send(DataBuffer(data));
2535 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2536 kDefaultTimeout);
2537}
2538
2539// Ensure that when the callee closes an SCTP data channel, the closing
2540// procedure results in the data channel being closed for the caller as well.
2541TEST_F(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
2542 // Same procedure as above test.
2543 ASSERT_TRUE(CreatePeerConnectionWrappers());
2544 ConnectFakeSignaling();
2545 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002546 caller()->AddAudioVideoTracks();
2547 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002548 caller()->CreateAndSetAndSignalOffer();
2549 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2550 ASSERT_NE(nullptr, caller()->data_channel());
2551 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2552 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2553 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2554
2555 // Close the data channel on the callee side, and wait for it to reach the
2556 // "closed" state on both sides.
2557 callee()->data_channel()->Close();
2558 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
2559 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
2560}
2561
Steve Antonda6c0952017-10-23 11:41:54 -07002562TEST_F(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
2563 ASSERT_TRUE(CreatePeerConnectionWrappers());
2564 ConnectFakeSignaling();
2565 webrtc::DataChannelInit init;
2566 init.id = 53;
2567 init.maxRetransmits = 52;
2568 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08002569 caller()->AddAudioVideoTracks();
2570 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07002571 caller()->CreateAndSetAndSignalOffer();
2572 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07002573 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2574 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07002575 EXPECT_EQ(init.id, callee()->data_channel()->id());
2576 EXPECT_EQ("data-channel", callee()->data_channel()->label());
2577 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
2578 EXPECT_FALSE(callee()->data_channel()->negotiated());
2579}
2580
deadbeef1dcb1642017-03-29 21:08:16 -07002581// Test usrsctp's ability to process unordered data stream, where data actually
2582// arrives out of order using simulated delays. Previously there have been some
2583// bugs in this area.
2584TEST_F(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
2585 // Introduce random network delays.
2586 // Otherwise it's not a true "unordered" test.
2587 virtual_socket_server()->set_delay_mean(20);
2588 virtual_socket_server()->set_delay_stddev(5);
2589 virtual_socket_server()->UpdateDelayDistribution();
2590 // Normal procedure, but with unordered data channel config.
2591 ASSERT_TRUE(CreatePeerConnectionWrappers());
2592 ConnectFakeSignaling();
2593 webrtc::DataChannelInit init;
2594 init.ordered = false;
2595 caller()->CreateDataChannel(&init);
2596 caller()->CreateAndSetAndSignalOffer();
2597 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2598 ASSERT_NE(nullptr, caller()->data_channel());
2599 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2600 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2601 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2602
2603 static constexpr int kNumMessages = 100;
2604 // Deliberately chosen to be larger than the MTU so messages get fragmented.
2605 static constexpr size_t kMaxMessageSize = 4096;
2606 // Create and send random messages.
2607 std::vector<std::string> sent_messages;
2608 for (int i = 0; i < kNumMessages; ++i) {
2609 size_t length =
2610 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
2611 std::string message;
2612 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
2613 caller()->data_channel()->Send(DataBuffer(message));
2614 callee()->data_channel()->Send(DataBuffer(message));
2615 sent_messages.push_back(message);
2616 }
2617
2618 // Wait for all messages to be received.
2619 EXPECT_EQ_WAIT(kNumMessages,
2620 caller()->data_observer()->received_message_count(),
2621 kDefaultTimeout);
2622 EXPECT_EQ_WAIT(kNumMessages,
2623 callee()->data_observer()->received_message_count(),
2624 kDefaultTimeout);
2625
2626 // Sort and compare to make sure none of the messages were corrupted.
2627 std::vector<std::string> caller_received_messages =
2628 caller()->data_observer()->messages();
2629 std::vector<std::string> callee_received_messages =
2630 callee()->data_observer()->messages();
2631 std::sort(sent_messages.begin(), sent_messages.end());
2632 std::sort(caller_received_messages.begin(), caller_received_messages.end());
2633 std::sort(callee_received_messages.begin(), callee_received_messages.end());
2634 EXPECT_EQ(sent_messages, caller_received_messages);
2635 EXPECT_EQ(sent_messages, callee_received_messages);
2636}
2637
2638// This test sets up a call between two parties with audio, and video. When
2639// audio and video are setup and flowing, an SCTP data channel is negotiated.
2640TEST_F(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
2641 ASSERT_TRUE(CreatePeerConnectionWrappers());
2642 ConnectFakeSignaling();
2643 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08002644 caller()->AddAudioVideoTracks();
2645 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002646 caller()->CreateAndSetAndSignalOffer();
2647 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2648 // Create data channel and do new offer and answer.
2649 caller()->CreateDataChannel();
2650 caller()->CreateAndSetAndSignalOffer();
2651 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2652 // Caller data channel should already exist (it created one). Callee data
2653 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2654 ASSERT_NE(nullptr, caller()->data_channel());
2655 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2656 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2657 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2658 // Ensure data can be sent in both directions.
2659 std::string data = "hello world";
2660 caller()->data_channel()->Send(DataBuffer(data));
2661 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2662 kDefaultTimeout);
2663 callee()->data_channel()->Send(DataBuffer(data));
2664 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2665 kDefaultTimeout);
2666}
2667
deadbeef7914b8c2017-04-21 03:23:33 -07002668// Set up a connection initially just using SCTP data channels, later upgrading
2669// to audio/video, ensuring frames are received end-to-end. Effectively the
2670// inverse of the test above.
2671// This was broken in M57; see https://crbug.com/711243
2672TEST_F(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
2673 ASSERT_TRUE(CreatePeerConnectionWrappers());
2674 ConnectFakeSignaling();
2675 // Do initial offer/answer with just data channel.
2676 caller()->CreateDataChannel();
2677 caller()->CreateAndSetAndSignalOffer();
2678 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2679 // Wait until data can be sent over the data channel.
2680 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2681 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2682 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2683
2684 // Do subsequent offer/answer with two-way audio and video. Audio and video
2685 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08002686 caller()->AddAudioVideoTracks();
2687 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002688 caller()->CreateAndSetAndSignalOffer();
2689 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2690 ExpectNewFramesReceivedWithWait(
2691 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2692 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2693 kMaxWaitForFramesMs);
2694}
2695
deadbeef8b7e9ad2017-05-25 09:38:55 -07002696static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07002697 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08002698 GetFirstDataContentDescription(desc);
2699 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07002700 dcd_offer->set_use_sctpmap(false);
2701 dcd_offer->set_protocol("UDP/DTLS/SCTP");
2702}
2703
2704// Test that the data channel works when a spec-compliant SCTP m= section is
2705// offered (using "a=sctp-port" instead of "a=sctpmap", and using
2706// "UDP/DTLS/SCTP" as the protocol).
2707TEST_F(PeerConnectionIntegrationTest,
2708 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
2709 ASSERT_TRUE(CreatePeerConnectionWrappers());
2710 ConnectFakeSignaling();
2711 caller()->CreateDataChannel();
2712 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
2713 caller()->CreateAndSetAndSignalOffer();
2714 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2715 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2716 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2717 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2718
2719 // Ensure data can be sent in both directions.
2720 std::string data = "hello world";
2721 caller()->data_channel()->Send(DataBuffer(data));
2722 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2723 kDefaultTimeout);
2724 callee()->data_channel()->Send(DataBuffer(data));
2725 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2726 kDefaultTimeout);
2727}
2728
deadbeef1dcb1642017-03-29 21:08:16 -07002729#endif // HAVE_SCTP
2730
2731// Test that the ICE connection and gathering states eventually reach
2732// "complete".
2733TEST_F(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
2734 ASSERT_TRUE(CreatePeerConnectionWrappers());
2735 ConnectFakeSignaling();
2736 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08002737 caller()->AddAudioVideoTracks();
2738 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002739 caller()->CreateAndSetAndSignalOffer();
2740 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2741 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
2742 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
2743 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
2744 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
2745 // After the best candidate pair is selected and all candidates are signaled,
2746 // the ICE connection state should reach "complete".
2747 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
2748 // answerer/"callee" by default) only reaches "connected". When this is
2749 // fixed, this test should be updated.
2750 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2751 caller()->ice_connection_state(), kDefaultTimeout);
2752 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2753 callee()->ice_connection_state(), kDefaultTimeout);
2754}
2755
Steve Antonede9ca52017-10-16 13:04:27 -07002756// Test that firewalling the ICE connection causes the clients to identify the
2757// disconnected state and then removing the firewall causes them to reconnect.
2758class PeerConnectionIntegrationIceStatesTest
2759 : public PeerConnectionIntegrationTest,
2760 public ::testing::WithParamInterface<std::tuple<std::string, uint32_t>> {
2761 protected:
2762 PeerConnectionIntegrationIceStatesTest() {
2763 port_allocator_flags_ = std::get<1>(GetParam());
2764 }
2765
2766 void StartStunServer(const SocketAddress& server_address) {
2767 stun_server_.reset(
2768 cricket::TestStunServer::Create(network_thread(), server_address));
2769 }
2770
2771 bool TestIPv6() {
2772 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
2773 }
2774
2775 void SetPortAllocatorFlags() {
2776 caller()->port_allocator()->set_flags(port_allocator_flags_);
2777 callee()->port_allocator()->set_flags(port_allocator_flags_);
2778 }
2779
2780 std::vector<SocketAddress> CallerAddresses() {
2781 std::vector<SocketAddress> addresses;
2782 addresses.push_back(SocketAddress("1.1.1.1", 0));
2783 if (TestIPv6()) {
2784 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
2785 }
2786 return addresses;
2787 }
2788
2789 std::vector<SocketAddress> CalleeAddresses() {
2790 std::vector<SocketAddress> addresses;
2791 addresses.push_back(SocketAddress("2.2.2.2", 0));
2792 if (TestIPv6()) {
2793 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
2794 }
2795 return addresses;
2796 }
2797
2798 void SetUpNetworkInterfaces() {
2799 // Remove the default interfaces added by the test infrastructure.
2800 caller()->network()->RemoveInterface(kDefaultLocalAddress);
2801 callee()->network()->RemoveInterface(kDefaultLocalAddress);
2802
2803 // Add network addresses for test.
2804 for (const auto& caller_address : CallerAddresses()) {
2805 caller()->network()->AddInterface(caller_address);
2806 }
2807 for (const auto& callee_address : CalleeAddresses()) {
2808 callee()->network()->AddInterface(callee_address);
2809 }
2810 }
2811
2812 private:
2813 uint32_t port_allocator_flags_;
2814 std::unique_ptr<cricket::TestStunServer> stun_server_;
2815};
2816
2817// Tests that the PeerConnection goes through all the ICE gathering/connection
2818// states over the duration of the call. This includes Disconnected and Failed
2819// states, induced by putting a firewall between the peers and waiting for them
2820// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08002821TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
2822 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
2823 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
2824 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07002825
2826 const SocketAddress kStunServerAddress =
2827 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
2828 StartStunServer(kStunServerAddress);
2829
2830 PeerConnectionInterface::RTCConfiguration config;
2831 PeerConnectionInterface::IceServer ice_stun_server;
2832 ice_stun_server.urls.push_back(
2833 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
2834 kStunServerAddress.PortAsString());
2835 config.servers.push_back(ice_stun_server);
2836
2837 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
2838 ConnectFakeSignaling();
2839 SetPortAllocatorFlags();
2840 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08002841 caller()->AddAudioVideoTracks();
2842 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07002843
2844 // Initial state before anything happens.
2845 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
2846 caller()->ice_gathering_state());
2847 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
2848 caller()->ice_connection_state());
2849
2850 // Start the call by creating the offer, setting it as the local description,
2851 // then sending it to the peer who will respond with an answer. This happens
2852 // asynchronously so that we can watch the states as it runs in the
2853 // background.
2854 caller()->CreateAndSetAndSignalOffer();
2855
Steve Anton83119dd2017-11-10 16:19:52 -08002856 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
2857 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07002858
2859 // Verify that the observer was notified of the intermediate transitions.
2860 EXPECT_THAT(caller()->ice_connection_state_history(),
2861 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
2862 PeerConnectionInterface::kIceConnectionConnected,
2863 PeerConnectionInterface::kIceConnectionCompleted));
2864 EXPECT_THAT(caller()->ice_gathering_state_history(),
2865 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
2866 PeerConnectionInterface::kIceGatheringComplete));
2867
2868 // Block connections to/from the caller and wait for ICE to become
2869 // disconnected.
2870 for (const auto& caller_address : CallerAddresses()) {
2871 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
2872 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01002873 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08002874 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
2875 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07002876
2877 // Let ICE re-establish by removing the firewall rules.
2878 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01002879 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08002880 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
2881 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07002882
2883 // According to RFC7675, if there is no response within 30 seconds then the
2884 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08002885 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07002886 constexpr int kConsentTimeout = 30000;
2887 for (const auto& caller_address : CallerAddresses()) {
2888 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
2889 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01002890 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08002891 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
2892 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07002893}
2894
2895// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
2896// and that the statistics in the metric observers are updated correctly.
2897TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
2898 ASSERT_TRUE(CreatePeerConnectionWrappers());
2899 ConnectFakeSignaling();
2900 SetPortAllocatorFlags();
2901 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08002902 caller()->AddAudioVideoTracks();
2903 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07002904
2905 rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer(
2906 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>());
2907 caller()->pc()->RegisterUMAObserver(metrics_observer.get());
2908
2909 caller()->CreateAndSetAndSignalOffer();
2910
2911 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2912
2913 const int num_best_ipv4 = metrics_observer->GetEnumCounter(
2914 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv4);
2915 const int num_best_ipv6 = metrics_observer->GetEnumCounter(
2916 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv6);
2917 if (TestIPv6()) {
2918 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
2919 // connection.
2920 EXPECT_EQ(0u, num_best_ipv4);
2921 EXPECT_EQ(1u, num_best_ipv6);
2922 } else {
2923 EXPECT_EQ(1u, num_best_ipv4);
2924 EXPECT_EQ(0u, num_best_ipv6);
2925 }
2926
2927 EXPECT_EQ(0u, metrics_observer->GetEnumCounter(
2928 webrtc::kEnumCounterIceCandidatePairTypeUdp,
2929 webrtc::kIceCandidatePairHostHost));
2930 EXPECT_EQ(1u, metrics_observer->GetEnumCounter(
2931 webrtc::kEnumCounterIceCandidatePairTypeUdp,
2932 webrtc::kIceCandidatePairHostPublicHostPublic));
2933}
2934
2935constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
2936 cricket::PORTALLOCATOR_DISABLE_STUN |
2937 cricket::PORTALLOCATOR_DISABLE_RELAY;
2938constexpr uint32_t kFlagsIPv6NoStun =
2939 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
2940 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
2941constexpr uint32_t kFlagsIPv4Stun =
2942 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
2943
2944INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
2945 PeerConnectionIntegrationIceStatesTest,
2946 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
2947 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
2948 std::make_pair("IPv4 with STUN",
2949 kFlagsIPv4Stun)));
2950
deadbeef1dcb1642017-03-29 21:08:16 -07002951// This test sets up a call between two parties with audio and video.
2952// During the call, the caller restarts ICE and the test verifies that
2953// new ICE candidates are generated and audio and video still can flow, and the
2954// ICE state reaches completed again.
2955TEST_F(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
2956 ASSERT_TRUE(CreatePeerConnectionWrappers());
2957 ConnectFakeSignaling();
2958 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08002959 caller()->AddAudioVideoTracks();
2960 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002961 caller()->CreateAndSetAndSignalOffer();
2962 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2963 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2964 caller()->ice_connection_state(), kMaxWaitForFramesMs);
2965 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2966 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2967
2968 // To verify that the ICE restart actually occurs, get
2969 // ufrag/password/candidates before and after restart.
2970 // Create an SDP string of the first audio candidate for both clients.
2971 const webrtc::IceCandidateCollection* audio_candidates_caller =
2972 caller()->pc()->local_description()->candidates(0);
2973 const webrtc::IceCandidateCollection* audio_candidates_callee =
2974 callee()->pc()->local_description()->candidates(0);
2975 ASSERT_GT(audio_candidates_caller->count(), 0u);
2976 ASSERT_GT(audio_candidates_callee->count(), 0u);
2977 std::string caller_candidate_pre_restart;
2978 ASSERT_TRUE(
2979 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
2980 std::string callee_candidate_pre_restart;
2981 ASSERT_TRUE(
2982 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
2983 const cricket::SessionDescription* desc =
2984 caller()->pc()->local_description()->description();
2985 std::string caller_ufrag_pre_restart =
2986 desc->transport_infos()[0].description.ice_ufrag;
2987 desc = callee()->pc()->local_description()->description();
2988 std::string callee_ufrag_pre_restart =
2989 desc->transport_infos()[0].description.ice_ufrag;
2990
2991 // Have the caller initiate an ICE restart.
2992 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2993 caller()->CreateAndSetAndSignalOffer();
2994 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2995 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2996 caller()->ice_connection_state(), kMaxWaitForFramesMs);
2997 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2998 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2999
3000 // Grab the ufrags/candidates again.
3001 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3002 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3003 ASSERT_GT(audio_candidates_caller->count(), 0u);
3004 ASSERT_GT(audio_candidates_callee->count(), 0u);
3005 std::string caller_candidate_post_restart;
3006 ASSERT_TRUE(
3007 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3008 std::string callee_candidate_post_restart;
3009 ASSERT_TRUE(
3010 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3011 desc = caller()->pc()->local_description()->description();
3012 std::string caller_ufrag_post_restart =
3013 desc->transport_infos()[0].description.ice_ufrag;
3014 desc = callee()->pc()->local_description()->description();
3015 std::string callee_ufrag_post_restart =
3016 desc->transport_infos()[0].description.ice_ufrag;
3017 // Sanity check that an ICE restart was actually negotiated in SDP.
3018 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3019 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3020 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3021 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3022
3023 // Ensure that additional frames are received after the ICE restart.
3024 ExpectNewFramesReceivedWithWait(
3025 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3026 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3027 kMaxWaitForFramesMs);
3028}
3029
3030// Verify that audio/video can be received end-to-end when ICE renomination is
3031// enabled.
3032TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
3033 PeerConnectionInterface::RTCConfiguration config;
3034 config.enable_ice_renomination = true;
3035 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3036 ConnectFakeSignaling();
3037 // Do normal offer/answer and wait for some frames to be received in each
3038 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003039 caller()->AddAudioVideoTracks();
3040 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003041 caller()->CreateAndSetAndSignalOffer();
3042 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3043 // Sanity check that ICE renomination was actually negotiated.
3044 const cricket::SessionDescription* desc =
3045 caller()->pc()->local_description()->description();
3046 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003047 ASSERT_NE(
3048 info.description.transport_options.end(),
3049 std::find(info.description.transport_options.begin(),
3050 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003051 }
3052 desc = callee()->pc()->local_description()->description();
3053 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003054 ASSERT_NE(
3055 info.description.transport_options.end(),
3056 std::find(info.description.transport_options.begin(),
3057 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003058 }
3059 ExpectNewFramesReceivedWithWait(
3060 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3061 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3062 kMaxWaitForFramesMs);
3063}
3064
Steve Anton6f25b092017-10-23 09:39:20 -07003065// With a max bundle policy and RTCP muxing, adding a new media description to
3066// the connection should not affect ICE at all because the new media will use
3067// the existing connection.
3068TEST_F(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003069 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07003070 PeerConnectionInterface::RTCConfiguration config;
3071 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3072 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3073 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3074 config, PeerConnectionInterface::RTCConfiguration()));
3075 ConnectFakeSignaling();
3076
Steve Anton15324772018-01-16 10:26:49 -08003077 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003078 caller()->CreateAndSetAndSignalOffer();
3079 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07003080 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3081 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07003082
3083 caller()->clear_ice_connection_state_history();
3084
Steve Anton15324772018-01-16 10:26:49 -08003085 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003086 caller()->CreateAndSetAndSignalOffer();
3087 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3088
3089 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
3090}
3091
deadbeef1dcb1642017-03-29 21:08:16 -07003092// This test sets up a call between two parties with audio and video. It then
3093// renegotiates setting the video m-line to "port 0", then later renegotiates
3094// again, enabling video.
3095TEST_F(PeerConnectionIntegrationTest,
3096 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
3097 ASSERT_TRUE(CreatePeerConnectionWrappers());
3098 ConnectFakeSignaling();
3099
3100 // Do initial negotiation, only sending media from the caller. Will result in
3101 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08003102 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003103 caller()->CreateAndSetAndSignalOffer();
3104 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3105
3106 // Negotiate again, disabling the video "m=" section (the callee will set the
3107 // port to 0 due to offer_to_receive_video = 0).
3108 PeerConnectionInterface::RTCOfferAnswerOptions options;
3109 options.offer_to_receive_video = 0;
3110 callee()->SetOfferAnswerOptions(options);
3111 caller()->CreateAndSetAndSignalOffer();
3112 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3113 // Sanity check that video "m=" section was actually rejected.
3114 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
3115 callee()->pc()->local_description()->description());
3116 ASSERT_NE(nullptr, answer_video_content);
3117 ASSERT_TRUE(answer_video_content->rejected);
3118
3119 // Enable video and do negotiation again, making sure video is received
3120 // end-to-end, also adding media stream to callee.
3121 options.offer_to_receive_video = 1;
3122 callee()->SetOfferAnswerOptions(options);
Steve Anton15324772018-01-16 10:26:49 -08003123 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003124 caller()->CreateAndSetAndSignalOffer();
3125 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3126 // Verify the caller receives frames from the newly added stream, and the
3127 // callee receives additional frames from the re-enabled video m= section.
3128 ExpectNewFramesReceivedWithWait(
3129 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3130 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3131 kMaxWaitForFramesMs);
3132}
3133
3134// This test sets up a Jsep call between two parties with external
3135// VideoDecoderFactory.
3136// TODO(holmer): Disabled due to sometimes crashing on buildbots.
3137// See issue webrtc/2378.
3138TEST_F(PeerConnectionIntegrationTest,
3139 DISABLED_EndToEndCallWithVideoDecoderFactory) {
3140 ASSERT_TRUE(CreatePeerConnectionWrappers());
3141 EnableVideoDecoderFactory();
3142 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003143 caller()->AddAudioVideoTracks();
3144 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003145 caller()->CreateAndSetAndSignalOffer();
3146 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3147 ExpectNewFramesReceivedWithWait(
3148 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3149 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3150 kMaxWaitForFramesMs);
3151}
3152
3153// This tests that if we negotiate after calling CreateSender but before we
3154// have a track, then set a track later, frames from the newly-set track are
3155// received end-to-end.
3156// TODO(deadbeef): Change this test to use AddTransceiver, once that's
3157// implemented.
3158TEST_F(PeerConnectionIntegrationTest,
3159 MediaFlowsAfterEarlyWarmupWithCreateSender) {
3160 ASSERT_TRUE(CreatePeerConnectionWrappers());
3161 ConnectFakeSignaling();
3162 auto caller_audio_sender =
3163 caller()->pc()->CreateSender("audio", "caller_stream");
3164 auto caller_video_sender =
3165 caller()->pc()->CreateSender("video", "caller_stream");
3166 auto callee_audio_sender =
3167 callee()->pc()->CreateSender("audio", "callee_stream");
3168 auto callee_video_sender =
3169 callee()->pc()->CreateSender("video", "callee_stream");
3170 caller()->CreateAndSetAndSignalOffer();
3171 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3172 // Wait for ICE to complete, without any tracks being set.
3173 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3174 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3175 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3176 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3177 // Now set the tracks, and expect frames to immediately start flowing.
3178 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3179 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3180 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3181 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
3182 ExpectNewFramesReceivedWithWait(
3183 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3184 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3185 kMaxWaitForFramesMs);
3186}
3187
3188// This test verifies that a remote video track can be added via AddStream,
3189// and sent end-to-end. For this particular test, it's simply echoed back
3190// from the caller to the callee, rather than being forwarded to a third
3191// PeerConnection.
3192TEST_F(PeerConnectionIntegrationTest, CanSendRemoteVideoTrack) {
3193 ASSERT_TRUE(CreatePeerConnectionWrappers());
3194 ConnectFakeSignaling();
3195 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08003196 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003197 caller()->CreateAndSetAndSignalOffer();
3198 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3199 ASSERT_EQ(1, callee()->remote_streams()->count());
3200
3201 // Echo the stream back, and do a new offer/anwer (initiated by callee this
3202 // time).
3203 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
3204 callee()->CreateAndSetAndSignalOffer();
3205 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3206
3207 int expected_caller_received_video_frames = kDefaultExpectedVideoFrameCount;
3208 ExpectNewFramesReceivedWithWait(0, expected_caller_received_video_frames, 0,
3209 0, kMaxWaitForFramesMs);
3210}
3211
3212// Test that we achieve the expected end-to-end connection time, using a
3213// fake clock and simulated latency on the media and signaling paths.
3214// We use a TURN<->TURN connection because this is usually the quickest to
3215// set up initially, especially when we're confident the connection will work
3216// and can start sending media before we get a STUN response.
3217//
3218// With various optimizations enabled, here are the network delays we expect to
3219// be on the critical path:
3220// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
3221// signaling answer (with DTLS fingerprint).
3222// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
3223// using TURN<->TURN pair, and DTLS exchange is 4 packets,
3224// the first of which should have arrived before the answer.
3225TEST_F(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
3226 rtc::ScopedFakeClock fake_clock;
3227 // Some things use a time of "0" as a special value, so we need to start out
3228 // the fake clock at a nonzero time.
3229 // TODO(deadbeef): Fix this.
3230 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
3231
3232 static constexpr int media_hop_delay_ms = 50;
3233 static constexpr int signaling_trip_delay_ms = 500;
3234 // For explanation of these values, see comment above.
3235 static constexpr int required_media_hops = 9;
3236 static constexpr int required_signaling_trips = 2;
3237 // For internal delays (such as posting an event asychronously).
3238 static constexpr int allowed_internal_delay_ms = 20;
3239 static constexpr int total_connection_time_ms =
3240 media_hop_delay_ms * required_media_hops +
3241 signaling_trip_delay_ms * required_signaling_trips +
3242 allowed_internal_delay_ms;
3243
3244 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3245 3478};
3246 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3247 0};
3248 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3249 3478};
3250 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3251 0};
3252 cricket::TestTurnServer turn_server_1(network_thread(),
3253 turn_server_1_internal_address,
3254 turn_server_1_external_address);
3255 cricket::TestTurnServer turn_server_2(network_thread(),
3256 turn_server_2_internal_address,
3257 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003258
deadbeef1dcb1642017-03-29 21:08:16 -07003259 // Bypass permission check on received packets so media can be sent before
3260 // the candidate is signaled.
3261 turn_server_1.set_enable_permission_checks(false);
3262 turn_server_2.set_enable_permission_checks(false);
3263
3264 PeerConnectionInterface::RTCConfiguration client_1_config;
3265 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3266 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3267 ice_server_1.username = "test";
3268 ice_server_1.password = "test";
3269 client_1_config.servers.push_back(ice_server_1);
3270 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3271 client_1_config.presume_writable_when_fully_relayed = true;
3272
3273 PeerConnectionInterface::RTCConfiguration client_2_config;
3274 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3275 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3276 ice_server_2.username = "test";
3277 ice_server_2.password = "test";
3278 client_2_config.servers.push_back(ice_server_2);
3279 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3280 client_2_config.presume_writable_when_fully_relayed = true;
3281
3282 ASSERT_TRUE(
3283 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3284 // Set up the simulated delays.
3285 SetSignalingDelayMs(signaling_trip_delay_ms);
3286 ConnectFakeSignaling();
3287 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
3288 virtual_socket_server()->UpdateDelayDistribution();
3289
3290 // Set "offer to receive audio/video" without adding any tracks, so we just
3291 // set up ICE/DTLS with no media.
3292 PeerConnectionInterface::RTCOfferAnswerOptions options;
3293 options.offer_to_receive_audio = 1;
3294 options.offer_to_receive_video = 1;
3295 caller()->SetOfferAnswerOptions(options);
3296 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07003297 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
3298 fake_clock);
deadbeef1dcb1642017-03-29 21:08:16 -07003299 // Need to free the clients here since they're using things we created on
3300 // the stack.
3301 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3302 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3303}
3304
Jonas Orelandbdcee282017-10-10 14:01:40 +02003305// Verify that a TurnCustomizer passed in through RTCConfiguration
3306// is actually used by the underlying TURN candidate pair.
3307// Note that turnport_unittest.cc contains more detailed, lower-level tests.
3308TEST_F(PeerConnectionIntegrationTest, \
3309 TurnCustomizerUsedForTurnConnections) {
3310 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3311 3478};
3312 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3313 0};
3314 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3315 3478};
3316 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3317 0};
3318 cricket::TestTurnServer turn_server_1(network_thread(),
3319 turn_server_1_internal_address,
3320 turn_server_1_external_address);
3321 cricket::TestTurnServer turn_server_2(network_thread(),
3322 turn_server_2_internal_address,
3323 turn_server_2_external_address);
3324
3325 PeerConnectionInterface::RTCConfiguration client_1_config;
3326 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3327 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3328 ice_server_1.username = "test";
3329 ice_server_1.password = "test";
3330 client_1_config.servers.push_back(ice_server_1);
3331 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3332 auto customizer1 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3333 client_1_config.turn_customizer = customizer1.get();
3334
3335 PeerConnectionInterface::RTCConfiguration client_2_config;
3336 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3337 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3338 ice_server_2.username = "test";
3339 ice_server_2.password = "test";
3340 client_2_config.servers.push_back(ice_server_2);
3341 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3342 auto customizer2 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3343 client_2_config.turn_customizer = customizer2.get();
3344
3345 ASSERT_TRUE(
3346 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3347 ConnectFakeSignaling();
3348
3349 // Set "offer to receive audio/video" without adding any tracks, so we just
3350 // set up ICE/DTLS with no media.
3351 PeerConnectionInterface::RTCOfferAnswerOptions options;
3352 options.offer_to_receive_audio = 1;
3353 options.offer_to_receive_video = 1;
3354 caller()->SetOfferAnswerOptions(options);
3355 caller()->CreateAndSetAndSignalOffer();
3356 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3357
3358 EXPECT_GT(customizer1->allow_channel_data_cnt_, 0u);
3359 EXPECT_GT(customizer1->modify_cnt_, 0u);
3360
3361 EXPECT_GT(customizer2->allow_channel_data_cnt_, 0u);
3362 EXPECT_GT(customizer2->modify_cnt_, 0u);
3363
3364 // Need to free the clients here since they're using things we created on
3365 // the stack.
3366 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3367 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3368}
3369
deadbeefc964d0b2017-04-03 10:03:35 -07003370// Test that audio and video flow end-to-end when codec names don't use the
3371// expected casing, given that they're supposed to be case insensitive. To test
3372// this, all but one codec is removed from each media description, and its
3373// casing is changed.
3374//
3375// In the past, this has regressed and caused crashes/black video, due to the
3376// fact that code at some layers was doing case-insensitive comparisons and
3377// code at other layers was not.
3378TEST_F(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
3379 ASSERT_TRUE(CreatePeerConnectionWrappers());
3380 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003381 caller()->AddAudioVideoTracks();
3382 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07003383
3384 // Remove all but one audio/video codec (opus and VP8), and change the
3385 // casing of the caller's generated offer.
3386 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
3387 cricket::AudioContentDescription* audio =
3388 GetFirstAudioContentDescription(description);
3389 ASSERT_NE(nullptr, audio);
3390 auto audio_codecs = audio->codecs();
3391 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
3392 [](const cricket::AudioCodec& codec) {
3393 return codec.name != "opus";
3394 }),
3395 audio_codecs.end());
3396 ASSERT_EQ(1u, audio_codecs.size());
3397 audio_codecs[0].name = "OpUs";
3398 audio->set_codecs(audio_codecs);
3399
3400 cricket::VideoContentDescription* video =
3401 GetFirstVideoContentDescription(description);
3402 ASSERT_NE(nullptr, video);
3403 auto video_codecs = video->codecs();
3404 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
3405 [](const cricket::VideoCodec& codec) {
3406 return codec.name != "VP8";
3407 }),
3408 video_codecs.end());
3409 ASSERT_EQ(1u, video_codecs.size());
3410 video_codecs[0].name = "vP8";
3411 video->set_codecs(video_codecs);
3412 });
3413
3414 caller()->CreateAndSetAndSignalOffer();
3415 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3416
3417 // Verify frames are still received end-to-end.
3418 ExpectNewFramesReceivedWithWait(
3419 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3420 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3421 kMaxWaitForFramesMs);
3422}
3423
hbos8d609f62017-04-10 07:39:05 -07003424TEST_F(PeerConnectionIntegrationTest, GetSources) {
3425 ASSERT_TRUE(CreatePeerConnectionWrappers());
3426 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003427 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07003428 caller()->CreateAndSetAndSignalOffer();
3429 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07003430 // Wait for one audio frame to be received by the callee.
hbos8d609f62017-04-10 07:39:05 -07003431 ExpectNewFramesReceivedWithWait(0, 0, 1, 0, kMaxWaitForFramesMs);
3432 ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u);
3433 auto receiver = callee()->pc()->GetReceivers()[0];
3434 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
3435
3436 auto contributing_sources = receiver->GetSources();
3437 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
3438 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
3439 contributing_sources[0].source_id());
3440}
3441
deadbeef2f425aa2017-04-14 10:41:32 -07003442// Test that if a track is removed and added again with a different stream ID,
3443// the new stream ID is successfully communicated in SDP and media continues to
3444// flow end-to-end.
3445TEST_F(PeerConnectionIntegrationTest, RemoveAndAddTrackWithNewStreamId) {
3446 ASSERT_TRUE(CreatePeerConnectionWrappers());
3447 ConnectFakeSignaling();
3448
3449 rtc::scoped_refptr<MediaStreamInterface> stream_1 =
3450 caller()->pc_factory()->CreateLocalMediaStream("stream_1");
3451 rtc::scoped_refptr<MediaStreamInterface> stream_2 =
3452 caller()->pc_factory()->CreateLocalMediaStream("stream_2");
3453
3454 // Add track using stream 1, do offer/answer.
3455 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
3456 caller()->CreateLocalAudioTrack();
3457 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
3458 caller()->pc()->AddTrack(track, {stream_1.get()});
3459 caller()->CreateAndSetAndSignalOffer();
3460 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3461 // Wait for one audio frame to be received by the callee.
3462 ExpectNewFramesReceivedWithWait(0, 0, 1, 0, kMaxWaitForFramesMs);
3463
3464 // Remove the sender, and create a new one with the new stream.
3465 caller()->pc()->RemoveTrack(sender);
3466 sender = caller()->pc()->AddTrack(track, {stream_2.get()});
3467 caller()->CreateAndSetAndSignalOffer();
3468 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3469 // Wait for additional audio frames to be received by the callee.
3470 ExpectNewFramesReceivedWithWait(0, 0, kDefaultExpectedAudioFrameCount, 0,
3471 kMaxWaitForFramesMs);
3472}
3473
Elad Alon99c3fe52017-10-13 16:29:40 +02003474TEST_F(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
3475 ASSERT_TRUE(CreatePeerConnectionWrappers());
3476 ConnectFakeSignaling();
3477
3478 auto output = rtc::MakeUnique<testing::NiceMock<MockRtcEventLogOutput>>();
3479 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
3480 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
3481 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01003482 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
3483 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003484
Steve Anton15324772018-01-16 10:26:49 -08003485 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02003486 caller()->CreateAndSetAndSignalOffer();
3487 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3488}
3489
Steve Antonede9ca52017-10-16 13:04:27 -07003490// Test that if candidates are only signaled by applying full session
3491// descriptions (instead of using AddIceCandidate), the peers can connect to
3492// each other and exchange media.
3493TEST_F(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
3494 ASSERT_TRUE(CreatePeerConnectionWrappers());
3495 // Each side will signal the session descriptions but not candidates.
3496 ConnectFakeSignalingForSdpOnly();
3497
3498 // Add audio video track and exchange the initial offer/answer with media
3499 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08003500 caller()->AddAudioVideoTracks();
3501 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003502 caller()->CreateAndSetAndSignalOffer();
3503
3504 // Wait for all candidates to be gathered on both the caller and callee.
3505 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3506 caller()->ice_gathering_state(), kDefaultTimeout);
3507 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3508 callee()->ice_gathering_state(), kDefaultTimeout);
3509
3510 // The candidates will now be included in the session description, so
3511 // signaling them will start the ICE connection.
3512 caller()->CreateAndSetAndSignalOffer();
3513 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3514
3515 // Ensure that media flows in both directions.
3516 ExpectNewFramesReceivedWithWait(
3517 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3518 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3519 kMaxWaitForFramesMs);
3520}
3521
henrika5f6bf242017-11-01 11:06:56 +01003522// Test that SetAudioPlayout can be used to disable audio playout from the
3523// start, then later enable it. This may be useful, for example, if the caller
3524// needs to play a local ringtone until some event occurs, after which it
3525// switches to playing the received audio.
3526TEST_F(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
3527 ASSERT_TRUE(CreatePeerConnectionWrappers());
3528 ConnectFakeSignaling();
3529
3530 // Set up audio-only call where audio playout is disabled on caller's side.
3531 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08003532 caller()->AddAudioTrack();
3533 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01003534 caller()->CreateAndSetAndSignalOffer();
3535 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3536
3537 // Pump messages for a second.
3538 WAIT(false, 1000);
3539 // Since audio playout is disabled, the caller shouldn't have received
3540 // anything (at the playout level, at least).
3541 EXPECT_EQ(0, caller()->audio_frames_received());
3542 // As a sanity check, make sure the callee (for which playout isn't disabled)
3543 // did still see frames on its audio level.
3544 ASSERT_GT(callee()->audio_frames_received(), 0);
3545
3546 // Enable playout again, and ensure audio starts flowing.
3547 caller()->pc()->SetAudioPlayout(true);
3548 ExpectNewFramesReceivedWithWait(kDefaultExpectedAudioFrameCount, 0,
3549 kDefaultExpectedAudioFrameCount, 0,
3550 kMaxWaitForFramesMs);
3551}
3552
3553double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
3554 auto report = pc->NewGetStats();
3555 auto track_stats_list =
3556 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3557 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
3558 for (const auto* track_stats : track_stats_list) {
3559 if (track_stats->remote_source.is_defined() &&
3560 *track_stats->remote_source) {
3561 remote_track_stats = track_stats;
3562 break;
3563 }
3564 }
3565
3566 if (!remote_track_stats->total_audio_energy.is_defined()) {
3567 return 0.0;
3568 }
3569 return *remote_track_stats->total_audio_energy;
3570}
3571
3572// Test that if audio playout is disabled via the SetAudioPlayout() method, then
3573// incoming audio is still processed and statistics are generated.
3574TEST_F(PeerConnectionIntegrationTest,
3575 DisableAudioPlayoutStillGeneratesAudioStats) {
3576 ASSERT_TRUE(CreatePeerConnectionWrappers());
3577 ConnectFakeSignaling();
3578
3579 // Set up audio-only call where playout is disabled but audio-processing is
3580 // still active.
Steve Anton15324772018-01-16 10:26:49 -08003581 caller()->AddAudioTrack();
3582 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01003583 caller()->pc()->SetAudioPlayout(false);
3584
3585 caller()->CreateAndSetAndSignalOffer();
3586 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3587
3588 // Wait for the callee to receive audio stats.
3589 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
3590}
3591
henrika4f167df2017-11-01 14:45:55 +01003592// Test that SetAudioRecording can be used to disable audio recording from the
3593// start, then later enable it. This may be useful, for example, if the caller
3594// wants to ensure that no audio resources are active before a certain state
3595// is reached.
3596TEST_F(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
3597 ASSERT_TRUE(CreatePeerConnectionWrappers());
3598 ConnectFakeSignaling();
3599
3600 // Set up audio-only call where audio recording is disabled on caller's side.
3601 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08003602 caller()->AddAudioTrack();
3603 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01003604 caller()->CreateAndSetAndSignalOffer();
3605 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3606
3607 // Pump messages for a second.
3608 WAIT(false, 1000);
3609 // Since caller has disabled audio recording, the callee shouldn't have
3610 // received anything.
3611 EXPECT_EQ(0, callee()->audio_frames_received());
3612 // As a sanity check, make sure the caller did still see frames on its
3613 // audio level since audio recording is enabled on the calle side.
3614 ASSERT_GT(caller()->audio_frames_received(), 0);
3615
3616 // Enable audio recording again, and ensure audio starts flowing.
3617 caller()->pc()->SetAudioRecording(true);
3618 ExpectNewFramesReceivedWithWait(kDefaultExpectedAudioFrameCount, 0,
3619 kDefaultExpectedAudioFrameCount, 0,
3620 kMaxWaitForFramesMs);
3621}
3622
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08003623// Test that after closing PeerConnections, they stop sending any packets (ICE,
3624// DTLS, RTP...).
3625TEST_F(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
3626 // Set up audio/video/data, wait for some frames to be received.
3627 ASSERT_TRUE(CreatePeerConnectionWrappers());
3628 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003629 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08003630#ifdef HAVE_SCTP
3631 caller()->CreateDataChannel();
3632#endif
3633 caller()->CreateAndSetAndSignalOffer();
3634 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3635 ExpectNewFramesReceivedWithWait(0, 0, kDefaultExpectedAudioFrameCount,
3636 kDefaultExpectedAudioFrameCount,
3637 kMaxWaitForFramesMs);
3638 // Close PeerConnections.
3639 caller()->pc()->Close();
3640 callee()->pc()->Close();
3641 // Pump messages for a second, and ensure no new packets end up sent.
3642 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
3643 WAIT(false, 1000);
3644 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
3645 EXPECT_EQ(sent_packets_a, sent_packets_b);
3646}
3647
Steve Antond3679212018-01-17 17:41:02 -08003648// Test that a basic 1 audio and 1 video track call works when Unified Plan
3649// semantics configured for both sides.
3650TEST_F(PeerConnectionIntegrationTest, UnifiedPlanMediaFlows) {
3651 PeerConnectionInterface::RTCConfiguration config;
3652 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3653 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3654 ConnectFakeSignaling();
3655 caller()->AddAudioVideoTracks();
3656 callee()->AddAudioVideoTracks();
3657 caller()->CreateAndSetAndSignalOffer();
3658 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3659 ExpectNewFramesReceivedWithWait(
3660 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3661 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3662 kMaxWaitForFramesMs);
3663}
3664
deadbeef1dcb1642017-03-29 21:08:16 -07003665} // namespace
3666
3667#endif // if !defined(THREAD_SANITIZER)