blob: c30ecef7d44cde310c2ce552fef379fce748a74f [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
Taylor Brandstettera4653442018-06-19 09:44:26 -07002584// Same as above but for the legacy stats implementation.
2585TEST_P(PeerConnectionIntegrationTest,
2586 GetStatsForUnsignaledStreamWithOldStatsApi) {
2587 ASSERT_TRUE(CreatePeerConnectionWrappers());
2588 ConnectFakeSignaling();
2589 caller()->AddAudioTrack();
2590 // Remove SSRCs and MSIDs from the received offer SDP.
2591 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2592 caller()->CreateAndSetAndSignalOffer();
2593 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2594
2595 // Note that, since the old stats implementation associates SSRCs with tracks
2596 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
2597 // associated track ID. So we can't use the track "selector" argument.
2598 //
2599 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
2600 // return cached stats if not enough time has passed since the last update.
2601 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0U,
2602 kDefaultTimeout);
2603}
2604
zhihuangf8164932017-05-19 13:09:47 -07002605// Test that we can successfully get the media related stats (audio level
2606// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002607TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07002608 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
2609 ASSERT_TRUE(CreatePeerConnectionWrappers());
2610 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002611 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07002612 // Remove SSRCs and MSIDs from the received offer SDP.
2613 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2614 caller()->CreateAndSetAndSignalOffer();
2615 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002616 MediaExpectations media_expectations;
2617 media_expectations.CalleeExpectsSomeAudio(1);
2618 media_expectations.CalleeExpectsSomeVideo(1);
2619 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07002620
2621 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2622 callee()->NewGetStats();
2623 ASSERT_NE(nullptr, report);
2624
2625 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2626 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
2627 ASSERT_GE(audio_index, 0);
2628 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07002629}
2630
deadbeef4e2deab2017-09-20 13:56:21 -07002631// Helper for test below.
2632void ModifySsrcs(cricket::SessionDescription* desc) {
2633 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07002634 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08002635 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07002636 for (uint32_t& ssrc : stream.ssrcs) {
2637 ssrc = rtc::CreateRandomId();
2638 }
2639 }
2640 }
2641}
2642
2643// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
2644// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
2645// This should result in two "RTCInboundRTPStreamStats", but only one
2646// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
2647// being reset to 0 once the SSRC change occurs.
2648//
2649// Regression test for this bug:
2650// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2651//
2652// The bug causes the track stats to only represent one of the two streams:
2653// whichever one has the higher SSRC. So with this bug, there was a 50% chance
2654// that the track stat counters would reset to 0 when the new stream is
2655// received, and a 50% chance that they'll stop updating (while
2656// "concealed_samples" continues increasing, due to silence being generated for
2657// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002658TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08002659 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07002660 ASSERT_TRUE(CreatePeerConnectionWrappers());
2661 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002662 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07002663 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
2664 // that doesn't signal SSRCs (from the callee's perspective).
2665 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2666 caller()->CreateAndSetAndSignalOffer();
2667 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2668 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002669 {
2670 MediaExpectations media_expectations;
2671 media_expectations.CalleeExpectsSomeAudio(50);
2672 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2673 }
deadbeef4e2deab2017-09-20 13:56:21 -07002674 // Some audio frames were received, so we should have nonzero "samples
2675 // received" for the track.
2676 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
2677 callee()->NewGetStats();
2678 ASSERT_NE(nullptr, report);
2679 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2680 ASSERT_EQ(1U, track_stats.size());
2681 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2682 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
2683 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
2684
2685 // Create a new offer and munge it to cause the caller to use a new SSRC.
2686 caller()->SetGeneratedSdpMunger(ModifySsrcs);
2687 caller()->CreateAndSetAndSignalOffer();
2688 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2689 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
2690 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002691 {
2692 MediaExpectations media_expectations;
2693 media_expectations.CalleeExpectsSomeAudio(25);
2694 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2695 }
deadbeef4e2deab2017-09-20 13:56:21 -07002696
2697 report = callee()->NewGetStats();
2698 ASSERT_NE(nullptr, report);
2699 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
2700 ASSERT_EQ(1U, track_stats.size());
2701 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
2702 // The "total samples received" stat should only be greater than it was
2703 // before.
2704 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
2705 // Right now, the new SSRC will cause the counters to reset to 0.
2706 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
2707
2708 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08002709 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07002710 // good sign that we're seeing stats from the old stream that's no longer
2711 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08002712 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07002713 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
2714 EXPECT_LT(*track_stats[0]->concealed_samples,
2715 *track_stats[0]->total_samples_received *
2716 kAcceptableConcealedSamplesPercentage);
2717
2718 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
2719 // sanity check that the SSRC really changed.
2720 // TODO(deadbeef): This isn't working right now, because we're not returning
2721 // *any* stats for the inactive stream. Uncomment when the bug is completely
2722 // fixed.
2723 // auto inbound_stream_stats =
2724 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
2725 // ASSERT_EQ(2U, inbound_stream_stats.size());
2726}
2727
deadbeef1dcb1642017-03-29 21:08:16 -07002728// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002729TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002730 PeerConnectionFactory::Options dtls_10_options;
2731 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2732 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2733 dtls_10_options));
2734 ConnectFakeSignaling();
2735 // Do normal offer/answer and wait for some frames to be received in each
2736 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002737 caller()->AddAudioVideoTracks();
2738 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002739 caller()->CreateAndSetAndSignalOffer();
2740 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002741 MediaExpectations media_expectations;
2742 media_expectations.ExpectBidirectionalAudioAndVideo();
2743 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002744}
2745
2746// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002747TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002748 PeerConnectionFactory::Options dtls_10_options;
2749 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2750 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
2751 dtls_10_options));
2752 ConnectFakeSignaling();
2753 // Register UMA observer before signaling begins.
2754 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2755 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2756 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002757 caller()->AddAudioVideoTracks();
2758 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002759 caller()->CreateAndSetAndSignalOffer();
2760 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2761 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002762 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002763 kDefaultTimeout);
2764 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002765 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002766 EXPECT_EQ(1,
2767 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2768 kDefaultSrtpCryptoSuite));
2769}
2770
2771// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002772TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07002773 PeerConnectionFactory::Options dtls_12_options;
2774 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2775 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
2776 dtls_12_options));
2777 ConnectFakeSignaling();
2778 // Register UMA observer before signaling begins.
2779 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
2780 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
2781 caller()->pc()->RegisterUMAObserver(caller_observer);
Steve Anton15324772018-01-16 10:26:49 -08002782 caller()->AddAudioVideoTracks();
2783 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002784 caller()->CreateAndSetAndSignalOffer();
2785 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2786 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07002787 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07002788 kDefaultTimeout);
2789 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07002790 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07002791 EXPECT_EQ(1,
2792 caller_observer->GetEnumCounter(webrtc::kEnumCounterAudioSrtpCipher,
2793 kDefaultSrtpCryptoSuite));
2794}
2795
2796// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
2797// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002798TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07002799 PeerConnectionFactory::Options caller_options;
2800 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2801 PeerConnectionFactory::Options callee_options;
2802 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
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
2817// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
2818// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002819TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07002820 PeerConnectionFactory::Options caller_options;
2821 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
2822 PeerConnectionFactory::Options callee_options;
2823 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
2824 ASSERT_TRUE(
2825 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
2826 ConnectFakeSignaling();
2827 // Do normal offer/answer and wait for some frames to be received in each
2828 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002829 caller()->AddAudioVideoTracks();
2830 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002831 caller()->CreateAndSetAndSignalOffer();
2832 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002833 MediaExpectations media_expectations;
2834 media_expectations.ExpectBidirectionalAudioAndVideo();
2835 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002836}
2837
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07002838// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
2839// works as expected; the cipher should only be used if enabled by both sides.
2840TEST_P(PeerConnectionIntegrationTest,
2841 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
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 = false;
2846 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2847 TestNegotiatedCipherSuite(caller_options, callee_options,
2848 expected_cipher_suite);
2849}
2850
2851TEST_P(PeerConnectionIntegrationTest,
2852 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
2853 PeerConnectionFactory::Options caller_options;
2854 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = false;
2855 PeerConnectionFactory::Options callee_options;
2856 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2857 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
2858 TestNegotiatedCipherSuite(caller_options, callee_options,
2859 expected_cipher_suite);
2860}
2861
2862TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
2863 PeerConnectionFactory::Options caller_options;
2864 caller_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2865 PeerConnectionFactory::Options callee_options;
2866 callee_options.crypto_options.enable_aes128_sha1_32_crypto_cipher = true;
2867 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
2868 TestNegotiatedCipherSuite(caller_options, callee_options,
2869 expected_cipher_suite);
2870}
2871
deadbeef1dcb1642017-03-29 21:08:16 -07002872// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002873TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002874 bool local_gcm_enabled = false;
2875 bool remote_gcm_enabled = false;
2876 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2877 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2878 expected_cipher_suite);
2879}
2880
2881// Test that a GCM cipher is used if both ends support it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002882TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07002883 bool local_gcm_enabled = true;
2884 bool remote_gcm_enabled = true;
2885 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
2886 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2887 expected_cipher_suite);
2888}
2889
2890// Test that GCM isn't used if only the offerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002891TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002892 NonGcmCipherUsedWhenOnlyCallerSupportsGcm) {
2893 bool local_gcm_enabled = true;
2894 bool remote_gcm_enabled = false;
2895 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2896 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2897 expected_cipher_suite);
2898}
2899
2900// Test that GCM isn't used if only the answerer supports it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002901TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002902 NonGcmCipherUsedWhenOnlyCalleeSupportsGcm) {
2903 bool local_gcm_enabled = false;
2904 bool remote_gcm_enabled = true;
2905 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
2906 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
2907 expected_cipher_suite);
2908}
2909
deadbeef7914b8c2017-04-21 03:23:33 -07002910// Verify that media can be transmitted end-to-end when GCM crypto suites are
2911// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
2912// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
2913// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002914TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07002915 PeerConnectionFactory::Options gcm_options;
2916 gcm_options.crypto_options.enable_gcm_crypto_suites = true;
2917 ASSERT_TRUE(
2918 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
2919 ConnectFakeSignaling();
2920 // Do normal offer/answer and wait for some frames to be received in each
2921 // direction.
Steve Anton15324772018-01-16 10:26:49 -08002922 caller()->AddAudioVideoTracks();
2923 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07002924 caller()->CreateAndSetAndSignalOffer();
2925 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002926 MediaExpectations media_expectations;
2927 media_expectations.ExpectBidirectionalAudioAndVideo();
2928 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07002929}
2930
deadbeef1dcb1642017-03-29 21:08:16 -07002931// This test sets up a call between two parties with audio, video and an RTP
2932// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002933TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07002934 FakeConstraints setup_constraints;
2935 setup_constraints.SetAllowRtpDataChannels();
2936 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2937 &setup_constraints));
2938 ConnectFakeSignaling();
2939 // Expect that data channel created on caller side will show up for callee as
2940 // well.
2941 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002942 caller()->AddAudioVideoTracks();
2943 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002944 caller()->CreateAndSetAndSignalOffer();
2945 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2946 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002947 MediaExpectations media_expectations;
2948 media_expectations.ExpectBidirectionalAudioAndVideo();
2949 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002950 ASSERT_NE(nullptr, caller()->data_channel());
2951 ASSERT_NE(nullptr, callee()->data_channel());
2952 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2953 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2954
2955 // Ensure data can be sent in both directions.
2956 std::string data = "hello world";
2957 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
2958 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
2959 kDefaultTimeout);
2960 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
2961 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
2962 kDefaultTimeout);
2963}
2964
2965// Ensure that an RTP data channel is signaled as closed for the caller when
2966// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002967TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002968 RtpDataChannelSignaledClosedInCalleeOffer) {
2969 // Same procedure as above test.
2970 FakeConstraints setup_constraints;
2971 setup_constraints.SetAllowRtpDataChannels();
2972 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
2973 &setup_constraints));
2974 ConnectFakeSignaling();
2975 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08002976 caller()->AddAudioVideoTracks();
2977 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002978 caller()->CreateAndSetAndSignalOffer();
2979 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2980 ASSERT_NE(nullptr, caller()->data_channel());
2981 ASSERT_NE(nullptr, callee()->data_channel());
2982 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
2983 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
2984
2985 // Close the data channel on the callee, and do an updated offer/answer.
2986 callee()->data_channel()->Close();
2987 callee()->CreateAndSetAndSignalOffer();
2988 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2989 EXPECT_FALSE(caller()->data_observer()->IsOpen());
2990 EXPECT_FALSE(callee()->data_observer()->IsOpen());
2991}
2992
2993// Tests that data is buffered in an RTP data channel until an observer is
2994// registered for it.
2995//
2996// NOTE: RTP data channels can receive data before the underlying
2997// transport has detected that a channel is writable and thus data can be
2998// received before the data channel state changes to open. That is hard to test
2999// but the same buffering is expected to be used in that case.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003000TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003001 DataBufferedUntilRtpDataChannelObserverRegistered) {
3002 // Use fake clock and simulated network delay so that we predictably can wait
3003 // until an SCTP message has been delivered without "sleep()"ing.
3004 rtc::ScopedFakeClock fake_clock;
3005 // Some things use a time of "0" as a special value, so we need to start out
3006 // the fake clock at a nonzero time.
3007 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003008 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003009 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3010 virtual_socket_server()->UpdateDelayDistribution();
3011
3012 FakeConstraints constraints;
3013 constraints.SetAllowRtpDataChannels();
3014 ASSERT_TRUE(
3015 CreatePeerConnectionWrappersWithConstraints(&constraints, &constraints));
3016 ConnectFakeSignaling();
3017 caller()->CreateDataChannel();
3018 caller()->CreateAndSetAndSignalOffer();
3019 ASSERT_TRUE(caller()->data_channel() != nullptr);
3020 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
3021 kDefaultTimeout, fake_clock);
3022 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
3023 kDefaultTimeout, fake_clock);
3024 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3025 callee()->data_channel()->state(), kDefaultTimeout,
3026 fake_clock);
3027
3028 // Unregister the observer which is normally automatically registered.
3029 callee()->data_channel()->UnregisterObserver();
3030 // Send data and advance fake clock until it should have been received.
3031 std::string data = "hello world";
3032 caller()->data_channel()->Send(DataBuffer(data));
3033 SIMULATED_WAIT(false, 50, fake_clock);
3034
3035 // Attach data channel and expect data to be received immediately. Note that
3036 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3037 // further, but data can be received even if the callback is asynchronous.
3038 MockDataChannelObserver new_observer(callee()->data_channel());
3039 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
3040 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07003041 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
3042 // If this is not done a DCHECK can be hit in ports.cc, because a large
3043 // negative number is calculated for the rtt due to the global clock changing.
3044 caller()->pc()->Close();
3045 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07003046}
3047
3048// This test sets up a call between two parties with audio, video and but only
3049// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003050TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07003051 FakeConstraints setup_constraints_1;
3052 setup_constraints_1.SetAllowRtpDataChannels();
3053 // Must disable DTLS to make negotiation succeed.
3054 setup_constraints_1.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
3055 false);
3056 FakeConstraints setup_constraints_2;
3057 setup_constraints_2.SetMandatory(MediaConstraintsInterface::kEnableDtlsSrtp,
3058 false);
3059 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(
3060 &setup_constraints_1, &setup_constraints_2));
3061 ConnectFakeSignaling();
3062 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003063 caller()->AddAudioVideoTracks();
3064 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003065 caller()->CreateAndSetAndSignalOffer();
3066 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3067 // The caller should still have a data channel, but it should be closed, and
3068 // one should ever have been created for the callee.
3069 EXPECT_TRUE(caller()->data_channel() != nullptr);
3070 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3071 EXPECT_EQ(nullptr, callee()->data_channel());
3072}
3073
3074// This test sets up a call between two parties with audio, and video. When
3075// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003076TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003077 FakeConstraints setup_constraints;
3078 setup_constraints.SetAllowRtpDataChannels();
3079 ASSERT_TRUE(CreatePeerConnectionWrappersWithConstraints(&setup_constraints,
3080 &setup_constraints));
3081 ConnectFakeSignaling();
3082 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003083 caller()->AddAudioVideoTracks();
3084 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003085 caller()->CreateAndSetAndSignalOffer();
3086 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3087 // Create data channel and do new offer and answer.
3088 caller()->CreateDataChannel();
3089 caller()->CreateAndSetAndSignalOffer();
3090 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3091 ASSERT_NE(nullptr, caller()->data_channel());
3092 ASSERT_NE(nullptr, callee()->data_channel());
3093 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3094 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3095 // Ensure data can be sent in both directions.
3096 std::string data = "hello world";
3097 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3098 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3099 kDefaultTimeout);
3100 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3101 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3102 kDefaultTimeout);
3103}
3104
3105#ifdef HAVE_SCTP
3106
3107// This test sets up a call between two parties with audio, video and an SCTP
3108// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003109TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003110 ASSERT_TRUE(CreatePeerConnectionWrappers());
3111 ConnectFakeSignaling();
3112 // Expect that data channel created on caller side will show up for callee as
3113 // well.
3114 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003115 caller()->AddAudioVideoTracks();
3116 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003117 caller()->CreateAndSetAndSignalOffer();
3118 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3119 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003120 MediaExpectations media_expectations;
3121 media_expectations.ExpectBidirectionalAudioAndVideo();
3122 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003123 // Caller data channel should already exist (it created one). Callee data
3124 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3125 ASSERT_NE(nullptr, caller()->data_channel());
3126 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3127 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3128 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3129
3130 // Ensure data can be sent in both directions.
3131 std::string data = "hello world";
3132 caller()->data_channel()->Send(DataBuffer(data));
3133 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3134 kDefaultTimeout);
3135 callee()->data_channel()->Send(DataBuffer(data));
3136 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3137 kDefaultTimeout);
3138}
3139
3140// Ensure that when the callee closes an SCTP data channel, the closing
3141// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003142TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003143 // Same procedure as above test.
3144 ASSERT_TRUE(CreatePeerConnectionWrappers());
3145 ConnectFakeSignaling();
3146 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003147 caller()->AddAudioVideoTracks();
3148 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003149 caller()->CreateAndSetAndSignalOffer();
3150 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3151 ASSERT_NE(nullptr, caller()->data_channel());
3152 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3153 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3154 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3155
3156 // Close the data channel on the callee side, and wait for it to reach the
3157 // "closed" state on both sides.
3158 callee()->data_channel()->Close();
3159 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3160 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3161}
3162
Seth Hampson2f0d7022018-02-20 11:54:42 -08003163TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003164 ASSERT_TRUE(CreatePeerConnectionWrappers());
3165 ConnectFakeSignaling();
3166 webrtc::DataChannelInit init;
3167 init.id = 53;
3168 init.maxRetransmits = 52;
3169 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003170 caller()->AddAudioVideoTracks();
3171 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003172 caller()->CreateAndSetAndSignalOffer();
3173 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003174 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3175 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Steve Antonda6c0952017-10-23 11:41:54 -07003176 EXPECT_EQ(init.id, callee()->data_channel()->id());
3177 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3178 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3179 EXPECT_FALSE(callee()->data_channel()->negotiated());
3180}
3181
deadbeef1dcb1642017-03-29 21:08:16 -07003182// Test usrsctp's ability to process unordered data stream, where data actually
3183// arrives out of order using simulated delays. Previously there have been some
3184// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003185TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003186 // Introduce random network delays.
3187 // Otherwise it's not a true "unordered" test.
3188 virtual_socket_server()->set_delay_mean(20);
3189 virtual_socket_server()->set_delay_stddev(5);
3190 virtual_socket_server()->UpdateDelayDistribution();
3191 // Normal procedure, but with unordered data channel config.
3192 ASSERT_TRUE(CreatePeerConnectionWrappers());
3193 ConnectFakeSignaling();
3194 webrtc::DataChannelInit init;
3195 init.ordered = false;
3196 caller()->CreateDataChannel(&init);
3197 caller()->CreateAndSetAndSignalOffer();
3198 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3199 ASSERT_NE(nullptr, caller()->data_channel());
3200 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3201 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3202 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3203
3204 static constexpr int kNumMessages = 100;
3205 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3206 static constexpr size_t kMaxMessageSize = 4096;
3207 // Create and send random messages.
3208 std::vector<std::string> sent_messages;
3209 for (int i = 0; i < kNumMessages; ++i) {
3210 size_t length =
3211 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3212 std::string message;
3213 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3214 caller()->data_channel()->Send(DataBuffer(message));
3215 callee()->data_channel()->Send(DataBuffer(message));
3216 sent_messages.push_back(message);
3217 }
3218
3219 // Wait for all messages to be received.
3220 EXPECT_EQ_WAIT(kNumMessages,
3221 caller()->data_observer()->received_message_count(),
3222 kDefaultTimeout);
3223 EXPECT_EQ_WAIT(kNumMessages,
3224 callee()->data_observer()->received_message_count(),
3225 kDefaultTimeout);
3226
3227 // Sort and compare to make sure none of the messages were corrupted.
3228 std::vector<std::string> caller_received_messages =
3229 caller()->data_observer()->messages();
3230 std::vector<std::string> callee_received_messages =
3231 callee()->data_observer()->messages();
3232 std::sort(sent_messages.begin(), sent_messages.end());
3233 std::sort(caller_received_messages.begin(), caller_received_messages.end());
3234 std::sort(callee_received_messages.begin(), callee_received_messages.end());
3235 EXPECT_EQ(sent_messages, caller_received_messages);
3236 EXPECT_EQ(sent_messages, callee_received_messages);
3237}
3238
3239// This test sets up a call between two parties with audio, and video. When
3240// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003241TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003242 ASSERT_TRUE(CreatePeerConnectionWrappers());
3243 ConnectFakeSignaling();
3244 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003245 caller()->AddAudioVideoTracks();
3246 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003247 caller()->CreateAndSetAndSignalOffer();
3248 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3249 // Create data channel and do new offer and answer.
3250 caller()->CreateDataChannel();
3251 caller()->CreateAndSetAndSignalOffer();
3252 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3253 // Caller data channel should already exist (it created one). Callee data
3254 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3255 ASSERT_NE(nullptr, caller()->data_channel());
3256 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3257 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3258 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3259 // Ensure data can be sent in both directions.
3260 std::string data = "hello world";
3261 caller()->data_channel()->Send(DataBuffer(data));
3262 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3263 kDefaultTimeout);
3264 callee()->data_channel()->Send(DataBuffer(data));
3265 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3266 kDefaultTimeout);
3267}
3268
deadbeef7914b8c2017-04-21 03:23:33 -07003269// Set up a connection initially just using SCTP data channels, later upgrading
3270// to audio/video, ensuring frames are received end-to-end. Effectively the
3271// inverse of the test above.
3272// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003273TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003274 ASSERT_TRUE(CreatePeerConnectionWrappers());
3275 ConnectFakeSignaling();
3276 // Do initial offer/answer with just data channel.
3277 caller()->CreateDataChannel();
3278 caller()->CreateAndSetAndSignalOffer();
3279 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3280 // Wait until data can be sent over the data channel.
3281 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3282 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3283 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3284
3285 // Do subsequent offer/answer with two-way audio and video. Audio and video
3286 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003287 caller()->AddAudioVideoTracks();
3288 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003289 caller()->CreateAndSetAndSignalOffer();
3290 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003291 MediaExpectations media_expectations;
3292 media_expectations.ExpectBidirectionalAudioAndVideo();
3293 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003294}
3295
deadbeef8b7e9ad2017-05-25 09:38:55 -07003296static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
deadbeef8b7e9ad2017-05-25 09:38:55 -07003297 cricket::DataContentDescription* dcd_offer =
Steve Antonb1c1de12017-12-21 15:14:30 -08003298 GetFirstDataContentDescription(desc);
3299 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003300 dcd_offer->set_use_sctpmap(false);
3301 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3302}
3303
3304// Test that the data channel works when a spec-compliant SCTP m= section is
3305// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3306// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003307TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003308 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3309 ASSERT_TRUE(CreatePeerConnectionWrappers());
3310 ConnectFakeSignaling();
3311 caller()->CreateDataChannel();
3312 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3313 caller()->CreateAndSetAndSignalOffer();
3314 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3315 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3316 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3317 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3318
3319 // Ensure data can be sent in both directions.
3320 std::string data = "hello world";
3321 caller()->data_channel()->Send(DataBuffer(data));
3322 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3323 kDefaultTimeout);
3324 callee()->data_channel()->Send(DataBuffer(data));
3325 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3326 kDefaultTimeout);
3327}
3328
deadbeef1dcb1642017-03-29 21:08:16 -07003329#endif // HAVE_SCTP
3330
3331// Test that the ICE connection and gathering states eventually reach
3332// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003333TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003334 ASSERT_TRUE(CreatePeerConnectionWrappers());
3335 ConnectFakeSignaling();
3336 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003337 caller()->AddAudioVideoTracks();
3338 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003339 caller()->CreateAndSetAndSignalOffer();
3340 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3341 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3342 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3343 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3344 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3345 // After the best candidate pair is selected and all candidates are signaled,
3346 // the ICE connection state should reach "complete".
3347 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3348 // answerer/"callee" by default) only reaches "connected". When this is
3349 // fixed, this test should be updated.
3350 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3351 caller()->ice_connection_state(), kDefaultTimeout);
3352 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3353 callee()->ice_connection_state(), kDefaultTimeout);
3354}
3355
Steve Antonede9ca52017-10-16 13:04:27 -07003356// Test that firewalling the ICE connection causes the clients to identify the
3357// disconnected state and then removing the firewall causes them to reconnect.
3358class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003359 : public PeerConnectionIntegrationBaseTest,
3360 public ::testing::WithParamInterface<
3361 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003362 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003363 PeerConnectionIntegrationIceStatesTest()
3364 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3365 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003366 }
3367
3368 void StartStunServer(const SocketAddress& server_address) {
3369 stun_server_.reset(
3370 cricket::TestStunServer::Create(network_thread(), server_address));
3371 }
3372
3373 bool TestIPv6() {
3374 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3375 }
3376
3377 void SetPortAllocatorFlags() {
Qingsi Wanga2d60672018-04-11 16:57:45 -07003378 network_thread()->Invoke<void>(
3379 RTC_FROM_HERE,
3380 rtc::Bind(&cricket::PortAllocator::set_flags,
3381 caller()->port_allocator(), port_allocator_flags_));
3382 network_thread()->Invoke<void>(
3383 RTC_FROM_HERE,
3384 rtc::Bind(&cricket::PortAllocator::set_flags,
3385 callee()->port_allocator(), port_allocator_flags_));
Steve Antonede9ca52017-10-16 13:04:27 -07003386 }
3387
3388 std::vector<SocketAddress> CallerAddresses() {
3389 std::vector<SocketAddress> addresses;
3390 addresses.push_back(SocketAddress("1.1.1.1", 0));
3391 if (TestIPv6()) {
3392 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3393 }
3394 return addresses;
3395 }
3396
3397 std::vector<SocketAddress> CalleeAddresses() {
3398 std::vector<SocketAddress> addresses;
3399 addresses.push_back(SocketAddress("2.2.2.2", 0));
3400 if (TestIPv6()) {
3401 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3402 }
3403 return addresses;
3404 }
3405
3406 void SetUpNetworkInterfaces() {
3407 // Remove the default interfaces added by the test infrastructure.
3408 caller()->network()->RemoveInterface(kDefaultLocalAddress);
3409 callee()->network()->RemoveInterface(kDefaultLocalAddress);
3410
3411 // Add network addresses for test.
3412 for (const auto& caller_address : CallerAddresses()) {
3413 caller()->network()->AddInterface(caller_address);
3414 }
3415 for (const auto& callee_address : CalleeAddresses()) {
3416 callee()->network()->AddInterface(callee_address);
3417 }
3418 }
3419
3420 private:
3421 uint32_t port_allocator_flags_;
3422 std::unique_ptr<cricket::TestStunServer> stun_server_;
3423};
3424
3425// Tests that the PeerConnection goes through all the ICE gathering/connection
3426// states over the duration of the call. This includes Disconnected and Failed
3427// states, induced by putting a firewall between the peers and waiting for them
3428// to time out.
Steve Anton83119dd2017-11-10 16:19:52 -08003429TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyIceStates) {
3430 // TODO(bugs.webrtc.org/8295): When using a ScopedFakeClock, this test will
3431 // sometimes hit a DCHECK in platform_thread.cc about the PacerThread being
3432 // too busy. For now, revert to running without a fake clock.
Steve Antonede9ca52017-10-16 13:04:27 -07003433
3434 const SocketAddress kStunServerAddress =
3435 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
3436 StartStunServer(kStunServerAddress);
3437
3438 PeerConnectionInterface::RTCConfiguration config;
3439 PeerConnectionInterface::IceServer ice_stun_server;
3440 ice_stun_server.urls.push_back(
3441 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
3442 kStunServerAddress.PortAsString());
3443 config.servers.push_back(ice_stun_server);
3444
3445 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3446 ConnectFakeSignaling();
3447 SetPortAllocatorFlags();
3448 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003449 caller()->AddAudioVideoTracks();
3450 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003451
3452 // Initial state before anything happens.
3453 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
3454 caller()->ice_gathering_state());
3455 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
3456 caller()->ice_connection_state());
3457
3458 // Start the call by creating the offer, setting it as the local description,
3459 // then sending it to the peer who will respond with an answer. This happens
3460 // asynchronously so that we can watch the states as it runs in the
3461 // background.
3462 caller()->CreateAndSetAndSignalOffer();
3463
Steve Anton83119dd2017-11-10 16:19:52 -08003464 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3465 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003466
3467 // Verify that the observer was notified of the intermediate transitions.
3468 EXPECT_THAT(caller()->ice_connection_state_history(),
3469 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
3470 PeerConnectionInterface::kIceConnectionConnected,
3471 PeerConnectionInterface::kIceConnectionCompleted));
3472 EXPECT_THAT(caller()->ice_gathering_state_history(),
3473 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
3474 PeerConnectionInterface::kIceGatheringComplete));
3475
3476 // Block connections to/from the caller and wait for ICE to become
3477 // disconnected.
3478 for (const auto& caller_address : CallerAddresses()) {
3479 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3480 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003481 RTC_LOG(LS_INFO) << "Firewall rules applied";
Steve Anton83119dd2017-11-10 16:19:52 -08003482 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
3483 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003484
3485 // Let ICE re-establish by removing the firewall rules.
3486 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01003487 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Steve Anton83119dd2017-11-10 16:19:52 -08003488 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3489 caller()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003490
3491 // According to RFC7675, if there is no response within 30 seconds then the
3492 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08003493 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07003494 constexpr int kConsentTimeout = 30000;
3495 for (const auto& caller_address : CallerAddresses()) {
3496 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
3497 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01003498 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Steve Anton83119dd2017-11-10 16:19:52 -08003499 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionFailed,
3500 caller()->ice_connection_state(), kConsentTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07003501}
3502
3503// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
3504// and that the statistics in the metric observers are updated correctly.
3505TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
3506 ASSERT_TRUE(CreatePeerConnectionWrappers());
3507 ConnectFakeSignaling();
3508 SetPortAllocatorFlags();
3509 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08003510 caller()->AddAudioVideoTracks();
3511 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07003512
3513 rtc::scoped_refptr<webrtc::FakeMetricsObserver> metrics_observer(
3514 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>());
3515 caller()->pc()->RegisterUMAObserver(metrics_observer.get());
3516
3517 caller()->CreateAndSetAndSignalOffer();
3518
3519 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3520
3521 const int num_best_ipv4 = metrics_observer->GetEnumCounter(
3522 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv4);
3523 const int num_best_ipv6 = metrics_observer->GetEnumCounter(
3524 webrtc::kEnumCounterAddressFamily, webrtc::kBestConnections_IPv6);
3525 if (TestIPv6()) {
3526 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
3527 // connection.
3528 EXPECT_EQ(0u, num_best_ipv4);
3529 EXPECT_EQ(1u, num_best_ipv6);
3530 } else {
3531 EXPECT_EQ(1u, num_best_ipv4);
3532 EXPECT_EQ(0u, num_best_ipv6);
3533 }
3534
3535 EXPECT_EQ(0u, metrics_observer->GetEnumCounter(
3536 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3537 webrtc::kIceCandidatePairHostHost));
3538 EXPECT_EQ(1u, metrics_observer->GetEnumCounter(
3539 webrtc::kEnumCounterIceCandidatePairTypeUdp,
3540 webrtc::kIceCandidatePairHostPublicHostPublic));
3541}
3542
3543constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
3544 cricket::PORTALLOCATOR_DISABLE_STUN |
3545 cricket::PORTALLOCATOR_DISABLE_RELAY;
3546constexpr uint32_t kFlagsIPv6NoStun =
3547 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
3548 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
3549constexpr uint32_t kFlagsIPv4Stun =
3550 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
3551
Seth Hampson2f0d7022018-02-20 11:54:42 -08003552INSTANTIATE_TEST_CASE_P(
3553 PeerConnectionIntegrationTest,
3554 PeerConnectionIntegrationIceStatesTest,
3555 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
3556 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
3557 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
3558 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07003559
deadbeef1dcb1642017-03-29 21:08:16 -07003560// This test sets up a call between two parties with audio and video.
3561// During the call, the caller restarts ICE and the test verifies that
3562// new ICE candidates are generated and audio and video still can flow, and the
3563// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003564TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07003565 ASSERT_TRUE(CreatePeerConnectionWrappers());
3566 ConnectFakeSignaling();
3567 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08003568 caller()->AddAudioVideoTracks();
3569 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003570 caller()->CreateAndSetAndSignalOffer();
3571 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3572 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3573 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3574 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3575 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3576
3577 // To verify that the ICE restart actually occurs, get
3578 // ufrag/password/candidates before and after restart.
3579 // Create an SDP string of the first audio candidate for both clients.
3580 const webrtc::IceCandidateCollection* audio_candidates_caller =
3581 caller()->pc()->local_description()->candidates(0);
3582 const webrtc::IceCandidateCollection* audio_candidates_callee =
3583 callee()->pc()->local_description()->candidates(0);
3584 ASSERT_GT(audio_candidates_caller->count(), 0u);
3585 ASSERT_GT(audio_candidates_callee->count(), 0u);
3586 std::string caller_candidate_pre_restart;
3587 ASSERT_TRUE(
3588 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
3589 std::string callee_candidate_pre_restart;
3590 ASSERT_TRUE(
3591 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
3592 const cricket::SessionDescription* desc =
3593 caller()->pc()->local_description()->description();
3594 std::string caller_ufrag_pre_restart =
3595 desc->transport_infos()[0].description.ice_ufrag;
3596 desc = callee()->pc()->local_description()->description();
3597 std::string callee_ufrag_pre_restart =
3598 desc->transport_infos()[0].description.ice_ufrag;
3599
3600 // Have the caller initiate an ICE restart.
3601 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
3602 caller()->CreateAndSetAndSignalOffer();
3603 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3604 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3605 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3606 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3607 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3608
3609 // Grab the ufrags/candidates again.
3610 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
3611 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
3612 ASSERT_GT(audio_candidates_caller->count(), 0u);
3613 ASSERT_GT(audio_candidates_callee->count(), 0u);
3614 std::string caller_candidate_post_restart;
3615 ASSERT_TRUE(
3616 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
3617 std::string callee_candidate_post_restart;
3618 ASSERT_TRUE(
3619 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
3620 desc = caller()->pc()->local_description()->description();
3621 std::string caller_ufrag_post_restart =
3622 desc->transport_infos()[0].description.ice_ufrag;
3623 desc = callee()->pc()->local_description()->description();
3624 std::string callee_ufrag_post_restart =
3625 desc->transport_infos()[0].description.ice_ufrag;
3626 // Sanity check that an ICE restart was actually negotiated in SDP.
3627 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
3628 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
3629 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
3630 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
3631
3632 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003633 MediaExpectations media_expectations;
3634 media_expectations.ExpectBidirectionalAudioAndVideo();
3635 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003636}
3637
3638// Verify that audio/video can be received end-to-end when ICE renomination is
3639// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003640TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07003641 PeerConnectionInterface::RTCConfiguration config;
3642 config.enable_ice_renomination = true;
3643 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
3644 ConnectFakeSignaling();
3645 // Do normal offer/answer and wait for some frames to be received in each
3646 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003647 caller()->AddAudioVideoTracks();
3648 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003649 caller()->CreateAndSetAndSignalOffer();
3650 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3651 // Sanity check that ICE renomination was actually negotiated.
3652 const cricket::SessionDescription* desc =
3653 caller()->pc()->local_description()->description();
3654 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003655 ASSERT_NE(
3656 info.description.transport_options.end(),
3657 std::find(info.description.transport_options.begin(),
3658 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003659 }
3660 desc = callee()->pc()->local_description()->description();
3661 for (const cricket::TransportInfo& info : desc->transport_infos()) {
deadbeef30952b42017-04-21 02:41:29 -07003662 ASSERT_NE(
3663 info.description.transport_options.end(),
3664 std::find(info.description.transport_options.begin(),
3665 info.description.transport_options.end(), "renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07003666 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08003667 MediaExpectations media_expectations;
3668 media_expectations.ExpectBidirectionalAudioAndVideo();
3669 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003670}
3671
Steve Anton6f25b092017-10-23 09:39:20 -07003672// With a max bundle policy and RTCP muxing, adding a new media description to
3673// the connection should not affect ICE at all because the new media will use
3674// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003675TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003676 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07003677 PeerConnectionInterface::RTCConfiguration config;
3678 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3679 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3680 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
3681 config, PeerConnectionInterface::RTCConfiguration()));
3682 ConnectFakeSignaling();
3683
Steve Anton15324772018-01-16 10:26:49 -08003684 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003685 caller()->CreateAndSetAndSignalOffer();
3686 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07003687 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
3688 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07003689
3690 caller()->clear_ice_connection_state_history();
3691
Steve Anton15324772018-01-16 10:26:49 -08003692 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07003693 caller()->CreateAndSetAndSignalOffer();
3694 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3695
3696 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
3697}
3698
deadbeef1dcb1642017-03-29 21:08:16 -07003699// This test sets up a call between two parties with audio and video. It then
3700// renegotiates setting the video m-line to "port 0", then later renegotiates
3701// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003702TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003703 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
3704 ASSERT_TRUE(CreatePeerConnectionWrappers());
3705 ConnectFakeSignaling();
3706
3707 // Do initial negotiation, only sending media from the caller. Will result in
3708 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08003709 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003710 caller()->CreateAndSetAndSignalOffer();
3711 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3712
3713 // Negotiate again, disabling the video "m=" section (the callee will set the
3714 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003715 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3716 PeerConnectionInterface::RTCOfferAnswerOptions options;
3717 options.offer_to_receive_video = 0;
3718 callee()->SetOfferAnswerOptions(options);
3719 } else {
3720 callee()->SetRemoteOfferHandler([this] {
3721 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
3722 });
3723 }
deadbeef1dcb1642017-03-29 21:08:16 -07003724 caller()->CreateAndSetAndSignalOffer();
3725 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3726 // Sanity check that video "m=" section was actually rejected.
3727 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
3728 callee()->pc()->local_description()->description());
3729 ASSERT_NE(nullptr, answer_video_content);
3730 ASSERT_TRUE(answer_video_content->rejected);
3731
3732 // Enable video and do negotiation again, making sure video is received
3733 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003734 if (sdp_semantics_ == SdpSemantics::kPlanB) {
3735 PeerConnectionInterface::RTCOfferAnswerOptions options;
3736 options.offer_to_receive_video = 1;
3737 callee()->SetOfferAnswerOptions(options);
3738 } else {
3739 // The caller's transceiver is stopped, so we need to add another track.
3740 auto caller_transceiver =
3741 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
3742 EXPECT_TRUE(caller_transceiver->stopped());
3743 caller()->AddVideoTrack();
3744 }
3745 callee()->AddVideoTrack();
3746 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07003747 caller()->CreateAndSetAndSignalOffer();
3748 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003749
deadbeef1dcb1642017-03-29 21:08:16 -07003750 // Verify the caller receives frames from the newly added stream, and the
3751 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003752 MediaExpectations media_expectations;
3753 media_expectations.CalleeExpectsSomeAudio();
3754 media_expectations.ExpectBidirectionalVideo();
3755 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003756}
3757
deadbeef1dcb1642017-03-29 21:08:16 -07003758// This tests that if we negotiate after calling CreateSender but before we
3759// have a track, then set a track later, frames from the newly-set track are
3760// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003761TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07003762 MediaFlowsAfterEarlyWarmupWithCreateSender) {
3763 ASSERT_TRUE(CreatePeerConnectionWrappers());
3764 ConnectFakeSignaling();
3765 auto caller_audio_sender =
3766 caller()->pc()->CreateSender("audio", "caller_stream");
3767 auto caller_video_sender =
3768 caller()->pc()->CreateSender("video", "caller_stream");
3769 auto callee_audio_sender =
3770 callee()->pc()->CreateSender("audio", "callee_stream");
3771 auto callee_video_sender =
3772 callee()->pc()->CreateSender("video", "callee_stream");
3773 caller()->CreateAndSetAndSignalOffer();
3774 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3775 // Wait for ICE to complete, without any tracks being set.
3776 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3777 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3778 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3779 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3780 // Now set the tracks, and expect frames to immediately start flowing.
3781 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3782 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3783 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3784 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08003785 MediaExpectations media_expectations;
3786 media_expectations.ExpectBidirectionalAudioAndVideo();
3787 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3788}
3789
3790// This tests that if we negotiate after calling AddTransceiver but before we
3791// have a track, then set a track later, frames from the newly-set tracks are
3792// received end-to-end.
3793TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
3794 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
3795 ASSERT_TRUE(CreatePeerConnectionWrappers());
3796 ConnectFakeSignaling();
3797 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
3798 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
3799 auto caller_audio_sender = audio_result.MoveValue()->sender();
3800 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
3801 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
3802 auto caller_video_sender = video_result.MoveValue()->sender();
3803 callee()->SetRemoteOfferHandler([this] {
3804 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
3805 callee()->pc()->GetTransceivers()[0]->SetDirection(
3806 RtpTransceiverDirection::kSendRecv);
3807 callee()->pc()->GetTransceivers()[1]->SetDirection(
3808 RtpTransceiverDirection::kSendRecv);
3809 });
3810 caller()->CreateAndSetAndSignalOffer();
3811 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3812 // Wait for ICE to complete, without any tracks being set.
3813 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3814 caller()->ice_connection_state(), kMaxWaitForFramesMs);
3815 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3816 callee()->ice_connection_state(), kMaxWaitForFramesMs);
3817 // Now set the tracks, and expect frames to immediately start flowing.
3818 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
3819 auto callee_video_sender = callee()->pc()->GetSenders()[1];
3820 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
3821 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
3822 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
3823 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
3824 MediaExpectations media_expectations;
3825 media_expectations.ExpectBidirectionalAudioAndVideo();
3826 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003827}
3828
3829// This test verifies that a remote video track can be added via AddStream,
3830// and sent end-to-end. For this particular test, it's simply echoed back
3831// from the caller to the callee, rather than being forwarded to a third
3832// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003833TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07003834 ASSERT_TRUE(CreatePeerConnectionWrappers());
3835 ConnectFakeSignaling();
3836 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08003837 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07003838 caller()->CreateAndSetAndSignalOffer();
3839 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3840 ASSERT_EQ(1, callee()->remote_streams()->count());
3841
3842 // Echo the stream back, and do a new offer/anwer (initiated by callee this
3843 // time).
3844 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
3845 callee()->CreateAndSetAndSignalOffer();
3846 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
3847
Seth Hampson2f0d7022018-02-20 11:54:42 -08003848 MediaExpectations media_expectations;
3849 media_expectations.ExpectBidirectionalVideo();
3850 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003851}
3852
3853// Test that we achieve the expected end-to-end connection time, using a
3854// fake clock and simulated latency on the media and signaling paths.
3855// We use a TURN<->TURN connection because this is usually the quickest to
3856// set up initially, especially when we're confident the connection will work
3857// and can start sending media before we get a STUN response.
3858//
3859// With various optimizations enabled, here are the network delays we expect to
3860// be on the critical path:
3861// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
3862// signaling answer (with DTLS fingerprint).
3863// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
3864// using TURN<->TURN pair, and DTLS exchange is 4 packets,
3865// the first of which should have arrived before the answer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003866TEST_P(PeerConnectionIntegrationTest, EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07003867 rtc::ScopedFakeClock fake_clock;
3868 // Some things use a time of "0" as a special value, so we need to start out
3869 // the fake clock at a nonzero time.
3870 // TODO(deadbeef): Fix this.
Sebastian Jansson5f83cf02018-05-08 14:52:22 +02003871 fake_clock.AdvanceTime(webrtc::TimeDelta::seconds(1));
deadbeef1dcb1642017-03-29 21:08:16 -07003872
3873 static constexpr int media_hop_delay_ms = 50;
3874 static constexpr int signaling_trip_delay_ms = 500;
3875 // For explanation of these values, see comment above.
3876 static constexpr int required_media_hops = 9;
3877 static constexpr int required_signaling_trips = 2;
3878 // For internal delays (such as posting an event asychronously).
3879 static constexpr int allowed_internal_delay_ms = 20;
3880 static constexpr int total_connection_time_ms =
3881 media_hop_delay_ms * required_media_hops +
3882 signaling_trip_delay_ms * required_signaling_trips +
3883 allowed_internal_delay_ms;
3884
3885 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3886 3478};
3887 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3888 0};
3889 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3890 3478};
3891 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3892 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07003893 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
3894 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003895
Seth Hampsonaed71642018-06-11 07:41:32 -07003896 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
3897 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07003898 // Bypass permission check on received packets so media can be sent before
3899 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07003900 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
3901 turn_server_1->set_enable_permission_checks(false);
3902 });
3903 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
3904 turn_server_2->set_enable_permission_checks(false);
3905 });
deadbeef1dcb1642017-03-29 21:08:16 -07003906
3907 PeerConnectionInterface::RTCConfiguration client_1_config;
3908 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3909 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3910 ice_server_1.username = "test";
3911 ice_server_1.password = "test";
3912 client_1_config.servers.push_back(ice_server_1);
3913 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
3914 client_1_config.presume_writable_when_fully_relayed = true;
3915
3916 PeerConnectionInterface::RTCConfiguration client_2_config;
3917 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3918 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3919 ice_server_2.username = "test";
3920 ice_server_2.password = "test";
3921 client_2_config.servers.push_back(ice_server_2);
3922 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
3923 client_2_config.presume_writable_when_fully_relayed = true;
3924
3925 ASSERT_TRUE(
3926 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3927 // Set up the simulated delays.
3928 SetSignalingDelayMs(signaling_trip_delay_ms);
3929 ConnectFakeSignaling();
3930 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
3931 virtual_socket_server()->UpdateDelayDistribution();
3932
3933 // Set "offer to receive audio/video" without adding any tracks, so we just
3934 // set up ICE/DTLS with no media.
3935 PeerConnectionInterface::RTCOfferAnswerOptions options;
3936 options.offer_to_receive_audio = 1;
3937 options.offer_to_receive_video = 1;
3938 caller()->SetOfferAnswerOptions(options);
3939 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07003940 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
3941 fake_clock);
Seth Hampson1d4a76d2018-06-19 14:31:41 -07003942 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
3943 // If this is not done a DCHECK can be hit in ports.cc, because a large
3944 // negative number is calculated for the rtt due to the global clock changing.
3945 caller()->pc()->Close();
3946 callee()->pc()->Close();
deadbeef1dcb1642017-03-29 21:08:16 -07003947}
3948
Jonas Orelandbdcee282017-10-10 14:01:40 +02003949// Verify that a TurnCustomizer passed in through RTCConfiguration
3950// is actually used by the underlying TURN candidate pair.
3951// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003952TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02003953 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
3954 3478};
3955 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
3956 0};
3957 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
3958 3478};
3959 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
3960 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07003961 CreateTurnServer(turn_server_1_internal_address,
3962 turn_server_1_external_address);
3963 CreateTurnServer(turn_server_2_internal_address,
3964 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02003965
3966 PeerConnectionInterface::RTCConfiguration client_1_config;
3967 webrtc::PeerConnectionInterface::IceServer ice_server_1;
3968 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
3969 ice_server_1.username = "test";
3970 ice_server_1.password = "test";
3971 client_1_config.servers.push_back(ice_server_1);
3972 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07003973 auto* customizer1 = CreateTurnCustomizer();
3974 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02003975
3976 PeerConnectionInterface::RTCConfiguration client_2_config;
3977 webrtc::PeerConnectionInterface::IceServer ice_server_2;
3978 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
3979 ice_server_2.username = "test";
3980 ice_server_2.password = "test";
3981 client_2_config.servers.push_back(ice_server_2);
3982 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07003983 auto* customizer2 = CreateTurnCustomizer();
3984 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02003985
3986 ASSERT_TRUE(
3987 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
3988 ConnectFakeSignaling();
3989
3990 // Set "offer to receive audio/video" without adding any tracks, so we just
3991 // set up ICE/DTLS with no media.
3992 PeerConnectionInterface::RTCOfferAnswerOptions options;
3993 options.offer_to_receive_audio = 1;
3994 options.offer_to_receive_video = 1;
3995 caller()->SetOfferAnswerOptions(options);
3996 caller()->CreateAndSetAndSignalOffer();
3997 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
3998
Seth Hampsonaed71642018-06-11 07:41:32 -07003999 ExpectTurnCustomizerCountersIncremented(customizer1);
4000 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004001}
4002
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004003// Verifies that you can use TCP instead of UDP to connect to a TURN server and
4004// send media between the caller and the callee.
4005TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
4006 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4007 3478};
4008 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4009
4010 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07004011 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4012 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004013
4014 webrtc::PeerConnectionInterface::IceServer ice_server;
4015 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4016 ice_server.username = "test";
4017 ice_server.password = "test";
4018
4019 PeerConnectionInterface::RTCConfiguration client_1_config;
4020 client_1_config.servers.push_back(ice_server);
4021 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4022
4023 PeerConnectionInterface::RTCConfiguration client_2_config;
4024 client_2_config.servers.push_back(ice_server);
4025 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4026
4027 ASSERT_TRUE(
4028 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4029
4030 // Do normal offer/answer and wait for ICE to complete.
4031 ConnectFakeSignaling();
4032 caller()->AddAudioVideoTracks();
4033 callee()->AddAudioVideoTracks();
4034 caller()->CreateAndSetAndSignalOffer();
4035 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4036 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4037 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4038
4039 MediaExpectations media_expectations;
4040 media_expectations.ExpectBidirectionalAudioAndVideo();
4041 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4042}
4043
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004044// Verify that a SSLCertificateVerifier passed in through
4045// PeerConnectionDependencies is actually used by the underlying SSL
4046// implementation to determine whether a certificate presented by the TURN
4047// server is accepted by the client. Note that openssladapter_unittest.cc
4048// contains more detailed, lower-level tests.
4049TEST_P(PeerConnectionIntegrationTest,
4050 SSLCertificateVerifierUsedForTurnConnections) {
4051 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4052 3478};
4053 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4054
4055 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4056 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004057 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4058 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004059
4060 webrtc::PeerConnectionInterface::IceServer ice_server;
4061 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4062 ice_server.username = "test";
4063 ice_server.password = "test";
4064
4065 PeerConnectionInterface::RTCConfiguration client_1_config;
4066 client_1_config.servers.push_back(ice_server);
4067 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4068
4069 PeerConnectionInterface::RTCConfiguration client_2_config;
4070 client_2_config.servers.push_back(ice_server);
4071 // Setting the type to kRelay forces the connection to go through a TURN
4072 // server.
4073 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4074
4075 // Get a copy to the pointer so we can verify calls later.
4076 rtc::TestCertificateVerifier* client_1_cert_verifier =
4077 new rtc::TestCertificateVerifier();
4078 client_1_cert_verifier->verify_certificate_ = true;
4079 rtc::TestCertificateVerifier* client_2_cert_verifier =
4080 new rtc::TestCertificateVerifier();
4081 client_2_cert_verifier->verify_certificate_ = true;
4082
4083 // Create the dependencies with the test certificate verifier.
4084 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4085 client_1_deps.tls_cert_verifier =
4086 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4087 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4088 client_2_deps.tls_cert_verifier =
4089 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4090
4091 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4092 client_1_config, std::move(client_1_deps), client_2_config,
4093 std::move(client_2_deps)));
4094 ConnectFakeSignaling();
4095
4096 // Set "offer to receive audio/video" without adding any tracks, so we just
4097 // set up ICE/DTLS with no media.
4098 PeerConnectionInterface::RTCOfferAnswerOptions options;
4099 options.offer_to_receive_audio = 1;
4100 options.offer_to_receive_video = 1;
4101 caller()->SetOfferAnswerOptions(options);
4102 caller()->CreateAndSetAndSignalOffer();
4103 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4104
4105 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4106 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004107}
4108
4109TEST_P(PeerConnectionIntegrationTest,
4110 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4111 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4112 3478};
4113 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4114
4115 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4116 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004117 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4118 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004119
4120 webrtc::PeerConnectionInterface::IceServer ice_server;
4121 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4122 ice_server.username = "test";
4123 ice_server.password = "test";
4124
4125 PeerConnectionInterface::RTCConfiguration client_1_config;
4126 client_1_config.servers.push_back(ice_server);
4127 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4128
4129 PeerConnectionInterface::RTCConfiguration client_2_config;
4130 client_2_config.servers.push_back(ice_server);
4131 // Setting the type to kRelay forces the connection to go through a TURN
4132 // server.
4133 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4134
4135 // Get a copy to the pointer so we can verify calls later.
4136 rtc::TestCertificateVerifier* client_1_cert_verifier =
4137 new rtc::TestCertificateVerifier();
4138 client_1_cert_verifier->verify_certificate_ = false;
4139 rtc::TestCertificateVerifier* client_2_cert_verifier =
4140 new rtc::TestCertificateVerifier();
4141 client_2_cert_verifier->verify_certificate_ = false;
4142
4143 // Create the dependencies with the test certificate verifier.
4144 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4145 client_1_deps.tls_cert_verifier =
4146 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4147 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4148 client_2_deps.tls_cert_verifier =
4149 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4150
4151 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4152 client_1_config, std::move(client_1_deps), client_2_config,
4153 std::move(client_2_deps)));
4154 ConnectFakeSignaling();
4155
4156 // Set "offer to receive audio/video" without adding any tracks, so we just
4157 // set up ICE/DTLS with no media.
4158 PeerConnectionInterface::RTCOfferAnswerOptions options;
4159 options.offer_to_receive_audio = 1;
4160 options.offer_to_receive_video = 1;
4161 caller()->SetOfferAnswerOptions(options);
4162 caller()->CreateAndSetAndSignalOffer();
4163 bool wait_res = true;
4164 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4165 // properly, should be able to just wait for a state of "failed" instead of
4166 // waiting a fixed 10 seconds.
4167 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4168 ASSERT_FALSE(wait_res);
4169
4170 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4171 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004172}
4173
deadbeefc964d0b2017-04-03 10:03:35 -07004174// Test that audio and video flow end-to-end when codec names don't use the
4175// expected casing, given that they're supposed to be case insensitive. To test
4176// this, all but one codec is removed from each media description, and its
4177// casing is changed.
4178//
4179// In the past, this has regressed and caused crashes/black video, due to the
4180// fact that code at some layers was doing case-insensitive comparisons and
4181// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004182TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004183 ASSERT_TRUE(CreatePeerConnectionWrappers());
4184 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004185 caller()->AddAudioVideoTracks();
4186 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004187
4188 // Remove all but one audio/video codec (opus and VP8), and change the
4189 // casing of the caller's generated offer.
4190 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4191 cricket::AudioContentDescription* audio =
4192 GetFirstAudioContentDescription(description);
4193 ASSERT_NE(nullptr, audio);
4194 auto audio_codecs = audio->codecs();
4195 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4196 [](const cricket::AudioCodec& codec) {
4197 return codec.name != "opus";
4198 }),
4199 audio_codecs.end());
4200 ASSERT_EQ(1u, audio_codecs.size());
4201 audio_codecs[0].name = "OpUs";
4202 audio->set_codecs(audio_codecs);
4203
4204 cricket::VideoContentDescription* video =
4205 GetFirstVideoContentDescription(description);
4206 ASSERT_NE(nullptr, video);
4207 auto video_codecs = video->codecs();
4208 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4209 [](const cricket::VideoCodec& codec) {
4210 return codec.name != "VP8";
4211 }),
4212 video_codecs.end());
4213 ASSERT_EQ(1u, video_codecs.size());
4214 video_codecs[0].name = "vP8";
4215 video->set_codecs(video_codecs);
4216 });
4217
4218 caller()->CreateAndSetAndSignalOffer();
4219 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4220
4221 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004222 MediaExpectations media_expectations;
4223 media_expectations.ExpectBidirectionalAudioAndVideo();
4224 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004225}
4226
Seth Hampson2f0d7022018-02-20 11:54:42 -08004227TEST_P(PeerConnectionIntegrationTest, GetSources) {
hbos8d609f62017-04-10 07:39:05 -07004228 ASSERT_TRUE(CreatePeerConnectionWrappers());
4229 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004230 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004231 caller()->CreateAndSetAndSignalOffer();
4232 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004233 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004234 MediaExpectations media_expectations;
4235 media_expectations.CalleeExpectsSomeAudio(1);
4236 ASSERT_TRUE(ExpectNewFrames(media_expectations));
hbos8d609f62017-04-10 07:39:05 -07004237 ASSERT_GT(callee()->pc()->GetReceivers().size(), 0u);
4238 auto receiver = callee()->pc()->GetReceivers()[0];
4239 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
4240
4241 auto contributing_sources = receiver->GetSources();
4242 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4243 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4244 contributing_sources[0].source_id());
4245}
4246
deadbeef2f425aa2017-04-14 10:41:32 -07004247// Test that if a track is removed and added again with a different stream ID,
4248// the new stream ID is successfully communicated in SDP and media continues to
4249// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004250// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4251// it will not reuse a transceiver that has already been sending. After creating
4252// a new transceiver it tries to create an offer with two senders of the same
4253// track ids and it fails.
4254TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004255 ASSERT_TRUE(CreatePeerConnectionWrappers());
4256 ConnectFakeSignaling();
4257
4258 rtc::scoped_refptr<MediaStreamInterface> stream_1 =
4259 caller()->pc_factory()->CreateLocalMediaStream("stream_1");
4260 rtc::scoped_refptr<MediaStreamInterface> stream_2 =
4261 caller()->pc_factory()->CreateLocalMediaStream("stream_2");
4262
4263 // Add track using stream 1, do offer/answer.
4264 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4265 caller()->CreateLocalAudioTrack();
4266 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
4267 caller()->pc()->AddTrack(track, {stream_1.get()});
4268 caller()->CreateAndSetAndSignalOffer();
4269 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004270 {
4271 MediaExpectations media_expectations;
4272 media_expectations.CalleeExpectsSomeAudio(1);
4273 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4274 }
deadbeef2f425aa2017-04-14 10:41:32 -07004275 // Remove the sender, and create a new one with the new stream.
4276 caller()->pc()->RemoveTrack(sender);
4277 sender = caller()->pc()->AddTrack(track, {stream_2.get()});
4278 caller()->CreateAndSetAndSignalOffer();
4279 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4280 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004281 {
4282 MediaExpectations media_expectations;
4283 media_expectations.CalleeExpectsSomeAudio();
4284 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4285 }
deadbeef2f425aa2017-04-14 10:41:32 -07004286}
4287
Seth Hampson2f0d7022018-02-20 11:54:42 -08004288TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004289 ASSERT_TRUE(CreatePeerConnectionWrappers());
4290 ConnectFakeSignaling();
4291
4292 auto output = rtc::MakeUnique<testing::NiceMock<MockRtcEventLogOutput>>();
4293 ON_CALL(*output, IsActive()).WillByDefault(testing::Return(true));
4294 ON_CALL(*output, Write(::testing::_)).WillByDefault(testing::Return(true));
4295 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004296 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4297 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004298
Steve Anton15324772018-01-16 10:26:49 -08004299 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004300 caller()->CreateAndSetAndSignalOffer();
4301 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4302}
4303
Steve Antonede9ca52017-10-16 13:04:27 -07004304// Test that if candidates are only signaled by applying full session
4305// descriptions (instead of using AddIceCandidate), the peers can connect to
4306// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004307TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004308 ASSERT_TRUE(CreatePeerConnectionWrappers());
4309 // Each side will signal the session descriptions but not candidates.
4310 ConnectFakeSignalingForSdpOnly();
4311
4312 // Add audio video track and exchange the initial offer/answer with media
4313 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004314 caller()->AddAudioVideoTracks();
4315 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004316 caller()->CreateAndSetAndSignalOffer();
4317
4318 // Wait for all candidates to be gathered on both the caller and callee.
4319 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4320 caller()->ice_gathering_state(), kDefaultTimeout);
4321 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4322 callee()->ice_gathering_state(), kDefaultTimeout);
4323
4324 // The candidates will now be included in the session description, so
4325 // signaling them will start the ICE connection.
4326 caller()->CreateAndSetAndSignalOffer();
4327 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4328
4329 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004330 MediaExpectations media_expectations;
4331 media_expectations.ExpectBidirectionalAudioAndVideo();
4332 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004333}
4334
henrika5f6bf242017-11-01 11:06:56 +01004335// Test that SetAudioPlayout can be used to disable audio playout from the
4336// start, then later enable it. This may be useful, for example, if the caller
4337// needs to play a local ringtone until some event occurs, after which it
4338// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004339TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004340 ASSERT_TRUE(CreatePeerConnectionWrappers());
4341 ConnectFakeSignaling();
4342
4343 // Set up audio-only call where audio playout is disabled on caller's side.
4344 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08004345 caller()->AddAudioTrack();
4346 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004347 caller()->CreateAndSetAndSignalOffer();
4348 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4349
4350 // Pump messages for a second.
4351 WAIT(false, 1000);
4352 // Since audio playout is disabled, the caller shouldn't have received
4353 // anything (at the playout level, at least).
4354 EXPECT_EQ(0, caller()->audio_frames_received());
4355 // As a sanity check, make sure the callee (for which playout isn't disabled)
4356 // did still see frames on its audio level.
4357 ASSERT_GT(callee()->audio_frames_received(), 0);
4358
4359 // Enable playout again, and ensure audio starts flowing.
4360 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004361 MediaExpectations media_expectations;
4362 media_expectations.ExpectBidirectionalAudio();
4363 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01004364}
4365
4366double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
4367 auto report = pc->NewGetStats();
4368 auto track_stats_list =
4369 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
4370 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
4371 for (const auto* track_stats : track_stats_list) {
4372 if (track_stats->remote_source.is_defined() &&
4373 *track_stats->remote_source) {
4374 remote_track_stats = track_stats;
4375 break;
4376 }
4377 }
4378
4379 if (!remote_track_stats->total_audio_energy.is_defined()) {
4380 return 0.0;
4381 }
4382 return *remote_track_stats->total_audio_energy;
4383}
4384
4385// Test that if audio playout is disabled via the SetAudioPlayout() method, then
4386// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004387TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01004388 DisableAudioPlayoutStillGeneratesAudioStats) {
4389 ASSERT_TRUE(CreatePeerConnectionWrappers());
4390 ConnectFakeSignaling();
4391
4392 // Set up audio-only call where playout is disabled but audio-processing is
4393 // still active.
Steve Anton15324772018-01-16 10:26:49 -08004394 caller()->AddAudioTrack();
4395 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01004396 caller()->pc()->SetAudioPlayout(false);
4397
4398 caller()->CreateAndSetAndSignalOffer();
4399 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4400
4401 // Wait for the callee to receive audio stats.
4402 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
4403}
4404
henrika4f167df2017-11-01 14:45:55 +01004405// Test that SetAudioRecording can be used to disable audio recording from the
4406// start, then later enable it. This may be useful, for example, if the caller
4407// wants to ensure that no audio resources are active before a certain state
4408// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004409TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01004410 ASSERT_TRUE(CreatePeerConnectionWrappers());
4411 ConnectFakeSignaling();
4412
4413 // Set up audio-only call where audio recording is disabled on caller's side.
4414 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08004415 caller()->AddAudioTrack();
4416 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01004417 caller()->CreateAndSetAndSignalOffer();
4418 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4419
4420 // Pump messages for a second.
4421 WAIT(false, 1000);
4422 // Since caller has disabled audio recording, the callee shouldn't have
4423 // received anything.
4424 EXPECT_EQ(0, callee()->audio_frames_received());
4425 // As a sanity check, make sure the caller did still see frames on its
4426 // audio level since audio recording is enabled on the calle side.
4427 ASSERT_GT(caller()->audio_frames_received(), 0);
4428
4429 // Enable audio recording again, and ensure audio starts flowing.
4430 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004431 MediaExpectations media_expectations;
4432 media_expectations.ExpectBidirectionalAudio();
4433 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01004434}
4435
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004436// Test that after closing PeerConnections, they stop sending any packets (ICE,
4437// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004438TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004439 // Set up audio/video/data, wait for some frames to be received.
4440 ASSERT_TRUE(CreatePeerConnectionWrappers());
4441 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004442 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004443#ifdef HAVE_SCTP
4444 caller()->CreateDataChannel();
4445#endif
4446 caller()->CreateAndSetAndSignalOffer();
4447 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004448 MediaExpectations media_expectations;
4449 media_expectations.CalleeExpectsSomeAudioAndVideo();
4450 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08004451 // Close PeerConnections.
4452 caller()->pc()->Close();
4453 callee()->pc()->Close();
4454 // Pump messages for a second, and ensure no new packets end up sent.
4455 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
4456 WAIT(false, 1000);
4457 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
4458 EXPECT_EQ(sent_packets_a, sent_packets_b);
4459}
4460
Steve Anton7eca0932018-03-30 15:18:41 -07004461// Test that transport stats are generated by the RTCStatsCollector for a
4462// connection that only involves data channels. This is a regression test for
4463// crbug.com/826972.
4464#ifdef HAVE_SCTP
4465TEST_P(PeerConnectionIntegrationTest,
4466 TransportStatsReportedForDataChannelOnlyConnection) {
4467 ASSERT_TRUE(CreatePeerConnectionWrappers());
4468 ConnectFakeSignaling();
4469 caller()->CreateDataChannel();
4470
4471 caller()->CreateAndSetAndSignalOffer();
4472 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4473 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
4474
4475 auto caller_report = caller()->NewGetStats();
4476 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
4477 auto callee_report = callee()->NewGetStats();
4478 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
4479}
4480#endif // HAVE_SCTP
4481
Qingsi Wang7685e862018-06-11 20:15:46 -07004482TEST_P(PeerConnectionIntegrationTest,
4483 IceEventsGeneratedAndLoggedInRtcEventLog) {
4484 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
4485 ConnectFakeSignaling();
4486 PeerConnectionInterface::RTCOfferAnswerOptions options;
4487 options.offer_to_receive_audio = 1;
4488 caller()->SetOfferAnswerOptions(options);
4489 caller()->CreateAndSetAndSignalOffer();
4490 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4491 ASSERT_NE(nullptr, caller()->event_log_factory());
4492 ASSERT_NE(nullptr, callee()->event_log_factory());
4493 webrtc::FakeRtcEventLog* caller_event_log =
4494 static_cast<webrtc::FakeRtcEventLog*>(
4495 caller()->event_log_factory()->last_log_created());
4496 webrtc::FakeRtcEventLog* callee_event_log =
4497 static_cast<webrtc::FakeRtcEventLog*>(
4498 callee()->event_log_factory()->last_log_created());
4499 ASSERT_NE(nullptr, caller_event_log);
4500 ASSERT_NE(nullptr, callee_event_log);
4501 int caller_ice_config_count = caller_event_log->GetEventCount(
4502 webrtc::RtcEvent::Type::IceCandidatePairConfig);
4503 int caller_ice_event_count = caller_event_log->GetEventCount(
4504 webrtc::RtcEvent::Type::IceCandidatePairEvent);
4505 int callee_ice_config_count = callee_event_log->GetEventCount(
4506 webrtc::RtcEvent::Type::IceCandidatePairConfig);
4507 int callee_ice_event_count = callee_event_log->GetEventCount(
4508 webrtc::RtcEvent::Type::IceCandidatePairEvent);
4509 EXPECT_LT(0, caller_ice_config_count);
4510 EXPECT_LT(0, caller_ice_event_count);
4511 EXPECT_LT(0, callee_ice_config_count);
4512 EXPECT_LT(0, callee_ice_event_count);
4513}
4514
Seth Hampson2f0d7022018-02-20 11:54:42 -08004515INSTANTIATE_TEST_CASE_P(PeerConnectionIntegrationTest,
4516 PeerConnectionIntegrationTest,
4517 Values(SdpSemantics::kPlanB,
4518 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08004519
Steve Anton74255ff2018-01-24 18:32:57 -08004520// Tests that verify interoperability between Plan B and Unified Plan
4521// PeerConnections.
4522class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08004523 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08004524 public ::testing::WithParamInterface<
4525 std::tuple<SdpSemantics, SdpSemantics>> {
4526 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08004527 // Setting the SdpSemantics for the base test to kDefault does not matter
4528 // because we specify not to use the test semantics when creating
4529 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08004530 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07004531 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08004532 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08004533 callee_semantics_(std::get<1>(GetParam())) {}
4534
4535 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07004536 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
4537 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08004538 }
4539
4540 const SdpSemantics caller_semantics_;
4541 const SdpSemantics callee_semantics_;
4542};
4543
4544TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
4545 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4546 ConnectFakeSignaling();
4547
4548 caller()->CreateAndSetAndSignalOffer();
4549 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4550}
4551
4552TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
4553 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4554 ConnectFakeSignaling();
4555 auto audio_sender = caller()->AddAudioTrack();
4556
4557 caller()->CreateAndSetAndSignalOffer();
4558 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4559
4560 // Verify that one audio receiver has been created on the remote and that it
4561 // has the same track ID as the sending track.
4562 auto receivers = callee()->pc()->GetReceivers();
4563 ASSERT_EQ(1u, receivers.size());
4564 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
4565 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
4566
Seth Hampson2f0d7022018-02-20 11:54:42 -08004567 MediaExpectations media_expectations;
4568 media_expectations.CalleeExpectsSomeAudio();
4569 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004570}
4571
4572TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
4573 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4574 ConnectFakeSignaling();
4575 auto video_sender = caller()->AddVideoTrack();
4576 auto audio_sender = caller()->AddAudioTrack();
4577
4578 caller()->CreateAndSetAndSignalOffer();
4579 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4580
4581 // Verify that one audio and one video receiver have been created on the
4582 // remote and that they have the same track IDs as the sending tracks.
4583 auto audio_receivers =
4584 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
4585 ASSERT_EQ(1u, audio_receivers.size());
4586 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
4587 auto video_receivers =
4588 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
4589 ASSERT_EQ(1u, video_receivers.size());
4590 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
4591
Seth Hampson2f0d7022018-02-20 11:54:42 -08004592 MediaExpectations media_expectations;
4593 media_expectations.CalleeExpectsSomeAudioAndVideo();
4594 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004595}
4596
4597TEST_P(PeerConnectionIntegrationInteropTest,
4598 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
4599 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4600 ConnectFakeSignaling();
4601 caller()->AddAudioVideoTracks();
4602 callee()->AddAudioVideoTracks();
4603
4604 caller()->CreateAndSetAndSignalOffer();
4605 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4606
Seth Hampson2f0d7022018-02-20 11:54:42 -08004607 MediaExpectations media_expectations;
4608 media_expectations.ExpectBidirectionalAudioAndVideo();
4609 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004610}
4611
4612TEST_P(PeerConnectionIntegrationInteropTest,
4613 ReverseRolesOneAudioLocalToOneVideoRemote) {
4614 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
4615 ConnectFakeSignaling();
4616 caller()->AddAudioTrack();
4617 callee()->AddVideoTrack();
4618
4619 caller()->CreateAndSetAndSignalOffer();
4620 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4621
4622 // Verify that only the audio track has been negotiated.
4623 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
4624 // Might also check that the callee's NegotiationNeeded flag is set.
4625
4626 // Reverse roles.
4627 callee()->CreateAndSetAndSignalOffer();
4628 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4629
Seth Hampson2f0d7022018-02-20 11:54:42 -08004630 MediaExpectations media_expectations;
4631 media_expectations.CallerExpectsSomeVideo();
4632 media_expectations.CalleeExpectsSomeAudio();
4633 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004634}
4635
Harald Alvestrand8ebba742018-05-31 14:00:34 +02004636// Test getting the usage fingerprint for a simple test case.
4637TEST_P(PeerConnectionIntegrationTest, UsageFingerprintHistogram) {
4638 ASSERT_TRUE(CreatePeerConnectionWrappers());
4639 ConnectFakeSignaling();
4640 // Register UMA observer before signaling begins.
4641 rtc::scoped_refptr<webrtc::FakeMetricsObserver> caller_observer =
4642 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
4643 caller()->pc()->RegisterUMAObserver(caller_observer);
4644 rtc::scoped_refptr<webrtc::FakeMetricsObserver> callee_observer =
4645 new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
4646 callee()->pc()->RegisterUMAObserver(callee_observer);
4647 caller()->AddAudioTrack();
4648 caller()->AddVideoTrack();
4649 caller()->CreateAndSetAndSignalOffer();
4650 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4651 caller()->pc()->Close();
4652 callee()->pc()->Close();
4653 int expected_fingerprint = MakeUsageFingerprint(
4654 {PeerConnection::UsageEvent::AUDIO_ADDED,
4655 PeerConnection::UsageEvent::VIDEO_ADDED,
4656 PeerConnection::UsageEvent::SET_LOCAL_DESCRIPTION_CALLED,
4657 PeerConnection::UsageEvent::SET_REMOTE_DESCRIPTION_CALLED,
4658 PeerConnection::UsageEvent::CANDIDATE_COLLECTED,
4659 PeerConnection::UsageEvent::REMOTE_CANDIDATE_ADDED,
4660 PeerConnection::UsageEvent::ICE_STATE_CONNECTED,
4661 PeerConnection::UsageEvent::CLOSE_CALLED});
4662 EXPECT_TRUE(caller_observer->ExpectOnlySingleEnumCount(
4663 webrtc::kEnumCounterUsagePattern, expected_fingerprint));
4664 EXPECT_TRUE(callee_observer->ExpectOnlySingleEnumCount(
4665 webrtc::kEnumCounterUsagePattern, expected_fingerprint));
4666}
4667
Steve Antonba42e992018-04-09 14:10:01 -07004668INSTANTIATE_TEST_CASE_P(
4669 PeerConnectionIntegrationTest,
4670 PeerConnectionIntegrationInteropTest,
4671 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4672 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
4673
4674// Test that if the Unified Plan side offers two video tracks then the Plan B
4675// side will only see the first one and ignore the second.
4676TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07004677 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
4678 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08004679 ConnectFakeSignaling();
4680 auto first_sender = caller()->AddVideoTrack();
4681 caller()->AddVideoTrack();
4682
4683 caller()->CreateAndSetAndSignalOffer();
4684 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4685
4686 // Verify that there is only one receiver and it corresponds to the first
4687 // added track.
4688 auto receivers = callee()->pc()->GetReceivers();
4689 ASSERT_EQ(1u, receivers.size());
4690 EXPECT_TRUE(receivers[0]->track()->enabled());
4691 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
4692
Seth Hampson2f0d7022018-02-20 11:54:42 -08004693 MediaExpectations media_expectations;
4694 media_expectations.CalleeExpectsSomeVideo();
4695 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08004696}
4697
deadbeef1dcb1642017-03-29 21:08:16 -07004698} // namespace
4699
4700#endif // if !defined(THREAD_SANITIZER)