blob: afb5f2ba752916770e2936eaf51995dbbe9c5035 [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
deadbeef1dcb1642017-03-29 21:08:16 -070017#include <functional>
18#include <list>
19#include <map>
20#include <memory>
21#include <utility>
22#include <vector>
23
Steve Anton64b626b2019-01-28 17:25:26 -080024#include "absl/algorithm/container.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "api/media_stream_interface.h"
26#include "api/peer_connection_interface.h"
27#include "api/peer_connection_proxy.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020028#include "api/rtc_event_log/rtc_event_log_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "api/rtp_receiver_interface.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020030#include "api/task_queue/default_task_queue_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "api/uma_metrics.h"
Anders Carlsson67537952018-05-03 11:28:29 +020032#include "api/video_codecs/sdp_video_format.h"
Qingsi Wang7685e862018-06-11 20:15:46 -070033#include "call/call.h"
34#include "logging/rtc_event_log/fake_rtc_event_log_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080035#include "media/engine/fake_webrtc_video_engine.h"
36#include "media/engine/webrtc_media_engine.h"
Danil Chapovalov9da25bd2019-06-20 10:19:42 +020037#include "media/engine/webrtc_media_engine_defaults.h"
Per Ã…hgrencc73ed32020-04-26 23:56:17 +020038#include "modules/audio_processing/test/audio_processing_builder_for_testing.h"
Qingsi Wang25ec8882019-11-15 12:33:05 -080039#include "p2p/base/fake_ice_transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080040#include "p2p/base/mock_async_resolver.h"
41#include "p2p/base/p2p_constants.h"
42#include "p2p/base/port_interface.h"
43#include "p2p/base/test_stun_server.h"
44#include "p2p/base/test_turn_customizer.h"
45#include "p2p/base/test_turn_server.h"
46#include "p2p/client/basic_port_allocator.h"
47#include "pc/dtmf_sender.h"
48#include "pc/local_audio_source.h"
49#include "pc/media_session.h"
50#include "pc/peer_connection.h"
51#include "pc/peer_connection_factory.h"
52#include "pc/rtp_media_utils.h"
53#include "pc/session_description.h"
54#include "pc/test/fake_audio_capture_module.h"
55#include "pc/test/fake_periodic_video_track_source.h"
56#include "pc/test/fake_rtc_certificate_generator.h"
57#include "pc/test/fake_video_track_renderer.h"
58#include "pc/test/mock_peer_connection_observers.h"
Jonas Olssonb75d9e92019-02-22 10:33:29 +010059#include "rtc_base/fake_clock.h"
Qingsi Wangecd30542019-05-22 14:34:56 -070060#include "rtc_base/fake_mdns_responder.h"
Steve Anton10542f22019-01-11 09:11:00 -080061#include "rtc_base/fake_network.h"
62#include "rtc_base/firewall_socket_server.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020063#include "rtc_base/gunit.h"
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +020064#include "rtc_base/numerics/safe_conversions.h"
Steve Anton10542f22019-01-11 09:11:00 -080065#include "rtc_base/test_certificate_verifier.h"
66#include "rtc_base/time_utils.h"
67#include "rtc_base/virtual_socket_server.h"
Mirko Bonadei17f48782018-09-28 08:51:10 +020068#include "system_wrappers/include/metrics.h"
Qingsi Wangc129c352019-04-18 10:41:58 -070069#include "test/field_trial.h"
Elad Alon99c3fe52017-10-13 16:29:40 +020070#include "test/gmock.h"
deadbeef1dcb1642017-03-29 21:08:16 -070071
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010072namespace webrtc {
73namespace {
74
75using ::cricket::ContentInfo;
76using ::cricket::StreamParams;
77using ::rtc::SocketAddress;
78using ::testing::_;
Seth Hampson2f0d7022018-02-20 11:54:42 -080079using ::testing::Combine;
Steve Anton64b626b2019-01-28 17:25:26 -080080using ::testing::Contains;
Mirko Bonadeie46f5db2019-03-26 20:14:46 +010081using ::testing::DoAll;
Steve Antonede9ca52017-10-16 13:04:27 -070082using ::testing::ElementsAre;
Qingsi Wang1dac6d82018-12-12 15:28:47 -080083using ::testing::NiceMock;
Steve Anton64b626b2019-01-28 17:25:26 -080084using ::testing::Return;
Zach Stein6fcdc2f2018-08-23 16:25:55 -070085using ::testing::SetArgPointee;
Steve Antonffa6ce42018-11-30 09:26:08 -080086using ::testing::UnorderedElementsAreArray;
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +010087using ::testing::Values;
Steve Anton74255ff2018-01-24 18:32:57 -080088using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
deadbeef1dcb1642017-03-29 21:08:16 -070089
90static const int kDefaultTimeout = 10000;
91static const int kMaxWaitForStatsMs = 3000;
92static const int kMaxWaitForActivationMs = 5000;
93static const int kMaxWaitForFramesMs = 10000;
94// Default number of audio/video frames to wait for before considering a test
95// successful.
96static const int kDefaultExpectedAudioFrameCount = 3;
97static const int kDefaultExpectedVideoFrameCount = 3;
98
deadbeef1dcb1642017-03-29 21:08:16 -070099static const char kDataChannelLabel[] = "data_channel";
100
101// SRTP cipher name negotiated by the tests. This must be updated if the
102// default changes.
Taylor Brandstetterfd350d72018-04-03 16:29:26 -0700103static const int kDefaultSrtpCryptoSuite = rtc::SRTP_AES128_CM_SHA1_80;
deadbeef1dcb1642017-03-29 21:08:16 -0700104static const int kDefaultSrtpCryptoSuiteGcm = rtc::SRTP_AEAD_AES_256_GCM;
105
Steve Antonede9ca52017-10-16 13:04:27 -0700106static const SocketAddress kDefaultLocalAddress("192.168.1.1", 0);
107
deadbeef1dcb1642017-03-29 21:08:16 -0700108// Helper function for constructing offer/answer options to initiate an ICE
109// restart.
110PeerConnectionInterface::RTCOfferAnswerOptions IceRestartOfferAnswerOptions() {
111 PeerConnectionInterface::RTCOfferAnswerOptions options;
112 options.ice_restart = true;
113 return options;
114}
115
deadbeefd8ad7882017-04-18 16:01:17 -0700116// Remove all stream information (SSRCs, track IDs, etc.) and "msid-semantic"
117// attribute from received SDP, simulating a legacy endpoint.
118void RemoveSsrcsAndMsids(cricket::SessionDescription* desc) {
119 for (ContentInfo& content : desc->contents()) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800120 content.media_description()->mutable_streams().clear();
deadbeefd8ad7882017-04-18 16:01:17 -0700121 }
122 desc->set_msid_supported(false);
Henrik Boström5b147782018-12-04 11:25:05 +0100123 desc->set_msid_signaling(0);
deadbeefd8ad7882017-04-18 16:01:17 -0700124}
125
Seth Hampson5897a6e2018-04-03 11:16:33 -0700126// Removes all stream information besides the stream ids, simulating an
127// endpoint that only signals a=msid lines to convey stream_ids.
128void RemoveSsrcsAndKeepMsids(cricket::SessionDescription* desc) {
129 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700130 std::string track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700131 std::vector<std::string> stream_ids;
132 if (!content.media_description()->streams().empty()) {
Steve Antondf527fd2018-04-27 15:52:03 -0700133 const StreamParams& first_stream =
134 content.media_description()->streams()[0];
135 track_id = first_stream.id;
136 stream_ids = first_stream.stream_ids();
Seth Hampson5897a6e2018-04-03 11:16:33 -0700137 }
138 content.media_description()->mutable_streams().clear();
Steve Antondf527fd2018-04-27 15:52:03 -0700139 StreamParams new_stream;
140 new_stream.id = track_id;
Seth Hampson5897a6e2018-04-03 11:16:33 -0700141 new_stream.set_stream_ids(stream_ids);
142 content.media_description()->AddStream(new_stream);
143 }
144}
145
zhihuangf8164932017-05-19 13:09:47 -0700146int FindFirstMediaStatsIndexByKind(
147 const std::string& kind,
148 const std::vector<const webrtc::RTCMediaStreamTrackStats*>&
149 media_stats_vec) {
150 for (size_t i = 0; i < media_stats_vec.size(); i++) {
151 if (media_stats_vec[i]->kind.ValueToString() == kind) {
152 return i;
153 }
154 }
155 return -1;
156}
157
deadbeef1dcb1642017-03-29 21:08:16 -0700158class SignalingMessageReceiver {
159 public:
Steve Antona3a92c22017-12-07 10:27:41 -0800160 virtual void ReceiveSdpMessage(SdpType type, const std::string& msg) = 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700161 virtual void ReceiveIceMessage(const std::string& sdp_mid,
162 int sdp_mline_index,
163 const std::string& msg) = 0;
164
165 protected:
166 SignalingMessageReceiver() {}
167 virtual ~SignalingMessageReceiver() {}
168};
169
170class MockRtpReceiverObserver : public webrtc::RtpReceiverObserverInterface {
171 public:
172 explicit MockRtpReceiverObserver(cricket::MediaType media_type)
173 : expected_media_type_(media_type) {}
174
175 void OnFirstPacketReceived(cricket::MediaType media_type) override {
176 ASSERT_EQ(expected_media_type_, media_type);
177 first_packet_received_ = true;
178 }
179
180 bool first_packet_received() const { return first_packet_received_; }
181
182 virtual ~MockRtpReceiverObserver() {}
183
184 private:
185 bool first_packet_received_ = false;
186 cricket::MediaType expected_media_type_;
187};
188
189// Helper class that wraps a peer connection, observes it, and can accept
190// signaling messages from another wrapper.
191//
192// Uses a fake network, fake A/V capture, and optionally fake
193// encoders/decoders, though they aren't used by default since they don't
194// advertise support of any codecs.
Steve Anton94286cb2017-09-26 16:20:19 -0700195// TODO(steveanton): See how this could become a subclass of
Seth Hampson2f0d7022018-02-20 11:54:42 -0800196// PeerConnectionWrapper defined in peerconnectionwrapper.h.
deadbeef1dcb1642017-03-29 21:08:16 -0700197class PeerConnectionWrapper : public webrtc::PeerConnectionObserver,
Steve Anton15324772018-01-16 10:26:49 -0800198 public SignalingMessageReceiver {
deadbeef1dcb1642017-03-29 21:08:16 -0700199 public:
200 // Different factory methods for convenience.
201 // TODO(deadbeef): Could use the pattern of:
202 //
203 // PeerConnectionWrapper =
204 // WrapperBuilder.WithConfig(...).WithOptions(...).build();
205 //
206 // To reduce some code duplication.
207 static PeerConnectionWrapper* CreateWithDtlsIdentityStore(
208 const std::string& debug_name,
209 std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator,
210 rtc::Thread* network_thread,
211 rtc::Thread* worker_thread) {
212 PeerConnectionWrapper* client(new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700213 webrtc::PeerConnectionDependencies dependencies(nullptr);
214 dependencies.cert_generator = std::move(cert_generator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200215 if (!client->Init(nullptr, nullptr, std::move(dependencies), network_thread,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800216 worker_thread, nullptr,
Johannes Kron3e983682020-03-29 22:17:00 +0200217 /*reset_encoder_factory=*/false,
218 /*reset_decoder_factory=*/false)) {
deadbeef1dcb1642017-03-29 21:08:16 -0700219 delete client;
220 return nullptr;
221 }
222 return client;
223 }
224
deadbeef2f425aa2017-04-14 10:41:32 -0700225 webrtc::PeerConnectionFactoryInterface* pc_factory() const {
226 return peer_connection_factory_.get();
227 }
228
deadbeef1dcb1642017-03-29 21:08:16 -0700229 webrtc::PeerConnectionInterface* pc() const { return peer_connection_.get(); }
230
231 // If a signaling message receiver is set (via ConnectFakeSignaling), this
232 // will set the whole offer/answer exchange in motion. Just need to wait for
233 // the signaling state to reach "stable".
234 void CreateAndSetAndSignalOffer() {
Eldar Rello5ab79e62019-10-09 18:29:44 +0300235 auto offer = CreateOfferAndWait();
deadbeef1dcb1642017-03-29 21:08:16 -0700236 ASSERT_NE(nullptr, offer);
237 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(offer)));
238 }
239
240 // Sets the options to be used when CreateAndSetAndSignalOffer is called, or
241 // when a remote offer is received (via fake signaling) and an answer is
242 // generated. By default, uses default options.
243 void SetOfferAnswerOptions(
244 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
245 offer_answer_options_ = options;
246 }
247
248 // Set a callback to be invoked when SDP is received via the fake signaling
249 // channel, which provides an opportunity to munge (modify) the SDP. This is
250 // used to test SDP being applied that a PeerConnection would normally not
251 // generate, but a non-JSEP endpoint might.
252 void SetReceivedSdpMunger(
253 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100254 received_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700255 }
256
deadbeefc964d0b2017-04-03 10:03:35 -0700257 // Similar to the above, but this is run on SDP immediately after it's
deadbeef1dcb1642017-03-29 21:08:16 -0700258 // generated.
259 void SetGeneratedSdpMunger(
260 std::function<void(cricket::SessionDescription*)> munger) {
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100261 generated_sdp_munger_ = std::move(munger);
deadbeef1dcb1642017-03-29 21:08:16 -0700262 }
263
Seth Hampson2f0d7022018-02-20 11:54:42 -0800264 // Set a callback to be invoked when a remote offer is received via the fake
265 // signaling channel. This provides an opportunity to change the
266 // PeerConnection state before an answer is created and sent to the caller.
267 void SetRemoteOfferHandler(std::function<void()> handler) {
268 remote_offer_handler_ = std::move(handler);
269 }
270
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800271 void SetRemoteAsyncResolver(rtc::MockAsyncResolver* resolver) {
272 remote_async_resolver_ = resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700273 }
274
Steve Antonede9ca52017-10-16 13:04:27 -0700275 // Every ICE connection state in order that has been seen by the observer.
276 std::vector<PeerConnectionInterface::IceConnectionState>
277 ice_connection_state_history() const {
278 return ice_connection_state_history_;
279 }
Steve Anton6f25b092017-10-23 09:39:20 -0700280 void clear_ice_connection_state_history() {
281 ice_connection_state_history_.clear();
282 }
Steve Antonede9ca52017-10-16 13:04:27 -0700283
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100284 // Every standardized ICE connection state in order that has been seen by the
285 // observer.
286 std::vector<PeerConnectionInterface::IceConnectionState>
287 standardized_ice_connection_state_history() const {
288 return standardized_ice_connection_state_history_;
289 }
290
Jonas Olsson635474e2018-10-18 15:58:17 +0200291 // Every PeerConnection state in order that has been seen by the observer.
292 std::vector<PeerConnectionInterface::PeerConnectionState>
293 peer_connection_state_history() const {
294 return peer_connection_state_history_;
295 }
296
Steve Antonede9ca52017-10-16 13:04:27 -0700297 // Every ICE gathering state in order that has been seen by the observer.
298 std::vector<PeerConnectionInterface::IceGatheringState>
299 ice_gathering_state_history() const {
300 return ice_gathering_state_history_;
deadbeef1dcb1642017-03-29 21:08:16 -0700301 }
Alex Drake00c7ecf2019-08-06 10:54:47 -0700302 std::vector<cricket::CandidatePairChangeEvent>
303 ice_candidate_pair_change_history() const {
304 return ice_candidate_pair_change_history_;
305 }
deadbeef1dcb1642017-03-29 21:08:16 -0700306
Eldar Rello5ab79e62019-10-09 18:29:44 +0300307 // Every PeerConnection signaling state in order that has been seen by the
308 // observer.
309 std::vector<PeerConnectionInterface::SignalingState>
310 peer_connection_signaling_state_history() const {
311 return peer_connection_signaling_state_history_;
312 }
313
Steve Anton15324772018-01-16 10:26:49 -0800314 void AddAudioVideoTracks() {
315 AddAudioTrack();
316 AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -0700317 }
318
Steve Anton74255ff2018-01-24 18:32:57 -0800319 rtc::scoped_refptr<RtpSenderInterface> AddAudioTrack() {
320 return AddTrack(CreateLocalAudioTrack());
321 }
deadbeef1dcb1642017-03-29 21:08:16 -0700322
Steve Anton74255ff2018-01-24 18:32:57 -0800323 rtc::scoped_refptr<RtpSenderInterface> AddVideoTrack() {
324 return AddTrack(CreateLocalVideoTrack());
325 }
deadbeef1dcb1642017-03-29 21:08:16 -0700326
327 rtc::scoped_refptr<webrtc::AudioTrackInterface> CreateLocalAudioTrack() {
Niels Möller2d02e082018-05-21 11:23:35 +0200328 cricket::AudioOptions options;
deadbeef1dcb1642017-03-29 21:08:16 -0700329 // Disable highpass filter so that we can get all the test audio frames.
Niels Möller2d02e082018-05-21 11:23:35 +0200330 options.highpass_filter = false;
deadbeef1dcb1642017-03-29 21:08:16 -0700331 rtc::scoped_refptr<webrtc::AudioSourceInterface> source =
Niels Möller2d02e082018-05-21 11:23:35 +0200332 peer_connection_factory_->CreateAudioSource(options);
deadbeef1dcb1642017-03-29 21:08:16 -0700333 // TODO(perkj): Test audio source when it is implemented. Currently audio
334 // always use the default input.
deadbeefb1a15d72017-09-07 14:12:05 -0700335 return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
deadbeef1dcb1642017-03-29 21:08:16 -0700336 source);
337 }
338
339 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
Johannes Kron965e7942018-09-13 15:36:20 +0200340 webrtc::FakePeriodicVideoSource::Config config;
341 config.timestamp_offset_ms = rtc::TimeMillis();
342 return CreateLocalVideoTrackInternal(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;
Johannes Kron965e7942018-09-13 15:36:20 +0200355 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +0200356 return CreateLocalVideoTrackInternal(config);
deadbeef1dcb1642017-03-29 21:08:16 -0700357 }
358
Steve Anton74255ff2018-01-24 18:32:57 -0800359 rtc::scoped_refptr<RtpSenderInterface> AddTrack(
360 rtc::scoped_refptr<MediaStreamTrackInterface> track,
Seth Hampson845e8782018-03-02 11:34:10 -0800361 const std::vector<std::string>& stream_ids = {}) {
362 auto result = pc()->AddTrack(track, stream_ids);
Steve Anton15324772018-01-16 10:26:49 -0800363 EXPECT_EQ(RTCErrorType::NONE, result.error().type());
Steve Anton74255ff2018-01-24 18:32:57 -0800364 return result.MoveValue();
365 }
366
367 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> GetReceiversOfType(
368 cricket::MediaType media_type) {
369 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers;
Mirko Bonadei739baf02019-01-27 17:29:42 +0100370 for (const auto& receiver : pc()->GetReceivers()) {
Steve Anton74255ff2018-01-24 18:32:57 -0800371 if (receiver->media_type() == media_type) {
372 receivers.push_back(receiver);
373 }
374 }
375 return receivers;
deadbeef1dcb1642017-03-29 21:08:16 -0700376 }
377
Seth Hampson2f0d7022018-02-20 11:54:42 -0800378 rtc::scoped_refptr<RtpTransceiverInterface> GetFirstTransceiverOfType(
379 cricket::MediaType media_type) {
380 for (auto transceiver : pc()->GetTransceivers()) {
381 if (transceiver->receiver()->media_type() == media_type) {
382 return transceiver;
383 }
384 }
385 return nullptr;
386 }
387
deadbeef1dcb1642017-03-29 21:08:16 -0700388 bool SignalingStateStable() {
389 return pc()->signaling_state() == webrtc::PeerConnectionInterface::kStable;
390 }
391
392 void CreateDataChannel() { CreateDataChannel(nullptr); }
393
394 void CreateDataChannel(const webrtc::DataChannelInit* init) {
Steve Antonda6c0952017-10-23 11:41:54 -0700395 CreateDataChannel(kDataChannelLabel, init);
396 }
397
398 void CreateDataChannel(const std::string& label,
399 const webrtc::DataChannelInit* init) {
400 data_channel_ = pc()->CreateDataChannel(label, init);
deadbeef1dcb1642017-03-29 21:08:16 -0700401 ASSERT_TRUE(data_channel_.get() != nullptr);
402 data_observer_.reset(new MockDataChannelObserver(data_channel_));
403 }
404
405 DataChannelInterface* data_channel() { return data_channel_; }
406 const MockDataChannelObserver* data_observer() const {
407 return data_observer_.get();
408 }
409
410 int audio_frames_received() const {
411 return fake_audio_capture_module_->frames_received();
412 }
413
414 // Takes minimum of video frames received for each track.
415 //
416 // Can be used like:
417 // EXPECT_GE(expected_frames, min_video_frames_received_per_track());
418 //
419 // To ensure that all video tracks received at least a certain number of
420 // frames.
421 int min_video_frames_received_per_track() const {
422 int min_frames = INT_MAX;
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200423 if (fake_video_renderers_.empty()) {
424 return 0;
deadbeef1dcb1642017-03-29 21:08:16 -0700425 }
deadbeef1dcb1642017-03-29 21:08:16 -0700426
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200427 for (const auto& pair : fake_video_renderers_) {
428 min_frames = std::min(min_frames, pair.second->num_rendered_frames());
deadbeef1dcb1642017-03-29 21:08:16 -0700429 }
Anders Carlsson5f2bb622018-05-14 09:48:06 +0200430 return min_frames;
deadbeef1dcb1642017-03-29 21:08:16 -0700431 }
432
433 // Returns a MockStatsObserver in a state after stats gathering finished,
434 // which can be used to access the gathered stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700435 rtc::scoped_refptr<MockStatsObserver> OldGetStatsForTrack(
deadbeef1dcb1642017-03-29 21:08:16 -0700436 webrtc::MediaStreamTrackInterface* track) {
437 rtc::scoped_refptr<MockStatsObserver> observer(
438 new rtc::RefCountedObject<MockStatsObserver>());
439 EXPECT_TRUE(peer_connection_->GetStats(
440 observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
441 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
442 return observer;
443 }
444
445 // Version that doesn't take a track "filter", and gathers all stats.
deadbeefd8ad7882017-04-18 16:01:17 -0700446 rtc::scoped_refptr<MockStatsObserver> OldGetStats() {
447 return OldGetStatsForTrack(nullptr);
448 }
449
450 // Synchronously gets stats and returns them. If it times out, fails the test
451 // and returns null.
452 rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
453 rtc::scoped_refptr<webrtc::MockRTCStatsCollectorCallback> callback(
454 new rtc::RefCountedObject<webrtc::MockRTCStatsCollectorCallback>());
455 peer_connection_->GetStats(callback);
456 EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
457 return callback->report();
deadbeef1dcb1642017-03-29 21:08:16 -0700458 }
459
460 int rendered_width() {
461 EXPECT_FALSE(fake_video_renderers_.empty());
462 return fake_video_renderers_.empty()
463 ? 0
464 : fake_video_renderers_.begin()->second->width();
465 }
466
467 int rendered_height() {
468 EXPECT_FALSE(fake_video_renderers_.empty());
469 return fake_video_renderers_.empty()
470 ? 0
471 : fake_video_renderers_.begin()->second->height();
472 }
473
474 double rendered_aspect_ratio() {
475 if (rendered_height() == 0) {
476 return 0.0;
477 }
478 return static_cast<double>(rendered_width()) / rendered_height();
479 }
480
481 webrtc::VideoRotation rendered_rotation() {
482 EXPECT_FALSE(fake_video_renderers_.empty());
483 return fake_video_renderers_.empty()
484 ? webrtc::kVideoRotation_0
485 : fake_video_renderers_.begin()->second->rotation();
486 }
487
488 int local_rendered_width() {
489 return local_video_renderer_ ? local_video_renderer_->width() : 0;
490 }
491
492 int local_rendered_height() {
493 return local_video_renderer_ ? local_video_renderer_->height() : 0;
494 }
495
496 double local_rendered_aspect_ratio() {
497 if (local_rendered_height() == 0) {
498 return 0.0;
499 }
500 return static_cast<double>(local_rendered_width()) /
501 local_rendered_height();
502 }
503
504 size_t number_of_remote_streams() {
505 if (!pc()) {
506 return 0;
507 }
508 return pc()->remote_streams()->count();
509 }
510
511 StreamCollectionInterface* remote_streams() const {
512 if (!pc()) {
513 ADD_FAILURE();
514 return nullptr;
515 }
516 return pc()->remote_streams();
517 }
518
519 StreamCollectionInterface* local_streams() {
520 if (!pc()) {
521 ADD_FAILURE();
522 return nullptr;
523 }
524 return pc()->local_streams();
525 }
526
527 webrtc::PeerConnectionInterface::SignalingState signaling_state() {
528 return pc()->signaling_state();
529 }
530
531 webrtc::PeerConnectionInterface::IceConnectionState ice_connection_state() {
532 return pc()->ice_connection_state();
533 }
534
Jonas Olsson7a6739e2019-01-15 16:31:55 +0100535 webrtc::PeerConnectionInterface::IceConnectionState
536 standardized_ice_connection_state() {
537 return pc()->standardized_ice_connection_state();
538 }
539
deadbeef1dcb1642017-03-29 21:08:16 -0700540 webrtc::PeerConnectionInterface::IceGatheringState ice_gathering_state() {
541 return pc()->ice_gathering_state();
542 }
543
544 // Returns a MockRtpReceiverObserver for each RtpReceiver returned by
545 // GetReceivers. They're updated automatically when a remote offer/answer
546 // from the fake signaling channel is applied, or when
547 // ResetRtpReceiverObservers below is called.
548 const std::vector<std::unique_ptr<MockRtpReceiverObserver>>&
549 rtp_receiver_observers() {
550 return rtp_receiver_observers_;
551 }
552
553 void ResetRtpReceiverObservers() {
554 rtp_receiver_observers_.clear();
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100555 for (const rtc::scoped_refptr<RtpReceiverInterface>& receiver :
556 pc()->GetReceivers()) {
deadbeef1dcb1642017-03-29 21:08:16 -0700557 std::unique_ptr<MockRtpReceiverObserver> observer(
558 new MockRtpReceiverObserver(receiver->media_type()));
559 receiver->SetObserver(observer.get());
560 rtp_receiver_observers_.push_back(std::move(observer));
561 }
562 }
563
Qingsi Wangecd30542019-05-22 14:34:56 -0700564 rtc::FakeNetworkManager* network_manager() const {
Steve Antonede9ca52017-10-16 13:04:27 -0700565 return fake_network_manager_.get();
566 }
567 cricket::PortAllocator* port_allocator() const { return port_allocator_; }
568
Qingsi Wang7685e862018-06-11 20:15:46 -0700569 webrtc::FakeRtcEventLogFactory* event_log_factory() const {
570 return event_log_factory_;
571 }
572
Qingsi Wangc129c352019-04-18 10:41:58 -0700573 const cricket::Candidate& last_candidate_gathered() const {
574 return last_candidate_gathered_;
575 }
Eldar Relloda13ea22019-06-01 12:23:43 +0300576 const cricket::IceCandidateErrorEvent& error_event() const {
577 return error_event_;
578 }
Qingsi Wangc129c352019-04-18 10:41:58 -0700579
Qingsi Wangecd30542019-05-22 14:34:56 -0700580 // Sets the mDNS responder for the owned fake network manager and keeps a
581 // reference to the responder.
582 void SetMdnsResponder(
583 std::unique_ptr<webrtc::FakeMdnsResponder> mdns_responder) {
584 RTC_DCHECK(mdns_responder != nullptr);
585 mdns_responder_ = mdns_responder.get();
586 network_manager()->set_mdns_responder(std::move(mdns_responder));
587 }
588
Eldar Rello5ab79e62019-10-09 18:29:44 +0300589 // Returns null on failure.
590 std::unique_ptr<SessionDescriptionInterface> CreateOfferAndWait() {
591 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
592 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
593 pc()->CreateOffer(observer, offer_answer_options_);
594 return WaitForDescriptionFromObserver(observer);
595 }
Eldar Rellod9ebe012020-03-18 20:41:45 +0200596 bool Rollback() {
597 return SetRemoteDescription(
598 webrtc::CreateSessionDescription(SdpType::kRollback, ""));
599 }
Eldar Rello5ab79e62019-10-09 18:29:44 +0300600
deadbeef1dcb1642017-03-29 21:08:16 -0700601 private:
602 explicit PeerConnectionWrapper(const std::string& debug_name)
603 : debug_name_(debug_name) {}
604
Bjorn Mellem175aa2e2018-11-08 11:23:22 -0800605 bool Init(
606 const PeerConnectionFactory::Options* options,
607 const PeerConnectionInterface::RTCConfiguration* config,
608 webrtc::PeerConnectionDependencies dependencies,
609 rtc::Thread* network_thread,
610 rtc::Thread* worker_thread,
611 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
Johannes Kron3e983682020-03-29 22:17:00 +0200612 bool reset_encoder_factory,
613 bool reset_decoder_factory) {
deadbeef1dcb1642017-03-29 21:08:16 -0700614 // There's an error in this test code if Init ends up being called twice.
615 RTC_DCHECK(!peer_connection_);
616 RTC_DCHECK(!peer_connection_factory_);
617
618 fake_network_manager_.reset(new rtc::FakeNetworkManager());
Steve Antonede9ca52017-10-16 13:04:27 -0700619 fake_network_manager_->AddInterface(kDefaultLocalAddress);
deadbeef1dcb1642017-03-29 21:08:16 -0700620
621 std::unique_ptr<cricket::PortAllocator> port_allocator(
622 new cricket::BasicPortAllocator(fake_network_manager_.get()));
Steve Antonede9ca52017-10-16 13:04:27 -0700623 port_allocator_ = port_allocator.get();
deadbeef1dcb1642017-03-29 21:08:16 -0700624 fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
625 if (!fake_audio_capture_module_) {
626 return false;
627 }
deadbeef1dcb1642017-03-29 21:08:16 -0700628 rtc::Thread* const signaling_thread = rtc::Thread::Current();
Qingsi Wang7685e862018-06-11 20:15:46 -0700629
630 webrtc::PeerConnectionFactoryDependencies pc_factory_dependencies;
631 pc_factory_dependencies.network_thread = network_thread;
632 pc_factory_dependencies.worker_thread = worker_thread;
633 pc_factory_dependencies.signaling_thread = signaling_thread;
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200634 pc_factory_dependencies.task_queue_factory =
635 webrtc::CreateDefaultTaskQueueFactory();
636 cricket::MediaEngineDependencies media_deps;
637 media_deps.task_queue_factory =
638 pc_factory_dependencies.task_queue_factory.get();
639 media_deps.adm = fake_audio_capture_module_;
640 webrtc::SetMediaEngineDefaults(&media_deps);
Johannes Kron3e983682020-03-29 22:17:00 +0200641
642 if (reset_encoder_factory) {
643 media_deps.video_encoder_factory.reset();
644 }
645 if (reset_decoder_factory) {
646 media_deps.video_decoder_factory.reset();
647 }
648
Per Ã…hgrencc73ed32020-04-26 23:56:17 +0200649 if (!media_deps.audio_processing) {
650 // If the standard Creation method for APM returns a null pointer, instead
651 // use the builder for testing to create an APM object.
652 media_deps.audio_processing = AudioProcessingBuilderForTesting().Create();
653 }
654
Qingsi Wang7685e862018-06-11 20:15:46 -0700655 pc_factory_dependencies.media_engine =
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200656 cricket::CreateMediaEngine(std::move(media_deps));
Qingsi Wang7685e862018-06-11 20:15:46 -0700657 pc_factory_dependencies.call_factory = webrtc::CreateCallFactory();
658 if (event_log_factory) {
659 event_log_factory_ = event_log_factory.get();
660 pc_factory_dependencies.event_log_factory = std::move(event_log_factory);
661 } else {
662 pc_factory_dependencies.event_log_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200663 std::make_unique<webrtc::RtcEventLogFactory>(
Danil Chapovalov9da25bd2019-06-20 10:19:42 +0200664 pc_factory_dependencies.task_queue_factory.get());
Qingsi Wang7685e862018-06-11 20:15:46 -0700665 }
666 peer_connection_factory_ = webrtc::CreateModularPeerConnectionFactory(
667 std::move(pc_factory_dependencies));
668
deadbeef1dcb1642017-03-29 21:08:16 -0700669 if (!peer_connection_factory_) {
670 return false;
671 }
672 if (options) {
673 peer_connection_factory_->SetOptions(*options);
674 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800675 if (config) {
676 sdp_semantics_ = config->sdp_semantics;
677 }
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700678
679 dependencies.allocator = std::move(port_allocator);
Niels Möllerf06f9232018-08-07 12:32:18 +0200680 peer_connection_ = CreatePeerConnection(config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700681 return peer_connection_.get() != nullptr;
682 }
683
684 rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
deadbeef1dcb1642017-03-29 21:08:16 -0700685 const PeerConnectionInterface::RTCConfiguration* config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700686 webrtc::PeerConnectionDependencies dependencies) {
deadbeef1dcb1642017-03-29 21:08:16 -0700687 PeerConnectionInterface::RTCConfiguration modified_config;
688 // If |config| is null, this will result in a default configuration being
689 // used.
690 if (config) {
691 modified_config = *config;
692 }
693 // Disable resolution adaptation; we don't want it interfering with the
694 // test results.
695 // TODO(deadbeef): Do something more robust. Since we're testing for aspect
696 // ratios and not specific resolutions, is this even necessary?
697 modified_config.set_cpu_adaptation(false);
698
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700699 dependencies.observer = this;
deadbeef1dcb1642017-03-29 21:08:16 -0700700 return peer_connection_factory_->CreatePeerConnection(
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700701 modified_config, std::move(dependencies));
deadbeef1dcb1642017-03-29 21:08:16 -0700702 }
703
704 void set_signaling_message_receiver(
705 SignalingMessageReceiver* signaling_message_receiver) {
706 signaling_message_receiver_ = signaling_message_receiver;
707 }
708
709 void set_signaling_delay_ms(int delay_ms) { signaling_delay_ms_ = delay_ms; }
710
Steve Antonede9ca52017-10-16 13:04:27 -0700711 void set_signal_ice_candidates(bool signal) {
712 signal_ice_candidates_ = signal;
713 }
714
deadbeef1dcb1642017-03-29 21:08:16 -0700715 rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrackInternal(
Niels Möller5c7efe72018-05-11 10:34:46 +0200716 webrtc::FakePeriodicVideoSource::Config config) {
deadbeef1dcb1642017-03-29 21:08:16 -0700717 // Set max frame rate to 10fps to reduce the risk of test flakiness.
718 // TODO(deadbeef): Do something more robust.
Niels Möller5c7efe72018-05-11 10:34:46 +0200719 config.frame_interval_ms = 100;
deadbeef1dcb1642017-03-29 21:08:16 -0700720
Niels Möller5c7efe72018-05-11 10:34:46 +0200721 video_track_sources_.emplace_back(
Niels Möller0f405822018-05-17 09:16:41 +0200722 new rtc::RefCountedObject<webrtc::FakePeriodicVideoTrackSource>(
723 config, false /* remote */));
deadbeef1dcb1642017-03-29 21:08:16 -0700724 rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
Niels Möller5c7efe72018-05-11 10:34:46 +0200725 peer_connection_factory_->CreateVideoTrack(
726 rtc::CreateRandomUuid(), video_track_sources_.back()));
deadbeef1dcb1642017-03-29 21:08:16 -0700727 if (!local_video_renderer_) {
728 local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
729 }
730 return track;
731 }
732
733 void HandleIncomingOffer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100734 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingOffer";
Steve Antona3a92c22017-12-07 10:27:41 -0800735 std::unique_ptr<SessionDescriptionInterface> desc =
736 webrtc::CreateSessionDescription(SdpType::kOffer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700737 if (received_sdp_munger_) {
738 received_sdp_munger_(desc->description());
739 }
740
741 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
742 // Setting a remote description may have changed the number of receivers,
743 // so reset the receiver observers.
744 ResetRtpReceiverObservers();
Seth Hampson2f0d7022018-02-20 11:54:42 -0800745 if (remote_offer_handler_) {
746 remote_offer_handler_();
747 }
deadbeef1dcb1642017-03-29 21:08:16 -0700748 auto answer = CreateAnswer();
749 ASSERT_NE(nullptr, answer);
750 EXPECT_TRUE(SetLocalDescriptionAndSendSdpMessage(std::move(answer)));
751 }
752
753 void HandleIncomingAnswer(const std::string& msg) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100754 RTC_LOG(LS_INFO) << debug_name_ << ": HandleIncomingAnswer";
Steve Antona3a92c22017-12-07 10:27:41 -0800755 std::unique_ptr<SessionDescriptionInterface> desc =
756 webrtc::CreateSessionDescription(SdpType::kAnswer, msg);
deadbeef1dcb1642017-03-29 21:08:16 -0700757 if (received_sdp_munger_) {
758 received_sdp_munger_(desc->description());
759 }
760
761 EXPECT_TRUE(SetRemoteDescription(std::move(desc)));
762 // Set the RtpReceiverObserver after receivers are created.
763 ResetRtpReceiverObservers();
764 }
765
766 // Returns null on failure.
deadbeef1dcb1642017-03-29 21:08:16 -0700767 std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
768 rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
769 new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
770 pc()->CreateAnswer(observer, offer_answer_options_);
771 return WaitForDescriptionFromObserver(observer);
772 }
773
774 std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +0100775 MockCreateSessionDescriptionObserver* observer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700776 EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
777 if (!observer->result()) {
778 return nullptr;
779 }
780 auto description = observer->MoveDescription();
781 if (generated_sdp_munger_) {
782 generated_sdp_munger_(description->description());
783 }
784 return description;
785 }
786
787 // Setting the local description and sending the SDP message over the fake
788 // signaling channel are combined into the same method because the SDP
789 // message needs to be sent as soon as SetLocalDescription finishes, without
790 // waiting for the observer to be called. This ensures that ICE candidates
791 // don't outrace the description.
792 bool SetLocalDescriptionAndSendSdpMessage(
793 std::unique_ptr<SessionDescriptionInterface> desc) {
794 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
795 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100796 RTC_LOG(LS_INFO) << debug_name_ << ": SetLocalDescriptionAndSendSdpMessage";
Steve Antona3a92c22017-12-07 10:27:41 -0800797 SdpType type = desc->GetType();
deadbeef1dcb1642017-03-29 21:08:16 -0700798 std::string sdp;
799 EXPECT_TRUE(desc->ToString(&sdp));
Bjorn A Mellemb689af42019-08-21 10:44:59 -0700800 RTC_LOG(LS_INFO) << debug_name_ << ": local SDP contents=\n" << sdp;
deadbeef1dcb1642017-03-29 21:08:16 -0700801 pc()->SetLocalDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800802 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
803 RemoveUnusedVideoRenderers();
804 }
deadbeef1dcb1642017-03-29 21:08:16 -0700805 // As mentioned above, we need to send the message immediately after
806 // SetLocalDescription.
807 SendSdpMessage(type, sdp);
808 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
809 return true;
810 }
811
812 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
813 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
814 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100815 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700816 pc()->SetRemoteDescription(observer, desc.release());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800817 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
818 RemoveUnusedVideoRenderers();
819 }
deadbeef1dcb1642017-03-29 21:08:16 -0700820 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
821 return observer->result();
822 }
823
Seth Hampson2f0d7022018-02-20 11:54:42 -0800824 // This is a work around to remove unused fake_video_renderers from
825 // transceivers that have either stopped or are no longer receiving.
826 void RemoveUnusedVideoRenderers() {
827 auto transceivers = pc()->GetTransceivers();
828 for (auto& transceiver : transceivers) {
829 if (transceiver->receiver()->media_type() != cricket::MEDIA_TYPE_VIDEO) {
830 continue;
831 }
832 // Remove fake video renderers from any stopped transceivers.
833 if (transceiver->stopped()) {
834 auto it =
835 fake_video_renderers_.find(transceiver->receiver()->track()->id());
836 if (it != fake_video_renderers_.end()) {
837 fake_video_renderers_.erase(it);
838 }
839 }
840 // Remove fake video renderers from any transceivers that are no longer
841 // receiving.
842 if ((transceiver->current_direction() &&
843 !webrtc::RtpTransceiverDirectionHasRecv(
844 *transceiver->current_direction()))) {
845 auto it =
846 fake_video_renderers_.find(transceiver->receiver()->track()->id());
847 if (it != fake_video_renderers_.end()) {
848 fake_video_renderers_.erase(it);
849 }
850 }
851 }
852 }
853
deadbeef1dcb1642017-03-29 21:08:16 -0700854 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
855 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800856 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700857 if (signaling_delay_ms_ == 0) {
858 RelaySdpMessageIfReceiverExists(type, msg);
859 } else {
860 invoker_.AsyncInvokeDelayed<void>(
861 RTC_FROM_HERE, rtc::Thread::Current(),
862 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
863 this, type, msg),
864 signaling_delay_ms_);
865 }
866 }
867
Steve Antona3a92c22017-12-07 10:27:41 -0800868 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700869 if (signaling_message_receiver_) {
870 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
871 }
872 }
873
874 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
875 // default).
876 void SendIceMessage(const std::string& sdp_mid,
877 int sdp_mline_index,
878 const std::string& msg) {
879 if (signaling_delay_ms_ == 0) {
880 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
881 } else {
882 invoker_.AsyncInvokeDelayed<void>(
883 RTC_FROM_HERE, rtc::Thread::Current(),
884 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
885 this, sdp_mid, sdp_mline_index, msg),
886 signaling_delay_ms_);
887 }
888 }
889
890 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
891 int sdp_mline_index,
892 const std::string& msg) {
893 if (signaling_message_receiver_) {
894 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
895 msg);
896 }
897 }
898
899 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800900 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
901 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700902 HandleIncomingOffer(msg);
903 } else {
904 HandleIncomingAnswer(msg);
905 }
906 }
907
908 void ReceiveIceMessage(const std::string& sdp_mid,
909 int sdp_mline_index,
910 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100911 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700912 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
913 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
914 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
915 }
916
917 // PeerConnectionObserver callbacks.
918 void OnSignalingChange(
919 webrtc::PeerConnectionInterface::SignalingState new_state) override {
920 EXPECT_EQ(pc()->signaling_state(), new_state);
Eldar Rello5ab79e62019-10-09 18:29:44 +0300921 peer_connection_signaling_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700922 }
Steve Anton15324772018-01-16 10:26:49 -0800923 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
924 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
925 streams) override {
926 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
927 rtc::scoped_refptr<VideoTrackInterface> video_track(
928 static_cast<VideoTrackInterface*>(receiver->track().get()));
929 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700930 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800931 fake_video_renderers_[video_track->id()] =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200932 std::make_unique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700933 }
934 }
Steve Anton15324772018-01-16 10:26:49 -0800935 void OnRemoveTrack(
936 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
937 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
938 auto it = fake_video_renderers_.find(receiver->track()->id());
939 RTC_DCHECK(it != fake_video_renderers_.end());
940 fake_video_renderers_.erase(it);
941 }
942 }
deadbeef1dcb1642017-03-29 21:08:16 -0700943 void OnRenegotiationNeeded() override {}
944 void OnIceConnectionChange(
945 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
946 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700947 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700948 }
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100949 void OnStandardizedIceConnectionChange(
950 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
951 standardized_ice_connection_state_history_.push_back(new_state);
952 }
Jonas Olsson635474e2018-10-18 15:58:17 +0200953 void OnConnectionChange(
954 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
955 peer_connection_state_history_.push_back(new_state);
956 }
957
deadbeef1dcb1642017-03-29 21:08:16 -0700958 void OnIceGatheringChange(
959 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700960 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700961 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700962 }
Alex Drake00c7ecf2019-08-06 10:54:47 -0700963
964 void OnIceSelectedCandidatePairChanged(
965 const cricket::CandidatePairChangeEvent& event) {
966 ice_candidate_pair_change_history_.push_back(event);
967 }
Alex Drake43faee02019-08-12 16:27:34 -0700968
deadbeef1dcb1642017-03-29 21:08:16 -0700969 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100970 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700971
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800972 if (remote_async_resolver_) {
973 const auto& local_candidate = candidate->candidate();
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800974 if (local_candidate.address().IsUnresolvedIP()) {
975 RTC_DCHECK(local_candidate.type() == cricket::LOCAL_PORT_TYPE);
976 rtc::SocketAddress resolved_addr(local_candidate.address());
Qingsi Wangecd30542019-05-22 14:34:56 -0700977 const auto resolved_ip = mdns_responder_->GetMappedAddressForName(
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800978 local_candidate.address().hostname());
979 RTC_DCHECK(!resolved_ip.IsNil());
980 resolved_addr.SetResolvedIP(resolved_ip);
981 EXPECT_CALL(*remote_async_resolver_, GetResolvedAddress(_, _))
982 .WillOnce(DoAll(SetArgPointee<1>(resolved_addr), Return(true)));
983 EXPECT_CALL(*remote_async_resolver_, Destroy(_));
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700984 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700985 }
986
deadbeef1dcb1642017-03-29 21:08:16 -0700987 std::string ice_sdp;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800988 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700989 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700990 // Remote party may be deleted.
991 return;
992 }
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800993 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
Qingsi Wangc129c352019-04-18 10:41:58 -0700994 last_candidate_gathered_ = candidate->candidate();
deadbeef1dcb1642017-03-29 21:08:16 -0700995 }
Eldar Rello0095d372019-12-02 22:22:07 +0200996 void OnIceCandidateError(const std::string& address,
997 int port,
Eldar Relloda13ea22019-06-01 12:23:43 +0300998 const std::string& url,
999 int error_code,
1000 const std::string& error_text) override {
Eldar Rello0095d372019-12-02 22:22:07 +02001001 error_event_ = cricket::IceCandidateErrorEvent(address, port, url,
Eldar Relloda13ea22019-06-01 12:23:43 +03001002 error_code, error_text);
1003 }
deadbeef1dcb1642017-03-29 21:08:16 -07001004 void OnDataChannel(
1005 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001006 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -07001007 data_channel_ = data_channel;
1008 data_observer_.reset(new MockDataChannelObserver(data_channel));
1009 }
1010
deadbeef1dcb1642017-03-29 21:08:16 -07001011 std::string debug_name_;
1012
1013 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
Qingsi Wangecd30542019-05-22 14:34:56 -07001014 // Reference to the mDNS responder owned by |fake_network_manager_| after set.
1015 webrtc::FakeMdnsResponder* mdns_responder_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -07001016
1017 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
1018 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
1019 peer_connection_factory_;
1020
Steve Antonede9ca52017-10-16 13:04:27 -07001021 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -07001022 // Needed to keep track of number of frames sent.
1023 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
1024 // Needed to keep track of number of frames received.
1025 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1026 fake_video_renderers_;
1027 // Needed to ensure frames aren't received for removed tracks.
1028 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1029 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -07001030
1031 // For remote peer communication.
1032 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
1033 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -07001034 bool signal_ice_candidates_ = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07001035 cricket::Candidate last_candidate_gathered_;
Eldar Relloda13ea22019-06-01 12:23:43 +03001036 cricket::IceCandidateErrorEvent error_event_;
deadbeef1dcb1642017-03-29 21:08:16 -07001037
Niels Möller5c7efe72018-05-11 10:34:46 +02001038 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -07001039 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +02001040 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
1041 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -07001042 // |local_video_renderer_| attached to the first created local video track.
1043 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
1044
Seth Hampson2f0d7022018-02-20 11:54:42 -08001045 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07001046 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
1047 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
1048 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001049 std::function<void()> remote_offer_handler_;
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001050 rtc::MockAsyncResolver* remote_async_resolver_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -07001051 rtc::scoped_refptr<DataChannelInterface> data_channel_;
1052 std::unique_ptr<MockDataChannelObserver> data_observer_;
1053
1054 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
1055
Steve Antonede9ca52017-10-16 13:04:27 -07001056 std::vector<PeerConnectionInterface::IceConnectionState>
1057 ice_connection_state_history_;
Jonas Olssonacd8ae72019-02-25 15:26:24 +01001058 std::vector<PeerConnectionInterface::IceConnectionState>
1059 standardized_ice_connection_state_history_;
Jonas Olsson635474e2018-10-18 15:58:17 +02001060 std::vector<PeerConnectionInterface::PeerConnectionState>
1061 peer_connection_state_history_;
Steve Antonede9ca52017-10-16 13:04:27 -07001062 std::vector<PeerConnectionInterface::IceGatheringState>
1063 ice_gathering_state_history_;
Alex Drake00c7ecf2019-08-06 10:54:47 -07001064 std::vector<cricket::CandidatePairChangeEvent>
1065 ice_candidate_pair_change_history_;
Eldar Rello5ab79e62019-10-09 18:29:44 +03001066 std::vector<PeerConnectionInterface::SignalingState>
1067 peer_connection_signaling_state_history_;
Qingsi Wang7685e862018-06-11 20:15:46 -07001068 webrtc::FakeRtcEventLogFactory* event_log_factory_;
1069
deadbeef1dcb1642017-03-29 21:08:16 -07001070 rtc::AsyncInvoker invoker_;
1071
Seth Hampson2f0d7022018-02-20 11:54:42 -08001072 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -07001073};
1074
Elad Alon99c3fe52017-10-13 16:29:40 +02001075class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
1076 public:
1077 virtual ~MockRtcEventLogOutput() = default;
Danil Chapovalov3a353122020-05-15 11:16:53 +02001078 MOCK_METHOD(bool, IsActive, (), (const, override));
1079 MOCK_METHOD(bool, Write, (const std::string&), (override));
Elad Alon99c3fe52017-10-13 16:29:40 +02001080};
1081
Seth Hampson2f0d7022018-02-20 11:54:42 -08001082// This helper object is used for both specifying how many audio/video frames
1083// are expected to be received for a caller/callee. It provides helper functions
1084// to specify these expectations. The object initially starts in a state of no
1085// expectations.
1086class MediaExpectations {
1087 public:
1088 enum ExpectFrames {
1089 kExpectSomeFrames,
1090 kExpectNoFrames,
1091 kNoExpectation,
1092 };
1093
1094 void ExpectBidirectionalAudioAndVideo() {
1095 ExpectBidirectionalAudio();
1096 ExpectBidirectionalVideo();
1097 }
1098
1099 void ExpectBidirectionalAudio() {
1100 CallerExpectsSomeAudio();
1101 CalleeExpectsSomeAudio();
1102 }
1103
1104 void ExpectNoAudio() {
1105 CallerExpectsNoAudio();
1106 CalleeExpectsNoAudio();
1107 }
1108
1109 void ExpectBidirectionalVideo() {
1110 CallerExpectsSomeVideo();
1111 CalleeExpectsSomeVideo();
1112 }
1113
1114 void ExpectNoVideo() {
1115 CallerExpectsNoVideo();
1116 CalleeExpectsNoVideo();
1117 }
1118
1119 void CallerExpectsSomeAudioAndVideo() {
1120 CallerExpectsSomeAudio();
1121 CallerExpectsSomeVideo();
1122 }
1123
1124 void CalleeExpectsSomeAudioAndVideo() {
1125 CalleeExpectsSomeAudio();
1126 CalleeExpectsSomeVideo();
1127 }
1128
1129 // Caller's audio functions.
1130 void CallerExpectsSomeAudio(
1131 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1132 caller_audio_expectation_ = kExpectSomeFrames;
1133 caller_audio_frames_expected_ = expected_audio_frames;
1134 }
1135
1136 void CallerExpectsNoAudio() {
1137 caller_audio_expectation_ = kExpectNoFrames;
1138 caller_audio_frames_expected_ = 0;
1139 }
1140
1141 // Caller's video functions.
1142 void CallerExpectsSomeVideo(
1143 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1144 caller_video_expectation_ = kExpectSomeFrames;
1145 caller_video_frames_expected_ = expected_video_frames;
1146 }
1147
1148 void CallerExpectsNoVideo() {
1149 caller_video_expectation_ = kExpectNoFrames;
1150 caller_video_frames_expected_ = 0;
1151 }
1152
1153 // Callee's audio functions.
1154 void CalleeExpectsSomeAudio(
1155 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1156 callee_audio_expectation_ = kExpectSomeFrames;
1157 callee_audio_frames_expected_ = expected_audio_frames;
1158 }
1159
1160 void CalleeExpectsNoAudio() {
1161 callee_audio_expectation_ = kExpectNoFrames;
1162 callee_audio_frames_expected_ = 0;
1163 }
1164
1165 // Callee's video functions.
1166 void CalleeExpectsSomeVideo(
1167 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1168 callee_video_expectation_ = kExpectSomeFrames;
1169 callee_video_frames_expected_ = expected_video_frames;
1170 }
1171
1172 void CalleeExpectsNoVideo() {
1173 callee_video_expectation_ = kExpectNoFrames;
1174 callee_video_frames_expected_ = 0;
1175 }
1176
1177 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1178 ExpectFrames caller_video_expectation_ = kNoExpectation;
1179 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1180 ExpectFrames callee_video_expectation_ = kNoExpectation;
1181 int caller_audio_frames_expected_ = 0;
1182 int caller_video_frames_expected_ = 0;
1183 int callee_audio_frames_expected_ = 0;
1184 int callee_video_frames_expected_ = 0;
1185};
1186
Qingsi Wang25ec8882019-11-15 12:33:05 -08001187class MockIceTransport : public webrtc::IceTransportInterface {
1188 public:
1189 MockIceTransport(const std::string& name, int component)
1190 : internal_(std::make_unique<cricket::FakeIceTransport>(
1191 name,
1192 component,
1193 nullptr /* network_thread */)) {}
1194 ~MockIceTransport() = default;
1195 cricket::IceTransportInternal* internal() { return internal_.get(); }
1196
1197 private:
1198 std::unique_ptr<cricket::FakeIceTransport> internal_;
1199};
1200
1201class MockIceTransportFactory : public IceTransportFactory {
1202 public:
1203 ~MockIceTransportFactory() override = default;
1204 rtc::scoped_refptr<IceTransportInterface> CreateIceTransport(
1205 const std::string& transport_name,
1206 int component,
1207 IceTransportInit init) {
1208 RecordIceTransportCreated();
1209 return new rtc::RefCountedObject<MockIceTransport>(transport_name,
1210 component);
1211 }
Danil Chapovalov3a353122020-05-15 11:16:53 +02001212 MOCK_METHOD(void, RecordIceTransportCreated, ());
Qingsi Wang25ec8882019-11-15 12:33:05 -08001213};
1214
deadbeef1dcb1642017-03-29 21:08:16 -07001215// Tests two PeerConnections connecting to each other end-to-end, using a
1216// virtual network, fake A/V capture and fake encoder/decoders. The
1217// PeerConnections share the threads/socket servers, but use separate versions
1218// of everything else (including "PeerConnectionFactory"s).
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001219class PeerConnectionIntegrationBaseTest : public ::testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001220 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001221 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1222 : sdp_semantics_(sdp_semantics),
1223 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001224 fss_(new rtc::FirewallSocketServer(ss_.get())),
1225 network_thread_(new rtc::Thread(fss_.get())),
Niels Möller2a707032020-06-16 16:39:13 +02001226 worker_thread_(rtc::Thread::Create()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001227 network_thread_->SetName("PCNetworkThread", this);
1228 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001229 RTC_CHECK(network_thread_->Start());
1230 RTC_CHECK(worker_thread_->Start());
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001231 webrtc::metrics::Reset();
deadbeef1dcb1642017-03-29 21:08:16 -07001232 }
1233
Seth Hampson2f0d7022018-02-20 11:54:42 -08001234 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001235 // The PeerConnections should deleted before the TurnCustomizers.
1236 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1237 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1238 // that the TurnCustomizer outlives the life of the PeerConnection or else
1239 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001240 if (caller_) {
1241 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001242 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001243 }
1244 if (callee_) {
1245 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001246 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001247 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001248
1249 // If turn servers were created for the test they need to be destroyed on
1250 // the network thread.
1251 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1252 turn_servers_.clear();
1253 turn_customizers_.clear();
1254 });
deadbeef1dcb1642017-03-29 21:08:16 -07001255 }
1256
1257 bool SignalingStateStable() {
1258 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1259 }
1260
deadbeef71452802017-05-07 17:21:01 -07001261 bool DtlsConnected() {
Alex Loiko9289eda2018-11-23 16:18:59 +00001262 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1263 // are connected. This is an important distinction. Once we have separate
1264 // ICE and DTLS state, this check needs to use the DTLS state.
1265 return (callee()->ice_connection_state() ==
1266 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1267 callee()->ice_connection_state() ==
1268 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1269 (caller()->ice_connection_state() ==
1270 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1271 caller()->ice_connection_state() ==
1272 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
deadbeef71452802017-05-07 17:21:01 -07001273 }
1274
Qingsi Wang7685e862018-06-11 20:15:46 -07001275 // When |event_log_factory| is null, the default implementation of the event
1276 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001277 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1278 const std::string& debug_name,
Seth Hampson2f0d7022018-02-20 11:54:42 -08001279 const PeerConnectionFactory::Options* options,
1280 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001281 webrtc::PeerConnectionDependencies dependencies,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001282 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
Johannes Kron3e983682020-03-29 22:17:00 +02001283 bool reset_encoder_factory,
1284 bool reset_decoder_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001285 RTCConfiguration modified_config;
1286 if (config) {
1287 modified_config = *config;
1288 }
Steve Anton3acffc32018-04-12 17:21:03 -07001289 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001290 if (!dependencies.cert_generator) {
1291 dependencies.cert_generator =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001292 std::make_unique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001293 }
1294 std::unique_ptr<PeerConnectionWrapper> client(
1295 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001296
Niels Möllerf06f9232018-08-07 12:32:18 +02001297 if (!client->Init(options, &modified_config, std::move(dependencies),
1298 network_thread_.get(), worker_thread_.get(),
Niels Möller2a707032020-06-16 16:39:13 +02001299 std::move(event_log_factory), reset_encoder_factory,
Johannes Kron3e983682020-03-29 22:17:00 +02001300 reset_decoder_factory)) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001301 return nullptr;
1302 }
1303 return client;
1304 }
1305
Qingsi Wang7685e862018-06-11 20:15:46 -07001306 std::unique_ptr<PeerConnectionWrapper>
1307 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1308 const std::string& debug_name,
Qingsi Wang7685e862018-06-11 20:15:46 -07001309 const PeerConnectionFactory::Options* options,
1310 const RTCConfiguration* config,
1311 webrtc::PeerConnectionDependencies dependencies) {
1312 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1313 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
Niels Möller2a707032020-06-16 16:39:13 +02001314 return CreatePeerConnectionWrapper(debug_name, options, config,
1315 std::move(dependencies),
1316 std::move(event_log_factory),
1317 /*reset_encoder_factory=*/false,
1318 /*reset_decoder_factory=*/false);
Qingsi Wang7685e862018-06-11 20:15:46 -07001319 }
1320
deadbeef1dcb1642017-03-29 21:08:16 -07001321 bool CreatePeerConnectionWrappers() {
1322 return CreatePeerConnectionWrappersWithConfig(
1323 PeerConnectionInterface::RTCConfiguration(),
1324 PeerConnectionInterface::RTCConfiguration());
1325 }
1326
Steve Anton3acffc32018-04-12 17:21:03 -07001327 bool CreatePeerConnectionWrappersWithSdpSemantics(
1328 SdpSemantics caller_semantics,
1329 SdpSemantics callee_semantics) {
1330 // Can't specify the sdp_semantics in the passed-in configuration since it
1331 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1332 // stored in sdp_semantics_. So get around this by modifying the instance
1333 // variable before calling CreatePeerConnectionWrapper for the caller and
1334 // callee PeerConnections.
1335 SdpSemantics original_semantics = sdp_semantics_;
1336 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001337 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001338 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Niels Möller2a707032020-06-16 16:39:13 +02001339 nullptr,
Johannes Kron3e983682020-03-29 22:17:00 +02001340 /*reset_encoder_factory=*/false,
1341 /*reset_decoder_factory=*/false);
Steve Anton3acffc32018-04-12 17:21:03 -07001342 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001343 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001344 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Niels Möller2a707032020-06-16 16:39:13 +02001345 nullptr,
Johannes Kron3e983682020-03-29 22:17:00 +02001346 /*reset_encoder_factory=*/false,
1347 /*reset_decoder_factory=*/false);
Steve Anton3acffc32018-04-12 17:21:03 -07001348 sdp_semantics_ = original_semantics;
1349 return caller_ && callee_;
1350 }
1351
deadbeef1dcb1642017-03-29 21:08:16 -07001352 bool CreatePeerConnectionWrappersWithConfig(
1353 const PeerConnectionInterface::RTCConfiguration& caller_config,
1354 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001355 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001356 "Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001357 webrtc::PeerConnectionDependencies(nullptr), nullptr,
Niels Möller2a707032020-06-16 16:39:13 +02001358 /*reset_encoder_factory=*/false,
Johannes Kron3e983682020-03-29 22:17:00 +02001359 /*reset_decoder_factory=*/false);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001360 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001361 "Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001362 webrtc::PeerConnectionDependencies(nullptr), nullptr,
Niels Möller2a707032020-06-16 16:39:13 +02001363 /*reset_encoder_factory=*/false,
Johannes Kron3e983682020-03-29 22:17:00 +02001364 /*reset_decoder_factory=*/false);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001365 return caller_ && callee_;
1366 }
1367
1368 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1369 const PeerConnectionInterface::RTCConfiguration& caller_config,
1370 webrtc::PeerConnectionDependencies caller_dependencies,
1371 const PeerConnectionInterface::RTCConfiguration& callee_config,
1372 webrtc::PeerConnectionDependencies callee_dependencies) {
Niels Möller2a707032020-06-16 16:39:13 +02001373 caller_ =
1374 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
1375 std::move(caller_dependencies), nullptr,
1376 /*reset_encoder_factory=*/false,
1377 /*reset_decoder_factory=*/false);
1378 callee_ =
1379 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
1380 std::move(callee_dependencies), nullptr,
1381 /*reset_encoder_factory=*/false,
1382 /*reset_decoder_factory=*/false);
deadbeef1dcb1642017-03-29 21:08:16 -07001383 return caller_ && callee_;
1384 }
1385
1386 bool CreatePeerConnectionWrappersWithOptions(
1387 const PeerConnectionFactory::Options& caller_options,
1388 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001389 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001390 "Caller", &caller_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001391 webrtc::PeerConnectionDependencies(nullptr), nullptr,
Niels Möller2a707032020-06-16 16:39:13 +02001392 /*reset_encoder_factory=*/false,
Johannes Kron3e983682020-03-29 22:17:00 +02001393 /*reset_decoder_factory=*/false);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001394 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001395 "Callee", &callee_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001396 webrtc::PeerConnectionDependencies(nullptr), nullptr,
Niels Möller2a707032020-06-16 16:39:13 +02001397 /*reset_encoder_factory=*/false,
Johannes Kron3e983682020-03-29 22:17:00 +02001398 /*reset_decoder_factory=*/false);
Qingsi Wang7685e862018-06-11 20:15:46 -07001399 return caller_ && callee_;
1400 }
1401
1402 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1403 PeerConnectionInterface::RTCConfiguration default_config;
1404 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001405 "Caller", nullptr, &default_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001406 webrtc::PeerConnectionDependencies(nullptr));
1407 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001408 "Callee", nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001409 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001410 return caller_ && callee_;
1411 }
1412
Seth Hampson2f0d7022018-02-20 11:54:42 -08001413 std::unique_ptr<PeerConnectionWrapper>
1414 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001415 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1416 new FakeRTCCertificateGenerator());
1417 cert_generator->use_alternate_key();
1418
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001419 webrtc::PeerConnectionDependencies dependencies(nullptr);
1420 dependencies.cert_generator = std::move(cert_generator);
Niels Möller2a707032020-06-16 16:39:13 +02001421 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
1422 std::move(dependencies), nullptr,
1423 /*reset_encoder_factory=*/false,
1424 /*reset_decoder_factory=*/false);
Johannes Kron3e983682020-03-29 22:17:00 +02001425 }
1426
1427 bool CreateOneDirectionalPeerConnectionWrappers(bool caller_to_callee) {
1428 caller_ = CreatePeerConnectionWrapper(
1429 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Niels Möller2a707032020-06-16 16:39:13 +02001430 nullptr,
Johannes Kron3e983682020-03-29 22:17:00 +02001431 /*reset_encoder_factory=*/!caller_to_callee,
1432 /*reset_decoder_factory=*/caller_to_callee);
1433 callee_ = CreatePeerConnectionWrapper(
1434 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Niels Möller2a707032020-06-16 16:39:13 +02001435 nullptr,
Johannes Kron3e983682020-03-29 22:17:00 +02001436 /*reset_encoder_factory=*/caller_to_callee,
1437 /*reset_decoder_factory=*/!caller_to_callee);
1438 return caller_ && callee_;
deadbeef1dcb1642017-03-29 21:08:16 -07001439 }
1440
Seth Hampsonaed71642018-06-11 07:41:32 -07001441 cricket::TestTurnServer* CreateTurnServer(
1442 rtc::SocketAddress internal_address,
1443 rtc::SocketAddress external_address,
1444 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1445 const std::string& common_name = "test turn server") {
1446 rtc::Thread* thread = network_thread();
1447 std::unique_ptr<cricket::TestTurnServer> turn_server =
1448 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1449 RTC_FROM_HERE,
1450 [thread, internal_address, external_address, type, common_name] {
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001451 return std::make_unique<cricket::TestTurnServer>(
Seth Hampsonaed71642018-06-11 07:41:32 -07001452 thread, internal_address, external_address, type,
1453 /*ignore_bad_certs=*/true, common_name);
1454 });
1455 turn_servers_.push_back(std::move(turn_server));
1456 // Interactions with the turn server should be done on the network thread.
1457 return turn_servers_.back().get();
1458 }
1459
1460 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1461 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1462 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1463 RTC_FROM_HERE,
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001464 [] { return std::make_unique<cricket::TestTurnCustomizer>(); });
Seth Hampsonaed71642018-06-11 07:41:32 -07001465 turn_customizers_.push_back(std::move(turn_customizer));
1466 // Interactions with the turn customizer should be done on the network
1467 // thread.
1468 return turn_customizers_.back().get();
1469 }
1470
1471 // Checks that the function counters for a TestTurnCustomizer are greater than
1472 // 0.
1473 void ExpectTurnCustomizerCountersIncremented(
1474 cricket::TestTurnCustomizer* turn_customizer) {
1475 unsigned int allow_channel_data_counter =
1476 network_thread()->Invoke<unsigned int>(
1477 RTC_FROM_HERE, [turn_customizer] {
1478 return turn_customizer->allow_channel_data_cnt_;
1479 });
1480 EXPECT_GT(allow_channel_data_counter, 0u);
1481 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1482 RTC_FROM_HERE,
1483 [turn_customizer] { return turn_customizer->modify_cnt_; });
1484 EXPECT_GT(modify_counter, 0u);
1485 }
1486
deadbeef1dcb1642017-03-29 21:08:16 -07001487 // Once called, SDP blobs and ICE candidates will be automatically signaled
1488 // between PeerConnections.
1489 void ConnectFakeSignaling() {
1490 caller_->set_signaling_message_receiver(callee_.get());
1491 callee_->set_signaling_message_receiver(caller_.get());
1492 }
1493
Steve Antonede9ca52017-10-16 13:04:27 -07001494 // Once called, SDP blobs will be automatically signaled between
1495 // PeerConnections. Note that ICE candidates will not be signaled unless they
1496 // are in the exchanged SDP blobs.
1497 void ConnectFakeSignalingForSdpOnly() {
1498 ConnectFakeSignaling();
1499 SetSignalIceCandidates(false);
1500 }
1501
deadbeef1dcb1642017-03-29 21:08:16 -07001502 void SetSignalingDelayMs(int delay_ms) {
1503 caller_->set_signaling_delay_ms(delay_ms);
1504 callee_->set_signaling_delay_ms(delay_ms);
1505 }
1506
Steve Antonede9ca52017-10-16 13:04:27 -07001507 void SetSignalIceCandidates(bool signal) {
1508 caller_->set_signal_ice_candidates(signal);
1509 callee_->set_signal_ice_candidates(signal);
1510 }
1511
deadbeef1dcb1642017-03-29 21:08:16 -07001512 // Messages may get lost on the unreliable DataChannel, so we send multiple
1513 // times to avoid test flakiness.
1514 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1515 const std::string& data,
1516 int retries) {
1517 for (int i = 0; i < retries; ++i) {
1518 dc->Send(DataBuffer(data));
1519 }
1520 }
1521
1522 rtc::Thread* network_thread() { return network_thread_.get(); }
1523
1524 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1525
1526 PeerConnectionWrapper* caller() { return caller_.get(); }
1527
1528 // Set the |caller_| to the |wrapper| passed in and return the
1529 // original |caller_|.
1530 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1531 PeerConnectionWrapper* wrapper) {
1532 PeerConnectionWrapper* old = caller_.release();
1533 caller_.reset(wrapper);
1534 return old;
1535 }
1536
1537 PeerConnectionWrapper* callee() { return callee_.get(); }
1538
1539 // Set the |callee_| to the |wrapper| passed in and return the
1540 // original |callee_|.
1541 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1542 PeerConnectionWrapper* wrapper) {
1543 PeerConnectionWrapper* old = callee_.release();
1544 callee_.reset(wrapper);
1545 return old;
1546 }
1547
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001548 void SetPortAllocatorFlags(uint32_t caller_flags, uint32_t callee_flags) {
1549 network_thread()->Invoke<void>(
1550 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1551 caller()->port_allocator(), caller_flags));
1552 network_thread()->Invoke<void>(
1553 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1554 callee()->port_allocator(), callee_flags));
1555 }
1556
Steve Antonede9ca52017-10-16 13:04:27 -07001557 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1558
Seth Hampson2f0d7022018-02-20 11:54:42 -08001559 // Expects the provided number of new frames to be received within
1560 // kMaxWaitForFramesMs. The new expected frames are specified in
1561 // |media_expectations|. Returns false if any of the expectations were
1562 // not met.
1563 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
1564 // First initialize the expected frame counts based upon the current
1565 // frame count.
1566 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1567 if (media_expectations.caller_audio_expectation_ ==
1568 MediaExpectations::kExpectSomeFrames) {
1569 total_caller_audio_frames_expected +=
1570 media_expectations.caller_audio_frames_expected_;
1571 }
1572 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001573 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001574 if (media_expectations.caller_video_expectation_ ==
1575 MediaExpectations::kExpectSomeFrames) {
1576 total_caller_video_frames_expected +=
1577 media_expectations.caller_video_frames_expected_;
1578 }
1579 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1580 if (media_expectations.callee_audio_expectation_ ==
1581 MediaExpectations::kExpectSomeFrames) {
1582 total_callee_audio_frames_expected +=
1583 media_expectations.callee_audio_frames_expected_;
1584 }
1585 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001586 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001587 if (media_expectations.callee_video_expectation_ ==
1588 MediaExpectations::kExpectSomeFrames) {
1589 total_callee_video_frames_expected +=
1590 media_expectations.callee_video_frames_expected_;
1591 }
deadbeef1dcb1642017-03-29 21:08:16 -07001592
Seth Hampson2f0d7022018-02-20 11:54:42 -08001593 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001594 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001595 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001596 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001597 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001598 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001599 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001600 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001601 total_callee_video_frames_expected,
1602 kMaxWaitForFramesMs);
1603 bool expectations_correct =
1604 caller()->audio_frames_received() >=
1605 total_caller_audio_frames_expected &&
1606 caller()->min_video_frames_received_per_track() >=
1607 total_caller_video_frames_expected &&
1608 callee()->audio_frames_received() >=
1609 total_callee_audio_frames_expected &&
1610 callee()->min_video_frames_received_per_track() >=
1611 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001612
Seth Hampson2f0d7022018-02-20 11:54:42 -08001613 // After the combined wait, print out a more detailed message upon
1614 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001615 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001616 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001617 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001618 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001619 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001620 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001621 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001622 total_callee_video_frames_expected);
1623
1624 // We want to make sure nothing unexpected was received.
1625 if (media_expectations.caller_audio_expectation_ ==
1626 MediaExpectations::kExpectNoFrames) {
1627 EXPECT_EQ(caller()->audio_frames_received(),
1628 total_caller_audio_frames_expected);
1629 if (caller()->audio_frames_received() !=
1630 total_caller_audio_frames_expected) {
1631 expectations_correct = false;
1632 }
1633 }
1634 if (media_expectations.caller_video_expectation_ ==
1635 MediaExpectations::kExpectNoFrames) {
1636 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1637 total_caller_video_frames_expected);
1638 if (caller()->min_video_frames_received_per_track() !=
1639 total_caller_video_frames_expected) {
1640 expectations_correct = false;
1641 }
1642 }
1643 if (media_expectations.callee_audio_expectation_ ==
1644 MediaExpectations::kExpectNoFrames) {
1645 EXPECT_EQ(callee()->audio_frames_received(),
1646 total_callee_audio_frames_expected);
1647 if (callee()->audio_frames_received() !=
1648 total_callee_audio_frames_expected) {
1649 expectations_correct = false;
1650 }
1651 }
1652 if (media_expectations.callee_video_expectation_ ==
1653 MediaExpectations::kExpectNoFrames) {
1654 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1655 total_callee_video_frames_expected);
1656 if (callee()->min_video_frames_received_per_track() !=
1657 total_callee_video_frames_expected) {
1658 expectations_correct = false;
1659 }
1660 }
1661 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001662 }
1663
Steve Antond91969e2019-05-30 12:27:03 -07001664 void ClosePeerConnections() {
1665 caller()->pc()->Close();
1666 callee()->pc()->Close();
1667 }
1668
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001669 void TestNegotiatedCipherSuite(
1670 const PeerConnectionFactory::Options& caller_options,
1671 const PeerConnectionFactory::Options& callee_options,
1672 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001673 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1674 callee_options));
deadbeef1dcb1642017-03-29 21:08:16 -07001675 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001676 caller()->AddAudioVideoTracks();
1677 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001678 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001679 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001680 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001681 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001682 // TODO(bugs.webrtc.org/9456): Fix it.
Ying Wangef3998f2019-12-09 13:06:53 +01001683 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
1684 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1685 expected_cipher_suite));
deadbeef1dcb1642017-03-29 21:08:16 -07001686 }
1687
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001688 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1689 bool remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001690 bool aes_ctr_enabled,
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001691 int expected_cipher_suite) {
1692 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001693 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1694 local_gcm_enabled;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001695 caller_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher =
1696 aes_ctr_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001697 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001698 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1699 remote_gcm_enabled;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001700 callee_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher =
1701 aes_ctr_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001702 TestNegotiatedCipherSuite(caller_options, callee_options,
1703 expected_cipher_suite);
1704 }
1705
Seth Hampson2f0d7022018-02-20 11:54:42 -08001706 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001707 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001708
deadbeef1dcb1642017-03-29 21:08:16 -07001709 private:
1710 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001711 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001712 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001713 // |network_thread_| and |worker_thread_| are used by both
1714 // |caller_| and |callee_| so they must be destroyed
1715 // later.
1716 std::unique_ptr<rtc::Thread> network_thread_;
1717 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001718 // The turn servers and turn customizers should be accessed & deleted on the
1719 // network thread to avoid a race with the socket read/write that occurs
1720 // on the network thread.
1721 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1722 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
deadbeef1dcb1642017-03-29 21:08:16 -07001723 std::unique_ptr<PeerConnectionWrapper> caller_;
1724 std::unique_ptr<PeerConnectionWrapper> callee_;
1725};
1726
Seth Hampson2f0d7022018-02-20 11:54:42 -08001727class PeerConnectionIntegrationTest
1728 : public PeerConnectionIntegrationBaseTest,
1729 public ::testing::WithParamInterface<SdpSemantics> {
1730 protected:
1731 PeerConnectionIntegrationTest()
1732 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1733};
1734
Yves Gerey100fe632020-01-17 19:15:53 +01001735// Fake clock must be set before threads are started to prevent race on
1736// Set/GetClockForTesting().
1737// To achieve that, multiple inheritance is used as a mixin pattern
1738// where order of construction is finely controlled.
1739// This also ensures peerconnection is closed before switching back to non-fake
1740// clock, avoiding other races and DCHECK failures such as in rtp_sender.cc.
1741class FakeClockForTest : public rtc::ScopedFakeClock {
1742 protected:
1743 FakeClockForTest() {
1744 // Some things use a time of "0" as a special value, so we need to start out
1745 // the fake clock at a nonzero time.
1746 // TODO(deadbeef): Fix this.
Danil Chapovalov0c626af2020-02-10 11:16:00 +01001747 AdvanceTime(webrtc::TimeDelta::Seconds(1));
Yves Gerey100fe632020-01-17 19:15:53 +01001748 }
1749
1750 // Explicit handle.
1751 ScopedFakeClock& FakeClock() { return *this; }
1752};
1753
1754// Ensure FakeClockForTest is constructed first (see class for rationale).
1755class PeerConnectionIntegrationTestWithFakeClock
1756 : public FakeClockForTest,
1757 public PeerConnectionIntegrationTest {};
1758
Seth Hampson2f0d7022018-02-20 11:54:42 -08001759class PeerConnectionIntegrationTestPlanB
1760 : public PeerConnectionIntegrationBaseTest {
1761 protected:
1762 PeerConnectionIntegrationTestPlanB()
1763 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1764};
1765
1766class PeerConnectionIntegrationTestUnifiedPlan
1767 : public PeerConnectionIntegrationBaseTest {
1768 protected:
1769 PeerConnectionIntegrationTestUnifiedPlan()
1770 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1771};
1772
deadbeef1dcb1642017-03-29 21:08:16 -07001773// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1774// includes testing that the callback is invoked if an observer is connected
1775// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001776TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001777 RtpReceiverObserverOnFirstPacketReceived) {
1778 ASSERT_TRUE(CreatePeerConnectionWrappers());
1779 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001780 caller()->AddAudioVideoTracks();
1781 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001782 // Start offer/answer exchange and wait for it to complete.
1783 caller()->CreateAndSetAndSignalOffer();
1784 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1785 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001786 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1787 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001788 // Wait for all "first packet received" callbacks to be fired.
1789 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001790 absl::c_all_of(caller()->rtp_receiver_observers(),
1791 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1792 return o->first_packet_received();
1793 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001794 kMaxWaitForFramesMs);
1795 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001796 absl::c_all_of(callee()->rtp_receiver_observers(),
1797 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1798 return o->first_packet_received();
1799 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001800 kMaxWaitForFramesMs);
1801 // If new observers are set after the first packet was already received, the
1802 // callback should still be invoked.
1803 caller()->ResetRtpReceiverObservers();
1804 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001805 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1806 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001807 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001808 absl::c_all_of(caller()->rtp_receiver_observers(),
1809 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1810 return o->first_packet_received();
1811 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001812 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001813 absl::c_all_of(callee()->rtp_receiver_observers(),
1814 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1815 return o->first_packet_received();
1816 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001817}
1818
1819class DummyDtmfObserver : public DtmfSenderObserverInterface {
1820 public:
1821 DummyDtmfObserver() : completed_(false) {}
1822
1823 // Implements DtmfSenderObserverInterface.
1824 void OnToneChange(const std::string& tone) override {
1825 tones_.push_back(tone);
1826 if (tone.empty()) {
1827 completed_ = true;
1828 }
1829 }
1830
1831 const std::vector<std::string>& tones() const { return tones_; }
1832 bool completed() const { return completed_; }
1833
1834 private:
1835 bool completed_;
1836 std::vector<std::string> tones_;
1837};
1838
1839// Assumes |sender| already has an audio track added and the offer/answer
1840// exchange is done.
1841void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1842 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001843 // We should be able to get a DTMF sender from the local sender.
1844 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1845 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1846 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001847 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001848 dtmf_sender->RegisterObserver(&observer);
1849
1850 // Test the DtmfSender object just created.
1851 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1852 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1853
1854 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1855 std::vector<std::string> tones = {"1", "a", ""};
1856 EXPECT_EQ(tones, observer.tones());
1857 dtmf_sender->UnregisterObserver();
1858 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1859}
1860
1861// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1862// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001863TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001864 ASSERT_TRUE(CreatePeerConnectionWrappers());
1865 ConnectFakeSignaling();
1866 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001867 caller()->AddAudioTrack();
1868 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001869 caller()->CreateAndSetAndSignalOffer();
1870 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001871 // DTLS must finish before the DTMF sender can be used reliably.
1872 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001873 TestDtmfFromSenderToReceiver(caller(), callee());
1874 TestDtmfFromSenderToReceiver(callee(), caller());
1875}
1876
1877// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1878// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001879TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001880 ASSERT_TRUE(CreatePeerConnectionWrappers());
1881 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001882
deadbeef1dcb1642017-03-29 21:08:16 -07001883 // Do normal offer/answer and wait for some frames to be received in each
1884 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001885 caller()->AddAudioVideoTracks();
1886 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001887 caller()->CreateAndSetAndSignalOffer();
1888 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001889 MediaExpectations media_expectations;
1890 media_expectations.ExpectBidirectionalAudioAndVideo();
1891 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Ying Wangef3998f2019-12-09 13:06:53 +01001892 EXPECT_METRIC_LE(
1893 2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1894 webrtc::kEnumCounterKeyProtocolDtls));
1895 EXPECT_METRIC_EQ(
1896 0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1897 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001898}
1899
1900// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001901TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001902 PeerConnectionInterface::RTCConfiguration sdes_config;
1903 sdes_config.enable_dtls_srtp.emplace(false);
1904 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1905 ConnectFakeSignaling();
1906
1907 // Do normal offer/answer and wait for some frames to be received in each
1908 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001909 caller()->AddAudioVideoTracks();
1910 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001911 caller()->CreateAndSetAndSignalOffer();
1912 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001913 MediaExpectations media_expectations;
1914 media_expectations.ExpectBidirectionalAudioAndVideo();
1915 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Ying Wangef3998f2019-12-09 13:06:53 +01001916 EXPECT_METRIC_LE(
1917 2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1918 webrtc::kEnumCounterKeyProtocolSdes));
1919 EXPECT_METRIC_EQ(
1920 0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1921 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001922}
1923
Steve Anton9a44b2d2019-07-12 12:58:30 -07001924// Basic end-to-end test specifying the |enable_encrypted_rtp_header_extensions|
1925// option to offer encrypted versions of all header extensions alongside the
1926// unencrypted versions.
1927TEST_P(PeerConnectionIntegrationTest,
1928 EndToEndCallWithEncryptedRtpHeaderExtensions) {
1929 CryptoOptions crypto_options;
1930 crypto_options.srtp.enable_encrypted_rtp_header_extensions = true;
1931 PeerConnectionInterface::RTCConfiguration config;
1932 config.crypto_options = crypto_options;
1933 // Note: This allows offering >14 RTP header extensions.
1934 config.offer_extmap_allow_mixed = true;
1935 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
1936 ConnectFakeSignaling();
1937
1938 // Do normal offer/answer and wait for some frames to be received in each
1939 // direction.
1940 caller()->AddAudioVideoTracks();
1941 callee()->AddAudioVideoTracks();
1942 caller()->CreateAndSetAndSignalOffer();
1943 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1944 MediaExpectations media_expectations;
1945 media_expectations.ExpectBidirectionalAudioAndVideo();
1946 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1947}
1948
Steve Anton8c0f7a72017-10-03 10:03:10 -07001949// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1950// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001951TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001952 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1953 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1954 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1955 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1956 return pc->GetRemoteAudioSSLCertificate();
1957 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001958 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1959 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1960 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1961 return pc->GetRemoteAudioSSLCertChain();
1962 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001963
1964 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1965 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1966
1967 // Configure each side with a known certificate so they can be compared later.
1968 PeerConnectionInterface::RTCConfiguration caller_config;
1969 caller_config.enable_dtls_srtp.emplace(true);
1970 caller_config.certificates.push_back(caller_cert);
1971 PeerConnectionInterface::RTCConfiguration callee_config;
1972 callee_config.enable_dtls_srtp.emplace(true);
1973 callee_config.certificates.push_back(callee_cert);
1974 ASSERT_TRUE(
1975 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1976 ConnectFakeSignaling();
1977
1978 // When first initialized, there should not be a remote SSL certificate (and
1979 // calling this method should not crash).
1980 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1981 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001982 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1983 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001984
Steve Anton15324772018-01-16 10:26:49 -08001985 caller()->AddAudioTrack();
1986 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001987 caller()->CreateAndSetAndSignalOffer();
1988 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1989 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1990
1991 // Once DTLS has been connected, each side should return the other's SSL
1992 // certificate when calling GetRemoteAudioSSLCertificate.
1993
1994 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1995 ASSERT_TRUE(caller_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001996 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001997 caller_remote_cert->ToPEMString());
1998
1999 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
2000 ASSERT_TRUE(callee_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07002001 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07002002 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08002003
2004 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
2005 ASSERT_TRUE(caller_remote_cert_chain);
2006 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
2007 auto remote_cert = &caller_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07002008 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08002009 remote_cert->ToPEMString());
2010
2011 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
2012 ASSERT_TRUE(callee_remote_cert_chain);
2013 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
2014 remote_cert = &callee_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07002015 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08002016 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07002017}
2018
deadbeef1dcb1642017-03-29 21:08:16 -07002019// This test sets up a call between two parties with a source resolution of
2020// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002021TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002022 Send1280By720ResolutionAndReceive16To9AspectRatio) {
2023 ASSERT_TRUE(CreatePeerConnectionWrappers());
2024 ConnectFakeSignaling();
2025
Niels Möller5c7efe72018-05-11 10:34:46 +02002026 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
2027 webrtc::FakePeriodicVideoSource::Config config;
2028 config.width = 1280;
2029 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02002030 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02002031 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
2032 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07002033
2034 // Do normal offer/answer and wait for at least one frame to be received in
2035 // each direction.
2036 caller()->CreateAndSetAndSignalOffer();
2037 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2038 callee()->min_video_frames_received_per_track() > 0,
2039 kMaxWaitForFramesMs);
2040
2041 // Check rendered aspect ratio.
2042 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
2043 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
2044 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
2045 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
2046}
2047
2048// This test sets up an one-way call, with media only from caller to
2049// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002050TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07002051 ASSERT_TRUE(CreatePeerConnectionWrappers());
2052 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002053 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002054 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002055 MediaExpectations media_expectations;
2056 media_expectations.CalleeExpectsSomeAudioAndVideo();
2057 media_expectations.CallerExpectsNoAudio();
2058 media_expectations.CallerExpectsNoVideo();
2059 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002060}
2061
Johannes Kron3e983682020-03-29 22:17:00 +02002062// Tests that send only works without the caller having a decoder factory and
2063// the callee having an encoder factory.
2064TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSendOnlyVideo) {
2065 ASSERT_TRUE(
2066 CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/true));
2067 ConnectFakeSignaling();
2068 // Add one-directional video, from caller to callee.
2069 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2070 caller()->CreateLocalVideoTrack();
2071 caller()->AddTrack(caller_track);
2072 PeerConnectionInterface::RTCOfferAnswerOptions options;
2073 options.offer_to_receive_video = 0;
2074 caller()->SetOfferAnswerOptions(options);
2075 caller()->CreateAndSetAndSignalOffer();
2076 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2077 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
2078
2079 // Expect video to be received in one direction.
2080 MediaExpectations media_expectations;
2081 media_expectations.CallerExpectsNoVideo();
2082 media_expectations.CalleeExpectsSomeVideo();
2083
2084 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2085}
2086
2087// Tests that receive only works without the caller having an encoder factory
2088// and the callee having a decoder factory.
2089TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithReceiveOnlyVideo) {
2090 ASSERT_TRUE(
2091 CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/false));
2092 ConnectFakeSignaling();
2093 // Add one-directional video, from callee to caller.
2094 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2095 callee()->CreateLocalVideoTrack();
2096 callee()->AddTrack(callee_track);
2097 PeerConnectionInterface::RTCOfferAnswerOptions options;
2098 options.offer_to_receive_video = 1;
2099 caller()->SetOfferAnswerOptions(options);
2100 caller()->CreateAndSetAndSignalOffer();
2101 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2102 ASSERT_EQ(caller()->pc()->GetReceivers().size(), 1u);
2103
2104 // Expect video to be received in one direction.
2105 MediaExpectations media_expectations;
2106 media_expectations.CallerExpectsSomeVideo();
2107 media_expectations.CalleeExpectsNoVideo();
2108
2109 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2110}
2111
2112TEST_P(PeerConnectionIntegrationTest,
2113 EndToEndCallAddReceiveVideoToSendOnlyCall) {
2114 ASSERT_TRUE(CreatePeerConnectionWrappers());
2115 ConnectFakeSignaling();
2116 // Add one-directional video, from caller to callee.
2117 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2118 caller()->CreateLocalVideoTrack();
2119 caller()->AddTrack(caller_track);
2120 caller()->CreateAndSetAndSignalOffer();
2121 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2122
2123 // Add receive video.
2124 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2125 callee()->CreateLocalVideoTrack();
2126 callee()->AddTrack(callee_track);
2127 caller()->CreateAndSetAndSignalOffer();
2128 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2129
2130 // Ensure that video frames are received end-to-end.
2131 MediaExpectations media_expectations;
2132 media_expectations.ExpectBidirectionalVideo();
2133 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2134}
2135
2136TEST_P(PeerConnectionIntegrationTest,
2137 EndToEndCallAddSendVideoToReceiveOnlyCall) {
2138 ASSERT_TRUE(CreatePeerConnectionWrappers());
2139 ConnectFakeSignaling();
2140 // Add one-directional video, from callee to caller.
2141 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2142 callee()->CreateLocalVideoTrack();
2143 callee()->AddTrack(callee_track);
2144 caller()->CreateAndSetAndSignalOffer();
2145 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2146
2147 // Add send video.
2148 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2149 caller()->CreateLocalVideoTrack();
2150 caller()->AddTrack(caller_track);
2151 caller()->CreateAndSetAndSignalOffer();
2152 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2153
2154 // Expect video to be received in one direction.
2155 MediaExpectations media_expectations;
2156 media_expectations.ExpectBidirectionalVideo();
2157 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2158}
2159
2160TEST_P(PeerConnectionIntegrationTest,
2161 EndToEndCallRemoveReceiveVideoFromSendReceiveCall) {
2162 ASSERT_TRUE(CreatePeerConnectionWrappers());
2163 ConnectFakeSignaling();
2164 // Add send video, from caller to callee.
2165 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2166 caller()->CreateLocalVideoTrack();
2167 rtc::scoped_refptr<webrtc::RtpSenderInterface> caller_sender =
2168 caller()->AddTrack(caller_track);
2169 // Add receive video, from callee to caller.
2170 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2171 callee()->CreateLocalVideoTrack();
2172
2173 rtc::scoped_refptr<webrtc::RtpSenderInterface> callee_sender =
2174 callee()->AddTrack(callee_track);
2175 caller()->CreateAndSetAndSignalOffer();
2176 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2177
2178 // Remove receive video (i.e., callee sender track).
2179 callee()->pc()->RemoveTrack(callee_sender);
2180
2181 caller()->CreateAndSetAndSignalOffer();
2182 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2183
2184 // Expect one-directional video.
2185 MediaExpectations media_expectations;
2186 media_expectations.CallerExpectsNoVideo();
2187 media_expectations.CalleeExpectsSomeVideo();
2188
2189 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2190}
2191
2192TEST_P(PeerConnectionIntegrationTest,
2193 EndToEndCallRemoveSendVideoFromSendReceiveCall) {
2194 ASSERT_TRUE(CreatePeerConnectionWrappers());
2195 ConnectFakeSignaling();
2196 // Add send video, from caller to callee.
2197 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2198 caller()->CreateLocalVideoTrack();
2199 rtc::scoped_refptr<webrtc::RtpSenderInterface> caller_sender =
2200 caller()->AddTrack(caller_track);
2201 // Add receive video, from callee to caller.
2202 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2203 callee()->CreateLocalVideoTrack();
2204
2205 rtc::scoped_refptr<webrtc::RtpSenderInterface> callee_sender =
2206 callee()->AddTrack(callee_track);
2207 caller()->CreateAndSetAndSignalOffer();
2208 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2209
2210 // Remove send video (i.e., caller sender track).
2211 caller()->pc()->RemoveTrack(caller_sender);
2212
2213 caller()->CreateAndSetAndSignalOffer();
2214 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2215
2216 // Expect one-directional video.
2217 MediaExpectations media_expectations;
2218 media_expectations.CalleeExpectsNoVideo();
2219 media_expectations.CallerExpectsSomeVideo();
2220
2221 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2222}
2223
deadbeef1dcb1642017-03-29 21:08:16 -07002224// This test sets up a audio call initially, with the callee rejecting video
2225// initially. Then later the callee decides to upgrade to audio/video, and
2226// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002227TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07002228 ASSERT_TRUE(CreatePeerConnectionWrappers());
2229 ConnectFakeSignaling();
2230 // Initially, offer an audio/video stream from the caller, but refuse to
2231 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08002232 caller()->AddAudioVideoTracks();
2233 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002234 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2235 PeerConnectionInterface::RTCOfferAnswerOptions options;
2236 options.offer_to_receive_video = 0;
2237 callee()->SetOfferAnswerOptions(options);
2238 } else {
2239 callee()->SetRemoteOfferHandler([this] {
2240 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2241 });
2242 }
deadbeef1dcb1642017-03-29 21:08:16 -07002243 // Do offer/answer and make sure audio is still received end-to-end.
2244 caller()->CreateAndSetAndSignalOffer();
2245 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002246 {
2247 MediaExpectations media_expectations;
2248 media_expectations.ExpectBidirectionalAudio();
2249 media_expectations.ExpectNoVideo();
2250 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2251 }
deadbeef1dcb1642017-03-29 21:08:16 -07002252 // Sanity check that the callee's description has a rejected video section.
2253 ASSERT_NE(nullptr, callee()->pc()->local_description());
2254 const ContentInfo* callee_video_content =
2255 GetFirstVideoContent(callee()->pc()->local_description()->description());
2256 ASSERT_NE(nullptr, callee_video_content);
2257 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002258
deadbeef1dcb1642017-03-29 21:08:16 -07002259 // Now negotiate with video and ensure negotiation succeeds, with video
2260 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08002261 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002262 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2263 PeerConnectionInterface::RTCOfferAnswerOptions options;
2264 options.offer_to_receive_video = 1;
2265 callee()->SetOfferAnswerOptions(options);
2266 } else {
2267 callee()->SetRemoteOfferHandler(nullptr);
2268 caller()->SetRemoteOfferHandler([this] {
2269 // The caller creates a new transceiver to receive video on when receiving
2270 // the offer, but by default it is send only.
2271 auto transceivers = caller()->pc()->GetTransceivers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02002272 ASSERT_EQ(3U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002273 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
2274 transceivers[2]->receiver()->media_type());
2275 transceivers[2]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
2276 transceivers[2]->SetDirection(RtpTransceiverDirection::kSendRecv);
2277 });
2278 }
deadbeef1dcb1642017-03-29 21:08:16 -07002279 callee()->CreateAndSetAndSignalOffer();
2280 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002281 {
2282 // Expect additional audio frames to be received after the upgrade.
2283 MediaExpectations media_expectations;
2284 media_expectations.ExpectBidirectionalAudioAndVideo();
2285 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2286 }
deadbeef1dcb1642017-03-29 21:08:16 -07002287}
2288
deadbeef4389b4d2017-09-07 09:07:36 -07002289// Simpler than the above test; just add an audio track to an established
2290// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002291TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07002292 ASSERT_TRUE(CreatePeerConnectionWrappers());
2293 ConnectFakeSignaling();
2294 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08002295 caller()->AddVideoTrack();
2296 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002297 caller()->CreateAndSetAndSignalOffer();
2298 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2299 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08002300 caller()->AddAudioTrack();
2301 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002302 caller()->CreateAndSetAndSignalOffer();
2303 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2304 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002305 MediaExpectations media_expectations;
2306 media_expectations.ExpectBidirectionalAudioAndVideo();
2307 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07002308}
2309
deadbeef1dcb1642017-03-29 21:08:16 -07002310// This test sets up a call that's transferred to a new caller with a different
2311// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002312TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002313 ASSERT_TRUE(CreatePeerConnectionWrappers());
2314 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002315 caller()->AddAudioVideoTracks();
2316 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002317 caller()->CreateAndSetAndSignalOffer();
2318 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2319
2320 // Keep the original peer around which will still send packets to the
2321 // receiving client. These SRTP packets will be dropped.
2322 std::unique_ptr<PeerConnectionWrapper> original_peer(
2323 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002324 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002325 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2326 // directly above.
2327 original_peer->pc()->Close();
2328
2329 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002330 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002331 caller()->CreateAndSetAndSignalOffer();
2332 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2333 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002334 MediaExpectations media_expectations;
2335 media_expectations.ExpectBidirectionalAudioAndVideo();
2336 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002337}
2338
2339// This test sets up a call that's transferred to a new callee with a different
2340// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002341TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002342 ASSERT_TRUE(CreatePeerConnectionWrappers());
2343 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002344 caller()->AddAudioVideoTracks();
2345 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002346 caller()->CreateAndSetAndSignalOffer();
2347 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2348
2349 // Keep the original peer around which will still send packets to the
2350 // receiving client. These SRTP packets will be dropped.
2351 std::unique_ptr<PeerConnectionWrapper> original_peer(
2352 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002353 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002354 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2355 // directly above.
2356 original_peer->pc()->Close();
2357
2358 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002359 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002360 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2361 caller()->CreateAndSetAndSignalOffer();
2362 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2363 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002364 MediaExpectations media_expectations;
2365 media_expectations.ExpectBidirectionalAudioAndVideo();
2366 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002367}
2368
2369// This test sets up a non-bundled call and negotiates bundling at the same
2370// time as starting an ICE restart. When bundling is in effect in the restart,
2371// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002372TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002373 ASSERT_TRUE(CreatePeerConnectionWrappers());
2374 ConnectFakeSignaling();
2375
Steve Anton15324772018-01-16 10:26:49 -08002376 caller()->AddAudioVideoTracks();
2377 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002378 // Remove the bundle group from the SDP received by the callee.
2379 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2380 desc->RemoveGroupByName("BUNDLE");
2381 });
2382 caller()->CreateAndSetAndSignalOffer();
2383 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002384 {
2385 MediaExpectations media_expectations;
2386 media_expectations.ExpectBidirectionalAudioAndVideo();
2387 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2388 }
deadbeef1dcb1642017-03-29 21:08:16 -07002389 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2390 callee()->SetReceivedSdpMunger(nullptr);
2391 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2392 caller()->CreateAndSetAndSignalOffer();
2393 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2394
2395 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002396 {
2397 MediaExpectations media_expectations;
2398 media_expectations.ExpectBidirectionalAudioAndVideo();
2399 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2400 }
deadbeef1dcb1642017-03-29 21:08:16 -07002401}
2402
2403// Test CVO (Coordination of Video Orientation). If a video source is rotated
2404// and both peers support the CVO RTP header extension, the actual video frames
2405// don't need to be encoded in different resolutions, since the rotation is
2406// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002407TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002408 ASSERT_TRUE(CreatePeerConnectionWrappers());
2409 ConnectFakeSignaling();
2410 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002411 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002412 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002413 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002414 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2415
2416 // Wait for video frames to be received by both sides.
2417 caller()->CreateAndSetAndSignalOffer();
2418 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2419 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2420 callee()->min_video_frames_received_per_track() > 0,
2421 kMaxWaitForFramesMs);
2422
2423 // Ensure that the aspect ratio is unmodified.
2424 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2425 // not just assumed.
2426 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2427 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2428 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2429 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2430 // Ensure that the CVO bits were surfaced to the renderer.
2431 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2432 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2433}
2434
2435// Test that when the CVO extension isn't supported, video is rotated the
2436// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002437TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002438 ASSERT_TRUE(CreatePeerConnectionWrappers());
2439 ConnectFakeSignaling();
2440 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002441 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002442 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002443 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002444 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2445
2446 // Remove the CVO extension from the offered SDP.
2447 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2448 cricket::VideoContentDescription* video =
2449 GetFirstVideoContentDescription(desc);
2450 video->ClearRtpHeaderExtensions();
2451 });
2452 // Wait for video frames to be received by both sides.
2453 caller()->CreateAndSetAndSignalOffer();
2454 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2455 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2456 callee()->min_video_frames_received_per_track() > 0,
2457 kMaxWaitForFramesMs);
2458
2459 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2460 // rotation.
2461 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2462 // not just assumed.
2463 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2464 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2465 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2466 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2467 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2468 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2469 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2470}
2471
deadbeef1dcb1642017-03-29 21:08:16 -07002472// Test that if the answerer rejects the audio m= section, no audio is sent or
2473// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002474TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002475 ASSERT_TRUE(CreatePeerConnectionWrappers());
2476 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002477 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002478 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2479 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2480 // it will reject the audio m= section completely.
2481 PeerConnectionInterface::RTCOfferAnswerOptions options;
2482 options.offer_to_receive_audio = 0;
2483 callee()->SetOfferAnswerOptions(options);
2484 } else {
2485 // Stopping the audio RtpTransceiver will cause the media section to be
2486 // rejected in the answer.
2487 callee()->SetRemoteOfferHandler([this] {
2488 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)->Stop();
2489 });
2490 }
Steve Anton15324772018-01-16 10:26:49 -08002491 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002492 // Do offer/answer and wait for successful end-to-end video frames.
2493 caller()->CreateAndSetAndSignalOffer();
2494 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002495 MediaExpectations media_expectations;
2496 media_expectations.ExpectBidirectionalVideo();
2497 media_expectations.ExpectNoAudio();
2498 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2499
deadbeef1dcb1642017-03-29 21:08:16 -07002500 // Sanity check that the callee's description has a rejected audio section.
2501 ASSERT_NE(nullptr, callee()->pc()->local_description());
2502 const ContentInfo* callee_audio_content =
2503 GetFirstAudioContent(callee()->pc()->local_description()->description());
2504 ASSERT_NE(nullptr, callee_audio_content);
2505 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002506 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2507 // The caller's transceiver should have stopped after receiving the answer.
2508 EXPECT_TRUE(caller()
2509 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2510 ->stopped());
2511 }
deadbeef1dcb1642017-03-29 21:08:16 -07002512}
2513
2514// Test that if the answerer rejects the video m= section, no video is sent or
2515// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002516TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002517 ASSERT_TRUE(CreatePeerConnectionWrappers());
2518 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002519 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002520 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2521 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2522 // it will reject the video m= section completely.
2523 PeerConnectionInterface::RTCOfferAnswerOptions options;
2524 options.offer_to_receive_video = 0;
2525 callee()->SetOfferAnswerOptions(options);
2526 } else {
2527 // Stopping the video RtpTransceiver will cause the media section to be
2528 // rejected in the answer.
2529 callee()->SetRemoteOfferHandler([this] {
2530 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2531 });
2532 }
Steve Anton15324772018-01-16 10:26:49 -08002533 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002534 // Do offer/answer and wait for successful end-to-end audio frames.
2535 caller()->CreateAndSetAndSignalOffer();
2536 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002537 MediaExpectations media_expectations;
2538 media_expectations.ExpectBidirectionalAudio();
2539 media_expectations.ExpectNoVideo();
2540 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2541
deadbeef1dcb1642017-03-29 21:08:16 -07002542 // Sanity check that the callee's description has a rejected video section.
2543 ASSERT_NE(nullptr, callee()->pc()->local_description());
2544 const ContentInfo* callee_video_content =
2545 GetFirstVideoContent(callee()->pc()->local_description()->description());
2546 ASSERT_NE(nullptr, callee_video_content);
2547 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002548 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
2549 // The caller's transceiver should have stopped after receiving the answer.
2550 EXPECT_TRUE(caller()
2551 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2552 ->stopped());
2553 }
deadbeef1dcb1642017-03-29 21:08:16 -07002554}
2555
2556// Test that if the answerer rejects both audio and video m= sections, nothing
2557// bad happens.
2558// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2559// test anything but the fact that negotiation succeeds, which doesn't mean
2560// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002561TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002562 ASSERT_TRUE(CreatePeerConnectionWrappers());
2563 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002564 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002565 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2566 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2567 // will reject both audio and video m= sections.
2568 PeerConnectionInterface::RTCOfferAnswerOptions options;
2569 options.offer_to_receive_audio = 0;
2570 options.offer_to_receive_video = 0;
2571 callee()->SetOfferAnswerOptions(options);
2572 } else {
2573 callee()->SetRemoteOfferHandler([this] {
2574 // Stopping all transceivers will cause all media sections to be rejected.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002575 for (const auto& transceiver : callee()->pc()->GetTransceivers()) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08002576 transceiver->Stop();
2577 }
2578 });
2579 }
deadbeef1dcb1642017-03-29 21:08:16 -07002580 // Do offer/answer and wait for stable signaling state.
2581 caller()->CreateAndSetAndSignalOffer();
2582 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002583
deadbeef1dcb1642017-03-29 21:08:16 -07002584 // Sanity check that the callee's description has rejected m= sections.
2585 ASSERT_NE(nullptr, callee()->pc()->local_description());
2586 const ContentInfo* callee_audio_content =
2587 GetFirstAudioContent(callee()->pc()->local_description()->description());
2588 ASSERT_NE(nullptr, callee_audio_content);
2589 EXPECT_TRUE(callee_audio_content->rejected);
2590 const ContentInfo* callee_video_content =
2591 GetFirstVideoContent(callee()->pc()->local_description()->description());
2592 ASSERT_NE(nullptr, callee_video_content);
2593 EXPECT_TRUE(callee_video_content->rejected);
2594}
2595
2596// This test sets up an audio and video call between two parties. After the
2597// call runs for a while, the caller sends an updated offer with video being
2598// rejected. Once the re-negotiation is done, the video flow should stop and
2599// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002600TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002601 ASSERT_TRUE(CreatePeerConnectionWrappers());
2602 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002603 caller()->AddAudioVideoTracks();
2604 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002605 caller()->CreateAndSetAndSignalOffer();
2606 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002607 {
2608 MediaExpectations media_expectations;
2609 media_expectations.ExpectBidirectionalAudioAndVideo();
2610 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2611 }
deadbeef1dcb1642017-03-29 21:08:16 -07002612 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002613 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2614 caller()->SetGeneratedSdpMunger(
2615 [](cricket::SessionDescription* description) {
2616 for (cricket::ContentInfo& content : description->contents()) {
2617 if (cricket::IsVideoContent(&content)) {
2618 content.rejected = true;
2619 }
2620 }
2621 });
2622 } else {
2623 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
2624 }
deadbeef1dcb1642017-03-29 21:08:16 -07002625 caller()->CreateAndSetAndSignalOffer();
2626 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2627
2628 // Sanity check that the caller's description has a rejected video section.
2629 ASSERT_NE(nullptr, caller()->pc()->local_description());
2630 const ContentInfo* caller_video_content =
2631 GetFirstVideoContent(caller()->pc()->local_description()->description());
2632 ASSERT_NE(nullptr, caller_video_content);
2633 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002634 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002635 {
2636 MediaExpectations media_expectations;
2637 media_expectations.ExpectBidirectionalAudio();
2638 media_expectations.ExpectNoVideo();
2639 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2640 }
deadbeef1dcb1642017-03-29 21:08:16 -07002641}
2642
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002643// Do one offer/answer with audio, another that disables it (rejecting the m=
2644// section), and another that re-enables it. Regression test for:
2645// bugs.webrtc.org/6023
2646TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2647 ASSERT_TRUE(CreatePeerConnectionWrappers());
2648 ConnectFakeSignaling();
2649
2650 // Add audio track, do normal offer/answer.
2651 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2652 caller()->CreateLocalAudioTrack();
2653 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2654 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2655 caller()->CreateAndSetAndSignalOffer();
2656 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2657
2658 // Remove audio track, and set offer_to_receive_audio to false to cause the
2659 // m= section to be completely disabled, not just "recvonly".
2660 caller()->pc()->RemoveTrack(sender);
2661 PeerConnectionInterface::RTCOfferAnswerOptions options;
2662 options.offer_to_receive_audio = 0;
2663 caller()->SetOfferAnswerOptions(options);
2664 caller()->CreateAndSetAndSignalOffer();
2665 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2666
2667 // Add the audio track again, expecting negotiation to succeed and frames to
2668 // flow.
2669 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2670 options.offer_to_receive_audio = 1;
2671 caller()->SetOfferAnswerOptions(options);
2672 caller()->CreateAndSetAndSignalOffer();
2673 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2674
2675 MediaExpectations media_expectations;
2676 media_expectations.CalleeExpectsSomeAudio();
2677 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2678}
2679
deadbeef1dcb1642017-03-29 21:08:16 -07002680// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2681// is needed to support legacy endpoints.
2682// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2683// add a test for an end-to-end test without MID signaling either (basically,
2684// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002685TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002686 ASSERT_TRUE(CreatePeerConnectionWrappers());
2687 ConnectFakeSignaling();
2688 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002689 caller()->AddAudioVideoTracks();
2690 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002691 // Remove SSRCs and MSIDs from the received offer SDP.
2692 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002693 caller()->CreateAndSetAndSignalOffer();
2694 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002695 MediaExpectations media_expectations;
2696 media_expectations.ExpectBidirectionalAudioAndVideo();
2697 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002698}
2699
Seth Hampson5897a6e2018-04-03 11:16:33 -07002700// Basic end-to-end test, without SSRC signaling. This means that the track
2701// was created properly and frames are delivered when the MSIDs are communicated
2702// with a=msid lines and no a=ssrc lines.
2703TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2704 EndToEndCallWithoutSsrcSignaling) {
2705 const char kStreamId[] = "streamId";
2706 ASSERT_TRUE(CreatePeerConnectionWrappers());
2707 ConnectFakeSignaling();
2708 // Add just audio tracks.
2709 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2710 callee()->AddAudioTrack();
2711
2712 // Remove SSRCs from the received offer SDP.
2713 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2714 caller()->CreateAndSetAndSignalOffer();
2715 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2716 MediaExpectations media_expectations;
2717 media_expectations.ExpectBidirectionalAudio();
2718 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2719}
2720
Johannes Kron3e983682020-03-29 22:17:00 +02002721TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2722 EndToEndCallAddReceiveVideoToSendOnlyCall) {
2723 ASSERT_TRUE(CreatePeerConnectionWrappers());
2724 ConnectFakeSignaling();
2725 // Add one-directional video, from caller to callee.
2726 rtc::scoped_refptr<webrtc::VideoTrackInterface> track =
2727 caller()->CreateLocalVideoTrack();
2728
2729 RtpTransceiverInit video_transceiver_init;
2730 video_transceiver_init.stream_ids = {"video1"};
2731 video_transceiver_init.direction = RtpTransceiverDirection::kSendOnly;
2732 auto video_sender =
2733 caller()->pc()->AddTransceiver(track, video_transceiver_init).MoveValue();
2734 caller()->CreateAndSetAndSignalOffer();
2735 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2736
2737 // Add receive direction.
2738 video_sender->SetDirection(RtpTransceiverDirection::kSendRecv);
2739
2740 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2741 callee()->CreateLocalVideoTrack();
2742
2743 callee()->AddTrack(callee_track);
2744 caller()->CreateAndSetAndSignalOffer();
2745 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2746 // Ensure that video frames are received end-to-end.
2747 MediaExpectations media_expectations;
2748 media_expectations.ExpectBidirectionalVideo();
2749 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2750}
2751
Steve Antondf527fd2018-04-27 15:52:03 -07002752// Tests that video flows between multiple video tracks when SSRCs are not
2753// signaled. This exercises the MID RTP header extension which is needed to
2754// demux the incoming video tracks.
2755TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2756 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2757 ASSERT_TRUE(CreatePeerConnectionWrappers());
2758 ConnectFakeSignaling();
2759 caller()->AddVideoTrack();
2760 caller()->AddVideoTrack();
2761 callee()->AddVideoTrack();
2762 callee()->AddVideoTrack();
2763
2764 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2765 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2766 caller()->CreateAndSetAndSignalOffer();
2767 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2768 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2769 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2770
2771 // Expect video to be received in both directions on both tracks.
2772 MediaExpectations media_expectations;
2773 media_expectations.ExpectBidirectionalVideo();
2774 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2775}
2776
Henrik Boström5b147782018-12-04 11:25:05 +01002777TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
2778 ASSERT_TRUE(CreatePeerConnectionWrappers());
2779 ConnectFakeSignaling();
2780 caller()->AddAudioTrack();
2781 caller()->AddVideoTrack();
2782 caller()->CreateAndSetAndSignalOffer();
2783 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2784 auto callee_receivers = callee()->pc()->GetReceivers();
2785 ASSERT_EQ(2u, callee_receivers.size());
2786 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
2787 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
2788}
2789
2790TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
2791 ASSERT_TRUE(CreatePeerConnectionWrappers());
2792 ConnectFakeSignaling();
2793 caller()->AddAudioTrack();
2794 caller()->AddVideoTrack();
2795 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2796 caller()->CreateAndSetAndSignalOffer();
2797 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2798 auto callee_receivers = callee()->pc()->GetReceivers();
2799 ASSERT_EQ(2u, callee_receivers.size());
2800 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
2801 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
2802 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
2803 callee_receivers[1]->stream_ids()[0]);
2804 EXPECT_EQ(callee_receivers[0]->streams()[0],
2805 callee_receivers[1]->streams()[0]);
2806}
2807
deadbeef1dcb1642017-03-29 21:08:16 -07002808// Test that if two video tracks are sent (from caller to callee, in this test),
2809// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002810TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002811 ASSERT_TRUE(CreatePeerConnectionWrappers());
2812 ConnectFakeSignaling();
2813 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002814 caller()->AddAudioVideoTracks();
2815 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002816 caller()->CreateAndSetAndSignalOffer();
2817 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002818 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002819
2820 MediaExpectations media_expectations;
2821 media_expectations.CalleeExpectsSomeAudioAndVideo();
2822 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002823}
2824
2825static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2826 bool first = true;
2827 for (cricket::ContentInfo& content : desc->contents()) {
2828 if (first) {
2829 first = false;
2830 continue;
2831 }
2832 content.bundle_only = true;
2833 }
2834 first = true;
2835 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2836 if (first) {
2837 first = false;
2838 continue;
2839 }
2840 transport.description.ice_ufrag.clear();
2841 transport.description.ice_pwd.clear();
2842 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2843 transport.description.identity_fingerprint.reset(nullptr);
2844 }
2845}
2846
2847// Test that if applying a true "max bundle" offer, which uses ports of 0,
2848// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2849// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2850// successfully and media flows.
2851// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2852// TODO(deadbeef): Won't need this test once we start generating actual
2853// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002854TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002855 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2856 ASSERT_TRUE(CreatePeerConnectionWrappers());
2857 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002858 caller()->AddAudioVideoTracks();
2859 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002860 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2861 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2862 // but the first m= section.
2863 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2864 caller()->CreateAndSetAndSignalOffer();
2865 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002866 MediaExpectations media_expectations;
2867 media_expectations.ExpectBidirectionalAudioAndVideo();
2868 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002869}
2870
2871// Test that we can receive the audio output level from a remote audio track.
2872// TODO(deadbeef): Use a fake audio source and verify that the output level is
2873// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002874TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002875 ASSERT_TRUE(CreatePeerConnectionWrappers());
2876 ConnectFakeSignaling();
2877 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002878 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002879 caller()->CreateAndSetAndSignalOffer();
2880 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2881
2882 // Get the audio output level stats. Note that the level is not available
2883 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002884 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002885 kMaxWaitForFramesMs);
2886}
2887
2888// Test that an audio input level is reported.
2889// TODO(deadbeef): Use a fake audio source and verify that the input level is
2890// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002891TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002892 ASSERT_TRUE(CreatePeerConnectionWrappers());
2893 ConnectFakeSignaling();
2894 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002895 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002896 caller()->CreateAndSetAndSignalOffer();
2897 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2898
2899 // Get the audio input level stats. The level should be available very
2900 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002901 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002902 kMaxWaitForStatsMs);
2903}
2904
2905// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002906TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002907 ASSERT_TRUE(CreatePeerConnectionWrappers());
2908 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002909 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002910 // Do offer/answer, wait for the callee to receive some frames.
2911 caller()->CreateAndSetAndSignalOffer();
2912 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002913
2914 MediaExpectations media_expectations;
2915 media_expectations.CalleeExpectsSomeAudioAndVideo();
2916 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002917
2918 // Get a handle to the remote tracks created, so they can be used as GetStats
2919 // filters.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002920 for (const auto& receiver : callee()->pc()->GetReceivers()) {
Steve Anton15324772018-01-16 10:26:49 -08002921 // We received frames, so we definitely should have nonzero "received bytes"
2922 // stats at this point.
2923 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2924 0);
2925 }
deadbeef1dcb1642017-03-29 21:08:16 -07002926}
2927
2928// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002929TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002930 ASSERT_TRUE(CreatePeerConnectionWrappers());
2931 ConnectFakeSignaling();
2932 auto audio_track = caller()->CreateLocalAudioTrack();
2933 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002934 caller()->AddTrack(audio_track);
2935 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002936 // Do offer/answer, wait for the callee to receive some frames.
2937 caller()->CreateAndSetAndSignalOffer();
2938 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002939 MediaExpectations media_expectations;
2940 media_expectations.CalleeExpectsSomeAudioAndVideo();
2941 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002942
2943 // The callee received frames, so we definitely should have nonzero "sent
2944 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002945 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2946 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2947}
2948
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002949// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002950TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002951 ASSERT_TRUE(CreatePeerConnectionWrappers());
2952 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002953 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002954
Steve Anton15324772018-01-16 10:26:49 -08002955 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002956
2957 // Do offer/answer, wait for the callee to receive some frames.
2958 caller()->CreateAndSetAndSignalOffer();
2959 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2960
2961 // Get the remote audio track created on the receiver, so they can be used as
2962 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002963 auto receivers = callee()->pc()->GetReceivers();
2964 ASSERT_EQ(1u, receivers.size());
2965 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002966
2967 // Get the audio output level stats. Note that the level is not available
2968 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002969 EXPECT_TRUE_WAIT(
2970 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2971 0,
2972 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002973}
2974
Steve Antona41959e2018-11-28 11:15:33 -08002975// Test that the track ID is associated with all local and remote SSRC stats
2976// using the old GetStats() and more than 1 audio and more than 1 video track.
2977// This is a regression test for crbug.com/906988
2978TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2979 OldGetStatsAssociatesTrackIdForManyMediaSections) {
2980 ASSERT_TRUE(CreatePeerConnectionWrappers());
2981 ConnectFakeSignaling();
2982 auto audio_sender_1 = caller()->AddAudioTrack();
2983 auto video_sender_1 = caller()->AddVideoTrack();
2984 auto audio_sender_2 = caller()->AddAudioTrack();
2985 auto video_sender_2 = caller()->AddVideoTrack();
2986 caller()->CreateAndSetAndSignalOffer();
2987 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2988
2989 MediaExpectations media_expectations;
2990 media_expectations.CalleeExpectsSomeAudioAndVideo();
2991 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
2992
2993 std::vector<std::string> track_ids = {
2994 audio_sender_1->track()->id(), video_sender_1->track()->id(),
2995 audio_sender_2->track()->id(), video_sender_2->track()->id()};
2996
2997 auto caller_stats = caller()->OldGetStats();
2998 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
2999 auto callee_stats = callee()->OldGetStats();
3000 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
3001}
3002
Steve Antonffa6ce42018-11-30 09:26:08 -08003003// Test that the new GetStats() returns stats for all outgoing/incoming streams
3004// with the correct track IDs if there are more than one audio and more than one
3005// video senders/receivers.
3006TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
3007 ASSERT_TRUE(CreatePeerConnectionWrappers());
3008 ConnectFakeSignaling();
3009 auto audio_sender_1 = caller()->AddAudioTrack();
3010 auto video_sender_1 = caller()->AddVideoTrack();
3011 auto audio_sender_2 = caller()->AddAudioTrack();
3012 auto video_sender_2 = caller()->AddVideoTrack();
3013 caller()->CreateAndSetAndSignalOffer();
3014 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3015
3016 MediaExpectations media_expectations;
3017 media_expectations.CalleeExpectsSomeAudioAndVideo();
3018 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
3019
3020 std::vector<std::string> track_ids = {
3021 audio_sender_1->track()->id(), video_sender_1->track()->id(),
3022 audio_sender_2->track()->id(), video_sender_2->track()->id()};
3023
3024 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
3025 caller()->NewGetStats();
3026 ASSERT_TRUE(caller_report);
3027 auto outbound_stream_stats =
3028 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
Henrik Boströma0ff50c2020-05-05 15:54:46 +02003029 ASSERT_EQ(outbound_stream_stats.size(), 4u);
Steve Antonffa6ce42018-11-30 09:26:08 -08003030 std::vector<std::string> outbound_track_ids;
3031 for (const auto& stat : outbound_stream_stats) {
3032 ASSERT_TRUE(stat->bytes_sent.is_defined());
3033 EXPECT_LT(0u, *stat->bytes_sent);
Rasmus Brandt2efae772019-06-27 14:29:34 +02003034 if (*stat->kind == "video") {
3035 ASSERT_TRUE(stat->key_frames_encoded.is_defined());
3036 EXPECT_GT(*stat->key_frames_encoded, 0u);
3037 ASSERT_TRUE(stat->frames_encoded.is_defined());
3038 EXPECT_GE(*stat->frames_encoded, *stat->key_frames_encoded);
3039 }
Steve Antonffa6ce42018-11-30 09:26:08 -08003040 ASSERT_TRUE(stat->track_id.is_defined());
3041 const auto* track_stat =
3042 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
3043 ASSERT_TRUE(track_stat);
3044 outbound_track_ids.push_back(*track_stat->track_identifier);
3045 }
3046 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
3047
3048 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
3049 callee()->NewGetStats();
3050 ASSERT_TRUE(callee_report);
3051 auto inbound_stream_stats =
3052 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
3053 ASSERT_EQ(4u, inbound_stream_stats.size());
3054 std::vector<std::string> inbound_track_ids;
3055 for (const auto& stat : inbound_stream_stats) {
3056 ASSERT_TRUE(stat->bytes_received.is_defined());
3057 EXPECT_LT(0u, *stat->bytes_received);
Rasmus Brandt2efae772019-06-27 14:29:34 +02003058 if (*stat->kind == "video") {
3059 ASSERT_TRUE(stat->key_frames_decoded.is_defined());
3060 EXPECT_GT(*stat->key_frames_decoded, 0u);
3061 ASSERT_TRUE(stat->frames_decoded.is_defined());
3062 EXPECT_GE(*stat->frames_decoded, *stat->key_frames_decoded);
3063 }
Steve Antonffa6ce42018-11-30 09:26:08 -08003064 ASSERT_TRUE(stat->track_id.is_defined());
3065 const auto* track_stat =
3066 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
3067 ASSERT_TRUE(track_stat);
3068 inbound_track_ids.push_back(*track_stat->track_identifier);
3069 }
3070 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
3071}
3072
3073// Test that we can get stats (using the new stats implementation) for
deadbeefd8ad7882017-04-18 16:01:17 -07003074// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
3075// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003076TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07003077 GetStatsForUnsignaledStreamWithNewStatsApi) {
3078 ASSERT_TRUE(CreatePeerConnectionWrappers());
3079 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003080 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07003081 // Remove SSRCs and MSIDs from the received offer SDP.
3082 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3083 caller()->CreateAndSetAndSignalOffer();
3084 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003085 MediaExpectations media_expectations;
3086 media_expectations.CalleeExpectsSomeAudio(1);
3087 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07003088
3089 // We received a frame, so we should have nonzero "bytes received" stats for
3090 // the unsignaled stream, if stats are working for it.
3091 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
3092 callee()->NewGetStats();
3093 ASSERT_NE(nullptr, report);
3094 auto inbound_stream_stats =
3095 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
3096 ASSERT_EQ(1U, inbound_stream_stats.size());
3097 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
3098 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07003099 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
3100}
3101
Taylor Brandstettera4653442018-06-19 09:44:26 -07003102// Same as above but for the legacy stats implementation.
3103TEST_P(PeerConnectionIntegrationTest,
3104 GetStatsForUnsignaledStreamWithOldStatsApi) {
3105 ASSERT_TRUE(CreatePeerConnectionWrappers());
3106 ConnectFakeSignaling();
3107 caller()->AddAudioTrack();
3108 // Remove SSRCs and MSIDs from the received offer SDP.
3109 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3110 caller()->CreateAndSetAndSignalOffer();
3111 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3112
3113 // Note that, since the old stats implementation associates SSRCs with tracks
3114 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
3115 // associated track ID. So we can't use the track "selector" argument.
3116 //
3117 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
3118 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003119 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07003120 kDefaultTimeout);
3121}
3122
zhihuangf8164932017-05-19 13:09:47 -07003123// Test that we can successfully get the media related stats (audio level
3124// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003125TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07003126 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
3127 ASSERT_TRUE(CreatePeerConnectionWrappers());
3128 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003129 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07003130 // Remove SSRCs and MSIDs from the received offer SDP.
3131 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3132 caller()->CreateAndSetAndSignalOffer();
3133 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003134 MediaExpectations media_expectations;
3135 media_expectations.CalleeExpectsSomeAudio(1);
3136 media_expectations.CalleeExpectsSomeVideo(1);
3137 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07003138
3139 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
3140 callee()->NewGetStats();
3141 ASSERT_NE(nullptr, report);
3142
3143 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3144 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
3145 ASSERT_GE(audio_index, 0);
3146 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07003147}
3148
deadbeef4e2deab2017-09-20 13:56:21 -07003149// Helper for test below.
3150void ModifySsrcs(cricket::SessionDescription* desc) {
3151 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07003152 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08003153 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07003154 for (uint32_t& ssrc : stream.ssrcs) {
3155 ssrc = rtc::CreateRandomId();
3156 }
3157 }
3158 }
3159}
3160
3161// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
3162// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
3163// This should result in two "RTCInboundRTPStreamStats", but only one
3164// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
3165// being reset to 0 once the SSRC change occurs.
3166//
3167// Regression test for this bug:
3168// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
3169//
3170// The bug causes the track stats to only represent one of the two streams:
3171// whichever one has the higher SSRC. So with this bug, there was a 50% chance
3172// that the track stat counters would reset to 0 when the new stream is
3173// received, and a 50% chance that they'll stop updating (while
3174// "concealed_samples" continues increasing, due to silence being generated for
3175// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003176TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003177 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07003178 ASSERT_TRUE(CreatePeerConnectionWrappers());
3179 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003180 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07003181 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
3182 // that doesn't signal SSRCs (from the callee's perspective).
3183 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3184 caller()->CreateAndSetAndSignalOffer();
3185 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3186 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003187 {
3188 MediaExpectations media_expectations;
3189 media_expectations.CalleeExpectsSomeAudio(50);
3190 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3191 }
deadbeef4e2deab2017-09-20 13:56:21 -07003192 // Some audio frames were received, so we should have nonzero "samples
3193 // received" for the track.
3194 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
3195 callee()->NewGetStats();
3196 ASSERT_NE(nullptr, report);
3197 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3198 ASSERT_EQ(1U, track_stats.size());
3199 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
3200 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
3201 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
3202
3203 // Create a new offer and munge it to cause the caller to use a new SSRC.
3204 caller()->SetGeneratedSdpMunger(ModifySsrcs);
3205 caller()->CreateAndSetAndSignalOffer();
3206 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3207 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
3208 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003209 {
3210 MediaExpectations media_expectations;
3211 media_expectations.CalleeExpectsSomeAudio(25);
3212 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3213 }
deadbeef4e2deab2017-09-20 13:56:21 -07003214
3215 report = callee()->NewGetStats();
3216 ASSERT_NE(nullptr, report);
3217 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3218 ASSERT_EQ(1U, track_stats.size());
3219 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
3220 // The "total samples received" stat should only be greater than it was
3221 // before.
3222 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
3223 // Right now, the new SSRC will cause the counters to reset to 0.
3224 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
3225
3226 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08003227 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07003228 // good sign that we're seeing stats from the old stream that's no longer
3229 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08003230 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07003231 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
3232 EXPECT_LT(*track_stats[0]->concealed_samples,
3233 *track_stats[0]->total_samples_received *
3234 kAcceptableConcealedSamplesPercentage);
3235
3236 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
3237 // sanity check that the SSRC really changed.
3238 // TODO(deadbeef): This isn't working right now, because we're not returning
3239 // *any* stats for the inactive stream. Uncomment when the bug is completely
3240 // fixed.
3241 // auto inbound_stream_stats =
3242 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
3243 // ASSERT_EQ(2U, inbound_stream_stats.size());
3244}
3245
deadbeef1dcb1642017-03-29 21:08:16 -07003246// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003247TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07003248 PeerConnectionFactory::Options dtls_10_options;
3249 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3250 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
3251 dtls_10_options));
3252 ConnectFakeSignaling();
3253 // Do normal offer/answer and wait for some frames to be received in each
3254 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003255 caller()->AddAudioVideoTracks();
3256 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003257 caller()->CreateAndSetAndSignalOffer();
3258 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003259 MediaExpectations media_expectations;
3260 media_expectations.ExpectBidirectionalAudioAndVideo();
3261 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003262}
3263
3264// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003265TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07003266 PeerConnectionFactory::Options dtls_10_options;
3267 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3268 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
3269 dtls_10_options));
3270 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003271 caller()->AddAudioVideoTracks();
3272 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003273 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003274 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003275 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07003276 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07003277 kDefaultTimeout);
3278 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07003279 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003280 // TODO(bugs.webrtc.org/9456): Fix it.
Ying Wangef3998f2019-12-09 13:06:53 +01003281 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
3282 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
3283 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07003284}
3285
3286// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003287TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07003288 PeerConnectionFactory::Options dtls_12_options;
3289 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3290 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
3291 dtls_12_options));
3292 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003293 caller()->AddAudioVideoTracks();
3294 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003295 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003296 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003297 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07003298 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07003299 kDefaultTimeout);
3300 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07003301 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003302 // TODO(bugs.webrtc.org/9456): Fix it.
Ying Wangef3998f2019-12-09 13:06:53 +01003303 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
3304 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
3305 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07003306}
3307
3308// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
3309// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003310TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07003311 PeerConnectionFactory::Options caller_options;
3312 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3313 PeerConnectionFactory::Options callee_options;
3314 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3315 ASSERT_TRUE(
3316 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
3317 ConnectFakeSignaling();
3318 // Do normal offer/answer and wait for some frames to be received in each
3319 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003320 caller()->AddAudioVideoTracks();
3321 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003322 caller()->CreateAndSetAndSignalOffer();
3323 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003324 MediaExpectations media_expectations;
3325 media_expectations.ExpectBidirectionalAudioAndVideo();
3326 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003327}
3328
3329// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
3330// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003331TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07003332 PeerConnectionFactory::Options caller_options;
3333 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3334 PeerConnectionFactory::Options callee_options;
3335 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3336 ASSERT_TRUE(
3337 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
3338 ConnectFakeSignaling();
3339 // Do normal offer/answer and wait for some frames to be received in each
3340 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003341 caller()->AddAudioVideoTracks();
3342 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003343 caller()->CreateAndSetAndSignalOffer();
3344 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003345 MediaExpectations media_expectations;
3346 media_expectations.ExpectBidirectionalAudioAndVideo();
3347 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003348}
3349
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003350// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
3351// works as expected; the cipher should only be used if enabled by both sides.
3352TEST_P(PeerConnectionIntegrationTest,
3353 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
3354 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003355 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003356 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003357 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3358 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003359 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3360 TestNegotiatedCipherSuite(caller_options, callee_options,
3361 expected_cipher_suite);
3362}
3363
3364TEST_P(PeerConnectionIntegrationTest,
3365 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
3366 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003367 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3368 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003369 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003370 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003371 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3372 TestNegotiatedCipherSuite(caller_options, callee_options,
3373 expected_cipher_suite);
3374}
3375
3376TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
3377 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003378 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003379 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003380 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003381 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
3382 TestNegotiatedCipherSuite(caller_options, callee_options,
3383 expected_cipher_suite);
3384}
3385
deadbeef1dcb1642017-03-29 21:08:16 -07003386// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003387TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003388 bool local_gcm_enabled = false;
3389 bool remote_gcm_enabled = false;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003390 bool aes_ctr_enabled = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003391 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3392 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003393 aes_ctr_enabled, expected_cipher_suite);
deadbeef1dcb1642017-03-29 21:08:16 -07003394}
3395
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003396// Test that a GCM cipher is used if both ends support it and non-GCM is
3397// disabled.
3398TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenOnlyGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003399 bool local_gcm_enabled = true;
3400 bool remote_gcm_enabled = true;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003401 bool aes_ctr_enabled = false;
deadbeef1dcb1642017-03-29 21:08:16 -07003402 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
3403 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003404 aes_ctr_enabled, expected_cipher_suite);
deadbeef1dcb1642017-03-29 21:08:16 -07003405}
3406
deadbeef7914b8c2017-04-21 03:23:33 -07003407// Verify that media can be transmitted end-to-end when GCM crypto suites are
3408// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
3409// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
3410// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003411TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07003412 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003413 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003414 gcm_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher = false;
deadbeef7914b8c2017-04-21 03:23:33 -07003415 ASSERT_TRUE(
3416 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
3417 ConnectFakeSignaling();
3418 // Do normal offer/answer and wait for some frames to be received in each
3419 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003420 caller()->AddAudioVideoTracks();
3421 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003422 caller()->CreateAndSetAndSignalOffer();
3423 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003424 MediaExpectations media_expectations;
3425 media_expectations.ExpectBidirectionalAudioAndVideo();
3426 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003427}
3428
deadbeef1dcb1642017-03-29 21:08:16 -07003429// This test sets up a call between two parties with audio, video and an RTP
3430// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003431TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003432 PeerConnectionInterface::RTCConfiguration rtc_config;
3433 rtc_config.enable_rtp_data_channel = true;
3434 rtc_config.enable_dtls_srtp = false;
3435 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003436 ConnectFakeSignaling();
3437 // Expect that data channel created on caller side will show up for callee as
3438 // well.
3439 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003440 caller()->AddAudioVideoTracks();
3441 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003442 caller()->CreateAndSetAndSignalOffer();
3443 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3444 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003445 MediaExpectations media_expectations;
3446 media_expectations.ExpectBidirectionalAudioAndVideo();
3447 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003448 ASSERT_NE(nullptr, caller()->data_channel());
3449 ASSERT_NE(nullptr, callee()->data_channel());
3450 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3451 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3452
3453 // Ensure data can be sent in both directions.
3454 std::string data = "hello world";
3455 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3456 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3457 kDefaultTimeout);
3458 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3459 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3460 kDefaultTimeout);
3461}
3462
Eldar Rellod9ebe012020-03-18 20:41:45 +02003463TEST_P(PeerConnectionIntegrationTest, RtpDataChannelWorksAfterRollback) {
3464 PeerConnectionInterface::RTCConfiguration rtc_config;
3465 rtc_config.enable_rtp_data_channel = true;
3466 rtc_config.enable_dtls_srtp = false;
3467 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
3468 ConnectFakeSignaling();
3469 auto data_channel = caller()->pc()->CreateDataChannel("label_1", nullptr);
3470 ASSERT_TRUE(data_channel.get() != nullptr);
3471 caller()->CreateAndSetAndSignalOffer();
3472 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3473
3474 caller()->CreateDataChannel("label_2", nullptr);
3475 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
3476 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
3477 caller()->pc()->SetLocalDescription(observer,
3478 caller()->CreateOfferAndWait().release());
3479 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
3480 caller()->Rollback();
3481
3482 std::string data = "hello world";
3483 SendRtpDataWithRetries(data_channel, data, 5);
3484 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3485 kDefaultTimeout);
3486}
3487
deadbeef1dcb1642017-03-29 21:08:16 -07003488// Ensure that an RTP data channel is signaled as closed for the caller when
3489// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003490TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003491 RtpDataChannelSignaledClosedInCalleeOffer) {
3492 // Same procedure as above test.
Niels Möllerf06f9232018-08-07 12:32:18 +02003493 PeerConnectionInterface::RTCConfiguration rtc_config;
3494 rtc_config.enable_rtp_data_channel = true;
3495 rtc_config.enable_dtls_srtp = false;
3496 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003497 ConnectFakeSignaling();
3498 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003499 caller()->AddAudioVideoTracks();
3500 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003501 caller()->CreateAndSetAndSignalOffer();
3502 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3503 ASSERT_NE(nullptr, caller()->data_channel());
3504 ASSERT_NE(nullptr, callee()->data_channel());
3505 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3506 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3507
3508 // Close the data channel on the callee, and do an updated offer/answer.
3509 callee()->data_channel()->Close();
3510 callee()->CreateAndSetAndSignalOffer();
3511 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3512 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3513 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3514}
3515
3516// Tests that data is buffered in an RTP data channel until an observer is
3517// registered for it.
3518//
3519// NOTE: RTP data channels can receive data before the underlying
3520// transport has detected that a channel is writable and thus data can be
3521// received before the data channel state changes to open. That is hard to test
3522// but the same buffering is expected to be used in that case.
Yves Gerey100fe632020-01-17 19:15:53 +01003523//
3524// Use fake clock and simulated network delay so that we predictably can wait
3525// until an SCTP message has been delivered without "sleep()"ing.
3526TEST_P(PeerConnectionIntegrationTestWithFakeClock,
deadbeef1dcb1642017-03-29 21:08:16 -07003527 DataBufferedUntilRtpDataChannelObserverRegistered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003528 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3529 virtual_socket_server()->UpdateDelayDistribution();
3530
Niels Möllerf06f9232018-08-07 12:32:18 +02003531 PeerConnectionInterface::RTCConfiguration rtc_config;
3532 rtc_config.enable_rtp_data_channel = true;
3533 rtc_config.enable_dtls_srtp = false;
3534 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003535 ConnectFakeSignaling();
3536 caller()->CreateDataChannel();
3537 caller()->CreateAndSetAndSignalOffer();
3538 ASSERT_TRUE(caller()->data_channel() != nullptr);
3539 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
Yves Gerey100fe632020-01-17 19:15:53 +01003540 kDefaultTimeout, FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003541 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
Yves Gerey100fe632020-01-17 19:15:53 +01003542 kDefaultTimeout, FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003543 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3544 callee()->data_channel()->state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01003545 FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003546
3547 // Unregister the observer which is normally automatically registered.
3548 callee()->data_channel()->UnregisterObserver();
3549 // Send data and advance fake clock until it should have been received.
3550 std::string data = "hello world";
3551 caller()->data_channel()->Send(DataBuffer(data));
Yves Gerey100fe632020-01-17 19:15:53 +01003552 SIMULATED_WAIT(false, 50, FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003553
3554 // Attach data channel and expect data to be received immediately. Note that
3555 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3556 // further, but data can be received even if the callback is asynchronous.
3557 MockDataChannelObserver new_observer(callee()->data_channel());
3558 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01003559 FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003560}
3561
3562// This test sets up a call between two parties with audio, video and but only
3563// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003564TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003565 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3566 rtc_config_1.enable_rtp_data_channel = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003567 // Must disable DTLS to make negotiation succeed.
Niels Möllerf06f9232018-08-07 12:32:18 +02003568 rtc_config_1.enable_dtls_srtp = false;
3569 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3570 rtc_config_2.enable_dtls_srtp = false;
3571 rtc_config_2.enable_dtls_srtp = false;
3572 ASSERT_TRUE(
3573 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
deadbeef1dcb1642017-03-29 21:08:16 -07003574 ConnectFakeSignaling();
3575 caller()->CreateDataChannel();
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02003576 ASSERT_TRUE(caller()->data_channel() != nullptr);
Steve Anton15324772018-01-16 10:26:49 -08003577 caller()->AddAudioVideoTracks();
3578 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003579 caller()->CreateAndSetAndSignalOffer();
3580 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3581 // The caller should still have a data channel, but it should be closed, and
3582 // one should ever have been created for the callee.
3583 EXPECT_TRUE(caller()->data_channel() != nullptr);
3584 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3585 EXPECT_EQ(nullptr, callee()->data_channel());
3586}
3587
3588// This test sets up a call between two parties with audio, and video. When
3589// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003590TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003591 PeerConnectionInterface::RTCConfiguration rtc_config;
3592 rtc_config.enable_rtp_data_channel = true;
3593 rtc_config.enable_dtls_srtp = false;
3594 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003595 ConnectFakeSignaling();
3596 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003597 caller()->AddAudioVideoTracks();
3598 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003599 caller()->CreateAndSetAndSignalOffer();
3600 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3601 // Create data channel and do new offer and answer.
3602 caller()->CreateDataChannel();
3603 caller()->CreateAndSetAndSignalOffer();
3604 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3605 ASSERT_NE(nullptr, caller()->data_channel());
3606 ASSERT_NE(nullptr, callee()->data_channel());
3607 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3608 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3609 // Ensure data can be sent in both directions.
3610 std::string data = "hello world";
3611 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3612 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3613 kDefaultTimeout);
3614 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3615 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3616 kDefaultTimeout);
3617}
3618
3619#ifdef HAVE_SCTP
3620
3621// This test sets up a call between two parties with audio, video and an SCTP
3622// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003623TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003624 ASSERT_TRUE(CreatePeerConnectionWrappers());
3625 ConnectFakeSignaling();
3626 // Expect that data channel created on caller side will show up for callee as
3627 // well.
3628 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003629 caller()->AddAudioVideoTracks();
3630 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003631 caller()->CreateAndSetAndSignalOffer();
3632 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3633 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003634 MediaExpectations media_expectations;
3635 media_expectations.ExpectBidirectionalAudioAndVideo();
3636 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003637 // Caller data channel should already exist (it created one). Callee data
3638 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3639 ASSERT_NE(nullptr, caller()->data_channel());
3640 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3641 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3642 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3643
3644 // Ensure data can be sent in both directions.
3645 std::string data = "hello world";
3646 caller()->data_channel()->Send(DataBuffer(data));
3647 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3648 kDefaultTimeout);
3649 callee()->data_channel()->Send(DataBuffer(data));
3650 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3651 kDefaultTimeout);
3652}
3653
3654// Ensure that when the callee closes an SCTP data channel, the closing
3655// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003656TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003657 // Same procedure as above test.
3658 ASSERT_TRUE(CreatePeerConnectionWrappers());
3659 ConnectFakeSignaling();
3660 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003661 caller()->AddAudioVideoTracks();
3662 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003663 caller()->CreateAndSetAndSignalOffer();
3664 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3665 ASSERT_NE(nullptr, caller()->data_channel());
3666 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3667 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3668 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3669
3670 // Close the data channel on the callee side, and wait for it to reach the
3671 // "closed" state on both sides.
3672 callee()->data_channel()->Close();
3673 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3674 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3675}
3676
Seth Hampson2f0d7022018-02-20 11:54:42 -08003677TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003678 ASSERT_TRUE(CreatePeerConnectionWrappers());
3679 ConnectFakeSignaling();
3680 webrtc::DataChannelInit init;
3681 init.id = 53;
3682 init.maxRetransmits = 52;
3683 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003684 caller()->AddAudioVideoTracks();
3685 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003686 caller()->CreateAndSetAndSignalOffer();
3687 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003688 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3689 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02003690 // Since "negotiated" is false, the "id" parameter should be ignored.
3691 EXPECT_NE(init.id, callee()->data_channel()->id());
Steve Antonda6c0952017-10-23 11:41:54 -07003692 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3693 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3694 EXPECT_FALSE(callee()->data_channel()->negotiated());
3695}
3696
deadbeef1dcb1642017-03-29 21:08:16 -07003697// Test usrsctp's ability to process unordered data stream, where data actually
3698// arrives out of order using simulated delays. Previously there have been some
3699// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003700TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003701 // Introduce random network delays.
3702 // Otherwise it's not a true "unordered" test.
3703 virtual_socket_server()->set_delay_mean(20);
3704 virtual_socket_server()->set_delay_stddev(5);
3705 virtual_socket_server()->UpdateDelayDistribution();
3706 // Normal procedure, but with unordered data channel config.
3707 ASSERT_TRUE(CreatePeerConnectionWrappers());
3708 ConnectFakeSignaling();
3709 webrtc::DataChannelInit init;
3710 init.ordered = false;
3711 caller()->CreateDataChannel(&init);
3712 caller()->CreateAndSetAndSignalOffer();
3713 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3714 ASSERT_NE(nullptr, caller()->data_channel());
3715 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3716 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3717 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3718
3719 static constexpr int kNumMessages = 100;
3720 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3721 static constexpr size_t kMaxMessageSize = 4096;
3722 // Create and send random messages.
3723 std::vector<std::string> sent_messages;
3724 for (int i = 0; i < kNumMessages; ++i) {
3725 size_t length =
3726 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3727 std::string message;
3728 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3729 caller()->data_channel()->Send(DataBuffer(message));
3730 callee()->data_channel()->Send(DataBuffer(message));
3731 sent_messages.push_back(message);
3732 }
3733
3734 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003735 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003736 caller()->data_observer()->received_message_count(),
3737 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003738 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003739 callee()->data_observer()->received_message_count(),
3740 kDefaultTimeout);
3741
3742 // Sort and compare to make sure none of the messages were corrupted.
3743 std::vector<std::string> caller_received_messages =
3744 caller()->data_observer()->messages();
3745 std::vector<std::string> callee_received_messages =
3746 callee()->data_observer()->messages();
Steve Anton64b626b2019-01-28 17:25:26 -08003747 absl::c_sort(sent_messages);
3748 absl::c_sort(caller_received_messages);
3749 absl::c_sort(callee_received_messages);
deadbeef1dcb1642017-03-29 21:08:16 -07003750 EXPECT_EQ(sent_messages, caller_received_messages);
3751 EXPECT_EQ(sent_messages, callee_received_messages);
3752}
3753
3754// This test sets up a call between two parties with audio, and video. When
3755// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003756TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003757 ASSERT_TRUE(CreatePeerConnectionWrappers());
3758 ConnectFakeSignaling();
3759 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003760 caller()->AddAudioVideoTracks();
3761 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003762 caller()->CreateAndSetAndSignalOffer();
3763 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3764 // Create data channel and do new offer and answer.
3765 caller()->CreateDataChannel();
3766 caller()->CreateAndSetAndSignalOffer();
3767 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3768 // Caller data channel should already exist (it created one). Callee data
3769 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3770 ASSERT_NE(nullptr, caller()->data_channel());
3771 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3772 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3773 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3774 // Ensure data can be sent in both directions.
3775 std::string data = "hello world";
3776 caller()->data_channel()->Send(DataBuffer(data));
3777 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3778 kDefaultTimeout);
3779 callee()->data_channel()->Send(DataBuffer(data));
3780 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3781 kDefaultTimeout);
3782}
3783
deadbeef7914b8c2017-04-21 03:23:33 -07003784// Set up a connection initially just using SCTP data channels, later upgrading
3785// to audio/video, ensuring frames are received end-to-end. Effectively the
3786// inverse of the test above.
3787// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003788TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003789 ASSERT_TRUE(CreatePeerConnectionWrappers());
3790 ConnectFakeSignaling();
3791 // Do initial offer/answer with just data channel.
3792 caller()->CreateDataChannel();
3793 caller()->CreateAndSetAndSignalOffer();
3794 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3795 // Wait until data can be sent over the data channel.
3796 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3797 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3798 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3799
3800 // Do subsequent offer/answer with two-way audio and video. Audio and video
3801 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003802 caller()->AddAudioVideoTracks();
3803 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003804 caller()->CreateAndSetAndSignalOffer();
3805 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003806 MediaExpectations media_expectations;
3807 media_expectations.ExpectBidirectionalAudioAndVideo();
3808 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003809}
3810
deadbeef8b7e9ad2017-05-25 09:38:55 -07003811static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
Harald Alvestrand5fc28b12019-05-13 13:36:16 +02003812 cricket::SctpDataContentDescription* dcd_offer =
3813 GetFirstSctpDataContentDescription(desc);
Harald Alvestrand17ea0682019-12-13 11:51:04 +01003814 // See https://crbug.com/webrtc/11211 - this function is a no-op
Steve Antonb1c1de12017-12-21 15:14:30 -08003815 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003816 dcd_offer->set_use_sctpmap(false);
3817 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3818}
3819
3820// Test that the data channel works when a spec-compliant SCTP m= section is
3821// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3822// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003823TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003824 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3825 ASSERT_TRUE(CreatePeerConnectionWrappers());
3826 ConnectFakeSignaling();
3827 caller()->CreateDataChannel();
3828 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3829 caller()->CreateAndSetAndSignalOffer();
3830 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3831 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3832 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3833 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3834
3835 // Ensure data can be sent in both directions.
3836 std::string data = "hello world";
3837 caller()->data_channel()->Send(DataBuffer(data));
3838 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3839 kDefaultTimeout);
3840 callee()->data_channel()->Send(DataBuffer(data));
3841 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3842 kDefaultTimeout);
3843}
3844
deadbeef1dcb1642017-03-29 21:08:16 -07003845#endif // HAVE_SCTP
3846
3847// Test that the ICE connection and gathering states eventually reach
3848// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003849TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003850 ASSERT_TRUE(CreatePeerConnectionWrappers());
3851 ConnectFakeSignaling();
3852 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003853 caller()->AddAudioVideoTracks();
3854 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003855 caller()->CreateAndSetAndSignalOffer();
3856 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3857 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3858 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3859 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3860 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3861 // After the best candidate pair is selected and all candidates are signaled,
3862 // the ICE connection state should reach "complete".
3863 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3864 // answerer/"callee" by default) only reaches "connected". When this is
3865 // fixed, this test should be updated.
3866 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3867 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00003868 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3869 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003870}
3871
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003872constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
3873 cricket::PORTALLOCATOR_DISABLE_RELAY |
3874 cricket::PORTALLOCATOR_DISABLE_TCP;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003875
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003876// Use a mock resolver to resolve the hostname back to the original IP on both
3877// sides and check that the ICE connection connects.
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003878TEST_P(PeerConnectionIntegrationTest,
3879 IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003880 auto caller_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003881 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003882 auto callee_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003883 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003884 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
3885 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003886
3887 // This also verifies that the injected AsyncResolverFactory is used by
3888 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003889 EXPECT_CALL(*caller_resolver_factory, Create())
3890 .WillOnce(Return(&caller_async_resolver));
3891 webrtc::PeerConnectionDependencies caller_deps(nullptr);
3892 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
3893
3894 EXPECT_CALL(*callee_resolver_factory, Create())
3895 .WillOnce(Return(&callee_async_resolver));
3896 webrtc::PeerConnectionDependencies callee_deps(nullptr);
3897 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
3898
3899 PeerConnectionInterface::RTCConfiguration config;
3900 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3901 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3902
3903 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3904 config, std::move(caller_deps), config, std::move(callee_deps)));
3905
3906 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
3907 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
3908
3909 // Enable hostname candidates with mDNS names.
Qingsi Wangecd30542019-05-22 14:34:56 -07003910 caller()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003911 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wangecd30542019-05-22 14:34:56 -07003912 callee()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003913 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003914
3915 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003916
3917 ConnectFakeSignaling();
3918 caller()->AddAudioVideoTracks();
3919 callee()->AddAudioVideoTracks();
3920 caller()->CreateAndSetAndSignalOffer();
3921 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3922 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3923 caller()->ice_connection_state(), kDefaultTimeout);
3924 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3925 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08003926
Ying Wangef3998f2019-12-09 13:06:53 +01003927 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
3928 "WebRTC.PeerConnection.CandidatePairType_UDP",
3929 webrtc::kIceCandidatePairHostNameHostName));
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003930}
3931
Steve Antonede9ca52017-10-16 13:04:27 -07003932// Test that firewalling the ICE connection causes the clients to identify the
3933// disconnected state and then removing the firewall causes them to reconnect.
3934class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003935 : public PeerConnectionIntegrationBaseTest,
3936 public ::testing::WithParamInterface<
3937 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003938 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003939 PeerConnectionIntegrationIceStatesTest()
3940 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3941 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003942 }
3943
3944 void StartStunServer(const SocketAddress& server_address) {
3945 stun_server_.reset(
3946 cricket::TestStunServer::Create(network_thread(), server_address));
3947 }
3948
3949 bool TestIPv6() {
3950 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3951 }
3952
3953 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003954 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
3955 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07003956 }
3957
3958 std::vector<SocketAddress> CallerAddresses() {
3959 std::vector<SocketAddress> addresses;
3960 addresses.push_back(SocketAddress("1.1.1.1", 0));
3961 if (TestIPv6()) {
3962 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3963 }
3964 return addresses;
3965 }
3966
3967 std::vector<SocketAddress> CalleeAddresses() {
3968 std::vector<SocketAddress> addresses;
3969 addresses.push_back(SocketAddress("2.2.2.2", 0));
3970 if (TestIPv6()) {
3971 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3972 }
3973 return addresses;
3974 }
3975
3976 void SetUpNetworkInterfaces() {
3977 // Remove the default interfaces added by the test infrastructure.
Qingsi Wangecd30542019-05-22 14:34:56 -07003978 caller()->network_manager()->RemoveInterface(kDefaultLocalAddress);
3979 callee()->network_manager()->RemoveInterface(kDefaultLocalAddress);
Steve Antonede9ca52017-10-16 13:04:27 -07003980
3981 // Add network addresses for test.
3982 for (const auto& caller_address : CallerAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07003983 caller()->network_manager()->AddInterface(caller_address);
Steve Antonede9ca52017-10-16 13:04:27 -07003984 }
3985 for (const auto& callee_address : CalleeAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07003986 callee()->network_manager()->AddInterface(callee_address);
Steve Antonede9ca52017-10-16 13:04:27 -07003987 }
3988 }
3989
3990 private:
3991 uint32_t port_allocator_flags_;
3992 std::unique_ptr<cricket::TestStunServer> stun_server_;
3993};
3994
Yves Gerey100fe632020-01-17 19:15:53 +01003995// Ensure FakeClockForTest is constructed first (see class for rationale).
3996class PeerConnectionIntegrationIceStatesTestWithFakeClock
3997 : public FakeClockForTest,
3998 public PeerConnectionIntegrationIceStatesTest {};
3999
Steve Antonede9ca52017-10-16 13:04:27 -07004000// Tests that the PeerConnection goes through all the ICE gathering/connection
4001// states over the duration of the call. This includes Disconnected and Failed
4002// states, induced by putting a firewall between the peers and waiting for them
4003// to time out.
Yves Gerey100fe632020-01-17 19:15:53 +01004004TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock, VerifyIceStates) {
Steve Antonede9ca52017-10-16 13:04:27 -07004005 const SocketAddress kStunServerAddress =
4006 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
4007 StartStunServer(kStunServerAddress);
4008
4009 PeerConnectionInterface::RTCConfiguration config;
4010 PeerConnectionInterface::IceServer ice_stun_server;
4011 ice_stun_server.urls.push_back(
4012 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
4013 kStunServerAddress.PortAsString());
4014 config.servers.push_back(ice_stun_server);
4015
4016 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4017 ConnectFakeSignaling();
4018 SetPortAllocatorFlags();
4019 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004020 caller()->AddAudioVideoTracks();
4021 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004022
4023 // Initial state before anything happens.
4024 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
4025 caller()->ice_gathering_state());
4026 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
4027 caller()->ice_connection_state());
Jonas Olsson7a6739e2019-01-15 16:31:55 +01004028 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
4029 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07004030
4031 // Start the call by creating the offer, setting it as the local description,
4032 // then sending it to the peer who will respond with an answer. This happens
4033 // asynchronously so that we can watch the states as it runs in the
4034 // background.
4035 caller()->CreateAndSetAndSignalOffer();
4036
Steve Antona9b67ce2020-01-16 14:00:44 -08004037 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4038 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004039 FakeClock());
Steve Antona9b67ce2020-01-16 14:00:44 -08004040 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4041 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004042 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004043
4044 // Verify that the observer was notified of the intermediate transitions.
4045 EXPECT_THAT(caller()->ice_connection_state_history(),
4046 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
4047 PeerConnectionInterface::kIceConnectionConnected,
4048 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004049 EXPECT_THAT(caller()->standardized_ice_connection_state_history(),
4050 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
4051 PeerConnectionInterface::kIceConnectionConnected,
4052 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02004053 EXPECT_THAT(
4054 caller()->peer_connection_state_history(),
4055 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02004056 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07004057 EXPECT_THAT(caller()->ice_gathering_state_history(),
4058 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
4059 PeerConnectionInterface::kIceGatheringComplete));
4060
4061 // Block connections to/from the caller and wait for ICE to become
4062 // disconnected.
4063 for (const auto& caller_address : CallerAddresses()) {
4064 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4065 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004066 RTC_LOG(LS_INFO) << "Firewall rules applied";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004067 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
4068 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004069 FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004070 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
4071 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004072 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004073
4074 // Let ICE re-establish by removing the firewall rules.
4075 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01004076 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004077 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4078 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004079 FakeClock());
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004080 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004081 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004082 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004083
4084 // According to RFC7675, if there is no response within 30 seconds then the
4085 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08004086 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07004087 constexpr int kConsentTimeout = 30000;
4088 for (const auto& caller_address : CallerAddresses()) {
4089 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4090 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004091 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004092 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4093 caller()->ice_connection_state(), kConsentTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004094 FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004095 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4096 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004097 kConsentTimeout, FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004098}
4099
4100// Tests that if the connection doesn't get set up properly we eventually reach
4101// the "failed" iceConnectionState.
Yves Gerey100fe632020-01-17 19:15:53 +01004102TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock,
4103 IceStateSetupFailure) {
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004104 // Block connections to/from the caller and wait for ICE to become
4105 // disconnected.
4106 for (const auto& caller_address : CallerAddresses()) {
4107 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4108 }
4109
4110 ASSERT_TRUE(CreatePeerConnectionWrappers());
4111 ConnectFakeSignaling();
4112 SetPortAllocatorFlags();
4113 SetUpNetworkInterfaces();
4114 caller()->AddAudioVideoTracks();
4115 caller()->CreateAndSetAndSignalOffer();
4116
4117 // According to RFC7675, if there is no response within 30 seconds then the
4118 // peer should consider the other side to have rejected the connection. This
4119 // is signaled by the state transitioning to "failed".
4120 constexpr int kConsentTimeout = 30000;
4121 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4122 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004123 kConsentTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004124}
4125
4126// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
4127// and that the statistics in the metric observers are updated correctly.
4128TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
4129 ASSERT_TRUE(CreatePeerConnectionWrappers());
4130 ConnectFakeSignaling();
4131 SetPortAllocatorFlags();
4132 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004133 caller()->AddAudioVideoTracks();
4134 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004135 caller()->CreateAndSetAndSignalOffer();
4136
4137 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton692f3c72020-01-16 14:12:31 -08004138 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4139 caller()->ice_connection_state(), kDefaultTimeout);
4140 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4141 callee()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07004142
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004143 // TODO(bugs.webrtc.org/9456): Fix it.
4144 const int num_best_ipv4 = webrtc::metrics::NumEvents(
4145 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
4146 const int num_best_ipv6 = webrtc::metrics::NumEvents(
4147 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004148 if (TestIPv6()) {
4149 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
4150 // connection.
Ying Wangef3998f2019-12-09 13:06:53 +01004151 EXPECT_METRIC_EQ(0, num_best_ipv4);
4152 EXPECT_METRIC_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004153 } else {
Ying Wangef3998f2019-12-09 13:06:53 +01004154 EXPECT_METRIC_EQ(1, num_best_ipv4);
4155 EXPECT_METRIC_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004156 }
4157
Ying Wangef3998f2019-12-09 13:06:53 +01004158 EXPECT_METRIC_EQ(0, webrtc::metrics::NumEvents(
4159 "WebRTC.PeerConnection.CandidatePairType_UDP",
4160 webrtc::kIceCandidatePairHostHost));
4161 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
4162 "WebRTC.PeerConnection.CandidatePairType_UDP",
4163 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07004164}
4165
4166constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
4167 cricket::PORTALLOCATOR_DISABLE_STUN |
4168 cricket::PORTALLOCATOR_DISABLE_RELAY;
4169constexpr uint32_t kFlagsIPv6NoStun =
4170 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
4171 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
4172constexpr uint32_t kFlagsIPv4Stun =
4173 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
4174
Mirko Bonadeic84f6612019-01-31 12:20:57 +01004175INSTANTIATE_TEST_SUITE_P(
Seth Hampson2f0d7022018-02-20 11:54:42 -08004176 PeerConnectionIntegrationTest,
4177 PeerConnectionIntegrationIceStatesTest,
4178 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4179 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
4180 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
4181 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
Steve Antonede9ca52017-10-16 13:04:27 -07004182
Yves Gerey100fe632020-01-17 19:15:53 +01004183INSTANTIATE_TEST_SUITE_P(
4184 PeerConnectionIntegrationTest,
4185 PeerConnectionIntegrationIceStatesTestWithFakeClock,
4186 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4187 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
4188 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
4189 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
4190
deadbeef1dcb1642017-03-29 21:08:16 -07004191// This test sets up a call between two parties with audio and video.
4192// During the call, the caller restarts ICE and the test verifies that
4193// new ICE candidates are generated and audio and video still can flow, and the
4194// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004195TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07004196 ASSERT_TRUE(CreatePeerConnectionWrappers());
4197 ConnectFakeSignaling();
4198 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08004199 caller()->AddAudioVideoTracks();
4200 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004201 caller()->CreateAndSetAndSignalOffer();
4202 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4203 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4204 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004205 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4206 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07004207
4208 // To verify that the ICE restart actually occurs, get
4209 // ufrag/password/candidates before and after restart.
4210 // Create an SDP string of the first audio candidate for both clients.
4211 const webrtc::IceCandidateCollection* audio_candidates_caller =
4212 caller()->pc()->local_description()->candidates(0);
4213 const webrtc::IceCandidateCollection* audio_candidates_callee =
4214 callee()->pc()->local_description()->candidates(0);
4215 ASSERT_GT(audio_candidates_caller->count(), 0u);
4216 ASSERT_GT(audio_candidates_callee->count(), 0u);
4217 std::string caller_candidate_pre_restart;
4218 ASSERT_TRUE(
4219 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
4220 std::string callee_candidate_pre_restart;
4221 ASSERT_TRUE(
4222 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
4223 const cricket::SessionDescription* desc =
4224 caller()->pc()->local_description()->description();
4225 std::string caller_ufrag_pre_restart =
4226 desc->transport_infos()[0].description.ice_ufrag;
4227 desc = callee()->pc()->local_description()->description();
4228 std::string callee_ufrag_pre_restart =
4229 desc->transport_infos()[0].description.ice_ufrag;
4230
Alex Drake00c7ecf2019-08-06 10:54:47 -07004231 EXPECT_EQ(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07004232 // Have the caller initiate an ICE restart.
4233 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
4234 caller()->CreateAndSetAndSignalOffer();
4235 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4236 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4237 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004238 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07004239 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4240
4241 // Grab the ufrags/candidates again.
4242 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
4243 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
4244 ASSERT_GT(audio_candidates_caller->count(), 0u);
4245 ASSERT_GT(audio_candidates_callee->count(), 0u);
4246 std::string caller_candidate_post_restart;
4247 ASSERT_TRUE(
4248 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
4249 std::string callee_candidate_post_restart;
4250 ASSERT_TRUE(
4251 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
4252 desc = caller()->pc()->local_description()->description();
4253 std::string caller_ufrag_post_restart =
4254 desc->transport_infos()[0].description.ice_ufrag;
4255 desc = callee()->pc()->local_description()->description();
4256 std::string callee_ufrag_post_restart =
4257 desc->transport_infos()[0].description.ice_ufrag;
4258 // Sanity check that an ICE restart was actually negotiated in SDP.
4259 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
4260 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
4261 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
4262 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
Alex Drake00c7ecf2019-08-06 10:54:47 -07004263 EXPECT_GT(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07004264
4265 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004266 MediaExpectations media_expectations;
4267 media_expectations.ExpectBidirectionalAudioAndVideo();
4268 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004269}
4270
4271// Verify that audio/video can be received end-to-end when ICE renomination is
4272// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004273TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07004274 PeerConnectionInterface::RTCConfiguration config;
4275 config.enable_ice_renomination = true;
4276 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4277 ConnectFakeSignaling();
4278 // Do normal offer/answer and wait for some frames to be received in each
4279 // direction.
Steve Anton15324772018-01-16 10:26:49 -08004280 caller()->AddAudioVideoTracks();
4281 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004282 caller()->CreateAndSetAndSignalOffer();
4283 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4284 // Sanity check that ICE renomination was actually negotiated.
4285 const cricket::SessionDescription* desc =
4286 caller()->pc()->local_description()->description();
4287 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004288 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004289 }
4290 desc = callee()->pc()->local_description()->description();
4291 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004292 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004293 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08004294 MediaExpectations media_expectations;
4295 media_expectations.ExpectBidirectionalAudioAndVideo();
4296 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004297}
4298
Steve Anton6f25b092017-10-23 09:39:20 -07004299// With a max bundle policy and RTCP muxing, adding a new media description to
4300// the connection should not affect ICE at all because the new media will use
4301// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004302TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08004303 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07004304 PeerConnectionInterface::RTCConfiguration config;
4305 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4306 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4307 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
4308 config, PeerConnectionInterface::RTCConfiguration()));
4309 ConnectFakeSignaling();
4310
Steve Anton15324772018-01-16 10:26:49 -08004311 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004312 caller()->CreateAndSetAndSignalOffer();
4313 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07004314 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4315 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07004316
4317 caller()->clear_ice_connection_state_history();
4318
Steve Anton15324772018-01-16 10:26:49 -08004319 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004320 caller()->CreateAndSetAndSignalOffer();
4321 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4322
4323 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
4324}
4325
deadbeef1dcb1642017-03-29 21:08:16 -07004326// This test sets up a call between two parties with audio and video. It then
4327// renegotiates setting the video m-line to "port 0", then later renegotiates
4328// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004329TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07004330 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
4331 ASSERT_TRUE(CreatePeerConnectionWrappers());
4332 ConnectFakeSignaling();
4333
4334 // Do initial negotiation, only sending media from the caller. Will result in
4335 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08004336 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004337 caller()->CreateAndSetAndSignalOffer();
4338 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4339
4340 // Negotiate again, disabling the video "m=" section (the callee will set the
4341 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004342 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4343 PeerConnectionInterface::RTCOfferAnswerOptions options;
4344 options.offer_to_receive_video = 0;
4345 callee()->SetOfferAnswerOptions(options);
4346 } else {
4347 callee()->SetRemoteOfferHandler([this] {
4348 callee()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)->Stop();
4349 });
4350 }
deadbeef1dcb1642017-03-29 21:08:16 -07004351 caller()->CreateAndSetAndSignalOffer();
4352 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4353 // Sanity check that video "m=" section was actually rejected.
4354 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
4355 callee()->pc()->local_description()->description());
4356 ASSERT_NE(nullptr, answer_video_content);
4357 ASSERT_TRUE(answer_video_content->rejected);
4358
4359 // Enable video and do negotiation again, making sure video is received
4360 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004361 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4362 PeerConnectionInterface::RTCOfferAnswerOptions options;
4363 options.offer_to_receive_video = 1;
4364 callee()->SetOfferAnswerOptions(options);
4365 } else {
4366 // The caller's transceiver is stopped, so we need to add another track.
4367 auto caller_transceiver =
4368 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
4369 EXPECT_TRUE(caller_transceiver->stopped());
4370 caller()->AddVideoTrack();
4371 }
4372 callee()->AddVideoTrack();
4373 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07004374 caller()->CreateAndSetAndSignalOffer();
4375 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004376
deadbeef1dcb1642017-03-29 21:08:16 -07004377 // Verify the caller receives frames from the newly added stream, and the
4378 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004379 MediaExpectations media_expectations;
4380 media_expectations.CalleeExpectsSomeAudio();
4381 media_expectations.ExpectBidirectionalVideo();
4382 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004383}
4384
deadbeef1dcb1642017-03-29 21:08:16 -07004385// This tests that if we negotiate after calling CreateSender but before we
4386// have a track, then set a track later, frames from the newly-set track are
4387// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004388TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07004389 MediaFlowsAfterEarlyWarmupWithCreateSender) {
4390 ASSERT_TRUE(CreatePeerConnectionWrappers());
4391 ConnectFakeSignaling();
4392 auto caller_audio_sender =
4393 caller()->pc()->CreateSender("audio", "caller_stream");
4394 auto caller_video_sender =
4395 caller()->pc()->CreateSender("video", "caller_stream");
4396 auto callee_audio_sender =
4397 callee()->pc()->CreateSender("audio", "callee_stream");
4398 auto callee_video_sender =
4399 callee()->pc()->CreateSender("video", "callee_stream");
4400 caller()->CreateAndSetAndSignalOffer();
4401 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4402 // Wait for ICE to complete, without any tracks being set.
4403 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4404 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4405 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4406 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4407 // Now set the tracks, and expect frames to immediately start flowing.
4408 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4409 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4410 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4411 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08004412 MediaExpectations media_expectations;
4413 media_expectations.ExpectBidirectionalAudioAndVideo();
4414 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4415}
4416
4417// This tests that if we negotiate after calling AddTransceiver but before we
4418// have a track, then set a track later, frames from the newly-set tracks are
4419// received end-to-end.
4420TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
4421 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
4422 ASSERT_TRUE(CreatePeerConnectionWrappers());
4423 ConnectFakeSignaling();
4424 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
4425 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
4426 auto caller_audio_sender = audio_result.MoveValue()->sender();
4427 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
4428 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
4429 auto caller_video_sender = video_result.MoveValue()->sender();
4430 callee()->SetRemoteOfferHandler([this] {
4431 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
4432 callee()->pc()->GetTransceivers()[0]->SetDirection(
4433 RtpTransceiverDirection::kSendRecv);
4434 callee()->pc()->GetTransceivers()[1]->SetDirection(
4435 RtpTransceiverDirection::kSendRecv);
4436 });
4437 caller()->CreateAndSetAndSignalOffer();
4438 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4439 // Wait for ICE to complete, without any tracks being set.
4440 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4441 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4442 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4443 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4444 // Now set the tracks, and expect frames to immediately start flowing.
4445 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
4446 auto callee_video_sender = callee()->pc()->GetSenders()[1];
4447 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4448 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4449 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4450 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
4451 MediaExpectations media_expectations;
4452 media_expectations.ExpectBidirectionalAudioAndVideo();
4453 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004454}
4455
4456// This test verifies that a remote video track can be added via AddStream,
4457// and sent end-to-end. For this particular test, it's simply echoed back
4458// from the caller to the callee, rather than being forwarded to a third
4459// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004460TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07004461 ASSERT_TRUE(CreatePeerConnectionWrappers());
4462 ConnectFakeSignaling();
4463 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08004464 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07004465 caller()->CreateAndSetAndSignalOffer();
4466 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004467 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07004468
4469 // Echo the stream back, and do a new offer/anwer (initiated by callee this
4470 // time).
4471 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
4472 callee()->CreateAndSetAndSignalOffer();
4473 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4474
Seth Hampson2f0d7022018-02-20 11:54:42 -08004475 MediaExpectations media_expectations;
4476 media_expectations.ExpectBidirectionalVideo();
4477 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004478}
4479
4480// Test that we achieve the expected end-to-end connection time, using a
4481// fake clock and simulated latency on the media and signaling paths.
4482// We use a TURN<->TURN connection because this is usually the quickest to
4483// set up initially, especially when we're confident the connection will work
4484// and can start sending media before we get a STUN response.
4485//
4486// With various optimizations enabled, here are the network delays we expect to
4487// be on the critical path:
4488// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
4489// signaling answer (with DTLS fingerprint).
4490// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
4491// using TURN<->TURN pair, and DTLS exchange is 4 packets,
4492// the first of which should have arrived before the answer.
Yves Gerey100fe632020-01-17 19:15:53 +01004493TEST_P(PeerConnectionIntegrationTestWithFakeClock,
4494 EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07004495 static constexpr int media_hop_delay_ms = 50;
4496 static constexpr int signaling_trip_delay_ms = 500;
4497 // For explanation of these values, see comment above.
4498 static constexpr int required_media_hops = 9;
4499 static constexpr int required_signaling_trips = 2;
4500 // For internal delays (such as posting an event asychronously).
4501 static constexpr int allowed_internal_delay_ms = 20;
4502 static constexpr int total_connection_time_ms =
4503 media_hop_delay_ms * required_media_hops +
4504 signaling_trip_delay_ms * required_signaling_trips +
4505 allowed_internal_delay_ms;
4506
4507 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4508 3478};
4509 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4510 0};
4511 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4512 3478};
4513 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4514 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004515 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
4516 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004517
Seth Hampsonaed71642018-06-11 07:41:32 -07004518 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
4519 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07004520 // Bypass permission check on received packets so media can be sent before
4521 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07004522 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
4523 turn_server_1->set_enable_permission_checks(false);
4524 });
4525 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
4526 turn_server_2->set_enable_permission_checks(false);
4527 });
deadbeef1dcb1642017-03-29 21:08:16 -07004528
4529 PeerConnectionInterface::RTCConfiguration client_1_config;
4530 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4531 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4532 ice_server_1.username = "test";
4533 ice_server_1.password = "test";
4534 client_1_config.servers.push_back(ice_server_1);
4535 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4536 client_1_config.presume_writable_when_fully_relayed = true;
4537
4538 PeerConnectionInterface::RTCConfiguration client_2_config;
4539 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4540 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4541 ice_server_2.username = "test";
4542 ice_server_2.password = "test";
4543 client_2_config.servers.push_back(ice_server_2);
4544 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4545 client_2_config.presume_writable_when_fully_relayed = true;
4546
4547 ASSERT_TRUE(
4548 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4549 // Set up the simulated delays.
4550 SetSignalingDelayMs(signaling_trip_delay_ms);
4551 ConnectFakeSignaling();
4552 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
4553 virtual_socket_server()->UpdateDelayDistribution();
4554
4555 // Set "offer to receive audio/video" without adding any tracks, so we just
4556 // set up ICE/DTLS with no media.
4557 PeerConnectionInterface::RTCOfferAnswerOptions options;
4558 options.offer_to_receive_audio = 1;
4559 options.offer_to_receive_video = 1;
4560 caller()->SetOfferAnswerOptions(options);
4561 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07004562 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
Yves Gerey100fe632020-01-17 19:15:53 +01004563 FakeClock());
Seth Hampson1d4a76d2018-06-19 14:31:41 -07004564 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
4565 // If this is not done a DCHECK can be hit in ports.cc, because a large
4566 // negative number is calculated for the rtt due to the global clock changing.
Steve Antond91969e2019-05-30 12:27:03 -07004567 ClosePeerConnections();
deadbeef1dcb1642017-03-29 21:08:16 -07004568}
4569
Jonas Orelandbdcee282017-10-10 14:01:40 +02004570// Verify that a TurnCustomizer passed in through RTCConfiguration
4571// is actually used by the underlying TURN candidate pair.
4572// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004573TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02004574 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4575 3478};
4576 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4577 0};
4578 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4579 3478};
4580 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4581 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004582 CreateTurnServer(turn_server_1_internal_address,
4583 turn_server_1_external_address);
4584 CreateTurnServer(turn_server_2_internal_address,
4585 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004586
4587 PeerConnectionInterface::RTCConfiguration client_1_config;
4588 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4589 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4590 ice_server_1.username = "test";
4591 ice_server_1.password = "test";
4592 client_1_config.servers.push_back(ice_server_1);
4593 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004594 auto* customizer1 = CreateTurnCustomizer();
4595 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004596
4597 PeerConnectionInterface::RTCConfiguration client_2_config;
4598 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4599 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4600 ice_server_2.username = "test";
4601 ice_server_2.password = "test";
4602 client_2_config.servers.push_back(ice_server_2);
4603 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004604 auto* customizer2 = CreateTurnCustomizer();
4605 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004606
4607 ASSERT_TRUE(
4608 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4609 ConnectFakeSignaling();
4610
4611 // Set "offer to receive audio/video" without adding any tracks, so we just
4612 // set up ICE/DTLS with no media.
4613 PeerConnectionInterface::RTCOfferAnswerOptions options;
4614 options.offer_to_receive_audio = 1;
4615 options.offer_to_receive_video = 1;
4616 caller()->SetOfferAnswerOptions(options);
4617 caller()->CreateAndSetAndSignalOffer();
4618 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4619
Seth Hampsonaed71642018-06-11 07:41:32 -07004620 ExpectTurnCustomizerCountersIncremented(customizer1);
4621 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004622}
4623
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004624// Verifies that you can use TCP instead of UDP to connect to a TURN server and
4625// send media between the caller and the callee.
4626TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
4627 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4628 3478};
4629 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4630
4631 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07004632 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4633 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004634
4635 webrtc::PeerConnectionInterface::IceServer ice_server;
4636 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4637 ice_server.username = "test";
4638 ice_server.password = "test";
4639
4640 PeerConnectionInterface::RTCConfiguration client_1_config;
4641 client_1_config.servers.push_back(ice_server);
4642 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4643
4644 PeerConnectionInterface::RTCConfiguration client_2_config;
4645 client_2_config.servers.push_back(ice_server);
4646 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4647
4648 ASSERT_TRUE(
4649 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4650
4651 // Do normal offer/answer and wait for ICE to complete.
4652 ConnectFakeSignaling();
4653 caller()->AddAudioVideoTracks();
4654 callee()->AddAudioVideoTracks();
4655 caller()->CreateAndSetAndSignalOffer();
4656 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4657 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4658 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4659
4660 MediaExpectations media_expectations;
4661 media_expectations.ExpectBidirectionalAudioAndVideo();
4662 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4663}
4664
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004665// Verify that a SSLCertificateVerifier passed in through
4666// PeerConnectionDependencies is actually used by the underlying SSL
4667// implementation to determine whether a certificate presented by the TURN
4668// server is accepted by the client. Note that openssladapter_unittest.cc
4669// contains more detailed, lower-level tests.
4670TEST_P(PeerConnectionIntegrationTest,
4671 SSLCertificateVerifierUsedForTurnConnections) {
4672 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4673 3478};
4674 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4675
4676 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4677 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004678 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4679 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004680
4681 webrtc::PeerConnectionInterface::IceServer ice_server;
4682 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4683 ice_server.username = "test";
4684 ice_server.password = "test";
4685
4686 PeerConnectionInterface::RTCConfiguration client_1_config;
4687 client_1_config.servers.push_back(ice_server);
4688 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4689
4690 PeerConnectionInterface::RTCConfiguration client_2_config;
4691 client_2_config.servers.push_back(ice_server);
4692 // Setting the type to kRelay forces the connection to go through a TURN
4693 // server.
4694 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4695
4696 // Get a copy to the pointer so we can verify calls later.
4697 rtc::TestCertificateVerifier* client_1_cert_verifier =
4698 new rtc::TestCertificateVerifier();
4699 client_1_cert_verifier->verify_certificate_ = true;
4700 rtc::TestCertificateVerifier* client_2_cert_verifier =
4701 new rtc::TestCertificateVerifier();
4702 client_2_cert_verifier->verify_certificate_ = true;
4703
4704 // Create the dependencies with the test certificate verifier.
4705 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4706 client_1_deps.tls_cert_verifier =
4707 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4708 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4709 client_2_deps.tls_cert_verifier =
4710 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4711
4712 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4713 client_1_config, std::move(client_1_deps), client_2_config,
4714 std::move(client_2_deps)));
4715 ConnectFakeSignaling();
4716
4717 // Set "offer to receive audio/video" without adding any tracks, so we just
4718 // set up ICE/DTLS with no media.
4719 PeerConnectionInterface::RTCOfferAnswerOptions options;
4720 options.offer_to_receive_audio = 1;
4721 options.offer_to_receive_video = 1;
4722 caller()->SetOfferAnswerOptions(options);
4723 caller()->CreateAndSetAndSignalOffer();
4724 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4725
4726 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4727 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004728}
4729
4730TEST_P(PeerConnectionIntegrationTest,
4731 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4732 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4733 3478};
4734 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4735
4736 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4737 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004738 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4739 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004740
4741 webrtc::PeerConnectionInterface::IceServer ice_server;
4742 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4743 ice_server.username = "test";
4744 ice_server.password = "test";
4745
4746 PeerConnectionInterface::RTCConfiguration client_1_config;
4747 client_1_config.servers.push_back(ice_server);
4748 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4749
4750 PeerConnectionInterface::RTCConfiguration client_2_config;
4751 client_2_config.servers.push_back(ice_server);
4752 // Setting the type to kRelay forces the connection to go through a TURN
4753 // server.
4754 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4755
4756 // Get a copy to the pointer so we can verify calls later.
4757 rtc::TestCertificateVerifier* client_1_cert_verifier =
4758 new rtc::TestCertificateVerifier();
4759 client_1_cert_verifier->verify_certificate_ = false;
4760 rtc::TestCertificateVerifier* client_2_cert_verifier =
4761 new rtc::TestCertificateVerifier();
4762 client_2_cert_verifier->verify_certificate_ = false;
4763
4764 // Create the dependencies with the test certificate verifier.
4765 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4766 client_1_deps.tls_cert_verifier =
4767 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4768 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4769 client_2_deps.tls_cert_verifier =
4770 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4771
4772 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4773 client_1_config, std::move(client_1_deps), client_2_config,
4774 std::move(client_2_deps)));
4775 ConnectFakeSignaling();
4776
4777 // Set "offer to receive audio/video" without adding any tracks, so we just
4778 // set up ICE/DTLS with no media.
4779 PeerConnectionInterface::RTCOfferAnswerOptions options;
4780 options.offer_to_receive_audio = 1;
4781 options.offer_to_receive_video = 1;
4782 caller()->SetOfferAnswerOptions(options);
4783 caller()->CreateAndSetAndSignalOffer();
4784 bool wait_res = true;
4785 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4786 // properly, should be able to just wait for a state of "failed" instead of
4787 // waiting a fixed 10 seconds.
4788 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4789 ASSERT_FALSE(wait_res);
4790
4791 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4792 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004793}
4794
Qingsi Wang25ec8882019-11-15 12:33:05 -08004795// Test that the injected ICE transport factory is used to create ICE transports
4796// for WebRTC connections.
4797TEST_P(PeerConnectionIntegrationTest, IceTransportFactoryUsedForConnections) {
4798 PeerConnectionInterface::RTCConfiguration default_config;
4799 PeerConnectionDependencies dependencies(nullptr);
4800 auto ice_transport_factory = std::make_unique<MockIceTransportFactory>();
4801 EXPECT_CALL(*ice_transport_factory, RecordIceTransportCreated()).Times(1);
4802 dependencies.ice_transport_factory = std::move(ice_transport_factory);
Niels Möller2a707032020-06-16 16:39:13 +02004803 auto wrapper = CreatePeerConnectionWrapper("Caller", nullptr, &default_config,
4804 std::move(dependencies), nullptr,
4805 /*reset_encoder_factory=*/false,
4806 /*reset_decoder_factory=*/false);
Qingsi Wang25ec8882019-11-15 12:33:05 -08004807 ASSERT_TRUE(wrapper);
4808 wrapper->CreateDataChannel();
4809 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
4810 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
4811 wrapper->pc()->SetLocalDescription(observer,
4812 wrapper->CreateOfferAndWait().release());
4813}
4814
deadbeefc964d0b2017-04-03 10:03:35 -07004815// Test that audio and video flow end-to-end when codec names don't use the
4816// expected casing, given that they're supposed to be case insensitive. To test
4817// this, all but one codec is removed from each media description, and its
4818// casing is changed.
4819//
4820// In the past, this has regressed and caused crashes/black video, due to the
4821// fact that code at some layers was doing case-insensitive comparisons and
4822// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004823TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004824 ASSERT_TRUE(CreatePeerConnectionWrappers());
4825 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004826 caller()->AddAudioVideoTracks();
4827 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004828
4829 // Remove all but one audio/video codec (opus and VP8), and change the
4830 // casing of the caller's generated offer.
4831 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4832 cricket::AudioContentDescription* audio =
4833 GetFirstAudioContentDescription(description);
4834 ASSERT_NE(nullptr, audio);
4835 auto audio_codecs = audio->codecs();
4836 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4837 [](const cricket::AudioCodec& codec) {
4838 return codec.name != "opus";
4839 }),
4840 audio_codecs.end());
4841 ASSERT_EQ(1u, audio_codecs.size());
4842 audio_codecs[0].name = "OpUs";
4843 audio->set_codecs(audio_codecs);
4844
4845 cricket::VideoContentDescription* video =
4846 GetFirstVideoContentDescription(description);
4847 ASSERT_NE(nullptr, video);
4848 auto video_codecs = video->codecs();
4849 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4850 [](const cricket::VideoCodec& codec) {
4851 return codec.name != "VP8";
4852 }),
4853 video_codecs.end());
4854 ASSERT_EQ(1u, video_codecs.size());
4855 video_codecs[0].name = "vP8";
4856 video->set_codecs(video_codecs);
4857 });
4858
4859 caller()->CreateAndSetAndSignalOffer();
4860 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4861
4862 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004863 MediaExpectations media_expectations;
4864 media_expectations.ExpectBidirectionalAudioAndVideo();
4865 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004866}
4867
Jonas Oreland49ac5952018-09-26 16:04:32 +02004868TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07004869 ASSERT_TRUE(CreatePeerConnectionWrappers());
4870 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004871 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004872 caller()->CreateAndSetAndSignalOffer();
4873 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004874 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004875 MediaExpectations media_expectations;
4876 media_expectations.CalleeExpectsSomeAudio(1);
4877 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02004878 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07004879 auto receiver = callee()->pc()->GetReceivers()[0];
4880 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02004881 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07004882 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4883 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02004884 sources[0].source_id());
4885 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
4886}
4887
4888TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
4889 ASSERT_TRUE(CreatePeerConnectionWrappers());
4890 ConnectFakeSignaling();
4891 caller()->AddVideoTrack();
4892 caller()->CreateAndSetAndSignalOffer();
4893 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4894 // Wait for one video frame to be received by the callee.
4895 MediaExpectations media_expectations;
4896 media_expectations.CalleeExpectsSomeVideo(1);
4897 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4898 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
4899 auto receiver = callee()->pc()->GetReceivers()[0];
4900 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
4901 auto sources = receiver->GetSources();
4902 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
Yves Gereyf781bb52019-07-23 19:15:39 +02004903 ASSERT_GT(sources.size(), 0u);
Jonas Oreland49ac5952018-09-26 16:04:32 +02004904 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4905 sources[0].source_id());
4906 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07004907}
4908
deadbeef2f425aa2017-04-14 10:41:32 -07004909// Test that if a track is removed and added again with a different stream ID,
4910// the new stream ID is successfully communicated in SDP and media continues to
4911// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004912// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4913// it will not reuse a transceiver that has already been sending. After creating
4914// a new transceiver it tries to create an offer with two senders of the same
4915// track ids and it fails.
4916TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004917 ASSERT_TRUE(CreatePeerConnectionWrappers());
4918 ConnectFakeSignaling();
4919
deadbeef2f425aa2017-04-14 10:41:32 -07004920 // Add track using stream 1, do offer/answer.
4921 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4922 caller()->CreateLocalAudioTrack();
4923 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07004924 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07004925 caller()->CreateAndSetAndSignalOffer();
4926 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004927 {
4928 MediaExpectations media_expectations;
4929 media_expectations.CalleeExpectsSomeAudio(1);
4930 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4931 }
deadbeef2f425aa2017-04-14 10:41:32 -07004932 // Remove the sender, and create a new one with the new stream.
4933 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07004934 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07004935 caller()->CreateAndSetAndSignalOffer();
4936 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4937 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004938 {
4939 MediaExpectations media_expectations;
4940 media_expectations.CalleeExpectsSomeAudio();
4941 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4942 }
deadbeef2f425aa2017-04-14 10:41:32 -07004943}
4944
Seth Hampson2f0d7022018-02-20 11:54:42 -08004945TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004946 ASSERT_TRUE(CreatePeerConnectionWrappers());
4947 ConnectFakeSignaling();
4948
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004949 auto output = std::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004950 ON_CALL(*output, IsActive()).WillByDefault(::testing::Return(true));
4951 ON_CALL(*output, Write(::testing::_)).WillByDefault(::testing::Return(true));
Elad Alon99c3fe52017-10-13 16:29:40 +02004952 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004953 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4954 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004955
Steve Anton15324772018-01-16 10:26:49 -08004956 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004957 caller()->CreateAndSetAndSignalOffer();
4958 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4959}
4960
Steve Antonede9ca52017-10-16 13:04:27 -07004961// Test that if candidates are only signaled by applying full session
4962// descriptions (instead of using AddIceCandidate), the peers can connect to
4963// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004964TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004965 ASSERT_TRUE(CreatePeerConnectionWrappers());
4966 // Each side will signal the session descriptions but not candidates.
4967 ConnectFakeSignalingForSdpOnly();
4968
4969 // Add audio video track and exchange the initial offer/answer with media
4970 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004971 caller()->AddAudioVideoTracks();
4972 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004973 caller()->CreateAndSetAndSignalOffer();
4974
4975 // Wait for all candidates to be gathered on both the caller and callee.
4976 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4977 caller()->ice_gathering_state(), kDefaultTimeout);
4978 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4979 callee()->ice_gathering_state(), kDefaultTimeout);
4980
4981 // The candidates will now be included in the session description, so
4982 // signaling them will start the ICE connection.
4983 caller()->CreateAndSetAndSignalOffer();
4984 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4985
4986 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004987 MediaExpectations media_expectations;
4988 media_expectations.ExpectBidirectionalAudioAndVideo();
4989 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07004990}
4991
henrika5f6bf242017-11-01 11:06:56 +01004992// Test that SetAudioPlayout can be used to disable audio playout from the
4993// start, then later enable it. This may be useful, for example, if the caller
4994// needs to play a local ringtone until some event occurs, after which it
4995// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004996TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01004997 ASSERT_TRUE(CreatePeerConnectionWrappers());
4998 ConnectFakeSignaling();
4999
5000 // Set up audio-only call where audio playout is disabled on caller's side.
5001 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08005002 caller()->AddAudioTrack();
5003 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01005004 caller()->CreateAndSetAndSignalOffer();
5005 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5006
5007 // Pump messages for a second.
5008 WAIT(false, 1000);
5009 // Since audio playout is disabled, the caller shouldn't have received
5010 // anything (at the playout level, at least).
5011 EXPECT_EQ(0, caller()->audio_frames_received());
5012 // As a sanity check, make sure the callee (for which playout isn't disabled)
5013 // did still see frames on its audio level.
5014 ASSERT_GT(callee()->audio_frames_received(), 0);
5015
5016 // Enable playout again, and ensure audio starts flowing.
5017 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005018 MediaExpectations media_expectations;
5019 media_expectations.ExpectBidirectionalAudio();
5020 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01005021}
5022
5023double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
5024 auto report = pc->NewGetStats();
5025 auto track_stats_list =
5026 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
5027 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
5028 for (const auto* track_stats : track_stats_list) {
5029 if (track_stats->remote_source.is_defined() &&
5030 *track_stats->remote_source) {
5031 remote_track_stats = track_stats;
5032 break;
5033 }
5034 }
5035
5036 if (!remote_track_stats->total_audio_energy.is_defined()) {
5037 return 0.0;
5038 }
5039 return *remote_track_stats->total_audio_energy;
5040}
5041
5042// Test that if audio playout is disabled via the SetAudioPlayout() method, then
5043// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005044TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01005045 DisableAudioPlayoutStillGeneratesAudioStats) {
5046 ASSERT_TRUE(CreatePeerConnectionWrappers());
5047 ConnectFakeSignaling();
5048
5049 // Set up audio-only call where playout is disabled but audio-processing is
5050 // still active.
Steve Anton15324772018-01-16 10:26:49 -08005051 caller()->AddAudioTrack();
5052 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01005053 caller()->pc()->SetAudioPlayout(false);
5054
5055 caller()->CreateAndSetAndSignalOffer();
5056 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5057
5058 // Wait for the callee to receive audio stats.
5059 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
5060}
5061
henrika4f167df2017-11-01 14:45:55 +01005062// Test that SetAudioRecording can be used to disable audio recording from the
5063// start, then later enable it. This may be useful, for example, if the caller
5064// wants to ensure that no audio resources are active before a certain state
5065// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005066TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01005067 ASSERT_TRUE(CreatePeerConnectionWrappers());
5068 ConnectFakeSignaling();
5069
5070 // Set up audio-only call where audio recording is disabled on caller's side.
5071 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08005072 caller()->AddAudioTrack();
5073 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01005074 caller()->CreateAndSetAndSignalOffer();
5075 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5076
5077 // Pump messages for a second.
5078 WAIT(false, 1000);
5079 // Since caller has disabled audio recording, the callee shouldn't have
5080 // received anything.
5081 EXPECT_EQ(0, callee()->audio_frames_received());
5082 // As a sanity check, make sure the caller did still see frames on its
5083 // audio level since audio recording is enabled on the calle side.
5084 ASSERT_GT(caller()->audio_frames_received(), 0);
5085
5086 // Enable audio recording again, and ensure audio starts flowing.
5087 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005088 MediaExpectations media_expectations;
5089 media_expectations.ExpectBidirectionalAudio();
5090 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01005091}
5092
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005093// Test that after closing PeerConnections, they stop sending any packets (ICE,
5094// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08005095TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005096 // Set up audio/video/data, wait for some frames to be received.
5097 ASSERT_TRUE(CreatePeerConnectionWrappers());
5098 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08005099 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005100#ifdef HAVE_SCTP
5101 caller()->CreateDataChannel();
5102#endif
5103 caller()->CreateAndSetAndSignalOffer();
5104 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005105 MediaExpectations media_expectations;
5106 media_expectations.CalleeExpectsSomeAudioAndVideo();
5107 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005108 // Close PeerConnections.
Steve Antond91969e2019-05-30 12:27:03 -07005109 ClosePeerConnections();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005110 // Pump messages for a second, and ensure no new packets end up sent.
5111 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
5112 WAIT(false, 1000);
5113 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
5114 EXPECT_EQ(sent_packets_a, sent_packets_b);
5115}
5116
Steve Anton7eca0932018-03-30 15:18:41 -07005117// Test that transport stats are generated by the RTCStatsCollector for a
5118// connection that only involves data channels. This is a regression test for
5119// crbug.com/826972.
5120#ifdef HAVE_SCTP
5121TEST_P(PeerConnectionIntegrationTest,
5122 TransportStatsReportedForDataChannelOnlyConnection) {
5123 ASSERT_TRUE(CreatePeerConnectionWrappers());
5124 ConnectFakeSignaling();
5125 caller()->CreateDataChannel();
5126
5127 caller()->CreateAndSetAndSignalOffer();
5128 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5129 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5130
5131 auto caller_report = caller()->NewGetStats();
5132 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
5133 auto callee_report = callee()->NewGetStats();
5134 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
5135}
5136#endif // HAVE_SCTP
5137
Qingsi Wang7685e862018-06-11 20:15:46 -07005138TEST_P(PeerConnectionIntegrationTest,
5139 IceEventsGeneratedAndLoggedInRtcEventLog) {
5140 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
5141 ConnectFakeSignaling();
5142 PeerConnectionInterface::RTCOfferAnswerOptions options;
5143 options.offer_to_receive_audio = 1;
5144 caller()->SetOfferAnswerOptions(options);
5145 caller()->CreateAndSetAndSignalOffer();
5146 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5147 ASSERT_NE(nullptr, caller()->event_log_factory());
5148 ASSERT_NE(nullptr, callee()->event_log_factory());
5149 webrtc::FakeRtcEventLog* caller_event_log =
5150 static_cast<webrtc::FakeRtcEventLog*>(
5151 caller()->event_log_factory()->last_log_created());
5152 webrtc::FakeRtcEventLog* callee_event_log =
5153 static_cast<webrtc::FakeRtcEventLog*>(
5154 callee()->event_log_factory()->last_log_created());
5155 ASSERT_NE(nullptr, caller_event_log);
5156 ASSERT_NE(nullptr, callee_event_log);
5157 int caller_ice_config_count = caller_event_log->GetEventCount(
5158 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5159 int caller_ice_event_count = caller_event_log->GetEventCount(
5160 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5161 int callee_ice_config_count = callee_event_log->GetEventCount(
5162 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5163 int callee_ice_event_count = callee_event_log->GetEventCount(
5164 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5165 EXPECT_LT(0, caller_ice_config_count);
5166 EXPECT_LT(0, caller_ice_event_count);
5167 EXPECT_LT(0, callee_ice_config_count);
5168 EXPECT_LT(0, callee_ice_event_count);
5169}
5170
Qingsi Wangc129c352019-04-18 10:41:58 -07005171TEST_P(PeerConnectionIntegrationTest, RegatherAfterChangingIceTransportType) {
Qingsi Wangc129c352019-04-18 10:41:58 -07005172 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5173 3478};
5174 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5175
5176 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
5177
5178 webrtc::PeerConnectionInterface::IceServer ice_server;
5179 ice_server.urls.push_back("turn:88.88.88.0:3478");
5180 ice_server.username = "test";
5181 ice_server.password = "test";
5182
5183 PeerConnectionInterface::RTCConfiguration caller_config;
5184 caller_config.servers.push_back(ice_server);
5185 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5186 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07005187 caller_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07005188
5189 PeerConnectionInterface::RTCConfiguration callee_config;
5190 callee_config.servers.push_back(ice_server);
5191 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5192 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07005193 callee_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07005194
5195 ASSERT_TRUE(
5196 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5197
5198 // Do normal offer/answer and wait for ICE to complete.
5199 ConnectFakeSignaling();
5200 caller()->AddAudioVideoTracks();
5201 callee()->AddAudioVideoTracks();
5202 caller()->CreateAndSetAndSignalOffer();
5203 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5204 // Since we are doing continual gathering, the ICE transport does not reach
5205 // kIceGatheringComplete (see
5206 // P2PTransportChannel::OnCandidatesAllocationDone), and consequently not
5207 // kIceConnectionComplete.
5208 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5209 caller()->ice_connection_state(), kDefaultTimeout);
5210 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5211 callee()->ice_connection_state(), kDefaultTimeout);
5212 // Note that we cannot use the metric
5213 // |WebRTC.PeerConnection.CandidatePairType_UDP| in this test since this
5214 // metric is only populated when we reach kIceConnectionComplete in the
5215 // current implementation.
5216 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5217 caller()->last_candidate_gathered().type());
5218 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5219 callee()->last_candidate_gathered().type());
5220
5221 // Loosen the caller's candidate filter.
5222 caller_config = caller()->pc()->GetConfiguration();
5223 caller_config.type = webrtc::PeerConnectionInterface::kAll;
5224 caller()->pc()->SetConfiguration(caller_config);
5225 // We should have gathered a new host candidate.
5226 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5227 caller()->last_candidate_gathered().type(), kDefaultTimeout);
5228
5229 // Loosen the callee's candidate filter.
5230 callee_config = callee()->pc()->GetConfiguration();
5231 callee_config.type = webrtc::PeerConnectionInterface::kAll;
5232 callee()->pc()->SetConfiguration(callee_config);
5233 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5234 callee()->last_candidate_gathered().type(), kDefaultTimeout);
Jonas Orelande3096512020-05-27 09:01:05 +02005235
5236 // Create an offer and verify that it does not contain an ICE restart (i.e new
5237 // ice credentials).
5238 std::string caller_ufrag_pre_offer = caller()
5239 ->pc()
5240 ->local_description()
5241 ->description()
5242 ->transport_infos()[0]
5243 .description.ice_ufrag;
5244 caller()->CreateAndSetAndSignalOffer();
5245 std::string caller_ufrag_post_offer = caller()
5246 ->pc()
5247 ->local_description()
5248 ->description()
5249 ->transport_infos()[0]
5250 .description.ice_ufrag;
5251 EXPECT_EQ(caller_ufrag_pre_offer, caller_ufrag_post_offer);
Qingsi Wangc129c352019-04-18 10:41:58 -07005252}
5253
Eldar Relloda13ea22019-06-01 12:23:43 +03005254TEST_P(PeerConnectionIntegrationTest, OnIceCandidateError) {
Eldar Relloda13ea22019-06-01 12:23:43 +03005255 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5256 3478};
5257 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5258
5259 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
5260
5261 webrtc::PeerConnectionInterface::IceServer ice_server;
5262 ice_server.urls.push_back("turn:88.88.88.0:3478");
5263 ice_server.username = "test";
5264 ice_server.password = "123";
5265
5266 PeerConnectionInterface::RTCConfiguration caller_config;
5267 caller_config.servers.push_back(ice_server);
5268 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5269 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5270
5271 PeerConnectionInterface::RTCConfiguration callee_config;
5272 callee_config.servers.push_back(ice_server);
5273 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5274 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5275
5276 ASSERT_TRUE(
5277 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5278
5279 // Do normal offer/answer and wait for ICE to complete.
5280 ConnectFakeSignaling();
5281 caller()->AddAudioVideoTracks();
5282 callee()->AddAudioVideoTracks();
5283 caller()->CreateAndSetAndSignalOffer();
5284 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5285 EXPECT_EQ_WAIT(401, caller()->error_event().error_code, kDefaultTimeout);
5286 EXPECT_EQ("Unauthorized", caller()->error_event().error_text);
5287 EXPECT_EQ("turn:88.88.88.0:3478?transport=udp", caller()->error_event().url);
Eldar Rello0095d372019-12-02 22:22:07 +02005288 EXPECT_NE(caller()->error_event().address, "");
Eldar Relloda13ea22019-06-01 12:23:43 +03005289}
5290
Eldar Rellofa8019c2020-05-14 11:59:33 +03005291TEST_P(PeerConnectionIntegrationTest, OnIceCandidateErrorWithEmptyAddress) {
5292 webrtc::PeerConnectionInterface::IceServer ice_server;
5293 ice_server.urls.push_back("turn:127.0.0.1:3478?transport=tcp");
5294 ice_server.username = "test";
5295 ice_server.password = "test";
5296
5297 PeerConnectionInterface::RTCConfiguration caller_config;
5298 caller_config.servers.push_back(ice_server);
5299 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5300 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5301
5302 PeerConnectionInterface::RTCConfiguration callee_config;
5303 callee_config.servers.push_back(ice_server);
5304 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5305 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5306
5307 ASSERT_TRUE(
5308 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5309
5310 // Do normal offer/answer and wait for ICE to complete.
5311 ConnectFakeSignaling();
5312 caller()->AddAudioVideoTracks();
5313 callee()->AddAudioVideoTracks();
5314 caller()->CreateAndSetAndSignalOffer();
5315 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5316 EXPECT_EQ_WAIT(701, caller()->error_event().error_code, kDefaultTimeout);
5317 EXPECT_EQ(caller()->error_event().address, "");
5318}
5319
Eldar Rello5ab79e62019-10-09 18:29:44 +03005320TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5321 AudioKeepsFlowingAfterImplicitRollback) {
5322 PeerConnectionInterface::RTCConfiguration config;
5323 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
5324 config.enable_implicit_rollback = true;
5325 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5326 ConnectFakeSignaling();
5327 caller()->AddAudioTrack();
5328 callee()->AddAudioTrack();
5329 caller()->CreateAndSetAndSignalOffer();
5330 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5331 MediaExpectations media_expectations;
5332 media_expectations.ExpectBidirectionalAudio();
5333 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5334 SetSignalIceCandidates(false); // Workaround candidate outrace sdp.
5335 caller()->AddVideoTrack();
5336 callee()->AddVideoTrack();
5337 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
5338 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
5339 callee()->pc()->SetLocalDescription(observer,
5340 callee()->CreateOfferAndWait().release());
5341 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
5342 caller()->CreateAndSetAndSignalOffer(); // Implicit rollback.
5343 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5344 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5345}
5346
5347TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5348 ImplicitRollbackVisitsStableState) {
5349 RTCConfiguration config;
5350 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
5351 config.enable_implicit_rollback = true;
5352
5353 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5354
5355 rtc::scoped_refptr<MockSetSessionDescriptionObserver> sld_observer(
5356 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
5357 callee()->pc()->SetLocalDescription(sld_observer,
5358 callee()->CreateOfferAndWait().release());
5359 EXPECT_TRUE_WAIT(sld_observer->called(), kDefaultTimeout);
5360 EXPECT_EQ(sld_observer->error(), "");
5361
5362 rtc::scoped_refptr<MockSetSessionDescriptionObserver> srd_observer(
5363 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
5364 callee()->pc()->SetRemoteDescription(
5365 srd_observer, caller()->CreateOfferAndWait().release());
5366 EXPECT_TRUE_WAIT(srd_observer->called(), kDefaultTimeout);
5367 EXPECT_EQ(srd_observer->error(), "");
5368
5369 EXPECT_THAT(callee()->peer_connection_signaling_state_history(),
5370 ElementsAre(PeerConnectionInterface::kHaveLocalOffer,
5371 PeerConnectionInterface::kStable,
5372 PeerConnectionInterface::kHaveRemoteOffer));
5373}
5374
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005375INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
5376 PeerConnectionIntegrationTest,
5377 Values(SdpSemantics::kPlanB,
5378 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08005379
Yves Gerey100fe632020-01-17 19:15:53 +01005380INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
5381 PeerConnectionIntegrationTestWithFakeClock,
5382 Values(SdpSemantics::kPlanB,
5383 SdpSemantics::kUnifiedPlan));
5384
Steve Anton74255ff2018-01-24 18:32:57 -08005385// Tests that verify interoperability between Plan B and Unified Plan
5386// PeerConnections.
5387class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08005388 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08005389 public ::testing::WithParamInterface<
5390 std::tuple<SdpSemantics, SdpSemantics>> {
5391 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08005392 // Setting the SdpSemantics for the base test to kDefault does not matter
5393 // because we specify not to use the test semantics when creating
5394 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08005395 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07005396 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08005397 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08005398 callee_semantics_(std::get<1>(GetParam())) {}
5399
5400 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07005401 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
5402 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08005403 }
5404
5405 const SdpSemantics caller_semantics_;
5406 const SdpSemantics callee_semantics_;
5407};
5408
5409TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
5410 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5411 ConnectFakeSignaling();
5412
5413 caller()->CreateAndSetAndSignalOffer();
5414 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5415}
5416
5417TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
5418 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5419 ConnectFakeSignaling();
5420 auto audio_sender = caller()->AddAudioTrack();
5421
5422 caller()->CreateAndSetAndSignalOffer();
5423 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5424
5425 // Verify that one audio receiver has been created on the remote and that it
5426 // has the same track ID as the sending track.
5427 auto receivers = callee()->pc()->GetReceivers();
5428 ASSERT_EQ(1u, receivers.size());
5429 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
5430 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
5431
Seth Hampson2f0d7022018-02-20 11:54:42 -08005432 MediaExpectations media_expectations;
5433 media_expectations.CalleeExpectsSomeAudio();
5434 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005435}
5436
5437TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
5438 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5439 ConnectFakeSignaling();
5440 auto video_sender = caller()->AddVideoTrack();
5441 auto audio_sender = caller()->AddAudioTrack();
5442
5443 caller()->CreateAndSetAndSignalOffer();
5444 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5445
5446 // Verify that one audio and one video receiver have been created on the
5447 // remote and that they have the same track IDs as the sending tracks.
5448 auto audio_receivers =
5449 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
5450 ASSERT_EQ(1u, audio_receivers.size());
5451 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
5452 auto video_receivers =
5453 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
5454 ASSERT_EQ(1u, video_receivers.size());
5455 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
5456
Seth Hampson2f0d7022018-02-20 11:54:42 -08005457 MediaExpectations media_expectations;
5458 media_expectations.CalleeExpectsSomeAudioAndVideo();
5459 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005460}
5461
5462TEST_P(PeerConnectionIntegrationInteropTest,
5463 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
5464 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5465 ConnectFakeSignaling();
5466 caller()->AddAudioVideoTracks();
5467 callee()->AddAudioVideoTracks();
5468
5469 caller()->CreateAndSetAndSignalOffer();
5470 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5471
Seth Hampson2f0d7022018-02-20 11:54:42 -08005472 MediaExpectations media_expectations;
5473 media_expectations.ExpectBidirectionalAudioAndVideo();
5474 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005475}
5476
5477TEST_P(PeerConnectionIntegrationInteropTest,
5478 ReverseRolesOneAudioLocalToOneVideoRemote) {
5479 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5480 ConnectFakeSignaling();
5481 caller()->AddAudioTrack();
5482 callee()->AddVideoTrack();
5483
5484 caller()->CreateAndSetAndSignalOffer();
5485 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5486
5487 // Verify that only the audio track has been negotiated.
5488 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
5489 // Might also check that the callee's NegotiationNeeded flag is set.
5490
5491 // Reverse roles.
5492 callee()->CreateAndSetAndSignalOffer();
5493 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5494
Seth Hampson2f0d7022018-02-20 11:54:42 -08005495 MediaExpectations media_expectations;
5496 media_expectations.CallerExpectsSomeVideo();
5497 media_expectations.CalleeExpectsSomeAudio();
5498 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005499}
5500
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005501INSTANTIATE_TEST_SUITE_P(
Steve Antonba42e992018-04-09 14:10:01 -07005502 PeerConnectionIntegrationTest,
5503 PeerConnectionIntegrationInteropTest,
5504 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
5505 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
5506
5507// Test that if the Unified Plan side offers two video tracks then the Plan B
5508// side will only see the first one and ignore the second.
5509TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07005510 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
5511 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08005512 ConnectFakeSignaling();
5513 auto first_sender = caller()->AddVideoTrack();
5514 caller()->AddVideoTrack();
5515
5516 caller()->CreateAndSetAndSignalOffer();
5517 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5518
5519 // Verify that there is only one receiver and it corresponds to the first
5520 // added track.
5521 auto receivers = callee()->pc()->GetReceivers();
5522 ASSERT_EQ(1u, receivers.size());
5523 EXPECT_TRUE(receivers[0]->track()->enabled());
5524 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
5525
Seth Hampson2f0d7022018-02-20 11:54:42 -08005526 MediaExpectations media_expectations;
5527 media_expectations.CalleeExpectsSomeVideo();
5528 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005529}
5530
Steve Anton2bed3972019-01-04 17:04:30 -08005531// Test that if the initial offer tagged BUNDLE section is rejected due to its
5532// associated RtpTransceiver being stopped and another transceiver is added,
5533// then renegotiation causes the callee to receive the new video track without
5534// error.
5535// This is a regression test for bugs.webrtc.org/9954
5536TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5537 ReOfferWithStoppedBundleTaggedTransceiver) {
5538 RTCConfiguration config;
5539 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
5540 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5541 ConnectFakeSignaling();
5542 auto audio_transceiver_or_error =
5543 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
5544 ASSERT_TRUE(audio_transceiver_or_error.ok());
5545 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
5546
5547 caller()->CreateAndSetAndSignalOffer();
5548 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5549 {
5550 MediaExpectations media_expectations;
5551 media_expectations.CalleeExpectsSomeAudio();
5552 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5553 }
5554
5555 audio_transceiver->Stop();
5556 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
5557
5558 caller()->CreateAndSetAndSignalOffer();
5559 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5560 {
5561 MediaExpectations media_expectations;
5562 media_expectations.CalleeExpectsSomeVideo();
5563 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5564 }
5565}
5566
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02005567#ifdef HAVE_SCTP
5568
5569TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5570 EndToEndCallWithBundledSctpDataChannel) {
5571 ASSERT_TRUE(CreatePeerConnectionWrappers());
5572 ConnectFakeSignaling();
5573 caller()->CreateDataChannel();
5574 caller()->AddAudioVideoTracks();
5575 callee()->AddAudioVideoTracks();
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02005576 caller()->CreateAndSetAndSignalOffer();
5577 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Harald Alvestrand17ea0682019-12-13 11:51:04 +01005578 ASSERT_EQ_WAIT(SctpTransportState::kConnected,
5579 caller()->pc()->GetSctpTransport()->Information().state(),
5580 kDefaultTimeout);
5581 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5582 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
5583}
5584
5585TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5586 EndToEndCallWithDataChannelOnlyConnects) {
5587 ASSERT_TRUE(CreatePeerConnectionWrappers());
5588 ConnectFakeSignaling();
5589 caller()->CreateDataChannel();
5590 caller()->CreateAndSetAndSignalOffer();
5591 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5592 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5593 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
5594 ASSERT_TRUE(caller()->data_observer()->IsOpen());
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02005595}
5596
Harald Alvestrand2697ac12019-12-16 10:37:04 +01005597TEST_F(PeerConnectionIntegrationTestUnifiedPlan, DataChannelClosesWhenClosed) {
5598 ASSERT_TRUE(CreatePeerConnectionWrappers());
5599 ConnectFakeSignaling();
5600 caller()->CreateDataChannel();
5601 caller()->CreateAndSetAndSignalOffer();
5602 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5603 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
5604 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
5605 caller()->data_channel()->Close();
5606 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
5607}
5608
5609TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5610 DataChannelClosesWhenClosedReverse) {
5611 ASSERT_TRUE(CreatePeerConnectionWrappers());
5612 ConnectFakeSignaling();
5613 caller()->CreateDataChannel();
5614 caller()->CreateAndSetAndSignalOffer();
5615 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5616 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
5617 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
5618 callee()->data_channel()->Close();
5619 ASSERT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
5620}
5621
5622TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5623 DataChannelClosesWhenPeerConnectionClosed) {
5624 ASSERT_TRUE(CreatePeerConnectionWrappers());
5625 ConnectFakeSignaling();
5626 caller()->CreateDataChannel();
5627 caller()->CreateAndSetAndSignalOffer();
5628 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5629 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
5630 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
5631 caller()->pc()->Close();
5632 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
5633}
5634
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02005635#endif // HAVE_SCTP
5636
deadbeef1dcb1642017-03-29 21:08:16 -07005637} // namespace
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01005638} // namespace webrtc
deadbeef1dcb1642017-03-29 21:08:16 -07005639
5640#endif // if !defined(THREAD_SANITIZER)