blob: 7bf6ad862eb0f4641e0c3db6f93f494299e33ae7 [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();
1287 // Do normal offer/answer and wait for some frames to be received in each
1288 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001289 caller()->AddAudioVideoTracks();
1290 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001291 caller()->CreateAndSetAndSignalOffer();
1292 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1293 ExpectNewFramesReceivedWithWait(
1294 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1295 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1296 kMaxWaitForFramesMs);
1297}
1298
1299// Uses SDES instead of DTLS for key agreement.
1300TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
1301 PeerConnectionInterface::RTCConfiguration sdes_config;
1302 sdes_config.enable_dtls_srtp.emplace(false);
1303 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1304 ConnectFakeSignaling();
1305
1306 // Do normal offer/answer and wait for some frames to be received in each
1307 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001308 caller()->AddAudioVideoTracks();
1309 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001310 caller()->CreateAndSetAndSignalOffer();
1311 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1312 ExpectNewFramesReceivedWithWait(
1313 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1314 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1315 kMaxWaitForFramesMs);
1316}
1317
Steve Anton8c0f7a72017-10-03 10:03:10 -07001318// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1319// certificate once the DTLS handshake has finished.
1320TEST_F(PeerConnectionIntegrationTest,
1321 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1322 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1323 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1324 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1325 return pc->GetRemoteAudioSSLCertificate();
1326 };
1327
1328 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1329 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1330
1331 // Configure each side with a known certificate so they can be compared later.
1332 PeerConnectionInterface::RTCConfiguration caller_config;
1333 caller_config.enable_dtls_srtp.emplace(true);
1334 caller_config.certificates.push_back(caller_cert);
1335 PeerConnectionInterface::RTCConfiguration callee_config;
1336 callee_config.enable_dtls_srtp.emplace(true);
1337 callee_config.certificates.push_back(callee_cert);
1338 ASSERT_TRUE(
1339 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1340 ConnectFakeSignaling();
1341
1342 // When first initialized, there should not be a remote SSL certificate (and
1343 // calling this method should not crash).
1344 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1345 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
1346
Steve Anton15324772018-01-16 10:26:49 -08001347 caller()->AddAudioTrack();
1348 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001349 caller()->CreateAndSetAndSignalOffer();
1350 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1351 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1352
1353 // Once DTLS has been connected, each side should return the other's SSL
1354 // certificate when calling GetRemoteAudioSSLCertificate.
1355
1356 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1357 ASSERT_TRUE(caller_remote_cert);
1358 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1359 caller_remote_cert->ToPEMString());
1360
1361 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1362 ASSERT_TRUE(callee_remote_cert);
1363 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1364 callee_remote_cert->ToPEMString());
1365}
1366
deadbeef1dcb1642017-03-29 21:08:16 -07001367// This test sets up a call between two parties (using DTLS) and tests that we
1368// can get a video aspect ratio of 16:9.
1369TEST_F(PeerConnectionIntegrationTest, SendAndReceive16To9AspectRatio) {
1370 ASSERT_TRUE(CreatePeerConnectionWrappers());
1371 ConnectFakeSignaling();
1372
1373 // Add video tracks with 16:9 constraint.
1374 FakeConstraints constraints;
1375 double requested_ratio = 16.0 / 9;
1376 constraints.SetMandatoryMinAspectRatio(requested_ratio);
Steve Anton15324772018-01-16 10:26:49 -08001377 caller()->AddTrack(
1378 caller()->CreateLocalVideoTrackWithConstraints(constraints));
1379 callee()->AddTrack(
1380 callee()->CreateLocalVideoTrackWithConstraints(constraints));
deadbeef1dcb1642017-03-29 21:08:16 -07001381
1382 // Do normal offer/answer and wait for at least one frame to be received in
1383 // each direction.
1384 caller()->CreateAndSetAndSignalOffer();
1385 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1386 callee()->min_video_frames_received_per_track() > 0,
1387 kMaxWaitForFramesMs);
1388
1389 // Check rendered aspect ratio.
1390 EXPECT_EQ(requested_ratio, caller()->local_rendered_aspect_ratio());
1391 EXPECT_EQ(requested_ratio, caller()->rendered_aspect_ratio());
1392 EXPECT_EQ(requested_ratio, callee()->local_rendered_aspect_ratio());
1393 EXPECT_EQ(requested_ratio, callee()->rendered_aspect_ratio());
1394}
1395
1396// This test sets up a call between two parties with a source resolution of
1397// 1280x720 and verifies that a 16:9 aspect ratio is received.
1398TEST_F(PeerConnectionIntegrationTest,
1399 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1400 ASSERT_TRUE(CreatePeerConnectionWrappers());
1401 ConnectFakeSignaling();
1402
1403 // Similar to above test, but uses MandatoryMin[Width/Height] constraint
1404 // instead of aspect ratio constraint.
1405 FakeConstraints constraints;
1406 constraints.SetMandatoryMinWidth(1280);
1407 constraints.SetMandatoryMinHeight(720);
Steve Anton15324772018-01-16 10:26:49 -08001408 caller()->AddTrack(
1409 caller()->CreateLocalVideoTrackWithConstraints(constraints));
1410 callee()->AddTrack(
1411 callee()->CreateLocalVideoTrackWithConstraints(constraints));
deadbeef1dcb1642017-03-29 21:08:16 -07001412
1413 // Do normal offer/answer and wait for at least one frame to be received in
1414 // each direction.
1415 caller()->CreateAndSetAndSignalOffer();
1416 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1417 callee()->min_video_frames_received_per_track() > 0,
1418 kMaxWaitForFramesMs);
1419
1420 // Check rendered aspect ratio.
1421 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1422 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1423 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1424 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1425}
1426
1427// This test sets up an one-way call, with media only from caller to
1428// callee.
1429TEST_F(PeerConnectionIntegrationTest, OneWayMediaCall) {
1430 ASSERT_TRUE(CreatePeerConnectionWrappers());
1431 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001432 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001433 caller()->CreateAndSetAndSignalOffer();
1434 int caller_received_frames = 0;
1435 ExpectNewFramesReceivedWithWait(
1436 caller_received_frames, caller_received_frames,
1437 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1438 kMaxWaitForFramesMs);
1439}
1440
1441// This test sets up a audio call initially, with the callee rejecting video
1442// initially. Then later the callee decides to upgrade to audio/video, and
1443// initiates a new offer/answer exchange.
1444TEST_F(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
1445 ASSERT_TRUE(CreatePeerConnectionWrappers());
1446 ConnectFakeSignaling();
1447 // Initially, offer an audio/video stream from the caller, but refuse to
1448 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001449 caller()->AddAudioVideoTracks();
1450 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001451 PeerConnectionInterface::RTCOfferAnswerOptions options;
1452 options.offer_to_receive_video = 0;
1453 callee()->SetOfferAnswerOptions(options);
1454 // Do offer/answer and make sure audio is still received end-to-end.
1455 caller()->CreateAndSetAndSignalOffer();
1456 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1457 ExpectNewFramesReceivedWithWait(kDefaultExpectedAudioFrameCount, 0,
1458 kDefaultExpectedAudioFrameCount, 0,
1459 kMaxWaitForFramesMs);
1460 // Sanity check that the callee's description has a rejected video section.
1461 ASSERT_NE(nullptr, callee()->pc()->local_description());
1462 const ContentInfo* callee_video_content =
1463 GetFirstVideoContent(callee()->pc()->local_description()->description());
1464 ASSERT_NE(nullptr, callee_video_content);
1465 EXPECT_TRUE(callee_video_content->rejected);
1466 // Now negotiate with video and ensure negotiation succeeds, with video
1467 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001468 callee()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001469 options.offer_to_receive_video = 1;
1470 callee()->SetOfferAnswerOptions(options);
1471 callee()->CreateAndSetAndSignalOffer();
1472 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1473 // Expect additional audio frames to be received after the upgrade.
1474 ExpectNewFramesReceivedWithWait(
1475 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1476 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1477 kMaxWaitForFramesMs);
1478}
1479
deadbeef4389b4d2017-09-07 09:07:36 -07001480// Simpler than the above test; just add an audio track to an established
1481// video-only connection.
1482TEST_F(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
1483 ASSERT_TRUE(CreatePeerConnectionWrappers());
1484 ConnectFakeSignaling();
1485 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001486 caller()->AddVideoTrack();
1487 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001488 caller()->CreateAndSetAndSignalOffer();
1489 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1490 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001491 caller()->AddAudioTrack();
1492 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001493 caller()->CreateAndSetAndSignalOffer();
1494 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1495 // Ensure both audio and video frames are received end-to-end.
1496 ExpectNewFramesReceivedWithWait(
1497 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1498 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1499 kMaxWaitForFramesMs);
1500}
1501
deadbeef1dcb1642017-03-29 21:08:16 -07001502// This test sets up a call that's transferred to a new caller with a different
1503// DTLS fingerprint.
1504TEST_F(PeerConnectionIntegrationTest, CallTransferredForCallee) {
1505 ASSERT_TRUE(CreatePeerConnectionWrappers());
1506 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001507 caller()->AddAudioVideoTracks();
1508 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001509 caller()->CreateAndSetAndSignalOffer();
1510 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1511
1512 // Keep the original peer around which will still send packets to the
1513 // receiving client. These SRTP packets will be dropped.
1514 std::unique_ptr<PeerConnectionWrapper> original_peer(
1515 SetCallerPcWrapperAndReturnCurrent(
1516 CreatePeerConnectionWrapperWithAlternateKey()));
1517 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1518 // directly above.
1519 original_peer->pc()->Close();
1520
1521 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001522 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001523 caller()->CreateAndSetAndSignalOffer();
1524 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1525 // Wait for some additional frames to be transmitted end-to-end.
1526 ExpectNewFramesReceivedWithWait(
1527 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1528 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1529 kMaxWaitForFramesMs);
1530}
1531
1532// This test sets up a call that's transferred to a new callee with a different
1533// DTLS fingerprint.
1534TEST_F(PeerConnectionIntegrationTest, CallTransferredForCaller) {
1535 ASSERT_TRUE(CreatePeerConnectionWrappers());
1536 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001537 caller()->AddAudioVideoTracks();
1538 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001539 caller()->CreateAndSetAndSignalOffer();
1540 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1541
1542 // Keep the original peer around which will still send packets to the
1543 // receiving client. These SRTP packets will be dropped.
1544 std::unique_ptr<PeerConnectionWrapper> original_peer(
1545 SetCalleePcWrapperAndReturnCurrent(
1546 CreatePeerConnectionWrapperWithAlternateKey()));
1547 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1548 // directly above.
1549 original_peer->pc()->Close();
1550
1551 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001552 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001553 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1554 caller()->CreateAndSetAndSignalOffer();
1555 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1556 // Wait for some additional frames to be transmitted end-to-end.
1557 ExpectNewFramesReceivedWithWait(
1558 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1559 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1560 kMaxWaitForFramesMs);
1561}
1562
1563// This test sets up a non-bundled call and negotiates bundling at the same
1564// time as starting an ICE restart. When bundling is in effect in the restart,
1565// the DTLS-SRTP context should be successfully reset.
1566TEST_F(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
1567 ASSERT_TRUE(CreatePeerConnectionWrappers());
1568 ConnectFakeSignaling();
1569
Steve Anton15324772018-01-16 10:26:49 -08001570 caller()->AddAudioVideoTracks();
1571 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001572 // Remove the bundle group from the SDP received by the callee.
1573 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1574 desc->RemoveGroupByName("BUNDLE");
1575 });
1576 caller()->CreateAndSetAndSignalOffer();
1577 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1578 ExpectNewFramesReceivedWithWait(
1579 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1580 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1581 kMaxWaitForFramesMs);
1582
1583 // Now stop removing the BUNDLE group, and trigger an ICE restart.
1584 callee()->SetReceivedSdpMunger(nullptr);
1585 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1586 caller()->CreateAndSetAndSignalOffer();
1587 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1588
1589 // Expect additional frames to be received after the ICE restart.
1590 ExpectNewFramesReceivedWithWait(
1591 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1592 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1593 kMaxWaitForFramesMs);
1594}
1595
1596// Test CVO (Coordination of Video Orientation). If a video source is rotated
1597// and both peers support the CVO RTP header extension, the actual video frames
1598// don't need to be encoded in different resolutions, since the rotation is
1599// communicated through the RTP header extension.
1600TEST_F(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
1601 ASSERT_TRUE(CreatePeerConnectionWrappers());
1602 ConnectFakeSignaling();
1603 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001604 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001605 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001606 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001607 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1608
1609 // Wait for video frames to be received by both sides.
1610 caller()->CreateAndSetAndSignalOffer();
1611 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1612 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1613 callee()->min_video_frames_received_per_track() > 0,
1614 kMaxWaitForFramesMs);
1615
1616 // Ensure that the aspect ratio is unmodified.
1617 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1618 // not just assumed.
1619 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
1620 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
1621 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
1622 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
1623 // Ensure that the CVO bits were surfaced to the renderer.
1624 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
1625 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
1626}
1627
1628// Test that when the CVO extension isn't supported, video is rotated the
1629// old-fashioned way, by encoding rotated frames.
1630TEST_F(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
1631 ASSERT_TRUE(CreatePeerConnectionWrappers());
1632 ConnectFakeSignaling();
1633 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001634 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001635 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08001636 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07001637 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
1638
1639 // Remove the CVO extension from the offered SDP.
1640 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1641 cricket::VideoContentDescription* video =
1642 GetFirstVideoContentDescription(desc);
1643 video->ClearRtpHeaderExtensions();
1644 });
1645 // Wait for video frames to be received by both sides.
1646 caller()->CreateAndSetAndSignalOffer();
1647 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1648 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1649 callee()->min_video_frames_received_per_track() > 0,
1650 kMaxWaitForFramesMs);
1651
1652 // Expect that the aspect ratio is inversed to account for the 90/270 degree
1653 // rotation.
1654 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
1655 // not just assumed.
1656 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
1657 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
1658 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
1659 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
1660 // Expect that each endpoint is unaware of the rotation of the other endpoint.
1661 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
1662 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
1663}
1664
1665// TODO(deadbeef): The tests below rely on RTCOfferAnswerOptions to reject an
1666// m= section. When we implement Unified Plan SDP, the right way to do this
1667// would be by stopping an RtpTransceiver.
1668
1669// Test that if the answerer rejects the audio m= section, no audio is sent or
1670// received, but video still can be.
1671TEST_F(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
1672 ASSERT_TRUE(CreatePeerConnectionWrappers());
1673 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001674 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001675 // Only add video track for callee, and set offer_to_receive_audio to 0, so
1676 // it will reject the audio m= section completely.
1677 PeerConnectionInterface::RTCOfferAnswerOptions options;
1678 options.offer_to_receive_audio = 0;
1679 callee()->SetOfferAnswerOptions(options);
Steve Anton15324772018-01-16 10:26:49 -08001680 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07001681 // Do offer/answer and wait for successful end-to-end video frames.
1682 caller()->CreateAndSetAndSignalOffer();
1683 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1684 ExpectNewFramesReceivedWithWait(0, kDefaultExpectedVideoFrameCount, 0,
1685 kDefaultExpectedVideoFrameCount,
1686 kMaxWaitForFramesMs);
1687 // Shouldn't have received audio frames at any point.
1688 EXPECT_EQ(0, caller()->audio_frames_received());
1689 EXPECT_EQ(0, callee()->audio_frames_received());
1690 // Sanity check that the callee's description has a rejected audio section.
1691 ASSERT_NE(nullptr, callee()->pc()->local_description());
1692 const ContentInfo* callee_audio_content =
1693 GetFirstAudioContent(callee()->pc()->local_description()->description());
1694 ASSERT_NE(nullptr, callee_audio_content);
1695 EXPECT_TRUE(callee_audio_content->rejected);
1696}
1697
1698// Test that if the answerer rejects the video m= section, no video is sent or
1699// received, but audio still can be.
1700TEST_F(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
1701 ASSERT_TRUE(CreatePeerConnectionWrappers());
1702 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001703 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001704 // Only add audio track for callee, and set offer_to_receive_video to 0, so
1705 // it will reject the video m= section completely.
1706 PeerConnectionInterface::RTCOfferAnswerOptions options;
1707 options.offer_to_receive_video = 0;
1708 callee()->SetOfferAnswerOptions(options);
Steve Anton15324772018-01-16 10:26:49 -08001709 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07001710 // Do offer/answer and wait for successful end-to-end audio frames.
1711 caller()->CreateAndSetAndSignalOffer();
1712 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1713 ExpectNewFramesReceivedWithWait(kDefaultExpectedAudioFrameCount, 0,
1714 kDefaultExpectedAudioFrameCount, 0,
1715 kMaxWaitForFramesMs);
1716 // Shouldn't have received video frames at any point.
1717 EXPECT_EQ(0, caller()->total_video_frames_received());
1718 EXPECT_EQ(0, callee()->total_video_frames_received());
1719 // Sanity check that the callee's description has a rejected video section.
1720 ASSERT_NE(nullptr, callee()->pc()->local_description());
1721 const ContentInfo* callee_video_content =
1722 GetFirstVideoContent(callee()->pc()->local_description()->description());
1723 ASSERT_NE(nullptr, callee_video_content);
1724 EXPECT_TRUE(callee_video_content->rejected);
1725}
1726
1727// Test that if the answerer rejects both audio and video m= sections, nothing
1728// bad happens.
1729// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
1730// test anything but the fact that negotiation succeeds, which doesn't mean
1731// much.
1732TEST_F(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
1733 ASSERT_TRUE(CreatePeerConnectionWrappers());
1734 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001735 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001736 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
1737 // will reject both audio and video m= sections.
1738 PeerConnectionInterface::RTCOfferAnswerOptions options;
1739 options.offer_to_receive_audio = 0;
1740 options.offer_to_receive_video = 0;
1741 callee()->SetOfferAnswerOptions(options);
1742 // Do offer/answer and wait for stable signaling state.
1743 caller()->CreateAndSetAndSignalOffer();
1744 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1745 // Sanity check that the callee's description has rejected m= sections.
1746 ASSERT_NE(nullptr, callee()->pc()->local_description());
1747 const ContentInfo* callee_audio_content =
1748 GetFirstAudioContent(callee()->pc()->local_description()->description());
1749 ASSERT_NE(nullptr, callee_audio_content);
1750 EXPECT_TRUE(callee_audio_content->rejected);
1751 const ContentInfo* callee_video_content =
1752 GetFirstVideoContent(callee()->pc()->local_description()->description());
1753 ASSERT_NE(nullptr, callee_video_content);
1754 EXPECT_TRUE(callee_video_content->rejected);
1755}
1756
1757// This test sets up an audio and video call between two parties. After the
1758// call runs for a while, the caller sends an updated offer with video being
1759// rejected. Once the re-negotiation is done, the video flow should stop and
1760// the audio flow should continue.
1761TEST_F(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
1762 ASSERT_TRUE(CreatePeerConnectionWrappers());
1763 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001764 caller()->AddAudioVideoTracks();
1765 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001766 caller()->CreateAndSetAndSignalOffer();
1767 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1768 ExpectNewFramesReceivedWithWait(
1769 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1770 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1771 kMaxWaitForFramesMs);
1772
1773 // Renegotiate, rejecting the video m= section.
1774 // TODO(deadbeef): When an RtpTransceiver API is available, use that to
1775 // reject the video m= section.
1776 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
1777 for (cricket::ContentInfo& content : description->contents()) {
1778 if (cricket::IsVideoContent(&content)) {
1779 content.rejected = true;
1780 }
1781 }
1782 });
1783 caller()->CreateAndSetAndSignalOffer();
1784 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
1785
1786 // Sanity check that the caller's description has a rejected video section.
1787 ASSERT_NE(nullptr, caller()->pc()->local_description());
1788 const ContentInfo* caller_video_content =
1789 GetFirstVideoContent(caller()->pc()->local_description()->description());
1790 ASSERT_NE(nullptr, caller_video_content);
1791 EXPECT_TRUE(caller_video_content->rejected);
1792
1793 int caller_video_received = caller()->total_video_frames_received();
1794 int callee_video_received = callee()->total_video_frames_received();
1795
1796 // Wait for some additional audio frames to be received.
1797 ExpectNewFramesReceivedWithWait(kDefaultExpectedAudioFrameCount, 0,
1798 kDefaultExpectedAudioFrameCount, 0,
1799 kMaxWaitForFramesMs);
1800
1801 // During this time, we shouldn't have received any additional video frames
1802 // for the rejected video tracks.
1803 EXPECT_EQ(caller_video_received, caller()->total_video_frames_received());
1804 EXPECT_EQ(callee_video_received, callee()->total_video_frames_received());
1805}
1806
1807// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
1808// is needed to support legacy endpoints.
1809// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
1810// add a test for an end-to-end test without MID signaling either (basically,
1811// the minimum acceptable SDP).
1812TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
1813 ASSERT_TRUE(CreatePeerConnectionWrappers());
1814 ConnectFakeSignaling();
1815 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08001816 caller()->AddAudioVideoTracks();
1817 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07001818 // Remove SSRCs and MSIDs from the received offer SDP.
1819 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07001820 caller()->CreateAndSetAndSignalOffer();
1821 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1822 ExpectNewFramesReceivedWithWait(
1823 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1824 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1825 kMaxWaitForFramesMs);
1826}
1827
1828// Test that if two video tracks are sent (from caller to callee, in this test),
1829// they're transmitted correctly end-to-end.
1830TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
1831 ASSERT_TRUE(CreatePeerConnectionWrappers());
1832 ConnectFakeSignaling();
1833 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08001834 caller()->AddAudioVideoTracks();
1835 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001836 caller()->CreateAndSetAndSignalOffer();
1837 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08001838 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001839 int expected_callee_received_frames = kDefaultExpectedVideoFrameCount;
1840 ExpectNewFramesReceivedWithWait(0, 0, 0, expected_callee_received_frames,
1841 kMaxWaitForFramesMs);
1842}
1843
1844static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
1845 bool first = true;
1846 for (cricket::ContentInfo& content : desc->contents()) {
1847 if (first) {
1848 first = false;
1849 continue;
1850 }
1851 content.bundle_only = true;
1852 }
1853 first = true;
1854 for (cricket::TransportInfo& transport : desc->transport_infos()) {
1855 if (first) {
1856 first = false;
1857 continue;
1858 }
1859 transport.description.ice_ufrag.clear();
1860 transport.description.ice_pwd.clear();
1861 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
1862 transport.description.identity_fingerprint.reset(nullptr);
1863 }
1864}
1865
1866// Test that if applying a true "max bundle" offer, which uses ports of 0,
1867// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
1868// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
1869// successfully and media flows.
1870// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
1871// TODO(deadbeef): Won't need this test once we start generating actual
1872// standards-compliant SDP.
1873TEST_F(PeerConnectionIntegrationTest,
1874 EndToEndCallWithSpecCompliantMaxBundleOffer) {
1875 ASSERT_TRUE(CreatePeerConnectionWrappers());
1876 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001877 caller()->AddAudioVideoTracks();
1878 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001879 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
1880 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
1881 // but the first m= section.
1882 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
1883 caller()->CreateAndSetAndSignalOffer();
1884 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1885 ExpectNewFramesReceivedWithWait(
1886 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1887 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1888 kMaxWaitForFramesMs);
1889}
1890
1891// Test that we can receive the audio output level from a remote audio track.
1892// TODO(deadbeef): Use a fake audio source and verify that the output level is
1893// exactly what the source on the other side was configured with.
deadbeefd8ad7882017-04-18 16:01:17 -07001894TEST_F(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07001895 ASSERT_TRUE(CreatePeerConnectionWrappers());
1896 ConnectFakeSignaling();
1897 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08001898 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001899 caller()->CreateAndSetAndSignalOffer();
1900 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1901
1902 // Get the audio output level stats. Note that the level is not available
1903 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07001904 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07001905 kMaxWaitForFramesMs);
1906}
1907
1908// Test that an audio input level is reported.
1909// TODO(deadbeef): Use a fake audio source and verify that the input level is
1910// exactly what the source was configured with.
deadbeefd8ad7882017-04-18 16:01:17 -07001911TEST_F(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07001912 ASSERT_TRUE(CreatePeerConnectionWrappers());
1913 ConnectFakeSignaling();
1914 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08001915 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001916 caller()->CreateAndSetAndSignalOffer();
1917 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1918
1919 // Get the audio input level stats. The level should be available very
1920 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07001921 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07001922 kMaxWaitForStatsMs);
1923}
1924
1925// Test that we can get incoming byte counts from both audio and video tracks.
deadbeefd8ad7882017-04-18 16:01:17 -07001926TEST_F(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07001927 ASSERT_TRUE(CreatePeerConnectionWrappers());
1928 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001929 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001930 // Do offer/answer, wait for the callee to receive some frames.
1931 caller()->CreateAndSetAndSignalOffer();
1932 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1933 int expected_caller_received_frames = 0;
1934 ExpectNewFramesReceivedWithWait(
1935 expected_caller_received_frames, expected_caller_received_frames,
1936 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1937 kMaxWaitForFramesMs);
1938
1939 // Get a handle to the remote tracks created, so they can be used as GetStats
1940 // filters.
Steve Anton15324772018-01-16 10:26:49 -08001941 for (auto receiver : callee()->pc()->GetReceivers()) {
1942 // We received frames, so we definitely should have nonzero "received bytes"
1943 // stats at this point.
1944 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
1945 0);
1946 }
deadbeef1dcb1642017-03-29 21:08:16 -07001947}
1948
1949// Test that we can get outgoing byte counts from both audio and video tracks.
deadbeefd8ad7882017-04-18 16:01:17 -07001950TEST_F(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07001951 ASSERT_TRUE(CreatePeerConnectionWrappers());
1952 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001953 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001954 auto audio_track = caller()->CreateLocalAudioTrack();
1955 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08001956 caller()->AddTrack(audio_track);
1957 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07001958 // Do offer/answer, wait for the callee to receive some frames.
1959 caller()->CreateAndSetAndSignalOffer();
1960 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1961 int expected_caller_received_frames = 0;
1962 ExpectNewFramesReceivedWithWait(
1963 expected_caller_received_frames, expected_caller_received_frames,
1964 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
1965 kMaxWaitForFramesMs);
1966
1967 // The callee received frames, so we definitely should have nonzero "sent
1968 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07001969 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
1970 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
1971}
1972
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02001973// Test that we can get capture start ntp time.
1974TEST_F(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
1975 ASSERT_TRUE(CreatePeerConnectionWrappers());
1976 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001977 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02001978
Steve Anton15324772018-01-16 10:26:49 -08001979 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02001980
1981 // Do offer/answer, wait for the callee to receive some frames.
1982 caller()->CreateAndSetAndSignalOffer();
1983 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1984
1985 // Get the remote audio track created on the receiver, so they can be used as
1986 // GetStats filters.
1987 StreamCollectionInterface* remote_streams = callee()->remote_streams();
1988 ASSERT_EQ(1u, remote_streams->count());
1989 ASSERT_EQ(1u, remote_streams->at(0)->GetAudioTracks().size());
1990 MediaStreamTrackInterface* remote_audio_track =
1991 remote_streams->at(0)->GetAudioTracks()[0];
1992
1993 // Get the audio output level stats. Note that the level is not available
1994 // until an RTCP packet has been received.
1995 EXPECT_TRUE_WAIT(callee()->OldGetStatsForTrack(remote_audio_track)->
1996 CaptureStartNtpTime() > 0, 2 * kMaxWaitForFramesMs);
1997}
1998
deadbeefd8ad7882017-04-18 16:01:17 -07001999// Test that we can get stats (using the new stats implemnetation) for
2000// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2001// SDP.
2002TEST_F(PeerConnectionIntegrationTest,
2003 GetStatsForUnsignaledStreamWithNewStatsApi) {
2004 ASSERT_TRUE(CreatePeerConnectionWrappers());
2005 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002006 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002007 // Remove SSRCs and MSIDs from the received offer SDP.
2008 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2009 caller()->CreateAndSetAndSignalOffer();
2010 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2011 // Wait for one audio frame to be received by the callee.
2012 ExpectNewFramesReceivedWithWait(0, 0, 1, 0, kMaxWaitForFramesMs);
2013
2014 // We received a frame, so we should have nonzero "bytes received" stats for
2015 // the unsignaled stream, if stats are working for it.
2016 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2017 callee()->NewGetStats();
2018 ASSERT_NE(nullptr, report);
2019 auto inbound_stream_stats =
2020 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2021 ASSERT_EQ(1U, inbound_stream_stats.size());
2022 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2023 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002024 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2025}
2026
2027// Test that we can successfully get the media related stats (audio level
2028// etc.) for the unsignaled stream.
2029TEST_F(PeerConnectionIntegrationTest,
2030 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2031 ASSERT_TRUE(CreatePeerConnectionWrappers());
2032 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002033 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002034 // Remove SSRCs and MSIDs from the received offer SDP.
2035 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2036 caller()->CreateAndSetAndSignalOffer();
2037 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2038 // Wait for one audio frame to be received by the callee.
2039 ExpectNewFramesReceivedWithWait(0, 0, 1, 1, kMaxWaitForFramesMs);
2040
2041 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2042 callee()->NewGetStats();
2043 ASSERT_NE(nullptr, report);
2044
2045 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2046 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2047 ASSERT_GE(audio_index, 0);
2048 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002049}
2050
deadbeef4e2deab2017-09-20 13:56:21 -07002051// Helper for test below.
2052void ModifySsrcs(cricket::SessionDescription* desc) {
2053 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -08002054 for (cricket::StreamParams& stream :
2055 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002056 for (uint32_t& ssrc : stream.ssrcs) {
2057 ssrc = rtc::CreateRandomId();
2058 }
2059 }
2060 }
2061}
2062
2063// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2064// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2065// This should result in two "RTCInboundRTPStreamStats", but only one
2066// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2067// being reset to 0 once the SSRC change occurs.
2068//
2069// Regression test for this bug:
2070// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2071//
2072// The bug causes the track stats to only represent one of the two streams:
2073// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2074// that the track stat counters would reset to 0 when the new stream is
2075// received, and a 50% chance that they'll stop updating (while
2076// "concealed_samples" continues increasing, due to silence being generated for
2077// the inactive stream).
2078TEST_F(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002079 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002080 ASSERT_TRUE(CreatePeerConnectionWrappers());
2081 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002082 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002083 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2084 // that doesn't signal SSRCs (from the callee's perspective).
2085 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2086 caller()->CreateAndSetAndSignalOffer();
2087 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2088 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
2089 ExpectNewFramesReceivedWithWait(0, 0, 25, 0, kMaxWaitForFramesMs);
2090
2091 // Some audio frames were received, so we should have nonzero "samples
2092 // received" for the track.
2093 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2094 callee()->NewGetStats();
2095 ASSERT_NE(nullptr, report);
2096 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2097 ASSERT_EQ(1U, track_stats.size());
2098 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2099 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2100 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2101
2102 // Create a new offer and munge it to cause the caller to use a new SSRC.
2103 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2104 caller()->CreateAndSetAndSignalOffer();
2105 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2106 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2107 // SSRC.
2108 ExpectNewFramesReceivedWithWait(0, 0, 25, 0, kMaxWaitForFramesMs);
2109
2110 report = callee()->NewGetStats();
2111 ASSERT_NE(nullptr, report);
2112 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2113 ASSERT_EQ(1U, track_stats.size());
2114 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2115 // The "total samples received" stat should only be greater than it was
2116 // before.
2117 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2118 // Right now, the new SSRC will cause the counters to reset to 0.
2119 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2120
2121 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002122 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002123 // good sign that we're seeing stats from the old stream that's no longer
2124 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002125 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002126 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2127 EXPECT_LT(*track_stats[0]->concealed_samples,
2128 *track_stats[0]->total_samples_received *
2129 kAcceptableConcealedSamplesPercentage);
2130
2131 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2132 // sanity check that the SSRC really changed.
2133 // TODO(deadbeef): This isn't working right now, because we're not returning
2134 // *any* stats for the inactive stream. Uncomment when the bug is completely
2135 // fixed.
2136 // auto inbound_stream_stats =
2137 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2138 // ASSERT_EQ(2U, inbound_stream_stats.size());
2139}
2140
deadbeef1dcb1642017-03-29 21:08:16 -07002141// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
2142TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
2143 PeerConnectionFactory::Options dtls_10_options;
2144 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2145 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2146 dtls_10_options));
2147 ConnectFakeSignaling();
2148 // Do normal offer/answer and wait for some frames to be received in each
2149 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002150 caller()->AddAudioVideoTracks();
2151 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002152 caller()->CreateAndSetAndSignalOffer();
2153 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2154 ExpectNewFramesReceivedWithWait(
2155 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2156 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2157 kMaxWaitForFramesMs);
2158}
2159
2160// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
2161TEST_F(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
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 // Register UMA observer before signaling begins.
2168 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2169 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2170 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002171 caller()->AddAudioVideoTracks();
2172 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002173 caller()->CreateAndSetAndSignalOffer();
2174 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2175 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002176 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002177 kDefaultTimeout);
2178 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002179 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002180 EXPECT_EQ(1,
2181 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2182 kDefaultSrtpCryptoSuite));
2183}
2184
2185// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
2186TEST_F(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
2187 PeerConnectionFactory::Options dtls_12_options;
2188 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2189 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2190 dtls_12_options));
2191 ConnectFakeSignaling();
2192 // Register UMA observer before signaling begins.
2193 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2194 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2195 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002196 caller()->AddAudioVideoTracks();
2197 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002198 caller()->CreateAndSetAndSignalOffer();
2199 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2200 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002201 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002202 kDefaultTimeout);
2203 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002204 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002205 EXPECT_EQ(1,
2206 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2207 kDefaultSrtpCryptoSuite));
2208}
2209
2210// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2211// callee only supports 1.0.
2212TEST_F(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
2213 PeerConnectionFactory::Options caller_options;
2214 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2215 PeerConnectionFactory::Options callee_options;
2216 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2217 ASSERT_TRUE(
2218 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2219 ConnectFakeSignaling();
2220 // Do normal offer/answer and wait for some frames to be received in each
2221 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002222 caller()->AddAudioVideoTracks();
2223 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002224 caller()->CreateAndSetAndSignalOffer();
2225 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2226 ExpectNewFramesReceivedWithWait(
2227 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2228 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2229 kMaxWaitForFramesMs);
2230}
2231
2232// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2233// callee supports 1.2.
2234TEST_F(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
2235 PeerConnectionFactory::Options caller_options;
2236 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2237 PeerConnectionFactory::Options callee_options;
2238 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2239 ASSERT_TRUE(
2240 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2241 ConnectFakeSignaling();
2242 // Do normal offer/answer and wait for some frames to be received in each
2243 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002244 caller()->AddAudioVideoTracks();
2245 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002246 caller()->CreateAndSetAndSignalOffer();
2247 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2248 ExpectNewFramesReceivedWithWait(
2249 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2250 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2251 kMaxWaitForFramesMs);
2252}
2253
2254// Test that a non-GCM cipher is used if both sides only support non-GCM.
2255TEST_F(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
2256 bool local_gcm_enabled = false;
2257 bool remote_gcm_enabled = false;
2258 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2259 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2260 expected_cipher_suite);
2261}
2262
2263// Test that a GCM cipher is used if both ends support it.
2264TEST_F(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
2265 bool local_gcm_enabled = true;
2266 bool remote_gcm_enabled = true;
2267 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2268 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2269 expected_cipher_suite);
2270}
2271
2272// Test that GCM isn't used if only the offerer supports it.
2273TEST_F(PeerConnectionIntegrationTest,
2274 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2275 bool local_gcm_enabled = true;
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 GCM isn't used if only the answerer supports it.
2283TEST_F(PeerConnectionIntegrationTest,
2284 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2285 bool local_gcm_enabled = false;
2286 bool remote_gcm_enabled = true;
2287 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2288 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2289 expected_cipher_suite);
2290}
2291
deadbeef7914b8c2017-04-21 03:23:33 -07002292// Verify that media can be transmitted end-to-end when GCM crypto suites are
2293// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2294// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2295// works with it.
2296TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
2297 PeerConnectionFactory::Options gcm_options;
2298 gcm_options.crypto_options.enable_gcm_crypto_suites = true;
2299 ASSERT_TRUE(
2300 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2301 ConnectFakeSignaling();
2302 // Do normal offer/answer and wait for some frames to be received in each
2303 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002304 caller()->AddAudioVideoTracks();
2305 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002306 caller()->CreateAndSetAndSignalOffer();
2307 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2308 ExpectNewFramesReceivedWithWait(
2309 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2310 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2311 kMaxWaitForFramesMs);
2312}
2313
deadbeef1dcb1642017-03-29 21:08:16 -07002314// This test sets up a call between two parties with audio, video and an RTP
2315// data channel.
2316TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
2317 FakeConstraints setup_constraints;
2318 setup_constraints.SetAllowRtpDataChannels();
2319 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2320 &setup_constraints));
2321 ConnectFakeSignaling();
2322 // Expect that data channel created on caller side will show up for callee as
2323 // well.
2324 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002325 caller()->AddAudioVideoTracks();
2326 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002327 caller()->CreateAndSetAndSignalOffer();
2328 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2329 // Ensure the existence of the RTP data channel didn't impede audio/video.
2330 ExpectNewFramesReceivedWithWait(
2331 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2332 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2333 kMaxWaitForFramesMs);
2334 ASSERT_NE(nullptr, caller()->data_channel());
2335 ASSERT_NE(nullptr, callee()->data_channel());
2336 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2337 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2338
2339 // Ensure data can be sent in both directions.
2340 std::string data = "hello world";
2341 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2342 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2343 kDefaultTimeout);
2344 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2345 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2346 kDefaultTimeout);
2347}
2348
2349// Ensure that an RTP data channel is signaled as closed for the caller when
2350// the callee rejects it in a subsequent offer.
2351TEST_F(PeerConnectionIntegrationTest,
2352 RtpDataChannelSignaledClosedInCalleeOffer) {
2353 // Same procedure as above test.
2354 FakeConstraints setup_constraints;
2355 setup_constraints.SetAllowRtpDataChannels();
2356 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2357 &setup_constraints));
2358 ConnectFakeSignaling();
2359 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002360 caller()->AddAudioVideoTracks();
2361 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002362 caller()->CreateAndSetAndSignalOffer();
2363 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2364 ASSERT_NE(nullptr, caller()->data_channel());
2365 ASSERT_NE(nullptr, callee()->data_channel());
2366 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2367 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2368
2369 // Close the data channel on the callee, and do an updated offer/answer.
2370 callee()->data_channel()->Close();
2371 callee()->CreateAndSetAndSignalOffer();
2372 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2373 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2374 EXPECT_FALSE(callee()->data_observer()->IsOpen());
2375}
2376
2377// Tests that data is buffered in an RTP data channel until an observer is
2378// registered for it.
2379//
2380// NOTE: RTP data channels can receive data before the underlying
2381// transport has detected that a channel is writable and thus data can be
2382// received before the data channel state changes to open. That is hard to test
2383// but the same buffering is expected to be used in that case.
2384TEST_F(PeerConnectionIntegrationTest,
2385 DataBufferedUntilRtpDataChannelObserverRegistered) {
2386 // Use fake clock and simulated network delay so that we predictably can wait
2387 // until an SCTP message has been delivered without "sleep()"ing.
2388 rtc::ScopedFakeClock fake_clock;
2389 // Some things use a time of "0" as a special value, so we need to start out
2390 // the fake clock at a nonzero time.
2391 // TODO(deadbeef): Fix this.
2392 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
2393 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
2394 virtual_socket_server()->UpdateDelayDistribution();
2395
2396 FakeConstraints constraints;
2397 constraints.SetAllowRtpDataChannels();
2398 ASSERT_TRUE(
2399 CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints));
2400 ConnectFakeSignaling();
2401 caller()->CreateDataChannel();
2402 caller()->CreateAndSetAndSignalOffer();
2403 ASSERT_TRUE(caller()->data_channel() != nullptr);
2404 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
2405 kDefaultTimeout, fake_clock);
2406 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
2407 kDefaultTimeout, fake_clock);
2408 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
2409 callee()->data_channel()->state(), kDefaultTimeout,
2410 fake_clock);
2411
2412 // Unregister the observer which is normally automatically registered.
2413 callee()->data_channel()->UnregisterObserver();
2414 // Send data and advance fake clock until it should have been received.
2415 std::string data = "hello world";
2416 caller()->data_channel()->Send(DataBuffer(data));
2417 SIMULATED_WAIT(false, 50, fake_clock);
2418
2419 // Attach data channel and expect data to be received immediately. Note that
2420 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
2421 // further, but data can be received even if the callback is asynchronous.
2422 MockDataChannelObserver new_observer(callee()->data_channel());
2423 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
2424 fake_clock);
2425}
2426
2427// This test sets up a call between two parties with audio, video and but only
2428// the caller client supports RTP data channels.
2429TEST_F(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
2430 FakeConstraints setup_constraints_1;
2431 setup_constraints_1.SetAllowRtpDataChannels();
2432 // Must disable DTLS to make negotiation succeed.
2433 setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2434 false);
2435 FakeConstraints setup_constraints_2;
2436 setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2437 false);
2438 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(
2439 &setup_constraints_1, &setup_constraints_2));
2440 ConnectFakeSignaling();
2441 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002442 caller()->AddAudioVideoTracks();
2443 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002444 caller()->CreateAndSetAndSignalOffer();
2445 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2446 // The caller should still have a data channel, but it should be closed, and
2447 // one should ever have been created for the callee.
2448 EXPECT_TRUE(caller()->data_channel() != nullptr);
2449 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2450 EXPECT_EQ(nullptr, callee()->data_channel());
2451}
2452
2453// This test sets up a call between two parties with audio, and video. When
2454// audio and video is setup and flowing, an RTP data channel is negotiated.
2455TEST_F(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
2456 FakeConstraints setup_constraints;
2457 setup_constraints.SetAllowRtpDataChannels();
2458 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2459 &setup_constraints));
2460 ConnectFakeSignaling();
2461 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08002462 caller()->AddAudioVideoTracks();
2463 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002464 caller()->CreateAndSetAndSignalOffer();
2465 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2466 // Create data channel and do new offer and answer.
2467 caller()->CreateDataChannel();
2468 caller()->CreateAndSetAndSignalOffer();
2469 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2470 ASSERT_NE(nullptr, caller()->data_channel());
2471 ASSERT_NE(nullptr, callee()->data_channel());
2472 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2473 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2474 // Ensure data can be sent in both directions.
2475 std::string data = "hello world";
2476 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2477 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2478 kDefaultTimeout);
2479 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2480 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2481 kDefaultTimeout);
2482}
2483
2484#ifdef HAVE_SCTP
2485
2486// This test sets up a call between two parties with audio, video and an SCTP
2487// data channel.
2488TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
2489 ASSERT_TRUE(CreatePeerConnectionWrappers());
2490 ConnectFakeSignaling();
2491 // Expect that data channel created on caller side will show up for callee as
2492 // well.
2493 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002494 caller()->AddAudioVideoTracks();
2495 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002496 caller()->CreateAndSetAndSignalOffer();
2497 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2498 // Ensure the existence of the SCTP data channel didn't impede audio/video.
2499 ExpectNewFramesReceivedWithWait(
2500 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2501 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2502 kMaxWaitForFramesMs);
2503 // Caller data channel should already exist (it created one). Callee data
2504 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2505 ASSERT_NE(nullptr, caller()->data_channel());
2506 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2507 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2508 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2509
2510 // Ensure data can be sent in both directions.
2511 std::string data = "hello world";
2512 caller()->data_channel()->Send(DataBuffer(data));
2513 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2514 kDefaultTimeout);
2515 callee()->data_channel()->Send(DataBuffer(data));
2516 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2517 kDefaultTimeout);
2518}
2519
2520// Ensure that when the callee closes an SCTP data channel, the closing
2521// procedure results in the data channel being closed for the caller as well.
2522TEST_F(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
2523 // Same procedure as above test.
2524 ASSERT_TRUE(CreatePeerConnectionWrappers());
2525 ConnectFakeSignaling();
2526 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002527 caller()->AddAudioVideoTracks();
2528 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002529 caller()->CreateAndSetAndSignalOffer();
2530 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2531 ASSERT_NE(nullptr, caller()->data_channel());
2532 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2533 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2534 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2535
2536 // Close the data channel on the callee side, and wait for it to reach the
2537 // "closed" state on both sides.
2538 callee()->data_channel()->Close();
2539 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
2540 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
2541}
2542
Steve Antonda6c0952017-10-23 11:41:54 -07002543TEST_F(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
2544 ASSERT_TRUE(CreatePeerConnectionWrappers());
2545 ConnectFakeSignaling();
2546 webrtc::DataChannelInit init;
2547 init.id = 53;
2548 init.maxRetransmits = 52;
2549 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08002550 caller()->AddAudioVideoTracks();
2551 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07002552 caller()->CreateAndSetAndSignalOffer();
2553 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07002554 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2555 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07002556 EXPECT_EQ(init.id, callee()->data_channel()->id());
2557 EXPECT_EQ("data-channel", callee()->data_channel()->label());
2558 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
2559 EXPECT_FALSE(callee()->data_channel()->negotiated());
2560}
2561
deadbeef1dcb1642017-03-29 21:08:16 -07002562// Test usrsctp's ability to process unordered data stream, where data actually
2563// arrives out of order using simulated delays. Previously there have been some
2564// bugs in this area.
2565TEST_F(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
2566 // Introduce random network delays.
2567 // Otherwise it's not a true "unordered" test.
2568 virtual_socket_server()->set_delay_mean(20);
2569 virtual_socket_server()->set_delay_stddev(5);
2570 virtual_socket_server()->UpdateDelayDistribution();
2571 // Normal procedure, but with unordered data channel config.
2572 ASSERT_TRUE(CreatePeerConnectionWrappers());
2573 ConnectFakeSignaling();
2574 webrtc::DataChannelInit init;
2575 init.ordered = false;
2576 caller()->CreateDataChannel(&init);
2577 caller()->CreateAndSetAndSignalOffer();
2578 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2579 ASSERT_NE(nullptr, caller()->data_channel());
2580 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2581 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2582 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2583
2584 static constexpr int kNumMessages = 100;
2585 // Deliberately chosen to be larger than the MTU so messages get fragmented.
2586 static constexpr size_t kMaxMessageSize = 4096;
2587 // Create and send random messages.
2588 std::vector<std::string> sent_messages;
2589 for (int i = 0; i < kNumMessages; ++i) {
2590 size_t length =
2591 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
2592 std::string message;
2593 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
2594 caller()->data_channel()->Send(DataBuffer(message));
2595 callee()->data_channel()->Send(DataBuffer(message));
2596 sent_messages.push_back(message);
2597 }
2598
2599 // Wait for all messages to be received.
2600 EXPECT_EQ_WAIT(kNumMessages,
2601 caller()->data_observer()->received_message_count(),
2602 kDefaultTimeout);
2603 EXPECT_EQ_WAIT(kNumMessages,
2604 callee()->data_observer()->received_message_count(),
2605 kDefaultTimeout);
2606
2607 // Sort and compare to make sure none of the messages were corrupted.
2608 std::vector<std::string> caller_received_messages =
2609 caller()->data_observer()->messages();
2610 std::vector<std::string> callee_received_messages =
2611 callee()->data_observer()->messages();
2612 std::sort(sent_messages.begin(), sent_messages.end());
2613 std::sort(caller_received_messages.begin(), caller_received_messages.end());
2614 std::sort(callee_received_messages.begin(), callee_received_messages.end());
2615 EXPECT_EQ(sent_messages, caller_received_messages);
2616 EXPECT_EQ(sent_messages, callee_received_messages);
2617}
2618
2619// This test sets up a call between two parties with audio, and video. When
2620// audio and video are setup and flowing, an SCTP data channel is negotiated.
2621TEST_F(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
2622 ASSERT_TRUE(CreatePeerConnectionWrappers());
2623 ConnectFakeSignaling();
2624 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08002625 caller()->AddAudioVideoTracks();
2626 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002627 caller()->CreateAndSetAndSignalOffer();
2628 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2629 // Create data channel and do new offer and answer.
2630 caller()->CreateDataChannel();
2631 caller()->CreateAndSetAndSignalOffer();
2632 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2633 // Caller data channel should already exist (it created one). Callee data
2634 // channel may not exist yet, since negotiation happens in-band, not in SDP.
2635 ASSERT_NE(nullptr, caller()->data_channel());
2636 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2637 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2638 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2639 // Ensure data can be sent in both directions.
2640 std::string data = "hello world";
2641 caller()->data_channel()->Send(DataBuffer(data));
2642 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2643 kDefaultTimeout);
2644 callee()->data_channel()->Send(DataBuffer(data));
2645 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2646 kDefaultTimeout);
2647}
2648
deadbeef7914b8c2017-04-21 03:23:33 -07002649// Set up a connection initially just using SCTP data channels, later upgrading
2650// to audio/video, ensuring frames are received end-to-end. Effectively the
2651// inverse of the test above.
2652// This was broken in M57; see https://crbug.com/711243
2653TEST_F(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
2654 ASSERT_TRUE(CreatePeerConnectionWrappers());
2655 ConnectFakeSignaling();
2656 // Do initial offer/answer with just data channel.
2657 caller()->CreateDataChannel();
2658 caller()->CreateAndSetAndSignalOffer();
2659 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2660 // Wait until data can be sent over the data channel.
2661 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2662 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2663 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2664
2665 // Do subsequent offer/answer with two-way audio and video. Audio and video
2666 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08002667 caller()->AddAudioVideoTracks();
2668 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002669 caller()->CreateAndSetAndSignalOffer();
2670 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2671 ExpectNewFramesReceivedWithWait(
2672 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2673 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
2674 kMaxWaitForFramesMs);
2675}
2676
deadbeef8b7e9ad2017-05-25 09:38:55 -07002677static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07002678 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08002679 GetFirstDataContentDescription(desc);
2680 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07002681 dcd_offer->set_use_sctpmap(false);
2682 dcd_offer->set_protocol("UDP/DTLS/SCTP");
2683}
2684
2685// Test that the data channel works when a spec-compliant SCTP m= section is
2686// offered (using "a=sctp-port" instead of "a=sctpmap", and using
2687// "UDP/DTLS/SCTP" as the protocol).
2688TEST_F(PeerConnectionIntegrationTest,
2689 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
2690 ASSERT_TRUE(CreatePeerConnectionWrappers());
2691 ConnectFakeSignaling();
2692 caller()->CreateDataChannel();
2693 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
2694 caller()->CreateAndSetAndSignalOffer();
2695 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2696 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
2697 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2698 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2699
2700 // Ensure data can be sent in both directions.
2701 std::string data = "hello world";
2702 caller()->data_channel()->Send(DataBuffer(data));
2703 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2704 kDefaultTimeout);
2705 callee()->data_channel()->Send(DataBuffer(data));
2706 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2707 kDefaultTimeout);
2708}
2709
deadbeef1dcb1642017-03-29 21:08:16 -07002710#endif // HAVE_SCTP
2711
2712// Test that the ICE connection and gathering states eventually reach
2713// "complete".
2714TEST_F(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
2715 ASSERT_TRUE(CreatePeerConnectionWrappers());
2716 ConnectFakeSignaling();
2717 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08002718 caller()->AddAudioVideoTracks();
2719 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002720 caller()->CreateAndSetAndSignalOffer();
2721 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2722 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
2723 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
2724 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
2725 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
2726 // After the best candidate pair is selected and all candidates are signaled,
2727 // the ICE connection state should reach "complete".
2728 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
2729 // answerer/"callee" by default) only reaches "connected". When this is
2730 // fixed, this test should be updated.
2731 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2732 caller()->ice_connection_state(), kDefaultTimeout);
2733 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2734 callee()->ice_connection_state(), kDefaultTimeout);
2735}
2736
Steve Antonede9ca52017-10-16 13:04:27 -07002737// Test that firewalling the ICE connection causes the clients to identify the
2738// disconnected state and then removing the firewall causes them to reconnect.
2739class PeerConnectionIntegrationIceStatesTest
2740 : public PeerConnectionIntegrationTest,
2741 public ::testing::WithParamInterface<std::tuple<std::string, uint32_t>> {
2742 protected:
2743 PeerConnectionIntegrationIceStatesTest() {
2744 port_allocator_flags_ = std::get<1>(GetParam());
2745 }
2746
2747 void StartStunServer(const SocketAddress& server_address) {
2748 stun_server_.reset(
2749 cricket::TestStunServer::Create(network_thread(), server_address));
2750 }
2751
2752 bool TestIPv6() {
2753 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
2754 }
2755
2756 void SetPortAllocatorFlags() {
2757 caller()->port_allocator()->set_flags(port_allocator_flags_);
2758 callee()->port_allocator()->set_flags(port_allocator_flags_);
2759 }
2760
2761 std::vector<SocketAddress> CallerAddresses() {
2762 std::vector<SocketAddress> addresses;
2763 addresses.push_back(SocketAddress("1.1.1.1", 0));
2764 if (TestIPv6()) {
2765 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
2766 }
2767 return addresses;
2768 }
2769
2770 std::vector<SocketAddress> CalleeAddresses() {
2771 std::vector<SocketAddress> addresses;
2772 addresses.push_back(SocketAddress("2.2.2.2", 0));
2773 if (TestIPv6()) {
2774 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
2775 }
2776 return addresses;
2777 }
2778
2779 void SetUpNetworkInterfaces() {
2780 // Remove the default interfaces added by the test infrastructure.
2781 caller()->network()->RemoveInterface(kDefaultLocalAddress);
2782 callee()->network()->RemoveInterface(kDefaultLocalAddress);
2783
2784 // Add network addresses for test.
2785 for (const auto& caller_address : CallerAddresses()) {
2786 caller()->network()->AddInterface(caller_address);
2787 }
2788 for (const auto& callee_address : CalleeAddresses()) {
2789 callee()->network()->AddInterface(callee_address);
2790 }
2791 }
2792
2793 private:
2794 uint32_t port_allocator_flags_;
2795 std::unique_ptr<cricket::TestStunServer> stun_server_;
2796};
2797
2798// Tests that the PeerConnection goes through all the ICE gathering/connection
2799// states over the duration of the call. This includes Disconnected and Failed
2800// states, induced by putting a firewall between the peers and waiting for them
2801// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08002802TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
2803 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
2804 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
2805 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07002806
2807 const SocketAddress kStunServerAddress =
2808 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
2809 StartStunServer(kStunServerAddress);
2810
2811 PeerConnectionInterface::RTCConfiguration config;
2812 PeerConnectionInterface::IceServer ice_stun_server;
2813 ice_stun_server.urls.push_back(
2814 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
2815 kStunServerAddress.PortAsString());
2816 config.servers.push_back(ice_stun_server);
2817
2818 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
2819 ConnectFakeSignaling();
2820 SetPortAllocatorFlags();
2821 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08002822 caller()->AddAudioVideoTracks();
2823 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07002824
2825 // Initial state before anything happens.
2826 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
2827 caller()->ice_gathering_state());
2828 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
2829 caller()->ice_connection_state());
2830
2831 // Start the call by creating the offer, setting it as the local description,
2832 // then sending it to the peer who will respond with an answer. This happens
2833 // asynchronously so that we can watch the states as it runs in the
2834 // background.
2835 caller()->CreateAndSetAndSignalOffer();
2836
Steve Anton83119dd2017-11-10 16:19:52 -08002837 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
2838 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07002839
2840 // Verify that the observer was notified of the intermediate transitions.
2841 EXPECT_THAT(caller()->ice_connection_state_history(),
2842 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
2843 PeerConnectionInterface::kIceConnectionConnected,
2844 PeerConnectionInterface::kIceConnectionCompleted));
2845 EXPECT_THAT(caller()->ice_gathering_state_history(),
2846 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
2847 PeerConnectionInterface::kIceGatheringComplete));
2848
2849 // Block connections to/from the caller and wait for ICE to become
2850 // disconnected.
2851 for (const auto& caller_address : CallerAddresses()) {
2852 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
2853 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01002854 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08002855 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
2856 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07002857
2858 // Let ICE re-establish by removing the firewall rules.
2859 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01002860 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08002861 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
2862 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07002863
2864 // According to RFC7675, if there is no response within 30 seconds then the
2865 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08002866 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07002867 constexpr int kConsentTimeout = 30000;
2868 for (const auto& caller_address : CallerAddresses()) {
2869 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
2870 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01002871 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08002872 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
2873 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07002874}
2875
2876// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
2877// and that the statistics in the metric observers are updated correctly.
2878TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
2879 ASSERT_TRUE(CreatePeerConnectionWrappers());
2880 ConnectFakeSignaling();
2881 SetPortAllocatorFlags();
2882 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08002883 caller()->AddAudioVideoTracks();
2884 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07002885
2886 rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer(
2887 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>());
2888 caller()->pc()->RegisterUMAObserver(metrics_observer.get());
2889
2890 caller()->CreateAndSetAndSignalOffer();
2891
2892 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2893
2894 const int num_best_ipv4 = metrics_observer->GetEnumCounter(
2895 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv4);
2896 const int num_best_ipv6 = metrics_observer->GetEnumCounter(
2897 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv6);
2898 if (TestIPv6()) {
2899 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
2900 // connection.
2901 EXPECT_EQ(0u, num_best_ipv4);
2902 EXPECT_EQ(1u, num_best_ipv6);
2903 } else {
2904 EXPECT_EQ(1u, num_best_ipv4);
2905 EXPECT_EQ(0u, num_best_ipv6);
2906 }
2907
2908 EXPECT_EQ(0u, metrics_observer->GetEnumCounter(
2909 webrtc::kEnumCounterIceCandidatePairTypeUdp,
2910 webrtc::kIceCandidatePairHostHost));
2911 EXPECT_EQ(1u, metrics_observer->GetEnumCounter(
2912 webrtc::kEnumCounterIceCandidatePairTypeUdp,
2913 webrtc::kIceCandidatePairHostPublicHostPublic));
2914}
2915
2916constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
2917 cricket::PORTALLOCATOR_DISABLE_STUN |
2918 cricket::PORTALLOCATOR_DISABLE_RELAY;
2919constexpr uint32_t kFlagsIPv6NoStun =
2920 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
2921 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
2922constexpr uint32_t kFlagsIPv4Stun =
2923 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
2924
2925INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
2926 PeerConnectionIntegrationIceStatesTest,
2927 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
2928 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
2929 std::make_pair("IPv4 with STUN",
2930 kFlagsIPv4Stun)));
2931
deadbeef1dcb1642017-03-29 21:08:16 -07002932// This test sets up a call between two parties with audio and video.
2933// During the call, the caller restarts ICE and the test verifies that
2934// new ICE candidates are generated and audio and video still can flow, and the
2935// ICE state reaches completed again.
2936TEST_F(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
2937 ASSERT_TRUE(CreatePeerConnectionWrappers());
2938 ConnectFakeSignaling();
2939 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08002940 caller()->AddAudioVideoTracks();
2941 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002942 caller()->CreateAndSetAndSignalOffer();
2943 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2944 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2945 caller()->ice_connection_state(), kMaxWaitForFramesMs);
2946 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2947 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2948
2949 // To verify that the ICE restart actually occurs, get
2950 // ufrag/password/candidates before and after restart.
2951 // Create an SDP string of the first audio candidate for both clients.
2952 const webrtc::IceCandidateCollection* audio_candidates_caller =
2953 caller()->pc()->local_description()->candidates(0);
2954 const webrtc::IceCandidateCollection* audio_candidates_callee =
2955 callee()->pc()->local_description()->candidates(0);
2956 ASSERT_GT(audio_candidates_caller->count(), 0u);
2957 ASSERT_GT(audio_candidates_callee->count(), 0u);
2958 std::string caller_candidate_pre_restart;
2959 ASSERT_TRUE(
2960 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
2961 std::string callee_candidate_pre_restart;
2962 ASSERT_TRUE(
2963 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
2964 const cricket::SessionDescription* desc =
2965 caller()->pc()->local_description()->description();
2966 std::string caller_ufrag_pre_restart =
2967 desc->transport_infos()[0].description.ice_ufrag;
2968 desc = callee()->pc()->local_description()->description();
2969 std::string callee_ufrag_pre_restart =
2970 desc->transport_infos()[0].description.ice_ufrag;
2971
2972 // Have the caller initiate an ICE restart.
2973 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2974 caller()->CreateAndSetAndSignalOffer();
2975 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2976 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
2977 caller()->ice_connection_state(), kMaxWaitForFramesMs);
2978 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
2979 callee()->ice_connection_state(), kMaxWaitForFramesMs);
2980
2981 // Grab the ufrags/candidates again.
2982 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
2983 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
2984 ASSERT_GT(audio_candidates_caller->count(), 0u);
2985 ASSERT_GT(audio_candidates_callee->count(), 0u);
2986 std::string caller_candidate_post_restart;
2987 ASSERT_TRUE(
2988 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
2989 std::string callee_candidate_post_restart;
2990 ASSERT_TRUE(
2991 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
2992 desc = caller()->pc()->local_description()->description();
2993 std::string caller_ufrag_post_restart =
2994 desc->transport_infos()[0].description.ice_ufrag;
2995 desc = callee()->pc()->local_description()->description();
2996 std::string callee_ufrag_post_restart =
2997 desc->transport_infos()[0].description.ice_ufrag;
2998 // Sanity check that an ICE restart was actually negotiated in SDP.
2999 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3000 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3001 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3002 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3003
3004 // Ensure that additional frames are received after the ICE restart.
3005 ExpectNewFramesReceivedWithWait(
3006 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3007 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3008 kMaxWaitForFramesMs);
3009}
3010
3011// Verify that audio/video can be received end-to-end when ICE renomination is
3012// enabled.
3013TEST_F(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
3014 PeerConnectionInterface::RTCConfiguration config;
3015 config.enable_ice_renomination = true;
3016 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3017 ConnectFakeSignaling();
3018 // Do normal offer/answer and wait for some frames to be received in each
3019 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003020 caller()->AddAudioVideoTracks();
3021 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003022 caller()->CreateAndSetAndSignalOffer();
3023 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3024 // Sanity check that ICE renomination was actually negotiated.
3025 const cricket::SessionDescription* desc =
3026 caller()->pc()->local_description()->description();
3027 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003028 ASSERT_NE(
3029 info.description.transport_options.end(),
3030 std::find(info.description.transport_options.begin(),
3031 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003032 }
3033 desc = callee()->pc()->local_description()->description();
3034 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003035 ASSERT_NE(
3036 info.description.transport_options.end(),
3037 std::find(info.description.transport_options.begin(),
3038 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003039 }
3040 ExpectNewFramesReceivedWithWait(
3041 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3042 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3043 kMaxWaitForFramesMs);
3044}
3045
Steve Anton6f25b092017-10-23 09:39:20 -07003046// With a max bundle policy and RTCP muxing, adding a new media description to
3047// the connection should not affect ICE at all because the new media will use
3048// the existing connection.
3049TEST_F(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003050 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07003051 PeerConnectionInterface::RTCConfiguration config;
3052 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3053 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3054 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3055 config, PeerConnectionInterface::RTCConfiguration()));
3056 ConnectFakeSignaling();
3057
Steve Anton15324772018-01-16 10:26:49 -08003058 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003059 caller()->CreateAndSetAndSignalOffer();
3060 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07003061 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3062 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07003063
3064 caller()->clear_ice_connection_state_history();
3065
Steve Anton15324772018-01-16 10:26:49 -08003066 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003067 caller()->CreateAndSetAndSignalOffer();
3068 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3069
3070 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
3071}
3072
deadbeef1dcb1642017-03-29 21:08:16 -07003073// This test sets up a call between two parties with audio and video. It then
3074// renegotiates setting the video m-line to "port 0", then later renegotiates
3075// again, enabling video.
3076TEST_F(PeerConnectionIntegrationTest,
3077 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
3078 ASSERT_TRUE(CreatePeerConnectionWrappers());
3079 ConnectFakeSignaling();
3080
3081 // Do initial negotiation, only sending media from the caller. Will result in
3082 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08003083 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003084 caller()->CreateAndSetAndSignalOffer();
3085 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3086
3087 // Negotiate again, disabling the video "m=" section (the callee will set the
3088 // port to 0 due to offer_to_receive_video = 0).
3089 PeerConnectionInterface::RTCOfferAnswerOptions options;
3090 options.offer_to_receive_video = 0;
3091 callee()->SetOfferAnswerOptions(options);
3092 caller()->CreateAndSetAndSignalOffer();
3093 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3094 // Sanity check that video "m=" section was actually rejected.
3095 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
3096 callee()->pc()->local_description()->description());
3097 ASSERT_NE(nullptr, answer_video_content);
3098 ASSERT_TRUE(answer_video_content->rejected);
3099
3100 // Enable video and do negotiation again, making sure video is received
3101 // end-to-end, also adding media stream to callee.
3102 options.offer_to_receive_video = 1;
3103 callee()->SetOfferAnswerOptions(options);
Steve Anton15324772018-01-16 10:26:49 -08003104 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003105 caller()->CreateAndSetAndSignalOffer();
3106 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3107 // Verify the caller receives frames from the newly added stream, and the
3108 // callee receives additional frames from the re-enabled video m= section.
3109 ExpectNewFramesReceivedWithWait(
3110 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3111 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3112 kMaxWaitForFramesMs);
3113}
3114
3115// This test sets up a Jsep call between two parties with external
3116// VideoDecoderFactory.
3117// TODO(holmer): Disabled due to sometimes crashing on buildbots.
3118// See issue webrtc/2378.
3119TEST_F(PeerConnectionIntegrationTest,
3120 DISABLED_EndToEndCallWithVideoDecoderFactory) {
3121 ASSERT_TRUE(CreatePeerConnectionWrappers());
3122 EnableVideoDecoderFactory();
3123 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003124 caller()->AddAudioVideoTracks();
3125 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003126 caller()->CreateAndSetAndSignalOffer();
3127 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3128 ExpectNewFramesReceivedWithWait(
3129 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3130 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3131 kMaxWaitForFramesMs);
3132}
3133
3134// This tests that if we negotiate after calling CreateSender but before we
3135// have a track, then set a track later, frames from the newly-set track are
3136// received end-to-end.
3137// TODO(deadbeef): Change this test to use AddTransceiver, once that's
3138// implemented.
3139TEST_F(PeerConnectionIntegrationTest,
3140 MediaFlowsAfterEarlyWarmupWithCreateSender) {
3141 ASSERT_TRUE(CreatePeerConnectionWrappers());
3142 ConnectFakeSignaling();
3143 auto caller_audio_sender =
3144 caller()->pc()->CreateSender("audio", "caller_stream");
3145 auto caller_video_sender =
3146 caller()->pc()->CreateSender("video", "caller_stream");
3147 auto callee_audio_sender =
3148 callee()->pc()->CreateSender("audio", "callee_stream");
3149 auto callee_video_sender =
3150 callee()->pc()->CreateSender("video", "callee_stream");
3151 caller()->CreateAndSetAndSignalOffer();
3152 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3153 // Wait for ICE to complete, without any tracks being set.
3154 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3155 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3156 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3157 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3158 // Now set the tracks, and expect frames to immediately start flowing.
3159 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3160 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3161 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3162 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
3163 ExpectNewFramesReceivedWithWait(
3164 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3165 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3166 kMaxWaitForFramesMs);
3167}
3168
3169// This test verifies that a remote video track can be added via AddStream,
3170// and sent end-to-end. For this particular test, it's simply echoed back
3171// from the caller to the callee, rather than being forwarded to a third
3172// PeerConnection.
3173TEST_F(PeerConnectionIntegrationTest, CanSendRemoteVideoTrack) {
3174 ASSERT_TRUE(CreatePeerConnectionWrappers());
3175 ConnectFakeSignaling();
3176 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08003177 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003178 caller()->CreateAndSetAndSignalOffer();
3179 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3180 ASSERT_EQ(1, callee()->remote_streams()->count());
3181
3182 // Echo the stream back, and do a new offer/anwer (initiated by callee this
3183 // time).
3184 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
3185 callee()->CreateAndSetAndSignalOffer();
3186 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3187
3188 int expected_caller_received_video_frames = kDefaultExpectedVideoFrameCount;
3189 ExpectNewFramesReceivedWithWait(0, expected_caller_received_video_frames, 0,
3190 0, kMaxWaitForFramesMs);
3191}
3192
3193// Test that we achieve the expected end-to-end connection time, using a
3194// fake clock and simulated latency on the media and signaling paths.
3195// We use a TURN<->TURN connection because this is usually the quickest to
3196// set up initially, especially when we're confident the connection will work
3197// and can start sending media before we get a STUN response.
3198//
3199// With various optimizations enabled, here are the network delays we expect to
3200// be on the critical path:
3201// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
3202// signaling answer (with DTLS fingerprint).
3203// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
3204// using TURN<->TURN pair, and DTLS exchange is 4 packets,
3205// the first of which should have arrived before the answer.
3206TEST_F(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
3207 rtc::ScopedFakeClock fake_clock;
3208 // Some things use a time of "0" as a special value, so we need to start out
3209 // the fake clock at a nonzero time.
3210 // TODO(deadbeef): Fix this.
3211 fake_clock.AdvanceTime(rtc::TimeDelta::FromSeconds(1));
3212
3213 static constexpr int media_hop_delay_ms = 50;
3214 static constexpr int signaling_trip_delay_ms = 500;
3215 // For explanation of these values, see comment above.
3216 static constexpr int required_media_hops = 9;
3217 static constexpr int required_signaling_trips = 2;
3218 // For internal delays (such as posting an event asychronously).
3219 static constexpr int allowed_internal_delay_ms = 20;
3220 static constexpr int total_connection_time_ms =
3221 media_hop_delay_ms * required_media_hops +
3222 signaling_trip_delay_ms * required_signaling_trips +
3223 allowed_internal_delay_ms;
3224
3225 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3226 3478};
3227 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3228 0};
3229 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3230 3478};
3231 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3232 0};
3233 cricket::TestTurnServer turn_server_1(network_thread(),
3234 turn_server_1_internal_address,
3235 turn_server_1_external_address);
3236 cricket::TestTurnServer turn_server_2(network_thread(),
3237 turn_server_2_internal_address,
3238 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003239
deadbeef1dcb1642017-03-29 21:08:16 -07003240 // Bypass permission check on received packets so media can be sent before
3241 // the candidate is signaled.
3242 turn_server_1.set_enable_permission_checks(false);
3243 turn_server_2.set_enable_permission_checks(false);
3244
3245 PeerConnectionInterface::RTCConfiguration client_1_config;
3246 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3247 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3248 ice_server_1.username = "test";
3249 ice_server_1.password = "test";
3250 client_1_config.servers.push_back(ice_server_1);
3251 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3252 client_1_config.presume_writable_when_fully_relayed = true;
3253
3254 PeerConnectionInterface::RTCConfiguration client_2_config;
3255 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3256 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3257 ice_server_2.username = "test";
3258 ice_server_2.password = "test";
3259 client_2_config.servers.push_back(ice_server_2);
3260 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3261 client_2_config.presume_writable_when_fully_relayed = true;
3262
3263 ASSERT_TRUE(
3264 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3265 // Set up the simulated delays.
3266 SetSignalingDelayMs(signaling_trip_delay_ms);
3267 ConnectFakeSignaling();
3268 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
3269 virtual_socket_server()->UpdateDelayDistribution();
3270
3271 // Set "offer to receive audio/video" without adding any tracks, so we just
3272 // set up ICE/DTLS with no media.
3273 PeerConnectionInterface::RTCOfferAnswerOptions options;
3274 options.offer_to_receive_audio = 1;
3275 options.offer_to_receive_video = 1;
3276 caller()->SetOfferAnswerOptions(options);
3277 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07003278 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
3279 fake_clock);
deadbeef1dcb1642017-03-29 21:08:16 -07003280 // Need to free the clients here since they're using things we created on
3281 // the stack.
3282 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3283 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3284}
3285
Jonas Orelandbdcee282017-10-10 14:01:40 +02003286// Verify that a TurnCustomizer passed in through RTCConfiguration
3287// is actually used by the underlying TURN candidate pair.
3288// Note that turnport_unittest.cc contains more detailed, lower-level tests.
3289TEST_F(PeerConnectionIntegrationTest, \
3290 TurnCustomizerUsedForTurnConnections) {
3291 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3292 3478};
3293 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3294 0};
3295 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3296 3478};
3297 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3298 0};
3299 cricket::TestTurnServer turn_server_1(network_thread(),
3300 turn_server_1_internal_address,
3301 turn_server_1_external_address);
3302 cricket::TestTurnServer turn_server_2(network_thread(),
3303 turn_server_2_internal_address,
3304 turn_server_2_external_address);
3305
3306 PeerConnectionInterface::RTCConfiguration client_1_config;
3307 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3308 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3309 ice_server_1.username = "test";
3310 ice_server_1.password = "test";
3311 client_1_config.servers.push_back(ice_server_1);
3312 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3313 auto customizer1 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3314 client_1_config.turn_customizer = customizer1.get();
3315
3316 PeerConnectionInterface::RTCConfiguration client_2_config;
3317 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3318 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3319 ice_server_2.username = "test";
3320 ice_server_2.password = "test";
3321 client_2_config.servers.push_back(ice_server_2);
3322 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3323 auto customizer2 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3324 client_2_config.turn_customizer = customizer2.get();
3325
3326 ASSERT_TRUE(
3327 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3328 ConnectFakeSignaling();
3329
3330 // Set "offer to receive audio/video" without adding any tracks, so we just
3331 // set up ICE/DTLS with no media.
3332 PeerConnectionInterface::RTCOfferAnswerOptions options;
3333 options.offer_to_receive_audio = 1;
3334 options.offer_to_receive_video = 1;
3335 caller()->SetOfferAnswerOptions(options);
3336 caller()->CreateAndSetAndSignalOffer();
3337 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3338
3339 EXPECT_GT(customizer1->allow_channel_data_cnt_, 0u);
3340 EXPECT_GT(customizer1->modify_cnt_, 0u);
3341
3342 EXPECT_GT(customizer2->allow_channel_data_cnt_, 0u);
3343 EXPECT_GT(customizer2->modify_cnt_, 0u);
3344
3345 // Need to free the clients here since they're using things we created on
3346 // the stack.
3347 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3348 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3349}
3350
deadbeefc964d0b2017-04-03 10:03:35 -07003351// Test that audio and video flow end-to-end when codec names don't use the
3352// expected casing, given that they're supposed to be case insensitive. To test
3353// this, all but one codec is removed from each media description, and its
3354// casing is changed.
3355//
3356// In the past, this has regressed and caused crashes/black video, due to the
3357// fact that code at some layers was doing case-insensitive comparisons and
3358// code at other layers was not.
3359TEST_F(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
3360 ASSERT_TRUE(CreatePeerConnectionWrappers());
3361 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003362 caller()->AddAudioVideoTracks();
3363 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07003364
3365 // Remove all but one audio/video codec (opus and VP8), and change the
3366 // casing of the caller's generated offer.
3367 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
3368 cricket::AudioContentDescription* audio =
3369 GetFirstAudioContentDescription(description);
3370 ASSERT_NE(nullptr, audio);
3371 auto audio_codecs = audio->codecs();
3372 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
3373 [](const cricket::AudioCodec& codec) {
3374 return codec.name != "opus";
3375 }),
3376 audio_codecs.end());
3377 ASSERT_EQ(1u, audio_codecs.size());
3378 audio_codecs[0].name = "OpUs";
3379 audio->set_codecs(audio_codecs);
3380
3381 cricket::VideoContentDescription* video =
3382 GetFirstVideoContentDescription(description);
3383 ASSERT_NE(nullptr, video);
3384 auto video_codecs = video->codecs();
3385 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
3386 [](const cricket::VideoCodec& codec) {
3387 return codec.name != "VP8";
3388 }),
3389 video_codecs.end());
3390 ASSERT_EQ(1u, video_codecs.size());
3391 video_codecs[0].name = "vP8";
3392 video->set_codecs(video_codecs);
3393 });
3394
3395 caller()->CreateAndSetAndSignalOffer();
3396 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3397
3398 // Verify frames are still received end-to-end.
3399 ExpectNewFramesReceivedWithWait(
3400 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3401 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3402 kMaxWaitForFramesMs);
3403}
3404
hbos8d609f62017-04-10 07:39:05 -07003405TEST_F(PeerConnectionIntegrationTest, GetSources) {
3406 ASSERT_TRUE(CreatePeerConnectionWrappers());
3407 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003408 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07003409 caller()->CreateAndSetAndSignalOffer();
3410 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07003411 // Wait for one audio frame to be received by the callee.
hbos8d609f62017-04-10 07:39:05 -07003412 ExpectNewFramesReceivedWithWait(0, 0, 1, 0, kMaxWaitForFramesMs);
3413 ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u);
3414 auto receiver = callee()->pc()->GetReceivers()[0];
3415 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
3416
3417 auto contributing_sources = receiver->GetSources();
3418 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
3419 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
3420 contributing_sources[0].source_id());
3421}
3422
deadbeef2f425aa2017-04-14 10:41:32 -07003423// Test that if a track is removed and added again with a different stream ID,
3424// the new stream ID is successfully communicated in SDP and media continues to
3425// flow end-to-end.
3426TEST_F(PeerConnectionIntegrationTest, RemoveAndAddTrackWithNewStreamId) {
3427 ASSERT_TRUE(CreatePeerConnectionWrappers());
3428 ConnectFakeSignaling();
3429
3430 rtc::scoped_refptr<MediaStreamInterface> stream_1 =
3431 caller()->pc_factory()->CreateLocalMediaStream("stream_1");
3432 rtc::scoped_refptr<MediaStreamInterface> stream_2 =
3433 caller()->pc_factory()->CreateLocalMediaStream("stream_2");
3434
3435 // Add track using stream 1, do offer/answer.
3436 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
3437 caller()->CreateLocalAudioTrack();
3438 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
3439 caller()->pc()->AddTrack(track, {stream_1.get()});
3440 caller()->CreateAndSetAndSignalOffer();
3441 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3442 // Wait for one audio frame to be received by the callee.
3443 ExpectNewFramesReceivedWithWait(0, 0, 1, 0, kMaxWaitForFramesMs);
3444
3445 // Remove the sender, and create a new one with the new stream.
3446 caller()->pc()->RemoveTrack(sender);
3447 sender = caller()->pc()->AddTrack(track, {stream_2.get()});
3448 caller()->CreateAndSetAndSignalOffer();
3449 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3450 // Wait for additional audio frames to be received by the callee.
3451 ExpectNewFramesReceivedWithWait(0, 0, kDefaultExpectedAudioFrameCount, 0,
3452 kMaxWaitForFramesMs);
3453}
3454
Elad Alon99c3fe52017-10-13 16:29:40 +02003455TEST_F(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
3456 ASSERT_TRUE(CreatePeerConnectionWrappers());
3457 ConnectFakeSignaling();
3458
3459 auto output = rtc::MakeUnique<testing::NiceMock<MockRtcEventLogOutput>>();
3460 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
3461 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
3462 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01003463 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
3464 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02003465
Steve Anton15324772018-01-16 10:26:49 -08003466 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02003467 caller()->CreateAndSetAndSignalOffer();
3468 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3469}
3470
Steve Antonede9ca52017-10-16 13:04:27 -07003471// Test that if candidates are only signaled by applying full session
3472// descriptions (instead of using AddIceCandidate), the peers can connect to
3473// each other and exchange media.
3474TEST_F(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
3475 ASSERT_TRUE(CreatePeerConnectionWrappers());
3476 // Each side will signal the session descriptions but not candidates.
3477 ConnectFakeSignalingForSdpOnly();
3478
3479 // Add audio video track and exchange the initial offer/answer with media
3480 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08003481 caller()->AddAudioVideoTracks();
3482 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003483 caller()->CreateAndSetAndSignalOffer();
3484
3485 // Wait for all candidates to be gathered on both the caller and callee.
3486 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3487 caller()->ice_gathering_state(), kDefaultTimeout);
3488 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
3489 callee()->ice_gathering_state(), kDefaultTimeout);
3490
3491 // The candidates will now be included in the session description, so
3492 // signaling them will start the ICE connection.
3493 caller()->CreateAndSetAndSignalOffer();
3494 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3495
3496 // Ensure that media flows in both directions.
3497 ExpectNewFramesReceivedWithWait(
3498 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3499 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3500 kMaxWaitForFramesMs);
3501}
3502
henrika5f6bf242017-11-01 11:06:56 +01003503// Test that SetAudioPlayout can be used to disable audio playout from the
3504// start, then later enable it. This may be useful, for example, if the caller
3505// needs to play a local ringtone until some event occurs, after which it
3506// switches to playing the received audio.
3507TEST_F(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
3508 ASSERT_TRUE(CreatePeerConnectionWrappers());
3509 ConnectFakeSignaling();
3510
3511 // Set up audio-only call where audio playout is disabled on caller's side.
3512 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08003513 caller()->AddAudioTrack();
3514 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01003515 caller()->CreateAndSetAndSignalOffer();
3516 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3517
3518 // Pump messages for a second.
3519 WAIT(false, 1000);
3520 // Since audio playout is disabled, the caller shouldn't have received
3521 // anything (at the playout level, at least).
3522 EXPECT_EQ(0, caller()->audio_frames_received());
3523 // As a sanity check, make sure the callee (for which playout isn't disabled)
3524 // did still see frames on its audio level.
3525 ASSERT_GT(callee()->audio_frames_received(), 0);
3526
3527 // Enable playout again, and ensure audio starts flowing.
3528 caller()->pc()->SetAudioPlayout(true);
3529 ExpectNewFramesReceivedWithWait(kDefaultExpectedAudioFrameCount, 0,
3530 kDefaultExpectedAudioFrameCount, 0,
3531 kMaxWaitForFramesMs);
3532}
3533
3534double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
3535 auto report = pc->NewGetStats();
3536 auto track_stats_list =
3537 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3538 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
3539 for (const auto* track_stats : track_stats_list) {
3540 if (track_stats->remote_source.is_defined() &&
3541 *track_stats->remote_source) {
3542 remote_track_stats = track_stats;
3543 break;
3544 }
3545 }
3546
3547 if (!remote_track_stats->total_audio_energy.is_defined()) {
3548 return 0.0;
3549 }
3550 return *remote_track_stats->total_audio_energy;
3551}
3552
3553// Test that if audio playout is disabled via the SetAudioPlayout() method, then
3554// incoming audio is still processed and statistics are generated.
3555TEST_F(PeerConnectionIntegrationTest,
3556 DisableAudioPlayoutStillGeneratesAudioStats) {
3557 ASSERT_TRUE(CreatePeerConnectionWrappers());
3558 ConnectFakeSignaling();
3559
3560 // Set up audio-only call where playout is disabled but audio-processing is
3561 // still active.
Steve Anton15324772018-01-16 10:26:49 -08003562 caller()->AddAudioTrack();
3563 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01003564 caller()->pc()->SetAudioPlayout(false);
3565
3566 caller()->CreateAndSetAndSignalOffer();
3567 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3568
3569 // Wait for the callee to receive audio stats.
3570 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
3571}
3572
henrika4f167df2017-11-01 14:45:55 +01003573// Test that SetAudioRecording can be used to disable audio recording from the
3574// start, then later enable it. This may be useful, for example, if the caller
3575// wants to ensure that no audio resources are active before a certain state
3576// is reached.
3577TEST_F(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
3578 ASSERT_TRUE(CreatePeerConnectionWrappers());
3579 ConnectFakeSignaling();
3580
3581 // Set up audio-only call where audio recording is disabled on caller's side.
3582 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08003583 caller()->AddAudioTrack();
3584 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01003585 caller()->CreateAndSetAndSignalOffer();
3586 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3587
3588 // Pump messages for a second.
3589 WAIT(false, 1000);
3590 // Since caller has disabled audio recording, the callee shouldn't have
3591 // received anything.
3592 EXPECT_EQ(0, callee()->audio_frames_received());
3593 // As a sanity check, make sure the caller did still see frames on its
3594 // audio level since audio recording is enabled on the calle side.
3595 ASSERT_GT(caller()->audio_frames_received(), 0);
3596
3597 // Enable audio recording again, and ensure audio starts flowing.
3598 caller()->pc()->SetAudioRecording(true);
3599 ExpectNewFramesReceivedWithWait(kDefaultExpectedAudioFrameCount, 0,
3600 kDefaultExpectedAudioFrameCount, 0,
3601 kMaxWaitForFramesMs);
3602}
3603
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08003604// Test that after closing PeerConnections, they stop sending any packets (ICE,
3605// DTLS, RTP...).
3606TEST_F(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
3607 // Set up audio/video/data, wait for some frames to be received.
3608 ASSERT_TRUE(CreatePeerConnectionWrappers());
3609 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003610 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08003611#ifdef HAVE_SCTP
3612 caller()->CreateDataChannel();
3613#endif
3614 caller()->CreateAndSetAndSignalOffer();
3615 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3616 ExpectNewFramesReceivedWithWait(0, 0, kDefaultExpectedAudioFrameCount,
3617 kDefaultExpectedAudioFrameCount,
3618 kMaxWaitForFramesMs);
3619 // Close PeerConnections.
3620 caller()->pc()->Close();
3621 callee()->pc()->Close();
3622 // Pump messages for a second, and ensure no new packets end up sent.
3623 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
3624 WAIT(false, 1000);
3625 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
3626 EXPECT_EQ(sent_packets_a, sent_packets_b);
3627}
3628
Steve Antond3679212018-01-17 17:41:02 -08003629// Test that a basic 1 audio and 1 video track call works when Unified Plan
3630// semantics configured for both sides.
3631TEST_F(PeerConnectionIntegrationTest, UnifiedPlanMediaFlows) {
3632 PeerConnectionInterface::RTCConfiguration config;
3633 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
3634 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3635 ConnectFakeSignaling();
3636 caller()->AddAudioVideoTracks();
3637 callee()->AddAudioVideoTracks();
3638 caller()->CreateAndSetAndSignalOffer();
3639 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3640 ExpectNewFramesReceivedWithWait(
3641 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3642 kDefaultExpectedAudioFrameCount, kDefaultExpectedVideoFrameCount,
3643 kMaxWaitForFramesMs);
3644}
3645
deadbeef1dcb1642017-03-29 21:08:16 -07003646} // namespace
3647
3648#endif // if !defined(THREAD_SANITIZER)