blob: e5f3ad424a6fbaa68c3d5473132c6280e172d430 [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"
Anders Carlsson67537952018-05-03 11:28:29 +020033#include "api/video_codecs/builtin_video_decoder_factory.h"
34#include "api/video_codecs/builtin_video_encoder_factory.h"
35#include "api/video_codecs/sdp_video_format.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "media/engine/fakewebrtcvideoengine.h"
37#include "p2p/base/p2pconstants.h"
38#include "p2p/base/portinterface.h"
Steve Antonede9ca52017-10-16 13:04:27 -070039#include "p2p/base/teststunserver.h"
Jonas Orelandbdcee282017-10-10 14:01:40 +020040#include "p2p/base/testturncustomizer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020041#include "p2p/base/testturnserver.h"
42#include "p2p/client/basicportallocator.h"
43#include "pc/dtmfsender.h"
44#include "pc/localaudiosource.h"
45#include "pc/mediasession.h"
46#include "pc/peerconnection.h"
47#include "pc/peerconnectionfactory.h"
Seth Hampson2f0d7022018-02-20 11:54:42 -080048#include "pc/rtpmediautils.h"
Steve Anton4ab68ee2017-12-19 14:26:11 -080049#include "pc/sessiondescription.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020050#include "pc/test/fakeaudiocapturemodule.h"
Niels Möller5c7efe72018-05-11 10:34:46 +020051#include "pc/test/fakeperiodicvideosource.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020052#include "pc/test/fakertccertificategenerator.h"
53#include "pc/test/fakevideotrackrenderer.h"
54#include "pc/test/mockpeerconnectionobservers.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020055#include "rtc_base/fakenetwork.h"
Steve Antonede9ca52017-10-16 13:04:27 -070056#include "rtc_base/firewallsocketserver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020057#include "rtc_base/gunit.h"
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070058#include "rtc_base/testcertificateverifier.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020059#include "rtc_base/virtualsocketserver.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020060#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070061
62using cricket::ContentInfo;
63using cricket::FakeWebRtcVideoDecoder;
64using cricket::FakeWebRtcVideoDecoderFactory;
65using cricket::FakeWebRtcVideoEncoder;
66using cricket::FakeWebRtcVideoEncoderFactory;
67using cricket::MediaContentDescription;
Steve Antondf527fd2018-04-27 15:52:03 -070068using cricket::StreamParams;
Steve Antonede9ca52017-10-16 13:04:27 -070069using rtc::SocketAddress;
Seth Hampson2f0d7022018-02-20 11:54:42 -080070using ::testing::Combine;
Steve Antonede9ca52017-10-16 13:04:27 -070071using ::testing::ElementsAre;
72using ::testing::Values;
deadbeef1dcb1642017-03-29 21:08:16 -070073using webrtc::DataBuffer;
74using webrtc::DataChannelInterface;
75using webrtc::DtmfSender;
76using webrtc::DtmfSenderInterface;
77using webrtc::DtmfSenderObserverInterface;
78using webrtc::FakeConstraints;
Steve Anton15324772018-01-16 10:26:49 -080079using webrtc::FakeVideoTrackRenderer;
deadbeef1dcb1642017-03-29 21:08:16 -070080using webrtc::MediaConstraintsInterface;
81using webrtc::MediaStreamInterface;
82using webrtc::MediaStreamTrackInterface;
83using webrtc::MockCreateSessionDescriptionObserver;
84using webrtc::MockDataChannelObserver;
85using webrtc::MockSetSessionDescriptionObserver;
86using webrtc::MockStatsObserver;
87using webrtc::ObserverInterface;
Steve Anton8c0f7a72017-10-03 10:03:10 -070088using webrtc::PeerConnection;
deadbeef1dcb1642017-03-29 21:08:16 -070089using webrtc::PeerConnectionInterface;
Steve Anton74255ff2018-01-24 18:32:57 -080090using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070091using webrtc::PeerConnectionFactory;
Steve Anton8c0f7a72017-10-03 10:03:10 -070092using webrtc::PeerConnectionProxy;
Steve Anton15324772018-01-16 10:26:49 -080093using webrtc::RTCErrorType;
Steve Anton7eca0932018-03-30 15:18:41 -070094using webrtc::RTCTransportStats;
Steve Anton74255ff2018-01-24 18:32:57 -080095using webrtc::RtpSenderInterface;
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +010096using webrtc::RtpReceiverInterface;
Seth Hampson2f0d7022018-02-20 11:54:42 -080097using webrtc::RtpSenderInterface;
98using webrtc::RtpTransceiverDirection;
99using webrtc::RtpTransceiverInit;
100using webrtc::RtpTransceiverInterface;
Steve Antond3679212018-01-17 17:41:02 -0800101using webrtc::SdpSemantics;
Steve Antona3a92c22017-12-07 10:27:41 -0800102using webrtc::SdpType;
deadbeef1dcb1642017-03-29 21:08:16 -0700103using webrtc::SessionDescriptionInterface;
104using webrtc::StreamCollectionInterface;
Steve Anton15324772018-01-16 10:26:49 -0800105using webrtc::VideoTrackInterface;
deadbeef1dcb1642017-03-29 21:08:16 -0700106
107namespace {
108
109static const int kDefaultTimeout = 10000;
110static const int kMaxWaitForStatsMs = 3000;
111static const int kMaxWaitForActivationMs = 5000;
112static const int kMaxWaitForFramesMs = 10000;
113// Default number of audio/video frames to wait for before considering a test
114// successful.
115static const int kDefaultExpectedAudioFrameCount = 3;
116static const int kDefaultExpectedVideoFrameCount = 3;
117
deadbeef1dcb1642017-03-29 21:08:16 -0700118static const char kDataChannelLabel[] = "data_channel";
119
120// SRTP cipher name negotiated by the tests. This must be updated if the
121// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700122static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700123static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
124
Steve Antonede9ca52017-10-16 13:04:27 -0700125static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
126
deadbeef1dcb1642017-03-29 21:08:16 -0700127// Helper function for constructing offer/answer options to initiate an ICE
128// restart.
129PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
130 PeerConnectionInterface::RTCOfferAnswerOptions options;
131 options.ice_restart = true;
132 return options;
133}
134
deadbeefd8ad7882017-04-18 16:01:17 -0700135// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
136// attribute from received SDP, simulating a legacy endpoint.
137void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
138 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800139 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700140 }
141 desc->set_msid_supported(false);
142}
143
Seth Hampson5897a6e2018-04-03 11:16:33 -0700144// Removes all stream information besides the stream ids, simulating an
145// endpoint that only signals a=msid lines to convey stream_ids.
146void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
147 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700148 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700149 std::vector<std::string> stream_ids;
150 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700151 const StreamParams& first_stream =
152 content.media_description()->streams()[0];
153 track_id = first_stream.id;
154 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700155 }
156 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700157 StreamParams new_stream;
158 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700159 new_stream.set_stream_ids(stream_ids);
160 content.media_description()->AddStream(new_stream);
161 }
162}
163
zhihuangf8164932017-05-19 13:09:47 -0700164int FindFirstMediaStatsIndexByKind(
165 const std::string& kind,
166 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
167 media_stats_vec) {
168 for (size_t i = 0; i < media_stats_vec.size(); i++) {
169 if (media_stats_vec[i]->kind.ValueToString() == kind) {
170 return i;
171 }
172 }
173 return -1;
174}
175
deadbeef1dcb1642017-03-29 21:08:16 -0700176class SignalingMessageReceiver {
177 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800178 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700179 virtual void ReceiveIceMessage(const std::string& sdp_mid,
180 int sdp_mline_index,
181 const std::string& msg) = 0;
182
183 protected:
184 SignalingMessageReceiver() {}
185 virtual ~SignalingMessageReceiver() {}
186};
187
188class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
189 public:
190 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
191 : expected_media_type_(media_type) {}
192
193 void OnFirstPacketReceived(cricket::MediaType media_type) override {
194 ASSERT_EQ(expected_media_type_, media_type);
195 first_packet_received_ = true;
196 }
197
198 bool first_packet_received() const { return first_packet_received_; }
199
200 virtual ~MockRtpReceiverObserver() {}
201
202 private:
203 bool first_packet_received_ = false;
204 cricket::MediaType expected_media_type_;
205};
206
207// Helper class that wraps a peer connection, observes it, and can accept
208// signaling messages from another wrapper.
209//
210// Uses a fake network, fake A/V capture, and optionally fake
211// encoders/decoders, though they aren't used by default since they don't
212// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700213// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800214// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700215class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800216 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700217 public:
218 // Different factory methods for convenience.
219 // TODO(deadbeef): Could use the pattern of:
220 //
221 // PeerConnectionWrapper =
222 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
223 //
224 // To reduce some code duplication.
225 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
226 const std::string& debug_name,
227 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
228 rtc::Thread* network_thread,
229 rtc::Thread* worker_thread) {
230 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700231 webrtc::PeerConnectionDependencies dependencies(nullptr);
232 dependencies.cert_generator = std::move(cert_generator);
233 if (!client->Init(nullptr, nullptr, nullptr, std::move(dependencies),
deadbeef1dcb1642017-03-29 21:08:16 -0700234 network_thread, worker_thread)) {
235 delete client;
236 return nullptr;
237 }
238 return client;
239 }
240
Niels Möller5c7efe72018-05-11 10:34:46 +0200241 ~PeerConnectionWrapper() {
242 // Tear down video sources in the proper order.
243 for (const auto& video_source : fake_video_sources_) {
244 // No more calls to downstream OnFrame
245 video_source->Stop();
246 }
247 for (const auto& track_source : video_track_sources_) {
248 // No more calls to upstream AddOrUpdateSink
249 track_source->OnSourceDestroyed();
250 }
251 fake_video_sources_.clear();
252 video_track_sources_.clear();
253 }
254
deadbeef2f425aa2017-04-14 10:41:32 -0700255 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
256 return peer_connection_factory_.get();
257 }
258
deadbeef1dcb1642017-03-29 21:08:16 -0700259 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
260
261 // If a signaling message receiver is set (via ConnectFakeSignaling), this
262 // will set the whole offer/answer exchange in motion. Just need to wait for
263 // the signaling state to reach "stable".
264 void CreateAndSetAndSignalOffer() {
265 auto offer = CreateOffer();
266 ASSERT_NE(nullptr, offer);
267 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
268 }
269
270 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
271 // when a remote offer is received (via fake signaling) and an answer is
272 // generated. By default, uses default options.
273 void SetOfferAnswerOptions(
274 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
275 offer_answer_options_ = options;
276 }
277
278 // Set a callback to be invoked when SDP is received via the fake signaling
279 // channel, which provides an opportunity to munge (modify) the SDP. This is
280 // used to test SDP being applied that a PeerConnection would normally not
281 // generate, but a non-JSEP endpoint might.
282 void SetReceivedSdpMunger(
283 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100284 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700285 }
286
deadbeefc964d0b2017-04-03 10:03:35 -0700287 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700288 // generated.
289 void SetGeneratedSdpMunger(
290 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100291 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700292 }
293
Seth Hampson2f0d7022018-02-20 11:54:42 -0800294 // Set a callback to be invoked when a remote offer is received via the fake
295 // signaling channel. This provides an opportunity to change the
296 // PeerConnection state before an answer is created and sent to the caller.
297 void SetRemoteOfferHandler(std::function<void()> handler) {
298 remote_offer_handler_ = std::move(handler);
299 }
300
Steve Antonede9ca52017-10-16 13:04:27 -0700301 // Every ICE connection state in order that has been seen by the observer.
302 std::vector<PeerConnectionInterface::IceConnectionState>
303 ice_connection_state_history() const {
304 return ice_connection_state_history_;
305 }
Steve Anton6f25b092017-10-23 09:39:20 -0700306 void clear_ice_connection_state_history() {
307 ice_connection_state_history_.clear();
308 }
Steve Antonede9ca52017-10-16 13:04:27 -0700309
310 // Every ICE gathering state in order that has been seen by the observer.
311 std::vector<PeerConnectionInterface::IceGatheringState>
312 ice_gathering_state_history() const {
313 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700314 }
315
Steve Anton15324772018-01-16 10:26:49 -0800316 void AddAudioVideoTracks() {
317 AddAudioTrack();
318 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700319 }
320
Steve Anton74255ff2018-01-24 18:32:57 -0800321 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
322 return AddTrack(CreateLocalAudioTrack());
323 }
deadbeef1dcb1642017-03-29 21:08:16 -0700324
Steve Anton74255ff2018-01-24 18:32:57 -0800325 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
326 return AddTrack(CreateLocalVideoTrack());
327 }
deadbeef1dcb1642017-03-29 21:08:16 -0700328
329 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
330 FakeConstraints constraints;
331 // Disable highpass filter so that we can get all the test audio frames.
332 constraints.AddMandatory(MediaConstraintsInterface::kHighpassFilter, false);
333 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
334 peer_connection_factory_->CreateAudioSource(&constraints);
335 // TODO(perkj): Test audio source when it is implemented. Currently audio
336 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700337 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700338 source);
339 }
340
341 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Niels Möller5c7efe72018-05-11 10:34:46 +0200342 return CreateLocalVideoTrackInternal(
343 webrtc::FakePeriodicVideoSource::Config());
deadbeef1dcb1642017-03-29 21:08:16 -0700344 }
345
346 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200347 CreateLocalVideoTrackWithConfig(
348 webrtc::FakePeriodicVideoSource::Config config) {
349 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700350 }
351
352 rtc::scoped_refptr<webrtc::VideoTrackInterface>
353 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200354 webrtc::FakePeriodicVideoSource::Config config;
355 config.rotation = rotation;
356 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700357 }
358
Steve Anton74255ff2018-01-24 18:32:57 -0800359 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
360 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800361 const std::vector<std::string>& stream_ids = {}) {
362 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800363 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800364 return result.MoveValue();
365 }
366
367 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
368 cricket::MediaType media_type) {
369 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
370 for (auto receiver : pc()->GetReceivers()) {
371 if (receiver->media_type() == media_type) {
372 receivers.push_back(receiver);
373 }
374 }
375 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700376 }
377
Seth Hampson2f0d7022018-02-20 11:54:42 -0800378 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
379 cricket::MediaType media_type) {
380 for (auto transceiver : pc()->GetTransceivers()) {
381 if (transceiver->receiver()->media_type() == media_type) {
382 return transceiver;
383 }
384 }
385 return nullptr;
386 }
387
deadbeef1dcb1642017-03-29 21:08:16 -0700388 bool SignalingStateStable() {
389 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
390 }
391
392 void CreateDataChannel() { CreateDataChannel(nullptr); }
393
394 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700395 CreateDataChannel(kDataChannelLabel, init);
396 }
397
398 void CreateDataChannel(const std::string& label,
399 const webrtc::DataChannelInit* init) {
400 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700401 ASSERT_TRUE(data_channel_.get() != nullptr);
402 data_observer_.reset(new MockDataChannelObserver(data_channel_));
403 }
404
405 DataChannelInterface* data_channel() { return data_channel_; }
406 const MockDataChannelObserver* data_observer() const {
407 return data_observer_.get();
408 }
409
410 int audio_frames_received() const {
411 return fake_audio_capture_module_->frames_received();
412 }
413
414 // Takes minimum of video frames received for each track.
415 //
416 // Can be used like:
417 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
418 //
419 // To ensure that all video tracks received at least a certain number of
420 // frames.
421 int min_video_frames_received_per_track() const {
422 int min_frames = INT_MAX;
423 if (video_decoder_factory_enabled_) {
424 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
425 fake_video_decoder_factory_->decoders();
426 if (decoders.empty()) {
427 return 0;
428 }
429 for (FakeWebRtcVideoDecoder* decoder : decoders) {
430 min_frames = std::min(min_frames, decoder->GetNumFramesReceived());
431 }
432 return min_frames;
433 } else {
434 if (fake_video_renderers_.empty()) {
435 return 0;
436 }
437
438 for (const auto& pair : fake_video_renderers_) {
439 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
440 }
441 return min_frames;
442 }
443 }
444
445 // In contrast to the above, sums the video frames received for all tracks.
446 // Can be used to verify that no video frames were received, or that the
447 // counts didn't increase.
448 int total_video_frames_received() const {
449 int total = 0;
450 if (video_decoder_factory_enabled_) {
451 const std::vector<FakeWebRtcVideoDecoder*>& decoders =
452 fake_video_decoder_factory_->decoders();
453 for (const FakeWebRtcVideoDecoder* decoder : decoders) {
454 total += decoder->GetNumFramesReceived();
455 }
456 } else {
457 for (const auto& pair : fake_video_renderers_) {
458 total += pair.second->num_rendered_frames();
459 }
460 for (const auto& renderer : removed_fake_video_renderers_) {
461 total += renderer->num_rendered_frames();
462 }
463 }
464 return total;
465 }
466
467 // Returns a MockStatsObserver in a state after stats gathering finished,
468 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700469 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700470 webrtc::MediaStreamTrackInterface* track) {
471 rtc::scoped_refptr<MockStatsObserver> observer(
472 new rtc::RefCountedObject<MockStatsObserver>());
473 EXPECT_TRUE(peer_connection_->GetStats(
474 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
475 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
476 return observer;
477 }
478
479 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700480 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
481 return OldGetStatsForTrack(nullptr);
482 }
483
484 // Synchronously gets stats and returns them. If it times out, fails the test
485 // and returns null.
486 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
487 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
488 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
489 peer_connection_->GetStats(callback);
490 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
491 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700492 }
493
494 int rendered_width() {
495 EXPECT_FALSE(fake_video_renderers_.empty());
496 return fake_video_renderers_.empty()
497 ? 0
498 : fake_video_renderers_.begin()->second->width();
499 }
500
501 int rendered_height() {
502 EXPECT_FALSE(fake_video_renderers_.empty());
503 return fake_video_renderers_.empty()
504 ? 0
505 : fake_video_renderers_.begin()->second->height();
506 }
507
508 double rendered_aspect_ratio() {
509 if (rendered_height() == 0) {
510 return 0.0;
511 }
512 return static_cast<double>(rendered_width()) / rendered_height();
513 }
514
515 webrtc::VideoRotation rendered_rotation() {
516 EXPECT_FALSE(fake_video_renderers_.empty());
517 return fake_video_renderers_.empty()
518 ? webrtc::kVideoRotation_0
519 : fake_video_renderers_.begin()->second->rotation();
520 }
521
522 int local_rendered_width() {
523 return local_video_renderer_ ? local_video_renderer_->width() : 0;
524 }
525
526 int local_rendered_height() {
527 return local_video_renderer_ ? local_video_renderer_->height() : 0;
528 }
529
530 double local_rendered_aspect_ratio() {
531 if (local_rendered_height() == 0) {
532 return 0.0;
533 }
534 return static_cast<double>(local_rendered_width()) /
535 local_rendered_height();
536 }
537
538 size_t number_of_remote_streams() {
539 if (!pc()) {
540 return 0;
541 }
542 return pc()->remote_streams()->count();
543 }
544
545 StreamCollectionInterface* remote_streams() const {
546 if (!pc()) {
547 ADD_FAILURE();
548 return nullptr;
549 }
550 return pc()->remote_streams();
551 }
552
553 StreamCollectionInterface* local_streams() {
554 if (!pc()) {
555 ADD_FAILURE();
556 return nullptr;
557 }
558 return pc()->local_streams();
559 }
560
561 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
562 return pc()->signaling_state();
563 }
564
565 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
566 return pc()->ice_connection_state();
567 }
568
569 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
570 return pc()->ice_gathering_state();
571 }
572
573 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
574 // GetReceivers. They're updated automatically when a remote offer/answer
575 // from the fake signaling channel is applied, or when
576 // ResetRtpReceiverObservers below is called.
577 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
578 rtp_receiver_observers() {
579 return rtp_receiver_observers_;
580 }
581
582 void ResetRtpReceiverObservers() {
583 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100584 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
585 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700586 std::unique_ptr<MockRtpReceiverObserver> observer(
587 new MockRtpReceiverObserver(receiver->media_type()));
588 receiver->SetObserver(observer.get());
589 rtp_receiver_observers_.push_back(std::move(observer));
590 }
591 }
592
Steve Antonede9ca52017-10-16 13:04:27 -0700593 rtc::FakeNetworkManager* network() const {
594 return fake_network_manager_.get();
595 }
596 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
597
deadbeef1dcb1642017-03-29 21:08:16 -0700598 private:
599 explicit PeerConnectionWrapper(const std::string& debug_name)
600 : debug_name_(debug_name) {}
601
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700602 bool Init(const MediaConstraintsInterface* constraints,
603 const PeerConnectionFactory::Options* options,
604 const PeerConnectionInterface::RTCConfiguration* config,
605 webrtc::PeerConnectionDependencies dependencies,
606 rtc::Thread* network_thread,
607 rtc::Thread* worker_thread) {
deadbeef1dcb1642017-03-29 21:08:16 -0700608 // There's an error in this test code if Init ends up being called twice.
609 RTC_DCHECK(!peer_connection_);
610 RTC_DCHECK(!peer_connection_factory_);
611
612 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700613 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700614
615 std::unique_ptr<cricket::PortAllocator> port_allocator(
616 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700617 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700618 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
619 if (!fake_audio_capture_module_) {
620 return false;
621 }
622 // Note that these factories don't end up getting used unless supported
623 // codecs are added to them.
624 fake_video_decoder_factory_ = new FakeWebRtcVideoDecoderFactory();
625 fake_video_encoder_factory_ = new FakeWebRtcVideoEncoderFactory();
626 rtc::Thread* const signaling_thread = rtc::Thread::Current();
627 peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
628 network_thread, worker_thread, signaling_thread,
Anders Carlsson67537952018-05-03 11:28:29 +0200629 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
630 fake_audio_capture_module_),
631 webrtc::CreateBuiltinAudioEncoderFactory(),
632 webrtc::CreateBuiltinAudioDecoderFactory(),
633 std::unique_ptr<FakeWebRtcVideoEncoderFactory>(
634 fake_video_encoder_factory_),
635 std::unique_ptr<FakeWebRtcVideoDecoderFactory>(
636 fake_video_decoder_factory_),
637 nullptr /* audio_mixer */, nullptr /* audio_processing */);
deadbeef1dcb1642017-03-29 21:08:16 -0700638 if (!peer_connection_factory_) {
639 return false;
640 }
641 if (options) {
642 peer_connection_factory_->SetOptions(*options);
643 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800644 if (config) {
645 sdp_semantics_ = config->sdp_semantics;
646 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700647
648 dependencies.allocator = std::move(port_allocator);
deadbeef1dcb1642017-03-29 21:08:16 -0700649 peer_connection_ =
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700650 CreatePeerConnection(constraints, config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700651 return peer_connection_.get() != nullptr;
652 }
653
654 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700655 const MediaConstraintsInterface* constraints,
656 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700657 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700658 PeerConnectionInterface::RTCConfiguration modified_config;
659 // If |config| is null, this will result in a default configuration being
660 // used.
661 if (config) {
662 modified_config = *config;
663 }
664 // Disable resolution adaptation; we don't want it interfering with the
665 // test results.
666 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
667 // ratios and not specific resolutions, is this even necessary?
668 modified_config.set_cpu_adaptation(false);
669
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700670 // Use the legacy interface.
671 if (constraints != nullptr) {
672 return peer_connection_factory_->CreatePeerConnection(
673 modified_config, constraints, std::move(dependencies.allocator),
674 std::move(dependencies.cert_generator), this);
675 }
676 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700677 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700678 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700679 }
680
681 void set_signaling_message_receiver(
682 SignalingMessageReceiver* signaling_message_receiver) {
683 signaling_message_receiver_ = signaling_message_receiver;
684 }
685
686 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
687
Steve Antonede9ca52017-10-16 13:04:27 -0700688 void set_signal_ice_candidates(bool signal) {
689 signal_ice_candidates_ = signal;
690 }
691
deadbeef1dcb1642017-03-29 21:08:16 -0700692 void EnableVideoDecoderFactory() {
693 video_decoder_factory_enabled_ = true;
694 fake_video_decoder_factory_->AddSupportedVideoCodecType(
Anders Carlsson67537952018-05-03 11:28:29 +0200695 webrtc::SdpVideoFormat("VP8"));
deadbeef1dcb1642017-03-29 21:08:16 -0700696 }
697
698 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200699 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700700 // Set max frame rate to 10fps to reduce the risk of test flakiness.
701 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200702 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700703
Niels Möller5c7efe72018-05-11 10:34:46 +0200704 fake_video_sources_.emplace_back(
705 rtc::MakeUnique<webrtc::FakePeriodicVideoSource>(config));
706
707 video_track_sources_.emplace_back(
708 new rtc::RefCountedObject<webrtc::VideoTrackSource>(
709 fake_video_sources_.back().get(), false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700710 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200711 peer_connection_factory_->CreateVideoTrack(
712 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700713 if (!local_video_renderer_) {
714 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
715 }
716 return track;
717 }
718
719 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100720 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800721 std::unique_ptr<SessionDescriptionInterface> desc =
722 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700723 if (received_sdp_munger_) {
724 received_sdp_munger_(desc->description());
725 }
726
727 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
728 // Setting a remote description may have changed the number of receivers,
729 // so reset the receiver observers.
730 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800731 if (remote_offer_handler_) {
732 remote_offer_handler_();
733 }
deadbeef1dcb1642017-03-29 21:08:16 -0700734 auto answer = CreateAnswer();
735 ASSERT_NE(nullptr, answer);
736 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
737 }
738
739 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100740 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800741 std::unique_ptr<SessionDescriptionInterface> desc =
742 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700743 if (received_sdp_munger_) {
744 received_sdp_munger_(desc->description());
745 }
746
747 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
748 // Set the RtpReceiverObserver after receivers are created.
749 ResetRtpReceiverObservers();
750 }
751
752 // Returns null on failure.
753 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
754 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
755 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
756 pc()->CreateOffer(observer, offer_answer_options_);
757 return WaitForDescriptionFromObserver(observer);
758 }
759
760 // Returns null on failure.
761 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
762 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
763 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
764 pc()->CreateAnswer(observer, offer_answer_options_);
765 return WaitForDescriptionFromObserver(observer);
766 }
767
768 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100769 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700770 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
771 if (!observer->result()) {
772 return nullptr;
773 }
774 auto description = observer->MoveDescription();
775 if (generated_sdp_munger_) {
776 generated_sdp_munger_(description->description());
777 }
778 return description;
779 }
780
781 // Setting the local description and sending the SDP message over the fake
782 // signaling channel are combined into the same method because the SDP
783 // message needs to be sent as soon as SetLocalDescription finishes, without
784 // waiting for the observer to be called. This ensures that ICE candidates
785 // don't outrace the description.
786 bool SetLocalDescriptionAndSendSdpMessage(
787 std::unique_ptr<SessionDescriptionInterface> desc) {
788 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
789 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100790 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800791 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700792 std::string sdp;
793 EXPECT_TRUE(desc->ToString(&sdp));
794 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800795 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
796 RemoveUnusedVideoRenderers();
797 }
deadbeef1dcb1642017-03-29 21:08:16 -0700798 // As mentioned above, we need to send the message immediately after
799 // SetLocalDescription.
800 SendSdpMessage(type, sdp);
801 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
802 return true;
803 }
804
805 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
806 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
807 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100808 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700809 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800810 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
811 RemoveUnusedVideoRenderers();
812 }
deadbeef1dcb1642017-03-29 21:08:16 -0700813 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
814 return observer->result();
815 }
816
Seth Hampson2f0d7022018-02-20 11:54:42 -0800817 // This is a work around to remove unused fake_video_renderers from
818 // transceivers that have either stopped or are no longer receiving.
819 void RemoveUnusedVideoRenderers() {
820 auto transceivers = pc()->GetTransceivers();
821 for (auto& transceiver : transceivers) {
822 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
823 continue;
824 }
825 // Remove fake video renderers from any stopped transceivers.
826 if (transceiver->stopped()) {
827 auto it =
828 fake_video_renderers_.find(transceiver->receiver()->track()->id());
829 if (it != fake_video_renderers_.end()) {
830 fake_video_renderers_.erase(it);
831 }
832 }
833 // Remove fake video renderers from any transceivers that are no longer
834 // receiving.
835 if ((transceiver->current_direction() &&
836 !webrtc::RtpTransceiverDirectionHasRecv(
837 *transceiver->current_direction()))) {
838 auto it =
839 fake_video_renderers_.find(transceiver->receiver()->track()->id());
840 if (it != fake_video_renderers_.end()) {
841 fake_video_renderers_.erase(it);
842 }
843 }
844 }
845 }
846
deadbeef1dcb1642017-03-29 21:08:16 -0700847 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
848 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800849 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700850 if (signaling_delay_ms_ == 0) {
851 RelaySdpMessageIfReceiverExists(type, msg);
852 } else {
853 invoker_.AsyncInvokeDelayed<void>(
854 RTC_FROM_HERE, rtc::Thread::Current(),
855 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
856 this, type, msg),
857 signaling_delay_ms_);
858 }
859 }
860
Steve Antona3a92c22017-12-07 10:27:41 -0800861 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700862 if (signaling_message_receiver_) {
863 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
864 }
865 }
866
867 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
868 // default).
869 void SendIceMessage(const std::string& sdp_mid,
870 int sdp_mline_index,
871 const std::string& msg) {
872 if (signaling_delay_ms_ == 0) {
873 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
874 } else {
875 invoker_.AsyncInvokeDelayed<void>(
876 RTC_FROM_HERE, rtc::Thread::Current(),
877 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
878 this, sdp_mid, sdp_mline_index, msg),
879 signaling_delay_ms_);
880 }
881 }
882
883 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
884 int sdp_mline_index,
885 const std::string& msg) {
886 if (signaling_message_receiver_) {
887 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
888 msg);
889 }
890 }
891
892 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800893 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
894 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700895 HandleIncomingOffer(msg);
896 } else {
897 HandleIncomingAnswer(msg);
898 }
899 }
900
901 void ReceiveIceMessage(const std::string& sdp_mid,
902 int sdp_mline_index,
903 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100904 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700905 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
906 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
907 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
908 }
909
910 // PeerConnectionObserver callbacks.
911 void OnSignalingChange(
912 webrtc::PeerConnectionInterface::SignalingState new_state) override {
913 EXPECT_EQ(pc()->signaling_state(), new_state);
914 }
Steve Anton15324772018-01-16 10:26:49 -0800915 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
916 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
917 streams) override {
918 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
919 rtc::scoped_refptr<VideoTrackInterface> video_track(
920 static_cast<VideoTrackInterface*>(receiver->track().get()));
921 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700922 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800923 fake_video_renderers_[video_track->id()] =
924 rtc::MakeUnique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700925 }
926 }
Steve Anton15324772018-01-16 10:26:49 -0800927 void OnRemoveTrack(
928 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
929 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
930 auto it = fake_video_renderers_.find(receiver->track()->id());
931 RTC_DCHECK(it != fake_video_renderers_.end());
932 fake_video_renderers_.erase(it);
933 }
934 }
deadbeef1dcb1642017-03-29 21:08:16 -0700935 void OnRenegotiationNeeded() override {}
936 void OnIceConnectionChange(
937 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
938 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700939 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700940 }
941 void OnIceGatheringChange(
942 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700943 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700944 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700945 }
946 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100947 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700948
949 std::string ice_sdp;
950 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700951 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700952 // Remote party may be deleted.
953 return;
954 }
955 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
956 }
957 void OnDataChannel(
958 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100959 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700960 data_channel_ = data_channel;
961 data_observer_.reset(new MockDataChannelObserver(data_channel));
962 }
963
deadbeef1dcb1642017-03-29 21:08:16 -0700964 std::string debug_name_;
965
966 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
967
968 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
969 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
970 peer_connection_factory_;
971
Steve Antonede9ca52017-10-16 13:04:27 -0700972 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700973 // Needed to keep track of number of frames sent.
974 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
975 // Needed to keep track of number of frames received.
976 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
977 fake_video_renderers_;
978 // Needed to ensure frames aren't received for removed tracks.
979 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
980 removed_fake_video_renderers_;
981 // Needed to keep track of number of frames received when external decoder
982 // used.
983 FakeWebRtcVideoDecoderFactory* fake_video_decoder_factory_ = nullptr;
984 FakeWebRtcVideoEncoderFactory* fake_video_encoder_factory_ = nullptr;
985 bool video_decoder_factory_enabled_ = false;
986
987 // For remote peer communication.
988 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
989 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700990 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -0700991
Niels Möller5c7efe72018-05-11 10:34:46 +0200992 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -0700993 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +0200994 std::vector<std::unique_ptr<webrtc::FakePeriodicVideoSource>>
995 fake_video_sources_;
996 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
997 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -0700998 // |local_video_renderer_| attached to the first created local video track.
999 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
1000
Seth Hampson2f0d7022018-02-20 11:54:42 -08001001 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07001002 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
1003 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
1004 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001005 std::function<void()> remote_offer_handler_;
deadbeef1dcb1642017-03-29 21:08:16 -07001006
1007 rtc::scoped_refptr<DataChannelInterface> data_channel_;
1008 std::unique_ptr<MockDataChannelObserver> data_observer_;
1009
1010 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
1011
Steve Antonede9ca52017-10-16 13:04:27 -07001012 std::vector<PeerConnectionInterface::IceConnectionState>
1013 ice_connection_state_history_;
1014 std::vector<PeerConnectionInterface::IceGatheringState>
1015 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -07001016
1017 rtc::AsyncInvoker invoker_;
1018
Seth Hampson2f0d7022018-02-20 11:54:42 -08001019 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -07001020};
1021
Elad Alon99c3fe52017-10-13 16:29:40 +02001022class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
1023 public:
1024 virtual ~MockRtcEventLogOutput() = default;
1025 MOCK_CONST_METHOD0(IsActive, bool());
1026 MOCK_METHOD1(Write, bool(const std::string&));
1027};
1028
Seth Hampson2f0d7022018-02-20 11:54:42 -08001029// This helper object is used for both specifying how many audio/video frames
1030// are expected to be received for a caller/callee. It provides helper functions
1031// to specify these expectations. The object initially starts in a state of no
1032// expectations.
1033class MediaExpectations {
1034 public:
1035 enum ExpectFrames {
1036 kExpectSomeFrames,
1037 kExpectNoFrames,
1038 kNoExpectation,
1039 };
1040
1041 void ExpectBidirectionalAudioAndVideo() {
1042 ExpectBidirectionalAudio();
1043 ExpectBidirectionalVideo();
1044 }
1045
1046 void ExpectBidirectionalAudio() {
1047 CallerExpectsSomeAudio();
1048 CalleeExpectsSomeAudio();
1049 }
1050
1051 void ExpectNoAudio() {
1052 CallerExpectsNoAudio();
1053 CalleeExpectsNoAudio();
1054 }
1055
1056 void ExpectBidirectionalVideo() {
1057 CallerExpectsSomeVideo();
1058 CalleeExpectsSomeVideo();
1059 }
1060
1061 void ExpectNoVideo() {
1062 CallerExpectsNoVideo();
1063 CalleeExpectsNoVideo();
1064 }
1065
1066 void CallerExpectsSomeAudioAndVideo() {
1067 CallerExpectsSomeAudio();
1068 CallerExpectsSomeVideo();
1069 }
1070
1071 void CalleeExpectsSomeAudioAndVideo() {
1072 CalleeExpectsSomeAudio();
1073 CalleeExpectsSomeVideo();
1074 }
1075
1076 // Caller's audio functions.
1077 void CallerExpectsSomeAudio(
1078 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1079 caller_audio_expectation_ = kExpectSomeFrames;
1080 caller_audio_frames_expected_ = expected_audio_frames;
1081 }
1082
1083 void CallerExpectsNoAudio() {
1084 caller_audio_expectation_ = kExpectNoFrames;
1085 caller_audio_frames_expected_ = 0;
1086 }
1087
1088 // Caller's video functions.
1089 void CallerExpectsSomeVideo(
1090 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1091 caller_video_expectation_ = kExpectSomeFrames;
1092 caller_video_frames_expected_ = expected_video_frames;
1093 }
1094
1095 void CallerExpectsNoVideo() {
1096 caller_video_expectation_ = kExpectNoFrames;
1097 caller_video_frames_expected_ = 0;
1098 }
1099
1100 // Callee's audio functions.
1101 void CalleeExpectsSomeAudio(
1102 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1103 callee_audio_expectation_ = kExpectSomeFrames;
1104 callee_audio_frames_expected_ = expected_audio_frames;
1105 }
1106
1107 void CalleeExpectsNoAudio() {
1108 callee_audio_expectation_ = kExpectNoFrames;
1109 callee_audio_frames_expected_ = 0;
1110 }
1111
1112 // Callee's video functions.
1113 void CalleeExpectsSomeVideo(
1114 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1115 callee_video_expectation_ = kExpectSomeFrames;
1116 callee_video_frames_expected_ = expected_video_frames;
1117 }
1118
1119 void CalleeExpectsNoVideo() {
1120 callee_video_expectation_ = kExpectNoFrames;
1121 callee_video_frames_expected_ = 0;
1122 }
1123
1124 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1125 ExpectFrames caller_video_expectation_ = kNoExpectation;
1126 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1127 ExpectFrames callee_video_expectation_ = kNoExpectation;
1128 int caller_audio_frames_expected_ = 0;
1129 int caller_video_frames_expected_ = 0;
1130 int callee_audio_frames_expected_ = 0;
1131 int callee_video_frames_expected_ = 0;
1132};
1133
deadbeef1dcb1642017-03-29 21:08:16 -07001134// Tests two PeerConnections connecting to each other end-to-end, using a
1135// virtual network, fake A/V capture and fake encoder/decoders. The
1136// PeerConnections share the threads/socket servers, but use separate versions
1137// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001138class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001139 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001140 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1141 : sdp_semantics_(sdp_semantics),
1142 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001143 fss_(new rtc::FirewallSocketServer(ss_.get())),
1144 network_thread_(new rtc::Thread(fss_.get())),
deadbeef1dcb1642017-03-29 21:08:16 -07001145 worker_thread_(rtc::Thread::Create()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001146 network_thread_->SetName("PCNetworkThread", this);
1147 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001148 RTC_CHECK(network_thread_->Start());
1149 RTC_CHECK(worker_thread_->Start());
1150 }
1151
Seth Hampson2f0d7022018-02-20 11:54:42 -08001152 ~PeerConnectionIntegrationBaseTest() {
deadbeef1dcb1642017-03-29 21:08:16 -07001153 if (caller_) {
1154 caller_->set_signaling_message_receiver(nullptr);
1155 }
1156 if (callee_) {
1157 callee_->set_signaling_message_receiver(nullptr);
1158 }
1159 }
1160
1161 bool SignalingStateStable() {
1162 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1163 }
1164
deadbeef71452802017-05-07 17:21:01 -07001165 bool DtlsConnected() {
1166 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1167 // are connected. This is an important distinction. Once we have separate
1168 // ICE and DTLS state, this check needs to use the DTLS state.
1169 return (callee()->ice_connection_state() ==
1170 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1171 callee()->ice_connection_state() ==
1172 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1173 (caller()->ice_connection_state() ==
1174 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1175 caller()->ice_connection_state() ==
1176 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
1177 }
1178
Seth Hampson2f0d7022018-02-20 11:54:42 -08001179 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1180 const std::string& debug_name,
1181 const MediaConstraintsInterface* constraints,
1182 const PeerConnectionFactory::Options* options,
1183 const RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001184 webrtc::PeerConnectionDependencies dependencies) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001185 RTCConfiguration modified_config;
1186 if (config) {
1187 modified_config = *config;
1188 }
Steve Anton3acffc32018-04-12 17:21:03 -07001189 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001190 if (!dependencies.cert_generator) {
1191 dependencies.cert_generator =
1192 rtc::MakeUnique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001193 }
1194 std::unique_ptr<PeerConnectionWrapper> client(
1195 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001196
Seth Hampson2f0d7022018-02-20 11:54:42 -08001197 if (!client->Init(constraints, options, &modified_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001198 std::move(dependencies), network_thread_.get(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001199 worker_thread_.get())) {
1200 return nullptr;
1201 }
1202 return client;
1203 }
1204
deadbeef1dcb1642017-03-29 21:08:16 -07001205 bool CreatePeerConnectionWrappers() {
1206 return CreatePeerConnectionWrappersWithConfig(
1207 PeerConnectionInterface::RTCConfiguration(),
1208 PeerConnectionInterface::RTCConfiguration());
1209 }
1210
Steve Anton3acffc32018-04-12 17:21:03 -07001211 bool CreatePeerConnectionWrappersWithSdpSemantics(
1212 SdpSemantics caller_semantics,
1213 SdpSemantics callee_semantics) {
1214 // Can't specify the sdp_semantics in the passed-in configuration since it
1215 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1216 // stored in sdp_semantics_. So get around this by modifying the instance
1217 // variable before calling CreatePeerConnectionWrapper for the caller and
1218 // callee PeerConnections.
1219 SdpSemantics original_semantics = sdp_semantics_;
1220 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001221 caller_ = CreatePeerConnectionWrapper(
1222 "Caller", nullptr, nullptr, nullptr,
1223 webrtc::PeerConnectionDependencies(nullptr));
Steve Anton3acffc32018-04-12 17:21:03 -07001224 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001225 callee_ = CreatePeerConnectionWrapper(
1226 "Callee", nullptr, nullptr, nullptr,
1227 webrtc::PeerConnectionDependencies(nullptr));
Steve Anton3acffc32018-04-12 17:21:03 -07001228 sdp_semantics_ = original_semantics;
1229 return caller_ && callee_;
1230 }
1231
deadbeef1dcb1642017-03-29 21:08:16 -07001232 bool CreatePeerConnectionWrappersWithConstraints(
1233 MediaConstraintsInterface* caller_constraints,
1234 MediaConstraintsInterface* callee_constraints) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001235 caller_ = CreatePeerConnectionWrapper(
1236 "Caller", caller_constraints, nullptr, nullptr,
1237 webrtc::PeerConnectionDependencies(nullptr));
1238 callee_ = CreatePeerConnectionWrapper(
1239 "Callee", callee_constraints, nullptr, nullptr,
1240 webrtc::PeerConnectionDependencies(nullptr));
1241
deadbeef1dcb1642017-03-29 21:08:16 -07001242 return caller_ && callee_;
1243 }
1244
1245 bool CreatePeerConnectionWrappersWithConfig(
1246 const PeerConnectionInterface::RTCConfiguration& caller_config,
1247 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001248 caller_ = CreatePeerConnectionWrapper(
1249 "Caller", nullptr, nullptr, &caller_config,
1250 webrtc::PeerConnectionDependencies(nullptr));
1251 callee_ = CreatePeerConnectionWrapper(
1252 "Callee", nullptr, nullptr, &callee_config,
1253 webrtc::PeerConnectionDependencies(nullptr));
1254 return caller_ && callee_;
1255 }
1256
1257 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1258 const PeerConnectionInterface::RTCConfiguration& caller_config,
1259 webrtc::PeerConnectionDependencies caller_dependencies,
1260 const PeerConnectionInterface::RTCConfiguration& callee_config,
1261 webrtc::PeerConnectionDependencies callee_dependencies) {
1262 caller_ =
1263 CreatePeerConnectionWrapper("Caller", nullptr, nullptr, &caller_config,
1264 std::move(caller_dependencies));
1265 callee_ =
1266 CreatePeerConnectionWrapper("Callee", nullptr, nullptr, &callee_config,
1267 std::move(callee_dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -07001268 return caller_ && callee_;
1269 }
1270
1271 bool CreatePeerConnectionWrappersWithOptions(
1272 const PeerConnectionFactory::Options& caller_options,
1273 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001274 caller_ = CreatePeerConnectionWrapper(
1275 "Caller", nullptr, &caller_options, nullptr,
1276 webrtc::PeerConnectionDependencies(nullptr));
1277 callee_ = CreatePeerConnectionWrapper(
1278 "Callee", nullptr, &callee_options, nullptr,
1279 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001280 return caller_ && callee_;
1281 }
1282
Seth Hampson2f0d7022018-02-20 11:54:42 -08001283 std::unique_ptr<PeerConnectionWrapper>
1284 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001285 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1286 new FakeRTCCertificateGenerator());
1287 cert_generator->use_alternate_key();
1288
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001289 webrtc::PeerConnectionDependencies dependencies(nullptr);
1290 dependencies.cert_generator = std::move(cert_generator);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001291 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr, nullptr,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001292 std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -07001293 }
1294
1295 // Once called, SDP blobs and ICE candidates will be automatically signaled
1296 // between PeerConnections.
1297 void ConnectFakeSignaling() {
1298 caller_->set_signaling_message_receiver(callee_.get());
1299 callee_->set_signaling_message_receiver(caller_.get());
1300 }
1301
Steve Antonede9ca52017-10-16 13:04:27 -07001302 // Once called, SDP blobs will be automatically signaled between
1303 // PeerConnections. Note that ICE candidates will not be signaled unless they
1304 // are in the exchanged SDP blobs.
1305 void ConnectFakeSignalingForSdpOnly() {
1306 ConnectFakeSignaling();
1307 SetSignalIceCandidates(false);
1308 }
1309
deadbeef1dcb1642017-03-29 21:08:16 -07001310 void SetSignalingDelayMs(int delay_ms) {
1311 caller_->set_signaling_delay_ms(delay_ms);
1312 callee_->set_signaling_delay_ms(delay_ms);
1313 }
1314
Steve Antonede9ca52017-10-16 13:04:27 -07001315 void SetSignalIceCandidates(bool signal) {
1316 caller_->set_signal_ice_candidates(signal);
1317 callee_->set_signal_ice_candidates(signal);
1318 }
1319
deadbeef1dcb1642017-03-29 21:08:16 -07001320 void EnableVideoDecoderFactory() {
1321 caller_->EnableVideoDecoderFactory();
1322 callee_->EnableVideoDecoderFactory();
1323 }
1324
1325 // Messages may get lost on the unreliable DataChannel, so we send multiple
1326 // times to avoid test flakiness.
1327 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1328 const std::string& data,
1329 int retries) {
1330 for (int i = 0; i < retries; ++i) {
1331 dc->Send(DataBuffer(data));
1332 }
1333 }
1334
1335 rtc::Thread* network_thread() { return network_thread_.get(); }
1336
1337 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1338
1339 PeerConnectionWrapper* caller() { return caller_.get(); }
1340
1341 // Set the |caller_| to the |wrapper| passed in and return the
1342 // original |caller_|.
1343 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1344 PeerConnectionWrapper* wrapper) {
1345 PeerConnectionWrapper* old = caller_.release();
1346 caller_.reset(wrapper);
1347 return old;
1348 }
1349
1350 PeerConnectionWrapper* callee() { return callee_.get(); }
1351
1352 // Set the |callee_| to the |wrapper| passed in and return the
1353 // original |callee_|.
1354 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1355 PeerConnectionWrapper* wrapper) {
1356 PeerConnectionWrapper* old = callee_.release();
1357 callee_.reset(wrapper);
1358 return old;
1359 }
1360
Steve Antonede9ca52017-10-16 13:04:27 -07001361 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1362
Seth Hampson2f0d7022018-02-20 11:54:42 -08001363 // Expects the provided number of new frames to be received within
1364 // kMaxWaitForFramesMs. The new expected frames are specified in
1365 // |media_expectations|. Returns false if any of the expectations were
1366 // not met.
1367 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1368 // First initialize the expected frame counts based upon the current
1369 // frame count.
1370 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1371 if (media_expectations.caller_audio_expectation_ ==
1372 MediaExpectations::kExpectSomeFrames) {
1373 total_caller_audio_frames_expected +=
1374 media_expectations.caller_audio_frames_expected_;
1375 }
1376 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001377 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001378 if (media_expectations.caller_video_expectation_ ==
1379 MediaExpectations::kExpectSomeFrames) {
1380 total_caller_video_frames_expected +=
1381 media_expectations.caller_video_frames_expected_;
1382 }
1383 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1384 if (media_expectations.callee_audio_expectation_ ==
1385 MediaExpectations::kExpectSomeFrames) {
1386 total_callee_audio_frames_expected +=
1387 media_expectations.callee_audio_frames_expected_;
1388 }
1389 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001390 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001391 if (media_expectations.callee_video_expectation_ ==
1392 MediaExpectations::kExpectSomeFrames) {
1393 total_callee_video_frames_expected +=
1394 media_expectations.callee_video_frames_expected_;
1395 }
deadbeef1dcb1642017-03-29 21:08:16 -07001396
Seth Hampson2f0d7022018-02-20 11:54:42 -08001397 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001398 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001399 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001400 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001401 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001402 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001403 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001404 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001405 total_callee_video_frames_expected,
1406 kMaxWaitForFramesMs);
1407 bool expectations_correct =
1408 caller()->audio_frames_received() >=
1409 total_caller_audio_frames_expected &&
1410 caller()->min_video_frames_received_per_track() >=
1411 total_caller_video_frames_expected &&
1412 callee()->audio_frames_received() >=
1413 total_callee_audio_frames_expected &&
1414 callee()->min_video_frames_received_per_track() >=
1415 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001416
Seth Hampson2f0d7022018-02-20 11:54:42 -08001417 // After the combined wait, print out a more detailed message upon
1418 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001419 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001420 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001421 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001422 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001423 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001424 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001425 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001426 total_callee_video_frames_expected);
1427
1428 // We want to make sure nothing unexpected was received.
1429 if (media_expectations.caller_audio_expectation_ ==
1430 MediaExpectations::kExpectNoFrames) {
1431 EXPECT_EQ(caller()->audio_frames_received(),
1432 total_caller_audio_frames_expected);
1433 if (caller()->audio_frames_received() !=
1434 total_caller_audio_frames_expected) {
1435 expectations_correct = false;
1436 }
1437 }
1438 if (media_expectations.caller_video_expectation_ ==
1439 MediaExpectations::kExpectNoFrames) {
1440 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1441 total_caller_video_frames_expected);
1442 if (caller()->min_video_frames_received_per_track() !=
1443 total_caller_video_frames_expected) {
1444 expectations_correct = false;
1445 }
1446 }
1447 if (media_expectations.callee_audio_expectation_ ==
1448 MediaExpectations::kExpectNoFrames) {
1449 EXPECT_EQ(callee()->audio_frames_received(),
1450 total_callee_audio_frames_expected);
1451 if (callee()->audio_frames_received() !=
1452 total_callee_audio_frames_expected) {
1453 expectations_correct = false;
1454 }
1455 }
1456 if (media_expectations.callee_video_expectation_ ==
1457 MediaExpectations::kExpectNoFrames) {
1458 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1459 total_callee_video_frames_expected);
1460 if (callee()->min_video_frames_received_per_track() !=
1461 total_callee_video_frames_expected) {
1462 expectations_correct = false;
1463 }
1464 }
1465 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001466 }
1467
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001468 void TestNegotiatedCipherSuite(
1469 const PeerConnectionFactory::Options& caller_options,
1470 const PeerConnectionFactory::Options& callee_options,
1471 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001472 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1473 callee_options));
1474 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1475 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1476 caller()->pc()->RegisterUMAObserver(caller_observer);
1477 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001478 caller()->AddAudioVideoTracks();
1479 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001480 caller()->CreateAndSetAndSignalOffer();
1481 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1482 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001483 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001484 EXPECT_EQ(
1485 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1486 expected_cipher_suite));
1487 caller()->pc()->RegisterUMAObserver(nullptr);
1488 }
1489
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001490 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1491 bool remote_gcm_enabled,
1492 int expected_cipher_suite) {
1493 PeerConnectionFactory::Options caller_options;
1494 caller_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled;
1495 PeerConnectionFactory::Options callee_options;
1496 callee_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled;
1497 TestNegotiatedCipherSuite(caller_options, callee_options,
1498 expected_cipher_suite);
1499 }
1500
Seth Hampson2f0d7022018-02-20 11:54:42 -08001501 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001502 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001503
deadbeef1dcb1642017-03-29 21:08:16 -07001504 private:
1505 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001506 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001507 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001508 // |network_thread_| and |worker_thread_| are used by both
1509 // |caller_| and |callee_| so they must be destroyed
1510 // later.
1511 std::unique_ptr<rtc::Thread> network_thread_;
1512 std::unique_ptr<rtc::Thread> worker_thread_;
1513 std::unique_ptr<PeerConnectionWrapper> caller_;
1514 std::unique_ptr<PeerConnectionWrapper> callee_;
1515};
1516
Seth Hampson2f0d7022018-02-20 11:54:42 -08001517class PeerConnectionIntegrationTest
1518 : public PeerConnectionIntegrationBaseTest,
1519 public ::testing::WithParamInterface<SdpSemantics> {
1520 protected:
1521 PeerConnectionIntegrationTest()
1522 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1523};
1524
1525class PeerConnectionIntegrationTestPlanB
1526 : public PeerConnectionIntegrationBaseTest {
1527 protected:
1528 PeerConnectionIntegrationTestPlanB()
1529 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1530};
1531
1532class PeerConnectionIntegrationTestUnifiedPlan
1533 : public PeerConnectionIntegrationBaseTest {
1534 protected:
1535 PeerConnectionIntegrationTestUnifiedPlan()
1536 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1537};
1538
deadbeef1dcb1642017-03-29 21:08:16 -07001539// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1540// includes testing that the callback is invoked if an observer is connected
1541// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001542TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001543 RtpReceiverObserverOnFirstPacketReceived) {
1544 ASSERT_TRUE(CreatePeerConnectionWrappers());
1545 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001546 caller()->AddAudioVideoTracks();
1547 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001548 // Start offer/answer exchange and wait for it to complete.
1549 caller()->CreateAndSetAndSignalOffer();
1550 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1551 // Should be one receiver each for audio/video.
1552 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1553 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1554 // Wait for all "first packet received" callbacks to be fired.
1555 EXPECT_TRUE_WAIT(
1556 std::all_of(caller()->rtp_receiver_observers().begin(),
1557 caller()->rtp_receiver_observers().end(),
1558 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1559 return o->first_packet_received();
1560 }),
1561 kMaxWaitForFramesMs);
1562 EXPECT_TRUE_WAIT(
1563 std::all_of(callee()->rtp_receiver_observers().begin(),
1564 callee()->rtp_receiver_observers().end(),
1565 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1566 return o->first_packet_received();
1567 }),
1568 kMaxWaitForFramesMs);
1569 // If new observers are set after the first packet was already received, the
1570 // callback should still be invoked.
1571 caller()->ResetRtpReceiverObservers();
1572 callee()->ResetRtpReceiverObservers();
1573 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1574 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1575 EXPECT_TRUE(
1576 std::all_of(caller()->rtp_receiver_observers().begin(),
1577 caller()->rtp_receiver_observers().end(),
1578 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1579 return o->first_packet_received();
1580 }));
1581 EXPECT_TRUE(
1582 std::all_of(callee()->rtp_receiver_observers().begin(),
1583 callee()->rtp_receiver_observers().end(),
1584 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1585 return o->first_packet_received();
1586 }));
1587}
1588
1589class DummyDtmfObserver : public DtmfSenderObserverInterface {
1590 public:
1591 DummyDtmfObserver() : completed_(false) {}
1592
1593 // Implements DtmfSenderObserverInterface.
1594 void OnToneChange(const std::string& tone) override {
1595 tones_.push_back(tone);
1596 if (tone.empty()) {
1597 completed_ = true;
1598 }
1599 }
1600
1601 const std::vector<std::string>& tones() const { return tones_; }
1602 bool completed() const { return completed_; }
1603
1604 private:
1605 bool completed_;
1606 std::vector<std::string> tones_;
1607};
1608
1609// Assumes |sender| already has an audio track added and the offer/answer
1610// exchange is done.
1611void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1612 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001613 // We should be able to get a DTMF sender from the local sender.
1614 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1615 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1616 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001617 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001618 dtmf_sender->RegisterObserver(&observer);
1619
1620 // Test the DtmfSender object just created.
1621 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1622 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1623
1624 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1625 std::vector<std::string> tones = {"1", "a", ""};
1626 EXPECT_EQ(tones, observer.tones());
1627 dtmf_sender->UnregisterObserver();
1628 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1629}
1630
1631// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1632// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001633TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001634 ASSERT_TRUE(CreatePeerConnectionWrappers());
1635 ConnectFakeSignaling();
1636 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001637 caller()->AddAudioTrack();
1638 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001639 caller()->CreateAndSetAndSignalOffer();
1640 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001641 // DTLS must finish before the DTMF sender can be used reliably.
1642 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001643 TestDtmfFromSenderToReceiver(caller(), callee());
1644 TestDtmfFromSenderToReceiver(callee(), caller());
1645}
1646
1647// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1648// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001649TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001650 ASSERT_TRUE(CreatePeerConnectionWrappers());
1651 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001652 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1653 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1654 caller()->pc()->RegisterUMAObserver(caller_observer);
1655
deadbeef1dcb1642017-03-29 21:08:16 -07001656 // Do normal offer/answer and wait for some frames to be received in each
1657 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001658 caller()->AddAudioVideoTracks();
1659 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001660 caller()->CreateAndSetAndSignalOffer();
1661 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001662 MediaExpectations media_expectations;
1663 media_expectations.ExpectBidirectionalAudioAndVideo();
1664 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001665 EXPECT_LE(
1666 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1667 webrtc::kEnumCounterKeyProtocolDtls));
1668 EXPECT_EQ(
1669 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1670 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001671}
1672
1673// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001674TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001675 PeerConnectionInterface::RTCConfiguration sdes_config;
1676 sdes_config.enable_dtls_srtp.emplace(false);
1677 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1678 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001679 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1680 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1681 caller()->pc()->RegisterUMAObserver(caller_observer);
deadbeef1dcb1642017-03-29 21:08:16 -07001682
1683 // Do normal offer/answer and wait for some frames to be received in each
1684 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001685 caller()->AddAudioVideoTracks();
1686 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001687 caller()->CreateAndSetAndSignalOffer();
1688 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001689 MediaExpectations media_expectations;
1690 media_expectations.ExpectBidirectionalAudioAndVideo();
1691 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001692 EXPECT_LE(
1693 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1694 webrtc::kEnumCounterKeyProtocolSdes));
1695 EXPECT_EQ(
1696 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1697 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001698}
1699
Steve Anton8c0f7a72017-10-03 10:03:10 -07001700// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1701// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001702TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001703 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1704 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1705 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1706 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1707 return pc->GetRemoteAudioSSLCertificate();
1708 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001709 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1710 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1711 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1712 return pc->GetRemoteAudioSSLCertChain();
1713 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001714
1715 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1716 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1717
1718 // Configure each side with a known certificate so they can be compared later.
1719 PeerConnectionInterface::RTCConfiguration caller_config;
1720 caller_config.enable_dtls_srtp.emplace(true);
1721 caller_config.certificates.push_back(caller_cert);
1722 PeerConnectionInterface::RTCConfiguration callee_config;
1723 callee_config.enable_dtls_srtp.emplace(true);
1724 callee_config.certificates.push_back(callee_cert);
1725 ASSERT_TRUE(
1726 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1727 ConnectFakeSignaling();
1728
1729 // When first initialized, there should not be a remote SSL certificate (and
1730 // calling this method should not crash).
1731 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1732 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001733 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1734 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001735
Steve Anton15324772018-01-16 10:26:49 -08001736 caller()->AddAudioTrack();
1737 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001738 caller()->CreateAndSetAndSignalOffer();
1739 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1740 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1741
1742 // Once DTLS has been connected, each side should return the other's SSL
1743 // certificate when calling GetRemoteAudioSSLCertificate.
1744
1745 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1746 ASSERT_TRUE(caller_remote_cert);
1747 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1748 caller_remote_cert->ToPEMString());
1749
1750 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1751 ASSERT_TRUE(callee_remote_cert);
1752 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1753 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001754
1755 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1756 ASSERT_TRUE(caller_remote_cert_chain);
1757 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1758 auto remote_cert = &caller_remote_cert_chain->Get(0);
1759 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1760 remote_cert->ToPEMString());
1761
1762 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1763 ASSERT_TRUE(callee_remote_cert_chain);
1764 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1765 remote_cert = &callee_remote_cert_chain->Get(0);
1766 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1767 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001768}
1769
deadbeef1dcb1642017-03-29 21:08:16 -07001770// This test sets up a call between two parties with a source resolution of
1771// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001772TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001773 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1774 ASSERT_TRUE(CreatePeerConnectionWrappers());
1775 ConnectFakeSignaling();
1776
Niels Möller5c7efe72018-05-11 10:34:46 +02001777 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1778 webrtc::FakePeriodicVideoSource::Config config;
1779 config.width = 1280;
1780 config.height = 720;
1781 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1782 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001783
1784 // Do normal offer/answer and wait for at least one frame to be received in
1785 // each direction.
1786 caller()->CreateAndSetAndSignalOffer();
1787 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1788 callee()->min_video_frames_received_per_track() > 0,
1789 kMaxWaitForFramesMs);
1790
1791 // Check rendered aspect ratio.
1792 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1793 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1794 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1795 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1796}
1797
1798// This test sets up an one-way call, with media only from caller to
1799// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001800TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001801 ASSERT_TRUE(CreatePeerConnectionWrappers());
1802 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001803 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001804 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001805 MediaExpectations media_expectations;
1806 media_expectations.CalleeExpectsSomeAudioAndVideo();
1807 media_expectations.CallerExpectsNoAudio();
1808 media_expectations.CallerExpectsNoVideo();
1809 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001810}
1811
1812// This test sets up a audio call initially, with the callee rejecting video
1813// initially. Then later the callee decides to upgrade to audio/video, and
1814// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001815TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001816 ASSERT_TRUE(CreatePeerConnectionWrappers());
1817 ConnectFakeSignaling();
1818 // Initially, offer an audio/video stream from the caller, but refuse to
1819 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001820 caller()->AddAudioVideoTracks();
1821 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001822 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1823 PeerConnectionInterface::RTCOfferAnswerOptions options;
1824 options.offer_to_receive_video = 0;
1825 callee()->SetOfferAnswerOptions(options);
1826 } else {
1827 callee()->SetRemoteOfferHandler([this] {
1828 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1829 });
1830 }
deadbeef1dcb1642017-03-29 21:08:16 -07001831 // Do offer/answer and make sure audio is still received end-to-end.
1832 caller()->CreateAndSetAndSignalOffer();
1833 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001834 {
1835 MediaExpectations media_expectations;
1836 media_expectations.ExpectBidirectionalAudio();
1837 media_expectations.ExpectNoVideo();
1838 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1839 }
deadbeef1dcb1642017-03-29 21:08:16 -07001840 // Sanity check that the callee's description has a rejected video section.
1841 ASSERT_NE(nullptr, callee()->pc()->local_description());
1842 const ContentInfo* callee_video_content =
1843 GetFirstVideoContent(callee()->pc()->local_description()->description());
1844 ASSERT_NE(nullptr, callee_video_content);
1845 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001846
deadbeef1dcb1642017-03-29 21:08:16 -07001847 // Now negotiate with video and ensure negotiation succeeds, with video
1848 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001849 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001850 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1851 PeerConnectionInterface::RTCOfferAnswerOptions options;
1852 options.offer_to_receive_video = 1;
1853 callee()->SetOfferAnswerOptions(options);
1854 } else {
1855 callee()->SetRemoteOfferHandler(nullptr);
1856 caller()->SetRemoteOfferHandler([this] {
1857 // The caller creates a new transceiver to receive video on when receiving
1858 // the offer, but by default it is send only.
1859 auto transceivers = caller()->pc()->GetTransceivers();
1860 ASSERT_EQ(3, transceivers.size());
1861 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1862 transceivers[2]->receiver()->media_type());
1863 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1864 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1865 });
1866 }
deadbeef1dcb1642017-03-29 21:08:16 -07001867 callee()->CreateAndSetAndSignalOffer();
1868 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001869 {
1870 // Expect additional audio frames to be received after the upgrade.
1871 MediaExpectations media_expectations;
1872 media_expectations.ExpectBidirectionalAudioAndVideo();
1873 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1874 }
deadbeef1dcb1642017-03-29 21:08:16 -07001875}
1876
deadbeef4389b4d2017-09-07 09:07:36 -07001877// Simpler than the above test; just add an audio track to an established
1878// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001879TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001880 ASSERT_TRUE(CreatePeerConnectionWrappers());
1881 ConnectFakeSignaling();
1882 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001883 caller()->AddVideoTrack();
1884 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001885 caller()->CreateAndSetAndSignalOffer();
1886 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1887 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001888 caller()->AddAudioTrack();
1889 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001890 caller()->CreateAndSetAndSignalOffer();
1891 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1892 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001893 MediaExpectations media_expectations;
1894 media_expectations.ExpectBidirectionalAudioAndVideo();
1895 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07001896}
1897
deadbeef1dcb1642017-03-29 21:08:16 -07001898// This test sets up a call that's transferred to a new caller with a different
1899// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001900TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07001901 ASSERT_TRUE(CreatePeerConnectionWrappers());
1902 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001903 caller()->AddAudioVideoTracks();
1904 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001905 caller()->CreateAndSetAndSignalOffer();
1906 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1907
1908 // Keep the original peer around which will still send packets to the
1909 // receiving client. These SRTP packets will be dropped.
1910 std::unique_ptr<PeerConnectionWrapper> original_peer(
1911 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001912 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001913 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1914 // directly above.
1915 original_peer->pc()->Close();
1916
1917 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001918 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001919 caller()->CreateAndSetAndSignalOffer();
1920 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1921 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001922 MediaExpectations media_expectations;
1923 media_expectations.ExpectBidirectionalAudioAndVideo();
1924 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001925}
1926
1927// This test sets up a call that's transferred to a new callee with a different
1928// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001929TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07001930 ASSERT_TRUE(CreatePeerConnectionWrappers());
1931 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001932 caller()->AddAudioVideoTracks();
1933 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001934 caller()->CreateAndSetAndSignalOffer();
1935 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1936
1937 // Keep the original peer around which will still send packets to the
1938 // receiving client. These SRTP packets will be dropped.
1939 std::unique_ptr<PeerConnectionWrapper> original_peer(
1940 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001941 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001942 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1943 // directly above.
1944 original_peer->pc()->Close();
1945
1946 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001947 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001948 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1949 caller()->CreateAndSetAndSignalOffer();
1950 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1951 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001952 MediaExpectations media_expectations;
1953 media_expectations.ExpectBidirectionalAudioAndVideo();
1954 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001955}
1956
1957// This test sets up a non-bundled call and negotiates bundling at the same
1958// time as starting an ICE restart. When bundling is in effect in the restart,
1959// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001960TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07001961 ASSERT_TRUE(CreatePeerConnectionWrappers());
1962 ConnectFakeSignaling();
1963
Steve Anton15324772018-01-16 10:26:49 -08001964 caller()->AddAudioVideoTracks();
1965 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001966 // Remove the bundle group from the SDP received by the callee.
1967 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
1968 desc->RemoveGroupByName("BUNDLE");
1969 });
1970 caller()->CreateAndSetAndSignalOffer();
1971 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001972 {
1973 MediaExpectations media_expectations;
1974 media_expectations.ExpectBidirectionalAudioAndVideo();
1975 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1976 }
deadbeef1dcb1642017-03-29 21:08:16 -07001977 // Now stop removing the BUNDLE group, and trigger an ICE restart.
1978 callee()->SetReceivedSdpMunger(nullptr);
1979 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
1980 caller()->CreateAndSetAndSignalOffer();
1981 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1982
1983 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001984 {
1985 MediaExpectations media_expectations;
1986 media_expectations.ExpectBidirectionalAudioAndVideo();
1987 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1988 }
deadbeef1dcb1642017-03-29 21:08:16 -07001989}
1990
1991// Test CVO (Coordination of Video Orientation). If a video source is rotated
1992// and both peers support the CVO RTP header extension, the actual video frames
1993// don't need to be encoded in different resolutions, since the rotation is
1994// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001995TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07001996 ASSERT_TRUE(CreatePeerConnectionWrappers());
1997 ConnectFakeSignaling();
1998 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08001999 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002000 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002001 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002002 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2003
2004 // Wait for video frames to be received by both sides.
2005 caller()->CreateAndSetAndSignalOffer();
2006 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2007 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2008 callee()->min_video_frames_received_per_track() > 0,
2009 kMaxWaitForFramesMs);
2010
2011 // Ensure that the aspect ratio is unmodified.
2012 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2013 // not just assumed.
2014 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2015 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2016 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2017 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2018 // Ensure that the CVO bits were surfaced to the renderer.
2019 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2020 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2021}
2022
2023// Test that when the CVO extension isn't supported, video is rotated the
2024// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002025TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002026 ASSERT_TRUE(CreatePeerConnectionWrappers());
2027 ConnectFakeSignaling();
2028 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002029 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002030 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002031 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002032 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2033
2034 // Remove the CVO extension from the offered SDP.
2035 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2036 cricket::VideoContentDescription* video =
2037 GetFirstVideoContentDescription(desc);
2038 video->ClearRtpHeaderExtensions();
2039 });
2040 // Wait for video frames to be received by both sides.
2041 caller()->CreateAndSetAndSignalOffer();
2042 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2043 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2044 callee()->min_video_frames_received_per_track() > 0,
2045 kMaxWaitForFramesMs);
2046
2047 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2048 // rotation.
2049 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2050 // not just assumed.
2051 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2052 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2053 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2054 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2055 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2056 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2057 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2058}
2059
deadbeef1dcb1642017-03-29 21:08:16 -07002060// Test that if the answerer rejects the audio m= section, no audio is sent or
2061// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002062TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002063 ASSERT_TRUE(CreatePeerConnectionWrappers());
2064 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002065 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002066 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2067 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2068 // it will reject the audio m= section completely.
2069 PeerConnectionInterface::RTCOfferAnswerOptions options;
2070 options.offer_to_receive_audio = 0;
2071 callee()->SetOfferAnswerOptions(options);
2072 } else {
2073 // Stopping the audio RtpTransceiver will cause the media section to be
2074 // rejected in the answer.
2075 callee()->SetRemoteOfferHandler([this] {
2076 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2077 });
2078 }
Steve Anton15324772018-01-16 10:26:49 -08002079 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002080 // Do offer/answer and wait for successful end-to-end video frames.
2081 caller()->CreateAndSetAndSignalOffer();
2082 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002083 MediaExpectations media_expectations;
2084 media_expectations.ExpectBidirectionalVideo();
2085 media_expectations.ExpectNoAudio();
2086 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2087
deadbeef1dcb1642017-03-29 21:08:16 -07002088 // Sanity check that the callee's description has a rejected audio section.
2089 ASSERT_NE(nullptr, callee()->pc()->local_description());
2090 const ContentInfo* callee_audio_content =
2091 GetFirstAudioContent(callee()->pc()->local_description()->description());
2092 ASSERT_NE(nullptr, callee_audio_content);
2093 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002094 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2095 // The caller's transceiver should have stopped after receiving the answer.
2096 EXPECT_TRUE(caller()
2097 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2098 ->stopped());
2099 }
deadbeef1dcb1642017-03-29 21:08:16 -07002100}
2101
2102// Test that if the answerer rejects the video m= section, no video is sent or
2103// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002104TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002105 ASSERT_TRUE(CreatePeerConnectionWrappers());
2106 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002107 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002108 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2109 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2110 // it will reject the video m= section completely.
2111 PeerConnectionInterface::RTCOfferAnswerOptions options;
2112 options.offer_to_receive_video = 0;
2113 callee()->SetOfferAnswerOptions(options);
2114 } else {
2115 // Stopping the video RtpTransceiver will cause the media section to be
2116 // rejected in the answer.
2117 callee()->SetRemoteOfferHandler([this] {
2118 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2119 });
2120 }
Steve Anton15324772018-01-16 10:26:49 -08002121 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002122 // Do offer/answer and wait for successful end-to-end audio frames.
2123 caller()->CreateAndSetAndSignalOffer();
2124 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002125 MediaExpectations media_expectations;
2126 media_expectations.ExpectBidirectionalAudio();
2127 media_expectations.ExpectNoVideo();
2128 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2129
deadbeef1dcb1642017-03-29 21:08:16 -07002130 // Sanity check that the callee's description has a rejected video section.
2131 ASSERT_NE(nullptr, callee()->pc()->local_description());
2132 const ContentInfo* callee_video_content =
2133 GetFirstVideoContent(callee()->pc()->local_description()->description());
2134 ASSERT_NE(nullptr, callee_video_content);
2135 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002136 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2137 // The caller's transceiver should have stopped after receiving the answer.
2138 EXPECT_TRUE(caller()
2139 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2140 ->stopped());
2141 }
deadbeef1dcb1642017-03-29 21:08:16 -07002142}
2143
2144// Test that if the answerer rejects both audio and video m= sections, nothing
2145// bad happens.
2146// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2147// test anything but the fact that negotiation succeeds, which doesn't mean
2148// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002149TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002150 ASSERT_TRUE(CreatePeerConnectionWrappers());
2151 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002152 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002153 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2154 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2155 // will reject both audio and video m= sections.
2156 PeerConnectionInterface::RTCOfferAnswerOptions options;
2157 options.offer_to_receive_audio = 0;
2158 options.offer_to_receive_video = 0;
2159 callee()->SetOfferAnswerOptions(options);
2160 } else {
2161 callee()->SetRemoteOfferHandler([this] {
2162 // Stopping all transceivers will cause all media sections to be rejected.
2163 for (auto transceiver : callee()->pc()->GetTransceivers()) {
2164 transceiver->Stop();
2165 }
2166 });
2167 }
deadbeef1dcb1642017-03-29 21:08:16 -07002168 // Do offer/answer and wait for stable signaling state.
2169 caller()->CreateAndSetAndSignalOffer();
2170 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002171
deadbeef1dcb1642017-03-29 21:08:16 -07002172 // Sanity check that the callee's description has rejected m= sections.
2173 ASSERT_NE(nullptr, callee()->pc()->local_description());
2174 const ContentInfo* callee_audio_content =
2175 GetFirstAudioContent(callee()->pc()->local_description()->description());
2176 ASSERT_NE(nullptr, callee_audio_content);
2177 EXPECT_TRUE(callee_audio_content->rejected);
2178 const ContentInfo* callee_video_content =
2179 GetFirstVideoContent(callee()->pc()->local_description()->description());
2180 ASSERT_NE(nullptr, callee_video_content);
2181 EXPECT_TRUE(callee_video_content->rejected);
2182}
2183
2184// This test sets up an audio and video call between two parties. After the
2185// call runs for a while, the caller sends an updated offer with video being
2186// rejected. Once the re-negotiation is done, the video flow should stop and
2187// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002188TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002189 ASSERT_TRUE(CreatePeerConnectionWrappers());
2190 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002191 caller()->AddAudioVideoTracks();
2192 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002193 caller()->CreateAndSetAndSignalOffer();
2194 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002195 {
2196 MediaExpectations media_expectations;
2197 media_expectations.ExpectBidirectionalAudioAndVideo();
2198 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2199 }
deadbeef1dcb1642017-03-29 21:08:16 -07002200 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002201 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2202 caller()->SetGeneratedSdpMunger(
2203 [](cricket::SessionDescription* description) {
2204 for (cricket::ContentInfo& content : description->contents()) {
2205 if (cricket::IsVideoContent(&content)) {
2206 content.rejected = true;
2207 }
2208 }
2209 });
2210 } else {
2211 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2212 }
deadbeef1dcb1642017-03-29 21:08:16 -07002213 caller()->CreateAndSetAndSignalOffer();
2214 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2215
2216 // Sanity check that the caller's description has a rejected video section.
2217 ASSERT_NE(nullptr, caller()->pc()->local_description());
2218 const ContentInfo* caller_video_content =
2219 GetFirstVideoContent(caller()->pc()->local_description()->description());
2220 ASSERT_NE(nullptr, caller_video_content);
2221 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002222 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002223 {
2224 MediaExpectations media_expectations;
2225 media_expectations.ExpectBidirectionalAudio();
2226 media_expectations.ExpectNoVideo();
2227 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2228 }
deadbeef1dcb1642017-03-29 21:08:16 -07002229}
2230
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002231// Do one offer/answer with audio, another that disables it (rejecting the m=
2232// section), and another that re-enables it. Regression test for:
2233// bugs.webrtc.org/6023
2234TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2235 ASSERT_TRUE(CreatePeerConnectionWrappers());
2236 ConnectFakeSignaling();
2237
2238 // Add audio track, do normal offer/answer.
2239 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2240 caller()->CreateLocalAudioTrack();
2241 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2242 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2243 caller()->CreateAndSetAndSignalOffer();
2244 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2245
2246 // Remove audio track, and set offer_to_receive_audio to false to cause the
2247 // m= section to be completely disabled, not just "recvonly".
2248 caller()->pc()->RemoveTrack(sender);
2249 PeerConnectionInterface::RTCOfferAnswerOptions options;
2250 options.offer_to_receive_audio = 0;
2251 caller()->SetOfferAnswerOptions(options);
2252 caller()->CreateAndSetAndSignalOffer();
2253 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2254
2255 // Add the audio track again, expecting negotiation to succeed and frames to
2256 // flow.
2257 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2258 options.offer_to_receive_audio = 1;
2259 caller()->SetOfferAnswerOptions(options);
2260 caller()->CreateAndSetAndSignalOffer();
2261 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2262
2263 MediaExpectations media_expectations;
2264 media_expectations.CalleeExpectsSomeAudio();
2265 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2266}
2267
deadbeef1dcb1642017-03-29 21:08:16 -07002268// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2269// is needed to support legacy endpoints.
2270// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2271// add a test for an end-to-end test without MID signaling either (basically,
2272// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002273TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002274 ASSERT_TRUE(CreatePeerConnectionWrappers());
2275 ConnectFakeSignaling();
2276 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002277 caller()->AddAudioVideoTracks();
2278 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002279 // Remove SSRCs and MSIDs from the received offer SDP.
2280 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002281 caller()->CreateAndSetAndSignalOffer();
2282 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002283 MediaExpectations media_expectations;
2284 media_expectations.ExpectBidirectionalAudioAndVideo();
2285 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002286}
2287
Seth Hampson5897a6e2018-04-03 11:16:33 -07002288// Basic end-to-end test, without SSRC signaling. This means that the track
2289// was created properly and frames are delivered when the MSIDs are communicated
2290// with a=msid lines and no a=ssrc lines.
2291TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2292 EndToEndCallWithoutSsrcSignaling) {
2293 const char kStreamId[] = "streamId";
2294 ASSERT_TRUE(CreatePeerConnectionWrappers());
2295 ConnectFakeSignaling();
2296 // Add just audio tracks.
2297 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2298 callee()->AddAudioTrack();
2299
2300 // Remove SSRCs from the received offer SDP.
2301 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2302 caller()->CreateAndSetAndSignalOffer();
2303 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2304 MediaExpectations media_expectations;
2305 media_expectations.ExpectBidirectionalAudio();
2306 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2307}
2308
Steve Antondf527fd2018-04-27 15:52:03 -07002309// Tests that video flows between multiple video tracks when SSRCs are not
2310// signaled. This exercises the MID RTP header extension which is needed to
2311// demux the incoming video tracks.
2312TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2313 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2314 ASSERT_TRUE(CreatePeerConnectionWrappers());
2315 ConnectFakeSignaling();
2316 caller()->AddVideoTrack();
2317 caller()->AddVideoTrack();
2318 callee()->AddVideoTrack();
2319 callee()->AddVideoTrack();
2320
2321 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2322 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2323 caller()->CreateAndSetAndSignalOffer();
2324 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2325 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2326 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2327
2328 // Expect video to be received in both directions on both tracks.
2329 MediaExpectations media_expectations;
2330 media_expectations.ExpectBidirectionalVideo();
2331 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2332}
2333
deadbeef1dcb1642017-03-29 21:08:16 -07002334// Test that if two video tracks are sent (from caller to callee, in this test),
2335// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002336TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002337 ASSERT_TRUE(CreatePeerConnectionWrappers());
2338 ConnectFakeSignaling();
2339 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002340 caller()->AddAudioVideoTracks();
2341 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002342 caller()->CreateAndSetAndSignalOffer();
2343 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002344 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002345
2346 MediaExpectations media_expectations;
2347 media_expectations.CalleeExpectsSomeAudioAndVideo();
2348 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002349}
2350
2351static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2352 bool first = true;
2353 for (cricket::ContentInfo& content : desc->contents()) {
2354 if (first) {
2355 first = false;
2356 continue;
2357 }
2358 content.bundle_only = true;
2359 }
2360 first = true;
2361 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2362 if (first) {
2363 first = false;
2364 continue;
2365 }
2366 transport.description.ice_ufrag.clear();
2367 transport.description.ice_pwd.clear();
2368 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2369 transport.description.identity_fingerprint.reset(nullptr);
2370 }
2371}
2372
2373// Test that if applying a true "max bundle" offer, which uses ports of 0,
2374// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2375// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2376// successfully and media flows.
2377// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2378// TODO(deadbeef): Won't need this test once we start generating actual
2379// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002380TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002381 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2382 ASSERT_TRUE(CreatePeerConnectionWrappers());
2383 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002384 caller()->AddAudioVideoTracks();
2385 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002386 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2387 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2388 // but the first m= section.
2389 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2390 caller()->CreateAndSetAndSignalOffer();
2391 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002392 MediaExpectations media_expectations;
2393 media_expectations.ExpectBidirectionalAudioAndVideo();
2394 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002395}
2396
2397// Test that we can receive the audio output level from a remote audio track.
2398// TODO(deadbeef): Use a fake audio source and verify that the output level is
2399// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002400TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002401 ASSERT_TRUE(CreatePeerConnectionWrappers());
2402 ConnectFakeSignaling();
2403 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002404 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002405 caller()->CreateAndSetAndSignalOffer();
2406 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2407
2408 // Get the audio output level stats. Note that the level is not available
2409 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002410 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002411 kMaxWaitForFramesMs);
2412}
2413
2414// Test that an audio input level is reported.
2415// TODO(deadbeef): Use a fake audio source and verify that the input level is
2416// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002417TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002418 ASSERT_TRUE(CreatePeerConnectionWrappers());
2419 ConnectFakeSignaling();
2420 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002421 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002422 caller()->CreateAndSetAndSignalOffer();
2423 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2424
2425 // Get the audio input level stats. The level should be available very
2426 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002427 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002428 kMaxWaitForStatsMs);
2429}
2430
2431// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002432TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002433 ASSERT_TRUE(CreatePeerConnectionWrappers());
2434 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002435 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002436 // Do offer/answer, wait for the callee to receive some frames.
2437 caller()->CreateAndSetAndSignalOffer();
2438 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002439
2440 MediaExpectations media_expectations;
2441 media_expectations.CalleeExpectsSomeAudioAndVideo();
2442 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002443
2444 // Get a handle to the remote tracks created, so they can be used as GetStats
2445 // filters.
Steve Anton15324772018-01-16 10:26:49 -08002446 for (auto receiver : callee()->pc()->GetReceivers()) {
2447 // We received frames, so we definitely should have nonzero "received bytes"
2448 // stats at this point.
2449 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2450 0);
2451 }
deadbeef1dcb1642017-03-29 21:08:16 -07002452}
2453
2454// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002455TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002456 ASSERT_TRUE(CreatePeerConnectionWrappers());
2457 ConnectFakeSignaling();
2458 auto audio_track = caller()->CreateLocalAudioTrack();
2459 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002460 caller()->AddTrack(audio_track);
2461 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002462 // Do offer/answer, wait for the callee to receive some frames.
2463 caller()->CreateAndSetAndSignalOffer();
2464 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002465 MediaExpectations media_expectations;
2466 media_expectations.CalleeExpectsSomeAudioAndVideo();
2467 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002468
2469 // The callee received frames, so we definitely should have nonzero "sent
2470 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002471 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2472 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2473}
2474
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002475// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002476TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002477 ASSERT_TRUE(CreatePeerConnectionWrappers());
2478 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002479 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002480
Steve Anton15324772018-01-16 10:26:49 -08002481 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002482
2483 // Do offer/answer, wait for the callee to receive some frames.
2484 caller()->CreateAndSetAndSignalOffer();
2485 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2486
2487 // Get the remote audio track created on the receiver, so they can be used as
2488 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002489 auto receivers = callee()->pc()->GetReceivers();
2490 ASSERT_EQ(1u, receivers.size());
2491 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002492
2493 // Get the audio output level stats. Note that the level is not available
2494 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002495 EXPECT_TRUE_WAIT(
2496 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2497 0,
2498 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002499}
2500
deadbeefd8ad7882017-04-18 16:01:17 -07002501// Test that we can get stats (using the new stats implemnetation) for
2502// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2503// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002504TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002505 GetStatsForUnsignaledStreamWithNewStatsApi) {
2506 ASSERT_TRUE(CreatePeerConnectionWrappers());
2507 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002508 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002509 // Remove SSRCs and MSIDs from the received offer SDP.
2510 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2511 caller()->CreateAndSetAndSignalOffer();
2512 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002513 MediaExpectations media_expectations;
2514 media_expectations.CalleeExpectsSomeAudio(1);
2515 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002516
2517 // We received a frame, so we should have nonzero "bytes received" stats for
2518 // the unsignaled stream, if stats are working for it.
2519 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2520 callee()->NewGetStats();
2521 ASSERT_NE(nullptr, report);
2522 auto inbound_stream_stats =
2523 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2524 ASSERT_EQ(1U, inbound_stream_stats.size());
2525 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2526 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002527 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2528}
2529
2530// Test that we can successfully get the media related stats (audio level
2531// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002532TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002533 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2534 ASSERT_TRUE(CreatePeerConnectionWrappers());
2535 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002536 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002537 // Remove SSRCs and MSIDs from the received offer SDP.
2538 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2539 caller()->CreateAndSetAndSignalOffer();
2540 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002541 MediaExpectations media_expectations;
2542 media_expectations.CalleeExpectsSomeAudio(1);
2543 media_expectations.CalleeExpectsSomeVideo(1);
2544 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002545
2546 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2547 callee()->NewGetStats();
2548 ASSERT_NE(nullptr, report);
2549
2550 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2551 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2552 ASSERT_GE(audio_index, 0);
2553 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002554}
2555
deadbeef4e2deab2017-09-20 13:56:21 -07002556// Helper for test below.
2557void ModifySsrcs(cricket::SessionDescription* desc) {
2558 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002559 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002560 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002561 for (uint32_t& ssrc : stream.ssrcs) {
2562 ssrc = rtc::CreateRandomId();
2563 }
2564 }
2565 }
2566}
2567
2568// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2569// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2570// This should result in two "RTCInboundRTPStreamStats", but only one
2571// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2572// being reset to 0 once the SSRC change occurs.
2573//
2574// Regression test for this bug:
2575// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2576//
2577// The bug causes the track stats to only represent one of the two streams:
2578// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2579// that the track stat counters would reset to 0 when the new stream is
2580// received, and a 50% chance that they'll stop updating (while
2581// "concealed_samples" continues increasing, due to silence being generated for
2582// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002583TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002584 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002585 ASSERT_TRUE(CreatePeerConnectionWrappers());
2586 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002587 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002588 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2589 // that doesn't signal SSRCs (from the callee's perspective).
2590 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2591 caller()->CreateAndSetAndSignalOffer();
2592 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2593 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002594 {
2595 MediaExpectations media_expectations;
2596 media_expectations.CalleeExpectsSomeAudio(50);
2597 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2598 }
deadbeef4e2deab2017-09-20 13:56:21 -07002599 // Some audio frames were received, so we should have nonzero "samples
2600 // received" for the track.
2601 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2602 callee()->NewGetStats();
2603 ASSERT_NE(nullptr, report);
2604 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2605 ASSERT_EQ(1U, track_stats.size());
2606 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2607 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2608 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2609
2610 // Create a new offer and munge it to cause the caller to use a new SSRC.
2611 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2612 caller()->CreateAndSetAndSignalOffer();
2613 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2614 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2615 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002616 {
2617 MediaExpectations media_expectations;
2618 media_expectations.CalleeExpectsSomeAudio(25);
2619 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2620 }
deadbeef4e2deab2017-09-20 13:56:21 -07002621
2622 report = callee()->NewGetStats();
2623 ASSERT_NE(nullptr, report);
2624 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2625 ASSERT_EQ(1U, track_stats.size());
2626 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2627 // The "total samples received" stat should only be greater than it was
2628 // before.
2629 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2630 // Right now, the new SSRC will cause the counters to reset to 0.
2631 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2632
2633 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002634 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002635 // good sign that we're seeing stats from the old stream that's no longer
2636 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002637 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002638 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2639 EXPECT_LT(*track_stats[0]->concealed_samples,
2640 *track_stats[0]->total_samples_received *
2641 kAcceptableConcealedSamplesPercentage);
2642
2643 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2644 // sanity check that the SSRC really changed.
2645 // TODO(deadbeef): This isn't working right now, because we're not returning
2646 // *any* stats for the inactive stream. Uncomment when the bug is completely
2647 // fixed.
2648 // auto inbound_stream_stats =
2649 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2650 // ASSERT_EQ(2U, inbound_stream_stats.size());
2651}
2652
deadbeef1dcb1642017-03-29 21:08:16 -07002653// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002654TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002655 PeerConnectionFactory::Options dtls_10_options;
2656 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2657 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2658 dtls_10_options));
2659 ConnectFakeSignaling();
2660 // Do normal offer/answer and wait for some frames to be received in each
2661 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002662 caller()->AddAudioVideoTracks();
2663 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002664 caller()->CreateAndSetAndSignalOffer();
2665 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002666 MediaExpectations media_expectations;
2667 media_expectations.ExpectBidirectionalAudioAndVideo();
2668 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002669}
2670
2671// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002672TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002673 PeerConnectionFactory::Options dtls_10_options;
2674 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2675 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2676 dtls_10_options));
2677 ConnectFakeSignaling();
2678 // Register UMA observer before signaling begins.
2679 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2680 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2681 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002682 caller()->AddAudioVideoTracks();
2683 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002684 caller()->CreateAndSetAndSignalOffer();
2685 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2686 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002687 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002688 kDefaultTimeout);
2689 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002690 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002691 EXPECT_EQ(1,
2692 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2693 kDefaultSrtpCryptoSuite));
2694}
2695
2696// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002697TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002698 PeerConnectionFactory::Options dtls_12_options;
2699 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2700 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2701 dtls_12_options));
2702 ConnectFakeSignaling();
2703 // Register UMA observer before signaling begins.
2704 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2705 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2706 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002707 caller()->AddAudioVideoTracks();
2708 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002709 caller()->CreateAndSetAndSignalOffer();
2710 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2711 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002712 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002713 kDefaultTimeout);
2714 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002715 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002716 EXPECT_EQ(1,
2717 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2718 kDefaultSrtpCryptoSuite));
2719}
2720
2721// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2722// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002723TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002724 PeerConnectionFactory::Options caller_options;
2725 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2726 PeerConnectionFactory::Options callee_options;
2727 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2728 ASSERT_TRUE(
2729 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2730 ConnectFakeSignaling();
2731 // Do normal offer/answer and wait for some frames to be received in each
2732 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002733 caller()->AddAudioVideoTracks();
2734 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002735 caller()->CreateAndSetAndSignalOffer();
2736 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002737 MediaExpectations media_expectations;
2738 media_expectations.ExpectBidirectionalAudioAndVideo();
2739 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002740}
2741
2742// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2743// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002744TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002745 PeerConnectionFactory::Options caller_options;
2746 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2747 PeerConnectionFactory::Options callee_options;
2748 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2749 ASSERT_TRUE(
2750 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2751 ConnectFakeSignaling();
2752 // Do normal offer/answer and wait for some frames to be received in each
2753 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002754 caller()->AddAudioVideoTracks();
2755 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002756 caller()->CreateAndSetAndSignalOffer();
2757 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002758 MediaExpectations media_expectations;
2759 media_expectations.ExpectBidirectionalAudioAndVideo();
2760 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002761}
2762
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002763// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2764// works as expected; the cipher should only be used if enabled by both sides.
2765TEST_P(PeerConnectionIntegrationTest,
2766 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2767 PeerConnectionFactory::Options caller_options;
2768 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2769 PeerConnectionFactory::Options callee_options;
2770 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2771 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2772 TestNegotiatedCipherSuite(caller_options, callee_options,
2773 expected_cipher_suite);
2774}
2775
2776TEST_P(PeerConnectionIntegrationTest,
2777 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2778 PeerConnectionFactory::Options caller_options;
2779 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2780 PeerConnectionFactory::Options callee_options;
2781 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2782 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2783 TestNegotiatedCipherSuite(caller_options, callee_options,
2784 expected_cipher_suite);
2785}
2786
2787TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2788 PeerConnectionFactory::Options caller_options;
2789 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2790 PeerConnectionFactory::Options callee_options;
2791 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2792 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
2793 TestNegotiatedCipherSuite(caller_options, callee_options,
2794 expected_cipher_suite);
2795}
2796
deadbeef1dcb1642017-03-29 21:08:16 -07002797// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002798TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002799 bool local_gcm_enabled = false;
2800 bool remote_gcm_enabled = false;
2801 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2802 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2803 expected_cipher_suite);
2804}
2805
2806// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002807TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002808 bool local_gcm_enabled = true;
2809 bool remote_gcm_enabled = true;
2810 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2811 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2812 expected_cipher_suite);
2813}
2814
2815// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002816TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002817 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2818 bool local_gcm_enabled = true;
2819 bool remote_gcm_enabled = false;
2820 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2821 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2822 expected_cipher_suite);
2823}
2824
2825// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002826TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002827 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2828 bool local_gcm_enabled = false;
2829 bool remote_gcm_enabled = true;
2830 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2831 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2832 expected_cipher_suite);
2833}
2834
deadbeef7914b8c2017-04-21 03:23:33 -07002835// Verify that media can be transmitted end-to-end when GCM crypto suites are
2836// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2837// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2838// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002839TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07002840 PeerConnectionFactory::Options gcm_options;
2841 gcm_options.crypto_options.enable_gcm_crypto_suites = true;
2842 ASSERT_TRUE(
2843 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2844 ConnectFakeSignaling();
2845 // Do normal offer/answer and wait for some frames to be received in each
2846 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002847 caller()->AddAudioVideoTracks();
2848 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002849 caller()->CreateAndSetAndSignalOffer();
2850 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002851 MediaExpectations media_expectations;
2852 media_expectations.ExpectBidirectionalAudioAndVideo();
2853 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07002854}
2855
deadbeef1dcb1642017-03-29 21:08:16 -07002856// This test sets up a call between two parties with audio, video and an RTP
2857// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002858TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002859 FakeConstraints setup_constraints;
2860 setup_constraints.SetAllowRtpDataChannels();
2861 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2862 &setup_constraints));
2863 ConnectFakeSignaling();
2864 // Expect that data channel created on caller side will show up for callee as
2865 // well.
2866 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002867 caller()->AddAudioVideoTracks();
2868 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002869 caller()->CreateAndSetAndSignalOffer();
2870 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2871 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002872 MediaExpectations media_expectations;
2873 media_expectations.ExpectBidirectionalAudioAndVideo();
2874 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002875 ASSERT_NE(nullptr, caller()->data_channel());
2876 ASSERT_NE(nullptr, callee()->data_channel());
2877 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2878 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2879
2880 // Ensure data can be sent in both directions.
2881 std::string data = "hello world";
2882 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2883 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2884 kDefaultTimeout);
2885 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2886 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2887 kDefaultTimeout);
2888}
2889
2890// Ensure that an RTP data channel is signaled as closed for the caller when
2891// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002892TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002893 RtpDataChannelSignaledClosedInCalleeOffer) {
2894 // Same procedure as above test.
2895 FakeConstraints setup_constraints;
2896 setup_constraints.SetAllowRtpDataChannels();
2897 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2898 &setup_constraints));
2899 ConnectFakeSignaling();
2900 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002901 caller()->AddAudioVideoTracks();
2902 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002903 caller()->CreateAndSetAndSignalOffer();
2904 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2905 ASSERT_NE(nullptr, caller()->data_channel());
2906 ASSERT_NE(nullptr, callee()->data_channel());
2907 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2908 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2909
2910 // Close the data channel on the callee, and do an updated offer/answer.
2911 callee()->data_channel()->Close();
2912 callee()->CreateAndSetAndSignalOffer();
2913 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2914 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2915 EXPECT_FALSE(callee()->data_observer()->IsOpen());
2916}
2917
2918// Tests that data is buffered in an RTP data channel until an observer is
2919// registered for it.
2920//
2921// NOTE: RTP data channels can receive data before the underlying
2922// transport has detected that a channel is writable and thus data can be
2923// received before the data channel state changes to open. That is hard to test
2924// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002925TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002926 DataBufferedUntilRtpDataChannelObserverRegistered) {
2927 // Use fake clock and simulated network delay so that we predictably can wait
2928 // until an SCTP message has been delivered without "sleep()"ing.
2929 rtc::ScopedFakeClock fake_clock;
2930 // Some things use a time of "0" as a special value, so we need to start out
2931 // the fake clock at a nonzero time.
2932 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02002933 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07002934 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
2935 virtual_socket_server()->UpdateDelayDistribution();
2936
2937 FakeConstraints constraints;
2938 constraints.SetAllowRtpDataChannels();
2939 ASSERT_TRUE(
2940 CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints));
2941 ConnectFakeSignaling();
2942 caller()->CreateDataChannel();
2943 caller()->CreateAndSetAndSignalOffer();
2944 ASSERT_TRUE(caller()->data_channel() != nullptr);
2945 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
2946 kDefaultTimeout, fake_clock);
2947 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
2948 kDefaultTimeout, fake_clock);
2949 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
2950 callee()->data_channel()->state(), kDefaultTimeout,
2951 fake_clock);
2952
2953 // Unregister the observer which is normally automatically registered.
2954 callee()->data_channel()->UnregisterObserver();
2955 // Send data and advance fake clock until it should have been received.
2956 std::string data = "hello world";
2957 caller()->data_channel()->Send(DataBuffer(data));
2958 SIMULATED_WAIT(false, 50, fake_clock);
2959
2960 // Attach data channel and expect data to be received immediately. Note that
2961 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
2962 // further, but data can be received even if the callback is asynchronous.
2963 MockDataChannelObserver new_observer(callee()->data_channel());
2964 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
2965 fake_clock);
2966}
2967
2968// This test sets up a call between two parties with audio, video and but only
2969// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002970TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002971 FakeConstraints setup_constraints_1;
2972 setup_constraints_1.SetAllowRtpDataChannels();
2973 // Must disable DTLS to make negotiation succeed.
2974 setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2975 false);
2976 FakeConstraints setup_constraints_2;
2977 setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
2978 false);
2979 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(
2980 &setup_constraints_1, &setup_constraints_2));
2981 ConnectFakeSignaling();
2982 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002983 caller()->AddAudioVideoTracks();
2984 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002985 caller()->CreateAndSetAndSignalOffer();
2986 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2987 // The caller should still have a data channel, but it should be closed, and
2988 // one should ever have been created for the callee.
2989 EXPECT_TRUE(caller()->data_channel() != nullptr);
2990 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2991 EXPECT_EQ(nullptr, callee()->data_channel());
2992}
2993
2994// This test sets up a call between two parties with audio, and video. When
2995// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002996TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002997 FakeConstraints setup_constraints;
2998 setup_constraints.SetAllowRtpDataChannels();
2999 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
3000 &setup_constraints));
3001 ConnectFakeSignaling();
3002 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003003 caller()->AddAudioVideoTracks();
3004 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003005 caller()->CreateAndSetAndSignalOffer();
3006 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3007 // Create data channel and do new offer and answer.
3008 caller()->CreateDataChannel();
3009 caller()->CreateAndSetAndSignalOffer();
3010 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3011 ASSERT_NE(nullptr, caller()->data_channel());
3012 ASSERT_NE(nullptr, callee()->data_channel());
3013 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3014 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3015 // Ensure data can be sent in both directions.
3016 std::string data = "hello world";
3017 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3018 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3019 kDefaultTimeout);
3020 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3021 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3022 kDefaultTimeout);
3023}
3024
3025#ifdef HAVE_SCTP
3026
3027// This test sets up a call between two parties with audio, video and an SCTP
3028// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003029TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003030 ASSERT_TRUE(CreatePeerConnectionWrappers());
3031 ConnectFakeSignaling();
3032 // Expect that data channel created on caller side will show up for callee as
3033 // well.
3034 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003035 caller()->AddAudioVideoTracks();
3036 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003037 caller()->CreateAndSetAndSignalOffer();
3038 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3039 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003040 MediaExpectations media_expectations;
3041 media_expectations.ExpectBidirectionalAudioAndVideo();
3042 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003043 // Caller data channel should already exist (it created one). Callee data
3044 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3045 ASSERT_NE(nullptr, caller()->data_channel());
3046 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3047 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3048 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3049
3050 // Ensure data can be sent in both directions.
3051 std::string data = "hello world";
3052 caller()->data_channel()->Send(DataBuffer(data));
3053 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3054 kDefaultTimeout);
3055 callee()->data_channel()->Send(DataBuffer(data));
3056 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3057 kDefaultTimeout);
3058}
3059
3060// Ensure that when the callee closes an SCTP data channel, the closing
3061// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003062TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003063 // Same procedure as above test.
3064 ASSERT_TRUE(CreatePeerConnectionWrappers());
3065 ConnectFakeSignaling();
3066 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003067 caller()->AddAudioVideoTracks();
3068 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003069 caller()->CreateAndSetAndSignalOffer();
3070 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3071 ASSERT_NE(nullptr, caller()->data_channel());
3072 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3073 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3074 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3075
3076 // Close the data channel on the callee side, and wait for it to reach the
3077 // "closed" state on both sides.
3078 callee()->data_channel()->Close();
3079 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3080 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3081}
3082
Seth Hampson2f0d7022018-02-20 11:54:42 -08003083TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003084 ASSERT_TRUE(CreatePeerConnectionWrappers());
3085 ConnectFakeSignaling();
3086 webrtc::DataChannelInit init;
3087 init.id = 53;
3088 init.maxRetransmits = 52;
3089 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003090 caller()->AddAudioVideoTracks();
3091 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003092 caller()->CreateAndSetAndSignalOffer();
3093 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003094 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3095 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07003096 EXPECT_EQ(init.id, callee()->data_channel()->id());
3097 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3098 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3099 EXPECT_FALSE(callee()->data_channel()->negotiated());
3100}
3101
deadbeef1dcb1642017-03-29 21:08:16 -07003102// Test usrsctp's ability to process unordered data stream, where data actually
3103// arrives out of order using simulated delays. Previously there have been some
3104// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003105TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003106 // Introduce random network delays.
3107 // Otherwise it's not a true "unordered" test.
3108 virtual_socket_server()->set_delay_mean(20);
3109 virtual_socket_server()->set_delay_stddev(5);
3110 virtual_socket_server()->UpdateDelayDistribution();
3111 // Normal procedure, but with unordered data channel config.
3112 ASSERT_TRUE(CreatePeerConnectionWrappers());
3113 ConnectFakeSignaling();
3114 webrtc::DataChannelInit init;
3115 init.ordered = false;
3116 caller()->CreateDataChannel(&init);
3117 caller()->CreateAndSetAndSignalOffer();
3118 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3119 ASSERT_NE(nullptr, caller()->data_channel());
3120 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3121 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3122 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3123
3124 static constexpr int kNumMessages = 100;
3125 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3126 static constexpr size_t kMaxMessageSize = 4096;
3127 // Create and send random messages.
3128 std::vector<std::string> sent_messages;
3129 for (int i = 0; i < kNumMessages; ++i) {
3130 size_t length =
3131 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3132 std::string message;
3133 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3134 caller()->data_channel()->Send(DataBuffer(message));
3135 callee()->data_channel()->Send(DataBuffer(message));
3136 sent_messages.push_back(message);
3137 }
3138
3139 // Wait for all messages to be received.
3140 EXPECT_EQ_WAIT(kNumMessages,
3141 caller()->data_observer()->received_message_count(),
3142 kDefaultTimeout);
3143 EXPECT_EQ_WAIT(kNumMessages,
3144 callee()->data_observer()->received_message_count(),
3145 kDefaultTimeout);
3146
3147 // Sort and compare to make sure none of the messages were corrupted.
3148 std::vector<std::string> caller_received_messages =
3149 caller()->data_observer()->messages();
3150 std::vector<std::string> callee_received_messages =
3151 callee()->data_observer()->messages();
3152 std::sort(sent_messages.begin(), sent_messages.end());
3153 std::sort(caller_received_messages.begin(), caller_received_messages.end());
3154 std::sort(callee_received_messages.begin(), callee_received_messages.end());
3155 EXPECT_EQ(sent_messages, caller_received_messages);
3156 EXPECT_EQ(sent_messages, callee_received_messages);
3157}
3158
3159// This test sets up a call between two parties with audio, and video. When
3160// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003161TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003162 ASSERT_TRUE(CreatePeerConnectionWrappers());
3163 ConnectFakeSignaling();
3164 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003165 caller()->AddAudioVideoTracks();
3166 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003167 caller()->CreateAndSetAndSignalOffer();
3168 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3169 // Create data channel and do new offer and answer.
3170 caller()->CreateDataChannel();
3171 caller()->CreateAndSetAndSignalOffer();
3172 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3173 // Caller data channel should already exist (it created one). Callee data
3174 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3175 ASSERT_NE(nullptr, caller()->data_channel());
3176 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3177 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3178 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3179 // Ensure data can be sent in both directions.
3180 std::string data = "hello world";
3181 caller()->data_channel()->Send(DataBuffer(data));
3182 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3183 kDefaultTimeout);
3184 callee()->data_channel()->Send(DataBuffer(data));
3185 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3186 kDefaultTimeout);
3187}
3188
deadbeef7914b8c2017-04-21 03:23:33 -07003189// Set up a connection initially just using SCTP data channels, later upgrading
3190// to audio/video, ensuring frames are received end-to-end. Effectively the
3191// inverse of the test above.
3192// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003193TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003194 ASSERT_TRUE(CreatePeerConnectionWrappers());
3195 ConnectFakeSignaling();
3196 // Do initial offer/answer with just data channel.
3197 caller()->CreateDataChannel();
3198 caller()->CreateAndSetAndSignalOffer();
3199 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3200 // Wait until data can be sent over the data channel.
3201 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3202 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3203 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3204
3205 // Do subsequent offer/answer with two-way audio and video. Audio and video
3206 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003207 caller()->AddAudioVideoTracks();
3208 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003209 caller()->CreateAndSetAndSignalOffer();
3210 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003211 MediaExpectations media_expectations;
3212 media_expectations.ExpectBidirectionalAudioAndVideo();
3213 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003214}
3215
deadbeef8b7e9ad2017-05-25 09:38:55 -07003216static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003217 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003218 GetFirstDataContentDescription(desc);
3219 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003220 dcd_offer->set_use_sctpmap(false);
3221 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3222}
3223
3224// Test that the data channel works when a spec-compliant SCTP m= section is
3225// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3226// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003227TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003228 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3229 ASSERT_TRUE(CreatePeerConnectionWrappers());
3230 ConnectFakeSignaling();
3231 caller()->CreateDataChannel();
3232 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3233 caller()->CreateAndSetAndSignalOffer();
3234 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3235 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3236 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3237 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3238
3239 // Ensure data can be sent in both directions.
3240 std::string data = "hello world";
3241 caller()->data_channel()->Send(DataBuffer(data));
3242 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3243 kDefaultTimeout);
3244 callee()->data_channel()->Send(DataBuffer(data));
3245 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3246 kDefaultTimeout);
3247}
3248
deadbeef1dcb1642017-03-29 21:08:16 -07003249#endif // HAVE_SCTP
3250
3251// Test that the ICE connection and gathering states eventually reach
3252// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003253TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003254 ASSERT_TRUE(CreatePeerConnectionWrappers());
3255 ConnectFakeSignaling();
3256 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003257 caller()->AddAudioVideoTracks();
3258 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003259 caller()->CreateAndSetAndSignalOffer();
3260 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3261 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3262 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3263 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3264 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3265 // After the best candidate pair is selected and all candidates are signaled,
3266 // the ICE connection state should reach "complete".
3267 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3268 // answerer/"callee" by default) only reaches "connected". When this is
3269 // fixed, this test should be updated.
3270 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3271 caller()->ice_connection_state(), kDefaultTimeout);
3272 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3273 callee()->ice_connection_state(), kDefaultTimeout);
3274}
3275
Steve Antonede9ca52017-10-16 13:04:27 -07003276// Test that firewalling the ICE connection causes the clients to identify the
3277// disconnected state and then removing the firewall causes them to reconnect.
3278class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003279 : public PeerConnectionIntegrationBaseTest,
3280 public ::testing::WithParamInterface<
3281 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003282 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003283 PeerConnectionIntegrationIceStatesTest()
3284 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3285 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003286 }
3287
3288 void StartStunServer(const SocketAddress& server_address) {
3289 stun_server_.reset(
3290 cricket::TestStunServer::Create(network_thread(), server_address));
3291 }
3292
3293 bool TestIPv6() {
3294 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3295 }
3296
3297 void SetPortAllocatorFlags() {
Qingsi Wanga2d60672018-04-11 16:57:45 -07003298 network_thread()->Invoke<void>(
3299 RTC_FROM_HERE,
3300 rtc::Bind(&cricket::PortAllocator::set_flags,
3301 caller()->port_allocator(), port_allocator_flags_));
3302 network_thread()->Invoke<void>(
3303 RTC_FROM_HERE,
3304 rtc::Bind(&cricket::PortAllocator::set_flags,
3305 callee()->port_allocator(), port_allocator_flags_));
Steve Antonede9ca52017-10-16 13:04:27 -07003306 }
3307
3308 std::vector<SocketAddress> CallerAddresses() {
3309 std::vector<SocketAddress> addresses;
3310 addresses.push_back(SocketAddress("1.1.1.1", 0));
3311 if (TestIPv6()) {
3312 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3313 }
3314 return addresses;
3315 }
3316
3317 std::vector<SocketAddress> CalleeAddresses() {
3318 std::vector<SocketAddress> addresses;
3319 addresses.push_back(SocketAddress("2.2.2.2", 0));
3320 if (TestIPv6()) {
3321 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3322 }
3323 return addresses;
3324 }
3325
3326 void SetUpNetworkInterfaces() {
3327 // Remove the default interfaces added by the test infrastructure.
3328 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3329 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3330
3331 // Add network addresses for test.
3332 for (const auto& caller_address : CallerAddresses()) {
3333 caller()->network()->AddInterface(caller_address);
3334 }
3335 for (const auto& callee_address : CalleeAddresses()) {
3336 callee()->network()->AddInterface(callee_address);
3337 }
3338 }
3339
3340 private:
3341 uint32_t port_allocator_flags_;
3342 std::unique_ptr<cricket::TestStunServer> stun_server_;
3343};
3344
3345// Tests that the PeerConnection goes through all the ICE gathering/connection
3346// states over the duration of the call. This includes Disconnected and Failed
3347// states, induced by putting a firewall between the peers and waiting for them
3348// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003349TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3350 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3351 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3352 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003353
3354 const SocketAddress kStunServerAddress =
3355 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3356 StartStunServer(kStunServerAddress);
3357
3358 PeerConnectionInterface::RTCConfiguration config;
3359 PeerConnectionInterface::IceServer ice_stun_server;
3360 ice_stun_server.urls.push_back(
3361 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3362 kStunServerAddress.PortAsString());
3363 config.servers.push_back(ice_stun_server);
3364
3365 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3366 ConnectFakeSignaling();
3367 SetPortAllocatorFlags();
3368 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003369 caller()->AddAudioVideoTracks();
3370 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003371
3372 // Initial state before anything happens.
3373 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3374 caller()->ice_gathering_state());
3375 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3376 caller()->ice_connection_state());
3377
3378 // Start the call by creating the offer, setting it as the local description,
3379 // then sending it to the peer who will respond with an answer. This happens
3380 // asynchronously so that we can watch the states as it runs in the
3381 // background.
3382 caller()->CreateAndSetAndSignalOffer();
3383
Steve Anton83119dd2017-11-10 16:19:52 -08003384 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3385 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003386
3387 // Verify that the observer was notified of the intermediate transitions.
3388 EXPECT_THAT(caller()->ice_connection_state_history(),
3389 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3390 PeerConnectionInterface::kIceConnectionConnected,
3391 PeerConnectionInterface::kIceConnectionCompleted));
3392 EXPECT_THAT(caller()->ice_gathering_state_history(),
3393 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3394 PeerConnectionInterface::kIceGatheringComplete));
3395
3396 // Block connections to/from the caller and wait for ICE to become
3397 // disconnected.
3398 for (const auto& caller_address : CallerAddresses()) {
3399 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3400 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003401 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003402 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3403 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003404
3405 // Let ICE re-establish by removing the firewall rules.
3406 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003407 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003408 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3409 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003410
3411 // According to RFC7675, if there is no response within 30 seconds then the
3412 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003413 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003414 constexpr int kConsentTimeout = 30000;
3415 for (const auto& caller_address : CallerAddresses()) {
3416 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3417 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003418 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003419 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3420 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003421}
3422
3423// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3424// and that the statistics in the metric observers are updated correctly.
3425TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3426 ASSERT_TRUE(CreatePeerConnectionWrappers());
3427 ConnectFakeSignaling();
3428 SetPortAllocatorFlags();
3429 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003430 caller()->AddAudioVideoTracks();
3431 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003432
3433 rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer(
3434 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>());
3435 caller()->pc()->RegisterUMAObserver(metrics_observer.get());
3436
3437 caller()->CreateAndSetAndSignalOffer();
3438
3439 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3440
3441 const int num_best_ipv4 = metrics_observer->GetEnumCounter(
3442 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv4);
3443 const int num_best_ipv6 = metrics_observer->GetEnumCounter(
3444 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv6);
3445 if (TestIPv6()) {
3446 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3447 // connection.
3448 EXPECT_EQ(0u, num_best_ipv4);
3449 EXPECT_EQ(1u, num_best_ipv6);
3450 } else {
3451 EXPECT_EQ(1u, num_best_ipv4);
3452 EXPECT_EQ(0u, num_best_ipv6);
3453 }
3454
3455 EXPECT_EQ(0u, metrics_observer->GetEnumCounter(
3456 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3457 webrtc::kIceCandidatePairHostHost));
3458 EXPECT_EQ(1u, metrics_observer->GetEnumCounter(
3459 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3460 webrtc::kIceCandidatePairHostPublicHostPublic));
3461}
3462
3463constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3464 cricket::PORTALLOCATOR_DISABLE_STUN |
3465 cricket::PORTALLOCATOR_DISABLE_RELAY;
3466constexpr uint32_t kFlagsIPv6NoStun =
3467 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3468 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3469constexpr uint32_t kFlagsIPv4Stun =
3470 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3471
Seth Hampson2f0d7022018-02-20 11:54:42 -08003472INSTANTIATE_TEST_CASE_P(
3473 PeerConnectionIntegrationTest,
3474 PeerConnectionIntegrationIceStatesTest,
3475 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3476 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3477 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3478 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003479
deadbeef1dcb1642017-03-29 21:08:16 -07003480// This test sets up a call between two parties with audio and video.
3481// During the call, the caller restarts ICE and the test verifies that
3482// new ICE candidates are generated and audio and video still can flow, and the
3483// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003484TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003485 ASSERT_TRUE(CreatePeerConnectionWrappers());
3486 ConnectFakeSignaling();
3487 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003488 caller()->AddAudioVideoTracks();
3489 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003490 caller()->CreateAndSetAndSignalOffer();
3491 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3492 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3493 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3494 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3495 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3496
3497 // To verify that the ICE restart actually occurs, get
3498 // ufrag/password/candidates before and after restart.
3499 // Create an SDP string of the first audio candidate for both clients.
3500 const webrtc::IceCandidateCollection* audio_candidates_caller =
3501 caller()->pc()->local_description()->candidates(0);
3502 const webrtc::IceCandidateCollection* audio_candidates_callee =
3503 callee()->pc()->local_description()->candidates(0);
3504 ASSERT_GT(audio_candidates_caller->count(), 0u);
3505 ASSERT_GT(audio_candidates_callee->count(), 0u);
3506 std::string caller_candidate_pre_restart;
3507 ASSERT_TRUE(
3508 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3509 std::string callee_candidate_pre_restart;
3510 ASSERT_TRUE(
3511 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3512 const cricket::SessionDescription* desc =
3513 caller()->pc()->local_description()->description();
3514 std::string caller_ufrag_pre_restart =
3515 desc->transport_infos()[0].description.ice_ufrag;
3516 desc = callee()->pc()->local_description()->description();
3517 std::string callee_ufrag_pre_restart =
3518 desc->transport_infos()[0].description.ice_ufrag;
3519
3520 // Have the caller initiate an ICE restart.
3521 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3522 caller()->CreateAndSetAndSignalOffer();
3523 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3524 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3525 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3526 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3527 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3528
3529 // Grab the ufrags/candidates again.
3530 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3531 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3532 ASSERT_GT(audio_candidates_caller->count(), 0u);
3533 ASSERT_GT(audio_candidates_callee->count(), 0u);
3534 std::string caller_candidate_post_restart;
3535 ASSERT_TRUE(
3536 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3537 std::string callee_candidate_post_restart;
3538 ASSERT_TRUE(
3539 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3540 desc = caller()->pc()->local_description()->description();
3541 std::string caller_ufrag_post_restart =
3542 desc->transport_infos()[0].description.ice_ufrag;
3543 desc = callee()->pc()->local_description()->description();
3544 std::string callee_ufrag_post_restart =
3545 desc->transport_infos()[0].description.ice_ufrag;
3546 // Sanity check that an ICE restart was actually negotiated in SDP.
3547 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3548 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3549 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3550 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3551
3552 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003553 MediaExpectations media_expectations;
3554 media_expectations.ExpectBidirectionalAudioAndVideo();
3555 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003556}
3557
3558// Verify that audio/video can be received end-to-end when ICE renomination is
3559// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003560TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07003561 PeerConnectionInterface::RTCConfiguration config;
3562 config.enable_ice_renomination = true;
3563 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3564 ConnectFakeSignaling();
3565 // Do normal offer/answer and wait for some frames to be received in each
3566 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003567 caller()->AddAudioVideoTracks();
3568 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003569 caller()->CreateAndSetAndSignalOffer();
3570 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3571 // Sanity check that ICE renomination was actually negotiated.
3572 const cricket::SessionDescription* desc =
3573 caller()->pc()->local_description()->description();
3574 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003575 ASSERT_NE(
3576 info.description.transport_options.end(),
3577 std::find(info.description.transport_options.begin(),
3578 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003579 }
3580 desc = callee()->pc()->local_description()->description();
3581 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003582 ASSERT_NE(
3583 info.description.transport_options.end(),
3584 std::find(info.description.transport_options.begin(),
3585 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003586 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08003587 MediaExpectations media_expectations;
3588 media_expectations.ExpectBidirectionalAudioAndVideo();
3589 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003590}
3591
Steve Anton6f25b092017-10-23 09:39:20 -07003592// With a max bundle policy and RTCP muxing, adding a new media description to
3593// the connection should not affect ICE at all because the new media will use
3594// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003595TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003596 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07003597 PeerConnectionInterface::RTCConfiguration config;
3598 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3599 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3600 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3601 config, PeerConnectionInterface::RTCConfiguration()));
3602 ConnectFakeSignaling();
3603
Steve Anton15324772018-01-16 10:26:49 -08003604 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003605 caller()->CreateAndSetAndSignalOffer();
3606 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07003607 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3608 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07003609
3610 caller()->clear_ice_connection_state_history();
3611
Steve Anton15324772018-01-16 10:26:49 -08003612 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003613 caller()->CreateAndSetAndSignalOffer();
3614 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3615
3616 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
3617}
3618
deadbeef1dcb1642017-03-29 21:08:16 -07003619// This test sets up a call between two parties with audio and video. It then
3620// renegotiates setting the video m-line to "port 0", then later renegotiates
3621// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003622TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003623 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
3624 ASSERT_TRUE(CreatePeerConnectionWrappers());
3625 ConnectFakeSignaling();
3626
3627 // Do initial negotiation, only sending media from the caller. Will result in
3628 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08003629 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003630 caller()->CreateAndSetAndSignalOffer();
3631 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3632
3633 // Negotiate again, disabling the video "m=" section (the callee will set the
3634 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003635 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3636 PeerConnectionInterface::RTCOfferAnswerOptions options;
3637 options.offer_to_receive_video = 0;
3638 callee()->SetOfferAnswerOptions(options);
3639 } else {
3640 callee()->SetRemoteOfferHandler([this] {
3641 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
3642 });
3643 }
deadbeef1dcb1642017-03-29 21:08:16 -07003644 caller()->CreateAndSetAndSignalOffer();
3645 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3646 // Sanity check that video "m=" section was actually rejected.
3647 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
3648 callee()->pc()->local_description()->description());
3649 ASSERT_NE(nullptr, answer_video_content);
3650 ASSERT_TRUE(answer_video_content->rejected);
3651
3652 // Enable video and do negotiation again, making sure video is received
3653 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003654 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3655 PeerConnectionInterface::RTCOfferAnswerOptions options;
3656 options.offer_to_receive_video = 1;
3657 callee()->SetOfferAnswerOptions(options);
3658 } else {
3659 // The caller's transceiver is stopped, so we need to add another track.
3660 auto caller_transceiver =
3661 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
3662 EXPECT_TRUE(caller_transceiver->stopped());
3663 caller()->AddVideoTrack();
3664 }
3665 callee()->AddVideoTrack();
3666 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07003667 caller()->CreateAndSetAndSignalOffer();
3668 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003669
deadbeef1dcb1642017-03-29 21:08:16 -07003670 // Verify the caller receives frames from the newly added stream, and the
3671 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003672 MediaExpectations media_expectations;
3673 media_expectations.CalleeExpectsSomeAudio();
3674 media_expectations.ExpectBidirectionalVideo();
3675 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003676}
3677
3678// This test sets up a Jsep call between two parties with external
3679// VideoDecoderFactory.
3680// TODO(holmer): Disabled due to sometimes crashing on buildbots.
3681// See issue webrtc/2378.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003682TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003683 DISABLED_EndToEndCallWithVideoDecoderFactory) {
3684 ASSERT_TRUE(CreatePeerConnectionWrappers());
3685 EnableVideoDecoderFactory();
3686 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003687 caller()->AddAudioVideoTracks();
3688 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003689 caller()->CreateAndSetAndSignalOffer();
3690 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003691 MediaExpectations media_expectations;
3692 media_expectations.ExpectBidirectionalAudioAndVideo();
3693 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003694}
3695
3696// This tests that if we negotiate after calling CreateSender but before we
3697// have a track, then set a track later, frames from the newly-set track are
3698// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003699TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07003700 MediaFlowsAfterEarlyWarmupWithCreateSender) {
3701 ASSERT_TRUE(CreatePeerConnectionWrappers());
3702 ConnectFakeSignaling();
3703 auto caller_audio_sender =
3704 caller()->pc()->CreateSender("audio", "caller_stream");
3705 auto caller_video_sender =
3706 caller()->pc()->CreateSender("video", "caller_stream");
3707 auto callee_audio_sender =
3708 callee()->pc()->CreateSender("audio", "callee_stream");
3709 auto callee_video_sender =
3710 callee()->pc()->CreateSender("video", "callee_stream");
3711 caller()->CreateAndSetAndSignalOffer();
3712 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3713 // Wait for ICE to complete, without any tracks being set.
3714 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3715 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3716 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3717 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3718 // Now set the tracks, and expect frames to immediately start flowing.
3719 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3720 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3721 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3722 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08003723 MediaExpectations media_expectations;
3724 media_expectations.ExpectBidirectionalAudioAndVideo();
3725 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3726}
3727
3728// This tests that if we negotiate after calling AddTransceiver but before we
3729// have a track, then set a track later, frames from the newly-set tracks are
3730// received end-to-end.
3731TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3732 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
3733 ASSERT_TRUE(CreatePeerConnectionWrappers());
3734 ConnectFakeSignaling();
3735 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3736 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
3737 auto caller_audio_sender = audio_result.MoveValue()->sender();
3738 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3739 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
3740 auto caller_video_sender = video_result.MoveValue()->sender();
3741 callee()->SetRemoteOfferHandler([this] {
3742 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
3743 callee()->pc()->GetTransceivers()[0]->SetDirection(
3744 RtpTransceiverDirection::kSendRecv);
3745 callee()->pc()->GetTransceivers()[1]->SetDirection(
3746 RtpTransceiverDirection::kSendRecv);
3747 });
3748 caller()->CreateAndSetAndSignalOffer();
3749 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3750 // Wait for ICE to complete, without any tracks being set.
3751 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3752 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3753 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3754 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3755 // Now set the tracks, and expect frames to immediately start flowing.
3756 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
3757 auto callee_video_sender = callee()->pc()->GetSenders()[1];
3758 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3759 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3760 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3761 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
3762 MediaExpectations media_expectations;
3763 media_expectations.ExpectBidirectionalAudioAndVideo();
3764 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003765}
3766
3767// This test verifies that a remote video track can be added via AddStream,
3768// and sent end-to-end. For this particular test, it's simply echoed back
3769// from the caller to the callee, rather than being forwarded to a third
3770// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003771TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07003772 ASSERT_TRUE(CreatePeerConnectionWrappers());
3773 ConnectFakeSignaling();
3774 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08003775 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003776 caller()->CreateAndSetAndSignalOffer();
3777 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3778 ASSERT_EQ(1, callee()->remote_streams()->count());
3779
3780 // Echo the stream back, and do a new offer/anwer (initiated by callee this
3781 // time).
3782 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
3783 callee()->CreateAndSetAndSignalOffer();
3784 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3785
Seth Hampson2f0d7022018-02-20 11:54:42 -08003786 MediaExpectations media_expectations;
3787 media_expectations.ExpectBidirectionalVideo();
3788 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003789}
3790
3791// Test that we achieve the expected end-to-end connection time, using a
3792// fake clock and simulated latency on the media and signaling paths.
3793// We use a TURN<->TURN connection because this is usually the quickest to
3794// set up initially, especially when we're confident the connection will work
3795// and can start sending media before we get a STUN response.
3796//
3797// With various optimizations enabled, here are the network delays we expect to
3798// be on the critical path:
3799// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
3800// signaling answer (with DTLS fingerprint).
3801// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
3802// using TURN<->TURN pair, and DTLS exchange is 4 packets,
3803// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003804TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07003805 rtc::ScopedFakeClock fake_clock;
3806 // Some things use a time of "0" as a special value, so we need to start out
3807 // the fake clock at a nonzero time.
3808 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003809 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003810
3811 static constexpr int media_hop_delay_ms = 50;
3812 static constexpr int signaling_trip_delay_ms = 500;
3813 // For explanation of these values, see comment above.
3814 static constexpr int required_media_hops = 9;
3815 static constexpr int required_signaling_trips = 2;
3816 // For internal delays (such as posting an event asychronously).
3817 static constexpr int allowed_internal_delay_ms = 20;
3818 static constexpr int total_connection_time_ms =
3819 media_hop_delay_ms * required_media_hops +
3820 signaling_trip_delay_ms * required_signaling_trips +
3821 allowed_internal_delay_ms;
3822
3823 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3824 3478};
3825 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3826 0};
3827 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3828 3478};
3829 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3830 0};
3831 cricket::TestTurnServer turn_server_1(network_thread(),
3832 turn_server_1_internal_address,
3833 turn_server_1_external_address);
3834 cricket::TestTurnServer turn_server_2(network_thread(),
3835 turn_server_2_internal_address,
3836 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003837
deadbeef1dcb1642017-03-29 21:08:16 -07003838 // Bypass permission check on received packets so media can be sent before
3839 // the candidate is signaled.
3840 turn_server_1.set_enable_permission_checks(false);
3841 turn_server_2.set_enable_permission_checks(false);
3842
3843 PeerConnectionInterface::RTCConfiguration client_1_config;
3844 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3845 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3846 ice_server_1.username = "test";
3847 ice_server_1.password = "test";
3848 client_1_config.servers.push_back(ice_server_1);
3849 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3850 client_1_config.presume_writable_when_fully_relayed = true;
3851
3852 PeerConnectionInterface::RTCConfiguration client_2_config;
3853 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3854 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3855 ice_server_2.username = "test";
3856 ice_server_2.password = "test";
3857 client_2_config.servers.push_back(ice_server_2);
3858 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3859 client_2_config.presume_writable_when_fully_relayed = true;
3860
3861 ASSERT_TRUE(
3862 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3863 // Set up the simulated delays.
3864 SetSignalingDelayMs(signaling_trip_delay_ms);
3865 ConnectFakeSignaling();
3866 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
3867 virtual_socket_server()->UpdateDelayDistribution();
3868
3869 // Set "offer to receive audio/video" without adding any tracks, so we just
3870 // set up ICE/DTLS with no media.
3871 PeerConnectionInterface::RTCOfferAnswerOptions options;
3872 options.offer_to_receive_audio = 1;
3873 options.offer_to_receive_video = 1;
3874 caller()->SetOfferAnswerOptions(options);
3875 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07003876 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
3877 fake_clock);
deadbeef1dcb1642017-03-29 21:08:16 -07003878 // Need to free the clients here since they're using things we created on
3879 // the stack.
3880 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3881 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3882}
3883
Jonas Orelandbdcee282017-10-10 14:01:40 +02003884// Verify that a TurnCustomizer passed in through RTCConfiguration
3885// is actually used by the underlying TURN candidate pair.
3886// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003887TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02003888 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3889 3478};
3890 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3891 0};
3892 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3893 3478};
3894 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3895 0};
3896 cricket::TestTurnServer turn_server_1(network_thread(),
3897 turn_server_1_internal_address,
3898 turn_server_1_external_address);
3899 cricket::TestTurnServer turn_server_2(network_thread(),
3900 turn_server_2_internal_address,
3901 turn_server_2_external_address);
3902
3903 PeerConnectionInterface::RTCConfiguration client_1_config;
3904 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3905 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3906 ice_server_1.username = "test";
3907 ice_server_1.password = "test";
3908 client_1_config.servers.push_back(ice_server_1);
3909 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3910 auto customizer1 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3911 client_1_config.turn_customizer = customizer1.get();
3912
3913 PeerConnectionInterface::RTCConfiguration client_2_config;
3914 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3915 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3916 ice_server_2.username = "test";
3917 ice_server_2.password = "test";
3918 client_2_config.servers.push_back(ice_server_2);
3919 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3920 auto customizer2 = rtc::MakeUnique<cricket::TestTurnCustomizer>();
3921 client_2_config.turn_customizer = customizer2.get();
3922
3923 ASSERT_TRUE(
3924 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3925 ConnectFakeSignaling();
3926
3927 // Set "offer to receive audio/video" without adding any tracks, so we just
3928 // set up ICE/DTLS with no media.
3929 PeerConnectionInterface::RTCOfferAnswerOptions options;
3930 options.offer_to_receive_audio = 1;
3931 options.offer_to_receive_video = 1;
3932 caller()->SetOfferAnswerOptions(options);
3933 caller()->CreateAndSetAndSignalOffer();
3934 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3935
3936 EXPECT_GT(customizer1->allow_channel_data_cnt_, 0u);
3937 EXPECT_GT(customizer1->modify_cnt_, 0u);
3938
3939 EXPECT_GT(customizer2->allow_channel_data_cnt_, 0u);
3940 EXPECT_GT(customizer2->modify_cnt_, 0u);
3941
3942 // Need to free the clients here since they're using things we created on
3943 // the stack.
3944 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
3945 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
3946}
3947
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07003948// Verify that a SSLCertificateVerifier passed in through
3949// PeerConnectionDependencies is actually used by the underlying SSL
3950// implementation to determine whether a certificate presented by the TURN
3951// server is accepted by the client. Note that openssladapter_unittest.cc
3952// contains more detailed, lower-level tests.
3953TEST_P(PeerConnectionIntegrationTest,
3954 SSLCertificateVerifierUsedForTurnConnections) {
3955 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
3956 3478};
3957 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
3958
3959 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
3960 // that host name verification passes on the fake certificate.
3961 cricket::TestTurnServer turn_server(
3962 network_thread(), turn_server_internal_address,
3963 turn_server_external_address, cricket::PROTO_TLS,
3964 /*ignore_bad_certs=*/true, "88.88.88.0");
3965
3966 webrtc::PeerConnectionInterface::IceServer ice_server;
3967 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
3968 ice_server.username = "test";
3969 ice_server.password = "test";
3970
3971 PeerConnectionInterface::RTCConfiguration client_1_config;
3972 client_1_config.servers.push_back(ice_server);
3973 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3974
3975 PeerConnectionInterface::RTCConfiguration client_2_config;
3976 client_2_config.servers.push_back(ice_server);
3977 // Setting the type to kRelay forces the connection to go through a TURN
3978 // server.
3979 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3980
3981 // Get a copy to the pointer so we can verify calls later.
3982 rtc::TestCertificateVerifier* client_1_cert_verifier =
3983 new rtc::TestCertificateVerifier();
3984 client_1_cert_verifier->verify_certificate_ = true;
3985 rtc::TestCertificateVerifier* client_2_cert_verifier =
3986 new rtc::TestCertificateVerifier();
3987 client_2_cert_verifier->verify_certificate_ = true;
3988
3989 // Create the dependencies with the test certificate verifier.
3990 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
3991 client_1_deps.tls_cert_verifier =
3992 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
3993 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
3994 client_2_deps.tls_cert_verifier =
3995 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
3996
3997 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3998 client_1_config, std::move(client_1_deps), client_2_config,
3999 std::move(client_2_deps)));
4000 ConnectFakeSignaling();
4001
4002 // Set "offer to receive audio/video" without adding any tracks, so we just
4003 // set up ICE/DTLS with no media.
4004 PeerConnectionInterface::RTCOfferAnswerOptions options;
4005 options.offer_to_receive_audio = 1;
4006 options.offer_to_receive_video = 1;
4007 caller()->SetOfferAnswerOptions(options);
4008 caller()->CreateAndSetAndSignalOffer();
4009 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4010
4011 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4012 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
4013
4014 // Need to free the clients here since they're using things we created on
4015 // the stack.
4016 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4017 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
4018}
4019
4020TEST_P(PeerConnectionIntegrationTest,
4021 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4022 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4023 3478};
4024 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4025
4026 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4027 // that host name verification passes on the fake certificate.
4028 cricket::TestTurnServer turn_server(
4029 network_thread(), turn_server_internal_address,
4030 turn_server_external_address, cricket::PROTO_TLS,
4031 /*ignore_bad_certs=*/true, "88.88.88.0");
4032
4033 webrtc::PeerConnectionInterface::IceServer ice_server;
4034 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4035 ice_server.username = "test";
4036 ice_server.password = "test";
4037
4038 PeerConnectionInterface::RTCConfiguration client_1_config;
4039 client_1_config.servers.push_back(ice_server);
4040 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4041
4042 PeerConnectionInterface::RTCConfiguration client_2_config;
4043 client_2_config.servers.push_back(ice_server);
4044 // Setting the type to kRelay forces the connection to go through a TURN
4045 // server.
4046 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4047
4048 // Get a copy to the pointer so we can verify calls later.
4049 rtc::TestCertificateVerifier* client_1_cert_verifier =
4050 new rtc::TestCertificateVerifier();
4051 client_1_cert_verifier->verify_certificate_ = false;
4052 rtc::TestCertificateVerifier* client_2_cert_verifier =
4053 new rtc::TestCertificateVerifier();
4054 client_2_cert_verifier->verify_certificate_ = false;
4055
4056 // Create the dependencies with the test certificate verifier.
4057 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4058 client_1_deps.tls_cert_verifier =
4059 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4060 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4061 client_2_deps.tls_cert_verifier =
4062 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4063
4064 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4065 client_1_config, std::move(client_1_deps), client_2_config,
4066 std::move(client_2_deps)));
4067 ConnectFakeSignaling();
4068
4069 // Set "offer to receive audio/video" without adding any tracks, so we just
4070 // set up ICE/DTLS with no media.
4071 PeerConnectionInterface::RTCOfferAnswerOptions options;
4072 options.offer_to_receive_audio = 1;
4073 options.offer_to_receive_video = 1;
4074 caller()->SetOfferAnswerOptions(options);
4075 caller()->CreateAndSetAndSignalOffer();
4076 bool wait_res = true;
4077 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4078 // properly, should be able to just wait for a state of "failed" instead of
4079 // waiting a fixed 10 seconds.
4080 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4081 ASSERT_FALSE(wait_res);
4082
4083 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4084 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
4085
4086 // Need to free the clients here since they're using things we created on
4087 // the stack.
4088 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
4089 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
4090}
4091
deadbeefc964d0b2017-04-03 10:03:35 -07004092// Test that audio and video flow end-to-end when codec names don't use the
4093// expected casing, given that they're supposed to be case insensitive. To test
4094// this, all but one codec is removed from each media description, and its
4095// casing is changed.
4096//
4097// In the past, this has regressed and caused crashes/black video, due to the
4098// fact that code at some layers was doing case-insensitive comparisons and
4099// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004100TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004101 ASSERT_TRUE(CreatePeerConnectionWrappers());
4102 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004103 caller()->AddAudioVideoTracks();
4104 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004105
4106 // Remove all but one audio/video codec (opus and VP8), and change the
4107 // casing of the caller's generated offer.
4108 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4109 cricket::AudioContentDescription* audio =
4110 GetFirstAudioContentDescription(description);
4111 ASSERT_NE(nullptr, audio);
4112 auto audio_codecs = audio->codecs();
4113 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4114 [](const cricket::AudioCodec& codec) {
4115 return codec.name != "opus";
4116 }),
4117 audio_codecs.end());
4118 ASSERT_EQ(1u, audio_codecs.size());
4119 audio_codecs[0].name = "OpUs";
4120 audio->set_codecs(audio_codecs);
4121
4122 cricket::VideoContentDescription* video =
4123 GetFirstVideoContentDescription(description);
4124 ASSERT_NE(nullptr, video);
4125 auto video_codecs = video->codecs();
4126 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4127 [](const cricket::VideoCodec& codec) {
4128 return codec.name != "VP8";
4129 }),
4130 video_codecs.end());
4131 ASSERT_EQ(1u, video_codecs.size());
4132 video_codecs[0].name = "vP8";
4133 video->set_codecs(video_codecs);
4134 });
4135
4136 caller()->CreateAndSetAndSignalOffer();
4137 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4138
4139 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004140 MediaExpectations media_expectations;
4141 media_expectations.ExpectBidirectionalAudioAndVideo();
4142 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004143}
4144
Seth Hampson2f0d7022018-02-20 11:54:42 -08004145TEST_P(PeerConnectionIntegrationTest, GetSources) {
hbos8d609f62017-04-10 07:39:05 -07004146 ASSERT_TRUE(CreatePeerConnectionWrappers());
4147 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004148 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004149 caller()->CreateAndSetAndSignalOffer();
4150 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004151 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004152 MediaExpectations media_expectations;
4153 media_expectations.CalleeExpectsSomeAudio(1);
4154 ASSERT_TRUE(ExpectNewFrames(media_expectations));
hbos8d609f62017-04-10 07:39:05 -07004155 ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u);
4156 auto receiver = callee()->pc()->GetReceivers()[0];
4157 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
4158
4159 auto contributing_sources = receiver->GetSources();
4160 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4161 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4162 contributing_sources[0].source_id());
4163}
4164
deadbeef2f425aa2017-04-14 10:41:32 -07004165// Test that if a track is removed and added again with a different stream ID,
4166// the new stream ID is successfully communicated in SDP and media continues to
4167// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004168// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4169// it will not reuse a transceiver that has already been sending. After creating
4170// a new transceiver it tries to create an offer with two senders of the same
4171// track ids and it fails.
4172TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004173 ASSERT_TRUE(CreatePeerConnectionWrappers());
4174 ConnectFakeSignaling();
4175
4176 rtc::scoped_refptr<MediaStreamInterface> stream_1 =
4177 caller()->pc_factory()->CreateLocalMediaStream("stream_1");
4178 rtc::scoped_refptr<MediaStreamInterface> stream_2 =
4179 caller()->pc_factory()->CreateLocalMediaStream("stream_2");
4180
4181 // Add track using stream 1, do offer/answer.
4182 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4183 caller()->CreateLocalAudioTrack();
4184 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
4185 caller()->pc()->AddTrack(track, {stream_1.get()});
4186 caller()->CreateAndSetAndSignalOffer();
4187 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004188 {
4189 MediaExpectations media_expectations;
4190 media_expectations.CalleeExpectsSomeAudio(1);
4191 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4192 }
deadbeef2f425aa2017-04-14 10:41:32 -07004193 // Remove the sender, and create a new one with the new stream.
4194 caller()->pc()->RemoveTrack(sender);
4195 sender = caller()->pc()->AddTrack(track, {stream_2.get()});
4196 caller()->CreateAndSetAndSignalOffer();
4197 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4198 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004199 {
4200 MediaExpectations media_expectations;
4201 media_expectations.CalleeExpectsSomeAudio();
4202 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4203 }
deadbeef2f425aa2017-04-14 10:41:32 -07004204}
4205
Seth Hampson2f0d7022018-02-20 11:54:42 -08004206TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004207 ASSERT_TRUE(CreatePeerConnectionWrappers());
4208 ConnectFakeSignaling();
4209
4210 auto output = rtc::MakeUnique<testing::NiceMock<MockRtcEventLogOutput>>();
4211 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
4212 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
4213 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004214 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4215 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004216
Steve Anton15324772018-01-16 10:26:49 -08004217 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004218 caller()->CreateAndSetAndSignalOffer();
4219 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4220}
4221
Steve Antonede9ca52017-10-16 13:04:27 -07004222// Test that if candidates are only signaled by applying full session
4223// descriptions (instead of using AddIceCandidate), the peers can connect to
4224// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004225TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004226 ASSERT_TRUE(CreatePeerConnectionWrappers());
4227 // Each side will signal the session descriptions but not candidates.
4228 ConnectFakeSignalingForSdpOnly();
4229
4230 // Add audio video track and exchange the initial offer/answer with media
4231 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004232 caller()->AddAudioVideoTracks();
4233 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004234 caller()->CreateAndSetAndSignalOffer();
4235
4236 // Wait for all candidates to be gathered on both the caller and callee.
4237 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4238 caller()->ice_gathering_state(), kDefaultTimeout);
4239 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4240 callee()->ice_gathering_state(), kDefaultTimeout);
4241
4242 // The candidates will now be included in the session description, so
4243 // signaling them will start the ICE connection.
4244 caller()->CreateAndSetAndSignalOffer();
4245 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4246
4247 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004248 MediaExpectations media_expectations;
4249 media_expectations.ExpectBidirectionalAudioAndVideo();
4250 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004251}
4252
henrika5f6bf242017-11-01 11:06:56 +01004253// Test that SetAudioPlayout can be used to disable audio playout from the
4254// start, then later enable it. This may be useful, for example, if the caller
4255// needs to play a local ringtone until some event occurs, after which it
4256// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004257TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004258 ASSERT_TRUE(CreatePeerConnectionWrappers());
4259 ConnectFakeSignaling();
4260
4261 // Set up audio-only call where audio playout is disabled on caller's side.
4262 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004263 caller()->AddAudioTrack();
4264 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004265 caller()->CreateAndSetAndSignalOffer();
4266 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4267
4268 // Pump messages for a second.
4269 WAIT(false, 1000);
4270 // Since audio playout is disabled, the caller shouldn't have received
4271 // anything (at the playout level, at least).
4272 EXPECT_EQ(0, caller()->audio_frames_received());
4273 // As a sanity check, make sure the callee (for which playout isn't disabled)
4274 // did still see frames on its audio level.
4275 ASSERT_GT(callee()->audio_frames_received(), 0);
4276
4277 // Enable playout again, and ensure audio starts flowing.
4278 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004279 MediaExpectations media_expectations;
4280 media_expectations.ExpectBidirectionalAudio();
4281 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004282}
4283
4284double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4285 auto report = pc->NewGetStats();
4286 auto track_stats_list =
4287 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4288 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4289 for (const auto* track_stats : track_stats_list) {
4290 if (track_stats->remote_source.is_defined() &&
4291 *track_stats->remote_source) {
4292 remote_track_stats = track_stats;
4293 break;
4294 }
4295 }
4296
4297 if (!remote_track_stats->total_audio_energy.is_defined()) {
4298 return 0.0;
4299 }
4300 return *remote_track_stats->total_audio_energy;
4301}
4302
4303// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4304// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004305TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004306 DisableAudioPlayoutStillGeneratesAudioStats) {
4307 ASSERT_TRUE(CreatePeerConnectionWrappers());
4308 ConnectFakeSignaling();
4309
4310 // Set up audio-only call where playout is disabled but audio-processing is
4311 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004312 caller()->AddAudioTrack();
4313 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004314 caller()->pc()->SetAudioPlayout(false);
4315
4316 caller()->CreateAndSetAndSignalOffer();
4317 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4318
4319 // Wait for the callee to receive audio stats.
4320 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4321}
4322
henrika4f167df2017-11-01 14:45:55 +01004323// Test that SetAudioRecording can be used to disable audio recording from the
4324// start, then later enable it. This may be useful, for example, if the caller
4325// wants to ensure that no audio resources are active before a certain state
4326// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004327TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004328 ASSERT_TRUE(CreatePeerConnectionWrappers());
4329 ConnectFakeSignaling();
4330
4331 // Set up audio-only call where audio recording is disabled on caller's side.
4332 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004333 caller()->AddAudioTrack();
4334 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004335 caller()->CreateAndSetAndSignalOffer();
4336 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4337
4338 // Pump messages for a second.
4339 WAIT(false, 1000);
4340 // Since caller has disabled audio recording, the callee shouldn't have
4341 // received anything.
4342 EXPECT_EQ(0, callee()->audio_frames_received());
4343 // As a sanity check, make sure the caller did still see frames on its
4344 // audio level since audio recording is enabled on the calle side.
4345 ASSERT_GT(caller()->audio_frames_received(), 0);
4346
4347 // Enable audio recording again, and ensure audio starts flowing.
4348 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004349 MediaExpectations media_expectations;
4350 media_expectations.ExpectBidirectionalAudio();
4351 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004352}
4353
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004354// Test that after closing PeerConnections, they stop sending any packets (ICE,
4355// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004356TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004357 // Set up audio/video/data, wait for some frames to be received.
4358 ASSERT_TRUE(CreatePeerConnectionWrappers());
4359 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004360 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004361#ifdef HAVE_SCTP
4362 caller()->CreateDataChannel();
4363#endif
4364 caller()->CreateAndSetAndSignalOffer();
4365 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004366 MediaExpectations media_expectations;
4367 media_expectations.CalleeExpectsSomeAudioAndVideo();
4368 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004369 // Close PeerConnections.
4370 caller()->pc()->Close();
4371 callee()->pc()->Close();
4372 // Pump messages for a second, and ensure no new packets end up sent.
4373 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4374 WAIT(false, 1000);
4375 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4376 EXPECT_EQ(sent_packets_a, sent_packets_b);
4377}
4378
Steve Anton7eca0932018-03-30 15:18:41 -07004379// Test that transport stats are generated by the RTCStatsCollector for a
4380// connection that only involves data channels. This is a regression test for
4381// crbug.com/826972.
4382#ifdef HAVE_SCTP
4383TEST_P(PeerConnectionIntegrationTest,
4384 TransportStatsReportedForDataChannelOnlyConnection) {
4385 ASSERT_TRUE(CreatePeerConnectionWrappers());
4386 ConnectFakeSignaling();
4387 caller()->CreateDataChannel();
4388
4389 caller()->CreateAndSetAndSignalOffer();
4390 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4391 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
4392
4393 auto caller_report = caller()->NewGetStats();
4394 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
4395 auto callee_report = callee()->NewGetStats();
4396 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
4397}
4398#endif // HAVE_SCTP
4399
Seth Hampson2f0d7022018-02-20 11:54:42 -08004400INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4401 PeerConnectionIntegrationTest,
4402 Values(SdpSemantics::kPlanB,
4403 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004404
Steve Anton74255ff2018-01-24 18:32:57 -08004405// Tests that verify interoperability between Plan B and Unified Plan
4406// PeerConnections.
4407class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004408 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004409 public ::testing::WithParamInterface<
4410 std::tuple<SdpSemantics, SdpSemantics>> {
4411 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004412 // Setting the SdpSemantics for the base test to kDefault does not matter
4413 // because we specify not to use the test semantics when creating
4414 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004415 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07004416 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08004417 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004418 callee_semantics_(std::get<1>(GetParam())) {}
4419
4420 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07004421 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
4422 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08004423 }
4424
4425 const SdpSemantics caller_semantics_;
4426 const SdpSemantics callee_semantics_;
4427};
4428
4429TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4430 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4431 ConnectFakeSignaling();
4432
4433 caller()->CreateAndSetAndSignalOffer();
4434 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4435}
4436
4437TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4438 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4439 ConnectFakeSignaling();
4440 auto audio_sender = caller()->AddAudioTrack();
4441
4442 caller()->CreateAndSetAndSignalOffer();
4443 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4444
4445 // Verify that one audio receiver has been created on the remote and that it
4446 // has the same track ID as the sending track.
4447 auto receivers = callee()->pc()->GetReceivers();
4448 ASSERT_EQ(1u, receivers.size());
4449 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4450 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4451
Seth Hampson2f0d7022018-02-20 11:54:42 -08004452 MediaExpectations media_expectations;
4453 media_expectations.CalleeExpectsSomeAudio();
4454 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004455}
4456
4457TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4458 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4459 ConnectFakeSignaling();
4460 auto video_sender = caller()->AddVideoTrack();
4461 auto audio_sender = caller()->AddAudioTrack();
4462
4463 caller()->CreateAndSetAndSignalOffer();
4464 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4465
4466 // Verify that one audio and one video receiver have been created on the
4467 // remote and that they have the same track IDs as the sending tracks.
4468 auto audio_receivers =
4469 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4470 ASSERT_EQ(1u, audio_receivers.size());
4471 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4472 auto video_receivers =
4473 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4474 ASSERT_EQ(1u, video_receivers.size());
4475 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4476
Seth Hampson2f0d7022018-02-20 11:54:42 -08004477 MediaExpectations media_expectations;
4478 media_expectations.CalleeExpectsSomeAudioAndVideo();
4479 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004480}
4481
4482TEST_P(PeerConnectionIntegrationInteropTest,
4483 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4484 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4485 ConnectFakeSignaling();
4486 caller()->AddAudioVideoTracks();
4487 callee()->AddAudioVideoTracks();
4488
4489 caller()->CreateAndSetAndSignalOffer();
4490 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4491
Seth Hampson2f0d7022018-02-20 11:54:42 -08004492 MediaExpectations media_expectations;
4493 media_expectations.ExpectBidirectionalAudioAndVideo();
4494 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004495}
4496
4497TEST_P(PeerConnectionIntegrationInteropTest,
4498 ReverseRolesOneAudioLocalToOneVideoRemote) {
4499 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4500 ConnectFakeSignaling();
4501 caller()->AddAudioTrack();
4502 callee()->AddVideoTrack();
4503
4504 caller()->CreateAndSetAndSignalOffer();
4505 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4506
4507 // Verify that only the audio track has been negotiated.
4508 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
4509 // Might also check that the callee's NegotiationNeeded flag is set.
4510
4511 // Reverse roles.
4512 callee()->CreateAndSetAndSignalOffer();
4513 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4514
Seth Hampson2f0d7022018-02-20 11:54:42 -08004515 MediaExpectations media_expectations;
4516 media_expectations.CallerExpectsSomeVideo();
4517 media_expectations.CalleeExpectsSomeAudio();
4518 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004519}
4520
Steve Antonba42e992018-04-09 14:10:01 -07004521INSTANTIATE_TEST_CASE_P(
4522 PeerConnectionIntegrationTest,
4523 PeerConnectionIntegrationInteropTest,
4524 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4525 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
4526
4527// Test that if the Unified Plan side offers two video tracks then the Plan B
4528// side will only see the first one and ignore the second.
4529TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07004530 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
4531 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08004532 ConnectFakeSignaling();
4533 auto first_sender = caller()->AddVideoTrack();
4534 caller()->AddVideoTrack();
4535
4536 caller()->CreateAndSetAndSignalOffer();
4537 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4538
4539 // Verify that there is only one receiver and it corresponds to the first
4540 // added track.
4541 auto receivers = callee()->pc()->GetReceivers();
4542 ASSERT_EQ(1u, receivers.size());
4543 EXPECT_TRUE(receivers[0]->track()->enabled());
4544 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
4545
Seth Hampson2f0d7022018-02-20 11:54:42 -08004546 MediaExpectations media_expectations;
4547 media_expectations.CalleeExpectsSomeVideo();
4548 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004549}
4550
deadbeef1dcb1642017-03-29 21:08:16 -07004551} // namespace
4552
4553#endif // if !defined(THREAD_SANITIZER)