blob: 1e61604019949fcbc51c6250915cb04c529114a9 [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"
Qingsi Wang7685e862018-06-11 20:15:46 -070036#include "call/call.h"
37#include "logging/rtc_event_log/fake_rtc_event_log_factory.h"
38#include "logging/rtc_event_log/rtc_event_log_factory_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020039#include "media/engine/fakewebrtcvideoengine.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070040#include "media/engine/webrtcmediaengine.h"
41#include "modules/audio_processing/include/audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "p2p/base/p2pconstants.h"
43#include "p2p/base/portinterface.h"
Steve Antonede9ca52017-10-16 13:04:27 -070044#include "p2p/base/teststunserver.h"
Jonas Orelandbdcee282017-10-10 14:01:40 +020045#include "p2p/base/testturncustomizer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "p2p/base/testturnserver.h"
47#include "p2p/client/basicportallocator.h"
48#include "pc/dtmfsender.h"
49#include "pc/localaudiosource.h"
50#include "pc/mediasession.h"
51#include "pc/peerconnection.h"
52#include "pc/peerconnectionfactory.h"
Seth Hampson2f0d7022018-02-20 11:54:42 -080053#include "pc/rtpmediautils.h"
Steve Anton4ab68ee2017-12-19 14:26:11 -080054#include "pc/sessiondescription.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020055#include "pc/test/fakeaudiocapturemodule.h"
Niels Möller0f405822018-05-17 09:16:41 +020056#include "pc/test/fakeperiodicvideotracksource.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020057#include "pc/test/fakertccertificategenerator.h"
58#include "pc/test/fakevideotrackrenderer.h"
59#include "pc/test/mockpeerconnectionobservers.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020060#include "rtc_base/fakenetwork.h"
Steve Antonede9ca52017-10-16 13:04:27 -070061#include "rtc_base/firewallsocketserver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020062#include "rtc_base/gunit.h"
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070063#include "rtc_base/testcertificateverifier.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020064#include "rtc_base/virtualsocketserver.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020065#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070066
67using cricket::ContentInfo;
68using cricket::FakeWebRtcVideoDecoder;
69using cricket::FakeWebRtcVideoDecoderFactory;
70using cricket::FakeWebRtcVideoEncoder;
71using cricket::FakeWebRtcVideoEncoderFactory;
72using cricket::MediaContentDescription;
Steve Antondf527fd2018-04-27 15:52:03 -070073using cricket::StreamParams;
Steve Antonede9ca52017-10-16 13:04:27 -070074using rtc::SocketAddress;
Seth Hampson2f0d7022018-02-20 11:54:42 -080075using ::testing::Combine;
Steve Antonede9ca52017-10-16 13:04:27 -070076using ::testing::ElementsAre;
77using ::testing::Values;
deadbeef1dcb1642017-03-29 21:08:16 -070078using webrtc::DataBuffer;
79using webrtc::DataChannelInterface;
80using webrtc::DtmfSender;
81using webrtc::DtmfSenderInterface;
82using webrtc::DtmfSenderObserverInterface;
83using webrtc::FakeConstraints;
Steve Anton15324772018-01-16 10:26:49 -080084using webrtc::FakeVideoTrackRenderer;
deadbeef1dcb1642017-03-29 21:08:16 -070085using webrtc::MediaConstraintsInterface;
86using webrtc::MediaStreamInterface;
87using webrtc::MediaStreamTrackInterface;
88using webrtc::MockCreateSessionDescriptionObserver;
89using webrtc::MockDataChannelObserver;
90using webrtc::MockSetSessionDescriptionObserver;
91using webrtc::MockStatsObserver;
92using webrtc::ObserverInterface;
Steve Anton8c0f7a72017-10-03 10:03:10 -070093using webrtc::PeerConnection;
deadbeef1dcb1642017-03-29 21:08:16 -070094using webrtc::PeerConnectionInterface;
Steve Anton74255ff2018-01-24 18:32:57 -080095using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070096using webrtc::PeerConnectionFactory;
Steve Anton8c0f7a72017-10-03 10:03:10 -070097using webrtc::PeerConnectionProxy;
Steve Anton15324772018-01-16 10:26:49 -080098using webrtc::RTCErrorType;
Steve Anton7eca0932018-03-30 15:18:41 -070099using webrtc::RTCTransportStats;
Steve Anton74255ff2018-01-24 18:32:57 -0800100using webrtc::RtpSenderInterface;
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100101using webrtc::RtpReceiverInterface;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800102using webrtc::RtpSenderInterface;
103using webrtc::RtpTransceiverDirection;
104using webrtc::RtpTransceiverInit;
105using webrtc::RtpTransceiverInterface;
Steve Antond3679212018-01-17 17:41:02 -0800106using webrtc::SdpSemantics;
Steve Antona3a92c22017-12-07 10:27:41 -0800107using webrtc::SdpType;
deadbeef1dcb1642017-03-29 21:08:16 -0700108using webrtc::SessionDescriptionInterface;
109using webrtc::StreamCollectionInterface;
Steve Anton15324772018-01-16 10:26:49 -0800110using webrtc::VideoTrackInterface;
deadbeef1dcb1642017-03-29 21:08:16 -0700111
112namespace {
113
114static const int kDefaultTimeout = 10000;
115static const int kMaxWaitForStatsMs = 3000;
116static const int kMaxWaitForActivationMs = 5000;
117static const int kMaxWaitForFramesMs = 10000;
118// Default number of audio/video frames to wait for before considering a test
119// successful.
120static const int kDefaultExpectedAudioFrameCount = 3;
121static const int kDefaultExpectedVideoFrameCount = 3;
122
deadbeef1dcb1642017-03-29 21:08:16 -0700123static const char kDataChannelLabel[] = "data_channel";
124
125// SRTP cipher name negotiated by the tests. This must be updated if the
126// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700127static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700128static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
129
Steve Antonede9ca52017-10-16 13:04:27 -0700130static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
131
deadbeef1dcb1642017-03-29 21:08:16 -0700132// Helper function for constructing offer/answer options to initiate an ICE
133// restart.
134PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
135 PeerConnectionInterface::RTCOfferAnswerOptions options;
136 options.ice_restart = true;
137 return options;
138}
139
deadbeefd8ad7882017-04-18 16:01:17 -0700140// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
141// attribute from received SDP, simulating a legacy endpoint.
142void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
143 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800144 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700145 }
146 desc->set_msid_supported(false);
147}
148
Seth Hampson5897a6e2018-04-03 11:16:33 -0700149// Removes all stream information besides the stream ids, simulating an
150// endpoint that only signals a=msid lines to convey stream_ids.
151void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
152 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700153 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700154 std::vector<std::string> stream_ids;
155 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700156 const StreamParams& first_stream =
157 content.media_description()->streams()[0];
158 track_id = first_stream.id;
159 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700160 }
161 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700162 StreamParams new_stream;
163 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700164 new_stream.set_stream_ids(stream_ids);
165 content.media_description()->AddStream(new_stream);
166 }
167}
168
zhihuangf8164932017-05-19 13:09:47 -0700169int FindFirstMediaStatsIndexByKind(
170 const std::string& kind,
171 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
172 media_stats_vec) {
173 for (size_t i = 0; i < media_stats_vec.size(); i++) {
174 if (media_stats_vec[i]->kind.ValueToString() == kind) {
175 return i;
176 }
177 }
178 return -1;
179}
180
Harald Alvestrand8ebba742018-05-31 14:00:34 +0200181int MakeUsageFingerprint(std::set<PeerConnection::UsageEvent> events) {
182 int signature = 0;
183 for (const auto it : events) {
184 signature |= static_cast<int>(it);
185 }
186 return signature;
187}
188
deadbeef1dcb1642017-03-29 21:08:16 -0700189class SignalingMessageReceiver {
190 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800191 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700192 virtual void ReceiveIceMessage(const std::string& sdp_mid,
193 int sdp_mline_index,
194 const std::string& msg) = 0;
195
196 protected:
197 SignalingMessageReceiver() {}
198 virtual ~SignalingMessageReceiver() {}
199};
200
201class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
202 public:
203 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
204 : expected_media_type_(media_type) {}
205
206 void OnFirstPacketReceived(cricket::MediaType media_type) override {
207 ASSERT_EQ(expected_media_type_, media_type);
208 first_packet_received_ = true;
209 }
210
211 bool first_packet_received() const { return first_packet_received_; }
212
213 virtual ~MockRtpReceiverObserver() {}
214
215 private:
216 bool first_packet_received_ = false;
217 cricket::MediaType expected_media_type_;
218};
219
220// Helper class that wraps a peer connection, observes it, and can accept
221// signaling messages from another wrapper.
222//
223// Uses a fake network, fake A/V capture, and optionally fake
224// encoders/decoders, though they aren't used by default since they don't
225// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700226// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800227// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700228class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800229 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700230 public:
231 // Different factory methods for convenience.
232 // TODO(deadbeef): Could use the pattern of:
233 //
234 // PeerConnectionWrapper =
235 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
236 //
237 // To reduce some code duplication.
238 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
239 const std::string& debug_name,
240 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
241 rtc::Thread* network_thread,
242 rtc::Thread* worker_thread) {
243 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700244 webrtc::PeerConnectionDependencies dependencies(nullptr);
245 dependencies.cert_generator = std::move(cert_generator);
246 if (!client->Init(nullptr, nullptr, nullptr, std::move(dependencies),
Qingsi Wang7685e862018-06-11 20:15:46 -0700247 network_thread, worker_thread, nullptr)) {
deadbeef1dcb1642017-03-29 21:08:16 -0700248 delete client;
249 return nullptr;
250 }
251 return client;
252 }
253
deadbeef2f425aa2017-04-14 10:41:32 -0700254 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
255 return peer_connection_factory_.get();
256 }
257
deadbeef1dcb1642017-03-29 21:08:16 -0700258 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
259
260 // If a signaling message receiver is set (via ConnectFakeSignaling), this
261 // will set the whole offer/answer exchange in motion. Just need to wait for
262 // the signaling state to reach "stable".
263 void CreateAndSetAndSignalOffer() {
264 auto offer = CreateOffer();
265 ASSERT_NE(nullptr, offer);
266 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
267 }
268
269 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
270 // when a remote offer is received (via fake signaling) and an answer is
271 // generated. By default, uses default options.
272 void SetOfferAnswerOptions(
273 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
274 offer_answer_options_ = options;
275 }
276
277 // Set a callback to be invoked when SDP is received via the fake signaling
278 // channel, which provides an opportunity to munge (modify) the SDP. This is
279 // used to test SDP being applied that a PeerConnection would normally not
280 // generate, but a non-JSEP endpoint might.
281 void SetReceivedSdpMunger(
282 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100283 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700284 }
285
deadbeefc964d0b2017-04-03 10:03:35 -0700286 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700287 // generated.
288 void SetGeneratedSdpMunger(
289 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100290 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700291 }
292
Seth Hampson2f0d7022018-02-20 11:54:42 -0800293 // Set a callback to be invoked when a remote offer is received via the fake
294 // signaling channel. This provides an opportunity to change the
295 // PeerConnection state before an answer is created and sent to the caller.
296 void SetRemoteOfferHandler(std::function<void()> handler) {
297 remote_offer_handler_ = std::move(handler);
298 }
299
Steve Antonede9ca52017-10-16 13:04:27 -0700300 // Every ICE connection state in order that has been seen by the observer.
301 std::vector<PeerConnectionInterface::IceConnectionState>
302 ice_connection_state_history() const {
303 return ice_connection_state_history_;
304 }
Steve Anton6f25b092017-10-23 09:39:20 -0700305 void clear_ice_connection_state_history() {
306 ice_connection_state_history_.clear();
307 }
Steve Antonede9ca52017-10-16 13:04:27 -0700308
309 // Every ICE gathering state in order that has been seen by the observer.
310 std::vector<PeerConnectionInterface::IceGatheringState>
311 ice_gathering_state_history() const {
312 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700313 }
314
Steve Anton15324772018-01-16 10:26:49 -0800315 void AddAudioVideoTracks() {
316 AddAudioTrack();
317 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700318 }
319
Steve Anton74255ff2018-01-24 18:32:57 -0800320 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
321 return AddTrack(CreateLocalAudioTrack());
322 }
deadbeef1dcb1642017-03-29 21:08:16 -0700323
Steve Anton74255ff2018-01-24 18:32:57 -0800324 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
325 return AddTrack(CreateLocalVideoTrack());
326 }
deadbeef1dcb1642017-03-29 21:08:16 -0700327
328 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
Niels Möller2d02e082018-05-21 11:23:35 +0200329 cricket::AudioOptions options;
deadbeef1dcb1642017-03-29 21:08:16 -0700330 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200331 options.highpass_filter = false;
deadbeef1dcb1642017-03-29 21:08:16 -0700332 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200333 peer_connection_factory_->CreateAudioSource(options);
deadbeef1dcb1642017-03-29 21:08:16 -0700334 // TODO(perkj): Test audio source when it is implemented. Currently audio
335 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700336 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700337 source);
338 }
339
340 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Niels Möller5c7efe72018-05-11 10:34:46 +0200341 return CreateLocalVideoTrackInternal(
342 webrtc::FakePeriodicVideoSource::Config());
deadbeef1dcb1642017-03-29 21:08:16 -0700343 }
344
345 rtc::scoped_refptr<webrtc::VideoTrackInterface>
Niels Möller5c7efe72018-05-11 10:34:46 +0200346 CreateLocalVideoTrackWithConfig(
347 webrtc::FakePeriodicVideoSource::Config config) {
348 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700349 }
350
351 rtc::scoped_refptr<webrtc::VideoTrackInterface>
352 CreateLocalVideoTrackWithRotation(webrtc::VideoRotation rotation) {
Niels Möller5c7efe72018-05-11 10:34:46 +0200353 webrtc::FakePeriodicVideoSource::Config config;
354 config.rotation = rotation;
355 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700356 }
357
Steve Anton74255ff2018-01-24 18:32:57 -0800358 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
359 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800360 const std::vector<std::string>& stream_ids = {}) {
361 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800362 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800363 return result.MoveValue();
364 }
365
366 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
367 cricket::MediaType media_type) {
368 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
369 for (auto receiver : pc()->GetReceivers()) {
370 if (receiver->media_type() == media_type) {
371 receivers.push_back(receiver);
372 }
373 }
374 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700375 }
376
Seth Hampson2f0d7022018-02-20 11:54:42 -0800377 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
378 cricket::MediaType media_type) {
379 for (auto transceiver : pc()->GetTransceivers()) {
380 if (transceiver->receiver()->media_type() == media_type) {
381 return transceiver;
382 }
383 }
384 return nullptr;
385 }
386
deadbeef1dcb1642017-03-29 21:08:16 -0700387 bool SignalingStateStable() {
388 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
389 }
390
391 void CreateDataChannel() { CreateDataChannel(nullptr); }
392
393 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700394 CreateDataChannel(kDataChannelLabel, init);
395 }
396
397 void CreateDataChannel(const std::string& label,
398 const webrtc::DataChannelInit* init) {
399 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700400 ASSERT_TRUE(data_channel_.get() != nullptr);
401 data_observer_.reset(new MockDataChannelObserver(data_channel_));
402 }
403
404 DataChannelInterface* data_channel() { return data_channel_; }
405 const MockDataChannelObserver* data_observer() const {
406 return data_observer_.get();
407 }
408
409 int audio_frames_received() const {
410 return fake_audio_capture_module_->frames_received();
411 }
412
413 // Takes minimum of video frames received for each track.
414 //
415 // Can be used like:
416 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
417 //
418 // To ensure that all video tracks received at least a certain number of
419 // frames.
420 int min_video_frames_received_per_track() const {
421 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200422 if (fake_video_renderers_.empty()) {
423 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700424 }
deadbeef1dcb1642017-03-29 21:08:16 -0700425
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200426 for (const auto& pair : fake_video_renderers_) {
427 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700428 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200429 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700430 }
431
432 // Returns a MockStatsObserver in a state after stats gathering finished,
433 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700434 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700435 webrtc::MediaStreamTrackInterface* track) {
436 rtc::scoped_refptr<MockStatsObserver> observer(
437 new rtc::RefCountedObject<MockStatsObserver>());
438 EXPECT_TRUE(peer_connection_->GetStats(
439 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
440 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
441 return observer;
442 }
443
444 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700445 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
446 return OldGetStatsForTrack(nullptr);
447 }
448
449 // Synchronously gets stats and returns them. If it times out, fails the test
450 // and returns null.
451 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
452 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
453 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
454 peer_connection_->GetStats(callback);
455 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
456 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700457 }
458
459 int rendered_width() {
460 EXPECT_FALSE(fake_video_renderers_.empty());
461 return fake_video_renderers_.empty()
462 ? 0
463 : fake_video_renderers_.begin()->second->width();
464 }
465
466 int rendered_height() {
467 EXPECT_FALSE(fake_video_renderers_.empty());
468 return fake_video_renderers_.empty()
469 ? 0
470 : fake_video_renderers_.begin()->second->height();
471 }
472
473 double rendered_aspect_ratio() {
474 if (rendered_height() == 0) {
475 return 0.0;
476 }
477 return static_cast<double>(rendered_width()) / rendered_height();
478 }
479
480 webrtc::VideoRotation rendered_rotation() {
481 EXPECT_FALSE(fake_video_renderers_.empty());
482 return fake_video_renderers_.empty()
483 ? webrtc::kVideoRotation_0
484 : fake_video_renderers_.begin()->second->rotation();
485 }
486
487 int local_rendered_width() {
488 return local_video_renderer_ ? local_video_renderer_->width() : 0;
489 }
490
491 int local_rendered_height() {
492 return local_video_renderer_ ? local_video_renderer_->height() : 0;
493 }
494
495 double local_rendered_aspect_ratio() {
496 if (local_rendered_height() == 0) {
497 return 0.0;
498 }
499 return static_cast<double>(local_rendered_width()) /
500 local_rendered_height();
501 }
502
503 size_t number_of_remote_streams() {
504 if (!pc()) {
505 return 0;
506 }
507 return pc()->remote_streams()->count();
508 }
509
510 StreamCollectionInterface* remote_streams() const {
511 if (!pc()) {
512 ADD_FAILURE();
513 return nullptr;
514 }
515 return pc()->remote_streams();
516 }
517
518 StreamCollectionInterface* local_streams() {
519 if (!pc()) {
520 ADD_FAILURE();
521 return nullptr;
522 }
523 return pc()->local_streams();
524 }
525
526 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
527 return pc()->signaling_state();
528 }
529
530 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
531 return pc()->ice_connection_state();
532 }
533
534 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
535 return pc()->ice_gathering_state();
536 }
537
538 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
539 // GetReceivers. They're updated automatically when a remote offer/answer
540 // from the fake signaling channel is applied, or when
541 // ResetRtpReceiverObservers below is called.
542 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
543 rtp_receiver_observers() {
544 return rtp_receiver_observers_;
545 }
546
547 void ResetRtpReceiverObservers() {
548 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100549 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
550 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700551 std::unique_ptr<MockRtpReceiverObserver> observer(
552 new MockRtpReceiverObserver(receiver->media_type()));
553 receiver->SetObserver(observer.get());
554 rtp_receiver_observers_.push_back(std::move(observer));
555 }
556 }
557
Steve Antonede9ca52017-10-16 13:04:27 -0700558 rtc::FakeNetworkManager* network() const {
559 return fake_network_manager_.get();
560 }
561 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
562
Qingsi Wang7685e862018-06-11 20:15:46 -0700563 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
564 return event_log_factory_;
565 }
566
deadbeef1dcb1642017-03-29 21:08:16 -0700567 private:
568 explicit PeerConnectionWrapper(const std::string& debug_name)
569 : debug_name_(debug_name) {}
570
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700571 bool Init(const MediaConstraintsInterface* constraints,
572 const PeerConnectionFactory::Options* options,
573 const PeerConnectionInterface::RTCConfiguration* config,
574 webrtc::PeerConnectionDependencies dependencies,
575 rtc::Thread* network_thread,
Qingsi Wang7685e862018-06-11 20:15:46 -0700576 rtc::Thread* worker_thread,
577 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory) {
deadbeef1dcb1642017-03-29 21:08:16 -0700578 // There's an error in this test code if Init ends up being called twice.
579 RTC_DCHECK(!peer_connection_);
580 RTC_DCHECK(!peer_connection_factory_);
581
582 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700583 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700584
585 std::unique_ptr<cricket::PortAllocator> port_allocator(
586 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700587 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700588 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
589 if (!fake_audio_capture_module_) {
590 return false;
591 }
deadbeef1dcb1642017-03-29 21:08:16 -0700592 rtc::Thread* const signaling_thread = rtc::Thread::Current();
Qingsi Wang7685e862018-06-11 20:15:46 -0700593
594 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
595 pc_factory_dependencies.network_thread = network_thread;
596 pc_factory_dependencies.worker_thread = worker_thread;
597 pc_factory_dependencies.signaling_thread = signaling_thread;
598 pc_factory_dependencies.media_engine =
599 cricket::WebRtcMediaEngineFactory::Create(
600 rtc::scoped_refptr<webrtc::AudioDeviceModule>(
601 fake_audio_capture_module_),
602 webrtc::CreateBuiltinAudioEncoderFactory(),
603 webrtc::CreateBuiltinAudioDecoderFactory(),
604 webrtc::CreateBuiltinVideoEncoderFactory(),
605 webrtc::CreateBuiltinVideoDecoderFactory(), nullptr,
606 webrtc::AudioProcessingBuilder().Create());
607 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
608 if (event_log_factory) {
609 event_log_factory_ = event_log_factory.get();
610 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
611 } else {
612 pc_factory_dependencies.event_log_factory =
613 webrtc::CreateRtcEventLogFactory();
614 }
615 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
616 std::move(pc_factory_dependencies));
617
deadbeef1dcb1642017-03-29 21:08:16 -0700618 if (!peer_connection_factory_) {
619 return false;
620 }
621 if (options) {
622 peer_connection_factory_->SetOptions(*options);
623 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800624 if (config) {
625 sdp_semantics_ = config->sdp_semantics;
626 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700627
628 dependencies.allocator = std::move(port_allocator);
deadbeef1dcb1642017-03-29 21:08:16 -0700629 peer_connection_ =
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700630 CreatePeerConnection(constraints, config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700631 return peer_connection_.get() != nullptr;
632 }
633
634 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700635 const MediaConstraintsInterface* constraints,
636 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700637 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700638 PeerConnectionInterface::RTCConfiguration modified_config;
639 // If |config| is null, this will result in a default configuration being
640 // used.
641 if (config) {
642 modified_config = *config;
643 }
644 // Disable resolution adaptation; we don't want it interfering with the
645 // test results.
646 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
647 // ratios and not specific resolutions, is this even necessary?
648 modified_config.set_cpu_adaptation(false);
649
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700650 // Use the legacy interface.
651 if (constraints != nullptr) {
652 return peer_connection_factory_->CreatePeerConnection(
653 modified_config, constraints, std::move(dependencies.allocator),
654 std::move(dependencies.cert_generator), this);
655 }
656 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700657 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700658 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700659 }
660
661 void set_signaling_message_receiver(
662 SignalingMessageReceiver* signaling_message_receiver) {
663 signaling_message_receiver_ = signaling_message_receiver;
664 }
665
666 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
667
Steve Antonede9ca52017-10-16 13:04:27 -0700668 void set_signal_ice_candidates(bool signal) {
669 signal_ice_candidates_ = signal;
670 }
671
deadbeef1dcb1642017-03-29 21:08:16 -0700672 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200673 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700674 // Set max frame rate to 10fps to reduce the risk of test flakiness.
675 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200676 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700677
Niels Möller5c7efe72018-05-11 10:34:46 +0200678 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200679 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
680 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700681 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200682 peer_connection_factory_->CreateVideoTrack(
683 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700684 if (!local_video_renderer_) {
685 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
686 }
687 return track;
688 }
689
690 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100691 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800692 std::unique_ptr<SessionDescriptionInterface> desc =
693 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700694 if (received_sdp_munger_) {
695 received_sdp_munger_(desc->description());
696 }
697
698 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
699 // Setting a remote description may have changed the number of receivers,
700 // so reset the receiver observers.
701 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800702 if (remote_offer_handler_) {
703 remote_offer_handler_();
704 }
deadbeef1dcb1642017-03-29 21:08:16 -0700705 auto answer = CreateAnswer();
706 ASSERT_NE(nullptr, answer);
707 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
708 }
709
710 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100711 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800712 std::unique_ptr<SessionDescriptionInterface> desc =
713 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700714 if (received_sdp_munger_) {
715 received_sdp_munger_(desc->description());
716 }
717
718 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
719 // Set the RtpReceiverObserver after receivers are created.
720 ResetRtpReceiverObservers();
721 }
722
723 // Returns null on failure.
724 std::unique_ptr<SessionDescriptionInterface> CreateOffer() {
725 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
726 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
727 pc()->CreateOffer(observer, offer_answer_options_);
728 return WaitForDescriptionFromObserver(observer);
729 }
730
731 // Returns null on failure.
732 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
733 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
734 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
735 pc()->CreateAnswer(observer, offer_answer_options_);
736 return WaitForDescriptionFromObserver(observer);
737 }
738
739 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100740 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700741 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
742 if (!observer->result()) {
743 return nullptr;
744 }
745 auto description = observer->MoveDescription();
746 if (generated_sdp_munger_) {
747 generated_sdp_munger_(description->description());
748 }
749 return description;
750 }
751
752 // Setting the local description and sending the SDP message over the fake
753 // signaling channel are combined into the same method because the SDP
754 // message needs to be sent as soon as SetLocalDescription finishes, without
755 // waiting for the observer to be called. This ensures that ICE candidates
756 // don't outrace the description.
757 bool SetLocalDescriptionAndSendSdpMessage(
758 std::unique_ptr<SessionDescriptionInterface> desc) {
759 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
760 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100761 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800762 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700763 std::string sdp;
764 EXPECT_TRUE(desc->ToString(&sdp));
765 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800766 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
767 RemoveUnusedVideoRenderers();
768 }
deadbeef1dcb1642017-03-29 21:08:16 -0700769 // As mentioned above, we need to send the message immediately after
770 // SetLocalDescription.
771 SendSdpMessage(type, sdp);
772 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
773 return true;
774 }
775
776 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
777 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
778 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100779 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700780 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800781 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
782 RemoveUnusedVideoRenderers();
783 }
deadbeef1dcb1642017-03-29 21:08:16 -0700784 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
785 return observer->result();
786 }
787
Seth Hampson2f0d7022018-02-20 11:54:42 -0800788 // This is a work around to remove unused fake_video_renderers from
789 // transceivers that have either stopped or are no longer receiving.
790 void RemoveUnusedVideoRenderers() {
791 auto transceivers = pc()->GetTransceivers();
792 for (auto& transceiver : transceivers) {
793 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
794 continue;
795 }
796 // Remove fake video renderers from any stopped transceivers.
797 if (transceiver->stopped()) {
798 auto it =
799 fake_video_renderers_.find(transceiver->receiver()->track()->id());
800 if (it != fake_video_renderers_.end()) {
801 fake_video_renderers_.erase(it);
802 }
803 }
804 // Remove fake video renderers from any transceivers that are no longer
805 // receiving.
806 if ((transceiver->current_direction() &&
807 !webrtc::RtpTransceiverDirectionHasRecv(
808 *transceiver->current_direction()))) {
809 auto it =
810 fake_video_renderers_.find(transceiver->receiver()->track()->id());
811 if (it != fake_video_renderers_.end()) {
812 fake_video_renderers_.erase(it);
813 }
814 }
815 }
816 }
817
deadbeef1dcb1642017-03-29 21:08:16 -0700818 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
819 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800820 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700821 if (signaling_delay_ms_ == 0) {
822 RelaySdpMessageIfReceiverExists(type, msg);
823 } else {
824 invoker_.AsyncInvokeDelayed<void>(
825 RTC_FROM_HERE, rtc::Thread::Current(),
826 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
827 this, type, msg),
828 signaling_delay_ms_);
829 }
830 }
831
Steve Antona3a92c22017-12-07 10:27:41 -0800832 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700833 if (signaling_message_receiver_) {
834 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
835 }
836 }
837
838 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
839 // default).
840 void SendIceMessage(const std::string& sdp_mid,
841 int sdp_mline_index,
842 const std::string& msg) {
843 if (signaling_delay_ms_ == 0) {
844 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
845 } else {
846 invoker_.AsyncInvokeDelayed<void>(
847 RTC_FROM_HERE, rtc::Thread::Current(),
848 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
849 this, sdp_mid, sdp_mline_index, msg),
850 signaling_delay_ms_);
851 }
852 }
853
854 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
855 int sdp_mline_index,
856 const std::string& msg) {
857 if (signaling_message_receiver_) {
858 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
859 msg);
860 }
861 }
862
863 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800864 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
865 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700866 HandleIncomingOffer(msg);
867 } else {
868 HandleIncomingAnswer(msg);
869 }
870 }
871
872 void ReceiveIceMessage(const std::string& sdp_mid,
873 int sdp_mline_index,
874 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100875 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700876 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
877 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
878 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
879 }
880
881 // PeerConnectionObserver callbacks.
882 void OnSignalingChange(
883 webrtc::PeerConnectionInterface::SignalingState new_state) override {
884 EXPECT_EQ(pc()->signaling_state(), new_state);
885 }
Steve Anton15324772018-01-16 10:26:49 -0800886 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
887 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
888 streams) override {
889 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
890 rtc::scoped_refptr<VideoTrackInterface> video_track(
891 static_cast<VideoTrackInterface*>(receiver->track().get()));
892 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700893 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800894 fake_video_renderers_[video_track->id()] =
895 rtc::MakeUnique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700896 }
897 }
Steve Anton15324772018-01-16 10:26:49 -0800898 void OnRemoveTrack(
899 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
900 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
901 auto it = fake_video_renderers_.find(receiver->track()->id());
902 RTC_DCHECK(it != fake_video_renderers_.end());
903 fake_video_renderers_.erase(it);
904 }
905 }
deadbeef1dcb1642017-03-29 21:08:16 -0700906 void OnRenegotiationNeeded() override {}
907 void OnIceConnectionChange(
908 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
909 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700910 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700911 }
912 void OnIceGatheringChange(
913 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700914 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700915 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700916 }
917 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100918 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700919
920 std::string ice_sdp;
921 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700922 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700923 // Remote party may be deleted.
924 return;
925 }
926 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
927 }
928 void OnDataChannel(
929 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100930 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -0700931 data_channel_ = data_channel;
932 data_observer_.reset(new MockDataChannelObserver(data_channel));
933 }
934
deadbeef1dcb1642017-03-29 21:08:16 -0700935 std::string debug_name_;
936
937 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
938
939 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
940 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
941 peer_connection_factory_;
942
Steve Antonede9ca52017-10-16 13:04:27 -0700943 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -0700944 // Needed to keep track of number of frames sent.
945 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
946 // Needed to keep track of number of frames received.
947 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
948 fake_video_renderers_;
949 // Needed to ensure frames aren't received for removed tracks.
950 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
951 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -0700952
953 // For remote peer communication.
954 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
955 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -0700956 bool signal_ice_candidates_ = true;
deadbeef1dcb1642017-03-29 21:08:16 -0700957
Niels Möller5c7efe72018-05-11 10:34:46 +0200958 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -0700959 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +0200960 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
961 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -0700962 // |local_video_renderer_| attached to the first created local video track.
963 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
964
Seth Hampson2f0d7022018-02-20 11:54:42 -0800965 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -0700966 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
967 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
968 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800969 std::function<void()> remote_offer_handler_;
deadbeef1dcb1642017-03-29 21:08:16 -0700970
971 rtc::scoped_refptr<DataChannelInterface> data_channel_;
972 std::unique_ptr<MockDataChannelObserver> data_observer_;
973
974 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
975
Steve Antonede9ca52017-10-16 13:04:27 -0700976 std::vector<PeerConnectionInterface::IceConnectionState>
977 ice_connection_state_history_;
978 std::vector<PeerConnectionInterface::IceGatheringState>
979 ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700980
Qingsi Wang7685e862018-06-11 20:15:46 -0700981 webrtc::FakeRtcEventLogFactory* event_log_factory_;
982
deadbeef1dcb1642017-03-29 21:08:16 -0700983 rtc::AsyncInvoker invoker_;
984
Seth Hampson2f0d7022018-02-20 11:54:42 -0800985 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -0700986};
987
Elad Alon99c3fe52017-10-13 16:29:40 +0200988class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
989 public:
990 virtual ~MockRtcEventLogOutput() = default;
991 MOCK_CONST_METHOD0(IsActive, bool());
992 MOCK_METHOD1(Write, bool(const std::string&));
993};
994
Seth Hampson2f0d7022018-02-20 11:54:42 -0800995// This helper object is used for both specifying how many audio/video frames
996// are expected to be received for a caller/callee. It provides helper functions
997// to specify these expectations. The object initially starts in a state of no
998// expectations.
999class MediaExpectations {
1000 public:
1001 enum ExpectFrames {
1002 kExpectSomeFrames,
1003 kExpectNoFrames,
1004 kNoExpectation,
1005 };
1006
1007 void ExpectBidirectionalAudioAndVideo() {
1008 ExpectBidirectionalAudio();
1009 ExpectBidirectionalVideo();
1010 }
1011
1012 void ExpectBidirectionalAudio() {
1013 CallerExpectsSomeAudio();
1014 CalleeExpectsSomeAudio();
1015 }
1016
1017 void ExpectNoAudio() {
1018 CallerExpectsNoAudio();
1019 CalleeExpectsNoAudio();
1020 }
1021
1022 void ExpectBidirectionalVideo() {
1023 CallerExpectsSomeVideo();
1024 CalleeExpectsSomeVideo();
1025 }
1026
1027 void ExpectNoVideo() {
1028 CallerExpectsNoVideo();
1029 CalleeExpectsNoVideo();
1030 }
1031
1032 void CallerExpectsSomeAudioAndVideo() {
1033 CallerExpectsSomeAudio();
1034 CallerExpectsSomeVideo();
1035 }
1036
1037 void CalleeExpectsSomeAudioAndVideo() {
1038 CalleeExpectsSomeAudio();
1039 CalleeExpectsSomeVideo();
1040 }
1041
1042 // Caller's audio functions.
1043 void CallerExpectsSomeAudio(
1044 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1045 caller_audio_expectation_ = kExpectSomeFrames;
1046 caller_audio_frames_expected_ = expected_audio_frames;
1047 }
1048
1049 void CallerExpectsNoAudio() {
1050 caller_audio_expectation_ = kExpectNoFrames;
1051 caller_audio_frames_expected_ = 0;
1052 }
1053
1054 // Caller's video functions.
1055 void CallerExpectsSomeVideo(
1056 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1057 caller_video_expectation_ = kExpectSomeFrames;
1058 caller_video_frames_expected_ = expected_video_frames;
1059 }
1060
1061 void CallerExpectsNoVideo() {
1062 caller_video_expectation_ = kExpectNoFrames;
1063 caller_video_frames_expected_ = 0;
1064 }
1065
1066 // Callee's audio functions.
1067 void CalleeExpectsSomeAudio(
1068 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1069 callee_audio_expectation_ = kExpectSomeFrames;
1070 callee_audio_frames_expected_ = expected_audio_frames;
1071 }
1072
1073 void CalleeExpectsNoAudio() {
1074 callee_audio_expectation_ = kExpectNoFrames;
1075 callee_audio_frames_expected_ = 0;
1076 }
1077
1078 // Callee's video functions.
1079 void CalleeExpectsSomeVideo(
1080 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1081 callee_video_expectation_ = kExpectSomeFrames;
1082 callee_video_frames_expected_ = expected_video_frames;
1083 }
1084
1085 void CalleeExpectsNoVideo() {
1086 callee_video_expectation_ = kExpectNoFrames;
1087 callee_video_frames_expected_ = 0;
1088 }
1089
1090 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1091 ExpectFrames caller_video_expectation_ = kNoExpectation;
1092 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1093 ExpectFrames callee_video_expectation_ = kNoExpectation;
1094 int caller_audio_frames_expected_ = 0;
1095 int caller_video_frames_expected_ = 0;
1096 int callee_audio_frames_expected_ = 0;
1097 int callee_video_frames_expected_ = 0;
1098};
1099
deadbeef1dcb1642017-03-29 21:08:16 -07001100// Tests two PeerConnections connecting to each other end-to-end, using a
1101// virtual network, fake A/V capture and fake encoder/decoders. The
1102// PeerConnections share the threads/socket servers, but use separate versions
1103// of everything else (including "PeerConnectionFactory"s).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001104class PeerConnectionIntegrationBaseTest : public testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001105 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001106 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1107 : sdp_semantics_(sdp_semantics),
1108 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001109 fss_(new rtc::FirewallSocketServer(ss_.get())),
1110 network_thread_(new rtc::Thread(fss_.get())),
deadbeef1dcb1642017-03-29 21:08:16 -07001111 worker_thread_(rtc::Thread::Create()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001112 network_thread_->SetName("PCNetworkThread", this);
1113 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001114 RTC_CHECK(network_thread_->Start());
1115 RTC_CHECK(worker_thread_->Start());
1116 }
1117
Seth Hampson2f0d7022018-02-20 11:54:42 -08001118 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001119 // The PeerConnections should deleted before the TurnCustomizers.
1120 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1121 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1122 // that the TurnCustomizer outlives the life of the PeerConnection or else
1123 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001124 if (caller_) {
1125 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001126 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001127 }
1128 if (callee_) {
1129 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001130 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001131 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001132
1133 // If turn servers were created for the test they need to be destroyed on
1134 // the network thread.
1135 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1136 turn_servers_.clear();
1137 turn_customizers_.clear();
1138 });
deadbeef1dcb1642017-03-29 21:08:16 -07001139 }
1140
1141 bool SignalingStateStable() {
1142 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1143 }
1144
deadbeef71452802017-05-07 17:21:01 -07001145 bool DtlsConnected() {
1146 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1147 // are connected. This is an important distinction. Once we have separate
1148 // ICE and DTLS state, this check needs to use the DTLS state.
1149 return (callee()->ice_connection_state() ==
1150 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1151 callee()->ice_connection_state() ==
1152 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1153 (caller()->ice_connection_state() ==
1154 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1155 caller()->ice_connection_state() ==
1156 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
1157 }
1158
Qingsi Wang7685e862018-06-11 20:15:46 -07001159 // When |event_log_factory| is null, the default implementation of the event
1160 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001161 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1162 const std::string& debug_name,
1163 const MediaConstraintsInterface* constraints,
1164 const PeerConnectionFactory::Options* options,
1165 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001166 webrtc::PeerConnectionDependencies dependencies,
1167 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001168 RTCConfiguration modified_config;
1169 if (config) {
1170 modified_config = *config;
1171 }
Steve Anton3acffc32018-04-12 17:21:03 -07001172 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001173 if (!dependencies.cert_generator) {
1174 dependencies.cert_generator =
1175 rtc::MakeUnique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001176 }
1177 std::unique_ptr<PeerConnectionWrapper> client(
1178 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001179
Seth Hampson2f0d7022018-02-20 11:54:42 -08001180 if (!client->Init(constraints, options, &modified_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001181 std::move(dependencies), network_thread_.get(),
Qingsi Wang7685e862018-06-11 20:15:46 -07001182 worker_thread_.get(), std::move(event_log_factory))) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001183 return nullptr;
1184 }
1185 return client;
1186 }
1187
Qingsi Wang7685e862018-06-11 20:15:46 -07001188 std::unique_ptr<PeerConnectionWrapper>
1189 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1190 const std::string& debug_name,
1191 const MediaConstraintsInterface* constraints,
1192 const PeerConnectionFactory::Options* options,
1193 const RTCConfiguration* config,
1194 webrtc::PeerConnectionDependencies dependencies) {
1195 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1196 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
1197 return CreatePeerConnectionWrapper(debug_name, constraints, options, config,
1198 std::move(dependencies),
1199 std::move(event_log_factory));
1200 }
1201
deadbeef1dcb1642017-03-29 21:08:16 -07001202 bool CreatePeerConnectionWrappers() {
1203 return CreatePeerConnectionWrappersWithConfig(
1204 PeerConnectionInterface::RTCConfiguration(),
1205 PeerConnectionInterface::RTCConfiguration());
1206 }
1207
Steve Anton3acffc32018-04-12 17:21:03 -07001208 bool CreatePeerConnectionWrappersWithSdpSemantics(
1209 SdpSemantics caller_semantics,
1210 SdpSemantics callee_semantics) {
1211 // Can't specify the sdp_semantics in the passed-in configuration since it
1212 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1213 // stored in sdp_semantics_. So get around this by modifying the instance
1214 // variable before calling CreatePeerConnectionWrapper for the caller and
1215 // callee PeerConnections.
1216 SdpSemantics original_semantics = sdp_semantics_;
1217 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001218 caller_ = CreatePeerConnectionWrapper(
1219 "Caller", nullptr, nullptr, nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -07001220 webrtc::PeerConnectionDependencies(nullptr), nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001221 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001222 callee_ = CreatePeerConnectionWrapper(
1223 "Callee", nullptr, nullptr, nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -07001224 webrtc::PeerConnectionDependencies(nullptr), nullptr);
Steve Anton3acffc32018-04-12 17:21:03 -07001225 sdp_semantics_ = original_semantics;
1226 return caller_ && callee_;
1227 }
1228
deadbeef1dcb1642017-03-29 21:08:16 -07001229 bool CreatePeerConnectionWrappersWithConstraints(
1230 MediaConstraintsInterface* caller_constraints,
1231 MediaConstraintsInterface* callee_constraints) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001232 caller_ = CreatePeerConnectionWrapper(
1233 "Caller", caller_constraints, nullptr, nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -07001234 webrtc::PeerConnectionDependencies(nullptr), nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001235 callee_ = CreatePeerConnectionWrapper(
1236 "Callee", callee_constraints, nullptr, nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -07001237 webrtc::PeerConnectionDependencies(nullptr), nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001238
deadbeef1dcb1642017-03-29 21:08:16 -07001239 return caller_ && callee_;
1240 }
1241
1242 bool CreatePeerConnectionWrappersWithConfig(
1243 const PeerConnectionInterface::RTCConfiguration& caller_config,
1244 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001245 caller_ = CreatePeerConnectionWrapper(
1246 "Caller", nullptr, nullptr, &caller_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001247 webrtc::PeerConnectionDependencies(nullptr), nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001248 callee_ = CreatePeerConnectionWrapper(
1249 "Callee", nullptr, nullptr, &callee_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001250 webrtc::PeerConnectionDependencies(nullptr), nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001251 return caller_ && callee_;
1252 }
1253
1254 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1255 const PeerConnectionInterface::RTCConfiguration& caller_config,
1256 webrtc::PeerConnectionDependencies caller_dependencies,
1257 const PeerConnectionInterface::RTCConfiguration& callee_config,
1258 webrtc::PeerConnectionDependencies callee_dependencies) {
1259 caller_ =
1260 CreatePeerConnectionWrapper("Caller", nullptr, nullptr, &caller_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001261 std::move(caller_dependencies), nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001262 callee_ =
1263 CreatePeerConnectionWrapper("Callee", nullptr, nullptr, &callee_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001264 std::move(callee_dependencies), nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001265 return caller_ && callee_;
1266 }
1267
1268 bool CreatePeerConnectionWrappersWithOptions(
1269 const PeerConnectionFactory::Options& caller_options,
1270 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001271 caller_ = CreatePeerConnectionWrapper(
1272 "Caller", nullptr, &caller_options, nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -07001273 webrtc::PeerConnectionDependencies(nullptr), nullptr);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001274 callee_ = CreatePeerConnectionWrapper(
1275 "Callee", nullptr, &callee_options, nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -07001276 webrtc::PeerConnectionDependencies(nullptr), nullptr);
1277 return caller_ && callee_;
1278 }
1279
1280 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1281 PeerConnectionInterface::RTCConfiguration default_config;
1282 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
1283 "Caller", nullptr, nullptr, &default_config,
1284 webrtc::PeerConnectionDependencies(nullptr));
1285 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
1286 "Callee", nullptr, nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001287 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001288 return caller_ && callee_;
1289 }
1290
Seth Hampson2f0d7022018-02-20 11:54:42 -08001291 std::unique_ptr<PeerConnectionWrapper>
1292 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001293 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1294 new FakeRTCCertificateGenerator());
1295 cert_generator->use_alternate_key();
1296
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001297 webrtc::PeerConnectionDependencies dependencies(nullptr);
1298 dependencies.cert_generator = std::move(cert_generator);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001299 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr, nullptr,
Qingsi Wang7685e862018-06-11 20:15:46 -07001300 std::move(dependencies), nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001301 }
1302
Seth Hampsonaed71642018-06-11 07:41:32 -07001303 cricket::TestTurnServer* CreateTurnServer(
1304 rtc::SocketAddress internal_address,
1305 rtc::SocketAddress external_address,
1306 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1307 const std::string& common_name = "test turn server") {
1308 rtc::Thread* thread = network_thread();
1309 std::unique_ptr<cricket::TestTurnServer> turn_server =
1310 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1311 RTC_FROM_HERE,
1312 [thread, internal_address, external_address, type, common_name] {
1313 return rtc::MakeUnique<cricket::TestTurnServer>(
1314 thread, internal_address, external_address, type,
1315 /*ignore_bad_certs=*/true, common_name);
1316 });
1317 turn_servers_.push_back(std::move(turn_server));
1318 // Interactions with the turn server should be done on the network thread.
1319 return turn_servers_.back().get();
1320 }
1321
1322 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1323 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1324 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1325 RTC_FROM_HERE,
1326 [] { return rtc::MakeUnique<cricket::TestTurnCustomizer>(); });
1327 turn_customizers_.push_back(std::move(turn_customizer));
1328 // Interactions with the turn customizer should be done on the network
1329 // thread.
1330 return turn_customizers_.back().get();
1331 }
1332
1333 // Checks that the function counters for a TestTurnCustomizer are greater than
1334 // 0.
1335 void ExpectTurnCustomizerCountersIncremented(
1336 cricket::TestTurnCustomizer* turn_customizer) {
1337 unsigned int allow_channel_data_counter =
1338 network_thread()->Invoke<unsigned int>(
1339 RTC_FROM_HERE, [turn_customizer] {
1340 return turn_customizer->allow_channel_data_cnt_;
1341 });
1342 EXPECT_GT(allow_channel_data_counter, 0u);
1343 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1344 RTC_FROM_HERE,
1345 [turn_customizer] { return turn_customizer->modify_cnt_; });
1346 EXPECT_GT(modify_counter, 0u);
1347 }
1348
deadbeef1dcb1642017-03-29 21:08:16 -07001349 // Once called, SDP blobs and ICE candidates will be automatically signaled
1350 // between PeerConnections.
1351 void ConnectFakeSignaling() {
1352 caller_->set_signaling_message_receiver(callee_.get());
1353 callee_->set_signaling_message_receiver(caller_.get());
1354 }
1355
Steve Antonede9ca52017-10-16 13:04:27 -07001356 // Once called, SDP blobs will be automatically signaled between
1357 // PeerConnections. Note that ICE candidates will not be signaled unless they
1358 // are in the exchanged SDP blobs.
1359 void ConnectFakeSignalingForSdpOnly() {
1360 ConnectFakeSignaling();
1361 SetSignalIceCandidates(false);
1362 }
1363
deadbeef1dcb1642017-03-29 21:08:16 -07001364 void SetSignalingDelayMs(int delay_ms) {
1365 caller_->set_signaling_delay_ms(delay_ms);
1366 callee_->set_signaling_delay_ms(delay_ms);
1367 }
1368
Steve Antonede9ca52017-10-16 13:04:27 -07001369 void SetSignalIceCandidates(bool signal) {
1370 caller_->set_signal_ice_candidates(signal);
1371 callee_->set_signal_ice_candidates(signal);
1372 }
1373
deadbeef1dcb1642017-03-29 21:08:16 -07001374 // Messages may get lost on the unreliable DataChannel, so we send multiple
1375 // times to avoid test flakiness.
1376 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1377 const std::string& data,
1378 int retries) {
1379 for (int i = 0; i < retries; ++i) {
1380 dc->Send(DataBuffer(data));
1381 }
1382 }
1383
1384 rtc::Thread* network_thread() { return network_thread_.get(); }
1385
1386 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1387
1388 PeerConnectionWrapper* caller() { return caller_.get(); }
1389
1390 // Set the |caller_| to the |wrapper| passed in and return the
1391 // original |caller_|.
1392 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1393 PeerConnectionWrapper* wrapper) {
1394 PeerConnectionWrapper* old = caller_.release();
1395 caller_.reset(wrapper);
1396 return old;
1397 }
1398
1399 PeerConnectionWrapper* callee() { return callee_.get(); }
1400
1401 // Set the |callee_| to the |wrapper| passed in and return the
1402 // original |callee_|.
1403 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1404 PeerConnectionWrapper* wrapper) {
1405 PeerConnectionWrapper* old = callee_.release();
1406 callee_.reset(wrapper);
1407 return old;
1408 }
1409
Steve Antonede9ca52017-10-16 13:04:27 -07001410 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1411
Seth Hampson2f0d7022018-02-20 11:54:42 -08001412 // Expects the provided number of new frames to be received within
1413 // kMaxWaitForFramesMs. The new expected frames are specified in
1414 // |media_expectations|. Returns false if any of the expectations were
1415 // not met.
1416 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1417 // First initialize the expected frame counts based upon the current
1418 // frame count.
1419 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1420 if (media_expectations.caller_audio_expectation_ ==
1421 MediaExpectations::kExpectSomeFrames) {
1422 total_caller_audio_frames_expected +=
1423 media_expectations.caller_audio_frames_expected_;
1424 }
1425 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001426 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001427 if (media_expectations.caller_video_expectation_ ==
1428 MediaExpectations::kExpectSomeFrames) {
1429 total_caller_video_frames_expected +=
1430 media_expectations.caller_video_frames_expected_;
1431 }
1432 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1433 if (media_expectations.callee_audio_expectation_ ==
1434 MediaExpectations::kExpectSomeFrames) {
1435 total_callee_audio_frames_expected +=
1436 media_expectations.callee_audio_frames_expected_;
1437 }
1438 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001439 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001440 if (media_expectations.callee_video_expectation_ ==
1441 MediaExpectations::kExpectSomeFrames) {
1442 total_callee_video_frames_expected +=
1443 media_expectations.callee_video_frames_expected_;
1444 }
deadbeef1dcb1642017-03-29 21:08:16 -07001445
Seth Hampson2f0d7022018-02-20 11:54:42 -08001446 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001447 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001448 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001449 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001450 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001451 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001452 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001453 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001454 total_callee_video_frames_expected,
1455 kMaxWaitForFramesMs);
1456 bool expectations_correct =
1457 caller()->audio_frames_received() >=
1458 total_caller_audio_frames_expected &&
1459 caller()->min_video_frames_received_per_track() >=
1460 total_caller_video_frames_expected &&
1461 callee()->audio_frames_received() >=
1462 total_callee_audio_frames_expected &&
1463 callee()->min_video_frames_received_per_track() >=
1464 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001465
Seth Hampson2f0d7022018-02-20 11:54:42 -08001466 // After the combined wait, print out a more detailed message upon
1467 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001468 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001469 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001470 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001471 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001472 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001473 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001474 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001475 total_callee_video_frames_expected);
1476
1477 // We want to make sure nothing unexpected was received.
1478 if (media_expectations.caller_audio_expectation_ ==
1479 MediaExpectations::kExpectNoFrames) {
1480 EXPECT_EQ(caller()->audio_frames_received(),
1481 total_caller_audio_frames_expected);
1482 if (caller()->audio_frames_received() !=
1483 total_caller_audio_frames_expected) {
1484 expectations_correct = false;
1485 }
1486 }
1487 if (media_expectations.caller_video_expectation_ ==
1488 MediaExpectations::kExpectNoFrames) {
1489 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1490 total_caller_video_frames_expected);
1491 if (caller()->min_video_frames_received_per_track() !=
1492 total_caller_video_frames_expected) {
1493 expectations_correct = false;
1494 }
1495 }
1496 if (media_expectations.callee_audio_expectation_ ==
1497 MediaExpectations::kExpectNoFrames) {
1498 EXPECT_EQ(callee()->audio_frames_received(),
1499 total_callee_audio_frames_expected);
1500 if (callee()->audio_frames_received() !=
1501 total_callee_audio_frames_expected) {
1502 expectations_correct = false;
1503 }
1504 }
1505 if (media_expectations.callee_video_expectation_ ==
1506 MediaExpectations::kExpectNoFrames) {
1507 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1508 total_callee_video_frames_expected);
1509 if (callee()->min_video_frames_received_per_track() !=
1510 total_callee_video_frames_expected) {
1511 expectations_correct = false;
1512 }
1513 }
1514 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001515 }
1516
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001517 void TestNegotiatedCipherSuite(
1518 const PeerConnectionFactory::Options& caller_options,
1519 const PeerConnectionFactory::Options& callee_options,
1520 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001521 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1522 callee_options));
1523 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1524 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1525 caller()->pc()->RegisterUMAObserver(caller_observer);
1526 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001527 caller()->AddAudioVideoTracks();
1528 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001529 caller()->CreateAndSetAndSignalOffer();
1530 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1531 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001532 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001533 EXPECT_EQ(
1534 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
1535 expected_cipher_suite));
1536 caller()->pc()->RegisterUMAObserver(nullptr);
1537 }
1538
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001539 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1540 bool remote_gcm_enabled,
1541 int expected_cipher_suite) {
1542 PeerConnectionFactory::Options caller_options;
1543 caller_options.crypto_options.enable_gcm_crypto_suites = local_gcm_enabled;
1544 PeerConnectionFactory::Options callee_options;
1545 callee_options.crypto_options.enable_gcm_crypto_suites = remote_gcm_enabled;
1546 TestNegotiatedCipherSuite(caller_options, callee_options,
1547 expected_cipher_suite);
1548 }
1549
Seth Hampson2f0d7022018-02-20 11:54:42 -08001550 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001551 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001552
deadbeef1dcb1642017-03-29 21:08:16 -07001553 private:
1554 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001555 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001556 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001557 // |network_thread_| and |worker_thread_| are used by both
1558 // |caller_| and |callee_| so they must be destroyed
1559 // later.
1560 std::unique_ptr<rtc::Thread> network_thread_;
1561 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001562 // The turn servers and turn customizers should be accessed & deleted on the
1563 // network thread to avoid a race with the socket read/write that occurs
1564 // on the network thread.
1565 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1566 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
deadbeef1dcb1642017-03-29 21:08:16 -07001567 std::unique_ptr<PeerConnectionWrapper> caller_;
1568 std::unique_ptr<PeerConnectionWrapper> callee_;
1569};
1570
Seth Hampson2f0d7022018-02-20 11:54:42 -08001571class PeerConnectionIntegrationTest
1572 : public PeerConnectionIntegrationBaseTest,
1573 public ::testing::WithParamInterface<SdpSemantics> {
1574 protected:
1575 PeerConnectionIntegrationTest()
1576 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1577};
1578
1579class PeerConnectionIntegrationTestPlanB
1580 : public PeerConnectionIntegrationBaseTest {
1581 protected:
1582 PeerConnectionIntegrationTestPlanB()
1583 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1584};
1585
1586class PeerConnectionIntegrationTestUnifiedPlan
1587 : public PeerConnectionIntegrationBaseTest {
1588 protected:
1589 PeerConnectionIntegrationTestUnifiedPlan()
1590 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1591};
1592
deadbeef1dcb1642017-03-29 21:08:16 -07001593// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1594// includes testing that the callback is invoked if an observer is connected
1595// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001596TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001597 RtpReceiverObserverOnFirstPacketReceived) {
1598 ASSERT_TRUE(CreatePeerConnectionWrappers());
1599 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001600 caller()->AddAudioVideoTracks();
1601 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001602 // Start offer/answer exchange and wait for it to complete.
1603 caller()->CreateAndSetAndSignalOffer();
1604 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1605 // Should be one receiver each for audio/video.
1606 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1607 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1608 // Wait for all "first packet received" callbacks to be fired.
1609 EXPECT_TRUE_WAIT(
1610 std::all_of(caller()->rtp_receiver_observers().begin(),
1611 caller()->rtp_receiver_observers().end(),
1612 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1613 return o->first_packet_received();
1614 }),
1615 kMaxWaitForFramesMs);
1616 EXPECT_TRUE_WAIT(
1617 std::all_of(callee()->rtp_receiver_observers().begin(),
1618 callee()->rtp_receiver_observers().end(),
1619 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1620 return o->first_packet_received();
1621 }),
1622 kMaxWaitForFramesMs);
1623 // If new observers are set after the first packet was already received, the
1624 // callback should still be invoked.
1625 caller()->ResetRtpReceiverObservers();
1626 callee()->ResetRtpReceiverObservers();
1627 EXPECT_EQ(2, caller()->rtp_receiver_observers().size());
1628 EXPECT_EQ(2, callee()->rtp_receiver_observers().size());
1629 EXPECT_TRUE(
1630 std::all_of(caller()->rtp_receiver_observers().begin(),
1631 caller()->rtp_receiver_observers().end(),
1632 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1633 return o->first_packet_received();
1634 }));
1635 EXPECT_TRUE(
1636 std::all_of(callee()->rtp_receiver_observers().begin(),
1637 callee()->rtp_receiver_observers().end(),
1638 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1639 return o->first_packet_received();
1640 }));
1641}
1642
1643class DummyDtmfObserver : public DtmfSenderObserverInterface {
1644 public:
1645 DummyDtmfObserver() : completed_(false) {}
1646
1647 // Implements DtmfSenderObserverInterface.
1648 void OnToneChange(const std::string& tone) override {
1649 tones_.push_back(tone);
1650 if (tone.empty()) {
1651 completed_ = true;
1652 }
1653 }
1654
1655 const std::vector<std::string>& tones() const { return tones_; }
1656 bool completed() const { return completed_; }
1657
1658 private:
1659 bool completed_;
1660 std::vector<std::string> tones_;
1661};
1662
1663// Assumes |sender| already has an audio track added and the offer/answer
1664// exchange is done.
1665void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1666 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001667 // We should be able to get a DTMF sender from the local sender.
1668 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1669 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1670 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001671 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001672 dtmf_sender->RegisterObserver(&observer);
1673
1674 // Test the DtmfSender object just created.
1675 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1676 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1677
1678 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1679 std::vector<std::string> tones = {"1", "a", ""};
1680 EXPECT_EQ(tones, observer.tones());
1681 dtmf_sender->UnregisterObserver();
1682 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1683}
1684
1685// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1686// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001687TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001688 ASSERT_TRUE(CreatePeerConnectionWrappers());
1689 ConnectFakeSignaling();
1690 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001691 caller()->AddAudioTrack();
1692 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001693 caller()->CreateAndSetAndSignalOffer();
1694 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001695 // DTLS must finish before the DTMF sender can be used reliably.
1696 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001697 TestDtmfFromSenderToReceiver(caller(), callee());
1698 TestDtmfFromSenderToReceiver(callee(), caller());
1699}
1700
1701// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1702// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001703TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001704 ASSERT_TRUE(CreatePeerConnectionWrappers());
1705 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001706 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1707 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1708 caller()->pc()->RegisterUMAObserver(caller_observer);
1709
deadbeef1dcb1642017-03-29 21:08:16 -07001710 // Do normal offer/answer and wait for some frames to be received in each
1711 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001712 caller()->AddAudioVideoTracks();
1713 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001714 caller()->CreateAndSetAndSignalOffer();
1715 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001716 MediaExpectations media_expectations;
1717 media_expectations.ExpectBidirectionalAudioAndVideo();
1718 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001719 EXPECT_LE(
1720 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1721 webrtc::kEnumCounterKeyProtocolDtls));
1722 EXPECT_EQ(
1723 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1724 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001725}
1726
1727// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001728TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001729 PeerConnectionInterface::RTCConfiguration sdes_config;
1730 sdes_config.enable_dtls_srtp.emplace(false);
1731 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1732 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001733 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
1734 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
1735 caller()->pc()->RegisterUMAObserver(caller_observer);
deadbeef1dcb1642017-03-29 21:08:16 -07001736
1737 // Do normal offer/answer and wait for some frames to be received in each
1738 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001739 caller()->AddAudioVideoTracks();
1740 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001741 caller()->CreateAndSetAndSignalOffer();
1742 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001743 MediaExpectations media_expectations;
1744 media_expectations.ExpectBidirectionalAudioAndVideo();
1745 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Harald Alvestrand194939b2018-01-24 16:04:13 +01001746 EXPECT_LE(
1747 1, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1748 webrtc::kEnumCounterKeyProtocolSdes));
1749 EXPECT_EQ(
1750 0, caller_observer->GetEnumCounter(webrtc::kEnumCounterKeyProtocol,
1751 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001752}
1753
Steve Anton8c0f7a72017-10-03 10:03:10 -07001754// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1755// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001756TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001757 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1758 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1759 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1760 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1761 return pc->GetRemoteAudioSSLCertificate();
1762 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001763 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1764 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1765 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1766 return pc->GetRemoteAudioSSLCertChain();
1767 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001768
1769 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1770 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1771
1772 // Configure each side with a known certificate so they can be compared later.
1773 PeerConnectionInterface::RTCConfiguration caller_config;
1774 caller_config.enable_dtls_srtp.emplace(true);
1775 caller_config.certificates.push_back(caller_cert);
1776 PeerConnectionInterface::RTCConfiguration callee_config;
1777 callee_config.enable_dtls_srtp.emplace(true);
1778 callee_config.certificates.push_back(callee_cert);
1779 ASSERT_TRUE(
1780 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1781 ConnectFakeSignaling();
1782
1783 // When first initialized, there should not be a remote SSL certificate (and
1784 // calling this method should not crash).
1785 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1786 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001787 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1788 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001789
Steve Anton15324772018-01-16 10:26:49 -08001790 caller()->AddAudioTrack();
1791 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001792 caller()->CreateAndSetAndSignalOffer();
1793 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1794 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1795
1796 // Once DTLS has been connected, each side should return the other's SSL
1797 // certificate when calling GetRemoteAudioSSLCertificate.
1798
1799 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1800 ASSERT_TRUE(caller_remote_cert);
1801 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1802 caller_remote_cert->ToPEMString());
1803
1804 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1805 ASSERT_TRUE(callee_remote_cert);
1806 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1807 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08001808
1809 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
1810 ASSERT_TRUE(caller_remote_cert_chain);
1811 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
1812 auto remote_cert = &caller_remote_cert_chain->Get(0);
1813 EXPECT_EQ(callee_cert->ssl_certificate().ToPEMString(),
1814 remote_cert->ToPEMString());
1815
1816 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
1817 ASSERT_TRUE(callee_remote_cert_chain);
1818 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
1819 remote_cert = &callee_remote_cert_chain->Get(0);
1820 EXPECT_EQ(caller_cert->ssl_certificate().ToPEMString(),
1821 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07001822}
1823
deadbeef1dcb1642017-03-29 21:08:16 -07001824// This test sets up a call between two parties with a source resolution of
1825// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001826TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001827 Send1280By720ResolutionAndReceive16To9AspectRatio) {
1828 ASSERT_TRUE(CreatePeerConnectionWrappers());
1829 ConnectFakeSignaling();
1830
Niels Möller5c7efe72018-05-11 10:34:46 +02001831 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
1832 webrtc::FakePeriodicVideoSource::Config config;
1833 config.width = 1280;
1834 config.height = 720;
1835 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
1836 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07001837
1838 // Do normal offer/answer and wait for at least one frame to be received in
1839 // each direction.
1840 caller()->CreateAndSetAndSignalOffer();
1841 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
1842 callee()->min_video_frames_received_per_track() > 0,
1843 kMaxWaitForFramesMs);
1844
1845 // Check rendered aspect ratio.
1846 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
1847 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
1848 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
1849 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
1850}
1851
1852// This test sets up an one-way call, with media only from caller to
1853// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001854TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07001855 ASSERT_TRUE(CreatePeerConnectionWrappers());
1856 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001857 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001858 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001859 MediaExpectations media_expectations;
1860 media_expectations.CalleeExpectsSomeAudioAndVideo();
1861 media_expectations.CallerExpectsNoAudio();
1862 media_expectations.CallerExpectsNoVideo();
1863 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001864}
1865
1866// This test sets up a audio call initially, with the callee rejecting video
1867// initially. Then later the callee decides to upgrade to audio/video, and
1868// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001869TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07001870 ASSERT_TRUE(CreatePeerConnectionWrappers());
1871 ConnectFakeSignaling();
1872 // Initially, offer an audio/video stream from the caller, but refuse to
1873 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08001874 caller()->AddAudioVideoTracks();
1875 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001876 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1877 PeerConnectionInterface::RTCOfferAnswerOptions options;
1878 options.offer_to_receive_video = 0;
1879 callee()->SetOfferAnswerOptions(options);
1880 } else {
1881 callee()->SetRemoteOfferHandler([this] {
1882 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
1883 });
1884 }
deadbeef1dcb1642017-03-29 21:08:16 -07001885 // Do offer/answer and make sure audio is still received end-to-end.
1886 caller()->CreateAndSetAndSignalOffer();
1887 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001888 {
1889 MediaExpectations media_expectations;
1890 media_expectations.ExpectBidirectionalAudio();
1891 media_expectations.ExpectNoVideo();
1892 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1893 }
deadbeef1dcb1642017-03-29 21:08:16 -07001894 // Sanity check that the callee's description has a rejected video section.
1895 ASSERT_NE(nullptr, callee()->pc()->local_description());
1896 const ContentInfo* callee_video_content =
1897 GetFirstVideoContent(callee()->pc()->local_description()->description());
1898 ASSERT_NE(nullptr, callee_video_content);
1899 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001900
deadbeef1dcb1642017-03-29 21:08:16 -07001901 // Now negotiate with video and ensure negotiation succeeds, with video
1902 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08001903 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001904 if (sdp_semantics_ == SdpSemantics::kPlanB) {
1905 PeerConnectionInterface::RTCOfferAnswerOptions options;
1906 options.offer_to_receive_video = 1;
1907 callee()->SetOfferAnswerOptions(options);
1908 } else {
1909 callee()->SetRemoteOfferHandler(nullptr);
1910 caller()->SetRemoteOfferHandler([this] {
1911 // The caller creates a new transceiver to receive video on when receiving
1912 // the offer, but by default it is send only.
1913 auto transceivers = caller()->pc()->GetTransceivers();
1914 ASSERT_EQ(3, transceivers.size());
1915 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
1916 transceivers[2]->receiver()->media_type());
1917 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
1918 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
1919 });
1920 }
deadbeef1dcb1642017-03-29 21:08:16 -07001921 callee()->CreateAndSetAndSignalOffer();
1922 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001923 {
1924 // Expect additional audio frames to be received after the upgrade.
1925 MediaExpectations media_expectations;
1926 media_expectations.ExpectBidirectionalAudioAndVideo();
1927 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1928 }
deadbeef1dcb1642017-03-29 21:08:16 -07001929}
1930
deadbeef4389b4d2017-09-07 09:07:36 -07001931// Simpler than the above test; just add an audio track to an established
1932// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001933TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07001934 ASSERT_TRUE(CreatePeerConnectionWrappers());
1935 ConnectFakeSignaling();
1936 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08001937 caller()->AddVideoTrack();
1938 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001939 caller()->CreateAndSetAndSignalOffer();
1940 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1941 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08001942 caller()->AddAudioTrack();
1943 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07001944 caller()->CreateAndSetAndSignalOffer();
1945 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1946 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001947 MediaExpectations media_expectations;
1948 media_expectations.ExpectBidirectionalAudioAndVideo();
1949 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07001950}
1951
deadbeef1dcb1642017-03-29 21:08:16 -07001952// This test sets up a call that's transferred to a new caller with a different
1953// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001954TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07001955 ASSERT_TRUE(CreatePeerConnectionWrappers());
1956 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001957 caller()->AddAudioVideoTracks();
1958 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001959 caller()->CreateAndSetAndSignalOffer();
1960 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1961
1962 // Keep the original peer around which will still send packets to the
1963 // receiving client. These SRTP packets will be dropped.
1964 std::unique_ptr<PeerConnectionWrapper> original_peer(
1965 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001966 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001967 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1968 // directly above.
1969 original_peer->pc()->Close();
1970
1971 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001972 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001973 caller()->CreateAndSetAndSignalOffer();
1974 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1975 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001976 MediaExpectations media_expectations;
1977 media_expectations.ExpectBidirectionalAudioAndVideo();
1978 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07001979}
1980
1981// This test sets up a call that's transferred to a new callee with a different
1982// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001983TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07001984 ASSERT_TRUE(CreatePeerConnectionWrappers());
1985 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001986 caller()->AddAudioVideoTracks();
1987 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001988 caller()->CreateAndSetAndSignalOffer();
1989 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1990
1991 // Keep the original peer around which will still send packets to the
1992 // receiving client. These SRTP packets will be dropped.
1993 std::unique_ptr<PeerConnectionWrapper> original_peer(
1994 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08001995 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07001996 // TODO(deadbeef): Why do we call Close here? That goes against the comment
1997 // directly above.
1998 original_peer->pc()->Close();
1999
2000 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002001 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002002 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2003 caller()->CreateAndSetAndSignalOffer();
2004 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2005 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002006 MediaExpectations media_expectations;
2007 media_expectations.ExpectBidirectionalAudioAndVideo();
2008 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002009}
2010
2011// This test sets up a non-bundled call and negotiates bundling at the same
2012// time as starting an ICE restart. When bundling is in effect in the restart,
2013// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002014TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002015 ASSERT_TRUE(CreatePeerConnectionWrappers());
2016 ConnectFakeSignaling();
2017
Steve Anton15324772018-01-16 10:26:49 -08002018 caller()->AddAudioVideoTracks();
2019 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002020 // Remove the bundle group from the SDP received by the callee.
2021 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2022 desc->RemoveGroupByName("BUNDLE");
2023 });
2024 caller()->CreateAndSetAndSignalOffer();
2025 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002026 {
2027 MediaExpectations media_expectations;
2028 media_expectations.ExpectBidirectionalAudioAndVideo();
2029 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2030 }
deadbeef1dcb1642017-03-29 21:08:16 -07002031 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2032 callee()->SetReceivedSdpMunger(nullptr);
2033 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2034 caller()->CreateAndSetAndSignalOffer();
2035 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2036
2037 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002038 {
2039 MediaExpectations media_expectations;
2040 media_expectations.ExpectBidirectionalAudioAndVideo();
2041 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2042 }
deadbeef1dcb1642017-03-29 21:08:16 -07002043}
2044
2045// Test CVO (Coordination of Video Orientation). If a video source is rotated
2046// and both peers support the CVO RTP header extension, the actual video frames
2047// don't need to be encoded in different resolutions, since the rotation is
2048// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002049TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002050 ASSERT_TRUE(CreatePeerConnectionWrappers());
2051 ConnectFakeSignaling();
2052 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002053 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002054 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002055 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002056 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2057
2058 // Wait for video frames to be received by both sides.
2059 caller()->CreateAndSetAndSignalOffer();
2060 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2061 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2062 callee()->min_video_frames_received_per_track() > 0,
2063 kMaxWaitForFramesMs);
2064
2065 // Ensure that the aspect ratio is unmodified.
2066 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2067 // not just assumed.
2068 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2069 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2070 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2071 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2072 // Ensure that the CVO bits were surfaced to the renderer.
2073 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2074 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2075}
2076
2077// Test that when the CVO extension isn't supported, video is rotated the
2078// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002079TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002080 ASSERT_TRUE(CreatePeerConnectionWrappers());
2081 ConnectFakeSignaling();
2082 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002083 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002084 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002085 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002086 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2087
2088 // Remove the CVO extension from the offered SDP.
2089 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2090 cricket::VideoContentDescription* video =
2091 GetFirstVideoContentDescription(desc);
2092 video->ClearRtpHeaderExtensions();
2093 });
2094 // Wait for video frames to be received by both sides.
2095 caller()->CreateAndSetAndSignalOffer();
2096 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2097 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2098 callee()->min_video_frames_received_per_track() > 0,
2099 kMaxWaitForFramesMs);
2100
2101 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2102 // rotation.
2103 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2104 // not just assumed.
2105 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2106 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2107 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2108 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2109 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2110 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2111 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2112}
2113
deadbeef1dcb1642017-03-29 21:08:16 -07002114// Test that if the answerer rejects the audio m= section, no audio is sent or
2115// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002116TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002117 ASSERT_TRUE(CreatePeerConnectionWrappers());
2118 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002119 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002120 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2121 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2122 // it will reject the audio m= section completely.
2123 PeerConnectionInterface::RTCOfferAnswerOptions options;
2124 options.offer_to_receive_audio = 0;
2125 callee()->SetOfferAnswerOptions(options);
2126 } else {
2127 // Stopping the audio RtpTransceiver will cause the media section to be
2128 // rejected in the answer.
2129 callee()->SetRemoteOfferHandler([this] {
2130 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2131 });
2132 }
Steve Anton15324772018-01-16 10:26:49 -08002133 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002134 // Do offer/answer and wait for successful end-to-end video frames.
2135 caller()->CreateAndSetAndSignalOffer();
2136 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002137 MediaExpectations media_expectations;
2138 media_expectations.ExpectBidirectionalVideo();
2139 media_expectations.ExpectNoAudio();
2140 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2141
deadbeef1dcb1642017-03-29 21:08:16 -07002142 // Sanity check that the callee's description has a rejected audio section.
2143 ASSERT_NE(nullptr, callee()->pc()->local_description());
2144 const ContentInfo* callee_audio_content =
2145 GetFirstAudioContent(callee()->pc()->local_description()->description());
2146 ASSERT_NE(nullptr, callee_audio_content);
2147 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002148 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2149 // The caller's transceiver should have stopped after receiving the answer.
2150 EXPECT_TRUE(caller()
2151 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2152 ->stopped());
2153 }
deadbeef1dcb1642017-03-29 21:08:16 -07002154}
2155
2156// Test that if the answerer rejects the video m= section, no video is sent or
2157// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002158TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002159 ASSERT_TRUE(CreatePeerConnectionWrappers());
2160 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002161 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002162 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2163 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2164 // it will reject the video m= section completely.
2165 PeerConnectionInterface::RTCOfferAnswerOptions options;
2166 options.offer_to_receive_video = 0;
2167 callee()->SetOfferAnswerOptions(options);
2168 } else {
2169 // Stopping the video RtpTransceiver will cause the media section to be
2170 // rejected in the answer.
2171 callee()->SetRemoteOfferHandler([this] {
2172 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2173 });
2174 }
Steve Anton15324772018-01-16 10:26:49 -08002175 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002176 // Do offer/answer and wait for successful end-to-end audio frames.
2177 caller()->CreateAndSetAndSignalOffer();
2178 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002179 MediaExpectations media_expectations;
2180 media_expectations.ExpectBidirectionalAudio();
2181 media_expectations.ExpectNoVideo();
2182 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2183
deadbeef1dcb1642017-03-29 21:08:16 -07002184 // Sanity check that the callee's description has a rejected video section.
2185 ASSERT_NE(nullptr, callee()->pc()->local_description());
2186 const ContentInfo* callee_video_content =
2187 GetFirstVideoContent(callee()->pc()->local_description()->description());
2188 ASSERT_NE(nullptr, callee_video_content);
2189 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002190 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2191 // The caller's transceiver should have stopped after receiving the answer.
2192 EXPECT_TRUE(caller()
2193 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2194 ->stopped());
2195 }
deadbeef1dcb1642017-03-29 21:08:16 -07002196}
2197
2198// Test that if the answerer rejects both audio and video m= sections, nothing
2199// bad happens.
2200// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2201// test anything but the fact that negotiation succeeds, which doesn't mean
2202// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002203TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002204 ASSERT_TRUE(CreatePeerConnectionWrappers());
2205 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002206 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002207 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2208 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2209 // will reject both audio and video m= sections.
2210 PeerConnectionInterface::RTCOfferAnswerOptions options;
2211 options.offer_to_receive_audio = 0;
2212 options.offer_to_receive_video = 0;
2213 callee()->SetOfferAnswerOptions(options);
2214 } else {
2215 callee()->SetRemoteOfferHandler([this] {
2216 // Stopping all transceivers will cause all media sections to be rejected.
2217 for (auto transceiver : callee()->pc()->GetTransceivers()) {
2218 transceiver->Stop();
2219 }
2220 });
2221 }
deadbeef1dcb1642017-03-29 21:08:16 -07002222 // Do offer/answer and wait for stable signaling state.
2223 caller()->CreateAndSetAndSignalOffer();
2224 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002225
deadbeef1dcb1642017-03-29 21:08:16 -07002226 // Sanity check that the callee's description has rejected m= sections.
2227 ASSERT_NE(nullptr, callee()->pc()->local_description());
2228 const ContentInfo* callee_audio_content =
2229 GetFirstAudioContent(callee()->pc()->local_description()->description());
2230 ASSERT_NE(nullptr, callee_audio_content);
2231 EXPECT_TRUE(callee_audio_content->rejected);
2232 const ContentInfo* callee_video_content =
2233 GetFirstVideoContent(callee()->pc()->local_description()->description());
2234 ASSERT_NE(nullptr, callee_video_content);
2235 EXPECT_TRUE(callee_video_content->rejected);
2236}
2237
2238// This test sets up an audio and video call between two parties. After the
2239// call runs for a while, the caller sends an updated offer with video being
2240// rejected. Once the re-negotiation is done, the video flow should stop and
2241// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002242TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002243 ASSERT_TRUE(CreatePeerConnectionWrappers());
2244 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002245 caller()->AddAudioVideoTracks();
2246 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002247 caller()->CreateAndSetAndSignalOffer();
2248 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002249 {
2250 MediaExpectations media_expectations;
2251 media_expectations.ExpectBidirectionalAudioAndVideo();
2252 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2253 }
deadbeef1dcb1642017-03-29 21:08:16 -07002254 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002255 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2256 caller()->SetGeneratedSdpMunger(
2257 [](cricket::SessionDescription* description) {
2258 for (cricket::ContentInfo& content : description->contents()) {
2259 if (cricket::IsVideoContent(&content)) {
2260 content.rejected = true;
2261 }
2262 }
2263 });
2264 } else {
2265 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2266 }
deadbeef1dcb1642017-03-29 21:08:16 -07002267 caller()->CreateAndSetAndSignalOffer();
2268 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2269
2270 // Sanity check that the caller's description has a rejected video section.
2271 ASSERT_NE(nullptr, caller()->pc()->local_description());
2272 const ContentInfo* caller_video_content =
2273 GetFirstVideoContent(caller()->pc()->local_description()->description());
2274 ASSERT_NE(nullptr, caller_video_content);
2275 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002276 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002277 {
2278 MediaExpectations media_expectations;
2279 media_expectations.ExpectBidirectionalAudio();
2280 media_expectations.ExpectNoVideo();
2281 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2282 }
deadbeef1dcb1642017-03-29 21:08:16 -07002283}
2284
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002285// Do one offer/answer with audio, another that disables it (rejecting the m=
2286// section), and another that re-enables it. Regression test for:
2287// bugs.webrtc.org/6023
2288TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2289 ASSERT_TRUE(CreatePeerConnectionWrappers());
2290 ConnectFakeSignaling();
2291
2292 // Add audio track, do normal offer/answer.
2293 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2294 caller()->CreateLocalAudioTrack();
2295 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2296 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2297 caller()->CreateAndSetAndSignalOffer();
2298 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2299
2300 // Remove audio track, and set offer_to_receive_audio to false to cause the
2301 // m= section to be completely disabled, not just "recvonly".
2302 caller()->pc()->RemoveTrack(sender);
2303 PeerConnectionInterface::RTCOfferAnswerOptions options;
2304 options.offer_to_receive_audio = 0;
2305 caller()->SetOfferAnswerOptions(options);
2306 caller()->CreateAndSetAndSignalOffer();
2307 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2308
2309 // Add the audio track again, expecting negotiation to succeed and frames to
2310 // flow.
2311 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2312 options.offer_to_receive_audio = 1;
2313 caller()->SetOfferAnswerOptions(options);
2314 caller()->CreateAndSetAndSignalOffer();
2315 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2316
2317 MediaExpectations media_expectations;
2318 media_expectations.CalleeExpectsSomeAudio();
2319 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2320}
2321
deadbeef1dcb1642017-03-29 21:08:16 -07002322// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2323// is needed to support legacy endpoints.
2324// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2325// add a test for an end-to-end test without MID signaling either (basically,
2326// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002327TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002328 ASSERT_TRUE(CreatePeerConnectionWrappers());
2329 ConnectFakeSignaling();
2330 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002331 caller()->AddAudioVideoTracks();
2332 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002333 // Remove SSRCs and MSIDs from the received offer SDP.
2334 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002335 caller()->CreateAndSetAndSignalOffer();
2336 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002337 MediaExpectations media_expectations;
2338 media_expectations.ExpectBidirectionalAudioAndVideo();
2339 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002340}
2341
Seth Hampson5897a6e2018-04-03 11:16:33 -07002342// Basic end-to-end test, without SSRC signaling. This means that the track
2343// was created properly and frames are delivered when the MSIDs are communicated
2344// with a=msid lines and no a=ssrc lines.
2345TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2346 EndToEndCallWithoutSsrcSignaling) {
2347 const char kStreamId[] = "streamId";
2348 ASSERT_TRUE(CreatePeerConnectionWrappers());
2349 ConnectFakeSignaling();
2350 // Add just audio tracks.
2351 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2352 callee()->AddAudioTrack();
2353
2354 // Remove SSRCs from the received offer SDP.
2355 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2356 caller()->CreateAndSetAndSignalOffer();
2357 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2358 MediaExpectations media_expectations;
2359 media_expectations.ExpectBidirectionalAudio();
2360 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2361}
2362
Steve Antondf527fd2018-04-27 15:52:03 -07002363// Tests that video flows between multiple video tracks when SSRCs are not
2364// signaled. This exercises the MID RTP header extension which is needed to
2365// demux the incoming video tracks.
2366TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2367 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2368 ASSERT_TRUE(CreatePeerConnectionWrappers());
2369 ConnectFakeSignaling();
2370 caller()->AddVideoTrack();
2371 caller()->AddVideoTrack();
2372 callee()->AddVideoTrack();
2373 callee()->AddVideoTrack();
2374
2375 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2376 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2377 caller()->CreateAndSetAndSignalOffer();
2378 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2379 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2380 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2381
2382 // Expect video to be received in both directions on both tracks.
2383 MediaExpectations media_expectations;
2384 media_expectations.ExpectBidirectionalVideo();
2385 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2386}
2387
deadbeef1dcb1642017-03-29 21:08:16 -07002388// Test that if two video tracks are sent (from caller to callee, in this test),
2389// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002390TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002391 ASSERT_TRUE(CreatePeerConnectionWrappers());
2392 ConnectFakeSignaling();
2393 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002394 caller()->AddAudioVideoTracks();
2395 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002396 caller()->CreateAndSetAndSignalOffer();
2397 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002398 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002399
2400 MediaExpectations media_expectations;
2401 media_expectations.CalleeExpectsSomeAudioAndVideo();
2402 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002403}
2404
2405static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2406 bool first = true;
2407 for (cricket::ContentInfo& content : desc->contents()) {
2408 if (first) {
2409 first = false;
2410 continue;
2411 }
2412 content.bundle_only = true;
2413 }
2414 first = true;
2415 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2416 if (first) {
2417 first = false;
2418 continue;
2419 }
2420 transport.description.ice_ufrag.clear();
2421 transport.description.ice_pwd.clear();
2422 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2423 transport.description.identity_fingerprint.reset(nullptr);
2424 }
2425}
2426
2427// Test that if applying a true "max bundle" offer, which uses ports of 0,
2428// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2429// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2430// successfully and media flows.
2431// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2432// TODO(deadbeef): Won't need this test once we start generating actual
2433// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002434TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002435 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2436 ASSERT_TRUE(CreatePeerConnectionWrappers());
2437 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002438 caller()->AddAudioVideoTracks();
2439 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002440 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2441 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2442 // but the first m= section.
2443 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2444 caller()->CreateAndSetAndSignalOffer();
2445 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002446 MediaExpectations media_expectations;
2447 media_expectations.ExpectBidirectionalAudioAndVideo();
2448 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002449}
2450
2451// Test that we can receive the audio output level from a remote audio track.
2452// TODO(deadbeef): Use a fake audio source and verify that the output level is
2453// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002454TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002455 ASSERT_TRUE(CreatePeerConnectionWrappers());
2456 ConnectFakeSignaling();
2457 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002458 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002459 caller()->CreateAndSetAndSignalOffer();
2460 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2461
2462 // Get the audio output level stats. Note that the level is not available
2463 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002464 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002465 kMaxWaitForFramesMs);
2466}
2467
2468// Test that an audio input level is reported.
2469// TODO(deadbeef): Use a fake audio source and verify that the input level is
2470// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002471TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002472 ASSERT_TRUE(CreatePeerConnectionWrappers());
2473 ConnectFakeSignaling();
2474 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002475 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002476 caller()->CreateAndSetAndSignalOffer();
2477 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2478
2479 // Get the audio input level stats. The level should be available very
2480 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002481 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002482 kMaxWaitForStatsMs);
2483}
2484
2485// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002486TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002487 ASSERT_TRUE(CreatePeerConnectionWrappers());
2488 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002489 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002490 // Do offer/answer, wait for the callee to receive some frames.
2491 caller()->CreateAndSetAndSignalOffer();
2492 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002493
2494 MediaExpectations media_expectations;
2495 media_expectations.CalleeExpectsSomeAudioAndVideo();
2496 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002497
2498 // Get a handle to the remote tracks created, so they can be used as GetStats
2499 // filters.
Steve Anton15324772018-01-16 10:26:49 -08002500 for (auto receiver : callee()->pc()->GetReceivers()) {
2501 // We received frames, so we definitely should have nonzero "received bytes"
2502 // stats at this point.
2503 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2504 0);
2505 }
deadbeef1dcb1642017-03-29 21:08:16 -07002506}
2507
2508// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002509TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002510 ASSERT_TRUE(CreatePeerConnectionWrappers());
2511 ConnectFakeSignaling();
2512 auto audio_track = caller()->CreateLocalAudioTrack();
2513 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002514 caller()->AddTrack(audio_track);
2515 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002516 // Do offer/answer, wait for the callee to receive some frames.
2517 caller()->CreateAndSetAndSignalOffer();
2518 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002519 MediaExpectations media_expectations;
2520 media_expectations.CalleeExpectsSomeAudioAndVideo();
2521 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002522
2523 // The callee received frames, so we definitely should have nonzero "sent
2524 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002525 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2526 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2527}
2528
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002529// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002530TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002531 ASSERT_TRUE(CreatePeerConnectionWrappers());
2532 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002533 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002534
Steve Anton15324772018-01-16 10:26:49 -08002535 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002536
2537 // Do offer/answer, wait for the callee to receive some frames.
2538 caller()->CreateAndSetAndSignalOffer();
2539 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2540
2541 // Get the remote audio track created on the receiver, so they can be used as
2542 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002543 auto receivers = callee()->pc()->GetReceivers();
2544 ASSERT_EQ(1u, receivers.size());
2545 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002546
2547 // Get the audio output level stats. Note that the level is not available
2548 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002549 EXPECT_TRUE_WAIT(
2550 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2551 0,
2552 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002553}
2554
deadbeefd8ad7882017-04-18 16:01:17 -07002555// Test that we can get stats (using the new stats implemnetation) for
2556// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
2557// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002558TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07002559 GetStatsForUnsignaledStreamWithNewStatsApi) {
2560 ASSERT_TRUE(CreatePeerConnectionWrappers());
2561 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002562 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07002563 // Remove SSRCs and MSIDs from the received offer SDP.
2564 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2565 caller()->CreateAndSetAndSignalOffer();
2566 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002567 MediaExpectations media_expectations;
2568 media_expectations.CalleeExpectsSomeAudio(1);
2569 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07002570
2571 // We received a frame, so we should have nonzero "bytes received" stats for
2572 // the unsignaled stream, if stats are working for it.
2573 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2574 callee()->NewGetStats();
2575 ASSERT_NE(nullptr, report);
2576 auto inbound_stream_stats =
2577 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2578 ASSERT_EQ(1U, inbound_stream_stats.size());
2579 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
2580 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07002581 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
2582}
2583
2584// Test that we can successfully get the media related stats (audio level
2585// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002586TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002587 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2588 ASSERT_TRUE(CreatePeerConnectionWrappers());
2589 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002590 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002591 // Remove SSRCs and MSIDs from the received offer SDP.
2592 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2593 caller()->CreateAndSetAndSignalOffer();
2594 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002595 MediaExpectations media_expectations;
2596 media_expectations.CalleeExpectsSomeAudio(1);
2597 media_expectations.CalleeExpectsSomeVideo(1);
2598 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002599
2600 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2601 callee()->NewGetStats();
2602 ASSERT_NE(nullptr, report);
2603
2604 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2605 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2606 ASSERT_GE(audio_index, 0);
2607 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002608}
2609
deadbeef4e2deab2017-09-20 13:56:21 -07002610// Helper for test below.
2611void ModifySsrcs(cricket::SessionDescription* desc) {
2612 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002613 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002614 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002615 for (uint32_t& ssrc : stream.ssrcs) {
2616 ssrc = rtc::CreateRandomId();
2617 }
2618 }
2619 }
2620}
2621
2622// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2623// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2624// This should result in two "RTCInboundRTPStreamStats", but only one
2625// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2626// being reset to 0 once the SSRC change occurs.
2627//
2628// Regression test for this bug:
2629// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2630//
2631// The bug causes the track stats to only represent one of the two streams:
2632// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2633// that the track stat counters would reset to 0 when the new stream is
2634// received, and a 50% chance that they'll stop updating (while
2635// "concealed_samples" continues increasing, due to silence being generated for
2636// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002637TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002638 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002639 ASSERT_TRUE(CreatePeerConnectionWrappers());
2640 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002641 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002642 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2643 // that doesn't signal SSRCs (from the callee's perspective).
2644 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2645 caller()->CreateAndSetAndSignalOffer();
2646 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2647 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002648 {
2649 MediaExpectations media_expectations;
2650 media_expectations.CalleeExpectsSomeAudio(50);
2651 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2652 }
deadbeef4e2deab2017-09-20 13:56:21 -07002653 // Some audio frames were received, so we should have nonzero "samples
2654 // received" for the track.
2655 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2656 callee()->NewGetStats();
2657 ASSERT_NE(nullptr, report);
2658 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2659 ASSERT_EQ(1U, track_stats.size());
2660 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2661 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2662 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2663
2664 // Create a new offer and munge it to cause the caller to use a new SSRC.
2665 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2666 caller()->CreateAndSetAndSignalOffer();
2667 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2668 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2669 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002670 {
2671 MediaExpectations media_expectations;
2672 media_expectations.CalleeExpectsSomeAudio(25);
2673 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2674 }
deadbeef4e2deab2017-09-20 13:56:21 -07002675
2676 report = callee()->NewGetStats();
2677 ASSERT_NE(nullptr, report);
2678 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2679 ASSERT_EQ(1U, track_stats.size());
2680 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2681 // The "total samples received" stat should only be greater than it was
2682 // before.
2683 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2684 // Right now, the new SSRC will cause the counters to reset to 0.
2685 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2686
2687 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002688 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002689 // good sign that we're seeing stats from the old stream that's no longer
2690 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002691 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002692 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2693 EXPECT_LT(*track_stats[0]->concealed_samples,
2694 *track_stats[0]->total_samples_received *
2695 kAcceptableConcealedSamplesPercentage);
2696
2697 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2698 // sanity check that the SSRC really changed.
2699 // TODO(deadbeef): This isn't working right now, because we're not returning
2700 // *any* stats for the inactive stream. Uncomment when the bug is completely
2701 // fixed.
2702 // auto inbound_stream_stats =
2703 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2704 // ASSERT_EQ(2U, inbound_stream_stats.size());
2705}
2706
deadbeef1dcb1642017-03-29 21:08:16 -07002707// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002708TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002709 PeerConnectionFactory::Options dtls_10_options;
2710 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2711 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2712 dtls_10_options));
2713 ConnectFakeSignaling();
2714 // Do normal offer/answer and wait for some frames to be received in each
2715 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002716 caller()->AddAudioVideoTracks();
2717 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002718 caller()->CreateAndSetAndSignalOffer();
2719 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002720 MediaExpectations media_expectations;
2721 media_expectations.ExpectBidirectionalAudioAndVideo();
2722 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002723}
2724
2725// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002726TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002727 PeerConnectionFactory::Options dtls_10_options;
2728 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2729 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2730 dtls_10_options));
2731 ConnectFakeSignaling();
2732 // Register UMA observer before signaling begins.
2733 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2734 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2735 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002736 caller()->AddAudioVideoTracks();
2737 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002738 caller()->CreateAndSetAndSignalOffer();
2739 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2740 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002741 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002742 kDefaultTimeout);
2743 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002744 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002745 EXPECT_EQ(1,
2746 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2747 kDefaultSrtpCryptoSuite));
2748}
2749
2750// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002751TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002752 PeerConnectionFactory::Options dtls_12_options;
2753 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2754 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2755 dtls_12_options));
2756 ConnectFakeSignaling();
2757 // Register UMA observer before signaling begins.
2758 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2759 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2760 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002761 caller()->AddAudioVideoTracks();
2762 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002763 caller()->CreateAndSetAndSignalOffer();
2764 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2765 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002766 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002767 kDefaultTimeout);
2768 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002769 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002770 EXPECT_EQ(1,
2771 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2772 kDefaultSrtpCryptoSuite));
2773}
2774
2775// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2776// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002777TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002778 PeerConnectionFactory::Options caller_options;
2779 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2780 PeerConnectionFactory::Options callee_options;
2781 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2782 ASSERT_TRUE(
2783 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2784 ConnectFakeSignaling();
2785 // Do normal offer/answer and wait for some frames to be received in each
2786 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002787 caller()->AddAudioVideoTracks();
2788 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002789 caller()->CreateAndSetAndSignalOffer();
2790 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002791 MediaExpectations media_expectations;
2792 media_expectations.ExpectBidirectionalAudioAndVideo();
2793 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002794}
2795
2796// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2797// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002798TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002799 PeerConnectionFactory::Options caller_options;
2800 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2801 PeerConnectionFactory::Options callee_options;
2802 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2803 ASSERT_TRUE(
2804 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2805 ConnectFakeSignaling();
2806 // Do normal offer/answer and wait for some frames to be received in each
2807 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002808 caller()->AddAudioVideoTracks();
2809 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002810 caller()->CreateAndSetAndSignalOffer();
2811 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002812 MediaExpectations media_expectations;
2813 media_expectations.ExpectBidirectionalAudioAndVideo();
2814 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002815}
2816
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002817// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2818// works as expected; the cipher should only be used if enabled by both sides.
2819TEST_P(PeerConnectionIntegrationTest,
2820 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
2821 PeerConnectionFactory::Options caller_options;
2822 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2823 PeerConnectionFactory::Options callee_options;
2824 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2825 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2826 TestNegotiatedCipherSuite(caller_options, callee_options,
2827 expected_cipher_suite);
2828}
2829
2830TEST_P(PeerConnectionIntegrationTest,
2831 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2832 PeerConnectionFactory::Options caller_options;
2833 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2834 PeerConnectionFactory::Options callee_options;
2835 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2836 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2837 TestNegotiatedCipherSuite(caller_options, callee_options,
2838 expected_cipher_suite);
2839}
2840
2841TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2842 PeerConnectionFactory::Options caller_options;
2843 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2844 PeerConnectionFactory::Options callee_options;
2845 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2846 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
2847 TestNegotiatedCipherSuite(caller_options, callee_options,
2848 expected_cipher_suite);
2849}
2850
deadbeef1dcb1642017-03-29 21:08:16 -07002851// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002852TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002853 bool local_gcm_enabled = false;
2854 bool remote_gcm_enabled = false;
2855 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2856 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2857 expected_cipher_suite);
2858}
2859
2860// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002861TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002862 bool local_gcm_enabled = true;
2863 bool remote_gcm_enabled = true;
2864 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2865 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2866 expected_cipher_suite);
2867}
2868
2869// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002870TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002871 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2872 bool local_gcm_enabled = true;
2873 bool remote_gcm_enabled = false;
2874 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2875 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2876 expected_cipher_suite);
2877}
2878
2879// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002880TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002881 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2882 bool local_gcm_enabled = false;
2883 bool remote_gcm_enabled = true;
2884 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2885 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2886 expected_cipher_suite);
2887}
2888
deadbeef7914b8c2017-04-21 03:23:33 -07002889// Verify that media can be transmitted end-to-end when GCM crypto suites are
2890// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2891// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2892// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002893TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07002894 PeerConnectionFactory::Options gcm_options;
2895 gcm_options.crypto_options.enable_gcm_crypto_suites = true;
2896 ASSERT_TRUE(
2897 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2898 ConnectFakeSignaling();
2899 // Do normal offer/answer and wait for some frames to be received in each
2900 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002901 caller()->AddAudioVideoTracks();
2902 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002903 caller()->CreateAndSetAndSignalOffer();
2904 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002905 MediaExpectations media_expectations;
2906 media_expectations.ExpectBidirectionalAudioAndVideo();
2907 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07002908}
2909
deadbeef1dcb1642017-03-29 21:08:16 -07002910// This test sets up a call between two parties with audio, video and an RTP
2911// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002912TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002913 FakeConstraints setup_constraints;
2914 setup_constraints.SetAllowRtpDataChannels();
2915 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2916 &setup_constraints));
2917 ConnectFakeSignaling();
2918 // Expect that data channel created on caller side will show up for callee as
2919 // well.
2920 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002921 caller()->AddAudioVideoTracks();
2922 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002923 caller()->CreateAndSetAndSignalOffer();
2924 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2925 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002926 MediaExpectations media_expectations;
2927 media_expectations.ExpectBidirectionalAudioAndVideo();
2928 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002929 ASSERT_NE(nullptr, caller()->data_channel());
2930 ASSERT_NE(nullptr, callee()->data_channel());
2931 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2932 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2933
2934 // Ensure data can be sent in both directions.
2935 std::string data = "hello world";
2936 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2937 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2938 kDefaultTimeout);
2939 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2940 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2941 kDefaultTimeout);
2942}
2943
2944// Ensure that an RTP data channel is signaled as closed for the caller when
2945// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002946TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002947 RtpDataChannelSignaledClosedInCalleeOffer) {
2948 // Same procedure as above test.
2949 FakeConstraints setup_constraints;
2950 setup_constraints.SetAllowRtpDataChannels();
2951 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2952 &setup_constraints));
2953 ConnectFakeSignaling();
2954 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002955 caller()->AddAudioVideoTracks();
2956 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002957 caller()->CreateAndSetAndSignalOffer();
2958 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2959 ASSERT_NE(nullptr, caller()->data_channel());
2960 ASSERT_NE(nullptr, callee()->data_channel());
2961 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2962 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2963
2964 // Close the data channel on the callee, and do an updated offer/answer.
2965 callee()->data_channel()->Close();
2966 callee()->CreateAndSetAndSignalOffer();
2967 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2968 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2969 EXPECT_FALSE(callee()->data_observer()->IsOpen());
2970}
2971
2972// Tests that data is buffered in an RTP data channel until an observer is
2973// registered for it.
2974//
2975// NOTE: RTP data channels can receive data before the underlying
2976// transport has detected that a channel is writable and thus data can be
2977// received before the data channel state changes to open. That is hard to test
2978// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002979TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002980 DataBufferedUntilRtpDataChannelObserverRegistered) {
2981 // Use fake clock and simulated network delay so that we predictably can wait
2982 // until an SCTP message has been delivered without "sleep()"ing.
2983 rtc::ScopedFakeClock fake_clock;
2984 // Some things use a time of "0" as a special value, so we need to start out
2985 // the fake clock at a nonzero time.
2986 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02002987 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07002988 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
2989 virtual_socket_server()->UpdateDelayDistribution();
2990
2991 FakeConstraints constraints;
2992 constraints.SetAllowRtpDataChannels();
2993 ASSERT_TRUE(
2994 CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints));
2995 ConnectFakeSignaling();
2996 caller()->CreateDataChannel();
2997 caller()->CreateAndSetAndSignalOffer();
2998 ASSERT_TRUE(caller()->data_channel() != nullptr);
2999 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
3000 kDefaultTimeout, fake_clock);
3001 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
3002 kDefaultTimeout, fake_clock);
3003 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3004 callee()->data_channel()->state(), kDefaultTimeout,
3005 fake_clock);
3006
3007 // Unregister the observer which is normally automatically registered.
3008 callee()->data_channel()->UnregisterObserver();
3009 // Send data and advance fake clock until it should have been received.
3010 std::string data = "hello world";
3011 caller()->data_channel()->Send(DataBuffer(data));
3012 SIMULATED_WAIT(false, 50, fake_clock);
3013
3014 // Attach data channel and expect data to be received immediately. Note that
3015 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3016 // further, but data can be received even if the callback is asynchronous.
3017 MockDataChannelObserver new_observer(callee()->data_channel());
3018 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
3019 fake_clock);
3020}
3021
3022// This test sets up a call between two parties with audio, video and but only
3023// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003024TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07003025 FakeConstraints setup_constraints_1;
3026 setup_constraints_1.SetAllowRtpDataChannels();
3027 // Must disable DTLS to make negotiation succeed.
3028 setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
3029 false);
3030 FakeConstraints setup_constraints_2;
3031 setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
3032 false);
3033 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(
3034 &setup_constraints_1, &setup_constraints_2));
3035 ConnectFakeSignaling();
3036 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003037 caller()->AddAudioVideoTracks();
3038 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003039 caller()->CreateAndSetAndSignalOffer();
3040 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3041 // The caller should still have a data channel, but it should be closed, and
3042 // one should ever have been created for the callee.
3043 EXPECT_TRUE(caller()->data_channel() != nullptr);
3044 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3045 EXPECT_EQ(nullptr, callee()->data_channel());
3046}
3047
3048// This test sets up a call between two parties with audio, and video. When
3049// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003050TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003051 FakeConstraints setup_constraints;
3052 setup_constraints.SetAllowRtpDataChannels();
3053 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
3054 &setup_constraints));
3055 ConnectFakeSignaling();
3056 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003057 caller()->AddAudioVideoTracks();
3058 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003059 caller()->CreateAndSetAndSignalOffer();
3060 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3061 // Create data channel and do new offer and answer.
3062 caller()->CreateDataChannel();
3063 caller()->CreateAndSetAndSignalOffer();
3064 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3065 ASSERT_NE(nullptr, caller()->data_channel());
3066 ASSERT_NE(nullptr, callee()->data_channel());
3067 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3068 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3069 // Ensure data can be sent in both directions.
3070 std::string data = "hello world";
3071 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3072 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3073 kDefaultTimeout);
3074 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3075 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3076 kDefaultTimeout);
3077}
3078
3079#ifdef HAVE_SCTP
3080
3081// This test sets up a call between two parties with audio, video and an SCTP
3082// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003083TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003084 ASSERT_TRUE(CreatePeerConnectionWrappers());
3085 ConnectFakeSignaling();
3086 // Expect that data channel created on caller side will show up for callee as
3087 // well.
3088 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003089 caller()->AddAudioVideoTracks();
3090 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003091 caller()->CreateAndSetAndSignalOffer();
3092 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3093 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003094 MediaExpectations media_expectations;
3095 media_expectations.ExpectBidirectionalAudioAndVideo();
3096 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003097 // Caller data channel should already exist (it created one). Callee data
3098 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3099 ASSERT_NE(nullptr, caller()->data_channel());
3100 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3101 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3102 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3103
3104 // Ensure data can be sent in both directions.
3105 std::string data = "hello world";
3106 caller()->data_channel()->Send(DataBuffer(data));
3107 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3108 kDefaultTimeout);
3109 callee()->data_channel()->Send(DataBuffer(data));
3110 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3111 kDefaultTimeout);
3112}
3113
3114// Ensure that when the callee closes an SCTP data channel, the closing
3115// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003116TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003117 // Same procedure as above test.
3118 ASSERT_TRUE(CreatePeerConnectionWrappers());
3119 ConnectFakeSignaling();
3120 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003121 caller()->AddAudioVideoTracks();
3122 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003123 caller()->CreateAndSetAndSignalOffer();
3124 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3125 ASSERT_NE(nullptr, caller()->data_channel());
3126 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3127 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3128 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3129
3130 // Close the data channel on the callee side, and wait for it to reach the
3131 // "closed" state on both sides.
3132 callee()->data_channel()->Close();
3133 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3134 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3135}
3136
Seth Hampson2f0d7022018-02-20 11:54:42 -08003137TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003138 ASSERT_TRUE(CreatePeerConnectionWrappers());
3139 ConnectFakeSignaling();
3140 webrtc::DataChannelInit init;
3141 init.id = 53;
3142 init.maxRetransmits = 52;
3143 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003144 caller()->AddAudioVideoTracks();
3145 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003146 caller()->CreateAndSetAndSignalOffer();
3147 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003148 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3149 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07003150 EXPECT_EQ(init.id, callee()->data_channel()->id());
3151 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3152 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3153 EXPECT_FALSE(callee()->data_channel()->negotiated());
3154}
3155
deadbeef1dcb1642017-03-29 21:08:16 -07003156// Test usrsctp's ability to process unordered data stream, where data actually
3157// arrives out of order using simulated delays. Previously there have been some
3158// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003159TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003160 // Introduce random network delays.
3161 // Otherwise it's not a true "unordered" test.
3162 virtual_socket_server()->set_delay_mean(20);
3163 virtual_socket_server()->set_delay_stddev(5);
3164 virtual_socket_server()->UpdateDelayDistribution();
3165 // Normal procedure, but with unordered data channel config.
3166 ASSERT_TRUE(CreatePeerConnectionWrappers());
3167 ConnectFakeSignaling();
3168 webrtc::DataChannelInit init;
3169 init.ordered = false;
3170 caller()->CreateDataChannel(&init);
3171 caller()->CreateAndSetAndSignalOffer();
3172 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3173 ASSERT_NE(nullptr, caller()->data_channel());
3174 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3175 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3176 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3177
3178 static constexpr int kNumMessages = 100;
3179 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3180 static constexpr size_t kMaxMessageSize = 4096;
3181 // Create and send random messages.
3182 std::vector<std::string> sent_messages;
3183 for (int i = 0; i < kNumMessages; ++i) {
3184 size_t length =
3185 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3186 std::string message;
3187 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3188 caller()->data_channel()->Send(DataBuffer(message));
3189 callee()->data_channel()->Send(DataBuffer(message));
3190 sent_messages.push_back(message);
3191 }
3192
3193 // Wait for all messages to be received.
3194 EXPECT_EQ_WAIT(kNumMessages,
3195 caller()->data_observer()->received_message_count(),
3196 kDefaultTimeout);
3197 EXPECT_EQ_WAIT(kNumMessages,
3198 callee()->data_observer()->received_message_count(),
3199 kDefaultTimeout);
3200
3201 // Sort and compare to make sure none of the messages were corrupted.
3202 std::vector<std::string> caller_received_messages =
3203 caller()->data_observer()->messages();
3204 std::vector<std::string> callee_received_messages =
3205 callee()->data_observer()->messages();
3206 std::sort(sent_messages.begin(), sent_messages.end());
3207 std::sort(caller_received_messages.begin(), caller_received_messages.end());
3208 std::sort(callee_received_messages.begin(), callee_received_messages.end());
3209 EXPECT_EQ(sent_messages, caller_received_messages);
3210 EXPECT_EQ(sent_messages, callee_received_messages);
3211}
3212
3213// This test sets up a call between two parties with audio, and video. When
3214// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003215TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003216 ASSERT_TRUE(CreatePeerConnectionWrappers());
3217 ConnectFakeSignaling();
3218 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003219 caller()->AddAudioVideoTracks();
3220 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003221 caller()->CreateAndSetAndSignalOffer();
3222 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3223 // Create data channel and do new offer and answer.
3224 caller()->CreateDataChannel();
3225 caller()->CreateAndSetAndSignalOffer();
3226 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3227 // Caller data channel should already exist (it created one). Callee data
3228 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3229 ASSERT_NE(nullptr, caller()->data_channel());
3230 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3231 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3232 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3233 // Ensure data can be sent in both directions.
3234 std::string data = "hello world";
3235 caller()->data_channel()->Send(DataBuffer(data));
3236 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3237 kDefaultTimeout);
3238 callee()->data_channel()->Send(DataBuffer(data));
3239 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3240 kDefaultTimeout);
3241}
3242
deadbeef7914b8c2017-04-21 03:23:33 -07003243// Set up a connection initially just using SCTP data channels, later upgrading
3244// to audio/video, ensuring frames are received end-to-end. Effectively the
3245// inverse of the test above.
3246// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003247TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003248 ASSERT_TRUE(CreatePeerConnectionWrappers());
3249 ConnectFakeSignaling();
3250 // Do initial offer/answer with just data channel.
3251 caller()->CreateDataChannel();
3252 caller()->CreateAndSetAndSignalOffer();
3253 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3254 // Wait until data can be sent over the data channel.
3255 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3256 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3257 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3258
3259 // Do subsequent offer/answer with two-way audio and video. Audio and video
3260 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003261 caller()->AddAudioVideoTracks();
3262 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003263 caller()->CreateAndSetAndSignalOffer();
3264 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003265 MediaExpectations media_expectations;
3266 media_expectations.ExpectBidirectionalAudioAndVideo();
3267 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003268}
3269
deadbeef8b7e9ad2017-05-25 09:38:55 -07003270static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003271 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003272 GetFirstDataContentDescription(desc);
3273 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003274 dcd_offer->set_use_sctpmap(false);
3275 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3276}
3277
3278// Test that the data channel works when a spec-compliant SCTP m= section is
3279// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3280// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003281TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003282 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3283 ASSERT_TRUE(CreatePeerConnectionWrappers());
3284 ConnectFakeSignaling();
3285 caller()->CreateDataChannel();
3286 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3287 caller()->CreateAndSetAndSignalOffer();
3288 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3289 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3290 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3291 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3292
3293 // Ensure data can be sent in both directions.
3294 std::string data = "hello world";
3295 caller()->data_channel()->Send(DataBuffer(data));
3296 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3297 kDefaultTimeout);
3298 callee()->data_channel()->Send(DataBuffer(data));
3299 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3300 kDefaultTimeout);
3301}
3302
deadbeef1dcb1642017-03-29 21:08:16 -07003303#endif // HAVE_SCTP
3304
3305// Test that the ICE connection and gathering states eventually reach
3306// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003307TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003308 ASSERT_TRUE(CreatePeerConnectionWrappers());
3309 ConnectFakeSignaling();
3310 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003311 caller()->AddAudioVideoTracks();
3312 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003313 caller()->CreateAndSetAndSignalOffer();
3314 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3315 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3316 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3317 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3318 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3319 // After the best candidate pair is selected and all candidates are signaled,
3320 // the ICE connection state should reach "complete".
3321 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3322 // answerer/"callee" by default) only reaches "connected". When this is
3323 // fixed, this test should be updated.
3324 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3325 caller()->ice_connection_state(), kDefaultTimeout);
3326 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3327 callee()->ice_connection_state(), kDefaultTimeout);
3328}
3329
Steve Antonede9ca52017-10-16 13:04:27 -07003330// Test that firewalling the ICE connection causes the clients to identify the
3331// disconnected state and then removing the firewall causes them to reconnect.
3332class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003333 : public PeerConnectionIntegrationBaseTest,
3334 public ::testing::WithParamInterface<
3335 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003336 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003337 PeerConnectionIntegrationIceStatesTest()
3338 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3339 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003340 }
3341
3342 void StartStunServer(const SocketAddress& server_address) {
3343 stun_server_.reset(
3344 cricket::TestStunServer::Create(network_thread(), server_address));
3345 }
3346
3347 bool TestIPv6() {
3348 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3349 }
3350
3351 void SetPortAllocatorFlags() {
Qingsi Wanga2d60672018-04-11 16:57:45 -07003352 network_thread()->Invoke<void>(
3353 RTC_FROM_HERE,
3354 rtc::Bind(&cricket::PortAllocator::set_flags,
3355 caller()->port_allocator(), port_allocator_flags_));
3356 network_thread()->Invoke<void>(
3357 RTC_FROM_HERE,
3358 rtc::Bind(&cricket::PortAllocator::set_flags,
3359 callee()->port_allocator(), port_allocator_flags_));
Steve Antonede9ca52017-10-16 13:04:27 -07003360 }
3361
3362 std::vector<SocketAddress> CallerAddresses() {
3363 std::vector<SocketAddress> addresses;
3364 addresses.push_back(SocketAddress("1.1.1.1", 0));
3365 if (TestIPv6()) {
3366 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3367 }
3368 return addresses;
3369 }
3370
3371 std::vector<SocketAddress> CalleeAddresses() {
3372 std::vector<SocketAddress> addresses;
3373 addresses.push_back(SocketAddress("2.2.2.2", 0));
3374 if (TestIPv6()) {
3375 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3376 }
3377 return addresses;
3378 }
3379
3380 void SetUpNetworkInterfaces() {
3381 // Remove the default interfaces added by the test infrastructure.
3382 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3383 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3384
3385 // Add network addresses for test.
3386 for (const auto& caller_address : CallerAddresses()) {
3387 caller()->network()->AddInterface(caller_address);
3388 }
3389 for (const auto& callee_address : CalleeAddresses()) {
3390 callee()->network()->AddInterface(callee_address);
3391 }
3392 }
3393
3394 private:
3395 uint32_t port_allocator_flags_;
3396 std::unique_ptr<cricket::TestStunServer> stun_server_;
3397};
3398
3399// Tests that the PeerConnection goes through all the ICE gathering/connection
3400// states over the duration of the call. This includes Disconnected and Failed
3401// states, induced by putting a firewall between the peers and waiting for them
3402// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003403TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3404 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3405 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3406 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003407
3408 const SocketAddress kStunServerAddress =
3409 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3410 StartStunServer(kStunServerAddress);
3411
3412 PeerConnectionInterface::RTCConfiguration config;
3413 PeerConnectionInterface::IceServer ice_stun_server;
3414 ice_stun_server.urls.push_back(
3415 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3416 kStunServerAddress.PortAsString());
3417 config.servers.push_back(ice_stun_server);
3418
3419 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3420 ConnectFakeSignaling();
3421 SetPortAllocatorFlags();
3422 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003423 caller()->AddAudioVideoTracks();
3424 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003425
3426 // Initial state before anything happens.
3427 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3428 caller()->ice_gathering_state());
3429 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3430 caller()->ice_connection_state());
3431
3432 // Start the call by creating the offer, setting it as the local description,
3433 // then sending it to the peer who will respond with an answer. This happens
3434 // asynchronously so that we can watch the states as it runs in the
3435 // background.
3436 caller()->CreateAndSetAndSignalOffer();
3437
Steve Anton83119dd2017-11-10 16:19:52 -08003438 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3439 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003440
3441 // Verify that the observer was notified of the intermediate transitions.
3442 EXPECT_THAT(caller()->ice_connection_state_history(),
3443 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3444 PeerConnectionInterface::kIceConnectionConnected,
3445 PeerConnectionInterface::kIceConnectionCompleted));
3446 EXPECT_THAT(caller()->ice_gathering_state_history(),
3447 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3448 PeerConnectionInterface::kIceGatheringComplete));
3449
3450 // Block connections to/from the caller and wait for ICE to become
3451 // disconnected.
3452 for (const auto& caller_address : CallerAddresses()) {
3453 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3454 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003455 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003456 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3457 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003458
3459 // Let ICE re-establish by removing the firewall rules.
3460 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003461 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003462 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3463 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003464
3465 // According to RFC7675, if there is no response within 30 seconds then the
3466 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003467 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003468 constexpr int kConsentTimeout = 30000;
3469 for (const auto& caller_address : CallerAddresses()) {
3470 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3471 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003472 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003473 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3474 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003475}
3476
3477// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3478// and that the statistics in the metric observers are updated correctly.
3479TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3480 ASSERT_TRUE(CreatePeerConnectionWrappers());
3481 ConnectFakeSignaling();
3482 SetPortAllocatorFlags();
3483 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003484 caller()->AddAudioVideoTracks();
3485 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003486
3487 rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer(
3488 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>());
3489 caller()->pc()->RegisterUMAObserver(metrics_observer.get());
3490
3491 caller()->CreateAndSetAndSignalOffer();
3492
3493 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3494
3495 const int num_best_ipv4 = metrics_observer->GetEnumCounter(
3496 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv4);
3497 const int num_best_ipv6 = metrics_observer->GetEnumCounter(
3498 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv6);
3499 if (TestIPv6()) {
3500 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3501 // connection.
3502 EXPECT_EQ(0u, num_best_ipv4);
3503 EXPECT_EQ(1u, num_best_ipv6);
3504 } else {
3505 EXPECT_EQ(1u, num_best_ipv4);
3506 EXPECT_EQ(0u, num_best_ipv6);
3507 }
3508
3509 EXPECT_EQ(0u, metrics_observer->GetEnumCounter(
3510 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3511 webrtc::kIceCandidatePairHostHost));
3512 EXPECT_EQ(1u, metrics_observer->GetEnumCounter(
3513 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3514 webrtc::kIceCandidatePairHostPublicHostPublic));
3515}
3516
3517constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3518 cricket::PORTALLOCATOR_DISABLE_STUN |
3519 cricket::PORTALLOCATOR_DISABLE_RELAY;
3520constexpr uint32_t kFlagsIPv6NoStun =
3521 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3522 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3523constexpr uint32_t kFlagsIPv4Stun =
3524 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3525
Seth Hampson2f0d7022018-02-20 11:54:42 -08003526INSTANTIATE_TEST_CASE_P(
3527 PeerConnectionIntegrationTest,
3528 PeerConnectionIntegrationIceStatesTest,
3529 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3530 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3531 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3532 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003533
deadbeef1dcb1642017-03-29 21:08:16 -07003534// This test sets up a call between two parties with audio and video.
3535// During the call, the caller restarts ICE and the test verifies that
3536// new ICE candidates are generated and audio and video still can flow, and the
3537// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003538TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003539 ASSERT_TRUE(CreatePeerConnectionWrappers());
3540 ConnectFakeSignaling();
3541 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003542 caller()->AddAudioVideoTracks();
3543 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003544 caller()->CreateAndSetAndSignalOffer();
3545 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3546 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3547 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3548 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3549 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3550
3551 // To verify that the ICE restart actually occurs, get
3552 // ufrag/password/candidates before and after restart.
3553 // Create an SDP string of the first audio candidate for both clients.
3554 const webrtc::IceCandidateCollection* audio_candidates_caller =
3555 caller()->pc()->local_description()->candidates(0);
3556 const webrtc::IceCandidateCollection* audio_candidates_callee =
3557 callee()->pc()->local_description()->candidates(0);
3558 ASSERT_GT(audio_candidates_caller->count(), 0u);
3559 ASSERT_GT(audio_candidates_callee->count(), 0u);
3560 std::string caller_candidate_pre_restart;
3561 ASSERT_TRUE(
3562 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3563 std::string callee_candidate_pre_restart;
3564 ASSERT_TRUE(
3565 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3566 const cricket::SessionDescription* desc =
3567 caller()->pc()->local_description()->description();
3568 std::string caller_ufrag_pre_restart =
3569 desc->transport_infos()[0].description.ice_ufrag;
3570 desc = callee()->pc()->local_description()->description();
3571 std::string callee_ufrag_pre_restart =
3572 desc->transport_infos()[0].description.ice_ufrag;
3573
3574 // Have the caller initiate an ICE restart.
3575 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3576 caller()->CreateAndSetAndSignalOffer();
3577 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3578 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3579 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3580 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3581 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3582
3583 // Grab the ufrags/candidates again.
3584 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3585 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3586 ASSERT_GT(audio_candidates_caller->count(), 0u);
3587 ASSERT_GT(audio_candidates_callee->count(), 0u);
3588 std::string caller_candidate_post_restart;
3589 ASSERT_TRUE(
3590 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3591 std::string callee_candidate_post_restart;
3592 ASSERT_TRUE(
3593 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3594 desc = caller()->pc()->local_description()->description();
3595 std::string caller_ufrag_post_restart =
3596 desc->transport_infos()[0].description.ice_ufrag;
3597 desc = callee()->pc()->local_description()->description();
3598 std::string callee_ufrag_post_restart =
3599 desc->transport_infos()[0].description.ice_ufrag;
3600 // Sanity check that an ICE restart was actually negotiated in SDP.
3601 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3602 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3603 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3604 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3605
3606 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003607 MediaExpectations media_expectations;
3608 media_expectations.ExpectBidirectionalAudioAndVideo();
3609 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003610}
3611
3612// Verify that audio/video can be received end-to-end when ICE renomination is
3613// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003614TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07003615 PeerConnectionInterface::RTCConfiguration config;
3616 config.enable_ice_renomination = true;
3617 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3618 ConnectFakeSignaling();
3619 // Do normal offer/answer and wait for some frames to be received in each
3620 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003621 caller()->AddAudioVideoTracks();
3622 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003623 caller()->CreateAndSetAndSignalOffer();
3624 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3625 // Sanity check that ICE renomination was actually negotiated.
3626 const cricket::SessionDescription* desc =
3627 caller()->pc()->local_description()->description();
3628 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003629 ASSERT_NE(
3630 info.description.transport_options.end(),
3631 std::find(info.description.transport_options.begin(),
3632 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003633 }
3634 desc = callee()->pc()->local_description()->description();
3635 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003636 ASSERT_NE(
3637 info.description.transport_options.end(),
3638 std::find(info.description.transport_options.begin(),
3639 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003640 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08003641 MediaExpectations media_expectations;
3642 media_expectations.ExpectBidirectionalAudioAndVideo();
3643 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003644}
3645
Steve Anton6f25b092017-10-23 09:39:20 -07003646// With a max bundle policy and RTCP muxing, adding a new media description to
3647// the connection should not affect ICE at all because the new media will use
3648// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003649TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003650 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07003651 PeerConnectionInterface::RTCConfiguration config;
3652 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3653 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3654 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3655 config, PeerConnectionInterface::RTCConfiguration()));
3656 ConnectFakeSignaling();
3657
Steve Anton15324772018-01-16 10:26:49 -08003658 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003659 caller()->CreateAndSetAndSignalOffer();
3660 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07003661 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3662 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07003663
3664 caller()->clear_ice_connection_state_history();
3665
Steve Anton15324772018-01-16 10:26:49 -08003666 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003667 caller()->CreateAndSetAndSignalOffer();
3668 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3669
3670 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
3671}
3672
deadbeef1dcb1642017-03-29 21:08:16 -07003673// This test sets up a call between two parties with audio and video. It then
3674// renegotiates setting the video m-line to "port 0", then later renegotiates
3675// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003676TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003677 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
3678 ASSERT_TRUE(CreatePeerConnectionWrappers());
3679 ConnectFakeSignaling();
3680
3681 // Do initial negotiation, only sending media from the caller. Will result in
3682 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08003683 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003684 caller()->CreateAndSetAndSignalOffer();
3685 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3686
3687 // Negotiate again, disabling the video "m=" section (the callee will set the
3688 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003689 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3690 PeerConnectionInterface::RTCOfferAnswerOptions options;
3691 options.offer_to_receive_video = 0;
3692 callee()->SetOfferAnswerOptions(options);
3693 } else {
3694 callee()->SetRemoteOfferHandler([this] {
3695 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
3696 });
3697 }
deadbeef1dcb1642017-03-29 21:08:16 -07003698 caller()->CreateAndSetAndSignalOffer();
3699 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3700 // Sanity check that video "m=" section was actually rejected.
3701 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
3702 callee()->pc()->local_description()->description());
3703 ASSERT_NE(nullptr, answer_video_content);
3704 ASSERT_TRUE(answer_video_content->rejected);
3705
3706 // Enable video and do negotiation again, making sure video is received
3707 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003708 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3709 PeerConnectionInterface::RTCOfferAnswerOptions options;
3710 options.offer_to_receive_video = 1;
3711 callee()->SetOfferAnswerOptions(options);
3712 } else {
3713 // The caller's transceiver is stopped, so we need to add another track.
3714 auto caller_transceiver =
3715 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
3716 EXPECT_TRUE(caller_transceiver->stopped());
3717 caller()->AddVideoTrack();
3718 }
3719 callee()->AddVideoTrack();
3720 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07003721 caller()->CreateAndSetAndSignalOffer();
3722 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003723
deadbeef1dcb1642017-03-29 21:08:16 -07003724 // Verify the caller receives frames from the newly added stream, and the
3725 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003726 MediaExpectations media_expectations;
3727 media_expectations.CalleeExpectsSomeAudio();
3728 media_expectations.ExpectBidirectionalVideo();
3729 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003730}
3731
deadbeef1dcb1642017-03-29 21:08:16 -07003732// This tests that if we negotiate after calling CreateSender but before we
3733// have a track, then set a track later, frames from the newly-set track are
3734// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003735TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07003736 MediaFlowsAfterEarlyWarmupWithCreateSender) {
3737 ASSERT_TRUE(CreatePeerConnectionWrappers());
3738 ConnectFakeSignaling();
3739 auto caller_audio_sender =
3740 caller()->pc()->CreateSender("audio", "caller_stream");
3741 auto caller_video_sender =
3742 caller()->pc()->CreateSender("video", "caller_stream");
3743 auto callee_audio_sender =
3744 callee()->pc()->CreateSender("audio", "callee_stream");
3745 auto callee_video_sender =
3746 callee()->pc()->CreateSender("video", "callee_stream");
3747 caller()->CreateAndSetAndSignalOffer();
3748 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3749 // Wait for ICE to complete, without any tracks being set.
3750 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3751 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3752 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3753 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3754 // Now set the tracks, and expect frames to immediately start flowing.
3755 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3756 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3757 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3758 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08003759 MediaExpectations media_expectations;
3760 media_expectations.ExpectBidirectionalAudioAndVideo();
3761 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3762}
3763
3764// This tests that if we negotiate after calling AddTransceiver but before we
3765// have a track, then set a track later, frames from the newly-set tracks are
3766// received end-to-end.
3767TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3768 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
3769 ASSERT_TRUE(CreatePeerConnectionWrappers());
3770 ConnectFakeSignaling();
3771 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3772 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
3773 auto caller_audio_sender = audio_result.MoveValue()->sender();
3774 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3775 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
3776 auto caller_video_sender = video_result.MoveValue()->sender();
3777 callee()->SetRemoteOfferHandler([this] {
3778 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
3779 callee()->pc()->GetTransceivers()[0]->SetDirection(
3780 RtpTransceiverDirection::kSendRecv);
3781 callee()->pc()->GetTransceivers()[1]->SetDirection(
3782 RtpTransceiverDirection::kSendRecv);
3783 });
3784 caller()->CreateAndSetAndSignalOffer();
3785 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3786 // Wait for ICE to complete, without any tracks being set.
3787 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3788 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3789 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3790 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3791 // Now set the tracks, and expect frames to immediately start flowing.
3792 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
3793 auto callee_video_sender = callee()->pc()->GetSenders()[1];
3794 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3795 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3796 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3797 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
3798 MediaExpectations media_expectations;
3799 media_expectations.ExpectBidirectionalAudioAndVideo();
3800 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003801}
3802
3803// This test verifies that a remote video track can be added via AddStream,
3804// and sent end-to-end. For this particular test, it's simply echoed back
3805// from the caller to the callee, rather than being forwarded to a third
3806// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003807TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07003808 ASSERT_TRUE(CreatePeerConnectionWrappers());
3809 ConnectFakeSignaling();
3810 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08003811 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003812 caller()->CreateAndSetAndSignalOffer();
3813 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3814 ASSERT_EQ(1, callee()->remote_streams()->count());
3815
3816 // Echo the stream back, and do a new offer/anwer (initiated by callee this
3817 // time).
3818 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
3819 callee()->CreateAndSetAndSignalOffer();
3820 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3821
Seth Hampson2f0d7022018-02-20 11:54:42 -08003822 MediaExpectations media_expectations;
3823 media_expectations.ExpectBidirectionalVideo();
3824 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003825}
3826
3827// Test that we achieve the expected end-to-end connection time, using a
3828// fake clock and simulated latency on the media and signaling paths.
3829// We use a TURN<->TURN connection because this is usually the quickest to
3830// set up initially, especially when we're confident the connection will work
3831// and can start sending media before we get a STUN response.
3832//
3833// With various optimizations enabled, here are the network delays we expect to
3834// be on the critical path:
3835// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
3836// signaling answer (with DTLS fingerprint).
3837// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
3838// using TURN<->TURN pair, and DTLS exchange is 4 packets,
3839// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003840TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07003841 rtc::ScopedFakeClock fake_clock;
3842 // Some things use a time of "0" as a special value, so we need to start out
3843 // the fake clock at a nonzero time.
3844 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003845 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003846
3847 static constexpr int media_hop_delay_ms = 50;
3848 static constexpr int signaling_trip_delay_ms = 500;
3849 // For explanation of these values, see comment above.
3850 static constexpr int required_media_hops = 9;
3851 static constexpr int required_signaling_trips = 2;
3852 // For internal delays (such as posting an event asychronously).
3853 static constexpr int allowed_internal_delay_ms = 20;
3854 static constexpr int total_connection_time_ms =
3855 media_hop_delay_ms * required_media_hops +
3856 signaling_trip_delay_ms * required_signaling_trips +
3857 allowed_internal_delay_ms;
3858
3859 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3860 3478};
3861 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3862 0};
3863 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3864 3478};
3865 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3866 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07003867 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
3868 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003869
Seth Hampsonaed71642018-06-11 07:41:32 -07003870 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
3871 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07003872 // Bypass permission check on received packets so media can be sent before
3873 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07003874 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
3875 turn_server_1->set_enable_permission_checks(false);
3876 });
3877 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
3878 turn_server_2->set_enable_permission_checks(false);
3879 });
deadbeef1dcb1642017-03-29 21:08:16 -07003880
3881 PeerConnectionInterface::RTCConfiguration client_1_config;
3882 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3883 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3884 ice_server_1.username = "test";
3885 ice_server_1.password = "test";
3886 client_1_config.servers.push_back(ice_server_1);
3887 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3888 client_1_config.presume_writable_when_fully_relayed = true;
3889
3890 PeerConnectionInterface::RTCConfiguration client_2_config;
3891 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3892 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3893 ice_server_2.username = "test";
3894 ice_server_2.password = "test";
3895 client_2_config.servers.push_back(ice_server_2);
3896 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3897 client_2_config.presume_writable_when_fully_relayed = true;
3898
3899 ASSERT_TRUE(
3900 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3901 // Set up the simulated delays.
3902 SetSignalingDelayMs(signaling_trip_delay_ms);
3903 ConnectFakeSignaling();
3904 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
3905 virtual_socket_server()->UpdateDelayDistribution();
3906
3907 // Set "offer to receive audio/video" without adding any tracks, so we just
3908 // set up ICE/DTLS with no media.
3909 PeerConnectionInterface::RTCOfferAnswerOptions options;
3910 options.offer_to_receive_audio = 1;
3911 options.offer_to_receive_video = 1;
3912 caller()->SetOfferAnswerOptions(options);
3913 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07003914 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
3915 fake_clock);
deadbeef1dcb1642017-03-29 21:08:16 -07003916}
3917
Jonas Orelandbdcee282017-10-10 14:01:40 +02003918// Verify that a TurnCustomizer passed in through RTCConfiguration
3919// is actually used by the underlying TURN candidate pair.
3920// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003921TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02003922 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3923 3478};
3924 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3925 0};
3926 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3927 3478};
3928 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3929 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07003930 CreateTurnServer(turn_server_1_internal_address,
3931 turn_server_1_external_address);
3932 CreateTurnServer(turn_server_2_internal_address,
3933 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003934
3935 PeerConnectionInterface::RTCConfiguration client_1_config;
3936 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3937 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3938 ice_server_1.username = "test";
3939 ice_server_1.password = "test";
3940 client_1_config.servers.push_back(ice_server_1);
3941 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07003942 auto* customizer1 = CreateTurnCustomizer();
3943 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02003944
3945 PeerConnectionInterface::RTCConfiguration client_2_config;
3946 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3947 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3948 ice_server_2.username = "test";
3949 ice_server_2.password = "test";
3950 client_2_config.servers.push_back(ice_server_2);
3951 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07003952 auto* customizer2 = CreateTurnCustomizer();
3953 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02003954
3955 ASSERT_TRUE(
3956 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3957 ConnectFakeSignaling();
3958
3959 // Set "offer to receive audio/video" without adding any tracks, so we just
3960 // set up ICE/DTLS with no media.
3961 PeerConnectionInterface::RTCOfferAnswerOptions options;
3962 options.offer_to_receive_audio = 1;
3963 options.offer_to_receive_video = 1;
3964 caller()->SetOfferAnswerOptions(options);
3965 caller()->CreateAndSetAndSignalOffer();
3966 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3967
Seth Hampsonaed71642018-06-11 07:41:32 -07003968 ExpectTurnCustomizerCountersIncremented(customizer1);
3969 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003970}
3971
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07003972// Verifies that you can use TCP instead of UDP to connect to a TURN server and
3973// send media between the caller and the callee.
3974TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
3975 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
3976 3478};
3977 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
3978
3979 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07003980 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
3981 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07003982
3983 webrtc::PeerConnectionInterface::IceServer ice_server;
3984 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
3985 ice_server.username = "test";
3986 ice_server.password = "test";
3987
3988 PeerConnectionInterface::RTCConfiguration client_1_config;
3989 client_1_config.servers.push_back(ice_server);
3990 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3991
3992 PeerConnectionInterface::RTCConfiguration client_2_config;
3993 client_2_config.servers.push_back(ice_server);
3994 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3995
3996 ASSERT_TRUE(
3997 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3998
3999 // Do normal offer/answer and wait for ICE to complete.
4000 ConnectFakeSignaling();
4001 caller()->AddAudioVideoTracks();
4002 callee()->AddAudioVideoTracks();
4003 caller()->CreateAndSetAndSignalOffer();
4004 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4005 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4006 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4007
4008 MediaExpectations media_expectations;
4009 media_expectations.ExpectBidirectionalAudioAndVideo();
4010 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4011}
4012
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004013// Verify that a SSLCertificateVerifier passed in through
4014// PeerConnectionDependencies is actually used by the underlying SSL
4015// implementation to determine whether a certificate presented by the TURN
4016// server is accepted by the client. Note that openssladapter_unittest.cc
4017// contains more detailed, lower-level tests.
4018TEST_P(PeerConnectionIntegrationTest,
4019 SSLCertificateVerifierUsedForTurnConnections) {
4020 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4021 3478};
4022 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4023
4024 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4025 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004026 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4027 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004028
4029 webrtc::PeerConnectionInterface::IceServer ice_server;
4030 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4031 ice_server.username = "test";
4032 ice_server.password = "test";
4033
4034 PeerConnectionInterface::RTCConfiguration client_1_config;
4035 client_1_config.servers.push_back(ice_server);
4036 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4037
4038 PeerConnectionInterface::RTCConfiguration client_2_config;
4039 client_2_config.servers.push_back(ice_server);
4040 // Setting the type to kRelay forces the connection to go through a TURN
4041 // server.
4042 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4043
4044 // Get a copy to the pointer so we can verify calls later.
4045 rtc::TestCertificateVerifier* client_1_cert_verifier =
4046 new rtc::TestCertificateVerifier();
4047 client_1_cert_verifier->verify_certificate_ = true;
4048 rtc::TestCertificateVerifier* client_2_cert_verifier =
4049 new rtc::TestCertificateVerifier();
4050 client_2_cert_verifier->verify_certificate_ = true;
4051
4052 // Create the dependencies with the test certificate verifier.
4053 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4054 client_1_deps.tls_cert_verifier =
4055 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4056 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4057 client_2_deps.tls_cert_verifier =
4058 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4059
4060 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4061 client_1_config, std::move(client_1_deps), client_2_config,
4062 std::move(client_2_deps)));
4063 ConnectFakeSignaling();
4064
4065 // Set "offer to receive audio/video" without adding any tracks, so we just
4066 // set up ICE/DTLS with no media.
4067 PeerConnectionInterface::RTCOfferAnswerOptions options;
4068 options.offer_to_receive_audio = 1;
4069 options.offer_to_receive_video = 1;
4070 caller()->SetOfferAnswerOptions(options);
4071 caller()->CreateAndSetAndSignalOffer();
4072 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4073
4074 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4075 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004076}
4077
4078TEST_P(PeerConnectionIntegrationTest,
4079 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4080 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4081 3478};
4082 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4083
4084 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4085 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004086 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4087 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004088
4089 webrtc::PeerConnectionInterface::IceServer ice_server;
4090 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4091 ice_server.username = "test";
4092 ice_server.password = "test";
4093
4094 PeerConnectionInterface::RTCConfiguration client_1_config;
4095 client_1_config.servers.push_back(ice_server);
4096 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4097
4098 PeerConnectionInterface::RTCConfiguration client_2_config;
4099 client_2_config.servers.push_back(ice_server);
4100 // Setting the type to kRelay forces the connection to go through a TURN
4101 // server.
4102 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4103
4104 // Get a copy to the pointer so we can verify calls later.
4105 rtc::TestCertificateVerifier* client_1_cert_verifier =
4106 new rtc::TestCertificateVerifier();
4107 client_1_cert_verifier->verify_certificate_ = false;
4108 rtc::TestCertificateVerifier* client_2_cert_verifier =
4109 new rtc::TestCertificateVerifier();
4110 client_2_cert_verifier->verify_certificate_ = false;
4111
4112 // Create the dependencies with the test certificate verifier.
4113 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4114 client_1_deps.tls_cert_verifier =
4115 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4116 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4117 client_2_deps.tls_cert_verifier =
4118 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4119
4120 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4121 client_1_config, std::move(client_1_deps), client_2_config,
4122 std::move(client_2_deps)));
4123 ConnectFakeSignaling();
4124
4125 // Set "offer to receive audio/video" without adding any tracks, so we just
4126 // set up ICE/DTLS with no media.
4127 PeerConnectionInterface::RTCOfferAnswerOptions options;
4128 options.offer_to_receive_audio = 1;
4129 options.offer_to_receive_video = 1;
4130 caller()->SetOfferAnswerOptions(options);
4131 caller()->CreateAndSetAndSignalOffer();
4132 bool wait_res = true;
4133 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4134 // properly, should be able to just wait for a state of "failed" instead of
4135 // waiting a fixed 10 seconds.
4136 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4137 ASSERT_FALSE(wait_res);
4138
4139 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4140 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004141}
4142
deadbeefc964d0b2017-04-03 10:03:35 -07004143// Test that audio and video flow end-to-end when codec names don't use the
4144// expected casing, given that they're supposed to be case insensitive. To test
4145// this, all but one codec is removed from each media description, and its
4146// casing is changed.
4147//
4148// In the past, this has regressed and caused crashes/black video, due to the
4149// fact that code at some layers was doing case-insensitive comparisons and
4150// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004151TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004152 ASSERT_TRUE(CreatePeerConnectionWrappers());
4153 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004154 caller()->AddAudioVideoTracks();
4155 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004156
4157 // Remove all but one audio/video codec (opus and VP8), and change the
4158 // casing of the caller's generated offer.
4159 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4160 cricket::AudioContentDescription* audio =
4161 GetFirstAudioContentDescription(description);
4162 ASSERT_NE(nullptr, audio);
4163 auto audio_codecs = audio->codecs();
4164 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4165 [](const cricket::AudioCodec& codec) {
4166 return codec.name != "opus";
4167 }),
4168 audio_codecs.end());
4169 ASSERT_EQ(1u, audio_codecs.size());
4170 audio_codecs[0].name = "OpUs";
4171 audio->set_codecs(audio_codecs);
4172
4173 cricket::VideoContentDescription* video =
4174 GetFirstVideoContentDescription(description);
4175 ASSERT_NE(nullptr, video);
4176 auto video_codecs = video->codecs();
4177 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4178 [](const cricket::VideoCodec& codec) {
4179 return codec.name != "VP8";
4180 }),
4181 video_codecs.end());
4182 ASSERT_EQ(1u, video_codecs.size());
4183 video_codecs[0].name = "vP8";
4184 video->set_codecs(video_codecs);
4185 });
4186
4187 caller()->CreateAndSetAndSignalOffer();
4188 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4189
4190 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004191 MediaExpectations media_expectations;
4192 media_expectations.ExpectBidirectionalAudioAndVideo();
4193 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004194}
4195
Seth Hampson2f0d7022018-02-20 11:54:42 -08004196TEST_P(PeerConnectionIntegrationTest, GetSources) {
hbos8d609f62017-04-10 07:39:05 -07004197 ASSERT_TRUE(CreatePeerConnectionWrappers());
4198 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004199 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004200 caller()->CreateAndSetAndSignalOffer();
4201 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004202 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004203 MediaExpectations media_expectations;
4204 media_expectations.CalleeExpectsSomeAudio(1);
4205 ASSERT_TRUE(ExpectNewFrames(media_expectations));
hbos8d609f62017-04-10 07:39:05 -07004206 ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u);
4207 auto receiver = callee()->pc()->GetReceivers()[0];
4208 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
4209
4210 auto contributing_sources = receiver->GetSources();
4211 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4212 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4213 contributing_sources[0].source_id());
4214}
4215
deadbeef2f425aa2017-04-14 10:41:32 -07004216// Test that if a track is removed and added again with a different stream ID,
4217// the new stream ID is successfully communicated in SDP and media continues to
4218// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004219// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4220// it will not reuse a transceiver that has already been sending. After creating
4221// a new transceiver it tries to create an offer with two senders of the same
4222// track ids and it fails.
4223TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004224 ASSERT_TRUE(CreatePeerConnectionWrappers());
4225 ConnectFakeSignaling();
4226
4227 rtc::scoped_refptr<MediaStreamInterface> stream_1 =
4228 caller()->pc_factory()->CreateLocalMediaStream("stream_1");
4229 rtc::scoped_refptr<MediaStreamInterface> stream_2 =
4230 caller()->pc_factory()->CreateLocalMediaStream("stream_2");
4231
4232 // Add track using stream 1, do offer/answer.
4233 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4234 caller()->CreateLocalAudioTrack();
4235 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
4236 caller()->pc()->AddTrack(track, {stream_1.get()});
4237 caller()->CreateAndSetAndSignalOffer();
4238 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004239 {
4240 MediaExpectations media_expectations;
4241 media_expectations.CalleeExpectsSomeAudio(1);
4242 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4243 }
deadbeef2f425aa2017-04-14 10:41:32 -07004244 // Remove the sender, and create a new one with the new stream.
4245 caller()->pc()->RemoveTrack(sender);
4246 sender = caller()->pc()->AddTrack(track, {stream_2.get()});
4247 caller()->CreateAndSetAndSignalOffer();
4248 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4249 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004250 {
4251 MediaExpectations media_expectations;
4252 media_expectations.CalleeExpectsSomeAudio();
4253 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4254 }
deadbeef2f425aa2017-04-14 10:41:32 -07004255}
4256
Seth Hampson2f0d7022018-02-20 11:54:42 -08004257TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004258 ASSERT_TRUE(CreatePeerConnectionWrappers());
4259 ConnectFakeSignaling();
4260
4261 auto output = rtc::MakeUnique<testing::NiceMock<MockRtcEventLogOutput>>();
4262 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
4263 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
4264 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004265 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4266 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004267
Steve Anton15324772018-01-16 10:26:49 -08004268 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004269 caller()->CreateAndSetAndSignalOffer();
4270 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4271}
4272
Steve Antonede9ca52017-10-16 13:04:27 -07004273// Test that if candidates are only signaled by applying full session
4274// descriptions (instead of using AddIceCandidate), the peers can connect to
4275// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004276TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004277 ASSERT_TRUE(CreatePeerConnectionWrappers());
4278 // Each side will signal the session descriptions but not candidates.
4279 ConnectFakeSignalingForSdpOnly();
4280
4281 // Add audio video track and exchange the initial offer/answer with media
4282 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004283 caller()->AddAudioVideoTracks();
4284 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004285 caller()->CreateAndSetAndSignalOffer();
4286
4287 // Wait for all candidates to be gathered on both the caller and callee.
4288 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4289 caller()->ice_gathering_state(), kDefaultTimeout);
4290 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4291 callee()->ice_gathering_state(), kDefaultTimeout);
4292
4293 // The candidates will now be included in the session description, so
4294 // signaling them will start the ICE connection.
4295 caller()->CreateAndSetAndSignalOffer();
4296 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4297
4298 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004299 MediaExpectations media_expectations;
4300 media_expectations.ExpectBidirectionalAudioAndVideo();
4301 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004302}
4303
henrika5f6bf242017-11-01 11:06:56 +01004304// Test that SetAudioPlayout can be used to disable audio playout from the
4305// start, then later enable it. This may be useful, for example, if the caller
4306// needs to play a local ringtone until some event occurs, after which it
4307// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004308TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004309 ASSERT_TRUE(CreatePeerConnectionWrappers());
4310 ConnectFakeSignaling();
4311
4312 // Set up audio-only call where audio playout is disabled on caller's side.
4313 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004314 caller()->AddAudioTrack();
4315 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004316 caller()->CreateAndSetAndSignalOffer();
4317 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4318
4319 // Pump messages for a second.
4320 WAIT(false, 1000);
4321 // Since audio playout is disabled, the caller shouldn't have received
4322 // anything (at the playout level, at least).
4323 EXPECT_EQ(0, caller()->audio_frames_received());
4324 // As a sanity check, make sure the callee (for which playout isn't disabled)
4325 // did still see frames on its audio level.
4326 ASSERT_GT(callee()->audio_frames_received(), 0);
4327
4328 // Enable playout again, and ensure audio starts flowing.
4329 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004330 MediaExpectations media_expectations;
4331 media_expectations.ExpectBidirectionalAudio();
4332 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004333}
4334
4335double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4336 auto report = pc->NewGetStats();
4337 auto track_stats_list =
4338 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4339 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4340 for (const auto* track_stats : track_stats_list) {
4341 if (track_stats->remote_source.is_defined() &&
4342 *track_stats->remote_source) {
4343 remote_track_stats = track_stats;
4344 break;
4345 }
4346 }
4347
4348 if (!remote_track_stats->total_audio_energy.is_defined()) {
4349 return 0.0;
4350 }
4351 return *remote_track_stats->total_audio_energy;
4352}
4353
4354// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4355// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004356TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004357 DisableAudioPlayoutStillGeneratesAudioStats) {
4358 ASSERT_TRUE(CreatePeerConnectionWrappers());
4359 ConnectFakeSignaling();
4360
4361 // Set up audio-only call where playout is disabled but audio-processing is
4362 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004363 caller()->AddAudioTrack();
4364 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004365 caller()->pc()->SetAudioPlayout(false);
4366
4367 caller()->CreateAndSetAndSignalOffer();
4368 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4369
4370 // Wait for the callee to receive audio stats.
4371 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4372}
4373
henrika4f167df2017-11-01 14:45:55 +01004374// Test that SetAudioRecording can be used to disable audio recording from the
4375// start, then later enable it. This may be useful, for example, if the caller
4376// wants to ensure that no audio resources are active before a certain state
4377// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004378TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004379 ASSERT_TRUE(CreatePeerConnectionWrappers());
4380 ConnectFakeSignaling();
4381
4382 // Set up audio-only call where audio recording is disabled on caller's side.
4383 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004384 caller()->AddAudioTrack();
4385 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004386 caller()->CreateAndSetAndSignalOffer();
4387 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4388
4389 // Pump messages for a second.
4390 WAIT(false, 1000);
4391 // Since caller has disabled audio recording, the callee shouldn't have
4392 // received anything.
4393 EXPECT_EQ(0, callee()->audio_frames_received());
4394 // As a sanity check, make sure the caller did still see frames on its
4395 // audio level since audio recording is enabled on the calle side.
4396 ASSERT_GT(caller()->audio_frames_received(), 0);
4397
4398 // Enable audio recording again, and ensure audio starts flowing.
4399 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004400 MediaExpectations media_expectations;
4401 media_expectations.ExpectBidirectionalAudio();
4402 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004403}
4404
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004405// Test that after closing PeerConnections, they stop sending any packets (ICE,
4406// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004407TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004408 // Set up audio/video/data, wait for some frames to be received.
4409 ASSERT_TRUE(CreatePeerConnectionWrappers());
4410 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004411 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004412#ifdef HAVE_SCTP
4413 caller()->CreateDataChannel();
4414#endif
4415 caller()->CreateAndSetAndSignalOffer();
4416 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004417 MediaExpectations media_expectations;
4418 media_expectations.CalleeExpectsSomeAudioAndVideo();
4419 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004420 // Close PeerConnections.
4421 caller()->pc()->Close();
4422 callee()->pc()->Close();
4423 // Pump messages for a second, and ensure no new packets end up sent.
4424 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4425 WAIT(false, 1000);
4426 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4427 EXPECT_EQ(sent_packets_a, sent_packets_b);
4428}
4429
Steve Anton7eca0932018-03-30 15:18:41 -07004430// Test that transport stats are generated by the RTCStatsCollector for a
4431// connection that only involves data channels. This is a regression test for
4432// crbug.com/826972.
4433#ifdef HAVE_SCTP
4434TEST_P(PeerConnectionIntegrationTest,
4435 TransportStatsReportedForDataChannelOnlyConnection) {
4436 ASSERT_TRUE(CreatePeerConnectionWrappers());
4437 ConnectFakeSignaling();
4438 caller()->CreateDataChannel();
4439
4440 caller()->CreateAndSetAndSignalOffer();
4441 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4442 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
4443
4444 auto caller_report = caller()->NewGetStats();
4445 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
4446 auto callee_report = callee()->NewGetStats();
4447 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
4448}
4449#endif // HAVE_SCTP
4450
Qingsi Wang7685e862018-06-11 20:15:46 -07004451TEST_P(PeerConnectionIntegrationTest,
4452 IceEventsGeneratedAndLoggedInRtcEventLog) {
4453 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
4454 ConnectFakeSignaling();
4455 PeerConnectionInterface::RTCOfferAnswerOptions options;
4456 options.offer_to_receive_audio = 1;
4457 caller()->SetOfferAnswerOptions(options);
4458 caller()->CreateAndSetAndSignalOffer();
4459 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4460 ASSERT_NE(nullptr, caller()->event_log_factory());
4461 ASSERT_NE(nullptr, callee()->event_log_factory());
4462 webrtc::FakeRtcEventLog* caller_event_log =
4463 static_cast<webrtc::FakeRtcEventLog*>(
4464 caller()->event_log_factory()->last_log_created());
4465 webrtc::FakeRtcEventLog* callee_event_log =
4466 static_cast<webrtc::FakeRtcEventLog*>(
4467 callee()->event_log_factory()->last_log_created());
4468 ASSERT_NE(nullptr, caller_event_log);
4469 ASSERT_NE(nullptr, callee_event_log);
4470 int caller_ice_config_count = caller_event_log->GetEventCount(
4471 webrtc::RtcEvent::Type::IceCandidatePairConfig);
4472 int caller_ice_event_count = caller_event_log->GetEventCount(
4473 webrtc::RtcEvent::Type::IceCandidatePairEvent);
4474 int callee_ice_config_count = callee_event_log->GetEventCount(
4475 webrtc::RtcEvent::Type::IceCandidatePairConfig);
4476 int callee_ice_event_count = callee_event_log->GetEventCount(
4477 webrtc::RtcEvent::Type::IceCandidatePairEvent);
4478 EXPECT_LT(0, caller_ice_config_count);
4479 EXPECT_LT(0, caller_ice_event_count);
4480 EXPECT_LT(0, callee_ice_config_count);
4481 EXPECT_LT(0, callee_ice_event_count);
4482}
4483
Seth Hampson2f0d7022018-02-20 11:54:42 -08004484INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4485 PeerConnectionIntegrationTest,
4486 Values(SdpSemantics::kPlanB,
4487 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004488
Steve Anton74255ff2018-01-24 18:32:57 -08004489// Tests that verify interoperability between Plan B and Unified Plan
4490// PeerConnections.
4491class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004492 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004493 public ::testing::WithParamInterface<
4494 std::tuple<SdpSemantics, SdpSemantics>> {
4495 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004496 // Setting the SdpSemantics for the base test to kDefault does not matter
4497 // because we specify not to use the test semantics when creating
4498 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004499 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07004500 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08004501 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004502 callee_semantics_(std::get<1>(GetParam())) {}
4503
4504 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07004505 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
4506 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08004507 }
4508
4509 const SdpSemantics caller_semantics_;
4510 const SdpSemantics callee_semantics_;
4511};
4512
4513TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4514 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4515 ConnectFakeSignaling();
4516
4517 caller()->CreateAndSetAndSignalOffer();
4518 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4519}
4520
4521TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4522 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4523 ConnectFakeSignaling();
4524 auto audio_sender = caller()->AddAudioTrack();
4525
4526 caller()->CreateAndSetAndSignalOffer();
4527 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4528
4529 // Verify that one audio receiver has been created on the remote and that it
4530 // has the same track ID as the sending track.
4531 auto receivers = callee()->pc()->GetReceivers();
4532 ASSERT_EQ(1u, receivers.size());
4533 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4534 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4535
Seth Hampson2f0d7022018-02-20 11:54:42 -08004536 MediaExpectations media_expectations;
4537 media_expectations.CalleeExpectsSomeAudio();
4538 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004539}
4540
4541TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4542 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4543 ConnectFakeSignaling();
4544 auto video_sender = caller()->AddVideoTrack();
4545 auto audio_sender = caller()->AddAudioTrack();
4546
4547 caller()->CreateAndSetAndSignalOffer();
4548 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4549
4550 // Verify that one audio and one video receiver have been created on the
4551 // remote and that they have the same track IDs as the sending tracks.
4552 auto audio_receivers =
4553 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4554 ASSERT_EQ(1u, audio_receivers.size());
4555 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4556 auto video_receivers =
4557 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4558 ASSERT_EQ(1u, video_receivers.size());
4559 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4560
Seth Hampson2f0d7022018-02-20 11:54:42 -08004561 MediaExpectations media_expectations;
4562 media_expectations.CalleeExpectsSomeAudioAndVideo();
4563 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004564}
4565
4566TEST_P(PeerConnectionIntegrationInteropTest,
4567 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4568 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4569 ConnectFakeSignaling();
4570 caller()->AddAudioVideoTracks();
4571 callee()->AddAudioVideoTracks();
4572
4573 caller()->CreateAndSetAndSignalOffer();
4574 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4575
Seth Hampson2f0d7022018-02-20 11:54:42 -08004576 MediaExpectations media_expectations;
4577 media_expectations.ExpectBidirectionalAudioAndVideo();
4578 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004579}
4580
4581TEST_P(PeerConnectionIntegrationInteropTest,
4582 ReverseRolesOneAudioLocalToOneVideoRemote) {
4583 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4584 ConnectFakeSignaling();
4585 caller()->AddAudioTrack();
4586 callee()->AddVideoTrack();
4587
4588 caller()->CreateAndSetAndSignalOffer();
4589 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4590
4591 // Verify that only the audio track has been negotiated.
4592 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
4593 // Might also check that the callee's NegotiationNeeded flag is set.
4594
4595 // Reverse roles.
4596 callee()->CreateAndSetAndSignalOffer();
4597 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4598
Seth Hampson2f0d7022018-02-20 11:54:42 -08004599 MediaExpectations media_expectations;
4600 media_expectations.CallerExpectsSomeVideo();
4601 media_expectations.CalleeExpectsSomeAudio();
4602 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004603}
4604
Harald Alvestrand8ebba742018-05-31 14:00:34 +02004605// Test getting the usage fingerprint for a simple test case.
4606TEST_P(PeerConnectionIntegrationTest, UsageFingerprintHistogram) {
4607 ASSERT_TRUE(CreatePeerConnectionWrappers());
4608 ConnectFakeSignaling();
4609 // Register UMA observer before signaling begins.
4610 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
4611 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
4612 caller()->pc()->RegisterUMAObserver(caller_observer);
4613 rtc::scoped_refptr<webrtc::FakeMetricsObserver> callee_observer =
4614 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
4615 callee()->pc()->RegisterUMAObserver(callee_observer);
4616 caller()->AddAudioTrack();
4617 caller()->AddVideoTrack();
4618 caller()->CreateAndSetAndSignalOffer();
4619 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4620 caller()->pc()->Close();
4621 callee()->pc()->Close();
4622 int expected_fingerprint = MakeUsageFingerprint(
4623 {PeerConnection::UsageEvent::AUDIO_ADDED,
4624 PeerConnection::UsageEvent::VIDEO_ADDED,
4625 PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_CALLED,
4626 PeerConnection::UsageEvent::SET_REMOTE_DESCRIPTION_CALLED,
4627 PeerConnection::UsageEvent::CANDIDATE_COLLECTED,
4628 PeerConnection::UsageEvent::REMOTE_CANDIDATE_ADDED,
4629 PeerConnection::UsageEvent::ICE_STATE_CONNECTED,
4630 PeerConnection::UsageEvent::CLOSE_CALLED});
4631 EXPECT_TRUE(caller_observer->ExpectOnlySingleEnumCount(
4632 webrtc::kEnumCounterUsagePattern, expected_fingerprint));
4633 EXPECT_TRUE(callee_observer->ExpectOnlySingleEnumCount(
4634 webrtc::kEnumCounterUsagePattern, expected_fingerprint));
4635}
4636
Steve Antonba42e992018-04-09 14:10:01 -07004637INSTANTIATE_TEST_CASE_P(
4638 PeerConnectionIntegrationTest,
4639 PeerConnectionIntegrationInteropTest,
4640 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4641 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
4642
4643// Test that if the Unified Plan side offers two video tracks then the Plan B
4644// side will only see the first one and ignore the second.
4645TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07004646 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
4647 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08004648 ConnectFakeSignaling();
4649 auto first_sender = caller()->AddVideoTrack();
4650 caller()->AddVideoTrack();
4651
4652 caller()->CreateAndSetAndSignalOffer();
4653 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4654
4655 // Verify that there is only one receiver and it corresponds to the first
4656 // added track.
4657 auto receivers = callee()->pc()->GetReceivers();
4658 ASSERT_EQ(1u, receivers.size());
4659 EXPECT_TRUE(receivers[0]->track()->enabled());
4660 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
4661
Seth Hampson2f0d7022018-02-20 11:54:42 -08004662 MediaExpectations media_expectations;
4663 media_expectations.CalleeExpectsSomeVideo();
4664 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004665}
4666
deadbeef1dcb1642017-03-29 21:08:16 -07004667} // namespace
4668
4669#endif // if !defined(THREAD_SANITIZER)