blob: dd24163f3e3d706f7f8813da69f7cc46322ba2ac [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());
Harald Alvestrand6060df52020-08-11 09:54:02 +0200802 RemoveUnusedVideoRenderers();
deadbeef1dcb1642017-03-29 21:08:16 -0700803 // As mentioned above, we need to send the message immediately after
804 // SetLocalDescription.
805 SendSdpMessage(type, sdp);
806 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
807 return true;
808 }
809
810 bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
811 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
812 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100813 RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
deadbeef1dcb1642017-03-29 21:08:16 -0700814 pc()->SetRemoteDescription(observer, desc.release());
Harald Alvestrand6060df52020-08-11 09:54:02 +0200815 RemoveUnusedVideoRenderers();
deadbeef1dcb1642017-03-29 21:08:16 -0700816 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
817 return observer->result();
818 }
819
Seth Hampson2f0d7022018-02-20 11:54:42 -0800820 // This is a work around to remove unused fake_video_renderers from
821 // transceivers that have either stopped or are no longer receiving.
822 void RemoveUnusedVideoRenderers() {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200823 if (sdp_semantics_ != SdpSemantics::kUnifiedPlan) {
824 return;
825 }
Seth Hampson2f0d7022018-02-20 11:54:42 -0800826 auto transceivers = pc()->GetTransceivers();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200827 std::set<std::string> active_renderers;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800828 for (auto& transceiver : transceivers) {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200829 // Note - we don't check for direction here. This function is called
830 // before direction is set, and in that case, we should not remove
831 // the renderer.
832 if (transceiver->receiver()->media_type() == cricket::MEDIA_TYPE_VIDEO) {
833 active_renderers.insert(transceiver->receiver()->track()->id());
Seth Hampson2f0d7022018-02-20 11:54:42 -0800834 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200835 }
836 for (auto it = fake_video_renderers_.begin();
837 it != fake_video_renderers_.end();) {
838 // Remove fake video renderers belonging to any non-active transceivers.
839 if (!active_renderers.count(it->first)) {
840 it = fake_video_renderers_.erase(it);
841 } else {
842 it++;
Seth Hampson2f0d7022018-02-20 11:54:42 -0800843 }
844 }
845 }
846
deadbeef1dcb1642017-03-29 21:08:16 -0700847 // Simulate sending a blob of SDP with delay |signaling_delay_ms_| (0 by
848 // default).
Steve Antona3a92c22017-12-07 10:27:41 -0800849 void SendSdpMessage(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700850 if (signaling_delay_ms_ == 0) {
851 RelaySdpMessageIfReceiverExists(type, msg);
852 } else {
853 invoker_.AsyncInvokeDelayed<void>(
854 RTC_FROM_HERE, rtc::Thread::Current(),
855 rtc::Bind(&PeerConnectionWrapper::RelaySdpMessageIfReceiverExists,
856 this, type, msg),
857 signaling_delay_ms_);
858 }
859 }
860
Steve Antona3a92c22017-12-07 10:27:41 -0800861 void RelaySdpMessageIfReceiverExists(SdpType type, const std::string& msg) {
deadbeef1dcb1642017-03-29 21:08:16 -0700862 if (signaling_message_receiver_) {
863 signaling_message_receiver_->ReceiveSdpMessage(type, msg);
864 }
865 }
866
867 // Simulate trickling an ICE candidate with delay |signaling_delay_ms_| (0 by
868 // default).
869 void SendIceMessage(const std::string& sdp_mid,
870 int sdp_mline_index,
871 const std::string& msg) {
872 if (signaling_delay_ms_ == 0) {
873 RelayIceMessageIfReceiverExists(sdp_mid, sdp_mline_index, msg);
874 } else {
875 invoker_.AsyncInvokeDelayed<void>(
876 RTC_FROM_HERE, rtc::Thread::Current(),
877 rtc::Bind(&PeerConnectionWrapper::RelayIceMessageIfReceiverExists,
878 this, sdp_mid, sdp_mline_index, msg),
879 signaling_delay_ms_);
880 }
881 }
882
883 void RelayIceMessageIfReceiverExists(const std::string& sdp_mid,
884 int sdp_mline_index,
885 const std::string& msg) {
886 if (signaling_message_receiver_) {
887 signaling_message_receiver_->ReceiveIceMessage(sdp_mid, sdp_mline_index,
888 msg);
889 }
890 }
891
892 // SignalingMessageReceiver callbacks.
Steve Antona3a92c22017-12-07 10:27:41 -0800893 void ReceiveSdpMessage(SdpType type, const std::string& msg) override {
894 if (type == SdpType::kOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -0700895 HandleIncomingOffer(msg);
896 } else {
897 HandleIncomingAnswer(msg);
898 }
899 }
900
901 void ReceiveIceMessage(const std::string& sdp_mid,
902 int sdp_mline_index,
903 const std::string& msg) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100904 RTC_LOG(LS_INFO) << debug_name_ << ": ReceiveIceMessage";
deadbeef1dcb1642017-03-29 21:08:16 -0700905 std::unique_ptr<webrtc::IceCandidateInterface> candidate(
906 webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, msg, nullptr));
907 EXPECT_TRUE(pc()->AddIceCandidate(candidate.get()));
908 }
909
910 // PeerConnectionObserver callbacks.
911 void OnSignalingChange(
912 webrtc::PeerConnectionInterface::SignalingState new_state) override {
913 EXPECT_EQ(pc()->signaling_state(), new_state);
Eldar Rello5ab79e62019-10-09 18:29:44 +0300914 peer_connection_signaling_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700915 }
Steve Anton15324772018-01-16 10:26:49 -0800916 void OnAddTrack(rtc::scoped_refptr<RtpReceiverInterface> receiver,
917 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
918 streams) override {
919 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
920 rtc::scoped_refptr<VideoTrackInterface> video_track(
921 static_cast<VideoTrackInterface*>(receiver->track().get()));
922 ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
deadbeef1dcb1642017-03-29 21:08:16 -0700923 fake_video_renderers_.end());
Steve Anton15324772018-01-16 10:26:49 -0800924 fake_video_renderers_[video_track->id()] =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200925 std::make_unique<FakeVideoTrackRenderer>(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -0700926 }
927 }
Steve Anton15324772018-01-16 10:26:49 -0800928 void OnRemoveTrack(
929 rtc::scoped_refptr<RtpReceiverInterface> receiver) override {
930 if (receiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
931 auto it = fake_video_renderers_.find(receiver->track()->id());
Harald Alvestrand6060df52020-08-11 09:54:02 +0200932 if (it != fake_video_renderers_.end()) {
933 fake_video_renderers_.erase(it);
934 } else {
935 RTC_LOG(LS_ERROR) << "OnRemoveTrack called for non-active renderer";
936 }
Steve Anton15324772018-01-16 10:26:49 -0800937 }
938 }
deadbeef1dcb1642017-03-29 21:08:16 -0700939 void OnRenegotiationNeeded() override {}
940 void OnIceConnectionChange(
941 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
942 EXPECT_EQ(pc()->ice_connection_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700943 ice_connection_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700944 }
Jonas Olssonacd8ae72019-02-25 15:26:24 +0100945 void OnStandardizedIceConnectionChange(
946 webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
947 standardized_ice_connection_state_history_.push_back(new_state);
948 }
Jonas Olsson635474e2018-10-18 15:58:17 +0200949 void OnConnectionChange(
950 webrtc::PeerConnectionInterface::PeerConnectionState new_state) override {
951 peer_connection_state_history_.push_back(new_state);
952 }
953
deadbeef1dcb1642017-03-29 21:08:16 -0700954 void OnIceGatheringChange(
955 webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
deadbeef1dcb1642017-03-29 21:08:16 -0700956 EXPECT_EQ(pc()->ice_gathering_state(), new_state);
Steve Antonede9ca52017-10-16 13:04:27 -0700957 ice_gathering_state_history_.push_back(new_state);
deadbeef1dcb1642017-03-29 21:08:16 -0700958 }
Alex Drake00c7ecf2019-08-06 10:54:47 -0700959
960 void OnIceSelectedCandidatePairChanged(
961 const cricket::CandidatePairChangeEvent& event) {
962 ice_candidate_pair_change_history_.push_back(event);
963 }
Alex Drake43faee02019-08-12 16:27:34 -0700964
deadbeef1dcb1642017-03-29 21:08:16 -0700965 void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100966 RTC_LOG(LS_INFO) << debug_name_ << ": OnIceCandidate";
deadbeef1dcb1642017-03-29 21:08:16 -0700967
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800968 if (remote_async_resolver_) {
969 const auto& local_candidate = candidate->candidate();
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800970 if (local_candidate.address().IsUnresolvedIP()) {
971 RTC_DCHECK(local_candidate.type() == cricket::LOCAL_PORT_TYPE);
972 rtc::SocketAddress resolved_addr(local_candidate.address());
Qingsi Wangecd30542019-05-22 14:34:56 -0700973 const auto resolved_ip = mdns_responder_->GetMappedAddressForName(
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800974 local_candidate.address().hostname());
975 RTC_DCHECK(!resolved_ip.IsNil());
976 resolved_addr.SetResolvedIP(resolved_ip);
977 EXPECT_CALL(*remote_async_resolver_, GetResolvedAddress(_, _))
978 .WillOnce(DoAll(SetArgPointee<1>(resolved_addr), Return(true)));
979 EXPECT_CALL(*remote_async_resolver_, Destroy(_));
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700980 }
Zach Stein6fcdc2f2018-08-23 16:25:55 -0700981 }
982
deadbeef1dcb1642017-03-29 21:08:16 -0700983 std::string ice_sdp;
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800984 EXPECT_TRUE(candidate->ToString(&ice_sdp));
Steve Antonede9ca52017-10-16 13:04:27 -0700985 if (signaling_message_receiver_ == nullptr || !signal_ice_candidates_) {
deadbeef1dcb1642017-03-29 21:08:16 -0700986 // Remote party may be deleted.
987 return;
988 }
Qingsi Wang1dac6d82018-12-12 15:28:47 -0800989 SendIceMessage(candidate->sdp_mid(), candidate->sdp_mline_index(), ice_sdp);
Qingsi Wangc129c352019-04-18 10:41:58 -0700990 last_candidate_gathered_ = candidate->candidate();
deadbeef1dcb1642017-03-29 21:08:16 -0700991 }
Eldar Rello0095d372019-12-02 22:22:07 +0200992 void OnIceCandidateError(const std::string& address,
993 int port,
Eldar Relloda13ea22019-06-01 12:23:43 +0300994 const std::string& url,
995 int error_code,
996 const std::string& error_text) override {
Eldar Rello0095d372019-12-02 22:22:07 +0200997 error_event_ = cricket::IceCandidateErrorEvent(address, port, url,
Eldar Relloda13ea22019-06-01 12:23:43 +0300998 error_code, error_text);
999 }
deadbeef1dcb1642017-03-29 21:08:16 -07001000 void OnDataChannel(
1001 rtc::scoped_refptr<DataChannelInterface> data_channel) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001002 RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
deadbeef1dcb1642017-03-29 21:08:16 -07001003 data_channel_ = data_channel;
1004 data_observer_.reset(new MockDataChannelObserver(data_channel));
1005 }
1006
deadbeef1dcb1642017-03-29 21:08:16 -07001007 std::string debug_name_;
1008
1009 std::unique_ptr<rtc::FakeNetworkManager> fake_network_manager_;
Qingsi Wangecd30542019-05-22 14:34:56 -07001010 // Reference to the mDNS responder owned by |fake_network_manager_| after set.
1011 webrtc::FakeMdnsResponder* mdns_responder_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -07001012
1013 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
1014 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
1015 peer_connection_factory_;
1016
Steve Antonede9ca52017-10-16 13:04:27 -07001017 cricket::PortAllocator* port_allocator_;
deadbeef1dcb1642017-03-29 21:08:16 -07001018 // Needed to keep track of number of frames sent.
1019 rtc::scoped_refptr<FakeAudioCaptureModule> fake_audio_capture_module_;
1020 // Needed to keep track of number of frames received.
1021 std::map<std::string, std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1022 fake_video_renderers_;
1023 // Needed to ensure frames aren't received for removed tracks.
1024 std::vector<std::unique_ptr<webrtc::FakeVideoTrackRenderer>>
1025 removed_fake_video_renderers_;
deadbeef1dcb1642017-03-29 21:08:16 -07001026
1027 // For remote peer communication.
1028 SignalingMessageReceiver* signaling_message_receiver_ = nullptr;
1029 int signaling_delay_ms_ = 0;
Steve Antonede9ca52017-10-16 13:04:27 -07001030 bool signal_ice_candidates_ = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07001031 cricket::Candidate last_candidate_gathered_;
Eldar Relloda13ea22019-06-01 12:23:43 +03001032 cricket::IceCandidateErrorEvent error_event_;
deadbeef1dcb1642017-03-29 21:08:16 -07001033
Niels Möller5c7efe72018-05-11 10:34:46 +02001034 // Store references to the video sources we've created, so that we can stop
deadbeef1dcb1642017-03-29 21:08:16 -07001035 // them, if required.
Niels Möller5c7efe72018-05-11 10:34:46 +02001036 std::vector<rtc::scoped_refptr<webrtc::VideoTrackSource>>
1037 video_track_sources_;
deadbeef1dcb1642017-03-29 21:08:16 -07001038 // |local_video_renderer_| attached to the first created local video track.
1039 std::unique_ptr<webrtc::FakeVideoTrackRenderer> local_video_renderer_;
1040
Seth Hampson2f0d7022018-02-20 11:54:42 -08001041 SdpSemantics sdp_semantics_;
deadbeef1dcb1642017-03-29 21:08:16 -07001042 PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options_;
1043 std::function<void(cricket::SessionDescription*)> received_sdp_munger_;
1044 std::function<void(cricket::SessionDescription*)> generated_sdp_munger_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001045 std::function<void()> remote_offer_handler_;
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001046 rtc::MockAsyncResolver* remote_async_resolver_ = nullptr;
deadbeef1dcb1642017-03-29 21:08:16 -07001047 rtc::scoped_refptr<DataChannelInterface> data_channel_;
1048 std::unique_ptr<MockDataChannelObserver> data_observer_;
1049
1050 std::vector<std::unique_ptr<MockRtpReceiverObserver>> rtp_receiver_observers_;
1051
Steve Antonede9ca52017-10-16 13:04:27 -07001052 std::vector<PeerConnectionInterface::IceConnectionState>
1053 ice_connection_state_history_;
Jonas Olssonacd8ae72019-02-25 15:26:24 +01001054 std::vector<PeerConnectionInterface::IceConnectionState>
1055 standardized_ice_connection_state_history_;
Jonas Olsson635474e2018-10-18 15:58:17 +02001056 std::vector<PeerConnectionInterface::PeerConnectionState>
1057 peer_connection_state_history_;
Steve Antonede9ca52017-10-16 13:04:27 -07001058 std::vector<PeerConnectionInterface::IceGatheringState>
1059 ice_gathering_state_history_;
Alex Drake00c7ecf2019-08-06 10:54:47 -07001060 std::vector<cricket::CandidatePairChangeEvent>
1061 ice_candidate_pair_change_history_;
Eldar Rello5ab79e62019-10-09 18:29:44 +03001062 std::vector<PeerConnectionInterface::SignalingState>
1063 peer_connection_signaling_state_history_;
Qingsi Wang7685e862018-06-11 20:15:46 -07001064 webrtc::FakeRtcEventLogFactory* event_log_factory_;
1065
deadbeef1dcb1642017-03-29 21:08:16 -07001066 rtc::AsyncInvoker invoker_;
1067
Seth Hampson2f0d7022018-02-20 11:54:42 -08001068 friend class PeerConnectionIntegrationBaseTest;
deadbeef1dcb1642017-03-29 21:08:16 -07001069};
1070
Elad Alon99c3fe52017-10-13 16:29:40 +02001071class MockRtcEventLogOutput : public webrtc::RtcEventLogOutput {
1072 public:
1073 virtual ~MockRtcEventLogOutput() = default;
Danil Chapovalov3a353122020-05-15 11:16:53 +02001074 MOCK_METHOD(bool, IsActive, (), (const, override));
1075 MOCK_METHOD(bool, Write, (const std::string&), (override));
Elad Alon99c3fe52017-10-13 16:29:40 +02001076};
1077
Seth Hampson2f0d7022018-02-20 11:54:42 -08001078// This helper object is used for both specifying how many audio/video frames
1079// are expected to be received for a caller/callee. It provides helper functions
1080// to specify these expectations. The object initially starts in a state of no
1081// expectations.
1082class MediaExpectations {
1083 public:
1084 enum ExpectFrames {
1085 kExpectSomeFrames,
1086 kExpectNoFrames,
1087 kNoExpectation,
1088 };
1089
1090 void ExpectBidirectionalAudioAndVideo() {
1091 ExpectBidirectionalAudio();
1092 ExpectBidirectionalVideo();
1093 }
1094
1095 void ExpectBidirectionalAudio() {
1096 CallerExpectsSomeAudio();
1097 CalleeExpectsSomeAudio();
1098 }
1099
1100 void ExpectNoAudio() {
1101 CallerExpectsNoAudio();
1102 CalleeExpectsNoAudio();
1103 }
1104
1105 void ExpectBidirectionalVideo() {
1106 CallerExpectsSomeVideo();
1107 CalleeExpectsSomeVideo();
1108 }
1109
1110 void ExpectNoVideo() {
1111 CallerExpectsNoVideo();
1112 CalleeExpectsNoVideo();
1113 }
1114
1115 void CallerExpectsSomeAudioAndVideo() {
1116 CallerExpectsSomeAudio();
1117 CallerExpectsSomeVideo();
1118 }
1119
1120 void CalleeExpectsSomeAudioAndVideo() {
1121 CalleeExpectsSomeAudio();
1122 CalleeExpectsSomeVideo();
1123 }
1124
1125 // Caller's audio functions.
1126 void CallerExpectsSomeAudio(
1127 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1128 caller_audio_expectation_ = kExpectSomeFrames;
1129 caller_audio_frames_expected_ = expected_audio_frames;
1130 }
1131
1132 void CallerExpectsNoAudio() {
1133 caller_audio_expectation_ = kExpectNoFrames;
1134 caller_audio_frames_expected_ = 0;
1135 }
1136
1137 // Caller's video functions.
1138 void CallerExpectsSomeVideo(
1139 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1140 caller_video_expectation_ = kExpectSomeFrames;
1141 caller_video_frames_expected_ = expected_video_frames;
1142 }
1143
1144 void CallerExpectsNoVideo() {
1145 caller_video_expectation_ = kExpectNoFrames;
1146 caller_video_frames_expected_ = 0;
1147 }
1148
1149 // Callee's audio functions.
1150 void CalleeExpectsSomeAudio(
1151 int expected_audio_frames = kDefaultExpectedAudioFrameCount) {
1152 callee_audio_expectation_ = kExpectSomeFrames;
1153 callee_audio_frames_expected_ = expected_audio_frames;
1154 }
1155
1156 void CalleeExpectsNoAudio() {
1157 callee_audio_expectation_ = kExpectNoFrames;
1158 callee_audio_frames_expected_ = 0;
1159 }
1160
1161 // Callee's video functions.
1162 void CalleeExpectsSomeVideo(
1163 int expected_video_frames = kDefaultExpectedVideoFrameCount) {
1164 callee_video_expectation_ = kExpectSomeFrames;
1165 callee_video_frames_expected_ = expected_video_frames;
1166 }
1167
1168 void CalleeExpectsNoVideo() {
1169 callee_video_expectation_ = kExpectNoFrames;
1170 callee_video_frames_expected_ = 0;
1171 }
1172
1173 ExpectFrames caller_audio_expectation_ = kNoExpectation;
1174 ExpectFrames caller_video_expectation_ = kNoExpectation;
1175 ExpectFrames callee_audio_expectation_ = kNoExpectation;
1176 ExpectFrames callee_video_expectation_ = kNoExpectation;
1177 int caller_audio_frames_expected_ = 0;
1178 int caller_video_frames_expected_ = 0;
1179 int callee_audio_frames_expected_ = 0;
1180 int callee_video_frames_expected_ = 0;
1181};
1182
Qingsi Wang25ec8882019-11-15 12:33:05 -08001183class MockIceTransport : public webrtc::IceTransportInterface {
1184 public:
1185 MockIceTransport(const std::string& name, int component)
1186 : internal_(std::make_unique<cricket::FakeIceTransport>(
1187 name,
1188 component,
1189 nullptr /* network_thread */)) {}
1190 ~MockIceTransport() = default;
1191 cricket::IceTransportInternal* internal() { return internal_.get(); }
1192
1193 private:
1194 std::unique_ptr<cricket::FakeIceTransport> internal_;
1195};
1196
1197class MockIceTransportFactory : public IceTransportFactory {
1198 public:
1199 ~MockIceTransportFactory() override = default;
1200 rtc::scoped_refptr<IceTransportInterface> CreateIceTransport(
1201 const std::string& transport_name,
1202 int component,
1203 IceTransportInit init) {
1204 RecordIceTransportCreated();
1205 return new rtc::RefCountedObject<MockIceTransport>(transport_name,
1206 component);
1207 }
Danil Chapovalov3a353122020-05-15 11:16:53 +02001208 MOCK_METHOD(void, RecordIceTransportCreated, ());
Qingsi Wang25ec8882019-11-15 12:33:05 -08001209};
1210
deadbeef1dcb1642017-03-29 21:08:16 -07001211// Tests two PeerConnections connecting to each other end-to-end, using a
1212// virtual network, fake A/V capture and fake encoder/decoders. The
1213// PeerConnections share the threads/socket servers, but use separate versions
1214// of everything else (including "PeerConnectionFactory"s).
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001215class PeerConnectionIntegrationBaseTest : public ::testing::Test {
deadbeef1dcb1642017-03-29 21:08:16 -07001216 public:
Seth Hampson2f0d7022018-02-20 11:54:42 -08001217 explicit PeerConnectionIntegrationBaseTest(SdpSemantics sdp_semantics)
1218 : sdp_semantics_(sdp_semantics),
1219 ss_(new rtc::VirtualSocketServer()),
Steve Antonede9ca52017-10-16 13:04:27 -07001220 fss_(new rtc::FirewallSocketServer(ss_.get())),
1221 network_thread_(new rtc::Thread(fss_.get())),
Niels Möller2a707032020-06-16 16:39:13 +02001222 worker_thread_(rtc::Thread::Create()) {
Sebastian Jansson8a793a02018-03-13 15:21:48 +01001223 network_thread_->SetName("PCNetworkThread", this);
1224 worker_thread_->SetName("PCWorkerThread", this);
deadbeef1dcb1642017-03-29 21:08:16 -07001225 RTC_CHECK(network_thread_->Start());
1226 RTC_CHECK(worker_thread_->Start());
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001227 webrtc::metrics::Reset();
deadbeef1dcb1642017-03-29 21:08:16 -07001228 }
1229
Seth Hampson2f0d7022018-02-20 11:54:42 -08001230 ~PeerConnectionIntegrationBaseTest() {
Seth Hampsonaed71642018-06-11 07:41:32 -07001231 // The PeerConnections should deleted before the TurnCustomizers.
1232 // A TurnPort is created with a raw pointer to a TurnCustomizer. The
1233 // TurnPort has the same lifetime as the PeerConnection, so it's expected
1234 // that the TurnCustomizer outlives the life of the PeerConnection or else
1235 // when Send() is called it will hit a seg fault.
deadbeef1dcb1642017-03-29 21:08:16 -07001236 if (caller_) {
1237 caller_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001238 delete SetCallerPcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001239 }
1240 if (callee_) {
1241 callee_->set_signaling_message_receiver(nullptr);
Seth Hampsonaed71642018-06-11 07:41:32 -07001242 delete SetCalleePcWrapperAndReturnCurrent(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07001243 }
Seth Hampsonaed71642018-06-11 07:41:32 -07001244
1245 // If turn servers were created for the test they need to be destroyed on
1246 // the network thread.
1247 network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
1248 turn_servers_.clear();
1249 turn_customizers_.clear();
1250 });
deadbeef1dcb1642017-03-29 21:08:16 -07001251 }
1252
1253 bool SignalingStateStable() {
1254 return caller_->SignalingStateStable() && callee_->SignalingStateStable();
1255 }
1256
deadbeef71452802017-05-07 17:21:01 -07001257 bool DtlsConnected() {
Alex Loiko9289eda2018-11-23 16:18:59 +00001258 // TODO(deadbeef): kIceConnectionConnected currently means both ICE and DTLS
1259 // are connected. This is an important distinction. Once we have separate
1260 // ICE and DTLS state, this check needs to use the DTLS state.
1261 return (callee()->ice_connection_state() ==
1262 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1263 callee()->ice_connection_state() ==
1264 webrtc::PeerConnectionInterface::kIceConnectionCompleted) &&
1265 (caller()->ice_connection_state() ==
1266 webrtc::PeerConnectionInterface::kIceConnectionConnected ||
1267 caller()->ice_connection_state() ==
1268 webrtc::PeerConnectionInterface::kIceConnectionCompleted);
deadbeef71452802017-05-07 17:21:01 -07001269 }
1270
Qingsi Wang7685e862018-06-11 20:15:46 -07001271 // When |event_log_factory| is null, the default implementation of the event
1272 // log factory will be used.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001273 std::unique_ptr<PeerConnectionWrapper> CreatePeerConnectionWrapper(
1274 const std::string& debug_name,
Seth Hampson2f0d7022018-02-20 11:54:42 -08001275 const PeerConnectionFactory::Options* options,
1276 const RTCConfiguration* config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001277 webrtc::PeerConnectionDependencies dependencies,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001278 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory,
Johannes Kron3e983682020-03-29 22:17:00 +02001279 bool reset_encoder_factory,
1280 bool reset_decoder_factory) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001281 RTCConfiguration modified_config;
1282 if (config) {
1283 modified_config = *config;
1284 }
Steve Anton3acffc32018-04-12 17:21:03 -07001285 modified_config.sdp_semantics = sdp_semantics_;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001286 if (!dependencies.cert_generator) {
1287 dependencies.cert_generator =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001288 std::make_unique<FakeRTCCertificateGenerator>();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001289 }
1290 std::unique_ptr<PeerConnectionWrapper> client(
1291 new PeerConnectionWrapper(debug_name));
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001292
Niels Möllerf06f9232018-08-07 12:32:18 +02001293 if (!client->Init(options, &modified_config, std::move(dependencies),
1294 network_thread_.get(), worker_thread_.get(),
Niels Möller2a707032020-06-16 16:39:13 +02001295 std::move(event_log_factory), reset_encoder_factory,
Johannes Kron3e983682020-03-29 22:17:00 +02001296 reset_decoder_factory)) {
Seth Hampson2f0d7022018-02-20 11:54:42 -08001297 return nullptr;
1298 }
1299 return client;
1300 }
1301
Qingsi Wang7685e862018-06-11 20:15:46 -07001302 std::unique_ptr<PeerConnectionWrapper>
1303 CreatePeerConnectionWrapperWithFakeRtcEventLog(
1304 const std::string& debug_name,
Qingsi Wang7685e862018-06-11 20:15:46 -07001305 const PeerConnectionFactory::Options* options,
1306 const RTCConfiguration* config,
1307 webrtc::PeerConnectionDependencies dependencies) {
1308 std::unique_ptr<webrtc::FakeRtcEventLogFactory> event_log_factory(
1309 new webrtc::FakeRtcEventLogFactory(rtc::Thread::Current()));
Niels Möller2a707032020-06-16 16:39:13 +02001310 return CreatePeerConnectionWrapper(debug_name, options, config,
1311 std::move(dependencies),
1312 std::move(event_log_factory),
1313 /*reset_encoder_factory=*/false,
1314 /*reset_decoder_factory=*/false);
Qingsi Wang7685e862018-06-11 20:15:46 -07001315 }
1316
deadbeef1dcb1642017-03-29 21:08:16 -07001317 bool CreatePeerConnectionWrappers() {
1318 return CreatePeerConnectionWrappersWithConfig(
1319 PeerConnectionInterface::RTCConfiguration(),
1320 PeerConnectionInterface::RTCConfiguration());
1321 }
1322
Steve Anton3acffc32018-04-12 17:21:03 -07001323 bool CreatePeerConnectionWrappersWithSdpSemantics(
1324 SdpSemantics caller_semantics,
1325 SdpSemantics callee_semantics) {
1326 // Can't specify the sdp_semantics in the passed-in configuration since it
1327 // will be overwritten by CreatePeerConnectionWrapper with whatever is
1328 // stored in sdp_semantics_. So get around this by modifying the instance
1329 // variable before calling CreatePeerConnectionWrapper for the caller and
1330 // callee PeerConnections.
1331 SdpSemantics original_semantics = sdp_semantics_;
1332 sdp_semantics_ = caller_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001333 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001334 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Niels Möller2a707032020-06-16 16:39:13 +02001335 nullptr,
Johannes Kron3e983682020-03-29 22:17:00 +02001336 /*reset_encoder_factory=*/false,
1337 /*reset_decoder_factory=*/false);
Steve Anton3acffc32018-04-12 17:21:03 -07001338 sdp_semantics_ = callee_semantics;
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001339 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001340 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Niels Möller2a707032020-06-16 16:39:13 +02001341 nullptr,
Johannes Kron3e983682020-03-29 22:17:00 +02001342 /*reset_encoder_factory=*/false,
1343 /*reset_decoder_factory=*/false);
Steve Anton3acffc32018-04-12 17:21:03 -07001344 sdp_semantics_ = original_semantics;
1345 return caller_ && callee_;
1346 }
1347
deadbeef1dcb1642017-03-29 21:08:16 -07001348 bool CreatePeerConnectionWrappersWithConfig(
1349 const PeerConnectionInterface::RTCConfiguration& caller_config,
1350 const PeerConnectionInterface::RTCConfiguration& callee_config) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001351 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001352 "Caller", nullptr, &caller_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001353 webrtc::PeerConnectionDependencies(nullptr), nullptr,
Niels Möller2a707032020-06-16 16:39:13 +02001354 /*reset_encoder_factory=*/false,
Johannes Kron3e983682020-03-29 22:17:00 +02001355 /*reset_decoder_factory=*/false);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001356 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001357 "Callee", nullptr, &callee_config,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001358 webrtc::PeerConnectionDependencies(nullptr), nullptr,
Niels Möller2a707032020-06-16 16:39:13 +02001359 /*reset_encoder_factory=*/false,
Johannes Kron3e983682020-03-29 22:17:00 +02001360 /*reset_decoder_factory=*/false);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001361 return caller_ && callee_;
1362 }
1363
1364 bool CreatePeerConnectionWrappersWithConfigAndDeps(
1365 const PeerConnectionInterface::RTCConfiguration& caller_config,
1366 webrtc::PeerConnectionDependencies caller_dependencies,
1367 const PeerConnectionInterface::RTCConfiguration& callee_config,
1368 webrtc::PeerConnectionDependencies callee_dependencies) {
Niels Möller2a707032020-06-16 16:39:13 +02001369 caller_ =
1370 CreatePeerConnectionWrapper("Caller", nullptr, &caller_config,
1371 std::move(caller_dependencies), nullptr,
1372 /*reset_encoder_factory=*/false,
1373 /*reset_decoder_factory=*/false);
1374 callee_ =
1375 CreatePeerConnectionWrapper("Callee", nullptr, &callee_config,
1376 std::move(callee_dependencies), nullptr,
1377 /*reset_encoder_factory=*/false,
1378 /*reset_decoder_factory=*/false);
deadbeef1dcb1642017-03-29 21:08:16 -07001379 return caller_ && callee_;
1380 }
1381
1382 bool CreatePeerConnectionWrappersWithOptions(
1383 const PeerConnectionFactory::Options& caller_options,
1384 const PeerConnectionFactory::Options& callee_options) {
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001385 caller_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001386 "Caller", &caller_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001387 webrtc::PeerConnectionDependencies(nullptr), nullptr,
Niels Möller2a707032020-06-16 16:39:13 +02001388 /*reset_encoder_factory=*/false,
Johannes Kron3e983682020-03-29 22:17:00 +02001389 /*reset_decoder_factory=*/false);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001390 callee_ = CreatePeerConnectionWrapper(
Niels Möllerf06f9232018-08-07 12:32:18 +02001391 "Callee", &callee_options, nullptr,
Bjorn Mellem175aa2e2018-11-08 11:23:22 -08001392 webrtc::PeerConnectionDependencies(nullptr), nullptr,
Niels Möller2a707032020-06-16 16:39:13 +02001393 /*reset_encoder_factory=*/false,
Johannes Kron3e983682020-03-29 22:17:00 +02001394 /*reset_decoder_factory=*/false);
Qingsi Wang7685e862018-06-11 20:15:46 -07001395 return caller_ && callee_;
1396 }
1397
1398 bool CreatePeerConnectionWrappersWithFakeRtcEventLog() {
1399 PeerConnectionInterface::RTCConfiguration default_config;
1400 caller_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001401 "Caller", nullptr, &default_config,
Qingsi Wang7685e862018-06-11 20:15:46 -07001402 webrtc::PeerConnectionDependencies(nullptr));
1403 callee_ = CreatePeerConnectionWrapperWithFakeRtcEventLog(
Niels Möllerf06f9232018-08-07 12:32:18 +02001404 "Callee", nullptr, &default_config,
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001405 webrtc::PeerConnectionDependencies(nullptr));
deadbeef1dcb1642017-03-29 21:08:16 -07001406 return caller_ && callee_;
1407 }
1408
Seth Hampson2f0d7022018-02-20 11:54:42 -08001409 std::unique_ptr<PeerConnectionWrapper>
1410 CreatePeerConnectionWrapperWithAlternateKey() {
deadbeef1dcb1642017-03-29 21:08:16 -07001411 std::unique_ptr<FakeRTCCertificateGenerator> cert_generator(
1412 new FakeRTCCertificateGenerator());
1413 cert_generator->use_alternate_key();
1414
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07001415 webrtc::PeerConnectionDependencies dependencies(nullptr);
1416 dependencies.cert_generator = std::move(cert_generator);
Niels Möller2a707032020-06-16 16:39:13 +02001417 return CreatePeerConnectionWrapper("New Peer", nullptr, nullptr,
1418 std::move(dependencies), nullptr,
1419 /*reset_encoder_factory=*/false,
1420 /*reset_decoder_factory=*/false);
Johannes Kron3e983682020-03-29 22:17:00 +02001421 }
1422
1423 bool CreateOneDirectionalPeerConnectionWrappers(bool caller_to_callee) {
1424 caller_ = CreatePeerConnectionWrapper(
1425 "Caller", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Niels Möller2a707032020-06-16 16:39:13 +02001426 nullptr,
Johannes Kron3e983682020-03-29 22:17:00 +02001427 /*reset_encoder_factory=*/!caller_to_callee,
1428 /*reset_decoder_factory=*/caller_to_callee);
1429 callee_ = CreatePeerConnectionWrapper(
1430 "Callee", nullptr, nullptr, webrtc::PeerConnectionDependencies(nullptr),
Niels Möller2a707032020-06-16 16:39:13 +02001431 nullptr,
Johannes Kron3e983682020-03-29 22:17:00 +02001432 /*reset_encoder_factory=*/caller_to_callee,
1433 /*reset_decoder_factory=*/!caller_to_callee);
1434 return caller_ && callee_;
deadbeef1dcb1642017-03-29 21:08:16 -07001435 }
1436
Seth Hampsonaed71642018-06-11 07:41:32 -07001437 cricket::TestTurnServer* CreateTurnServer(
1438 rtc::SocketAddress internal_address,
1439 rtc::SocketAddress external_address,
1440 cricket::ProtocolType type = cricket::ProtocolType::PROTO_UDP,
1441 const std::string& common_name = "test turn server") {
1442 rtc::Thread* thread = network_thread();
1443 std::unique_ptr<cricket::TestTurnServer> turn_server =
1444 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnServer>>(
1445 RTC_FROM_HERE,
1446 [thread, internal_address, external_address, type, common_name] {
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001447 return std::make_unique<cricket::TestTurnServer>(
Seth Hampsonaed71642018-06-11 07:41:32 -07001448 thread, internal_address, external_address, type,
1449 /*ignore_bad_certs=*/true, common_name);
1450 });
1451 turn_servers_.push_back(std::move(turn_server));
1452 // Interactions with the turn server should be done on the network thread.
1453 return turn_servers_.back().get();
1454 }
1455
1456 cricket::TestTurnCustomizer* CreateTurnCustomizer() {
1457 std::unique_ptr<cricket::TestTurnCustomizer> turn_customizer =
1458 network_thread()->Invoke<std::unique_ptr<cricket::TestTurnCustomizer>>(
1459 RTC_FROM_HERE,
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001460 [] { return std::make_unique<cricket::TestTurnCustomizer>(); });
Seth Hampsonaed71642018-06-11 07:41:32 -07001461 turn_customizers_.push_back(std::move(turn_customizer));
1462 // Interactions with the turn customizer should be done on the network
1463 // thread.
1464 return turn_customizers_.back().get();
1465 }
1466
1467 // Checks that the function counters for a TestTurnCustomizer are greater than
1468 // 0.
1469 void ExpectTurnCustomizerCountersIncremented(
1470 cricket::TestTurnCustomizer* turn_customizer) {
1471 unsigned int allow_channel_data_counter =
1472 network_thread()->Invoke<unsigned int>(
1473 RTC_FROM_HERE, [turn_customizer] {
1474 return turn_customizer->allow_channel_data_cnt_;
1475 });
1476 EXPECT_GT(allow_channel_data_counter, 0u);
1477 unsigned int modify_counter = network_thread()->Invoke<unsigned int>(
1478 RTC_FROM_HERE,
1479 [turn_customizer] { return turn_customizer->modify_cnt_; });
1480 EXPECT_GT(modify_counter, 0u);
1481 }
1482
deadbeef1dcb1642017-03-29 21:08:16 -07001483 // Once called, SDP blobs and ICE candidates will be automatically signaled
1484 // between PeerConnections.
1485 void ConnectFakeSignaling() {
1486 caller_->set_signaling_message_receiver(callee_.get());
1487 callee_->set_signaling_message_receiver(caller_.get());
1488 }
1489
Steve Antonede9ca52017-10-16 13:04:27 -07001490 // Once called, SDP blobs will be automatically signaled between
1491 // PeerConnections. Note that ICE candidates will not be signaled unless they
1492 // are in the exchanged SDP blobs.
1493 void ConnectFakeSignalingForSdpOnly() {
1494 ConnectFakeSignaling();
1495 SetSignalIceCandidates(false);
1496 }
1497
deadbeef1dcb1642017-03-29 21:08:16 -07001498 void SetSignalingDelayMs(int delay_ms) {
1499 caller_->set_signaling_delay_ms(delay_ms);
1500 callee_->set_signaling_delay_ms(delay_ms);
1501 }
1502
Steve Antonede9ca52017-10-16 13:04:27 -07001503 void SetSignalIceCandidates(bool signal) {
1504 caller_->set_signal_ice_candidates(signal);
1505 callee_->set_signal_ice_candidates(signal);
1506 }
1507
deadbeef1dcb1642017-03-29 21:08:16 -07001508 // Messages may get lost on the unreliable DataChannel, so we send multiple
1509 // times to avoid test flakiness.
1510 void SendRtpDataWithRetries(webrtc::DataChannelInterface* dc,
1511 const std::string& data,
1512 int retries) {
1513 for (int i = 0; i < retries; ++i) {
1514 dc->Send(DataBuffer(data));
1515 }
1516 }
1517
1518 rtc::Thread* network_thread() { return network_thread_.get(); }
1519
1520 rtc::VirtualSocketServer* virtual_socket_server() { return ss_.get(); }
1521
1522 PeerConnectionWrapper* caller() { return caller_.get(); }
1523
1524 // Set the |caller_| to the |wrapper| passed in and return the
1525 // original |caller_|.
1526 PeerConnectionWrapper* SetCallerPcWrapperAndReturnCurrent(
1527 PeerConnectionWrapper* wrapper) {
1528 PeerConnectionWrapper* old = caller_.release();
1529 caller_.reset(wrapper);
1530 return old;
1531 }
1532
1533 PeerConnectionWrapper* callee() { return callee_.get(); }
1534
1535 // Set the |callee_| to the |wrapper| passed in and return the
1536 // original |callee_|.
1537 PeerConnectionWrapper* SetCalleePcWrapperAndReturnCurrent(
1538 PeerConnectionWrapper* wrapper) {
1539 PeerConnectionWrapper* old = callee_.release();
1540 callee_.reset(wrapper);
1541 return old;
1542 }
1543
Qingsi Wang1dac6d82018-12-12 15:28:47 -08001544 void SetPortAllocatorFlags(uint32_t caller_flags, uint32_t callee_flags) {
1545 network_thread()->Invoke<void>(
1546 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1547 caller()->port_allocator(), caller_flags));
1548 network_thread()->Invoke<void>(
1549 RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::set_flags,
1550 callee()->port_allocator(), callee_flags));
1551 }
1552
Steve Antonede9ca52017-10-16 13:04:27 -07001553 rtc::FirewallSocketServer* firewall() const { return fss_.get(); }
1554
Seth Hampson2f0d7022018-02-20 11:54:42 -08001555 // Expects the provided number of new frames to be received within
1556 // kMaxWaitForFramesMs. The new expected frames are specified in
1557 // |media_expectations|. Returns false if any of the expectations were
1558 // not met.
1559 bool ExpectNewFrames(const MediaExpectations& media_expectations) {
Harald Alvestrand6060df52020-08-11 09:54:02 +02001560 // Make sure there are no bogus tracks confusing the issue.
1561 caller()->RemoveUnusedVideoRenderers();
1562 callee()->RemoveUnusedVideoRenderers();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001563 // First initialize the expected frame counts based upon the current
1564 // frame count.
1565 int total_caller_audio_frames_expected = caller()->audio_frames_received();
1566 if (media_expectations.caller_audio_expectation_ ==
1567 MediaExpectations::kExpectSomeFrames) {
1568 total_caller_audio_frames_expected +=
1569 media_expectations.caller_audio_frames_expected_;
1570 }
1571 int total_caller_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001572 caller()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001573 if (media_expectations.caller_video_expectation_ ==
1574 MediaExpectations::kExpectSomeFrames) {
1575 total_caller_video_frames_expected +=
1576 media_expectations.caller_video_frames_expected_;
1577 }
1578 int total_callee_audio_frames_expected = callee()->audio_frames_received();
1579 if (media_expectations.callee_audio_expectation_ ==
1580 MediaExpectations::kExpectSomeFrames) {
1581 total_callee_audio_frames_expected +=
1582 media_expectations.callee_audio_frames_expected_;
1583 }
1584 int total_callee_video_frames_expected =
deadbeef1dcb1642017-03-29 21:08:16 -07001585 callee()->min_video_frames_received_per_track();
Seth Hampson2f0d7022018-02-20 11:54:42 -08001586 if (media_expectations.callee_video_expectation_ ==
1587 MediaExpectations::kExpectSomeFrames) {
1588 total_callee_video_frames_expected +=
1589 media_expectations.callee_video_frames_expected_;
1590 }
deadbeef1dcb1642017-03-29 21:08:16 -07001591
Seth Hampson2f0d7022018-02-20 11:54:42 -08001592 // Wait for the expected frames.
deadbeef1dcb1642017-03-29 21:08:16 -07001593 EXPECT_TRUE_WAIT(caller()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001594 total_caller_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001595 caller()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001596 total_caller_video_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001597 callee()->audio_frames_received() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001598 total_callee_audio_frames_expected &&
deadbeef1dcb1642017-03-29 21:08:16 -07001599 callee()->min_video_frames_received_per_track() >=
Seth Hampson2f0d7022018-02-20 11:54:42 -08001600 total_callee_video_frames_expected,
1601 kMaxWaitForFramesMs);
1602 bool expectations_correct =
1603 caller()->audio_frames_received() >=
1604 total_caller_audio_frames_expected &&
1605 caller()->min_video_frames_received_per_track() >=
1606 total_caller_video_frames_expected &&
1607 callee()->audio_frames_received() >=
1608 total_callee_audio_frames_expected &&
1609 callee()->min_video_frames_received_per_track() >=
1610 total_callee_video_frames_expected;
deadbeef1dcb1642017-03-29 21:08:16 -07001611
Seth Hampson2f0d7022018-02-20 11:54:42 -08001612 // After the combined wait, print out a more detailed message upon
1613 // failure.
deadbeef1dcb1642017-03-29 21:08:16 -07001614 EXPECT_GE(caller()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001615 total_caller_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001616 EXPECT_GE(caller()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001617 total_caller_video_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001618 EXPECT_GE(callee()->audio_frames_received(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001619 total_callee_audio_frames_expected);
deadbeef1dcb1642017-03-29 21:08:16 -07001620 EXPECT_GE(callee()->min_video_frames_received_per_track(),
Seth Hampson2f0d7022018-02-20 11:54:42 -08001621 total_callee_video_frames_expected);
1622
1623 // We want to make sure nothing unexpected was received.
1624 if (media_expectations.caller_audio_expectation_ ==
1625 MediaExpectations::kExpectNoFrames) {
1626 EXPECT_EQ(caller()->audio_frames_received(),
1627 total_caller_audio_frames_expected);
1628 if (caller()->audio_frames_received() !=
1629 total_caller_audio_frames_expected) {
1630 expectations_correct = false;
1631 }
1632 }
1633 if (media_expectations.caller_video_expectation_ ==
1634 MediaExpectations::kExpectNoFrames) {
1635 EXPECT_EQ(caller()->min_video_frames_received_per_track(),
1636 total_caller_video_frames_expected);
1637 if (caller()->min_video_frames_received_per_track() !=
1638 total_caller_video_frames_expected) {
1639 expectations_correct = false;
1640 }
1641 }
1642 if (media_expectations.callee_audio_expectation_ ==
1643 MediaExpectations::kExpectNoFrames) {
1644 EXPECT_EQ(callee()->audio_frames_received(),
1645 total_callee_audio_frames_expected);
1646 if (callee()->audio_frames_received() !=
1647 total_callee_audio_frames_expected) {
1648 expectations_correct = false;
1649 }
1650 }
1651 if (media_expectations.callee_video_expectation_ ==
1652 MediaExpectations::kExpectNoFrames) {
1653 EXPECT_EQ(callee()->min_video_frames_received_per_track(),
1654 total_callee_video_frames_expected);
1655 if (callee()->min_video_frames_received_per_track() !=
1656 total_callee_video_frames_expected) {
1657 expectations_correct = false;
1658 }
1659 }
1660 return expectations_correct;
deadbeef1dcb1642017-03-29 21:08:16 -07001661 }
1662
Steve Antond91969e2019-05-30 12:27:03 -07001663 void ClosePeerConnections() {
1664 caller()->pc()->Close();
1665 callee()->pc()->Close();
1666 }
1667
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001668 void TestNegotiatedCipherSuite(
1669 const PeerConnectionFactory::Options& caller_options,
1670 const PeerConnectionFactory::Options& callee_options,
1671 int expected_cipher_suite) {
deadbeef1dcb1642017-03-29 21:08:16 -07001672 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(caller_options,
1673 callee_options));
deadbeef1dcb1642017-03-29 21:08:16 -07001674 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001675 caller()->AddAudioVideoTracks();
1676 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001677 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001678 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001679 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(expected_cipher_suite),
deadbeefd8ad7882017-04-18 16:01:17 -07001680 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07001681 // TODO(bugs.webrtc.org/9456): Fix it.
Ying Wangef3998f2019-12-09 13:06:53 +01001682 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
1683 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
1684 expected_cipher_suite));
deadbeef1dcb1642017-03-29 21:08:16 -07001685 }
1686
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001687 void TestGcmNegotiationUsesCipherSuite(bool local_gcm_enabled,
1688 bool remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001689 bool aes_ctr_enabled,
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001690 int expected_cipher_suite) {
1691 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001692 caller_options.crypto_options.srtp.enable_gcm_crypto_suites =
1693 local_gcm_enabled;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001694 caller_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher =
1695 aes_ctr_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001696 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07001697 callee_options.crypto_options.srtp.enable_gcm_crypto_suites =
1698 remote_gcm_enabled;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02001699 callee_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher =
1700 aes_ctr_enabled;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07001701 TestNegotiatedCipherSuite(caller_options, callee_options,
1702 expected_cipher_suite);
1703 }
1704
Seth Hampson2f0d7022018-02-20 11:54:42 -08001705 protected:
Steve Anton3acffc32018-04-12 17:21:03 -07001706 SdpSemantics sdp_semantics_;
Seth Hampson2f0d7022018-02-20 11:54:42 -08001707
deadbeef1dcb1642017-03-29 21:08:16 -07001708 private:
1709 // |ss_| is used by |network_thread_| so it must be destroyed later.
deadbeef1dcb1642017-03-29 21:08:16 -07001710 std::unique_ptr<rtc::VirtualSocketServer> ss_;
Steve Antonede9ca52017-10-16 13:04:27 -07001711 std::unique_ptr<rtc::FirewallSocketServer> fss_;
deadbeef1dcb1642017-03-29 21:08:16 -07001712 // |network_thread_| and |worker_thread_| are used by both
1713 // |caller_| and |callee_| so they must be destroyed
1714 // later.
1715 std::unique_ptr<rtc::Thread> network_thread_;
1716 std::unique_ptr<rtc::Thread> worker_thread_;
Seth Hampsonaed71642018-06-11 07:41:32 -07001717 // The turn servers and turn customizers should be accessed & deleted on the
1718 // network thread to avoid a race with the socket read/write that occurs
1719 // on the network thread.
1720 std::vector<std::unique_ptr<cricket::TestTurnServer>> turn_servers_;
1721 std::vector<std::unique_ptr<cricket::TestTurnCustomizer>> turn_customizers_;
deadbeef1dcb1642017-03-29 21:08:16 -07001722 std::unique_ptr<PeerConnectionWrapper> caller_;
1723 std::unique_ptr<PeerConnectionWrapper> callee_;
1724};
1725
Seth Hampson2f0d7022018-02-20 11:54:42 -08001726class PeerConnectionIntegrationTest
1727 : public PeerConnectionIntegrationBaseTest,
1728 public ::testing::WithParamInterface<SdpSemantics> {
1729 protected:
1730 PeerConnectionIntegrationTest()
1731 : PeerConnectionIntegrationBaseTest(GetParam()) {}
1732};
1733
Yves Gerey100fe632020-01-17 19:15:53 +01001734// Fake clock must be set before threads are started to prevent race on
1735// Set/GetClockForTesting().
1736// To achieve that, multiple inheritance is used as a mixin pattern
1737// where order of construction is finely controlled.
1738// This also ensures peerconnection is closed before switching back to non-fake
1739// clock, avoiding other races and DCHECK failures such as in rtp_sender.cc.
1740class FakeClockForTest : public rtc::ScopedFakeClock {
1741 protected:
1742 FakeClockForTest() {
1743 // Some things use a time of "0" as a special value, so we need to start out
1744 // the fake clock at a nonzero time.
1745 // TODO(deadbeef): Fix this.
Danil Chapovalov0c626af2020-02-10 11:16:00 +01001746 AdvanceTime(webrtc::TimeDelta::Seconds(1));
Yves Gerey100fe632020-01-17 19:15:53 +01001747 }
1748
1749 // Explicit handle.
1750 ScopedFakeClock& FakeClock() { return *this; }
1751};
1752
1753// Ensure FakeClockForTest is constructed first (see class for rationale).
1754class PeerConnectionIntegrationTestWithFakeClock
1755 : public FakeClockForTest,
1756 public PeerConnectionIntegrationTest {};
1757
Seth Hampson2f0d7022018-02-20 11:54:42 -08001758class PeerConnectionIntegrationTestPlanB
1759 : public PeerConnectionIntegrationBaseTest {
1760 protected:
1761 PeerConnectionIntegrationTestPlanB()
1762 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB) {}
1763};
1764
1765class PeerConnectionIntegrationTestUnifiedPlan
1766 : public PeerConnectionIntegrationBaseTest {
1767 protected:
1768 PeerConnectionIntegrationTestUnifiedPlan()
1769 : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
1770};
1771
deadbeef1dcb1642017-03-29 21:08:16 -07001772// Test the OnFirstPacketReceived callback from audio/video RtpReceivers. This
1773// includes testing that the callback is invoked if an observer is connected
1774// after the first packet has already been received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001775TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07001776 RtpReceiverObserverOnFirstPacketReceived) {
1777 ASSERT_TRUE(CreatePeerConnectionWrappers());
1778 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08001779 caller()->AddAudioVideoTracks();
1780 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001781 // Start offer/answer exchange and wait for it to complete.
1782 caller()->CreateAndSetAndSignalOffer();
1783 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1784 // Should be one receiver each for audio/video.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001785 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1786 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001787 // Wait for all "first packet received" callbacks to be fired.
1788 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001789 absl::c_all_of(caller()->rtp_receiver_observers(),
1790 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1791 return o->first_packet_received();
1792 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001793 kMaxWaitForFramesMs);
1794 EXPECT_TRUE_WAIT(
Steve Anton64b626b2019-01-28 17:25:26 -08001795 absl::c_all_of(callee()->rtp_receiver_observers(),
1796 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1797 return o->first_packet_received();
1798 }),
deadbeef1dcb1642017-03-29 21:08:16 -07001799 kMaxWaitForFramesMs);
1800 // If new observers are set after the first packet was already received, the
1801 // callback should still be invoked.
1802 caller()->ResetRtpReceiverObservers();
1803 callee()->ResetRtpReceiverObservers();
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02001804 EXPECT_EQ(2U, caller()->rtp_receiver_observers().size());
1805 EXPECT_EQ(2U, callee()->rtp_receiver_observers().size());
deadbeef1dcb1642017-03-29 21:08:16 -07001806 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001807 absl::c_all_of(caller()->rtp_receiver_observers(),
1808 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1809 return o->first_packet_received();
1810 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001811 EXPECT_TRUE(
Steve Anton64b626b2019-01-28 17:25:26 -08001812 absl::c_all_of(callee()->rtp_receiver_observers(),
1813 [](const std::unique_ptr<MockRtpReceiverObserver>& o) {
1814 return o->first_packet_received();
1815 }));
deadbeef1dcb1642017-03-29 21:08:16 -07001816}
1817
1818class DummyDtmfObserver : public DtmfSenderObserverInterface {
1819 public:
1820 DummyDtmfObserver() : completed_(false) {}
1821
1822 // Implements DtmfSenderObserverInterface.
1823 void OnToneChange(const std::string& tone) override {
1824 tones_.push_back(tone);
1825 if (tone.empty()) {
1826 completed_ = true;
1827 }
1828 }
1829
1830 const std::vector<std::string>& tones() const { return tones_; }
1831 bool completed() const { return completed_; }
1832
1833 private:
1834 bool completed_;
1835 std::vector<std::string> tones_;
1836};
1837
1838// Assumes |sender| already has an audio track added and the offer/answer
1839// exchange is done.
1840void TestDtmfFromSenderToReceiver(PeerConnectionWrapper* sender,
1841 PeerConnectionWrapper* receiver) {
Steve Anton15324772018-01-16 10:26:49 -08001842 // We should be able to get a DTMF sender from the local sender.
1843 rtc::scoped_refptr<DtmfSenderInterface> dtmf_sender =
1844 sender->pc()->GetSenders().at(0)->GetDtmfSender();
1845 ASSERT_TRUE(dtmf_sender);
deadbeef1dcb1642017-03-29 21:08:16 -07001846 DummyDtmfObserver observer;
deadbeef1dcb1642017-03-29 21:08:16 -07001847 dtmf_sender->RegisterObserver(&observer);
1848
1849 // Test the DtmfSender object just created.
1850 EXPECT_TRUE(dtmf_sender->CanInsertDtmf());
1851 EXPECT_TRUE(dtmf_sender->InsertDtmf("1a", 100, 50));
1852
1853 EXPECT_TRUE_WAIT(observer.completed(), kDefaultTimeout);
1854 std::vector<std::string> tones = {"1", "a", ""};
1855 EXPECT_EQ(tones, observer.tones());
1856 dtmf_sender->UnregisterObserver();
1857 // TODO(deadbeef): Verify the tones were actually received end-to-end.
1858}
1859
1860// Verifies the DtmfSenderObserver callbacks for a DtmfSender (one in each
1861// direction).
Seth Hampson2f0d7022018-02-20 11:54:42 -08001862TEST_P(PeerConnectionIntegrationTest, DtmfSenderObserver) {
deadbeef1dcb1642017-03-29 21:08:16 -07001863 ASSERT_TRUE(CreatePeerConnectionWrappers());
1864 ConnectFakeSignaling();
1865 // Only need audio for DTMF.
Steve Anton15324772018-01-16 10:26:49 -08001866 caller()->AddAudioTrack();
1867 callee()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07001868 caller()->CreateAndSetAndSignalOffer();
1869 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeef71452802017-05-07 17:21:01 -07001870 // DTLS must finish before the DTMF sender can be used reliably.
1871 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07001872 TestDtmfFromSenderToReceiver(caller(), callee());
1873 TestDtmfFromSenderToReceiver(callee(), caller());
1874}
1875
1876// Basic end-to-end test, verifying media can be encoded/transmitted/decoded
1877// between two connections, using DTLS-SRTP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001878TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls) {
deadbeef1dcb1642017-03-29 21:08:16 -07001879 ASSERT_TRUE(CreatePeerConnectionWrappers());
1880 ConnectFakeSignaling();
Harald Alvestrand194939b2018-01-24 16:04:13 +01001881
deadbeef1dcb1642017-03-29 21:08:16 -07001882 // Do normal offer/answer and wait for some frames to be received in each
1883 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001884 caller()->AddAudioVideoTracks();
1885 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001886 caller()->CreateAndSetAndSignalOffer();
1887 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001888 MediaExpectations media_expectations;
1889 media_expectations.ExpectBidirectionalAudioAndVideo();
1890 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Ying Wangef3998f2019-12-09 13:06:53 +01001891 EXPECT_METRIC_LE(
1892 2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1893 webrtc::kEnumCounterKeyProtocolDtls));
1894 EXPECT_METRIC_EQ(
1895 0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1896 webrtc::kEnumCounterKeyProtocolSdes));
deadbeef1dcb1642017-03-29 21:08:16 -07001897}
1898
1899// Uses SDES instead of DTLS for key agreement.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001900TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSdes) {
deadbeef1dcb1642017-03-29 21:08:16 -07001901 PeerConnectionInterface::RTCConfiguration sdes_config;
1902 sdes_config.enable_dtls_srtp.emplace(false);
1903 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(sdes_config, sdes_config));
1904 ConnectFakeSignaling();
1905
1906 // Do normal offer/answer and wait for some frames to be received in each
1907 // direction.
Steve Anton15324772018-01-16 10:26:49 -08001908 caller()->AddAudioVideoTracks();
1909 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07001910 caller()->CreateAndSetAndSignalOffer();
1911 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08001912 MediaExpectations media_expectations;
1913 media_expectations.ExpectBidirectionalAudioAndVideo();
1914 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Ying Wangef3998f2019-12-09 13:06:53 +01001915 EXPECT_METRIC_LE(
1916 2, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1917 webrtc::kEnumCounterKeyProtocolSdes));
1918 EXPECT_METRIC_EQ(
1919 0, webrtc::metrics::NumEvents("WebRTC.PeerConnection.KeyProtocol",
1920 webrtc::kEnumCounterKeyProtocolDtls));
deadbeef1dcb1642017-03-29 21:08:16 -07001921}
1922
Steve Anton9a44b2d2019-07-12 12:58:30 -07001923// Basic end-to-end test specifying the |enable_encrypted_rtp_header_extensions|
1924// option to offer encrypted versions of all header extensions alongside the
1925// unencrypted versions.
1926TEST_P(PeerConnectionIntegrationTest,
1927 EndToEndCallWithEncryptedRtpHeaderExtensions) {
1928 CryptoOptions crypto_options;
1929 crypto_options.srtp.enable_encrypted_rtp_header_extensions = true;
1930 PeerConnectionInterface::RTCConfiguration config;
1931 config.crypto_options = crypto_options;
1932 // Note: This allows offering >14 RTP header extensions.
1933 config.offer_extmap_allow_mixed = true;
1934 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
1935 ConnectFakeSignaling();
1936
1937 // Do normal offer/answer and wait for some frames to be received in each
1938 // direction.
1939 caller()->AddAudioVideoTracks();
1940 callee()->AddAudioVideoTracks();
1941 caller()->CreateAndSetAndSignalOffer();
1942 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1943 MediaExpectations media_expectations;
1944 media_expectations.ExpectBidirectionalAudioAndVideo();
1945 ASSERT_TRUE(ExpectNewFrames(media_expectations));
1946}
1947
Steve Anton8c0f7a72017-10-03 10:03:10 -07001948// Tests that the GetRemoteAudioSSLCertificate method returns the remote DTLS
1949// certificate once the DTLS handshake has finished.
Seth Hampson2f0d7022018-02-20 11:54:42 -08001950TEST_P(PeerConnectionIntegrationTest,
Steve Anton8c0f7a72017-10-03 10:03:10 -07001951 GetRemoteAudioSSLCertificateReturnsExchangedCertificate) {
1952 auto GetRemoteAudioSSLCertificate = [](PeerConnectionWrapper* wrapper) {
1953 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1954 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1955 return pc->GetRemoteAudioSSLCertificate();
1956 };
Zhi Huang70b820f2018-01-27 14:16:15 -08001957 auto GetRemoteAudioSSLCertChain = [](PeerConnectionWrapper* wrapper) {
1958 auto pci = reinterpret_cast<PeerConnectionProxy*>(wrapper->pc());
1959 auto pc = reinterpret_cast<PeerConnection*>(pci->internal());
1960 return pc->GetRemoteAudioSSLCertChain();
1961 };
Steve Anton8c0f7a72017-10-03 10:03:10 -07001962
1963 auto caller_cert = rtc::RTCCertificate::FromPEM(kRsaPems[0]);
1964 auto callee_cert = rtc::RTCCertificate::FromPEM(kRsaPems[1]);
1965
1966 // Configure each side with a known certificate so they can be compared later.
1967 PeerConnectionInterface::RTCConfiguration caller_config;
1968 caller_config.enable_dtls_srtp.emplace(true);
1969 caller_config.certificates.push_back(caller_cert);
1970 PeerConnectionInterface::RTCConfiguration callee_config;
1971 callee_config.enable_dtls_srtp.emplace(true);
1972 callee_config.certificates.push_back(callee_cert);
1973 ASSERT_TRUE(
1974 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
1975 ConnectFakeSignaling();
1976
1977 // When first initialized, there should not be a remote SSL certificate (and
1978 // calling this method should not crash).
1979 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(caller()));
1980 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertificate(callee()));
Zhi Huang70b820f2018-01-27 14:16:15 -08001981 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(caller()));
1982 EXPECT_EQ(nullptr, GetRemoteAudioSSLCertChain(callee()));
Steve Anton8c0f7a72017-10-03 10:03:10 -07001983
Steve Anton15324772018-01-16 10:26:49 -08001984 caller()->AddAudioTrack();
1985 callee()->AddAudioTrack();
Steve Anton8c0f7a72017-10-03 10:03:10 -07001986 caller()->CreateAndSetAndSignalOffer();
1987 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
1988 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
1989
1990 // Once DTLS has been connected, each side should return the other's SSL
1991 // certificate when calling GetRemoteAudioSSLCertificate.
1992
1993 auto caller_remote_cert = GetRemoteAudioSSLCertificate(caller());
1994 ASSERT_TRUE(caller_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07001995 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07001996 caller_remote_cert->ToPEMString());
1997
1998 auto callee_remote_cert = GetRemoteAudioSSLCertificate(callee());
1999 ASSERT_TRUE(callee_remote_cert);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07002000 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Steve Anton8c0f7a72017-10-03 10:03:10 -07002001 callee_remote_cert->ToPEMString());
Zhi Huang70b820f2018-01-27 14:16:15 -08002002
2003 auto caller_remote_cert_chain = GetRemoteAudioSSLCertChain(caller());
2004 ASSERT_TRUE(caller_remote_cert_chain);
2005 ASSERT_EQ(1U, caller_remote_cert_chain->GetSize());
2006 auto remote_cert = &caller_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07002007 EXPECT_EQ(callee_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08002008 remote_cert->ToPEMString());
2009
2010 auto callee_remote_cert_chain = GetRemoteAudioSSLCertChain(callee());
2011 ASSERT_TRUE(callee_remote_cert_chain);
2012 ASSERT_EQ(1U, callee_remote_cert_chain->GetSize());
2013 remote_cert = &callee_remote_cert_chain->Get(0);
Benjamin Wright6c6c9df2018-10-25 01:16:26 -07002014 EXPECT_EQ(caller_cert->GetSSLCertificate().ToPEMString(),
Zhi Huang70b820f2018-01-27 14:16:15 -08002015 remote_cert->ToPEMString());
Steve Anton8c0f7a72017-10-03 10:03:10 -07002016}
2017
deadbeef1dcb1642017-03-29 21:08:16 -07002018// This test sets up a call between two parties with a source resolution of
2019// 1280x720 and verifies that a 16:9 aspect ratio is received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002020TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002021 Send1280By720ResolutionAndReceive16To9AspectRatio) {
2022 ASSERT_TRUE(CreatePeerConnectionWrappers());
2023 ConnectFakeSignaling();
2024
Niels Möller5c7efe72018-05-11 10:34:46 +02002025 // Add video tracks with 16:9 aspect ratio, size 1280 x 720.
2026 webrtc::FakePeriodicVideoSource::Config config;
2027 config.width = 1280;
2028 config.height = 720;
Johannes Kron965e7942018-09-13 15:36:20 +02002029 config.timestamp_offset_ms = rtc::TimeMillis();
Niels Möller5c7efe72018-05-11 10:34:46 +02002030 caller()->AddTrack(caller()->CreateLocalVideoTrackWithConfig(config));
2031 callee()->AddTrack(callee()->CreateLocalVideoTrackWithConfig(config));
deadbeef1dcb1642017-03-29 21:08:16 -07002032
2033 // Do normal offer/answer and wait for at least one frame to be received in
2034 // each direction.
2035 caller()->CreateAndSetAndSignalOffer();
2036 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2037 callee()->min_video_frames_received_per_track() > 0,
2038 kMaxWaitForFramesMs);
2039
2040 // Check rendered aspect ratio.
2041 EXPECT_EQ(16.0 / 9, caller()->local_rendered_aspect_ratio());
2042 EXPECT_EQ(16.0 / 9, caller()->rendered_aspect_ratio());
2043 EXPECT_EQ(16.0 / 9, callee()->local_rendered_aspect_ratio());
2044 EXPECT_EQ(16.0 / 9, callee()->rendered_aspect_ratio());
2045}
2046
2047// This test sets up an one-way call, with media only from caller to
2048// callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002049TEST_P(PeerConnectionIntegrationTest, OneWayMediaCall) {
deadbeef1dcb1642017-03-29 21:08:16 -07002050 ASSERT_TRUE(CreatePeerConnectionWrappers());
2051 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002052 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002053 caller()->CreateAndSetAndSignalOffer();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002054 MediaExpectations media_expectations;
2055 media_expectations.CalleeExpectsSomeAudioAndVideo();
2056 media_expectations.CallerExpectsNoAudio();
2057 media_expectations.CallerExpectsNoVideo();
2058 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002059}
2060
Johannes Kron3e983682020-03-29 22:17:00 +02002061// Tests that send only works without the caller having a decoder factory and
2062// the callee having an encoder factory.
2063TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSendOnlyVideo) {
2064 ASSERT_TRUE(
2065 CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/true));
2066 ConnectFakeSignaling();
2067 // Add one-directional video, from caller to callee.
2068 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2069 caller()->CreateLocalVideoTrack();
2070 caller()->AddTrack(caller_track);
2071 PeerConnectionInterface::RTCOfferAnswerOptions options;
2072 options.offer_to_receive_video = 0;
2073 caller()->SetOfferAnswerOptions(options);
2074 caller()->CreateAndSetAndSignalOffer();
2075 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2076 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
2077
2078 // Expect video to be received in one direction.
2079 MediaExpectations media_expectations;
2080 media_expectations.CallerExpectsNoVideo();
2081 media_expectations.CalleeExpectsSomeVideo();
2082
2083 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2084}
2085
2086// Tests that receive only works without the caller having an encoder factory
2087// and the callee having a decoder factory.
2088TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithReceiveOnlyVideo) {
2089 ASSERT_TRUE(
2090 CreateOneDirectionalPeerConnectionWrappers(/*caller_to_callee=*/false));
2091 ConnectFakeSignaling();
2092 // Add one-directional video, from callee to caller.
2093 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2094 callee()->CreateLocalVideoTrack();
2095 callee()->AddTrack(callee_track);
2096 PeerConnectionInterface::RTCOfferAnswerOptions options;
2097 options.offer_to_receive_video = 1;
2098 caller()->SetOfferAnswerOptions(options);
2099 caller()->CreateAndSetAndSignalOffer();
2100 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2101 ASSERT_EQ(caller()->pc()->GetReceivers().size(), 1u);
2102
2103 // Expect video to be received in one direction.
2104 MediaExpectations media_expectations;
2105 media_expectations.CallerExpectsSomeVideo();
2106 media_expectations.CalleeExpectsNoVideo();
2107
2108 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2109}
2110
2111TEST_P(PeerConnectionIntegrationTest,
2112 EndToEndCallAddReceiveVideoToSendOnlyCall) {
2113 ASSERT_TRUE(CreatePeerConnectionWrappers());
2114 ConnectFakeSignaling();
2115 // Add one-directional video, from caller to callee.
2116 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2117 caller()->CreateLocalVideoTrack();
2118 caller()->AddTrack(caller_track);
2119 caller()->CreateAndSetAndSignalOffer();
2120 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2121
2122 // Add receive video.
2123 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2124 callee()->CreateLocalVideoTrack();
2125 callee()->AddTrack(callee_track);
2126 caller()->CreateAndSetAndSignalOffer();
2127 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2128
2129 // Ensure that video frames are received end-to-end.
2130 MediaExpectations media_expectations;
2131 media_expectations.ExpectBidirectionalVideo();
2132 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2133}
2134
2135TEST_P(PeerConnectionIntegrationTest,
2136 EndToEndCallAddSendVideoToReceiveOnlyCall) {
2137 ASSERT_TRUE(CreatePeerConnectionWrappers());
2138 ConnectFakeSignaling();
2139 // Add one-directional video, from callee to caller.
2140 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2141 callee()->CreateLocalVideoTrack();
2142 callee()->AddTrack(callee_track);
2143 caller()->CreateAndSetAndSignalOffer();
2144 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2145
2146 // Add send video.
2147 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2148 caller()->CreateLocalVideoTrack();
2149 caller()->AddTrack(caller_track);
2150 caller()->CreateAndSetAndSignalOffer();
2151 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2152
2153 // Expect video to be received in one direction.
2154 MediaExpectations media_expectations;
2155 media_expectations.ExpectBidirectionalVideo();
2156 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2157}
2158
2159TEST_P(PeerConnectionIntegrationTest,
2160 EndToEndCallRemoveReceiveVideoFromSendReceiveCall) {
2161 ASSERT_TRUE(CreatePeerConnectionWrappers());
2162 ConnectFakeSignaling();
2163 // Add send video, from caller to callee.
2164 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2165 caller()->CreateLocalVideoTrack();
2166 rtc::scoped_refptr<webrtc::RtpSenderInterface> caller_sender =
2167 caller()->AddTrack(caller_track);
2168 // Add receive video, from callee to caller.
2169 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2170 callee()->CreateLocalVideoTrack();
2171
2172 rtc::scoped_refptr<webrtc::RtpSenderInterface> callee_sender =
2173 callee()->AddTrack(callee_track);
2174 caller()->CreateAndSetAndSignalOffer();
2175 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2176
2177 // Remove receive video (i.e., callee sender track).
2178 callee()->pc()->RemoveTrack(callee_sender);
2179
2180 caller()->CreateAndSetAndSignalOffer();
2181 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2182
2183 // Expect one-directional video.
2184 MediaExpectations media_expectations;
2185 media_expectations.CallerExpectsNoVideo();
2186 media_expectations.CalleeExpectsSomeVideo();
2187
2188 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2189}
2190
2191TEST_P(PeerConnectionIntegrationTest,
2192 EndToEndCallRemoveSendVideoFromSendReceiveCall) {
2193 ASSERT_TRUE(CreatePeerConnectionWrappers());
2194 ConnectFakeSignaling();
2195 // Add send video, from caller to callee.
2196 rtc::scoped_refptr<webrtc::VideoTrackInterface> caller_track =
2197 caller()->CreateLocalVideoTrack();
2198 rtc::scoped_refptr<webrtc::RtpSenderInterface> caller_sender =
2199 caller()->AddTrack(caller_track);
2200 // Add receive video, from callee to caller.
2201 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2202 callee()->CreateLocalVideoTrack();
2203
2204 rtc::scoped_refptr<webrtc::RtpSenderInterface> callee_sender =
2205 callee()->AddTrack(callee_track);
2206 caller()->CreateAndSetAndSignalOffer();
2207 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2208
2209 // Remove send video (i.e., caller sender track).
2210 caller()->pc()->RemoveTrack(caller_sender);
2211
2212 caller()->CreateAndSetAndSignalOffer();
2213 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2214
2215 // Expect one-directional video.
2216 MediaExpectations media_expectations;
2217 media_expectations.CalleeExpectsNoVideo();
2218 media_expectations.CallerExpectsSomeVideo();
2219
2220 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2221}
2222
deadbeef1dcb1642017-03-29 21:08:16 -07002223// This test sets up a audio call initially, with the callee rejecting video
2224// initially. Then later the callee decides to upgrade to audio/video, and
2225// initiates a new offer/answer exchange.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002226TEST_P(PeerConnectionIntegrationTest, AudioToVideoUpgrade) {
deadbeef1dcb1642017-03-29 21:08:16 -07002227 ASSERT_TRUE(CreatePeerConnectionWrappers());
2228 ConnectFakeSignaling();
2229 // Initially, offer an audio/video stream from the caller, but refuse to
2230 // send/receive video on the callee side.
Steve Anton15324772018-01-16 10:26:49 -08002231 caller()->AddAudioVideoTracks();
2232 callee()->AddAudioTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002233 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2234 PeerConnectionInterface::RTCOfferAnswerOptions options;
2235 options.offer_to_receive_video = 0;
2236 callee()->SetOfferAnswerOptions(options);
2237 } else {
2238 callee()->SetRemoteOfferHandler([this] {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002239 callee()
2240 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2241 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002242 });
2243 }
deadbeef1dcb1642017-03-29 21:08:16 -07002244 // Do offer/answer and make sure audio is still received end-to-end.
2245 caller()->CreateAndSetAndSignalOffer();
2246 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002247 {
2248 MediaExpectations media_expectations;
2249 media_expectations.ExpectBidirectionalAudio();
2250 media_expectations.ExpectNoVideo();
2251 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2252 }
deadbeef1dcb1642017-03-29 21:08:16 -07002253 // Sanity check that the callee's description has a rejected video section.
2254 ASSERT_NE(nullptr, callee()->pc()->local_description());
2255 const ContentInfo* callee_video_content =
2256 GetFirstVideoContent(callee()->pc()->local_description()->description());
2257 ASSERT_NE(nullptr, callee_video_content);
2258 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002259
deadbeef1dcb1642017-03-29 21:08:16 -07002260 // Now negotiate with video and ensure negotiation succeeds, with video
2261 // frames and additional audio frames being received.
Steve Anton15324772018-01-16 10:26:49 -08002262 callee()->AddVideoTrack();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002263 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2264 PeerConnectionInterface::RTCOfferAnswerOptions options;
2265 options.offer_to_receive_video = 1;
2266 callee()->SetOfferAnswerOptions(options);
2267 } else {
2268 callee()->SetRemoteOfferHandler(nullptr);
2269 caller()->SetRemoteOfferHandler([this] {
2270 // The caller creates a new transceiver to receive video on when receiving
2271 // the offer, but by default it is send only.
2272 auto transceivers = caller()->pc()->GetTransceivers();
Harald Alvestrand6060df52020-08-11 09:54:02 +02002273 ASSERT_EQ(2U, transceivers.size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002274 ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
Harald Alvestrand6060df52020-08-11 09:54:02 +02002275 transceivers[1]->receiver()->media_type());
2276 transceivers[1]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
2277 transceivers[1]->SetDirectionWithError(
2278 RtpTransceiverDirection::kSendRecv);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002279 });
2280 }
deadbeef1dcb1642017-03-29 21:08:16 -07002281 callee()->CreateAndSetAndSignalOffer();
2282 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002283 {
2284 // Expect additional audio frames to be received after the upgrade.
2285 MediaExpectations media_expectations;
2286 media_expectations.ExpectBidirectionalAudioAndVideo();
2287 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2288 }
deadbeef1dcb1642017-03-29 21:08:16 -07002289}
2290
deadbeef4389b4d2017-09-07 09:07:36 -07002291// Simpler than the above test; just add an audio track to an established
2292// video-only connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002293TEST_P(PeerConnectionIntegrationTest, AddAudioToVideoOnlyCall) {
deadbeef4389b4d2017-09-07 09:07:36 -07002294 ASSERT_TRUE(CreatePeerConnectionWrappers());
2295 ConnectFakeSignaling();
2296 // Do initial offer/answer with just a video track.
Steve Anton15324772018-01-16 10:26:49 -08002297 caller()->AddVideoTrack();
2298 callee()->AddVideoTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002299 caller()->CreateAndSetAndSignalOffer();
2300 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2301 // Now add an audio track and do another offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08002302 caller()->AddAudioTrack();
2303 callee()->AddAudioTrack();
deadbeef4389b4d2017-09-07 09:07:36 -07002304 caller()->CreateAndSetAndSignalOffer();
2305 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2306 // Ensure both audio and video frames are received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002307 MediaExpectations media_expectations;
2308 media_expectations.ExpectBidirectionalAudioAndVideo();
2309 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef4389b4d2017-09-07 09:07:36 -07002310}
2311
deadbeef1dcb1642017-03-29 21:08:16 -07002312// This test sets up a call that's transferred to a new caller with a different
2313// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002314TEST_P(PeerConnectionIntegrationTest, CallTransferredForCallee) {
deadbeef1dcb1642017-03-29 21:08:16 -07002315 ASSERT_TRUE(CreatePeerConnectionWrappers());
2316 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002317 caller()->AddAudioVideoTracks();
2318 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002319 caller()->CreateAndSetAndSignalOffer();
2320 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2321
2322 // Keep the original peer around which will still send packets to the
2323 // receiving client. These SRTP packets will be dropped.
2324 std::unique_ptr<PeerConnectionWrapper> original_peer(
2325 SetCallerPcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002326 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002327 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2328 // directly above.
2329 original_peer->pc()->Close();
2330
2331 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002332 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002333 caller()->CreateAndSetAndSignalOffer();
2334 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2335 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002336 MediaExpectations media_expectations;
2337 media_expectations.ExpectBidirectionalAudioAndVideo();
2338 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002339}
2340
2341// This test sets up a call that's transferred to a new callee with a different
2342// DTLS fingerprint.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002343TEST_P(PeerConnectionIntegrationTest, CallTransferredForCaller) {
deadbeef1dcb1642017-03-29 21:08:16 -07002344 ASSERT_TRUE(CreatePeerConnectionWrappers());
2345 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002346 caller()->AddAudioVideoTracks();
2347 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002348 caller()->CreateAndSetAndSignalOffer();
2349 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2350
2351 // Keep the original peer around which will still send packets to the
2352 // receiving client. These SRTP packets will be dropped.
2353 std::unique_ptr<PeerConnectionWrapper> original_peer(
2354 SetCalleePcWrapperAndReturnCurrent(
Seth Hampson2f0d7022018-02-20 11:54:42 -08002355 CreatePeerConnectionWrapperWithAlternateKey().release()));
deadbeef1dcb1642017-03-29 21:08:16 -07002356 // TODO(deadbeef): Why do we call Close here? That goes against the comment
2357 // directly above.
2358 original_peer->pc()->Close();
2359
2360 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002361 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002362 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2363 caller()->CreateAndSetAndSignalOffer();
2364 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2365 // Wait for some additional frames to be transmitted end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002366 MediaExpectations media_expectations;
2367 media_expectations.ExpectBidirectionalAudioAndVideo();
2368 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002369}
2370
2371// This test sets up a non-bundled call and negotiates bundling at the same
2372// time as starting an ICE restart. When bundling is in effect in the restart,
2373// the DTLS-SRTP context should be successfully reset.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002374TEST_P(PeerConnectionIntegrationTest, BundlingEnabledWhileIceRestartOccurs) {
deadbeef1dcb1642017-03-29 21:08:16 -07002375 ASSERT_TRUE(CreatePeerConnectionWrappers());
2376 ConnectFakeSignaling();
2377
Steve Anton15324772018-01-16 10:26:49 -08002378 caller()->AddAudioVideoTracks();
2379 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002380 // Remove the bundle group from the SDP received by the callee.
2381 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2382 desc->RemoveGroupByName("BUNDLE");
2383 });
2384 caller()->CreateAndSetAndSignalOffer();
2385 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002386 {
2387 MediaExpectations media_expectations;
2388 media_expectations.ExpectBidirectionalAudioAndVideo();
2389 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2390 }
deadbeef1dcb1642017-03-29 21:08:16 -07002391 // Now stop removing the BUNDLE group, and trigger an ICE restart.
2392 callee()->SetReceivedSdpMunger(nullptr);
2393 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
2394 caller()->CreateAndSetAndSignalOffer();
2395 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2396
2397 // Expect additional frames to be received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002398 {
2399 MediaExpectations media_expectations;
2400 media_expectations.ExpectBidirectionalAudioAndVideo();
2401 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2402 }
deadbeef1dcb1642017-03-29 21:08:16 -07002403}
2404
2405// Test CVO (Coordination of Video Orientation). If a video source is rotated
2406// and both peers support the CVO RTP header extension, the actual video frames
2407// don't need to be encoded in different resolutions, since the rotation is
2408// communicated through the RTP header extension.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002409TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002410 ASSERT_TRUE(CreatePeerConnectionWrappers());
2411 ConnectFakeSignaling();
2412 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002413 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002414 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002415 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002416 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2417
2418 // Wait for video frames to be received by both sides.
2419 caller()->CreateAndSetAndSignalOffer();
2420 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2421 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2422 callee()->min_video_frames_received_per_track() > 0,
2423 kMaxWaitForFramesMs);
2424
2425 // Ensure that the aspect ratio is unmodified.
2426 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2427 // not just assumed.
2428 EXPECT_EQ(4.0 / 3, caller()->local_rendered_aspect_ratio());
2429 EXPECT_EQ(4.0 / 3, caller()->rendered_aspect_ratio());
2430 EXPECT_EQ(4.0 / 3, callee()->local_rendered_aspect_ratio());
2431 EXPECT_EQ(4.0 / 3, callee()->rendered_aspect_ratio());
2432 // Ensure that the CVO bits were surfaced to the renderer.
2433 EXPECT_EQ(webrtc::kVideoRotation_270, caller()->rendered_rotation());
2434 EXPECT_EQ(webrtc::kVideoRotation_90, callee()->rendered_rotation());
2435}
2436
2437// Test that when the CVO extension isn't supported, video is rotated the
2438// old-fashioned way, by encoding rotated frames.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002439TEST_P(PeerConnectionIntegrationTest, RotatedVideoWithoutCVOExtension) {
deadbeef1dcb1642017-03-29 21:08:16 -07002440 ASSERT_TRUE(CreatePeerConnectionWrappers());
2441 ConnectFakeSignaling();
2442 // Add rotated video tracks.
Steve Anton15324772018-01-16 10:26:49 -08002443 caller()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002444 caller()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_90));
Steve Anton15324772018-01-16 10:26:49 -08002445 callee()->AddTrack(
deadbeef1dcb1642017-03-29 21:08:16 -07002446 callee()->CreateLocalVideoTrackWithRotation(webrtc::kVideoRotation_270));
2447
2448 // Remove the CVO extension from the offered SDP.
2449 callee()->SetReceivedSdpMunger([](cricket::SessionDescription* desc) {
2450 cricket::VideoContentDescription* video =
2451 GetFirstVideoContentDescription(desc);
2452 video->ClearRtpHeaderExtensions();
2453 });
2454 // Wait for video frames to be received by both sides.
2455 caller()->CreateAndSetAndSignalOffer();
2456 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2457 ASSERT_TRUE_WAIT(caller()->min_video_frames_received_per_track() > 0 &&
2458 callee()->min_video_frames_received_per_track() > 0,
2459 kMaxWaitForFramesMs);
2460
2461 // Expect that the aspect ratio is inversed to account for the 90/270 degree
2462 // rotation.
2463 // TODO(deadbeef): Where does 4:3 come from? Should be explicit in the test,
2464 // not just assumed.
2465 EXPECT_EQ(3.0 / 4, caller()->local_rendered_aspect_ratio());
2466 EXPECT_EQ(3.0 / 4, caller()->rendered_aspect_ratio());
2467 EXPECT_EQ(3.0 / 4, callee()->local_rendered_aspect_ratio());
2468 EXPECT_EQ(3.0 / 4, callee()->rendered_aspect_ratio());
2469 // Expect that each endpoint is unaware of the rotation of the other endpoint.
2470 EXPECT_EQ(webrtc::kVideoRotation_0, caller()->rendered_rotation());
2471 EXPECT_EQ(webrtc::kVideoRotation_0, callee()->rendered_rotation());
2472}
2473
deadbeef1dcb1642017-03-29 21:08:16 -07002474// Test that if the answerer rejects the audio m= section, no audio is sent or
2475// received, but video still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002476TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002477 ASSERT_TRUE(CreatePeerConnectionWrappers());
2478 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002479 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002480 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2481 // Only add video track for callee, and set offer_to_receive_audio to 0, so
2482 // it will reject the audio m= section completely.
2483 PeerConnectionInterface::RTCOfferAnswerOptions options;
2484 options.offer_to_receive_audio = 0;
2485 callee()->SetOfferAnswerOptions(options);
2486 } else {
2487 // Stopping the audio RtpTransceiver will cause the media section to be
2488 // rejected in the answer.
2489 callee()->SetRemoteOfferHandler([this] {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002490 callee()
2491 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO)
2492 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002493 });
2494 }
Steve Anton15324772018-01-16 10:26:49 -08002495 callee()->AddTrack(callee()->CreateLocalVideoTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002496 // Do offer/answer and wait for successful end-to-end video frames.
2497 caller()->CreateAndSetAndSignalOffer();
2498 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002499 MediaExpectations media_expectations;
2500 media_expectations.ExpectBidirectionalVideo();
2501 media_expectations.ExpectNoAudio();
2502 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2503
deadbeef1dcb1642017-03-29 21:08:16 -07002504 // Sanity check that the callee's description has a rejected audio section.
2505 ASSERT_NE(nullptr, callee()->pc()->local_description());
2506 const ContentInfo* callee_audio_content =
2507 GetFirstAudioContent(callee()->pc()->local_description()->description());
2508 ASSERT_NE(nullptr, callee_audio_content);
2509 EXPECT_TRUE(callee_audio_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002510 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002511 // The caller's transceiver should have stopped after receiving the answer,
2512 // and thus no longer listed in transceivers.
2513 EXPECT_EQ(nullptr,
2514 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_AUDIO));
Seth Hampson2f0d7022018-02-20 11:54:42 -08002515 }
deadbeef1dcb1642017-03-29 21:08:16 -07002516}
2517
2518// Test that if the answerer rejects the video m= section, no video is sent or
2519// received, but audio still can be.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002520TEST_P(PeerConnectionIntegrationTest, AnswererRejectsVideoSection) {
deadbeef1dcb1642017-03-29 21:08:16 -07002521 ASSERT_TRUE(CreatePeerConnectionWrappers());
2522 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002523 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002524 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2525 // Only add audio track for callee, and set offer_to_receive_video to 0, so
2526 // it will reject the video m= section completely.
2527 PeerConnectionInterface::RTCOfferAnswerOptions options;
2528 options.offer_to_receive_video = 0;
2529 callee()->SetOfferAnswerOptions(options);
2530 } else {
2531 // Stopping the video RtpTransceiver will cause the media section to be
2532 // rejected in the answer.
2533 callee()->SetRemoteOfferHandler([this] {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002534 callee()
2535 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2536 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002537 });
2538 }
Steve Anton15324772018-01-16 10:26:49 -08002539 callee()->AddTrack(callee()->CreateLocalAudioTrack());
deadbeef1dcb1642017-03-29 21:08:16 -07002540 // Do offer/answer and wait for successful end-to-end audio frames.
2541 caller()->CreateAndSetAndSignalOffer();
2542 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002543 MediaExpectations media_expectations;
2544 media_expectations.ExpectBidirectionalAudio();
2545 media_expectations.ExpectNoVideo();
2546 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2547
deadbeef1dcb1642017-03-29 21:08:16 -07002548 // Sanity check that the callee's description has a rejected video section.
2549 ASSERT_NE(nullptr, callee()->pc()->local_description());
2550 const ContentInfo* callee_video_content =
2551 GetFirstVideoContent(callee()->pc()->local_description()->description());
2552 ASSERT_NE(nullptr, callee_video_content);
2553 EXPECT_TRUE(callee_video_content->rejected);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002554 if (sdp_semantics_ == SdpSemantics::kUnifiedPlan) {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002555 // The caller's transceiver should have stopped after receiving the answer,
2556 // and thus is no longer present.
2557 EXPECT_EQ(nullptr,
2558 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO));
Seth Hampson2f0d7022018-02-20 11:54:42 -08002559 }
deadbeef1dcb1642017-03-29 21:08:16 -07002560}
2561
2562// Test that if the answerer rejects both audio and video m= sections, nothing
2563// bad happens.
2564// TODO(deadbeef): Test that a data channel still works. Currently this doesn't
2565// test anything but the fact that negotiation succeeds, which doesn't mean
2566// much.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002567TEST_P(PeerConnectionIntegrationTest, AnswererRejectsAudioAndVideoSections) {
deadbeef1dcb1642017-03-29 21:08:16 -07002568 ASSERT_TRUE(CreatePeerConnectionWrappers());
2569 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002570 caller()->AddAudioVideoTracks();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002571 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2572 // Don't give the callee any tracks, and set offer_to_receive_X to 0, so it
2573 // will reject both audio and video m= sections.
2574 PeerConnectionInterface::RTCOfferAnswerOptions options;
2575 options.offer_to_receive_audio = 0;
2576 options.offer_to_receive_video = 0;
2577 callee()->SetOfferAnswerOptions(options);
2578 } else {
2579 callee()->SetRemoteOfferHandler([this] {
2580 // Stopping all transceivers will cause all media sections to be rejected.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002581 for (const auto& transceiver : callee()->pc()->GetTransceivers()) {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002582 transceiver->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002583 }
2584 });
2585 }
deadbeef1dcb1642017-03-29 21:08:16 -07002586 // Do offer/answer and wait for stable signaling state.
2587 caller()->CreateAndSetAndSignalOffer();
2588 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002589
deadbeef1dcb1642017-03-29 21:08:16 -07002590 // Sanity check that the callee's description has rejected m= sections.
2591 ASSERT_NE(nullptr, callee()->pc()->local_description());
2592 const ContentInfo* callee_audio_content =
2593 GetFirstAudioContent(callee()->pc()->local_description()->description());
2594 ASSERT_NE(nullptr, callee_audio_content);
2595 EXPECT_TRUE(callee_audio_content->rejected);
2596 const ContentInfo* callee_video_content =
2597 GetFirstVideoContent(callee()->pc()->local_description()->description());
2598 ASSERT_NE(nullptr, callee_video_content);
2599 EXPECT_TRUE(callee_video_content->rejected);
2600}
2601
2602// This test sets up an audio and video call between two parties. After the
2603// call runs for a while, the caller sends an updated offer with video being
2604// rejected. Once the re-negotiation is done, the video flow should stop and
2605// the audio flow should continue.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002606TEST_P(PeerConnectionIntegrationTest, VideoRejectedInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07002607 ASSERT_TRUE(CreatePeerConnectionWrappers());
2608 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002609 caller()->AddAudioVideoTracks();
2610 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002611 caller()->CreateAndSetAndSignalOffer();
2612 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002613 {
2614 MediaExpectations media_expectations;
2615 media_expectations.ExpectBidirectionalAudioAndVideo();
2616 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2617 }
deadbeef1dcb1642017-03-29 21:08:16 -07002618 // Renegotiate, rejecting the video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002619 if (sdp_semantics_ == SdpSemantics::kPlanB) {
2620 caller()->SetGeneratedSdpMunger(
2621 [](cricket::SessionDescription* description) {
2622 for (cricket::ContentInfo& content : description->contents()) {
2623 if (cricket::IsVideoContent(&content)) {
2624 content.rejected = true;
2625 }
2626 }
2627 });
2628 } else {
Harald Alvestrand6060df52020-08-11 09:54:02 +02002629 caller()
2630 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
2631 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08002632 }
deadbeef1dcb1642017-03-29 21:08:16 -07002633 caller()->CreateAndSetAndSignalOffer();
2634 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
2635
2636 // Sanity check that the caller's description has a rejected video section.
2637 ASSERT_NE(nullptr, caller()->pc()->local_description());
2638 const ContentInfo* caller_video_content =
2639 GetFirstVideoContent(caller()->pc()->local_description()->description());
2640 ASSERT_NE(nullptr, caller_video_content);
2641 EXPECT_TRUE(caller_video_content->rejected);
deadbeef1dcb1642017-03-29 21:08:16 -07002642 // Wait for some additional audio frames to be received.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002643 {
2644 MediaExpectations media_expectations;
2645 media_expectations.ExpectBidirectionalAudio();
2646 media_expectations.ExpectNoVideo();
2647 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2648 }
deadbeef1dcb1642017-03-29 21:08:16 -07002649}
2650
Taylor Brandstetter60c8dc82018-04-11 15:20:27 -07002651// Do one offer/answer with audio, another that disables it (rejecting the m=
2652// section), and another that re-enables it. Regression test for:
2653// bugs.webrtc.org/6023
2654TEST_F(PeerConnectionIntegrationTestPlanB, EnableAudioAfterRejecting) {
2655 ASSERT_TRUE(CreatePeerConnectionWrappers());
2656 ConnectFakeSignaling();
2657
2658 // Add audio track, do normal offer/answer.
2659 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
2660 caller()->CreateLocalAudioTrack();
2661 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
2662 caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2663 caller()->CreateAndSetAndSignalOffer();
2664 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2665
2666 // Remove audio track, and set offer_to_receive_audio to false to cause the
2667 // m= section to be completely disabled, not just "recvonly".
2668 caller()->pc()->RemoveTrack(sender);
2669 PeerConnectionInterface::RTCOfferAnswerOptions options;
2670 options.offer_to_receive_audio = 0;
2671 caller()->SetOfferAnswerOptions(options);
2672 caller()->CreateAndSetAndSignalOffer();
2673 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2674
2675 // Add the audio track again, expecting negotiation to succeed and frames to
2676 // flow.
2677 sender = caller()->pc()->AddTrack(track, {"stream"}).MoveValue();
2678 options.offer_to_receive_audio = 1;
2679 caller()->SetOfferAnswerOptions(options);
2680 caller()->CreateAndSetAndSignalOffer();
2681 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2682
2683 MediaExpectations media_expectations;
2684 media_expectations.CalleeExpectsSomeAudio();
2685 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2686}
2687
deadbeef1dcb1642017-03-29 21:08:16 -07002688// Basic end-to-end test, but without SSRC/MSID signaling. This functionality
2689// is needed to support legacy endpoints.
2690// TODO(deadbeef): When we support the MID extension and demuxing on MID, also
2691// add a test for an end-to-end test without MID signaling either (basically,
2692// the minimum acceptable SDP).
Seth Hampson2f0d7022018-02-20 11:54:42 -08002693TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithoutSsrcOrMsidSignaling) {
deadbeef1dcb1642017-03-29 21:08:16 -07002694 ASSERT_TRUE(CreatePeerConnectionWrappers());
2695 ConnectFakeSignaling();
2696 // Add audio and video, testing that packets can be demuxed on payload type.
Steve Anton15324772018-01-16 10:26:49 -08002697 caller()->AddAudioVideoTracks();
2698 callee()->AddAudioVideoTracks();
deadbeefd8ad7882017-04-18 16:01:17 -07002699 // Remove SSRCs and MSIDs from the received offer SDP.
2700 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
deadbeef1dcb1642017-03-29 21:08:16 -07002701 caller()->CreateAndSetAndSignalOffer();
2702 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002703 MediaExpectations media_expectations;
2704 media_expectations.ExpectBidirectionalAudioAndVideo();
2705 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002706}
2707
Seth Hampson5897a6e2018-04-03 11:16:33 -07002708// Basic end-to-end test, without SSRC signaling. This means that the track
2709// was created properly and frames are delivered when the MSIDs are communicated
2710// with a=msid lines and no a=ssrc lines.
2711TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2712 EndToEndCallWithoutSsrcSignaling) {
2713 const char kStreamId[] = "streamId";
2714 ASSERT_TRUE(CreatePeerConnectionWrappers());
2715 ConnectFakeSignaling();
2716 // Add just audio tracks.
2717 caller()->AddTrack(caller()->CreateLocalAudioTrack(), {kStreamId});
2718 callee()->AddAudioTrack();
2719
2720 // Remove SSRCs from the received offer SDP.
2721 callee()->SetReceivedSdpMunger(RemoveSsrcsAndKeepMsids);
2722 caller()->CreateAndSetAndSignalOffer();
2723 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2724 MediaExpectations media_expectations;
2725 media_expectations.ExpectBidirectionalAudio();
2726 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2727}
2728
Johannes Kron3e983682020-03-29 22:17:00 +02002729TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2730 EndToEndCallAddReceiveVideoToSendOnlyCall) {
2731 ASSERT_TRUE(CreatePeerConnectionWrappers());
2732 ConnectFakeSignaling();
2733 // Add one-directional video, from caller to callee.
2734 rtc::scoped_refptr<webrtc::VideoTrackInterface> track =
2735 caller()->CreateLocalVideoTrack();
2736
2737 RtpTransceiverInit video_transceiver_init;
2738 video_transceiver_init.stream_ids = {"video1"};
2739 video_transceiver_init.direction = RtpTransceiverDirection::kSendOnly;
2740 auto video_sender =
2741 caller()->pc()->AddTransceiver(track, video_transceiver_init).MoveValue();
2742 caller()->CreateAndSetAndSignalOffer();
2743 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2744
2745 // Add receive direction.
Harald Alvestrand6060df52020-08-11 09:54:02 +02002746 video_sender->SetDirectionWithError(RtpTransceiverDirection::kSendRecv);
Johannes Kron3e983682020-03-29 22:17:00 +02002747
2748 rtc::scoped_refptr<webrtc::VideoTrackInterface> callee_track =
2749 callee()->CreateLocalVideoTrack();
2750
2751 callee()->AddTrack(callee_track);
2752 caller()->CreateAndSetAndSignalOffer();
2753 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2754 // Ensure that video frames are received end-to-end.
2755 MediaExpectations media_expectations;
2756 media_expectations.ExpectBidirectionalVideo();
2757 ASSERT_TRUE(ExpectNewFrames(media_expectations));
2758}
2759
Steve Antondf527fd2018-04-27 15:52:03 -07002760// Tests that video flows between multiple video tracks when SSRCs are not
2761// signaled. This exercises the MID RTP header extension which is needed to
2762// demux the incoming video tracks.
2763TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2764 EndToEndCallWithTwoVideoTracksAndNoSignaledSsrc) {
2765 ASSERT_TRUE(CreatePeerConnectionWrappers());
2766 ConnectFakeSignaling();
2767 caller()->AddVideoTrack();
2768 caller()->AddVideoTrack();
2769 callee()->AddVideoTrack();
2770 callee()->AddVideoTrack();
2771
2772 caller()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2773 callee()->SetReceivedSdpMunger(&RemoveSsrcsAndKeepMsids);
2774 caller()->CreateAndSetAndSignalOffer();
2775 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2776 ASSERT_EQ(2u, caller()->pc()->GetReceivers().size());
2777 ASSERT_EQ(2u, callee()->pc()->GetReceivers().size());
2778
2779 // Expect video to be received in both directions on both tracks.
2780 MediaExpectations media_expectations;
2781 media_expectations.ExpectBidirectionalVideo();
2782 EXPECT_TRUE(ExpectNewFrames(media_expectations));
2783}
2784
Henrik Boström5b147782018-12-04 11:25:05 +01002785TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLinePresent) {
2786 ASSERT_TRUE(CreatePeerConnectionWrappers());
2787 ConnectFakeSignaling();
2788 caller()->AddAudioTrack();
2789 caller()->AddVideoTrack();
2790 caller()->CreateAndSetAndSignalOffer();
2791 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2792 auto callee_receivers = callee()->pc()->GetReceivers();
2793 ASSERT_EQ(2u, callee_receivers.size());
2794 EXPECT_TRUE(callee_receivers[0]->stream_ids().empty());
2795 EXPECT_TRUE(callee_receivers[1]->stream_ids().empty());
2796}
2797
2798TEST_F(PeerConnectionIntegrationTestUnifiedPlan, NoStreamsMsidLineMissing) {
2799 ASSERT_TRUE(CreatePeerConnectionWrappers());
2800 ConnectFakeSignaling();
2801 caller()->AddAudioTrack();
2802 caller()->AddVideoTrack();
2803 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
2804 caller()->CreateAndSetAndSignalOffer();
2805 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2806 auto callee_receivers = callee()->pc()->GetReceivers();
2807 ASSERT_EQ(2u, callee_receivers.size());
2808 ASSERT_EQ(1u, callee_receivers[0]->stream_ids().size());
2809 ASSERT_EQ(1u, callee_receivers[1]->stream_ids().size());
2810 EXPECT_EQ(callee_receivers[0]->stream_ids()[0],
2811 callee_receivers[1]->stream_ids()[0]);
2812 EXPECT_EQ(callee_receivers[0]->streams()[0],
2813 callee_receivers[1]->streams()[0]);
2814}
2815
deadbeef1dcb1642017-03-29 21:08:16 -07002816// Test that if two video tracks are sent (from caller to callee, in this test),
2817// they're transmitted correctly end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002818TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithTwoVideoTracks) {
deadbeef1dcb1642017-03-29 21:08:16 -07002819 ASSERT_TRUE(CreatePeerConnectionWrappers());
2820 ConnectFakeSignaling();
2821 // Add one audio/video stream, and one video-only stream.
Steve Anton15324772018-01-16 10:26:49 -08002822 caller()->AddAudioVideoTracks();
2823 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002824 caller()->CreateAndSetAndSignalOffer();
2825 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton15324772018-01-16 10:26:49 -08002826 ASSERT_EQ(3u, callee()->pc()->GetReceivers().size());
Seth Hampson2f0d7022018-02-20 11:54:42 -08002827
2828 MediaExpectations media_expectations;
2829 media_expectations.CalleeExpectsSomeAudioAndVideo();
2830 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002831}
2832
2833static void MakeSpecCompliantMaxBundleOffer(cricket::SessionDescription* desc) {
2834 bool first = true;
2835 for (cricket::ContentInfo& content : desc->contents()) {
2836 if (first) {
2837 first = false;
2838 continue;
2839 }
2840 content.bundle_only = true;
2841 }
2842 first = true;
2843 for (cricket::TransportInfo& transport : desc->transport_infos()) {
2844 if (first) {
2845 first = false;
2846 continue;
2847 }
2848 transport.description.ice_ufrag.clear();
2849 transport.description.ice_pwd.clear();
2850 transport.description.connection_role = cricket::CONNECTIONROLE_NONE;
2851 transport.description.identity_fingerprint.reset(nullptr);
2852 }
2853}
2854
2855// Test that if applying a true "max bundle" offer, which uses ports of 0,
2856// "a=bundle-only", omitting "a=fingerprint", "a=setup", "a=ice-ufrag" and
2857// "a=ice-pwd" for all but the audio "m=" section, negotiation still completes
2858// successfully and media flows.
2859// TODO(deadbeef): Update this test to also omit "a=rtcp-mux", once that works.
2860// TODO(deadbeef): Won't need this test once we start generating actual
2861// standards-compliant SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002862TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07002863 EndToEndCallWithSpecCompliantMaxBundleOffer) {
2864 ASSERT_TRUE(CreatePeerConnectionWrappers());
2865 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002866 caller()->AddAudioVideoTracks();
2867 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002868 // Do the equivalent of setting the port to 0, adding a=bundle-only, and
2869 // removing a=ice-ufrag, a=ice-pwd, a=fingerprint and a=setup from all
2870 // but the first m= section.
2871 callee()->SetReceivedSdpMunger(MakeSpecCompliantMaxBundleOffer);
2872 caller()->CreateAndSetAndSignalOffer();
2873 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002874 MediaExpectations media_expectations;
2875 media_expectations.ExpectBidirectionalAudioAndVideo();
2876 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002877}
2878
2879// Test that we can receive the audio output level from a remote audio track.
2880// TODO(deadbeef): Use a fake audio source and verify that the output level is
2881// exactly what the source on the other side was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002882TEST_P(PeerConnectionIntegrationTest, GetAudioOutputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002883 ASSERT_TRUE(CreatePeerConnectionWrappers());
2884 ConnectFakeSignaling();
2885 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002886 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002887 caller()->CreateAndSetAndSignalOffer();
2888 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2889
2890 // Get the audio output level stats. Note that the level is not available
2891 // until an RTCP packet has been received.
deadbeefd8ad7882017-04-18 16:01:17 -07002892 EXPECT_TRUE_WAIT(callee()->OldGetStats()->AudioOutputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002893 kMaxWaitForFramesMs);
2894}
2895
2896// Test that an audio input level is reported.
2897// TODO(deadbeef): Use a fake audio source and verify that the input level is
2898// exactly what the source was configured with.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002899TEST_P(PeerConnectionIntegrationTest, GetAudioInputLevelStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002900 ASSERT_TRUE(CreatePeerConnectionWrappers());
2901 ConnectFakeSignaling();
2902 // Just add an audio track.
Steve Anton15324772018-01-16 10:26:49 -08002903 caller()->AddAudioTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07002904 caller()->CreateAndSetAndSignalOffer();
2905 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2906
2907 // Get the audio input level stats. The level should be available very
2908 // soon after the test starts.
deadbeefd8ad7882017-04-18 16:01:17 -07002909 EXPECT_TRUE_WAIT(caller()->OldGetStats()->AudioInputLevel() > 0,
deadbeef1dcb1642017-03-29 21:08:16 -07002910 kMaxWaitForStatsMs);
2911}
2912
2913// Test that we can get incoming byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002914TEST_P(PeerConnectionIntegrationTest, GetBytesReceivedStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002915 ASSERT_TRUE(CreatePeerConnectionWrappers());
2916 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002917 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07002918 // Do offer/answer, wait for the callee to receive some frames.
2919 caller()->CreateAndSetAndSignalOffer();
2920 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002921
2922 MediaExpectations media_expectations;
2923 media_expectations.CalleeExpectsSomeAudioAndVideo();
2924 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002925
2926 // Get a handle to the remote tracks created, so they can be used as GetStats
2927 // filters.
Mirko Bonadei739baf02019-01-27 17:29:42 +01002928 for (const auto& receiver : callee()->pc()->GetReceivers()) {
Steve Anton15324772018-01-16 10:26:49 -08002929 // We received frames, so we definitely should have nonzero "received bytes"
2930 // stats at this point.
2931 EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
2932 0);
2933 }
deadbeef1dcb1642017-03-29 21:08:16 -07002934}
2935
2936// Test that we can get outgoing byte counts from both audio and video tracks.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002937TEST_P(PeerConnectionIntegrationTest, GetBytesSentStatsWithOldStatsApi) {
deadbeef1dcb1642017-03-29 21:08:16 -07002938 ASSERT_TRUE(CreatePeerConnectionWrappers());
2939 ConnectFakeSignaling();
2940 auto audio_track = caller()->CreateLocalAudioTrack();
2941 auto video_track = caller()->CreateLocalVideoTrack();
Steve Anton15324772018-01-16 10:26:49 -08002942 caller()->AddTrack(audio_track);
2943 caller()->AddTrack(video_track);
deadbeef1dcb1642017-03-29 21:08:16 -07002944 // Do offer/answer, wait for the callee to receive some frames.
2945 caller()->CreateAndSetAndSignalOffer();
2946 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08002947 MediaExpectations media_expectations;
2948 media_expectations.CalleeExpectsSomeAudioAndVideo();
2949 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07002950
2951 // The callee received frames, so we definitely should have nonzero "sent
2952 // bytes" stats at this point.
deadbeefd8ad7882017-04-18 16:01:17 -07002953 EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
2954 EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
2955}
2956
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002957// Test that we can get capture start ntp time.
Seth Hampson2f0d7022018-02-20 11:54:42 -08002958TEST_P(PeerConnectionIntegrationTest, GetCaptureStartNtpTimeWithOldStatsApi) {
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002959 ASSERT_TRUE(CreatePeerConnectionWrappers());
2960 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08002961 caller()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002962
Steve Anton15324772018-01-16 10:26:49 -08002963 callee()->AddAudioTrack();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002964
2965 // Do offer/answer, wait for the callee to receive some frames.
2966 caller()->CreateAndSetAndSignalOffer();
2967 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2968
2969 // Get the remote audio track created on the receiver, so they can be used as
2970 // GetStats filters.
Steve Antonfc853712018-03-01 13:48:58 -08002971 auto receivers = callee()->pc()->GetReceivers();
2972 ASSERT_EQ(1u, receivers.size());
2973 auto remote_audio_track = receivers[0]->track();
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002974
2975 // Get the audio output level stats. Note that the level is not available
2976 // until an RTCP packet has been received.
Zhi Huange830e682018-03-30 10:48:35 -07002977 EXPECT_TRUE_WAIT(
2978 callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
2979 0,
2980 2 * kMaxWaitForFramesMs);
Fredrik Solenberg73276ad2017-09-14 14:46:47 +02002981}
2982
Steve Antona41959e2018-11-28 11:15:33 -08002983// Test that the track ID is associated with all local and remote SSRC stats
2984// using the old GetStats() and more than 1 audio and more than 1 video track.
2985// This is a regression test for crbug.com/906988
2986TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
2987 OldGetStatsAssociatesTrackIdForManyMediaSections) {
2988 ASSERT_TRUE(CreatePeerConnectionWrappers());
2989 ConnectFakeSignaling();
2990 auto audio_sender_1 = caller()->AddAudioTrack();
2991 auto video_sender_1 = caller()->AddVideoTrack();
2992 auto audio_sender_2 = caller()->AddAudioTrack();
2993 auto video_sender_2 = caller()->AddVideoTrack();
2994 caller()->CreateAndSetAndSignalOffer();
2995 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
2996
2997 MediaExpectations media_expectations;
2998 media_expectations.CalleeExpectsSomeAudioAndVideo();
2999 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
3000
3001 std::vector<std::string> track_ids = {
3002 audio_sender_1->track()->id(), video_sender_1->track()->id(),
3003 audio_sender_2->track()->id(), video_sender_2->track()->id()};
3004
3005 auto caller_stats = caller()->OldGetStats();
3006 EXPECT_THAT(caller_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
3007 auto callee_stats = callee()->OldGetStats();
3008 EXPECT_THAT(callee_stats->TrackIds(), UnorderedElementsAreArray(track_ids));
3009}
3010
Steve Antonffa6ce42018-11-30 09:26:08 -08003011// Test that the new GetStats() returns stats for all outgoing/incoming streams
3012// with the correct track IDs if there are more than one audio and more than one
3013// video senders/receivers.
3014TEST_P(PeerConnectionIntegrationTest, NewGetStatsManyAudioAndManyVideoStreams) {
3015 ASSERT_TRUE(CreatePeerConnectionWrappers());
3016 ConnectFakeSignaling();
3017 auto audio_sender_1 = caller()->AddAudioTrack();
3018 auto video_sender_1 = caller()->AddVideoTrack();
3019 auto audio_sender_2 = caller()->AddAudioTrack();
3020 auto video_sender_2 = caller()->AddVideoTrack();
3021 caller()->CreateAndSetAndSignalOffer();
3022 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3023
3024 MediaExpectations media_expectations;
3025 media_expectations.CalleeExpectsSomeAudioAndVideo();
3026 ASSERT_TRUE_WAIT(ExpectNewFrames(media_expectations), kDefaultTimeout);
3027
3028 std::vector<std::string> track_ids = {
3029 audio_sender_1->track()->id(), video_sender_1->track()->id(),
3030 audio_sender_2->track()->id(), video_sender_2->track()->id()};
3031
3032 rtc::scoped_refptr<const webrtc::RTCStatsReport> caller_report =
3033 caller()->NewGetStats();
3034 ASSERT_TRUE(caller_report);
3035 auto outbound_stream_stats =
3036 caller_report->GetStatsOfType<webrtc::RTCOutboundRTPStreamStats>();
Henrik Boströma0ff50c2020-05-05 15:54:46 +02003037 ASSERT_EQ(outbound_stream_stats.size(), 4u);
Steve Antonffa6ce42018-11-30 09:26:08 -08003038 std::vector<std::string> outbound_track_ids;
3039 for (const auto& stat : outbound_stream_stats) {
3040 ASSERT_TRUE(stat->bytes_sent.is_defined());
3041 EXPECT_LT(0u, *stat->bytes_sent);
Rasmus Brandt2efae772019-06-27 14:29:34 +02003042 if (*stat->kind == "video") {
3043 ASSERT_TRUE(stat->key_frames_encoded.is_defined());
3044 EXPECT_GT(*stat->key_frames_encoded, 0u);
3045 ASSERT_TRUE(stat->frames_encoded.is_defined());
3046 EXPECT_GE(*stat->frames_encoded, *stat->key_frames_encoded);
3047 }
Steve Antonffa6ce42018-11-30 09:26:08 -08003048 ASSERT_TRUE(stat->track_id.is_defined());
3049 const auto* track_stat =
3050 caller_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
3051 ASSERT_TRUE(track_stat);
3052 outbound_track_ids.push_back(*track_stat->track_identifier);
3053 }
3054 EXPECT_THAT(outbound_track_ids, UnorderedElementsAreArray(track_ids));
3055
3056 rtc::scoped_refptr<const webrtc::RTCStatsReport> callee_report =
3057 callee()->NewGetStats();
3058 ASSERT_TRUE(callee_report);
3059 auto inbound_stream_stats =
3060 callee_report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
3061 ASSERT_EQ(4u, inbound_stream_stats.size());
3062 std::vector<std::string> inbound_track_ids;
3063 for (const auto& stat : inbound_stream_stats) {
3064 ASSERT_TRUE(stat->bytes_received.is_defined());
3065 EXPECT_LT(0u, *stat->bytes_received);
Rasmus Brandt2efae772019-06-27 14:29:34 +02003066 if (*stat->kind == "video") {
3067 ASSERT_TRUE(stat->key_frames_decoded.is_defined());
3068 EXPECT_GT(*stat->key_frames_decoded, 0u);
3069 ASSERT_TRUE(stat->frames_decoded.is_defined());
3070 EXPECT_GE(*stat->frames_decoded, *stat->key_frames_decoded);
3071 }
Steve Antonffa6ce42018-11-30 09:26:08 -08003072 ASSERT_TRUE(stat->track_id.is_defined());
3073 const auto* track_stat =
3074 callee_report->GetAs<webrtc::RTCMediaStreamTrackStats>(*stat->track_id);
3075 ASSERT_TRUE(track_stat);
3076 inbound_track_ids.push_back(*track_stat->track_identifier);
3077 }
3078 EXPECT_THAT(inbound_track_ids, UnorderedElementsAreArray(track_ids));
3079}
3080
3081// Test that we can get stats (using the new stats implementation) for
deadbeefd8ad7882017-04-18 16:01:17 -07003082// unsignaled streams. Meaning when SSRCs/MSIDs aren't signaled explicitly in
3083// SDP.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003084TEST_P(PeerConnectionIntegrationTest,
deadbeefd8ad7882017-04-18 16:01:17 -07003085 GetStatsForUnsignaledStreamWithNewStatsApi) {
3086 ASSERT_TRUE(CreatePeerConnectionWrappers());
3087 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003088 caller()->AddAudioTrack();
deadbeefd8ad7882017-04-18 16:01:17 -07003089 // Remove SSRCs and MSIDs from the received offer SDP.
3090 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3091 caller()->CreateAndSetAndSignalOffer();
3092 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003093 MediaExpectations media_expectations;
3094 media_expectations.CalleeExpectsSomeAudio(1);
3095 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefd8ad7882017-04-18 16:01:17 -07003096
3097 // We received a frame, so we should have nonzero "bytes received" stats for
3098 // the unsignaled stream, if stats are working for it.
3099 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
3100 callee()->NewGetStats();
3101 ASSERT_NE(nullptr, report);
3102 auto inbound_stream_stats =
3103 report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
3104 ASSERT_EQ(1U, inbound_stream_stats.size());
3105 ASSERT_TRUE(inbound_stream_stats[0]->bytes_received.is_defined());
3106 ASSERT_GT(*inbound_stream_stats[0]->bytes_received, 0U);
zhihuangf8164932017-05-19 13:09:47 -07003107 ASSERT_TRUE(inbound_stream_stats[0]->track_id.is_defined());
3108}
3109
Taylor Brandstettera4653442018-06-19 09:44:26 -07003110// Same as above but for the legacy stats implementation.
3111TEST_P(PeerConnectionIntegrationTest,
3112 GetStatsForUnsignaledStreamWithOldStatsApi) {
3113 ASSERT_TRUE(CreatePeerConnectionWrappers());
3114 ConnectFakeSignaling();
3115 caller()->AddAudioTrack();
3116 // Remove SSRCs and MSIDs from the received offer SDP.
3117 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3118 caller()->CreateAndSetAndSignalOffer();
3119 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3120
3121 // Note that, since the old stats implementation associates SSRCs with tracks
3122 // using SDP, when SSRCs aren't signaled in SDP these stats won't have an
3123 // associated track ID. So we can't use the track "selector" argument.
3124 //
3125 // Also, we use "EXPECT_TRUE_WAIT" because the stats collector may decide to
3126 // return cached stats if not enough time has passed since the last update.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003127 EXPECT_TRUE_WAIT(callee()->OldGetStats()->BytesReceived() > 0,
Taylor Brandstettera4653442018-06-19 09:44:26 -07003128 kDefaultTimeout);
3129}
3130
zhihuangf8164932017-05-19 13:09:47 -07003131// Test that we can successfully get the media related stats (audio level
3132// etc.) for the unsignaled stream.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003133TEST_P(PeerConnectionIntegrationTest,
zhihuangf8164932017-05-19 13:09:47 -07003134 GetMediaStatsForUnsignaledStreamWithNewStatsApi) {
3135 ASSERT_TRUE(CreatePeerConnectionWrappers());
3136 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003137 caller()->AddAudioVideoTracks();
zhihuangf8164932017-05-19 13:09:47 -07003138 // Remove SSRCs and MSIDs from the received offer SDP.
3139 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3140 caller()->CreateAndSetAndSignalOffer();
3141 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003142 MediaExpectations media_expectations;
3143 media_expectations.CalleeExpectsSomeAudio(1);
3144 media_expectations.CalleeExpectsSomeVideo(1);
3145 ASSERT_TRUE(ExpectNewFrames(media_expectations));
zhihuangf8164932017-05-19 13:09:47 -07003146
3147 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
3148 callee()->NewGetStats();
3149 ASSERT_NE(nullptr, report);
3150
3151 auto media_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3152 auto audio_index = FindFirstMediaStatsIndexByKind("audio", media_stats);
3153 ASSERT_GE(audio_index, 0);
3154 EXPECT_TRUE(media_stats[audio_index]->audio_level.is_defined());
deadbeef1dcb1642017-03-29 21:08:16 -07003155}
3156
deadbeef4e2deab2017-09-20 13:56:21 -07003157// Helper for test below.
3158void ModifySsrcs(cricket::SessionDescription* desc) {
3159 for (ContentInfo& content : desc->contents()) {
Steve Antondf527fd2018-04-27 15:52:03 -07003160 for (StreamParams& stream :
Steve Antonb1c1de12017-12-21 15:14:30 -08003161 content.media_description()->mutable_streams()) {
deadbeef4e2deab2017-09-20 13:56:21 -07003162 for (uint32_t& ssrc : stream.ssrcs) {
3163 ssrc = rtc::CreateRandomId();
3164 }
3165 }
3166 }
3167}
3168
3169// Test that the "RTCMediaSteamTrackStats" object is updated correctly when
3170// SSRCs are unsignaled, and the SSRC of the received (audio) stream changes.
3171// This should result in two "RTCInboundRTPStreamStats", but only one
3172// "RTCMediaStreamTrackStats", whose counters go up continuously rather than
3173// being reset to 0 once the SSRC change occurs.
3174//
3175// Regression test for this bug:
3176// https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
3177//
3178// The bug causes the track stats to only represent one of the two streams:
3179// whichever one has the higher SSRC. So with this bug, there was a 50% chance
3180// that the track stat counters would reset to 0 when the new stream is
3181// received, and a 50% chance that they'll stop updating (while
3182// "concealed_samples" continues increasing, due to silence being generated for
3183// the inactive stream).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003184TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08003185 TrackStatsUpdatedCorrectlyWhenUnsignaledSsrcChanges) {
deadbeef4e2deab2017-09-20 13:56:21 -07003186 ASSERT_TRUE(CreatePeerConnectionWrappers());
3187 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003188 caller()->AddAudioTrack();
deadbeef4e2deab2017-09-20 13:56:21 -07003189 // Remove SSRCs and MSIDs from the received offer SDP, simulating an endpoint
3190 // that doesn't signal SSRCs (from the callee's perspective).
3191 callee()->SetReceivedSdpMunger(RemoveSsrcsAndMsids);
3192 caller()->CreateAndSetAndSignalOffer();
3193 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3194 // Wait for 50 audio frames (500ms of audio) to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003195 {
3196 MediaExpectations media_expectations;
3197 media_expectations.CalleeExpectsSomeAudio(50);
3198 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3199 }
deadbeef4e2deab2017-09-20 13:56:21 -07003200 // Some audio frames were received, so we should have nonzero "samples
3201 // received" for the track.
3202 rtc::scoped_refptr<const webrtc::RTCStatsReport> report =
3203 callee()->NewGetStats();
3204 ASSERT_NE(nullptr, report);
3205 auto track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3206 ASSERT_EQ(1U, track_stats.size());
3207 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
3208 ASSERT_GT(*track_stats[0]->total_samples_received, 0U);
3209 // uint64_t prev_samples_received = *track_stats[0]->total_samples_received;
3210
3211 // Create a new offer and munge it to cause the caller to use a new SSRC.
3212 caller()->SetGeneratedSdpMunger(ModifySsrcs);
3213 caller()->CreateAndSetAndSignalOffer();
3214 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3215 // Wait for 25 more audio frames (250ms of audio) to be received, from the new
3216 // SSRC.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003217 {
3218 MediaExpectations media_expectations;
3219 media_expectations.CalleeExpectsSomeAudio(25);
3220 ASSERT_TRUE(ExpectNewFrames(media_expectations));
3221 }
deadbeef4e2deab2017-09-20 13:56:21 -07003222
3223 report = callee()->NewGetStats();
3224 ASSERT_NE(nullptr, report);
3225 track_stats = report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
3226 ASSERT_EQ(1U, track_stats.size());
3227 ASSERT_TRUE(track_stats[0]->total_samples_received.is_defined());
3228 // The "total samples received" stat should only be greater than it was
3229 // before.
3230 // TODO(deadbeef): Uncomment this assertion once the bug is completely fixed.
3231 // Right now, the new SSRC will cause the counters to reset to 0.
3232 // EXPECT_GT(*track_stats[0]->total_samples_received, prev_samples_received);
3233
3234 // Additionally, the percentage of concealed samples (samples generated to
Steve Anton83119dd2017-11-10 16:19:52 -08003235 // conceal packet loss) should be less than 50%. If it's greater, that's a
deadbeef4e2deab2017-09-20 13:56:21 -07003236 // good sign that we're seeing stats from the old stream that's no longer
3237 // receiving packets, and is generating concealed samples of silence.
Steve Anton83119dd2017-11-10 16:19:52 -08003238 constexpr double kAcceptableConcealedSamplesPercentage = 0.50;
deadbeef4e2deab2017-09-20 13:56:21 -07003239 ASSERT_TRUE(track_stats[0]->concealed_samples.is_defined());
3240 EXPECT_LT(*track_stats[0]->concealed_samples,
3241 *track_stats[0]->total_samples_received *
3242 kAcceptableConcealedSamplesPercentage);
3243
3244 // Also ensure that we have two "RTCInboundRTPStreamStats" as expected, as a
3245 // sanity check that the SSRC really changed.
3246 // TODO(deadbeef): This isn't working right now, because we're not returning
3247 // *any* stats for the inactive stream. Uncomment when the bug is completely
3248 // fixed.
3249 // auto inbound_stream_stats =
3250 // report->GetStatsOfType<webrtc::RTCInboundRTPStreamStats>();
3251 // ASSERT_EQ(2U, inbound_stream_stats.size());
3252}
3253
deadbeef1dcb1642017-03-29 21:08:16 -07003254// Test that DTLS 1.0 is used if both sides only support DTLS 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003255TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07003256 PeerConnectionFactory::Options dtls_10_options;
3257 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3258 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
3259 dtls_10_options));
3260 ConnectFakeSignaling();
3261 // Do normal offer/answer and wait for some frames to be received in each
3262 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003263 caller()->AddAudioVideoTracks();
3264 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003265 caller()->CreateAndSetAndSignalOffer();
3266 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003267 MediaExpectations media_expectations;
3268 media_expectations.ExpectBidirectionalAudioAndVideo();
3269 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003270}
3271
3272// Test getting cipher stats and UMA metrics when DTLS 1.0 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003273TEST_P(PeerConnectionIntegrationTest, Dtls10CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07003274 PeerConnectionFactory::Options dtls_10_options;
3275 dtls_10_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3276 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_10_options,
3277 dtls_10_options));
3278 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003279 caller()->AddAudioVideoTracks();
3280 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003281 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003282 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003283 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07003284 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07003285 kDefaultTimeout);
3286 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07003287 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003288 // TODO(bugs.webrtc.org/9456): Fix it.
Ying Wangef3998f2019-12-09 13:06:53 +01003289 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
3290 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
3291 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07003292}
3293
3294// Test getting cipher stats and UMA metrics when DTLS 1.2 is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003295TEST_P(PeerConnectionIntegrationTest, Dtls12CipherStatsAndUmaMetrics) {
deadbeef1dcb1642017-03-29 21:08:16 -07003296 PeerConnectionFactory::Options dtls_12_options;
3297 dtls_12_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3298 ASSERT_TRUE(CreatePeerConnectionWrappersWithOptions(dtls_12_options,
3299 dtls_12_options));
3300 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08003301 caller()->AddAudioVideoTracks();
3302 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003303 caller()->CreateAndSetAndSignalOffer();
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003304 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003305 EXPECT_TRUE_WAIT(rtc::SSLStreamAdapter::IsAcceptableCipher(
deadbeefd8ad7882017-04-18 16:01:17 -07003306 caller()->OldGetStats()->DtlsCipher(), rtc::KT_DEFAULT),
deadbeef1dcb1642017-03-29 21:08:16 -07003307 kDefaultTimeout);
3308 EXPECT_EQ_WAIT(rtc::SrtpCryptoSuiteToName(kDefaultSrtpCryptoSuite),
deadbeefd8ad7882017-04-18 16:01:17 -07003309 caller()->OldGetStats()->SrtpCipher(), kDefaultTimeout);
Qingsi Wang7fc821d2018-07-12 12:54:53 -07003310 // TODO(bugs.webrtc.org/9456): Fix it.
Ying Wangef3998f2019-12-09 13:06:53 +01003311 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
3312 "WebRTC.PeerConnection.SrtpCryptoSuite.Audio",
3313 kDefaultSrtpCryptoSuite));
deadbeef1dcb1642017-03-29 21:08:16 -07003314}
3315
3316// Test that DTLS 1.0 can be used if the caller supports DTLS 1.2 and the
3317// callee only supports 1.0.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003318TEST_P(PeerConnectionIntegrationTest, CallerDtls12ToCalleeDtls10) {
deadbeef1dcb1642017-03-29 21:08:16 -07003319 PeerConnectionFactory::Options caller_options;
3320 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3321 PeerConnectionFactory::Options callee_options;
3322 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3323 ASSERT_TRUE(
3324 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
3325 ConnectFakeSignaling();
3326 // Do normal offer/answer and wait for some frames to be received in each
3327 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003328 caller()->AddAudioVideoTracks();
3329 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003330 caller()->CreateAndSetAndSignalOffer();
3331 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003332 MediaExpectations media_expectations;
3333 media_expectations.ExpectBidirectionalAudioAndVideo();
3334 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003335}
3336
3337// Test that DTLS 1.0 can be used if the caller only supports DTLS 1.0 and the
3338// callee supports 1.2.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003339TEST_P(PeerConnectionIntegrationTest, CallerDtls10ToCalleeDtls12) {
deadbeef1dcb1642017-03-29 21:08:16 -07003340 PeerConnectionFactory::Options caller_options;
3341 caller_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_10;
3342 PeerConnectionFactory::Options callee_options;
3343 callee_options.ssl_max_version = rtc::SSL_PROTOCOL_DTLS_12;
3344 ASSERT_TRUE(
3345 CreatePeerConnectionWrappersWithOptions(caller_options, callee_options));
3346 ConnectFakeSignaling();
3347 // Do normal offer/answer and wait for some frames to be received in each
3348 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003349 caller()->AddAudioVideoTracks();
3350 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003351 caller()->CreateAndSetAndSignalOffer();
3352 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003353 MediaExpectations media_expectations;
3354 media_expectations.ExpectBidirectionalAudioAndVideo();
3355 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003356}
3357
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003358// The three tests below verify that "enable_aes128_sha1_32_crypto_cipher"
3359// works as expected; the cipher should only be used if enabled by both sides.
3360TEST_P(PeerConnectionIntegrationTest,
3361 Aes128Sha1_32_CipherNotUsedWhenOnlyCallerSupported) {
3362 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003363 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003364 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003365 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3366 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003367 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3368 TestNegotiatedCipherSuite(caller_options, callee_options,
3369 expected_cipher_suite);
3370}
3371
3372TEST_P(PeerConnectionIntegrationTest,
3373 Aes128Sha1_32_CipherNotUsedWhenOnlyCalleeSupported) {
3374 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003375 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher =
3376 false;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003377 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003378 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003379 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_80;
3380 TestNegotiatedCipherSuite(caller_options, callee_options,
3381 expected_cipher_suite);
3382}
3383
3384TEST_P(PeerConnectionIntegrationTest, Aes128Sha1_32_CipherUsedWhenSupported) {
3385 PeerConnectionFactory::Options caller_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003386 caller_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003387 PeerConnectionFactory::Options callee_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003388 callee_options.crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher = true;
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -07003389 int expected_cipher_suite = rtc::SRTP_AES128_CM_SHA1_32;
3390 TestNegotiatedCipherSuite(caller_options, callee_options,
3391 expected_cipher_suite);
3392}
3393
deadbeef1dcb1642017-03-29 21:08:16 -07003394// Test that a non-GCM cipher is used if both sides only support non-GCM.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003395TEST_P(PeerConnectionIntegrationTest, NonGcmCipherUsedWhenGcmNotSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003396 bool local_gcm_enabled = false;
3397 bool remote_gcm_enabled = false;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003398 bool aes_ctr_enabled = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003399 int expected_cipher_suite = kDefaultSrtpCryptoSuite;
3400 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003401 aes_ctr_enabled, expected_cipher_suite);
deadbeef1dcb1642017-03-29 21:08:16 -07003402}
3403
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003404// Test that a GCM cipher is used if both ends support it and non-GCM is
3405// disabled.
3406TEST_P(PeerConnectionIntegrationTest, GcmCipherUsedWhenOnlyGcmSupported) {
deadbeef1dcb1642017-03-29 21:08:16 -07003407 bool local_gcm_enabled = true;
3408 bool remote_gcm_enabled = true;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003409 bool aes_ctr_enabled = false;
deadbeef1dcb1642017-03-29 21:08:16 -07003410 int expected_cipher_suite = kDefaultSrtpCryptoSuiteGcm;
3411 TestGcmNegotiationUsesCipherSuite(local_gcm_enabled, remote_gcm_enabled,
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003412 aes_ctr_enabled, expected_cipher_suite);
deadbeef1dcb1642017-03-29 21:08:16 -07003413}
3414
deadbeef7914b8c2017-04-21 03:23:33 -07003415// Verify that media can be transmitted end-to-end when GCM crypto suites are
3416// enabled. Note that the above tests, such as GcmCipherUsedWhenGcmSupported,
3417// only verify that a GCM cipher is negotiated, and not necessarily that SRTP
3418// works with it.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003419TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithGcmCipher) {
deadbeef7914b8c2017-04-21 03:23:33 -07003420 PeerConnectionFactory::Options gcm_options;
Benjamin Wrighta54daf12018-10-11 15:33:17 -07003421 gcm_options.crypto_options.srtp.enable_gcm_crypto_suites = true;
Philipp Hancke2ebbff82019-10-26 06:12:55 +02003422 gcm_options.crypto_options.srtp.enable_aes128_sha1_80_crypto_cipher = false;
deadbeef7914b8c2017-04-21 03:23:33 -07003423 ASSERT_TRUE(
3424 CreatePeerConnectionWrappersWithOptions(gcm_options, gcm_options));
3425 ConnectFakeSignaling();
3426 // Do normal offer/answer and wait for some frames to be received in each
3427 // direction.
Steve Anton15324772018-01-16 10:26:49 -08003428 caller()->AddAudioVideoTracks();
3429 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003430 caller()->CreateAndSetAndSignalOffer();
3431 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003432 MediaExpectations media_expectations;
3433 media_expectations.ExpectBidirectionalAudioAndVideo();
3434 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003435}
3436
deadbeef1dcb1642017-03-29 21:08:16 -07003437// This test sets up a call between two parties with audio, video and an RTP
3438// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003439TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithRtpDataChannel) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003440 PeerConnectionInterface::RTCConfiguration rtc_config;
3441 rtc_config.enable_rtp_data_channel = true;
3442 rtc_config.enable_dtls_srtp = false;
3443 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003444 ConnectFakeSignaling();
3445 // Expect that data channel created on caller side will show up for callee as
3446 // well.
3447 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003448 caller()->AddAudioVideoTracks();
3449 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003450 caller()->CreateAndSetAndSignalOffer();
3451 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3452 // Ensure the existence of the RTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003453 MediaExpectations media_expectations;
3454 media_expectations.ExpectBidirectionalAudioAndVideo();
3455 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003456 ASSERT_NE(nullptr, caller()->data_channel());
3457 ASSERT_NE(nullptr, callee()->data_channel());
3458 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3459 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3460
3461 // Ensure data can be sent in both directions.
3462 std::string data = "hello world";
3463 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3464 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3465 kDefaultTimeout);
3466 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3467 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3468 kDefaultTimeout);
3469}
3470
Eldar Rellod9ebe012020-03-18 20:41:45 +02003471TEST_P(PeerConnectionIntegrationTest, RtpDataChannelWorksAfterRollback) {
3472 PeerConnectionInterface::RTCConfiguration rtc_config;
3473 rtc_config.enable_rtp_data_channel = true;
3474 rtc_config.enable_dtls_srtp = false;
3475 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
3476 ConnectFakeSignaling();
3477 auto data_channel = caller()->pc()->CreateDataChannel("label_1", nullptr);
3478 ASSERT_TRUE(data_channel.get() != nullptr);
3479 caller()->CreateAndSetAndSignalOffer();
3480 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3481
3482 caller()->CreateDataChannel("label_2", nullptr);
3483 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
3484 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
3485 caller()->pc()->SetLocalDescription(observer,
3486 caller()->CreateOfferAndWait().release());
3487 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
3488 caller()->Rollback();
3489
3490 std::string data = "hello world";
3491 SendRtpDataWithRetries(data_channel, data, 5);
3492 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3493 kDefaultTimeout);
3494}
3495
deadbeef1dcb1642017-03-29 21:08:16 -07003496// Ensure that an RTP data channel is signaled as closed for the caller when
3497// the callee rejects it in a subsequent offer.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003498TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07003499 RtpDataChannelSignaledClosedInCalleeOffer) {
3500 // Same procedure as above test.
Niels Möllerf06f9232018-08-07 12:32:18 +02003501 PeerConnectionInterface::RTCConfiguration rtc_config;
3502 rtc_config.enable_rtp_data_channel = true;
3503 rtc_config.enable_dtls_srtp = false;
3504 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003505 ConnectFakeSignaling();
3506 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003507 caller()->AddAudioVideoTracks();
3508 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003509 caller()->CreateAndSetAndSignalOffer();
3510 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3511 ASSERT_NE(nullptr, caller()->data_channel());
3512 ASSERT_NE(nullptr, callee()->data_channel());
3513 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3514 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3515
3516 // Close the data channel on the callee, and do an updated offer/answer.
3517 callee()->data_channel()->Close();
3518 callee()->CreateAndSetAndSignalOffer();
3519 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3520 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3521 EXPECT_FALSE(callee()->data_observer()->IsOpen());
3522}
3523
3524// Tests that data is buffered in an RTP data channel until an observer is
3525// registered for it.
3526//
3527// NOTE: RTP data channels can receive data before the underlying
3528// transport has detected that a channel is writable and thus data can be
3529// received before the data channel state changes to open. That is hard to test
3530// but the same buffering is expected to be used in that case.
Yves Gerey100fe632020-01-17 19:15:53 +01003531//
3532// Use fake clock and simulated network delay so that we predictably can wait
3533// until an SCTP message has been delivered without "sleep()"ing.
3534TEST_P(PeerConnectionIntegrationTestWithFakeClock,
deadbeef1dcb1642017-03-29 21:08:16 -07003535 DataBufferedUntilRtpDataChannelObserverRegistered) {
deadbeef1dcb1642017-03-29 21:08:16 -07003536 virtual_socket_server()->set_delay_mean(5); // 5 ms per hop.
3537 virtual_socket_server()->UpdateDelayDistribution();
3538
Niels Möllerf06f9232018-08-07 12:32:18 +02003539 PeerConnectionInterface::RTCConfiguration rtc_config;
3540 rtc_config.enable_rtp_data_channel = true;
3541 rtc_config.enable_dtls_srtp = false;
3542 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003543 ConnectFakeSignaling();
3544 caller()->CreateDataChannel();
3545 caller()->CreateAndSetAndSignalOffer();
3546 ASSERT_TRUE(caller()->data_channel() != nullptr);
3547 ASSERT_TRUE_SIMULATED_WAIT(callee()->data_channel() != nullptr,
Yves Gerey100fe632020-01-17 19:15:53 +01003548 kDefaultTimeout, FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003549 ASSERT_TRUE_SIMULATED_WAIT(caller()->data_observer()->IsOpen(),
Yves Gerey100fe632020-01-17 19:15:53 +01003550 kDefaultTimeout, FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003551 ASSERT_EQ_SIMULATED_WAIT(DataChannelInterface::kOpen,
3552 callee()->data_channel()->state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01003553 FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003554
3555 // Unregister the observer which is normally automatically registered.
3556 callee()->data_channel()->UnregisterObserver();
3557 // Send data and advance fake clock until it should have been received.
3558 std::string data = "hello world";
3559 caller()->data_channel()->Send(DataBuffer(data));
Yves Gerey100fe632020-01-17 19:15:53 +01003560 SIMULATED_WAIT(false, 50, FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003561
3562 // Attach data channel and expect data to be received immediately. Note that
3563 // EXPECT_EQ_WAIT is used, such that the simulated clock is not advanced any
3564 // further, but data can be received even if the callback is asynchronous.
3565 MockDataChannelObserver new_observer(callee()->data_channel());
3566 EXPECT_EQ_SIMULATED_WAIT(data, new_observer.last_message(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01003567 FakeClock());
deadbeef1dcb1642017-03-29 21:08:16 -07003568}
3569
3570// This test sets up a call between two parties with audio, video and but only
3571// the caller client supports RTP data channels.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003572TEST_P(PeerConnectionIntegrationTest, RtpDataChannelsRejectedByCallee) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003573 PeerConnectionInterface::RTCConfiguration rtc_config_1;
3574 rtc_config_1.enable_rtp_data_channel = true;
deadbeef1dcb1642017-03-29 21:08:16 -07003575 // Must disable DTLS to make negotiation succeed.
Niels Möllerf06f9232018-08-07 12:32:18 +02003576 rtc_config_1.enable_dtls_srtp = false;
3577 PeerConnectionInterface::RTCConfiguration rtc_config_2;
3578 rtc_config_2.enable_dtls_srtp = false;
3579 rtc_config_2.enable_dtls_srtp = false;
3580 ASSERT_TRUE(
3581 CreatePeerConnectionWrappersWithConfig(rtc_config_1, rtc_config_2));
deadbeef1dcb1642017-03-29 21:08:16 -07003582 ConnectFakeSignaling();
3583 caller()->CreateDataChannel();
Harald Alvestrandf3736ed2019-04-08 13:09:30 +02003584 ASSERT_TRUE(caller()->data_channel() != nullptr);
Steve Anton15324772018-01-16 10:26:49 -08003585 caller()->AddAudioVideoTracks();
3586 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003587 caller()->CreateAndSetAndSignalOffer();
3588 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3589 // The caller should still have a data channel, but it should be closed, and
3590 // one should ever have been created for the callee.
3591 EXPECT_TRUE(caller()->data_channel() != nullptr);
3592 EXPECT_FALSE(caller()->data_observer()->IsOpen());
3593 EXPECT_EQ(nullptr, callee()->data_channel());
3594}
3595
3596// This test sets up a call between two parties with audio, and video. When
3597// audio and video is setup and flowing, an RTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003598TEST_P(PeerConnectionIntegrationTest, AddRtpDataChannelInSubsequentOffer) {
Niels Möllerf06f9232018-08-07 12:32:18 +02003599 PeerConnectionInterface::RTCConfiguration rtc_config;
3600 rtc_config.enable_rtp_data_channel = true;
3601 rtc_config.enable_dtls_srtp = false;
3602 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(rtc_config, rtc_config));
deadbeef1dcb1642017-03-29 21:08:16 -07003603 ConnectFakeSignaling();
3604 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003605 caller()->AddAudioVideoTracks();
3606 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003607 caller()->CreateAndSetAndSignalOffer();
3608 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3609 // Create data channel and do new offer and answer.
3610 caller()->CreateDataChannel();
3611 caller()->CreateAndSetAndSignalOffer();
3612 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3613 ASSERT_NE(nullptr, caller()->data_channel());
3614 ASSERT_NE(nullptr, callee()->data_channel());
3615 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3616 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3617 // Ensure data can be sent in both directions.
3618 std::string data = "hello world";
3619 SendRtpDataWithRetries(caller()->data_channel(), data, 5);
3620 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3621 kDefaultTimeout);
3622 SendRtpDataWithRetries(callee()->data_channel(), data, 5);
3623 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3624 kDefaultTimeout);
3625}
3626
3627#ifdef HAVE_SCTP
3628
3629// This test sets up a call between two parties with audio, video and an SCTP
3630// data channel.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003631TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003632 ASSERT_TRUE(CreatePeerConnectionWrappers());
3633 ConnectFakeSignaling();
3634 // Expect that data channel created on caller side will show up for callee as
3635 // well.
3636 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003637 caller()->AddAudioVideoTracks();
3638 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003639 caller()->CreateAndSetAndSignalOffer();
3640 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3641 // Ensure the existence of the SCTP data channel didn't impede audio/video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003642 MediaExpectations media_expectations;
3643 media_expectations.ExpectBidirectionalAudioAndVideo();
3644 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07003645 // Caller data channel should already exist (it created one). Callee data
3646 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3647 ASSERT_NE(nullptr, caller()->data_channel());
3648 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3649 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3650 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3651
3652 // Ensure data can be sent in both directions.
3653 std::string data = "hello world";
3654 caller()->data_channel()->Send(DataBuffer(data));
3655 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3656 kDefaultTimeout);
3657 callee()->data_channel()->Send(DataBuffer(data));
3658 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3659 kDefaultTimeout);
3660}
3661
3662// Ensure that when the callee closes an SCTP data channel, the closing
3663// procedure results in the data channel being closed for the caller as well.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003664TEST_P(PeerConnectionIntegrationTest, CalleeClosesSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003665 // Same procedure as above test.
3666 ASSERT_TRUE(CreatePeerConnectionWrappers());
3667 ConnectFakeSignaling();
3668 caller()->CreateDataChannel();
Steve Anton15324772018-01-16 10:26:49 -08003669 caller()->AddAudioVideoTracks();
3670 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003671 caller()->CreateAndSetAndSignalOffer();
3672 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3673 ASSERT_NE(nullptr, caller()->data_channel());
3674 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3675 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3676 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3677
3678 // Close the data channel on the callee side, and wait for it to reach the
3679 // "closed" state on both sides.
3680 callee()->data_channel()->Close();
3681 EXPECT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
3682 EXPECT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
3683}
3684
Seth Hampson2f0d7022018-02-20 11:54:42 -08003685TEST_P(PeerConnectionIntegrationTest, SctpDataChannelConfigSentToOtherSide) {
Steve Antonda6c0952017-10-23 11:41:54 -07003686 ASSERT_TRUE(CreatePeerConnectionWrappers());
3687 ConnectFakeSignaling();
3688 webrtc::DataChannelInit init;
3689 init.id = 53;
3690 init.maxRetransmits = 52;
3691 caller()->CreateDataChannel("data-channel", &init);
Steve Anton15324772018-01-16 10:26:49 -08003692 caller()->AddAudioVideoTracks();
3693 callee()->AddAudioVideoTracks();
Steve Antonda6c0952017-10-23 11:41:54 -07003694 caller()->CreateAndSetAndSignalOffer();
3695 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton074dece2017-10-24 13:04:12 -07003696 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3697 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
Harald Alvestrand5c4d2ee2019-04-01 12:58:15 +02003698 // Since "negotiated" is false, the "id" parameter should be ignored.
3699 EXPECT_NE(init.id, callee()->data_channel()->id());
Steve Antonda6c0952017-10-23 11:41:54 -07003700 EXPECT_EQ("data-channel", callee()->data_channel()->label());
3701 EXPECT_EQ(init.maxRetransmits, callee()->data_channel()->maxRetransmits());
3702 EXPECT_FALSE(callee()->data_channel()->negotiated());
3703}
3704
deadbeef1dcb1642017-03-29 21:08:16 -07003705// Test usrsctp's ability to process unordered data stream, where data actually
3706// arrives out of order using simulated delays. Previously there have been some
3707// bugs in this area.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003708TEST_P(PeerConnectionIntegrationTest, StressTestUnorderedSctpDataChannel) {
deadbeef1dcb1642017-03-29 21:08:16 -07003709 // Introduce random network delays.
3710 // Otherwise it's not a true "unordered" test.
3711 virtual_socket_server()->set_delay_mean(20);
3712 virtual_socket_server()->set_delay_stddev(5);
3713 virtual_socket_server()->UpdateDelayDistribution();
3714 // Normal procedure, but with unordered data channel config.
3715 ASSERT_TRUE(CreatePeerConnectionWrappers());
3716 ConnectFakeSignaling();
3717 webrtc::DataChannelInit init;
3718 init.ordered = false;
3719 caller()->CreateDataChannel(&init);
3720 caller()->CreateAndSetAndSignalOffer();
3721 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3722 ASSERT_NE(nullptr, caller()->data_channel());
3723 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3724 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3725 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3726
3727 static constexpr int kNumMessages = 100;
3728 // Deliberately chosen to be larger than the MTU so messages get fragmented.
3729 static constexpr size_t kMaxMessageSize = 4096;
3730 // Create and send random messages.
3731 std::vector<std::string> sent_messages;
3732 for (int i = 0; i < kNumMessages; ++i) {
3733 size_t length =
3734 (rand() % kMaxMessageSize) + 1; // NOLINT (rand_r instead of rand)
3735 std::string message;
3736 ASSERT_TRUE(rtc::CreateRandomString(length, &message));
3737 caller()->data_channel()->Send(DataBuffer(message));
3738 callee()->data_channel()->Send(DataBuffer(message));
3739 sent_messages.push_back(message);
3740 }
3741
3742 // Wait for all messages to be received.
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003743 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003744 caller()->data_observer()->received_message_count(),
3745 kDefaultTimeout);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02003746 EXPECT_EQ_WAIT(rtc::checked_cast<size_t>(kNumMessages),
deadbeef1dcb1642017-03-29 21:08:16 -07003747 callee()->data_observer()->received_message_count(),
3748 kDefaultTimeout);
3749
3750 // Sort and compare to make sure none of the messages were corrupted.
3751 std::vector<std::string> caller_received_messages =
3752 caller()->data_observer()->messages();
3753 std::vector<std::string> callee_received_messages =
3754 callee()->data_observer()->messages();
Steve Anton64b626b2019-01-28 17:25:26 -08003755 absl::c_sort(sent_messages);
3756 absl::c_sort(caller_received_messages);
3757 absl::c_sort(callee_received_messages);
deadbeef1dcb1642017-03-29 21:08:16 -07003758 EXPECT_EQ(sent_messages, caller_received_messages);
3759 EXPECT_EQ(sent_messages, callee_received_messages);
3760}
3761
3762// This test sets up a call between two parties with audio, and video. When
3763// audio and video are setup and flowing, an SCTP data channel is negotiated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08003764TEST_P(PeerConnectionIntegrationTest, AddSctpDataChannelInSubsequentOffer) {
deadbeef1dcb1642017-03-29 21:08:16 -07003765 ASSERT_TRUE(CreatePeerConnectionWrappers());
3766 ConnectFakeSignaling();
3767 // Do initial offer/answer with audio/video.
Steve Anton15324772018-01-16 10:26:49 -08003768 caller()->AddAudioVideoTracks();
3769 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003770 caller()->CreateAndSetAndSignalOffer();
3771 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3772 // Create data channel and do new offer and answer.
3773 caller()->CreateDataChannel();
3774 caller()->CreateAndSetAndSignalOffer();
3775 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3776 // Caller data channel should already exist (it created one). Callee data
3777 // channel may not exist yet, since negotiation happens in-band, not in SDP.
3778 ASSERT_NE(nullptr, caller()->data_channel());
3779 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3780 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3781 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3782 // Ensure data can be sent in both directions.
3783 std::string data = "hello world";
3784 caller()->data_channel()->Send(DataBuffer(data));
3785 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3786 kDefaultTimeout);
3787 callee()->data_channel()->Send(DataBuffer(data));
3788 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3789 kDefaultTimeout);
3790}
3791
deadbeef7914b8c2017-04-21 03:23:33 -07003792// Set up a connection initially just using SCTP data channels, later upgrading
3793// to audio/video, ensuring frames are received end-to-end. Effectively the
3794// inverse of the test above.
3795// This was broken in M57; see https://crbug.com/711243
Seth Hampson2f0d7022018-02-20 11:54:42 -08003796TEST_P(PeerConnectionIntegrationTest, SctpDataChannelToAudioVideoUpgrade) {
deadbeef7914b8c2017-04-21 03:23:33 -07003797 ASSERT_TRUE(CreatePeerConnectionWrappers());
3798 ConnectFakeSignaling();
3799 // Do initial offer/answer with just data channel.
3800 caller()->CreateDataChannel();
3801 caller()->CreateAndSetAndSignalOffer();
3802 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3803 // Wait until data can be sent over the data channel.
3804 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3805 ASSERT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3806 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3807
3808 // Do subsequent offer/answer with two-way audio and video. Audio and video
3809 // should end up bundled on the DTLS/ICE transport already used for data.
Steve Anton15324772018-01-16 10:26:49 -08003810 caller()->AddAudioVideoTracks();
3811 callee()->AddAudioVideoTracks();
deadbeef7914b8c2017-04-21 03:23:33 -07003812 caller()->CreateAndSetAndSignalOffer();
3813 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08003814 MediaExpectations media_expectations;
3815 media_expectations.ExpectBidirectionalAudioAndVideo();
3816 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef7914b8c2017-04-21 03:23:33 -07003817}
3818
deadbeef8b7e9ad2017-05-25 09:38:55 -07003819static void MakeSpecCompliantSctpOffer(cricket::SessionDescription* desc) {
Harald Alvestrand5fc28b12019-05-13 13:36:16 +02003820 cricket::SctpDataContentDescription* dcd_offer =
3821 GetFirstSctpDataContentDescription(desc);
Harald Alvestrand17ea0682019-12-13 11:51:04 +01003822 // See https://crbug.com/webrtc/11211 - this function is a no-op
Steve Antonb1c1de12017-12-21 15:14:30 -08003823 ASSERT_TRUE(dcd_offer);
deadbeef8b7e9ad2017-05-25 09:38:55 -07003824 dcd_offer->set_use_sctpmap(false);
3825 dcd_offer->set_protocol("UDP/DTLS/SCTP");
3826}
3827
3828// Test that the data channel works when a spec-compliant SCTP m= section is
3829// offered (using "a=sctp-port" instead of "a=sctpmap", and using
3830// "UDP/DTLS/SCTP" as the protocol).
Seth Hampson2f0d7022018-02-20 11:54:42 -08003831TEST_P(PeerConnectionIntegrationTest,
deadbeef8b7e9ad2017-05-25 09:38:55 -07003832 DataChannelWorksWhenSpecCompliantSctpOfferReceived) {
3833 ASSERT_TRUE(CreatePeerConnectionWrappers());
3834 ConnectFakeSignaling();
3835 caller()->CreateDataChannel();
3836 caller()->SetGeneratedSdpMunger(MakeSpecCompliantSctpOffer);
3837 caller()->CreateAndSetAndSignalOffer();
3838 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3839 ASSERT_TRUE_WAIT(callee()->data_channel() != nullptr, kDefaultTimeout);
3840 EXPECT_TRUE_WAIT(caller()->data_observer()->IsOpen(), kDefaultTimeout);
3841 EXPECT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
3842
3843 // Ensure data can be sent in both directions.
3844 std::string data = "hello world";
3845 caller()->data_channel()->Send(DataBuffer(data));
3846 EXPECT_EQ_WAIT(data, callee()->data_observer()->last_message(),
3847 kDefaultTimeout);
3848 callee()->data_channel()->Send(DataBuffer(data));
3849 EXPECT_EQ_WAIT(data, caller()->data_observer()->last_message(),
3850 kDefaultTimeout);
3851}
3852
deadbeef1dcb1642017-03-29 21:08:16 -07003853#endif // HAVE_SCTP
3854
3855// Test that the ICE connection and gathering states eventually reach
3856// "complete".
Seth Hampson2f0d7022018-02-20 11:54:42 -08003857TEST_P(PeerConnectionIntegrationTest, IceStatesReachCompletion) {
deadbeef1dcb1642017-03-29 21:08:16 -07003858 ASSERT_TRUE(CreatePeerConnectionWrappers());
3859 ConnectFakeSignaling();
3860 // Do normal offer/answer.
Steve Anton15324772018-01-16 10:26:49 -08003861 caller()->AddAudioVideoTracks();
3862 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07003863 caller()->CreateAndSetAndSignalOffer();
3864 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3865 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3866 caller()->ice_gathering_state(), kMaxWaitForFramesMs);
3867 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceGatheringComplete,
3868 callee()->ice_gathering_state(), kMaxWaitForFramesMs);
3869 // After the best candidate pair is selected and all candidates are signaled,
3870 // the ICE connection state should reach "complete".
3871 // TODO(deadbeef): Currently, the ICE "controlled" agent (the
3872 // answerer/"callee" by default) only reaches "connected". When this is
3873 // fixed, this test should be updated.
3874 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3875 caller()->ice_connection_state(), kDefaultTimeout);
Alex Loiko9289eda2018-11-23 16:18:59 +00003876 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3877 callee()->ice_connection_state(), kDefaultTimeout);
deadbeef1dcb1642017-03-29 21:08:16 -07003878}
3879
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003880constexpr int kOnlyLocalPorts = cricket::PORTALLOCATOR_DISABLE_STUN |
3881 cricket::PORTALLOCATOR_DISABLE_RELAY |
3882 cricket::PORTALLOCATOR_DISABLE_TCP;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003883
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003884// Use a mock resolver to resolve the hostname back to the original IP on both
3885// sides and check that the ICE connection connects.
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003886TEST_P(PeerConnectionIntegrationTest,
3887 IceStatesReachCompletionWithRemoteHostname) {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003888 auto caller_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003889 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003890 auto callee_resolver_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003891 std::make_unique<NiceMock<webrtc::MockAsyncResolverFactory>>();
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003892 NiceMock<rtc::MockAsyncResolver> callee_async_resolver;
3893 NiceMock<rtc::MockAsyncResolver> caller_async_resolver;
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003894
3895 // This also verifies that the injected AsyncResolverFactory is used by
3896 // P2PTransportChannel.
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003897 EXPECT_CALL(*caller_resolver_factory, Create())
3898 .WillOnce(Return(&caller_async_resolver));
3899 webrtc::PeerConnectionDependencies caller_deps(nullptr);
3900 caller_deps.async_resolver_factory = std::move(caller_resolver_factory);
3901
3902 EXPECT_CALL(*callee_resolver_factory, Create())
3903 .WillOnce(Return(&callee_async_resolver));
3904 webrtc::PeerConnectionDependencies callee_deps(nullptr);
3905 callee_deps.async_resolver_factory = std::move(callee_resolver_factory);
3906
3907 PeerConnectionInterface::RTCConfiguration config;
3908 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
3909 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
3910
3911 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
3912 config, std::move(caller_deps), config, std::move(callee_deps)));
3913
3914 caller()->SetRemoteAsyncResolver(&callee_async_resolver);
3915 callee()->SetRemoteAsyncResolver(&caller_async_resolver);
3916
3917 // Enable hostname candidates with mDNS names.
Qingsi Wangecd30542019-05-22 14:34:56 -07003918 caller()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003919 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wangecd30542019-05-22 14:34:56 -07003920 callee()->SetMdnsResponder(
Mirko Bonadei317a1f02019-09-17 17:06:18 +02003921 std::make_unique<webrtc::FakeMdnsResponder>(network_thread()));
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003922
3923 SetPortAllocatorFlags(kOnlyLocalPorts, kOnlyLocalPorts);
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003924
3925 ConnectFakeSignaling();
3926 caller()->AddAudioVideoTracks();
3927 callee()->AddAudioVideoTracks();
3928 caller()->CreateAndSetAndSignalOffer();
3929 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
3930 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
3931 caller()->ice_connection_state(), kDefaultTimeout);
3932 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
3933 callee()->ice_connection_state(), kDefaultTimeout);
Jeroen de Borst833979f2018-12-13 08:25:54 -08003934
Ying Wangef3998f2019-12-09 13:06:53 +01003935 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
3936 "WebRTC.PeerConnection.CandidatePairType_UDP",
3937 webrtc::kIceCandidatePairHostNameHostName));
Zach Stein6fcdc2f2018-08-23 16:25:55 -07003938}
3939
Steve Antonede9ca52017-10-16 13:04:27 -07003940// Test that firewalling the ICE connection causes the clients to identify the
3941// disconnected state and then removing the firewall causes them to reconnect.
3942class PeerConnectionIntegrationIceStatesTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08003943 : public PeerConnectionIntegrationBaseTest,
3944 public ::testing::WithParamInterface<
3945 std::tuple<SdpSemantics, std::tuple<std::string, uint32_t>>> {
Steve Antonede9ca52017-10-16 13:04:27 -07003946 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08003947 PeerConnectionIntegrationIceStatesTest()
3948 : PeerConnectionIntegrationBaseTest(std::get<0>(GetParam())) {
3949 port_allocator_flags_ = std::get<1>(std::get<1>(GetParam()));
Steve Antonede9ca52017-10-16 13:04:27 -07003950 }
3951
3952 void StartStunServer(const SocketAddress& server_address) {
3953 stun_server_.reset(
3954 cricket::TestStunServer::Create(network_thread(), server_address));
3955 }
3956
3957 bool TestIPv6() {
3958 return (port_allocator_flags_ & cricket::PORTALLOCATOR_ENABLE_IPV6);
3959 }
3960
3961 void SetPortAllocatorFlags() {
Qingsi Wang1dac6d82018-12-12 15:28:47 -08003962 PeerConnectionIntegrationBaseTest::SetPortAllocatorFlags(
3963 port_allocator_flags_, port_allocator_flags_);
Steve Antonede9ca52017-10-16 13:04:27 -07003964 }
3965
3966 std::vector<SocketAddress> CallerAddresses() {
3967 std::vector<SocketAddress> addresses;
3968 addresses.push_back(SocketAddress("1.1.1.1", 0));
3969 if (TestIPv6()) {
3970 addresses.push_back(SocketAddress("1111:0:a:b:c:d:e:f", 0));
3971 }
3972 return addresses;
3973 }
3974
3975 std::vector<SocketAddress> CalleeAddresses() {
3976 std::vector<SocketAddress> addresses;
3977 addresses.push_back(SocketAddress("2.2.2.2", 0));
3978 if (TestIPv6()) {
3979 addresses.push_back(SocketAddress("2222:0:a:b:c:d:e:f", 0));
3980 }
3981 return addresses;
3982 }
3983
3984 void SetUpNetworkInterfaces() {
3985 // Remove the default interfaces added by the test infrastructure.
Qingsi Wangecd30542019-05-22 14:34:56 -07003986 caller()->network_manager()->RemoveInterface(kDefaultLocalAddress);
3987 callee()->network_manager()->RemoveInterface(kDefaultLocalAddress);
Steve Antonede9ca52017-10-16 13:04:27 -07003988
3989 // Add network addresses for test.
3990 for (const auto& caller_address : CallerAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07003991 caller()->network_manager()->AddInterface(caller_address);
Steve Antonede9ca52017-10-16 13:04:27 -07003992 }
3993 for (const auto& callee_address : CalleeAddresses()) {
Qingsi Wangecd30542019-05-22 14:34:56 -07003994 callee()->network_manager()->AddInterface(callee_address);
Steve Antonede9ca52017-10-16 13:04:27 -07003995 }
3996 }
3997
3998 private:
3999 uint32_t port_allocator_flags_;
4000 std::unique_ptr<cricket::TestStunServer> stun_server_;
4001};
4002
Yves Gerey100fe632020-01-17 19:15:53 +01004003// Ensure FakeClockForTest is constructed first (see class for rationale).
4004class PeerConnectionIntegrationIceStatesTestWithFakeClock
4005 : public FakeClockForTest,
4006 public PeerConnectionIntegrationIceStatesTest {};
4007
Steve Antonede9ca52017-10-16 13:04:27 -07004008// Tests that the PeerConnection goes through all the ICE gathering/connection
4009// states over the duration of the call. This includes Disconnected and Failed
4010// states, induced by putting a firewall between the peers and waiting for them
4011// to time out.
Yves Gerey100fe632020-01-17 19:15:53 +01004012TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock, VerifyIceStates) {
Steve Antonede9ca52017-10-16 13:04:27 -07004013 const SocketAddress kStunServerAddress =
4014 SocketAddress("99.99.99.1", cricket::STUN_SERVER_PORT);
4015 StartStunServer(kStunServerAddress);
4016
4017 PeerConnectionInterface::RTCConfiguration config;
4018 PeerConnectionInterface::IceServer ice_stun_server;
4019 ice_stun_server.urls.push_back(
4020 "stun:" + kStunServerAddress.HostAsURIString() + ":" +
4021 kStunServerAddress.PortAsString());
4022 config.servers.push_back(ice_stun_server);
4023
4024 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4025 ConnectFakeSignaling();
4026 SetPortAllocatorFlags();
4027 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004028 caller()->AddAudioVideoTracks();
4029 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004030
4031 // Initial state before anything happens.
4032 ASSERT_EQ(PeerConnectionInterface::kIceGatheringNew,
4033 caller()->ice_gathering_state());
4034 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
4035 caller()->ice_connection_state());
Jonas Olsson7a6739e2019-01-15 16:31:55 +01004036 ASSERT_EQ(PeerConnectionInterface::kIceConnectionNew,
4037 caller()->standardized_ice_connection_state());
Steve Antonede9ca52017-10-16 13:04:27 -07004038
4039 // Start the call by creating the offer, setting it as the local description,
4040 // then sending it to the peer who will respond with an answer. This happens
4041 // asynchronously so that we can watch the states as it runs in the
4042 // background.
4043 caller()->CreateAndSetAndSignalOffer();
4044
Steve Antona9b67ce2020-01-16 14:00:44 -08004045 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4046 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004047 FakeClock());
Steve Antona9b67ce2020-01-16 14:00:44 -08004048 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4049 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004050 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004051
4052 // Verify that the observer was notified of the intermediate transitions.
4053 EXPECT_THAT(caller()->ice_connection_state_history(),
4054 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
4055 PeerConnectionInterface::kIceConnectionConnected,
4056 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004057 EXPECT_THAT(caller()->standardized_ice_connection_state_history(),
4058 ElementsAre(PeerConnectionInterface::kIceConnectionChecking,
4059 PeerConnectionInterface::kIceConnectionConnected,
4060 PeerConnectionInterface::kIceConnectionCompleted));
Jonas Olsson635474e2018-10-18 15:58:17 +02004061 EXPECT_THAT(
4062 caller()->peer_connection_state_history(),
4063 ElementsAre(PeerConnectionInterface::PeerConnectionState::kConnecting,
Jonas Olsson635474e2018-10-18 15:58:17 +02004064 PeerConnectionInterface::PeerConnectionState::kConnected));
Steve Antonede9ca52017-10-16 13:04:27 -07004065 EXPECT_THAT(caller()->ice_gathering_state_history(),
4066 ElementsAre(PeerConnectionInterface::kIceGatheringGathering,
4067 PeerConnectionInterface::kIceGatheringComplete));
4068
4069 // Block connections to/from the caller and wait for ICE to become
4070 // disconnected.
4071 for (const auto& caller_address : CallerAddresses()) {
4072 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4073 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004074 RTC_LOG(LS_INFO) << "Firewall rules applied";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004075 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
4076 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004077 FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004078 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionDisconnected,
4079 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004080 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004081
4082 // Let ICE re-establish by removing the firewall rules.
4083 firewall()->ClearRules();
Mirko Bonadei675513b2017-11-09 11:09:25 +01004084 RTC_LOG(LS_INFO) << "Firewall rules cleared";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004085 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4086 caller()->ice_connection_state(), kDefaultTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004087 FakeClock());
Jonas Olssonacd8ae72019-02-25 15:26:24 +01004088 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004089 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004090 kDefaultTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004091
4092 // According to RFC7675, if there is no response within 30 seconds then the
4093 // peer should consider the other side to have rejected the connection. This
Steve Anton83119dd2017-11-10 16:19:52 -08004094 // is signaled by the state transitioning to "failed".
Steve Antonede9ca52017-10-16 13:04:27 -07004095 constexpr int kConsentTimeout = 30000;
4096 for (const auto& caller_address : CallerAddresses()) {
4097 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4098 }
Mirko Bonadei675513b2017-11-09 11:09:25 +01004099 RTC_LOG(LS_INFO) << "Firewall rules applied again";
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004100 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4101 caller()->ice_connection_state(), kConsentTimeout,
Yves Gerey100fe632020-01-17 19:15:53 +01004102 FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004103 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4104 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004105 kConsentTimeout, FakeClock());
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004106}
4107
4108// Tests that if the connection doesn't get set up properly we eventually reach
4109// the "failed" iceConnectionState.
Yves Gerey100fe632020-01-17 19:15:53 +01004110TEST_P(PeerConnectionIntegrationIceStatesTestWithFakeClock,
4111 IceStateSetupFailure) {
Jonas Olssonb75d9e92019-02-22 10:33:29 +01004112 // Block connections to/from the caller and wait for ICE to become
4113 // disconnected.
4114 for (const auto& caller_address : CallerAddresses()) {
4115 firewall()->AddRule(false, rtc::FP_ANY, rtc::FD_ANY, caller_address);
4116 }
4117
4118 ASSERT_TRUE(CreatePeerConnectionWrappers());
4119 ConnectFakeSignaling();
4120 SetPortAllocatorFlags();
4121 SetUpNetworkInterfaces();
4122 caller()->AddAudioVideoTracks();
4123 caller()->CreateAndSetAndSignalOffer();
4124
4125 // According to RFC7675, if there is no response within 30 seconds then the
4126 // peer should consider the other side to have rejected the connection. This
4127 // is signaled by the state transitioning to "failed".
4128 constexpr int kConsentTimeout = 30000;
4129 ASSERT_EQ_SIMULATED_WAIT(PeerConnectionInterface::kIceConnectionFailed,
4130 caller()->standardized_ice_connection_state(),
Yves Gerey100fe632020-01-17 19:15:53 +01004131 kConsentTimeout, FakeClock());
Steve Antonede9ca52017-10-16 13:04:27 -07004132}
4133
4134// Tests that the best connection is set to the appropriate IPv4/IPv6 connection
4135// and that the statistics in the metric observers are updated correctly.
4136TEST_P(PeerConnectionIntegrationIceStatesTest, VerifyBestConnection) {
4137 ASSERT_TRUE(CreatePeerConnectionWrappers());
4138 ConnectFakeSignaling();
4139 SetPortAllocatorFlags();
4140 SetUpNetworkInterfaces();
Steve Anton15324772018-01-16 10:26:49 -08004141 caller()->AddAudioVideoTracks();
4142 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004143 caller()->CreateAndSetAndSignalOffer();
4144
4145 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Anton692f3c72020-01-16 14:12:31 -08004146 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4147 caller()->ice_connection_state(), kDefaultTimeout);
4148 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4149 callee()->ice_connection_state(), kDefaultTimeout);
Steve Antonede9ca52017-10-16 13:04:27 -07004150
Qingsi Wang7fc821d2018-07-12 12:54:53 -07004151 // TODO(bugs.webrtc.org/9456): Fix it.
4152 const int num_best_ipv4 = webrtc::metrics::NumEvents(
4153 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv4);
4154 const int num_best_ipv6 = webrtc::metrics::NumEvents(
4155 "WebRTC.PeerConnection.IPMetrics", webrtc::kBestConnections_IPv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004156 if (TestIPv6()) {
4157 // When IPv6 is enabled, we should prefer an IPv6 connection over an IPv4
4158 // connection.
Ying Wangef3998f2019-12-09 13:06:53 +01004159 EXPECT_METRIC_EQ(0, num_best_ipv4);
4160 EXPECT_METRIC_EQ(1, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004161 } else {
Ying Wangef3998f2019-12-09 13:06:53 +01004162 EXPECT_METRIC_EQ(1, num_best_ipv4);
4163 EXPECT_METRIC_EQ(0, num_best_ipv6);
Steve Antonede9ca52017-10-16 13:04:27 -07004164 }
4165
Ying Wangef3998f2019-12-09 13:06:53 +01004166 EXPECT_METRIC_EQ(0, webrtc::metrics::NumEvents(
4167 "WebRTC.PeerConnection.CandidatePairType_UDP",
4168 webrtc::kIceCandidatePairHostHost));
4169 EXPECT_METRIC_EQ(1, webrtc::metrics::NumEvents(
4170 "WebRTC.PeerConnection.CandidatePairType_UDP",
4171 webrtc::kIceCandidatePairHostPublicHostPublic));
Steve Antonede9ca52017-10-16 13:04:27 -07004172}
4173
4174constexpr uint32_t kFlagsIPv4NoStun = cricket::PORTALLOCATOR_DISABLE_TCP |
4175 cricket::PORTALLOCATOR_DISABLE_STUN |
4176 cricket::PORTALLOCATOR_DISABLE_RELAY;
4177constexpr uint32_t kFlagsIPv6NoStun =
4178 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_STUN |
4179 cricket::PORTALLOCATOR_ENABLE_IPV6 | cricket::PORTALLOCATOR_DISABLE_RELAY;
4180constexpr uint32_t kFlagsIPv4Stun =
4181 cricket::PORTALLOCATOR_DISABLE_TCP | cricket::PORTALLOCATOR_DISABLE_RELAY;
4182
Mirko Bonadeic84f6612019-01-31 12:20:57 +01004183INSTANTIATE_TEST_SUITE_P(
Seth Hampson2f0d7022018-02-20 11:54:42 -08004184 PeerConnectionIntegrationTest,
4185 PeerConnectionIntegrationIceStatesTest,
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))));
Steve Antonede9ca52017-10-16 13:04:27 -07004190
Yves Gerey100fe632020-01-17 19:15:53 +01004191INSTANTIATE_TEST_SUITE_P(
4192 PeerConnectionIntegrationTest,
4193 PeerConnectionIntegrationIceStatesTestWithFakeClock,
4194 Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
4195 Values(std::make_pair("IPv4 no STUN", kFlagsIPv4NoStun),
4196 std::make_pair("IPv6 no STUN", kFlagsIPv6NoStun),
4197 std::make_pair("IPv4 with STUN", kFlagsIPv4Stun))));
4198
deadbeef1dcb1642017-03-29 21:08:16 -07004199// This test sets up a call between two parties with audio and video.
4200// During the call, the caller restarts ICE and the test verifies that
4201// new ICE candidates are generated and audio and video still can flow, and the
4202// ICE state reaches completed again.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004203TEST_P(PeerConnectionIntegrationTest, MediaContinuesFlowingAfterIceRestart) {
deadbeef1dcb1642017-03-29 21:08:16 -07004204 ASSERT_TRUE(CreatePeerConnectionWrappers());
4205 ConnectFakeSignaling();
4206 // Do normal offer/answer and wait for ICE to complete.
Steve Anton15324772018-01-16 10:26:49 -08004207 caller()->AddAudioVideoTracks();
4208 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004209 caller()->CreateAndSetAndSignalOffer();
4210 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4211 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4212 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004213 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4214 callee()->ice_connection_state(), kMaxWaitForFramesMs);
deadbeef1dcb1642017-03-29 21:08:16 -07004215
4216 // To verify that the ICE restart actually occurs, get
4217 // ufrag/password/candidates before and after restart.
4218 // Create an SDP string of the first audio candidate for both clients.
4219 const webrtc::IceCandidateCollection* audio_candidates_caller =
4220 caller()->pc()->local_description()->candidates(0);
4221 const webrtc::IceCandidateCollection* audio_candidates_callee =
4222 callee()->pc()->local_description()->candidates(0);
4223 ASSERT_GT(audio_candidates_caller->count(), 0u);
4224 ASSERT_GT(audio_candidates_callee->count(), 0u);
4225 std::string caller_candidate_pre_restart;
4226 ASSERT_TRUE(
4227 audio_candidates_caller->at(0)->ToString(&caller_candidate_pre_restart));
4228 std::string callee_candidate_pre_restart;
4229 ASSERT_TRUE(
4230 audio_candidates_callee->at(0)->ToString(&callee_candidate_pre_restart));
4231 const cricket::SessionDescription* desc =
4232 caller()->pc()->local_description()->description();
4233 std::string caller_ufrag_pre_restart =
4234 desc->transport_infos()[0].description.ice_ufrag;
4235 desc = callee()->pc()->local_description()->description();
4236 std::string callee_ufrag_pre_restart =
4237 desc->transport_infos()[0].description.ice_ufrag;
4238
Alex Drake00c7ecf2019-08-06 10:54:47 -07004239 EXPECT_EQ(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07004240 // Have the caller initiate an ICE restart.
4241 caller()->SetOfferAnswerOptions(IceRestartOfferAnswerOptions());
4242 caller()->CreateAndSetAndSignalOffer();
4243 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4244 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4245 caller()->ice_connection_state(), kMaxWaitForFramesMs);
Alex Loiko9289eda2018-11-23 16:18:59 +00004246 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
deadbeef1dcb1642017-03-29 21:08:16 -07004247 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4248
4249 // Grab the ufrags/candidates again.
4250 audio_candidates_caller = caller()->pc()->local_description()->candidates(0);
4251 audio_candidates_callee = callee()->pc()->local_description()->candidates(0);
4252 ASSERT_GT(audio_candidates_caller->count(), 0u);
4253 ASSERT_GT(audio_candidates_callee->count(), 0u);
4254 std::string caller_candidate_post_restart;
4255 ASSERT_TRUE(
4256 audio_candidates_caller->at(0)->ToString(&caller_candidate_post_restart));
4257 std::string callee_candidate_post_restart;
4258 ASSERT_TRUE(
4259 audio_candidates_callee->at(0)->ToString(&callee_candidate_post_restart));
4260 desc = caller()->pc()->local_description()->description();
4261 std::string caller_ufrag_post_restart =
4262 desc->transport_infos()[0].description.ice_ufrag;
4263 desc = callee()->pc()->local_description()->description();
4264 std::string callee_ufrag_post_restart =
4265 desc->transport_infos()[0].description.ice_ufrag;
4266 // Sanity check that an ICE restart was actually negotiated in SDP.
4267 ASSERT_NE(caller_candidate_pre_restart, caller_candidate_post_restart);
4268 ASSERT_NE(callee_candidate_pre_restart, callee_candidate_post_restart);
4269 ASSERT_NE(caller_ufrag_pre_restart, caller_ufrag_post_restart);
4270 ASSERT_NE(callee_ufrag_pre_restart, callee_ufrag_post_restart);
Alex Drake00c7ecf2019-08-06 10:54:47 -07004271 EXPECT_GT(caller()->ice_candidate_pair_change_history().size(), 1u);
deadbeef1dcb1642017-03-29 21:08:16 -07004272
4273 // Ensure that additional frames are received after the ICE restart.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004274 MediaExpectations media_expectations;
4275 media_expectations.ExpectBidirectionalAudioAndVideo();
4276 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004277}
4278
4279// Verify that audio/video can be received end-to-end when ICE renomination is
4280// enabled.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004281TEST_P(PeerConnectionIntegrationTest, EndToEndCallWithIceRenomination) {
deadbeef1dcb1642017-03-29 21:08:16 -07004282 PeerConnectionInterface::RTCConfiguration config;
4283 config.enable_ice_renomination = true;
4284 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
4285 ConnectFakeSignaling();
4286 // Do normal offer/answer and wait for some frames to be received in each
4287 // direction.
Steve Anton15324772018-01-16 10:26:49 -08004288 caller()->AddAudioVideoTracks();
4289 callee()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004290 caller()->CreateAndSetAndSignalOffer();
4291 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4292 // Sanity check that ICE renomination was actually negotiated.
4293 const cricket::SessionDescription* desc =
4294 caller()->pc()->local_description()->description();
4295 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004296 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004297 }
4298 desc = callee()->pc()->local_description()->description();
4299 for (const cricket::TransportInfo& info : desc->transport_infos()) {
Steve Anton64b626b2019-01-28 17:25:26 -08004300 ASSERT_THAT(info.description.transport_options, Contains("renomination"));
deadbeef1dcb1642017-03-29 21:08:16 -07004301 }
Seth Hampson2f0d7022018-02-20 11:54:42 -08004302 MediaExpectations media_expectations;
4303 media_expectations.ExpectBidirectionalAudioAndVideo();
4304 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004305}
4306
Steve Anton6f25b092017-10-23 09:39:20 -07004307// With a max bundle policy and RTCP muxing, adding a new media description to
4308// the connection should not affect ICE at all because the new media will use
4309// the existing connection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004310TEST_P(PeerConnectionIntegrationTest,
Steve Anton83119dd2017-11-10 16:19:52 -08004311 AddMediaToConnectedBundleDoesNotRestartIce) {
Steve Anton6f25b092017-10-23 09:39:20 -07004312 PeerConnectionInterface::RTCConfiguration config;
4313 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
4314 config.rtcp_mux_policy = PeerConnectionInterface::kRtcpMuxPolicyRequire;
4315 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(
4316 config, PeerConnectionInterface::RTCConfiguration()));
4317 ConnectFakeSignaling();
4318
Steve Anton15324772018-01-16 10:26:49 -08004319 caller()->AddAudioTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004320 caller()->CreateAndSetAndSignalOffer();
4321 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Steve Antonff52f1b2017-10-26 12:24:50 -07004322 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceConnectionCompleted,
4323 caller()->ice_connection_state(), kDefaultTimeout);
Steve Anton6f25b092017-10-23 09:39:20 -07004324
4325 caller()->clear_ice_connection_state_history();
4326
Steve Anton15324772018-01-16 10:26:49 -08004327 caller()->AddVideoTrack();
Steve Anton6f25b092017-10-23 09:39:20 -07004328 caller()->CreateAndSetAndSignalOffer();
4329 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4330
4331 EXPECT_EQ(0u, caller()->ice_connection_state_history().size());
4332}
4333
deadbeef1dcb1642017-03-29 21:08:16 -07004334// This test sets up a call between two parties with audio and video. It then
4335// renegotiates setting the video m-line to "port 0", then later renegotiates
4336// again, enabling video.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004337TEST_P(PeerConnectionIntegrationTest,
deadbeef1dcb1642017-03-29 21:08:16 -07004338 VideoFlowsAfterMediaSectionIsRejectedAndRecycled) {
4339 ASSERT_TRUE(CreatePeerConnectionWrappers());
4340 ConnectFakeSignaling();
4341
4342 // Do initial negotiation, only sending media from the caller. Will result in
4343 // video and audio recvonly "m=" sections.
Steve Anton15324772018-01-16 10:26:49 -08004344 caller()->AddAudioVideoTracks();
deadbeef1dcb1642017-03-29 21:08:16 -07004345 caller()->CreateAndSetAndSignalOffer();
4346 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4347
4348 // Negotiate again, disabling the video "m=" section (the callee will set the
4349 // port to 0 due to offer_to_receive_video = 0).
Seth Hampson2f0d7022018-02-20 11:54:42 -08004350 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4351 PeerConnectionInterface::RTCOfferAnswerOptions options;
4352 options.offer_to_receive_video = 0;
4353 callee()->SetOfferAnswerOptions(options);
4354 } else {
4355 callee()->SetRemoteOfferHandler([this] {
Harald Alvestrand6060df52020-08-11 09:54:02 +02004356 callee()
4357 ->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO)
4358 ->StopInternal();
Seth Hampson2f0d7022018-02-20 11:54:42 -08004359 });
4360 }
deadbeef1dcb1642017-03-29 21:08:16 -07004361 caller()->CreateAndSetAndSignalOffer();
4362 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4363 // Sanity check that video "m=" section was actually rejected.
4364 const ContentInfo* answer_video_content = cricket::GetFirstVideoContent(
4365 callee()->pc()->local_description()->description());
4366 ASSERT_NE(nullptr, answer_video_content);
4367 ASSERT_TRUE(answer_video_content->rejected);
4368
4369 // Enable video and do negotiation again, making sure video is received
4370 // end-to-end, also adding media stream to callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004371 if (sdp_semantics_ == SdpSemantics::kPlanB) {
4372 PeerConnectionInterface::RTCOfferAnswerOptions options;
4373 options.offer_to_receive_video = 1;
4374 callee()->SetOfferAnswerOptions(options);
4375 } else {
4376 // The caller's transceiver is stopped, so we need to add another track.
4377 auto caller_transceiver =
4378 caller()->GetFirstTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
Harald Alvestrand6060df52020-08-11 09:54:02 +02004379 EXPECT_EQ(nullptr, caller_transceiver.get());
Seth Hampson2f0d7022018-02-20 11:54:42 -08004380 caller()->AddVideoTrack();
4381 }
4382 callee()->AddVideoTrack();
4383 callee()->SetRemoteOfferHandler(nullptr);
deadbeef1dcb1642017-03-29 21:08:16 -07004384 caller()->CreateAndSetAndSignalOffer();
4385 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004386
deadbeef1dcb1642017-03-29 21:08:16 -07004387 // Verify the caller receives frames from the newly added stream, and the
4388 // callee receives additional frames from the re-enabled video m= section.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004389 MediaExpectations media_expectations;
4390 media_expectations.CalleeExpectsSomeAudio();
4391 media_expectations.ExpectBidirectionalVideo();
4392 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004393}
4394
deadbeef1dcb1642017-03-29 21:08:16 -07004395// This tests that if we negotiate after calling CreateSender but before we
4396// have a track, then set a track later, frames from the newly-set track are
4397// received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004398TEST_F(PeerConnectionIntegrationTestPlanB,
deadbeef1dcb1642017-03-29 21:08:16 -07004399 MediaFlowsAfterEarlyWarmupWithCreateSender) {
4400 ASSERT_TRUE(CreatePeerConnectionWrappers());
4401 ConnectFakeSignaling();
4402 auto caller_audio_sender =
4403 caller()->pc()->CreateSender("audio", "caller_stream");
4404 auto caller_video_sender =
4405 caller()->pc()->CreateSender("video", "caller_stream");
4406 auto callee_audio_sender =
4407 callee()->pc()->CreateSender("audio", "callee_stream");
4408 auto callee_video_sender =
4409 callee()->pc()->CreateSender("video", "callee_stream");
4410 caller()->CreateAndSetAndSignalOffer();
4411 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4412 // Wait for ICE to complete, without any tracks being set.
4413 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4414 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4415 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4416 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4417 // Now set the tracks, and expect frames to immediately start flowing.
4418 EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4419 EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4420 EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4421 EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
Seth Hampson2f0d7022018-02-20 11:54:42 -08004422 MediaExpectations media_expectations;
4423 media_expectations.ExpectBidirectionalAudioAndVideo();
4424 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4425}
4426
4427// This tests that if we negotiate after calling AddTransceiver but before we
4428// have a track, then set a track later, frames from the newly-set tracks are
4429// received end-to-end.
4430TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
4431 MediaFlowsAfterEarlyWarmupWithAddTransceiver) {
4432 ASSERT_TRUE(CreatePeerConnectionWrappers());
4433 ConnectFakeSignaling();
4434 auto audio_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
4435 ASSERT_EQ(RTCErrorType::NONE, audio_result.error().type());
4436 auto caller_audio_sender = audio_result.MoveValue()->sender();
4437 auto video_result = caller()->pc()->AddTransceiver(cricket::MEDIA_TYPE_VIDEO);
4438 ASSERT_EQ(RTCErrorType::NONE, video_result.error().type());
4439 auto caller_video_sender = video_result.MoveValue()->sender();
4440 callee()->SetRemoteOfferHandler([this] {
4441 ASSERT_EQ(2u, callee()->pc()->GetTransceivers().size());
Harald Alvestrand6060df52020-08-11 09:54:02 +02004442 callee()->pc()->GetTransceivers()[0]->SetDirectionWithError(
Seth Hampson2f0d7022018-02-20 11:54:42 -08004443 RtpTransceiverDirection::kSendRecv);
Harald Alvestrand6060df52020-08-11 09:54:02 +02004444 callee()->pc()->GetTransceivers()[1]->SetDirectionWithError(
Seth Hampson2f0d7022018-02-20 11:54:42 -08004445 RtpTransceiverDirection::kSendRecv);
4446 });
4447 caller()->CreateAndSetAndSignalOffer();
4448 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4449 // Wait for ICE to complete, without any tracks being set.
4450 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionCompleted,
4451 caller()->ice_connection_state(), kMaxWaitForFramesMs);
4452 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4453 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4454 // Now set the tracks, and expect frames to immediately start flowing.
4455 auto callee_audio_sender = callee()->pc()->GetSenders()[0];
4456 auto callee_video_sender = callee()->pc()->GetSenders()[1];
4457 ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
4458 ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
4459 ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
4460 ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
4461 MediaExpectations media_expectations;
4462 media_expectations.ExpectBidirectionalAudioAndVideo();
4463 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004464}
4465
4466// This test verifies that a remote video track can be added via AddStream,
4467// and sent end-to-end. For this particular test, it's simply echoed back
4468// from the caller to the callee, rather than being forwarded to a third
4469// PeerConnection.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004470TEST_F(PeerConnectionIntegrationTestPlanB, CanSendRemoteVideoTrack) {
deadbeef1dcb1642017-03-29 21:08:16 -07004471 ASSERT_TRUE(CreatePeerConnectionWrappers());
4472 ConnectFakeSignaling();
4473 // Just send a video track from the caller.
Steve Anton15324772018-01-16 10:26:49 -08004474 caller()->AddVideoTrack();
deadbeef1dcb1642017-03-29 21:08:16 -07004475 caller()->CreateAndSetAndSignalOffer();
4476 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
Mirko Bonadeie12c1fe2018-07-03 12:53:23 +02004477 ASSERT_EQ(1U, callee()->remote_streams()->count());
deadbeef1dcb1642017-03-29 21:08:16 -07004478
4479 // Echo the stream back, and do a new offer/anwer (initiated by callee this
4480 // time).
4481 callee()->pc()->AddStream(callee()->remote_streams()->at(0));
4482 callee()->CreateAndSetAndSignalOffer();
4483 ASSERT_TRUE_WAIT(SignalingStateStable(), kMaxWaitForActivationMs);
4484
Seth Hampson2f0d7022018-02-20 11:54:42 -08004485 MediaExpectations media_expectations;
4486 media_expectations.ExpectBidirectionalVideo();
4487 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeef1dcb1642017-03-29 21:08:16 -07004488}
4489
4490// Test that we achieve the expected end-to-end connection time, using a
4491// fake clock and simulated latency on the media and signaling paths.
4492// We use a TURN<->TURN connection because this is usually the quickest to
4493// set up initially, especially when we're confident the connection will work
4494// and can start sending media before we get a STUN response.
4495//
4496// With various optimizations enabled, here are the network delays we expect to
4497// be on the critical path:
4498// 1. 2 signaling trips: Signaling offer and offerer's TURN candidate, then
4499// signaling answer (with DTLS fingerprint).
4500// 2. 9 media hops: Rest of the DTLS handshake. 3 hops in each direction when
4501// using TURN<->TURN pair, and DTLS exchange is 4 packets,
4502// the first of which should have arrived before the answer.
Yves Gerey100fe632020-01-17 19:15:53 +01004503TEST_P(PeerConnectionIntegrationTestWithFakeClock,
4504 EndToEndConnectionTimeWithTurnTurnPair) {
deadbeef1dcb1642017-03-29 21:08:16 -07004505 static constexpr int media_hop_delay_ms = 50;
4506 static constexpr int signaling_trip_delay_ms = 500;
4507 // For explanation of these values, see comment above.
4508 static constexpr int required_media_hops = 9;
4509 static constexpr int required_signaling_trips = 2;
4510 // For internal delays (such as posting an event asychronously).
4511 static constexpr int allowed_internal_delay_ms = 20;
4512 static constexpr int total_connection_time_ms =
4513 media_hop_delay_ms * required_media_hops +
4514 signaling_trip_delay_ms * required_signaling_trips +
4515 allowed_internal_delay_ms;
4516
4517 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4518 3478};
4519 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4520 0};
4521 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4522 3478};
4523 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4524 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004525 cricket::TestTurnServer* turn_server_1 = CreateTurnServer(
4526 turn_server_1_internal_address, turn_server_1_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004527
Seth Hampsonaed71642018-06-11 07:41:32 -07004528 cricket::TestTurnServer* turn_server_2 = CreateTurnServer(
4529 turn_server_2_internal_address, turn_server_2_external_address);
deadbeef1dcb1642017-03-29 21:08:16 -07004530 // Bypass permission check on received packets so media can be sent before
4531 // the candidate is signaled.
Seth Hampsonaed71642018-06-11 07:41:32 -07004532 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_1] {
4533 turn_server_1->set_enable_permission_checks(false);
4534 });
4535 network_thread()->Invoke<void>(RTC_FROM_HERE, [turn_server_2] {
4536 turn_server_2->set_enable_permission_checks(false);
4537 });
deadbeef1dcb1642017-03-29 21:08:16 -07004538
4539 PeerConnectionInterface::RTCConfiguration client_1_config;
4540 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4541 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4542 ice_server_1.username = "test";
4543 ice_server_1.password = "test";
4544 client_1_config.servers.push_back(ice_server_1);
4545 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4546 client_1_config.presume_writable_when_fully_relayed = true;
4547
4548 PeerConnectionInterface::RTCConfiguration client_2_config;
4549 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4550 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4551 ice_server_2.username = "test";
4552 ice_server_2.password = "test";
4553 client_2_config.servers.push_back(ice_server_2);
4554 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4555 client_2_config.presume_writable_when_fully_relayed = true;
4556
4557 ASSERT_TRUE(
4558 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4559 // Set up the simulated delays.
4560 SetSignalingDelayMs(signaling_trip_delay_ms);
4561 ConnectFakeSignaling();
4562 virtual_socket_server()->set_delay_mean(media_hop_delay_ms);
4563 virtual_socket_server()->UpdateDelayDistribution();
4564
4565 // Set "offer to receive audio/video" without adding any tracks, so we just
4566 // set up ICE/DTLS with no media.
4567 PeerConnectionInterface::RTCOfferAnswerOptions options;
4568 options.offer_to_receive_audio = 1;
4569 options.offer_to_receive_video = 1;
4570 caller()->SetOfferAnswerOptions(options);
4571 caller()->CreateAndSetAndSignalOffer();
deadbeef71452802017-05-07 17:21:01 -07004572 EXPECT_TRUE_SIMULATED_WAIT(DtlsConnected(), total_connection_time_ms,
Yves Gerey100fe632020-01-17 19:15:53 +01004573 FakeClock());
Seth Hampson1d4a76d2018-06-19 14:31:41 -07004574 // Closing the PeerConnections destroys the ports before the ScopedFakeClock.
4575 // If this is not done a DCHECK can be hit in ports.cc, because a large
4576 // negative number is calculated for the rtt due to the global clock changing.
Steve Antond91969e2019-05-30 12:27:03 -07004577 ClosePeerConnections();
deadbeef1dcb1642017-03-29 21:08:16 -07004578}
4579
Jonas Orelandbdcee282017-10-10 14:01:40 +02004580// Verify that a TurnCustomizer passed in through RTCConfiguration
4581// is actually used by the underlying TURN candidate pair.
4582// Note that turnport_unittest.cc contains more detailed, lower-level tests.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004583TEST_P(PeerConnectionIntegrationTest, TurnCustomizerUsedForTurnConnections) {
Jonas Orelandbdcee282017-10-10 14:01:40 +02004584 static const rtc::SocketAddress turn_server_1_internal_address{"88.88.88.0",
4585 3478};
4586 static const rtc::SocketAddress turn_server_1_external_address{"88.88.88.1",
4587 0};
4588 static const rtc::SocketAddress turn_server_2_internal_address{"99.99.99.0",
4589 3478};
4590 static const rtc::SocketAddress turn_server_2_external_address{"99.99.99.1",
4591 0};
Seth Hampsonaed71642018-06-11 07:41:32 -07004592 CreateTurnServer(turn_server_1_internal_address,
4593 turn_server_1_external_address);
4594 CreateTurnServer(turn_server_2_internal_address,
4595 turn_server_2_external_address);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004596
4597 PeerConnectionInterface::RTCConfiguration client_1_config;
4598 webrtc::PeerConnectionInterface::IceServer ice_server_1;
4599 ice_server_1.urls.push_back("turn:88.88.88.0:3478");
4600 ice_server_1.username = "test";
4601 ice_server_1.password = "test";
4602 client_1_config.servers.push_back(ice_server_1);
4603 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004604 auto* customizer1 = CreateTurnCustomizer();
4605 client_1_config.turn_customizer = customizer1;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004606
4607 PeerConnectionInterface::RTCConfiguration client_2_config;
4608 webrtc::PeerConnectionInterface::IceServer ice_server_2;
4609 ice_server_2.urls.push_back("turn:99.99.99.0:3478");
4610 ice_server_2.username = "test";
4611 ice_server_2.password = "test";
4612 client_2_config.servers.push_back(ice_server_2);
4613 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
Seth Hampsonaed71642018-06-11 07:41:32 -07004614 auto* customizer2 = CreateTurnCustomizer();
4615 client_2_config.turn_customizer = customizer2;
Jonas Orelandbdcee282017-10-10 14:01:40 +02004616
4617 ASSERT_TRUE(
4618 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4619 ConnectFakeSignaling();
4620
4621 // Set "offer to receive audio/video" without adding any tracks, so we just
4622 // set up ICE/DTLS with no media.
4623 PeerConnectionInterface::RTCOfferAnswerOptions options;
4624 options.offer_to_receive_audio = 1;
4625 options.offer_to_receive_video = 1;
4626 caller()->SetOfferAnswerOptions(options);
4627 caller()->CreateAndSetAndSignalOffer();
4628 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4629
Seth Hampsonaed71642018-06-11 07:41:32 -07004630 ExpectTurnCustomizerCountersIncremented(customizer1);
4631 ExpectTurnCustomizerCountersIncremented(customizer2);
Jonas Orelandbdcee282017-10-10 14:01:40 +02004632}
4633
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004634// Verifies that you can use TCP instead of UDP to connect to a TURN server and
4635// send media between the caller and the callee.
4636TEST_P(PeerConnectionIntegrationTest, TCPUsedForTurnConnections) {
4637 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4638 3478};
4639 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4640
4641 // Enable TCP for the fake turn server.
Seth Hampsonaed71642018-06-11 07:41:32 -07004642 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4643 cricket::PROTO_TCP);
Benjamin Wright2d5f3cb2018-05-22 14:46:06 -07004644
4645 webrtc::PeerConnectionInterface::IceServer ice_server;
4646 ice_server.urls.push_back("turn:88.88.88.0:3478?transport=tcp");
4647 ice_server.username = "test";
4648 ice_server.password = "test";
4649
4650 PeerConnectionInterface::RTCConfiguration client_1_config;
4651 client_1_config.servers.push_back(ice_server);
4652 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4653
4654 PeerConnectionInterface::RTCConfiguration client_2_config;
4655 client_2_config.servers.push_back(ice_server);
4656 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4657
4658 ASSERT_TRUE(
4659 CreatePeerConnectionWrappersWithConfig(client_1_config, client_2_config));
4660
4661 // Do normal offer/answer and wait for ICE to complete.
4662 ConnectFakeSignaling();
4663 caller()->AddAudioVideoTracks();
4664 callee()->AddAudioVideoTracks();
4665 caller()->CreateAndSetAndSignalOffer();
4666 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4667 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
4668 callee()->ice_connection_state(), kMaxWaitForFramesMs);
4669
4670 MediaExpectations media_expectations;
4671 media_expectations.ExpectBidirectionalAudioAndVideo();
4672 EXPECT_TRUE(ExpectNewFrames(media_expectations));
4673}
4674
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004675// Verify that a SSLCertificateVerifier passed in through
4676// PeerConnectionDependencies is actually used by the underlying SSL
4677// implementation to determine whether a certificate presented by the TURN
4678// server is accepted by the client. Note that openssladapter_unittest.cc
4679// contains more detailed, lower-level tests.
4680TEST_P(PeerConnectionIntegrationTest,
4681 SSLCertificateVerifierUsedForTurnConnections) {
4682 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4683 3478};
4684 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4685
4686 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4687 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004688 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4689 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004690
4691 webrtc::PeerConnectionInterface::IceServer ice_server;
4692 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4693 ice_server.username = "test";
4694 ice_server.password = "test";
4695
4696 PeerConnectionInterface::RTCConfiguration client_1_config;
4697 client_1_config.servers.push_back(ice_server);
4698 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4699
4700 PeerConnectionInterface::RTCConfiguration client_2_config;
4701 client_2_config.servers.push_back(ice_server);
4702 // Setting the type to kRelay forces the connection to go through a TURN
4703 // server.
4704 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4705
4706 // Get a copy to the pointer so we can verify calls later.
4707 rtc::TestCertificateVerifier* client_1_cert_verifier =
4708 new rtc::TestCertificateVerifier();
4709 client_1_cert_verifier->verify_certificate_ = true;
4710 rtc::TestCertificateVerifier* client_2_cert_verifier =
4711 new rtc::TestCertificateVerifier();
4712 client_2_cert_verifier->verify_certificate_ = true;
4713
4714 // Create the dependencies with the test certificate verifier.
4715 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4716 client_1_deps.tls_cert_verifier =
4717 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4718 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4719 client_2_deps.tls_cert_verifier =
4720 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4721
4722 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4723 client_1_config, std::move(client_1_deps), client_2_config,
4724 std::move(client_2_deps)));
4725 ConnectFakeSignaling();
4726
4727 // Set "offer to receive audio/video" without adding any tracks, so we just
4728 // set up ICE/DTLS with no media.
4729 PeerConnectionInterface::RTCOfferAnswerOptions options;
4730 options.offer_to_receive_audio = 1;
4731 options.offer_to_receive_video = 1;
4732 caller()->SetOfferAnswerOptions(options);
4733 caller()->CreateAndSetAndSignalOffer();
4734 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
4735
4736 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4737 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004738}
4739
4740TEST_P(PeerConnectionIntegrationTest,
4741 SSLCertificateVerifierFailureUsedForTurnConnectionsFailsConnection) {
4742 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
4743 3478};
4744 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
4745
4746 // Enable TCP-TLS for the fake turn server. We need to pass in 88.88.88.0 so
4747 // that host name verification passes on the fake certificate.
Seth Hampsonaed71642018-06-11 07:41:32 -07004748 CreateTurnServer(turn_server_internal_address, turn_server_external_address,
4749 cricket::PROTO_TLS, "88.88.88.0");
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004750
4751 webrtc::PeerConnectionInterface::IceServer ice_server;
4752 ice_server.urls.push_back("turns:88.88.88.0:3478?transport=tcp");
4753 ice_server.username = "test";
4754 ice_server.password = "test";
4755
4756 PeerConnectionInterface::RTCConfiguration client_1_config;
4757 client_1_config.servers.push_back(ice_server);
4758 client_1_config.type = webrtc::PeerConnectionInterface::kRelay;
4759
4760 PeerConnectionInterface::RTCConfiguration client_2_config;
4761 client_2_config.servers.push_back(ice_server);
4762 // Setting the type to kRelay forces the connection to go through a TURN
4763 // server.
4764 client_2_config.type = webrtc::PeerConnectionInterface::kRelay;
4765
4766 // Get a copy to the pointer so we can verify calls later.
4767 rtc::TestCertificateVerifier* client_1_cert_verifier =
4768 new rtc::TestCertificateVerifier();
4769 client_1_cert_verifier->verify_certificate_ = false;
4770 rtc::TestCertificateVerifier* client_2_cert_verifier =
4771 new rtc::TestCertificateVerifier();
4772 client_2_cert_verifier->verify_certificate_ = false;
4773
4774 // Create the dependencies with the test certificate verifier.
4775 webrtc::PeerConnectionDependencies client_1_deps(nullptr);
4776 client_1_deps.tls_cert_verifier =
4777 std::unique_ptr<rtc::TestCertificateVerifier>(client_1_cert_verifier);
4778 webrtc::PeerConnectionDependencies client_2_deps(nullptr);
4779 client_2_deps.tls_cert_verifier =
4780 std::unique_ptr<rtc::TestCertificateVerifier>(client_2_cert_verifier);
4781
4782 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfigAndDeps(
4783 client_1_config, std::move(client_1_deps), client_2_config,
4784 std::move(client_2_deps)));
4785 ConnectFakeSignaling();
4786
4787 // Set "offer to receive audio/video" without adding any tracks, so we just
4788 // set up ICE/DTLS with no media.
4789 PeerConnectionInterface::RTCOfferAnswerOptions options;
4790 options.offer_to_receive_audio = 1;
4791 options.offer_to_receive_video = 1;
4792 caller()->SetOfferAnswerOptions(options);
4793 caller()->CreateAndSetAndSignalOffer();
4794 bool wait_res = true;
4795 // TODO(bugs.webrtc.org/9219): When IceConnectionState is implemented
4796 // properly, should be able to just wait for a state of "failed" instead of
4797 // waiting a fixed 10 seconds.
4798 WAIT_(DtlsConnected(), kDefaultTimeout, wait_res);
4799 ASSERT_FALSE(wait_res);
4800
4801 EXPECT_GT(client_1_cert_verifier->call_count_, 0u);
4802 EXPECT_GT(client_2_cert_verifier->call_count_, 0u);
Benjamin Wrightd6f86e82018-05-08 13:12:25 -07004803}
4804
Qingsi Wang25ec8882019-11-15 12:33:05 -08004805// Test that the injected ICE transport factory is used to create ICE transports
4806// for WebRTC connections.
4807TEST_P(PeerConnectionIntegrationTest, IceTransportFactoryUsedForConnections) {
4808 PeerConnectionInterface::RTCConfiguration default_config;
4809 PeerConnectionDependencies dependencies(nullptr);
4810 auto ice_transport_factory = std::make_unique<MockIceTransportFactory>();
4811 EXPECT_CALL(*ice_transport_factory, RecordIceTransportCreated()).Times(1);
4812 dependencies.ice_transport_factory = std::move(ice_transport_factory);
Niels Möller2a707032020-06-16 16:39:13 +02004813 auto wrapper = CreatePeerConnectionWrapper("Caller", nullptr, &default_config,
4814 std::move(dependencies), nullptr,
4815 /*reset_encoder_factory=*/false,
4816 /*reset_decoder_factory=*/false);
Qingsi Wang25ec8882019-11-15 12:33:05 -08004817 ASSERT_TRUE(wrapper);
4818 wrapper->CreateDataChannel();
4819 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
4820 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
4821 wrapper->pc()->SetLocalDescription(observer,
4822 wrapper->CreateOfferAndWait().release());
4823}
4824
deadbeefc964d0b2017-04-03 10:03:35 -07004825// Test that audio and video flow end-to-end when codec names don't use the
4826// expected casing, given that they're supposed to be case insensitive. To test
4827// this, all but one codec is removed from each media description, and its
4828// casing is changed.
4829//
4830// In the past, this has regressed and caused crashes/black video, due to the
4831// fact that code at some layers was doing case-insensitive comparisons and
4832// code at other layers was not.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004833TEST_P(PeerConnectionIntegrationTest, CodecNamesAreCaseInsensitive) {
deadbeefc964d0b2017-04-03 10:03:35 -07004834 ASSERT_TRUE(CreatePeerConnectionWrappers());
4835 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004836 caller()->AddAudioVideoTracks();
4837 callee()->AddAudioVideoTracks();
deadbeefc964d0b2017-04-03 10:03:35 -07004838
4839 // Remove all but one audio/video codec (opus and VP8), and change the
4840 // casing of the caller's generated offer.
4841 caller()->SetGeneratedSdpMunger([](cricket::SessionDescription* description) {
4842 cricket::AudioContentDescription* audio =
4843 GetFirstAudioContentDescription(description);
4844 ASSERT_NE(nullptr, audio);
4845 auto audio_codecs = audio->codecs();
4846 audio_codecs.erase(std::remove_if(audio_codecs.begin(), audio_codecs.end(),
4847 [](const cricket::AudioCodec& codec) {
4848 return codec.name != "opus";
4849 }),
4850 audio_codecs.end());
4851 ASSERT_EQ(1u, audio_codecs.size());
4852 audio_codecs[0].name = "OpUs";
4853 audio->set_codecs(audio_codecs);
4854
4855 cricket::VideoContentDescription* video =
4856 GetFirstVideoContentDescription(description);
4857 ASSERT_NE(nullptr, video);
4858 auto video_codecs = video->codecs();
4859 video_codecs.erase(std::remove_if(video_codecs.begin(), video_codecs.end(),
4860 [](const cricket::VideoCodec& codec) {
4861 return codec.name != "VP8";
4862 }),
4863 video_codecs.end());
4864 ASSERT_EQ(1u, video_codecs.size());
4865 video_codecs[0].name = "vP8";
4866 video->set_codecs(video_codecs);
4867 });
4868
4869 caller()->CreateAndSetAndSignalOffer();
4870 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4871
4872 // Verify frames are still received end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004873 MediaExpectations media_expectations;
4874 media_expectations.ExpectBidirectionalAudioAndVideo();
4875 ASSERT_TRUE(ExpectNewFrames(media_expectations));
deadbeefc964d0b2017-04-03 10:03:35 -07004876}
4877
Jonas Oreland49ac5952018-09-26 16:04:32 +02004878TEST_P(PeerConnectionIntegrationTest, GetSourcesAudio) {
hbos8d609f62017-04-10 07:39:05 -07004879 ASSERT_TRUE(CreatePeerConnectionWrappers());
4880 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08004881 caller()->AddAudioTrack();
hbos8d609f62017-04-10 07:39:05 -07004882 caller()->CreateAndSetAndSignalOffer();
4883 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
deadbeefd8ad7882017-04-18 16:01:17 -07004884 // Wait for one audio frame to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004885 MediaExpectations media_expectations;
4886 media_expectations.CalleeExpectsSomeAudio(1);
4887 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Jonas Oreland49ac5952018-09-26 16:04:32 +02004888 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
hbos8d609f62017-04-10 07:39:05 -07004889 auto receiver = callee()->pc()->GetReceivers()[0];
4890 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_AUDIO);
Jonas Oreland49ac5952018-09-26 16:04:32 +02004891 auto sources = receiver->GetSources();
hbos8d609f62017-04-10 07:39:05 -07004892 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
4893 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
Jonas Oreland49ac5952018-09-26 16:04:32 +02004894 sources[0].source_id());
4895 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
4896}
4897
4898TEST_P(PeerConnectionIntegrationTest, GetSourcesVideo) {
4899 ASSERT_TRUE(CreatePeerConnectionWrappers());
4900 ConnectFakeSignaling();
4901 caller()->AddVideoTrack();
4902 caller()->CreateAndSetAndSignalOffer();
4903 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4904 // Wait for one video frame to be received by the callee.
4905 MediaExpectations media_expectations;
4906 media_expectations.CalleeExpectsSomeVideo(1);
4907 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4908 ASSERT_EQ(callee()->pc()->GetReceivers().size(), 1u);
4909 auto receiver = callee()->pc()->GetReceivers()[0];
4910 ASSERT_EQ(receiver->media_type(), cricket::MEDIA_TYPE_VIDEO);
4911 auto sources = receiver->GetSources();
4912 ASSERT_GT(receiver->GetParameters().encodings.size(), 0u);
Yves Gereyf781bb52019-07-23 19:15:39 +02004913 ASSERT_GT(sources.size(), 0u);
Jonas Oreland49ac5952018-09-26 16:04:32 +02004914 EXPECT_EQ(receiver->GetParameters().encodings[0].ssrc,
4915 sources[0].source_id());
4916 EXPECT_EQ(webrtc::RtpSourceType::SSRC, sources[0].source_type());
hbos8d609f62017-04-10 07:39:05 -07004917}
4918
deadbeef2f425aa2017-04-14 10:41:32 -07004919// Test that if a track is removed and added again with a different stream ID,
4920// the new stream ID is successfully communicated in SDP and media continues to
4921// flow end-to-end.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004922// TODO(webrtc.bugs.org/8734): This test does not work for Unified Plan because
4923// it will not reuse a transceiver that has already been sending. After creating
4924// a new transceiver it tries to create an offer with two senders of the same
4925// track ids and it fails.
4926TEST_F(PeerConnectionIntegrationTestPlanB, RemoveAndAddTrackWithNewStreamId) {
deadbeef2f425aa2017-04-14 10:41:32 -07004927 ASSERT_TRUE(CreatePeerConnectionWrappers());
4928 ConnectFakeSignaling();
4929
deadbeef2f425aa2017-04-14 10:41:32 -07004930 // Add track using stream 1, do offer/answer.
4931 rtc::scoped_refptr<webrtc::AudioTrackInterface> track =
4932 caller()->CreateLocalAudioTrack();
4933 rtc::scoped_refptr<webrtc::RtpSenderInterface> sender =
Steve Antond78323f2018-07-11 11:13:44 -07004934 caller()->AddTrack(track, {"stream_1"});
deadbeef2f425aa2017-04-14 10:41:32 -07004935 caller()->CreateAndSetAndSignalOffer();
4936 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08004937 {
4938 MediaExpectations media_expectations;
4939 media_expectations.CalleeExpectsSomeAudio(1);
4940 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4941 }
deadbeef2f425aa2017-04-14 10:41:32 -07004942 // Remove the sender, and create a new one with the new stream.
4943 caller()->pc()->RemoveTrack(sender);
Steve Antond78323f2018-07-11 11:13:44 -07004944 sender = caller()->AddTrack(track, {"stream_2"});
deadbeef2f425aa2017-04-14 10:41:32 -07004945 caller()->CreateAndSetAndSignalOffer();
4946 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4947 // Wait for additional audio frames to be received by the callee.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004948 {
4949 MediaExpectations media_expectations;
4950 media_expectations.CalleeExpectsSomeAudio();
4951 ASSERT_TRUE(ExpectNewFrames(media_expectations));
4952 }
deadbeef2f425aa2017-04-14 10:41:32 -07004953}
4954
Seth Hampson2f0d7022018-02-20 11:54:42 -08004955TEST_P(PeerConnectionIntegrationTest, RtcEventLogOutputWriteCalled) {
Elad Alon99c3fe52017-10-13 16:29:40 +02004956 ASSERT_TRUE(CreatePeerConnectionWrappers());
4957 ConnectFakeSignaling();
4958
Mirko Bonadei317a1f02019-09-17 17:06:18 +02004959 auto output = std::make_unique<testing::NiceMock<MockRtcEventLogOutput>>();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02004960 ON_CALL(*output, IsActive()).WillByDefault(::testing::Return(true));
4961 ON_CALL(*output, Write(::testing::_)).WillByDefault(::testing::Return(true));
Elad Alon99c3fe52017-10-13 16:29:40 +02004962 EXPECT_CALL(*output, Write(::testing::_)).Times(::testing::AtLeast(1));
Bjorn Tereliusde939432017-11-20 17:38:14 +01004963 EXPECT_TRUE(caller()->pc()->StartRtcEventLog(
4964 std::move(output), webrtc::RtcEventLog::kImmediateOutput));
Elad Alon99c3fe52017-10-13 16:29:40 +02004965
Steve Anton15324772018-01-16 10:26:49 -08004966 caller()->AddAudioVideoTracks();
Elad Alon99c3fe52017-10-13 16:29:40 +02004967 caller()->CreateAndSetAndSignalOffer();
4968 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4969}
4970
Steve Antonede9ca52017-10-16 13:04:27 -07004971// Test that if candidates are only signaled by applying full session
4972// descriptions (instead of using AddIceCandidate), the peers can connect to
4973// each other and exchange media.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004974TEST_P(PeerConnectionIntegrationTest, MediaFlowsWhenCandidatesSetOnlyInSdp) {
Steve Antonede9ca52017-10-16 13:04:27 -07004975 ASSERT_TRUE(CreatePeerConnectionWrappers());
4976 // Each side will signal the session descriptions but not candidates.
4977 ConnectFakeSignalingForSdpOnly();
4978
4979 // Add audio video track and exchange the initial offer/answer with media
4980 // information only. This will start ICE gathering on each side.
Steve Anton15324772018-01-16 10:26:49 -08004981 caller()->AddAudioVideoTracks();
4982 callee()->AddAudioVideoTracks();
Steve Antonede9ca52017-10-16 13:04:27 -07004983 caller()->CreateAndSetAndSignalOffer();
4984
4985 // Wait for all candidates to be gathered on both the caller and callee.
4986 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4987 caller()->ice_gathering_state(), kDefaultTimeout);
4988 ASSERT_EQ_WAIT(PeerConnectionInterface::kIceGatheringComplete,
4989 callee()->ice_gathering_state(), kDefaultTimeout);
4990
4991 // The candidates will now be included in the session description, so
4992 // signaling them will start the ICE connection.
4993 caller()->CreateAndSetAndSignalOffer();
4994 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
4995
4996 // Ensure that media flows in both directions.
Seth Hampson2f0d7022018-02-20 11:54:42 -08004997 MediaExpectations media_expectations;
4998 media_expectations.ExpectBidirectionalAudioAndVideo();
4999 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Antonede9ca52017-10-16 13:04:27 -07005000}
5001
henrika5f6bf242017-11-01 11:06:56 +01005002// Test that SetAudioPlayout can be used to disable audio playout from the
5003// start, then later enable it. This may be useful, for example, if the caller
5004// needs to play a local ringtone until some event occurs, after which it
5005// switches to playing the received audio.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005006TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioPlayout) {
henrika5f6bf242017-11-01 11:06:56 +01005007 ASSERT_TRUE(CreatePeerConnectionWrappers());
5008 ConnectFakeSignaling();
5009
5010 // Set up audio-only call where audio playout is disabled on caller's side.
5011 caller()->pc()->SetAudioPlayout(false);
Steve Anton15324772018-01-16 10:26:49 -08005012 caller()->AddAudioTrack();
5013 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01005014 caller()->CreateAndSetAndSignalOffer();
5015 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5016
5017 // Pump messages for a second.
5018 WAIT(false, 1000);
5019 // Since audio playout is disabled, the caller shouldn't have received
5020 // anything (at the playout level, at least).
5021 EXPECT_EQ(0, caller()->audio_frames_received());
5022 // As a sanity check, make sure the callee (for which playout isn't disabled)
5023 // did still see frames on its audio level.
5024 ASSERT_GT(callee()->audio_frames_received(), 0);
5025
5026 // Enable playout again, and ensure audio starts flowing.
5027 caller()->pc()->SetAudioPlayout(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005028 MediaExpectations media_expectations;
5029 media_expectations.ExpectBidirectionalAudio();
5030 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika5f6bf242017-11-01 11:06:56 +01005031}
5032
5033double GetAudioEnergyStat(PeerConnectionWrapper* pc) {
5034 auto report = pc->NewGetStats();
5035 auto track_stats_list =
5036 report->GetStatsOfType<webrtc::RTCMediaStreamTrackStats>();
5037 const webrtc::RTCMediaStreamTrackStats* remote_track_stats = nullptr;
5038 for (const auto* track_stats : track_stats_list) {
5039 if (track_stats->remote_source.is_defined() &&
5040 *track_stats->remote_source) {
5041 remote_track_stats = track_stats;
5042 break;
5043 }
5044 }
5045
5046 if (!remote_track_stats->total_audio_energy.is_defined()) {
5047 return 0.0;
5048 }
5049 return *remote_track_stats->total_audio_energy;
5050}
5051
5052// Test that if audio playout is disabled via the SetAudioPlayout() method, then
5053// incoming audio is still processed and statistics are generated.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005054TEST_P(PeerConnectionIntegrationTest,
henrika5f6bf242017-11-01 11:06:56 +01005055 DisableAudioPlayoutStillGeneratesAudioStats) {
5056 ASSERT_TRUE(CreatePeerConnectionWrappers());
5057 ConnectFakeSignaling();
5058
5059 // Set up audio-only call where playout is disabled but audio-processing is
5060 // still active.
Steve Anton15324772018-01-16 10:26:49 -08005061 caller()->AddAudioTrack();
5062 callee()->AddAudioTrack();
henrika5f6bf242017-11-01 11:06:56 +01005063 caller()->pc()->SetAudioPlayout(false);
5064
5065 caller()->CreateAndSetAndSignalOffer();
5066 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5067
5068 // Wait for the callee to receive audio stats.
5069 EXPECT_TRUE_WAIT(GetAudioEnergyStat(caller()) > 0, kMaxWaitForFramesMs);
5070}
5071
henrika4f167df2017-11-01 14:45:55 +01005072// Test that SetAudioRecording can be used to disable audio recording from the
5073// start, then later enable it. This may be useful, for example, if the caller
5074// wants to ensure that no audio resources are active before a certain state
5075// is reached.
Seth Hampson2f0d7022018-02-20 11:54:42 -08005076TEST_P(PeerConnectionIntegrationTest, DisableAndEnableAudioRecording) {
henrika4f167df2017-11-01 14:45:55 +01005077 ASSERT_TRUE(CreatePeerConnectionWrappers());
5078 ConnectFakeSignaling();
5079
5080 // Set up audio-only call where audio recording is disabled on caller's side.
5081 caller()->pc()->SetAudioRecording(false);
Steve Anton15324772018-01-16 10:26:49 -08005082 caller()->AddAudioTrack();
5083 callee()->AddAudioTrack();
henrika4f167df2017-11-01 14:45:55 +01005084 caller()->CreateAndSetAndSignalOffer();
5085 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5086
5087 // Pump messages for a second.
5088 WAIT(false, 1000);
5089 // Since caller has disabled audio recording, the callee shouldn't have
5090 // received anything.
5091 EXPECT_EQ(0, callee()->audio_frames_received());
5092 // As a sanity check, make sure the caller did still see frames on its
5093 // audio level since audio recording is enabled on the calle side.
5094 ASSERT_GT(caller()->audio_frames_received(), 0);
5095
5096 // Enable audio recording again, and ensure audio starts flowing.
5097 caller()->pc()->SetAudioRecording(true);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005098 MediaExpectations media_expectations;
5099 media_expectations.ExpectBidirectionalAudio();
5100 ASSERT_TRUE(ExpectNewFrames(media_expectations));
henrika4f167df2017-11-01 14:45:55 +01005101}
5102
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005103// Test that after closing PeerConnections, they stop sending any packets (ICE,
5104// DTLS, RTP...).
Seth Hampson2f0d7022018-02-20 11:54:42 -08005105TEST_P(PeerConnectionIntegrationTest, ClosingConnectionStopsPacketFlow) {
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005106 // Set up audio/video/data, wait for some frames to be received.
5107 ASSERT_TRUE(CreatePeerConnectionWrappers());
5108 ConnectFakeSignaling();
Steve Anton15324772018-01-16 10:26:49 -08005109 caller()->AddAudioVideoTracks();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005110#ifdef HAVE_SCTP
5111 caller()->CreateDataChannel();
5112#endif
5113 caller()->CreateAndSetAndSignalOffer();
5114 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Seth Hampson2f0d7022018-02-20 11:54:42 -08005115 MediaExpectations media_expectations;
5116 media_expectations.CalleeExpectsSomeAudioAndVideo();
5117 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005118 // Close PeerConnections.
Steve Antond91969e2019-05-30 12:27:03 -07005119 ClosePeerConnections();
Taylor Brandstetter389a97c2018-01-03 16:26:06 -08005120 // Pump messages for a second, and ensure no new packets end up sent.
5121 uint32_t sent_packets_a = virtual_socket_server()->sent_packets();
5122 WAIT(false, 1000);
5123 uint32_t sent_packets_b = virtual_socket_server()->sent_packets();
5124 EXPECT_EQ(sent_packets_a, sent_packets_b);
5125}
5126
Steve Anton7eca0932018-03-30 15:18:41 -07005127// Test that transport stats are generated by the RTCStatsCollector for a
5128// connection that only involves data channels. This is a regression test for
5129// crbug.com/826972.
5130#ifdef HAVE_SCTP
5131TEST_P(PeerConnectionIntegrationTest,
5132 TransportStatsReportedForDataChannelOnlyConnection) {
5133 ASSERT_TRUE(CreatePeerConnectionWrappers());
5134 ConnectFakeSignaling();
5135 caller()->CreateDataChannel();
5136
5137 caller()->CreateAndSetAndSignalOffer();
5138 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5139 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5140
5141 auto caller_report = caller()->NewGetStats();
5142 EXPECT_EQ(1u, caller_report->GetStatsOfType<RTCTransportStats>().size());
5143 auto callee_report = callee()->NewGetStats();
5144 EXPECT_EQ(1u, callee_report->GetStatsOfType<RTCTransportStats>().size());
5145}
5146#endif // HAVE_SCTP
5147
Qingsi Wang7685e862018-06-11 20:15:46 -07005148TEST_P(PeerConnectionIntegrationTest,
5149 IceEventsGeneratedAndLoggedInRtcEventLog) {
5150 ASSERT_TRUE(CreatePeerConnectionWrappersWithFakeRtcEventLog());
5151 ConnectFakeSignaling();
5152 PeerConnectionInterface::RTCOfferAnswerOptions options;
5153 options.offer_to_receive_audio = 1;
5154 caller()->SetOfferAnswerOptions(options);
5155 caller()->CreateAndSetAndSignalOffer();
5156 ASSERT_TRUE_WAIT(DtlsConnected(), kDefaultTimeout);
5157 ASSERT_NE(nullptr, caller()->event_log_factory());
5158 ASSERT_NE(nullptr, callee()->event_log_factory());
5159 webrtc::FakeRtcEventLog* caller_event_log =
5160 static_cast<webrtc::FakeRtcEventLog*>(
5161 caller()->event_log_factory()->last_log_created());
5162 webrtc::FakeRtcEventLog* callee_event_log =
5163 static_cast<webrtc::FakeRtcEventLog*>(
5164 callee()->event_log_factory()->last_log_created());
5165 ASSERT_NE(nullptr, caller_event_log);
5166 ASSERT_NE(nullptr, callee_event_log);
5167 int caller_ice_config_count = caller_event_log->GetEventCount(
5168 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5169 int caller_ice_event_count = caller_event_log->GetEventCount(
5170 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5171 int callee_ice_config_count = callee_event_log->GetEventCount(
5172 webrtc::RtcEvent::Type::IceCandidatePairConfig);
5173 int callee_ice_event_count = callee_event_log->GetEventCount(
5174 webrtc::RtcEvent::Type::IceCandidatePairEvent);
5175 EXPECT_LT(0, caller_ice_config_count);
5176 EXPECT_LT(0, caller_ice_event_count);
5177 EXPECT_LT(0, callee_ice_config_count);
5178 EXPECT_LT(0, callee_ice_event_count);
5179}
5180
Qingsi Wangc129c352019-04-18 10:41:58 -07005181TEST_P(PeerConnectionIntegrationTest, RegatherAfterChangingIceTransportType) {
Qingsi Wangc129c352019-04-18 10:41:58 -07005182 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5183 3478};
5184 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5185
5186 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
5187
5188 webrtc::PeerConnectionInterface::IceServer ice_server;
5189 ice_server.urls.push_back("turn:88.88.88.0:3478");
5190 ice_server.username = "test";
5191 ice_server.password = "test";
5192
5193 PeerConnectionInterface::RTCConfiguration caller_config;
5194 caller_config.servers.push_back(ice_server);
5195 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5196 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07005197 caller_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07005198
5199 PeerConnectionInterface::RTCConfiguration callee_config;
5200 callee_config.servers.push_back(ice_server);
5201 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5202 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
Qingsi Wang1fe119f2019-05-31 16:55:33 -07005203 callee_config.surface_ice_candidates_on_ice_transport_type_changed = true;
Qingsi Wangc129c352019-04-18 10:41:58 -07005204
5205 ASSERT_TRUE(
5206 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5207
5208 // Do normal offer/answer and wait for ICE to complete.
5209 ConnectFakeSignaling();
5210 caller()->AddAudioVideoTracks();
5211 callee()->AddAudioVideoTracks();
5212 caller()->CreateAndSetAndSignalOffer();
5213 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5214 // Since we are doing continual gathering, the ICE transport does not reach
5215 // kIceGatheringComplete (see
5216 // P2PTransportChannel::OnCandidatesAllocationDone), and consequently not
5217 // kIceConnectionComplete.
5218 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5219 caller()->ice_connection_state(), kDefaultTimeout);
5220 EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
5221 callee()->ice_connection_state(), kDefaultTimeout);
5222 // Note that we cannot use the metric
5223 // |WebRTC.PeerConnection.CandidatePairType_UDP| in this test since this
5224 // metric is only populated when we reach kIceConnectionComplete in the
5225 // current implementation.
5226 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5227 caller()->last_candidate_gathered().type());
5228 EXPECT_EQ(cricket::RELAY_PORT_TYPE,
5229 callee()->last_candidate_gathered().type());
5230
5231 // Loosen the caller's candidate filter.
5232 caller_config = caller()->pc()->GetConfiguration();
5233 caller_config.type = webrtc::PeerConnectionInterface::kAll;
5234 caller()->pc()->SetConfiguration(caller_config);
5235 // We should have gathered a new host candidate.
5236 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5237 caller()->last_candidate_gathered().type(), kDefaultTimeout);
5238
5239 // Loosen the callee's candidate filter.
5240 callee_config = callee()->pc()->GetConfiguration();
5241 callee_config.type = webrtc::PeerConnectionInterface::kAll;
5242 callee()->pc()->SetConfiguration(callee_config);
5243 EXPECT_EQ_WAIT(cricket::LOCAL_PORT_TYPE,
5244 callee()->last_candidate_gathered().type(), kDefaultTimeout);
Jonas Orelande3096512020-05-27 09:01:05 +02005245
5246 // Create an offer and verify that it does not contain an ICE restart (i.e new
5247 // ice credentials).
5248 std::string caller_ufrag_pre_offer = caller()
5249 ->pc()
5250 ->local_description()
5251 ->description()
5252 ->transport_infos()[0]
5253 .description.ice_ufrag;
5254 caller()->CreateAndSetAndSignalOffer();
5255 std::string caller_ufrag_post_offer = caller()
5256 ->pc()
5257 ->local_description()
5258 ->description()
5259 ->transport_infos()[0]
5260 .description.ice_ufrag;
5261 EXPECT_EQ(caller_ufrag_pre_offer, caller_ufrag_post_offer);
Qingsi Wangc129c352019-04-18 10:41:58 -07005262}
5263
Eldar Relloda13ea22019-06-01 12:23:43 +03005264TEST_P(PeerConnectionIntegrationTest, OnIceCandidateError) {
Eldar Relloda13ea22019-06-01 12:23:43 +03005265 static const rtc::SocketAddress turn_server_internal_address{"88.88.88.0",
5266 3478};
5267 static const rtc::SocketAddress turn_server_external_address{"88.88.88.1", 0};
5268
5269 CreateTurnServer(turn_server_internal_address, turn_server_external_address);
5270
5271 webrtc::PeerConnectionInterface::IceServer ice_server;
5272 ice_server.urls.push_back("turn:88.88.88.0:3478");
5273 ice_server.username = "test";
5274 ice_server.password = "123";
5275
5276 PeerConnectionInterface::RTCConfiguration caller_config;
5277 caller_config.servers.push_back(ice_server);
5278 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5279 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5280
5281 PeerConnectionInterface::RTCConfiguration callee_config;
5282 callee_config.servers.push_back(ice_server);
5283 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5284 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5285
5286 ASSERT_TRUE(
5287 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5288
5289 // Do normal offer/answer and wait for ICE to complete.
5290 ConnectFakeSignaling();
5291 caller()->AddAudioVideoTracks();
5292 callee()->AddAudioVideoTracks();
5293 caller()->CreateAndSetAndSignalOffer();
5294 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5295 EXPECT_EQ_WAIT(401, caller()->error_event().error_code, kDefaultTimeout);
5296 EXPECT_EQ("Unauthorized", caller()->error_event().error_text);
5297 EXPECT_EQ("turn:88.88.88.0:3478?transport=udp", caller()->error_event().url);
Eldar Rello0095d372019-12-02 22:22:07 +02005298 EXPECT_NE(caller()->error_event().address, "");
Eldar Relloda13ea22019-06-01 12:23:43 +03005299}
5300
Eldar Rellofa8019c2020-05-14 11:59:33 +03005301TEST_P(PeerConnectionIntegrationTest, OnIceCandidateErrorWithEmptyAddress) {
5302 webrtc::PeerConnectionInterface::IceServer ice_server;
5303 ice_server.urls.push_back("turn:127.0.0.1:3478?transport=tcp");
5304 ice_server.username = "test";
5305 ice_server.password = "test";
5306
5307 PeerConnectionInterface::RTCConfiguration caller_config;
5308 caller_config.servers.push_back(ice_server);
5309 caller_config.type = webrtc::PeerConnectionInterface::kRelay;
5310 caller_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5311
5312 PeerConnectionInterface::RTCConfiguration callee_config;
5313 callee_config.servers.push_back(ice_server);
5314 callee_config.type = webrtc::PeerConnectionInterface::kRelay;
5315 callee_config.continual_gathering_policy = PeerConnection::GATHER_CONTINUALLY;
5316
5317 ASSERT_TRUE(
5318 CreatePeerConnectionWrappersWithConfig(caller_config, callee_config));
5319
5320 // Do normal offer/answer and wait for ICE to complete.
5321 ConnectFakeSignaling();
5322 caller()->AddAudioVideoTracks();
5323 callee()->AddAudioVideoTracks();
5324 caller()->CreateAndSetAndSignalOffer();
5325 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5326 EXPECT_EQ_WAIT(701, caller()->error_event().error_code, kDefaultTimeout);
5327 EXPECT_EQ(caller()->error_event().address, "");
5328}
5329
Eldar Rello5ab79e62019-10-09 18:29:44 +03005330TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5331 AudioKeepsFlowingAfterImplicitRollback) {
5332 PeerConnectionInterface::RTCConfiguration config;
5333 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
5334 config.enable_implicit_rollback = true;
5335 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5336 ConnectFakeSignaling();
5337 caller()->AddAudioTrack();
5338 callee()->AddAudioTrack();
5339 caller()->CreateAndSetAndSignalOffer();
5340 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5341 MediaExpectations media_expectations;
5342 media_expectations.ExpectBidirectionalAudio();
5343 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5344 SetSignalIceCandidates(false); // Workaround candidate outrace sdp.
5345 caller()->AddVideoTrack();
5346 callee()->AddVideoTrack();
5347 rtc::scoped_refptr<MockSetSessionDescriptionObserver> observer(
5348 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
5349 callee()->pc()->SetLocalDescription(observer,
5350 callee()->CreateOfferAndWait().release());
5351 EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
5352 caller()->CreateAndSetAndSignalOffer(); // Implicit rollback.
5353 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5354 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5355}
5356
5357TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5358 ImplicitRollbackVisitsStableState) {
5359 RTCConfiguration config;
5360 config.sdp_semantics = SdpSemantics::kUnifiedPlan;
5361 config.enable_implicit_rollback = true;
5362
5363 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5364
5365 rtc::scoped_refptr<MockSetSessionDescriptionObserver> sld_observer(
5366 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
5367 callee()->pc()->SetLocalDescription(sld_observer,
5368 callee()->CreateOfferAndWait().release());
5369 EXPECT_TRUE_WAIT(sld_observer->called(), kDefaultTimeout);
5370 EXPECT_EQ(sld_observer->error(), "");
5371
5372 rtc::scoped_refptr<MockSetSessionDescriptionObserver> srd_observer(
5373 new rtc::RefCountedObject<MockSetSessionDescriptionObserver>());
5374 callee()->pc()->SetRemoteDescription(
5375 srd_observer, caller()->CreateOfferAndWait().release());
5376 EXPECT_TRUE_WAIT(srd_observer->called(), kDefaultTimeout);
5377 EXPECT_EQ(srd_observer->error(), "");
5378
5379 EXPECT_THAT(callee()->peer_connection_signaling_state_history(),
5380 ElementsAre(PeerConnectionInterface::kHaveLocalOffer,
5381 PeerConnectionInterface::kStable,
5382 PeerConnectionInterface::kHaveRemoteOffer));
5383}
5384
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005385INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
5386 PeerConnectionIntegrationTest,
5387 Values(SdpSemantics::kPlanB,
5388 SdpSemantics::kUnifiedPlan));
Steve Antond3679212018-01-17 17:41:02 -08005389
Yves Gerey100fe632020-01-17 19:15:53 +01005390INSTANTIATE_TEST_SUITE_P(PeerConnectionIntegrationTest,
5391 PeerConnectionIntegrationTestWithFakeClock,
5392 Values(SdpSemantics::kPlanB,
5393 SdpSemantics::kUnifiedPlan));
5394
Steve Anton74255ff2018-01-24 18:32:57 -08005395// Tests that verify interoperability between Plan B and Unified Plan
5396// PeerConnections.
5397class PeerConnectionIntegrationInteropTest
Seth Hampson2f0d7022018-02-20 11:54:42 -08005398 : public PeerConnectionIntegrationBaseTest,
Steve Anton74255ff2018-01-24 18:32:57 -08005399 public ::testing::WithParamInterface<
5400 std::tuple<SdpSemantics, SdpSemantics>> {
5401 protected:
Seth Hampson2f0d7022018-02-20 11:54:42 -08005402 // Setting the SdpSemantics for the base test to kDefault does not matter
5403 // because we specify not to use the test semantics when creating
5404 // PeerConnectionWrappers.
Steve Anton74255ff2018-01-24 18:32:57 -08005405 PeerConnectionIntegrationInteropTest()
Steve Anton3acffc32018-04-12 17:21:03 -07005406 : PeerConnectionIntegrationBaseTest(SdpSemantics::kPlanB),
Seth Hampson2f0d7022018-02-20 11:54:42 -08005407 caller_semantics_(std::get<0>(GetParam())),
Steve Anton74255ff2018-01-24 18:32:57 -08005408 callee_semantics_(std::get<1>(GetParam())) {}
5409
5410 bool CreatePeerConnectionWrappersWithSemantics() {
Steve Anton3acffc32018-04-12 17:21:03 -07005411 return CreatePeerConnectionWrappersWithSdpSemantics(caller_semantics_,
5412 callee_semantics_);
Steve Anton74255ff2018-01-24 18:32:57 -08005413 }
5414
5415 const SdpSemantics caller_semantics_;
5416 const SdpSemantics callee_semantics_;
5417};
5418
5419TEST_P(PeerConnectionIntegrationInteropTest, NoMediaLocalToNoMediaRemote) {
5420 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5421 ConnectFakeSignaling();
5422
5423 caller()->CreateAndSetAndSignalOffer();
5424 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5425}
5426
5427TEST_P(PeerConnectionIntegrationInteropTest, OneAudioLocalToNoMediaRemote) {
5428 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5429 ConnectFakeSignaling();
5430 auto audio_sender = caller()->AddAudioTrack();
5431
5432 caller()->CreateAndSetAndSignalOffer();
5433 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5434
5435 // Verify that one audio receiver has been created on the remote and that it
5436 // has the same track ID as the sending track.
5437 auto receivers = callee()->pc()->GetReceivers();
5438 ASSERT_EQ(1u, receivers.size());
5439 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, receivers[0]->media_type());
5440 EXPECT_EQ(receivers[0]->track()->id(), audio_sender->track()->id());
5441
Seth Hampson2f0d7022018-02-20 11:54:42 -08005442 MediaExpectations media_expectations;
5443 media_expectations.CalleeExpectsSomeAudio();
5444 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005445}
5446
5447TEST_P(PeerConnectionIntegrationInteropTest, OneAudioOneVideoToNoMediaRemote) {
5448 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5449 ConnectFakeSignaling();
5450 auto video_sender = caller()->AddVideoTrack();
5451 auto audio_sender = caller()->AddAudioTrack();
5452
5453 caller()->CreateAndSetAndSignalOffer();
5454 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5455
5456 // Verify that one audio and one video receiver have been created on the
5457 // remote and that they have the same track IDs as the sending tracks.
5458 auto audio_receivers =
5459 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_AUDIO);
5460 ASSERT_EQ(1u, audio_receivers.size());
5461 EXPECT_EQ(audio_receivers[0]->track()->id(), audio_sender->track()->id());
5462 auto video_receivers =
5463 callee()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO);
5464 ASSERT_EQ(1u, video_receivers.size());
5465 EXPECT_EQ(video_receivers[0]->track()->id(), video_sender->track()->id());
5466
Seth Hampson2f0d7022018-02-20 11:54:42 -08005467 MediaExpectations media_expectations;
5468 media_expectations.CalleeExpectsSomeAudioAndVideo();
5469 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005470}
5471
5472TEST_P(PeerConnectionIntegrationInteropTest,
5473 OneAudioOneVideoLocalToOneAudioOneVideoRemote) {
5474 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5475 ConnectFakeSignaling();
5476 caller()->AddAudioVideoTracks();
5477 callee()->AddAudioVideoTracks();
5478
5479 caller()->CreateAndSetAndSignalOffer();
5480 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5481
Seth Hampson2f0d7022018-02-20 11:54:42 -08005482 MediaExpectations media_expectations;
5483 media_expectations.ExpectBidirectionalAudioAndVideo();
5484 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005485}
5486
5487TEST_P(PeerConnectionIntegrationInteropTest,
5488 ReverseRolesOneAudioLocalToOneVideoRemote) {
5489 ASSERT_TRUE(CreatePeerConnectionWrappersWithSemantics());
5490 ConnectFakeSignaling();
5491 caller()->AddAudioTrack();
5492 callee()->AddVideoTrack();
5493
5494 caller()->CreateAndSetAndSignalOffer();
5495 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5496
5497 // Verify that only the audio track has been negotiated.
5498 EXPECT_EQ(0u, caller()->GetReceiversOfType(cricket::MEDIA_TYPE_VIDEO).size());
5499 // Might also check that the callee's NegotiationNeeded flag is set.
5500
5501 // Reverse roles.
5502 callee()->CreateAndSetAndSignalOffer();
5503 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5504
Seth Hampson2f0d7022018-02-20 11:54:42 -08005505 MediaExpectations media_expectations;
5506 media_expectations.CallerExpectsSomeVideo();
5507 media_expectations.CalleeExpectsSomeAudio();
5508 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005509}
5510
Mirko Bonadeic84f6612019-01-31 12:20:57 +01005511INSTANTIATE_TEST_SUITE_P(
Steve Antonba42e992018-04-09 14:10:01 -07005512 PeerConnectionIntegrationTest,
5513 PeerConnectionIntegrationInteropTest,
5514 Values(std::make_tuple(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan),
5515 std::make_tuple(SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB)));
5516
5517// Test that if the Unified Plan side offers two video tracks then the Plan B
5518// side will only see the first one and ignore the second.
5519TEST_F(PeerConnectionIntegrationTestPlanB, TwoVideoUnifiedPlanToNoMediaPlanB) {
Steve Anton3acffc32018-04-12 17:21:03 -07005520 ASSERT_TRUE(CreatePeerConnectionWrappersWithSdpSemantics(
5521 SdpSemantics::kUnifiedPlan, SdpSemantics::kPlanB));
Steve Anton74255ff2018-01-24 18:32:57 -08005522 ConnectFakeSignaling();
5523 auto first_sender = caller()->AddVideoTrack();
5524 caller()->AddVideoTrack();
5525
5526 caller()->CreateAndSetAndSignalOffer();
5527 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5528
5529 // Verify that there is only one receiver and it corresponds to the first
5530 // added track.
5531 auto receivers = callee()->pc()->GetReceivers();
5532 ASSERT_EQ(1u, receivers.size());
5533 EXPECT_TRUE(receivers[0]->track()->enabled());
5534 EXPECT_EQ(first_sender->track()->id(), receivers[0]->track()->id());
5535
Seth Hampson2f0d7022018-02-20 11:54:42 -08005536 MediaExpectations media_expectations;
5537 media_expectations.CalleeExpectsSomeVideo();
5538 ASSERT_TRUE(ExpectNewFrames(media_expectations));
Steve Anton74255ff2018-01-24 18:32:57 -08005539}
5540
Steve Anton2bed3972019-01-04 17:04:30 -08005541// Test that if the initial offer tagged BUNDLE section is rejected due to its
5542// associated RtpTransceiver being stopped and another transceiver is added,
5543// then renegotiation causes the callee to receive the new video track without
5544// error.
5545// This is a regression test for bugs.webrtc.org/9954
5546TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5547 ReOfferWithStoppedBundleTaggedTransceiver) {
5548 RTCConfiguration config;
5549 config.bundle_policy = PeerConnectionInterface::kBundlePolicyMaxBundle;
5550 ASSERT_TRUE(CreatePeerConnectionWrappersWithConfig(config, config));
5551 ConnectFakeSignaling();
5552 auto audio_transceiver_or_error =
5553 caller()->pc()->AddTransceiver(caller()->CreateLocalAudioTrack());
5554 ASSERT_TRUE(audio_transceiver_or_error.ok());
5555 auto audio_transceiver = audio_transceiver_or_error.MoveValue();
5556
5557 caller()->CreateAndSetAndSignalOffer();
5558 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5559 {
5560 MediaExpectations media_expectations;
5561 media_expectations.CalleeExpectsSomeAudio();
5562 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5563 }
5564
Harald Alvestrand6060df52020-08-11 09:54:02 +02005565 audio_transceiver->StopInternal();
Steve Anton2bed3972019-01-04 17:04:30 -08005566 caller()->pc()->AddTransceiver(caller()->CreateLocalVideoTrack());
5567
5568 caller()->CreateAndSetAndSignalOffer();
5569 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5570 {
5571 MediaExpectations media_expectations;
5572 media_expectations.CalleeExpectsSomeVideo();
5573 ASSERT_TRUE(ExpectNewFrames(media_expectations));
5574 }
5575}
5576
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02005577#ifdef HAVE_SCTP
5578
5579TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5580 EndToEndCallWithBundledSctpDataChannel) {
5581 ASSERT_TRUE(CreatePeerConnectionWrappers());
5582 ConnectFakeSignaling();
5583 caller()->CreateDataChannel();
5584 caller()->AddAudioVideoTracks();
5585 callee()->AddAudioVideoTracks();
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02005586 caller()->CreateAndSetAndSignalOffer();
5587 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
Harald Alvestrand17ea0682019-12-13 11:51:04 +01005588 ASSERT_EQ_WAIT(SctpTransportState::kConnected,
5589 caller()->pc()->GetSctpTransport()->Information().state(),
5590 kDefaultTimeout);
5591 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5592 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
5593}
5594
5595TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5596 EndToEndCallWithDataChannelOnlyConnects) {
5597 ASSERT_TRUE(CreatePeerConnectionWrappers());
5598 ConnectFakeSignaling();
5599 caller()->CreateDataChannel();
5600 caller()->CreateAndSetAndSignalOffer();
5601 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5602 ASSERT_TRUE_WAIT(callee()->data_channel(), kDefaultTimeout);
5603 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
5604 ASSERT_TRUE(caller()->data_observer()->IsOpen());
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02005605}
5606
Harald Alvestrand2697ac12019-12-16 10:37:04 +01005607TEST_F(PeerConnectionIntegrationTestUnifiedPlan, DataChannelClosesWhenClosed) {
5608 ASSERT_TRUE(CreatePeerConnectionWrappers());
5609 ConnectFakeSignaling();
5610 caller()->CreateDataChannel();
5611 caller()->CreateAndSetAndSignalOffer();
5612 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5613 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
5614 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
5615 caller()->data_channel()->Close();
5616 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
5617}
5618
5619TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5620 DataChannelClosesWhenClosedReverse) {
5621 ASSERT_TRUE(CreatePeerConnectionWrappers());
5622 ConnectFakeSignaling();
5623 caller()->CreateDataChannel();
5624 caller()->CreateAndSetAndSignalOffer();
5625 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5626 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
5627 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
5628 callee()->data_channel()->Close();
5629 ASSERT_TRUE_WAIT(!caller()->data_observer()->IsOpen(), kDefaultTimeout);
5630}
5631
5632TEST_F(PeerConnectionIntegrationTestUnifiedPlan,
5633 DataChannelClosesWhenPeerConnectionClosed) {
5634 ASSERT_TRUE(CreatePeerConnectionWrappers());
5635 ConnectFakeSignaling();
5636 caller()->CreateDataChannel();
5637 caller()->CreateAndSetAndSignalOffer();
5638 ASSERT_TRUE_WAIT(SignalingStateStable(), kDefaultTimeout);
5639 ASSERT_TRUE_WAIT(callee()->data_observer(), kDefaultTimeout);
5640 ASSERT_TRUE_WAIT(callee()->data_observer()->IsOpen(), kDefaultTimeout);
5641 caller()->pc()->Close();
5642 ASSERT_TRUE_WAIT(!callee()->data_observer()->IsOpen(), kDefaultTimeout);
5643}
5644
Harald Alvestrandd61f2a72019-05-08 20:20:59 +02005645#endif // HAVE_SCTP
5646
deadbeef1dcb1642017-03-29 21:08:16 -07005647} // namespace
Mirko Bonadeiab64e8a2018-12-12 12:10:18 +01005648} // namespace webrtc
deadbeef1dcb1642017-03-29 21:08:16 -07005649
5650#endif // if !defined(THREAD_SANITIZER)